blob: e969d1b61d5db94c1a1a6a6504a64d891040bfc8 [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
Riku Voipio0c866a72011-04-18 15:23:06 +0300331#if defined(TARGET_NR_fchownat)
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
Riku Voipio0c866a72011-04-18 15:23:06 +0300440#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
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}
Riku Voipio0c866a72011-04-18 15:23:06 +03004167static inline int tswapid(int id)
4168{
4169 return tswap16(id);
4170}
4171#else /* !USE_UID16 */
4172static inline int high2lowuid(int uid)
4173{
4174 return uid;
4175}
4176static inline int high2lowgid(int gid)
4177{
4178 return gid;
4179}
4180static inline int low2highuid(int uid)
4181{
4182 return uid;
4183}
4184static inline int low2highgid(int gid)
4185{
4186 return gid;
4187}
4188static inline int tswapid(int id)
4189{
4190 return tswap32(id);
4191}
bellard67867302003-11-23 17:05:30 +00004192#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004193
bellard31e31b82003-02-18 22:55:36 +00004194void syscall_init(void)
4195{
bellard2ab83ea2003-06-15 19:56:46 +00004196 IOCTLEntry *ie;
4197 const argtype *arg_type;
4198 int size;
thsb92c47c2007-11-01 00:07:38 +00004199 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004200
Blue Swirl001faf32009-05-13 17:53:17 +00004201#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004202#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004203#include "syscall_types.h"
4204#undef STRUCT
4205#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00004206
4207 /* we patch the ioctl size if necessary. We rely on the fact that
4208 no ioctl has all the bits at '1' in the size field */
4209 ie = ioctl_entries;
4210 while (ie->target_cmd != 0) {
4211 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4212 TARGET_IOC_SIZEMASK) {
4213 arg_type = ie->arg_type;
4214 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00004215 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00004216 ie->target_cmd);
4217 exit(1);
4218 }
4219 arg_type++;
4220 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00004221 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00004222 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4223 (size << TARGET_IOC_SIZESHIFT);
4224 }
thsb92c47c2007-11-01 00:07:38 +00004225
4226 /* Build target_to_host_errno_table[] table from
4227 * host_to_target_errno_table[]. */
4228 for (i=0; i < ERRNO_TABLE_SIZE; i++)
4229 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4230
bellard2ab83ea2003-06-15 19:56:46 +00004231 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00004232#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4233 (defined(__x86_64__) && defined(TARGET_X86_64))
4234 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4235 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4236 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00004237 }
4238#endif
4239 ie++;
4240 }
bellard31e31b82003-02-18 22:55:36 +00004241}
bellardc573ff62004-01-04 15:51:36 +00004242
blueswir1992f48a2007-10-14 16:27:31 +00004243#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004244static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4245{
thsaf325d32008-06-10 15:29:15 +00004246#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004247 return ((uint64_t)word0 << 32) | word1;
4248#else
4249 return ((uint64_t)word1 << 32) | word0;
4250#endif
4251}
blueswir1992f48a2007-10-14 16:27:31 +00004252#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004253static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4254{
4255 return word0;
4256}
blueswir1992f48a2007-10-14 16:27:31 +00004257#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004258
4259#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004260static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4261 abi_long arg2,
4262 abi_long arg3,
4263 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004264{
4265#ifdef TARGET_ARM
4266 if (((CPUARMState *)cpu_env)->eabi)
4267 {
4268 arg2 = arg3;
4269 arg3 = arg4;
4270 }
4271#endif
4272 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4273}
4274#endif
4275
4276#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004277static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4278 abi_long arg2,
4279 abi_long arg3,
4280 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004281{
4282#ifdef TARGET_ARM
4283 if (((CPUARMState *)cpu_env)->eabi)
4284 {
4285 arg2 = arg3;
4286 arg3 = arg4;
4287 }
4288#endif
4289 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4290}
4291#endif
4292
bellard579a97f2007-11-11 14:26:47 +00004293static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4294 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004295{
4296 struct target_timespec *target_ts;
4297
bellard579a97f2007-11-11 14:26:47 +00004298 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4299 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004300 host_ts->tv_sec = tswapl(target_ts->tv_sec);
4301 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
4302 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004303 return 0;
pbrook53a59602006-03-25 19:31:22 +00004304}
4305
bellard579a97f2007-11-11 14:26:47 +00004306static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4307 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004308{
4309 struct target_timespec *target_ts;
4310
bellard579a97f2007-11-11 14:26:47 +00004311 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4312 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004313 target_ts->tv_sec = tswapl(host_ts->tv_sec);
4314 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
4315 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004316 return 0;
pbrook53a59602006-03-25 19:31:22 +00004317}
4318
aurel329d33b762009-04-08 23:07:05 +00004319#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004320static inline abi_long host_to_target_stat64(void *cpu_env,
4321 abi_ulong target_addr,
4322 struct stat *host_st)
4323{
4324#ifdef TARGET_ARM
4325 if (((CPUARMState *)cpu_env)->eabi) {
4326 struct target_eabi_stat64 *target_st;
4327
4328 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4329 return -TARGET_EFAULT;
4330 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4331 __put_user(host_st->st_dev, &target_st->st_dev);
4332 __put_user(host_st->st_ino, &target_st->st_ino);
4333#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4334 __put_user(host_st->st_ino, &target_st->__st_ino);
4335#endif
4336 __put_user(host_st->st_mode, &target_st->st_mode);
4337 __put_user(host_st->st_nlink, &target_st->st_nlink);
4338 __put_user(host_st->st_uid, &target_st->st_uid);
4339 __put_user(host_st->st_gid, &target_st->st_gid);
4340 __put_user(host_st->st_rdev, &target_st->st_rdev);
4341 __put_user(host_st->st_size, &target_st->st_size);
4342 __put_user(host_st->st_blksize, &target_st->st_blksize);
4343 __put_user(host_st->st_blocks, &target_st->st_blocks);
4344 __put_user(host_st->st_atime, &target_st->target_st_atime);
4345 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4346 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4347 unlock_user_struct(target_st, target_addr, 1);
4348 } else
4349#endif
4350 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004351#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004352 struct target_stat *target_st;
4353#else
balrog6a24a772008-09-20 02:23:36 +00004354 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004355#endif
balrog6a24a772008-09-20 02:23:36 +00004356
4357 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4358 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004359 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004360 __put_user(host_st->st_dev, &target_st->st_dev);
4361 __put_user(host_st->st_ino, &target_st->st_ino);
4362#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4363 __put_user(host_st->st_ino, &target_st->__st_ino);
4364#endif
4365 __put_user(host_st->st_mode, &target_st->st_mode);
4366 __put_user(host_st->st_nlink, &target_st->st_nlink);
4367 __put_user(host_st->st_uid, &target_st->st_uid);
4368 __put_user(host_st->st_gid, &target_st->st_gid);
4369 __put_user(host_st->st_rdev, &target_st->st_rdev);
4370 /* XXX: better use of kernel struct */
4371 __put_user(host_st->st_size, &target_st->st_size);
4372 __put_user(host_st->st_blksize, &target_st->st_blksize);
4373 __put_user(host_st->st_blocks, &target_st->st_blocks);
4374 __put_user(host_st->st_atime, &target_st->target_st_atime);
4375 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4376 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4377 unlock_user_struct(target_st, target_addr, 1);
4378 }
4379
4380 return 0;
4381}
4382#endif
4383
Juan Quintela2f7bb872009-07-27 16:13:24 +02004384#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004385/* ??? Using host futex calls even when target atomic operations
4386 are not really atomic probably breaks things. However implementing
4387 futexes locally would make futexes shared between multiple processes
4388 tricky. However they're probably useless because guest atomic
4389 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004390static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4391 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004392{
4393 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004394 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004395
4396 /* ??? We assume FUTEX_* constants are the same on both host
4397 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004398#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004399 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004400#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004401 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004402#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004403 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004404 case FUTEX_WAIT:
4405 if (timeout) {
4406 pts = &ts;
4407 target_to_host_timespec(pts, timeout);
4408 } else {
4409 pts = NULL;
4410 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004411 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004412 pts, NULL, 0));
4413 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004414 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004415 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004416 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004417 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004418 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004419 case FUTEX_WAKE_OP:
4420 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4421 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4422 But the prototype takes a `struct timespec *'; insert casts
4423 to satisfy the compiler. We do not need to tswap TIMEOUT
4424 since it's not compared to guest memory. */
4425 pts = (struct timespec *)(uintptr_t) timeout;
4426 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4427 g2h(uaddr2),
4428 (base_op == FUTEX_CMP_REQUEUE
4429 ? tswap32(val3)
4430 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004431 default:
4432 return -TARGET_ENOSYS;
4433 }
4434}
4435#endif
4436
pbrook1d9d8b52009-04-16 15:17:02 +00004437/* Map host to target signal numbers for the wait family of syscalls.
4438 Assume all other status bits are the same. */
4439static int host_to_target_waitstatus(int status)
4440{
4441 if (WIFSIGNALED(status)) {
4442 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4443 }
4444 if (WIFSTOPPED(status)) {
4445 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4446 | (status & 0xff);
4447 }
4448 return status;
4449}
4450
pbrooka745ec62008-05-06 15:36:17 +00004451int get_osversion(void)
4452{
4453 static int osversion;
4454 struct new_utsname buf;
4455 const char *s;
4456 int i, n, tmp;
4457 if (osversion)
4458 return osversion;
4459 if (qemu_uname_release && *qemu_uname_release) {
4460 s = qemu_uname_release;
4461 } else {
4462 if (sys_uname(&buf))
4463 return 0;
4464 s = buf.release;
4465 }
4466 tmp = 0;
4467 for (i = 0; i < 3; i++) {
4468 n = 0;
4469 while (*s >= '0' && *s <= '9') {
4470 n *= 10;
4471 n += *s - '0';
4472 s++;
4473 }
4474 tmp = (tmp << 8) + n;
4475 if (*s == '.')
4476 s++;
4477 }
4478 osversion = tmp;
4479 return osversion;
4480}
4481
ths0da46a62007-10-20 20:23:07 +00004482/* do_syscall() should always have a single exit point at the end so
4483 that actions, such as logging of syscall results, can be performed.
4484 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00004485abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4486 abi_long arg2, abi_long arg3, abi_long arg4,
4487 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00004488{
blueswir1992f48a2007-10-14 16:27:31 +00004489 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004490 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00004491 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00004492 void *p;
ths3b46e622007-09-17 08:09:54 +00004493
bellard72f03902003-02-18 23:33:18 +00004494#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00004495 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00004496#endif
thsb92c47c2007-11-01 00:07:38 +00004497 if(do_strace)
4498 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4499
bellard31e31b82003-02-18 22:55:36 +00004500 switch(num) {
4501 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02004502#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00004503 /* In old applications this may be used to implement _exit(2).
4504 However in threaded applictions it is used for thread termination,
4505 and _exit_group is used for application termination.
4506 Do thread termination if we have more then one thread. */
4507 /* FIXME: This probably breaks if a signal arrives. We should probably
4508 be disabling signals. */
4509 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004510 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00004511 CPUState **lastp;
4512 CPUState *p;
4513
4514 cpu_list_lock();
4515 lastp = &first_cpu;
4516 p = first_cpu;
4517 while (p && p != (CPUState *)cpu_env) {
4518 lastp = &p->next_cpu;
4519 p = p->next_cpu;
4520 }
4521 /* If we didn't find the CPU for this thread then something is
4522 horribly wrong. */
4523 if (!p)
4524 abort();
4525 /* Remove the CPU from the list. */
4526 *lastp = p->next_cpu;
4527 cpu_list_unlock();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004528 ts = ((CPUState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00004529 if (ts->child_tidptr) {
4530 put_user_u32(0, ts->child_tidptr);
4531 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4532 NULL, NULL, 0);
4533 }
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004534 thread_env = NULL;
4535 qemu_free(cpu_env);
4536 qemu_free(ts);
pbrookc2764712009-03-07 15:24:59 +00004537 pthread_exit(NULL);
4538 }
4539#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02004540#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00004541 _mcleanup();
4542#endif
bellarde9009672005-04-26 20:42:36 +00004543 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00004544 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00004545 ret = 0; /* avoid warning */
4546 break;
4547 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00004548 if (arg3 == 0)
4549 ret = 0;
4550 else {
4551 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4552 goto efault;
4553 ret = get_errno(read(arg1, p, arg3));
4554 unlock_user(p, arg2, ret);
4555 }
bellard31e31b82003-02-18 22:55:36 +00004556 break;
4557 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00004558 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4559 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004560 ret = get_errno(write(arg1, p, arg3));
4561 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00004562 break;
4563 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00004564 if (!(p = lock_user_string(arg1)))
4565 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004566 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00004567 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4568 arg3));
pbrook53a59602006-03-25 19:31:22 +00004569 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004570 break;
ths82424832007-09-24 09:21:55 +00004571#if defined(TARGET_NR_openat) && defined(__NR_openat)
4572 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00004573 if (!(p = lock_user_string(arg2)))
4574 goto efault;
4575 ret = get_errno(sys_openat(arg1,
4576 path(p),
4577 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4578 arg4));
4579 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00004580 break;
4581#endif
bellard31e31b82003-02-18 22:55:36 +00004582 case TARGET_NR_close:
4583 ret = get_errno(close(arg1));
4584 break;
4585 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00004586 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00004587 break;
4588 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00004589 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00004590 break;
thse5febef2007-04-01 18:31:35 +00004591#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00004592 case TARGET_NR_waitpid:
4593 {
pbrook53a59602006-03-25 19:31:22 +00004594 int status;
4595 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00004596 if (!is_error(ret) && arg2
pbrook1d9d8b52009-04-16 15:17:02 +00004597 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00004598 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004599 }
4600 break;
thse5febef2007-04-01 18:31:35 +00004601#endif
pbrookf0cbb612008-05-30 18:20:05 +00004602#ifdef TARGET_NR_waitid
4603 case TARGET_NR_waitid:
4604 {
4605 siginfo_t info;
4606 info.si_pid = 0;
4607 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4608 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004609 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00004610 goto efault;
4611 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05004612 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00004613 }
4614 }
4615 break;
4616#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004617#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004618 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00004619 if (!(p = lock_user_string(arg1)))
4620 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004621 ret = get_errno(creat(p, arg2));
4622 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004623 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004624#endif
bellard31e31b82003-02-18 22:55:36 +00004625 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00004626 {
4627 void * p2;
4628 p = lock_user_string(arg1);
4629 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004630 if (!p || !p2)
4631 ret = -TARGET_EFAULT;
4632 else
4633 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004634 unlock_user(p2, arg2, 0);
4635 unlock_user(p, arg1, 0);
4636 }
bellard31e31b82003-02-18 22:55:36 +00004637 break;
ths64f0ce42007-09-24 09:25:06 +00004638#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4639 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00004640 {
4641 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00004642 if (!arg2 || !arg4)
4643 goto efault;
ths64f0ce42007-09-24 09:25:06 +00004644 p = lock_user_string(arg2);
4645 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004646 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004647 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00004648 else
4649 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00004650 unlock_user(p, arg2, 0);
4651 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00004652 }
4653 break;
4654#endif
bellard31e31b82003-02-18 22:55:36 +00004655 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00004656 if (!(p = lock_user_string(arg1)))
4657 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004658 ret = get_errno(unlink(p));
4659 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004660 break;
ths8170f562007-09-24 09:24:11 +00004661#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4662 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00004663 if (!(p = lock_user_string(arg2)))
4664 goto efault;
4665 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4666 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00004667 break;
balrogb7d35e62007-12-12 00:40:24 +00004668#endif
bellard31e31b82003-02-18 22:55:36 +00004669 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00004670 {
4671 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00004672 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00004673 abi_ulong gp;
4674 abi_ulong guest_argp;
4675 abi_ulong guest_envp;
4676 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00004677 char **q;
4678
bellardf7341ff2003-03-30 21:00:25 +00004679 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00004680 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00004681 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004682 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004683 goto efault;
ths03aa1972007-12-02 06:28:08 +00004684 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004685 break;
bellard7854b052003-03-29 17:22:23 +00004686 argc++;
bellard2f619692007-11-16 10:46:05 +00004687 }
bellardf7341ff2003-03-30 21:00:25 +00004688 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00004689 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00004690 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004691 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004692 goto efault;
ths03aa1972007-12-02 06:28:08 +00004693 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004694 break;
bellard7854b052003-03-29 17:22:23 +00004695 envc++;
bellard2f619692007-11-16 10:46:05 +00004696 }
bellard7854b052003-03-29 17:22:23 +00004697
bellardf7341ff2003-03-30 21:00:25 +00004698 argp = alloca((argc + 1) * sizeof(void *));
4699 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00004700
pbrookda94d262008-05-30 18:24:00 +00004701 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004702 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004703 if (get_user_ual(addr, gp))
4704 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004705 if (!addr)
4706 break;
bellard2f619692007-11-16 10:46:05 +00004707 if (!(*q = lock_user_string(addr)))
4708 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004709 }
bellardf7341ff2003-03-30 21:00:25 +00004710 *q = NULL;
4711
pbrookda94d262008-05-30 18:24:00 +00004712 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004713 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004714 if (get_user_ual(addr, gp))
4715 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004716 if (!addr)
4717 break;
bellard2f619692007-11-16 10:46:05 +00004718 if (!(*q = lock_user_string(addr)))
4719 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004720 }
bellardf7341ff2003-03-30 21:00:25 +00004721 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00004722
bellard2f619692007-11-16 10:46:05 +00004723 if (!(p = lock_user_string(arg1)))
4724 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004725 ret = get_errno(execve(p, argp, envp));
4726 unlock_user(p, arg1, 0);
4727
bellard2f619692007-11-16 10:46:05 +00004728 goto execve_end;
4729
4730 execve_efault:
4731 ret = -TARGET_EFAULT;
4732
4733 execve_end:
pbrook53a59602006-03-25 19:31:22 +00004734 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004735 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004736 if (get_user_ual(addr, gp)
4737 || !addr)
4738 break;
pbrook53a59602006-03-25 19:31:22 +00004739 unlock_user(*q, addr, 0);
4740 }
4741 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004742 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004743 if (get_user_ual(addr, gp)
4744 || !addr)
4745 break;
pbrook53a59602006-03-25 19:31:22 +00004746 unlock_user(*q, addr, 0);
4747 }
bellard7854b052003-03-29 17:22:23 +00004748 }
bellard31e31b82003-02-18 22:55:36 +00004749 break;
4750 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00004751 if (!(p = lock_user_string(arg1)))
4752 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004753 ret = get_errno(chdir(p));
4754 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004755 break;
bellarda315a142005-01-30 22:59:18 +00004756#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00004757 case TARGET_NR_time:
4758 {
pbrook53a59602006-03-25 19:31:22 +00004759 time_t host_time;
4760 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00004761 if (!is_error(ret)
4762 && arg1
4763 && put_user_sal(host_time, arg1))
4764 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004765 }
4766 break;
bellarda315a142005-01-30 22:59:18 +00004767#endif
bellard31e31b82003-02-18 22:55:36 +00004768 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00004769 if (!(p = lock_user_string(arg1)))
4770 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004771 ret = get_errno(mknod(p, arg2, arg3));
4772 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004773 break;
ths75ac37a2007-09-24 09:23:05 +00004774#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4775 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00004776 if (!(p = lock_user_string(arg2)))
4777 goto efault;
4778 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4779 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00004780 break;
4781#endif
bellard31e31b82003-02-18 22:55:36 +00004782 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00004783 if (!(p = lock_user_string(arg1)))
4784 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004785 ret = get_errno(chmod(p, arg2));
4786 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004787 break;
bellardebc05482003-09-30 21:08:41 +00004788#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00004789 case TARGET_NR_break:
4790 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004791#endif
4792#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00004793 case TARGET_NR_oldstat:
4794 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004795#endif
bellard31e31b82003-02-18 22:55:36 +00004796 case TARGET_NR_lseek:
4797 ret = get_errno(lseek(arg1, arg2, arg3));
4798 break;
Richard Henderson92317332010-05-03 10:07:53 -07004799#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
4800 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00004801 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07004802 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00004803 ret = get_errno(getpid());
4804 break;
Richard Henderson92317332010-05-03 10:07:53 -07004805#endif
4806#ifdef TARGET_NR_getpid
4807 case TARGET_NR_getpid:
4808 ret = get_errno(getpid());
4809 break;
4810#endif
bellard31e31b82003-02-18 22:55:36 +00004811 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00004812 {
4813 /* need to look at the data field */
4814 void *p2, *p3;
4815 p = lock_user_string(arg1);
4816 p2 = lock_user_string(arg2);
4817 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004818 if (!p || !p2 || !p3)
4819 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004820 else {
bellard579a97f2007-11-11 14:26:47 +00004821 /* FIXME - arg5 should be locked, but it isn't clear how to
4822 * do that since it's not guaranteed to be a NULL-terminated
4823 * string.
4824 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004825 if ( ! arg5 )
4826 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4827 else
4828 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4829 }
bellard579a97f2007-11-11 14:26:47 +00004830 unlock_user(p, arg1, 0);
4831 unlock_user(p2, arg2, 0);
4832 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00004833 break;
4834 }
thse5febef2007-04-01 18:31:35 +00004835#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00004836 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00004837 if (!(p = lock_user_string(arg1)))
4838 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004839 ret = get_errno(umount(p));
4840 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004841 break;
thse5febef2007-04-01 18:31:35 +00004842#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004843#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004844 case TARGET_NR_stime:
4845 {
pbrook53a59602006-03-25 19:31:22 +00004846 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00004847 if (get_user_sal(host_time, arg1))
4848 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004849 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00004850 }
4851 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004852#endif
bellard31e31b82003-02-18 22:55:36 +00004853 case TARGET_NR_ptrace:
4854 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00004855#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004856 case TARGET_NR_alarm:
4857 ret = alarm(arg1);
4858 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004859#endif
bellardebc05482003-09-30 21:08:41 +00004860#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00004861 case TARGET_NR_oldfstat:
4862 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004863#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004864#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004865 case TARGET_NR_pause:
4866 ret = get_errno(pause());
4867 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004868#endif
thse5febef2007-04-01 18:31:35 +00004869#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00004870 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00004871 {
pbrook53a59602006-03-25 19:31:22 +00004872 struct utimbuf tbuf, *host_tbuf;
4873 struct target_utimbuf *target_tbuf;
4874 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004875 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4876 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004877 tbuf.actime = tswapl(target_tbuf->actime);
4878 tbuf.modtime = tswapl(target_tbuf->modtime);
4879 unlock_user_struct(target_tbuf, arg2, 0);
4880 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00004881 } else {
pbrook53a59602006-03-25 19:31:22 +00004882 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00004883 }
bellard579a97f2007-11-11 14:26:47 +00004884 if (!(p = lock_user_string(arg1)))
4885 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004886 ret = get_errno(utime(p, host_tbuf));
4887 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00004888 }
4889 break;
thse5febef2007-04-01 18:31:35 +00004890#endif
bellard978a66f2004-12-06 22:58:05 +00004891 case TARGET_NR_utimes:
4892 {
bellard978a66f2004-12-06 22:58:05 +00004893 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00004894 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00004895 if (copy_from_user_timeval(&tv[0], arg2)
4896 || copy_from_user_timeval(&tv[1],
4897 arg2 + sizeof(struct target_timeval)))
4898 goto efault;
bellard978a66f2004-12-06 22:58:05 +00004899 tvp = tv;
4900 } else {
4901 tvp = NULL;
4902 }
bellard579a97f2007-11-11 14:26:47 +00004903 if (!(p = lock_user_string(arg1)))
4904 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004905 ret = get_errno(utimes(p, tvp));
4906 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00004907 }
4908 break;
balrogac8a6552008-09-20 02:25:39 +00004909#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4910 case TARGET_NR_futimesat:
4911 {
4912 struct timeval *tvp, tv[2];
4913 if (arg3) {
4914 if (copy_from_user_timeval(&tv[0], arg3)
4915 || copy_from_user_timeval(&tv[1],
4916 arg3 + sizeof(struct target_timeval)))
4917 goto efault;
4918 tvp = tv;
4919 } else {
4920 tvp = NULL;
4921 }
4922 if (!(p = lock_user_string(arg2)))
4923 goto efault;
4924 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4925 unlock_user(p, arg2, 0);
4926 }
4927 break;
4928#endif
bellardebc05482003-09-30 21:08:41 +00004929#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00004930 case TARGET_NR_stty:
4931 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004932#endif
4933#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00004934 case TARGET_NR_gtty:
4935 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004936#endif
bellard31e31b82003-02-18 22:55:36 +00004937 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00004938 if (!(p = lock_user_string(arg1)))
4939 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02004940 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00004941 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004942 break;
ths92a34c12007-09-24 09:27:49 +00004943#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4944 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00004945 if (!(p = lock_user_string(arg2)))
4946 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00004947 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00004948 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00004949 break;
4950#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004951#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004952 case TARGET_NR_nice:
4953 ret = get_errno(nice(arg1));
4954 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004955#endif
bellardebc05482003-09-30 21:08:41 +00004956#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00004957 case TARGET_NR_ftime:
4958 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004959#endif
bellard31e31b82003-02-18 22:55:36 +00004960 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00004961 sync();
4962 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00004963 break;
4964 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00004965 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00004966 break;
4967 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00004968 {
4969 void *p2;
4970 p = lock_user_string(arg1);
4971 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004972 if (!p || !p2)
4973 ret = -TARGET_EFAULT;
4974 else
4975 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004976 unlock_user(p2, arg2, 0);
4977 unlock_user(p, arg1, 0);
4978 }
bellard31e31b82003-02-18 22:55:36 +00004979 break;
ths722183f2007-09-24 09:24:37 +00004980#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4981 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00004982 {
bellard579a97f2007-11-11 14:26:47 +00004983 void *p2;
ths722183f2007-09-24 09:24:37 +00004984 p = lock_user_string(arg2);
4985 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004986 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004987 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00004988 else
4989 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00004990 unlock_user(p2, arg4, 0);
4991 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00004992 }
4993 break;
4994#endif
bellard31e31b82003-02-18 22:55:36 +00004995 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00004996 if (!(p = lock_user_string(arg1)))
4997 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004998 ret = get_errno(mkdir(p, arg2));
4999 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005000 break;
ths4472ad02007-09-24 09:22:32 +00005001#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
5002 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00005003 if (!(p = lock_user_string(arg2)))
5004 goto efault;
5005 ret = get_errno(sys_mkdirat(arg1, p, arg3));
5006 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00005007 break;
5008#endif
bellard31e31b82003-02-18 22:55:36 +00005009 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00005010 if (!(p = lock_user_string(arg1)))
5011 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005012 ret = get_errno(rmdir(p));
5013 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005014 break;
5015 case TARGET_NR_dup:
5016 ret = get_errno(dup(arg1));
5017 break;
5018 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005019 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00005020 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03005021#ifdef TARGET_NR_pipe2
5022 case TARGET_NR_pipe2:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005023 ret = do_pipe(cpu_env, arg1, arg2, 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03005024 break;
5025#endif
bellard31e31b82003-02-18 22:55:36 +00005026 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00005027 {
pbrook53a59602006-03-25 19:31:22 +00005028 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00005029 struct tms tms;
5030 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00005031 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00005032 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5033 if (!tmsp)
5034 goto efault;
bellardc596ed12003-07-13 17:32:31 +00005035 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
5036 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
5037 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
5038 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00005039 }
bellardc596ed12003-07-13 17:32:31 +00005040 if (!is_error(ret))
5041 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00005042 }
5043 break;
bellardebc05482003-09-30 21:08:41 +00005044#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00005045 case TARGET_NR_prof:
5046 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005047#endif
thse5febef2007-04-01 18:31:35 +00005048#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00005049 case TARGET_NR_signal:
5050 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005051#endif
bellard31e31b82003-02-18 22:55:36 +00005052 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00005053 if (arg1 == 0) {
5054 ret = get_errno(acct(NULL));
5055 } else {
5056 if (!(p = lock_user_string(arg1)))
5057 goto efault;
5058 ret = get_errno(acct(path(p)));
5059 unlock_user(p, arg1, 0);
5060 }
pbrook24836682006-04-16 14:14:53 +00005061 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005062#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005063 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00005064 if (!(p = lock_user_string(arg1)))
5065 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005066 ret = get_errno(umount2(p, arg2));
5067 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005068 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005069#endif
bellardebc05482003-09-30 21:08:41 +00005070#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00005071 case TARGET_NR_lock:
5072 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005073#endif
bellard31e31b82003-02-18 22:55:36 +00005074 case TARGET_NR_ioctl:
5075 ret = do_ioctl(arg1, arg2, arg3);
5076 break;
5077 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00005078 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00005079 break;
bellardebc05482003-09-30 21:08:41 +00005080#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00005081 case TARGET_NR_mpx:
5082 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005083#endif
bellard31e31b82003-02-18 22:55:36 +00005084 case TARGET_NR_setpgid:
5085 ret = get_errno(setpgid(arg1, arg2));
5086 break;
bellardebc05482003-09-30 21:08:41 +00005087#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00005088 case TARGET_NR_ulimit:
5089 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005090#endif
5091#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00005092 case TARGET_NR_oldolduname:
5093 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005094#endif
bellard31e31b82003-02-18 22:55:36 +00005095 case TARGET_NR_umask:
5096 ret = get_errno(umask(arg1));
5097 break;
5098 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00005099 if (!(p = lock_user_string(arg1)))
5100 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005101 ret = get_errno(chroot(p));
5102 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005103 break;
5104 case TARGET_NR_ustat:
5105 goto unimplemented;
5106 case TARGET_NR_dup2:
5107 ret = get_errno(dup2(arg1, arg2));
5108 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03005109#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5110 case TARGET_NR_dup3:
5111 ret = get_errno(dup3(arg1, arg2, arg3));
5112 break;
5113#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005114#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005115 case TARGET_NR_getppid:
5116 ret = get_errno(getppid());
5117 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005118#endif
bellard31e31b82003-02-18 22:55:36 +00005119 case TARGET_NR_getpgrp:
5120 ret = get_errno(getpgrp());
5121 break;
5122 case TARGET_NR_setsid:
5123 ret = get_errno(setsid());
5124 break;
thse5febef2007-04-01 18:31:35 +00005125#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00005126 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00005127 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005128#if defined(TARGET_ALPHA)
5129 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00005130 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00005131 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005132 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5133 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005134 act._sa_handler = old_act->_sa_handler;
5135 target_siginitset(&act.sa_mask, old_act->sa_mask);
5136 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005137 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00005138 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005139 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00005140 }
5141 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00005142 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005143 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5144 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005145 old_act->_sa_handler = oact._sa_handler;
5146 old_act->sa_mask = oact.sa_mask.sig[0];
5147 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00005148 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00005149 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005150#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00005151 struct target_sigaction act, oact, *pact, *old_act;
5152
5153 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005154 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5155 goto efault;
bellard106ec872006-06-27 21:08:10 +00005156 act._sa_handler = old_act->_sa_handler;
5157 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5158 act.sa_flags = old_act->sa_flags;
5159 unlock_user_struct(old_act, arg2, 0);
5160 pact = &act;
5161 } else {
5162 pact = NULL;
5163 }
5164
5165 ret = get_errno(do_sigaction(arg1, pact, &oact));
5166
5167 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005168 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5169 goto efault;
bellard106ec872006-06-27 21:08:10 +00005170 old_act->_sa_handler = oact._sa_handler;
5171 old_act->sa_flags = oact.sa_flags;
5172 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5173 old_act->sa_mask.sig[1] = 0;
5174 old_act->sa_mask.sig[2] = 0;
5175 old_act->sa_mask.sig[3] = 0;
5176 unlock_user_struct(old_act, arg3, 1);
5177 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005178#else
5179 struct target_old_sigaction *old_act;
5180 struct target_sigaction act, oact, *pact;
5181 if (arg2) {
5182 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5183 goto efault;
5184 act._sa_handler = old_act->_sa_handler;
5185 target_siginitset(&act.sa_mask, old_act->sa_mask);
5186 act.sa_flags = old_act->sa_flags;
5187 act.sa_restorer = old_act->sa_restorer;
5188 unlock_user_struct(old_act, arg2, 0);
5189 pact = &act;
5190 } else {
5191 pact = NULL;
5192 }
5193 ret = get_errno(do_sigaction(arg1, pact, &oact));
5194 if (!is_error(ret) && arg3) {
5195 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5196 goto efault;
5197 old_act->_sa_handler = oact._sa_handler;
5198 old_act->sa_mask = oact.sa_mask.sig[0];
5199 old_act->sa_flags = oact.sa_flags;
5200 old_act->sa_restorer = oact.sa_restorer;
5201 unlock_user_struct(old_act, arg3, 1);
5202 }
ths388bb212007-05-13 13:58:00 +00005203#endif
bellard31e31b82003-02-18 22:55:36 +00005204 }
5205 break;
thse5febef2007-04-01 18:31:35 +00005206#endif
bellard66fb9762003-03-23 01:06:05 +00005207 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00005208 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005209#if defined(TARGET_ALPHA)
5210 struct target_sigaction act, oact, *pact = 0;
5211 struct target_rt_sigaction *rt_act;
5212 /* ??? arg4 == sizeof(sigset_t). */
5213 if (arg2) {
5214 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5215 goto efault;
5216 act._sa_handler = rt_act->_sa_handler;
5217 act.sa_mask = rt_act->sa_mask;
5218 act.sa_flags = rt_act->sa_flags;
5219 act.sa_restorer = arg5;
5220 unlock_user_struct(rt_act, arg2, 0);
5221 pact = &act;
5222 }
5223 ret = get_errno(do_sigaction(arg1, pact, &oact));
5224 if (!is_error(ret) && arg3) {
5225 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5226 goto efault;
5227 rt_act->_sa_handler = oact._sa_handler;
5228 rt_act->sa_mask = oact.sa_mask;
5229 rt_act->sa_flags = oact.sa_flags;
5230 unlock_user_struct(rt_act, arg3, 1);
5231 }
5232#else
pbrook53a59602006-03-25 19:31:22 +00005233 struct target_sigaction *act;
5234 struct target_sigaction *oact;
5235
bellard579a97f2007-11-11 14:26:47 +00005236 if (arg2) {
5237 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5238 goto efault;
5239 } else
pbrook53a59602006-03-25 19:31:22 +00005240 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005241 if (arg3) {
5242 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5243 ret = -TARGET_EFAULT;
5244 goto rt_sigaction_fail;
5245 }
5246 } else
pbrook53a59602006-03-25 19:31:22 +00005247 oact = NULL;
5248 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005249 rt_sigaction_fail:
5250 if (act)
pbrook53a59602006-03-25 19:31:22 +00005251 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005252 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005253 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005254#endif
pbrook53a59602006-03-25 19:31:22 +00005255 }
bellard66fb9762003-03-23 01:06:05 +00005256 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005257#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005258 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005259 {
5260 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005261 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005262 sigprocmask(0, NULL, &cur_set);
5263 host_to_target_old_sigset(&target_set, &cur_set);
5264 ret = target_set;
5265 }
5266 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005267#endif
5268#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005269 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005270 {
5271 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005272 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00005273 sigprocmask(0, NULL, &cur_set);
5274 target_to_host_old_sigset(&set, &target_set);
5275 sigorset(&set, &set, &cur_set);
5276 sigprocmask(SIG_SETMASK, &set, &oset);
5277 host_to_target_old_sigset(&target_set, &oset);
5278 ret = target_set;
5279 }
5280 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005281#endif
thse5febef2007-04-01 18:31:35 +00005282#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00005283 case TARGET_NR_sigprocmask:
5284 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005285#if defined(TARGET_ALPHA)
5286 sigset_t set, oldset;
5287 abi_ulong mask;
5288 int how;
5289
5290 switch (arg1) {
5291 case TARGET_SIG_BLOCK:
5292 how = SIG_BLOCK;
5293 break;
5294 case TARGET_SIG_UNBLOCK:
5295 how = SIG_UNBLOCK;
5296 break;
5297 case TARGET_SIG_SETMASK:
5298 how = SIG_SETMASK;
5299 break;
5300 default:
5301 ret = -TARGET_EINVAL;
5302 goto fail;
5303 }
5304 mask = arg2;
5305 target_to_host_old_sigset(&set, &mask);
5306
5307 ret = get_errno(sigprocmask(how, &set, &oldset));
5308
5309 if (!is_error(ret)) {
5310 host_to_target_old_sigset(&mask, &oldset);
5311 ret = mask;
5312 ((CPUAlphaState *)cpu_env)->[IR_V0] = 0; /* force no error */
5313 }
5314#else
bellard66fb9762003-03-23 01:06:05 +00005315 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07005316 int how;
ths3b46e622007-09-17 08:09:54 +00005317
pbrook53a59602006-03-25 19:31:22 +00005318 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005319 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00005320 case TARGET_SIG_BLOCK:
5321 how = SIG_BLOCK;
5322 break;
5323 case TARGET_SIG_UNBLOCK:
5324 how = SIG_UNBLOCK;
5325 break;
5326 case TARGET_SIG_SETMASK:
5327 how = SIG_SETMASK;
5328 break;
5329 default:
ths0da46a62007-10-20 20:23:07 +00005330 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005331 goto fail;
5332 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005333 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005334 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005335 target_to_host_old_sigset(&set, p);
5336 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005337 set_ptr = &set;
5338 } else {
5339 how = 0;
5340 set_ptr = NULL;
5341 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005342 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005343 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005344 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005345 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005346 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005347 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005348 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005349#endif
bellard66fb9762003-03-23 01:06:05 +00005350 }
5351 break;
thse5febef2007-04-01 18:31:35 +00005352#endif
bellard66fb9762003-03-23 01:06:05 +00005353 case TARGET_NR_rt_sigprocmask:
5354 {
5355 int how = arg1;
5356 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00005357
pbrook53a59602006-03-25 19:31:22 +00005358 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005359 switch(how) {
5360 case TARGET_SIG_BLOCK:
5361 how = SIG_BLOCK;
5362 break;
5363 case TARGET_SIG_UNBLOCK:
5364 how = SIG_UNBLOCK;
5365 break;
5366 case TARGET_SIG_SETMASK:
5367 how = SIG_SETMASK;
5368 break;
5369 default:
ths0da46a62007-10-20 20:23:07 +00005370 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005371 goto fail;
5372 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005373 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005374 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005375 target_to_host_sigset(&set, p);
5376 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005377 set_ptr = &set;
5378 } else {
5379 how = 0;
5380 set_ptr = NULL;
5381 }
5382 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005383 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005384 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005385 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005386 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005387 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005388 }
5389 }
5390 break;
thse5febef2007-04-01 18:31:35 +00005391#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005392 case TARGET_NR_sigpending:
5393 {
5394 sigset_t set;
5395 ret = get_errno(sigpending(&set));
5396 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005397 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005398 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005399 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005400 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005401 }
5402 }
5403 break;
thse5febef2007-04-01 18:31:35 +00005404#endif
bellard66fb9762003-03-23 01:06:05 +00005405 case TARGET_NR_rt_sigpending:
5406 {
5407 sigset_t set;
5408 ret = get_errno(sigpending(&set));
5409 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005410 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005411 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005412 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005413 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005414 }
5415 }
5416 break;
thse5febef2007-04-01 18:31:35 +00005417#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00005418 case TARGET_NR_sigsuspend:
5419 {
5420 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07005421#if defined(TARGET_ALPHA)
5422 abi_ulong mask = arg1;
5423 target_to_host_old_sigset(&set, &mask);
5424#else
Anthony Liguoric227f092009-10-01 16:12:16 -05005425 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005426 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005427 target_to_host_old_sigset(&set, p);
5428 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07005429#endif
bellard66fb9762003-03-23 01:06:05 +00005430 ret = get_errno(sigsuspend(&set));
5431 }
5432 break;
thse5febef2007-04-01 18:31:35 +00005433#endif
bellard66fb9762003-03-23 01:06:05 +00005434 case TARGET_NR_rt_sigsuspend:
5435 {
5436 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005437 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005438 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005439 target_to_host_sigset(&set, p);
5440 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005441 ret = get_errno(sigsuspend(&set));
5442 }
5443 break;
5444 case TARGET_NR_rt_sigtimedwait:
5445 {
bellard66fb9762003-03-23 01:06:05 +00005446 sigset_t set;
5447 struct timespec uts, *puts;
5448 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00005449
Anthony Liguoric227f092009-10-01 16:12:16 -05005450 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005451 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005452 target_to_host_sigset(&set, p);
5453 unlock_user(p, arg1, 0);
5454 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00005455 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00005456 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00005457 } else {
5458 puts = NULL;
5459 }
5460 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00005461 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005462 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005463 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005464 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05005465 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00005466 }
5467 }
5468 break;
5469 case TARGET_NR_rt_sigqueueinfo:
5470 {
5471 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05005472 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005473 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005474 target_to_host_siginfo(&uinfo, p);
5475 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005476 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5477 }
5478 break;
thse5febef2007-04-01 18:31:35 +00005479#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00005480 case TARGET_NR_sigreturn:
5481 /* NOTE: ret is eax, so not transcoding must be done */
5482 ret = do_sigreturn(cpu_env);
5483 break;
thse5febef2007-04-01 18:31:35 +00005484#endif
bellard66fb9762003-03-23 01:06:05 +00005485 case TARGET_NR_rt_sigreturn:
5486 /* NOTE: ret is eax, so not transcoding must be done */
5487 ret = do_rt_sigreturn(cpu_env);
5488 break;
bellard31e31b82003-02-18 22:55:36 +00005489 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00005490 if (!(p = lock_user_string(arg1)))
5491 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005492 ret = get_errno(sethostname(p, arg2));
5493 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005494 break;
5495 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00005496 {
bellard9de5e442003-03-23 16:49:39 +00005497 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005498 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005499 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00005500 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5501 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005502 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5503 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005504 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00005505 ret = get_errno(setrlimit(resource, &rlim));
5506 }
5507 break;
bellard31e31b82003-02-18 22:55:36 +00005508 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00005509 {
bellard9de5e442003-03-23 16:49:39 +00005510 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005511 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005512 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00005513
bellard9de5e442003-03-23 16:49:39 +00005514 ret = get_errno(getrlimit(resource, &rlim));
5515 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005516 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5517 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005518 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5519 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005520 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00005521 }
5522 }
5523 break;
bellard31e31b82003-02-18 22:55:36 +00005524 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00005525 {
5526 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00005527 ret = get_errno(getrusage(arg1, &rusage));
5528 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005529 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00005530 }
5531 }
5532 break;
bellard31e31b82003-02-18 22:55:36 +00005533 case TARGET_NR_gettimeofday:
5534 {
bellard31e31b82003-02-18 22:55:36 +00005535 struct timeval tv;
5536 ret = get_errno(gettimeofday(&tv, NULL));
5537 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00005538 if (copy_to_user_timeval(arg1, &tv))
5539 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005540 }
5541 }
5542 break;
5543 case TARGET_NR_settimeofday:
5544 {
bellard31e31b82003-02-18 22:55:36 +00005545 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00005546 if (copy_from_user_timeval(&tv, arg1))
5547 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005548 ret = get_errno(settimeofday(&tv, NULL));
5549 }
5550 break;
bellard048f6b42005-11-26 18:47:20 +00005551#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00005552 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00005553 {
pbrook53a59602006-03-25 19:31:22 +00005554 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00005555 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00005556 long nsel;
5557
bellard579a97f2007-11-11 14:26:47 +00005558 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5559 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005560 nsel = tswapl(sel->n);
5561 inp = tswapl(sel->inp);
5562 outp = tswapl(sel->outp);
5563 exp = tswapl(sel->exp);
5564 tvp = tswapl(sel->tvp);
5565 unlock_user_struct(sel, arg1, 0);
5566 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00005567 }
5568 break;
bellard048f6b42005-11-26 18:47:20 +00005569#endif
Riku Voipio9e423822010-05-07 12:28:05 +00005570#ifdef TARGET_NR_pselect6
5571 case TARGET_NR_pselect6:
5572 goto unimplemented_nowarn;
5573#endif
bellard31e31b82003-02-18 22:55:36 +00005574 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00005575 {
5576 void *p2;
5577 p = lock_user_string(arg1);
5578 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005579 if (!p || !p2)
5580 ret = -TARGET_EFAULT;
5581 else
5582 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005583 unlock_user(p2, arg2, 0);
5584 unlock_user(p, arg1, 0);
5585 }
bellard31e31b82003-02-18 22:55:36 +00005586 break;
thsf0b62432007-09-24 09:25:40 +00005587#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5588 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00005589 {
bellard579a97f2007-11-11 14:26:47 +00005590 void *p2;
thsf0b62432007-09-24 09:25:40 +00005591 p = lock_user_string(arg1);
5592 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005593 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005594 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00005595 else
5596 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00005597 unlock_user(p2, arg3, 0);
5598 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00005599 }
5600 break;
5601#endif
bellardebc05482003-09-30 21:08:41 +00005602#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00005603 case TARGET_NR_oldlstat:
5604 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005605#endif
bellard31e31b82003-02-18 22:55:36 +00005606 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00005607 {
aurel32d088d662009-01-30 20:09:01 +00005608 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00005609 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00005610 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5611 if (!p || !p2)
5612 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00005613 else {
5614 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5615 char real[PATH_MAX];
5616 temp = realpath(exec_path,real);
5617 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5618 snprintf((char *)p2, arg3, "%s", real);
5619 }
5620 else
5621 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00005622 }
pbrook53a59602006-03-25 19:31:22 +00005623 unlock_user(p2, arg2, ret);
5624 unlock_user(p, arg1, 0);
5625 }
bellard31e31b82003-02-18 22:55:36 +00005626 break;
ths5e0ccb12007-09-24 09:26:10 +00005627#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5628 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00005629 {
bellard579a97f2007-11-11 14:26:47 +00005630 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00005631 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005632 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5633 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005634 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00005635 else
5636 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00005637 unlock_user(p2, arg3, ret);
5638 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00005639 }
5640 break;
5641#endif
thse5febef2007-04-01 18:31:35 +00005642#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00005643 case TARGET_NR_uselib:
5644 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005645#endif
5646#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00005647 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00005648 if (!(p = lock_user_string(arg1)))
5649 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005650 ret = get_errno(swapon(p, arg2));
5651 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005652 break;
thse5febef2007-04-01 18:31:35 +00005653#endif
bellard31e31b82003-02-18 22:55:36 +00005654 case TARGET_NR_reboot:
5655 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005656#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00005657 case TARGET_NR_readdir:
5658 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005659#endif
5660#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00005661 case TARGET_NR_mmap:
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02005662#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 +00005663 {
blueswir1992f48a2007-10-14 16:27:31 +00005664 abi_ulong *v;
5665 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00005666 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5667 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005668 v1 = tswapl(v[0]);
5669 v2 = tswapl(v[1]);
5670 v3 = tswapl(v[2]);
5671 v4 = tswapl(v[3]);
5672 v5 = tswapl(v[4]);
5673 v6 = tswapl(v[5]);
5674 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00005675 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00005676 target_to_host_bitmask(v4, mmap_flags_tbl),
5677 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00005678 }
bellard31e31b82003-02-18 22:55:36 +00005679#else
ths5fafdf22007-09-16 21:08:06 +00005680 ret = get_errno(target_mmap(arg1, arg2, arg3,
5681 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00005682 arg5,
5683 arg6));
bellard31e31b82003-02-18 22:55:36 +00005684#endif
bellard6fb883e2003-07-09 17:12:39 +00005685 break;
thse5febef2007-04-01 18:31:35 +00005686#endif
bellarda315a142005-01-30 22:59:18 +00005687#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00005688 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00005689#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00005690#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00005691#endif
ths5fafdf22007-09-16 21:08:06 +00005692 ret = get_errno(target_mmap(arg1, arg2, arg3,
5693 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00005694 arg5,
bellardc573ff62004-01-04 15:51:36 +00005695 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00005696 break;
bellarda315a142005-01-30 22:59:18 +00005697#endif
bellard31e31b82003-02-18 22:55:36 +00005698 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00005699 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005700 break;
bellard9de5e442003-03-23 16:49:39 +00005701 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01005702 {
5703 TaskState *ts = ((CPUState *)cpu_env)->opaque;
5704 /* Special hack to detect libc making the stack executable. */
5705 if ((arg3 & PROT_GROWSDOWN)
5706 && arg1 >= ts->info->stack_limit
5707 && arg1 <= ts->info->start_stack) {
5708 arg3 &= ~PROT_GROWSDOWN;
5709 arg2 = arg2 + arg1 - ts->info->stack_limit;
5710 arg1 = ts->info->stack_limit;
5711 }
5712 }
bellard54936002003-05-13 00:25:15 +00005713 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005714 break;
thse5febef2007-04-01 18:31:35 +00005715#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00005716 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00005717 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00005718 break;
thse5febef2007-04-01 18:31:35 +00005719#endif
pbrook53a59602006-03-25 19:31:22 +00005720 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00005721#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00005722 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00005723 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005724 break;
thse5febef2007-04-01 18:31:35 +00005725#endif
5726#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00005727 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00005728 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005729 break;
thse5febef2007-04-01 18:31:35 +00005730#endif
5731#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00005732 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00005733 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005734 break;
thse5febef2007-04-01 18:31:35 +00005735#endif
5736#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00005737 case TARGET_NR_mlockall:
5738 ret = get_errno(mlockall(arg1));
5739 break;
thse5febef2007-04-01 18:31:35 +00005740#endif
5741#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00005742 case TARGET_NR_munlockall:
5743 ret = get_errno(munlockall());
5744 break;
thse5febef2007-04-01 18:31:35 +00005745#endif
bellard31e31b82003-02-18 22:55:36 +00005746 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00005747 if (!(p = lock_user_string(arg1)))
5748 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005749 ret = get_errno(truncate(p, arg2));
5750 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005751 break;
5752 case TARGET_NR_ftruncate:
5753 ret = get_errno(ftruncate(arg1, arg2));
5754 break;
5755 case TARGET_NR_fchmod:
5756 ret = get_errno(fchmod(arg1, arg2));
5757 break;
ths814d7972007-09-24 09:26:51 +00005758#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5759 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00005760 if (!(p = lock_user_string(arg2)))
5761 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005762 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005763 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00005764 break;
5765#endif
bellard31e31b82003-02-18 22:55:36 +00005766 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00005767 /* libc does special remapping of the return value of
5768 * sys_getpriority() so it's just easiest to call
5769 * sys_getpriority() directly rather than through libc. */
Ulrich Hecht69137202009-09-17 21:08:37 +03005770 ret = get_errno(sys_getpriority(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005771 break;
5772 case TARGET_NR_setpriority:
5773 ret = get_errno(setpriority(arg1, arg2, arg3));
5774 break;
bellardebc05482003-09-30 21:08:41 +00005775#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00005776 case TARGET_NR_profil:
5777 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005778#endif
bellard31e31b82003-02-18 22:55:36 +00005779 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00005780 if (!(p = lock_user_string(arg1)))
5781 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005782 ret = get_errno(statfs(path(p), &stfs));
5783 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005784 convert_statfs:
5785 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005786 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005787
bellard579a97f2007-11-11 14:26:47 +00005788 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5789 goto efault;
5790 __put_user(stfs.f_type, &target_stfs->f_type);
5791 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5792 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5793 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5794 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5795 __put_user(stfs.f_files, &target_stfs->f_files);
5796 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5797 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5798 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5799 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00005800 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005801 }
5802 break;
5803 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00005804 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00005805 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00005806#ifdef TARGET_NR_statfs64
5807 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00005808 if (!(p = lock_user_string(arg1)))
5809 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005810 ret = get_errno(statfs(path(p), &stfs));
5811 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00005812 convert_statfs64:
5813 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005814 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005815
bellard579a97f2007-11-11 14:26:47 +00005816 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5817 goto efault;
5818 __put_user(stfs.f_type, &target_stfs->f_type);
5819 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5820 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5821 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5822 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5823 __put_user(stfs.f_files, &target_stfs->f_files);
5824 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5825 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5826 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5827 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5828 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00005829 }
5830 break;
5831 case TARGET_NR_fstatfs64:
5832 ret = get_errno(fstatfs(arg1, &stfs));
5833 goto convert_statfs64;
5834#endif
bellardebc05482003-09-30 21:08:41 +00005835#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00005836 case TARGET_NR_ioperm:
5837 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005838#endif
thse5febef2007-04-01 18:31:35 +00005839#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00005840 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00005841 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00005842 break;
thse5febef2007-04-01 18:31:35 +00005843#endif
bellard3532fa72006-06-24 15:06:03 +00005844#ifdef TARGET_NR_accept
5845 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00005846 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005847 break;
5848#endif
5849#ifdef TARGET_NR_bind
5850 case TARGET_NR_bind:
5851 ret = do_bind(arg1, arg2, arg3);
5852 break;
5853#endif
5854#ifdef TARGET_NR_connect
5855 case TARGET_NR_connect:
5856 ret = do_connect(arg1, arg2, arg3);
5857 break;
5858#endif
5859#ifdef TARGET_NR_getpeername
5860 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00005861 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005862 break;
5863#endif
5864#ifdef TARGET_NR_getsockname
5865 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00005866 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005867 break;
5868#endif
5869#ifdef TARGET_NR_getsockopt
5870 case TARGET_NR_getsockopt:
5871 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5872 break;
5873#endif
5874#ifdef TARGET_NR_listen
5875 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00005876 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005877 break;
5878#endif
5879#ifdef TARGET_NR_recv
5880 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00005881 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005882 break;
5883#endif
5884#ifdef TARGET_NR_recvfrom
5885 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00005886 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005887 break;
5888#endif
5889#ifdef TARGET_NR_recvmsg
5890 case TARGET_NR_recvmsg:
5891 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5892 break;
5893#endif
5894#ifdef TARGET_NR_send
5895 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00005896 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005897 break;
5898#endif
5899#ifdef TARGET_NR_sendmsg
5900 case TARGET_NR_sendmsg:
5901 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5902 break;
5903#endif
5904#ifdef TARGET_NR_sendto
5905 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00005906 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005907 break;
5908#endif
5909#ifdef TARGET_NR_shutdown
5910 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00005911 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005912 break;
5913#endif
5914#ifdef TARGET_NR_socket
5915 case TARGET_NR_socket:
5916 ret = do_socket(arg1, arg2, arg3);
5917 break;
5918#endif
5919#ifdef TARGET_NR_socketpair
5920 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00005921 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00005922 break;
5923#endif
5924#ifdef TARGET_NR_setsockopt
5925 case TARGET_NR_setsockopt:
5926 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5927 break;
5928#endif
ths7494b0f2007-02-11 18:26:53 +00005929
bellard31e31b82003-02-18 22:55:36 +00005930 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00005931 if (!(p = lock_user_string(arg2)))
5932 goto efault;
thse5574482007-02-11 20:03:13 +00005933 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5934 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00005935 break;
5936
bellard31e31b82003-02-18 22:55:36 +00005937 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00005938 {
bellard66fb9762003-03-23 01:06:05 +00005939 struct itimerval value, ovalue, *pvalue;
5940
pbrook53a59602006-03-25 19:31:22 +00005941 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005942 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00005943 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5944 || copy_from_user_timeval(&pvalue->it_value,
5945 arg2 + sizeof(struct target_timeval)))
5946 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005947 } else {
5948 pvalue = NULL;
5949 }
5950 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00005951 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00005952 if (copy_to_user_timeval(arg3,
5953 &ovalue.it_interval)
5954 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5955 &ovalue.it_value))
5956 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005957 }
5958 }
5959 break;
bellard31e31b82003-02-18 22:55:36 +00005960 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00005961 {
bellard66fb9762003-03-23 01:06:05 +00005962 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00005963
bellard66fb9762003-03-23 01:06:05 +00005964 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00005965 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00005966 if (copy_to_user_timeval(arg2,
5967 &value.it_interval)
5968 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5969 &value.it_value))
5970 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005971 }
5972 }
5973 break;
bellard31e31b82003-02-18 22:55:36 +00005974 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00005975 if (!(p = lock_user_string(arg1)))
5976 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005977 ret = get_errno(stat(path(p), &st));
5978 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005979 goto do_stat;
5980 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00005981 if (!(p = lock_user_string(arg1)))
5982 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005983 ret = get_errno(lstat(path(p), &st));
5984 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005985 goto do_stat;
5986 case TARGET_NR_fstat:
5987 {
5988 ret = get_errno(fstat(arg1, &st));
5989 do_stat:
5990 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005991 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00005992
bellard579a97f2007-11-11 14:26:47 +00005993 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5994 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02005995 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00005996 __put_user(st.st_dev, &target_st->st_dev);
5997 __put_user(st.st_ino, &target_st->st_ino);
5998 __put_user(st.st_mode, &target_st->st_mode);
5999 __put_user(st.st_uid, &target_st->st_uid);
6000 __put_user(st.st_gid, &target_st->st_gid);
6001 __put_user(st.st_nlink, &target_st->st_nlink);
6002 __put_user(st.st_rdev, &target_st->st_rdev);
6003 __put_user(st.st_size, &target_st->st_size);
6004 __put_user(st.st_blksize, &target_st->st_blksize);
6005 __put_user(st.st_blocks, &target_st->st_blocks);
6006 __put_user(st.st_atime, &target_st->target_st_atime);
6007 __put_user(st.st_mtime, &target_st->target_st_mtime);
6008 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00006009 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006010 }
6011 }
6012 break;
bellardebc05482003-09-30 21:08:41 +00006013#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00006014 case TARGET_NR_olduname:
6015 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006016#endif
6017#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00006018 case TARGET_NR_iopl:
6019 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006020#endif
bellard31e31b82003-02-18 22:55:36 +00006021 case TARGET_NR_vhangup:
6022 ret = get_errno(vhangup());
6023 break;
bellardebc05482003-09-30 21:08:41 +00006024#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00006025 case TARGET_NR_idle:
6026 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006027#endif
bellard42ad6ae2005-01-03 22:48:11 +00006028#ifdef TARGET_NR_syscall
6029 case TARGET_NR_syscall:
6030 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
6031 break;
6032#endif
bellard31e31b82003-02-18 22:55:36 +00006033 case TARGET_NR_wait4:
6034 {
6035 int status;
blueswir1992f48a2007-10-14 16:27:31 +00006036 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00006037 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00006038 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00006039 if (target_rusage)
6040 rusage_ptr = &rusage;
6041 else
6042 rusage_ptr = NULL;
6043 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6044 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006045 if (status_ptr) {
pbrook1d9d8b52009-04-16 15:17:02 +00006046 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00006047 if (put_user_s32(status, status_ptr))
6048 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006049 }
bellard2f619692007-11-16 10:46:05 +00006050 if (target_rusage)
6051 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00006052 }
6053 }
6054 break;
thse5febef2007-04-01 18:31:35 +00006055#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00006056 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00006057 if (!(p = lock_user_string(arg1)))
6058 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006059 ret = get_errno(swapoff(p));
6060 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006061 break;
thse5febef2007-04-01 18:31:35 +00006062#endif
bellard31e31b82003-02-18 22:55:36 +00006063 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00006064 {
pbrook53a59602006-03-25 19:31:22 +00006065 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00006066 struct sysinfo value;
6067 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00006068 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00006069 {
bellard579a97f2007-11-11 14:26:47 +00006070 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6071 goto efault;
bellarda5448a72004-06-19 16:59:03 +00006072 __put_user(value.uptime, &target_value->uptime);
6073 __put_user(value.loads[0], &target_value->loads[0]);
6074 __put_user(value.loads[1], &target_value->loads[1]);
6075 __put_user(value.loads[2], &target_value->loads[2]);
6076 __put_user(value.totalram, &target_value->totalram);
6077 __put_user(value.freeram, &target_value->freeram);
6078 __put_user(value.sharedram, &target_value->sharedram);
6079 __put_user(value.bufferram, &target_value->bufferram);
6080 __put_user(value.totalswap, &target_value->totalswap);
6081 __put_user(value.freeswap, &target_value->freeswap);
6082 __put_user(value.procs, &target_value->procs);
6083 __put_user(value.totalhigh, &target_value->totalhigh);
6084 __put_user(value.freehigh, &target_value->freehigh);
6085 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00006086 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00006087 }
6088 }
6089 break;
thse5febef2007-04-01 18:31:35 +00006090#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00006091 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00006092 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6093 break;
thse5febef2007-04-01 18:31:35 +00006094#endif
aurel32e5289082009-04-18 16:16:12 +00006095#ifdef TARGET_NR_semget
6096 case TARGET_NR_semget:
6097 ret = get_errno(semget(arg1, arg2, arg3));
6098 break;
6099#endif
6100#ifdef TARGET_NR_semop
6101 case TARGET_NR_semop:
6102 ret = get_errno(do_semop(arg1, arg2, arg3));
6103 break;
6104#endif
6105#ifdef TARGET_NR_semctl
6106 case TARGET_NR_semctl:
6107 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6108 break;
6109#endif
aurel32eeb438c2008-10-13 21:08:55 +00006110#ifdef TARGET_NR_msgctl
6111 case TARGET_NR_msgctl:
6112 ret = do_msgctl(arg1, arg2, arg3);
6113 break;
6114#endif
6115#ifdef TARGET_NR_msgget
6116 case TARGET_NR_msgget:
6117 ret = get_errno(msgget(arg1, arg2));
6118 break;
6119#endif
6120#ifdef TARGET_NR_msgrcv
6121 case TARGET_NR_msgrcv:
6122 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
6123 break;
6124#endif
6125#ifdef TARGET_NR_msgsnd
6126 case TARGET_NR_msgsnd:
6127 ret = do_msgsnd(arg1, arg2, arg3, arg4);
6128 break;
6129#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03006130#ifdef TARGET_NR_shmget
6131 case TARGET_NR_shmget:
6132 ret = get_errno(shmget(arg1, arg2, arg3));
6133 break;
6134#endif
6135#ifdef TARGET_NR_shmctl
6136 case TARGET_NR_shmctl:
6137 ret = do_shmctl(arg1, arg2, arg3);
6138 break;
6139#endif
6140#ifdef TARGET_NR_shmat
6141 case TARGET_NR_shmat:
6142 ret = do_shmat(arg1, arg2, arg3);
6143 break;
6144#endif
6145#ifdef TARGET_NR_shmdt
6146 case TARGET_NR_shmdt:
6147 ret = do_shmdt(arg1);
6148 break;
6149#endif
bellard31e31b82003-02-18 22:55:36 +00006150 case TARGET_NR_fsync:
6151 ret = get_errno(fsync(arg1));
6152 break;
bellard31e31b82003-02-18 22:55:36 +00006153 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07006154#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00006155 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00006156#elif defined(TARGET_CRIS)
6157 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006158#else
pbrookd865bab2008-06-07 22:12:17 +00006159 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006160#endif
bellard1b6b0292003-03-22 17:31:38 +00006161 break;
bellardec86b0f2003-04-11 00:15:04 +00006162#ifdef __NR_exit_group
6163 /* new thread calls */
6164 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02006165#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00006166 _mcleanup();
6167#endif
bellarde9009672005-04-26 20:42:36 +00006168 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00006169 ret = get_errno(exit_group(arg1));
6170 break;
6171#endif
bellard31e31b82003-02-18 22:55:36 +00006172 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00006173 if (!(p = lock_user_string(arg1)))
6174 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006175 ret = get_errno(setdomainname(p, arg2));
6176 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006177 break;
6178 case TARGET_NR_uname:
6179 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00006180 {
6181 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00006182
bellard579a97f2007-11-11 14:26:47 +00006183 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
6184 goto efault;
bellard29e619b2004-09-13 21:41:04 +00006185 ret = get_errno(sys_uname(buf));
6186 if (!is_error(ret)) {
6187 /* Overrite the native machine name with whatever is being
6188 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01006189 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00006190 /* Allow the user to override the reported release. */
6191 if (qemu_uname_release && *qemu_uname_release)
6192 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00006193 }
pbrook53a59602006-03-25 19:31:22 +00006194 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00006195 }
bellard31e31b82003-02-18 22:55:36 +00006196 break;
bellard6dbad632003-03-16 18:05:05 +00006197#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00006198 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00006199 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00006200 break;
j_mayer84409dd2007-04-06 08:56:50 +00006201#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00006202 case TARGET_NR_vm86old:
6203 goto unimplemented;
6204 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00006205 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00006206 break;
6207#endif
j_mayer84409dd2007-04-06 08:56:50 +00006208#endif
bellard31e31b82003-02-18 22:55:36 +00006209 case TARGET_NR_adjtimex:
6210 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006211#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00006212 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00006213#endif
bellard31e31b82003-02-18 22:55:36 +00006214 case TARGET_NR_init_module:
6215 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00006216#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00006217 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00006218#endif
bellard31e31b82003-02-18 22:55:36 +00006219 goto unimplemented;
6220 case TARGET_NR_quotactl:
6221 goto unimplemented;
6222 case TARGET_NR_getpgid:
6223 ret = get_errno(getpgid(arg1));
6224 break;
6225 case TARGET_NR_fchdir:
6226 ret = get_errno(fchdir(arg1));
6227 break;
j_mayer84409dd2007-04-06 08:56:50 +00006228#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00006229 case TARGET_NR_bdflush:
6230 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00006231#endif
thse5febef2007-04-01 18:31:35 +00006232#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00006233 case TARGET_NR_sysfs:
6234 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006235#endif
bellard31e31b82003-02-18 22:55:36 +00006236 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00006237 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00006238 break;
thse5febef2007-04-01 18:31:35 +00006239#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00006240 case TARGET_NR_afs_syscall:
6241 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006242#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006243#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006244 case TARGET_NR__llseek:
6245 {
6246 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00006247#if !defined(__NR_llseek)
6248 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
6249 if (res == -1) {
6250 ret = get_errno(res);
6251 } else {
6252 ret = 0;
6253 }
6254#else
bellard31e31b82003-02-18 22:55:36 +00006255 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00006256#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00006257 if ((ret == 0) && put_user_s64(res, arg4)) {
6258 goto efault;
6259 }
bellard31e31b82003-02-18 22:55:36 +00006260 }
6261 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006262#endif
bellard31e31b82003-02-18 22:55:36 +00006263 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02006264#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00006265 {
pbrook53a59602006-03-25 19:31:22 +00006266 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00006267 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006268 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00006269
6270 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00006271 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00006272 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00006273 goto fail;
6274 }
ths3b46e622007-09-17 08:09:54 +00006275
bellard4add45b2003-06-05 01:52:59 +00006276 ret = get_errno(sys_getdents(arg1, dirp, count));
6277 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006278 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00006279 struct target_dirent *tde;
6280 int len = ret;
6281 int reclen, treclen;
6282 int count1, tnamelen;
6283
6284 count1 = 0;
6285 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00006286 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6287 goto efault;
bellard4add45b2003-06-05 01:52:59 +00006288 tde = target_dirp;
6289 while (len > 0) {
6290 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00006291 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00006292 tde->d_reclen = tswap16(treclen);
6293 tde->d_ino = tswapl(de->d_ino);
6294 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00006295 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00006296 if (tnamelen > 256)
6297 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00006298 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00006299 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00006300 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00006301 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00006302 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00006303 count1 += treclen;
6304 }
6305 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00006306 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00006307 }
6308 free(dirp);
6309 }
6310#else
bellard31e31b82003-02-18 22:55:36 +00006311 {
aurel326556a832008-10-13 21:08:17 +00006312 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006313 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00006314
bellard579a97f2007-11-11 14:26:47 +00006315 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6316 goto efault;
bellard72f03902003-02-18 23:33:18 +00006317 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00006318 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006319 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00006320 int len = ret;
6321 int reclen;
6322 de = dirp;
6323 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006324 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00006325 if (reclen > len)
6326 break;
bellard8083a3e2003-03-24 23:12:16 +00006327 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00006328 tswapls(&de->d_ino);
6329 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006330 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00006331 len -= reclen;
6332 }
6333 }
pbrook53a59602006-03-25 19:31:22 +00006334 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00006335 }
bellard4add45b2003-06-05 01:52:59 +00006336#endif
bellard31e31b82003-02-18 22:55:36 +00006337 break;
ths3ae43202007-09-16 21:39:48 +00006338#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00006339 case TARGET_NR_getdents64:
6340 {
aurel326556a832008-10-13 21:08:17 +00006341 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006342 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00006343 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6344 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00006345 ret = get_errno(sys_getdents64(arg1, dirp, count));
6346 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006347 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00006348 int len = ret;
6349 int reclen;
6350 de = dirp;
6351 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006352 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00006353 if (reclen > len)
6354 break;
bellard8083a3e2003-03-24 23:12:16 +00006355 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00006356 tswap64s((uint64_t *)&de->d_ino);
6357 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006358 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00006359 len -= reclen;
6360 }
6361 }
pbrook53a59602006-03-25 19:31:22 +00006362 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00006363 }
6364 break;
bellarda541f292004-04-12 20:39:29 +00006365#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00006366#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00006367 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00006368 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00006369 break;
thse5febef2007-04-01 18:31:35 +00006370#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05006371#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
6372# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00006373 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05006374# endif
6375# ifdef TARGET_NR_ppoll
6376 case TARGET_NR_ppoll:
6377# endif
bellard9de5e442003-03-23 16:49:39 +00006378 {
pbrook53a59602006-03-25 19:31:22 +00006379 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00006380 unsigned int nfds = arg2;
6381 int timeout = arg3;
6382 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00006383 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00006384
bellard579a97f2007-11-11 14:26:47 +00006385 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6386 if (!target_pfd)
6387 goto efault;
Mike Frysingerd8035d42011-02-07 01:05:51 -05006388
bellard9de5e442003-03-23 16:49:39 +00006389 pfd = alloca(sizeof(struct pollfd) * nfds);
6390 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006391 pfd[i].fd = tswap32(target_pfd[i].fd);
6392 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00006393 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05006394
6395# ifdef TARGET_NR_ppoll
6396 if (num == TARGET_NR_ppoll) {
6397 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
6398 target_sigset_t *target_set;
6399 sigset_t _set, *set = &_set;
6400
6401 if (arg3) {
6402 if (target_to_host_timespec(timeout_ts, arg3)) {
6403 unlock_user(target_pfd, arg1, 0);
6404 goto efault;
6405 }
6406 } else {
6407 timeout_ts = NULL;
6408 }
6409
6410 if (arg4) {
6411 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
6412 if (!target_set) {
6413 unlock_user(target_pfd, arg1, 0);
6414 goto efault;
6415 }
6416 target_to_host_sigset(set, target_set);
6417 } else {
6418 set = NULL;
6419 }
6420
6421 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
6422
6423 if (!is_error(ret) && arg3) {
6424 host_to_target_timespec(arg3, timeout_ts);
6425 }
6426 if (arg4) {
6427 unlock_user(target_set, arg4, 0);
6428 }
6429 } else
6430# endif
6431 ret = get_errno(poll(pfd, nfds, timeout));
6432
bellard9de5e442003-03-23 16:49:39 +00006433 if (!is_error(ret)) {
6434 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006435 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00006436 }
6437 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00006438 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00006439 }
6440 break;
thse5febef2007-04-01 18:31:35 +00006441#endif
bellard31e31b82003-02-18 22:55:36 +00006442 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00006443 /* NOTE: the flock constant seems to be the same for every
6444 Linux platform */
6445 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006446 break;
6447 case TARGET_NR_readv:
6448 {
6449 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006450 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006451
6452 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006453 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6454 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006455 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006456 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00006457 }
6458 break;
6459 case TARGET_NR_writev:
6460 {
6461 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006462 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006463
6464 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006465 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6466 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006467 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006468 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00006469 }
6470 break;
6471 case TARGET_NR_getsid:
6472 ret = get_errno(getsid(arg1));
6473 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006474#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00006475 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00006476 ret = get_errno(fdatasync(arg1));
6477 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006478#endif
bellard31e31b82003-02-18 22:55:36 +00006479 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00006480 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00006481 return value. */
ths0da46a62007-10-20 20:23:07 +00006482 ret = -TARGET_ENOTDIR;
6483 break;
Mike Frysinger737de1d2011-02-07 01:05:55 -05006484 case TARGET_NR_sched_getaffinity:
6485 {
6486 unsigned int mask_size;
6487 unsigned long *mask;
6488
6489 /*
6490 * sched_getaffinity needs multiples of ulong, so need to take
6491 * care of mismatches between target ulong and host ulong sizes.
6492 */
6493 if (arg2 & (sizeof(abi_ulong) - 1)) {
6494 ret = -TARGET_EINVAL;
6495 break;
6496 }
6497 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
6498
6499 mask = alloca(mask_size);
6500 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
6501
6502 if (!is_error(ret)) {
6503 if (arg2 > ret) {
6504 /* Zero out any extra space kernel didn't fill */
6505 unsigned long zero = arg2 - ret;
6506 p = alloca(zero);
6507 memset(p, 0, zero);
6508 if (copy_to_user(arg3 + zero, p, zero)) {
6509 goto efault;
6510 }
6511 arg2 = ret;
6512 }
6513 if (copy_to_user(arg3, mask, arg2)) {
6514 goto efault;
6515 }
6516 ret = arg2;
6517 }
6518 }
6519 break;
6520 case TARGET_NR_sched_setaffinity:
6521 {
6522 unsigned int mask_size;
6523 unsigned long *mask;
6524
6525 /*
6526 * sched_setaffinity needs multiples of ulong, so need to take
6527 * care of mismatches between target ulong and host ulong sizes.
6528 */
6529 if (arg2 & (sizeof(abi_ulong) - 1)) {
6530 ret = -TARGET_EINVAL;
6531 break;
6532 }
6533 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
6534
6535 mask = alloca(mask_size);
6536 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
6537 goto efault;
6538 }
6539 memcpy(mask, p, arg2);
6540 unlock_user_struct(p, arg2, 0);
6541
6542 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
6543 }
6544 break;
bellard31e31b82003-02-18 22:55:36 +00006545 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00006546 {
pbrook53a59602006-03-25 19:31:22 +00006547 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006548 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00006549
bellard579a97f2007-11-11 14:26:47 +00006550 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 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, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00006554 ret = get_errno(sched_setparam(arg1, &schp));
6555 }
6556 break;
bellard31e31b82003-02-18 22:55:36 +00006557 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00006558 {
pbrook53a59602006-03-25 19:31:22 +00006559 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006560 struct sched_param schp;
6561 ret = get_errno(sched_getparam(arg1, &schp));
6562 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006563 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6564 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006565 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006566 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00006567 }
6568 }
6569 break;
bellard31e31b82003-02-18 22:55:36 +00006570 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00006571 {
pbrook53a59602006-03-25 19:31:22 +00006572 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006573 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00006574 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6575 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006576 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006577 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00006578 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6579 }
6580 break;
bellard31e31b82003-02-18 22:55:36 +00006581 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00006582 ret = get_errno(sched_getscheduler(arg1));
6583 break;
bellard31e31b82003-02-18 22:55:36 +00006584 case TARGET_NR_sched_yield:
6585 ret = get_errno(sched_yield());
6586 break;
6587 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00006588 ret = get_errno(sched_get_priority_max(arg1));
6589 break;
bellard31e31b82003-02-18 22:55:36 +00006590 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00006591 ret = get_errno(sched_get_priority_min(arg1));
6592 break;
bellard31e31b82003-02-18 22:55:36 +00006593 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00006594 {
bellard5cd43932003-03-29 16:54:36 +00006595 struct timespec ts;
6596 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6597 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006598 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00006599 }
6600 }
6601 break;
bellard31e31b82003-02-18 22:55:36 +00006602 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00006603 {
bellard1b6b0292003-03-22 17:31:38 +00006604 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00006605 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00006606 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00006607 if (is_error(ret) && arg2) {
6608 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00006609 }
6610 }
6611 break;
thse5febef2007-04-01 18:31:35 +00006612#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00006613 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00006614 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006615#endif
6616#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00006617 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00006618 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006619#endif
bellard31e31b82003-02-18 22:55:36 +00006620 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00006621 switch (arg1)
6622 {
6623 case PR_GET_PDEATHSIG:
6624 {
6625 int deathsig;
6626 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00006627 if (!is_error(ret) && arg2
6628 && put_user_ual(deathsig, arg2))
6629 goto efault;
thse5574482007-02-11 20:03:13 +00006630 }
6631 break;
6632 default:
6633 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6634 break;
6635 }
ths39b9aae2007-02-11 18:36:44 +00006636 break;
bellardd2fd1af2007-11-14 18:08:56 +00006637#ifdef TARGET_NR_arch_prctl
6638 case TARGET_NR_arch_prctl:
6639#if defined(TARGET_I386) && !defined(TARGET_ABI32)
6640 ret = do_arch_prctl(cpu_env, arg1, arg2);
6641 break;
6642#else
6643 goto unimplemented;
6644#endif
6645#endif
bellard67867302003-11-23 17:05:30 +00006646#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00006647 case TARGET_NR_pread:
balroga4ae00b2008-09-20 03:14:14 +00006648#ifdef TARGET_ARM
6649 if (((CPUARMState *)cpu_env)->eabi)
6650 arg4 = arg5;
6651#endif
bellard579a97f2007-11-11 14:26:47 +00006652 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6653 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006654 ret = get_errno(pread(arg1, p, arg3, arg4));
6655 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00006656 break;
bellard31e31b82003-02-18 22:55:36 +00006657 case TARGET_NR_pwrite:
balroga4ae00b2008-09-20 03:14:14 +00006658#ifdef TARGET_ARM
6659 if (((CPUARMState *)cpu_env)->eabi)
6660 arg4 = arg5;
6661#endif
bellard579a97f2007-11-11 14:26:47 +00006662 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6663 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006664 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6665 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00006666 break;
bellard67867302003-11-23 17:05:30 +00006667#endif
aurel32f2c7ba12008-03-28 22:32:06 +00006668#ifdef TARGET_NR_pread64
6669 case TARGET_NR_pread64:
6670 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6671 goto efault;
6672 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6673 unlock_user(p, arg2, ret);
6674 break;
6675 case TARGET_NR_pwrite64:
6676 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6677 goto efault;
6678 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6679 unlock_user(p, arg2, 0);
6680 break;
6681#endif
bellard31e31b82003-02-18 22:55:36 +00006682 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00006683 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6684 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006685 ret = get_errno(sys_getcwd1(p, arg2));
6686 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00006687 break;
6688 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00006689 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006690 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00006691 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006692 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00006693#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02006694 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6695 defined(TARGET_M68K)
bellard579a97f2007-11-11 14:26:47 +00006696 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00006697 break;
6698#else
bellard5cd43932003-03-29 16:54:36 +00006699 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00006700#endif
bellard31e31b82003-02-18 22:55:36 +00006701 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00006702 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006703#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00006704 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00006705 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006706#endif
6707#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00006708 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00006709 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006710#endif
bellard048f6b42005-11-26 18:47:20 +00006711#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00006712 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00006713 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6714 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00006715 break;
bellard048f6b42005-11-26 18:47:20 +00006716#endif
bellardebc05482003-09-30 21:08:41 +00006717#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00006718 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00006719 {
6720 struct rlimit rlim;
6721 ret = get_errno(getrlimit(arg1, &rlim));
6722 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006723 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00006724 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6725 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006726 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6727 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006728 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00006729 }
6730 break;
6731 }
bellardebc05482003-09-30 21:08:41 +00006732#endif
bellarda315a142005-01-30 22:59:18 +00006733#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00006734 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00006735 if (!(p = lock_user_string(arg1)))
6736 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006737 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6738 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00006739 break;
bellarda315a142005-01-30 22:59:18 +00006740#endif
6741#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00006742 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00006743 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00006744 break;
bellarda315a142005-01-30 22:59:18 +00006745#endif
6746#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00006747 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00006748 if (!(p = lock_user_string(arg1)))
6749 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006750 ret = get_errno(stat(path(p), &st));
6751 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006752 if (!is_error(ret))
6753 ret = host_to_target_stat64(cpu_env, arg2, &st);
6754 break;
bellarda315a142005-01-30 22:59:18 +00006755#endif
6756#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00006757 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00006758 if (!(p = lock_user_string(arg1)))
6759 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006760 ret = get_errno(lstat(path(p), &st));
6761 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006762 if (!is_error(ret))
6763 ret = host_to_target_stat64(cpu_env, arg2, &st);
6764 break;
bellarda315a142005-01-30 22:59:18 +00006765#endif
6766#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00006767 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00006768 ret = get_errno(fstat(arg1, &st));
6769 if (!is_error(ret))
6770 ret = host_to_target_stat64(cpu_env, arg2, &st);
6771 break;
bellardec86b0f2003-04-11 00:15:04 +00006772#endif
aurel329d33b762009-04-08 23:07:05 +00006773#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6774 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6775#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006776 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00006777#endif
6778#ifdef TARGET_NR_newfstatat
6779 case TARGET_NR_newfstatat:
6780#endif
balrog6a24a772008-09-20 02:23:36 +00006781 if (!(p = lock_user_string(arg2)))
6782 goto efault;
aurel329d33b762009-04-08 23:07:05 +00006783#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006784 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00006785#else
6786 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6787#endif
balrog6a24a772008-09-20 02:23:36 +00006788 if (!is_error(ret))
6789 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00006790 break;
bellarda315a142005-01-30 22:59:18 +00006791#endif
bellard67867302003-11-23 17:05:30 +00006792 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00006793 if (!(p = lock_user_string(arg1)))
6794 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006795 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6796 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006797 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03006798#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00006799 case TARGET_NR_getuid:
6800 ret = get_errno(high2lowuid(getuid()));
6801 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03006802#endif
6803#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00006804 case TARGET_NR_getgid:
6805 ret = get_errno(high2lowgid(getgid()));
6806 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03006807#endif
6808#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00006809 case TARGET_NR_geteuid:
6810 ret = get_errno(high2lowuid(geteuid()));
6811 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03006812#endif
6813#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00006814 case TARGET_NR_getegid:
6815 ret = get_errno(high2lowgid(getegid()));
6816 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03006817#endif
bellard67867302003-11-23 17:05:30 +00006818 case TARGET_NR_setreuid:
6819 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6820 break;
6821 case TARGET_NR_setregid:
6822 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6823 break;
6824 case TARGET_NR_getgroups:
6825 {
6826 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03006827 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006828 gid_t *grouplist;
6829 int i;
6830
6831 grouplist = alloca(gidsetsize * sizeof(gid_t));
6832 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006833 if (gidsetsize == 0)
6834 break;
bellard67867302003-11-23 17:05:30 +00006835 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006836 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6837 if (!target_grouplist)
6838 goto efault;
balroga2155fc2008-09-20 02:12:08 +00006839 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03006840 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00006841 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00006842 }
6843 }
6844 break;
6845 case TARGET_NR_setgroups:
6846 {
6847 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03006848 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006849 gid_t *grouplist;
6850 int i;
6851
6852 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006853 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6854 if (!target_grouplist) {
6855 ret = -TARGET_EFAULT;
6856 goto fail;
6857 }
bellard67867302003-11-23 17:05:30 +00006858 for(i = 0;i < gidsetsize; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03006859 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00006860 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00006861 ret = get_errno(setgroups(gidsetsize, grouplist));
6862 }
6863 break;
6864 case TARGET_NR_fchown:
6865 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6866 break;
thsccfa72b2007-09-24 09:23:34 +00006867#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6868 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00006869 if (!(p = lock_user_string(arg2)))
6870 goto efault;
6871 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6872 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00006873 break;
6874#endif
bellard67867302003-11-23 17:05:30 +00006875#ifdef TARGET_NR_setresuid
6876 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00006877 ret = get_errno(setresuid(low2highuid(arg1),
6878 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00006879 low2highuid(arg3)));
6880 break;
6881#endif
6882#ifdef TARGET_NR_getresuid
6883 case TARGET_NR_getresuid:
6884 {
pbrook53a59602006-03-25 19:31:22 +00006885 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00006886 ret = get_errno(getresuid(&ruid, &euid, &suid));
6887 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006888 if (put_user_u16(high2lowuid(ruid), arg1)
6889 || put_user_u16(high2lowuid(euid), arg2)
6890 || put_user_u16(high2lowuid(suid), arg3))
6891 goto efault;
bellard67867302003-11-23 17:05:30 +00006892 }
6893 }
6894 break;
6895#endif
6896#ifdef TARGET_NR_getresgid
6897 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00006898 ret = get_errno(setresgid(low2highgid(arg1),
6899 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00006900 low2highgid(arg3)));
6901 break;
6902#endif
6903#ifdef TARGET_NR_getresgid
6904 case TARGET_NR_getresgid:
6905 {
pbrook53a59602006-03-25 19:31:22 +00006906 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00006907 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6908 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006909 if (put_user_u16(high2lowgid(rgid), arg1)
6910 || put_user_u16(high2lowgid(egid), arg2)
6911 || put_user_u16(high2lowgid(sgid), arg3))
6912 goto efault;
bellard67867302003-11-23 17:05:30 +00006913 }
6914 }
6915 break;
6916#endif
6917 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00006918 if (!(p = lock_user_string(arg1)))
6919 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006920 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6921 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006922 break;
6923 case TARGET_NR_setuid:
6924 ret = get_errno(setuid(low2highuid(arg1)));
6925 break;
6926 case TARGET_NR_setgid:
6927 ret = get_errno(setgid(low2highgid(arg1)));
6928 break;
6929 case TARGET_NR_setfsuid:
6930 ret = get_errno(setfsuid(arg1));
6931 break;
6932 case TARGET_NR_setfsgid:
6933 ret = get_errno(setfsgid(arg1));
6934 break;
bellard67867302003-11-23 17:05:30 +00006935
bellarda315a142005-01-30 22:59:18 +00006936#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00006937 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00006938 if (!(p = lock_user_string(arg1)))
6939 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006940 ret = get_errno(lchown(p, arg2, arg3));
6941 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006942 break;
bellarda315a142005-01-30 22:59:18 +00006943#endif
6944#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00006945 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00006946 ret = get_errno(getuid());
6947 break;
bellarda315a142005-01-30 22:59:18 +00006948#endif
aurel3264b4d282008-11-14 17:20:15 +00006949
6950#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6951 /* Alpha specific */
6952 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006953 {
6954 uid_t euid;
6955 euid=geteuid();
6956 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6957 }
aurel3264b4d282008-11-14 17:20:15 +00006958 ret = get_errno(getuid());
6959 break;
6960#endif
6961#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6962 /* Alpha specific */
6963 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006964 {
6965 uid_t egid;
6966 egid=getegid();
6967 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6968 }
aurel3264b4d282008-11-14 17:20:15 +00006969 ret = get_errno(getgid());
6970 break;
6971#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08006972#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
6973 /* Alpha specific */
6974 case TARGET_NR_osf_getsysinfo:
6975 ret = -TARGET_EOPNOTSUPP;
6976 switch (arg1) {
6977 case TARGET_GSI_IEEE_FP_CONTROL:
6978 {
6979 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
6980
6981 /* Copied from linux ieee_fpcr_to_swcr. */
6982 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
6983 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
6984 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
6985 | SWCR_TRAP_ENABLE_DZE
6986 | SWCR_TRAP_ENABLE_OVF);
6987 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
6988 | SWCR_TRAP_ENABLE_INE);
6989 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
6990 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
6991
6992 if (put_user_u64 (swcr, arg2))
6993 goto efault;
6994 ret = 0;
6995 }
6996 break;
6997
6998 /* case GSI_IEEE_STATE_AT_SIGNAL:
6999 -- Not implemented in linux kernel.
7000 case GSI_UACPROC:
7001 -- Retrieves current unaligned access state; not much used.
7002 case GSI_PROC_TYPE:
7003 -- Retrieves implver information; surely not used.
7004 case GSI_GET_HWRPB:
7005 -- Grabs a copy of the HWRPB; surely not used.
7006 */
7007 }
7008 break;
7009#endif
7010#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
7011 /* Alpha specific */
7012 case TARGET_NR_osf_setsysinfo:
7013 ret = -TARGET_EOPNOTSUPP;
7014 switch (arg1) {
7015 case TARGET_SSI_IEEE_FP_CONTROL:
7016 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
7017 {
7018 uint64_t swcr, fpcr, orig_fpcr;
7019
7020 if (get_user_u64 (swcr, arg2))
7021 goto efault;
7022 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
7023 fpcr = orig_fpcr & FPCR_DYN_MASK;
7024
7025 /* Copied from linux ieee_swcr_to_fpcr. */
7026 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
7027 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
7028 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
7029 | SWCR_TRAP_ENABLE_DZE
7030 | SWCR_TRAP_ENABLE_OVF)) << 48;
7031 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
7032 | SWCR_TRAP_ENABLE_INE)) << 57;
7033 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
7034 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
7035
7036 cpu_alpha_store_fpcr (cpu_env, fpcr);
7037 ret = 0;
7038
7039 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
7040 /* Old exceptions are not signaled. */
7041 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
7042
7043 /* If any exceptions set by this call, and are unmasked,
7044 send a signal. */
7045 /* ??? FIXME */
7046 }
7047 }
7048 break;
7049
7050 /* case SSI_NVPAIRS:
7051 -- Used with SSIN_UACPROC to enable unaligned accesses.
7052 case SSI_IEEE_STATE_AT_SIGNAL:
7053 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
7054 -- Not implemented in linux kernel
7055 */
7056 }
7057 break;
7058#endif
7059#ifdef TARGET_NR_osf_sigprocmask
7060 /* Alpha specific. */
7061 case TARGET_NR_osf_sigprocmask:
7062 {
7063 abi_ulong mask;
7064 int how = arg1;
7065 sigset_t set, oldset;
7066
7067 switch(arg1) {
7068 case TARGET_SIG_BLOCK:
7069 how = SIG_BLOCK;
7070 break;
7071 case TARGET_SIG_UNBLOCK:
7072 how = SIG_UNBLOCK;
7073 break;
7074 case TARGET_SIG_SETMASK:
7075 how = SIG_SETMASK;
7076 break;
7077 default:
7078 ret = -TARGET_EINVAL;
7079 goto fail;
7080 }
7081 mask = arg2;
7082 target_to_host_old_sigset(&set, &mask);
7083 sigprocmask(arg1, &set, &oldset);
7084 host_to_target_old_sigset(&mask, &oldset);
7085 ret = mask;
7086 }
7087 break;
7088#endif
aurel3264b4d282008-11-14 17:20:15 +00007089
bellarda315a142005-01-30 22:59:18 +00007090#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00007091 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00007092 ret = get_errno(getgid());
7093 break;
bellarda315a142005-01-30 22:59:18 +00007094#endif
7095#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00007096 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00007097 ret = get_errno(geteuid());
7098 break;
bellarda315a142005-01-30 22:59:18 +00007099#endif
7100#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00007101 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00007102 ret = get_errno(getegid());
7103 break;
bellarda315a142005-01-30 22:59:18 +00007104#endif
7105#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00007106 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00007107 ret = get_errno(setreuid(arg1, arg2));
7108 break;
bellarda315a142005-01-30 22:59:18 +00007109#endif
7110#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00007111 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00007112 ret = get_errno(setregid(arg1, arg2));
7113 break;
bellarda315a142005-01-30 22:59:18 +00007114#endif
7115#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00007116 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00007117 {
7118 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007119 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007120 gid_t *grouplist;
7121 int i;
7122
7123 grouplist = alloca(gidsetsize * sizeof(gid_t));
7124 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007125 if (gidsetsize == 0)
7126 break;
bellard99c475a2005-01-31 20:45:13 +00007127 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007128 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
7129 if (!target_grouplist) {
7130 ret = -TARGET_EFAULT;
7131 goto fail;
7132 }
balroga2155fc2008-09-20 02:12:08 +00007133 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00007134 target_grouplist[i] = tswap32(grouplist[i]);
7135 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00007136 }
7137 }
7138 break;
bellarda315a142005-01-30 22:59:18 +00007139#endif
7140#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00007141 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00007142 {
7143 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007144 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007145 gid_t *grouplist;
7146 int i;
ths3b46e622007-09-17 08:09:54 +00007147
bellard99c475a2005-01-31 20:45:13 +00007148 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007149 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
7150 if (!target_grouplist) {
7151 ret = -TARGET_EFAULT;
7152 goto fail;
7153 }
bellard99c475a2005-01-31 20:45:13 +00007154 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00007155 grouplist[i] = tswap32(target_grouplist[i]);
7156 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00007157 ret = get_errno(setgroups(gidsetsize, grouplist));
7158 }
7159 break;
bellarda315a142005-01-30 22:59:18 +00007160#endif
7161#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00007162 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00007163 ret = get_errno(fchown(arg1, arg2, arg3));
7164 break;
bellarda315a142005-01-30 22:59:18 +00007165#endif
7166#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00007167 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00007168 ret = get_errno(setresuid(arg1, arg2, arg3));
7169 break;
bellarda315a142005-01-30 22:59:18 +00007170#endif
7171#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00007172 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00007173 {
pbrook53a59602006-03-25 19:31:22 +00007174 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00007175 ret = get_errno(getresuid(&ruid, &euid, &suid));
7176 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007177 if (put_user_u32(ruid, arg1)
7178 || put_user_u32(euid, arg2)
7179 || put_user_u32(suid, arg3))
7180 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00007181 }
7182 }
7183 break;
bellarda315a142005-01-30 22:59:18 +00007184#endif
7185#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00007186 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00007187 ret = get_errno(setresgid(arg1, arg2, arg3));
7188 break;
bellarda315a142005-01-30 22:59:18 +00007189#endif
7190#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00007191 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00007192 {
pbrook53a59602006-03-25 19:31:22 +00007193 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00007194 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7195 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007196 if (put_user_u32(rgid, arg1)
7197 || put_user_u32(egid, arg2)
7198 || put_user_u32(sgid, arg3))
7199 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00007200 }
7201 }
7202 break;
bellarda315a142005-01-30 22:59:18 +00007203#endif
7204#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00007205 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00007206 if (!(p = lock_user_string(arg1)))
7207 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007208 ret = get_errno(chown(p, arg2, arg3));
7209 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007210 break;
bellarda315a142005-01-30 22:59:18 +00007211#endif
7212#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00007213 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00007214 ret = get_errno(setuid(arg1));
7215 break;
bellarda315a142005-01-30 22:59:18 +00007216#endif
7217#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00007218 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00007219 ret = get_errno(setgid(arg1));
7220 break;
bellarda315a142005-01-30 22:59:18 +00007221#endif
7222#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00007223 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00007224 ret = get_errno(setfsuid(arg1));
7225 break;
bellarda315a142005-01-30 22:59:18 +00007226#endif
7227#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00007228 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00007229 ret = get_errno(setfsgid(arg1));
7230 break;
bellarda315a142005-01-30 22:59:18 +00007231#endif
bellard67867302003-11-23 17:05:30 +00007232
bellard31e31b82003-02-18 22:55:36 +00007233 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00007234 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00007235#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00007236 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00007237 {
7238 void *a;
7239 ret = -TARGET_EFAULT;
7240 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
7241 goto efault;
7242 if (!(p = lock_user_string(arg3)))
7243 goto mincore_fail;
7244 ret = get_errno(mincore(a, arg2, p));
7245 unlock_user(p, arg3, ret);
7246 mincore_fail:
7247 unlock_user(a, arg1, 0);
7248 }
7249 break;
bellardffa65c32004-01-04 23:57:22 +00007250#endif
aurel32408321b2008-10-01 21:46:32 +00007251#ifdef TARGET_NR_arm_fadvise64_64
7252 case TARGET_NR_arm_fadvise64_64:
7253 {
7254 /*
7255 * arm_fadvise64_64 looks like fadvise64_64 but
7256 * with different argument order
7257 */
7258 abi_long temp;
7259 temp = arg3;
7260 arg3 = arg4;
7261 arg4 = temp;
7262 }
7263#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02007264#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00007265#ifdef TARGET_NR_fadvise64_64
7266 case TARGET_NR_fadvise64_64:
7267#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02007268#ifdef TARGET_NR_fadvise64
7269 case TARGET_NR_fadvise64:
7270#endif
7271#ifdef TARGET_S390X
7272 switch (arg4) {
7273 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
7274 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
7275 case 6: arg4 = POSIX_FADV_DONTNEED; break;
7276 case 7: arg4 = POSIX_FADV_NOREUSE; break;
7277 default: break;
7278 }
7279#endif
7280 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00007281 break;
7282#endif
bellardffa65c32004-01-04 23:57:22 +00007283#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00007284 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00007285 /* A straight passthrough may not be safe because qemu sometimes
7286 turns private flie-backed mappings into anonymous mappings.
7287 This will break MADV_DONTNEED.
7288 This is a hint, so ignoring and returning success is ok. */
7289 ret = get_errno(0);
7290 break;
bellardffa65c32004-01-04 23:57:22 +00007291#endif
blueswir1992f48a2007-10-14 16:27:31 +00007292#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00007293 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00007294 {
thsb1e341e2007-03-20 21:50:52 +00007295 int cmd;
bellard77e46722003-04-29 20:39:06 +00007296 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00007297 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00007298#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00007299 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00007300#endif
bellard77e46722003-04-29 20:39:06 +00007301
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007302 cmd = target_to_host_fcntl_cmd(arg2);
7303 if (cmd == -TARGET_EINVAL)
7304 return cmd;
thsb1e341e2007-03-20 21:50:52 +00007305
bellard60cd49d2003-03-16 22:53:56 +00007306 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00007307 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00007308#ifdef TARGET_ARM
7309 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007310 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7311 goto efault;
ths58134272007-03-31 18:59:32 +00007312 fl.l_type = tswap16(target_efl->l_type);
7313 fl.l_whence = tswap16(target_efl->l_whence);
7314 fl.l_start = tswap64(target_efl->l_start);
7315 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007316 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00007317 unlock_user_struct(target_efl, arg3, 0);
7318 } else
7319#endif
7320 {
bellard9ee1fa22007-11-11 15:11:19 +00007321 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7322 goto efault;
ths58134272007-03-31 18:59:32 +00007323 fl.l_type = tswap16(target_fl->l_type);
7324 fl.l_whence = tswap16(target_fl->l_whence);
7325 fl.l_start = tswap64(target_fl->l_start);
7326 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007327 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00007328 unlock_user_struct(target_fl, arg3, 0);
7329 }
thsb1e341e2007-03-20 21:50:52 +00007330 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00007331 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00007332#ifdef TARGET_ARM
7333 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007334 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
7335 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007336 target_efl->l_type = tswap16(fl.l_type);
7337 target_efl->l_whence = tswap16(fl.l_whence);
7338 target_efl->l_start = tswap64(fl.l_start);
7339 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007340 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00007341 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00007342 } else
7343#endif
7344 {
bellard9ee1fa22007-11-11 15:11:19 +00007345 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
7346 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007347 target_fl->l_type = tswap16(fl.l_type);
7348 target_fl->l_whence = tswap16(fl.l_whence);
7349 target_fl->l_start = tswap64(fl.l_start);
7350 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007351 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00007352 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00007353 }
bellard77e46722003-04-29 20:39:06 +00007354 }
7355 break;
7356
thsb1e341e2007-03-20 21:50:52 +00007357 case TARGET_F_SETLK64:
7358 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00007359#ifdef TARGET_ARM
7360 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007361 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7362 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007363 fl.l_type = tswap16(target_efl->l_type);
7364 fl.l_whence = tswap16(target_efl->l_whence);
7365 fl.l_start = tswap64(target_efl->l_start);
7366 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007367 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00007368 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00007369 } else
7370#endif
7371 {
bellard9ee1fa22007-11-11 15:11:19 +00007372 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7373 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007374 fl.l_type = tswap16(target_fl->l_type);
7375 fl.l_whence = tswap16(target_fl->l_whence);
7376 fl.l_start = tswap64(target_fl->l_start);
7377 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007378 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00007379 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00007380 }
thsb1e341e2007-03-20 21:50:52 +00007381 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00007382 break;
bellard60cd49d2003-03-16 22:53:56 +00007383 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007384 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00007385 break;
7386 }
bellard77e46722003-04-29 20:39:06 +00007387 break;
7388 }
bellard60cd49d2003-03-16 22:53:56 +00007389#endif
ths7d600c82006-12-08 01:32:58 +00007390#ifdef TARGET_NR_cacheflush
7391 case TARGET_NR_cacheflush:
7392 /* self-modifying code is handled automatically, so nothing needed */
7393 ret = 0;
7394 break;
7395#endif
bellardebc05482003-09-30 21:08:41 +00007396#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00007397 case TARGET_NR_security:
7398 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007399#endif
bellardc573ff62004-01-04 15:51:36 +00007400#ifdef TARGET_NR_getpagesize
7401 case TARGET_NR_getpagesize:
7402 ret = TARGET_PAGE_SIZE;
7403 break;
7404#endif
bellard31e31b82003-02-18 22:55:36 +00007405 case TARGET_NR_gettid:
7406 ret = get_errno(gettid());
7407 break;
thse5febef2007-04-01 18:31:35 +00007408#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00007409 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00007410#if TARGET_ABI_BITS == 32
7411#ifdef TARGET_ARM
7412 if (((CPUARMState *)cpu_env)->eabi)
7413 {
7414 arg2 = arg3;
7415 arg3 = arg4;
7416 arg4 = arg5;
7417 }
7418#endif
7419 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
7420#else
7421 ret = get_errno(readahead(arg1, arg2, arg3));
7422#endif
7423 break;
thse5febef2007-04-01 18:31:35 +00007424#endif
bellardebc05482003-09-30 21:08:41 +00007425#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00007426 case TARGET_NR_setxattr:
7427 case TARGET_NR_lsetxattr:
7428 case TARGET_NR_fsetxattr:
7429 case TARGET_NR_getxattr:
7430 case TARGET_NR_lgetxattr:
7431 case TARGET_NR_fgetxattr:
7432 case TARGET_NR_listxattr:
7433 case TARGET_NR_llistxattr:
7434 case TARGET_NR_flistxattr:
7435 case TARGET_NR_removexattr:
7436 case TARGET_NR_lremovexattr:
7437 case TARGET_NR_fremovexattr:
Arnaud Patard6f932f92009-04-21 21:04:18 +03007438 ret = -TARGET_EOPNOTSUPP;
7439 break;
bellardebc05482003-09-30 21:08:41 +00007440#endif
7441#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00007442 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007443#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00007444 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
7445 ret = 0;
7446 break;
edgar_iglef967792009-01-07 14:19:38 +00007447#elif defined(TARGET_CRIS)
7448 if (arg1 & 0xff)
7449 ret = -TARGET_EINVAL;
7450 else {
7451 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
7452 ret = 0;
7453 }
7454 break;
bellard8d18e892007-11-14 15:18:40 +00007455#elif defined(TARGET_I386) && defined(TARGET_ABI32)
7456 ret = do_set_thread_area(cpu_env, arg1);
7457 break;
ths6f5b89a2007-03-02 20:48:00 +00007458#else
7459 goto unimplemented_nowarn;
7460#endif
7461#endif
7462#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00007463 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007464#if defined(TARGET_I386) && defined(TARGET_ABI32)
7465 ret = do_get_thread_area(cpu_env, arg1);
7466#else
bellard5cd43932003-03-29 16:54:36 +00007467 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00007468#endif
bellard8d18e892007-11-14 15:18:40 +00007469#endif
bellard48dc41e2006-06-21 18:15:50 +00007470#ifdef TARGET_NR_getdomainname
7471 case TARGET_NR_getdomainname:
7472 goto unimplemented_nowarn;
7473#endif
ths6f5b89a2007-03-02 20:48:00 +00007474
thsb5906f92007-03-19 13:32:45 +00007475#ifdef TARGET_NR_clock_gettime
7476 case TARGET_NR_clock_gettime:
7477 {
7478 struct timespec ts;
7479 ret = get_errno(clock_gettime(arg1, &ts));
7480 if (!is_error(ret)) {
7481 host_to_target_timespec(arg2, &ts);
7482 }
7483 break;
7484 }
7485#endif
7486#ifdef TARGET_NR_clock_getres
7487 case TARGET_NR_clock_getres:
7488 {
7489 struct timespec ts;
7490 ret = get_errno(clock_getres(arg1, &ts));
7491 if (!is_error(ret)) {
7492 host_to_target_timespec(arg2, &ts);
7493 }
7494 break;
7495 }
7496#endif
pbrook63d76512008-05-29 13:43:29 +00007497#ifdef TARGET_NR_clock_nanosleep
7498 case TARGET_NR_clock_nanosleep:
7499 {
7500 struct timespec ts;
7501 target_to_host_timespec(&ts, arg3);
7502 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7503 if (arg4)
7504 host_to_target_timespec(arg4, &ts);
7505 break;
7506 }
7507#endif
thsb5906f92007-03-19 13:32:45 +00007508
ths6f5b89a2007-03-02 20:48:00 +00007509#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7510 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00007511 ret = get_errno(set_tid_address((int *)g2h(arg1)));
7512 break;
ths6f5b89a2007-03-02 20:48:00 +00007513#endif
7514
ths3ae43202007-09-16 21:39:48 +00007515#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00007516 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00007517 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00007518 break;
7519#endif
7520
ths3ae43202007-09-16 21:39:48 +00007521#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00007522 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00007523 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7524 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00007525 break;
7526#endif
7527
ths4f2b1fe2007-06-21 21:57:12 +00007528#ifdef TARGET_NR_set_robust_list
7529 case TARGET_NR_set_robust_list:
7530 goto unimplemented_nowarn;
7531#endif
7532
ths9007f0e2007-09-25 17:50:37 +00007533#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7534 case TARGET_NR_utimensat:
7535 {
Riku Voipioebc996f2009-04-21 15:01:51 +03007536 struct timespec *tsp, ts[2];
7537 if (!arg3) {
7538 tsp = NULL;
7539 } else {
7540 target_to_host_timespec(ts, arg3);
7541 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7542 tsp = ts;
7543 }
ths9007f0e2007-09-25 17:50:37 +00007544 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03007545 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00007546 else {
bellard579a97f2007-11-11 14:26:47 +00007547 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00007548 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00007549 goto fail;
7550 }
Riku Voipioebc996f2009-04-21 15:01:51 +03007551 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00007552 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00007553 }
7554 }
7555 break;
7556#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02007557#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00007558 case TARGET_NR_futex:
7559 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7560 break;
7561#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007562#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00007563 case TARGET_NR_inotify_init:
7564 ret = get_errno(sys_inotify_init());
7565 break;
7566#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007567#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00007568#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7569 case TARGET_NR_inotify_init1:
7570 ret = get_errno(sys_inotify_init1(arg1));
7571 break;
7572#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007573#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007574#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00007575 case TARGET_NR_inotify_add_watch:
7576 p = lock_user_string(arg2);
7577 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7578 unlock_user(p, arg2, 0);
7579 break;
7580#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007581#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00007582 case TARGET_NR_inotify_rm_watch:
7583 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7584 break;
7585#endif
ths9007f0e2007-09-25 17:50:37 +00007586
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07007587#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00007588 case TARGET_NR_mq_open:
7589 {
7590 struct mq_attr posix_mq_attr;
7591
7592 p = lock_user_string(arg1 - 1);
7593 if (arg4 != 0)
7594 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7595 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7596 unlock_user (p, arg1, 0);
7597 }
7598 break;
7599
7600 case TARGET_NR_mq_unlink:
7601 p = lock_user_string(arg1 - 1);
7602 ret = get_errno(mq_unlink(p));
7603 unlock_user (p, arg1, 0);
7604 break;
7605
7606 case TARGET_NR_mq_timedsend:
7607 {
7608 struct timespec ts;
7609
7610 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7611 if (arg5 != 0) {
7612 target_to_host_timespec(&ts, arg5);
7613 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7614 host_to_target_timespec(arg5, &ts);
7615 }
7616 else
7617 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7618 unlock_user (p, arg2, arg3);
7619 }
7620 break;
7621
7622 case TARGET_NR_mq_timedreceive:
7623 {
7624 struct timespec ts;
7625 unsigned int prio;
7626
7627 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7628 if (arg5 != 0) {
7629 target_to_host_timespec(&ts, arg5);
7630 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7631 host_to_target_timespec(arg5, &ts);
7632 }
7633 else
7634 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7635 unlock_user (p, arg2, arg3);
7636 if (arg4 != 0)
7637 put_user_u32(prio, arg4);
7638 }
7639 break;
7640
7641 /* Not implemented for now... */
7642/* case TARGET_NR_mq_notify: */
7643/* break; */
7644
7645 case TARGET_NR_mq_getsetattr:
7646 {
7647 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7648 ret = 0;
7649 if (arg3 != 0) {
7650 ret = mq_getattr(arg1, &posix_mq_attr_out);
7651 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7652 }
7653 if (arg2 != 0) {
7654 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7655 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7656 }
7657
7658 }
7659 break;
7660#endif
7661
vibisreenivasan3ce34df2009-05-16 18:32:41 +05307662#ifdef CONFIG_SPLICE
7663#ifdef TARGET_NR_tee
7664 case TARGET_NR_tee:
7665 {
7666 ret = get_errno(tee(arg1,arg2,arg3,arg4));
7667 }
7668 break;
7669#endif
7670#ifdef TARGET_NR_splice
7671 case TARGET_NR_splice:
7672 {
7673 loff_t loff_in, loff_out;
7674 loff_t *ploff_in = NULL, *ploff_out = NULL;
7675 if(arg2) {
7676 get_user_u64(loff_in, arg2);
7677 ploff_in = &loff_in;
7678 }
7679 if(arg4) {
7680 get_user_u64(loff_out, arg2);
7681 ploff_out = &loff_out;
7682 }
7683 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7684 }
7685 break;
7686#endif
7687#ifdef TARGET_NR_vmsplice
7688 case TARGET_NR_vmsplice:
7689 {
7690 int count = arg3;
7691 struct iovec *vec;
7692
7693 vec = alloca(count * sizeof(struct iovec));
7694 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7695 goto efault;
7696 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7697 unlock_iovec(vec, arg2, count, 0);
7698 }
7699 break;
7700#endif
7701#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03007702#ifdef CONFIG_EVENTFD
7703#if defined(TARGET_NR_eventfd)
7704 case TARGET_NR_eventfd:
7705 ret = get_errno(eventfd(arg1, 0));
7706 break;
7707#endif
7708#if defined(TARGET_NR_eventfd2)
7709 case TARGET_NR_eventfd2:
7710 ret = get_errno(eventfd(arg1, arg2));
7711 break;
7712#endif
7713#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03007714#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7715 case TARGET_NR_fallocate:
7716 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7717 break;
7718#endif
Peter Maydellc727f472011-01-06 11:05:10 +00007719#if defined(CONFIG_SYNC_FILE_RANGE)
7720#if defined(TARGET_NR_sync_file_range)
7721 case TARGET_NR_sync_file_range:
7722#if TARGET_ABI_BITS == 32
7723 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
7724 target_offset64(arg4, arg5), arg6));
7725#else
7726 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
7727#endif
7728 break;
7729#endif
7730#if defined(TARGET_NR_sync_file_range2)
7731 case TARGET_NR_sync_file_range2:
7732 /* This is like sync_file_range but the arguments are reordered */
7733#if TARGET_ABI_BITS == 32
7734 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
7735 target_offset64(arg5, arg6), arg2));
7736#else
7737 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
7738#endif
7739 break;
7740#endif
7741#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00007742#if defined(CONFIG_EPOLL)
7743#if defined(TARGET_NR_epoll_create)
7744 case TARGET_NR_epoll_create:
7745 ret = get_errno(epoll_create(arg1));
7746 break;
7747#endif
7748#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
7749 case TARGET_NR_epoll_create1:
7750 ret = get_errno(epoll_create1(arg1));
7751 break;
7752#endif
7753#if defined(TARGET_NR_epoll_ctl)
7754 case TARGET_NR_epoll_ctl:
7755 {
7756 struct epoll_event ep;
7757 struct epoll_event *epp = 0;
7758 if (arg4) {
7759 struct target_epoll_event *target_ep;
7760 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
7761 goto efault;
7762 }
7763 ep.events = tswap32(target_ep->events);
7764 /* The epoll_data_t union is just opaque data to the kernel,
7765 * so we transfer all 64 bits across and need not worry what
7766 * actual data type it is.
7767 */
7768 ep.data.u64 = tswap64(target_ep->data.u64);
7769 unlock_user_struct(target_ep, arg4, 0);
7770 epp = &ep;
7771 }
7772 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
7773 break;
7774 }
7775#endif
7776
7777#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
7778#define IMPLEMENT_EPOLL_PWAIT
7779#endif
7780#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
7781#if defined(TARGET_NR_epoll_wait)
7782 case TARGET_NR_epoll_wait:
7783#endif
7784#if defined(IMPLEMENT_EPOLL_PWAIT)
7785 case TARGET_NR_epoll_pwait:
7786#endif
7787 {
7788 struct target_epoll_event *target_ep;
7789 struct epoll_event *ep;
7790 int epfd = arg1;
7791 int maxevents = arg3;
7792 int timeout = arg4;
7793
7794 target_ep = lock_user(VERIFY_WRITE, arg2,
7795 maxevents * sizeof(struct target_epoll_event), 1);
7796 if (!target_ep) {
7797 goto efault;
7798 }
7799
7800 ep = alloca(maxevents * sizeof(struct epoll_event));
7801
7802 switch (num) {
7803#if defined(IMPLEMENT_EPOLL_PWAIT)
7804 case TARGET_NR_epoll_pwait:
7805 {
7806 target_sigset_t *target_set;
7807 sigset_t _set, *set = &_set;
7808
7809 if (arg5) {
7810 target_set = lock_user(VERIFY_READ, arg5,
7811 sizeof(target_sigset_t), 1);
7812 if (!target_set) {
7813 unlock_user(target_ep, arg2, 0);
7814 goto efault;
7815 }
7816 target_to_host_sigset(set, target_set);
7817 unlock_user(target_set, arg5, 0);
7818 } else {
7819 set = NULL;
7820 }
7821
7822 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
7823 break;
7824 }
7825#endif
7826#if defined(TARGET_NR_epoll_wait)
7827 case TARGET_NR_epoll_wait:
7828 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
7829 break;
7830#endif
7831 default:
7832 ret = -TARGET_ENOSYS;
7833 }
7834 if (!is_error(ret)) {
7835 int i;
7836 for (i = 0; i < ret; i++) {
7837 target_ep[i].events = tswap32(ep[i].events);
7838 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
7839 }
7840 }
7841 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
7842 break;
7843 }
7844#endif
7845#endif
bellard31e31b82003-02-18 22:55:36 +00007846 default:
7847 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00007848 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00007849#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 +00007850 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00007851#endif
ths0da46a62007-10-20 20:23:07 +00007852 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00007853 break;
7854 }
bellard579a97f2007-11-11 14:26:47 +00007855fail:
bellardc573ff62004-01-04 15:51:36 +00007856#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00007857 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00007858#endif
thsb92c47c2007-11-01 00:07:38 +00007859 if(do_strace)
7860 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00007861 return ret;
bellard579a97f2007-11-11 14:26:47 +00007862efault:
7863 ret = -TARGET_EFAULT;
7864 goto fail;
bellard31e31b82003-02-18 22:55:36 +00007865}