blob: e7af2ea1c0704d604eadef21545a31bfbb131a35 [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>
aurel320b6d3ae2008-09-15 07:43:43 +000063#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
bellard31e31b82003-02-18 22:55:36 +000073
74#define termios host_termios
75#define winsize host_winsize
76#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000077#define sgttyb host_sgttyb /* same as target */
78#define tchars host_tchars /* same as target */
79#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000080
81#include <linux/termios.h>
82#include <linux/unistd.h>
83#include <linux/utsname.h>
84#include <linux/cdrom.h>
85#include <linux/hdreg.h>
86#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000087#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000088#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +030089#include <linux/fs.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000090#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +000091#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000092#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +020093#include <linux/fb.h>
94#include <linux/vt.h>
pbrookd7e40362008-05-23 16:06:43 +000095#include "linux_loop.h"
Loïc Minierda790302009-12-29 22:06:13 +010096#include "cpu-uname.h"
bellard31e31b82003-02-18 22:55:36 +000097
bellard3ef693a2003-03-23 20:17:16 +000098#include "qemu.h"
balrog526ccb72008-07-16 12:13:52 +000099#include "qemu-common.h"
bellard31e31b82003-02-18 22:55:36 +0000100
Juan Quintela2f7bb872009-07-27 16:13:24 +0200101#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +0000102#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
103 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
104#else
105/* XXX: Hardcode the above values. */
106#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +0000107#endif
108
bellard72f03902003-02-18 23:33:18 +0000109//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000110
bellard1a9353d2003-03-16 20:28:50 +0000111//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000112#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
113#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000114
bellard70a194b2003-08-11 22:20:16 +0000115
bellard70a194b2003-08-11 22:20:16 +0000116#undef _syscall0
117#undef _syscall1
118#undef _syscall2
119#undef _syscall3
120#undef _syscall4
121#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000122#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000123
bellard83fcb512006-06-14 13:37:16 +0000124#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000125static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000126{ \
127 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000128}
129
bellard83fcb512006-06-14 13:37:16 +0000130#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000131static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000132{ \
133 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000134}
135
bellard83fcb512006-06-14 13:37:16 +0000136#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000137static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000138{ \
139 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000140}
141
bellard83fcb512006-06-14 13:37:16 +0000142#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000143static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000144{ \
145 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000146}
147
bellard83fcb512006-06-14 13:37:16 +0000148#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000149static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000150{ \
151 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000152}
153
bellard83fcb512006-06-14 13:37:16 +0000154#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
155 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000156static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000157{ \
158 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000159}
bellard83fcb512006-06-14 13:37:16 +0000160
161
162#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
163 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000164static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
165 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000166{ \
167 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
168}
169
bellard70a194b2003-08-11 22:20:16 +0000170
bellard31e31b82003-02-18 22:55:36 +0000171#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000172#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000173#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000174#define __NR_sys_fchownat __NR_fchownat
balrog6a24a772008-09-20 02:23:36 +0000175#define __NR_sys_fstatat64 __NR_fstatat64
balrogac8a6552008-09-20 02:25:39 +0000176#define __NR_sys_futimesat __NR_futimesat
bellard72f03902003-02-18 23:33:18 +0000177#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000178#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000179#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000180#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000181#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000182#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000183#define __NR_sys_mknodat __NR_mknodat
aurel329d33b762009-04-08 23:07:05 +0000184#define __NR_sys_newfstatat __NR_newfstatat
ths82424832007-09-24 09:21:55 +0000185#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000186#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000187#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000188#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000189#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000190#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000191#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000192#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000193#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000194#define __NR_sys_utimensat __NR_utimensat
pbrookbd0c5662008-05-29 14:34:11 +0000195#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000196#define __NR_sys_inotify_init __NR_inotify_init
197#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
198#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000199
Alexander Graf42a39fb2011-04-15 17:32:45 +0200200#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
201 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000202#define __NR__llseek __NR_lseek
203#endif
204
bellard72f03902003-02-18 23:33:18 +0000205#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000206_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000207#else
ths0da46a62007-10-20 20:23:07 +0000208/* This is a replacement for the host gettid() and must return a host
209 errno. */
bellard72f03902003-02-18 23:33:18 +0000210static int gettid(void) {
211 return -ENOSYS;
212}
213#endif
aurel323b3f24a2009-04-15 16:12:13 +0000214_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
aurel323b3f24a2009-04-15 16:12:13 +0000215#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
216_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
217#endif
218_syscall2(int, sys_getpriority, int, which, int, who);
Richard Hendersond35b2612010-06-04 12:14:10 -0700219#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000220_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
221 loff_t *, res, uint, wh);
222#endif
223_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
224_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
225#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
226_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
227#endif
228#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
229_syscall2(int,sys_tkill,int,tid,int,sig)
230#endif
231#ifdef __NR_exit_group
232_syscall1(int,exit_group,int,error_code)
233#endif
234#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
235_syscall1(int,set_tid_address,int *,tidptr)
236#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +0200237#if defined(CONFIG_USE_NPTL)
aurel323b3f24a2009-04-15 16:12:13 +0000238#if defined(TARGET_NR_futex) && defined(__NR_futex)
239_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
240 const struct timespec *,timeout,int *,uaddr2,int,val3)
241#endif
242#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500243#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
244_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
245 unsigned long *, user_mask_ptr);
246#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
247_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
248 unsigned long *, user_mask_ptr);
aurel323b3f24a2009-04-15 16:12:13 +0000249
250static bitmask_transtbl fcntl_flags_tbl[] = {
251 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
252 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
253 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
254 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
255 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
256 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
257 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
258 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
259 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
260 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
261 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
262 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
263 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
264#if defined(O_DIRECT)
265 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
266#endif
267 { 0, 0, 0, 0 }
268};
269
270#define COPY_UTSNAME_FIELD(dest, src) \
271 do { \
272 /* __NEW_UTS_LEN doesn't include terminating null */ \
273 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
274 (dest)[__NEW_UTS_LEN] = '\0'; \
275 } while (0)
276
277static int sys_uname(struct new_utsname *buf)
278{
279 struct utsname uts_buf;
280
281 if (uname(&uts_buf) < 0)
282 return (-1);
283
284 /*
285 * Just in case these have some differences, we
286 * translate utsname to new_utsname (which is the
287 * struct linux kernel uses).
288 */
289
290 bzero(buf, sizeof (*buf));
291 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
292 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
293 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
294 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
295 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
296#ifdef _GNU_SOURCE
297 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
298#endif
299 return (0);
300
301#undef COPY_UTSNAME_FIELD
302}
303
304static int sys_getcwd1(char *buf, size_t size)
305{
306 if (getcwd(buf, size) == NULL) {
307 /* getcwd() sets errno */
308 return (-1);
309 }
aurel32aaf4ad32009-04-16 14:17:14 +0000310 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000311}
312
313#ifdef CONFIG_ATFILE
314/*
315 * Host system seems to have atfile syscall stubs available. We
316 * now enable them one by one as specified by target syscall_nr.h.
317 */
318
319#ifdef TARGET_NR_faccessat
aurel32465c9f02009-04-19 08:52:17 +0000320static int sys_faccessat(int dirfd, const char *pathname, int mode)
aurel323b3f24a2009-04-15 16:12:13 +0000321{
aurel32465c9f02009-04-19 08:52:17 +0000322 return (faccessat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000323}
324#endif
325#ifdef TARGET_NR_fchmodat
aurel32465c9f02009-04-19 08:52:17 +0000326static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000327{
aurel32465c9f02009-04-19 08:52:17 +0000328 return (fchmodat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000329}
330#endif
aurel32fda33742009-04-15 17:12:01 +0000331#if defined(TARGET_NR_fchownat) && defined(USE_UID16)
aurel323b3f24a2009-04-15 16:12:13 +0000332static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
333 gid_t group, int flags)
334{
335 return (fchownat(dirfd, pathname, owner, group, flags));
336}
337#endif
338#ifdef __NR_fstatat64
339static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
340 int flags)
341{
342 return (fstatat(dirfd, pathname, buf, flags));
343}
344#endif
345#ifdef __NR_newfstatat
346static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
347 int flags)
348{
349 return (fstatat(dirfd, pathname, buf, flags));
350}
351#endif
352#ifdef TARGET_NR_futimesat
353static int sys_futimesat(int dirfd, const char *pathname,
354 const struct timeval times[2])
355{
356 return (futimesat(dirfd, pathname, times));
357}
358#endif
359#ifdef TARGET_NR_linkat
360static int sys_linkat(int olddirfd, const char *oldpath,
361 int newdirfd, const char *newpath, int flags)
362{
363 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
364}
365#endif
366#ifdef TARGET_NR_mkdirat
367static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
368{
369 return (mkdirat(dirfd, pathname, mode));
370}
371#endif
372#ifdef TARGET_NR_mknodat
373static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
374 dev_t dev)
375{
376 return (mknodat(dirfd, pathname, mode, dev));
377}
378#endif
379#ifdef TARGET_NR_openat
380static int sys_openat(int dirfd, const char *pathname, int flags, ...)
381{
382 /*
383 * open(2) has extra parameter 'mode' when called with
384 * flag O_CREAT.
385 */
386 if ((flags & O_CREAT) != 0) {
387 va_list ap;
388 mode_t mode;
389
390 /*
391 * Get the 'mode' parameter and translate it to
392 * host bits.
393 */
394 va_start(ap, flags);
395 mode = va_arg(ap, mode_t);
396 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
397 va_end(ap);
398
399 return (openat(dirfd, pathname, flags, mode));
400 }
401 return (openat(dirfd, pathname, flags));
402}
403#endif
404#ifdef TARGET_NR_readlinkat
405static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
406{
407 return (readlinkat(dirfd, pathname, buf, bufsiz));
408}
409#endif
410#ifdef TARGET_NR_renameat
411static int sys_renameat(int olddirfd, const char *oldpath,
412 int newdirfd, const char *newpath)
413{
414 return (renameat(olddirfd, oldpath, newdirfd, newpath));
415}
416#endif
417#ifdef TARGET_NR_symlinkat
418static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
419{
420 return (symlinkat(oldpath, newdirfd, newpath));
421}
422#endif
423#ifdef TARGET_NR_unlinkat
424static int sys_unlinkat(int dirfd, const char *pathname, int flags)
425{
426 return (unlinkat(dirfd, pathname, flags));
427}
428#endif
aurel323b3f24a2009-04-15 16:12:13 +0000429#else /* !CONFIG_ATFILE */
430
431/*
432 * Try direct syscalls instead
433 */
ths92a34c12007-09-24 09:27:49 +0000434#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
aurel32465c9f02009-04-19 08:52:17 +0000435_syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
ths92a34c12007-09-24 09:27:49 +0000436#endif
ths814d7972007-09-24 09:26:51 +0000437#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
aurel32465c9f02009-04-19 08:52:17 +0000438_syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
ths814d7972007-09-24 09:26:51 +0000439#endif
blueswir14583f582008-08-24 10:35:55 +0000440#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
thsccfa72b2007-09-24 09:23:34 +0000441_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
442 uid_t,owner,gid_t,group,int,flags)
443#endif
aurel329d33b762009-04-08 23:07:05 +0000444#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
445 defined(__NR_fstatat64)
balrog6a24a772008-09-20 02:23:36 +0000446_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
447 struct stat *,buf,int,flags)
448#endif
balrogac8a6552008-09-20 02:25:39 +0000449#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
450_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
451 const struct timeval *,times)
452#endif
aurel323b3f24a2009-04-15 16:12:13 +0000453#if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
454 defined(__NR_newfstatat)
455_syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
456 struct stat *,buf,int,flags)
blueswir18fcd3692008-08-17 20:26:25 +0000457#endif
ths64f0ce42007-09-24 09:25:06 +0000458#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
459_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
aurel323b3f24a2009-04-15 16:12:13 +0000460 int,newdirfd,const char *,newpath,int,flags)
ths64f0ce42007-09-24 09:25:06 +0000461#endif
ths4472ad02007-09-24 09:22:32 +0000462#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
463_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
464#endif
ths75ac37a2007-09-24 09:23:05 +0000465#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
466_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
467 mode_t,mode,dev_t,dev)
468#endif
ths82424832007-09-24 09:21:55 +0000469#if defined(TARGET_NR_openat) && defined(__NR_openat)
470_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
471#endif
ths5e0ccb12007-09-24 09:26:10 +0000472#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
473_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
474 char *,buf,size_t,bufsize)
475#endif
ths722183f2007-09-24 09:24:37 +0000476#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
477_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
478 int,newdirfd,const char *,newpath)
479#endif
thsb51eaa82007-09-25 16:09:22 +0000480#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000481_syscall3(int,sys_symlinkat,const char *,oldpath,
482 int,newdirfd,const char *,newpath)
483#endif
ths8170f562007-09-24 09:24:11 +0000484#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
485_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
486#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300487
488#endif /* CONFIG_ATFILE */
489
490#ifdef CONFIG_UTIMENSAT
491static int sys_utimensat(int dirfd, const char *pathname,
492 const struct timespec times[2], int flags)
493{
494 if (pathname == NULL)
495 return futimens(dirfd, times);
496 else
497 return utimensat(dirfd, pathname, times, flags);
498}
499#else
ths9007f0e2007-09-25 17:50:37 +0000500#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
501_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
502 const struct timespec *,tsp,int,flags)
503#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300504#endif /* CONFIG_UTIMENSAT */
aurel323b3f24a2009-04-15 16:12:13 +0000505
506#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000507#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000508
aurel3239b59762008-10-01 21:46:50 +0000509#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000510static int sys_inotify_init(void)
511{
512 return (inotify_init());
513}
aurel3239b59762008-10-01 21:46:50 +0000514#endif
515#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000516static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
517{
518 return (inotify_add_watch(fd, pathname, mask));
519}
aurel3239b59762008-10-01 21:46:50 +0000520#endif
521#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000522static int sys_inotify_rm_watch(int fd, int32_t wd)
523{
aurel328690e422009-04-17 13:50:32 +0000524 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000525}
aurel3239b59762008-10-01 21:46:50 +0000526#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000527#ifdef CONFIG_INOTIFY1
528#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
529static int sys_inotify_init1(int flags)
530{
531 return (inotify_init1(flags));
532}
533#endif
534#endif
aurel323b3f24a2009-04-15 16:12:13 +0000535#else
536/* Userspace can usually survive runtime without inotify */
537#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000538#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000539#undef TARGET_NR_inotify_add_watch
540#undef TARGET_NR_inotify_rm_watch
541#endif /* CONFIG_INOTIFY */
542
Mike Frysingerd8035d42011-02-07 01:05:51 -0500543#if defined(TARGET_NR_ppoll)
544#ifndef __NR_ppoll
545# define __NR_ppoll -1
546#endif
547#define __NR_sys_ppoll __NR_ppoll
548_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
549 struct timespec *, timeout, const __sigset_t *, sigmask,
550 size_t, sigsetsize)
551#endif
bellard66fb9762003-03-23 01:06:05 +0000552
553extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000554extern int flock(int, int);
555extern int setfsuid(int);
556extern int setfsgid(int);
bellard19b84f32003-05-08 15:41:49 +0000557extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000558
thsb92c47c2007-11-01 00:07:38 +0000559#define ERRNO_TABLE_SIZE 1200
560
561/* target_to_host_errno_table[] is initialized from
562 * host_to_target_errno_table[] in syscall_init(). */
563static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
564};
565
ths637947f2007-06-01 12:09:19 +0000566/*
thsfe8f0962007-07-12 10:59:21 +0000567 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000568 * minus the errnos that are not actually generic to all archs.
569 */
thsb92c47c2007-11-01 00:07:38 +0000570static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000571 [EIDRM] = TARGET_EIDRM,
572 [ECHRNG] = TARGET_ECHRNG,
573 [EL2NSYNC] = TARGET_EL2NSYNC,
574 [EL3HLT] = TARGET_EL3HLT,
575 [EL3RST] = TARGET_EL3RST,
576 [ELNRNG] = TARGET_ELNRNG,
577 [EUNATCH] = TARGET_EUNATCH,
578 [ENOCSI] = TARGET_ENOCSI,
579 [EL2HLT] = TARGET_EL2HLT,
580 [EDEADLK] = TARGET_EDEADLK,
581 [ENOLCK] = TARGET_ENOLCK,
582 [EBADE] = TARGET_EBADE,
583 [EBADR] = TARGET_EBADR,
584 [EXFULL] = TARGET_EXFULL,
585 [ENOANO] = TARGET_ENOANO,
586 [EBADRQC] = TARGET_EBADRQC,
587 [EBADSLT] = TARGET_EBADSLT,
588 [EBFONT] = TARGET_EBFONT,
589 [ENOSTR] = TARGET_ENOSTR,
590 [ENODATA] = TARGET_ENODATA,
591 [ETIME] = TARGET_ETIME,
592 [ENOSR] = TARGET_ENOSR,
593 [ENONET] = TARGET_ENONET,
594 [ENOPKG] = TARGET_ENOPKG,
595 [EREMOTE] = TARGET_EREMOTE,
596 [ENOLINK] = TARGET_ENOLINK,
597 [EADV] = TARGET_EADV,
598 [ESRMNT] = TARGET_ESRMNT,
599 [ECOMM] = TARGET_ECOMM,
600 [EPROTO] = TARGET_EPROTO,
601 [EDOTDOT] = TARGET_EDOTDOT,
602 [EMULTIHOP] = TARGET_EMULTIHOP,
603 [EBADMSG] = TARGET_EBADMSG,
604 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
605 [EOVERFLOW] = TARGET_EOVERFLOW,
606 [ENOTUNIQ] = TARGET_ENOTUNIQ,
607 [EBADFD] = TARGET_EBADFD,
608 [EREMCHG] = TARGET_EREMCHG,
609 [ELIBACC] = TARGET_ELIBACC,
610 [ELIBBAD] = TARGET_ELIBBAD,
611 [ELIBSCN] = TARGET_ELIBSCN,
612 [ELIBMAX] = TARGET_ELIBMAX,
613 [ELIBEXEC] = TARGET_ELIBEXEC,
614 [EILSEQ] = TARGET_EILSEQ,
615 [ENOSYS] = TARGET_ENOSYS,
616 [ELOOP] = TARGET_ELOOP,
617 [ERESTART] = TARGET_ERESTART,
618 [ESTRPIPE] = TARGET_ESTRPIPE,
619 [ENOTEMPTY] = TARGET_ENOTEMPTY,
620 [EUSERS] = TARGET_EUSERS,
621 [ENOTSOCK] = TARGET_ENOTSOCK,
622 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
623 [EMSGSIZE] = TARGET_EMSGSIZE,
624 [EPROTOTYPE] = TARGET_EPROTOTYPE,
625 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
626 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
627 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
628 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
629 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
630 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
631 [EADDRINUSE] = TARGET_EADDRINUSE,
632 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
633 [ENETDOWN] = TARGET_ENETDOWN,
634 [ENETUNREACH] = TARGET_ENETUNREACH,
635 [ENETRESET] = TARGET_ENETRESET,
636 [ECONNABORTED] = TARGET_ECONNABORTED,
637 [ECONNRESET] = TARGET_ECONNRESET,
638 [ENOBUFS] = TARGET_ENOBUFS,
639 [EISCONN] = TARGET_EISCONN,
640 [ENOTCONN] = TARGET_ENOTCONN,
641 [EUCLEAN] = TARGET_EUCLEAN,
642 [ENOTNAM] = TARGET_ENOTNAM,
643 [ENAVAIL] = TARGET_ENAVAIL,
644 [EISNAM] = TARGET_EISNAM,
645 [EREMOTEIO] = TARGET_EREMOTEIO,
646 [ESHUTDOWN] = TARGET_ESHUTDOWN,
647 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
648 [ETIMEDOUT] = TARGET_ETIMEDOUT,
649 [ECONNREFUSED] = TARGET_ECONNREFUSED,
650 [EHOSTDOWN] = TARGET_EHOSTDOWN,
651 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
652 [EALREADY] = TARGET_EALREADY,
653 [EINPROGRESS] = TARGET_EINPROGRESS,
654 [ESTALE] = TARGET_ESTALE,
655 [ECANCELED] = TARGET_ECANCELED,
656 [ENOMEDIUM] = TARGET_ENOMEDIUM,
657 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000658#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000659 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000660#endif
661#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000662 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000663#endif
664#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000665 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000666#endif
667#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000668 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000669#endif
670#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000671 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000672#endif
673#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000674 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000675#endif
thsb92c47c2007-11-01 00:07:38 +0000676};
ths637947f2007-06-01 12:09:19 +0000677
678static inline int host_to_target_errno(int err)
679{
680 if(host_to_target_errno_table[err])
681 return host_to_target_errno_table[err];
682 return err;
683}
684
thsb92c47c2007-11-01 00:07:38 +0000685static inline int target_to_host_errno(int err)
686{
687 if (target_to_host_errno_table[err])
688 return target_to_host_errno_table[err];
689 return err;
690}
691
blueswir1992f48a2007-10-14 16:27:31 +0000692static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000693{
694 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000695 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000696 else
697 return ret;
698}
699
blueswir1992f48a2007-10-14 16:27:31 +0000700static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000701{
blueswir1992f48a2007-10-14 16:27:31 +0000702 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000703}
704
thsb92c47c2007-11-01 00:07:38 +0000705char *target_strerror(int err)
706{
707 return strerror(target_to_host_errno(err));
708}
709
blueswir1992f48a2007-10-14 16:27:31 +0000710static abi_ulong target_brk;
711static abi_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000712
blueswir1992f48a2007-10-14 16:27:31 +0000713void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000714{
blueswir14c1de732007-07-07 20:45:44 +0000715 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000716}
717
ths0da46a62007-10-20 20:23:07 +0000718/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000719abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000720{
blueswir1992f48a2007-10-14 16:27:31 +0000721 abi_ulong brk_page;
722 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000723 int new_alloc_size;
724
725 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000726 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000727 if (new_brk < target_original_brk)
balrog7ab240a2008-04-26 12:17:34 +0000728 return target_brk;
ths3b46e622007-09-17 08:09:54 +0000729
pbrook53a59602006-03-25 19:31:22 +0000730 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000731
732 /* If the new brk is less than this, set it and we're done... */
733 if (new_brk < brk_page) {
734 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000735 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000736 }
737
738 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000739 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000740 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000741 PROT_READ|PROT_WRITE,
742 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
balrog7ab240a2008-04-26 12:17:34 +0000743
Richard Henderson7dd46c02010-05-03 10:07:49 -0700744#if defined(TARGET_ALPHA)
745 /* We (partially) emulate OSF/1 on Alpha, which requires we
746 return a proper errno, not an unchanged brk value. */
747 if (is_error(mapped_addr)) {
748 return -TARGET_ENOMEM;
749 }
750#endif
751
752 if (!is_error(mapped_addr)) {
bellard31e31b82003-02-18 22:55:36 +0000753 target_brk = new_brk;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700754 }
balrog7ab240a2008-04-26 12:17:34 +0000755 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000756}
757
ths26edcf42007-12-09 02:25:24 +0000758static inline abi_long copy_from_user_fdset(fd_set *fds,
759 abi_ulong target_fds_addr,
760 int n)
bellard31e31b82003-02-18 22:55:36 +0000761{
ths26edcf42007-12-09 02:25:24 +0000762 int i, nw, j, k;
763 abi_ulong b, *target_fds;
764
765 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
766 if (!(target_fds = lock_user(VERIFY_READ,
767 target_fds_addr,
768 sizeof(abi_ulong) * nw,
769 1)))
770 return -TARGET_EFAULT;
771
772 FD_ZERO(fds);
773 k = 0;
774 for (i = 0; i < nw; i++) {
775 /* grab the abi_ulong */
776 __get_user(b, &target_fds[i]);
777 for (j = 0; j < TARGET_ABI_BITS; j++) {
778 /* check the bit inside the abi_ulong */
779 if ((b >> j) & 1)
780 FD_SET(k, fds);
781 k++;
bellard31e31b82003-02-18 22:55:36 +0000782 }
bellard31e31b82003-02-18 22:55:36 +0000783 }
ths26edcf42007-12-09 02:25:24 +0000784
785 unlock_user(target_fds, target_fds_addr, 0);
786
787 return 0;
bellard31e31b82003-02-18 22:55:36 +0000788}
789
ths26edcf42007-12-09 02:25:24 +0000790static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
791 const fd_set *fds,
792 int n)
bellard31e31b82003-02-18 22:55:36 +0000793{
bellard31e31b82003-02-18 22:55:36 +0000794 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000795 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000796 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000797
ths26edcf42007-12-09 02:25:24 +0000798 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
799 if (!(target_fds = lock_user(VERIFY_WRITE,
800 target_fds_addr,
801 sizeof(abi_ulong) * nw,
802 0)))
803 return -TARGET_EFAULT;
804
805 k = 0;
806 for (i = 0; i < nw; i++) {
807 v = 0;
808 for (j = 0; j < TARGET_ABI_BITS; j++) {
809 v |= ((FD_ISSET(k, fds) != 0) << j);
810 k++;
bellard31e31b82003-02-18 22:55:36 +0000811 }
ths26edcf42007-12-09 02:25:24 +0000812 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000813 }
ths26edcf42007-12-09 02:25:24 +0000814
815 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
816
817 return 0;
bellard31e31b82003-02-18 22:55:36 +0000818}
819
bellardc596ed12003-07-13 17:32:31 +0000820#if defined(__alpha__)
821#define HOST_HZ 1024
822#else
823#define HOST_HZ 100
824#endif
825
blueswir1992f48a2007-10-14 16:27:31 +0000826static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000827{
828#if HOST_HZ == TARGET_HZ
829 return ticks;
830#else
831 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
832#endif
833}
834
bellard579a97f2007-11-11 14:26:47 +0000835static inline abi_long host_to_target_rusage(abi_ulong target_addr,
836 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000837{
pbrook53a59602006-03-25 19:31:22 +0000838 struct target_rusage *target_rusage;
839
bellard579a97f2007-11-11 14:26:47 +0000840 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
841 return -TARGET_EFAULT;
bellardb4091862003-05-16 15:39:34 +0000842 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
843 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
844 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
845 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
846 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
847 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
848 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
849 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
850 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
851 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
852 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
853 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
854 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
855 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
856 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
857 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
858 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
859 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000860 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000861
862 return 0;
bellardb4091862003-05-16 15:39:34 +0000863}
864
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900865static inline rlim_t target_to_host_rlim(target_ulong target_rlim)
866{
867 if (target_rlim == TARGET_RLIM_INFINITY)
868 return RLIM_INFINITY;
869 else
870 return tswapl(target_rlim);
871}
872
873static inline target_ulong host_to_target_rlim(rlim_t rlim)
874{
875 if (rlim == RLIM_INFINITY || rlim != (target_long)rlim)
876 return TARGET_RLIM_INFINITY;
877 else
878 return tswapl(rlim);
879}
880
ths788f5ec2007-12-09 02:37:05 +0000881static inline abi_long copy_from_user_timeval(struct timeval *tv,
882 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000883{
pbrook53a59602006-03-25 19:31:22 +0000884 struct target_timeval *target_tv;
885
ths788f5ec2007-12-09 02:37:05 +0000886 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000887 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000888
889 __get_user(tv->tv_sec, &target_tv->tv_sec);
890 __get_user(tv->tv_usec, &target_tv->tv_usec);
891
892 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000893
894 return 0;
bellard31e31b82003-02-18 22:55:36 +0000895}
896
ths788f5ec2007-12-09 02:37:05 +0000897static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
898 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000899{
pbrook53a59602006-03-25 19:31:22 +0000900 struct target_timeval *target_tv;
901
ths788f5ec2007-12-09 02:37:05 +0000902 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000903 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000904
905 __put_user(tv->tv_sec, &target_tv->tv_sec);
906 __put_user(tv->tv_usec, &target_tv->tv_usec);
907
908 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000909
910 return 0;
bellard31e31b82003-02-18 22:55:36 +0000911}
912
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700913#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
914#include <mqueue.h>
915
aurel3224e10032009-04-15 16:11:43 +0000916static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
917 abi_ulong target_mq_attr_addr)
918{
919 struct target_mq_attr *target_mq_attr;
920
921 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
922 target_mq_attr_addr, 1))
923 return -TARGET_EFAULT;
924
925 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
926 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
927 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
928 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
929
930 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
931
932 return 0;
933}
934
935static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
936 const struct mq_attr *attr)
937{
938 struct target_mq_attr *target_mq_attr;
939
940 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
941 target_mq_attr_addr, 0))
942 return -TARGET_EFAULT;
943
944 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
945 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
946 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
947 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
948
949 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
950
951 return 0;
952}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700953#endif
bellard31e31b82003-02-18 22:55:36 +0000954
ths0da46a62007-10-20 20:23:07 +0000955/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000956static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000957 abi_ulong rfd_addr, abi_ulong wfd_addr,
958 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000959{
960 fd_set rfds, wfds, efds;
961 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
962 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000963 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000964
ths26edcf42007-12-09 02:25:24 +0000965 if (rfd_addr) {
966 if (copy_from_user_fdset(&rfds, rfd_addr, n))
967 return -TARGET_EFAULT;
968 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000969 } else {
pbrook53a59602006-03-25 19:31:22 +0000970 rfds_ptr = NULL;
971 }
ths26edcf42007-12-09 02:25:24 +0000972 if (wfd_addr) {
973 if (copy_from_user_fdset(&wfds, wfd_addr, n))
974 return -TARGET_EFAULT;
975 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000976 } else {
pbrook53a59602006-03-25 19:31:22 +0000977 wfds_ptr = NULL;
978 }
ths26edcf42007-12-09 02:25:24 +0000979 if (efd_addr) {
980 if (copy_from_user_fdset(&efds, efd_addr, n))
981 return -TARGET_EFAULT;
982 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000983 } else {
pbrook53a59602006-03-25 19:31:22 +0000984 efds_ptr = NULL;
985 }
ths3b46e622007-09-17 08:09:54 +0000986
ths26edcf42007-12-09 02:25:24 +0000987 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000988 if (copy_from_user_timeval(&tv, target_tv_addr))
989 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000990 tv_ptr = &tv;
991 } else {
992 tv_ptr = NULL;
993 }
ths26edcf42007-12-09 02:25:24 +0000994
bellard31e31b82003-02-18 22:55:36 +0000995 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000996
ths26edcf42007-12-09 02:25:24 +0000997 if (!is_error(ret)) {
998 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
999 return -TARGET_EFAULT;
1000 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1001 return -TARGET_EFAULT;
1002 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1003 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001004
ths788f5ec2007-12-09 02:37:05 +00001005 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1006 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001007 }
bellard579a97f2007-11-11 14:26:47 +00001008
bellard31e31b82003-02-18 22:55:36 +00001009 return ret;
1010}
1011
Riku Voipio099d6b02009-05-05 12:10:04 +03001012static abi_long do_pipe2(int host_pipe[], int flags)
1013{
1014#ifdef CONFIG_PIPE2
1015 return pipe2(host_pipe, flags);
1016#else
1017 return -ENOSYS;
1018#endif
1019}
1020
Richard Hendersonfb41a662010-05-03 10:07:52 -07001021static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1022 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001023{
1024 int host_pipe[2];
1025 abi_long ret;
1026 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1027
1028 if (is_error(ret))
1029 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001030
1031 /* Several targets have special calling conventions for the original
1032 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1033 if (!is_pipe2) {
1034#if defined(TARGET_ALPHA)
1035 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1036 return host_pipe[0];
1037#elif defined(TARGET_MIPS)
1038 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1039 return host_pipe[0];
1040#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001041 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001042 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001043#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001044 }
1045
Riku Voipio099d6b02009-05-05 12:10:04 +03001046 if (put_user_s32(host_pipe[0], pipedes)
1047 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1048 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001049 return get_errno(ret);
1050}
1051
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001052static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1053 abi_ulong target_addr,
1054 socklen_t len)
1055{
1056 struct target_ip_mreqn *target_smreqn;
1057
1058 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1059 if (!target_smreqn)
1060 return -TARGET_EFAULT;
1061 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1062 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1063 if (len == sizeof(struct target_ip_mreqn))
1064 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
1065 unlock_user(target_smreqn, target_addr, 0);
1066
1067 return 0;
1068}
1069
bellard579a97f2007-11-11 14:26:47 +00001070static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1071 abi_ulong target_addr,
1072 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001073{
aurel32607175e2009-04-15 16:11:59 +00001074 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1075 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001076 struct target_sockaddr *target_saddr;
1077
bellard579a97f2007-11-11 14:26:47 +00001078 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1079 if (!target_saddr)
1080 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001081
1082 sa_family = tswap16(target_saddr->sa_family);
1083
1084 /* Oops. The caller might send a incomplete sun_path; sun_path
1085 * must be terminated by \0 (see the manual page), but
1086 * unfortunately it is quite common to specify sockaddr_un
1087 * length as "strlen(x->sun_path)" while it should be
1088 * "strlen(...) + 1". We'll fix that here if needed.
1089 * Linux kernel has a similar feature.
1090 */
1091
1092 if (sa_family == AF_UNIX) {
1093 if (len < unix_maxlen && len > 0) {
1094 char *cp = (char*)target_saddr;
1095
1096 if ( cp[len-1] && !cp[len] )
1097 len++;
1098 }
1099 if (len > unix_maxlen)
1100 len = unix_maxlen;
1101 }
1102
pbrook53a59602006-03-25 19:31:22 +00001103 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001104 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001105 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001106
1107 return 0;
bellard7854b052003-03-29 17:22:23 +00001108}
1109
bellard579a97f2007-11-11 14:26:47 +00001110static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1111 struct sockaddr *addr,
1112 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001113{
pbrook53a59602006-03-25 19:31:22 +00001114 struct target_sockaddr *target_saddr;
1115
bellard579a97f2007-11-11 14:26:47 +00001116 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1117 if (!target_saddr)
1118 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001119 memcpy(target_saddr, addr, len);
1120 target_saddr->sa_family = tswap16(addr->sa_family);
1121 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001122
1123 return 0;
bellard7854b052003-03-29 17:22:23 +00001124}
1125
pbrook53a59602006-03-25 19:31:22 +00001126/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001127static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1128 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001129{
1130 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001131 abi_long msg_controllen;
1132 abi_ulong target_cmsg_addr;
1133 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001134 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001135
1136 msg_controllen = tswapl(target_msgh->msg_controllen);
1137 if (msg_controllen < sizeof (struct target_cmsghdr))
1138 goto the_end;
1139 target_cmsg_addr = tswapl(target_msgh->msg_control);
1140 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1141 if (!target_cmsg)
1142 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001143
1144 while (cmsg && target_cmsg) {
1145 void *data = CMSG_DATA(cmsg);
1146 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1147
ths5fafdf22007-09-16 21:08:06 +00001148 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001149 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1150
1151 space += CMSG_SPACE(len);
1152 if (space > msgh->msg_controllen) {
1153 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001154 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001155 break;
1156 }
1157
1158 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1159 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1160 cmsg->cmsg_len = CMSG_LEN(len);
1161
bellard3532fa72006-06-24 15:06:03 +00001162 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001163 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1164 memcpy(data, target_data, len);
1165 } else {
1166 int *fd = (int *)data;
1167 int *target_fd = (int *)target_data;
1168 int i, numfds = len / sizeof(int);
1169
1170 for (i = 0; i < numfds; i++)
1171 fd[i] = tswap32(target_fd[i]);
1172 }
1173
1174 cmsg = CMSG_NXTHDR(msgh, cmsg);
1175 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1176 }
bellard5a4a8982007-11-11 17:39:18 +00001177 unlock_user(target_cmsg, target_cmsg_addr, 0);
1178 the_end:
bellard7854b052003-03-29 17:22:23 +00001179 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001180 return 0;
bellard7854b052003-03-29 17:22:23 +00001181}
1182
pbrook53a59602006-03-25 19:31:22 +00001183/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001184static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1185 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001186{
1187 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001188 abi_long msg_controllen;
1189 abi_ulong target_cmsg_addr;
1190 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001191 socklen_t space = 0;
1192
bellard5a4a8982007-11-11 17:39:18 +00001193 msg_controllen = tswapl(target_msgh->msg_controllen);
1194 if (msg_controllen < sizeof (struct target_cmsghdr))
1195 goto the_end;
1196 target_cmsg_addr = tswapl(target_msgh->msg_control);
1197 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1198 if (!target_cmsg)
1199 return -TARGET_EFAULT;
1200
bellard7854b052003-03-29 17:22:23 +00001201 while (cmsg && target_cmsg) {
1202 void *data = CMSG_DATA(cmsg);
1203 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1204
1205 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1206
1207 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001208 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001209 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001210 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001211 break;
1212 }
1213
1214 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1215 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1216 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1217
bellard3532fa72006-06-24 15:06:03 +00001218 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001219 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1220 memcpy(target_data, data, len);
1221 } else {
1222 int *fd = (int *)data;
1223 int *target_fd = (int *)target_data;
1224 int i, numfds = len / sizeof(int);
1225
1226 for (i = 0; i < numfds; i++)
1227 target_fd[i] = tswap32(fd[i]);
1228 }
1229
1230 cmsg = CMSG_NXTHDR(msgh, cmsg);
1231 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1232 }
bellard5a4a8982007-11-11 17:39:18 +00001233 unlock_user(target_cmsg, target_cmsg_addr, space);
1234 the_end:
1235 target_msgh->msg_controllen = tswapl(space);
1236 return 0;
bellard7854b052003-03-29 17:22:23 +00001237}
1238
ths0da46a62007-10-20 20:23:07 +00001239/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001240static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001241 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001242{
blueswir1992f48a2007-10-14 16:27:31 +00001243 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001244 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001245 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001246 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001247
bellard8853f862004-02-22 14:57:26 +00001248 switch(level) {
1249 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001250 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001251 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001252 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001253
bellard2f619692007-11-16 10:46:05 +00001254 if (get_user_u32(val, optval_addr))
1255 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001256 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1257 break;
1258 case SOL_IP:
1259 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001260 case IP_TOS:
1261 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001262 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001263 case IP_ROUTER_ALERT:
1264 case IP_RECVOPTS:
1265 case IP_RETOPTS:
1266 case IP_PKTINFO:
1267 case IP_MTU_DISCOVER:
1268 case IP_RECVERR:
1269 case IP_RECVTOS:
1270#ifdef IP_FREEBIND
1271 case IP_FREEBIND:
1272#endif
1273 case IP_MULTICAST_TTL:
1274 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001275 val = 0;
1276 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001277 if (get_user_u32(val, optval_addr))
1278 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001279 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001280 if (get_user_u8(val, optval_addr))
1281 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001282 }
1283 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1284 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001285 case IP_ADD_MEMBERSHIP:
1286 case IP_DROP_MEMBERSHIP:
1287 if (optlen < sizeof (struct target_ip_mreq) ||
1288 optlen > sizeof (struct target_ip_mreqn))
1289 return -TARGET_EINVAL;
1290
1291 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1292 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1293 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1294 break;
1295
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001296 case IP_BLOCK_SOURCE:
1297 case IP_UNBLOCK_SOURCE:
1298 case IP_ADD_SOURCE_MEMBERSHIP:
1299 case IP_DROP_SOURCE_MEMBERSHIP:
1300 if (optlen != sizeof (struct target_ip_mreq_source))
1301 return -TARGET_EINVAL;
1302
1303 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1304 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1305 unlock_user (ip_mreq_source, optval_addr, 0);
1306 break;
1307
bellard8853f862004-02-22 14:57:26 +00001308 default:
1309 goto unimplemented;
1310 }
1311 break;
bellard3532fa72006-06-24 15:06:03 +00001312 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001313 switch (optname) {
1314 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001315 case TARGET_SO_DEBUG:
1316 optname = SO_DEBUG;
1317 break;
1318 case TARGET_SO_REUSEADDR:
1319 optname = SO_REUSEADDR;
1320 break;
1321 case TARGET_SO_TYPE:
1322 optname = SO_TYPE;
1323 break;
1324 case TARGET_SO_ERROR:
1325 optname = SO_ERROR;
1326 break;
1327 case TARGET_SO_DONTROUTE:
1328 optname = SO_DONTROUTE;
1329 break;
1330 case TARGET_SO_BROADCAST:
1331 optname = SO_BROADCAST;
1332 break;
1333 case TARGET_SO_SNDBUF:
1334 optname = SO_SNDBUF;
1335 break;
1336 case TARGET_SO_RCVBUF:
1337 optname = SO_RCVBUF;
1338 break;
1339 case TARGET_SO_KEEPALIVE:
1340 optname = SO_KEEPALIVE;
1341 break;
1342 case TARGET_SO_OOBINLINE:
1343 optname = SO_OOBINLINE;
1344 break;
1345 case TARGET_SO_NO_CHECK:
1346 optname = SO_NO_CHECK;
1347 break;
1348 case TARGET_SO_PRIORITY:
1349 optname = SO_PRIORITY;
1350 break;
bellard5e83e8e2005-03-01 22:32:06 +00001351#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001352 case TARGET_SO_BSDCOMPAT:
1353 optname = SO_BSDCOMPAT;
1354 break;
bellard5e83e8e2005-03-01 22:32:06 +00001355#endif
bellard3532fa72006-06-24 15:06:03 +00001356 case TARGET_SO_PASSCRED:
1357 optname = SO_PASSCRED;
1358 break;
1359 case TARGET_SO_TIMESTAMP:
1360 optname = SO_TIMESTAMP;
1361 break;
1362 case TARGET_SO_RCVLOWAT:
1363 optname = SO_RCVLOWAT;
1364 break;
1365 case TARGET_SO_RCVTIMEO:
1366 optname = SO_RCVTIMEO;
1367 break;
1368 case TARGET_SO_SNDTIMEO:
1369 optname = SO_SNDTIMEO;
1370 break;
bellard8853f862004-02-22 14:57:26 +00001371 break;
1372 default:
1373 goto unimplemented;
1374 }
bellard3532fa72006-06-24 15:06:03 +00001375 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001376 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001377
bellard2f619692007-11-16 10:46:05 +00001378 if (get_user_u32(val, optval_addr))
1379 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001380 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001381 break;
bellard7854b052003-03-29 17:22:23 +00001382 default:
bellard8853f862004-02-22 14:57:26 +00001383 unimplemented:
1384 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001385 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001386 }
bellard8853f862004-02-22 14:57:26 +00001387 return ret;
bellard7854b052003-03-29 17:22:23 +00001388}
1389
ths0da46a62007-10-20 20:23:07 +00001390/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001391static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001392 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001393{
blueswir1992f48a2007-10-14 16:27:31 +00001394 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001395 int len, val;
1396 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001397
1398 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001399 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001400 level = SOL_SOCKET;
1401 switch (optname) {
1402 /* These don't just return a single integer */
1403 case TARGET_SO_LINGER:
1404 case TARGET_SO_RCVTIMEO:
1405 case TARGET_SO_SNDTIMEO:
1406 case TARGET_SO_PEERCRED:
1407 case TARGET_SO_PEERNAME:
1408 goto unimplemented;
1409 /* Options with 'int' argument. */
1410 case TARGET_SO_DEBUG:
1411 optname = SO_DEBUG;
1412 goto int_case;
1413 case TARGET_SO_REUSEADDR:
1414 optname = SO_REUSEADDR;
1415 goto int_case;
1416 case TARGET_SO_TYPE:
1417 optname = SO_TYPE;
1418 goto int_case;
1419 case TARGET_SO_ERROR:
1420 optname = SO_ERROR;
1421 goto int_case;
1422 case TARGET_SO_DONTROUTE:
1423 optname = SO_DONTROUTE;
1424 goto int_case;
1425 case TARGET_SO_BROADCAST:
1426 optname = SO_BROADCAST;
1427 goto int_case;
1428 case TARGET_SO_SNDBUF:
1429 optname = SO_SNDBUF;
1430 goto int_case;
1431 case TARGET_SO_RCVBUF:
1432 optname = SO_RCVBUF;
1433 goto int_case;
1434 case TARGET_SO_KEEPALIVE:
1435 optname = SO_KEEPALIVE;
1436 goto int_case;
1437 case TARGET_SO_OOBINLINE:
1438 optname = SO_OOBINLINE;
1439 goto int_case;
1440 case TARGET_SO_NO_CHECK:
1441 optname = SO_NO_CHECK;
1442 goto int_case;
1443 case TARGET_SO_PRIORITY:
1444 optname = SO_PRIORITY;
1445 goto int_case;
1446#ifdef SO_BSDCOMPAT
1447 case TARGET_SO_BSDCOMPAT:
1448 optname = SO_BSDCOMPAT;
1449 goto int_case;
1450#endif
1451 case TARGET_SO_PASSCRED:
1452 optname = SO_PASSCRED;
1453 goto int_case;
1454 case TARGET_SO_TIMESTAMP:
1455 optname = SO_TIMESTAMP;
1456 goto int_case;
1457 case TARGET_SO_RCVLOWAT:
1458 optname = SO_RCVLOWAT;
1459 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001460 default:
bellard2efbe912005-07-23 15:10:20 +00001461 goto int_case;
1462 }
1463 break;
1464 case SOL_TCP:
1465 /* TCP options all take an 'int' value. */
1466 int_case:
bellard2f619692007-11-16 10:46:05 +00001467 if (get_user_u32(len, optlen))
1468 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001469 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001470 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001471 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00001472 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1473 if (ret < 0)
1474 return ret;
bellard2efbe912005-07-23 15:10:20 +00001475 if (len > lv)
1476 len = lv;
bellard2f619692007-11-16 10:46:05 +00001477 if (len == 4) {
1478 if (put_user_u32(val, optval_addr))
1479 return -TARGET_EFAULT;
1480 } else {
1481 if (put_user_u8(val, optval_addr))
1482 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001483 }
bellard2f619692007-11-16 10:46:05 +00001484 if (put_user_u32(len, optlen))
1485 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001486 break;
1487 case SOL_IP:
1488 switch(optname) {
1489 case IP_TOS:
1490 case IP_TTL:
1491 case IP_HDRINCL:
1492 case IP_ROUTER_ALERT:
1493 case IP_RECVOPTS:
1494 case IP_RETOPTS:
1495 case IP_PKTINFO:
1496 case IP_MTU_DISCOVER:
1497 case IP_RECVERR:
1498 case IP_RECVTOS:
1499#ifdef IP_FREEBIND
1500 case IP_FREEBIND:
1501#endif
1502 case IP_MULTICAST_TTL:
1503 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001504 if (get_user_u32(len, optlen))
1505 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001506 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001507 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001508 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001509 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1510 if (ret < 0)
1511 return ret;
bellard2efbe912005-07-23 15:10:20 +00001512 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001513 len = 1;
bellard2f619692007-11-16 10:46:05 +00001514 if (put_user_u32(len, optlen)
1515 || put_user_u8(val, optval_addr))
1516 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001517 } else {
bellard2efbe912005-07-23 15:10:20 +00001518 if (len > sizeof(int))
1519 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001520 if (put_user_u32(len, optlen)
1521 || put_user_u32(val, optval_addr))
1522 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001523 }
bellard8853f862004-02-22 14:57:26 +00001524 break;
bellard2efbe912005-07-23 15:10:20 +00001525 default:
thsc02f4992007-12-18 02:39:59 +00001526 ret = -TARGET_ENOPROTOOPT;
1527 break;
bellard8853f862004-02-22 14:57:26 +00001528 }
1529 break;
1530 default:
1531 unimplemented:
1532 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1533 level, optname);
thsc02f4992007-12-18 02:39:59 +00001534 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001535 break;
1536 }
1537 return ret;
bellard7854b052003-03-29 17:22:23 +00001538}
1539
bellard579a97f2007-11-11 14:26:47 +00001540/* FIXME
1541 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1542 * other lock functions have a return code of 0 for failure.
1543 */
1544static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1545 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001546{
1547 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001548 abi_ulong base;
balrogd732dcb2008-10-28 10:21:03 +00001549 int i;
pbrook53a59602006-03-25 19:31:22 +00001550
bellard579a97f2007-11-11 14:26:47 +00001551 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1552 if (!target_vec)
1553 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001554 for(i = 0;i < count; i++) {
1555 base = tswapl(target_vec[i].iov_base);
1556 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001557 if (vec[i].iov_len != 0) {
1558 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrogd732dcb2008-10-28 10:21:03 +00001559 /* Don't check lock_user return value. We must call writev even
1560 if a element has invalid base address. */
bellard41df8412008-02-04 22:26:57 +00001561 } else {
1562 /* zero length pointer is ignored */
1563 vec[i].iov_base = NULL;
1564 }
pbrook53a59602006-03-25 19:31:22 +00001565 }
1566 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001567 return 0;
pbrook53a59602006-03-25 19:31:22 +00001568}
1569
bellard579a97f2007-11-11 14:26:47 +00001570static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1571 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001572{
1573 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001574 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001575 int i;
1576
bellard579a97f2007-11-11 14:26:47 +00001577 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1578 if (!target_vec)
1579 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001580 for(i = 0;i < count; i++) {
balrogd732dcb2008-10-28 10:21:03 +00001581 if (target_vec[i].iov_base) {
1582 base = tswapl(target_vec[i].iov_base);
1583 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1584 }
pbrook53a59602006-03-25 19:31:22 +00001585 }
1586 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001587
1588 return 0;
pbrook53a59602006-03-25 19:31:22 +00001589}
1590
ths0da46a62007-10-20 20:23:07 +00001591/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001592static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001593{
1594#if defined(TARGET_MIPS)
1595 switch(type) {
1596 case TARGET_SOCK_DGRAM:
1597 type = SOCK_DGRAM;
1598 break;
1599 case TARGET_SOCK_STREAM:
1600 type = SOCK_STREAM;
1601 break;
1602 case TARGET_SOCK_RAW:
1603 type = SOCK_RAW;
1604 break;
1605 case TARGET_SOCK_RDM:
1606 type = SOCK_RDM;
1607 break;
1608 case TARGET_SOCK_SEQPACKET:
1609 type = SOCK_SEQPACKET;
1610 break;
1611 case TARGET_SOCK_PACKET:
1612 type = SOCK_PACKET;
1613 break;
1614 }
1615#endif
balrog12bc92a2007-10-30 21:06:14 +00001616 if (domain == PF_NETLINK)
1617 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001618 return get_errno(socket(domain, type, protocol));
1619}
1620
ths0da46a62007-10-20 20:23:07 +00001621/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001622static abi_long do_bind(int sockfd, abi_ulong target_addr,
1623 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001624{
aurel328f7aeaf2009-01-30 19:47:57 +00001625 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001626 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001627
Blue Swirl38724252010-09-18 05:53:14 +00001628 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001629 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001630 }
aurel328f7aeaf2009-01-30 19:47:57 +00001631
aurel32607175e2009-04-15 16:11:59 +00001632 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001633
Arnaud Patard917507b2009-06-19 10:44:45 +03001634 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1635 if (ret)
1636 return ret;
1637
bellard3532fa72006-06-24 15:06:03 +00001638 return get_errno(bind(sockfd, addr, addrlen));
1639}
1640
ths0da46a62007-10-20 20:23:07 +00001641/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001642static abi_long do_connect(int sockfd, abi_ulong target_addr,
1643 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001644{
aurel328f7aeaf2009-01-30 19:47:57 +00001645 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001646 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001647
Blue Swirl38724252010-09-18 05:53:14 +00001648 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001649 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001650 }
aurel328f7aeaf2009-01-30 19:47:57 +00001651
1652 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001653
Arnaud Patard917507b2009-06-19 10:44:45 +03001654 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1655 if (ret)
1656 return ret;
1657
bellard3532fa72006-06-24 15:06:03 +00001658 return get_errno(connect(sockfd, addr, addrlen));
1659}
1660
ths0da46a62007-10-20 20:23:07 +00001661/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001662static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1663 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001664{
balrog6de645c2008-10-28 10:26:29 +00001665 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001666 struct target_msghdr *msgp;
1667 struct msghdr msg;
1668 int count;
1669 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001670 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001671
bellard579a97f2007-11-11 14:26:47 +00001672 /* FIXME */
1673 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1674 msgp,
1675 target_msg,
1676 send ? 1 : 0))
1677 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001678 if (msgp->msg_name) {
1679 msg.msg_namelen = tswap32(msgp->msg_namelen);
1680 msg.msg_name = alloca(msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001681 ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001682 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001683 if (ret) {
1684 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1685 return ret;
1686 }
bellard3532fa72006-06-24 15:06:03 +00001687 } else {
1688 msg.msg_name = NULL;
1689 msg.msg_namelen = 0;
1690 }
1691 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1692 msg.msg_control = alloca(msg.msg_controllen);
1693 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001694
bellard3532fa72006-06-24 15:06:03 +00001695 count = tswapl(msgp->msg_iovlen);
1696 vec = alloca(count * sizeof(struct iovec));
1697 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001698 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001699 msg.msg_iovlen = count;
1700 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001701
bellard3532fa72006-06-24 15:06:03 +00001702 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001703 ret = target_to_host_cmsg(&msg, msgp);
1704 if (ret == 0)
1705 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001706 } else {
1707 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001708 if (!is_error(ret)) {
1709 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001710 ret = host_to_target_cmsg(msgp, &msg);
balrog6de645c2008-10-28 10:26:29 +00001711 if (!is_error(ret))
1712 ret = len;
1713 }
bellard3532fa72006-06-24 15:06:03 +00001714 }
1715 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001716 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001717 return ret;
1718}
1719
ths0da46a62007-10-20 20:23:07 +00001720/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001721static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001722 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001723{
bellard2f619692007-11-16 10:46:05 +00001724 socklen_t addrlen;
1725 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001726 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001727
Arnaud Patard917507b2009-06-19 10:44:45 +03001728 if (target_addr == 0)
1729 return get_errno(accept(fd, NULL, NULL));
1730
1731 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00001732 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03001733 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00001734
Blue Swirl38724252010-09-18 05:53:14 +00001735 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001736 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001737 }
aurel328f7aeaf2009-01-30 19:47:57 +00001738
Arnaud Patard917507b2009-06-19 10:44:45 +03001739 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1740 return -TARGET_EINVAL;
1741
bellard2f619692007-11-16 10:46:05 +00001742 addr = alloca(addrlen);
1743
pbrook1be9e1d2006-11-19 15:26:04 +00001744 ret = get_errno(accept(fd, addr, &addrlen));
1745 if (!is_error(ret)) {
1746 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001747 if (put_user_u32(addrlen, target_addrlen_addr))
1748 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001749 }
1750 return ret;
1751}
1752
ths0da46a62007-10-20 20:23:07 +00001753/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001754static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001755 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001756{
bellard2f619692007-11-16 10:46:05 +00001757 socklen_t addrlen;
1758 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001759 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001760
bellard2f619692007-11-16 10:46:05 +00001761 if (get_user_u32(addrlen, target_addrlen_addr))
1762 return -TARGET_EFAULT;
1763
Blue Swirl38724252010-09-18 05:53:14 +00001764 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001765 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001766 }
aurel328f7aeaf2009-01-30 19:47:57 +00001767
Arnaud Patard917507b2009-06-19 10:44:45 +03001768 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1769 return -TARGET_EFAULT;
1770
bellard2f619692007-11-16 10:46:05 +00001771 addr = alloca(addrlen);
1772
pbrook1be9e1d2006-11-19 15:26:04 +00001773 ret = get_errno(getpeername(fd, addr, &addrlen));
1774 if (!is_error(ret)) {
1775 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001776 if (put_user_u32(addrlen, target_addrlen_addr))
1777 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001778 }
1779 return ret;
1780}
1781
ths0da46a62007-10-20 20:23:07 +00001782/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001783static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001784 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001785{
bellard2f619692007-11-16 10:46:05 +00001786 socklen_t addrlen;
1787 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001788 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001789
bellard2f619692007-11-16 10:46:05 +00001790 if (get_user_u32(addrlen, target_addrlen_addr))
1791 return -TARGET_EFAULT;
1792
Blue Swirl38724252010-09-18 05:53:14 +00001793 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001794 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001795 }
aurel328f7aeaf2009-01-30 19:47:57 +00001796
Arnaud Patard917507b2009-06-19 10:44:45 +03001797 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1798 return -TARGET_EFAULT;
1799
bellard2f619692007-11-16 10:46:05 +00001800 addr = alloca(addrlen);
1801
pbrook1be9e1d2006-11-19 15:26:04 +00001802 ret = get_errno(getsockname(fd, addr, &addrlen));
1803 if (!is_error(ret)) {
1804 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001805 if (put_user_u32(addrlen, target_addrlen_addr))
1806 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001807 }
1808 return ret;
1809}
1810
ths0da46a62007-10-20 20:23:07 +00001811/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001812static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001813 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001814{
1815 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001816 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001817
1818 ret = get_errno(socketpair(domain, type, protocol, tab));
1819 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001820 if (put_user_s32(tab[0], target_tab_addr)
1821 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1822 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001823 }
1824 return ret;
1825}
1826
ths0da46a62007-10-20 20:23:07 +00001827/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001828static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1829 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001830{
1831 void *addr;
1832 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001833 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001834
Blue Swirl38724252010-09-18 05:53:14 +00001835 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001836 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001837 }
aurel328f7aeaf2009-01-30 19:47:57 +00001838
bellard579a97f2007-11-11 14:26:47 +00001839 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1840 if (!host_msg)
1841 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001842 if (target_addr) {
1843 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001844 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1845 if (ret) {
1846 unlock_user(host_msg, msg, 0);
1847 return ret;
1848 }
pbrook1be9e1d2006-11-19 15:26:04 +00001849 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1850 } else {
1851 ret = get_errno(send(fd, host_msg, len, flags));
1852 }
1853 unlock_user(host_msg, msg, 0);
1854 return ret;
1855}
1856
ths0da46a62007-10-20 20:23:07 +00001857/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001858static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1859 abi_ulong target_addr,
1860 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001861{
1862 socklen_t addrlen;
1863 void *addr;
1864 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001865 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001866
bellard579a97f2007-11-11 14:26:47 +00001867 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1868 if (!host_msg)
1869 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001870 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001871 if (get_user_u32(addrlen, target_addrlen)) {
1872 ret = -TARGET_EFAULT;
1873 goto fail;
1874 }
Blue Swirl38724252010-09-18 05:53:14 +00001875 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001876 ret = -TARGET_EINVAL;
1877 goto fail;
1878 }
pbrook1be9e1d2006-11-19 15:26:04 +00001879 addr = alloca(addrlen);
1880 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1881 } else {
1882 addr = NULL; /* To keep compiler quiet. */
1883 ret = get_errno(recv(fd, host_msg, len, flags));
1884 }
1885 if (!is_error(ret)) {
1886 if (target_addr) {
1887 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001888 if (put_user_u32(addrlen, target_addrlen)) {
1889 ret = -TARGET_EFAULT;
1890 goto fail;
1891 }
pbrook1be9e1d2006-11-19 15:26:04 +00001892 }
1893 unlock_user(host_msg, msg, len);
1894 } else {
bellard2f619692007-11-16 10:46:05 +00001895fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001896 unlock_user(host_msg, msg, 0);
1897 }
1898 return ret;
1899}
1900
j_mayer32407102007-09-26 23:01:49 +00001901#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001902/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001903static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001904{
blueswir1992f48a2007-10-14 16:27:31 +00001905 abi_long ret;
1906 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001907
1908 switch(num) {
1909 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001910 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001911 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001912
Ulrich Hecht98818182009-07-03 17:09:28 +02001913 if (get_user_ual(domain, vptr)
1914 || get_user_ual(type, vptr + n)
1915 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001916 return -TARGET_EFAULT;
1917
bellard3532fa72006-06-24 15:06:03 +00001918 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001919 }
bellard31e31b82003-02-18 22:55:36 +00001920 break;
1921 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001922 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001923 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001924 abi_ulong target_addr;
1925 socklen_t addrlen;
1926
Ulrich Hecht98818182009-07-03 17:09:28 +02001927 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001928 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001929 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001930 return -TARGET_EFAULT;
1931
bellard3532fa72006-06-24 15:06:03 +00001932 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001933 }
bellard31e31b82003-02-18 22:55:36 +00001934 break;
1935 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001936 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001937 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001938 abi_ulong target_addr;
1939 socklen_t addrlen;
1940
Ulrich Hecht98818182009-07-03 17:09:28 +02001941 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001942 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001943 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001944 return -TARGET_EFAULT;
1945
bellard3532fa72006-06-24 15:06:03 +00001946 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001947 }
bellard31e31b82003-02-18 22:55:36 +00001948 break;
1949 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001950 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001951 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00001952
Ulrich Hecht98818182009-07-03 17:09:28 +02001953 if (get_user_ual(sockfd, vptr)
1954 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00001955 return -TARGET_EFAULT;
1956
bellard7854b052003-03-29 17:22:23 +00001957 ret = get_errno(listen(sockfd, backlog));
1958 }
bellard31e31b82003-02-18 22:55:36 +00001959 break;
1960 case SOCKOP_accept:
1961 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001962 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001963 abi_ulong target_addr, target_addrlen;
1964
Ulrich Hecht98818182009-07-03 17:09:28 +02001965 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001966 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001967 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001968 return -TARGET_EFAULT;
1969
pbrook1be9e1d2006-11-19 15:26:04 +00001970 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001971 }
1972 break;
1973 case SOCKOP_getsockname:
1974 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001975 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001976 abi_ulong target_addr, target_addrlen;
1977
Ulrich Hecht98818182009-07-03 17:09:28 +02001978 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001979 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001980 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001981 return -TARGET_EFAULT;
1982
pbrook1be9e1d2006-11-19 15:26:04 +00001983 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001984 }
1985 break;
1986 case SOCKOP_getpeername:
1987 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001988 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001989 abi_ulong target_addr, target_addrlen;
1990
Ulrich Hecht98818182009-07-03 17:09:28 +02001991 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001992 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001993 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001994 return -TARGET_EFAULT;
1995
pbrook1be9e1d2006-11-19 15:26:04 +00001996 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001997 }
1998 break;
1999 case SOCKOP_socketpair:
2000 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002001 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002002 abi_ulong tab;
2003
Ulrich Hecht98818182009-07-03 17:09:28 +02002004 if (get_user_ual(domain, vptr)
2005 || get_user_ual(type, vptr + n)
2006 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002007 || get_user_ual(tab, vptr + 3 * n))
2008 return -TARGET_EFAULT;
2009
pbrook1be9e1d2006-11-19 15:26:04 +00002010 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00002011 }
2012 break;
2013 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00002014 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002015 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002016 abi_ulong msg;
2017 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002018 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002019
Ulrich Hecht98818182009-07-03 17:09:28 +02002020 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002021 || get_user_ual(msg, vptr + n)
2022 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002023 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002024 return -TARGET_EFAULT;
2025
pbrook1be9e1d2006-11-19 15:26:04 +00002026 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002027 }
bellard31e31b82003-02-18 22:55:36 +00002028 break;
2029 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00002030 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002031 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002032 abi_ulong msg;
2033 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002034 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002035
Ulrich Hecht98818182009-07-03 17:09:28 +02002036 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002037 || get_user_ual(msg, vptr + n)
2038 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002039 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002040 return -TARGET_EFAULT;
2041
pbrook1be9e1d2006-11-19 15:26:04 +00002042 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002043 }
bellard31e31b82003-02-18 22:55:36 +00002044 break;
2045 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00002046 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002047 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002048 abi_ulong msg;
2049 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002050 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002051 abi_ulong addr;
2052 socklen_t addrlen;
2053
Ulrich Hecht98818182009-07-03 17:09:28 +02002054 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002055 || get_user_ual(msg, vptr + n)
2056 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002057 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002058 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002059 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002060 return -TARGET_EFAULT;
2061
pbrook1be9e1d2006-11-19 15:26:04 +00002062 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002063 }
bellard31e31b82003-02-18 22:55:36 +00002064 break;
2065 case SOCKOP_recvfrom:
2066 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002067 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002068 abi_ulong msg;
2069 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002070 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002071 abi_ulong addr;
2072 socklen_t addrlen;
2073
Ulrich Hecht98818182009-07-03 17:09:28 +02002074 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002075 || get_user_ual(msg, vptr + n)
2076 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002077 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002078 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002079 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002080 return -TARGET_EFAULT;
2081
pbrook1be9e1d2006-11-19 15:26:04 +00002082 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00002083 }
2084 break;
2085 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00002086 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002087 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00002088
Ulrich Hecht98818182009-07-03 17:09:28 +02002089 if (get_user_ual(sockfd, vptr)
2090 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002091 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002092
2093 ret = get_errno(shutdown(sockfd, how));
2094 }
bellard31e31b82003-02-18 22:55:36 +00002095 break;
2096 case SOCKOP_sendmsg:
2097 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002098 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002099 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002100 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002101 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002102
Ulrich Hecht98818182009-07-03 17:09:28 +02002103 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002104 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002105 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002106 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002107
ths5fafdf22007-09-16 21:08:06 +00002108 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002109 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002110 }
2111 break;
bellard31e31b82003-02-18 22:55:36 +00002112 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002113 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002114 abi_ulong sockfd;
2115 abi_ulong level;
2116 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002117 abi_ulong optval;
2118 socklen_t optlen;
2119
Ulrich Hecht98818182009-07-03 17:09:28 +02002120 if (get_user_ual(sockfd, vptr)
2121 || get_user_ual(level, vptr + n)
2122 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002123 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002124 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002125 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002126
2127 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2128 }
2129 break;
bellard31e31b82003-02-18 22:55:36 +00002130 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002131 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002132 abi_ulong sockfd;
2133 abi_ulong level;
2134 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002135 abi_ulong optval;
2136 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002137
Ulrich Hecht98818182009-07-03 17:09:28 +02002138 if (get_user_ual(sockfd, vptr)
2139 || get_user_ual(level, vptr + n)
2140 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002141 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002142 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002143 return -TARGET_EFAULT;
2144
2145 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002146 }
2147 break;
bellard31e31b82003-02-18 22:55:36 +00002148 default:
2149 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002150 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002151 break;
2152 }
2153 return ret;
2154}
j_mayer32407102007-09-26 23:01:49 +00002155#endif
bellard31e31b82003-02-18 22:55:36 +00002156
bellard8853f862004-02-22 14:57:26 +00002157#define N_SHM_REGIONS 32
2158
2159static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002160 abi_ulong start;
2161 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002162} shm_regions[N_SHM_REGIONS];
2163
ths3eb6b042007-06-03 14:26:27 +00002164struct target_ipc_perm
2165{
blueswir1992f48a2007-10-14 16:27:31 +00002166 abi_long __key;
2167 abi_ulong uid;
2168 abi_ulong gid;
2169 abi_ulong cuid;
2170 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002171 unsigned short int mode;
2172 unsigned short int __pad1;
2173 unsigned short int __seq;
2174 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002175 abi_ulong __unused1;
2176 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002177};
2178
2179struct target_semid_ds
2180{
2181 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002182 abi_ulong sem_otime;
2183 abi_ulong __unused1;
2184 abi_ulong sem_ctime;
2185 abi_ulong __unused2;
2186 abi_ulong sem_nsems;
2187 abi_ulong __unused3;
2188 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002189};
2190
bellard579a97f2007-11-11 14:26:47 +00002191static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2192 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002193{
2194 struct target_ipc_perm *target_ip;
2195 struct target_semid_ds *target_sd;
2196
bellard579a97f2007-11-11 14:26:47 +00002197 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2198 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002199 target_ip = &(target_sd->sem_perm);
ths3eb6b042007-06-03 14:26:27 +00002200 host_ip->__key = tswapl(target_ip->__key);
2201 host_ip->uid = tswapl(target_ip->uid);
2202 host_ip->gid = tswapl(target_ip->gid);
2203 host_ip->cuid = tswapl(target_ip->cuid);
2204 host_ip->cgid = tswapl(target_ip->cgid);
2205 host_ip->mode = tswapl(target_ip->mode);
2206 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002207 return 0;
ths3eb6b042007-06-03 14:26:27 +00002208}
2209
bellard579a97f2007-11-11 14:26:47 +00002210static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2211 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002212{
2213 struct target_ipc_perm *target_ip;
2214 struct target_semid_ds *target_sd;
2215
bellard579a97f2007-11-11 14:26:47 +00002216 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2217 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002218 target_ip = &(target_sd->sem_perm);
2219 target_ip->__key = tswapl(host_ip->__key);
2220 target_ip->uid = tswapl(host_ip->uid);
2221 target_ip->gid = tswapl(host_ip->gid);
2222 target_ip->cuid = tswapl(host_ip->cuid);
2223 target_ip->cgid = tswapl(host_ip->cgid);
2224 target_ip->mode = tswapl(host_ip->mode);
2225 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002226 return 0;
ths3eb6b042007-06-03 14:26:27 +00002227}
2228
bellard579a97f2007-11-11 14:26:47 +00002229static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2230 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002231{
2232 struct target_semid_ds *target_sd;
2233
bellard579a97f2007-11-11 14:26:47 +00002234 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2235 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002236 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2237 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002238 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2239 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2240 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2241 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002242 return 0;
ths3eb6b042007-06-03 14:26:27 +00002243}
2244
bellard579a97f2007-11-11 14:26:47 +00002245static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2246 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002247{
2248 struct target_semid_ds *target_sd;
2249
bellard579a97f2007-11-11 14:26:47 +00002250 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2251 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002252 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2253 return -TARGET_EFAULT;;
ths3eb6b042007-06-03 14:26:27 +00002254 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2255 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2256 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2257 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002258 return 0;
ths3eb6b042007-06-03 14:26:27 +00002259}
2260
aurel32e5289082009-04-18 16:16:12 +00002261struct target_seminfo {
2262 int semmap;
2263 int semmni;
2264 int semmns;
2265 int semmnu;
2266 int semmsl;
2267 int semopm;
2268 int semume;
2269 int semusz;
2270 int semvmx;
2271 int semaem;
2272};
2273
2274static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2275 struct seminfo *host_seminfo)
2276{
2277 struct target_seminfo *target_seminfo;
2278 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2279 return -TARGET_EFAULT;
2280 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2281 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2282 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2283 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2284 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2285 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2286 __put_user(host_seminfo->semume, &target_seminfo->semume);
2287 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2288 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2289 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2290 unlock_user_struct(target_seminfo, target_addr, 1);
2291 return 0;
2292}
2293
thsfa294812007-02-02 22:05:00 +00002294union semun {
2295 int val;
ths3eb6b042007-06-03 14:26:27 +00002296 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002297 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002298 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002299};
2300
ths3eb6b042007-06-03 14:26:27 +00002301union target_semun {
2302 int val;
aurel32e5289082009-04-18 16:16:12 +00002303 abi_ulong buf;
2304 abi_ulong array;
2305 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002306};
2307
aurel32e5289082009-04-18 16:16:12 +00002308static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2309 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002310{
aurel32e5289082009-04-18 16:16:12 +00002311 int nsems;
2312 unsigned short *array;
2313 union semun semun;
2314 struct semid_ds semid_ds;
2315 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002316
aurel32e5289082009-04-18 16:16:12 +00002317 semun.buf = &semid_ds;
2318
2319 ret = semctl(semid, 0, IPC_STAT, semun);
2320 if (ret == -1)
2321 return get_errno(ret);
2322
2323 nsems = semid_ds.sem_nsems;
2324
2325 *host_array = malloc(nsems*sizeof(unsigned short));
2326 array = lock_user(VERIFY_READ, target_addr,
2327 nsems*sizeof(unsigned short), 1);
2328 if (!array)
2329 return -TARGET_EFAULT;
2330
2331 for(i=0; i<nsems; i++) {
2332 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002333 }
aurel32e5289082009-04-18 16:16:12 +00002334 unlock_user(array, target_addr, 0);
2335
bellard579a97f2007-11-11 14:26:47 +00002336 return 0;
ths3eb6b042007-06-03 14:26:27 +00002337}
2338
aurel32e5289082009-04-18 16:16:12 +00002339static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2340 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002341{
aurel32e5289082009-04-18 16:16:12 +00002342 int nsems;
2343 unsigned short *array;
2344 union semun semun;
2345 struct semid_ds semid_ds;
2346 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002347
aurel32e5289082009-04-18 16:16:12 +00002348 semun.buf = &semid_ds;
2349
2350 ret = semctl(semid, 0, IPC_STAT, semun);
2351 if (ret == -1)
2352 return get_errno(ret);
2353
2354 nsems = semid_ds.sem_nsems;
2355
2356 array = lock_user(VERIFY_WRITE, target_addr,
2357 nsems*sizeof(unsigned short), 0);
2358 if (!array)
2359 return -TARGET_EFAULT;
2360
2361 for(i=0; i<nsems; i++) {
2362 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002363 }
aurel32e5289082009-04-18 16:16:12 +00002364 free(*host_array);
2365 unlock_user(array, target_addr, 1);
2366
bellard579a97f2007-11-11 14:26:47 +00002367 return 0;
ths3eb6b042007-06-03 14:26:27 +00002368}
2369
aurel32e5289082009-04-18 16:16:12 +00002370static inline abi_long do_semctl(int semid, int semnum, int cmd,
2371 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002372{
2373 union semun arg;
2374 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302375 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002376 struct seminfo seminfo;
2377 abi_long ret = -TARGET_EINVAL;
2378 abi_long err;
2379 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002380
2381 switch( cmd ) {
2382 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002383 case SETVAL:
aurel32e5289082009-04-18 16:16:12 +00002384 arg.val = tswapl(target_su.val);
2385 ret = get_errno(semctl(semid, semnum, cmd, arg));
2386 target_su.val = tswapl(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002387 break;
2388 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002389 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002390 err = target_to_host_semarray(semid, &array, target_su.array);
2391 if (err)
2392 return err;
2393 arg.array = array;
2394 ret = get_errno(semctl(semid, semnum, cmd, arg));
2395 err = host_to_target_semarray(semid, target_su.array, &array);
2396 if (err)
2397 return err;
ths3eb6b042007-06-03 14:26:27 +00002398 break;
2399 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002400 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002401 case SEM_STAT:
2402 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2403 if (err)
2404 return err;
2405 arg.buf = &dsarg;
2406 ret = get_errno(semctl(semid, semnum, cmd, arg));
2407 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2408 if (err)
2409 return err;
ths3eb6b042007-06-03 14:26:27 +00002410 break;
aurel32e5289082009-04-18 16:16:12 +00002411 case IPC_INFO:
2412 case SEM_INFO:
2413 arg.__buf = &seminfo;
2414 ret = get_errno(semctl(semid, semnum, cmd, arg));
2415 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2416 if (err)
2417 return err;
2418 break;
2419 case IPC_RMID:
2420 case GETPID:
2421 case GETNCNT:
2422 case GETZCNT:
2423 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2424 break;
ths3eb6b042007-06-03 14:26:27 +00002425 }
2426
2427 return ret;
2428}
2429
aurel32e5289082009-04-18 16:16:12 +00002430struct target_sembuf {
2431 unsigned short sem_num;
2432 short sem_op;
2433 short sem_flg;
2434};
2435
2436static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2437 abi_ulong target_addr,
2438 unsigned nsops)
2439{
2440 struct target_sembuf *target_sembuf;
2441 int i;
2442
2443 target_sembuf = lock_user(VERIFY_READ, target_addr,
2444 nsops*sizeof(struct target_sembuf), 1);
2445 if (!target_sembuf)
2446 return -TARGET_EFAULT;
2447
2448 for(i=0; i<nsops; i++) {
2449 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2450 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2451 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2452 }
2453
2454 unlock_user(target_sembuf, target_addr, 0);
2455
2456 return 0;
2457}
2458
2459static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2460{
2461 struct sembuf sops[nsops];
2462
2463 if (target_to_host_sembuf(sops, ptr, nsops))
2464 return -TARGET_EFAULT;
2465
2466 return semop(semid, sops, nsops);
2467}
2468
ths1bc012f2007-06-03 14:27:49 +00002469struct target_msqid_ds
2470{
aurel321c54ff92008-10-13 21:08:44 +00002471 struct target_ipc_perm msg_perm;
2472 abi_ulong msg_stime;
2473#if TARGET_ABI_BITS == 32
2474 abi_ulong __unused1;
2475#endif
2476 abi_ulong msg_rtime;
2477#if TARGET_ABI_BITS == 32
2478 abi_ulong __unused2;
2479#endif
2480 abi_ulong msg_ctime;
2481#if TARGET_ABI_BITS == 32
2482 abi_ulong __unused3;
2483#endif
2484 abi_ulong __msg_cbytes;
2485 abi_ulong msg_qnum;
2486 abi_ulong msg_qbytes;
2487 abi_ulong msg_lspid;
2488 abi_ulong msg_lrpid;
2489 abi_ulong __unused4;
2490 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002491};
2492
bellard579a97f2007-11-11 14:26:47 +00002493static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2494 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002495{
2496 struct target_msqid_ds *target_md;
2497
bellard579a97f2007-11-11 14:26:47 +00002498 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2499 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002500 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2501 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002502 host_md->msg_stime = tswapl(target_md->msg_stime);
2503 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2504 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2505 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2506 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2507 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2508 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2509 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2510 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002511 return 0;
ths1bc012f2007-06-03 14:27:49 +00002512}
2513
bellard579a97f2007-11-11 14:26:47 +00002514static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2515 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002516{
2517 struct target_msqid_ds *target_md;
2518
bellard579a97f2007-11-11 14:26:47 +00002519 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2520 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002521 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2522 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002523 target_md->msg_stime = tswapl(host_md->msg_stime);
2524 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2525 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2526 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2527 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2528 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2529 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2530 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2531 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002532 return 0;
ths1bc012f2007-06-03 14:27:49 +00002533}
2534
aurel321c54ff92008-10-13 21:08:44 +00002535struct target_msginfo {
2536 int msgpool;
2537 int msgmap;
2538 int msgmax;
2539 int msgmnb;
2540 int msgmni;
2541 int msgssz;
2542 int msgtql;
2543 unsigned short int msgseg;
2544};
2545
2546static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2547 struct msginfo *host_msginfo)
2548{
2549 struct target_msginfo *target_msginfo;
2550 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2551 return -TARGET_EFAULT;
2552 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2553 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2554 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2555 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2556 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2557 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2558 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2559 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2560 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002561 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002562}
2563
2564static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002565{
2566 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002567 struct msginfo msginfo;
2568 abi_long ret = -TARGET_EINVAL;
2569
2570 cmd &= 0xff;
2571
2572 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002573 case IPC_STAT:
2574 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002575 case MSG_STAT:
2576 if (target_to_host_msqid_ds(&dsarg,ptr))
2577 return -TARGET_EFAULT;
2578 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2579 if (host_to_target_msqid_ds(ptr,&dsarg))
2580 return -TARGET_EFAULT;
2581 break;
2582 case IPC_RMID:
2583 ret = get_errno(msgctl(msgid, cmd, NULL));
2584 break;
2585 case IPC_INFO:
2586 case MSG_INFO:
2587 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2588 if (host_to_target_msginfo(ptr, &msginfo))
2589 return -TARGET_EFAULT;
2590 break;
ths1bc012f2007-06-03 14:27:49 +00002591 }
aurel321c54ff92008-10-13 21:08:44 +00002592
ths1bc012f2007-06-03 14:27:49 +00002593 return ret;
2594}
2595
2596struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002597 abi_long mtype;
2598 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002599};
2600
blueswir1992f48a2007-10-14 16:27:31 +00002601static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2602 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002603{
2604 struct target_msgbuf *target_mb;
2605 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002606 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002607
bellard579a97f2007-11-11 14:26:47 +00002608 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2609 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002610 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002611 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2612 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002613 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2614 free(host_mb);
2615 unlock_user_struct(target_mb, msgp, 0);
2616
2617 return ret;
2618}
2619
blueswir1992f48a2007-10-14 16:27:31 +00002620static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002621 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002622 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002623{
2624 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002625 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002626 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002627 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002628
bellard579a97f2007-11-11 14:26:47 +00002629 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2630 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002631
ths1bc012f2007-06-03 14:27:49 +00002632 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002633 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2634
bellard579a97f2007-11-11 14:26:47 +00002635 if (ret > 0) {
2636 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2637 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2638 if (!target_mtext) {
2639 ret = -TARGET_EFAULT;
2640 goto end;
2641 }
aurel321c54ff92008-10-13 21:08:44 +00002642 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002643 unlock_user(target_mtext, target_mtext_addr, ret);
2644 }
aurel321c54ff92008-10-13 21:08:44 +00002645
ths1bc012f2007-06-03 14:27:49 +00002646 target_mb->mtype = tswapl(host_mb->mtype);
2647 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002648
bellard579a97f2007-11-11 14:26:47 +00002649end:
2650 if (target_mb)
2651 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00002652 return ret;
2653}
2654
Riku Voipio88a8c982009-04-03 10:42:00 +03002655struct target_shmid_ds
2656{
2657 struct target_ipc_perm shm_perm;
2658 abi_ulong shm_segsz;
2659 abi_ulong shm_atime;
2660#if TARGET_ABI_BITS == 32
2661 abi_ulong __unused1;
2662#endif
2663 abi_ulong shm_dtime;
2664#if TARGET_ABI_BITS == 32
2665 abi_ulong __unused2;
2666#endif
2667 abi_ulong shm_ctime;
2668#if TARGET_ABI_BITS == 32
2669 abi_ulong __unused3;
2670#endif
2671 int shm_cpid;
2672 int shm_lpid;
2673 abi_ulong shm_nattch;
2674 unsigned long int __unused4;
2675 unsigned long int __unused5;
2676};
2677
2678static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2679 abi_ulong target_addr)
2680{
2681 struct target_shmid_ds *target_sd;
2682
2683 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2684 return -TARGET_EFAULT;
2685 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2686 return -TARGET_EFAULT;
2687 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2688 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2689 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2690 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2691 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2692 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2693 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2694 unlock_user_struct(target_sd, target_addr, 0);
2695 return 0;
2696}
2697
2698static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2699 struct shmid_ds *host_sd)
2700{
2701 struct target_shmid_ds *target_sd;
2702
2703 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2704 return -TARGET_EFAULT;
2705 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2706 return -TARGET_EFAULT;
2707 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2708 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2709 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2710 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2711 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2712 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2713 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2714 unlock_user_struct(target_sd, target_addr, 1);
2715 return 0;
2716}
2717
2718struct target_shminfo {
2719 abi_ulong shmmax;
2720 abi_ulong shmmin;
2721 abi_ulong shmmni;
2722 abi_ulong shmseg;
2723 abi_ulong shmall;
2724};
2725
2726static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2727 struct shminfo *host_shminfo)
2728{
2729 struct target_shminfo *target_shminfo;
2730 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2731 return -TARGET_EFAULT;
2732 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2733 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2734 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2735 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2736 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2737 unlock_user_struct(target_shminfo, target_addr, 1);
2738 return 0;
2739}
2740
2741struct target_shm_info {
2742 int used_ids;
2743 abi_ulong shm_tot;
2744 abi_ulong shm_rss;
2745 abi_ulong shm_swp;
2746 abi_ulong swap_attempts;
2747 abi_ulong swap_successes;
2748};
2749
2750static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2751 struct shm_info *host_shm_info)
2752{
2753 struct target_shm_info *target_shm_info;
2754 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2755 return -TARGET_EFAULT;
2756 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2757 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2758 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2759 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2760 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2761 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2762 unlock_user_struct(target_shm_info, target_addr, 1);
2763 return 0;
2764}
2765
2766static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2767{
2768 struct shmid_ds dsarg;
2769 struct shminfo shminfo;
2770 struct shm_info shm_info;
2771 abi_long ret = -TARGET_EINVAL;
2772
2773 cmd &= 0xff;
2774
2775 switch(cmd) {
2776 case IPC_STAT:
2777 case IPC_SET:
2778 case SHM_STAT:
2779 if (target_to_host_shmid_ds(&dsarg, buf))
2780 return -TARGET_EFAULT;
2781 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2782 if (host_to_target_shmid_ds(buf, &dsarg))
2783 return -TARGET_EFAULT;
2784 break;
2785 case IPC_INFO:
2786 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2787 if (host_to_target_shminfo(buf, &shminfo))
2788 return -TARGET_EFAULT;
2789 break;
2790 case SHM_INFO:
2791 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2792 if (host_to_target_shm_info(buf, &shm_info))
2793 return -TARGET_EFAULT;
2794 break;
2795 case IPC_RMID:
2796 case SHM_LOCK:
2797 case SHM_UNLOCK:
2798 ret = get_errno(shmctl(shmid, cmd, NULL));
2799 break;
2800 }
2801
2802 return ret;
2803}
2804
2805static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2806{
2807 abi_long raddr;
2808 void *host_raddr;
2809 struct shmid_ds shm_info;
2810 int i,ret;
2811
2812 /* find out the length of the shared memory segment */
2813 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2814 if (is_error(ret)) {
2815 /* can't get length, bail out */
2816 return ret;
2817 }
2818
2819 mmap_lock();
2820
2821 if (shmaddr)
2822 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2823 else {
2824 abi_ulong mmap_start;
2825
2826 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2827
2828 if (mmap_start == -1) {
2829 errno = ENOMEM;
2830 host_raddr = (void *)-1;
2831 } else
2832 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2833 }
2834
2835 if (host_raddr == (void *)-1) {
2836 mmap_unlock();
2837 return get_errno((long)host_raddr);
2838 }
2839 raddr=h2g((unsigned long)host_raddr);
2840
2841 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2842 PAGE_VALID | PAGE_READ |
2843 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2844
2845 for (i = 0; i < N_SHM_REGIONS; i++) {
2846 if (shm_regions[i].start == 0) {
2847 shm_regions[i].start = raddr;
2848 shm_regions[i].size = shm_info.shm_segsz;
2849 break;
2850 }
2851 }
2852
2853 mmap_unlock();
2854 return raddr;
2855
2856}
2857
2858static inline abi_long do_shmdt(abi_ulong shmaddr)
2859{
2860 int i;
2861
2862 for (i = 0; i < N_SHM_REGIONS; ++i) {
2863 if (shm_regions[i].start == shmaddr) {
2864 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09002865 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03002866 break;
2867 }
2868 }
2869
2870 return get_errno(shmdt(g2h(shmaddr)));
2871}
2872
aurel321c54ff92008-10-13 21:08:44 +00002873#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00002874/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00002875/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002876static abi_long do_ipc(unsigned int call, int first,
2877 int second, int third,
2878 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00002879{
2880 int version;
blueswir1992f48a2007-10-14 16:27:31 +00002881 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00002882
2883 version = call >> 16;
2884 call &= 0xffff;
2885
2886 switch (call) {
thsfa294812007-02-02 22:05:00 +00002887 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00002888 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00002889 break;
2890
2891 case IPCOP_semget:
2892 ret = get_errno(semget(first, second, third));
2893 break;
2894
2895 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00002896 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00002897 break;
thsd96372e2007-02-02 22:05:44 +00002898
aurel321c54ff92008-10-13 21:08:44 +00002899 case IPCOP_msgget:
2900 ret = get_errno(msgget(first, second));
2901 break;
thsd96372e2007-02-02 22:05:44 +00002902
aurel321c54ff92008-10-13 21:08:44 +00002903 case IPCOP_msgsnd:
2904 ret = do_msgsnd(first, ptr, second, third);
2905 break;
thsd96372e2007-02-02 22:05:44 +00002906
aurel321c54ff92008-10-13 21:08:44 +00002907 case IPCOP_msgctl:
2908 ret = do_msgctl(first, second, ptr);
2909 break;
thsd96372e2007-02-02 22:05:44 +00002910
aurel321c54ff92008-10-13 21:08:44 +00002911 case IPCOP_msgrcv:
2912 switch (version) {
2913 case 0:
2914 {
2915 struct target_ipc_kludge {
2916 abi_long msgp;
2917 abi_long msgtyp;
2918 } *tmp;
thsd96372e2007-02-02 22:05:44 +00002919
aurel321c54ff92008-10-13 21:08:44 +00002920 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2921 ret = -TARGET_EFAULT;
2922 break;
ths1bc012f2007-06-03 14:27:49 +00002923 }
aurel321c54ff92008-10-13 21:08:44 +00002924
2925 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2926
2927 unlock_user_struct(tmp, ptr, 0);
2928 break;
2929 }
2930 default:
2931 ret = do_msgrcv(first, ptr, second, fifth, third);
2932 }
2933 break;
thsd96372e2007-02-02 22:05:44 +00002934
bellard8853f862004-02-22 14:57:26 +00002935 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03002936 switch (version) {
2937 default:
bellard5a4a8982007-11-11 17:39:18 +00002938 {
2939 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03002940 raddr = do_shmat(first, ptr, second);
2941 if (is_error(raddr))
2942 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00002943 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002944 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03002945 break;
2946 }
2947 case 1:
2948 ret = -TARGET_EINVAL;
2949 break;
bellard5a4a8982007-11-11 17:39:18 +00002950 }
bellard8853f862004-02-22 14:57:26 +00002951 break;
2952 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03002953 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00002954 break;
2955
2956 case IPCOP_shmget:
2957 /* IPC_* flag values are the same on all linux platforms */
2958 ret = get_errno(shmget(first, second, third));
2959 break;
2960
2961 /* IPC_* and SHM_* command values are the same on all linux platforms */
2962 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03002963 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00002964 break;
2965 default:
j_mayer32407102007-09-26 23:01:49 +00002966 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002967 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002968 break;
2969 }
2970 return ret;
2971}
j_mayer32407102007-09-26 23:01:49 +00002972#endif
bellard8853f862004-02-22 14:57:26 +00002973
bellard31e31b82003-02-18 22:55:36 +00002974/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00002975
Blue Swirl001faf32009-05-13 17:53:17 +00002976#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00002977#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2978enum {
2979#include "syscall_types.h"
2980};
2981#undef STRUCT
2982#undef STRUCT_SPECIAL
2983
Blue Swirl001faf32009-05-13 17:53:17 +00002984#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00002985#define STRUCT_SPECIAL(name)
2986#include "syscall_types.h"
2987#undef STRUCT
2988#undef STRUCT_SPECIAL
2989
Peter Maydelld2ef05b2011-01-06 15:04:17 +00002990typedef struct IOCTLEntry IOCTLEntry;
2991
2992typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
2993 int fd, abi_long cmd, abi_long arg);
2994
2995struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002996 unsigned int target_cmd;
2997 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002998 const char *name;
2999 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003000 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003001 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003002};
bellard31e31b82003-02-18 22:55:36 +00003003
3004#define IOC_R 0x0001
3005#define IOC_W 0x0002
3006#define IOC_RW (IOC_R | IOC_W)
3007
3008#define MAX_STRUCT_SIZE 4096
3009
Peter Maydelldace20d2011-01-10 13:11:24 +00003010#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003011/* So fiemap access checks don't overflow on 32 bit systems.
3012 * This is very slightly smaller than the limit imposed by
3013 * the underlying kernel.
3014 */
3015#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3016 / sizeof(struct fiemap_extent))
3017
3018static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3019 int fd, abi_long cmd, abi_long arg)
3020{
3021 /* The parameter for this ioctl is a struct fiemap followed
3022 * by an array of struct fiemap_extent whose size is set
3023 * in fiemap->fm_extent_count. The array is filled in by the
3024 * ioctl.
3025 */
3026 int target_size_in, target_size_out;
3027 struct fiemap *fm;
3028 const argtype *arg_type = ie->arg_type;
3029 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3030 void *argptr, *p;
3031 abi_long ret;
3032 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3033 uint32_t outbufsz;
3034 int free_fm = 0;
3035
3036 assert(arg_type[0] == TYPE_PTR);
3037 assert(ie->access == IOC_RW);
3038 arg_type++;
3039 target_size_in = thunk_type_size(arg_type, 0);
3040 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3041 if (!argptr) {
3042 return -TARGET_EFAULT;
3043 }
3044 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3045 unlock_user(argptr, arg, 0);
3046 fm = (struct fiemap *)buf_temp;
3047 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3048 return -TARGET_EINVAL;
3049 }
3050
3051 outbufsz = sizeof (*fm) +
3052 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3053
3054 if (outbufsz > MAX_STRUCT_SIZE) {
3055 /* We can't fit all the extents into the fixed size buffer.
3056 * Allocate one that is large enough and use it instead.
3057 */
3058 fm = malloc(outbufsz);
3059 if (!fm) {
3060 return -TARGET_ENOMEM;
3061 }
3062 memcpy(fm, buf_temp, sizeof(struct fiemap));
3063 free_fm = 1;
3064 }
3065 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3066 if (!is_error(ret)) {
3067 target_size_out = target_size_in;
3068 /* An extent_count of 0 means we were only counting the extents
3069 * so there are no structs to copy
3070 */
3071 if (fm->fm_extent_count != 0) {
3072 target_size_out += fm->fm_mapped_extents * extent_size;
3073 }
3074 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3075 if (!argptr) {
3076 ret = -TARGET_EFAULT;
3077 } else {
3078 /* Convert the struct fiemap */
3079 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3080 if (fm->fm_extent_count != 0) {
3081 p = argptr + target_size_in;
3082 /* ...and then all the struct fiemap_extents */
3083 for (i = 0; i < fm->fm_mapped_extents; i++) {
3084 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3085 THUNK_TARGET);
3086 p += extent_size;
3087 }
3088 }
3089 unlock_user(argptr, arg, target_size_out);
3090 }
3091 }
3092 if (free_fm) {
3093 free(fm);
3094 }
3095 return ret;
3096}
Peter Maydelldace20d2011-01-10 13:11:24 +00003097#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003098
Laurent Vivier059c2f22011-03-30 00:12:12 +02003099static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3100 int fd, abi_long cmd, abi_long arg)
3101{
3102 const argtype *arg_type = ie->arg_type;
3103 int target_size;
3104 void *argptr;
3105 int ret;
3106 struct ifconf *host_ifconf;
3107 uint32_t outbufsz;
3108 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3109 int target_ifreq_size;
3110 int nb_ifreq;
3111 int free_buf = 0;
3112 int i;
3113 int target_ifc_len;
3114 abi_long target_ifc_buf;
3115 int host_ifc_len;
3116 char *host_ifc_buf;
3117
3118 assert(arg_type[0] == TYPE_PTR);
3119 assert(ie->access == IOC_RW);
3120
3121 arg_type++;
3122 target_size = thunk_type_size(arg_type, 0);
3123
3124 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3125 if (!argptr)
3126 return -TARGET_EFAULT;
3127 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3128 unlock_user(argptr, arg, 0);
3129
3130 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3131 target_ifc_len = host_ifconf->ifc_len;
3132 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3133
3134 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3135 nb_ifreq = target_ifc_len / target_ifreq_size;
3136 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3137
3138 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3139 if (outbufsz > MAX_STRUCT_SIZE) {
3140 /* We can't fit all the extents into the fixed size buffer.
3141 * Allocate one that is large enough and use it instead.
3142 */
3143 host_ifconf = malloc(outbufsz);
3144 if (!host_ifconf) {
3145 return -TARGET_ENOMEM;
3146 }
3147 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3148 free_buf = 1;
3149 }
3150 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3151
3152 host_ifconf->ifc_len = host_ifc_len;
3153 host_ifconf->ifc_buf = host_ifc_buf;
3154
3155 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3156 if (!is_error(ret)) {
3157 /* convert host ifc_len to target ifc_len */
3158
3159 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3160 target_ifc_len = nb_ifreq * target_ifreq_size;
3161 host_ifconf->ifc_len = target_ifc_len;
3162
3163 /* restore target ifc_buf */
3164
3165 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3166
3167 /* copy struct ifconf to target user */
3168
3169 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3170 if (!argptr)
3171 return -TARGET_EFAULT;
3172 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3173 unlock_user(argptr, arg, target_size);
3174
3175 /* copy ifreq[] to target user */
3176
3177 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3178 for (i = 0; i < nb_ifreq ; i++) {
3179 thunk_convert(argptr + i * target_ifreq_size,
3180 host_ifc_buf + i * sizeof(struct ifreq),
3181 ifreq_arg_type, THUNK_TARGET);
3182 }
3183 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3184 }
3185
3186 if (free_buf) {
3187 free(host_ifconf);
3188 }
3189
3190 return ret;
3191}
3192
blueswir19f106a72008-10-05 10:52:52 +00003193static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00003194#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003195 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3196#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3197 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00003198#include "ioctls.h"
3199 { 0, 0, },
3200};
3201
pbrook53a59602006-03-25 19:31:22 +00003202/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00003203/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003204static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00003205{
3206 const IOCTLEntry *ie;
3207 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00003208 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003209 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00003210 int target_size;
3211 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00003212
3213 ie = ioctl_entries;
3214 for(;;) {
3215 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003216 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003217 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003218 }
3219 if (ie->target_cmd == cmd)
3220 break;
3221 ie++;
3222 }
3223 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003224#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003225 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003226#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003227 if (ie->do_ioctl) {
3228 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3229 }
3230
bellard31e31b82003-02-18 22:55:36 +00003231 switch(arg_type[0]) {
3232 case TYPE_NULL:
3233 /* no argument */
3234 ret = get_errno(ioctl(fd, ie->host_cmd));
3235 break;
3236 case TYPE_PTRVOID:
3237 case TYPE_INT:
3238 /* int argment */
3239 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3240 break;
3241 case TYPE_PTR:
3242 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00003243 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00003244 switch(ie->access) {
3245 case IOC_R:
3246 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3247 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003248 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3249 if (!argptr)
3250 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003251 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3252 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003253 }
3254 break;
3255 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00003256 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3257 if (!argptr)
3258 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003259 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3260 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003261 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3262 break;
3263 default:
3264 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00003265 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3266 if (!argptr)
3267 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003268 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3269 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003270 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3271 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003272 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3273 if (!argptr)
3274 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003275 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3276 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003277 }
3278 break;
3279 }
3280 break;
3281 default:
j_mayer32407102007-09-26 23:01:49 +00003282 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3283 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003284 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003285 break;
3286 }
3287 return ret;
3288}
3289
blueswir1b39bc502008-10-05 10:51:10 +00003290static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003291 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3292 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3293 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3294 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3295 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3296 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3297 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3298 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3299 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3300 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3301 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3302 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3303 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3304 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3305 { 0, 0, 0, 0 }
3306};
3307
blueswir1b39bc502008-10-05 10:51:10 +00003308static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003309 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3310 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3311 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3312 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3313 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3314 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3315 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3316 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3317 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3318 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3319 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3320 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3321 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3322 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3323 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3324 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3325 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3326 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3327 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3328 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3329 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3330 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3331 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3332 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3333 { 0, 0, 0, 0 }
3334};
3335
blueswir1b39bc502008-10-05 10:51:10 +00003336static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003337 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3338 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3339 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3340 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3341 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3342 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3343 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3344 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3345 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3346 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3347 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3348 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3349 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3350 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3351 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3352 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3353 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3354 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3355 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3356 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3357 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3358 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3359 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3360 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3361 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3362 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3363 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3364 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3365 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3366 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3367 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3368 { 0, 0, 0, 0 }
3369};
3370
blueswir1b39bc502008-10-05 10:51:10 +00003371static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003372 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3373 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3374 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3375 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3376 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3377 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3378 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3379 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3380 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3381 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3382 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3383 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3384 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3385 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3386 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3387 { 0, 0, 0, 0 }
3388};
3389
3390static void target_to_host_termios (void *dst, const void *src)
3391{
3392 struct host_termios *host = dst;
3393 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003394
ths5fafdf22007-09-16 21:08:06 +00003395 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003396 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003397 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003398 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003399 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003400 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003401 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003402 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3403 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003404
Arnaud Patard44607122009-04-21 17:39:08 +03003405 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003406 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3407 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003408 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003409 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003410 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003411 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003412 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003413 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003414 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003415 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3416 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003417 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3418 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3419 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3420 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3421 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003422 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003423}
ths3b46e622007-09-17 08:09:54 +00003424
bellard31e31b82003-02-18 22:55:36 +00003425static void host_to_target_termios (void *dst, const void *src)
3426{
3427 struct target_termios *target = dst;
3428 const struct host_termios *host = src;
3429
ths5fafdf22007-09-16 21:08:06 +00003430 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003431 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003432 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003433 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003434 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003435 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003436 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003437 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3438 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003439
Arnaud Patard44607122009-04-21 17:39:08 +03003440 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003441 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3442 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3443 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3444 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3445 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3446 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3447 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3448 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3449 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3450 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3451 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3452 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3453 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3454 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3455 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3456 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3457 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3458}
3459
blueswir18e853dc2008-10-05 10:49:32 +00003460static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003461 .convert = { host_to_target_termios, target_to_host_termios },
3462 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3463 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3464};
3465
bellard5286db72003-06-05 00:57:30 +00003466static bitmask_transtbl mmap_flags_tbl[] = {
3467 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3468 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3469 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3470 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3471 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3472 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3473 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3474 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3475 { 0, 0, 0, 0 }
3476};
3477
bellard2ab83ea2003-06-15 19:56:46 +00003478#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003479
3480/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003481static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003482
bellard03acab62007-11-11 14:57:14 +00003483static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003484{
3485 int size;
pbrook53a59602006-03-25 19:31:22 +00003486 void *p;
bellard6dbad632003-03-16 18:05:05 +00003487
3488 if (!ldt_table)
3489 return 0;
3490 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3491 if (size > bytecount)
3492 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003493 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3494 if (!p)
bellard03acab62007-11-11 14:57:14 +00003495 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003496 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003497 memcpy(p, ldt_table, size);
3498 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003499 return size;
3500}
3501
3502/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003503static abi_long write_ldt(CPUX86State *env,
3504 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003505{
3506 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003507 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003508 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003509 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003510 uint32_t *lp, entry_1, entry_2;
3511
3512 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003513 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003514 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003515 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003516 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3517 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3518 ldt_info.limit = tswap32(target_ldt_info->limit);
3519 ldt_info.flags = tswap32(target_ldt_info->flags);
3520 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003521
bellard6dbad632003-03-16 18:05:05 +00003522 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003523 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003524 seg_32bit = ldt_info.flags & 1;
3525 contents = (ldt_info.flags >> 1) & 3;
3526 read_exec_only = (ldt_info.flags >> 3) & 1;
3527 limit_in_pages = (ldt_info.flags >> 4) & 1;
3528 seg_not_present = (ldt_info.flags >> 5) & 1;
3529 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003530#ifdef TARGET_ABI32
3531 lm = 0;
3532#else
3533 lm = (ldt_info.flags >> 7) & 1;
3534#endif
bellard6dbad632003-03-16 18:05:05 +00003535 if (contents == 3) {
3536 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003537 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003538 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003539 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003540 }
3541 /* allocate the LDT */
3542 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003543 env->ldt.base = target_mmap(0,
3544 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3545 PROT_READ|PROT_WRITE,
3546 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3547 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003548 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003549 memset(g2h(env->ldt.base), 0,
3550 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003551 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003552 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003553 }
3554
3555 /* NOTE: same code as Linux kernel */
3556 /* Allow LDTs to be cleared by the user. */
3557 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3558 if (oldmode ||
3559 (contents == 0 &&
3560 read_exec_only == 1 &&
3561 seg_32bit == 0 &&
3562 limit_in_pages == 0 &&
3563 seg_not_present == 1 &&
3564 useable == 0 )) {
3565 entry_1 = 0;
3566 entry_2 = 0;
3567 goto install;
3568 }
3569 }
ths3b46e622007-09-17 08:09:54 +00003570
bellard6dbad632003-03-16 18:05:05 +00003571 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3572 (ldt_info.limit & 0x0ffff);
3573 entry_2 = (ldt_info.base_addr & 0xff000000) |
3574 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3575 (ldt_info.limit & 0xf0000) |
3576 ((read_exec_only ^ 1) << 9) |
3577 (contents << 10) |
3578 ((seg_not_present ^ 1) << 15) |
3579 (seg_32bit << 22) |
3580 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00003581 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00003582 0x7000;
3583 if (!oldmode)
3584 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00003585
bellard6dbad632003-03-16 18:05:05 +00003586 /* Install the new entry ... */
3587install:
3588 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3589 lp[0] = tswap32(entry_1);
3590 lp[1] = tswap32(entry_2);
3591 return 0;
3592}
3593
3594/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00003595static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3596 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003597{
bellard03acab62007-11-11 14:57:14 +00003598 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00003599
bellard6dbad632003-03-16 18:05:05 +00003600 switch (func) {
3601 case 0:
3602 ret = read_ldt(ptr, bytecount);
3603 break;
3604 case 1:
3605 ret = write_ldt(env, ptr, bytecount, 1);
3606 break;
3607 case 0x11:
3608 ret = write_ldt(env, ptr, bytecount, 0);
3609 break;
bellard03acab62007-11-11 14:57:14 +00003610 default:
3611 ret = -TARGET_ENOSYS;
3612 break;
bellard6dbad632003-03-16 18:05:05 +00003613 }
3614 return ret;
3615}
bellard1b6b0292003-03-22 17:31:38 +00003616
blueswir14583f582008-08-24 10:35:55 +00003617#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00003618static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003619{
3620 uint64_t *gdt_table = g2h(env->gdt.base);
3621 struct target_modify_ldt_ldt_s ldt_info;
3622 struct target_modify_ldt_ldt_s *target_ldt_info;
3623 int seg_32bit, contents, read_exec_only, limit_in_pages;
3624 int seg_not_present, useable, lm;
3625 uint32_t *lp, entry_1, entry_2;
3626 int i;
3627
3628 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3629 if (!target_ldt_info)
3630 return -TARGET_EFAULT;
3631 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3632 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3633 ldt_info.limit = tswap32(target_ldt_info->limit);
3634 ldt_info.flags = tswap32(target_ldt_info->flags);
3635 if (ldt_info.entry_number == -1) {
3636 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3637 if (gdt_table[i] == 0) {
3638 ldt_info.entry_number = i;
3639 target_ldt_info->entry_number = tswap32(i);
3640 break;
3641 }
3642 }
3643 }
3644 unlock_user_struct(target_ldt_info, ptr, 1);
3645
3646 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3647 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3648 return -TARGET_EINVAL;
3649 seg_32bit = ldt_info.flags & 1;
3650 contents = (ldt_info.flags >> 1) & 3;
3651 read_exec_only = (ldt_info.flags >> 3) & 1;
3652 limit_in_pages = (ldt_info.flags >> 4) & 1;
3653 seg_not_present = (ldt_info.flags >> 5) & 1;
3654 useable = (ldt_info.flags >> 6) & 1;
3655#ifdef TARGET_ABI32
3656 lm = 0;
3657#else
3658 lm = (ldt_info.flags >> 7) & 1;
3659#endif
3660
3661 if (contents == 3) {
3662 if (seg_not_present == 0)
3663 return -TARGET_EINVAL;
3664 }
3665
3666 /* NOTE: same code as Linux kernel */
3667 /* Allow LDTs to be cleared by the user. */
3668 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3669 if ((contents == 0 &&
3670 read_exec_only == 1 &&
3671 seg_32bit == 0 &&
3672 limit_in_pages == 0 &&
3673 seg_not_present == 1 &&
3674 useable == 0 )) {
3675 entry_1 = 0;
3676 entry_2 = 0;
3677 goto install;
3678 }
3679 }
3680
3681 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3682 (ldt_info.limit & 0x0ffff);
3683 entry_2 = (ldt_info.base_addr & 0xff000000) |
3684 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3685 (ldt_info.limit & 0xf0000) |
3686 ((read_exec_only ^ 1) << 9) |
3687 (contents << 10) |
3688 ((seg_not_present ^ 1) << 15) |
3689 (seg_32bit << 22) |
3690 (limit_in_pages << 23) |
3691 (useable << 20) |
3692 (lm << 21) |
3693 0x7000;
3694
3695 /* Install the new entry ... */
3696install:
3697 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3698 lp[0] = tswap32(entry_1);
3699 lp[1] = tswap32(entry_2);
3700 return 0;
3701}
3702
blueswir18fcd3692008-08-17 20:26:25 +00003703static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003704{
3705 struct target_modify_ldt_ldt_s *target_ldt_info;
3706 uint64_t *gdt_table = g2h(env->gdt.base);
3707 uint32_t base_addr, limit, flags;
3708 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3709 int seg_not_present, useable, lm;
3710 uint32_t *lp, entry_1, entry_2;
3711
3712 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3713 if (!target_ldt_info)
3714 return -TARGET_EFAULT;
3715 idx = tswap32(target_ldt_info->entry_number);
3716 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3717 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3718 unlock_user_struct(target_ldt_info, ptr, 1);
3719 return -TARGET_EINVAL;
3720 }
3721 lp = (uint32_t *)(gdt_table + idx);
3722 entry_1 = tswap32(lp[0]);
3723 entry_2 = tswap32(lp[1]);
3724
3725 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3726 contents = (entry_2 >> 10) & 3;
3727 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3728 seg_32bit = (entry_2 >> 22) & 1;
3729 limit_in_pages = (entry_2 >> 23) & 1;
3730 useable = (entry_2 >> 20) & 1;
3731#ifdef TARGET_ABI32
3732 lm = 0;
3733#else
3734 lm = (entry_2 >> 21) & 1;
3735#endif
3736 flags = (seg_32bit << 0) | (contents << 1) |
3737 (read_exec_only << 3) | (limit_in_pages << 4) |
3738 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3739 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3740 base_addr = (entry_1 >> 16) |
3741 (entry_2 & 0xff000000) |
3742 ((entry_2 & 0xff) << 16);
3743 target_ldt_info->base_addr = tswapl(base_addr);
3744 target_ldt_info->limit = tswap32(limit);
3745 target_ldt_info->flags = tswap32(flags);
3746 unlock_user_struct(target_ldt_info, ptr, 1);
3747 return 0;
3748}
blueswir14583f582008-08-24 10:35:55 +00003749#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00003750
bellardd2fd1af2007-11-14 18:08:56 +00003751#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00003752static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00003753{
3754 abi_long ret;
3755 abi_ulong val;
3756 int idx;
3757
3758 switch(code) {
3759 case TARGET_ARCH_SET_GS:
3760 case TARGET_ARCH_SET_FS:
3761 if (code == TARGET_ARCH_SET_GS)
3762 idx = R_GS;
3763 else
3764 idx = R_FS;
3765 cpu_x86_load_seg(env, idx, 0);
3766 env->segs[idx].base = addr;
3767 break;
3768 case TARGET_ARCH_GET_GS:
3769 case TARGET_ARCH_GET_FS:
3770 if (code == TARGET_ARCH_GET_GS)
3771 idx = R_GS;
3772 else
3773 idx = R_FS;
3774 val = env->segs[idx].base;
3775 if (put_user(val, addr, abi_ulong))
3776 return -TARGET_EFAULT;
3777 break;
3778 default:
3779 ret = -TARGET_EINVAL;
3780 break;
3781 }
3782 return 0;
3783}
3784#endif
3785
bellard2ab83ea2003-06-15 19:56:46 +00003786#endif /* defined(TARGET_I386) */
3787
Riku Voipio05098a92011-03-04 15:27:29 +02003788#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00003789
Riku Voipio05098a92011-03-04 15:27:29 +02003790#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003791
3792static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3793typedef struct {
3794 CPUState *env;
3795 pthread_mutex_t mutex;
3796 pthread_cond_t cond;
3797 pthread_t thread;
3798 uint32_t tid;
3799 abi_ulong child_tidptr;
3800 abi_ulong parent_tidptr;
3801 sigset_t sigmask;
3802} new_thread_info;
3803
3804static void *clone_func(void *arg)
3805{
3806 new_thread_info *info = arg;
3807 CPUState *env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003808 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00003809
3810 env = info->env;
3811 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003812 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00003813 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003814 env->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003815 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00003816 if (info->child_tidptr)
3817 put_user_u32(info->tid, info->child_tidptr);
3818 if (info->parent_tidptr)
3819 put_user_u32(info->tid, info->parent_tidptr);
3820 /* Enable signals. */
3821 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3822 /* Signal to the parent that we're ready. */
3823 pthread_mutex_lock(&info->mutex);
3824 pthread_cond_broadcast(&info->cond);
3825 pthread_mutex_unlock(&info->mutex);
3826 /* Wait until the parent has finshed initializing the tls state. */
3827 pthread_mutex_lock(&clone_lock);
3828 pthread_mutex_unlock(&clone_lock);
3829 cpu_loop(env);
3830 /* never exits */
3831 return NULL;
3832}
3833#else
bellard1b6b0292003-03-22 17:31:38 +00003834
3835static int clone_func(void *arg)
3836{
bellard2ab83ea2003-06-15 19:56:46 +00003837 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00003838 cpu_loop(env);
3839 /* never exits */
3840 return 0;
3841}
pbrookd865bab2008-06-07 22:12:17 +00003842#endif
bellard1b6b0292003-03-22 17:31:38 +00003843
ths0da46a62007-10-20 20:23:07 +00003844/* do_fork() Must return host values and target errnos (unlike most
3845 do_*() functions). */
pbrookd865bab2008-06-07 22:12:17 +00003846static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3847 abi_ulong parent_tidptr, target_ulong newtls,
3848 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00003849{
3850 int ret;
bellard5cd43932003-03-29 16:54:36 +00003851 TaskState *ts;
bellard2ab83ea2003-06-15 19:56:46 +00003852 CPUState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003853#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003854 unsigned int nptl_flags;
3855 sigset_t sigmask;
Riku Voipio91907492010-11-26 16:21:34 +02003856#else
3857 uint8_t *new_stack;
pbrookd865bab2008-06-07 22:12:17 +00003858#endif
ths3b46e622007-09-17 08:09:54 +00003859
balrog436d1242008-09-21 02:39:45 +00003860 /* Emulate vfork() with fork() */
3861 if (flags & CLONE_VFORK)
3862 flags &= ~(CLONE_VFORK | CLONE_VM);
3863
bellard1b6b0292003-03-22 17:31:38 +00003864 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003865 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003866#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003867 new_thread_info info;
3868 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00003869#endif
Nathan Froyd48e15fc2010-10-29 07:48:57 -07003870 ts = qemu_mallocz(sizeof(TaskState));
pbrook624f7972008-05-31 16:11:38 +00003871 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00003872 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00003873 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00003874#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3875 cpu_reset(new_env);
3876#endif
pbrook6e68e072008-05-30 17:22:15 +00003877 /* Init regs that differ from the parent. */
3878 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00003879 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003880 ts->bprm = parent_ts->bprm;
3881 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003882#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003883 nptl_flags = flags;
3884 flags &= ~CLONE_NPTL_FLAGS2;
3885
pbrookc2764712009-03-07 15:24:59 +00003886 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3887 ts->child_tidptr = child_tidptr;
3888 }
3889
pbrookd865bab2008-06-07 22:12:17 +00003890 if (nptl_flags & CLONE_SETTLS)
3891 cpu_set_tls (new_env, newtls);
3892
3893 /* Grab a mutex so that thread setup appears atomic. */
3894 pthread_mutex_lock(&clone_lock);
3895
3896 memset(&info, 0, sizeof(info));
3897 pthread_mutex_init(&info.mutex, NULL);
3898 pthread_mutex_lock(&info.mutex);
3899 pthread_cond_init(&info.cond, NULL);
3900 info.env = new_env;
3901 if (nptl_flags & CLONE_CHILD_SETTID)
3902 info.child_tidptr = child_tidptr;
3903 if (nptl_flags & CLONE_PARENT_SETTID)
3904 info.parent_tidptr = parent_tidptr;
3905
3906 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07003907 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
3908 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00003909 /* It is not safe to deliver signals until the child has finished
3910 initializing, so temporarily block all signals. */
3911 sigfillset(&sigmask);
3912 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3913
3914 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00003915 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00003916
3917 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3918 pthread_attr_destroy(&attr);
3919 if (ret == 0) {
3920 /* Wait for the child to initialize. */
3921 pthread_cond_wait(&info.cond, &info.mutex);
3922 ret = info.tid;
3923 if (flags & CLONE_PARENT_SETTID)
3924 put_user_u32(ret, parent_tidptr);
3925 } else {
3926 ret = -1;
3927 }
3928 pthread_mutex_unlock(&info.mutex);
3929 pthread_cond_destroy(&info.cond);
3930 pthread_mutex_destroy(&info.mutex);
3931 pthread_mutex_unlock(&clone_lock);
3932#else
3933 if (flags & CLONE_NPTL_FLAGS2)
3934 return -EINVAL;
3935 /* This is probably going to die very quickly, but do it anyway. */
Nathan Froyd48e15fc2010-10-29 07:48:57 -07003936 new_stack = qemu_mallocz (NEW_STACK_SIZE);
bellard27725c12003-04-29 21:08:18 +00003937#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02003938 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00003939#else
3940 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3941#endif
pbrookd865bab2008-06-07 22:12:17 +00003942#endif
bellard1b6b0292003-03-22 17:31:38 +00003943 } else {
3944 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00003945 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00003946 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00003947 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00003948 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00003949 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00003950 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00003951 cpu_clone_regs(env, newsp);
3952 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02003953#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00003954 /* There is a race condition here. The parent process could
3955 theoretically read the TID in the child process before the child
3956 tid is set. This would require using either ptrace
3957 (not implemented) or having *_tidptr to point at a shared memory
3958 mapping. We can't repeat the spinlock hack used above because
3959 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00003960 if (flags & CLONE_CHILD_SETTID)
3961 put_user_u32(gettid(), child_tidptr);
3962 if (flags & CLONE_PARENT_SETTID)
3963 put_user_u32(gettid(), parent_tidptr);
3964 ts = (TaskState *)env->opaque;
3965 if (flags & CLONE_SETTLS)
3966 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00003967 if (flags & CLONE_CHILD_CLEARTID)
3968 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00003969#endif
pbrookd865bab2008-06-07 22:12:17 +00003970 } else {
3971 fork_end(0);
3972 }
bellard1b6b0292003-03-22 17:31:38 +00003973 }
3974 return ret;
3975}
3976
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003977/* warning : doesn't handle linux specific flags... */
3978static int target_to_host_fcntl_cmd(int cmd)
3979{
3980 switch(cmd) {
3981 case TARGET_F_DUPFD:
3982 case TARGET_F_GETFD:
3983 case TARGET_F_SETFD:
3984 case TARGET_F_GETFL:
3985 case TARGET_F_SETFL:
3986 return cmd;
3987 case TARGET_F_GETLK:
3988 return F_GETLK;
3989 case TARGET_F_SETLK:
3990 return F_SETLK;
3991 case TARGET_F_SETLKW:
3992 return F_SETLKW;
3993 case TARGET_F_GETOWN:
3994 return F_GETOWN;
3995 case TARGET_F_SETOWN:
3996 return F_SETOWN;
3997 case TARGET_F_GETSIG:
3998 return F_GETSIG;
3999 case TARGET_F_SETSIG:
4000 return F_SETSIG;
4001#if TARGET_ABI_BITS == 32
4002 case TARGET_F_GETLK64:
4003 return F_GETLK64;
4004 case TARGET_F_SETLK64:
4005 return F_SETLK64;
4006 case TARGET_F_SETLKW64:
4007 return F_SETLKW64;
4008#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004009 case TARGET_F_SETLEASE:
4010 return F_SETLEASE;
4011 case TARGET_F_GETLEASE:
4012 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004013#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004014 case TARGET_F_DUPFD_CLOEXEC:
4015 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004016#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004017 case TARGET_F_NOTIFY:
4018 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004019 default:
4020 return -TARGET_EINVAL;
4021 }
4022 return -TARGET_EINVAL;
4023}
4024
blueswir1992f48a2007-10-14 16:27:31 +00004025static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004026{
4027 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004028 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004029 struct flock64 fl64;
4030 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00004031 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004032 int host_cmd = target_to_host_fcntl_cmd(cmd);
4033
4034 if (host_cmd == -TARGET_EINVAL)
4035 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004036
bellard7775e9e2003-05-14 22:46:48 +00004037 switch(cmd) {
4038 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004039 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4040 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00004041 fl.l_type = tswap16(target_fl->l_type);
4042 fl.l_whence = tswap16(target_fl->l_whence);
4043 fl.l_start = tswapl(target_fl->l_start);
4044 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004045 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004046 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004047 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004048 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004049 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4050 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00004051 target_fl->l_type = tswap16(fl.l_type);
4052 target_fl->l_whence = tswap16(fl.l_whence);
4053 target_fl->l_start = tswapl(fl.l_start);
4054 target_fl->l_len = tswapl(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004055 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004056 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004057 }
4058 break;
ths3b46e622007-09-17 08:09:54 +00004059
bellard7775e9e2003-05-14 22:46:48 +00004060 case TARGET_F_SETLK:
4061 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004062 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4063 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00004064 fl.l_type = tswap16(target_fl->l_type);
4065 fl.l_whence = tswap16(target_fl->l_whence);
4066 fl.l_start = tswapl(target_fl->l_start);
4067 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004068 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004069 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004070 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004071 break;
ths3b46e622007-09-17 08:09:54 +00004072
bellard7775e9e2003-05-14 22:46:48 +00004073 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004074 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4075 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00004076 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4077 fl64.l_whence = tswap16(target_fl64->l_whence);
4078 fl64.l_start = tswapl(target_fl64->l_start);
4079 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004080 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004081 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004082 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004083 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004084 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4085 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00004086 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
4087 target_fl64->l_whence = tswap16(fl64.l_whence);
4088 target_fl64->l_start = tswapl(fl64.l_start);
4089 target_fl64->l_len = tswapl(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004090 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004091 unlock_user_struct(target_fl64, arg, 1);
4092 }
bellard9ee1fa22007-11-11 15:11:19 +00004093 break;
bellard7775e9e2003-05-14 22:46:48 +00004094 case TARGET_F_SETLK64:
4095 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004096 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4097 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00004098 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
4099 fl64.l_whence = tswap16(target_fl64->l_whence);
4100 fl64.l_start = tswapl(target_fl64->l_start);
4101 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004102 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004103 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004104 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004105 break;
4106
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004107 case TARGET_F_GETFL:
4108 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004109 if (ret >= 0) {
4110 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4111 }
bellardffa65c32004-01-04 23:57:22 +00004112 break;
4113
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004114 case TARGET_F_SETFL:
4115 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4116 break;
4117
4118 case TARGET_F_SETOWN:
4119 case TARGET_F_GETOWN:
4120 case TARGET_F_SETSIG:
4121 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004122 case TARGET_F_SETLEASE:
4123 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004124 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00004125 break;
4126
bellard7775e9e2003-05-14 22:46:48 +00004127 default:
bellard9ee1fa22007-11-11 15:11:19 +00004128 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00004129 break;
4130 }
4131 return ret;
4132}
4133
bellard67867302003-11-23 17:05:30 +00004134#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00004135
bellard67867302003-11-23 17:05:30 +00004136static inline int high2lowuid(int uid)
4137{
4138 if (uid > 65535)
4139 return 65534;
4140 else
4141 return uid;
4142}
4143
4144static inline int high2lowgid(int gid)
4145{
4146 if (gid > 65535)
4147 return 65534;
4148 else
4149 return gid;
4150}
4151
4152static inline int low2highuid(int uid)
4153{
4154 if ((int16_t)uid == -1)
4155 return -1;
4156 else
4157 return uid;
4158}
4159
4160static inline int low2highgid(int gid)
4161{
4162 if ((int16_t)gid == -1)
4163 return -1;
4164 else
4165 return gid;
4166}
4167
4168#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004169
bellard31e31b82003-02-18 22:55:36 +00004170void syscall_init(void)
4171{
bellard2ab83ea2003-06-15 19:56:46 +00004172 IOCTLEntry *ie;
4173 const argtype *arg_type;
4174 int size;
thsb92c47c2007-11-01 00:07:38 +00004175 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004176
Blue Swirl001faf32009-05-13 17:53:17 +00004177#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004178#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004179#include "syscall_types.h"
4180#undef STRUCT
4181#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00004182
4183 /* we patch the ioctl size if necessary. We rely on the fact that
4184 no ioctl has all the bits at '1' in the size field */
4185 ie = ioctl_entries;
4186 while (ie->target_cmd != 0) {
4187 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4188 TARGET_IOC_SIZEMASK) {
4189 arg_type = ie->arg_type;
4190 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00004191 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00004192 ie->target_cmd);
4193 exit(1);
4194 }
4195 arg_type++;
4196 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00004197 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00004198 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4199 (size << TARGET_IOC_SIZESHIFT);
4200 }
thsb92c47c2007-11-01 00:07:38 +00004201
4202 /* Build target_to_host_errno_table[] table from
4203 * host_to_target_errno_table[]. */
4204 for (i=0; i < ERRNO_TABLE_SIZE; i++)
4205 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4206
bellard2ab83ea2003-06-15 19:56:46 +00004207 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00004208#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4209 (defined(__x86_64__) && defined(TARGET_X86_64))
4210 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4211 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4212 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00004213 }
4214#endif
4215 ie++;
4216 }
bellard31e31b82003-02-18 22:55:36 +00004217}
bellardc573ff62004-01-04 15:51:36 +00004218
blueswir1992f48a2007-10-14 16:27:31 +00004219#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004220static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4221{
thsaf325d32008-06-10 15:29:15 +00004222#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004223 return ((uint64_t)word0 << 32) | word1;
4224#else
4225 return ((uint64_t)word1 << 32) | word0;
4226#endif
4227}
blueswir1992f48a2007-10-14 16:27:31 +00004228#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004229static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4230{
4231 return word0;
4232}
blueswir1992f48a2007-10-14 16:27:31 +00004233#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004234
4235#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004236static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4237 abi_long arg2,
4238 abi_long arg3,
4239 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004240{
4241#ifdef TARGET_ARM
4242 if (((CPUARMState *)cpu_env)->eabi)
4243 {
4244 arg2 = arg3;
4245 arg3 = arg4;
4246 }
4247#endif
4248 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4249}
4250#endif
4251
4252#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004253static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4254 abi_long arg2,
4255 abi_long arg3,
4256 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004257{
4258#ifdef TARGET_ARM
4259 if (((CPUARMState *)cpu_env)->eabi)
4260 {
4261 arg2 = arg3;
4262 arg3 = arg4;
4263 }
4264#endif
4265 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4266}
4267#endif
4268
bellard579a97f2007-11-11 14:26:47 +00004269static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4270 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004271{
4272 struct target_timespec *target_ts;
4273
bellard579a97f2007-11-11 14:26:47 +00004274 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4275 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004276 host_ts->tv_sec = tswapl(target_ts->tv_sec);
4277 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
4278 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004279 return 0;
pbrook53a59602006-03-25 19:31:22 +00004280}
4281
bellard579a97f2007-11-11 14:26:47 +00004282static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4283 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004284{
4285 struct target_timespec *target_ts;
4286
bellard579a97f2007-11-11 14:26:47 +00004287 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4288 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004289 target_ts->tv_sec = tswapl(host_ts->tv_sec);
4290 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
4291 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004292 return 0;
pbrook53a59602006-03-25 19:31:22 +00004293}
4294
aurel329d33b762009-04-08 23:07:05 +00004295#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004296static inline abi_long host_to_target_stat64(void *cpu_env,
4297 abi_ulong target_addr,
4298 struct stat *host_st)
4299{
4300#ifdef TARGET_ARM
4301 if (((CPUARMState *)cpu_env)->eabi) {
4302 struct target_eabi_stat64 *target_st;
4303
4304 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4305 return -TARGET_EFAULT;
4306 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4307 __put_user(host_st->st_dev, &target_st->st_dev);
4308 __put_user(host_st->st_ino, &target_st->st_ino);
4309#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4310 __put_user(host_st->st_ino, &target_st->__st_ino);
4311#endif
4312 __put_user(host_st->st_mode, &target_st->st_mode);
4313 __put_user(host_st->st_nlink, &target_st->st_nlink);
4314 __put_user(host_st->st_uid, &target_st->st_uid);
4315 __put_user(host_st->st_gid, &target_st->st_gid);
4316 __put_user(host_st->st_rdev, &target_st->st_rdev);
4317 __put_user(host_st->st_size, &target_st->st_size);
4318 __put_user(host_st->st_blksize, &target_st->st_blksize);
4319 __put_user(host_st->st_blocks, &target_st->st_blocks);
4320 __put_user(host_st->st_atime, &target_st->target_st_atime);
4321 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4322 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4323 unlock_user_struct(target_st, target_addr, 1);
4324 } else
4325#endif
4326 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004327#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004328 struct target_stat *target_st;
4329#else
balrog6a24a772008-09-20 02:23:36 +00004330 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004331#endif
balrog6a24a772008-09-20 02:23:36 +00004332
4333 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4334 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004335 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004336 __put_user(host_st->st_dev, &target_st->st_dev);
4337 __put_user(host_st->st_ino, &target_st->st_ino);
4338#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4339 __put_user(host_st->st_ino, &target_st->__st_ino);
4340#endif
4341 __put_user(host_st->st_mode, &target_st->st_mode);
4342 __put_user(host_st->st_nlink, &target_st->st_nlink);
4343 __put_user(host_st->st_uid, &target_st->st_uid);
4344 __put_user(host_st->st_gid, &target_st->st_gid);
4345 __put_user(host_st->st_rdev, &target_st->st_rdev);
4346 /* XXX: better use of kernel struct */
4347 __put_user(host_st->st_size, &target_st->st_size);
4348 __put_user(host_st->st_blksize, &target_st->st_blksize);
4349 __put_user(host_st->st_blocks, &target_st->st_blocks);
4350 __put_user(host_st->st_atime, &target_st->target_st_atime);
4351 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4352 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4353 unlock_user_struct(target_st, target_addr, 1);
4354 }
4355
4356 return 0;
4357}
4358#endif
4359
Juan Quintela2f7bb872009-07-27 16:13:24 +02004360#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004361/* ??? Using host futex calls even when target atomic operations
4362 are not really atomic probably breaks things. However implementing
4363 futexes locally would make futexes shared between multiple processes
4364 tricky. However they're probably useless because guest atomic
4365 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004366static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4367 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004368{
4369 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004370 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004371
4372 /* ??? We assume FUTEX_* constants are the same on both host
4373 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004374#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004375 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004376#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004377 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004378#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004379 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004380 case FUTEX_WAIT:
4381 if (timeout) {
4382 pts = &ts;
4383 target_to_host_timespec(pts, timeout);
4384 } else {
4385 pts = NULL;
4386 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004387 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004388 pts, NULL, 0));
4389 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004390 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004391 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004392 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004393 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004394 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004395 case FUTEX_WAKE_OP:
4396 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4397 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4398 But the prototype takes a `struct timespec *'; insert casts
4399 to satisfy the compiler. We do not need to tswap TIMEOUT
4400 since it's not compared to guest memory. */
4401 pts = (struct timespec *)(uintptr_t) timeout;
4402 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4403 g2h(uaddr2),
4404 (base_op == FUTEX_CMP_REQUEUE
4405 ? tswap32(val3)
4406 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004407 default:
4408 return -TARGET_ENOSYS;
4409 }
4410}
4411#endif
4412
pbrook1d9d8b52009-04-16 15:17:02 +00004413/* Map host to target signal numbers for the wait family of syscalls.
4414 Assume all other status bits are the same. */
4415static int host_to_target_waitstatus(int status)
4416{
4417 if (WIFSIGNALED(status)) {
4418 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4419 }
4420 if (WIFSTOPPED(status)) {
4421 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4422 | (status & 0xff);
4423 }
4424 return status;
4425}
4426
pbrooka745ec62008-05-06 15:36:17 +00004427int get_osversion(void)
4428{
4429 static int osversion;
4430 struct new_utsname buf;
4431 const char *s;
4432 int i, n, tmp;
4433 if (osversion)
4434 return osversion;
4435 if (qemu_uname_release && *qemu_uname_release) {
4436 s = qemu_uname_release;
4437 } else {
4438 if (sys_uname(&buf))
4439 return 0;
4440 s = buf.release;
4441 }
4442 tmp = 0;
4443 for (i = 0; i < 3; i++) {
4444 n = 0;
4445 while (*s >= '0' && *s <= '9') {
4446 n *= 10;
4447 n += *s - '0';
4448 s++;
4449 }
4450 tmp = (tmp << 8) + n;
4451 if (*s == '.')
4452 s++;
4453 }
4454 osversion = tmp;
4455 return osversion;
4456}
4457
ths0da46a62007-10-20 20:23:07 +00004458/* do_syscall() should always have a single exit point at the end so
4459 that actions, such as logging of syscall results, can be performed.
4460 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00004461abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4462 abi_long arg2, abi_long arg3, abi_long arg4,
4463 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00004464{
blueswir1992f48a2007-10-14 16:27:31 +00004465 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004466 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00004467 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00004468 void *p;
ths3b46e622007-09-17 08:09:54 +00004469
bellard72f03902003-02-18 23:33:18 +00004470#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00004471 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00004472#endif
thsb92c47c2007-11-01 00:07:38 +00004473 if(do_strace)
4474 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4475
bellard31e31b82003-02-18 22:55:36 +00004476 switch(num) {
4477 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02004478#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00004479 /* In old applications this may be used to implement _exit(2).
4480 However in threaded applictions it is used for thread termination,
4481 and _exit_group is used for application termination.
4482 Do thread termination if we have more then one thread. */
4483 /* FIXME: This probably breaks if a signal arrives. We should probably
4484 be disabling signals. */
4485 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004486 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00004487 CPUState **lastp;
4488 CPUState *p;
4489
4490 cpu_list_lock();
4491 lastp = &first_cpu;
4492 p = first_cpu;
4493 while (p && p != (CPUState *)cpu_env) {
4494 lastp = &p->next_cpu;
4495 p = p->next_cpu;
4496 }
4497 /* If we didn't find the CPU for this thread then something is
4498 horribly wrong. */
4499 if (!p)
4500 abort();
4501 /* Remove the CPU from the list. */
4502 *lastp = p->next_cpu;
4503 cpu_list_unlock();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004504 ts = ((CPUState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00004505 if (ts->child_tidptr) {
4506 put_user_u32(0, ts->child_tidptr);
4507 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4508 NULL, NULL, 0);
4509 }
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004510 thread_env = NULL;
4511 qemu_free(cpu_env);
4512 qemu_free(ts);
pbrookc2764712009-03-07 15:24:59 +00004513 pthread_exit(NULL);
4514 }
4515#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02004516#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00004517 _mcleanup();
4518#endif
bellarde9009672005-04-26 20:42:36 +00004519 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00004520 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00004521 ret = 0; /* avoid warning */
4522 break;
4523 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00004524 if (arg3 == 0)
4525 ret = 0;
4526 else {
4527 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4528 goto efault;
4529 ret = get_errno(read(arg1, p, arg3));
4530 unlock_user(p, arg2, ret);
4531 }
bellard31e31b82003-02-18 22:55:36 +00004532 break;
4533 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00004534 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4535 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004536 ret = get_errno(write(arg1, p, arg3));
4537 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00004538 break;
4539 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00004540 if (!(p = lock_user_string(arg1)))
4541 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004542 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00004543 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4544 arg3));
pbrook53a59602006-03-25 19:31:22 +00004545 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004546 break;
ths82424832007-09-24 09:21:55 +00004547#if defined(TARGET_NR_openat) && defined(__NR_openat)
4548 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00004549 if (!(p = lock_user_string(arg2)))
4550 goto efault;
4551 ret = get_errno(sys_openat(arg1,
4552 path(p),
4553 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4554 arg4));
4555 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00004556 break;
4557#endif
bellard31e31b82003-02-18 22:55:36 +00004558 case TARGET_NR_close:
4559 ret = get_errno(close(arg1));
4560 break;
4561 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00004562 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00004563 break;
4564 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00004565 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00004566 break;
thse5febef2007-04-01 18:31:35 +00004567#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00004568 case TARGET_NR_waitpid:
4569 {
pbrook53a59602006-03-25 19:31:22 +00004570 int status;
4571 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00004572 if (!is_error(ret) && arg2
pbrook1d9d8b52009-04-16 15:17:02 +00004573 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00004574 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004575 }
4576 break;
thse5febef2007-04-01 18:31:35 +00004577#endif
pbrookf0cbb612008-05-30 18:20:05 +00004578#ifdef TARGET_NR_waitid
4579 case TARGET_NR_waitid:
4580 {
4581 siginfo_t info;
4582 info.si_pid = 0;
4583 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4584 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004585 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00004586 goto efault;
4587 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05004588 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00004589 }
4590 }
4591 break;
4592#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004593#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004594 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00004595 if (!(p = lock_user_string(arg1)))
4596 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004597 ret = get_errno(creat(p, arg2));
4598 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004599 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004600#endif
bellard31e31b82003-02-18 22:55:36 +00004601 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00004602 {
4603 void * p2;
4604 p = lock_user_string(arg1);
4605 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004606 if (!p || !p2)
4607 ret = -TARGET_EFAULT;
4608 else
4609 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004610 unlock_user(p2, arg2, 0);
4611 unlock_user(p, arg1, 0);
4612 }
bellard31e31b82003-02-18 22:55:36 +00004613 break;
ths64f0ce42007-09-24 09:25:06 +00004614#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4615 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00004616 {
4617 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00004618 if (!arg2 || !arg4)
4619 goto efault;
ths64f0ce42007-09-24 09:25:06 +00004620 p = lock_user_string(arg2);
4621 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004622 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004623 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00004624 else
4625 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00004626 unlock_user(p, arg2, 0);
4627 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00004628 }
4629 break;
4630#endif
bellard31e31b82003-02-18 22:55:36 +00004631 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00004632 if (!(p = lock_user_string(arg1)))
4633 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004634 ret = get_errno(unlink(p));
4635 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004636 break;
ths8170f562007-09-24 09:24:11 +00004637#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4638 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00004639 if (!(p = lock_user_string(arg2)))
4640 goto efault;
4641 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4642 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00004643 break;
balrogb7d35e62007-12-12 00:40:24 +00004644#endif
bellard31e31b82003-02-18 22:55:36 +00004645 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00004646 {
4647 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00004648 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00004649 abi_ulong gp;
4650 abi_ulong guest_argp;
4651 abi_ulong guest_envp;
4652 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00004653 char **q;
4654
bellardf7341ff2003-03-30 21:00:25 +00004655 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00004656 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00004657 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004658 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004659 goto efault;
ths03aa1972007-12-02 06:28:08 +00004660 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004661 break;
bellard7854b052003-03-29 17:22:23 +00004662 argc++;
bellard2f619692007-11-16 10:46:05 +00004663 }
bellardf7341ff2003-03-30 21:00:25 +00004664 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00004665 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00004666 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004667 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004668 goto efault;
ths03aa1972007-12-02 06:28:08 +00004669 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004670 break;
bellard7854b052003-03-29 17:22:23 +00004671 envc++;
bellard2f619692007-11-16 10:46:05 +00004672 }
bellard7854b052003-03-29 17:22:23 +00004673
bellardf7341ff2003-03-30 21:00:25 +00004674 argp = alloca((argc + 1) * sizeof(void *));
4675 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00004676
pbrookda94d262008-05-30 18:24:00 +00004677 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004678 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004679 if (get_user_ual(addr, gp))
4680 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004681 if (!addr)
4682 break;
bellard2f619692007-11-16 10:46:05 +00004683 if (!(*q = lock_user_string(addr)))
4684 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004685 }
bellardf7341ff2003-03-30 21:00:25 +00004686 *q = NULL;
4687
pbrookda94d262008-05-30 18:24:00 +00004688 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004689 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004690 if (get_user_ual(addr, gp))
4691 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004692 if (!addr)
4693 break;
bellard2f619692007-11-16 10:46:05 +00004694 if (!(*q = lock_user_string(addr)))
4695 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004696 }
bellardf7341ff2003-03-30 21:00:25 +00004697 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00004698
bellard2f619692007-11-16 10:46:05 +00004699 if (!(p = lock_user_string(arg1)))
4700 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004701 ret = get_errno(execve(p, argp, envp));
4702 unlock_user(p, arg1, 0);
4703
bellard2f619692007-11-16 10:46:05 +00004704 goto execve_end;
4705
4706 execve_efault:
4707 ret = -TARGET_EFAULT;
4708
4709 execve_end:
pbrook53a59602006-03-25 19:31:22 +00004710 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004711 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004712 if (get_user_ual(addr, gp)
4713 || !addr)
4714 break;
pbrook53a59602006-03-25 19:31:22 +00004715 unlock_user(*q, addr, 0);
4716 }
4717 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004718 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004719 if (get_user_ual(addr, gp)
4720 || !addr)
4721 break;
pbrook53a59602006-03-25 19:31:22 +00004722 unlock_user(*q, addr, 0);
4723 }
bellard7854b052003-03-29 17:22:23 +00004724 }
bellard31e31b82003-02-18 22:55:36 +00004725 break;
4726 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00004727 if (!(p = lock_user_string(arg1)))
4728 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004729 ret = get_errno(chdir(p));
4730 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004731 break;
bellarda315a142005-01-30 22:59:18 +00004732#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00004733 case TARGET_NR_time:
4734 {
pbrook53a59602006-03-25 19:31:22 +00004735 time_t host_time;
4736 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00004737 if (!is_error(ret)
4738 && arg1
4739 && put_user_sal(host_time, arg1))
4740 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004741 }
4742 break;
bellarda315a142005-01-30 22:59:18 +00004743#endif
bellard31e31b82003-02-18 22:55:36 +00004744 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00004745 if (!(p = lock_user_string(arg1)))
4746 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004747 ret = get_errno(mknod(p, arg2, arg3));
4748 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004749 break;
ths75ac37a2007-09-24 09:23:05 +00004750#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4751 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00004752 if (!(p = lock_user_string(arg2)))
4753 goto efault;
4754 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4755 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00004756 break;
4757#endif
bellard31e31b82003-02-18 22:55:36 +00004758 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00004759 if (!(p = lock_user_string(arg1)))
4760 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004761 ret = get_errno(chmod(p, arg2));
4762 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004763 break;
bellardebc05482003-09-30 21:08:41 +00004764#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00004765 case TARGET_NR_break:
4766 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004767#endif
4768#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00004769 case TARGET_NR_oldstat:
4770 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004771#endif
bellard31e31b82003-02-18 22:55:36 +00004772 case TARGET_NR_lseek:
4773 ret = get_errno(lseek(arg1, arg2, arg3));
4774 break;
Richard Henderson92317332010-05-03 10:07:53 -07004775#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
4776 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00004777 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07004778 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00004779 ret = get_errno(getpid());
4780 break;
Richard Henderson92317332010-05-03 10:07:53 -07004781#endif
4782#ifdef TARGET_NR_getpid
4783 case TARGET_NR_getpid:
4784 ret = get_errno(getpid());
4785 break;
4786#endif
bellard31e31b82003-02-18 22:55:36 +00004787 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00004788 {
4789 /* need to look at the data field */
4790 void *p2, *p3;
4791 p = lock_user_string(arg1);
4792 p2 = lock_user_string(arg2);
4793 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004794 if (!p || !p2 || !p3)
4795 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004796 else {
bellard579a97f2007-11-11 14:26:47 +00004797 /* FIXME - arg5 should be locked, but it isn't clear how to
4798 * do that since it's not guaranteed to be a NULL-terminated
4799 * string.
4800 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004801 if ( ! arg5 )
4802 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4803 else
4804 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4805 }
bellard579a97f2007-11-11 14:26:47 +00004806 unlock_user(p, arg1, 0);
4807 unlock_user(p2, arg2, 0);
4808 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00004809 break;
4810 }
thse5febef2007-04-01 18:31:35 +00004811#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00004812 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00004813 if (!(p = lock_user_string(arg1)))
4814 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004815 ret = get_errno(umount(p));
4816 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004817 break;
thse5febef2007-04-01 18:31:35 +00004818#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004819#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004820 case TARGET_NR_stime:
4821 {
pbrook53a59602006-03-25 19:31:22 +00004822 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00004823 if (get_user_sal(host_time, arg1))
4824 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004825 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00004826 }
4827 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004828#endif
bellard31e31b82003-02-18 22:55:36 +00004829 case TARGET_NR_ptrace:
4830 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00004831#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004832 case TARGET_NR_alarm:
4833 ret = alarm(arg1);
4834 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004835#endif
bellardebc05482003-09-30 21:08:41 +00004836#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00004837 case TARGET_NR_oldfstat:
4838 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004839#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004840#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004841 case TARGET_NR_pause:
4842 ret = get_errno(pause());
4843 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004844#endif
thse5febef2007-04-01 18:31:35 +00004845#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00004846 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00004847 {
pbrook53a59602006-03-25 19:31:22 +00004848 struct utimbuf tbuf, *host_tbuf;
4849 struct target_utimbuf *target_tbuf;
4850 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004851 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4852 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004853 tbuf.actime = tswapl(target_tbuf->actime);
4854 tbuf.modtime = tswapl(target_tbuf->modtime);
4855 unlock_user_struct(target_tbuf, arg2, 0);
4856 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00004857 } else {
pbrook53a59602006-03-25 19:31:22 +00004858 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00004859 }
bellard579a97f2007-11-11 14:26:47 +00004860 if (!(p = lock_user_string(arg1)))
4861 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004862 ret = get_errno(utime(p, host_tbuf));
4863 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00004864 }
4865 break;
thse5febef2007-04-01 18:31:35 +00004866#endif
bellard978a66f2004-12-06 22:58:05 +00004867 case TARGET_NR_utimes:
4868 {
bellard978a66f2004-12-06 22:58:05 +00004869 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00004870 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00004871 if (copy_from_user_timeval(&tv[0], arg2)
4872 || copy_from_user_timeval(&tv[1],
4873 arg2 + sizeof(struct target_timeval)))
4874 goto efault;
bellard978a66f2004-12-06 22:58:05 +00004875 tvp = tv;
4876 } else {
4877 tvp = NULL;
4878 }
bellard579a97f2007-11-11 14:26:47 +00004879 if (!(p = lock_user_string(arg1)))
4880 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004881 ret = get_errno(utimes(p, tvp));
4882 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00004883 }
4884 break;
balrogac8a6552008-09-20 02:25:39 +00004885#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4886 case TARGET_NR_futimesat:
4887 {
4888 struct timeval *tvp, tv[2];
4889 if (arg3) {
4890 if (copy_from_user_timeval(&tv[0], arg3)
4891 || copy_from_user_timeval(&tv[1],
4892 arg3 + sizeof(struct target_timeval)))
4893 goto efault;
4894 tvp = tv;
4895 } else {
4896 tvp = NULL;
4897 }
4898 if (!(p = lock_user_string(arg2)))
4899 goto efault;
4900 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4901 unlock_user(p, arg2, 0);
4902 }
4903 break;
4904#endif
bellardebc05482003-09-30 21:08:41 +00004905#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00004906 case TARGET_NR_stty:
4907 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004908#endif
4909#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00004910 case TARGET_NR_gtty:
4911 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004912#endif
bellard31e31b82003-02-18 22:55:36 +00004913 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00004914 if (!(p = lock_user_string(arg1)))
4915 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02004916 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00004917 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004918 break;
ths92a34c12007-09-24 09:27:49 +00004919#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4920 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00004921 if (!(p = lock_user_string(arg2)))
4922 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00004923 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00004924 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00004925 break;
4926#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004927#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004928 case TARGET_NR_nice:
4929 ret = get_errno(nice(arg1));
4930 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004931#endif
bellardebc05482003-09-30 21:08:41 +00004932#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00004933 case TARGET_NR_ftime:
4934 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004935#endif
bellard31e31b82003-02-18 22:55:36 +00004936 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00004937 sync();
4938 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00004939 break;
4940 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00004941 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00004942 break;
4943 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00004944 {
4945 void *p2;
4946 p = lock_user_string(arg1);
4947 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004948 if (!p || !p2)
4949 ret = -TARGET_EFAULT;
4950 else
4951 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004952 unlock_user(p2, arg2, 0);
4953 unlock_user(p, arg1, 0);
4954 }
bellard31e31b82003-02-18 22:55:36 +00004955 break;
ths722183f2007-09-24 09:24:37 +00004956#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4957 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00004958 {
bellard579a97f2007-11-11 14:26:47 +00004959 void *p2;
ths722183f2007-09-24 09:24:37 +00004960 p = lock_user_string(arg2);
4961 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004962 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004963 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00004964 else
4965 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00004966 unlock_user(p2, arg4, 0);
4967 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00004968 }
4969 break;
4970#endif
bellard31e31b82003-02-18 22:55:36 +00004971 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00004972 if (!(p = lock_user_string(arg1)))
4973 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004974 ret = get_errno(mkdir(p, arg2));
4975 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004976 break;
ths4472ad02007-09-24 09:22:32 +00004977#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4978 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00004979 if (!(p = lock_user_string(arg2)))
4980 goto efault;
4981 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4982 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00004983 break;
4984#endif
bellard31e31b82003-02-18 22:55:36 +00004985 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00004986 if (!(p = lock_user_string(arg1)))
4987 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004988 ret = get_errno(rmdir(p));
4989 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004990 break;
4991 case TARGET_NR_dup:
4992 ret = get_errno(dup(arg1));
4993 break;
4994 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07004995 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00004996 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03004997#ifdef TARGET_NR_pipe2
4998 case TARGET_NR_pipe2:
Richard Hendersonfb41a662010-05-03 10:07:52 -07004999 ret = do_pipe(cpu_env, arg1, arg2, 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03005000 break;
5001#endif
bellard31e31b82003-02-18 22:55:36 +00005002 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00005003 {
pbrook53a59602006-03-25 19:31:22 +00005004 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00005005 struct tms tms;
5006 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00005007 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00005008 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5009 if (!tmsp)
5010 goto efault;
bellardc596ed12003-07-13 17:32:31 +00005011 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
5012 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
5013 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
5014 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00005015 }
bellardc596ed12003-07-13 17:32:31 +00005016 if (!is_error(ret))
5017 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00005018 }
5019 break;
bellardebc05482003-09-30 21:08:41 +00005020#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00005021 case TARGET_NR_prof:
5022 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005023#endif
thse5febef2007-04-01 18:31:35 +00005024#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00005025 case TARGET_NR_signal:
5026 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005027#endif
bellard31e31b82003-02-18 22:55:36 +00005028 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00005029 if (arg1 == 0) {
5030 ret = get_errno(acct(NULL));
5031 } else {
5032 if (!(p = lock_user_string(arg1)))
5033 goto efault;
5034 ret = get_errno(acct(path(p)));
5035 unlock_user(p, arg1, 0);
5036 }
pbrook24836682006-04-16 14:14:53 +00005037 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005038#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005039 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00005040 if (!(p = lock_user_string(arg1)))
5041 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005042 ret = get_errno(umount2(p, arg2));
5043 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005044 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005045#endif
bellardebc05482003-09-30 21:08:41 +00005046#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00005047 case TARGET_NR_lock:
5048 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005049#endif
bellard31e31b82003-02-18 22:55:36 +00005050 case TARGET_NR_ioctl:
5051 ret = do_ioctl(arg1, arg2, arg3);
5052 break;
5053 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00005054 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00005055 break;
bellardebc05482003-09-30 21:08:41 +00005056#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00005057 case TARGET_NR_mpx:
5058 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005059#endif
bellard31e31b82003-02-18 22:55:36 +00005060 case TARGET_NR_setpgid:
5061 ret = get_errno(setpgid(arg1, arg2));
5062 break;
bellardebc05482003-09-30 21:08:41 +00005063#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00005064 case TARGET_NR_ulimit:
5065 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005066#endif
5067#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00005068 case TARGET_NR_oldolduname:
5069 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005070#endif
bellard31e31b82003-02-18 22:55:36 +00005071 case TARGET_NR_umask:
5072 ret = get_errno(umask(arg1));
5073 break;
5074 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00005075 if (!(p = lock_user_string(arg1)))
5076 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005077 ret = get_errno(chroot(p));
5078 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005079 break;
5080 case TARGET_NR_ustat:
5081 goto unimplemented;
5082 case TARGET_NR_dup2:
5083 ret = get_errno(dup2(arg1, arg2));
5084 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03005085#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5086 case TARGET_NR_dup3:
5087 ret = get_errno(dup3(arg1, arg2, arg3));
5088 break;
5089#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005090#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005091 case TARGET_NR_getppid:
5092 ret = get_errno(getppid());
5093 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005094#endif
bellard31e31b82003-02-18 22:55:36 +00005095 case TARGET_NR_getpgrp:
5096 ret = get_errno(getpgrp());
5097 break;
5098 case TARGET_NR_setsid:
5099 ret = get_errno(setsid());
5100 break;
thse5febef2007-04-01 18:31:35 +00005101#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00005102 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00005103 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005104#if defined(TARGET_ALPHA)
5105 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00005106 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00005107 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005108 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5109 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005110 act._sa_handler = old_act->_sa_handler;
5111 target_siginitset(&act.sa_mask, old_act->sa_mask);
5112 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005113 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00005114 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005115 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00005116 }
5117 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00005118 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005119 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5120 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005121 old_act->_sa_handler = oact._sa_handler;
5122 old_act->sa_mask = oact.sa_mask.sig[0];
5123 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00005124 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00005125 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005126#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00005127 struct target_sigaction act, oact, *pact, *old_act;
5128
5129 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005130 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5131 goto efault;
bellard106ec872006-06-27 21:08:10 +00005132 act._sa_handler = old_act->_sa_handler;
5133 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5134 act.sa_flags = old_act->sa_flags;
5135 unlock_user_struct(old_act, arg2, 0);
5136 pact = &act;
5137 } else {
5138 pact = NULL;
5139 }
5140
5141 ret = get_errno(do_sigaction(arg1, pact, &oact));
5142
5143 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005144 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5145 goto efault;
bellard106ec872006-06-27 21:08:10 +00005146 old_act->_sa_handler = oact._sa_handler;
5147 old_act->sa_flags = oact.sa_flags;
5148 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5149 old_act->sa_mask.sig[1] = 0;
5150 old_act->sa_mask.sig[2] = 0;
5151 old_act->sa_mask.sig[3] = 0;
5152 unlock_user_struct(old_act, arg3, 1);
5153 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005154#else
5155 struct target_old_sigaction *old_act;
5156 struct target_sigaction act, oact, *pact;
5157 if (arg2) {
5158 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5159 goto efault;
5160 act._sa_handler = old_act->_sa_handler;
5161 target_siginitset(&act.sa_mask, old_act->sa_mask);
5162 act.sa_flags = old_act->sa_flags;
5163 act.sa_restorer = old_act->sa_restorer;
5164 unlock_user_struct(old_act, arg2, 0);
5165 pact = &act;
5166 } else {
5167 pact = NULL;
5168 }
5169 ret = get_errno(do_sigaction(arg1, pact, &oact));
5170 if (!is_error(ret) && arg3) {
5171 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5172 goto efault;
5173 old_act->_sa_handler = oact._sa_handler;
5174 old_act->sa_mask = oact.sa_mask.sig[0];
5175 old_act->sa_flags = oact.sa_flags;
5176 old_act->sa_restorer = oact.sa_restorer;
5177 unlock_user_struct(old_act, arg3, 1);
5178 }
ths388bb212007-05-13 13:58:00 +00005179#endif
bellard31e31b82003-02-18 22:55:36 +00005180 }
5181 break;
thse5febef2007-04-01 18:31:35 +00005182#endif
bellard66fb9762003-03-23 01:06:05 +00005183 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00005184 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005185#if defined(TARGET_ALPHA)
5186 struct target_sigaction act, oact, *pact = 0;
5187 struct target_rt_sigaction *rt_act;
5188 /* ??? arg4 == sizeof(sigset_t). */
5189 if (arg2) {
5190 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5191 goto efault;
5192 act._sa_handler = rt_act->_sa_handler;
5193 act.sa_mask = rt_act->sa_mask;
5194 act.sa_flags = rt_act->sa_flags;
5195 act.sa_restorer = arg5;
5196 unlock_user_struct(rt_act, arg2, 0);
5197 pact = &act;
5198 }
5199 ret = get_errno(do_sigaction(arg1, pact, &oact));
5200 if (!is_error(ret) && arg3) {
5201 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5202 goto efault;
5203 rt_act->_sa_handler = oact._sa_handler;
5204 rt_act->sa_mask = oact.sa_mask;
5205 rt_act->sa_flags = oact.sa_flags;
5206 unlock_user_struct(rt_act, arg3, 1);
5207 }
5208#else
pbrook53a59602006-03-25 19:31:22 +00005209 struct target_sigaction *act;
5210 struct target_sigaction *oact;
5211
bellard579a97f2007-11-11 14:26:47 +00005212 if (arg2) {
5213 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5214 goto efault;
5215 } else
pbrook53a59602006-03-25 19:31:22 +00005216 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005217 if (arg3) {
5218 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5219 ret = -TARGET_EFAULT;
5220 goto rt_sigaction_fail;
5221 }
5222 } else
pbrook53a59602006-03-25 19:31:22 +00005223 oact = NULL;
5224 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005225 rt_sigaction_fail:
5226 if (act)
pbrook53a59602006-03-25 19:31:22 +00005227 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005228 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005229 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005230#endif
pbrook53a59602006-03-25 19:31:22 +00005231 }
bellard66fb9762003-03-23 01:06:05 +00005232 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005233#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005234 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005235 {
5236 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005237 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005238 sigprocmask(0, NULL, &cur_set);
5239 host_to_target_old_sigset(&target_set, &cur_set);
5240 ret = target_set;
5241 }
5242 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005243#endif
5244#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005245 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005246 {
5247 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005248 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00005249 sigprocmask(0, NULL, &cur_set);
5250 target_to_host_old_sigset(&set, &target_set);
5251 sigorset(&set, &set, &cur_set);
5252 sigprocmask(SIG_SETMASK, &set, &oset);
5253 host_to_target_old_sigset(&target_set, &oset);
5254 ret = target_set;
5255 }
5256 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005257#endif
thse5febef2007-04-01 18:31:35 +00005258#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00005259 case TARGET_NR_sigprocmask:
5260 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005261#if defined(TARGET_ALPHA)
5262 sigset_t set, oldset;
5263 abi_ulong mask;
5264 int how;
5265
5266 switch (arg1) {
5267 case TARGET_SIG_BLOCK:
5268 how = SIG_BLOCK;
5269 break;
5270 case TARGET_SIG_UNBLOCK:
5271 how = SIG_UNBLOCK;
5272 break;
5273 case TARGET_SIG_SETMASK:
5274 how = SIG_SETMASK;
5275 break;
5276 default:
5277 ret = -TARGET_EINVAL;
5278 goto fail;
5279 }
5280 mask = arg2;
5281 target_to_host_old_sigset(&set, &mask);
5282
5283 ret = get_errno(sigprocmask(how, &set, &oldset));
5284
5285 if (!is_error(ret)) {
5286 host_to_target_old_sigset(&mask, &oldset);
5287 ret = mask;
5288 ((CPUAlphaState *)cpu_env)->[IR_V0] = 0; /* force no error */
5289 }
5290#else
bellard66fb9762003-03-23 01:06:05 +00005291 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07005292 int how;
ths3b46e622007-09-17 08:09:54 +00005293
pbrook53a59602006-03-25 19:31:22 +00005294 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005295 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00005296 case TARGET_SIG_BLOCK:
5297 how = SIG_BLOCK;
5298 break;
5299 case TARGET_SIG_UNBLOCK:
5300 how = SIG_UNBLOCK;
5301 break;
5302 case TARGET_SIG_SETMASK:
5303 how = SIG_SETMASK;
5304 break;
5305 default:
ths0da46a62007-10-20 20:23:07 +00005306 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005307 goto fail;
5308 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005309 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005310 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005311 target_to_host_old_sigset(&set, p);
5312 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005313 set_ptr = &set;
5314 } else {
5315 how = 0;
5316 set_ptr = NULL;
5317 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005318 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005319 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005320 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005321 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005322 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005323 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005324 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005325#endif
bellard66fb9762003-03-23 01:06:05 +00005326 }
5327 break;
thse5febef2007-04-01 18:31:35 +00005328#endif
bellard66fb9762003-03-23 01:06:05 +00005329 case TARGET_NR_rt_sigprocmask:
5330 {
5331 int how = arg1;
5332 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00005333
pbrook53a59602006-03-25 19:31:22 +00005334 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005335 switch(how) {
5336 case TARGET_SIG_BLOCK:
5337 how = SIG_BLOCK;
5338 break;
5339 case TARGET_SIG_UNBLOCK:
5340 how = SIG_UNBLOCK;
5341 break;
5342 case TARGET_SIG_SETMASK:
5343 how = SIG_SETMASK;
5344 break;
5345 default:
ths0da46a62007-10-20 20:23:07 +00005346 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005347 goto fail;
5348 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005349 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005350 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005351 target_to_host_sigset(&set, p);
5352 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005353 set_ptr = &set;
5354 } else {
5355 how = 0;
5356 set_ptr = NULL;
5357 }
5358 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005359 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005360 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005361 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005362 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005363 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005364 }
5365 }
5366 break;
thse5febef2007-04-01 18:31:35 +00005367#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005368 case TARGET_NR_sigpending:
5369 {
5370 sigset_t set;
5371 ret = get_errno(sigpending(&set));
5372 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005373 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005374 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005375 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005376 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005377 }
5378 }
5379 break;
thse5febef2007-04-01 18:31:35 +00005380#endif
bellard66fb9762003-03-23 01:06:05 +00005381 case TARGET_NR_rt_sigpending:
5382 {
5383 sigset_t set;
5384 ret = get_errno(sigpending(&set));
5385 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005386 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005387 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005388 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005389 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005390 }
5391 }
5392 break;
thse5febef2007-04-01 18:31:35 +00005393#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00005394 case TARGET_NR_sigsuspend:
5395 {
5396 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07005397#if defined(TARGET_ALPHA)
5398 abi_ulong mask = arg1;
5399 target_to_host_old_sigset(&set, &mask);
5400#else
Anthony Liguoric227f092009-10-01 16:12:16 -05005401 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005402 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005403 target_to_host_old_sigset(&set, p);
5404 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07005405#endif
bellard66fb9762003-03-23 01:06:05 +00005406 ret = get_errno(sigsuspend(&set));
5407 }
5408 break;
thse5febef2007-04-01 18:31:35 +00005409#endif
bellard66fb9762003-03-23 01:06:05 +00005410 case TARGET_NR_rt_sigsuspend:
5411 {
5412 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005413 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005414 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005415 target_to_host_sigset(&set, p);
5416 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005417 ret = get_errno(sigsuspend(&set));
5418 }
5419 break;
5420 case TARGET_NR_rt_sigtimedwait:
5421 {
bellard66fb9762003-03-23 01:06:05 +00005422 sigset_t set;
5423 struct timespec uts, *puts;
5424 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00005425
Anthony Liguoric227f092009-10-01 16:12:16 -05005426 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005427 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005428 target_to_host_sigset(&set, p);
5429 unlock_user(p, arg1, 0);
5430 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00005431 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00005432 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00005433 } else {
5434 puts = NULL;
5435 }
5436 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00005437 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005438 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005439 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005440 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05005441 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00005442 }
5443 }
5444 break;
5445 case TARGET_NR_rt_sigqueueinfo:
5446 {
5447 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05005448 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005449 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005450 target_to_host_siginfo(&uinfo, p);
5451 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005452 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5453 }
5454 break;
thse5febef2007-04-01 18:31:35 +00005455#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00005456 case TARGET_NR_sigreturn:
5457 /* NOTE: ret is eax, so not transcoding must be done */
5458 ret = do_sigreturn(cpu_env);
5459 break;
thse5febef2007-04-01 18:31:35 +00005460#endif
bellard66fb9762003-03-23 01:06:05 +00005461 case TARGET_NR_rt_sigreturn:
5462 /* NOTE: ret is eax, so not transcoding must be done */
5463 ret = do_rt_sigreturn(cpu_env);
5464 break;
bellard31e31b82003-02-18 22:55:36 +00005465 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00005466 if (!(p = lock_user_string(arg1)))
5467 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005468 ret = get_errno(sethostname(p, arg2));
5469 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005470 break;
5471 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00005472 {
bellard9de5e442003-03-23 16:49:39 +00005473 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005474 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005475 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00005476 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5477 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005478 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5479 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005480 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00005481 ret = get_errno(setrlimit(resource, &rlim));
5482 }
5483 break;
bellard31e31b82003-02-18 22:55:36 +00005484 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00005485 {
bellard9de5e442003-03-23 16:49:39 +00005486 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005487 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005488 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00005489
bellard9de5e442003-03-23 16:49:39 +00005490 ret = get_errno(getrlimit(resource, &rlim));
5491 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005492 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5493 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005494 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5495 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005496 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00005497 }
5498 }
5499 break;
bellard31e31b82003-02-18 22:55:36 +00005500 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00005501 {
5502 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00005503 ret = get_errno(getrusage(arg1, &rusage));
5504 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005505 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00005506 }
5507 }
5508 break;
bellard31e31b82003-02-18 22:55:36 +00005509 case TARGET_NR_gettimeofday:
5510 {
bellard31e31b82003-02-18 22:55:36 +00005511 struct timeval tv;
5512 ret = get_errno(gettimeofday(&tv, NULL));
5513 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00005514 if (copy_to_user_timeval(arg1, &tv))
5515 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005516 }
5517 }
5518 break;
5519 case TARGET_NR_settimeofday:
5520 {
bellard31e31b82003-02-18 22:55:36 +00005521 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00005522 if (copy_from_user_timeval(&tv, arg1))
5523 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005524 ret = get_errno(settimeofday(&tv, NULL));
5525 }
5526 break;
bellard048f6b42005-11-26 18:47:20 +00005527#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00005528 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00005529 {
pbrook53a59602006-03-25 19:31:22 +00005530 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00005531 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00005532 long nsel;
5533
bellard579a97f2007-11-11 14:26:47 +00005534 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5535 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005536 nsel = tswapl(sel->n);
5537 inp = tswapl(sel->inp);
5538 outp = tswapl(sel->outp);
5539 exp = tswapl(sel->exp);
5540 tvp = tswapl(sel->tvp);
5541 unlock_user_struct(sel, arg1, 0);
5542 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00005543 }
5544 break;
bellard048f6b42005-11-26 18:47:20 +00005545#endif
Riku Voipio9e423822010-05-07 12:28:05 +00005546#ifdef TARGET_NR_pselect6
5547 case TARGET_NR_pselect6:
5548 goto unimplemented_nowarn;
5549#endif
bellard31e31b82003-02-18 22:55:36 +00005550 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00005551 {
5552 void *p2;
5553 p = lock_user_string(arg1);
5554 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005555 if (!p || !p2)
5556 ret = -TARGET_EFAULT;
5557 else
5558 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005559 unlock_user(p2, arg2, 0);
5560 unlock_user(p, arg1, 0);
5561 }
bellard31e31b82003-02-18 22:55:36 +00005562 break;
thsf0b62432007-09-24 09:25:40 +00005563#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5564 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00005565 {
bellard579a97f2007-11-11 14:26:47 +00005566 void *p2;
thsf0b62432007-09-24 09:25:40 +00005567 p = lock_user_string(arg1);
5568 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005569 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005570 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00005571 else
5572 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00005573 unlock_user(p2, arg3, 0);
5574 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00005575 }
5576 break;
5577#endif
bellardebc05482003-09-30 21:08:41 +00005578#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00005579 case TARGET_NR_oldlstat:
5580 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005581#endif
bellard31e31b82003-02-18 22:55:36 +00005582 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00005583 {
aurel32d088d662009-01-30 20:09:01 +00005584 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00005585 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00005586 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5587 if (!p || !p2)
5588 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00005589 else {
5590 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5591 char real[PATH_MAX];
5592 temp = realpath(exec_path,real);
5593 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5594 snprintf((char *)p2, arg3, "%s", real);
5595 }
5596 else
5597 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00005598 }
pbrook53a59602006-03-25 19:31:22 +00005599 unlock_user(p2, arg2, ret);
5600 unlock_user(p, arg1, 0);
5601 }
bellard31e31b82003-02-18 22:55:36 +00005602 break;
ths5e0ccb12007-09-24 09:26:10 +00005603#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5604 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00005605 {
bellard579a97f2007-11-11 14:26:47 +00005606 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00005607 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005608 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5609 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005610 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00005611 else
5612 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00005613 unlock_user(p2, arg3, ret);
5614 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00005615 }
5616 break;
5617#endif
thse5febef2007-04-01 18:31:35 +00005618#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00005619 case TARGET_NR_uselib:
5620 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005621#endif
5622#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00005623 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00005624 if (!(p = lock_user_string(arg1)))
5625 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005626 ret = get_errno(swapon(p, arg2));
5627 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005628 break;
thse5febef2007-04-01 18:31:35 +00005629#endif
bellard31e31b82003-02-18 22:55:36 +00005630 case TARGET_NR_reboot:
5631 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005632#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00005633 case TARGET_NR_readdir:
5634 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005635#endif
5636#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00005637 case TARGET_NR_mmap:
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02005638#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE)
bellard31e31b82003-02-18 22:55:36 +00005639 {
blueswir1992f48a2007-10-14 16:27:31 +00005640 abi_ulong *v;
5641 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00005642 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5643 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005644 v1 = tswapl(v[0]);
5645 v2 = tswapl(v[1]);
5646 v3 = tswapl(v[2]);
5647 v4 = tswapl(v[3]);
5648 v5 = tswapl(v[4]);
5649 v6 = tswapl(v[5]);
5650 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00005651 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00005652 target_to_host_bitmask(v4, mmap_flags_tbl),
5653 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00005654 }
bellard31e31b82003-02-18 22:55:36 +00005655#else
ths5fafdf22007-09-16 21:08:06 +00005656 ret = get_errno(target_mmap(arg1, arg2, arg3,
5657 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00005658 arg5,
5659 arg6));
bellard31e31b82003-02-18 22:55:36 +00005660#endif
bellard6fb883e2003-07-09 17:12:39 +00005661 break;
thse5febef2007-04-01 18:31:35 +00005662#endif
bellarda315a142005-01-30 22:59:18 +00005663#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00005664 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00005665#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00005666#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00005667#endif
ths5fafdf22007-09-16 21:08:06 +00005668 ret = get_errno(target_mmap(arg1, arg2, arg3,
5669 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00005670 arg5,
bellardc573ff62004-01-04 15:51:36 +00005671 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00005672 break;
bellarda315a142005-01-30 22:59:18 +00005673#endif
bellard31e31b82003-02-18 22:55:36 +00005674 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00005675 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005676 break;
bellard9de5e442003-03-23 16:49:39 +00005677 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01005678 {
5679 TaskState *ts = ((CPUState *)cpu_env)->opaque;
5680 /* Special hack to detect libc making the stack executable. */
5681 if ((arg3 & PROT_GROWSDOWN)
5682 && arg1 >= ts->info->stack_limit
5683 && arg1 <= ts->info->start_stack) {
5684 arg3 &= ~PROT_GROWSDOWN;
5685 arg2 = arg2 + arg1 - ts->info->stack_limit;
5686 arg1 = ts->info->stack_limit;
5687 }
5688 }
bellard54936002003-05-13 00:25:15 +00005689 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005690 break;
thse5febef2007-04-01 18:31:35 +00005691#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00005692 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00005693 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00005694 break;
thse5febef2007-04-01 18:31:35 +00005695#endif
pbrook53a59602006-03-25 19:31:22 +00005696 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00005697#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00005698 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00005699 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005700 break;
thse5febef2007-04-01 18:31:35 +00005701#endif
5702#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00005703 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00005704 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005705 break;
thse5febef2007-04-01 18:31:35 +00005706#endif
5707#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00005708 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00005709 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005710 break;
thse5febef2007-04-01 18:31:35 +00005711#endif
5712#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00005713 case TARGET_NR_mlockall:
5714 ret = get_errno(mlockall(arg1));
5715 break;
thse5febef2007-04-01 18:31:35 +00005716#endif
5717#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00005718 case TARGET_NR_munlockall:
5719 ret = get_errno(munlockall());
5720 break;
thse5febef2007-04-01 18:31:35 +00005721#endif
bellard31e31b82003-02-18 22:55:36 +00005722 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00005723 if (!(p = lock_user_string(arg1)))
5724 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005725 ret = get_errno(truncate(p, arg2));
5726 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005727 break;
5728 case TARGET_NR_ftruncate:
5729 ret = get_errno(ftruncate(arg1, arg2));
5730 break;
5731 case TARGET_NR_fchmod:
5732 ret = get_errno(fchmod(arg1, arg2));
5733 break;
ths814d7972007-09-24 09:26:51 +00005734#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5735 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00005736 if (!(p = lock_user_string(arg2)))
5737 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005738 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005739 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00005740 break;
5741#endif
bellard31e31b82003-02-18 22:55:36 +00005742 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00005743 /* libc does special remapping of the return value of
5744 * sys_getpriority() so it's just easiest to call
5745 * sys_getpriority() directly rather than through libc. */
Ulrich Hecht69137202009-09-17 21:08:37 +03005746 ret = get_errno(sys_getpriority(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005747 break;
5748 case TARGET_NR_setpriority:
5749 ret = get_errno(setpriority(arg1, arg2, arg3));
5750 break;
bellardebc05482003-09-30 21:08:41 +00005751#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00005752 case TARGET_NR_profil:
5753 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005754#endif
bellard31e31b82003-02-18 22:55:36 +00005755 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00005756 if (!(p = lock_user_string(arg1)))
5757 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005758 ret = get_errno(statfs(path(p), &stfs));
5759 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005760 convert_statfs:
5761 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005762 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005763
bellard579a97f2007-11-11 14:26:47 +00005764 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5765 goto efault;
5766 __put_user(stfs.f_type, &target_stfs->f_type);
5767 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5768 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5769 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5770 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5771 __put_user(stfs.f_files, &target_stfs->f_files);
5772 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5773 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5774 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5775 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00005776 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005777 }
5778 break;
5779 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00005780 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00005781 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00005782#ifdef TARGET_NR_statfs64
5783 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00005784 if (!(p = lock_user_string(arg1)))
5785 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005786 ret = get_errno(statfs(path(p), &stfs));
5787 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00005788 convert_statfs64:
5789 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005790 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005791
bellard579a97f2007-11-11 14:26:47 +00005792 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5793 goto efault;
5794 __put_user(stfs.f_type, &target_stfs->f_type);
5795 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5796 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5797 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5798 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5799 __put_user(stfs.f_files, &target_stfs->f_files);
5800 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5801 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5802 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5803 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5804 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00005805 }
5806 break;
5807 case TARGET_NR_fstatfs64:
5808 ret = get_errno(fstatfs(arg1, &stfs));
5809 goto convert_statfs64;
5810#endif
bellardebc05482003-09-30 21:08:41 +00005811#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00005812 case TARGET_NR_ioperm:
5813 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005814#endif
thse5febef2007-04-01 18:31:35 +00005815#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00005816 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00005817 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00005818 break;
thse5febef2007-04-01 18:31:35 +00005819#endif
bellard3532fa72006-06-24 15:06:03 +00005820#ifdef TARGET_NR_accept
5821 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00005822 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005823 break;
5824#endif
5825#ifdef TARGET_NR_bind
5826 case TARGET_NR_bind:
5827 ret = do_bind(arg1, arg2, arg3);
5828 break;
5829#endif
5830#ifdef TARGET_NR_connect
5831 case TARGET_NR_connect:
5832 ret = do_connect(arg1, arg2, arg3);
5833 break;
5834#endif
5835#ifdef TARGET_NR_getpeername
5836 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00005837 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005838 break;
5839#endif
5840#ifdef TARGET_NR_getsockname
5841 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00005842 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005843 break;
5844#endif
5845#ifdef TARGET_NR_getsockopt
5846 case TARGET_NR_getsockopt:
5847 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5848 break;
5849#endif
5850#ifdef TARGET_NR_listen
5851 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00005852 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005853 break;
5854#endif
5855#ifdef TARGET_NR_recv
5856 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00005857 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005858 break;
5859#endif
5860#ifdef TARGET_NR_recvfrom
5861 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00005862 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005863 break;
5864#endif
5865#ifdef TARGET_NR_recvmsg
5866 case TARGET_NR_recvmsg:
5867 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5868 break;
5869#endif
5870#ifdef TARGET_NR_send
5871 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00005872 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005873 break;
5874#endif
5875#ifdef TARGET_NR_sendmsg
5876 case TARGET_NR_sendmsg:
5877 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5878 break;
5879#endif
5880#ifdef TARGET_NR_sendto
5881 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00005882 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005883 break;
5884#endif
5885#ifdef TARGET_NR_shutdown
5886 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00005887 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005888 break;
5889#endif
5890#ifdef TARGET_NR_socket
5891 case TARGET_NR_socket:
5892 ret = do_socket(arg1, arg2, arg3);
5893 break;
5894#endif
5895#ifdef TARGET_NR_socketpair
5896 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00005897 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00005898 break;
5899#endif
5900#ifdef TARGET_NR_setsockopt
5901 case TARGET_NR_setsockopt:
5902 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5903 break;
5904#endif
ths7494b0f2007-02-11 18:26:53 +00005905
bellard31e31b82003-02-18 22:55:36 +00005906 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00005907 if (!(p = lock_user_string(arg2)))
5908 goto efault;
thse5574482007-02-11 20:03:13 +00005909 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5910 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00005911 break;
5912
bellard31e31b82003-02-18 22:55:36 +00005913 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00005914 {
bellard66fb9762003-03-23 01:06:05 +00005915 struct itimerval value, ovalue, *pvalue;
5916
pbrook53a59602006-03-25 19:31:22 +00005917 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005918 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00005919 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5920 || copy_from_user_timeval(&pvalue->it_value,
5921 arg2 + sizeof(struct target_timeval)))
5922 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005923 } else {
5924 pvalue = NULL;
5925 }
5926 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00005927 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00005928 if (copy_to_user_timeval(arg3,
5929 &ovalue.it_interval)
5930 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5931 &ovalue.it_value))
5932 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005933 }
5934 }
5935 break;
bellard31e31b82003-02-18 22:55:36 +00005936 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00005937 {
bellard66fb9762003-03-23 01:06:05 +00005938 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00005939
bellard66fb9762003-03-23 01:06:05 +00005940 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00005941 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00005942 if (copy_to_user_timeval(arg2,
5943 &value.it_interval)
5944 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5945 &value.it_value))
5946 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005947 }
5948 }
5949 break;
bellard31e31b82003-02-18 22:55:36 +00005950 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00005951 if (!(p = lock_user_string(arg1)))
5952 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005953 ret = get_errno(stat(path(p), &st));
5954 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005955 goto do_stat;
5956 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00005957 if (!(p = lock_user_string(arg1)))
5958 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005959 ret = get_errno(lstat(path(p), &st));
5960 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005961 goto do_stat;
5962 case TARGET_NR_fstat:
5963 {
5964 ret = get_errno(fstat(arg1, &st));
5965 do_stat:
5966 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005967 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00005968
bellard579a97f2007-11-11 14:26:47 +00005969 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5970 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02005971 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00005972 __put_user(st.st_dev, &target_st->st_dev);
5973 __put_user(st.st_ino, &target_st->st_ino);
5974 __put_user(st.st_mode, &target_st->st_mode);
5975 __put_user(st.st_uid, &target_st->st_uid);
5976 __put_user(st.st_gid, &target_st->st_gid);
5977 __put_user(st.st_nlink, &target_st->st_nlink);
5978 __put_user(st.st_rdev, &target_st->st_rdev);
5979 __put_user(st.st_size, &target_st->st_size);
5980 __put_user(st.st_blksize, &target_st->st_blksize);
5981 __put_user(st.st_blocks, &target_st->st_blocks);
5982 __put_user(st.st_atime, &target_st->target_st_atime);
5983 __put_user(st.st_mtime, &target_st->target_st_mtime);
5984 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00005985 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005986 }
5987 }
5988 break;
bellardebc05482003-09-30 21:08:41 +00005989#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00005990 case TARGET_NR_olduname:
5991 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005992#endif
5993#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00005994 case TARGET_NR_iopl:
5995 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005996#endif
bellard31e31b82003-02-18 22:55:36 +00005997 case TARGET_NR_vhangup:
5998 ret = get_errno(vhangup());
5999 break;
bellardebc05482003-09-30 21:08:41 +00006000#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00006001 case TARGET_NR_idle:
6002 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006003#endif
bellard42ad6ae2005-01-03 22:48:11 +00006004#ifdef TARGET_NR_syscall
6005 case TARGET_NR_syscall:
6006 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
6007 break;
6008#endif
bellard31e31b82003-02-18 22:55:36 +00006009 case TARGET_NR_wait4:
6010 {
6011 int status;
blueswir1992f48a2007-10-14 16:27:31 +00006012 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00006013 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00006014 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00006015 if (target_rusage)
6016 rusage_ptr = &rusage;
6017 else
6018 rusage_ptr = NULL;
6019 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6020 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006021 if (status_ptr) {
pbrook1d9d8b52009-04-16 15:17:02 +00006022 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00006023 if (put_user_s32(status, status_ptr))
6024 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006025 }
bellard2f619692007-11-16 10:46:05 +00006026 if (target_rusage)
6027 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00006028 }
6029 }
6030 break;
thse5febef2007-04-01 18:31:35 +00006031#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00006032 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00006033 if (!(p = lock_user_string(arg1)))
6034 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006035 ret = get_errno(swapoff(p));
6036 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006037 break;
thse5febef2007-04-01 18:31:35 +00006038#endif
bellard31e31b82003-02-18 22:55:36 +00006039 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00006040 {
pbrook53a59602006-03-25 19:31:22 +00006041 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00006042 struct sysinfo value;
6043 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00006044 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00006045 {
bellard579a97f2007-11-11 14:26:47 +00006046 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6047 goto efault;
bellarda5448a72004-06-19 16:59:03 +00006048 __put_user(value.uptime, &target_value->uptime);
6049 __put_user(value.loads[0], &target_value->loads[0]);
6050 __put_user(value.loads[1], &target_value->loads[1]);
6051 __put_user(value.loads[2], &target_value->loads[2]);
6052 __put_user(value.totalram, &target_value->totalram);
6053 __put_user(value.freeram, &target_value->freeram);
6054 __put_user(value.sharedram, &target_value->sharedram);
6055 __put_user(value.bufferram, &target_value->bufferram);
6056 __put_user(value.totalswap, &target_value->totalswap);
6057 __put_user(value.freeswap, &target_value->freeswap);
6058 __put_user(value.procs, &target_value->procs);
6059 __put_user(value.totalhigh, &target_value->totalhigh);
6060 __put_user(value.freehigh, &target_value->freehigh);
6061 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00006062 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00006063 }
6064 }
6065 break;
thse5febef2007-04-01 18:31:35 +00006066#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00006067 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00006068 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6069 break;
thse5febef2007-04-01 18:31:35 +00006070#endif
aurel32e5289082009-04-18 16:16:12 +00006071#ifdef TARGET_NR_semget
6072 case TARGET_NR_semget:
6073 ret = get_errno(semget(arg1, arg2, arg3));
6074 break;
6075#endif
6076#ifdef TARGET_NR_semop
6077 case TARGET_NR_semop:
6078 ret = get_errno(do_semop(arg1, arg2, arg3));
6079 break;
6080#endif
6081#ifdef TARGET_NR_semctl
6082 case TARGET_NR_semctl:
6083 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6084 break;
6085#endif
aurel32eeb438c2008-10-13 21:08:55 +00006086#ifdef TARGET_NR_msgctl
6087 case TARGET_NR_msgctl:
6088 ret = do_msgctl(arg1, arg2, arg3);
6089 break;
6090#endif
6091#ifdef TARGET_NR_msgget
6092 case TARGET_NR_msgget:
6093 ret = get_errno(msgget(arg1, arg2));
6094 break;
6095#endif
6096#ifdef TARGET_NR_msgrcv
6097 case TARGET_NR_msgrcv:
6098 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
6099 break;
6100#endif
6101#ifdef TARGET_NR_msgsnd
6102 case TARGET_NR_msgsnd:
6103 ret = do_msgsnd(arg1, arg2, arg3, arg4);
6104 break;
6105#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03006106#ifdef TARGET_NR_shmget
6107 case TARGET_NR_shmget:
6108 ret = get_errno(shmget(arg1, arg2, arg3));
6109 break;
6110#endif
6111#ifdef TARGET_NR_shmctl
6112 case TARGET_NR_shmctl:
6113 ret = do_shmctl(arg1, arg2, arg3);
6114 break;
6115#endif
6116#ifdef TARGET_NR_shmat
6117 case TARGET_NR_shmat:
6118 ret = do_shmat(arg1, arg2, arg3);
6119 break;
6120#endif
6121#ifdef TARGET_NR_shmdt
6122 case TARGET_NR_shmdt:
6123 ret = do_shmdt(arg1);
6124 break;
6125#endif
bellard31e31b82003-02-18 22:55:36 +00006126 case TARGET_NR_fsync:
6127 ret = get_errno(fsync(arg1));
6128 break;
bellard31e31b82003-02-18 22:55:36 +00006129 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07006130#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00006131 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00006132#elif defined(TARGET_CRIS)
6133 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006134#else
pbrookd865bab2008-06-07 22:12:17 +00006135 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006136#endif
bellard1b6b0292003-03-22 17:31:38 +00006137 break;
bellardec86b0f2003-04-11 00:15:04 +00006138#ifdef __NR_exit_group
6139 /* new thread calls */
6140 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02006141#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00006142 _mcleanup();
6143#endif
bellarde9009672005-04-26 20:42:36 +00006144 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00006145 ret = get_errno(exit_group(arg1));
6146 break;
6147#endif
bellard31e31b82003-02-18 22:55:36 +00006148 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00006149 if (!(p = lock_user_string(arg1)))
6150 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006151 ret = get_errno(setdomainname(p, arg2));
6152 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006153 break;
6154 case TARGET_NR_uname:
6155 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00006156 {
6157 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00006158
bellard579a97f2007-11-11 14:26:47 +00006159 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
6160 goto efault;
bellard29e619b2004-09-13 21:41:04 +00006161 ret = get_errno(sys_uname(buf));
6162 if (!is_error(ret)) {
6163 /* Overrite the native machine name with whatever is being
6164 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01006165 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00006166 /* Allow the user to override the reported release. */
6167 if (qemu_uname_release && *qemu_uname_release)
6168 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00006169 }
pbrook53a59602006-03-25 19:31:22 +00006170 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00006171 }
bellard31e31b82003-02-18 22:55:36 +00006172 break;
bellard6dbad632003-03-16 18:05:05 +00006173#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00006174 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00006175 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00006176 break;
j_mayer84409dd2007-04-06 08:56:50 +00006177#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00006178 case TARGET_NR_vm86old:
6179 goto unimplemented;
6180 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00006181 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00006182 break;
6183#endif
j_mayer84409dd2007-04-06 08:56:50 +00006184#endif
bellard31e31b82003-02-18 22:55:36 +00006185 case TARGET_NR_adjtimex:
6186 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006187#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00006188 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00006189#endif
bellard31e31b82003-02-18 22:55:36 +00006190 case TARGET_NR_init_module:
6191 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00006192#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00006193 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00006194#endif
bellard31e31b82003-02-18 22:55:36 +00006195 goto unimplemented;
6196 case TARGET_NR_quotactl:
6197 goto unimplemented;
6198 case TARGET_NR_getpgid:
6199 ret = get_errno(getpgid(arg1));
6200 break;
6201 case TARGET_NR_fchdir:
6202 ret = get_errno(fchdir(arg1));
6203 break;
j_mayer84409dd2007-04-06 08:56:50 +00006204#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00006205 case TARGET_NR_bdflush:
6206 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00006207#endif
thse5febef2007-04-01 18:31:35 +00006208#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00006209 case TARGET_NR_sysfs:
6210 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006211#endif
bellard31e31b82003-02-18 22:55:36 +00006212 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00006213 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00006214 break;
thse5febef2007-04-01 18:31:35 +00006215#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00006216 case TARGET_NR_afs_syscall:
6217 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006218#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006219#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006220 case TARGET_NR__llseek:
6221 {
6222 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00006223#if !defined(__NR_llseek)
6224 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
6225 if (res == -1) {
6226 ret = get_errno(res);
6227 } else {
6228 ret = 0;
6229 }
6230#else
bellard31e31b82003-02-18 22:55:36 +00006231 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00006232#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00006233 if ((ret == 0) && put_user_s64(res, arg4)) {
6234 goto efault;
6235 }
bellard31e31b82003-02-18 22:55:36 +00006236 }
6237 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006238#endif
bellard31e31b82003-02-18 22:55:36 +00006239 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02006240#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00006241 {
pbrook53a59602006-03-25 19:31:22 +00006242 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00006243 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006244 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00006245
6246 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00006247 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00006248 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00006249 goto fail;
6250 }
ths3b46e622007-09-17 08:09:54 +00006251
bellard4add45b2003-06-05 01:52:59 +00006252 ret = get_errno(sys_getdents(arg1, dirp, count));
6253 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006254 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00006255 struct target_dirent *tde;
6256 int len = ret;
6257 int reclen, treclen;
6258 int count1, tnamelen;
6259
6260 count1 = 0;
6261 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00006262 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6263 goto efault;
bellard4add45b2003-06-05 01:52:59 +00006264 tde = target_dirp;
6265 while (len > 0) {
6266 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00006267 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00006268 tde->d_reclen = tswap16(treclen);
6269 tde->d_ino = tswapl(de->d_ino);
6270 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00006271 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00006272 if (tnamelen > 256)
6273 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00006274 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00006275 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00006276 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00006277 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00006278 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00006279 count1 += treclen;
6280 }
6281 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00006282 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00006283 }
6284 free(dirp);
6285 }
6286#else
bellard31e31b82003-02-18 22:55:36 +00006287 {
aurel326556a832008-10-13 21:08:17 +00006288 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006289 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00006290
bellard579a97f2007-11-11 14:26:47 +00006291 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6292 goto efault;
bellard72f03902003-02-18 23:33:18 +00006293 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00006294 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006295 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00006296 int len = ret;
6297 int reclen;
6298 de = dirp;
6299 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006300 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00006301 if (reclen > len)
6302 break;
bellard8083a3e2003-03-24 23:12:16 +00006303 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00006304 tswapls(&de->d_ino);
6305 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006306 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00006307 len -= reclen;
6308 }
6309 }
pbrook53a59602006-03-25 19:31:22 +00006310 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00006311 }
bellard4add45b2003-06-05 01:52:59 +00006312#endif
bellard31e31b82003-02-18 22:55:36 +00006313 break;
ths3ae43202007-09-16 21:39:48 +00006314#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00006315 case TARGET_NR_getdents64:
6316 {
aurel326556a832008-10-13 21:08:17 +00006317 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006318 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00006319 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6320 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00006321 ret = get_errno(sys_getdents64(arg1, dirp, count));
6322 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006323 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00006324 int len = ret;
6325 int reclen;
6326 de = dirp;
6327 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006328 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00006329 if (reclen > len)
6330 break;
bellard8083a3e2003-03-24 23:12:16 +00006331 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00006332 tswap64s((uint64_t *)&de->d_ino);
6333 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006334 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00006335 len -= reclen;
6336 }
6337 }
pbrook53a59602006-03-25 19:31:22 +00006338 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00006339 }
6340 break;
bellarda541f292004-04-12 20:39:29 +00006341#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00006342#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00006343 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00006344 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00006345 break;
thse5febef2007-04-01 18:31:35 +00006346#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05006347#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
6348# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00006349 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05006350# endif
6351# ifdef TARGET_NR_ppoll
6352 case TARGET_NR_ppoll:
6353# endif
bellard9de5e442003-03-23 16:49:39 +00006354 {
pbrook53a59602006-03-25 19:31:22 +00006355 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00006356 unsigned int nfds = arg2;
6357 int timeout = arg3;
6358 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00006359 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00006360
bellard579a97f2007-11-11 14:26:47 +00006361 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6362 if (!target_pfd)
6363 goto efault;
Mike Frysingerd8035d42011-02-07 01:05:51 -05006364
bellard9de5e442003-03-23 16:49:39 +00006365 pfd = alloca(sizeof(struct pollfd) * nfds);
6366 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006367 pfd[i].fd = tswap32(target_pfd[i].fd);
6368 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00006369 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05006370
6371# ifdef TARGET_NR_ppoll
6372 if (num == TARGET_NR_ppoll) {
6373 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
6374 target_sigset_t *target_set;
6375 sigset_t _set, *set = &_set;
6376
6377 if (arg3) {
6378 if (target_to_host_timespec(timeout_ts, arg3)) {
6379 unlock_user(target_pfd, arg1, 0);
6380 goto efault;
6381 }
6382 } else {
6383 timeout_ts = NULL;
6384 }
6385
6386 if (arg4) {
6387 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
6388 if (!target_set) {
6389 unlock_user(target_pfd, arg1, 0);
6390 goto efault;
6391 }
6392 target_to_host_sigset(set, target_set);
6393 } else {
6394 set = NULL;
6395 }
6396
6397 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
6398
6399 if (!is_error(ret) && arg3) {
6400 host_to_target_timespec(arg3, timeout_ts);
6401 }
6402 if (arg4) {
6403 unlock_user(target_set, arg4, 0);
6404 }
6405 } else
6406# endif
6407 ret = get_errno(poll(pfd, nfds, timeout));
6408
bellard9de5e442003-03-23 16:49:39 +00006409 if (!is_error(ret)) {
6410 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006411 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00006412 }
6413 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00006414 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00006415 }
6416 break;
thse5febef2007-04-01 18:31:35 +00006417#endif
bellard31e31b82003-02-18 22:55:36 +00006418 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00006419 /* NOTE: the flock constant seems to be the same for every
6420 Linux platform */
6421 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006422 break;
6423 case TARGET_NR_readv:
6424 {
6425 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006426 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006427
6428 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006429 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6430 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006431 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006432 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00006433 }
6434 break;
6435 case TARGET_NR_writev:
6436 {
6437 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006438 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006439
6440 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006441 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6442 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006443 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006444 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00006445 }
6446 break;
6447 case TARGET_NR_getsid:
6448 ret = get_errno(getsid(arg1));
6449 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006450#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00006451 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00006452 ret = get_errno(fdatasync(arg1));
6453 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006454#endif
bellard31e31b82003-02-18 22:55:36 +00006455 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00006456 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00006457 return value. */
ths0da46a62007-10-20 20:23:07 +00006458 ret = -TARGET_ENOTDIR;
6459 break;
Mike Frysinger737de1d2011-02-07 01:05:55 -05006460 case TARGET_NR_sched_getaffinity:
6461 {
6462 unsigned int mask_size;
6463 unsigned long *mask;
6464
6465 /*
6466 * sched_getaffinity needs multiples of ulong, so need to take
6467 * care of mismatches between target ulong and host ulong sizes.
6468 */
6469 if (arg2 & (sizeof(abi_ulong) - 1)) {
6470 ret = -TARGET_EINVAL;
6471 break;
6472 }
6473 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
6474
6475 mask = alloca(mask_size);
6476 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
6477
6478 if (!is_error(ret)) {
6479 if (arg2 > ret) {
6480 /* Zero out any extra space kernel didn't fill */
6481 unsigned long zero = arg2 - ret;
6482 p = alloca(zero);
6483 memset(p, 0, zero);
6484 if (copy_to_user(arg3 + zero, p, zero)) {
6485 goto efault;
6486 }
6487 arg2 = ret;
6488 }
6489 if (copy_to_user(arg3, mask, arg2)) {
6490 goto efault;
6491 }
6492 ret = arg2;
6493 }
6494 }
6495 break;
6496 case TARGET_NR_sched_setaffinity:
6497 {
6498 unsigned int mask_size;
6499 unsigned long *mask;
6500
6501 /*
6502 * sched_setaffinity needs multiples of ulong, so need to take
6503 * care of mismatches between target ulong and host ulong sizes.
6504 */
6505 if (arg2 & (sizeof(abi_ulong) - 1)) {
6506 ret = -TARGET_EINVAL;
6507 break;
6508 }
6509 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
6510
6511 mask = alloca(mask_size);
6512 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
6513 goto efault;
6514 }
6515 memcpy(mask, p, arg2);
6516 unlock_user_struct(p, arg2, 0);
6517
6518 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
6519 }
6520 break;
bellard31e31b82003-02-18 22:55:36 +00006521 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00006522 {
pbrook53a59602006-03-25 19:31:22 +00006523 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006524 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00006525
bellard579a97f2007-11-11 14:26:47 +00006526 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6527 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006528 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006529 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00006530 ret = get_errno(sched_setparam(arg1, &schp));
6531 }
6532 break;
bellard31e31b82003-02-18 22:55:36 +00006533 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00006534 {
pbrook53a59602006-03-25 19:31:22 +00006535 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006536 struct sched_param schp;
6537 ret = get_errno(sched_getparam(arg1, &schp));
6538 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006539 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6540 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006541 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006542 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00006543 }
6544 }
6545 break;
bellard31e31b82003-02-18 22:55:36 +00006546 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00006547 {
pbrook53a59602006-03-25 19:31:22 +00006548 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006549 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00006550 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6551 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006552 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006553 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00006554 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6555 }
6556 break;
bellard31e31b82003-02-18 22:55:36 +00006557 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00006558 ret = get_errno(sched_getscheduler(arg1));
6559 break;
bellard31e31b82003-02-18 22:55:36 +00006560 case TARGET_NR_sched_yield:
6561 ret = get_errno(sched_yield());
6562 break;
6563 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00006564 ret = get_errno(sched_get_priority_max(arg1));
6565 break;
bellard31e31b82003-02-18 22:55:36 +00006566 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00006567 ret = get_errno(sched_get_priority_min(arg1));
6568 break;
bellard31e31b82003-02-18 22:55:36 +00006569 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00006570 {
bellard5cd43932003-03-29 16:54:36 +00006571 struct timespec ts;
6572 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6573 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006574 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00006575 }
6576 }
6577 break;
bellard31e31b82003-02-18 22:55:36 +00006578 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00006579 {
bellard1b6b0292003-03-22 17:31:38 +00006580 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00006581 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00006582 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00006583 if (is_error(ret) && arg2) {
6584 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00006585 }
6586 }
6587 break;
thse5febef2007-04-01 18:31:35 +00006588#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00006589 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00006590 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006591#endif
6592#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00006593 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00006594 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006595#endif
bellard31e31b82003-02-18 22:55:36 +00006596 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00006597 switch (arg1)
6598 {
6599 case PR_GET_PDEATHSIG:
6600 {
6601 int deathsig;
6602 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00006603 if (!is_error(ret) && arg2
6604 && put_user_ual(deathsig, arg2))
6605 goto efault;
thse5574482007-02-11 20:03:13 +00006606 }
6607 break;
6608 default:
6609 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6610 break;
6611 }
ths39b9aae2007-02-11 18:36:44 +00006612 break;
bellardd2fd1af2007-11-14 18:08:56 +00006613#ifdef TARGET_NR_arch_prctl
6614 case TARGET_NR_arch_prctl:
6615#if defined(TARGET_I386) && !defined(TARGET_ABI32)
6616 ret = do_arch_prctl(cpu_env, arg1, arg2);
6617 break;
6618#else
6619 goto unimplemented;
6620#endif
6621#endif
bellard67867302003-11-23 17:05:30 +00006622#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00006623 case TARGET_NR_pread:
balroga4ae00b2008-09-20 03:14:14 +00006624#ifdef TARGET_ARM
6625 if (((CPUARMState *)cpu_env)->eabi)
6626 arg4 = arg5;
6627#endif
bellard579a97f2007-11-11 14:26:47 +00006628 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6629 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006630 ret = get_errno(pread(arg1, p, arg3, arg4));
6631 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00006632 break;
bellard31e31b82003-02-18 22:55:36 +00006633 case TARGET_NR_pwrite:
balroga4ae00b2008-09-20 03:14:14 +00006634#ifdef TARGET_ARM
6635 if (((CPUARMState *)cpu_env)->eabi)
6636 arg4 = arg5;
6637#endif
bellard579a97f2007-11-11 14:26:47 +00006638 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6639 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006640 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6641 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00006642 break;
bellard67867302003-11-23 17:05:30 +00006643#endif
aurel32f2c7ba12008-03-28 22:32:06 +00006644#ifdef TARGET_NR_pread64
6645 case TARGET_NR_pread64:
6646 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6647 goto efault;
6648 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6649 unlock_user(p, arg2, ret);
6650 break;
6651 case TARGET_NR_pwrite64:
6652 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6653 goto efault;
6654 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6655 unlock_user(p, arg2, 0);
6656 break;
6657#endif
bellard31e31b82003-02-18 22:55:36 +00006658 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00006659 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6660 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006661 ret = get_errno(sys_getcwd1(p, arg2));
6662 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00006663 break;
6664 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00006665 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006666 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00006667 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006668 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00006669#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02006670 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6671 defined(TARGET_M68K)
bellard579a97f2007-11-11 14:26:47 +00006672 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00006673 break;
6674#else
bellard5cd43932003-03-29 16:54:36 +00006675 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00006676#endif
bellard31e31b82003-02-18 22:55:36 +00006677 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00006678 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006679#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00006680 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00006681 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006682#endif
6683#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00006684 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00006685 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006686#endif
bellard048f6b42005-11-26 18:47:20 +00006687#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00006688 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00006689 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6690 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00006691 break;
bellard048f6b42005-11-26 18:47:20 +00006692#endif
bellardebc05482003-09-30 21:08:41 +00006693#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00006694 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00006695 {
6696 struct rlimit rlim;
6697 ret = get_errno(getrlimit(arg1, &rlim));
6698 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006699 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00006700 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6701 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006702 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6703 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006704 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00006705 }
6706 break;
6707 }
bellardebc05482003-09-30 21:08:41 +00006708#endif
bellarda315a142005-01-30 22:59:18 +00006709#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00006710 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00006711 if (!(p = lock_user_string(arg1)))
6712 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006713 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6714 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00006715 break;
bellarda315a142005-01-30 22:59:18 +00006716#endif
6717#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00006718 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00006719 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00006720 break;
bellarda315a142005-01-30 22:59:18 +00006721#endif
6722#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00006723 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00006724 if (!(p = lock_user_string(arg1)))
6725 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006726 ret = get_errno(stat(path(p), &st));
6727 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006728 if (!is_error(ret))
6729 ret = host_to_target_stat64(cpu_env, arg2, &st);
6730 break;
bellarda315a142005-01-30 22:59:18 +00006731#endif
6732#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00006733 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00006734 if (!(p = lock_user_string(arg1)))
6735 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006736 ret = get_errno(lstat(path(p), &st));
6737 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006738 if (!is_error(ret))
6739 ret = host_to_target_stat64(cpu_env, arg2, &st);
6740 break;
bellarda315a142005-01-30 22:59:18 +00006741#endif
6742#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00006743 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00006744 ret = get_errno(fstat(arg1, &st));
6745 if (!is_error(ret))
6746 ret = host_to_target_stat64(cpu_env, arg2, &st);
6747 break;
bellardec86b0f2003-04-11 00:15:04 +00006748#endif
aurel329d33b762009-04-08 23:07:05 +00006749#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6750 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6751#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006752 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00006753#endif
6754#ifdef TARGET_NR_newfstatat
6755 case TARGET_NR_newfstatat:
6756#endif
balrog6a24a772008-09-20 02:23:36 +00006757 if (!(p = lock_user_string(arg2)))
6758 goto efault;
aurel329d33b762009-04-08 23:07:05 +00006759#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006760 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00006761#else
6762 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6763#endif
balrog6a24a772008-09-20 02:23:36 +00006764 if (!is_error(ret))
6765 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00006766 break;
bellarda315a142005-01-30 22:59:18 +00006767#endif
bellard67867302003-11-23 17:05:30 +00006768#ifdef USE_UID16
6769 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00006770 if (!(p = lock_user_string(arg1)))
6771 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006772 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6773 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006774 break;
6775 case TARGET_NR_getuid:
6776 ret = get_errno(high2lowuid(getuid()));
6777 break;
6778 case TARGET_NR_getgid:
6779 ret = get_errno(high2lowgid(getgid()));
6780 break;
6781 case TARGET_NR_geteuid:
6782 ret = get_errno(high2lowuid(geteuid()));
6783 break;
6784 case TARGET_NR_getegid:
6785 ret = get_errno(high2lowgid(getegid()));
6786 break;
6787 case TARGET_NR_setreuid:
6788 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6789 break;
6790 case TARGET_NR_setregid:
6791 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6792 break;
6793 case TARGET_NR_getgroups:
6794 {
6795 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006796 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006797 gid_t *grouplist;
6798 int i;
6799
6800 grouplist = alloca(gidsetsize * sizeof(gid_t));
6801 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006802 if (gidsetsize == 0)
6803 break;
bellard67867302003-11-23 17:05:30 +00006804 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006805 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6806 if (!target_grouplist)
6807 goto efault;
balroga2155fc2008-09-20 02:12:08 +00006808 for(i = 0;i < ret; i++)
bellard67867302003-11-23 17:05:30 +00006809 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006810 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00006811 }
6812 }
6813 break;
6814 case TARGET_NR_setgroups:
6815 {
6816 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006817 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006818 gid_t *grouplist;
6819 int i;
6820
6821 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006822 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6823 if (!target_grouplist) {
6824 ret = -TARGET_EFAULT;
6825 goto fail;
6826 }
bellard67867302003-11-23 17:05:30 +00006827 for(i = 0;i < gidsetsize; i++)
6828 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006829 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00006830 ret = get_errno(setgroups(gidsetsize, grouplist));
6831 }
6832 break;
6833 case TARGET_NR_fchown:
6834 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6835 break;
thsccfa72b2007-09-24 09:23:34 +00006836#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6837 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00006838 if (!(p = lock_user_string(arg2)))
6839 goto efault;
6840 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6841 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00006842 break;
6843#endif
bellard67867302003-11-23 17:05:30 +00006844#ifdef TARGET_NR_setresuid
6845 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00006846 ret = get_errno(setresuid(low2highuid(arg1),
6847 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00006848 low2highuid(arg3)));
6849 break;
6850#endif
6851#ifdef TARGET_NR_getresuid
6852 case TARGET_NR_getresuid:
6853 {
pbrook53a59602006-03-25 19:31:22 +00006854 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00006855 ret = get_errno(getresuid(&ruid, &euid, &suid));
6856 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006857 if (put_user_u16(high2lowuid(ruid), arg1)
6858 || put_user_u16(high2lowuid(euid), arg2)
6859 || put_user_u16(high2lowuid(suid), arg3))
6860 goto efault;
bellard67867302003-11-23 17:05:30 +00006861 }
6862 }
6863 break;
6864#endif
6865#ifdef TARGET_NR_getresgid
6866 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00006867 ret = get_errno(setresgid(low2highgid(arg1),
6868 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00006869 low2highgid(arg3)));
6870 break;
6871#endif
6872#ifdef TARGET_NR_getresgid
6873 case TARGET_NR_getresgid:
6874 {
pbrook53a59602006-03-25 19:31:22 +00006875 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00006876 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6877 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006878 if (put_user_u16(high2lowgid(rgid), arg1)
6879 || put_user_u16(high2lowgid(egid), arg2)
6880 || put_user_u16(high2lowgid(sgid), arg3))
6881 goto efault;
bellard67867302003-11-23 17:05:30 +00006882 }
6883 }
6884 break;
6885#endif
6886 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00006887 if (!(p = lock_user_string(arg1)))
6888 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006889 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6890 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006891 break;
6892 case TARGET_NR_setuid:
6893 ret = get_errno(setuid(low2highuid(arg1)));
6894 break;
6895 case TARGET_NR_setgid:
6896 ret = get_errno(setgid(low2highgid(arg1)));
6897 break;
6898 case TARGET_NR_setfsuid:
6899 ret = get_errno(setfsuid(arg1));
6900 break;
6901 case TARGET_NR_setfsgid:
6902 ret = get_errno(setfsgid(arg1));
6903 break;
6904#endif /* USE_UID16 */
6905
bellarda315a142005-01-30 22:59:18 +00006906#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00006907 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00006908 if (!(p = lock_user_string(arg1)))
6909 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006910 ret = get_errno(lchown(p, arg2, arg3));
6911 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006912 break;
bellarda315a142005-01-30 22:59:18 +00006913#endif
6914#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00006915 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00006916 ret = get_errno(getuid());
6917 break;
bellarda315a142005-01-30 22:59:18 +00006918#endif
aurel3264b4d282008-11-14 17:20:15 +00006919
6920#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6921 /* Alpha specific */
6922 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006923 {
6924 uid_t euid;
6925 euid=geteuid();
6926 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6927 }
aurel3264b4d282008-11-14 17:20:15 +00006928 ret = get_errno(getuid());
6929 break;
6930#endif
6931#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6932 /* Alpha specific */
6933 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006934 {
6935 uid_t egid;
6936 egid=getegid();
6937 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6938 }
aurel3264b4d282008-11-14 17:20:15 +00006939 ret = get_errno(getgid());
6940 break;
6941#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08006942#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
6943 /* Alpha specific */
6944 case TARGET_NR_osf_getsysinfo:
6945 ret = -TARGET_EOPNOTSUPP;
6946 switch (arg1) {
6947 case TARGET_GSI_IEEE_FP_CONTROL:
6948 {
6949 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
6950
6951 /* Copied from linux ieee_fpcr_to_swcr. */
6952 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
6953 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
6954 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
6955 | SWCR_TRAP_ENABLE_DZE
6956 | SWCR_TRAP_ENABLE_OVF);
6957 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
6958 | SWCR_TRAP_ENABLE_INE);
6959 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
6960 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
6961
6962 if (put_user_u64 (swcr, arg2))
6963 goto efault;
6964 ret = 0;
6965 }
6966 break;
6967
6968 /* case GSI_IEEE_STATE_AT_SIGNAL:
6969 -- Not implemented in linux kernel.
6970 case GSI_UACPROC:
6971 -- Retrieves current unaligned access state; not much used.
6972 case GSI_PROC_TYPE:
6973 -- Retrieves implver information; surely not used.
6974 case GSI_GET_HWRPB:
6975 -- Grabs a copy of the HWRPB; surely not used.
6976 */
6977 }
6978 break;
6979#endif
6980#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
6981 /* Alpha specific */
6982 case TARGET_NR_osf_setsysinfo:
6983 ret = -TARGET_EOPNOTSUPP;
6984 switch (arg1) {
6985 case TARGET_SSI_IEEE_FP_CONTROL:
6986 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
6987 {
6988 uint64_t swcr, fpcr, orig_fpcr;
6989
6990 if (get_user_u64 (swcr, arg2))
6991 goto efault;
6992 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
6993 fpcr = orig_fpcr & FPCR_DYN_MASK;
6994
6995 /* Copied from linux ieee_swcr_to_fpcr. */
6996 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
6997 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
6998 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
6999 | SWCR_TRAP_ENABLE_DZE
7000 | SWCR_TRAP_ENABLE_OVF)) << 48;
7001 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
7002 | SWCR_TRAP_ENABLE_INE)) << 57;
7003 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
7004 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
7005
7006 cpu_alpha_store_fpcr (cpu_env, fpcr);
7007 ret = 0;
7008
7009 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
7010 /* Old exceptions are not signaled. */
7011 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
7012
7013 /* If any exceptions set by this call, and are unmasked,
7014 send a signal. */
7015 /* ??? FIXME */
7016 }
7017 }
7018 break;
7019
7020 /* case SSI_NVPAIRS:
7021 -- Used with SSIN_UACPROC to enable unaligned accesses.
7022 case SSI_IEEE_STATE_AT_SIGNAL:
7023 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
7024 -- Not implemented in linux kernel
7025 */
7026 }
7027 break;
7028#endif
7029#ifdef TARGET_NR_osf_sigprocmask
7030 /* Alpha specific. */
7031 case TARGET_NR_osf_sigprocmask:
7032 {
7033 abi_ulong mask;
7034 int how = arg1;
7035 sigset_t set, oldset;
7036
7037 switch(arg1) {
7038 case TARGET_SIG_BLOCK:
7039 how = SIG_BLOCK;
7040 break;
7041 case TARGET_SIG_UNBLOCK:
7042 how = SIG_UNBLOCK;
7043 break;
7044 case TARGET_SIG_SETMASK:
7045 how = SIG_SETMASK;
7046 break;
7047 default:
7048 ret = -TARGET_EINVAL;
7049 goto fail;
7050 }
7051 mask = arg2;
7052 target_to_host_old_sigset(&set, &mask);
7053 sigprocmask(arg1, &set, &oldset);
7054 host_to_target_old_sigset(&mask, &oldset);
7055 ret = mask;
7056 }
7057 break;
7058#endif
aurel3264b4d282008-11-14 17:20:15 +00007059
bellarda315a142005-01-30 22:59:18 +00007060#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00007061 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00007062 ret = get_errno(getgid());
7063 break;
bellarda315a142005-01-30 22:59:18 +00007064#endif
7065#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00007066 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00007067 ret = get_errno(geteuid());
7068 break;
bellarda315a142005-01-30 22:59:18 +00007069#endif
7070#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00007071 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00007072 ret = get_errno(getegid());
7073 break;
bellarda315a142005-01-30 22:59:18 +00007074#endif
7075#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00007076 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00007077 ret = get_errno(setreuid(arg1, arg2));
7078 break;
bellarda315a142005-01-30 22:59:18 +00007079#endif
7080#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00007081 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00007082 ret = get_errno(setregid(arg1, arg2));
7083 break;
bellarda315a142005-01-30 22:59:18 +00007084#endif
7085#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00007086 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00007087 {
7088 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007089 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007090 gid_t *grouplist;
7091 int i;
7092
7093 grouplist = alloca(gidsetsize * sizeof(gid_t));
7094 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007095 if (gidsetsize == 0)
7096 break;
bellard99c475a2005-01-31 20:45:13 +00007097 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007098 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
7099 if (!target_grouplist) {
7100 ret = -TARGET_EFAULT;
7101 goto fail;
7102 }
balroga2155fc2008-09-20 02:12:08 +00007103 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00007104 target_grouplist[i] = tswap32(grouplist[i]);
7105 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00007106 }
7107 }
7108 break;
bellarda315a142005-01-30 22:59:18 +00007109#endif
7110#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00007111 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00007112 {
7113 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007114 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007115 gid_t *grouplist;
7116 int i;
ths3b46e622007-09-17 08:09:54 +00007117
bellard99c475a2005-01-31 20:45:13 +00007118 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007119 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
7120 if (!target_grouplist) {
7121 ret = -TARGET_EFAULT;
7122 goto fail;
7123 }
bellard99c475a2005-01-31 20:45:13 +00007124 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00007125 grouplist[i] = tswap32(target_grouplist[i]);
7126 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00007127 ret = get_errno(setgroups(gidsetsize, grouplist));
7128 }
7129 break;
bellarda315a142005-01-30 22:59:18 +00007130#endif
7131#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00007132 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00007133 ret = get_errno(fchown(arg1, arg2, arg3));
7134 break;
bellarda315a142005-01-30 22:59:18 +00007135#endif
7136#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00007137 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00007138 ret = get_errno(setresuid(arg1, arg2, arg3));
7139 break;
bellarda315a142005-01-30 22:59:18 +00007140#endif
7141#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00007142 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00007143 {
pbrook53a59602006-03-25 19:31:22 +00007144 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00007145 ret = get_errno(getresuid(&ruid, &euid, &suid));
7146 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007147 if (put_user_u32(ruid, arg1)
7148 || put_user_u32(euid, arg2)
7149 || put_user_u32(suid, arg3))
7150 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00007151 }
7152 }
7153 break;
bellarda315a142005-01-30 22:59:18 +00007154#endif
7155#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00007156 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00007157 ret = get_errno(setresgid(arg1, arg2, arg3));
7158 break;
bellarda315a142005-01-30 22:59:18 +00007159#endif
7160#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00007161 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00007162 {
pbrook53a59602006-03-25 19:31:22 +00007163 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00007164 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7165 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007166 if (put_user_u32(rgid, arg1)
7167 || put_user_u32(egid, arg2)
7168 || put_user_u32(sgid, arg3))
7169 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00007170 }
7171 }
7172 break;
bellarda315a142005-01-30 22:59:18 +00007173#endif
7174#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00007175 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00007176 if (!(p = lock_user_string(arg1)))
7177 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007178 ret = get_errno(chown(p, arg2, arg3));
7179 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007180 break;
bellarda315a142005-01-30 22:59:18 +00007181#endif
7182#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00007183 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00007184 ret = get_errno(setuid(arg1));
7185 break;
bellarda315a142005-01-30 22:59:18 +00007186#endif
7187#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00007188 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00007189 ret = get_errno(setgid(arg1));
7190 break;
bellarda315a142005-01-30 22:59:18 +00007191#endif
7192#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00007193 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00007194 ret = get_errno(setfsuid(arg1));
7195 break;
bellarda315a142005-01-30 22:59:18 +00007196#endif
7197#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00007198 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00007199 ret = get_errno(setfsgid(arg1));
7200 break;
bellarda315a142005-01-30 22:59:18 +00007201#endif
bellard67867302003-11-23 17:05:30 +00007202
bellard31e31b82003-02-18 22:55:36 +00007203 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00007204 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00007205#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00007206 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00007207 {
7208 void *a;
7209 ret = -TARGET_EFAULT;
7210 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
7211 goto efault;
7212 if (!(p = lock_user_string(arg3)))
7213 goto mincore_fail;
7214 ret = get_errno(mincore(a, arg2, p));
7215 unlock_user(p, arg3, ret);
7216 mincore_fail:
7217 unlock_user(a, arg1, 0);
7218 }
7219 break;
bellardffa65c32004-01-04 23:57:22 +00007220#endif
aurel32408321b2008-10-01 21:46:32 +00007221#ifdef TARGET_NR_arm_fadvise64_64
7222 case TARGET_NR_arm_fadvise64_64:
7223 {
7224 /*
7225 * arm_fadvise64_64 looks like fadvise64_64 but
7226 * with different argument order
7227 */
7228 abi_long temp;
7229 temp = arg3;
7230 arg3 = arg4;
7231 arg4 = temp;
7232 }
7233#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02007234#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00007235#ifdef TARGET_NR_fadvise64_64
7236 case TARGET_NR_fadvise64_64:
7237#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02007238#ifdef TARGET_NR_fadvise64
7239 case TARGET_NR_fadvise64:
7240#endif
7241#ifdef TARGET_S390X
7242 switch (arg4) {
7243 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
7244 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
7245 case 6: arg4 = POSIX_FADV_DONTNEED; break;
7246 case 7: arg4 = POSIX_FADV_NOREUSE; break;
7247 default: break;
7248 }
7249#endif
7250 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00007251 break;
7252#endif
bellardffa65c32004-01-04 23:57:22 +00007253#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00007254 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00007255 /* A straight passthrough may not be safe because qemu sometimes
7256 turns private flie-backed mappings into anonymous mappings.
7257 This will break MADV_DONTNEED.
7258 This is a hint, so ignoring and returning success is ok. */
7259 ret = get_errno(0);
7260 break;
bellardffa65c32004-01-04 23:57:22 +00007261#endif
blueswir1992f48a2007-10-14 16:27:31 +00007262#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00007263 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00007264 {
thsb1e341e2007-03-20 21:50:52 +00007265 int cmd;
bellard77e46722003-04-29 20:39:06 +00007266 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00007267 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00007268#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00007269 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00007270#endif
bellard77e46722003-04-29 20:39:06 +00007271
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007272 cmd = target_to_host_fcntl_cmd(arg2);
7273 if (cmd == -TARGET_EINVAL)
7274 return cmd;
thsb1e341e2007-03-20 21:50:52 +00007275
bellard60cd49d2003-03-16 22:53:56 +00007276 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00007277 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00007278#ifdef TARGET_ARM
7279 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007280 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7281 goto efault;
ths58134272007-03-31 18:59:32 +00007282 fl.l_type = tswap16(target_efl->l_type);
7283 fl.l_whence = tswap16(target_efl->l_whence);
7284 fl.l_start = tswap64(target_efl->l_start);
7285 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007286 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00007287 unlock_user_struct(target_efl, arg3, 0);
7288 } else
7289#endif
7290 {
bellard9ee1fa22007-11-11 15:11:19 +00007291 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7292 goto efault;
ths58134272007-03-31 18:59:32 +00007293 fl.l_type = tswap16(target_fl->l_type);
7294 fl.l_whence = tswap16(target_fl->l_whence);
7295 fl.l_start = tswap64(target_fl->l_start);
7296 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007297 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00007298 unlock_user_struct(target_fl, arg3, 0);
7299 }
thsb1e341e2007-03-20 21:50:52 +00007300 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00007301 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00007302#ifdef TARGET_ARM
7303 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007304 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
7305 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007306 target_efl->l_type = tswap16(fl.l_type);
7307 target_efl->l_whence = tswap16(fl.l_whence);
7308 target_efl->l_start = tswap64(fl.l_start);
7309 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007310 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00007311 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00007312 } else
7313#endif
7314 {
bellard9ee1fa22007-11-11 15:11:19 +00007315 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
7316 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007317 target_fl->l_type = tswap16(fl.l_type);
7318 target_fl->l_whence = tswap16(fl.l_whence);
7319 target_fl->l_start = tswap64(fl.l_start);
7320 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007321 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00007322 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00007323 }
bellard77e46722003-04-29 20:39:06 +00007324 }
7325 break;
7326
thsb1e341e2007-03-20 21:50:52 +00007327 case TARGET_F_SETLK64:
7328 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00007329#ifdef TARGET_ARM
7330 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007331 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7332 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007333 fl.l_type = tswap16(target_efl->l_type);
7334 fl.l_whence = tswap16(target_efl->l_whence);
7335 fl.l_start = tswap64(target_efl->l_start);
7336 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007337 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00007338 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00007339 } else
7340#endif
7341 {
bellard9ee1fa22007-11-11 15:11:19 +00007342 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7343 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007344 fl.l_type = tswap16(target_fl->l_type);
7345 fl.l_whence = tswap16(target_fl->l_whence);
7346 fl.l_start = tswap64(target_fl->l_start);
7347 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007348 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00007349 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00007350 }
thsb1e341e2007-03-20 21:50:52 +00007351 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00007352 break;
bellard60cd49d2003-03-16 22:53:56 +00007353 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007354 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00007355 break;
7356 }
bellard77e46722003-04-29 20:39:06 +00007357 break;
7358 }
bellard60cd49d2003-03-16 22:53:56 +00007359#endif
ths7d600c82006-12-08 01:32:58 +00007360#ifdef TARGET_NR_cacheflush
7361 case TARGET_NR_cacheflush:
7362 /* self-modifying code is handled automatically, so nothing needed */
7363 ret = 0;
7364 break;
7365#endif
bellardebc05482003-09-30 21:08:41 +00007366#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00007367 case TARGET_NR_security:
7368 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007369#endif
bellardc573ff62004-01-04 15:51:36 +00007370#ifdef TARGET_NR_getpagesize
7371 case TARGET_NR_getpagesize:
7372 ret = TARGET_PAGE_SIZE;
7373 break;
7374#endif
bellard31e31b82003-02-18 22:55:36 +00007375 case TARGET_NR_gettid:
7376 ret = get_errno(gettid());
7377 break;
thse5febef2007-04-01 18:31:35 +00007378#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00007379 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00007380#if TARGET_ABI_BITS == 32
7381#ifdef TARGET_ARM
7382 if (((CPUARMState *)cpu_env)->eabi)
7383 {
7384 arg2 = arg3;
7385 arg3 = arg4;
7386 arg4 = arg5;
7387 }
7388#endif
7389 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
7390#else
7391 ret = get_errno(readahead(arg1, arg2, arg3));
7392#endif
7393 break;
thse5febef2007-04-01 18:31:35 +00007394#endif
bellardebc05482003-09-30 21:08:41 +00007395#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00007396 case TARGET_NR_setxattr:
7397 case TARGET_NR_lsetxattr:
7398 case TARGET_NR_fsetxattr:
7399 case TARGET_NR_getxattr:
7400 case TARGET_NR_lgetxattr:
7401 case TARGET_NR_fgetxattr:
7402 case TARGET_NR_listxattr:
7403 case TARGET_NR_llistxattr:
7404 case TARGET_NR_flistxattr:
7405 case TARGET_NR_removexattr:
7406 case TARGET_NR_lremovexattr:
7407 case TARGET_NR_fremovexattr:
Arnaud Patard6f932f92009-04-21 21:04:18 +03007408 ret = -TARGET_EOPNOTSUPP;
7409 break;
bellardebc05482003-09-30 21:08:41 +00007410#endif
7411#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00007412 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007413#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00007414 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
7415 ret = 0;
7416 break;
edgar_iglef967792009-01-07 14:19:38 +00007417#elif defined(TARGET_CRIS)
7418 if (arg1 & 0xff)
7419 ret = -TARGET_EINVAL;
7420 else {
7421 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
7422 ret = 0;
7423 }
7424 break;
bellard8d18e892007-11-14 15:18:40 +00007425#elif defined(TARGET_I386) && defined(TARGET_ABI32)
7426 ret = do_set_thread_area(cpu_env, arg1);
7427 break;
ths6f5b89a2007-03-02 20:48:00 +00007428#else
7429 goto unimplemented_nowarn;
7430#endif
7431#endif
7432#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00007433 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007434#if defined(TARGET_I386) && defined(TARGET_ABI32)
7435 ret = do_get_thread_area(cpu_env, arg1);
7436#else
bellard5cd43932003-03-29 16:54:36 +00007437 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00007438#endif
bellard8d18e892007-11-14 15:18:40 +00007439#endif
bellard48dc41e2006-06-21 18:15:50 +00007440#ifdef TARGET_NR_getdomainname
7441 case TARGET_NR_getdomainname:
7442 goto unimplemented_nowarn;
7443#endif
ths6f5b89a2007-03-02 20:48:00 +00007444
thsb5906f92007-03-19 13:32:45 +00007445#ifdef TARGET_NR_clock_gettime
7446 case TARGET_NR_clock_gettime:
7447 {
7448 struct timespec ts;
7449 ret = get_errno(clock_gettime(arg1, &ts));
7450 if (!is_error(ret)) {
7451 host_to_target_timespec(arg2, &ts);
7452 }
7453 break;
7454 }
7455#endif
7456#ifdef TARGET_NR_clock_getres
7457 case TARGET_NR_clock_getres:
7458 {
7459 struct timespec ts;
7460 ret = get_errno(clock_getres(arg1, &ts));
7461 if (!is_error(ret)) {
7462 host_to_target_timespec(arg2, &ts);
7463 }
7464 break;
7465 }
7466#endif
pbrook63d76512008-05-29 13:43:29 +00007467#ifdef TARGET_NR_clock_nanosleep
7468 case TARGET_NR_clock_nanosleep:
7469 {
7470 struct timespec ts;
7471 target_to_host_timespec(&ts, arg3);
7472 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7473 if (arg4)
7474 host_to_target_timespec(arg4, &ts);
7475 break;
7476 }
7477#endif
thsb5906f92007-03-19 13:32:45 +00007478
ths6f5b89a2007-03-02 20:48:00 +00007479#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7480 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00007481 ret = get_errno(set_tid_address((int *)g2h(arg1)));
7482 break;
ths6f5b89a2007-03-02 20:48:00 +00007483#endif
7484
ths3ae43202007-09-16 21:39:48 +00007485#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00007486 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00007487 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00007488 break;
7489#endif
7490
ths3ae43202007-09-16 21:39:48 +00007491#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00007492 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00007493 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7494 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00007495 break;
7496#endif
7497
ths4f2b1fe2007-06-21 21:57:12 +00007498#ifdef TARGET_NR_set_robust_list
7499 case TARGET_NR_set_robust_list:
7500 goto unimplemented_nowarn;
7501#endif
7502
ths9007f0e2007-09-25 17:50:37 +00007503#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7504 case TARGET_NR_utimensat:
7505 {
Riku Voipioebc996f2009-04-21 15:01:51 +03007506 struct timespec *tsp, ts[2];
7507 if (!arg3) {
7508 tsp = NULL;
7509 } else {
7510 target_to_host_timespec(ts, arg3);
7511 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7512 tsp = ts;
7513 }
ths9007f0e2007-09-25 17:50:37 +00007514 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03007515 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00007516 else {
bellard579a97f2007-11-11 14:26:47 +00007517 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00007518 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00007519 goto fail;
7520 }
Riku Voipioebc996f2009-04-21 15:01:51 +03007521 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00007522 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00007523 }
7524 }
7525 break;
7526#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02007527#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00007528 case TARGET_NR_futex:
7529 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7530 break;
7531#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007532#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00007533 case TARGET_NR_inotify_init:
7534 ret = get_errno(sys_inotify_init());
7535 break;
7536#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007537#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00007538#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7539 case TARGET_NR_inotify_init1:
7540 ret = get_errno(sys_inotify_init1(arg1));
7541 break;
7542#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007543#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007544#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00007545 case TARGET_NR_inotify_add_watch:
7546 p = lock_user_string(arg2);
7547 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7548 unlock_user(p, arg2, 0);
7549 break;
7550#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007551#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00007552 case TARGET_NR_inotify_rm_watch:
7553 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7554 break;
7555#endif
ths9007f0e2007-09-25 17:50:37 +00007556
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07007557#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00007558 case TARGET_NR_mq_open:
7559 {
7560 struct mq_attr posix_mq_attr;
7561
7562 p = lock_user_string(arg1 - 1);
7563 if (arg4 != 0)
7564 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7565 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7566 unlock_user (p, arg1, 0);
7567 }
7568 break;
7569
7570 case TARGET_NR_mq_unlink:
7571 p = lock_user_string(arg1 - 1);
7572 ret = get_errno(mq_unlink(p));
7573 unlock_user (p, arg1, 0);
7574 break;
7575
7576 case TARGET_NR_mq_timedsend:
7577 {
7578 struct timespec ts;
7579
7580 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7581 if (arg5 != 0) {
7582 target_to_host_timespec(&ts, arg5);
7583 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7584 host_to_target_timespec(arg5, &ts);
7585 }
7586 else
7587 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7588 unlock_user (p, arg2, arg3);
7589 }
7590 break;
7591
7592 case TARGET_NR_mq_timedreceive:
7593 {
7594 struct timespec ts;
7595 unsigned int prio;
7596
7597 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7598 if (arg5 != 0) {
7599 target_to_host_timespec(&ts, arg5);
7600 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7601 host_to_target_timespec(arg5, &ts);
7602 }
7603 else
7604 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7605 unlock_user (p, arg2, arg3);
7606 if (arg4 != 0)
7607 put_user_u32(prio, arg4);
7608 }
7609 break;
7610
7611 /* Not implemented for now... */
7612/* case TARGET_NR_mq_notify: */
7613/* break; */
7614
7615 case TARGET_NR_mq_getsetattr:
7616 {
7617 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7618 ret = 0;
7619 if (arg3 != 0) {
7620 ret = mq_getattr(arg1, &posix_mq_attr_out);
7621 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7622 }
7623 if (arg2 != 0) {
7624 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7625 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7626 }
7627
7628 }
7629 break;
7630#endif
7631
vibisreenivasan3ce34df2009-05-16 18:32:41 +05307632#ifdef CONFIG_SPLICE
7633#ifdef TARGET_NR_tee
7634 case TARGET_NR_tee:
7635 {
7636 ret = get_errno(tee(arg1,arg2,arg3,arg4));
7637 }
7638 break;
7639#endif
7640#ifdef TARGET_NR_splice
7641 case TARGET_NR_splice:
7642 {
7643 loff_t loff_in, loff_out;
7644 loff_t *ploff_in = NULL, *ploff_out = NULL;
7645 if(arg2) {
7646 get_user_u64(loff_in, arg2);
7647 ploff_in = &loff_in;
7648 }
7649 if(arg4) {
7650 get_user_u64(loff_out, arg2);
7651 ploff_out = &loff_out;
7652 }
7653 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7654 }
7655 break;
7656#endif
7657#ifdef TARGET_NR_vmsplice
7658 case TARGET_NR_vmsplice:
7659 {
7660 int count = arg3;
7661 struct iovec *vec;
7662
7663 vec = alloca(count * sizeof(struct iovec));
7664 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7665 goto efault;
7666 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7667 unlock_iovec(vec, arg2, count, 0);
7668 }
7669 break;
7670#endif
7671#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03007672#ifdef CONFIG_EVENTFD
7673#if defined(TARGET_NR_eventfd)
7674 case TARGET_NR_eventfd:
7675 ret = get_errno(eventfd(arg1, 0));
7676 break;
7677#endif
7678#if defined(TARGET_NR_eventfd2)
7679 case TARGET_NR_eventfd2:
7680 ret = get_errno(eventfd(arg1, arg2));
7681 break;
7682#endif
7683#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03007684#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7685 case TARGET_NR_fallocate:
7686 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7687 break;
7688#endif
Peter Maydellc727f472011-01-06 11:05:10 +00007689#if defined(CONFIG_SYNC_FILE_RANGE)
7690#if defined(TARGET_NR_sync_file_range)
7691 case TARGET_NR_sync_file_range:
7692#if TARGET_ABI_BITS == 32
7693 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
7694 target_offset64(arg4, arg5), arg6));
7695#else
7696 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
7697#endif
7698 break;
7699#endif
7700#if defined(TARGET_NR_sync_file_range2)
7701 case TARGET_NR_sync_file_range2:
7702 /* This is like sync_file_range but the arguments are reordered */
7703#if TARGET_ABI_BITS == 32
7704 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
7705 target_offset64(arg5, arg6), arg2));
7706#else
7707 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
7708#endif
7709 break;
7710#endif
7711#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00007712#if defined(CONFIG_EPOLL)
7713#if defined(TARGET_NR_epoll_create)
7714 case TARGET_NR_epoll_create:
7715 ret = get_errno(epoll_create(arg1));
7716 break;
7717#endif
7718#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
7719 case TARGET_NR_epoll_create1:
7720 ret = get_errno(epoll_create1(arg1));
7721 break;
7722#endif
7723#if defined(TARGET_NR_epoll_ctl)
7724 case TARGET_NR_epoll_ctl:
7725 {
7726 struct epoll_event ep;
7727 struct epoll_event *epp = 0;
7728 if (arg4) {
7729 struct target_epoll_event *target_ep;
7730 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
7731 goto efault;
7732 }
7733 ep.events = tswap32(target_ep->events);
7734 /* The epoll_data_t union is just opaque data to the kernel,
7735 * so we transfer all 64 bits across and need not worry what
7736 * actual data type it is.
7737 */
7738 ep.data.u64 = tswap64(target_ep->data.u64);
7739 unlock_user_struct(target_ep, arg4, 0);
7740 epp = &ep;
7741 }
7742 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
7743 break;
7744 }
7745#endif
7746
7747#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
7748#define IMPLEMENT_EPOLL_PWAIT
7749#endif
7750#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
7751#if defined(TARGET_NR_epoll_wait)
7752 case TARGET_NR_epoll_wait:
7753#endif
7754#if defined(IMPLEMENT_EPOLL_PWAIT)
7755 case TARGET_NR_epoll_pwait:
7756#endif
7757 {
7758 struct target_epoll_event *target_ep;
7759 struct epoll_event *ep;
7760 int epfd = arg1;
7761 int maxevents = arg3;
7762 int timeout = arg4;
7763
7764 target_ep = lock_user(VERIFY_WRITE, arg2,
7765 maxevents * sizeof(struct target_epoll_event), 1);
7766 if (!target_ep) {
7767 goto efault;
7768 }
7769
7770 ep = alloca(maxevents * sizeof(struct epoll_event));
7771
7772 switch (num) {
7773#if defined(IMPLEMENT_EPOLL_PWAIT)
7774 case TARGET_NR_epoll_pwait:
7775 {
7776 target_sigset_t *target_set;
7777 sigset_t _set, *set = &_set;
7778
7779 if (arg5) {
7780 target_set = lock_user(VERIFY_READ, arg5,
7781 sizeof(target_sigset_t), 1);
7782 if (!target_set) {
7783 unlock_user(target_ep, arg2, 0);
7784 goto efault;
7785 }
7786 target_to_host_sigset(set, target_set);
7787 unlock_user(target_set, arg5, 0);
7788 } else {
7789 set = NULL;
7790 }
7791
7792 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
7793 break;
7794 }
7795#endif
7796#if defined(TARGET_NR_epoll_wait)
7797 case TARGET_NR_epoll_wait:
7798 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
7799 break;
7800#endif
7801 default:
7802 ret = -TARGET_ENOSYS;
7803 }
7804 if (!is_error(ret)) {
7805 int i;
7806 for (i = 0; i < ret; i++) {
7807 target_ep[i].events = tswap32(ep[i].events);
7808 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
7809 }
7810 }
7811 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
7812 break;
7813 }
7814#endif
7815#endif
bellard31e31b82003-02-18 22:55:36 +00007816 default:
7817 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00007818 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00007819#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 +00007820 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00007821#endif
ths0da46a62007-10-20 20:23:07 +00007822 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00007823 break;
7824 }
bellard579a97f2007-11-11 14:26:47 +00007825fail:
bellardc573ff62004-01-04 15:51:36 +00007826#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00007827 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00007828#endif
thsb92c47c2007-11-01 00:07:38 +00007829 if(do_strace)
7830 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00007831 return ret;
bellard579a97f2007-11-11 14:26:47 +00007832efault:
7833 ret = -TARGET_EFAULT;
7834 goto fail;
bellard31e31b82003-02-18 22:55:36 +00007835}