blob: 1939a5f0ec8aa4960e0d6b13b8600637a1d29da8 [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>
aurel320b6d3ae2008-09-15 07:43:43 +000062#include <qemu-common.h>
Juan Quintela9788c9c2009-07-27 16:13:02 +020063#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +000064#include <sys/gmon.h>
65#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030066#ifdef CONFIG_EVENTFD
67#include <sys/eventfd.h>
68#endif
bellard31e31b82003-02-18 22:55:36 +000069
70#define termios host_termios
71#define winsize host_winsize
72#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000073#define sgttyb host_sgttyb /* same as target */
74#define tchars host_tchars /* same as target */
75#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000076
77#include <linux/termios.h>
78#include <linux/unistd.h>
79#include <linux/utsname.h>
80#include <linux/cdrom.h>
81#include <linux/hdreg.h>
82#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000083#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000084#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +030085#include <linux/fs.h>
Ulrich Hechtf7680a52009-10-16 17:00:44 +020086#include <linux/fb.h>
87#include <linux/vt.h>
pbrookd7e40362008-05-23 16:06:43 +000088#include "linux_loop.h"
Loïc Minierda790302009-12-29 22:06:13 +010089#include "cpu-uname.h"
bellard31e31b82003-02-18 22:55:36 +000090
bellard3ef693a2003-03-23 20:17:16 +000091#include "qemu.h"
balrog526ccb72008-07-16 12:13:52 +000092#include "qemu-common.h"
bellard31e31b82003-02-18 22:55:36 +000093
Juan Quintela2f7bb872009-07-27 16:13:24 +020094#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +000095#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
96 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
97#else
98/* XXX: Hardcode the above values. */
99#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +0000100#endif
101
bellard72f03902003-02-18 23:33:18 +0000102//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000103
bellard1a9353d2003-03-16 20:28:50 +0000104//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000105#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
106#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000107
bellard70a194b2003-08-11 22:20:16 +0000108
bellard70a194b2003-08-11 22:20:16 +0000109#undef _syscall0
110#undef _syscall1
111#undef _syscall2
112#undef _syscall3
113#undef _syscall4
114#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000115#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000116
bellard83fcb512006-06-14 13:37:16 +0000117#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000118static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000119{ \
120 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000121}
122
bellard83fcb512006-06-14 13:37:16 +0000123#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000124static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000125{ \
126 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000127}
128
bellard83fcb512006-06-14 13:37:16 +0000129#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000130static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000131{ \
132 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000133}
134
bellard83fcb512006-06-14 13:37:16 +0000135#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000136static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000137{ \
138 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000139}
140
bellard83fcb512006-06-14 13:37:16 +0000141#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000142static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000143{ \
144 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000145}
146
bellard83fcb512006-06-14 13:37:16 +0000147#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
148 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000149static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000150{ \
151 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000152}
bellard83fcb512006-06-14 13:37:16 +0000153
154
155#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
156 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000157static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
158 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000159{ \
160 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
161}
162
bellard70a194b2003-08-11 22:20:16 +0000163
bellard31e31b82003-02-18 22:55:36 +0000164#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000165#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000166#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000167#define __NR_sys_fchownat __NR_fchownat
balrog6a24a772008-09-20 02:23:36 +0000168#define __NR_sys_fstatat64 __NR_fstatat64
balrogac8a6552008-09-20 02:25:39 +0000169#define __NR_sys_futimesat __NR_futimesat
bellard72f03902003-02-18 23:33:18 +0000170#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000171#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000172#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000173#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000174#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000175#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000176#define __NR_sys_mknodat __NR_mknodat
aurel329d33b762009-04-08 23:07:05 +0000177#define __NR_sys_newfstatat __NR_newfstatat
ths82424832007-09-24 09:21:55 +0000178#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000179#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000180#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000181#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000182#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000183#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000184#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000185#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000186#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000187#define __NR_sys_utimensat __NR_utimensat
pbrookbd0c5662008-05-29 14:34:11 +0000188#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000189#define __NR_sys_inotify_init __NR_inotify_init
190#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
191#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000192
bellardbc51c5c2004-03-17 23:46:04 +0000193#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard9af9eaa2003-04-07 21:34:41 +0000194#define __NR__llseek __NR_lseek
195#endif
196
bellard72f03902003-02-18 23:33:18 +0000197#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000198_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000199#else
ths0da46a62007-10-20 20:23:07 +0000200/* This is a replacement for the host gettid() and must return a host
201 errno. */
bellard72f03902003-02-18 23:33:18 +0000202static int gettid(void) {
203 return -ENOSYS;
204}
205#endif
aurel323b3f24a2009-04-15 16:12:13 +0000206_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
aurel323b3f24a2009-04-15 16:12:13 +0000207#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
208_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
209#endif
210_syscall2(int, sys_getpriority, int, which, int, who);
Richard Hendersond35b2612010-06-04 12:14:10 -0700211#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000212_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
213 loff_t *, res, uint, wh);
214#endif
215_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
216_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
217#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
218_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
219#endif
220#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
221_syscall2(int,sys_tkill,int,tid,int,sig)
222#endif
223#ifdef __NR_exit_group
224_syscall1(int,exit_group,int,error_code)
225#endif
226#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
227_syscall1(int,set_tid_address,int *,tidptr)
228#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +0200229#if defined(CONFIG_USE_NPTL)
aurel323b3f24a2009-04-15 16:12:13 +0000230#if defined(TARGET_NR_futex) && defined(__NR_futex)
231_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
232 const struct timespec *,timeout,int *,uaddr2,int,val3)
233#endif
234#endif
235
236static bitmask_transtbl fcntl_flags_tbl[] = {
237 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
238 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
239 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
240 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
241 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
242 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
243 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
244 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
245 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
246 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
247 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
248 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
249 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
250#if defined(O_DIRECT)
251 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
252#endif
253 { 0, 0, 0, 0 }
254};
255
256#define COPY_UTSNAME_FIELD(dest, src) \
257 do { \
258 /* __NEW_UTS_LEN doesn't include terminating null */ \
259 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
260 (dest)[__NEW_UTS_LEN] = '\0'; \
261 } while (0)
262
263static int sys_uname(struct new_utsname *buf)
264{
265 struct utsname uts_buf;
266
267 if (uname(&uts_buf) < 0)
268 return (-1);
269
270 /*
271 * Just in case these have some differences, we
272 * translate utsname to new_utsname (which is the
273 * struct linux kernel uses).
274 */
275
276 bzero(buf, sizeof (*buf));
277 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
278 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
279 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
280 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
281 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
282#ifdef _GNU_SOURCE
283 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
284#endif
285 return (0);
286
287#undef COPY_UTSNAME_FIELD
288}
289
290static int sys_getcwd1(char *buf, size_t size)
291{
292 if (getcwd(buf, size) == NULL) {
293 /* getcwd() sets errno */
294 return (-1);
295 }
aurel32aaf4ad32009-04-16 14:17:14 +0000296 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000297}
298
299#ifdef CONFIG_ATFILE
300/*
301 * Host system seems to have atfile syscall stubs available. We
302 * now enable them one by one as specified by target syscall_nr.h.
303 */
304
305#ifdef TARGET_NR_faccessat
aurel32465c9f02009-04-19 08:52:17 +0000306static int sys_faccessat(int dirfd, const char *pathname, int mode)
aurel323b3f24a2009-04-15 16:12:13 +0000307{
aurel32465c9f02009-04-19 08:52:17 +0000308 return (faccessat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000309}
310#endif
311#ifdef TARGET_NR_fchmodat
aurel32465c9f02009-04-19 08:52:17 +0000312static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000313{
aurel32465c9f02009-04-19 08:52:17 +0000314 return (fchmodat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000315}
316#endif
aurel32fda33742009-04-15 17:12:01 +0000317#if defined(TARGET_NR_fchownat) && defined(USE_UID16)
aurel323b3f24a2009-04-15 16:12:13 +0000318static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
319 gid_t group, int flags)
320{
321 return (fchownat(dirfd, pathname, owner, group, flags));
322}
323#endif
324#ifdef __NR_fstatat64
325static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
326 int flags)
327{
328 return (fstatat(dirfd, pathname, buf, flags));
329}
330#endif
331#ifdef __NR_newfstatat
332static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
333 int flags)
334{
335 return (fstatat(dirfd, pathname, buf, flags));
336}
337#endif
338#ifdef TARGET_NR_futimesat
339static int sys_futimesat(int dirfd, const char *pathname,
340 const struct timeval times[2])
341{
342 return (futimesat(dirfd, pathname, times));
343}
344#endif
345#ifdef TARGET_NR_linkat
346static int sys_linkat(int olddirfd, const char *oldpath,
347 int newdirfd, const char *newpath, int flags)
348{
349 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
350}
351#endif
352#ifdef TARGET_NR_mkdirat
353static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
354{
355 return (mkdirat(dirfd, pathname, mode));
356}
357#endif
358#ifdef TARGET_NR_mknodat
359static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
360 dev_t dev)
361{
362 return (mknodat(dirfd, pathname, mode, dev));
363}
364#endif
365#ifdef TARGET_NR_openat
366static int sys_openat(int dirfd, const char *pathname, int flags, ...)
367{
368 /*
369 * open(2) has extra parameter 'mode' when called with
370 * flag O_CREAT.
371 */
372 if ((flags & O_CREAT) != 0) {
373 va_list ap;
374 mode_t mode;
375
376 /*
377 * Get the 'mode' parameter and translate it to
378 * host bits.
379 */
380 va_start(ap, flags);
381 mode = va_arg(ap, mode_t);
382 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
383 va_end(ap);
384
385 return (openat(dirfd, pathname, flags, mode));
386 }
387 return (openat(dirfd, pathname, flags));
388}
389#endif
390#ifdef TARGET_NR_readlinkat
391static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
392{
393 return (readlinkat(dirfd, pathname, buf, bufsiz));
394}
395#endif
396#ifdef TARGET_NR_renameat
397static int sys_renameat(int olddirfd, const char *oldpath,
398 int newdirfd, const char *newpath)
399{
400 return (renameat(olddirfd, oldpath, newdirfd, newpath));
401}
402#endif
403#ifdef TARGET_NR_symlinkat
404static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
405{
406 return (symlinkat(oldpath, newdirfd, newpath));
407}
408#endif
409#ifdef TARGET_NR_unlinkat
410static int sys_unlinkat(int dirfd, const char *pathname, int flags)
411{
412 return (unlinkat(dirfd, pathname, flags));
413}
414#endif
aurel323b3f24a2009-04-15 16:12:13 +0000415#else /* !CONFIG_ATFILE */
416
417/*
418 * Try direct syscalls instead
419 */
ths92a34c12007-09-24 09:27:49 +0000420#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
aurel32465c9f02009-04-19 08:52:17 +0000421_syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
ths92a34c12007-09-24 09:27:49 +0000422#endif
ths814d7972007-09-24 09:26:51 +0000423#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
aurel32465c9f02009-04-19 08:52:17 +0000424_syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
ths814d7972007-09-24 09:26:51 +0000425#endif
blueswir14583f582008-08-24 10:35:55 +0000426#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
thsccfa72b2007-09-24 09:23:34 +0000427_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
428 uid_t,owner,gid_t,group,int,flags)
429#endif
aurel329d33b762009-04-08 23:07:05 +0000430#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
431 defined(__NR_fstatat64)
balrog6a24a772008-09-20 02:23:36 +0000432_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
433 struct stat *,buf,int,flags)
434#endif
balrogac8a6552008-09-20 02:25:39 +0000435#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
436_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
437 const struct timeval *,times)
438#endif
aurel323b3f24a2009-04-15 16:12:13 +0000439#if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
440 defined(__NR_newfstatat)
441_syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
442 struct stat *,buf,int,flags)
blueswir18fcd3692008-08-17 20:26:25 +0000443#endif
ths64f0ce42007-09-24 09:25:06 +0000444#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
445_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
aurel323b3f24a2009-04-15 16:12:13 +0000446 int,newdirfd,const char *,newpath,int,flags)
ths64f0ce42007-09-24 09:25:06 +0000447#endif
ths4472ad02007-09-24 09:22:32 +0000448#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
449_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
450#endif
ths75ac37a2007-09-24 09:23:05 +0000451#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
452_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
453 mode_t,mode,dev_t,dev)
454#endif
ths82424832007-09-24 09:21:55 +0000455#if defined(TARGET_NR_openat) && defined(__NR_openat)
456_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
457#endif
ths5e0ccb12007-09-24 09:26:10 +0000458#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
459_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
460 char *,buf,size_t,bufsize)
461#endif
ths722183f2007-09-24 09:24:37 +0000462#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
463_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
464 int,newdirfd,const char *,newpath)
465#endif
thsb51eaa82007-09-25 16:09:22 +0000466#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000467_syscall3(int,sys_symlinkat,const char *,oldpath,
468 int,newdirfd,const char *,newpath)
469#endif
ths8170f562007-09-24 09:24:11 +0000470#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
471_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
472#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300473
474#endif /* CONFIG_ATFILE */
475
476#ifdef CONFIG_UTIMENSAT
477static int sys_utimensat(int dirfd, const char *pathname,
478 const struct timespec times[2], int flags)
479{
480 if (pathname == NULL)
481 return futimens(dirfd, times);
482 else
483 return utimensat(dirfd, pathname, times, flags);
484}
485#else
ths9007f0e2007-09-25 17:50:37 +0000486#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
487_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
488 const struct timespec *,tsp,int,flags)
489#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300490#endif /* CONFIG_UTIMENSAT */
aurel323b3f24a2009-04-15 16:12:13 +0000491
492#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000493#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000494
aurel3239b59762008-10-01 21:46:50 +0000495#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000496static int sys_inotify_init(void)
497{
498 return (inotify_init());
499}
aurel3239b59762008-10-01 21:46:50 +0000500#endif
501#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000502static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
503{
504 return (inotify_add_watch(fd, pathname, mask));
505}
aurel3239b59762008-10-01 21:46:50 +0000506#endif
507#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000508static int sys_inotify_rm_watch(int fd, int32_t wd)
509{
aurel328690e422009-04-17 13:50:32 +0000510 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000511}
aurel3239b59762008-10-01 21:46:50 +0000512#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000513#ifdef CONFIG_INOTIFY1
514#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
515static int sys_inotify_init1(int flags)
516{
517 return (inotify_init1(flags));
518}
519#endif
520#endif
aurel323b3f24a2009-04-15 16:12:13 +0000521#else
522/* Userspace can usually survive runtime without inotify */
523#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000524#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000525#undef TARGET_NR_inotify_add_watch
526#undef TARGET_NR_inotify_rm_watch
527#endif /* CONFIG_INOTIFY */
528
bellard66fb9762003-03-23 01:06:05 +0000529
530extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000531extern int flock(int, int);
532extern int setfsuid(int);
533extern int setfsgid(int);
bellard19b84f32003-05-08 15:41:49 +0000534extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000535
thsb92c47c2007-11-01 00:07:38 +0000536#define ERRNO_TABLE_SIZE 1200
537
538/* target_to_host_errno_table[] is initialized from
539 * host_to_target_errno_table[] in syscall_init(). */
540static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
541};
542
ths637947f2007-06-01 12:09:19 +0000543/*
thsfe8f0962007-07-12 10:59:21 +0000544 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000545 * minus the errnos that are not actually generic to all archs.
546 */
thsb92c47c2007-11-01 00:07:38 +0000547static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000548 [EIDRM] = TARGET_EIDRM,
549 [ECHRNG] = TARGET_ECHRNG,
550 [EL2NSYNC] = TARGET_EL2NSYNC,
551 [EL3HLT] = TARGET_EL3HLT,
552 [EL3RST] = TARGET_EL3RST,
553 [ELNRNG] = TARGET_ELNRNG,
554 [EUNATCH] = TARGET_EUNATCH,
555 [ENOCSI] = TARGET_ENOCSI,
556 [EL2HLT] = TARGET_EL2HLT,
557 [EDEADLK] = TARGET_EDEADLK,
558 [ENOLCK] = TARGET_ENOLCK,
559 [EBADE] = TARGET_EBADE,
560 [EBADR] = TARGET_EBADR,
561 [EXFULL] = TARGET_EXFULL,
562 [ENOANO] = TARGET_ENOANO,
563 [EBADRQC] = TARGET_EBADRQC,
564 [EBADSLT] = TARGET_EBADSLT,
565 [EBFONT] = TARGET_EBFONT,
566 [ENOSTR] = TARGET_ENOSTR,
567 [ENODATA] = TARGET_ENODATA,
568 [ETIME] = TARGET_ETIME,
569 [ENOSR] = TARGET_ENOSR,
570 [ENONET] = TARGET_ENONET,
571 [ENOPKG] = TARGET_ENOPKG,
572 [EREMOTE] = TARGET_EREMOTE,
573 [ENOLINK] = TARGET_ENOLINK,
574 [EADV] = TARGET_EADV,
575 [ESRMNT] = TARGET_ESRMNT,
576 [ECOMM] = TARGET_ECOMM,
577 [EPROTO] = TARGET_EPROTO,
578 [EDOTDOT] = TARGET_EDOTDOT,
579 [EMULTIHOP] = TARGET_EMULTIHOP,
580 [EBADMSG] = TARGET_EBADMSG,
581 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
582 [EOVERFLOW] = TARGET_EOVERFLOW,
583 [ENOTUNIQ] = TARGET_ENOTUNIQ,
584 [EBADFD] = TARGET_EBADFD,
585 [EREMCHG] = TARGET_EREMCHG,
586 [ELIBACC] = TARGET_ELIBACC,
587 [ELIBBAD] = TARGET_ELIBBAD,
588 [ELIBSCN] = TARGET_ELIBSCN,
589 [ELIBMAX] = TARGET_ELIBMAX,
590 [ELIBEXEC] = TARGET_ELIBEXEC,
591 [EILSEQ] = TARGET_EILSEQ,
592 [ENOSYS] = TARGET_ENOSYS,
593 [ELOOP] = TARGET_ELOOP,
594 [ERESTART] = TARGET_ERESTART,
595 [ESTRPIPE] = TARGET_ESTRPIPE,
596 [ENOTEMPTY] = TARGET_ENOTEMPTY,
597 [EUSERS] = TARGET_EUSERS,
598 [ENOTSOCK] = TARGET_ENOTSOCK,
599 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
600 [EMSGSIZE] = TARGET_EMSGSIZE,
601 [EPROTOTYPE] = TARGET_EPROTOTYPE,
602 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
603 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
604 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
605 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
606 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
607 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
608 [EADDRINUSE] = TARGET_EADDRINUSE,
609 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
610 [ENETDOWN] = TARGET_ENETDOWN,
611 [ENETUNREACH] = TARGET_ENETUNREACH,
612 [ENETRESET] = TARGET_ENETRESET,
613 [ECONNABORTED] = TARGET_ECONNABORTED,
614 [ECONNRESET] = TARGET_ECONNRESET,
615 [ENOBUFS] = TARGET_ENOBUFS,
616 [EISCONN] = TARGET_EISCONN,
617 [ENOTCONN] = TARGET_ENOTCONN,
618 [EUCLEAN] = TARGET_EUCLEAN,
619 [ENOTNAM] = TARGET_ENOTNAM,
620 [ENAVAIL] = TARGET_ENAVAIL,
621 [EISNAM] = TARGET_EISNAM,
622 [EREMOTEIO] = TARGET_EREMOTEIO,
623 [ESHUTDOWN] = TARGET_ESHUTDOWN,
624 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
625 [ETIMEDOUT] = TARGET_ETIMEDOUT,
626 [ECONNREFUSED] = TARGET_ECONNREFUSED,
627 [EHOSTDOWN] = TARGET_EHOSTDOWN,
628 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
629 [EALREADY] = TARGET_EALREADY,
630 [EINPROGRESS] = TARGET_EINPROGRESS,
631 [ESTALE] = TARGET_ESTALE,
632 [ECANCELED] = TARGET_ECANCELED,
633 [ENOMEDIUM] = TARGET_ENOMEDIUM,
634 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000635#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000636 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000637#endif
638#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000639 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000640#endif
641#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000642 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000643#endif
644#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000645 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000646#endif
647#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000648 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000649#endif
650#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000651 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000652#endif
thsb92c47c2007-11-01 00:07:38 +0000653};
ths637947f2007-06-01 12:09:19 +0000654
655static inline int host_to_target_errno(int err)
656{
657 if(host_to_target_errno_table[err])
658 return host_to_target_errno_table[err];
659 return err;
660}
661
thsb92c47c2007-11-01 00:07:38 +0000662static inline int target_to_host_errno(int err)
663{
664 if (target_to_host_errno_table[err])
665 return target_to_host_errno_table[err];
666 return err;
667}
668
blueswir1992f48a2007-10-14 16:27:31 +0000669static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000670{
671 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000672 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000673 else
674 return ret;
675}
676
blueswir1992f48a2007-10-14 16:27:31 +0000677static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000678{
blueswir1992f48a2007-10-14 16:27:31 +0000679 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000680}
681
thsb92c47c2007-11-01 00:07:38 +0000682char *target_strerror(int err)
683{
684 return strerror(target_to_host_errno(err));
685}
686
blueswir1992f48a2007-10-14 16:27:31 +0000687static abi_ulong target_brk;
688static abi_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000689
blueswir1992f48a2007-10-14 16:27:31 +0000690void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000691{
blueswir14c1de732007-07-07 20:45:44 +0000692 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000693}
694
ths0da46a62007-10-20 20:23:07 +0000695/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000696abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000697{
blueswir1992f48a2007-10-14 16:27:31 +0000698 abi_ulong brk_page;
699 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000700 int new_alloc_size;
701
702 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000703 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000704 if (new_brk < target_original_brk)
balrog7ab240a2008-04-26 12:17:34 +0000705 return target_brk;
ths3b46e622007-09-17 08:09:54 +0000706
pbrook53a59602006-03-25 19:31:22 +0000707 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000708
709 /* If the new brk is less than this, set it and we're done... */
710 if (new_brk < brk_page) {
711 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000712 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000713 }
714
715 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000716 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000717 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000718 PROT_READ|PROT_WRITE,
719 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
balrog7ab240a2008-04-26 12:17:34 +0000720
Richard Henderson7dd46c02010-05-03 10:07:49 -0700721#if defined(TARGET_ALPHA)
722 /* We (partially) emulate OSF/1 on Alpha, which requires we
723 return a proper errno, not an unchanged brk value. */
724 if (is_error(mapped_addr)) {
725 return -TARGET_ENOMEM;
726 }
727#endif
728
729 if (!is_error(mapped_addr)) {
bellard31e31b82003-02-18 22:55:36 +0000730 target_brk = new_brk;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700731 }
balrog7ab240a2008-04-26 12:17:34 +0000732 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000733}
734
ths26edcf42007-12-09 02:25:24 +0000735static inline abi_long copy_from_user_fdset(fd_set *fds,
736 abi_ulong target_fds_addr,
737 int n)
bellard31e31b82003-02-18 22:55:36 +0000738{
ths26edcf42007-12-09 02:25:24 +0000739 int i, nw, j, k;
740 abi_ulong b, *target_fds;
741
742 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
743 if (!(target_fds = lock_user(VERIFY_READ,
744 target_fds_addr,
745 sizeof(abi_ulong) * nw,
746 1)))
747 return -TARGET_EFAULT;
748
749 FD_ZERO(fds);
750 k = 0;
751 for (i = 0; i < nw; i++) {
752 /* grab the abi_ulong */
753 __get_user(b, &target_fds[i]);
754 for (j = 0; j < TARGET_ABI_BITS; j++) {
755 /* check the bit inside the abi_ulong */
756 if ((b >> j) & 1)
757 FD_SET(k, fds);
758 k++;
bellard31e31b82003-02-18 22:55:36 +0000759 }
bellard31e31b82003-02-18 22:55:36 +0000760 }
ths26edcf42007-12-09 02:25:24 +0000761
762 unlock_user(target_fds, target_fds_addr, 0);
763
764 return 0;
bellard31e31b82003-02-18 22:55:36 +0000765}
766
ths26edcf42007-12-09 02:25:24 +0000767static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
768 const fd_set *fds,
769 int n)
bellard31e31b82003-02-18 22:55:36 +0000770{
bellard31e31b82003-02-18 22:55:36 +0000771 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000772 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000773 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000774
ths26edcf42007-12-09 02:25:24 +0000775 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
776 if (!(target_fds = lock_user(VERIFY_WRITE,
777 target_fds_addr,
778 sizeof(abi_ulong) * nw,
779 0)))
780 return -TARGET_EFAULT;
781
782 k = 0;
783 for (i = 0; i < nw; i++) {
784 v = 0;
785 for (j = 0; j < TARGET_ABI_BITS; j++) {
786 v |= ((FD_ISSET(k, fds) != 0) << j);
787 k++;
bellard31e31b82003-02-18 22:55:36 +0000788 }
ths26edcf42007-12-09 02:25:24 +0000789 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000790 }
ths26edcf42007-12-09 02:25:24 +0000791
792 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
793
794 return 0;
bellard31e31b82003-02-18 22:55:36 +0000795}
796
bellardc596ed12003-07-13 17:32:31 +0000797#if defined(__alpha__)
798#define HOST_HZ 1024
799#else
800#define HOST_HZ 100
801#endif
802
blueswir1992f48a2007-10-14 16:27:31 +0000803static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000804{
805#if HOST_HZ == TARGET_HZ
806 return ticks;
807#else
808 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
809#endif
810}
811
bellard579a97f2007-11-11 14:26:47 +0000812static inline abi_long host_to_target_rusage(abi_ulong target_addr,
813 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000814{
pbrook53a59602006-03-25 19:31:22 +0000815 struct target_rusage *target_rusage;
816
bellard579a97f2007-11-11 14:26:47 +0000817 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
818 return -TARGET_EFAULT;
bellardb4091862003-05-16 15:39:34 +0000819 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
820 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
821 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
822 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
823 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
824 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
825 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
826 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
827 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
828 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
829 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
830 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
831 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
832 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
833 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
834 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
835 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
836 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000837 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000838
839 return 0;
bellardb4091862003-05-16 15:39:34 +0000840}
841
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900842static inline rlim_t target_to_host_rlim(target_ulong target_rlim)
843{
844 if (target_rlim == TARGET_RLIM_INFINITY)
845 return RLIM_INFINITY;
846 else
847 return tswapl(target_rlim);
848}
849
850static inline target_ulong host_to_target_rlim(rlim_t rlim)
851{
852 if (rlim == RLIM_INFINITY || rlim != (target_long)rlim)
853 return TARGET_RLIM_INFINITY;
854 else
855 return tswapl(rlim);
856}
857
ths788f5ec2007-12-09 02:37:05 +0000858static inline abi_long copy_from_user_timeval(struct timeval *tv,
859 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000860{
pbrook53a59602006-03-25 19:31:22 +0000861 struct target_timeval *target_tv;
862
ths788f5ec2007-12-09 02:37:05 +0000863 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000864 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000865
866 __get_user(tv->tv_sec, &target_tv->tv_sec);
867 __get_user(tv->tv_usec, &target_tv->tv_usec);
868
869 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000870
871 return 0;
bellard31e31b82003-02-18 22:55:36 +0000872}
873
ths788f5ec2007-12-09 02:37:05 +0000874static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
875 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000876{
pbrook53a59602006-03-25 19:31:22 +0000877 struct target_timeval *target_tv;
878
ths788f5ec2007-12-09 02:37:05 +0000879 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000880 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000881
882 __put_user(tv->tv_sec, &target_tv->tv_sec);
883 __put_user(tv->tv_usec, &target_tv->tv_usec);
884
885 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000886
887 return 0;
bellard31e31b82003-02-18 22:55:36 +0000888}
889
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700890#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
891#include <mqueue.h>
892
aurel3224e10032009-04-15 16:11:43 +0000893static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
894 abi_ulong target_mq_attr_addr)
895{
896 struct target_mq_attr *target_mq_attr;
897
898 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
899 target_mq_attr_addr, 1))
900 return -TARGET_EFAULT;
901
902 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
903 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
904 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
905 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
906
907 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
908
909 return 0;
910}
911
912static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
913 const struct mq_attr *attr)
914{
915 struct target_mq_attr *target_mq_attr;
916
917 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
918 target_mq_attr_addr, 0))
919 return -TARGET_EFAULT;
920
921 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
922 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
923 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
924 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
925
926 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
927
928 return 0;
929}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700930#endif
bellard31e31b82003-02-18 22:55:36 +0000931
ths0da46a62007-10-20 20:23:07 +0000932/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000933static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000934 abi_ulong rfd_addr, abi_ulong wfd_addr,
935 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000936{
937 fd_set rfds, wfds, efds;
938 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
939 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000940 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000941
ths26edcf42007-12-09 02:25:24 +0000942 if (rfd_addr) {
943 if (copy_from_user_fdset(&rfds, rfd_addr, n))
944 return -TARGET_EFAULT;
945 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000946 } else {
pbrook53a59602006-03-25 19:31:22 +0000947 rfds_ptr = NULL;
948 }
ths26edcf42007-12-09 02:25:24 +0000949 if (wfd_addr) {
950 if (copy_from_user_fdset(&wfds, wfd_addr, n))
951 return -TARGET_EFAULT;
952 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000953 } else {
pbrook53a59602006-03-25 19:31:22 +0000954 wfds_ptr = NULL;
955 }
ths26edcf42007-12-09 02:25:24 +0000956 if (efd_addr) {
957 if (copy_from_user_fdset(&efds, efd_addr, n))
958 return -TARGET_EFAULT;
959 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000960 } else {
pbrook53a59602006-03-25 19:31:22 +0000961 efds_ptr = NULL;
962 }
ths3b46e622007-09-17 08:09:54 +0000963
ths26edcf42007-12-09 02:25:24 +0000964 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000965 if (copy_from_user_timeval(&tv, target_tv_addr))
966 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000967 tv_ptr = &tv;
968 } else {
969 tv_ptr = NULL;
970 }
ths26edcf42007-12-09 02:25:24 +0000971
bellard31e31b82003-02-18 22:55:36 +0000972 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000973
ths26edcf42007-12-09 02:25:24 +0000974 if (!is_error(ret)) {
975 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
976 return -TARGET_EFAULT;
977 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
978 return -TARGET_EFAULT;
979 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
980 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000981
ths788f5ec2007-12-09 02:37:05 +0000982 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
983 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000984 }
bellard579a97f2007-11-11 14:26:47 +0000985
bellard31e31b82003-02-18 22:55:36 +0000986 return ret;
987}
988
Riku Voipio099d6b02009-05-05 12:10:04 +0300989static abi_long do_pipe2(int host_pipe[], int flags)
990{
991#ifdef CONFIG_PIPE2
992 return pipe2(host_pipe, flags);
993#else
994 return -ENOSYS;
995#endif
996}
997
Richard Hendersonfb41a662010-05-03 10:07:52 -0700998static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
999 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001000{
1001 int host_pipe[2];
1002 abi_long ret;
1003 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1004
1005 if (is_error(ret))
1006 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001007
1008 /* Several targets have special calling conventions for the original
1009 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1010 if (!is_pipe2) {
1011#if defined(TARGET_ALPHA)
1012 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1013 return host_pipe[0];
1014#elif defined(TARGET_MIPS)
1015 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1016 return host_pipe[0];
1017#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001018 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001019 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001020#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001021 }
1022
Riku Voipio099d6b02009-05-05 12:10:04 +03001023 if (put_user_s32(host_pipe[0], pipedes)
1024 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1025 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001026 return get_errno(ret);
1027}
1028
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001029static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1030 abi_ulong target_addr,
1031 socklen_t len)
1032{
1033 struct target_ip_mreqn *target_smreqn;
1034
1035 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1036 if (!target_smreqn)
1037 return -TARGET_EFAULT;
1038 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1039 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1040 if (len == sizeof(struct target_ip_mreqn))
1041 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
1042 unlock_user(target_smreqn, target_addr, 0);
1043
1044 return 0;
1045}
1046
bellard579a97f2007-11-11 14:26:47 +00001047static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1048 abi_ulong target_addr,
1049 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001050{
aurel32607175e2009-04-15 16:11:59 +00001051 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1052 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001053 struct target_sockaddr *target_saddr;
1054
bellard579a97f2007-11-11 14:26:47 +00001055 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1056 if (!target_saddr)
1057 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001058
1059 sa_family = tswap16(target_saddr->sa_family);
1060
1061 /* Oops. The caller might send a incomplete sun_path; sun_path
1062 * must be terminated by \0 (see the manual page), but
1063 * unfortunately it is quite common to specify sockaddr_un
1064 * length as "strlen(x->sun_path)" while it should be
1065 * "strlen(...) + 1". We'll fix that here if needed.
1066 * Linux kernel has a similar feature.
1067 */
1068
1069 if (sa_family == AF_UNIX) {
1070 if (len < unix_maxlen && len > 0) {
1071 char *cp = (char*)target_saddr;
1072
1073 if ( cp[len-1] && !cp[len] )
1074 len++;
1075 }
1076 if (len > unix_maxlen)
1077 len = unix_maxlen;
1078 }
1079
pbrook53a59602006-03-25 19:31:22 +00001080 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001081 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001082 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001083
1084 return 0;
bellard7854b052003-03-29 17:22:23 +00001085}
1086
bellard579a97f2007-11-11 14:26:47 +00001087static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1088 struct sockaddr *addr,
1089 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001090{
pbrook53a59602006-03-25 19:31:22 +00001091 struct target_sockaddr *target_saddr;
1092
bellard579a97f2007-11-11 14:26:47 +00001093 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1094 if (!target_saddr)
1095 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001096 memcpy(target_saddr, addr, len);
1097 target_saddr->sa_family = tswap16(addr->sa_family);
1098 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001099
1100 return 0;
bellard7854b052003-03-29 17:22:23 +00001101}
1102
pbrook53a59602006-03-25 19:31:22 +00001103/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001104static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1105 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001106{
1107 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001108 abi_long msg_controllen;
1109 abi_ulong target_cmsg_addr;
1110 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001111 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001112
1113 msg_controllen = tswapl(target_msgh->msg_controllen);
1114 if (msg_controllen < sizeof (struct target_cmsghdr))
1115 goto the_end;
1116 target_cmsg_addr = tswapl(target_msgh->msg_control);
1117 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1118 if (!target_cmsg)
1119 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001120
1121 while (cmsg && target_cmsg) {
1122 void *data = CMSG_DATA(cmsg);
1123 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1124
ths5fafdf22007-09-16 21:08:06 +00001125 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001126 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1127
1128 space += CMSG_SPACE(len);
1129 if (space > msgh->msg_controllen) {
1130 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001131 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001132 break;
1133 }
1134
1135 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1136 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1137 cmsg->cmsg_len = CMSG_LEN(len);
1138
bellard3532fa72006-06-24 15:06:03 +00001139 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001140 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1141 memcpy(data, target_data, len);
1142 } else {
1143 int *fd = (int *)data;
1144 int *target_fd = (int *)target_data;
1145 int i, numfds = len / sizeof(int);
1146
1147 for (i = 0; i < numfds; i++)
1148 fd[i] = tswap32(target_fd[i]);
1149 }
1150
1151 cmsg = CMSG_NXTHDR(msgh, cmsg);
1152 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1153 }
bellard5a4a8982007-11-11 17:39:18 +00001154 unlock_user(target_cmsg, target_cmsg_addr, 0);
1155 the_end:
bellard7854b052003-03-29 17:22:23 +00001156 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001157 return 0;
bellard7854b052003-03-29 17:22:23 +00001158}
1159
pbrook53a59602006-03-25 19:31:22 +00001160/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001161static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1162 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001163{
1164 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001165 abi_long msg_controllen;
1166 abi_ulong target_cmsg_addr;
1167 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001168 socklen_t space = 0;
1169
bellard5a4a8982007-11-11 17:39:18 +00001170 msg_controllen = tswapl(target_msgh->msg_controllen);
1171 if (msg_controllen < sizeof (struct target_cmsghdr))
1172 goto the_end;
1173 target_cmsg_addr = tswapl(target_msgh->msg_control);
1174 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1175 if (!target_cmsg)
1176 return -TARGET_EFAULT;
1177
bellard7854b052003-03-29 17:22:23 +00001178 while (cmsg && target_cmsg) {
1179 void *data = CMSG_DATA(cmsg);
1180 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1181
1182 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1183
1184 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001185 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001186 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001187 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001188 break;
1189 }
1190
1191 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1192 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1193 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1194
bellard3532fa72006-06-24 15:06:03 +00001195 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001196 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1197 memcpy(target_data, data, len);
1198 } else {
1199 int *fd = (int *)data;
1200 int *target_fd = (int *)target_data;
1201 int i, numfds = len / sizeof(int);
1202
1203 for (i = 0; i < numfds; i++)
1204 target_fd[i] = tswap32(fd[i]);
1205 }
1206
1207 cmsg = CMSG_NXTHDR(msgh, cmsg);
1208 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1209 }
bellard5a4a8982007-11-11 17:39:18 +00001210 unlock_user(target_cmsg, target_cmsg_addr, space);
1211 the_end:
1212 target_msgh->msg_controllen = tswapl(space);
1213 return 0;
bellard7854b052003-03-29 17:22:23 +00001214}
1215
ths0da46a62007-10-20 20:23:07 +00001216/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001217static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001218 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001219{
blueswir1992f48a2007-10-14 16:27:31 +00001220 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001221 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001222 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001223 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001224
bellard8853f862004-02-22 14:57:26 +00001225 switch(level) {
1226 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001227 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001228 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001229 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001230
bellard2f619692007-11-16 10:46:05 +00001231 if (get_user_u32(val, optval_addr))
1232 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001233 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1234 break;
1235 case SOL_IP:
1236 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001237 case IP_TOS:
1238 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001239 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001240 case IP_ROUTER_ALERT:
1241 case IP_RECVOPTS:
1242 case IP_RETOPTS:
1243 case IP_PKTINFO:
1244 case IP_MTU_DISCOVER:
1245 case IP_RECVERR:
1246 case IP_RECVTOS:
1247#ifdef IP_FREEBIND
1248 case IP_FREEBIND:
1249#endif
1250 case IP_MULTICAST_TTL:
1251 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001252 val = 0;
1253 if (optlen >= sizeof(uint32_t)) {
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 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001257 if (get_user_u8(val, optval_addr))
1258 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001259 }
1260 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1261 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001262 case IP_ADD_MEMBERSHIP:
1263 case IP_DROP_MEMBERSHIP:
1264 if (optlen < sizeof (struct target_ip_mreq) ||
1265 optlen > sizeof (struct target_ip_mreqn))
1266 return -TARGET_EINVAL;
1267
1268 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1269 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1270 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1271 break;
1272
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001273 case IP_BLOCK_SOURCE:
1274 case IP_UNBLOCK_SOURCE:
1275 case IP_ADD_SOURCE_MEMBERSHIP:
1276 case IP_DROP_SOURCE_MEMBERSHIP:
1277 if (optlen != sizeof (struct target_ip_mreq_source))
1278 return -TARGET_EINVAL;
1279
1280 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1281 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1282 unlock_user (ip_mreq_source, optval_addr, 0);
1283 break;
1284
bellard8853f862004-02-22 14:57:26 +00001285 default:
1286 goto unimplemented;
1287 }
1288 break;
bellard3532fa72006-06-24 15:06:03 +00001289 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001290 switch (optname) {
1291 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001292 case TARGET_SO_DEBUG:
1293 optname = SO_DEBUG;
1294 break;
1295 case TARGET_SO_REUSEADDR:
1296 optname = SO_REUSEADDR;
1297 break;
1298 case TARGET_SO_TYPE:
1299 optname = SO_TYPE;
1300 break;
1301 case TARGET_SO_ERROR:
1302 optname = SO_ERROR;
1303 break;
1304 case TARGET_SO_DONTROUTE:
1305 optname = SO_DONTROUTE;
1306 break;
1307 case TARGET_SO_BROADCAST:
1308 optname = SO_BROADCAST;
1309 break;
1310 case TARGET_SO_SNDBUF:
1311 optname = SO_SNDBUF;
1312 break;
1313 case TARGET_SO_RCVBUF:
1314 optname = SO_RCVBUF;
1315 break;
1316 case TARGET_SO_KEEPALIVE:
1317 optname = SO_KEEPALIVE;
1318 break;
1319 case TARGET_SO_OOBINLINE:
1320 optname = SO_OOBINLINE;
1321 break;
1322 case TARGET_SO_NO_CHECK:
1323 optname = SO_NO_CHECK;
1324 break;
1325 case TARGET_SO_PRIORITY:
1326 optname = SO_PRIORITY;
1327 break;
bellard5e83e8e2005-03-01 22:32:06 +00001328#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001329 case TARGET_SO_BSDCOMPAT:
1330 optname = SO_BSDCOMPAT;
1331 break;
bellard5e83e8e2005-03-01 22:32:06 +00001332#endif
bellard3532fa72006-06-24 15:06:03 +00001333 case TARGET_SO_PASSCRED:
1334 optname = SO_PASSCRED;
1335 break;
1336 case TARGET_SO_TIMESTAMP:
1337 optname = SO_TIMESTAMP;
1338 break;
1339 case TARGET_SO_RCVLOWAT:
1340 optname = SO_RCVLOWAT;
1341 break;
1342 case TARGET_SO_RCVTIMEO:
1343 optname = SO_RCVTIMEO;
1344 break;
1345 case TARGET_SO_SNDTIMEO:
1346 optname = SO_SNDTIMEO;
1347 break;
bellard8853f862004-02-22 14:57:26 +00001348 break;
1349 default:
1350 goto unimplemented;
1351 }
bellard3532fa72006-06-24 15:06:03 +00001352 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001353 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001354
bellard2f619692007-11-16 10:46:05 +00001355 if (get_user_u32(val, optval_addr))
1356 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001357 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001358 break;
bellard7854b052003-03-29 17:22:23 +00001359 default:
bellard8853f862004-02-22 14:57:26 +00001360 unimplemented:
1361 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001362 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001363 }
bellard8853f862004-02-22 14:57:26 +00001364 return ret;
bellard7854b052003-03-29 17:22:23 +00001365}
1366
ths0da46a62007-10-20 20:23:07 +00001367/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001368static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001369 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001370{
blueswir1992f48a2007-10-14 16:27:31 +00001371 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001372 int len, val;
1373 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001374
1375 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001376 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001377 level = SOL_SOCKET;
1378 switch (optname) {
1379 /* These don't just return a single integer */
1380 case TARGET_SO_LINGER:
1381 case TARGET_SO_RCVTIMEO:
1382 case TARGET_SO_SNDTIMEO:
1383 case TARGET_SO_PEERCRED:
1384 case TARGET_SO_PEERNAME:
1385 goto unimplemented;
1386 /* Options with 'int' argument. */
1387 case TARGET_SO_DEBUG:
1388 optname = SO_DEBUG;
1389 goto int_case;
1390 case TARGET_SO_REUSEADDR:
1391 optname = SO_REUSEADDR;
1392 goto int_case;
1393 case TARGET_SO_TYPE:
1394 optname = SO_TYPE;
1395 goto int_case;
1396 case TARGET_SO_ERROR:
1397 optname = SO_ERROR;
1398 goto int_case;
1399 case TARGET_SO_DONTROUTE:
1400 optname = SO_DONTROUTE;
1401 goto int_case;
1402 case TARGET_SO_BROADCAST:
1403 optname = SO_BROADCAST;
1404 goto int_case;
1405 case TARGET_SO_SNDBUF:
1406 optname = SO_SNDBUF;
1407 goto int_case;
1408 case TARGET_SO_RCVBUF:
1409 optname = SO_RCVBUF;
1410 goto int_case;
1411 case TARGET_SO_KEEPALIVE:
1412 optname = SO_KEEPALIVE;
1413 goto int_case;
1414 case TARGET_SO_OOBINLINE:
1415 optname = SO_OOBINLINE;
1416 goto int_case;
1417 case TARGET_SO_NO_CHECK:
1418 optname = SO_NO_CHECK;
1419 goto int_case;
1420 case TARGET_SO_PRIORITY:
1421 optname = SO_PRIORITY;
1422 goto int_case;
1423#ifdef SO_BSDCOMPAT
1424 case TARGET_SO_BSDCOMPAT:
1425 optname = SO_BSDCOMPAT;
1426 goto int_case;
1427#endif
1428 case TARGET_SO_PASSCRED:
1429 optname = SO_PASSCRED;
1430 goto int_case;
1431 case TARGET_SO_TIMESTAMP:
1432 optname = SO_TIMESTAMP;
1433 goto int_case;
1434 case TARGET_SO_RCVLOWAT:
1435 optname = SO_RCVLOWAT;
1436 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001437 default:
bellard2efbe912005-07-23 15:10:20 +00001438 goto int_case;
1439 }
1440 break;
1441 case SOL_TCP:
1442 /* TCP options all take an 'int' value. */
1443 int_case:
bellard2f619692007-11-16 10:46:05 +00001444 if (get_user_u32(len, optlen))
1445 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001446 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001447 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +00001448 lv = sizeof(int);
1449 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1450 if (ret < 0)
1451 return ret;
bellard2efbe912005-07-23 15:10:20 +00001452 if (len > lv)
1453 len = lv;
bellard2f619692007-11-16 10:46:05 +00001454 if (len == 4) {
1455 if (put_user_u32(val, optval_addr))
1456 return -TARGET_EFAULT;
1457 } else {
1458 if (put_user_u8(val, optval_addr))
1459 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001460 }
bellard2f619692007-11-16 10:46:05 +00001461 if (put_user_u32(len, optlen))
1462 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001463 break;
1464 case SOL_IP:
1465 switch(optname) {
1466 case IP_TOS:
1467 case IP_TTL:
1468 case IP_HDRINCL:
1469 case IP_ROUTER_ALERT:
1470 case IP_RECVOPTS:
1471 case IP_RETOPTS:
1472 case IP_PKTINFO:
1473 case IP_MTU_DISCOVER:
1474 case IP_RECVERR:
1475 case IP_RECVTOS:
1476#ifdef IP_FREEBIND
1477 case IP_FREEBIND:
1478#endif
1479 case IP_MULTICAST_TTL:
1480 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001481 if (get_user_u32(len, optlen))
1482 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001483 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001484 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +00001485 lv = sizeof(int);
1486 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1487 if (ret < 0)
1488 return ret;
bellard2efbe912005-07-23 15:10:20 +00001489 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001490 len = 1;
bellard2f619692007-11-16 10:46:05 +00001491 if (put_user_u32(len, optlen)
1492 || put_user_u8(val, optval_addr))
1493 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001494 } else {
bellard2efbe912005-07-23 15:10:20 +00001495 if (len > sizeof(int))
1496 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001497 if (put_user_u32(len, optlen)
1498 || put_user_u32(val, optval_addr))
1499 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001500 }
bellard8853f862004-02-22 14:57:26 +00001501 break;
bellard2efbe912005-07-23 15:10:20 +00001502 default:
thsc02f4992007-12-18 02:39:59 +00001503 ret = -TARGET_ENOPROTOOPT;
1504 break;
bellard8853f862004-02-22 14:57:26 +00001505 }
1506 break;
1507 default:
1508 unimplemented:
1509 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1510 level, optname);
thsc02f4992007-12-18 02:39:59 +00001511 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001512 break;
1513 }
1514 return ret;
bellard7854b052003-03-29 17:22:23 +00001515}
1516
bellard579a97f2007-11-11 14:26:47 +00001517/* FIXME
1518 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1519 * other lock functions have a return code of 0 for failure.
1520 */
1521static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1522 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001523{
1524 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001525 abi_ulong base;
balrogd732dcb2008-10-28 10:21:03 +00001526 int i;
pbrook53a59602006-03-25 19:31:22 +00001527
bellard579a97f2007-11-11 14:26:47 +00001528 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1529 if (!target_vec)
1530 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001531 for(i = 0;i < count; i++) {
1532 base = tswapl(target_vec[i].iov_base);
1533 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001534 if (vec[i].iov_len != 0) {
1535 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrogd732dcb2008-10-28 10:21:03 +00001536 /* Don't check lock_user return value. We must call writev even
1537 if a element has invalid base address. */
bellard41df8412008-02-04 22:26:57 +00001538 } else {
1539 /* zero length pointer is ignored */
1540 vec[i].iov_base = NULL;
1541 }
pbrook53a59602006-03-25 19:31:22 +00001542 }
1543 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001544 return 0;
pbrook53a59602006-03-25 19:31:22 +00001545}
1546
bellard579a97f2007-11-11 14:26:47 +00001547static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1548 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001549{
1550 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001551 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001552 int i;
1553
bellard579a97f2007-11-11 14:26:47 +00001554 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1555 if (!target_vec)
1556 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001557 for(i = 0;i < count; i++) {
balrogd732dcb2008-10-28 10:21:03 +00001558 if (target_vec[i].iov_base) {
1559 base = tswapl(target_vec[i].iov_base);
1560 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1561 }
pbrook53a59602006-03-25 19:31:22 +00001562 }
1563 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001564
1565 return 0;
pbrook53a59602006-03-25 19:31:22 +00001566}
1567
ths0da46a62007-10-20 20:23:07 +00001568/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001569static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001570{
1571#if defined(TARGET_MIPS)
1572 switch(type) {
1573 case TARGET_SOCK_DGRAM:
1574 type = SOCK_DGRAM;
1575 break;
1576 case TARGET_SOCK_STREAM:
1577 type = SOCK_STREAM;
1578 break;
1579 case TARGET_SOCK_RAW:
1580 type = SOCK_RAW;
1581 break;
1582 case TARGET_SOCK_RDM:
1583 type = SOCK_RDM;
1584 break;
1585 case TARGET_SOCK_SEQPACKET:
1586 type = SOCK_SEQPACKET;
1587 break;
1588 case TARGET_SOCK_PACKET:
1589 type = SOCK_PACKET;
1590 break;
1591 }
1592#endif
balrog12bc92a2007-10-30 21:06:14 +00001593 if (domain == PF_NETLINK)
1594 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001595 return get_errno(socket(domain, type, protocol));
1596}
1597
ths0da46a62007-10-20 20:23:07 +00001598/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001599static abi_long do_bind(int sockfd, abi_ulong target_addr,
1600 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001601{
aurel328f7aeaf2009-01-30 19:47:57 +00001602 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001603 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001604
Blue Swirl38724252010-09-18 05:53:14 +00001605 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001606 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001607 }
aurel328f7aeaf2009-01-30 19:47:57 +00001608
aurel32607175e2009-04-15 16:11:59 +00001609 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001610
Arnaud Patard917507b2009-06-19 10:44:45 +03001611 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1612 if (ret)
1613 return ret;
1614
bellard3532fa72006-06-24 15:06:03 +00001615 return get_errno(bind(sockfd, addr, addrlen));
1616}
1617
ths0da46a62007-10-20 20:23:07 +00001618/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001619static abi_long do_connect(int sockfd, abi_ulong target_addr,
1620 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001621{
aurel328f7aeaf2009-01-30 19:47:57 +00001622 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001623 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001624
Blue Swirl38724252010-09-18 05:53:14 +00001625 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001626 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001627 }
aurel328f7aeaf2009-01-30 19:47:57 +00001628
1629 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001630
Arnaud Patard917507b2009-06-19 10:44:45 +03001631 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1632 if (ret)
1633 return ret;
1634
bellard3532fa72006-06-24 15:06:03 +00001635 return get_errno(connect(sockfd, addr, addrlen));
1636}
1637
ths0da46a62007-10-20 20:23:07 +00001638/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001639static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1640 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001641{
balrog6de645c2008-10-28 10:26:29 +00001642 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001643 struct target_msghdr *msgp;
1644 struct msghdr msg;
1645 int count;
1646 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001647 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001648
bellard579a97f2007-11-11 14:26:47 +00001649 /* FIXME */
1650 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1651 msgp,
1652 target_msg,
1653 send ? 1 : 0))
1654 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001655 if (msgp->msg_name) {
1656 msg.msg_namelen = tswap32(msgp->msg_namelen);
1657 msg.msg_name = alloca(msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001658 ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001659 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001660 if (ret) {
1661 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1662 return ret;
1663 }
bellard3532fa72006-06-24 15:06:03 +00001664 } else {
1665 msg.msg_name = NULL;
1666 msg.msg_namelen = 0;
1667 }
1668 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1669 msg.msg_control = alloca(msg.msg_controllen);
1670 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001671
bellard3532fa72006-06-24 15:06:03 +00001672 count = tswapl(msgp->msg_iovlen);
1673 vec = alloca(count * sizeof(struct iovec));
1674 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001675 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001676 msg.msg_iovlen = count;
1677 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001678
bellard3532fa72006-06-24 15:06:03 +00001679 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001680 ret = target_to_host_cmsg(&msg, msgp);
1681 if (ret == 0)
1682 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001683 } else {
1684 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001685 if (!is_error(ret)) {
1686 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001687 ret = host_to_target_cmsg(msgp, &msg);
balrog6de645c2008-10-28 10:26:29 +00001688 if (!is_error(ret))
1689 ret = len;
1690 }
bellard3532fa72006-06-24 15:06:03 +00001691 }
1692 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001693 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001694 return ret;
1695}
1696
ths0da46a62007-10-20 20:23:07 +00001697/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001698static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001699 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001700{
bellard2f619692007-11-16 10:46:05 +00001701 socklen_t addrlen;
1702 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001703 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001704
Arnaud Patard917507b2009-06-19 10:44:45 +03001705 if (target_addr == 0)
1706 return get_errno(accept(fd, NULL, NULL));
1707
1708 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00001709 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03001710 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00001711
Blue Swirl38724252010-09-18 05:53:14 +00001712 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001713 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001714 }
aurel328f7aeaf2009-01-30 19:47:57 +00001715
Arnaud Patard917507b2009-06-19 10:44:45 +03001716 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1717 return -TARGET_EINVAL;
1718
bellard2f619692007-11-16 10:46:05 +00001719 addr = alloca(addrlen);
1720
pbrook1be9e1d2006-11-19 15:26:04 +00001721 ret = get_errno(accept(fd, addr, &addrlen));
1722 if (!is_error(ret)) {
1723 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001724 if (put_user_u32(addrlen, target_addrlen_addr))
1725 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001726 }
1727 return ret;
1728}
1729
ths0da46a62007-10-20 20:23:07 +00001730/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001731static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001732 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001733{
bellard2f619692007-11-16 10:46:05 +00001734 socklen_t addrlen;
1735 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001736 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001737
bellard2f619692007-11-16 10:46:05 +00001738 if (get_user_u32(addrlen, target_addrlen_addr))
1739 return -TARGET_EFAULT;
1740
Blue Swirl38724252010-09-18 05:53:14 +00001741 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001742 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001743 }
aurel328f7aeaf2009-01-30 19:47:57 +00001744
Arnaud Patard917507b2009-06-19 10:44:45 +03001745 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1746 return -TARGET_EFAULT;
1747
bellard2f619692007-11-16 10:46:05 +00001748 addr = alloca(addrlen);
1749
pbrook1be9e1d2006-11-19 15:26:04 +00001750 ret = get_errno(getpeername(fd, addr, &addrlen));
1751 if (!is_error(ret)) {
1752 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001753 if (put_user_u32(addrlen, target_addrlen_addr))
1754 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001755 }
1756 return ret;
1757}
1758
ths0da46a62007-10-20 20:23:07 +00001759/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001760static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001761 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001762{
bellard2f619692007-11-16 10:46:05 +00001763 socklen_t addrlen;
1764 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001765 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001766
bellard2f619692007-11-16 10:46:05 +00001767 if (get_user_u32(addrlen, target_addrlen_addr))
1768 return -TARGET_EFAULT;
1769
Blue Swirl38724252010-09-18 05:53:14 +00001770 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001771 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001772 }
aurel328f7aeaf2009-01-30 19:47:57 +00001773
Arnaud Patard917507b2009-06-19 10:44:45 +03001774 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1775 return -TARGET_EFAULT;
1776
bellard2f619692007-11-16 10:46:05 +00001777 addr = alloca(addrlen);
1778
pbrook1be9e1d2006-11-19 15:26:04 +00001779 ret = get_errno(getsockname(fd, addr, &addrlen));
1780 if (!is_error(ret)) {
1781 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001782 if (put_user_u32(addrlen, target_addrlen_addr))
1783 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001784 }
1785 return ret;
1786}
1787
ths0da46a62007-10-20 20:23:07 +00001788/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001789static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001790 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001791{
1792 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001793 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001794
1795 ret = get_errno(socketpair(domain, type, protocol, tab));
1796 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001797 if (put_user_s32(tab[0], target_tab_addr)
1798 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1799 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001800 }
1801 return ret;
1802}
1803
ths0da46a62007-10-20 20:23:07 +00001804/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001805static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1806 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001807{
1808 void *addr;
1809 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001810 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001811
Blue Swirl38724252010-09-18 05:53:14 +00001812 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001813 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001814 }
aurel328f7aeaf2009-01-30 19:47:57 +00001815
bellard579a97f2007-11-11 14:26:47 +00001816 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1817 if (!host_msg)
1818 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001819 if (target_addr) {
1820 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001821 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1822 if (ret) {
1823 unlock_user(host_msg, msg, 0);
1824 return ret;
1825 }
pbrook1be9e1d2006-11-19 15:26:04 +00001826 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1827 } else {
1828 ret = get_errno(send(fd, host_msg, len, flags));
1829 }
1830 unlock_user(host_msg, msg, 0);
1831 return ret;
1832}
1833
ths0da46a62007-10-20 20:23:07 +00001834/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001835static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1836 abi_ulong target_addr,
1837 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001838{
1839 socklen_t addrlen;
1840 void *addr;
1841 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001842 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001843
bellard579a97f2007-11-11 14:26:47 +00001844 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1845 if (!host_msg)
1846 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001847 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001848 if (get_user_u32(addrlen, target_addrlen)) {
1849 ret = -TARGET_EFAULT;
1850 goto fail;
1851 }
Blue Swirl38724252010-09-18 05:53:14 +00001852 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001853 ret = -TARGET_EINVAL;
1854 goto fail;
1855 }
pbrook1be9e1d2006-11-19 15:26:04 +00001856 addr = alloca(addrlen);
1857 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1858 } else {
1859 addr = NULL; /* To keep compiler quiet. */
1860 ret = get_errno(recv(fd, host_msg, len, flags));
1861 }
1862 if (!is_error(ret)) {
1863 if (target_addr) {
1864 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001865 if (put_user_u32(addrlen, target_addrlen)) {
1866 ret = -TARGET_EFAULT;
1867 goto fail;
1868 }
pbrook1be9e1d2006-11-19 15:26:04 +00001869 }
1870 unlock_user(host_msg, msg, len);
1871 } else {
bellard2f619692007-11-16 10:46:05 +00001872fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001873 unlock_user(host_msg, msg, 0);
1874 }
1875 return ret;
1876}
1877
j_mayer32407102007-09-26 23:01:49 +00001878#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001879/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001880static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001881{
blueswir1992f48a2007-10-14 16:27:31 +00001882 abi_long ret;
1883 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001884
1885 switch(num) {
1886 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001887 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001888 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001889
Ulrich Hecht98818182009-07-03 17:09:28 +02001890 if (get_user_ual(domain, vptr)
1891 || get_user_ual(type, vptr + n)
1892 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001893 return -TARGET_EFAULT;
1894
bellard3532fa72006-06-24 15:06:03 +00001895 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001896 }
bellard31e31b82003-02-18 22:55:36 +00001897 break;
1898 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001899 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001900 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001901 abi_ulong target_addr;
1902 socklen_t addrlen;
1903
Ulrich Hecht98818182009-07-03 17:09:28 +02001904 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001905 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001906 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001907 return -TARGET_EFAULT;
1908
bellard3532fa72006-06-24 15:06:03 +00001909 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001910 }
bellard31e31b82003-02-18 22:55:36 +00001911 break;
1912 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001913 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001914 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001915 abi_ulong target_addr;
1916 socklen_t addrlen;
1917
Ulrich Hecht98818182009-07-03 17:09:28 +02001918 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001919 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001920 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001921 return -TARGET_EFAULT;
1922
bellard3532fa72006-06-24 15:06:03 +00001923 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001924 }
bellard31e31b82003-02-18 22:55:36 +00001925 break;
1926 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001927 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001928 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00001929
Ulrich Hecht98818182009-07-03 17:09:28 +02001930 if (get_user_ual(sockfd, vptr)
1931 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00001932 return -TARGET_EFAULT;
1933
bellard7854b052003-03-29 17:22:23 +00001934 ret = get_errno(listen(sockfd, backlog));
1935 }
bellard31e31b82003-02-18 22:55:36 +00001936 break;
1937 case SOCKOP_accept:
1938 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001939 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001940 abi_ulong target_addr, target_addrlen;
1941
Ulrich Hecht98818182009-07-03 17:09:28 +02001942 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001943 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001944 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001945 return -TARGET_EFAULT;
1946
pbrook1be9e1d2006-11-19 15:26:04 +00001947 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001948 }
1949 break;
1950 case SOCKOP_getsockname:
1951 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001952 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001953 abi_ulong target_addr, target_addrlen;
1954
Ulrich Hecht98818182009-07-03 17:09:28 +02001955 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001956 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001957 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001958 return -TARGET_EFAULT;
1959
pbrook1be9e1d2006-11-19 15:26:04 +00001960 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001961 }
1962 break;
1963 case SOCKOP_getpeername:
1964 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001965 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001966 abi_ulong target_addr, target_addrlen;
1967
Ulrich Hecht98818182009-07-03 17:09:28 +02001968 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001969 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001970 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001971 return -TARGET_EFAULT;
1972
pbrook1be9e1d2006-11-19 15:26:04 +00001973 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001974 }
1975 break;
1976 case SOCKOP_socketpair:
1977 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001978 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001979 abi_ulong tab;
1980
Ulrich Hecht98818182009-07-03 17:09:28 +02001981 if (get_user_ual(domain, vptr)
1982 || get_user_ual(type, vptr + n)
1983 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00001984 || get_user_ual(tab, vptr + 3 * n))
1985 return -TARGET_EFAULT;
1986
pbrook1be9e1d2006-11-19 15:26:04 +00001987 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001988 }
1989 break;
1990 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001991 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001992 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001993 abi_ulong msg;
1994 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001995 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001996
Ulrich Hecht98818182009-07-03 17:09:28 +02001997 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001998 || get_user_ual(msg, vptr + n)
1999 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002000 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002001 return -TARGET_EFAULT;
2002
pbrook1be9e1d2006-11-19 15:26:04 +00002003 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002004 }
bellard31e31b82003-02-18 22:55:36 +00002005 break;
2006 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00002007 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002008 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002009 abi_ulong msg;
2010 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002011 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002012
Ulrich Hecht98818182009-07-03 17:09:28 +02002013 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002014 || get_user_ual(msg, vptr + n)
2015 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002016 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002017 return -TARGET_EFAULT;
2018
pbrook1be9e1d2006-11-19 15:26:04 +00002019 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002020 }
bellard31e31b82003-02-18 22:55:36 +00002021 break;
2022 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00002023 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002024 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002025 abi_ulong msg;
2026 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002027 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002028 abi_ulong addr;
2029 socklen_t addrlen;
2030
Ulrich Hecht98818182009-07-03 17:09:28 +02002031 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002032 || get_user_ual(msg, vptr + n)
2033 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002034 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002035 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002036 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002037 return -TARGET_EFAULT;
2038
pbrook1be9e1d2006-11-19 15:26:04 +00002039 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002040 }
bellard31e31b82003-02-18 22:55:36 +00002041 break;
2042 case SOCKOP_recvfrom:
2043 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002044 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002045 abi_ulong msg;
2046 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002047 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002048 abi_ulong addr;
2049 socklen_t addrlen;
2050
Ulrich Hecht98818182009-07-03 17:09:28 +02002051 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002052 || get_user_ual(msg, vptr + n)
2053 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002054 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002055 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002056 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002057 return -TARGET_EFAULT;
2058
pbrook1be9e1d2006-11-19 15:26:04 +00002059 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00002060 }
2061 break;
2062 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00002063 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002064 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00002065
Ulrich Hecht98818182009-07-03 17:09:28 +02002066 if (get_user_ual(sockfd, vptr)
2067 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002068 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002069
2070 ret = get_errno(shutdown(sockfd, how));
2071 }
bellard31e31b82003-02-18 22:55:36 +00002072 break;
2073 case SOCKOP_sendmsg:
2074 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002075 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002076 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002077 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002078 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002079
Ulrich Hecht98818182009-07-03 17:09:28 +02002080 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002081 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002082 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002083 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002084
ths5fafdf22007-09-16 21:08:06 +00002085 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002086 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002087 }
2088 break;
bellard31e31b82003-02-18 22:55:36 +00002089 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002090 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002091 abi_ulong sockfd;
2092 abi_ulong level;
2093 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002094 abi_ulong optval;
2095 socklen_t optlen;
2096
Ulrich Hecht98818182009-07-03 17:09:28 +02002097 if (get_user_ual(sockfd, vptr)
2098 || get_user_ual(level, vptr + n)
2099 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002100 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002101 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002102 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002103
2104 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2105 }
2106 break;
bellard31e31b82003-02-18 22:55:36 +00002107 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002108 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002109 abi_ulong sockfd;
2110 abi_ulong level;
2111 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002112 abi_ulong optval;
2113 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002114
Ulrich Hecht98818182009-07-03 17:09:28 +02002115 if (get_user_ual(sockfd, vptr)
2116 || get_user_ual(level, vptr + n)
2117 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002118 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002119 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002120 return -TARGET_EFAULT;
2121
2122 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002123 }
2124 break;
bellard31e31b82003-02-18 22:55:36 +00002125 default:
2126 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002127 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002128 break;
2129 }
2130 return ret;
2131}
j_mayer32407102007-09-26 23:01:49 +00002132#endif
bellard31e31b82003-02-18 22:55:36 +00002133
bellard8853f862004-02-22 14:57:26 +00002134#define N_SHM_REGIONS 32
2135
2136static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002137 abi_ulong start;
2138 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002139} shm_regions[N_SHM_REGIONS];
2140
ths3eb6b042007-06-03 14:26:27 +00002141struct target_ipc_perm
2142{
blueswir1992f48a2007-10-14 16:27:31 +00002143 abi_long __key;
2144 abi_ulong uid;
2145 abi_ulong gid;
2146 abi_ulong cuid;
2147 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002148 unsigned short int mode;
2149 unsigned short int __pad1;
2150 unsigned short int __seq;
2151 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002152 abi_ulong __unused1;
2153 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002154};
2155
2156struct target_semid_ds
2157{
2158 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002159 abi_ulong sem_otime;
2160 abi_ulong __unused1;
2161 abi_ulong sem_ctime;
2162 abi_ulong __unused2;
2163 abi_ulong sem_nsems;
2164 abi_ulong __unused3;
2165 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002166};
2167
bellard579a97f2007-11-11 14:26:47 +00002168static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2169 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002170{
2171 struct target_ipc_perm *target_ip;
2172 struct target_semid_ds *target_sd;
2173
bellard579a97f2007-11-11 14:26:47 +00002174 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2175 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002176 target_ip = &(target_sd->sem_perm);
ths3eb6b042007-06-03 14:26:27 +00002177 host_ip->__key = tswapl(target_ip->__key);
2178 host_ip->uid = tswapl(target_ip->uid);
2179 host_ip->gid = tswapl(target_ip->gid);
2180 host_ip->cuid = tswapl(target_ip->cuid);
2181 host_ip->cgid = tswapl(target_ip->cgid);
2182 host_ip->mode = tswapl(target_ip->mode);
2183 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002184 return 0;
ths3eb6b042007-06-03 14:26:27 +00002185}
2186
bellard579a97f2007-11-11 14:26:47 +00002187static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2188 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002189{
2190 struct target_ipc_perm *target_ip;
2191 struct target_semid_ds *target_sd;
2192
bellard579a97f2007-11-11 14:26:47 +00002193 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2194 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002195 target_ip = &(target_sd->sem_perm);
2196 target_ip->__key = tswapl(host_ip->__key);
2197 target_ip->uid = tswapl(host_ip->uid);
2198 target_ip->gid = tswapl(host_ip->gid);
2199 target_ip->cuid = tswapl(host_ip->cuid);
2200 target_ip->cgid = tswapl(host_ip->cgid);
2201 target_ip->mode = tswapl(host_ip->mode);
2202 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002203 return 0;
ths3eb6b042007-06-03 14:26:27 +00002204}
2205
bellard579a97f2007-11-11 14:26:47 +00002206static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2207 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002208{
2209 struct target_semid_ds *target_sd;
2210
bellard579a97f2007-11-11 14:26:47 +00002211 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2212 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002213 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2214 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002215 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2216 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2217 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2218 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002219 return 0;
ths3eb6b042007-06-03 14:26:27 +00002220}
2221
bellard579a97f2007-11-11 14:26:47 +00002222static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2223 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002224{
2225 struct target_semid_ds *target_sd;
2226
bellard579a97f2007-11-11 14:26:47 +00002227 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2228 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002229 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2230 return -TARGET_EFAULT;;
ths3eb6b042007-06-03 14:26:27 +00002231 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2232 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2233 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2234 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002235 return 0;
ths3eb6b042007-06-03 14:26:27 +00002236}
2237
aurel32e5289082009-04-18 16:16:12 +00002238struct target_seminfo {
2239 int semmap;
2240 int semmni;
2241 int semmns;
2242 int semmnu;
2243 int semmsl;
2244 int semopm;
2245 int semume;
2246 int semusz;
2247 int semvmx;
2248 int semaem;
2249};
2250
2251static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2252 struct seminfo *host_seminfo)
2253{
2254 struct target_seminfo *target_seminfo;
2255 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2256 return -TARGET_EFAULT;
2257 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2258 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2259 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2260 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2261 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2262 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2263 __put_user(host_seminfo->semume, &target_seminfo->semume);
2264 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2265 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2266 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2267 unlock_user_struct(target_seminfo, target_addr, 1);
2268 return 0;
2269}
2270
thsfa294812007-02-02 22:05:00 +00002271union semun {
2272 int val;
ths3eb6b042007-06-03 14:26:27 +00002273 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002274 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002275 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002276};
2277
ths3eb6b042007-06-03 14:26:27 +00002278union target_semun {
2279 int val;
aurel32e5289082009-04-18 16:16:12 +00002280 abi_ulong buf;
2281 abi_ulong array;
2282 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002283};
2284
aurel32e5289082009-04-18 16:16:12 +00002285static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2286 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002287{
aurel32e5289082009-04-18 16:16:12 +00002288 int nsems;
2289 unsigned short *array;
2290 union semun semun;
2291 struct semid_ds semid_ds;
2292 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002293
aurel32e5289082009-04-18 16:16:12 +00002294 semun.buf = &semid_ds;
2295
2296 ret = semctl(semid, 0, IPC_STAT, semun);
2297 if (ret == -1)
2298 return get_errno(ret);
2299
2300 nsems = semid_ds.sem_nsems;
2301
2302 *host_array = malloc(nsems*sizeof(unsigned short));
2303 array = lock_user(VERIFY_READ, target_addr,
2304 nsems*sizeof(unsigned short), 1);
2305 if (!array)
2306 return -TARGET_EFAULT;
2307
2308 for(i=0; i<nsems; i++) {
2309 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002310 }
aurel32e5289082009-04-18 16:16:12 +00002311 unlock_user(array, target_addr, 0);
2312
bellard579a97f2007-11-11 14:26:47 +00002313 return 0;
ths3eb6b042007-06-03 14:26:27 +00002314}
2315
aurel32e5289082009-04-18 16:16:12 +00002316static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2317 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002318{
aurel32e5289082009-04-18 16:16:12 +00002319 int nsems;
2320 unsigned short *array;
2321 union semun semun;
2322 struct semid_ds semid_ds;
2323 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002324
aurel32e5289082009-04-18 16:16:12 +00002325 semun.buf = &semid_ds;
2326
2327 ret = semctl(semid, 0, IPC_STAT, semun);
2328 if (ret == -1)
2329 return get_errno(ret);
2330
2331 nsems = semid_ds.sem_nsems;
2332
2333 array = lock_user(VERIFY_WRITE, target_addr,
2334 nsems*sizeof(unsigned short), 0);
2335 if (!array)
2336 return -TARGET_EFAULT;
2337
2338 for(i=0; i<nsems; i++) {
2339 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002340 }
aurel32e5289082009-04-18 16:16:12 +00002341 free(*host_array);
2342 unlock_user(array, target_addr, 1);
2343
bellard579a97f2007-11-11 14:26:47 +00002344 return 0;
ths3eb6b042007-06-03 14:26:27 +00002345}
2346
aurel32e5289082009-04-18 16:16:12 +00002347static inline abi_long do_semctl(int semid, int semnum, int cmd,
2348 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002349{
2350 union semun arg;
2351 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302352 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002353 struct seminfo seminfo;
2354 abi_long ret = -TARGET_EINVAL;
2355 abi_long err;
2356 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002357
2358 switch( cmd ) {
2359 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002360 case SETVAL:
aurel32e5289082009-04-18 16:16:12 +00002361 arg.val = tswapl(target_su.val);
2362 ret = get_errno(semctl(semid, semnum, cmd, arg));
2363 target_su.val = tswapl(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002364 break;
2365 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002366 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002367 err = target_to_host_semarray(semid, &array, target_su.array);
2368 if (err)
2369 return err;
2370 arg.array = array;
2371 ret = get_errno(semctl(semid, semnum, cmd, arg));
2372 err = host_to_target_semarray(semid, target_su.array, &array);
2373 if (err)
2374 return err;
ths3eb6b042007-06-03 14:26:27 +00002375 break;
2376 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002377 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002378 case SEM_STAT:
2379 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2380 if (err)
2381 return err;
2382 arg.buf = &dsarg;
2383 ret = get_errno(semctl(semid, semnum, cmd, arg));
2384 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2385 if (err)
2386 return err;
ths3eb6b042007-06-03 14:26:27 +00002387 break;
aurel32e5289082009-04-18 16:16:12 +00002388 case IPC_INFO:
2389 case SEM_INFO:
2390 arg.__buf = &seminfo;
2391 ret = get_errno(semctl(semid, semnum, cmd, arg));
2392 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2393 if (err)
2394 return err;
2395 break;
2396 case IPC_RMID:
2397 case GETPID:
2398 case GETNCNT:
2399 case GETZCNT:
2400 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2401 break;
ths3eb6b042007-06-03 14:26:27 +00002402 }
2403
2404 return ret;
2405}
2406
aurel32e5289082009-04-18 16:16:12 +00002407struct target_sembuf {
2408 unsigned short sem_num;
2409 short sem_op;
2410 short sem_flg;
2411};
2412
2413static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2414 abi_ulong target_addr,
2415 unsigned nsops)
2416{
2417 struct target_sembuf *target_sembuf;
2418 int i;
2419
2420 target_sembuf = lock_user(VERIFY_READ, target_addr,
2421 nsops*sizeof(struct target_sembuf), 1);
2422 if (!target_sembuf)
2423 return -TARGET_EFAULT;
2424
2425 for(i=0; i<nsops; i++) {
2426 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2427 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2428 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2429 }
2430
2431 unlock_user(target_sembuf, target_addr, 0);
2432
2433 return 0;
2434}
2435
2436static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2437{
2438 struct sembuf sops[nsops];
2439
2440 if (target_to_host_sembuf(sops, ptr, nsops))
2441 return -TARGET_EFAULT;
2442
2443 return semop(semid, sops, nsops);
2444}
2445
ths1bc012f2007-06-03 14:27:49 +00002446struct target_msqid_ds
2447{
aurel321c54ff92008-10-13 21:08:44 +00002448 struct target_ipc_perm msg_perm;
2449 abi_ulong msg_stime;
2450#if TARGET_ABI_BITS == 32
2451 abi_ulong __unused1;
2452#endif
2453 abi_ulong msg_rtime;
2454#if TARGET_ABI_BITS == 32
2455 abi_ulong __unused2;
2456#endif
2457 abi_ulong msg_ctime;
2458#if TARGET_ABI_BITS == 32
2459 abi_ulong __unused3;
2460#endif
2461 abi_ulong __msg_cbytes;
2462 abi_ulong msg_qnum;
2463 abi_ulong msg_qbytes;
2464 abi_ulong msg_lspid;
2465 abi_ulong msg_lrpid;
2466 abi_ulong __unused4;
2467 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002468};
2469
bellard579a97f2007-11-11 14:26:47 +00002470static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2471 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002472{
2473 struct target_msqid_ds *target_md;
2474
bellard579a97f2007-11-11 14:26:47 +00002475 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2476 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002477 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2478 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002479 host_md->msg_stime = tswapl(target_md->msg_stime);
2480 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2481 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2482 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2483 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2484 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2485 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2486 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2487 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002488 return 0;
ths1bc012f2007-06-03 14:27:49 +00002489}
2490
bellard579a97f2007-11-11 14:26:47 +00002491static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2492 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002493{
2494 struct target_msqid_ds *target_md;
2495
bellard579a97f2007-11-11 14:26:47 +00002496 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2497 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002498 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2499 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002500 target_md->msg_stime = tswapl(host_md->msg_stime);
2501 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2502 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2503 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2504 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2505 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2506 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2507 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2508 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002509 return 0;
ths1bc012f2007-06-03 14:27:49 +00002510}
2511
aurel321c54ff92008-10-13 21:08:44 +00002512struct target_msginfo {
2513 int msgpool;
2514 int msgmap;
2515 int msgmax;
2516 int msgmnb;
2517 int msgmni;
2518 int msgssz;
2519 int msgtql;
2520 unsigned short int msgseg;
2521};
2522
2523static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2524 struct msginfo *host_msginfo)
2525{
2526 struct target_msginfo *target_msginfo;
2527 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2528 return -TARGET_EFAULT;
2529 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2530 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2531 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2532 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2533 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2534 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2535 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2536 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2537 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002538 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002539}
2540
2541static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002542{
2543 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002544 struct msginfo msginfo;
2545 abi_long ret = -TARGET_EINVAL;
2546
2547 cmd &= 0xff;
2548
2549 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002550 case IPC_STAT:
2551 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002552 case MSG_STAT:
2553 if (target_to_host_msqid_ds(&dsarg,ptr))
2554 return -TARGET_EFAULT;
2555 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2556 if (host_to_target_msqid_ds(ptr,&dsarg))
2557 return -TARGET_EFAULT;
2558 break;
2559 case IPC_RMID:
2560 ret = get_errno(msgctl(msgid, cmd, NULL));
2561 break;
2562 case IPC_INFO:
2563 case MSG_INFO:
2564 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2565 if (host_to_target_msginfo(ptr, &msginfo))
2566 return -TARGET_EFAULT;
2567 break;
ths1bc012f2007-06-03 14:27:49 +00002568 }
aurel321c54ff92008-10-13 21:08:44 +00002569
ths1bc012f2007-06-03 14:27:49 +00002570 return ret;
2571}
2572
2573struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002574 abi_long mtype;
2575 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002576};
2577
blueswir1992f48a2007-10-14 16:27:31 +00002578static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2579 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002580{
2581 struct target_msgbuf *target_mb;
2582 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002583 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002584
bellard579a97f2007-11-11 14:26:47 +00002585 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2586 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002587 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002588 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2589 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002590 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2591 free(host_mb);
2592 unlock_user_struct(target_mb, msgp, 0);
2593
2594 return ret;
2595}
2596
blueswir1992f48a2007-10-14 16:27:31 +00002597static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002598 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002599 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002600{
2601 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002602 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002603 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002604 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002605
bellard579a97f2007-11-11 14:26:47 +00002606 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2607 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002608
ths1bc012f2007-06-03 14:27:49 +00002609 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002610 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2611
bellard579a97f2007-11-11 14:26:47 +00002612 if (ret > 0) {
2613 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2614 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2615 if (!target_mtext) {
2616 ret = -TARGET_EFAULT;
2617 goto end;
2618 }
aurel321c54ff92008-10-13 21:08:44 +00002619 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002620 unlock_user(target_mtext, target_mtext_addr, ret);
2621 }
aurel321c54ff92008-10-13 21:08:44 +00002622
ths1bc012f2007-06-03 14:27:49 +00002623 target_mb->mtype = tswapl(host_mb->mtype);
2624 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002625
bellard579a97f2007-11-11 14:26:47 +00002626end:
2627 if (target_mb)
2628 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00002629 return ret;
2630}
2631
Riku Voipio88a8c982009-04-03 10:42:00 +03002632struct target_shmid_ds
2633{
2634 struct target_ipc_perm shm_perm;
2635 abi_ulong shm_segsz;
2636 abi_ulong shm_atime;
2637#if TARGET_ABI_BITS == 32
2638 abi_ulong __unused1;
2639#endif
2640 abi_ulong shm_dtime;
2641#if TARGET_ABI_BITS == 32
2642 abi_ulong __unused2;
2643#endif
2644 abi_ulong shm_ctime;
2645#if TARGET_ABI_BITS == 32
2646 abi_ulong __unused3;
2647#endif
2648 int shm_cpid;
2649 int shm_lpid;
2650 abi_ulong shm_nattch;
2651 unsigned long int __unused4;
2652 unsigned long int __unused5;
2653};
2654
2655static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2656 abi_ulong target_addr)
2657{
2658 struct target_shmid_ds *target_sd;
2659
2660 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2661 return -TARGET_EFAULT;
2662 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2663 return -TARGET_EFAULT;
2664 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2665 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2666 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2667 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2668 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2669 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2670 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2671 unlock_user_struct(target_sd, target_addr, 0);
2672 return 0;
2673}
2674
2675static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2676 struct shmid_ds *host_sd)
2677{
2678 struct target_shmid_ds *target_sd;
2679
2680 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2681 return -TARGET_EFAULT;
2682 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2683 return -TARGET_EFAULT;
2684 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2685 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2686 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2687 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2688 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2689 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2690 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2691 unlock_user_struct(target_sd, target_addr, 1);
2692 return 0;
2693}
2694
2695struct target_shminfo {
2696 abi_ulong shmmax;
2697 abi_ulong shmmin;
2698 abi_ulong shmmni;
2699 abi_ulong shmseg;
2700 abi_ulong shmall;
2701};
2702
2703static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2704 struct shminfo *host_shminfo)
2705{
2706 struct target_shminfo *target_shminfo;
2707 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2708 return -TARGET_EFAULT;
2709 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2710 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2711 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2712 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2713 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2714 unlock_user_struct(target_shminfo, target_addr, 1);
2715 return 0;
2716}
2717
2718struct target_shm_info {
2719 int used_ids;
2720 abi_ulong shm_tot;
2721 abi_ulong shm_rss;
2722 abi_ulong shm_swp;
2723 abi_ulong swap_attempts;
2724 abi_ulong swap_successes;
2725};
2726
2727static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2728 struct shm_info *host_shm_info)
2729{
2730 struct target_shm_info *target_shm_info;
2731 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2732 return -TARGET_EFAULT;
2733 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2734 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2735 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2736 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2737 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2738 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2739 unlock_user_struct(target_shm_info, target_addr, 1);
2740 return 0;
2741}
2742
2743static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2744{
2745 struct shmid_ds dsarg;
2746 struct shminfo shminfo;
2747 struct shm_info shm_info;
2748 abi_long ret = -TARGET_EINVAL;
2749
2750 cmd &= 0xff;
2751
2752 switch(cmd) {
2753 case IPC_STAT:
2754 case IPC_SET:
2755 case SHM_STAT:
2756 if (target_to_host_shmid_ds(&dsarg, buf))
2757 return -TARGET_EFAULT;
2758 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2759 if (host_to_target_shmid_ds(buf, &dsarg))
2760 return -TARGET_EFAULT;
2761 break;
2762 case IPC_INFO:
2763 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2764 if (host_to_target_shminfo(buf, &shminfo))
2765 return -TARGET_EFAULT;
2766 break;
2767 case SHM_INFO:
2768 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2769 if (host_to_target_shm_info(buf, &shm_info))
2770 return -TARGET_EFAULT;
2771 break;
2772 case IPC_RMID:
2773 case SHM_LOCK:
2774 case SHM_UNLOCK:
2775 ret = get_errno(shmctl(shmid, cmd, NULL));
2776 break;
2777 }
2778
2779 return ret;
2780}
2781
2782static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2783{
2784 abi_long raddr;
2785 void *host_raddr;
2786 struct shmid_ds shm_info;
2787 int i,ret;
2788
2789 /* find out the length of the shared memory segment */
2790 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2791 if (is_error(ret)) {
2792 /* can't get length, bail out */
2793 return ret;
2794 }
2795
2796 mmap_lock();
2797
2798 if (shmaddr)
2799 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2800 else {
2801 abi_ulong mmap_start;
2802
2803 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2804
2805 if (mmap_start == -1) {
2806 errno = ENOMEM;
2807 host_raddr = (void *)-1;
2808 } else
2809 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2810 }
2811
2812 if (host_raddr == (void *)-1) {
2813 mmap_unlock();
2814 return get_errno((long)host_raddr);
2815 }
2816 raddr=h2g((unsigned long)host_raddr);
2817
2818 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2819 PAGE_VALID | PAGE_READ |
2820 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2821
2822 for (i = 0; i < N_SHM_REGIONS; i++) {
2823 if (shm_regions[i].start == 0) {
2824 shm_regions[i].start = raddr;
2825 shm_regions[i].size = shm_info.shm_segsz;
2826 break;
2827 }
2828 }
2829
2830 mmap_unlock();
2831 return raddr;
2832
2833}
2834
2835static inline abi_long do_shmdt(abi_ulong shmaddr)
2836{
2837 int i;
2838
2839 for (i = 0; i < N_SHM_REGIONS; ++i) {
2840 if (shm_regions[i].start == shmaddr) {
2841 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09002842 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03002843 break;
2844 }
2845 }
2846
2847 return get_errno(shmdt(g2h(shmaddr)));
2848}
2849
aurel321c54ff92008-10-13 21:08:44 +00002850#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00002851/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00002852/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002853static abi_long do_ipc(unsigned int call, int first,
2854 int second, int third,
2855 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00002856{
2857 int version;
blueswir1992f48a2007-10-14 16:27:31 +00002858 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00002859
2860 version = call >> 16;
2861 call &= 0xffff;
2862
2863 switch (call) {
thsfa294812007-02-02 22:05:00 +00002864 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00002865 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00002866 break;
2867
2868 case IPCOP_semget:
2869 ret = get_errno(semget(first, second, third));
2870 break;
2871
2872 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00002873 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00002874 break;
thsd96372e2007-02-02 22:05:44 +00002875
aurel321c54ff92008-10-13 21:08:44 +00002876 case IPCOP_msgget:
2877 ret = get_errno(msgget(first, second));
2878 break;
thsd96372e2007-02-02 22:05:44 +00002879
aurel321c54ff92008-10-13 21:08:44 +00002880 case IPCOP_msgsnd:
2881 ret = do_msgsnd(first, ptr, second, third);
2882 break;
thsd96372e2007-02-02 22:05:44 +00002883
aurel321c54ff92008-10-13 21:08:44 +00002884 case IPCOP_msgctl:
2885 ret = do_msgctl(first, second, ptr);
2886 break;
thsd96372e2007-02-02 22:05:44 +00002887
aurel321c54ff92008-10-13 21:08:44 +00002888 case IPCOP_msgrcv:
2889 switch (version) {
2890 case 0:
2891 {
2892 struct target_ipc_kludge {
2893 abi_long msgp;
2894 abi_long msgtyp;
2895 } *tmp;
thsd96372e2007-02-02 22:05:44 +00002896
aurel321c54ff92008-10-13 21:08:44 +00002897 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2898 ret = -TARGET_EFAULT;
2899 break;
ths1bc012f2007-06-03 14:27:49 +00002900 }
aurel321c54ff92008-10-13 21:08:44 +00002901
2902 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2903
2904 unlock_user_struct(tmp, ptr, 0);
2905 break;
2906 }
2907 default:
2908 ret = do_msgrcv(first, ptr, second, fifth, third);
2909 }
2910 break;
thsd96372e2007-02-02 22:05:44 +00002911
bellard8853f862004-02-22 14:57:26 +00002912 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03002913 switch (version) {
2914 default:
bellard5a4a8982007-11-11 17:39:18 +00002915 {
2916 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03002917 raddr = do_shmat(first, ptr, second);
2918 if (is_error(raddr))
2919 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00002920 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002921 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03002922 break;
2923 }
2924 case 1:
2925 ret = -TARGET_EINVAL;
2926 break;
bellard5a4a8982007-11-11 17:39:18 +00002927 }
bellard8853f862004-02-22 14:57:26 +00002928 break;
2929 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03002930 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00002931 break;
2932
2933 case IPCOP_shmget:
2934 /* IPC_* flag values are the same on all linux platforms */
2935 ret = get_errno(shmget(first, second, third));
2936 break;
2937
2938 /* IPC_* and SHM_* command values are the same on all linux platforms */
2939 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03002940 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00002941 break;
2942 default:
j_mayer32407102007-09-26 23:01:49 +00002943 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002944 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002945 break;
2946 }
2947 return ret;
2948}
j_mayer32407102007-09-26 23:01:49 +00002949#endif
bellard8853f862004-02-22 14:57:26 +00002950
bellard31e31b82003-02-18 22:55:36 +00002951/* kernel structure types definitions */
2952#define IFNAMSIZ 16
2953
Blue Swirl001faf32009-05-13 17:53:17 +00002954#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00002955#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2956enum {
2957#include "syscall_types.h"
2958};
2959#undef STRUCT
2960#undef STRUCT_SPECIAL
2961
Blue Swirl001faf32009-05-13 17:53:17 +00002962#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00002963#define STRUCT_SPECIAL(name)
2964#include "syscall_types.h"
2965#undef STRUCT
2966#undef STRUCT_SPECIAL
2967
2968typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002969 unsigned int target_cmd;
2970 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002971 const char *name;
2972 int access;
bellard1a9353d2003-03-16 20:28:50 +00002973 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002974} IOCTLEntry;
2975
2976#define IOC_R 0x0001
2977#define IOC_W 0x0002
2978#define IOC_RW (IOC_R | IOC_W)
2979
2980#define MAX_STRUCT_SIZE 4096
2981
blueswir19f106a72008-10-05 10:52:52 +00002982static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00002983#define IOCTL(cmd, access, ...) \
2984 { TARGET_ ## cmd, cmd, #cmd, access, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00002985#include "ioctls.h"
2986 { 0, 0, },
2987};
2988
pbrook53a59602006-03-25 19:31:22 +00002989/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002990/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002991static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002992{
2993 const IOCTLEntry *ie;
2994 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002995 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002996 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002997 int target_size;
2998 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002999
3000 ie = ioctl_entries;
3001 for(;;) {
3002 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003003 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003004 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003005 }
3006 if (ie->target_cmd == cmd)
3007 break;
3008 ie++;
3009 }
3010 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003011#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003012 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003013#endif
bellard31e31b82003-02-18 22:55:36 +00003014 switch(arg_type[0]) {
3015 case TYPE_NULL:
3016 /* no argument */
3017 ret = get_errno(ioctl(fd, ie->host_cmd));
3018 break;
3019 case TYPE_PTRVOID:
3020 case TYPE_INT:
3021 /* int argment */
3022 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3023 break;
3024 case TYPE_PTR:
3025 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00003026 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00003027 switch(ie->access) {
3028 case IOC_R:
3029 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3030 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003031 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3032 if (!argptr)
3033 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003034 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3035 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003036 }
3037 break;
3038 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00003039 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3040 if (!argptr)
3041 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003042 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3043 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003044 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3045 break;
3046 default:
3047 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00003048 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3049 if (!argptr)
3050 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003051 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3052 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003053 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3054 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003055 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3056 if (!argptr)
3057 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003058 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3059 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003060 }
3061 break;
3062 }
3063 break;
3064 default:
j_mayer32407102007-09-26 23:01:49 +00003065 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3066 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003067 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003068 break;
3069 }
3070 return ret;
3071}
3072
blueswir1b39bc502008-10-05 10:51:10 +00003073static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003074 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3075 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3076 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3077 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3078 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3079 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3080 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3081 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3082 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3083 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3084 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3085 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3086 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3087 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3088 { 0, 0, 0, 0 }
3089};
3090
blueswir1b39bc502008-10-05 10:51:10 +00003091static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003092 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3093 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3094 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3095 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3096 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3097 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3098 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3099 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3100 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3101 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3102 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3103 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3104 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3105 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3106 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3107 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3108 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3109 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3110 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3111 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3112 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3113 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3114 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3115 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3116 { 0, 0, 0, 0 }
3117};
3118
blueswir1b39bc502008-10-05 10:51:10 +00003119static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003120 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3121 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3122 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3123 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3124 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3125 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3126 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3127 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3128 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3129 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3130 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3131 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3132 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3133 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3134 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3135 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3136 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3137 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3138 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3139 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3140 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3141 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3142 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3143 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3144 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3145 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3146 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3147 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3148 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3149 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3150 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3151 { 0, 0, 0, 0 }
3152};
3153
blueswir1b39bc502008-10-05 10:51:10 +00003154static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003155 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3156 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3157 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3158 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3159 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3160 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3161 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3162 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3163 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3164 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3165 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3166 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3167 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3168 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3169 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3170 { 0, 0, 0, 0 }
3171};
3172
3173static void target_to_host_termios (void *dst, const void *src)
3174{
3175 struct host_termios *host = dst;
3176 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003177
ths5fafdf22007-09-16 21:08:06 +00003178 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003179 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003180 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003181 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003182 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003183 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003184 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003185 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3186 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003187
Arnaud Patard44607122009-04-21 17:39:08 +03003188 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003189 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3190 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003191 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003192 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003193 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003194 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003195 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003196 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003197 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003198 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3199 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003200 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3201 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3202 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3203 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3204 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003205 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003206}
ths3b46e622007-09-17 08:09:54 +00003207
bellard31e31b82003-02-18 22:55:36 +00003208static void host_to_target_termios (void *dst, const void *src)
3209{
3210 struct target_termios *target = dst;
3211 const struct host_termios *host = src;
3212
ths5fafdf22007-09-16 21:08:06 +00003213 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003214 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003215 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003216 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003217 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003218 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003219 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003220 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3221 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003222
Arnaud Patard44607122009-04-21 17:39:08 +03003223 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003224 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3225 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3226 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3227 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3228 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3229 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3230 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3231 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3232 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3233 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3234 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3235 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3236 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3237 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3238 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3239 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3240 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3241}
3242
blueswir18e853dc2008-10-05 10:49:32 +00003243static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003244 .convert = { host_to_target_termios, target_to_host_termios },
3245 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3246 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3247};
3248
bellard5286db72003-06-05 00:57:30 +00003249static bitmask_transtbl mmap_flags_tbl[] = {
3250 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3251 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3252 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3253 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3254 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3255 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3256 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3257 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3258 { 0, 0, 0, 0 }
3259};
3260
bellard2ab83ea2003-06-15 19:56:46 +00003261#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003262
3263/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003264static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003265
bellard03acab62007-11-11 14:57:14 +00003266static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003267{
3268 int size;
pbrook53a59602006-03-25 19:31:22 +00003269 void *p;
bellard6dbad632003-03-16 18:05:05 +00003270
3271 if (!ldt_table)
3272 return 0;
3273 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3274 if (size > bytecount)
3275 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003276 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3277 if (!p)
bellard03acab62007-11-11 14:57:14 +00003278 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003279 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003280 memcpy(p, ldt_table, size);
3281 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003282 return size;
3283}
3284
3285/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003286static abi_long write_ldt(CPUX86State *env,
3287 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003288{
3289 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003290 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003291 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003292 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003293 uint32_t *lp, entry_1, entry_2;
3294
3295 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003296 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003297 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003298 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003299 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3300 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3301 ldt_info.limit = tswap32(target_ldt_info->limit);
3302 ldt_info.flags = tswap32(target_ldt_info->flags);
3303 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003304
bellard6dbad632003-03-16 18:05:05 +00003305 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003306 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003307 seg_32bit = ldt_info.flags & 1;
3308 contents = (ldt_info.flags >> 1) & 3;
3309 read_exec_only = (ldt_info.flags >> 3) & 1;
3310 limit_in_pages = (ldt_info.flags >> 4) & 1;
3311 seg_not_present = (ldt_info.flags >> 5) & 1;
3312 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003313#ifdef TARGET_ABI32
3314 lm = 0;
3315#else
3316 lm = (ldt_info.flags >> 7) & 1;
3317#endif
bellard6dbad632003-03-16 18:05:05 +00003318 if (contents == 3) {
3319 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003320 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003321 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003322 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003323 }
3324 /* allocate the LDT */
3325 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003326 env->ldt.base = target_mmap(0,
3327 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3328 PROT_READ|PROT_WRITE,
3329 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3330 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003331 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003332 memset(g2h(env->ldt.base), 0,
3333 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003334 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003335 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003336 }
3337
3338 /* NOTE: same code as Linux kernel */
3339 /* Allow LDTs to be cleared by the user. */
3340 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3341 if (oldmode ||
3342 (contents == 0 &&
3343 read_exec_only == 1 &&
3344 seg_32bit == 0 &&
3345 limit_in_pages == 0 &&
3346 seg_not_present == 1 &&
3347 useable == 0 )) {
3348 entry_1 = 0;
3349 entry_2 = 0;
3350 goto install;
3351 }
3352 }
ths3b46e622007-09-17 08:09:54 +00003353
bellard6dbad632003-03-16 18:05:05 +00003354 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3355 (ldt_info.limit & 0x0ffff);
3356 entry_2 = (ldt_info.base_addr & 0xff000000) |
3357 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3358 (ldt_info.limit & 0xf0000) |
3359 ((read_exec_only ^ 1) << 9) |
3360 (contents << 10) |
3361 ((seg_not_present ^ 1) << 15) |
3362 (seg_32bit << 22) |
3363 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00003364 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00003365 0x7000;
3366 if (!oldmode)
3367 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00003368
bellard6dbad632003-03-16 18:05:05 +00003369 /* Install the new entry ... */
3370install:
3371 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3372 lp[0] = tswap32(entry_1);
3373 lp[1] = tswap32(entry_2);
3374 return 0;
3375}
3376
3377/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00003378static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3379 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003380{
bellard03acab62007-11-11 14:57:14 +00003381 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00003382
bellard6dbad632003-03-16 18:05:05 +00003383 switch (func) {
3384 case 0:
3385 ret = read_ldt(ptr, bytecount);
3386 break;
3387 case 1:
3388 ret = write_ldt(env, ptr, bytecount, 1);
3389 break;
3390 case 0x11:
3391 ret = write_ldt(env, ptr, bytecount, 0);
3392 break;
bellard03acab62007-11-11 14:57:14 +00003393 default:
3394 ret = -TARGET_ENOSYS;
3395 break;
bellard6dbad632003-03-16 18:05:05 +00003396 }
3397 return ret;
3398}
bellard1b6b0292003-03-22 17:31:38 +00003399
blueswir14583f582008-08-24 10:35:55 +00003400#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00003401static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003402{
3403 uint64_t *gdt_table = g2h(env->gdt.base);
3404 struct target_modify_ldt_ldt_s ldt_info;
3405 struct target_modify_ldt_ldt_s *target_ldt_info;
3406 int seg_32bit, contents, read_exec_only, limit_in_pages;
3407 int seg_not_present, useable, lm;
3408 uint32_t *lp, entry_1, entry_2;
3409 int i;
3410
3411 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3412 if (!target_ldt_info)
3413 return -TARGET_EFAULT;
3414 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3415 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3416 ldt_info.limit = tswap32(target_ldt_info->limit);
3417 ldt_info.flags = tswap32(target_ldt_info->flags);
3418 if (ldt_info.entry_number == -1) {
3419 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3420 if (gdt_table[i] == 0) {
3421 ldt_info.entry_number = i;
3422 target_ldt_info->entry_number = tswap32(i);
3423 break;
3424 }
3425 }
3426 }
3427 unlock_user_struct(target_ldt_info, ptr, 1);
3428
3429 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3430 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3431 return -TARGET_EINVAL;
3432 seg_32bit = ldt_info.flags & 1;
3433 contents = (ldt_info.flags >> 1) & 3;
3434 read_exec_only = (ldt_info.flags >> 3) & 1;
3435 limit_in_pages = (ldt_info.flags >> 4) & 1;
3436 seg_not_present = (ldt_info.flags >> 5) & 1;
3437 useable = (ldt_info.flags >> 6) & 1;
3438#ifdef TARGET_ABI32
3439 lm = 0;
3440#else
3441 lm = (ldt_info.flags >> 7) & 1;
3442#endif
3443
3444 if (contents == 3) {
3445 if (seg_not_present == 0)
3446 return -TARGET_EINVAL;
3447 }
3448
3449 /* NOTE: same code as Linux kernel */
3450 /* Allow LDTs to be cleared by the user. */
3451 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3452 if ((contents == 0 &&
3453 read_exec_only == 1 &&
3454 seg_32bit == 0 &&
3455 limit_in_pages == 0 &&
3456 seg_not_present == 1 &&
3457 useable == 0 )) {
3458 entry_1 = 0;
3459 entry_2 = 0;
3460 goto install;
3461 }
3462 }
3463
3464 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3465 (ldt_info.limit & 0x0ffff);
3466 entry_2 = (ldt_info.base_addr & 0xff000000) |
3467 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3468 (ldt_info.limit & 0xf0000) |
3469 ((read_exec_only ^ 1) << 9) |
3470 (contents << 10) |
3471 ((seg_not_present ^ 1) << 15) |
3472 (seg_32bit << 22) |
3473 (limit_in_pages << 23) |
3474 (useable << 20) |
3475 (lm << 21) |
3476 0x7000;
3477
3478 /* Install the new entry ... */
3479install:
3480 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3481 lp[0] = tswap32(entry_1);
3482 lp[1] = tswap32(entry_2);
3483 return 0;
3484}
3485
blueswir18fcd3692008-08-17 20:26:25 +00003486static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003487{
3488 struct target_modify_ldt_ldt_s *target_ldt_info;
3489 uint64_t *gdt_table = g2h(env->gdt.base);
3490 uint32_t base_addr, limit, flags;
3491 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3492 int seg_not_present, useable, lm;
3493 uint32_t *lp, entry_1, entry_2;
3494
3495 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3496 if (!target_ldt_info)
3497 return -TARGET_EFAULT;
3498 idx = tswap32(target_ldt_info->entry_number);
3499 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3500 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3501 unlock_user_struct(target_ldt_info, ptr, 1);
3502 return -TARGET_EINVAL;
3503 }
3504 lp = (uint32_t *)(gdt_table + idx);
3505 entry_1 = tswap32(lp[0]);
3506 entry_2 = tswap32(lp[1]);
3507
3508 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3509 contents = (entry_2 >> 10) & 3;
3510 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3511 seg_32bit = (entry_2 >> 22) & 1;
3512 limit_in_pages = (entry_2 >> 23) & 1;
3513 useable = (entry_2 >> 20) & 1;
3514#ifdef TARGET_ABI32
3515 lm = 0;
3516#else
3517 lm = (entry_2 >> 21) & 1;
3518#endif
3519 flags = (seg_32bit << 0) | (contents << 1) |
3520 (read_exec_only << 3) | (limit_in_pages << 4) |
3521 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3522 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3523 base_addr = (entry_1 >> 16) |
3524 (entry_2 & 0xff000000) |
3525 ((entry_2 & 0xff) << 16);
3526 target_ldt_info->base_addr = tswapl(base_addr);
3527 target_ldt_info->limit = tswap32(limit);
3528 target_ldt_info->flags = tswap32(flags);
3529 unlock_user_struct(target_ldt_info, ptr, 1);
3530 return 0;
3531}
blueswir14583f582008-08-24 10:35:55 +00003532#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00003533
bellardd2fd1af2007-11-14 18:08:56 +00003534#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00003535static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00003536{
3537 abi_long ret;
3538 abi_ulong val;
3539 int idx;
3540
3541 switch(code) {
3542 case TARGET_ARCH_SET_GS:
3543 case TARGET_ARCH_SET_FS:
3544 if (code == TARGET_ARCH_SET_GS)
3545 idx = R_GS;
3546 else
3547 idx = R_FS;
3548 cpu_x86_load_seg(env, idx, 0);
3549 env->segs[idx].base = addr;
3550 break;
3551 case TARGET_ARCH_GET_GS:
3552 case TARGET_ARCH_GET_FS:
3553 if (code == TARGET_ARCH_GET_GS)
3554 idx = R_GS;
3555 else
3556 idx = R_FS;
3557 val = env->segs[idx].base;
3558 if (put_user(val, addr, abi_ulong))
3559 return -TARGET_EFAULT;
3560 break;
3561 default:
3562 ret = -TARGET_EINVAL;
3563 break;
3564 }
3565 return 0;
3566}
3567#endif
3568
bellard2ab83ea2003-06-15 19:56:46 +00003569#endif /* defined(TARGET_I386) */
3570
Juan Quintela2f7bb872009-07-27 16:13:24 +02003571#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003572
3573#define NEW_STACK_SIZE PTHREAD_STACK_MIN
3574
3575static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3576typedef struct {
3577 CPUState *env;
3578 pthread_mutex_t mutex;
3579 pthread_cond_t cond;
3580 pthread_t thread;
3581 uint32_t tid;
3582 abi_ulong child_tidptr;
3583 abi_ulong parent_tidptr;
3584 sigset_t sigmask;
3585} new_thread_info;
3586
3587static void *clone_func(void *arg)
3588{
3589 new_thread_info *info = arg;
3590 CPUState *env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003591 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00003592
3593 env = info->env;
3594 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003595 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00003596 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003597 env->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003598 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00003599 if (info->child_tidptr)
3600 put_user_u32(info->tid, info->child_tidptr);
3601 if (info->parent_tidptr)
3602 put_user_u32(info->tid, info->parent_tidptr);
3603 /* Enable signals. */
3604 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3605 /* Signal to the parent that we're ready. */
3606 pthread_mutex_lock(&info->mutex);
3607 pthread_cond_broadcast(&info->cond);
3608 pthread_mutex_unlock(&info->mutex);
3609 /* Wait until the parent has finshed initializing the tls state. */
3610 pthread_mutex_lock(&clone_lock);
3611 pthread_mutex_unlock(&clone_lock);
3612 cpu_loop(env);
3613 /* never exits */
3614 return NULL;
3615}
3616#else
bellard1b6b0292003-03-22 17:31:38 +00003617/* this stack is the equivalent of the kernel stack associated with a
3618 thread/process */
3619#define NEW_STACK_SIZE 8192
3620
3621static int clone_func(void *arg)
3622{
bellard2ab83ea2003-06-15 19:56:46 +00003623 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00003624 cpu_loop(env);
3625 /* never exits */
3626 return 0;
3627}
pbrookd865bab2008-06-07 22:12:17 +00003628#endif
bellard1b6b0292003-03-22 17:31:38 +00003629
ths0da46a62007-10-20 20:23:07 +00003630/* do_fork() Must return host values and target errnos (unlike most
3631 do_*() functions). */
pbrookd865bab2008-06-07 22:12:17 +00003632static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3633 abi_ulong parent_tidptr, target_ulong newtls,
3634 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00003635{
3636 int ret;
bellard5cd43932003-03-29 16:54:36 +00003637 TaskState *ts;
bellard2ab83ea2003-06-15 19:56:46 +00003638 CPUState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003639#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003640 unsigned int nptl_flags;
3641 sigset_t sigmask;
Riku Voipio91907492010-11-26 16:21:34 +02003642#else
3643 uint8_t *new_stack;
pbrookd865bab2008-06-07 22:12:17 +00003644#endif
ths3b46e622007-09-17 08:09:54 +00003645
balrog436d1242008-09-21 02:39:45 +00003646 /* Emulate vfork() with fork() */
3647 if (flags & CLONE_VFORK)
3648 flags &= ~(CLONE_VFORK | CLONE_VM);
3649
bellard1b6b0292003-03-22 17:31:38 +00003650 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003651 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003652#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003653 new_thread_info info;
3654 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00003655#endif
Nathan Froyd48e15fc2010-10-29 07:48:57 -07003656 ts = qemu_mallocz(sizeof(TaskState));
pbrook624f7972008-05-31 16:11:38 +00003657 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00003658 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00003659 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00003660#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3661 cpu_reset(new_env);
3662#endif
pbrook6e68e072008-05-30 17:22:15 +00003663 /* Init regs that differ from the parent. */
3664 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00003665 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003666 ts->bprm = parent_ts->bprm;
3667 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003668#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003669 nptl_flags = flags;
3670 flags &= ~CLONE_NPTL_FLAGS2;
3671
pbrookc2764712009-03-07 15:24:59 +00003672 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3673 ts->child_tidptr = child_tidptr;
3674 }
3675
pbrookd865bab2008-06-07 22:12:17 +00003676 if (nptl_flags & CLONE_SETTLS)
3677 cpu_set_tls (new_env, newtls);
3678
3679 /* Grab a mutex so that thread setup appears atomic. */
3680 pthread_mutex_lock(&clone_lock);
3681
3682 memset(&info, 0, sizeof(info));
3683 pthread_mutex_init(&info.mutex, NULL);
3684 pthread_mutex_lock(&info.mutex);
3685 pthread_cond_init(&info.cond, NULL);
3686 info.env = new_env;
3687 if (nptl_flags & CLONE_CHILD_SETTID)
3688 info.child_tidptr = child_tidptr;
3689 if (nptl_flags & CLONE_PARENT_SETTID)
3690 info.parent_tidptr = parent_tidptr;
3691
3692 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07003693 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
3694 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00003695 /* It is not safe to deliver signals until the child has finished
3696 initializing, so temporarily block all signals. */
3697 sigfillset(&sigmask);
3698 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3699
3700 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00003701 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00003702
3703 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3704 pthread_attr_destroy(&attr);
3705 if (ret == 0) {
3706 /* Wait for the child to initialize. */
3707 pthread_cond_wait(&info.cond, &info.mutex);
3708 ret = info.tid;
3709 if (flags & CLONE_PARENT_SETTID)
3710 put_user_u32(ret, parent_tidptr);
3711 } else {
3712 ret = -1;
3713 }
3714 pthread_mutex_unlock(&info.mutex);
3715 pthread_cond_destroy(&info.cond);
3716 pthread_mutex_destroy(&info.mutex);
3717 pthread_mutex_unlock(&clone_lock);
3718#else
3719 if (flags & CLONE_NPTL_FLAGS2)
3720 return -EINVAL;
3721 /* This is probably going to die very quickly, but do it anyway. */
Nathan Froyd48e15fc2010-10-29 07:48:57 -07003722 new_stack = qemu_mallocz (NEW_STACK_SIZE);
bellard27725c12003-04-29 21:08:18 +00003723#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02003724 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00003725#else
3726 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3727#endif
pbrookd865bab2008-06-07 22:12:17 +00003728#endif
bellard1b6b0292003-03-22 17:31:38 +00003729 } else {
3730 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00003731 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00003732 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00003733 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00003734 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00003735 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00003736 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00003737 cpu_clone_regs(env, newsp);
3738 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02003739#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00003740 /* There is a race condition here. The parent process could
3741 theoretically read the TID in the child process before the child
3742 tid is set. This would require using either ptrace
3743 (not implemented) or having *_tidptr to point at a shared memory
3744 mapping. We can't repeat the spinlock hack used above because
3745 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00003746 if (flags & CLONE_CHILD_SETTID)
3747 put_user_u32(gettid(), child_tidptr);
3748 if (flags & CLONE_PARENT_SETTID)
3749 put_user_u32(gettid(), parent_tidptr);
3750 ts = (TaskState *)env->opaque;
3751 if (flags & CLONE_SETTLS)
3752 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00003753 if (flags & CLONE_CHILD_CLEARTID)
3754 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00003755#endif
pbrookd865bab2008-06-07 22:12:17 +00003756 } else {
3757 fork_end(0);
3758 }
bellard1b6b0292003-03-22 17:31:38 +00003759 }
3760 return ret;
3761}
3762
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003763/* warning : doesn't handle linux specific flags... */
3764static int target_to_host_fcntl_cmd(int cmd)
3765{
3766 switch(cmd) {
3767 case TARGET_F_DUPFD:
3768 case TARGET_F_GETFD:
3769 case TARGET_F_SETFD:
3770 case TARGET_F_GETFL:
3771 case TARGET_F_SETFL:
3772 return cmd;
3773 case TARGET_F_GETLK:
3774 return F_GETLK;
3775 case TARGET_F_SETLK:
3776 return F_SETLK;
3777 case TARGET_F_SETLKW:
3778 return F_SETLKW;
3779 case TARGET_F_GETOWN:
3780 return F_GETOWN;
3781 case TARGET_F_SETOWN:
3782 return F_SETOWN;
3783 case TARGET_F_GETSIG:
3784 return F_GETSIG;
3785 case TARGET_F_SETSIG:
3786 return F_SETSIG;
3787#if TARGET_ABI_BITS == 32
3788 case TARGET_F_GETLK64:
3789 return F_GETLK64;
3790 case TARGET_F_SETLK64:
3791 return F_SETLK64;
3792 case TARGET_F_SETLKW64:
3793 return F_SETLKW64;
3794#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003795 case TARGET_F_SETLEASE:
3796 return F_SETLEASE;
3797 case TARGET_F_GETLEASE:
3798 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04003799#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003800 case TARGET_F_DUPFD_CLOEXEC:
3801 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04003802#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003803 case TARGET_F_NOTIFY:
3804 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003805 default:
3806 return -TARGET_EINVAL;
3807 }
3808 return -TARGET_EINVAL;
3809}
3810
blueswir1992f48a2007-10-14 16:27:31 +00003811static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00003812{
3813 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00003814 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00003815 struct flock64 fl64;
3816 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00003817 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003818 int host_cmd = target_to_host_fcntl_cmd(cmd);
3819
3820 if (host_cmd == -TARGET_EINVAL)
3821 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00003822
bellard7775e9e2003-05-14 22:46:48 +00003823 switch(cmd) {
3824 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00003825 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3826 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003827 fl.l_type = tswap16(target_fl->l_type);
3828 fl.l_whence = tswap16(target_fl->l_whence);
3829 fl.l_start = tswapl(target_fl->l_start);
3830 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003831 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00003832 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003833 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003834 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003835 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3836 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003837 target_fl->l_type = tswap16(fl.l_type);
3838 target_fl->l_whence = tswap16(fl.l_whence);
3839 target_fl->l_start = tswapl(fl.l_start);
3840 target_fl->l_len = tswapl(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003841 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00003842 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00003843 }
3844 break;
ths3b46e622007-09-17 08:09:54 +00003845
bellard7775e9e2003-05-14 22:46:48 +00003846 case TARGET_F_SETLK:
3847 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00003848 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3849 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003850 fl.l_type = tswap16(target_fl->l_type);
3851 fl.l_whence = tswap16(target_fl->l_whence);
3852 fl.l_start = tswapl(target_fl->l_start);
3853 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003854 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00003855 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003856 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003857 break;
ths3b46e622007-09-17 08:09:54 +00003858
bellard7775e9e2003-05-14 22:46:48 +00003859 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00003860 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3861 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003862 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3863 fl64.l_whence = tswap16(target_fl64->l_whence);
3864 fl64.l_start = tswapl(target_fl64->l_start);
3865 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003866 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00003867 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003868 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00003869 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003870 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3871 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003872 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3873 target_fl64->l_whence = tswap16(fl64.l_whence);
3874 target_fl64->l_start = tswapl(fl64.l_start);
3875 target_fl64->l_len = tswapl(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003876 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00003877 unlock_user_struct(target_fl64, arg, 1);
3878 }
bellard9ee1fa22007-11-11 15:11:19 +00003879 break;
bellard7775e9e2003-05-14 22:46:48 +00003880 case TARGET_F_SETLK64:
3881 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00003882 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3883 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003884 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3885 fl64.l_whence = tswap16(target_fl64->l_whence);
3886 fl64.l_start = tswapl(target_fl64->l_start);
3887 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003888 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00003889 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003890 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00003891 break;
3892
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003893 case TARGET_F_GETFL:
3894 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00003895 if (ret >= 0) {
3896 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3897 }
bellardffa65c32004-01-04 23:57:22 +00003898 break;
3899
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003900 case TARGET_F_SETFL:
3901 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3902 break;
3903
3904 case TARGET_F_SETOWN:
3905 case TARGET_F_GETOWN:
3906 case TARGET_F_SETSIG:
3907 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003908 case TARGET_F_SETLEASE:
3909 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003910 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00003911 break;
3912
bellard7775e9e2003-05-14 22:46:48 +00003913 default:
bellard9ee1fa22007-11-11 15:11:19 +00003914 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00003915 break;
3916 }
3917 return ret;
3918}
3919
bellard67867302003-11-23 17:05:30 +00003920#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00003921
bellard67867302003-11-23 17:05:30 +00003922static inline int high2lowuid(int uid)
3923{
3924 if (uid > 65535)
3925 return 65534;
3926 else
3927 return uid;
3928}
3929
3930static inline int high2lowgid(int gid)
3931{
3932 if (gid > 65535)
3933 return 65534;
3934 else
3935 return gid;
3936}
3937
3938static inline int low2highuid(int uid)
3939{
3940 if ((int16_t)uid == -1)
3941 return -1;
3942 else
3943 return uid;
3944}
3945
3946static inline int low2highgid(int gid)
3947{
3948 if ((int16_t)gid == -1)
3949 return -1;
3950 else
3951 return gid;
3952}
3953
3954#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00003955
bellard31e31b82003-02-18 22:55:36 +00003956void syscall_init(void)
3957{
bellard2ab83ea2003-06-15 19:56:46 +00003958 IOCTLEntry *ie;
3959 const argtype *arg_type;
3960 int size;
thsb92c47c2007-11-01 00:07:38 +00003961 int i;
bellard2ab83ea2003-06-15 19:56:46 +00003962
Blue Swirl001faf32009-05-13 17:53:17 +00003963#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00003964#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00003965#include "syscall_types.h"
3966#undef STRUCT
3967#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00003968
3969 /* we patch the ioctl size if necessary. We rely on the fact that
3970 no ioctl has all the bits at '1' in the size field */
3971 ie = ioctl_entries;
3972 while (ie->target_cmd != 0) {
3973 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3974 TARGET_IOC_SIZEMASK) {
3975 arg_type = ie->arg_type;
3976 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00003977 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00003978 ie->target_cmd);
3979 exit(1);
3980 }
3981 arg_type++;
3982 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00003983 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00003984 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3985 (size << TARGET_IOC_SIZESHIFT);
3986 }
thsb92c47c2007-11-01 00:07:38 +00003987
3988 /* Build target_to_host_errno_table[] table from
3989 * host_to_target_errno_table[]. */
3990 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3991 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3992
bellard2ab83ea2003-06-15 19:56:46 +00003993 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00003994#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3995 (defined(__x86_64__) && defined(TARGET_X86_64))
3996 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3997 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3998 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00003999 }
4000#endif
4001 ie++;
4002 }
bellard31e31b82003-02-18 22:55:36 +00004003}
bellardc573ff62004-01-04 15:51:36 +00004004
blueswir1992f48a2007-10-14 16:27:31 +00004005#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004006static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4007{
thsaf325d32008-06-10 15:29:15 +00004008#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004009 return ((uint64_t)word0 << 32) | word1;
4010#else
4011 return ((uint64_t)word1 << 32) | word0;
4012#endif
4013}
blueswir1992f48a2007-10-14 16:27:31 +00004014#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004015static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4016{
4017 return word0;
4018}
blueswir1992f48a2007-10-14 16:27:31 +00004019#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004020
4021#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004022static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4023 abi_long arg2,
4024 abi_long arg3,
4025 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004026{
4027#ifdef TARGET_ARM
4028 if (((CPUARMState *)cpu_env)->eabi)
4029 {
4030 arg2 = arg3;
4031 arg3 = arg4;
4032 }
4033#endif
4034 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4035}
4036#endif
4037
4038#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004039static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4040 abi_long arg2,
4041 abi_long arg3,
4042 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004043{
4044#ifdef TARGET_ARM
4045 if (((CPUARMState *)cpu_env)->eabi)
4046 {
4047 arg2 = arg3;
4048 arg3 = arg4;
4049 }
4050#endif
4051 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4052}
4053#endif
4054
bellard579a97f2007-11-11 14:26:47 +00004055static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4056 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004057{
4058 struct target_timespec *target_ts;
4059
bellard579a97f2007-11-11 14:26:47 +00004060 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4061 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004062 host_ts->tv_sec = tswapl(target_ts->tv_sec);
4063 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
4064 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004065 return 0;
pbrook53a59602006-03-25 19:31:22 +00004066}
4067
bellard579a97f2007-11-11 14:26:47 +00004068static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4069 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004070{
4071 struct target_timespec *target_ts;
4072
bellard579a97f2007-11-11 14:26:47 +00004073 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4074 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004075 target_ts->tv_sec = tswapl(host_ts->tv_sec);
4076 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
4077 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004078 return 0;
pbrook53a59602006-03-25 19:31:22 +00004079}
4080
aurel329d33b762009-04-08 23:07:05 +00004081#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004082static inline abi_long host_to_target_stat64(void *cpu_env,
4083 abi_ulong target_addr,
4084 struct stat *host_st)
4085{
4086#ifdef TARGET_ARM
4087 if (((CPUARMState *)cpu_env)->eabi) {
4088 struct target_eabi_stat64 *target_st;
4089
4090 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4091 return -TARGET_EFAULT;
4092 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4093 __put_user(host_st->st_dev, &target_st->st_dev);
4094 __put_user(host_st->st_ino, &target_st->st_ino);
4095#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4096 __put_user(host_st->st_ino, &target_st->__st_ino);
4097#endif
4098 __put_user(host_st->st_mode, &target_st->st_mode);
4099 __put_user(host_st->st_nlink, &target_st->st_nlink);
4100 __put_user(host_st->st_uid, &target_st->st_uid);
4101 __put_user(host_st->st_gid, &target_st->st_gid);
4102 __put_user(host_st->st_rdev, &target_st->st_rdev);
4103 __put_user(host_st->st_size, &target_st->st_size);
4104 __put_user(host_st->st_blksize, &target_st->st_blksize);
4105 __put_user(host_st->st_blocks, &target_st->st_blocks);
4106 __put_user(host_st->st_atime, &target_st->target_st_atime);
4107 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4108 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4109 unlock_user_struct(target_st, target_addr, 1);
4110 } else
4111#endif
4112 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004113#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004114 struct target_stat *target_st;
4115#else
balrog6a24a772008-09-20 02:23:36 +00004116 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004117#endif
balrog6a24a772008-09-20 02:23:36 +00004118
4119 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4120 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004121 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004122 __put_user(host_st->st_dev, &target_st->st_dev);
4123 __put_user(host_st->st_ino, &target_st->st_ino);
4124#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4125 __put_user(host_st->st_ino, &target_st->__st_ino);
4126#endif
4127 __put_user(host_st->st_mode, &target_st->st_mode);
4128 __put_user(host_st->st_nlink, &target_st->st_nlink);
4129 __put_user(host_st->st_uid, &target_st->st_uid);
4130 __put_user(host_st->st_gid, &target_st->st_gid);
4131 __put_user(host_st->st_rdev, &target_st->st_rdev);
4132 /* XXX: better use of kernel struct */
4133 __put_user(host_st->st_size, &target_st->st_size);
4134 __put_user(host_st->st_blksize, &target_st->st_blksize);
4135 __put_user(host_st->st_blocks, &target_st->st_blocks);
4136 __put_user(host_st->st_atime, &target_st->target_st_atime);
4137 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4138 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4139 unlock_user_struct(target_st, target_addr, 1);
4140 }
4141
4142 return 0;
4143}
4144#endif
4145
Juan Quintela2f7bb872009-07-27 16:13:24 +02004146#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004147/* ??? Using host futex calls even when target atomic operations
4148 are not really atomic probably breaks things. However implementing
4149 futexes locally would make futexes shared between multiple processes
4150 tricky. However they're probably useless because guest atomic
4151 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004152static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4153 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004154{
4155 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004156 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004157
4158 /* ??? We assume FUTEX_* constants are the same on both host
4159 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004160#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004161 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004162#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004163 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004164#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004165 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004166 case FUTEX_WAIT:
4167 if (timeout) {
4168 pts = &ts;
4169 target_to_host_timespec(pts, timeout);
4170 } else {
4171 pts = NULL;
4172 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004173 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004174 pts, NULL, 0));
4175 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004176 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004177 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004178 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004179 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004180 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004181 case FUTEX_WAKE_OP:
4182 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4183 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4184 But the prototype takes a `struct timespec *'; insert casts
4185 to satisfy the compiler. We do not need to tswap TIMEOUT
4186 since it's not compared to guest memory. */
4187 pts = (struct timespec *)(uintptr_t) timeout;
4188 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4189 g2h(uaddr2),
4190 (base_op == FUTEX_CMP_REQUEUE
4191 ? tswap32(val3)
4192 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004193 default:
4194 return -TARGET_ENOSYS;
4195 }
4196}
4197#endif
4198
pbrook1d9d8b52009-04-16 15:17:02 +00004199/* Map host to target signal numbers for the wait family of syscalls.
4200 Assume all other status bits are the same. */
4201static int host_to_target_waitstatus(int status)
4202{
4203 if (WIFSIGNALED(status)) {
4204 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4205 }
4206 if (WIFSTOPPED(status)) {
4207 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4208 | (status & 0xff);
4209 }
4210 return status;
4211}
4212
pbrooka745ec62008-05-06 15:36:17 +00004213int get_osversion(void)
4214{
4215 static int osversion;
4216 struct new_utsname buf;
4217 const char *s;
4218 int i, n, tmp;
4219 if (osversion)
4220 return osversion;
4221 if (qemu_uname_release && *qemu_uname_release) {
4222 s = qemu_uname_release;
4223 } else {
4224 if (sys_uname(&buf))
4225 return 0;
4226 s = buf.release;
4227 }
4228 tmp = 0;
4229 for (i = 0; i < 3; i++) {
4230 n = 0;
4231 while (*s >= '0' && *s <= '9') {
4232 n *= 10;
4233 n += *s - '0';
4234 s++;
4235 }
4236 tmp = (tmp << 8) + n;
4237 if (*s == '.')
4238 s++;
4239 }
4240 osversion = tmp;
4241 return osversion;
4242}
4243
ths0da46a62007-10-20 20:23:07 +00004244/* do_syscall() should always have a single exit point at the end so
4245 that actions, such as logging of syscall results, can be performed.
4246 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00004247abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4248 abi_long arg2, abi_long arg3, abi_long arg4,
4249 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00004250{
blueswir1992f48a2007-10-14 16:27:31 +00004251 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004252 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00004253 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00004254 void *p;
ths3b46e622007-09-17 08:09:54 +00004255
bellard72f03902003-02-18 23:33:18 +00004256#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00004257 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00004258#endif
thsb92c47c2007-11-01 00:07:38 +00004259 if(do_strace)
4260 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4261
bellard31e31b82003-02-18 22:55:36 +00004262 switch(num) {
4263 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02004264#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00004265 /* In old applications this may be used to implement _exit(2).
4266 However in threaded applictions it is used for thread termination,
4267 and _exit_group is used for application termination.
4268 Do thread termination if we have more then one thread. */
4269 /* FIXME: This probably breaks if a signal arrives. We should probably
4270 be disabling signals. */
4271 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004272 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00004273 CPUState **lastp;
4274 CPUState *p;
4275
4276 cpu_list_lock();
4277 lastp = &first_cpu;
4278 p = first_cpu;
4279 while (p && p != (CPUState *)cpu_env) {
4280 lastp = &p->next_cpu;
4281 p = p->next_cpu;
4282 }
4283 /* If we didn't find the CPU for this thread then something is
4284 horribly wrong. */
4285 if (!p)
4286 abort();
4287 /* Remove the CPU from the list. */
4288 *lastp = p->next_cpu;
4289 cpu_list_unlock();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004290 ts = ((CPUState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00004291 if (ts->child_tidptr) {
4292 put_user_u32(0, ts->child_tidptr);
4293 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4294 NULL, NULL, 0);
4295 }
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004296 thread_env = NULL;
4297 qemu_free(cpu_env);
4298 qemu_free(ts);
pbrookc2764712009-03-07 15:24:59 +00004299 pthread_exit(NULL);
4300 }
4301#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02004302#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00004303 _mcleanup();
4304#endif
bellarde9009672005-04-26 20:42:36 +00004305 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00004306 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00004307 ret = 0; /* avoid warning */
4308 break;
4309 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00004310 if (arg3 == 0)
4311 ret = 0;
4312 else {
4313 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4314 goto efault;
4315 ret = get_errno(read(arg1, p, arg3));
4316 unlock_user(p, arg2, ret);
4317 }
bellard31e31b82003-02-18 22:55:36 +00004318 break;
4319 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00004320 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4321 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004322 ret = get_errno(write(arg1, p, arg3));
4323 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00004324 break;
4325 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00004326 if (!(p = lock_user_string(arg1)))
4327 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004328 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00004329 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4330 arg3));
pbrook53a59602006-03-25 19:31:22 +00004331 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004332 break;
ths82424832007-09-24 09:21:55 +00004333#if defined(TARGET_NR_openat) && defined(__NR_openat)
4334 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00004335 if (!(p = lock_user_string(arg2)))
4336 goto efault;
4337 ret = get_errno(sys_openat(arg1,
4338 path(p),
4339 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4340 arg4));
4341 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00004342 break;
4343#endif
bellard31e31b82003-02-18 22:55:36 +00004344 case TARGET_NR_close:
4345 ret = get_errno(close(arg1));
4346 break;
4347 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00004348 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00004349 break;
4350 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00004351 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00004352 break;
thse5febef2007-04-01 18:31:35 +00004353#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00004354 case TARGET_NR_waitpid:
4355 {
pbrook53a59602006-03-25 19:31:22 +00004356 int status;
4357 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00004358 if (!is_error(ret) && arg2
pbrook1d9d8b52009-04-16 15:17:02 +00004359 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00004360 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004361 }
4362 break;
thse5febef2007-04-01 18:31:35 +00004363#endif
pbrookf0cbb612008-05-30 18:20:05 +00004364#ifdef TARGET_NR_waitid
4365 case TARGET_NR_waitid:
4366 {
4367 siginfo_t info;
4368 info.si_pid = 0;
4369 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4370 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004371 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00004372 goto efault;
4373 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05004374 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00004375 }
4376 }
4377 break;
4378#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004379#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004380 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00004381 if (!(p = lock_user_string(arg1)))
4382 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004383 ret = get_errno(creat(p, arg2));
4384 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004385 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004386#endif
bellard31e31b82003-02-18 22:55:36 +00004387 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00004388 {
4389 void * p2;
4390 p = lock_user_string(arg1);
4391 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004392 if (!p || !p2)
4393 ret = -TARGET_EFAULT;
4394 else
4395 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004396 unlock_user(p2, arg2, 0);
4397 unlock_user(p, arg1, 0);
4398 }
bellard31e31b82003-02-18 22:55:36 +00004399 break;
ths64f0ce42007-09-24 09:25:06 +00004400#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4401 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00004402 {
4403 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00004404 if (!arg2 || !arg4)
4405 goto efault;
ths64f0ce42007-09-24 09:25:06 +00004406 p = lock_user_string(arg2);
4407 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004408 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004409 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00004410 else
4411 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00004412 unlock_user(p, arg2, 0);
4413 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00004414 }
4415 break;
4416#endif
bellard31e31b82003-02-18 22:55:36 +00004417 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00004418 if (!(p = lock_user_string(arg1)))
4419 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004420 ret = get_errno(unlink(p));
4421 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004422 break;
ths8170f562007-09-24 09:24:11 +00004423#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4424 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00004425 if (!(p = lock_user_string(arg2)))
4426 goto efault;
4427 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4428 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00004429 break;
balrogb7d35e62007-12-12 00:40:24 +00004430#endif
bellard31e31b82003-02-18 22:55:36 +00004431 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00004432 {
4433 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00004434 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00004435 abi_ulong gp;
4436 abi_ulong guest_argp;
4437 abi_ulong guest_envp;
4438 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00004439 char **q;
4440
bellardf7341ff2003-03-30 21:00:25 +00004441 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00004442 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00004443 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004444 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004445 goto efault;
ths03aa1972007-12-02 06:28:08 +00004446 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004447 break;
bellard7854b052003-03-29 17:22:23 +00004448 argc++;
bellard2f619692007-11-16 10:46:05 +00004449 }
bellardf7341ff2003-03-30 21:00:25 +00004450 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00004451 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00004452 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004453 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004454 goto efault;
ths03aa1972007-12-02 06:28:08 +00004455 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004456 break;
bellard7854b052003-03-29 17:22:23 +00004457 envc++;
bellard2f619692007-11-16 10:46:05 +00004458 }
bellard7854b052003-03-29 17:22:23 +00004459
bellardf7341ff2003-03-30 21:00:25 +00004460 argp = alloca((argc + 1) * sizeof(void *));
4461 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00004462
pbrookda94d262008-05-30 18:24:00 +00004463 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004464 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004465 if (get_user_ual(addr, gp))
4466 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004467 if (!addr)
4468 break;
bellard2f619692007-11-16 10:46:05 +00004469 if (!(*q = lock_user_string(addr)))
4470 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004471 }
bellardf7341ff2003-03-30 21:00:25 +00004472 *q = NULL;
4473
pbrookda94d262008-05-30 18:24:00 +00004474 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004475 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004476 if (get_user_ual(addr, gp))
4477 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004478 if (!addr)
4479 break;
bellard2f619692007-11-16 10:46:05 +00004480 if (!(*q = lock_user_string(addr)))
4481 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004482 }
bellardf7341ff2003-03-30 21:00:25 +00004483 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00004484
bellard2f619692007-11-16 10:46:05 +00004485 if (!(p = lock_user_string(arg1)))
4486 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004487 ret = get_errno(execve(p, argp, envp));
4488 unlock_user(p, arg1, 0);
4489
bellard2f619692007-11-16 10:46:05 +00004490 goto execve_end;
4491
4492 execve_efault:
4493 ret = -TARGET_EFAULT;
4494
4495 execve_end:
pbrook53a59602006-03-25 19:31:22 +00004496 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004497 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004498 if (get_user_ual(addr, gp)
4499 || !addr)
4500 break;
pbrook53a59602006-03-25 19:31:22 +00004501 unlock_user(*q, addr, 0);
4502 }
4503 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004504 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004505 if (get_user_ual(addr, gp)
4506 || !addr)
4507 break;
pbrook53a59602006-03-25 19:31:22 +00004508 unlock_user(*q, addr, 0);
4509 }
bellard7854b052003-03-29 17:22:23 +00004510 }
bellard31e31b82003-02-18 22:55:36 +00004511 break;
4512 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00004513 if (!(p = lock_user_string(arg1)))
4514 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004515 ret = get_errno(chdir(p));
4516 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004517 break;
bellarda315a142005-01-30 22:59:18 +00004518#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00004519 case TARGET_NR_time:
4520 {
pbrook53a59602006-03-25 19:31:22 +00004521 time_t host_time;
4522 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00004523 if (!is_error(ret)
4524 && arg1
4525 && put_user_sal(host_time, arg1))
4526 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004527 }
4528 break;
bellarda315a142005-01-30 22:59:18 +00004529#endif
bellard31e31b82003-02-18 22:55:36 +00004530 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00004531 if (!(p = lock_user_string(arg1)))
4532 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004533 ret = get_errno(mknod(p, arg2, arg3));
4534 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004535 break;
ths75ac37a2007-09-24 09:23:05 +00004536#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4537 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00004538 if (!(p = lock_user_string(arg2)))
4539 goto efault;
4540 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4541 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00004542 break;
4543#endif
bellard31e31b82003-02-18 22:55:36 +00004544 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00004545 if (!(p = lock_user_string(arg1)))
4546 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004547 ret = get_errno(chmod(p, arg2));
4548 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004549 break;
bellardebc05482003-09-30 21:08:41 +00004550#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00004551 case TARGET_NR_break:
4552 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004553#endif
4554#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00004555 case TARGET_NR_oldstat:
4556 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004557#endif
bellard31e31b82003-02-18 22:55:36 +00004558 case TARGET_NR_lseek:
4559 ret = get_errno(lseek(arg1, arg2, arg3));
4560 break;
Richard Henderson92317332010-05-03 10:07:53 -07004561#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
4562 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00004563 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07004564 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00004565 ret = get_errno(getpid());
4566 break;
Richard Henderson92317332010-05-03 10:07:53 -07004567#endif
4568#ifdef TARGET_NR_getpid
4569 case TARGET_NR_getpid:
4570 ret = get_errno(getpid());
4571 break;
4572#endif
bellard31e31b82003-02-18 22:55:36 +00004573 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00004574 {
4575 /* need to look at the data field */
4576 void *p2, *p3;
4577 p = lock_user_string(arg1);
4578 p2 = lock_user_string(arg2);
4579 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004580 if (!p || !p2 || !p3)
4581 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004582 else {
bellard579a97f2007-11-11 14:26:47 +00004583 /* FIXME - arg5 should be locked, but it isn't clear how to
4584 * do that since it's not guaranteed to be a NULL-terminated
4585 * string.
4586 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004587 if ( ! arg5 )
4588 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4589 else
4590 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4591 }
bellard579a97f2007-11-11 14:26:47 +00004592 unlock_user(p, arg1, 0);
4593 unlock_user(p2, arg2, 0);
4594 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00004595 break;
4596 }
thse5febef2007-04-01 18:31:35 +00004597#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00004598 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00004599 if (!(p = lock_user_string(arg1)))
4600 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004601 ret = get_errno(umount(p));
4602 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004603 break;
thse5febef2007-04-01 18:31:35 +00004604#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004605#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004606 case TARGET_NR_stime:
4607 {
pbrook53a59602006-03-25 19:31:22 +00004608 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00004609 if (get_user_sal(host_time, arg1))
4610 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004611 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00004612 }
4613 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004614#endif
bellard31e31b82003-02-18 22:55:36 +00004615 case TARGET_NR_ptrace:
4616 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00004617#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004618 case TARGET_NR_alarm:
4619 ret = alarm(arg1);
4620 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004621#endif
bellardebc05482003-09-30 21:08:41 +00004622#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00004623 case TARGET_NR_oldfstat:
4624 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004625#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004626#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004627 case TARGET_NR_pause:
4628 ret = get_errno(pause());
4629 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004630#endif
thse5febef2007-04-01 18:31:35 +00004631#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00004632 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00004633 {
pbrook53a59602006-03-25 19:31:22 +00004634 struct utimbuf tbuf, *host_tbuf;
4635 struct target_utimbuf *target_tbuf;
4636 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004637 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4638 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004639 tbuf.actime = tswapl(target_tbuf->actime);
4640 tbuf.modtime = tswapl(target_tbuf->modtime);
4641 unlock_user_struct(target_tbuf, arg2, 0);
4642 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00004643 } else {
pbrook53a59602006-03-25 19:31:22 +00004644 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00004645 }
bellard579a97f2007-11-11 14:26:47 +00004646 if (!(p = lock_user_string(arg1)))
4647 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004648 ret = get_errno(utime(p, host_tbuf));
4649 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00004650 }
4651 break;
thse5febef2007-04-01 18:31:35 +00004652#endif
bellard978a66f2004-12-06 22:58:05 +00004653 case TARGET_NR_utimes:
4654 {
bellard978a66f2004-12-06 22:58:05 +00004655 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00004656 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00004657 if (copy_from_user_timeval(&tv[0], arg2)
4658 || copy_from_user_timeval(&tv[1],
4659 arg2 + sizeof(struct target_timeval)))
4660 goto efault;
bellard978a66f2004-12-06 22:58:05 +00004661 tvp = tv;
4662 } else {
4663 tvp = NULL;
4664 }
bellard579a97f2007-11-11 14:26:47 +00004665 if (!(p = lock_user_string(arg1)))
4666 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004667 ret = get_errno(utimes(p, tvp));
4668 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00004669 }
4670 break;
balrogac8a6552008-09-20 02:25:39 +00004671#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4672 case TARGET_NR_futimesat:
4673 {
4674 struct timeval *tvp, tv[2];
4675 if (arg3) {
4676 if (copy_from_user_timeval(&tv[0], arg3)
4677 || copy_from_user_timeval(&tv[1],
4678 arg3 + sizeof(struct target_timeval)))
4679 goto efault;
4680 tvp = tv;
4681 } else {
4682 tvp = NULL;
4683 }
4684 if (!(p = lock_user_string(arg2)))
4685 goto efault;
4686 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4687 unlock_user(p, arg2, 0);
4688 }
4689 break;
4690#endif
bellardebc05482003-09-30 21:08:41 +00004691#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00004692 case TARGET_NR_stty:
4693 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004694#endif
4695#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00004696 case TARGET_NR_gtty:
4697 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004698#endif
bellard31e31b82003-02-18 22:55:36 +00004699 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00004700 if (!(p = lock_user_string(arg1)))
4701 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02004702 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00004703 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004704 break;
ths92a34c12007-09-24 09:27:49 +00004705#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4706 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00004707 if (!(p = lock_user_string(arg2)))
4708 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00004709 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00004710 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00004711 break;
4712#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004713#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004714 case TARGET_NR_nice:
4715 ret = get_errno(nice(arg1));
4716 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004717#endif
bellardebc05482003-09-30 21:08:41 +00004718#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00004719 case TARGET_NR_ftime:
4720 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004721#endif
bellard31e31b82003-02-18 22:55:36 +00004722 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00004723 sync();
4724 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00004725 break;
4726 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00004727 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00004728 break;
4729 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00004730 {
4731 void *p2;
4732 p = lock_user_string(arg1);
4733 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004734 if (!p || !p2)
4735 ret = -TARGET_EFAULT;
4736 else
4737 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004738 unlock_user(p2, arg2, 0);
4739 unlock_user(p, arg1, 0);
4740 }
bellard31e31b82003-02-18 22:55:36 +00004741 break;
ths722183f2007-09-24 09:24:37 +00004742#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4743 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00004744 {
bellard579a97f2007-11-11 14:26:47 +00004745 void *p2;
ths722183f2007-09-24 09:24:37 +00004746 p = lock_user_string(arg2);
4747 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004748 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004749 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00004750 else
4751 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00004752 unlock_user(p2, arg4, 0);
4753 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00004754 }
4755 break;
4756#endif
bellard31e31b82003-02-18 22:55:36 +00004757 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00004758 if (!(p = lock_user_string(arg1)))
4759 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004760 ret = get_errno(mkdir(p, arg2));
4761 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004762 break;
ths4472ad02007-09-24 09:22:32 +00004763#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4764 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00004765 if (!(p = lock_user_string(arg2)))
4766 goto efault;
4767 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4768 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00004769 break;
4770#endif
bellard31e31b82003-02-18 22:55:36 +00004771 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00004772 if (!(p = lock_user_string(arg1)))
4773 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004774 ret = get_errno(rmdir(p));
4775 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004776 break;
4777 case TARGET_NR_dup:
4778 ret = get_errno(dup(arg1));
4779 break;
4780 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07004781 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00004782 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03004783#ifdef TARGET_NR_pipe2
4784 case TARGET_NR_pipe2:
Richard Hendersonfb41a662010-05-03 10:07:52 -07004785 ret = do_pipe(cpu_env, arg1, arg2, 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03004786 break;
4787#endif
bellard31e31b82003-02-18 22:55:36 +00004788 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00004789 {
pbrook53a59602006-03-25 19:31:22 +00004790 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00004791 struct tms tms;
4792 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00004793 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00004794 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4795 if (!tmsp)
4796 goto efault;
bellardc596ed12003-07-13 17:32:31 +00004797 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4798 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4799 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4800 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00004801 }
bellardc596ed12003-07-13 17:32:31 +00004802 if (!is_error(ret))
4803 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00004804 }
4805 break;
bellardebc05482003-09-30 21:08:41 +00004806#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00004807 case TARGET_NR_prof:
4808 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004809#endif
thse5febef2007-04-01 18:31:35 +00004810#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00004811 case TARGET_NR_signal:
4812 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004813#endif
bellard31e31b82003-02-18 22:55:36 +00004814 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00004815 if (arg1 == 0) {
4816 ret = get_errno(acct(NULL));
4817 } else {
4818 if (!(p = lock_user_string(arg1)))
4819 goto efault;
4820 ret = get_errno(acct(path(p)));
4821 unlock_user(p, arg1, 0);
4822 }
pbrook24836682006-04-16 14:14:53 +00004823 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004824#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004825 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00004826 if (!(p = lock_user_string(arg1)))
4827 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004828 ret = get_errno(umount2(p, arg2));
4829 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004830 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004831#endif
bellardebc05482003-09-30 21:08:41 +00004832#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00004833 case TARGET_NR_lock:
4834 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004835#endif
bellard31e31b82003-02-18 22:55:36 +00004836 case TARGET_NR_ioctl:
4837 ret = do_ioctl(arg1, arg2, arg3);
4838 break;
4839 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00004840 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00004841 break;
bellardebc05482003-09-30 21:08:41 +00004842#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00004843 case TARGET_NR_mpx:
4844 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004845#endif
bellard31e31b82003-02-18 22:55:36 +00004846 case TARGET_NR_setpgid:
4847 ret = get_errno(setpgid(arg1, arg2));
4848 break;
bellardebc05482003-09-30 21:08:41 +00004849#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00004850 case TARGET_NR_ulimit:
4851 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004852#endif
4853#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00004854 case TARGET_NR_oldolduname:
4855 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004856#endif
bellard31e31b82003-02-18 22:55:36 +00004857 case TARGET_NR_umask:
4858 ret = get_errno(umask(arg1));
4859 break;
4860 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00004861 if (!(p = lock_user_string(arg1)))
4862 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004863 ret = get_errno(chroot(p));
4864 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004865 break;
4866 case TARGET_NR_ustat:
4867 goto unimplemented;
4868 case TARGET_NR_dup2:
4869 ret = get_errno(dup2(arg1, arg2));
4870 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03004871#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
4872 case TARGET_NR_dup3:
4873 ret = get_errno(dup3(arg1, arg2, arg3));
4874 break;
4875#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004876#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004877 case TARGET_NR_getppid:
4878 ret = get_errno(getppid());
4879 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004880#endif
bellard31e31b82003-02-18 22:55:36 +00004881 case TARGET_NR_getpgrp:
4882 ret = get_errno(getpgrp());
4883 break;
4884 case TARGET_NR_setsid:
4885 ret = get_errno(setsid());
4886 break;
thse5febef2007-04-01 18:31:35 +00004887#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00004888 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00004889 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08004890#if defined(TARGET_ALPHA)
4891 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00004892 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00004893 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004894 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4895 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004896 act._sa_handler = old_act->_sa_handler;
4897 target_siginitset(&act.sa_mask, old_act->sa_mask);
4898 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08004899 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00004900 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004901 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00004902 }
4903 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00004904 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004905 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4906 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004907 old_act->_sa_handler = oact._sa_handler;
4908 old_act->sa_mask = oact.sa_mask.sig[0];
4909 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00004910 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00004911 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08004912#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00004913 struct target_sigaction act, oact, *pact, *old_act;
4914
4915 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004916 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4917 goto efault;
bellard106ec872006-06-27 21:08:10 +00004918 act._sa_handler = old_act->_sa_handler;
4919 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4920 act.sa_flags = old_act->sa_flags;
4921 unlock_user_struct(old_act, arg2, 0);
4922 pact = &act;
4923 } else {
4924 pact = NULL;
4925 }
4926
4927 ret = get_errno(do_sigaction(arg1, pact, &oact));
4928
4929 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004930 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4931 goto efault;
bellard106ec872006-06-27 21:08:10 +00004932 old_act->_sa_handler = oact._sa_handler;
4933 old_act->sa_flags = oact.sa_flags;
4934 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4935 old_act->sa_mask.sig[1] = 0;
4936 old_act->sa_mask.sig[2] = 0;
4937 old_act->sa_mask.sig[3] = 0;
4938 unlock_user_struct(old_act, arg3, 1);
4939 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08004940#else
4941 struct target_old_sigaction *old_act;
4942 struct target_sigaction act, oact, *pact;
4943 if (arg2) {
4944 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4945 goto efault;
4946 act._sa_handler = old_act->_sa_handler;
4947 target_siginitset(&act.sa_mask, old_act->sa_mask);
4948 act.sa_flags = old_act->sa_flags;
4949 act.sa_restorer = old_act->sa_restorer;
4950 unlock_user_struct(old_act, arg2, 0);
4951 pact = &act;
4952 } else {
4953 pact = NULL;
4954 }
4955 ret = get_errno(do_sigaction(arg1, pact, &oact));
4956 if (!is_error(ret) && arg3) {
4957 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4958 goto efault;
4959 old_act->_sa_handler = oact._sa_handler;
4960 old_act->sa_mask = oact.sa_mask.sig[0];
4961 old_act->sa_flags = oact.sa_flags;
4962 old_act->sa_restorer = oact.sa_restorer;
4963 unlock_user_struct(old_act, arg3, 1);
4964 }
ths388bb212007-05-13 13:58:00 +00004965#endif
bellard31e31b82003-02-18 22:55:36 +00004966 }
4967 break;
thse5febef2007-04-01 18:31:35 +00004968#endif
bellard66fb9762003-03-23 01:06:05 +00004969 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00004970 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08004971#if defined(TARGET_ALPHA)
4972 struct target_sigaction act, oact, *pact = 0;
4973 struct target_rt_sigaction *rt_act;
4974 /* ??? arg4 == sizeof(sigset_t). */
4975 if (arg2) {
4976 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
4977 goto efault;
4978 act._sa_handler = rt_act->_sa_handler;
4979 act.sa_mask = rt_act->sa_mask;
4980 act.sa_flags = rt_act->sa_flags;
4981 act.sa_restorer = arg5;
4982 unlock_user_struct(rt_act, arg2, 0);
4983 pact = &act;
4984 }
4985 ret = get_errno(do_sigaction(arg1, pact, &oact));
4986 if (!is_error(ret) && arg3) {
4987 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
4988 goto efault;
4989 rt_act->_sa_handler = oact._sa_handler;
4990 rt_act->sa_mask = oact.sa_mask;
4991 rt_act->sa_flags = oact.sa_flags;
4992 unlock_user_struct(rt_act, arg3, 1);
4993 }
4994#else
pbrook53a59602006-03-25 19:31:22 +00004995 struct target_sigaction *act;
4996 struct target_sigaction *oact;
4997
bellard579a97f2007-11-11 14:26:47 +00004998 if (arg2) {
4999 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5000 goto efault;
5001 } else
pbrook53a59602006-03-25 19:31:22 +00005002 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005003 if (arg3) {
5004 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5005 ret = -TARGET_EFAULT;
5006 goto rt_sigaction_fail;
5007 }
5008 } else
pbrook53a59602006-03-25 19:31:22 +00005009 oact = NULL;
5010 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005011 rt_sigaction_fail:
5012 if (act)
pbrook53a59602006-03-25 19:31:22 +00005013 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005014 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005015 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005016#endif
pbrook53a59602006-03-25 19:31:22 +00005017 }
bellard66fb9762003-03-23 01:06:05 +00005018 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005019#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005020 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005021 {
5022 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005023 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005024 sigprocmask(0, NULL, &cur_set);
5025 host_to_target_old_sigset(&target_set, &cur_set);
5026 ret = target_set;
5027 }
5028 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005029#endif
5030#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005031 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005032 {
5033 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005034 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00005035 sigprocmask(0, NULL, &cur_set);
5036 target_to_host_old_sigset(&set, &target_set);
5037 sigorset(&set, &set, &cur_set);
5038 sigprocmask(SIG_SETMASK, &set, &oset);
5039 host_to_target_old_sigset(&target_set, &oset);
5040 ret = target_set;
5041 }
5042 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005043#endif
thse5febef2007-04-01 18:31:35 +00005044#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00005045 case TARGET_NR_sigprocmask:
5046 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005047#if defined(TARGET_ALPHA)
5048 sigset_t set, oldset;
5049 abi_ulong mask;
5050 int how;
5051
5052 switch (arg1) {
5053 case TARGET_SIG_BLOCK:
5054 how = SIG_BLOCK;
5055 break;
5056 case TARGET_SIG_UNBLOCK:
5057 how = SIG_UNBLOCK;
5058 break;
5059 case TARGET_SIG_SETMASK:
5060 how = SIG_SETMASK;
5061 break;
5062 default:
5063 ret = -TARGET_EINVAL;
5064 goto fail;
5065 }
5066 mask = arg2;
5067 target_to_host_old_sigset(&set, &mask);
5068
5069 ret = get_errno(sigprocmask(how, &set, &oldset));
5070
5071 if (!is_error(ret)) {
5072 host_to_target_old_sigset(&mask, &oldset);
5073 ret = mask;
5074 ((CPUAlphaState *)cpu_env)->[IR_V0] = 0; /* force no error */
5075 }
5076#else
bellard66fb9762003-03-23 01:06:05 +00005077 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07005078 int how;
ths3b46e622007-09-17 08:09:54 +00005079
pbrook53a59602006-03-25 19:31:22 +00005080 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005081 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00005082 case TARGET_SIG_BLOCK:
5083 how = SIG_BLOCK;
5084 break;
5085 case TARGET_SIG_UNBLOCK:
5086 how = SIG_UNBLOCK;
5087 break;
5088 case TARGET_SIG_SETMASK:
5089 how = SIG_SETMASK;
5090 break;
5091 default:
ths0da46a62007-10-20 20:23:07 +00005092 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005093 goto fail;
5094 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005095 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005096 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005097 target_to_host_old_sigset(&set, p);
5098 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005099 set_ptr = &set;
5100 } else {
5101 how = 0;
5102 set_ptr = NULL;
5103 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005104 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005105 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005106 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005107 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005108 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005109 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005110 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005111#endif
bellard66fb9762003-03-23 01:06:05 +00005112 }
5113 break;
thse5febef2007-04-01 18:31:35 +00005114#endif
bellard66fb9762003-03-23 01:06:05 +00005115 case TARGET_NR_rt_sigprocmask:
5116 {
5117 int how = arg1;
5118 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00005119
pbrook53a59602006-03-25 19:31:22 +00005120 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005121 switch(how) {
5122 case TARGET_SIG_BLOCK:
5123 how = SIG_BLOCK;
5124 break;
5125 case TARGET_SIG_UNBLOCK:
5126 how = SIG_UNBLOCK;
5127 break;
5128 case TARGET_SIG_SETMASK:
5129 how = SIG_SETMASK;
5130 break;
5131 default:
ths0da46a62007-10-20 20:23:07 +00005132 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005133 goto fail;
5134 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005135 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005136 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005137 target_to_host_sigset(&set, p);
5138 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005139 set_ptr = &set;
5140 } else {
5141 how = 0;
5142 set_ptr = NULL;
5143 }
5144 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005145 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005146 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005147 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005148 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005149 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005150 }
5151 }
5152 break;
thse5febef2007-04-01 18:31:35 +00005153#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005154 case TARGET_NR_sigpending:
5155 {
5156 sigset_t set;
5157 ret = get_errno(sigpending(&set));
5158 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005159 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005160 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005161 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005162 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005163 }
5164 }
5165 break;
thse5febef2007-04-01 18:31:35 +00005166#endif
bellard66fb9762003-03-23 01:06:05 +00005167 case TARGET_NR_rt_sigpending:
5168 {
5169 sigset_t set;
5170 ret = get_errno(sigpending(&set));
5171 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005172 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005173 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005174 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005175 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005176 }
5177 }
5178 break;
thse5febef2007-04-01 18:31:35 +00005179#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00005180 case TARGET_NR_sigsuspend:
5181 {
5182 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07005183#if defined(TARGET_ALPHA)
5184 abi_ulong mask = arg1;
5185 target_to_host_old_sigset(&set, &mask);
5186#else
Anthony Liguoric227f092009-10-01 16:12:16 -05005187 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005188 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005189 target_to_host_old_sigset(&set, p);
5190 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07005191#endif
bellard66fb9762003-03-23 01:06:05 +00005192 ret = get_errno(sigsuspend(&set));
5193 }
5194 break;
thse5febef2007-04-01 18:31:35 +00005195#endif
bellard66fb9762003-03-23 01:06:05 +00005196 case TARGET_NR_rt_sigsuspend:
5197 {
5198 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005199 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005200 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005201 target_to_host_sigset(&set, p);
5202 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005203 ret = get_errno(sigsuspend(&set));
5204 }
5205 break;
5206 case TARGET_NR_rt_sigtimedwait:
5207 {
bellard66fb9762003-03-23 01:06:05 +00005208 sigset_t set;
5209 struct timespec uts, *puts;
5210 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00005211
Anthony Liguoric227f092009-10-01 16:12:16 -05005212 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005213 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005214 target_to_host_sigset(&set, p);
5215 unlock_user(p, arg1, 0);
5216 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00005217 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00005218 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00005219 } else {
5220 puts = NULL;
5221 }
5222 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00005223 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005224 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005225 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005226 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05005227 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00005228 }
5229 }
5230 break;
5231 case TARGET_NR_rt_sigqueueinfo:
5232 {
5233 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05005234 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005235 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005236 target_to_host_siginfo(&uinfo, p);
5237 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005238 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5239 }
5240 break;
thse5febef2007-04-01 18:31:35 +00005241#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00005242 case TARGET_NR_sigreturn:
5243 /* NOTE: ret is eax, so not transcoding must be done */
5244 ret = do_sigreturn(cpu_env);
5245 break;
thse5febef2007-04-01 18:31:35 +00005246#endif
bellard66fb9762003-03-23 01:06:05 +00005247 case TARGET_NR_rt_sigreturn:
5248 /* NOTE: ret is eax, so not transcoding must be done */
5249 ret = do_rt_sigreturn(cpu_env);
5250 break;
bellard31e31b82003-02-18 22:55:36 +00005251 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00005252 if (!(p = lock_user_string(arg1)))
5253 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005254 ret = get_errno(sethostname(p, arg2));
5255 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005256 break;
5257 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00005258 {
bellard9de5e442003-03-23 16:49:39 +00005259 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005260 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005261 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00005262 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5263 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005264 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5265 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005266 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00005267 ret = get_errno(setrlimit(resource, &rlim));
5268 }
5269 break;
bellard31e31b82003-02-18 22:55:36 +00005270 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00005271 {
bellard9de5e442003-03-23 16:49:39 +00005272 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005273 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005274 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00005275
bellard9de5e442003-03-23 16:49:39 +00005276 ret = get_errno(getrlimit(resource, &rlim));
5277 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005278 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5279 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005280 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5281 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005282 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00005283 }
5284 }
5285 break;
bellard31e31b82003-02-18 22:55:36 +00005286 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00005287 {
5288 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00005289 ret = get_errno(getrusage(arg1, &rusage));
5290 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005291 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00005292 }
5293 }
5294 break;
bellard31e31b82003-02-18 22:55:36 +00005295 case TARGET_NR_gettimeofday:
5296 {
bellard31e31b82003-02-18 22:55:36 +00005297 struct timeval tv;
5298 ret = get_errno(gettimeofday(&tv, NULL));
5299 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00005300 if (copy_to_user_timeval(arg1, &tv))
5301 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005302 }
5303 }
5304 break;
5305 case TARGET_NR_settimeofday:
5306 {
bellard31e31b82003-02-18 22:55:36 +00005307 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00005308 if (copy_from_user_timeval(&tv, arg1))
5309 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005310 ret = get_errno(settimeofday(&tv, NULL));
5311 }
5312 break;
bellard048f6b42005-11-26 18:47:20 +00005313#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00005314 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00005315 {
pbrook53a59602006-03-25 19:31:22 +00005316 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00005317 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00005318 long nsel;
5319
bellard579a97f2007-11-11 14:26:47 +00005320 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5321 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005322 nsel = tswapl(sel->n);
5323 inp = tswapl(sel->inp);
5324 outp = tswapl(sel->outp);
5325 exp = tswapl(sel->exp);
5326 tvp = tswapl(sel->tvp);
5327 unlock_user_struct(sel, arg1, 0);
5328 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00005329 }
5330 break;
bellard048f6b42005-11-26 18:47:20 +00005331#endif
Riku Voipio9e423822010-05-07 12:28:05 +00005332#ifdef TARGET_NR_pselect6
5333 case TARGET_NR_pselect6:
5334 goto unimplemented_nowarn;
5335#endif
bellard31e31b82003-02-18 22:55:36 +00005336 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00005337 {
5338 void *p2;
5339 p = lock_user_string(arg1);
5340 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005341 if (!p || !p2)
5342 ret = -TARGET_EFAULT;
5343 else
5344 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005345 unlock_user(p2, arg2, 0);
5346 unlock_user(p, arg1, 0);
5347 }
bellard31e31b82003-02-18 22:55:36 +00005348 break;
thsf0b62432007-09-24 09:25:40 +00005349#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5350 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00005351 {
bellard579a97f2007-11-11 14:26:47 +00005352 void *p2;
thsf0b62432007-09-24 09:25:40 +00005353 p = lock_user_string(arg1);
5354 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005355 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005356 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00005357 else
5358 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00005359 unlock_user(p2, arg3, 0);
5360 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00005361 }
5362 break;
5363#endif
bellardebc05482003-09-30 21:08:41 +00005364#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00005365 case TARGET_NR_oldlstat:
5366 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005367#endif
bellard31e31b82003-02-18 22:55:36 +00005368 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00005369 {
aurel32d088d662009-01-30 20:09:01 +00005370 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00005371 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00005372 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5373 if (!p || !p2)
5374 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00005375 else {
5376 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5377 char real[PATH_MAX];
5378 temp = realpath(exec_path,real);
5379 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5380 snprintf((char *)p2, arg3, "%s", real);
5381 }
5382 else
5383 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00005384 }
pbrook53a59602006-03-25 19:31:22 +00005385 unlock_user(p2, arg2, ret);
5386 unlock_user(p, arg1, 0);
5387 }
bellard31e31b82003-02-18 22:55:36 +00005388 break;
ths5e0ccb12007-09-24 09:26:10 +00005389#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5390 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00005391 {
bellard579a97f2007-11-11 14:26:47 +00005392 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00005393 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005394 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5395 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005396 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00005397 else
5398 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00005399 unlock_user(p2, arg3, ret);
5400 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00005401 }
5402 break;
5403#endif
thse5febef2007-04-01 18:31:35 +00005404#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00005405 case TARGET_NR_uselib:
5406 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005407#endif
5408#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00005409 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00005410 if (!(p = lock_user_string(arg1)))
5411 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005412 ret = get_errno(swapon(p, arg2));
5413 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005414 break;
thse5febef2007-04-01 18:31:35 +00005415#endif
bellard31e31b82003-02-18 22:55:36 +00005416 case TARGET_NR_reboot:
5417 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005418#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00005419 case TARGET_NR_readdir:
5420 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005421#endif
5422#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00005423 case TARGET_NR_mmap:
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02005424#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 +00005425 {
blueswir1992f48a2007-10-14 16:27:31 +00005426 abi_ulong *v;
5427 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00005428 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5429 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005430 v1 = tswapl(v[0]);
5431 v2 = tswapl(v[1]);
5432 v3 = tswapl(v[2]);
5433 v4 = tswapl(v[3]);
5434 v5 = tswapl(v[4]);
5435 v6 = tswapl(v[5]);
5436 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00005437 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00005438 target_to_host_bitmask(v4, mmap_flags_tbl),
5439 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00005440 }
bellard31e31b82003-02-18 22:55:36 +00005441#else
ths5fafdf22007-09-16 21:08:06 +00005442 ret = get_errno(target_mmap(arg1, arg2, arg3,
5443 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00005444 arg5,
5445 arg6));
bellard31e31b82003-02-18 22:55:36 +00005446#endif
bellard6fb883e2003-07-09 17:12:39 +00005447 break;
thse5febef2007-04-01 18:31:35 +00005448#endif
bellarda315a142005-01-30 22:59:18 +00005449#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00005450 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00005451#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00005452#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00005453#endif
ths5fafdf22007-09-16 21:08:06 +00005454 ret = get_errno(target_mmap(arg1, arg2, arg3,
5455 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00005456 arg5,
bellardc573ff62004-01-04 15:51:36 +00005457 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00005458 break;
bellarda315a142005-01-30 22:59:18 +00005459#endif
bellard31e31b82003-02-18 22:55:36 +00005460 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00005461 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005462 break;
bellard9de5e442003-03-23 16:49:39 +00005463 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01005464 {
5465 TaskState *ts = ((CPUState *)cpu_env)->opaque;
5466 /* Special hack to detect libc making the stack executable. */
5467 if ((arg3 & PROT_GROWSDOWN)
5468 && arg1 >= ts->info->stack_limit
5469 && arg1 <= ts->info->start_stack) {
5470 arg3 &= ~PROT_GROWSDOWN;
5471 arg2 = arg2 + arg1 - ts->info->stack_limit;
5472 arg1 = ts->info->stack_limit;
5473 }
5474 }
bellard54936002003-05-13 00:25:15 +00005475 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005476 break;
thse5febef2007-04-01 18:31:35 +00005477#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00005478 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00005479 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00005480 break;
thse5febef2007-04-01 18:31:35 +00005481#endif
pbrook53a59602006-03-25 19:31:22 +00005482 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00005483#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00005484 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00005485 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005486 break;
thse5febef2007-04-01 18:31:35 +00005487#endif
5488#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00005489 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00005490 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005491 break;
thse5febef2007-04-01 18:31:35 +00005492#endif
5493#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00005494 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00005495 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005496 break;
thse5febef2007-04-01 18:31:35 +00005497#endif
5498#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00005499 case TARGET_NR_mlockall:
5500 ret = get_errno(mlockall(arg1));
5501 break;
thse5febef2007-04-01 18:31:35 +00005502#endif
5503#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00005504 case TARGET_NR_munlockall:
5505 ret = get_errno(munlockall());
5506 break;
thse5febef2007-04-01 18:31:35 +00005507#endif
bellard31e31b82003-02-18 22:55:36 +00005508 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00005509 if (!(p = lock_user_string(arg1)))
5510 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005511 ret = get_errno(truncate(p, arg2));
5512 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005513 break;
5514 case TARGET_NR_ftruncate:
5515 ret = get_errno(ftruncate(arg1, arg2));
5516 break;
5517 case TARGET_NR_fchmod:
5518 ret = get_errno(fchmod(arg1, arg2));
5519 break;
ths814d7972007-09-24 09:26:51 +00005520#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5521 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00005522 if (!(p = lock_user_string(arg2)))
5523 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005524 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005525 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00005526 break;
5527#endif
bellard31e31b82003-02-18 22:55:36 +00005528 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00005529 /* libc does special remapping of the return value of
5530 * sys_getpriority() so it's just easiest to call
5531 * sys_getpriority() directly rather than through libc. */
Ulrich Hecht69137202009-09-17 21:08:37 +03005532 ret = get_errno(sys_getpriority(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005533 break;
5534 case TARGET_NR_setpriority:
5535 ret = get_errno(setpriority(arg1, arg2, arg3));
5536 break;
bellardebc05482003-09-30 21:08:41 +00005537#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00005538 case TARGET_NR_profil:
5539 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005540#endif
bellard31e31b82003-02-18 22:55:36 +00005541 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00005542 if (!(p = lock_user_string(arg1)))
5543 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005544 ret = get_errno(statfs(path(p), &stfs));
5545 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005546 convert_statfs:
5547 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005548 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005549
bellard579a97f2007-11-11 14:26:47 +00005550 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5551 goto efault;
5552 __put_user(stfs.f_type, &target_stfs->f_type);
5553 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5554 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5555 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5556 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5557 __put_user(stfs.f_files, &target_stfs->f_files);
5558 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5559 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5560 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5561 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00005562 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005563 }
5564 break;
5565 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00005566 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00005567 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00005568#ifdef TARGET_NR_statfs64
5569 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00005570 if (!(p = lock_user_string(arg1)))
5571 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005572 ret = get_errno(statfs(path(p), &stfs));
5573 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00005574 convert_statfs64:
5575 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005576 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005577
bellard579a97f2007-11-11 14:26:47 +00005578 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5579 goto efault;
5580 __put_user(stfs.f_type, &target_stfs->f_type);
5581 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5582 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5583 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5584 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5585 __put_user(stfs.f_files, &target_stfs->f_files);
5586 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5587 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5588 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5589 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5590 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00005591 }
5592 break;
5593 case TARGET_NR_fstatfs64:
5594 ret = get_errno(fstatfs(arg1, &stfs));
5595 goto convert_statfs64;
5596#endif
bellardebc05482003-09-30 21:08:41 +00005597#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00005598 case TARGET_NR_ioperm:
5599 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005600#endif
thse5febef2007-04-01 18:31:35 +00005601#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00005602 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00005603 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00005604 break;
thse5febef2007-04-01 18:31:35 +00005605#endif
bellard3532fa72006-06-24 15:06:03 +00005606#ifdef TARGET_NR_accept
5607 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00005608 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005609 break;
5610#endif
5611#ifdef TARGET_NR_bind
5612 case TARGET_NR_bind:
5613 ret = do_bind(arg1, arg2, arg3);
5614 break;
5615#endif
5616#ifdef TARGET_NR_connect
5617 case TARGET_NR_connect:
5618 ret = do_connect(arg1, arg2, arg3);
5619 break;
5620#endif
5621#ifdef TARGET_NR_getpeername
5622 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00005623 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005624 break;
5625#endif
5626#ifdef TARGET_NR_getsockname
5627 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00005628 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005629 break;
5630#endif
5631#ifdef TARGET_NR_getsockopt
5632 case TARGET_NR_getsockopt:
5633 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5634 break;
5635#endif
5636#ifdef TARGET_NR_listen
5637 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00005638 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005639 break;
5640#endif
5641#ifdef TARGET_NR_recv
5642 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00005643 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005644 break;
5645#endif
5646#ifdef TARGET_NR_recvfrom
5647 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00005648 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005649 break;
5650#endif
5651#ifdef TARGET_NR_recvmsg
5652 case TARGET_NR_recvmsg:
5653 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5654 break;
5655#endif
5656#ifdef TARGET_NR_send
5657 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00005658 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005659 break;
5660#endif
5661#ifdef TARGET_NR_sendmsg
5662 case TARGET_NR_sendmsg:
5663 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5664 break;
5665#endif
5666#ifdef TARGET_NR_sendto
5667 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00005668 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005669 break;
5670#endif
5671#ifdef TARGET_NR_shutdown
5672 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00005673 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005674 break;
5675#endif
5676#ifdef TARGET_NR_socket
5677 case TARGET_NR_socket:
5678 ret = do_socket(arg1, arg2, arg3);
5679 break;
5680#endif
5681#ifdef TARGET_NR_socketpair
5682 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00005683 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00005684 break;
5685#endif
5686#ifdef TARGET_NR_setsockopt
5687 case TARGET_NR_setsockopt:
5688 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5689 break;
5690#endif
ths7494b0f2007-02-11 18:26:53 +00005691
bellard31e31b82003-02-18 22:55:36 +00005692 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00005693 if (!(p = lock_user_string(arg2)))
5694 goto efault;
thse5574482007-02-11 20:03:13 +00005695 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5696 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00005697 break;
5698
bellard31e31b82003-02-18 22:55:36 +00005699 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00005700 {
bellard66fb9762003-03-23 01:06:05 +00005701 struct itimerval value, ovalue, *pvalue;
5702
pbrook53a59602006-03-25 19:31:22 +00005703 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005704 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00005705 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5706 || copy_from_user_timeval(&pvalue->it_value,
5707 arg2 + sizeof(struct target_timeval)))
5708 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005709 } else {
5710 pvalue = NULL;
5711 }
5712 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00005713 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00005714 if (copy_to_user_timeval(arg3,
5715 &ovalue.it_interval)
5716 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5717 &ovalue.it_value))
5718 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005719 }
5720 }
5721 break;
bellard31e31b82003-02-18 22:55:36 +00005722 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00005723 {
bellard66fb9762003-03-23 01:06:05 +00005724 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00005725
bellard66fb9762003-03-23 01:06:05 +00005726 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00005727 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00005728 if (copy_to_user_timeval(arg2,
5729 &value.it_interval)
5730 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5731 &value.it_value))
5732 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005733 }
5734 }
5735 break;
bellard31e31b82003-02-18 22:55:36 +00005736 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00005737 if (!(p = lock_user_string(arg1)))
5738 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005739 ret = get_errno(stat(path(p), &st));
5740 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005741 goto do_stat;
5742 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00005743 if (!(p = lock_user_string(arg1)))
5744 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005745 ret = get_errno(lstat(path(p), &st));
5746 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005747 goto do_stat;
5748 case TARGET_NR_fstat:
5749 {
5750 ret = get_errno(fstat(arg1, &st));
5751 do_stat:
5752 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005753 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00005754
bellard579a97f2007-11-11 14:26:47 +00005755 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5756 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02005757 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00005758 __put_user(st.st_dev, &target_st->st_dev);
5759 __put_user(st.st_ino, &target_st->st_ino);
5760 __put_user(st.st_mode, &target_st->st_mode);
5761 __put_user(st.st_uid, &target_st->st_uid);
5762 __put_user(st.st_gid, &target_st->st_gid);
5763 __put_user(st.st_nlink, &target_st->st_nlink);
5764 __put_user(st.st_rdev, &target_st->st_rdev);
5765 __put_user(st.st_size, &target_st->st_size);
5766 __put_user(st.st_blksize, &target_st->st_blksize);
5767 __put_user(st.st_blocks, &target_st->st_blocks);
5768 __put_user(st.st_atime, &target_st->target_st_atime);
5769 __put_user(st.st_mtime, &target_st->target_st_mtime);
5770 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00005771 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005772 }
5773 }
5774 break;
bellardebc05482003-09-30 21:08:41 +00005775#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00005776 case TARGET_NR_olduname:
5777 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005778#endif
5779#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00005780 case TARGET_NR_iopl:
5781 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005782#endif
bellard31e31b82003-02-18 22:55:36 +00005783 case TARGET_NR_vhangup:
5784 ret = get_errno(vhangup());
5785 break;
bellardebc05482003-09-30 21:08:41 +00005786#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00005787 case TARGET_NR_idle:
5788 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005789#endif
bellard42ad6ae2005-01-03 22:48:11 +00005790#ifdef TARGET_NR_syscall
5791 case TARGET_NR_syscall:
5792 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5793 break;
5794#endif
bellard31e31b82003-02-18 22:55:36 +00005795 case TARGET_NR_wait4:
5796 {
5797 int status;
blueswir1992f48a2007-10-14 16:27:31 +00005798 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00005799 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00005800 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00005801 if (target_rusage)
5802 rusage_ptr = &rusage;
5803 else
5804 rusage_ptr = NULL;
5805 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5806 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005807 if (status_ptr) {
pbrook1d9d8b52009-04-16 15:17:02 +00005808 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00005809 if (put_user_s32(status, status_ptr))
5810 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005811 }
bellard2f619692007-11-16 10:46:05 +00005812 if (target_rusage)
5813 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00005814 }
5815 }
5816 break;
thse5febef2007-04-01 18:31:35 +00005817#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00005818 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00005819 if (!(p = lock_user_string(arg1)))
5820 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005821 ret = get_errno(swapoff(p));
5822 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005823 break;
thse5febef2007-04-01 18:31:35 +00005824#endif
bellard31e31b82003-02-18 22:55:36 +00005825 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00005826 {
pbrook53a59602006-03-25 19:31:22 +00005827 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00005828 struct sysinfo value;
5829 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00005830 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00005831 {
bellard579a97f2007-11-11 14:26:47 +00005832 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5833 goto efault;
bellarda5448a72004-06-19 16:59:03 +00005834 __put_user(value.uptime, &target_value->uptime);
5835 __put_user(value.loads[0], &target_value->loads[0]);
5836 __put_user(value.loads[1], &target_value->loads[1]);
5837 __put_user(value.loads[2], &target_value->loads[2]);
5838 __put_user(value.totalram, &target_value->totalram);
5839 __put_user(value.freeram, &target_value->freeram);
5840 __put_user(value.sharedram, &target_value->sharedram);
5841 __put_user(value.bufferram, &target_value->bufferram);
5842 __put_user(value.totalswap, &target_value->totalswap);
5843 __put_user(value.freeswap, &target_value->freeswap);
5844 __put_user(value.procs, &target_value->procs);
5845 __put_user(value.totalhigh, &target_value->totalhigh);
5846 __put_user(value.freehigh, &target_value->freehigh);
5847 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00005848 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00005849 }
5850 }
5851 break;
thse5febef2007-04-01 18:31:35 +00005852#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00005853 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00005854 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5855 break;
thse5febef2007-04-01 18:31:35 +00005856#endif
aurel32e5289082009-04-18 16:16:12 +00005857#ifdef TARGET_NR_semget
5858 case TARGET_NR_semget:
5859 ret = get_errno(semget(arg1, arg2, arg3));
5860 break;
5861#endif
5862#ifdef TARGET_NR_semop
5863 case TARGET_NR_semop:
5864 ret = get_errno(do_semop(arg1, arg2, arg3));
5865 break;
5866#endif
5867#ifdef TARGET_NR_semctl
5868 case TARGET_NR_semctl:
5869 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5870 break;
5871#endif
aurel32eeb438c2008-10-13 21:08:55 +00005872#ifdef TARGET_NR_msgctl
5873 case TARGET_NR_msgctl:
5874 ret = do_msgctl(arg1, arg2, arg3);
5875 break;
5876#endif
5877#ifdef TARGET_NR_msgget
5878 case TARGET_NR_msgget:
5879 ret = get_errno(msgget(arg1, arg2));
5880 break;
5881#endif
5882#ifdef TARGET_NR_msgrcv
5883 case TARGET_NR_msgrcv:
5884 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5885 break;
5886#endif
5887#ifdef TARGET_NR_msgsnd
5888 case TARGET_NR_msgsnd:
5889 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5890 break;
5891#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03005892#ifdef TARGET_NR_shmget
5893 case TARGET_NR_shmget:
5894 ret = get_errno(shmget(arg1, arg2, arg3));
5895 break;
5896#endif
5897#ifdef TARGET_NR_shmctl
5898 case TARGET_NR_shmctl:
5899 ret = do_shmctl(arg1, arg2, arg3);
5900 break;
5901#endif
5902#ifdef TARGET_NR_shmat
5903 case TARGET_NR_shmat:
5904 ret = do_shmat(arg1, arg2, arg3);
5905 break;
5906#endif
5907#ifdef TARGET_NR_shmdt
5908 case TARGET_NR_shmdt:
5909 ret = do_shmdt(arg1);
5910 break;
5911#endif
bellard31e31b82003-02-18 22:55:36 +00005912 case TARGET_NR_fsync:
5913 ret = get_errno(fsync(arg1));
5914 break;
bellard31e31b82003-02-18 22:55:36 +00005915 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07005916#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00005917 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00005918#elif defined(TARGET_CRIS)
5919 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005920#else
pbrookd865bab2008-06-07 22:12:17 +00005921 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005922#endif
bellard1b6b0292003-03-22 17:31:38 +00005923 break;
bellardec86b0f2003-04-11 00:15:04 +00005924#ifdef __NR_exit_group
5925 /* new thread calls */
5926 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02005927#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00005928 _mcleanup();
5929#endif
bellarde9009672005-04-26 20:42:36 +00005930 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00005931 ret = get_errno(exit_group(arg1));
5932 break;
5933#endif
bellard31e31b82003-02-18 22:55:36 +00005934 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00005935 if (!(p = lock_user_string(arg1)))
5936 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005937 ret = get_errno(setdomainname(p, arg2));
5938 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005939 break;
5940 case TARGET_NR_uname:
5941 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00005942 {
5943 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00005944
bellard579a97f2007-11-11 14:26:47 +00005945 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5946 goto efault;
bellard29e619b2004-09-13 21:41:04 +00005947 ret = get_errno(sys_uname(buf));
5948 if (!is_error(ret)) {
5949 /* Overrite the native machine name with whatever is being
5950 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01005951 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00005952 /* Allow the user to override the reported release. */
5953 if (qemu_uname_release && *qemu_uname_release)
5954 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00005955 }
pbrook53a59602006-03-25 19:31:22 +00005956 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00005957 }
bellard31e31b82003-02-18 22:55:36 +00005958 break;
bellard6dbad632003-03-16 18:05:05 +00005959#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00005960 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00005961 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00005962 break;
j_mayer84409dd2007-04-06 08:56:50 +00005963#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00005964 case TARGET_NR_vm86old:
5965 goto unimplemented;
5966 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00005967 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00005968 break;
5969#endif
j_mayer84409dd2007-04-06 08:56:50 +00005970#endif
bellard31e31b82003-02-18 22:55:36 +00005971 case TARGET_NR_adjtimex:
5972 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005973#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00005974 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00005975#endif
bellard31e31b82003-02-18 22:55:36 +00005976 case TARGET_NR_init_module:
5977 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00005978#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00005979 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00005980#endif
bellard31e31b82003-02-18 22:55:36 +00005981 goto unimplemented;
5982 case TARGET_NR_quotactl:
5983 goto unimplemented;
5984 case TARGET_NR_getpgid:
5985 ret = get_errno(getpgid(arg1));
5986 break;
5987 case TARGET_NR_fchdir:
5988 ret = get_errno(fchdir(arg1));
5989 break;
j_mayer84409dd2007-04-06 08:56:50 +00005990#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00005991 case TARGET_NR_bdflush:
5992 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00005993#endif
thse5febef2007-04-01 18:31:35 +00005994#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00005995 case TARGET_NR_sysfs:
5996 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005997#endif
bellard31e31b82003-02-18 22:55:36 +00005998 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00005999 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00006000 break;
thse5febef2007-04-01 18:31:35 +00006001#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00006002 case TARGET_NR_afs_syscall:
6003 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006004#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006005#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006006 case TARGET_NR__llseek:
6007 {
Richard Hendersond35b2612010-06-04 12:14:10 -07006008#if !defined(__NR_llseek)
bellard4f2ac232004-04-26 19:44:02 +00006009 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00006010 if (put_user_s64(ret, arg4))
6011 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00006012#else
bellard31e31b82003-02-18 22:55:36 +00006013 int64_t res;
6014 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00006015 if (put_user_s64(res, arg4))
6016 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00006017#endif
bellard31e31b82003-02-18 22:55:36 +00006018 }
6019 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006020#endif
bellard31e31b82003-02-18 22:55:36 +00006021 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02006022#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00006023 {
pbrook53a59602006-03-25 19:31:22 +00006024 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00006025 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006026 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00006027
6028 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00006029 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00006030 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00006031 goto fail;
6032 }
ths3b46e622007-09-17 08:09:54 +00006033
bellard4add45b2003-06-05 01:52:59 +00006034 ret = get_errno(sys_getdents(arg1, dirp, count));
6035 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006036 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00006037 struct target_dirent *tde;
6038 int len = ret;
6039 int reclen, treclen;
6040 int count1, tnamelen;
6041
6042 count1 = 0;
6043 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00006044 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6045 goto efault;
bellard4add45b2003-06-05 01:52:59 +00006046 tde = target_dirp;
6047 while (len > 0) {
6048 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00006049 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00006050 tde->d_reclen = tswap16(treclen);
6051 tde->d_ino = tswapl(de->d_ino);
6052 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00006053 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00006054 if (tnamelen > 256)
6055 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00006056 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00006057 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00006058 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00006059 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00006060 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00006061 count1 += treclen;
6062 }
6063 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00006064 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00006065 }
6066 free(dirp);
6067 }
6068#else
bellard31e31b82003-02-18 22:55:36 +00006069 {
aurel326556a832008-10-13 21:08:17 +00006070 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006071 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00006072
bellard579a97f2007-11-11 14:26:47 +00006073 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6074 goto efault;
bellard72f03902003-02-18 23:33:18 +00006075 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00006076 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006077 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00006078 int len = ret;
6079 int reclen;
6080 de = dirp;
6081 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006082 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00006083 if (reclen > len)
6084 break;
bellard8083a3e2003-03-24 23:12:16 +00006085 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00006086 tswapls(&de->d_ino);
6087 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006088 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00006089 len -= reclen;
6090 }
6091 }
pbrook53a59602006-03-25 19:31:22 +00006092 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00006093 }
bellard4add45b2003-06-05 01:52:59 +00006094#endif
bellard31e31b82003-02-18 22:55:36 +00006095 break;
ths3ae43202007-09-16 21:39:48 +00006096#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00006097 case TARGET_NR_getdents64:
6098 {
aurel326556a832008-10-13 21:08:17 +00006099 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006100 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00006101 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6102 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00006103 ret = get_errno(sys_getdents64(arg1, dirp, count));
6104 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006105 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00006106 int len = ret;
6107 int reclen;
6108 de = dirp;
6109 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006110 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00006111 if (reclen > len)
6112 break;
bellard8083a3e2003-03-24 23:12:16 +00006113 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00006114 tswap64s((uint64_t *)&de->d_ino);
6115 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006116 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00006117 len -= reclen;
6118 }
6119 }
pbrook53a59602006-03-25 19:31:22 +00006120 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00006121 }
6122 break;
bellarda541f292004-04-12 20:39:29 +00006123#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00006124#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00006125 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00006126 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00006127 break;
thse5febef2007-04-01 18:31:35 +00006128#endif
6129#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00006130 case TARGET_NR_poll:
6131 {
pbrook53a59602006-03-25 19:31:22 +00006132 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00006133 unsigned int nfds = arg2;
6134 int timeout = arg3;
6135 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00006136 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00006137
bellard579a97f2007-11-11 14:26:47 +00006138 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6139 if (!target_pfd)
6140 goto efault;
bellard9de5e442003-03-23 16:49:39 +00006141 pfd = alloca(sizeof(struct pollfd) * nfds);
6142 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006143 pfd[i].fd = tswap32(target_pfd[i].fd);
6144 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00006145 }
6146 ret = get_errno(poll(pfd, nfds, timeout));
6147 if (!is_error(ret)) {
6148 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006149 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00006150 }
pbrook53a59602006-03-25 19:31:22 +00006151 ret += nfds * (sizeof(struct target_pollfd)
6152 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00006153 }
pbrook53a59602006-03-25 19:31:22 +00006154 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00006155 }
6156 break;
thse5febef2007-04-01 18:31:35 +00006157#endif
bellard31e31b82003-02-18 22:55:36 +00006158 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00006159 /* NOTE: the flock constant seems to be the same for every
6160 Linux platform */
6161 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006162 break;
6163 case TARGET_NR_readv:
6164 {
6165 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006166 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006167
6168 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006169 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6170 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006171 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006172 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00006173 }
6174 break;
6175 case TARGET_NR_writev:
6176 {
6177 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006178 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006179
6180 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006181 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6182 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006183 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006184 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00006185 }
6186 break;
6187 case TARGET_NR_getsid:
6188 ret = get_errno(getsid(arg1));
6189 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006190#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00006191 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00006192 ret = get_errno(fdatasync(arg1));
6193 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006194#endif
bellard31e31b82003-02-18 22:55:36 +00006195 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00006196 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00006197 return value. */
ths0da46a62007-10-20 20:23:07 +00006198 ret = -TARGET_ENOTDIR;
6199 break;
bellard31e31b82003-02-18 22:55:36 +00006200 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00006201 {
pbrook53a59602006-03-25 19:31:22 +00006202 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006203 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00006204
bellard579a97f2007-11-11 14:26:47 +00006205 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6206 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006207 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006208 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00006209 ret = get_errno(sched_setparam(arg1, &schp));
6210 }
6211 break;
bellard31e31b82003-02-18 22:55:36 +00006212 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00006213 {
pbrook53a59602006-03-25 19:31:22 +00006214 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006215 struct sched_param schp;
6216 ret = get_errno(sched_getparam(arg1, &schp));
6217 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006218 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6219 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006220 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006221 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00006222 }
6223 }
6224 break;
bellard31e31b82003-02-18 22:55:36 +00006225 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00006226 {
pbrook53a59602006-03-25 19:31:22 +00006227 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006228 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00006229 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6230 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006231 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006232 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00006233 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6234 }
6235 break;
bellard31e31b82003-02-18 22:55:36 +00006236 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00006237 ret = get_errno(sched_getscheduler(arg1));
6238 break;
bellard31e31b82003-02-18 22:55:36 +00006239 case TARGET_NR_sched_yield:
6240 ret = get_errno(sched_yield());
6241 break;
6242 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00006243 ret = get_errno(sched_get_priority_max(arg1));
6244 break;
bellard31e31b82003-02-18 22:55:36 +00006245 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00006246 ret = get_errno(sched_get_priority_min(arg1));
6247 break;
bellard31e31b82003-02-18 22:55:36 +00006248 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00006249 {
bellard5cd43932003-03-29 16:54:36 +00006250 struct timespec ts;
6251 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6252 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006253 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00006254 }
6255 }
6256 break;
bellard31e31b82003-02-18 22:55:36 +00006257 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00006258 {
bellard1b6b0292003-03-22 17:31:38 +00006259 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00006260 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00006261 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00006262 if (is_error(ret) && arg2) {
6263 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00006264 }
6265 }
6266 break;
thse5febef2007-04-01 18:31:35 +00006267#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00006268 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00006269 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006270#endif
6271#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00006272 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00006273 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006274#endif
bellard31e31b82003-02-18 22:55:36 +00006275 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00006276 switch (arg1)
6277 {
6278 case PR_GET_PDEATHSIG:
6279 {
6280 int deathsig;
6281 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00006282 if (!is_error(ret) && arg2
6283 && put_user_ual(deathsig, arg2))
6284 goto efault;
thse5574482007-02-11 20:03:13 +00006285 }
6286 break;
6287 default:
6288 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6289 break;
6290 }
ths39b9aae2007-02-11 18:36:44 +00006291 break;
bellardd2fd1af2007-11-14 18:08:56 +00006292#ifdef TARGET_NR_arch_prctl
6293 case TARGET_NR_arch_prctl:
6294#if defined(TARGET_I386) && !defined(TARGET_ABI32)
6295 ret = do_arch_prctl(cpu_env, arg1, arg2);
6296 break;
6297#else
6298 goto unimplemented;
6299#endif
6300#endif
bellard67867302003-11-23 17:05:30 +00006301#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00006302 case TARGET_NR_pread:
balroga4ae00b2008-09-20 03:14:14 +00006303#ifdef TARGET_ARM
6304 if (((CPUARMState *)cpu_env)->eabi)
6305 arg4 = arg5;
6306#endif
bellard579a97f2007-11-11 14:26:47 +00006307 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6308 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006309 ret = get_errno(pread(arg1, p, arg3, arg4));
6310 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00006311 break;
bellard31e31b82003-02-18 22:55:36 +00006312 case TARGET_NR_pwrite:
balroga4ae00b2008-09-20 03:14:14 +00006313#ifdef TARGET_ARM
6314 if (((CPUARMState *)cpu_env)->eabi)
6315 arg4 = arg5;
6316#endif
bellard579a97f2007-11-11 14:26:47 +00006317 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6318 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006319 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6320 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00006321 break;
bellard67867302003-11-23 17:05:30 +00006322#endif
aurel32f2c7ba12008-03-28 22:32:06 +00006323#ifdef TARGET_NR_pread64
6324 case TARGET_NR_pread64:
6325 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6326 goto efault;
6327 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6328 unlock_user(p, arg2, ret);
6329 break;
6330 case TARGET_NR_pwrite64:
6331 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6332 goto efault;
6333 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6334 unlock_user(p, arg2, 0);
6335 break;
6336#endif
bellard31e31b82003-02-18 22:55:36 +00006337 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00006338 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6339 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006340 ret = get_errno(sys_getcwd1(p, arg2));
6341 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00006342 break;
6343 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00006344 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006345 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00006346 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006347 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00006348#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02006349 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6350 defined(TARGET_M68K)
bellard579a97f2007-11-11 14:26:47 +00006351 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00006352 break;
6353#else
bellard5cd43932003-03-29 16:54:36 +00006354 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00006355#endif
bellard31e31b82003-02-18 22:55:36 +00006356 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00006357 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006358#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00006359 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00006360 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006361#endif
6362#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00006363 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00006364 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006365#endif
bellard048f6b42005-11-26 18:47:20 +00006366#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00006367 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00006368 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6369 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00006370 break;
bellard048f6b42005-11-26 18:47:20 +00006371#endif
bellardebc05482003-09-30 21:08:41 +00006372#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00006373 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00006374 {
6375 struct rlimit rlim;
6376 ret = get_errno(getrlimit(arg1, &rlim));
6377 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006378 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00006379 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6380 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006381 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6382 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006383 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00006384 }
6385 break;
6386 }
bellardebc05482003-09-30 21:08:41 +00006387#endif
bellarda315a142005-01-30 22:59:18 +00006388#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00006389 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00006390 if (!(p = lock_user_string(arg1)))
6391 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006392 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6393 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00006394 break;
bellarda315a142005-01-30 22:59:18 +00006395#endif
6396#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00006397 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00006398 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00006399 break;
bellarda315a142005-01-30 22:59:18 +00006400#endif
6401#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00006402 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00006403 if (!(p = lock_user_string(arg1)))
6404 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006405 ret = get_errno(stat(path(p), &st));
6406 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006407 if (!is_error(ret))
6408 ret = host_to_target_stat64(cpu_env, arg2, &st);
6409 break;
bellarda315a142005-01-30 22:59:18 +00006410#endif
6411#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00006412 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00006413 if (!(p = lock_user_string(arg1)))
6414 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006415 ret = get_errno(lstat(path(p), &st));
6416 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006417 if (!is_error(ret))
6418 ret = host_to_target_stat64(cpu_env, arg2, &st);
6419 break;
bellarda315a142005-01-30 22:59:18 +00006420#endif
6421#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00006422 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00006423 ret = get_errno(fstat(arg1, &st));
6424 if (!is_error(ret))
6425 ret = host_to_target_stat64(cpu_env, arg2, &st);
6426 break;
bellardec86b0f2003-04-11 00:15:04 +00006427#endif
aurel329d33b762009-04-08 23:07:05 +00006428#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6429 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6430#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006431 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00006432#endif
6433#ifdef TARGET_NR_newfstatat
6434 case TARGET_NR_newfstatat:
6435#endif
balrog6a24a772008-09-20 02:23:36 +00006436 if (!(p = lock_user_string(arg2)))
6437 goto efault;
aurel329d33b762009-04-08 23:07:05 +00006438#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006439 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00006440#else
6441 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6442#endif
balrog6a24a772008-09-20 02:23:36 +00006443 if (!is_error(ret))
6444 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00006445 break;
bellarda315a142005-01-30 22:59:18 +00006446#endif
bellard67867302003-11-23 17:05:30 +00006447#ifdef USE_UID16
6448 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00006449 if (!(p = lock_user_string(arg1)))
6450 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006451 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6452 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006453 break;
6454 case TARGET_NR_getuid:
6455 ret = get_errno(high2lowuid(getuid()));
6456 break;
6457 case TARGET_NR_getgid:
6458 ret = get_errno(high2lowgid(getgid()));
6459 break;
6460 case TARGET_NR_geteuid:
6461 ret = get_errno(high2lowuid(geteuid()));
6462 break;
6463 case TARGET_NR_getegid:
6464 ret = get_errno(high2lowgid(getegid()));
6465 break;
6466 case TARGET_NR_setreuid:
6467 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6468 break;
6469 case TARGET_NR_setregid:
6470 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6471 break;
6472 case TARGET_NR_getgroups:
6473 {
6474 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006475 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006476 gid_t *grouplist;
6477 int i;
6478
6479 grouplist = alloca(gidsetsize * sizeof(gid_t));
6480 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006481 if (gidsetsize == 0)
6482 break;
bellard67867302003-11-23 17:05:30 +00006483 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006484 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6485 if (!target_grouplist)
6486 goto efault;
balroga2155fc2008-09-20 02:12:08 +00006487 for(i = 0;i < ret; i++)
bellard67867302003-11-23 17:05:30 +00006488 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006489 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00006490 }
6491 }
6492 break;
6493 case TARGET_NR_setgroups:
6494 {
6495 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006496 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006497 gid_t *grouplist;
6498 int i;
6499
6500 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006501 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6502 if (!target_grouplist) {
6503 ret = -TARGET_EFAULT;
6504 goto fail;
6505 }
bellard67867302003-11-23 17:05:30 +00006506 for(i = 0;i < gidsetsize; i++)
6507 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006508 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00006509 ret = get_errno(setgroups(gidsetsize, grouplist));
6510 }
6511 break;
6512 case TARGET_NR_fchown:
6513 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6514 break;
thsccfa72b2007-09-24 09:23:34 +00006515#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6516 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00006517 if (!(p = lock_user_string(arg2)))
6518 goto efault;
6519 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6520 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00006521 break;
6522#endif
bellard67867302003-11-23 17:05:30 +00006523#ifdef TARGET_NR_setresuid
6524 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00006525 ret = get_errno(setresuid(low2highuid(arg1),
6526 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00006527 low2highuid(arg3)));
6528 break;
6529#endif
6530#ifdef TARGET_NR_getresuid
6531 case TARGET_NR_getresuid:
6532 {
pbrook53a59602006-03-25 19:31:22 +00006533 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00006534 ret = get_errno(getresuid(&ruid, &euid, &suid));
6535 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006536 if (put_user_u16(high2lowuid(ruid), arg1)
6537 || put_user_u16(high2lowuid(euid), arg2)
6538 || put_user_u16(high2lowuid(suid), arg3))
6539 goto efault;
bellard67867302003-11-23 17:05:30 +00006540 }
6541 }
6542 break;
6543#endif
6544#ifdef TARGET_NR_getresgid
6545 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00006546 ret = get_errno(setresgid(low2highgid(arg1),
6547 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00006548 low2highgid(arg3)));
6549 break;
6550#endif
6551#ifdef TARGET_NR_getresgid
6552 case TARGET_NR_getresgid:
6553 {
pbrook53a59602006-03-25 19:31:22 +00006554 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00006555 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6556 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006557 if (put_user_u16(high2lowgid(rgid), arg1)
6558 || put_user_u16(high2lowgid(egid), arg2)
6559 || put_user_u16(high2lowgid(sgid), arg3))
6560 goto efault;
bellard67867302003-11-23 17:05:30 +00006561 }
6562 }
6563 break;
6564#endif
6565 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00006566 if (!(p = lock_user_string(arg1)))
6567 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006568 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6569 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006570 break;
6571 case TARGET_NR_setuid:
6572 ret = get_errno(setuid(low2highuid(arg1)));
6573 break;
6574 case TARGET_NR_setgid:
6575 ret = get_errno(setgid(low2highgid(arg1)));
6576 break;
6577 case TARGET_NR_setfsuid:
6578 ret = get_errno(setfsuid(arg1));
6579 break;
6580 case TARGET_NR_setfsgid:
6581 ret = get_errno(setfsgid(arg1));
6582 break;
6583#endif /* USE_UID16 */
6584
bellarda315a142005-01-30 22:59:18 +00006585#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00006586 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00006587 if (!(p = lock_user_string(arg1)))
6588 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006589 ret = get_errno(lchown(p, arg2, arg3));
6590 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006591 break;
bellarda315a142005-01-30 22:59:18 +00006592#endif
6593#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00006594 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00006595 ret = get_errno(getuid());
6596 break;
bellarda315a142005-01-30 22:59:18 +00006597#endif
aurel3264b4d282008-11-14 17:20:15 +00006598
6599#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6600 /* Alpha specific */
6601 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006602 {
6603 uid_t euid;
6604 euid=geteuid();
6605 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6606 }
aurel3264b4d282008-11-14 17:20:15 +00006607 ret = get_errno(getuid());
6608 break;
6609#endif
6610#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6611 /* Alpha specific */
6612 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006613 {
6614 uid_t egid;
6615 egid=getegid();
6616 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6617 }
aurel3264b4d282008-11-14 17:20:15 +00006618 ret = get_errno(getgid());
6619 break;
6620#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08006621#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
6622 /* Alpha specific */
6623 case TARGET_NR_osf_getsysinfo:
6624 ret = -TARGET_EOPNOTSUPP;
6625 switch (arg1) {
6626 case TARGET_GSI_IEEE_FP_CONTROL:
6627 {
6628 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
6629
6630 /* Copied from linux ieee_fpcr_to_swcr. */
6631 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
6632 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
6633 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
6634 | SWCR_TRAP_ENABLE_DZE
6635 | SWCR_TRAP_ENABLE_OVF);
6636 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
6637 | SWCR_TRAP_ENABLE_INE);
6638 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
6639 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
6640
6641 if (put_user_u64 (swcr, arg2))
6642 goto efault;
6643 ret = 0;
6644 }
6645 break;
6646
6647 /* case GSI_IEEE_STATE_AT_SIGNAL:
6648 -- Not implemented in linux kernel.
6649 case GSI_UACPROC:
6650 -- Retrieves current unaligned access state; not much used.
6651 case GSI_PROC_TYPE:
6652 -- Retrieves implver information; surely not used.
6653 case GSI_GET_HWRPB:
6654 -- Grabs a copy of the HWRPB; surely not used.
6655 */
6656 }
6657 break;
6658#endif
6659#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
6660 /* Alpha specific */
6661 case TARGET_NR_osf_setsysinfo:
6662 ret = -TARGET_EOPNOTSUPP;
6663 switch (arg1) {
6664 case TARGET_SSI_IEEE_FP_CONTROL:
6665 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
6666 {
6667 uint64_t swcr, fpcr, orig_fpcr;
6668
6669 if (get_user_u64 (swcr, arg2))
6670 goto efault;
6671 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
6672 fpcr = orig_fpcr & FPCR_DYN_MASK;
6673
6674 /* Copied from linux ieee_swcr_to_fpcr. */
6675 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
6676 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
6677 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
6678 | SWCR_TRAP_ENABLE_DZE
6679 | SWCR_TRAP_ENABLE_OVF)) << 48;
6680 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
6681 | SWCR_TRAP_ENABLE_INE)) << 57;
6682 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
6683 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
6684
6685 cpu_alpha_store_fpcr (cpu_env, fpcr);
6686 ret = 0;
6687
6688 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
6689 /* Old exceptions are not signaled. */
6690 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
6691
6692 /* If any exceptions set by this call, and are unmasked,
6693 send a signal. */
6694 /* ??? FIXME */
6695 }
6696 }
6697 break;
6698
6699 /* case SSI_NVPAIRS:
6700 -- Used with SSIN_UACPROC to enable unaligned accesses.
6701 case SSI_IEEE_STATE_AT_SIGNAL:
6702 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
6703 -- Not implemented in linux kernel
6704 */
6705 }
6706 break;
6707#endif
6708#ifdef TARGET_NR_osf_sigprocmask
6709 /* Alpha specific. */
6710 case TARGET_NR_osf_sigprocmask:
6711 {
6712 abi_ulong mask;
6713 int how = arg1;
6714 sigset_t set, oldset;
6715
6716 switch(arg1) {
6717 case TARGET_SIG_BLOCK:
6718 how = SIG_BLOCK;
6719 break;
6720 case TARGET_SIG_UNBLOCK:
6721 how = SIG_UNBLOCK;
6722 break;
6723 case TARGET_SIG_SETMASK:
6724 how = SIG_SETMASK;
6725 break;
6726 default:
6727 ret = -TARGET_EINVAL;
6728 goto fail;
6729 }
6730 mask = arg2;
6731 target_to_host_old_sigset(&set, &mask);
6732 sigprocmask(arg1, &set, &oldset);
6733 host_to_target_old_sigset(&mask, &oldset);
6734 ret = mask;
6735 }
6736 break;
6737#endif
aurel3264b4d282008-11-14 17:20:15 +00006738
bellarda315a142005-01-30 22:59:18 +00006739#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00006740 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00006741 ret = get_errno(getgid());
6742 break;
bellarda315a142005-01-30 22:59:18 +00006743#endif
6744#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00006745 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00006746 ret = get_errno(geteuid());
6747 break;
bellarda315a142005-01-30 22:59:18 +00006748#endif
6749#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00006750 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00006751 ret = get_errno(getegid());
6752 break;
bellarda315a142005-01-30 22:59:18 +00006753#endif
6754#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00006755 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00006756 ret = get_errno(setreuid(arg1, arg2));
6757 break;
bellarda315a142005-01-30 22:59:18 +00006758#endif
6759#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00006760 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00006761 ret = get_errno(setregid(arg1, arg2));
6762 break;
bellarda315a142005-01-30 22:59:18 +00006763#endif
6764#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00006765 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00006766 {
6767 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006768 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006769 gid_t *grouplist;
6770 int i;
6771
6772 grouplist = alloca(gidsetsize * sizeof(gid_t));
6773 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006774 if (gidsetsize == 0)
6775 break;
bellard99c475a2005-01-31 20:45:13 +00006776 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006777 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6778 if (!target_grouplist) {
6779 ret = -TARGET_EFAULT;
6780 goto fail;
6781 }
balroga2155fc2008-09-20 02:12:08 +00006782 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00006783 target_grouplist[i] = tswap32(grouplist[i]);
6784 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00006785 }
6786 }
6787 break;
bellarda315a142005-01-30 22:59:18 +00006788#endif
6789#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00006790 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00006791 {
6792 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006793 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006794 gid_t *grouplist;
6795 int i;
ths3b46e622007-09-17 08:09:54 +00006796
bellard99c475a2005-01-31 20:45:13 +00006797 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006798 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6799 if (!target_grouplist) {
6800 ret = -TARGET_EFAULT;
6801 goto fail;
6802 }
bellard99c475a2005-01-31 20:45:13 +00006803 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00006804 grouplist[i] = tswap32(target_grouplist[i]);
6805 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00006806 ret = get_errno(setgroups(gidsetsize, grouplist));
6807 }
6808 break;
bellarda315a142005-01-30 22:59:18 +00006809#endif
6810#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00006811 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00006812 ret = get_errno(fchown(arg1, arg2, arg3));
6813 break;
bellarda315a142005-01-30 22:59:18 +00006814#endif
6815#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00006816 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006817 ret = get_errno(setresuid(arg1, arg2, arg3));
6818 break;
bellarda315a142005-01-30 22:59:18 +00006819#endif
6820#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00006821 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006822 {
pbrook53a59602006-03-25 19:31:22 +00006823 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00006824 ret = get_errno(getresuid(&ruid, &euid, &suid));
6825 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006826 if (put_user_u32(ruid, arg1)
6827 || put_user_u32(euid, arg2)
6828 || put_user_u32(suid, arg3))
6829 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006830 }
6831 }
6832 break;
bellarda315a142005-01-30 22:59:18 +00006833#endif
6834#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00006835 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006836 ret = get_errno(setresgid(arg1, arg2, arg3));
6837 break;
bellarda315a142005-01-30 22:59:18 +00006838#endif
6839#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00006840 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006841 {
pbrook53a59602006-03-25 19:31:22 +00006842 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00006843 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6844 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006845 if (put_user_u32(rgid, arg1)
6846 || put_user_u32(egid, arg2)
6847 || put_user_u32(sgid, arg3))
6848 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006849 }
6850 }
6851 break;
bellarda315a142005-01-30 22:59:18 +00006852#endif
6853#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00006854 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00006855 if (!(p = lock_user_string(arg1)))
6856 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006857 ret = get_errno(chown(p, arg2, arg3));
6858 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006859 break;
bellarda315a142005-01-30 22:59:18 +00006860#endif
6861#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00006862 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00006863 ret = get_errno(setuid(arg1));
6864 break;
bellarda315a142005-01-30 22:59:18 +00006865#endif
6866#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00006867 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00006868 ret = get_errno(setgid(arg1));
6869 break;
bellarda315a142005-01-30 22:59:18 +00006870#endif
6871#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00006872 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00006873 ret = get_errno(setfsuid(arg1));
6874 break;
bellarda315a142005-01-30 22:59:18 +00006875#endif
6876#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00006877 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00006878 ret = get_errno(setfsgid(arg1));
6879 break;
bellarda315a142005-01-30 22:59:18 +00006880#endif
bellard67867302003-11-23 17:05:30 +00006881
bellard31e31b82003-02-18 22:55:36 +00006882 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00006883 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00006884#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00006885 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00006886 {
6887 void *a;
6888 ret = -TARGET_EFAULT;
6889 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6890 goto efault;
6891 if (!(p = lock_user_string(arg3)))
6892 goto mincore_fail;
6893 ret = get_errno(mincore(a, arg2, p));
6894 unlock_user(p, arg3, ret);
6895 mincore_fail:
6896 unlock_user(a, arg1, 0);
6897 }
6898 break;
bellardffa65c32004-01-04 23:57:22 +00006899#endif
aurel32408321b2008-10-01 21:46:32 +00006900#ifdef TARGET_NR_arm_fadvise64_64
6901 case TARGET_NR_arm_fadvise64_64:
6902 {
6903 /*
6904 * arm_fadvise64_64 looks like fadvise64_64 but
6905 * with different argument order
6906 */
6907 abi_long temp;
6908 temp = arg3;
6909 arg3 = arg4;
6910 arg4 = temp;
6911 }
6912#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02006913#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00006914#ifdef TARGET_NR_fadvise64_64
6915 case TARGET_NR_fadvise64_64:
6916#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02006917#ifdef TARGET_NR_fadvise64
6918 case TARGET_NR_fadvise64:
6919#endif
6920#ifdef TARGET_S390X
6921 switch (arg4) {
6922 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
6923 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
6924 case 6: arg4 = POSIX_FADV_DONTNEED; break;
6925 case 7: arg4 = POSIX_FADV_NOREUSE; break;
6926 default: break;
6927 }
6928#endif
6929 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00006930 break;
6931#endif
bellardffa65c32004-01-04 23:57:22 +00006932#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00006933 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00006934 /* A straight passthrough may not be safe because qemu sometimes
6935 turns private flie-backed mappings into anonymous mappings.
6936 This will break MADV_DONTNEED.
6937 This is a hint, so ignoring and returning success is ok. */
6938 ret = get_errno(0);
6939 break;
bellardffa65c32004-01-04 23:57:22 +00006940#endif
blueswir1992f48a2007-10-14 16:27:31 +00006941#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00006942 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00006943 {
thsb1e341e2007-03-20 21:50:52 +00006944 int cmd;
bellard77e46722003-04-29 20:39:06 +00006945 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00006946 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00006947#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00006948 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00006949#endif
bellard77e46722003-04-29 20:39:06 +00006950
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006951 cmd = target_to_host_fcntl_cmd(arg2);
6952 if (cmd == -TARGET_EINVAL)
6953 return cmd;
thsb1e341e2007-03-20 21:50:52 +00006954
bellard60cd49d2003-03-16 22:53:56 +00006955 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00006956 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00006957#ifdef TARGET_ARM
6958 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006959 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6960 goto efault;
ths58134272007-03-31 18:59:32 +00006961 fl.l_type = tswap16(target_efl->l_type);
6962 fl.l_whence = tswap16(target_efl->l_whence);
6963 fl.l_start = tswap64(target_efl->l_start);
6964 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006965 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00006966 unlock_user_struct(target_efl, arg3, 0);
6967 } else
6968#endif
6969 {
bellard9ee1fa22007-11-11 15:11:19 +00006970 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6971 goto efault;
ths58134272007-03-31 18:59:32 +00006972 fl.l_type = tswap16(target_fl->l_type);
6973 fl.l_whence = tswap16(target_fl->l_whence);
6974 fl.l_start = tswap64(target_fl->l_start);
6975 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006976 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00006977 unlock_user_struct(target_fl, arg3, 0);
6978 }
thsb1e341e2007-03-20 21:50:52 +00006979 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006980 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00006981#ifdef TARGET_ARM
6982 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006983 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6984 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006985 target_efl->l_type = tswap16(fl.l_type);
6986 target_efl->l_whence = tswap16(fl.l_whence);
6987 target_efl->l_start = tswap64(fl.l_start);
6988 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006989 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006990 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006991 } else
6992#endif
6993 {
bellard9ee1fa22007-11-11 15:11:19 +00006994 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6995 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006996 target_fl->l_type = tswap16(fl.l_type);
6997 target_fl->l_whence = tswap16(fl.l_whence);
6998 target_fl->l_start = tswap64(fl.l_start);
6999 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007000 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00007001 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00007002 }
bellard77e46722003-04-29 20:39:06 +00007003 }
7004 break;
7005
thsb1e341e2007-03-20 21:50:52 +00007006 case TARGET_F_SETLK64:
7007 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00007008#ifdef TARGET_ARM
7009 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00007010 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7011 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007012 fl.l_type = tswap16(target_efl->l_type);
7013 fl.l_whence = tswap16(target_efl->l_whence);
7014 fl.l_start = tswap64(target_efl->l_start);
7015 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007016 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00007017 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00007018 } else
7019#endif
7020 {
bellard9ee1fa22007-11-11 15:11:19 +00007021 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7022 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00007023 fl.l_type = tswap16(target_fl->l_type);
7024 fl.l_whence = tswap16(target_fl->l_whence);
7025 fl.l_start = tswap64(target_fl->l_start);
7026 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02007027 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00007028 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00007029 }
thsb1e341e2007-03-20 21:50:52 +00007030 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00007031 break;
bellard60cd49d2003-03-16 22:53:56 +00007032 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02007033 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00007034 break;
7035 }
bellard77e46722003-04-29 20:39:06 +00007036 break;
7037 }
bellard60cd49d2003-03-16 22:53:56 +00007038#endif
ths7d600c82006-12-08 01:32:58 +00007039#ifdef TARGET_NR_cacheflush
7040 case TARGET_NR_cacheflush:
7041 /* self-modifying code is handled automatically, so nothing needed */
7042 ret = 0;
7043 break;
7044#endif
bellardebc05482003-09-30 21:08:41 +00007045#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00007046 case TARGET_NR_security:
7047 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007048#endif
bellardc573ff62004-01-04 15:51:36 +00007049#ifdef TARGET_NR_getpagesize
7050 case TARGET_NR_getpagesize:
7051 ret = TARGET_PAGE_SIZE;
7052 break;
7053#endif
bellard31e31b82003-02-18 22:55:36 +00007054 case TARGET_NR_gettid:
7055 ret = get_errno(gettid());
7056 break;
thse5febef2007-04-01 18:31:35 +00007057#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00007058 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00007059#if TARGET_ABI_BITS == 32
7060#ifdef TARGET_ARM
7061 if (((CPUARMState *)cpu_env)->eabi)
7062 {
7063 arg2 = arg3;
7064 arg3 = arg4;
7065 arg4 = arg5;
7066 }
7067#endif
7068 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
7069#else
7070 ret = get_errno(readahead(arg1, arg2, arg3));
7071#endif
7072 break;
thse5febef2007-04-01 18:31:35 +00007073#endif
bellardebc05482003-09-30 21:08:41 +00007074#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00007075 case TARGET_NR_setxattr:
7076 case TARGET_NR_lsetxattr:
7077 case TARGET_NR_fsetxattr:
7078 case TARGET_NR_getxattr:
7079 case TARGET_NR_lgetxattr:
7080 case TARGET_NR_fgetxattr:
7081 case TARGET_NR_listxattr:
7082 case TARGET_NR_llistxattr:
7083 case TARGET_NR_flistxattr:
7084 case TARGET_NR_removexattr:
7085 case TARGET_NR_lremovexattr:
7086 case TARGET_NR_fremovexattr:
Arnaud Patard6f932f92009-04-21 21:04:18 +03007087 ret = -TARGET_EOPNOTSUPP;
7088 break;
bellardebc05482003-09-30 21:08:41 +00007089#endif
7090#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00007091 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007092#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00007093 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
7094 ret = 0;
7095 break;
edgar_iglef967792009-01-07 14:19:38 +00007096#elif defined(TARGET_CRIS)
7097 if (arg1 & 0xff)
7098 ret = -TARGET_EINVAL;
7099 else {
7100 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
7101 ret = 0;
7102 }
7103 break;
bellard8d18e892007-11-14 15:18:40 +00007104#elif defined(TARGET_I386) && defined(TARGET_ABI32)
7105 ret = do_set_thread_area(cpu_env, arg1);
7106 break;
ths6f5b89a2007-03-02 20:48:00 +00007107#else
7108 goto unimplemented_nowarn;
7109#endif
7110#endif
7111#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00007112 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007113#if defined(TARGET_I386) && defined(TARGET_ABI32)
7114 ret = do_get_thread_area(cpu_env, arg1);
7115#else
bellard5cd43932003-03-29 16:54:36 +00007116 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00007117#endif
bellard8d18e892007-11-14 15:18:40 +00007118#endif
bellard48dc41e2006-06-21 18:15:50 +00007119#ifdef TARGET_NR_getdomainname
7120 case TARGET_NR_getdomainname:
7121 goto unimplemented_nowarn;
7122#endif
ths6f5b89a2007-03-02 20:48:00 +00007123
thsb5906f92007-03-19 13:32:45 +00007124#ifdef TARGET_NR_clock_gettime
7125 case TARGET_NR_clock_gettime:
7126 {
7127 struct timespec ts;
7128 ret = get_errno(clock_gettime(arg1, &ts));
7129 if (!is_error(ret)) {
7130 host_to_target_timespec(arg2, &ts);
7131 }
7132 break;
7133 }
7134#endif
7135#ifdef TARGET_NR_clock_getres
7136 case TARGET_NR_clock_getres:
7137 {
7138 struct timespec ts;
7139 ret = get_errno(clock_getres(arg1, &ts));
7140 if (!is_error(ret)) {
7141 host_to_target_timespec(arg2, &ts);
7142 }
7143 break;
7144 }
7145#endif
pbrook63d76512008-05-29 13:43:29 +00007146#ifdef TARGET_NR_clock_nanosleep
7147 case TARGET_NR_clock_nanosleep:
7148 {
7149 struct timespec ts;
7150 target_to_host_timespec(&ts, arg3);
7151 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7152 if (arg4)
7153 host_to_target_timespec(arg4, &ts);
7154 break;
7155 }
7156#endif
thsb5906f92007-03-19 13:32:45 +00007157
ths6f5b89a2007-03-02 20:48:00 +00007158#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7159 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00007160 ret = get_errno(set_tid_address((int *)g2h(arg1)));
7161 break;
ths6f5b89a2007-03-02 20:48:00 +00007162#endif
7163
ths3ae43202007-09-16 21:39:48 +00007164#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00007165 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00007166 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00007167 break;
7168#endif
7169
ths3ae43202007-09-16 21:39:48 +00007170#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00007171 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00007172 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7173 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00007174 break;
7175#endif
7176
ths4f2b1fe2007-06-21 21:57:12 +00007177#ifdef TARGET_NR_set_robust_list
7178 case TARGET_NR_set_robust_list:
7179 goto unimplemented_nowarn;
7180#endif
7181
ths9007f0e2007-09-25 17:50:37 +00007182#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7183 case TARGET_NR_utimensat:
7184 {
Riku Voipioebc996f2009-04-21 15:01:51 +03007185 struct timespec *tsp, ts[2];
7186 if (!arg3) {
7187 tsp = NULL;
7188 } else {
7189 target_to_host_timespec(ts, arg3);
7190 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7191 tsp = ts;
7192 }
ths9007f0e2007-09-25 17:50:37 +00007193 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03007194 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00007195 else {
bellard579a97f2007-11-11 14:26:47 +00007196 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00007197 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00007198 goto fail;
7199 }
Riku Voipioebc996f2009-04-21 15:01:51 +03007200 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00007201 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00007202 }
7203 }
7204 break;
7205#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02007206#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00007207 case TARGET_NR_futex:
7208 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7209 break;
7210#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007211#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00007212 case TARGET_NR_inotify_init:
7213 ret = get_errno(sys_inotify_init());
7214 break;
7215#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007216#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00007217#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7218 case TARGET_NR_inotify_init1:
7219 ret = get_errno(sys_inotify_init1(arg1));
7220 break;
7221#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007222#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007223#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00007224 case TARGET_NR_inotify_add_watch:
7225 p = lock_user_string(arg2);
7226 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7227 unlock_user(p, arg2, 0);
7228 break;
7229#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007230#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00007231 case TARGET_NR_inotify_rm_watch:
7232 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7233 break;
7234#endif
ths9007f0e2007-09-25 17:50:37 +00007235
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07007236#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00007237 case TARGET_NR_mq_open:
7238 {
7239 struct mq_attr posix_mq_attr;
7240
7241 p = lock_user_string(arg1 - 1);
7242 if (arg4 != 0)
7243 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7244 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7245 unlock_user (p, arg1, 0);
7246 }
7247 break;
7248
7249 case TARGET_NR_mq_unlink:
7250 p = lock_user_string(arg1 - 1);
7251 ret = get_errno(mq_unlink(p));
7252 unlock_user (p, arg1, 0);
7253 break;
7254
7255 case TARGET_NR_mq_timedsend:
7256 {
7257 struct timespec ts;
7258
7259 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7260 if (arg5 != 0) {
7261 target_to_host_timespec(&ts, arg5);
7262 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7263 host_to_target_timespec(arg5, &ts);
7264 }
7265 else
7266 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7267 unlock_user (p, arg2, arg3);
7268 }
7269 break;
7270
7271 case TARGET_NR_mq_timedreceive:
7272 {
7273 struct timespec ts;
7274 unsigned int prio;
7275
7276 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7277 if (arg5 != 0) {
7278 target_to_host_timespec(&ts, arg5);
7279 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7280 host_to_target_timespec(arg5, &ts);
7281 }
7282 else
7283 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7284 unlock_user (p, arg2, arg3);
7285 if (arg4 != 0)
7286 put_user_u32(prio, arg4);
7287 }
7288 break;
7289
7290 /* Not implemented for now... */
7291/* case TARGET_NR_mq_notify: */
7292/* break; */
7293
7294 case TARGET_NR_mq_getsetattr:
7295 {
7296 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7297 ret = 0;
7298 if (arg3 != 0) {
7299 ret = mq_getattr(arg1, &posix_mq_attr_out);
7300 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7301 }
7302 if (arg2 != 0) {
7303 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7304 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7305 }
7306
7307 }
7308 break;
7309#endif
7310
vibisreenivasan3ce34df2009-05-16 18:32:41 +05307311#ifdef CONFIG_SPLICE
7312#ifdef TARGET_NR_tee
7313 case TARGET_NR_tee:
7314 {
7315 ret = get_errno(tee(arg1,arg2,arg3,arg4));
7316 }
7317 break;
7318#endif
7319#ifdef TARGET_NR_splice
7320 case TARGET_NR_splice:
7321 {
7322 loff_t loff_in, loff_out;
7323 loff_t *ploff_in = NULL, *ploff_out = NULL;
7324 if(arg2) {
7325 get_user_u64(loff_in, arg2);
7326 ploff_in = &loff_in;
7327 }
7328 if(arg4) {
7329 get_user_u64(loff_out, arg2);
7330 ploff_out = &loff_out;
7331 }
7332 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7333 }
7334 break;
7335#endif
7336#ifdef TARGET_NR_vmsplice
7337 case TARGET_NR_vmsplice:
7338 {
7339 int count = arg3;
7340 struct iovec *vec;
7341
7342 vec = alloca(count * sizeof(struct iovec));
7343 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7344 goto efault;
7345 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7346 unlock_iovec(vec, arg2, count, 0);
7347 }
7348 break;
7349#endif
7350#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03007351#ifdef CONFIG_EVENTFD
7352#if defined(TARGET_NR_eventfd)
7353 case TARGET_NR_eventfd:
7354 ret = get_errno(eventfd(arg1, 0));
7355 break;
7356#endif
7357#if defined(TARGET_NR_eventfd2)
7358 case TARGET_NR_eventfd2:
7359 ret = get_errno(eventfd(arg1, arg2));
7360 break;
7361#endif
7362#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03007363#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7364 case TARGET_NR_fallocate:
7365 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7366 break;
7367#endif
Peter Maydellc727f472011-01-06 11:05:10 +00007368#if defined(CONFIG_SYNC_FILE_RANGE)
7369#if defined(TARGET_NR_sync_file_range)
7370 case TARGET_NR_sync_file_range:
7371#if TARGET_ABI_BITS == 32
7372 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
7373 target_offset64(arg4, arg5), arg6));
7374#else
7375 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
7376#endif
7377 break;
7378#endif
7379#if defined(TARGET_NR_sync_file_range2)
7380 case TARGET_NR_sync_file_range2:
7381 /* This is like sync_file_range but the arguments are reordered */
7382#if TARGET_ABI_BITS == 32
7383 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
7384 target_offset64(arg5, arg6), arg2));
7385#else
7386 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
7387#endif
7388 break;
7389#endif
7390#endif
bellard31e31b82003-02-18 22:55:36 +00007391 default:
7392 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00007393 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00007394#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 +00007395 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00007396#endif
ths0da46a62007-10-20 20:23:07 +00007397 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00007398 break;
7399 }
bellard579a97f2007-11-11 14:26:47 +00007400fail:
bellardc573ff62004-01-04 15:51:36 +00007401#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00007402 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00007403#endif
thsb92c47c2007-11-01 00:07:38 +00007404 if(do_strace)
7405 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00007406 return ret;
bellard579a97f2007-11-11 14:26:47 +00007407efault:
7408 ret = -TARGET_EFAULT;
7409 goto fail;
bellard31e31b82003-02-18 22:55:36 +00007410}