blob: ce728faa4ddeb74404f752453ea7304fa67a54cb [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);
Paul Brook909b69c2009-05-19 15:58:52 +0100211#if defined(TARGET_NR__llseek) && !defined (__x86_64__)
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
721 if (!is_error(mapped_addr))
bellard31e31b82003-02-18 22:55:36 +0000722 target_brk = new_brk;
balrog7ab240a2008-04-26 12:17:34 +0000723
724 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000725}
726
ths26edcf42007-12-09 02:25:24 +0000727static inline abi_long copy_from_user_fdset(fd_set *fds,
728 abi_ulong target_fds_addr,
729 int n)
bellard31e31b82003-02-18 22:55:36 +0000730{
ths26edcf42007-12-09 02:25:24 +0000731 int i, nw, j, k;
732 abi_ulong b, *target_fds;
733
734 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
735 if (!(target_fds = lock_user(VERIFY_READ,
736 target_fds_addr,
737 sizeof(abi_ulong) * nw,
738 1)))
739 return -TARGET_EFAULT;
740
741 FD_ZERO(fds);
742 k = 0;
743 for (i = 0; i < nw; i++) {
744 /* grab the abi_ulong */
745 __get_user(b, &target_fds[i]);
746 for (j = 0; j < TARGET_ABI_BITS; j++) {
747 /* check the bit inside the abi_ulong */
748 if ((b >> j) & 1)
749 FD_SET(k, fds);
750 k++;
bellard31e31b82003-02-18 22:55:36 +0000751 }
bellard31e31b82003-02-18 22:55:36 +0000752 }
ths26edcf42007-12-09 02:25:24 +0000753
754 unlock_user(target_fds, target_fds_addr, 0);
755
756 return 0;
bellard31e31b82003-02-18 22:55:36 +0000757}
758
ths26edcf42007-12-09 02:25:24 +0000759static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
760 const fd_set *fds,
761 int n)
bellard31e31b82003-02-18 22:55:36 +0000762{
bellard31e31b82003-02-18 22:55:36 +0000763 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000764 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000765 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000766
ths26edcf42007-12-09 02:25:24 +0000767 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
768 if (!(target_fds = lock_user(VERIFY_WRITE,
769 target_fds_addr,
770 sizeof(abi_ulong) * nw,
771 0)))
772 return -TARGET_EFAULT;
773
774 k = 0;
775 for (i = 0; i < nw; i++) {
776 v = 0;
777 for (j = 0; j < TARGET_ABI_BITS; j++) {
778 v |= ((FD_ISSET(k, fds) != 0) << j);
779 k++;
bellard31e31b82003-02-18 22:55:36 +0000780 }
ths26edcf42007-12-09 02:25:24 +0000781 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000782 }
ths26edcf42007-12-09 02:25:24 +0000783
784 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
785
786 return 0;
bellard31e31b82003-02-18 22:55:36 +0000787}
788
bellardc596ed12003-07-13 17:32:31 +0000789#if defined(__alpha__)
790#define HOST_HZ 1024
791#else
792#define HOST_HZ 100
793#endif
794
blueswir1992f48a2007-10-14 16:27:31 +0000795static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000796{
797#if HOST_HZ == TARGET_HZ
798 return ticks;
799#else
800 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
801#endif
802}
803
bellard579a97f2007-11-11 14:26:47 +0000804static inline abi_long host_to_target_rusage(abi_ulong target_addr,
805 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000806{
pbrook53a59602006-03-25 19:31:22 +0000807 struct target_rusage *target_rusage;
808
bellard579a97f2007-11-11 14:26:47 +0000809 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
810 return -TARGET_EFAULT;
bellardb4091862003-05-16 15:39:34 +0000811 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
812 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
813 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
814 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
815 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
816 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
817 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
818 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
819 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
820 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
821 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
822 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
823 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
824 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
825 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
826 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
827 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
828 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000829 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000830
831 return 0;
bellardb4091862003-05-16 15:39:34 +0000832}
833
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900834static inline rlim_t target_to_host_rlim(target_ulong target_rlim)
835{
836 if (target_rlim == TARGET_RLIM_INFINITY)
837 return RLIM_INFINITY;
838 else
839 return tswapl(target_rlim);
840}
841
842static inline target_ulong host_to_target_rlim(rlim_t rlim)
843{
844 if (rlim == RLIM_INFINITY || rlim != (target_long)rlim)
845 return TARGET_RLIM_INFINITY;
846 else
847 return tswapl(rlim);
848}
849
ths788f5ec2007-12-09 02:37:05 +0000850static inline abi_long copy_from_user_timeval(struct timeval *tv,
851 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000852{
pbrook53a59602006-03-25 19:31:22 +0000853 struct target_timeval *target_tv;
854
ths788f5ec2007-12-09 02:37:05 +0000855 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000856 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000857
858 __get_user(tv->tv_sec, &target_tv->tv_sec);
859 __get_user(tv->tv_usec, &target_tv->tv_usec);
860
861 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000862
863 return 0;
bellard31e31b82003-02-18 22:55:36 +0000864}
865
ths788f5ec2007-12-09 02:37:05 +0000866static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
867 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000868{
pbrook53a59602006-03-25 19:31:22 +0000869 struct target_timeval *target_tv;
870
ths788f5ec2007-12-09 02:37:05 +0000871 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000872 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000873
874 __put_user(tv->tv_sec, &target_tv->tv_sec);
875 __put_user(tv->tv_usec, &target_tv->tv_usec);
876
877 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000878
879 return 0;
bellard31e31b82003-02-18 22:55:36 +0000880}
881
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700882#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
883#include <mqueue.h>
884
aurel3224e10032009-04-15 16:11:43 +0000885static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
886 abi_ulong target_mq_attr_addr)
887{
888 struct target_mq_attr *target_mq_attr;
889
890 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
891 target_mq_attr_addr, 1))
892 return -TARGET_EFAULT;
893
894 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
895 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
896 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
897 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
898
899 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
900
901 return 0;
902}
903
904static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
905 const struct mq_attr *attr)
906{
907 struct target_mq_attr *target_mq_attr;
908
909 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
910 target_mq_attr_addr, 0))
911 return -TARGET_EFAULT;
912
913 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
914 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
915 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
916 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
917
918 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
919
920 return 0;
921}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700922#endif
bellard31e31b82003-02-18 22:55:36 +0000923
ths0da46a62007-10-20 20:23:07 +0000924/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000925static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000926 abi_ulong rfd_addr, abi_ulong wfd_addr,
927 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000928{
929 fd_set rfds, wfds, efds;
930 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
931 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000932 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000933
ths26edcf42007-12-09 02:25:24 +0000934 if (rfd_addr) {
935 if (copy_from_user_fdset(&rfds, rfd_addr, n))
936 return -TARGET_EFAULT;
937 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000938 } else {
pbrook53a59602006-03-25 19:31:22 +0000939 rfds_ptr = NULL;
940 }
ths26edcf42007-12-09 02:25:24 +0000941 if (wfd_addr) {
942 if (copy_from_user_fdset(&wfds, wfd_addr, n))
943 return -TARGET_EFAULT;
944 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000945 } else {
pbrook53a59602006-03-25 19:31:22 +0000946 wfds_ptr = NULL;
947 }
ths26edcf42007-12-09 02:25:24 +0000948 if (efd_addr) {
949 if (copy_from_user_fdset(&efds, efd_addr, n))
950 return -TARGET_EFAULT;
951 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000952 } else {
pbrook53a59602006-03-25 19:31:22 +0000953 efds_ptr = NULL;
954 }
ths3b46e622007-09-17 08:09:54 +0000955
ths26edcf42007-12-09 02:25:24 +0000956 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000957 if (copy_from_user_timeval(&tv, target_tv_addr))
958 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000959 tv_ptr = &tv;
960 } else {
961 tv_ptr = NULL;
962 }
ths26edcf42007-12-09 02:25:24 +0000963
bellard31e31b82003-02-18 22:55:36 +0000964 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000965
ths26edcf42007-12-09 02:25:24 +0000966 if (!is_error(ret)) {
967 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
968 return -TARGET_EFAULT;
969 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
970 return -TARGET_EFAULT;
971 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
972 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000973
ths788f5ec2007-12-09 02:37:05 +0000974 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
975 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000976 }
bellard579a97f2007-11-11 14:26:47 +0000977
bellard31e31b82003-02-18 22:55:36 +0000978 return ret;
979}
980
Riku Voipio099d6b02009-05-05 12:10:04 +0300981static abi_long do_pipe2(int host_pipe[], int flags)
982{
983#ifdef CONFIG_PIPE2
984 return pipe2(host_pipe, flags);
985#else
986 return -ENOSYS;
987#endif
988}
989
Ulrich Hecht784ccfd2009-07-03 17:09:26 +0200990static abi_long do_pipe(void *cpu_env, abi_ulong pipedes, int flags)
Riku Voipio099d6b02009-05-05 12:10:04 +0300991{
992 int host_pipe[2];
993 abi_long ret;
994 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
995
996 if (is_error(ret))
997 return get_errno(ret);
998#if defined(TARGET_MIPS)
999 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1000 ret = host_pipe[0];
Riku Voipio099d6b02009-05-05 12:10:04 +03001001#else
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001002#if defined(TARGET_SH4)
1003 if (!flags) {
1004 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1005 ret = host_pipe[0];
1006 } else
1007#endif
Riku Voipio099d6b02009-05-05 12:10:04 +03001008 if (put_user_s32(host_pipe[0], pipedes)
1009 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1010 return -TARGET_EFAULT;
1011#endif
1012 return get_errno(ret);
1013}
1014
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001015static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1016 abi_ulong target_addr,
1017 socklen_t len)
1018{
1019 struct target_ip_mreqn *target_smreqn;
1020
1021 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1022 if (!target_smreqn)
1023 return -TARGET_EFAULT;
1024 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1025 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1026 if (len == sizeof(struct target_ip_mreqn))
1027 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
1028 unlock_user(target_smreqn, target_addr, 0);
1029
1030 return 0;
1031}
1032
bellard579a97f2007-11-11 14:26:47 +00001033static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1034 abi_ulong target_addr,
1035 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001036{
aurel32607175e2009-04-15 16:11:59 +00001037 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1038 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001039 struct target_sockaddr *target_saddr;
1040
bellard579a97f2007-11-11 14:26:47 +00001041 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1042 if (!target_saddr)
1043 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001044
1045 sa_family = tswap16(target_saddr->sa_family);
1046
1047 /* Oops. The caller might send a incomplete sun_path; sun_path
1048 * must be terminated by \0 (see the manual page), but
1049 * unfortunately it is quite common to specify sockaddr_un
1050 * length as "strlen(x->sun_path)" while it should be
1051 * "strlen(...) + 1". We'll fix that here if needed.
1052 * Linux kernel has a similar feature.
1053 */
1054
1055 if (sa_family == AF_UNIX) {
1056 if (len < unix_maxlen && len > 0) {
1057 char *cp = (char*)target_saddr;
1058
1059 if ( cp[len-1] && !cp[len] )
1060 len++;
1061 }
1062 if (len > unix_maxlen)
1063 len = unix_maxlen;
1064 }
1065
pbrook53a59602006-03-25 19:31:22 +00001066 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001067 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001068 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001069
1070 return 0;
bellard7854b052003-03-29 17:22:23 +00001071}
1072
bellard579a97f2007-11-11 14:26:47 +00001073static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1074 struct sockaddr *addr,
1075 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001076{
pbrook53a59602006-03-25 19:31:22 +00001077 struct target_sockaddr *target_saddr;
1078
bellard579a97f2007-11-11 14:26:47 +00001079 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1080 if (!target_saddr)
1081 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001082 memcpy(target_saddr, addr, len);
1083 target_saddr->sa_family = tswap16(addr->sa_family);
1084 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001085
1086 return 0;
bellard7854b052003-03-29 17:22:23 +00001087}
1088
pbrook53a59602006-03-25 19:31:22 +00001089/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001090static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1091 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001092{
1093 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001094 abi_long msg_controllen;
1095 abi_ulong target_cmsg_addr;
1096 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001097 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001098
1099 msg_controllen = tswapl(target_msgh->msg_controllen);
1100 if (msg_controllen < sizeof (struct target_cmsghdr))
1101 goto the_end;
1102 target_cmsg_addr = tswapl(target_msgh->msg_control);
1103 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1104 if (!target_cmsg)
1105 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001106
1107 while (cmsg && target_cmsg) {
1108 void *data = CMSG_DATA(cmsg);
1109 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1110
ths5fafdf22007-09-16 21:08:06 +00001111 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001112 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1113
1114 space += CMSG_SPACE(len);
1115 if (space > msgh->msg_controllen) {
1116 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001117 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001118 break;
1119 }
1120
1121 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1122 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1123 cmsg->cmsg_len = CMSG_LEN(len);
1124
bellard3532fa72006-06-24 15:06:03 +00001125 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001126 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1127 memcpy(data, target_data, len);
1128 } else {
1129 int *fd = (int *)data;
1130 int *target_fd = (int *)target_data;
1131 int i, numfds = len / sizeof(int);
1132
1133 for (i = 0; i < numfds; i++)
1134 fd[i] = tswap32(target_fd[i]);
1135 }
1136
1137 cmsg = CMSG_NXTHDR(msgh, cmsg);
1138 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1139 }
bellard5a4a8982007-11-11 17:39:18 +00001140 unlock_user(target_cmsg, target_cmsg_addr, 0);
1141 the_end:
bellard7854b052003-03-29 17:22:23 +00001142 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001143 return 0;
bellard7854b052003-03-29 17:22:23 +00001144}
1145
pbrook53a59602006-03-25 19:31:22 +00001146/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001147static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1148 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001149{
1150 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001151 abi_long msg_controllen;
1152 abi_ulong target_cmsg_addr;
1153 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001154 socklen_t space = 0;
1155
bellard5a4a8982007-11-11 17:39:18 +00001156 msg_controllen = tswapl(target_msgh->msg_controllen);
1157 if (msg_controllen < sizeof (struct target_cmsghdr))
1158 goto the_end;
1159 target_cmsg_addr = tswapl(target_msgh->msg_control);
1160 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1161 if (!target_cmsg)
1162 return -TARGET_EFAULT;
1163
bellard7854b052003-03-29 17:22:23 +00001164 while (cmsg && target_cmsg) {
1165 void *data = CMSG_DATA(cmsg);
1166 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1167
1168 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1169
1170 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001171 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001172 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001173 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001174 break;
1175 }
1176
1177 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1178 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1179 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1180
bellard3532fa72006-06-24 15:06:03 +00001181 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001182 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1183 memcpy(target_data, data, len);
1184 } else {
1185 int *fd = (int *)data;
1186 int *target_fd = (int *)target_data;
1187 int i, numfds = len / sizeof(int);
1188
1189 for (i = 0; i < numfds; i++)
1190 target_fd[i] = tswap32(fd[i]);
1191 }
1192
1193 cmsg = CMSG_NXTHDR(msgh, cmsg);
1194 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1195 }
bellard5a4a8982007-11-11 17:39:18 +00001196 unlock_user(target_cmsg, target_cmsg_addr, space);
1197 the_end:
1198 target_msgh->msg_controllen = tswapl(space);
1199 return 0;
bellard7854b052003-03-29 17:22:23 +00001200}
1201
ths0da46a62007-10-20 20:23:07 +00001202/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001203static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001204 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001205{
blueswir1992f48a2007-10-14 16:27:31 +00001206 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001207 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001208 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001209 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001210
bellard8853f862004-02-22 14:57:26 +00001211 switch(level) {
1212 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001213 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001214 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001215 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001216
bellard2f619692007-11-16 10:46:05 +00001217 if (get_user_u32(val, optval_addr))
1218 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001219 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1220 break;
1221 case SOL_IP:
1222 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001223 case IP_TOS:
1224 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001225 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001226 case IP_ROUTER_ALERT:
1227 case IP_RECVOPTS:
1228 case IP_RETOPTS:
1229 case IP_PKTINFO:
1230 case IP_MTU_DISCOVER:
1231 case IP_RECVERR:
1232 case IP_RECVTOS:
1233#ifdef IP_FREEBIND
1234 case IP_FREEBIND:
1235#endif
1236 case IP_MULTICAST_TTL:
1237 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001238 val = 0;
1239 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001240 if (get_user_u32(val, optval_addr))
1241 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001242 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001243 if (get_user_u8(val, optval_addr))
1244 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001245 }
1246 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1247 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001248 case IP_ADD_MEMBERSHIP:
1249 case IP_DROP_MEMBERSHIP:
1250 if (optlen < sizeof (struct target_ip_mreq) ||
1251 optlen > sizeof (struct target_ip_mreqn))
1252 return -TARGET_EINVAL;
1253
1254 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1255 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1256 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1257 break;
1258
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001259 case IP_BLOCK_SOURCE:
1260 case IP_UNBLOCK_SOURCE:
1261 case IP_ADD_SOURCE_MEMBERSHIP:
1262 case IP_DROP_SOURCE_MEMBERSHIP:
1263 if (optlen != sizeof (struct target_ip_mreq_source))
1264 return -TARGET_EINVAL;
1265
1266 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1267 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1268 unlock_user (ip_mreq_source, optval_addr, 0);
1269 break;
1270
bellard8853f862004-02-22 14:57:26 +00001271 default:
1272 goto unimplemented;
1273 }
1274 break;
bellard3532fa72006-06-24 15:06:03 +00001275 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001276 switch (optname) {
1277 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001278 case TARGET_SO_DEBUG:
1279 optname = SO_DEBUG;
1280 break;
1281 case TARGET_SO_REUSEADDR:
1282 optname = SO_REUSEADDR;
1283 break;
1284 case TARGET_SO_TYPE:
1285 optname = SO_TYPE;
1286 break;
1287 case TARGET_SO_ERROR:
1288 optname = SO_ERROR;
1289 break;
1290 case TARGET_SO_DONTROUTE:
1291 optname = SO_DONTROUTE;
1292 break;
1293 case TARGET_SO_BROADCAST:
1294 optname = SO_BROADCAST;
1295 break;
1296 case TARGET_SO_SNDBUF:
1297 optname = SO_SNDBUF;
1298 break;
1299 case TARGET_SO_RCVBUF:
1300 optname = SO_RCVBUF;
1301 break;
1302 case TARGET_SO_KEEPALIVE:
1303 optname = SO_KEEPALIVE;
1304 break;
1305 case TARGET_SO_OOBINLINE:
1306 optname = SO_OOBINLINE;
1307 break;
1308 case TARGET_SO_NO_CHECK:
1309 optname = SO_NO_CHECK;
1310 break;
1311 case TARGET_SO_PRIORITY:
1312 optname = SO_PRIORITY;
1313 break;
bellard5e83e8e2005-03-01 22:32:06 +00001314#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001315 case TARGET_SO_BSDCOMPAT:
1316 optname = SO_BSDCOMPAT;
1317 break;
bellard5e83e8e2005-03-01 22:32:06 +00001318#endif
bellard3532fa72006-06-24 15:06:03 +00001319 case TARGET_SO_PASSCRED:
1320 optname = SO_PASSCRED;
1321 break;
1322 case TARGET_SO_TIMESTAMP:
1323 optname = SO_TIMESTAMP;
1324 break;
1325 case TARGET_SO_RCVLOWAT:
1326 optname = SO_RCVLOWAT;
1327 break;
1328 case TARGET_SO_RCVTIMEO:
1329 optname = SO_RCVTIMEO;
1330 break;
1331 case TARGET_SO_SNDTIMEO:
1332 optname = SO_SNDTIMEO;
1333 break;
bellard8853f862004-02-22 14:57:26 +00001334 break;
1335 default:
1336 goto unimplemented;
1337 }
bellard3532fa72006-06-24 15:06:03 +00001338 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001339 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001340
bellard2f619692007-11-16 10:46:05 +00001341 if (get_user_u32(val, optval_addr))
1342 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001343 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001344 break;
bellard7854b052003-03-29 17:22:23 +00001345 default:
bellard8853f862004-02-22 14:57:26 +00001346 unimplemented:
1347 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001348 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001349 }
bellard8853f862004-02-22 14:57:26 +00001350 return ret;
bellard7854b052003-03-29 17:22:23 +00001351}
1352
ths0da46a62007-10-20 20:23:07 +00001353/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001354static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001355 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001356{
blueswir1992f48a2007-10-14 16:27:31 +00001357 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001358 int len, val;
1359 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001360
1361 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001362 case TARGET_SOL_SOCKET:
1363 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +00001364 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +00001365 case TARGET_SO_LINGER:
1366 case TARGET_SO_RCVTIMEO:
1367 case TARGET_SO_SNDTIMEO:
1368 case TARGET_SO_PEERCRED:
1369 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +00001370 /* These don't just return a single integer */
1371 goto unimplemented;
1372 default:
bellard2efbe912005-07-23 15:10:20 +00001373 goto int_case;
1374 }
1375 break;
1376 case SOL_TCP:
1377 /* TCP options all take an 'int' value. */
1378 int_case:
bellard2f619692007-11-16 10:46:05 +00001379 if (get_user_u32(len, optlen))
1380 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001381 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001382 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +00001383 lv = sizeof(int);
1384 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1385 if (ret < 0)
1386 return ret;
bellard2efbe912005-07-23 15:10:20 +00001387 if (len > lv)
1388 len = lv;
bellard2f619692007-11-16 10:46:05 +00001389 if (len == 4) {
1390 if (put_user_u32(val, optval_addr))
1391 return -TARGET_EFAULT;
1392 } else {
1393 if (put_user_u8(val, optval_addr))
1394 return -TARGET_EFAULT;
1395 }
1396 if (put_user_u32(len, optlen))
1397 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001398 break;
1399 case SOL_IP:
1400 switch(optname) {
1401 case IP_TOS:
1402 case IP_TTL:
1403 case IP_HDRINCL:
1404 case IP_ROUTER_ALERT:
1405 case IP_RECVOPTS:
1406 case IP_RETOPTS:
1407 case IP_PKTINFO:
1408 case IP_MTU_DISCOVER:
1409 case IP_RECVERR:
1410 case IP_RECVTOS:
1411#ifdef IP_FREEBIND
1412 case IP_FREEBIND:
1413#endif
1414 case IP_MULTICAST_TTL:
1415 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001416 if (get_user_u32(len, optlen))
1417 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001418 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001419 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +00001420 lv = sizeof(int);
1421 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1422 if (ret < 0)
1423 return ret;
bellard2efbe912005-07-23 15:10:20 +00001424 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001425 len = 1;
bellard2f619692007-11-16 10:46:05 +00001426 if (put_user_u32(len, optlen)
1427 || put_user_u8(val, optval_addr))
1428 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001429 } else {
bellard2efbe912005-07-23 15:10:20 +00001430 if (len > sizeof(int))
1431 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001432 if (put_user_u32(len, optlen)
1433 || put_user_u32(val, optval_addr))
1434 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001435 }
bellard8853f862004-02-22 14:57:26 +00001436 break;
bellard2efbe912005-07-23 15:10:20 +00001437 default:
thsc02f4992007-12-18 02:39:59 +00001438 ret = -TARGET_ENOPROTOOPT;
1439 break;
bellard8853f862004-02-22 14:57:26 +00001440 }
1441 break;
1442 default:
1443 unimplemented:
1444 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1445 level, optname);
thsc02f4992007-12-18 02:39:59 +00001446 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001447 break;
1448 }
1449 return ret;
bellard7854b052003-03-29 17:22:23 +00001450}
1451
bellard579a97f2007-11-11 14:26:47 +00001452/* FIXME
1453 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1454 * other lock functions have a return code of 0 for failure.
1455 */
1456static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1457 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001458{
1459 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001460 abi_ulong base;
balrogd732dcb2008-10-28 10:21:03 +00001461 int i;
pbrook53a59602006-03-25 19:31:22 +00001462
bellard579a97f2007-11-11 14:26:47 +00001463 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1464 if (!target_vec)
1465 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001466 for(i = 0;i < count; i++) {
1467 base = tswapl(target_vec[i].iov_base);
1468 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001469 if (vec[i].iov_len != 0) {
1470 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrogd732dcb2008-10-28 10:21:03 +00001471 /* Don't check lock_user return value. We must call writev even
1472 if a element has invalid base address. */
bellard41df8412008-02-04 22:26:57 +00001473 } else {
1474 /* zero length pointer is ignored */
1475 vec[i].iov_base = NULL;
1476 }
pbrook53a59602006-03-25 19:31:22 +00001477 }
1478 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001479 return 0;
pbrook53a59602006-03-25 19:31:22 +00001480}
1481
bellard579a97f2007-11-11 14:26:47 +00001482static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1483 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001484{
1485 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001486 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001487 int i;
1488
bellard579a97f2007-11-11 14:26:47 +00001489 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1490 if (!target_vec)
1491 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001492 for(i = 0;i < count; i++) {
balrogd732dcb2008-10-28 10:21:03 +00001493 if (target_vec[i].iov_base) {
1494 base = tswapl(target_vec[i].iov_base);
1495 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1496 }
pbrook53a59602006-03-25 19:31:22 +00001497 }
1498 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001499
1500 return 0;
pbrook53a59602006-03-25 19:31:22 +00001501}
1502
ths0da46a62007-10-20 20:23:07 +00001503/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001504static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001505{
1506#if defined(TARGET_MIPS)
1507 switch(type) {
1508 case TARGET_SOCK_DGRAM:
1509 type = SOCK_DGRAM;
1510 break;
1511 case TARGET_SOCK_STREAM:
1512 type = SOCK_STREAM;
1513 break;
1514 case TARGET_SOCK_RAW:
1515 type = SOCK_RAW;
1516 break;
1517 case TARGET_SOCK_RDM:
1518 type = SOCK_RDM;
1519 break;
1520 case TARGET_SOCK_SEQPACKET:
1521 type = SOCK_SEQPACKET;
1522 break;
1523 case TARGET_SOCK_PACKET:
1524 type = SOCK_PACKET;
1525 break;
1526 }
1527#endif
balrog12bc92a2007-10-30 21:06:14 +00001528 if (domain == PF_NETLINK)
1529 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001530 return get_errno(socket(domain, type, protocol));
1531}
1532
ths0da46a62007-10-20 20:23:07 +00001533/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001534static abi_long do_bind(int sockfd, abi_ulong target_addr,
1535 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001536{
aurel328f7aeaf2009-01-30 19:47:57 +00001537 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001538 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001539
aurel32be09ac42009-04-15 16:12:06 +00001540 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001541 return -TARGET_EINVAL;
1542
aurel32607175e2009-04-15 16:11:59 +00001543 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001544
Arnaud Patard917507b2009-06-19 10:44:45 +03001545 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1546 if (ret)
1547 return ret;
1548
bellard3532fa72006-06-24 15:06:03 +00001549 return get_errno(bind(sockfd, addr, addrlen));
1550}
1551
ths0da46a62007-10-20 20:23:07 +00001552/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001553static abi_long do_connect(int sockfd, abi_ulong target_addr,
1554 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001555{
aurel328f7aeaf2009-01-30 19:47:57 +00001556 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001557 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001558
aurel32be09ac42009-04-15 16:12:06 +00001559 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001560 return -TARGET_EINVAL;
1561
1562 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001563
Arnaud Patard917507b2009-06-19 10:44:45 +03001564 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1565 if (ret)
1566 return ret;
1567
bellard3532fa72006-06-24 15:06:03 +00001568 return get_errno(connect(sockfd, addr, addrlen));
1569}
1570
ths0da46a62007-10-20 20:23:07 +00001571/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001572static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1573 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001574{
balrog6de645c2008-10-28 10:26:29 +00001575 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001576 struct target_msghdr *msgp;
1577 struct msghdr msg;
1578 int count;
1579 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001580 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001581
bellard579a97f2007-11-11 14:26:47 +00001582 /* FIXME */
1583 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1584 msgp,
1585 target_msg,
1586 send ? 1 : 0))
1587 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001588 if (msgp->msg_name) {
1589 msg.msg_namelen = tswap32(msgp->msg_namelen);
1590 msg.msg_name = alloca(msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001591 ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001592 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001593 if (ret) {
1594 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1595 return ret;
1596 }
bellard3532fa72006-06-24 15:06:03 +00001597 } else {
1598 msg.msg_name = NULL;
1599 msg.msg_namelen = 0;
1600 }
1601 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1602 msg.msg_control = alloca(msg.msg_controllen);
1603 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001604
bellard3532fa72006-06-24 15:06:03 +00001605 count = tswapl(msgp->msg_iovlen);
1606 vec = alloca(count * sizeof(struct iovec));
1607 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001608 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001609 msg.msg_iovlen = count;
1610 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001611
bellard3532fa72006-06-24 15:06:03 +00001612 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001613 ret = target_to_host_cmsg(&msg, msgp);
1614 if (ret == 0)
1615 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001616 } else {
1617 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001618 if (!is_error(ret)) {
1619 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001620 ret = host_to_target_cmsg(msgp, &msg);
balrog6de645c2008-10-28 10:26:29 +00001621 if (!is_error(ret))
1622 ret = len;
1623 }
bellard3532fa72006-06-24 15:06:03 +00001624 }
1625 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001626 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001627 return ret;
1628}
1629
ths0da46a62007-10-20 20:23:07 +00001630/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001631static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001632 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001633{
bellard2f619692007-11-16 10:46:05 +00001634 socklen_t addrlen;
1635 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001636 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001637
Arnaud Patard917507b2009-06-19 10:44:45 +03001638 if (target_addr == 0)
1639 return get_errno(accept(fd, NULL, NULL));
1640
1641 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00001642 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03001643 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00001644
aurel32be09ac42009-04-15 16:12:06 +00001645 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001646 return -TARGET_EINVAL;
1647
Arnaud Patard917507b2009-06-19 10:44:45 +03001648 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1649 return -TARGET_EINVAL;
1650
bellard2f619692007-11-16 10:46:05 +00001651 addr = alloca(addrlen);
1652
pbrook1be9e1d2006-11-19 15:26:04 +00001653 ret = get_errno(accept(fd, addr, &addrlen));
1654 if (!is_error(ret)) {
1655 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001656 if (put_user_u32(addrlen, target_addrlen_addr))
1657 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001658 }
1659 return ret;
1660}
1661
ths0da46a62007-10-20 20:23:07 +00001662/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001663static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001664 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001665{
bellard2f619692007-11-16 10:46:05 +00001666 socklen_t addrlen;
1667 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001668 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001669
bellard2f619692007-11-16 10:46:05 +00001670 if (get_user_u32(addrlen, target_addrlen_addr))
1671 return -TARGET_EFAULT;
1672
aurel32be09ac42009-04-15 16:12:06 +00001673 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001674 return -TARGET_EINVAL;
1675
Arnaud Patard917507b2009-06-19 10:44:45 +03001676 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1677 return -TARGET_EFAULT;
1678
bellard2f619692007-11-16 10:46:05 +00001679 addr = alloca(addrlen);
1680
pbrook1be9e1d2006-11-19 15:26:04 +00001681 ret = get_errno(getpeername(fd, addr, &addrlen));
1682 if (!is_error(ret)) {
1683 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001684 if (put_user_u32(addrlen, target_addrlen_addr))
1685 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001686 }
1687 return ret;
1688}
1689
ths0da46a62007-10-20 20:23:07 +00001690/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001691static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001692 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001693{
bellard2f619692007-11-16 10:46:05 +00001694 socklen_t addrlen;
1695 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001696 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001697
bellard2f619692007-11-16 10:46:05 +00001698 if (get_user_u32(addrlen, target_addrlen_addr))
1699 return -TARGET_EFAULT;
1700
aurel32be09ac42009-04-15 16:12:06 +00001701 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001702 return -TARGET_EINVAL;
1703
Arnaud Patard917507b2009-06-19 10:44:45 +03001704 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1705 return -TARGET_EFAULT;
1706
bellard2f619692007-11-16 10:46:05 +00001707 addr = alloca(addrlen);
1708
pbrook1be9e1d2006-11-19 15:26:04 +00001709 ret = get_errno(getsockname(fd, addr, &addrlen));
1710 if (!is_error(ret)) {
1711 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001712 if (put_user_u32(addrlen, target_addrlen_addr))
1713 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001714 }
1715 return ret;
1716}
1717
ths0da46a62007-10-20 20:23:07 +00001718/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001719static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001720 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001721{
1722 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001723 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001724
1725 ret = get_errno(socketpair(domain, type, protocol, tab));
1726 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001727 if (put_user_s32(tab[0], target_tab_addr)
1728 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1729 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001730 }
1731 return ret;
1732}
1733
ths0da46a62007-10-20 20:23:07 +00001734/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001735static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1736 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001737{
1738 void *addr;
1739 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001740 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001741
aurel32be09ac42009-04-15 16:12:06 +00001742 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001743 return -TARGET_EINVAL;
1744
bellard579a97f2007-11-11 14:26:47 +00001745 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1746 if (!host_msg)
1747 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001748 if (target_addr) {
1749 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001750 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1751 if (ret) {
1752 unlock_user(host_msg, msg, 0);
1753 return ret;
1754 }
pbrook1be9e1d2006-11-19 15:26:04 +00001755 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1756 } else {
1757 ret = get_errno(send(fd, host_msg, len, flags));
1758 }
1759 unlock_user(host_msg, msg, 0);
1760 return ret;
1761}
1762
ths0da46a62007-10-20 20:23:07 +00001763/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001764static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1765 abi_ulong target_addr,
1766 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001767{
1768 socklen_t addrlen;
1769 void *addr;
1770 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001771 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001772
bellard579a97f2007-11-11 14:26:47 +00001773 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1774 if (!host_msg)
1775 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001776 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001777 if (get_user_u32(addrlen, target_addrlen)) {
1778 ret = -TARGET_EFAULT;
1779 goto fail;
1780 }
aurel32be09ac42009-04-15 16:12:06 +00001781 if (addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001782 ret = -TARGET_EINVAL;
1783 goto fail;
1784 }
pbrook1be9e1d2006-11-19 15:26:04 +00001785 addr = alloca(addrlen);
1786 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1787 } else {
1788 addr = NULL; /* To keep compiler quiet. */
1789 ret = get_errno(recv(fd, host_msg, len, flags));
1790 }
1791 if (!is_error(ret)) {
1792 if (target_addr) {
1793 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001794 if (put_user_u32(addrlen, target_addrlen)) {
1795 ret = -TARGET_EFAULT;
1796 goto fail;
1797 }
pbrook1be9e1d2006-11-19 15:26:04 +00001798 }
1799 unlock_user(host_msg, msg, len);
1800 } else {
bellard2f619692007-11-16 10:46:05 +00001801fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001802 unlock_user(host_msg, msg, 0);
1803 }
1804 return ret;
1805}
1806
j_mayer32407102007-09-26 23:01:49 +00001807#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001808/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001809static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001810{
blueswir1992f48a2007-10-14 16:27:31 +00001811 abi_long ret;
1812 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001813
1814 switch(num) {
1815 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001816 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001817 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001818
Ulrich Hecht98818182009-07-03 17:09:28 +02001819 if (get_user_ual(domain, vptr)
1820 || get_user_ual(type, vptr + n)
1821 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001822 return -TARGET_EFAULT;
1823
bellard3532fa72006-06-24 15:06:03 +00001824 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001825 }
bellard31e31b82003-02-18 22:55:36 +00001826 break;
1827 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001828 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001829 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001830 abi_ulong target_addr;
1831 socklen_t addrlen;
1832
Ulrich Hecht98818182009-07-03 17:09:28 +02001833 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001834 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001835 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001836 return -TARGET_EFAULT;
1837
bellard3532fa72006-06-24 15:06:03 +00001838 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001839 }
bellard31e31b82003-02-18 22:55:36 +00001840 break;
1841 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001842 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001843 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001844 abi_ulong target_addr;
1845 socklen_t addrlen;
1846
Ulrich Hecht98818182009-07-03 17:09:28 +02001847 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001848 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001849 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001850 return -TARGET_EFAULT;
1851
bellard3532fa72006-06-24 15:06:03 +00001852 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001853 }
bellard31e31b82003-02-18 22:55:36 +00001854 break;
1855 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001856 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001857 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00001858
Ulrich Hecht98818182009-07-03 17:09:28 +02001859 if (get_user_ual(sockfd, vptr)
1860 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00001861 return -TARGET_EFAULT;
1862
bellard7854b052003-03-29 17:22:23 +00001863 ret = get_errno(listen(sockfd, backlog));
1864 }
bellard31e31b82003-02-18 22:55:36 +00001865 break;
1866 case SOCKOP_accept:
1867 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001868 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001869 abi_ulong target_addr, target_addrlen;
1870
Ulrich Hecht98818182009-07-03 17:09:28 +02001871 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001872 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001873 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001874 return -TARGET_EFAULT;
1875
pbrook1be9e1d2006-11-19 15:26:04 +00001876 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001877 }
1878 break;
1879 case SOCKOP_getsockname:
1880 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001881 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001882 abi_ulong target_addr, target_addrlen;
1883
Ulrich Hecht98818182009-07-03 17:09:28 +02001884 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001885 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001886 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001887 return -TARGET_EFAULT;
1888
pbrook1be9e1d2006-11-19 15:26:04 +00001889 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001890 }
1891 break;
1892 case SOCKOP_getpeername:
1893 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001894 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001895 abi_ulong target_addr, target_addrlen;
1896
Ulrich Hecht98818182009-07-03 17:09:28 +02001897 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001898 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001899 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001900 return -TARGET_EFAULT;
1901
pbrook1be9e1d2006-11-19 15:26:04 +00001902 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001903 }
1904 break;
1905 case SOCKOP_socketpair:
1906 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001907 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001908 abi_ulong tab;
1909
Ulrich Hecht98818182009-07-03 17:09:28 +02001910 if (get_user_ual(domain, vptr)
1911 || get_user_ual(type, vptr + n)
1912 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00001913 || get_user_ual(tab, vptr + 3 * n))
1914 return -TARGET_EFAULT;
1915
pbrook1be9e1d2006-11-19 15:26:04 +00001916 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001917 }
1918 break;
1919 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001920 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001921 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001922 abi_ulong msg;
1923 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001924 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001925
Ulrich Hecht98818182009-07-03 17:09:28 +02001926 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001927 || get_user_ual(msg, vptr + n)
1928 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001929 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00001930 return -TARGET_EFAULT;
1931
pbrook1be9e1d2006-11-19 15:26:04 +00001932 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001933 }
bellard31e31b82003-02-18 22:55:36 +00001934 break;
1935 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001936 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001937 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001938 abi_ulong msg;
1939 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001940 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001941
Ulrich Hecht98818182009-07-03 17:09:28 +02001942 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001943 || get_user_ual(msg, vptr + n)
1944 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001945 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00001946 return -TARGET_EFAULT;
1947
pbrook1be9e1d2006-11-19 15:26:04 +00001948 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001949 }
bellard31e31b82003-02-18 22:55:36 +00001950 break;
1951 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001952 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001953 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001954 abi_ulong msg;
1955 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001956 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001957 abi_ulong addr;
1958 socklen_t addrlen;
1959
Ulrich Hecht98818182009-07-03 17:09:28 +02001960 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001961 || get_user_ual(msg, vptr + n)
1962 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001963 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00001964 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001965 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00001966 return -TARGET_EFAULT;
1967
pbrook1be9e1d2006-11-19 15:26:04 +00001968 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001969 }
bellard31e31b82003-02-18 22:55:36 +00001970 break;
1971 case SOCKOP_recvfrom:
1972 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001973 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001974 abi_ulong msg;
1975 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001976 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001977 abi_ulong addr;
1978 socklen_t addrlen;
1979
Ulrich Hecht98818182009-07-03 17:09:28 +02001980 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001981 || get_user_ual(msg, vptr + n)
1982 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001983 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00001984 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001985 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00001986 return -TARGET_EFAULT;
1987
pbrook1be9e1d2006-11-19 15:26:04 +00001988 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001989 }
1990 break;
1991 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001992 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001993 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00001994
Ulrich Hecht98818182009-07-03 17:09:28 +02001995 if (get_user_ual(sockfd, vptr)
1996 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00001997 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001998
1999 ret = get_errno(shutdown(sockfd, how));
2000 }
bellard31e31b82003-02-18 22:55:36 +00002001 break;
2002 case SOCKOP_sendmsg:
2003 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002004 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002005 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002006 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002007 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002008
Ulrich Hecht98818182009-07-03 17:09:28 +02002009 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002010 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002011 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002012 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002013
ths5fafdf22007-09-16 21:08:06 +00002014 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002015 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002016 }
2017 break;
bellard31e31b82003-02-18 22:55:36 +00002018 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002019 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002020 abi_ulong sockfd;
2021 abi_ulong level;
2022 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002023 abi_ulong optval;
2024 socklen_t optlen;
2025
Ulrich Hecht98818182009-07-03 17:09:28 +02002026 if (get_user_ual(sockfd, vptr)
2027 || get_user_ual(level, vptr + n)
2028 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002029 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002030 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002031 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002032
2033 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2034 }
2035 break;
bellard31e31b82003-02-18 22:55:36 +00002036 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002037 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002038 abi_ulong sockfd;
2039 abi_ulong level;
2040 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002041 abi_ulong optval;
2042 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002043
Ulrich Hecht98818182009-07-03 17:09:28 +02002044 if (get_user_ual(sockfd, vptr)
2045 || get_user_ual(level, vptr + n)
2046 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002047 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002048 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002049 return -TARGET_EFAULT;
2050
2051 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002052 }
2053 break;
bellard31e31b82003-02-18 22:55:36 +00002054 default:
2055 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002056 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002057 break;
2058 }
2059 return ret;
2060}
j_mayer32407102007-09-26 23:01:49 +00002061#endif
bellard31e31b82003-02-18 22:55:36 +00002062
bellard8853f862004-02-22 14:57:26 +00002063#define N_SHM_REGIONS 32
2064
2065static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002066 abi_ulong start;
2067 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002068} shm_regions[N_SHM_REGIONS];
2069
ths3eb6b042007-06-03 14:26:27 +00002070struct target_ipc_perm
2071{
blueswir1992f48a2007-10-14 16:27:31 +00002072 abi_long __key;
2073 abi_ulong uid;
2074 abi_ulong gid;
2075 abi_ulong cuid;
2076 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002077 unsigned short int mode;
2078 unsigned short int __pad1;
2079 unsigned short int __seq;
2080 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002081 abi_ulong __unused1;
2082 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002083};
2084
2085struct target_semid_ds
2086{
2087 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002088 abi_ulong sem_otime;
2089 abi_ulong __unused1;
2090 abi_ulong sem_ctime;
2091 abi_ulong __unused2;
2092 abi_ulong sem_nsems;
2093 abi_ulong __unused3;
2094 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002095};
2096
bellard579a97f2007-11-11 14:26:47 +00002097static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2098 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002099{
2100 struct target_ipc_perm *target_ip;
2101 struct target_semid_ds *target_sd;
2102
bellard579a97f2007-11-11 14:26:47 +00002103 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2104 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002105 target_ip = &(target_sd->sem_perm);
ths3eb6b042007-06-03 14:26:27 +00002106 host_ip->__key = tswapl(target_ip->__key);
2107 host_ip->uid = tswapl(target_ip->uid);
2108 host_ip->gid = tswapl(target_ip->gid);
2109 host_ip->cuid = tswapl(target_ip->cuid);
2110 host_ip->cgid = tswapl(target_ip->cgid);
2111 host_ip->mode = tswapl(target_ip->mode);
2112 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002113 return 0;
ths3eb6b042007-06-03 14:26:27 +00002114}
2115
bellard579a97f2007-11-11 14:26:47 +00002116static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2117 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002118{
2119 struct target_ipc_perm *target_ip;
2120 struct target_semid_ds *target_sd;
2121
bellard579a97f2007-11-11 14:26:47 +00002122 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2123 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002124 target_ip = &(target_sd->sem_perm);
2125 target_ip->__key = tswapl(host_ip->__key);
2126 target_ip->uid = tswapl(host_ip->uid);
2127 target_ip->gid = tswapl(host_ip->gid);
2128 target_ip->cuid = tswapl(host_ip->cuid);
2129 target_ip->cgid = tswapl(host_ip->cgid);
2130 target_ip->mode = tswapl(host_ip->mode);
2131 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002132 return 0;
ths3eb6b042007-06-03 14:26:27 +00002133}
2134
bellard579a97f2007-11-11 14:26:47 +00002135static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2136 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002137{
2138 struct target_semid_ds *target_sd;
2139
bellard579a97f2007-11-11 14:26:47 +00002140 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2141 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002142 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2143 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002144 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2145 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2146 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2147 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002148 return 0;
ths3eb6b042007-06-03 14:26:27 +00002149}
2150
bellard579a97f2007-11-11 14:26:47 +00002151static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2152 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002153{
2154 struct target_semid_ds *target_sd;
2155
bellard579a97f2007-11-11 14:26:47 +00002156 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2157 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002158 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2159 return -TARGET_EFAULT;;
ths3eb6b042007-06-03 14:26:27 +00002160 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2161 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2162 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2163 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002164 return 0;
ths3eb6b042007-06-03 14:26:27 +00002165}
2166
aurel32e5289082009-04-18 16:16:12 +00002167struct target_seminfo {
2168 int semmap;
2169 int semmni;
2170 int semmns;
2171 int semmnu;
2172 int semmsl;
2173 int semopm;
2174 int semume;
2175 int semusz;
2176 int semvmx;
2177 int semaem;
2178};
2179
2180static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2181 struct seminfo *host_seminfo)
2182{
2183 struct target_seminfo *target_seminfo;
2184 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2185 return -TARGET_EFAULT;
2186 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2187 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2188 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2189 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2190 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2191 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2192 __put_user(host_seminfo->semume, &target_seminfo->semume);
2193 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2194 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2195 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2196 unlock_user_struct(target_seminfo, target_addr, 1);
2197 return 0;
2198}
2199
thsfa294812007-02-02 22:05:00 +00002200union semun {
2201 int val;
ths3eb6b042007-06-03 14:26:27 +00002202 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002203 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002204 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002205};
2206
ths3eb6b042007-06-03 14:26:27 +00002207union target_semun {
2208 int val;
aurel32e5289082009-04-18 16:16:12 +00002209 abi_ulong buf;
2210 abi_ulong array;
2211 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002212};
2213
aurel32e5289082009-04-18 16:16:12 +00002214static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2215 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002216{
aurel32e5289082009-04-18 16:16:12 +00002217 int nsems;
2218 unsigned short *array;
2219 union semun semun;
2220 struct semid_ds semid_ds;
2221 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002222
aurel32e5289082009-04-18 16:16:12 +00002223 semun.buf = &semid_ds;
2224
2225 ret = semctl(semid, 0, IPC_STAT, semun);
2226 if (ret == -1)
2227 return get_errno(ret);
2228
2229 nsems = semid_ds.sem_nsems;
2230
2231 *host_array = malloc(nsems*sizeof(unsigned short));
2232 array = lock_user(VERIFY_READ, target_addr,
2233 nsems*sizeof(unsigned short), 1);
2234 if (!array)
2235 return -TARGET_EFAULT;
2236
2237 for(i=0; i<nsems; i++) {
2238 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002239 }
aurel32e5289082009-04-18 16:16:12 +00002240 unlock_user(array, target_addr, 0);
2241
bellard579a97f2007-11-11 14:26:47 +00002242 return 0;
ths3eb6b042007-06-03 14:26:27 +00002243}
2244
aurel32e5289082009-04-18 16:16:12 +00002245static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2246 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002247{
aurel32e5289082009-04-18 16:16:12 +00002248 int nsems;
2249 unsigned short *array;
2250 union semun semun;
2251 struct semid_ds semid_ds;
2252 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002253
aurel32e5289082009-04-18 16:16:12 +00002254 semun.buf = &semid_ds;
2255
2256 ret = semctl(semid, 0, IPC_STAT, semun);
2257 if (ret == -1)
2258 return get_errno(ret);
2259
2260 nsems = semid_ds.sem_nsems;
2261
2262 array = lock_user(VERIFY_WRITE, target_addr,
2263 nsems*sizeof(unsigned short), 0);
2264 if (!array)
2265 return -TARGET_EFAULT;
2266
2267 for(i=0; i<nsems; i++) {
2268 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002269 }
aurel32e5289082009-04-18 16:16:12 +00002270 free(*host_array);
2271 unlock_user(array, target_addr, 1);
2272
bellard579a97f2007-11-11 14:26:47 +00002273 return 0;
ths3eb6b042007-06-03 14:26:27 +00002274}
2275
aurel32e5289082009-04-18 16:16:12 +00002276static inline abi_long do_semctl(int semid, int semnum, int cmd,
2277 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002278{
2279 union semun arg;
2280 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302281 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002282 struct seminfo seminfo;
2283 abi_long ret = -TARGET_EINVAL;
2284 abi_long err;
2285 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002286
2287 switch( cmd ) {
2288 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002289 case SETVAL:
aurel32e5289082009-04-18 16:16:12 +00002290 arg.val = tswapl(target_su.val);
2291 ret = get_errno(semctl(semid, semnum, cmd, arg));
2292 target_su.val = tswapl(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002293 break;
2294 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002295 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002296 err = target_to_host_semarray(semid, &array, target_su.array);
2297 if (err)
2298 return err;
2299 arg.array = array;
2300 ret = get_errno(semctl(semid, semnum, cmd, arg));
2301 err = host_to_target_semarray(semid, target_su.array, &array);
2302 if (err)
2303 return err;
ths3eb6b042007-06-03 14:26:27 +00002304 break;
2305 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002306 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002307 case SEM_STAT:
2308 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2309 if (err)
2310 return err;
2311 arg.buf = &dsarg;
2312 ret = get_errno(semctl(semid, semnum, cmd, arg));
2313 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2314 if (err)
2315 return err;
ths3eb6b042007-06-03 14:26:27 +00002316 break;
aurel32e5289082009-04-18 16:16:12 +00002317 case IPC_INFO:
2318 case SEM_INFO:
2319 arg.__buf = &seminfo;
2320 ret = get_errno(semctl(semid, semnum, cmd, arg));
2321 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2322 if (err)
2323 return err;
2324 break;
2325 case IPC_RMID:
2326 case GETPID:
2327 case GETNCNT:
2328 case GETZCNT:
2329 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2330 break;
ths3eb6b042007-06-03 14:26:27 +00002331 }
2332
2333 return ret;
2334}
2335
aurel32e5289082009-04-18 16:16:12 +00002336struct target_sembuf {
2337 unsigned short sem_num;
2338 short sem_op;
2339 short sem_flg;
2340};
2341
2342static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2343 abi_ulong target_addr,
2344 unsigned nsops)
2345{
2346 struct target_sembuf *target_sembuf;
2347 int i;
2348
2349 target_sembuf = lock_user(VERIFY_READ, target_addr,
2350 nsops*sizeof(struct target_sembuf), 1);
2351 if (!target_sembuf)
2352 return -TARGET_EFAULT;
2353
2354 for(i=0; i<nsops; i++) {
2355 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2356 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2357 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2358 }
2359
2360 unlock_user(target_sembuf, target_addr, 0);
2361
2362 return 0;
2363}
2364
2365static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2366{
2367 struct sembuf sops[nsops];
2368
2369 if (target_to_host_sembuf(sops, ptr, nsops))
2370 return -TARGET_EFAULT;
2371
2372 return semop(semid, sops, nsops);
2373}
2374
ths1bc012f2007-06-03 14:27:49 +00002375struct target_msqid_ds
2376{
aurel321c54ff92008-10-13 21:08:44 +00002377 struct target_ipc_perm msg_perm;
2378 abi_ulong msg_stime;
2379#if TARGET_ABI_BITS == 32
2380 abi_ulong __unused1;
2381#endif
2382 abi_ulong msg_rtime;
2383#if TARGET_ABI_BITS == 32
2384 abi_ulong __unused2;
2385#endif
2386 abi_ulong msg_ctime;
2387#if TARGET_ABI_BITS == 32
2388 abi_ulong __unused3;
2389#endif
2390 abi_ulong __msg_cbytes;
2391 abi_ulong msg_qnum;
2392 abi_ulong msg_qbytes;
2393 abi_ulong msg_lspid;
2394 abi_ulong msg_lrpid;
2395 abi_ulong __unused4;
2396 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002397};
2398
bellard579a97f2007-11-11 14:26:47 +00002399static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2400 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002401{
2402 struct target_msqid_ds *target_md;
2403
bellard579a97f2007-11-11 14:26:47 +00002404 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2405 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002406 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2407 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002408 host_md->msg_stime = tswapl(target_md->msg_stime);
2409 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2410 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2411 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2412 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2413 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2414 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2415 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2416 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002417 return 0;
ths1bc012f2007-06-03 14:27:49 +00002418}
2419
bellard579a97f2007-11-11 14:26:47 +00002420static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2421 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002422{
2423 struct target_msqid_ds *target_md;
2424
bellard579a97f2007-11-11 14:26:47 +00002425 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2426 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002427 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2428 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002429 target_md->msg_stime = tswapl(host_md->msg_stime);
2430 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2431 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2432 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2433 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2434 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2435 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2436 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2437 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002438 return 0;
ths1bc012f2007-06-03 14:27:49 +00002439}
2440
aurel321c54ff92008-10-13 21:08:44 +00002441struct target_msginfo {
2442 int msgpool;
2443 int msgmap;
2444 int msgmax;
2445 int msgmnb;
2446 int msgmni;
2447 int msgssz;
2448 int msgtql;
2449 unsigned short int msgseg;
2450};
2451
2452static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2453 struct msginfo *host_msginfo)
2454{
2455 struct target_msginfo *target_msginfo;
2456 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2457 return -TARGET_EFAULT;
2458 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2459 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2460 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2461 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2462 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2463 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2464 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2465 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2466 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002467 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002468}
2469
2470static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002471{
2472 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002473 struct msginfo msginfo;
2474 abi_long ret = -TARGET_EINVAL;
2475
2476 cmd &= 0xff;
2477
2478 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002479 case IPC_STAT:
2480 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002481 case MSG_STAT:
2482 if (target_to_host_msqid_ds(&dsarg,ptr))
2483 return -TARGET_EFAULT;
2484 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2485 if (host_to_target_msqid_ds(ptr,&dsarg))
2486 return -TARGET_EFAULT;
2487 break;
2488 case IPC_RMID:
2489 ret = get_errno(msgctl(msgid, cmd, NULL));
2490 break;
2491 case IPC_INFO:
2492 case MSG_INFO:
2493 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2494 if (host_to_target_msginfo(ptr, &msginfo))
2495 return -TARGET_EFAULT;
2496 break;
ths1bc012f2007-06-03 14:27:49 +00002497 }
aurel321c54ff92008-10-13 21:08:44 +00002498
ths1bc012f2007-06-03 14:27:49 +00002499 return ret;
2500}
2501
2502struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002503 abi_long mtype;
2504 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002505};
2506
blueswir1992f48a2007-10-14 16:27:31 +00002507static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2508 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002509{
2510 struct target_msgbuf *target_mb;
2511 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002512 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002513
bellard579a97f2007-11-11 14:26:47 +00002514 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2515 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002516 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002517 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2518 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002519 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2520 free(host_mb);
2521 unlock_user_struct(target_mb, msgp, 0);
2522
2523 return ret;
2524}
2525
blueswir1992f48a2007-10-14 16:27:31 +00002526static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002527 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002528 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002529{
2530 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002531 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002532 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002533 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002534
bellard579a97f2007-11-11 14:26:47 +00002535 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2536 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002537
ths1bc012f2007-06-03 14:27:49 +00002538 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002539 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2540
bellard579a97f2007-11-11 14:26:47 +00002541 if (ret > 0) {
2542 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2543 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2544 if (!target_mtext) {
2545 ret = -TARGET_EFAULT;
2546 goto end;
2547 }
aurel321c54ff92008-10-13 21:08:44 +00002548 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002549 unlock_user(target_mtext, target_mtext_addr, ret);
2550 }
aurel321c54ff92008-10-13 21:08:44 +00002551
ths1bc012f2007-06-03 14:27:49 +00002552 target_mb->mtype = tswapl(host_mb->mtype);
2553 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002554
bellard579a97f2007-11-11 14:26:47 +00002555end:
2556 if (target_mb)
2557 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00002558 return ret;
2559}
2560
Riku Voipio88a8c982009-04-03 10:42:00 +03002561struct target_shmid_ds
2562{
2563 struct target_ipc_perm shm_perm;
2564 abi_ulong shm_segsz;
2565 abi_ulong shm_atime;
2566#if TARGET_ABI_BITS == 32
2567 abi_ulong __unused1;
2568#endif
2569 abi_ulong shm_dtime;
2570#if TARGET_ABI_BITS == 32
2571 abi_ulong __unused2;
2572#endif
2573 abi_ulong shm_ctime;
2574#if TARGET_ABI_BITS == 32
2575 abi_ulong __unused3;
2576#endif
2577 int shm_cpid;
2578 int shm_lpid;
2579 abi_ulong shm_nattch;
2580 unsigned long int __unused4;
2581 unsigned long int __unused5;
2582};
2583
2584static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2585 abi_ulong target_addr)
2586{
2587 struct target_shmid_ds *target_sd;
2588
2589 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2590 return -TARGET_EFAULT;
2591 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2592 return -TARGET_EFAULT;
2593 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2594 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2595 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2596 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2597 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2598 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2599 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2600 unlock_user_struct(target_sd, target_addr, 0);
2601 return 0;
2602}
2603
2604static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2605 struct shmid_ds *host_sd)
2606{
2607 struct target_shmid_ds *target_sd;
2608
2609 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2610 return -TARGET_EFAULT;
2611 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2612 return -TARGET_EFAULT;
2613 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2614 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2615 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2616 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2617 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2618 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2619 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2620 unlock_user_struct(target_sd, target_addr, 1);
2621 return 0;
2622}
2623
2624struct target_shminfo {
2625 abi_ulong shmmax;
2626 abi_ulong shmmin;
2627 abi_ulong shmmni;
2628 abi_ulong shmseg;
2629 abi_ulong shmall;
2630};
2631
2632static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2633 struct shminfo *host_shminfo)
2634{
2635 struct target_shminfo *target_shminfo;
2636 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2637 return -TARGET_EFAULT;
2638 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2639 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2640 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2641 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2642 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2643 unlock_user_struct(target_shminfo, target_addr, 1);
2644 return 0;
2645}
2646
2647struct target_shm_info {
2648 int used_ids;
2649 abi_ulong shm_tot;
2650 abi_ulong shm_rss;
2651 abi_ulong shm_swp;
2652 abi_ulong swap_attempts;
2653 abi_ulong swap_successes;
2654};
2655
2656static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2657 struct shm_info *host_shm_info)
2658{
2659 struct target_shm_info *target_shm_info;
2660 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2661 return -TARGET_EFAULT;
2662 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2663 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2664 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2665 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2666 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2667 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2668 unlock_user_struct(target_shm_info, target_addr, 1);
2669 return 0;
2670}
2671
2672static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2673{
2674 struct shmid_ds dsarg;
2675 struct shminfo shminfo;
2676 struct shm_info shm_info;
2677 abi_long ret = -TARGET_EINVAL;
2678
2679 cmd &= 0xff;
2680
2681 switch(cmd) {
2682 case IPC_STAT:
2683 case IPC_SET:
2684 case SHM_STAT:
2685 if (target_to_host_shmid_ds(&dsarg, buf))
2686 return -TARGET_EFAULT;
2687 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2688 if (host_to_target_shmid_ds(buf, &dsarg))
2689 return -TARGET_EFAULT;
2690 break;
2691 case IPC_INFO:
2692 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2693 if (host_to_target_shminfo(buf, &shminfo))
2694 return -TARGET_EFAULT;
2695 break;
2696 case SHM_INFO:
2697 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2698 if (host_to_target_shm_info(buf, &shm_info))
2699 return -TARGET_EFAULT;
2700 break;
2701 case IPC_RMID:
2702 case SHM_LOCK:
2703 case SHM_UNLOCK:
2704 ret = get_errno(shmctl(shmid, cmd, NULL));
2705 break;
2706 }
2707
2708 return ret;
2709}
2710
2711static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2712{
2713 abi_long raddr;
2714 void *host_raddr;
2715 struct shmid_ds shm_info;
2716 int i,ret;
2717
2718 /* find out the length of the shared memory segment */
2719 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2720 if (is_error(ret)) {
2721 /* can't get length, bail out */
2722 return ret;
2723 }
2724
2725 mmap_lock();
2726
2727 if (shmaddr)
2728 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2729 else {
2730 abi_ulong mmap_start;
2731
2732 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2733
2734 if (mmap_start == -1) {
2735 errno = ENOMEM;
2736 host_raddr = (void *)-1;
2737 } else
2738 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2739 }
2740
2741 if (host_raddr == (void *)-1) {
2742 mmap_unlock();
2743 return get_errno((long)host_raddr);
2744 }
2745 raddr=h2g((unsigned long)host_raddr);
2746
2747 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2748 PAGE_VALID | PAGE_READ |
2749 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2750
2751 for (i = 0; i < N_SHM_REGIONS; i++) {
2752 if (shm_regions[i].start == 0) {
2753 shm_regions[i].start = raddr;
2754 shm_regions[i].size = shm_info.shm_segsz;
2755 break;
2756 }
2757 }
2758
2759 mmap_unlock();
2760 return raddr;
2761
2762}
2763
2764static inline abi_long do_shmdt(abi_ulong shmaddr)
2765{
2766 int i;
2767
2768 for (i = 0; i < N_SHM_REGIONS; ++i) {
2769 if (shm_regions[i].start == shmaddr) {
2770 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09002771 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03002772 break;
2773 }
2774 }
2775
2776 return get_errno(shmdt(g2h(shmaddr)));
2777}
2778
aurel321c54ff92008-10-13 21:08:44 +00002779#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00002780/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00002781/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002782static abi_long do_ipc(unsigned int call, int first,
2783 int second, int third,
2784 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00002785{
2786 int version;
blueswir1992f48a2007-10-14 16:27:31 +00002787 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00002788
2789 version = call >> 16;
2790 call &= 0xffff;
2791
2792 switch (call) {
thsfa294812007-02-02 22:05:00 +00002793 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00002794 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00002795 break;
2796
2797 case IPCOP_semget:
2798 ret = get_errno(semget(first, second, third));
2799 break;
2800
2801 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00002802 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00002803 break;
thsd96372e2007-02-02 22:05:44 +00002804
aurel321c54ff92008-10-13 21:08:44 +00002805 case IPCOP_msgget:
2806 ret = get_errno(msgget(first, second));
2807 break;
thsd96372e2007-02-02 22:05:44 +00002808
aurel321c54ff92008-10-13 21:08:44 +00002809 case IPCOP_msgsnd:
2810 ret = do_msgsnd(first, ptr, second, third);
2811 break;
thsd96372e2007-02-02 22:05:44 +00002812
aurel321c54ff92008-10-13 21:08:44 +00002813 case IPCOP_msgctl:
2814 ret = do_msgctl(first, second, ptr);
2815 break;
thsd96372e2007-02-02 22:05:44 +00002816
aurel321c54ff92008-10-13 21:08:44 +00002817 case IPCOP_msgrcv:
2818 switch (version) {
2819 case 0:
2820 {
2821 struct target_ipc_kludge {
2822 abi_long msgp;
2823 abi_long msgtyp;
2824 } *tmp;
thsd96372e2007-02-02 22:05:44 +00002825
aurel321c54ff92008-10-13 21:08:44 +00002826 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2827 ret = -TARGET_EFAULT;
2828 break;
ths1bc012f2007-06-03 14:27:49 +00002829 }
aurel321c54ff92008-10-13 21:08:44 +00002830
2831 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2832
2833 unlock_user_struct(tmp, ptr, 0);
2834 break;
2835 }
2836 default:
2837 ret = do_msgrcv(first, ptr, second, fifth, third);
2838 }
2839 break;
thsd96372e2007-02-02 22:05:44 +00002840
bellard8853f862004-02-22 14:57:26 +00002841 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03002842 switch (version) {
2843 default:
bellard5a4a8982007-11-11 17:39:18 +00002844 {
2845 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03002846 raddr = do_shmat(first, ptr, second);
2847 if (is_error(raddr))
2848 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00002849 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002850 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03002851 break;
2852 }
2853 case 1:
2854 ret = -TARGET_EINVAL;
2855 break;
bellard5a4a8982007-11-11 17:39:18 +00002856 }
bellard8853f862004-02-22 14:57:26 +00002857 break;
2858 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03002859 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00002860 break;
2861
2862 case IPCOP_shmget:
2863 /* IPC_* flag values are the same on all linux platforms */
2864 ret = get_errno(shmget(first, second, third));
2865 break;
2866
2867 /* IPC_* and SHM_* command values are the same on all linux platforms */
2868 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03002869 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00002870 break;
2871 default:
j_mayer32407102007-09-26 23:01:49 +00002872 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002873 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002874 break;
2875 }
2876 return ret;
2877}
j_mayer32407102007-09-26 23:01:49 +00002878#endif
bellard8853f862004-02-22 14:57:26 +00002879
bellard31e31b82003-02-18 22:55:36 +00002880/* kernel structure types definitions */
2881#define IFNAMSIZ 16
2882
Blue Swirl001faf32009-05-13 17:53:17 +00002883#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00002884#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2885enum {
2886#include "syscall_types.h"
2887};
2888#undef STRUCT
2889#undef STRUCT_SPECIAL
2890
Blue Swirl001faf32009-05-13 17:53:17 +00002891#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00002892#define STRUCT_SPECIAL(name)
2893#include "syscall_types.h"
2894#undef STRUCT
2895#undef STRUCT_SPECIAL
2896
2897typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002898 unsigned int target_cmd;
2899 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002900 const char *name;
2901 int access;
bellard1a9353d2003-03-16 20:28:50 +00002902 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002903} IOCTLEntry;
2904
2905#define IOC_R 0x0001
2906#define IOC_W 0x0002
2907#define IOC_RW (IOC_R | IOC_W)
2908
2909#define MAX_STRUCT_SIZE 4096
2910
blueswir19f106a72008-10-05 10:52:52 +00002911static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00002912#define IOCTL(cmd, access, ...) \
2913 { TARGET_ ## cmd, cmd, #cmd, access, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00002914#include "ioctls.h"
2915 { 0, 0, },
2916};
2917
pbrook53a59602006-03-25 19:31:22 +00002918/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002919/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002920static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002921{
2922 const IOCTLEntry *ie;
2923 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002924 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002925 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002926 int target_size;
2927 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002928
2929 ie = ioctl_entries;
2930 for(;;) {
2931 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00002932 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00002933 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002934 }
2935 if (ie->target_cmd == cmd)
2936 break;
2937 ie++;
2938 }
2939 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00002940#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00002941 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00002942#endif
bellard31e31b82003-02-18 22:55:36 +00002943 switch(arg_type[0]) {
2944 case TYPE_NULL:
2945 /* no argument */
2946 ret = get_errno(ioctl(fd, ie->host_cmd));
2947 break;
2948 case TYPE_PTRVOID:
2949 case TYPE_INT:
2950 /* int argment */
2951 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2952 break;
2953 case TYPE_PTR:
2954 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002955 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002956 switch(ie->access) {
2957 case IOC_R:
2958 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2959 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002960 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2961 if (!argptr)
2962 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002963 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2964 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002965 }
2966 break;
2967 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002968 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2969 if (!argptr)
2970 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002971 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2972 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002973 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2974 break;
2975 default:
2976 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002977 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2978 if (!argptr)
2979 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002980 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2981 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002982 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2983 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002984 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2985 if (!argptr)
2986 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002987 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2988 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002989 }
2990 break;
2991 }
2992 break;
2993 default:
j_mayer32407102007-09-26 23:01:49 +00002994 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2995 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002996 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002997 break;
2998 }
2999 return ret;
3000}
3001
blueswir1b39bc502008-10-05 10:51:10 +00003002static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003003 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3004 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3005 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3006 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3007 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3008 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3009 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3010 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3011 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3012 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3013 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3014 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3015 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3016 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3017 { 0, 0, 0, 0 }
3018};
3019
blueswir1b39bc502008-10-05 10:51:10 +00003020static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003021 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3022 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3023 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3024 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3025 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3026 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3027 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3028 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3029 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3030 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3031 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3032 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3033 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3034 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3035 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3036 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3037 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3038 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3039 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3040 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3041 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3042 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3043 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3044 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3045 { 0, 0, 0, 0 }
3046};
3047
blueswir1b39bc502008-10-05 10:51:10 +00003048static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003049 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3050 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3051 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3052 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3053 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3054 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3055 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3056 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3057 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3058 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3059 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3060 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3061 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3062 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3063 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3064 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3065 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3066 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3067 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3068 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3069 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3070 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3071 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3072 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3073 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3074 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3075 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3076 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3077 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3078 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3079 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3080 { 0, 0, 0, 0 }
3081};
3082
blueswir1b39bc502008-10-05 10:51:10 +00003083static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003084 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3085 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3086 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3087 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3088 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3089 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3090 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3091 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3092 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3093 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3094 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3095 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3096 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3097 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3098 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3099 { 0, 0, 0, 0 }
3100};
3101
3102static void target_to_host_termios (void *dst, const void *src)
3103{
3104 struct host_termios *host = dst;
3105 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003106
ths5fafdf22007-09-16 21:08:06 +00003107 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003108 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003109 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003110 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003111 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003112 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003113 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003114 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3115 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003116
Arnaud Patard44607122009-04-21 17:39:08 +03003117 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003118 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3119 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003120 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003121 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003122 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003123 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003124 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003125 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003126 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003127 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3128 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003129 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3130 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3131 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3132 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3133 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003134 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003135}
ths3b46e622007-09-17 08:09:54 +00003136
bellard31e31b82003-02-18 22:55:36 +00003137static void host_to_target_termios (void *dst, const void *src)
3138{
3139 struct target_termios *target = dst;
3140 const struct host_termios *host = src;
3141
ths5fafdf22007-09-16 21:08:06 +00003142 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003143 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003144 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003145 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003146 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003147 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003148 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003149 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3150 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003151
Arnaud Patard44607122009-04-21 17:39:08 +03003152 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003153 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3154 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3155 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3156 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3157 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3158 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3159 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3160 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3161 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3162 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3163 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3164 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3165 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3166 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3167 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3168 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3169 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3170}
3171
blueswir18e853dc2008-10-05 10:49:32 +00003172static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003173 .convert = { host_to_target_termios, target_to_host_termios },
3174 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3175 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3176};
3177
bellard5286db72003-06-05 00:57:30 +00003178static bitmask_transtbl mmap_flags_tbl[] = {
3179 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3180 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3181 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3182 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3183 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3184 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3185 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3186 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3187 { 0, 0, 0, 0 }
3188};
3189
bellard2ab83ea2003-06-15 19:56:46 +00003190#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003191
3192/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003193static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003194
bellard03acab62007-11-11 14:57:14 +00003195static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003196{
3197 int size;
pbrook53a59602006-03-25 19:31:22 +00003198 void *p;
bellard6dbad632003-03-16 18:05:05 +00003199
3200 if (!ldt_table)
3201 return 0;
3202 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3203 if (size > bytecount)
3204 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003205 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3206 if (!p)
bellard03acab62007-11-11 14:57:14 +00003207 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003208 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003209 memcpy(p, ldt_table, size);
3210 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003211 return size;
3212}
3213
3214/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003215static abi_long write_ldt(CPUX86State *env,
3216 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003217{
3218 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003219 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003220 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003221 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003222 uint32_t *lp, entry_1, entry_2;
3223
3224 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003225 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003226 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003227 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003228 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3229 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3230 ldt_info.limit = tswap32(target_ldt_info->limit);
3231 ldt_info.flags = tswap32(target_ldt_info->flags);
3232 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003233
bellard6dbad632003-03-16 18:05:05 +00003234 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003235 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003236 seg_32bit = ldt_info.flags & 1;
3237 contents = (ldt_info.flags >> 1) & 3;
3238 read_exec_only = (ldt_info.flags >> 3) & 1;
3239 limit_in_pages = (ldt_info.flags >> 4) & 1;
3240 seg_not_present = (ldt_info.flags >> 5) & 1;
3241 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003242#ifdef TARGET_ABI32
3243 lm = 0;
3244#else
3245 lm = (ldt_info.flags >> 7) & 1;
3246#endif
bellard6dbad632003-03-16 18:05:05 +00003247 if (contents == 3) {
3248 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003249 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003250 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003251 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003252 }
3253 /* allocate the LDT */
3254 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003255 env->ldt.base = target_mmap(0,
3256 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3257 PROT_READ|PROT_WRITE,
3258 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3259 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003260 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003261 memset(g2h(env->ldt.base), 0,
3262 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003263 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003264 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003265 }
3266
3267 /* NOTE: same code as Linux kernel */
3268 /* Allow LDTs to be cleared by the user. */
3269 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3270 if (oldmode ||
3271 (contents == 0 &&
3272 read_exec_only == 1 &&
3273 seg_32bit == 0 &&
3274 limit_in_pages == 0 &&
3275 seg_not_present == 1 &&
3276 useable == 0 )) {
3277 entry_1 = 0;
3278 entry_2 = 0;
3279 goto install;
3280 }
3281 }
ths3b46e622007-09-17 08:09:54 +00003282
bellard6dbad632003-03-16 18:05:05 +00003283 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3284 (ldt_info.limit & 0x0ffff);
3285 entry_2 = (ldt_info.base_addr & 0xff000000) |
3286 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3287 (ldt_info.limit & 0xf0000) |
3288 ((read_exec_only ^ 1) << 9) |
3289 (contents << 10) |
3290 ((seg_not_present ^ 1) << 15) |
3291 (seg_32bit << 22) |
3292 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00003293 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00003294 0x7000;
3295 if (!oldmode)
3296 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00003297
bellard6dbad632003-03-16 18:05:05 +00003298 /* Install the new entry ... */
3299install:
3300 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3301 lp[0] = tswap32(entry_1);
3302 lp[1] = tswap32(entry_2);
3303 return 0;
3304}
3305
3306/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00003307static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3308 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003309{
bellard03acab62007-11-11 14:57:14 +00003310 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00003311
bellard6dbad632003-03-16 18:05:05 +00003312 switch (func) {
3313 case 0:
3314 ret = read_ldt(ptr, bytecount);
3315 break;
3316 case 1:
3317 ret = write_ldt(env, ptr, bytecount, 1);
3318 break;
3319 case 0x11:
3320 ret = write_ldt(env, ptr, bytecount, 0);
3321 break;
bellard03acab62007-11-11 14:57:14 +00003322 default:
3323 ret = -TARGET_ENOSYS;
3324 break;
bellard6dbad632003-03-16 18:05:05 +00003325 }
3326 return ret;
3327}
bellard1b6b0292003-03-22 17:31:38 +00003328
blueswir14583f582008-08-24 10:35:55 +00003329#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00003330static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003331{
3332 uint64_t *gdt_table = g2h(env->gdt.base);
3333 struct target_modify_ldt_ldt_s ldt_info;
3334 struct target_modify_ldt_ldt_s *target_ldt_info;
3335 int seg_32bit, contents, read_exec_only, limit_in_pages;
3336 int seg_not_present, useable, lm;
3337 uint32_t *lp, entry_1, entry_2;
3338 int i;
3339
3340 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3341 if (!target_ldt_info)
3342 return -TARGET_EFAULT;
3343 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3344 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3345 ldt_info.limit = tswap32(target_ldt_info->limit);
3346 ldt_info.flags = tswap32(target_ldt_info->flags);
3347 if (ldt_info.entry_number == -1) {
3348 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3349 if (gdt_table[i] == 0) {
3350 ldt_info.entry_number = i;
3351 target_ldt_info->entry_number = tswap32(i);
3352 break;
3353 }
3354 }
3355 }
3356 unlock_user_struct(target_ldt_info, ptr, 1);
3357
3358 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3359 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3360 return -TARGET_EINVAL;
3361 seg_32bit = ldt_info.flags & 1;
3362 contents = (ldt_info.flags >> 1) & 3;
3363 read_exec_only = (ldt_info.flags >> 3) & 1;
3364 limit_in_pages = (ldt_info.flags >> 4) & 1;
3365 seg_not_present = (ldt_info.flags >> 5) & 1;
3366 useable = (ldt_info.flags >> 6) & 1;
3367#ifdef TARGET_ABI32
3368 lm = 0;
3369#else
3370 lm = (ldt_info.flags >> 7) & 1;
3371#endif
3372
3373 if (contents == 3) {
3374 if (seg_not_present == 0)
3375 return -TARGET_EINVAL;
3376 }
3377
3378 /* NOTE: same code as Linux kernel */
3379 /* Allow LDTs to be cleared by the user. */
3380 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3381 if ((contents == 0 &&
3382 read_exec_only == 1 &&
3383 seg_32bit == 0 &&
3384 limit_in_pages == 0 &&
3385 seg_not_present == 1 &&
3386 useable == 0 )) {
3387 entry_1 = 0;
3388 entry_2 = 0;
3389 goto install;
3390 }
3391 }
3392
3393 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3394 (ldt_info.limit & 0x0ffff);
3395 entry_2 = (ldt_info.base_addr & 0xff000000) |
3396 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3397 (ldt_info.limit & 0xf0000) |
3398 ((read_exec_only ^ 1) << 9) |
3399 (contents << 10) |
3400 ((seg_not_present ^ 1) << 15) |
3401 (seg_32bit << 22) |
3402 (limit_in_pages << 23) |
3403 (useable << 20) |
3404 (lm << 21) |
3405 0x7000;
3406
3407 /* Install the new entry ... */
3408install:
3409 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3410 lp[0] = tswap32(entry_1);
3411 lp[1] = tswap32(entry_2);
3412 return 0;
3413}
3414
blueswir18fcd3692008-08-17 20:26:25 +00003415static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003416{
3417 struct target_modify_ldt_ldt_s *target_ldt_info;
3418 uint64_t *gdt_table = g2h(env->gdt.base);
3419 uint32_t base_addr, limit, flags;
3420 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3421 int seg_not_present, useable, lm;
3422 uint32_t *lp, entry_1, entry_2;
3423
3424 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3425 if (!target_ldt_info)
3426 return -TARGET_EFAULT;
3427 idx = tswap32(target_ldt_info->entry_number);
3428 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3429 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3430 unlock_user_struct(target_ldt_info, ptr, 1);
3431 return -TARGET_EINVAL;
3432 }
3433 lp = (uint32_t *)(gdt_table + idx);
3434 entry_1 = tswap32(lp[0]);
3435 entry_2 = tswap32(lp[1]);
3436
3437 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3438 contents = (entry_2 >> 10) & 3;
3439 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3440 seg_32bit = (entry_2 >> 22) & 1;
3441 limit_in_pages = (entry_2 >> 23) & 1;
3442 useable = (entry_2 >> 20) & 1;
3443#ifdef TARGET_ABI32
3444 lm = 0;
3445#else
3446 lm = (entry_2 >> 21) & 1;
3447#endif
3448 flags = (seg_32bit << 0) | (contents << 1) |
3449 (read_exec_only << 3) | (limit_in_pages << 4) |
3450 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3451 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3452 base_addr = (entry_1 >> 16) |
3453 (entry_2 & 0xff000000) |
3454 ((entry_2 & 0xff) << 16);
3455 target_ldt_info->base_addr = tswapl(base_addr);
3456 target_ldt_info->limit = tswap32(limit);
3457 target_ldt_info->flags = tswap32(flags);
3458 unlock_user_struct(target_ldt_info, ptr, 1);
3459 return 0;
3460}
blueswir14583f582008-08-24 10:35:55 +00003461#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00003462
bellardd2fd1af2007-11-14 18:08:56 +00003463#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00003464static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00003465{
3466 abi_long ret;
3467 abi_ulong val;
3468 int idx;
3469
3470 switch(code) {
3471 case TARGET_ARCH_SET_GS:
3472 case TARGET_ARCH_SET_FS:
3473 if (code == TARGET_ARCH_SET_GS)
3474 idx = R_GS;
3475 else
3476 idx = R_FS;
3477 cpu_x86_load_seg(env, idx, 0);
3478 env->segs[idx].base = addr;
3479 break;
3480 case TARGET_ARCH_GET_GS:
3481 case TARGET_ARCH_GET_FS:
3482 if (code == TARGET_ARCH_GET_GS)
3483 idx = R_GS;
3484 else
3485 idx = R_FS;
3486 val = env->segs[idx].base;
3487 if (put_user(val, addr, abi_ulong))
3488 return -TARGET_EFAULT;
3489 break;
3490 default:
3491 ret = -TARGET_EINVAL;
3492 break;
3493 }
3494 return 0;
3495}
3496#endif
3497
bellard2ab83ea2003-06-15 19:56:46 +00003498#endif /* defined(TARGET_I386) */
3499
Juan Quintela2f7bb872009-07-27 16:13:24 +02003500#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003501
3502#define NEW_STACK_SIZE PTHREAD_STACK_MIN
3503
3504static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3505typedef struct {
3506 CPUState *env;
3507 pthread_mutex_t mutex;
3508 pthread_cond_t cond;
3509 pthread_t thread;
3510 uint32_t tid;
3511 abi_ulong child_tidptr;
3512 abi_ulong parent_tidptr;
3513 sigset_t sigmask;
3514} new_thread_info;
3515
3516static void *clone_func(void *arg)
3517{
3518 new_thread_info *info = arg;
3519 CPUState *env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003520 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00003521
3522 env = info->env;
3523 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003524 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00003525 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003526 env->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003527 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00003528 if (info->child_tidptr)
3529 put_user_u32(info->tid, info->child_tidptr);
3530 if (info->parent_tidptr)
3531 put_user_u32(info->tid, info->parent_tidptr);
3532 /* Enable signals. */
3533 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3534 /* Signal to the parent that we're ready. */
3535 pthread_mutex_lock(&info->mutex);
3536 pthread_cond_broadcast(&info->cond);
3537 pthread_mutex_unlock(&info->mutex);
3538 /* Wait until the parent has finshed initializing the tls state. */
3539 pthread_mutex_lock(&clone_lock);
3540 pthread_mutex_unlock(&clone_lock);
3541 cpu_loop(env);
3542 /* never exits */
3543 return NULL;
3544}
3545#else
bellard1b6b0292003-03-22 17:31:38 +00003546/* this stack is the equivalent of the kernel stack associated with a
3547 thread/process */
3548#define NEW_STACK_SIZE 8192
3549
3550static int clone_func(void *arg)
3551{
bellard2ab83ea2003-06-15 19:56:46 +00003552 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00003553 cpu_loop(env);
3554 /* never exits */
3555 return 0;
3556}
pbrookd865bab2008-06-07 22:12:17 +00003557#endif
bellard1b6b0292003-03-22 17:31:38 +00003558
ths0da46a62007-10-20 20:23:07 +00003559/* do_fork() Must return host values and target errnos (unlike most
3560 do_*() functions). */
pbrookd865bab2008-06-07 22:12:17 +00003561static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3562 abi_ulong parent_tidptr, target_ulong newtls,
3563 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00003564{
3565 int ret;
bellard5cd43932003-03-29 16:54:36 +00003566 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00003567 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00003568 CPUState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003569#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003570 unsigned int nptl_flags;
3571 sigset_t sigmask;
3572#endif
ths3b46e622007-09-17 08:09:54 +00003573
balrog436d1242008-09-21 02:39:45 +00003574 /* Emulate vfork() with fork() */
3575 if (flags & CLONE_VFORK)
3576 flags &= ~(CLONE_VFORK | CLONE_VM);
3577
bellard1b6b0292003-03-22 17:31:38 +00003578 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003579 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003580#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003581 new_thread_info info;
3582 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00003583#endif
pbrookc3a92832008-06-09 14:02:50 +00003584 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
pbrook624f7972008-05-31 16:11:38 +00003585 init_task_state(ts);
bellard5cd43932003-03-29 16:54:36 +00003586 new_stack = ts->stack;
bellard1b6b0292003-03-22 17:31:38 +00003587 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00003588 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00003589#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3590 cpu_reset(new_env);
3591#endif
pbrook6e68e072008-05-30 17:22:15 +00003592 /* Init regs that differ from the parent. */
3593 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00003594 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003595 ts->bprm = parent_ts->bprm;
3596 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003597#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003598 nptl_flags = flags;
3599 flags &= ~CLONE_NPTL_FLAGS2;
3600
pbrookc2764712009-03-07 15:24:59 +00003601 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3602 ts->child_tidptr = child_tidptr;
3603 }
3604
pbrookd865bab2008-06-07 22:12:17 +00003605 if (nptl_flags & CLONE_SETTLS)
3606 cpu_set_tls (new_env, newtls);
3607
3608 /* Grab a mutex so that thread setup appears atomic. */
3609 pthread_mutex_lock(&clone_lock);
3610
3611 memset(&info, 0, sizeof(info));
3612 pthread_mutex_init(&info.mutex, NULL);
3613 pthread_mutex_lock(&info.mutex);
3614 pthread_cond_init(&info.cond, NULL);
3615 info.env = new_env;
3616 if (nptl_flags & CLONE_CHILD_SETTID)
3617 info.child_tidptr = child_tidptr;
3618 if (nptl_flags & CLONE_PARENT_SETTID)
3619 info.parent_tidptr = parent_tidptr;
3620
3621 ret = pthread_attr_init(&attr);
3622 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
3623 /* It is not safe to deliver signals until the child has finished
3624 initializing, so temporarily block all signals. */
3625 sigfillset(&sigmask);
3626 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3627
3628 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00003629 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00003630
3631 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3632 pthread_attr_destroy(&attr);
3633 if (ret == 0) {
3634 /* Wait for the child to initialize. */
3635 pthread_cond_wait(&info.cond, &info.mutex);
3636 ret = info.tid;
3637 if (flags & CLONE_PARENT_SETTID)
3638 put_user_u32(ret, parent_tidptr);
3639 } else {
3640 ret = -1;
3641 }
3642 pthread_mutex_unlock(&info.mutex);
3643 pthread_cond_destroy(&info.cond);
3644 pthread_mutex_destroy(&info.mutex);
3645 pthread_mutex_unlock(&clone_lock);
3646#else
3647 if (flags & CLONE_NPTL_FLAGS2)
3648 return -EINVAL;
3649 /* This is probably going to die very quickly, but do it anyway. */
bellard27725c12003-04-29 21:08:18 +00003650#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02003651 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00003652#else
3653 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3654#endif
pbrookd865bab2008-06-07 22:12:17 +00003655#endif
bellard1b6b0292003-03-22 17:31:38 +00003656 } else {
3657 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00003658 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00003659 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00003660 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00003661 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00003662 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00003663 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00003664 cpu_clone_regs(env, newsp);
3665 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02003666#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00003667 /* There is a race condition here. The parent process could
3668 theoretically read the TID in the child process before the child
3669 tid is set. This would require using either ptrace
3670 (not implemented) or having *_tidptr to point at a shared memory
3671 mapping. We can't repeat the spinlock hack used above because
3672 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00003673 if (flags & CLONE_CHILD_SETTID)
3674 put_user_u32(gettid(), child_tidptr);
3675 if (flags & CLONE_PARENT_SETTID)
3676 put_user_u32(gettid(), parent_tidptr);
3677 ts = (TaskState *)env->opaque;
3678 if (flags & CLONE_SETTLS)
3679 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00003680 if (flags & CLONE_CHILD_CLEARTID)
3681 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00003682#endif
pbrookd865bab2008-06-07 22:12:17 +00003683 } else {
3684 fork_end(0);
3685 }
bellard1b6b0292003-03-22 17:31:38 +00003686 }
3687 return ret;
3688}
3689
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003690/* warning : doesn't handle linux specific flags... */
3691static int target_to_host_fcntl_cmd(int cmd)
3692{
3693 switch(cmd) {
3694 case TARGET_F_DUPFD:
3695 case TARGET_F_GETFD:
3696 case TARGET_F_SETFD:
3697 case TARGET_F_GETFL:
3698 case TARGET_F_SETFL:
3699 return cmd;
3700 case TARGET_F_GETLK:
3701 return F_GETLK;
3702 case TARGET_F_SETLK:
3703 return F_SETLK;
3704 case TARGET_F_SETLKW:
3705 return F_SETLKW;
3706 case TARGET_F_GETOWN:
3707 return F_GETOWN;
3708 case TARGET_F_SETOWN:
3709 return F_SETOWN;
3710 case TARGET_F_GETSIG:
3711 return F_GETSIG;
3712 case TARGET_F_SETSIG:
3713 return F_SETSIG;
3714#if TARGET_ABI_BITS == 32
3715 case TARGET_F_GETLK64:
3716 return F_GETLK64;
3717 case TARGET_F_SETLK64:
3718 return F_SETLK64;
3719 case TARGET_F_SETLKW64:
3720 return F_SETLKW64;
3721#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003722 case TARGET_F_SETLEASE:
3723 return F_SETLEASE;
3724 case TARGET_F_GETLEASE:
3725 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04003726#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003727 case TARGET_F_DUPFD_CLOEXEC:
3728 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04003729#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003730 case TARGET_F_NOTIFY:
3731 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003732 default:
3733 return -TARGET_EINVAL;
3734 }
3735 return -TARGET_EINVAL;
3736}
3737
blueswir1992f48a2007-10-14 16:27:31 +00003738static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00003739{
3740 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00003741 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00003742 struct flock64 fl64;
3743 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00003744 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003745 int host_cmd = target_to_host_fcntl_cmd(cmd);
3746
3747 if (host_cmd == -TARGET_EINVAL)
3748 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00003749
bellard7775e9e2003-05-14 22:46:48 +00003750 switch(cmd) {
3751 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00003752 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3753 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003754 fl.l_type = tswap16(target_fl->l_type);
3755 fl.l_whence = tswap16(target_fl->l_whence);
3756 fl.l_start = tswapl(target_fl->l_start);
3757 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003758 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00003759 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003760 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003761 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003762 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3763 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003764 target_fl->l_type = tswap16(fl.l_type);
3765 target_fl->l_whence = tswap16(fl.l_whence);
3766 target_fl->l_start = tswapl(fl.l_start);
3767 target_fl->l_len = tswapl(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003768 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00003769 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00003770 }
3771 break;
ths3b46e622007-09-17 08:09:54 +00003772
bellard7775e9e2003-05-14 22:46:48 +00003773 case TARGET_F_SETLK:
3774 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00003775 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3776 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003777 fl.l_type = tswap16(target_fl->l_type);
3778 fl.l_whence = tswap16(target_fl->l_whence);
3779 fl.l_start = tswapl(target_fl->l_start);
3780 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003781 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00003782 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003783 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003784 break;
ths3b46e622007-09-17 08:09:54 +00003785
bellard7775e9e2003-05-14 22:46:48 +00003786 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00003787 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3788 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003789 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3790 fl64.l_whence = tswap16(target_fl64->l_whence);
3791 fl64.l_start = tswapl(target_fl64->l_start);
3792 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003793 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00003794 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003795 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00003796 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003797 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3798 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003799 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3800 target_fl64->l_whence = tswap16(fl64.l_whence);
3801 target_fl64->l_start = tswapl(fl64.l_start);
3802 target_fl64->l_len = tswapl(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003803 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00003804 unlock_user_struct(target_fl64, arg, 1);
3805 }
bellard9ee1fa22007-11-11 15:11:19 +00003806 break;
bellard7775e9e2003-05-14 22:46:48 +00003807 case TARGET_F_SETLK64:
3808 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00003809 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3810 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003811 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3812 fl64.l_whence = tswap16(target_fl64->l_whence);
3813 fl64.l_start = tswapl(target_fl64->l_start);
3814 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003815 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00003816 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003817 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00003818 break;
3819
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003820 case TARGET_F_GETFL:
3821 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00003822 if (ret >= 0) {
3823 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3824 }
bellardffa65c32004-01-04 23:57:22 +00003825 break;
3826
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003827 case TARGET_F_SETFL:
3828 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3829 break;
3830
3831 case TARGET_F_SETOWN:
3832 case TARGET_F_GETOWN:
3833 case TARGET_F_SETSIG:
3834 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003835 case TARGET_F_SETLEASE:
3836 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003837 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00003838 break;
3839
bellard7775e9e2003-05-14 22:46:48 +00003840 default:
bellard9ee1fa22007-11-11 15:11:19 +00003841 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00003842 break;
3843 }
3844 return ret;
3845}
3846
bellard67867302003-11-23 17:05:30 +00003847#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00003848
bellard67867302003-11-23 17:05:30 +00003849static inline int high2lowuid(int uid)
3850{
3851 if (uid > 65535)
3852 return 65534;
3853 else
3854 return uid;
3855}
3856
3857static inline int high2lowgid(int gid)
3858{
3859 if (gid > 65535)
3860 return 65534;
3861 else
3862 return gid;
3863}
3864
3865static inline int low2highuid(int uid)
3866{
3867 if ((int16_t)uid == -1)
3868 return -1;
3869 else
3870 return uid;
3871}
3872
3873static inline int low2highgid(int gid)
3874{
3875 if ((int16_t)gid == -1)
3876 return -1;
3877 else
3878 return gid;
3879}
3880
3881#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00003882
bellard31e31b82003-02-18 22:55:36 +00003883void syscall_init(void)
3884{
bellard2ab83ea2003-06-15 19:56:46 +00003885 IOCTLEntry *ie;
3886 const argtype *arg_type;
3887 int size;
thsb92c47c2007-11-01 00:07:38 +00003888 int i;
bellard2ab83ea2003-06-15 19:56:46 +00003889
Blue Swirl001faf32009-05-13 17:53:17 +00003890#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00003891#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00003892#include "syscall_types.h"
3893#undef STRUCT
3894#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00003895
3896 /* we patch the ioctl size if necessary. We rely on the fact that
3897 no ioctl has all the bits at '1' in the size field */
3898 ie = ioctl_entries;
3899 while (ie->target_cmd != 0) {
3900 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3901 TARGET_IOC_SIZEMASK) {
3902 arg_type = ie->arg_type;
3903 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00003904 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00003905 ie->target_cmd);
3906 exit(1);
3907 }
3908 arg_type++;
3909 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00003910 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00003911 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3912 (size << TARGET_IOC_SIZESHIFT);
3913 }
thsb92c47c2007-11-01 00:07:38 +00003914
3915 /* Build target_to_host_errno_table[] table from
3916 * host_to_target_errno_table[]. */
3917 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3918 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3919
bellard2ab83ea2003-06-15 19:56:46 +00003920 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00003921#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3922 (defined(__x86_64__) && defined(TARGET_X86_64))
3923 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3924 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3925 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00003926 }
3927#endif
3928 ie++;
3929 }
bellard31e31b82003-02-18 22:55:36 +00003930}
bellardc573ff62004-01-04 15:51:36 +00003931
blueswir1992f48a2007-10-14 16:27:31 +00003932#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00003933static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3934{
thsaf325d32008-06-10 15:29:15 +00003935#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00003936 return ((uint64_t)word0 << 32) | word1;
3937#else
3938 return ((uint64_t)word1 << 32) | word0;
3939#endif
3940}
blueswir1992f48a2007-10-14 16:27:31 +00003941#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00003942static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3943{
3944 return word0;
3945}
blueswir1992f48a2007-10-14 16:27:31 +00003946#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00003947
3948#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00003949static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3950 abi_long arg2,
3951 abi_long arg3,
3952 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003953{
3954#ifdef TARGET_ARM
3955 if (((CPUARMState *)cpu_env)->eabi)
3956 {
3957 arg2 = arg3;
3958 arg3 = arg4;
3959 }
3960#endif
3961 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3962}
3963#endif
3964
3965#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00003966static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3967 abi_long arg2,
3968 abi_long arg3,
3969 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003970{
3971#ifdef TARGET_ARM
3972 if (((CPUARMState *)cpu_env)->eabi)
3973 {
3974 arg2 = arg3;
3975 arg3 = arg4;
3976 }
3977#endif
3978 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3979}
3980#endif
3981
bellard579a97f2007-11-11 14:26:47 +00003982static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3983 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00003984{
3985 struct target_timespec *target_ts;
3986
bellard579a97f2007-11-11 14:26:47 +00003987 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3988 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003989 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3990 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3991 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00003992 return 0;
pbrook53a59602006-03-25 19:31:22 +00003993}
3994
bellard579a97f2007-11-11 14:26:47 +00003995static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3996 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00003997{
3998 struct target_timespec *target_ts;
3999
bellard579a97f2007-11-11 14:26:47 +00004000 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4001 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004002 target_ts->tv_sec = tswapl(host_ts->tv_sec);
4003 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
4004 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004005 return 0;
pbrook53a59602006-03-25 19:31:22 +00004006}
4007
aurel329d33b762009-04-08 23:07:05 +00004008#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004009static inline abi_long host_to_target_stat64(void *cpu_env,
4010 abi_ulong target_addr,
4011 struct stat *host_st)
4012{
4013#ifdef TARGET_ARM
4014 if (((CPUARMState *)cpu_env)->eabi) {
4015 struct target_eabi_stat64 *target_st;
4016
4017 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4018 return -TARGET_EFAULT;
4019 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4020 __put_user(host_st->st_dev, &target_st->st_dev);
4021 __put_user(host_st->st_ino, &target_st->st_ino);
4022#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4023 __put_user(host_st->st_ino, &target_st->__st_ino);
4024#endif
4025 __put_user(host_st->st_mode, &target_st->st_mode);
4026 __put_user(host_st->st_nlink, &target_st->st_nlink);
4027 __put_user(host_st->st_uid, &target_st->st_uid);
4028 __put_user(host_st->st_gid, &target_st->st_gid);
4029 __put_user(host_st->st_rdev, &target_st->st_rdev);
4030 __put_user(host_st->st_size, &target_st->st_size);
4031 __put_user(host_st->st_blksize, &target_st->st_blksize);
4032 __put_user(host_st->st_blocks, &target_st->st_blocks);
4033 __put_user(host_st->st_atime, &target_st->target_st_atime);
4034 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4035 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4036 unlock_user_struct(target_st, target_addr, 1);
4037 } else
4038#endif
4039 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004040#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004041 struct target_stat *target_st;
4042#else
balrog6a24a772008-09-20 02:23:36 +00004043 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004044#endif
balrog6a24a772008-09-20 02:23:36 +00004045
4046 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4047 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004048 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004049 __put_user(host_st->st_dev, &target_st->st_dev);
4050 __put_user(host_st->st_ino, &target_st->st_ino);
4051#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4052 __put_user(host_st->st_ino, &target_st->__st_ino);
4053#endif
4054 __put_user(host_st->st_mode, &target_st->st_mode);
4055 __put_user(host_st->st_nlink, &target_st->st_nlink);
4056 __put_user(host_st->st_uid, &target_st->st_uid);
4057 __put_user(host_st->st_gid, &target_st->st_gid);
4058 __put_user(host_st->st_rdev, &target_st->st_rdev);
4059 /* XXX: better use of kernel struct */
4060 __put_user(host_st->st_size, &target_st->st_size);
4061 __put_user(host_st->st_blksize, &target_st->st_blksize);
4062 __put_user(host_st->st_blocks, &target_st->st_blocks);
4063 __put_user(host_st->st_atime, &target_st->target_st_atime);
4064 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4065 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4066 unlock_user_struct(target_st, target_addr, 1);
4067 }
4068
4069 return 0;
4070}
4071#endif
4072
Juan Quintela2f7bb872009-07-27 16:13:24 +02004073#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004074/* ??? Using host futex calls even when target atomic operations
4075 are not really atomic probably breaks things. However implementing
4076 futexes locally would make futexes shared between multiple processes
4077 tricky. However they're probably useless because guest atomic
4078 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004079static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4080 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004081{
4082 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004083 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004084
4085 /* ??? We assume FUTEX_* constants are the same on both host
4086 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004087#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004088 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004089#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004090 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004091#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004092 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004093 case FUTEX_WAIT:
4094 if (timeout) {
4095 pts = &ts;
4096 target_to_host_timespec(pts, timeout);
4097 } else {
4098 pts = NULL;
4099 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004100 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004101 pts, NULL, 0));
4102 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004103 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004104 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004105 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004106 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004107 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004108 case FUTEX_WAKE_OP:
4109 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4110 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4111 But the prototype takes a `struct timespec *'; insert casts
4112 to satisfy the compiler. We do not need to tswap TIMEOUT
4113 since it's not compared to guest memory. */
4114 pts = (struct timespec *)(uintptr_t) timeout;
4115 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4116 g2h(uaddr2),
4117 (base_op == FUTEX_CMP_REQUEUE
4118 ? tswap32(val3)
4119 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004120 default:
4121 return -TARGET_ENOSYS;
4122 }
4123}
4124#endif
4125
pbrook1d9d8b52009-04-16 15:17:02 +00004126/* Map host to target signal numbers for the wait family of syscalls.
4127 Assume all other status bits are the same. */
4128static int host_to_target_waitstatus(int status)
4129{
4130 if (WIFSIGNALED(status)) {
4131 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4132 }
4133 if (WIFSTOPPED(status)) {
4134 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4135 | (status & 0xff);
4136 }
4137 return status;
4138}
4139
pbrooka745ec62008-05-06 15:36:17 +00004140int get_osversion(void)
4141{
4142 static int osversion;
4143 struct new_utsname buf;
4144 const char *s;
4145 int i, n, tmp;
4146 if (osversion)
4147 return osversion;
4148 if (qemu_uname_release && *qemu_uname_release) {
4149 s = qemu_uname_release;
4150 } else {
4151 if (sys_uname(&buf))
4152 return 0;
4153 s = buf.release;
4154 }
4155 tmp = 0;
4156 for (i = 0; i < 3; i++) {
4157 n = 0;
4158 while (*s >= '0' && *s <= '9') {
4159 n *= 10;
4160 n += *s - '0';
4161 s++;
4162 }
4163 tmp = (tmp << 8) + n;
4164 if (*s == '.')
4165 s++;
4166 }
4167 osversion = tmp;
4168 return osversion;
4169}
4170
ths0da46a62007-10-20 20:23:07 +00004171/* do_syscall() should always have a single exit point at the end so
4172 that actions, such as logging of syscall results, can be performed.
4173 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00004174abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4175 abi_long arg2, abi_long arg3, abi_long arg4,
4176 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00004177{
blueswir1992f48a2007-10-14 16:27:31 +00004178 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004179 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00004180 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00004181 void *p;
ths3b46e622007-09-17 08:09:54 +00004182
bellard72f03902003-02-18 23:33:18 +00004183#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00004184 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00004185#endif
thsb92c47c2007-11-01 00:07:38 +00004186 if(do_strace)
4187 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4188
bellard31e31b82003-02-18 22:55:36 +00004189 switch(num) {
4190 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02004191#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00004192 /* In old applications this may be used to implement _exit(2).
4193 However in threaded applictions it is used for thread termination,
4194 and _exit_group is used for application termination.
4195 Do thread termination if we have more then one thread. */
4196 /* FIXME: This probably breaks if a signal arrives. We should probably
4197 be disabling signals. */
4198 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004199 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00004200 CPUState **lastp;
4201 CPUState *p;
4202
4203 cpu_list_lock();
4204 lastp = &first_cpu;
4205 p = first_cpu;
4206 while (p && p != (CPUState *)cpu_env) {
4207 lastp = &p->next_cpu;
4208 p = p->next_cpu;
4209 }
4210 /* If we didn't find the CPU for this thread then something is
4211 horribly wrong. */
4212 if (!p)
4213 abort();
4214 /* Remove the CPU from the list. */
4215 *lastp = p->next_cpu;
4216 cpu_list_unlock();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004217 ts = ((CPUState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00004218 if (ts->child_tidptr) {
4219 put_user_u32(0, ts->child_tidptr);
4220 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4221 NULL, NULL, 0);
4222 }
4223 /* TODO: Free CPU state. */
4224 pthread_exit(NULL);
4225 }
4226#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02004227#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00004228 _mcleanup();
4229#endif
bellarde9009672005-04-26 20:42:36 +00004230 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00004231 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00004232 ret = 0; /* avoid warning */
4233 break;
4234 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00004235 if (arg3 == 0)
4236 ret = 0;
4237 else {
4238 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4239 goto efault;
4240 ret = get_errno(read(arg1, p, arg3));
4241 unlock_user(p, arg2, ret);
4242 }
bellard31e31b82003-02-18 22:55:36 +00004243 break;
4244 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00004245 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4246 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004247 ret = get_errno(write(arg1, p, arg3));
4248 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00004249 break;
4250 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00004251 if (!(p = lock_user_string(arg1)))
4252 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004253 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00004254 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4255 arg3));
pbrook53a59602006-03-25 19:31:22 +00004256 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004257 break;
ths82424832007-09-24 09:21:55 +00004258#if defined(TARGET_NR_openat) && defined(__NR_openat)
4259 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00004260 if (!(p = lock_user_string(arg2)))
4261 goto efault;
4262 ret = get_errno(sys_openat(arg1,
4263 path(p),
4264 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4265 arg4));
4266 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00004267 break;
4268#endif
bellard31e31b82003-02-18 22:55:36 +00004269 case TARGET_NR_close:
4270 ret = get_errno(close(arg1));
4271 break;
4272 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00004273 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00004274 break;
4275 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00004276 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00004277 break;
thse5febef2007-04-01 18:31:35 +00004278#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00004279 case TARGET_NR_waitpid:
4280 {
pbrook53a59602006-03-25 19:31:22 +00004281 int status;
4282 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00004283 if (!is_error(ret) && arg2
pbrook1d9d8b52009-04-16 15:17:02 +00004284 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00004285 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004286 }
4287 break;
thse5febef2007-04-01 18:31:35 +00004288#endif
pbrookf0cbb612008-05-30 18:20:05 +00004289#ifdef TARGET_NR_waitid
4290 case TARGET_NR_waitid:
4291 {
4292 siginfo_t info;
4293 info.si_pid = 0;
4294 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4295 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004296 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00004297 goto efault;
4298 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05004299 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00004300 }
4301 }
4302 break;
4303#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004304#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004305 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00004306 if (!(p = lock_user_string(arg1)))
4307 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004308 ret = get_errno(creat(p, arg2));
4309 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004310 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004311#endif
bellard31e31b82003-02-18 22:55:36 +00004312 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00004313 {
4314 void * p2;
4315 p = lock_user_string(arg1);
4316 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004317 if (!p || !p2)
4318 ret = -TARGET_EFAULT;
4319 else
4320 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004321 unlock_user(p2, arg2, 0);
4322 unlock_user(p, arg1, 0);
4323 }
bellard31e31b82003-02-18 22:55:36 +00004324 break;
ths64f0ce42007-09-24 09:25:06 +00004325#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4326 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00004327 {
4328 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00004329 if (!arg2 || !arg4)
4330 goto efault;
ths64f0ce42007-09-24 09:25:06 +00004331 p = lock_user_string(arg2);
4332 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004333 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004334 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00004335 else
4336 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00004337 unlock_user(p, arg2, 0);
4338 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00004339 }
4340 break;
4341#endif
bellard31e31b82003-02-18 22:55:36 +00004342 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00004343 if (!(p = lock_user_string(arg1)))
4344 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004345 ret = get_errno(unlink(p));
4346 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004347 break;
ths8170f562007-09-24 09:24:11 +00004348#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4349 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00004350 if (!(p = lock_user_string(arg2)))
4351 goto efault;
4352 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4353 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00004354 break;
balrogb7d35e62007-12-12 00:40:24 +00004355#endif
bellard31e31b82003-02-18 22:55:36 +00004356 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00004357 {
4358 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00004359 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00004360 abi_ulong gp;
4361 abi_ulong guest_argp;
4362 abi_ulong guest_envp;
4363 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00004364 char **q;
4365
bellardf7341ff2003-03-30 21:00:25 +00004366 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00004367 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00004368 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004369 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004370 goto efault;
ths03aa1972007-12-02 06:28:08 +00004371 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004372 break;
bellard7854b052003-03-29 17:22:23 +00004373 argc++;
bellard2f619692007-11-16 10:46:05 +00004374 }
bellardf7341ff2003-03-30 21:00:25 +00004375 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00004376 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00004377 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004378 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004379 goto efault;
ths03aa1972007-12-02 06:28:08 +00004380 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004381 break;
bellard7854b052003-03-29 17:22:23 +00004382 envc++;
bellard2f619692007-11-16 10:46:05 +00004383 }
bellard7854b052003-03-29 17:22:23 +00004384
bellardf7341ff2003-03-30 21:00:25 +00004385 argp = alloca((argc + 1) * sizeof(void *));
4386 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00004387
pbrookda94d262008-05-30 18:24:00 +00004388 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004389 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004390 if (get_user_ual(addr, gp))
4391 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004392 if (!addr)
4393 break;
bellard2f619692007-11-16 10:46:05 +00004394 if (!(*q = lock_user_string(addr)))
4395 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004396 }
bellardf7341ff2003-03-30 21:00:25 +00004397 *q = NULL;
4398
pbrookda94d262008-05-30 18:24:00 +00004399 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004400 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004401 if (get_user_ual(addr, gp))
4402 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004403 if (!addr)
4404 break;
bellard2f619692007-11-16 10:46:05 +00004405 if (!(*q = lock_user_string(addr)))
4406 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004407 }
bellardf7341ff2003-03-30 21:00:25 +00004408 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00004409
bellard2f619692007-11-16 10:46:05 +00004410 if (!(p = lock_user_string(arg1)))
4411 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004412 ret = get_errno(execve(p, argp, envp));
4413 unlock_user(p, arg1, 0);
4414
bellard2f619692007-11-16 10:46:05 +00004415 goto execve_end;
4416
4417 execve_efault:
4418 ret = -TARGET_EFAULT;
4419
4420 execve_end:
pbrook53a59602006-03-25 19:31:22 +00004421 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004422 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004423 if (get_user_ual(addr, gp)
4424 || !addr)
4425 break;
pbrook53a59602006-03-25 19:31:22 +00004426 unlock_user(*q, addr, 0);
4427 }
4428 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004429 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004430 if (get_user_ual(addr, gp)
4431 || !addr)
4432 break;
pbrook53a59602006-03-25 19:31:22 +00004433 unlock_user(*q, addr, 0);
4434 }
bellard7854b052003-03-29 17:22:23 +00004435 }
bellard31e31b82003-02-18 22:55:36 +00004436 break;
4437 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00004438 if (!(p = lock_user_string(arg1)))
4439 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004440 ret = get_errno(chdir(p));
4441 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004442 break;
bellarda315a142005-01-30 22:59:18 +00004443#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00004444 case TARGET_NR_time:
4445 {
pbrook53a59602006-03-25 19:31:22 +00004446 time_t host_time;
4447 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00004448 if (!is_error(ret)
4449 && arg1
4450 && put_user_sal(host_time, arg1))
4451 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004452 }
4453 break;
bellarda315a142005-01-30 22:59:18 +00004454#endif
bellard31e31b82003-02-18 22:55:36 +00004455 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00004456 if (!(p = lock_user_string(arg1)))
4457 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004458 ret = get_errno(mknod(p, arg2, arg3));
4459 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004460 break;
ths75ac37a2007-09-24 09:23:05 +00004461#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4462 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00004463 if (!(p = lock_user_string(arg2)))
4464 goto efault;
4465 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4466 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00004467 break;
4468#endif
bellard31e31b82003-02-18 22:55:36 +00004469 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00004470 if (!(p = lock_user_string(arg1)))
4471 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004472 ret = get_errno(chmod(p, arg2));
4473 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004474 break;
bellardebc05482003-09-30 21:08:41 +00004475#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00004476 case TARGET_NR_break:
4477 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004478#endif
4479#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00004480 case TARGET_NR_oldstat:
4481 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004482#endif
bellard31e31b82003-02-18 22:55:36 +00004483 case TARGET_NR_lseek:
4484 ret = get_errno(lseek(arg1, arg2, arg3));
4485 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004486#ifdef TARGET_NR_getxpid
4487 case TARGET_NR_getxpid:
4488#else
bellard31e31b82003-02-18 22:55:36 +00004489 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00004490#endif
bellard31e31b82003-02-18 22:55:36 +00004491 ret = get_errno(getpid());
4492 break;
4493 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00004494 {
4495 /* need to look at the data field */
4496 void *p2, *p3;
4497 p = lock_user_string(arg1);
4498 p2 = lock_user_string(arg2);
4499 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004500 if (!p || !p2 || !p3)
4501 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004502 else {
bellard579a97f2007-11-11 14:26:47 +00004503 /* FIXME - arg5 should be locked, but it isn't clear how to
4504 * do that since it's not guaranteed to be a NULL-terminated
4505 * string.
4506 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004507 if ( ! arg5 )
4508 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4509 else
4510 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4511 }
bellard579a97f2007-11-11 14:26:47 +00004512 unlock_user(p, arg1, 0);
4513 unlock_user(p2, arg2, 0);
4514 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00004515 break;
4516 }
thse5febef2007-04-01 18:31:35 +00004517#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00004518 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00004519 if (!(p = lock_user_string(arg1)))
4520 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004521 ret = get_errno(umount(p));
4522 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004523 break;
thse5febef2007-04-01 18:31:35 +00004524#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004525#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004526 case TARGET_NR_stime:
4527 {
pbrook53a59602006-03-25 19:31:22 +00004528 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00004529 if (get_user_sal(host_time, arg1))
4530 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004531 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00004532 }
4533 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004534#endif
bellard31e31b82003-02-18 22:55:36 +00004535 case TARGET_NR_ptrace:
4536 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00004537#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004538 case TARGET_NR_alarm:
4539 ret = alarm(arg1);
4540 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004541#endif
bellardebc05482003-09-30 21:08:41 +00004542#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00004543 case TARGET_NR_oldfstat:
4544 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004545#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004546#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004547 case TARGET_NR_pause:
4548 ret = get_errno(pause());
4549 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004550#endif
thse5febef2007-04-01 18:31:35 +00004551#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00004552 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00004553 {
pbrook53a59602006-03-25 19:31:22 +00004554 struct utimbuf tbuf, *host_tbuf;
4555 struct target_utimbuf *target_tbuf;
4556 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004557 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4558 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004559 tbuf.actime = tswapl(target_tbuf->actime);
4560 tbuf.modtime = tswapl(target_tbuf->modtime);
4561 unlock_user_struct(target_tbuf, arg2, 0);
4562 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00004563 } else {
pbrook53a59602006-03-25 19:31:22 +00004564 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00004565 }
bellard579a97f2007-11-11 14:26:47 +00004566 if (!(p = lock_user_string(arg1)))
4567 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004568 ret = get_errno(utime(p, host_tbuf));
4569 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00004570 }
4571 break;
thse5febef2007-04-01 18:31:35 +00004572#endif
bellard978a66f2004-12-06 22:58:05 +00004573 case TARGET_NR_utimes:
4574 {
bellard978a66f2004-12-06 22:58:05 +00004575 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00004576 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00004577 if (copy_from_user_timeval(&tv[0], arg2)
4578 || copy_from_user_timeval(&tv[1],
4579 arg2 + sizeof(struct target_timeval)))
4580 goto efault;
bellard978a66f2004-12-06 22:58:05 +00004581 tvp = tv;
4582 } else {
4583 tvp = NULL;
4584 }
bellard579a97f2007-11-11 14:26:47 +00004585 if (!(p = lock_user_string(arg1)))
4586 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004587 ret = get_errno(utimes(p, tvp));
4588 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00004589 }
4590 break;
balrogac8a6552008-09-20 02:25:39 +00004591#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4592 case TARGET_NR_futimesat:
4593 {
4594 struct timeval *tvp, tv[2];
4595 if (arg3) {
4596 if (copy_from_user_timeval(&tv[0], arg3)
4597 || copy_from_user_timeval(&tv[1],
4598 arg3 + sizeof(struct target_timeval)))
4599 goto efault;
4600 tvp = tv;
4601 } else {
4602 tvp = NULL;
4603 }
4604 if (!(p = lock_user_string(arg2)))
4605 goto efault;
4606 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4607 unlock_user(p, arg2, 0);
4608 }
4609 break;
4610#endif
bellardebc05482003-09-30 21:08:41 +00004611#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00004612 case TARGET_NR_stty:
4613 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004614#endif
4615#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00004616 case TARGET_NR_gtty:
4617 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004618#endif
bellard31e31b82003-02-18 22:55:36 +00004619 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00004620 if (!(p = lock_user_string(arg1)))
4621 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02004622 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00004623 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004624 break;
ths92a34c12007-09-24 09:27:49 +00004625#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4626 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00004627 if (!(p = lock_user_string(arg2)))
4628 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00004629 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00004630 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00004631 break;
4632#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004633#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004634 case TARGET_NR_nice:
4635 ret = get_errno(nice(arg1));
4636 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004637#endif
bellardebc05482003-09-30 21:08:41 +00004638#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00004639 case TARGET_NR_ftime:
4640 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004641#endif
bellard31e31b82003-02-18 22:55:36 +00004642 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00004643 sync();
4644 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00004645 break;
4646 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00004647 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00004648 break;
4649 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00004650 {
4651 void *p2;
4652 p = lock_user_string(arg1);
4653 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004654 if (!p || !p2)
4655 ret = -TARGET_EFAULT;
4656 else
4657 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004658 unlock_user(p2, arg2, 0);
4659 unlock_user(p, arg1, 0);
4660 }
bellard31e31b82003-02-18 22:55:36 +00004661 break;
ths722183f2007-09-24 09:24:37 +00004662#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4663 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00004664 {
bellard579a97f2007-11-11 14:26:47 +00004665 void *p2;
ths722183f2007-09-24 09:24:37 +00004666 p = lock_user_string(arg2);
4667 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004668 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004669 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00004670 else
4671 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00004672 unlock_user(p2, arg4, 0);
4673 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00004674 }
4675 break;
4676#endif
bellard31e31b82003-02-18 22:55:36 +00004677 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00004678 if (!(p = lock_user_string(arg1)))
4679 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004680 ret = get_errno(mkdir(p, arg2));
4681 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004682 break;
ths4472ad02007-09-24 09:22:32 +00004683#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4684 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00004685 if (!(p = lock_user_string(arg2)))
4686 goto efault;
4687 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4688 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00004689 break;
4690#endif
bellard31e31b82003-02-18 22:55:36 +00004691 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00004692 if (!(p = lock_user_string(arg1)))
4693 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004694 ret = get_errno(rmdir(p));
4695 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004696 break;
4697 case TARGET_NR_dup:
4698 ret = get_errno(dup(arg1));
4699 break;
4700 case TARGET_NR_pipe:
Riku Voipio099d6b02009-05-05 12:10:04 +03004701 ret = do_pipe(cpu_env, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004702 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03004703#ifdef TARGET_NR_pipe2
4704 case TARGET_NR_pipe2:
4705 ret = do_pipe(cpu_env, arg1, arg2);
4706 break;
4707#endif
bellard31e31b82003-02-18 22:55:36 +00004708 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00004709 {
pbrook53a59602006-03-25 19:31:22 +00004710 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00004711 struct tms tms;
4712 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00004713 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00004714 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4715 if (!tmsp)
4716 goto efault;
bellardc596ed12003-07-13 17:32:31 +00004717 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4718 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4719 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4720 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00004721 }
bellardc596ed12003-07-13 17:32:31 +00004722 if (!is_error(ret))
4723 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00004724 }
4725 break;
bellardebc05482003-09-30 21:08:41 +00004726#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00004727 case TARGET_NR_prof:
4728 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004729#endif
thse5febef2007-04-01 18:31:35 +00004730#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00004731 case TARGET_NR_signal:
4732 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004733#endif
bellard31e31b82003-02-18 22:55:36 +00004734 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00004735 if (arg1 == 0) {
4736 ret = get_errno(acct(NULL));
4737 } else {
4738 if (!(p = lock_user_string(arg1)))
4739 goto efault;
4740 ret = get_errno(acct(path(p)));
4741 unlock_user(p, arg1, 0);
4742 }
pbrook24836682006-04-16 14:14:53 +00004743 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004744#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004745 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00004746 if (!(p = lock_user_string(arg1)))
4747 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004748 ret = get_errno(umount2(p, arg2));
4749 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004750 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004751#endif
bellardebc05482003-09-30 21:08:41 +00004752#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00004753 case TARGET_NR_lock:
4754 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004755#endif
bellard31e31b82003-02-18 22:55:36 +00004756 case TARGET_NR_ioctl:
4757 ret = do_ioctl(arg1, arg2, arg3);
4758 break;
4759 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00004760 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00004761 break;
bellardebc05482003-09-30 21:08:41 +00004762#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00004763 case TARGET_NR_mpx:
4764 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004765#endif
bellard31e31b82003-02-18 22:55:36 +00004766 case TARGET_NR_setpgid:
4767 ret = get_errno(setpgid(arg1, arg2));
4768 break;
bellardebc05482003-09-30 21:08:41 +00004769#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00004770 case TARGET_NR_ulimit:
4771 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004772#endif
4773#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00004774 case TARGET_NR_oldolduname:
4775 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004776#endif
bellard31e31b82003-02-18 22:55:36 +00004777 case TARGET_NR_umask:
4778 ret = get_errno(umask(arg1));
4779 break;
4780 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00004781 if (!(p = lock_user_string(arg1)))
4782 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004783 ret = get_errno(chroot(p));
4784 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004785 break;
4786 case TARGET_NR_ustat:
4787 goto unimplemented;
4788 case TARGET_NR_dup2:
4789 ret = get_errno(dup2(arg1, arg2));
4790 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03004791#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
4792 case TARGET_NR_dup3:
4793 ret = get_errno(dup3(arg1, arg2, arg3));
4794 break;
4795#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004796#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004797 case TARGET_NR_getppid:
4798 ret = get_errno(getppid());
4799 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004800#endif
bellard31e31b82003-02-18 22:55:36 +00004801 case TARGET_NR_getpgrp:
4802 ret = get_errno(getpgrp());
4803 break;
4804 case TARGET_NR_setsid:
4805 ret = get_errno(setsid());
4806 break;
thse5febef2007-04-01 18:31:35 +00004807#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00004808 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00004809 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08004810#if defined(TARGET_ALPHA)
4811 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00004812 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00004813 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004814 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4815 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004816 act._sa_handler = old_act->_sa_handler;
4817 target_siginitset(&act.sa_mask, old_act->sa_mask);
4818 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08004819 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00004820 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004821 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00004822 }
4823 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00004824 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004825 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4826 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004827 old_act->_sa_handler = oact._sa_handler;
4828 old_act->sa_mask = oact.sa_mask.sig[0];
4829 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00004830 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00004831 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08004832#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00004833 struct target_sigaction act, oact, *pact, *old_act;
4834
4835 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004836 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4837 goto efault;
bellard106ec872006-06-27 21:08:10 +00004838 act._sa_handler = old_act->_sa_handler;
4839 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4840 act.sa_flags = old_act->sa_flags;
4841 unlock_user_struct(old_act, arg2, 0);
4842 pact = &act;
4843 } else {
4844 pact = NULL;
4845 }
4846
4847 ret = get_errno(do_sigaction(arg1, pact, &oact));
4848
4849 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004850 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4851 goto efault;
bellard106ec872006-06-27 21:08:10 +00004852 old_act->_sa_handler = oact._sa_handler;
4853 old_act->sa_flags = oact.sa_flags;
4854 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4855 old_act->sa_mask.sig[1] = 0;
4856 old_act->sa_mask.sig[2] = 0;
4857 old_act->sa_mask.sig[3] = 0;
4858 unlock_user_struct(old_act, arg3, 1);
4859 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08004860#else
4861 struct target_old_sigaction *old_act;
4862 struct target_sigaction act, oact, *pact;
4863 if (arg2) {
4864 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4865 goto efault;
4866 act._sa_handler = old_act->_sa_handler;
4867 target_siginitset(&act.sa_mask, old_act->sa_mask);
4868 act.sa_flags = old_act->sa_flags;
4869 act.sa_restorer = old_act->sa_restorer;
4870 unlock_user_struct(old_act, arg2, 0);
4871 pact = &act;
4872 } else {
4873 pact = NULL;
4874 }
4875 ret = get_errno(do_sigaction(arg1, pact, &oact));
4876 if (!is_error(ret) && arg3) {
4877 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4878 goto efault;
4879 old_act->_sa_handler = oact._sa_handler;
4880 old_act->sa_mask = oact.sa_mask.sig[0];
4881 old_act->sa_flags = oact.sa_flags;
4882 old_act->sa_restorer = oact.sa_restorer;
4883 unlock_user_struct(old_act, arg3, 1);
4884 }
ths388bb212007-05-13 13:58:00 +00004885#endif
bellard31e31b82003-02-18 22:55:36 +00004886 }
4887 break;
thse5febef2007-04-01 18:31:35 +00004888#endif
bellard66fb9762003-03-23 01:06:05 +00004889 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00004890 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08004891#if defined(TARGET_ALPHA)
4892 struct target_sigaction act, oact, *pact = 0;
4893 struct target_rt_sigaction *rt_act;
4894 /* ??? arg4 == sizeof(sigset_t). */
4895 if (arg2) {
4896 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
4897 goto efault;
4898 act._sa_handler = rt_act->_sa_handler;
4899 act.sa_mask = rt_act->sa_mask;
4900 act.sa_flags = rt_act->sa_flags;
4901 act.sa_restorer = arg5;
4902 unlock_user_struct(rt_act, arg2, 0);
4903 pact = &act;
4904 }
4905 ret = get_errno(do_sigaction(arg1, pact, &oact));
4906 if (!is_error(ret) && arg3) {
4907 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
4908 goto efault;
4909 rt_act->_sa_handler = oact._sa_handler;
4910 rt_act->sa_mask = oact.sa_mask;
4911 rt_act->sa_flags = oact.sa_flags;
4912 unlock_user_struct(rt_act, arg3, 1);
4913 }
4914#else
pbrook53a59602006-03-25 19:31:22 +00004915 struct target_sigaction *act;
4916 struct target_sigaction *oact;
4917
bellard579a97f2007-11-11 14:26:47 +00004918 if (arg2) {
4919 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4920 goto efault;
4921 } else
pbrook53a59602006-03-25 19:31:22 +00004922 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00004923 if (arg3) {
4924 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4925 ret = -TARGET_EFAULT;
4926 goto rt_sigaction_fail;
4927 }
4928 } else
pbrook53a59602006-03-25 19:31:22 +00004929 oact = NULL;
4930 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00004931 rt_sigaction_fail:
4932 if (act)
pbrook53a59602006-03-25 19:31:22 +00004933 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00004934 if (oact)
pbrook53a59602006-03-25 19:31:22 +00004935 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08004936#endif
pbrook53a59602006-03-25 19:31:22 +00004937 }
bellard66fb9762003-03-23 01:06:05 +00004938 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004939#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004940 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00004941 {
4942 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00004943 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00004944 sigprocmask(0, NULL, &cur_set);
4945 host_to_target_old_sigset(&target_set, &cur_set);
4946 ret = target_set;
4947 }
4948 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004949#endif
4950#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004951 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00004952 {
4953 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00004954 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00004955 sigprocmask(0, NULL, &cur_set);
4956 target_to_host_old_sigset(&set, &target_set);
4957 sigorset(&set, &set, &cur_set);
4958 sigprocmask(SIG_SETMASK, &set, &oset);
4959 host_to_target_old_sigset(&target_set, &oset);
4960 ret = target_set;
4961 }
4962 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004963#endif
thse5febef2007-04-01 18:31:35 +00004964#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00004965 case TARGET_NR_sigprocmask:
4966 {
4967 int how = arg1;
4968 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00004969
pbrook53a59602006-03-25 19:31:22 +00004970 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004971 switch(how) {
4972 case TARGET_SIG_BLOCK:
4973 how = SIG_BLOCK;
4974 break;
4975 case TARGET_SIG_UNBLOCK:
4976 how = SIG_UNBLOCK;
4977 break;
4978 case TARGET_SIG_SETMASK:
4979 how = SIG_SETMASK;
4980 break;
4981 default:
ths0da46a62007-10-20 20:23:07 +00004982 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00004983 goto fail;
4984 }
Anthony Liguoric227f092009-10-01 16:12:16 -05004985 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00004986 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004987 target_to_host_old_sigset(&set, p);
4988 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004989 set_ptr = &set;
4990 } else {
4991 how = 0;
4992 set_ptr = NULL;
4993 }
4994 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00004995 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004996 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00004997 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004998 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05004999 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005000 }
5001 }
5002 break;
thse5febef2007-04-01 18:31:35 +00005003#endif
bellard66fb9762003-03-23 01:06:05 +00005004 case TARGET_NR_rt_sigprocmask:
5005 {
5006 int how = arg1;
5007 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00005008
pbrook53a59602006-03-25 19:31:22 +00005009 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005010 switch(how) {
5011 case TARGET_SIG_BLOCK:
5012 how = SIG_BLOCK;
5013 break;
5014 case TARGET_SIG_UNBLOCK:
5015 how = SIG_UNBLOCK;
5016 break;
5017 case TARGET_SIG_SETMASK:
5018 how = SIG_SETMASK;
5019 break;
5020 default:
ths0da46a62007-10-20 20:23:07 +00005021 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005022 goto fail;
5023 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005024 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005025 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005026 target_to_host_sigset(&set, p);
5027 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005028 set_ptr = &set;
5029 } else {
5030 how = 0;
5031 set_ptr = NULL;
5032 }
5033 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005034 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005035 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005036 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005037 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005038 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005039 }
5040 }
5041 break;
thse5febef2007-04-01 18:31:35 +00005042#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005043 case TARGET_NR_sigpending:
5044 {
5045 sigset_t set;
5046 ret = get_errno(sigpending(&set));
5047 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005048 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005049 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005050 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005051 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005052 }
5053 }
5054 break;
thse5febef2007-04-01 18:31:35 +00005055#endif
bellard66fb9762003-03-23 01:06:05 +00005056 case TARGET_NR_rt_sigpending:
5057 {
5058 sigset_t set;
5059 ret = get_errno(sigpending(&set));
5060 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005061 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005062 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005063 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005064 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005065 }
5066 }
5067 break;
thse5febef2007-04-01 18:31:35 +00005068#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00005069 case TARGET_NR_sigsuspend:
5070 {
5071 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005072 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005073 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005074 target_to_host_old_sigset(&set, p);
5075 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005076 ret = get_errno(sigsuspend(&set));
5077 }
5078 break;
thse5febef2007-04-01 18:31:35 +00005079#endif
bellard66fb9762003-03-23 01:06:05 +00005080 case TARGET_NR_rt_sigsuspend:
5081 {
5082 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005083 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005084 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005085 target_to_host_sigset(&set, p);
5086 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005087 ret = get_errno(sigsuspend(&set));
5088 }
5089 break;
5090 case TARGET_NR_rt_sigtimedwait:
5091 {
bellard66fb9762003-03-23 01:06:05 +00005092 sigset_t set;
5093 struct timespec uts, *puts;
5094 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00005095
Anthony Liguoric227f092009-10-01 16:12:16 -05005096 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005097 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005098 target_to_host_sigset(&set, p);
5099 unlock_user(p, arg1, 0);
5100 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00005101 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00005102 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00005103 } else {
5104 puts = NULL;
5105 }
5106 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00005107 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005108 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005109 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005110 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05005111 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00005112 }
5113 }
5114 break;
5115 case TARGET_NR_rt_sigqueueinfo:
5116 {
5117 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05005118 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005119 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005120 target_to_host_siginfo(&uinfo, p);
5121 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005122 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5123 }
5124 break;
thse5febef2007-04-01 18:31:35 +00005125#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00005126 case TARGET_NR_sigreturn:
5127 /* NOTE: ret is eax, so not transcoding must be done */
5128 ret = do_sigreturn(cpu_env);
5129 break;
thse5febef2007-04-01 18:31:35 +00005130#endif
bellard66fb9762003-03-23 01:06:05 +00005131 case TARGET_NR_rt_sigreturn:
5132 /* NOTE: ret is eax, so not transcoding must be done */
5133 ret = do_rt_sigreturn(cpu_env);
5134 break;
bellard31e31b82003-02-18 22:55:36 +00005135 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00005136 if (!(p = lock_user_string(arg1)))
5137 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005138 ret = get_errno(sethostname(p, arg2));
5139 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005140 break;
5141 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00005142 {
bellard9de5e442003-03-23 16:49:39 +00005143 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005144 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005145 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00005146 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5147 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005148 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5149 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005150 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00005151 ret = get_errno(setrlimit(resource, &rlim));
5152 }
5153 break;
bellard31e31b82003-02-18 22:55:36 +00005154 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00005155 {
bellard9de5e442003-03-23 16:49:39 +00005156 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005157 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005158 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00005159
bellard9de5e442003-03-23 16:49:39 +00005160 ret = get_errno(getrlimit(resource, &rlim));
5161 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005162 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5163 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005164 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5165 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005166 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00005167 }
5168 }
5169 break;
bellard31e31b82003-02-18 22:55:36 +00005170 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00005171 {
5172 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00005173 ret = get_errno(getrusage(arg1, &rusage));
5174 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005175 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00005176 }
5177 }
5178 break;
bellard31e31b82003-02-18 22:55:36 +00005179 case TARGET_NR_gettimeofday:
5180 {
bellard31e31b82003-02-18 22:55:36 +00005181 struct timeval tv;
5182 ret = get_errno(gettimeofday(&tv, NULL));
5183 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00005184 if (copy_to_user_timeval(arg1, &tv))
5185 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005186 }
5187 }
5188 break;
5189 case TARGET_NR_settimeofday:
5190 {
bellard31e31b82003-02-18 22:55:36 +00005191 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00005192 if (copy_from_user_timeval(&tv, arg1))
5193 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005194 ret = get_errno(settimeofday(&tv, NULL));
5195 }
5196 break;
bellard048f6b42005-11-26 18:47:20 +00005197#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00005198 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00005199 {
pbrook53a59602006-03-25 19:31:22 +00005200 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00005201 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00005202 long nsel;
5203
bellard579a97f2007-11-11 14:26:47 +00005204 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5205 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005206 nsel = tswapl(sel->n);
5207 inp = tswapl(sel->inp);
5208 outp = tswapl(sel->outp);
5209 exp = tswapl(sel->exp);
5210 tvp = tswapl(sel->tvp);
5211 unlock_user_struct(sel, arg1, 0);
5212 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00005213 }
5214 break;
bellard048f6b42005-11-26 18:47:20 +00005215#endif
bellard31e31b82003-02-18 22:55:36 +00005216 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00005217 {
5218 void *p2;
5219 p = lock_user_string(arg1);
5220 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005221 if (!p || !p2)
5222 ret = -TARGET_EFAULT;
5223 else
5224 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005225 unlock_user(p2, arg2, 0);
5226 unlock_user(p, arg1, 0);
5227 }
bellard31e31b82003-02-18 22:55:36 +00005228 break;
thsf0b62432007-09-24 09:25:40 +00005229#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5230 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00005231 {
bellard579a97f2007-11-11 14:26:47 +00005232 void *p2;
thsf0b62432007-09-24 09:25:40 +00005233 p = lock_user_string(arg1);
5234 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005235 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005236 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00005237 else
5238 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00005239 unlock_user(p2, arg3, 0);
5240 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00005241 }
5242 break;
5243#endif
bellardebc05482003-09-30 21:08:41 +00005244#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00005245 case TARGET_NR_oldlstat:
5246 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005247#endif
bellard31e31b82003-02-18 22:55:36 +00005248 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00005249 {
aurel32d088d662009-01-30 20:09:01 +00005250 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00005251 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00005252 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5253 if (!p || !p2)
5254 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00005255 else {
5256 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5257 char real[PATH_MAX];
5258 temp = realpath(exec_path,real);
5259 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5260 snprintf((char *)p2, arg3, "%s", real);
5261 }
5262 else
5263 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00005264 }
pbrook53a59602006-03-25 19:31:22 +00005265 unlock_user(p2, arg2, ret);
5266 unlock_user(p, arg1, 0);
5267 }
bellard31e31b82003-02-18 22:55:36 +00005268 break;
ths5e0ccb12007-09-24 09:26:10 +00005269#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5270 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00005271 {
bellard579a97f2007-11-11 14:26:47 +00005272 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00005273 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005274 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5275 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005276 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00005277 else
5278 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00005279 unlock_user(p2, arg3, ret);
5280 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00005281 }
5282 break;
5283#endif
thse5febef2007-04-01 18:31:35 +00005284#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00005285 case TARGET_NR_uselib:
5286 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005287#endif
5288#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00005289 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00005290 if (!(p = lock_user_string(arg1)))
5291 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005292 ret = get_errno(swapon(p, arg2));
5293 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005294 break;
thse5febef2007-04-01 18:31:35 +00005295#endif
bellard31e31b82003-02-18 22:55:36 +00005296 case TARGET_NR_reboot:
5297 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005298#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00005299 case TARGET_NR_readdir:
5300 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005301#endif
5302#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00005303 case TARGET_NR_mmap:
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02005304#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 +00005305 {
blueswir1992f48a2007-10-14 16:27:31 +00005306 abi_ulong *v;
5307 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00005308 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5309 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005310 v1 = tswapl(v[0]);
5311 v2 = tswapl(v[1]);
5312 v3 = tswapl(v[2]);
5313 v4 = tswapl(v[3]);
5314 v5 = tswapl(v[4]);
5315 v6 = tswapl(v[5]);
5316 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00005317 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00005318 target_to_host_bitmask(v4, mmap_flags_tbl),
5319 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00005320 }
bellard31e31b82003-02-18 22:55:36 +00005321#else
ths5fafdf22007-09-16 21:08:06 +00005322 ret = get_errno(target_mmap(arg1, arg2, arg3,
5323 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00005324 arg5,
5325 arg6));
bellard31e31b82003-02-18 22:55:36 +00005326#endif
bellard6fb883e2003-07-09 17:12:39 +00005327 break;
thse5febef2007-04-01 18:31:35 +00005328#endif
bellarda315a142005-01-30 22:59:18 +00005329#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00005330 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00005331#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00005332#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00005333#endif
ths5fafdf22007-09-16 21:08:06 +00005334 ret = get_errno(target_mmap(arg1, arg2, arg3,
5335 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00005336 arg5,
bellardc573ff62004-01-04 15:51:36 +00005337 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00005338 break;
bellarda315a142005-01-30 22:59:18 +00005339#endif
bellard31e31b82003-02-18 22:55:36 +00005340 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00005341 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005342 break;
bellard9de5e442003-03-23 16:49:39 +00005343 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00005344 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005345 break;
thse5febef2007-04-01 18:31:35 +00005346#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00005347 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00005348 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00005349 break;
thse5febef2007-04-01 18:31:35 +00005350#endif
pbrook53a59602006-03-25 19:31:22 +00005351 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00005352#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00005353 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00005354 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005355 break;
thse5febef2007-04-01 18:31:35 +00005356#endif
5357#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00005358 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00005359 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005360 break;
thse5febef2007-04-01 18:31:35 +00005361#endif
5362#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00005363 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00005364 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005365 break;
thse5febef2007-04-01 18:31:35 +00005366#endif
5367#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00005368 case TARGET_NR_mlockall:
5369 ret = get_errno(mlockall(arg1));
5370 break;
thse5febef2007-04-01 18:31:35 +00005371#endif
5372#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00005373 case TARGET_NR_munlockall:
5374 ret = get_errno(munlockall());
5375 break;
thse5febef2007-04-01 18:31:35 +00005376#endif
bellard31e31b82003-02-18 22:55:36 +00005377 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00005378 if (!(p = lock_user_string(arg1)))
5379 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005380 ret = get_errno(truncate(p, arg2));
5381 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005382 break;
5383 case TARGET_NR_ftruncate:
5384 ret = get_errno(ftruncate(arg1, arg2));
5385 break;
5386 case TARGET_NR_fchmod:
5387 ret = get_errno(fchmod(arg1, arg2));
5388 break;
ths814d7972007-09-24 09:26:51 +00005389#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5390 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00005391 if (!(p = lock_user_string(arg2)))
5392 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005393 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005394 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00005395 break;
5396#endif
bellard31e31b82003-02-18 22:55:36 +00005397 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00005398 /* libc does special remapping of the return value of
5399 * sys_getpriority() so it's just easiest to call
5400 * sys_getpriority() directly rather than through libc. */
Ulrich Hecht69137202009-09-17 21:08:37 +03005401 ret = get_errno(sys_getpriority(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005402 break;
5403 case TARGET_NR_setpriority:
5404 ret = get_errno(setpriority(arg1, arg2, arg3));
5405 break;
bellardebc05482003-09-30 21:08:41 +00005406#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00005407 case TARGET_NR_profil:
5408 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005409#endif
bellard31e31b82003-02-18 22:55:36 +00005410 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00005411 if (!(p = lock_user_string(arg1)))
5412 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005413 ret = get_errno(statfs(path(p), &stfs));
5414 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005415 convert_statfs:
5416 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005417 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005418
bellard579a97f2007-11-11 14:26:47 +00005419 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5420 goto efault;
5421 __put_user(stfs.f_type, &target_stfs->f_type);
5422 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5423 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5424 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5425 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5426 __put_user(stfs.f_files, &target_stfs->f_files);
5427 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5428 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5429 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5430 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00005431 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005432 }
5433 break;
5434 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00005435 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00005436 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00005437#ifdef TARGET_NR_statfs64
5438 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00005439 if (!(p = lock_user_string(arg1)))
5440 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005441 ret = get_errno(statfs(path(p), &stfs));
5442 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00005443 convert_statfs64:
5444 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005445 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005446
bellard579a97f2007-11-11 14:26:47 +00005447 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5448 goto efault;
5449 __put_user(stfs.f_type, &target_stfs->f_type);
5450 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5451 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5452 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5453 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5454 __put_user(stfs.f_files, &target_stfs->f_files);
5455 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5456 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5457 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5458 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5459 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00005460 }
5461 break;
5462 case TARGET_NR_fstatfs64:
5463 ret = get_errno(fstatfs(arg1, &stfs));
5464 goto convert_statfs64;
5465#endif
bellardebc05482003-09-30 21:08:41 +00005466#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00005467 case TARGET_NR_ioperm:
5468 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005469#endif
thse5febef2007-04-01 18:31:35 +00005470#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00005471 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00005472 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00005473 break;
thse5febef2007-04-01 18:31:35 +00005474#endif
bellard3532fa72006-06-24 15:06:03 +00005475#ifdef TARGET_NR_accept
5476 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00005477 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005478 break;
5479#endif
5480#ifdef TARGET_NR_bind
5481 case TARGET_NR_bind:
5482 ret = do_bind(arg1, arg2, arg3);
5483 break;
5484#endif
5485#ifdef TARGET_NR_connect
5486 case TARGET_NR_connect:
5487 ret = do_connect(arg1, arg2, arg3);
5488 break;
5489#endif
5490#ifdef TARGET_NR_getpeername
5491 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00005492 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005493 break;
5494#endif
5495#ifdef TARGET_NR_getsockname
5496 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00005497 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005498 break;
5499#endif
5500#ifdef TARGET_NR_getsockopt
5501 case TARGET_NR_getsockopt:
5502 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5503 break;
5504#endif
5505#ifdef TARGET_NR_listen
5506 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00005507 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005508 break;
5509#endif
5510#ifdef TARGET_NR_recv
5511 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00005512 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005513 break;
5514#endif
5515#ifdef TARGET_NR_recvfrom
5516 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00005517 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005518 break;
5519#endif
5520#ifdef TARGET_NR_recvmsg
5521 case TARGET_NR_recvmsg:
5522 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5523 break;
5524#endif
5525#ifdef TARGET_NR_send
5526 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00005527 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005528 break;
5529#endif
5530#ifdef TARGET_NR_sendmsg
5531 case TARGET_NR_sendmsg:
5532 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5533 break;
5534#endif
5535#ifdef TARGET_NR_sendto
5536 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00005537 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005538 break;
5539#endif
5540#ifdef TARGET_NR_shutdown
5541 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00005542 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005543 break;
5544#endif
5545#ifdef TARGET_NR_socket
5546 case TARGET_NR_socket:
5547 ret = do_socket(arg1, arg2, arg3);
5548 break;
5549#endif
5550#ifdef TARGET_NR_socketpair
5551 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00005552 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00005553 break;
5554#endif
5555#ifdef TARGET_NR_setsockopt
5556 case TARGET_NR_setsockopt:
5557 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5558 break;
5559#endif
ths7494b0f2007-02-11 18:26:53 +00005560
bellard31e31b82003-02-18 22:55:36 +00005561 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00005562 if (!(p = lock_user_string(arg2)))
5563 goto efault;
thse5574482007-02-11 20:03:13 +00005564 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5565 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00005566 break;
5567
bellard31e31b82003-02-18 22:55:36 +00005568 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00005569 {
bellard66fb9762003-03-23 01:06:05 +00005570 struct itimerval value, ovalue, *pvalue;
5571
pbrook53a59602006-03-25 19:31:22 +00005572 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005573 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00005574 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5575 || copy_from_user_timeval(&pvalue->it_value,
5576 arg2 + sizeof(struct target_timeval)))
5577 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005578 } else {
5579 pvalue = NULL;
5580 }
5581 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00005582 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00005583 if (copy_to_user_timeval(arg3,
5584 &ovalue.it_interval)
5585 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5586 &ovalue.it_value))
5587 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005588 }
5589 }
5590 break;
bellard31e31b82003-02-18 22:55:36 +00005591 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00005592 {
bellard66fb9762003-03-23 01:06:05 +00005593 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00005594
bellard66fb9762003-03-23 01:06:05 +00005595 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00005596 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00005597 if (copy_to_user_timeval(arg2,
5598 &value.it_interval)
5599 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5600 &value.it_value))
5601 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005602 }
5603 }
5604 break;
bellard31e31b82003-02-18 22:55:36 +00005605 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00005606 if (!(p = lock_user_string(arg1)))
5607 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005608 ret = get_errno(stat(path(p), &st));
5609 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005610 goto do_stat;
5611 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00005612 if (!(p = lock_user_string(arg1)))
5613 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005614 ret = get_errno(lstat(path(p), &st));
5615 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005616 goto do_stat;
5617 case TARGET_NR_fstat:
5618 {
5619 ret = get_errno(fstat(arg1, &st));
5620 do_stat:
5621 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005622 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00005623
bellard579a97f2007-11-11 14:26:47 +00005624 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5625 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02005626 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00005627 __put_user(st.st_dev, &target_st->st_dev);
5628 __put_user(st.st_ino, &target_st->st_ino);
5629 __put_user(st.st_mode, &target_st->st_mode);
5630 __put_user(st.st_uid, &target_st->st_uid);
5631 __put_user(st.st_gid, &target_st->st_gid);
5632 __put_user(st.st_nlink, &target_st->st_nlink);
5633 __put_user(st.st_rdev, &target_st->st_rdev);
5634 __put_user(st.st_size, &target_st->st_size);
5635 __put_user(st.st_blksize, &target_st->st_blksize);
5636 __put_user(st.st_blocks, &target_st->st_blocks);
5637 __put_user(st.st_atime, &target_st->target_st_atime);
5638 __put_user(st.st_mtime, &target_st->target_st_mtime);
5639 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00005640 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005641 }
5642 }
5643 break;
bellardebc05482003-09-30 21:08:41 +00005644#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00005645 case TARGET_NR_olduname:
5646 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005647#endif
5648#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00005649 case TARGET_NR_iopl:
5650 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005651#endif
bellard31e31b82003-02-18 22:55:36 +00005652 case TARGET_NR_vhangup:
5653 ret = get_errno(vhangup());
5654 break;
bellardebc05482003-09-30 21:08:41 +00005655#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00005656 case TARGET_NR_idle:
5657 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005658#endif
bellard42ad6ae2005-01-03 22:48:11 +00005659#ifdef TARGET_NR_syscall
5660 case TARGET_NR_syscall:
5661 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5662 break;
5663#endif
bellard31e31b82003-02-18 22:55:36 +00005664 case TARGET_NR_wait4:
5665 {
5666 int status;
blueswir1992f48a2007-10-14 16:27:31 +00005667 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00005668 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00005669 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00005670 if (target_rusage)
5671 rusage_ptr = &rusage;
5672 else
5673 rusage_ptr = NULL;
5674 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5675 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005676 if (status_ptr) {
pbrook1d9d8b52009-04-16 15:17:02 +00005677 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00005678 if (put_user_s32(status, status_ptr))
5679 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005680 }
bellard2f619692007-11-16 10:46:05 +00005681 if (target_rusage)
5682 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00005683 }
5684 }
5685 break;
thse5febef2007-04-01 18:31:35 +00005686#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00005687 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00005688 if (!(p = lock_user_string(arg1)))
5689 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005690 ret = get_errno(swapoff(p));
5691 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005692 break;
thse5febef2007-04-01 18:31:35 +00005693#endif
bellard31e31b82003-02-18 22:55:36 +00005694 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00005695 {
pbrook53a59602006-03-25 19:31:22 +00005696 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00005697 struct sysinfo value;
5698 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00005699 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00005700 {
bellard579a97f2007-11-11 14:26:47 +00005701 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5702 goto efault;
bellarda5448a72004-06-19 16:59:03 +00005703 __put_user(value.uptime, &target_value->uptime);
5704 __put_user(value.loads[0], &target_value->loads[0]);
5705 __put_user(value.loads[1], &target_value->loads[1]);
5706 __put_user(value.loads[2], &target_value->loads[2]);
5707 __put_user(value.totalram, &target_value->totalram);
5708 __put_user(value.freeram, &target_value->freeram);
5709 __put_user(value.sharedram, &target_value->sharedram);
5710 __put_user(value.bufferram, &target_value->bufferram);
5711 __put_user(value.totalswap, &target_value->totalswap);
5712 __put_user(value.freeswap, &target_value->freeswap);
5713 __put_user(value.procs, &target_value->procs);
5714 __put_user(value.totalhigh, &target_value->totalhigh);
5715 __put_user(value.freehigh, &target_value->freehigh);
5716 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00005717 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00005718 }
5719 }
5720 break;
thse5febef2007-04-01 18:31:35 +00005721#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00005722 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00005723 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5724 break;
thse5febef2007-04-01 18:31:35 +00005725#endif
aurel32e5289082009-04-18 16:16:12 +00005726#ifdef TARGET_NR_semget
5727 case TARGET_NR_semget:
5728 ret = get_errno(semget(arg1, arg2, arg3));
5729 break;
5730#endif
5731#ifdef TARGET_NR_semop
5732 case TARGET_NR_semop:
5733 ret = get_errno(do_semop(arg1, arg2, arg3));
5734 break;
5735#endif
5736#ifdef TARGET_NR_semctl
5737 case TARGET_NR_semctl:
5738 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5739 break;
5740#endif
aurel32eeb438c2008-10-13 21:08:55 +00005741#ifdef TARGET_NR_msgctl
5742 case TARGET_NR_msgctl:
5743 ret = do_msgctl(arg1, arg2, arg3);
5744 break;
5745#endif
5746#ifdef TARGET_NR_msgget
5747 case TARGET_NR_msgget:
5748 ret = get_errno(msgget(arg1, arg2));
5749 break;
5750#endif
5751#ifdef TARGET_NR_msgrcv
5752 case TARGET_NR_msgrcv:
5753 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5754 break;
5755#endif
5756#ifdef TARGET_NR_msgsnd
5757 case TARGET_NR_msgsnd:
5758 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5759 break;
5760#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03005761#ifdef TARGET_NR_shmget
5762 case TARGET_NR_shmget:
5763 ret = get_errno(shmget(arg1, arg2, arg3));
5764 break;
5765#endif
5766#ifdef TARGET_NR_shmctl
5767 case TARGET_NR_shmctl:
5768 ret = do_shmctl(arg1, arg2, arg3);
5769 break;
5770#endif
5771#ifdef TARGET_NR_shmat
5772 case TARGET_NR_shmat:
5773 ret = do_shmat(arg1, arg2, arg3);
5774 break;
5775#endif
5776#ifdef TARGET_NR_shmdt
5777 case TARGET_NR_shmdt:
5778 ret = do_shmdt(arg1);
5779 break;
5780#endif
bellard31e31b82003-02-18 22:55:36 +00005781 case TARGET_NR_fsync:
5782 ret = get_errno(fsync(arg1));
5783 break;
bellard31e31b82003-02-18 22:55:36 +00005784 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07005785#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00005786 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00005787#elif defined(TARGET_CRIS)
5788 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005789#else
pbrookd865bab2008-06-07 22:12:17 +00005790 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005791#endif
bellard1b6b0292003-03-22 17:31:38 +00005792 break;
bellardec86b0f2003-04-11 00:15:04 +00005793#ifdef __NR_exit_group
5794 /* new thread calls */
5795 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02005796#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00005797 _mcleanup();
5798#endif
bellarde9009672005-04-26 20:42:36 +00005799 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00005800 ret = get_errno(exit_group(arg1));
5801 break;
5802#endif
bellard31e31b82003-02-18 22:55:36 +00005803 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00005804 if (!(p = lock_user_string(arg1)))
5805 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005806 ret = get_errno(setdomainname(p, arg2));
5807 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005808 break;
5809 case TARGET_NR_uname:
5810 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00005811 {
5812 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00005813
bellard579a97f2007-11-11 14:26:47 +00005814 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5815 goto efault;
bellard29e619b2004-09-13 21:41:04 +00005816 ret = get_errno(sys_uname(buf));
5817 if (!is_error(ret)) {
5818 /* Overrite the native machine name with whatever is being
5819 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01005820 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00005821 /* Allow the user to override the reported release. */
5822 if (qemu_uname_release && *qemu_uname_release)
5823 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00005824 }
pbrook53a59602006-03-25 19:31:22 +00005825 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00005826 }
bellard31e31b82003-02-18 22:55:36 +00005827 break;
bellard6dbad632003-03-16 18:05:05 +00005828#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00005829 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00005830 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00005831 break;
j_mayer84409dd2007-04-06 08:56:50 +00005832#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00005833 case TARGET_NR_vm86old:
5834 goto unimplemented;
5835 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00005836 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00005837 break;
5838#endif
j_mayer84409dd2007-04-06 08:56:50 +00005839#endif
bellard31e31b82003-02-18 22:55:36 +00005840 case TARGET_NR_adjtimex:
5841 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005842#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00005843 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00005844#endif
bellard31e31b82003-02-18 22:55:36 +00005845 case TARGET_NR_init_module:
5846 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00005847#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00005848 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00005849#endif
bellard31e31b82003-02-18 22:55:36 +00005850 goto unimplemented;
5851 case TARGET_NR_quotactl:
5852 goto unimplemented;
5853 case TARGET_NR_getpgid:
5854 ret = get_errno(getpgid(arg1));
5855 break;
5856 case TARGET_NR_fchdir:
5857 ret = get_errno(fchdir(arg1));
5858 break;
j_mayer84409dd2007-04-06 08:56:50 +00005859#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00005860 case TARGET_NR_bdflush:
5861 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00005862#endif
thse5febef2007-04-01 18:31:35 +00005863#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00005864 case TARGET_NR_sysfs:
5865 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005866#endif
bellard31e31b82003-02-18 22:55:36 +00005867 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00005868 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00005869 break;
thse5febef2007-04-01 18:31:35 +00005870#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00005871 case TARGET_NR_afs_syscall:
5872 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005873#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005874#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005875 case TARGET_NR__llseek:
5876 {
bellard4f2ac232004-04-26 19:44:02 +00005877#if defined (__x86_64__)
5878 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00005879 if (put_user_s64(ret, arg4))
5880 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00005881#else
bellard31e31b82003-02-18 22:55:36 +00005882 int64_t res;
5883 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00005884 if (put_user_s64(res, arg4))
5885 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00005886#endif
bellard31e31b82003-02-18 22:55:36 +00005887 }
5888 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005889#endif
bellard31e31b82003-02-18 22:55:36 +00005890 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02005891#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00005892 {
pbrook53a59602006-03-25 19:31:22 +00005893 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00005894 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005895 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00005896
5897 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00005898 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00005899 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00005900 goto fail;
5901 }
ths3b46e622007-09-17 08:09:54 +00005902
bellard4add45b2003-06-05 01:52:59 +00005903 ret = get_errno(sys_getdents(arg1, dirp, count));
5904 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005905 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00005906 struct target_dirent *tde;
5907 int len = ret;
5908 int reclen, treclen;
5909 int count1, tnamelen;
5910
5911 count1 = 0;
5912 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00005913 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5914 goto efault;
bellard4add45b2003-06-05 01:52:59 +00005915 tde = target_dirp;
5916 while (len > 0) {
5917 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00005918 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00005919 tde->d_reclen = tswap16(treclen);
5920 tde->d_ino = tswapl(de->d_ino);
5921 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00005922 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00005923 if (tnamelen > 256)
5924 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00005925 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00005926 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00005927 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00005928 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00005929 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00005930 count1 += treclen;
5931 }
5932 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00005933 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00005934 }
5935 free(dirp);
5936 }
5937#else
bellard31e31b82003-02-18 22:55:36 +00005938 {
aurel326556a832008-10-13 21:08:17 +00005939 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005940 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00005941
bellard579a97f2007-11-11 14:26:47 +00005942 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5943 goto efault;
bellard72f03902003-02-18 23:33:18 +00005944 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00005945 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005946 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00005947 int len = ret;
5948 int reclen;
5949 de = dirp;
5950 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00005951 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00005952 if (reclen > len)
5953 break;
bellard8083a3e2003-03-24 23:12:16 +00005954 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00005955 tswapls(&de->d_ino);
5956 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00005957 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00005958 len -= reclen;
5959 }
5960 }
pbrook53a59602006-03-25 19:31:22 +00005961 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00005962 }
bellard4add45b2003-06-05 01:52:59 +00005963#endif
bellard31e31b82003-02-18 22:55:36 +00005964 break;
ths3ae43202007-09-16 21:39:48 +00005965#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00005966 case TARGET_NR_getdents64:
5967 {
aurel326556a832008-10-13 21:08:17 +00005968 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005969 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00005970 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5971 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00005972 ret = get_errno(sys_getdents64(arg1, dirp, count));
5973 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005974 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00005975 int len = ret;
5976 int reclen;
5977 de = dirp;
5978 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00005979 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00005980 if (reclen > len)
5981 break;
bellard8083a3e2003-03-24 23:12:16 +00005982 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00005983 tswap64s((uint64_t *)&de->d_ino);
5984 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00005985 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00005986 len -= reclen;
5987 }
5988 }
pbrook53a59602006-03-25 19:31:22 +00005989 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00005990 }
5991 break;
bellarda541f292004-04-12 20:39:29 +00005992#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00005993#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00005994 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00005995 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00005996 break;
thse5febef2007-04-01 18:31:35 +00005997#endif
5998#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00005999 case TARGET_NR_poll:
6000 {
pbrook53a59602006-03-25 19:31:22 +00006001 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00006002 unsigned int nfds = arg2;
6003 int timeout = arg3;
6004 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00006005 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00006006
bellard579a97f2007-11-11 14:26:47 +00006007 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6008 if (!target_pfd)
6009 goto efault;
bellard9de5e442003-03-23 16:49:39 +00006010 pfd = alloca(sizeof(struct pollfd) * nfds);
6011 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006012 pfd[i].fd = tswap32(target_pfd[i].fd);
6013 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00006014 }
6015 ret = get_errno(poll(pfd, nfds, timeout));
6016 if (!is_error(ret)) {
6017 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006018 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00006019 }
pbrook53a59602006-03-25 19:31:22 +00006020 ret += nfds * (sizeof(struct target_pollfd)
6021 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00006022 }
pbrook53a59602006-03-25 19:31:22 +00006023 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00006024 }
6025 break;
thse5febef2007-04-01 18:31:35 +00006026#endif
bellard31e31b82003-02-18 22:55:36 +00006027 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00006028 /* NOTE: the flock constant seems to be the same for every
6029 Linux platform */
6030 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006031 break;
6032 case TARGET_NR_readv:
6033 {
6034 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006035 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006036
6037 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006038 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6039 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006040 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006041 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00006042 }
6043 break;
6044 case TARGET_NR_writev:
6045 {
6046 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006047 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006048
6049 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006050 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6051 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006052 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006053 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00006054 }
6055 break;
6056 case TARGET_NR_getsid:
6057 ret = get_errno(getsid(arg1));
6058 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006059#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00006060 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00006061 ret = get_errno(fdatasync(arg1));
6062 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006063#endif
bellard31e31b82003-02-18 22:55:36 +00006064 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00006065 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00006066 return value. */
ths0da46a62007-10-20 20:23:07 +00006067 ret = -TARGET_ENOTDIR;
6068 break;
bellard31e31b82003-02-18 22:55:36 +00006069 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00006070 {
pbrook53a59602006-03-25 19:31:22 +00006071 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006072 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00006073
bellard579a97f2007-11-11 14:26:47 +00006074 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6075 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006076 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006077 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00006078 ret = get_errno(sched_setparam(arg1, &schp));
6079 }
6080 break;
bellard31e31b82003-02-18 22:55:36 +00006081 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00006082 {
pbrook53a59602006-03-25 19:31:22 +00006083 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006084 struct sched_param schp;
6085 ret = get_errno(sched_getparam(arg1, &schp));
6086 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006087 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6088 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006089 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006090 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00006091 }
6092 }
6093 break;
bellard31e31b82003-02-18 22:55:36 +00006094 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00006095 {
pbrook53a59602006-03-25 19:31:22 +00006096 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006097 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00006098 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6099 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006100 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006101 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00006102 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6103 }
6104 break;
bellard31e31b82003-02-18 22:55:36 +00006105 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00006106 ret = get_errno(sched_getscheduler(arg1));
6107 break;
bellard31e31b82003-02-18 22:55:36 +00006108 case TARGET_NR_sched_yield:
6109 ret = get_errno(sched_yield());
6110 break;
6111 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00006112 ret = get_errno(sched_get_priority_max(arg1));
6113 break;
bellard31e31b82003-02-18 22:55:36 +00006114 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00006115 ret = get_errno(sched_get_priority_min(arg1));
6116 break;
bellard31e31b82003-02-18 22:55:36 +00006117 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00006118 {
bellard5cd43932003-03-29 16:54:36 +00006119 struct timespec ts;
6120 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6121 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006122 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00006123 }
6124 }
6125 break;
bellard31e31b82003-02-18 22:55:36 +00006126 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00006127 {
bellard1b6b0292003-03-22 17:31:38 +00006128 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00006129 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00006130 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00006131 if (is_error(ret) && arg2) {
6132 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00006133 }
6134 }
6135 break;
thse5febef2007-04-01 18:31:35 +00006136#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00006137 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00006138 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006139#endif
6140#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00006141 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00006142 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006143#endif
bellard31e31b82003-02-18 22:55:36 +00006144 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00006145 switch (arg1)
6146 {
6147 case PR_GET_PDEATHSIG:
6148 {
6149 int deathsig;
6150 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00006151 if (!is_error(ret) && arg2
6152 && put_user_ual(deathsig, arg2))
6153 goto efault;
thse5574482007-02-11 20:03:13 +00006154 }
6155 break;
6156 default:
6157 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6158 break;
6159 }
ths39b9aae2007-02-11 18:36:44 +00006160 break;
bellardd2fd1af2007-11-14 18:08:56 +00006161#ifdef TARGET_NR_arch_prctl
6162 case TARGET_NR_arch_prctl:
6163#if defined(TARGET_I386) && !defined(TARGET_ABI32)
6164 ret = do_arch_prctl(cpu_env, arg1, arg2);
6165 break;
6166#else
6167 goto unimplemented;
6168#endif
6169#endif
bellard67867302003-11-23 17:05:30 +00006170#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00006171 case TARGET_NR_pread:
balroga4ae00b2008-09-20 03:14:14 +00006172#ifdef TARGET_ARM
6173 if (((CPUARMState *)cpu_env)->eabi)
6174 arg4 = arg5;
6175#endif
bellard579a97f2007-11-11 14:26:47 +00006176 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6177 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006178 ret = get_errno(pread(arg1, p, arg3, arg4));
6179 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00006180 break;
bellard31e31b82003-02-18 22:55:36 +00006181 case TARGET_NR_pwrite:
balroga4ae00b2008-09-20 03:14:14 +00006182#ifdef TARGET_ARM
6183 if (((CPUARMState *)cpu_env)->eabi)
6184 arg4 = arg5;
6185#endif
bellard579a97f2007-11-11 14:26:47 +00006186 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6187 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006188 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6189 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00006190 break;
bellard67867302003-11-23 17:05:30 +00006191#endif
aurel32f2c7ba12008-03-28 22:32:06 +00006192#ifdef TARGET_NR_pread64
6193 case TARGET_NR_pread64:
6194 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6195 goto efault;
6196 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6197 unlock_user(p, arg2, ret);
6198 break;
6199 case TARGET_NR_pwrite64:
6200 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6201 goto efault;
6202 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6203 unlock_user(p, arg2, 0);
6204 break;
6205#endif
bellard31e31b82003-02-18 22:55:36 +00006206 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00006207 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6208 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006209 ret = get_errno(sys_getcwd1(p, arg2));
6210 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00006211 break;
6212 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00006213 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006214 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00006215 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006216 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00006217#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02006218 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6219 defined(TARGET_M68K)
bellard579a97f2007-11-11 14:26:47 +00006220 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00006221 break;
6222#else
bellard5cd43932003-03-29 16:54:36 +00006223 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00006224#endif
bellard31e31b82003-02-18 22:55:36 +00006225 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00006226 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006227#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00006228 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00006229 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006230#endif
6231#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00006232 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00006233 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006234#endif
bellard048f6b42005-11-26 18:47:20 +00006235#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00006236 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00006237 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6238 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00006239 break;
bellard048f6b42005-11-26 18:47:20 +00006240#endif
bellardebc05482003-09-30 21:08:41 +00006241#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00006242 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00006243 {
6244 struct rlimit rlim;
6245 ret = get_errno(getrlimit(arg1, &rlim));
6246 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006247 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00006248 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6249 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006250 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6251 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006252 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00006253 }
6254 break;
6255 }
bellardebc05482003-09-30 21:08:41 +00006256#endif
bellarda315a142005-01-30 22:59:18 +00006257#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00006258 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00006259 if (!(p = lock_user_string(arg1)))
6260 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006261 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6262 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00006263 break;
bellarda315a142005-01-30 22:59:18 +00006264#endif
6265#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00006266 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00006267 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00006268 break;
bellarda315a142005-01-30 22:59:18 +00006269#endif
6270#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00006271 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00006272 if (!(p = lock_user_string(arg1)))
6273 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006274 ret = get_errno(stat(path(p), &st));
6275 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006276 if (!is_error(ret))
6277 ret = host_to_target_stat64(cpu_env, arg2, &st);
6278 break;
bellarda315a142005-01-30 22:59:18 +00006279#endif
6280#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00006281 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00006282 if (!(p = lock_user_string(arg1)))
6283 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006284 ret = get_errno(lstat(path(p), &st));
6285 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006286 if (!is_error(ret))
6287 ret = host_to_target_stat64(cpu_env, arg2, &st);
6288 break;
bellarda315a142005-01-30 22:59:18 +00006289#endif
6290#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00006291 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00006292 ret = get_errno(fstat(arg1, &st));
6293 if (!is_error(ret))
6294 ret = host_to_target_stat64(cpu_env, arg2, &st);
6295 break;
bellardec86b0f2003-04-11 00:15:04 +00006296#endif
aurel329d33b762009-04-08 23:07:05 +00006297#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6298 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6299#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006300 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00006301#endif
6302#ifdef TARGET_NR_newfstatat
6303 case TARGET_NR_newfstatat:
6304#endif
balrog6a24a772008-09-20 02:23:36 +00006305 if (!(p = lock_user_string(arg2)))
6306 goto efault;
aurel329d33b762009-04-08 23:07:05 +00006307#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006308 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00006309#else
6310 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6311#endif
balrog6a24a772008-09-20 02:23:36 +00006312 if (!is_error(ret))
6313 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00006314 break;
bellarda315a142005-01-30 22:59:18 +00006315#endif
bellard67867302003-11-23 17:05:30 +00006316#ifdef USE_UID16
6317 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00006318 if (!(p = lock_user_string(arg1)))
6319 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006320 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6321 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006322 break;
6323 case TARGET_NR_getuid:
6324 ret = get_errno(high2lowuid(getuid()));
6325 break;
6326 case TARGET_NR_getgid:
6327 ret = get_errno(high2lowgid(getgid()));
6328 break;
6329 case TARGET_NR_geteuid:
6330 ret = get_errno(high2lowuid(geteuid()));
6331 break;
6332 case TARGET_NR_getegid:
6333 ret = get_errno(high2lowgid(getegid()));
6334 break;
6335 case TARGET_NR_setreuid:
6336 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6337 break;
6338 case TARGET_NR_setregid:
6339 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6340 break;
6341 case TARGET_NR_getgroups:
6342 {
6343 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006344 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006345 gid_t *grouplist;
6346 int i;
6347
6348 grouplist = alloca(gidsetsize * sizeof(gid_t));
6349 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006350 if (gidsetsize == 0)
6351 break;
bellard67867302003-11-23 17:05:30 +00006352 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006353 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6354 if (!target_grouplist)
6355 goto efault;
balroga2155fc2008-09-20 02:12:08 +00006356 for(i = 0;i < ret; i++)
bellard67867302003-11-23 17:05:30 +00006357 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006358 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00006359 }
6360 }
6361 break;
6362 case TARGET_NR_setgroups:
6363 {
6364 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006365 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006366 gid_t *grouplist;
6367 int i;
6368
6369 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006370 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6371 if (!target_grouplist) {
6372 ret = -TARGET_EFAULT;
6373 goto fail;
6374 }
bellard67867302003-11-23 17:05:30 +00006375 for(i = 0;i < gidsetsize; i++)
6376 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006377 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00006378 ret = get_errno(setgroups(gidsetsize, grouplist));
6379 }
6380 break;
6381 case TARGET_NR_fchown:
6382 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6383 break;
thsccfa72b2007-09-24 09:23:34 +00006384#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6385 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00006386 if (!(p = lock_user_string(arg2)))
6387 goto efault;
6388 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6389 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00006390 break;
6391#endif
bellard67867302003-11-23 17:05:30 +00006392#ifdef TARGET_NR_setresuid
6393 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00006394 ret = get_errno(setresuid(low2highuid(arg1),
6395 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00006396 low2highuid(arg3)));
6397 break;
6398#endif
6399#ifdef TARGET_NR_getresuid
6400 case TARGET_NR_getresuid:
6401 {
pbrook53a59602006-03-25 19:31:22 +00006402 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00006403 ret = get_errno(getresuid(&ruid, &euid, &suid));
6404 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006405 if (put_user_u16(high2lowuid(ruid), arg1)
6406 || put_user_u16(high2lowuid(euid), arg2)
6407 || put_user_u16(high2lowuid(suid), arg3))
6408 goto efault;
bellard67867302003-11-23 17:05:30 +00006409 }
6410 }
6411 break;
6412#endif
6413#ifdef TARGET_NR_getresgid
6414 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00006415 ret = get_errno(setresgid(low2highgid(arg1),
6416 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00006417 low2highgid(arg3)));
6418 break;
6419#endif
6420#ifdef TARGET_NR_getresgid
6421 case TARGET_NR_getresgid:
6422 {
pbrook53a59602006-03-25 19:31:22 +00006423 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00006424 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6425 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006426 if (put_user_u16(high2lowgid(rgid), arg1)
6427 || put_user_u16(high2lowgid(egid), arg2)
6428 || put_user_u16(high2lowgid(sgid), arg3))
6429 goto efault;
bellard67867302003-11-23 17:05:30 +00006430 }
6431 }
6432 break;
6433#endif
6434 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00006435 if (!(p = lock_user_string(arg1)))
6436 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006437 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6438 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006439 break;
6440 case TARGET_NR_setuid:
6441 ret = get_errno(setuid(low2highuid(arg1)));
6442 break;
6443 case TARGET_NR_setgid:
6444 ret = get_errno(setgid(low2highgid(arg1)));
6445 break;
6446 case TARGET_NR_setfsuid:
6447 ret = get_errno(setfsuid(arg1));
6448 break;
6449 case TARGET_NR_setfsgid:
6450 ret = get_errno(setfsgid(arg1));
6451 break;
6452#endif /* USE_UID16 */
6453
bellarda315a142005-01-30 22:59:18 +00006454#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00006455 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00006456 if (!(p = lock_user_string(arg1)))
6457 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006458 ret = get_errno(lchown(p, arg2, arg3));
6459 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006460 break;
bellarda315a142005-01-30 22:59:18 +00006461#endif
6462#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00006463 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00006464 ret = get_errno(getuid());
6465 break;
bellarda315a142005-01-30 22:59:18 +00006466#endif
aurel3264b4d282008-11-14 17:20:15 +00006467
6468#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6469 /* Alpha specific */
6470 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006471 {
6472 uid_t euid;
6473 euid=geteuid();
6474 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6475 }
aurel3264b4d282008-11-14 17:20:15 +00006476 ret = get_errno(getuid());
6477 break;
6478#endif
6479#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6480 /* Alpha specific */
6481 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006482 {
6483 uid_t egid;
6484 egid=getegid();
6485 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6486 }
aurel3264b4d282008-11-14 17:20:15 +00006487 ret = get_errno(getgid());
6488 break;
6489#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08006490#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
6491 /* Alpha specific */
6492 case TARGET_NR_osf_getsysinfo:
6493 ret = -TARGET_EOPNOTSUPP;
6494 switch (arg1) {
6495 case TARGET_GSI_IEEE_FP_CONTROL:
6496 {
6497 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
6498
6499 /* Copied from linux ieee_fpcr_to_swcr. */
6500 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
6501 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
6502 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
6503 | SWCR_TRAP_ENABLE_DZE
6504 | SWCR_TRAP_ENABLE_OVF);
6505 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
6506 | SWCR_TRAP_ENABLE_INE);
6507 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
6508 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
6509
6510 if (put_user_u64 (swcr, arg2))
6511 goto efault;
6512 ret = 0;
6513 }
6514 break;
6515
6516 /* case GSI_IEEE_STATE_AT_SIGNAL:
6517 -- Not implemented in linux kernel.
6518 case GSI_UACPROC:
6519 -- Retrieves current unaligned access state; not much used.
6520 case GSI_PROC_TYPE:
6521 -- Retrieves implver information; surely not used.
6522 case GSI_GET_HWRPB:
6523 -- Grabs a copy of the HWRPB; surely not used.
6524 */
6525 }
6526 break;
6527#endif
6528#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
6529 /* Alpha specific */
6530 case TARGET_NR_osf_setsysinfo:
6531 ret = -TARGET_EOPNOTSUPP;
6532 switch (arg1) {
6533 case TARGET_SSI_IEEE_FP_CONTROL:
6534 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
6535 {
6536 uint64_t swcr, fpcr, orig_fpcr;
6537
6538 if (get_user_u64 (swcr, arg2))
6539 goto efault;
6540 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
6541 fpcr = orig_fpcr & FPCR_DYN_MASK;
6542
6543 /* Copied from linux ieee_swcr_to_fpcr. */
6544 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
6545 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
6546 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
6547 | SWCR_TRAP_ENABLE_DZE
6548 | SWCR_TRAP_ENABLE_OVF)) << 48;
6549 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
6550 | SWCR_TRAP_ENABLE_INE)) << 57;
6551 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
6552 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
6553
6554 cpu_alpha_store_fpcr (cpu_env, fpcr);
6555 ret = 0;
6556
6557 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
6558 /* Old exceptions are not signaled. */
6559 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
6560
6561 /* If any exceptions set by this call, and are unmasked,
6562 send a signal. */
6563 /* ??? FIXME */
6564 }
6565 }
6566 break;
6567
6568 /* case SSI_NVPAIRS:
6569 -- Used with SSIN_UACPROC to enable unaligned accesses.
6570 case SSI_IEEE_STATE_AT_SIGNAL:
6571 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
6572 -- Not implemented in linux kernel
6573 */
6574 }
6575 break;
6576#endif
6577#ifdef TARGET_NR_osf_sigprocmask
6578 /* Alpha specific. */
6579 case TARGET_NR_osf_sigprocmask:
6580 {
6581 abi_ulong mask;
6582 int how = arg1;
6583 sigset_t set, oldset;
6584
6585 switch(arg1) {
6586 case TARGET_SIG_BLOCK:
6587 how = SIG_BLOCK;
6588 break;
6589 case TARGET_SIG_UNBLOCK:
6590 how = SIG_UNBLOCK;
6591 break;
6592 case TARGET_SIG_SETMASK:
6593 how = SIG_SETMASK;
6594 break;
6595 default:
6596 ret = -TARGET_EINVAL;
6597 goto fail;
6598 }
6599 mask = arg2;
6600 target_to_host_old_sigset(&set, &mask);
6601 sigprocmask(arg1, &set, &oldset);
6602 host_to_target_old_sigset(&mask, &oldset);
6603 ret = mask;
6604 }
6605 break;
6606#endif
aurel3264b4d282008-11-14 17:20:15 +00006607
bellarda315a142005-01-30 22:59:18 +00006608#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00006609 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00006610 ret = get_errno(getgid());
6611 break;
bellarda315a142005-01-30 22:59:18 +00006612#endif
6613#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00006614 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00006615 ret = get_errno(geteuid());
6616 break;
bellarda315a142005-01-30 22:59:18 +00006617#endif
6618#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00006619 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00006620 ret = get_errno(getegid());
6621 break;
bellarda315a142005-01-30 22:59:18 +00006622#endif
6623#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00006624 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00006625 ret = get_errno(setreuid(arg1, arg2));
6626 break;
bellarda315a142005-01-30 22:59:18 +00006627#endif
6628#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00006629 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00006630 ret = get_errno(setregid(arg1, arg2));
6631 break;
bellarda315a142005-01-30 22:59:18 +00006632#endif
6633#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00006634 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00006635 {
6636 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006637 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006638 gid_t *grouplist;
6639 int i;
6640
6641 grouplist = alloca(gidsetsize * sizeof(gid_t));
6642 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006643 if (gidsetsize == 0)
6644 break;
bellard99c475a2005-01-31 20:45:13 +00006645 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006646 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6647 if (!target_grouplist) {
6648 ret = -TARGET_EFAULT;
6649 goto fail;
6650 }
balroga2155fc2008-09-20 02:12:08 +00006651 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00006652 target_grouplist[i] = tswap32(grouplist[i]);
6653 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00006654 }
6655 }
6656 break;
bellarda315a142005-01-30 22:59:18 +00006657#endif
6658#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00006659 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00006660 {
6661 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006662 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006663 gid_t *grouplist;
6664 int i;
ths3b46e622007-09-17 08:09:54 +00006665
bellard99c475a2005-01-31 20:45:13 +00006666 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006667 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6668 if (!target_grouplist) {
6669 ret = -TARGET_EFAULT;
6670 goto fail;
6671 }
bellard99c475a2005-01-31 20:45:13 +00006672 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00006673 grouplist[i] = tswap32(target_grouplist[i]);
6674 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00006675 ret = get_errno(setgroups(gidsetsize, grouplist));
6676 }
6677 break;
bellarda315a142005-01-30 22:59:18 +00006678#endif
6679#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00006680 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00006681 ret = get_errno(fchown(arg1, arg2, arg3));
6682 break;
bellarda315a142005-01-30 22:59:18 +00006683#endif
6684#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00006685 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006686 ret = get_errno(setresuid(arg1, arg2, arg3));
6687 break;
bellarda315a142005-01-30 22:59:18 +00006688#endif
6689#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00006690 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006691 {
pbrook53a59602006-03-25 19:31:22 +00006692 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00006693 ret = get_errno(getresuid(&ruid, &euid, &suid));
6694 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006695 if (put_user_u32(ruid, arg1)
6696 || put_user_u32(euid, arg2)
6697 || put_user_u32(suid, arg3))
6698 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006699 }
6700 }
6701 break;
bellarda315a142005-01-30 22:59:18 +00006702#endif
6703#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00006704 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006705 ret = get_errno(setresgid(arg1, arg2, arg3));
6706 break;
bellarda315a142005-01-30 22:59:18 +00006707#endif
6708#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00006709 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006710 {
pbrook53a59602006-03-25 19:31:22 +00006711 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00006712 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6713 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006714 if (put_user_u32(rgid, arg1)
6715 || put_user_u32(egid, arg2)
6716 || put_user_u32(sgid, arg3))
6717 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006718 }
6719 }
6720 break;
bellarda315a142005-01-30 22:59:18 +00006721#endif
6722#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00006723 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00006724 if (!(p = lock_user_string(arg1)))
6725 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006726 ret = get_errno(chown(p, arg2, arg3));
6727 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006728 break;
bellarda315a142005-01-30 22:59:18 +00006729#endif
6730#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00006731 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00006732 ret = get_errno(setuid(arg1));
6733 break;
bellarda315a142005-01-30 22:59:18 +00006734#endif
6735#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00006736 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00006737 ret = get_errno(setgid(arg1));
6738 break;
bellarda315a142005-01-30 22:59:18 +00006739#endif
6740#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00006741 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00006742 ret = get_errno(setfsuid(arg1));
6743 break;
bellarda315a142005-01-30 22:59:18 +00006744#endif
6745#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00006746 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00006747 ret = get_errno(setfsgid(arg1));
6748 break;
bellarda315a142005-01-30 22:59:18 +00006749#endif
bellard67867302003-11-23 17:05:30 +00006750
bellard31e31b82003-02-18 22:55:36 +00006751 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00006752 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00006753#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00006754 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00006755 {
6756 void *a;
6757 ret = -TARGET_EFAULT;
6758 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6759 goto efault;
6760 if (!(p = lock_user_string(arg3)))
6761 goto mincore_fail;
6762 ret = get_errno(mincore(a, arg2, p));
6763 unlock_user(p, arg3, ret);
6764 mincore_fail:
6765 unlock_user(a, arg1, 0);
6766 }
6767 break;
bellardffa65c32004-01-04 23:57:22 +00006768#endif
aurel32408321b2008-10-01 21:46:32 +00006769#ifdef TARGET_NR_arm_fadvise64_64
6770 case TARGET_NR_arm_fadvise64_64:
6771 {
6772 /*
6773 * arm_fadvise64_64 looks like fadvise64_64 but
6774 * with different argument order
6775 */
6776 abi_long temp;
6777 temp = arg3;
6778 arg3 = arg4;
6779 arg4 = temp;
6780 }
6781#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02006782#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00006783#ifdef TARGET_NR_fadvise64_64
6784 case TARGET_NR_fadvise64_64:
6785#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02006786#ifdef TARGET_NR_fadvise64
6787 case TARGET_NR_fadvise64:
6788#endif
6789#ifdef TARGET_S390X
6790 switch (arg4) {
6791 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
6792 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
6793 case 6: arg4 = POSIX_FADV_DONTNEED; break;
6794 case 7: arg4 = POSIX_FADV_NOREUSE; break;
6795 default: break;
6796 }
6797#endif
6798 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00006799 break;
6800#endif
bellardffa65c32004-01-04 23:57:22 +00006801#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00006802 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00006803 /* A straight passthrough may not be safe because qemu sometimes
6804 turns private flie-backed mappings into anonymous mappings.
6805 This will break MADV_DONTNEED.
6806 This is a hint, so ignoring and returning success is ok. */
6807 ret = get_errno(0);
6808 break;
bellardffa65c32004-01-04 23:57:22 +00006809#endif
blueswir1992f48a2007-10-14 16:27:31 +00006810#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00006811 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00006812 {
thsb1e341e2007-03-20 21:50:52 +00006813 int cmd;
bellard77e46722003-04-29 20:39:06 +00006814 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00006815 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00006816#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00006817 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00006818#endif
bellard77e46722003-04-29 20:39:06 +00006819
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006820 cmd = target_to_host_fcntl_cmd(arg2);
6821 if (cmd == -TARGET_EINVAL)
6822 return cmd;
thsb1e341e2007-03-20 21:50:52 +00006823
bellard60cd49d2003-03-16 22:53:56 +00006824 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00006825 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00006826#ifdef TARGET_ARM
6827 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006828 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6829 goto efault;
ths58134272007-03-31 18:59:32 +00006830 fl.l_type = tswap16(target_efl->l_type);
6831 fl.l_whence = tswap16(target_efl->l_whence);
6832 fl.l_start = tswap64(target_efl->l_start);
6833 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006834 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00006835 unlock_user_struct(target_efl, arg3, 0);
6836 } else
6837#endif
6838 {
bellard9ee1fa22007-11-11 15:11:19 +00006839 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6840 goto efault;
ths58134272007-03-31 18:59:32 +00006841 fl.l_type = tswap16(target_fl->l_type);
6842 fl.l_whence = tswap16(target_fl->l_whence);
6843 fl.l_start = tswap64(target_fl->l_start);
6844 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006845 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00006846 unlock_user_struct(target_fl, arg3, 0);
6847 }
thsb1e341e2007-03-20 21:50:52 +00006848 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006849 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00006850#ifdef TARGET_ARM
6851 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006852 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6853 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006854 target_efl->l_type = tswap16(fl.l_type);
6855 target_efl->l_whence = tswap16(fl.l_whence);
6856 target_efl->l_start = tswap64(fl.l_start);
6857 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006858 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006859 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006860 } else
6861#endif
6862 {
bellard9ee1fa22007-11-11 15:11:19 +00006863 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6864 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006865 target_fl->l_type = tswap16(fl.l_type);
6866 target_fl->l_whence = tswap16(fl.l_whence);
6867 target_fl->l_start = tswap64(fl.l_start);
6868 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006869 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006870 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006871 }
bellard77e46722003-04-29 20:39:06 +00006872 }
6873 break;
6874
thsb1e341e2007-03-20 21:50:52 +00006875 case TARGET_F_SETLK64:
6876 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00006877#ifdef TARGET_ARM
6878 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006879 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6880 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006881 fl.l_type = tswap16(target_efl->l_type);
6882 fl.l_whence = tswap16(target_efl->l_whence);
6883 fl.l_start = tswap64(target_efl->l_start);
6884 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006885 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00006886 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00006887 } else
6888#endif
6889 {
bellard9ee1fa22007-11-11 15:11:19 +00006890 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6891 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006892 fl.l_type = tswap16(target_fl->l_type);
6893 fl.l_whence = tswap16(target_fl->l_whence);
6894 fl.l_start = tswap64(target_fl->l_start);
6895 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006896 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00006897 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00006898 }
thsb1e341e2007-03-20 21:50:52 +00006899 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006900 break;
bellard60cd49d2003-03-16 22:53:56 +00006901 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006902 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00006903 break;
6904 }
bellard77e46722003-04-29 20:39:06 +00006905 break;
6906 }
bellard60cd49d2003-03-16 22:53:56 +00006907#endif
ths7d600c82006-12-08 01:32:58 +00006908#ifdef TARGET_NR_cacheflush
6909 case TARGET_NR_cacheflush:
6910 /* self-modifying code is handled automatically, so nothing needed */
6911 ret = 0;
6912 break;
6913#endif
bellardebc05482003-09-30 21:08:41 +00006914#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00006915 case TARGET_NR_security:
6916 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006917#endif
bellardc573ff62004-01-04 15:51:36 +00006918#ifdef TARGET_NR_getpagesize
6919 case TARGET_NR_getpagesize:
6920 ret = TARGET_PAGE_SIZE;
6921 break;
6922#endif
bellard31e31b82003-02-18 22:55:36 +00006923 case TARGET_NR_gettid:
6924 ret = get_errno(gettid());
6925 break;
thse5febef2007-04-01 18:31:35 +00006926#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00006927 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00006928#if TARGET_ABI_BITS == 32
6929#ifdef TARGET_ARM
6930 if (((CPUARMState *)cpu_env)->eabi)
6931 {
6932 arg2 = arg3;
6933 arg3 = arg4;
6934 arg4 = arg5;
6935 }
6936#endif
6937 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
6938#else
6939 ret = get_errno(readahead(arg1, arg2, arg3));
6940#endif
6941 break;
thse5febef2007-04-01 18:31:35 +00006942#endif
bellardebc05482003-09-30 21:08:41 +00006943#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00006944 case TARGET_NR_setxattr:
6945 case TARGET_NR_lsetxattr:
6946 case TARGET_NR_fsetxattr:
6947 case TARGET_NR_getxattr:
6948 case TARGET_NR_lgetxattr:
6949 case TARGET_NR_fgetxattr:
6950 case TARGET_NR_listxattr:
6951 case TARGET_NR_llistxattr:
6952 case TARGET_NR_flistxattr:
6953 case TARGET_NR_removexattr:
6954 case TARGET_NR_lremovexattr:
6955 case TARGET_NR_fremovexattr:
Arnaud Patard6f932f92009-04-21 21:04:18 +03006956 ret = -TARGET_EOPNOTSUPP;
6957 break;
bellardebc05482003-09-30 21:08:41 +00006958#endif
6959#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00006960 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00006961#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00006962 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
6963 ret = 0;
6964 break;
edgar_iglef967792009-01-07 14:19:38 +00006965#elif defined(TARGET_CRIS)
6966 if (arg1 & 0xff)
6967 ret = -TARGET_EINVAL;
6968 else {
6969 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
6970 ret = 0;
6971 }
6972 break;
bellard8d18e892007-11-14 15:18:40 +00006973#elif defined(TARGET_I386) && defined(TARGET_ABI32)
6974 ret = do_set_thread_area(cpu_env, arg1);
6975 break;
ths6f5b89a2007-03-02 20:48:00 +00006976#else
6977 goto unimplemented_nowarn;
6978#endif
6979#endif
6980#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00006981 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00006982#if defined(TARGET_I386) && defined(TARGET_ABI32)
6983 ret = do_get_thread_area(cpu_env, arg1);
6984#else
bellard5cd43932003-03-29 16:54:36 +00006985 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00006986#endif
bellard8d18e892007-11-14 15:18:40 +00006987#endif
bellard48dc41e2006-06-21 18:15:50 +00006988#ifdef TARGET_NR_getdomainname
6989 case TARGET_NR_getdomainname:
6990 goto unimplemented_nowarn;
6991#endif
ths6f5b89a2007-03-02 20:48:00 +00006992
thsb5906f92007-03-19 13:32:45 +00006993#ifdef TARGET_NR_clock_gettime
6994 case TARGET_NR_clock_gettime:
6995 {
6996 struct timespec ts;
6997 ret = get_errno(clock_gettime(arg1, &ts));
6998 if (!is_error(ret)) {
6999 host_to_target_timespec(arg2, &ts);
7000 }
7001 break;
7002 }
7003#endif
7004#ifdef TARGET_NR_clock_getres
7005 case TARGET_NR_clock_getres:
7006 {
7007 struct timespec ts;
7008 ret = get_errno(clock_getres(arg1, &ts));
7009 if (!is_error(ret)) {
7010 host_to_target_timespec(arg2, &ts);
7011 }
7012 break;
7013 }
7014#endif
pbrook63d76512008-05-29 13:43:29 +00007015#ifdef TARGET_NR_clock_nanosleep
7016 case TARGET_NR_clock_nanosleep:
7017 {
7018 struct timespec ts;
7019 target_to_host_timespec(&ts, arg3);
7020 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7021 if (arg4)
7022 host_to_target_timespec(arg4, &ts);
7023 break;
7024 }
7025#endif
thsb5906f92007-03-19 13:32:45 +00007026
ths6f5b89a2007-03-02 20:48:00 +00007027#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7028 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00007029 ret = get_errno(set_tid_address((int *)g2h(arg1)));
7030 break;
ths6f5b89a2007-03-02 20:48:00 +00007031#endif
7032
ths3ae43202007-09-16 21:39:48 +00007033#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00007034 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00007035 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00007036 break;
7037#endif
7038
ths3ae43202007-09-16 21:39:48 +00007039#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00007040 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00007041 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7042 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00007043 break;
7044#endif
7045
ths4f2b1fe2007-06-21 21:57:12 +00007046#ifdef TARGET_NR_set_robust_list
7047 case TARGET_NR_set_robust_list:
7048 goto unimplemented_nowarn;
7049#endif
7050
ths9007f0e2007-09-25 17:50:37 +00007051#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7052 case TARGET_NR_utimensat:
7053 {
Riku Voipioebc996f2009-04-21 15:01:51 +03007054 struct timespec *tsp, ts[2];
7055 if (!arg3) {
7056 tsp = NULL;
7057 } else {
7058 target_to_host_timespec(ts, arg3);
7059 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7060 tsp = ts;
7061 }
ths9007f0e2007-09-25 17:50:37 +00007062 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03007063 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00007064 else {
bellard579a97f2007-11-11 14:26:47 +00007065 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00007066 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00007067 goto fail;
7068 }
Riku Voipioebc996f2009-04-21 15:01:51 +03007069 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00007070 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00007071 }
7072 }
7073 break;
7074#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02007075#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00007076 case TARGET_NR_futex:
7077 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7078 break;
7079#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007080#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00007081 case TARGET_NR_inotify_init:
7082 ret = get_errno(sys_inotify_init());
7083 break;
7084#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007085#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00007086#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7087 case TARGET_NR_inotify_init1:
7088 ret = get_errno(sys_inotify_init1(arg1));
7089 break;
7090#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007091#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007092#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00007093 case TARGET_NR_inotify_add_watch:
7094 p = lock_user_string(arg2);
7095 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7096 unlock_user(p, arg2, 0);
7097 break;
7098#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007099#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00007100 case TARGET_NR_inotify_rm_watch:
7101 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7102 break;
7103#endif
ths9007f0e2007-09-25 17:50:37 +00007104
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07007105#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00007106 case TARGET_NR_mq_open:
7107 {
7108 struct mq_attr posix_mq_attr;
7109
7110 p = lock_user_string(arg1 - 1);
7111 if (arg4 != 0)
7112 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7113 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7114 unlock_user (p, arg1, 0);
7115 }
7116 break;
7117
7118 case TARGET_NR_mq_unlink:
7119 p = lock_user_string(arg1 - 1);
7120 ret = get_errno(mq_unlink(p));
7121 unlock_user (p, arg1, 0);
7122 break;
7123
7124 case TARGET_NR_mq_timedsend:
7125 {
7126 struct timespec ts;
7127
7128 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7129 if (arg5 != 0) {
7130 target_to_host_timespec(&ts, arg5);
7131 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7132 host_to_target_timespec(arg5, &ts);
7133 }
7134 else
7135 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7136 unlock_user (p, arg2, arg3);
7137 }
7138 break;
7139
7140 case TARGET_NR_mq_timedreceive:
7141 {
7142 struct timespec ts;
7143 unsigned int prio;
7144
7145 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7146 if (arg5 != 0) {
7147 target_to_host_timespec(&ts, arg5);
7148 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7149 host_to_target_timespec(arg5, &ts);
7150 }
7151 else
7152 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7153 unlock_user (p, arg2, arg3);
7154 if (arg4 != 0)
7155 put_user_u32(prio, arg4);
7156 }
7157 break;
7158
7159 /* Not implemented for now... */
7160/* case TARGET_NR_mq_notify: */
7161/* break; */
7162
7163 case TARGET_NR_mq_getsetattr:
7164 {
7165 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7166 ret = 0;
7167 if (arg3 != 0) {
7168 ret = mq_getattr(arg1, &posix_mq_attr_out);
7169 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7170 }
7171 if (arg2 != 0) {
7172 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7173 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7174 }
7175
7176 }
7177 break;
7178#endif
7179
vibisreenivasan3ce34df2009-05-16 18:32:41 +05307180#ifdef CONFIG_SPLICE
7181#ifdef TARGET_NR_tee
7182 case TARGET_NR_tee:
7183 {
7184 ret = get_errno(tee(arg1,arg2,arg3,arg4));
7185 }
7186 break;
7187#endif
7188#ifdef TARGET_NR_splice
7189 case TARGET_NR_splice:
7190 {
7191 loff_t loff_in, loff_out;
7192 loff_t *ploff_in = NULL, *ploff_out = NULL;
7193 if(arg2) {
7194 get_user_u64(loff_in, arg2);
7195 ploff_in = &loff_in;
7196 }
7197 if(arg4) {
7198 get_user_u64(loff_out, arg2);
7199 ploff_out = &loff_out;
7200 }
7201 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7202 }
7203 break;
7204#endif
7205#ifdef TARGET_NR_vmsplice
7206 case TARGET_NR_vmsplice:
7207 {
7208 int count = arg3;
7209 struct iovec *vec;
7210
7211 vec = alloca(count * sizeof(struct iovec));
7212 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7213 goto efault;
7214 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7215 unlock_iovec(vec, arg2, count, 0);
7216 }
7217 break;
7218#endif
7219#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03007220#ifdef CONFIG_EVENTFD
7221#if defined(TARGET_NR_eventfd)
7222 case TARGET_NR_eventfd:
7223 ret = get_errno(eventfd(arg1, 0));
7224 break;
7225#endif
7226#if defined(TARGET_NR_eventfd2)
7227 case TARGET_NR_eventfd2:
7228 ret = get_errno(eventfd(arg1, arg2));
7229 break;
7230#endif
7231#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03007232#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7233 case TARGET_NR_fallocate:
7234 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7235 break;
7236#endif
bellard31e31b82003-02-18 22:55:36 +00007237 default:
7238 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00007239 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00007240#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 +00007241 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00007242#endif
ths0da46a62007-10-20 20:23:07 +00007243 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00007244 break;
7245 }
bellard579a97f2007-11-11 14:26:47 +00007246fail:
bellardc573ff62004-01-04 15:51:36 +00007247#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00007248 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00007249#endif
thsb92c47c2007-11-01 00:07:38 +00007250 if(do_strace)
7251 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00007252 return ret;
bellard579a97f2007-11-11 14:26:47 +00007253efault:
7254 ret = -TARGET_EFAULT;
7255 goto fail;
bellard31e31b82003-02-18 22:55:36 +00007256}