blob: 4bc11f186ddf126175f045f1c6c8b514952656a3 [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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard31e31b82003-02-18 22:55:36 +000018 */
Eduardo Habkostd5b3a9b2009-06-09 18:26:31 -030019#define _ATFILE_SOURCE
bellard31e31b82003-02-18 22:55:36 +000020#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
bellard04369ff2003-03-20 22:33:23 +000023#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000024#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
bellard7854b052003-03-29 17:22:23 +000029#include <time.h>
pbrook82e671d2008-06-09 12:10:22 +000030#include <limits.h>
bellard31e31b82003-02-18 22:55:36 +000031#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000032#include <sys/ipc.h>
33#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000034#include <sys/wait.h>
35#include <sys/time.h>
36#include <sys/stat.h>
37#include <sys/mount.h>
ths39b9aae2007-02-11 18:36:44 +000038#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000039#include <sys/resource.h>
40#include <sys/mman.h>
41#include <sys/swap.h>
42#include <signal.h>
43#include <sched.h>
Aurelien Jarno60e99242010-03-29 02:12:51 +020044#ifdef __ia64__
45int __clone2(int (*fn)(void *), void *child_stack_base,
46 size_t stack_size, int flags, void *arg, ...);
47#endif
bellard31e31b82003-02-18 22:55:36 +000048#include <sys/socket.h>
aurel32607175e2009-04-15 16:11:59 +000049#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000050#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000051#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000052#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000053#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000054#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000055#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000056#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000057#include <sys/sysinfo.h>
aurel323b3f24a2009-04-15 16:12:13 +000058#include <sys/utsname.h>
bellard72f03902003-02-18 23:33:18 +000059//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000060#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000061#include <netinet/tcp.h>
Laurent Vivier86fcd942011-03-30 01:35:23 +020062#include <linux/wireless.h>
Stefan Weil5a61cb62011-09-08 17:55:32 +020063#include "qemu-common.h"
Juan Quintela9788c9c2009-07-27 16:13:02 +020064#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +000065#include <sys/gmon.h>
66#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030067#ifdef CONFIG_EVENTFD
68#include <sys/eventfd.h>
69#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000070#ifdef CONFIG_EPOLL
71#include <sys/epoll.h>
72#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070073#ifdef CONFIG_ATTR
Avi Kivity4f26f2b2011-11-09 14:44:52 +020074#include "qemu-xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070075#endif
bellard31e31b82003-02-18 22:55:36 +000076
77#define termios host_termios
78#define winsize host_winsize
79#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000080#define sgttyb host_sgttyb /* same as target */
81#define tchars host_tchars /* same as target */
82#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000083
84#include <linux/termios.h>
85#include <linux/unistd.h>
86#include <linux/utsname.h>
87#include <linux/cdrom.h>
88#include <linux/hdreg.h>
89#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000090#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000091#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +030092#include <linux/fs.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000093#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +000094#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000095#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +020096#include <linux/fb.h>
97#include <linux/vt.h>
Alexander Graf56e904e2012-01-31 18:42:06 +010098#include <linux/dm-ioctl.h>
pbrookd7e40362008-05-23 16:06:43 +000099#include "linux_loop.h"
Loïc Minierda790302009-12-29 22:06:13 +0100100#include "cpu-uname.h"
bellard31e31b82003-02-18 22:55:36 +0000101
bellard3ef693a2003-03-23 20:17:16 +0000102#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000103
Juan Quintela2f7bb872009-07-27 16:13:24 +0200104#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +0000105#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
106 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
107#else
108/* XXX: Hardcode the above values. */
109#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +0000110#endif
111
bellard72f03902003-02-18 23:33:18 +0000112//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000113
bellard1a9353d2003-03-16 20:28:50 +0000114//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000115#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
116#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000117
bellard70a194b2003-08-11 22:20:16 +0000118
bellard70a194b2003-08-11 22:20:16 +0000119#undef _syscall0
120#undef _syscall1
121#undef _syscall2
122#undef _syscall3
123#undef _syscall4
124#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000125#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000126
bellard83fcb512006-06-14 13:37:16 +0000127#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000128static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000129{ \
130 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000131}
132
bellard83fcb512006-06-14 13:37:16 +0000133#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000134static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000135{ \
136 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000137}
138
bellard83fcb512006-06-14 13:37:16 +0000139#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000140static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000141{ \
142 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000143}
144
bellard83fcb512006-06-14 13:37:16 +0000145#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000146static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000147{ \
148 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000149}
150
bellard83fcb512006-06-14 13:37:16 +0000151#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000152static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000153{ \
154 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000155}
156
bellard83fcb512006-06-14 13:37:16 +0000157#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
158 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000159static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000160{ \
161 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000162}
bellard83fcb512006-06-14 13:37:16 +0000163
164
165#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
166 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000167static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
168 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000169{ \
170 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
171}
172
bellard70a194b2003-08-11 22:20:16 +0000173
bellard31e31b82003-02-18 22:55:36 +0000174#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000175#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000176#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000177#define __NR_sys_fchownat __NR_fchownat
balrog6a24a772008-09-20 02:23:36 +0000178#define __NR_sys_fstatat64 __NR_fstatat64
balrogac8a6552008-09-20 02:25:39 +0000179#define __NR_sys_futimesat __NR_futimesat
bellard72f03902003-02-18 23:33:18 +0000180#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000181#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000182#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000183#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000184#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000185#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000186#define __NR_sys_mknodat __NR_mknodat
aurel329d33b762009-04-08 23:07:05 +0000187#define __NR_sys_newfstatat __NR_newfstatat
ths82424832007-09-24 09:21:55 +0000188#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000189#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000190#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000191#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000192#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000193#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000194#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000195#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000196#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000197#define __NR_sys_utimensat __NR_utimensat
pbrookbd0c5662008-05-29 14:34:11 +0000198#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000199#define __NR_sys_inotify_init __NR_inotify_init
200#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
201#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000202
Alexander Graf42a39fb2011-04-15 17:32:45 +0200203#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
204 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000205#define __NR__llseek __NR_lseek
206#endif
207
bellard72f03902003-02-18 23:33:18 +0000208#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000209_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000210#else
ths0da46a62007-10-20 20:23:07 +0000211/* This is a replacement for the host gettid() and must return a host
212 errno. */
bellard72f03902003-02-18 23:33:18 +0000213static int gettid(void) {
214 return -ENOSYS;
215}
216#endif
aurel323b3f24a2009-04-15 16:12:13 +0000217_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
aurel323b3f24a2009-04-15 16:12:13 +0000218#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
219_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
220#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700221#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000222_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
223 loff_t *, res, uint, wh);
224#endif
225_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
226_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
227#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
228_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
229#endif
230#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
231_syscall2(int,sys_tkill,int,tid,int,sig)
232#endif
233#ifdef __NR_exit_group
234_syscall1(int,exit_group,int,error_code)
235#endif
236#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
237_syscall1(int,set_tid_address,int *,tidptr)
238#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +0200239#if defined(CONFIG_USE_NPTL)
aurel323b3f24a2009-04-15 16:12:13 +0000240#if defined(TARGET_NR_futex) && defined(__NR_futex)
241_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
242 const struct timespec *,timeout,int *,uaddr2,int,val3)
243#endif
244#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500245#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
246_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
247 unsigned long *, user_mask_ptr);
248#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
249_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
250 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200251_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
252 void *, arg);
aurel323b3f24a2009-04-15 16:12:13 +0000253
254static bitmask_transtbl fcntl_flags_tbl[] = {
255 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
256 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
257 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
258 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
259 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
260 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
261 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
262 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700263 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000264 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
265 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
266 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
267 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000268#if defined(O_DIRECT)
269 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
270#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700271#if defined(O_NOATIME)
272 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
273#endif
274#if defined(O_CLOEXEC)
275 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
276#endif
277#if defined(O_PATH)
278 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
279#endif
280 /* Don't terminate the list prematurely on 64-bit host+guest. */
281#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
282 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
283#endif
aurel323b3f24a2009-04-15 16:12:13 +0000284 { 0, 0, 0, 0 }
285};
286
287#define COPY_UTSNAME_FIELD(dest, src) \
288 do { \
289 /* __NEW_UTS_LEN doesn't include terminating null */ \
290 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
291 (dest)[__NEW_UTS_LEN] = '\0'; \
292 } while (0)
293
294static int sys_uname(struct new_utsname *buf)
295{
296 struct utsname uts_buf;
297
298 if (uname(&uts_buf) < 0)
299 return (-1);
300
301 /*
302 * Just in case these have some differences, we
303 * translate utsname to new_utsname (which is the
304 * struct linux kernel uses).
305 */
306
Stefan Weil67bd9ed2011-05-07 22:32:25 +0200307 memset(buf, 0, sizeof(*buf));
aurel323b3f24a2009-04-15 16:12:13 +0000308 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
309 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
310 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
311 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
312 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
313#ifdef _GNU_SOURCE
314 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
315#endif
316 return (0);
317
318#undef COPY_UTSNAME_FIELD
319}
320
321static int sys_getcwd1(char *buf, size_t size)
322{
323 if (getcwd(buf, size) == NULL) {
324 /* getcwd() sets errno */
325 return (-1);
326 }
aurel32aaf4ad32009-04-16 14:17:14 +0000327 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000328}
329
330#ifdef CONFIG_ATFILE
331/*
332 * Host system seems to have atfile syscall stubs available. We
333 * now enable them one by one as specified by target syscall_nr.h.
334 */
335
336#ifdef TARGET_NR_faccessat
aurel32465c9f02009-04-19 08:52:17 +0000337static int sys_faccessat(int dirfd, const char *pathname, int mode)
aurel323b3f24a2009-04-15 16:12:13 +0000338{
aurel32465c9f02009-04-19 08:52:17 +0000339 return (faccessat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000340}
341#endif
342#ifdef TARGET_NR_fchmodat
aurel32465c9f02009-04-19 08:52:17 +0000343static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000344{
aurel32465c9f02009-04-19 08:52:17 +0000345 return (fchmodat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000346}
347#endif
Riku Voipio0c866a72011-04-18 15:23:06 +0300348#if defined(TARGET_NR_fchownat)
aurel323b3f24a2009-04-15 16:12:13 +0000349static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
350 gid_t group, int flags)
351{
352 return (fchownat(dirfd, pathname, owner, group, flags));
353}
354#endif
355#ifdef __NR_fstatat64
356static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
357 int flags)
358{
359 return (fstatat(dirfd, pathname, buf, flags));
360}
361#endif
362#ifdef __NR_newfstatat
363static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
364 int flags)
365{
366 return (fstatat(dirfd, pathname, buf, flags));
367}
368#endif
369#ifdef TARGET_NR_futimesat
370static int sys_futimesat(int dirfd, const char *pathname,
371 const struct timeval times[2])
372{
373 return (futimesat(dirfd, pathname, times));
374}
375#endif
376#ifdef TARGET_NR_linkat
377static int sys_linkat(int olddirfd, const char *oldpath,
378 int newdirfd, const char *newpath, int flags)
379{
380 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
381}
382#endif
383#ifdef TARGET_NR_mkdirat
384static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
385{
386 return (mkdirat(dirfd, pathname, mode));
387}
388#endif
389#ifdef TARGET_NR_mknodat
390static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
391 dev_t dev)
392{
393 return (mknodat(dirfd, pathname, mode, dev));
394}
395#endif
396#ifdef TARGET_NR_openat
Alexander Graff4c69012011-09-25 06:25:35 +0200397static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000398{
399 /*
400 * open(2) has extra parameter 'mode' when called with
401 * flag O_CREAT.
402 */
403 if ((flags & O_CREAT) != 0) {
aurel323b3f24a2009-04-15 16:12:13 +0000404 return (openat(dirfd, pathname, flags, mode));
405 }
406 return (openat(dirfd, pathname, flags));
407}
408#endif
409#ifdef TARGET_NR_readlinkat
410static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
411{
412 return (readlinkat(dirfd, pathname, buf, bufsiz));
413}
414#endif
415#ifdef TARGET_NR_renameat
416static int sys_renameat(int olddirfd, const char *oldpath,
417 int newdirfd, const char *newpath)
418{
419 return (renameat(olddirfd, oldpath, newdirfd, newpath));
420}
421#endif
422#ifdef TARGET_NR_symlinkat
423static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
424{
425 return (symlinkat(oldpath, newdirfd, newpath));
426}
427#endif
428#ifdef TARGET_NR_unlinkat
429static int sys_unlinkat(int dirfd, const char *pathname, int flags)
430{
431 return (unlinkat(dirfd, pathname, flags));
432}
433#endif
aurel323b3f24a2009-04-15 16:12:13 +0000434#else /* !CONFIG_ATFILE */
435
436/*
437 * Try direct syscalls instead
438 */
ths92a34c12007-09-24 09:27:49 +0000439#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
aurel32465c9f02009-04-19 08:52:17 +0000440_syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
ths92a34c12007-09-24 09:27:49 +0000441#endif
ths814d7972007-09-24 09:26:51 +0000442#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
aurel32465c9f02009-04-19 08:52:17 +0000443_syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
ths814d7972007-09-24 09:26:51 +0000444#endif
Riku Voipio0c866a72011-04-18 15:23:06 +0300445#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +0000446_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
447 uid_t,owner,gid_t,group,int,flags)
448#endif
aurel329d33b762009-04-08 23:07:05 +0000449#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
450 defined(__NR_fstatat64)
balrog6a24a772008-09-20 02:23:36 +0000451_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
452 struct stat *,buf,int,flags)
453#endif
balrogac8a6552008-09-20 02:25:39 +0000454#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
455_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
456 const struct timeval *,times)
457#endif
aurel323b3f24a2009-04-15 16:12:13 +0000458#if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
459 defined(__NR_newfstatat)
460_syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
461 struct stat *,buf,int,flags)
blueswir18fcd3692008-08-17 20:26:25 +0000462#endif
ths64f0ce42007-09-24 09:25:06 +0000463#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
464_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
aurel323b3f24a2009-04-15 16:12:13 +0000465 int,newdirfd,const char *,newpath,int,flags)
ths64f0ce42007-09-24 09:25:06 +0000466#endif
ths4472ad02007-09-24 09:22:32 +0000467#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
468_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
469#endif
ths75ac37a2007-09-24 09:23:05 +0000470#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
471_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
472 mode_t,mode,dev_t,dev)
473#endif
ths82424832007-09-24 09:21:55 +0000474#if defined(TARGET_NR_openat) && defined(__NR_openat)
475_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
476#endif
ths5e0ccb12007-09-24 09:26:10 +0000477#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
478_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
479 char *,buf,size_t,bufsize)
480#endif
ths722183f2007-09-24 09:24:37 +0000481#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
482_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
483 int,newdirfd,const char *,newpath)
484#endif
thsb51eaa82007-09-25 16:09:22 +0000485#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000486_syscall3(int,sys_symlinkat,const char *,oldpath,
487 int,newdirfd,const char *,newpath)
488#endif
ths8170f562007-09-24 09:24:11 +0000489#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
490_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
491#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300492
493#endif /* CONFIG_ATFILE */
494
495#ifdef CONFIG_UTIMENSAT
496static int sys_utimensat(int dirfd, const char *pathname,
497 const struct timespec times[2], int flags)
498{
499 if (pathname == NULL)
500 return futimens(dirfd, times);
501 else
502 return utimensat(dirfd, pathname, times, flags);
503}
504#else
ths9007f0e2007-09-25 17:50:37 +0000505#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
506_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
507 const struct timespec *,tsp,int,flags)
508#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300509#endif /* CONFIG_UTIMENSAT */
aurel323b3f24a2009-04-15 16:12:13 +0000510
511#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000512#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000513
aurel3239b59762008-10-01 21:46:50 +0000514#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000515static int sys_inotify_init(void)
516{
517 return (inotify_init());
518}
aurel3239b59762008-10-01 21:46:50 +0000519#endif
520#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000521static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
522{
523 return (inotify_add_watch(fd, pathname, mask));
524}
aurel3239b59762008-10-01 21:46:50 +0000525#endif
526#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000527static int sys_inotify_rm_watch(int fd, int32_t wd)
528{
aurel328690e422009-04-17 13:50:32 +0000529 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000530}
aurel3239b59762008-10-01 21:46:50 +0000531#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000532#ifdef CONFIG_INOTIFY1
533#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
534static int sys_inotify_init1(int flags)
535{
536 return (inotify_init1(flags));
537}
538#endif
539#endif
aurel323b3f24a2009-04-15 16:12:13 +0000540#else
541/* Userspace can usually survive runtime without inotify */
542#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000543#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000544#undef TARGET_NR_inotify_add_watch
545#undef TARGET_NR_inotify_rm_watch
546#endif /* CONFIG_INOTIFY */
547
Mike Frysingerd8035d42011-02-07 01:05:51 -0500548#if defined(TARGET_NR_ppoll)
549#ifndef __NR_ppoll
550# define __NR_ppoll -1
551#endif
552#define __NR_sys_ppoll __NR_ppoll
553_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
554 struct timespec *, timeout, const __sigset_t *, sigmask,
555 size_t, sigsetsize)
556#endif
bellard66fb9762003-03-23 01:06:05 +0000557
Mike Frysinger055e0902011-06-03 17:01:49 -0400558#if defined(TARGET_NR_pselect6)
559#ifndef __NR_pselect6
560# define __NR_pselect6 -1
561#endif
562#define __NR_sys_pselect6 __NR_pselect6
563_syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
564 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
565#endif
566
Peter Maydell163a05a2011-06-27 17:44:52 +0100567#if defined(TARGET_NR_prlimit64)
568#ifndef __NR_prlimit64
569# define __NR_prlimit64 -1
570#endif
571#define __NR_sys_prlimit64 __NR_prlimit64
572/* The glibc rlimit structure may not be that used by the underlying syscall */
573struct host_rlimit64 {
574 uint64_t rlim_cur;
575 uint64_t rlim_max;
576};
577_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
578 const struct host_rlimit64 *, new_limit,
579 struct host_rlimit64 *, old_limit)
580#endif
581
bellard66fb9762003-03-23 01:06:05 +0000582extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000583extern int flock(int, int);
584extern int setfsuid(int);
585extern int setfsgid(int);
bellard19b84f32003-05-08 15:41:49 +0000586extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000587
Riku Voipio48e515d2011-07-12 15:40:51 +0300588/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
589#ifdef TARGET_ARM
590static inline int regpairs_aligned(void *cpu_env) {
591 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
592}
593#elif defined(TARGET_MIPS)
594static inline int regpairs_aligned(void *cpu_env) { return 1; }
595#else
596static inline int regpairs_aligned(void *cpu_env) { return 0; }
597#endif
598
thsb92c47c2007-11-01 00:07:38 +0000599#define ERRNO_TABLE_SIZE 1200
600
601/* target_to_host_errno_table[] is initialized from
602 * host_to_target_errno_table[] in syscall_init(). */
603static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
604};
605
ths637947f2007-06-01 12:09:19 +0000606/*
thsfe8f0962007-07-12 10:59:21 +0000607 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000608 * minus the errnos that are not actually generic to all archs.
609 */
thsb92c47c2007-11-01 00:07:38 +0000610static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000611 [EIDRM] = TARGET_EIDRM,
612 [ECHRNG] = TARGET_ECHRNG,
613 [EL2NSYNC] = TARGET_EL2NSYNC,
614 [EL3HLT] = TARGET_EL3HLT,
615 [EL3RST] = TARGET_EL3RST,
616 [ELNRNG] = TARGET_ELNRNG,
617 [EUNATCH] = TARGET_EUNATCH,
618 [ENOCSI] = TARGET_ENOCSI,
619 [EL2HLT] = TARGET_EL2HLT,
620 [EDEADLK] = TARGET_EDEADLK,
621 [ENOLCK] = TARGET_ENOLCK,
622 [EBADE] = TARGET_EBADE,
623 [EBADR] = TARGET_EBADR,
624 [EXFULL] = TARGET_EXFULL,
625 [ENOANO] = TARGET_ENOANO,
626 [EBADRQC] = TARGET_EBADRQC,
627 [EBADSLT] = TARGET_EBADSLT,
628 [EBFONT] = TARGET_EBFONT,
629 [ENOSTR] = TARGET_ENOSTR,
630 [ENODATA] = TARGET_ENODATA,
631 [ETIME] = TARGET_ETIME,
632 [ENOSR] = TARGET_ENOSR,
633 [ENONET] = TARGET_ENONET,
634 [ENOPKG] = TARGET_ENOPKG,
635 [EREMOTE] = TARGET_EREMOTE,
636 [ENOLINK] = TARGET_ENOLINK,
637 [EADV] = TARGET_EADV,
638 [ESRMNT] = TARGET_ESRMNT,
639 [ECOMM] = TARGET_ECOMM,
640 [EPROTO] = TARGET_EPROTO,
641 [EDOTDOT] = TARGET_EDOTDOT,
642 [EMULTIHOP] = TARGET_EMULTIHOP,
643 [EBADMSG] = TARGET_EBADMSG,
644 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
645 [EOVERFLOW] = TARGET_EOVERFLOW,
646 [ENOTUNIQ] = TARGET_ENOTUNIQ,
647 [EBADFD] = TARGET_EBADFD,
648 [EREMCHG] = TARGET_EREMCHG,
649 [ELIBACC] = TARGET_ELIBACC,
650 [ELIBBAD] = TARGET_ELIBBAD,
651 [ELIBSCN] = TARGET_ELIBSCN,
652 [ELIBMAX] = TARGET_ELIBMAX,
653 [ELIBEXEC] = TARGET_ELIBEXEC,
654 [EILSEQ] = TARGET_EILSEQ,
655 [ENOSYS] = TARGET_ENOSYS,
656 [ELOOP] = TARGET_ELOOP,
657 [ERESTART] = TARGET_ERESTART,
658 [ESTRPIPE] = TARGET_ESTRPIPE,
659 [ENOTEMPTY] = TARGET_ENOTEMPTY,
660 [EUSERS] = TARGET_EUSERS,
661 [ENOTSOCK] = TARGET_ENOTSOCK,
662 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
663 [EMSGSIZE] = TARGET_EMSGSIZE,
664 [EPROTOTYPE] = TARGET_EPROTOTYPE,
665 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
666 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
667 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
668 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
669 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
670 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
671 [EADDRINUSE] = TARGET_EADDRINUSE,
672 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
673 [ENETDOWN] = TARGET_ENETDOWN,
674 [ENETUNREACH] = TARGET_ENETUNREACH,
675 [ENETRESET] = TARGET_ENETRESET,
676 [ECONNABORTED] = TARGET_ECONNABORTED,
677 [ECONNRESET] = TARGET_ECONNRESET,
678 [ENOBUFS] = TARGET_ENOBUFS,
679 [EISCONN] = TARGET_EISCONN,
680 [ENOTCONN] = TARGET_ENOTCONN,
681 [EUCLEAN] = TARGET_EUCLEAN,
682 [ENOTNAM] = TARGET_ENOTNAM,
683 [ENAVAIL] = TARGET_ENAVAIL,
684 [EISNAM] = TARGET_EISNAM,
685 [EREMOTEIO] = TARGET_EREMOTEIO,
686 [ESHUTDOWN] = TARGET_ESHUTDOWN,
687 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
688 [ETIMEDOUT] = TARGET_ETIMEDOUT,
689 [ECONNREFUSED] = TARGET_ECONNREFUSED,
690 [EHOSTDOWN] = TARGET_EHOSTDOWN,
691 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
692 [EALREADY] = TARGET_EALREADY,
693 [EINPROGRESS] = TARGET_EINPROGRESS,
694 [ESTALE] = TARGET_ESTALE,
695 [ECANCELED] = TARGET_ECANCELED,
696 [ENOMEDIUM] = TARGET_ENOMEDIUM,
697 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000698#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000699 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000700#endif
701#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000702 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000703#endif
704#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000705 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000706#endif
707#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000708 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000709#endif
710#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000711 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000712#endif
713#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000714 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000715#endif
thsb92c47c2007-11-01 00:07:38 +0000716};
ths637947f2007-06-01 12:09:19 +0000717
718static inline int host_to_target_errno(int err)
719{
720 if(host_to_target_errno_table[err])
721 return host_to_target_errno_table[err];
722 return err;
723}
724
thsb92c47c2007-11-01 00:07:38 +0000725static inline int target_to_host_errno(int err)
726{
727 if (target_to_host_errno_table[err])
728 return target_to_host_errno_table[err];
729 return err;
730}
731
blueswir1992f48a2007-10-14 16:27:31 +0000732static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000733{
734 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000735 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000736 else
737 return ret;
738}
739
blueswir1992f48a2007-10-14 16:27:31 +0000740static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000741{
blueswir1992f48a2007-10-14 16:27:31 +0000742 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000743}
744
thsb92c47c2007-11-01 00:07:38 +0000745char *target_strerror(int err)
746{
Alexander Graf962b2892011-11-21 12:04:07 +0100747 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
748 return NULL;
749 }
thsb92c47c2007-11-01 00:07:38 +0000750 return strerror(target_to_host_errno(err));
751}
752
blueswir1992f48a2007-10-14 16:27:31 +0000753static abi_ulong target_brk;
754static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000755static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000756
blueswir1992f48a2007-10-14 16:27:31 +0000757void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000758{
blueswir14c1de732007-07-07 20:45:44 +0000759 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000760 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000761}
762
vincent4d1de872011-06-14 21:56:33 +0000763//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
764#define DEBUGF_BRK(message, args...)
765
ths0da46a62007-10-20 20:23:07 +0000766/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000767abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000768{
blueswir1992f48a2007-10-14 16:27:31 +0000769 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000770 int new_alloc_size;
771
Paul Brook3a0c6c42012-02-09 19:04:27 +0000772 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000773
vincent4d1de872011-06-14 21:56:33 +0000774 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000775 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000776 return target_brk;
777 }
778 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000779 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
780 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000781 return target_brk;
782 }
bellard31e31b82003-02-18 22:55:36 +0000783
vincent4d1de872011-06-14 21:56:33 +0000784 /* If the new brk is less than the highest page reserved to the
785 * target heap allocation, set it and we're almost done... */
786 if (new_brk <= brk_page) {
787 /* Heap contents are initialized to zero, as for anonymous
788 * mapped pages. */
789 if (new_brk > target_brk) {
790 memset(g2h(target_brk), 0, new_brk - target_brk);
791 }
bellard31e31b82003-02-18 22:55:36 +0000792 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000793 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000794 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000795 }
796
Peter Maydell00faf082011-04-18 16:34:24 +0100797 /* We need to allocate more memory after the brk... Note that
798 * we don't use MAP_FIXED because that will map over the top of
799 * any existing mapping (like the one with the host libc or qemu
800 * itself); instead we treat "mapped but at wrong address" as
801 * a failure and unmap again.
802 */
vincent4d1de872011-06-14 21:56:33 +0000803 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000804 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000805 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100806 MAP_ANON|MAP_PRIVATE, 0, 0));
807
808 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200809 /* Heap contents are initialized to zero, as for anonymous
810 * mapped pages. Technically the new pages are already
811 * initialized to zero since they *are* anonymous mapped
812 * pages, however we have to take care with the contents that
813 * come from the remaining part of the previous page: it may
814 * contains garbage data due to a previous heap usage (grown
815 * then shrunken). */
816 memset(g2h(target_brk), 0, brk_page - target_brk);
817
Peter Maydell00faf082011-04-18 16:34:24 +0100818 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000819 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000820 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
821 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100822 return target_brk;
823 } else if (mapped_addr != -1) {
824 /* Mapped but at wrong address, meaning there wasn't actually
825 * enough space for this brk.
826 */
827 target_munmap(mapped_addr, new_alloc_size);
828 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000829 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000830 }
831 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000832 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100833 }
balrog7ab240a2008-04-26 12:17:34 +0000834
Richard Henderson7dd46c02010-05-03 10:07:49 -0700835#if defined(TARGET_ALPHA)
836 /* We (partially) emulate OSF/1 on Alpha, which requires we
837 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100838 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700839#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100840 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000841 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000842}
843
ths26edcf42007-12-09 02:25:24 +0000844static inline abi_long copy_from_user_fdset(fd_set *fds,
845 abi_ulong target_fds_addr,
846 int n)
bellard31e31b82003-02-18 22:55:36 +0000847{
ths26edcf42007-12-09 02:25:24 +0000848 int i, nw, j, k;
849 abi_ulong b, *target_fds;
850
851 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
852 if (!(target_fds = lock_user(VERIFY_READ,
853 target_fds_addr,
854 sizeof(abi_ulong) * nw,
855 1)))
856 return -TARGET_EFAULT;
857
858 FD_ZERO(fds);
859 k = 0;
860 for (i = 0; i < nw; i++) {
861 /* grab the abi_ulong */
862 __get_user(b, &target_fds[i]);
863 for (j = 0; j < TARGET_ABI_BITS; j++) {
864 /* check the bit inside the abi_ulong */
865 if ((b >> j) & 1)
866 FD_SET(k, fds);
867 k++;
bellard31e31b82003-02-18 22:55:36 +0000868 }
bellard31e31b82003-02-18 22:55:36 +0000869 }
ths26edcf42007-12-09 02:25:24 +0000870
871 unlock_user(target_fds, target_fds_addr, 0);
872
873 return 0;
bellard31e31b82003-02-18 22:55:36 +0000874}
875
Mike Frysinger055e0902011-06-03 17:01:49 -0400876static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
877 abi_ulong target_fds_addr,
878 int n)
879{
880 if (target_fds_addr) {
881 if (copy_from_user_fdset(fds, target_fds_addr, n))
882 return -TARGET_EFAULT;
883 *fds_ptr = fds;
884 } else {
885 *fds_ptr = NULL;
886 }
887 return 0;
888}
889
ths26edcf42007-12-09 02:25:24 +0000890static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
891 const fd_set *fds,
892 int n)
bellard31e31b82003-02-18 22:55:36 +0000893{
bellard31e31b82003-02-18 22:55:36 +0000894 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000895 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000896 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000897
ths26edcf42007-12-09 02:25:24 +0000898 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
899 if (!(target_fds = lock_user(VERIFY_WRITE,
900 target_fds_addr,
901 sizeof(abi_ulong) * nw,
902 0)))
903 return -TARGET_EFAULT;
904
905 k = 0;
906 for (i = 0; i < nw; i++) {
907 v = 0;
908 for (j = 0; j < TARGET_ABI_BITS; j++) {
909 v |= ((FD_ISSET(k, fds) != 0) << j);
910 k++;
bellard31e31b82003-02-18 22:55:36 +0000911 }
ths26edcf42007-12-09 02:25:24 +0000912 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000913 }
ths26edcf42007-12-09 02:25:24 +0000914
915 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
916
917 return 0;
bellard31e31b82003-02-18 22:55:36 +0000918}
919
bellardc596ed12003-07-13 17:32:31 +0000920#if defined(__alpha__)
921#define HOST_HZ 1024
922#else
923#define HOST_HZ 100
924#endif
925
blueswir1992f48a2007-10-14 16:27:31 +0000926static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000927{
928#if HOST_HZ == TARGET_HZ
929 return ticks;
930#else
931 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
932#endif
933}
934
bellard579a97f2007-11-11 14:26:47 +0000935static inline abi_long host_to_target_rusage(abi_ulong target_addr,
936 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000937{
pbrook53a59602006-03-25 19:31:22 +0000938 struct target_rusage *target_rusage;
939
bellard579a97f2007-11-11 14:26:47 +0000940 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
941 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200942 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
943 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
944 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
945 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
946 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
947 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
948 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
949 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
950 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
951 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
952 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
953 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
954 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
955 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
956 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
957 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
958 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
959 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000960 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000961
962 return 0;
bellardb4091862003-05-16 15:39:34 +0000963}
964
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200965static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900966{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200967 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300968 rlim_t result;
969
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200970 target_rlim_swap = tswapal(target_rlim);
971 if (target_rlim_swap == TARGET_RLIM_INFINITY)
972 return RLIM_INFINITY;
973
974 result = target_rlim_swap;
975 if (target_rlim_swap != (rlim_t)result)
976 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300977
978 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900979}
980
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200981static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900982{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200983 abi_ulong target_rlim_swap;
984 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300985
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200986 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300987 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900988 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300989 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200990 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300991
992 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900993}
994
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +0300995static inline int target_to_host_resource(int code)
996{
997 switch (code) {
998 case TARGET_RLIMIT_AS:
999 return RLIMIT_AS;
1000 case TARGET_RLIMIT_CORE:
1001 return RLIMIT_CORE;
1002 case TARGET_RLIMIT_CPU:
1003 return RLIMIT_CPU;
1004 case TARGET_RLIMIT_DATA:
1005 return RLIMIT_DATA;
1006 case TARGET_RLIMIT_FSIZE:
1007 return RLIMIT_FSIZE;
1008 case TARGET_RLIMIT_LOCKS:
1009 return RLIMIT_LOCKS;
1010 case TARGET_RLIMIT_MEMLOCK:
1011 return RLIMIT_MEMLOCK;
1012 case TARGET_RLIMIT_MSGQUEUE:
1013 return RLIMIT_MSGQUEUE;
1014 case TARGET_RLIMIT_NICE:
1015 return RLIMIT_NICE;
1016 case TARGET_RLIMIT_NOFILE:
1017 return RLIMIT_NOFILE;
1018 case TARGET_RLIMIT_NPROC:
1019 return RLIMIT_NPROC;
1020 case TARGET_RLIMIT_RSS:
1021 return RLIMIT_RSS;
1022 case TARGET_RLIMIT_RTPRIO:
1023 return RLIMIT_RTPRIO;
1024 case TARGET_RLIMIT_SIGPENDING:
1025 return RLIMIT_SIGPENDING;
1026 case TARGET_RLIMIT_STACK:
1027 return RLIMIT_STACK;
1028 default:
1029 return code;
1030 }
1031}
1032
ths788f5ec2007-12-09 02:37:05 +00001033static inline abi_long copy_from_user_timeval(struct timeval *tv,
1034 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001035{
pbrook53a59602006-03-25 19:31:22 +00001036 struct target_timeval *target_tv;
1037
ths788f5ec2007-12-09 02:37:05 +00001038 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +00001039 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001040
1041 __get_user(tv->tv_sec, &target_tv->tv_sec);
1042 __get_user(tv->tv_usec, &target_tv->tv_usec);
1043
1044 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001045
1046 return 0;
bellard31e31b82003-02-18 22:55:36 +00001047}
1048
ths788f5ec2007-12-09 02:37:05 +00001049static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1050 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +00001051{
pbrook53a59602006-03-25 19:31:22 +00001052 struct target_timeval *target_tv;
1053
ths788f5ec2007-12-09 02:37:05 +00001054 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +00001055 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001056
1057 __put_user(tv->tv_sec, &target_tv->tv_sec);
1058 __put_user(tv->tv_usec, &target_tv->tv_usec);
1059
1060 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001061
1062 return 0;
bellard31e31b82003-02-18 22:55:36 +00001063}
1064
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001065#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1066#include <mqueue.h>
1067
aurel3224e10032009-04-15 16:11:43 +00001068static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1069 abi_ulong target_mq_attr_addr)
1070{
1071 struct target_mq_attr *target_mq_attr;
1072
1073 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1074 target_mq_attr_addr, 1))
1075 return -TARGET_EFAULT;
1076
1077 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1078 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1079 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1080 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1081
1082 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1083
1084 return 0;
1085}
1086
1087static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1088 const struct mq_attr *attr)
1089{
1090 struct target_mq_attr *target_mq_attr;
1091
1092 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1093 target_mq_attr_addr, 0))
1094 return -TARGET_EFAULT;
1095
1096 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1097 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1098 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1099 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1100
1101 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1102
1103 return 0;
1104}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001105#endif
bellard31e31b82003-02-18 22:55:36 +00001106
Mike Frysinger055e0902011-06-03 17:01:49 -04001107#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001108/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001109static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001110 abi_ulong rfd_addr, abi_ulong wfd_addr,
1111 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001112{
1113 fd_set rfds, wfds, efds;
1114 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1115 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001116 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001117
Mike Frysinger055e0902011-06-03 17:01:49 -04001118 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1119 if (ret) {
1120 return ret;
pbrook53a59602006-03-25 19:31:22 +00001121 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001122 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1123 if (ret) {
1124 return ret;
pbrook53a59602006-03-25 19:31:22 +00001125 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001126 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1127 if (ret) {
1128 return ret;
pbrook53a59602006-03-25 19:31:22 +00001129 }
ths3b46e622007-09-17 08:09:54 +00001130
ths26edcf42007-12-09 02:25:24 +00001131 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001132 if (copy_from_user_timeval(&tv, target_tv_addr))
1133 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001134 tv_ptr = &tv;
1135 } else {
1136 tv_ptr = NULL;
1137 }
ths26edcf42007-12-09 02:25:24 +00001138
bellard31e31b82003-02-18 22:55:36 +00001139 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +00001140
ths26edcf42007-12-09 02:25:24 +00001141 if (!is_error(ret)) {
1142 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1143 return -TARGET_EFAULT;
1144 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1145 return -TARGET_EFAULT;
1146 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1147 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001148
ths788f5ec2007-12-09 02:37:05 +00001149 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1150 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001151 }
bellard579a97f2007-11-11 14:26:47 +00001152
bellard31e31b82003-02-18 22:55:36 +00001153 return ret;
1154}
Mike Frysinger055e0902011-06-03 17:01:49 -04001155#endif
bellard31e31b82003-02-18 22:55:36 +00001156
Riku Voipio099d6b02009-05-05 12:10:04 +03001157static abi_long do_pipe2(int host_pipe[], int flags)
1158{
1159#ifdef CONFIG_PIPE2
1160 return pipe2(host_pipe, flags);
1161#else
1162 return -ENOSYS;
1163#endif
1164}
1165
Richard Hendersonfb41a662010-05-03 10:07:52 -07001166static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1167 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001168{
1169 int host_pipe[2];
1170 abi_long ret;
1171 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1172
1173 if (is_error(ret))
1174 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001175
1176 /* Several targets have special calling conventions for the original
1177 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1178 if (!is_pipe2) {
1179#if defined(TARGET_ALPHA)
1180 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1181 return host_pipe[0];
1182#elif defined(TARGET_MIPS)
1183 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1184 return host_pipe[0];
1185#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001186 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001187 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001188#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001189 }
1190
Riku Voipio099d6b02009-05-05 12:10:04 +03001191 if (put_user_s32(host_pipe[0], pipedes)
1192 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1193 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001194 return get_errno(ret);
1195}
1196
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001197static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1198 abi_ulong target_addr,
1199 socklen_t len)
1200{
1201 struct target_ip_mreqn *target_smreqn;
1202
1203 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1204 if (!target_smreqn)
1205 return -TARGET_EFAULT;
1206 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1207 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1208 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001209 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001210 unlock_user(target_smreqn, target_addr, 0);
1211
1212 return 0;
1213}
1214
bellard579a97f2007-11-11 14:26:47 +00001215static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1216 abi_ulong target_addr,
1217 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001218{
aurel32607175e2009-04-15 16:11:59 +00001219 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1220 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001221 struct target_sockaddr *target_saddr;
1222
bellard579a97f2007-11-11 14:26:47 +00001223 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1224 if (!target_saddr)
1225 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001226
1227 sa_family = tswap16(target_saddr->sa_family);
1228
1229 /* Oops. The caller might send a incomplete sun_path; sun_path
1230 * must be terminated by \0 (see the manual page), but
1231 * unfortunately it is quite common to specify sockaddr_un
1232 * length as "strlen(x->sun_path)" while it should be
1233 * "strlen(...) + 1". We'll fix that here if needed.
1234 * Linux kernel has a similar feature.
1235 */
1236
1237 if (sa_family == AF_UNIX) {
1238 if (len < unix_maxlen && len > 0) {
1239 char *cp = (char*)target_saddr;
1240
1241 if ( cp[len-1] && !cp[len] )
1242 len++;
1243 }
1244 if (len > unix_maxlen)
1245 len = unix_maxlen;
1246 }
1247
pbrook53a59602006-03-25 19:31:22 +00001248 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001249 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001250 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001251
1252 return 0;
bellard7854b052003-03-29 17:22:23 +00001253}
1254
bellard579a97f2007-11-11 14:26:47 +00001255static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1256 struct sockaddr *addr,
1257 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001258{
pbrook53a59602006-03-25 19:31:22 +00001259 struct target_sockaddr *target_saddr;
1260
bellard579a97f2007-11-11 14:26:47 +00001261 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1262 if (!target_saddr)
1263 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001264 memcpy(target_saddr, addr, len);
1265 target_saddr->sa_family = tswap16(addr->sa_family);
1266 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001267
1268 return 0;
bellard7854b052003-03-29 17:22:23 +00001269}
1270
bellard5a4a8982007-11-11 17:39:18 +00001271static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1272 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001273{
1274 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001275 abi_long msg_controllen;
1276 abi_ulong target_cmsg_addr;
1277 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001278 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001279
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001280 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001281 if (msg_controllen < sizeof (struct target_cmsghdr))
1282 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001283 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001284 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1285 if (!target_cmsg)
1286 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001287
1288 while (cmsg && target_cmsg) {
1289 void *data = CMSG_DATA(cmsg);
1290 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1291
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001292 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001293 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1294
1295 space += CMSG_SPACE(len);
1296 if (space > msgh->msg_controllen) {
1297 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001298 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001299 break;
1300 }
1301
1302 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1303 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1304 cmsg->cmsg_len = CMSG_LEN(len);
1305
bellard3532fa72006-06-24 15:06:03 +00001306 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001307 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1308 memcpy(data, target_data, len);
1309 } else {
1310 int *fd = (int *)data;
1311 int *target_fd = (int *)target_data;
1312 int i, numfds = len / sizeof(int);
1313
1314 for (i = 0; i < numfds; i++)
1315 fd[i] = tswap32(target_fd[i]);
1316 }
1317
1318 cmsg = CMSG_NXTHDR(msgh, cmsg);
1319 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1320 }
bellard5a4a8982007-11-11 17:39:18 +00001321 unlock_user(target_cmsg, target_cmsg_addr, 0);
1322 the_end:
bellard7854b052003-03-29 17:22:23 +00001323 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001324 return 0;
bellard7854b052003-03-29 17:22:23 +00001325}
1326
bellard5a4a8982007-11-11 17:39:18 +00001327static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1328 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001329{
1330 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001331 abi_long msg_controllen;
1332 abi_ulong target_cmsg_addr;
1333 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001334 socklen_t space = 0;
1335
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001336 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001337 if (msg_controllen < sizeof (struct target_cmsghdr))
1338 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001339 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001340 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1341 if (!target_cmsg)
1342 return -TARGET_EFAULT;
1343
bellard7854b052003-03-29 17:22:23 +00001344 while (cmsg && target_cmsg) {
1345 void *data = CMSG_DATA(cmsg);
1346 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1347
1348 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1349
1350 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001351 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001352 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001353 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001354 break;
1355 }
1356
1357 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1358 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001359 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
bellard7854b052003-03-29 17:22:23 +00001360
bellard3532fa72006-06-24 15:06:03 +00001361 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001362 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1363 memcpy(target_data, data, len);
1364 } else {
1365 int *fd = (int *)data;
1366 int *target_fd = (int *)target_data;
1367 int i, numfds = len / sizeof(int);
1368
1369 for (i = 0; i < numfds; i++)
1370 target_fd[i] = tswap32(fd[i]);
1371 }
1372
1373 cmsg = CMSG_NXTHDR(msgh, cmsg);
1374 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1375 }
bellard5a4a8982007-11-11 17:39:18 +00001376 unlock_user(target_cmsg, target_cmsg_addr, space);
1377 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001378 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001379 return 0;
bellard7854b052003-03-29 17:22:23 +00001380}
1381
ths0da46a62007-10-20 20:23:07 +00001382/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001383static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001384 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001385{
blueswir1992f48a2007-10-14 16:27:31 +00001386 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001387 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001388 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001389 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001390
bellard8853f862004-02-22 14:57:26 +00001391 switch(level) {
1392 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001393 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001394 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001395 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001396
bellard2f619692007-11-16 10:46:05 +00001397 if (get_user_u32(val, optval_addr))
1398 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001399 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1400 break;
1401 case SOL_IP:
1402 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001403 case IP_TOS:
1404 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001405 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001406 case IP_ROUTER_ALERT:
1407 case IP_RECVOPTS:
1408 case IP_RETOPTS:
1409 case IP_PKTINFO:
1410 case IP_MTU_DISCOVER:
1411 case IP_RECVERR:
1412 case IP_RECVTOS:
1413#ifdef IP_FREEBIND
1414 case IP_FREEBIND:
1415#endif
1416 case IP_MULTICAST_TTL:
1417 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001418 val = 0;
1419 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001420 if (get_user_u32(val, optval_addr))
1421 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001422 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001423 if (get_user_u8(val, optval_addr))
1424 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001425 }
1426 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1427 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001428 case IP_ADD_MEMBERSHIP:
1429 case IP_DROP_MEMBERSHIP:
1430 if (optlen < sizeof (struct target_ip_mreq) ||
1431 optlen > sizeof (struct target_ip_mreqn))
1432 return -TARGET_EINVAL;
1433
1434 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1435 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1436 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1437 break;
1438
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001439 case IP_BLOCK_SOURCE:
1440 case IP_UNBLOCK_SOURCE:
1441 case IP_ADD_SOURCE_MEMBERSHIP:
1442 case IP_DROP_SOURCE_MEMBERSHIP:
1443 if (optlen != sizeof (struct target_ip_mreq_source))
1444 return -TARGET_EINVAL;
1445
1446 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1447 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1448 unlock_user (ip_mreq_source, optval_addr, 0);
1449 break;
1450
bellard8853f862004-02-22 14:57:26 +00001451 default:
1452 goto unimplemented;
1453 }
1454 break;
bellard3532fa72006-06-24 15:06:03 +00001455 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001456 switch (optname) {
1457 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001458 case TARGET_SO_DEBUG:
1459 optname = SO_DEBUG;
1460 break;
1461 case TARGET_SO_REUSEADDR:
1462 optname = SO_REUSEADDR;
1463 break;
1464 case TARGET_SO_TYPE:
1465 optname = SO_TYPE;
1466 break;
1467 case TARGET_SO_ERROR:
1468 optname = SO_ERROR;
1469 break;
1470 case TARGET_SO_DONTROUTE:
1471 optname = SO_DONTROUTE;
1472 break;
1473 case TARGET_SO_BROADCAST:
1474 optname = SO_BROADCAST;
1475 break;
1476 case TARGET_SO_SNDBUF:
1477 optname = SO_SNDBUF;
1478 break;
1479 case TARGET_SO_RCVBUF:
1480 optname = SO_RCVBUF;
1481 break;
1482 case TARGET_SO_KEEPALIVE:
1483 optname = SO_KEEPALIVE;
1484 break;
1485 case TARGET_SO_OOBINLINE:
1486 optname = SO_OOBINLINE;
1487 break;
1488 case TARGET_SO_NO_CHECK:
1489 optname = SO_NO_CHECK;
1490 break;
1491 case TARGET_SO_PRIORITY:
1492 optname = SO_PRIORITY;
1493 break;
bellard5e83e8e2005-03-01 22:32:06 +00001494#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001495 case TARGET_SO_BSDCOMPAT:
1496 optname = SO_BSDCOMPAT;
1497 break;
bellard5e83e8e2005-03-01 22:32:06 +00001498#endif
bellard3532fa72006-06-24 15:06:03 +00001499 case TARGET_SO_PASSCRED:
1500 optname = SO_PASSCRED;
1501 break;
1502 case TARGET_SO_TIMESTAMP:
1503 optname = SO_TIMESTAMP;
1504 break;
1505 case TARGET_SO_RCVLOWAT:
1506 optname = SO_RCVLOWAT;
1507 break;
1508 case TARGET_SO_RCVTIMEO:
1509 optname = SO_RCVTIMEO;
1510 break;
1511 case TARGET_SO_SNDTIMEO:
1512 optname = SO_SNDTIMEO;
1513 break;
bellard8853f862004-02-22 14:57:26 +00001514 break;
1515 default:
1516 goto unimplemented;
1517 }
bellard3532fa72006-06-24 15:06:03 +00001518 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001519 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001520
bellard2f619692007-11-16 10:46:05 +00001521 if (get_user_u32(val, optval_addr))
1522 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001523 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001524 break;
bellard7854b052003-03-29 17:22:23 +00001525 default:
bellard8853f862004-02-22 14:57:26 +00001526 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02001527 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001528 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001529 }
bellard8853f862004-02-22 14:57:26 +00001530 return ret;
bellard7854b052003-03-29 17:22:23 +00001531}
1532
ths0da46a62007-10-20 20:23:07 +00001533/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001534static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001535 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001536{
blueswir1992f48a2007-10-14 16:27:31 +00001537 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001538 int len, val;
1539 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001540
1541 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001542 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001543 level = SOL_SOCKET;
1544 switch (optname) {
1545 /* These don't just return a single integer */
1546 case TARGET_SO_LINGER:
1547 case TARGET_SO_RCVTIMEO:
1548 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001549 case TARGET_SO_PEERNAME:
1550 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02001551 case TARGET_SO_PEERCRED: {
1552 struct ucred cr;
1553 socklen_t crlen;
1554 struct target_ucred *tcr;
1555
1556 if (get_user_u32(len, optlen)) {
1557 return -TARGET_EFAULT;
1558 }
1559 if (len < 0) {
1560 return -TARGET_EINVAL;
1561 }
1562
1563 crlen = sizeof(cr);
1564 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1565 &cr, &crlen));
1566 if (ret < 0) {
1567 return ret;
1568 }
1569 if (len > crlen) {
1570 len = crlen;
1571 }
1572 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1573 return -TARGET_EFAULT;
1574 }
1575 __put_user(cr.pid, &tcr->pid);
1576 __put_user(cr.uid, &tcr->uid);
1577 __put_user(cr.gid, &tcr->gid);
1578 unlock_user_struct(tcr, optval_addr, 1);
1579 if (put_user_u32(len, optlen)) {
1580 return -TARGET_EFAULT;
1581 }
1582 break;
1583 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001584 /* Options with 'int' argument. */
1585 case TARGET_SO_DEBUG:
1586 optname = SO_DEBUG;
1587 goto int_case;
1588 case TARGET_SO_REUSEADDR:
1589 optname = SO_REUSEADDR;
1590 goto int_case;
1591 case TARGET_SO_TYPE:
1592 optname = SO_TYPE;
1593 goto int_case;
1594 case TARGET_SO_ERROR:
1595 optname = SO_ERROR;
1596 goto int_case;
1597 case TARGET_SO_DONTROUTE:
1598 optname = SO_DONTROUTE;
1599 goto int_case;
1600 case TARGET_SO_BROADCAST:
1601 optname = SO_BROADCAST;
1602 goto int_case;
1603 case TARGET_SO_SNDBUF:
1604 optname = SO_SNDBUF;
1605 goto int_case;
1606 case TARGET_SO_RCVBUF:
1607 optname = SO_RCVBUF;
1608 goto int_case;
1609 case TARGET_SO_KEEPALIVE:
1610 optname = SO_KEEPALIVE;
1611 goto int_case;
1612 case TARGET_SO_OOBINLINE:
1613 optname = SO_OOBINLINE;
1614 goto int_case;
1615 case TARGET_SO_NO_CHECK:
1616 optname = SO_NO_CHECK;
1617 goto int_case;
1618 case TARGET_SO_PRIORITY:
1619 optname = SO_PRIORITY;
1620 goto int_case;
1621#ifdef SO_BSDCOMPAT
1622 case TARGET_SO_BSDCOMPAT:
1623 optname = SO_BSDCOMPAT;
1624 goto int_case;
1625#endif
1626 case TARGET_SO_PASSCRED:
1627 optname = SO_PASSCRED;
1628 goto int_case;
1629 case TARGET_SO_TIMESTAMP:
1630 optname = SO_TIMESTAMP;
1631 goto int_case;
1632 case TARGET_SO_RCVLOWAT:
1633 optname = SO_RCVLOWAT;
1634 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001635 default:
bellard2efbe912005-07-23 15:10:20 +00001636 goto int_case;
1637 }
1638 break;
1639 case SOL_TCP:
1640 /* TCP options all take an 'int' value. */
1641 int_case:
bellard2f619692007-11-16 10:46:05 +00001642 if (get_user_u32(len, optlen))
1643 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001644 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001645 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001646 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00001647 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1648 if (ret < 0)
1649 return ret;
bellard2efbe912005-07-23 15:10:20 +00001650 if (len > lv)
1651 len = lv;
bellard2f619692007-11-16 10:46:05 +00001652 if (len == 4) {
1653 if (put_user_u32(val, optval_addr))
1654 return -TARGET_EFAULT;
1655 } else {
1656 if (put_user_u8(val, optval_addr))
1657 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001658 }
bellard2f619692007-11-16 10:46:05 +00001659 if (put_user_u32(len, optlen))
1660 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001661 break;
1662 case SOL_IP:
1663 switch(optname) {
1664 case IP_TOS:
1665 case IP_TTL:
1666 case IP_HDRINCL:
1667 case IP_ROUTER_ALERT:
1668 case IP_RECVOPTS:
1669 case IP_RETOPTS:
1670 case IP_PKTINFO:
1671 case IP_MTU_DISCOVER:
1672 case IP_RECVERR:
1673 case IP_RECVTOS:
1674#ifdef IP_FREEBIND
1675 case IP_FREEBIND:
1676#endif
1677 case IP_MULTICAST_TTL:
1678 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001679 if (get_user_u32(len, optlen))
1680 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001681 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001682 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001683 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001684 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1685 if (ret < 0)
1686 return ret;
bellard2efbe912005-07-23 15:10:20 +00001687 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001688 len = 1;
bellard2f619692007-11-16 10:46:05 +00001689 if (put_user_u32(len, optlen)
1690 || put_user_u8(val, optval_addr))
1691 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001692 } else {
bellard2efbe912005-07-23 15:10:20 +00001693 if (len > sizeof(int))
1694 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001695 if (put_user_u32(len, optlen)
1696 || put_user_u32(val, optval_addr))
1697 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001698 }
bellard8853f862004-02-22 14:57:26 +00001699 break;
bellard2efbe912005-07-23 15:10:20 +00001700 default:
thsc02f4992007-12-18 02:39:59 +00001701 ret = -TARGET_ENOPROTOOPT;
1702 break;
bellard8853f862004-02-22 14:57:26 +00001703 }
1704 break;
1705 default:
1706 unimplemented:
1707 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1708 level, optname);
thsc02f4992007-12-18 02:39:59 +00001709 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001710 break;
1711 }
1712 return ret;
bellard7854b052003-03-29 17:22:23 +00001713}
1714
bellard579a97f2007-11-11 14:26:47 +00001715/* FIXME
1716 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1717 * other lock functions have a return code of 0 for failure.
1718 */
1719static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1720 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001721{
1722 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001723 abi_ulong base;
balrogd732dcb2008-10-28 10:21:03 +00001724 int i;
pbrook53a59602006-03-25 19:31:22 +00001725
bellard579a97f2007-11-11 14:26:47 +00001726 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1727 if (!target_vec)
1728 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001729 for(i = 0;i < count; i++) {
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001730 base = tswapal(target_vec[i].iov_base);
1731 vec[i].iov_len = tswapal(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001732 if (vec[i].iov_len != 0) {
1733 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrogd732dcb2008-10-28 10:21:03 +00001734 /* Don't check lock_user return value. We must call writev even
1735 if a element has invalid base address. */
bellard41df8412008-02-04 22:26:57 +00001736 } else {
1737 /* zero length pointer is ignored */
1738 vec[i].iov_base = NULL;
1739 }
pbrook53a59602006-03-25 19:31:22 +00001740 }
1741 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001742 return 0;
pbrook53a59602006-03-25 19:31:22 +00001743}
1744
bellard579a97f2007-11-11 14:26:47 +00001745static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1746 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001747{
1748 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001749 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001750 int i;
1751
bellard579a97f2007-11-11 14:26:47 +00001752 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1753 if (!target_vec)
1754 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001755 for(i = 0;i < count; i++) {
balrogd732dcb2008-10-28 10:21:03 +00001756 if (target_vec[i].iov_base) {
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001757 base = tswapal(target_vec[i].iov_base);
balrogd732dcb2008-10-28 10:21:03 +00001758 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1759 }
pbrook53a59602006-03-25 19:31:22 +00001760 }
1761 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001762
1763 return 0;
pbrook53a59602006-03-25 19:31:22 +00001764}
1765
ths0da46a62007-10-20 20:23:07 +00001766/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001767static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001768{
1769#if defined(TARGET_MIPS)
1770 switch(type) {
1771 case TARGET_SOCK_DGRAM:
1772 type = SOCK_DGRAM;
1773 break;
1774 case TARGET_SOCK_STREAM:
1775 type = SOCK_STREAM;
1776 break;
1777 case TARGET_SOCK_RAW:
1778 type = SOCK_RAW;
1779 break;
1780 case TARGET_SOCK_RDM:
1781 type = SOCK_RDM;
1782 break;
1783 case TARGET_SOCK_SEQPACKET:
1784 type = SOCK_SEQPACKET;
1785 break;
1786 case TARGET_SOCK_PACKET:
1787 type = SOCK_PACKET;
1788 break;
1789 }
1790#endif
balrog12bc92a2007-10-30 21:06:14 +00001791 if (domain == PF_NETLINK)
1792 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001793 return get_errno(socket(domain, type, protocol));
1794}
1795
ths0da46a62007-10-20 20:23:07 +00001796/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001797static abi_long do_bind(int sockfd, abi_ulong target_addr,
1798 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001799{
aurel328f7aeaf2009-01-30 19:47:57 +00001800 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001801 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001802
Blue Swirl38724252010-09-18 05:53:14 +00001803 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001804 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001805 }
aurel328f7aeaf2009-01-30 19:47:57 +00001806
aurel32607175e2009-04-15 16:11:59 +00001807 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001808
Arnaud Patard917507b2009-06-19 10:44:45 +03001809 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1810 if (ret)
1811 return ret;
1812
bellard3532fa72006-06-24 15:06:03 +00001813 return get_errno(bind(sockfd, addr, addrlen));
1814}
1815
ths0da46a62007-10-20 20:23:07 +00001816/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001817static abi_long do_connect(int sockfd, abi_ulong target_addr,
1818 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001819{
aurel328f7aeaf2009-01-30 19:47:57 +00001820 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001821 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001822
Blue Swirl38724252010-09-18 05:53:14 +00001823 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001824 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001825 }
aurel328f7aeaf2009-01-30 19:47:57 +00001826
1827 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001828
Arnaud Patard917507b2009-06-19 10:44:45 +03001829 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1830 if (ret)
1831 return ret;
1832
bellard3532fa72006-06-24 15:06:03 +00001833 return get_errno(connect(sockfd, addr, addrlen));
1834}
1835
ths0da46a62007-10-20 20:23:07 +00001836/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001837static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1838 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001839{
balrog6de645c2008-10-28 10:26:29 +00001840 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001841 struct target_msghdr *msgp;
1842 struct msghdr msg;
1843 int count;
1844 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001845 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001846
bellard579a97f2007-11-11 14:26:47 +00001847 /* FIXME */
1848 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1849 msgp,
1850 target_msg,
1851 send ? 1 : 0))
1852 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001853 if (msgp->msg_name) {
1854 msg.msg_namelen = tswap32(msgp->msg_namelen);
1855 msg.msg_name = alloca(msg.msg_namelen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001856 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001857 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001858 if (ret) {
1859 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1860 return ret;
1861 }
bellard3532fa72006-06-24 15:06:03 +00001862 } else {
1863 msg.msg_name = NULL;
1864 msg.msg_namelen = 0;
1865 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001866 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00001867 msg.msg_control = alloca(msg.msg_controllen);
1868 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001869
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001870 count = tswapal(msgp->msg_iovlen);
bellard3532fa72006-06-24 15:06:03 +00001871 vec = alloca(count * sizeof(struct iovec));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001872 target_vec = tswapal(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001873 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001874 msg.msg_iovlen = count;
1875 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001876
bellard3532fa72006-06-24 15:06:03 +00001877 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001878 ret = target_to_host_cmsg(&msg, msgp);
1879 if (ret == 0)
1880 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001881 } else {
1882 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001883 if (!is_error(ret)) {
1884 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001885 ret = host_to_target_cmsg(msgp, &msg);
Jing Huangca619062012-07-24 13:58:02 +00001886 if (!is_error(ret)) {
1887 msgp->msg_namelen = tswap32(msg.msg_namelen);
1888 if (msg.msg_name != NULL) {
1889 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
1890 msg.msg_name, msg.msg_namelen);
1891 if (ret) {
1892 goto out;
1893 }
1894 }
1895
balrog6de645c2008-10-28 10:26:29 +00001896 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00001897 }
balrog6de645c2008-10-28 10:26:29 +00001898 }
bellard3532fa72006-06-24 15:06:03 +00001899 }
Jing Huangca619062012-07-24 13:58:02 +00001900
1901out:
bellard3532fa72006-06-24 15:06:03 +00001902 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001903 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001904 return ret;
1905}
1906
ths0da46a62007-10-20 20:23:07 +00001907/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001908static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001909 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001910{
bellard2f619692007-11-16 10:46:05 +00001911 socklen_t addrlen;
1912 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001913 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001914
Arnaud Patard917507b2009-06-19 10:44:45 +03001915 if (target_addr == 0)
1916 return get_errno(accept(fd, NULL, NULL));
1917
1918 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00001919 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03001920 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00001921
Blue Swirl38724252010-09-18 05:53:14 +00001922 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001923 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001924 }
aurel328f7aeaf2009-01-30 19:47:57 +00001925
Arnaud Patard917507b2009-06-19 10:44:45 +03001926 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1927 return -TARGET_EINVAL;
1928
bellard2f619692007-11-16 10:46:05 +00001929 addr = alloca(addrlen);
1930
pbrook1be9e1d2006-11-19 15:26:04 +00001931 ret = get_errno(accept(fd, addr, &addrlen));
1932 if (!is_error(ret)) {
1933 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001934 if (put_user_u32(addrlen, target_addrlen_addr))
1935 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001936 }
1937 return ret;
1938}
1939
ths0da46a62007-10-20 20:23:07 +00001940/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001941static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001942 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001943{
bellard2f619692007-11-16 10:46:05 +00001944 socklen_t addrlen;
1945 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001946 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001947
bellard2f619692007-11-16 10:46:05 +00001948 if (get_user_u32(addrlen, target_addrlen_addr))
1949 return -TARGET_EFAULT;
1950
Blue Swirl38724252010-09-18 05:53:14 +00001951 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001952 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001953 }
aurel328f7aeaf2009-01-30 19:47:57 +00001954
Arnaud Patard917507b2009-06-19 10:44:45 +03001955 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1956 return -TARGET_EFAULT;
1957
bellard2f619692007-11-16 10:46:05 +00001958 addr = alloca(addrlen);
1959
pbrook1be9e1d2006-11-19 15:26:04 +00001960 ret = get_errno(getpeername(fd, addr, &addrlen));
1961 if (!is_error(ret)) {
1962 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001963 if (put_user_u32(addrlen, target_addrlen_addr))
1964 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001965 }
1966 return ret;
1967}
1968
ths0da46a62007-10-20 20:23:07 +00001969/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001970static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001971 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001972{
bellard2f619692007-11-16 10:46:05 +00001973 socklen_t addrlen;
1974 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001975 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001976
bellard2f619692007-11-16 10:46:05 +00001977 if (get_user_u32(addrlen, target_addrlen_addr))
1978 return -TARGET_EFAULT;
1979
Blue Swirl38724252010-09-18 05:53:14 +00001980 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001981 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001982 }
aurel328f7aeaf2009-01-30 19:47:57 +00001983
Arnaud Patard917507b2009-06-19 10:44:45 +03001984 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1985 return -TARGET_EFAULT;
1986
bellard2f619692007-11-16 10:46:05 +00001987 addr = alloca(addrlen);
1988
pbrook1be9e1d2006-11-19 15:26:04 +00001989 ret = get_errno(getsockname(fd, addr, &addrlen));
1990 if (!is_error(ret)) {
1991 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001992 if (put_user_u32(addrlen, target_addrlen_addr))
1993 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001994 }
1995 return ret;
1996}
1997
ths0da46a62007-10-20 20:23:07 +00001998/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001999static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00002000 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002001{
2002 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00002003 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002004
2005 ret = get_errno(socketpair(domain, type, protocol, tab));
2006 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00002007 if (put_user_s32(tab[0], target_tab_addr)
2008 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2009 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002010 }
2011 return ret;
2012}
2013
ths0da46a62007-10-20 20:23:07 +00002014/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002015static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2016 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002017{
2018 void *addr;
2019 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002020 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002021
Blue Swirl38724252010-09-18 05:53:14 +00002022 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002023 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002024 }
aurel328f7aeaf2009-01-30 19:47:57 +00002025
bellard579a97f2007-11-11 14:26:47 +00002026 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2027 if (!host_msg)
2028 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002029 if (target_addr) {
2030 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002031 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2032 if (ret) {
2033 unlock_user(host_msg, msg, 0);
2034 return ret;
2035 }
pbrook1be9e1d2006-11-19 15:26:04 +00002036 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2037 } else {
2038 ret = get_errno(send(fd, host_msg, len, flags));
2039 }
2040 unlock_user(host_msg, msg, 0);
2041 return ret;
2042}
2043
ths0da46a62007-10-20 20:23:07 +00002044/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002045static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2046 abi_ulong target_addr,
2047 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002048{
2049 socklen_t addrlen;
2050 void *addr;
2051 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002052 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002053
bellard579a97f2007-11-11 14:26:47 +00002054 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2055 if (!host_msg)
2056 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002057 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00002058 if (get_user_u32(addrlen, target_addrlen)) {
2059 ret = -TARGET_EFAULT;
2060 goto fail;
2061 }
Blue Swirl38724252010-09-18 05:53:14 +00002062 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002063 ret = -TARGET_EINVAL;
2064 goto fail;
2065 }
pbrook1be9e1d2006-11-19 15:26:04 +00002066 addr = alloca(addrlen);
2067 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2068 } else {
2069 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00002070 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002071 }
2072 if (!is_error(ret)) {
2073 if (target_addr) {
2074 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002075 if (put_user_u32(addrlen, target_addrlen)) {
2076 ret = -TARGET_EFAULT;
2077 goto fail;
2078 }
pbrook1be9e1d2006-11-19 15:26:04 +00002079 }
2080 unlock_user(host_msg, msg, len);
2081 } else {
bellard2f619692007-11-16 10:46:05 +00002082fail:
pbrook1be9e1d2006-11-19 15:26:04 +00002083 unlock_user(host_msg, msg, 0);
2084 }
2085 return ret;
2086}
2087
j_mayer32407102007-09-26 23:01:49 +00002088#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00002089/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002090static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00002091{
blueswir1992f48a2007-10-14 16:27:31 +00002092 abi_long ret;
2093 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00002094
2095 switch(num) {
2096 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00002097 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002098 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002099
Ulrich Hecht98818182009-07-03 17:09:28 +02002100 if (get_user_ual(domain, vptr)
2101 || get_user_ual(type, vptr + n)
2102 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002103 return -TARGET_EFAULT;
2104
bellard3532fa72006-06-24 15:06:03 +00002105 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00002106 }
bellard31e31b82003-02-18 22:55:36 +00002107 break;
2108 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00002109 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002110 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002111 abi_ulong target_addr;
2112 socklen_t addrlen;
2113
Ulrich Hecht98818182009-07-03 17:09:28 +02002114 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002115 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002116 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002117 return -TARGET_EFAULT;
2118
bellard3532fa72006-06-24 15:06:03 +00002119 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002120 }
bellard31e31b82003-02-18 22:55:36 +00002121 break;
2122 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00002123 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002124 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002125 abi_ulong target_addr;
2126 socklen_t addrlen;
2127
Ulrich Hecht98818182009-07-03 17:09:28 +02002128 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002129 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002130 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002131 return -TARGET_EFAULT;
2132
bellard3532fa72006-06-24 15:06:03 +00002133 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002134 }
bellard31e31b82003-02-18 22:55:36 +00002135 break;
2136 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00002137 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002138 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00002139
Ulrich Hecht98818182009-07-03 17:09:28 +02002140 if (get_user_ual(sockfd, vptr)
2141 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002142 return -TARGET_EFAULT;
2143
bellard7854b052003-03-29 17:22:23 +00002144 ret = get_errno(listen(sockfd, backlog));
2145 }
bellard31e31b82003-02-18 22:55:36 +00002146 break;
2147 case SOCKOP_accept:
2148 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002149 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002150 abi_ulong target_addr, target_addrlen;
2151
Ulrich Hecht98818182009-07-03 17:09:28 +02002152 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002153 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002154 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002155 return -TARGET_EFAULT;
2156
pbrook1be9e1d2006-11-19 15:26:04 +00002157 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002158 }
2159 break;
2160 case SOCKOP_getsockname:
2161 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002162 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002163 abi_ulong target_addr, target_addrlen;
2164
Ulrich Hecht98818182009-07-03 17:09:28 +02002165 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002166 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002167 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002168 return -TARGET_EFAULT;
2169
pbrook1be9e1d2006-11-19 15:26:04 +00002170 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002171 }
2172 break;
2173 case SOCKOP_getpeername:
2174 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002175 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002176 abi_ulong target_addr, target_addrlen;
2177
Ulrich Hecht98818182009-07-03 17:09:28 +02002178 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002179 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002180 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002181 return -TARGET_EFAULT;
2182
pbrook1be9e1d2006-11-19 15:26:04 +00002183 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002184 }
2185 break;
2186 case SOCKOP_socketpair:
2187 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002188 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002189 abi_ulong tab;
2190
Ulrich Hecht98818182009-07-03 17:09:28 +02002191 if (get_user_ual(domain, vptr)
2192 || get_user_ual(type, vptr + n)
2193 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002194 || get_user_ual(tab, vptr + 3 * n))
2195 return -TARGET_EFAULT;
2196
pbrook1be9e1d2006-11-19 15:26:04 +00002197 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00002198 }
2199 break;
2200 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00002201 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002202 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002203 abi_ulong msg;
2204 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002205 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002206
Ulrich Hecht98818182009-07-03 17:09:28 +02002207 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002208 || get_user_ual(msg, vptr + n)
2209 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002210 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002211 return -TARGET_EFAULT;
2212
pbrook1be9e1d2006-11-19 15:26:04 +00002213 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002214 }
bellard31e31b82003-02-18 22:55:36 +00002215 break;
2216 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00002217 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002218 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002219 abi_ulong msg;
2220 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002221 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002222
Ulrich Hecht98818182009-07-03 17:09:28 +02002223 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002224 || get_user_ual(msg, vptr + n)
2225 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002226 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002227 return -TARGET_EFAULT;
2228
pbrook1be9e1d2006-11-19 15:26:04 +00002229 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002230 }
bellard31e31b82003-02-18 22:55:36 +00002231 break;
2232 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00002233 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002234 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002235 abi_ulong msg;
2236 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002237 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002238 abi_ulong addr;
2239 socklen_t addrlen;
2240
Ulrich Hecht98818182009-07-03 17:09:28 +02002241 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002242 || get_user_ual(msg, vptr + n)
2243 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002244 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002245 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002246 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002247 return -TARGET_EFAULT;
2248
pbrook1be9e1d2006-11-19 15:26:04 +00002249 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002250 }
bellard31e31b82003-02-18 22:55:36 +00002251 break;
2252 case SOCKOP_recvfrom:
2253 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002254 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002255 abi_ulong msg;
2256 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002257 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002258 abi_ulong addr;
2259 socklen_t addrlen;
2260
Ulrich Hecht98818182009-07-03 17:09:28 +02002261 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002262 || get_user_ual(msg, vptr + n)
2263 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002264 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002265 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002266 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002267 return -TARGET_EFAULT;
2268
pbrook1be9e1d2006-11-19 15:26:04 +00002269 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00002270 }
2271 break;
2272 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00002273 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002274 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00002275
Ulrich Hecht98818182009-07-03 17:09:28 +02002276 if (get_user_ual(sockfd, vptr)
2277 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002278 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002279
2280 ret = get_errno(shutdown(sockfd, how));
2281 }
bellard31e31b82003-02-18 22:55:36 +00002282 break;
2283 case SOCKOP_sendmsg:
2284 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002285 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002286 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002287 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002288 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002289
Ulrich Hecht98818182009-07-03 17:09:28 +02002290 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002291 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002292 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002293 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002294
ths5fafdf22007-09-16 21:08:06 +00002295 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002296 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002297 }
2298 break;
bellard31e31b82003-02-18 22:55:36 +00002299 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002300 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002301 abi_ulong sockfd;
2302 abi_ulong level;
2303 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002304 abi_ulong optval;
2305 socklen_t optlen;
2306
Ulrich Hecht98818182009-07-03 17:09:28 +02002307 if (get_user_ual(sockfd, vptr)
2308 || get_user_ual(level, vptr + n)
2309 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002310 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002311 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002312 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002313
2314 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2315 }
2316 break;
bellard31e31b82003-02-18 22:55:36 +00002317 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002318 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002319 abi_ulong sockfd;
2320 abi_ulong level;
2321 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002322 abi_ulong optval;
2323 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002324
Ulrich Hecht98818182009-07-03 17:09:28 +02002325 if (get_user_ual(sockfd, vptr)
2326 || get_user_ual(level, vptr + n)
2327 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002328 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002329 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002330 return -TARGET_EFAULT;
2331
2332 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002333 }
2334 break;
bellard31e31b82003-02-18 22:55:36 +00002335 default:
2336 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002337 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002338 break;
2339 }
2340 return ret;
2341}
j_mayer32407102007-09-26 23:01:49 +00002342#endif
bellard31e31b82003-02-18 22:55:36 +00002343
bellard8853f862004-02-22 14:57:26 +00002344#define N_SHM_REGIONS 32
2345
2346static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002347 abi_ulong start;
2348 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002349} shm_regions[N_SHM_REGIONS];
2350
ths3eb6b042007-06-03 14:26:27 +00002351struct target_ipc_perm
2352{
blueswir1992f48a2007-10-14 16:27:31 +00002353 abi_long __key;
2354 abi_ulong uid;
2355 abi_ulong gid;
2356 abi_ulong cuid;
2357 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002358 unsigned short int mode;
2359 unsigned short int __pad1;
2360 unsigned short int __seq;
2361 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002362 abi_ulong __unused1;
2363 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002364};
2365
2366struct target_semid_ds
2367{
2368 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002369 abi_ulong sem_otime;
2370 abi_ulong __unused1;
2371 abi_ulong sem_ctime;
2372 abi_ulong __unused2;
2373 abi_ulong sem_nsems;
2374 abi_ulong __unused3;
2375 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002376};
2377
bellard579a97f2007-11-11 14:26:47 +00002378static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2379 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002380{
2381 struct target_ipc_perm *target_ip;
2382 struct target_semid_ds *target_sd;
2383
bellard579a97f2007-11-11 14:26:47 +00002384 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2385 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002386 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002387 host_ip->__key = tswapal(target_ip->__key);
2388 host_ip->uid = tswapal(target_ip->uid);
2389 host_ip->gid = tswapal(target_ip->gid);
2390 host_ip->cuid = tswapal(target_ip->cuid);
2391 host_ip->cgid = tswapal(target_ip->cgid);
2392 host_ip->mode = tswap16(target_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002393 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002394 return 0;
ths3eb6b042007-06-03 14:26:27 +00002395}
2396
bellard579a97f2007-11-11 14:26:47 +00002397static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2398 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002399{
2400 struct target_ipc_perm *target_ip;
2401 struct target_semid_ds *target_sd;
2402
bellard579a97f2007-11-11 14:26:47 +00002403 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2404 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002405 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002406 target_ip->__key = tswapal(host_ip->__key);
2407 target_ip->uid = tswapal(host_ip->uid);
2408 target_ip->gid = tswapal(host_ip->gid);
2409 target_ip->cuid = tswapal(host_ip->cuid);
2410 target_ip->cgid = tswapal(host_ip->cgid);
2411 target_ip->mode = tswap16(host_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002412 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002413 return 0;
ths3eb6b042007-06-03 14:26:27 +00002414}
2415
bellard579a97f2007-11-11 14:26:47 +00002416static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2417 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002418{
2419 struct target_semid_ds *target_sd;
2420
bellard579a97f2007-11-11 14:26:47 +00002421 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2422 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002423 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2424 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002425 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2426 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2427 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002428 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002429 return 0;
ths3eb6b042007-06-03 14:26:27 +00002430}
2431
bellard579a97f2007-11-11 14:26:47 +00002432static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2433 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002434{
2435 struct target_semid_ds *target_sd;
2436
bellard579a97f2007-11-11 14:26:47 +00002437 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2438 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002439 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08002440 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002441 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2442 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2443 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002444 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002445 return 0;
ths3eb6b042007-06-03 14:26:27 +00002446}
2447
aurel32e5289082009-04-18 16:16:12 +00002448struct target_seminfo {
2449 int semmap;
2450 int semmni;
2451 int semmns;
2452 int semmnu;
2453 int semmsl;
2454 int semopm;
2455 int semume;
2456 int semusz;
2457 int semvmx;
2458 int semaem;
2459};
2460
2461static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2462 struct seminfo *host_seminfo)
2463{
2464 struct target_seminfo *target_seminfo;
2465 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2466 return -TARGET_EFAULT;
2467 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2468 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2469 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2470 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2471 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2472 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2473 __put_user(host_seminfo->semume, &target_seminfo->semume);
2474 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2475 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2476 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2477 unlock_user_struct(target_seminfo, target_addr, 1);
2478 return 0;
2479}
2480
thsfa294812007-02-02 22:05:00 +00002481union semun {
2482 int val;
ths3eb6b042007-06-03 14:26:27 +00002483 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002484 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002485 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002486};
2487
ths3eb6b042007-06-03 14:26:27 +00002488union target_semun {
2489 int val;
aurel32e5289082009-04-18 16:16:12 +00002490 abi_ulong buf;
2491 abi_ulong array;
2492 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002493};
2494
aurel32e5289082009-04-18 16:16:12 +00002495static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2496 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002497{
aurel32e5289082009-04-18 16:16:12 +00002498 int nsems;
2499 unsigned short *array;
2500 union semun semun;
2501 struct semid_ds semid_ds;
2502 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002503
aurel32e5289082009-04-18 16:16:12 +00002504 semun.buf = &semid_ds;
2505
2506 ret = semctl(semid, 0, IPC_STAT, semun);
2507 if (ret == -1)
2508 return get_errno(ret);
2509
2510 nsems = semid_ds.sem_nsems;
2511
2512 *host_array = malloc(nsems*sizeof(unsigned short));
2513 array = lock_user(VERIFY_READ, target_addr,
2514 nsems*sizeof(unsigned short), 1);
2515 if (!array)
2516 return -TARGET_EFAULT;
2517
2518 for(i=0; i<nsems; i++) {
2519 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002520 }
aurel32e5289082009-04-18 16:16:12 +00002521 unlock_user(array, target_addr, 0);
2522
bellard579a97f2007-11-11 14:26:47 +00002523 return 0;
ths3eb6b042007-06-03 14:26:27 +00002524}
2525
aurel32e5289082009-04-18 16:16:12 +00002526static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2527 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002528{
aurel32e5289082009-04-18 16:16:12 +00002529 int nsems;
2530 unsigned short *array;
2531 union semun semun;
2532 struct semid_ds semid_ds;
2533 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002534
aurel32e5289082009-04-18 16:16:12 +00002535 semun.buf = &semid_ds;
2536
2537 ret = semctl(semid, 0, IPC_STAT, semun);
2538 if (ret == -1)
2539 return get_errno(ret);
2540
2541 nsems = semid_ds.sem_nsems;
2542
2543 array = lock_user(VERIFY_WRITE, target_addr,
2544 nsems*sizeof(unsigned short), 0);
2545 if (!array)
2546 return -TARGET_EFAULT;
2547
2548 for(i=0; i<nsems; i++) {
2549 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002550 }
aurel32e5289082009-04-18 16:16:12 +00002551 free(*host_array);
2552 unlock_user(array, target_addr, 1);
2553
bellard579a97f2007-11-11 14:26:47 +00002554 return 0;
ths3eb6b042007-06-03 14:26:27 +00002555}
2556
aurel32e5289082009-04-18 16:16:12 +00002557static inline abi_long do_semctl(int semid, int semnum, int cmd,
2558 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002559{
2560 union semun arg;
2561 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302562 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002563 struct seminfo seminfo;
2564 abi_long ret = -TARGET_EINVAL;
2565 abi_long err;
2566 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002567
2568 switch( cmd ) {
2569 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002570 case SETVAL:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002571 arg.val = tswap32(target_su.val);
aurel32e5289082009-04-18 16:16:12 +00002572 ret = get_errno(semctl(semid, semnum, cmd, arg));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002573 target_su.val = tswap32(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002574 break;
2575 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002576 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002577 err = target_to_host_semarray(semid, &array, target_su.array);
2578 if (err)
2579 return err;
2580 arg.array = array;
2581 ret = get_errno(semctl(semid, semnum, cmd, arg));
2582 err = host_to_target_semarray(semid, target_su.array, &array);
2583 if (err)
2584 return err;
ths3eb6b042007-06-03 14:26:27 +00002585 break;
2586 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002587 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002588 case SEM_STAT:
2589 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2590 if (err)
2591 return err;
2592 arg.buf = &dsarg;
2593 ret = get_errno(semctl(semid, semnum, cmd, arg));
2594 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2595 if (err)
2596 return err;
ths3eb6b042007-06-03 14:26:27 +00002597 break;
aurel32e5289082009-04-18 16:16:12 +00002598 case IPC_INFO:
2599 case SEM_INFO:
2600 arg.__buf = &seminfo;
2601 ret = get_errno(semctl(semid, semnum, cmd, arg));
2602 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2603 if (err)
2604 return err;
2605 break;
2606 case IPC_RMID:
2607 case GETPID:
2608 case GETNCNT:
2609 case GETZCNT:
2610 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2611 break;
ths3eb6b042007-06-03 14:26:27 +00002612 }
2613
2614 return ret;
2615}
2616
aurel32e5289082009-04-18 16:16:12 +00002617struct target_sembuf {
2618 unsigned short sem_num;
2619 short sem_op;
2620 short sem_flg;
2621};
2622
2623static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2624 abi_ulong target_addr,
2625 unsigned nsops)
2626{
2627 struct target_sembuf *target_sembuf;
2628 int i;
2629
2630 target_sembuf = lock_user(VERIFY_READ, target_addr,
2631 nsops*sizeof(struct target_sembuf), 1);
2632 if (!target_sembuf)
2633 return -TARGET_EFAULT;
2634
2635 for(i=0; i<nsops; i++) {
2636 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2637 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2638 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2639 }
2640
2641 unlock_user(target_sembuf, target_addr, 0);
2642
2643 return 0;
2644}
2645
2646static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2647{
2648 struct sembuf sops[nsops];
2649
2650 if (target_to_host_sembuf(sops, ptr, nsops))
2651 return -TARGET_EFAULT;
2652
2653 return semop(semid, sops, nsops);
2654}
2655
ths1bc012f2007-06-03 14:27:49 +00002656struct target_msqid_ds
2657{
aurel321c54ff92008-10-13 21:08:44 +00002658 struct target_ipc_perm msg_perm;
2659 abi_ulong msg_stime;
2660#if TARGET_ABI_BITS == 32
2661 abi_ulong __unused1;
2662#endif
2663 abi_ulong msg_rtime;
2664#if TARGET_ABI_BITS == 32
2665 abi_ulong __unused2;
2666#endif
2667 abi_ulong msg_ctime;
2668#if TARGET_ABI_BITS == 32
2669 abi_ulong __unused3;
2670#endif
2671 abi_ulong __msg_cbytes;
2672 abi_ulong msg_qnum;
2673 abi_ulong msg_qbytes;
2674 abi_ulong msg_lspid;
2675 abi_ulong msg_lrpid;
2676 abi_ulong __unused4;
2677 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002678};
2679
bellard579a97f2007-11-11 14:26:47 +00002680static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2681 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002682{
2683 struct target_msqid_ds *target_md;
2684
bellard579a97f2007-11-11 14:26:47 +00002685 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2686 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002687 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2688 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002689 host_md->msg_stime = tswapal(target_md->msg_stime);
2690 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2691 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2692 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2693 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2694 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2695 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2696 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002697 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002698 return 0;
ths1bc012f2007-06-03 14:27:49 +00002699}
2700
bellard579a97f2007-11-11 14:26:47 +00002701static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2702 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002703{
2704 struct target_msqid_ds *target_md;
2705
bellard579a97f2007-11-11 14:26:47 +00002706 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2707 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002708 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2709 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002710 target_md->msg_stime = tswapal(host_md->msg_stime);
2711 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2712 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2713 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2714 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2715 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2716 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2717 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002718 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002719 return 0;
ths1bc012f2007-06-03 14:27:49 +00002720}
2721
aurel321c54ff92008-10-13 21:08:44 +00002722struct target_msginfo {
2723 int msgpool;
2724 int msgmap;
2725 int msgmax;
2726 int msgmnb;
2727 int msgmni;
2728 int msgssz;
2729 int msgtql;
2730 unsigned short int msgseg;
2731};
2732
2733static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2734 struct msginfo *host_msginfo)
2735{
2736 struct target_msginfo *target_msginfo;
2737 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2738 return -TARGET_EFAULT;
2739 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2740 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2741 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2742 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2743 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2744 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2745 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2746 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2747 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002748 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002749}
2750
2751static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002752{
2753 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002754 struct msginfo msginfo;
2755 abi_long ret = -TARGET_EINVAL;
2756
2757 cmd &= 0xff;
2758
2759 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002760 case IPC_STAT:
2761 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002762 case MSG_STAT:
2763 if (target_to_host_msqid_ds(&dsarg,ptr))
2764 return -TARGET_EFAULT;
2765 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2766 if (host_to_target_msqid_ds(ptr,&dsarg))
2767 return -TARGET_EFAULT;
2768 break;
2769 case IPC_RMID:
2770 ret = get_errno(msgctl(msgid, cmd, NULL));
2771 break;
2772 case IPC_INFO:
2773 case MSG_INFO:
2774 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2775 if (host_to_target_msginfo(ptr, &msginfo))
2776 return -TARGET_EFAULT;
2777 break;
ths1bc012f2007-06-03 14:27:49 +00002778 }
aurel321c54ff92008-10-13 21:08:44 +00002779
ths1bc012f2007-06-03 14:27:49 +00002780 return ret;
2781}
2782
2783struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002784 abi_long mtype;
2785 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002786};
2787
blueswir1992f48a2007-10-14 16:27:31 +00002788static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2789 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002790{
2791 struct target_msgbuf *target_mb;
2792 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002793 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002794
bellard579a97f2007-11-11 14:26:47 +00002795 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2796 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002797 host_mb = malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002798 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00002799 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002800 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2801 free(host_mb);
2802 unlock_user_struct(target_mb, msgp, 0);
2803
2804 return ret;
2805}
2806
blueswir1992f48a2007-10-14 16:27:31 +00002807static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002808 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002809 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002810{
2811 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002812 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002813 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002814 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002815
bellard579a97f2007-11-11 14:26:47 +00002816 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2817 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002818
ths1bc012f2007-06-03 14:27:49 +00002819 host_mb = malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002820 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapal(msgtyp), msgflg));
aurel321c54ff92008-10-13 21:08:44 +00002821
bellard579a97f2007-11-11 14:26:47 +00002822 if (ret > 0) {
2823 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2824 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2825 if (!target_mtext) {
2826 ret = -TARGET_EFAULT;
2827 goto end;
2828 }
aurel321c54ff92008-10-13 21:08:44 +00002829 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002830 unlock_user(target_mtext, target_mtext_addr, ret);
2831 }
aurel321c54ff92008-10-13 21:08:44 +00002832
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002833 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00002834 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002835
bellard579a97f2007-11-11 14:26:47 +00002836end:
2837 if (target_mb)
2838 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00002839 return ret;
2840}
2841
Riku Voipio88a8c982009-04-03 10:42:00 +03002842struct target_shmid_ds
2843{
2844 struct target_ipc_perm shm_perm;
2845 abi_ulong shm_segsz;
2846 abi_ulong shm_atime;
2847#if TARGET_ABI_BITS == 32
2848 abi_ulong __unused1;
2849#endif
2850 abi_ulong shm_dtime;
2851#if TARGET_ABI_BITS == 32
2852 abi_ulong __unused2;
2853#endif
2854 abi_ulong shm_ctime;
2855#if TARGET_ABI_BITS == 32
2856 abi_ulong __unused3;
2857#endif
2858 int shm_cpid;
2859 int shm_lpid;
2860 abi_ulong shm_nattch;
2861 unsigned long int __unused4;
2862 unsigned long int __unused5;
2863};
2864
2865static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2866 abi_ulong target_addr)
2867{
2868 struct target_shmid_ds *target_sd;
2869
2870 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2871 return -TARGET_EFAULT;
2872 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2873 return -TARGET_EFAULT;
2874 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2875 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2876 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2877 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2878 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2879 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2880 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2881 unlock_user_struct(target_sd, target_addr, 0);
2882 return 0;
2883}
2884
2885static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2886 struct shmid_ds *host_sd)
2887{
2888 struct target_shmid_ds *target_sd;
2889
2890 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2891 return -TARGET_EFAULT;
2892 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2893 return -TARGET_EFAULT;
2894 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2895 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2896 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2897 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2898 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2899 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2900 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2901 unlock_user_struct(target_sd, target_addr, 1);
2902 return 0;
2903}
2904
2905struct target_shminfo {
2906 abi_ulong shmmax;
2907 abi_ulong shmmin;
2908 abi_ulong shmmni;
2909 abi_ulong shmseg;
2910 abi_ulong shmall;
2911};
2912
2913static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2914 struct shminfo *host_shminfo)
2915{
2916 struct target_shminfo *target_shminfo;
2917 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2918 return -TARGET_EFAULT;
2919 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2920 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2921 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2922 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2923 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2924 unlock_user_struct(target_shminfo, target_addr, 1);
2925 return 0;
2926}
2927
2928struct target_shm_info {
2929 int used_ids;
2930 abi_ulong shm_tot;
2931 abi_ulong shm_rss;
2932 abi_ulong shm_swp;
2933 abi_ulong swap_attempts;
2934 abi_ulong swap_successes;
2935};
2936
2937static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2938 struct shm_info *host_shm_info)
2939{
2940 struct target_shm_info *target_shm_info;
2941 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2942 return -TARGET_EFAULT;
2943 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2944 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2945 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2946 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2947 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2948 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2949 unlock_user_struct(target_shm_info, target_addr, 1);
2950 return 0;
2951}
2952
2953static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2954{
2955 struct shmid_ds dsarg;
2956 struct shminfo shminfo;
2957 struct shm_info shm_info;
2958 abi_long ret = -TARGET_EINVAL;
2959
2960 cmd &= 0xff;
2961
2962 switch(cmd) {
2963 case IPC_STAT:
2964 case IPC_SET:
2965 case SHM_STAT:
2966 if (target_to_host_shmid_ds(&dsarg, buf))
2967 return -TARGET_EFAULT;
2968 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2969 if (host_to_target_shmid_ds(buf, &dsarg))
2970 return -TARGET_EFAULT;
2971 break;
2972 case IPC_INFO:
2973 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2974 if (host_to_target_shminfo(buf, &shminfo))
2975 return -TARGET_EFAULT;
2976 break;
2977 case SHM_INFO:
2978 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2979 if (host_to_target_shm_info(buf, &shm_info))
2980 return -TARGET_EFAULT;
2981 break;
2982 case IPC_RMID:
2983 case SHM_LOCK:
2984 case SHM_UNLOCK:
2985 ret = get_errno(shmctl(shmid, cmd, NULL));
2986 break;
2987 }
2988
2989 return ret;
2990}
2991
2992static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2993{
2994 abi_long raddr;
2995 void *host_raddr;
2996 struct shmid_ds shm_info;
2997 int i,ret;
2998
2999 /* find out the length of the shared memory segment */
3000 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3001 if (is_error(ret)) {
3002 /* can't get length, bail out */
3003 return ret;
3004 }
3005
3006 mmap_lock();
3007
3008 if (shmaddr)
3009 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3010 else {
3011 abi_ulong mmap_start;
3012
3013 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3014
3015 if (mmap_start == -1) {
3016 errno = ENOMEM;
3017 host_raddr = (void *)-1;
3018 } else
3019 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3020 }
3021
3022 if (host_raddr == (void *)-1) {
3023 mmap_unlock();
3024 return get_errno((long)host_raddr);
3025 }
3026 raddr=h2g((unsigned long)host_raddr);
3027
3028 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3029 PAGE_VALID | PAGE_READ |
3030 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3031
3032 for (i = 0; i < N_SHM_REGIONS; i++) {
3033 if (shm_regions[i].start == 0) {
3034 shm_regions[i].start = raddr;
3035 shm_regions[i].size = shm_info.shm_segsz;
3036 break;
3037 }
3038 }
3039
3040 mmap_unlock();
3041 return raddr;
3042
3043}
3044
3045static inline abi_long do_shmdt(abi_ulong shmaddr)
3046{
3047 int i;
3048
3049 for (i = 0; i < N_SHM_REGIONS; ++i) {
3050 if (shm_regions[i].start == shmaddr) {
3051 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09003052 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03003053 break;
3054 }
3055 }
3056
3057 return get_errno(shmdt(g2h(shmaddr)));
3058}
3059
aurel321c54ff92008-10-13 21:08:44 +00003060#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00003061/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00003062/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003063static abi_long do_ipc(unsigned int call, int first,
3064 int second, int third,
3065 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00003066{
3067 int version;
blueswir1992f48a2007-10-14 16:27:31 +00003068 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00003069
3070 version = call >> 16;
3071 call &= 0xffff;
3072
3073 switch (call) {
thsfa294812007-02-02 22:05:00 +00003074 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00003075 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00003076 break;
3077
3078 case IPCOP_semget:
3079 ret = get_errno(semget(first, second, third));
3080 break;
3081
3082 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00003083 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00003084 break;
thsd96372e2007-02-02 22:05:44 +00003085
aurel321c54ff92008-10-13 21:08:44 +00003086 case IPCOP_msgget:
3087 ret = get_errno(msgget(first, second));
3088 break;
thsd96372e2007-02-02 22:05:44 +00003089
aurel321c54ff92008-10-13 21:08:44 +00003090 case IPCOP_msgsnd:
3091 ret = do_msgsnd(first, ptr, second, third);
3092 break;
thsd96372e2007-02-02 22:05:44 +00003093
aurel321c54ff92008-10-13 21:08:44 +00003094 case IPCOP_msgctl:
3095 ret = do_msgctl(first, second, ptr);
3096 break;
thsd96372e2007-02-02 22:05:44 +00003097
aurel321c54ff92008-10-13 21:08:44 +00003098 case IPCOP_msgrcv:
3099 switch (version) {
3100 case 0:
3101 {
3102 struct target_ipc_kludge {
3103 abi_long msgp;
3104 abi_long msgtyp;
3105 } *tmp;
thsd96372e2007-02-02 22:05:44 +00003106
aurel321c54ff92008-10-13 21:08:44 +00003107 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3108 ret = -TARGET_EFAULT;
3109 break;
ths1bc012f2007-06-03 14:27:49 +00003110 }
aurel321c54ff92008-10-13 21:08:44 +00003111
3112 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
3113
3114 unlock_user_struct(tmp, ptr, 0);
3115 break;
3116 }
3117 default:
3118 ret = do_msgrcv(first, ptr, second, fifth, third);
3119 }
3120 break;
thsd96372e2007-02-02 22:05:44 +00003121
bellard8853f862004-02-22 14:57:26 +00003122 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03003123 switch (version) {
3124 default:
bellard5a4a8982007-11-11 17:39:18 +00003125 {
3126 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03003127 raddr = do_shmat(first, ptr, second);
3128 if (is_error(raddr))
3129 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00003130 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00003131 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03003132 break;
3133 }
3134 case 1:
3135 ret = -TARGET_EINVAL;
3136 break;
bellard5a4a8982007-11-11 17:39:18 +00003137 }
bellard8853f862004-02-22 14:57:26 +00003138 break;
3139 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03003140 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00003141 break;
3142
3143 case IPCOP_shmget:
3144 /* IPC_* flag values are the same on all linux platforms */
3145 ret = get_errno(shmget(first, second, third));
3146 break;
3147
3148 /* IPC_* and SHM_* command values are the same on all linux platforms */
3149 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03003150 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00003151 break;
3152 default:
j_mayer32407102007-09-26 23:01:49 +00003153 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00003154 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00003155 break;
3156 }
3157 return ret;
3158}
j_mayer32407102007-09-26 23:01:49 +00003159#endif
bellard8853f862004-02-22 14:57:26 +00003160
bellard31e31b82003-02-18 22:55:36 +00003161/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00003162
Blue Swirl001faf32009-05-13 17:53:17 +00003163#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00003164#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3165enum {
3166#include "syscall_types.h"
3167};
3168#undef STRUCT
3169#undef STRUCT_SPECIAL
3170
Blue Swirl001faf32009-05-13 17:53:17 +00003171#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00003172#define STRUCT_SPECIAL(name)
3173#include "syscall_types.h"
3174#undef STRUCT
3175#undef STRUCT_SPECIAL
3176
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003177typedef struct IOCTLEntry IOCTLEntry;
3178
3179typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3180 int fd, abi_long cmd, abi_long arg);
3181
3182struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00003183 unsigned int target_cmd;
3184 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00003185 const char *name;
3186 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003187 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003188 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003189};
bellard31e31b82003-02-18 22:55:36 +00003190
3191#define IOC_R 0x0001
3192#define IOC_W 0x0002
3193#define IOC_RW (IOC_R | IOC_W)
3194
3195#define MAX_STRUCT_SIZE 4096
3196
Peter Maydelldace20d2011-01-10 13:11:24 +00003197#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003198/* So fiemap access checks don't overflow on 32 bit systems.
3199 * This is very slightly smaller than the limit imposed by
3200 * the underlying kernel.
3201 */
3202#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3203 / sizeof(struct fiemap_extent))
3204
3205static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3206 int fd, abi_long cmd, abi_long arg)
3207{
3208 /* The parameter for this ioctl is a struct fiemap followed
3209 * by an array of struct fiemap_extent whose size is set
3210 * in fiemap->fm_extent_count. The array is filled in by the
3211 * ioctl.
3212 */
3213 int target_size_in, target_size_out;
3214 struct fiemap *fm;
3215 const argtype *arg_type = ie->arg_type;
3216 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3217 void *argptr, *p;
3218 abi_long ret;
3219 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3220 uint32_t outbufsz;
3221 int free_fm = 0;
3222
3223 assert(arg_type[0] == TYPE_PTR);
3224 assert(ie->access == IOC_RW);
3225 arg_type++;
3226 target_size_in = thunk_type_size(arg_type, 0);
3227 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3228 if (!argptr) {
3229 return -TARGET_EFAULT;
3230 }
3231 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3232 unlock_user(argptr, arg, 0);
3233 fm = (struct fiemap *)buf_temp;
3234 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3235 return -TARGET_EINVAL;
3236 }
3237
3238 outbufsz = sizeof (*fm) +
3239 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3240
3241 if (outbufsz > MAX_STRUCT_SIZE) {
3242 /* We can't fit all the extents into the fixed size buffer.
3243 * Allocate one that is large enough and use it instead.
3244 */
3245 fm = malloc(outbufsz);
3246 if (!fm) {
3247 return -TARGET_ENOMEM;
3248 }
3249 memcpy(fm, buf_temp, sizeof(struct fiemap));
3250 free_fm = 1;
3251 }
3252 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3253 if (!is_error(ret)) {
3254 target_size_out = target_size_in;
3255 /* An extent_count of 0 means we were only counting the extents
3256 * so there are no structs to copy
3257 */
3258 if (fm->fm_extent_count != 0) {
3259 target_size_out += fm->fm_mapped_extents * extent_size;
3260 }
3261 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3262 if (!argptr) {
3263 ret = -TARGET_EFAULT;
3264 } else {
3265 /* Convert the struct fiemap */
3266 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3267 if (fm->fm_extent_count != 0) {
3268 p = argptr + target_size_in;
3269 /* ...and then all the struct fiemap_extents */
3270 for (i = 0; i < fm->fm_mapped_extents; i++) {
3271 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3272 THUNK_TARGET);
3273 p += extent_size;
3274 }
3275 }
3276 unlock_user(argptr, arg, target_size_out);
3277 }
3278 }
3279 if (free_fm) {
3280 free(fm);
3281 }
3282 return ret;
3283}
Peter Maydelldace20d2011-01-10 13:11:24 +00003284#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003285
Laurent Vivier059c2f22011-03-30 00:12:12 +02003286static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3287 int fd, abi_long cmd, abi_long arg)
3288{
3289 const argtype *arg_type = ie->arg_type;
3290 int target_size;
3291 void *argptr;
3292 int ret;
3293 struct ifconf *host_ifconf;
3294 uint32_t outbufsz;
3295 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3296 int target_ifreq_size;
3297 int nb_ifreq;
3298 int free_buf = 0;
3299 int i;
3300 int target_ifc_len;
3301 abi_long target_ifc_buf;
3302 int host_ifc_len;
3303 char *host_ifc_buf;
3304
3305 assert(arg_type[0] == TYPE_PTR);
3306 assert(ie->access == IOC_RW);
3307
3308 arg_type++;
3309 target_size = thunk_type_size(arg_type, 0);
3310
3311 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3312 if (!argptr)
3313 return -TARGET_EFAULT;
3314 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3315 unlock_user(argptr, arg, 0);
3316
3317 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3318 target_ifc_len = host_ifconf->ifc_len;
3319 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3320
3321 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3322 nb_ifreq = target_ifc_len / target_ifreq_size;
3323 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3324
3325 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3326 if (outbufsz > MAX_STRUCT_SIZE) {
3327 /* We can't fit all the extents into the fixed size buffer.
3328 * Allocate one that is large enough and use it instead.
3329 */
3330 host_ifconf = malloc(outbufsz);
3331 if (!host_ifconf) {
3332 return -TARGET_ENOMEM;
3333 }
3334 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3335 free_buf = 1;
3336 }
3337 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3338
3339 host_ifconf->ifc_len = host_ifc_len;
3340 host_ifconf->ifc_buf = host_ifc_buf;
3341
3342 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3343 if (!is_error(ret)) {
3344 /* convert host ifc_len to target ifc_len */
3345
3346 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3347 target_ifc_len = nb_ifreq * target_ifreq_size;
3348 host_ifconf->ifc_len = target_ifc_len;
3349
3350 /* restore target ifc_buf */
3351
3352 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3353
3354 /* copy struct ifconf to target user */
3355
3356 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3357 if (!argptr)
3358 return -TARGET_EFAULT;
3359 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3360 unlock_user(argptr, arg, target_size);
3361
3362 /* copy ifreq[] to target user */
3363
3364 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3365 for (i = 0; i < nb_ifreq ; i++) {
3366 thunk_convert(argptr + i * target_ifreq_size,
3367 host_ifc_buf + i * sizeof(struct ifreq),
3368 ifreq_arg_type, THUNK_TARGET);
3369 }
3370 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3371 }
3372
3373 if (free_buf) {
3374 free(host_ifconf);
3375 }
3376
3377 return ret;
3378}
3379
Alexander Graf56e904e2012-01-31 18:42:06 +01003380static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3381 abi_long cmd, abi_long arg)
3382{
3383 void *argptr;
3384 struct dm_ioctl *host_dm;
3385 abi_long guest_data;
3386 uint32_t guest_data_size;
3387 int target_size;
3388 const argtype *arg_type = ie->arg_type;
3389 abi_long ret;
3390 void *big_buf = NULL;
3391 char *host_data;
3392
3393 arg_type++;
3394 target_size = thunk_type_size(arg_type, 0);
3395 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3396 if (!argptr) {
3397 ret = -TARGET_EFAULT;
3398 goto out;
3399 }
3400 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3401 unlock_user(argptr, arg, 0);
3402
3403 /* buf_temp is too small, so fetch things into a bigger buffer */
3404 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3405 memcpy(big_buf, buf_temp, target_size);
3406 buf_temp = big_buf;
3407 host_dm = big_buf;
3408
3409 guest_data = arg + host_dm->data_start;
3410 if ((guest_data - arg) < 0) {
3411 ret = -EINVAL;
3412 goto out;
3413 }
3414 guest_data_size = host_dm->data_size - host_dm->data_start;
3415 host_data = (char*)host_dm + host_dm->data_start;
3416
3417 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3418 switch (ie->host_cmd) {
3419 case DM_REMOVE_ALL:
3420 case DM_LIST_DEVICES:
3421 case DM_DEV_CREATE:
3422 case DM_DEV_REMOVE:
3423 case DM_DEV_SUSPEND:
3424 case DM_DEV_STATUS:
3425 case DM_DEV_WAIT:
3426 case DM_TABLE_STATUS:
3427 case DM_TABLE_CLEAR:
3428 case DM_TABLE_DEPS:
3429 case DM_LIST_VERSIONS:
3430 /* no input data */
3431 break;
3432 case DM_DEV_RENAME:
3433 case DM_DEV_SET_GEOMETRY:
3434 /* data contains only strings */
3435 memcpy(host_data, argptr, guest_data_size);
3436 break;
3437 case DM_TARGET_MSG:
3438 memcpy(host_data, argptr, guest_data_size);
3439 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3440 break;
3441 case DM_TABLE_LOAD:
3442 {
3443 void *gspec = argptr;
3444 void *cur_data = host_data;
3445 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3446 int spec_size = thunk_type_size(arg_type, 0);
3447 int i;
3448
3449 for (i = 0; i < host_dm->target_count; i++) {
3450 struct dm_target_spec *spec = cur_data;
3451 uint32_t next;
3452 int slen;
3453
3454 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3455 slen = strlen((char*)gspec + spec_size) + 1;
3456 next = spec->next;
3457 spec->next = sizeof(*spec) + slen;
3458 strcpy((char*)&spec[1], gspec + spec_size);
3459 gspec += next;
3460 cur_data += spec->next;
3461 }
3462 break;
3463 }
3464 default:
3465 ret = -TARGET_EINVAL;
3466 goto out;
3467 }
3468 unlock_user(argptr, guest_data, 0);
3469
3470 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3471 if (!is_error(ret)) {
3472 guest_data = arg + host_dm->data_start;
3473 guest_data_size = host_dm->data_size - host_dm->data_start;
3474 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3475 switch (ie->host_cmd) {
3476 case DM_REMOVE_ALL:
3477 case DM_DEV_CREATE:
3478 case DM_DEV_REMOVE:
3479 case DM_DEV_RENAME:
3480 case DM_DEV_SUSPEND:
3481 case DM_DEV_STATUS:
3482 case DM_TABLE_LOAD:
3483 case DM_TABLE_CLEAR:
3484 case DM_TARGET_MSG:
3485 case DM_DEV_SET_GEOMETRY:
3486 /* no return data */
3487 break;
3488 case DM_LIST_DEVICES:
3489 {
3490 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3491 uint32_t remaining_data = guest_data_size;
3492 void *cur_data = argptr;
3493 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3494 int nl_size = 12; /* can't use thunk_size due to alignment */
3495
3496 while (1) {
3497 uint32_t next = nl->next;
3498 if (next) {
3499 nl->next = nl_size + (strlen(nl->name) + 1);
3500 }
3501 if (remaining_data < nl->next) {
3502 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3503 break;
3504 }
3505 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3506 strcpy(cur_data + nl_size, nl->name);
3507 cur_data += nl->next;
3508 remaining_data -= nl->next;
3509 if (!next) {
3510 break;
3511 }
3512 nl = (void*)nl + next;
3513 }
3514 break;
3515 }
3516 case DM_DEV_WAIT:
3517 case DM_TABLE_STATUS:
3518 {
3519 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3520 void *cur_data = argptr;
3521 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3522 int spec_size = thunk_type_size(arg_type, 0);
3523 int i;
3524
3525 for (i = 0; i < host_dm->target_count; i++) {
3526 uint32_t next = spec->next;
3527 int slen = strlen((char*)&spec[1]) + 1;
3528 spec->next = (cur_data - argptr) + spec_size + slen;
3529 if (guest_data_size < spec->next) {
3530 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3531 break;
3532 }
3533 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3534 strcpy(cur_data + spec_size, (char*)&spec[1]);
3535 cur_data = argptr + spec->next;
3536 spec = (void*)host_dm + host_dm->data_start + next;
3537 }
3538 break;
3539 }
3540 case DM_TABLE_DEPS:
3541 {
3542 void *hdata = (void*)host_dm + host_dm->data_start;
3543 int count = *(uint32_t*)hdata;
3544 uint64_t *hdev = hdata + 8;
3545 uint64_t *gdev = argptr + 8;
3546 int i;
3547
3548 *(uint32_t*)argptr = tswap32(count);
3549 for (i = 0; i < count; i++) {
3550 *gdev = tswap64(*hdev);
3551 gdev++;
3552 hdev++;
3553 }
3554 break;
3555 }
3556 case DM_LIST_VERSIONS:
3557 {
3558 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3559 uint32_t remaining_data = guest_data_size;
3560 void *cur_data = argptr;
3561 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3562 int vers_size = thunk_type_size(arg_type, 0);
3563
3564 while (1) {
3565 uint32_t next = vers->next;
3566 if (next) {
3567 vers->next = vers_size + (strlen(vers->name) + 1);
3568 }
3569 if (remaining_data < vers->next) {
3570 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3571 break;
3572 }
3573 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3574 strcpy(cur_data + vers_size, vers->name);
3575 cur_data += vers->next;
3576 remaining_data -= vers->next;
3577 if (!next) {
3578 break;
3579 }
3580 vers = (void*)vers + next;
3581 }
3582 break;
3583 }
3584 default:
3585 ret = -TARGET_EINVAL;
3586 goto out;
3587 }
3588 unlock_user(argptr, guest_data, guest_data_size);
3589
3590 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3591 if (!argptr) {
3592 ret = -TARGET_EFAULT;
3593 goto out;
3594 }
3595 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3596 unlock_user(argptr, arg, target_size);
3597 }
3598out:
3599 if (big_buf) {
3600 free(big_buf);
3601 }
3602 return ret;
3603}
3604
blueswir19f106a72008-10-05 10:52:52 +00003605static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00003606#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003607 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3608#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3609 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00003610#include "ioctls.h"
3611 { 0, 0, },
3612};
3613
pbrook53a59602006-03-25 19:31:22 +00003614/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00003615/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003616static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00003617{
3618 const IOCTLEntry *ie;
3619 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00003620 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003621 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00003622 int target_size;
3623 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00003624
3625 ie = ioctl_entries;
3626 for(;;) {
3627 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003628 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003629 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003630 }
3631 if (ie->target_cmd == cmd)
3632 break;
3633 ie++;
3634 }
3635 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003636#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003637 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003638#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003639 if (ie->do_ioctl) {
3640 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3641 }
3642
bellard31e31b82003-02-18 22:55:36 +00003643 switch(arg_type[0]) {
3644 case TYPE_NULL:
3645 /* no argument */
3646 ret = get_errno(ioctl(fd, ie->host_cmd));
3647 break;
3648 case TYPE_PTRVOID:
3649 case TYPE_INT:
3650 /* int argment */
3651 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3652 break;
3653 case TYPE_PTR:
3654 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00003655 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00003656 switch(ie->access) {
3657 case IOC_R:
3658 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3659 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003660 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3661 if (!argptr)
3662 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003663 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3664 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003665 }
3666 break;
3667 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00003668 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3669 if (!argptr)
3670 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003671 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3672 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003673 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3674 break;
3675 default:
3676 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00003677 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3678 if (!argptr)
3679 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003680 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3681 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003682 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3683 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003684 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3685 if (!argptr)
3686 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003687 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3688 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003689 }
3690 break;
3691 }
3692 break;
3693 default:
j_mayer32407102007-09-26 23:01:49 +00003694 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3695 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003696 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003697 break;
3698 }
3699 return ret;
3700}
3701
blueswir1b39bc502008-10-05 10:51:10 +00003702static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003703 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3704 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3705 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3706 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3707 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3708 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3709 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3710 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3711 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3712 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3713 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3714 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3715 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3716 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3717 { 0, 0, 0, 0 }
3718};
3719
blueswir1b39bc502008-10-05 10:51:10 +00003720static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003721 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3722 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3723 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3724 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3725 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3726 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3727 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3728 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3729 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3730 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3731 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3732 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3733 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3734 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3735 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3736 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3737 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3738 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3739 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3740 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3741 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3742 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3743 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3744 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3745 { 0, 0, 0, 0 }
3746};
3747
blueswir1b39bc502008-10-05 10:51:10 +00003748static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003749 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3750 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3751 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3752 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3753 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3754 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3755 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3756 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3757 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3758 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3759 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3760 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3761 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3762 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3763 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3764 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3765 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3766 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3767 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3768 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3769 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3770 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3771 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3772 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3773 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3774 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3775 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3776 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3777 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3778 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3779 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3780 { 0, 0, 0, 0 }
3781};
3782
blueswir1b39bc502008-10-05 10:51:10 +00003783static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003784 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3785 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3786 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3787 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3788 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3789 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3790 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3791 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3792 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3793 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3794 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3795 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3796 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3797 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3798 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3799 { 0, 0, 0, 0 }
3800};
3801
3802static void target_to_host_termios (void *dst, const void *src)
3803{
3804 struct host_termios *host = dst;
3805 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003806
ths5fafdf22007-09-16 21:08:06 +00003807 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003808 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003809 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003810 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003811 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003812 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003813 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003814 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3815 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003816
Arnaud Patard44607122009-04-21 17:39:08 +03003817 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003818 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3819 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003820 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003821 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003822 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003823 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003824 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003825 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003826 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003827 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3828 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003829 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3830 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3831 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3832 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3833 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003834 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003835}
ths3b46e622007-09-17 08:09:54 +00003836
bellard31e31b82003-02-18 22:55:36 +00003837static void host_to_target_termios (void *dst, const void *src)
3838{
3839 struct target_termios *target = dst;
3840 const struct host_termios *host = src;
3841
ths5fafdf22007-09-16 21:08:06 +00003842 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003843 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003844 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003845 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003846 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003847 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003848 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003849 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3850 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003851
Arnaud Patard44607122009-04-21 17:39:08 +03003852 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003853 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3854 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3855 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3856 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3857 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3858 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3859 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3860 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3861 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3862 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3863 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3864 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3865 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3866 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3867 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3868 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3869 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3870}
3871
blueswir18e853dc2008-10-05 10:49:32 +00003872static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003873 .convert = { host_to_target_termios, target_to_host_termios },
3874 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3875 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3876};
3877
bellard5286db72003-06-05 00:57:30 +00003878static bitmask_transtbl mmap_flags_tbl[] = {
3879 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3880 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3881 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3882 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3883 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3884 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3885 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3886 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3887 { 0, 0, 0, 0 }
3888};
3889
bellard2ab83ea2003-06-15 19:56:46 +00003890#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003891
3892/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003893static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003894
bellard03acab62007-11-11 14:57:14 +00003895static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003896{
3897 int size;
pbrook53a59602006-03-25 19:31:22 +00003898 void *p;
bellard6dbad632003-03-16 18:05:05 +00003899
3900 if (!ldt_table)
3901 return 0;
3902 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3903 if (size > bytecount)
3904 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003905 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3906 if (!p)
bellard03acab62007-11-11 14:57:14 +00003907 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003908 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003909 memcpy(p, ldt_table, size);
3910 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003911 return size;
3912}
3913
3914/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003915static abi_long write_ldt(CPUX86State *env,
3916 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003917{
3918 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003919 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003920 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003921 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003922 uint32_t *lp, entry_1, entry_2;
3923
3924 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003925 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003926 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003927 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003928 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003929 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00003930 ldt_info.limit = tswap32(target_ldt_info->limit);
3931 ldt_info.flags = tswap32(target_ldt_info->flags);
3932 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003933
bellard6dbad632003-03-16 18:05:05 +00003934 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003935 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003936 seg_32bit = ldt_info.flags & 1;
3937 contents = (ldt_info.flags >> 1) & 3;
3938 read_exec_only = (ldt_info.flags >> 3) & 1;
3939 limit_in_pages = (ldt_info.flags >> 4) & 1;
3940 seg_not_present = (ldt_info.flags >> 5) & 1;
3941 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003942#ifdef TARGET_ABI32
3943 lm = 0;
3944#else
3945 lm = (ldt_info.flags >> 7) & 1;
3946#endif
bellard6dbad632003-03-16 18:05:05 +00003947 if (contents == 3) {
3948 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003949 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003950 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003951 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003952 }
3953 /* allocate the LDT */
3954 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003955 env->ldt.base = target_mmap(0,
3956 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3957 PROT_READ|PROT_WRITE,
3958 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3959 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003960 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003961 memset(g2h(env->ldt.base), 0,
3962 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003963 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003964 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003965 }
3966
3967 /* NOTE: same code as Linux kernel */
3968 /* Allow LDTs to be cleared by the user. */
3969 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3970 if (oldmode ||
3971 (contents == 0 &&
3972 read_exec_only == 1 &&
3973 seg_32bit == 0 &&
3974 limit_in_pages == 0 &&
3975 seg_not_present == 1 &&
3976 useable == 0 )) {
3977 entry_1 = 0;
3978 entry_2 = 0;
3979 goto install;
3980 }
3981 }
ths3b46e622007-09-17 08:09:54 +00003982
bellard6dbad632003-03-16 18:05:05 +00003983 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3984 (ldt_info.limit & 0x0ffff);
3985 entry_2 = (ldt_info.base_addr & 0xff000000) |
3986 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3987 (ldt_info.limit & 0xf0000) |
3988 ((read_exec_only ^ 1) << 9) |
3989 (contents << 10) |
3990 ((seg_not_present ^ 1) << 15) |
3991 (seg_32bit << 22) |
3992 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00003993 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00003994 0x7000;
3995 if (!oldmode)
3996 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00003997
bellard6dbad632003-03-16 18:05:05 +00003998 /* Install the new entry ... */
3999install:
4000 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4001 lp[0] = tswap32(entry_1);
4002 lp[1] = tswap32(entry_2);
4003 return 0;
4004}
4005
4006/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00004007static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4008 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00004009{
bellard03acab62007-11-11 14:57:14 +00004010 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00004011
bellard6dbad632003-03-16 18:05:05 +00004012 switch (func) {
4013 case 0:
4014 ret = read_ldt(ptr, bytecount);
4015 break;
4016 case 1:
4017 ret = write_ldt(env, ptr, bytecount, 1);
4018 break;
4019 case 0x11:
4020 ret = write_ldt(env, ptr, bytecount, 0);
4021 break;
bellard03acab62007-11-11 14:57:14 +00004022 default:
4023 ret = -TARGET_ENOSYS;
4024 break;
bellard6dbad632003-03-16 18:05:05 +00004025 }
4026 return ret;
4027}
bellard1b6b0292003-03-22 17:31:38 +00004028
blueswir14583f582008-08-24 10:35:55 +00004029#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00004030static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004031{
4032 uint64_t *gdt_table = g2h(env->gdt.base);
4033 struct target_modify_ldt_ldt_s ldt_info;
4034 struct target_modify_ldt_ldt_s *target_ldt_info;
4035 int seg_32bit, contents, read_exec_only, limit_in_pages;
4036 int seg_not_present, useable, lm;
4037 uint32_t *lp, entry_1, entry_2;
4038 int i;
4039
4040 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4041 if (!target_ldt_info)
4042 return -TARGET_EFAULT;
4043 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004044 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00004045 ldt_info.limit = tswap32(target_ldt_info->limit);
4046 ldt_info.flags = tswap32(target_ldt_info->flags);
4047 if (ldt_info.entry_number == -1) {
4048 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4049 if (gdt_table[i] == 0) {
4050 ldt_info.entry_number = i;
4051 target_ldt_info->entry_number = tswap32(i);
4052 break;
4053 }
4054 }
4055 }
4056 unlock_user_struct(target_ldt_info, ptr, 1);
4057
4058 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4059 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4060 return -TARGET_EINVAL;
4061 seg_32bit = ldt_info.flags & 1;
4062 contents = (ldt_info.flags >> 1) & 3;
4063 read_exec_only = (ldt_info.flags >> 3) & 1;
4064 limit_in_pages = (ldt_info.flags >> 4) & 1;
4065 seg_not_present = (ldt_info.flags >> 5) & 1;
4066 useable = (ldt_info.flags >> 6) & 1;
4067#ifdef TARGET_ABI32
4068 lm = 0;
4069#else
4070 lm = (ldt_info.flags >> 7) & 1;
4071#endif
4072
4073 if (contents == 3) {
4074 if (seg_not_present == 0)
4075 return -TARGET_EINVAL;
4076 }
4077
4078 /* NOTE: same code as Linux kernel */
4079 /* Allow LDTs to be cleared by the user. */
4080 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4081 if ((contents == 0 &&
4082 read_exec_only == 1 &&
4083 seg_32bit == 0 &&
4084 limit_in_pages == 0 &&
4085 seg_not_present == 1 &&
4086 useable == 0 )) {
4087 entry_1 = 0;
4088 entry_2 = 0;
4089 goto install;
4090 }
4091 }
4092
4093 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4094 (ldt_info.limit & 0x0ffff);
4095 entry_2 = (ldt_info.base_addr & 0xff000000) |
4096 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4097 (ldt_info.limit & 0xf0000) |
4098 ((read_exec_only ^ 1) << 9) |
4099 (contents << 10) |
4100 ((seg_not_present ^ 1) << 15) |
4101 (seg_32bit << 22) |
4102 (limit_in_pages << 23) |
4103 (useable << 20) |
4104 (lm << 21) |
4105 0x7000;
4106
4107 /* Install the new entry ... */
4108install:
4109 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4110 lp[0] = tswap32(entry_1);
4111 lp[1] = tswap32(entry_2);
4112 return 0;
4113}
4114
blueswir18fcd3692008-08-17 20:26:25 +00004115static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004116{
4117 struct target_modify_ldt_ldt_s *target_ldt_info;
4118 uint64_t *gdt_table = g2h(env->gdt.base);
4119 uint32_t base_addr, limit, flags;
4120 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4121 int seg_not_present, useable, lm;
4122 uint32_t *lp, entry_1, entry_2;
4123
4124 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4125 if (!target_ldt_info)
4126 return -TARGET_EFAULT;
4127 idx = tswap32(target_ldt_info->entry_number);
4128 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4129 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4130 unlock_user_struct(target_ldt_info, ptr, 1);
4131 return -TARGET_EINVAL;
4132 }
4133 lp = (uint32_t *)(gdt_table + idx);
4134 entry_1 = tswap32(lp[0]);
4135 entry_2 = tswap32(lp[1]);
4136
4137 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4138 contents = (entry_2 >> 10) & 3;
4139 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4140 seg_32bit = (entry_2 >> 22) & 1;
4141 limit_in_pages = (entry_2 >> 23) & 1;
4142 useable = (entry_2 >> 20) & 1;
4143#ifdef TARGET_ABI32
4144 lm = 0;
4145#else
4146 lm = (entry_2 >> 21) & 1;
4147#endif
4148 flags = (seg_32bit << 0) | (contents << 1) |
4149 (read_exec_only << 3) | (limit_in_pages << 4) |
4150 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4151 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4152 base_addr = (entry_1 >> 16) |
4153 (entry_2 & 0xff000000) |
4154 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004155 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00004156 target_ldt_info->limit = tswap32(limit);
4157 target_ldt_info->flags = tswap32(flags);
4158 unlock_user_struct(target_ldt_info, ptr, 1);
4159 return 0;
4160}
blueswir14583f582008-08-24 10:35:55 +00004161#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00004162
bellardd2fd1af2007-11-14 18:08:56 +00004163#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00004164static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00004165{
Juan Quintela1add8692011-06-16 17:37:09 +01004166 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00004167 abi_ulong val;
4168 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01004169
bellardd2fd1af2007-11-14 18:08:56 +00004170 switch(code) {
4171 case TARGET_ARCH_SET_GS:
4172 case TARGET_ARCH_SET_FS:
4173 if (code == TARGET_ARCH_SET_GS)
4174 idx = R_GS;
4175 else
4176 idx = R_FS;
4177 cpu_x86_load_seg(env, idx, 0);
4178 env->segs[idx].base = addr;
4179 break;
4180 case TARGET_ARCH_GET_GS:
4181 case TARGET_ARCH_GET_FS:
4182 if (code == TARGET_ARCH_GET_GS)
4183 idx = R_GS;
4184 else
4185 idx = R_FS;
4186 val = env->segs[idx].base;
4187 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01004188 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00004189 break;
4190 default:
4191 ret = -TARGET_EINVAL;
4192 break;
4193 }
Juan Quintela1add8692011-06-16 17:37:09 +01004194 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00004195}
4196#endif
4197
bellard2ab83ea2003-06-15 19:56:46 +00004198#endif /* defined(TARGET_I386) */
4199
Riku Voipio05098a92011-03-04 15:27:29 +02004200#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00004201
Riku Voipio05098a92011-03-04 15:27:29 +02004202#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004203
4204static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4205typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01004206 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00004207 pthread_mutex_t mutex;
4208 pthread_cond_t cond;
4209 pthread_t thread;
4210 uint32_t tid;
4211 abi_ulong child_tidptr;
4212 abi_ulong parent_tidptr;
4213 sigset_t sigmask;
4214} new_thread_info;
4215
4216static void *clone_func(void *arg)
4217{
4218 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004219 CPUArchState *env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004220 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00004221
4222 env = info->env;
4223 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004224 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004225 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004226 env->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004227 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00004228 if (info->child_tidptr)
4229 put_user_u32(info->tid, info->child_tidptr);
4230 if (info->parent_tidptr)
4231 put_user_u32(info->tid, info->parent_tidptr);
4232 /* Enable signals. */
4233 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4234 /* Signal to the parent that we're ready. */
4235 pthread_mutex_lock(&info->mutex);
4236 pthread_cond_broadcast(&info->cond);
4237 pthread_mutex_unlock(&info->mutex);
4238 /* Wait until the parent has finshed initializing the tls state. */
4239 pthread_mutex_lock(&clone_lock);
4240 pthread_mutex_unlock(&clone_lock);
4241 cpu_loop(env);
4242 /* never exits */
4243 return NULL;
4244}
4245#else
bellard1b6b0292003-03-22 17:31:38 +00004246
4247static int clone_func(void *arg)
4248{
Andreas Färber9349b4f2012-03-14 01:38:32 +01004249 CPUArchState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00004250 cpu_loop(env);
4251 /* never exits */
4252 return 0;
4253}
pbrookd865bab2008-06-07 22:12:17 +00004254#endif
bellard1b6b0292003-03-22 17:31:38 +00004255
ths0da46a62007-10-20 20:23:07 +00004256/* do_fork() Must return host values and target errnos (unlike most
4257 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01004258static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00004259 abi_ulong parent_tidptr, target_ulong newtls,
4260 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00004261{
4262 int ret;
bellard5cd43932003-03-29 16:54:36 +00004263 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004264 CPUArchState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004265#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004266 unsigned int nptl_flags;
4267 sigset_t sigmask;
Riku Voipio91907492010-11-26 16:21:34 +02004268#else
4269 uint8_t *new_stack;
pbrookd865bab2008-06-07 22:12:17 +00004270#endif
ths3b46e622007-09-17 08:09:54 +00004271
balrog436d1242008-09-21 02:39:45 +00004272 /* Emulate vfork() with fork() */
4273 if (flags & CLONE_VFORK)
4274 flags &= ~(CLONE_VFORK | CLONE_VM);
4275
bellard1b6b0292003-03-22 17:31:38 +00004276 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004277 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004278#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004279 new_thread_info info;
4280 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00004281#endif
Anthony Liguori7267c092011-08-20 22:09:37 -05004282 ts = g_malloc0(sizeof(TaskState));
pbrook624f7972008-05-31 16:11:38 +00004283 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00004284 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00004285 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00004286#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
Andreas Färberff18b762012-05-05 14:47:34 +02004287 cpu_reset(ENV_GET_CPU(new_env));
Blue Swirlb4558d72009-11-07 16:34:12 +00004288#endif
pbrook6e68e072008-05-30 17:22:15 +00004289 /* Init regs that differ from the parent. */
4290 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00004291 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004292 ts->bprm = parent_ts->bprm;
4293 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004294#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004295 nptl_flags = flags;
4296 flags &= ~CLONE_NPTL_FLAGS2;
4297
pbrookc2764712009-03-07 15:24:59 +00004298 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4299 ts->child_tidptr = child_tidptr;
4300 }
4301
pbrookd865bab2008-06-07 22:12:17 +00004302 if (nptl_flags & CLONE_SETTLS)
4303 cpu_set_tls (new_env, newtls);
4304
4305 /* Grab a mutex so that thread setup appears atomic. */
4306 pthread_mutex_lock(&clone_lock);
4307
4308 memset(&info, 0, sizeof(info));
4309 pthread_mutex_init(&info.mutex, NULL);
4310 pthread_mutex_lock(&info.mutex);
4311 pthread_cond_init(&info.cond, NULL);
4312 info.env = new_env;
4313 if (nptl_flags & CLONE_CHILD_SETTID)
4314 info.child_tidptr = child_tidptr;
4315 if (nptl_flags & CLONE_PARENT_SETTID)
4316 info.parent_tidptr = parent_tidptr;
4317
4318 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004319 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4320 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00004321 /* It is not safe to deliver signals until the child has finished
4322 initializing, so temporarily block all signals. */
4323 sigfillset(&sigmask);
4324 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4325
4326 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00004327 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00004328
4329 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4330 pthread_attr_destroy(&attr);
4331 if (ret == 0) {
4332 /* Wait for the child to initialize. */
4333 pthread_cond_wait(&info.cond, &info.mutex);
4334 ret = info.tid;
4335 if (flags & CLONE_PARENT_SETTID)
4336 put_user_u32(ret, parent_tidptr);
4337 } else {
4338 ret = -1;
4339 }
4340 pthread_mutex_unlock(&info.mutex);
4341 pthread_cond_destroy(&info.cond);
4342 pthread_mutex_destroy(&info.mutex);
4343 pthread_mutex_unlock(&clone_lock);
4344#else
4345 if (flags & CLONE_NPTL_FLAGS2)
4346 return -EINVAL;
4347 /* This is probably going to die very quickly, but do it anyway. */
Anthony Liguori7267c092011-08-20 22:09:37 -05004348 new_stack = g_malloc0 (NEW_STACK_SIZE);
bellard27725c12003-04-29 21:08:18 +00004349#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02004350 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00004351#else
4352 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
4353#endif
pbrookd865bab2008-06-07 22:12:17 +00004354#endif
bellard1b6b0292003-03-22 17:31:38 +00004355 } else {
4356 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00004357 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00004358 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00004359 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00004360 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00004361 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00004362 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00004363 cpu_clone_regs(env, newsp);
4364 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02004365#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00004366 /* There is a race condition here. The parent process could
4367 theoretically read the TID in the child process before the child
4368 tid is set. This would require using either ptrace
4369 (not implemented) or having *_tidptr to point at a shared memory
4370 mapping. We can't repeat the spinlock hack used above because
4371 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00004372 if (flags & CLONE_CHILD_SETTID)
4373 put_user_u32(gettid(), child_tidptr);
4374 if (flags & CLONE_PARENT_SETTID)
4375 put_user_u32(gettid(), parent_tidptr);
4376 ts = (TaskState *)env->opaque;
4377 if (flags & CLONE_SETTLS)
4378 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00004379 if (flags & CLONE_CHILD_CLEARTID)
4380 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00004381#endif
pbrookd865bab2008-06-07 22:12:17 +00004382 } else {
4383 fork_end(0);
4384 }
bellard1b6b0292003-03-22 17:31:38 +00004385 }
4386 return ret;
4387}
4388
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004389/* warning : doesn't handle linux specific flags... */
4390static int target_to_host_fcntl_cmd(int cmd)
4391{
4392 switch(cmd) {
4393 case TARGET_F_DUPFD:
4394 case TARGET_F_GETFD:
4395 case TARGET_F_SETFD:
4396 case TARGET_F_GETFL:
4397 case TARGET_F_SETFL:
4398 return cmd;
4399 case TARGET_F_GETLK:
4400 return F_GETLK;
4401 case TARGET_F_SETLK:
4402 return F_SETLK;
4403 case TARGET_F_SETLKW:
4404 return F_SETLKW;
4405 case TARGET_F_GETOWN:
4406 return F_GETOWN;
4407 case TARGET_F_SETOWN:
4408 return F_SETOWN;
4409 case TARGET_F_GETSIG:
4410 return F_GETSIG;
4411 case TARGET_F_SETSIG:
4412 return F_SETSIG;
4413#if TARGET_ABI_BITS == 32
4414 case TARGET_F_GETLK64:
4415 return F_GETLK64;
4416 case TARGET_F_SETLK64:
4417 return F_SETLK64;
4418 case TARGET_F_SETLKW64:
4419 return F_SETLKW64;
4420#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004421 case TARGET_F_SETLEASE:
4422 return F_SETLEASE;
4423 case TARGET_F_GETLEASE:
4424 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004425#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004426 case TARGET_F_DUPFD_CLOEXEC:
4427 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004428#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004429 case TARGET_F_NOTIFY:
4430 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004431 default:
4432 return -TARGET_EINVAL;
4433 }
4434 return -TARGET_EINVAL;
4435}
4436
blueswir1992f48a2007-10-14 16:27:31 +00004437static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004438{
4439 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004440 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004441 struct flock64 fl64;
4442 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00004443 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004444 int host_cmd = target_to_host_fcntl_cmd(cmd);
4445
4446 if (host_cmd == -TARGET_EINVAL)
4447 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004448
bellard7775e9e2003-05-14 22:46:48 +00004449 switch(cmd) {
4450 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004451 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4452 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00004453 fl.l_type = tswap16(target_fl->l_type);
4454 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004455 fl.l_start = tswapal(target_fl->l_start);
4456 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004457 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004458 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004459 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004460 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004461 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4462 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00004463 target_fl->l_type = tswap16(fl.l_type);
4464 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004465 target_fl->l_start = tswapal(fl.l_start);
4466 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004467 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004468 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004469 }
4470 break;
ths3b46e622007-09-17 08:09:54 +00004471
bellard7775e9e2003-05-14 22:46:48 +00004472 case TARGET_F_SETLK:
4473 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004474 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4475 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00004476 fl.l_type = tswap16(target_fl->l_type);
4477 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004478 fl.l_start = tswapal(target_fl->l_start);
4479 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004480 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004481 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004482 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004483 break;
ths3b46e622007-09-17 08:09:54 +00004484
bellard7775e9e2003-05-14 22:46:48 +00004485 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004486 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4487 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00004488 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4489 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004490 fl64.l_start = tswap64(target_fl64->l_start);
4491 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004492 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004493 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004494 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004495 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004496 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4497 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00004498 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
4499 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004500 target_fl64->l_start = tswap64(fl64.l_start);
4501 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004502 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004503 unlock_user_struct(target_fl64, arg, 1);
4504 }
bellard9ee1fa22007-11-11 15:11:19 +00004505 break;
bellard7775e9e2003-05-14 22:46:48 +00004506 case TARGET_F_SETLK64:
4507 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004508 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4509 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00004510 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4511 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004512 fl64.l_start = tswap64(target_fl64->l_start);
4513 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004514 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004515 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004516 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004517 break;
4518
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004519 case TARGET_F_GETFL:
4520 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004521 if (ret >= 0) {
4522 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4523 }
bellardffa65c32004-01-04 23:57:22 +00004524 break;
4525
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004526 case TARGET_F_SETFL:
4527 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4528 break;
4529
4530 case TARGET_F_SETOWN:
4531 case TARGET_F_GETOWN:
4532 case TARGET_F_SETSIG:
4533 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004534 case TARGET_F_SETLEASE:
4535 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004536 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00004537 break;
4538
bellard7775e9e2003-05-14 22:46:48 +00004539 default:
bellard9ee1fa22007-11-11 15:11:19 +00004540 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00004541 break;
4542 }
4543 return ret;
4544}
4545
bellard67867302003-11-23 17:05:30 +00004546#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00004547
bellard67867302003-11-23 17:05:30 +00004548static inline int high2lowuid(int uid)
4549{
4550 if (uid > 65535)
4551 return 65534;
4552 else
4553 return uid;
4554}
4555
4556static inline int high2lowgid(int gid)
4557{
4558 if (gid > 65535)
4559 return 65534;
4560 else
4561 return gid;
4562}
4563
4564static inline int low2highuid(int uid)
4565{
4566 if ((int16_t)uid == -1)
4567 return -1;
4568 else
4569 return uid;
4570}
4571
4572static inline int low2highgid(int gid)
4573{
4574 if ((int16_t)gid == -1)
4575 return -1;
4576 else
4577 return gid;
4578}
Riku Voipio0c866a72011-04-18 15:23:06 +03004579static inline int tswapid(int id)
4580{
4581 return tswap16(id);
4582}
4583#else /* !USE_UID16 */
4584static inline int high2lowuid(int uid)
4585{
4586 return uid;
4587}
4588static inline int high2lowgid(int gid)
4589{
4590 return gid;
4591}
4592static inline int low2highuid(int uid)
4593{
4594 return uid;
4595}
4596static inline int low2highgid(int gid)
4597{
4598 return gid;
4599}
4600static inline int tswapid(int id)
4601{
4602 return tswap32(id);
4603}
bellard67867302003-11-23 17:05:30 +00004604#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004605
bellard31e31b82003-02-18 22:55:36 +00004606void syscall_init(void)
4607{
bellard2ab83ea2003-06-15 19:56:46 +00004608 IOCTLEntry *ie;
4609 const argtype *arg_type;
4610 int size;
thsb92c47c2007-11-01 00:07:38 +00004611 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004612
Blue Swirl001faf32009-05-13 17:53:17 +00004613#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004614#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004615#include "syscall_types.h"
4616#undef STRUCT
4617#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00004618
Peter Maydelldd6e9572012-07-23 08:07:22 +00004619 /* Build target_to_host_errno_table[] table from
4620 * host_to_target_errno_table[]. */
4621 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4622 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4623 }
4624
bellard2ab83ea2003-06-15 19:56:46 +00004625 /* we patch the ioctl size if necessary. We rely on the fact that
4626 no ioctl has all the bits at '1' in the size field */
4627 ie = ioctl_entries;
4628 while (ie->target_cmd != 0) {
4629 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4630 TARGET_IOC_SIZEMASK) {
4631 arg_type = ie->arg_type;
4632 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00004633 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00004634 ie->target_cmd);
4635 exit(1);
4636 }
4637 arg_type++;
4638 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00004639 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00004640 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4641 (size << TARGET_IOC_SIZESHIFT);
4642 }
thsb92c47c2007-11-01 00:07:38 +00004643
bellard2ab83ea2003-06-15 19:56:46 +00004644 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00004645#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4646 (defined(__x86_64__) && defined(TARGET_X86_64))
4647 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4648 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4649 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00004650 }
4651#endif
4652 ie++;
4653 }
bellard31e31b82003-02-18 22:55:36 +00004654}
bellardc573ff62004-01-04 15:51:36 +00004655
blueswir1992f48a2007-10-14 16:27:31 +00004656#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004657static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4658{
thsaf325d32008-06-10 15:29:15 +00004659#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004660 return ((uint64_t)word0 << 32) | word1;
4661#else
4662 return ((uint64_t)word1 << 32) | word0;
4663#endif
4664}
blueswir1992f48a2007-10-14 16:27:31 +00004665#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004666static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4667{
4668 return word0;
4669}
blueswir1992f48a2007-10-14 16:27:31 +00004670#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004671
4672#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004673static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4674 abi_long arg2,
4675 abi_long arg3,
4676 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004677{
Riku Voipio48e515d2011-07-12 15:40:51 +03004678 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004679 arg2 = arg3;
4680 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004681 }
pbrookce4defa2006-02-09 16:49:55 +00004682 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4683}
4684#endif
4685
4686#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004687static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4688 abi_long arg2,
4689 abi_long arg3,
4690 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004691{
Riku Voipio48e515d2011-07-12 15:40:51 +03004692 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004693 arg2 = arg3;
4694 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004695 }
pbrookce4defa2006-02-09 16:49:55 +00004696 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4697}
4698#endif
4699
bellard579a97f2007-11-11 14:26:47 +00004700static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4701 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004702{
4703 struct target_timespec *target_ts;
4704
bellard579a97f2007-11-11 14:26:47 +00004705 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4706 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004707 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4708 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004709 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004710 return 0;
pbrook53a59602006-03-25 19:31:22 +00004711}
4712
bellard579a97f2007-11-11 14:26:47 +00004713static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4714 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004715{
4716 struct target_timespec *target_ts;
4717
bellard579a97f2007-11-11 14:26:47 +00004718 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4719 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004720 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4721 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004722 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004723 return 0;
pbrook53a59602006-03-25 19:31:22 +00004724}
4725
aurel329d33b762009-04-08 23:07:05 +00004726#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004727static inline abi_long host_to_target_stat64(void *cpu_env,
4728 abi_ulong target_addr,
4729 struct stat *host_st)
4730{
4731#ifdef TARGET_ARM
4732 if (((CPUARMState *)cpu_env)->eabi) {
4733 struct target_eabi_stat64 *target_st;
4734
4735 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4736 return -TARGET_EFAULT;
4737 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4738 __put_user(host_st->st_dev, &target_st->st_dev);
4739 __put_user(host_st->st_ino, &target_st->st_ino);
4740#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4741 __put_user(host_st->st_ino, &target_st->__st_ino);
4742#endif
4743 __put_user(host_st->st_mode, &target_st->st_mode);
4744 __put_user(host_st->st_nlink, &target_st->st_nlink);
4745 __put_user(host_st->st_uid, &target_st->st_uid);
4746 __put_user(host_st->st_gid, &target_st->st_gid);
4747 __put_user(host_st->st_rdev, &target_st->st_rdev);
4748 __put_user(host_st->st_size, &target_st->st_size);
4749 __put_user(host_st->st_blksize, &target_st->st_blksize);
4750 __put_user(host_st->st_blocks, &target_st->st_blocks);
4751 __put_user(host_st->st_atime, &target_st->target_st_atime);
4752 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4753 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4754 unlock_user_struct(target_st, target_addr, 1);
4755 } else
4756#endif
4757 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004758#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004759 struct target_stat *target_st;
4760#else
balrog6a24a772008-09-20 02:23:36 +00004761 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004762#endif
balrog6a24a772008-09-20 02:23:36 +00004763
4764 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4765 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004766 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004767 __put_user(host_st->st_dev, &target_st->st_dev);
4768 __put_user(host_st->st_ino, &target_st->st_ino);
4769#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4770 __put_user(host_st->st_ino, &target_st->__st_ino);
4771#endif
4772 __put_user(host_st->st_mode, &target_st->st_mode);
4773 __put_user(host_st->st_nlink, &target_st->st_nlink);
4774 __put_user(host_st->st_uid, &target_st->st_uid);
4775 __put_user(host_st->st_gid, &target_st->st_gid);
4776 __put_user(host_st->st_rdev, &target_st->st_rdev);
4777 /* XXX: better use of kernel struct */
4778 __put_user(host_st->st_size, &target_st->st_size);
4779 __put_user(host_st->st_blksize, &target_st->st_blksize);
4780 __put_user(host_st->st_blocks, &target_st->st_blocks);
4781 __put_user(host_st->st_atime, &target_st->target_st_atime);
4782 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4783 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4784 unlock_user_struct(target_st, target_addr, 1);
4785 }
4786
4787 return 0;
4788}
4789#endif
4790
Juan Quintela2f7bb872009-07-27 16:13:24 +02004791#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004792/* ??? Using host futex calls even when target atomic operations
4793 are not really atomic probably breaks things. However implementing
4794 futexes locally would make futexes shared between multiple processes
4795 tricky. However they're probably useless because guest atomic
4796 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004797static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4798 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004799{
4800 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004801 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004802
4803 /* ??? We assume FUTEX_* constants are the same on both host
4804 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004805#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004806 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004807#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004808 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004809#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004810 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004811 case FUTEX_WAIT:
4812 if (timeout) {
4813 pts = &ts;
4814 target_to_host_timespec(pts, timeout);
4815 } else {
4816 pts = NULL;
4817 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004818 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004819 pts, NULL, 0));
4820 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004821 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004822 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004823 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004824 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004825 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004826 case FUTEX_WAKE_OP:
4827 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4828 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4829 But the prototype takes a `struct timespec *'; insert casts
4830 to satisfy the compiler. We do not need to tswap TIMEOUT
4831 since it's not compared to guest memory. */
4832 pts = (struct timespec *)(uintptr_t) timeout;
4833 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4834 g2h(uaddr2),
4835 (base_op == FUTEX_CMP_REQUEUE
4836 ? tswap32(val3)
4837 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004838 default:
4839 return -TARGET_ENOSYS;
4840 }
4841}
4842#endif
4843
pbrook1d9d8b52009-04-16 15:17:02 +00004844/* Map host to target signal numbers for the wait family of syscalls.
4845 Assume all other status bits are the same. */
4846static int host_to_target_waitstatus(int status)
4847{
4848 if (WIFSIGNALED(status)) {
4849 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4850 }
4851 if (WIFSTOPPED(status)) {
4852 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4853 | (status & 0xff);
4854 }
4855 return status;
4856}
4857
pbrooka745ec62008-05-06 15:36:17 +00004858int get_osversion(void)
4859{
4860 static int osversion;
4861 struct new_utsname buf;
4862 const char *s;
4863 int i, n, tmp;
4864 if (osversion)
4865 return osversion;
4866 if (qemu_uname_release && *qemu_uname_release) {
4867 s = qemu_uname_release;
4868 } else {
4869 if (sys_uname(&buf))
4870 return 0;
4871 s = buf.release;
4872 }
4873 tmp = 0;
4874 for (i = 0; i < 3; i++) {
4875 n = 0;
4876 while (*s >= '0' && *s <= '9') {
4877 n *= 10;
4878 n += *s - '0';
4879 s++;
4880 }
4881 tmp = (tmp << 8) + n;
4882 if (*s == '.')
4883 s++;
4884 }
4885 osversion = tmp;
4886 return osversion;
4887}
4888
Alexander Graf36c08d42011-11-02 20:23:24 +01004889
4890static int open_self_maps(void *cpu_env, int fd)
4891{
Alexander Graf1a49ef22012-05-01 16:30:28 +01004892#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Andreas Färber9349b4f2012-03-14 01:38:32 +01004893 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf1a49ef22012-05-01 16:30:28 +01004894#endif
4895 FILE *fp;
4896 char *line = NULL;
4897 size_t len = 0;
4898 ssize_t read;
Alexander Graf36c08d42011-11-02 20:23:24 +01004899
Alexander Graf1a49ef22012-05-01 16:30:28 +01004900 fp = fopen("/proc/self/maps", "r");
4901 if (fp == NULL) {
4902 return -EACCES;
4903 }
4904
4905 while ((read = getline(&line, &len, fp)) != -1) {
4906 int fields, dev_maj, dev_min, inode;
4907 uint64_t min, max, offset;
4908 char flag_r, flag_w, flag_x, flag_p;
4909 char path[512] = "";
4910 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
4911 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
4912 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
4913
4914 if ((fields < 10) || (fields > 11)) {
4915 continue;
4916 }
4917 if (!strncmp(path, "[stack]", 7)) {
4918 continue;
4919 }
4920 if (h2g_valid(min) && h2g_valid(max)) {
4921 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
4922 " %c%c%c%c %08" PRIx64 " %02x:%02x %d%s%s\n",
4923 h2g(min), h2g(max), flag_r, flag_w,
4924 flag_x, flag_p, offset, dev_maj, dev_min, inode,
4925 path[0] ? " " : "", path);
4926 }
4927 }
4928
4929 free(line);
4930 fclose(fp);
4931
4932#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Alexander Graf36c08d42011-11-02 20:23:24 +01004933 dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
4934 (unsigned long long)ts->info->stack_limit,
4935 (unsigned long long)(ts->stack_base + (TARGET_PAGE_SIZE - 1))
4936 & TARGET_PAGE_MASK,
Alexander Graf1a49ef22012-05-01 16:30:28 +01004937 (unsigned long long)0);
4938#endif
Alexander Graf36c08d42011-11-02 20:23:24 +01004939
4940 return 0;
4941}
4942
Alexander Graf480b8e72011-11-02 20:23:25 +01004943static int open_self_stat(void *cpu_env, int fd)
4944{
Andreas Färber9349b4f2012-03-14 01:38:32 +01004945 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf480b8e72011-11-02 20:23:25 +01004946 abi_ulong start_stack = ts->info->start_stack;
4947 int i;
4948
4949 for (i = 0; i < 44; i++) {
4950 char buf[128];
4951 int len;
4952 uint64_t val = 0;
4953
Fabio Erculianie0e65be2012-01-03 09:38:34 +00004954 if (i == 0) {
4955 /* pid */
4956 val = getpid();
4957 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
4958 } else if (i == 1) {
4959 /* app name */
4960 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
4961 } else if (i == 27) {
4962 /* stack bottom */
4963 val = start_stack;
4964 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
4965 } else {
4966 /* for the rest, there is MasterCard */
4967 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
Alexander Graf480b8e72011-11-02 20:23:25 +01004968 }
Fabio Erculianie0e65be2012-01-03 09:38:34 +00004969
Alexander Graf480b8e72011-11-02 20:23:25 +01004970 len = strlen(buf);
4971 if (write(fd, buf, len) != len) {
4972 return -1;
4973 }
4974 }
4975
4976 return 0;
4977}
4978
Alexander Graf257450e2011-11-02 20:23:26 +01004979static int open_self_auxv(void *cpu_env, int fd)
4980{
Andreas Färber9349b4f2012-03-14 01:38:32 +01004981 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01004982 abi_ulong auxv = ts->info->saved_auxv;
4983 abi_ulong len = ts->info->auxv_len;
4984 char *ptr;
4985
4986 /*
4987 * Auxiliary vector is stored in target process stack.
4988 * read in whole auxv vector and copy it to file
4989 */
4990 ptr = lock_user(VERIFY_READ, auxv, len, 0);
4991 if (ptr != NULL) {
4992 while (len > 0) {
4993 ssize_t r;
4994 r = write(fd, ptr, len);
4995 if (r <= 0) {
4996 break;
4997 }
4998 len -= r;
4999 ptr += r;
5000 }
5001 lseek(fd, 0, SEEK_SET);
5002 unlock_user(ptr, auxv, len);
5003 }
5004
5005 return 0;
5006}
5007
Alexander Graf3be14d02011-11-02 20:23:23 +01005008static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
5009{
5010 struct fake_open {
5011 const char *filename;
5012 int (*fill)(void *cpu_env, int fd);
5013 };
5014 const struct fake_open *fake_open;
5015 static const struct fake_open fakes[] = {
Alexander Graf36c08d42011-11-02 20:23:24 +01005016 { "/proc/self/maps", open_self_maps },
Alexander Graf480b8e72011-11-02 20:23:25 +01005017 { "/proc/self/stat", open_self_stat },
Alexander Graf257450e2011-11-02 20:23:26 +01005018 { "/proc/self/auxv", open_self_auxv },
Alexander Graf3be14d02011-11-02 20:23:23 +01005019 { NULL, NULL }
5020 };
5021
5022 for (fake_open = fakes; fake_open->filename; fake_open++) {
5023 if (!strncmp(pathname, fake_open->filename,
5024 strlen(fake_open->filename))) {
5025 break;
5026 }
5027 }
5028
5029 if (fake_open->filename) {
5030 const char *tmpdir;
5031 char filename[PATH_MAX];
5032 int fd, r;
5033
5034 /* create temporary file to map stat to */
5035 tmpdir = getenv("TMPDIR");
5036 if (!tmpdir)
5037 tmpdir = "/tmp";
5038 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5039 fd = mkstemp(filename);
5040 if (fd < 0) {
5041 return fd;
5042 }
5043 unlink(filename);
5044
5045 if ((r = fake_open->fill(cpu_env, fd))) {
5046 close(fd);
5047 return r;
5048 }
5049 lseek(fd, 0, SEEK_SET);
5050
5051 return fd;
5052 }
5053
5054 return get_errno(open(path(pathname), flags, mode));
5055}
5056
ths0da46a62007-10-20 20:23:07 +00005057/* do_syscall() should always have a single exit point at the end so
5058 that actions, such as logging of syscall results, can be performed.
5059 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00005060abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5061 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01005062 abi_long arg5, abi_long arg6, abi_long arg7,
5063 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00005064{
blueswir1992f48a2007-10-14 16:27:31 +00005065 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005066 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00005067 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00005068 void *p;
ths3b46e622007-09-17 08:09:54 +00005069
bellard72f03902003-02-18 23:33:18 +00005070#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00005071 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00005072#endif
thsb92c47c2007-11-01 00:07:38 +00005073 if(do_strace)
5074 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5075
bellard31e31b82003-02-18 22:55:36 +00005076 switch(num) {
5077 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02005078#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00005079 /* In old applications this may be used to implement _exit(2).
5080 However in threaded applictions it is used for thread termination,
5081 and _exit_group is used for application termination.
5082 Do thread termination if we have more then one thread. */
5083 /* FIXME: This probably breaks if a signal arrives. We should probably
5084 be disabling signals. */
5085 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07005086 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005087 CPUArchState **lastp;
5088 CPUArchState *p;
pbrookc2764712009-03-07 15:24:59 +00005089
5090 cpu_list_lock();
5091 lastp = &first_cpu;
5092 p = first_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005093 while (p && p != (CPUArchState *)cpu_env) {
pbrookc2764712009-03-07 15:24:59 +00005094 lastp = &p->next_cpu;
5095 p = p->next_cpu;
5096 }
5097 /* If we didn't find the CPU for this thread then something is
5098 horribly wrong. */
5099 if (!p)
5100 abort();
5101 /* Remove the CPU from the list. */
5102 *lastp = p->next_cpu;
5103 cpu_list_unlock();
Andreas Färber9349b4f2012-03-14 01:38:32 +01005104 ts = ((CPUArchState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00005105 if (ts->child_tidptr) {
5106 put_user_u32(0, ts->child_tidptr);
5107 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5108 NULL, NULL, 0);
5109 }
Nathan Froyd48e15fc2010-10-29 07:48:57 -07005110 thread_env = NULL;
Andreas Färber11ea4092012-04-15 18:51:01 +02005111 object_delete(OBJECT(ENV_GET_CPU(cpu_env)));
Anthony Liguori7267c092011-08-20 22:09:37 -05005112 g_free(ts);
pbrookc2764712009-03-07 15:24:59 +00005113 pthread_exit(NULL);
5114 }
5115#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02005116#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00005117 _mcleanup();
5118#endif
bellarde9009672005-04-26 20:42:36 +00005119 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00005120 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00005121 ret = 0; /* avoid warning */
5122 break;
5123 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00005124 if (arg3 == 0)
5125 ret = 0;
5126 else {
5127 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5128 goto efault;
5129 ret = get_errno(read(arg1, p, arg3));
5130 unlock_user(p, arg2, ret);
5131 }
bellard31e31b82003-02-18 22:55:36 +00005132 break;
5133 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00005134 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5135 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005136 ret = get_errno(write(arg1, p, arg3));
5137 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00005138 break;
5139 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00005140 if (!(p = lock_user_string(arg1)))
5141 goto efault;
Alexander Graf3be14d02011-11-02 20:23:23 +01005142 ret = get_errno(do_open(cpu_env, p,
5143 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5144 arg3));
pbrook53a59602006-03-25 19:31:22 +00005145 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005146 break;
ths82424832007-09-24 09:21:55 +00005147#if defined(TARGET_NR_openat) && defined(__NR_openat)
5148 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00005149 if (!(p = lock_user_string(arg2)))
5150 goto efault;
5151 ret = get_errno(sys_openat(arg1,
5152 path(p),
5153 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5154 arg4));
5155 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00005156 break;
5157#endif
bellard31e31b82003-02-18 22:55:36 +00005158 case TARGET_NR_close:
5159 ret = get_errno(close(arg1));
5160 break;
5161 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00005162 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00005163 break;
5164 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00005165 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00005166 break;
thse5febef2007-04-01 18:31:35 +00005167#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00005168 case TARGET_NR_waitpid:
5169 {
pbrook53a59602006-03-25 19:31:22 +00005170 int status;
5171 ret = get_errno(waitpid(arg1, &status, arg3));
Alexander Graf53795572011-11-24 00:44:43 +01005172 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00005173 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00005174 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005175 }
5176 break;
thse5febef2007-04-01 18:31:35 +00005177#endif
pbrookf0cbb612008-05-30 18:20:05 +00005178#ifdef TARGET_NR_waitid
5179 case TARGET_NR_waitid:
5180 {
5181 siginfo_t info;
5182 info.si_pid = 0;
5183 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5184 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005185 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00005186 goto efault;
5187 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05005188 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00005189 }
5190 }
5191 break;
5192#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005193#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005194 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00005195 if (!(p = lock_user_string(arg1)))
5196 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005197 ret = get_errno(creat(p, arg2));
5198 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005199 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005200#endif
bellard31e31b82003-02-18 22:55:36 +00005201 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00005202 {
5203 void * p2;
5204 p = lock_user_string(arg1);
5205 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005206 if (!p || !p2)
5207 ret = -TARGET_EFAULT;
5208 else
5209 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005210 unlock_user(p2, arg2, 0);
5211 unlock_user(p, arg1, 0);
5212 }
bellard31e31b82003-02-18 22:55:36 +00005213 break;
ths64f0ce42007-09-24 09:25:06 +00005214#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
5215 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00005216 {
5217 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00005218 if (!arg2 || !arg4)
5219 goto efault;
ths64f0ce42007-09-24 09:25:06 +00005220 p = lock_user_string(arg2);
5221 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005222 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005223 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00005224 else
5225 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00005226 unlock_user(p, arg2, 0);
5227 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00005228 }
5229 break;
5230#endif
bellard31e31b82003-02-18 22:55:36 +00005231 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00005232 if (!(p = lock_user_string(arg1)))
5233 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005234 ret = get_errno(unlink(p));
5235 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005236 break;
ths8170f562007-09-24 09:24:11 +00005237#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
5238 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00005239 if (!(p = lock_user_string(arg2)))
5240 goto efault;
5241 ret = get_errno(sys_unlinkat(arg1, p, arg3));
5242 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00005243 break;
balrogb7d35e62007-12-12 00:40:24 +00005244#endif
bellard31e31b82003-02-18 22:55:36 +00005245 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00005246 {
5247 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00005248 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00005249 abi_ulong gp;
5250 abi_ulong guest_argp;
5251 abi_ulong guest_envp;
5252 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00005253 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005254 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00005255
bellardf7341ff2003-03-30 21:00:25 +00005256 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00005257 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00005258 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005259 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005260 goto efault;
ths03aa1972007-12-02 06:28:08 +00005261 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005262 break;
bellard7854b052003-03-29 17:22:23 +00005263 argc++;
bellard2f619692007-11-16 10:46:05 +00005264 }
bellardf7341ff2003-03-30 21:00:25 +00005265 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00005266 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00005267 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005268 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005269 goto efault;
ths03aa1972007-12-02 06:28:08 +00005270 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005271 break;
bellard7854b052003-03-29 17:22:23 +00005272 envc++;
bellard2f619692007-11-16 10:46:05 +00005273 }
bellard7854b052003-03-29 17:22:23 +00005274
bellardf7341ff2003-03-30 21:00:25 +00005275 argp = alloca((argc + 1) * sizeof(void *));
5276 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00005277
pbrookda94d262008-05-30 18:24:00 +00005278 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005279 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005280 if (get_user_ual(addr, gp))
5281 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005282 if (!addr)
5283 break;
bellard2f619692007-11-16 10:46:05 +00005284 if (!(*q = lock_user_string(addr)))
5285 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005286 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005287 }
bellardf7341ff2003-03-30 21:00:25 +00005288 *q = NULL;
5289
pbrookda94d262008-05-30 18:24:00 +00005290 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005291 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005292 if (get_user_ual(addr, gp))
5293 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005294 if (!addr)
5295 break;
bellard2f619692007-11-16 10:46:05 +00005296 if (!(*q = lock_user_string(addr)))
5297 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005298 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005299 }
bellardf7341ff2003-03-30 21:00:25 +00005300 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00005301
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005302 /* This case will not be caught by the host's execve() if its
5303 page size is bigger than the target's. */
5304 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5305 ret = -TARGET_E2BIG;
5306 goto execve_end;
5307 }
bellard2f619692007-11-16 10:46:05 +00005308 if (!(p = lock_user_string(arg1)))
5309 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005310 ret = get_errno(execve(p, argp, envp));
5311 unlock_user(p, arg1, 0);
5312
bellard2f619692007-11-16 10:46:05 +00005313 goto execve_end;
5314
5315 execve_efault:
5316 ret = -TARGET_EFAULT;
5317
5318 execve_end:
pbrook53a59602006-03-25 19:31:22 +00005319 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005320 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005321 if (get_user_ual(addr, gp)
5322 || !addr)
5323 break;
pbrook53a59602006-03-25 19:31:22 +00005324 unlock_user(*q, addr, 0);
5325 }
5326 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005327 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005328 if (get_user_ual(addr, gp)
5329 || !addr)
5330 break;
pbrook53a59602006-03-25 19:31:22 +00005331 unlock_user(*q, addr, 0);
5332 }
bellard7854b052003-03-29 17:22:23 +00005333 }
bellard31e31b82003-02-18 22:55:36 +00005334 break;
5335 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00005336 if (!(p = lock_user_string(arg1)))
5337 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005338 ret = get_errno(chdir(p));
5339 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005340 break;
bellarda315a142005-01-30 22:59:18 +00005341#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00005342 case TARGET_NR_time:
5343 {
pbrook53a59602006-03-25 19:31:22 +00005344 time_t host_time;
5345 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00005346 if (!is_error(ret)
5347 && arg1
5348 && put_user_sal(host_time, arg1))
5349 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005350 }
5351 break;
bellarda315a142005-01-30 22:59:18 +00005352#endif
bellard31e31b82003-02-18 22:55:36 +00005353 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00005354 if (!(p = lock_user_string(arg1)))
5355 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005356 ret = get_errno(mknod(p, arg2, arg3));
5357 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005358 break;
ths75ac37a2007-09-24 09:23:05 +00005359#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
5360 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00005361 if (!(p = lock_user_string(arg2)))
5362 goto efault;
5363 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
5364 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00005365 break;
5366#endif
bellard31e31b82003-02-18 22:55:36 +00005367 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00005368 if (!(p = lock_user_string(arg1)))
5369 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005370 ret = get_errno(chmod(p, arg2));
5371 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005372 break;
bellardebc05482003-09-30 21:08:41 +00005373#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00005374 case TARGET_NR_break:
5375 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005376#endif
5377#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00005378 case TARGET_NR_oldstat:
5379 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005380#endif
bellard31e31b82003-02-18 22:55:36 +00005381 case TARGET_NR_lseek:
5382 ret = get_errno(lseek(arg1, arg2, arg3));
5383 break;
Richard Henderson92317332010-05-03 10:07:53 -07005384#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5385 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00005386 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07005387 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00005388 ret = get_errno(getpid());
5389 break;
Richard Henderson92317332010-05-03 10:07:53 -07005390#endif
5391#ifdef TARGET_NR_getpid
5392 case TARGET_NR_getpid:
5393 ret = get_errno(getpid());
5394 break;
5395#endif
bellard31e31b82003-02-18 22:55:36 +00005396 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00005397 {
5398 /* need to look at the data field */
5399 void *p2, *p3;
5400 p = lock_user_string(arg1);
5401 p2 = lock_user_string(arg2);
5402 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005403 if (!p || !p2 || !p3)
5404 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005405 else {
bellard579a97f2007-11-11 14:26:47 +00005406 /* FIXME - arg5 should be locked, but it isn't clear how to
5407 * do that since it's not guaranteed to be a NULL-terminated
5408 * string.
5409 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005410 if ( ! arg5 )
5411 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
5412 else
5413 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
5414 }
bellard579a97f2007-11-11 14:26:47 +00005415 unlock_user(p, arg1, 0);
5416 unlock_user(p2, arg2, 0);
5417 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00005418 break;
5419 }
thse5febef2007-04-01 18:31:35 +00005420#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00005421 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00005422 if (!(p = lock_user_string(arg1)))
5423 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005424 ret = get_errno(umount(p));
5425 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005426 break;
thse5febef2007-04-01 18:31:35 +00005427#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005428#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005429 case TARGET_NR_stime:
5430 {
pbrook53a59602006-03-25 19:31:22 +00005431 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00005432 if (get_user_sal(host_time, arg1))
5433 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005434 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00005435 }
5436 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005437#endif
bellard31e31b82003-02-18 22:55:36 +00005438 case TARGET_NR_ptrace:
5439 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00005440#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005441 case TARGET_NR_alarm:
5442 ret = alarm(arg1);
5443 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005444#endif
bellardebc05482003-09-30 21:08:41 +00005445#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00005446 case TARGET_NR_oldfstat:
5447 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005448#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005449#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005450 case TARGET_NR_pause:
5451 ret = get_errno(pause());
5452 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005453#endif
thse5febef2007-04-01 18:31:35 +00005454#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00005455 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00005456 {
pbrook53a59602006-03-25 19:31:22 +00005457 struct utimbuf tbuf, *host_tbuf;
5458 struct target_utimbuf *target_tbuf;
5459 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005460 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5461 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005462 tbuf.actime = tswapal(target_tbuf->actime);
5463 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00005464 unlock_user_struct(target_tbuf, arg2, 0);
5465 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00005466 } else {
pbrook53a59602006-03-25 19:31:22 +00005467 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00005468 }
bellard579a97f2007-11-11 14:26:47 +00005469 if (!(p = lock_user_string(arg1)))
5470 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005471 ret = get_errno(utime(p, host_tbuf));
5472 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00005473 }
5474 break;
thse5febef2007-04-01 18:31:35 +00005475#endif
bellard978a66f2004-12-06 22:58:05 +00005476 case TARGET_NR_utimes:
5477 {
bellard978a66f2004-12-06 22:58:05 +00005478 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00005479 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00005480 if (copy_from_user_timeval(&tv[0], arg2)
5481 || copy_from_user_timeval(&tv[1],
5482 arg2 + sizeof(struct target_timeval)))
5483 goto efault;
bellard978a66f2004-12-06 22:58:05 +00005484 tvp = tv;
5485 } else {
5486 tvp = NULL;
5487 }
bellard579a97f2007-11-11 14:26:47 +00005488 if (!(p = lock_user_string(arg1)))
5489 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005490 ret = get_errno(utimes(p, tvp));
5491 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00005492 }
5493 break;
balrogac8a6552008-09-20 02:25:39 +00005494#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
5495 case TARGET_NR_futimesat:
5496 {
5497 struct timeval *tvp, tv[2];
5498 if (arg3) {
5499 if (copy_from_user_timeval(&tv[0], arg3)
5500 || copy_from_user_timeval(&tv[1],
5501 arg3 + sizeof(struct target_timeval)))
5502 goto efault;
5503 tvp = tv;
5504 } else {
5505 tvp = NULL;
5506 }
5507 if (!(p = lock_user_string(arg2)))
5508 goto efault;
5509 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
5510 unlock_user(p, arg2, 0);
5511 }
5512 break;
5513#endif
bellardebc05482003-09-30 21:08:41 +00005514#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00005515 case TARGET_NR_stty:
5516 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005517#endif
5518#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00005519 case TARGET_NR_gtty:
5520 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005521#endif
bellard31e31b82003-02-18 22:55:36 +00005522 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00005523 if (!(p = lock_user_string(arg1)))
5524 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02005525 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00005526 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005527 break;
ths92a34c12007-09-24 09:27:49 +00005528#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5529 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00005530 if (!(p = lock_user_string(arg2)))
5531 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005532 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005533 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00005534 break;
5535#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005536#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005537 case TARGET_NR_nice:
5538 ret = get_errno(nice(arg1));
5539 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005540#endif
bellardebc05482003-09-30 21:08:41 +00005541#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00005542 case TARGET_NR_ftime:
5543 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005544#endif
bellard31e31b82003-02-18 22:55:36 +00005545 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00005546 sync();
5547 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00005548 break;
5549 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00005550 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00005551 break;
5552 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00005553 {
5554 void *p2;
5555 p = lock_user_string(arg1);
5556 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005557 if (!p || !p2)
5558 ret = -TARGET_EFAULT;
5559 else
5560 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005561 unlock_user(p2, arg2, 0);
5562 unlock_user(p, arg1, 0);
5563 }
bellard31e31b82003-02-18 22:55:36 +00005564 break;
ths722183f2007-09-24 09:24:37 +00005565#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
5566 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00005567 {
bellard579a97f2007-11-11 14:26:47 +00005568 void *p2;
ths722183f2007-09-24 09:24:37 +00005569 p = lock_user_string(arg2);
5570 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005571 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005572 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00005573 else
5574 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00005575 unlock_user(p2, arg4, 0);
5576 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00005577 }
5578 break;
5579#endif
bellard31e31b82003-02-18 22:55:36 +00005580 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00005581 if (!(p = lock_user_string(arg1)))
5582 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005583 ret = get_errno(mkdir(p, arg2));
5584 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005585 break;
ths4472ad02007-09-24 09:22:32 +00005586#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
5587 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00005588 if (!(p = lock_user_string(arg2)))
5589 goto efault;
5590 ret = get_errno(sys_mkdirat(arg1, p, arg3));
5591 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00005592 break;
5593#endif
bellard31e31b82003-02-18 22:55:36 +00005594 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00005595 if (!(p = lock_user_string(arg1)))
5596 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005597 ret = get_errno(rmdir(p));
5598 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005599 break;
5600 case TARGET_NR_dup:
5601 ret = get_errno(dup(arg1));
5602 break;
5603 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005604 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00005605 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03005606#ifdef TARGET_NR_pipe2
5607 case TARGET_NR_pipe2:
Richard Hendersone7ea6cb2012-06-01 18:48:39 -07005608 ret = do_pipe(cpu_env, arg1,
5609 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03005610 break;
5611#endif
bellard31e31b82003-02-18 22:55:36 +00005612 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00005613 {
pbrook53a59602006-03-25 19:31:22 +00005614 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00005615 struct tms tms;
5616 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00005617 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00005618 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5619 if (!tmsp)
5620 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005621 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5622 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5623 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5624 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00005625 }
bellardc596ed12003-07-13 17:32:31 +00005626 if (!is_error(ret))
5627 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00005628 }
5629 break;
bellardebc05482003-09-30 21:08:41 +00005630#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00005631 case TARGET_NR_prof:
5632 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005633#endif
thse5febef2007-04-01 18:31:35 +00005634#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00005635 case TARGET_NR_signal:
5636 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005637#endif
bellard31e31b82003-02-18 22:55:36 +00005638 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00005639 if (arg1 == 0) {
5640 ret = get_errno(acct(NULL));
5641 } else {
5642 if (!(p = lock_user_string(arg1)))
5643 goto efault;
5644 ret = get_errno(acct(path(p)));
5645 unlock_user(p, arg1, 0);
5646 }
pbrook24836682006-04-16 14:14:53 +00005647 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005648#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005649 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00005650 if (!(p = lock_user_string(arg1)))
5651 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005652 ret = get_errno(umount2(p, arg2));
5653 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005654 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005655#endif
bellardebc05482003-09-30 21:08:41 +00005656#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00005657 case TARGET_NR_lock:
5658 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005659#endif
bellard31e31b82003-02-18 22:55:36 +00005660 case TARGET_NR_ioctl:
5661 ret = do_ioctl(arg1, arg2, arg3);
5662 break;
5663 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00005664 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00005665 break;
bellardebc05482003-09-30 21:08:41 +00005666#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00005667 case TARGET_NR_mpx:
5668 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005669#endif
bellard31e31b82003-02-18 22:55:36 +00005670 case TARGET_NR_setpgid:
5671 ret = get_errno(setpgid(arg1, arg2));
5672 break;
bellardebc05482003-09-30 21:08:41 +00005673#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00005674 case TARGET_NR_ulimit:
5675 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005676#endif
5677#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00005678 case TARGET_NR_oldolduname:
5679 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005680#endif
bellard31e31b82003-02-18 22:55:36 +00005681 case TARGET_NR_umask:
5682 ret = get_errno(umask(arg1));
5683 break;
5684 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00005685 if (!(p = lock_user_string(arg1)))
5686 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005687 ret = get_errno(chroot(p));
5688 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005689 break;
5690 case TARGET_NR_ustat:
5691 goto unimplemented;
5692 case TARGET_NR_dup2:
5693 ret = get_errno(dup2(arg1, arg2));
5694 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03005695#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5696 case TARGET_NR_dup3:
5697 ret = get_errno(dup3(arg1, arg2, arg3));
5698 break;
5699#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005700#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005701 case TARGET_NR_getppid:
5702 ret = get_errno(getppid());
5703 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005704#endif
bellard31e31b82003-02-18 22:55:36 +00005705 case TARGET_NR_getpgrp:
5706 ret = get_errno(getpgrp());
5707 break;
5708 case TARGET_NR_setsid:
5709 ret = get_errno(setsid());
5710 break;
thse5febef2007-04-01 18:31:35 +00005711#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00005712 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00005713 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005714#if defined(TARGET_ALPHA)
5715 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00005716 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00005717 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005718 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5719 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005720 act._sa_handler = old_act->_sa_handler;
5721 target_siginitset(&act.sa_mask, old_act->sa_mask);
5722 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005723 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00005724 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005725 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00005726 }
5727 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00005728 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005729 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5730 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005731 old_act->_sa_handler = oact._sa_handler;
5732 old_act->sa_mask = oact.sa_mask.sig[0];
5733 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00005734 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00005735 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005736#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00005737 struct target_sigaction act, oact, *pact, *old_act;
5738
5739 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005740 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5741 goto efault;
bellard106ec872006-06-27 21:08:10 +00005742 act._sa_handler = old_act->_sa_handler;
5743 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5744 act.sa_flags = old_act->sa_flags;
5745 unlock_user_struct(old_act, arg2, 0);
5746 pact = &act;
5747 } else {
5748 pact = NULL;
5749 }
5750
5751 ret = get_errno(do_sigaction(arg1, pact, &oact));
5752
5753 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005754 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5755 goto efault;
bellard106ec872006-06-27 21:08:10 +00005756 old_act->_sa_handler = oact._sa_handler;
5757 old_act->sa_flags = oact.sa_flags;
5758 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5759 old_act->sa_mask.sig[1] = 0;
5760 old_act->sa_mask.sig[2] = 0;
5761 old_act->sa_mask.sig[3] = 0;
5762 unlock_user_struct(old_act, arg3, 1);
5763 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005764#else
5765 struct target_old_sigaction *old_act;
5766 struct target_sigaction act, oact, *pact;
5767 if (arg2) {
5768 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5769 goto efault;
5770 act._sa_handler = old_act->_sa_handler;
5771 target_siginitset(&act.sa_mask, old_act->sa_mask);
5772 act.sa_flags = old_act->sa_flags;
5773 act.sa_restorer = old_act->sa_restorer;
5774 unlock_user_struct(old_act, arg2, 0);
5775 pact = &act;
5776 } else {
5777 pact = NULL;
5778 }
5779 ret = get_errno(do_sigaction(arg1, pact, &oact));
5780 if (!is_error(ret) && arg3) {
5781 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5782 goto efault;
5783 old_act->_sa_handler = oact._sa_handler;
5784 old_act->sa_mask = oact.sa_mask.sig[0];
5785 old_act->sa_flags = oact.sa_flags;
5786 old_act->sa_restorer = oact.sa_restorer;
5787 unlock_user_struct(old_act, arg3, 1);
5788 }
ths388bb212007-05-13 13:58:00 +00005789#endif
bellard31e31b82003-02-18 22:55:36 +00005790 }
5791 break;
thse5febef2007-04-01 18:31:35 +00005792#endif
bellard66fb9762003-03-23 01:06:05 +00005793 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00005794 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005795#if defined(TARGET_ALPHA)
5796 struct target_sigaction act, oact, *pact = 0;
5797 struct target_rt_sigaction *rt_act;
5798 /* ??? arg4 == sizeof(sigset_t). */
5799 if (arg2) {
5800 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5801 goto efault;
5802 act._sa_handler = rt_act->_sa_handler;
5803 act.sa_mask = rt_act->sa_mask;
5804 act.sa_flags = rt_act->sa_flags;
5805 act.sa_restorer = arg5;
5806 unlock_user_struct(rt_act, arg2, 0);
5807 pact = &act;
5808 }
5809 ret = get_errno(do_sigaction(arg1, pact, &oact));
5810 if (!is_error(ret) && arg3) {
5811 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5812 goto efault;
5813 rt_act->_sa_handler = oact._sa_handler;
5814 rt_act->sa_mask = oact.sa_mask;
5815 rt_act->sa_flags = oact.sa_flags;
5816 unlock_user_struct(rt_act, arg3, 1);
5817 }
5818#else
pbrook53a59602006-03-25 19:31:22 +00005819 struct target_sigaction *act;
5820 struct target_sigaction *oact;
5821
bellard579a97f2007-11-11 14:26:47 +00005822 if (arg2) {
5823 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5824 goto efault;
5825 } else
pbrook53a59602006-03-25 19:31:22 +00005826 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005827 if (arg3) {
5828 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5829 ret = -TARGET_EFAULT;
5830 goto rt_sigaction_fail;
5831 }
5832 } else
pbrook53a59602006-03-25 19:31:22 +00005833 oact = NULL;
5834 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005835 rt_sigaction_fail:
5836 if (act)
pbrook53a59602006-03-25 19:31:22 +00005837 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005838 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005839 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005840#endif
pbrook53a59602006-03-25 19:31:22 +00005841 }
bellard66fb9762003-03-23 01:06:05 +00005842 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005843#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005844 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005845 {
5846 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005847 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005848 sigprocmask(0, NULL, &cur_set);
5849 host_to_target_old_sigset(&target_set, &cur_set);
5850 ret = target_set;
5851 }
5852 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005853#endif
5854#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005855 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005856 {
5857 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005858 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00005859 sigprocmask(0, NULL, &cur_set);
5860 target_to_host_old_sigset(&set, &target_set);
5861 sigorset(&set, &set, &cur_set);
5862 sigprocmask(SIG_SETMASK, &set, &oset);
5863 host_to_target_old_sigset(&target_set, &oset);
5864 ret = target_set;
5865 }
5866 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005867#endif
thse5febef2007-04-01 18:31:35 +00005868#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00005869 case TARGET_NR_sigprocmask:
5870 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005871#if defined(TARGET_ALPHA)
5872 sigset_t set, oldset;
5873 abi_ulong mask;
5874 int how;
5875
5876 switch (arg1) {
5877 case TARGET_SIG_BLOCK:
5878 how = SIG_BLOCK;
5879 break;
5880 case TARGET_SIG_UNBLOCK:
5881 how = SIG_UNBLOCK;
5882 break;
5883 case TARGET_SIG_SETMASK:
5884 how = SIG_SETMASK;
5885 break;
5886 default:
5887 ret = -TARGET_EINVAL;
5888 goto fail;
5889 }
5890 mask = arg2;
5891 target_to_host_old_sigset(&set, &mask);
5892
5893 ret = get_errno(sigprocmask(how, &set, &oldset));
Richard Hendersona5b3b132010-05-03 10:07:55 -07005894 if (!is_error(ret)) {
5895 host_to_target_old_sigset(&mask, &oldset);
5896 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07005897 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07005898 }
5899#else
bellard66fb9762003-03-23 01:06:05 +00005900 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07005901 int how;
ths3b46e622007-09-17 08:09:54 +00005902
pbrook53a59602006-03-25 19:31:22 +00005903 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005904 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00005905 case TARGET_SIG_BLOCK:
5906 how = SIG_BLOCK;
5907 break;
5908 case TARGET_SIG_UNBLOCK:
5909 how = SIG_UNBLOCK;
5910 break;
5911 case TARGET_SIG_SETMASK:
5912 how = SIG_SETMASK;
5913 break;
5914 default:
ths0da46a62007-10-20 20:23:07 +00005915 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005916 goto fail;
5917 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005918 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005919 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005920 target_to_host_old_sigset(&set, p);
5921 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005922 set_ptr = &set;
5923 } else {
5924 how = 0;
5925 set_ptr = NULL;
5926 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005927 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005928 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005929 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005930 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005931 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005932 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005933 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005934#endif
bellard66fb9762003-03-23 01:06:05 +00005935 }
5936 break;
thse5febef2007-04-01 18:31:35 +00005937#endif
bellard66fb9762003-03-23 01:06:05 +00005938 case TARGET_NR_rt_sigprocmask:
5939 {
5940 int how = arg1;
5941 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00005942
pbrook53a59602006-03-25 19:31:22 +00005943 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005944 switch(how) {
5945 case TARGET_SIG_BLOCK:
5946 how = SIG_BLOCK;
5947 break;
5948 case TARGET_SIG_UNBLOCK:
5949 how = SIG_UNBLOCK;
5950 break;
5951 case TARGET_SIG_SETMASK:
5952 how = SIG_SETMASK;
5953 break;
5954 default:
ths0da46a62007-10-20 20:23:07 +00005955 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005956 goto fail;
5957 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005958 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005959 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005960 target_to_host_sigset(&set, p);
5961 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005962 set_ptr = &set;
5963 } else {
5964 how = 0;
5965 set_ptr = NULL;
5966 }
5967 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005968 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005969 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005970 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005971 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005972 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005973 }
5974 }
5975 break;
thse5febef2007-04-01 18:31:35 +00005976#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005977 case TARGET_NR_sigpending:
5978 {
5979 sigset_t set;
5980 ret = get_errno(sigpending(&set));
5981 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005982 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005983 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005984 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005985 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005986 }
5987 }
5988 break;
thse5febef2007-04-01 18:31:35 +00005989#endif
bellard66fb9762003-03-23 01:06:05 +00005990 case TARGET_NR_rt_sigpending:
5991 {
5992 sigset_t set;
5993 ret = get_errno(sigpending(&set));
5994 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005995 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005996 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005997 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005998 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005999 }
6000 }
6001 break;
thse5febef2007-04-01 18:31:35 +00006002#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00006003 case TARGET_NR_sigsuspend:
6004 {
6005 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07006006#if defined(TARGET_ALPHA)
6007 abi_ulong mask = arg1;
6008 target_to_host_old_sigset(&set, &mask);
6009#else
Anthony Liguoric227f092009-10-01 16:12:16 -05006010 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006011 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006012 target_to_host_old_sigset(&set, p);
6013 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07006014#endif
bellard66fb9762003-03-23 01:06:05 +00006015 ret = get_errno(sigsuspend(&set));
6016 }
6017 break;
thse5febef2007-04-01 18:31:35 +00006018#endif
bellard66fb9762003-03-23 01:06:05 +00006019 case TARGET_NR_rt_sigsuspend:
6020 {
6021 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05006022 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006023 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006024 target_to_host_sigset(&set, p);
6025 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006026 ret = get_errno(sigsuspend(&set));
6027 }
6028 break;
6029 case TARGET_NR_rt_sigtimedwait:
6030 {
bellard66fb9762003-03-23 01:06:05 +00006031 sigset_t set;
6032 struct timespec uts, *puts;
6033 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00006034
Anthony Liguoric227f092009-10-01 16:12:16 -05006035 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006036 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006037 target_to_host_sigset(&set, p);
6038 unlock_user(p, arg1, 0);
6039 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00006040 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00006041 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00006042 } else {
6043 puts = NULL;
6044 }
6045 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00006046 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006047 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006048 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006049 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05006050 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00006051 }
6052 }
6053 break;
6054 case TARGET_NR_rt_sigqueueinfo:
6055 {
6056 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05006057 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006058 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006059 target_to_host_siginfo(&uinfo, p);
6060 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006061 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6062 }
6063 break;
thse5febef2007-04-01 18:31:35 +00006064#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00006065 case TARGET_NR_sigreturn:
6066 /* NOTE: ret is eax, so not transcoding must be done */
6067 ret = do_sigreturn(cpu_env);
6068 break;
thse5febef2007-04-01 18:31:35 +00006069#endif
bellard66fb9762003-03-23 01:06:05 +00006070 case TARGET_NR_rt_sigreturn:
6071 /* NOTE: ret is eax, so not transcoding must be done */
6072 ret = do_rt_sigreturn(cpu_env);
6073 break;
bellard31e31b82003-02-18 22:55:36 +00006074 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00006075 if (!(p = lock_user_string(arg1)))
6076 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006077 ret = get_errno(sethostname(p, arg2));
6078 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006079 break;
6080 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00006081 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006082 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006083 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006084 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00006085 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6086 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006087 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6088 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006089 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00006090 ret = get_errno(setrlimit(resource, &rlim));
6091 }
6092 break;
bellard31e31b82003-02-18 22:55:36 +00006093 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00006094 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006095 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006096 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006097 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00006098
bellard9de5e442003-03-23 16:49:39 +00006099 ret = get_errno(getrlimit(resource, &rlim));
6100 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006101 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6102 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006103 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6104 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006105 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00006106 }
6107 }
6108 break;
bellard31e31b82003-02-18 22:55:36 +00006109 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00006110 {
6111 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00006112 ret = get_errno(getrusage(arg1, &rusage));
6113 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006114 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00006115 }
6116 }
6117 break;
bellard31e31b82003-02-18 22:55:36 +00006118 case TARGET_NR_gettimeofday:
6119 {
bellard31e31b82003-02-18 22:55:36 +00006120 struct timeval tv;
6121 ret = get_errno(gettimeofday(&tv, NULL));
6122 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00006123 if (copy_to_user_timeval(arg1, &tv))
6124 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006125 }
6126 }
6127 break;
6128 case TARGET_NR_settimeofday:
6129 {
bellard31e31b82003-02-18 22:55:36 +00006130 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00006131 if (copy_from_user_timeval(&tv, arg1))
6132 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006133 ret = get_errno(settimeofday(&tv, NULL));
6134 }
6135 break;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006136#if defined(TARGET_NR_select) && !defined(TARGET_S390X) && !defined(TARGET_S390)
bellard31e31b82003-02-18 22:55:36 +00006137 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00006138 {
pbrook53a59602006-03-25 19:31:22 +00006139 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00006140 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00006141 long nsel;
6142
bellard579a97f2007-11-11 14:26:47 +00006143 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6144 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006145 nsel = tswapal(sel->n);
6146 inp = tswapal(sel->inp);
6147 outp = tswapal(sel->outp);
6148 exp = tswapal(sel->exp);
6149 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00006150 unlock_user_struct(sel, arg1, 0);
6151 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00006152 }
6153 break;
bellard048f6b42005-11-26 18:47:20 +00006154#endif
Riku Voipio9e423822010-05-07 12:28:05 +00006155#ifdef TARGET_NR_pselect6
6156 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04006157 {
6158 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6159 fd_set rfds, wfds, efds;
6160 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6161 struct timespec ts, *ts_ptr;
6162
6163 /*
6164 * The 6th arg is actually two args smashed together,
6165 * so we cannot use the C library.
6166 */
6167 sigset_t set;
6168 struct {
6169 sigset_t *set;
6170 size_t size;
6171 } sig, *sig_ptr;
6172
6173 abi_ulong arg_sigset, arg_sigsize, *arg7;
6174 target_sigset_t *target_sigset;
6175
6176 n = arg1;
6177 rfd_addr = arg2;
6178 wfd_addr = arg3;
6179 efd_addr = arg4;
6180 ts_addr = arg5;
6181
6182 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6183 if (ret) {
6184 goto fail;
6185 }
6186 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6187 if (ret) {
6188 goto fail;
6189 }
6190 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6191 if (ret) {
6192 goto fail;
6193 }
6194
6195 /*
6196 * This takes a timespec, and not a timeval, so we cannot
6197 * use the do_select() helper ...
6198 */
6199 if (ts_addr) {
6200 if (target_to_host_timespec(&ts, ts_addr)) {
6201 goto efault;
6202 }
6203 ts_ptr = &ts;
6204 } else {
6205 ts_ptr = NULL;
6206 }
6207
6208 /* Extract the two packed args for the sigset */
6209 if (arg6) {
6210 sig_ptr = &sig;
6211 sig.size = _NSIG / 8;
6212
6213 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6214 if (!arg7) {
6215 goto efault;
6216 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006217 arg_sigset = tswapal(arg7[0]);
6218 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04006219 unlock_user(arg7, arg6, 0);
6220
6221 if (arg_sigset) {
6222 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01006223 if (arg_sigsize != sizeof(*target_sigset)) {
6224 /* Like the kernel, we enforce correct size sigsets */
6225 ret = -TARGET_EINVAL;
6226 goto fail;
6227 }
Mike Frysinger055e0902011-06-03 17:01:49 -04006228 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6229 sizeof(*target_sigset), 1);
6230 if (!target_sigset) {
6231 goto efault;
6232 }
6233 target_to_host_sigset(&set, target_sigset);
6234 unlock_user(target_sigset, arg_sigset, 0);
6235 } else {
6236 sig.set = NULL;
6237 }
6238 } else {
6239 sig_ptr = NULL;
6240 }
6241
6242 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6243 ts_ptr, sig_ptr));
6244
6245 if (!is_error(ret)) {
6246 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6247 goto efault;
6248 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6249 goto efault;
6250 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6251 goto efault;
6252
6253 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6254 goto efault;
6255 }
6256 }
6257 break;
Riku Voipio9e423822010-05-07 12:28:05 +00006258#endif
bellard31e31b82003-02-18 22:55:36 +00006259 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00006260 {
6261 void *p2;
6262 p = lock_user_string(arg1);
6263 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006264 if (!p || !p2)
6265 ret = -TARGET_EFAULT;
6266 else
6267 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006268 unlock_user(p2, arg2, 0);
6269 unlock_user(p, arg1, 0);
6270 }
bellard31e31b82003-02-18 22:55:36 +00006271 break;
thsf0b62432007-09-24 09:25:40 +00006272#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
6273 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00006274 {
bellard579a97f2007-11-11 14:26:47 +00006275 void *p2;
thsf0b62432007-09-24 09:25:40 +00006276 p = lock_user_string(arg1);
6277 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00006278 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006279 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00006280 else
6281 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00006282 unlock_user(p2, arg3, 0);
6283 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00006284 }
6285 break;
6286#endif
bellardebc05482003-09-30 21:08:41 +00006287#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00006288 case TARGET_NR_oldlstat:
6289 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006290#endif
bellard31e31b82003-02-18 22:55:36 +00006291 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00006292 {
aurel32d088d662009-01-30 20:09:01 +00006293 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00006294 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00006295 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
6296 if (!p || !p2)
6297 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00006298 else {
6299 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
6300 char real[PATH_MAX];
6301 temp = realpath(exec_path,real);
6302 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
6303 snprintf((char *)p2, arg3, "%s", real);
6304 }
6305 else
6306 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00006307 }
pbrook53a59602006-03-25 19:31:22 +00006308 unlock_user(p2, arg2, ret);
6309 unlock_user(p, arg1, 0);
6310 }
bellard31e31b82003-02-18 22:55:36 +00006311 break;
ths5e0ccb12007-09-24 09:26:10 +00006312#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
6313 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00006314 {
bellard579a97f2007-11-11 14:26:47 +00006315 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00006316 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006317 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
6318 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006319 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00006320 else
6321 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00006322 unlock_user(p2, arg3, ret);
6323 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00006324 }
6325 break;
6326#endif
thse5febef2007-04-01 18:31:35 +00006327#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00006328 case TARGET_NR_uselib:
6329 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006330#endif
6331#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00006332 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00006333 if (!(p = lock_user_string(arg1)))
6334 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006335 ret = get_errno(swapon(p, arg2));
6336 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006337 break;
thse5febef2007-04-01 18:31:35 +00006338#endif
bellard31e31b82003-02-18 22:55:36 +00006339 case TARGET_NR_reboot:
Alexander Graf0f6b4d22011-09-27 14:39:42 +02006340 if (!(p = lock_user_string(arg4)))
6341 goto efault;
6342 ret = reboot(arg1, arg2, arg3, p);
6343 unlock_user(p, arg4, 0);
6344 break;
thse5febef2007-04-01 18:31:35 +00006345#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00006346 case TARGET_NR_readdir:
6347 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006348#endif
6349#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00006350 case TARGET_NR_mmap:
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006351#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || \
6352 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6353 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00006354 {
blueswir1992f48a2007-10-14 16:27:31 +00006355 abi_ulong *v;
6356 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00006357 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6358 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006359 v1 = tswapal(v[0]);
6360 v2 = tswapal(v[1]);
6361 v3 = tswapal(v[2]);
6362 v4 = tswapal(v[3]);
6363 v5 = tswapal(v[4]);
6364 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00006365 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00006366 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00006367 target_to_host_bitmask(v4, mmap_flags_tbl),
6368 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00006369 }
bellard31e31b82003-02-18 22:55:36 +00006370#else
ths5fafdf22007-09-16 21:08:06 +00006371 ret = get_errno(target_mmap(arg1, arg2, arg3,
6372 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00006373 arg5,
6374 arg6));
bellard31e31b82003-02-18 22:55:36 +00006375#endif
bellard6fb883e2003-07-09 17:12:39 +00006376 break;
thse5febef2007-04-01 18:31:35 +00006377#endif
bellarda315a142005-01-30 22:59:18 +00006378#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00006379 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00006380#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00006381#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00006382#endif
ths5fafdf22007-09-16 21:08:06 +00006383 ret = get_errno(target_mmap(arg1, arg2, arg3,
6384 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00006385 arg5,
bellardc573ff62004-01-04 15:51:36 +00006386 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00006387 break;
bellarda315a142005-01-30 22:59:18 +00006388#endif
bellard31e31b82003-02-18 22:55:36 +00006389 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00006390 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006391 break;
bellard9de5e442003-03-23 16:49:39 +00006392 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01006393 {
Andreas Färber9349b4f2012-03-14 01:38:32 +01006394 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Paul Brook97374d32010-06-16 13:03:51 +01006395 /* Special hack to detect libc making the stack executable. */
6396 if ((arg3 & PROT_GROWSDOWN)
6397 && arg1 >= ts->info->stack_limit
6398 && arg1 <= ts->info->start_stack) {
6399 arg3 &= ~PROT_GROWSDOWN;
6400 arg2 = arg2 + arg1 - ts->info->stack_limit;
6401 arg1 = ts->info->stack_limit;
6402 }
6403 }
bellard54936002003-05-13 00:25:15 +00006404 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006405 break;
thse5febef2007-04-01 18:31:35 +00006406#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00006407 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00006408 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00006409 break;
thse5febef2007-04-01 18:31:35 +00006410#endif
pbrook53a59602006-03-25 19:31:22 +00006411 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00006412#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00006413 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00006414 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006415 break;
thse5febef2007-04-01 18:31:35 +00006416#endif
6417#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00006418 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00006419 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006420 break;
thse5febef2007-04-01 18:31:35 +00006421#endif
6422#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00006423 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00006424 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006425 break;
thse5febef2007-04-01 18:31:35 +00006426#endif
6427#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00006428 case TARGET_NR_mlockall:
6429 ret = get_errno(mlockall(arg1));
6430 break;
thse5febef2007-04-01 18:31:35 +00006431#endif
6432#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00006433 case TARGET_NR_munlockall:
6434 ret = get_errno(munlockall());
6435 break;
thse5febef2007-04-01 18:31:35 +00006436#endif
bellard31e31b82003-02-18 22:55:36 +00006437 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00006438 if (!(p = lock_user_string(arg1)))
6439 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006440 ret = get_errno(truncate(p, arg2));
6441 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006442 break;
6443 case TARGET_NR_ftruncate:
6444 ret = get_errno(ftruncate(arg1, arg2));
6445 break;
6446 case TARGET_NR_fchmod:
6447 ret = get_errno(fchmod(arg1, arg2));
6448 break;
ths814d7972007-09-24 09:26:51 +00006449#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
6450 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00006451 if (!(p = lock_user_string(arg2)))
6452 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00006453 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00006454 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00006455 break;
6456#endif
bellard31e31b82003-02-18 22:55:36 +00006457 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -07006458 /* Note that negative values are valid for getpriority, so we must
6459 differentiate based on errno settings. */
6460 errno = 0;
6461 ret = getpriority(arg1, arg2);
6462 if (ret == -1 && errno != 0) {
6463 ret = -host_to_target_errno(errno);
6464 break;
6465 }
6466#ifdef TARGET_ALPHA
6467 /* Return value is the unbiased priority. Signal no error. */
6468 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
6469#else
6470 /* Return value is a biased priority to avoid negative numbers. */
6471 ret = 20 - ret;
6472#endif
bellard31e31b82003-02-18 22:55:36 +00006473 break;
6474 case TARGET_NR_setpriority:
6475 ret = get_errno(setpriority(arg1, arg2, arg3));
6476 break;
bellardebc05482003-09-30 21:08:41 +00006477#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00006478 case TARGET_NR_profil:
6479 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006480#endif
bellard31e31b82003-02-18 22:55:36 +00006481 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00006482 if (!(p = lock_user_string(arg1)))
6483 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006484 ret = get_errno(statfs(path(p), &stfs));
6485 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006486 convert_statfs:
6487 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006488 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006489
bellard579a97f2007-11-11 14:26:47 +00006490 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6491 goto efault;
6492 __put_user(stfs.f_type, &target_stfs->f_type);
6493 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6494 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6495 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6496 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6497 __put_user(stfs.f_files, &target_stfs->f_files);
6498 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6499 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6500 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6501 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00006502 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006503 }
6504 break;
6505 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00006506 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00006507 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00006508#ifdef TARGET_NR_statfs64
6509 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00006510 if (!(p = lock_user_string(arg1)))
6511 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006512 ret = get_errno(statfs(path(p), &stfs));
6513 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00006514 convert_statfs64:
6515 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006516 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006517
bellard579a97f2007-11-11 14:26:47 +00006518 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6519 goto efault;
6520 __put_user(stfs.f_type, &target_stfs->f_type);
6521 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6522 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6523 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6524 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6525 __put_user(stfs.f_files, &target_stfs->f_files);
6526 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6527 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6528 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6529 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
6530 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00006531 }
6532 break;
6533 case TARGET_NR_fstatfs64:
6534 ret = get_errno(fstatfs(arg1, &stfs));
6535 goto convert_statfs64;
6536#endif
bellardebc05482003-09-30 21:08:41 +00006537#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00006538 case TARGET_NR_ioperm:
6539 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006540#endif
thse5febef2007-04-01 18:31:35 +00006541#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00006542 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00006543 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00006544 break;
thse5febef2007-04-01 18:31:35 +00006545#endif
bellard3532fa72006-06-24 15:06:03 +00006546#ifdef TARGET_NR_accept
6547 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00006548 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006549 break;
6550#endif
6551#ifdef TARGET_NR_bind
6552 case TARGET_NR_bind:
6553 ret = do_bind(arg1, arg2, arg3);
6554 break;
6555#endif
6556#ifdef TARGET_NR_connect
6557 case TARGET_NR_connect:
6558 ret = do_connect(arg1, arg2, arg3);
6559 break;
6560#endif
6561#ifdef TARGET_NR_getpeername
6562 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00006563 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006564 break;
6565#endif
6566#ifdef TARGET_NR_getsockname
6567 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00006568 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006569 break;
6570#endif
6571#ifdef TARGET_NR_getsockopt
6572 case TARGET_NR_getsockopt:
6573 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6574 break;
6575#endif
6576#ifdef TARGET_NR_listen
6577 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00006578 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006579 break;
6580#endif
6581#ifdef TARGET_NR_recv
6582 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00006583 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006584 break;
6585#endif
6586#ifdef TARGET_NR_recvfrom
6587 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00006588 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006589 break;
6590#endif
6591#ifdef TARGET_NR_recvmsg
6592 case TARGET_NR_recvmsg:
6593 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6594 break;
6595#endif
6596#ifdef TARGET_NR_send
6597 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00006598 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006599 break;
6600#endif
6601#ifdef TARGET_NR_sendmsg
6602 case TARGET_NR_sendmsg:
6603 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6604 break;
6605#endif
6606#ifdef TARGET_NR_sendto
6607 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00006608 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006609 break;
6610#endif
6611#ifdef TARGET_NR_shutdown
6612 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00006613 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006614 break;
6615#endif
6616#ifdef TARGET_NR_socket
6617 case TARGET_NR_socket:
6618 ret = do_socket(arg1, arg2, arg3);
6619 break;
6620#endif
6621#ifdef TARGET_NR_socketpair
6622 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00006623 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00006624 break;
6625#endif
6626#ifdef TARGET_NR_setsockopt
6627 case TARGET_NR_setsockopt:
6628 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6629 break;
6630#endif
ths7494b0f2007-02-11 18:26:53 +00006631
bellard31e31b82003-02-18 22:55:36 +00006632 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00006633 if (!(p = lock_user_string(arg2)))
6634 goto efault;
thse5574482007-02-11 20:03:13 +00006635 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6636 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00006637 break;
6638
bellard31e31b82003-02-18 22:55:36 +00006639 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00006640 {
bellard66fb9762003-03-23 01:06:05 +00006641 struct itimerval value, ovalue, *pvalue;
6642
pbrook53a59602006-03-25 19:31:22 +00006643 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006644 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00006645 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6646 || copy_from_user_timeval(&pvalue->it_value,
6647 arg2 + sizeof(struct target_timeval)))
6648 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006649 } else {
6650 pvalue = NULL;
6651 }
6652 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00006653 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00006654 if (copy_to_user_timeval(arg3,
6655 &ovalue.it_interval)
6656 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6657 &ovalue.it_value))
6658 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006659 }
6660 }
6661 break;
bellard31e31b82003-02-18 22:55:36 +00006662 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00006663 {
bellard66fb9762003-03-23 01:06:05 +00006664 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00006665
bellard66fb9762003-03-23 01:06:05 +00006666 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00006667 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00006668 if (copy_to_user_timeval(arg2,
6669 &value.it_interval)
6670 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6671 &value.it_value))
6672 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006673 }
6674 }
6675 break;
bellard31e31b82003-02-18 22:55:36 +00006676 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00006677 if (!(p = lock_user_string(arg1)))
6678 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006679 ret = get_errno(stat(path(p), &st));
6680 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006681 goto do_stat;
6682 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00006683 if (!(p = lock_user_string(arg1)))
6684 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006685 ret = get_errno(lstat(path(p), &st));
6686 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006687 goto do_stat;
6688 case TARGET_NR_fstat:
6689 {
6690 ret = get_errno(fstat(arg1, &st));
6691 do_stat:
6692 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006693 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00006694
bellard579a97f2007-11-11 14:26:47 +00006695 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6696 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02006697 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00006698 __put_user(st.st_dev, &target_st->st_dev);
6699 __put_user(st.st_ino, &target_st->st_ino);
6700 __put_user(st.st_mode, &target_st->st_mode);
6701 __put_user(st.st_uid, &target_st->st_uid);
6702 __put_user(st.st_gid, &target_st->st_gid);
6703 __put_user(st.st_nlink, &target_st->st_nlink);
6704 __put_user(st.st_rdev, &target_st->st_rdev);
6705 __put_user(st.st_size, &target_st->st_size);
6706 __put_user(st.st_blksize, &target_st->st_blksize);
6707 __put_user(st.st_blocks, &target_st->st_blocks);
6708 __put_user(st.st_atime, &target_st->target_st_atime);
6709 __put_user(st.st_mtime, &target_st->target_st_mtime);
6710 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00006711 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006712 }
6713 }
6714 break;
bellardebc05482003-09-30 21:08:41 +00006715#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00006716 case TARGET_NR_olduname:
6717 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006718#endif
6719#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00006720 case TARGET_NR_iopl:
6721 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006722#endif
bellard31e31b82003-02-18 22:55:36 +00006723 case TARGET_NR_vhangup:
6724 ret = get_errno(vhangup());
6725 break;
bellardebc05482003-09-30 21:08:41 +00006726#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00006727 case TARGET_NR_idle:
6728 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006729#endif
bellard42ad6ae2005-01-03 22:48:11 +00006730#ifdef TARGET_NR_syscall
6731 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01006732 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6733 arg6, arg7, arg8, 0);
6734 break;
bellard42ad6ae2005-01-03 22:48:11 +00006735#endif
bellard31e31b82003-02-18 22:55:36 +00006736 case TARGET_NR_wait4:
6737 {
6738 int status;
blueswir1992f48a2007-10-14 16:27:31 +00006739 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00006740 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00006741 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00006742 if (target_rusage)
6743 rusage_ptr = &rusage;
6744 else
6745 rusage_ptr = NULL;
6746 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6747 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01006748 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00006749 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00006750 if (put_user_s32(status, status_ptr))
6751 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006752 }
bellard2f619692007-11-16 10:46:05 +00006753 if (target_rusage)
6754 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00006755 }
6756 }
6757 break;
thse5febef2007-04-01 18:31:35 +00006758#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00006759 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00006760 if (!(p = lock_user_string(arg1)))
6761 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006762 ret = get_errno(swapoff(p));
6763 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006764 break;
thse5febef2007-04-01 18:31:35 +00006765#endif
bellard31e31b82003-02-18 22:55:36 +00006766 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00006767 {
pbrook53a59602006-03-25 19:31:22 +00006768 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00006769 struct sysinfo value;
6770 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00006771 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00006772 {
bellard579a97f2007-11-11 14:26:47 +00006773 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6774 goto efault;
bellarda5448a72004-06-19 16:59:03 +00006775 __put_user(value.uptime, &target_value->uptime);
6776 __put_user(value.loads[0], &target_value->loads[0]);
6777 __put_user(value.loads[1], &target_value->loads[1]);
6778 __put_user(value.loads[2], &target_value->loads[2]);
6779 __put_user(value.totalram, &target_value->totalram);
6780 __put_user(value.freeram, &target_value->freeram);
6781 __put_user(value.sharedram, &target_value->sharedram);
6782 __put_user(value.bufferram, &target_value->bufferram);
6783 __put_user(value.totalswap, &target_value->totalswap);
6784 __put_user(value.freeswap, &target_value->freeswap);
6785 __put_user(value.procs, &target_value->procs);
6786 __put_user(value.totalhigh, &target_value->totalhigh);
6787 __put_user(value.freehigh, &target_value->freehigh);
6788 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00006789 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00006790 }
6791 }
6792 break;
thse5febef2007-04-01 18:31:35 +00006793#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00006794 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00006795 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6796 break;
thse5febef2007-04-01 18:31:35 +00006797#endif
aurel32e5289082009-04-18 16:16:12 +00006798#ifdef TARGET_NR_semget
6799 case TARGET_NR_semget:
6800 ret = get_errno(semget(arg1, arg2, arg3));
6801 break;
6802#endif
6803#ifdef TARGET_NR_semop
6804 case TARGET_NR_semop:
6805 ret = get_errno(do_semop(arg1, arg2, arg3));
6806 break;
6807#endif
6808#ifdef TARGET_NR_semctl
6809 case TARGET_NR_semctl:
6810 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6811 break;
6812#endif
aurel32eeb438c2008-10-13 21:08:55 +00006813#ifdef TARGET_NR_msgctl
6814 case TARGET_NR_msgctl:
6815 ret = do_msgctl(arg1, arg2, arg3);
6816 break;
6817#endif
6818#ifdef TARGET_NR_msgget
6819 case TARGET_NR_msgget:
6820 ret = get_errno(msgget(arg1, arg2));
6821 break;
6822#endif
6823#ifdef TARGET_NR_msgrcv
6824 case TARGET_NR_msgrcv:
6825 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
6826 break;
6827#endif
6828#ifdef TARGET_NR_msgsnd
6829 case TARGET_NR_msgsnd:
6830 ret = do_msgsnd(arg1, arg2, arg3, arg4);
6831 break;
6832#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03006833#ifdef TARGET_NR_shmget
6834 case TARGET_NR_shmget:
6835 ret = get_errno(shmget(arg1, arg2, arg3));
6836 break;
6837#endif
6838#ifdef TARGET_NR_shmctl
6839 case TARGET_NR_shmctl:
6840 ret = do_shmctl(arg1, arg2, arg3);
6841 break;
6842#endif
6843#ifdef TARGET_NR_shmat
6844 case TARGET_NR_shmat:
6845 ret = do_shmat(arg1, arg2, arg3);
6846 break;
6847#endif
6848#ifdef TARGET_NR_shmdt
6849 case TARGET_NR_shmdt:
6850 ret = do_shmdt(arg1);
6851 break;
6852#endif
bellard31e31b82003-02-18 22:55:36 +00006853 case TARGET_NR_fsync:
6854 ret = get_errno(fsync(arg1));
6855 break;
bellard31e31b82003-02-18 22:55:36 +00006856 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07006857#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00006858 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00006859#elif defined(TARGET_CRIS)
6860 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006861#elif defined(TARGET_S390X)
6862 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00006863#else
pbrookd865bab2008-06-07 22:12:17 +00006864 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006865#endif
bellard1b6b0292003-03-22 17:31:38 +00006866 break;
bellardec86b0f2003-04-11 00:15:04 +00006867#ifdef __NR_exit_group
6868 /* new thread calls */
6869 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02006870#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00006871 _mcleanup();
6872#endif
bellarde9009672005-04-26 20:42:36 +00006873 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00006874 ret = get_errno(exit_group(arg1));
6875 break;
6876#endif
bellard31e31b82003-02-18 22:55:36 +00006877 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00006878 if (!(p = lock_user_string(arg1)))
6879 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006880 ret = get_errno(setdomainname(p, arg2));
6881 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006882 break;
6883 case TARGET_NR_uname:
6884 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00006885 {
6886 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00006887
bellard579a97f2007-11-11 14:26:47 +00006888 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
6889 goto efault;
bellard29e619b2004-09-13 21:41:04 +00006890 ret = get_errno(sys_uname(buf));
6891 if (!is_error(ret)) {
6892 /* Overrite the native machine name with whatever is being
6893 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01006894 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00006895 /* Allow the user to override the reported release. */
6896 if (qemu_uname_release && *qemu_uname_release)
6897 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00006898 }
pbrook53a59602006-03-25 19:31:22 +00006899 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00006900 }
bellard31e31b82003-02-18 22:55:36 +00006901 break;
bellard6dbad632003-03-16 18:05:05 +00006902#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00006903 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00006904 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00006905 break;
j_mayer84409dd2007-04-06 08:56:50 +00006906#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00006907 case TARGET_NR_vm86old:
6908 goto unimplemented;
6909 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00006910 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00006911 break;
6912#endif
j_mayer84409dd2007-04-06 08:56:50 +00006913#endif
bellard31e31b82003-02-18 22:55:36 +00006914 case TARGET_NR_adjtimex:
6915 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006916#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00006917 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00006918#endif
bellard31e31b82003-02-18 22:55:36 +00006919 case TARGET_NR_init_module:
6920 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00006921#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00006922 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00006923#endif
bellard31e31b82003-02-18 22:55:36 +00006924 goto unimplemented;
6925 case TARGET_NR_quotactl:
6926 goto unimplemented;
6927 case TARGET_NR_getpgid:
6928 ret = get_errno(getpgid(arg1));
6929 break;
6930 case TARGET_NR_fchdir:
6931 ret = get_errno(fchdir(arg1));
6932 break;
j_mayer84409dd2007-04-06 08:56:50 +00006933#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00006934 case TARGET_NR_bdflush:
6935 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00006936#endif
thse5febef2007-04-01 18:31:35 +00006937#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00006938 case TARGET_NR_sysfs:
6939 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006940#endif
bellard31e31b82003-02-18 22:55:36 +00006941 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00006942 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00006943 break;
thse5febef2007-04-01 18:31:35 +00006944#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00006945 case TARGET_NR_afs_syscall:
6946 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006947#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006948#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006949 case TARGET_NR__llseek:
6950 {
6951 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00006952#if !defined(__NR_llseek)
6953 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
6954 if (res == -1) {
6955 ret = get_errno(res);
6956 } else {
6957 ret = 0;
6958 }
6959#else
bellard31e31b82003-02-18 22:55:36 +00006960 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00006961#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00006962 if ((ret == 0) && put_user_s64(res, arg4)) {
6963 goto efault;
6964 }
bellard31e31b82003-02-18 22:55:36 +00006965 }
6966 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006967#endif
bellard31e31b82003-02-18 22:55:36 +00006968 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02006969#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00006970 {
pbrook53a59602006-03-25 19:31:22 +00006971 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00006972 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006973 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00006974
6975 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00006976 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00006977 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00006978 goto fail;
6979 }
ths3b46e622007-09-17 08:09:54 +00006980
bellard4add45b2003-06-05 01:52:59 +00006981 ret = get_errno(sys_getdents(arg1, dirp, count));
6982 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006983 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00006984 struct target_dirent *tde;
6985 int len = ret;
6986 int reclen, treclen;
6987 int count1, tnamelen;
6988
6989 count1 = 0;
6990 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00006991 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6992 goto efault;
bellard4add45b2003-06-05 01:52:59 +00006993 tde = target_dirp;
6994 while (len > 0) {
6995 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00006996 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00006997 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006998 tde->d_ino = tswapal(de->d_ino);
6999 tde->d_off = tswapal(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00007000 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00007001 if (tnamelen > 256)
7002 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00007003 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00007004 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00007005 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00007006 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00007007 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00007008 count1 += treclen;
7009 }
7010 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00007011 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00007012 }
7013 free(dirp);
7014 }
7015#else
bellard31e31b82003-02-18 22:55:36 +00007016 {
aurel326556a832008-10-13 21:08:17 +00007017 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007018 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00007019
bellard579a97f2007-11-11 14:26:47 +00007020 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7021 goto efault;
bellard72f03902003-02-18 23:33:18 +00007022 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00007023 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007024 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00007025 int len = ret;
7026 int reclen;
7027 de = dirp;
7028 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007029 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00007030 if (reclen > len)
7031 break;
bellard8083a3e2003-03-24 23:12:16 +00007032 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00007033 tswapls(&de->d_ino);
7034 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007035 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00007036 len -= reclen;
7037 }
7038 }
pbrook53a59602006-03-25 19:31:22 +00007039 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00007040 }
bellard4add45b2003-06-05 01:52:59 +00007041#endif
bellard31e31b82003-02-18 22:55:36 +00007042 break;
ths3ae43202007-09-16 21:39:48 +00007043#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00007044 case TARGET_NR_getdents64:
7045 {
aurel326556a832008-10-13 21:08:17 +00007046 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007047 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00007048 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7049 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00007050 ret = get_errno(sys_getdents64(arg1, dirp, count));
7051 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007052 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00007053 int len = ret;
7054 int reclen;
7055 de = dirp;
7056 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007057 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00007058 if (reclen > len)
7059 break;
bellard8083a3e2003-03-24 23:12:16 +00007060 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00007061 tswap64s((uint64_t *)&de->d_ino);
7062 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007063 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00007064 len -= reclen;
7065 }
7066 }
pbrook53a59602006-03-25 19:31:22 +00007067 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00007068 }
7069 break;
bellarda541f292004-04-12 20:39:29 +00007070#endif /* TARGET_NR_getdents64 */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007071#if defined(TARGET_NR__newselect) || defined(TARGET_S390X)
7072#ifdef TARGET_S390X
7073 case TARGET_NR_select:
7074#else
bellard31e31b82003-02-18 22:55:36 +00007075 case TARGET_NR__newselect:
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007076#endif
pbrook53a59602006-03-25 19:31:22 +00007077 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00007078 break;
thse5febef2007-04-01 18:31:35 +00007079#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05007080#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7081# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00007082 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05007083# endif
7084# ifdef TARGET_NR_ppoll
7085 case TARGET_NR_ppoll:
7086# endif
bellard9de5e442003-03-23 16:49:39 +00007087 {
pbrook53a59602006-03-25 19:31:22 +00007088 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00007089 unsigned int nfds = arg2;
7090 int timeout = arg3;
7091 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00007092 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00007093
bellard579a97f2007-11-11 14:26:47 +00007094 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7095 if (!target_pfd)
7096 goto efault;
Mike Frysingerd8035d42011-02-07 01:05:51 -05007097
bellard9de5e442003-03-23 16:49:39 +00007098 pfd = alloca(sizeof(struct pollfd) * nfds);
7099 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007100 pfd[i].fd = tswap32(target_pfd[i].fd);
7101 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00007102 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05007103
7104# ifdef TARGET_NR_ppoll
7105 if (num == TARGET_NR_ppoll) {
7106 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7107 target_sigset_t *target_set;
7108 sigset_t _set, *set = &_set;
7109
7110 if (arg3) {
7111 if (target_to_host_timespec(timeout_ts, arg3)) {
7112 unlock_user(target_pfd, arg1, 0);
7113 goto efault;
7114 }
7115 } else {
7116 timeout_ts = NULL;
7117 }
7118
7119 if (arg4) {
7120 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7121 if (!target_set) {
7122 unlock_user(target_pfd, arg1, 0);
7123 goto efault;
7124 }
7125 target_to_host_sigset(set, target_set);
7126 } else {
7127 set = NULL;
7128 }
7129
7130 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7131
7132 if (!is_error(ret) && arg3) {
7133 host_to_target_timespec(arg3, timeout_ts);
7134 }
7135 if (arg4) {
7136 unlock_user(target_set, arg4, 0);
7137 }
7138 } else
7139# endif
7140 ret = get_errno(poll(pfd, nfds, timeout));
7141
bellard9de5e442003-03-23 16:49:39 +00007142 if (!is_error(ret)) {
7143 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007144 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00007145 }
7146 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00007147 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00007148 }
7149 break;
thse5febef2007-04-01 18:31:35 +00007150#endif
bellard31e31b82003-02-18 22:55:36 +00007151 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00007152 /* NOTE: the flock constant seems to be the same for every
7153 Linux platform */
7154 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00007155 break;
7156 case TARGET_NR_readv:
7157 {
7158 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00007159 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00007160
7161 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00007162 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
7163 goto efault;
bellard31e31b82003-02-18 22:55:36 +00007164 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00007165 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00007166 }
7167 break;
7168 case TARGET_NR_writev:
7169 {
7170 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00007171 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00007172
7173 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00007174 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7175 goto efault;
bellard31e31b82003-02-18 22:55:36 +00007176 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00007177 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00007178 }
7179 break;
7180 case TARGET_NR_getsid:
7181 ret = get_errno(getsid(arg1));
7182 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007183#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00007184 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00007185 ret = get_errno(fdatasync(arg1));
7186 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007187#endif
bellard31e31b82003-02-18 22:55:36 +00007188 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00007189 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00007190 return value. */
ths0da46a62007-10-20 20:23:07 +00007191 ret = -TARGET_ENOTDIR;
7192 break;
Mike Frysinger737de1d2011-02-07 01:05:55 -05007193 case TARGET_NR_sched_getaffinity:
7194 {
7195 unsigned int mask_size;
7196 unsigned long *mask;
7197
7198 /*
7199 * sched_getaffinity needs multiples of ulong, so need to take
7200 * care of mismatches between target ulong and host ulong sizes.
7201 */
7202 if (arg2 & (sizeof(abi_ulong) - 1)) {
7203 ret = -TARGET_EINVAL;
7204 break;
7205 }
7206 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7207
7208 mask = alloca(mask_size);
7209 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7210
7211 if (!is_error(ret)) {
Mike McCormackcd18f052011-04-18 14:43:36 +09007212 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05007213 goto efault;
7214 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05007215 }
7216 }
7217 break;
7218 case TARGET_NR_sched_setaffinity:
7219 {
7220 unsigned int mask_size;
7221 unsigned long *mask;
7222
7223 /*
7224 * sched_setaffinity needs multiples of ulong, so need to take
7225 * care of mismatches between target ulong and host ulong sizes.
7226 */
7227 if (arg2 & (sizeof(abi_ulong) - 1)) {
7228 ret = -TARGET_EINVAL;
7229 break;
7230 }
7231 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7232
7233 mask = alloca(mask_size);
7234 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7235 goto efault;
7236 }
7237 memcpy(mask, p, arg2);
7238 unlock_user_struct(p, arg2, 0);
7239
7240 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7241 }
7242 break;
bellard31e31b82003-02-18 22:55:36 +00007243 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00007244 {
pbrook53a59602006-03-25 19:31:22 +00007245 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007246 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00007247
bellard579a97f2007-11-11 14:26:47 +00007248 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7249 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007250 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007251 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00007252 ret = get_errno(sched_setparam(arg1, &schp));
7253 }
7254 break;
bellard31e31b82003-02-18 22:55:36 +00007255 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00007256 {
pbrook53a59602006-03-25 19:31:22 +00007257 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007258 struct sched_param schp;
7259 ret = get_errno(sched_getparam(arg1, &schp));
7260 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007261 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7262 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007263 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007264 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00007265 }
7266 }
7267 break;
bellard31e31b82003-02-18 22:55:36 +00007268 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00007269 {
pbrook53a59602006-03-25 19:31:22 +00007270 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007271 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00007272 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7273 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007274 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007275 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00007276 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7277 }
7278 break;
bellard31e31b82003-02-18 22:55:36 +00007279 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00007280 ret = get_errno(sched_getscheduler(arg1));
7281 break;
bellard31e31b82003-02-18 22:55:36 +00007282 case TARGET_NR_sched_yield:
7283 ret = get_errno(sched_yield());
7284 break;
7285 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00007286 ret = get_errno(sched_get_priority_max(arg1));
7287 break;
bellard31e31b82003-02-18 22:55:36 +00007288 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00007289 ret = get_errno(sched_get_priority_min(arg1));
7290 break;
bellard31e31b82003-02-18 22:55:36 +00007291 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00007292 {
bellard5cd43932003-03-29 16:54:36 +00007293 struct timespec ts;
7294 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7295 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007296 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00007297 }
7298 }
7299 break;
bellard31e31b82003-02-18 22:55:36 +00007300 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00007301 {
bellard1b6b0292003-03-22 17:31:38 +00007302 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00007303 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00007304 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00007305 if (is_error(ret) && arg2) {
7306 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00007307 }
7308 }
7309 break;
thse5febef2007-04-01 18:31:35 +00007310#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00007311 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00007312 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007313#endif
7314#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00007315 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00007316 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007317#endif
bellard31e31b82003-02-18 22:55:36 +00007318 case TARGET_NR_prctl:
Peter Maydell1e6722f2012-02-03 14:48:03 +00007319 switch (arg1) {
7320 case PR_GET_PDEATHSIG:
7321 {
7322 int deathsig;
7323 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
7324 if (!is_error(ret) && arg2
7325 && put_user_ual(deathsig, arg2)) {
7326 goto efault;
thse5574482007-02-11 20:03:13 +00007327 }
Peter Maydell1e6722f2012-02-03 14:48:03 +00007328 break;
7329 }
Peter Maydelldb9526b2012-02-03 14:48:03 +00007330#ifdef PR_GET_NAME
7331 case PR_GET_NAME:
7332 {
7333 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
7334 if (!name) {
7335 goto efault;
7336 }
7337 ret = get_errno(prctl(arg1, (unsigned long)name,
7338 arg3, arg4, arg5));
7339 unlock_user(name, arg2, 16);
7340 break;
7341 }
7342 case PR_SET_NAME:
7343 {
7344 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
7345 if (!name) {
7346 goto efault;
7347 }
7348 ret = get_errno(prctl(arg1, (unsigned long)name,
7349 arg3, arg4, arg5));
7350 unlock_user(name, arg2, 0);
7351 break;
7352 }
7353#endif
Peter Maydell1e6722f2012-02-03 14:48:03 +00007354 default:
7355 /* Most prctl options have no pointer arguments */
7356 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7357 break;
7358 }
ths39b9aae2007-02-11 18:36:44 +00007359 break;
bellardd2fd1af2007-11-14 18:08:56 +00007360#ifdef TARGET_NR_arch_prctl
7361 case TARGET_NR_arch_prctl:
7362#if defined(TARGET_I386) && !defined(TARGET_ABI32)
7363 ret = do_arch_prctl(cpu_env, arg1, arg2);
7364 break;
7365#else
7366 goto unimplemented;
7367#endif
7368#endif
bellard67867302003-11-23 17:05:30 +00007369#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00007370 case TARGET_NR_pread:
Riku Voipio48e515d2011-07-12 15:40:51 +03007371 if (regpairs_aligned(cpu_env))
balroga4ae00b2008-09-20 03:14:14 +00007372 arg4 = arg5;
bellard579a97f2007-11-11 14:26:47 +00007373 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7374 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007375 ret = get_errno(pread(arg1, p, arg3, arg4));
7376 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00007377 break;
bellard31e31b82003-02-18 22:55:36 +00007378 case TARGET_NR_pwrite:
Riku Voipio48e515d2011-07-12 15:40:51 +03007379 if (regpairs_aligned(cpu_env))
balroga4ae00b2008-09-20 03:14:14 +00007380 arg4 = arg5;
bellard579a97f2007-11-11 14:26:47 +00007381 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7382 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007383 ret = get_errno(pwrite(arg1, p, arg3, arg4));
7384 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00007385 break;
bellard67867302003-11-23 17:05:30 +00007386#endif
aurel32f2c7ba12008-03-28 22:32:06 +00007387#ifdef TARGET_NR_pread64
7388 case TARGET_NR_pread64:
7389 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7390 goto efault;
7391 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7392 unlock_user(p, arg2, ret);
7393 break;
7394 case TARGET_NR_pwrite64:
7395 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7396 goto efault;
7397 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7398 unlock_user(p, arg2, 0);
7399 break;
7400#endif
bellard31e31b82003-02-18 22:55:36 +00007401 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00007402 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7403 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007404 ret = get_errno(sys_getcwd1(p, arg2));
7405 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00007406 break;
7407 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00007408 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007409 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00007410 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007411 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00007412#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02007413 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
Jia Liud9627832012-07-20 15:50:52 +08007414 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
Andreas Färber9349b4f2012-03-14 01:38:32 +01007415 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00007416 break;
7417#else
bellard5cd43932003-03-29 16:54:36 +00007418 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00007419#endif
bellard31e31b82003-02-18 22:55:36 +00007420 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00007421 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007422#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00007423 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00007424 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007425#endif
7426#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00007427 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00007428 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007429#endif
bellard048f6b42005-11-26 18:47:20 +00007430#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00007431 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00007432 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
7433 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00007434 break;
bellard048f6b42005-11-26 18:47:20 +00007435#endif
bellardebc05482003-09-30 21:08:41 +00007436#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00007437 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00007438 {
7439 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007440 int resource = target_to_host_resource(arg1);
7441 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00007442 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007443 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00007444 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7445 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007446 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7447 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007448 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00007449 }
7450 break;
7451 }
bellardebc05482003-09-30 21:08:41 +00007452#endif
bellarda315a142005-01-30 22:59:18 +00007453#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00007454 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00007455 if (!(p = lock_user_string(arg1)))
7456 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007457 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
7458 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00007459 break;
bellarda315a142005-01-30 22:59:18 +00007460#endif
7461#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00007462 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00007463 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00007464 break;
bellarda315a142005-01-30 22:59:18 +00007465#endif
7466#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00007467 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00007468 if (!(p = lock_user_string(arg1)))
7469 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007470 ret = get_errno(stat(path(p), &st));
7471 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007472 if (!is_error(ret))
7473 ret = host_to_target_stat64(cpu_env, arg2, &st);
7474 break;
bellarda315a142005-01-30 22:59:18 +00007475#endif
7476#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00007477 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00007478 if (!(p = lock_user_string(arg1)))
7479 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007480 ret = get_errno(lstat(path(p), &st));
7481 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007482 if (!is_error(ret))
7483 ret = host_to_target_stat64(cpu_env, arg2, &st);
7484 break;
bellarda315a142005-01-30 22:59:18 +00007485#endif
7486#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00007487 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00007488 ret = get_errno(fstat(arg1, &st));
7489 if (!is_error(ret))
7490 ret = host_to_target_stat64(cpu_env, arg2, &st);
7491 break;
bellardec86b0f2003-04-11 00:15:04 +00007492#endif
aurel329d33b762009-04-08 23:07:05 +00007493#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
7494 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
7495#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007496 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00007497#endif
7498#ifdef TARGET_NR_newfstatat
7499 case TARGET_NR_newfstatat:
7500#endif
balrog6a24a772008-09-20 02:23:36 +00007501 if (!(p = lock_user_string(arg2)))
7502 goto efault;
aurel329d33b762009-04-08 23:07:05 +00007503#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007504 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00007505#else
7506 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
7507#endif
balrog6a24a772008-09-20 02:23:36 +00007508 if (!is_error(ret))
7509 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00007510 break;
bellarda315a142005-01-30 22:59:18 +00007511#endif
bellard67867302003-11-23 17:05:30 +00007512 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00007513 if (!(p = lock_user_string(arg1)))
7514 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007515 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7516 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007517 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007518#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00007519 case TARGET_NR_getuid:
7520 ret = get_errno(high2lowuid(getuid()));
7521 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007522#endif
7523#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00007524 case TARGET_NR_getgid:
7525 ret = get_errno(high2lowgid(getgid()));
7526 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007527#endif
7528#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00007529 case TARGET_NR_geteuid:
7530 ret = get_errno(high2lowuid(geteuid()));
7531 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007532#endif
7533#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00007534 case TARGET_NR_getegid:
7535 ret = get_errno(high2lowgid(getegid()));
7536 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007537#endif
bellard67867302003-11-23 17:05:30 +00007538 case TARGET_NR_setreuid:
7539 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7540 break;
7541 case TARGET_NR_setregid:
7542 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7543 break;
7544 case TARGET_NR_getgroups:
7545 {
7546 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007547 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007548 gid_t *grouplist;
7549 int i;
7550
7551 grouplist = alloca(gidsetsize * sizeof(gid_t));
7552 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007553 if (gidsetsize == 0)
7554 break;
bellard67867302003-11-23 17:05:30 +00007555 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007556 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
7557 if (!target_grouplist)
7558 goto efault;
balroga2155fc2008-09-20 02:12:08 +00007559 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007560 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00007561 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00007562 }
7563 }
7564 break;
7565 case TARGET_NR_setgroups:
7566 {
7567 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007568 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007569 gid_t *grouplist;
7570 int i;
7571
7572 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007573 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
7574 if (!target_grouplist) {
7575 ret = -TARGET_EFAULT;
7576 goto fail;
7577 }
bellard67867302003-11-23 17:05:30 +00007578 for(i = 0;i < gidsetsize; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007579 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00007580 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00007581 ret = get_errno(setgroups(gidsetsize, grouplist));
7582 }
7583 break;
7584 case TARGET_NR_fchown:
7585 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7586 break;
thsccfa72b2007-09-24 09:23:34 +00007587#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
7588 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00007589 if (!(p = lock_user_string(arg2)))
7590 goto efault;
7591 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
7592 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00007593 break;
7594#endif
bellard67867302003-11-23 17:05:30 +00007595#ifdef TARGET_NR_setresuid
7596 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00007597 ret = get_errno(setresuid(low2highuid(arg1),
7598 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00007599 low2highuid(arg3)));
7600 break;
7601#endif
7602#ifdef TARGET_NR_getresuid
7603 case TARGET_NR_getresuid:
7604 {
pbrook53a59602006-03-25 19:31:22 +00007605 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00007606 ret = get_errno(getresuid(&ruid, &euid, &suid));
7607 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007608 if (put_user_u16(high2lowuid(ruid), arg1)
7609 || put_user_u16(high2lowuid(euid), arg2)
7610 || put_user_u16(high2lowuid(suid), arg3))
7611 goto efault;
bellard67867302003-11-23 17:05:30 +00007612 }
7613 }
7614 break;
7615#endif
7616#ifdef TARGET_NR_getresgid
7617 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00007618 ret = get_errno(setresgid(low2highgid(arg1),
7619 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00007620 low2highgid(arg3)));
7621 break;
7622#endif
7623#ifdef TARGET_NR_getresgid
7624 case TARGET_NR_getresgid:
7625 {
pbrook53a59602006-03-25 19:31:22 +00007626 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00007627 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7628 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007629 if (put_user_u16(high2lowgid(rgid), arg1)
7630 || put_user_u16(high2lowgid(egid), arg2)
7631 || put_user_u16(high2lowgid(sgid), arg3))
7632 goto efault;
bellard67867302003-11-23 17:05:30 +00007633 }
7634 }
7635 break;
7636#endif
7637 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00007638 if (!(p = lock_user_string(arg1)))
7639 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007640 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7641 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007642 break;
7643 case TARGET_NR_setuid:
7644 ret = get_errno(setuid(low2highuid(arg1)));
7645 break;
7646 case TARGET_NR_setgid:
7647 ret = get_errno(setgid(low2highgid(arg1)));
7648 break;
7649 case TARGET_NR_setfsuid:
7650 ret = get_errno(setfsuid(arg1));
7651 break;
7652 case TARGET_NR_setfsgid:
7653 ret = get_errno(setfsgid(arg1));
7654 break;
bellard67867302003-11-23 17:05:30 +00007655
bellarda315a142005-01-30 22:59:18 +00007656#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00007657 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00007658 if (!(p = lock_user_string(arg1)))
7659 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007660 ret = get_errno(lchown(p, arg2, arg3));
7661 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007662 break;
bellarda315a142005-01-30 22:59:18 +00007663#endif
7664#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00007665 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00007666 ret = get_errno(getuid());
7667 break;
bellarda315a142005-01-30 22:59:18 +00007668#endif
aurel3264b4d282008-11-14 17:20:15 +00007669
7670#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7671 /* Alpha specific */
7672 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007673 {
7674 uid_t euid;
7675 euid=geteuid();
7676 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7677 }
aurel3264b4d282008-11-14 17:20:15 +00007678 ret = get_errno(getuid());
7679 break;
7680#endif
7681#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7682 /* Alpha specific */
7683 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007684 {
7685 uid_t egid;
7686 egid=getegid();
7687 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7688 }
aurel3264b4d282008-11-14 17:20:15 +00007689 ret = get_errno(getgid());
7690 break;
7691#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08007692#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7693 /* Alpha specific */
7694 case TARGET_NR_osf_getsysinfo:
7695 ret = -TARGET_EOPNOTSUPP;
7696 switch (arg1) {
7697 case TARGET_GSI_IEEE_FP_CONTROL:
7698 {
7699 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7700
7701 /* Copied from linux ieee_fpcr_to_swcr. */
7702 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7703 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7704 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7705 | SWCR_TRAP_ENABLE_DZE
7706 | SWCR_TRAP_ENABLE_OVF);
7707 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
7708 | SWCR_TRAP_ENABLE_INE);
7709 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
7710 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
7711
7712 if (put_user_u64 (swcr, arg2))
7713 goto efault;
7714 ret = 0;
7715 }
7716 break;
7717
7718 /* case GSI_IEEE_STATE_AT_SIGNAL:
7719 -- Not implemented in linux kernel.
7720 case GSI_UACPROC:
7721 -- Retrieves current unaligned access state; not much used.
7722 case GSI_PROC_TYPE:
7723 -- Retrieves implver information; surely not used.
7724 case GSI_GET_HWRPB:
7725 -- Grabs a copy of the HWRPB; surely not used.
7726 */
7727 }
7728 break;
7729#endif
7730#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
7731 /* Alpha specific */
7732 case TARGET_NR_osf_setsysinfo:
7733 ret = -TARGET_EOPNOTSUPP;
7734 switch (arg1) {
7735 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007736 {
7737 uint64_t swcr, fpcr, orig_fpcr;
7738
Richard Henderson6e06d512012-06-01 09:08:21 -07007739 if (get_user_u64 (swcr, arg2)) {
Richard Hendersonba0e2762009-12-09 15:56:29 -08007740 goto efault;
Richard Henderson6e06d512012-06-01 09:08:21 -07007741 }
7742 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007743 fpcr = orig_fpcr & FPCR_DYN_MASK;
7744
7745 /* Copied from linux ieee_swcr_to_fpcr. */
7746 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
7747 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
7748 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
7749 | SWCR_TRAP_ENABLE_DZE
7750 | SWCR_TRAP_ENABLE_OVF)) << 48;
7751 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
7752 | SWCR_TRAP_ENABLE_INE)) << 57;
7753 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
7754 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
7755
Richard Henderson6e06d512012-06-01 09:08:21 -07007756 cpu_alpha_store_fpcr(cpu_env, fpcr);
7757 ret = 0;
7758 }
7759 break;
7760
7761 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
7762 {
7763 uint64_t exc, fpcr, orig_fpcr;
7764 int si_code;
7765
7766 if (get_user_u64(exc, arg2)) {
7767 goto efault;
7768 }
7769
7770 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
7771
7772 /* We only add to the exception status here. */
7773 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
7774
7775 cpu_alpha_store_fpcr(cpu_env, fpcr);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007776 ret = 0;
7777
Richard Henderson6e06d512012-06-01 09:08:21 -07007778 /* Old exceptions are not signaled. */
7779 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007780
Richard Henderson6e06d512012-06-01 09:08:21 -07007781 /* If any exceptions set by this call,
7782 and are unmasked, send a signal. */
7783 si_code = 0;
7784 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
7785 si_code = TARGET_FPE_FLTRES;
7786 }
7787 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
7788 si_code = TARGET_FPE_FLTUND;
7789 }
7790 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
7791 si_code = TARGET_FPE_FLTOVF;
7792 }
7793 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
7794 si_code = TARGET_FPE_FLTDIV;
7795 }
7796 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
7797 si_code = TARGET_FPE_FLTINV;
7798 }
7799 if (si_code != 0) {
7800 target_siginfo_t info;
7801 info.si_signo = SIGFPE;
7802 info.si_errno = 0;
7803 info.si_code = si_code;
7804 info._sifields._sigfault._addr
7805 = ((CPUArchState *)cpu_env)->pc;
7806 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007807 }
7808 }
7809 break;
7810
7811 /* case SSI_NVPAIRS:
7812 -- Used with SSIN_UACPROC to enable unaligned accesses.
7813 case SSI_IEEE_STATE_AT_SIGNAL:
7814 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
7815 -- Not implemented in linux kernel
7816 */
7817 }
7818 break;
7819#endif
7820#ifdef TARGET_NR_osf_sigprocmask
7821 /* Alpha specific. */
7822 case TARGET_NR_osf_sigprocmask:
7823 {
7824 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01007825 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08007826 sigset_t set, oldset;
7827
7828 switch(arg1) {
7829 case TARGET_SIG_BLOCK:
7830 how = SIG_BLOCK;
7831 break;
7832 case TARGET_SIG_UNBLOCK:
7833 how = SIG_UNBLOCK;
7834 break;
7835 case TARGET_SIG_SETMASK:
7836 how = SIG_SETMASK;
7837 break;
7838 default:
7839 ret = -TARGET_EINVAL;
7840 goto fail;
7841 }
7842 mask = arg2;
7843 target_to_host_old_sigset(&set, &mask);
Juan Quintelabc088ba2011-06-16 17:37:10 +01007844 sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007845 host_to_target_old_sigset(&mask, &oldset);
7846 ret = mask;
7847 }
7848 break;
7849#endif
aurel3264b4d282008-11-14 17:20:15 +00007850
bellarda315a142005-01-30 22:59:18 +00007851#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00007852 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00007853 ret = get_errno(getgid());
7854 break;
bellarda315a142005-01-30 22:59:18 +00007855#endif
7856#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00007857 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00007858 ret = get_errno(geteuid());
7859 break;
bellarda315a142005-01-30 22:59:18 +00007860#endif
7861#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00007862 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00007863 ret = get_errno(getegid());
7864 break;
bellarda315a142005-01-30 22:59:18 +00007865#endif
7866#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00007867 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00007868 ret = get_errno(setreuid(arg1, arg2));
7869 break;
bellarda315a142005-01-30 22:59:18 +00007870#endif
7871#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00007872 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00007873 ret = get_errno(setregid(arg1, arg2));
7874 break;
bellarda315a142005-01-30 22:59:18 +00007875#endif
7876#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00007877 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00007878 {
7879 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007880 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007881 gid_t *grouplist;
7882 int i;
7883
7884 grouplist = alloca(gidsetsize * sizeof(gid_t));
7885 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007886 if (gidsetsize == 0)
7887 break;
bellard99c475a2005-01-31 20:45:13 +00007888 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007889 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
7890 if (!target_grouplist) {
7891 ret = -TARGET_EFAULT;
7892 goto fail;
7893 }
balroga2155fc2008-09-20 02:12:08 +00007894 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00007895 target_grouplist[i] = tswap32(grouplist[i]);
7896 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00007897 }
7898 }
7899 break;
bellarda315a142005-01-30 22:59:18 +00007900#endif
7901#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00007902 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00007903 {
7904 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007905 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007906 gid_t *grouplist;
7907 int i;
ths3b46e622007-09-17 08:09:54 +00007908
bellard99c475a2005-01-31 20:45:13 +00007909 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007910 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
7911 if (!target_grouplist) {
7912 ret = -TARGET_EFAULT;
7913 goto fail;
7914 }
bellard99c475a2005-01-31 20:45:13 +00007915 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00007916 grouplist[i] = tswap32(target_grouplist[i]);
7917 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00007918 ret = get_errno(setgroups(gidsetsize, grouplist));
7919 }
7920 break;
bellarda315a142005-01-30 22:59:18 +00007921#endif
7922#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00007923 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00007924 ret = get_errno(fchown(arg1, arg2, arg3));
7925 break;
bellarda315a142005-01-30 22:59:18 +00007926#endif
7927#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00007928 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00007929 ret = get_errno(setresuid(arg1, arg2, arg3));
7930 break;
bellarda315a142005-01-30 22:59:18 +00007931#endif
7932#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00007933 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00007934 {
pbrook53a59602006-03-25 19:31:22 +00007935 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00007936 ret = get_errno(getresuid(&ruid, &euid, &suid));
7937 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007938 if (put_user_u32(ruid, arg1)
7939 || put_user_u32(euid, arg2)
7940 || put_user_u32(suid, arg3))
7941 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00007942 }
7943 }
7944 break;
bellarda315a142005-01-30 22:59:18 +00007945#endif
7946#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00007947 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00007948 ret = get_errno(setresgid(arg1, arg2, arg3));
7949 break;
bellarda315a142005-01-30 22:59:18 +00007950#endif
7951#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00007952 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00007953 {
pbrook53a59602006-03-25 19:31:22 +00007954 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00007955 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7956 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007957 if (put_user_u32(rgid, arg1)
7958 || put_user_u32(egid, arg2)
7959 || put_user_u32(sgid, arg3))
7960 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00007961 }
7962 }
7963 break;
bellarda315a142005-01-30 22:59:18 +00007964#endif
7965#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00007966 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00007967 if (!(p = lock_user_string(arg1)))
7968 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007969 ret = get_errno(chown(p, arg2, arg3));
7970 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007971 break;
bellarda315a142005-01-30 22:59:18 +00007972#endif
7973#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00007974 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00007975 ret = get_errno(setuid(arg1));
7976 break;
bellarda315a142005-01-30 22:59:18 +00007977#endif
7978#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00007979 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00007980 ret = get_errno(setgid(arg1));
7981 break;
bellarda315a142005-01-30 22:59:18 +00007982#endif
7983#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00007984 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00007985 ret = get_errno(setfsuid(arg1));
7986 break;
bellarda315a142005-01-30 22:59:18 +00007987#endif
7988#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00007989 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00007990 ret = get_errno(setfsgid(arg1));
7991 break;
bellarda315a142005-01-30 22:59:18 +00007992#endif
bellard67867302003-11-23 17:05:30 +00007993
bellard31e31b82003-02-18 22:55:36 +00007994 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00007995 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00007996#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00007997 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00007998 {
7999 void *a;
8000 ret = -TARGET_EFAULT;
8001 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8002 goto efault;
8003 if (!(p = lock_user_string(arg3)))
8004 goto mincore_fail;
8005 ret = get_errno(mincore(a, arg2, p));
8006 unlock_user(p, arg3, ret);
8007 mincore_fail:
8008 unlock_user(a, arg1, 0);
8009 }
8010 break;
bellardffa65c32004-01-04 23:57:22 +00008011#endif
aurel32408321b2008-10-01 21:46:32 +00008012#ifdef TARGET_NR_arm_fadvise64_64
8013 case TARGET_NR_arm_fadvise64_64:
8014 {
8015 /*
8016 * arm_fadvise64_64 looks like fadvise64_64 but
8017 * with different argument order
8018 */
8019 abi_long temp;
8020 temp = arg3;
8021 arg3 = arg4;
8022 arg4 = temp;
8023 }
8024#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008025#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00008026#ifdef TARGET_NR_fadvise64_64
8027 case TARGET_NR_fadvise64_64:
8028#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008029#ifdef TARGET_NR_fadvise64
8030 case TARGET_NR_fadvise64:
8031#endif
8032#ifdef TARGET_S390X
8033 switch (arg4) {
8034 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8035 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8036 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8037 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8038 default: break;
8039 }
8040#endif
8041 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00008042 break;
8043#endif
bellardffa65c32004-01-04 23:57:22 +00008044#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00008045 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00008046 /* A straight passthrough may not be safe because qemu sometimes
8047 turns private flie-backed mappings into anonymous mappings.
8048 This will break MADV_DONTNEED.
8049 This is a hint, so ignoring and returning success is ok. */
8050 ret = get_errno(0);
8051 break;
bellardffa65c32004-01-04 23:57:22 +00008052#endif
blueswir1992f48a2007-10-14 16:27:31 +00008053#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00008054 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00008055 {
thsb1e341e2007-03-20 21:50:52 +00008056 int cmd;
bellard77e46722003-04-29 20:39:06 +00008057 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00008058 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00008059#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00008060 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00008061#endif
bellard77e46722003-04-29 20:39:06 +00008062
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008063 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00008064 if (cmd == -TARGET_EINVAL) {
8065 ret = cmd;
8066 break;
8067 }
thsb1e341e2007-03-20 21:50:52 +00008068
bellard60cd49d2003-03-16 22:53:56 +00008069 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00008070 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00008071#ifdef TARGET_ARM
8072 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008073 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8074 goto efault;
ths58134272007-03-31 18:59:32 +00008075 fl.l_type = tswap16(target_efl->l_type);
8076 fl.l_whence = tswap16(target_efl->l_whence);
8077 fl.l_start = tswap64(target_efl->l_start);
8078 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008079 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00008080 unlock_user_struct(target_efl, arg3, 0);
8081 } else
8082#endif
8083 {
bellard9ee1fa22007-11-11 15:11:19 +00008084 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8085 goto efault;
ths58134272007-03-31 18:59:32 +00008086 fl.l_type = tswap16(target_fl->l_type);
8087 fl.l_whence = tswap16(target_fl->l_whence);
8088 fl.l_start = tswap64(target_fl->l_start);
8089 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008090 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00008091 unlock_user_struct(target_fl, arg3, 0);
8092 }
thsb1e341e2007-03-20 21:50:52 +00008093 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008094 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00008095#ifdef TARGET_ARM
8096 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008097 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8098 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008099 target_efl->l_type = tswap16(fl.l_type);
8100 target_efl->l_whence = tswap16(fl.l_whence);
8101 target_efl->l_start = tswap64(fl.l_start);
8102 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008103 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008104 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008105 } else
8106#endif
8107 {
bellard9ee1fa22007-11-11 15:11:19 +00008108 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8109 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008110 target_fl->l_type = tswap16(fl.l_type);
8111 target_fl->l_whence = tswap16(fl.l_whence);
8112 target_fl->l_start = tswap64(fl.l_start);
8113 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008114 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008115 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008116 }
bellard77e46722003-04-29 20:39:06 +00008117 }
8118 break;
8119
thsb1e341e2007-03-20 21:50:52 +00008120 case TARGET_F_SETLK64:
8121 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00008122#ifdef TARGET_ARM
8123 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008124 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8125 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008126 fl.l_type = tswap16(target_efl->l_type);
8127 fl.l_whence = tswap16(target_efl->l_whence);
8128 fl.l_start = tswap64(target_efl->l_start);
8129 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008130 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008131 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008132 } else
8133#endif
8134 {
bellard9ee1fa22007-11-11 15:11:19 +00008135 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8136 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008137 fl.l_type = tswap16(target_fl->l_type);
8138 fl.l_whence = tswap16(target_fl->l_whence);
8139 fl.l_start = tswap64(target_fl->l_start);
8140 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008141 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008142 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008143 }
thsb1e341e2007-03-20 21:50:52 +00008144 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008145 break;
bellard60cd49d2003-03-16 22:53:56 +00008146 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008147 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00008148 break;
8149 }
bellard77e46722003-04-29 20:39:06 +00008150 break;
8151 }
bellard60cd49d2003-03-16 22:53:56 +00008152#endif
ths7d600c82006-12-08 01:32:58 +00008153#ifdef TARGET_NR_cacheflush
8154 case TARGET_NR_cacheflush:
8155 /* self-modifying code is handled automatically, so nothing needed */
8156 ret = 0;
8157 break;
8158#endif
bellardebc05482003-09-30 21:08:41 +00008159#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00008160 case TARGET_NR_security:
8161 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008162#endif
bellardc573ff62004-01-04 15:51:36 +00008163#ifdef TARGET_NR_getpagesize
8164 case TARGET_NR_getpagesize:
8165 ret = TARGET_PAGE_SIZE;
8166 break;
8167#endif
bellard31e31b82003-02-18 22:55:36 +00008168 case TARGET_NR_gettid:
8169 ret = get_errno(gettid());
8170 break;
thse5febef2007-04-01 18:31:35 +00008171#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00008172 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00008173#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +03008174 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +00008175 arg2 = arg3;
8176 arg3 = arg4;
8177 arg4 = arg5;
8178 }
aurel322054ac92008-10-13 21:08:07 +00008179 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8180#else
8181 ret = get_errno(readahead(arg1, arg2, arg3));
8182#endif
8183 break;
thse5febef2007-04-01 18:31:35 +00008184#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008185#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +00008186#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00008187 case TARGET_NR_listxattr:
8188 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +00008189 {
8190 void *p, *b = 0;
8191 if (arg2) {
8192 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8193 if (!b) {
8194 ret = -TARGET_EFAULT;
8195 break;
8196 }
8197 }
8198 p = lock_user_string(arg1);
8199 if (p) {
8200 if (num == TARGET_NR_listxattr) {
8201 ret = get_errno(listxattr(p, b, arg3));
8202 } else {
8203 ret = get_errno(llistxattr(p, b, arg3));
8204 }
8205 } else {
8206 ret = -TARGET_EFAULT;
8207 }
8208 unlock_user(p, arg1, 0);
8209 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +03008210 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +00008211 }
8212 case TARGET_NR_flistxattr:
8213 {
8214 void *b = 0;
8215 if (arg2) {
8216 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8217 if (!b) {
8218 ret = -TARGET_EFAULT;
8219 break;
8220 }
8221 }
8222 ret = get_errno(flistxattr(arg1, b, arg3));
8223 unlock_user(b, arg2, arg3);
8224 break;
8225 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008226 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008227 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008228 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008229 void *p, *n, *v = 0;
8230 if (arg3) {
8231 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8232 if (!v) {
8233 ret = -TARGET_EFAULT;
8234 break;
8235 }
8236 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008237 p = lock_user_string(arg1);
8238 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008239 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008240 if (num == TARGET_NR_setxattr) {
8241 ret = get_errno(setxattr(p, n, v, arg4, arg5));
8242 } else {
8243 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
8244 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008245 } else {
8246 ret = -TARGET_EFAULT;
8247 }
8248 unlock_user(p, arg1, 0);
8249 unlock_user(n, arg2, 0);
8250 unlock_user(v, arg3, 0);
8251 }
8252 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008253 case TARGET_NR_fsetxattr:
8254 {
8255 void *n, *v = 0;
8256 if (arg3) {
8257 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8258 if (!v) {
8259 ret = -TARGET_EFAULT;
8260 break;
8261 }
8262 }
8263 n = lock_user_string(arg2);
8264 if (n) {
8265 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
8266 } else {
8267 ret = -TARGET_EFAULT;
8268 }
8269 unlock_user(n, arg2, 0);
8270 unlock_user(v, arg3, 0);
8271 }
8272 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008273 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008274 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008275 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008276 void *p, *n, *v = 0;
8277 if (arg3) {
8278 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8279 if (!v) {
8280 ret = -TARGET_EFAULT;
8281 break;
8282 }
8283 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008284 p = lock_user_string(arg1);
8285 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008286 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008287 if (num == TARGET_NR_getxattr) {
8288 ret = get_errno(getxattr(p, n, v, arg4));
8289 } else {
8290 ret = get_errno(lgetxattr(p, n, v, arg4));
8291 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008292 } else {
8293 ret = -TARGET_EFAULT;
8294 }
8295 unlock_user(p, arg1, 0);
8296 unlock_user(n, arg2, 0);
8297 unlock_user(v, arg3, arg4);
8298 }
8299 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008300 case TARGET_NR_fgetxattr:
8301 {
8302 void *n, *v = 0;
8303 if (arg3) {
8304 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8305 if (!v) {
8306 ret = -TARGET_EFAULT;
8307 break;
8308 }
8309 }
8310 n = lock_user_string(arg2);
8311 if (n) {
8312 ret = get_errno(fgetxattr(arg1, n, v, arg4));
8313 } else {
8314 ret = -TARGET_EFAULT;
8315 }
8316 unlock_user(n, arg2, 0);
8317 unlock_user(v, arg3, arg4);
8318 }
8319 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008320 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008321 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008322 {
8323 void *p, *n;
8324 p = lock_user_string(arg1);
8325 n = lock_user_string(arg2);
8326 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008327 if (num == TARGET_NR_removexattr) {
8328 ret = get_errno(removexattr(p, n));
8329 } else {
8330 ret = get_errno(lremovexattr(p, n));
8331 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008332 } else {
8333 ret = -TARGET_EFAULT;
8334 }
8335 unlock_user(p, arg1, 0);
8336 unlock_user(n, arg2, 0);
8337 }
8338 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008339 case TARGET_NR_fremovexattr:
8340 {
8341 void *n;
8342 n = lock_user_string(arg2);
8343 if (n) {
8344 ret = get_errno(fremovexattr(arg1, n));
8345 } else {
8346 ret = -TARGET_EFAULT;
8347 }
8348 unlock_user(n, arg2, 0);
8349 }
8350 break;
bellardebc05482003-09-30 21:08:41 +00008351#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008352#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +00008353#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00008354 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008355#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00008356 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
8357 ret = 0;
8358 break;
edgar_iglef967792009-01-07 14:19:38 +00008359#elif defined(TARGET_CRIS)
8360 if (arg1 & 0xff)
8361 ret = -TARGET_EINVAL;
8362 else {
8363 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
8364 ret = 0;
8365 }
8366 break;
bellard8d18e892007-11-14 15:18:40 +00008367#elif defined(TARGET_I386) && defined(TARGET_ABI32)
8368 ret = do_set_thread_area(cpu_env, arg1);
8369 break;
ths6f5b89a2007-03-02 20:48:00 +00008370#else
8371 goto unimplemented_nowarn;
8372#endif
8373#endif
8374#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00008375 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008376#if defined(TARGET_I386) && defined(TARGET_ABI32)
8377 ret = do_get_thread_area(cpu_env, arg1);
8378#else
bellard5cd43932003-03-29 16:54:36 +00008379 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00008380#endif
bellard8d18e892007-11-14 15:18:40 +00008381#endif
bellard48dc41e2006-06-21 18:15:50 +00008382#ifdef TARGET_NR_getdomainname
8383 case TARGET_NR_getdomainname:
8384 goto unimplemented_nowarn;
8385#endif
ths6f5b89a2007-03-02 20:48:00 +00008386
thsb5906f92007-03-19 13:32:45 +00008387#ifdef TARGET_NR_clock_gettime
8388 case TARGET_NR_clock_gettime:
8389 {
8390 struct timespec ts;
8391 ret = get_errno(clock_gettime(arg1, &ts));
8392 if (!is_error(ret)) {
8393 host_to_target_timespec(arg2, &ts);
8394 }
8395 break;
8396 }
8397#endif
8398#ifdef TARGET_NR_clock_getres
8399 case TARGET_NR_clock_getres:
8400 {
8401 struct timespec ts;
8402 ret = get_errno(clock_getres(arg1, &ts));
8403 if (!is_error(ret)) {
8404 host_to_target_timespec(arg2, &ts);
8405 }
8406 break;
8407 }
8408#endif
pbrook63d76512008-05-29 13:43:29 +00008409#ifdef TARGET_NR_clock_nanosleep
8410 case TARGET_NR_clock_nanosleep:
8411 {
8412 struct timespec ts;
8413 target_to_host_timespec(&ts, arg3);
8414 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
8415 if (arg4)
8416 host_to_target_timespec(arg4, &ts);
8417 break;
8418 }
8419#endif
thsb5906f92007-03-19 13:32:45 +00008420
ths6f5b89a2007-03-02 20:48:00 +00008421#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8422 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00008423 ret = get_errno(set_tid_address((int *)g2h(arg1)));
8424 break;
ths6f5b89a2007-03-02 20:48:00 +00008425#endif
8426
ths3ae43202007-09-16 21:39:48 +00008427#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00008428 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00008429 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00008430 break;
8431#endif
8432
ths3ae43202007-09-16 21:39:48 +00008433#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00008434 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00008435 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
8436 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00008437 break;
8438#endif
8439
ths4f2b1fe2007-06-21 21:57:12 +00008440#ifdef TARGET_NR_set_robust_list
8441 case TARGET_NR_set_robust_list:
8442 goto unimplemented_nowarn;
8443#endif
8444
ths9007f0e2007-09-25 17:50:37 +00008445#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
8446 case TARGET_NR_utimensat:
8447 {
Riku Voipioebc996f2009-04-21 15:01:51 +03008448 struct timespec *tsp, ts[2];
8449 if (!arg3) {
8450 tsp = NULL;
8451 } else {
8452 target_to_host_timespec(ts, arg3);
8453 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
8454 tsp = ts;
8455 }
ths9007f0e2007-09-25 17:50:37 +00008456 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03008457 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00008458 else {
bellard579a97f2007-11-11 14:26:47 +00008459 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00008460 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00008461 goto fail;
8462 }
Riku Voipioebc996f2009-04-21 15:01:51 +03008463 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00008464 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00008465 }
8466 }
8467 break;
8468#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02008469#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00008470 case TARGET_NR_futex:
8471 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
8472 break;
8473#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008474#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00008475 case TARGET_NR_inotify_init:
8476 ret = get_errno(sys_inotify_init());
8477 break;
8478#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008479#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00008480#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8481 case TARGET_NR_inotify_init1:
8482 ret = get_errno(sys_inotify_init1(arg1));
8483 break;
8484#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008485#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008486#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00008487 case TARGET_NR_inotify_add_watch:
8488 p = lock_user_string(arg2);
8489 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
8490 unlock_user(p, arg2, 0);
8491 break;
8492#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008493#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00008494 case TARGET_NR_inotify_rm_watch:
8495 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
8496 break;
8497#endif
ths9007f0e2007-09-25 17:50:37 +00008498
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07008499#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00008500 case TARGET_NR_mq_open:
8501 {
8502 struct mq_attr posix_mq_attr;
8503
8504 p = lock_user_string(arg1 - 1);
8505 if (arg4 != 0)
8506 copy_from_user_mq_attr (&posix_mq_attr, arg4);
8507 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
8508 unlock_user (p, arg1, 0);
8509 }
8510 break;
8511
8512 case TARGET_NR_mq_unlink:
8513 p = lock_user_string(arg1 - 1);
8514 ret = get_errno(mq_unlink(p));
8515 unlock_user (p, arg1, 0);
8516 break;
8517
8518 case TARGET_NR_mq_timedsend:
8519 {
8520 struct timespec ts;
8521
8522 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8523 if (arg5 != 0) {
8524 target_to_host_timespec(&ts, arg5);
8525 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
8526 host_to_target_timespec(arg5, &ts);
8527 }
8528 else
8529 ret = get_errno(mq_send(arg1, p, arg3, arg4));
8530 unlock_user (p, arg2, arg3);
8531 }
8532 break;
8533
8534 case TARGET_NR_mq_timedreceive:
8535 {
8536 struct timespec ts;
8537 unsigned int prio;
8538
8539 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8540 if (arg5 != 0) {
8541 target_to_host_timespec(&ts, arg5);
8542 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
8543 host_to_target_timespec(arg5, &ts);
8544 }
8545 else
8546 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
8547 unlock_user (p, arg2, arg3);
8548 if (arg4 != 0)
8549 put_user_u32(prio, arg4);
8550 }
8551 break;
8552
8553 /* Not implemented for now... */
8554/* case TARGET_NR_mq_notify: */
8555/* break; */
8556
8557 case TARGET_NR_mq_getsetattr:
8558 {
8559 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
8560 ret = 0;
8561 if (arg3 != 0) {
8562 ret = mq_getattr(arg1, &posix_mq_attr_out);
8563 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
8564 }
8565 if (arg2 != 0) {
8566 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
8567 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
8568 }
8569
8570 }
8571 break;
8572#endif
8573
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308574#ifdef CONFIG_SPLICE
8575#ifdef TARGET_NR_tee
8576 case TARGET_NR_tee:
8577 {
8578 ret = get_errno(tee(arg1,arg2,arg3,arg4));
8579 }
8580 break;
8581#endif
8582#ifdef TARGET_NR_splice
8583 case TARGET_NR_splice:
8584 {
8585 loff_t loff_in, loff_out;
8586 loff_t *ploff_in = NULL, *ploff_out = NULL;
8587 if(arg2) {
8588 get_user_u64(loff_in, arg2);
8589 ploff_in = &loff_in;
8590 }
8591 if(arg4) {
8592 get_user_u64(loff_out, arg2);
8593 ploff_out = &loff_out;
8594 }
8595 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
8596 }
8597 break;
8598#endif
8599#ifdef TARGET_NR_vmsplice
8600 case TARGET_NR_vmsplice:
8601 {
8602 int count = arg3;
8603 struct iovec *vec;
8604
8605 vec = alloca(count * sizeof(struct iovec));
8606 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
8607 goto efault;
8608 ret = get_errno(vmsplice(arg1, vec, count, arg4));
8609 unlock_iovec(vec, arg2, count, 0);
8610 }
8611 break;
8612#endif
8613#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03008614#ifdef CONFIG_EVENTFD
8615#if defined(TARGET_NR_eventfd)
8616 case TARGET_NR_eventfd:
8617 ret = get_errno(eventfd(arg1, 0));
8618 break;
8619#endif
8620#if defined(TARGET_NR_eventfd2)
8621 case TARGET_NR_eventfd2:
8622 ret = get_errno(eventfd(arg1, arg2));
8623 break;
8624#endif
8625#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03008626#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8627 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +01008628#if TARGET_ABI_BITS == 32
8629 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
8630 target_offset64(arg5, arg6)));
8631#else
Ulrich Hechtd0927932009-09-17 20:22:14 +03008632 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +01008633#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03008634 break;
8635#endif
Peter Maydellc727f472011-01-06 11:05:10 +00008636#if defined(CONFIG_SYNC_FILE_RANGE)
8637#if defined(TARGET_NR_sync_file_range)
8638 case TARGET_NR_sync_file_range:
8639#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +03008640#if defined(TARGET_MIPS)
8641 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8642 target_offset64(arg5, arg6), arg7));
8643#else
Peter Maydellc727f472011-01-06 11:05:10 +00008644 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
8645 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +03008646#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +00008647#else
8648 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
8649#endif
8650 break;
8651#endif
8652#if defined(TARGET_NR_sync_file_range2)
8653 case TARGET_NR_sync_file_range2:
8654 /* This is like sync_file_range but the arguments are reordered */
8655#if TARGET_ABI_BITS == 32
8656 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8657 target_offset64(arg5, arg6), arg2));
8658#else
8659 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
8660#endif
8661 break;
8662#endif
8663#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00008664#if defined(CONFIG_EPOLL)
8665#if defined(TARGET_NR_epoll_create)
8666 case TARGET_NR_epoll_create:
8667 ret = get_errno(epoll_create(arg1));
8668 break;
8669#endif
8670#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
8671 case TARGET_NR_epoll_create1:
8672 ret = get_errno(epoll_create1(arg1));
8673 break;
8674#endif
8675#if defined(TARGET_NR_epoll_ctl)
8676 case TARGET_NR_epoll_ctl:
8677 {
8678 struct epoll_event ep;
8679 struct epoll_event *epp = 0;
8680 if (arg4) {
8681 struct target_epoll_event *target_ep;
8682 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
8683 goto efault;
8684 }
8685 ep.events = tswap32(target_ep->events);
8686 /* The epoll_data_t union is just opaque data to the kernel,
8687 * so we transfer all 64 bits across and need not worry what
8688 * actual data type it is.
8689 */
8690 ep.data.u64 = tswap64(target_ep->data.u64);
8691 unlock_user_struct(target_ep, arg4, 0);
8692 epp = &ep;
8693 }
8694 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
8695 break;
8696 }
8697#endif
8698
8699#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
8700#define IMPLEMENT_EPOLL_PWAIT
8701#endif
8702#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
8703#if defined(TARGET_NR_epoll_wait)
8704 case TARGET_NR_epoll_wait:
8705#endif
8706#if defined(IMPLEMENT_EPOLL_PWAIT)
8707 case TARGET_NR_epoll_pwait:
8708#endif
8709 {
8710 struct target_epoll_event *target_ep;
8711 struct epoll_event *ep;
8712 int epfd = arg1;
8713 int maxevents = arg3;
8714 int timeout = arg4;
8715
8716 target_ep = lock_user(VERIFY_WRITE, arg2,
8717 maxevents * sizeof(struct target_epoll_event), 1);
8718 if (!target_ep) {
8719 goto efault;
8720 }
8721
8722 ep = alloca(maxevents * sizeof(struct epoll_event));
8723
8724 switch (num) {
8725#if defined(IMPLEMENT_EPOLL_PWAIT)
8726 case TARGET_NR_epoll_pwait:
8727 {
8728 target_sigset_t *target_set;
8729 sigset_t _set, *set = &_set;
8730
8731 if (arg5) {
8732 target_set = lock_user(VERIFY_READ, arg5,
8733 sizeof(target_sigset_t), 1);
8734 if (!target_set) {
8735 unlock_user(target_ep, arg2, 0);
8736 goto efault;
8737 }
8738 target_to_host_sigset(set, target_set);
8739 unlock_user(target_set, arg5, 0);
8740 } else {
8741 set = NULL;
8742 }
8743
8744 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
8745 break;
8746 }
8747#endif
8748#if defined(TARGET_NR_epoll_wait)
8749 case TARGET_NR_epoll_wait:
8750 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
8751 break;
8752#endif
8753 default:
8754 ret = -TARGET_ENOSYS;
8755 }
8756 if (!is_error(ret)) {
8757 int i;
8758 for (i = 0; i < ret; i++) {
8759 target_ep[i].events = tswap32(ep[i].events);
8760 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
8761 }
8762 }
8763 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
8764 break;
8765 }
8766#endif
8767#endif
Peter Maydell163a05a2011-06-27 17:44:52 +01008768#ifdef TARGET_NR_prlimit64
8769 case TARGET_NR_prlimit64:
8770 {
8771 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
8772 struct target_rlimit64 *target_rnew, *target_rold;
8773 struct host_rlimit64 rnew, rold, *rnewp = 0;
8774 if (arg3) {
8775 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
8776 goto efault;
8777 }
8778 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
8779 rnew.rlim_max = tswap64(target_rnew->rlim_max);
8780 unlock_user_struct(target_rnew, arg3, 0);
8781 rnewp = &rnew;
8782 }
8783
8784 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
8785 if (!is_error(ret) && arg4) {
8786 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
8787 goto efault;
8788 }
8789 target_rold->rlim_cur = tswap64(rold.rlim_cur);
8790 target_rold->rlim_max = tswap64(rold.rlim_max);
8791 unlock_user_struct(target_rold, arg4, 1);
8792 }
8793 break;
8794 }
8795#endif
bellard31e31b82003-02-18 22:55:36 +00008796 default:
8797 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00008798 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00008799#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 +00008800 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00008801#endif
ths0da46a62007-10-20 20:23:07 +00008802 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00008803 break;
8804 }
bellard579a97f2007-11-11 14:26:47 +00008805fail:
bellardc573ff62004-01-04 15:51:36 +00008806#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00008807 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00008808#endif
thsb92c47c2007-11-01 00:07:38 +00008809 if(do_strace)
8810 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00008811 return ret;
bellard579a97f2007-11-11 14:26:47 +00008812efault:
8813 ret = -TARGET_EFAULT;
8814 goto fail;
bellard31e31b82003-02-18 22:55:36 +00008815}