blob: e868ec6afff26c7c88bed5b9d9847f29ca5e72e1 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
2 * Linux syscalls
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard31e31b82003-02-18 22:55:36 +000018 */
Eduardo Habkostd5b3a9b2009-06-09 18:26:31 -030019#define _ATFILE_SOURCE
bellard31e31b82003-02-18 22:55:36 +000020#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
bellard04369ff2003-03-20 22:33:23 +000023#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000024#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
bellard7854b052003-03-29 17:22:23 +000029#include <time.h>
pbrook82e671d2008-06-09 12:10:22 +000030#include <limits.h>
bellard31e31b82003-02-18 22:55:36 +000031#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000032#include <sys/ipc.h>
33#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000034#include <sys/wait.h>
35#include <sys/time.h>
36#include <sys/stat.h>
37#include <sys/mount.h>
ths39b9aae2007-02-11 18:36:44 +000038#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000039#include <sys/resource.h>
40#include <sys/mman.h>
41#include <sys/swap.h>
42#include <signal.h>
43#include <sched.h>
Aurelien Jarno60e99242010-03-29 02:12:51 +020044#ifdef __ia64__
45int __clone2(int (*fn)(void *), void *child_stack_base,
46 size_t stack_size, int flags, void *arg, ...);
47#endif
bellard31e31b82003-02-18 22:55:36 +000048#include <sys/socket.h>
aurel32607175e2009-04-15 16:11:59 +000049#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000050#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000051#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000052#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000053#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000054#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000055#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000056#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000057#include <sys/sysinfo.h>
aurel323b3f24a2009-04-15 16:12:13 +000058#include <sys/utsname.h>
bellard72f03902003-02-18 23:33:18 +000059//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000060#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000061#include <netinet/tcp.h>
Laurent Vivier86fcd942011-03-30 01:35:23 +020062#include <linux/wireless.h>
Stefan Weil5a61cb62011-09-08 17:55:32 +020063#include "qemu-common.h"
Juan Quintela9788c9c2009-07-27 16:13:02 +020064#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +000065#include <sys/gmon.h>
66#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030067#ifdef CONFIG_EVENTFD
68#include <sys/eventfd.h>
69#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000070#ifdef CONFIG_EPOLL
71#include <sys/epoll.h>
72#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070073#ifdef CONFIG_ATTR
Avi Kivity4f26f2b2011-11-09 14:44:52 +020074#include "qemu-xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070075#endif
bellard31e31b82003-02-18 22:55:36 +000076
77#define termios host_termios
78#define winsize host_winsize
79#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000080#define sgttyb host_sgttyb /* same as target */
81#define tchars host_tchars /* same as target */
82#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000083
84#include <linux/termios.h>
85#include <linux/unistd.h>
86#include <linux/utsname.h>
87#include <linux/cdrom.h>
88#include <linux/hdreg.h>
89#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000090#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000091#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +030092#include <linux/fs.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000093#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +000094#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000095#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +020096#include <linux/fb.h>
97#include <linux/vt.h>
pbrookd7e40362008-05-23 16:06:43 +000098#include "linux_loop.h"
Loïc Minierda790302009-12-29 22:06:13 +010099#include "cpu-uname.h"
bellard31e31b82003-02-18 22:55:36 +0000100
bellard3ef693a2003-03-23 20:17:16 +0000101#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000102
Juan Quintela2f7bb872009-07-27 16:13:24 +0200103#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +0000104#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
105 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
106#else
107/* XXX: Hardcode the above values. */
108#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +0000109#endif
110
bellard72f03902003-02-18 23:33:18 +0000111//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000112
bellard1a9353d2003-03-16 20:28:50 +0000113//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000114#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
115#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000116
bellard70a194b2003-08-11 22:20:16 +0000117
bellard70a194b2003-08-11 22:20:16 +0000118#undef _syscall0
119#undef _syscall1
120#undef _syscall2
121#undef _syscall3
122#undef _syscall4
123#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000124#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000125
bellard83fcb512006-06-14 13:37:16 +0000126#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000127static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000128{ \
129 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000130}
131
bellard83fcb512006-06-14 13:37:16 +0000132#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000133static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000134{ \
135 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000136}
137
bellard83fcb512006-06-14 13:37:16 +0000138#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000139static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000140{ \
141 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000142}
143
bellard83fcb512006-06-14 13:37:16 +0000144#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000145static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000146{ \
147 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000148}
149
bellard83fcb512006-06-14 13:37:16 +0000150#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000151static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000152{ \
153 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000154}
155
bellard83fcb512006-06-14 13:37:16 +0000156#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
157 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000158static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000159{ \
160 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000161}
bellard83fcb512006-06-14 13:37:16 +0000162
163
164#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
165 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000166static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
167 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000168{ \
169 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
170}
171
bellard70a194b2003-08-11 22:20:16 +0000172
bellard31e31b82003-02-18 22:55:36 +0000173#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000174#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000175#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000176#define __NR_sys_fchownat __NR_fchownat
balrog6a24a772008-09-20 02:23:36 +0000177#define __NR_sys_fstatat64 __NR_fstatat64
balrogac8a6552008-09-20 02:25:39 +0000178#define __NR_sys_futimesat __NR_futimesat
bellard72f03902003-02-18 23:33:18 +0000179#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000180#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000181#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000182#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000183#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000184#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000185#define __NR_sys_mknodat __NR_mknodat
aurel329d33b762009-04-08 23:07:05 +0000186#define __NR_sys_newfstatat __NR_newfstatat
ths82424832007-09-24 09:21:55 +0000187#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000188#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000189#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000190#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000191#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000192#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000193#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000194#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000195#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000196#define __NR_sys_utimensat __NR_utimensat
pbrookbd0c5662008-05-29 14:34:11 +0000197#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000198#define __NR_sys_inotify_init __NR_inotify_init
199#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
200#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000201
Alexander Graf42a39fb2011-04-15 17:32:45 +0200202#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
203 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000204#define __NR__llseek __NR_lseek
205#endif
206
bellard72f03902003-02-18 23:33:18 +0000207#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000208_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000209#else
ths0da46a62007-10-20 20:23:07 +0000210/* This is a replacement for the host gettid() and must return a host
211 errno. */
bellard72f03902003-02-18 23:33:18 +0000212static int gettid(void) {
213 return -ENOSYS;
214}
215#endif
aurel323b3f24a2009-04-15 16:12:13 +0000216_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
aurel323b3f24a2009-04-15 16:12:13 +0000217#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
218_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
219#endif
220_syscall2(int, sys_getpriority, int, which, int, who);
Richard Hendersond35b2612010-06-04 12:14:10 -0700221#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000222_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
223 loff_t *, res, uint, wh);
224#endif
225_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
226_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
227#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
228_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
229#endif
230#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
231_syscall2(int,sys_tkill,int,tid,int,sig)
232#endif
233#ifdef __NR_exit_group
234_syscall1(int,exit_group,int,error_code)
235#endif
236#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
237_syscall1(int,set_tid_address,int *,tidptr)
238#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +0200239#if defined(CONFIG_USE_NPTL)
aurel323b3f24a2009-04-15 16:12:13 +0000240#if defined(TARGET_NR_futex) && defined(__NR_futex)
241_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
242 const struct timespec *,timeout,int *,uaddr2,int,val3)
243#endif
244#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500245#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
246_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
247 unsigned long *, user_mask_ptr);
248#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
249_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
250 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200251_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
252 void *, arg);
aurel323b3f24a2009-04-15 16:12:13 +0000253
254static bitmask_transtbl fcntl_flags_tbl[] = {
255 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
256 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
257 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
258 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
259 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
260 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
261 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
262 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
263 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
264 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
265 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
266 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
267 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
268#if defined(O_DIRECT)
269 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
270#endif
271 { 0, 0, 0, 0 }
272};
273
274#define COPY_UTSNAME_FIELD(dest, src) \
275 do { \
276 /* __NEW_UTS_LEN doesn't include terminating null */ \
277 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
278 (dest)[__NEW_UTS_LEN] = '\0'; \
279 } while (0)
280
281static int sys_uname(struct new_utsname *buf)
282{
283 struct utsname uts_buf;
284
285 if (uname(&uts_buf) < 0)
286 return (-1);
287
288 /*
289 * Just in case these have some differences, we
290 * translate utsname to new_utsname (which is the
291 * struct linux kernel uses).
292 */
293
Stefan Weil67bd9ed2011-05-07 22:32:25 +0200294 memset(buf, 0, sizeof(*buf));
aurel323b3f24a2009-04-15 16:12:13 +0000295 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
296 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
297 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
298 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
299 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
300#ifdef _GNU_SOURCE
301 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
302#endif
303 return (0);
304
305#undef COPY_UTSNAME_FIELD
306}
307
308static int sys_getcwd1(char *buf, size_t size)
309{
310 if (getcwd(buf, size) == NULL) {
311 /* getcwd() sets errno */
312 return (-1);
313 }
aurel32aaf4ad32009-04-16 14:17:14 +0000314 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000315}
316
317#ifdef CONFIG_ATFILE
318/*
319 * Host system seems to have atfile syscall stubs available. We
320 * now enable them one by one as specified by target syscall_nr.h.
321 */
322
323#ifdef TARGET_NR_faccessat
aurel32465c9f02009-04-19 08:52:17 +0000324static int sys_faccessat(int dirfd, const char *pathname, int mode)
aurel323b3f24a2009-04-15 16:12:13 +0000325{
aurel32465c9f02009-04-19 08:52:17 +0000326 return (faccessat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000327}
328#endif
329#ifdef TARGET_NR_fchmodat
aurel32465c9f02009-04-19 08:52:17 +0000330static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000331{
aurel32465c9f02009-04-19 08:52:17 +0000332 return (fchmodat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000333}
334#endif
Riku Voipio0c866a72011-04-18 15:23:06 +0300335#if defined(TARGET_NR_fchownat)
aurel323b3f24a2009-04-15 16:12:13 +0000336static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
337 gid_t group, int flags)
338{
339 return (fchownat(dirfd, pathname, owner, group, flags));
340}
341#endif
342#ifdef __NR_fstatat64
343static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
344 int flags)
345{
346 return (fstatat(dirfd, pathname, buf, flags));
347}
348#endif
349#ifdef __NR_newfstatat
350static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
351 int flags)
352{
353 return (fstatat(dirfd, pathname, buf, flags));
354}
355#endif
356#ifdef TARGET_NR_futimesat
357static int sys_futimesat(int dirfd, const char *pathname,
358 const struct timeval times[2])
359{
360 return (futimesat(dirfd, pathname, times));
361}
362#endif
363#ifdef TARGET_NR_linkat
364static int sys_linkat(int olddirfd, const char *oldpath,
365 int newdirfd, const char *newpath, int flags)
366{
367 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
368}
369#endif
370#ifdef TARGET_NR_mkdirat
371static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
372{
373 return (mkdirat(dirfd, pathname, mode));
374}
375#endif
376#ifdef TARGET_NR_mknodat
377static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
378 dev_t dev)
379{
380 return (mknodat(dirfd, pathname, mode, dev));
381}
382#endif
383#ifdef TARGET_NR_openat
Alexander Graff4c69012011-09-25 06:25:35 +0200384static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000385{
386 /*
387 * open(2) has extra parameter 'mode' when called with
388 * flag O_CREAT.
389 */
390 if ((flags & O_CREAT) != 0) {
aurel323b3f24a2009-04-15 16:12:13 +0000391 return (openat(dirfd, pathname, flags, mode));
392 }
393 return (openat(dirfd, pathname, flags));
394}
395#endif
396#ifdef TARGET_NR_readlinkat
397static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
398{
399 return (readlinkat(dirfd, pathname, buf, bufsiz));
400}
401#endif
402#ifdef TARGET_NR_renameat
403static int sys_renameat(int olddirfd, const char *oldpath,
404 int newdirfd, const char *newpath)
405{
406 return (renameat(olddirfd, oldpath, newdirfd, newpath));
407}
408#endif
409#ifdef TARGET_NR_symlinkat
410static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
411{
412 return (symlinkat(oldpath, newdirfd, newpath));
413}
414#endif
415#ifdef TARGET_NR_unlinkat
416static int sys_unlinkat(int dirfd, const char *pathname, int flags)
417{
418 return (unlinkat(dirfd, pathname, flags));
419}
420#endif
aurel323b3f24a2009-04-15 16:12:13 +0000421#else /* !CONFIG_ATFILE */
422
423/*
424 * Try direct syscalls instead
425 */
ths92a34c12007-09-24 09:27:49 +0000426#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
aurel32465c9f02009-04-19 08:52:17 +0000427_syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
ths92a34c12007-09-24 09:27:49 +0000428#endif
ths814d7972007-09-24 09:26:51 +0000429#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
aurel32465c9f02009-04-19 08:52:17 +0000430_syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
ths814d7972007-09-24 09:26:51 +0000431#endif
Riku Voipio0c866a72011-04-18 15:23:06 +0300432#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +0000433_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
434 uid_t,owner,gid_t,group,int,flags)
435#endif
aurel329d33b762009-04-08 23:07:05 +0000436#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
437 defined(__NR_fstatat64)
balrog6a24a772008-09-20 02:23:36 +0000438_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
439 struct stat *,buf,int,flags)
440#endif
balrogac8a6552008-09-20 02:25:39 +0000441#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
442_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
443 const struct timeval *,times)
444#endif
aurel323b3f24a2009-04-15 16:12:13 +0000445#if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
446 defined(__NR_newfstatat)
447_syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
448 struct stat *,buf,int,flags)
blueswir18fcd3692008-08-17 20:26:25 +0000449#endif
ths64f0ce42007-09-24 09:25:06 +0000450#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
451_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
aurel323b3f24a2009-04-15 16:12:13 +0000452 int,newdirfd,const char *,newpath,int,flags)
ths64f0ce42007-09-24 09:25:06 +0000453#endif
ths4472ad02007-09-24 09:22:32 +0000454#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
455_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
456#endif
ths75ac37a2007-09-24 09:23:05 +0000457#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
458_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
459 mode_t,mode,dev_t,dev)
460#endif
ths82424832007-09-24 09:21:55 +0000461#if defined(TARGET_NR_openat) && defined(__NR_openat)
462_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
463#endif
ths5e0ccb12007-09-24 09:26:10 +0000464#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
465_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
466 char *,buf,size_t,bufsize)
467#endif
ths722183f2007-09-24 09:24:37 +0000468#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
469_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
470 int,newdirfd,const char *,newpath)
471#endif
thsb51eaa82007-09-25 16:09:22 +0000472#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000473_syscall3(int,sys_symlinkat,const char *,oldpath,
474 int,newdirfd,const char *,newpath)
475#endif
ths8170f562007-09-24 09:24:11 +0000476#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
477_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
478#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300479
480#endif /* CONFIG_ATFILE */
481
482#ifdef CONFIG_UTIMENSAT
483static int sys_utimensat(int dirfd, const char *pathname,
484 const struct timespec times[2], int flags)
485{
486 if (pathname == NULL)
487 return futimens(dirfd, times);
488 else
489 return utimensat(dirfd, pathname, times, flags);
490}
491#else
ths9007f0e2007-09-25 17:50:37 +0000492#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
493_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
494 const struct timespec *,tsp,int,flags)
495#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300496#endif /* CONFIG_UTIMENSAT */
aurel323b3f24a2009-04-15 16:12:13 +0000497
498#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000499#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000500
aurel3239b59762008-10-01 21:46:50 +0000501#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000502static int sys_inotify_init(void)
503{
504 return (inotify_init());
505}
aurel3239b59762008-10-01 21:46:50 +0000506#endif
507#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000508static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
509{
510 return (inotify_add_watch(fd, pathname, mask));
511}
aurel3239b59762008-10-01 21:46:50 +0000512#endif
513#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000514static int sys_inotify_rm_watch(int fd, int32_t wd)
515{
aurel328690e422009-04-17 13:50:32 +0000516 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000517}
aurel3239b59762008-10-01 21:46:50 +0000518#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000519#ifdef CONFIG_INOTIFY1
520#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
521static int sys_inotify_init1(int flags)
522{
523 return (inotify_init1(flags));
524}
525#endif
526#endif
aurel323b3f24a2009-04-15 16:12:13 +0000527#else
528/* Userspace can usually survive runtime without inotify */
529#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000530#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000531#undef TARGET_NR_inotify_add_watch
532#undef TARGET_NR_inotify_rm_watch
533#endif /* CONFIG_INOTIFY */
534
Mike Frysingerd8035d42011-02-07 01:05:51 -0500535#if defined(TARGET_NR_ppoll)
536#ifndef __NR_ppoll
537# define __NR_ppoll -1
538#endif
539#define __NR_sys_ppoll __NR_ppoll
540_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
541 struct timespec *, timeout, const __sigset_t *, sigmask,
542 size_t, sigsetsize)
543#endif
bellard66fb9762003-03-23 01:06:05 +0000544
Mike Frysinger055e0902011-06-03 17:01:49 -0400545#if defined(TARGET_NR_pselect6)
546#ifndef __NR_pselect6
547# define __NR_pselect6 -1
548#endif
549#define __NR_sys_pselect6 __NR_pselect6
550_syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
551 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
552#endif
553
Peter Maydell163a05a2011-06-27 17:44:52 +0100554#if defined(TARGET_NR_prlimit64)
555#ifndef __NR_prlimit64
556# define __NR_prlimit64 -1
557#endif
558#define __NR_sys_prlimit64 __NR_prlimit64
559/* The glibc rlimit structure may not be that used by the underlying syscall */
560struct host_rlimit64 {
561 uint64_t rlim_cur;
562 uint64_t rlim_max;
563};
564_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
565 const struct host_rlimit64 *, new_limit,
566 struct host_rlimit64 *, old_limit)
567#endif
568
bellard66fb9762003-03-23 01:06:05 +0000569extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000570extern int flock(int, int);
571extern int setfsuid(int);
572extern int setfsgid(int);
bellard19b84f32003-05-08 15:41:49 +0000573extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000574
Riku Voipio48e515d2011-07-12 15:40:51 +0300575/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
576#ifdef TARGET_ARM
577static inline int regpairs_aligned(void *cpu_env) {
578 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
579}
580#elif defined(TARGET_MIPS)
581static inline int regpairs_aligned(void *cpu_env) { return 1; }
582#else
583static inline int regpairs_aligned(void *cpu_env) { return 0; }
584#endif
585
thsb92c47c2007-11-01 00:07:38 +0000586#define ERRNO_TABLE_SIZE 1200
587
588/* target_to_host_errno_table[] is initialized from
589 * host_to_target_errno_table[] in syscall_init(). */
590static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
591};
592
ths637947f2007-06-01 12:09:19 +0000593/*
thsfe8f0962007-07-12 10:59:21 +0000594 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000595 * minus the errnos that are not actually generic to all archs.
596 */
thsb92c47c2007-11-01 00:07:38 +0000597static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000598 [EIDRM] = TARGET_EIDRM,
599 [ECHRNG] = TARGET_ECHRNG,
600 [EL2NSYNC] = TARGET_EL2NSYNC,
601 [EL3HLT] = TARGET_EL3HLT,
602 [EL3RST] = TARGET_EL3RST,
603 [ELNRNG] = TARGET_ELNRNG,
604 [EUNATCH] = TARGET_EUNATCH,
605 [ENOCSI] = TARGET_ENOCSI,
606 [EL2HLT] = TARGET_EL2HLT,
607 [EDEADLK] = TARGET_EDEADLK,
608 [ENOLCK] = TARGET_ENOLCK,
609 [EBADE] = TARGET_EBADE,
610 [EBADR] = TARGET_EBADR,
611 [EXFULL] = TARGET_EXFULL,
612 [ENOANO] = TARGET_ENOANO,
613 [EBADRQC] = TARGET_EBADRQC,
614 [EBADSLT] = TARGET_EBADSLT,
615 [EBFONT] = TARGET_EBFONT,
616 [ENOSTR] = TARGET_ENOSTR,
617 [ENODATA] = TARGET_ENODATA,
618 [ETIME] = TARGET_ETIME,
619 [ENOSR] = TARGET_ENOSR,
620 [ENONET] = TARGET_ENONET,
621 [ENOPKG] = TARGET_ENOPKG,
622 [EREMOTE] = TARGET_EREMOTE,
623 [ENOLINK] = TARGET_ENOLINK,
624 [EADV] = TARGET_EADV,
625 [ESRMNT] = TARGET_ESRMNT,
626 [ECOMM] = TARGET_ECOMM,
627 [EPROTO] = TARGET_EPROTO,
628 [EDOTDOT] = TARGET_EDOTDOT,
629 [EMULTIHOP] = TARGET_EMULTIHOP,
630 [EBADMSG] = TARGET_EBADMSG,
631 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
632 [EOVERFLOW] = TARGET_EOVERFLOW,
633 [ENOTUNIQ] = TARGET_ENOTUNIQ,
634 [EBADFD] = TARGET_EBADFD,
635 [EREMCHG] = TARGET_EREMCHG,
636 [ELIBACC] = TARGET_ELIBACC,
637 [ELIBBAD] = TARGET_ELIBBAD,
638 [ELIBSCN] = TARGET_ELIBSCN,
639 [ELIBMAX] = TARGET_ELIBMAX,
640 [ELIBEXEC] = TARGET_ELIBEXEC,
641 [EILSEQ] = TARGET_EILSEQ,
642 [ENOSYS] = TARGET_ENOSYS,
643 [ELOOP] = TARGET_ELOOP,
644 [ERESTART] = TARGET_ERESTART,
645 [ESTRPIPE] = TARGET_ESTRPIPE,
646 [ENOTEMPTY] = TARGET_ENOTEMPTY,
647 [EUSERS] = TARGET_EUSERS,
648 [ENOTSOCK] = TARGET_ENOTSOCK,
649 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
650 [EMSGSIZE] = TARGET_EMSGSIZE,
651 [EPROTOTYPE] = TARGET_EPROTOTYPE,
652 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
653 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
654 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
655 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
656 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
657 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
658 [EADDRINUSE] = TARGET_EADDRINUSE,
659 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
660 [ENETDOWN] = TARGET_ENETDOWN,
661 [ENETUNREACH] = TARGET_ENETUNREACH,
662 [ENETRESET] = TARGET_ENETRESET,
663 [ECONNABORTED] = TARGET_ECONNABORTED,
664 [ECONNRESET] = TARGET_ECONNRESET,
665 [ENOBUFS] = TARGET_ENOBUFS,
666 [EISCONN] = TARGET_EISCONN,
667 [ENOTCONN] = TARGET_ENOTCONN,
668 [EUCLEAN] = TARGET_EUCLEAN,
669 [ENOTNAM] = TARGET_ENOTNAM,
670 [ENAVAIL] = TARGET_ENAVAIL,
671 [EISNAM] = TARGET_EISNAM,
672 [EREMOTEIO] = TARGET_EREMOTEIO,
673 [ESHUTDOWN] = TARGET_ESHUTDOWN,
674 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
675 [ETIMEDOUT] = TARGET_ETIMEDOUT,
676 [ECONNREFUSED] = TARGET_ECONNREFUSED,
677 [EHOSTDOWN] = TARGET_EHOSTDOWN,
678 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
679 [EALREADY] = TARGET_EALREADY,
680 [EINPROGRESS] = TARGET_EINPROGRESS,
681 [ESTALE] = TARGET_ESTALE,
682 [ECANCELED] = TARGET_ECANCELED,
683 [ENOMEDIUM] = TARGET_ENOMEDIUM,
684 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000685#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000686 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000687#endif
688#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000689 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000690#endif
691#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000692 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000693#endif
694#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000695 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000696#endif
697#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000698 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000699#endif
700#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000701 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000702#endif
thsb92c47c2007-11-01 00:07:38 +0000703};
ths637947f2007-06-01 12:09:19 +0000704
705static inline int host_to_target_errno(int err)
706{
707 if(host_to_target_errno_table[err])
708 return host_to_target_errno_table[err];
709 return err;
710}
711
thsb92c47c2007-11-01 00:07:38 +0000712static inline int target_to_host_errno(int err)
713{
714 if (target_to_host_errno_table[err])
715 return target_to_host_errno_table[err];
716 return err;
717}
718
blueswir1992f48a2007-10-14 16:27:31 +0000719static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000720{
721 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000722 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000723 else
724 return ret;
725}
726
blueswir1992f48a2007-10-14 16:27:31 +0000727static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000728{
blueswir1992f48a2007-10-14 16:27:31 +0000729 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000730}
731
thsb92c47c2007-11-01 00:07:38 +0000732char *target_strerror(int err)
733{
Alexander Graf962b2892011-11-21 12:04:07 +0100734 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
735 return NULL;
736 }
thsb92c47c2007-11-01 00:07:38 +0000737 return strerror(target_to_host_errno(err));
738}
739
blueswir1992f48a2007-10-14 16:27:31 +0000740static abi_ulong target_brk;
741static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000742static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000743
blueswir1992f48a2007-10-14 16:27:31 +0000744void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000745{
blueswir14c1de732007-07-07 20:45:44 +0000746 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000747 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000748}
749
vincent4d1de872011-06-14 21:56:33 +0000750//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
751#define DEBUGF_BRK(message, args...)
752
ths0da46a62007-10-20 20:23:07 +0000753/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000754abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000755{
blueswir1992f48a2007-10-14 16:27:31 +0000756 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000757 int new_alloc_size;
758
vincent4d1de872011-06-14 21:56:33 +0000759 DEBUGF_BRK("do_brk(%#010x) -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000760
vincent4d1de872011-06-14 21:56:33 +0000761 if (!new_brk) {
762 DEBUGF_BRK("%#010x (!new_brk)\n", target_brk);
763 return target_brk;
764 }
765 if (new_brk < target_original_brk) {
766 DEBUGF_BRK("%#010x (new_brk < target_original_brk)\n", target_brk);
767 return target_brk;
768 }
bellard31e31b82003-02-18 22:55:36 +0000769
vincent4d1de872011-06-14 21:56:33 +0000770 /* If the new brk is less than the highest page reserved to the
771 * target heap allocation, set it and we're almost done... */
772 if (new_brk <= brk_page) {
773 /* Heap contents are initialized to zero, as for anonymous
774 * mapped pages. */
775 if (new_brk > target_brk) {
776 memset(g2h(target_brk), 0, new_brk - target_brk);
777 }
bellard31e31b82003-02-18 22:55:36 +0000778 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000779 DEBUGF_BRK("%#010x (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000780 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000781 }
782
Peter Maydell00faf082011-04-18 16:34:24 +0100783 /* We need to allocate more memory after the brk... Note that
784 * we don't use MAP_FIXED because that will map over the top of
785 * any existing mapping (like the one with the host libc or qemu
786 * itself); instead we treat "mapped but at wrong address" as
787 * a failure and unmap again.
788 */
vincent4d1de872011-06-14 21:56:33 +0000789 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000790 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000791 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100792 MAP_ANON|MAP_PRIVATE, 0, 0));
793
794 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200795 /* Heap contents are initialized to zero, as for anonymous
796 * mapped pages. Technically the new pages are already
797 * initialized to zero since they *are* anonymous mapped
798 * pages, however we have to take care with the contents that
799 * come from the remaining part of the previous page: it may
800 * contains garbage data due to a previous heap usage (grown
801 * then shrunken). */
802 memset(g2h(target_brk), 0, brk_page - target_brk);
803
Peter Maydell00faf082011-04-18 16:34:24 +0100804 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000805 brk_page = HOST_PAGE_ALIGN(target_brk);
806 DEBUGF_BRK("%#010x (mapped_addr == brk_page)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100807 return target_brk;
808 } else if (mapped_addr != -1) {
809 /* Mapped but at wrong address, meaning there wasn't actually
810 * enough space for this brk.
811 */
812 target_munmap(mapped_addr, new_alloc_size);
813 mapped_addr = -1;
vincent4d1de872011-06-14 21:56:33 +0000814 DEBUGF_BRK("%#010x (mapped_addr != -1)\n", target_brk);
815 }
816 else {
817 DEBUGF_BRK("%#010x (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100818 }
balrog7ab240a2008-04-26 12:17:34 +0000819
Richard Henderson7dd46c02010-05-03 10:07:49 -0700820#if defined(TARGET_ALPHA)
821 /* We (partially) emulate OSF/1 on Alpha, which requires we
822 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100823 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700824#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100825 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000826 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000827}
828
ths26edcf42007-12-09 02:25:24 +0000829static inline abi_long copy_from_user_fdset(fd_set *fds,
830 abi_ulong target_fds_addr,
831 int n)
bellard31e31b82003-02-18 22:55:36 +0000832{
ths26edcf42007-12-09 02:25:24 +0000833 int i, nw, j, k;
834 abi_ulong b, *target_fds;
835
836 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
837 if (!(target_fds = lock_user(VERIFY_READ,
838 target_fds_addr,
839 sizeof(abi_ulong) * nw,
840 1)))
841 return -TARGET_EFAULT;
842
843 FD_ZERO(fds);
844 k = 0;
845 for (i = 0; i < nw; i++) {
846 /* grab the abi_ulong */
847 __get_user(b, &target_fds[i]);
848 for (j = 0; j < TARGET_ABI_BITS; j++) {
849 /* check the bit inside the abi_ulong */
850 if ((b >> j) & 1)
851 FD_SET(k, fds);
852 k++;
bellard31e31b82003-02-18 22:55:36 +0000853 }
bellard31e31b82003-02-18 22:55:36 +0000854 }
ths26edcf42007-12-09 02:25:24 +0000855
856 unlock_user(target_fds, target_fds_addr, 0);
857
858 return 0;
bellard31e31b82003-02-18 22:55:36 +0000859}
860
Mike Frysinger055e0902011-06-03 17:01:49 -0400861static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
862 abi_ulong target_fds_addr,
863 int n)
864{
865 if (target_fds_addr) {
866 if (copy_from_user_fdset(fds, target_fds_addr, n))
867 return -TARGET_EFAULT;
868 *fds_ptr = fds;
869 } else {
870 *fds_ptr = NULL;
871 }
872 return 0;
873}
874
ths26edcf42007-12-09 02:25:24 +0000875static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
876 const fd_set *fds,
877 int n)
bellard31e31b82003-02-18 22:55:36 +0000878{
bellard31e31b82003-02-18 22:55:36 +0000879 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000880 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000881 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000882
ths26edcf42007-12-09 02:25:24 +0000883 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
884 if (!(target_fds = lock_user(VERIFY_WRITE,
885 target_fds_addr,
886 sizeof(abi_ulong) * nw,
887 0)))
888 return -TARGET_EFAULT;
889
890 k = 0;
891 for (i = 0; i < nw; i++) {
892 v = 0;
893 for (j = 0; j < TARGET_ABI_BITS; j++) {
894 v |= ((FD_ISSET(k, fds) != 0) << j);
895 k++;
bellard31e31b82003-02-18 22:55:36 +0000896 }
ths26edcf42007-12-09 02:25:24 +0000897 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000898 }
ths26edcf42007-12-09 02:25:24 +0000899
900 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
901
902 return 0;
bellard31e31b82003-02-18 22:55:36 +0000903}
904
bellardc596ed12003-07-13 17:32:31 +0000905#if defined(__alpha__)
906#define HOST_HZ 1024
907#else
908#define HOST_HZ 100
909#endif
910
blueswir1992f48a2007-10-14 16:27:31 +0000911static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000912{
913#if HOST_HZ == TARGET_HZ
914 return ticks;
915#else
916 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
917#endif
918}
919
bellard579a97f2007-11-11 14:26:47 +0000920static inline abi_long host_to_target_rusage(abi_ulong target_addr,
921 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000922{
pbrook53a59602006-03-25 19:31:22 +0000923 struct target_rusage *target_rusage;
924
bellard579a97f2007-11-11 14:26:47 +0000925 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
926 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200927 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
928 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
929 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
930 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
931 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
932 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
933 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
934 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
935 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
936 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
937 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
938 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
939 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
940 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
941 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
942 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
943 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
944 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000945 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000946
947 return 0;
bellardb4091862003-05-16 15:39:34 +0000948}
949
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200950static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900951{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200952 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300953 rlim_t result;
954
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200955 target_rlim_swap = tswapal(target_rlim);
956 if (target_rlim_swap == TARGET_RLIM_INFINITY)
957 return RLIM_INFINITY;
958
959 result = target_rlim_swap;
960 if (target_rlim_swap != (rlim_t)result)
961 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300962
963 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900964}
965
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200966static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900967{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200968 abi_ulong target_rlim_swap;
969 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300970
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200971 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300972 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900973 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300974 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200975 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300976
977 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900978}
979
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +0300980static inline int target_to_host_resource(int code)
981{
982 switch (code) {
983 case TARGET_RLIMIT_AS:
984 return RLIMIT_AS;
985 case TARGET_RLIMIT_CORE:
986 return RLIMIT_CORE;
987 case TARGET_RLIMIT_CPU:
988 return RLIMIT_CPU;
989 case TARGET_RLIMIT_DATA:
990 return RLIMIT_DATA;
991 case TARGET_RLIMIT_FSIZE:
992 return RLIMIT_FSIZE;
993 case TARGET_RLIMIT_LOCKS:
994 return RLIMIT_LOCKS;
995 case TARGET_RLIMIT_MEMLOCK:
996 return RLIMIT_MEMLOCK;
997 case TARGET_RLIMIT_MSGQUEUE:
998 return RLIMIT_MSGQUEUE;
999 case TARGET_RLIMIT_NICE:
1000 return RLIMIT_NICE;
1001 case TARGET_RLIMIT_NOFILE:
1002 return RLIMIT_NOFILE;
1003 case TARGET_RLIMIT_NPROC:
1004 return RLIMIT_NPROC;
1005 case TARGET_RLIMIT_RSS:
1006 return RLIMIT_RSS;
1007 case TARGET_RLIMIT_RTPRIO:
1008 return RLIMIT_RTPRIO;
1009 case TARGET_RLIMIT_SIGPENDING:
1010 return RLIMIT_SIGPENDING;
1011 case TARGET_RLIMIT_STACK:
1012 return RLIMIT_STACK;
1013 default:
1014 return code;
1015 }
1016}
1017
ths788f5ec2007-12-09 02:37:05 +00001018static inline abi_long copy_from_user_timeval(struct timeval *tv,
1019 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001020{
pbrook53a59602006-03-25 19:31:22 +00001021 struct target_timeval *target_tv;
1022
ths788f5ec2007-12-09 02:37:05 +00001023 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +00001024 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001025
1026 __get_user(tv->tv_sec, &target_tv->tv_sec);
1027 __get_user(tv->tv_usec, &target_tv->tv_usec);
1028
1029 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001030
1031 return 0;
bellard31e31b82003-02-18 22:55:36 +00001032}
1033
ths788f5ec2007-12-09 02:37:05 +00001034static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1035 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +00001036{
pbrook53a59602006-03-25 19:31:22 +00001037 struct target_timeval *target_tv;
1038
ths788f5ec2007-12-09 02:37:05 +00001039 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +00001040 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001041
1042 __put_user(tv->tv_sec, &target_tv->tv_sec);
1043 __put_user(tv->tv_usec, &target_tv->tv_usec);
1044
1045 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001046
1047 return 0;
bellard31e31b82003-02-18 22:55:36 +00001048}
1049
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001050#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1051#include <mqueue.h>
1052
aurel3224e10032009-04-15 16:11:43 +00001053static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1054 abi_ulong target_mq_attr_addr)
1055{
1056 struct target_mq_attr *target_mq_attr;
1057
1058 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1059 target_mq_attr_addr, 1))
1060 return -TARGET_EFAULT;
1061
1062 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1063 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1064 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1065 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1066
1067 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1068
1069 return 0;
1070}
1071
1072static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1073 const struct mq_attr *attr)
1074{
1075 struct target_mq_attr *target_mq_attr;
1076
1077 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1078 target_mq_attr_addr, 0))
1079 return -TARGET_EFAULT;
1080
1081 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1082 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1083 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1084 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1085
1086 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1087
1088 return 0;
1089}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001090#endif
bellard31e31b82003-02-18 22:55:36 +00001091
Mike Frysinger055e0902011-06-03 17:01:49 -04001092#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001093/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001094static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001095 abi_ulong rfd_addr, abi_ulong wfd_addr,
1096 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001097{
1098 fd_set rfds, wfds, efds;
1099 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1100 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001101 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001102
Mike Frysinger055e0902011-06-03 17:01:49 -04001103 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1104 if (ret) {
1105 return ret;
pbrook53a59602006-03-25 19:31:22 +00001106 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001107 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1108 if (ret) {
1109 return ret;
pbrook53a59602006-03-25 19:31:22 +00001110 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001111 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1112 if (ret) {
1113 return ret;
pbrook53a59602006-03-25 19:31:22 +00001114 }
ths3b46e622007-09-17 08:09:54 +00001115
ths26edcf42007-12-09 02:25:24 +00001116 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001117 if (copy_from_user_timeval(&tv, target_tv_addr))
1118 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001119 tv_ptr = &tv;
1120 } else {
1121 tv_ptr = NULL;
1122 }
ths26edcf42007-12-09 02:25:24 +00001123
bellard31e31b82003-02-18 22:55:36 +00001124 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +00001125
ths26edcf42007-12-09 02:25:24 +00001126 if (!is_error(ret)) {
1127 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1128 return -TARGET_EFAULT;
1129 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1130 return -TARGET_EFAULT;
1131 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1132 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001133
ths788f5ec2007-12-09 02:37:05 +00001134 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1135 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001136 }
bellard579a97f2007-11-11 14:26:47 +00001137
bellard31e31b82003-02-18 22:55:36 +00001138 return ret;
1139}
Mike Frysinger055e0902011-06-03 17:01:49 -04001140#endif
bellard31e31b82003-02-18 22:55:36 +00001141
Riku Voipio099d6b02009-05-05 12:10:04 +03001142static abi_long do_pipe2(int host_pipe[], int flags)
1143{
1144#ifdef CONFIG_PIPE2
1145 return pipe2(host_pipe, flags);
1146#else
1147 return -ENOSYS;
1148#endif
1149}
1150
Richard Hendersonfb41a662010-05-03 10:07:52 -07001151static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1152 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001153{
1154 int host_pipe[2];
1155 abi_long ret;
1156 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1157
1158 if (is_error(ret))
1159 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001160
1161 /* Several targets have special calling conventions for the original
1162 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1163 if (!is_pipe2) {
1164#if defined(TARGET_ALPHA)
1165 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1166 return host_pipe[0];
1167#elif defined(TARGET_MIPS)
1168 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1169 return host_pipe[0];
1170#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001171 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001172 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001173#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001174 }
1175
Riku Voipio099d6b02009-05-05 12:10:04 +03001176 if (put_user_s32(host_pipe[0], pipedes)
1177 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1178 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001179 return get_errno(ret);
1180}
1181
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001182static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1183 abi_ulong target_addr,
1184 socklen_t len)
1185{
1186 struct target_ip_mreqn *target_smreqn;
1187
1188 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1189 if (!target_smreqn)
1190 return -TARGET_EFAULT;
1191 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1192 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1193 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001194 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001195 unlock_user(target_smreqn, target_addr, 0);
1196
1197 return 0;
1198}
1199
bellard579a97f2007-11-11 14:26:47 +00001200static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1201 abi_ulong target_addr,
1202 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001203{
aurel32607175e2009-04-15 16:11:59 +00001204 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1205 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001206 struct target_sockaddr *target_saddr;
1207
bellard579a97f2007-11-11 14:26:47 +00001208 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1209 if (!target_saddr)
1210 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001211
1212 sa_family = tswap16(target_saddr->sa_family);
1213
1214 /* Oops. The caller might send a incomplete sun_path; sun_path
1215 * must be terminated by \0 (see the manual page), but
1216 * unfortunately it is quite common to specify sockaddr_un
1217 * length as "strlen(x->sun_path)" while it should be
1218 * "strlen(...) + 1". We'll fix that here if needed.
1219 * Linux kernel has a similar feature.
1220 */
1221
1222 if (sa_family == AF_UNIX) {
1223 if (len < unix_maxlen && len > 0) {
1224 char *cp = (char*)target_saddr;
1225
1226 if ( cp[len-1] && !cp[len] )
1227 len++;
1228 }
1229 if (len > unix_maxlen)
1230 len = unix_maxlen;
1231 }
1232
pbrook53a59602006-03-25 19:31:22 +00001233 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001234 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001235 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001236
1237 return 0;
bellard7854b052003-03-29 17:22:23 +00001238}
1239
bellard579a97f2007-11-11 14:26:47 +00001240static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1241 struct sockaddr *addr,
1242 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001243{
pbrook53a59602006-03-25 19:31:22 +00001244 struct target_sockaddr *target_saddr;
1245
bellard579a97f2007-11-11 14:26:47 +00001246 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1247 if (!target_saddr)
1248 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001249 memcpy(target_saddr, addr, len);
1250 target_saddr->sa_family = tswap16(addr->sa_family);
1251 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001252
1253 return 0;
bellard7854b052003-03-29 17:22:23 +00001254}
1255
pbrook53a59602006-03-25 19:31:22 +00001256/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001257static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1258 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001259{
1260 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001261 abi_long msg_controllen;
1262 abi_ulong target_cmsg_addr;
1263 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001264 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001265
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001266 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001267 if (msg_controllen < sizeof (struct target_cmsghdr))
1268 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001269 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001270 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1271 if (!target_cmsg)
1272 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001273
1274 while (cmsg && target_cmsg) {
1275 void *data = CMSG_DATA(cmsg);
1276 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1277
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001278 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001279 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1280
1281 space += CMSG_SPACE(len);
1282 if (space > msgh->msg_controllen) {
1283 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001284 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001285 break;
1286 }
1287
1288 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1289 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1290 cmsg->cmsg_len = CMSG_LEN(len);
1291
bellard3532fa72006-06-24 15:06:03 +00001292 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001293 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1294 memcpy(data, target_data, len);
1295 } else {
1296 int *fd = (int *)data;
1297 int *target_fd = (int *)target_data;
1298 int i, numfds = len / sizeof(int);
1299
1300 for (i = 0; i < numfds; i++)
1301 fd[i] = tswap32(target_fd[i]);
1302 }
1303
1304 cmsg = CMSG_NXTHDR(msgh, cmsg);
1305 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1306 }
bellard5a4a8982007-11-11 17:39:18 +00001307 unlock_user(target_cmsg, target_cmsg_addr, 0);
1308 the_end:
bellard7854b052003-03-29 17:22:23 +00001309 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001310 return 0;
bellard7854b052003-03-29 17:22:23 +00001311}
1312
pbrook53a59602006-03-25 19:31:22 +00001313/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001314static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1315 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001316{
1317 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001318 abi_long msg_controllen;
1319 abi_ulong target_cmsg_addr;
1320 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001321 socklen_t space = 0;
1322
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001323 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001324 if (msg_controllen < sizeof (struct target_cmsghdr))
1325 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001326 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001327 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1328 if (!target_cmsg)
1329 return -TARGET_EFAULT;
1330
bellard7854b052003-03-29 17:22:23 +00001331 while (cmsg && target_cmsg) {
1332 void *data = CMSG_DATA(cmsg);
1333 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1334
1335 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1336
1337 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001338 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001339 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001340 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001341 break;
1342 }
1343
1344 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1345 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001346 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
bellard7854b052003-03-29 17:22:23 +00001347
bellard3532fa72006-06-24 15:06:03 +00001348 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001349 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1350 memcpy(target_data, data, len);
1351 } else {
1352 int *fd = (int *)data;
1353 int *target_fd = (int *)target_data;
1354 int i, numfds = len / sizeof(int);
1355
1356 for (i = 0; i < numfds; i++)
1357 target_fd[i] = tswap32(fd[i]);
1358 }
1359
1360 cmsg = CMSG_NXTHDR(msgh, cmsg);
1361 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1362 }
bellard5a4a8982007-11-11 17:39:18 +00001363 unlock_user(target_cmsg, target_cmsg_addr, space);
1364 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001365 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001366 return 0;
bellard7854b052003-03-29 17:22:23 +00001367}
1368
ths0da46a62007-10-20 20:23:07 +00001369/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001370static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001371 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001372{
blueswir1992f48a2007-10-14 16:27:31 +00001373 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001374 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001375 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001376 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001377
bellard8853f862004-02-22 14:57:26 +00001378 switch(level) {
1379 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001380 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001381 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001382 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001383
bellard2f619692007-11-16 10:46:05 +00001384 if (get_user_u32(val, optval_addr))
1385 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001386 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1387 break;
1388 case SOL_IP:
1389 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001390 case IP_TOS:
1391 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001392 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001393 case IP_ROUTER_ALERT:
1394 case IP_RECVOPTS:
1395 case IP_RETOPTS:
1396 case IP_PKTINFO:
1397 case IP_MTU_DISCOVER:
1398 case IP_RECVERR:
1399 case IP_RECVTOS:
1400#ifdef IP_FREEBIND
1401 case IP_FREEBIND:
1402#endif
1403 case IP_MULTICAST_TTL:
1404 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001405 val = 0;
1406 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001407 if (get_user_u32(val, optval_addr))
1408 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001409 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001410 if (get_user_u8(val, optval_addr))
1411 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001412 }
1413 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1414 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001415 case IP_ADD_MEMBERSHIP:
1416 case IP_DROP_MEMBERSHIP:
1417 if (optlen < sizeof (struct target_ip_mreq) ||
1418 optlen > sizeof (struct target_ip_mreqn))
1419 return -TARGET_EINVAL;
1420
1421 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1422 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1423 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1424 break;
1425
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001426 case IP_BLOCK_SOURCE:
1427 case IP_UNBLOCK_SOURCE:
1428 case IP_ADD_SOURCE_MEMBERSHIP:
1429 case IP_DROP_SOURCE_MEMBERSHIP:
1430 if (optlen != sizeof (struct target_ip_mreq_source))
1431 return -TARGET_EINVAL;
1432
1433 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1434 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1435 unlock_user (ip_mreq_source, optval_addr, 0);
1436 break;
1437
bellard8853f862004-02-22 14:57:26 +00001438 default:
1439 goto unimplemented;
1440 }
1441 break;
bellard3532fa72006-06-24 15:06:03 +00001442 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001443 switch (optname) {
1444 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001445 case TARGET_SO_DEBUG:
1446 optname = SO_DEBUG;
1447 break;
1448 case TARGET_SO_REUSEADDR:
1449 optname = SO_REUSEADDR;
1450 break;
1451 case TARGET_SO_TYPE:
1452 optname = SO_TYPE;
1453 break;
1454 case TARGET_SO_ERROR:
1455 optname = SO_ERROR;
1456 break;
1457 case TARGET_SO_DONTROUTE:
1458 optname = SO_DONTROUTE;
1459 break;
1460 case TARGET_SO_BROADCAST:
1461 optname = SO_BROADCAST;
1462 break;
1463 case TARGET_SO_SNDBUF:
1464 optname = SO_SNDBUF;
1465 break;
1466 case TARGET_SO_RCVBUF:
1467 optname = SO_RCVBUF;
1468 break;
1469 case TARGET_SO_KEEPALIVE:
1470 optname = SO_KEEPALIVE;
1471 break;
1472 case TARGET_SO_OOBINLINE:
1473 optname = SO_OOBINLINE;
1474 break;
1475 case TARGET_SO_NO_CHECK:
1476 optname = SO_NO_CHECK;
1477 break;
1478 case TARGET_SO_PRIORITY:
1479 optname = SO_PRIORITY;
1480 break;
bellard5e83e8e2005-03-01 22:32:06 +00001481#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001482 case TARGET_SO_BSDCOMPAT:
1483 optname = SO_BSDCOMPAT;
1484 break;
bellard5e83e8e2005-03-01 22:32:06 +00001485#endif
bellard3532fa72006-06-24 15:06:03 +00001486 case TARGET_SO_PASSCRED:
1487 optname = SO_PASSCRED;
1488 break;
1489 case TARGET_SO_TIMESTAMP:
1490 optname = SO_TIMESTAMP;
1491 break;
1492 case TARGET_SO_RCVLOWAT:
1493 optname = SO_RCVLOWAT;
1494 break;
1495 case TARGET_SO_RCVTIMEO:
1496 optname = SO_RCVTIMEO;
1497 break;
1498 case TARGET_SO_SNDTIMEO:
1499 optname = SO_SNDTIMEO;
1500 break;
bellard8853f862004-02-22 14:57:26 +00001501 break;
1502 default:
1503 goto unimplemented;
1504 }
bellard3532fa72006-06-24 15:06:03 +00001505 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001506 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001507
bellard2f619692007-11-16 10:46:05 +00001508 if (get_user_u32(val, optval_addr))
1509 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001510 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001511 break;
bellard7854b052003-03-29 17:22:23 +00001512 default:
bellard8853f862004-02-22 14:57:26 +00001513 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02001514 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001515 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001516 }
bellard8853f862004-02-22 14:57:26 +00001517 return ret;
bellard7854b052003-03-29 17:22:23 +00001518}
1519
ths0da46a62007-10-20 20:23:07 +00001520/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001521static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001522 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001523{
blueswir1992f48a2007-10-14 16:27:31 +00001524 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001525 int len, val;
1526 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001527
1528 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001529 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001530 level = SOL_SOCKET;
1531 switch (optname) {
1532 /* These don't just return a single integer */
1533 case TARGET_SO_LINGER:
1534 case TARGET_SO_RCVTIMEO:
1535 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001536 case TARGET_SO_PEERNAME:
1537 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02001538 case TARGET_SO_PEERCRED: {
1539 struct ucred cr;
1540 socklen_t crlen;
1541 struct target_ucred *tcr;
1542
1543 if (get_user_u32(len, optlen)) {
1544 return -TARGET_EFAULT;
1545 }
1546 if (len < 0) {
1547 return -TARGET_EINVAL;
1548 }
1549
1550 crlen = sizeof(cr);
1551 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1552 &cr, &crlen));
1553 if (ret < 0) {
1554 return ret;
1555 }
1556 if (len > crlen) {
1557 len = crlen;
1558 }
1559 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1560 return -TARGET_EFAULT;
1561 }
1562 __put_user(cr.pid, &tcr->pid);
1563 __put_user(cr.uid, &tcr->uid);
1564 __put_user(cr.gid, &tcr->gid);
1565 unlock_user_struct(tcr, optval_addr, 1);
1566 if (put_user_u32(len, optlen)) {
1567 return -TARGET_EFAULT;
1568 }
1569 break;
1570 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001571 /* Options with 'int' argument. */
1572 case TARGET_SO_DEBUG:
1573 optname = SO_DEBUG;
1574 goto int_case;
1575 case TARGET_SO_REUSEADDR:
1576 optname = SO_REUSEADDR;
1577 goto int_case;
1578 case TARGET_SO_TYPE:
1579 optname = SO_TYPE;
1580 goto int_case;
1581 case TARGET_SO_ERROR:
1582 optname = SO_ERROR;
1583 goto int_case;
1584 case TARGET_SO_DONTROUTE:
1585 optname = SO_DONTROUTE;
1586 goto int_case;
1587 case TARGET_SO_BROADCAST:
1588 optname = SO_BROADCAST;
1589 goto int_case;
1590 case TARGET_SO_SNDBUF:
1591 optname = SO_SNDBUF;
1592 goto int_case;
1593 case TARGET_SO_RCVBUF:
1594 optname = SO_RCVBUF;
1595 goto int_case;
1596 case TARGET_SO_KEEPALIVE:
1597 optname = SO_KEEPALIVE;
1598 goto int_case;
1599 case TARGET_SO_OOBINLINE:
1600 optname = SO_OOBINLINE;
1601 goto int_case;
1602 case TARGET_SO_NO_CHECK:
1603 optname = SO_NO_CHECK;
1604 goto int_case;
1605 case TARGET_SO_PRIORITY:
1606 optname = SO_PRIORITY;
1607 goto int_case;
1608#ifdef SO_BSDCOMPAT
1609 case TARGET_SO_BSDCOMPAT:
1610 optname = SO_BSDCOMPAT;
1611 goto int_case;
1612#endif
1613 case TARGET_SO_PASSCRED:
1614 optname = SO_PASSCRED;
1615 goto int_case;
1616 case TARGET_SO_TIMESTAMP:
1617 optname = SO_TIMESTAMP;
1618 goto int_case;
1619 case TARGET_SO_RCVLOWAT:
1620 optname = SO_RCVLOWAT;
1621 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001622 default:
bellard2efbe912005-07-23 15:10:20 +00001623 goto int_case;
1624 }
1625 break;
1626 case SOL_TCP:
1627 /* TCP options all take an 'int' value. */
1628 int_case:
bellard2f619692007-11-16 10:46:05 +00001629 if (get_user_u32(len, optlen))
1630 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001631 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001632 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001633 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00001634 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1635 if (ret < 0)
1636 return ret;
bellard2efbe912005-07-23 15:10:20 +00001637 if (len > lv)
1638 len = lv;
bellard2f619692007-11-16 10:46:05 +00001639 if (len == 4) {
1640 if (put_user_u32(val, optval_addr))
1641 return -TARGET_EFAULT;
1642 } else {
1643 if (put_user_u8(val, optval_addr))
1644 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001645 }
bellard2f619692007-11-16 10:46:05 +00001646 if (put_user_u32(len, optlen))
1647 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001648 break;
1649 case SOL_IP:
1650 switch(optname) {
1651 case IP_TOS:
1652 case IP_TTL:
1653 case IP_HDRINCL:
1654 case IP_ROUTER_ALERT:
1655 case IP_RECVOPTS:
1656 case IP_RETOPTS:
1657 case IP_PKTINFO:
1658 case IP_MTU_DISCOVER:
1659 case IP_RECVERR:
1660 case IP_RECVTOS:
1661#ifdef IP_FREEBIND
1662 case IP_FREEBIND:
1663#endif
1664 case IP_MULTICAST_TTL:
1665 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001666 if (get_user_u32(len, optlen))
1667 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001668 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001669 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001670 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001671 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1672 if (ret < 0)
1673 return ret;
bellard2efbe912005-07-23 15:10:20 +00001674 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001675 len = 1;
bellard2f619692007-11-16 10:46:05 +00001676 if (put_user_u32(len, optlen)
1677 || put_user_u8(val, optval_addr))
1678 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001679 } else {
bellard2efbe912005-07-23 15:10:20 +00001680 if (len > sizeof(int))
1681 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001682 if (put_user_u32(len, optlen)
1683 || put_user_u32(val, optval_addr))
1684 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001685 }
bellard8853f862004-02-22 14:57:26 +00001686 break;
bellard2efbe912005-07-23 15:10:20 +00001687 default:
thsc02f4992007-12-18 02:39:59 +00001688 ret = -TARGET_ENOPROTOOPT;
1689 break;
bellard8853f862004-02-22 14:57:26 +00001690 }
1691 break;
1692 default:
1693 unimplemented:
1694 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1695 level, optname);
thsc02f4992007-12-18 02:39:59 +00001696 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001697 break;
1698 }
1699 return ret;
bellard7854b052003-03-29 17:22:23 +00001700}
1701
bellard579a97f2007-11-11 14:26:47 +00001702/* FIXME
1703 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1704 * other lock functions have a return code of 0 for failure.
1705 */
1706static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1707 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001708{
1709 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001710 abi_ulong base;
balrogd732dcb2008-10-28 10:21:03 +00001711 int i;
pbrook53a59602006-03-25 19:31:22 +00001712
bellard579a97f2007-11-11 14:26:47 +00001713 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1714 if (!target_vec)
1715 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001716 for(i = 0;i < count; i++) {
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001717 base = tswapal(target_vec[i].iov_base);
1718 vec[i].iov_len = tswapal(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001719 if (vec[i].iov_len != 0) {
1720 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrogd732dcb2008-10-28 10:21:03 +00001721 /* Don't check lock_user return value. We must call writev even
1722 if a element has invalid base address. */
bellard41df8412008-02-04 22:26:57 +00001723 } else {
1724 /* zero length pointer is ignored */
1725 vec[i].iov_base = NULL;
1726 }
pbrook53a59602006-03-25 19:31:22 +00001727 }
1728 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001729 return 0;
pbrook53a59602006-03-25 19:31:22 +00001730}
1731
bellard579a97f2007-11-11 14:26:47 +00001732static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1733 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001734{
1735 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001736 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001737 int i;
1738
bellard579a97f2007-11-11 14:26:47 +00001739 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1740 if (!target_vec)
1741 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001742 for(i = 0;i < count; i++) {
balrogd732dcb2008-10-28 10:21:03 +00001743 if (target_vec[i].iov_base) {
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001744 base = tswapal(target_vec[i].iov_base);
balrogd732dcb2008-10-28 10:21:03 +00001745 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1746 }
pbrook53a59602006-03-25 19:31:22 +00001747 }
1748 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001749
1750 return 0;
pbrook53a59602006-03-25 19:31:22 +00001751}
1752
ths0da46a62007-10-20 20:23:07 +00001753/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001754static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001755{
1756#if defined(TARGET_MIPS)
1757 switch(type) {
1758 case TARGET_SOCK_DGRAM:
1759 type = SOCK_DGRAM;
1760 break;
1761 case TARGET_SOCK_STREAM:
1762 type = SOCK_STREAM;
1763 break;
1764 case TARGET_SOCK_RAW:
1765 type = SOCK_RAW;
1766 break;
1767 case TARGET_SOCK_RDM:
1768 type = SOCK_RDM;
1769 break;
1770 case TARGET_SOCK_SEQPACKET:
1771 type = SOCK_SEQPACKET;
1772 break;
1773 case TARGET_SOCK_PACKET:
1774 type = SOCK_PACKET;
1775 break;
1776 }
1777#endif
balrog12bc92a2007-10-30 21:06:14 +00001778 if (domain == PF_NETLINK)
1779 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001780 return get_errno(socket(domain, type, protocol));
1781}
1782
ths0da46a62007-10-20 20:23:07 +00001783/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001784static abi_long do_bind(int sockfd, abi_ulong target_addr,
1785 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001786{
aurel328f7aeaf2009-01-30 19:47:57 +00001787 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001788 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001789
Blue Swirl38724252010-09-18 05:53:14 +00001790 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001791 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001792 }
aurel328f7aeaf2009-01-30 19:47:57 +00001793
aurel32607175e2009-04-15 16:11:59 +00001794 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001795
Arnaud Patard917507b2009-06-19 10:44:45 +03001796 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1797 if (ret)
1798 return ret;
1799
bellard3532fa72006-06-24 15:06:03 +00001800 return get_errno(bind(sockfd, addr, addrlen));
1801}
1802
ths0da46a62007-10-20 20:23:07 +00001803/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001804static abi_long do_connect(int sockfd, abi_ulong target_addr,
1805 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001806{
aurel328f7aeaf2009-01-30 19:47:57 +00001807 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001808 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001809
Blue Swirl38724252010-09-18 05:53:14 +00001810 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001811 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001812 }
aurel328f7aeaf2009-01-30 19:47:57 +00001813
1814 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001815
Arnaud Patard917507b2009-06-19 10:44:45 +03001816 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1817 if (ret)
1818 return ret;
1819
bellard3532fa72006-06-24 15:06:03 +00001820 return get_errno(connect(sockfd, addr, addrlen));
1821}
1822
ths0da46a62007-10-20 20:23:07 +00001823/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001824static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1825 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001826{
balrog6de645c2008-10-28 10:26:29 +00001827 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001828 struct target_msghdr *msgp;
1829 struct msghdr msg;
1830 int count;
1831 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001832 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001833
bellard579a97f2007-11-11 14:26:47 +00001834 /* FIXME */
1835 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1836 msgp,
1837 target_msg,
1838 send ? 1 : 0))
1839 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001840 if (msgp->msg_name) {
1841 msg.msg_namelen = tswap32(msgp->msg_namelen);
1842 msg.msg_name = alloca(msg.msg_namelen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001843 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001844 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001845 if (ret) {
1846 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1847 return ret;
1848 }
bellard3532fa72006-06-24 15:06:03 +00001849 } else {
1850 msg.msg_name = NULL;
1851 msg.msg_namelen = 0;
1852 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001853 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00001854 msg.msg_control = alloca(msg.msg_controllen);
1855 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001856
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001857 count = tswapal(msgp->msg_iovlen);
bellard3532fa72006-06-24 15:06:03 +00001858 vec = alloca(count * sizeof(struct iovec));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001859 target_vec = tswapal(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001860 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001861 msg.msg_iovlen = count;
1862 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001863
bellard3532fa72006-06-24 15:06:03 +00001864 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001865 ret = target_to_host_cmsg(&msg, msgp);
1866 if (ret == 0)
1867 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001868 } else {
1869 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001870 if (!is_error(ret)) {
1871 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001872 ret = host_to_target_cmsg(msgp, &msg);
balrog6de645c2008-10-28 10:26:29 +00001873 if (!is_error(ret))
1874 ret = len;
1875 }
bellard3532fa72006-06-24 15:06:03 +00001876 }
1877 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001878 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001879 return ret;
1880}
1881
ths0da46a62007-10-20 20:23:07 +00001882/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001883static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001884 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001885{
bellard2f619692007-11-16 10:46:05 +00001886 socklen_t addrlen;
1887 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001888 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001889
Arnaud Patard917507b2009-06-19 10:44:45 +03001890 if (target_addr == 0)
1891 return get_errno(accept(fd, NULL, NULL));
1892
1893 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00001894 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03001895 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00001896
Blue Swirl38724252010-09-18 05:53:14 +00001897 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001898 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001899 }
aurel328f7aeaf2009-01-30 19:47:57 +00001900
Arnaud Patard917507b2009-06-19 10:44:45 +03001901 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1902 return -TARGET_EINVAL;
1903
bellard2f619692007-11-16 10:46:05 +00001904 addr = alloca(addrlen);
1905
pbrook1be9e1d2006-11-19 15:26:04 +00001906 ret = get_errno(accept(fd, addr, &addrlen));
1907 if (!is_error(ret)) {
1908 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001909 if (put_user_u32(addrlen, target_addrlen_addr))
1910 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001911 }
1912 return ret;
1913}
1914
ths0da46a62007-10-20 20:23:07 +00001915/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001916static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001917 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001918{
bellard2f619692007-11-16 10:46:05 +00001919 socklen_t addrlen;
1920 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001921 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001922
bellard2f619692007-11-16 10:46:05 +00001923 if (get_user_u32(addrlen, target_addrlen_addr))
1924 return -TARGET_EFAULT;
1925
Blue Swirl38724252010-09-18 05:53:14 +00001926 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001927 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001928 }
aurel328f7aeaf2009-01-30 19:47:57 +00001929
Arnaud Patard917507b2009-06-19 10:44:45 +03001930 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1931 return -TARGET_EFAULT;
1932
bellard2f619692007-11-16 10:46:05 +00001933 addr = alloca(addrlen);
1934
pbrook1be9e1d2006-11-19 15:26:04 +00001935 ret = get_errno(getpeername(fd, addr, &addrlen));
1936 if (!is_error(ret)) {
1937 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001938 if (put_user_u32(addrlen, target_addrlen_addr))
1939 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001940 }
1941 return ret;
1942}
1943
ths0da46a62007-10-20 20:23:07 +00001944/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001945static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001946 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001947{
bellard2f619692007-11-16 10:46:05 +00001948 socklen_t addrlen;
1949 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001950 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001951
bellard2f619692007-11-16 10:46:05 +00001952 if (get_user_u32(addrlen, target_addrlen_addr))
1953 return -TARGET_EFAULT;
1954
Blue Swirl38724252010-09-18 05:53:14 +00001955 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001956 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001957 }
aurel328f7aeaf2009-01-30 19:47:57 +00001958
Arnaud Patard917507b2009-06-19 10:44:45 +03001959 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1960 return -TARGET_EFAULT;
1961
bellard2f619692007-11-16 10:46:05 +00001962 addr = alloca(addrlen);
1963
pbrook1be9e1d2006-11-19 15:26:04 +00001964 ret = get_errno(getsockname(fd, addr, &addrlen));
1965 if (!is_error(ret)) {
1966 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001967 if (put_user_u32(addrlen, target_addrlen_addr))
1968 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001969 }
1970 return ret;
1971}
1972
ths0da46a62007-10-20 20:23:07 +00001973/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001974static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001975 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001976{
1977 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001978 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001979
1980 ret = get_errno(socketpair(domain, type, protocol, tab));
1981 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001982 if (put_user_s32(tab[0], target_tab_addr)
1983 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1984 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001985 }
1986 return ret;
1987}
1988
ths0da46a62007-10-20 20:23:07 +00001989/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001990static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1991 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001992{
1993 void *addr;
1994 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001995 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001996
Blue Swirl38724252010-09-18 05:53:14 +00001997 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001998 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001999 }
aurel328f7aeaf2009-01-30 19:47:57 +00002000
bellard579a97f2007-11-11 14:26:47 +00002001 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2002 if (!host_msg)
2003 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002004 if (target_addr) {
2005 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002006 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2007 if (ret) {
2008 unlock_user(host_msg, msg, 0);
2009 return ret;
2010 }
pbrook1be9e1d2006-11-19 15:26:04 +00002011 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2012 } else {
2013 ret = get_errno(send(fd, host_msg, len, flags));
2014 }
2015 unlock_user(host_msg, msg, 0);
2016 return ret;
2017}
2018
ths0da46a62007-10-20 20:23:07 +00002019/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002020static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2021 abi_ulong target_addr,
2022 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002023{
2024 socklen_t addrlen;
2025 void *addr;
2026 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002027 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002028
bellard579a97f2007-11-11 14:26:47 +00002029 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2030 if (!host_msg)
2031 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002032 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00002033 if (get_user_u32(addrlen, target_addrlen)) {
2034 ret = -TARGET_EFAULT;
2035 goto fail;
2036 }
Blue Swirl38724252010-09-18 05:53:14 +00002037 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002038 ret = -TARGET_EINVAL;
2039 goto fail;
2040 }
pbrook1be9e1d2006-11-19 15:26:04 +00002041 addr = alloca(addrlen);
2042 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2043 } else {
2044 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00002045 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002046 }
2047 if (!is_error(ret)) {
2048 if (target_addr) {
2049 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002050 if (put_user_u32(addrlen, target_addrlen)) {
2051 ret = -TARGET_EFAULT;
2052 goto fail;
2053 }
pbrook1be9e1d2006-11-19 15:26:04 +00002054 }
2055 unlock_user(host_msg, msg, len);
2056 } else {
bellard2f619692007-11-16 10:46:05 +00002057fail:
pbrook1be9e1d2006-11-19 15:26:04 +00002058 unlock_user(host_msg, msg, 0);
2059 }
2060 return ret;
2061}
2062
j_mayer32407102007-09-26 23:01:49 +00002063#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00002064/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002065static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00002066{
blueswir1992f48a2007-10-14 16:27:31 +00002067 abi_long ret;
2068 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00002069
2070 switch(num) {
2071 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00002072 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002073 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002074
Ulrich Hecht98818182009-07-03 17:09:28 +02002075 if (get_user_ual(domain, vptr)
2076 || get_user_ual(type, vptr + n)
2077 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002078 return -TARGET_EFAULT;
2079
bellard3532fa72006-06-24 15:06:03 +00002080 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00002081 }
bellard31e31b82003-02-18 22:55:36 +00002082 break;
2083 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00002084 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002085 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002086 abi_ulong target_addr;
2087 socklen_t addrlen;
2088
Ulrich Hecht98818182009-07-03 17:09:28 +02002089 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002090 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002091 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002092 return -TARGET_EFAULT;
2093
bellard3532fa72006-06-24 15:06:03 +00002094 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002095 }
bellard31e31b82003-02-18 22:55:36 +00002096 break;
2097 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00002098 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002099 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002100 abi_ulong target_addr;
2101 socklen_t addrlen;
2102
Ulrich Hecht98818182009-07-03 17:09:28 +02002103 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002104 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002105 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002106 return -TARGET_EFAULT;
2107
bellard3532fa72006-06-24 15:06:03 +00002108 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002109 }
bellard31e31b82003-02-18 22:55:36 +00002110 break;
2111 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00002112 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002113 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00002114
Ulrich Hecht98818182009-07-03 17:09:28 +02002115 if (get_user_ual(sockfd, vptr)
2116 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002117 return -TARGET_EFAULT;
2118
bellard7854b052003-03-29 17:22:23 +00002119 ret = get_errno(listen(sockfd, backlog));
2120 }
bellard31e31b82003-02-18 22:55:36 +00002121 break;
2122 case SOCKOP_accept:
2123 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002124 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002125 abi_ulong target_addr, target_addrlen;
2126
Ulrich Hecht98818182009-07-03 17:09:28 +02002127 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002128 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002129 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002130 return -TARGET_EFAULT;
2131
pbrook1be9e1d2006-11-19 15:26:04 +00002132 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002133 }
2134 break;
2135 case SOCKOP_getsockname:
2136 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002137 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002138 abi_ulong target_addr, target_addrlen;
2139
Ulrich Hecht98818182009-07-03 17:09:28 +02002140 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002141 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002142 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002143 return -TARGET_EFAULT;
2144
pbrook1be9e1d2006-11-19 15:26:04 +00002145 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002146 }
2147 break;
2148 case SOCKOP_getpeername:
2149 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002150 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002151 abi_ulong target_addr, target_addrlen;
2152
Ulrich Hecht98818182009-07-03 17:09:28 +02002153 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002154 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002155 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002156 return -TARGET_EFAULT;
2157
pbrook1be9e1d2006-11-19 15:26:04 +00002158 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002159 }
2160 break;
2161 case SOCKOP_socketpair:
2162 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002163 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002164 abi_ulong tab;
2165
Ulrich Hecht98818182009-07-03 17:09:28 +02002166 if (get_user_ual(domain, vptr)
2167 || get_user_ual(type, vptr + n)
2168 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002169 || get_user_ual(tab, vptr + 3 * n))
2170 return -TARGET_EFAULT;
2171
pbrook1be9e1d2006-11-19 15:26:04 +00002172 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00002173 }
2174 break;
2175 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00002176 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002177 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002178 abi_ulong msg;
2179 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002180 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002181
Ulrich Hecht98818182009-07-03 17:09:28 +02002182 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002183 || get_user_ual(msg, vptr + n)
2184 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002185 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002186 return -TARGET_EFAULT;
2187
pbrook1be9e1d2006-11-19 15:26:04 +00002188 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002189 }
bellard31e31b82003-02-18 22:55:36 +00002190 break;
2191 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00002192 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002193 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002194 abi_ulong msg;
2195 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002196 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002197
Ulrich Hecht98818182009-07-03 17:09:28 +02002198 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002199 || get_user_ual(msg, vptr + n)
2200 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002201 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002202 return -TARGET_EFAULT;
2203
pbrook1be9e1d2006-11-19 15:26:04 +00002204 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002205 }
bellard31e31b82003-02-18 22:55:36 +00002206 break;
2207 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00002208 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002209 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002210 abi_ulong msg;
2211 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002212 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002213 abi_ulong addr;
2214 socklen_t addrlen;
2215
Ulrich Hecht98818182009-07-03 17:09:28 +02002216 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002217 || get_user_ual(msg, vptr + n)
2218 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002219 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002220 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002221 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002222 return -TARGET_EFAULT;
2223
pbrook1be9e1d2006-11-19 15:26:04 +00002224 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002225 }
bellard31e31b82003-02-18 22:55:36 +00002226 break;
2227 case SOCKOP_recvfrom:
2228 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002229 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002230 abi_ulong msg;
2231 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002232 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002233 abi_ulong addr;
2234 socklen_t addrlen;
2235
Ulrich Hecht98818182009-07-03 17:09:28 +02002236 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002237 || get_user_ual(msg, vptr + n)
2238 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002239 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002240 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002241 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002242 return -TARGET_EFAULT;
2243
pbrook1be9e1d2006-11-19 15:26:04 +00002244 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00002245 }
2246 break;
2247 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00002248 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002249 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00002250
Ulrich Hecht98818182009-07-03 17:09:28 +02002251 if (get_user_ual(sockfd, vptr)
2252 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002253 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002254
2255 ret = get_errno(shutdown(sockfd, how));
2256 }
bellard31e31b82003-02-18 22:55:36 +00002257 break;
2258 case SOCKOP_sendmsg:
2259 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002260 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002261 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002262 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002263 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002264
Ulrich Hecht98818182009-07-03 17:09:28 +02002265 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002266 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002267 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002268 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002269
ths5fafdf22007-09-16 21:08:06 +00002270 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002271 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002272 }
2273 break;
bellard31e31b82003-02-18 22:55:36 +00002274 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002275 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002276 abi_ulong sockfd;
2277 abi_ulong level;
2278 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002279 abi_ulong optval;
2280 socklen_t optlen;
2281
Ulrich Hecht98818182009-07-03 17:09:28 +02002282 if (get_user_ual(sockfd, vptr)
2283 || get_user_ual(level, vptr + n)
2284 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002285 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002286 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002287 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002288
2289 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2290 }
2291 break;
bellard31e31b82003-02-18 22:55:36 +00002292 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002293 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002294 abi_ulong sockfd;
2295 abi_ulong level;
2296 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002297 abi_ulong optval;
2298 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002299
Ulrich Hecht98818182009-07-03 17:09:28 +02002300 if (get_user_ual(sockfd, vptr)
2301 || get_user_ual(level, vptr + n)
2302 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002303 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002304 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002305 return -TARGET_EFAULT;
2306
2307 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002308 }
2309 break;
bellard31e31b82003-02-18 22:55:36 +00002310 default:
2311 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002312 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002313 break;
2314 }
2315 return ret;
2316}
j_mayer32407102007-09-26 23:01:49 +00002317#endif
bellard31e31b82003-02-18 22:55:36 +00002318
bellard8853f862004-02-22 14:57:26 +00002319#define N_SHM_REGIONS 32
2320
2321static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002322 abi_ulong start;
2323 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002324} shm_regions[N_SHM_REGIONS];
2325
ths3eb6b042007-06-03 14:26:27 +00002326struct target_ipc_perm
2327{
blueswir1992f48a2007-10-14 16:27:31 +00002328 abi_long __key;
2329 abi_ulong uid;
2330 abi_ulong gid;
2331 abi_ulong cuid;
2332 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002333 unsigned short int mode;
2334 unsigned short int __pad1;
2335 unsigned short int __seq;
2336 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002337 abi_ulong __unused1;
2338 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002339};
2340
2341struct target_semid_ds
2342{
2343 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002344 abi_ulong sem_otime;
2345 abi_ulong __unused1;
2346 abi_ulong sem_ctime;
2347 abi_ulong __unused2;
2348 abi_ulong sem_nsems;
2349 abi_ulong __unused3;
2350 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002351};
2352
bellard579a97f2007-11-11 14:26:47 +00002353static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2354 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002355{
2356 struct target_ipc_perm *target_ip;
2357 struct target_semid_ds *target_sd;
2358
bellard579a97f2007-11-11 14:26:47 +00002359 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2360 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002361 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002362 host_ip->__key = tswapal(target_ip->__key);
2363 host_ip->uid = tswapal(target_ip->uid);
2364 host_ip->gid = tswapal(target_ip->gid);
2365 host_ip->cuid = tswapal(target_ip->cuid);
2366 host_ip->cgid = tswapal(target_ip->cgid);
2367 host_ip->mode = tswap16(target_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002368 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002369 return 0;
ths3eb6b042007-06-03 14:26:27 +00002370}
2371
bellard579a97f2007-11-11 14:26:47 +00002372static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2373 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002374{
2375 struct target_ipc_perm *target_ip;
2376 struct target_semid_ds *target_sd;
2377
bellard579a97f2007-11-11 14:26:47 +00002378 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2379 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002380 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002381 target_ip->__key = tswapal(host_ip->__key);
2382 target_ip->uid = tswapal(host_ip->uid);
2383 target_ip->gid = tswapal(host_ip->gid);
2384 target_ip->cuid = tswapal(host_ip->cuid);
2385 target_ip->cgid = tswapal(host_ip->cgid);
2386 target_ip->mode = tswap16(host_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002387 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002388 return 0;
ths3eb6b042007-06-03 14:26:27 +00002389}
2390
bellard579a97f2007-11-11 14:26:47 +00002391static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2392 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002393{
2394 struct target_semid_ds *target_sd;
2395
bellard579a97f2007-11-11 14:26:47 +00002396 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2397 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002398 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2399 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002400 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2401 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2402 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002403 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002404 return 0;
ths3eb6b042007-06-03 14:26:27 +00002405}
2406
bellard579a97f2007-11-11 14:26:47 +00002407static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2408 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002409{
2410 struct target_semid_ds *target_sd;
2411
bellard579a97f2007-11-11 14:26:47 +00002412 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2413 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002414 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08002415 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002416 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2417 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2418 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002419 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002420 return 0;
ths3eb6b042007-06-03 14:26:27 +00002421}
2422
aurel32e5289082009-04-18 16:16:12 +00002423struct target_seminfo {
2424 int semmap;
2425 int semmni;
2426 int semmns;
2427 int semmnu;
2428 int semmsl;
2429 int semopm;
2430 int semume;
2431 int semusz;
2432 int semvmx;
2433 int semaem;
2434};
2435
2436static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2437 struct seminfo *host_seminfo)
2438{
2439 struct target_seminfo *target_seminfo;
2440 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2441 return -TARGET_EFAULT;
2442 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2443 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2444 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2445 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2446 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2447 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2448 __put_user(host_seminfo->semume, &target_seminfo->semume);
2449 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2450 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2451 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2452 unlock_user_struct(target_seminfo, target_addr, 1);
2453 return 0;
2454}
2455
thsfa294812007-02-02 22:05:00 +00002456union semun {
2457 int val;
ths3eb6b042007-06-03 14:26:27 +00002458 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002459 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002460 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002461};
2462
ths3eb6b042007-06-03 14:26:27 +00002463union target_semun {
2464 int val;
aurel32e5289082009-04-18 16:16:12 +00002465 abi_ulong buf;
2466 abi_ulong array;
2467 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002468};
2469
aurel32e5289082009-04-18 16:16:12 +00002470static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2471 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002472{
aurel32e5289082009-04-18 16:16:12 +00002473 int nsems;
2474 unsigned short *array;
2475 union semun semun;
2476 struct semid_ds semid_ds;
2477 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002478
aurel32e5289082009-04-18 16:16:12 +00002479 semun.buf = &semid_ds;
2480
2481 ret = semctl(semid, 0, IPC_STAT, semun);
2482 if (ret == -1)
2483 return get_errno(ret);
2484
2485 nsems = semid_ds.sem_nsems;
2486
2487 *host_array = malloc(nsems*sizeof(unsigned short));
2488 array = lock_user(VERIFY_READ, target_addr,
2489 nsems*sizeof(unsigned short), 1);
2490 if (!array)
2491 return -TARGET_EFAULT;
2492
2493 for(i=0; i<nsems; i++) {
2494 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002495 }
aurel32e5289082009-04-18 16:16:12 +00002496 unlock_user(array, target_addr, 0);
2497
bellard579a97f2007-11-11 14:26:47 +00002498 return 0;
ths3eb6b042007-06-03 14:26:27 +00002499}
2500
aurel32e5289082009-04-18 16:16:12 +00002501static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2502 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002503{
aurel32e5289082009-04-18 16:16:12 +00002504 int nsems;
2505 unsigned short *array;
2506 union semun semun;
2507 struct semid_ds semid_ds;
2508 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002509
aurel32e5289082009-04-18 16:16:12 +00002510 semun.buf = &semid_ds;
2511
2512 ret = semctl(semid, 0, IPC_STAT, semun);
2513 if (ret == -1)
2514 return get_errno(ret);
2515
2516 nsems = semid_ds.sem_nsems;
2517
2518 array = lock_user(VERIFY_WRITE, target_addr,
2519 nsems*sizeof(unsigned short), 0);
2520 if (!array)
2521 return -TARGET_EFAULT;
2522
2523 for(i=0; i<nsems; i++) {
2524 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002525 }
aurel32e5289082009-04-18 16:16:12 +00002526 free(*host_array);
2527 unlock_user(array, target_addr, 1);
2528
bellard579a97f2007-11-11 14:26:47 +00002529 return 0;
ths3eb6b042007-06-03 14:26:27 +00002530}
2531
aurel32e5289082009-04-18 16:16:12 +00002532static inline abi_long do_semctl(int semid, int semnum, int cmd,
2533 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002534{
2535 union semun arg;
2536 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302537 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002538 struct seminfo seminfo;
2539 abi_long ret = -TARGET_EINVAL;
2540 abi_long err;
2541 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002542
2543 switch( cmd ) {
2544 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002545 case SETVAL:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002546 arg.val = tswap32(target_su.val);
aurel32e5289082009-04-18 16:16:12 +00002547 ret = get_errno(semctl(semid, semnum, cmd, arg));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002548 target_su.val = tswap32(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002549 break;
2550 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002551 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002552 err = target_to_host_semarray(semid, &array, target_su.array);
2553 if (err)
2554 return err;
2555 arg.array = array;
2556 ret = get_errno(semctl(semid, semnum, cmd, arg));
2557 err = host_to_target_semarray(semid, target_su.array, &array);
2558 if (err)
2559 return err;
ths3eb6b042007-06-03 14:26:27 +00002560 break;
2561 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002562 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002563 case SEM_STAT:
2564 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2565 if (err)
2566 return err;
2567 arg.buf = &dsarg;
2568 ret = get_errno(semctl(semid, semnum, cmd, arg));
2569 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2570 if (err)
2571 return err;
ths3eb6b042007-06-03 14:26:27 +00002572 break;
aurel32e5289082009-04-18 16:16:12 +00002573 case IPC_INFO:
2574 case SEM_INFO:
2575 arg.__buf = &seminfo;
2576 ret = get_errno(semctl(semid, semnum, cmd, arg));
2577 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2578 if (err)
2579 return err;
2580 break;
2581 case IPC_RMID:
2582 case GETPID:
2583 case GETNCNT:
2584 case GETZCNT:
2585 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2586 break;
ths3eb6b042007-06-03 14:26:27 +00002587 }
2588
2589 return ret;
2590}
2591
aurel32e5289082009-04-18 16:16:12 +00002592struct target_sembuf {
2593 unsigned short sem_num;
2594 short sem_op;
2595 short sem_flg;
2596};
2597
2598static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2599 abi_ulong target_addr,
2600 unsigned nsops)
2601{
2602 struct target_sembuf *target_sembuf;
2603 int i;
2604
2605 target_sembuf = lock_user(VERIFY_READ, target_addr,
2606 nsops*sizeof(struct target_sembuf), 1);
2607 if (!target_sembuf)
2608 return -TARGET_EFAULT;
2609
2610 for(i=0; i<nsops; i++) {
2611 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2612 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2613 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2614 }
2615
2616 unlock_user(target_sembuf, target_addr, 0);
2617
2618 return 0;
2619}
2620
2621static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2622{
2623 struct sembuf sops[nsops];
2624
2625 if (target_to_host_sembuf(sops, ptr, nsops))
2626 return -TARGET_EFAULT;
2627
2628 return semop(semid, sops, nsops);
2629}
2630
ths1bc012f2007-06-03 14:27:49 +00002631struct target_msqid_ds
2632{
aurel321c54ff92008-10-13 21:08:44 +00002633 struct target_ipc_perm msg_perm;
2634 abi_ulong msg_stime;
2635#if TARGET_ABI_BITS == 32
2636 abi_ulong __unused1;
2637#endif
2638 abi_ulong msg_rtime;
2639#if TARGET_ABI_BITS == 32
2640 abi_ulong __unused2;
2641#endif
2642 abi_ulong msg_ctime;
2643#if TARGET_ABI_BITS == 32
2644 abi_ulong __unused3;
2645#endif
2646 abi_ulong __msg_cbytes;
2647 abi_ulong msg_qnum;
2648 abi_ulong msg_qbytes;
2649 abi_ulong msg_lspid;
2650 abi_ulong msg_lrpid;
2651 abi_ulong __unused4;
2652 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002653};
2654
bellard579a97f2007-11-11 14:26:47 +00002655static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2656 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002657{
2658 struct target_msqid_ds *target_md;
2659
bellard579a97f2007-11-11 14:26:47 +00002660 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2661 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002662 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2663 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002664 host_md->msg_stime = tswapal(target_md->msg_stime);
2665 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2666 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2667 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2668 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2669 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2670 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2671 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002672 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002673 return 0;
ths1bc012f2007-06-03 14:27:49 +00002674}
2675
bellard579a97f2007-11-11 14:26:47 +00002676static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2677 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002678{
2679 struct target_msqid_ds *target_md;
2680
bellard579a97f2007-11-11 14:26:47 +00002681 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2682 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002683 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2684 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002685 target_md->msg_stime = tswapal(host_md->msg_stime);
2686 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2687 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2688 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2689 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2690 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2691 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2692 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002693 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002694 return 0;
ths1bc012f2007-06-03 14:27:49 +00002695}
2696
aurel321c54ff92008-10-13 21:08:44 +00002697struct target_msginfo {
2698 int msgpool;
2699 int msgmap;
2700 int msgmax;
2701 int msgmnb;
2702 int msgmni;
2703 int msgssz;
2704 int msgtql;
2705 unsigned short int msgseg;
2706};
2707
2708static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2709 struct msginfo *host_msginfo)
2710{
2711 struct target_msginfo *target_msginfo;
2712 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2713 return -TARGET_EFAULT;
2714 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2715 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2716 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2717 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2718 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2719 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2720 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2721 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2722 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002723 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002724}
2725
2726static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002727{
2728 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002729 struct msginfo msginfo;
2730 abi_long ret = -TARGET_EINVAL;
2731
2732 cmd &= 0xff;
2733
2734 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002735 case IPC_STAT:
2736 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002737 case MSG_STAT:
2738 if (target_to_host_msqid_ds(&dsarg,ptr))
2739 return -TARGET_EFAULT;
2740 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2741 if (host_to_target_msqid_ds(ptr,&dsarg))
2742 return -TARGET_EFAULT;
2743 break;
2744 case IPC_RMID:
2745 ret = get_errno(msgctl(msgid, cmd, NULL));
2746 break;
2747 case IPC_INFO:
2748 case MSG_INFO:
2749 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2750 if (host_to_target_msginfo(ptr, &msginfo))
2751 return -TARGET_EFAULT;
2752 break;
ths1bc012f2007-06-03 14:27:49 +00002753 }
aurel321c54ff92008-10-13 21:08:44 +00002754
ths1bc012f2007-06-03 14:27:49 +00002755 return ret;
2756}
2757
2758struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002759 abi_long mtype;
2760 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002761};
2762
blueswir1992f48a2007-10-14 16:27:31 +00002763static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2764 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002765{
2766 struct target_msgbuf *target_mb;
2767 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002768 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002769
bellard579a97f2007-11-11 14:26:47 +00002770 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2771 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002772 host_mb = malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002773 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00002774 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002775 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2776 free(host_mb);
2777 unlock_user_struct(target_mb, msgp, 0);
2778
2779 return ret;
2780}
2781
blueswir1992f48a2007-10-14 16:27:31 +00002782static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002783 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002784 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002785{
2786 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002787 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002788 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002789 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002790
bellard579a97f2007-11-11 14:26:47 +00002791 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2792 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002793
ths1bc012f2007-06-03 14:27:49 +00002794 host_mb = malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002795 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapal(msgtyp), msgflg));
aurel321c54ff92008-10-13 21:08:44 +00002796
bellard579a97f2007-11-11 14:26:47 +00002797 if (ret > 0) {
2798 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2799 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2800 if (!target_mtext) {
2801 ret = -TARGET_EFAULT;
2802 goto end;
2803 }
aurel321c54ff92008-10-13 21:08:44 +00002804 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002805 unlock_user(target_mtext, target_mtext_addr, ret);
2806 }
aurel321c54ff92008-10-13 21:08:44 +00002807
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002808 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00002809 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002810
bellard579a97f2007-11-11 14:26:47 +00002811end:
2812 if (target_mb)
2813 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00002814 return ret;
2815}
2816
Riku Voipio88a8c982009-04-03 10:42:00 +03002817struct target_shmid_ds
2818{
2819 struct target_ipc_perm shm_perm;
2820 abi_ulong shm_segsz;
2821 abi_ulong shm_atime;
2822#if TARGET_ABI_BITS == 32
2823 abi_ulong __unused1;
2824#endif
2825 abi_ulong shm_dtime;
2826#if TARGET_ABI_BITS == 32
2827 abi_ulong __unused2;
2828#endif
2829 abi_ulong shm_ctime;
2830#if TARGET_ABI_BITS == 32
2831 abi_ulong __unused3;
2832#endif
2833 int shm_cpid;
2834 int shm_lpid;
2835 abi_ulong shm_nattch;
2836 unsigned long int __unused4;
2837 unsigned long int __unused5;
2838};
2839
2840static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2841 abi_ulong target_addr)
2842{
2843 struct target_shmid_ds *target_sd;
2844
2845 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2846 return -TARGET_EFAULT;
2847 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2848 return -TARGET_EFAULT;
2849 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2850 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2851 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2852 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2853 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2854 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2855 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2856 unlock_user_struct(target_sd, target_addr, 0);
2857 return 0;
2858}
2859
2860static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2861 struct shmid_ds *host_sd)
2862{
2863 struct target_shmid_ds *target_sd;
2864
2865 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2866 return -TARGET_EFAULT;
2867 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2868 return -TARGET_EFAULT;
2869 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2870 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2871 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2872 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2873 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2874 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2875 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2876 unlock_user_struct(target_sd, target_addr, 1);
2877 return 0;
2878}
2879
2880struct target_shminfo {
2881 abi_ulong shmmax;
2882 abi_ulong shmmin;
2883 abi_ulong shmmni;
2884 abi_ulong shmseg;
2885 abi_ulong shmall;
2886};
2887
2888static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2889 struct shminfo *host_shminfo)
2890{
2891 struct target_shminfo *target_shminfo;
2892 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2893 return -TARGET_EFAULT;
2894 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2895 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2896 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2897 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2898 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2899 unlock_user_struct(target_shminfo, target_addr, 1);
2900 return 0;
2901}
2902
2903struct target_shm_info {
2904 int used_ids;
2905 abi_ulong shm_tot;
2906 abi_ulong shm_rss;
2907 abi_ulong shm_swp;
2908 abi_ulong swap_attempts;
2909 abi_ulong swap_successes;
2910};
2911
2912static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2913 struct shm_info *host_shm_info)
2914{
2915 struct target_shm_info *target_shm_info;
2916 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2917 return -TARGET_EFAULT;
2918 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2919 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2920 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2921 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2922 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2923 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2924 unlock_user_struct(target_shm_info, target_addr, 1);
2925 return 0;
2926}
2927
2928static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2929{
2930 struct shmid_ds dsarg;
2931 struct shminfo shminfo;
2932 struct shm_info shm_info;
2933 abi_long ret = -TARGET_EINVAL;
2934
2935 cmd &= 0xff;
2936
2937 switch(cmd) {
2938 case IPC_STAT:
2939 case IPC_SET:
2940 case SHM_STAT:
2941 if (target_to_host_shmid_ds(&dsarg, buf))
2942 return -TARGET_EFAULT;
2943 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2944 if (host_to_target_shmid_ds(buf, &dsarg))
2945 return -TARGET_EFAULT;
2946 break;
2947 case IPC_INFO:
2948 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2949 if (host_to_target_shminfo(buf, &shminfo))
2950 return -TARGET_EFAULT;
2951 break;
2952 case SHM_INFO:
2953 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2954 if (host_to_target_shm_info(buf, &shm_info))
2955 return -TARGET_EFAULT;
2956 break;
2957 case IPC_RMID:
2958 case SHM_LOCK:
2959 case SHM_UNLOCK:
2960 ret = get_errno(shmctl(shmid, cmd, NULL));
2961 break;
2962 }
2963
2964 return ret;
2965}
2966
2967static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2968{
2969 abi_long raddr;
2970 void *host_raddr;
2971 struct shmid_ds shm_info;
2972 int i,ret;
2973
2974 /* find out the length of the shared memory segment */
2975 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2976 if (is_error(ret)) {
2977 /* can't get length, bail out */
2978 return ret;
2979 }
2980
2981 mmap_lock();
2982
2983 if (shmaddr)
2984 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2985 else {
2986 abi_ulong mmap_start;
2987
2988 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2989
2990 if (mmap_start == -1) {
2991 errno = ENOMEM;
2992 host_raddr = (void *)-1;
2993 } else
2994 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2995 }
2996
2997 if (host_raddr == (void *)-1) {
2998 mmap_unlock();
2999 return get_errno((long)host_raddr);
3000 }
3001 raddr=h2g((unsigned long)host_raddr);
3002
3003 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3004 PAGE_VALID | PAGE_READ |
3005 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3006
3007 for (i = 0; i < N_SHM_REGIONS; i++) {
3008 if (shm_regions[i].start == 0) {
3009 shm_regions[i].start = raddr;
3010 shm_regions[i].size = shm_info.shm_segsz;
3011 break;
3012 }
3013 }
3014
3015 mmap_unlock();
3016 return raddr;
3017
3018}
3019
3020static inline abi_long do_shmdt(abi_ulong shmaddr)
3021{
3022 int i;
3023
3024 for (i = 0; i < N_SHM_REGIONS; ++i) {
3025 if (shm_regions[i].start == shmaddr) {
3026 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09003027 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03003028 break;
3029 }
3030 }
3031
3032 return get_errno(shmdt(g2h(shmaddr)));
3033}
3034
aurel321c54ff92008-10-13 21:08:44 +00003035#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00003036/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00003037/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003038static abi_long do_ipc(unsigned int call, int first,
3039 int second, int third,
3040 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00003041{
3042 int version;
blueswir1992f48a2007-10-14 16:27:31 +00003043 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00003044
3045 version = call >> 16;
3046 call &= 0xffff;
3047
3048 switch (call) {
thsfa294812007-02-02 22:05:00 +00003049 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00003050 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00003051 break;
3052
3053 case IPCOP_semget:
3054 ret = get_errno(semget(first, second, third));
3055 break;
3056
3057 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00003058 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00003059 break;
thsd96372e2007-02-02 22:05:44 +00003060
aurel321c54ff92008-10-13 21:08:44 +00003061 case IPCOP_msgget:
3062 ret = get_errno(msgget(first, second));
3063 break;
thsd96372e2007-02-02 22:05:44 +00003064
aurel321c54ff92008-10-13 21:08:44 +00003065 case IPCOP_msgsnd:
3066 ret = do_msgsnd(first, ptr, second, third);
3067 break;
thsd96372e2007-02-02 22:05:44 +00003068
aurel321c54ff92008-10-13 21:08:44 +00003069 case IPCOP_msgctl:
3070 ret = do_msgctl(first, second, ptr);
3071 break;
thsd96372e2007-02-02 22:05:44 +00003072
aurel321c54ff92008-10-13 21:08:44 +00003073 case IPCOP_msgrcv:
3074 switch (version) {
3075 case 0:
3076 {
3077 struct target_ipc_kludge {
3078 abi_long msgp;
3079 abi_long msgtyp;
3080 } *tmp;
thsd96372e2007-02-02 22:05:44 +00003081
aurel321c54ff92008-10-13 21:08:44 +00003082 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3083 ret = -TARGET_EFAULT;
3084 break;
ths1bc012f2007-06-03 14:27:49 +00003085 }
aurel321c54ff92008-10-13 21:08:44 +00003086
3087 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
3088
3089 unlock_user_struct(tmp, ptr, 0);
3090 break;
3091 }
3092 default:
3093 ret = do_msgrcv(first, ptr, second, fifth, third);
3094 }
3095 break;
thsd96372e2007-02-02 22:05:44 +00003096
bellard8853f862004-02-22 14:57:26 +00003097 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03003098 switch (version) {
3099 default:
bellard5a4a8982007-11-11 17:39:18 +00003100 {
3101 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03003102 raddr = do_shmat(first, ptr, second);
3103 if (is_error(raddr))
3104 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00003105 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00003106 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03003107 break;
3108 }
3109 case 1:
3110 ret = -TARGET_EINVAL;
3111 break;
bellard5a4a8982007-11-11 17:39:18 +00003112 }
bellard8853f862004-02-22 14:57:26 +00003113 break;
3114 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03003115 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00003116 break;
3117
3118 case IPCOP_shmget:
3119 /* IPC_* flag values are the same on all linux platforms */
3120 ret = get_errno(shmget(first, second, third));
3121 break;
3122
3123 /* IPC_* and SHM_* command values are the same on all linux platforms */
3124 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03003125 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00003126 break;
3127 default:
j_mayer32407102007-09-26 23:01:49 +00003128 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00003129 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00003130 break;
3131 }
3132 return ret;
3133}
j_mayer32407102007-09-26 23:01:49 +00003134#endif
bellard8853f862004-02-22 14:57:26 +00003135
bellard31e31b82003-02-18 22:55:36 +00003136/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00003137
Blue Swirl001faf32009-05-13 17:53:17 +00003138#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00003139#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3140enum {
3141#include "syscall_types.h"
3142};
3143#undef STRUCT
3144#undef STRUCT_SPECIAL
3145
Blue Swirl001faf32009-05-13 17:53:17 +00003146#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00003147#define STRUCT_SPECIAL(name)
3148#include "syscall_types.h"
3149#undef STRUCT
3150#undef STRUCT_SPECIAL
3151
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003152typedef struct IOCTLEntry IOCTLEntry;
3153
3154typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3155 int fd, abi_long cmd, abi_long arg);
3156
3157struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00003158 unsigned int target_cmd;
3159 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00003160 const char *name;
3161 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003162 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003163 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003164};
bellard31e31b82003-02-18 22:55:36 +00003165
3166#define IOC_R 0x0001
3167#define IOC_W 0x0002
3168#define IOC_RW (IOC_R | IOC_W)
3169
3170#define MAX_STRUCT_SIZE 4096
3171
Peter Maydelldace20d2011-01-10 13:11:24 +00003172#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003173/* So fiemap access checks don't overflow on 32 bit systems.
3174 * This is very slightly smaller than the limit imposed by
3175 * the underlying kernel.
3176 */
3177#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3178 / sizeof(struct fiemap_extent))
3179
3180static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3181 int fd, abi_long cmd, abi_long arg)
3182{
3183 /* The parameter for this ioctl is a struct fiemap followed
3184 * by an array of struct fiemap_extent whose size is set
3185 * in fiemap->fm_extent_count. The array is filled in by the
3186 * ioctl.
3187 */
3188 int target_size_in, target_size_out;
3189 struct fiemap *fm;
3190 const argtype *arg_type = ie->arg_type;
3191 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3192 void *argptr, *p;
3193 abi_long ret;
3194 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3195 uint32_t outbufsz;
3196 int free_fm = 0;
3197
3198 assert(arg_type[0] == TYPE_PTR);
3199 assert(ie->access == IOC_RW);
3200 arg_type++;
3201 target_size_in = thunk_type_size(arg_type, 0);
3202 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3203 if (!argptr) {
3204 return -TARGET_EFAULT;
3205 }
3206 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3207 unlock_user(argptr, arg, 0);
3208 fm = (struct fiemap *)buf_temp;
3209 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3210 return -TARGET_EINVAL;
3211 }
3212
3213 outbufsz = sizeof (*fm) +
3214 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3215
3216 if (outbufsz > MAX_STRUCT_SIZE) {
3217 /* We can't fit all the extents into the fixed size buffer.
3218 * Allocate one that is large enough and use it instead.
3219 */
3220 fm = malloc(outbufsz);
3221 if (!fm) {
3222 return -TARGET_ENOMEM;
3223 }
3224 memcpy(fm, buf_temp, sizeof(struct fiemap));
3225 free_fm = 1;
3226 }
3227 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3228 if (!is_error(ret)) {
3229 target_size_out = target_size_in;
3230 /* An extent_count of 0 means we were only counting the extents
3231 * so there are no structs to copy
3232 */
3233 if (fm->fm_extent_count != 0) {
3234 target_size_out += fm->fm_mapped_extents * extent_size;
3235 }
3236 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3237 if (!argptr) {
3238 ret = -TARGET_EFAULT;
3239 } else {
3240 /* Convert the struct fiemap */
3241 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3242 if (fm->fm_extent_count != 0) {
3243 p = argptr + target_size_in;
3244 /* ...and then all the struct fiemap_extents */
3245 for (i = 0; i < fm->fm_mapped_extents; i++) {
3246 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3247 THUNK_TARGET);
3248 p += extent_size;
3249 }
3250 }
3251 unlock_user(argptr, arg, target_size_out);
3252 }
3253 }
3254 if (free_fm) {
3255 free(fm);
3256 }
3257 return ret;
3258}
Peter Maydelldace20d2011-01-10 13:11:24 +00003259#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003260
Laurent Vivier059c2f22011-03-30 00:12:12 +02003261static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3262 int fd, abi_long cmd, abi_long arg)
3263{
3264 const argtype *arg_type = ie->arg_type;
3265 int target_size;
3266 void *argptr;
3267 int ret;
3268 struct ifconf *host_ifconf;
3269 uint32_t outbufsz;
3270 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3271 int target_ifreq_size;
3272 int nb_ifreq;
3273 int free_buf = 0;
3274 int i;
3275 int target_ifc_len;
3276 abi_long target_ifc_buf;
3277 int host_ifc_len;
3278 char *host_ifc_buf;
3279
3280 assert(arg_type[0] == TYPE_PTR);
3281 assert(ie->access == IOC_RW);
3282
3283 arg_type++;
3284 target_size = thunk_type_size(arg_type, 0);
3285
3286 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3287 if (!argptr)
3288 return -TARGET_EFAULT;
3289 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3290 unlock_user(argptr, arg, 0);
3291
3292 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3293 target_ifc_len = host_ifconf->ifc_len;
3294 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3295
3296 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3297 nb_ifreq = target_ifc_len / target_ifreq_size;
3298 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3299
3300 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3301 if (outbufsz > MAX_STRUCT_SIZE) {
3302 /* We can't fit all the extents into the fixed size buffer.
3303 * Allocate one that is large enough and use it instead.
3304 */
3305 host_ifconf = malloc(outbufsz);
3306 if (!host_ifconf) {
3307 return -TARGET_ENOMEM;
3308 }
3309 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3310 free_buf = 1;
3311 }
3312 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3313
3314 host_ifconf->ifc_len = host_ifc_len;
3315 host_ifconf->ifc_buf = host_ifc_buf;
3316
3317 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3318 if (!is_error(ret)) {
3319 /* convert host ifc_len to target ifc_len */
3320
3321 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3322 target_ifc_len = nb_ifreq * target_ifreq_size;
3323 host_ifconf->ifc_len = target_ifc_len;
3324
3325 /* restore target ifc_buf */
3326
3327 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3328
3329 /* copy struct ifconf to target user */
3330
3331 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3332 if (!argptr)
3333 return -TARGET_EFAULT;
3334 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3335 unlock_user(argptr, arg, target_size);
3336
3337 /* copy ifreq[] to target user */
3338
3339 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3340 for (i = 0; i < nb_ifreq ; i++) {
3341 thunk_convert(argptr + i * target_ifreq_size,
3342 host_ifc_buf + i * sizeof(struct ifreq),
3343 ifreq_arg_type, THUNK_TARGET);
3344 }
3345 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3346 }
3347
3348 if (free_buf) {
3349 free(host_ifconf);
3350 }
3351
3352 return ret;
3353}
3354
blueswir19f106a72008-10-05 10:52:52 +00003355static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00003356#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003357 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3358#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3359 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00003360#include "ioctls.h"
3361 { 0, 0, },
3362};
3363
pbrook53a59602006-03-25 19:31:22 +00003364/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00003365/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003366static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00003367{
3368 const IOCTLEntry *ie;
3369 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00003370 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003371 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00003372 int target_size;
3373 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00003374
3375 ie = ioctl_entries;
3376 for(;;) {
3377 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003378 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003379 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003380 }
3381 if (ie->target_cmd == cmd)
3382 break;
3383 ie++;
3384 }
3385 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003386#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003387 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003388#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003389 if (ie->do_ioctl) {
3390 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3391 }
3392
bellard31e31b82003-02-18 22:55:36 +00003393 switch(arg_type[0]) {
3394 case TYPE_NULL:
3395 /* no argument */
3396 ret = get_errno(ioctl(fd, ie->host_cmd));
3397 break;
3398 case TYPE_PTRVOID:
3399 case TYPE_INT:
3400 /* int argment */
3401 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3402 break;
3403 case TYPE_PTR:
3404 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00003405 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00003406 switch(ie->access) {
3407 case IOC_R:
3408 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3409 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003410 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3411 if (!argptr)
3412 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003413 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3414 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003415 }
3416 break;
3417 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00003418 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3419 if (!argptr)
3420 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003421 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3422 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003423 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3424 break;
3425 default:
3426 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00003427 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3428 if (!argptr)
3429 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003430 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3431 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003432 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3433 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003434 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3435 if (!argptr)
3436 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003437 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3438 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003439 }
3440 break;
3441 }
3442 break;
3443 default:
j_mayer32407102007-09-26 23:01:49 +00003444 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3445 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003446 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003447 break;
3448 }
3449 return ret;
3450}
3451
blueswir1b39bc502008-10-05 10:51:10 +00003452static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003453 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3454 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3455 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3456 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3457 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3458 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3459 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3460 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3461 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3462 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3463 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3464 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3465 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3466 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3467 { 0, 0, 0, 0 }
3468};
3469
blueswir1b39bc502008-10-05 10:51:10 +00003470static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003471 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3472 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3473 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3474 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3475 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3476 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3477 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3478 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3479 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3480 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3481 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3482 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3483 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3484 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3485 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3486 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3487 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3488 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3489 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3490 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3491 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3492 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3493 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3494 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3495 { 0, 0, 0, 0 }
3496};
3497
blueswir1b39bc502008-10-05 10:51:10 +00003498static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003499 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3500 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3501 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3502 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3503 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3504 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3505 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3506 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3507 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3508 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3509 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3510 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3511 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3512 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3513 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3514 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3515 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3516 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3517 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3518 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3519 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3520 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3521 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3522 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3523 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3524 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3525 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3526 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3527 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3528 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3529 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3530 { 0, 0, 0, 0 }
3531};
3532
blueswir1b39bc502008-10-05 10:51:10 +00003533static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003534 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3535 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3536 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3537 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3538 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3539 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3540 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3541 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3542 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3543 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3544 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3545 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3546 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3547 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3548 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3549 { 0, 0, 0, 0 }
3550};
3551
3552static void target_to_host_termios (void *dst, const void *src)
3553{
3554 struct host_termios *host = dst;
3555 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003556
ths5fafdf22007-09-16 21:08:06 +00003557 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003558 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003559 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003560 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003561 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003562 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003563 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003564 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3565 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003566
Arnaud Patard44607122009-04-21 17:39:08 +03003567 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003568 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3569 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003570 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003571 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003572 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003573 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003574 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003575 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003576 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003577 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3578 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003579 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3580 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3581 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3582 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3583 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003584 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003585}
ths3b46e622007-09-17 08:09:54 +00003586
bellard31e31b82003-02-18 22:55:36 +00003587static void host_to_target_termios (void *dst, const void *src)
3588{
3589 struct target_termios *target = dst;
3590 const struct host_termios *host = src;
3591
ths5fafdf22007-09-16 21:08:06 +00003592 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003593 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003594 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003595 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003596 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003597 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003598 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003599 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3600 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003601
Arnaud Patard44607122009-04-21 17:39:08 +03003602 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003603 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3604 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3605 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3606 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3607 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3608 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3609 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3610 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3611 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3612 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3613 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3614 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3615 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3616 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3617 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3618 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3619 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3620}
3621
blueswir18e853dc2008-10-05 10:49:32 +00003622static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003623 .convert = { host_to_target_termios, target_to_host_termios },
3624 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3625 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3626};
3627
bellard5286db72003-06-05 00:57:30 +00003628static bitmask_transtbl mmap_flags_tbl[] = {
3629 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3630 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3631 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3632 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3633 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3634 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3635 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3636 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3637 { 0, 0, 0, 0 }
3638};
3639
bellard2ab83ea2003-06-15 19:56:46 +00003640#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003641
3642/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003643static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003644
bellard03acab62007-11-11 14:57:14 +00003645static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003646{
3647 int size;
pbrook53a59602006-03-25 19:31:22 +00003648 void *p;
bellard6dbad632003-03-16 18:05:05 +00003649
3650 if (!ldt_table)
3651 return 0;
3652 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3653 if (size > bytecount)
3654 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003655 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3656 if (!p)
bellard03acab62007-11-11 14:57:14 +00003657 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003658 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003659 memcpy(p, ldt_table, size);
3660 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003661 return size;
3662}
3663
3664/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003665static abi_long write_ldt(CPUX86State *env,
3666 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003667{
3668 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003669 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003670 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003671 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003672 uint32_t *lp, entry_1, entry_2;
3673
3674 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003675 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003676 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003677 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003678 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003679 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00003680 ldt_info.limit = tswap32(target_ldt_info->limit);
3681 ldt_info.flags = tswap32(target_ldt_info->flags);
3682 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003683
bellard6dbad632003-03-16 18:05:05 +00003684 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003685 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003686 seg_32bit = ldt_info.flags & 1;
3687 contents = (ldt_info.flags >> 1) & 3;
3688 read_exec_only = (ldt_info.flags >> 3) & 1;
3689 limit_in_pages = (ldt_info.flags >> 4) & 1;
3690 seg_not_present = (ldt_info.flags >> 5) & 1;
3691 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003692#ifdef TARGET_ABI32
3693 lm = 0;
3694#else
3695 lm = (ldt_info.flags >> 7) & 1;
3696#endif
bellard6dbad632003-03-16 18:05:05 +00003697 if (contents == 3) {
3698 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003699 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003700 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003701 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003702 }
3703 /* allocate the LDT */
3704 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003705 env->ldt.base = target_mmap(0,
3706 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3707 PROT_READ|PROT_WRITE,
3708 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3709 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003710 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003711 memset(g2h(env->ldt.base), 0,
3712 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003713 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003714 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003715 }
3716
3717 /* NOTE: same code as Linux kernel */
3718 /* Allow LDTs to be cleared by the user. */
3719 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3720 if (oldmode ||
3721 (contents == 0 &&
3722 read_exec_only == 1 &&
3723 seg_32bit == 0 &&
3724 limit_in_pages == 0 &&
3725 seg_not_present == 1 &&
3726 useable == 0 )) {
3727 entry_1 = 0;
3728 entry_2 = 0;
3729 goto install;
3730 }
3731 }
ths3b46e622007-09-17 08:09:54 +00003732
bellard6dbad632003-03-16 18:05:05 +00003733 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3734 (ldt_info.limit & 0x0ffff);
3735 entry_2 = (ldt_info.base_addr & 0xff000000) |
3736 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3737 (ldt_info.limit & 0xf0000) |
3738 ((read_exec_only ^ 1) << 9) |
3739 (contents << 10) |
3740 ((seg_not_present ^ 1) << 15) |
3741 (seg_32bit << 22) |
3742 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00003743 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00003744 0x7000;
3745 if (!oldmode)
3746 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00003747
bellard6dbad632003-03-16 18:05:05 +00003748 /* Install the new entry ... */
3749install:
3750 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3751 lp[0] = tswap32(entry_1);
3752 lp[1] = tswap32(entry_2);
3753 return 0;
3754}
3755
3756/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00003757static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3758 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003759{
bellard03acab62007-11-11 14:57:14 +00003760 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00003761
bellard6dbad632003-03-16 18:05:05 +00003762 switch (func) {
3763 case 0:
3764 ret = read_ldt(ptr, bytecount);
3765 break;
3766 case 1:
3767 ret = write_ldt(env, ptr, bytecount, 1);
3768 break;
3769 case 0x11:
3770 ret = write_ldt(env, ptr, bytecount, 0);
3771 break;
bellard03acab62007-11-11 14:57:14 +00003772 default:
3773 ret = -TARGET_ENOSYS;
3774 break;
bellard6dbad632003-03-16 18:05:05 +00003775 }
3776 return ret;
3777}
bellard1b6b0292003-03-22 17:31:38 +00003778
blueswir14583f582008-08-24 10:35:55 +00003779#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00003780static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003781{
3782 uint64_t *gdt_table = g2h(env->gdt.base);
3783 struct target_modify_ldt_ldt_s ldt_info;
3784 struct target_modify_ldt_ldt_s *target_ldt_info;
3785 int seg_32bit, contents, read_exec_only, limit_in_pages;
3786 int seg_not_present, useable, lm;
3787 uint32_t *lp, entry_1, entry_2;
3788 int i;
3789
3790 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3791 if (!target_ldt_info)
3792 return -TARGET_EFAULT;
3793 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003794 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00003795 ldt_info.limit = tswap32(target_ldt_info->limit);
3796 ldt_info.flags = tswap32(target_ldt_info->flags);
3797 if (ldt_info.entry_number == -1) {
3798 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3799 if (gdt_table[i] == 0) {
3800 ldt_info.entry_number = i;
3801 target_ldt_info->entry_number = tswap32(i);
3802 break;
3803 }
3804 }
3805 }
3806 unlock_user_struct(target_ldt_info, ptr, 1);
3807
3808 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3809 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3810 return -TARGET_EINVAL;
3811 seg_32bit = ldt_info.flags & 1;
3812 contents = (ldt_info.flags >> 1) & 3;
3813 read_exec_only = (ldt_info.flags >> 3) & 1;
3814 limit_in_pages = (ldt_info.flags >> 4) & 1;
3815 seg_not_present = (ldt_info.flags >> 5) & 1;
3816 useable = (ldt_info.flags >> 6) & 1;
3817#ifdef TARGET_ABI32
3818 lm = 0;
3819#else
3820 lm = (ldt_info.flags >> 7) & 1;
3821#endif
3822
3823 if (contents == 3) {
3824 if (seg_not_present == 0)
3825 return -TARGET_EINVAL;
3826 }
3827
3828 /* NOTE: same code as Linux kernel */
3829 /* Allow LDTs to be cleared by the user. */
3830 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3831 if ((contents == 0 &&
3832 read_exec_only == 1 &&
3833 seg_32bit == 0 &&
3834 limit_in_pages == 0 &&
3835 seg_not_present == 1 &&
3836 useable == 0 )) {
3837 entry_1 = 0;
3838 entry_2 = 0;
3839 goto install;
3840 }
3841 }
3842
3843 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3844 (ldt_info.limit & 0x0ffff);
3845 entry_2 = (ldt_info.base_addr & 0xff000000) |
3846 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3847 (ldt_info.limit & 0xf0000) |
3848 ((read_exec_only ^ 1) << 9) |
3849 (contents << 10) |
3850 ((seg_not_present ^ 1) << 15) |
3851 (seg_32bit << 22) |
3852 (limit_in_pages << 23) |
3853 (useable << 20) |
3854 (lm << 21) |
3855 0x7000;
3856
3857 /* Install the new entry ... */
3858install:
3859 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3860 lp[0] = tswap32(entry_1);
3861 lp[1] = tswap32(entry_2);
3862 return 0;
3863}
3864
blueswir18fcd3692008-08-17 20:26:25 +00003865static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003866{
3867 struct target_modify_ldt_ldt_s *target_ldt_info;
3868 uint64_t *gdt_table = g2h(env->gdt.base);
3869 uint32_t base_addr, limit, flags;
3870 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3871 int seg_not_present, useable, lm;
3872 uint32_t *lp, entry_1, entry_2;
3873
3874 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3875 if (!target_ldt_info)
3876 return -TARGET_EFAULT;
3877 idx = tswap32(target_ldt_info->entry_number);
3878 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3879 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3880 unlock_user_struct(target_ldt_info, ptr, 1);
3881 return -TARGET_EINVAL;
3882 }
3883 lp = (uint32_t *)(gdt_table + idx);
3884 entry_1 = tswap32(lp[0]);
3885 entry_2 = tswap32(lp[1]);
3886
3887 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3888 contents = (entry_2 >> 10) & 3;
3889 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3890 seg_32bit = (entry_2 >> 22) & 1;
3891 limit_in_pages = (entry_2 >> 23) & 1;
3892 useable = (entry_2 >> 20) & 1;
3893#ifdef TARGET_ABI32
3894 lm = 0;
3895#else
3896 lm = (entry_2 >> 21) & 1;
3897#endif
3898 flags = (seg_32bit << 0) | (contents << 1) |
3899 (read_exec_only << 3) | (limit_in_pages << 4) |
3900 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3901 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3902 base_addr = (entry_1 >> 16) |
3903 (entry_2 & 0xff000000) |
3904 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003905 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00003906 target_ldt_info->limit = tswap32(limit);
3907 target_ldt_info->flags = tswap32(flags);
3908 unlock_user_struct(target_ldt_info, ptr, 1);
3909 return 0;
3910}
blueswir14583f582008-08-24 10:35:55 +00003911#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00003912
bellardd2fd1af2007-11-14 18:08:56 +00003913#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00003914static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00003915{
Juan Quintela1add8692011-06-16 17:37:09 +01003916 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00003917 abi_ulong val;
3918 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01003919
bellardd2fd1af2007-11-14 18:08:56 +00003920 switch(code) {
3921 case TARGET_ARCH_SET_GS:
3922 case TARGET_ARCH_SET_FS:
3923 if (code == TARGET_ARCH_SET_GS)
3924 idx = R_GS;
3925 else
3926 idx = R_FS;
3927 cpu_x86_load_seg(env, idx, 0);
3928 env->segs[idx].base = addr;
3929 break;
3930 case TARGET_ARCH_GET_GS:
3931 case TARGET_ARCH_GET_FS:
3932 if (code == TARGET_ARCH_GET_GS)
3933 idx = R_GS;
3934 else
3935 idx = R_FS;
3936 val = env->segs[idx].base;
3937 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01003938 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00003939 break;
3940 default:
3941 ret = -TARGET_EINVAL;
3942 break;
3943 }
Juan Quintela1add8692011-06-16 17:37:09 +01003944 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00003945}
3946#endif
3947
bellard2ab83ea2003-06-15 19:56:46 +00003948#endif /* defined(TARGET_I386) */
3949
Riku Voipio05098a92011-03-04 15:27:29 +02003950#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00003951
Riku Voipio05098a92011-03-04 15:27:29 +02003952#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003953
3954static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3955typedef struct {
3956 CPUState *env;
3957 pthread_mutex_t mutex;
3958 pthread_cond_t cond;
3959 pthread_t thread;
3960 uint32_t tid;
3961 abi_ulong child_tidptr;
3962 abi_ulong parent_tidptr;
3963 sigset_t sigmask;
3964} new_thread_info;
3965
3966static void *clone_func(void *arg)
3967{
3968 new_thread_info *info = arg;
3969 CPUState *env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003970 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00003971
3972 env = info->env;
3973 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003974 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00003975 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003976 env->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003977 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00003978 if (info->child_tidptr)
3979 put_user_u32(info->tid, info->child_tidptr);
3980 if (info->parent_tidptr)
3981 put_user_u32(info->tid, info->parent_tidptr);
3982 /* Enable signals. */
3983 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3984 /* Signal to the parent that we're ready. */
3985 pthread_mutex_lock(&info->mutex);
3986 pthread_cond_broadcast(&info->cond);
3987 pthread_mutex_unlock(&info->mutex);
3988 /* Wait until the parent has finshed initializing the tls state. */
3989 pthread_mutex_lock(&clone_lock);
3990 pthread_mutex_unlock(&clone_lock);
3991 cpu_loop(env);
3992 /* never exits */
3993 return NULL;
3994}
3995#else
bellard1b6b0292003-03-22 17:31:38 +00003996
3997static int clone_func(void *arg)
3998{
bellard2ab83ea2003-06-15 19:56:46 +00003999 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00004000 cpu_loop(env);
4001 /* never exits */
4002 return 0;
4003}
pbrookd865bab2008-06-07 22:12:17 +00004004#endif
bellard1b6b0292003-03-22 17:31:38 +00004005
ths0da46a62007-10-20 20:23:07 +00004006/* do_fork() Must return host values and target errnos (unlike most
4007 do_*() functions). */
pbrookd865bab2008-06-07 22:12:17 +00004008static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
4009 abi_ulong parent_tidptr, target_ulong newtls,
4010 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00004011{
4012 int ret;
bellard5cd43932003-03-29 16:54:36 +00004013 TaskState *ts;
bellard2ab83ea2003-06-15 19:56:46 +00004014 CPUState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004015#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004016 unsigned int nptl_flags;
4017 sigset_t sigmask;
Riku Voipio91907492010-11-26 16:21:34 +02004018#else
4019 uint8_t *new_stack;
pbrookd865bab2008-06-07 22:12:17 +00004020#endif
ths3b46e622007-09-17 08:09:54 +00004021
balrog436d1242008-09-21 02:39:45 +00004022 /* Emulate vfork() with fork() */
4023 if (flags & CLONE_VFORK)
4024 flags &= ~(CLONE_VFORK | CLONE_VM);
4025
bellard1b6b0292003-03-22 17:31:38 +00004026 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004027 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004028#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004029 new_thread_info info;
4030 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00004031#endif
Anthony Liguori7267c092011-08-20 22:09:37 -05004032 ts = g_malloc0(sizeof(TaskState));
pbrook624f7972008-05-31 16:11:38 +00004033 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00004034 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00004035 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00004036#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
4037 cpu_reset(new_env);
4038#endif
pbrook6e68e072008-05-30 17:22:15 +00004039 /* Init regs that differ from the parent. */
4040 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00004041 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004042 ts->bprm = parent_ts->bprm;
4043 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004044#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004045 nptl_flags = flags;
4046 flags &= ~CLONE_NPTL_FLAGS2;
4047
pbrookc2764712009-03-07 15:24:59 +00004048 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4049 ts->child_tidptr = child_tidptr;
4050 }
4051
pbrookd865bab2008-06-07 22:12:17 +00004052 if (nptl_flags & CLONE_SETTLS)
4053 cpu_set_tls (new_env, newtls);
4054
4055 /* Grab a mutex so that thread setup appears atomic. */
4056 pthread_mutex_lock(&clone_lock);
4057
4058 memset(&info, 0, sizeof(info));
4059 pthread_mutex_init(&info.mutex, NULL);
4060 pthread_mutex_lock(&info.mutex);
4061 pthread_cond_init(&info.cond, NULL);
4062 info.env = new_env;
4063 if (nptl_flags & CLONE_CHILD_SETTID)
4064 info.child_tidptr = child_tidptr;
4065 if (nptl_flags & CLONE_PARENT_SETTID)
4066 info.parent_tidptr = parent_tidptr;
4067
4068 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004069 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4070 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00004071 /* It is not safe to deliver signals until the child has finished
4072 initializing, so temporarily block all signals. */
4073 sigfillset(&sigmask);
4074 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4075
4076 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00004077 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00004078
4079 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4080 pthread_attr_destroy(&attr);
4081 if (ret == 0) {
4082 /* Wait for the child to initialize. */
4083 pthread_cond_wait(&info.cond, &info.mutex);
4084 ret = info.tid;
4085 if (flags & CLONE_PARENT_SETTID)
4086 put_user_u32(ret, parent_tidptr);
4087 } else {
4088 ret = -1;
4089 }
4090 pthread_mutex_unlock(&info.mutex);
4091 pthread_cond_destroy(&info.cond);
4092 pthread_mutex_destroy(&info.mutex);
4093 pthread_mutex_unlock(&clone_lock);
4094#else
4095 if (flags & CLONE_NPTL_FLAGS2)
4096 return -EINVAL;
4097 /* This is probably going to die very quickly, but do it anyway. */
Anthony Liguori7267c092011-08-20 22:09:37 -05004098 new_stack = g_malloc0 (NEW_STACK_SIZE);
bellard27725c12003-04-29 21:08:18 +00004099#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02004100 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00004101#else
4102 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
4103#endif
pbrookd865bab2008-06-07 22:12:17 +00004104#endif
bellard1b6b0292003-03-22 17:31:38 +00004105 } else {
4106 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00004107 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00004108 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00004109 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00004110 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00004111 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00004112 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00004113 cpu_clone_regs(env, newsp);
4114 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02004115#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00004116 /* There is a race condition here. The parent process could
4117 theoretically read the TID in the child process before the child
4118 tid is set. This would require using either ptrace
4119 (not implemented) or having *_tidptr to point at a shared memory
4120 mapping. We can't repeat the spinlock hack used above because
4121 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00004122 if (flags & CLONE_CHILD_SETTID)
4123 put_user_u32(gettid(), child_tidptr);
4124 if (flags & CLONE_PARENT_SETTID)
4125 put_user_u32(gettid(), parent_tidptr);
4126 ts = (TaskState *)env->opaque;
4127 if (flags & CLONE_SETTLS)
4128 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00004129 if (flags & CLONE_CHILD_CLEARTID)
4130 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00004131#endif
pbrookd865bab2008-06-07 22:12:17 +00004132 } else {
4133 fork_end(0);
4134 }
bellard1b6b0292003-03-22 17:31:38 +00004135 }
4136 return ret;
4137}
4138
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004139/* warning : doesn't handle linux specific flags... */
4140static int target_to_host_fcntl_cmd(int cmd)
4141{
4142 switch(cmd) {
4143 case TARGET_F_DUPFD:
4144 case TARGET_F_GETFD:
4145 case TARGET_F_SETFD:
4146 case TARGET_F_GETFL:
4147 case TARGET_F_SETFL:
4148 return cmd;
4149 case TARGET_F_GETLK:
4150 return F_GETLK;
4151 case TARGET_F_SETLK:
4152 return F_SETLK;
4153 case TARGET_F_SETLKW:
4154 return F_SETLKW;
4155 case TARGET_F_GETOWN:
4156 return F_GETOWN;
4157 case TARGET_F_SETOWN:
4158 return F_SETOWN;
4159 case TARGET_F_GETSIG:
4160 return F_GETSIG;
4161 case TARGET_F_SETSIG:
4162 return F_SETSIG;
4163#if TARGET_ABI_BITS == 32
4164 case TARGET_F_GETLK64:
4165 return F_GETLK64;
4166 case TARGET_F_SETLK64:
4167 return F_SETLK64;
4168 case TARGET_F_SETLKW64:
4169 return F_SETLKW64;
4170#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004171 case TARGET_F_SETLEASE:
4172 return F_SETLEASE;
4173 case TARGET_F_GETLEASE:
4174 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004175#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004176 case TARGET_F_DUPFD_CLOEXEC:
4177 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004178#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004179 case TARGET_F_NOTIFY:
4180 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004181 default:
4182 return -TARGET_EINVAL;
4183 }
4184 return -TARGET_EINVAL;
4185}
4186
blueswir1992f48a2007-10-14 16:27:31 +00004187static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004188{
4189 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004190 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004191 struct flock64 fl64;
4192 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00004193 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004194 int host_cmd = target_to_host_fcntl_cmd(cmd);
4195
4196 if (host_cmd == -TARGET_EINVAL)
4197 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004198
bellard7775e9e2003-05-14 22:46:48 +00004199 switch(cmd) {
4200 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004201 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4202 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00004203 fl.l_type = tswap16(target_fl->l_type);
4204 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004205 fl.l_start = tswapal(target_fl->l_start);
4206 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004207 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004208 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004209 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004210 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004211 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4212 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00004213 target_fl->l_type = tswap16(fl.l_type);
4214 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004215 target_fl->l_start = tswapal(fl.l_start);
4216 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004217 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004218 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004219 }
4220 break;
ths3b46e622007-09-17 08:09:54 +00004221
bellard7775e9e2003-05-14 22:46:48 +00004222 case TARGET_F_SETLK:
4223 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004224 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4225 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00004226 fl.l_type = tswap16(target_fl->l_type);
4227 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004228 fl.l_start = tswapal(target_fl->l_start);
4229 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004230 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004231 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004232 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004233 break;
ths3b46e622007-09-17 08:09:54 +00004234
bellard7775e9e2003-05-14 22:46:48 +00004235 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004236 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4237 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00004238 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4239 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004240 fl64.l_start = tswap64(target_fl64->l_start);
4241 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004242 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004243 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004244 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004245 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004246 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4247 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00004248 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
4249 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004250 target_fl64->l_start = tswap64(fl64.l_start);
4251 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004252 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004253 unlock_user_struct(target_fl64, arg, 1);
4254 }
bellard9ee1fa22007-11-11 15:11:19 +00004255 break;
bellard7775e9e2003-05-14 22:46:48 +00004256 case TARGET_F_SETLK64:
4257 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004258 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4259 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00004260 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4261 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004262 fl64.l_start = tswap64(target_fl64->l_start);
4263 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004264 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004265 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004266 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004267 break;
4268
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004269 case TARGET_F_GETFL:
4270 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004271 if (ret >= 0) {
4272 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4273 }
bellardffa65c32004-01-04 23:57:22 +00004274 break;
4275
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004276 case TARGET_F_SETFL:
4277 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4278 break;
4279
4280 case TARGET_F_SETOWN:
4281 case TARGET_F_GETOWN:
4282 case TARGET_F_SETSIG:
4283 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004284 case TARGET_F_SETLEASE:
4285 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004286 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00004287 break;
4288
bellard7775e9e2003-05-14 22:46:48 +00004289 default:
bellard9ee1fa22007-11-11 15:11:19 +00004290 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00004291 break;
4292 }
4293 return ret;
4294}
4295
bellard67867302003-11-23 17:05:30 +00004296#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00004297
bellard67867302003-11-23 17:05:30 +00004298static inline int high2lowuid(int uid)
4299{
4300 if (uid > 65535)
4301 return 65534;
4302 else
4303 return uid;
4304}
4305
4306static inline int high2lowgid(int gid)
4307{
4308 if (gid > 65535)
4309 return 65534;
4310 else
4311 return gid;
4312}
4313
4314static inline int low2highuid(int uid)
4315{
4316 if ((int16_t)uid == -1)
4317 return -1;
4318 else
4319 return uid;
4320}
4321
4322static inline int low2highgid(int gid)
4323{
4324 if ((int16_t)gid == -1)
4325 return -1;
4326 else
4327 return gid;
4328}
Riku Voipio0c866a72011-04-18 15:23:06 +03004329static inline int tswapid(int id)
4330{
4331 return tswap16(id);
4332}
4333#else /* !USE_UID16 */
4334static inline int high2lowuid(int uid)
4335{
4336 return uid;
4337}
4338static inline int high2lowgid(int gid)
4339{
4340 return gid;
4341}
4342static inline int low2highuid(int uid)
4343{
4344 return uid;
4345}
4346static inline int low2highgid(int gid)
4347{
4348 return gid;
4349}
4350static inline int tswapid(int id)
4351{
4352 return tswap32(id);
4353}
bellard67867302003-11-23 17:05:30 +00004354#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004355
bellard31e31b82003-02-18 22:55:36 +00004356void syscall_init(void)
4357{
bellard2ab83ea2003-06-15 19:56:46 +00004358 IOCTLEntry *ie;
4359 const argtype *arg_type;
4360 int size;
thsb92c47c2007-11-01 00:07:38 +00004361 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004362
Blue Swirl001faf32009-05-13 17:53:17 +00004363#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004364#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004365#include "syscall_types.h"
4366#undef STRUCT
4367#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00004368
4369 /* we patch the ioctl size if necessary. We rely on the fact that
4370 no ioctl has all the bits at '1' in the size field */
4371 ie = ioctl_entries;
4372 while (ie->target_cmd != 0) {
4373 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4374 TARGET_IOC_SIZEMASK) {
4375 arg_type = ie->arg_type;
4376 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00004377 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00004378 ie->target_cmd);
4379 exit(1);
4380 }
4381 arg_type++;
4382 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00004383 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00004384 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4385 (size << TARGET_IOC_SIZESHIFT);
4386 }
thsb92c47c2007-11-01 00:07:38 +00004387
4388 /* Build target_to_host_errno_table[] table from
4389 * host_to_target_errno_table[]. */
4390 for (i=0; i < ERRNO_TABLE_SIZE; i++)
4391 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4392
bellard2ab83ea2003-06-15 19:56:46 +00004393 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00004394#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4395 (defined(__x86_64__) && defined(TARGET_X86_64))
4396 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4397 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4398 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00004399 }
4400#endif
4401 ie++;
4402 }
bellard31e31b82003-02-18 22:55:36 +00004403}
bellardc573ff62004-01-04 15:51:36 +00004404
blueswir1992f48a2007-10-14 16:27:31 +00004405#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004406static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4407{
thsaf325d32008-06-10 15:29:15 +00004408#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004409 return ((uint64_t)word0 << 32) | word1;
4410#else
4411 return ((uint64_t)word1 << 32) | word0;
4412#endif
4413}
blueswir1992f48a2007-10-14 16:27:31 +00004414#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004415static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4416{
4417 return word0;
4418}
blueswir1992f48a2007-10-14 16:27:31 +00004419#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004420
4421#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004422static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4423 abi_long arg2,
4424 abi_long arg3,
4425 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004426{
Riku Voipio48e515d2011-07-12 15:40:51 +03004427 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004428 arg2 = arg3;
4429 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004430 }
pbrookce4defa2006-02-09 16:49:55 +00004431 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4432}
4433#endif
4434
4435#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004436static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4437 abi_long arg2,
4438 abi_long arg3,
4439 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004440{
Riku Voipio48e515d2011-07-12 15:40:51 +03004441 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004442 arg2 = arg3;
4443 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004444 }
pbrookce4defa2006-02-09 16:49:55 +00004445 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4446}
4447#endif
4448
bellard579a97f2007-11-11 14:26:47 +00004449static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4450 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004451{
4452 struct target_timespec *target_ts;
4453
bellard579a97f2007-11-11 14:26:47 +00004454 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4455 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004456 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4457 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004458 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004459 return 0;
pbrook53a59602006-03-25 19:31:22 +00004460}
4461
bellard579a97f2007-11-11 14:26:47 +00004462static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4463 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004464{
4465 struct target_timespec *target_ts;
4466
bellard579a97f2007-11-11 14:26:47 +00004467 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4468 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004469 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4470 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004471 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004472 return 0;
pbrook53a59602006-03-25 19:31:22 +00004473}
4474
aurel329d33b762009-04-08 23:07:05 +00004475#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004476static inline abi_long host_to_target_stat64(void *cpu_env,
4477 abi_ulong target_addr,
4478 struct stat *host_st)
4479{
4480#ifdef TARGET_ARM
4481 if (((CPUARMState *)cpu_env)->eabi) {
4482 struct target_eabi_stat64 *target_st;
4483
4484 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4485 return -TARGET_EFAULT;
4486 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4487 __put_user(host_st->st_dev, &target_st->st_dev);
4488 __put_user(host_st->st_ino, &target_st->st_ino);
4489#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4490 __put_user(host_st->st_ino, &target_st->__st_ino);
4491#endif
4492 __put_user(host_st->st_mode, &target_st->st_mode);
4493 __put_user(host_st->st_nlink, &target_st->st_nlink);
4494 __put_user(host_st->st_uid, &target_st->st_uid);
4495 __put_user(host_st->st_gid, &target_st->st_gid);
4496 __put_user(host_st->st_rdev, &target_st->st_rdev);
4497 __put_user(host_st->st_size, &target_st->st_size);
4498 __put_user(host_st->st_blksize, &target_st->st_blksize);
4499 __put_user(host_st->st_blocks, &target_st->st_blocks);
4500 __put_user(host_st->st_atime, &target_st->target_st_atime);
4501 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4502 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4503 unlock_user_struct(target_st, target_addr, 1);
4504 } else
4505#endif
4506 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004507#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004508 struct target_stat *target_st;
4509#else
balrog6a24a772008-09-20 02:23:36 +00004510 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004511#endif
balrog6a24a772008-09-20 02:23:36 +00004512
4513 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4514 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004515 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004516 __put_user(host_st->st_dev, &target_st->st_dev);
4517 __put_user(host_st->st_ino, &target_st->st_ino);
4518#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4519 __put_user(host_st->st_ino, &target_st->__st_ino);
4520#endif
4521 __put_user(host_st->st_mode, &target_st->st_mode);
4522 __put_user(host_st->st_nlink, &target_st->st_nlink);
4523 __put_user(host_st->st_uid, &target_st->st_uid);
4524 __put_user(host_st->st_gid, &target_st->st_gid);
4525 __put_user(host_st->st_rdev, &target_st->st_rdev);
4526 /* XXX: better use of kernel struct */
4527 __put_user(host_st->st_size, &target_st->st_size);
4528 __put_user(host_st->st_blksize, &target_st->st_blksize);
4529 __put_user(host_st->st_blocks, &target_st->st_blocks);
4530 __put_user(host_st->st_atime, &target_st->target_st_atime);
4531 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4532 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4533 unlock_user_struct(target_st, target_addr, 1);
4534 }
4535
4536 return 0;
4537}
4538#endif
4539
Juan Quintela2f7bb872009-07-27 16:13:24 +02004540#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004541/* ??? Using host futex calls even when target atomic operations
4542 are not really atomic probably breaks things. However implementing
4543 futexes locally would make futexes shared between multiple processes
4544 tricky. However they're probably useless because guest atomic
4545 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004546static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4547 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004548{
4549 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004550 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004551
4552 /* ??? We assume FUTEX_* constants are the same on both host
4553 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004554#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004555 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004556#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004557 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004558#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004559 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004560 case FUTEX_WAIT:
4561 if (timeout) {
4562 pts = &ts;
4563 target_to_host_timespec(pts, timeout);
4564 } else {
4565 pts = NULL;
4566 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004567 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004568 pts, NULL, 0));
4569 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004570 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004571 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004572 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004573 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004574 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004575 case FUTEX_WAKE_OP:
4576 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4577 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4578 But the prototype takes a `struct timespec *'; insert casts
4579 to satisfy the compiler. We do not need to tswap TIMEOUT
4580 since it's not compared to guest memory. */
4581 pts = (struct timespec *)(uintptr_t) timeout;
4582 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4583 g2h(uaddr2),
4584 (base_op == FUTEX_CMP_REQUEUE
4585 ? tswap32(val3)
4586 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004587 default:
4588 return -TARGET_ENOSYS;
4589 }
4590}
4591#endif
4592
pbrook1d9d8b52009-04-16 15:17:02 +00004593/* Map host to target signal numbers for the wait family of syscalls.
4594 Assume all other status bits are the same. */
4595static int host_to_target_waitstatus(int status)
4596{
4597 if (WIFSIGNALED(status)) {
4598 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4599 }
4600 if (WIFSTOPPED(status)) {
4601 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4602 | (status & 0xff);
4603 }
4604 return status;
4605}
4606
pbrooka745ec62008-05-06 15:36:17 +00004607int get_osversion(void)
4608{
4609 static int osversion;
4610 struct new_utsname buf;
4611 const char *s;
4612 int i, n, tmp;
4613 if (osversion)
4614 return osversion;
4615 if (qemu_uname_release && *qemu_uname_release) {
4616 s = qemu_uname_release;
4617 } else {
4618 if (sys_uname(&buf))
4619 return 0;
4620 s = buf.release;
4621 }
4622 tmp = 0;
4623 for (i = 0; i < 3; i++) {
4624 n = 0;
4625 while (*s >= '0' && *s <= '9') {
4626 n *= 10;
4627 n += *s - '0';
4628 s++;
4629 }
4630 tmp = (tmp << 8) + n;
4631 if (*s == '.')
4632 s++;
4633 }
4634 osversion = tmp;
4635 return osversion;
4636}
4637
Alexander Graf36c08d42011-11-02 20:23:24 +01004638
4639static int open_self_maps(void *cpu_env, int fd)
4640{
4641 TaskState *ts = ((CPUState *)cpu_env)->opaque;
4642
4643 dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
4644 (unsigned long long)ts->info->stack_limit,
4645 (unsigned long long)(ts->stack_base + (TARGET_PAGE_SIZE - 1))
4646 & TARGET_PAGE_MASK,
4647 (unsigned long long)ts->stack_base);
4648
4649 return 0;
4650}
4651
Alexander Graf480b8e72011-11-02 20:23:25 +01004652static int open_self_stat(void *cpu_env, int fd)
4653{
4654 TaskState *ts = ((CPUState *)cpu_env)->opaque;
4655 abi_ulong start_stack = ts->info->start_stack;
4656 int i;
4657
4658 for (i = 0; i < 44; i++) {
4659 char buf[128];
4660 int len;
4661 uint64_t val = 0;
4662
4663 if (i == 27) {
4664 /* stack bottom */
4665 val = start_stack;
4666 }
4667 snprintf(buf, sizeof(buf), "%"PRId64 "%c", val, i == 43 ? '\n' : ' ');
4668 len = strlen(buf);
4669 if (write(fd, buf, len) != len) {
4670 return -1;
4671 }
4672 }
4673
4674 return 0;
4675}
4676
Alexander Graf257450e2011-11-02 20:23:26 +01004677static int open_self_auxv(void *cpu_env, int fd)
4678{
4679 TaskState *ts = ((CPUState *)cpu_env)->opaque;
4680 abi_ulong auxv = ts->info->saved_auxv;
4681 abi_ulong len = ts->info->auxv_len;
4682 char *ptr;
4683
4684 /*
4685 * Auxiliary vector is stored in target process stack.
4686 * read in whole auxv vector and copy it to file
4687 */
4688 ptr = lock_user(VERIFY_READ, auxv, len, 0);
4689 if (ptr != NULL) {
4690 while (len > 0) {
4691 ssize_t r;
4692 r = write(fd, ptr, len);
4693 if (r <= 0) {
4694 break;
4695 }
4696 len -= r;
4697 ptr += r;
4698 }
4699 lseek(fd, 0, SEEK_SET);
4700 unlock_user(ptr, auxv, len);
4701 }
4702
4703 return 0;
4704}
4705
Alexander Graf3be14d02011-11-02 20:23:23 +01004706static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
4707{
4708 struct fake_open {
4709 const char *filename;
4710 int (*fill)(void *cpu_env, int fd);
4711 };
4712 const struct fake_open *fake_open;
4713 static const struct fake_open fakes[] = {
Alexander Graf36c08d42011-11-02 20:23:24 +01004714 { "/proc/self/maps", open_self_maps },
Alexander Graf480b8e72011-11-02 20:23:25 +01004715 { "/proc/self/stat", open_self_stat },
Alexander Graf257450e2011-11-02 20:23:26 +01004716 { "/proc/self/auxv", open_self_auxv },
Alexander Graf3be14d02011-11-02 20:23:23 +01004717 { NULL, NULL }
4718 };
4719
4720 for (fake_open = fakes; fake_open->filename; fake_open++) {
4721 if (!strncmp(pathname, fake_open->filename,
4722 strlen(fake_open->filename))) {
4723 break;
4724 }
4725 }
4726
4727 if (fake_open->filename) {
4728 const char *tmpdir;
4729 char filename[PATH_MAX];
4730 int fd, r;
4731
4732 /* create temporary file to map stat to */
4733 tmpdir = getenv("TMPDIR");
4734 if (!tmpdir)
4735 tmpdir = "/tmp";
4736 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
4737 fd = mkstemp(filename);
4738 if (fd < 0) {
4739 return fd;
4740 }
4741 unlink(filename);
4742
4743 if ((r = fake_open->fill(cpu_env, fd))) {
4744 close(fd);
4745 return r;
4746 }
4747 lseek(fd, 0, SEEK_SET);
4748
4749 return fd;
4750 }
4751
4752 return get_errno(open(path(pathname), flags, mode));
4753}
4754
ths0da46a62007-10-20 20:23:07 +00004755/* do_syscall() should always have a single exit point at the end so
4756 that actions, such as logging of syscall results, can be performed.
4757 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00004758abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4759 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01004760 abi_long arg5, abi_long arg6, abi_long arg7,
4761 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00004762{
blueswir1992f48a2007-10-14 16:27:31 +00004763 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004764 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00004765 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00004766 void *p;
ths3b46e622007-09-17 08:09:54 +00004767
bellard72f03902003-02-18 23:33:18 +00004768#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00004769 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00004770#endif
thsb92c47c2007-11-01 00:07:38 +00004771 if(do_strace)
4772 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4773
bellard31e31b82003-02-18 22:55:36 +00004774 switch(num) {
4775 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02004776#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00004777 /* In old applications this may be used to implement _exit(2).
4778 However in threaded applictions it is used for thread termination,
4779 and _exit_group is used for application termination.
4780 Do thread termination if we have more then one thread. */
4781 /* FIXME: This probably breaks if a signal arrives. We should probably
4782 be disabling signals. */
4783 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004784 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00004785 CPUState **lastp;
4786 CPUState *p;
4787
4788 cpu_list_lock();
4789 lastp = &first_cpu;
4790 p = first_cpu;
4791 while (p && p != (CPUState *)cpu_env) {
4792 lastp = &p->next_cpu;
4793 p = p->next_cpu;
4794 }
4795 /* If we didn't find the CPU for this thread then something is
4796 horribly wrong. */
4797 if (!p)
4798 abort();
4799 /* Remove the CPU from the list. */
4800 *lastp = p->next_cpu;
4801 cpu_list_unlock();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004802 ts = ((CPUState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00004803 if (ts->child_tidptr) {
4804 put_user_u32(0, ts->child_tidptr);
4805 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4806 NULL, NULL, 0);
4807 }
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004808 thread_env = NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -05004809 g_free(cpu_env);
4810 g_free(ts);
pbrookc2764712009-03-07 15:24:59 +00004811 pthread_exit(NULL);
4812 }
4813#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02004814#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00004815 _mcleanup();
4816#endif
bellarde9009672005-04-26 20:42:36 +00004817 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00004818 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00004819 ret = 0; /* avoid warning */
4820 break;
4821 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00004822 if (arg3 == 0)
4823 ret = 0;
4824 else {
4825 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4826 goto efault;
4827 ret = get_errno(read(arg1, p, arg3));
4828 unlock_user(p, arg2, ret);
4829 }
bellard31e31b82003-02-18 22:55:36 +00004830 break;
4831 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00004832 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4833 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004834 ret = get_errno(write(arg1, p, arg3));
4835 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00004836 break;
4837 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00004838 if (!(p = lock_user_string(arg1)))
4839 goto efault;
Alexander Graf3be14d02011-11-02 20:23:23 +01004840 ret = get_errno(do_open(cpu_env, p,
4841 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4842 arg3));
pbrook53a59602006-03-25 19:31:22 +00004843 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004844 break;
ths82424832007-09-24 09:21:55 +00004845#if defined(TARGET_NR_openat) && defined(__NR_openat)
4846 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00004847 if (!(p = lock_user_string(arg2)))
4848 goto efault;
4849 ret = get_errno(sys_openat(arg1,
4850 path(p),
4851 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4852 arg4));
4853 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00004854 break;
4855#endif
bellard31e31b82003-02-18 22:55:36 +00004856 case TARGET_NR_close:
4857 ret = get_errno(close(arg1));
4858 break;
4859 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00004860 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00004861 break;
4862 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00004863 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00004864 break;
thse5febef2007-04-01 18:31:35 +00004865#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00004866 case TARGET_NR_waitpid:
4867 {
pbrook53a59602006-03-25 19:31:22 +00004868 int status;
4869 ret = get_errno(waitpid(arg1, &status, arg3));
Alexander Graf53795572011-11-24 00:44:43 +01004870 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00004871 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00004872 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004873 }
4874 break;
thse5febef2007-04-01 18:31:35 +00004875#endif
pbrookf0cbb612008-05-30 18:20:05 +00004876#ifdef TARGET_NR_waitid
4877 case TARGET_NR_waitid:
4878 {
4879 siginfo_t info;
4880 info.si_pid = 0;
4881 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4882 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004883 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00004884 goto efault;
4885 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05004886 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00004887 }
4888 }
4889 break;
4890#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004891#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004892 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00004893 if (!(p = lock_user_string(arg1)))
4894 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004895 ret = get_errno(creat(p, arg2));
4896 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004897 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004898#endif
bellard31e31b82003-02-18 22:55:36 +00004899 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00004900 {
4901 void * p2;
4902 p = lock_user_string(arg1);
4903 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004904 if (!p || !p2)
4905 ret = -TARGET_EFAULT;
4906 else
4907 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004908 unlock_user(p2, arg2, 0);
4909 unlock_user(p, arg1, 0);
4910 }
bellard31e31b82003-02-18 22:55:36 +00004911 break;
ths64f0ce42007-09-24 09:25:06 +00004912#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4913 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00004914 {
4915 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00004916 if (!arg2 || !arg4)
4917 goto efault;
ths64f0ce42007-09-24 09:25:06 +00004918 p = lock_user_string(arg2);
4919 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004920 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004921 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00004922 else
4923 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00004924 unlock_user(p, arg2, 0);
4925 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00004926 }
4927 break;
4928#endif
bellard31e31b82003-02-18 22:55:36 +00004929 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00004930 if (!(p = lock_user_string(arg1)))
4931 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004932 ret = get_errno(unlink(p));
4933 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004934 break;
ths8170f562007-09-24 09:24:11 +00004935#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4936 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00004937 if (!(p = lock_user_string(arg2)))
4938 goto efault;
4939 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4940 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00004941 break;
balrogb7d35e62007-12-12 00:40:24 +00004942#endif
bellard31e31b82003-02-18 22:55:36 +00004943 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00004944 {
4945 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00004946 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00004947 abi_ulong gp;
4948 abi_ulong guest_argp;
4949 abi_ulong guest_envp;
4950 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00004951 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01004952 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00004953
bellardf7341ff2003-03-30 21:00:25 +00004954 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00004955 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00004956 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004957 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004958 goto efault;
ths03aa1972007-12-02 06:28:08 +00004959 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004960 break;
bellard7854b052003-03-29 17:22:23 +00004961 argc++;
bellard2f619692007-11-16 10:46:05 +00004962 }
bellardf7341ff2003-03-30 21:00:25 +00004963 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00004964 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00004965 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004966 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004967 goto efault;
ths03aa1972007-12-02 06:28:08 +00004968 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004969 break;
bellard7854b052003-03-29 17:22:23 +00004970 envc++;
bellard2f619692007-11-16 10:46:05 +00004971 }
bellard7854b052003-03-29 17:22:23 +00004972
bellardf7341ff2003-03-30 21:00:25 +00004973 argp = alloca((argc + 1) * sizeof(void *));
4974 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00004975
pbrookda94d262008-05-30 18:24:00 +00004976 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004977 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004978 if (get_user_ual(addr, gp))
4979 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004980 if (!addr)
4981 break;
bellard2f619692007-11-16 10:46:05 +00004982 if (!(*q = lock_user_string(addr)))
4983 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01004984 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00004985 }
bellardf7341ff2003-03-30 21:00:25 +00004986 *q = NULL;
4987
pbrookda94d262008-05-30 18:24:00 +00004988 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004989 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004990 if (get_user_ual(addr, gp))
4991 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004992 if (!addr)
4993 break;
bellard2f619692007-11-16 10:46:05 +00004994 if (!(*q = lock_user_string(addr)))
4995 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01004996 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00004997 }
bellardf7341ff2003-03-30 21:00:25 +00004998 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00004999
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005000 /* This case will not be caught by the host's execve() if its
5001 page size is bigger than the target's. */
5002 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5003 ret = -TARGET_E2BIG;
5004 goto execve_end;
5005 }
bellard2f619692007-11-16 10:46:05 +00005006 if (!(p = lock_user_string(arg1)))
5007 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005008 ret = get_errno(execve(p, argp, envp));
5009 unlock_user(p, arg1, 0);
5010
bellard2f619692007-11-16 10:46:05 +00005011 goto execve_end;
5012
5013 execve_efault:
5014 ret = -TARGET_EFAULT;
5015
5016 execve_end:
pbrook53a59602006-03-25 19:31:22 +00005017 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005018 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005019 if (get_user_ual(addr, gp)
5020 || !addr)
5021 break;
pbrook53a59602006-03-25 19:31:22 +00005022 unlock_user(*q, addr, 0);
5023 }
5024 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005025 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005026 if (get_user_ual(addr, gp)
5027 || !addr)
5028 break;
pbrook53a59602006-03-25 19:31:22 +00005029 unlock_user(*q, addr, 0);
5030 }
bellard7854b052003-03-29 17:22:23 +00005031 }
bellard31e31b82003-02-18 22:55:36 +00005032 break;
5033 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00005034 if (!(p = lock_user_string(arg1)))
5035 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005036 ret = get_errno(chdir(p));
5037 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005038 break;
bellarda315a142005-01-30 22:59:18 +00005039#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00005040 case TARGET_NR_time:
5041 {
pbrook53a59602006-03-25 19:31:22 +00005042 time_t host_time;
5043 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00005044 if (!is_error(ret)
5045 && arg1
5046 && put_user_sal(host_time, arg1))
5047 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005048 }
5049 break;
bellarda315a142005-01-30 22:59:18 +00005050#endif
bellard31e31b82003-02-18 22:55:36 +00005051 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00005052 if (!(p = lock_user_string(arg1)))
5053 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005054 ret = get_errno(mknod(p, arg2, arg3));
5055 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005056 break;
ths75ac37a2007-09-24 09:23:05 +00005057#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
5058 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00005059 if (!(p = lock_user_string(arg2)))
5060 goto efault;
5061 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
5062 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00005063 break;
5064#endif
bellard31e31b82003-02-18 22:55:36 +00005065 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00005066 if (!(p = lock_user_string(arg1)))
5067 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005068 ret = get_errno(chmod(p, arg2));
5069 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005070 break;
bellardebc05482003-09-30 21:08:41 +00005071#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00005072 case TARGET_NR_break:
5073 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005074#endif
5075#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00005076 case TARGET_NR_oldstat:
5077 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005078#endif
bellard31e31b82003-02-18 22:55:36 +00005079 case TARGET_NR_lseek:
5080 ret = get_errno(lseek(arg1, arg2, arg3));
5081 break;
Richard Henderson92317332010-05-03 10:07:53 -07005082#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5083 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00005084 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07005085 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00005086 ret = get_errno(getpid());
5087 break;
Richard Henderson92317332010-05-03 10:07:53 -07005088#endif
5089#ifdef TARGET_NR_getpid
5090 case TARGET_NR_getpid:
5091 ret = get_errno(getpid());
5092 break;
5093#endif
bellard31e31b82003-02-18 22:55:36 +00005094 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00005095 {
5096 /* need to look at the data field */
5097 void *p2, *p3;
5098 p = lock_user_string(arg1);
5099 p2 = lock_user_string(arg2);
5100 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005101 if (!p || !p2 || !p3)
5102 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005103 else {
bellard579a97f2007-11-11 14:26:47 +00005104 /* FIXME - arg5 should be locked, but it isn't clear how to
5105 * do that since it's not guaranteed to be a NULL-terminated
5106 * string.
5107 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005108 if ( ! arg5 )
5109 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
5110 else
5111 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
5112 }
bellard579a97f2007-11-11 14:26:47 +00005113 unlock_user(p, arg1, 0);
5114 unlock_user(p2, arg2, 0);
5115 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00005116 break;
5117 }
thse5febef2007-04-01 18:31:35 +00005118#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00005119 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00005120 if (!(p = lock_user_string(arg1)))
5121 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005122 ret = get_errno(umount(p));
5123 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005124 break;
thse5febef2007-04-01 18:31:35 +00005125#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005126#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005127 case TARGET_NR_stime:
5128 {
pbrook53a59602006-03-25 19:31:22 +00005129 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00005130 if (get_user_sal(host_time, arg1))
5131 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005132 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00005133 }
5134 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005135#endif
bellard31e31b82003-02-18 22:55:36 +00005136 case TARGET_NR_ptrace:
5137 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00005138#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005139 case TARGET_NR_alarm:
5140 ret = alarm(arg1);
5141 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005142#endif
bellardebc05482003-09-30 21:08:41 +00005143#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00005144 case TARGET_NR_oldfstat:
5145 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005146#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005147#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005148 case TARGET_NR_pause:
5149 ret = get_errno(pause());
5150 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005151#endif
thse5febef2007-04-01 18:31:35 +00005152#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00005153 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00005154 {
pbrook53a59602006-03-25 19:31:22 +00005155 struct utimbuf tbuf, *host_tbuf;
5156 struct target_utimbuf *target_tbuf;
5157 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005158 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5159 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005160 tbuf.actime = tswapal(target_tbuf->actime);
5161 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00005162 unlock_user_struct(target_tbuf, arg2, 0);
5163 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00005164 } else {
pbrook53a59602006-03-25 19:31:22 +00005165 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00005166 }
bellard579a97f2007-11-11 14:26:47 +00005167 if (!(p = lock_user_string(arg1)))
5168 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005169 ret = get_errno(utime(p, host_tbuf));
5170 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00005171 }
5172 break;
thse5febef2007-04-01 18:31:35 +00005173#endif
bellard978a66f2004-12-06 22:58:05 +00005174 case TARGET_NR_utimes:
5175 {
bellard978a66f2004-12-06 22:58:05 +00005176 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00005177 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00005178 if (copy_from_user_timeval(&tv[0], arg2)
5179 || copy_from_user_timeval(&tv[1],
5180 arg2 + sizeof(struct target_timeval)))
5181 goto efault;
bellard978a66f2004-12-06 22:58:05 +00005182 tvp = tv;
5183 } else {
5184 tvp = NULL;
5185 }
bellard579a97f2007-11-11 14:26:47 +00005186 if (!(p = lock_user_string(arg1)))
5187 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005188 ret = get_errno(utimes(p, tvp));
5189 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00005190 }
5191 break;
balrogac8a6552008-09-20 02:25:39 +00005192#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
5193 case TARGET_NR_futimesat:
5194 {
5195 struct timeval *tvp, tv[2];
5196 if (arg3) {
5197 if (copy_from_user_timeval(&tv[0], arg3)
5198 || copy_from_user_timeval(&tv[1],
5199 arg3 + sizeof(struct target_timeval)))
5200 goto efault;
5201 tvp = tv;
5202 } else {
5203 tvp = NULL;
5204 }
5205 if (!(p = lock_user_string(arg2)))
5206 goto efault;
5207 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
5208 unlock_user(p, arg2, 0);
5209 }
5210 break;
5211#endif
bellardebc05482003-09-30 21:08:41 +00005212#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00005213 case TARGET_NR_stty:
5214 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005215#endif
5216#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00005217 case TARGET_NR_gtty:
5218 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005219#endif
bellard31e31b82003-02-18 22:55:36 +00005220 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00005221 if (!(p = lock_user_string(arg1)))
5222 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02005223 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00005224 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005225 break;
ths92a34c12007-09-24 09:27:49 +00005226#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5227 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00005228 if (!(p = lock_user_string(arg2)))
5229 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005230 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005231 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00005232 break;
5233#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005234#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005235 case TARGET_NR_nice:
5236 ret = get_errno(nice(arg1));
5237 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005238#endif
bellardebc05482003-09-30 21:08:41 +00005239#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00005240 case TARGET_NR_ftime:
5241 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005242#endif
bellard31e31b82003-02-18 22:55:36 +00005243 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00005244 sync();
5245 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00005246 break;
5247 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00005248 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00005249 break;
5250 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00005251 {
5252 void *p2;
5253 p = lock_user_string(arg1);
5254 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005255 if (!p || !p2)
5256 ret = -TARGET_EFAULT;
5257 else
5258 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005259 unlock_user(p2, arg2, 0);
5260 unlock_user(p, arg1, 0);
5261 }
bellard31e31b82003-02-18 22:55:36 +00005262 break;
ths722183f2007-09-24 09:24:37 +00005263#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
5264 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00005265 {
bellard579a97f2007-11-11 14:26:47 +00005266 void *p2;
ths722183f2007-09-24 09:24:37 +00005267 p = lock_user_string(arg2);
5268 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005269 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005270 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00005271 else
5272 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00005273 unlock_user(p2, arg4, 0);
5274 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00005275 }
5276 break;
5277#endif
bellard31e31b82003-02-18 22:55:36 +00005278 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00005279 if (!(p = lock_user_string(arg1)))
5280 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005281 ret = get_errno(mkdir(p, arg2));
5282 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005283 break;
ths4472ad02007-09-24 09:22:32 +00005284#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
5285 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00005286 if (!(p = lock_user_string(arg2)))
5287 goto efault;
5288 ret = get_errno(sys_mkdirat(arg1, p, arg3));
5289 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00005290 break;
5291#endif
bellard31e31b82003-02-18 22:55:36 +00005292 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00005293 if (!(p = lock_user_string(arg1)))
5294 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005295 ret = get_errno(rmdir(p));
5296 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005297 break;
5298 case TARGET_NR_dup:
5299 ret = get_errno(dup(arg1));
5300 break;
5301 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005302 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00005303 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03005304#ifdef TARGET_NR_pipe2
5305 case TARGET_NR_pipe2:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005306 ret = do_pipe(cpu_env, arg1, arg2, 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03005307 break;
5308#endif
bellard31e31b82003-02-18 22:55:36 +00005309 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00005310 {
pbrook53a59602006-03-25 19:31:22 +00005311 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00005312 struct tms tms;
5313 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00005314 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00005315 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5316 if (!tmsp)
5317 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005318 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5319 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5320 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5321 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00005322 }
bellardc596ed12003-07-13 17:32:31 +00005323 if (!is_error(ret))
5324 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00005325 }
5326 break;
bellardebc05482003-09-30 21:08:41 +00005327#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00005328 case TARGET_NR_prof:
5329 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005330#endif
thse5febef2007-04-01 18:31:35 +00005331#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00005332 case TARGET_NR_signal:
5333 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005334#endif
bellard31e31b82003-02-18 22:55:36 +00005335 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00005336 if (arg1 == 0) {
5337 ret = get_errno(acct(NULL));
5338 } else {
5339 if (!(p = lock_user_string(arg1)))
5340 goto efault;
5341 ret = get_errno(acct(path(p)));
5342 unlock_user(p, arg1, 0);
5343 }
pbrook24836682006-04-16 14:14:53 +00005344 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005345#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005346 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00005347 if (!(p = lock_user_string(arg1)))
5348 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005349 ret = get_errno(umount2(p, arg2));
5350 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005351 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005352#endif
bellardebc05482003-09-30 21:08:41 +00005353#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00005354 case TARGET_NR_lock:
5355 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005356#endif
bellard31e31b82003-02-18 22:55:36 +00005357 case TARGET_NR_ioctl:
5358 ret = do_ioctl(arg1, arg2, arg3);
5359 break;
5360 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00005361 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00005362 break;
bellardebc05482003-09-30 21:08:41 +00005363#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00005364 case TARGET_NR_mpx:
5365 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005366#endif
bellard31e31b82003-02-18 22:55:36 +00005367 case TARGET_NR_setpgid:
5368 ret = get_errno(setpgid(arg1, arg2));
5369 break;
bellardebc05482003-09-30 21:08:41 +00005370#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00005371 case TARGET_NR_ulimit:
5372 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005373#endif
5374#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00005375 case TARGET_NR_oldolduname:
5376 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005377#endif
bellard31e31b82003-02-18 22:55:36 +00005378 case TARGET_NR_umask:
5379 ret = get_errno(umask(arg1));
5380 break;
5381 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00005382 if (!(p = lock_user_string(arg1)))
5383 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005384 ret = get_errno(chroot(p));
5385 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005386 break;
5387 case TARGET_NR_ustat:
5388 goto unimplemented;
5389 case TARGET_NR_dup2:
5390 ret = get_errno(dup2(arg1, arg2));
5391 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03005392#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5393 case TARGET_NR_dup3:
5394 ret = get_errno(dup3(arg1, arg2, arg3));
5395 break;
5396#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005397#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005398 case TARGET_NR_getppid:
5399 ret = get_errno(getppid());
5400 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005401#endif
bellard31e31b82003-02-18 22:55:36 +00005402 case TARGET_NR_getpgrp:
5403 ret = get_errno(getpgrp());
5404 break;
5405 case TARGET_NR_setsid:
5406 ret = get_errno(setsid());
5407 break;
thse5febef2007-04-01 18:31:35 +00005408#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00005409 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00005410 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005411#if defined(TARGET_ALPHA)
5412 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00005413 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00005414 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005415 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5416 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005417 act._sa_handler = old_act->_sa_handler;
5418 target_siginitset(&act.sa_mask, old_act->sa_mask);
5419 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005420 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00005421 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005422 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00005423 }
5424 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00005425 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005426 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5427 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005428 old_act->_sa_handler = oact._sa_handler;
5429 old_act->sa_mask = oact.sa_mask.sig[0];
5430 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00005431 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00005432 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005433#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00005434 struct target_sigaction act, oact, *pact, *old_act;
5435
5436 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005437 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5438 goto efault;
bellard106ec872006-06-27 21:08:10 +00005439 act._sa_handler = old_act->_sa_handler;
5440 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5441 act.sa_flags = old_act->sa_flags;
5442 unlock_user_struct(old_act, arg2, 0);
5443 pact = &act;
5444 } else {
5445 pact = NULL;
5446 }
5447
5448 ret = get_errno(do_sigaction(arg1, pact, &oact));
5449
5450 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005451 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5452 goto efault;
bellard106ec872006-06-27 21:08:10 +00005453 old_act->_sa_handler = oact._sa_handler;
5454 old_act->sa_flags = oact.sa_flags;
5455 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5456 old_act->sa_mask.sig[1] = 0;
5457 old_act->sa_mask.sig[2] = 0;
5458 old_act->sa_mask.sig[3] = 0;
5459 unlock_user_struct(old_act, arg3, 1);
5460 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005461#else
5462 struct target_old_sigaction *old_act;
5463 struct target_sigaction act, oact, *pact;
5464 if (arg2) {
5465 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5466 goto efault;
5467 act._sa_handler = old_act->_sa_handler;
5468 target_siginitset(&act.sa_mask, old_act->sa_mask);
5469 act.sa_flags = old_act->sa_flags;
5470 act.sa_restorer = old_act->sa_restorer;
5471 unlock_user_struct(old_act, arg2, 0);
5472 pact = &act;
5473 } else {
5474 pact = NULL;
5475 }
5476 ret = get_errno(do_sigaction(arg1, pact, &oact));
5477 if (!is_error(ret) && arg3) {
5478 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5479 goto efault;
5480 old_act->_sa_handler = oact._sa_handler;
5481 old_act->sa_mask = oact.sa_mask.sig[0];
5482 old_act->sa_flags = oact.sa_flags;
5483 old_act->sa_restorer = oact.sa_restorer;
5484 unlock_user_struct(old_act, arg3, 1);
5485 }
ths388bb212007-05-13 13:58:00 +00005486#endif
bellard31e31b82003-02-18 22:55:36 +00005487 }
5488 break;
thse5febef2007-04-01 18:31:35 +00005489#endif
bellard66fb9762003-03-23 01:06:05 +00005490 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00005491 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005492#if defined(TARGET_ALPHA)
5493 struct target_sigaction act, oact, *pact = 0;
5494 struct target_rt_sigaction *rt_act;
5495 /* ??? arg4 == sizeof(sigset_t). */
5496 if (arg2) {
5497 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5498 goto efault;
5499 act._sa_handler = rt_act->_sa_handler;
5500 act.sa_mask = rt_act->sa_mask;
5501 act.sa_flags = rt_act->sa_flags;
5502 act.sa_restorer = arg5;
5503 unlock_user_struct(rt_act, arg2, 0);
5504 pact = &act;
5505 }
5506 ret = get_errno(do_sigaction(arg1, pact, &oact));
5507 if (!is_error(ret) && arg3) {
5508 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5509 goto efault;
5510 rt_act->_sa_handler = oact._sa_handler;
5511 rt_act->sa_mask = oact.sa_mask;
5512 rt_act->sa_flags = oact.sa_flags;
5513 unlock_user_struct(rt_act, arg3, 1);
5514 }
5515#else
pbrook53a59602006-03-25 19:31:22 +00005516 struct target_sigaction *act;
5517 struct target_sigaction *oact;
5518
bellard579a97f2007-11-11 14:26:47 +00005519 if (arg2) {
5520 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5521 goto efault;
5522 } else
pbrook53a59602006-03-25 19:31:22 +00005523 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005524 if (arg3) {
5525 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5526 ret = -TARGET_EFAULT;
5527 goto rt_sigaction_fail;
5528 }
5529 } else
pbrook53a59602006-03-25 19:31:22 +00005530 oact = NULL;
5531 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005532 rt_sigaction_fail:
5533 if (act)
pbrook53a59602006-03-25 19:31:22 +00005534 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005535 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005536 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005537#endif
pbrook53a59602006-03-25 19:31:22 +00005538 }
bellard66fb9762003-03-23 01:06:05 +00005539 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005540#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005541 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005542 {
5543 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005544 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005545 sigprocmask(0, NULL, &cur_set);
5546 host_to_target_old_sigset(&target_set, &cur_set);
5547 ret = target_set;
5548 }
5549 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005550#endif
5551#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005552 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005553 {
5554 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005555 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00005556 sigprocmask(0, NULL, &cur_set);
5557 target_to_host_old_sigset(&set, &target_set);
5558 sigorset(&set, &set, &cur_set);
5559 sigprocmask(SIG_SETMASK, &set, &oset);
5560 host_to_target_old_sigset(&target_set, &oset);
5561 ret = target_set;
5562 }
5563 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005564#endif
thse5febef2007-04-01 18:31:35 +00005565#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00005566 case TARGET_NR_sigprocmask:
5567 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005568#if defined(TARGET_ALPHA)
5569 sigset_t set, oldset;
5570 abi_ulong mask;
5571 int how;
5572
5573 switch (arg1) {
5574 case TARGET_SIG_BLOCK:
5575 how = SIG_BLOCK;
5576 break;
5577 case TARGET_SIG_UNBLOCK:
5578 how = SIG_UNBLOCK;
5579 break;
5580 case TARGET_SIG_SETMASK:
5581 how = SIG_SETMASK;
5582 break;
5583 default:
5584 ret = -TARGET_EINVAL;
5585 goto fail;
5586 }
5587 mask = arg2;
5588 target_to_host_old_sigset(&set, &mask);
5589
5590 ret = get_errno(sigprocmask(how, &set, &oldset));
5591
5592 if (!is_error(ret)) {
5593 host_to_target_old_sigset(&mask, &oldset);
5594 ret = mask;
5595 ((CPUAlphaState *)cpu_env)->[IR_V0] = 0; /* force no error */
5596 }
5597#else
bellard66fb9762003-03-23 01:06:05 +00005598 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07005599 int how;
ths3b46e622007-09-17 08:09:54 +00005600
pbrook53a59602006-03-25 19:31:22 +00005601 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005602 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00005603 case TARGET_SIG_BLOCK:
5604 how = SIG_BLOCK;
5605 break;
5606 case TARGET_SIG_UNBLOCK:
5607 how = SIG_UNBLOCK;
5608 break;
5609 case TARGET_SIG_SETMASK:
5610 how = SIG_SETMASK;
5611 break;
5612 default:
ths0da46a62007-10-20 20:23:07 +00005613 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005614 goto fail;
5615 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005616 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005617 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005618 target_to_host_old_sigset(&set, p);
5619 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005620 set_ptr = &set;
5621 } else {
5622 how = 0;
5623 set_ptr = NULL;
5624 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005625 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005626 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005627 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005628 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005629 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005630 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005631 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005632#endif
bellard66fb9762003-03-23 01:06:05 +00005633 }
5634 break;
thse5febef2007-04-01 18:31:35 +00005635#endif
bellard66fb9762003-03-23 01:06:05 +00005636 case TARGET_NR_rt_sigprocmask:
5637 {
5638 int how = arg1;
5639 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00005640
pbrook53a59602006-03-25 19:31:22 +00005641 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005642 switch(how) {
5643 case TARGET_SIG_BLOCK:
5644 how = SIG_BLOCK;
5645 break;
5646 case TARGET_SIG_UNBLOCK:
5647 how = SIG_UNBLOCK;
5648 break;
5649 case TARGET_SIG_SETMASK:
5650 how = SIG_SETMASK;
5651 break;
5652 default:
ths0da46a62007-10-20 20:23:07 +00005653 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005654 goto fail;
5655 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005656 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005657 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005658 target_to_host_sigset(&set, p);
5659 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005660 set_ptr = &set;
5661 } else {
5662 how = 0;
5663 set_ptr = NULL;
5664 }
5665 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005666 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005667 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005668 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005669 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005670 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005671 }
5672 }
5673 break;
thse5febef2007-04-01 18:31:35 +00005674#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005675 case TARGET_NR_sigpending:
5676 {
5677 sigset_t set;
5678 ret = get_errno(sigpending(&set));
5679 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005680 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005681 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005682 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005683 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005684 }
5685 }
5686 break;
thse5febef2007-04-01 18:31:35 +00005687#endif
bellard66fb9762003-03-23 01:06:05 +00005688 case TARGET_NR_rt_sigpending:
5689 {
5690 sigset_t set;
5691 ret = get_errno(sigpending(&set));
5692 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005693 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005694 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005695 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005696 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005697 }
5698 }
5699 break;
thse5febef2007-04-01 18:31:35 +00005700#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00005701 case TARGET_NR_sigsuspend:
5702 {
5703 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07005704#if defined(TARGET_ALPHA)
5705 abi_ulong mask = arg1;
5706 target_to_host_old_sigset(&set, &mask);
5707#else
Anthony Liguoric227f092009-10-01 16:12:16 -05005708 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005709 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005710 target_to_host_old_sigset(&set, p);
5711 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07005712#endif
bellard66fb9762003-03-23 01:06:05 +00005713 ret = get_errno(sigsuspend(&set));
5714 }
5715 break;
thse5febef2007-04-01 18:31:35 +00005716#endif
bellard66fb9762003-03-23 01:06:05 +00005717 case TARGET_NR_rt_sigsuspend:
5718 {
5719 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005720 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005721 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005722 target_to_host_sigset(&set, p);
5723 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005724 ret = get_errno(sigsuspend(&set));
5725 }
5726 break;
5727 case TARGET_NR_rt_sigtimedwait:
5728 {
bellard66fb9762003-03-23 01:06:05 +00005729 sigset_t set;
5730 struct timespec uts, *puts;
5731 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00005732
Anthony Liguoric227f092009-10-01 16:12:16 -05005733 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005734 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005735 target_to_host_sigset(&set, p);
5736 unlock_user(p, arg1, 0);
5737 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00005738 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00005739 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00005740 } else {
5741 puts = NULL;
5742 }
5743 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00005744 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005745 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005746 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005747 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05005748 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00005749 }
5750 }
5751 break;
5752 case TARGET_NR_rt_sigqueueinfo:
5753 {
5754 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05005755 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005756 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005757 target_to_host_siginfo(&uinfo, p);
5758 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005759 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5760 }
5761 break;
thse5febef2007-04-01 18:31:35 +00005762#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00005763 case TARGET_NR_sigreturn:
5764 /* NOTE: ret is eax, so not transcoding must be done */
5765 ret = do_sigreturn(cpu_env);
5766 break;
thse5febef2007-04-01 18:31:35 +00005767#endif
bellard66fb9762003-03-23 01:06:05 +00005768 case TARGET_NR_rt_sigreturn:
5769 /* NOTE: ret is eax, so not transcoding must be done */
5770 ret = do_rt_sigreturn(cpu_env);
5771 break;
bellard31e31b82003-02-18 22:55:36 +00005772 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00005773 if (!(p = lock_user_string(arg1)))
5774 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005775 ret = get_errno(sethostname(p, arg2));
5776 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005777 break;
5778 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00005779 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03005780 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00005781 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005782 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00005783 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5784 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005785 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5786 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005787 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00005788 ret = get_errno(setrlimit(resource, &rlim));
5789 }
5790 break;
bellard31e31b82003-02-18 22:55:36 +00005791 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00005792 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03005793 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00005794 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005795 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00005796
bellard9de5e442003-03-23 16:49:39 +00005797 ret = get_errno(getrlimit(resource, &rlim));
5798 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005799 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5800 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005801 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5802 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005803 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00005804 }
5805 }
5806 break;
bellard31e31b82003-02-18 22:55:36 +00005807 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00005808 {
5809 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00005810 ret = get_errno(getrusage(arg1, &rusage));
5811 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005812 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00005813 }
5814 }
5815 break;
bellard31e31b82003-02-18 22:55:36 +00005816 case TARGET_NR_gettimeofday:
5817 {
bellard31e31b82003-02-18 22:55:36 +00005818 struct timeval tv;
5819 ret = get_errno(gettimeofday(&tv, NULL));
5820 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00005821 if (copy_to_user_timeval(arg1, &tv))
5822 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005823 }
5824 }
5825 break;
5826 case TARGET_NR_settimeofday:
5827 {
bellard31e31b82003-02-18 22:55:36 +00005828 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00005829 if (copy_from_user_timeval(&tv, arg1))
5830 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005831 ret = get_errno(settimeofday(&tv, NULL));
5832 }
5833 break;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02005834#if defined(TARGET_NR_select) && !defined(TARGET_S390X) && !defined(TARGET_S390)
bellard31e31b82003-02-18 22:55:36 +00005835 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00005836 {
pbrook53a59602006-03-25 19:31:22 +00005837 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00005838 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00005839 long nsel;
5840
bellard579a97f2007-11-11 14:26:47 +00005841 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5842 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005843 nsel = tswapal(sel->n);
5844 inp = tswapal(sel->inp);
5845 outp = tswapal(sel->outp);
5846 exp = tswapal(sel->exp);
5847 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00005848 unlock_user_struct(sel, arg1, 0);
5849 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00005850 }
5851 break;
bellard048f6b42005-11-26 18:47:20 +00005852#endif
Riku Voipio9e423822010-05-07 12:28:05 +00005853#ifdef TARGET_NR_pselect6
5854 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04005855 {
5856 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
5857 fd_set rfds, wfds, efds;
5858 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
5859 struct timespec ts, *ts_ptr;
5860
5861 /*
5862 * The 6th arg is actually two args smashed together,
5863 * so we cannot use the C library.
5864 */
5865 sigset_t set;
5866 struct {
5867 sigset_t *set;
5868 size_t size;
5869 } sig, *sig_ptr;
5870
5871 abi_ulong arg_sigset, arg_sigsize, *arg7;
5872 target_sigset_t *target_sigset;
5873
5874 n = arg1;
5875 rfd_addr = arg2;
5876 wfd_addr = arg3;
5877 efd_addr = arg4;
5878 ts_addr = arg5;
5879
5880 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
5881 if (ret) {
5882 goto fail;
5883 }
5884 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
5885 if (ret) {
5886 goto fail;
5887 }
5888 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
5889 if (ret) {
5890 goto fail;
5891 }
5892
5893 /*
5894 * This takes a timespec, and not a timeval, so we cannot
5895 * use the do_select() helper ...
5896 */
5897 if (ts_addr) {
5898 if (target_to_host_timespec(&ts, ts_addr)) {
5899 goto efault;
5900 }
5901 ts_ptr = &ts;
5902 } else {
5903 ts_ptr = NULL;
5904 }
5905
5906 /* Extract the two packed args for the sigset */
5907 if (arg6) {
5908 sig_ptr = &sig;
5909 sig.size = _NSIG / 8;
5910
5911 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
5912 if (!arg7) {
5913 goto efault;
5914 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005915 arg_sigset = tswapal(arg7[0]);
5916 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04005917 unlock_user(arg7, arg6, 0);
5918
5919 if (arg_sigset) {
5920 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01005921 if (arg_sigsize != sizeof(*target_sigset)) {
5922 /* Like the kernel, we enforce correct size sigsets */
5923 ret = -TARGET_EINVAL;
5924 goto fail;
5925 }
Mike Frysinger055e0902011-06-03 17:01:49 -04005926 target_sigset = lock_user(VERIFY_READ, arg_sigset,
5927 sizeof(*target_sigset), 1);
5928 if (!target_sigset) {
5929 goto efault;
5930 }
5931 target_to_host_sigset(&set, target_sigset);
5932 unlock_user(target_sigset, arg_sigset, 0);
5933 } else {
5934 sig.set = NULL;
5935 }
5936 } else {
5937 sig_ptr = NULL;
5938 }
5939
5940 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
5941 ts_ptr, sig_ptr));
5942
5943 if (!is_error(ret)) {
5944 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
5945 goto efault;
5946 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
5947 goto efault;
5948 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
5949 goto efault;
5950
5951 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
5952 goto efault;
5953 }
5954 }
5955 break;
Riku Voipio9e423822010-05-07 12:28:05 +00005956#endif
bellard31e31b82003-02-18 22:55:36 +00005957 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00005958 {
5959 void *p2;
5960 p = lock_user_string(arg1);
5961 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005962 if (!p || !p2)
5963 ret = -TARGET_EFAULT;
5964 else
5965 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005966 unlock_user(p2, arg2, 0);
5967 unlock_user(p, arg1, 0);
5968 }
bellard31e31b82003-02-18 22:55:36 +00005969 break;
thsf0b62432007-09-24 09:25:40 +00005970#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5971 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00005972 {
bellard579a97f2007-11-11 14:26:47 +00005973 void *p2;
thsf0b62432007-09-24 09:25:40 +00005974 p = lock_user_string(arg1);
5975 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005976 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005977 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00005978 else
5979 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00005980 unlock_user(p2, arg3, 0);
5981 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00005982 }
5983 break;
5984#endif
bellardebc05482003-09-30 21:08:41 +00005985#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00005986 case TARGET_NR_oldlstat:
5987 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005988#endif
bellard31e31b82003-02-18 22:55:36 +00005989 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00005990 {
aurel32d088d662009-01-30 20:09:01 +00005991 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00005992 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00005993 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5994 if (!p || !p2)
5995 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00005996 else {
5997 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5998 char real[PATH_MAX];
5999 temp = realpath(exec_path,real);
6000 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
6001 snprintf((char *)p2, arg3, "%s", real);
6002 }
6003 else
6004 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00006005 }
pbrook53a59602006-03-25 19:31:22 +00006006 unlock_user(p2, arg2, ret);
6007 unlock_user(p, arg1, 0);
6008 }
bellard31e31b82003-02-18 22:55:36 +00006009 break;
ths5e0ccb12007-09-24 09:26:10 +00006010#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
6011 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00006012 {
bellard579a97f2007-11-11 14:26:47 +00006013 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00006014 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006015 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
6016 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006017 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00006018 else
6019 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00006020 unlock_user(p2, arg3, ret);
6021 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00006022 }
6023 break;
6024#endif
thse5febef2007-04-01 18:31:35 +00006025#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00006026 case TARGET_NR_uselib:
6027 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006028#endif
6029#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00006030 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00006031 if (!(p = lock_user_string(arg1)))
6032 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006033 ret = get_errno(swapon(p, arg2));
6034 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006035 break;
thse5febef2007-04-01 18:31:35 +00006036#endif
bellard31e31b82003-02-18 22:55:36 +00006037 case TARGET_NR_reboot:
Alexander Graf0f6b4d22011-09-27 14:39:42 +02006038 if (!(p = lock_user_string(arg4)))
6039 goto efault;
6040 ret = reboot(arg1, arg2, arg3, p);
6041 unlock_user(p, arg4, 0);
6042 break;
thse5febef2007-04-01 18:31:35 +00006043#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00006044 case TARGET_NR_readdir:
6045 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006046#endif
6047#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00006048 case TARGET_NR_mmap:
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006049#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || \
6050 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6051 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00006052 {
blueswir1992f48a2007-10-14 16:27:31 +00006053 abi_ulong *v;
6054 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00006055 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6056 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006057 v1 = tswapal(v[0]);
6058 v2 = tswapal(v[1]);
6059 v3 = tswapal(v[2]);
6060 v4 = tswapal(v[3]);
6061 v5 = tswapal(v[4]);
6062 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00006063 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00006064 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00006065 target_to_host_bitmask(v4, mmap_flags_tbl),
6066 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00006067 }
bellard31e31b82003-02-18 22:55:36 +00006068#else
ths5fafdf22007-09-16 21:08:06 +00006069 ret = get_errno(target_mmap(arg1, arg2, arg3,
6070 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00006071 arg5,
6072 arg6));
bellard31e31b82003-02-18 22:55:36 +00006073#endif
bellard6fb883e2003-07-09 17:12:39 +00006074 break;
thse5febef2007-04-01 18:31:35 +00006075#endif
bellarda315a142005-01-30 22:59:18 +00006076#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00006077 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00006078#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00006079#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00006080#endif
ths5fafdf22007-09-16 21:08:06 +00006081 ret = get_errno(target_mmap(arg1, arg2, arg3,
6082 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00006083 arg5,
bellardc573ff62004-01-04 15:51:36 +00006084 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00006085 break;
bellarda315a142005-01-30 22:59:18 +00006086#endif
bellard31e31b82003-02-18 22:55:36 +00006087 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00006088 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006089 break;
bellard9de5e442003-03-23 16:49:39 +00006090 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01006091 {
6092 TaskState *ts = ((CPUState *)cpu_env)->opaque;
6093 /* Special hack to detect libc making the stack executable. */
6094 if ((arg3 & PROT_GROWSDOWN)
6095 && arg1 >= ts->info->stack_limit
6096 && arg1 <= ts->info->start_stack) {
6097 arg3 &= ~PROT_GROWSDOWN;
6098 arg2 = arg2 + arg1 - ts->info->stack_limit;
6099 arg1 = ts->info->stack_limit;
6100 }
6101 }
bellard54936002003-05-13 00:25:15 +00006102 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006103 break;
thse5febef2007-04-01 18:31:35 +00006104#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00006105 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00006106 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00006107 break;
thse5febef2007-04-01 18:31:35 +00006108#endif
pbrook53a59602006-03-25 19:31:22 +00006109 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00006110#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00006111 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00006112 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006113 break;
thse5febef2007-04-01 18:31:35 +00006114#endif
6115#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00006116 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00006117 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006118 break;
thse5febef2007-04-01 18:31:35 +00006119#endif
6120#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00006121 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00006122 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006123 break;
thse5febef2007-04-01 18:31:35 +00006124#endif
6125#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00006126 case TARGET_NR_mlockall:
6127 ret = get_errno(mlockall(arg1));
6128 break;
thse5febef2007-04-01 18:31:35 +00006129#endif
6130#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00006131 case TARGET_NR_munlockall:
6132 ret = get_errno(munlockall());
6133 break;
thse5febef2007-04-01 18:31:35 +00006134#endif
bellard31e31b82003-02-18 22:55:36 +00006135 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00006136 if (!(p = lock_user_string(arg1)))
6137 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006138 ret = get_errno(truncate(p, arg2));
6139 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006140 break;
6141 case TARGET_NR_ftruncate:
6142 ret = get_errno(ftruncate(arg1, arg2));
6143 break;
6144 case TARGET_NR_fchmod:
6145 ret = get_errno(fchmod(arg1, arg2));
6146 break;
ths814d7972007-09-24 09:26:51 +00006147#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
6148 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00006149 if (!(p = lock_user_string(arg2)))
6150 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00006151 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00006152 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00006153 break;
6154#endif
bellard31e31b82003-02-18 22:55:36 +00006155 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00006156 /* libc does special remapping of the return value of
6157 * sys_getpriority() so it's just easiest to call
6158 * sys_getpriority() directly rather than through libc. */
Ulrich Hecht69137202009-09-17 21:08:37 +03006159 ret = get_errno(sys_getpriority(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006160 break;
6161 case TARGET_NR_setpriority:
6162 ret = get_errno(setpriority(arg1, arg2, arg3));
6163 break;
bellardebc05482003-09-30 21:08:41 +00006164#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00006165 case TARGET_NR_profil:
6166 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006167#endif
bellard31e31b82003-02-18 22:55:36 +00006168 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00006169 if (!(p = lock_user_string(arg1)))
6170 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006171 ret = get_errno(statfs(path(p), &stfs));
6172 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006173 convert_statfs:
6174 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006175 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006176
bellard579a97f2007-11-11 14:26:47 +00006177 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6178 goto efault;
6179 __put_user(stfs.f_type, &target_stfs->f_type);
6180 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6181 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6182 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6183 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6184 __put_user(stfs.f_files, &target_stfs->f_files);
6185 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6186 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6187 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6188 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00006189 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006190 }
6191 break;
6192 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00006193 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00006194 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00006195#ifdef TARGET_NR_statfs64
6196 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00006197 if (!(p = lock_user_string(arg1)))
6198 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006199 ret = get_errno(statfs(path(p), &stfs));
6200 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00006201 convert_statfs64:
6202 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006203 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006204
bellard579a97f2007-11-11 14:26:47 +00006205 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6206 goto efault;
6207 __put_user(stfs.f_type, &target_stfs->f_type);
6208 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6209 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6210 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6211 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6212 __put_user(stfs.f_files, &target_stfs->f_files);
6213 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6214 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6215 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6216 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
6217 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00006218 }
6219 break;
6220 case TARGET_NR_fstatfs64:
6221 ret = get_errno(fstatfs(arg1, &stfs));
6222 goto convert_statfs64;
6223#endif
bellardebc05482003-09-30 21:08:41 +00006224#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00006225 case TARGET_NR_ioperm:
6226 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006227#endif
thse5febef2007-04-01 18:31:35 +00006228#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00006229 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00006230 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00006231 break;
thse5febef2007-04-01 18:31:35 +00006232#endif
bellard3532fa72006-06-24 15:06:03 +00006233#ifdef TARGET_NR_accept
6234 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00006235 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006236 break;
6237#endif
6238#ifdef TARGET_NR_bind
6239 case TARGET_NR_bind:
6240 ret = do_bind(arg1, arg2, arg3);
6241 break;
6242#endif
6243#ifdef TARGET_NR_connect
6244 case TARGET_NR_connect:
6245 ret = do_connect(arg1, arg2, arg3);
6246 break;
6247#endif
6248#ifdef TARGET_NR_getpeername
6249 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00006250 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006251 break;
6252#endif
6253#ifdef TARGET_NR_getsockname
6254 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00006255 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006256 break;
6257#endif
6258#ifdef TARGET_NR_getsockopt
6259 case TARGET_NR_getsockopt:
6260 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6261 break;
6262#endif
6263#ifdef TARGET_NR_listen
6264 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00006265 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006266 break;
6267#endif
6268#ifdef TARGET_NR_recv
6269 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00006270 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006271 break;
6272#endif
6273#ifdef TARGET_NR_recvfrom
6274 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00006275 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006276 break;
6277#endif
6278#ifdef TARGET_NR_recvmsg
6279 case TARGET_NR_recvmsg:
6280 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6281 break;
6282#endif
6283#ifdef TARGET_NR_send
6284 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00006285 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006286 break;
6287#endif
6288#ifdef TARGET_NR_sendmsg
6289 case TARGET_NR_sendmsg:
6290 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6291 break;
6292#endif
6293#ifdef TARGET_NR_sendto
6294 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00006295 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006296 break;
6297#endif
6298#ifdef TARGET_NR_shutdown
6299 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00006300 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006301 break;
6302#endif
6303#ifdef TARGET_NR_socket
6304 case TARGET_NR_socket:
6305 ret = do_socket(arg1, arg2, arg3);
6306 break;
6307#endif
6308#ifdef TARGET_NR_socketpair
6309 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00006310 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00006311 break;
6312#endif
6313#ifdef TARGET_NR_setsockopt
6314 case TARGET_NR_setsockopt:
6315 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6316 break;
6317#endif
ths7494b0f2007-02-11 18:26:53 +00006318
bellard31e31b82003-02-18 22:55:36 +00006319 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00006320 if (!(p = lock_user_string(arg2)))
6321 goto efault;
thse5574482007-02-11 20:03:13 +00006322 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6323 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00006324 break;
6325
bellard31e31b82003-02-18 22:55:36 +00006326 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00006327 {
bellard66fb9762003-03-23 01:06:05 +00006328 struct itimerval value, ovalue, *pvalue;
6329
pbrook53a59602006-03-25 19:31:22 +00006330 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006331 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00006332 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6333 || copy_from_user_timeval(&pvalue->it_value,
6334 arg2 + sizeof(struct target_timeval)))
6335 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006336 } else {
6337 pvalue = NULL;
6338 }
6339 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00006340 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00006341 if (copy_to_user_timeval(arg3,
6342 &ovalue.it_interval)
6343 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6344 &ovalue.it_value))
6345 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006346 }
6347 }
6348 break;
bellard31e31b82003-02-18 22:55:36 +00006349 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00006350 {
bellard66fb9762003-03-23 01:06:05 +00006351 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00006352
bellard66fb9762003-03-23 01:06:05 +00006353 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00006354 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00006355 if (copy_to_user_timeval(arg2,
6356 &value.it_interval)
6357 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6358 &value.it_value))
6359 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006360 }
6361 }
6362 break;
bellard31e31b82003-02-18 22:55:36 +00006363 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00006364 if (!(p = lock_user_string(arg1)))
6365 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006366 ret = get_errno(stat(path(p), &st));
6367 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006368 goto do_stat;
6369 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00006370 if (!(p = lock_user_string(arg1)))
6371 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006372 ret = get_errno(lstat(path(p), &st));
6373 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006374 goto do_stat;
6375 case TARGET_NR_fstat:
6376 {
6377 ret = get_errno(fstat(arg1, &st));
6378 do_stat:
6379 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006380 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00006381
bellard579a97f2007-11-11 14:26:47 +00006382 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6383 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02006384 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00006385 __put_user(st.st_dev, &target_st->st_dev);
6386 __put_user(st.st_ino, &target_st->st_ino);
6387 __put_user(st.st_mode, &target_st->st_mode);
6388 __put_user(st.st_uid, &target_st->st_uid);
6389 __put_user(st.st_gid, &target_st->st_gid);
6390 __put_user(st.st_nlink, &target_st->st_nlink);
6391 __put_user(st.st_rdev, &target_st->st_rdev);
6392 __put_user(st.st_size, &target_st->st_size);
6393 __put_user(st.st_blksize, &target_st->st_blksize);
6394 __put_user(st.st_blocks, &target_st->st_blocks);
6395 __put_user(st.st_atime, &target_st->target_st_atime);
6396 __put_user(st.st_mtime, &target_st->target_st_mtime);
6397 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00006398 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006399 }
6400 }
6401 break;
bellardebc05482003-09-30 21:08:41 +00006402#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00006403 case TARGET_NR_olduname:
6404 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006405#endif
6406#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00006407 case TARGET_NR_iopl:
6408 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006409#endif
bellard31e31b82003-02-18 22:55:36 +00006410 case TARGET_NR_vhangup:
6411 ret = get_errno(vhangup());
6412 break;
bellardebc05482003-09-30 21:08:41 +00006413#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00006414 case TARGET_NR_idle:
6415 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006416#endif
bellard42ad6ae2005-01-03 22:48:11 +00006417#ifdef TARGET_NR_syscall
6418 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01006419 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6420 arg6, arg7, arg8, 0);
6421 break;
bellard42ad6ae2005-01-03 22:48:11 +00006422#endif
bellard31e31b82003-02-18 22:55:36 +00006423 case TARGET_NR_wait4:
6424 {
6425 int status;
blueswir1992f48a2007-10-14 16:27:31 +00006426 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00006427 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00006428 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00006429 if (target_rusage)
6430 rusage_ptr = &rusage;
6431 else
6432 rusage_ptr = NULL;
6433 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6434 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01006435 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00006436 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00006437 if (put_user_s32(status, status_ptr))
6438 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006439 }
bellard2f619692007-11-16 10:46:05 +00006440 if (target_rusage)
6441 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00006442 }
6443 }
6444 break;
thse5febef2007-04-01 18:31:35 +00006445#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00006446 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00006447 if (!(p = lock_user_string(arg1)))
6448 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006449 ret = get_errno(swapoff(p));
6450 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006451 break;
thse5febef2007-04-01 18:31:35 +00006452#endif
bellard31e31b82003-02-18 22:55:36 +00006453 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00006454 {
pbrook53a59602006-03-25 19:31:22 +00006455 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00006456 struct sysinfo value;
6457 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00006458 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00006459 {
bellard579a97f2007-11-11 14:26:47 +00006460 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6461 goto efault;
bellarda5448a72004-06-19 16:59:03 +00006462 __put_user(value.uptime, &target_value->uptime);
6463 __put_user(value.loads[0], &target_value->loads[0]);
6464 __put_user(value.loads[1], &target_value->loads[1]);
6465 __put_user(value.loads[2], &target_value->loads[2]);
6466 __put_user(value.totalram, &target_value->totalram);
6467 __put_user(value.freeram, &target_value->freeram);
6468 __put_user(value.sharedram, &target_value->sharedram);
6469 __put_user(value.bufferram, &target_value->bufferram);
6470 __put_user(value.totalswap, &target_value->totalswap);
6471 __put_user(value.freeswap, &target_value->freeswap);
6472 __put_user(value.procs, &target_value->procs);
6473 __put_user(value.totalhigh, &target_value->totalhigh);
6474 __put_user(value.freehigh, &target_value->freehigh);
6475 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00006476 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00006477 }
6478 }
6479 break;
thse5febef2007-04-01 18:31:35 +00006480#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00006481 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00006482 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6483 break;
thse5febef2007-04-01 18:31:35 +00006484#endif
aurel32e5289082009-04-18 16:16:12 +00006485#ifdef TARGET_NR_semget
6486 case TARGET_NR_semget:
6487 ret = get_errno(semget(arg1, arg2, arg3));
6488 break;
6489#endif
6490#ifdef TARGET_NR_semop
6491 case TARGET_NR_semop:
6492 ret = get_errno(do_semop(arg1, arg2, arg3));
6493 break;
6494#endif
6495#ifdef TARGET_NR_semctl
6496 case TARGET_NR_semctl:
6497 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6498 break;
6499#endif
aurel32eeb438c2008-10-13 21:08:55 +00006500#ifdef TARGET_NR_msgctl
6501 case TARGET_NR_msgctl:
6502 ret = do_msgctl(arg1, arg2, arg3);
6503 break;
6504#endif
6505#ifdef TARGET_NR_msgget
6506 case TARGET_NR_msgget:
6507 ret = get_errno(msgget(arg1, arg2));
6508 break;
6509#endif
6510#ifdef TARGET_NR_msgrcv
6511 case TARGET_NR_msgrcv:
6512 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
6513 break;
6514#endif
6515#ifdef TARGET_NR_msgsnd
6516 case TARGET_NR_msgsnd:
6517 ret = do_msgsnd(arg1, arg2, arg3, arg4);
6518 break;
6519#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03006520#ifdef TARGET_NR_shmget
6521 case TARGET_NR_shmget:
6522 ret = get_errno(shmget(arg1, arg2, arg3));
6523 break;
6524#endif
6525#ifdef TARGET_NR_shmctl
6526 case TARGET_NR_shmctl:
6527 ret = do_shmctl(arg1, arg2, arg3);
6528 break;
6529#endif
6530#ifdef TARGET_NR_shmat
6531 case TARGET_NR_shmat:
6532 ret = do_shmat(arg1, arg2, arg3);
6533 break;
6534#endif
6535#ifdef TARGET_NR_shmdt
6536 case TARGET_NR_shmdt:
6537 ret = do_shmdt(arg1);
6538 break;
6539#endif
bellard31e31b82003-02-18 22:55:36 +00006540 case TARGET_NR_fsync:
6541 ret = get_errno(fsync(arg1));
6542 break;
bellard31e31b82003-02-18 22:55:36 +00006543 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07006544#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00006545 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00006546#elif defined(TARGET_CRIS)
6547 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006548#elif defined(TARGET_S390X)
6549 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00006550#else
pbrookd865bab2008-06-07 22:12:17 +00006551 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006552#endif
bellard1b6b0292003-03-22 17:31:38 +00006553 break;
bellardec86b0f2003-04-11 00:15:04 +00006554#ifdef __NR_exit_group
6555 /* new thread calls */
6556 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02006557#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00006558 _mcleanup();
6559#endif
bellarde9009672005-04-26 20:42:36 +00006560 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00006561 ret = get_errno(exit_group(arg1));
6562 break;
6563#endif
bellard31e31b82003-02-18 22:55:36 +00006564 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00006565 if (!(p = lock_user_string(arg1)))
6566 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006567 ret = get_errno(setdomainname(p, arg2));
6568 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006569 break;
6570 case TARGET_NR_uname:
6571 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00006572 {
6573 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00006574
bellard579a97f2007-11-11 14:26:47 +00006575 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
6576 goto efault;
bellard29e619b2004-09-13 21:41:04 +00006577 ret = get_errno(sys_uname(buf));
6578 if (!is_error(ret)) {
6579 /* Overrite the native machine name with whatever is being
6580 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01006581 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00006582 /* Allow the user to override the reported release. */
6583 if (qemu_uname_release && *qemu_uname_release)
6584 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00006585 }
pbrook53a59602006-03-25 19:31:22 +00006586 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00006587 }
bellard31e31b82003-02-18 22:55:36 +00006588 break;
bellard6dbad632003-03-16 18:05:05 +00006589#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00006590 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00006591 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00006592 break;
j_mayer84409dd2007-04-06 08:56:50 +00006593#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00006594 case TARGET_NR_vm86old:
6595 goto unimplemented;
6596 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00006597 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00006598 break;
6599#endif
j_mayer84409dd2007-04-06 08:56:50 +00006600#endif
bellard31e31b82003-02-18 22:55:36 +00006601 case TARGET_NR_adjtimex:
6602 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006603#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00006604 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00006605#endif
bellard31e31b82003-02-18 22:55:36 +00006606 case TARGET_NR_init_module:
6607 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00006608#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00006609 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00006610#endif
bellard31e31b82003-02-18 22:55:36 +00006611 goto unimplemented;
6612 case TARGET_NR_quotactl:
6613 goto unimplemented;
6614 case TARGET_NR_getpgid:
6615 ret = get_errno(getpgid(arg1));
6616 break;
6617 case TARGET_NR_fchdir:
6618 ret = get_errno(fchdir(arg1));
6619 break;
j_mayer84409dd2007-04-06 08:56:50 +00006620#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00006621 case TARGET_NR_bdflush:
6622 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00006623#endif
thse5febef2007-04-01 18:31:35 +00006624#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00006625 case TARGET_NR_sysfs:
6626 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006627#endif
bellard31e31b82003-02-18 22:55:36 +00006628 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00006629 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00006630 break;
thse5febef2007-04-01 18:31:35 +00006631#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00006632 case TARGET_NR_afs_syscall:
6633 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006634#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006635#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006636 case TARGET_NR__llseek:
6637 {
6638 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00006639#if !defined(__NR_llseek)
6640 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
6641 if (res == -1) {
6642 ret = get_errno(res);
6643 } else {
6644 ret = 0;
6645 }
6646#else
bellard31e31b82003-02-18 22:55:36 +00006647 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00006648#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00006649 if ((ret == 0) && put_user_s64(res, arg4)) {
6650 goto efault;
6651 }
bellard31e31b82003-02-18 22:55:36 +00006652 }
6653 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006654#endif
bellard31e31b82003-02-18 22:55:36 +00006655 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02006656#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00006657 {
pbrook53a59602006-03-25 19:31:22 +00006658 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00006659 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006660 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00006661
6662 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00006663 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00006664 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00006665 goto fail;
6666 }
ths3b46e622007-09-17 08:09:54 +00006667
bellard4add45b2003-06-05 01:52:59 +00006668 ret = get_errno(sys_getdents(arg1, dirp, count));
6669 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006670 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00006671 struct target_dirent *tde;
6672 int len = ret;
6673 int reclen, treclen;
6674 int count1, tnamelen;
6675
6676 count1 = 0;
6677 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00006678 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6679 goto efault;
bellard4add45b2003-06-05 01:52:59 +00006680 tde = target_dirp;
6681 while (len > 0) {
6682 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00006683 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00006684 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006685 tde->d_ino = tswapal(de->d_ino);
6686 tde->d_off = tswapal(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00006687 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00006688 if (tnamelen > 256)
6689 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00006690 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00006691 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00006692 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00006693 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00006694 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00006695 count1 += treclen;
6696 }
6697 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00006698 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00006699 }
6700 free(dirp);
6701 }
6702#else
bellard31e31b82003-02-18 22:55:36 +00006703 {
aurel326556a832008-10-13 21:08:17 +00006704 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006705 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00006706
bellard579a97f2007-11-11 14:26:47 +00006707 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6708 goto efault;
bellard72f03902003-02-18 23:33:18 +00006709 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00006710 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006711 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00006712 int len = ret;
6713 int reclen;
6714 de = dirp;
6715 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006716 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00006717 if (reclen > len)
6718 break;
bellard8083a3e2003-03-24 23:12:16 +00006719 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00006720 tswapls(&de->d_ino);
6721 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006722 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00006723 len -= reclen;
6724 }
6725 }
pbrook53a59602006-03-25 19:31:22 +00006726 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00006727 }
bellard4add45b2003-06-05 01:52:59 +00006728#endif
bellard31e31b82003-02-18 22:55:36 +00006729 break;
ths3ae43202007-09-16 21:39:48 +00006730#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00006731 case TARGET_NR_getdents64:
6732 {
aurel326556a832008-10-13 21:08:17 +00006733 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006734 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00006735 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6736 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00006737 ret = get_errno(sys_getdents64(arg1, dirp, count));
6738 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006739 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00006740 int len = ret;
6741 int reclen;
6742 de = dirp;
6743 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006744 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00006745 if (reclen > len)
6746 break;
bellard8083a3e2003-03-24 23:12:16 +00006747 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00006748 tswap64s((uint64_t *)&de->d_ino);
6749 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006750 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00006751 len -= reclen;
6752 }
6753 }
pbrook53a59602006-03-25 19:31:22 +00006754 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00006755 }
6756 break;
bellarda541f292004-04-12 20:39:29 +00006757#endif /* TARGET_NR_getdents64 */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006758#if defined(TARGET_NR__newselect) || defined(TARGET_S390X)
6759#ifdef TARGET_S390X
6760 case TARGET_NR_select:
6761#else
bellard31e31b82003-02-18 22:55:36 +00006762 case TARGET_NR__newselect:
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006763#endif
pbrook53a59602006-03-25 19:31:22 +00006764 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00006765 break;
thse5febef2007-04-01 18:31:35 +00006766#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05006767#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
6768# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00006769 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05006770# endif
6771# ifdef TARGET_NR_ppoll
6772 case TARGET_NR_ppoll:
6773# endif
bellard9de5e442003-03-23 16:49:39 +00006774 {
pbrook53a59602006-03-25 19:31:22 +00006775 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00006776 unsigned int nfds = arg2;
6777 int timeout = arg3;
6778 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00006779 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00006780
bellard579a97f2007-11-11 14:26:47 +00006781 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6782 if (!target_pfd)
6783 goto efault;
Mike Frysingerd8035d42011-02-07 01:05:51 -05006784
bellard9de5e442003-03-23 16:49:39 +00006785 pfd = alloca(sizeof(struct pollfd) * nfds);
6786 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006787 pfd[i].fd = tswap32(target_pfd[i].fd);
6788 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00006789 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05006790
6791# ifdef TARGET_NR_ppoll
6792 if (num == TARGET_NR_ppoll) {
6793 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
6794 target_sigset_t *target_set;
6795 sigset_t _set, *set = &_set;
6796
6797 if (arg3) {
6798 if (target_to_host_timespec(timeout_ts, arg3)) {
6799 unlock_user(target_pfd, arg1, 0);
6800 goto efault;
6801 }
6802 } else {
6803 timeout_ts = NULL;
6804 }
6805
6806 if (arg4) {
6807 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
6808 if (!target_set) {
6809 unlock_user(target_pfd, arg1, 0);
6810 goto efault;
6811 }
6812 target_to_host_sigset(set, target_set);
6813 } else {
6814 set = NULL;
6815 }
6816
6817 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
6818
6819 if (!is_error(ret) && arg3) {
6820 host_to_target_timespec(arg3, timeout_ts);
6821 }
6822 if (arg4) {
6823 unlock_user(target_set, arg4, 0);
6824 }
6825 } else
6826# endif
6827 ret = get_errno(poll(pfd, nfds, timeout));
6828
bellard9de5e442003-03-23 16:49:39 +00006829 if (!is_error(ret)) {
6830 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006831 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00006832 }
6833 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00006834 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00006835 }
6836 break;
thse5febef2007-04-01 18:31:35 +00006837#endif
bellard31e31b82003-02-18 22:55:36 +00006838 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00006839 /* NOTE: the flock constant seems to be the same for every
6840 Linux platform */
6841 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006842 break;
6843 case TARGET_NR_readv:
6844 {
6845 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006846 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006847
6848 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006849 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6850 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006851 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006852 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00006853 }
6854 break;
6855 case TARGET_NR_writev:
6856 {
6857 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006858 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006859
6860 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006861 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6862 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006863 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006864 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00006865 }
6866 break;
6867 case TARGET_NR_getsid:
6868 ret = get_errno(getsid(arg1));
6869 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006870#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00006871 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00006872 ret = get_errno(fdatasync(arg1));
6873 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006874#endif
bellard31e31b82003-02-18 22:55:36 +00006875 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00006876 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00006877 return value. */
ths0da46a62007-10-20 20:23:07 +00006878 ret = -TARGET_ENOTDIR;
6879 break;
Mike Frysinger737de1d2011-02-07 01:05:55 -05006880 case TARGET_NR_sched_getaffinity:
6881 {
6882 unsigned int mask_size;
6883 unsigned long *mask;
6884
6885 /*
6886 * sched_getaffinity needs multiples of ulong, so need to take
6887 * care of mismatches between target ulong and host ulong sizes.
6888 */
6889 if (arg2 & (sizeof(abi_ulong) - 1)) {
6890 ret = -TARGET_EINVAL;
6891 break;
6892 }
6893 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
6894
6895 mask = alloca(mask_size);
6896 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
6897
6898 if (!is_error(ret)) {
Mike McCormackcd18f052011-04-18 14:43:36 +09006899 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05006900 goto efault;
6901 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05006902 }
6903 }
6904 break;
6905 case TARGET_NR_sched_setaffinity:
6906 {
6907 unsigned int mask_size;
6908 unsigned long *mask;
6909
6910 /*
6911 * sched_setaffinity needs multiples of ulong, so need to take
6912 * care of mismatches between target ulong and host ulong sizes.
6913 */
6914 if (arg2 & (sizeof(abi_ulong) - 1)) {
6915 ret = -TARGET_EINVAL;
6916 break;
6917 }
6918 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
6919
6920 mask = alloca(mask_size);
6921 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
6922 goto efault;
6923 }
6924 memcpy(mask, p, arg2);
6925 unlock_user_struct(p, arg2, 0);
6926
6927 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
6928 }
6929 break;
bellard31e31b82003-02-18 22:55:36 +00006930 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00006931 {
pbrook53a59602006-03-25 19:31:22 +00006932 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006933 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00006934
bellard579a97f2007-11-11 14:26:47 +00006935 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6936 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006937 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006938 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00006939 ret = get_errno(sched_setparam(arg1, &schp));
6940 }
6941 break;
bellard31e31b82003-02-18 22:55:36 +00006942 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00006943 {
pbrook53a59602006-03-25 19:31:22 +00006944 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006945 struct sched_param schp;
6946 ret = get_errno(sched_getparam(arg1, &schp));
6947 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006948 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6949 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006950 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006951 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00006952 }
6953 }
6954 break;
bellard31e31b82003-02-18 22:55:36 +00006955 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00006956 {
pbrook53a59602006-03-25 19:31:22 +00006957 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006958 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00006959 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6960 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006961 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006962 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00006963 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6964 }
6965 break;
bellard31e31b82003-02-18 22:55:36 +00006966 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00006967 ret = get_errno(sched_getscheduler(arg1));
6968 break;
bellard31e31b82003-02-18 22:55:36 +00006969 case TARGET_NR_sched_yield:
6970 ret = get_errno(sched_yield());
6971 break;
6972 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00006973 ret = get_errno(sched_get_priority_max(arg1));
6974 break;
bellard31e31b82003-02-18 22:55:36 +00006975 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00006976 ret = get_errno(sched_get_priority_min(arg1));
6977 break;
bellard31e31b82003-02-18 22:55:36 +00006978 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00006979 {
bellard5cd43932003-03-29 16:54:36 +00006980 struct timespec ts;
6981 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6982 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006983 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00006984 }
6985 }
6986 break;
bellard31e31b82003-02-18 22:55:36 +00006987 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00006988 {
bellard1b6b0292003-03-22 17:31:38 +00006989 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00006990 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00006991 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00006992 if (is_error(ret) && arg2) {
6993 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00006994 }
6995 }
6996 break;
thse5febef2007-04-01 18:31:35 +00006997#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00006998 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00006999 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007000#endif
7001#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00007002 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00007003 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007004#endif
bellard31e31b82003-02-18 22:55:36 +00007005 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00007006 switch (arg1)
7007 {
7008 case PR_GET_PDEATHSIG:
7009 {
7010 int deathsig;
7011 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00007012 if (!is_error(ret) && arg2
7013 && put_user_ual(deathsig, arg2))
7014 goto efault;
thse5574482007-02-11 20:03:13 +00007015 }
7016 break;
7017 default:
7018 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7019 break;
7020 }
ths39b9aae2007-02-11 18:36:44 +00007021 break;
bellardd2fd1af2007-11-14 18:08:56 +00007022#ifdef TARGET_NR_arch_prctl
7023 case TARGET_NR_arch_prctl:
7024#if defined(TARGET_I386) && !defined(TARGET_ABI32)
7025 ret = do_arch_prctl(cpu_env, arg1, arg2);
7026 break;
7027#else
7028 goto unimplemented;
7029#endif
7030#endif
bellard67867302003-11-23 17:05:30 +00007031#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00007032 case TARGET_NR_pread:
Riku Voipio48e515d2011-07-12 15:40:51 +03007033 if (regpairs_aligned(cpu_env))
balroga4ae00b2008-09-20 03:14:14 +00007034 arg4 = arg5;
bellard579a97f2007-11-11 14:26:47 +00007035 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7036 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007037 ret = get_errno(pread(arg1, p, arg3, arg4));
7038 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00007039 break;
bellard31e31b82003-02-18 22:55:36 +00007040 case TARGET_NR_pwrite:
Riku Voipio48e515d2011-07-12 15:40:51 +03007041 if (regpairs_aligned(cpu_env))
balroga4ae00b2008-09-20 03:14:14 +00007042 arg4 = arg5;
bellard579a97f2007-11-11 14:26:47 +00007043 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7044 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007045 ret = get_errno(pwrite(arg1, p, arg3, arg4));
7046 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00007047 break;
bellard67867302003-11-23 17:05:30 +00007048#endif
aurel32f2c7ba12008-03-28 22:32:06 +00007049#ifdef TARGET_NR_pread64
7050 case TARGET_NR_pread64:
7051 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7052 goto efault;
7053 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7054 unlock_user(p, arg2, ret);
7055 break;
7056 case TARGET_NR_pwrite64:
7057 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7058 goto efault;
7059 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7060 unlock_user(p, arg2, 0);
7061 break;
7062#endif
bellard31e31b82003-02-18 22:55:36 +00007063 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00007064 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7065 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007066 ret = get_errno(sys_getcwd1(p, arg2));
7067 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00007068 break;
7069 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00007070 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007071 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00007072 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007073 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00007074#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02007075 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007076 defined(TARGET_M68K) || defined(TARGET_S390X)
bellard579a97f2007-11-11 14:26:47 +00007077 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00007078 break;
7079#else
bellard5cd43932003-03-29 16:54:36 +00007080 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00007081#endif
bellard31e31b82003-02-18 22:55:36 +00007082 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00007083 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007084#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00007085 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00007086 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007087#endif
7088#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00007089 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00007090 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007091#endif
bellard048f6b42005-11-26 18:47:20 +00007092#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00007093 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00007094 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
7095 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00007096 break;
bellard048f6b42005-11-26 18:47:20 +00007097#endif
bellardebc05482003-09-30 21:08:41 +00007098#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00007099 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00007100 {
7101 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007102 int resource = target_to_host_resource(arg1);
7103 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00007104 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007105 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00007106 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7107 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007108 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7109 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007110 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00007111 }
7112 break;
7113 }
bellardebc05482003-09-30 21:08:41 +00007114#endif
bellarda315a142005-01-30 22:59:18 +00007115#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00007116 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00007117 if (!(p = lock_user_string(arg1)))
7118 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007119 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
7120 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00007121 break;
bellarda315a142005-01-30 22:59:18 +00007122#endif
7123#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00007124 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00007125 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00007126 break;
bellarda315a142005-01-30 22:59:18 +00007127#endif
7128#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00007129 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00007130 if (!(p = lock_user_string(arg1)))
7131 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007132 ret = get_errno(stat(path(p), &st));
7133 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007134 if (!is_error(ret))
7135 ret = host_to_target_stat64(cpu_env, arg2, &st);
7136 break;
bellarda315a142005-01-30 22:59:18 +00007137#endif
7138#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00007139 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00007140 if (!(p = lock_user_string(arg1)))
7141 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007142 ret = get_errno(lstat(path(p), &st));
7143 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007144 if (!is_error(ret))
7145 ret = host_to_target_stat64(cpu_env, arg2, &st);
7146 break;
bellarda315a142005-01-30 22:59:18 +00007147#endif
7148#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00007149 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00007150 ret = get_errno(fstat(arg1, &st));
7151 if (!is_error(ret))
7152 ret = host_to_target_stat64(cpu_env, arg2, &st);
7153 break;
bellardec86b0f2003-04-11 00:15:04 +00007154#endif
aurel329d33b762009-04-08 23:07:05 +00007155#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
7156 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
7157#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007158 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00007159#endif
7160#ifdef TARGET_NR_newfstatat
7161 case TARGET_NR_newfstatat:
7162#endif
balrog6a24a772008-09-20 02:23:36 +00007163 if (!(p = lock_user_string(arg2)))
7164 goto efault;
aurel329d33b762009-04-08 23:07:05 +00007165#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007166 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00007167#else
7168 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
7169#endif
balrog6a24a772008-09-20 02:23:36 +00007170 if (!is_error(ret))
7171 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00007172 break;
bellarda315a142005-01-30 22:59:18 +00007173#endif
bellard67867302003-11-23 17:05:30 +00007174 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00007175 if (!(p = lock_user_string(arg1)))
7176 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007177 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7178 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007179 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007180#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00007181 case TARGET_NR_getuid:
7182 ret = get_errno(high2lowuid(getuid()));
7183 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007184#endif
7185#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00007186 case TARGET_NR_getgid:
7187 ret = get_errno(high2lowgid(getgid()));
7188 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007189#endif
7190#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00007191 case TARGET_NR_geteuid:
7192 ret = get_errno(high2lowuid(geteuid()));
7193 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007194#endif
7195#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00007196 case TARGET_NR_getegid:
7197 ret = get_errno(high2lowgid(getegid()));
7198 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007199#endif
bellard67867302003-11-23 17:05:30 +00007200 case TARGET_NR_setreuid:
7201 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7202 break;
7203 case TARGET_NR_setregid:
7204 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7205 break;
7206 case TARGET_NR_getgroups:
7207 {
7208 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007209 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007210 gid_t *grouplist;
7211 int i;
7212
7213 grouplist = alloca(gidsetsize * sizeof(gid_t));
7214 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007215 if (gidsetsize == 0)
7216 break;
bellard67867302003-11-23 17:05:30 +00007217 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007218 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
7219 if (!target_grouplist)
7220 goto efault;
balroga2155fc2008-09-20 02:12:08 +00007221 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007222 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00007223 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00007224 }
7225 }
7226 break;
7227 case TARGET_NR_setgroups:
7228 {
7229 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007230 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007231 gid_t *grouplist;
7232 int i;
7233
7234 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007235 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
7236 if (!target_grouplist) {
7237 ret = -TARGET_EFAULT;
7238 goto fail;
7239 }
bellard67867302003-11-23 17:05:30 +00007240 for(i = 0;i < gidsetsize; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007241 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00007242 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00007243 ret = get_errno(setgroups(gidsetsize, grouplist));
7244 }
7245 break;
7246 case TARGET_NR_fchown:
7247 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7248 break;
thsccfa72b2007-09-24 09:23:34 +00007249#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
7250 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00007251 if (!(p = lock_user_string(arg2)))
7252 goto efault;
7253 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
7254 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00007255 break;
7256#endif
bellard67867302003-11-23 17:05:30 +00007257#ifdef TARGET_NR_setresuid
7258 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00007259 ret = get_errno(setresuid(low2highuid(arg1),
7260 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00007261 low2highuid(arg3)));
7262 break;
7263#endif
7264#ifdef TARGET_NR_getresuid
7265 case TARGET_NR_getresuid:
7266 {
pbrook53a59602006-03-25 19:31:22 +00007267 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00007268 ret = get_errno(getresuid(&ruid, &euid, &suid));
7269 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007270 if (put_user_u16(high2lowuid(ruid), arg1)
7271 || put_user_u16(high2lowuid(euid), arg2)
7272 || put_user_u16(high2lowuid(suid), arg3))
7273 goto efault;
bellard67867302003-11-23 17:05:30 +00007274 }
7275 }
7276 break;
7277#endif
7278#ifdef TARGET_NR_getresgid
7279 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00007280 ret = get_errno(setresgid(low2highgid(arg1),
7281 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00007282 low2highgid(arg3)));
7283 break;
7284#endif
7285#ifdef TARGET_NR_getresgid
7286 case TARGET_NR_getresgid:
7287 {
pbrook53a59602006-03-25 19:31:22 +00007288 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00007289 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7290 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007291 if (put_user_u16(high2lowgid(rgid), arg1)
7292 || put_user_u16(high2lowgid(egid), arg2)
7293 || put_user_u16(high2lowgid(sgid), arg3))
7294 goto efault;
bellard67867302003-11-23 17:05:30 +00007295 }
7296 }
7297 break;
7298#endif
7299 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00007300 if (!(p = lock_user_string(arg1)))
7301 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007302 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7303 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007304 break;
7305 case TARGET_NR_setuid:
7306 ret = get_errno(setuid(low2highuid(arg1)));
7307 break;
7308 case TARGET_NR_setgid:
7309 ret = get_errno(setgid(low2highgid(arg1)));
7310 break;
7311 case TARGET_NR_setfsuid:
7312 ret = get_errno(setfsuid(arg1));
7313 break;
7314 case TARGET_NR_setfsgid:
7315 ret = get_errno(setfsgid(arg1));
7316 break;
bellard67867302003-11-23 17:05:30 +00007317
bellarda315a142005-01-30 22:59:18 +00007318#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00007319 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00007320 if (!(p = lock_user_string(arg1)))
7321 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007322 ret = get_errno(lchown(p, arg2, arg3));
7323 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007324 break;
bellarda315a142005-01-30 22:59:18 +00007325#endif
7326#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00007327 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00007328 ret = get_errno(getuid());
7329 break;
bellarda315a142005-01-30 22:59:18 +00007330#endif
aurel3264b4d282008-11-14 17:20:15 +00007331
7332#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7333 /* Alpha specific */
7334 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007335 {
7336 uid_t euid;
7337 euid=geteuid();
7338 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7339 }
aurel3264b4d282008-11-14 17:20:15 +00007340 ret = get_errno(getuid());
7341 break;
7342#endif
7343#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7344 /* Alpha specific */
7345 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007346 {
7347 uid_t egid;
7348 egid=getegid();
7349 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7350 }
aurel3264b4d282008-11-14 17:20:15 +00007351 ret = get_errno(getgid());
7352 break;
7353#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08007354#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7355 /* Alpha specific */
7356 case TARGET_NR_osf_getsysinfo:
7357 ret = -TARGET_EOPNOTSUPP;
7358 switch (arg1) {
7359 case TARGET_GSI_IEEE_FP_CONTROL:
7360 {
7361 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7362
7363 /* Copied from linux ieee_fpcr_to_swcr. */
7364 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7365 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7366 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7367 | SWCR_TRAP_ENABLE_DZE
7368 | SWCR_TRAP_ENABLE_OVF);
7369 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
7370 | SWCR_TRAP_ENABLE_INE);
7371 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
7372 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
7373
7374 if (put_user_u64 (swcr, arg2))
7375 goto efault;
7376 ret = 0;
7377 }
7378 break;
7379
7380 /* case GSI_IEEE_STATE_AT_SIGNAL:
7381 -- Not implemented in linux kernel.
7382 case GSI_UACPROC:
7383 -- Retrieves current unaligned access state; not much used.
7384 case GSI_PROC_TYPE:
7385 -- Retrieves implver information; surely not used.
7386 case GSI_GET_HWRPB:
7387 -- Grabs a copy of the HWRPB; surely not used.
7388 */
7389 }
7390 break;
7391#endif
7392#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
7393 /* Alpha specific */
7394 case TARGET_NR_osf_setsysinfo:
7395 ret = -TARGET_EOPNOTSUPP;
7396 switch (arg1) {
7397 case TARGET_SSI_IEEE_FP_CONTROL:
7398 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
7399 {
7400 uint64_t swcr, fpcr, orig_fpcr;
7401
7402 if (get_user_u64 (swcr, arg2))
7403 goto efault;
7404 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
7405 fpcr = orig_fpcr & FPCR_DYN_MASK;
7406
7407 /* Copied from linux ieee_swcr_to_fpcr. */
7408 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
7409 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
7410 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
7411 | SWCR_TRAP_ENABLE_DZE
7412 | SWCR_TRAP_ENABLE_OVF)) << 48;
7413 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
7414 | SWCR_TRAP_ENABLE_INE)) << 57;
7415 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
7416 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
7417
7418 cpu_alpha_store_fpcr (cpu_env, fpcr);
7419 ret = 0;
7420
7421 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
7422 /* Old exceptions are not signaled. */
7423 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
7424
7425 /* If any exceptions set by this call, and are unmasked,
7426 send a signal. */
7427 /* ??? FIXME */
7428 }
7429 }
7430 break;
7431
7432 /* case SSI_NVPAIRS:
7433 -- Used with SSIN_UACPROC to enable unaligned accesses.
7434 case SSI_IEEE_STATE_AT_SIGNAL:
7435 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
7436 -- Not implemented in linux kernel
7437 */
7438 }
7439 break;
7440#endif
7441#ifdef TARGET_NR_osf_sigprocmask
7442 /* Alpha specific. */
7443 case TARGET_NR_osf_sigprocmask:
7444 {
7445 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01007446 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08007447 sigset_t set, oldset;
7448
7449 switch(arg1) {
7450 case TARGET_SIG_BLOCK:
7451 how = SIG_BLOCK;
7452 break;
7453 case TARGET_SIG_UNBLOCK:
7454 how = SIG_UNBLOCK;
7455 break;
7456 case TARGET_SIG_SETMASK:
7457 how = SIG_SETMASK;
7458 break;
7459 default:
7460 ret = -TARGET_EINVAL;
7461 goto fail;
7462 }
7463 mask = arg2;
7464 target_to_host_old_sigset(&set, &mask);
Juan Quintelabc088ba2011-06-16 17:37:10 +01007465 sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007466 host_to_target_old_sigset(&mask, &oldset);
7467 ret = mask;
7468 }
7469 break;
7470#endif
aurel3264b4d282008-11-14 17:20:15 +00007471
bellarda315a142005-01-30 22:59:18 +00007472#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00007473 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00007474 ret = get_errno(getgid());
7475 break;
bellarda315a142005-01-30 22:59:18 +00007476#endif
7477#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00007478 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00007479 ret = get_errno(geteuid());
7480 break;
bellarda315a142005-01-30 22:59:18 +00007481#endif
7482#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00007483 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00007484 ret = get_errno(getegid());
7485 break;
bellarda315a142005-01-30 22:59:18 +00007486#endif
7487#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00007488 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00007489 ret = get_errno(setreuid(arg1, arg2));
7490 break;
bellarda315a142005-01-30 22:59:18 +00007491#endif
7492#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00007493 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00007494 ret = get_errno(setregid(arg1, arg2));
7495 break;
bellarda315a142005-01-30 22:59:18 +00007496#endif
7497#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00007498 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00007499 {
7500 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007501 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007502 gid_t *grouplist;
7503 int i;
7504
7505 grouplist = alloca(gidsetsize * sizeof(gid_t));
7506 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007507 if (gidsetsize == 0)
7508 break;
bellard99c475a2005-01-31 20:45:13 +00007509 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007510 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
7511 if (!target_grouplist) {
7512 ret = -TARGET_EFAULT;
7513 goto fail;
7514 }
balroga2155fc2008-09-20 02:12:08 +00007515 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00007516 target_grouplist[i] = tswap32(grouplist[i]);
7517 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00007518 }
7519 }
7520 break;
bellarda315a142005-01-30 22:59:18 +00007521#endif
7522#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00007523 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00007524 {
7525 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007526 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007527 gid_t *grouplist;
7528 int i;
ths3b46e622007-09-17 08:09:54 +00007529
bellard99c475a2005-01-31 20:45:13 +00007530 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007531 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
7532 if (!target_grouplist) {
7533 ret = -TARGET_EFAULT;
7534 goto fail;
7535 }
bellard99c475a2005-01-31 20:45:13 +00007536 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00007537 grouplist[i] = tswap32(target_grouplist[i]);
7538 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00007539 ret = get_errno(setgroups(gidsetsize, grouplist));
7540 }
7541 break;
bellarda315a142005-01-30 22:59:18 +00007542#endif
7543#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00007544 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00007545 ret = get_errno(fchown(arg1, arg2, arg3));
7546 break;
bellarda315a142005-01-30 22:59:18 +00007547#endif
7548#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00007549 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00007550 ret = get_errno(setresuid(arg1, arg2, arg3));
7551 break;
bellarda315a142005-01-30 22:59:18 +00007552#endif
7553#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00007554 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00007555 {
pbrook53a59602006-03-25 19:31:22 +00007556 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00007557 ret = get_errno(getresuid(&ruid, &euid, &suid));
7558 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007559 if (put_user_u32(ruid, arg1)
7560 || put_user_u32(euid, arg2)
7561 || put_user_u32(suid, arg3))
7562 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00007563 }
7564 }
7565 break;
bellarda315a142005-01-30 22:59:18 +00007566#endif
7567#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00007568 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00007569 ret = get_errno(setresgid(arg1, arg2, arg3));
7570 break;
bellarda315a142005-01-30 22:59:18 +00007571#endif
7572#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00007573 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00007574 {
pbrook53a59602006-03-25 19:31:22 +00007575 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00007576 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7577 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007578 if (put_user_u32(rgid, arg1)
7579 || put_user_u32(egid, arg2)
7580 || put_user_u32(sgid, arg3))
7581 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00007582 }
7583 }
7584 break;
bellarda315a142005-01-30 22:59:18 +00007585#endif
7586#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00007587 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00007588 if (!(p = lock_user_string(arg1)))
7589 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007590 ret = get_errno(chown(p, arg2, arg3));
7591 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007592 break;
bellarda315a142005-01-30 22:59:18 +00007593#endif
7594#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00007595 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00007596 ret = get_errno(setuid(arg1));
7597 break;
bellarda315a142005-01-30 22:59:18 +00007598#endif
7599#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00007600 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00007601 ret = get_errno(setgid(arg1));
7602 break;
bellarda315a142005-01-30 22:59:18 +00007603#endif
7604#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00007605 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00007606 ret = get_errno(setfsuid(arg1));
7607 break;
bellarda315a142005-01-30 22:59:18 +00007608#endif
7609#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00007610 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00007611 ret = get_errno(setfsgid(arg1));
7612 break;
bellarda315a142005-01-30 22:59:18 +00007613#endif
bellard67867302003-11-23 17:05:30 +00007614
bellard31e31b82003-02-18 22:55:36 +00007615 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00007616 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00007617#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00007618 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00007619 {
7620 void *a;
7621 ret = -TARGET_EFAULT;
7622 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
7623 goto efault;
7624 if (!(p = lock_user_string(arg3)))
7625 goto mincore_fail;
7626 ret = get_errno(mincore(a, arg2, p));
7627 unlock_user(p, arg3, ret);
7628 mincore_fail:
7629 unlock_user(a, arg1, 0);
7630 }
7631 break;
bellardffa65c32004-01-04 23:57:22 +00007632#endif
aurel32408321b2008-10-01 21:46:32 +00007633#ifdef TARGET_NR_arm_fadvise64_64
7634 case TARGET_NR_arm_fadvise64_64:
7635 {
7636 /*
7637 * arm_fadvise64_64 looks like fadvise64_64 but
7638 * with different argument order
7639 */
7640 abi_long temp;
7641 temp = arg3;
7642 arg3 = arg4;
7643 arg4 = temp;
7644 }
7645#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02007646#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00007647#ifdef TARGET_NR_fadvise64_64
7648 case TARGET_NR_fadvise64_64:
7649#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02007650#ifdef TARGET_NR_fadvise64
7651 case TARGET_NR_fadvise64:
7652#endif
7653#ifdef TARGET_S390X
7654 switch (arg4) {
7655 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
7656 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
7657 case 6: arg4 = POSIX_FADV_DONTNEED; break;
7658 case 7: arg4 = POSIX_FADV_NOREUSE; break;
7659 default: break;
7660 }
7661#endif
7662 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00007663 break;
7664#endif
bellardffa65c32004-01-04 23:57:22 +00007665#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00007666 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00007667 /* A straight passthrough may not be safe because qemu sometimes
7668 turns private flie-backed mappings into anonymous mappings.
7669 This will break MADV_DONTNEED.
7670 This is a hint, so ignoring and returning success is ok. */
7671 ret = get_errno(0);
7672 break;
bellardffa65c32004-01-04 23:57:22 +00007673#endif
blueswir1992f48a2007-10-14 16:27:31 +00007674#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00007675 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00007676 {
thsb1e341e2007-03-20 21:50:52 +00007677 int cmd;
bellard77e46722003-04-29 20:39:06 +00007678 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00007679 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00007680#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00007681 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00007682#endif
bellard77e46722003-04-29 20:39:06 +00007683
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007684 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00007685 if (cmd == -TARGET_EINVAL) {
7686 ret = cmd;
7687 break;
7688 }
thsb1e341e2007-03-20 21:50:52 +00007689
bellard60cd49d2003-03-16 22:53:56 +00007690 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00007691 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00007692#ifdef TARGET_ARM
7693 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007694 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7695 goto efault;
ths58134272007-03-31 18:59:32 +00007696 fl.l_type = tswap16(target_efl->l_type);
7697 fl.l_whence = tswap16(target_efl->l_whence);
7698 fl.l_start = tswap64(target_efl->l_start);
7699 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007700 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00007701 unlock_user_struct(target_efl, arg3, 0);
7702 } else
7703#endif
7704 {
bellard9ee1fa22007-11-11 15:11:19 +00007705 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7706 goto efault;
ths58134272007-03-31 18:59:32 +00007707 fl.l_type = tswap16(target_fl->l_type);
7708 fl.l_whence = tswap16(target_fl->l_whence);
7709 fl.l_start = tswap64(target_fl->l_start);
7710 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007711 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00007712 unlock_user_struct(target_fl, arg3, 0);
7713 }
thsb1e341e2007-03-20 21:50:52 +00007714 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00007715 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00007716#ifdef TARGET_ARM
7717 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007718 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
7719 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007720 target_efl->l_type = tswap16(fl.l_type);
7721 target_efl->l_whence = tswap16(fl.l_whence);
7722 target_efl->l_start = tswap64(fl.l_start);
7723 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007724 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00007725 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00007726 } else
7727#endif
7728 {
bellard9ee1fa22007-11-11 15:11:19 +00007729 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
7730 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007731 target_fl->l_type = tswap16(fl.l_type);
7732 target_fl->l_whence = tswap16(fl.l_whence);
7733 target_fl->l_start = tswap64(fl.l_start);
7734 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007735 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00007736 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00007737 }
bellard77e46722003-04-29 20:39:06 +00007738 }
7739 break;
7740
thsb1e341e2007-03-20 21:50:52 +00007741 case TARGET_F_SETLK64:
7742 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00007743#ifdef TARGET_ARM
7744 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007745 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7746 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007747 fl.l_type = tswap16(target_efl->l_type);
7748 fl.l_whence = tswap16(target_efl->l_whence);
7749 fl.l_start = tswap64(target_efl->l_start);
7750 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007751 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00007752 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00007753 } else
7754#endif
7755 {
bellard9ee1fa22007-11-11 15:11:19 +00007756 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7757 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007758 fl.l_type = tswap16(target_fl->l_type);
7759 fl.l_whence = tswap16(target_fl->l_whence);
7760 fl.l_start = tswap64(target_fl->l_start);
7761 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007762 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00007763 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00007764 }
thsb1e341e2007-03-20 21:50:52 +00007765 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00007766 break;
bellard60cd49d2003-03-16 22:53:56 +00007767 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007768 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00007769 break;
7770 }
bellard77e46722003-04-29 20:39:06 +00007771 break;
7772 }
bellard60cd49d2003-03-16 22:53:56 +00007773#endif
ths7d600c82006-12-08 01:32:58 +00007774#ifdef TARGET_NR_cacheflush
7775 case TARGET_NR_cacheflush:
7776 /* self-modifying code is handled automatically, so nothing needed */
7777 ret = 0;
7778 break;
7779#endif
bellardebc05482003-09-30 21:08:41 +00007780#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00007781 case TARGET_NR_security:
7782 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007783#endif
bellardc573ff62004-01-04 15:51:36 +00007784#ifdef TARGET_NR_getpagesize
7785 case TARGET_NR_getpagesize:
7786 ret = TARGET_PAGE_SIZE;
7787 break;
7788#endif
bellard31e31b82003-02-18 22:55:36 +00007789 case TARGET_NR_gettid:
7790 ret = get_errno(gettid());
7791 break;
thse5febef2007-04-01 18:31:35 +00007792#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00007793 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00007794#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +03007795 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +00007796 arg2 = arg3;
7797 arg3 = arg4;
7798 arg4 = arg5;
7799 }
aurel322054ac92008-10-13 21:08:07 +00007800 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
7801#else
7802 ret = get_errno(readahead(arg1, arg2, arg3));
7803#endif
7804 break;
thse5febef2007-04-01 18:31:35 +00007805#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007806#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +00007807#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00007808 case TARGET_NR_listxattr:
7809 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +00007810 {
7811 void *p, *b = 0;
7812 if (arg2) {
7813 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
7814 if (!b) {
7815 ret = -TARGET_EFAULT;
7816 break;
7817 }
7818 }
7819 p = lock_user_string(arg1);
7820 if (p) {
7821 if (num == TARGET_NR_listxattr) {
7822 ret = get_errno(listxattr(p, b, arg3));
7823 } else {
7824 ret = get_errno(llistxattr(p, b, arg3));
7825 }
7826 } else {
7827 ret = -TARGET_EFAULT;
7828 }
7829 unlock_user(p, arg1, 0);
7830 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +03007831 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +00007832 }
7833 case TARGET_NR_flistxattr:
7834 {
7835 void *b = 0;
7836 if (arg2) {
7837 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
7838 if (!b) {
7839 ret = -TARGET_EFAULT;
7840 break;
7841 }
7842 }
7843 ret = get_errno(flistxattr(arg1, b, arg3));
7844 unlock_user(b, arg2, arg3);
7845 break;
7846 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007847 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00007848 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007849 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00007850 void *p, *n, *v = 0;
7851 if (arg3) {
7852 v = lock_user(VERIFY_READ, arg3, arg4, 1);
7853 if (!v) {
7854 ret = -TARGET_EFAULT;
7855 break;
7856 }
7857 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007858 p = lock_user_string(arg1);
7859 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00007860 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00007861 if (num == TARGET_NR_setxattr) {
7862 ret = get_errno(setxattr(p, n, v, arg4, arg5));
7863 } else {
7864 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
7865 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007866 } else {
7867 ret = -TARGET_EFAULT;
7868 }
7869 unlock_user(p, arg1, 0);
7870 unlock_user(n, arg2, 0);
7871 unlock_user(v, arg3, 0);
7872 }
7873 break;
Peter Maydell30297b52011-12-14 15:37:18 +00007874 case TARGET_NR_fsetxattr:
7875 {
7876 void *n, *v = 0;
7877 if (arg3) {
7878 v = lock_user(VERIFY_READ, arg3, arg4, 1);
7879 if (!v) {
7880 ret = -TARGET_EFAULT;
7881 break;
7882 }
7883 }
7884 n = lock_user_string(arg2);
7885 if (n) {
7886 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
7887 } else {
7888 ret = -TARGET_EFAULT;
7889 }
7890 unlock_user(n, arg2, 0);
7891 unlock_user(v, arg3, 0);
7892 }
7893 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007894 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00007895 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007896 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00007897 void *p, *n, *v = 0;
7898 if (arg3) {
7899 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
7900 if (!v) {
7901 ret = -TARGET_EFAULT;
7902 break;
7903 }
7904 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007905 p = lock_user_string(arg1);
7906 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00007907 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00007908 if (num == TARGET_NR_getxattr) {
7909 ret = get_errno(getxattr(p, n, v, arg4));
7910 } else {
7911 ret = get_errno(lgetxattr(p, n, v, arg4));
7912 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007913 } else {
7914 ret = -TARGET_EFAULT;
7915 }
7916 unlock_user(p, arg1, 0);
7917 unlock_user(n, arg2, 0);
7918 unlock_user(v, arg3, arg4);
7919 }
7920 break;
Peter Maydell30297b52011-12-14 15:37:18 +00007921 case TARGET_NR_fgetxattr:
7922 {
7923 void *n, *v = 0;
7924 if (arg3) {
7925 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
7926 if (!v) {
7927 ret = -TARGET_EFAULT;
7928 break;
7929 }
7930 }
7931 n = lock_user_string(arg2);
7932 if (n) {
7933 ret = get_errno(fgetxattr(arg1, n, v, arg4));
7934 } else {
7935 ret = -TARGET_EFAULT;
7936 }
7937 unlock_user(n, arg2, 0);
7938 unlock_user(v, arg3, arg4);
7939 }
7940 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007941 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +00007942 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007943 {
7944 void *p, *n;
7945 p = lock_user_string(arg1);
7946 n = lock_user_string(arg2);
7947 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00007948 if (num == TARGET_NR_removexattr) {
7949 ret = get_errno(removexattr(p, n));
7950 } else {
7951 ret = get_errno(lremovexattr(p, n));
7952 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007953 } else {
7954 ret = -TARGET_EFAULT;
7955 }
7956 unlock_user(p, arg1, 0);
7957 unlock_user(n, arg2, 0);
7958 }
7959 break;
Peter Maydell30297b52011-12-14 15:37:18 +00007960 case TARGET_NR_fremovexattr:
7961 {
7962 void *n;
7963 n = lock_user_string(arg2);
7964 if (n) {
7965 ret = get_errno(fremovexattr(arg1, n));
7966 } else {
7967 ret = -TARGET_EFAULT;
7968 }
7969 unlock_user(n, arg2, 0);
7970 }
7971 break;
bellardebc05482003-09-30 21:08:41 +00007972#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07007973#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +00007974#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00007975 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007976#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00007977 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
7978 ret = 0;
7979 break;
edgar_iglef967792009-01-07 14:19:38 +00007980#elif defined(TARGET_CRIS)
7981 if (arg1 & 0xff)
7982 ret = -TARGET_EINVAL;
7983 else {
7984 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
7985 ret = 0;
7986 }
7987 break;
bellard8d18e892007-11-14 15:18:40 +00007988#elif defined(TARGET_I386) && defined(TARGET_ABI32)
7989 ret = do_set_thread_area(cpu_env, arg1);
7990 break;
ths6f5b89a2007-03-02 20:48:00 +00007991#else
7992 goto unimplemented_nowarn;
7993#endif
7994#endif
7995#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00007996 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007997#if defined(TARGET_I386) && defined(TARGET_ABI32)
7998 ret = do_get_thread_area(cpu_env, arg1);
7999#else
bellard5cd43932003-03-29 16:54:36 +00008000 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00008001#endif
bellard8d18e892007-11-14 15:18:40 +00008002#endif
bellard48dc41e2006-06-21 18:15:50 +00008003#ifdef TARGET_NR_getdomainname
8004 case TARGET_NR_getdomainname:
8005 goto unimplemented_nowarn;
8006#endif
ths6f5b89a2007-03-02 20:48:00 +00008007
thsb5906f92007-03-19 13:32:45 +00008008#ifdef TARGET_NR_clock_gettime
8009 case TARGET_NR_clock_gettime:
8010 {
8011 struct timespec ts;
8012 ret = get_errno(clock_gettime(arg1, &ts));
8013 if (!is_error(ret)) {
8014 host_to_target_timespec(arg2, &ts);
8015 }
8016 break;
8017 }
8018#endif
8019#ifdef TARGET_NR_clock_getres
8020 case TARGET_NR_clock_getres:
8021 {
8022 struct timespec ts;
8023 ret = get_errno(clock_getres(arg1, &ts));
8024 if (!is_error(ret)) {
8025 host_to_target_timespec(arg2, &ts);
8026 }
8027 break;
8028 }
8029#endif
pbrook63d76512008-05-29 13:43:29 +00008030#ifdef TARGET_NR_clock_nanosleep
8031 case TARGET_NR_clock_nanosleep:
8032 {
8033 struct timespec ts;
8034 target_to_host_timespec(&ts, arg3);
8035 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
8036 if (arg4)
8037 host_to_target_timespec(arg4, &ts);
8038 break;
8039 }
8040#endif
thsb5906f92007-03-19 13:32:45 +00008041
ths6f5b89a2007-03-02 20:48:00 +00008042#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8043 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00008044 ret = get_errno(set_tid_address((int *)g2h(arg1)));
8045 break;
ths6f5b89a2007-03-02 20:48:00 +00008046#endif
8047
ths3ae43202007-09-16 21:39:48 +00008048#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00008049 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00008050 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00008051 break;
8052#endif
8053
ths3ae43202007-09-16 21:39:48 +00008054#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00008055 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00008056 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
8057 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00008058 break;
8059#endif
8060
ths4f2b1fe2007-06-21 21:57:12 +00008061#ifdef TARGET_NR_set_robust_list
8062 case TARGET_NR_set_robust_list:
8063 goto unimplemented_nowarn;
8064#endif
8065
ths9007f0e2007-09-25 17:50:37 +00008066#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
8067 case TARGET_NR_utimensat:
8068 {
Riku Voipioebc996f2009-04-21 15:01:51 +03008069 struct timespec *tsp, ts[2];
8070 if (!arg3) {
8071 tsp = NULL;
8072 } else {
8073 target_to_host_timespec(ts, arg3);
8074 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
8075 tsp = ts;
8076 }
ths9007f0e2007-09-25 17:50:37 +00008077 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03008078 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00008079 else {
bellard579a97f2007-11-11 14:26:47 +00008080 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00008081 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00008082 goto fail;
8083 }
Riku Voipioebc996f2009-04-21 15:01:51 +03008084 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00008085 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00008086 }
8087 }
8088 break;
8089#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02008090#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00008091 case TARGET_NR_futex:
8092 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
8093 break;
8094#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008095#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00008096 case TARGET_NR_inotify_init:
8097 ret = get_errno(sys_inotify_init());
8098 break;
8099#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008100#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00008101#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8102 case TARGET_NR_inotify_init1:
8103 ret = get_errno(sys_inotify_init1(arg1));
8104 break;
8105#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008106#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008107#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00008108 case TARGET_NR_inotify_add_watch:
8109 p = lock_user_string(arg2);
8110 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
8111 unlock_user(p, arg2, 0);
8112 break;
8113#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008114#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00008115 case TARGET_NR_inotify_rm_watch:
8116 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
8117 break;
8118#endif
ths9007f0e2007-09-25 17:50:37 +00008119
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07008120#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00008121 case TARGET_NR_mq_open:
8122 {
8123 struct mq_attr posix_mq_attr;
8124
8125 p = lock_user_string(arg1 - 1);
8126 if (arg4 != 0)
8127 copy_from_user_mq_attr (&posix_mq_attr, arg4);
8128 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
8129 unlock_user (p, arg1, 0);
8130 }
8131 break;
8132
8133 case TARGET_NR_mq_unlink:
8134 p = lock_user_string(arg1 - 1);
8135 ret = get_errno(mq_unlink(p));
8136 unlock_user (p, arg1, 0);
8137 break;
8138
8139 case TARGET_NR_mq_timedsend:
8140 {
8141 struct timespec ts;
8142
8143 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8144 if (arg5 != 0) {
8145 target_to_host_timespec(&ts, arg5);
8146 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
8147 host_to_target_timespec(arg5, &ts);
8148 }
8149 else
8150 ret = get_errno(mq_send(arg1, p, arg3, arg4));
8151 unlock_user (p, arg2, arg3);
8152 }
8153 break;
8154
8155 case TARGET_NR_mq_timedreceive:
8156 {
8157 struct timespec ts;
8158 unsigned int prio;
8159
8160 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8161 if (arg5 != 0) {
8162 target_to_host_timespec(&ts, arg5);
8163 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
8164 host_to_target_timespec(arg5, &ts);
8165 }
8166 else
8167 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
8168 unlock_user (p, arg2, arg3);
8169 if (arg4 != 0)
8170 put_user_u32(prio, arg4);
8171 }
8172 break;
8173
8174 /* Not implemented for now... */
8175/* case TARGET_NR_mq_notify: */
8176/* break; */
8177
8178 case TARGET_NR_mq_getsetattr:
8179 {
8180 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
8181 ret = 0;
8182 if (arg3 != 0) {
8183 ret = mq_getattr(arg1, &posix_mq_attr_out);
8184 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
8185 }
8186 if (arg2 != 0) {
8187 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
8188 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
8189 }
8190
8191 }
8192 break;
8193#endif
8194
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308195#ifdef CONFIG_SPLICE
8196#ifdef TARGET_NR_tee
8197 case TARGET_NR_tee:
8198 {
8199 ret = get_errno(tee(arg1,arg2,arg3,arg4));
8200 }
8201 break;
8202#endif
8203#ifdef TARGET_NR_splice
8204 case TARGET_NR_splice:
8205 {
8206 loff_t loff_in, loff_out;
8207 loff_t *ploff_in = NULL, *ploff_out = NULL;
8208 if(arg2) {
8209 get_user_u64(loff_in, arg2);
8210 ploff_in = &loff_in;
8211 }
8212 if(arg4) {
8213 get_user_u64(loff_out, arg2);
8214 ploff_out = &loff_out;
8215 }
8216 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
8217 }
8218 break;
8219#endif
8220#ifdef TARGET_NR_vmsplice
8221 case TARGET_NR_vmsplice:
8222 {
8223 int count = arg3;
8224 struct iovec *vec;
8225
8226 vec = alloca(count * sizeof(struct iovec));
8227 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
8228 goto efault;
8229 ret = get_errno(vmsplice(arg1, vec, count, arg4));
8230 unlock_iovec(vec, arg2, count, 0);
8231 }
8232 break;
8233#endif
8234#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03008235#ifdef CONFIG_EVENTFD
8236#if defined(TARGET_NR_eventfd)
8237 case TARGET_NR_eventfd:
8238 ret = get_errno(eventfd(arg1, 0));
8239 break;
8240#endif
8241#if defined(TARGET_NR_eventfd2)
8242 case TARGET_NR_eventfd2:
8243 ret = get_errno(eventfd(arg1, arg2));
8244 break;
8245#endif
8246#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03008247#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8248 case TARGET_NR_fallocate:
8249 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
8250 break;
8251#endif
Peter Maydellc727f472011-01-06 11:05:10 +00008252#if defined(CONFIG_SYNC_FILE_RANGE)
8253#if defined(TARGET_NR_sync_file_range)
8254 case TARGET_NR_sync_file_range:
8255#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +03008256#if defined(TARGET_MIPS)
8257 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8258 target_offset64(arg5, arg6), arg7));
8259#else
Peter Maydellc727f472011-01-06 11:05:10 +00008260 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
8261 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +03008262#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +00008263#else
8264 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
8265#endif
8266 break;
8267#endif
8268#if defined(TARGET_NR_sync_file_range2)
8269 case TARGET_NR_sync_file_range2:
8270 /* This is like sync_file_range but the arguments are reordered */
8271#if TARGET_ABI_BITS == 32
8272 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8273 target_offset64(arg5, arg6), arg2));
8274#else
8275 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
8276#endif
8277 break;
8278#endif
8279#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00008280#if defined(CONFIG_EPOLL)
8281#if defined(TARGET_NR_epoll_create)
8282 case TARGET_NR_epoll_create:
8283 ret = get_errno(epoll_create(arg1));
8284 break;
8285#endif
8286#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
8287 case TARGET_NR_epoll_create1:
8288 ret = get_errno(epoll_create1(arg1));
8289 break;
8290#endif
8291#if defined(TARGET_NR_epoll_ctl)
8292 case TARGET_NR_epoll_ctl:
8293 {
8294 struct epoll_event ep;
8295 struct epoll_event *epp = 0;
8296 if (arg4) {
8297 struct target_epoll_event *target_ep;
8298 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
8299 goto efault;
8300 }
8301 ep.events = tswap32(target_ep->events);
8302 /* The epoll_data_t union is just opaque data to the kernel,
8303 * so we transfer all 64 bits across and need not worry what
8304 * actual data type it is.
8305 */
8306 ep.data.u64 = tswap64(target_ep->data.u64);
8307 unlock_user_struct(target_ep, arg4, 0);
8308 epp = &ep;
8309 }
8310 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
8311 break;
8312 }
8313#endif
8314
8315#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
8316#define IMPLEMENT_EPOLL_PWAIT
8317#endif
8318#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
8319#if defined(TARGET_NR_epoll_wait)
8320 case TARGET_NR_epoll_wait:
8321#endif
8322#if defined(IMPLEMENT_EPOLL_PWAIT)
8323 case TARGET_NR_epoll_pwait:
8324#endif
8325 {
8326 struct target_epoll_event *target_ep;
8327 struct epoll_event *ep;
8328 int epfd = arg1;
8329 int maxevents = arg3;
8330 int timeout = arg4;
8331
8332 target_ep = lock_user(VERIFY_WRITE, arg2,
8333 maxevents * sizeof(struct target_epoll_event), 1);
8334 if (!target_ep) {
8335 goto efault;
8336 }
8337
8338 ep = alloca(maxevents * sizeof(struct epoll_event));
8339
8340 switch (num) {
8341#if defined(IMPLEMENT_EPOLL_PWAIT)
8342 case TARGET_NR_epoll_pwait:
8343 {
8344 target_sigset_t *target_set;
8345 sigset_t _set, *set = &_set;
8346
8347 if (arg5) {
8348 target_set = lock_user(VERIFY_READ, arg5,
8349 sizeof(target_sigset_t), 1);
8350 if (!target_set) {
8351 unlock_user(target_ep, arg2, 0);
8352 goto efault;
8353 }
8354 target_to_host_sigset(set, target_set);
8355 unlock_user(target_set, arg5, 0);
8356 } else {
8357 set = NULL;
8358 }
8359
8360 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
8361 break;
8362 }
8363#endif
8364#if defined(TARGET_NR_epoll_wait)
8365 case TARGET_NR_epoll_wait:
8366 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
8367 break;
8368#endif
8369 default:
8370 ret = -TARGET_ENOSYS;
8371 }
8372 if (!is_error(ret)) {
8373 int i;
8374 for (i = 0; i < ret; i++) {
8375 target_ep[i].events = tswap32(ep[i].events);
8376 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
8377 }
8378 }
8379 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
8380 break;
8381 }
8382#endif
8383#endif
Peter Maydell163a05a2011-06-27 17:44:52 +01008384#ifdef TARGET_NR_prlimit64
8385 case TARGET_NR_prlimit64:
8386 {
8387 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
8388 struct target_rlimit64 *target_rnew, *target_rold;
8389 struct host_rlimit64 rnew, rold, *rnewp = 0;
8390 if (arg3) {
8391 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
8392 goto efault;
8393 }
8394 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
8395 rnew.rlim_max = tswap64(target_rnew->rlim_max);
8396 unlock_user_struct(target_rnew, arg3, 0);
8397 rnewp = &rnew;
8398 }
8399
8400 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
8401 if (!is_error(ret) && arg4) {
8402 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
8403 goto efault;
8404 }
8405 target_rold->rlim_cur = tswap64(rold.rlim_cur);
8406 target_rold->rlim_max = tswap64(rold.rlim_max);
8407 unlock_user_struct(target_rold, arg4, 1);
8408 }
8409 break;
8410 }
8411#endif
bellard31e31b82003-02-18 22:55:36 +00008412 default:
8413 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00008414 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00008415#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 +00008416 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00008417#endif
ths0da46a62007-10-20 20:23:07 +00008418 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00008419 break;
8420 }
bellard579a97f2007-11-11 14:26:47 +00008421fail:
bellardc573ff62004-01-04 15:51:36 +00008422#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00008423 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00008424#endif
thsb92c47c2007-11-01 00:07:38 +00008425 if(do_strace)
8426 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00008427 return ret;
bellard579a97f2007-11-11 14:26:47 +00008428efault:
8429 ret = -TARGET_EFAULT;
8430 goto fail;
bellard31e31b82003-02-18 22:55:36 +00008431}