blob: eb77ade9e7c400d78f2bd9aba8414060a45ad973 [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
ths788f5ec2007-12-09 02:37:05 +0000834static inline abi_long copy_from_user_timeval(struct timeval *tv,
835 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000836{
pbrook53a59602006-03-25 19:31:22 +0000837 struct target_timeval *target_tv;
838
ths788f5ec2007-12-09 02:37:05 +0000839 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000840 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000841
842 __get_user(tv->tv_sec, &target_tv->tv_sec);
843 __get_user(tv->tv_usec, &target_tv->tv_usec);
844
845 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000846
847 return 0;
bellard31e31b82003-02-18 22:55:36 +0000848}
849
ths788f5ec2007-12-09 02:37:05 +0000850static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
851 const struct timeval *tv)
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_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000856 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000857
858 __put_user(tv->tv_sec, &target_tv->tv_sec);
859 __put_user(tv->tv_usec, &target_tv->tv_usec);
860
861 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000862
863 return 0;
bellard31e31b82003-02-18 22:55:36 +0000864}
865
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700866#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
867#include <mqueue.h>
868
aurel3224e10032009-04-15 16:11:43 +0000869static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
870 abi_ulong target_mq_attr_addr)
871{
872 struct target_mq_attr *target_mq_attr;
873
874 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
875 target_mq_attr_addr, 1))
876 return -TARGET_EFAULT;
877
878 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
879 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
880 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
881 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
882
883 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
884
885 return 0;
886}
887
888static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
889 const struct mq_attr *attr)
890{
891 struct target_mq_attr *target_mq_attr;
892
893 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
894 target_mq_attr_addr, 0))
895 return -TARGET_EFAULT;
896
897 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
898 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
899 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
900 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
901
902 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
903
904 return 0;
905}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700906#endif
bellard31e31b82003-02-18 22:55:36 +0000907
ths0da46a62007-10-20 20:23:07 +0000908/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000909static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000910 abi_ulong rfd_addr, abi_ulong wfd_addr,
911 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000912{
913 fd_set rfds, wfds, efds;
914 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
915 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000916 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000917
ths26edcf42007-12-09 02:25:24 +0000918 if (rfd_addr) {
919 if (copy_from_user_fdset(&rfds, rfd_addr, n))
920 return -TARGET_EFAULT;
921 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000922 } else {
pbrook53a59602006-03-25 19:31:22 +0000923 rfds_ptr = NULL;
924 }
ths26edcf42007-12-09 02:25:24 +0000925 if (wfd_addr) {
926 if (copy_from_user_fdset(&wfds, wfd_addr, n))
927 return -TARGET_EFAULT;
928 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000929 } else {
pbrook53a59602006-03-25 19:31:22 +0000930 wfds_ptr = NULL;
931 }
ths26edcf42007-12-09 02:25:24 +0000932 if (efd_addr) {
933 if (copy_from_user_fdset(&efds, efd_addr, n))
934 return -TARGET_EFAULT;
935 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000936 } else {
pbrook53a59602006-03-25 19:31:22 +0000937 efds_ptr = NULL;
938 }
ths3b46e622007-09-17 08:09:54 +0000939
ths26edcf42007-12-09 02:25:24 +0000940 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000941 if (copy_from_user_timeval(&tv, target_tv_addr))
942 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000943 tv_ptr = &tv;
944 } else {
945 tv_ptr = NULL;
946 }
ths26edcf42007-12-09 02:25:24 +0000947
bellard31e31b82003-02-18 22:55:36 +0000948 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000949
ths26edcf42007-12-09 02:25:24 +0000950 if (!is_error(ret)) {
951 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
952 return -TARGET_EFAULT;
953 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
954 return -TARGET_EFAULT;
955 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
956 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000957
ths788f5ec2007-12-09 02:37:05 +0000958 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
959 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000960 }
bellard579a97f2007-11-11 14:26:47 +0000961
bellard31e31b82003-02-18 22:55:36 +0000962 return ret;
963}
964
Riku Voipio099d6b02009-05-05 12:10:04 +0300965static abi_long do_pipe2(int host_pipe[], int flags)
966{
967#ifdef CONFIG_PIPE2
968 return pipe2(host_pipe, flags);
969#else
970 return -ENOSYS;
971#endif
972}
973
Ulrich Hecht784ccfd2009-07-03 17:09:26 +0200974static abi_long do_pipe(void *cpu_env, abi_ulong pipedes, int flags)
Riku Voipio099d6b02009-05-05 12:10:04 +0300975{
976 int host_pipe[2];
977 abi_long ret;
978 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
979
980 if (is_error(ret))
981 return get_errno(ret);
982#if defined(TARGET_MIPS)
983 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
984 ret = host_pipe[0];
Riku Voipio099d6b02009-05-05 12:10:04 +0300985#else
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +0900986#if defined(TARGET_SH4)
987 if (!flags) {
988 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
989 ret = host_pipe[0];
990 } else
991#endif
Riku Voipio099d6b02009-05-05 12:10:04 +0300992 if (put_user_s32(host_pipe[0], pipedes)
993 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
994 return -TARGET_EFAULT;
995#endif
996 return get_errno(ret);
997}
998
Lionel Landwerlinb975b832009-04-25 23:30:19 +0200999static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1000 abi_ulong target_addr,
1001 socklen_t len)
1002{
1003 struct target_ip_mreqn *target_smreqn;
1004
1005 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1006 if (!target_smreqn)
1007 return -TARGET_EFAULT;
1008 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1009 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1010 if (len == sizeof(struct target_ip_mreqn))
1011 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
1012 unlock_user(target_smreqn, target_addr, 0);
1013
1014 return 0;
1015}
1016
bellard579a97f2007-11-11 14:26:47 +00001017static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1018 abi_ulong target_addr,
1019 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001020{
aurel32607175e2009-04-15 16:11:59 +00001021 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1022 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001023 struct target_sockaddr *target_saddr;
1024
bellard579a97f2007-11-11 14:26:47 +00001025 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1026 if (!target_saddr)
1027 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001028
1029 sa_family = tswap16(target_saddr->sa_family);
1030
1031 /* Oops. The caller might send a incomplete sun_path; sun_path
1032 * must be terminated by \0 (see the manual page), but
1033 * unfortunately it is quite common to specify sockaddr_un
1034 * length as "strlen(x->sun_path)" while it should be
1035 * "strlen(...) + 1". We'll fix that here if needed.
1036 * Linux kernel has a similar feature.
1037 */
1038
1039 if (sa_family == AF_UNIX) {
1040 if (len < unix_maxlen && len > 0) {
1041 char *cp = (char*)target_saddr;
1042
1043 if ( cp[len-1] && !cp[len] )
1044 len++;
1045 }
1046 if (len > unix_maxlen)
1047 len = unix_maxlen;
1048 }
1049
pbrook53a59602006-03-25 19:31:22 +00001050 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001051 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001052 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001053
1054 return 0;
bellard7854b052003-03-29 17:22:23 +00001055}
1056
bellard579a97f2007-11-11 14:26:47 +00001057static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1058 struct sockaddr *addr,
1059 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001060{
pbrook53a59602006-03-25 19:31:22 +00001061 struct target_sockaddr *target_saddr;
1062
bellard579a97f2007-11-11 14:26:47 +00001063 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1064 if (!target_saddr)
1065 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001066 memcpy(target_saddr, addr, len);
1067 target_saddr->sa_family = tswap16(addr->sa_family);
1068 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001069
1070 return 0;
bellard7854b052003-03-29 17:22:23 +00001071}
1072
pbrook53a59602006-03-25 19:31:22 +00001073/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001074static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1075 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001076{
1077 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001078 abi_long msg_controllen;
1079 abi_ulong target_cmsg_addr;
1080 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001081 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001082
1083 msg_controllen = tswapl(target_msgh->msg_controllen);
1084 if (msg_controllen < sizeof (struct target_cmsghdr))
1085 goto the_end;
1086 target_cmsg_addr = tswapl(target_msgh->msg_control);
1087 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1088 if (!target_cmsg)
1089 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001090
1091 while (cmsg && target_cmsg) {
1092 void *data = CMSG_DATA(cmsg);
1093 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1094
ths5fafdf22007-09-16 21:08:06 +00001095 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001096 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1097
1098 space += CMSG_SPACE(len);
1099 if (space > msgh->msg_controllen) {
1100 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001101 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001102 break;
1103 }
1104
1105 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1106 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1107 cmsg->cmsg_len = CMSG_LEN(len);
1108
bellard3532fa72006-06-24 15:06:03 +00001109 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001110 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1111 memcpy(data, target_data, len);
1112 } else {
1113 int *fd = (int *)data;
1114 int *target_fd = (int *)target_data;
1115 int i, numfds = len / sizeof(int);
1116
1117 for (i = 0; i < numfds; i++)
1118 fd[i] = tswap32(target_fd[i]);
1119 }
1120
1121 cmsg = CMSG_NXTHDR(msgh, cmsg);
1122 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1123 }
bellard5a4a8982007-11-11 17:39:18 +00001124 unlock_user(target_cmsg, target_cmsg_addr, 0);
1125 the_end:
bellard7854b052003-03-29 17:22:23 +00001126 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001127 return 0;
bellard7854b052003-03-29 17:22:23 +00001128}
1129
pbrook53a59602006-03-25 19:31:22 +00001130/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001131static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1132 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001133{
1134 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001135 abi_long msg_controllen;
1136 abi_ulong target_cmsg_addr;
1137 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001138 socklen_t space = 0;
1139
bellard5a4a8982007-11-11 17:39:18 +00001140 msg_controllen = tswapl(target_msgh->msg_controllen);
1141 if (msg_controllen < sizeof (struct target_cmsghdr))
1142 goto the_end;
1143 target_cmsg_addr = tswapl(target_msgh->msg_control);
1144 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1145 if (!target_cmsg)
1146 return -TARGET_EFAULT;
1147
bellard7854b052003-03-29 17:22:23 +00001148 while (cmsg && target_cmsg) {
1149 void *data = CMSG_DATA(cmsg);
1150 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1151
1152 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1153
1154 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001155 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001156 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001157 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001158 break;
1159 }
1160
1161 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1162 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1163 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1164
bellard3532fa72006-06-24 15:06:03 +00001165 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001166 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1167 memcpy(target_data, data, len);
1168 } else {
1169 int *fd = (int *)data;
1170 int *target_fd = (int *)target_data;
1171 int i, numfds = len / sizeof(int);
1172
1173 for (i = 0; i < numfds; i++)
1174 target_fd[i] = tswap32(fd[i]);
1175 }
1176
1177 cmsg = CMSG_NXTHDR(msgh, cmsg);
1178 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1179 }
bellard5a4a8982007-11-11 17:39:18 +00001180 unlock_user(target_cmsg, target_cmsg_addr, space);
1181 the_end:
1182 target_msgh->msg_controllen = tswapl(space);
1183 return 0;
bellard7854b052003-03-29 17:22:23 +00001184}
1185
ths0da46a62007-10-20 20:23:07 +00001186/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001187static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001188 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001189{
blueswir1992f48a2007-10-14 16:27:31 +00001190 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001191 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001192 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001193 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001194
bellard8853f862004-02-22 14:57:26 +00001195 switch(level) {
1196 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001197 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001198 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001199 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001200
bellard2f619692007-11-16 10:46:05 +00001201 if (get_user_u32(val, optval_addr))
1202 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001203 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1204 break;
1205 case SOL_IP:
1206 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001207 case IP_TOS:
1208 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001209 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001210 case IP_ROUTER_ALERT:
1211 case IP_RECVOPTS:
1212 case IP_RETOPTS:
1213 case IP_PKTINFO:
1214 case IP_MTU_DISCOVER:
1215 case IP_RECVERR:
1216 case IP_RECVTOS:
1217#ifdef IP_FREEBIND
1218 case IP_FREEBIND:
1219#endif
1220 case IP_MULTICAST_TTL:
1221 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001222 val = 0;
1223 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001224 if (get_user_u32(val, optval_addr))
1225 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001226 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001227 if (get_user_u8(val, optval_addr))
1228 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001229 }
1230 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1231 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001232 case IP_ADD_MEMBERSHIP:
1233 case IP_DROP_MEMBERSHIP:
1234 if (optlen < sizeof (struct target_ip_mreq) ||
1235 optlen > sizeof (struct target_ip_mreqn))
1236 return -TARGET_EINVAL;
1237
1238 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1239 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1240 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1241 break;
1242
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001243 case IP_BLOCK_SOURCE:
1244 case IP_UNBLOCK_SOURCE:
1245 case IP_ADD_SOURCE_MEMBERSHIP:
1246 case IP_DROP_SOURCE_MEMBERSHIP:
1247 if (optlen != sizeof (struct target_ip_mreq_source))
1248 return -TARGET_EINVAL;
1249
1250 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1251 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1252 unlock_user (ip_mreq_source, optval_addr, 0);
1253 break;
1254
bellard8853f862004-02-22 14:57:26 +00001255 default:
1256 goto unimplemented;
1257 }
1258 break;
bellard3532fa72006-06-24 15:06:03 +00001259 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001260 switch (optname) {
1261 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001262 case TARGET_SO_DEBUG:
1263 optname = SO_DEBUG;
1264 break;
1265 case TARGET_SO_REUSEADDR:
1266 optname = SO_REUSEADDR;
1267 break;
1268 case TARGET_SO_TYPE:
1269 optname = SO_TYPE;
1270 break;
1271 case TARGET_SO_ERROR:
1272 optname = SO_ERROR;
1273 break;
1274 case TARGET_SO_DONTROUTE:
1275 optname = SO_DONTROUTE;
1276 break;
1277 case TARGET_SO_BROADCAST:
1278 optname = SO_BROADCAST;
1279 break;
1280 case TARGET_SO_SNDBUF:
1281 optname = SO_SNDBUF;
1282 break;
1283 case TARGET_SO_RCVBUF:
1284 optname = SO_RCVBUF;
1285 break;
1286 case TARGET_SO_KEEPALIVE:
1287 optname = SO_KEEPALIVE;
1288 break;
1289 case TARGET_SO_OOBINLINE:
1290 optname = SO_OOBINLINE;
1291 break;
1292 case TARGET_SO_NO_CHECK:
1293 optname = SO_NO_CHECK;
1294 break;
1295 case TARGET_SO_PRIORITY:
1296 optname = SO_PRIORITY;
1297 break;
bellard5e83e8e2005-03-01 22:32:06 +00001298#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001299 case TARGET_SO_BSDCOMPAT:
1300 optname = SO_BSDCOMPAT;
1301 break;
bellard5e83e8e2005-03-01 22:32:06 +00001302#endif
bellard3532fa72006-06-24 15:06:03 +00001303 case TARGET_SO_PASSCRED:
1304 optname = SO_PASSCRED;
1305 break;
1306 case TARGET_SO_TIMESTAMP:
1307 optname = SO_TIMESTAMP;
1308 break;
1309 case TARGET_SO_RCVLOWAT:
1310 optname = SO_RCVLOWAT;
1311 break;
1312 case TARGET_SO_RCVTIMEO:
1313 optname = SO_RCVTIMEO;
1314 break;
1315 case TARGET_SO_SNDTIMEO:
1316 optname = SO_SNDTIMEO;
1317 break;
bellard8853f862004-02-22 14:57:26 +00001318 break;
1319 default:
1320 goto unimplemented;
1321 }
bellard3532fa72006-06-24 15:06:03 +00001322 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001323 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001324
bellard2f619692007-11-16 10:46:05 +00001325 if (get_user_u32(val, optval_addr))
1326 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001327 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001328 break;
bellard7854b052003-03-29 17:22:23 +00001329 default:
bellard8853f862004-02-22 14:57:26 +00001330 unimplemented:
1331 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001332 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001333 }
bellard8853f862004-02-22 14:57:26 +00001334 return ret;
bellard7854b052003-03-29 17:22:23 +00001335}
1336
ths0da46a62007-10-20 20:23:07 +00001337/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001338static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001339 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001340{
blueswir1992f48a2007-10-14 16:27:31 +00001341 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001342 int len, val;
1343 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001344
1345 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001346 case TARGET_SOL_SOCKET:
1347 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +00001348 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +00001349 case TARGET_SO_LINGER:
1350 case TARGET_SO_RCVTIMEO:
1351 case TARGET_SO_SNDTIMEO:
1352 case TARGET_SO_PEERCRED:
1353 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +00001354 /* These don't just return a single integer */
1355 goto unimplemented;
1356 default:
bellard2efbe912005-07-23 15:10:20 +00001357 goto int_case;
1358 }
1359 break;
1360 case SOL_TCP:
1361 /* TCP options all take an 'int' value. */
1362 int_case:
bellard2f619692007-11-16 10:46:05 +00001363 if (get_user_u32(len, optlen))
1364 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001365 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001366 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +00001367 lv = sizeof(int);
1368 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1369 if (ret < 0)
1370 return ret;
bellard2efbe912005-07-23 15:10:20 +00001371 if (len > lv)
1372 len = lv;
bellard2f619692007-11-16 10:46:05 +00001373 if (len == 4) {
1374 if (put_user_u32(val, optval_addr))
1375 return -TARGET_EFAULT;
1376 } else {
1377 if (put_user_u8(val, optval_addr))
1378 return -TARGET_EFAULT;
1379 }
1380 if (put_user_u32(len, optlen))
1381 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001382 break;
1383 case SOL_IP:
1384 switch(optname) {
1385 case IP_TOS:
1386 case IP_TTL:
1387 case IP_HDRINCL:
1388 case IP_ROUTER_ALERT:
1389 case IP_RECVOPTS:
1390 case IP_RETOPTS:
1391 case IP_PKTINFO:
1392 case IP_MTU_DISCOVER:
1393 case IP_RECVERR:
1394 case IP_RECVTOS:
1395#ifdef IP_FREEBIND
1396 case IP_FREEBIND:
1397#endif
1398 case IP_MULTICAST_TTL:
1399 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001400 if (get_user_u32(len, optlen))
1401 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001402 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001403 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +00001404 lv = sizeof(int);
1405 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1406 if (ret < 0)
1407 return ret;
bellard2efbe912005-07-23 15:10:20 +00001408 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001409 len = 1;
bellard2f619692007-11-16 10:46:05 +00001410 if (put_user_u32(len, optlen)
1411 || put_user_u8(val, optval_addr))
1412 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001413 } else {
bellard2efbe912005-07-23 15:10:20 +00001414 if (len > sizeof(int))
1415 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001416 if (put_user_u32(len, optlen)
1417 || put_user_u32(val, optval_addr))
1418 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001419 }
bellard8853f862004-02-22 14:57:26 +00001420 break;
bellard2efbe912005-07-23 15:10:20 +00001421 default:
thsc02f4992007-12-18 02:39:59 +00001422 ret = -TARGET_ENOPROTOOPT;
1423 break;
bellard8853f862004-02-22 14:57:26 +00001424 }
1425 break;
1426 default:
1427 unimplemented:
1428 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1429 level, optname);
thsc02f4992007-12-18 02:39:59 +00001430 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001431 break;
1432 }
1433 return ret;
bellard7854b052003-03-29 17:22:23 +00001434}
1435
bellard579a97f2007-11-11 14:26:47 +00001436/* FIXME
1437 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1438 * other lock functions have a return code of 0 for failure.
1439 */
1440static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1441 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001442{
1443 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001444 abi_ulong base;
balrogd732dcb2008-10-28 10:21:03 +00001445 int i;
pbrook53a59602006-03-25 19:31:22 +00001446
bellard579a97f2007-11-11 14:26:47 +00001447 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1448 if (!target_vec)
1449 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001450 for(i = 0;i < count; i++) {
1451 base = tswapl(target_vec[i].iov_base);
1452 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001453 if (vec[i].iov_len != 0) {
1454 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrogd732dcb2008-10-28 10:21:03 +00001455 /* Don't check lock_user return value. We must call writev even
1456 if a element has invalid base address. */
bellard41df8412008-02-04 22:26:57 +00001457 } else {
1458 /* zero length pointer is ignored */
1459 vec[i].iov_base = NULL;
1460 }
pbrook53a59602006-03-25 19:31:22 +00001461 }
1462 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001463 return 0;
pbrook53a59602006-03-25 19:31:22 +00001464}
1465
bellard579a97f2007-11-11 14:26:47 +00001466static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1467 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001468{
1469 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001470 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001471 int i;
1472
bellard579a97f2007-11-11 14:26:47 +00001473 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1474 if (!target_vec)
1475 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001476 for(i = 0;i < count; i++) {
balrogd732dcb2008-10-28 10:21:03 +00001477 if (target_vec[i].iov_base) {
1478 base = tswapl(target_vec[i].iov_base);
1479 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1480 }
pbrook53a59602006-03-25 19:31:22 +00001481 }
1482 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001483
1484 return 0;
pbrook53a59602006-03-25 19:31:22 +00001485}
1486
ths0da46a62007-10-20 20:23:07 +00001487/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001488static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001489{
1490#if defined(TARGET_MIPS)
1491 switch(type) {
1492 case TARGET_SOCK_DGRAM:
1493 type = SOCK_DGRAM;
1494 break;
1495 case TARGET_SOCK_STREAM:
1496 type = SOCK_STREAM;
1497 break;
1498 case TARGET_SOCK_RAW:
1499 type = SOCK_RAW;
1500 break;
1501 case TARGET_SOCK_RDM:
1502 type = SOCK_RDM;
1503 break;
1504 case TARGET_SOCK_SEQPACKET:
1505 type = SOCK_SEQPACKET;
1506 break;
1507 case TARGET_SOCK_PACKET:
1508 type = SOCK_PACKET;
1509 break;
1510 }
1511#endif
balrog12bc92a2007-10-30 21:06:14 +00001512 if (domain == PF_NETLINK)
1513 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001514 return get_errno(socket(domain, type, protocol));
1515}
1516
ths0da46a62007-10-20 20:23:07 +00001517/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001518static abi_long do_bind(int sockfd, abi_ulong target_addr,
1519 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001520{
aurel328f7aeaf2009-01-30 19:47:57 +00001521 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001522 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001523
aurel32be09ac42009-04-15 16:12:06 +00001524 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001525 return -TARGET_EINVAL;
1526
aurel32607175e2009-04-15 16:11:59 +00001527 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001528
Arnaud Patard917507b2009-06-19 10:44:45 +03001529 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1530 if (ret)
1531 return ret;
1532
bellard3532fa72006-06-24 15:06:03 +00001533 return get_errno(bind(sockfd, addr, addrlen));
1534}
1535
ths0da46a62007-10-20 20:23:07 +00001536/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001537static abi_long do_connect(int sockfd, abi_ulong target_addr,
1538 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001539{
aurel328f7aeaf2009-01-30 19:47:57 +00001540 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001541 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001542
aurel32be09ac42009-04-15 16:12:06 +00001543 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001544 return -TARGET_EINVAL;
1545
1546 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001547
Arnaud Patard917507b2009-06-19 10:44:45 +03001548 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1549 if (ret)
1550 return ret;
1551
bellard3532fa72006-06-24 15:06:03 +00001552 return get_errno(connect(sockfd, addr, addrlen));
1553}
1554
ths0da46a62007-10-20 20:23:07 +00001555/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001556static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1557 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001558{
balrog6de645c2008-10-28 10:26:29 +00001559 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001560 struct target_msghdr *msgp;
1561 struct msghdr msg;
1562 int count;
1563 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001564 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001565
bellard579a97f2007-11-11 14:26:47 +00001566 /* FIXME */
1567 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1568 msgp,
1569 target_msg,
1570 send ? 1 : 0))
1571 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001572 if (msgp->msg_name) {
1573 msg.msg_namelen = tswap32(msgp->msg_namelen);
1574 msg.msg_name = alloca(msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001575 ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001576 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001577 if (ret) {
1578 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1579 return ret;
1580 }
bellard3532fa72006-06-24 15:06:03 +00001581 } else {
1582 msg.msg_name = NULL;
1583 msg.msg_namelen = 0;
1584 }
1585 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1586 msg.msg_control = alloca(msg.msg_controllen);
1587 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001588
bellard3532fa72006-06-24 15:06:03 +00001589 count = tswapl(msgp->msg_iovlen);
1590 vec = alloca(count * sizeof(struct iovec));
1591 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001592 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001593 msg.msg_iovlen = count;
1594 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001595
bellard3532fa72006-06-24 15:06:03 +00001596 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001597 ret = target_to_host_cmsg(&msg, msgp);
1598 if (ret == 0)
1599 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001600 } else {
1601 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001602 if (!is_error(ret)) {
1603 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001604 ret = host_to_target_cmsg(msgp, &msg);
balrog6de645c2008-10-28 10:26:29 +00001605 if (!is_error(ret))
1606 ret = len;
1607 }
bellard3532fa72006-06-24 15:06:03 +00001608 }
1609 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001610 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001611 return ret;
1612}
1613
ths0da46a62007-10-20 20:23:07 +00001614/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001615static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001616 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001617{
bellard2f619692007-11-16 10:46:05 +00001618 socklen_t addrlen;
1619 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001620 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001621
Arnaud Patard917507b2009-06-19 10:44:45 +03001622 if (target_addr == 0)
1623 return get_errno(accept(fd, NULL, NULL));
1624
1625 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00001626 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03001627 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00001628
aurel32be09ac42009-04-15 16:12:06 +00001629 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001630 return -TARGET_EINVAL;
1631
Arnaud Patard917507b2009-06-19 10:44:45 +03001632 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1633 return -TARGET_EINVAL;
1634
bellard2f619692007-11-16 10:46:05 +00001635 addr = alloca(addrlen);
1636
pbrook1be9e1d2006-11-19 15:26:04 +00001637 ret = get_errno(accept(fd, addr, &addrlen));
1638 if (!is_error(ret)) {
1639 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001640 if (put_user_u32(addrlen, target_addrlen_addr))
1641 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001642 }
1643 return ret;
1644}
1645
ths0da46a62007-10-20 20:23:07 +00001646/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001647static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001648 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001649{
bellard2f619692007-11-16 10:46:05 +00001650 socklen_t addrlen;
1651 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001652 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001653
bellard2f619692007-11-16 10:46:05 +00001654 if (get_user_u32(addrlen, target_addrlen_addr))
1655 return -TARGET_EFAULT;
1656
aurel32be09ac42009-04-15 16:12:06 +00001657 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001658 return -TARGET_EINVAL;
1659
Arnaud Patard917507b2009-06-19 10:44:45 +03001660 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1661 return -TARGET_EFAULT;
1662
bellard2f619692007-11-16 10:46:05 +00001663 addr = alloca(addrlen);
1664
pbrook1be9e1d2006-11-19 15:26:04 +00001665 ret = get_errno(getpeername(fd, addr, &addrlen));
1666 if (!is_error(ret)) {
1667 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001668 if (put_user_u32(addrlen, target_addrlen_addr))
1669 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001670 }
1671 return ret;
1672}
1673
ths0da46a62007-10-20 20:23:07 +00001674/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001675static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001676 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001677{
bellard2f619692007-11-16 10:46:05 +00001678 socklen_t addrlen;
1679 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001680 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001681
bellard2f619692007-11-16 10:46:05 +00001682 if (get_user_u32(addrlen, target_addrlen_addr))
1683 return -TARGET_EFAULT;
1684
aurel32be09ac42009-04-15 16:12:06 +00001685 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001686 return -TARGET_EINVAL;
1687
Arnaud Patard917507b2009-06-19 10:44:45 +03001688 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1689 return -TARGET_EFAULT;
1690
bellard2f619692007-11-16 10:46:05 +00001691 addr = alloca(addrlen);
1692
pbrook1be9e1d2006-11-19 15:26:04 +00001693 ret = get_errno(getsockname(fd, addr, &addrlen));
1694 if (!is_error(ret)) {
1695 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001696 if (put_user_u32(addrlen, target_addrlen_addr))
1697 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001698 }
1699 return ret;
1700}
1701
ths0da46a62007-10-20 20:23:07 +00001702/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001703static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001704 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001705{
1706 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001707 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001708
1709 ret = get_errno(socketpair(domain, type, protocol, tab));
1710 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001711 if (put_user_s32(tab[0], target_tab_addr)
1712 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1713 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001714 }
1715 return ret;
1716}
1717
ths0da46a62007-10-20 20:23:07 +00001718/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001719static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1720 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001721{
1722 void *addr;
1723 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001724 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001725
aurel32be09ac42009-04-15 16:12:06 +00001726 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001727 return -TARGET_EINVAL;
1728
bellard579a97f2007-11-11 14:26:47 +00001729 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1730 if (!host_msg)
1731 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001732 if (target_addr) {
1733 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001734 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1735 if (ret) {
1736 unlock_user(host_msg, msg, 0);
1737 return ret;
1738 }
pbrook1be9e1d2006-11-19 15:26:04 +00001739 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1740 } else {
1741 ret = get_errno(send(fd, host_msg, len, flags));
1742 }
1743 unlock_user(host_msg, msg, 0);
1744 return ret;
1745}
1746
ths0da46a62007-10-20 20:23:07 +00001747/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001748static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1749 abi_ulong target_addr,
1750 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001751{
1752 socklen_t addrlen;
1753 void *addr;
1754 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001755 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001756
bellard579a97f2007-11-11 14:26:47 +00001757 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1758 if (!host_msg)
1759 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001760 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001761 if (get_user_u32(addrlen, target_addrlen)) {
1762 ret = -TARGET_EFAULT;
1763 goto fail;
1764 }
aurel32be09ac42009-04-15 16:12:06 +00001765 if (addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001766 ret = -TARGET_EINVAL;
1767 goto fail;
1768 }
pbrook1be9e1d2006-11-19 15:26:04 +00001769 addr = alloca(addrlen);
1770 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1771 } else {
1772 addr = NULL; /* To keep compiler quiet. */
1773 ret = get_errno(recv(fd, host_msg, len, flags));
1774 }
1775 if (!is_error(ret)) {
1776 if (target_addr) {
1777 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001778 if (put_user_u32(addrlen, target_addrlen)) {
1779 ret = -TARGET_EFAULT;
1780 goto fail;
1781 }
pbrook1be9e1d2006-11-19 15:26:04 +00001782 }
1783 unlock_user(host_msg, msg, len);
1784 } else {
bellard2f619692007-11-16 10:46:05 +00001785fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001786 unlock_user(host_msg, msg, 0);
1787 }
1788 return ret;
1789}
1790
j_mayer32407102007-09-26 23:01:49 +00001791#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001792/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001793static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001794{
blueswir1992f48a2007-10-14 16:27:31 +00001795 abi_long ret;
1796 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001797
1798 switch(num) {
1799 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001800 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001801 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001802
Ulrich Hecht98818182009-07-03 17:09:28 +02001803 if (get_user_ual(domain, vptr)
1804 || get_user_ual(type, vptr + n)
1805 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001806 return -TARGET_EFAULT;
1807
bellard3532fa72006-06-24 15:06:03 +00001808 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001809 }
bellard31e31b82003-02-18 22:55:36 +00001810 break;
1811 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001812 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001813 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001814 abi_ulong target_addr;
1815 socklen_t addrlen;
1816
Ulrich Hecht98818182009-07-03 17:09:28 +02001817 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001818 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001819 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001820 return -TARGET_EFAULT;
1821
bellard3532fa72006-06-24 15:06:03 +00001822 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001823 }
bellard31e31b82003-02-18 22:55:36 +00001824 break;
1825 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001826 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001827 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001828 abi_ulong target_addr;
1829 socklen_t addrlen;
1830
Ulrich Hecht98818182009-07-03 17:09:28 +02001831 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001832 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001833 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001834 return -TARGET_EFAULT;
1835
bellard3532fa72006-06-24 15:06:03 +00001836 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001837 }
bellard31e31b82003-02-18 22:55:36 +00001838 break;
1839 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001840 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001841 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00001842
Ulrich Hecht98818182009-07-03 17:09:28 +02001843 if (get_user_ual(sockfd, vptr)
1844 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00001845 return -TARGET_EFAULT;
1846
bellard7854b052003-03-29 17:22:23 +00001847 ret = get_errno(listen(sockfd, backlog));
1848 }
bellard31e31b82003-02-18 22:55:36 +00001849 break;
1850 case SOCKOP_accept:
1851 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001852 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001853 abi_ulong target_addr, target_addrlen;
1854
Ulrich Hecht98818182009-07-03 17:09:28 +02001855 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001856 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001857 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001858 return -TARGET_EFAULT;
1859
pbrook1be9e1d2006-11-19 15:26:04 +00001860 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001861 }
1862 break;
1863 case SOCKOP_getsockname:
1864 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001865 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001866 abi_ulong target_addr, target_addrlen;
1867
Ulrich Hecht98818182009-07-03 17:09:28 +02001868 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001869 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001870 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001871 return -TARGET_EFAULT;
1872
pbrook1be9e1d2006-11-19 15:26:04 +00001873 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001874 }
1875 break;
1876 case SOCKOP_getpeername:
1877 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001878 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001879 abi_ulong target_addr, target_addrlen;
1880
Ulrich Hecht98818182009-07-03 17:09:28 +02001881 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001882 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001883 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001884 return -TARGET_EFAULT;
1885
pbrook1be9e1d2006-11-19 15:26:04 +00001886 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001887 }
1888 break;
1889 case SOCKOP_socketpair:
1890 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001891 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001892 abi_ulong tab;
1893
Ulrich Hecht98818182009-07-03 17:09:28 +02001894 if (get_user_ual(domain, vptr)
1895 || get_user_ual(type, vptr + n)
1896 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00001897 || get_user_ual(tab, vptr + 3 * n))
1898 return -TARGET_EFAULT;
1899
pbrook1be9e1d2006-11-19 15:26:04 +00001900 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001901 }
1902 break;
1903 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001904 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001905 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001906 abi_ulong msg;
1907 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001908 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001909
Ulrich Hecht98818182009-07-03 17:09:28 +02001910 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001911 || get_user_ual(msg, vptr + n)
1912 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001913 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00001914 return -TARGET_EFAULT;
1915
pbrook1be9e1d2006-11-19 15:26:04 +00001916 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001917 }
bellard31e31b82003-02-18 22:55:36 +00001918 break;
1919 case SOCKOP_recv:
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_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001933 }
bellard31e31b82003-02-18 22:55:36 +00001934 break;
1935 case SOCKOP_sendto:
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 abi_ulong addr;
1942 socklen_t addrlen;
1943
Ulrich Hecht98818182009-07-03 17:09:28 +02001944 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001945 || get_user_ual(msg, vptr + n)
1946 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001947 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00001948 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001949 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00001950 return -TARGET_EFAULT;
1951
pbrook1be9e1d2006-11-19 15:26:04 +00001952 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001953 }
bellard31e31b82003-02-18 22:55:36 +00001954 break;
1955 case SOCKOP_recvfrom:
1956 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001957 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001958 abi_ulong msg;
1959 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001960 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001961 abi_ulong addr;
1962 socklen_t addrlen;
1963
Ulrich Hecht98818182009-07-03 17:09:28 +02001964 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001965 || get_user_ual(msg, vptr + n)
1966 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001967 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00001968 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001969 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00001970 return -TARGET_EFAULT;
1971
pbrook1be9e1d2006-11-19 15:26:04 +00001972 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001973 }
1974 break;
1975 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001976 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001977 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00001978
Ulrich Hecht98818182009-07-03 17:09:28 +02001979 if (get_user_ual(sockfd, vptr)
1980 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00001981 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001982
1983 ret = get_errno(shutdown(sockfd, how));
1984 }
bellard31e31b82003-02-18 22:55:36 +00001985 break;
1986 case SOCKOP_sendmsg:
1987 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001988 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001989 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00001990 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02001991 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00001992
Ulrich Hecht98818182009-07-03 17:09:28 +02001993 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00001994 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001995 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001996 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001997
ths5fafdf22007-09-16 21:08:06 +00001998 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001999 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002000 }
2001 break;
bellard31e31b82003-02-18 22:55:36 +00002002 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002003 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002004 abi_ulong sockfd;
2005 abi_ulong level;
2006 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002007 abi_ulong optval;
2008 socklen_t optlen;
2009
Ulrich Hecht98818182009-07-03 17:09:28 +02002010 if (get_user_ual(sockfd, vptr)
2011 || get_user_ual(level, vptr + n)
2012 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002013 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002014 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002015 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002016
2017 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2018 }
2019 break;
bellard31e31b82003-02-18 22:55:36 +00002020 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002021 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002022 abi_ulong sockfd;
2023 abi_ulong level;
2024 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002025 abi_ulong optval;
2026 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002027
Ulrich Hecht98818182009-07-03 17:09:28 +02002028 if (get_user_ual(sockfd, vptr)
2029 || get_user_ual(level, vptr + n)
2030 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002031 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002032 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002033 return -TARGET_EFAULT;
2034
2035 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002036 }
2037 break;
bellard31e31b82003-02-18 22:55:36 +00002038 default:
2039 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002040 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002041 break;
2042 }
2043 return ret;
2044}
j_mayer32407102007-09-26 23:01:49 +00002045#endif
bellard31e31b82003-02-18 22:55:36 +00002046
bellard8853f862004-02-22 14:57:26 +00002047#define N_SHM_REGIONS 32
2048
2049static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002050 abi_ulong start;
2051 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002052} shm_regions[N_SHM_REGIONS];
2053
ths3eb6b042007-06-03 14:26:27 +00002054struct target_ipc_perm
2055{
blueswir1992f48a2007-10-14 16:27:31 +00002056 abi_long __key;
2057 abi_ulong uid;
2058 abi_ulong gid;
2059 abi_ulong cuid;
2060 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002061 unsigned short int mode;
2062 unsigned short int __pad1;
2063 unsigned short int __seq;
2064 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002065 abi_ulong __unused1;
2066 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002067};
2068
2069struct target_semid_ds
2070{
2071 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002072 abi_ulong sem_otime;
2073 abi_ulong __unused1;
2074 abi_ulong sem_ctime;
2075 abi_ulong __unused2;
2076 abi_ulong sem_nsems;
2077 abi_ulong __unused3;
2078 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002079};
2080
bellard579a97f2007-11-11 14:26:47 +00002081static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2082 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002083{
2084 struct target_ipc_perm *target_ip;
2085 struct target_semid_ds *target_sd;
2086
bellard579a97f2007-11-11 14:26:47 +00002087 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2088 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002089 target_ip = &(target_sd->sem_perm);
ths3eb6b042007-06-03 14:26:27 +00002090 host_ip->__key = tswapl(target_ip->__key);
2091 host_ip->uid = tswapl(target_ip->uid);
2092 host_ip->gid = tswapl(target_ip->gid);
2093 host_ip->cuid = tswapl(target_ip->cuid);
2094 host_ip->cgid = tswapl(target_ip->cgid);
2095 host_ip->mode = tswapl(target_ip->mode);
2096 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002097 return 0;
ths3eb6b042007-06-03 14:26:27 +00002098}
2099
bellard579a97f2007-11-11 14:26:47 +00002100static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2101 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002102{
2103 struct target_ipc_perm *target_ip;
2104 struct target_semid_ds *target_sd;
2105
bellard579a97f2007-11-11 14:26:47 +00002106 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2107 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002108 target_ip = &(target_sd->sem_perm);
2109 target_ip->__key = tswapl(host_ip->__key);
2110 target_ip->uid = tswapl(host_ip->uid);
2111 target_ip->gid = tswapl(host_ip->gid);
2112 target_ip->cuid = tswapl(host_ip->cuid);
2113 target_ip->cgid = tswapl(host_ip->cgid);
2114 target_ip->mode = tswapl(host_ip->mode);
2115 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002116 return 0;
ths3eb6b042007-06-03 14:26:27 +00002117}
2118
bellard579a97f2007-11-11 14:26:47 +00002119static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2120 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002121{
2122 struct target_semid_ds *target_sd;
2123
bellard579a97f2007-11-11 14:26:47 +00002124 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2125 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002126 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2127 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002128 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2129 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2130 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2131 unlock_user_struct(target_sd, target_addr, 0);
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 host_to_target_semid_ds(abi_ulong target_addr,
2136 struct semid_ds *host_sd)
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_WRITE, target_sd, target_addr, 0))
2141 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002142 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2143 return -TARGET_EFAULT;;
ths3eb6b042007-06-03 14:26:27 +00002144 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2145 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2146 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2147 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002148 return 0;
ths3eb6b042007-06-03 14:26:27 +00002149}
2150
aurel32e5289082009-04-18 16:16:12 +00002151struct target_seminfo {
2152 int semmap;
2153 int semmni;
2154 int semmns;
2155 int semmnu;
2156 int semmsl;
2157 int semopm;
2158 int semume;
2159 int semusz;
2160 int semvmx;
2161 int semaem;
2162};
2163
2164static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2165 struct seminfo *host_seminfo)
2166{
2167 struct target_seminfo *target_seminfo;
2168 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2169 return -TARGET_EFAULT;
2170 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2171 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2172 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2173 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2174 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2175 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2176 __put_user(host_seminfo->semume, &target_seminfo->semume);
2177 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2178 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2179 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2180 unlock_user_struct(target_seminfo, target_addr, 1);
2181 return 0;
2182}
2183
thsfa294812007-02-02 22:05:00 +00002184union semun {
2185 int val;
ths3eb6b042007-06-03 14:26:27 +00002186 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002187 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002188 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002189};
2190
ths3eb6b042007-06-03 14:26:27 +00002191union target_semun {
2192 int val;
aurel32e5289082009-04-18 16:16:12 +00002193 abi_ulong buf;
2194 abi_ulong array;
2195 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002196};
2197
aurel32e5289082009-04-18 16:16:12 +00002198static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2199 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002200{
aurel32e5289082009-04-18 16:16:12 +00002201 int nsems;
2202 unsigned short *array;
2203 union semun semun;
2204 struct semid_ds semid_ds;
2205 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002206
aurel32e5289082009-04-18 16:16:12 +00002207 semun.buf = &semid_ds;
2208
2209 ret = semctl(semid, 0, IPC_STAT, semun);
2210 if (ret == -1)
2211 return get_errno(ret);
2212
2213 nsems = semid_ds.sem_nsems;
2214
2215 *host_array = malloc(nsems*sizeof(unsigned short));
2216 array = lock_user(VERIFY_READ, target_addr,
2217 nsems*sizeof(unsigned short), 1);
2218 if (!array)
2219 return -TARGET_EFAULT;
2220
2221 for(i=0; i<nsems; i++) {
2222 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002223 }
aurel32e5289082009-04-18 16:16:12 +00002224 unlock_user(array, target_addr, 0);
2225
bellard579a97f2007-11-11 14:26:47 +00002226 return 0;
ths3eb6b042007-06-03 14:26:27 +00002227}
2228
aurel32e5289082009-04-18 16:16:12 +00002229static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2230 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002231{
aurel32e5289082009-04-18 16:16:12 +00002232 int nsems;
2233 unsigned short *array;
2234 union semun semun;
2235 struct semid_ds semid_ds;
2236 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002237
aurel32e5289082009-04-18 16:16:12 +00002238 semun.buf = &semid_ds;
2239
2240 ret = semctl(semid, 0, IPC_STAT, semun);
2241 if (ret == -1)
2242 return get_errno(ret);
2243
2244 nsems = semid_ds.sem_nsems;
2245
2246 array = lock_user(VERIFY_WRITE, target_addr,
2247 nsems*sizeof(unsigned short), 0);
2248 if (!array)
2249 return -TARGET_EFAULT;
2250
2251 for(i=0; i<nsems; i++) {
2252 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002253 }
aurel32e5289082009-04-18 16:16:12 +00002254 free(*host_array);
2255 unlock_user(array, target_addr, 1);
2256
bellard579a97f2007-11-11 14:26:47 +00002257 return 0;
ths3eb6b042007-06-03 14:26:27 +00002258}
2259
aurel32e5289082009-04-18 16:16:12 +00002260static inline abi_long do_semctl(int semid, int semnum, int cmd,
2261 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002262{
2263 union semun arg;
2264 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302265 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002266 struct seminfo seminfo;
2267 abi_long ret = -TARGET_EINVAL;
2268 abi_long err;
2269 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002270
2271 switch( cmd ) {
2272 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002273 case SETVAL:
aurel32e5289082009-04-18 16:16:12 +00002274 arg.val = tswapl(target_su.val);
2275 ret = get_errno(semctl(semid, semnum, cmd, arg));
2276 target_su.val = tswapl(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002277 break;
2278 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002279 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002280 err = target_to_host_semarray(semid, &array, target_su.array);
2281 if (err)
2282 return err;
2283 arg.array = array;
2284 ret = get_errno(semctl(semid, semnum, cmd, arg));
2285 err = host_to_target_semarray(semid, target_su.array, &array);
2286 if (err)
2287 return err;
ths3eb6b042007-06-03 14:26:27 +00002288 break;
2289 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002290 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002291 case SEM_STAT:
2292 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2293 if (err)
2294 return err;
2295 arg.buf = &dsarg;
2296 ret = get_errno(semctl(semid, semnum, cmd, arg));
2297 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2298 if (err)
2299 return err;
ths3eb6b042007-06-03 14:26:27 +00002300 break;
aurel32e5289082009-04-18 16:16:12 +00002301 case IPC_INFO:
2302 case SEM_INFO:
2303 arg.__buf = &seminfo;
2304 ret = get_errno(semctl(semid, semnum, cmd, arg));
2305 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2306 if (err)
2307 return err;
2308 break;
2309 case IPC_RMID:
2310 case GETPID:
2311 case GETNCNT:
2312 case GETZCNT:
2313 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2314 break;
ths3eb6b042007-06-03 14:26:27 +00002315 }
2316
2317 return ret;
2318}
2319
aurel32e5289082009-04-18 16:16:12 +00002320struct target_sembuf {
2321 unsigned short sem_num;
2322 short sem_op;
2323 short sem_flg;
2324};
2325
2326static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2327 abi_ulong target_addr,
2328 unsigned nsops)
2329{
2330 struct target_sembuf *target_sembuf;
2331 int i;
2332
2333 target_sembuf = lock_user(VERIFY_READ, target_addr,
2334 nsops*sizeof(struct target_sembuf), 1);
2335 if (!target_sembuf)
2336 return -TARGET_EFAULT;
2337
2338 for(i=0; i<nsops; i++) {
2339 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2340 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2341 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2342 }
2343
2344 unlock_user(target_sembuf, target_addr, 0);
2345
2346 return 0;
2347}
2348
2349static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2350{
2351 struct sembuf sops[nsops];
2352
2353 if (target_to_host_sembuf(sops, ptr, nsops))
2354 return -TARGET_EFAULT;
2355
2356 return semop(semid, sops, nsops);
2357}
2358
ths1bc012f2007-06-03 14:27:49 +00002359struct target_msqid_ds
2360{
aurel321c54ff92008-10-13 21:08:44 +00002361 struct target_ipc_perm msg_perm;
2362 abi_ulong msg_stime;
2363#if TARGET_ABI_BITS == 32
2364 abi_ulong __unused1;
2365#endif
2366 abi_ulong msg_rtime;
2367#if TARGET_ABI_BITS == 32
2368 abi_ulong __unused2;
2369#endif
2370 abi_ulong msg_ctime;
2371#if TARGET_ABI_BITS == 32
2372 abi_ulong __unused3;
2373#endif
2374 abi_ulong __msg_cbytes;
2375 abi_ulong msg_qnum;
2376 abi_ulong msg_qbytes;
2377 abi_ulong msg_lspid;
2378 abi_ulong msg_lrpid;
2379 abi_ulong __unused4;
2380 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002381};
2382
bellard579a97f2007-11-11 14:26:47 +00002383static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2384 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002385{
2386 struct target_msqid_ds *target_md;
2387
bellard579a97f2007-11-11 14:26:47 +00002388 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2389 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002390 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2391 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002392 host_md->msg_stime = tswapl(target_md->msg_stime);
2393 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2394 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2395 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2396 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2397 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2398 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2399 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2400 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002401 return 0;
ths1bc012f2007-06-03 14:27:49 +00002402}
2403
bellard579a97f2007-11-11 14:26:47 +00002404static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2405 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002406{
2407 struct target_msqid_ds *target_md;
2408
bellard579a97f2007-11-11 14:26:47 +00002409 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2410 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002411 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2412 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002413 target_md->msg_stime = tswapl(host_md->msg_stime);
2414 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2415 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2416 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2417 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2418 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2419 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2420 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2421 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002422 return 0;
ths1bc012f2007-06-03 14:27:49 +00002423}
2424
aurel321c54ff92008-10-13 21:08:44 +00002425struct target_msginfo {
2426 int msgpool;
2427 int msgmap;
2428 int msgmax;
2429 int msgmnb;
2430 int msgmni;
2431 int msgssz;
2432 int msgtql;
2433 unsigned short int msgseg;
2434};
2435
2436static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2437 struct msginfo *host_msginfo)
2438{
2439 struct target_msginfo *target_msginfo;
2440 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2441 return -TARGET_EFAULT;
2442 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2443 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2444 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2445 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2446 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2447 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2448 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2449 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2450 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002451 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002452}
2453
2454static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002455{
2456 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002457 struct msginfo msginfo;
2458 abi_long ret = -TARGET_EINVAL;
2459
2460 cmd &= 0xff;
2461
2462 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002463 case IPC_STAT:
2464 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002465 case MSG_STAT:
2466 if (target_to_host_msqid_ds(&dsarg,ptr))
2467 return -TARGET_EFAULT;
2468 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2469 if (host_to_target_msqid_ds(ptr,&dsarg))
2470 return -TARGET_EFAULT;
2471 break;
2472 case IPC_RMID:
2473 ret = get_errno(msgctl(msgid, cmd, NULL));
2474 break;
2475 case IPC_INFO:
2476 case MSG_INFO:
2477 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2478 if (host_to_target_msginfo(ptr, &msginfo))
2479 return -TARGET_EFAULT;
2480 break;
ths1bc012f2007-06-03 14:27:49 +00002481 }
aurel321c54ff92008-10-13 21:08:44 +00002482
ths1bc012f2007-06-03 14:27:49 +00002483 return ret;
2484}
2485
2486struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002487 abi_long mtype;
2488 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002489};
2490
blueswir1992f48a2007-10-14 16:27:31 +00002491static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2492 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002493{
2494 struct target_msgbuf *target_mb;
2495 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002496 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002497
bellard579a97f2007-11-11 14:26:47 +00002498 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2499 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002500 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002501 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2502 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002503 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2504 free(host_mb);
2505 unlock_user_struct(target_mb, msgp, 0);
2506
2507 return ret;
2508}
2509
blueswir1992f48a2007-10-14 16:27:31 +00002510static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002511 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002512 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002513{
2514 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002515 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002516 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002517 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002518
bellard579a97f2007-11-11 14:26:47 +00002519 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2520 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002521
ths1bc012f2007-06-03 14:27:49 +00002522 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002523 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2524
bellard579a97f2007-11-11 14:26:47 +00002525 if (ret > 0) {
2526 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2527 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2528 if (!target_mtext) {
2529 ret = -TARGET_EFAULT;
2530 goto end;
2531 }
aurel321c54ff92008-10-13 21:08:44 +00002532 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002533 unlock_user(target_mtext, target_mtext_addr, ret);
2534 }
aurel321c54ff92008-10-13 21:08:44 +00002535
ths1bc012f2007-06-03 14:27:49 +00002536 target_mb->mtype = tswapl(host_mb->mtype);
2537 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002538
bellard579a97f2007-11-11 14:26:47 +00002539end:
2540 if (target_mb)
2541 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00002542 return ret;
2543}
2544
Riku Voipio88a8c982009-04-03 10:42:00 +03002545struct target_shmid_ds
2546{
2547 struct target_ipc_perm shm_perm;
2548 abi_ulong shm_segsz;
2549 abi_ulong shm_atime;
2550#if TARGET_ABI_BITS == 32
2551 abi_ulong __unused1;
2552#endif
2553 abi_ulong shm_dtime;
2554#if TARGET_ABI_BITS == 32
2555 abi_ulong __unused2;
2556#endif
2557 abi_ulong shm_ctime;
2558#if TARGET_ABI_BITS == 32
2559 abi_ulong __unused3;
2560#endif
2561 int shm_cpid;
2562 int shm_lpid;
2563 abi_ulong shm_nattch;
2564 unsigned long int __unused4;
2565 unsigned long int __unused5;
2566};
2567
2568static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2569 abi_ulong target_addr)
2570{
2571 struct target_shmid_ds *target_sd;
2572
2573 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2574 return -TARGET_EFAULT;
2575 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2576 return -TARGET_EFAULT;
2577 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2578 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2579 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2580 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2581 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2582 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2583 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2584 unlock_user_struct(target_sd, target_addr, 0);
2585 return 0;
2586}
2587
2588static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2589 struct shmid_ds *host_sd)
2590{
2591 struct target_shmid_ds *target_sd;
2592
2593 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2594 return -TARGET_EFAULT;
2595 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2596 return -TARGET_EFAULT;
2597 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2598 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2599 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2600 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2601 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2602 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2603 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2604 unlock_user_struct(target_sd, target_addr, 1);
2605 return 0;
2606}
2607
2608struct target_shminfo {
2609 abi_ulong shmmax;
2610 abi_ulong shmmin;
2611 abi_ulong shmmni;
2612 abi_ulong shmseg;
2613 abi_ulong shmall;
2614};
2615
2616static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2617 struct shminfo *host_shminfo)
2618{
2619 struct target_shminfo *target_shminfo;
2620 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2621 return -TARGET_EFAULT;
2622 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2623 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2624 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2625 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2626 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2627 unlock_user_struct(target_shminfo, target_addr, 1);
2628 return 0;
2629}
2630
2631struct target_shm_info {
2632 int used_ids;
2633 abi_ulong shm_tot;
2634 abi_ulong shm_rss;
2635 abi_ulong shm_swp;
2636 abi_ulong swap_attempts;
2637 abi_ulong swap_successes;
2638};
2639
2640static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2641 struct shm_info *host_shm_info)
2642{
2643 struct target_shm_info *target_shm_info;
2644 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2645 return -TARGET_EFAULT;
2646 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2647 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2648 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2649 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2650 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2651 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2652 unlock_user_struct(target_shm_info, target_addr, 1);
2653 return 0;
2654}
2655
2656static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2657{
2658 struct shmid_ds dsarg;
2659 struct shminfo shminfo;
2660 struct shm_info shm_info;
2661 abi_long ret = -TARGET_EINVAL;
2662
2663 cmd &= 0xff;
2664
2665 switch(cmd) {
2666 case IPC_STAT:
2667 case IPC_SET:
2668 case SHM_STAT:
2669 if (target_to_host_shmid_ds(&dsarg, buf))
2670 return -TARGET_EFAULT;
2671 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2672 if (host_to_target_shmid_ds(buf, &dsarg))
2673 return -TARGET_EFAULT;
2674 break;
2675 case IPC_INFO:
2676 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2677 if (host_to_target_shminfo(buf, &shminfo))
2678 return -TARGET_EFAULT;
2679 break;
2680 case SHM_INFO:
2681 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2682 if (host_to_target_shm_info(buf, &shm_info))
2683 return -TARGET_EFAULT;
2684 break;
2685 case IPC_RMID:
2686 case SHM_LOCK:
2687 case SHM_UNLOCK:
2688 ret = get_errno(shmctl(shmid, cmd, NULL));
2689 break;
2690 }
2691
2692 return ret;
2693}
2694
2695static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2696{
2697 abi_long raddr;
2698 void *host_raddr;
2699 struct shmid_ds shm_info;
2700 int i,ret;
2701
2702 /* find out the length of the shared memory segment */
2703 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2704 if (is_error(ret)) {
2705 /* can't get length, bail out */
2706 return ret;
2707 }
2708
2709 mmap_lock();
2710
2711 if (shmaddr)
2712 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2713 else {
2714 abi_ulong mmap_start;
2715
2716 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2717
2718 if (mmap_start == -1) {
2719 errno = ENOMEM;
2720 host_raddr = (void *)-1;
2721 } else
2722 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2723 }
2724
2725 if (host_raddr == (void *)-1) {
2726 mmap_unlock();
2727 return get_errno((long)host_raddr);
2728 }
2729 raddr=h2g((unsigned long)host_raddr);
2730
2731 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2732 PAGE_VALID | PAGE_READ |
2733 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2734
2735 for (i = 0; i < N_SHM_REGIONS; i++) {
2736 if (shm_regions[i].start == 0) {
2737 shm_regions[i].start = raddr;
2738 shm_regions[i].size = shm_info.shm_segsz;
2739 break;
2740 }
2741 }
2742
2743 mmap_unlock();
2744 return raddr;
2745
2746}
2747
2748static inline abi_long do_shmdt(abi_ulong shmaddr)
2749{
2750 int i;
2751
2752 for (i = 0; i < N_SHM_REGIONS; ++i) {
2753 if (shm_regions[i].start == shmaddr) {
2754 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09002755 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03002756 break;
2757 }
2758 }
2759
2760 return get_errno(shmdt(g2h(shmaddr)));
2761}
2762
aurel321c54ff92008-10-13 21:08:44 +00002763#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00002764/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00002765/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002766static abi_long do_ipc(unsigned int call, int first,
2767 int second, int third,
2768 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00002769{
2770 int version;
blueswir1992f48a2007-10-14 16:27:31 +00002771 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00002772
2773 version = call >> 16;
2774 call &= 0xffff;
2775
2776 switch (call) {
thsfa294812007-02-02 22:05:00 +00002777 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00002778 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00002779 break;
2780
2781 case IPCOP_semget:
2782 ret = get_errno(semget(first, second, third));
2783 break;
2784
2785 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00002786 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00002787 break;
thsd96372e2007-02-02 22:05:44 +00002788
aurel321c54ff92008-10-13 21:08:44 +00002789 case IPCOP_msgget:
2790 ret = get_errno(msgget(first, second));
2791 break;
thsd96372e2007-02-02 22:05:44 +00002792
aurel321c54ff92008-10-13 21:08:44 +00002793 case IPCOP_msgsnd:
2794 ret = do_msgsnd(first, ptr, second, third);
2795 break;
thsd96372e2007-02-02 22:05:44 +00002796
aurel321c54ff92008-10-13 21:08:44 +00002797 case IPCOP_msgctl:
2798 ret = do_msgctl(first, second, ptr);
2799 break;
thsd96372e2007-02-02 22:05:44 +00002800
aurel321c54ff92008-10-13 21:08:44 +00002801 case IPCOP_msgrcv:
2802 switch (version) {
2803 case 0:
2804 {
2805 struct target_ipc_kludge {
2806 abi_long msgp;
2807 abi_long msgtyp;
2808 } *tmp;
thsd96372e2007-02-02 22:05:44 +00002809
aurel321c54ff92008-10-13 21:08:44 +00002810 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2811 ret = -TARGET_EFAULT;
2812 break;
ths1bc012f2007-06-03 14:27:49 +00002813 }
aurel321c54ff92008-10-13 21:08:44 +00002814
2815 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2816
2817 unlock_user_struct(tmp, ptr, 0);
2818 break;
2819 }
2820 default:
2821 ret = do_msgrcv(first, ptr, second, fifth, third);
2822 }
2823 break;
thsd96372e2007-02-02 22:05:44 +00002824
bellard8853f862004-02-22 14:57:26 +00002825 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03002826 switch (version) {
2827 default:
bellard5a4a8982007-11-11 17:39:18 +00002828 {
2829 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03002830 raddr = do_shmat(first, ptr, second);
2831 if (is_error(raddr))
2832 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00002833 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002834 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03002835 break;
2836 }
2837 case 1:
2838 ret = -TARGET_EINVAL;
2839 break;
bellard5a4a8982007-11-11 17:39:18 +00002840 }
bellard8853f862004-02-22 14:57:26 +00002841 break;
2842 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03002843 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00002844 break;
2845
2846 case IPCOP_shmget:
2847 /* IPC_* flag values are the same on all linux platforms */
2848 ret = get_errno(shmget(first, second, third));
2849 break;
2850
2851 /* IPC_* and SHM_* command values are the same on all linux platforms */
2852 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03002853 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00002854 break;
2855 default:
j_mayer32407102007-09-26 23:01:49 +00002856 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002857 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002858 break;
2859 }
2860 return ret;
2861}
j_mayer32407102007-09-26 23:01:49 +00002862#endif
bellard8853f862004-02-22 14:57:26 +00002863
bellard31e31b82003-02-18 22:55:36 +00002864/* kernel structure types definitions */
2865#define IFNAMSIZ 16
2866
Blue Swirl001faf32009-05-13 17:53:17 +00002867#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00002868#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2869enum {
2870#include "syscall_types.h"
2871};
2872#undef STRUCT
2873#undef STRUCT_SPECIAL
2874
Blue Swirl001faf32009-05-13 17:53:17 +00002875#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00002876#define STRUCT_SPECIAL(name)
2877#include "syscall_types.h"
2878#undef STRUCT
2879#undef STRUCT_SPECIAL
2880
2881typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002882 unsigned int target_cmd;
2883 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002884 const char *name;
2885 int access;
bellard1a9353d2003-03-16 20:28:50 +00002886 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002887} IOCTLEntry;
2888
2889#define IOC_R 0x0001
2890#define IOC_W 0x0002
2891#define IOC_RW (IOC_R | IOC_W)
2892
2893#define MAX_STRUCT_SIZE 4096
2894
blueswir19f106a72008-10-05 10:52:52 +00002895static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00002896#define IOCTL(cmd, access, ...) \
2897 { TARGET_ ## cmd, cmd, #cmd, access, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00002898#include "ioctls.h"
2899 { 0, 0, },
2900};
2901
pbrook53a59602006-03-25 19:31:22 +00002902/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002903/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002904static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002905{
2906 const IOCTLEntry *ie;
2907 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002908 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002909 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002910 int target_size;
2911 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002912
2913 ie = ioctl_entries;
2914 for(;;) {
2915 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00002916 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00002917 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002918 }
2919 if (ie->target_cmd == cmd)
2920 break;
2921 ie++;
2922 }
2923 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00002924#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00002925 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00002926#endif
bellard31e31b82003-02-18 22:55:36 +00002927 switch(arg_type[0]) {
2928 case TYPE_NULL:
2929 /* no argument */
2930 ret = get_errno(ioctl(fd, ie->host_cmd));
2931 break;
2932 case TYPE_PTRVOID:
2933 case TYPE_INT:
2934 /* int argment */
2935 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2936 break;
2937 case TYPE_PTR:
2938 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002939 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002940 switch(ie->access) {
2941 case IOC_R:
2942 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2943 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002944 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2945 if (!argptr)
2946 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002947 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2948 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002949 }
2950 break;
2951 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002952 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2953 if (!argptr)
2954 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002955 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2956 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002957 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2958 break;
2959 default:
2960 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002961 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2962 if (!argptr)
2963 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002964 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2965 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002966 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2967 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002968 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2969 if (!argptr)
2970 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002971 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2972 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002973 }
2974 break;
2975 }
2976 break;
2977 default:
j_mayer32407102007-09-26 23:01:49 +00002978 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2979 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002980 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002981 break;
2982 }
2983 return ret;
2984}
2985
blueswir1b39bc502008-10-05 10:51:10 +00002986static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00002987 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2988 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2989 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2990 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2991 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2992 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2993 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2994 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2995 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2996 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2997 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2998 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2999 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3000 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3001 { 0, 0, 0, 0 }
3002};
3003
blueswir1b39bc502008-10-05 10:51:10 +00003004static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003005 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3006 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3007 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3008 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3009 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3010 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3011 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3012 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3013 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3014 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3015 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3016 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3017 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3018 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3019 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3020 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3021 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3022 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3023 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3024 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3025 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3026 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3027 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3028 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3029 { 0, 0, 0, 0 }
3030};
3031
blueswir1b39bc502008-10-05 10:51:10 +00003032static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003033 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3034 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3035 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3036 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3037 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3038 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3039 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3040 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3041 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3042 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3043 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3044 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3045 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3046 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3047 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3048 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3049 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3050 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3051 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3052 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3053 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3054 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3055 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3056 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3057 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3058 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3059 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3060 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3061 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3062 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3063 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3064 { 0, 0, 0, 0 }
3065};
3066
blueswir1b39bc502008-10-05 10:51:10 +00003067static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003068 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3069 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3070 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3071 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3072 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3073 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3074 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3075 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3076 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3077 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3078 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3079 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3080 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3081 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3082 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3083 { 0, 0, 0, 0 }
3084};
3085
3086static void target_to_host_termios (void *dst, const void *src)
3087{
3088 struct host_termios *host = dst;
3089 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003090
ths5fafdf22007-09-16 21:08:06 +00003091 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003092 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003093 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003094 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003095 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003096 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003097 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003098 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3099 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003100
Arnaud Patard44607122009-04-21 17:39:08 +03003101 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003102 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3103 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003104 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003105 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003106 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003107 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003108 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003109 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003110 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003111 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3112 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003113 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3114 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3115 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3116 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3117 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003118 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003119}
ths3b46e622007-09-17 08:09:54 +00003120
bellard31e31b82003-02-18 22:55:36 +00003121static void host_to_target_termios (void *dst, const void *src)
3122{
3123 struct target_termios *target = dst;
3124 const struct host_termios *host = src;
3125
ths5fafdf22007-09-16 21:08:06 +00003126 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003127 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003128 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003129 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003130 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003131 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003132 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003133 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3134 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003135
Arnaud Patard44607122009-04-21 17:39:08 +03003136 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003137 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3138 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3139 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3140 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3141 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3142 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3143 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3144 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3145 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3146 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3147 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3148 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3149 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3150 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3151 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3152 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3153 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3154}
3155
blueswir18e853dc2008-10-05 10:49:32 +00003156static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003157 .convert = { host_to_target_termios, target_to_host_termios },
3158 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3159 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3160};
3161
bellard5286db72003-06-05 00:57:30 +00003162static bitmask_transtbl mmap_flags_tbl[] = {
3163 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3164 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3165 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3166 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3167 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3168 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3169 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3170 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3171 { 0, 0, 0, 0 }
3172};
3173
bellard2ab83ea2003-06-15 19:56:46 +00003174#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003175
3176/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003177static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003178
bellard03acab62007-11-11 14:57:14 +00003179static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003180{
3181 int size;
pbrook53a59602006-03-25 19:31:22 +00003182 void *p;
bellard6dbad632003-03-16 18:05:05 +00003183
3184 if (!ldt_table)
3185 return 0;
3186 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3187 if (size > bytecount)
3188 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003189 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3190 if (!p)
bellard03acab62007-11-11 14:57:14 +00003191 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003192 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003193 memcpy(p, ldt_table, size);
3194 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003195 return size;
3196}
3197
3198/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003199static abi_long write_ldt(CPUX86State *env,
3200 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003201{
3202 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003203 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003204 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003205 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003206 uint32_t *lp, entry_1, entry_2;
3207
3208 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003209 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003210 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003211 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003212 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3213 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3214 ldt_info.limit = tswap32(target_ldt_info->limit);
3215 ldt_info.flags = tswap32(target_ldt_info->flags);
3216 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003217
bellard6dbad632003-03-16 18:05:05 +00003218 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003219 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003220 seg_32bit = ldt_info.flags & 1;
3221 contents = (ldt_info.flags >> 1) & 3;
3222 read_exec_only = (ldt_info.flags >> 3) & 1;
3223 limit_in_pages = (ldt_info.flags >> 4) & 1;
3224 seg_not_present = (ldt_info.flags >> 5) & 1;
3225 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003226#ifdef TARGET_ABI32
3227 lm = 0;
3228#else
3229 lm = (ldt_info.flags >> 7) & 1;
3230#endif
bellard6dbad632003-03-16 18:05:05 +00003231 if (contents == 3) {
3232 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003233 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003234 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003235 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003236 }
3237 /* allocate the LDT */
3238 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003239 env->ldt.base = target_mmap(0,
3240 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3241 PROT_READ|PROT_WRITE,
3242 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3243 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003244 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003245 memset(g2h(env->ldt.base), 0,
3246 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003247 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003248 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003249 }
3250
3251 /* NOTE: same code as Linux kernel */
3252 /* Allow LDTs to be cleared by the user. */
3253 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3254 if (oldmode ||
3255 (contents == 0 &&
3256 read_exec_only == 1 &&
3257 seg_32bit == 0 &&
3258 limit_in_pages == 0 &&
3259 seg_not_present == 1 &&
3260 useable == 0 )) {
3261 entry_1 = 0;
3262 entry_2 = 0;
3263 goto install;
3264 }
3265 }
ths3b46e622007-09-17 08:09:54 +00003266
bellard6dbad632003-03-16 18:05:05 +00003267 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3268 (ldt_info.limit & 0x0ffff);
3269 entry_2 = (ldt_info.base_addr & 0xff000000) |
3270 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3271 (ldt_info.limit & 0xf0000) |
3272 ((read_exec_only ^ 1) << 9) |
3273 (contents << 10) |
3274 ((seg_not_present ^ 1) << 15) |
3275 (seg_32bit << 22) |
3276 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00003277 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00003278 0x7000;
3279 if (!oldmode)
3280 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00003281
bellard6dbad632003-03-16 18:05:05 +00003282 /* Install the new entry ... */
3283install:
3284 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3285 lp[0] = tswap32(entry_1);
3286 lp[1] = tswap32(entry_2);
3287 return 0;
3288}
3289
3290/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00003291static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3292 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003293{
bellard03acab62007-11-11 14:57:14 +00003294 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00003295
bellard6dbad632003-03-16 18:05:05 +00003296 switch (func) {
3297 case 0:
3298 ret = read_ldt(ptr, bytecount);
3299 break;
3300 case 1:
3301 ret = write_ldt(env, ptr, bytecount, 1);
3302 break;
3303 case 0x11:
3304 ret = write_ldt(env, ptr, bytecount, 0);
3305 break;
bellard03acab62007-11-11 14:57:14 +00003306 default:
3307 ret = -TARGET_ENOSYS;
3308 break;
bellard6dbad632003-03-16 18:05:05 +00003309 }
3310 return ret;
3311}
bellard1b6b0292003-03-22 17:31:38 +00003312
blueswir14583f582008-08-24 10:35:55 +00003313#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00003314static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003315{
3316 uint64_t *gdt_table = g2h(env->gdt.base);
3317 struct target_modify_ldt_ldt_s ldt_info;
3318 struct target_modify_ldt_ldt_s *target_ldt_info;
3319 int seg_32bit, contents, read_exec_only, limit_in_pages;
3320 int seg_not_present, useable, lm;
3321 uint32_t *lp, entry_1, entry_2;
3322 int i;
3323
3324 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3325 if (!target_ldt_info)
3326 return -TARGET_EFAULT;
3327 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3328 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3329 ldt_info.limit = tswap32(target_ldt_info->limit);
3330 ldt_info.flags = tswap32(target_ldt_info->flags);
3331 if (ldt_info.entry_number == -1) {
3332 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3333 if (gdt_table[i] == 0) {
3334 ldt_info.entry_number = i;
3335 target_ldt_info->entry_number = tswap32(i);
3336 break;
3337 }
3338 }
3339 }
3340 unlock_user_struct(target_ldt_info, ptr, 1);
3341
3342 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3343 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3344 return -TARGET_EINVAL;
3345 seg_32bit = ldt_info.flags & 1;
3346 contents = (ldt_info.flags >> 1) & 3;
3347 read_exec_only = (ldt_info.flags >> 3) & 1;
3348 limit_in_pages = (ldt_info.flags >> 4) & 1;
3349 seg_not_present = (ldt_info.flags >> 5) & 1;
3350 useable = (ldt_info.flags >> 6) & 1;
3351#ifdef TARGET_ABI32
3352 lm = 0;
3353#else
3354 lm = (ldt_info.flags >> 7) & 1;
3355#endif
3356
3357 if (contents == 3) {
3358 if (seg_not_present == 0)
3359 return -TARGET_EINVAL;
3360 }
3361
3362 /* NOTE: same code as Linux kernel */
3363 /* Allow LDTs to be cleared by the user. */
3364 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3365 if ((contents == 0 &&
3366 read_exec_only == 1 &&
3367 seg_32bit == 0 &&
3368 limit_in_pages == 0 &&
3369 seg_not_present == 1 &&
3370 useable == 0 )) {
3371 entry_1 = 0;
3372 entry_2 = 0;
3373 goto install;
3374 }
3375 }
3376
3377 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3378 (ldt_info.limit & 0x0ffff);
3379 entry_2 = (ldt_info.base_addr & 0xff000000) |
3380 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3381 (ldt_info.limit & 0xf0000) |
3382 ((read_exec_only ^ 1) << 9) |
3383 (contents << 10) |
3384 ((seg_not_present ^ 1) << 15) |
3385 (seg_32bit << 22) |
3386 (limit_in_pages << 23) |
3387 (useable << 20) |
3388 (lm << 21) |
3389 0x7000;
3390
3391 /* Install the new entry ... */
3392install:
3393 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3394 lp[0] = tswap32(entry_1);
3395 lp[1] = tswap32(entry_2);
3396 return 0;
3397}
3398
blueswir18fcd3692008-08-17 20:26:25 +00003399static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003400{
3401 struct target_modify_ldt_ldt_s *target_ldt_info;
3402 uint64_t *gdt_table = g2h(env->gdt.base);
3403 uint32_t base_addr, limit, flags;
3404 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3405 int seg_not_present, useable, lm;
3406 uint32_t *lp, entry_1, entry_2;
3407
3408 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3409 if (!target_ldt_info)
3410 return -TARGET_EFAULT;
3411 idx = tswap32(target_ldt_info->entry_number);
3412 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3413 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3414 unlock_user_struct(target_ldt_info, ptr, 1);
3415 return -TARGET_EINVAL;
3416 }
3417 lp = (uint32_t *)(gdt_table + idx);
3418 entry_1 = tswap32(lp[0]);
3419 entry_2 = tswap32(lp[1]);
3420
3421 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3422 contents = (entry_2 >> 10) & 3;
3423 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3424 seg_32bit = (entry_2 >> 22) & 1;
3425 limit_in_pages = (entry_2 >> 23) & 1;
3426 useable = (entry_2 >> 20) & 1;
3427#ifdef TARGET_ABI32
3428 lm = 0;
3429#else
3430 lm = (entry_2 >> 21) & 1;
3431#endif
3432 flags = (seg_32bit << 0) | (contents << 1) |
3433 (read_exec_only << 3) | (limit_in_pages << 4) |
3434 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3435 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3436 base_addr = (entry_1 >> 16) |
3437 (entry_2 & 0xff000000) |
3438 ((entry_2 & 0xff) << 16);
3439 target_ldt_info->base_addr = tswapl(base_addr);
3440 target_ldt_info->limit = tswap32(limit);
3441 target_ldt_info->flags = tswap32(flags);
3442 unlock_user_struct(target_ldt_info, ptr, 1);
3443 return 0;
3444}
blueswir14583f582008-08-24 10:35:55 +00003445#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00003446
bellardd2fd1af2007-11-14 18:08:56 +00003447#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00003448static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00003449{
3450 abi_long ret;
3451 abi_ulong val;
3452 int idx;
3453
3454 switch(code) {
3455 case TARGET_ARCH_SET_GS:
3456 case TARGET_ARCH_SET_FS:
3457 if (code == TARGET_ARCH_SET_GS)
3458 idx = R_GS;
3459 else
3460 idx = R_FS;
3461 cpu_x86_load_seg(env, idx, 0);
3462 env->segs[idx].base = addr;
3463 break;
3464 case TARGET_ARCH_GET_GS:
3465 case TARGET_ARCH_GET_FS:
3466 if (code == TARGET_ARCH_GET_GS)
3467 idx = R_GS;
3468 else
3469 idx = R_FS;
3470 val = env->segs[idx].base;
3471 if (put_user(val, addr, abi_ulong))
3472 return -TARGET_EFAULT;
3473 break;
3474 default:
3475 ret = -TARGET_EINVAL;
3476 break;
3477 }
3478 return 0;
3479}
3480#endif
3481
bellard2ab83ea2003-06-15 19:56:46 +00003482#endif /* defined(TARGET_I386) */
3483
Juan Quintela2f7bb872009-07-27 16:13:24 +02003484#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003485
3486#define NEW_STACK_SIZE PTHREAD_STACK_MIN
3487
3488static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3489typedef struct {
3490 CPUState *env;
3491 pthread_mutex_t mutex;
3492 pthread_cond_t cond;
3493 pthread_t thread;
3494 uint32_t tid;
3495 abi_ulong child_tidptr;
3496 abi_ulong parent_tidptr;
3497 sigset_t sigmask;
3498} new_thread_info;
3499
3500static void *clone_func(void *arg)
3501{
3502 new_thread_info *info = arg;
3503 CPUState *env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003504 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00003505
3506 env = info->env;
3507 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003508 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00003509 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003510 env->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003511 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00003512 if (info->child_tidptr)
3513 put_user_u32(info->tid, info->child_tidptr);
3514 if (info->parent_tidptr)
3515 put_user_u32(info->tid, info->parent_tidptr);
3516 /* Enable signals. */
3517 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3518 /* Signal to the parent that we're ready. */
3519 pthread_mutex_lock(&info->mutex);
3520 pthread_cond_broadcast(&info->cond);
3521 pthread_mutex_unlock(&info->mutex);
3522 /* Wait until the parent has finshed initializing the tls state. */
3523 pthread_mutex_lock(&clone_lock);
3524 pthread_mutex_unlock(&clone_lock);
3525 cpu_loop(env);
3526 /* never exits */
3527 return NULL;
3528}
3529#else
bellard1b6b0292003-03-22 17:31:38 +00003530/* this stack is the equivalent of the kernel stack associated with a
3531 thread/process */
3532#define NEW_STACK_SIZE 8192
3533
3534static int clone_func(void *arg)
3535{
bellard2ab83ea2003-06-15 19:56:46 +00003536 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00003537 cpu_loop(env);
3538 /* never exits */
3539 return 0;
3540}
pbrookd865bab2008-06-07 22:12:17 +00003541#endif
bellard1b6b0292003-03-22 17:31:38 +00003542
ths0da46a62007-10-20 20:23:07 +00003543/* do_fork() Must return host values and target errnos (unlike most
3544 do_*() functions). */
pbrookd865bab2008-06-07 22:12:17 +00003545static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3546 abi_ulong parent_tidptr, target_ulong newtls,
3547 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00003548{
3549 int ret;
bellard5cd43932003-03-29 16:54:36 +00003550 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00003551 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00003552 CPUState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003553#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003554 unsigned int nptl_flags;
3555 sigset_t sigmask;
3556#endif
ths3b46e622007-09-17 08:09:54 +00003557
balrog436d1242008-09-21 02:39:45 +00003558 /* Emulate vfork() with fork() */
3559 if (flags & CLONE_VFORK)
3560 flags &= ~(CLONE_VFORK | CLONE_VM);
3561
bellard1b6b0292003-03-22 17:31:38 +00003562 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003563 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003564#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003565 new_thread_info info;
3566 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00003567#endif
pbrookc3a92832008-06-09 14:02:50 +00003568 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
pbrook624f7972008-05-31 16:11:38 +00003569 init_task_state(ts);
bellard5cd43932003-03-29 16:54:36 +00003570 new_stack = ts->stack;
bellard1b6b0292003-03-22 17:31:38 +00003571 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00003572 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00003573#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3574 cpu_reset(new_env);
3575#endif
pbrook6e68e072008-05-30 17:22:15 +00003576 /* Init regs that differ from the parent. */
3577 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00003578 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003579 ts->bprm = parent_ts->bprm;
3580 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003581#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003582 nptl_flags = flags;
3583 flags &= ~CLONE_NPTL_FLAGS2;
3584
pbrookc2764712009-03-07 15:24:59 +00003585 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3586 ts->child_tidptr = child_tidptr;
3587 }
3588
pbrookd865bab2008-06-07 22:12:17 +00003589 if (nptl_flags & CLONE_SETTLS)
3590 cpu_set_tls (new_env, newtls);
3591
3592 /* Grab a mutex so that thread setup appears atomic. */
3593 pthread_mutex_lock(&clone_lock);
3594
3595 memset(&info, 0, sizeof(info));
3596 pthread_mutex_init(&info.mutex, NULL);
3597 pthread_mutex_lock(&info.mutex);
3598 pthread_cond_init(&info.cond, NULL);
3599 info.env = new_env;
3600 if (nptl_flags & CLONE_CHILD_SETTID)
3601 info.child_tidptr = child_tidptr;
3602 if (nptl_flags & CLONE_PARENT_SETTID)
3603 info.parent_tidptr = parent_tidptr;
3604
3605 ret = pthread_attr_init(&attr);
3606 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
3607 /* It is not safe to deliver signals until the child has finished
3608 initializing, so temporarily block all signals. */
3609 sigfillset(&sigmask);
3610 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3611
3612 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00003613 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00003614
3615 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3616 pthread_attr_destroy(&attr);
3617 if (ret == 0) {
3618 /* Wait for the child to initialize. */
3619 pthread_cond_wait(&info.cond, &info.mutex);
3620 ret = info.tid;
3621 if (flags & CLONE_PARENT_SETTID)
3622 put_user_u32(ret, parent_tidptr);
3623 } else {
3624 ret = -1;
3625 }
3626 pthread_mutex_unlock(&info.mutex);
3627 pthread_cond_destroy(&info.cond);
3628 pthread_mutex_destroy(&info.mutex);
3629 pthread_mutex_unlock(&clone_lock);
3630#else
3631 if (flags & CLONE_NPTL_FLAGS2)
3632 return -EINVAL;
3633 /* This is probably going to die very quickly, but do it anyway. */
bellard27725c12003-04-29 21:08:18 +00003634#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02003635 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00003636#else
3637 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3638#endif
pbrookd865bab2008-06-07 22:12:17 +00003639#endif
bellard1b6b0292003-03-22 17:31:38 +00003640 } else {
3641 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00003642 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00003643 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00003644 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00003645 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00003646 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00003647 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00003648 cpu_clone_regs(env, newsp);
3649 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02003650#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00003651 /* There is a race condition here. The parent process could
3652 theoretically read the TID in the child process before the child
3653 tid is set. This would require using either ptrace
3654 (not implemented) or having *_tidptr to point at a shared memory
3655 mapping. We can't repeat the spinlock hack used above because
3656 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00003657 if (flags & CLONE_CHILD_SETTID)
3658 put_user_u32(gettid(), child_tidptr);
3659 if (flags & CLONE_PARENT_SETTID)
3660 put_user_u32(gettid(), parent_tidptr);
3661 ts = (TaskState *)env->opaque;
3662 if (flags & CLONE_SETTLS)
3663 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00003664 if (flags & CLONE_CHILD_CLEARTID)
3665 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00003666#endif
pbrookd865bab2008-06-07 22:12:17 +00003667 } else {
3668 fork_end(0);
3669 }
bellard1b6b0292003-03-22 17:31:38 +00003670 }
3671 return ret;
3672}
3673
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003674/* warning : doesn't handle linux specific flags... */
3675static int target_to_host_fcntl_cmd(int cmd)
3676{
3677 switch(cmd) {
3678 case TARGET_F_DUPFD:
3679 case TARGET_F_GETFD:
3680 case TARGET_F_SETFD:
3681 case TARGET_F_GETFL:
3682 case TARGET_F_SETFL:
3683 return cmd;
3684 case TARGET_F_GETLK:
3685 return F_GETLK;
3686 case TARGET_F_SETLK:
3687 return F_SETLK;
3688 case TARGET_F_SETLKW:
3689 return F_SETLKW;
3690 case TARGET_F_GETOWN:
3691 return F_GETOWN;
3692 case TARGET_F_SETOWN:
3693 return F_SETOWN;
3694 case TARGET_F_GETSIG:
3695 return F_GETSIG;
3696 case TARGET_F_SETSIG:
3697 return F_SETSIG;
3698#if TARGET_ABI_BITS == 32
3699 case TARGET_F_GETLK64:
3700 return F_GETLK64;
3701 case TARGET_F_SETLK64:
3702 return F_SETLK64;
3703 case TARGET_F_SETLKW64:
3704 return F_SETLKW64;
3705#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003706 case TARGET_F_SETLEASE:
3707 return F_SETLEASE;
3708 case TARGET_F_GETLEASE:
3709 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04003710#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003711 case TARGET_F_DUPFD_CLOEXEC:
3712 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04003713#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003714 case TARGET_F_NOTIFY:
3715 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003716 default:
3717 return -TARGET_EINVAL;
3718 }
3719 return -TARGET_EINVAL;
3720}
3721
blueswir1992f48a2007-10-14 16:27:31 +00003722static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00003723{
3724 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00003725 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00003726 struct flock64 fl64;
3727 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00003728 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003729 int host_cmd = target_to_host_fcntl_cmd(cmd);
3730
3731 if (host_cmd == -TARGET_EINVAL)
3732 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00003733
bellard7775e9e2003-05-14 22:46:48 +00003734 switch(cmd) {
3735 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00003736 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3737 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003738 fl.l_type = tswap16(target_fl->l_type);
3739 fl.l_whence = tswap16(target_fl->l_whence);
3740 fl.l_start = tswapl(target_fl->l_start);
3741 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003742 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00003743 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003744 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003745 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003746 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3747 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003748 target_fl->l_type = tswap16(fl.l_type);
3749 target_fl->l_whence = tswap16(fl.l_whence);
3750 target_fl->l_start = tswapl(fl.l_start);
3751 target_fl->l_len = tswapl(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003752 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00003753 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00003754 }
3755 break;
ths3b46e622007-09-17 08:09:54 +00003756
bellard7775e9e2003-05-14 22:46:48 +00003757 case TARGET_F_SETLK:
3758 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00003759 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3760 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003761 fl.l_type = tswap16(target_fl->l_type);
3762 fl.l_whence = tswap16(target_fl->l_whence);
3763 fl.l_start = tswapl(target_fl->l_start);
3764 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003765 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00003766 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003767 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003768 break;
ths3b46e622007-09-17 08:09:54 +00003769
bellard7775e9e2003-05-14 22:46:48 +00003770 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00003771 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3772 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003773 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3774 fl64.l_whence = tswap16(target_fl64->l_whence);
3775 fl64.l_start = tswapl(target_fl64->l_start);
3776 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003777 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00003778 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003779 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00003780 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003781 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3782 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003783 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3784 target_fl64->l_whence = tswap16(fl64.l_whence);
3785 target_fl64->l_start = tswapl(fl64.l_start);
3786 target_fl64->l_len = tswapl(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003787 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00003788 unlock_user_struct(target_fl64, arg, 1);
3789 }
bellard9ee1fa22007-11-11 15:11:19 +00003790 break;
bellard7775e9e2003-05-14 22:46:48 +00003791 case TARGET_F_SETLK64:
3792 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00003793 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3794 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003795 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3796 fl64.l_whence = tswap16(target_fl64->l_whence);
3797 fl64.l_start = tswapl(target_fl64->l_start);
3798 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003799 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00003800 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003801 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00003802 break;
3803
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003804 case TARGET_F_GETFL:
3805 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00003806 if (ret >= 0) {
3807 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3808 }
bellardffa65c32004-01-04 23:57:22 +00003809 break;
3810
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003811 case TARGET_F_SETFL:
3812 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3813 break;
3814
3815 case TARGET_F_SETOWN:
3816 case TARGET_F_GETOWN:
3817 case TARGET_F_SETSIG:
3818 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003819 case TARGET_F_SETLEASE:
3820 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003821 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00003822 break;
3823
bellard7775e9e2003-05-14 22:46:48 +00003824 default:
bellard9ee1fa22007-11-11 15:11:19 +00003825 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00003826 break;
3827 }
3828 return ret;
3829}
3830
bellard67867302003-11-23 17:05:30 +00003831#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00003832
bellard67867302003-11-23 17:05:30 +00003833static inline int high2lowuid(int uid)
3834{
3835 if (uid > 65535)
3836 return 65534;
3837 else
3838 return uid;
3839}
3840
3841static inline int high2lowgid(int gid)
3842{
3843 if (gid > 65535)
3844 return 65534;
3845 else
3846 return gid;
3847}
3848
3849static inline int low2highuid(int uid)
3850{
3851 if ((int16_t)uid == -1)
3852 return -1;
3853 else
3854 return uid;
3855}
3856
3857static inline int low2highgid(int gid)
3858{
3859 if ((int16_t)gid == -1)
3860 return -1;
3861 else
3862 return gid;
3863}
3864
3865#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00003866
bellard31e31b82003-02-18 22:55:36 +00003867void syscall_init(void)
3868{
bellard2ab83ea2003-06-15 19:56:46 +00003869 IOCTLEntry *ie;
3870 const argtype *arg_type;
3871 int size;
thsb92c47c2007-11-01 00:07:38 +00003872 int i;
bellard2ab83ea2003-06-15 19:56:46 +00003873
Blue Swirl001faf32009-05-13 17:53:17 +00003874#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00003875#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00003876#include "syscall_types.h"
3877#undef STRUCT
3878#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00003879
3880 /* we patch the ioctl size if necessary. We rely on the fact that
3881 no ioctl has all the bits at '1' in the size field */
3882 ie = ioctl_entries;
3883 while (ie->target_cmd != 0) {
3884 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3885 TARGET_IOC_SIZEMASK) {
3886 arg_type = ie->arg_type;
3887 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00003888 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00003889 ie->target_cmd);
3890 exit(1);
3891 }
3892 arg_type++;
3893 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00003894 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00003895 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3896 (size << TARGET_IOC_SIZESHIFT);
3897 }
thsb92c47c2007-11-01 00:07:38 +00003898
3899 /* Build target_to_host_errno_table[] table from
3900 * host_to_target_errno_table[]. */
3901 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3902 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3903
bellard2ab83ea2003-06-15 19:56:46 +00003904 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00003905#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3906 (defined(__x86_64__) && defined(TARGET_X86_64))
3907 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3908 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3909 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00003910 }
3911#endif
3912 ie++;
3913 }
bellard31e31b82003-02-18 22:55:36 +00003914}
bellardc573ff62004-01-04 15:51:36 +00003915
blueswir1992f48a2007-10-14 16:27:31 +00003916#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00003917static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3918{
thsaf325d32008-06-10 15:29:15 +00003919#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00003920 return ((uint64_t)word0 << 32) | word1;
3921#else
3922 return ((uint64_t)word1 << 32) | word0;
3923#endif
3924}
blueswir1992f48a2007-10-14 16:27:31 +00003925#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00003926static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3927{
3928 return word0;
3929}
blueswir1992f48a2007-10-14 16:27:31 +00003930#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00003931
3932#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00003933static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3934 abi_long arg2,
3935 abi_long arg3,
3936 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003937{
3938#ifdef TARGET_ARM
3939 if (((CPUARMState *)cpu_env)->eabi)
3940 {
3941 arg2 = arg3;
3942 arg3 = arg4;
3943 }
3944#endif
3945 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3946}
3947#endif
3948
3949#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00003950static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3951 abi_long arg2,
3952 abi_long arg3,
3953 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003954{
3955#ifdef TARGET_ARM
3956 if (((CPUARMState *)cpu_env)->eabi)
3957 {
3958 arg2 = arg3;
3959 arg3 = arg4;
3960 }
3961#endif
3962 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3963}
3964#endif
3965
bellard579a97f2007-11-11 14:26:47 +00003966static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3967 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00003968{
3969 struct target_timespec *target_ts;
3970
bellard579a97f2007-11-11 14:26:47 +00003971 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3972 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003973 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3974 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3975 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00003976 return 0;
pbrook53a59602006-03-25 19:31:22 +00003977}
3978
bellard579a97f2007-11-11 14:26:47 +00003979static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3980 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00003981{
3982 struct target_timespec *target_ts;
3983
bellard579a97f2007-11-11 14:26:47 +00003984 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3985 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003986 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3987 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3988 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00003989 return 0;
pbrook53a59602006-03-25 19:31:22 +00003990}
3991
aurel329d33b762009-04-08 23:07:05 +00003992#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00003993static inline abi_long host_to_target_stat64(void *cpu_env,
3994 abi_ulong target_addr,
3995 struct stat *host_st)
3996{
3997#ifdef TARGET_ARM
3998 if (((CPUARMState *)cpu_env)->eabi) {
3999 struct target_eabi_stat64 *target_st;
4000
4001 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4002 return -TARGET_EFAULT;
4003 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4004 __put_user(host_st->st_dev, &target_st->st_dev);
4005 __put_user(host_st->st_ino, &target_st->st_ino);
4006#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4007 __put_user(host_st->st_ino, &target_st->__st_ino);
4008#endif
4009 __put_user(host_st->st_mode, &target_st->st_mode);
4010 __put_user(host_st->st_nlink, &target_st->st_nlink);
4011 __put_user(host_st->st_uid, &target_st->st_uid);
4012 __put_user(host_st->st_gid, &target_st->st_gid);
4013 __put_user(host_st->st_rdev, &target_st->st_rdev);
4014 __put_user(host_st->st_size, &target_st->st_size);
4015 __put_user(host_st->st_blksize, &target_st->st_blksize);
4016 __put_user(host_st->st_blocks, &target_st->st_blocks);
4017 __put_user(host_st->st_atime, &target_st->target_st_atime);
4018 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4019 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4020 unlock_user_struct(target_st, target_addr, 1);
4021 } else
4022#endif
4023 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004024#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004025 struct target_stat *target_st;
4026#else
balrog6a24a772008-09-20 02:23:36 +00004027 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004028#endif
balrog6a24a772008-09-20 02:23:36 +00004029
4030 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4031 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004032 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004033 __put_user(host_st->st_dev, &target_st->st_dev);
4034 __put_user(host_st->st_ino, &target_st->st_ino);
4035#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4036 __put_user(host_st->st_ino, &target_st->__st_ino);
4037#endif
4038 __put_user(host_st->st_mode, &target_st->st_mode);
4039 __put_user(host_st->st_nlink, &target_st->st_nlink);
4040 __put_user(host_st->st_uid, &target_st->st_uid);
4041 __put_user(host_st->st_gid, &target_st->st_gid);
4042 __put_user(host_st->st_rdev, &target_st->st_rdev);
4043 /* XXX: better use of kernel struct */
4044 __put_user(host_st->st_size, &target_st->st_size);
4045 __put_user(host_st->st_blksize, &target_st->st_blksize);
4046 __put_user(host_st->st_blocks, &target_st->st_blocks);
4047 __put_user(host_st->st_atime, &target_st->target_st_atime);
4048 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4049 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4050 unlock_user_struct(target_st, target_addr, 1);
4051 }
4052
4053 return 0;
4054}
4055#endif
4056
Juan Quintela2f7bb872009-07-27 16:13:24 +02004057#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004058/* ??? Using host futex calls even when target atomic operations
4059 are not really atomic probably breaks things. However implementing
4060 futexes locally would make futexes shared between multiple processes
4061 tricky. However they're probably useless because guest atomic
4062 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004063static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4064 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004065{
4066 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004067 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004068
4069 /* ??? We assume FUTEX_* constants are the same on both host
4070 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004071#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004072 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004073#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004074 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004075#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004076 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004077 case FUTEX_WAIT:
4078 if (timeout) {
4079 pts = &ts;
4080 target_to_host_timespec(pts, timeout);
4081 } else {
4082 pts = NULL;
4083 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004084 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004085 pts, NULL, 0));
4086 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004087 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004088 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004089 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004090 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004091 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004092 case FUTEX_WAKE_OP:
4093 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4094 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4095 But the prototype takes a `struct timespec *'; insert casts
4096 to satisfy the compiler. We do not need to tswap TIMEOUT
4097 since it's not compared to guest memory. */
4098 pts = (struct timespec *)(uintptr_t) timeout;
4099 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4100 g2h(uaddr2),
4101 (base_op == FUTEX_CMP_REQUEUE
4102 ? tswap32(val3)
4103 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004104 default:
4105 return -TARGET_ENOSYS;
4106 }
4107}
4108#endif
4109
pbrook1d9d8b52009-04-16 15:17:02 +00004110/* Map host to target signal numbers for the wait family of syscalls.
4111 Assume all other status bits are the same. */
4112static int host_to_target_waitstatus(int status)
4113{
4114 if (WIFSIGNALED(status)) {
4115 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4116 }
4117 if (WIFSTOPPED(status)) {
4118 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4119 | (status & 0xff);
4120 }
4121 return status;
4122}
4123
pbrooka745ec62008-05-06 15:36:17 +00004124int get_osversion(void)
4125{
4126 static int osversion;
4127 struct new_utsname buf;
4128 const char *s;
4129 int i, n, tmp;
4130 if (osversion)
4131 return osversion;
4132 if (qemu_uname_release && *qemu_uname_release) {
4133 s = qemu_uname_release;
4134 } else {
4135 if (sys_uname(&buf))
4136 return 0;
4137 s = buf.release;
4138 }
4139 tmp = 0;
4140 for (i = 0; i < 3; i++) {
4141 n = 0;
4142 while (*s >= '0' && *s <= '9') {
4143 n *= 10;
4144 n += *s - '0';
4145 s++;
4146 }
4147 tmp = (tmp << 8) + n;
4148 if (*s == '.')
4149 s++;
4150 }
4151 osversion = tmp;
4152 return osversion;
4153}
4154
ths0da46a62007-10-20 20:23:07 +00004155/* do_syscall() should always have a single exit point at the end so
4156 that actions, such as logging of syscall results, can be performed.
4157 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00004158abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4159 abi_long arg2, abi_long arg3, abi_long arg4,
4160 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00004161{
blueswir1992f48a2007-10-14 16:27:31 +00004162 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004163 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00004164 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00004165 void *p;
ths3b46e622007-09-17 08:09:54 +00004166
bellard72f03902003-02-18 23:33:18 +00004167#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00004168 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00004169#endif
thsb92c47c2007-11-01 00:07:38 +00004170 if(do_strace)
4171 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4172
bellard31e31b82003-02-18 22:55:36 +00004173 switch(num) {
4174 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02004175#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00004176 /* In old applications this may be used to implement _exit(2).
4177 However in threaded applictions it is used for thread termination,
4178 and _exit_group is used for application termination.
4179 Do thread termination if we have more then one thread. */
4180 /* FIXME: This probably breaks if a signal arrives. We should probably
4181 be disabling signals. */
4182 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004183 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00004184 CPUState **lastp;
4185 CPUState *p;
4186
4187 cpu_list_lock();
4188 lastp = &first_cpu;
4189 p = first_cpu;
4190 while (p && p != (CPUState *)cpu_env) {
4191 lastp = &p->next_cpu;
4192 p = p->next_cpu;
4193 }
4194 /* If we didn't find the CPU for this thread then something is
4195 horribly wrong. */
4196 if (!p)
4197 abort();
4198 /* Remove the CPU from the list. */
4199 *lastp = p->next_cpu;
4200 cpu_list_unlock();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004201 ts = ((CPUState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00004202 if (ts->child_tidptr) {
4203 put_user_u32(0, ts->child_tidptr);
4204 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4205 NULL, NULL, 0);
4206 }
4207 /* TODO: Free CPU state. */
4208 pthread_exit(NULL);
4209 }
4210#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02004211#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00004212 _mcleanup();
4213#endif
bellarde9009672005-04-26 20:42:36 +00004214 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00004215 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00004216 ret = 0; /* avoid warning */
4217 break;
4218 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00004219 if (arg3 == 0)
4220 ret = 0;
4221 else {
4222 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4223 goto efault;
4224 ret = get_errno(read(arg1, p, arg3));
4225 unlock_user(p, arg2, ret);
4226 }
bellard31e31b82003-02-18 22:55:36 +00004227 break;
4228 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00004229 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4230 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004231 ret = get_errno(write(arg1, p, arg3));
4232 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00004233 break;
4234 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00004235 if (!(p = lock_user_string(arg1)))
4236 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004237 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00004238 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4239 arg3));
pbrook53a59602006-03-25 19:31:22 +00004240 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004241 break;
ths82424832007-09-24 09:21:55 +00004242#if defined(TARGET_NR_openat) && defined(__NR_openat)
4243 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00004244 if (!(p = lock_user_string(arg2)))
4245 goto efault;
4246 ret = get_errno(sys_openat(arg1,
4247 path(p),
4248 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4249 arg4));
4250 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00004251 break;
4252#endif
bellard31e31b82003-02-18 22:55:36 +00004253 case TARGET_NR_close:
4254 ret = get_errno(close(arg1));
4255 break;
4256 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00004257 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00004258 break;
4259 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00004260 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00004261 break;
thse5febef2007-04-01 18:31:35 +00004262#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00004263 case TARGET_NR_waitpid:
4264 {
pbrook53a59602006-03-25 19:31:22 +00004265 int status;
4266 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00004267 if (!is_error(ret) && arg2
pbrook1d9d8b52009-04-16 15:17:02 +00004268 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00004269 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004270 }
4271 break;
thse5febef2007-04-01 18:31:35 +00004272#endif
pbrookf0cbb612008-05-30 18:20:05 +00004273#ifdef TARGET_NR_waitid
4274 case TARGET_NR_waitid:
4275 {
4276 siginfo_t info;
4277 info.si_pid = 0;
4278 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4279 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004280 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00004281 goto efault;
4282 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05004283 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00004284 }
4285 }
4286 break;
4287#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004288#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004289 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00004290 if (!(p = lock_user_string(arg1)))
4291 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004292 ret = get_errno(creat(p, arg2));
4293 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004294 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004295#endif
bellard31e31b82003-02-18 22:55:36 +00004296 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00004297 {
4298 void * p2;
4299 p = lock_user_string(arg1);
4300 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004301 if (!p || !p2)
4302 ret = -TARGET_EFAULT;
4303 else
4304 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004305 unlock_user(p2, arg2, 0);
4306 unlock_user(p, arg1, 0);
4307 }
bellard31e31b82003-02-18 22:55:36 +00004308 break;
ths64f0ce42007-09-24 09:25:06 +00004309#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4310 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00004311 {
4312 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00004313 if (!arg2 || !arg4)
4314 goto efault;
ths64f0ce42007-09-24 09:25:06 +00004315 p = lock_user_string(arg2);
4316 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004317 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004318 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00004319 else
4320 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00004321 unlock_user(p, arg2, 0);
4322 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00004323 }
4324 break;
4325#endif
bellard31e31b82003-02-18 22:55:36 +00004326 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00004327 if (!(p = lock_user_string(arg1)))
4328 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004329 ret = get_errno(unlink(p));
4330 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004331 break;
ths8170f562007-09-24 09:24:11 +00004332#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4333 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00004334 if (!(p = lock_user_string(arg2)))
4335 goto efault;
4336 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4337 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00004338 break;
balrogb7d35e62007-12-12 00:40:24 +00004339#endif
bellard31e31b82003-02-18 22:55:36 +00004340 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00004341 {
4342 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00004343 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00004344 abi_ulong gp;
4345 abi_ulong guest_argp;
4346 abi_ulong guest_envp;
4347 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00004348 char **q;
4349
bellardf7341ff2003-03-30 21:00:25 +00004350 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00004351 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00004352 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004353 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004354 goto efault;
ths03aa1972007-12-02 06:28:08 +00004355 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004356 break;
bellard7854b052003-03-29 17:22:23 +00004357 argc++;
bellard2f619692007-11-16 10:46:05 +00004358 }
bellardf7341ff2003-03-30 21:00:25 +00004359 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00004360 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00004361 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004362 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004363 goto efault;
ths03aa1972007-12-02 06:28:08 +00004364 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004365 break;
bellard7854b052003-03-29 17:22:23 +00004366 envc++;
bellard2f619692007-11-16 10:46:05 +00004367 }
bellard7854b052003-03-29 17:22:23 +00004368
bellardf7341ff2003-03-30 21:00:25 +00004369 argp = alloca((argc + 1) * sizeof(void *));
4370 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00004371
pbrookda94d262008-05-30 18:24:00 +00004372 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004373 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004374 if (get_user_ual(addr, gp))
4375 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004376 if (!addr)
4377 break;
bellard2f619692007-11-16 10:46:05 +00004378 if (!(*q = lock_user_string(addr)))
4379 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004380 }
bellardf7341ff2003-03-30 21:00:25 +00004381 *q = NULL;
4382
pbrookda94d262008-05-30 18:24:00 +00004383 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004384 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004385 if (get_user_ual(addr, gp))
4386 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004387 if (!addr)
4388 break;
bellard2f619692007-11-16 10:46:05 +00004389 if (!(*q = lock_user_string(addr)))
4390 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004391 }
bellardf7341ff2003-03-30 21:00:25 +00004392 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00004393
bellard2f619692007-11-16 10:46:05 +00004394 if (!(p = lock_user_string(arg1)))
4395 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004396 ret = get_errno(execve(p, argp, envp));
4397 unlock_user(p, arg1, 0);
4398
bellard2f619692007-11-16 10:46:05 +00004399 goto execve_end;
4400
4401 execve_efault:
4402 ret = -TARGET_EFAULT;
4403
4404 execve_end:
pbrook53a59602006-03-25 19:31:22 +00004405 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004406 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004407 if (get_user_ual(addr, gp)
4408 || !addr)
4409 break;
pbrook53a59602006-03-25 19:31:22 +00004410 unlock_user(*q, addr, 0);
4411 }
4412 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004413 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004414 if (get_user_ual(addr, gp)
4415 || !addr)
4416 break;
pbrook53a59602006-03-25 19:31:22 +00004417 unlock_user(*q, addr, 0);
4418 }
bellard7854b052003-03-29 17:22:23 +00004419 }
bellard31e31b82003-02-18 22:55:36 +00004420 break;
4421 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00004422 if (!(p = lock_user_string(arg1)))
4423 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004424 ret = get_errno(chdir(p));
4425 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004426 break;
bellarda315a142005-01-30 22:59:18 +00004427#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00004428 case TARGET_NR_time:
4429 {
pbrook53a59602006-03-25 19:31:22 +00004430 time_t host_time;
4431 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00004432 if (!is_error(ret)
4433 && arg1
4434 && put_user_sal(host_time, arg1))
4435 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004436 }
4437 break;
bellarda315a142005-01-30 22:59:18 +00004438#endif
bellard31e31b82003-02-18 22:55:36 +00004439 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00004440 if (!(p = lock_user_string(arg1)))
4441 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004442 ret = get_errno(mknod(p, arg2, arg3));
4443 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004444 break;
ths75ac37a2007-09-24 09:23:05 +00004445#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4446 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00004447 if (!(p = lock_user_string(arg2)))
4448 goto efault;
4449 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4450 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00004451 break;
4452#endif
bellard31e31b82003-02-18 22:55:36 +00004453 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00004454 if (!(p = lock_user_string(arg1)))
4455 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004456 ret = get_errno(chmod(p, arg2));
4457 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004458 break;
bellardebc05482003-09-30 21:08:41 +00004459#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00004460 case TARGET_NR_break:
4461 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004462#endif
4463#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00004464 case TARGET_NR_oldstat:
4465 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004466#endif
bellard31e31b82003-02-18 22:55:36 +00004467 case TARGET_NR_lseek:
4468 ret = get_errno(lseek(arg1, arg2, arg3));
4469 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004470#ifdef TARGET_NR_getxpid
4471 case TARGET_NR_getxpid:
4472#else
bellard31e31b82003-02-18 22:55:36 +00004473 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00004474#endif
bellard31e31b82003-02-18 22:55:36 +00004475 ret = get_errno(getpid());
4476 break;
4477 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00004478 {
4479 /* need to look at the data field */
4480 void *p2, *p3;
4481 p = lock_user_string(arg1);
4482 p2 = lock_user_string(arg2);
4483 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004484 if (!p || !p2 || !p3)
4485 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004486 else {
bellard579a97f2007-11-11 14:26:47 +00004487 /* FIXME - arg5 should be locked, but it isn't clear how to
4488 * do that since it's not guaranteed to be a NULL-terminated
4489 * string.
4490 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004491 if ( ! arg5 )
4492 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4493 else
4494 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4495 }
bellard579a97f2007-11-11 14:26:47 +00004496 unlock_user(p, arg1, 0);
4497 unlock_user(p2, arg2, 0);
4498 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00004499 break;
4500 }
thse5febef2007-04-01 18:31:35 +00004501#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00004502 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00004503 if (!(p = lock_user_string(arg1)))
4504 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004505 ret = get_errno(umount(p));
4506 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004507 break;
thse5febef2007-04-01 18:31:35 +00004508#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004509#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004510 case TARGET_NR_stime:
4511 {
pbrook53a59602006-03-25 19:31:22 +00004512 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00004513 if (get_user_sal(host_time, arg1))
4514 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004515 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00004516 }
4517 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004518#endif
bellard31e31b82003-02-18 22:55:36 +00004519 case TARGET_NR_ptrace:
4520 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00004521#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004522 case TARGET_NR_alarm:
4523 ret = alarm(arg1);
4524 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004525#endif
bellardebc05482003-09-30 21:08:41 +00004526#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00004527 case TARGET_NR_oldfstat:
4528 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004529#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004530#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004531 case TARGET_NR_pause:
4532 ret = get_errno(pause());
4533 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004534#endif
thse5febef2007-04-01 18:31:35 +00004535#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00004536 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00004537 {
pbrook53a59602006-03-25 19:31:22 +00004538 struct utimbuf tbuf, *host_tbuf;
4539 struct target_utimbuf *target_tbuf;
4540 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004541 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4542 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004543 tbuf.actime = tswapl(target_tbuf->actime);
4544 tbuf.modtime = tswapl(target_tbuf->modtime);
4545 unlock_user_struct(target_tbuf, arg2, 0);
4546 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00004547 } else {
pbrook53a59602006-03-25 19:31:22 +00004548 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00004549 }
bellard579a97f2007-11-11 14:26:47 +00004550 if (!(p = lock_user_string(arg1)))
4551 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004552 ret = get_errno(utime(p, host_tbuf));
4553 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00004554 }
4555 break;
thse5febef2007-04-01 18:31:35 +00004556#endif
bellard978a66f2004-12-06 22:58:05 +00004557 case TARGET_NR_utimes:
4558 {
bellard978a66f2004-12-06 22:58:05 +00004559 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00004560 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00004561 if (copy_from_user_timeval(&tv[0], arg2)
4562 || copy_from_user_timeval(&tv[1],
4563 arg2 + sizeof(struct target_timeval)))
4564 goto efault;
bellard978a66f2004-12-06 22:58:05 +00004565 tvp = tv;
4566 } else {
4567 tvp = NULL;
4568 }
bellard579a97f2007-11-11 14:26:47 +00004569 if (!(p = lock_user_string(arg1)))
4570 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004571 ret = get_errno(utimes(p, tvp));
4572 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00004573 }
4574 break;
balrogac8a6552008-09-20 02:25:39 +00004575#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4576 case TARGET_NR_futimesat:
4577 {
4578 struct timeval *tvp, tv[2];
4579 if (arg3) {
4580 if (copy_from_user_timeval(&tv[0], arg3)
4581 || copy_from_user_timeval(&tv[1],
4582 arg3 + sizeof(struct target_timeval)))
4583 goto efault;
4584 tvp = tv;
4585 } else {
4586 tvp = NULL;
4587 }
4588 if (!(p = lock_user_string(arg2)))
4589 goto efault;
4590 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4591 unlock_user(p, arg2, 0);
4592 }
4593 break;
4594#endif
bellardebc05482003-09-30 21:08:41 +00004595#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00004596 case TARGET_NR_stty:
4597 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004598#endif
4599#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00004600 case TARGET_NR_gtty:
4601 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004602#endif
bellard31e31b82003-02-18 22:55:36 +00004603 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00004604 if (!(p = lock_user_string(arg1)))
4605 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02004606 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00004607 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004608 break;
ths92a34c12007-09-24 09:27:49 +00004609#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4610 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00004611 if (!(p = lock_user_string(arg2)))
4612 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00004613 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00004614 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00004615 break;
4616#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004617#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004618 case TARGET_NR_nice:
4619 ret = get_errno(nice(arg1));
4620 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004621#endif
bellardebc05482003-09-30 21:08:41 +00004622#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00004623 case TARGET_NR_ftime:
4624 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004625#endif
bellard31e31b82003-02-18 22:55:36 +00004626 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00004627 sync();
4628 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00004629 break;
4630 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00004631 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00004632 break;
4633 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00004634 {
4635 void *p2;
4636 p = lock_user_string(arg1);
4637 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004638 if (!p || !p2)
4639 ret = -TARGET_EFAULT;
4640 else
4641 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004642 unlock_user(p2, arg2, 0);
4643 unlock_user(p, arg1, 0);
4644 }
bellard31e31b82003-02-18 22:55:36 +00004645 break;
ths722183f2007-09-24 09:24:37 +00004646#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4647 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00004648 {
bellard579a97f2007-11-11 14:26:47 +00004649 void *p2;
ths722183f2007-09-24 09:24:37 +00004650 p = lock_user_string(arg2);
4651 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004652 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004653 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00004654 else
4655 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00004656 unlock_user(p2, arg4, 0);
4657 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00004658 }
4659 break;
4660#endif
bellard31e31b82003-02-18 22:55:36 +00004661 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00004662 if (!(p = lock_user_string(arg1)))
4663 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004664 ret = get_errno(mkdir(p, arg2));
4665 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004666 break;
ths4472ad02007-09-24 09:22:32 +00004667#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4668 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00004669 if (!(p = lock_user_string(arg2)))
4670 goto efault;
4671 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4672 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00004673 break;
4674#endif
bellard31e31b82003-02-18 22:55:36 +00004675 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00004676 if (!(p = lock_user_string(arg1)))
4677 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004678 ret = get_errno(rmdir(p));
4679 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004680 break;
4681 case TARGET_NR_dup:
4682 ret = get_errno(dup(arg1));
4683 break;
4684 case TARGET_NR_pipe:
Riku Voipio099d6b02009-05-05 12:10:04 +03004685 ret = do_pipe(cpu_env, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004686 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03004687#ifdef TARGET_NR_pipe2
4688 case TARGET_NR_pipe2:
4689 ret = do_pipe(cpu_env, arg1, arg2);
4690 break;
4691#endif
bellard31e31b82003-02-18 22:55:36 +00004692 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00004693 {
pbrook53a59602006-03-25 19:31:22 +00004694 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00004695 struct tms tms;
4696 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00004697 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00004698 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4699 if (!tmsp)
4700 goto efault;
bellardc596ed12003-07-13 17:32:31 +00004701 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4702 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4703 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4704 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00004705 }
bellardc596ed12003-07-13 17:32:31 +00004706 if (!is_error(ret))
4707 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00004708 }
4709 break;
bellardebc05482003-09-30 21:08:41 +00004710#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00004711 case TARGET_NR_prof:
4712 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004713#endif
thse5febef2007-04-01 18:31:35 +00004714#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00004715 case TARGET_NR_signal:
4716 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004717#endif
bellard31e31b82003-02-18 22:55:36 +00004718 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00004719 if (arg1 == 0) {
4720 ret = get_errno(acct(NULL));
4721 } else {
4722 if (!(p = lock_user_string(arg1)))
4723 goto efault;
4724 ret = get_errno(acct(path(p)));
4725 unlock_user(p, arg1, 0);
4726 }
pbrook24836682006-04-16 14:14:53 +00004727 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004728#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004729 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00004730 if (!(p = lock_user_string(arg1)))
4731 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004732 ret = get_errno(umount2(p, arg2));
4733 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004734 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004735#endif
bellardebc05482003-09-30 21:08:41 +00004736#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00004737 case TARGET_NR_lock:
4738 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004739#endif
bellard31e31b82003-02-18 22:55:36 +00004740 case TARGET_NR_ioctl:
4741 ret = do_ioctl(arg1, arg2, arg3);
4742 break;
4743 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00004744 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00004745 break;
bellardebc05482003-09-30 21:08:41 +00004746#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00004747 case TARGET_NR_mpx:
4748 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004749#endif
bellard31e31b82003-02-18 22:55:36 +00004750 case TARGET_NR_setpgid:
4751 ret = get_errno(setpgid(arg1, arg2));
4752 break;
bellardebc05482003-09-30 21:08:41 +00004753#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00004754 case TARGET_NR_ulimit:
4755 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004756#endif
4757#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00004758 case TARGET_NR_oldolduname:
4759 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004760#endif
bellard31e31b82003-02-18 22:55:36 +00004761 case TARGET_NR_umask:
4762 ret = get_errno(umask(arg1));
4763 break;
4764 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00004765 if (!(p = lock_user_string(arg1)))
4766 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004767 ret = get_errno(chroot(p));
4768 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004769 break;
4770 case TARGET_NR_ustat:
4771 goto unimplemented;
4772 case TARGET_NR_dup2:
4773 ret = get_errno(dup2(arg1, arg2));
4774 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03004775#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
4776 case TARGET_NR_dup3:
4777 ret = get_errno(dup3(arg1, arg2, arg3));
4778 break;
4779#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004780#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004781 case TARGET_NR_getppid:
4782 ret = get_errno(getppid());
4783 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004784#endif
bellard31e31b82003-02-18 22:55:36 +00004785 case TARGET_NR_getpgrp:
4786 ret = get_errno(getpgrp());
4787 break;
4788 case TARGET_NR_setsid:
4789 ret = get_errno(setsid());
4790 break;
thse5febef2007-04-01 18:31:35 +00004791#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00004792 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00004793 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08004794#if defined(TARGET_ALPHA)
4795 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00004796 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00004797 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004798 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4799 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004800 act._sa_handler = old_act->_sa_handler;
4801 target_siginitset(&act.sa_mask, old_act->sa_mask);
4802 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08004803 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00004804 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004805 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00004806 }
4807 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00004808 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004809 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4810 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004811 old_act->_sa_handler = oact._sa_handler;
4812 old_act->sa_mask = oact.sa_mask.sig[0];
4813 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00004814 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00004815 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08004816#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00004817 struct target_sigaction act, oact, *pact, *old_act;
4818
4819 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004820 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4821 goto efault;
bellard106ec872006-06-27 21:08:10 +00004822 act._sa_handler = old_act->_sa_handler;
4823 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4824 act.sa_flags = old_act->sa_flags;
4825 unlock_user_struct(old_act, arg2, 0);
4826 pact = &act;
4827 } else {
4828 pact = NULL;
4829 }
4830
4831 ret = get_errno(do_sigaction(arg1, pact, &oact));
4832
4833 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004834 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4835 goto efault;
bellard106ec872006-06-27 21:08:10 +00004836 old_act->_sa_handler = oact._sa_handler;
4837 old_act->sa_flags = oact.sa_flags;
4838 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4839 old_act->sa_mask.sig[1] = 0;
4840 old_act->sa_mask.sig[2] = 0;
4841 old_act->sa_mask.sig[3] = 0;
4842 unlock_user_struct(old_act, arg3, 1);
4843 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08004844#else
4845 struct target_old_sigaction *old_act;
4846 struct target_sigaction act, oact, *pact;
4847 if (arg2) {
4848 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4849 goto efault;
4850 act._sa_handler = old_act->_sa_handler;
4851 target_siginitset(&act.sa_mask, old_act->sa_mask);
4852 act.sa_flags = old_act->sa_flags;
4853 act.sa_restorer = old_act->sa_restorer;
4854 unlock_user_struct(old_act, arg2, 0);
4855 pact = &act;
4856 } else {
4857 pact = NULL;
4858 }
4859 ret = get_errno(do_sigaction(arg1, pact, &oact));
4860 if (!is_error(ret) && arg3) {
4861 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4862 goto efault;
4863 old_act->_sa_handler = oact._sa_handler;
4864 old_act->sa_mask = oact.sa_mask.sig[0];
4865 old_act->sa_flags = oact.sa_flags;
4866 old_act->sa_restorer = oact.sa_restorer;
4867 unlock_user_struct(old_act, arg3, 1);
4868 }
ths388bb212007-05-13 13:58:00 +00004869#endif
bellard31e31b82003-02-18 22:55:36 +00004870 }
4871 break;
thse5febef2007-04-01 18:31:35 +00004872#endif
bellard66fb9762003-03-23 01:06:05 +00004873 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00004874 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08004875#if defined(TARGET_ALPHA)
4876 struct target_sigaction act, oact, *pact = 0;
4877 struct target_rt_sigaction *rt_act;
4878 /* ??? arg4 == sizeof(sigset_t). */
4879 if (arg2) {
4880 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
4881 goto efault;
4882 act._sa_handler = rt_act->_sa_handler;
4883 act.sa_mask = rt_act->sa_mask;
4884 act.sa_flags = rt_act->sa_flags;
4885 act.sa_restorer = arg5;
4886 unlock_user_struct(rt_act, arg2, 0);
4887 pact = &act;
4888 }
4889 ret = get_errno(do_sigaction(arg1, pact, &oact));
4890 if (!is_error(ret) && arg3) {
4891 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
4892 goto efault;
4893 rt_act->_sa_handler = oact._sa_handler;
4894 rt_act->sa_mask = oact.sa_mask;
4895 rt_act->sa_flags = oact.sa_flags;
4896 unlock_user_struct(rt_act, arg3, 1);
4897 }
4898#else
pbrook53a59602006-03-25 19:31:22 +00004899 struct target_sigaction *act;
4900 struct target_sigaction *oact;
4901
bellard579a97f2007-11-11 14:26:47 +00004902 if (arg2) {
4903 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4904 goto efault;
4905 } else
pbrook53a59602006-03-25 19:31:22 +00004906 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00004907 if (arg3) {
4908 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4909 ret = -TARGET_EFAULT;
4910 goto rt_sigaction_fail;
4911 }
4912 } else
pbrook53a59602006-03-25 19:31:22 +00004913 oact = NULL;
4914 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00004915 rt_sigaction_fail:
4916 if (act)
pbrook53a59602006-03-25 19:31:22 +00004917 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00004918 if (oact)
pbrook53a59602006-03-25 19:31:22 +00004919 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08004920#endif
pbrook53a59602006-03-25 19:31:22 +00004921 }
bellard66fb9762003-03-23 01:06:05 +00004922 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004923#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004924 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00004925 {
4926 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00004927 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00004928 sigprocmask(0, NULL, &cur_set);
4929 host_to_target_old_sigset(&target_set, &cur_set);
4930 ret = target_set;
4931 }
4932 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004933#endif
4934#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004935 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00004936 {
4937 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00004938 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00004939 sigprocmask(0, NULL, &cur_set);
4940 target_to_host_old_sigset(&set, &target_set);
4941 sigorset(&set, &set, &cur_set);
4942 sigprocmask(SIG_SETMASK, &set, &oset);
4943 host_to_target_old_sigset(&target_set, &oset);
4944 ret = target_set;
4945 }
4946 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004947#endif
thse5febef2007-04-01 18:31:35 +00004948#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00004949 case TARGET_NR_sigprocmask:
4950 {
4951 int how = arg1;
4952 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00004953
pbrook53a59602006-03-25 19:31:22 +00004954 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004955 switch(how) {
4956 case TARGET_SIG_BLOCK:
4957 how = SIG_BLOCK;
4958 break;
4959 case TARGET_SIG_UNBLOCK:
4960 how = SIG_UNBLOCK;
4961 break;
4962 case TARGET_SIG_SETMASK:
4963 how = SIG_SETMASK;
4964 break;
4965 default:
ths0da46a62007-10-20 20:23:07 +00004966 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00004967 goto fail;
4968 }
Anthony Liguoric227f092009-10-01 16:12:16 -05004969 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00004970 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004971 target_to_host_old_sigset(&set, p);
4972 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004973 set_ptr = &set;
4974 } else {
4975 how = 0;
4976 set_ptr = NULL;
4977 }
4978 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00004979 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004980 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00004981 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004982 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05004983 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004984 }
4985 }
4986 break;
thse5febef2007-04-01 18:31:35 +00004987#endif
bellard66fb9762003-03-23 01:06:05 +00004988 case TARGET_NR_rt_sigprocmask:
4989 {
4990 int how = arg1;
4991 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00004992
pbrook53a59602006-03-25 19:31:22 +00004993 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004994 switch(how) {
4995 case TARGET_SIG_BLOCK:
4996 how = SIG_BLOCK;
4997 break;
4998 case TARGET_SIG_UNBLOCK:
4999 how = SIG_UNBLOCK;
5000 break;
5001 case TARGET_SIG_SETMASK:
5002 how = SIG_SETMASK;
5003 break;
5004 default:
ths0da46a62007-10-20 20:23:07 +00005005 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005006 goto fail;
5007 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005008 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005009 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005010 target_to_host_sigset(&set, p);
5011 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005012 set_ptr = &set;
5013 } else {
5014 how = 0;
5015 set_ptr = NULL;
5016 }
5017 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005018 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005019 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005020 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005021 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005022 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005023 }
5024 }
5025 break;
thse5febef2007-04-01 18:31:35 +00005026#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005027 case TARGET_NR_sigpending:
5028 {
5029 sigset_t set;
5030 ret = get_errno(sigpending(&set));
5031 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005032 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005033 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005034 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005035 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005036 }
5037 }
5038 break;
thse5febef2007-04-01 18:31:35 +00005039#endif
bellard66fb9762003-03-23 01:06:05 +00005040 case TARGET_NR_rt_sigpending:
5041 {
5042 sigset_t set;
5043 ret = get_errno(sigpending(&set));
5044 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005045 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005046 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005047 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005048 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005049 }
5050 }
5051 break;
thse5febef2007-04-01 18:31:35 +00005052#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00005053 case TARGET_NR_sigsuspend:
5054 {
5055 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005056 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005057 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005058 target_to_host_old_sigset(&set, p);
5059 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005060 ret = get_errno(sigsuspend(&set));
5061 }
5062 break;
thse5febef2007-04-01 18:31:35 +00005063#endif
bellard66fb9762003-03-23 01:06:05 +00005064 case TARGET_NR_rt_sigsuspend:
5065 {
5066 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005067 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005068 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005069 target_to_host_sigset(&set, p);
5070 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005071 ret = get_errno(sigsuspend(&set));
5072 }
5073 break;
5074 case TARGET_NR_rt_sigtimedwait:
5075 {
bellard66fb9762003-03-23 01:06:05 +00005076 sigset_t set;
5077 struct timespec uts, *puts;
5078 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00005079
Anthony Liguoric227f092009-10-01 16:12:16 -05005080 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005081 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005082 target_to_host_sigset(&set, p);
5083 unlock_user(p, arg1, 0);
5084 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00005085 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00005086 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00005087 } else {
5088 puts = NULL;
5089 }
5090 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00005091 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005092 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005093 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005094 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05005095 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00005096 }
5097 }
5098 break;
5099 case TARGET_NR_rt_sigqueueinfo:
5100 {
5101 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05005102 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005103 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005104 target_to_host_siginfo(&uinfo, p);
5105 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005106 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5107 }
5108 break;
thse5febef2007-04-01 18:31:35 +00005109#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00005110 case TARGET_NR_sigreturn:
5111 /* NOTE: ret is eax, so not transcoding must be done */
5112 ret = do_sigreturn(cpu_env);
5113 break;
thse5febef2007-04-01 18:31:35 +00005114#endif
bellard66fb9762003-03-23 01:06:05 +00005115 case TARGET_NR_rt_sigreturn:
5116 /* NOTE: ret is eax, so not transcoding must be done */
5117 ret = do_rt_sigreturn(cpu_env);
5118 break;
bellard31e31b82003-02-18 22:55:36 +00005119 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00005120 if (!(p = lock_user_string(arg1)))
5121 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005122 ret = get_errno(sethostname(p, arg2));
5123 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005124 break;
5125 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00005126 {
5127 /* XXX: convert resource ? */
5128 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005129 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005130 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00005131 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5132 goto efault;
bellard9de5e442003-03-23 16:49:39 +00005133 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
5134 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005135 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00005136 ret = get_errno(setrlimit(resource, &rlim));
5137 }
5138 break;
bellard31e31b82003-02-18 22:55:36 +00005139 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00005140 {
5141 /* XXX: convert resource ? */
5142 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005143 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005144 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00005145
bellard9de5e442003-03-23 16:49:39 +00005146 ret = get_errno(getrlimit(resource, &rlim));
5147 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005148 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5149 goto efault;
Ulrich Hecht666bcd92009-07-03 17:09:30 +02005150 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
5151 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005152 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00005153 }
5154 }
5155 break;
bellard31e31b82003-02-18 22:55:36 +00005156 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00005157 {
5158 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00005159 ret = get_errno(getrusage(arg1, &rusage));
5160 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005161 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00005162 }
5163 }
5164 break;
bellard31e31b82003-02-18 22:55:36 +00005165 case TARGET_NR_gettimeofday:
5166 {
bellard31e31b82003-02-18 22:55:36 +00005167 struct timeval tv;
5168 ret = get_errno(gettimeofday(&tv, NULL));
5169 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00005170 if (copy_to_user_timeval(arg1, &tv))
5171 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005172 }
5173 }
5174 break;
5175 case TARGET_NR_settimeofday:
5176 {
bellard31e31b82003-02-18 22:55:36 +00005177 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00005178 if (copy_from_user_timeval(&tv, arg1))
5179 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005180 ret = get_errno(settimeofday(&tv, NULL));
5181 }
5182 break;
bellard048f6b42005-11-26 18:47:20 +00005183#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00005184 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00005185 {
pbrook53a59602006-03-25 19:31:22 +00005186 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00005187 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00005188 long nsel;
5189
bellard579a97f2007-11-11 14:26:47 +00005190 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5191 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005192 nsel = tswapl(sel->n);
5193 inp = tswapl(sel->inp);
5194 outp = tswapl(sel->outp);
5195 exp = tswapl(sel->exp);
5196 tvp = tswapl(sel->tvp);
5197 unlock_user_struct(sel, arg1, 0);
5198 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00005199 }
5200 break;
bellard048f6b42005-11-26 18:47:20 +00005201#endif
bellard31e31b82003-02-18 22:55:36 +00005202 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00005203 {
5204 void *p2;
5205 p = lock_user_string(arg1);
5206 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005207 if (!p || !p2)
5208 ret = -TARGET_EFAULT;
5209 else
5210 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005211 unlock_user(p2, arg2, 0);
5212 unlock_user(p, arg1, 0);
5213 }
bellard31e31b82003-02-18 22:55:36 +00005214 break;
thsf0b62432007-09-24 09:25:40 +00005215#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5216 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00005217 {
bellard579a97f2007-11-11 14:26:47 +00005218 void *p2;
thsf0b62432007-09-24 09:25:40 +00005219 p = lock_user_string(arg1);
5220 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005221 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005222 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00005223 else
5224 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00005225 unlock_user(p2, arg3, 0);
5226 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00005227 }
5228 break;
5229#endif
bellardebc05482003-09-30 21:08:41 +00005230#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00005231 case TARGET_NR_oldlstat:
5232 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005233#endif
bellard31e31b82003-02-18 22:55:36 +00005234 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00005235 {
aurel32d088d662009-01-30 20:09:01 +00005236 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00005237 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00005238 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5239 if (!p || !p2)
5240 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00005241 else {
5242 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5243 char real[PATH_MAX];
5244 temp = realpath(exec_path,real);
5245 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5246 snprintf((char *)p2, arg3, "%s", real);
5247 }
5248 else
5249 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00005250 }
pbrook53a59602006-03-25 19:31:22 +00005251 unlock_user(p2, arg2, ret);
5252 unlock_user(p, arg1, 0);
5253 }
bellard31e31b82003-02-18 22:55:36 +00005254 break;
ths5e0ccb12007-09-24 09:26:10 +00005255#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5256 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00005257 {
bellard579a97f2007-11-11 14:26:47 +00005258 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00005259 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005260 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5261 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005262 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00005263 else
5264 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00005265 unlock_user(p2, arg3, ret);
5266 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00005267 }
5268 break;
5269#endif
thse5febef2007-04-01 18:31:35 +00005270#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00005271 case TARGET_NR_uselib:
5272 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005273#endif
5274#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00005275 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00005276 if (!(p = lock_user_string(arg1)))
5277 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005278 ret = get_errno(swapon(p, arg2));
5279 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005280 break;
thse5febef2007-04-01 18:31:35 +00005281#endif
bellard31e31b82003-02-18 22:55:36 +00005282 case TARGET_NR_reboot:
5283 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005284#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00005285 case TARGET_NR_readdir:
5286 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005287#endif
5288#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00005289 case TARGET_NR_mmap:
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02005290#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 +00005291 {
blueswir1992f48a2007-10-14 16:27:31 +00005292 abi_ulong *v;
5293 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00005294 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5295 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005296 v1 = tswapl(v[0]);
5297 v2 = tswapl(v[1]);
5298 v3 = tswapl(v[2]);
5299 v4 = tswapl(v[3]);
5300 v5 = tswapl(v[4]);
5301 v6 = tswapl(v[5]);
5302 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00005303 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00005304 target_to_host_bitmask(v4, mmap_flags_tbl),
5305 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00005306 }
bellard31e31b82003-02-18 22:55:36 +00005307#else
ths5fafdf22007-09-16 21:08:06 +00005308 ret = get_errno(target_mmap(arg1, arg2, arg3,
5309 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00005310 arg5,
5311 arg6));
bellard31e31b82003-02-18 22:55:36 +00005312#endif
bellard6fb883e2003-07-09 17:12:39 +00005313 break;
thse5febef2007-04-01 18:31:35 +00005314#endif
bellarda315a142005-01-30 22:59:18 +00005315#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00005316 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00005317#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00005318#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00005319#endif
ths5fafdf22007-09-16 21:08:06 +00005320 ret = get_errno(target_mmap(arg1, arg2, arg3,
5321 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00005322 arg5,
bellardc573ff62004-01-04 15:51:36 +00005323 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00005324 break;
bellarda315a142005-01-30 22:59:18 +00005325#endif
bellard31e31b82003-02-18 22:55:36 +00005326 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00005327 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005328 break;
bellard9de5e442003-03-23 16:49:39 +00005329 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00005330 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005331 break;
thse5febef2007-04-01 18:31:35 +00005332#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00005333 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00005334 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00005335 break;
thse5febef2007-04-01 18:31:35 +00005336#endif
pbrook53a59602006-03-25 19:31:22 +00005337 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00005338#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00005339 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00005340 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005341 break;
thse5febef2007-04-01 18:31:35 +00005342#endif
5343#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00005344 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00005345 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005346 break;
thse5febef2007-04-01 18:31:35 +00005347#endif
5348#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00005349 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00005350 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005351 break;
thse5febef2007-04-01 18:31:35 +00005352#endif
5353#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00005354 case TARGET_NR_mlockall:
5355 ret = get_errno(mlockall(arg1));
5356 break;
thse5febef2007-04-01 18:31:35 +00005357#endif
5358#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00005359 case TARGET_NR_munlockall:
5360 ret = get_errno(munlockall());
5361 break;
thse5febef2007-04-01 18:31:35 +00005362#endif
bellard31e31b82003-02-18 22:55:36 +00005363 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00005364 if (!(p = lock_user_string(arg1)))
5365 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005366 ret = get_errno(truncate(p, arg2));
5367 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005368 break;
5369 case TARGET_NR_ftruncate:
5370 ret = get_errno(ftruncate(arg1, arg2));
5371 break;
5372 case TARGET_NR_fchmod:
5373 ret = get_errno(fchmod(arg1, arg2));
5374 break;
ths814d7972007-09-24 09:26:51 +00005375#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5376 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00005377 if (!(p = lock_user_string(arg2)))
5378 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005379 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005380 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00005381 break;
5382#endif
bellard31e31b82003-02-18 22:55:36 +00005383 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00005384 /* libc does special remapping of the return value of
5385 * sys_getpriority() so it's just easiest to call
5386 * sys_getpriority() directly rather than through libc. */
Ulrich Hecht69137202009-09-17 21:08:37 +03005387 ret = get_errno(sys_getpriority(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005388 break;
5389 case TARGET_NR_setpriority:
5390 ret = get_errno(setpriority(arg1, arg2, arg3));
5391 break;
bellardebc05482003-09-30 21:08:41 +00005392#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00005393 case TARGET_NR_profil:
5394 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005395#endif
bellard31e31b82003-02-18 22:55:36 +00005396 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00005397 if (!(p = lock_user_string(arg1)))
5398 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005399 ret = get_errno(statfs(path(p), &stfs));
5400 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005401 convert_statfs:
5402 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005403 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005404
bellard579a97f2007-11-11 14:26:47 +00005405 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5406 goto efault;
5407 __put_user(stfs.f_type, &target_stfs->f_type);
5408 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5409 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5410 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5411 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5412 __put_user(stfs.f_files, &target_stfs->f_files);
5413 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5414 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5415 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5416 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00005417 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005418 }
5419 break;
5420 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00005421 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00005422 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00005423#ifdef TARGET_NR_statfs64
5424 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00005425 if (!(p = lock_user_string(arg1)))
5426 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005427 ret = get_errno(statfs(path(p), &stfs));
5428 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00005429 convert_statfs64:
5430 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005431 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005432
bellard579a97f2007-11-11 14:26:47 +00005433 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5434 goto efault;
5435 __put_user(stfs.f_type, &target_stfs->f_type);
5436 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5437 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5438 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5439 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5440 __put_user(stfs.f_files, &target_stfs->f_files);
5441 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5442 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5443 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5444 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5445 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00005446 }
5447 break;
5448 case TARGET_NR_fstatfs64:
5449 ret = get_errno(fstatfs(arg1, &stfs));
5450 goto convert_statfs64;
5451#endif
bellardebc05482003-09-30 21:08:41 +00005452#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00005453 case TARGET_NR_ioperm:
5454 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005455#endif
thse5febef2007-04-01 18:31:35 +00005456#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00005457 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00005458 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00005459 break;
thse5febef2007-04-01 18:31:35 +00005460#endif
bellard3532fa72006-06-24 15:06:03 +00005461#ifdef TARGET_NR_accept
5462 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00005463 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005464 break;
5465#endif
5466#ifdef TARGET_NR_bind
5467 case TARGET_NR_bind:
5468 ret = do_bind(arg1, arg2, arg3);
5469 break;
5470#endif
5471#ifdef TARGET_NR_connect
5472 case TARGET_NR_connect:
5473 ret = do_connect(arg1, arg2, arg3);
5474 break;
5475#endif
5476#ifdef TARGET_NR_getpeername
5477 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00005478 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005479 break;
5480#endif
5481#ifdef TARGET_NR_getsockname
5482 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00005483 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005484 break;
5485#endif
5486#ifdef TARGET_NR_getsockopt
5487 case TARGET_NR_getsockopt:
5488 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5489 break;
5490#endif
5491#ifdef TARGET_NR_listen
5492 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00005493 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005494 break;
5495#endif
5496#ifdef TARGET_NR_recv
5497 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00005498 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005499 break;
5500#endif
5501#ifdef TARGET_NR_recvfrom
5502 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00005503 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005504 break;
5505#endif
5506#ifdef TARGET_NR_recvmsg
5507 case TARGET_NR_recvmsg:
5508 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5509 break;
5510#endif
5511#ifdef TARGET_NR_send
5512 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00005513 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005514 break;
5515#endif
5516#ifdef TARGET_NR_sendmsg
5517 case TARGET_NR_sendmsg:
5518 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5519 break;
5520#endif
5521#ifdef TARGET_NR_sendto
5522 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00005523 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005524 break;
5525#endif
5526#ifdef TARGET_NR_shutdown
5527 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00005528 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005529 break;
5530#endif
5531#ifdef TARGET_NR_socket
5532 case TARGET_NR_socket:
5533 ret = do_socket(arg1, arg2, arg3);
5534 break;
5535#endif
5536#ifdef TARGET_NR_socketpair
5537 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00005538 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00005539 break;
5540#endif
5541#ifdef TARGET_NR_setsockopt
5542 case TARGET_NR_setsockopt:
5543 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5544 break;
5545#endif
ths7494b0f2007-02-11 18:26:53 +00005546
bellard31e31b82003-02-18 22:55:36 +00005547 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00005548 if (!(p = lock_user_string(arg2)))
5549 goto efault;
thse5574482007-02-11 20:03:13 +00005550 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5551 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00005552 break;
5553
bellard31e31b82003-02-18 22:55:36 +00005554 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00005555 {
bellard66fb9762003-03-23 01:06:05 +00005556 struct itimerval value, ovalue, *pvalue;
5557
pbrook53a59602006-03-25 19:31:22 +00005558 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005559 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00005560 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5561 || copy_from_user_timeval(&pvalue->it_value,
5562 arg2 + sizeof(struct target_timeval)))
5563 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005564 } else {
5565 pvalue = NULL;
5566 }
5567 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00005568 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00005569 if (copy_to_user_timeval(arg3,
5570 &ovalue.it_interval)
5571 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5572 &ovalue.it_value))
5573 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005574 }
5575 }
5576 break;
bellard31e31b82003-02-18 22:55:36 +00005577 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00005578 {
bellard66fb9762003-03-23 01:06:05 +00005579 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00005580
bellard66fb9762003-03-23 01:06:05 +00005581 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00005582 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00005583 if (copy_to_user_timeval(arg2,
5584 &value.it_interval)
5585 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5586 &value.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_stat:
bellard579a97f2007-11-11 14:26:47 +00005592 if (!(p = lock_user_string(arg1)))
5593 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005594 ret = get_errno(stat(path(p), &st));
5595 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005596 goto do_stat;
5597 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00005598 if (!(p = lock_user_string(arg1)))
5599 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005600 ret = get_errno(lstat(path(p), &st));
5601 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005602 goto do_stat;
5603 case TARGET_NR_fstat:
5604 {
5605 ret = get_errno(fstat(arg1, &st));
5606 do_stat:
5607 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005608 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00005609
bellard579a97f2007-11-11 14:26:47 +00005610 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5611 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02005612 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00005613 __put_user(st.st_dev, &target_st->st_dev);
5614 __put_user(st.st_ino, &target_st->st_ino);
5615 __put_user(st.st_mode, &target_st->st_mode);
5616 __put_user(st.st_uid, &target_st->st_uid);
5617 __put_user(st.st_gid, &target_st->st_gid);
5618 __put_user(st.st_nlink, &target_st->st_nlink);
5619 __put_user(st.st_rdev, &target_st->st_rdev);
5620 __put_user(st.st_size, &target_st->st_size);
5621 __put_user(st.st_blksize, &target_st->st_blksize);
5622 __put_user(st.st_blocks, &target_st->st_blocks);
5623 __put_user(st.st_atime, &target_st->target_st_atime);
5624 __put_user(st.st_mtime, &target_st->target_st_mtime);
5625 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00005626 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005627 }
5628 }
5629 break;
bellardebc05482003-09-30 21:08:41 +00005630#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00005631 case TARGET_NR_olduname:
5632 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005633#endif
5634#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00005635 case TARGET_NR_iopl:
5636 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005637#endif
bellard31e31b82003-02-18 22:55:36 +00005638 case TARGET_NR_vhangup:
5639 ret = get_errno(vhangup());
5640 break;
bellardebc05482003-09-30 21:08:41 +00005641#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00005642 case TARGET_NR_idle:
5643 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005644#endif
bellard42ad6ae2005-01-03 22:48:11 +00005645#ifdef TARGET_NR_syscall
5646 case TARGET_NR_syscall:
5647 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5648 break;
5649#endif
bellard31e31b82003-02-18 22:55:36 +00005650 case TARGET_NR_wait4:
5651 {
5652 int status;
blueswir1992f48a2007-10-14 16:27:31 +00005653 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00005654 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00005655 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00005656 if (target_rusage)
5657 rusage_ptr = &rusage;
5658 else
5659 rusage_ptr = NULL;
5660 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5661 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005662 if (status_ptr) {
pbrook1d9d8b52009-04-16 15:17:02 +00005663 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00005664 if (put_user_s32(status, status_ptr))
5665 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005666 }
bellard2f619692007-11-16 10:46:05 +00005667 if (target_rusage)
5668 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00005669 }
5670 }
5671 break;
thse5febef2007-04-01 18:31:35 +00005672#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00005673 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00005674 if (!(p = lock_user_string(arg1)))
5675 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005676 ret = get_errno(swapoff(p));
5677 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005678 break;
thse5febef2007-04-01 18:31:35 +00005679#endif
bellard31e31b82003-02-18 22:55:36 +00005680 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00005681 {
pbrook53a59602006-03-25 19:31:22 +00005682 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00005683 struct sysinfo value;
5684 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00005685 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00005686 {
bellard579a97f2007-11-11 14:26:47 +00005687 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5688 goto efault;
bellarda5448a72004-06-19 16:59:03 +00005689 __put_user(value.uptime, &target_value->uptime);
5690 __put_user(value.loads[0], &target_value->loads[0]);
5691 __put_user(value.loads[1], &target_value->loads[1]);
5692 __put_user(value.loads[2], &target_value->loads[2]);
5693 __put_user(value.totalram, &target_value->totalram);
5694 __put_user(value.freeram, &target_value->freeram);
5695 __put_user(value.sharedram, &target_value->sharedram);
5696 __put_user(value.bufferram, &target_value->bufferram);
5697 __put_user(value.totalswap, &target_value->totalswap);
5698 __put_user(value.freeswap, &target_value->freeswap);
5699 __put_user(value.procs, &target_value->procs);
5700 __put_user(value.totalhigh, &target_value->totalhigh);
5701 __put_user(value.freehigh, &target_value->freehigh);
5702 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00005703 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00005704 }
5705 }
5706 break;
thse5febef2007-04-01 18:31:35 +00005707#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00005708 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00005709 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5710 break;
thse5febef2007-04-01 18:31:35 +00005711#endif
aurel32e5289082009-04-18 16:16:12 +00005712#ifdef TARGET_NR_semget
5713 case TARGET_NR_semget:
5714 ret = get_errno(semget(arg1, arg2, arg3));
5715 break;
5716#endif
5717#ifdef TARGET_NR_semop
5718 case TARGET_NR_semop:
5719 ret = get_errno(do_semop(arg1, arg2, arg3));
5720 break;
5721#endif
5722#ifdef TARGET_NR_semctl
5723 case TARGET_NR_semctl:
5724 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5725 break;
5726#endif
aurel32eeb438c2008-10-13 21:08:55 +00005727#ifdef TARGET_NR_msgctl
5728 case TARGET_NR_msgctl:
5729 ret = do_msgctl(arg1, arg2, arg3);
5730 break;
5731#endif
5732#ifdef TARGET_NR_msgget
5733 case TARGET_NR_msgget:
5734 ret = get_errno(msgget(arg1, arg2));
5735 break;
5736#endif
5737#ifdef TARGET_NR_msgrcv
5738 case TARGET_NR_msgrcv:
5739 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5740 break;
5741#endif
5742#ifdef TARGET_NR_msgsnd
5743 case TARGET_NR_msgsnd:
5744 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5745 break;
5746#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03005747#ifdef TARGET_NR_shmget
5748 case TARGET_NR_shmget:
5749 ret = get_errno(shmget(arg1, arg2, arg3));
5750 break;
5751#endif
5752#ifdef TARGET_NR_shmctl
5753 case TARGET_NR_shmctl:
5754 ret = do_shmctl(arg1, arg2, arg3);
5755 break;
5756#endif
5757#ifdef TARGET_NR_shmat
5758 case TARGET_NR_shmat:
5759 ret = do_shmat(arg1, arg2, arg3);
5760 break;
5761#endif
5762#ifdef TARGET_NR_shmdt
5763 case TARGET_NR_shmdt:
5764 ret = do_shmdt(arg1);
5765 break;
5766#endif
bellard31e31b82003-02-18 22:55:36 +00005767 case TARGET_NR_fsync:
5768 ret = get_errno(fsync(arg1));
5769 break;
bellard31e31b82003-02-18 22:55:36 +00005770 case TARGET_NR_clone:
aurel320b6d3ae2008-09-15 07:43:43 +00005771#if defined(TARGET_SH4)
5772 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00005773#elif defined(TARGET_CRIS)
5774 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005775#else
pbrookd865bab2008-06-07 22:12:17 +00005776 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005777#endif
bellard1b6b0292003-03-22 17:31:38 +00005778 break;
bellardec86b0f2003-04-11 00:15:04 +00005779#ifdef __NR_exit_group
5780 /* new thread calls */
5781 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02005782#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00005783 _mcleanup();
5784#endif
bellarde9009672005-04-26 20:42:36 +00005785 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00005786 ret = get_errno(exit_group(arg1));
5787 break;
5788#endif
bellard31e31b82003-02-18 22:55:36 +00005789 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00005790 if (!(p = lock_user_string(arg1)))
5791 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005792 ret = get_errno(setdomainname(p, arg2));
5793 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005794 break;
5795 case TARGET_NR_uname:
5796 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00005797 {
5798 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00005799
bellard579a97f2007-11-11 14:26:47 +00005800 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5801 goto efault;
bellard29e619b2004-09-13 21:41:04 +00005802 ret = get_errno(sys_uname(buf));
5803 if (!is_error(ret)) {
5804 /* Overrite the native machine name with whatever is being
5805 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01005806 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00005807 /* Allow the user to override the reported release. */
5808 if (qemu_uname_release && *qemu_uname_release)
5809 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00005810 }
pbrook53a59602006-03-25 19:31:22 +00005811 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00005812 }
bellard31e31b82003-02-18 22:55:36 +00005813 break;
bellard6dbad632003-03-16 18:05:05 +00005814#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00005815 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00005816 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00005817 break;
j_mayer84409dd2007-04-06 08:56:50 +00005818#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00005819 case TARGET_NR_vm86old:
5820 goto unimplemented;
5821 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00005822 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00005823 break;
5824#endif
j_mayer84409dd2007-04-06 08:56:50 +00005825#endif
bellard31e31b82003-02-18 22:55:36 +00005826 case TARGET_NR_adjtimex:
5827 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005828#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00005829 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00005830#endif
bellard31e31b82003-02-18 22:55:36 +00005831 case TARGET_NR_init_module:
5832 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00005833#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00005834 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00005835#endif
bellard31e31b82003-02-18 22:55:36 +00005836 goto unimplemented;
5837 case TARGET_NR_quotactl:
5838 goto unimplemented;
5839 case TARGET_NR_getpgid:
5840 ret = get_errno(getpgid(arg1));
5841 break;
5842 case TARGET_NR_fchdir:
5843 ret = get_errno(fchdir(arg1));
5844 break;
j_mayer84409dd2007-04-06 08:56:50 +00005845#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00005846 case TARGET_NR_bdflush:
5847 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00005848#endif
thse5febef2007-04-01 18:31:35 +00005849#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00005850 case TARGET_NR_sysfs:
5851 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005852#endif
bellard31e31b82003-02-18 22:55:36 +00005853 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00005854 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00005855 break;
thse5febef2007-04-01 18:31:35 +00005856#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00005857 case TARGET_NR_afs_syscall:
5858 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005859#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005860#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005861 case TARGET_NR__llseek:
5862 {
bellard4f2ac232004-04-26 19:44:02 +00005863#if defined (__x86_64__)
5864 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00005865 if (put_user_s64(ret, arg4))
5866 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00005867#else
bellard31e31b82003-02-18 22:55:36 +00005868 int64_t res;
5869 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00005870 if (put_user_s64(res, arg4))
5871 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00005872#endif
bellard31e31b82003-02-18 22:55:36 +00005873 }
5874 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005875#endif
bellard31e31b82003-02-18 22:55:36 +00005876 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02005877#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00005878 {
pbrook53a59602006-03-25 19:31:22 +00005879 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00005880 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005881 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00005882
5883 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00005884 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00005885 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00005886 goto fail;
5887 }
ths3b46e622007-09-17 08:09:54 +00005888
bellard4add45b2003-06-05 01:52:59 +00005889 ret = get_errno(sys_getdents(arg1, dirp, count));
5890 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005891 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00005892 struct target_dirent *tde;
5893 int len = ret;
5894 int reclen, treclen;
5895 int count1, tnamelen;
5896
5897 count1 = 0;
5898 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00005899 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5900 goto efault;
bellard4add45b2003-06-05 01:52:59 +00005901 tde = target_dirp;
5902 while (len > 0) {
5903 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00005904 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00005905 tde->d_reclen = tswap16(treclen);
5906 tde->d_ino = tswapl(de->d_ino);
5907 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00005908 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00005909 if (tnamelen > 256)
5910 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00005911 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00005912 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00005913 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00005914 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00005915 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00005916 count1 += treclen;
5917 }
5918 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00005919 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00005920 }
5921 free(dirp);
5922 }
5923#else
bellard31e31b82003-02-18 22:55:36 +00005924 {
aurel326556a832008-10-13 21:08:17 +00005925 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005926 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00005927
bellard579a97f2007-11-11 14:26:47 +00005928 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5929 goto efault;
bellard72f03902003-02-18 23:33:18 +00005930 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00005931 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005932 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00005933 int len = ret;
5934 int reclen;
5935 de = dirp;
5936 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00005937 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00005938 if (reclen > len)
5939 break;
bellard8083a3e2003-03-24 23:12:16 +00005940 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00005941 tswapls(&de->d_ino);
5942 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00005943 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00005944 len -= reclen;
5945 }
5946 }
pbrook53a59602006-03-25 19:31:22 +00005947 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00005948 }
bellard4add45b2003-06-05 01:52:59 +00005949#endif
bellard31e31b82003-02-18 22:55:36 +00005950 break;
ths3ae43202007-09-16 21:39:48 +00005951#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00005952 case TARGET_NR_getdents64:
5953 {
aurel326556a832008-10-13 21:08:17 +00005954 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005955 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00005956 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5957 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00005958 ret = get_errno(sys_getdents64(arg1, dirp, count));
5959 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005960 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00005961 int len = ret;
5962 int reclen;
5963 de = dirp;
5964 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00005965 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00005966 if (reclen > len)
5967 break;
bellard8083a3e2003-03-24 23:12:16 +00005968 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00005969 tswap64s((uint64_t *)&de->d_ino);
5970 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00005971 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00005972 len -= reclen;
5973 }
5974 }
pbrook53a59602006-03-25 19:31:22 +00005975 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00005976 }
5977 break;
bellarda541f292004-04-12 20:39:29 +00005978#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00005979#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00005980 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00005981 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00005982 break;
thse5febef2007-04-01 18:31:35 +00005983#endif
5984#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00005985 case TARGET_NR_poll:
5986 {
pbrook53a59602006-03-25 19:31:22 +00005987 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00005988 unsigned int nfds = arg2;
5989 int timeout = arg3;
5990 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00005991 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00005992
bellard579a97f2007-11-11 14:26:47 +00005993 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
5994 if (!target_pfd)
5995 goto efault;
bellard9de5e442003-03-23 16:49:39 +00005996 pfd = alloca(sizeof(struct pollfd) * nfds);
5997 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00005998 pfd[i].fd = tswap32(target_pfd[i].fd);
5999 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00006000 }
6001 ret = get_errno(poll(pfd, nfds, timeout));
6002 if (!is_error(ret)) {
6003 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006004 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00006005 }
pbrook53a59602006-03-25 19:31:22 +00006006 ret += nfds * (sizeof(struct target_pollfd)
6007 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00006008 }
pbrook53a59602006-03-25 19:31:22 +00006009 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00006010 }
6011 break;
thse5febef2007-04-01 18:31:35 +00006012#endif
bellard31e31b82003-02-18 22:55:36 +00006013 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00006014 /* NOTE: the flock constant seems to be the same for every
6015 Linux platform */
6016 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006017 break;
6018 case TARGET_NR_readv:
6019 {
6020 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006021 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006022
6023 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006024 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6025 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006026 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006027 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00006028 }
6029 break;
6030 case TARGET_NR_writev:
6031 {
6032 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006033 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006034
6035 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006036 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6037 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006038 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006039 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00006040 }
6041 break;
6042 case TARGET_NR_getsid:
6043 ret = get_errno(getsid(arg1));
6044 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006045#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00006046 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00006047 ret = get_errno(fdatasync(arg1));
6048 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006049#endif
bellard31e31b82003-02-18 22:55:36 +00006050 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00006051 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00006052 return value. */
ths0da46a62007-10-20 20:23:07 +00006053 ret = -TARGET_ENOTDIR;
6054 break;
bellard31e31b82003-02-18 22:55:36 +00006055 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00006056 {
pbrook53a59602006-03-25 19:31:22 +00006057 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006058 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00006059
bellard579a97f2007-11-11 14:26:47 +00006060 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6061 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006062 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006063 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00006064 ret = get_errno(sched_setparam(arg1, &schp));
6065 }
6066 break;
bellard31e31b82003-02-18 22:55:36 +00006067 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00006068 {
pbrook53a59602006-03-25 19:31:22 +00006069 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006070 struct sched_param schp;
6071 ret = get_errno(sched_getparam(arg1, &schp));
6072 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006073 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6074 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006075 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006076 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00006077 }
6078 }
6079 break;
bellard31e31b82003-02-18 22:55:36 +00006080 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00006081 {
pbrook53a59602006-03-25 19:31:22 +00006082 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006083 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00006084 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6085 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006086 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006087 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00006088 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6089 }
6090 break;
bellard31e31b82003-02-18 22:55:36 +00006091 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00006092 ret = get_errno(sched_getscheduler(arg1));
6093 break;
bellard31e31b82003-02-18 22:55:36 +00006094 case TARGET_NR_sched_yield:
6095 ret = get_errno(sched_yield());
6096 break;
6097 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00006098 ret = get_errno(sched_get_priority_max(arg1));
6099 break;
bellard31e31b82003-02-18 22:55:36 +00006100 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00006101 ret = get_errno(sched_get_priority_min(arg1));
6102 break;
bellard31e31b82003-02-18 22:55:36 +00006103 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00006104 {
bellard5cd43932003-03-29 16:54:36 +00006105 struct timespec ts;
6106 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6107 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006108 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00006109 }
6110 }
6111 break;
bellard31e31b82003-02-18 22:55:36 +00006112 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00006113 {
bellard1b6b0292003-03-22 17:31:38 +00006114 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00006115 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00006116 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00006117 if (is_error(ret) && arg2) {
6118 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00006119 }
6120 }
6121 break;
thse5febef2007-04-01 18:31:35 +00006122#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00006123 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00006124 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006125#endif
6126#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00006127 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00006128 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006129#endif
bellard31e31b82003-02-18 22:55:36 +00006130 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00006131 switch (arg1)
6132 {
6133 case PR_GET_PDEATHSIG:
6134 {
6135 int deathsig;
6136 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00006137 if (!is_error(ret) && arg2
6138 && put_user_ual(deathsig, arg2))
6139 goto efault;
thse5574482007-02-11 20:03:13 +00006140 }
6141 break;
6142 default:
6143 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6144 break;
6145 }
ths39b9aae2007-02-11 18:36:44 +00006146 break;
bellardd2fd1af2007-11-14 18:08:56 +00006147#ifdef TARGET_NR_arch_prctl
6148 case TARGET_NR_arch_prctl:
6149#if defined(TARGET_I386) && !defined(TARGET_ABI32)
6150 ret = do_arch_prctl(cpu_env, arg1, arg2);
6151 break;
6152#else
6153 goto unimplemented;
6154#endif
6155#endif
bellard67867302003-11-23 17:05:30 +00006156#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00006157 case TARGET_NR_pread:
balroga4ae00b2008-09-20 03:14:14 +00006158#ifdef TARGET_ARM
6159 if (((CPUARMState *)cpu_env)->eabi)
6160 arg4 = arg5;
6161#endif
bellard579a97f2007-11-11 14:26:47 +00006162 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6163 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006164 ret = get_errno(pread(arg1, p, arg3, arg4));
6165 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00006166 break;
bellard31e31b82003-02-18 22:55:36 +00006167 case TARGET_NR_pwrite:
balroga4ae00b2008-09-20 03:14:14 +00006168#ifdef TARGET_ARM
6169 if (((CPUARMState *)cpu_env)->eabi)
6170 arg4 = arg5;
6171#endif
bellard579a97f2007-11-11 14:26:47 +00006172 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6173 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006174 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6175 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00006176 break;
bellard67867302003-11-23 17:05:30 +00006177#endif
aurel32f2c7ba12008-03-28 22:32:06 +00006178#ifdef TARGET_NR_pread64
6179 case TARGET_NR_pread64:
6180 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6181 goto efault;
6182 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6183 unlock_user(p, arg2, ret);
6184 break;
6185 case TARGET_NR_pwrite64:
6186 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6187 goto efault;
6188 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6189 unlock_user(p, arg2, 0);
6190 break;
6191#endif
bellard31e31b82003-02-18 22:55:36 +00006192 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00006193 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6194 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006195 ret = get_errno(sys_getcwd1(p, arg2));
6196 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00006197 break;
6198 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00006199 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006200 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00006201 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006202 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00006203#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02006204 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6205 defined(TARGET_M68K)
bellard579a97f2007-11-11 14:26:47 +00006206 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00006207 break;
6208#else
bellard5cd43932003-03-29 16:54:36 +00006209 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00006210#endif
bellard31e31b82003-02-18 22:55:36 +00006211 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00006212 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006213#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00006214 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00006215 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006216#endif
6217#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00006218 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00006219 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006220#endif
bellard048f6b42005-11-26 18:47:20 +00006221#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00006222 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00006223 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6224 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00006225 break;
bellard048f6b42005-11-26 18:47:20 +00006226#endif
bellardebc05482003-09-30 21:08:41 +00006227#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00006228 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00006229 {
6230 struct rlimit rlim;
6231 ret = get_errno(getrlimit(arg1, &rlim));
6232 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006233 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00006234 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6235 goto efault;
bellard728584b2003-04-29 20:43:36 +00006236 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
6237 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006238 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00006239 }
6240 break;
6241 }
bellardebc05482003-09-30 21:08:41 +00006242#endif
bellarda315a142005-01-30 22:59:18 +00006243#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00006244 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00006245 if (!(p = lock_user_string(arg1)))
6246 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006247 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6248 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00006249 break;
bellarda315a142005-01-30 22:59:18 +00006250#endif
6251#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00006252 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00006253 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00006254 break;
bellarda315a142005-01-30 22:59:18 +00006255#endif
6256#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00006257 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00006258 if (!(p = lock_user_string(arg1)))
6259 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006260 ret = get_errno(stat(path(p), &st));
6261 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006262 if (!is_error(ret))
6263 ret = host_to_target_stat64(cpu_env, arg2, &st);
6264 break;
bellarda315a142005-01-30 22:59:18 +00006265#endif
6266#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00006267 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00006268 if (!(p = lock_user_string(arg1)))
6269 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006270 ret = get_errno(lstat(path(p), &st));
6271 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006272 if (!is_error(ret))
6273 ret = host_to_target_stat64(cpu_env, arg2, &st);
6274 break;
bellarda315a142005-01-30 22:59:18 +00006275#endif
6276#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00006277 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00006278 ret = get_errno(fstat(arg1, &st));
6279 if (!is_error(ret))
6280 ret = host_to_target_stat64(cpu_env, arg2, &st);
6281 break;
bellardec86b0f2003-04-11 00:15:04 +00006282#endif
aurel329d33b762009-04-08 23:07:05 +00006283#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6284 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6285#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006286 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00006287#endif
6288#ifdef TARGET_NR_newfstatat
6289 case TARGET_NR_newfstatat:
6290#endif
balrog6a24a772008-09-20 02:23:36 +00006291 if (!(p = lock_user_string(arg2)))
6292 goto efault;
aurel329d33b762009-04-08 23:07:05 +00006293#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006294 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00006295#else
6296 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6297#endif
balrog6a24a772008-09-20 02:23:36 +00006298 if (!is_error(ret))
6299 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00006300 break;
bellarda315a142005-01-30 22:59:18 +00006301#endif
bellard67867302003-11-23 17:05:30 +00006302#ifdef USE_UID16
6303 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00006304 if (!(p = lock_user_string(arg1)))
6305 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006306 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6307 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006308 break;
6309 case TARGET_NR_getuid:
6310 ret = get_errno(high2lowuid(getuid()));
6311 break;
6312 case TARGET_NR_getgid:
6313 ret = get_errno(high2lowgid(getgid()));
6314 break;
6315 case TARGET_NR_geteuid:
6316 ret = get_errno(high2lowuid(geteuid()));
6317 break;
6318 case TARGET_NR_getegid:
6319 ret = get_errno(high2lowgid(getegid()));
6320 break;
6321 case TARGET_NR_setreuid:
6322 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6323 break;
6324 case TARGET_NR_setregid:
6325 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6326 break;
6327 case TARGET_NR_getgroups:
6328 {
6329 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006330 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006331 gid_t *grouplist;
6332 int i;
6333
6334 grouplist = alloca(gidsetsize * sizeof(gid_t));
6335 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006336 if (gidsetsize == 0)
6337 break;
bellard67867302003-11-23 17:05:30 +00006338 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006339 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6340 if (!target_grouplist)
6341 goto efault;
balroga2155fc2008-09-20 02:12:08 +00006342 for(i = 0;i < ret; i++)
bellard67867302003-11-23 17:05:30 +00006343 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006344 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00006345 }
6346 }
6347 break;
6348 case TARGET_NR_setgroups:
6349 {
6350 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006351 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006352 gid_t *grouplist;
6353 int i;
6354
6355 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006356 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6357 if (!target_grouplist) {
6358 ret = -TARGET_EFAULT;
6359 goto fail;
6360 }
bellard67867302003-11-23 17:05:30 +00006361 for(i = 0;i < gidsetsize; i++)
6362 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006363 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00006364 ret = get_errno(setgroups(gidsetsize, grouplist));
6365 }
6366 break;
6367 case TARGET_NR_fchown:
6368 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6369 break;
thsccfa72b2007-09-24 09:23:34 +00006370#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6371 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00006372 if (!(p = lock_user_string(arg2)))
6373 goto efault;
6374 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6375 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00006376 break;
6377#endif
bellard67867302003-11-23 17:05:30 +00006378#ifdef TARGET_NR_setresuid
6379 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00006380 ret = get_errno(setresuid(low2highuid(arg1),
6381 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00006382 low2highuid(arg3)));
6383 break;
6384#endif
6385#ifdef TARGET_NR_getresuid
6386 case TARGET_NR_getresuid:
6387 {
pbrook53a59602006-03-25 19:31:22 +00006388 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00006389 ret = get_errno(getresuid(&ruid, &euid, &suid));
6390 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006391 if (put_user_u16(high2lowuid(ruid), arg1)
6392 || put_user_u16(high2lowuid(euid), arg2)
6393 || put_user_u16(high2lowuid(suid), arg3))
6394 goto efault;
bellard67867302003-11-23 17:05:30 +00006395 }
6396 }
6397 break;
6398#endif
6399#ifdef TARGET_NR_getresgid
6400 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00006401 ret = get_errno(setresgid(low2highgid(arg1),
6402 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00006403 low2highgid(arg3)));
6404 break;
6405#endif
6406#ifdef TARGET_NR_getresgid
6407 case TARGET_NR_getresgid:
6408 {
pbrook53a59602006-03-25 19:31:22 +00006409 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00006410 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6411 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006412 if (put_user_u16(high2lowgid(rgid), arg1)
6413 || put_user_u16(high2lowgid(egid), arg2)
6414 || put_user_u16(high2lowgid(sgid), arg3))
6415 goto efault;
bellard67867302003-11-23 17:05:30 +00006416 }
6417 }
6418 break;
6419#endif
6420 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00006421 if (!(p = lock_user_string(arg1)))
6422 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006423 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6424 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006425 break;
6426 case TARGET_NR_setuid:
6427 ret = get_errno(setuid(low2highuid(arg1)));
6428 break;
6429 case TARGET_NR_setgid:
6430 ret = get_errno(setgid(low2highgid(arg1)));
6431 break;
6432 case TARGET_NR_setfsuid:
6433 ret = get_errno(setfsuid(arg1));
6434 break;
6435 case TARGET_NR_setfsgid:
6436 ret = get_errno(setfsgid(arg1));
6437 break;
6438#endif /* USE_UID16 */
6439
bellarda315a142005-01-30 22:59:18 +00006440#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00006441 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00006442 if (!(p = lock_user_string(arg1)))
6443 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006444 ret = get_errno(lchown(p, arg2, arg3));
6445 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006446 break;
bellarda315a142005-01-30 22:59:18 +00006447#endif
6448#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00006449 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00006450 ret = get_errno(getuid());
6451 break;
bellarda315a142005-01-30 22:59:18 +00006452#endif
aurel3264b4d282008-11-14 17:20:15 +00006453
6454#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6455 /* Alpha specific */
6456 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006457 {
6458 uid_t euid;
6459 euid=geteuid();
6460 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6461 }
aurel3264b4d282008-11-14 17:20:15 +00006462 ret = get_errno(getuid());
6463 break;
6464#endif
6465#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6466 /* Alpha specific */
6467 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006468 {
6469 uid_t egid;
6470 egid=getegid();
6471 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6472 }
aurel3264b4d282008-11-14 17:20:15 +00006473 ret = get_errno(getgid());
6474 break;
6475#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08006476#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
6477 /* Alpha specific */
6478 case TARGET_NR_osf_getsysinfo:
6479 ret = -TARGET_EOPNOTSUPP;
6480 switch (arg1) {
6481 case TARGET_GSI_IEEE_FP_CONTROL:
6482 {
6483 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
6484
6485 /* Copied from linux ieee_fpcr_to_swcr. */
6486 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
6487 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
6488 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
6489 | SWCR_TRAP_ENABLE_DZE
6490 | SWCR_TRAP_ENABLE_OVF);
6491 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
6492 | SWCR_TRAP_ENABLE_INE);
6493 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
6494 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
6495
6496 if (put_user_u64 (swcr, arg2))
6497 goto efault;
6498 ret = 0;
6499 }
6500 break;
6501
6502 /* case GSI_IEEE_STATE_AT_SIGNAL:
6503 -- Not implemented in linux kernel.
6504 case GSI_UACPROC:
6505 -- Retrieves current unaligned access state; not much used.
6506 case GSI_PROC_TYPE:
6507 -- Retrieves implver information; surely not used.
6508 case GSI_GET_HWRPB:
6509 -- Grabs a copy of the HWRPB; surely not used.
6510 */
6511 }
6512 break;
6513#endif
6514#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
6515 /* Alpha specific */
6516 case TARGET_NR_osf_setsysinfo:
6517 ret = -TARGET_EOPNOTSUPP;
6518 switch (arg1) {
6519 case TARGET_SSI_IEEE_FP_CONTROL:
6520 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
6521 {
6522 uint64_t swcr, fpcr, orig_fpcr;
6523
6524 if (get_user_u64 (swcr, arg2))
6525 goto efault;
6526 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
6527 fpcr = orig_fpcr & FPCR_DYN_MASK;
6528
6529 /* Copied from linux ieee_swcr_to_fpcr. */
6530 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
6531 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
6532 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
6533 | SWCR_TRAP_ENABLE_DZE
6534 | SWCR_TRAP_ENABLE_OVF)) << 48;
6535 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
6536 | SWCR_TRAP_ENABLE_INE)) << 57;
6537 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
6538 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
6539
6540 cpu_alpha_store_fpcr (cpu_env, fpcr);
6541 ret = 0;
6542
6543 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
6544 /* Old exceptions are not signaled. */
6545 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
6546
6547 /* If any exceptions set by this call, and are unmasked,
6548 send a signal. */
6549 /* ??? FIXME */
6550 }
6551 }
6552 break;
6553
6554 /* case SSI_NVPAIRS:
6555 -- Used with SSIN_UACPROC to enable unaligned accesses.
6556 case SSI_IEEE_STATE_AT_SIGNAL:
6557 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
6558 -- Not implemented in linux kernel
6559 */
6560 }
6561 break;
6562#endif
6563#ifdef TARGET_NR_osf_sigprocmask
6564 /* Alpha specific. */
6565 case TARGET_NR_osf_sigprocmask:
6566 {
6567 abi_ulong mask;
6568 int how = arg1;
6569 sigset_t set, oldset;
6570
6571 switch(arg1) {
6572 case TARGET_SIG_BLOCK:
6573 how = SIG_BLOCK;
6574 break;
6575 case TARGET_SIG_UNBLOCK:
6576 how = SIG_UNBLOCK;
6577 break;
6578 case TARGET_SIG_SETMASK:
6579 how = SIG_SETMASK;
6580 break;
6581 default:
6582 ret = -TARGET_EINVAL;
6583 goto fail;
6584 }
6585 mask = arg2;
6586 target_to_host_old_sigset(&set, &mask);
6587 sigprocmask(arg1, &set, &oldset);
6588 host_to_target_old_sigset(&mask, &oldset);
6589 ret = mask;
6590 }
6591 break;
6592#endif
aurel3264b4d282008-11-14 17:20:15 +00006593
bellarda315a142005-01-30 22:59:18 +00006594#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00006595 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00006596 ret = get_errno(getgid());
6597 break;
bellarda315a142005-01-30 22:59:18 +00006598#endif
6599#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00006600 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00006601 ret = get_errno(geteuid());
6602 break;
bellarda315a142005-01-30 22:59:18 +00006603#endif
6604#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00006605 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00006606 ret = get_errno(getegid());
6607 break;
bellarda315a142005-01-30 22:59:18 +00006608#endif
6609#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00006610 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00006611 ret = get_errno(setreuid(arg1, arg2));
6612 break;
bellarda315a142005-01-30 22:59:18 +00006613#endif
6614#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00006615 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00006616 ret = get_errno(setregid(arg1, arg2));
6617 break;
bellarda315a142005-01-30 22:59:18 +00006618#endif
6619#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00006620 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00006621 {
6622 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006623 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006624 gid_t *grouplist;
6625 int i;
6626
6627 grouplist = alloca(gidsetsize * sizeof(gid_t));
6628 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006629 if (gidsetsize == 0)
6630 break;
bellard99c475a2005-01-31 20:45:13 +00006631 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006632 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6633 if (!target_grouplist) {
6634 ret = -TARGET_EFAULT;
6635 goto fail;
6636 }
balroga2155fc2008-09-20 02:12:08 +00006637 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00006638 target_grouplist[i] = tswap32(grouplist[i]);
6639 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00006640 }
6641 }
6642 break;
bellarda315a142005-01-30 22:59:18 +00006643#endif
6644#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00006645 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00006646 {
6647 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006648 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006649 gid_t *grouplist;
6650 int i;
ths3b46e622007-09-17 08:09:54 +00006651
bellard99c475a2005-01-31 20:45:13 +00006652 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006653 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6654 if (!target_grouplist) {
6655 ret = -TARGET_EFAULT;
6656 goto fail;
6657 }
bellard99c475a2005-01-31 20:45:13 +00006658 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00006659 grouplist[i] = tswap32(target_grouplist[i]);
6660 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00006661 ret = get_errno(setgroups(gidsetsize, grouplist));
6662 }
6663 break;
bellarda315a142005-01-30 22:59:18 +00006664#endif
6665#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00006666 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00006667 ret = get_errno(fchown(arg1, arg2, arg3));
6668 break;
bellarda315a142005-01-30 22:59:18 +00006669#endif
6670#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00006671 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006672 ret = get_errno(setresuid(arg1, arg2, arg3));
6673 break;
bellarda315a142005-01-30 22:59:18 +00006674#endif
6675#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00006676 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006677 {
pbrook53a59602006-03-25 19:31:22 +00006678 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00006679 ret = get_errno(getresuid(&ruid, &euid, &suid));
6680 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006681 if (put_user_u32(ruid, arg1)
6682 || put_user_u32(euid, arg2)
6683 || put_user_u32(suid, arg3))
6684 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006685 }
6686 }
6687 break;
bellarda315a142005-01-30 22:59:18 +00006688#endif
6689#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00006690 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006691 ret = get_errno(setresgid(arg1, arg2, arg3));
6692 break;
bellarda315a142005-01-30 22:59:18 +00006693#endif
6694#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00006695 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006696 {
pbrook53a59602006-03-25 19:31:22 +00006697 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00006698 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6699 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006700 if (put_user_u32(rgid, arg1)
6701 || put_user_u32(egid, arg2)
6702 || put_user_u32(sgid, arg3))
6703 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006704 }
6705 }
6706 break;
bellarda315a142005-01-30 22:59:18 +00006707#endif
6708#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00006709 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00006710 if (!(p = lock_user_string(arg1)))
6711 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006712 ret = get_errno(chown(p, arg2, arg3));
6713 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006714 break;
bellarda315a142005-01-30 22:59:18 +00006715#endif
6716#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00006717 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00006718 ret = get_errno(setuid(arg1));
6719 break;
bellarda315a142005-01-30 22:59:18 +00006720#endif
6721#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00006722 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00006723 ret = get_errno(setgid(arg1));
6724 break;
bellarda315a142005-01-30 22:59:18 +00006725#endif
6726#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00006727 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00006728 ret = get_errno(setfsuid(arg1));
6729 break;
bellarda315a142005-01-30 22:59:18 +00006730#endif
6731#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00006732 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00006733 ret = get_errno(setfsgid(arg1));
6734 break;
bellarda315a142005-01-30 22:59:18 +00006735#endif
bellard67867302003-11-23 17:05:30 +00006736
bellard31e31b82003-02-18 22:55:36 +00006737 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00006738 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00006739#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00006740 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00006741 {
6742 void *a;
6743 ret = -TARGET_EFAULT;
6744 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6745 goto efault;
6746 if (!(p = lock_user_string(arg3)))
6747 goto mincore_fail;
6748 ret = get_errno(mincore(a, arg2, p));
6749 unlock_user(p, arg3, ret);
6750 mincore_fail:
6751 unlock_user(a, arg1, 0);
6752 }
6753 break;
bellardffa65c32004-01-04 23:57:22 +00006754#endif
aurel32408321b2008-10-01 21:46:32 +00006755#ifdef TARGET_NR_arm_fadvise64_64
6756 case TARGET_NR_arm_fadvise64_64:
6757 {
6758 /*
6759 * arm_fadvise64_64 looks like fadvise64_64 but
6760 * with different argument order
6761 */
6762 abi_long temp;
6763 temp = arg3;
6764 arg3 = arg4;
6765 arg4 = temp;
6766 }
6767#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02006768#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00006769#ifdef TARGET_NR_fadvise64_64
6770 case TARGET_NR_fadvise64_64:
6771#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02006772#ifdef TARGET_NR_fadvise64
6773 case TARGET_NR_fadvise64:
6774#endif
6775#ifdef TARGET_S390X
6776 switch (arg4) {
6777 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
6778 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
6779 case 6: arg4 = POSIX_FADV_DONTNEED; break;
6780 case 7: arg4 = POSIX_FADV_NOREUSE; break;
6781 default: break;
6782 }
6783#endif
6784 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00006785 break;
6786#endif
bellardffa65c32004-01-04 23:57:22 +00006787#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00006788 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00006789 /* A straight passthrough may not be safe because qemu sometimes
6790 turns private flie-backed mappings into anonymous mappings.
6791 This will break MADV_DONTNEED.
6792 This is a hint, so ignoring and returning success is ok. */
6793 ret = get_errno(0);
6794 break;
bellardffa65c32004-01-04 23:57:22 +00006795#endif
blueswir1992f48a2007-10-14 16:27:31 +00006796#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00006797 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00006798 {
thsb1e341e2007-03-20 21:50:52 +00006799 int cmd;
bellard77e46722003-04-29 20:39:06 +00006800 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00006801 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00006802#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00006803 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00006804#endif
bellard77e46722003-04-29 20:39:06 +00006805
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006806 cmd = target_to_host_fcntl_cmd(arg2);
6807 if (cmd == -TARGET_EINVAL)
6808 return cmd;
thsb1e341e2007-03-20 21:50:52 +00006809
bellard60cd49d2003-03-16 22:53:56 +00006810 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00006811 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00006812#ifdef TARGET_ARM
6813 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006814 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6815 goto efault;
ths58134272007-03-31 18:59:32 +00006816 fl.l_type = tswap16(target_efl->l_type);
6817 fl.l_whence = tswap16(target_efl->l_whence);
6818 fl.l_start = tswap64(target_efl->l_start);
6819 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006820 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00006821 unlock_user_struct(target_efl, arg3, 0);
6822 } else
6823#endif
6824 {
bellard9ee1fa22007-11-11 15:11:19 +00006825 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6826 goto efault;
ths58134272007-03-31 18:59:32 +00006827 fl.l_type = tswap16(target_fl->l_type);
6828 fl.l_whence = tswap16(target_fl->l_whence);
6829 fl.l_start = tswap64(target_fl->l_start);
6830 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006831 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00006832 unlock_user_struct(target_fl, arg3, 0);
6833 }
thsb1e341e2007-03-20 21:50:52 +00006834 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006835 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00006836#ifdef TARGET_ARM
6837 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006838 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6839 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006840 target_efl->l_type = tswap16(fl.l_type);
6841 target_efl->l_whence = tswap16(fl.l_whence);
6842 target_efl->l_start = tswap64(fl.l_start);
6843 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006844 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006845 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006846 } else
6847#endif
6848 {
bellard9ee1fa22007-11-11 15:11:19 +00006849 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6850 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006851 target_fl->l_type = tswap16(fl.l_type);
6852 target_fl->l_whence = tswap16(fl.l_whence);
6853 target_fl->l_start = tswap64(fl.l_start);
6854 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006855 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006856 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006857 }
bellard77e46722003-04-29 20:39:06 +00006858 }
6859 break;
6860
thsb1e341e2007-03-20 21:50:52 +00006861 case TARGET_F_SETLK64:
6862 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00006863#ifdef TARGET_ARM
6864 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006865 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6866 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006867 fl.l_type = tswap16(target_efl->l_type);
6868 fl.l_whence = tswap16(target_efl->l_whence);
6869 fl.l_start = tswap64(target_efl->l_start);
6870 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006871 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00006872 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00006873 } else
6874#endif
6875 {
bellard9ee1fa22007-11-11 15:11:19 +00006876 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6877 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006878 fl.l_type = tswap16(target_fl->l_type);
6879 fl.l_whence = tswap16(target_fl->l_whence);
6880 fl.l_start = tswap64(target_fl->l_start);
6881 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006882 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00006883 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00006884 }
thsb1e341e2007-03-20 21:50:52 +00006885 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006886 break;
bellard60cd49d2003-03-16 22:53:56 +00006887 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006888 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00006889 break;
6890 }
bellard77e46722003-04-29 20:39:06 +00006891 break;
6892 }
bellard60cd49d2003-03-16 22:53:56 +00006893#endif
ths7d600c82006-12-08 01:32:58 +00006894#ifdef TARGET_NR_cacheflush
6895 case TARGET_NR_cacheflush:
6896 /* self-modifying code is handled automatically, so nothing needed */
6897 ret = 0;
6898 break;
6899#endif
bellardebc05482003-09-30 21:08:41 +00006900#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00006901 case TARGET_NR_security:
6902 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006903#endif
bellardc573ff62004-01-04 15:51:36 +00006904#ifdef TARGET_NR_getpagesize
6905 case TARGET_NR_getpagesize:
6906 ret = TARGET_PAGE_SIZE;
6907 break;
6908#endif
bellard31e31b82003-02-18 22:55:36 +00006909 case TARGET_NR_gettid:
6910 ret = get_errno(gettid());
6911 break;
thse5febef2007-04-01 18:31:35 +00006912#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00006913 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00006914#if TARGET_ABI_BITS == 32
6915#ifdef TARGET_ARM
6916 if (((CPUARMState *)cpu_env)->eabi)
6917 {
6918 arg2 = arg3;
6919 arg3 = arg4;
6920 arg4 = arg5;
6921 }
6922#endif
6923 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
6924#else
6925 ret = get_errno(readahead(arg1, arg2, arg3));
6926#endif
6927 break;
thse5febef2007-04-01 18:31:35 +00006928#endif
bellardebc05482003-09-30 21:08:41 +00006929#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00006930 case TARGET_NR_setxattr:
6931 case TARGET_NR_lsetxattr:
6932 case TARGET_NR_fsetxattr:
6933 case TARGET_NR_getxattr:
6934 case TARGET_NR_lgetxattr:
6935 case TARGET_NR_fgetxattr:
6936 case TARGET_NR_listxattr:
6937 case TARGET_NR_llistxattr:
6938 case TARGET_NR_flistxattr:
6939 case TARGET_NR_removexattr:
6940 case TARGET_NR_lremovexattr:
6941 case TARGET_NR_fremovexattr:
Arnaud Patard6f932f92009-04-21 21:04:18 +03006942 ret = -TARGET_EOPNOTSUPP;
6943 break;
bellardebc05482003-09-30 21:08:41 +00006944#endif
6945#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00006946 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00006947#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00006948 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
6949 ret = 0;
6950 break;
edgar_iglef967792009-01-07 14:19:38 +00006951#elif defined(TARGET_CRIS)
6952 if (arg1 & 0xff)
6953 ret = -TARGET_EINVAL;
6954 else {
6955 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
6956 ret = 0;
6957 }
6958 break;
bellard8d18e892007-11-14 15:18:40 +00006959#elif defined(TARGET_I386) && defined(TARGET_ABI32)
6960 ret = do_set_thread_area(cpu_env, arg1);
6961 break;
ths6f5b89a2007-03-02 20:48:00 +00006962#else
6963 goto unimplemented_nowarn;
6964#endif
6965#endif
6966#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00006967 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00006968#if defined(TARGET_I386) && defined(TARGET_ABI32)
6969 ret = do_get_thread_area(cpu_env, arg1);
6970#else
bellard5cd43932003-03-29 16:54:36 +00006971 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00006972#endif
bellard8d18e892007-11-14 15:18:40 +00006973#endif
bellard48dc41e2006-06-21 18:15:50 +00006974#ifdef TARGET_NR_getdomainname
6975 case TARGET_NR_getdomainname:
6976 goto unimplemented_nowarn;
6977#endif
ths6f5b89a2007-03-02 20:48:00 +00006978
thsb5906f92007-03-19 13:32:45 +00006979#ifdef TARGET_NR_clock_gettime
6980 case TARGET_NR_clock_gettime:
6981 {
6982 struct timespec ts;
6983 ret = get_errno(clock_gettime(arg1, &ts));
6984 if (!is_error(ret)) {
6985 host_to_target_timespec(arg2, &ts);
6986 }
6987 break;
6988 }
6989#endif
6990#ifdef TARGET_NR_clock_getres
6991 case TARGET_NR_clock_getres:
6992 {
6993 struct timespec ts;
6994 ret = get_errno(clock_getres(arg1, &ts));
6995 if (!is_error(ret)) {
6996 host_to_target_timespec(arg2, &ts);
6997 }
6998 break;
6999 }
7000#endif
pbrook63d76512008-05-29 13:43:29 +00007001#ifdef TARGET_NR_clock_nanosleep
7002 case TARGET_NR_clock_nanosleep:
7003 {
7004 struct timespec ts;
7005 target_to_host_timespec(&ts, arg3);
7006 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7007 if (arg4)
7008 host_to_target_timespec(arg4, &ts);
7009 break;
7010 }
7011#endif
thsb5906f92007-03-19 13:32:45 +00007012
ths6f5b89a2007-03-02 20:48:00 +00007013#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7014 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00007015 ret = get_errno(set_tid_address((int *)g2h(arg1)));
7016 break;
ths6f5b89a2007-03-02 20:48:00 +00007017#endif
7018
ths3ae43202007-09-16 21:39:48 +00007019#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00007020 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00007021 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00007022 break;
7023#endif
7024
ths3ae43202007-09-16 21:39:48 +00007025#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00007026 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00007027 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7028 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00007029 break;
7030#endif
7031
ths4f2b1fe2007-06-21 21:57:12 +00007032#ifdef TARGET_NR_set_robust_list
7033 case TARGET_NR_set_robust_list:
7034 goto unimplemented_nowarn;
7035#endif
7036
ths9007f0e2007-09-25 17:50:37 +00007037#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7038 case TARGET_NR_utimensat:
7039 {
Riku Voipioebc996f2009-04-21 15:01:51 +03007040 struct timespec *tsp, ts[2];
7041 if (!arg3) {
7042 tsp = NULL;
7043 } else {
7044 target_to_host_timespec(ts, arg3);
7045 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7046 tsp = ts;
7047 }
ths9007f0e2007-09-25 17:50:37 +00007048 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03007049 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00007050 else {
bellard579a97f2007-11-11 14:26:47 +00007051 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00007052 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00007053 goto fail;
7054 }
Riku Voipioebc996f2009-04-21 15:01:51 +03007055 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00007056 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00007057 }
7058 }
7059 break;
7060#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02007061#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00007062 case TARGET_NR_futex:
7063 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7064 break;
7065#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007066#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00007067 case TARGET_NR_inotify_init:
7068 ret = get_errno(sys_inotify_init());
7069 break;
7070#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007071#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00007072#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7073 case TARGET_NR_inotify_init1:
7074 ret = get_errno(sys_inotify_init1(arg1));
7075 break;
7076#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007077#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007078#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00007079 case TARGET_NR_inotify_add_watch:
7080 p = lock_user_string(arg2);
7081 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7082 unlock_user(p, arg2, 0);
7083 break;
7084#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007085#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00007086 case TARGET_NR_inotify_rm_watch:
7087 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7088 break;
7089#endif
ths9007f0e2007-09-25 17:50:37 +00007090
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07007091#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00007092 case TARGET_NR_mq_open:
7093 {
7094 struct mq_attr posix_mq_attr;
7095
7096 p = lock_user_string(arg1 - 1);
7097 if (arg4 != 0)
7098 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7099 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7100 unlock_user (p, arg1, 0);
7101 }
7102 break;
7103
7104 case TARGET_NR_mq_unlink:
7105 p = lock_user_string(arg1 - 1);
7106 ret = get_errno(mq_unlink(p));
7107 unlock_user (p, arg1, 0);
7108 break;
7109
7110 case TARGET_NR_mq_timedsend:
7111 {
7112 struct timespec ts;
7113
7114 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7115 if (arg5 != 0) {
7116 target_to_host_timespec(&ts, arg5);
7117 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7118 host_to_target_timespec(arg5, &ts);
7119 }
7120 else
7121 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7122 unlock_user (p, arg2, arg3);
7123 }
7124 break;
7125
7126 case TARGET_NR_mq_timedreceive:
7127 {
7128 struct timespec ts;
7129 unsigned int prio;
7130
7131 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7132 if (arg5 != 0) {
7133 target_to_host_timespec(&ts, arg5);
7134 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7135 host_to_target_timespec(arg5, &ts);
7136 }
7137 else
7138 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7139 unlock_user (p, arg2, arg3);
7140 if (arg4 != 0)
7141 put_user_u32(prio, arg4);
7142 }
7143 break;
7144
7145 /* Not implemented for now... */
7146/* case TARGET_NR_mq_notify: */
7147/* break; */
7148
7149 case TARGET_NR_mq_getsetattr:
7150 {
7151 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7152 ret = 0;
7153 if (arg3 != 0) {
7154 ret = mq_getattr(arg1, &posix_mq_attr_out);
7155 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7156 }
7157 if (arg2 != 0) {
7158 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7159 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7160 }
7161
7162 }
7163 break;
7164#endif
7165
vibisreenivasan3ce34df2009-05-16 18:32:41 +05307166#ifdef CONFIG_SPLICE
7167#ifdef TARGET_NR_tee
7168 case TARGET_NR_tee:
7169 {
7170 ret = get_errno(tee(arg1,arg2,arg3,arg4));
7171 }
7172 break;
7173#endif
7174#ifdef TARGET_NR_splice
7175 case TARGET_NR_splice:
7176 {
7177 loff_t loff_in, loff_out;
7178 loff_t *ploff_in = NULL, *ploff_out = NULL;
7179 if(arg2) {
7180 get_user_u64(loff_in, arg2);
7181 ploff_in = &loff_in;
7182 }
7183 if(arg4) {
7184 get_user_u64(loff_out, arg2);
7185 ploff_out = &loff_out;
7186 }
7187 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7188 }
7189 break;
7190#endif
7191#ifdef TARGET_NR_vmsplice
7192 case TARGET_NR_vmsplice:
7193 {
7194 int count = arg3;
7195 struct iovec *vec;
7196
7197 vec = alloca(count * sizeof(struct iovec));
7198 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7199 goto efault;
7200 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7201 unlock_iovec(vec, arg2, count, 0);
7202 }
7203 break;
7204#endif
7205#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03007206#ifdef CONFIG_EVENTFD
7207#if defined(TARGET_NR_eventfd)
7208 case TARGET_NR_eventfd:
7209 ret = get_errno(eventfd(arg1, 0));
7210 break;
7211#endif
7212#if defined(TARGET_NR_eventfd2)
7213 case TARGET_NR_eventfd2:
7214 ret = get_errno(eventfd(arg1, arg2));
7215 break;
7216#endif
7217#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03007218#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7219 case TARGET_NR_fallocate:
7220 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7221 break;
7222#endif
bellard31e31b82003-02-18 22:55:36 +00007223 default:
7224 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00007225 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00007226#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 +00007227 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00007228#endif
ths0da46a62007-10-20 20:23:07 +00007229 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00007230 break;
7231 }
bellard579a97f2007-11-11 14:26:47 +00007232fail:
bellardc573ff62004-01-04 15:51:36 +00007233#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00007234 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00007235#endif
thsb92c47c2007-11-01 00:07:38 +00007236 if(do_strace)
7237 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00007238 return ret;
bellard579a97f2007-11-11 14:26:47 +00007239efault:
7240 ret = -TARGET_EFAULT;
7241 goto fail;
bellard31e31b82003-02-18 22:55:36 +00007242}