blob: 14a6b32ab19e771287c324751949fc5495ab318b [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>
Jing Huang920394d2012-07-24 13:59:23 +000063#include <linux/icmp.h>
Stefan Weil5a61cb62011-09-08 17:55:32 +020064#include "qemu-common.h"
Juan Quintela9788c9c2009-07-27 16:13:02 +020065#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +000066#include <sys/gmon.h>
67#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030068#ifdef CONFIG_EVENTFD
69#include <sys/eventfd.h>
70#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000071#ifdef CONFIG_EPOLL
72#include <sys/epoll.h>
73#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070074#ifdef CONFIG_ATTR
Avi Kivity4f26f2b2011-11-09 14:44:52 +020075#include "qemu-xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070076#endif
bellard31e31b82003-02-18 22:55:36 +000077
78#define termios host_termios
79#define winsize host_winsize
80#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000081#define sgttyb host_sgttyb /* same as target */
82#define tchars host_tchars /* same as target */
83#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000084
85#include <linux/termios.h>
86#include <linux/unistd.h>
87#include <linux/utsname.h>
88#include <linux/cdrom.h>
89#include <linux/hdreg.h>
90#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000091#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000092#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +030093#include <linux/fs.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000094#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +000095#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000096#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +020097#include <linux/fb.h>
98#include <linux/vt.h>
Alexander Graf56e904e2012-01-31 18:42:06 +010099#include <linux/dm-ioctl.h>
pbrookd7e40362008-05-23 16:06:43 +0000100#include "linux_loop.h"
Loïc Minierda790302009-12-29 22:06:13 +0100101#include "cpu-uname.h"
bellard31e31b82003-02-18 22:55:36 +0000102
bellard3ef693a2003-03-23 20:17:16 +0000103#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000104
Juan Quintela2f7bb872009-07-27 16:13:24 +0200105#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +0000106#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
107 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
108#else
109/* XXX: Hardcode the above values. */
110#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +0000111#endif
112
bellard72f03902003-02-18 23:33:18 +0000113//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000114
bellard1a9353d2003-03-16 20:28:50 +0000115//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000116#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
117#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000118
bellard70a194b2003-08-11 22:20:16 +0000119
bellard70a194b2003-08-11 22:20:16 +0000120#undef _syscall0
121#undef _syscall1
122#undef _syscall2
123#undef _syscall3
124#undef _syscall4
125#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000126#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000127
bellard83fcb512006-06-14 13:37:16 +0000128#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000129static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000130{ \
131 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000132}
133
bellard83fcb512006-06-14 13:37:16 +0000134#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000135static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000136{ \
137 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000138}
139
bellard83fcb512006-06-14 13:37:16 +0000140#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000141static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000142{ \
143 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000144}
145
bellard83fcb512006-06-14 13:37:16 +0000146#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000147static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000148{ \
149 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000150}
151
bellard83fcb512006-06-14 13:37:16 +0000152#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000153static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000154{ \
155 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000156}
157
bellard83fcb512006-06-14 13:37:16 +0000158#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
159 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000160static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000161{ \
162 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000163}
bellard83fcb512006-06-14 13:37:16 +0000164
165
166#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
167 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000168static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
169 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000170{ \
171 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
172}
173
bellard70a194b2003-08-11 22:20:16 +0000174
bellard31e31b82003-02-18 22:55:36 +0000175#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000176#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000177#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000178#define __NR_sys_fchownat __NR_fchownat
balrog6a24a772008-09-20 02:23:36 +0000179#define __NR_sys_fstatat64 __NR_fstatat64
balrogac8a6552008-09-20 02:25:39 +0000180#define __NR_sys_futimesat __NR_futimesat
bellard72f03902003-02-18 23:33:18 +0000181#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000182#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000183#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000184#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000185#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000186#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000187#define __NR_sys_mknodat __NR_mknodat
aurel329d33b762009-04-08 23:07:05 +0000188#define __NR_sys_newfstatat __NR_newfstatat
ths82424832007-09-24 09:21:55 +0000189#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000190#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000191#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000192#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000193#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000194#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000195#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000196#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000197#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000198#define __NR_sys_utimensat __NR_utimensat
pbrookbd0c5662008-05-29 14:34:11 +0000199#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000200#define __NR_sys_inotify_init __NR_inotify_init
201#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
202#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000203
Alexander Graf42a39fb2011-04-15 17:32:45 +0200204#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
205 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000206#define __NR__llseek __NR_lseek
207#endif
208
bellard72f03902003-02-18 23:33:18 +0000209#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000210_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000211#else
ths0da46a62007-10-20 20:23:07 +0000212/* This is a replacement for the host gettid() and must return a host
213 errno. */
bellard72f03902003-02-18 23:33:18 +0000214static int gettid(void) {
215 return -ENOSYS;
216}
217#endif
aurel323b3f24a2009-04-15 16:12:13 +0000218_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
aurel323b3f24a2009-04-15 16:12:13 +0000219#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
220_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
221#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700222#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000223_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
224 loff_t *, res, uint, wh);
225#endif
226_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
227_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
228#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
229_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
230#endif
231#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
232_syscall2(int,sys_tkill,int,tid,int,sig)
233#endif
234#ifdef __NR_exit_group
235_syscall1(int,exit_group,int,error_code)
236#endif
237#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
238_syscall1(int,set_tid_address,int *,tidptr)
239#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +0200240#if defined(CONFIG_USE_NPTL)
aurel323b3f24a2009-04-15 16:12:13 +0000241#if defined(TARGET_NR_futex) && defined(__NR_futex)
242_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
243 const struct timespec *,timeout,int *,uaddr2,int,val3)
244#endif
245#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500246#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
247_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
248 unsigned long *, user_mask_ptr);
249#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
250_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
251 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200252_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
253 void *, arg);
aurel323b3f24a2009-04-15 16:12:13 +0000254
255static bitmask_transtbl fcntl_flags_tbl[] = {
256 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
257 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
258 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
259 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
260 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
261 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
262 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
263 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700264 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000265 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
266 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
267 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
268 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000269#if defined(O_DIRECT)
270 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
271#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700272#if defined(O_NOATIME)
273 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
274#endif
275#if defined(O_CLOEXEC)
276 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
277#endif
278#if defined(O_PATH)
279 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
280#endif
281 /* Don't terminate the list prematurely on 64-bit host+guest. */
282#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
283 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
284#endif
aurel323b3f24a2009-04-15 16:12:13 +0000285 { 0, 0, 0, 0 }
286};
287
288#define COPY_UTSNAME_FIELD(dest, src) \
289 do { \
290 /* __NEW_UTS_LEN doesn't include terminating null */ \
291 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
292 (dest)[__NEW_UTS_LEN] = '\0'; \
293 } while (0)
294
295static int sys_uname(struct new_utsname *buf)
296{
297 struct utsname uts_buf;
298
299 if (uname(&uts_buf) < 0)
300 return (-1);
301
302 /*
303 * Just in case these have some differences, we
304 * translate utsname to new_utsname (which is the
305 * struct linux kernel uses).
306 */
307
Stefan Weil67bd9ed2011-05-07 22:32:25 +0200308 memset(buf, 0, sizeof(*buf));
aurel323b3f24a2009-04-15 16:12:13 +0000309 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
310 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
311 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
312 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
313 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
314#ifdef _GNU_SOURCE
315 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
316#endif
317 return (0);
318
319#undef COPY_UTSNAME_FIELD
320}
321
322static int sys_getcwd1(char *buf, size_t size)
323{
324 if (getcwd(buf, size) == NULL) {
325 /* getcwd() sets errno */
326 return (-1);
327 }
aurel32aaf4ad32009-04-16 14:17:14 +0000328 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000329}
330
331#ifdef CONFIG_ATFILE
332/*
333 * Host system seems to have atfile syscall stubs available. We
334 * now enable them one by one as specified by target syscall_nr.h.
335 */
336
337#ifdef TARGET_NR_faccessat
aurel32465c9f02009-04-19 08:52:17 +0000338static int sys_faccessat(int dirfd, const char *pathname, int mode)
aurel323b3f24a2009-04-15 16:12:13 +0000339{
aurel32465c9f02009-04-19 08:52:17 +0000340 return (faccessat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000341}
342#endif
343#ifdef TARGET_NR_fchmodat
aurel32465c9f02009-04-19 08:52:17 +0000344static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000345{
aurel32465c9f02009-04-19 08:52:17 +0000346 return (fchmodat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000347}
348#endif
Riku Voipio0c866a72011-04-18 15:23:06 +0300349#if defined(TARGET_NR_fchownat)
aurel323b3f24a2009-04-15 16:12:13 +0000350static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
351 gid_t group, int flags)
352{
353 return (fchownat(dirfd, pathname, owner, group, flags));
354}
355#endif
356#ifdef __NR_fstatat64
357static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
358 int flags)
359{
360 return (fstatat(dirfd, pathname, buf, flags));
361}
362#endif
363#ifdef __NR_newfstatat
364static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
365 int flags)
366{
367 return (fstatat(dirfd, pathname, buf, flags));
368}
369#endif
370#ifdef TARGET_NR_futimesat
371static int sys_futimesat(int dirfd, const char *pathname,
372 const struct timeval times[2])
373{
374 return (futimesat(dirfd, pathname, times));
375}
376#endif
377#ifdef TARGET_NR_linkat
378static int sys_linkat(int olddirfd, const char *oldpath,
379 int newdirfd, const char *newpath, int flags)
380{
381 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
382}
383#endif
384#ifdef TARGET_NR_mkdirat
385static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
386{
387 return (mkdirat(dirfd, pathname, mode));
388}
389#endif
390#ifdef TARGET_NR_mknodat
391static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
392 dev_t dev)
393{
394 return (mknodat(dirfd, pathname, mode, dev));
395}
396#endif
397#ifdef TARGET_NR_openat
Alexander Graff4c69012011-09-25 06:25:35 +0200398static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000399{
400 /*
401 * open(2) has extra parameter 'mode' when called with
402 * flag O_CREAT.
403 */
404 if ((flags & O_CREAT) != 0) {
aurel323b3f24a2009-04-15 16:12:13 +0000405 return (openat(dirfd, pathname, flags, mode));
406 }
407 return (openat(dirfd, pathname, flags));
408}
409#endif
410#ifdef TARGET_NR_readlinkat
411static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
412{
413 return (readlinkat(dirfd, pathname, buf, bufsiz));
414}
415#endif
416#ifdef TARGET_NR_renameat
417static int sys_renameat(int olddirfd, const char *oldpath,
418 int newdirfd, const char *newpath)
419{
420 return (renameat(olddirfd, oldpath, newdirfd, newpath));
421}
422#endif
423#ifdef TARGET_NR_symlinkat
424static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
425{
426 return (symlinkat(oldpath, newdirfd, newpath));
427}
428#endif
429#ifdef TARGET_NR_unlinkat
430static int sys_unlinkat(int dirfd, const char *pathname, int flags)
431{
432 return (unlinkat(dirfd, pathname, flags));
433}
434#endif
aurel323b3f24a2009-04-15 16:12:13 +0000435#else /* !CONFIG_ATFILE */
436
437/*
438 * Try direct syscalls instead
439 */
ths92a34c12007-09-24 09:27:49 +0000440#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
aurel32465c9f02009-04-19 08:52:17 +0000441_syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
ths92a34c12007-09-24 09:27:49 +0000442#endif
ths814d7972007-09-24 09:26:51 +0000443#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
aurel32465c9f02009-04-19 08:52:17 +0000444_syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
ths814d7972007-09-24 09:26:51 +0000445#endif
Riku Voipio0c866a72011-04-18 15:23:06 +0300446#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +0000447_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
448 uid_t,owner,gid_t,group,int,flags)
449#endif
aurel329d33b762009-04-08 23:07:05 +0000450#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
451 defined(__NR_fstatat64)
balrog6a24a772008-09-20 02:23:36 +0000452_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
453 struct stat *,buf,int,flags)
454#endif
balrogac8a6552008-09-20 02:25:39 +0000455#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
456_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
457 const struct timeval *,times)
458#endif
aurel323b3f24a2009-04-15 16:12:13 +0000459#if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
460 defined(__NR_newfstatat)
461_syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
462 struct stat *,buf,int,flags)
blueswir18fcd3692008-08-17 20:26:25 +0000463#endif
ths64f0ce42007-09-24 09:25:06 +0000464#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
465_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
aurel323b3f24a2009-04-15 16:12:13 +0000466 int,newdirfd,const char *,newpath,int,flags)
ths64f0ce42007-09-24 09:25:06 +0000467#endif
ths4472ad02007-09-24 09:22:32 +0000468#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
469_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
470#endif
ths75ac37a2007-09-24 09:23:05 +0000471#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
472_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
473 mode_t,mode,dev_t,dev)
474#endif
ths82424832007-09-24 09:21:55 +0000475#if defined(TARGET_NR_openat) && defined(__NR_openat)
476_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
477#endif
ths5e0ccb12007-09-24 09:26:10 +0000478#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
479_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
480 char *,buf,size_t,bufsize)
481#endif
ths722183f2007-09-24 09:24:37 +0000482#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
483_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
484 int,newdirfd,const char *,newpath)
485#endif
thsb51eaa82007-09-25 16:09:22 +0000486#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000487_syscall3(int,sys_symlinkat,const char *,oldpath,
488 int,newdirfd,const char *,newpath)
489#endif
ths8170f562007-09-24 09:24:11 +0000490#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
491_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
492#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300493
494#endif /* CONFIG_ATFILE */
495
496#ifdef CONFIG_UTIMENSAT
497static int sys_utimensat(int dirfd, const char *pathname,
498 const struct timespec times[2], int flags)
499{
500 if (pathname == NULL)
501 return futimens(dirfd, times);
502 else
503 return utimensat(dirfd, pathname, times, flags);
504}
505#else
ths9007f0e2007-09-25 17:50:37 +0000506#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
507_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
508 const struct timespec *,tsp,int,flags)
509#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300510#endif /* CONFIG_UTIMENSAT */
aurel323b3f24a2009-04-15 16:12:13 +0000511
512#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000513#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000514
aurel3239b59762008-10-01 21:46:50 +0000515#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000516static int sys_inotify_init(void)
517{
518 return (inotify_init());
519}
aurel3239b59762008-10-01 21:46:50 +0000520#endif
521#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000522static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
523{
524 return (inotify_add_watch(fd, pathname, mask));
525}
aurel3239b59762008-10-01 21:46:50 +0000526#endif
527#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000528static int sys_inotify_rm_watch(int fd, int32_t wd)
529{
aurel328690e422009-04-17 13:50:32 +0000530 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000531}
aurel3239b59762008-10-01 21:46:50 +0000532#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000533#ifdef CONFIG_INOTIFY1
534#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
535static int sys_inotify_init1(int flags)
536{
537 return (inotify_init1(flags));
538}
539#endif
540#endif
aurel323b3f24a2009-04-15 16:12:13 +0000541#else
542/* Userspace can usually survive runtime without inotify */
543#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000544#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000545#undef TARGET_NR_inotify_add_watch
546#undef TARGET_NR_inotify_rm_watch
547#endif /* CONFIG_INOTIFY */
548
Mike Frysingerd8035d42011-02-07 01:05:51 -0500549#if defined(TARGET_NR_ppoll)
550#ifndef __NR_ppoll
551# define __NR_ppoll -1
552#endif
553#define __NR_sys_ppoll __NR_ppoll
554_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
555 struct timespec *, timeout, const __sigset_t *, sigmask,
556 size_t, sigsetsize)
557#endif
bellard66fb9762003-03-23 01:06:05 +0000558
Mike Frysinger055e0902011-06-03 17:01:49 -0400559#if defined(TARGET_NR_pselect6)
560#ifndef __NR_pselect6
561# define __NR_pselect6 -1
562#endif
563#define __NR_sys_pselect6 __NR_pselect6
564_syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
565 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
566#endif
567
Peter Maydell163a05a2011-06-27 17:44:52 +0100568#if defined(TARGET_NR_prlimit64)
569#ifndef __NR_prlimit64
570# define __NR_prlimit64 -1
571#endif
572#define __NR_sys_prlimit64 __NR_prlimit64
573/* The glibc rlimit structure may not be that used by the underlying syscall */
574struct host_rlimit64 {
575 uint64_t rlim_cur;
576 uint64_t rlim_max;
577};
578_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
579 const struct host_rlimit64 *, new_limit,
580 struct host_rlimit64 *, old_limit)
581#endif
582
bellard66fb9762003-03-23 01:06:05 +0000583extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000584extern int flock(int, int);
585extern int setfsuid(int);
586extern int setfsgid(int);
bellard19b84f32003-05-08 15:41:49 +0000587extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000588
Riku Voipio48e515d2011-07-12 15:40:51 +0300589/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
Alexander Graf4a1def42012-09-29 15:32:38 +0000590#ifdef TARGET_ARM
Riku Voipio48e515d2011-07-12 15:40:51 +0300591static inline int regpairs_aligned(void *cpu_env) {
592 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
593}
594#elif defined(TARGET_MIPS)
595static inline int regpairs_aligned(void *cpu_env) { return 1; }
Alexander Graf4a1def42012-09-29 15:32:38 +0000596#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
597/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
598 * of registers which translates to the same as ARM/MIPS, because we start with
599 * r3 as arg1 */
600static inline int regpairs_aligned(void *cpu_env) { return 1; }
Riku Voipio48e515d2011-07-12 15:40:51 +0300601#else
602static inline int regpairs_aligned(void *cpu_env) { return 0; }
603#endif
604
thsb92c47c2007-11-01 00:07:38 +0000605#define ERRNO_TABLE_SIZE 1200
606
607/* target_to_host_errno_table[] is initialized from
608 * host_to_target_errno_table[] in syscall_init(). */
609static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
610};
611
ths637947f2007-06-01 12:09:19 +0000612/*
thsfe8f0962007-07-12 10:59:21 +0000613 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000614 * minus the errnos that are not actually generic to all archs.
615 */
thsb92c47c2007-11-01 00:07:38 +0000616static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000617 [EIDRM] = TARGET_EIDRM,
618 [ECHRNG] = TARGET_ECHRNG,
619 [EL2NSYNC] = TARGET_EL2NSYNC,
620 [EL3HLT] = TARGET_EL3HLT,
621 [EL3RST] = TARGET_EL3RST,
622 [ELNRNG] = TARGET_ELNRNG,
623 [EUNATCH] = TARGET_EUNATCH,
624 [ENOCSI] = TARGET_ENOCSI,
625 [EL2HLT] = TARGET_EL2HLT,
626 [EDEADLK] = TARGET_EDEADLK,
627 [ENOLCK] = TARGET_ENOLCK,
628 [EBADE] = TARGET_EBADE,
629 [EBADR] = TARGET_EBADR,
630 [EXFULL] = TARGET_EXFULL,
631 [ENOANO] = TARGET_ENOANO,
632 [EBADRQC] = TARGET_EBADRQC,
633 [EBADSLT] = TARGET_EBADSLT,
634 [EBFONT] = TARGET_EBFONT,
635 [ENOSTR] = TARGET_ENOSTR,
636 [ENODATA] = TARGET_ENODATA,
637 [ETIME] = TARGET_ETIME,
638 [ENOSR] = TARGET_ENOSR,
639 [ENONET] = TARGET_ENONET,
640 [ENOPKG] = TARGET_ENOPKG,
641 [EREMOTE] = TARGET_EREMOTE,
642 [ENOLINK] = TARGET_ENOLINK,
643 [EADV] = TARGET_EADV,
644 [ESRMNT] = TARGET_ESRMNT,
645 [ECOMM] = TARGET_ECOMM,
646 [EPROTO] = TARGET_EPROTO,
647 [EDOTDOT] = TARGET_EDOTDOT,
648 [EMULTIHOP] = TARGET_EMULTIHOP,
649 [EBADMSG] = TARGET_EBADMSG,
650 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
651 [EOVERFLOW] = TARGET_EOVERFLOW,
652 [ENOTUNIQ] = TARGET_ENOTUNIQ,
653 [EBADFD] = TARGET_EBADFD,
654 [EREMCHG] = TARGET_EREMCHG,
655 [ELIBACC] = TARGET_ELIBACC,
656 [ELIBBAD] = TARGET_ELIBBAD,
657 [ELIBSCN] = TARGET_ELIBSCN,
658 [ELIBMAX] = TARGET_ELIBMAX,
659 [ELIBEXEC] = TARGET_ELIBEXEC,
660 [EILSEQ] = TARGET_EILSEQ,
661 [ENOSYS] = TARGET_ENOSYS,
662 [ELOOP] = TARGET_ELOOP,
663 [ERESTART] = TARGET_ERESTART,
664 [ESTRPIPE] = TARGET_ESTRPIPE,
665 [ENOTEMPTY] = TARGET_ENOTEMPTY,
666 [EUSERS] = TARGET_EUSERS,
667 [ENOTSOCK] = TARGET_ENOTSOCK,
668 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
669 [EMSGSIZE] = TARGET_EMSGSIZE,
670 [EPROTOTYPE] = TARGET_EPROTOTYPE,
671 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
672 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
673 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
674 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
675 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
676 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
677 [EADDRINUSE] = TARGET_EADDRINUSE,
678 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
679 [ENETDOWN] = TARGET_ENETDOWN,
680 [ENETUNREACH] = TARGET_ENETUNREACH,
681 [ENETRESET] = TARGET_ENETRESET,
682 [ECONNABORTED] = TARGET_ECONNABORTED,
683 [ECONNRESET] = TARGET_ECONNRESET,
684 [ENOBUFS] = TARGET_ENOBUFS,
685 [EISCONN] = TARGET_EISCONN,
686 [ENOTCONN] = TARGET_ENOTCONN,
687 [EUCLEAN] = TARGET_EUCLEAN,
688 [ENOTNAM] = TARGET_ENOTNAM,
689 [ENAVAIL] = TARGET_ENAVAIL,
690 [EISNAM] = TARGET_EISNAM,
691 [EREMOTEIO] = TARGET_EREMOTEIO,
692 [ESHUTDOWN] = TARGET_ESHUTDOWN,
693 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
694 [ETIMEDOUT] = TARGET_ETIMEDOUT,
695 [ECONNREFUSED] = TARGET_ECONNREFUSED,
696 [EHOSTDOWN] = TARGET_EHOSTDOWN,
697 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
698 [EALREADY] = TARGET_EALREADY,
699 [EINPROGRESS] = TARGET_EINPROGRESS,
700 [ESTALE] = TARGET_ESTALE,
701 [ECANCELED] = TARGET_ECANCELED,
702 [ENOMEDIUM] = TARGET_ENOMEDIUM,
703 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000704#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000705 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000706#endif
707#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000708 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000709#endif
710#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000711 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000712#endif
713#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000714 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000715#endif
716#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000717 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000718#endif
719#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000720 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000721#endif
thsb92c47c2007-11-01 00:07:38 +0000722};
ths637947f2007-06-01 12:09:19 +0000723
724static inline int host_to_target_errno(int err)
725{
726 if(host_to_target_errno_table[err])
727 return host_to_target_errno_table[err];
728 return err;
729}
730
thsb92c47c2007-11-01 00:07:38 +0000731static inline int target_to_host_errno(int err)
732{
733 if (target_to_host_errno_table[err])
734 return target_to_host_errno_table[err];
735 return err;
736}
737
blueswir1992f48a2007-10-14 16:27:31 +0000738static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000739{
740 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000741 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000742 else
743 return ret;
744}
745
blueswir1992f48a2007-10-14 16:27:31 +0000746static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000747{
blueswir1992f48a2007-10-14 16:27:31 +0000748 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000749}
750
thsb92c47c2007-11-01 00:07:38 +0000751char *target_strerror(int err)
752{
Alexander Graf962b2892011-11-21 12:04:07 +0100753 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
754 return NULL;
755 }
thsb92c47c2007-11-01 00:07:38 +0000756 return strerror(target_to_host_errno(err));
757}
758
blueswir1992f48a2007-10-14 16:27:31 +0000759static abi_ulong target_brk;
760static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000761static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000762
blueswir1992f48a2007-10-14 16:27:31 +0000763void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000764{
blueswir14c1de732007-07-07 20:45:44 +0000765 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000766 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000767}
768
vincent4d1de872011-06-14 21:56:33 +0000769//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
770#define DEBUGF_BRK(message, args...)
771
ths0da46a62007-10-20 20:23:07 +0000772/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000773abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000774{
blueswir1992f48a2007-10-14 16:27:31 +0000775 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000776 int new_alloc_size;
777
Paul Brook3a0c6c42012-02-09 19:04:27 +0000778 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000779
vincent4d1de872011-06-14 21:56:33 +0000780 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000781 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000782 return target_brk;
783 }
784 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000785 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
786 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000787 return target_brk;
788 }
bellard31e31b82003-02-18 22:55:36 +0000789
vincent4d1de872011-06-14 21:56:33 +0000790 /* If the new brk is less than the highest page reserved to the
791 * target heap allocation, set it and we're almost done... */
792 if (new_brk <= brk_page) {
793 /* Heap contents are initialized to zero, as for anonymous
794 * mapped pages. */
795 if (new_brk > target_brk) {
796 memset(g2h(target_brk), 0, new_brk - target_brk);
797 }
bellard31e31b82003-02-18 22:55:36 +0000798 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000799 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000800 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000801 }
802
Peter Maydell00faf082011-04-18 16:34:24 +0100803 /* We need to allocate more memory after the brk... Note that
804 * we don't use MAP_FIXED because that will map over the top of
805 * any existing mapping (like the one with the host libc or qemu
806 * itself); instead we treat "mapped but at wrong address" as
807 * a failure and unmap again.
808 */
vincent4d1de872011-06-14 21:56:33 +0000809 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000810 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000811 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100812 MAP_ANON|MAP_PRIVATE, 0, 0));
813
814 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200815 /* Heap contents are initialized to zero, as for anonymous
816 * mapped pages. Technically the new pages are already
817 * initialized to zero since they *are* anonymous mapped
818 * pages, however we have to take care with the contents that
819 * come from the remaining part of the previous page: it may
820 * contains garbage data due to a previous heap usage (grown
821 * then shrunken). */
822 memset(g2h(target_brk), 0, brk_page - target_brk);
823
Peter Maydell00faf082011-04-18 16:34:24 +0100824 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000825 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000826 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
827 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100828 return target_brk;
829 } else if (mapped_addr != -1) {
830 /* Mapped but at wrong address, meaning there wasn't actually
831 * enough space for this brk.
832 */
833 target_munmap(mapped_addr, new_alloc_size);
834 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000835 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000836 }
837 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000838 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100839 }
balrog7ab240a2008-04-26 12:17:34 +0000840
Richard Henderson7dd46c02010-05-03 10:07:49 -0700841#if defined(TARGET_ALPHA)
842 /* We (partially) emulate OSF/1 on Alpha, which requires we
843 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100844 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700845#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100846 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000847 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000848}
849
ths26edcf42007-12-09 02:25:24 +0000850static inline abi_long copy_from_user_fdset(fd_set *fds,
851 abi_ulong target_fds_addr,
852 int n)
bellard31e31b82003-02-18 22:55:36 +0000853{
ths26edcf42007-12-09 02:25:24 +0000854 int i, nw, j, k;
855 abi_ulong b, *target_fds;
856
857 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
858 if (!(target_fds = lock_user(VERIFY_READ,
859 target_fds_addr,
860 sizeof(abi_ulong) * nw,
861 1)))
862 return -TARGET_EFAULT;
863
864 FD_ZERO(fds);
865 k = 0;
866 for (i = 0; i < nw; i++) {
867 /* grab the abi_ulong */
868 __get_user(b, &target_fds[i]);
869 for (j = 0; j < TARGET_ABI_BITS; j++) {
870 /* check the bit inside the abi_ulong */
871 if ((b >> j) & 1)
872 FD_SET(k, fds);
873 k++;
bellard31e31b82003-02-18 22:55:36 +0000874 }
bellard31e31b82003-02-18 22:55:36 +0000875 }
ths26edcf42007-12-09 02:25:24 +0000876
877 unlock_user(target_fds, target_fds_addr, 0);
878
879 return 0;
bellard31e31b82003-02-18 22:55:36 +0000880}
881
Mike Frysinger055e0902011-06-03 17:01:49 -0400882static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
883 abi_ulong target_fds_addr,
884 int n)
885{
886 if (target_fds_addr) {
887 if (copy_from_user_fdset(fds, target_fds_addr, n))
888 return -TARGET_EFAULT;
889 *fds_ptr = fds;
890 } else {
891 *fds_ptr = NULL;
892 }
893 return 0;
894}
895
ths26edcf42007-12-09 02:25:24 +0000896static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
897 const fd_set *fds,
898 int n)
bellard31e31b82003-02-18 22:55:36 +0000899{
bellard31e31b82003-02-18 22:55:36 +0000900 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000901 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000902 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000903
ths26edcf42007-12-09 02:25:24 +0000904 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
905 if (!(target_fds = lock_user(VERIFY_WRITE,
906 target_fds_addr,
907 sizeof(abi_ulong) * nw,
908 0)))
909 return -TARGET_EFAULT;
910
911 k = 0;
912 for (i = 0; i < nw; i++) {
913 v = 0;
914 for (j = 0; j < TARGET_ABI_BITS; j++) {
915 v |= ((FD_ISSET(k, fds) != 0) << j);
916 k++;
bellard31e31b82003-02-18 22:55:36 +0000917 }
ths26edcf42007-12-09 02:25:24 +0000918 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000919 }
ths26edcf42007-12-09 02:25:24 +0000920
921 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
922
923 return 0;
bellard31e31b82003-02-18 22:55:36 +0000924}
925
bellardc596ed12003-07-13 17:32:31 +0000926#if defined(__alpha__)
927#define HOST_HZ 1024
928#else
929#define HOST_HZ 100
930#endif
931
blueswir1992f48a2007-10-14 16:27:31 +0000932static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000933{
934#if HOST_HZ == TARGET_HZ
935 return ticks;
936#else
937 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
938#endif
939}
940
bellard579a97f2007-11-11 14:26:47 +0000941static inline abi_long host_to_target_rusage(abi_ulong target_addr,
942 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000943{
pbrook53a59602006-03-25 19:31:22 +0000944 struct target_rusage *target_rusage;
945
bellard579a97f2007-11-11 14:26:47 +0000946 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
947 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200948 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
949 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
950 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
951 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
952 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
953 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
954 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
955 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
956 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
957 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
958 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
959 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
960 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
961 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
962 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
963 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
964 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
965 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000966 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000967
968 return 0;
bellardb4091862003-05-16 15:39:34 +0000969}
970
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200971static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900972{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200973 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300974 rlim_t result;
975
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200976 target_rlim_swap = tswapal(target_rlim);
977 if (target_rlim_swap == TARGET_RLIM_INFINITY)
978 return RLIM_INFINITY;
979
980 result = target_rlim_swap;
981 if (target_rlim_swap != (rlim_t)result)
982 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300983
984 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900985}
986
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200987static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900988{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200989 abi_ulong target_rlim_swap;
990 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300991
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200992 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300993 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900994 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300995 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200996 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300997
998 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900999}
1000
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03001001static inline int target_to_host_resource(int code)
1002{
1003 switch (code) {
1004 case TARGET_RLIMIT_AS:
1005 return RLIMIT_AS;
1006 case TARGET_RLIMIT_CORE:
1007 return RLIMIT_CORE;
1008 case TARGET_RLIMIT_CPU:
1009 return RLIMIT_CPU;
1010 case TARGET_RLIMIT_DATA:
1011 return RLIMIT_DATA;
1012 case TARGET_RLIMIT_FSIZE:
1013 return RLIMIT_FSIZE;
1014 case TARGET_RLIMIT_LOCKS:
1015 return RLIMIT_LOCKS;
1016 case TARGET_RLIMIT_MEMLOCK:
1017 return RLIMIT_MEMLOCK;
1018 case TARGET_RLIMIT_MSGQUEUE:
1019 return RLIMIT_MSGQUEUE;
1020 case TARGET_RLIMIT_NICE:
1021 return RLIMIT_NICE;
1022 case TARGET_RLIMIT_NOFILE:
1023 return RLIMIT_NOFILE;
1024 case TARGET_RLIMIT_NPROC:
1025 return RLIMIT_NPROC;
1026 case TARGET_RLIMIT_RSS:
1027 return RLIMIT_RSS;
1028 case TARGET_RLIMIT_RTPRIO:
1029 return RLIMIT_RTPRIO;
1030 case TARGET_RLIMIT_SIGPENDING:
1031 return RLIMIT_SIGPENDING;
1032 case TARGET_RLIMIT_STACK:
1033 return RLIMIT_STACK;
1034 default:
1035 return code;
1036 }
1037}
1038
ths788f5ec2007-12-09 02:37:05 +00001039static inline abi_long copy_from_user_timeval(struct timeval *tv,
1040 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001041{
pbrook53a59602006-03-25 19:31:22 +00001042 struct target_timeval *target_tv;
1043
ths788f5ec2007-12-09 02:37:05 +00001044 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +00001045 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001046
1047 __get_user(tv->tv_sec, &target_tv->tv_sec);
1048 __get_user(tv->tv_usec, &target_tv->tv_usec);
1049
1050 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001051
1052 return 0;
bellard31e31b82003-02-18 22:55:36 +00001053}
1054
ths788f5ec2007-12-09 02:37:05 +00001055static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1056 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +00001057{
pbrook53a59602006-03-25 19:31:22 +00001058 struct target_timeval *target_tv;
1059
ths788f5ec2007-12-09 02:37:05 +00001060 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +00001061 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001062
1063 __put_user(tv->tv_sec, &target_tv->tv_sec);
1064 __put_user(tv->tv_usec, &target_tv->tv_usec);
1065
1066 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001067
1068 return 0;
bellard31e31b82003-02-18 22:55:36 +00001069}
1070
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001071#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1072#include <mqueue.h>
1073
aurel3224e10032009-04-15 16:11:43 +00001074static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1075 abi_ulong target_mq_attr_addr)
1076{
1077 struct target_mq_attr *target_mq_attr;
1078
1079 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1080 target_mq_attr_addr, 1))
1081 return -TARGET_EFAULT;
1082
1083 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1084 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1085 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1086 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1087
1088 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1089
1090 return 0;
1091}
1092
1093static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1094 const struct mq_attr *attr)
1095{
1096 struct target_mq_attr *target_mq_attr;
1097
1098 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1099 target_mq_attr_addr, 0))
1100 return -TARGET_EFAULT;
1101
1102 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1103 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1104 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1105 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1106
1107 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1108
1109 return 0;
1110}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001111#endif
bellard31e31b82003-02-18 22:55:36 +00001112
Mike Frysinger055e0902011-06-03 17:01:49 -04001113#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001114/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001115static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001116 abi_ulong rfd_addr, abi_ulong wfd_addr,
1117 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001118{
1119 fd_set rfds, wfds, efds;
1120 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1121 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001122 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001123
Mike Frysinger055e0902011-06-03 17:01:49 -04001124 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1125 if (ret) {
1126 return ret;
pbrook53a59602006-03-25 19:31:22 +00001127 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001128 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1129 if (ret) {
1130 return ret;
pbrook53a59602006-03-25 19:31:22 +00001131 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001132 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1133 if (ret) {
1134 return ret;
pbrook53a59602006-03-25 19:31:22 +00001135 }
ths3b46e622007-09-17 08:09:54 +00001136
ths26edcf42007-12-09 02:25:24 +00001137 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001138 if (copy_from_user_timeval(&tv, target_tv_addr))
1139 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001140 tv_ptr = &tv;
1141 } else {
1142 tv_ptr = NULL;
1143 }
ths26edcf42007-12-09 02:25:24 +00001144
bellard31e31b82003-02-18 22:55:36 +00001145 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +00001146
ths26edcf42007-12-09 02:25:24 +00001147 if (!is_error(ret)) {
1148 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1149 return -TARGET_EFAULT;
1150 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1151 return -TARGET_EFAULT;
1152 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1153 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001154
ths788f5ec2007-12-09 02:37:05 +00001155 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1156 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001157 }
bellard579a97f2007-11-11 14:26:47 +00001158
bellard31e31b82003-02-18 22:55:36 +00001159 return ret;
1160}
Mike Frysinger055e0902011-06-03 17:01:49 -04001161#endif
bellard31e31b82003-02-18 22:55:36 +00001162
Riku Voipio099d6b02009-05-05 12:10:04 +03001163static abi_long do_pipe2(int host_pipe[], int flags)
1164{
1165#ifdef CONFIG_PIPE2
1166 return pipe2(host_pipe, flags);
1167#else
1168 return -ENOSYS;
1169#endif
1170}
1171
Richard Hendersonfb41a662010-05-03 10:07:52 -07001172static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1173 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001174{
1175 int host_pipe[2];
1176 abi_long ret;
1177 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1178
1179 if (is_error(ret))
1180 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001181
1182 /* Several targets have special calling conventions for the original
1183 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1184 if (!is_pipe2) {
1185#if defined(TARGET_ALPHA)
1186 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1187 return host_pipe[0];
1188#elif defined(TARGET_MIPS)
1189 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1190 return host_pipe[0];
1191#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001192 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001193 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001194#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001195 }
1196
Riku Voipio099d6b02009-05-05 12:10:04 +03001197 if (put_user_s32(host_pipe[0], pipedes)
1198 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1199 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001200 return get_errno(ret);
1201}
1202
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001203static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1204 abi_ulong target_addr,
1205 socklen_t len)
1206{
1207 struct target_ip_mreqn *target_smreqn;
1208
1209 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1210 if (!target_smreqn)
1211 return -TARGET_EFAULT;
1212 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1213 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1214 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001215 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001216 unlock_user(target_smreqn, target_addr, 0);
1217
1218 return 0;
1219}
1220
bellard579a97f2007-11-11 14:26:47 +00001221static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1222 abi_ulong target_addr,
1223 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001224{
aurel32607175e2009-04-15 16:11:59 +00001225 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1226 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001227 struct target_sockaddr *target_saddr;
1228
bellard579a97f2007-11-11 14:26:47 +00001229 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1230 if (!target_saddr)
1231 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001232
1233 sa_family = tswap16(target_saddr->sa_family);
1234
1235 /* Oops. The caller might send a incomplete sun_path; sun_path
1236 * must be terminated by \0 (see the manual page), but
1237 * unfortunately it is quite common to specify sockaddr_un
1238 * length as "strlen(x->sun_path)" while it should be
1239 * "strlen(...) + 1". We'll fix that here if needed.
1240 * Linux kernel has a similar feature.
1241 */
1242
1243 if (sa_family == AF_UNIX) {
1244 if (len < unix_maxlen && len > 0) {
1245 char *cp = (char*)target_saddr;
1246
1247 if ( cp[len-1] && !cp[len] )
1248 len++;
1249 }
1250 if (len > unix_maxlen)
1251 len = unix_maxlen;
1252 }
1253
pbrook53a59602006-03-25 19:31:22 +00001254 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001255 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001256 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001257
1258 return 0;
bellard7854b052003-03-29 17:22:23 +00001259}
1260
bellard579a97f2007-11-11 14:26:47 +00001261static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1262 struct sockaddr *addr,
1263 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001264{
pbrook53a59602006-03-25 19:31:22 +00001265 struct target_sockaddr *target_saddr;
1266
bellard579a97f2007-11-11 14:26:47 +00001267 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1268 if (!target_saddr)
1269 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001270 memcpy(target_saddr, addr, len);
1271 target_saddr->sa_family = tswap16(addr->sa_family);
1272 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001273
1274 return 0;
bellard7854b052003-03-29 17:22:23 +00001275}
1276
bellard5a4a8982007-11-11 17:39:18 +00001277static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1278 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001279{
1280 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001281 abi_long msg_controllen;
1282 abi_ulong target_cmsg_addr;
1283 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001284 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001285
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001286 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001287 if (msg_controllen < sizeof (struct target_cmsghdr))
1288 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001289 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001290 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1291 if (!target_cmsg)
1292 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001293
1294 while (cmsg && target_cmsg) {
1295 void *data = CMSG_DATA(cmsg);
1296 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1297
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001298 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001299 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1300
1301 space += CMSG_SPACE(len);
1302 if (space > msgh->msg_controllen) {
1303 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001304 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001305 break;
1306 }
1307
1308 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1309 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1310 cmsg->cmsg_len = CMSG_LEN(len);
1311
bellard3532fa72006-06-24 15:06:03 +00001312 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001313 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1314 memcpy(data, target_data, len);
1315 } else {
1316 int *fd = (int *)data;
1317 int *target_fd = (int *)target_data;
1318 int i, numfds = len / sizeof(int);
1319
1320 for (i = 0; i < numfds; i++)
1321 fd[i] = tswap32(target_fd[i]);
1322 }
1323
1324 cmsg = CMSG_NXTHDR(msgh, cmsg);
1325 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1326 }
bellard5a4a8982007-11-11 17:39:18 +00001327 unlock_user(target_cmsg, target_cmsg_addr, 0);
1328 the_end:
bellard7854b052003-03-29 17:22:23 +00001329 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001330 return 0;
bellard7854b052003-03-29 17:22:23 +00001331}
1332
bellard5a4a8982007-11-11 17:39:18 +00001333static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1334 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001335{
1336 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001337 abi_long msg_controllen;
1338 abi_ulong target_cmsg_addr;
1339 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001340 socklen_t space = 0;
1341
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001342 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001343 if (msg_controllen < sizeof (struct target_cmsghdr))
1344 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001345 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001346 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1347 if (!target_cmsg)
1348 return -TARGET_EFAULT;
1349
bellard7854b052003-03-29 17:22:23 +00001350 while (cmsg && target_cmsg) {
1351 void *data = CMSG_DATA(cmsg);
1352 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1353
1354 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1355
1356 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001357 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001358 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001359 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001360 break;
1361 }
1362
1363 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1364 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001365 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
bellard7854b052003-03-29 17:22:23 +00001366
Jing Huangaebf5bc2012-07-24 14:01:42 +00001367 if ((cmsg->cmsg_level == TARGET_SOL_SOCKET) &&
1368 (cmsg->cmsg_type == SCM_RIGHTS)) {
bellard7854b052003-03-29 17:22:23 +00001369 int *fd = (int *)data;
1370 int *target_fd = (int *)target_data;
1371 int i, numfds = len / sizeof(int);
1372
1373 for (i = 0; i < numfds; i++)
1374 target_fd[i] = tswap32(fd[i]);
Jing Huangaebf5bc2012-07-24 14:01:42 +00001375 } else if ((cmsg->cmsg_level == TARGET_SOL_SOCKET) &&
1376 (cmsg->cmsg_type == SO_TIMESTAMP) &&
1377 (len == sizeof(struct timeval))) {
1378 /* copy struct timeval to target */
1379 struct timeval *tv = (struct timeval *)data;
1380 struct target_timeval *target_tv =
1381 (struct target_timeval *)target_data;
1382
1383 target_tv->tv_sec = tswapal(tv->tv_sec);
1384 target_tv->tv_usec = tswapal(tv->tv_usec);
1385 } else {
1386 gemu_log("Unsupported ancillary data: %d/%d\n",
1387 cmsg->cmsg_level, cmsg->cmsg_type);
1388 memcpy(target_data, data, len);
bellard7854b052003-03-29 17:22:23 +00001389 }
1390
1391 cmsg = CMSG_NXTHDR(msgh, cmsg);
1392 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1393 }
bellard5a4a8982007-11-11 17:39:18 +00001394 unlock_user(target_cmsg, target_cmsg_addr, space);
1395 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001396 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001397 return 0;
bellard7854b052003-03-29 17:22:23 +00001398}
1399
ths0da46a62007-10-20 20:23:07 +00001400/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001401static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001402 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001403{
blueswir1992f48a2007-10-14 16:27:31 +00001404 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001405 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001406 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001407 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001408
bellard8853f862004-02-22 14:57:26 +00001409 switch(level) {
1410 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001411 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001412 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001413 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001414
bellard2f619692007-11-16 10:46:05 +00001415 if (get_user_u32(val, optval_addr))
1416 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001417 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1418 break;
1419 case SOL_IP:
1420 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001421 case IP_TOS:
1422 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001423 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001424 case IP_ROUTER_ALERT:
1425 case IP_RECVOPTS:
1426 case IP_RETOPTS:
1427 case IP_PKTINFO:
1428 case IP_MTU_DISCOVER:
1429 case IP_RECVERR:
1430 case IP_RECVTOS:
1431#ifdef IP_FREEBIND
1432 case IP_FREEBIND:
1433#endif
1434 case IP_MULTICAST_TTL:
1435 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001436 val = 0;
1437 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001438 if (get_user_u32(val, optval_addr))
1439 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001440 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001441 if (get_user_u8(val, optval_addr))
1442 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001443 }
1444 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1445 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001446 case IP_ADD_MEMBERSHIP:
1447 case IP_DROP_MEMBERSHIP:
1448 if (optlen < sizeof (struct target_ip_mreq) ||
1449 optlen > sizeof (struct target_ip_mreqn))
1450 return -TARGET_EINVAL;
1451
1452 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1453 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1454 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1455 break;
1456
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001457 case IP_BLOCK_SOURCE:
1458 case IP_UNBLOCK_SOURCE:
1459 case IP_ADD_SOURCE_MEMBERSHIP:
1460 case IP_DROP_SOURCE_MEMBERSHIP:
1461 if (optlen != sizeof (struct target_ip_mreq_source))
1462 return -TARGET_EINVAL;
1463
1464 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1465 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1466 unlock_user (ip_mreq_source, optval_addr, 0);
1467 break;
1468
bellard8853f862004-02-22 14:57:26 +00001469 default:
1470 goto unimplemented;
1471 }
1472 break;
Jing Huang920394d2012-07-24 13:59:23 +00001473 case SOL_RAW:
1474 switch (optname) {
1475 case ICMP_FILTER:
1476 /* struct icmp_filter takes an u32 value */
1477 if (optlen < sizeof(uint32_t)) {
1478 return -TARGET_EINVAL;
1479 }
1480
1481 if (get_user_u32(val, optval_addr)) {
1482 return -TARGET_EFAULT;
1483 }
1484 ret = get_errno(setsockopt(sockfd, level, optname,
1485 &val, sizeof(val)));
1486 break;
1487
1488 default:
1489 goto unimplemented;
1490 }
1491 break;
bellard3532fa72006-06-24 15:06:03 +00001492 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001493 switch (optname) {
1494 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001495 case TARGET_SO_DEBUG:
1496 optname = SO_DEBUG;
1497 break;
1498 case TARGET_SO_REUSEADDR:
1499 optname = SO_REUSEADDR;
1500 break;
1501 case TARGET_SO_TYPE:
1502 optname = SO_TYPE;
1503 break;
1504 case TARGET_SO_ERROR:
1505 optname = SO_ERROR;
1506 break;
1507 case TARGET_SO_DONTROUTE:
1508 optname = SO_DONTROUTE;
1509 break;
1510 case TARGET_SO_BROADCAST:
1511 optname = SO_BROADCAST;
1512 break;
1513 case TARGET_SO_SNDBUF:
1514 optname = SO_SNDBUF;
1515 break;
1516 case TARGET_SO_RCVBUF:
1517 optname = SO_RCVBUF;
1518 break;
1519 case TARGET_SO_KEEPALIVE:
1520 optname = SO_KEEPALIVE;
1521 break;
1522 case TARGET_SO_OOBINLINE:
1523 optname = SO_OOBINLINE;
1524 break;
1525 case TARGET_SO_NO_CHECK:
1526 optname = SO_NO_CHECK;
1527 break;
1528 case TARGET_SO_PRIORITY:
1529 optname = SO_PRIORITY;
1530 break;
bellard5e83e8e2005-03-01 22:32:06 +00001531#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001532 case TARGET_SO_BSDCOMPAT:
1533 optname = SO_BSDCOMPAT;
1534 break;
bellard5e83e8e2005-03-01 22:32:06 +00001535#endif
bellard3532fa72006-06-24 15:06:03 +00001536 case TARGET_SO_PASSCRED:
1537 optname = SO_PASSCRED;
1538 break;
1539 case TARGET_SO_TIMESTAMP:
1540 optname = SO_TIMESTAMP;
1541 break;
1542 case TARGET_SO_RCVLOWAT:
1543 optname = SO_RCVLOWAT;
1544 break;
1545 case TARGET_SO_RCVTIMEO:
1546 optname = SO_RCVTIMEO;
1547 break;
1548 case TARGET_SO_SNDTIMEO:
1549 optname = SO_SNDTIMEO;
1550 break;
bellard8853f862004-02-22 14:57:26 +00001551 break;
1552 default:
1553 goto unimplemented;
1554 }
bellard3532fa72006-06-24 15:06:03 +00001555 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001556 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001557
bellard2f619692007-11-16 10:46:05 +00001558 if (get_user_u32(val, optval_addr))
1559 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001560 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001561 break;
bellard7854b052003-03-29 17:22:23 +00001562 default:
bellard8853f862004-02-22 14:57:26 +00001563 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02001564 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001565 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001566 }
bellard8853f862004-02-22 14:57:26 +00001567 return ret;
bellard7854b052003-03-29 17:22:23 +00001568}
1569
ths0da46a62007-10-20 20:23:07 +00001570/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001571static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001572 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001573{
blueswir1992f48a2007-10-14 16:27:31 +00001574 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001575 int len, val;
1576 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001577
1578 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001579 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001580 level = SOL_SOCKET;
1581 switch (optname) {
1582 /* These don't just return a single integer */
1583 case TARGET_SO_LINGER:
1584 case TARGET_SO_RCVTIMEO:
1585 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001586 case TARGET_SO_PEERNAME:
1587 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02001588 case TARGET_SO_PEERCRED: {
1589 struct ucred cr;
1590 socklen_t crlen;
1591 struct target_ucred *tcr;
1592
1593 if (get_user_u32(len, optlen)) {
1594 return -TARGET_EFAULT;
1595 }
1596 if (len < 0) {
1597 return -TARGET_EINVAL;
1598 }
1599
1600 crlen = sizeof(cr);
1601 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1602 &cr, &crlen));
1603 if (ret < 0) {
1604 return ret;
1605 }
1606 if (len > crlen) {
1607 len = crlen;
1608 }
1609 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1610 return -TARGET_EFAULT;
1611 }
1612 __put_user(cr.pid, &tcr->pid);
1613 __put_user(cr.uid, &tcr->uid);
1614 __put_user(cr.gid, &tcr->gid);
1615 unlock_user_struct(tcr, optval_addr, 1);
1616 if (put_user_u32(len, optlen)) {
1617 return -TARGET_EFAULT;
1618 }
1619 break;
1620 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001621 /* Options with 'int' argument. */
1622 case TARGET_SO_DEBUG:
1623 optname = SO_DEBUG;
1624 goto int_case;
1625 case TARGET_SO_REUSEADDR:
1626 optname = SO_REUSEADDR;
1627 goto int_case;
1628 case TARGET_SO_TYPE:
1629 optname = SO_TYPE;
1630 goto int_case;
1631 case TARGET_SO_ERROR:
1632 optname = SO_ERROR;
1633 goto int_case;
1634 case TARGET_SO_DONTROUTE:
1635 optname = SO_DONTROUTE;
1636 goto int_case;
1637 case TARGET_SO_BROADCAST:
1638 optname = SO_BROADCAST;
1639 goto int_case;
1640 case TARGET_SO_SNDBUF:
1641 optname = SO_SNDBUF;
1642 goto int_case;
1643 case TARGET_SO_RCVBUF:
1644 optname = SO_RCVBUF;
1645 goto int_case;
1646 case TARGET_SO_KEEPALIVE:
1647 optname = SO_KEEPALIVE;
1648 goto int_case;
1649 case TARGET_SO_OOBINLINE:
1650 optname = SO_OOBINLINE;
1651 goto int_case;
1652 case TARGET_SO_NO_CHECK:
1653 optname = SO_NO_CHECK;
1654 goto int_case;
1655 case TARGET_SO_PRIORITY:
1656 optname = SO_PRIORITY;
1657 goto int_case;
1658#ifdef SO_BSDCOMPAT
1659 case TARGET_SO_BSDCOMPAT:
1660 optname = SO_BSDCOMPAT;
1661 goto int_case;
1662#endif
1663 case TARGET_SO_PASSCRED:
1664 optname = SO_PASSCRED;
1665 goto int_case;
1666 case TARGET_SO_TIMESTAMP:
1667 optname = SO_TIMESTAMP;
1668 goto int_case;
1669 case TARGET_SO_RCVLOWAT:
1670 optname = SO_RCVLOWAT;
1671 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001672 default:
bellard2efbe912005-07-23 15:10:20 +00001673 goto int_case;
1674 }
1675 break;
1676 case SOL_TCP:
1677 /* TCP options all take an 'int' value. */
1678 int_case:
bellard2f619692007-11-16 10:46:05 +00001679 if (get_user_u32(len, optlen))
1680 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +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);
bellard2efbe912005-07-23 15:10:20 +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 > lv)
1688 len = lv;
bellard2f619692007-11-16 10:46:05 +00001689 if (len == 4) {
1690 if (put_user_u32(val, optval_addr))
1691 return -TARGET_EFAULT;
1692 } else {
1693 if (put_user_u8(val, optval_addr))
1694 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001695 }
bellard2f619692007-11-16 10:46:05 +00001696 if (put_user_u32(len, optlen))
1697 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001698 break;
1699 case SOL_IP:
1700 switch(optname) {
1701 case IP_TOS:
1702 case IP_TTL:
1703 case IP_HDRINCL:
1704 case IP_ROUTER_ALERT:
1705 case IP_RECVOPTS:
1706 case IP_RETOPTS:
1707 case IP_PKTINFO:
1708 case IP_MTU_DISCOVER:
1709 case IP_RECVERR:
1710 case IP_RECVTOS:
1711#ifdef IP_FREEBIND
1712 case IP_FREEBIND:
1713#endif
1714 case IP_MULTICAST_TTL:
1715 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001716 if (get_user_u32(len, optlen))
1717 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001718 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001719 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001720 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001721 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1722 if (ret < 0)
1723 return ret;
bellard2efbe912005-07-23 15:10:20 +00001724 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001725 len = 1;
bellard2f619692007-11-16 10:46:05 +00001726 if (put_user_u32(len, optlen)
1727 || put_user_u8(val, optval_addr))
1728 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001729 } else {
bellard2efbe912005-07-23 15:10:20 +00001730 if (len > sizeof(int))
1731 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001732 if (put_user_u32(len, optlen)
1733 || put_user_u32(val, optval_addr))
1734 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001735 }
bellard8853f862004-02-22 14:57:26 +00001736 break;
bellard2efbe912005-07-23 15:10:20 +00001737 default:
thsc02f4992007-12-18 02:39:59 +00001738 ret = -TARGET_ENOPROTOOPT;
1739 break;
bellard8853f862004-02-22 14:57:26 +00001740 }
1741 break;
1742 default:
1743 unimplemented:
1744 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1745 level, optname);
thsc02f4992007-12-18 02:39:59 +00001746 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001747 break;
1748 }
1749 return ret;
bellard7854b052003-03-29 17:22:23 +00001750}
1751
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001752static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1753 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001754{
1755 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001756 struct iovec *vec;
1757 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00001758 int i;
pbrook53a59602006-03-25 19:31:22 +00001759
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001760 if (count == 0) {
1761 errno = 0;
1762 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001763 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001764 if (count > IOV_MAX) {
1765 errno = EINVAL;
1766 return NULL;
1767 }
1768
1769 vec = calloc(count, sizeof(struct iovec));
1770 if (vec == NULL) {
1771 errno = ENOMEM;
1772 return NULL;
1773 }
1774
1775 target_vec = lock_user(VERIFY_READ, target_addr,
1776 count * sizeof(struct target_iovec), 1);
1777 if (target_vec == NULL) {
1778 errno = EFAULT;
1779 goto fail2;
1780 }
1781
1782 /* ??? If host page size > target page size, this will result in a
1783 value larger than what we can actually support. */
1784 max_len = 0x7fffffff & TARGET_PAGE_MASK;
1785 total_len = 0;
1786
1787 for (i = 0; i < count; i++) {
1788 abi_ulong base = tswapal(target_vec[i].iov_base);
1789 abi_long len = tswapal(target_vec[i].iov_len);
1790
1791 if (len < 0) {
1792 errno = EINVAL;
1793 goto fail;
1794 } else if (len == 0) {
1795 /* Zero length pointer is ignored. */
1796 vec[i].iov_base = 0;
1797 } else {
1798 vec[i].iov_base = lock_user(type, base, len, copy);
1799 if (!vec[i].iov_base) {
1800 errno = EFAULT;
1801 goto fail;
1802 }
1803 if (len > max_len - total_len) {
1804 len = max_len - total_len;
1805 }
1806 }
1807 vec[i].iov_len = len;
1808 total_len += len;
1809 }
1810
1811 unlock_user(target_vec, target_addr, 0);
1812 return vec;
1813
1814 fail:
1815 free(vec);
1816 fail2:
1817 unlock_user(target_vec, target_addr, 0);
1818 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001819}
1820
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001821static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1822 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001823{
1824 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00001825 int i;
1826
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001827 target_vec = lock_user(VERIFY_READ, target_addr,
1828 count * sizeof(struct target_iovec), 1);
1829 if (target_vec) {
1830 for (i = 0; i < count; i++) {
1831 abi_ulong base = tswapal(target_vec[i].iov_base);
1832 abi_long len = tswapal(target_vec[i].iov_base);
1833 if (len < 0) {
1834 break;
1835 }
balrogd732dcb2008-10-28 10:21:03 +00001836 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1837 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001838 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00001839 }
bellard579a97f2007-11-11 14:26:47 +00001840
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001841 free(vec);
pbrook53a59602006-03-25 19:31:22 +00001842}
1843
ths0da46a62007-10-20 20:23:07 +00001844/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001845static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001846{
1847#if defined(TARGET_MIPS)
1848 switch(type) {
1849 case TARGET_SOCK_DGRAM:
1850 type = SOCK_DGRAM;
1851 break;
1852 case TARGET_SOCK_STREAM:
1853 type = SOCK_STREAM;
1854 break;
1855 case TARGET_SOCK_RAW:
1856 type = SOCK_RAW;
1857 break;
1858 case TARGET_SOCK_RDM:
1859 type = SOCK_RDM;
1860 break;
1861 case TARGET_SOCK_SEQPACKET:
1862 type = SOCK_SEQPACKET;
1863 break;
1864 case TARGET_SOCK_PACKET:
1865 type = SOCK_PACKET;
1866 break;
1867 }
1868#endif
balrog12bc92a2007-10-30 21:06:14 +00001869 if (domain == PF_NETLINK)
1870 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001871 return get_errno(socket(domain, type, protocol));
1872}
1873
ths0da46a62007-10-20 20:23:07 +00001874/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001875static abi_long do_bind(int sockfd, abi_ulong target_addr,
1876 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001877{
aurel328f7aeaf2009-01-30 19:47:57 +00001878 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001879 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001880
Blue Swirl38724252010-09-18 05:53:14 +00001881 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001882 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001883 }
aurel328f7aeaf2009-01-30 19:47:57 +00001884
aurel32607175e2009-04-15 16:11:59 +00001885 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001886
Arnaud Patard917507b2009-06-19 10:44:45 +03001887 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1888 if (ret)
1889 return ret;
1890
bellard3532fa72006-06-24 15:06:03 +00001891 return get_errno(bind(sockfd, addr, addrlen));
1892}
1893
ths0da46a62007-10-20 20:23:07 +00001894/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001895static abi_long do_connect(int sockfd, abi_ulong target_addr,
1896 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001897{
aurel328f7aeaf2009-01-30 19:47:57 +00001898 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001899 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001900
Blue Swirl38724252010-09-18 05:53:14 +00001901 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001902 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001903 }
aurel328f7aeaf2009-01-30 19:47:57 +00001904
1905 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001906
Arnaud Patard917507b2009-06-19 10:44:45 +03001907 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1908 if (ret)
1909 return ret;
1910
bellard3532fa72006-06-24 15:06:03 +00001911 return get_errno(connect(sockfd, addr, addrlen));
1912}
1913
ths0da46a62007-10-20 20:23:07 +00001914/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001915static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1916 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001917{
balrog6de645c2008-10-28 10:26:29 +00001918 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001919 struct target_msghdr *msgp;
1920 struct msghdr msg;
1921 int count;
1922 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001923 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001924
bellard579a97f2007-11-11 14:26:47 +00001925 /* FIXME */
1926 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1927 msgp,
1928 target_msg,
1929 send ? 1 : 0))
1930 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001931 if (msgp->msg_name) {
1932 msg.msg_namelen = tswap32(msgp->msg_namelen);
1933 msg.msg_name = alloca(msg.msg_namelen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001934 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001935 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001936 if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001937 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03001938 }
bellard3532fa72006-06-24 15:06:03 +00001939 } else {
1940 msg.msg_name = NULL;
1941 msg.msg_namelen = 0;
1942 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001943 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00001944 msg.msg_control = alloca(msg.msg_controllen);
1945 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001946
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001947 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001948 target_vec = tswapal(msgp->msg_iov);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001949 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
1950 target_vec, count, send);
1951 if (vec == NULL) {
1952 ret = -host_to_target_errno(errno);
1953 goto out2;
1954 }
bellard3532fa72006-06-24 15:06:03 +00001955 msg.msg_iovlen = count;
1956 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001957
bellard3532fa72006-06-24 15:06:03 +00001958 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001959 ret = target_to_host_cmsg(&msg, msgp);
1960 if (ret == 0)
1961 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001962 } else {
1963 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001964 if (!is_error(ret)) {
1965 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001966 ret = host_to_target_cmsg(msgp, &msg);
Jing Huangca619062012-07-24 13:58:02 +00001967 if (!is_error(ret)) {
1968 msgp->msg_namelen = tswap32(msg.msg_namelen);
1969 if (msg.msg_name != NULL) {
1970 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
1971 msg.msg_name, msg.msg_namelen);
1972 if (ret) {
1973 goto out;
1974 }
1975 }
1976
balrog6de645c2008-10-28 10:26:29 +00001977 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00001978 }
balrog6de645c2008-10-28 10:26:29 +00001979 }
bellard3532fa72006-06-24 15:06:03 +00001980 }
Jing Huangca619062012-07-24 13:58:02 +00001981
1982out:
bellard3532fa72006-06-24 15:06:03 +00001983 unlock_iovec(vec, target_vec, count, !send);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001984out2:
bellard579a97f2007-11-11 14:26:47 +00001985 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001986 return ret;
1987}
1988
ths0da46a62007-10-20 20:23:07 +00001989/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001990static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001991 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001992{
bellard2f619692007-11-16 10:46:05 +00001993 socklen_t addrlen;
1994 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001995 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001996
Arnaud Patard917507b2009-06-19 10:44:45 +03001997 if (target_addr == 0)
1998 return get_errno(accept(fd, NULL, NULL));
1999
2000 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00002001 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03002002 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00002003
Blue Swirl38724252010-09-18 05:53:14 +00002004 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002005 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002006 }
aurel328f7aeaf2009-01-30 19:47:57 +00002007
Arnaud Patard917507b2009-06-19 10:44:45 +03002008 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2009 return -TARGET_EINVAL;
2010
bellard2f619692007-11-16 10:46:05 +00002011 addr = alloca(addrlen);
2012
pbrook1be9e1d2006-11-19 15:26:04 +00002013 ret = get_errno(accept(fd, addr, &addrlen));
2014 if (!is_error(ret)) {
2015 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002016 if (put_user_u32(addrlen, target_addrlen_addr))
2017 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002018 }
2019 return ret;
2020}
2021
ths0da46a62007-10-20 20:23:07 +00002022/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002023static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002024 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002025{
bellard2f619692007-11-16 10:46:05 +00002026 socklen_t addrlen;
2027 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002028 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002029
bellard2f619692007-11-16 10:46:05 +00002030 if (get_user_u32(addrlen, target_addrlen_addr))
2031 return -TARGET_EFAULT;
2032
Blue Swirl38724252010-09-18 05:53:14 +00002033 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002034 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002035 }
aurel328f7aeaf2009-01-30 19:47:57 +00002036
Arnaud Patard917507b2009-06-19 10:44:45 +03002037 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2038 return -TARGET_EFAULT;
2039
bellard2f619692007-11-16 10:46:05 +00002040 addr = alloca(addrlen);
2041
pbrook1be9e1d2006-11-19 15:26:04 +00002042 ret = get_errno(getpeername(fd, addr, &addrlen));
2043 if (!is_error(ret)) {
2044 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002045 if (put_user_u32(addrlen, target_addrlen_addr))
2046 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002047 }
2048 return ret;
2049}
2050
ths0da46a62007-10-20 20:23:07 +00002051/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002052static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002053 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002054{
bellard2f619692007-11-16 10:46:05 +00002055 socklen_t addrlen;
2056 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002057 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002058
bellard2f619692007-11-16 10:46:05 +00002059 if (get_user_u32(addrlen, target_addrlen_addr))
2060 return -TARGET_EFAULT;
2061
Blue Swirl38724252010-09-18 05:53:14 +00002062 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002063 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002064 }
aurel328f7aeaf2009-01-30 19:47:57 +00002065
Arnaud Patard917507b2009-06-19 10:44:45 +03002066 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2067 return -TARGET_EFAULT;
2068
bellard2f619692007-11-16 10:46:05 +00002069 addr = alloca(addrlen);
2070
pbrook1be9e1d2006-11-19 15:26:04 +00002071 ret = get_errno(getsockname(fd, addr, &addrlen));
2072 if (!is_error(ret)) {
2073 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002074 if (put_user_u32(addrlen, target_addrlen_addr))
2075 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002076 }
2077 return ret;
2078}
2079
ths0da46a62007-10-20 20:23:07 +00002080/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002081static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00002082 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002083{
2084 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00002085 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002086
2087 ret = get_errno(socketpair(domain, type, protocol, tab));
2088 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00002089 if (put_user_s32(tab[0], target_tab_addr)
2090 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2091 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002092 }
2093 return ret;
2094}
2095
ths0da46a62007-10-20 20:23:07 +00002096/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002097static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2098 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002099{
2100 void *addr;
2101 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002102 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002103
Blue Swirl38724252010-09-18 05:53:14 +00002104 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002105 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002106 }
aurel328f7aeaf2009-01-30 19:47:57 +00002107
bellard579a97f2007-11-11 14:26:47 +00002108 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2109 if (!host_msg)
2110 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002111 if (target_addr) {
2112 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002113 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2114 if (ret) {
2115 unlock_user(host_msg, msg, 0);
2116 return ret;
2117 }
pbrook1be9e1d2006-11-19 15:26:04 +00002118 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2119 } else {
2120 ret = get_errno(send(fd, host_msg, len, flags));
2121 }
2122 unlock_user(host_msg, msg, 0);
2123 return ret;
2124}
2125
ths0da46a62007-10-20 20:23:07 +00002126/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002127static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2128 abi_ulong target_addr,
2129 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002130{
2131 socklen_t addrlen;
2132 void *addr;
2133 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002134 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002135
bellard579a97f2007-11-11 14:26:47 +00002136 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2137 if (!host_msg)
2138 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002139 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00002140 if (get_user_u32(addrlen, target_addrlen)) {
2141 ret = -TARGET_EFAULT;
2142 goto fail;
2143 }
Blue Swirl38724252010-09-18 05:53:14 +00002144 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002145 ret = -TARGET_EINVAL;
2146 goto fail;
2147 }
pbrook1be9e1d2006-11-19 15:26:04 +00002148 addr = alloca(addrlen);
2149 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2150 } else {
2151 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00002152 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002153 }
2154 if (!is_error(ret)) {
2155 if (target_addr) {
2156 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002157 if (put_user_u32(addrlen, target_addrlen)) {
2158 ret = -TARGET_EFAULT;
2159 goto fail;
2160 }
pbrook1be9e1d2006-11-19 15:26:04 +00002161 }
2162 unlock_user(host_msg, msg, len);
2163 } else {
bellard2f619692007-11-16 10:46:05 +00002164fail:
pbrook1be9e1d2006-11-19 15:26:04 +00002165 unlock_user(host_msg, msg, 0);
2166 }
2167 return ret;
2168}
2169
j_mayer32407102007-09-26 23:01:49 +00002170#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00002171/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002172static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00002173{
blueswir1992f48a2007-10-14 16:27:31 +00002174 abi_long ret;
2175 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00002176
2177 switch(num) {
2178 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00002179 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002180 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002181
Ulrich Hecht98818182009-07-03 17:09:28 +02002182 if (get_user_ual(domain, vptr)
2183 || get_user_ual(type, vptr + n)
2184 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002185 return -TARGET_EFAULT;
2186
bellard3532fa72006-06-24 15:06:03 +00002187 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00002188 }
bellard31e31b82003-02-18 22:55:36 +00002189 break;
2190 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00002191 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002192 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002193 abi_ulong target_addr;
2194 socklen_t addrlen;
2195
Ulrich Hecht98818182009-07-03 17:09:28 +02002196 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002197 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002198 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002199 return -TARGET_EFAULT;
2200
bellard3532fa72006-06-24 15:06:03 +00002201 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002202 }
bellard31e31b82003-02-18 22:55:36 +00002203 break;
2204 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00002205 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002206 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002207 abi_ulong target_addr;
2208 socklen_t addrlen;
2209
Ulrich Hecht98818182009-07-03 17:09:28 +02002210 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002211 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002212 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002213 return -TARGET_EFAULT;
2214
bellard3532fa72006-06-24 15:06:03 +00002215 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002216 }
bellard31e31b82003-02-18 22:55:36 +00002217 break;
2218 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00002219 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002220 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00002221
Ulrich Hecht98818182009-07-03 17:09:28 +02002222 if (get_user_ual(sockfd, vptr)
2223 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002224 return -TARGET_EFAULT;
2225
bellard7854b052003-03-29 17:22:23 +00002226 ret = get_errno(listen(sockfd, backlog));
2227 }
bellard31e31b82003-02-18 22:55:36 +00002228 break;
2229 case SOCKOP_accept:
2230 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002231 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002232 abi_ulong target_addr, target_addrlen;
2233
Ulrich Hecht98818182009-07-03 17:09:28 +02002234 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002235 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002236 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002237 return -TARGET_EFAULT;
2238
pbrook1be9e1d2006-11-19 15:26:04 +00002239 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002240 }
2241 break;
2242 case SOCKOP_getsockname:
2243 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002244 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002245 abi_ulong target_addr, target_addrlen;
2246
Ulrich Hecht98818182009-07-03 17:09:28 +02002247 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002248 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002249 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002250 return -TARGET_EFAULT;
2251
pbrook1be9e1d2006-11-19 15:26:04 +00002252 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002253 }
2254 break;
2255 case SOCKOP_getpeername:
2256 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002257 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002258 abi_ulong target_addr, target_addrlen;
2259
Ulrich Hecht98818182009-07-03 17:09:28 +02002260 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002261 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002262 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002263 return -TARGET_EFAULT;
2264
pbrook1be9e1d2006-11-19 15:26:04 +00002265 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002266 }
2267 break;
2268 case SOCKOP_socketpair:
2269 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002270 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002271 abi_ulong tab;
2272
Ulrich Hecht98818182009-07-03 17:09:28 +02002273 if (get_user_ual(domain, vptr)
2274 || get_user_ual(type, vptr + n)
2275 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002276 || get_user_ual(tab, vptr + 3 * n))
2277 return -TARGET_EFAULT;
2278
pbrook1be9e1d2006-11-19 15:26:04 +00002279 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00002280 }
2281 break;
2282 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00002283 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002284 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002285 abi_ulong msg;
2286 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002287 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002288
Ulrich Hecht98818182009-07-03 17:09:28 +02002289 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002290 || get_user_ual(msg, vptr + n)
2291 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002292 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002293 return -TARGET_EFAULT;
2294
pbrook1be9e1d2006-11-19 15:26:04 +00002295 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002296 }
bellard31e31b82003-02-18 22:55:36 +00002297 break;
2298 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00002299 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002300 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002301 abi_ulong msg;
2302 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002303 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002304
Ulrich Hecht98818182009-07-03 17:09:28 +02002305 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002306 || get_user_ual(msg, vptr + n)
2307 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002308 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002309 return -TARGET_EFAULT;
2310
pbrook1be9e1d2006-11-19 15:26:04 +00002311 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002312 }
bellard31e31b82003-02-18 22:55:36 +00002313 break;
2314 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00002315 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002316 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002317 abi_ulong msg;
2318 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002319 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002320 abi_ulong addr;
2321 socklen_t addrlen;
2322
Ulrich Hecht98818182009-07-03 17:09:28 +02002323 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002324 || get_user_ual(msg, vptr + n)
2325 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002326 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002327 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002328 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002329 return -TARGET_EFAULT;
2330
pbrook1be9e1d2006-11-19 15:26:04 +00002331 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002332 }
bellard31e31b82003-02-18 22:55:36 +00002333 break;
2334 case SOCKOP_recvfrom:
2335 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002336 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002337 abi_ulong msg;
2338 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002339 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002340 abi_ulong addr;
2341 socklen_t addrlen;
2342
Ulrich Hecht98818182009-07-03 17:09:28 +02002343 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002344 || get_user_ual(msg, vptr + n)
2345 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002346 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002347 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002348 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002349 return -TARGET_EFAULT;
2350
pbrook1be9e1d2006-11-19 15:26:04 +00002351 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00002352 }
2353 break;
2354 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00002355 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002356 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00002357
Ulrich Hecht98818182009-07-03 17:09:28 +02002358 if (get_user_ual(sockfd, vptr)
2359 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002360 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002361
2362 ret = get_errno(shutdown(sockfd, how));
2363 }
bellard31e31b82003-02-18 22:55:36 +00002364 break;
2365 case SOCKOP_sendmsg:
2366 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002367 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002368 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002369 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002370 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002371
Ulrich Hecht98818182009-07-03 17:09:28 +02002372 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002373 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002374 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002375 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002376
ths5fafdf22007-09-16 21:08:06 +00002377 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002378 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002379 }
2380 break;
bellard31e31b82003-02-18 22:55:36 +00002381 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002382 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002383 abi_ulong sockfd;
2384 abi_ulong level;
2385 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002386 abi_ulong optval;
2387 socklen_t optlen;
2388
Ulrich Hecht98818182009-07-03 17:09:28 +02002389 if (get_user_ual(sockfd, vptr)
2390 || get_user_ual(level, vptr + n)
2391 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002392 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002393 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002394 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002395
2396 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2397 }
2398 break;
bellard31e31b82003-02-18 22:55:36 +00002399 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002400 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002401 abi_ulong sockfd;
2402 abi_ulong level;
2403 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002404 abi_ulong optval;
2405 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002406
Ulrich Hecht98818182009-07-03 17:09:28 +02002407 if (get_user_ual(sockfd, vptr)
2408 || get_user_ual(level, vptr + n)
2409 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002410 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002411 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002412 return -TARGET_EFAULT;
2413
2414 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002415 }
2416 break;
bellard31e31b82003-02-18 22:55:36 +00002417 default:
2418 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002419 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002420 break;
2421 }
2422 return ret;
2423}
j_mayer32407102007-09-26 23:01:49 +00002424#endif
bellard31e31b82003-02-18 22:55:36 +00002425
bellard8853f862004-02-22 14:57:26 +00002426#define N_SHM_REGIONS 32
2427
2428static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002429 abi_ulong start;
2430 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002431} shm_regions[N_SHM_REGIONS];
2432
ths3eb6b042007-06-03 14:26:27 +00002433struct target_ipc_perm
2434{
blueswir1992f48a2007-10-14 16:27:31 +00002435 abi_long __key;
2436 abi_ulong uid;
2437 abi_ulong gid;
2438 abi_ulong cuid;
2439 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002440 unsigned short int mode;
2441 unsigned short int __pad1;
2442 unsigned short int __seq;
2443 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002444 abi_ulong __unused1;
2445 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002446};
2447
2448struct target_semid_ds
2449{
2450 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002451 abi_ulong sem_otime;
2452 abi_ulong __unused1;
2453 abi_ulong sem_ctime;
2454 abi_ulong __unused2;
2455 abi_ulong sem_nsems;
2456 abi_ulong __unused3;
2457 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002458};
2459
bellard579a97f2007-11-11 14:26:47 +00002460static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2461 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002462{
2463 struct target_ipc_perm *target_ip;
2464 struct target_semid_ds *target_sd;
2465
bellard579a97f2007-11-11 14:26:47 +00002466 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2467 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002468 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002469 host_ip->__key = tswapal(target_ip->__key);
2470 host_ip->uid = tswapal(target_ip->uid);
2471 host_ip->gid = tswapal(target_ip->gid);
2472 host_ip->cuid = tswapal(target_ip->cuid);
2473 host_ip->cgid = tswapal(target_ip->cgid);
2474 host_ip->mode = tswap16(target_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002475 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002476 return 0;
ths3eb6b042007-06-03 14:26:27 +00002477}
2478
bellard579a97f2007-11-11 14:26:47 +00002479static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2480 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002481{
2482 struct target_ipc_perm *target_ip;
2483 struct target_semid_ds *target_sd;
2484
bellard579a97f2007-11-11 14:26:47 +00002485 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2486 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002487 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002488 target_ip->__key = tswapal(host_ip->__key);
2489 target_ip->uid = tswapal(host_ip->uid);
2490 target_ip->gid = tswapal(host_ip->gid);
2491 target_ip->cuid = tswapal(host_ip->cuid);
2492 target_ip->cgid = tswapal(host_ip->cgid);
2493 target_ip->mode = tswap16(host_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002494 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002495 return 0;
ths3eb6b042007-06-03 14:26:27 +00002496}
2497
bellard579a97f2007-11-11 14:26:47 +00002498static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2499 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002500{
2501 struct target_semid_ds *target_sd;
2502
bellard579a97f2007-11-11 14:26:47 +00002503 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2504 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002505 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2506 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002507 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2508 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2509 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002510 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002511 return 0;
ths3eb6b042007-06-03 14:26:27 +00002512}
2513
bellard579a97f2007-11-11 14:26:47 +00002514static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2515 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002516{
2517 struct target_semid_ds *target_sd;
2518
bellard579a97f2007-11-11 14:26:47 +00002519 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2520 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002521 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08002522 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002523 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2524 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2525 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002526 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002527 return 0;
ths3eb6b042007-06-03 14:26:27 +00002528}
2529
aurel32e5289082009-04-18 16:16:12 +00002530struct target_seminfo {
2531 int semmap;
2532 int semmni;
2533 int semmns;
2534 int semmnu;
2535 int semmsl;
2536 int semopm;
2537 int semume;
2538 int semusz;
2539 int semvmx;
2540 int semaem;
2541};
2542
2543static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2544 struct seminfo *host_seminfo)
2545{
2546 struct target_seminfo *target_seminfo;
2547 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2548 return -TARGET_EFAULT;
2549 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2550 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2551 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2552 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2553 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2554 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2555 __put_user(host_seminfo->semume, &target_seminfo->semume);
2556 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2557 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2558 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2559 unlock_user_struct(target_seminfo, target_addr, 1);
2560 return 0;
2561}
2562
thsfa294812007-02-02 22:05:00 +00002563union semun {
2564 int val;
ths3eb6b042007-06-03 14:26:27 +00002565 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002566 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002567 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002568};
2569
ths3eb6b042007-06-03 14:26:27 +00002570union target_semun {
2571 int val;
aurel32e5289082009-04-18 16:16:12 +00002572 abi_ulong buf;
2573 abi_ulong array;
2574 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002575};
2576
aurel32e5289082009-04-18 16:16:12 +00002577static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2578 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002579{
aurel32e5289082009-04-18 16:16:12 +00002580 int nsems;
2581 unsigned short *array;
2582 union semun semun;
2583 struct semid_ds semid_ds;
2584 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002585
aurel32e5289082009-04-18 16:16:12 +00002586 semun.buf = &semid_ds;
2587
2588 ret = semctl(semid, 0, IPC_STAT, semun);
2589 if (ret == -1)
2590 return get_errno(ret);
2591
2592 nsems = semid_ds.sem_nsems;
2593
2594 *host_array = malloc(nsems*sizeof(unsigned short));
2595 array = lock_user(VERIFY_READ, target_addr,
2596 nsems*sizeof(unsigned short), 1);
2597 if (!array)
2598 return -TARGET_EFAULT;
2599
2600 for(i=0; i<nsems; i++) {
2601 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002602 }
aurel32e5289082009-04-18 16:16:12 +00002603 unlock_user(array, target_addr, 0);
2604
bellard579a97f2007-11-11 14:26:47 +00002605 return 0;
ths3eb6b042007-06-03 14:26:27 +00002606}
2607
aurel32e5289082009-04-18 16:16:12 +00002608static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2609 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002610{
aurel32e5289082009-04-18 16:16:12 +00002611 int nsems;
2612 unsigned short *array;
2613 union semun semun;
2614 struct semid_ds semid_ds;
2615 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002616
aurel32e5289082009-04-18 16:16:12 +00002617 semun.buf = &semid_ds;
2618
2619 ret = semctl(semid, 0, IPC_STAT, semun);
2620 if (ret == -1)
2621 return get_errno(ret);
2622
2623 nsems = semid_ds.sem_nsems;
2624
2625 array = lock_user(VERIFY_WRITE, target_addr,
2626 nsems*sizeof(unsigned short), 0);
2627 if (!array)
2628 return -TARGET_EFAULT;
2629
2630 for(i=0; i<nsems; i++) {
2631 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002632 }
aurel32e5289082009-04-18 16:16:12 +00002633 free(*host_array);
2634 unlock_user(array, target_addr, 1);
2635
bellard579a97f2007-11-11 14:26:47 +00002636 return 0;
ths3eb6b042007-06-03 14:26:27 +00002637}
2638
aurel32e5289082009-04-18 16:16:12 +00002639static inline abi_long do_semctl(int semid, int semnum, int cmd,
2640 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002641{
2642 union semun arg;
2643 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302644 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002645 struct seminfo seminfo;
2646 abi_long ret = -TARGET_EINVAL;
2647 abi_long err;
2648 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002649
2650 switch( cmd ) {
2651 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002652 case SETVAL:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002653 arg.val = tswap32(target_su.val);
aurel32e5289082009-04-18 16:16:12 +00002654 ret = get_errno(semctl(semid, semnum, cmd, arg));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002655 target_su.val = tswap32(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002656 break;
2657 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002658 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002659 err = target_to_host_semarray(semid, &array, target_su.array);
2660 if (err)
2661 return err;
2662 arg.array = array;
2663 ret = get_errno(semctl(semid, semnum, cmd, arg));
2664 err = host_to_target_semarray(semid, target_su.array, &array);
2665 if (err)
2666 return err;
ths3eb6b042007-06-03 14:26:27 +00002667 break;
2668 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002669 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002670 case SEM_STAT:
2671 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2672 if (err)
2673 return err;
2674 arg.buf = &dsarg;
2675 ret = get_errno(semctl(semid, semnum, cmd, arg));
2676 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2677 if (err)
2678 return err;
ths3eb6b042007-06-03 14:26:27 +00002679 break;
aurel32e5289082009-04-18 16:16:12 +00002680 case IPC_INFO:
2681 case SEM_INFO:
2682 arg.__buf = &seminfo;
2683 ret = get_errno(semctl(semid, semnum, cmd, arg));
2684 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2685 if (err)
2686 return err;
2687 break;
2688 case IPC_RMID:
2689 case GETPID:
2690 case GETNCNT:
2691 case GETZCNT:
2692 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2693 break;
ths3eb6b042007-06-03 14:26:27 +00002694 }
2695
2696 return ret;
2697}
2698
aurel32e5289082009-04-18 16:16:12 +00002699struct target_sembuf {
2700 unsigned short sem_num;
2701 short sem_op;
2702 short sem_flg;
2703};
2704
2705static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2706 abi_ulong target_addr,
2707 unsigned nsops)
2708{
2709 struct target_sembuf *target_sembuf;
2710 int i;
2711
2712 target_sembuf = lock_user(VERIFY_READ, target_addr,
2713 nsops*sizeof(struct target_sembuf), 1);
2714 if (!target_sembuf)
2715 return -TARGET_EFAULT;
2716
2717 for(i=0; i<nsops; i++) {
2718 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2719 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2720 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2721 }
2722
2723 unlock_user(target_sembuf, target_addr, 0);
2724
2725 return 0;
2726}
2727
2728static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2729{
2730 struct sembuf sops[nsops];
2731
2732 if (target_to_host_sembuf(sops, ptr, nsops))
2733 return -TARGET_EFAULT;
2734
2735 return semop(semid, sops, nsops);
2736}
2737
ths1bc012f2007-06-03 14:27:49 +00002738struct target_msqid_ds
2739{
aurel321c54ff92008-10-13 21:08:44 +00002740 struct target_ipc_perm msg_perm;
2741 abi_ulong msg_stime;
2742#if TARGET_ABI_BITS == 32
2743 abi_ulong __unused1;
2744#endif
2745 abi_ulong msg_rtime;
2746#if TARGET_ABI_BITS == 32
2747 abi_ulong __unused2;
2748#endif
2749 abi_ulong msg_ctime;
2750#if TARGET_ABI_BITS == 32
2751 abi_ulong __unused3;
2752#endif
2753 abi_ulong __msg_cbytes;
2754 abi_ulong msg_qnum;
2755 abi_ulong msg_qbytes;
2756 abi_ulong msg_lspid;
2757 abi_ulong msg_lrpid;
2758 abi_ulong __unused4;
2759 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002760};
2761
bellard579a97f2007-11-11 14:26:47 +00002762static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2763 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002764{
2765 struct target_msqid_ds *target_md;
2766
bellard579a97f2007-11-11 14:26:47 +00002767 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2768 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002769 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2770 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002771 host_md->msg_stime = tswapal(target_md->msg_stime);
2772 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2773 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2774 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2775 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2776 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2777 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2778 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002779 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002780 return 0;
ths1bc012f2007-06-03 14:27:49 +00002781}
2782
bellard579a97f2007-11-11 14:26:47 +00002783static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2784 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002785{
2786 struct target_msqid_ds *target_md;
2787
bellard579a97f2007-11-11 14:26:47 +00002788 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2789 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002790 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2791 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002792 target_md->msg_stime = tswapal(host_md->msg_stime);
2793 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2794 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2795 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2796 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2797 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2798 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2799 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002800 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002801 return 0;
ths1bc012f2007-06-03 14:27:49 +00002802}
2803
aurel321c54ff92008-10-13 21:08:44 +00002804struct target_msginfo {
2805 int msgpool;
2806 int msgmap;
2807 int msgmax;
2808 int msgmnb;
2809 int msgmni;
2810 int msgssz;
2811 int msgtql;
2812 unsigned short int msgseg;
2813};
2814
2815static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2816 struct msginfo *host_msginfo)
2817{
2818 struct target_msginfo *target_msginfo;
2819 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2820 return -TARGET_EFAULT;
2821 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2822 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2823 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2824 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2825 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2826 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2827 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2828 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2829 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002830 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002831}
2832
2833static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002834{
2835 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002836 struct msginfo msginfo;
2837 abi_long ret = -TARGET_EINVAL;
2838
2839 cmd &= 0xff;
2840
2841 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002842 case IPC_STAT:
2843 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002844 case MSG_STAT:
2845 if (target_to_host_msqid_ds(&dsarg,ptr))
2846 return -TARGET_EFAULT;
2847 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2848 if (host_to_target_msqid_ds(ptr,&dsarg))
2849 return -TARGET_EFAULT;
2850 break;
2851 case IPC_RMID:
2852 ret = get_errno(msgctl(msgid, cmd, NULL));
2853 break;
2854 case IPC_INFO:
2855 case MSG_INFO:
2856 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2857 if (host_to_target_msginfo(ptr, &msginfo))
2858 return -TARGET_EFAULT;
2859 break;
ths1bc012f2007-06-03 14:27:49 +00002860 }
aurel321c54ff92008-10-13 21:08:44 +00002861
ths1bc012f2007-06-03 14:27:49 +00002862 return ret;
2863}
2864
2865struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002866 abi_long mtype;
2867 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002868};
2869
blueswir1992f48a2007-10-14 16:27:31 +00002870static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2871 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002872{
2873 struct target_msgbuf *target_mb;
2874 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002875 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002876
bellard579a97f2007-11-11 14:26:47 +00002877 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2878 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002879 host_mb = malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002880 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00002881 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002882 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2883 free(host_mb);
2884 unlock_user_struct(target_mb, msgp, 0);
2885
2886 return ret;
2887}
2888
blueswir1992f48a2007-10-14 16:27:31 +00002889static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002890 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002891 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002892{
2893 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002894 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002895 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002896 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002897
bellard579a97f2007-11-11 14:26:47 +00002898 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2899 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002900
Jim Meyering0d07fe42012-08-22 13:55:53 +02002901 host_mb = g_malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002902 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapal(msgtyp), msgflg));
aurel321c54ff92008-10-13 21:08:44 +00002903
bellard579a97f2007-11-11 14:26:47 +00002904 if (ret > 0) {
2905 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2906 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2907 if (!target_mtext) {
2908 ret = -TARGET_EFAULT;
2909 goto end;
2910 }
aurel321c54ff92008-10-13 21:08:44 +00002911 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002912 unlock_user(target_mtext, target_mtext_addr, ret);
2913 }
aurel321c54ff92008-10-13 21:08:44 +00002914
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002915 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00002916
bellard579a97f2007-11-11 14:26:47 +00002917end:
2918 if (target_mb)
2919 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02002920 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002921 return ret;
2922}
2923
Riku Voipio88a8c982009-04-03 10:42:00 +03002924struct target_shmid_ds
2925{
2926 struct target_ipc_perm shm_perm;
2927 abi_ulong shm_segsz;
2928 abi_ulong shm_atime;
2929#if TARGET_ABI_BITS == 32
2930 abi_ulong __unused1;
2931#endif
2932 abi_ulong shm_dtime;
2933#if TARGET_ABI_BITS == 32
2934 abi_ulong __unused2;
2935#endif
2936 abi_ulong shm_ctime;
2937#if TARGET_ABI_BITS == 32
2938 abi_ulong __unused3;
2939#endif
2940 int shm_cpid;
2941 int shm_lpid;
2942 abi_ulong shm_nattch;
2943 unsigned long int __unused4;
2944 unsigned long int __unused5;
2945};
2946
2947static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2948 abi_ulong target_addr)
2949{
2950 struct target_shmid_ds *target_sd;
2951
2952 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2953 return -TARGET_EFAULT;
2954 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2955 return -TARGET_EFAULT;
2956 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2957 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2958 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2959 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2960 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2961 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2962 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2963 unlock_user_struct(target_sd, target_addr, 0);
2964 return 0;
2965}
2966
2967static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2968 struct shmid_ds *host_sd)
2969{
2970 struct target_shmid_ds *target_sd;
2971
2972 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2973 return -TARGET_EFAULT;
2974 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2975 return -TARGET_EFAULT;
2976 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2977 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2978 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2979 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2980 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2981 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2982 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2983 unlock_user_struct(target_sd, target_addr, 1);
2984 return 0;
2985}
2986
2987struct target_shminfo {
2988 abi_ulong shmmax;
2989 abi_ulong shmmin;
2990 abi_ulong shmmni;
2991 abi_ulong shmseg;
2992 abi_ulong shmall;
2993};
2994
2995static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2996 struct shminfo *host_shminfo)
2997{
2998 struct target_shminfo *target_shminfo;
2999 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3000 return -TARGET_EFAULT;
3001 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3002 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3003 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3004 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3005 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3006 unlock_user_struct(target_shminfo, target_addr, 1);
3007 return 0;
3008}
3009
3010struct target_shm_info {
3011 int used_ids;
3012 abi_ulong shm_tot;
3013 abi_ulong shm_rss;
3014 abi_ulong shm_swp;
3015 abi_ulong swap_attempts;
3016 abi_ulong swap_successes;
3017};
3018
3019static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3020 struct shm_info *host_shm_info)
3021{
3022 struct target_shm_info *target_shm_info;
3023 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3024 return -TARGET_EFAULT;
3025 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3026 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3027 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3028 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3029 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3030 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3031 unlock_user_struct(target_shm_info, target_addr, 1);
3032 return 0;
3033}
3034
3035static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3036{
3037 struct shmid_ds dsarg;
3038 struct shminfo shminfo;
3039 struct shm_info shm_info;
3040 abi_long ret = -TARGET_EINVAL;
3041
3042 cmd &= 0xff;
3043
3044 switch(cmd) {
3045 case IPC_STAT:
3046 case IPC_SET:
3047 case SHM_STAT:
3048 if (target_to_host_shmid_ds(&dsarg, buf))
3049 return -TARGET_EFAULT;
3050 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3051 if (host_to_target_shmid_ds(buf, &dsarg))
3052 return -TARGET_EFAULT;
3053 break;
3054 case IPC_INFO:
3055 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3056 if (host_to_target_shminfo(buf, &shminfo))
3057 return -TARGET_EFAULT;
3058 break;
3059 case SHM_INFO:
3060 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3061 if (host_to_target_shm_info(buf, &shm_info))
3062 return -TARGET_EFAULT;
3063 break;
3064 case IPC_RMID:
3065 case SHM_LOCK:
3066 case SHM_UNLOCK:
3067 ret = get_errno(shmctl(shmid, cmd, NULL));
3068 break;
3069 }
3070
3071 return ret;
3072}
3073
3074static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3075{
3076 abi_long raddr;
3077 void *host_raddr;
3078 struct shmid_ds shm_info;
3079 int i,ret;
3080
3081 /* find out the length of the shared memory segment */
3082 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3083 if (is_error(ret)) {
3084 /* can't get length, bail out */
3085 return ret;
3086 }
3087
3088 mmap_lock();
3089
3090 if (shmaddr)
3091 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3092 else {
3093 abi_ulong mmap_start;
3094
3095 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3096
3097 if (mmap_start == -1) {
3098 errno = ENOMEM;
3099 host_raddr = (void *)-1;
3100 } else
3101 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3102 }
3103
3104 if (host_raddr == (void *)-1) {
3105 mmap_unlock();
3106 return get_errno((long)host_raddr);
3107 }
3108 raddr=h2g((unsigned long)host_raddr);
3109
3110 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3111 PAGE_VALID | PAGE_READ |
3112 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3113
3114 for (i = 0; i < N_SHM_REGIONS; i++) {
3115 if (shm_regions[i].start == 0) {
3116 shm_regions[i].start = raddr;
3117 shm_regions[i].size = shm_info.shm_segsz;
3118 break;
3119 }
3120 }
3121
3122 mmap_unlock();
3123 return raddr;
3124
3125}
3126
3127static inline abi_long do_shmdt(abi_ulong shmaddr)
3128{
3129 int i;
3130
3131 for (i = 0; i < N_SHM_REGIONS; ++i) {
3132 if (shm_regions[i].start == shmaddr) {
3133 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09003134 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03003135 break;
3136 }
3137 }
3138
3139 return get_errno(shmdt(g2h(shmaddr)));
3140}
3141
aurel321c54ff92008-10-13 21:08:44 +00003142#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00003143/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00003144/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003145static abi_long do_ipc(unsigned int call, int first,
3146 int second, int third,
3147 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00003148{
3149 int version;
blueswir1992f48a2007-10-14 16:27:31 +00003150 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00003151
3152 version = call >> 16;
3153 call &= 0xffff;
3154
3155 switch (call) {
thsfa294812007-02-02 22:05:00 +00003156 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00003157 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00003158 break;
3159
3160 case IPCOP_semget:
3161 ret = get_errno(semget(first, second, third));
3162 break;
3163
3164 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00003165 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00003166 break;
thsd96372e2007-02-02 22:05:44 +00003167
aurel321c54ff92008-10-13 21:08:44 +00003168 case IPCOP_msgget:
3169 ret = get_errno(msgget(first, second));
3170 break;
thsd96372e2007-02-02 22:05:44 +00003171
aurel321c54ff92008-10-13 21:08:44 +00003172 case IPCOP_msgsnd:
3173 ret = do_msgsnd(first, ptr, second, third);
3174 break;
thsd96372e2007-02-02 22:05:44 +00003175
aurel321c54ff92008-10-13 21:08:44 +00003176 case IPCOP_msgctl:
3177 ret = do_msgctl(first, second, ptr);
3178 break;
thsd96372e2007-02-02 22:05:44 +00003179
aurel321c54ff92008-10-13 21:08:44 +00003180 case IPCOP_msgrcv:
3181 switch (version) {
3182 case 0:
3183 {
3184 struct target_ipc_kludge {
3185 abi_long msgp;
3186 abi_long msgtyp;
3187 } *tmp;
thsd96372e2007-02-02 22:05:44 +00003188
aurel321c54ff92008-10-13 21:08:44 +00003189 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3190 ret = -TARGET_EFAULT;
3191 break;
ths1bc012f2007-06-03 14:27:49 +00003192 }
aurel321c54ff92008-10-13 21:08:44 +00003193
3194 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
3195
3196 unlock_user_struct(tmp, ptr, 0);
3197 break;
3198 }
3199 default:
3200 ret = do_msgrcv(first, ptr, second, fifth, third);
3201 }
3202 break;
thsd96372e2007-02-02 22:05:44 +00003203
bellard8853f862004-02-22 14:57:26 +00003204 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03003205 switch (version) {
3206 default:
bellard5a4a8982007-11-11 17:39:18 +00003207 {
3208 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03003209 raddr = do_shmat(first, ptr, second);
3210 if (is_error(raddr))
3211 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00003212 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00003213 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03003214 break;
3215 }
3216 case 1:
3217 ret = -TARGET_EINVAL;
3218 break;
bellard5a4a8982007-11-11 17:39:18 +00003219 }
bellard8853f862004-02-22 14:57:26 +00003220 break;
3221 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03003222 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00003223 break;
3224
3225 case IPCOP_shmget:
3226 /* IPC_* flag values are the same on all linux platforms */
3227 ret = get_errno(shmget(first, second, third));
3228 break;
3229
3230 /* IPC_* and SHM_* command values are the same on all linux platforms */
3231 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03003232 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00003233 break;
3234 default:
j_mayer32407102007-09-26 23:01:49 +00003235 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00003236 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00003237 break;
3238 }
3239 return ret;
3240}
j_mayer32407102007-09-26 23:01:49 +00003241#endif
bellard8853f862004-02-22 14:57:26 +00003242
bellard31e31b82003-02-18 22:55:36 +00003243/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00003244
Blue Swirl001faf32009-05-13 17:53:17 +00003245#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00003246#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3247enum {
3248#include "syscall_types.h"
3249};
3250#undef STRUCT
3251#undef STRUCT_SPECIAL
3252
Blue Swirl001faf32009-05-13 17:53:17 +00003253#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00003254#define STRUCT_SPECIAL(name)
3255#include "syscall_types.h"
3256#undef STRUCT
3257#undef STRUCT_SPECIAL
3258
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003259typedef struct IOCTLEntry IOCTLEntry;
3260
3261typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3262 int fd, abi_long cmd, abi_long arg);
3263
3264struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00003265 unsigned int target_cmd;
3266 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00003267 const char *name;
3268 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003269 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003270 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003271};
bellard31e31b82003-02-18 22:55:36 +00003272
3273#define IOC_R 0x0001
3274#define IOC_W 0x0002
3275#define IOC_RW (IOC_R | IOC_W)
3276
3277#define MAX_STRUCT_SIZE 4096
3278
Peter Maydelldace20d2011-01-10 13:11:24 +00003279#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003280/* So fiemap access checks don't overflow on 32 bit systems.
3281 * This is very slightly smaller than the limit imposed by
3282 * the underlying kernel.
3283 */
3284#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3285 / sizeof(struct fiemap_extent))
3286
3287static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3288 int fd, abi_long cmd, abi_long arg)
3289{
3290 /* The parameter for this ioctl is a struct fiemap followed
3291 * by an array of struct fiemap_extent whose size is set
3292 * in fiemap->fm_extent_count. The array is filled in by the
3293 * ioctl.
3294 */
3295 int target_size_in, target_size_out;
3296 struct fiemap *fm;
3297 const argtype *arg_type = ie->arg_type;
3298 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3299 void *argptr, *p;
3300 abi_long ret;
3301 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3302 uint32_t outbufsz;
3303 int free_fm = 0;
3304
3305 assert(arg_type[0] == TYPE_PTR);
3306 assert(ie->access == IOC_RW);
3307 arg_type++;
3308 target_size_in = thunk_type_size(arg_type, 0);
3309 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3310 if (!argptr) {
3311 return -TARGET_EFAULT;
3312 }
3313 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3314 unlock_user(argptr, arg, 0);
3315 fm = (struct fiemap *)buf_temp;
3316 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3317 return -TARGET_EINVAL;
3318 }
3319
3320 outbufsz = sizeof (*fm) +
3321 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3322
3323 if (outbufsz > MAX_STRUCT_SIZE) {
3324 /* We can't fit all the extents into the fixed size buffer.
3325 * Allocate one that is large enough and use it instead.
3326 */
3327 fm = malloc(outbufsz);
3328 if (!fm) {
3329 return -TARGET_ENOMEM;
3330 }
3331 memcpy(fm, buf_temp, sizeof(struct fiemap));
3332 free_fm = 1;
3333 }
3334 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3335 if (!is_error(ret)) {
3336 target_size_out = target_size_in;
3337 /* An extent_count of 0 means we were only counting the extents
3338 * so there are no structs to copy
3339 */
3340 if (fm->fm_extent_count != 0) {
3341 target_size_out += fm->fm_mapped_extents * extent_size;
3342 }
3343 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3344 if (!argptr) {
3345 ret = -TARGET_EFAULT;
3346 } else {
3347 /* Convert the struct fiemap */
3348 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3349 if (fm->fm_extent_count != 0) {
3350 p = argptr + target_size_in;
3351 /* ...and then all the struct fiemap_extents */
3352 for (i = 0; i < fm->fm_mapped_extents; i++) {
3353 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3354 THUNK_TARGET);
3355 p += extent_size;
3356 }
3357 }
3358 unlock_user(argptr, arg, target_size_out);
3359 }
3360 }
3361 if (free_fm) {
3362 free(fm);
3363 }
3364 return ret;
3365}
Peter Maydelldace20d2011-01-10 13:11:24 +00003366#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003367
Laurent Vivier059c2f22011-03-30 00:12:12 +02003368static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3369 int fd, abi_long cmd, abi_long arg)
3370{
3371 const argtype *arg_type = ie->arg_type;
3372 int target_size;
3373 void *argptr;
3374 int ret;
3375 struct ifconf *host_ifconf;
3376 uint32_t outbufsz;
3377 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3378 int target_ifreq_size;
3379 int nb_ifreq;
3380 int free_buf = 0;
3381 int i;
3382 int target_ifc_len;
3383 abi_long target_ifc_buf;
3384 int host_ifc_len;
3385 char *host_ifc_buf;
3386
3387 assert(arg_type[0] == TYPE_PTR);
3388 assert(ie->access == IOC_RW);
3389
3390 arg_type++;
3391 target_size = thunk_type_size(arg_type, 0);
3392
3393 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3394 if (!argptr)
3395 return -TARGET_EFAULT;
3396 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3397 unlock_user(argptr, arg, 0);
3398
3399 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3400 target_ifc_len = host_ifconf->ifc_len;
3401 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3402
3403 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3404 nb_ifreq = target_ifc_len / target_ifreq_size;
3405 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3406
3407 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3408 if (outbufsz > MAX_STRUCT_SIZE) {
3409 /* We can't fit all the extents into the fixed size buffer.
3410 * Allocate one that is large enough and use it instead.
3411 */
3412 host_ifconf = malloc(outbufsz);
3413 if (!host_ifconf) {
3414 return -TARGET_ENOMEM;
3415 }
3416 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3417 free_buf = 1;
3418 }
3419 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3420
3421 host_ifconf->ifc_len = host_ifc_len;
3422 host_ifconf->ifc_buf = host_ifc_buf;
3423
3424 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3425 if (!is_error(ret)) {
3426 /* convert host ifc_len to target ifc_len */
3427
3428 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3429 target_ifc_len = nb_ifreq * target_ifreq_size;
3430 host_ifconf->ifc_len = target_ifc_len;
3431
3432 /* restore target ifc_buf */
3433
3434 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3435
3436 /* copy struct ifconf to target user */
3437
3438 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3439 if (!argptr)
3440 return -TARGET_EFAULT;
3441 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3442 unlock_user(argptr, arg, target_size);
3443
3444 /* copy ifreq[] to target user */
3445
3446 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3447 for (i = 0; i < nb_ifreq ; i++) {
3448 thunk_convert(argptr + i * target_ifreq_size,
3449 host_ifc_buf + i * sizeof(struct ifreq),
3450 ifreq_arg_type, THUNK_TARGET);
3451 }
3452 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3453 }
3454
3455 if (free_buf) {
3456 free(host_ifconf);
3457 }
3458
3459 return ret;
3460}
3461
Alexander Graf56e904e2012-01-31 18:42:06 +01003462static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3463 abi_long cmd, abi_long arg)
3464{
3465 void *argptr;
3466 struct dm_ioctl *host_dm;
3467 abi_long guest_data;
3468 uint32_t guest_data_size;
3469 int target_size;
3470 const argtype *arg_type = ie->arg_type;
3471 abi_long ret;
3472 void *big_buf = NULL;
3473 char *host_data;
3474
3475 arg_type++;
3476 target_size = thunk_type_size(arg_type, 0);
3477 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3478 if (!argptr) {
3479 ret = -TARGET_EFAULT;
3480 goto out;
3481 }
3482 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3483 unlock_user(argptr, arg, 0);
3484
3485 /* buf_temp is too small, so fetch things into a bigger buffer */
3486 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3487 memcpy(big_buf, buf_temp, target_size);
3488 buf_temp = big_buf;
3489 host_dm = big_buf;
3490
3491 guest_data = arg + host_dm->data_start;
3492 if ((guest_data - arg) < 0) {
3493 ret = -EINVAL;
3494 goto out;
3495 }
3496 guest_data_size = host_dm->data_size - host_dm->data_start;
3497 host_data = (char*)host_dm + host_dm->data_start;
3498
3499 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3500 switch (ie->host_cmd) {
3501 case DM_REMOVE_ALL:
3502 case DM_LIST_DEVICES:
3503 case DM_DEV_CREATE:
3504 case DM_DEV_REMOVE:
3505 case DM_DEV_SUSPEND:
3506 case DM_DEV_STATUS:
3507 case DM_DEV_WAIT:
3508 case DM_TABLE_STATUS:
3509 case DM_TABLE_CLEAR:
3510 case DM_TABLE_DEPS:
3511 case DM_LIST_VERSIONS:
3512 /* no input data */
3513 break;
3514 case DM_DEV_RENAME:
3515 case DM_DEV_SET_GEOMETRY:
3516 /* data contains only strings */
3517 memcpy(host_data, argptr, guest_data_size);
3518 break;
3519 case DM_TARGET_MSG:
3520 memcpy(host_data, argptr, guest_data_size);
3521 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3522 break;
3523 case DM_TABLE_LOAD:
3524 {
3525 void *gspec = argptr;
3526 void *cur_data = host_data;
3527 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3528 int spec_size = thunk_type_size(arg_type, 0);
3529 int i;
3530
3531 for (i = 0; i < host_dm->target_count; i++) {
3532 struct dm_target_spec *spec = cur_data;
3533 uint32_t next;
3534 int slen;
3535
3536 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3537 slen = strlen((char*)gspec + spec_size) + 1;
3538 next = spec->next;
3539 spec->next = sizeof(*spec) + slen;
3540 strcpy((char*)&spec[1], gspec + spec_size);
3541 gspec += next;
3542 cur_data += spec->next;
3543 }
3544 break;
3545 }
3546 default:
3547 ret = -TARGET_EINVAL;
3548 goto out;
3549 }
3550 unlock_user(argptr, guest_data, 0);
3551
3552 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3553 if (!is_error(ret)) {
3554 guest_data = arg + host_dm->data_start;
3555 guest_data_size = host_dm->data_size - host_dm->data_start;
3556 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3557 switch (ie->host_cmd) {
3558 case DM_REMOVE_ALL:
3559 case DM_DEV_CREATE:
3560 case DM_DEV_REMOVE:
3561 case DM_DEV_RENAME:
3562 case DM_DEV_SUSPEND:
3563 case DM_DEV_STATUS:
3564 case DM_TABLE_LOAD:
3565 case DM_TABLE_CLEAR:
3566 case DM_TARGET_MSG:
3567 case DM_DEV_SET_GEOMETRY:
3568 /* no return data */
3569 break;
3570 case DM_LIST_DEVICES:
3571 {
3572 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3573 uint32_t remaining_data = guest_data_size;
3574 void *cur_data = argptr;
3575 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3576 int nl_size = 12; /* can't use thunk_size due to alignment */
3577
3578 while (1) {
3579 uint32_t next = nl->next;
3580 if (next) {
3581 nl->next = nl_size + (strlen(nl->name) + 1);
3582 }
3583 if (remaining_data < nl->next) {
3584 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3585 break;
3586 }
3587 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3588 strcpy(cur_data + nl_size, nl->name);
3589 cur_data += nl->next;
3590 remaining_data -= nl->next;
3591 if (!next) {
3592 break;
3593 }
3594 nl = (void*)nl + next;
3595 }
3596 break;
3597 }
3598 case DM_DEV_WAIT:
3599 case DM_TABLE_STATUS:
3600 {
3601 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3602 void *cur_data = argptr;
3603 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3604 int spec_size = thunk_type_size(arg_type, 0);
3605 int i;
3606
3607 for (i = 0; i < host_dm->target_count; i++) {
3608 uint32_t next = spec->next;
3609 int slen = strlen((char*)&spec[1]) + 1;
3610 spec->next = (cur_data - argptr) + spec_size + slen;
3611 if (guest_data_size < spec->next) {
3612 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3613 break;
3614 }
3615 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3616 strcpy(cur_data + spec_size, (char*)&spec[1]);
3617 cur_data = argptr + spec->next;
3618 spec = (void*)host_dm + host_dm->data_start + next;
3619 }
3620 break;
3621 }
3622 case DM_TABLE_DEPS:
3623 {
3624 void *hdata = (void*)host_dm + host_dm->data_start;
3625 int count = *(uint32_t*)hdata;
3626 uint64_t *hdev = hdata + 8;
3627 uint64_t *gdev = argptr + 8;
3628 int i;
3629
3630 *(uint32_t*)argptr = tswap32(count);
3631 for (i = 0; i < count; i++) {
3632 *gdev = tswap64(*hdev);
3633 gdev++;
3634 hdev++;
3635 }
3636 break;
3637 }
3638 case DM_LIST_VERSIONS:
3639 {
3640 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3641 uint32_t remaining_data = guest_data_size;
3642 void *cur_data = argptr;
3643 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3644 int vers_size = thunk_type_size(arg_type, 0);
3645
3646 while (1) {
3647 uint32_t next = vers->next;
3648 if (next) {
3649 vers->next = vers_size + (strlen(vers->name) + 1);
3650 }
3651 if (remaining_data < vers->next) {
3652 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3653 break;
3654 }
3655 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3656 strcpy(cur_data + vers_size, vers->name);
3657 cur_data += vers->next;
3658 remaining_data -= vers->next;
3659 if (!next) {
3660 break;
3661 }
3662 vers = (void*)vers + next;
3663 }
3664 break;
3665 }
3666 default:
3667 ret = -TARGET_EINVAL;
3668 goto out;
3669 }
3670 unlock_user(argptr, guest_data, guest_data_size);
3671
3672 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3673 if (!argptr) {
3674 ret = -TARGET_EFAULT;
3675 goto out;
3676 }
3677 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3678 unlock_user(argptr, arg, target_size);
3679 }
3680out:
Stefan Weilad11ad72012-09-04 22:14:19 +02003681 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01003682 return ret;
3683}
3684
blueswir19f106a72008-10-05 10:52:52 +00003685static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00003686#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003687 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3688#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3689 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00003690#include "ioctls.h"
3691 { 0, 0, },
3692};
3693
pbrook53a59602006-03-25 19:31:22 +00003694/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00003695/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003696static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00003697{
3698 const IOCTLEntry *ie;
3699 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00003700 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003701 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00003702 int target_size;
3703 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00003704
3705 ie = ioctl_entries;
3706 for(;;) {
3707 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003708 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003709 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003710 }
3711 if (ie->target_cmd == cmd)
3712 break;
3713 ie++;
3714 }
3715 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003716#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003717 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003718#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003719 if (ie->do_ioctl) {
3720 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3721 }
3722
bellard31e31b82003-02-18 22:55:36 +00003723 switch(arg_type[0]) {
3724 case TYPE_NULL:
3725 /* no argument */
3726 ret = get_errno(ioctl(fd, ie->host_cmd));
3727 break;
3728 case TYPE_PTRVOID:
3729 case TYPE_INT:
3730 /* int argment */
3731 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3732 break;
3733 case TYPE_PTR:
3734 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00003735 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00003736 switch(ie->access) {
3737 case IOC_R:
3738 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3739 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003740 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3741 if (!argptr)
3742 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003743 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3744 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003745 }
3746 break;
3747 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00003748 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3749 if (!argptr)
3750 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003751 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3752 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003753 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3754 break;
3755 default:
3756 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00003757 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3758 if (!argptr)
3759 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003760 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3761 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003762 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3763 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003764 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3765 if (!argptr)
3766 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003767 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3768 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003769 }
3770 break;
3771 }
3772 break;
3773 default:
j_mayer32407102007-09-26 23:01:49 +00003774 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3775 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003776 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003777 break;
3778 }
3779 return ret;
3780}
3781
blueswir1b39bc502008-10-05 10:51:10 +00003782static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003783 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3784 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3785 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3786 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3787 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3788 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3789 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3790 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3791 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3792 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3793 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3794 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3795 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3796 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3797 { 0, 0, 0, 0 }
3798};
3799
blueswir1b39bc502008-10-05 10:51:10 +00003800static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003801 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3802 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3803 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3804 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3805 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3806 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3807 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3808 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3809 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3810 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3811 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3812 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3813 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3814 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3815 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3816 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3817 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3818 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3819 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3820 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3821 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3822 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3823 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3824 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3825 { 0, 0, 0, 0 }
3826};
3827
blueswir1b39bc502008-10-05 10:51:10 +00003828static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003829 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3830 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3831 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3832 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3833 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3834 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3835 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3836 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3837 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3838 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3839 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3840 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3841 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3842 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3843 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3844 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3845 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3846 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3847 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3848 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3849 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3850 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3851 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3852 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3853 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3854 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3855 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3856 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3857 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3858 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3859 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3860 { 0, 0, 0, 0 }
3861};
3862
blueswir1b39bc502008-10-05 10:51:10 +00003863static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003864 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3865 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3866 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3867 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3868 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3869 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3870 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3871 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3872 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3873 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3874 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3875 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3876 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3877 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3878 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3879 { 0, 0, 0, 0 }
3880};
3881
3882static void target_to_host_termios (void *dst, const void *src)
3883{
3884 struct host_termios *host = dst;
3885 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003886
ths5fafdf22007-09-16 21:08:06 +00003887 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003888 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003889 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003890 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003891 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003892 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003893 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003894 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3895 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003896
Arnaud Patard44607122009-04-21 17:39:08 +03003897 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003898 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3899 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003900 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003901 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003902 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003903 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003904 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003905 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003906 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003907 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3908 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003909 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3910 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3911 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3912 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3913 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003914 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003915}
ths3b46e622007-09-17 08:09:54 +00003916
bellard31e31b82003-02-18 22:55:36 +00003917static void host_to_target_termios (void *dst, const void *src)
3918{
3919 struct target_termios *target = dst;
3920 const struct host_termios *host = src;
3921
ths5fafdf22007-09-16 21:08:06 +00003922 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003923 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003924 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003925 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003926 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003927 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003928 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003929 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3930 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003931
Arnaud Patard44607122009-04-21 17:39:08 +03003932 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003933 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3934 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3935 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3936 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3937 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3938 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3939 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3940 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3941 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3942 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3943 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3944 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3945 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3946 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3947 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3948 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3949 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3950}
3951
blueswir18e853dc2008-10-05 10:49:32 +00003952static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003953 .convert = { host_to_target_termios, target_to_host_termios },
3954 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3955 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3956};
3957
bellard5286db72003-06-05 00:57:30 +00003958static bitmask_transtbl mmap_flags_tbl[] = {
3959 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3960 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3961 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3962 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3963 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3964 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3965 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3966 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3967 { 0, 0, 0, 0 }
3968};
3969
bellard2ab83ea2003-06-15 19:56:46 +00003970#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003971
3972/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003973static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003974
bellard03acab62007-11-11 14:57:14 +00003975static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003976{
3977 int size;
pbrook53a59602006-03-25 19:31:22 +00003978 void *p;
bellard6dbad632003-03-16 18:05:05 +00003979
3980 if (!ldt_table)
3981 return 0;
3982 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3983 if (size > bytecount)
3984 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003985 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3986 if (!p)
bellard03acab62007-11-11 14:57:14 +00003987 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003988 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003989 memcpy(p, ldt_table, size);
3990 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003991 return size;
3992}
3993
3994/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003995static abi_long write_ldt(CPUX86State *env,
3996 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003997{
3998 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003999 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00004000 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00004001 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00004002 uint32_t *lp, entry_1, entry_2;
4003
4004 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00004005 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00004006 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00004007 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004008 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004009 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00004010 ldt_info.limit = tswap32(target_ldt_info->limit);
4011 ldt_info.flags = tswap32(target_ldt_info->flags);
4012 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00004013
bellard6dbad632003-03-16 18:05:05 +00004014 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00004015 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004016 seg_32bit = ldt_info.flags & 1;
4017 contents = (ldt_info.flags >> 1) & 3;
4018 read_exec_only = (ldt_info.flags >> 3) & 1;
4019 limit_in_pages = (ldt_info.flags >> 4) & 1;
4020 seg_not_present = (ldt_info.flags >> 5) & 1;
4021 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00004022#ifdef TARGET_ABI32
4023 lm = 0;
4024#else
4025 lm = (ldt_info.flags >> 7) & 1;
4026#endif
bellard6dbad632003-03-16 18:05:05 +00004027 if (contents == 3) {
4028 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00004029 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004030 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00004031 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004032 }
4033 /* allocate the LDT */
4034 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00004035 env->ldt.base = target_mmap(0,
4036 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
4037 PROT_READ|PROT_WRITE,
4038 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4039 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00004040 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00004041 memset(g2h(env->ldt.base), 0,
4042 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00004043 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00004044 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00004045 }
4046
4047 /* NOTE: same code as Linux kernel */
4048 /* Allow LDTs to be cleared by the user. */
4049 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4050 if (oldmode ||
4051 (contents == 0 &&
4052 read_exec_only == 1 &&
4053 seg_32bit == 0 &&
4054 limit_in_pages == 0 &&
4055 seg_not_present == 1 &&
4056 useable == 0 )) {
4057 entry_1 = 0;
4058 entry_2 = 0;
4059 goto install;
4060 }
4061 }
ths3b46e622007-09-17 08:09:54 +00004062
bellard6dbad632003-03-16 18:05:05 +00004063 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4064 (ldt_info.limit & 0x0ffff);
4065 entry_2 = (ldt_info.base_addr & 0xff000000) |
4066 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4067 (ldt_info.limit & 0xf0000) |
4068 ((read_exec_only ^ 1) << 9) |
4069 (contents << 10) |
4070 ((seg_not_present ^ 1) << 15) |
4071 (seg_32bit << 22) |
4072 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00004073 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00004074 0x7000;
4075 if (!oldmode)
4076 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00004077
bellard6dbad632003-03-16 18:05:05 +00004078 /* Install the new entry ... */
4079install:
4080 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4081 lp[0] = tswap32(entry_1);
4082 lp[1] = tswap32(entry_2);
4083 return 0;
4084}
4085
4086/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00004087static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4088 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00004089{
bellard03acab62007-11-11 14:57:14 +00004090 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00004091
bellard6dbad632003-03-16 18:05:05 +00004092 switch (func) {
4093 case 0:
4094 ret = read_ldt(ptr, bytecount);
4095 break;
4096 case 1:
4097 ret = write_ldt(env, ptr, bytecount, 1);
4098 break;
4099 case 0x11:
4100 ret = write_ldt(env, ptr, bytecount, 0);
4101 break;
bellard03acab62007-11-11 14:57:14 +00004102 default:
4103 ret = -TARGET_ENOSYS;
4104 break;
bellard6dbad632003-03-16 18:05:05 +00004105 }
4106 return ret;
4107}
bellard1b6b0292003-03-22 17:31:38 +00004108
blueswir14583f582008-08-24 10:35:55 +00004109#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00004110static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004111{
4112 uint64_t *gdt_table = g2h(env->gdt.base);
4113 struct target_modify_ldt_ldt_s ldt_info;
4114 struct target_modify_ldt_ldt_s *target_ldt_info;
4115 int seg_32bit, contents, read_exec_only, limit_in_pages;
4116 int seg_not_present, useable, lm;
4117 uint32_t *lp, entry_1, entry_2;
4118 int i;
4119
4120 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4121 if (!target_ldt_info)
4122 return -TARGET_EFAULT;
4123 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004124 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00004125 ldt_info.limit = tswap32(target_ldt_info->limit);
4126 ldt_info.flags = tswap32(target_ldt_info->flags);
4127 if (ldt_info.entry_number == -1) {
4128 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4129 if (gdt_table[i] == 0) {
4130 ldt_info.entry_number = i;
4131 target_ldt_info->entry_number = tswap32(i);
4132 break;
4133 }
4134 }
4135 }
4136 unlock_user_struct(target_ldt_info, ptr, 1);
4137
4138 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4139 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4140 return -TARGET_EINVAL;
4141 seg_32bit = ldt_info.flags & 1;
4142 contents = (ldt_info.flags >> 1) & 3;
4143 read_exec_only = (ldt_info.flags >> 3) & 1;
4144 limit_in_pages = (ldt_info.flags >> 4) & 1;
4145 seg_not_present = (ldt_info.flags >> 5) & 1;
4146 useable = (ldt_info.flags >> 6) & 1;
4147#ifdef TARGET_ABI32
4148 lm = 0;
4149#else
4150 lm = (ldt_info.flags >> 7) & 1;
4151#endif
4152
4153 if (contents == 3) {
4154 if (seg_not_present == 0)
4155 return -TARGET_EINVAL;
4156 }
4157
4158 /* NOTE: same code as Linux kernel */
4159 /* Allow LDTs to be cleared by the user. */
4160 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4161 if ((contents == 0 &&
4162 read_exec_only == 1 &&
4163 seg_32bit == 0 &&
4164 limit_in_pages == 0 &&
4165 seg_not_present == 1 &&
4166 useable == 0 )) {
4167 entry_1 = 0;
4168 entry_2 = 0;
4169 goto install;
4170 }
4171 }
4172
4173 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4174 (ldt_info.limit & 0x0ffff);
4175 entry_2 = (ldt_info.base_addr & 0xff000000) |
4176 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4177 (ldt_info.limit & 0xf0000) |
4178 ((read_exec_only ^ 1) << 9) |
4179 (contents << 10) |
4180 ((seg_not_present ^ 1) << 15) |
4181 (seg_32bit << 22) |
4182 (limit_in_pages << 23) |
4183 (useable << 20) |
4184 (lm << 21) |
4185 0x7000;
4186
4187 /* Install the new entry ... */
4188install:
4189 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4190 lp[0] = tswap32(entry_1);
4191 lp[1] = tswap32(entry_2);
4192 return 0;
4193}
4194
blueswir18fcd3692008-08-17 20:26:25 +00004195static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004196{
4197 struct target_modify_ldt_ldt_s *target_ldt_info;
4198 uint64_t *gdt_table = g2h(env->gdt.base);
4199 uint32_t base_addr, limit, flags;
4200 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4201 int seg_not_present, useable, lm;
4202 uint32_t *lp, entry_1, entry_2;
4203
4204 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4205 if (!target_ldt_info)
4206 return -TARGET_EFAULT;
4207 idx = tswap32(target_ldt_info->entry_number);
4208 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4209 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4210 unlock_user_struct(target_ldt_info, ptr, 1);
4211 return -TARGET_EINVAL;
4212 }
4213 lp = (uint32_t *)(gdt_table + idx);
4214 entry_1 = tswap32(lp[0]);
4215 entry_2 = tswap32(lp[1]);
4216
4217 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4218 contents = (entry_2 >> 10) & 3;
4219 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4220 seg_32bit = (entry_2 >> 22) & 1;
4221 limit_in_pages = (entry_2 >> 23) & 1;
4222 useable = (entry_2 >> 20) & 1;
4223#ifdef TARGET_ABI32
4224 lm = 0;
4225#else
4226 lm = (entry_2 >> 21) & 1;
4227#endif
4228 flags = (seg_32bit << 0) | (contents << 1) |
4229 (read_exec_only << 3) | (limit_in_pages << 4) |
4230 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4231 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4232 base_addr = (entry_1 >> 16) |
4233 (entry_2 & 0xff000000) |
4234 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004235 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00004236 target_ldt_info->limit = tswap32(limit);
4237 target_ldt_info->flags = tswap32(flags);
4238 unlock_user_struct(target_ldt_info, ptr, 1);
4239 return 0;
4240}
blueswir14583f582008-08-24 10:35:55 +00004241#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00004242
bellardd2fd1af2007-11-14 18:08:56 +00004243#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00004244static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00004245{
Juan Quintela1add8692011-06-16 17:37:09 +01004246 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00004247 abi_ulong val;
4248 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01004249
bellardd2fd1af2007-11-14 18:08:56 +00004250 switch(code) {
4251 case TARGET_ARCH_SET_GS:
4252 case TARGET_ARCH_SET_FS:
4253 if (code == TARGET_ARCH_SET_GS)
4254 idx = R_GS;
4255 else
4256 idx = R_FS;
4257 cpu_x86_load_seg(env, idx, 0);
4258 env->segs[idx].base = addr;
4259 break;
4260 case TARGET_ARCH_GET_GS:
4261 case TARGET_ARCH_GET_FS:
4262 if (code == TARGET_ARCH_GET_GS)
4263 idx = R_GS;
4264 else
4265 idx = R_FS;
4266 val = env->segs[idx].base;
4267 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01004268 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00004269 break;
4270 default:
4271 ret = -TARGET_EINVAL;
4272 break;
4273 }
Juan Quintela1add8692011-06-16 17:37:09 +01004274 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00004275}
4276#endif
4277
bellard2ab83ea2003-06-15 19:56:46 +00004278#endif /* defined(TARGET_I386) */
4279
Riku Voipio05098a92011-03-04 15:27:29 +02004280#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00004281
Riku Voipio05098a92011-03-04 15:27:29 +02004282#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004283
4284static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4285typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01004286 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00004287 pthread_mutex_t mutex;
4288 pthread_cond_t cond;
4289 pthread_t thread;
4290 uint32_t tid;
4291 abi_ulong child_tidptr;
4292 abi_ulong parent_tidptr;
4293 sigset_t sigmask;
4294} new_thread_info;
4295
4296static void *clone_func(void *arg)
4297{
4298 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004299 CPUArchState *env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004300 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00004301
4302 env = info->env;
4303 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004304 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004305 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004306 env->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004307 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00004308 if (info->child_tidptr)
4309 put_user_u32(info->tid, info->child_tidptr);
4310 if (info->parent_tidptr)
4311 put_user_u32(info->tid, info->parent_tidptr);
4312 /* Enable signals. */
4313 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4314 /* Signal to the parent that we're ready. */
4315 pthread_mutex_lock(&info->mutex);
4316 pthread_cond_broadcast(&info->cond);
4317 pthread_mutex_unlock(&info->mutex);
4318 /* Wait until the parent has finshed initializing the tls state. */
4319 pthread_mutex_lock(&clone_lock);
4320 pthread_mutex_unlock(&clone_lock);
4321 cpu_loop(env);
4322 /* never exits */
4323 return NULL;
4324}
4325#else
bellard1b6b0292003-03-22 17:31:38 +00004326
4327static int clone_func(void *arg)
4328{
Andreas Färber9349b4f2012-03-14 01:38:32 +01004329 CPUArchState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00004330 cpu_loop(env);
4331 /* never exits */
4332 return 0;
4333}
pbrookd865bab2008-06-07 22:12:17 +00004334#endif
bellard1b6b0292003-03-22 17:31:38 +00004335
ths0da46a62007-10-20 20:23:07 +00004336/* do_fork() Must return host values and target errnos (unlike most
4337 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01004338static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00004339 abi_ulong parent_tidptr, target_ulong newtls,
4340 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00004341{
4342 int ret;
bellard5cd43932003-03-29 16:54:36 +00004343 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004344 CPUArchState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004345#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004346 unsigned int nptl_flags;
4347 sigset_t sigmask;
Riku Voipio91907492010-11-26 16:21:34 +02004348#else
4349 uint8_t *new_stack;
pbrookd865bab2008-06-07 22:12:17 +00004350#endif
ths3b46e622007-09-17 08:09:54 +00004351
balrog436d1242008-09-21 02:39:45 +00004352 /* Emulate vfork() with fork() */
4353 if (flags & CLONE_VFORK)
4354 flags &= ~(CLONE_VFORK | CLONE_VM);
4355
bellard1b6b0292003-03-22 17:31:38 +00004356 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004357 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004358#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004359 new_thread_info info;
4360 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00004361#endif
Anthony Liguori7267c092011-08-20 22:09:37 -05004362 ts = g_malloc0(sizeof(TaskState));
pbrook624f7972008-05-31 16:11:38 +00004363 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00004364 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00004365 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00004366#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
Andreas Färberff18b762012-05-05 14:47:34 +02004367 cpu_reset(ENV_GET_CPU(new_env));
Blue Swirlb4558d72009-11-07 16:34:12 +00004368#endif
pbrook6e68e072008-05-30 17:22:15 +00004369 /* Init regs that differ from the parent. */
4370 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00004371 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004372 ts->bprm = parent_ts->bprm;
4373 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004374#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004375 nptl_flags = flags;
4376 flags &= ~CLONE_NPTL_FLAGS2;
4377
pbrookc2764712009-03-07 15:24:59 +00004378 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4379 ts->child_tidptr = child_tidptr;
4380 }
4381
pbrookd865bab2008-06-07 22:12:17 +00004382 if (nptl_flags & CLONE_SETTLS)
4383 cpu_set_tls (new_env, newtls);
4384
4385 /* Grab a mutex so that thread setup appears atomic. */
4386 pthread_mutex_lock(&clone_lock);
4387
4388 memset(&info, 0, sizeof(info));
4389 pthread_mutex_init(&info.mutex, NULL);
4390 pthread_mutex_lock(&info.mutex);
4391 pthread_cond_init(&info.cond, NULL);
4392 info.env = new_env;
4393 if (nptl_flags & CLONE_CHILD_SETTID)
4394 info.child_tidptr = child_tidptr;
4395 if (nptl_flags & CLONE_PARENT_SETTID)
4396 info.parent_tidptr = parent_tidptr;
4397
4398 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004399 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4400 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00004401 /* It is not safe to deliver signals until the child has finished
4402 initializing, so temporarily block all signals. */
4403 sigfillset(&sigmask);
4404 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4405
4406 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00004407 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00004408
4409 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4410 pthread_attr_destroy(&attr);
4411 if (ret == 0) {
4412 /* Wait for the child to initialize. */
4413 pthread_cond_wait(&info.cond, &info.mutex);
4414 ret = info.tid;
4415 if (flags & CLONE_PARENT_SETTID)
4416 put_user_u32(ret, parent_tidptr);
4417 } else {
4418 ret = -1;
4419 }
4420 pthread_mutex_unlock(&info.mutex);
4421 pthread_cond_destroy(&info.cond);
4422 pthread_mutex_destroy(&info.mutex);
4423 pthread_mutex_unlock(&clone_lock);
4424#else
4425 if (flags & CLONE_NPTL_FLAGS2)
4426 return -EINVAL;
4427 /* This is probably going to die very quickly, but do it anyway. */
Anthony Liguori7267c092011-08-20 22:09:37 -05004428 new_stack = g_malloc0 (NEW_STACK_SIZE);
bellard27725c12003-04-29 21:08:18 +00004429#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02004430 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00004431#else
4432 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
4433#endif
pbrookd865bab2008-06-07 22:12:17 +00004434#endif
bellard1b6b0292003-03-22 17:31:38 +00004435 } else {
4436 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00004437 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00004438 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00004439 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00004440 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00004441 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00004442 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00004443 cpu_clone_regs(env, newsp);
4444 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02004445#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00004446 /* There is a race condition here. The parent process could
4447 theoretically read the TID in the child process before the child
4448 tid is set. This would require using either ptrace
4449 (not implemented) or having *_tidptr to point at a shared memory
4450 mapping. We can't repeat the spinlock hack used above because
4451 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00004452 if (flags & CLONE_CHILD_SETTID)
4453 put_user_u32(gettid(), child_tidptr);
4454 if (flags & CLONE_PARENT_SETTID)
4455 put_user_u32(gettid(), parent_tidptr);
4456 ts = (TaskState *)env->opaque;
4457 if (flags & CLONE_SETTLS)
4458 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00004459 if (flags & CLONE_CHILD_CLEARTID)
4460 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00004461#endif
pbrookd865bab2008-06-07 22:12:17 +00004462 } else {
4463 fork_end(0);
4464 }
bellard1b6b0292003-03-22 17:31:38 +00004465 }
4466 return ret;
4467}
4468
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004469/* warning : doesn't handle linux specific flags... */
4470static int target_to_host_fcntl_cmd(int cmd)
4471{
4472 switch(cmd) {
4473 case TARGET_F_DUPFD:
4474 case TARGET_F_GETFD:
4475 case TARGET_F_SETFD:
4476 case TARGET_F_GETFL:
4477 case TARGET_F_SETFL:
4478 return cmd;
4479 case TARGET_F_GETLK:
4480 return F_GETLK;
4481 case TARGET_F_SETLK:
4482 return F_SETLK;
4483 case TARGET_F_SETLKW:
4484 return F_SETLKW;
4485 case TARGET_F_GETOWN:
4486 return F_GETOWN;
4487 case TARGET_F_SETOWN:
4488 return F_SETOWN;
4489 case TARGET_F_GETSIG:
4490 return F_GETSIG;
4491 case TARGET_F_SETSIG:
4492 return F_SETSIG;
4493#if TARGET_ABI_BITS == 32
4494 case TARGET_F_GETLK64:
4495 return F_GETLK64;
4496 case TARGET_F_SETLK64:
4497 return F_SETLK64;
4498 case TARGET_F_SETLKW64:
4499 return F_SETLKW64;
4500#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004501 case TARGET_F_SETLEASE:
4502 return F_SETLEASE;
4503 case TARGET_F_GETLEASE:
4504 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004505#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004506 case TARGET_F_DUPFD_CLOEXEC:
4507 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004508#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004509 case TARGET_F_NOTIFY:
4510 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004511 default:
4512 return -TARGET_EINVAL;
4513 }
4514 return -TARGET_EINVAL;
4515}
4516
blueswir1992f48a2007-10-14 16:27:31 +00004517static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004518{
4519 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004520 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004521 struct flock64 fl64;
4522 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00004523 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004524 int host_cmd = target_to_host_fcntl_cmd(cmd);
4525
4526 if (host_cmd == -TARGET_EINVAL)
4527 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004528
bellard7775e9e2003-05-14 22:46:48 +00004529 switch(cmd) {
4530 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004531 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4532 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00004533 fl.l_type = tswap16(target_fl->l_type);
4534 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004535 fl.l_start = tswapal(target_fl->l_start);
4536 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004537 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004538 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004539 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004540 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004541 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4542 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00004543 target_fl->l_type = tswap16(fl.l_type);
4544 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004545 target_fl->l_start = tswapal(fl.l_start);
4546 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004547 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004548 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004549 }
4550 break;
ths3b46e622007-09-17 08:09:54 +00004551
bellard7775e9e2003-05-14 22:46:48 +00004552 case TARGET_F_SETLK:
4553 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004554 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4555 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00004556 fl.l_type = tswap16(target_fl->l_type);
4557 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004558 fl.l_start = tswapal(target_fl->l_start);
4559 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004560 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004561 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004562 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004563 break;
ths3b46e622007-09-17 08:09:54 +00004564
bellard7775e9e2003-05-14 22:46:48 +00004565 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004566 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4567 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00004568 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4569 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004570 fl64.l_start = tswap64(target_fl64->l_start);
4571 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004572 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004573 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004574 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004575 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004576 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4577 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00004578 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
4579 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004580 target_fl64->l_start = tswap64(fl64.l_start);
4581 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004582 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004583 unlock_user_struct(target_fl64, arg, 1);
4584 }
bellard9ee1fa22007-11-11 15:11:19 +00004585 break;
bellard7775e9e2003-05-14 22:46:48 +00004586 case TARGET_F_SETLK64:
4587 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004588 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4589 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00004590 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4591 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004592 fl64.l_start = tswap64(target_fl64->l_start);
4593 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004594 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004595 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004596 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004597 break;
4598
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004599 case TARGET_F_GETFL:
4600 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004601 if (ret >= 0) {
4602 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4603 }
bellardffa65c32004-01-04 23:57:22 +00004604 break;
4605
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004606 case TARGET_F_SETFL:
4607 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4608 break;
4609
4610 case TARGET_F_SETOWN:
4611 case TARGET_F_GETOWN:
4612 case TARGET_F_SETSIG:
4613 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004614 case TARGET_F_SETLEASE:
4615 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004616 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00004617 break;
4618
bellard7775e9e2003-05-14 22:46:48 +00004619 default:
bellard9ee1fa22007-11-11 15:11:19 +00004620 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00004621 break;
4622 }
4623 return ret;
4624}
4625
bellard67867302003-11-23 17:05:30 +00004626#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00004627
bellard67867302003-11-23 17:05:30 +00004628static inline int high2lowuid(int uid)
4629{
4630 if (uid > 65535)
4631 return 65534;
4632 else
4633 return uid;
4634}
4635
4636static inline int high2lowgid(int gid)
4637{
4638 if (gid > 65535)
4639 return 65534;
4640 else
4641 return gid;
4642}
4643
4644static inline int low2highuid(int uid)
4645{
4646 if ((int16_t)uid == -1)
4647 return -1;
4648 else
4649 return uid;
4650}
4651
4652static inline int low2highgid(int gid)
4653{
4654 if ((int16_t)gid == -1)
4655 return -1;
4656 else
4657 return gid;
4658}
Riku Voipio0c866a72011-04-18 15:23:06 +03004659static inline int tswapid(int id)
4660{
4661 return tswap16(id);
4662}
4663#else /* !USE_UID16 */
4664static inline int high2lowuid(int uid)
4665{
4666 return uid;
4667}
4668static inline int high2lowgid(int gid)
4669{
4670 return gid;
4671}
4672static inline int low2highuid(int uid)
4673{
4674 return uid;
4675}
4676static inline int low2highgid(int gid)
4677{
4678 return gid;
4679}
4680static inline int tswapid(int id)
4681{
4682 return tswap32(id);
4683}
bellard67867302003-11-23 17:05:30 +00004684#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004685
bellard31e31b82003-02-18 22:55:36 +00004686void syscall_init(void)
4687{
bellard2ab83ea2003-06-15 19:56:46 +00004688 IOCTLEntry *ie;
4689 const argtype *arg_type;
4690 int size;
thsb92c47c2007-11-01 00:07:38 +00004691 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004692
Blue Swirl001faf32009-05-13 17:53:17 +00004693#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004694#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004695#include "syscall_types.h"
4696#undef STRUCT
4697#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00004698
Peter Maydelldd6e9572012-07-23 08:07:22 +00004699 /* Build target_to_host_errno_table[] table from
4700 * host_to_target_errno_table[]. */
4701 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4702 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4703 }
4704
bellard2ab83ea2003-06-15 19:56:46 +00004705 /* we patch the ioctl size if necessary. We rely on the fact that
4706 no ioctl has all the bits at '1' in the size field */
4707 ie = ioctl_entries;
4708 while (ie->target_cmd != 0) {
4709 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4710 TARGET_IOC_SIZEMASK) {
4711 arg_type = ie->arg_type;
4712 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00004713 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00004714 ie->target_cmd);
4715 exit(1);
4716 }
4717 arg_type++;
4718 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00004719 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00004720 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4721 (size << TARGET_IOC_SIZESHIFT);
4722 }
thsb92c47c2007-11-01 00:07:38 +00004723
bellard2ab83ea2003-06-15 19:56:46 +00004724 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00004725#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4726 (defined(__x86_64__) && defined(TARGET_X86_64))
4727 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4728 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4729 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00004730 }
4731#endif
4732 ie++;
4733 }
bellard31e31b82003-02-18 22:55:36 +00004734}
bellardc573ff62004-01-04 15:51:36 +00004735
blueswir1992f48a2007-10-14 16:27:31 +00004736#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004737static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4738{
thsaf325d32008-06-10 15:29:15 +00004739#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004740 return ((uint64_t)word0 << 32) | word1;
4741#else
4742 return ((uint64_t)word1 << 32) | word0;
4743#endif
4744}
blueswir1992f48a2007-10-14 16:27:31 +00004745#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004746static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4747{
4748 return word0;
4749}
blueswir1992f48a2007-10-14 16:27:31 +00004750#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004751
4752#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004753static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4754 abi_long arg2,
4755 abi_long arg3,
4756 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004757{
Riku Voipio48e515d2011-07-12 15:40:51 +03004758 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004759 arg2 = arg3;
4760 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004761 }
pbrookce4defa2006-02-09 16:49:55 +00004762 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4763}
4764#endif
4765
4766#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004767static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4768 abi_long arg2,
4769 abi_long arg3,
4770 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004771{
Riku Voipio48e515d2011-07-12 15:40:51 +03004772 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004773 arg2 = arg3;
4774 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004775 }
pbrookce4defa2006-02-09 16:49:55 +00004776 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4777}
4778#endif
4779
bellard579a97f2007-11-11 14:26:47 +00004780static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4781 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004782{
4783 struct target_timespec *target_ts;
4784
bellard579a97f2007-11-11 14:26:47 +00004785 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4786 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004787 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4788 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004789 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004790 return 0;
pbrook53a59602006-03-25 19:31:22 +00004791}
4792
bellard579a97f2007-11-11 14:26:47 +00004793static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4794 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004795{
4796 struct target_timespec *target_ts;
4797
bellard579a97f2007-11-11 14:26:47 +00004798 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4799 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004800 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4801 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004802 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004803 return 0;
pbrook53a59602006-03-25 19:31:22 +00004804}
4805
aurel329d33b762009-04-08 23:07:05 +00004806#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004807static inline abi_long host_to_target_stat64(void *cpu_env,
4808 abi_ulong target_addr,
4809 struct stat *host_st)
4810{
4811#ifdef TARGET_ARM
4812 if (((CPUARMState *)cpu_env)->eabi) {
4813 struct target_eabi_stat64 *target_st;
4814
4815 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4816 return -TARGET_EFAULT;
4817 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4818 __put_user(host_st->st_dev, &target_st->st_dev);
4819 __put_user(host_st->st_ino, &target_st->st_ino);
4820#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4821 __put_user(host_st->st_ino, &target_st->__st_ino);
4822#endif
4823 __put_user(host_st->st_mode, &target_st->st_mode);
4824 __put_user(host_st->st_nlink, &target_st->st_nlink);
4825 __put_user(host_st->st_uid, &target_st->st_uid);
4826 __put_user(host_st->st_gid, &target_st->st_gid);
4827 __put_user(host_st->st_rdev, &target_st->st_rdev);
4828 __put_user(host_st->st_size, &target_st->st_size);
4829 __put_user(host_st->st_blksize, &target_st->st_blksize);
4830 __put_user(host_st->st_blocks, &target_st->st_blocks);
4831 __put_user(host_st->st_atime, &target_st->target_st_atime);
4832 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4833 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4834 unlock_user_struct(target_st, target_addr, 1);
4835 } else
4836#endif
4837 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004838#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004839 struct target_stat *target_st;
4840#else
balrog6a24a772008-09-20 02:23:36 +00004841 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004842#endif
balrog6a24a772008-09-20 02:23:36 +00004843
4844 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4845 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004846 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004847 __put_user(host_st->st_dev, &target_st->st_dev);
4848 __put_user(host_st->st_ino, &target_st->st_ino);
4849#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4850 __put_user(host_st->st_ino, &target_st->__st_ino);
4851#endif
4852 __put_user(host_st->st_mode, &target_st->st_mode);
4853 __put_user(host_st->st_nlink, &target_st->st_nlink);
4854 __put_user(host_st->st_uid, &target_st->st_uid);
4855 __put_user(host_st->st_gid, &target_st->st_gid);
4856 __put_user(host_st->st_rdev, &target_st->st_rdev);
4857 /* XXX: better use of kernel struct */
4858 __put_user(host_st->st_size, &target_st->st_size);
4859 __put_user(host_st->st_blksize, &target_st->st_blksize);
4860 __put_user(host_st->st_blocks, &target_st->st_blocks);
4861 __put_user(host_st->st_atime, &target_st->target_st_atime);
4862 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4863 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4864 unlock_user_struct(target_st, target_addr, 1);
4865 }
4866
4867 return 0;
4868}
4869#endif
4870
Juan Quintela2f7bb872009-07-27 16:13:24 +02004871#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004872/* ??? Using host futex calls even when target atomic operations
4873 are not really atomic probably breaks things. However implementing
4874 futexes locally would make futexes shared between multiple processes
4875 tricky. However they're probably useless because guest atomic
4876 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004877static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4878 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004879{
4880 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004881 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004882
4883 /* ??? We assume FUTEX_* constants are the same on both host
4884 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004885#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004886 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004887#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004888 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004889#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004890 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004891 case FUTEX_WAIT:
4892 if (timeout) {
4893 pts = &ts;
4894 target_to_host_timespec(pts, timeout);
4895 } else {
4896 pts = NULL;
4897 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004898 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004899 pts, NULL, 0));
4900 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004901 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004902 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004903 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004904 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004905 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004906 case FUTEX_WAKE_OP:
4907 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4908 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4909 But the prototype takes a `struct timespec *'; insert casts
4910 to satisfy the compiler. We do not need to tswap TIMEOUT
4911 since it's not compared to guest memory. */
4912 pts = (struct timespec *)(uintptr_t) timeout;
4913 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4914 g2h(uaddr2),
4915 (base_op == FUTEX_CMP_REQUEUE
4916 ? tswap32(val3)
4917 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004918 default:
4919 return -TARGET_ENOSYS;
4920 }
4921}
4922#endif
4923
pbrook1d9d8b52009-04-16 15:17:02 +00004924/* Map host to target signal numbers for the wait family of syscalls.
4925 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07004926int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00004927{
4928 if (WIFSIGNALED(status)) {
4929 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4930 }
4931 if (WIFSTOPPED(status)) {
4932 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4933 | (status & 0xff);
4934 }
4935 return status;
4936}
4937
pbrooka745ec62008-05-06 15:36:17 +00004938int get_osversion(void)
4939{
4940 static int osversion;
4941 struct new_utsname buf;
4942 const char *s;
4943 int i, n, tmp;
4944 if (osversion)
4945 return osversion;
4946 if (qemu_uname_release && *qemu_uname_release) {
4947 s = qemu_uname_release;
4948 } else {
4949 if (sys_uname(&buf))
4950 return 0;
4951 s = buf.release;
4952 }
4953 tmp = 0;
4954 for (i = 0; i < 3; i++) {
4955 n = 0;
4956 while (*s >= '0' && *s <= '9') {
4957 n *= 10;
4958 n += *s - '0';
4959 s++;
4960 }
4961 tmp = (tmp << 8) + n;
4962 if (*s == '.')
4963 s++;
4964 }
4965 osversion = tmp;
4966 return osversion;
4967}
4968
Alexander Graf36c08d42011-11-02 20:23:24 +01004969
4970static int open_self_maps(void *cpu_env, int fd)
4971{
Alexander Graf1a49ef22012-05-01 16:30:28 +01004972#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Andreas Färber9349b4f2012-03-14 01:38:32 +01004973 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf1a49ef22012-05-01 16:30:28 +01004974#endif
4975 FILE *fp;
4976 char *line = NULL;
4977 size_t len = 0;
4978 ssize_t read;
Alexander Graf36c08d42011-11-02 20:23:24 +01004979
Alexander Graf1a49ef22012-05-01 16:30:28 +01004980 fp = fopen("/proc/self/maps", "r");
4981 if (fp == NULL) {
4982 return -EACCES;
4983 }
4984
4985 while ((read = getline(&line, &len, fp)) != -1) {
4986 int fields, dev_maj, dev_min, inode;
4987 uint64_t min, max, offset;
4988 char flag_r, flag_w, flag_x, flag_p;
4989 char path[512] = "";
4990 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
4991 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
4992 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
4993
4994 if ((fields < 10) || (fields > 11)) {
4995 continue;
4996 }
4997 if (!strncmp(path, "[stack]", 7)) {
4998 continue;
4999 }
5000 if (h2g_valid(min) && h2g_valid(max)) {
5001 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
5002 " %c%c%c%c %08" PRIx64 " %02x:%02x %d%s%s\n",
5003 h2g(min), h2g(max), flag_r, flag_w,
5004 flag_x, flag_p, offset, dev_maj, dev_min, inode,
5005 path[0] ? " " : "", path);
5006 }
5007 }
5008
5009 free(line);
5010 fclose(fp);
5011
5012#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Alexander Graf36c08d42011-11-02 20:23:24 +01005013 dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
5014 (unsigned long long)ts->info->stack_limit,
Alexander Graf1bdd7c72012-05-30 14:45:21 +02005015 (unsigned long long)(ts->info->start_stack +
5016 (TARGET_PAGE_SIZE - 1)) & TARGET_PAGE_MASK,
Alexander Graf1a49ef22012-05-01 16:30:28 +01005017 (unsigned long long)0);
5018#endif
Alexander Graf36c08d42011-11-02 20:23:24 +01005019
5020 return 0;
5021}
5022
Alexander Graf480b8e72011-11-02 20:23:25 +01005023static int open_self_stat(void *cpu_env, int fd)
5024{
Andreas Färber9349b4f2012-03-14 01:38:32 +01005025 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf480b8e72011-11-02 20:23:25 +01005026 abi_ulong start_stack = ts->info->start_stack;
5027 int i;
5028
5029 for (i = 0; i < 44; i++) {
5030 char buf[128];
5031 int len;
5032 uint64_t val = 0;
5033
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005034 if (i == 0) {
5035 /* pid */
5036 val = getpid();
5037 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5038 } else if (i == 1) {
5039 /* app name */
5040 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5041 } else if (i == 27) {
5042 /* stack bottom */
5043 val = start_stack;
5044 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5045 } else {
5046 /* for the rest, there is MasterCard */
5047 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
Alexander Graf480b8e72011-11-02 20:23:25 +01005048 }
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005049
Alexander Graf480b8e72011-11-02 20:23:25 +01005050 len = strlen(buf);
5051 if (write(fd, buf, len) != len) {
5052 return -1;
5053 }
5054 }
5055
5056 return 0;
5057}
5058
Alexander Graf257450e2011-11-02 20:23:26 +01005059static int open_self_auxv(void *cpu_env, int fd)
5060{
Andreas Färber9349b4f2012-03-14 01:38:32 +01005061 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01005062 abi_ulong auxv = ts->info->saved_auxv;
5063 abi_ulong len = ts->info->auxv_len;
5064 char *ptr;
5065
5066 /*
5067 * Auxiliary vector is stored in target process stack.
5068 * read in whole auxv vector and copy it to file
5069 */
5070 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5071 if (ptr != NULL) {
5072 while (len > 0) {
5073 ssize_t r;
5074 r = write(fd, ptr, len);
5075 if (r <= 0) {
5076 break;
5077 }
5078 len -= r;
5079 ptr += r;
5080 }
5081 lseek(fd, 0, SEEK_SET);
5082 unlock_user(ptr, auxv, len);
5083 }
5084
5085 return 0;
5086}
5087
Alexander Graf3be14d02011-11-02 20:23:23 +01005088static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
5089{
5090 struct fake_open {
5091 const char *filename;
5092 int (*fill)(void *cpu_env, int fd);
5093 };
5094 const struct fake_open *fake_open;
5095 static const struct fake_open fakes[] = {
Alexander Graf36c08d42011-11-02 20:23:24 +01005096 { "/proc/self/maps", open_self_maps },
Alexander Graf480b8e72011-11-02 20:23:25 +01005097 { "/proc/self/stat", open_self_stat },
Alexander Graf257450e2011-11-02 20:23:26 +01005098 { "/proc/self/auxv", open_self_auxv },
Alexander Graf3be14d02011-11-02 20:23:23 +01005099 { NULL, NULL }
5100 };
5101
5102 for (fake_open = fakes; fake_open->filename; fake_open++) {
5103 if (!strncmp(pathname, fake_open->filename,
5104 strlen(fake_open->filename))) {
5105 break;
5106 }
5107 }
5108
5109 if (fake_open->filename) {
5110 const char *tmpdir;
5111 char filename[PATH_MAX];
5112 int fd, r;
5113
5114 /* create temporary file to map stat to */
5115 tmpdir = getenv("TMPDIR");
5116 if (!tmpdir)
5117 tmpdir = "/tmp";
5118 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5119 fd = mkstemp(filename);
5120 if (fd < 0) {
5121 return fd;
5122 }
5123 unlink(filename);
5124
5125 if ((r = fake_open->fill(cpu_env, fd))) {
5126 close(fd);
5127 return r;
5128 }
5129 lseek(fd, 0, SEEK_SET);
5130
5131 return fd;
5132 }
5133
5134 return get_errno(open(path(pathname), flags, mode));
5135}
5136
ths0da46a62007-10-20 20:23:07 +00005137/* do_syscall() should always have a single exit point at the end so
5138 that actions, such as logging of syscall results, can be performed.
5139 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00005140abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5141 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01005142 abi_long arg5, abi_long arg6, abi_long arg7,
5143 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00005144{
blueswir1992f48a2007-10-14 16:27:31 +00005145 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005146 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00005147 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00005148 void *p;
ths3b46e622007-09-17 08:09:54 +00005149
bellard72f03902003-02-18 23:33:18 +00005150#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00005151 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00005152#endif
thsb92c47c2007-11-01 00:07:38 +00005153 if(do_strace)
5154 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5155
bellard31e31b82003-02-18 22:55:36 +00005156 switch(num) {
5157 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02005158#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00005159 /* In old applications this may be used to implement _exit(2).
5160 However in threaded applictions it is used for thread termination,
5161 and _exit_group is used for application termination.
5162 Do thread termination if we have more then one thread. */
5163 /* FIXME: This probably breaks if a signal arrives. We should probably
5164 be disabling signals. */
5165 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07005166 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005167 CPUArchState **lastp;
5168 CPUArchState *p;
pbrookc2764712009-03-07 15:24:59 +00005169
5170 cpu_list_lock();
5171 lastp = &first_cpu;
5172 p = first_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005173 while (p && p != (CPUArchState *)cpu_env) {
pbrookc2764712009-03-07 15:24:59 +00005174 lastp = &p->next_cpu;
5175 p = p->next_cpu;
5176 }
5177 /* If we didn't find the CPU for this thread then something is
5178 horribly wrong. */
5179 if (!p)
5180 abort();
5181 /* Remove the CPU from the list. */
5182 *lastp = p->next_cpu;
5183 cpu_list_unlock();
Andreas Färber9349b4f2012-03-14 01:38:32 +01005184 ts = ((CPUArchState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00005185 if (ts->child_tidptr) {
5186 put_user_u32(0, ts->child_tidptr);
5187 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5188 NULL, NULL, 0);
5189 }
Nathan Froyd48e15fc2010-10-29 07:48:57 -07005190 thread_env = NULL;
Andreas Färber11ea4092012-04-15 18:51:01 +02005191 object_delete(OBJECT(ENV_GET_CPU(cpu_env)));
Anthony Liguori7267c092011-08-20 22:09:37 -05005192 g_free(ts);
pbrookc2764712009-03-07 15:24:59 +00005193 pthread_exit(NULL);
5194 }
5195#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02005196#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00005197 _mcleanup();
5198#endif
bellarde9009672005-04-26 20:42:36 +00005199 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00005200 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00005201 ret = 0; /* avoid warning */
5202 break;
5203 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00005204 if (arg3 == 0)
5205 ret = 0;
5206 else {
5207 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5208 goto efault;
5209 ret = get_errno(read(arg1, p, arg3));
5210 unlock_user(p, arg2, ret);
5211 }
bellard31e31b82003-02-18 22:55:36 +00005212 break;
5213 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00005214 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5215 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005216 ret = get_errno(write(arg1, p, arg3));
5217 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00005218 break;
5219 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00005220 if (!(p = lock_user_string(arg1)))
5221 goto efault;
Alexander Graf3be14d02011-11-02 20:23:23 +01005222 ret = get_errno(do_open(cpu_env, p,
5223 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5224 arg3));
pbrook53a59602006-03-25 19:31:22 +00005225 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005226 break;
ths82424832007-09-24 09:21:55 +00005227#if defined(TARGET_NR_openat) && defined(__NR_openat)
5228 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00005229 if (!(p = lock_user_string(arg2)))
5230 goto efault;
5231 ret = get_errno(sys_openat(arg1,
5232 path(p),
5233 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5234 arg4));
5235 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00005236 break;
5237#endif
bellard31e31b82003-02-18 22:55:36 +00005238 case TARGET_NR_close:
5239 ret = get_errno(close(arg1));
5240 break;
5241 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00005242 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00005243 break;
5244 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00005245 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00005246 break;
thse5febef2007-04-01 18:31:35 +00005247#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00005248 case TARGET_NR_waitpid:
5249 {
pbrook53a59602006-03-25 19:31:22 +00005250 int status;
5251 ret = get_errno(waitpid(arg1, &status, arg3));
Alexander Graf53795572011-11-24 00:44:43 +01005252 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00005253 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00005254 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005255 }
5256 break;
thse5febef2007-04-01 18:31:35 +00005257#endif
pbrookf0cbb612008-05-30 18:20:05 +00005258#ifdef TARGET_NR_waitid
5259 case TARGET_NR_waitid:
5260 {
5261 siginfo_t info;
5262 info.si_pid = 0;
5263 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5264 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005265 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00005266 goto efault;
5267 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05005268 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00005269 }
5270 }
5271 break;
5272#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005273#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005274 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00005275 if (!(p = lock_user_string(arg1)))
5276 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005277 ret = get_errno(creat(p, arg2));
5278 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005279 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005280#endif
bellard31e31b82003-02-18 22:55:36 +00005281 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00005282 {
5283 void * p2;
5284 p = lock_user_string(arg1);
5285 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005286 if (!p || !p2)
5287 ret = -TARGET_EFAULT;
5288 else
5289 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005290 unlock_user(p2, arg2, 0);
5291 unlock_user(p, arg1, 0);
5292 }
bellard31e31b82003-02-18 22:55:36 +00005293 break;
ths64f0ce42007-09-24 09:25:06 +00005294#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
5295 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00005296 {
5297 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00005298 if (!arg2 || !arg4)
5299 goto efault;
ths64f0ce42007-09-24 09:25:06 +00005300 p = lock_user_string(arg2);
5301 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005302 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005303 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00005304 else
5305 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00005306 unlock_user(p, arg2, 0);
5307 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00005308 }
5309 break;
5310#endif
bellard31e31b82003-02-18 22:55:36 +00005311 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00005312 if (!(p = lock_user_string(arg1)))
5313 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005314 ret = get_errno(unlink(p));
5315 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005316 break;
ths8170f562007-09-24 09:24:11 +00005317#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
5318 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00005319 if (!(p = lock_user_string(arg2)))
5320 goto efault;
5321 ret = get_errno(sys_unlinkat(arg1, p, arg3));
5322 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00005323 break;
balrogb7d35e62007-12-12 00:40:24 +00005324#endif
bellard31e31b82003-02-18 22:55:36 +00005325 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00005326 {
5327 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00005328 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00005329 abi_ulong gp;
5330 abi_ulong guest_argp;
5331 abi_ulong guest_envp;
5332 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00005333 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005334 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00005335
bellardf7341ff2003-03-30 21:00:25 +00005336 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00005337 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00005338 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005339 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005340 goto efault;
ths03aa1972007-12-02 06:28:08 +00005341 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005342 break;
bellard7854b052003-03-29 17:22:23 +00005343 argc++;
bellard2f619692007-11-16 10:46:05 +00005344 }
bellardf7341ff2003-03-30 21:00:25 +00005345 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00005346 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00005347 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005348 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005349 goto efault;
ths03aa1972007-12-02 06:28:08 +00005350 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005351 break;
bellard7854b052003-03-29 17:22:23 +00005352 envc++;
bellard2f619692007-11-16 10:46:05 +00005353 }
bellard7854b052003-03-29 17:22:23 +00005354
bellardf7341ff2003-03-30 21:00:25 +00005355 argp = alloca((argc + 1) * sizeof(void *));
5356 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00005357
pbrookda94d262008-05-30 18:24:00 +00005358 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005359 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005360 if (get_user_ual(addr, gp))
5361 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005362 if (!addr)
5363 break;
bellard2f619692007-11-16 10:46:05 +00005364 if (!(*q = lock_user_string(addr)))
5365 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005366 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005367 }
bellardf7341ff2003-03-30 21:00:25 +00005368 *q = NULL;
5369
pbrookda94d262008-05-30 18:24:00 +00005370 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005371 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005372 if (get_user_ual(addr, gp))
5373 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005374 if (!addr)
5375 break;
bellard2f619692007-11-16 10:46:05 +00005376 if (!(*q = lock_user_string(addr)))
5377 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005378 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005379 }
bellardf7341ff2003-03-30 21:00:25 +00005380 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00005381
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005382 /* This case will not be caught by the host's execve() if its
5383 page size is bigger than the target's. */
5384 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5385 ret = -TARGET_E2BIG;
5386 goto execve_end;
5387 }
bellard2f619692007-11-16 10:46:05 +00005388 if (!(p = lock_user_string(arg1)))
5389 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005390 ret = get_errno(execve(p, argp, envp));
5391 unlock_user(p, arg1, 0);
5392
bellard2f619692007-11-16 10:46:05 +00005393 goto execve_end;
5394
5395 execve_efault:
5396 ret = -TARGET_EFAULT;
5397
5398 execve_end:
pbrook53a59602006-03-25 19:31:22 +00005399 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005400 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005401 if (get_user_ual(addr, gp)
5402 || !addr)
5403 break;
pbrook53a59602006-03-25 19:31:22 +00005404 unlock_user(*q, addr, 0);
5405 }
5406 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005407 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005408 if (get_user_ual(addr, gp)
5409 || !addr)
5410 break;
pbrook53a59602006-03-25 19:31:22 +00005411 unlock_user(*q, addr, 0);
5412 }
bellard7854b052003-03-29 17:22:23 +00005413 }
bellard31e31b82003-02-18 22:55:36 +00005414 break;
5415 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00005416 if (!(p = lock_user_string(arg1)))
5417 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005418 ret = get_errno(chdir(p));
5419 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005420 break;
bellarda315a142005-01-30 22:59:18 +00005421#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00005422 case TARGET_NR_time:
5423 {
pbrook53a59602006-03-25 19:31:22 +00005424 time_t host_time;
5425 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00005426 if (!is_error(ret)
5427 && arg1
5428 && put_user_sal(host_time, arg1))
5429 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005430 }
5431 break;
bellarda315a142005-01-30 22:59:18 +00005432#endif
bellard31e31b82003-02-18 22:55:36 +00005433 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00005434 if (!(p = lock_user_string(arg1)))
5435 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005436 ret = get_errno(mknod(p, arg2, arg3));
5437 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005438 break;
ths75ac37a2007-09-24 09:23:05 +00005439#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
5440 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00005441 if (!(p = lock_user_string(arg2)))
5442 goto efault;
5443 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
5444 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00005445 break;
5446#endif
bellard31e31b82003-02-18 22:55:36 +00005447 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00005448 if (!(p = lock_user_string(arg1)))
5449 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005450 ret = get_errno(chmod(p, arg2));
5451 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005452 break;
bellardebc05482003-09-30 21:08:41 +00005453#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00005454 case TARGET_NR_break:
5455 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005456#endif
5457#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00005458 case TARGET_NR_oldstat:
5459 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005460#endif
bellard31e31b82003-02-18 22:55:36 +00005461 case TARGET_NR_lseek:
5462 ret = get_errno(lseek(arg1, arg2, arg3));
5463 break;
Richard Henderson92317332010-05-03 10:07:53 -07005464#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5465 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00005466 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07005467 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00005468 ret = get_errno(getpid());
5469 break;
Richard Henderson92317332010-05-03 10:07:53 -07005470#endif
5471#ifdef TARGET_NR_getpid
5472 case TARGET_NR_getpid:
5473 ret = get_errno(getpid());
5474 break;
5475#endif
bellard31e31b82003-02-18 22:55:36 +00005476 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00005477 {
5478 /* need to look at the data field */
5479 void *p2, *p3;
5480 p = lock_user_string(arg1);
5481 p2 = lock_user_string(arg2);
5482 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005483 if (!p || !p2 || !p3)
5484 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005485 else {
bellard579a97f2007-11-11 14:26:47 +00005486 /* FIXME - arg5 should be locked, but it isn't clear how to
5487 * do that since it's not guaranteed to be a NULL-terminated
5488 * string.
5489 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005490 if ( ! arg5 )
5491 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
5492 else
5493 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
5494 }
bellard579a97f2007-11-11 14:26:47 +00005495 unlock_user(p, arg1, 0);
5496 unlock_user(p2, arg2, 0);
5497 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00005498 break;
5499 }
thse5febef2007-04-01 18:31:35 +00005500#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00005501 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00005502 if (!(p = lock_user_string(arg1)))
5503 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005504 ret = get_errno(umount(p));
5505 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005506 break;
thse5febef2007-04-01 18:31:35 +00005507#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005508#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005509 case TARGET_NR_stime:
5510 {
pbrook53a59602006-03-25 19:31:22 +00005511 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00005512 if (get_user_sal(host_time, arg1))
5513 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005514 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00005515 }
5516 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005517#endif
bellard31e31b82003-02-18 22:55:36 +00005518 case TARGET_NR_ptrace:
5519 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00005520#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005521 case TARGET_NR_alarm:
5522 ret = alarm(arg1);
5523 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005524#endif
bellardebc05482003-09-30 21:08:41 +00005525#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00005526 case TARGET_NR_oldfstat:
5527 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005528#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005529#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005530 case TARGET_NR_pause:
5531 ret = get_errno(pause());
5532 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005533#endif
thse5febef2007-04-01 18:31:35 +00005534#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00005535 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00005536 {
pbrook53a59602006-03-25 19:31:22 +00005537 struct utimbuf tbuf, *host_tbuf;
5538 struct target_utimbuf *target_tbuf;
5539 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005540 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5541 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005542 tbuf.actime = tswapal(target_tbuf->actime);
5543 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00005544 unlock_user_struct(target_tbuf, arg2, 0);
5545 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00005546 } else {
pbrook53a59602006-03-25 19:31:22 +00005547 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00005548 }
bellard579a97f2007-11-11 14:26:47 +00005549 if (!(p = lock_user_string(arg1)))
5550 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005551 ret = get_errno(utime(p, host_tbuf));
5552 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00005553 }
5554 break;
thse5febef2007-04-01 18:31:35 +00005555#endif
bellard978a66f2004-12-06 22:58:05 +00005556 case TARGET_NR_utimes:
5557 {
bellard978a66f2004-12-06 22:58:05 +00005558 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00005559 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00005560 if (copy_from_user_timeval(&tv[0], arg2)
5561 || copy_from_user_timeval(&tv[1],
5562 arg2 + sizeof(struct target_timeval)))
5563 goto efault;
bellard978a66f2004-12-06 22:58:05 +00005564 tvp = tv;
5565 } else {
5566 tvp = NULL;
5567 }
bellard579a97f2007-11-11 14:26:47 +00005568 if (!(p = lock_user_string(arg1)))
5569 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005570 ret = get_errno(utimes(p, tvp));
5571 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00005572 }
5573 break;
balrogac8a6552008-09-20 02:25:39 +00005574#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
5575 case TARGET_NR_futimesat:
5576 {
5577 struct timeval *tvp, tv[2];
5578 if (arg3) {
5579 if (copy_from_user_timeval(&tv[0], arg3)
5580 || copy_from_user_timeval(&tv[1],
5581 arg3 + sizeof(struct target_timeval)))
5582 goto efault;
5583 tvp = tv;
5584 } else {
5585 tvp = NULL;
5586 }
5587 if (!(p = lock_user_string(arg2)))
5588 goto efault;
5589 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
5590 unlock_user(p, arg2, 0);
5591 }
5592 break;
5593#endif
bellardebc05482003-09-30 21:08:41 +00005594#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00005595 case TARGET_NR_stty:
5596 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005597#endif
5598#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00005599 case TARGET_NR_gtty:
5600 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005601#endif
bellard31e31b82003-02-18 22:55:36 +00005602 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00005603 if (!(p = lock_user_string(arg1)))
5604 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02005605 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00005606 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005607 break;
ths92a34c12007-09-24 09:27:49 +00005608#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5609 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00005610 if (!(p = lock_user_string(arg2)))
5611 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005612 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005613 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00005614 break;
5615#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005616#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005617 case TARGET_NR_nice:
5618 ret = get_errno(nice(arg1));
5619 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005620#endif
bellardebc05482003-09-30 21:08:41 +00005621#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00005622 case TARGET_NR_ftime:
5623 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005624#endif
bellard31e31b82003-02-18 22:55:36 +00005625 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00005626 sync();
5627 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00005628 break;
5629 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00005630 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00005631 break;
5632 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00005633 {
5634 void *p2;
5635 p = lock_user_string(arg1);
5636 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005637 if (!p || !p2)
5638 ret = -TARGET_EFAULT;
5639 else
5640 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005641 unlock_user(p2, arg2, 0);
5642 unlock_user(p, arg1, 0);
5643 }
bellard31e31b82003-02-18 22:55:36 +00005644 break;
ths722183f2007-09-24 09:24:37 +00005645#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
5646 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00005647 {
bellard579a97f2007-11-11 14:26:47 +00005648 void *p2;
ths722183f2007-09-24 09:24:37 +00005649 p = lock_user_string(arg2);
5650 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005651 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005652 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00005653 else
5654 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00005655 unlock_user(p2, arg4, 0);
5656 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00005657 }
5658 break;
5659#endif
bellard31e31b82003-02-18 22:55:36 +00005660 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00005661 if (!(p = lock_user_string(arg1)))
5662 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005663 ret = get_errno(mkdir(p, arg2));
5664 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005665 break;
ths4472ad02007-09-24 09:22:32 +00005666#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
5667 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00005668 if (!(p = lock_user_string(arg2)))
5669 goto efault;
5670 ret = get_errno(sys_mkdirat(arg1, p, arg3));
5671 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00005672 break;
5673#endif
bellard31e31b82003-02-18 22:55:36 +00005674 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00005675 if (!(p = lock_user_string(arg1)))
5676 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005677 ret = get_errno(rmdir(p));
5678 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005679 break;
5680 case TARGET_NR_dup:
5681 ret = get_errno(dup(arg1));
5682 break;
5683 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005684 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00005685 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03005686#ifdef TARGET_NR_pipe2
5687 case TARGET_NR_pipe2:
Richard Hendersone7ea6cb2012-06-01 18:48:39 -07005688 ret = do_pipe(cpu_env, arg1,
5689 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03005690 break;
5691#endif
bellard31e31b82003-02-18 22:55:36 +00005692 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00005693 {
pbrook53a59602006-03-25 19:31:22 +00005694 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00005695 struct tms tms;
5696 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00005697 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00005698 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5699 if (!tmsp)
5700 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005701 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5702 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5703 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5704 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00005705 }
bellardc596ed12003-07-13 17:32:31 +00005706 if (!is_error(ret))
5707 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00005708 }
5709 break;
bellardebc05482003-09-30 21:08:41 +00005710#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00005711 case TARGET_NR_prof:
5712 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005713#endif
thse5febef2007-04-01 18:31:35 +00005714#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00005715 case TARGET_NR_signal:
5716 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005717#endif
bellard31e31b82003-02-18 22:55:36 +00005718 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00005719 if (arg1 == 0) {
5720 ret = get_errno(acct(NULL));
5721 } else {
5722 if (!(p = lock_user_string(arg1)))
5723 goto efault;
5724 ret = get_errno(acct(path(p)));
5725 unlock_user(p, arg1, 0);
5726 }
pbrook24836682006-04-16 14:14:53 +00005727 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005728#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005729 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00005730 if (!(p = lock_user_string(arg1)))
5731 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005732 ret = get_errno(umount2(p, arg2));
5733 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005734 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005735#endif
bellardebc05482003-09-30 21:08:41 +00005736#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00005737 case TARGET_NR_lock:
5738 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005739#endif
bellard31e31b82003-02-18 22:55:36 +00005740 case TARGET_NR_ioctl:
5741 ret = do_ioctl(arg1, arg2, arg3);
5742 break;
5743 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00005744 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00005745 break;
bellardebc05482003-09-30 21:08:41 +00005746#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00005747 case TARGET_NR_mpx:
5748 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005749#endif
bellard31e31b82003-02-18 22:55:36 +00005750 case TARGET_NR_setpgid:
5751 ret = get_errno(setpgid(arg1, arg2));
5752 break;
bellardebc05482003-09-30 21:08:41 +00005753#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00005754 case TARGET_NR_ulimit:
5755 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005756#endif
5757#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00005758 case TARGET_NR_oldolduname:
5759 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005760#endif
bellard31e31b82003-02-18 22:55:36 +00005761 case TARGET_NR_umask:
5762 ret = get_errno(umask(arg1));
5763 break;
5764 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00005765 if (!(p = lock_user_string(arg1)))
5766 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005767 ret = get_errno(chroot(p));
5768 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005769 break;
5770 case TARGET_NR_ustat:
5771 goto unimplemented;
5772 case TARGET_NR_dup2:
5773 ret = get_errno(dup2(arg1, arg2));
5774 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03005775#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5776 case TARGET_NR_dup3:
5777 ret = get_errno(dup3(arg1, arg2, arg3));
5778 break;
5779#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005780#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005781 case TARGET_NR_getppid:
5782 ret = get_errno(getppid());
5783 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005784#endif
bellard31e31b82003-02-18 22:55:36 +00005785 case TARGET_NR_getpgrp:
5786 ret = get_errno(getpgrp());
5787 break;
5788 case TARGET_NR_setsid:
5789 ret = get_errno(setsid());
5790 break;
thse5febef2007-04-01 18:31:35 +00005791#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00005792 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00005793 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005794#if defined(TARGET_ALPHA)
5795 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00005796 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00005797 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005798 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5799 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005800 act._sa_handler = old_act->_sa_handler;
5801 target_siginitset(&act.sa_mask, old_act->sa_mask);
5802 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005803 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00005804 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005805 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00005806 }
5807 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00005808 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005809 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5810 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005811 old_act->_sa_handler = oact._sa_handler;
5812 old_act->sa_mask = oact.sa_mask.sig[0];
5813 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00005814 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00005815 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005816#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00005817 struct target_sigaction act, oact, *pact, *old_act;
5818
5819 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005820 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5821 goto efault;
bellard106ec872006-06-27 21:08:10 +00005822 act._sa_handler = old_act->_sa_handler;
5823 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5824 act.sa_flags = old_act->sa_flags;
5825 unlock_user_struct(old_act, arg2, 0);
5826 pact = &act;
5827 } else {
5828 pact = NULL;
5829 }
5830
5831 ret = get_errno(do_sigaction(arg1, pact, &oact));
5832
5833 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005834 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5835 goto efault;
bellard106ec872006-06-27 21:08:10 +00005836 old_act->_sa_handler = oact._sa_handler;
5837 old_act->sa_flags = oact.sa_flags;
5838 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5839 old_act->sa_mask.sig[1] = 0;
5840 old_act->sa_mask.sig[2] = 0;
5841 old_act->sa_mask.sig[3] = 0;
5842 unlock_user_struct(old_act, arg3, 1);
5843 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005844#else
5845 struct target_old_sigaction *old_act;
5846 struct target_sigaction act, oact, *pact;
5847 if (arg2) {
5848 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5849 goto efault;
5850 act._sa_handler = old_act->_sa_handler;
5851 target_siginitset(&act.sa_mask, old_act->sa_mask);
5852 act.sa_flags = old_act->sa_flags;
5853 act.sa_restorer = old_act->sa_restorer;
5854 unlock_user_struct(old_act, arg2, 0);
5855 pact = &act;
5856 } else {
5857 pact = NULL;
5858 }
5859 ret = get_errno(do_sigaction(arg1, pact, &oact));
5860 if (!is_error(ret) && arg3) {
5861 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5862 goto efault;
5863 old_act->_sa_handler = oact._sa_handler;
5864 old_act->sa_mask = oact.sa_mask.sig[0];
5865 old_act->sa_flags = oact.sa_flags;
5866 old_act->sa_restorer = oact.sa_restorer;
5867 unlock_user_struct(old_act, arg3, 1);
5868 }
ths388bb212007-05-13 13:58:00 +00005869#endif
bellard31e31b82003-02-18 22:55:36 +00005870 }
5871 break;
thse5febef2007-04-01 18:31:35 +00005872#endif
bellard66fb9762003-03-23 01:06:05 +00005873 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00005874 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005875#if defined(TARGET_ALPHA)
5876 struct target_sigaction act, oact, *pact = 0;
5877 struct target_rt_sigaction *rt_act;
5878 /* ??? arg4 == sizeof(sigset_t). */
5879 if (arg2) {
5880 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5881 goto efault;
5882 act._sa_handler = rt_act->_sa_handler;
5883 act.sa_mask = rt_act->sa_mask;
5884 act.sa_flags = rt_act->sa_flags;
5885 act.sa_restorer = arg5;
5886 unlock_user_struct(rt_act, arg2, 0);
5887 pact = &act;
5888 }
5889 ret = get_errno(do_sigaction(arg1, pact, &oact));
5890 if (!is_error(ret) && arg3) {
5891 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5892 goto efault;
5893 rt_act->_sa_handler = oact._sa_handler;
5894 rt_act->sa_mask = oact.sa_mask;
5895 rt_act->sa_flags = oact.sa_flags;
5896 unlock_user_struct(rt_act, arg3, 1);
5897 }
5898#else
pbrook53a59602006-03-25 19:31:22 +00005899 struct target_sigaction *act;
5900 struct target_sigaction *oact;
5901
bellard579a97f2007-11-11 14:26:47 +00005902 if (arg2) {
5903 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5904 goto efault;
5905 } else
pbrook53a59602006-03-25 19:31:22 +00005906 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005907 if (arg3) {
5908 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5909 ret = -TARGET_EFAULT;
5910 goto rt_sigaction_fail;
5911 }
5912 } else
pbrook53a59602006-03-25 19:31:22 +00005913 oact = NULL;
5914 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005915 rt_sigaction_fail:
5916 if (act)
pbrook53a59602006-03-25 19:31:22 +00005917 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005918 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005919 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005920#endif
pbrook53a59602006-03-25 19:31:22 +00005921 }
bellard66fb9762003-03-23 01:06:05 +00005922 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005923#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005924 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005925 {
5926 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005927 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005928 sigprocmask(0, NULL, &cur_set);
5929 host_to_target_old_sigset(&target_set, &cur_set);
5930 ret = target_set;
5931 }
5932 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005933#endif
5934#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005935 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005936 {
5937 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005938 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00005939 sigprocmask(0, NULL, &cur_set);
5940 target_to_host_old_sigset(&set, &target_set);
5941 sigorset(&set, &set, &cur_set);
5942 sigprocmask(SIG_SETMASK, &set, &oset);
5943 host_to_target_old_sigset(&target_set, &oset);
5944 ret = target_set;
5945 }
5946 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005947#endif
thse5febef2007-04-01 18:31:35 +00005948#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00005949 case TARGET_NR_sigprocmask:
5950 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005951#if defined(TARGET_ALPHA)
5952 sigset_t set, oldset;
5953 abi_ulong mask;
5954 int how;
5955
5956 switch (arg1) {
5957 case TARGET_SIG_BLOCK:
5958 how = SIG_BLOCK;
5959 break;
5960 case TARGET_SIG_UNBLOCK:
5961 how = SIG_UNBLOCK;
5962 break;
5963 case TARGET_SIG_SETMASK:
5964 how = SIG_SETMASK;
5965 break;
5966 default:
5967 ret = -TARGET_EINVAL;
5968 goto fail;
5969 }
5970 mask = arg2;
5971 target_to_host_old_sigset(&set, &mask);
5972
5973 ret = get_errno(sigprocmask(how, &set, &oldset));
Richard Hendersona5b3b132010-05-03 10:07:55 -07005974 if (!is_error(ret)) {
5975 host_to_target_old_sigset(&mask, &oldset);
5976 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07005977 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07005978 }
5979#else
bellard66fb9762003-03-23 01:06:05 +00005980 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07005981 int how;
ths3b46e622007-09-17 08:09:54 +00005982
pbrook53a59602006-03-25 19:31:22 +00005983 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005984 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00005985 case TARGET_SIG_BLOCK:
5986 how = SIG_BLOCK;
5987 break;
5988 case TARGET_SIG_UNBLOCK:
5989 how = SIG_UNBLOCK;
5990 break;
5991 case TARGET_SIG_SETMASK:
5992 how = SIG_SETMASK;
5993 break;
5994 default:
ths0da46a62007-10-20 20:23:07 +00005995 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005996 goto fail;
5997 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005998 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005999 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006000 target_to_host_old_sigset(&set, p);
6001 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006002 set_ptr = &set;
6003 } else {
6004 how = 0;
6005 set_ptr = NULL;
6006 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07006007 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006008 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006009 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006010 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006011 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006012 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006013 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07006014#endif
bellard66fb9762003-03-23 01:06:05 +00006015 }
6016 break;
thse5febef2007-04-01 18:31:35 +00006017#endif
bellard66fb9762003-03-23 01:06:05 +00006018 case TARGET_NR_rt_sigprocmask:
6019 {
6020 int how = arg1;
6021 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00006022
pbrook53a59602006-03-25 19:31:22 +00006023 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006024 switch(how) {
6025 case TARGET_SIG_BLOCK:
6026 how = SIG_BLOCK;
6027 break;
6028 case TARGET_SIG_UNBLOCK:
6029 how = SIG_UNBLOCK;
6030 break;
6031 case TARGET_SIG_SETMASK:
6032 how = SIG_SETMASK;
6033 break;
6034 default:
ths0da46a62007-10-20 20:23:07 +00006035 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006036 goto fail;
6037 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006038 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006039 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006040 target_to_host_sigset(&set, p);
6041 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006042 set_ptr = &set;
6043 } else {
6044 how = 0;
6045 set_ptr = NULL;
6046 }
6047 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006048 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006049 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006050 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006051 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006052 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006053 }
6054 }
6055 break;
thse5febef2007-04-01 18:31:35 +00006056#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00006057 case TARGET_NR_sigpending:
6058 {
6059 sigset_t set;
6060 ret = get_errno(sigpending(&set));
6061 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006062 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006063 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006064 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006065 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006066 }
6067 }
6068 break;
thse5febef2007-04-01 18:31:35 +00006069#endif
bellard66fb9762003-03-23 01:06:05 +00006070 case TARGET_NR_rt_sigpending:
6071 {
6072 sigset_t set;
6073 ret = get_errno(sigpending(&set));
6074 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006075 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006076 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006077 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006078 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006079 }
6080 }
6081 break;
thse5febef2007-04-01 18:31:35 +00006082#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00006083 case TARGET_NR_sigsuspend:
6084 {
6085 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07006086#if defined(TARGET_ALPHA)
6087 abi_ulong mask = arg1;
6088 target_to_host_old_sigset(&set, &mask);
6089#else
Anthony Liguoric227f092009-10-01 16:12:16 -05006090 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006091 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006092 target_to_host_old_sigset(&set, p);
6093 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07006094#endif
bellard66fb9762003-03-23 01:06:05 +00006095 ret = get_errno(sigsuspend(&set));
6096 }
6097 break;
thse5febef2007-04-01 18:31:35 +00006098#endif
bellard66fb9762003-03-23 01:06:05 +00006099 case TARGET_NR_rt_sigsuspend:
6100 {
6101 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05006102 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006103 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006104 target_to_host_sigset(&set, p);
6105 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006106 ret = get_errno(sigsuspend(&set));
6107 }
6108 break;
6109 case TARGET_NR_rt_sigtimedwait:
6110 {
bellard66fb9762003-03-23 01:06:05 +00006111 sigset_t set;
6112 struct timespec uts, *puts;
6113 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00006114
Anthony Liguoric227f092009-10-01 16:12:16 -05006115 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006116 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006117 target_to_host_sigset(&set, p);
6118 unlock_user(p, arg1, 0);
6119 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00006120 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00006121 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00006122 } else {
6123 puts = NULL;
6124 }
6125 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00006126 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006127 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006128 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006129 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05006130 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00006131 }
6132 }
6133 break;
6134 case TARGET_NR_rt_sigqueueinfo:
6135 {
6136 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05006137 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006138 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006139 target_to_host_siginfo(&uinfo, p);
6140 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006141 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6142 }
6143 break;
thse5febef2007-04-01 18:31:35 +00006144#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00006145 case TARGET_NR_sigreturn:
6146 /* NOTE: ret is eax, so not transcoding must be done */
6147 ret = do_sigreturn(cpu_env);
6148 break;
thse5febef2007-04-01 18:31:35 +00006149#endif
bellard66fb9762003-03-23 01:06:05 +00006150 case TARGET_NR_rt_sigreturn:
6151 /* NOTE: ret is eax, so not transcoding must be done */
6152 ret = do_rt_sigreturn(cpu_env);
6153 break;
bellard31e31b82003-02-18 22:55:36 +00006154 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00006155 if (!(p = lock_user_string(arg1)))
6156 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006157 ret = get_errno(sethostname(p, arg2));
6158 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006159 break;
6160 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00006161 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006162 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006163 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006164 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00006165 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6166 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006167 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6168 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006169 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00006170 ret = get_errno(setrlimit(resource, &rlim));
6171 }
6172 break;
bellard31e31b82003-02-18 22:55:36 +00006173 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00006174 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006175 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006176 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006177 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00006178
bellard9de5e442003-03-23 16:49:39 +00006179 ret = get_errno(getrlimit(resource, &rlim));
6180 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006181 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6182 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006183 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6184 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006185 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00006186 }
6187 }
6188 break;
bellard31e31b82003-02-18 22:55:36 +00006189 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00006190 {
6191 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00006192 ret = get_errno(getrusage(arg1, &rusage));
6193 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006194 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00006195 }
6196 }
6197 break;
bellard31e31b82003-02-18 22:55:36 +00006198 case TARGET_NR_gettimeofday:
6199 {
bellard31e31b82003-02-18 22:55:36 +00006200 struct timeval tv;
6201 ret = get_errno(gettimeofday(&tv, NULL));
6202 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00006203 if (copy_to_user_timeval(arg1, &tv))
6204 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006205 }
6206 }
6207 break;
6208 case TARGET_NR_settimeofday:
6209 {
bellard31e31b82003-02-18 22:55:36 +00006210 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00006211 if (copy_from_user_timeval(&tv, arg1))
6212 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006213 ret = get_errno(settimeofday(&tv, NULL));
6214 }
6215 break;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006216#if defined(TARGET_NR_select) && !defined(TARGET_S390X) && !defined(TARGET_S390)
bellard31e31b82003-02-18 22:55:36 +00006217 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00006218 {
pbrook53a59602006-03-25 19:31:22 +00006219 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00006220 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00006221 long nsel;
6222
bellard579a97f2007-11-11 14:26:47 +00006223 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6224 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006225 nsel = tswapal(sel->n);
6226 inp = tswapal(sel->inp);
6227 outp = tswapal(sel->outp);
6228 exp = tswapal(sel->exp);
6229 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00006230 unlock_user_struct(sel, arg1, 0);
6231 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00006232 }
6233 break;
bellard048f6b42005-11-26 18:47:20 +00006234#endif
Riku Voipio9e423822010-05-07 12:28:05 +00006235#ifdef TARGET_NR_pselect6
6236 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04006237 {
6238 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6239 fd_set rfds, wfds, efds;
6240 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6241 struct timespec ts, *ts_ptr;
6242
6243 /*
6244 * The 6th arg is actually two args smashed together,
6245 * so we cannot use the C library.
6246 */
6247 sigset_t set;
6248 struct {
6249 sigset_t *set;
6250 size_t size;
6251 } sig, *sig_ptr;
6252
6253 abi_ulong arg_sigset, arg_sigsize, *arg7;
6254 target_sigset_t *target_sigset;
6255
6256 n = arg1;
6257 rfd_addr = arg2;
6258 wfd_addr = arg3;
6259 efd_addr = arg4;
6260 ts_addr = arg5;
6261
6262 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6263 if (ret) {
6264 goto fail;
6265 }
6266 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6267 if (ret) {
6268 goto fail;
6269 }
6270 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6271 if (ret) {
6272 goto fail;
6273 }
6274
6275 /*
6276 * This takes a timespec, and not a timeval, so we cannot
6277 * use the do_select() helper ...
6278 */
6279 if (ts_addr) {
6280 if (target_to_host_timespec(&ts, ts_addr)) {
6281 goto efault;
6282 }
6283 ts_ptr = &ts;
6284 } else {
6285 ts_ptr = NULL;
6286 }
6287
6288 /* Extract the two packed args for the sigset */
6289 if (arg6) {
6290 sig_ptr = &sig;
6291 sig.size = _NSIG / 8;
6292
6293 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6294 if (!arg7) {
6295 goto efault;
6296 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006297 arg_sigset = tswapal(arg7[0]);
6298 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04006299 unlock_user(arg7, arg6, 0);
6300
6301 if (arg_sigset) {
6302 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01006303 if (arg_sigsize != sizeof(*target_sigset)) {
6304 /* Like the kernel, we enforce correct size sigsets */
6305 ret = -TARGET_EINVAL;
6306 goto fail;
6307 }
Mike Frysinger055e0902011-06-03 17:01:49 -04006308 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6309 sizeof(*target_sigset), 1);
6310 if (!target_sigset) {
6311 goto efault;
6312 }
6313 target_to_host_sigset(&set, target_sigset);
6314 unlock_user(target_sigset, arg_sigset, 0);
6315 } else {
6316 sig.set = NULL;
6317 }
6318 } else {
6319 sig_ptr = NULL;
6320 }
6321
6322 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6323 ts_ptr, sig_ptr));
6324
6325 if (!is_error(ret)) {
6326 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6327 goto efault;
6328 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6329 goto efault;
6330 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6331 goto efault;
6332
6333 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6334 goto efault;
6335 }
6336 }
6337 break;
Riku Voipio9e423822010-05-07 12:28:05 +00006338#endif
bellard31e31b82003-02-18 22:55:36 +00006339 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00006340 {
6341 void *p2;
6342 p = lock_user_string(arg1);
6343 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006344 if (!p || !p2)
6345 ret = -TARGET_EFAULT;
6346 else
6347 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006348 unlock_user(p2, arg2, 0);
6349 unlock_user(p, arg1, 0);
6350 }
bellard31e31b82003-02-18 22:55:36 +00006351 break;
thsf0b62432007-09-24 09:25:40 +00006352#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
6353 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00006354 {
bellard579a97f2007-11-11 14:26:47 +00006355 void *p2;
thsf0b62432007-09-24 09:25:40 +00006356 p = lock_user_string(arg1);
6357 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00006358 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006359 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00006360 else
6361 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00006362 unlock_user(p2, arg3, 0);
6363 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00006364 }
6365 break;
6366#endif
bellardebc05482003-09-30 21:08:41 +00006367#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00006368 case TARGET_NR_oldlstat:
6369 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006370#endif
bellard31e31b82003-02-18 22:55:36 +00006371 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00006372 {
aurel32d088d662009-01-30 20:09:01 +00006373 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00006374 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00006375 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
6376 if (!p || !p2)
6377 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00006378 else {
6379 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
6380 char real[PATH_MAX];
6381 temp = realpath(exec_path,real);
6382 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
6383 snprintf((char *)p2, arg3, "%s", real);
6384 }
6385 else
6386 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00006387 }
pbrook53a59602006-03-25 19:31:22 +00006388 unlock_user(p2, arg2, ret);
6389 unlock_user(p, arg1, 0);
6390 }
bellard31e31b82003-02-18 22:55:36 +00006391 break;
ths5e0ccb12007-09-24 09:26:10 +00006392#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
6393 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00006394 {
bellard579a97f2007-11-11 14:26:47 +00006395 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00006396 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006397 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
6398 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006399 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00006400 else
6401 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00006402 unlock_user(p2, arg3, ret);
6403 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00006404 }
6405 break;
6406#endif
thse5febef2007-04-01 18:31:35 +00006407#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00006408 case TARGET_NR_uselib:
6409 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006410#endif
6411#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00006412 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00006413 if (!(p = lock_user_string(arg1)))
6414 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006415 ret = get_errno(swapon(p, arg2));
6416 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006417 break;
thse5febef2007-04-01 18:31:35 +00006418#endif
bellard31e31b82003-02-18 22:55:36 +00006419 case TARGET_NR_reboot:
Alexander Graf0f6b4d22011-09-27 14:39:42 +02006420 if (!(p = lock_user_string(arg4)))
6421 goto efault;
6422 ret = reboot(arg1, arg2, arg3, p);
6423 unlock_user(p, arg4, 0);
6424 break;
thse5febef2007-04-01 18:31:35 +00006425#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00006426 case TARGET_NR_readdir:
6427 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006428#endif
6429#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00006430 case TARGET_NR_mmap:
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006431#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || \
6432 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6433 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00006434 {
blueswir1992f48a2007-10-14 16:27:31 +00006435 abi_ulong *v;
6436 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00006437 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6438 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006439 v1 = tswapal(v[0]);
6440 v2 = tswapal(v[1]);
6441 v3 = tswapal(v[2]);
6442 v4 = tswapal(v[3]);
6443 v5 = tswapal(v[4]);
6444 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00006445 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00006446 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00006447 target_to_host_bitmask(v4, mmap_flags_tbl),
6448 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00006449 }
bellard31e31b82003-02-18 22:55:36 +00006450#else
ths5fafdf22007-09-16 21:08:06 +00006451 ret = get_errno(target_mmap(arg1, arg2, arg3,
6452 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00006453 arg5,
6454 arg6));
bellard31e31b82003-02-18 22:55:36 +00006455#endif
bellard6fb883e2003-07-09 17:12:39 +00006456 break;
thse5febef2007-04-01 18:31:35 +00006457#endif
bellarda315a142005-01-30 22:59:18 +00006458#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00006459 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00006460#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00006461#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00006462#endif
ths5fafdf22007-09-16 21:08:06 +00006463 ret = get_errno(target_mmap(arg1, arg2, arg3,
6464 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00006465 arg5,
bellardc573ff62004-01-04 15:51:36 +00006466 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00006467 break;
bellarda315a142005-01-30 22:59:18 +00006468#endif
bellard31e31b82003-02-18 22:55:36 +00006469 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00006470 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006471 break;
bellard9de5e442003-03-23 16:49:39 +00006472 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01006473 {
Andreas Färber9349b4f2012-03-14 01:38:32 +01006474 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Paul Brook97374d32010-06-16 13:03:51 +01006475 /* Special hack to detect libc making the stack executable. */
6476 if ((arg3 & PROT_GROWSDOWN)
6477 && arg1 >= ts->info->stack_limit
6478 && arg1 <= ts->info->start_stack) {
6479 arg3 &= ~PROT_GROWSDOWN;
6480 arg2 = arg2 + arg1 - ts->info->stack_limit;
6481 arg1 = ts->info->stack_limit;
6482 }
6483 }
bellard54936002003-05-13 00:25:15 +00006484 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006485 break;
thse5febef2007-04-01 18:31:35 +00006486#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00006487 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00006488 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00006489 break;
thse5febef2007-04-01 18:31:35 +00006490#endif
pbrook53a59602006-03-25 19:31:22 +00006491 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00006492#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00006493 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00006494 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006495 break;
thse5febef2007-04-01 18:31:35 +00006496#endif
6497#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00006498 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00006499 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006500 break;
thse5febef2007-04-01 18:31:35 +00006501#endif
6502#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00006503 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00006504 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006505 break;
thse5febef2007-04-01 18:31:35 +00006506#endif
6507#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00006508 case TARGET_NR_mlockall:
6509 ret = get_errno(mlockall(arg1));
6510 break;
thse5febef2007-04-01 18:31:35 +00006511#endif
6512#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00006513 case TARGET_NR_munlockall:
6514 ret = get_errno(munlockall());
6515 break;
thse5febef2007-04-01 18:31:35 +00006516#endif
bellard31e31b82003-02-18 22:55:36 +00006517 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00006518 if (!(p = lock_user_string(arg1)))
6519 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006520 ret = get_errno(truncate(p, arg2));
6521 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006522 break;
6523 case TARGET_NR_ftruncate:
6524 ret = get_errno(ftruncate(arg1, arg2));
6525 break;
6526 case TARGET_NR_fchmod:
6527 ret = get_errno(fchmod(arg1, arg2));
6528 break;
ths814d7972007-09-24 09:26:51 +00006529#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
6530 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00006531 if (!(p = lock_user_string(arg2)))
6532 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00006533 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00006534 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00006535 break;
6536#endif
bellard31e31b82003-02-18 22:55:36 +00006537 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -07006538 /* Note that negative values are valid for getpriority, so we must
6539 differentiate based on errno settings. */
6540 errno = 0;
6541 ret = getpriority(arg1, arg2);
6542 if (ret == -1 && errno != 0) {
6543 ret = -host_to_target_errno(errno);
6544 break;
6545 }
6546#ifdef TARGET_ALPHA
6547 /* Return value is the unbiased priority. Signal no error. */
6548 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
6549#else
6550 /* Return value is a biased priority to avoid negative numbers. */
6551 ret = 20 - ret;
6552#endif
bellard31e31b82003-02-18 22:55:36 +00006553 break;
6554 case TARGET_NR_setpriority:
6555 ret = get_errno(setpriority(arg1, arg2, arg3));
6556 break;
bellardebc05482003-09-30 21:08:41 +00006557#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00006558 case TARGET_NR_profil:
6559 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006560#endif
bellard31e31b82003-02-18 22:55:36 +00006561 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00006562 if (!(p = lock_user_string(arg1)))
6563 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006564 ret = get_errno(statfs(path(p), &stfs));
6565 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006566 convert_statfs:
6567 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006568 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006569
bellard579a97f2007-11-11 14:26:47 +00006570 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6571 goto efault;
6572 __put_user(stfs.f_type, &target_stfs->f_type);
6573 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6574 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6575 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6576 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6577 __put_user(stfs.f_files, &target_stfs->f_files);
6578 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6579 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6580 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6581 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02006582 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6583 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +00006584 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006585 }
6586 break;
6587 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00006588 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00006589 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00006590#ifdef TARGET_NR_statfs64
6591 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00006592 if (!(p = lock_user_string(arg1)))
6593 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006594 ret = get_errno(statfs(path(p), &stfs));
6595 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00006596 convert_statfs64:
6597 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006598 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006599
bellard579a97f2007-11-11 14:26:47 +00006600 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6601 goto efault;
6602 __put_user(stfs.f_type, &target_stfs->f_type);
6603 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6604 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6605 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6606 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6607 __put_user(stfs.f_files, &target_stfs->f_files);
6608 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6609 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6610 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6611 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02006612 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6613 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +00006614 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00006615 }
6616 break;
6617 case TARGET_NR_fstatfs64:
6618 ret = get_errno(fstatfs(arg1, &stfs));
6619 goto convert_statfs64;
6620#endif
bellardebc05482003-09-30 21:08:41 +00006621#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00006622 case TARGET_NR_ioperm:
6623 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006624#endif
thse5febef2007-04-01 18:31:35 +00006625#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00006626 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00006627 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00006628 break;
thse5febef2007-04-01 18:31:35 +00006629#endif
bellard3532fa72006-06-24 15:06:03 +00006630#ifdef TARGET_NR_accept
6631 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00006632 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006633 break;
6634#endif
6635#ifdef TARGET_NR_bind
6636 case TARGET_NR_bind:
6637 ret = do_bind(arg1, arg2, arg3);
6638 break;
6639#endif
6640#ifdef TARGET_NR_connect
6641 case TARGET_NR_connect:
6642 ret = do_connect(arg1, arg2, arg3);
6643 break;
6644#endif
6645#ifdef TARGET_NR_getpeername
6646 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00006647 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006648 break;
6649#endif
6650#ifdef TARGET_NR_getsockname
6651 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00006652 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006653 break;
6654#endif
6655#ifdef TARGET_NR_getsockopt
6656 case TARGET_NR_getsockopt:
6657 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6658 break;
6659#endif
6660#ifdef TARGET_NR_listen
6661 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00006662 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006663 break;
6664#endif
6665#ifdef TARGET_NR_recv
6666 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00006667 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006668 break;
6669#endif
6670#ifdef TARGET_NR_recvfrom
6671 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00006672 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006673 break;
6674#endif
6675#ifdef TARGET_NR_recvmsg
6676 case TARGET_NR_recvmsg:
6677 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6678 break;
6679#endif
6680#ifdef TARGET_NR_send
6681 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00006682 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006683 break;
6684#endif
6685#ifdef TARGET_NR_sendmsg
6686 case TARGET_NR_sendmsg:
6687 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6688 break;
6689#endif
6690#ifdef TARGET_NR_sendto
6691 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00006692 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006693 break;
6694#endif
6695#ifdef TARGET_NR_shutdown
6696 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00006697 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006698 break;
6699#endif
6700#ifdef TARGET_NR_socket
6701 case TARGET_NR_socket:
6702 ret = do_socket(arg1, arg2, arg3);
6703 break;
6704#endif
6705#ifdef TARGET_NR_socketpair
6706 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00006707 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00006708 break;
6709#endif
6710#ifdef TARGET_NR_setsockopt
6711 case TARGET_NR_setsockopt:
6712 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6713 break;
6714#endif
ths7494b0f2007-02-11 18:26:53 +00006715
bellard31e31b82003-02-18 22:55:36 +00006716 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00006717 if (!(p = lock_user_string(arg2)))
6718 goto efault;
thse5574482007-02-11 20:03:13 +00006719 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6720 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00006721 break;
6722
bellard31e31b82003-02-18 22:55:36 +00006723 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00006724 {
bellard66fb9762003-03-23 01:06:05 +00006725 struct itimerval value, ovalue, *pvalue;
6726
pbrook53a59602006-03-25 19:31:22 +00006727 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006728 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00006729 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6730 || copy_from_user_timeval(&pvalue->it_value,
6731 arg2 + sizeof(struct target_timeval)))
6732 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006733 } else {
6734 pvalue = NULL;
6735 }
6736 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00006737 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00006738 if (copy_to_user_timeval(arg3,
6739 &ovalue.it_interval)
6740 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6741 &ovalue.it_value))
6742 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006743 }
6744 }
6745 break;
bellard31e31b82003-02-18 22:55:36 +00006746 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00006747 {
bellard66fb9762003-03-23 01:06:05 +00006748 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00006749
bellard66fb9762003-03-23 01:06:05 +00006750 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00006751 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00006752 if (copy_to_user_timeval(arg2,
6753 &value.it_interval)
6754 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6755 &value.it_value))
6756 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006757 }
6758 }
6759 break;
bellard31e31b82003-02-18 22:55:36 +00006760 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00006761 if (!(p = lock_user_string(arg1)))
6762 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006763 ret = get_errno(stat(path(p), &st));
6764 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006765 goto do_stat;
6766 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00006767 if (!(p = lock_user_string(arg1)))
6768 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006769 ret = get_errno(lstat(path(p), &st));
6770 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006771 goto do_stat;
6772 case TARGET_NR_fstat:
6773 {
6774 ret = get_errno(fstat(arg1, &st));
6775 do_stat:
6776 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006777 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00006778
bellard579a97f2007-11-11 14:26:47 +00006779 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6780 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02006781 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00006782 __put_user(st.st_dev, &target_st->st_dev);
6783 __put_user(st.st_ino, &target_st->st_ino);
6784 __put_user(st.st_mode, &target_st->st_mode);
6785 __put_user(st.st_uid, &target_st->st_uid);
6786 __put_user(st.st_gid, &target_st->st_gid);
6787 __put_user(st.st_nlink, &target_st->st_nlink);
6788 __put_user(st.st_rdev, &target_st->st_rdev);
6789 __put_user(st.st_size, &target_st->st_size);
6790 __put_user(st.st_blksize, &target_st->st_blksize);
6791 __put_user(st.st_blocks, &target_st->st_blocks);
6792 __put_user(st.st_atime, &target_st->target_st_atime);
6793 __put_user(st.st_mtime, &target_st->target_st_mtime);
6794 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00006795 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006796 }
6797 }
6798 break;
bellardebc05482003-09-30 21:08:41 +00006799#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00006800 case TARGET_NR_olduname:
6801 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006802#endif
6803#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00006804 case TARGET_NR_iopl:
6805 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006806#endif
bellard31e31b82003-02-18 22:55:36 +00006807 case TARGET_NR_vhangup:
6808 ret = get_errno(vhangup());
6809 break;
bellardebc05482003-09-30 21:08:41 +00006810#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00006811 case TARGET_NR_idle:
6812 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006813#endif
bellard42ad6ae2005-01-03 22:48:11 +00006814#ifdef TARGET_NR_syscall
6815 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01006816 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6817 arg6, arg7, arg8, 0);
6818 break;
bellard42ad6ae2005-01-03 22:48:11 +00006819#endif
bellard31e31b82003-02-18 22:55:36 +00006820 case TARGET_NR_wait4:
6821 {
6822 int status;
blueswir1992f48a2007-10-14 16:27:31 +00006823 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00006824 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00006825 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00006826 if (target_rusage)
6827 rusage_ptr = &rusage;
6828 else
6829 rusage_ptr = NULL;
6830 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6831 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01006832 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00006833 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00006834 if (put_user_s32(status, status_ptr))
6835 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006836 }
bellard2f619692007-11-16 10:46:05 +00006837 if (target_rusage)
6838 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00006839 }
6840 }
6841 break;
thse5febef2007-04-01 18:31:35 +00006842#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00006843 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00006844 if (!(p = lock_user_string(arg1)))
6845 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006846 ret = get_errno(swapoff(p));
6847 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006848 break;
thse5febef2007-04-01 18:31:35 +00006849#endif
bellard31e31b82003-02-18 22:55:36 +00006850 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00006851 {
pbrook53a59602006-03-25 19:31:22 +00006852 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00006853 struct sysinfo value;
6854 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00006855 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00006856 {
bellard579a97f2007-11-11 14:26:47 +00006857 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6858 goto efault;
bellarda5448a72004-06-19 16:59:03 +00006859 __put_user(value.uptime, &target_value->uptime);
6860 __put_user(value.loads[0], &target_value->loads[0]);
6861 __put_user(value.loads[1], &target_value->loads[1]);
6862 __put_user(value.loads[2], &target_value->loads[2]);
6863 __put_user(value.totalram, &target_value->totalram);
6864 __put_user(value.freeram, &target_value->freeram);
6865 __put_user(value.sharedram, &target_value->sharedram);
6866 __put_user(value.bufferram, &target_value->bufferram);
6867 __put_user(value.totalswap, &target_value->totalswap);
6868 __put_user(value.freeswap, &target_value->freeswap);
6869 __put_user(value.procs, &target_value->procs);
6870 __put_user(value.totalhigh, &target_value->totalhigh);
6871 __put_user(value.freehigh, &target_value->freehigh);
6872 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00006873 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00006874 }
6875 }
6876 break;
thse5febef2007-04-01 18:31:35 +00006877#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00006878 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00006879 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6880 break;
thse5febef2007-04-01 18:31:35 +00006881#endif
aurel32e5289082009-04-18 16:16:12 +00006882#ifdef TARGET_NR_semget
6883 case TARGET_NR_semget:
6884 ret = get_errno(semget(arg1, arg2, arg3));
6885 break;
6886#endif
6887#ifdef TARGET_NR_semop
6888 case TARGET_NR_semop:
6889 ret = get_errno(do_semop(arg1, arg2, arg3));
6890 break;
6891#endif
6892#ifdef TARGET_NR_semctl
6893 case TARGET_NR_semctl:
6894 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6895 break;
6896#endif
aurel32eeb438c2008-10-13 21:08:55 +00006897#ifdef TARGET_NR_msgctl
6898 case TARGET_NR_msgctl:
6899 ret = do_msgctl(arg1, arg2, arg3);
6900 break;
6901#endif
6902#ifdef TARGET_NR_msgget
6903 case TARGET_NR_msgget:
6904 ret = get_errno(msgget(arg1, arg2));
6905 break;
6906#endif
6907#ifdef TARGET_NR_msgrcv
6908 case TARGET_NR_msgrcv:
6909 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
6910 break;
6911#endif
6912#ifdef TARGET_NR_msgsnd
6913 case TARGET_NR_msgsnd:
6914 ret = do_msgsnd(arg1, arg2, arg3, arg4);
6915 break;
6916#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03006917#ifdef TARGET_NR_shmget
6918 case TARGET_NR_shmget:
6919 ret = get_errno(shmget(arg1, arg2, arg3));
6920 break;
6921#endif
6922#ifdef TARGET_NR_shmctl
6923 case TARGET_NR_shmctl:
6924 ret = do_shmctl(arg1, arg2, arg3);
6925 break;
6926#endif
6927#ifdef TARGET_NR_shmat
6928 case TARGET_NR_shmat:
6929 ret = do_shmat(arg1, arg2, arg3);
6930 break;
6931#endif
6932#ifdef TARGET_NR_shmdt
6933 case TARGET_NR_shmdt:
6934 ret = do_shmdt(arg1);
6935 break;
6936#endif
bellard31e31b82003-02-18 22:55:36 +00006937 case TARGET_NR_fsync:
6938 ret = get_errno(fsync(arg1));
6939 break;
bellard31e31b82003-02-18 22:55:36 +00006940 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07006941#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00006942 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00006943#elif defined(TARGET_CRIS)
6944 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006945#elif defined(TARGET_S390X)
6946 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00006947#else
pbrookd865bab2008-06-07 22:12:17 +00006948 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006949#endif
bellard1b6b0292003-03-22 17:31:38 +00006950 break;
bellardec86b0f2003-04-11 00:15:04 +00006951#ifdef __NR_exit_group
6952 /* new thread calls */
6953 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02006954#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00006955 _mcleanup();
6956#endif
bellarde9009672005-04-26 20:42:36 +00006957 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00006958 ret = get_errno(exit_group(arg1));
6959 break;
6960#endif
bellard31e31b82003-02-18 22:55:36 +00006961 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00006962 if (!(p = lock_user_string(arg1)))
6963 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006964 ret = get_errno(setdomainname(p, arg2));
6965 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006966 break;
6967 case TARGET_NR_uname:
6968 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00006969 {
6970 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00006971
bellard579a97f2007-11-11 14:26:47 +00006972 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
6973 goto efault;
bellard29e619b2004-09-13 21:41:04 +00006974 ret = get_errno(sys_uname(buf));
6975 if (!is_error(ret)) {
6976 /* Overrite the native machine name with whatever is being
6977 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01006978 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00006979 /* Allow the user to override the reported release. */
6980 if (qemu_uname_release && *qemu_uname_release)
6981 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00006982 }
pbrook53a59602006-03-25 19:31:22 +00006983 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00006984 }
bellard31e31b82003-02-18 22:55:36 +00006985 break;
bellard6dbad632003-03-16 18:05:05 +00006986#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00006987 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00006988 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00006989 break;
j_mayer84409dd2007-04-06 08:56:50 +00006990#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00006991 case TARGET_NR_vm86old:
6992 goto unimplemented;
6993 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00006994 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00006995 break;
6996#endif
j_mayer84409dd2007-04-06 08:56:50 +00006997#endif
bellard31e31b82003-02-18 22:55:36 +00006998 case TARGET_NR_adjtimex:
6999 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007000#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00007001 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00007002#endif
bellard31e31b82003-02-18 22:55:36 +00007003 case TARGET_NR_init_module:
7004 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00007005#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00007006 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00007007#endif
bellard31e31b82003-02-18 22:55:36 +00007008 goto unimplemented;
7009 case TARGET_NR_quotactl:
7010 goto unimplemented;
7011 case TARGET_NR_getpgid:
7012 ret = get_errno(getpgid(arg1));
7013 break;
7014 case TARGET_NR_fchdir:
7015 ret = get_errno(fchdir(arg1));
7016 break;
j_mayer84409dd2007-04-06 08:56:50 +00007017#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00007018 case TARGET_NR_bdflush:
7019 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00007020#endif
thse5febef2007-04-01 18:31:35 +00007021#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00007022 case TARGET_NR_sysfs:
7023 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007024#endif
bellard31e31b82003-02-18 22:55:36 +00007025 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00007026 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00007027 break;
thse5febef2007-04-01 18:31:35 +00007028#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00007029 case TARGET_NR_afs_syscall:
7030 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007031#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007032#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007033 case TARGET_NR__llseek:
7034 {
7035 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00007036#if !defined(__NR_llseek)
7037 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7038 if (res == -1) {
7039 ret = get_errno(res);
7040 } else {
7041 ret = 0;
7042 }
7043#else
bellard31e31b82003-02-18 22:55:36 +00007044 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00007045#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00007046 if ((ret == 0) && put_user_s64(res, arg4)) {
7047 goto efault;
7048 }
bellard31e31b82003-02-18 22:55:36 +00007049 }
7050 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007051#endif
bellard31e31b82003-02-18 22:55:36 +00007052 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02007053#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00007054 {
pbrook53a59602006-03-25 19:31:22 +00007055 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00007056 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007057 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00007058
7059 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00007060 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00007061 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00007062 goto fail;
7063 }
ths3b46e622007-09-17 08:09:54 +00007064
bellard4add45b2003-06-05 01:52:59 +00007065 ret = get_errno(sys_getdents(arg1, dirp, count));
7066 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007067 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00007068 struct target_dirent *tde;
7069 int len = ret;
7070 int reclen, treclen;
7071 int count1, tnamelen;
7072
7073 count1 = 0;
7074 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00007075 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7076 goto efault;
bellard4add45b2003-06-05 01:52:59 +00007077 tde = target_dirp;
7078 while (len > 0) {
7079 reclen = de->d_reclen;
Dmitry V. Levin333858b2012-08-21 02:13:12 +04007080 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
7081 assert(tnamelen >= 0);
7082 treclen = tnamelen + offsetof(struct target_dirent, d_name);
7083 assert(count1 + treclen <= count);
bellard4add45b2003-06-05 01:52:59 +00007084 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007085 tde->d_ino = tswapal(de->d_ino);
7086 tde->d_off = tswapal(de->d_off);
Dmitry V. Levin333858b2012-08-21 02:13:12 +04007087 memcpy(tde->d_name, de->d_name, tnamelen);
aurel326556a832008-10-13 21:08:17 +00007088 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00007089 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00007090 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00007091 count1 += treclen;
7092 }
7093 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00007094 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00007095 }
7096 free(dirp);
7097 }
7098#else
bellard31e31b82003-02-18 22:55:36 +00007099 {
aurel326556a832008-10-13 21:08:17 +00007100 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007101 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00007102
bellard579a97f2007-11-11 14:26:47 +00007103 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7104 goto efault;
bellard72f03902003-02-18 23:33:18 +00007105 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00007106 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007107 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00007108 int len = ret;
7109 int reclen;
7110 de = dirp;
7111 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007112 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00007113 if (reclen > len)
7114 break;
bellard8083a3e2003-03-24 23:12:16 +00007115 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00007116 tswapls(&de->d_ino);
7117 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007118 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00007119 len -= reclen;
7120 }
7121 }
pbrook53a59602006-03-25 19:31:22 +00007122 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00007123 }
bellard4add45b2003-06-05 01:52:59 +00007124#endif
bellard31e31b82003-02-18 22:55:36 +00007125 break;
ths3ae43202007-09-16 21:39:48 +00007126#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00007127 case TARGET_NR_getdents64:
7128 {
aurel326556a832008-10-13 21:08:17 +00007129 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007130 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00007131 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7132 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00007133 ret = get_errno(sys_getdents64(arg1, dirp, count));
7134 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007135 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00007136 int len = ret;
7137 int reclen;
7138 de = dirp;
7139 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007140 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00007141 if (reclen > len)
7142 break;
bellard8083a3e2003-03-24 23:12:16 +00007143 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00007144 tswap64s((uint64_t *)&de->d_ino);
7145 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007146 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00007147 len -= reclen;
7148 }
7149 }
pbrook53a59602006-03-25 19:31:22 +00007150 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00007151 }
7152 break;
bellarda541f292004-04-12 20:39:29 +00007153#endif /* TARGET_NR_getdents64 */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007154#if defined(TARGET_NR__newselect) || defined(TARGET_S390X)
7155#ifdef TARGET_S390X
7156 case TARGET_NR_select:
7157#else
bellard31e31b82003-02-18 22:55:36 +00007158 case TARGET_NR__newselect:
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007159#endif
pbrook53a59602006-03-25 19:31:22 +00007160 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00007161 break;
thse5febef2007-04-01 18:31:35 +00007162#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05007163#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7164# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00007165 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05007166# endif
7167# ifdef TARGET_NR_ppoll
7168 case TARGET_NR_ppoll:
7169# endif
bellard9de5e442003-03-23 16:49:39 +00007170 {
pbrook53a59602006-03-25 19:31:22 +00007171 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00007172 unsigned int nfds = arg2;
7173 int timeout = arg3;
7174 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00007175 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00007176
bellard579a97f2007-11-11 14:26:47 +00007177 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7178 if (!target_pfd)
7179 goto efault;
Mike Frysingerd8035d42011-02-07 01:05:51 -05007180
bellard9de5e442003-03-23 16:49:39 +00007181 pfd = alloca(sizeof(struct pollfd) * nfds);
7182 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007183 pfd[i].fd = tswap32(target_pfd[i].fd);
7184 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00007185 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05007186
7187# ifdef TARGET_NR_ppoll
7188 if (num == TARGET_NR_ppoll) {
7189 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7190 target_sigset_t *target_set;
7191 sigset_t _set, *set = &_set;
7192
7193 if (arg3) {
7194 if (target_to_host_timespec(timeout_ts, arg3)) {
7195 unlock_user(target_pfd, arg1, 0);
7196 goto efault;
7197 }
7198 } else {
7199 timeout_ts = NULL;
7200 }
7201
7202 if (arg4) {
7203 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7204 if (!target_set) {
7205 unlock_user(target_pfd, arg1, 0);
7206 goto efault;
7207 }
7208 target_to_host_sigset(set, target_set);
7209 } else {
7210 set = NULL;
7211 }
7212
7213 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7214
7215 if (!is_error(ret) && arg3) {
7216 host_to_target_timespec(arg3, timeout_ts);
7217 }
7218 if (arg4) {
7219 unlock_user(target_set, arg4, 0);
7220 }
7221 } else
7222# endif
7223 ret = get_errno(poll(pfd, nfds, timeout));
7224
bellard9de5e442003-03-23 16:49:39 +00007225 if (!is_error(ret)) {
7226 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007227 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00007228 }
7229 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00007230 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00007231 }
7232 break;
thse5febef2007-04-01 18:31:35 +00007233#endif
bellard31e31b82003-02-18 22:55:36 +00007234 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00007235 /* NOTE: the flock constant seems to be the same for every
7236 Linux platform */
7237 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00007238 break;
7239 case TARGET_NR_readv:
7240 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07007241 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
7242 if (vec != NULL) {
7243 ret = get_errno(readv(arg1, vec, arg3));
7244 unlock_iovec(vec, arg2, arg3, 1);
7245 } else {
7246 ret = -host_to_target_errno(errno);
7247 }
bellard31e31b82003-02-18 22:55:36 +00007248 }
7249 break;
7250 case TARGET_NR_writev:
7251 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07007252 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
7253 if (vec != NULL) {
7254 ret = get_errno(writev(arg1, vec, arg3));
7255 unlock_iovec(vec, arg2, arg3, 0);
7256 } else {
7257 ret = -host_to_target_errno(errno);
7258 }
bellard31e31b82003-02-18 22:55:36 +00007259 }
7260 break;
7261 case TARGET_NR_getsid:
7262 ret = get_errno(getsid(arg1));
7263 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007264#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00007265 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00007266 ret = get_errno(fdatasync(arg1));
7267 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007268#endif
bellard31e31b82003-02-18 22:55:36 +00007269 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00007270 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00007271 return value. */
ths0da46a62007-10-20 20:23:07 +00007272 ret = -TARGET_ENOTDIR;
7273 break;
Mike Frysinger737de1d2011-02-07 01:05:55 -05007274 case TARGET_NR_sched_getaffinity:
7275 {
7276 unsigned int mask_size;
7277 unsigned long *mask;
7278
7279 /*
7280 * sched_getaffinity needs multiples of ulong, so need to take
7281 * care of mismatches between target ulong and host ulong sizes.
7282 */
7283 if (arg2 & (sizeof(abi_ulong) - 1)) {
7284 ret = -TARGET_EINVAL;
7285 break;
7286 }
7287 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7288
7289 mask = alloca(mask_size);
7290 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7291
7292 if (!is_error(ret)) {
Mike McCormackcd18f052011-04-18 14:43:36 +09007293 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05007294 goto efault;
7295 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05007296 }
7297 }
7298 break;
7299 case TARGET_NR_sched_setaffinity:
7300 {
7301 unsigned int mask_size;
7302 unsigned long *mask;
7303
7304 /*
7305 * sched_setaffinity needs multiples of ulong, so need to take
7306 * care of mismatches between target ulong and host ulong sizes.
7307 */
7308 if (arg2 & (sizeof(abi_ulong) - 1)) {
7309 ret = -TARGET_EINVAL;
7310 break;
7311 }
7312 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7313
7314 mask = alloca(mask_size);
7315 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7316 goto efault;
7317 }
7318 memcpy(mask, p, arg2);
7319 unlock_user_struct(p, arg2, 0);
7320
7321 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7322 }
7323 break;
bellard31e31b82003-02-18 22:55:36 +00007324 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00007325 {
pbrook53a59602006-03-25 19:31:22 +00007326 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007327 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00007328
bellard579a97f2007-11-11 14:26:47 +00007329 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7330 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007331 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007332 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00007333 ret = get_errno(sched_setparam(arg1, &schp));
7334 }
7335 break;
bellard31e31b82003-02-18 22:55:36 +00007336 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00007337 {
pbrook53a59602006-03-25 19:31:22 +00007338 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007339 struct sched_param schp;
7340 ret = get_errno(sched_getparam(arg1, &schp));
7341 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007342 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7343 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007344 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007345 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00007346 }
7347 }
7348 break;
bellard31e31b82003-02-18 22:55:36 +00007349 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00007350 {
pbrook53a59602006-03-25 19:31:22 +00007351 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007352 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00007353 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7354 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007355 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007356 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00007357 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7358 }
7359 break;
bellard31e31b82003-02-18 22:55:36 +00007360 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00007361 ret = get_errno(sched_getscheduler(arg1));
7362 break;
bellard31e31b82003-02-18 22:55:36 +00007363 case TARGET_NR_sched_yield:
7364 ret = get_errno(sched_yield());
7365 break;
7366 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00007367 ret = get_errno(sched_get_priority_max(arg1));
7368 break;
bellard31e31b82003-02-18 22:55:36 +00007369 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00007370 ret = get_errno(sched_get_priority_min(arg1));
7371 break;
bellard31e31b82003-02-18 22:55:36 +00007372 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00007373 {
bellard5cd43932003-03-29 16:54:36 +00007374 struct timespec ts;
7375 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7376 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007377 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00007378 }
7379 }
7380 break;
bellard31e31b82003-02-18 22:55:36 +00007381 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00007382 {
bellard1b6b0292003-03-22 17:31:38 +00007383 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00007384 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00007385 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00007386 if (is_error(ret) && arg2) {
7387 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00007388 }
7389 }
7390 break;
thse5febef2007-04-01 18:31:35 +00007391#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00007392 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00007393 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007394#endif
7395#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00007396 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00007397 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007398#endif
bellard31e31b82003-02-18 22:55:36 +00007399 case TARGET_NR_prctl:
Peter Maydell1e6722f2012-02-03 14:48:03 +00007400 switch (arg1) {
7401 case PR_GET_PDEATHSIG:
7402 {
7403 int deathsig;
7404 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
7405 if (!is_error(ret) && arg2
7406 && put_user_ual(deathsig, arg2)) {
7407 goto efault;
thse5574482007-02-11 20:03:13 +00007408 }
Peter Maydell1e6722f2012-02-03 14:48:03 +00007409 break;
7410 }
Peter Maydelldb9526b2012-02-03 14:48:03 +00007411#ifdef PR_GET_NAME
7412 case PR_GET_NAME:
7413 {
7414 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
7415 if (!name) {
7416 goto efault;
7417 }
7418 ret = get_errno(prctl(arg1, (unsigned long)name,
7419 arg3, arg4, arg5));
7420 unlock_user(name, arg2, 16);
7421 break;
7422 }
7423 case PR_SET_NAME:
7424 {
7425 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
7426 if (!name) {
7427 goto efault;
7428 }
7429 ret = get_errno(prctl(arg1, (unsigned long)name,
7430 arg3, arg4, arg5));
7431 unlock_user(name, arg2, 0);
7432 break;
7433 }
7434#endif
Peter Maydell1e6722f2012-02-03 14:48:03 +00007435 default:
7436 /* Most prctl options have no pointer arguments */
7437 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7438 break;
7439 }
ths39b9aae2007-02-11 18:36:44 +00007440 break;
bellardd2fd1af2007-11-14 18:08:56 +00007441#ifdef TARGET_NR_arch_prctl
7442 case TARGET_NR_arch_prctl:
7443#if defined(TARGET_I386) && !defined(TARGET_ABI32)
7444 ret = do_arch_prctl(cpu_env, arg1, arg2);
7445 break;
7446#else
7447 goto unimplemented;
7448#endif
7449#endif
bellard67867302003-11-23 17:05:30 +00007450#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00007451 case TARGET_NR_pread:
Riku Voipio48e515d2011-07-12 15:40:51 +03007452 if (regpairs_aligned(cpu_env))
balroga4ae00b2008-09-20 03:14:14 +00007453 arg4 = arg5;
bellard579a97f2007-11-11 14:26:47 +00007454 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7455 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007456 ret = get_errno(pread(arg1, p, arg3, arg4));
7457 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00007458 break;
bellard31e31b82003-02-18 22:55:36 +00007459 case TARGET_NR_pwrite:
Riku Voipio48e515d2011-07-12 15:40:51 +03007460 if (regpairs_aligned(cpu_env))
balroga4ae00b2008-09-20 03:14:14 +00007461 arg4 = arg5;
bellard579a97f2007-11-11 14:26:47 +00007462 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7463 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007464 ret = get_errno(pwrite(arg1, p, arg3, arg4));
7465 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00007466 break;
bellard67867302003-11-23 17:05:30 +00007467#endif
aurel32f2c7ba12008-03-28 22:32:06 +00007468#ifdef TARGET_NR_pread64
7469 case TARGET_NR_pread64:
Alexander Grafae017a52012-09-29 15:32:39 +00007470 if (regpairs_aligned(cpu_env)) {
7471 arg4 = arg5;
7472 arg5 = arg6;
7473 }
aurel32f2c7ba12008-03-28 22:32:06 +00007474 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7475 goto efault;
7476 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7477 unlock_user(p, arg2, ret);
7478 break;
7479 case TARGET_NR_pwrite64:
Alexander Grafae017a52012-09-29 15:32:39 +00007480 if (regpairs_aligned(cpu_env)) {
7481 arg4 = arg5;
7482 arg5 = arg6;
7483 }
aurel32f2c7ba12008-03-28 22:32:06 +00007484 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7485 goto efault;
7486 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7487 unlock_user(p, arg2, 0);
7488 break;
7489#endif
bellard31e31b82003-02-18 22:55:36 +00007490 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00007491 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7492 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007493 ret = get_errno(sys_getcwd1(p, arg2));
7494 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00007495 break;
7496 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00007497 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007498 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00007499 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007500 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00007501#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02007502 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
Jia Liud9627832012-07-20 15:50:52 +08007503 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
Andreas Färber9349b4f2012-03-14 01:38:32 +01007504 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00007505 break;
7506#else
bellard5cd43932003-03-29 16:54:36 +00007507 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00007508#endif
bellard31e31b82003-02-18 22:55:36 +00007509 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00007510 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007511#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00007512 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00007513 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007514#endif
7515#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00007516 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00007517 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007518#endif
bellard048f6b42005-11-26 18:47:20 +00007519#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00007520 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00007521 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
7522 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00007523 break;
bellard048f6b42005-11-26 18:47:20 +00007524#endif
bellardebc05482003-09-30 21:08:41 +00007525#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00007526 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00007527 {
7528 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007529 int resource = target_to_host_resource(arg1);
7530 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00007531 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007532 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00007533 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7534 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007535 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7536 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007537 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00007538 }
7539 break;
7540 }
bellardebc05482003-09-30 21:08:41 +00007541#endif
bellarda315a142005-01-30 22:59:18 +00007542#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00007543 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00007544 if (!(p = lock_user_string(arg1)))
7545 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007546 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
7547 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00007548 break;
bellarda315a142005-01-30 22:59:18 +00007549#endif
7550#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00007551 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00007552 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00007553 break;
bellarda315a142005-01-30 22:59:18 +00007554#endif
7555#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00007556 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00007557 if (!(p = lock_user_string(arg1)))
7558 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007559 ret = get_errno(stat(path(p), &st));
7560 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007561 if (!is_error(ret))
7562 ret = host_to_target_stat64(cpu_env, arg2, &st);
7563 break;
bellarda315a142005-01-30 22:59:18 +00007564#endif
7565#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00007566 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00007567 if (!(p = lock_user_string(arg1)))
7568 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007569 ret = get_errno(lstat(path(p), &st));
7570 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007571 if (!is_error(ret))
7572 ret = host_to_target_stat64(cpu_env, arg2, &st);
7573 break;
bellarda315a142005-01-30 22:59:18 +00007574#endif
7575#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00007576 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00007577 ret = get_errno(fstat(arg1, &st));
7578 if (!is_error(ret))
7579 ret = host_to_target_stat64(cpu_env, arg2, &st);
7580 break;
bellardec86b0f2003-04-11 00:15:04 +00007581#endif
aurel329d33b762009-04-08 23:07:05 +00007582#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
7583 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
7584#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007585 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00007586#endif
7587#ifdef TARGET_NR_newfstatat
7588 case TARGET_NR_newfstatat:
7589#endif
balrog6a24a772008-09-20 02:23:36 +00007590 if (!(p = lock_user_string(arg2)))
7591 goto efault;
aurel329d33b762009-04-08 23:07:05 +00007592#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007593 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00007594#else
7595 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
7596#endif
balrog6a24a772008-09-20 02:23:36 +00007597 if (!is_error(ret))
7598 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00007599 break;
bellarda315a142005-01-30 22:59:18 +00007600#endif
bellard67867302003-11-23 17:05:30 +00007601 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00007602 if (!(p = lock_user_string(arg1)))
7603 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007604 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7605 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007606 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007607#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00007608 case TARGET_NR_getuid:
7609 ret = get_errno(high2lowuid(getuid()));
7610 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007611#endif
7612#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00007613 case TARGET_NR_getgid:
7614 ret = get_errno(high2lowgid(getgid()));
7615 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007616#endif
7617#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00007618 case TARGET_NR_geteuid:
7619 ret = get_errno(high2lowuid(geteuid()));
7620 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007621#endif
7622#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00007623 case TARGET_NR_getegid:
7624 ret = get_errno(high2lowgid(getegid()));
7625 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007626#endif
bellard67867302003-11-23 17:05:30 +00007627 case TARGET_NR_setreuid:
7628 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7629 break;
7630 case TARGET_NR_setregid:
7631 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7632 break;
7633 case TARGET_NR_getgroups:
7634 {
7635 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007636 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007637 gid_t *grouplist;
7638 int i;
7639
7640 grouplist = alloca(gidsetsize * sizeof(gid_t));
7641 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007642 if (gidsetsize == 0)
7643 break;
bellard67867302003-11-23 17:05:30 +00007644 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007645 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
7646 if (!target_grouplist)
7647 goto efault;
balroga2155fc2008-09-20 02:12:08 +00007648 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007649 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00007650 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00007651 }
7652 }
7653 break;
7654 case TARGET_NR_setgroups:
7655 {
7656 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007657 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007658 gid_t *grouplist;
7659 int i;
7660
7661 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007662 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
7663 if (!target_grouplist) {
7664 ret = -TARGET_EFAULT;
7665 goto fail;
7666 }
bellard67867302003-11-23 17:05:30 +00007667 for(i = 0;i < gidsetsize; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007668 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00007669 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00007670 ret = get_errno(setgroups(gidsetsize, grouplist));
7671 }
7672 break;
7673 case TARGET_NR_fchown:
7674 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7675 break;
thsccfa72b2007-09-24 09:23:34 +00007676#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
7677 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00007678 if (!(p = lock_user_string(arg2)))
7679 goto efault;
7680 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
7681 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00007682 break;
7683#endif
bellard67867302003-11-23 17:05:30 +00007684#ifdef TARGET_NR_setresuid
7685 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00007686 ret = get_errno(setresuid(low2highuid(arg1),
7687 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00007688 low2highuid(arg3)));
7689 break;
7690#endif
7691#ifdef TARGET_NR_getresuid
7692 case TARGET_NR_getresuid:
7693 {
pbrook53a59602006-03-25 19:31:22 +00007694 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00007695 ret = get_errno(getresuid(&ruid, &euid, &suid));
7696 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007697 if (put_user_u16(high2lowuid(ruid), arg1)
7698 || put_user_u16(high2lowuid(euid), arg2)
7699 || put_user_u16(high2lowuid(suid), arg3))
7700 goto efault;
bellard67867302003-11-23 17:05:30 +00007701 }
7702 }
7703 break;
7704#endif
7705#ifdef TARGET_NR_getresgid
7706 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00007707 ret = get_errno(setresgid(low2highgid(arg1),
7708 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00007709 low2highgid(arg3)));
7710 break;
7711#endif
7712#ifdef TARGET_NR_getresgid
7713 case TARGET_NR_getresgid:
7714 {
pbrook53a59602006-03-25 19:31:22 +00007715 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00007716 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7717 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007718 if (put_user_u16(high2lowgid(rgid), arg1)
7719 || put_user_u16(high2lowgid(egid), arg2)
7720 || put_user_u16(high2lowgid(sgid), arg3))
7721 goto efault;
bellard67867302003-11-23 17:05:30 +00007722 }
7723 }
7724 break;
7725#endif
7726 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00007727 if (!(p = lock_user_string(arg1)))
7728 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007729 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7730 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007731 break;
7732 case TARGET_NR_setuid:
7733 ret = get_errno(setuid(low2highuid(arg1)));
7734 break;
7735 case TARGET_NR_setgid:
7736 ret = get_errno(setgid(low2highgid(arg1)));
7737 break;
7738 case TARGET_NR_setfsuid:
7739 ret = get_errno(setfsuid(arg1));
7740 break;
7741 case TARGET_NR_setfsgid:
7742 ret = get_errno(setfsgid(arg1));
7743 break;
bellard67867302003-11-23 17:05:30 +00007744
bellarda315a142005-01-30 22:59:18 +00007745#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00007746 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00007747 if (!(p = lock_user_string(arg1)))
7748 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007749 ret = get_errno(lchown(p, arg2, arg3));
7750 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007751 break;
bellarda315a142005-01-30 22:59:18 +00007752#endif
7753#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00007754 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00007755 ret = get_errno(getuid());
7756 break;
bellarda315a142005-01-30 22:59:18 +00007757#endif
aurel3264b4d282008-11-14 17:20:15 +00007758
7759#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7760 /* Alpha specific */
7761 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007762 {
7763 uid_t euid;
7764 euid=geteuid();
7765 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7766 }
aurel3264b4d282008-11-14 17:20:15 +00007767 ret = get_errno(getuid());
7768 break;
7769#endif
7770#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7771 /* Alpha specific */
7772 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007773 {
7774 uid_t egid;
7775 egid=getegid();
7776 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7777 }
aurel3264b4d282008-11-14 17:20:15 +00007778 ret = get_errno(getgid());
7779 break;
7780#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08007781#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7782 /* Alpha specific */
7783 case TARGET_NR_osf_getsysinfo:
7784 ret = -TARGET_EOPNOTSUPP;
7785 switch (arg1) {
7786 case TARGET_GSI_IEEE_FP_CONTROL:
7787 {
7788 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7789
7790 /* Copied from linux ieee_fpcr_to_swcr. */
7791 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7792 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7793 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7794 | SWCR_TRAP_ENABLE_DZE
7795 | SWCR_TRAP_ENABLE_OVF);
7796 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
7797 | SWCR_TRAP_ENABLE_INE);
7798 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
7799 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
7800
7801 if (put_user_u64 (swcr, arg2))
7802 goto efault;
7803 ret = 0;
7804 }
7805 break;
7806
7807 /* case GSI_IEEE_STATE_AT_SIGNAL:
7808 -- Not implemented in linux kernel.
7809 case GSI_UACPROC:
7810 -- Retrieves current unaligned access state; not much used.
7811 case GSI_PROC_TYPE:
7812 -- Retrieves implver information; surely not used.
7813 case GSI_GET_HWRPB:
7814 -- Grabs a copy of the HWRPB; surely not used.
7815 */
7816 }
7817 break;
7818#endif
7819#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
7820 /* Alpha specific */
7821 case TARGET_NR_osf_setsysinfo:
7822 ret = -TARGET_EOPNOTSUPP;
7823 switch (arg1) {
7824 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007825 {
7826 uint64_t swcr, fpcr, orig_fpcr;
7827
Richard Henderson6e06d512012-06-01 09:08:21 -07007828 if (get_user_u64 (swcr, arg2)) {
Richard Hendersonba0e2762009-12-09 15:56:29 -08007829 goto efault;
Richard Henderson6e06d512012-06-01 09:08:21 -07007830 }
7831 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007832 fpcr = orig_fpcr & FPCR_DYN_MASK;
7833
7834 /* Copied from linux ieee_swcr_to_fpcr. */
7835 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
7836 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
7837 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
7838 | SWCR_TRAP_ENABLE_DZE
7839 | SWCR_TRAP_ENABLE_OVF)) << 48;
7840 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
7841 | SWCR_TRAP_ENABLE_INE)) << 57;
7842 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
7843 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
7844
Richard Henderson6e06d512012-06-01 09:08:21 -07007845 cpu_alpha_store_fpcr(cpu_env, fpcr);
7846 ret = 0;
7847 }
7848 break;
7849
7850 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
7851 {
7852 uint64_t exc, fpcr, orig_fpcr;
7853 int si_code;
7854
7855 if (get_user_u64(exc, arg2)) {
7856 goto efault;
7857 }
7858
7859 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
7860
7861 /* We only add to the exception status here. */
7862 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
7863
7864 cpu_alpha_store_fpcr(cpu_env, fpcr);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007865 ret = 0;
7866
Richard Henderson6e06d512012-06-01 09:08:21 -07007867 /* Old exceptions are not signaled. */
7868 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007869
Richard Henderson6e06d512012-06-01 09:08:21 -07007870 /* If any exceptions set by this call,
7871 and are unmasked, send a signal. */
7872 si_code = 0;
7873 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
7874 si_code = TARGET_FPE_FLTRES;
7875 }
7876 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
7877 si_code = TARGET_FPE_FLTUND;
7878 }
7879 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
7880 si_code = TARGET_FPE_FLTOVF;
7881 }
7882 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
7883 si_code = TARGET_FPE_FLTDIV;
7884 }
7885 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
7886 si_code = TARGET_FPE_FLTINV;
7887 }
7888 if (si_code != 0) {
7889 target_siginfo_t info;
7890 info.si_signo = SIGFPE;
7891 info.si_errno = 0;
7892 info.si_code = si_code;
7893 info._sifields._sigfault._addr
7894 = ((CPUArchState *)cpu_env)->pc;
7895 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007896 }
7897 }
7898 break;
7899
7900 /* case SSI_NVPAIRS:
7901 -- Used with SSIN_UACPROC to enable unaligned accesses.
7902 case SSI_IEEE_STATE_AT_SIGNAL:
7903 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
7904 -- Not implemented in linux kernel
7905 */
7906 }
7907 break;
7908#endif
7909#ifdef TARGET_NR_osf_sigprocmask
7910 /* Alpha specific. */
7911 case TARGET_NR_osf_sigprocmask:
7912 {
7913 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01007914 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08007915 sigset_t set, oldset;
7916
7917 switch(arg1) {
7918 case TARGET_SIG_BLOCK:
7919 how = SIG_BLOCK;
7920 break;
7921 case TARGET_SIG_UNBLOCK:
7922 how = SIG_UNBLOCK;
7923 break;
7924 case TARGET_SIG_SETMASK:
7925 how = SIG_SETMASK;
7926 break;
7927 default:
7928 ret = -TARGET_EINVAL;
7929 goto fail;
7930 }
7931 mask = arg2;
7932 target_to_host_old_sigset(&set, &mask);
Juan Quintelabc088ba2011-06-16 17:37:10 +01007933 sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007934 host_to_target_old_sigset(&mask, &oldset);
7935 ret = mask;
7936 }
7937 break;
7938#endif
aurel3264b4d282008-11-14 17:20:15 +00007939
bellarda315a142005-01-30 22:59:18 +00007940#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00007941 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00007942 ret = get_errno(getgid());
7943 break;
bellarda315a142005-01-30 22:59:18 +00007944#endif
7945#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00007946 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00007947 ret = get_errno(geteuid());
7948 break;
bellarda315a142005-01-30 22:59:18 +00007949#endif
7950#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00007951 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00007952 ret = get_errno(getegid());
7953 break;
bellarda315a142005-01-30 22:59:18 +00007954#endif
7955#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00007956 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00007957 ret = get_errno(setreuid(arg1, arg2));
7958 break;
bellarda315a142005-01-30 22:59:18 +00007959#endif
7960#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00007961 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00007962 ret = get_errno(setregid(arg1, arg2));
7963 break;
bellarda315a142005-01-30 22:59:18 +00007964#endif
7965#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00007966 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00007967 {
7968 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007969 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007970 gid_t *grouplist;
7971 int i;
7972
7973 grouplist = alloca(gidsetsize * sizeof(gid_t));
7974 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007975 if (gidsetsize == 0)
7976 break;
bellard99c475a2005-01-31 20:45:13 +00007977 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007978 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
7979 if (!target_grouplist) {
7980 ret = -TARGET_EFAULT;
7981 goto fail;
7982 }
balroga2155fc2008-09-20 02:12:08 +00007983 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00007984 target_grouplist[i] = tswap32(grouplist[i]);
7985 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00007986 }
7987 }
7988 break;
bellarda315a142005-01-30 22:59:18 +00007989#endif
7990#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00007991 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00007992 {
7993 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007994 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007995 gid_t *grouplist;
7996 int i;
ths3b46e622007-09-17 08:09:54 +00007997
bellard99c475a2005-01-31 20:45:13 +00007998 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007999 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
8000 if (!target_grouplist) {
8001 ret = -TARGET_EFAULT;
8002 goto fail;
8003 }
bellard99c475a2005-01-31 20:45:13 +00008004 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00008005 grouplist[i] = tswap32(target_grouplist[i]);
8006 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00008007 ret = get_errno(setgroups(gidsetsize, grouplist));
8008 }
8009 break;
bellarda315a142005-01-30 22:59:18 +00008010#endif
8011#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00008012 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00008013 ret = get_errno(fchown(arg1, arg2, arg3));
8014 break;
bellarda315a142005-01-30 22:59:18 +00008015#endif
8016#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00008017 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00008018 ret = get_errno(setresuid(arg1, arg2, arg3));
8019 break;
bellarda315a142005-01-30 22:59:18 +00008020#endif
8021#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00008022 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00008023 {
pbrook53a59602006-03-25 19:31:22 +00008024 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00008025 ret = get_errno(getresuid(&ruid, &euid, &suid));
8026 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00008027 if (put_user_u32(ruid, arg1)
8028 || put_user_u32(euid, arg2)
8029 || put_user_u32(suid, arg3))
8030 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00008031 }
8032 }
8033 break;
bellarda315a142005-01-30 22:59:18 +00008034#endif
8035#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00008036 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00008037 ret = get_errno(setresgid(arg1, arg2, arg3));
8038 break;
bellarda315a142005-01-30 22:59:18 +00008039#endif
8040#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00008041 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00008042 {
pbrook53a59602006-03-25 19:31:22 +00008043 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00008044 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8045 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00008046 if (put_user_u32(rgid, arg1)
8047 || put_user_u32(egid, arg2)
8048 || put_user_u32(sgid, arg3))
8049 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00008050 }
8051 }
8052 break;
bellarda315a142005-01-30 22:59:18 +00008053#endif
8054#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00008055 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00008056 if (!(p = lock_user_string(arg1)))
8057 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008058 ret = get_errno(chown(p, arg2, arg3));
8059 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00008060 break;
bellarda315a142005-01-30 22:59:18 +00008061#endif
8062#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00008063 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00008064 ret = get_errno(setuid(arg1));
8065 break;
bellarda315a142005-01-30 22:59:18 +00008066#endif
8067#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00008068 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00008069 ret = get_errno(setgid(arg1));
8070 break;
bellarda315a142005-01-30 22:59:18 +00008071#endif
8072#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00008073 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00008074 ret = get_errno(setfsuid(arg1));
8075 break;
bellarda315a142005-01-30 22:59:18 +00008076#endif
8077#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00008078 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00008079 ret = get_errno(setfsgid(arg1));
8080 break;
bellarda315a142005-01-30 22:59:18 +00008081#endif
bellard67867302003-11-23 17:05:30 +00008082
bellard31e31b82003-02-18 22:55:36 +00008083 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00008084 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00008085#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00008086 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00008087 {
8088 void *a;
8089 ret = -TARGET_EFAULT;
8090 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8091 goto efault;
8092 if (!(p = lock_user_string(arg3)))
8093 goto mincore_fail;
8094 ret = get_errno(mincore(a, arg2, p));
8095 unlock_user(p, arg3, ret);
8096 mincore_fail:
8097 unlock_user(a, arg1, 0);
8098 }
8099 break;
bellardffa65c32004-01-04 23:57:22 +00008100#endif
aurel32408321b2008-10-01 21:46:32 +00008101#ifdef TARGET_NR_arm_fadvise64_64
8102 case TARGET_NR_arm_fadvise64_64:
8103 {
8104 /*
8105 * arm_fadvise64_64 looks like fadvise64_64 but
8106 * with different argument order
8107 */
8108 abi_long temp;
8109 temp = arg3;
8110 arg3 = arg4;
8111 arg4 = temp;
8112 }
8113#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008114#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00008115#ifdef TARGET_NR_fadvise64_64
8116 case TARGET_NR_fadvise64_64:
8117#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008118#ifdef TARGET_NR_fadvise64
8119 case TARGET_NR_fadvise64:
8120#endif
8121#ifdef TARGET_S390X
8122 switch (arg4) {
8123 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8124 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8125 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8126 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8127 default: break;
8128 }
8129#endif
8130 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00008131 break;
8132#endif
bellardffa65c32004-01-04 23:57:22 +00008133#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00008134 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00008135 /* A straight passthrough may not be safe because qemu sometimes
8136 turns private flie-backed mappings into anonymous mappings.
8137 This will break MADV_DONTNEED.
8138 This is a hint, so ignoring and returning success is ok. */
8139 ret = get_errno(0);
8140 break;
bellardffa65c32004-01-04 23:57:22 +00008141#endif
blueswir1992f48a2007-10-14 16:27:31 +00008142#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00008143 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00008144 {
thsb1e341e2007-03-20 21:50:52 +00008145 int cmd;
bellard77e46722003-04-29 20:39:06 +00008146 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00008147 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00008148#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00008149 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00008150#endif
bellard77e46722003-04-29 20:39:06 +00008151
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008152 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00008153 if (cmd == -TARGET_EINVAL) {
8154 ret = cmd;
8155 break;
8156 }
thsb1e341e2007-03-20 21:50:52 +00008157
bellard60cd49d2003-03-16 22:53:56 +00008158 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00008159 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00008160#ifdef TARGET_ARM
8161 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008162 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8163 goto efault;
ths58134272007-03-31 18:59:32 +00008164 fl.l_type = tswap16(target_efl->l_type);
8165 fl.l_whence = tswap16(target_efl->l_whence);
8166 fl.l_start = tswap64(target_efl->l_start);
8167 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008168 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00008169 unlock_user_struct(target_efl, arg3, 0);
8170 } else
8171#endif
8172 {
bellard9ee1fa22007-11-11 15:11:19 +00008173 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8174 goto efault;
ths58134272007-03-31 18:59:32 +00008175 fl.l_type = tswap16(target_fl->l_type);
8176 fl.l_whence = tswap16(target_fl->l_whence);
8177 fl.l_start = tswap64(target_fl->l_start);
8178 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008179 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00008180 unlock_user_struct(target_fl, arg3, 0);
8181 }
thsb1e341e2007-03-20 21:50:52 +00008182 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008183 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00008184#ifdef TARGET_ARM
8185 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008186 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8187 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008188 target_efl->l_type = tswap16(fl.l_type);
8189 target_efl->l_whence = tswap16(fl.l_whence);
8190 target_efl->l_start = tswap64(fl.l_start);
8191 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008192 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008193 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008194 } else
8195#endif
8196 {
bellard9ee1fa22007-11-11 15:11:19 +00008197 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8198 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008199 target_fl->l_type = tswap16(fl.l_type);
8200 target_fl->l_whence = tswap16(fl.l_whence);
8201 target_fl->l_start = tswap64(fl.l_start);
8202 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008203 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008204 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008205 }
bellard77e46722003-04-29 20:39:06 +00008206 }
8207 break;
8208
thsb1e341e2007-03-20 21:50:52 +00008209 case TARGET_F_SETLK64:
8210 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00008211#ifdef TARGET_ARM
8212 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008213 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8214 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008215 fl.l_type = tswap16(target_efl->l_type);
8216 fl.l_whence = tswap16(target_efl->l_whence);
8217 fl.l_start = tswap64(target_efl->l_start);
8218 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008219 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008220 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008221 } else
8222#endif
8223 {
bellard9ee1fa22007-11-11 15:11:19 +00008224 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8225 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008226 fl.l_type = tswap16(target_fl->l_type);
8227 fl.l_whence = tswap16(target_fl->l_whence);
8228 fl.l_start = tswap64(target_fl->l_start);
8229 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008230 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008231 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008232 }
thsb1e341e2007-03-20 21:50:52 +00008233 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008234 break;
bellard60cd49d2003-03-16 22:53:56 +00008235 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008236 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00008237 break;
8238 }
bellard77e46722003-04-29 20:39:06 +00008239 break;
8240 }
bellard60cd49d2003-03-16 22:53:56 +00008241#endif
ths7d600c82006-12-08 01:32:58 +00008242#ifdef TARGET_NR_cacheflush
8243 case TARGET_NR_cacheflush:
8244 /* self-modifying code is handled automatically, so nothing needed */
8245 ret = 0;
8246 break;
8247#endif
bellardebc05482003-09-30 21:08:41 +00008248#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00008249 case TARGET_NR_security:
8250 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008251#endif
bellardc573ff62004-01-04 15:51:36 +00008252#ifdef TARGET_NR_getpagesize
8253 case TARGET_NR_getpagesize:
8254 ret = TARGET_PAGE_SIZE;
8255 break;
8256#endif
bellard31e31b82003-02-18 22:55:36 +00008257 case TARGET_NR_gettid:
8258 ret = get_errno(gettid());
8259 break;
thse5febef2007-04-01 18:31:35 +00008260#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00008261 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00008262#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +03008263 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +00008264 arg2 = arg3;
8265 arg3 = arg4;
8266 arg4 = arg5;
8267 }
aurel322054ac92008-10-13 21:08:07 +00008268 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8269#else
8270 ret = get_errno(readahead(arg1, arg2, arg3));
8271#endif
8272 break;
thse5febef2007-04-01 18:31:35 +00008273#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008274#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +00008275#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00008276 case TARGET_NR_listxattr:
8277 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +00008278 {
8279 void *p, *b = 0;
8280 if (arg2) {
8281 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8282 if (!b) {
8283 ret = -TARGET_EFAULT;
8284 break;
8285 }
8286 }
8287 p = lock_user_string(arg1);
8288 if (p) {
8289 if (num == TARGET_NR_listxattr) {
8290 ret = get_errno(listxattr(p, b, arg3));
8291 } else {
8292 ret = get_errno(llistxattr(p, b, arg3));
8293 }
8294 } else {
8295 ret = -TARGET_EFAULT;
8296 }
8297 unlock_user(p, arg1, 0);
8298 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +03008299 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +00008300 }
8301 case TARGET_NR_flistxattr:
8302 {
8303 void *b = 0;
8304 if (arg2) {
8305 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8306 if (!b) {
8307 ret = -TARGET_EFAULT;
8308 break;
8309 }
8310 }
8311 ret = get_errno(flistxattr(arg1, b, arg3));
8312 unlock_user(b, arg2, arg3);
8313 break;
8314 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008315 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008316 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008317 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008318 void *p, *n, *v = 0;
8319 if (arg3) {
8320 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8321 if (!v) {
8322 ret = -TARGET_EFAULT;
8323 break;
8324 }
8325 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008326 p = lock_user_string(arg1);
8327 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008328 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008329 if (num == TARGET_NR_setxattr) {
8330 ret = get_errno(setxattr(p, n, v, arg4, arg5));
8331 } else {
8332 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
8333 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008334 } else {
8335 ret = -TARGET_EFAULT;
8336 }
8337 unlock_user(p, arg1, 0);
8338 unlock_user(n, arg2, 0);
8339 unlock_user(v, arg3, 0);
8340 }
8341 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008342 case TARGET_NR_fsetxattr:
8343 {
8344 void *n, *v = 0;
8345 if (arg3) {
8346 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8347 if (!v) {
8348 ret = -TARGET_EFAULT;
8349 break;
8350 }
8351 }
8352 n = lock_user_string(arg2);
8353 if (n) {
8354 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
8355 } else {
8356 ret = -TARGET_EFAULT;
8357 }
8358 unlock_user(n, arg2, 0);
8359 unlock_user(v, arg3, 0);
8360 }
8361 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008362 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008363 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008364 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008365 void *p, *n, *v = 0;
8366 if (arg3) {
8367 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8368 if (!v) {
8369 ret = -TARGET_EFAULT;
8370 break;
8371 }
8372 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008373 p = lock_user_string(arg1);
8374 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008375 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008376 if (num == TARGET_NR_getxattr) {
8377 ret = get_errno(getxattr(p, n, v, arg4));
8378 } else {
8379 ret = get_errno(lgetxattr(p, n, v, arg4));
8380 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008381 } else {
8382 ret = -TARGET_EFAULT;
8383 }
8384 unlock_user(p, arg1, 0);
8385 unlock_user(n, arg2, 0);
8386 unlock_user(v, arg3, arg4);
8387 }
8388 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008389 case TARGET_NR_fgetxattr:
8390 {
8391 void *n, *v = 0;
8392 if (arg3) {
8393 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8394 if (!v) {
8395 ret = -TARGET_EFAULT;
8396 break;
8397 }
8398 }
8399 n = lock_user_string(arg2);
8400 if (n) {
8401 ret = get_errno(fgetxattr(arg1, n, v, arg4));
8402 } else {
8403 ret = -TARGET_EFAULT;
8404 }
8405 unlock_user(n, arg2, 0);
8406 unlock_user(v, arg3, arg4);
8407 }
8408 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008409 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008410 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008411 {
8412 void *p, *n;
8413 p = lock_user_string(arg1);
8414 n = lock_user_string(arg2);
8415 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008416 if (num == TARGET_NR_removexattr) {
8417 ret = get_errno(removexattr(p, n));
8418 } else {
8419 ret = get_errno(lremovexattr(p, n));
8420 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008421 } else {
8422 ret = -TARGET_EFAULT;
8423 }
8424 unlock_user(p, arg1, 0);
8425 unlock_user(n, arg2, 0);
8426 }
8427 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008428 case TARGET_NR_fremovexattr:
8429 {
8430 void *n;
8431 n = lock_user_string(arg2);
8432 if (n) {
8433 ret = get_errno(fremovexattr(arg1, n));
8434 } else {
8435 ret = -TARGET_EFAULT;
8436 }
8437 unlock_user(n, arg2, 0);
8438 }
8439 break;
bellardebc05482003-09-30 21:08:41 +00008440#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008441#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +00008442#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00008443 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008444#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00008445 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
8446 ret = 0;
8447 break;
edgar_iglef967792009-01-07 14:19:38 +00008448#elif defined(TARGET_CRIS)
8449 if (arg1 & 0xff)
8450 ret = -TARGET_EINVAL;
8451 else {
8452 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
8453 ret = 0;
8454 }
8455 break;
bellard8d18e892007-11-14 15:18:40 +00008456#elif defined(TARGET_I386) && defined(TARGET_ABI32)
8457 ret = do_set_thread_area(cpu_env, arg1);
8458 break;
ths6f5b89a2007-03-02 20:48:00 +00008459#else
8460 goto unimplemented_nowarn;
8461#endif
8462#endif
8463#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00008464 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008465#if defined(TARGET_I386) && defined(TARGET_ABI32)
8466 ret = do_get_thread_area(cpu_env, arg1);
8467#else
bellard5cd43932003-03-29 16:54:36 +00008468 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00008469#endif
bellard8d18e892007-11-14 15:18:40 +00008470#endif
bellard48dc41e2006-06-21 18:15:50 +00008471#ifdef TARGET_NR_getdomainname
8472 case TARGET_NR_getdomainname:
8473 goto unimplemented_nowarn;
8474#endif
ths6f5b89a2007-03-02 20:48:00 +00008475
thsb5906f92007-03-19 13:32:45 +00008476#ifdef TARGET_NR_clock_gettime
8477 case TARGET_NR_clock_gettime:
8478 {
8479 struct timespec ts;
8480 ret = get_errno(clock_gettime(arg1, &ts));
8481 if (!is_error(ret)) {
8482 host_to_target_timespec(arg2, &ts);
8483 }
8484 break;
8485 }
8486#endif
8487#ifdef TARGET_NR_clock_getres
8488 case TARGET_NR_clock_getres:
8489 {
8490 struct timespec ts;
8491 ret = get_errno(clock_getres(arg1, &ts));
8492 if (!is_error(ret)) {
8493 host_to_target_timespec(arg2, &ts);
8494 }
8495 break;
8496 }
8497#endif
pbrook63d76512008-05-29 13:43:29 +00008498#ifdef TARGET_NR_clock_nanosleep
8499 case TARGET_NR_clock_nanosleep:
8500 {
8501 struct timespec ts;
8502 target_to_host_timespec(&ts, arg3);
8503 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
8504 if (arg4)
8505 host_to_target_timespec(arg4, &ts);
8506 break;
8507 }
8508#endif
thsb5906f92007-03-19 13:32:45 +00008509
ths6f5b89a2007-03-02 20:48:00 +00008510#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8511 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00008512 ret = get_errno(set_tid_address((int *)g2h(arg1)));
8513 break;
ths6f5b89a2007-03-02 20:48:00 +00008514#endif
8515
ths3ae43202007-09-16 21:39:48 +00008516#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00008517 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00008518 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00008519 break;
8520#endif
8521
ths3ae43202007-09-16 21:39:48 +00008522#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00008523 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00008524 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
8525 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00008526 break;
8527#endif
8528
ths4f2b1fe2007-06-21 21:57:12 +00008529#ifdef TARGET_NR_set_robust_list
8530 case TARGET_NR_set_robust_list:
8531 goto unimplemented_nowarn;
8532#endif
8533
ths9007f0e2007-09-25 17:50:37 +00008534#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
8535 case TARGET_NR_utimensat:
8536 {
Riku Voipioebc996f2009-04-21 15:01:51 +03008537 struct timespec *tsp, ts[2];
8538 if (!arg3) {
8539 tsp = NULL;
8540 } else {
8541 target_to_host_timespec(ts, arg3);
8542 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
8543 tsp = ts;
8544 }
ths9007f0e2007-09-25 17:50:37 +00008545 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03008546 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00008547 else {
bellard579a97f2007-11-11 14:26:47 +00008548 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00008549 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00008550 goto fail;
8551 }
Riku Voipioebc996f2009-04-21 15:01:51 +03008552 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00008553 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00008554 }
8555 }
8556 break;
8557#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02008558#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00008559 case TARGET_NR_futex:
8560 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
8561 break;
8562#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008563#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00008564 case TARGET_NR_inotify_init:
8565 ret = get_errno(sys_inotify_init());
8566 break;
8567#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008568#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00008569#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8570 case TARGET_NR_inotify_init1:
8571 ret = get_errno(sys_inotify_init1(arg1));
8572 break;
8573#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008574#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008575#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00008576 case TARGET_NR_inotify_add_watch:
8577 p = lock_user_string(arg2);
8578 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
8579 unlock_user(p, arg2, 0);
8580 break;
8581#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008582#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00008583 case TARGET_NR_inotify_rm_watch:
8584 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
8585 break;
8586#endif
ths9007f0e2007-09-25 17:50:37 +00008587
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07008588#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00008589 case TARGET_NR_mq_open:
8590 {
8591 struct mq_attr posix_mq_attr;
8592
8593 p = lock_user_string(arg1 - 1);
8594 if (arg4 != 0)
8595 copy_from_user_mq_attr (&posix_mq_attr, arg4);
8596 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
8597 unlock_user (p, arg1, 0);
8598 }
8599 break;
8600
8601 case TARGET_NR_mq_unlink:
8602 p = lock_user_string(arg1 - 1);
8603 ret = get_errno(mq_unlink(p));
8604 unlock_user (p, arg1, 0);
8605 break;
8606
8607 case TARGET_NR_mq_timedsend:
8608 {
8609 struct timespec ts;
8610
8611 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8612 if (arg5 != 0) {
8613 target_to_host_timespec(&ts, arg5);
8614 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
8615 host_to_target_timespec(arg5, &ts);
8616 }
8617 else
8618 ret = get_errno(mq_send(arg1, p, arg3, arg4));
8619 unlock_user (p, arg2, arg3);
8620 }
8621 break;
8622
8623 case TARGET_NR_mq_timedreceive:
8624 {
8625 struct timespec ts;
8626 unsigned int prio;
8627
8628 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8629 if (arg5 != 0) {
8630 target_to_host_timespec(&ts, arg5);
8631 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
8632 host_to_target_timespec(arg5, &ts);
8633 }
8634 else
8635 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
8636 unlock_user (p, arg2, arg3);
8637 if (arg4 != 0)
8638 put_user_u32(prio, arg4);
8639 }
8640 break;
8641
8642 /* Not implemented for now... */
8643/* case TARGET_NR_mq_notify: */
8644/* break; */
8645
8646 case TARGET_NR_mq_getsetattr:
8647 {
8648 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
8649 ret = 0;
8650 if (arg3 != 0) {
8651 ret = mq_getattr(arg1, &posix_mq_attr_out);
8652 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
8653 }
8654 if (arg2 != 0) {
8655 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
8656 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
8657 }
8658
8659 }
8660 break;
8661#endif
8662
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308663#ifdef CONFIG_SPLICE
8664#ifdef TARGET_NR_tee
8665 case TARGET_NR_tee:
8666 {
8667 ret = get_errno(tee(arg1,arg2,arg3,arg4));
8668 }
8669 break;
8670#endif
8671#ifdef TARGET_NR_splice
8672 case TARGET_NR_splice:
8673 {
8674 loff_t loff_in, loff_out;
8675 loff_t *ploff_in = NULL, *ploff_out = NULL;
8676 if(arg2) {
8677 get_user_u64(loff_in, arg2);
8678 ploff_in = &loff_in;
8679 }
8680 if(arg4) {
8681 get_user_u64(loff_out, arg2);
8682 ploff_out = &loff_out;
8683 }
8684 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
8685 }
8686 break;
8687#endif
8688#ifdef TARGET_NR_vmsplice
8689 case TARGET_NR_vmsplice:
8690 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008691 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8692 if (vec != NULL) {
8693 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
8694 unlock_iovec(vec, arg2, arg3, 0);
8695 } else {
8696 ret = -host_to_target_errno(errno);
8697 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308698 }
8699 break;
8700#endif
8701#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03008702#ifdef CONFIG_EVENTFD
8703#if defined(TARGET_NR_eventfd)
8704 case TARGET_NR_eventfd:
8705 ret = get_errno(eventfd(arg1, 0));
8706 break;
8707#endif
8708#if defined(TARGET_NR_eventfd2)
8709 case TARGET_NR_eventfd2:
8710 ret = get_errno(eventfd(arg1, arg2));
8711 break;
8712#endif
8713#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03008714#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8715 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +01008716#if TARGET_ABI_BITS == 32
8717 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
8718 target_offset64(arg5, arg6)));
8719#else
Ulrich Hechtd0927932009-09-17 20:22:14 +03008720 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +01008721#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03008722 break;
8723#endif
Peter Maydellc727f472011-01-06 11:05:10 +00008724#if defined(CONFIG_SYNC_FILE_RANGE)
8725#if defined(TARGET_NR_sync_file_range)
8726 case TARGET_NR_sync_file_range:
8727#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +03008728#if defined(TARGET_MIPS)
8729 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8730 target_offset64(arg5, arg6), arg7));
8731#else
Peter Maydellc727f472011-01-06 11:05:10 +00008732 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
8733 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +03008734#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +00008735#else
8736 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
8737#endif
8738 break;
8739#endif
8740#if defined(TARGET_NR_sync_file_range2)
8741 case TARGET_NR_sync_file_range2:
8742 /* This is like sync_file_range but the arguments are reordered */
8743#if TARGET_ABI_BITS == 32
8744 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8745 target_offset64(arg5, arg6), arg2));
8746#else
8747 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
8748#endif
8749 break;
8750#endif
8751#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00008752#if defined(CONFIG_EPOLL)
8753#if defined(TARGET_NR_epoll_create)
8754 case TARGET_NR_epoll_create:
8755 ret = get_errno(epoll_create(arg1));
8756 break;
8757#endif
8758#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
8759 case TARGET_NR_epoll_create1:
8760 ret = get_errno(epoll_create1(arg1));
8761 break;
8762#endif
8763#if defined(TARGET_NR_epoll_ctl)
8764 case TARGET_NR_epoll_ctl:
8765 {
8766 struct epoll_event ep;
8767 struct epoll_event *epp = 0;
8768 if (arg4) {
8769 struct target_epoll_event *target_ep;
8770 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
8771 goto efault;
8772 }
8773 ep.events = tswap32(target_ep->events);
8774 /* The epoll_data_t union is just opaque data to the kernel,
8775 * so we transfer all 64 bits across and need not worry what
8776 * actual data type it is.
8777 */
8778 ep.data.u64 = tswap64(target_ep->data.u64);
8779 unlock_user_struct(target_ep, arg4, 0);
8780 epp = &ep;
8781 }
8782 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
8783 break;
8784 }
8785#endif
8786
8787#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
8788#define IMPLEMENT_EPOLL_PWAIT
8789#endif
8790#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
8791#if defined(TARGET_NR_epoll_wait)
8792 case TARGET_NR_epoll_wait:
8793#endif
8794#if defined(IMPLEMENT_EPOLL_PWAIT)
8795 case TARGET_NR_epoll_pwait:
8796#endif
8797 {
8798 struct target_epoll_event *target_ep;
8799 struct epoll_event *ep;
8800 int epfd = arg1;
8801 int maxevents = arg3;
8802 int timeout = arg4;
8803
8804 target_ep = lock_user(VERIFY_WRITE, arg2,
8805 maxevents * sizeof(struct target_epoll_event), 1);
8806 if (!target_ep) {
8807 goto efault;
8808 }
8809
8810 ep = alloca(maxevents * sizeof(struct epoll_event));
8811
8812 switch (num) {
8813#if defined(IMPLEMENT_EPOLL_PWAIT)
8814 case TARGET_NR_epoll_pwait:
8815 {
8816 target_sigset_t *target_set;
8817 sigset_t _set, *set = &_set;
8818
8819 if (arg5) {
8820 target_set = lock_user(VERIFY_READ, arg5,
8821 sizeof(target_sigset_t), 1);
8822 if (!target_set) {
8823 unlock_user(target_ep, arg2, 0);
8824 goto efault;
8825 }
8826 target_to_host_sigset(set, target_set);
8827 unlock_user(target_set, arg5, 0);
8828 } else {
8829 set = NULL;
8830 }
8831
8832 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
8833 break;
8834 }
8835#endif
8836#if defined(TARGET_NR_epoll_wait)
8837 case TARGET_NR_epoll_wait:
8838 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
8839 break;
8840#endif
8841 default:
8842 ret = -TARGET_ENOSYS;
8843 }
8844 if (!is_error(ret)) {
8845 int i;
8846 for (i = 0; i < ret; i++) {
8847 target_ep[i].events = tswap32(ep[i].events);
8848 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
8849 }
8850 }
8851 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
8852 break;
8853 }
8854#endif
8855#endif
Peter Maydell163a05a2011-06-27 17:44:52 +01008856#ifdef TARGET_NR_prlimit64
8857 case TARGET_NR_prlimit64:
8858 {
8859 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
8860 struct target_rlimit64 *target_rnew, *target_rold;
8861 struct host_rlimit64 rnew, rold, *rnewp = 0;
8862 if (arg3) {
8863 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
8864 goto efault;
8865 }
8866 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
8867 rnew.rlim_max = tswap64(target_rnew->rlim_max);
8868 unlock_user_struct(target_rnew, arg3, 0);
8869 rnewp = &rnew;
8870 }
8871
8872 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
8873 if (!is_error(ret) && arg4) {
8874 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
8875 goto efault;
8876 }
8877 target_rold->rlim_cur = tswap64(rold.rlim_cur);
8878 target_rold->rlim_max = tswap64(rold.rlim_max);
8879 unlock_user_struct(target_rold, arg4, 1);
8880 }
8881 break;
8882 }
8883#endif
Richard Henderson3d21d292012-09-15 13:20:46 -07008884#ifdef TARGET_NR_gethostname
8885 case TARGET_NR_gethostname:
8886 {
8887 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
8888 if (name) {
8889 ret = get_errno(gethostname(name, arg2));
8890 unlock_user(name, arg1, arg2);
8891 } else {
8892 ret = -TARGET_EFAULT;
8893 }
8894 break;
8895 }
8896#endif
bellard31e31b82003-02-18 22:55:36 +00008897 default:
8898 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00008899 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00008900#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 +00008901 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00008902#endif
ths0da46a62007-10-20 20:23:07 +00008903 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00008904 break;
8905 }
bellard579a97f2007-11-11 14:26:47 +00008906fail:
bellardc573ff62004-01-04 15:51:36 +00008907#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00008908 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00008909#endif
thsb92c47c2007-11-01 00:07:38 +00008910 if(do_strace)
8911 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00008912 return ret;
bellard579a97f2007-11-11 14:26:47 +00008913efault:
8914 ret = -TARGET_EFAULT;
8915 goto fail;
bellard31e31b82003-02-18 22:55:36 +00008916}