blob: bca8f70f2cfca67860280faeacae39fe542ae623 [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
Richard Henderson7dd46c02010-05-03 10:07:49 -0700721#if defined(TARGET_ALPHA)
722 /* We (partially) emulate OSF/1 on Alpha, which requires we
723 return a proper errno, not an unchanged brk value. */
724 if (is_error(mapped_addr)) {
725 return -TARGET_ENOMEM;
726 }
727#endif
728
729 if (!is_error(mapped_addr)) {
bellard31e31b82003-02-18 22:55:36 +0000730 target_brk = new_brk;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700731 }
balrog7ab240a2008-04-26 12:17:34 +0000732 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000733}
734
ths26edcf42007-12-09 02:25:24 +0000735static inline abi_long copy_from_user_fdset(fd_set *fds,
736 abi_ulong target_fds_addr,
737 int n)
bellard31e31b82003-02-18 22:55:36 +0000738{
ths26edcf42007-12-09 02:25:24 +0000739 int i, nw, j, k;
740 abi_ulong b, *target_fds;
741
742 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
743 if (!(target_fds = lock_user(VERIFY_READ,
744 target_fds_addr,
745 sizeof(abi_ulong) * nw,
746 1)))
747 return -TARGET_EFAULT;
748
749 FD_ZERO(fds);
750 k = 0;
751 for (i = 0; i < nw; i++) {
752 /* grab the abi_ulong */
753 __get_user(b, &target_fds[i]);
754 for (j = 0; j < TARGET_ABI_BITS; j++) {
755 /* check the bit inside the abi_ulong */
756 if ((b >> j) & 1)
757 FD_SET(k, fds);
758 k++;
bellard31e31b82003-02-18 22:55:36 +0000759 }
bellard31e31b82003-02-18 22:55:36 +0000760 }
ths26edcf42007-12-09 02:25:24 +0000761
762 unlock_user(target_fds, target_fds_addr, 0);
763
764 return 0;
bellard31e31b82003-02-18 22:55:36 +0000765}
766
ths26edcf42007-12-09 02:25:24 +0000767static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
768 const fd_set *fds,
769 int n)
bellard31e31b82003-02-18 22:55:36 +0000770{
bellard31e31b82003-02-18 22:55:36 +0000771 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000772 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000773 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000774
ths26edcf42007-12-09 02:25:24 +0000775 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
776 if (!(target_fds = lock_user(VERIFY_WRITE,
777 target_fds_addr,
778 sizeof(abi_ulong) * nw,
779 0)))
780 return -TARGET_EFAULT;
781
782 k = 0;
783 for (i = 0; i < nw; i++) {
784 v = 0;
785 for (j = 0; j < TARGET_ABI_BITS; j++) {
786 v |= ((FD_ISSET(k, fds) != 0) << j);
787 k++;
bellard31e31b82003-02-18 22:55:36 +0000788 }
ths26edcf42007-12-09 02:25:24 +0000789 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000790 }
ths26edcf42007-12-09 02:25:24 +0000791
792 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
793
794 return 0;
bellard31e31b82003-02-18 22:55:36 +0000795}
796
bellardc596ed12003-07-13 17:32:31 +0000797#if defined(__alpha__)
798#define HOST_HZ 1024
799#else
800#define HOST_HZ 100
801#endif
802
blueswir1992f48a2007-10-14 16:27:31 +0000803static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000804{
805#if HOST_HZ == TARGET_HZ
806 return ticks;
807#else
808 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
809#endif
810}
811
bellard579a97f2007-11-11 14:26:47 +0000812static inline abi_long host_to_target_rusage(abi_ulong target_addr,
813 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000814{
pbrook53a59602006-03-25 19:31:22 +0000815 struct target_rusage *target_rusage;
816
bellard579a97f2007-11-11 14:26:47 +0000817 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
818 return -TARGET_EFAULT;
bellardb4091862003-05-16 15:39:34 +0000819 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
820 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
821 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
822 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
823 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
824 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
825 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
826 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
827 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
828 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
829 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
830 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
831 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
832 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
833 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
834 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
835 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
836 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000837 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000838
839 return 0;
bellardb4091862003-05-16 15:39:34 +0000840}
841
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900842static inline rlim_t target_to_host_rlim(target_ulong target_rlim)
843{
844 if (target_rlim == TARGET_RLIM_INFINITY)
845 return RLIM_INFINITY;
846 else
847 return tswapl(target_rlim);
848}
849
850static inline target_ulong host_to_target_rlim(rlim_t rlim)
851{
852 if (rlim == RLIM_INFINITY || rlim != (target_long)rlim)
853 return TARGET_RLIM_INFINITY;
854 else
855 return tswapl(rlim);
856}
857
ths788f5ec2007-12-09 02:37:05 +0000858static inline abi_long copy_from_user_timeval(struct timeval *tv,
859 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000860{
pbrook53a59602006-03-25 19:31:22 +0000861 struct target_timeval *target_tv;
862
ths788f5ec2007-12-09 02:37:05 +0000863 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000864 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000865
866 __get_user(tv->tv_sec, &target_tv->tv_sec);
867 __get_user(tv->tv_usec, &target_tv->tv_usec);
868
869 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000870
871 return 0;
bellard31e31b82003-02-18 22:55:36 +0000872}
873
ths788f5ec2007-12-09 02:37:05 +0000874static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
875 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000876{
pbrook53a59602006-03-25 19:31:22 +0000877 struct target_timeval *target_tv;
878
ths788f5ec2007-12-09 02:37:05 +0000879 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000880 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000881
882 __put_user(tv->tv_sec, &target_tv->tv_sec);
883 __put_user(tv->tv_usec, &target_tv->tv_usec);
884
885 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000886
887 return 0;
bellard31e31b82003-02-18 22:55:36 +0000888}
889
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700890#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
891#include <mqueue.h>
892
aurel3224e10032009-04-15 16:11:43 +0000893static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
894 abi_ulong target_mq_attr_addr)
895{
896 struct target_mq_attr *target_mq_attr;
897
898 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
899 target_mq_attr_addr, 1))
900 return -TARGET_EFAULT;
901
902 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
903 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
904 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
905 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
906
907 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
908
909 return 0;
910}
911
912static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
913 const struct mq_attr *attr)
914{
915 struct target_mq_attr *target_mq_attr;
916
917 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
918 target_mq_attr_addr, 0))
919 return -TARGET_EFAULT;
920
921 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
922 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
923 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
924 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
925
926 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
927
928 return 0;
929}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700930#endif
bellard31e31b82003-02-18 22:55:36 +0000931
ths0da46a62007-10-20 20:23:07 +0000932/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000933static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000934 abi_ulong rfd_addr, abi_ulong wfd_addr,
935 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000936{
937 fd_set rfds, wfds, efds;
938 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
939 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000940 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000941
ths26edcf42007-12-09 02:25:24 +0000942 if (rfd_addr) {
943 if (copy_from_user_fdset(&rfds, rfd_addr, n))
944 return -TARGET_EFAULT;
945 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000946 } else {
pbrook53a59602006-03-25 19:31:22 +0000947 rfds_ptr = NULL;
948 }
ths26edcf42007-12-09 02:25:24 +0000949 if (wfd_addr) {
950 if (copy_from_user_fdset(&wfds, wfd_addr, n))
951 return -TARGET_EFAULT;
952 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000953 } else {
pbrook53a59602006-03-25 19:31:22 +0000954 wfds_ptr = NULL;
955 }
ths26edcf42007-12-09 02:25:24 +0000956 if (efd_addr) {
957 if (copy_from_user_fdset(&efds, efd_addr, n))
958 return -TARGET_EFAULT;
959 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000960 } else {
pbrook53a59602006-03-25 19:31:22 +0000961 efds_ptr = NULL;
962 }
ths3b46e622007-09-17 08:09:54 +0000963
ths26edcf42007-12-09 02:25:24 +0000964 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000965 if (copy_from_user_timeval(&tv, target_tv_addr))
966 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000967 tv_ptr = &tv;
968 } else {
969 tv_ptr = NULL;
970 }
ths26edcf42007-12-09 02:25:24 +0000971
bellard31e31b82003-02-18 22:55:36 +0000972 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000973
ths26edcf42007-12-09 02:25:24 +0000974 if (!is_error(ret)) {
975 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
976 return -TARGET_EFAULT;
977 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
978 return -TARGET_EFAULT;
979 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
980 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000981
ths788f5ec2007-12-09 02:37:05 +0000982 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
983 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000984 }
bellard579a97f2007-11-11 14:26:47 +0000985
bellard31e31b82003-02-18 22:55:36 +0000986 return ret;
987}
988
Riku Voipio099d6b02009-05-05 12:10:04 +0300989static abi_long do_pipe2(int host_pipe[], int flags)
990{
991#ifdef CONFIG_PIPE2
992 return pipe2(host_pipe, flags);
993#else
994 return -ENOSYS;
995#endif
996}
997
Richard Hendersonfb41a662010-05-03 10:07:52 -0700998static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
999 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001000{
1001 int host_pipe[2];
1002 abi_long ret;
1003 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1004
1005 if (is_error(ret))
1006 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001007
1008 /* Several targets have special calling conventions for the original
1009 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1010 if (!is_pipe2) {
1011#if defined(TARGET_ALPHA)
1012 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1013 return host_pipe[0];
1014#elif defined(TARGET_MIPS)
1015 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1016 return host_pipe[0];
1017#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001018 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001019 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001020#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001021 }
1022
Riku Voipio099d6b02009-05-05 12:10:04 +03001023 if (put_user_s32(host_pipe[0], pipedes)
1024 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1025 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001026 return get_errno(ret);
1027}
1028
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001029static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1030 abi_ulong target_addr,
1031 socklen_t len)
1032{
1033 struct target_ip_mreqn *target_smreqn;
1034
1035 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1036 if (!target_smreqn)
1037 return -TARGET_EFAULT;
1038 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1039 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1040 if (len == sizeof(struct target_ip_mreqn))
1041 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
1042 unlock_user(target_smreqn, target_addr, 0);
1043
1044 return 0;
1045}
1046
bellard579a97f2007-11-11 14:26:47 +00001047static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1048 abi_ulong target_addr,
1049 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001050{
aurel32607175e2009-04-15 16:11:59 +00001051 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1052 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001053 struct target_sockaddr *target_saddr;
1054
bellard579a97f2007-11-11 14:26:47 +00001055 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1056 if (!target_saddr)
1057 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001058
1059 sa_family = tswap16(target_saddr->sa_family);
1060
1061 /* Oops. The caller might send a incomplete sun_path; sun_path
1062 * must be terminated by \0 (see the manual page), but
1063 * unfortunately it is quite common to specify sockaddr_un
1064 * length as "strlen(x->sun_path)" while it should be
1065 * "strlen(...) + 1". We'll fix that here if needed.
1066 * Linux kernel has a similar feature.
1067 */
1068
1069 if (sa_family == AF_UNIX) {
1070 if (len < unix_maxlen && len > 0) {
1071 char *cp = (char*)target_saddr;
1072
1073 if ( cp[len-1] && !cp[len] )
1074 len++;
1075 }
1076 if (len > unix_maxlen)
1077 len = unix_maxlen;
1078 }
1079
pbrook53a59602006-03-25 19:31:22 +00001080 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001081 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001082 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001083
1084 return 0;
bellard7854b052003-03-29 17:22:23 +00001085}
1086
bellard579a97f2007-11-11 14:26:47 +00001087static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1088 struct sockaddr *addr,
1089 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001090{
pbrook53a59602006-03-25 19:31:22 +00001091 struct target_sockaddr *target_saddr;
1092
bellard579a97f2007-11-11 14:26:47 +00001093 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1094 if (!target_saddr)
1095 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001096 memcpy(target_saddr, addr, len);
1097 target_saddr->sa_family = tswap16(addr->sa_family);
1098 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001099
1100 return 0;
bellard7854b052003-03-29 17:22:23 +00001101}
1102
pbrook53a59602006-03-25 19:31:22 +00001103/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001104static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1105 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001106{
1107 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001108 abi_long msg_controllen;
1109 abi_ulong target_cmsg_addr;
1110 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001111 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001112
1113 msg_controllen = tswapl(target_msgh->msg_controllen);
1114 if (msg_controllen < sizeof (struct target_cmsghdr))
1115 goto the_end;
1116 target_cmsg_addr = tswapl(target_msgh->msg_control);
1117 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1118 if (!target_cmsg)
1119 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001120
1121 while (cmsg && target_cmsg) {
1122 void *data = CMSG_DATA(cmsg);
1123 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1124
ths5fafdf22007-09-16 21:08:06 +00001125 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001126 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1127
1128 space += CMSG_SPACE(len);
1129 if (space > msgh->msg_controllen) {
1130 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001131 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001132 break;
1133 }
1134
1135 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1136 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1137 cmsg->cmsg_len = CMSG_LEN(len);
1138
bellard3532fa72006-06-24 15:06:03 +00001139 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001140 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1141 memcpy(data, target_data, len);
1142 } else {
1143 int *fd = (int *)data;
1144 int *target_fd = (int *)target_data;
1145 int i, numfds = len / sizeof(int);
1146
1147 for (i = 0; i < numfds; i++)
1148 fd[i] = tswap32(target_fd[i]);
1149 }
1150
1151 cmsg = CMSG_NXTHDR(msgh, cmsg);
1152 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1153 }
bellard5a4a8982007-11-11 17:39:18 +00001154 unlock_user(target_cmsg, target_cmsg_addr, 0);
1155 the_end:
bellard7854b052003-03-29 17:22:23 +00001156 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001157 return 0;
bellard7854b052003-03-29 17:22:23 +00001158}
1159
pbrook53a59602006-03-25 19:31:22 +00001160/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001161static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1162 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001163{
1164 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001165 abi_long msg_controllen;
1166 abi_ulong target_cmsg_addr;
1167 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001168 socklen_t space = 0;
1169
bellard5a4a8982007-11-11 17:39:18 +00001170 msg_controllen = tswapl(target_msgh->msg_controllen);
1171 if (msg_controllen < sizeof (struct target_cmsghdr))
1172 goto the_end;
1173 target_cmsg_addr = tswapl(target_msgh->msg_control);
1174 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1175 if (!target_cmsg)
1176 return -TARGET_EFAULT;
1177
bellard7854b052003-03-29 17:22:23 +00001178 while (cmsg && target_cmsg) {
1179 void *data = CMSG_DATA(cmsg);
1180 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1181
1182 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1183
1184 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001185 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001186 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001187 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001188 break;
1189 }
1190
1191 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1192 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1193 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1194
bellard3532fa72006-06-24 15:06:03 +00001195 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001196 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1197 memcpy(target_data, data, len);
1198 } else {
1199 int *fd = (int *)data;
1200 int *target_fd = (int *)target_data;
1201 int i, numfds = len / sizeof(int);
1202
1203 for (i = 0; i < numfds; i++)
1204 target_fd[i] = tswap32(fd[i]);
1205 }
1206
1207 cmsg = CMSG_NXTHDR(msgh, cmsg);
1208 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1209 }
bellard5a4a8982007-11-11 17:39:18 +00001210 unlock_user(target_cmsg, target_cmsg_addr, space);
1211 the_end:
1212 target_msgh->msg_controllen = tswapl(space);
1213 return 0;
bellard7854b052003-03-29 17:22:23 +00001214}
1215
ths0da46a62007-10-20 20:23:07 +00001216/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001217static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001218 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001219{
blueswir1992f48a2007-10-14 16:27:31 +00001220 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001221 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001222 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001223 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001224
bellard8853f862004-02-22 14:57:26 +00001225 switch(level) {
1226 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001227 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001228 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001229 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001230
bellard2f619692007-11-16 10:46:05 +00001231 if (get_user_u32(val, optval_addr))
1232 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001233 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1234 break;
1235 case SOL_IP:
1236 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001237 case IP_TOS:
1238 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001239 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001240 case IP_ROUTER_ALERT:
1241 case IP_RECVOPTS:
1242 case IP_RETOPTS:
1243 case IP_PKTINFO:
1244 case IP_MTU_DISCOVER:
1245 case IP_RECVERR:
1246 case IP_RECVTOS:
1247#ifdef IP_FREEBIND
1248 case IP_FREEBIND:
1249#endif
1250 case IP_MULTICAST_TTL:
1251 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001252 val = 0;
1253 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001254 if (get_user_u32(val, optval_addr))
1255 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001256 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001257 if (get_user_u8(val, optval_addr))
1258 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001259 }
1260 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1261 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001262 case IP_ADD_MEMBERSHIP:
1263 case IP_DROP_MEMBERSHIP:
1264 if (optlen < sizeof (struct target_ip_mreq) ||
1265 optlen > sizeof (struct target_ip_mreqn))
1266 return -TARGET_EINVAL;
1267
1268 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1269 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1270 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1271 break;
1272
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001273 case IP_BLOCK_SOURCE:
1274 case IP_UNBLOCK_SOURCE:
1275 case IP_ADD_SOURCE_MEMBERSHIP:
1276 case IP_DROP_SOURCE_MEMBERSHIP:
1277 if (optlen != sizeof (struct target_ip_mreq_source))
1278 return -TARGET_EINVAL;
1279
1280 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1281 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1282 unlock_user (ip_mreq_source, optval_addr, 0);
1283 break;
1284
bellard8853f862004-02-22 14:57:26 +00001285 default:
1286 goto unimplemented;
1287 }
1288 break;
bellard3532fa72006-06-24 15:06:03 +00001289 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001290 switch (optname) {
1291 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001292 case TARGET_SO_DEBUG:
1293 optname = SO_DEBUG;
1294 break;
1295 case TARGET_SO_REUSEADDR:
1296 optname = SO_REUSEADDR;
1297 break;
1298 case TARGET_SO_TYPE:
1299 optname = SO_TYPE;
1300 break;
1301 case TARGET_SO_ERROR:
1302 optname = SO_ERROR;
1303 break;
1304 case TARGET_SO_DONTROUTE:
1305 optname = SO_DONTROUTE;
1306 break;
1307 case TARGET_SO_BROADCAST:
1308 optname = SO_BROADCAST;
1309 break;
1310 case TARGET_SO_SNDBUF:
1311 optname = SO_SNDBUF;
1312 break;
1313 case TARGET_SO_RCVBUF:
1314 optname = SO_RCVBUF;
1315 break;
1316 case TARGET_SO_KEEPALIVE:
1317 optname = SO_KEEPALIVE;
1318 break;
1319 case TARGET_SO_OOBINLINE:
1320 optname = SO_OOBINLINE;
1321 break;
1322 case TARGET_SO_NO_CHECK:
1323 optname = SO_NO_CHECK;
1324 break;
1325 case TARGET_SO_PRIORITY:
1326 optname = SO_PRIORITY;
1327 break;
bellard5e83e8e2005-03-01 22:32:06 +00001328#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001329 case TARGET_SO_BSDCOMPAT:
1330 optname = SO_BSDCOMPAT;
1331 break;
bellard5e83e8e2005-03-01 22:32:06 +00001332#endif
bellard3532fa72006-06-24 15:06:03 +00001333 case TARGET_SO_PASSCRED:
1334 optname = SO_PASSCRED;
1335 break;
1336 case TARGET_SO_TIMESTAMP:
1337 optname = SO_TIMESTAMP;
1338 break;
1339 case TARGET_SO_RCVLOWAT:
1340 optname = SO_RCVLOWAT;
1341 break;
1342 case TARGET_SO_RCVTIMEO:
1343 optname = SO_RCVTIMEO;
1344 break;
1345 case TARGET_SO_SNDTIMEO:
1346 optname = SO_SNDTIMEO;
1347 break;
bellard8853f862004-02-22 14:57:26 +00001348 break;
1349 default:
1350 goto unimplemented;
1351 }
bellard3532fa72006-06-24 15:06:03 +00001352 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001353 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001354
bellard2f619692007-11-16 10:46:05 +00001355 if (get_user_u32(val, optval_addr))
1356 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001357 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001358 break;
bellard7854b052003-03-29 17:22:23 +00001359 default:
bellard8853f862004-02-22 14:57:26 +00001360 unimplemented:
1361 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001362 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001363 }
bellard8853f862004-02-22 14:57:26 +00001364 return ret;
bellard7854b052003-03-29 17:22:23 +00001365}
1366
ths0da46a62007-10-20 20:23:07 +00001367/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001368static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001369 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001370{
blueswir1992f48a2007-10-14 16:27:31 +00001371 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001372 int len, val;
1373 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001374
1375 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001376 case TARGET_SOL_SOCKET:
1377 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +00001378 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +00001379 case TARGET_SO_LINGER:
1380 case TARGET_SO_RCVTIMEO:
1381 case TARGET_SO_SNDTIMEO:
1382 case TARGET_SO_PEERCRED:
1383 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +00001384 /* These don't just return a single integer */
1385 goto unimplemented;
1386 default:
bellard2efbe912005-07-23 15:10:20 +00001387 goto int_case;
1388 }
1389 break;
1390 case SOL_TCP:
1391 /* TCP options all take an 'int' value. */
1392 int_case:
bellard2f619692007-11-16 10:46:05 +00001393 if (get_user_u32(len, optlen))
1394 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001395 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001396 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +00001397 lv = sizeof(int);
1398 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1399 if (ret < 0)
1400 return ret;
bellard2efbe912005-07-23 15:10:20 +00001401 if (len > lv)
1402 len = lv;
bellard2f619692007-11-16 10:46:05 +00001403 if (len == 4) {
1404 if (put_user_u32(val, optval_addr))
1405 return -TARGET_EFAULT;
1406 } else {
1407 if (put_user_u8(val, optval_addr))
1408 return -TARGET_EFAULT;
1409 }
1410 if (put_user_u32(len, optlen))
1411 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001412 break;
1413 case SOL_IP:
1414 switch(optname) {
1415 case IP_TOS:
1416 case IP_TTL:
1417 case IP_HDRINCL:
1418 case IP_ROUTER_ALERT:
1419 case IP_RECVOPTS:
1420 case IP_RETOPTS:
1421 case IP_PKTINFO:
1422 case IP_MTU_DISCOVER:
1423 case IP_RECVERR:
1424 case IP_RECVTOS:
1425#ifdef IP_FREEBIND
1426 case IP_FREEBIND:
1427#endif
1428 case IP_MULTICAST_TTL:
1429 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001430 if (get_user_u32(len, optlen))
1431 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001432 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001433 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +00001434 lv = sizeof(int);
1435 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1436 if (ret < 0)
1437 return ret;
bellard2efbe912005-07-23 15:10:20 +00001438 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001439 len = 1;
bellard2f619692007-11-16 10:46:05 +00001440 if (put_user_u32(len, optlen)
1441 || put_user_u8(val, optval_addr))
1442 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001443 } else {
bellard2efbe912005-07-23 15:10:20 +00001444 if (len > sizeof(int))
1445 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001446 if (put_user_u32(len, optlen)
1447 || put_user_u32(val, optval_addr))
1448 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001449 }
bellard8853f862004-02-22 14:57:26 +00001450 break;
bellard2efbe912005-07-23 15:10:20 +00001451 default:
thsc02f4992007-12-18 02:39:59 +00001452 ret = -TARGET_ENOPROTOOPT;
1453 break;
bellard8853f862004-02-22 14:57:26 +00001454 }
1455 break;
1456 default:
1457 unimplemented:
1458 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1459 level, optname);
thsc02f4992007-12-18 02:39:59 +00001460 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001461 break;
1462 }
1463 return ret;
bellard7854b052003-03-29 17:22:23 +00001464}
1465
bellard579a97f2007-11-11 14:26:47 +00001466/* FIXME
1467 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1468 * other lock functions have a return code of 0 for failure.
1469 */
1470static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1471 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001472{
1473 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001474 abi_ulong base;
balrogd732dcb2008-10-28 10:21:03 +00001475 int i;
pbrook53a59602006-03-25 19:31:22 +00001476
bellard579a97f2007-11-11 14:26:47 +00001477 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1478 if (!target_vec)
1479 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001480 for(i = 0;i < count; i++) {
1481 base = tswapl(target_vec[i].iov_base);
1482 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001483 if (vec[i].iov_len != 0) {
1484 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrogd732dcb2008-10-28 10:21:03 +00001485 /* Don't check lock_user return value. We must call writev even
1486 if a element has invalid base address. */
bellard41df8412008-02-04 22:26:57 +00001487 } else {
1488 /* zero length pointer is ignored */
1489 vec[i].iov_base = NULL;
1490 }
pbrook53a59602006-03-25 19:31:22 +00001491 }
1492 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001493 return 0;
pbrook53a59602006-03-25 19:31:22 +00001494}
1495
bellard579a97f2007-11-11 14:26:47 +00001496static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1497 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001498{
1499 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001500 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001501 int i;
1502
bellard579a97f2007-11-11 14:26:47 +00001503 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1504 if (!target_vec)
1505 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001506 for(i = 0;i < count; i++) {
balrogd732dcb2008-10-28 10:21:03 +00001507 if (target_vec[i].iov_base) {
1508 base = tswapl(target_vec[i].iov_base);
1509 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1510 }
pbrook53a59602006-03-25 19:31:22 +00001511 }
1512 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001513
1514 return 0;
pbrook53a59602006-03-25 19:31:22 +00001515}
1516
ths0da46a62007-10-20 20:23:07 +00001517/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001518static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001519{
1520#if defined(TARGET_MIPS)
1521 switch(type) {
1522 case TARGET_SOCK_DGRAM:
1523 type = SOCK_DGRAM;
1524 break;
1525 case TARGET_SOCK_STREAM:
1526 type = SOCK_STREAM;
1527 break;
1528 case TARGET_SOCK_RAW:
1529 type = SOCK_RAW;
1530 break;
1531 case TARGET_SOCK_RDM:
1532 type = SOCK_RDM;
1533 break;
1534 case TARGET_SOCK_SEQPACKET:
1535 type = SOCK_SEQPACKET;
1536 break;
1537 case TARGET_SOCK_PACKET:
1538 type = SOCK_PACKET;
1539 break;
1540 }
1541#endif
balrog12bc92a2007-10-30 21:06:14 +00001542 if (domain == PF_NETLINK)
1543 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001544 return get_errno(socket(domain, type, protocol));
1545}
1546
ths0da46a62007-10-20 20:23:07 +00001547/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001548static abi_long do_bind(int sockfd, abi_ulong target_addr,
1549 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001550{
aurel328f7aeaf2009-01-30 19:47:57 +00001551 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001552 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001553
aurel32be09ac42009-04-15 16:12:06 +00001554 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001555 return -TARGET_EINVAL;
1556
aurel32607175e2009-04-15 16:11:59 +00001557 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001558
Arnaud Patard917507b2009-06-19 10:44:45 +03001559 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1560 if (ret)
1561 return ret;
1562
bellard3532fa72006-06-24 15:06:03 +00001563 return get_errno(bind(sockfd, addr, addrlen));
1564}
1565
ths0da46a62007-10-20 20:23:07 +00001566/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001567static abi_long do_connect(int sockfd, abi_ulong target_addr,
1568 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001569{
aurel328f7aeaf2009-01-30 19:47:57 +00001570 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001571 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001572
aurel32be09ac42009-04-15 16:12:06 +00001573 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001574 return -TARGET_EINVAL;
1575
1576 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001577
Arnaud Patard917507b2009-06-19 10:44:45 +03001578 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1579 if (ret)
1580 return ret;
1581
bellard3532fa72006-06-24 15:06:03 +00001582 return get_errno(connect(sockfd, addr, addrlen));
1583}
1584
ths0da46a62007-10-20 20:23:07 +00001585/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001586static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1587 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001588{
balrog6de645c2008-10-28 10:26:29 +00001589 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001590 struct target_msghdr *msgp;
1591 struct msghdr msg;
1592 int count;
1593 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001594 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001595
bellard579a97f2007-11-11 14:26:47 +00001596 /* FIXME */
1597 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1598 msgp,
1599 target_msg,
1600 send ? 1 : 0))
1601 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001602 if (msgp->msg_name) {
1603 msg.msg_namelen = tswap32(msgp->msg_namelen);
1604 msg.msg_name = alloca(msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001605 ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001606 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001607 if (ret) {
1608 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1609 return ret;
1610 }
bellard3532fa72006-06-24 15:06:03 +00001611 } else {
1612 msg.msg_name = NULL;
1613 msg.msg_namelen = 0;
1614 }
1615 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1616 msg.msg_control = alloca(msg.msg_controllen);
1617 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001618
bellard3532fa72006-06-24 15:06:03 +00001619 count = tswapl(msgp->msg_iovlen);
1620 vec = alloca(count * sizeof(struct iovec));
1621 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001622 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001623 msg.msg_iovlen = count;
1624 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001625
bellard3532fa72006-06-24 15:06:03 +00001626 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001627 ret = target_to_host_cmsg(&msg, msgp);
1628 if (ret == 0)
1629 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001630 } else {
1631 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001632 if (!is_error(ret)) {
1633 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001634 ret = host_to_target_cmsg(msgp, &msg);
balrog6de645c2008-10-28 10:26:29 +00001635 if (!is_error(ret))
1636 ret = len;
1637 }
bellard3532fa72006-06-24 15:06:03 +00001638 }
1639 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001640 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001641 return ret;
1642}
1643
ths0da46a62007-10-20 20:23:07 +00001644/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001645static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001646 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001647{
bellard2f619692007-11-16 10:46:05 +00001648 socklen_t addrlen;
1649 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001650 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001651
Arnaud Patard917507b2009-06-19 10:44:45 +03001652 if (target_addr == 0)
1653 return get_errno(accept(fd, NULL, NULL));
1654
1655 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00001656 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03001657 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00001658
aurel32be09ac42009-04-15 16:12:06 +00001659 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001660 return -TARGET_EINVAL;
1661
Arnaud Patard917507b2009-06-19 10:44:45 +03001662 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1663 return -TARGET_EINVAL;
1664
bellard2f619692007-11-16 10:46:05 +00001665 addr = alloca(addrlen);
1666
pbrook1be9e1d2006-11-19 15:26:04 +00001667 ret = get_errno(accept(fd, addr, &addrlen));
1668 if (!is_error(ret)) {
1669 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001670 if (put_user_u32(addrlen, target_addrlen_addr))
1671 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001672 }
1673 return ret;
1674}
1675
ths0da46a62007-10-20 20:23:07 +00001676/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001677static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001678 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001679{
bellard2f619692007-11-16 10:46:05 +00001680 socklen_t addrlen;
1681 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001682 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001683
bellard2f619692007-11-16 10:46:05 +00001684 if (get_user_u32(addrlen, target_addrlen_addr))
1685 return -TARGET_EFAULT;
1686
aurel32be09ac42009-04-15 16:12:06 +00001687 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001688 return -TARGET_EINVAL;
1689
Arnaud Patard917507b2009-06-19 10:44:45 +03001690 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1691 return -TARGET_EFAULT;
1692
bellard2f619692007-11-16 10:46:05 +00001693 addr = alloca(addrlen);
1694
pbrook1be9e1d2006-11-19 15:26:04 +00001695 ret = get_errno(getpeername(fd, addr, &addrlen));
1696 if (!is_error(ret)) {
1697 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001698 if (put_user_u32(addrlen, target_addrlen_addr))
1699 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001700 }
1701 return ret;
1702}
1703
ths0da46a62007-10-20 20:23:07 +00001704/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001705static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001706 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001707{
bellard2f619692007-11-16 10:46:05 +00001708 socklen_t addrlen;
1709 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001710 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001711
bellard2f619692007-11-16 10:46:05 +00001712 if (get_user_u32(addrlen, target_addrlen_addr))
1713 return -TARGET_EFAULT;
1714
aurel32be09ac42009-04-15 16:12:06 +00001715 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001716 return -TARGET_EINVAL;
1717
Arnaud Patard917507b2009-06-19 10:44:45 +03001718 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1719 return -TARGET_EFAULT;
1720
bellard2f619692007-11-16 10:46:05 +00001721 addr = alloca(addrlen);
1722
pbrook1be9e1d2006-11-19 15:26:04 +00001723 ret = get_errno(getsockname(fd, addr, &addrlen));
1724 if (!is_error(ret)) {
1725 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001726 if (put_user_u32(addrlen, target_addrlen_addr))
1727 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001728 }
1729 return ret;
1730}
1731
ths0da46a62007-10-20 20:23:07 +00001732/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001733static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001734 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001735{
1736 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001737 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001738
1739 ret = get_errno(socketpair(domain, type, protocol, tab));
1740 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001741 if (put_user_s32(tab[0], target_tab_addr)
1742 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1743 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001744 }
1745 return ret;
1746}
1747
ths0da46a62007-10-20 20:23:07 +00001748/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001749static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1750 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001751{
1752 void *addr;
1753 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001754 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001755
aurel32be09ac42009-04-15 16:12:06 +00001756 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001757 return -TARGET_EINVAL;
1758
bellard579a97f2007-11-11 14:26:47 +00001759 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1760 if (!host_msg)
1761 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001762 if (target_addr) {
1763 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001764 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1765 if (ret) {
1766 unlock_user(host_msg, msg, 0);
1767 return ret;
1768 }
pbrook1be9e1d2006-11-19 15:26:04 +00001769 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1770 } else {
1771 ret = get_errno(send(fd, host_msg, len, flags));
1772 }
1773 unlock_user(host_msg, msg, 0);
1774 return ret;
1775}
1776
ths0da46a62007-10-20 20:23:07 +00001777/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001778static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1779 abi_ulong target_addr,
1780 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001781{
1782 socklen_t addrlen;
1783 void *addr;
1784 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001785 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001786
bellard579a97f2007-11-11 14:26:47 +00001787 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1788 if (!host_msg)
1789 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001790 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001791 if (get_user_u32(addrlen, target_addrlen)) {
1792 ret = -TARGET_EFAULT;
1793 goto fail;
1794 }
aurel32be09ac42009-04-15 16:12:06 +00001795 if (addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001796 ret = -TARGET_EINVAL;
1797 goto fail;
1798 }
pbrook1be9e1d2006-11-19 15:26:04 +00001799 addr = alloca(addrlen);
1800 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1801 } else {
1802 addr = NULL; /* To keep compiler quiet. */
1803 ret = get_errno(recv(fd, host_msg, len, flags));
1804 }
1805 if (!is_error(ret)) {
1806 if (target_addr) {
1807 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001808 if (put_user_u32(addrlen, target_addrlen)) {
1809 ret = -TARGET_EFAULT;
1810 goto fail;
1811 }
pbrook1be9e1d2006-11-19 15:26:04 +00001812 }
1813 unlock_user(host_msg, msg, len);
1814 } else {
bellard2f619692007-11-16 10:46:05 +00001815fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001816 unlock_user(host_msg, msg, 0);
1817 }
1818 return ret;
1819}
1820
j_mayer32407102007-09-26 23:01:49 +00001821#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001822/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001823static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001824{
blueswir1992f48a2007-10-14 16:27:31 +00001825 abi_long ret;
1826 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001827
1828 switch(num) {
1829 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001830 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001831 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001832
Ulrich Hecht98818182009-07-03 17:09:28 +02001833 if (get_user_ual(domain, vptr)
1834 || get_user_ual(type, vptr + n)
1835 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001836 return -TARGET_EFAULT;
1837
bellard3532fa72006-06-24 15:06:03 +00001838 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001839 }
bellard31e31b82003-02-18 22:55:36 +00001840 break;
1841 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001842 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001843 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001844 abi_ulong target_addr;
1845 socklen_t addrlen;
1846
Ulrich Hecht98818182009-07-03 17:09:28 +02001847 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001848 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001849 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001850 return -TARGET_EFAULT;
1851
bellard3532fa72006-06-24 15:06:03 +00001852 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001853 }
bellard31e31b82003-02-18 22:55:36 +00001854 break;
1855 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001856 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001857 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001858 abi_ulong target_addr;
1859 socklen_t addrlen;
1860
Ulrich Hecht98818182009-07-03 17:09:28 +02001861 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001862 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001863 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001864 return -TARGET_EFAULT;
1865
bellard3532fa72006-06-24 15:06:03 +00001866 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001867 }
bellard31e31b82003-02-18 22:55:36 +00001868 break;
1869 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001870 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001871 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00001872
Ulrich Hecht98818182009-07-03 17:09:28 +02001873 if (get_user_ual(sockfd, vptr)
1874 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00001875 return -TARGET_EFAULT;
1876
bellard7854b052003-03-29 17:22:23 +00001877 ret = get_errno(listen(sockfd, backlog));
1878 }
bellard31e31b82003-02-18 22:55:36 +00001879 break;
1880 case SOCKOP_accept:
1881 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001882 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001883 abi_ulong target_addr, target_addrlen;
1884
Ulrich Hecht98818182009-07-03 17:09:28 +02001885 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001886 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001887 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001888 return -TARGET_EFAULT;
1889
pbrook1be9e1d2006-11-19 15:26:04 +00001890 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001891 }
1892 break;
1893 case SOCKOP_getsockname:
1894 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001895 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001896 abi_ulong target_addr, target_addrlen;
1897
Ulrich Hecht98818182009-07-03 17:09:28 +02001898 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001899 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001900 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001901 return -TARGET_EFAULT;
1902
pbrook1be9e1d2006-11-19 15:26:04 +00001903 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001904 }
1905 break;
1906 case SOCKOP_getpeername:
1907 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001908 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001909 abi_ulong target_addr, target_addrlen;
1910
Ulrich Hecht98818182009-07-03 17:09:28 +02001911 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001912 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001913 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00001914 return -TARGET_EFAULT;
1915
pbrook1be9e1d2006-11-19 15:26:04 +00001916 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001917 }
1918 break;
1919 case SOCKOP_socketpair:
1920 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001921 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00001922 abi_ulong tab;
1923
Ulrich Hecht98818182009-07-03 17:09:28 +02001924 if (get_user_ual(domain, vptr)
1925 || get_user_ual(type, vptr + n)
1926 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00001927 || get_user_ual(tab, vptr + 3 * n))
1928 return -TARGET_EFAULT;
1929
pbrook1be9e1d2006-11-19 15:26:04 +00001930 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001931 }
1932 break;
1933 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001934 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001935 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001936 abi_ulong msg;
1937 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001938 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001939
Ulrich Hecht98818182009-07-03 17:09:28 +02001940 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001941 || get_user_ual(msg, vptr + n)
1942 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001943 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00001944 return -TARGET_EFAULT;
1945
pbrook1be9e1d2006-11-19 15:26:04 +00001946 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001947 }
bellard31e31b82003-02-18 22:55:36 +00001948 break;
1949 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001950 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001951 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001952 abi_ulong msg;
1953 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001954 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001955
Ulrich Hecht98818182009-07-03 17:09:28 +02001956 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001957 || get_user_ual(msg, vptr + n)
1958 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001959 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00001960 return -TARGET_EFAULT;
1961
pbrook1be9e1d2006-11-19 15:26:04 +00001962 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001963 }
bellard31e31b82003-02-18 22:55:36 +00001964 break;
1965 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001966 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001967 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001968 abi_ulong msg;
1969 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001970 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001971 abi_ulong addr;
1972 socklen_t addrlen;
1973
Ulrich Hecht98818182009-07-03 17:09:28 +02001974 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001975 || get_user_ual(msg, vptr + n)
1976 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001977 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00001978 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001979 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00001980 return -TARGET_EFAULT;
1981
pbrook1be9e1d2006-11-19 15:26:04 +00001982 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001983 }
bellard31e31b82003-02-18 22:55:36 +00001984 break;
1985 case SOCKOP_recvfrom:
1986 {
Ulrich Hecht98818182009-07-03 17:09:28 +02001987 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00001988 abi_ulong msg;
1989 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02001990 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00001991 abi_ulong addr;
1992 socklen_t addrlen;
1993
Ulrich Hecht98818182009-07-03 17:09:28 +02001994 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00001995 || get_user_ual(msg, vptr + n)
1996 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001997 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00001998 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02001999 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002000 return -TARGET_EFAULT;
2001
pbrook1be9e1d2006-11-19 15:26:04 +00002002 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00002003 }
2004 break;
2005 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00002006 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002007 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00002008
Ulrich Hecht98818182009-07-03 17:09:28 +02002009 if (get_user_ual(sockfd, vptr)
2010 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002011 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002012
2013 ret = get_errno(shutdown(sockfd, how));
2014 }
bellard31e31b82003-02-18 22:55:36 +00002015 break;
2016 case SOCKOP_sendmsg:
2017 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002018 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002019 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002020 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002021 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002022
Ulrich Hecht98818182009-07-03 17:09:28 +02002023 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002024 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002025 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002026 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002027
ths5fafdf22007-09-16 21:08:06 +00002028 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002029 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002030 }
2031 break;
bellard31e31b82003-02-18 22:55:36 +00002032 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002033 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002034 abi_ulong sockfd;
2035 abi_ulong level;
2036 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002037 abi_ulong optval;
2038 socklen_t optlen;
2039
Ulrich Hecht98818182009-07-03 17:09:28 +02002040 if (get_user_ual(sockfd, vptr)
2041 || get_user_ual(level, vptr + n)
2042 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002043 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002044 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002045 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002046
2047 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2048 }
2049 break;
bellard31e31b82003-02-18 22:55:36 +00002050 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002051 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002052 abi_ulong sockfd;
2053 abi_ulong level;
2054 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002055 abi_ulong optval;
2056 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002057
Ulrich Hecht98818182009-07-03 17:09:28 +02002058 if (get_user_ual(sockfd, vptr)
2059 || get_user_ual(level, vptr + n)
2060 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002061 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002062 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002063 return -TARGET_EFAULT;
2064
2065 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002066 }
2067 break;
bellard31e31b82003-02-18 22:55:36 +00002068 default:
2069 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002070 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002071 break;
2072 }
2073 return ret;
2074}
j_mayer32407102007-09-26 23:01:49 +00002075#endif
bellard31e31b82003-02-18 22:55:36 +00002076
bellard8853f862004-02-22 14:57:26 +00002077#define N_SHM_REGIONS 32
2078
2079static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002080 abi_ulong start;
2081 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002082} shm_regions[N_SHM_REGIONS];
2083
ths3eb6b042007-06-03 14:26:27 +00002084struct target_ipc_perm
2085{
blueswir1992f48a2007-10-14 16:27:31 +00002086 abi_long __key;
2087 abi_ulong uid;
2088 abi_ulong gid;
2089 abi_ulong cuid;
2090 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002091 unsigned short int mode;
2092 unsigned short int __pad1;
2093 unsigned short int __seq;
2094 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002095 abi_ulong __unused1;
2096 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002097};
2098
2099struct target_semid_ds
2100{
2101 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002102 abi_ulong sem_otime;
2103 abi_ulong __unused1;
2104 abi_ulong sem_ctime;
2105 abi_ulong __unused2;
2106 abi_ulong sem_nsems;
2107 abi_ulong __unused3;
2108 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002109};
2110
bellard579a97f2007-11-11 14:26:47 +00002111static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2112 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002113{
2114 struct target_ipc_perm *target_ip;
2115 struct target_semid_ds *target_sd;
2116
bellard579a97f2007-11-11 14:26:47 +00002117 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2118 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002119 target_ip = &(target_sd->sem_perm);
ths3eb6b042007-06-03 14:26:27 +00002120 host_ip->__key = tswapl(target_ip->__key);
2121 host_ip->uid = tswapl(target_ip->uid);
2122 host_ip->gid = tswapl(target_ip->gid);
2123 host_ip->cuid = tswapl(target_ip->cuid);
2124 host_ip->cgid = tswapl(target_ip->cgid);
2125 host_ip->mode = tswapl(target_ip->mode);
2126 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002127 return 0;
ths3eb6b042007-06-03 14:26:27 +00002128}
2129
bellard579a97f2007-11-11 14:26:47 +00002130static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2131 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002132{
2133 struct target_ipc_perm *target_ip;
2134 struct target_semid_ds *target_sd;
2135
bellard579a97f2007-11-11 14:26:47 +00002136 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2137 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002138 target_ip = &(target_sd->sem_perm);
2139 target_ip->__key = tswapl(host_ip->__key);
2140 target_ip->uid = tswapl(host_ip->uid);
2141 target_ip->gid = tswapl(host_ip->gid);
2142 target_ip->cuid = tswapl(host_ip->cuid);
2143 target_ip->cgid = tswapl(host_ip->cgid);
2144 target_ip->mode = tswapl(host_ip->mode);
2145 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002146 return 0;
ths3eb6b042007-06-03 14:26:27 +00002147}
2148
bellard579a97f2007-11-11 14:26:47 +00002149static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2150 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002151{
2152 struct target_semid_ds *target_sd;
2153
bellard579a97f2007-11-11 14:26:47 +00002154 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2155 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002156 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2157 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002158 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2159 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2160 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2161 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002162 return 0;
ths3eb6b042007-06-03 14:26:27 +00002163}
2164
bellard579a97f2007-11-11 14:26:47 +00002165static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2166 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002167{
2168 struct target_semid_ds *target_sd;
2169
bellard579a97f2007-11-11 14:26:47 +00002170 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2171 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002172 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2173 return -TARGET_EFAULT;;
ths3eb6b042007-06-03 14:26:27 +00002174 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2175 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2176 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2177 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002178 return 0;
ths3eb6b042007-06-03 14:26:27 +00002179}
2180
aurel32e5289082009-04-18 16:16:12 +00002181struct target_seminfo {
2182 int semmap;
2183 int semmni;
2184 int semmns;
2185 int semmnu;
2186 int semmsl;
2187 int semopm;
2188 int semume;
2189 int semusz;
2190 int semvmx;
2191 int semaem;
2192};
2193
2194static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2195 struct seminfo *host_seminfo)
2196{
2197 struct target_seminfo *target_seminfo;
2198 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2199 return -TARGET_EFAULT;
2200 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2201 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2202 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2203 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2204 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2205 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2206 __put_user(host_seminfo->semume, &target_seminfo->semume);
2207 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2208 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2209 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2210 unlock_user_struct(target_seminfo, target_addr, 1);
2211 return 0;
2212}
2213
thsfa294812007-02-02 22:05:00 +00002214union semun {
2215 int val;
ths3eb6b042007-06-03 14:26:27 +00002216 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002217 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002218 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002219};
2220
ths3eb6b042007-06-03 14:26:27 +00002221union target_semun {
2222 int val;
aurel32e5289082009-04-18 16:16:12 +00002223 abi_ulong buf;
2224 abi_ulong array;
2225 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002226};
2227
aurel32e5289082009-04-18 16:16:12 +00002228static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2229 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002230{
aurel32e5289082009-04-18 16:16:12 +00002231 int nsems;
2232 unsigned short *array;
2233 union semun semun;
2234 struct semid_ds semid_ds;
2235 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002236
aurel32e5289082009-04-18 16:16:12 +00002237 semun.buf = &semid_ds;
2238
2239 ret = semctl(semid, 0, IPC_STAT, semun);
2240 if (ret == -1)
2241 return get_errno(ret);
2242
2243 nsems = semid_ds.sem_nsems;
2244
2245 *host_array = malloc(nsems*sizeof(unsigned short));
2246 array = lock_user(VERIFY_READ, target_addr,
2247 nsems*sizeof(unsigned short), 1);
2248 if (!array)
2249 return -TARGET_EFAULT;
2250
2251 for(i=0; i<nsems; i++) {
2252 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002253 }
aurel32e5289082009-04-18 16:16:12 +00002254 unlock_user(array, target_addr, 0);
2255
bellard579a97f2007-11-11 14:26:47 +00002256 return 0;
ths3eb6b042007-06-03 14:26:27 +00002257}
2258
aurel32e5289082009-04-18 16:16:12 +00002259static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2260 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002261{
aurel32e5289082009-04-18 16:16:12 +00002262 int nsems;
2263 unsigned short *array;
2264 union semun semun;
2265 struct semid_ds semid_ds;
2266 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002267
aurel32e5289082009-04-18 16:16:12 +00002268 semun.buf = &semid_ds;
2269
2270 ret = semctl(semid, 0, IPC_STAT, semun);
2271 if (ret == -1)
2272 return get_errno(ret);
2273
2274 nsems = semid_ds.sem_nsems;
2275
2276 array = lock_user(VERIFY_WRITE, target_addr,
2277 nsems*sizeof(unsigned short), 0);
2278 if (!array)
2279 return -TARGET_EFAULT;
2280
2281 for(i=0; i<nsems; i++) {
2282 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002283 }
aurel32e5289082009-04-18 16:16:12 +00002284 free(*host_array);
2285 unlock_user(array, target_addr, 1);
2286
bellard579a97f2007-11-11 14:26:47 +00002287 return 0;
ths3eb6b042007-06-03 14:26:27 +00002288}
2289
aurel32e5289082009-04-18 16:16:12 +00002290static inline abi_long do_semctl(int semid, int semnum, int cmd,
2291 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002292{
2293 union semun arg;
2294 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302295 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002296 struct seminfo seminfo;
2297 abi_long ret = -TARGET_EINVAL;
2298 abi_long err;
2299 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002300
2301 switch( cmd ) {
2302 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002303 case SETVAL:
aurel32e5289082009-04-18 16:16:12 +00002304 arg.val = tswapl(target_su.val);
2305 ret = get_errno(semctl(semid, semnum, cmd, arg));
2306 target_su.val = tswapl(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002307 break;
2308 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002309 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002310 err = target_to_host_semarray(semid, &array, target_su.array);
2311 if (err)
2312 return err;
2313 arg.array = array;
2314 ret = get_errno(semctl(semid, semnum, cmd, arg));
2315 err = host_to_target_semarray(semid, target_su.array, &array);
2316 if (err)
2317 return err;
ths3eb6b042007-06-03 14:26:27 +00002318 break;
2319 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002320 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002321 case SEM_STAT:
2322 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2323 if (err)
2324 return err;
2325 arg.buf = &dsarg;
2326 ret = get_errno(semctl(semid, semnum, cmd, arg));
2327 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2328 if (err)
2329 return err;
ths3eb6b042007-06-03 14:26:27 +00002330 break;
aurel32e5289082009-04-18 16:16:12 +00002331 case IPC_INFO:
2332 case SEM_INFO:
2333 arg.__buf = &seminfo;
2334 ret = get_errno(semctl(semid, semnum, cmd, arg));
2335 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2336 if (err)
2337 return err;
2338 break;
2339 case IPC_RMID:
2340 case GETPID:
2341 case GETNCNT:
2342 case GETZCNT:
2343 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2344 break;
ths3eb6b042007-06-03 14:26:27 +00002345 }
2346
2347 return ret;
2348}
2349
aurel32e5289082009-04-18 16:16:12 +00002350struct target_sembuf {
2351 unsigned short sem_num;
2352 short sem_op;
2353 short sem_flg;
2354};
2355
2356static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2357 abi_ulong target_addr,
2358 unsigned nsops)
2359{
2360 struct target_sembuf *target_sembuf;
2361 int i;
2362
2363 target_sembuf = lock_user(VERIFY_READ, target_addr,
2364 nsops*sizeof(struct target_sembuf), 1);
2365 if (!target_sembuf)
2366 return -TARGET_EFAULT;
2367
2368 for(i=0; i<nsops; i++) {
2369 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2370 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2371 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2372 }
2373
2374 unlock_user(target_sembuf, target_addr, 0);
2375
2376 return 0;
2377}
2378
2379static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2380{
2381 struct sembuf sops[nsops];
2382
2383 if (target_to_host_sembuf(sops, ptr, nsops))
2384 return -TARGET_EFAULT;
2385
2386 return semop(semid, sops, nsops);
2387}
2388
ths1bc012f2007-06-03 14:27:49 +00002389struct target_msqid_ds
2390{
aurel321c54ff92008-10-13 21:08:44 +00002391 struct target_ipc_perm msg_perm;
2392 abi_ulong msg_stime;
2393#if TARGET_ABI_BITS == 32
2394 abi_ulong __unused1;
2395#endif
2396 abi_ulong msg_rtime;
2397#if TARGET_ABI_BITS == 32
2398 abi_ulong __unused2;
2399#endif
2400 abi_ulong msg_ctime;
2401#if TARGET_ABI_BITS == 32
2402 abi_ulong __unused3;
2403#endif
2404 abi_ulong __msg_cbytes;
2405 abi_ulong msg_qnum;
2406 abi_ulong msg_qbytes;
2407 abi_ulong msg_lspid;
2408 abi_ulong msg_lrpid;
2409 abi_ulong __unused4;
2410 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002411};
2412
bellard579a97f2007-11-11 14:26:47 +00002413static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2414 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002415{
2416 struct target_msqid_ds *target_md;
2417
bellard579a97f2007-11-11 14:26:47 +00002418 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2419 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002420 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2421 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002422 host_md->msg_stime = tswapl(target_md->msg_stime);
2423 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2424 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2425 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2426 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2427 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2428 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2429 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2430 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002431 return 0;
ths1bc012f2007-06-03 14:27:49 +00002432}
2433
bellard579a97f2007-11-11 14:26:47 +00002434static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2435 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002436{
2437 struct target_msqid_ds *target_md;
2438
bellard579a97f2007-11-11 14:26:47 +00002439 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2440 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002441 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2442 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002443 target_md->msg_stime = tswapl(host_md->msg_stime);
2444 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2445 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2446 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2447 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2448 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2449 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2450 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2451 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002452 return 0;
ths1bc012f2007-06-03 14:27:49 +00002453}
2454
aurel321c54ff92008-10-13 21:08:44 +00002455struct target_msginfo {
2456 int msgpool;
2457 int msgmap;
2458 int msgmax;
2459 int msgmnb;
2460 int msgmni;
2461 int msgssz;
2462 int msgtql;
2463 unsigned short int msgseg;
2464};
2465
2466static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2467 struct msginfo *host_msginfo)
2468{
2469 struct target_msginfo *target_msginfo;
2470 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2471 return -TARGET_EFAULT;
2472 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2473 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2474 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2475 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2476 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2477 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2478 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2479 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2480 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002481 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002482}
2483
2484static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002485{
2486 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002487 struct msginfo msginfo;
2488 abi_long ret = -TARGET_EINVAL;
2489
2490 cmd &= 0xff;
2491
2492 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002493 case IPC_STAT:
2494 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002495 case MSG_STAT:
2496 if (target_to_host_msqid_ds(&dsarg,ptr))
2497 return -TARGET_EFAULT;
2498 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2499 if (host_to_target_msqid_ds(ptr,&dsarg))
2500 return -TARGET_EFAULT;
2501 break;
2502 case IPC_RMID:
2503 ret = get_errno(msgctl(msgid, cmd, NULL));
2504 break;
2505 case IPC_INFO:
2506 case MSG_INFO:
2507 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2508 if (host_to_target_msginfo(ptr, &msginfo))
2509 return -TARGET_EFAULT;
2510 break;
ths1bc012f2007-06-03 14:27:49 +00002511 }
aurel321c54ff92008-10-13 21:08:44 +00002512
ths1bc012f2007-06-03 14:27:49 +00002513 return ret;
2514}
2515
2516struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002517 abi_long mtype;
2518 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002519};
2520
blueswir1992f48a2007-10-14 16:27:31 +00002521static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2522 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002523{
2524 struct target_msgbuf *target_mb;
2525 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002526 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002527
bellard579a97f2007-11-11 14:26:47 +00002528 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2529 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002530 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002531 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2532 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002533 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2534 free(host_mb);
2535 unlock_user_struct(target_mb, msgp, 0);
2536
2537 return ret;
2538}
2539
blueswir1992f48a2007-10-14 16:27:31 +00002540static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002541 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002542 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002543{
2544 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002545 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002546 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002547 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002548
bellard579a97f2007-11-11 14:26:47 +00002549 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2550 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002551
ths1bc012f2007-06-03 14:27:49 +00002552 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002553 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2554
bellard579a97f2007-11-11 14:26:47 +00002555 if (ret > 0) {
2556 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2557 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2558 if (!target_mtext) {
2559 ret = -TARGET_EFAULT;
2560 goto end;
2561 }
aurel321c54ff92008-10-13 21:08:44 +00002562 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002563 unlock_user(target_mtext, target_mtext_addr, ret);
2564 }
aurel321c54ff92008-10-13 21:08:44 +00002565
ths1bc012f2007-06-03 14:27:49 +00002566 target_mb->mtype = tswapl(host_mb->mtype);
2567 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002568
bellard579a97f2007-11-11 14:26:47 +00002569end:
2570 if (target_mb)
2571 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00002572 return ret;
2573}
2574
Riku Voipio88a8c982009-04-03 10:42:00 +03002575struct target_shmid_ds
2576{
2577 struct target_ipc_perm shm_perm;
2578 abi_ulong shm_segsz;
2579 abi_ulong shm_atime;
2580#if TARGET_ABI_BITS == 32
2581 abi_ulong __unused1;
2582#endif
2583 abi_ulong shm_dtime;
2584#if TARGET_ABI_BITS == 32
2585 abi_ulong __unused2;
2586#endif
2587 abi_ulong shm_ctime;
2588#if TARGET_ABI_BITS == 32
2589 abi_ulong __unused3;
2590#endif
2591 int shm_cpid;
2592 int shm_lpid;
2593 abi_ulong shm_nattch;
2594 unsigned long int __unused4;
2595 unsigned long int __unused5;
2596};
2597
2598static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2599 abi_ulong target_addr)
2600{
2601 struct target_shmid_ds *target_sd;
2602
2603 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2604 return -TARGET_EFAULT;
2605 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2606 return -TARGET_EFAULT;
2607 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2608 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2609 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2610 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2611 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2612 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2613 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2614 unlock_user_struct(target_sd, target_addr, 0);
2615 return 0;
2616}
2617
2618static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2619 struct shmid_ds *host_sd)
2620{
2621 struct target_shmid_ds *target_sd;
2622
2623 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2624 return -TARGET_EFAULT;
2625 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2626 return -TARGET_EFAULT;
2627 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2628 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2629 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2630 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2631 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2632 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2633 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2634 unlock_user_struct(target_sd, target_addr, 1);
2635 return 0;
2636}
2637
2638struct target_shminfo {
2639 abi_ulong shmmax;
2640 abi_ulong shmmin;
2641 abi_ulong shmmni;
2642 abi_ulong shmseg;
2643 abi_ulong shmall;
2644};
2645
2646static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2647 struct shminfo *host_shminfo)
2648{
2649 struct target_shminfo *target_shminfo;
2650 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2651 return -TARGET_EFAULT;
2652 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2653 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2654 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2655 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2656 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2657 unlock_user_struct(target_shminfo, target_addr, 1);
2658 return 0;
2659}
2660
2661struct target_shm_info {
2662 int used_ids;
2663 abi_ulong shm_tot;
2664 abi_ulong shm_rss;
2665 abi_ulong shm_swp;
2666 abi_ulong swap_attempts;
2667 abi_ulong swap_successes;
2668};
2669
2670static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2671 struct shm_info *host_shm_info)
2672{
2673 struct target_shm_info *target_shm_info;
2674 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2675 return -TARGET_EFAULT;
2676 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2677 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2678 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2679 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2680 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2681 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2682 unlock_user_struct(target_shm_info, target_addr, 1);
2683 return 0;
2684}
2685
2686static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2687{
2688 struct shmid_ds dsarg;
2689 struct shminfo shminfo;
2690 struct shm_info shm_info;
2691 abi_long ret = -TARGET_EINVAL;
2692
2693 cmd &= 0xff;
2694
2695 switch(cmd) {
2696 case IPC_STAT:
2697 case IPC_SET:
2698 case SHM_STAT:
2699 if (target_to_host_shmid_ds(&dsarg, buf))
2700 return -TARGET_EFAULT;
2701 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2702 if (host_to_target_shmid_ds(buf, &dsarg))
2703 return -TARGET_EFAULT;
2704 break;
2705 case IPC_INFO:
2706 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2707 if (host_to_target_shminfo(buf, &shminfo))
2708 return -TARGET_EFAULT;
2709 break;
2710 case SHM_INFO:
2711 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2712 if (host_to_target_shm_info(buf, &shm_info))
2713 return -TARGET_EFAULT;
2714 break;
2715 case IPC_RMID:
2716 case SHM_LOCK:
2717 case SHM_UNLOCK:
2718 ret = get_errno(shmctl(shmid, cmd, NULL));
2719 break;
2720 }
2721
2722 return ret;
2723}
2724
2725static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2726{
2727 abi_long raddr;
2728 void *host_raddr;
2729 struct shmid_ds shm_info;
2730 int i,ret;
2731
2732 /* find out the length of the shared memory segment */
2733 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2734 if (is_error(ret)) {
2735 /* can't get length, bail out */
2736 return ret;
2737 }
2738
2739 mmap_lock();
2740
2741 if (shmaddr)
2742 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2743 else {
2744 abi_ulong mmap_start;
2745
2746 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2747
2748 if (mmap_start == -1) {
2749 errno = ENOMEM;
2750 host_raddr = (void *)-1;
2751 } else
2752 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2753 }
2754
2755 if (host_raddr == (void *)-1) {
2756 mmap_unlock();
2757 return get_errno((long)host_raddr);
2758 }
2759 raddr=h2g((unsigned long)host_raddr);
2760
2761 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2762 PAGE_VALID | PAGE_READ |
2763 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2764
2765 for (i = 0; i < N_SHM_REGIONS; i++) {
2766 if (shm_regions[i].start == 0) {
2767 shm_regions[i].start = raddr;
2768 shm_regions[i].size = shm_info.shm_segsz;
2769 break;
2770 }
2771 }
2772
2773 mmap_unlock();
2774 return raddr;
2775
2776}
2777
2778static inline abi_long do_shmdt(abi_ulong shmaddr)
2779{
2780 int i;
2781
2782 for (i = 0; i < N_SHM_REGIONS; ++i) {
2783 if (shm_regions[i].start == shmaddr) {
2784 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09002785 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03002786 break;
2787 }
2788 }
2789
2790 return get_errno(shmdt(g2h(shmaddr)));
2791}
2792
aurel321c54ff92008-10-13 21:08:44 +00002793#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00002794/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00002795/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002796static abi_long do_ipc(unsigned int call, int first,
2797 int second, int third,
2798 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00002799{
2800 int version;
blueswir1992f48a2007-10-14 16:27:31 +00002801 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00002802
2803 version = call >> 16;
2804 call &= 0xffff;
2805
2806 switch (call) {
thsfa294812007-02-02 22:05:00 +00002807 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00002808 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00002809 break;
2810
2811 case IPCOP_semget:
2812 ret = get_errno(semget(first, second, third));
2813 break;
2814
2815 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00002816 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00002817 break;
thsd96372e2007-02-02 22:05:44 +00002818
aurel321c54ff92008-10-13 21:08:44 +00002819 case IPCOP_msgget:
2820 ret = get_errno(msgget(first, second));
2821 break;
thsd96372e2007-02-02 22:05:44 +00002822
aurel321c54ff92008-10-13 21:08:44 +00002823 case IPCOP_msgsnd:
2824 ret = do_msgsnd(first, ptr, second, third);
2825 break;
thsd96372e2007-02-02 22:05:44 +00002826
aurel321c54ff92008-10-13 21:08:44 +00002827 case IPCOP_msgctl:
2828 ret = do_msgctl(first, second, ptr);
2829 break;
thsd96372e2007-02-02 22:05:44 +00002830
aurel321c54ff92008-10-13 21:08:44 +00002831 case IPCOP_msgrcv:
2832 switch (version) {
2833 case 0:
2834 {
2835 struct target_ipc_kludge {
2836 abi_long msgp;
2837 abi_long msgtyp;
2838 } *tmp;
thsd96372e2007-02-02 22:05:44 +00002839
aurel321c54ff92008-10-13 21:08:44 +00002840 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2841 ret = -TARGET_EFAULT;
2842 break;
ths1bc012f2007-06-03 14:27:49 +00002843 }
aurel321c54ff92008-10-13 21:08:44 +00002844
2845 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2846
2847 unlock_user_struct(tmp, ptr, 0);
2848 break;
2849 }
2850 default:
2851 ret = do_msgrcv(first, ptr, second, fifth, third);
2852 }
2853 break;
thsd96372e2007-02-02 22:05:44 +00002854
bellard8853f862004-02-22 14:57:26 +00002855 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03002856 switch (version) {
2857 default:
bellard5a4a8982007-11-11 17:39:18 +00002858 {
2859 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03002860 raddr = do_shmat(first, ptr, second);
2861 if (is_error(raddr))
2862 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00002863 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002864 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03002865 break;
2866 }
2867 case 1:
2868 ret = -TARGET_EINVAL;
2869 break;
bellard5a4a8982007-11-11 17:39:18 +00002870 }
bellard8853f862004-02-22 14:57:26 +00002871 break;
2872 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03002873 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00002874 break;
2875
2876 case IPCOP_shmget:
2877 /* IPC_* flag values are the same on all linux platforms */
2878 ret = get_errno(shmget(first, second, third));
2879 break;
2880
2881 /* IPC_* and SHM_* command values are the same on all linux platforms */
2882 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03002883 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00002884 break;
2885 default:
j_mayer32407102007-09-26 23:01:49 +00002886 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002887 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002888 break;
2889 }
2890 return ret;
2891}
j_mayer32407102007-09-26 23:01:49 +00002892#endif
bellard8853f862004-02-22 14:57:26 +00002893
bellard31e31b82003-02-18 22:55:36 +00002894/* kernel structure types definitions */
2895#define IFNAMSIZ 16
2896
Blue Swirl001faf32009-05-13 17:53:17 +00002897#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00002898#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2899enum {
2900#include "syscall_types.h"
2901};
2902#undef STRUCT
2903#undef STRUCT_SPECIAL
2904
Blue Swirl001faf32009-05-13 17:53:17 +00002905#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00002906#define STRUCT_SPECIAL(name)
2907#include "syscall_types.h"
2908#undef STRUCT
2909#undef STRUCT_SPECIAL
2910
2911typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002912 unsigned int target_cmd;
2913 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002914 const char *name;
2915 int access;
bellard1a9353d2003-03-16 20:28:50 +00002916 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002917} IOCTLEntry;
2918
2919#define IOC_R 0x0001
2920#define IOC_W 0x0002
2921#define IOC_RW (IOC_R | IOC_W)
2922
2923#define MAX_STRUCT_SIZE 4096
2924
blueswir19f106a72008-10-05 10:52:52 +00002925static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00002926#define IOCTL(cmd, access, ...) \
2927 { TARGET_ ## cmd, cmd, #cmd, access, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00002928#include "ioctls.h"
2929 { 0, 0, },
2930};
2931
pbrook53a59602006-03-25 19:31:22 +00002932/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002933/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002934static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002935{
2936 const IOCTLEntry *ie;
2937 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002938 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002939 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002940 int target_size;
2941 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002942
2943 ie = ioctl_entries;
2944 for(;;) {
2945 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00002946 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00002947 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002948 }
2949 if (ie->target_cmd == cmd)
2950 break;
2951 ie++;
2952 }
2953 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00002954#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00002955 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00002956#endif
bellard31e31b82003-02-18 22:55:36 +00002957 switch(arg_type[0]) {
2958 case TYPE_NULL:
2959 /* no argument */
2960 ret = get_errno(ioctl(fd, ie->host_cmd));
2961 break;
2962 case TYPE_PTRVOID:
2963 case TYPE_INT:
2964 /* int argment */
2965 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2966 break;
2967 case TYPE_PTR:
2968 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002969 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002970 switch(ie->access) {
2971 case IOC_R:
2972 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2973 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002974 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2975 if (!argptr)
2976 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002977 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2978 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002979 }
2980 break;
2981 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002982 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2983 if (!argptr)
2984 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002985 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2986 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002987 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2988 break;
2989 default:
2990 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002991 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2992 if (!argptr)
2993 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002994 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2995 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002996 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2997 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002998 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2999 if (!argptr)
3000 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003001 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3002 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003003 }
3004 break;
3005 }
3006 break;
3007 default:
j_mayer32407102007-09-26 23:01:49 +00003008 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3009 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003010 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003011 break;
3012 }
3013 return ret;
3014}
3015
blueswir1b39bc502008-10-05 10:51:10 +00003016static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003017 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3018 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3019 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3020 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3021 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3022 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3023 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3024 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3025 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3026 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3027 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3028 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3029 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3030 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3031 { 0, 0, 0, 0 }
3032};
3033
blueswir1b39bc502008-10-05 10:51:10 +00003034static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003035 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3036 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3037 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3038 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3039 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3040 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3041 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3042 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3043 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3044 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3045 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3046 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3047 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3048 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3049 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3050 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3051 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3052 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3053 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3054 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3055 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3056 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3057 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3058 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3059 { 0, 0, 0, 0 }
3060};
3061
blueswir1b39bc502008-10-05 10:51:10 +00003062static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003063 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3064 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3065 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3066 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3067 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3068 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3069 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3070 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3071 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3072 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3073 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3074 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3075 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3076 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3077 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3078 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3079 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3080 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3081 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3082 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3083 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3084 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3085 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3086 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3087 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3088 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3089 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3090 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3091 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3092 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3093 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3094 { 0, 0, 0, 0 }
3095};
3096
blueswir1b39bc502008-10-05 10:51:10 +00003097static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003098 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3099 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3100 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3101 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3102 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3103 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3104 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3105 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3106 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3107 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3108 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3109 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3110 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3111 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3112 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3113 { 0, 0, 0, 0 }
3114};
3115
3116static void target_to_host_termios (void *dst, const void *src)
3117{
3118 struct host_termios *host = dst;
3119 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003120
ths5fafdf22007-09-16 21:08:06 +00003121 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003122 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003123 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003124 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003125 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003126 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003127 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003128 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3129 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003130
Arnaud Patard44607122009-04-21 17:39:08 +03003131 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003132 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3133 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003134 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003135 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003136 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003137 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003138 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003139 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003140 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003141 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3142 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003143 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3144 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3145 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3146 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3147 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003148 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003149}
ths3b46e622007-09-17 08:09:54 +00003150
bellard31e31b82003-02-18 22:55:36 +00003151static void host_to_target_termios (void *dst, const void *src)
3152{
3153 struct target_termios *target = dst;
3154 const struct host_termios *host = src;
3155
ths5fafdf22007-09-16 21:08:06 +00003156 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003157 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003158 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003159 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003160 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003161 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003162 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003163 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3164 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003165
Arnaud Patard44607122009-04-21 17:39:08 +03003166 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003167 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3168 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3169 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3170 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3171 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3172 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3173 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3174 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3175 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3176 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3177 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3178 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3179 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3180 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3181 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3182 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3183 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3184}
3185
blueswir18e853dc2008-10-05 10:49:32 +00003186static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003187 .convert = { host_to_target_termios, target_to_host_termios },
3188 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3189 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3190};
3191
bellard5286db72003-06-05 00:57:30 +00003192static bitmask_transtbl mmap_flags_tbl[] = {
3193 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3194 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3195 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3196 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3197 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3198 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3199 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3200 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3201 { 0, 0, 0, 0 }
3202};
3203
bellard2ab83ea2003-06-15 19:56:46 +00003204#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003205
3206/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003207static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003208
bellard03acab62007-11-11 14:57:14 +00003209static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003210{
3211 int size;
pbrook53a59602006-03-25 19:31:22 +00003212 void *p;
bellard6dbad632003-03-16 18:05:05 +00003213
3214 if (!ldt_table)
3215 return 0;
3216 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3217 if (size > bytecount)
3218 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003219 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3220 if (!p)
bellard03acab62007-11-11 14:57:14 +00003221 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003222 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003223 memcpy(p, ldt_table, size);
3224 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003225 return size;
3226}
3227
3228/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003229static abi_long write_ldt(CPUX86State *env,
3230 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003231{
3232 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003233 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003234 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003235 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003236 uint32_t *lp, entry_1, entry_2;
3237
3238 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003239 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003240 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003241 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003242 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3243 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3244 ldt_info.limit = tswap32(target_ldt_info->limit);
3245 ldt_info.flags = tswap32(target_ldt_info->flags);
3246 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003247
bellard6dbad632003-03-16 18:05:05 +00003248 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003249 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003250 seg_32bit = ldt_info.flags & 1;
3251 contents = (ldt_info.flags >> 1) & 3;
3252 read_exec_only = (ldt_info.flags >> 3) & 1;
3253 limit_in_pages = (ldt_info.flags >> 4) & 1;
3254 seg_not_present = (ldt_info.flags >> 5) & 1;
3255 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003256#ifdef TARGET_ABI32
3257 lm = 0;
3258#else
3259 lm = (ldt_info.flags >> 7) & 1;
3260#endif
bellard6dbad632003-03-16 18:05:05 +00003261 if (contents == 3) {
3262 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003263 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003264 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003265 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003266 }
3267 /* allocate the LDT */
3268 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003269 env->ldt.base = target_mmap(0,
3270 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3271 PROT_READ|PROT_WRITE,
3272 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3273 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003274 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003275 memset(g2h(env->ldt.base), 0,
3276 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003277 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003278 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003279 }
3280
3281 /* NOTE: same code as Linux kernel */
3282 /* Allow LDTs to be cleared by the user. */
3283 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3284 if (oldmode ||
3285 (contents == 0 &&
3286 read_exec_only == 1 &&
3287 seg_32bit == 0 &&
3288 limit_in_pages == 0 &&
3289 seg_not_present == 1 &&
3290 useable == 0 )) {
3291 entry_1 = 0;
3292 entry_2 = 0;
3293 goto install;
3294 }
3295 }
ths3b46e622007-09-17 08:09:54 +00003296
bellard6dbad632003-03-16 18:05:05 +00003297 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3298 (ldt_info.limit & 0x0ffff);
3299 entry_2 = (ldt_info.base_addr & 0xff000000) |
3300 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3301 (ldt_info.limit & 0xf0000) |
3302 ((read_exec_only ^ 1) << 9) |
3303 (contents << 10) |
3304 ((seg_not_present ^ 1) << 15) |
3305 (seg_32bit << 22) |
3306 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00003307 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00003308 0x7000;
3309 if (!oldmode)
3310 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00003311
bellard6dbad632003-03-16 18:05:05 +00003312 /* Install the new entry ... */
3313install:
3314 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3315 lp[0] = tswap32(entry_1);
3316 lp[1] = tswap32(entry_2);
3317 return 0;
3318}
3319
3320/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00003321static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3322 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003323{
bellard03acab62007-11-11 14:57:14 +00003324 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00003325
bellard6dbad632003-03-16 18:05:05 +00003326 switch (func) {
3327 case 0:
3328 ret = read_ldt(ptr, bytecount);
3329 break;
3330 case 1:
3331 ret = write_ldt(env, ptr, bytecount, 1);
3332 break;
3333 case 0x11:
3334 ret = write_ldt(env, ptr, bytecount, 0);
3335 break;
bellard03acab62007-11-11 14:57:14 +00003336 default:
3337 ret = -TARGET_ENOSYS;
3338 break;
bellard6dbad632003-03-16 18:05:05 +00003339 }
3340 return ret;
3341}
bellard1b6b0292003-03-22 17:31:38 +00003342
blueswir14583f582008-08-24 10:35:55 +00003343#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00003344static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003345{
3346 uint64_t *gdt_table = g2h(env->gdt.base);
3347 struct target_modify_ldt_ldt_s ldt_info;
3348 struct target_modify_ldt_ldt_s *target_ldt_info;
3349 int seg_32bit, contents, read_exec_only, limit_in_pages;
3350 int seg_not_present, useable, lm;
3351 uint32_t *lp, entry_1, entry_2;
3352 int i;
3353
3354 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3355 if (!target_ldt_info)
3356 return -TARGET_EFAULT;
3357 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3358 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3359 ldt_info.limit = tswap32(target_ldt_info->limit);
3360 ldt_info.flags = tswap32(target_ldt_info->flags);
3361 if (ldt_info.entry_number == -1) {
3362 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3363 if (gdt_table[i] == 0) {
3364 ldt_info.entry_number = i;
3365 target_ldt_info->entry_number = tswap32(i);
3366 break;
3367 }
3368 }
3369 }
3370 unlock_user_struct(target_ldt_info, ptr, 1);
3371
3372 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3373 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3374 return -TARGET_EINVAL;
3375 seg_32bit = ldt_info.flags & 1;
3376 contents = (ldt_info.flags >> 1) & 3;
3377 read_exec_only = (ldt_info.flags >> 3) & 1;
3378 limit_in_pages = (ldt_info.flags >> 4) & 1;
3379 seg_not_present = (ldt_info.flags >> 5) & 1;
3380 useable = (ldt_info.flags >> 6) & 1;
3381#ifdef TARGET_ABI32
3382 lm = 0;
3383#else
3384 lm = (ldt_info.flags >> 7) & 1;
3385#endif
3386
3387 if (contents == 3) {
3388 if (seg_not_present == 0)
3389 return -TARGET_EINVAL;
3390 }
3391
3392 /* NOTE: same code as Linux kernel */
3393 /* Allow LDTs to be cleared by the user. */
3394 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3395 if ((contents == 0 &&
3396 read_exec_only == 1 &&
3397 seg_32bit == 0 &&
3398 limit_in_pages == 0 &&
3399 seg_not_present == 1 &&
3400 useable == 0 )) {
3401 entry_1 = 0;
3402 entry_2 = 0;
3403 goto install;
3404 }
3405 }
3406
3407 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3408 (ldt_info.limit & 0x0ffff);
3409 entry_2 = (ldt_info.base_addr & 0xff000000) |
3410 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3411 (ldt_info.limit & 0xf0000) |
3412 ((read_exec_only ^ 1) << 9) |
3413 (contents << 10) |
3414 ((seg_not_present ^ 1) << 15) |
3415 (seg_32bit << 22) |
3416 (limit_in_pages << 23) |
3417 (useable << 20) |
3418 (lm << 21) |
3419 0x7000;
3420
3421 /* Install the new entry ... */
3422install:
3423 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3424 lp[0] = tswap32(entry_1);
3425 lp[1] = tswap32(entry_2);
3426 return 0;
3427}
3428
blueswir18fcd3692008-08-17 20:26:25 +00003429static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003430{
3431 struct target_modify_ldt_ldt_s *target_ldt_info;
3432 uint64_t *gdt_table = g2h(env->gdt.base);
3433 uint32_t base_addr, limit, flags;
3434 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3435 int seg_not_present, useable, lm;
3436 uint32_t *lp, entry_1, entry_2;
3437
3438 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3439 if (!target_ldt_info)
3440 return -TARGET_EFAULT;
3441 idx = tswap32(target_ldt_info->entry_number);
3442 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3443 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3444 unlock_user_struct(target_ldt_info, ptr, 1);
3445 return -TARGET_EINVAL;
3446 }
3447 lp = (uint32_t *)(gdt_table + idx);
3448 entry_1 = tswap32(lp[0]);
3449 entry_2 = tswap32(lp[1]);
3450
3451 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3452 contents = (entry_2 >> 10) & 3;
3453 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3454 seg_32bit = (entry_2 >> 22) & 1;
3455 limit_in_pages = (entry_2 >> 23) & 1;
3456 useable = (entry_2 >> 20) & 1;
3457#ifdef TARGET_ABI32
3458 lm = 0;
3459#else
3460 lm = (entry_2 >> 21) & 1;
3461#endif
3462 flags = (seg_32bit << 0) | (contents << 1) |
3463 (read_exec_only << 3) | (limit_in_pages << 4) |
3464 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3465 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3466 base_addr = (entry_1 >> 16) |
3467 (entry_2 & 0xff000000) |
3468 ((entry_2 & 0xff) << 16);
3469 target_ldt_info->base_addr = tswapl(base_addr);
3470 target_ldt_info->limit = tswap32(limit);
3471 target_ldt_info->flags = tswap32(flags);
3472 unlock_user_struct(target_ldt_info, ptr, 1);
3473 return 0;
3474}
blueswir14583f582008-08-24 10:35:55 +00003475#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00003476
bellardd2fd1af2007-11-14 18:08:56 +00003477#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00003478static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00003479{
3480 abi_long ret;
3481 abi_ulong val;
3482 int idx;
3483
3484 switch(code) {
3485 case TARGET_ARCH_SET_GS:
3486 case TARGET_ARCH_SET_FS:
3487 if (code == TARGET_ARCH_SET_GS)
3488 idx = R_GS;
3489 else
3490 idx = R_FS;
3491 cpu_x86_load_seg(env, idx, 0);
3492 env->segs[idx].base = addr;
3493 break;
3494 case TARGET_ARCH_GET_GS:
3495 case TARGET_ARCH_GET_FS:
3496 if (code == TARGET_ARCH_GET_GS)
3497 idx = R_GS;
3498 else
3499 idx = R_FS;
3500 val = env->segs[idx].base;
3501 if (put_user(val, addr, abi_ulong))
3502 return -TARGET_EFAULT;
3503 break;
3504 default:
3505 ret = -TARGET_EINVAL;
3506 break;
3507 }
3508 return 0;
3509}
3510#endif
3511
bellard2ab83ea2003-06-15 19:56:46 +00003512#endif /* defined(TARGET_I386) */
3513
Juan Quintela2f7bb872009-07-27 16:13:24 +02003514#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003515
3516#define NEW_STACK_SIZE PTHREAD_STACK_MIN
3517
3518static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3519typedef struct {
3520 CPUState *env;
3521 pthread_mutex_t mutex;
3522 pthread_cond_t cond;
3523 pthread_t thread;
3524 uint32_t tid;
3525 abi_ulong child_tidptr;
3526 abi_ulong parent_tidptr;
3527 sigset_t sigmask;
3528} new_thread_info;
3529
3530static void *clone_func(void *arg)
3531{
3532 new_thread_info *info = arg;
3533 CPUState *env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003534 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00003535
3536 env = info->env;
3537 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003538 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00003539 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003540 env->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003541 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00003542 if (info->child_tidptr)
3543 put_user_u32(info->tid, info->child_tidptr);
3544 if (info->parent_tidptr)
3545 put_user_u32(info->tid, info->parent_tidptr);
3546 /* Enable signals. */
3547 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3548 /* Signal to the parent that we're ready. */
3549 pthread_mutex_lock(&info->mutex);
3550 pthread_cond_broadcast(&info->cond);
3551 pthread_mutex_unlock(&info->mutex);
3552 /* Wait until the parent has finshed initializing the tls state. */
3553 pthread_mutex_lock(&clone_lock);
3554 pthread_mutex_unlock(&clone_lock);
3555 cpu_loop(env);
3556 /* never exits */
3557 return NULL;
3558}
3559#else
bellard1b6b0292003-03-22 17:31:38 +00003560/* this stack is the equivalent of the kernel stack associated with a
3561 thread/process */
3562#define NEW_STACK_SIZE 8192
3563
3564static int clone_func(void *arg)
3565{
bellard2ab83ea2003-06-15 19:56:46 +00003566 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00003567 cpu_loop(env);
3568 /* never exits */
3569 return 0;
3570}
pbrookd865bab2008-06-07 22:12:17 +00003571#endif
bellard1b6b0292003-03-22 17:31:38 +00003572
ths0da46a62007-10-20 20:23:07 +00003573/* do_fork() Must return host values and target errnos (unlike most
3574 do_*() functions). */
pbrookd865bab2008-06-07 22:12:17 +00003575static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3576 abi_ulong parent_tidptr, target_ulong newtls,
3577 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00003578{
3579 int ret;
bellard5cd43932003-03-29 16:54:36 +00003580 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00003581 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00003582 CPUState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003583#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003584 unsigned int nptl_flags;
3585 sigset_t sigmask;
3586#endif
ths3b46e622007-09-17 08:09:54 +00003587
balrog436d1242008-09-21 02:39:45 +00003588 /* Emulate vfork() with fork() */
3589 if (flags & CLONE_VFORK)
3590 flags &= ~(CLONE_VFORK | CLONE_VM);
3591
bellard1b6b0292003-03-22 17:31:38 +00003592 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003593 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003594#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003595 new_thread_info info;
3596 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00003597#endif
pbrookc3a92832008-06-09 14:02:50 +00003598 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
pbrook624f7972008-05-31 16:11:38 +00003599 init_task_state(ts);
bellard5cd43932003-03-29 16:54:36 +00003600 new_stack = ts->stack;
bellard1b6b0292003-03-22 17:31:38 +00003601 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00003602 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00003603#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3604 cpu_reset(new_env);
3605#endif
pbrook6e68e072008-05-30 17:22:15 +00003606 /* Init regs that differ from the parent. */
3607 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00003608 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03003609 ts->bprm = parent_ts->bprm;
3610 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02003611#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003612 nptl_flags = flags;
3613 flags &= ~CLONE_NPTL_FLAGS2;
3614
pbrookc2764712009-03-07 15:24:59 +00003615 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3616 ts->child_tidptr = child_tidptr;
3617 }
3618
pbrookd865bab2008-06-07 22:12:17 +00003619 if (nptl_flags & CLONE_SETTLS)
3620 cpu_set_tls (new_env, newtls);
3621
3622 /* Grab a mutex so that thread setup appears atomic. */
3623 pthread_mutex_lock(&clone_lock);
3624
3625 memset(&info, 0, sizeof(info));
3626 pthread_mutex_init(&info.mutex, NULL);
3627 pthread_mutex_lock(&info.mutex);
3628 pthread_cond_init(&info.cond, NULL);
3629 info.env = new_env;
3630 if (nptl_flags & CLONE_CHILD_SETTID)
3631 info.child_tidptr = child_tidptr;
3632 if (nptl_flags & CLONE_PARENT_SETTID)
3633 info.parent_tidptr = parent_tidptr;
3634
3635 ret = pthread_attr_init(&attr);
3636 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
3637 /* It is not safe to deliver signals until the child has finished
3638 initializing, so temporarily block all signals. */
3639 sigfillset(&sigmask);
3640 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3641
3642 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00003643 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00003644
3645 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3646 pthread_attr_destroy(&attr);
3647 if (ret == 0) {
3648 /* Wait for the child to initialize. */
3649 pthread_cond_wait(&info.cond, &info.mutex);
3650 ret = info.tid;
3651 if (flags & CLONE_PARENT_SETTID)
3652 put_user_u32(ret, parent_tidptr);
3653 } else {
3654 ret = -1;
3655 }
3656 pthread_mutex_unlock(&info.mutex);
3657 pthread_cond_destroy(&info.cond);
3658 pthread_mutex_destroy(&info.mutex);
3659 pthread_mutex_unlock(&clone_lock);
3660#else
3661 if (flags & CLONE_NPTL_FLAGS2)
3662 return -EINVAL;
3663 /* This is probably going to die very quickly, but do it anyway. */
bellard27725c12003-04-29 21:08:18 +00003664#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02003665 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00003666#else
3667 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3668#endif
pbrookd865bab2008-06-07 22:12:17 +00003669#endif
bellard1b6b0292003-03-22 17:31:38 +00003670 } else {
3671 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00003672 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00003673 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00003674 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00003675 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00003676 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00003677 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00003678 cpu_clone_regs(env, newsp);
3679 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02003680#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00003681 /* There is a race condition here. The parent process could
3682 theoretically read the TID in the child process before the child
3683 tid is set. This would require using either ptrace
3684 (not implemented) or having *_tidptr to point at a shared memory
3685 mapping. We can't repeat the spinlock hack used above because
3686 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00003687 if (flags & CLONE_CHILD_SETTID)
3688 put_user_u32(gettid(), child_tidptr);
3689 if (flags & CLONE_PARENT_SETTID)
3690 put_user_u32(gettid(), parent_tidptr);
3691 ts = (TaskState *)env->opaque;
3692 if (flags & CLONE_SETTLS)
3693 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00003694 if (flags & CLONE_CHILD_CLEARTID)
3695 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00003696#endif
pbrookd865bab2008-06-07 22:12:17 +00003697 } else {
3698 fork_end(0);
3699 }
bellard1b6b0292003-03-22 17:31:38 +00003700 }
3701 return ret;
3702}
3703
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003704/* warning : doesn't handle linux specific flags... */
3705static int target_to_host_fcntl_cmd(int cmd)
3706{
3707 switch(cmd) {
3708 case TARGET_F_DUPFD:
3709 case TARGET_F_GETFD:
3710 case TARGET_F_SETFD:
3711 case TARGET_F_GETFL:
3712 case TARGET_F_SETFL:
3713 return cmd;
3714 case TARGET_F_GETLK:
3715 return F_GETLK;
3716 case TARGET_F_SETLK:
3717 return F_SETLK;
3718 case TARGET_F_SETLKW:
3719 return F_SETLKW;
3720 case TARGET_F_GETOWN:
3721 return F_GETOWN;
3722 case TARGET_F_SETOWN:
3723 return F_SETOWN;
3724 case TARGET_F_GETSIG:
3725 return F_GETSIG;
3726 case TARGET_F_SETSIG:
3727 return F_SETSIG;
3728#if TARGET_ABI_BITS == 32
3729 case TARGET_F_GETLK64:
3730 return F_GETLK64;
3731 case TARGET_F_SETLK64:
3732 return F_SETLK64;
3733 case TARGET_F_SETLKW64:
3734 return F_SETLKW64;
3735#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003736 case TARGET_F_SETLEASE:
3737 return F_SETLEASE;
3738 case TARGET_F_GETLEASE:
3739 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04003740#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003741 case TARGET_F_DUPFD_CLOEXEC:
3742 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04003743#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003744 case TARGET_F_NOTIFY:
3745 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003746 default:
3747 return -TARGET_EINVAL;
3748 }
3749 return -TARGET_EINVAL;
3750}
3751
blueswir1992f48a2007-10-14 16:27:31 +00003752static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00003753{
3754 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00003755 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00003756 struct flock64 fl64;
3757 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00003758 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003759 int host_cmd = target_to_host_fcntl_cmd(cmd);
3760
3761 if (host_cmd == -TARGET_EINVAL)
3762 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00003763
bellard7775e9e2003-05-14 22:46:48 +00003764 switch(cmd) {
3765 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00003766 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3767 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003768 fl.l_type = tswap16(target_fl->l_type);
3769 fl.l_whence = tswap16(target_fl->l_whence);
3770 fl.l_start = tswapl(target_fl->l_start);
3771 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003772 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00003773 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003774 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003775 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003776 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3777 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003778 target_fl->l_type = tswap16(fl.l_type);
3779 target_fl->l_whence = tswap16(fl.l_whence);
3780 target_fl->l_start = tswapl(fl.l_start);
3781 target_fl->l_len = tswapl(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003782 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00003783 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00003784 }
3785 break;
ths3b46e622007-09-17 08:09:54 +00003786
bellard7775e9e2003-05-14 22:46:48 +00003787 case TARGET_F_SETLK:
3788 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00003789 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3790 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003791 fl.l_type = tswap16(target_fl->l_type);
3792 fl.l_whence = tswap16(target_fl->l_whence);
3793 fl.l_start = tswapl(target_fl->l_start);
3794 fl.l_len = tswapl(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003795 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00003796 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003797 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003798 break;
ths3b46e622007-09-17 08:09:54 +00003799
bellard7775e9e2003-05-14 22:46:48 +00003800 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00003801 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3802 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003803 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3804 fl64.l_whence = tswap16(target_fl64->l_whence);
3805 fl64.l_start = tswapl(target_fl64->l_start);
3806 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003807 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00003808 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003809 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00003810 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003811 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3812 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003813 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3814 target_fl64->l_whence = tswap16(fl64.l_whence);
3815 target_fl64->l_start = tswapl(fl64.l_start);
3816 target_fl64->l_len = tswapl(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003817 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00003818 unlock_user_struct(target_fl64, arg, 1);
3819 }
bellard9ee1fa22007-11-11 15:11:19 +00003820 break;
bellard7775e9e2003-05-14 22:46:48 +00003821 case TARGET_F_SETLK64:
3822 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00003823 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3824 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003825 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3826 fl64.l_whence = tswap16(target_fl64->l_whence);
3827 fl64.l_start = tswapl(target_fl64->l_start);
3828 fl64.l_len = tswapl(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003829 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00003830 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003831 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00003832 break;
3833
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003834 case TARGET_F_GETFL:
3835 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00003836 if (ret >= 0) {
3837 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3838 }
bellardffa65c32004-01-04 23:57:22 +00003839 break;
3840
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003841 case TARGET_F_SETFL:
3842 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3843 break;
3844
3845 case TARGET_F_SETOWN:
3846 case TARGET_F_GETOWN:
3847 case TARGET_F_SETSIG:
3848 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02003849 case TARGET_F_SETLEASE:
3850 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02003851 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00003852 break;
3853
bellard7775e9e2003-05-14 22:46:48 +00003854 default:
bellard9ee1fa22007-11-11 15:11:19 +00003855 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00003856 break;
3857 }
3858 return ret;
3859}
3860
bellard67867302003-11-23 17:05:30 +00003861#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00003862
bellard67867302003-11-23 17:05:30 +00003863static inline int high2lowuid(int uid)
3864{
3865 if (uid > 65535)
3866 return 65534;
3867 else
3868 return uid;
3869}
3870
3871static inline int high2lowgid(int gid)
3872{
3873 if (gid > 65535)
3874 return 65534;
3875 else
3876 return gid;
3877}
3878
3879static inline int low2highuid(int uid)
3880{
3881 if ((int16_t)uid == -1)
3882 return -1;
3883 else
3884 return uid;
3885}
3886
3887static inline int low2highgid(int gid)
3888{
3889 if ((int16_t)gid == -1)
3890 return -1;
3891 else
3892 return gid;
3893}
3894
3895#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00003896
bellard31e31b82003-02-18 22:55:36 +00003897void syscall_init(void)
3898{
bellard2ab83ea2003-06-15 19:56:46 +00003899 IOCTLEntry *ie;
3900 const argtype *arg_type;
3901 int size;
thsb92c47c2007-11-01 00:07:38 +00003902 int i;
bellard2ab83ea2003-06-15 19:56:46 +00003903
Blue Swirl001faf32009-05-13 17:53:17 +00003904#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00003905#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00003906#include "syscall_types.h"
3907#undef STRUCT
3908#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00003909
3910 /* we patch the ioctl size if necessary. We rely on the fact that
3911 no ioctl has all the bits at '1' in the size field */
3912 ie = ioctl_entries;
3913 while (ie->target_cmd != 0) {
3914 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3915 TARGET_IOC_SIZEMASK) {
3916 arg_type = ie->arg_type;
3917 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00003918 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00003919 ie->target_cmd);
3920 exit(1);
3921 }
3922 arg_type++;
3923 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00003924 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00003925 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3926 (size << TARGET_IOC_SIZESHIFT);
3927 }
thsb92c47c2007-11-01 00:07:38 +00003928
3929 /* Build target_to_host_errno_table[] table from
3930 * host_to_target_errno_table[]. */
3931 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3932 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3933
bellard2ab83ea2003-06-15 19:56:46 +00003934 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00003935#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3936 (defined(__x86_64__) && defined(TARGET_X86_64))
3937 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3938 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3939 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00003940 }
3941#endif
3942 ie++;
3943 }
bellard31e31b82003-02-18 22:55:36 +00003944}
bellardc573ff62004-01-04 15:51:36 +00003945
blueswir1992f48a2007-10-14 16:27:31 +00003946#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00003947static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3948{
thsaf325d32008-06-10 15:29:15 +00003949#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00003950 return ((uint64_t)word0 << 32) | word1;
3951#else
3952 return ((uint64_t)word1 << 32) | word0;
3953#endif
3954}
blueswir1992f48a2007-10-14 16:27:31 +00003955#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00003956static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3957{
3958 return word0;
3959}
blueswir1992f48a2007-10-14 16:27:31 +00003960#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00003961
3962#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00003963static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3964 abi_long arg2,
3965 abi_long arg3,
3966 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003967{
3968#ifdef TARGET_ARM
3969 if (((CPUARMState *)cpu_env)->eabi)
3970 {
3971 arg2 = arg3;
3972 arg3 = arg4;
3973 }
3974#endif
3975 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3976}
3977#endif
3978
3979#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00003980static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3981 abi_long arg2,
3982 abi_long arg3,
3983 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003984{
3985#ifdef TARGET_ARM
3986 if (((CPUARMState *)cpu_env)->eabi)
3987 {
3988 arg2 = arg3;
3989 arg3 = arg4;
3990 }
3991#endif
3992 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3993}
3994#endif
3995
bellard579a97f2007-11-11 14:26:47 +00003996static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3997 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00003998{
3999 struct target_timespec *target_ts;
4000
bellard579a97f2007-11-11 14:26:47 +00004001 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4002 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004003 host_ts->tv_sec = tswapl(target_ts->tv_sec);
4004 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
4005 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004006 return 0;
pbrook53a59602006-03-25 19:31:22 +00004007}
4008
bellard579a97f2007-11-11 14:26:47 +00004009static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4010 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004011{
4012 struct target_timespec *target_ts;
4013
bellard579a97f2007-11-11 14:26:47 +00004014 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4015 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004016 target_ts->tv_sec = tswapl(host_ts->tv_sec);
4017 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
4018 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004019 return 0;
pbrook53a59602006-03-25 19:31:22 +00004020}
4021
aurel329d33b762009-04-08 23:07:05 +00004022#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004023static inline abi_long host_to_target_stat64(void *cpu_env,
4024 abi_ulong target_addr,
4025 struct stat *host_st)
4026{
4027#ifdef TARGET_ARM
4028 if (((CPUARMState *)cpu_env)->eabi) {
4029 struct target_eabi_stat64 *target_st;
4030
4031 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4032 return -TARGET_EFAULT;
4033 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4034 __put_user(host_st->st_dev, &target_st->st_dev);
4035 __put_user(host_st->st_ino, &target_st->st_ino);
4036#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4037 __put_user(host_st->st_ino, &target_st->__st_ino);
4038#endif
4039 __put_user(host_st->st_mode, &target_st->st_mode);
4040 __put_user(host_st->st_nlink, &target_st->st_nlink);
4041 __put_user(host_st->st_uid, &target_st->st_uid);
4042 __put_user(host_st->st_gid, &target_st->st_gid);
4043 __put_user(host_st->st_rdev, &target_st->st_rdev);
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 } else
4052#endif
4053 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004054#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004055 struct target_stat *target_st;
4056#else
balrog6a24a772008-09-20 02:23:36 +00004057 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004058#endif
balrog6a24a772008-09-20 02:23:36 +00004059
4060 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4061 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004062 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004063 __put_user(host_st->st_dev, &target_st->st_dev);
4064 __put_user(host_st->st_ino, &target_st->st_ino);
4065#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4066 __put_user(host_st->st_ino, &target_st->__st_ino);
4067#endif
4068 __put_user(host_st->st_mode, &target_st->st_mode);
4069 __put_user(host_st->st_nlink, &target_st->st_nlink);
4070 __put_user(host_st->st_uid, &target_st->st_uid);
4071 __put_user(host_st->st_gid, &target_st->st_gid);
4072 __put_user(host_st->st_rdev, &target_st->st_rdev);
4073 /* XXX: better use of kernel struct */
4074 __put_user(host_st->st_size, &target_st->st_size);
4075 __put_user(host_st->st_blksize, &target_st->st_blksize);
4076 __put_user(host_st->st_blocks, &target_st->st_blocks);
4077 __put_user(host_st->st_atime, &target_st->target_st_atime);
4078 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4079 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4080 unlock_user_struct(target_st, target_addr, 1);
4081 }
4082
4083 return 0;
4084}
4085#endif
4086
Juan Quintela2f7bb872009-07-27 16:13:24 +02004087#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004088/* ??? Using host futex calls even when target atomic operations
4089 are not really atomic probably breaks things. However implementing
4090 futexes locally would make futexes shared between multiple processes
4091 tricky. However they're probably useless because guest atomic
4092 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004093static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4094 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004095{
4096 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004097 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004098
4099 /* ??? We assume FUTEX_* constants are the same on both host
4100 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004101#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004102 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004103#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004104 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004105#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004106 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004107 case FUTEX_WAIT:
4108 if (timeout) {
4109 pts = &ts;
4110 target_to_host_timespec(pts, timeout);
4111 } else {
4112 pts = NULL;
4113 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004114 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
pbrookbd0c5662008-05-29 14:34:11 +00004115 pts, NULL, 0));
4116 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004117 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004118 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004119 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004120 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004121 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004122 case FUTEX_WAKE_OP:
4123 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4124 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4125 But the prototype takes a `struct timespec *'; insert casts
4126 to satisfy the compiler. We do not need to tswap TIMEOUT
4127 since it's not compared to guest memory. */
4128 pts = (struct timespec *)(uintptr_t) timeout;
4129 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4130 g2h(uaddr2),
4131 (base_op == FUTEX_CMP_REQUEUE
4132 ? tswap32(val3)
4133 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004134 default:
4135 return -TARGET_ENOSYS;
4136 }
4137}
4138#endif
4139
pbrook1d9d8b52009-04-16 15:17:02 +00004140/* Map host to target signal numbers for the wait family of syscalls.
4141 Assume all other status bits are the same. */
4142static int host_to_target_waitstatus(int status)
4143{
4144 if (WIFSIGNALED(status)) {
4145 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4146 }
4147 if (WIFSTOPPED(status)) {
4148 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4149 | (status & 0xff);
4150 }
4151 return status;
4152}
4153
pbrooka745ec62008-05-06 15:36:17 +00004154int get_osversion(void)
4155{
4156 static int osversion;
4157 struct new_utsname buf;
4158 const char *s;
4159 int i, n, tmp;
4160 if (osversion)
4161 return osversion;
4162 if (qemu_uname_release && *qemu_uname_release) {
4163 s = qemu_uname_release;
4164 } else {
4165 if (sys_uname(&buf))
4166 return 0;
4167 s = buf.release;
4168 }
4169 tmp = 0;
4170 for (i = 0; i < 3; i++) {
4171 n = 0;
4172 while (*s >= '0' && *s <= '9') {
4173 n *= 10;
4174 n += *s - '0';
4175 s++;
4176 }
4177 tmp = (tmp << 8) + n;
4178 if (*s == '.')
4179 s++;
4180 }
4181 osversion = tmp;
4182 return osversion;
4183}
4184
ths0da46a62007-10-20 20:23:07 +00004185/* do_syscall() should always have a single exit point at the end so
4186 that actions, such as logging of syscall results, can be performed.
4187 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00004188abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4189 abi_long arg2, abi_long arg3, abi_long arg4,
4190 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00004191{
blueswir1992f48a2007-10-14 16:27:31 +00004192 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004193 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00004194 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00004195 void *p;
ths3b46e622007-09-17 08:09:54 +00004196
bellard72f03902003-02-18 23:33:18 +00004197#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00004198 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00004199#endif
thsb92c47c2007-11-01 00:07:38 +00004200 if(do_strace)
4201 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4202
bellard31e31b82003-02-18 22:55:36 +00004203 switch(num) {
4204 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02004205#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00004206 /* In old applications this may be used to implement _exit(2).
4207 However in threaded applictions it is used for thread termination,
4208 and _exit_group is used for application termination.
4209 Do thread termination if we have more then one thread. */
4210 /* FIXME: This probably breaks if a signal arrives. We should probably
4211 be disabling signals. */
4212 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004213 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00004214 CPUState **lastp;
4215 CPUState *p;
4216
4217 cpu_list_lock();
4218 lastp = &first_cpu;
4219 p = first_cpu;
4220 while (p && p != (CPUState *)cpu_env) {
4221 lastp = &p->next_cpu;
4222 p = p->next_cpu;
4223 }
4224 /* If we didn't find the CPU for this thread then something is
4225 horribly wrong. */
4226 if (!p)
4227 abort();
4228 /* Remove the CPU from the list. */
4229 *lastp = p->next_cpu;
4230 cpu_list_unlock();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07004231 ts = ((CPUState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00004232 if (ts->child_tidptr) {
4233 put_user_u32(0, ts->child_tidptr);
4234 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4235 NULL, NULL, 0);
4236 }
4237 /* TODO: Free CPU state. */
4238 pthread_exit(NULL);
4239 }
4240#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02004241#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00004242 _mcleanup();
4243#endif
bellarde9009672005-04-26 20:42:36 +00004244 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00004245 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00004246 ret = 0; /* avoid warning */
4247 break;
4248 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00004249 if (arg3 == 0)
4250 ret = 0;
4251 else {
4252 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4253 goto efault;
4254 ret = get_errno(read(arg1, p, arg3));
4255 unlock_user(p, arg2, ret);
4256 }
bellard31e31b82003-02-18 22:55:36 +00004257 break;
4258 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00004259 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4260 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004261 ret = get_errno(write(arg1, p, arg3));
4262 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00004263 break;
4264 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00004265 if (!(p = lock_user_string(arg1)))
4266 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004267 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00004268 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4269 arg3));
pbrook53a59602006-03-25 19:31:22 +00004270 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004271 break;
ths82424832007-09-24 09:21:55 +00004272#if defined(TARGET_NR_openat) && defined(__NR_openat)
4273 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00004274 if (!(p = lock_user_string(arg2)))
4275 goto efault;
4276 ret = get_errno(sys_openat(arg1,
4277 path(p),
4278 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4279 arg4));
4280 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00004281 break;
4282#endif
bellard31e31b82003-02-18 22:55:36 +00004283 case TARGET_NR_close:
4284 ret = get_errno(close(arg1));
4285 break;
4286 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00004287 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00004288 break;
4289 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00004290 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00004291 break;
thse5febef2007-04-01 18:31:35 +00004292#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00004293 case TARGET_NR_waitpid:
4294 {
pbrook53a59602006-03-25 19:31:22 +00004295 int status;
4296 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00004297 if (!is_error(ret) && arg2
pbrook1d9d8b52009-04-16 15:17:02 +00004298 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00004299 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004300 }
4301 break;
thse5febef2007-04-01 18:31:35 +00004302#endif
pbrookf0cbb612008-05-30 18:20:05 +00004303#ifdef TARGET_NR_waitid
4304 case TARGET_NR_waitid:
4305 {
4306 siginfo_t info;
4307 info.si_pid = 0;
4308 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4309 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004310 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00004311 goto efault;
4312 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05004313 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00004314 }
4315 }
4316 break;
4317#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004318#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004319 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00004320 if (!(p = lock_user_string(arg1)))
4321 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004322 ret = get_errno(creat(p, arg2));
4323 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004324 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004325#endif
bellard31e31b82003-02-18 22:55:36 +00004326 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00004327 {
4328 void * p2;
4329 p = lock_user_string(arg1);
4330 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004331 if (!p || !p2)
4332 ret = -TARGET_EFAULT;
4333 else
4334 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004335 unlock_user(p2, arg2, 0);
4336 unlock_user(p, arg1, 0);
4337 }
bellard31e31b82003-02-18 22:55:36 +00004338 break;
ths64f0ce42007-09-24 09:25:06 +00004339#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4340 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00004341 {
4342 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00004343 if (!arg2 || !arg4)
4344 goto efault;
ths64f0ce42007-09-24 09:25:06 +00004345 p = lock_user_string(arg2);
4346 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004347 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004348 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00004349 else
4350 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00004351 unlock_user(p, arg2, 0);
4352 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00004353 }
4354 break;
4355#endif
bellard31e31b82003-02-18 22:55:36 +00004356 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00004357 if (!(p = lock_user_string(arg1)))
4358 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004359 ret = get_errno(unlink(p));
4360 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004361 break;
ths8170f562007-09-24 09:24:11 +00004362#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4363 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00004364 if (!(p = lock_user_string(arg2)))
4365 goto efault;
4366 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4367 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00004368 break;
balrogb7d35e62007-12-12 00:40:24 +00004369#endif
bellard31e31b82003-02-18 22:55:36 +00004370 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00004371 {
4372 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00004373 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00004374 abi_ulong gp;
4375 abi_ulong guest_argp;
4376 abi_ulong guest_envp;
4377 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00004378 char **q;
4379
bellardf7341ff2003-03-30 21:00:25 +00004380 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00004381 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00004382 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004383 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004384 goto efault;
ths03aa1972007-12-02 06:28:08 +00004385 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004386 break;
bellard7854b052003-03-29 17:22:23 +00004387 argc++;
bellard2f619692007-11-16 10:46:05 +00004388 }
bellardf7341ff2003-03-30 21:00:25 +00004389 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00004390 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00004391 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00004392 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00004393 goto efault;
ths03aa1972007-12-02 06:28:08 +00004394 if (!addr)
bellard2f619692007-11-16 10:46:05 +00004395 break;
bellard7854b052003-03-29 17:22:23 +00004396 envc++;
bellard2f619692007-11-16 10:46:05 +00004397 }
bellard7854b052003-03-29 17:22:23 +00004398
bellardf7341ff2003-03-30 21:00:25 +00004399 argp = alloca((argc + 1) * sizeof(void *));
4400 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00004401
pbrookda94d262008-05-30 18:24:00 +00004402 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004403 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004404 if (get_user_ual(addr, gp))
4405 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004406 if (!addr)
4407 break;
bellard2f619692007-11-16 10:46:05 +00004408 if (!(*q = lock_user_string(addr)))
4409 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004410 }
bellardf7341ff2003-03-30 21:00:25 +00004411 *q = NULL;
4412
pbrookda94d262008-05-30 18:24:00 +00004413 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00004414 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004415 if (get_user_ual(addr, gp))
4416 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004417 if (!addr)
4418 break;
bellard2f619692007-11-16 10:46:05 +00004419 if (!(*q = lock_user_string(addr)))
4420 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004421 }
bellardf7341ff2003-03-30 21:00:25 +00004422 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00004423
bellard2f619692007-11-16 10:46:05 +00004424 if (!(p = lock_user_string(arg1)))
4425 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004426 ret = get_errno(execve(p, argp, envp));
4427 unlock_user(p, arg1, 0);
4428
bellard2f619692007-11-16 10:46:05 +00004429 goto execve_end;
4430
4431 execve_efault:
4432 ret = -TARGET_EFAULT;
4433
4434 execve_end:
pbrook53a59602006-03-25 19:31:22 +00004435 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004436 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004437 if (get_user_ual(addr, gp)
4438 || !addr)
4439 break;
pbrook53a59602006-03-25 19:31:22 +00004440 unlock_user(*q, addr, 0);
4441 }
4442 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004443 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004444 if (get_user_ual(addr, gp)
4445 || !addr)
4446 break;
pbrook53a59602006-03-25 19:31:22 +00004447 unlock_user(*q, addr, 0);
4448 }
bellard7854b052003-03-29 17:22:23 +00004449 }
bellard31e31b82003-02-18 22:55:36 +00004450 break;
4451 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00004452 if (!(p = lock_user_string(arg1)))
4453 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004454 ret = get_errno(chdir(p));
4455 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004456 break;
bellarda315a142005-01-30 22:59:18 +00004457#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00004458 case TARGET_NR_time:
4459 {
pbrook53a59602006-03-25 19:31:22 +00004460 time_t host_time;
4461 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00004462 if (!is_error(ret)
4463 && arg1
4464 && put_user_sal(host_time, arg1))
4465 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004466 }
4467 break;
bellarda315a142005-01-30 22:59:18 +00004468#endif
bellard31e31b82003-02-18 22:55:36 +00004469 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00004470 if (!(p = lock_user_string(arg1)))
4471 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004472 ret = get_errno(mknod(p, arg2, arg3));
4473 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004474 break;
ths75ac37a2007-09-24 09:23:05 +00004475#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4476 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00004477 if (!(p = lock_user_string(arg2)))
4478 goto efault;
4479 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4480 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00004481 break;
4482#endif
bellard31e31b82003-02-18 22:55:36 +00004483 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00004484 if (!(p = lock_user_string(arg1)))
4485 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004486 ret = get_errno(chmod(p, arg2));
4487 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004488 break;
bellardebc05482003-09-30 21:08:41 +00004489#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00004490 case TARGET_NR_break:
4491 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004492#endif
4493#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00004494 case TARGET_NR_oldstat:
4495 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004496#endif
bellard31e31b82003-02-18 22:55:36 +00004497 case TARGET_NR_lseek:
4498 ret = get_errno(lseek(arg1, arg2, arg3));
4499 break;
Richard Henderson92317332010-05-03 10:07:53 -07004500#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
4501 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00004502 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07004503 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00004504 ret = get_errno(getpid());
4505 break;
Richard Henderson92317332010-05-03 10:07:53 -07004506#endif
4507#ifdef TARGET_NR_getpid
4508 case TARGET_NR_getpid:
4509 ret = get_errno(getpid());
4510 break;
4511#endif
bellard31e31b82003-02-18 22:55:36 +00004512 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00004513 {
4514 /* need to look at the data field */
4515 void *p2, *p3;
4516 p = lock_user_string(arg1);
4517 p2 = lock_user_string(arg2);
4518 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004519 if (!p || !p2 || !p3)
4520 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004521 else {
bellard579a97f2007-11-11 14:26:47 +00004522 /* FIXME - arg5 should be locked, but it isn't clear how to
4523 * do that since it's not guaranteed to be a NULL-terminated
4524 * string.
4525 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03004526 if ( ! arg5 )
4527 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4528 else
4529 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4530 }
bellard579a97f2007-11-11 14:26:47 +00004531 unlock_user(p, arg1, 0);
4532 unlock_user(p2, arg2, 0);
4533 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00004534 break;
4535 }
thse5febef2007-04-01 18:31:35 +00004536#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00004537 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00004538 if (!(p = lock_user_string(arg1)))
4539 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004540 ret = get_errno(umount(p));
4541 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004542 break;
thse5febef2007-04-01 18:31:35 +00004543#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004544#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004545 case TARGET_NR_stime:
4546 {
pbrook53a59602006-03-25 19:31:22 +00004547 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00004548 if (get_user_sal(host_time, arg1))
4549 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004550 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00004551 }
4552 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004553#endif
bellard31e31b82003-02-18 22:55:36 +00004554 case TARGET_NR_ptrace:
4555 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00004556#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004557 case TARGET_NR_alarm:
4558 ret = alarm(arg1);
4559 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004560#endif
bellardebc05482003-09-30 21:08:41 +00004561#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00004562 case TARGET_NR_oldfstat:
4563 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004564#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004565#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004566 case TARGET_NR_pause:
4567 ret = get_errno(pause());
4568 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004569#endif
thse5febef2007-04-01 18:31:35 +00004570#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00004571 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00004572 {
pbrook53a59602006-03-25 19:31:22 +00004573 struct utimbuf tbuf, *host_tbuf;
4574 struct target_utimbuf *target_tbuf;
4575 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004576 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4577 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004578 tbuf.actime = tswapl(target_tbuf->actime);
4579 tbuf.modtime = tswapl(target_tbuf->modtime);
4580 unlock_user_struct(target_tbuf, arg2, 0);
4581 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00004582 } else {
pbrook53a59602006-03-25 19:31:22 +00004583 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00004584 }
bellard579a97f2007-11-11 14:26:47 +00004585 if (!(p = lock_user_string(arg1)))
4586 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004587 ret = get_errno(utime(p, host_tbuf));
4588 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00004589 }
4590 break;
thse5febef2007-04-01 18:31:35 +00004591#endif
bellard978a66f2004-12-06 22:58:05 +00004592 case TARGET_NR_utimes:
4593 {
bellard978a66f2004-12-06 22:58:05 +00004594 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00004595 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00004596 if (copy_from_user_timeval(&tv[0], arg2)
4597 || copy_from_user_timeval(&tv[1],
4598 arg2 + sizeof(struct target_timeval)))
4599 goto efault;
bellard978a66f2004-12-06 22:58:05 +00004600 tvp = tv;
4601 } else {
4602 tvp = NULL;
4603 }
bellard579a97f2007-11-11 14:26:47 +00004604 if (!(p = lock_user_string(arg1)))
4605 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004606 ret = get_errno(utimes(p, tvp));
4607 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00004608 }
4609 break;
balrogac8a6552008-09-20 02:25:39 +00004610#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4611 case TARGET_NR_futimesat:
4612 {
4613 struct timeval *tvp, tv[2];
4614 if (arg3) {
4615 if (copy_from_user_timeval(&tv[0], arg3)
4616 || copy_from_user_timeval(&tv[1],
4617 arg3 + sizeof(struct target_timeval)))
4618 goto efault;
4619 tvp = tv;
4620 } else {
4621 tvp = NULL;
4622 }
4623 if (!(p = lock_user_string(arg2)))
4624 goto efault;
4625 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4626 unlock_user(p, arg2, 0);
4627 }
4628 break;
4629#endif
bellardebc05482003-09-30 21:08:41 +00004630#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00004631 case TARGET_NR_stty:
4632 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004633#endif
4634#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00004635 case TARGET_NR_gtty:
4636 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004637#endif
bellard31e31b82003-02-18 22:55:36 +00004638 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00004639 if (!(p = lock_user_string(arg1)))
4640 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02004641 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00004642 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004643 break;
ths92a34c12007-09-24 09:27:49 +00004644#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4645 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00004646 if (!(p = lock_user_string(arg2)))
4647 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00004648 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00004649 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00004650 break;
4651#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004652#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004653 case TARGET_NR_nice:
4654 ret = get_errno(nice(arg1));
4655 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004656#endif
bellardebc05482003-09-30 21:08:41 +00004657#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00004658 case TARGET_NR_ftime:
4659 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004660#endif
bellard31e31b82003-02-18 22:55:36 +00004661 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00004662 sync();
4663 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00004664 break;
4665 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00004666 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00004667 break;
4668 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00004669 {
4670 void *p2;
4671 p = lock_user_string(arg1);
4672 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004673 if (!p || !p2)
4674 ret = -TARGET_EFAULT;
4675 else
4676 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004677 unlock_user(p2, arg2, 0);
4678 unlock_user(p, arg1, 0);
4679 }
bellard31e31b82003-02-18 22:55:36 +00004680 break;
ths722183f2007-09-24 09:24:37 +00004681#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4682 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00004683 {
bellard579a97f2007-11-11 14:26:47 +00004684 void *p2;
ths722183f2007-09-24 09:24:37 +00004685 p = lock_user_string(arg2);
4686 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004687 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004688 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00004689 else
4690 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00004691 unlock_user(p2, arg4, 0);
4692 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00004693 }
4694 break;
4695#endif
bellard31e31b82003-02-18 22:55:36 +00004696 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00004697 if (!(p = lock_user_string(arg1)))
4698 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004699 ret = get_errno(mkdir(p, arg2));
4700 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004701 break;
ths4472ad02007-09-24 09:22:32 +00004702#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4703 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00004704 if (!(p = lock_user_string(arg2)))
4705 goto efault;
4706 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4707 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00004708 break;
4709#endif
bellard31e31b82003-02-18 22:55:36 +00004710 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00004711 if (!(p = lock_user_string(arg1)))
4712 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004713 ret = get_errno(rmdir(p));
4714 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004715 break;
4716 case TARGET_NR_dup:
4717 ret = get_errno(dup(arg1));
4718 break;
4719 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07004720 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00004721 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03004722#ifdef TARGET_NR_pipe2
4723 case TARGET_NR_pipe2:
Richard Hendersonfb41a662010-05-03 10:07:52 -07004724 ret = do_pipe(cpu_env, arg1, arg2, 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03004725 break;
4726#endif
bellard31e31b82003-02-18 22:55:36 +00004727 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00004728 {
pbrook53a59602006-03-25 19:31:22 +00004729 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00004730 struct tms tms;
4731 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00004732 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00004733 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4734 if (!tmsp)
4735 goto efault;
bellardc596ed12003-07-13 17:32:31 +00004736 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4737 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4738 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4739 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00004740 }
bellardc596ed12003-07-13 17:32:31 +00004741 if (!is_error(ret))
4742 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00004743 }
4744 break;
bellardebc05482003-09-30 21:08:41 +00004745#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00004746 case TARGET_NR_prof:
4747 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004748#endif
thse5febef2007-04-01 18:31:35 +00004749#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00004750 case TARGET_NR_signal:
4751 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004752#endif
bellard31e31b82003-02-18 22:55:36 +00004753 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00004754 if (arg1 == 0) {
4755 ret = get_errno(acct(NULL));
4756 } else {
4757 if (!(p = lock_user_string(arg1)))
4758 goto efault;
4759 ret = get_errno(acct(path(p)));
4760 unlock_user(p, arg1, 0);
4761 }
pbrook24836682006-04-16 14:14:53 +00004762 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004763#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004764 case TARGET_NR_umount2:
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(umount2(p, arg2));
4768 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004769 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004770#endif
bellardebc05482003-09-30 21:08:41 +00004771#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00004772 case TARGET_NR_lock:
4773 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004774#endif
bellard31e31b82003-02-18 22:55:36 +00004775 case TARGET_NR_ioctl:
4776 ret = do_ioctl(arg1, arg2, arg3);
4777 break;
4778 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00004779 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00004780 break;
bellardebc05482003-09-30 21:08:41 +00004781#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00004782 case TARGET_NR_mpx:
4783 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004784#endif
bellard31e31b82003-02-18 22:55:36 +00004785 case TARGET_NR_setpgid:
4786 ret = get_errno(setpgid(arg1, arg2));
4787 break;
bellardebc05482003-09-30 21:08:41 +00004788#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00004789 case TARGET_NR_ulimit:
4790 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004791#endif
4792#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00004793 case TARGET_NR_oldolduname:
4794 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004795#endif
bellard31e31b82003-02-18 22:55:36 +00004796 case TARGET_NR_umask:
4797 ret = get_errno(umask(arg1));
4798 break;
4799 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00004800 if (!(p = lock_user_string(arg1)))
4801 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004802 ret = get_errno(chroot(p));
4803 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004804 break;
4805 case TARGET_NR_ustat:
4806 goto unimplemented;
4807 case TARGET_NR_dup2:
4808 ret = get_errno(dup2(arg1, arg2));
4809 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03004810#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
4811 case TARGET_NR_dup3:
4812 ret = get_errno(dup3(arg1, arg2, arg3));
4813 break;
4814#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004815#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004816 case TARGET_NR_getppid:
4817 ret = get_errno(getppid());
4818 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004819#endif
bellard31e31b82003-02-18 22:55:36 +00004820 case TARGET_NR_getpgrp:
4821 ret = get_errno(getpgrp());
4822 break;
4823 case TARGET_NR_setsid:
4824 ret = get_errno(setsid());
4825 break;
thse5febef2007-04-01 18:31:35 +00004826#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00004827 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00004828 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08004829#if defined(TARGET_ALPHA)
4830 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00004831 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00004832 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004833 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4834 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004835 act._sa_handler = old_act->_sa_handler;
4836 target_siginitset(&act.sa_mask, old_act->sa_mask);
4837 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08004838 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00004839 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004840 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00004841 }
4842 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00004843 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004844 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4845 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004846 old_act->_sa_handler = oact._sa_handler;
4847 old_act->sa_mask = oact.sa_mask.sig[0];
4848 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00004849 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00004850 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08004851#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00004852 struct target_sigaction act, oact, *pact, *old_act;
4853
4854 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004855 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4856 goto efault;
bellard106ec872006-06-27 21:08:10 +00004857 act._sa_handler = old_act->_sa_handler;
4858 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4859 act.sa_flags = old_act->sa_flags;
4860 unlock_user_struct(old_act, arg2, 0);
4861 pact = &act;
4862 } else {
4863 pact = NULL;
4864 }
4865
4866 ret = get_errno(do_sigaction(arg1, pact, &oact));
4867
4868 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004869 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4870 goto efault;
bellard106ec872006-06-27 21:08:10 +00004871 old_act->_sa_handler = oact._sa_handler;
4872 old_act->sa_flags = oact.sa_flags;
4873 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4874 old_act->sa_mask.sig[1] = 0;
4875 old_act->sa_mask.sig[2] = 0;
4876 old_act->sa_mask.sig[3] = 0;
4877 unlock_user_struct(old_act, arg3, 1);
4878 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08004879#else
4880 struct target_old_sigaction *old_act;
4881 struct target_sigaction act, oact, *pact;
4882 if (arg2) {
4883 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4884 goto efault;
4885 act._sa_handler = old_act->_sa_handler;
4886 target_siginitset(&act.sa_mask, old_act->sa_mask);
4887 act.sa_flags = old_act->sa_flags;
4888 act.sa_restorer = old_act->sa_restorer;
4889 unlock_user_struct(old_act, arg2, 0);
4890 pact = &act;
4891 } else {
4892 pact = NULL;
4893 }
4894 ret = get_errno(do_sigaction(arg1, pact, &oact));
4895 if (!is_error(ret) && arg3) {
4896 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4897 goto efault;
4898 old_act->_sa_handler = oact._sa_handler;
4899 old_act->sa_mask = oact.sa_mask.sig[0];
4900 old_act->sa_flags = oact.sa_flags;
4901 old_act->sa_restorer = oact.sa_restorer;
4902 unlock_user_struct(old_act, arg3, 1);
4903 }
ths388bb212007-05-13 13:58:00 +00004904#endif
bellard31e31b82003-02-18 22:55:36 +00004905 }
4906 break;
thse5febef2007-04-01 18:31:35 +00004907#endif
bellard66fb9762003-03-23 01:06:05 +00004908 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00004909 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08004910#if defined(TARGET_ALPHA)
4911 struct target_sigaction act, oact, *pact = 0;
4912 struct target_rt_sigaction *rt_act;
4913 /* ??? arg4 == sizeof(sigset_t). */
4914 if (arg2) {
4915 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
4916 goto efault;
4917 act._sa_handler = rt_act->_sa_handler;
4918 act.sa_mask = rt_act->sa_mask;
4919 act.sa_flags = rt_act->sa_flags;
4920 act.sa_restorer = arg5;
4921 unlock_user_struct(rt_act, arg2, 0);
4922 pact = &act;
4923 }
4924 ret = get_errno(do_sigaction(arg1, pact, &oact));
4925 if (!is_error(ret) && arg3) {
4926 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
4927 goto efault;
4928 rt_act->_sa_handler = oact._sa_handler;
4929 rt_act->sa_mask = oact.sa_mask;
4930 rt_act->sa_flags = oact.sa_flags;
4931 unlock_user_struct(rt_act, arg3, 1);
4932 }
4933#else
pbrook53a59602006-03-25 19:31:22 +00004934 struct target_sigaction *act;
4935 struct target_sigaction *oact;
4936
bellard579a97f2007-11-11 14:26:47 +00004937 if (arg2) {
4938 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4939 goto efault;
4940 } else
pbrook53a59602006-03-25 19:31:22 +00004941 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00004942 if (arg3) {
4943 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4944 ret = -TARGET_EFAULT;
4945 goto rt_sigaction_fail;
4946 }
4947 } else
pbrook53a59602006-03-25 19:31:22 +00004948 oact = NULL;
4949 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00004950 rt_sigaction_fail:
4951 if (act)
pbrook53a59602006-03-25 19:31:22 +00004952 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00004953 if (oact)
pbrook53a59602006-03-25 19:31:22 +00004954 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08004955#endif
pbrook53a59602006-03-25 19:31:22 +00004956 }
bellard66fb9762003-03-23 01:06:05 +00004957 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004958#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004959 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00004960 {
4961 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00004962 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00004963 sigprocmask(0, NULL, &cur_set);
4964 host_to_target_old_sigset(&target_set, &cur_set);
4965 ret = target_set;
4966 }
4967 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004968#endif
4969#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004970 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00004971 {
4972 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00004973 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00004974 sigprocmask(0, NULL, &cur_set);
4975 target_to_host_old_sigset(&set, &target_set);
4976 sigorset(&set, &set, &cur_set);
4977 sigprocmask(SIG_SETMASK, &set, &oset);
4978 host_to_target_old_sigset(&target_set, &oset);
4979 ret = target_set;
4980 }
4981 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004982#endif
thse5febef2007-04-01 18:31:35 +00004983#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00004984 case TARGET_NR_sigprocmask:
4985 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07004986#if defined(TARGET_ALPHA)
4987 sigset_t set, oldset;
4988 abi_ulong mask;
4989 int how;
4990
4991 switch (arg1) {
4992 case TARGET_SIG_BLOCK:
4993 how = SIG_BLOCK;
4994 break;
4995 case TARGET_SIG_UNBLOCK:
4996 how = SIG_UNBLOCK;
4997 break;
4998 case TARGET_SIG_SETMASK:
4999 how = SIG_SETMASK;
5000 break;
5001 default:
5002 ret = -TARGET_EINVAL;
5003 goto fail;
5004 }
5005 mask = arg2;
5006 target_to_host_old_sigset(&set, &mask);
5007
5008 ret = get_errno(sigprocmask(how, &set, &oldset));
5009
5010 if (!is_error(ret)) {
5011 host_to_target_old_sigset(&mask, &oldset);
5012 ret = mask;
5013 ((CPUAlphaState *)cpu_env)->[IR_V0] = 0; /* force no error */
5014 }
5015#else
bellard66fb9762003-03-23 01:06:05 +00005016 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07005017 int how;
ths3b46e622007-09-17 08:09:54 +00005018
pbrook53a59602006-03-25 19:31:22 +00005019 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005020 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00005021 case TARGET_SIG_BLOCK:
5022 how = SIG_BLOCK;
5023 break;
5024 case TARGET_SIG_UNBLOCK:
5025 how = SIG_UNBLOCK;
5026 break;
5027 case TARGET_SIG_SETMASK:
5028 how = SIG_SETMASK;
5029 break;
5030 default:
ths0da46a62007-10-20 20:23:07 +00005031 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005032 goto fail;
5033 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005034 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005035 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005036 target_to_host_old_sigset(&set, p);
5037 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005038 set_ptr = &set;
5039 } else {
5040 how = 0;
5041 set_ptr = NULL;
5042 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005043 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005044 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005045 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005046 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005047 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005048 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005049 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005050#endif
bellard66fb9762003-03-23 01:06:05 +00005051 }
5052 break;
thse5febef2007-04-01 18:31:35 +00005053#endif
bellard66fb9762003-03-23 01:06:05 +00005054 case TARGET_NR_rt_sigprocmask:
5055 {
5056 int how = arg1;
5057 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00005058
pbrook53a59602006-03-25 19:31:22 +00005059 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005060 switch(how) {
5061 case TARGET_SIG_BLOCK:
5062 how = SIG_BLOCK;
5063 break;
5064 case TARGET_SIG_UNBLOCK:
5065 how = SIG_UNBLOCK;
5066 break;
5067 case TARGET_SIG_SETMASK:
5068 how = SIG_SETMASK;
5069 break;
5070 default:
ths0da46a62007-10-20 20:23:07 +00005071 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005072 goto fail;
5073 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005074 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005075 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005076 target_to_host_sigset(&set, p);
5077 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005078 set_ptr = &set;
5079 } else {
5080 how = 0;
5081 set_ptr = NULL;
5082 }
5083 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005084 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005085 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005086 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005087 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005088 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005089 }
5090 }
5091 break;
thse5febef2007-04-01 18:31:35 +00005092#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005093 case TARGET_NR_sigpending:
5094 {
5095 sigset_t set;
5096 ret = get_errno(sigpending(&set));
5097 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005098 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005099 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005100 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005101 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005102 }
5103 }
5104 break;
thse5febef2007-04-01 18:31:35 +00005105#endif
bellard66fb9762003-03-23 01:06:05 +00005106 case TARGET_NR_rt_sigpending:
5107 {
5108 sigset_t set;
5109 ret = get_errno(sigpending(&set));
5110 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005111 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005112 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005113 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005114 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005115 }
5116 }
5117 break;
thse5febef2007-04-01 18:31:35 +00005118#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00005119 case TARGET_NR_sigsuspend:
5120 {
5121 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07005122#if defined(TARGET_ALPHA)
5123 abi_ulong mask = arg1;
5124 target_to_host_old_sigset(&set, &mask);
5125#else
Anthony Liguoric227f092009-10-01 16:12:16 -05005126 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005127 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005128 target_to_host_old_sigset(&set, p);
5129 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07005130#endif
bellard66fb9762003-03-23 01:06:05 +00005131 ret = get_errno(sigsuspend(&set));
5132 }
5133 break;
thse5febef2007-04-01 18:31:35 +00005134#endif
bellard66fb9762003-03-23 01:06:05 +00005135 case TARGET_NR_rt_sigsuspend:
5136 {
5137 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005138 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005139 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005140 target_to_host_sigset(&set, p);
5141 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005142 ret = get_errno(sigsuspend(&set));
5143 }
5144 break;
5145 case TARGET_NR_rt_sigtimedwait:
5146 {
bellard66fb9762003-03-23 01:06:05 +00005147 sigset_t set;
5148 struct timespec uts, *puts;
5149 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00005150
Anthony Liguoric227f092009-10-01 16:12:16 -05005151 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005152 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005153 target_to_host_sigset(&set, p);
5154 unlock_user(p, arg1, 0);
5155 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00005156 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00005157 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00005158 } else {
5159 puts = NULL;
5160 }
5161 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00005162 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005163 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005164 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005165 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05005166 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00005167 }
5168 }
5169 break;
5170 case TARGET_NR_rt_sigqueueinfo:
5171 {
5172 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05005173 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005174 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005175 target_to_host_siginfo(&uinfo, p);
5176 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005177 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5178 }
5179 break;
thse5febef2007-04-01 18:31:35 +00005180#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00005181 case TARGET_NR_sigreturn:
5182 /* NOTE: ret is eax, so not transcoding must be done */
5183 ret = do_sigreturn(cpu_env);
5184 break;
thse5febef2007-04-01 18:31:35 +00005185#endif
bellard66fb9762003-03-23 01:06:05 +00005186 case TARGET_NR_rt_sigreturn:
5187 /* NOTE: ret is eax, so not transcoding must be done */
5188 ret = do_rt_sigreturn(cpu_env);
5189 break;
bellard31e31b82003-02-18 22:55:36 +00005190 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00005191 if (!(p = lock_user_string(arg1)))
5192 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005193 ret = get_errno(sethostname(p, arg2));
5194 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005195 break;
5196 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00005197 {
bellard9de5e442003-03-23 16:49:39 +00005198 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005199 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005200 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00005201 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5202 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005203 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5204 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005205 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00005206 ret = get_errno(setrlimit(resource, &rlim));
5207 }
5208 break;
bellard31e31b82003-02-18 22:55:36 +00005209 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00005210 {
bellard9de5e442003-03-23 16:49:39 +00005211 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00005212 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00005213 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00005214
bellard9de5e442003-03-23 16:49:39 +00005215 ret = get_errno(getrlimit(resource, &rlim));
5216 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005217 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5218 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09005219 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5220 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005221 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00005222 }
5223 }
5224 break;
bellard31e31b82003-02-18 22:55:36 +00005225 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00005226 {
5227 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00005228 ret = get_errno(getrusage(arg1, &rusage));
5229 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005230 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00005231 }
5232 }
5233 break;
bellard31e31b82003-02-18 22:55:36 +00005234 case TARGET_NR_gettimeofday:
5235 {
bellard31e31b82003-02-18 22:55:36 +00005236 struct timeval tv;
5237 ret = get_errno(gettimeofday(&tv, NULL));
5238 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00005239 if (copy_to_user_timeval(arg1, &tv))
5240 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005241 }
5242 }
5243 break;
5244 case TARGET_NR_settimeofday:
5245 {
bellard31e31b82003-02-18 22:55:36 +00005246 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00005247 if (copy_from_user_timeval(&tv, arg1))
5248 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005249 ret = get_errno(settimeofday(&tv, NULL));
5250 }
5251 break;
bellard048f6b42005-11-26 18:47:20 +00005252#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00005253 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00005254 {
pbrook53a59602006-03-25 19:31:22 +00005255 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00005256 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00005257 long nsel;
5258
bellard579a97f2007-11-11 14:26:47 +00005259 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5260 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005261 nsel = tswapl(sel->n);
5262 inp = tswapl(sel->inp);
5263 outp = tswapl(sel->outp);
5264 exp = tswapl(sel->exp);
5265 tvp = tswapl(sel->tvp);
5266 unlock_user_struct(sel, arg1, 0);
5267 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00005268 }
5269 break;
bellard048f6b42005-11-26 18:47:20 +00005270#endif
bellard31e31b82003-02-18 22:55:36 +00005271 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00005272 {
5273 void *p2;
5274 p = lock_user_string(arg1);
5275 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005276 if (!p || !p2)
5277 ret = -TARGET_EFAULT;
5278 else
5279 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005280 unlock_user(p2, arg2, 0);
5281 unlock_user(p, arg1, 0);
5282 }
bellard31e31b82003-02-18 22:55:36 +00005283 break;
thsf0b62432007-09-24 09:25:40 +00005284#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5285 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00005286 {
bellard579a97f2007-11-11 14:26:47 +00005287 void *p2;
thsf0b62432007-09-24 09:25:40 +00005288 p = lock_user_string(arg1);
5289 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005290 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005291 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00005292 else
5293 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00005294 unlock_user(p2, arg3, 0);
5295 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00005296 }
5297 break;
5298#endif
bellardebc05482003-09-30 21:08:41 +00005299#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00005300 case TARGET_NR_oldlstat:
5301 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005302#endif
bellard31e31b82003-02-18 22:55:36 +00005303 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00005304 {
aurel32d088d662009-01-30 20:09:01 +00005305 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00005306 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00005307 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5308 if (!p || !p2)
5309 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00005310 else {
5311 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5312 char real[PATH_MAX];
5313 temp = realpath(exec_path,real);
5314 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5315 snprintf((char *)p2, arg3, "%s", real);
5316 }
5317 else
5318 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00005319 }
pbrook53a59602006-03-25 19:31:22 +00005320 unlock_user(p2, arg2, ret);
5321 unlock_user(p, arg1, 0);
5322 }
bellard31e31b82003-02-18 22:55:36 +00005323 break;
ths5e0ccb12007-09-24 09:26:10 +00005324#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5325 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00005326 {
bellard579a97f2007-11-11 14:26:47 +00005327 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00005328 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005329 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5330 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005331 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00005332 else
5333 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00005334 unlock_user(p2, arg3, ret);
5335 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00005336 }
5337 break;
5338#endif
thse5febef2007-04-01 18:31:35 +00005339#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00005340 case TARGET_NR_uselib:
5341 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005342#endif
5343#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00005344 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00005345 if (!(p = lock_user_string(arg1)))
5346 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005347 ret = get_errno(swapon(p, arg2));
5348 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005349 break;
thse5febef2007-04-01 18:31:35 +00005350#endif
bellard31e31b82003-02-18 22:55:36 +00005351 case TARGET_NR_reboot:
5352 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005353#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00005354 case TARGET_NR_readdir:
5355 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005356#endif
5357#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00005358 case TARGET_NR_mmap:
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02005359#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 +00005360 {
blueswir1992f48a2007-10-14 16:27:31 +00005361 abi_ulong *v;
5362 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00005363 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5364 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005365 v1 = tswapl(v[0]);
5366 v2 = tswapl(v[1]);
5367 v3 = tswapl(v[2]);
5368 v4 = tswapl(v[3]);
5369 v5 = tswapl(v[4]);
5370 v6 = tswapl(v[5]);
5371 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00005372 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00005373 target_to_host_bitmask(v4, mmap_flags_tbl),
5374 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00005375 }
bellard31e31b82003-02-18 22:55:36 +00005376#else
ths5fafdf22007-09-16 21:08:06 +00005377 ret = get_errno(target_mmap(arg1, arg2, arg3,
5378 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00005379 arg5,
5380 arg6));
bellard31e31b82003-02-18 22:55:36 +00005381#endif
bellard6fb883e2003-07-09 17:12:39 +00005382 break;
thse5febef2007-04-01 18:31:35 +00005383#endif
bellarda315a142005-01-30 22:59:18 +00005384#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00005385 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00005386#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00005387#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00005388#endif
ths5fafdf22007-09-16 21:08:06 +00005389 ret = get_errno(target_mmap(arg1, arg2, arg3,
5390 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00005391 arg5,
bellardc573ff62004-01-04 15:51:36 +00005392 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00005393 break;
bellarda315a142005-01-30 22:59:18 +00005394#endif
bellard31e31b82003-02-18 22:55:36 +00005395 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00005396 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005397 break;
bellard9de5e442003-03-23 16:49:39 +00005398 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00005399 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005400 break;
thse5febef2007-04-01 18:31:35 +00005401#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00005402 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00005403 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00005404 break;
thse5febef2007-04-01 18:31:35 +00005405#endif
pbrook53a59602006-03-25 19:31:22 +00005406 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00005407#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00005408 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00005409 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00005410 break;
thse5febef2007-04-01 18:31:35 +00005411#endif
5412#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00005413 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00005414 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005415 break;
thse5febef2007-04-01 18:31:35 +00005416#endif
5417#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00005418 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00005419 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00005420 break;
thse5febef2007-04-01 18:31:35 +00005421#endif
5422#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00005423 case TARGET_NR_mlockall:
5424 ret = get_errno(mlockall(arg1));
5425 break;
thse5febef2007-04-01 18:31:35 +00005426#endif
5427#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00005428 case TARGET_NR_munlockall:
5429 ret = get_errno(munlockall());
5430 break;
thse5febef2007-04-01 18:31:35 +00005431#endif
bellard31e31b82003-02-18 22:55:36 +00005432 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00005433 if (!(p = lock_user_string(arg1)))
5434 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005435 ret = get_errno(truncate(p, arg2));
5436 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005437 break;
5438 case TARGET_NR_ftruncate:
5439 ret = get_errno(ftruncate(arg1, arg2));
5440 break;
5441 case TARGET_NR_fchmod:
5442 ret = get_errno(fchmod(arg1, arg2));
5443 break;
ths814d7972007-09-24 09:26:51 +00005444#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5445 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00005446 if (!(p = lock_user_string(arg2)))
5447 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005448 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005449 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00005450 break;
5451#endif
bellard31e31b82003-02-18 22:55:36 +00005452 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00005453 /* libc does special remapping of the return value of
5454 * sys_getpriority() so it's just easiest to call
5455 * sys_getpriority() directly rather than through libc. */
Ulrich Hecht69137202009-09-17 21:08:37 +03005456 ret = get_errno(sys_getpriority(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005457 break;
5458 case TARGET_NR_setpriority:
5459 ret = get_errno(setpriority(arg1, arg2, arg3));
5460 break;
bellardebc05482003-09-30 21:08:41 +00005461#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00005462 case TARGET_NR_profil:
5463 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005464#endif
bellard31e31b82003-02-18 22:55:36 +00005465 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00005466 if (!(p = lock_user_string(arg1)))
5467 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005468 ret = get_errno(statfs(path(p), &stfs));
5469 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005470 convert_statfs:
5471 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005472 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005473
bellard579a97f2007-11-11 14:26:47 +00005474 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5475 goto efault;
5476 __put_user(stfs.f_type, &target_stfs->f_type);
5477 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5478 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5479 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5480 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5481 __put_user(stfs.f_files, &target_stfs->f_files);
5482 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5483 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5484 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5485 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00005486 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005487 }
5488 break;
5489 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00005490 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00005491 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00005492#ifdef TARGET_NR_statfs64
5493 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00005494 if (!(p = lock_user_string(arg1)))
5495 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005496 ret = get_errno(statfs(path(p), &stfs));
5497 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00005498 convert_statfs64:
5499 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005500 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005501
bellard579a97f2007-11-11 14:26:47 +00005502 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5503 goto efault;
5504 __put_user(stfs.f_type, &target_stfs->f_type);
5505 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5506 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5507 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5508 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5509 __put_user(stfs.f_files, &target_stfs->f_files);
5510 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5511 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5512 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5513 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5514 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00005515 }
5516 break;
5517 case TARGET_NR_fstatfs64:
5518 ret = get_errno(fstatfs(arg1, &stfs));
5519 goto convert_statfs64;
5520#endif
bellardebc05482003-09-30 21:08:41 +00005521#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00005522 case TARGET_NR_ioperm:
5523 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005524#endif
thse5febef2007-04-01 18:31:35 +00005525#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00005526 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00005527 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00005528 break;
thse5febef2007-04-01 18:31:35 +00005529#endif
bellard3532fa72006-06-24 15:06:03 +00005530#ifdef TARGET_NR_accept
5531 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00005532 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005533 break;
5534#endif
5535#ifdef TARGET_NR_bind
5536 case TARGET_NR_bind:
5537 ret = do_bind(arg1, arg2, arg3);
5538 break;
5539#endif
5540#ifdef TARGET_NR_connect
5541 case TARGET_NR_connect:
5542 ret = do_connect(arg1, arg2, arg3);
5543 break;
5544#endif
5545#ifdef TARGET_NR_getpeername
5546 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00005547 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005548 break;
5549#endif
5550#ifdef TARGET_NR_getsockname
5551 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00005552 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005553 break;
5554#endif
5555#ifdef TARGET_NR_getsockopt
5556 case TARGET_NR_getsockopt:
5557 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5558 break;
5559#endif
5560#ifdef TARGET_NR_listen
5561 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00005562 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005563 break;
5564#endif
5565#ifdef TARGET_NR_recv
5566 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00005567 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005568 break;
5569#endif
5570#ifdef TARGET_NR_recvfrom
5571 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00005572 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005573 break;
5574#endif
5575#ifdef TARGET_NR_recvmsg
5576 case TARGET_NR_recvmsg:
5577 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5578 break;
5579#endif
5580#ifdef TARGET_NR_send
5581 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00005582 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005583 break;
5584#endif
5585#ifdef TARGET_NR_sendmsg
5586 case TARGET_NR_sendmsg:
5587 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5588 break;
5589#endif
5590#ifdef TARGET_NR_sendto
5591 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00005592 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005593 break;
5594#endif
5595#ifdef TARGET_NR_shutdown
5596 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00005597 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005598 break;
5599#endif
5600#ifdef TARGET_NR_socket
5601 case TARGET_NR_socket:
5602 ret = do_socket(arg1, arg2, arg3);
5603 break;
5604#endif
5605#ifdef TARGET_NR_socketpair
5606 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00005607 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00005608 break;
5609#endif
5610#ifdef TARGET_NR_setsockopt
5611 case TARGET_NR_setsockopt:
5612 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5613 break;
5614#endif
ths7494b0f2007-02-11 18:26:53 +00005615
bellard31e31b82003-02-18 22:55:36 +00005616 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00005617 if (!(p = lock_user_string(arg2)))
5618 goto efault;
thse5574482007-02-11 20:03:13 +00005619 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5620 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00005621 break;
5622
bellard31e31b82003-02-18 22:55:36 +00005623 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00005624 {
bellard66fb9762003-03-23 01:06:05 +00005625 struct itimerval value, ovalue, *pvalue;
5626
pbrook53a59602006-03-25 19:31:22 +00005627 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005628 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00005629 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5630 || copy_from_user_timeval(&pvalue->it_value,
5631 arg2 + sizeof(struct target_timeval)))
5632 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005633 } else {
5634 pvalue = NULL;
5635 }
5636 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00005637 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00005638 if (copy_to_user_timeval(arg3,
5639 &ovalue.it_interval)
5640 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5641 &ovalue.it_value))
5642 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005643 }
5644 }
5645 break;
bellard31e31b82003-02-18 22:55:36 +00005646 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00005647 {
bellard66fb9762003-03-23 01:06:05 +00005648 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00005649
bellard66fb9762003-03-23 01:06:05 +00005650 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00005651 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00005652 if (copy_to_user_timeval(arg2,
5653 &value.it_interval)
5654 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5655 &value.it_value))
5656 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005657 }
5658 }
5659 break;
bellard31e31b82003-02-18 22:55:36 +00005660 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00005661 if (!(p = lock_user_string(arg1)))
5662 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005663 ret = get_errno(stat(path(p), &st));
5664 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005665 goto do_stat;
5666 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00005667 if (!(p = lock_user_string(arg1)))
5668 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005669 ret = get_errno(lstat(path(p), &st));
5670 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005671 goto do_stat;
5672 case TARGET_NR_fstat:
5673 {
5674 ret = get_errno(fstat(arg1, &st));
5675 do_stat:
5676 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005677 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00005678
bellard579a97f2007-11-11 14:26:47 +00005679 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5680 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02005681 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00005682 __put_user(st.st_dev, &target_st->st_dev);
5683 __put_user(st.st_ino, &target_st->st_ino);
5684 __put_user(st.st_mode, &target_st->st_mode);
5685 __put_user(st.st_uid, &target_st->st_uid);
5686 __put_user(st.st_gid, &target_st->st_gid);
5687 __put_user(st.st_nlink, &target_st->st_nlink);
5688 __put_user(st.st_rdev, &target_st->st_rdev);
5689 __put_user(st.st_size, &target_st->st_size);
5690 __put_user(st.st_blksize, &target_st->st_blksize);
5691 __put_user(st.st_blocks, &target_st->st_blocks);
5692 __put_user(st.st_atime, &target_st->target_st_atime);
5693 __put_user(st.st_mtime, &target_st->target_st_mtime);
5694 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00005695 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005696 }
5697 }
5698 break;
bellardebc05482003-09-30 21:08:41 +00005699#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00005700 case TARGET_NR_olduname:
5701 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005702#endif
5703#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00005704 case TARGET_NR_iopl:
5705 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005706#endif
bellard31e31b82003-02-18 22:55:36 +00005707 case TARGET_NR_vhangup:
5708 ret = get_errno(vhangup());
5709 break;
bellardebc05482003-09-30 21:08:41 +00005710#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00005711 case TARGET_NR_idle:
5712 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005713#endif
bellard42ad6ae2005-01-03 22:48:11 +00005714#ifdef TARGET_NR_syscall
5715 case TARGET_NR_syscall:
5716 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5717 break;
5718#endif
bellard31e31b82003-02-18 22:55:36 +00005719 case TARGET_NR_wait4:
5720 {
5721 int status;
blueswir1992f48a2007-10-14 16:27:31 +00005722 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00005723 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00005724 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00005725 if (target_rusage)
5726 rusage_ptr = &rusage;
5727 else
5728 rusage_ptr = NULL;
5729 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5730 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005731 if (status_ptr) {
pbrook1d9d8b52009-04-16 15:17:02 +00005732 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00005733 if (put_user_s32(status, status_ptr))
5734 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005735 }
bellard2f619692007-11-16 10:46:05 +00005736 if (target_rusage)
5737 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00005738 }
5739 }
5740 break;
thse5febef2007-04-01 18:31:35 +00005741#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00005742 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00005743 if (!(p = lock_user_string(arg1)))
5744 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005745 ret = get_errno(swapoff(p));
5746 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005747 break;
thse5febef2007-04-01 18:31:35 +00005748#endif
bellard31e31b82003-02-18 22:55:36 +00005749 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00005750 {
pbrook53a59602006-03-25 19:31:22 +00005751 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00005752 struct sysinfo value;
5753 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00005754 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00005755 {
bellard579a97f2007-11-11 14:26:47 +00005756 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5757 goto efault;
bellarda5448a72004-06-19 16:59:03 +00005758 __put_user(value.uptime, &target_value->uptime);
5759 __put_user(value.loads[0], &target_value->loads[0]);
5760 __put_user(value.loads[1], &target_value->loads[1]);
5761 __put_user(value.loads[2], &target_value->loads[2]);
5762 __put_user(value.totalram, &target_value->totalram);
5763 __put_user(value.freeram, &target_value->freeram);
5764 __put_user(value.sharedram, &target_value->sharedram);
5765 __put_user(value.bufferram, &target_value->bufferram);
5766 __put_user(value.totalswap, &target_value->totalswap);
5767 __put_user(value.freeswap, &target_value->freeswap);
5768 __put_user(value.procs, &target_value->procs);
5769 __put_user(value.totalhigh, &target_value->totalhigh);
5770 __put_user(value.freehigh, &target_value->freehigh);
5771 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00005772 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00005773 }
5774 }
5775 break;
thse5febef2007-04-01 18:31:35 +00005776#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00005777 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00005778 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5779 break;
thse5febef2007-04-01 18:31:35 +00005780#endif
aurel32e5289082009-04-18 16:16:12 +00005781#ifdef TARGET_NR_semget
5782 case TARGET_NR_semget:
5783 ret = get_errno(semget(arg1, arg2, arg3));
5784 break;
5785#endif
5786#ifdef TARGET_NR_semop
5787 case TARGET_NR_semop:
5788 ret = get_errno(do_semop(arg1, arg2, arg3));
5789 break;
5790#endif
5791#ifdef TARGET_NR_semctl
5792 case TARGET_NR_semctl:
5793 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5794 break;
5795#endif
aurel32eeb438c2008-10-13 21:08:55 +00005796#ifdef TARGET_NR_msgctl
5797 case TARGET_NR_msgctl:
5798 ret = do_msgctl(arg1, arg2, arg3);
5799 break;
5800#endif
5801#ifdef TARGET_NR_msgget
5802 case TARGET_NR_msgget:
5803 ret = get_errno(msgget(arg1, arg2));
5804 break;
5805#endif
5806#ifdef TARGET_NR_msgrcv
5807 case TARGET_NR_msgrcv:
5808 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5809 break;
5810#endif
5811#ifdef TARGET_NR_msgsnd
5812 case TARGET_NR_msgsnd:
5813 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5814 break;
5815#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03005816#ifdef TARGET_NR_shmget
5817 case TARGET_NR_shmget:
5818 ret = get_errno(shmget(arg1, arg2, arg3));
5819 break;
5820#endif
5821#ifdef TARGET_NR_shmctl
5822 case TARGET_NR_shmctl:
5823 ret = do_shmctl(arg1, arg2, arg3);
5824 break;
5825#endif
5826#ifdef TARGET_NR_shmat
5827 case TARGET_NR_shmat:
5828 ret = do_shmat(arg1, arg2, arg3);
5829 break;
5830#endif
5831#ifdef TARGET_NR_shmdt
5832 case TARGET_NR_shmdt:
5833 ret = do_shmdt(arg1);
5834 break;
5835#endif
bellard31e31b82003-02-18 22:55:36 +00005836 case TARGET_NR_fsync:
5837 ret = get_errno(fsync(arg1));
5838 break;
bellard31e31b82003-02-18 22:55:36 +00005839 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07005840#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00005841 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00005842#elif defined(TARGET_CRIS)
5843 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005844#else
pbrookd865bab2008-06-07 22:12:17 +00005845 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005846#endif
bellard1b6b0292003-03-22 17:31:38 +00005847 break;
bellardec86b0f2003-04-11 00:15:04 +00005848#ifdef __NR_exit_group
5849 /* new thread calls */
5850 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02005851#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00005852 _mcleanup();
5853#endif
bellarde9009672005-04-26 20:42:36 +00005854 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00005855 ret = get_errno(exit_group(arg1));
5856 break;
5857#endif
bellard31e31b82003-02-18 22:55:36 +00005858 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00005859 if (!(p = lock_user_string(arg1)))
5860 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005861 ret = get_errno(setdomainname(p, arg2));
5862 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005863 break;
5864 case TARGET_NR_uname:
5865 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00005866 {
5867 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00005868
bellard579a97f2007-11-11 14:26:47 +00005869 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5870 goto efault;
bellard29e619b2004-09-13 21:41:04 +00005871 ret = get_errno(sys_uname(buf));
5872 if (!is_error(ret)) {
5873 /* Overrite the native machine name with whatever is being
5874 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01005875 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00005876 /* Allow the user to override the reported release. */
5877 if (qemu_uname_release && *qemu_uname_release)
5878 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00005879 }
pbrook53a59602006-03-25 19:31:22 +00005880 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00005881 }
bellard31e31b82003-02-18 22:55:36 +00005882 break;
bellard6dbad632003-03-16 18:05:05 +00005883#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00005884 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00005885 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00005886 break;
j_mayer84409dd2007-04-06 08:56:50 +00005887#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00005888 case TARGET_NR_vm86old:
5889 goto unimplemented;
5890 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00005891 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00005892 break;
5893#endif
j_mayer84409dd2007-04-06 08:56:50 +00005894#endif
bellard31e31b82003-02-18 22:55:36 +00005895 case TARGET_NR_adjtimex:
5896 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005897#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00005898 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00005899#endif
bellard31e31b82003-02-18 22:55:36 +00005900 case TARGET_NR_init_module:
5901 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00005902#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00005903 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00005904#endif
bellard31e31b82003-02-18 22:55:36 +00005905 goto unimplemented;
5906 case TARGET_NR_quotactl:
5907 goto unimplemented;
5908 case TARGET_NR_getpgid:
5909 ret = get_errno(getpgid(arg1));
5910 break;
5911 case TARGET_NR_fchdir:
5912 ret = get_errno(fchdir(arg1));
5913 break;
j_mayer84409dd2007-04-06 08:56:50 +00005914#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00005915 case TARGET_NR_bdflush:
5916 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00005917#endif
thse5febef2007-04-01 18:31:35 +00005918#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00005919 case TARGET_NR_sysfs:
5920 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005921#endif
bellard31e31b82003-02-18 22:55:36 +00005922 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00005923 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00005924 break;
thse5febef2007-04-01 18:31:35 +00005925#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00005926 case TARGET_NR_afs_syscall:
5927 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005928#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005929#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005930 case TARGET_NR__llseek:
5931 {
bellard4f2ac232004-04-26 19:44:02 +00005932#if defined (__x86_64__)
5933 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00005934 if (put_user_s64(ret, arg4))
5935 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00005936#else
bellard31e31b82003-02-18 22:55:36 +00005937 int64_t res;
5938 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00005939 if (put_user_s64(res, arg4))
5940 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00005941#endif
bellard31e31b82003-02-18 22:55:36 +00005942 }
5943 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005944#endif
bellard31e31b82003-02-18 22:55:36 +00005945 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02005946#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00005947 {
pbrook53a59602006-03-25 19:31:22 +00005948 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00005949 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005950 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00005951
5952 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00005953 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00005954 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00005955 goto fail;
5956 }
ths3b46e622007-09-17 08:09:54 +00005957
bellard4add45b2003-06-05 01:52:59 +00005958 ret = get_errno(sys_getdents(arg1, dirp, count));
5959 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005960 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00005961 struct target_dirent *tde;
5962 int len = ret;
5963 int reclen, treclen;
5964 int count1, tnamelen;
5965
5966 count1 = 0;
5967 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00005968 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5969 goto efault;
bellard4add45b2003-06-05 01:52:59 +00005970 tde = target_dirp;
5971 while (len > 0) {
5972 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00005973 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00005974 tde->d_reclen = tswap16(treclen);
5975 tde->d_ino = tswapl(de->d_ino);
5976 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00005977 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00005978 if (tnamelen > 256)
5979 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00005980 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00005981 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00005982 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00005983 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00005984 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00005985 count1 += treclen;
5986 }
5987 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00005988 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00005989 }
5990 free(dirp);
5991 }
5992#else
bellard31e31b82003-02-18 22:55:36 +00005993 {
aurel326556a832008-10-13 21:08:17 +00005994 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005995 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00005996
bellard579a97f2007-11-11 14:26:47 +00005997 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5998 goto efault;
bellard72f03902003-02-18 23:33:18 +00005999 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00006000 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006001 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00006002 int len = ret;
6003 int reclen;
6004 de = dirp;
6005 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006006 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00006007 if (reclen > len)
6008 break;
bellard8083a3e2003-03-24 23:12:16 +00006009 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00006010 tswapls(&de->d_ino);
6011 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006012 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00006013 len -= reclen;
6014 }
6015 }
pbrook53a59602006-03-25 19:31:22 +00006016 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00006017 }
bellard4add45b2003-06-05 01:52:59 +00006018#endif
bellard31e31b82003-02-18 22:55:36 +00006019 break;
ths3ae43202007-09-16 21:39:48 +00006020#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00006021 case TARGET_NR_getdents64:
6022 {
aurel326556a832008-10-13 21:08:17 +00006023 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006024 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00006025 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6026 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00006027 ret = get_errno(sys_getdents64(arg1, dirp, count));
6028 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006029 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00006030 int len = ret;
6031 int reclen;
6032 de = dirp;
6033 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00006034 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00006035 if (reclen > len)
6036 break;
bellard8083a3e2003-03-24 23:12:16 +00006037 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00006038 tswap64s((uint64_t *)&de->d_ino);
6039 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00006040 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00006041 len -= reclen;
6042 }
6043 }
pbrook53a59602006-03-25 19:31:22 +00006044 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00006045 }
6046 break;
bellarda541f292004-04-12 20:39:29 +00006047#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00006048#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00006049 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00006050 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00006051 break;
thse5febef2007-04-01 18:31:35 +00006052#endif
6053#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00006054 case TARGET_NR_poll:
6055 {
pbrook53a59602006-03-25 19:31:22 +00006056 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00006057 unsigned int nfds = arg2;
6058 int timeout = arg3;
6059 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00006060 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00006061
bellard579a97f2007-11-11 14:26:47 +00006062 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6063 if (!target_pfd)
6064 goto efault;
bellard9de5e442003-03-23 16:49:39 +00006065 pfd = alloca(sizeof(struct pollfd) * nfds);
6066 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006067 pfd[i].fd = tswap32(target_pfd[i].fd);
6068 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00006069 }
6070 ret = get_errno(poll(pfd, nfds, timeout));
6071 if (!is_error(ret)) {
6072 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00006073 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00006074 }
pbrook53a59602006-03-25 19:31:22 +00006075 ret += nfds * (sizeof(struct target_pollfd)
6076 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00006077 }
pbrook53a59602006-03-25 19:31:22 +00006078 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00006079 }
6080 break;
thse5febef2007-04-01 18:31:35 +00006081#endif
bellard31e31b82003-02-18 22:55:36 +00006082 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00006083 /* NOTE: the flock constant seems to be the same for every
6084 Linux platform */
6085 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006086 break;
6087 case TARGET_NR_readv:
6088 {
6089 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006090 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006091
6092 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006093 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6094 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006095 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006096 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00006097 }
6098 break;
6099 case TARGET_NR_writev:
6100 {
6101 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00006102 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00006103
6104 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00006105 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6106 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006107 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00006108 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00006109 }
6110 break;
6111 case TARGET_NR_getsid:
6112 ret = get_errno(getsid(arg1));
6113 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006114#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00006115 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00006116 ret = get_errno(fdatasync(arg1));
6117 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006118#endif
bellard31e31b82003-02-18 22:55:36 +00006119 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00006120 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00006121 return value. */
ths0da46a62007-10-20 20:23:07 +00006122 ret = -TARGET_ENOTDIR;
6123 break;
bellard31e31b82003-02-18 22:55:36 +00006124 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00006125 {
pbrook53a59602006-03-25 19:31:22 +00006126 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006127 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00006128
bellard579a97f2007-11-11 14:26:47 +00006129 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6130 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006131 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006132 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00006133 ret = get_errno(sched_setparam(arg1, &schp));
6134 }
6135 break;
bellard31e31b82003-02-18 22:55:36 +00006136 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00006137 {
pbrook53a59602006-03-25 19:31:22 +00006138 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006139 struct sched_param schp;
6140 ret = get_errno(sched_getparam(arg1, &schp));
6141 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006142 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6143 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006144 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006145 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00006146 }
6147 }
6148 break;
bellard31e31b82003-02-18 22:55:36 +00006149 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00006150 {
pbrook53a59602006-03-25 19:31:22 +00006151 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00006152 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00006153 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6154 goto efault;
bellard5cd43932003-03-29 16:54:36 +00006155 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00006156 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00006157 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6158 }
6159 break;
bellard31e31b82003-02-18 22:55:36 +00006160 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00006161 ret = get_errno(sched_getscheduler(arg1));
6162 break;
bellard31e31b82003-02-18 22:55:36 +00006163 case TARGET_NR_sched_yield:
6164 ret = get_errno(sched_yield());
6165 break;
6166 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00006167 ret = get_errno(sched_get_priority_max(arg1));
6168 break;
bellard31e31b82003-02-18 22:55:36 +00006169 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00006170 ret = get_errno(sched_get_priority_min(arg1));
6171 break;
bellard31e31b82003-02-18 22:55:36 +00006172 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00006173 {
bellard5cd43932003-03-29 16:54:36 +00006174 struct timespec ts;
6175 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6176 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006177 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00006178 }
6179 }
6180 break;
bellard31e31b82003-02-18 22:55:36 +00006181 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00006182 {
bellard1b6b0292003-03-22 17:31:38 +00006183 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00006184 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00006185 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00006186 if (is_error(ret) && arg2) {
6187 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00006188 }
6189 }
6190 break;
thse5febef2007-04-01 18:31:35 +00006191#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00006192 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00006193 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006194#endif
6195#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00006196 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00006197 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006198#endif
bellard31e31b82003-02-18 22:55:36 +00006199 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00006200 switch (arg1)
6201 {
6202 case PR_GET_PDEATHSIG:
6203 {
6204 int deathsig;
6205 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00006206 if (!is_error(ret) && arg2
6207 && put_user_ual(deathsig, arg2))
6208 goto efault;
thse5574482007-02-11 20:03:13 +00006209 }
6210 break;
6211 default:
6212 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6213 break;
6214 }
ths39b9aae2007-02-11 18:36:44 +00006215 break;
bellardd2fd1af2007-11-14 18:08:56 +00006216#ifdef TARGET_NR_arch_prctl
6217 case TARGET_NR_arch_prctl:
6218#if defined(TARGET_I386) && !defined(TARGET_ABI32)
6219 ret = do_arch_prctl(cpu_env, arg1, arg2);
6220 break;
6221#else
6222 goto unimplemented;
6223#endif
6224#endif
bellard67867302003-11-23 17:05:30 +00006225#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00006226 case TARGET_NR_pread:
balroga4ae00b2008-09-20 03:14:14 +00006227#ifdef TARGET_ARM
6228 if (((CPUARMState *)cpu_env)->eabi)
6229 arg4 = arg5;
6230#endif
bellard579a97f2007-11-11 14:26:47 +00006231 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6232 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006233 ret = get_errno(pread(arg1, p, arg3, arg4));
6234 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00006235 break;
bellard31e31b82003-02-18 22:55:36 +00006236 case TARGET_NR_pwrite:
balroga4ae00b2008-09-20 03:14:14 +00006237#ifdef TARGET_ARM
6238 if (((CPUARMState *)cpu_env)->eabi)
6239 arg4 = arg5;
6240#endif
bellard579a97f2007-11-11 14:26:47 +00006241 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6242 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006243 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6244 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00006245 break;
bellard67867302003-11-23 17:05:30 +00006246#endif
aurel32f2c7ba12008-03-28 22:32:06 +00006247#ifdef TARGET_NR_pread64
6248 case TARGET_NR_pread64:
6249 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6250 goto efault;
6251 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6252 unlock_user(p, arg2, ret);
6253 break;
6254 case TARGET_NR_pwrite64:
6255 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6256 goto efault;
6257 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6258 unlock_user(p, arg2, 0);
6259 break;
6260#endif
bellard31e31b82003-02-18 22:55:36 +00006261 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00006262 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6263 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006264 ret = get_errno(sys_getcwd1(p, arg2));
6265 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00006266 break;
6267 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00006268 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006269 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00006270 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00006271 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00006272#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02006273 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6274 defined(TARGET_M68K)
bellard579a97f2007-11-11 14:26:47 +00006275 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00006276 break;
6277#else
bellard5cd43932003-03-29 16:54:36 +00006278 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00006279#endif
bellard31e31b82003-02-18 22:55:36 +00006280 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00006281 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006282#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00006283 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00006284 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006285#endif
6286#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00006287 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00006288 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006289#endif
bellard048f6b42005-11-26 18:47:20 +00006290#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00006291 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00006292 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6293 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00006294 break;
bellard048f6b42005-11-26 18:47:20 +00006295#endif
bellardebc05482003-09-30 21:08:41 +00006296#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00006297 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00006298 {
6299 struct rlimit rlim;
6300 ret = get_errno(getrlimit(arg1, &rlim));
6301 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006302 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00006303 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6304 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006305 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6306 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006307 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00006308 }
6309 break;
6310 }
bellardebc05482003-09-30 21:08:41 +00006311#endif
bellarda315a142005-01-30 22:59:18 +00006312#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00006313 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00006314 if (!(p = lock_user_string(arg1)))
6315 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006316 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6317 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00006318 break;
bellarda315a142005-01-30 22:59:18 +00006319#endif
6320#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00006321 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00006322 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00006323 break;
bellarda315a142005-01-30 22:59:18 +00006324#endif
6325#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00006326 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00006327 if (!(p = lock_user_string(arg1)))
6328 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006329 ret = get_errno(stat(path(p), &st));
6330 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006331 if (!is_error(ret))
6332 ret = host_to_target_stat64(cpu_env, arg2, &st);
6333 break;
bellarda315a142005-01-30 22:59:18 +00006334#endif
6335#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00006336 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00006337 if (!(p = lock_user_string(arg1)))
6338 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006339 ret = get_errno(lstat(path(p), &st));
6340 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00006341 if (!is_error(ret))
6342 ret = host_to_target_stat64(cpu_env, arg2, &st);
6343 break;
bellarda315a142005-01-30 22:59:18 +00006344#endif
6345#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00006346 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00006347 ret = get_errno(fstat(arg1, &st));
6348 if (!is_error(ret))
6349 ret = host_to_target_stat64(cpu_env, arg2, &st);
6350 break;
bellardec86b0f2003-04-11 00:15:04 +00006351#endif
aurel329d33b762009-04-08 23:07:05 +00006352#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6353 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6354#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006355 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00006356#endif
6357#ifdef TARGET_NR_newfstatat
6358 case TARGET_NR_newfstatat:
6359#endif
balrog6a24a772008-09-20 02:23:36 +00006360 if (!(p = lock_user_string(arg2)))
6361 goto efault;
aurel329d33b762009-04-08 23:07:05 +00006362#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00006363 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00006364#else
6365 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6366#endif
balrog6a24a772008-09-20 02:23:36 +00006367 if (!is_error(ret))
6368 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00006369 break;
bellarda315a142005-01-30 22:59:18 +00006370#endif
bellard67867302003-11-23 17:05:30 +00006371#ifdef USE_UID16
6372 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00006373 if (!(p = lock_user_string(arg1)))
6374 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006375 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6376 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006377 break;
6378 case TARGET_NR_getuid:
6379 ret = get_errno(high2lowuid(getuid()));
6380 break;
6381 case TARGET_NR_getgid:
6382 ret = get_errno(high2lowgid(getgid()));
6383 break;
6384 case TARGET_NR_geteuid:
6385 ret = get_errno(high2lowuid(geteuid()));
6386 break;
6387 case TARGET_NR_getegid:
6388 ret = get_errno(high2lowgid(getegid()));
6389 break;
6390 case TARGET_NR_setreuid:
6391 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6392 break;
6393 case TARGET_NR_setregid:
6394 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6395 break;
6396 case TARGET_NR_getgroups:
6397 {
6398 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006399 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006400 gid_t *grouplist;
6401 int i;
6402
6403 grouplist = alloca(gidsetsize * sizeof(gid_t));
6404 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006405 if (gidsetsize == 0)
6406 break;
bellard67867302003-11-23 17:05:30 +00006407 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006408 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6409 if (!target_grouplist)
6410 goto efault;
balroga2155fc2008-09-20 02:12:08 +00006411 for(i = 0;i < ret; i++)
bellard67867302003-11-23 17:05:30 +00006412 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006413 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00006414 }
6415 }
6416 break;
6417 case TARGET_NR_setgroups:
6418 {
6419 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006420 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00006421 gid_t *grouplist;
6422 int i;
6423
6424 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006425 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6426 if (!target_grouplist) {
6427 ret = -TARGET_EFAULT;
6428 goto fail;
6429 }
bellard67867302003-11-23 17:05:30 +00006430 for(i = 0;i < gidsetsize; i++)
6431 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00006432 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00006433 ret = get_errno(setgroups(gidsetsize, grouplist));
6434 }
6435 break;
6436 case TARGET_NR_fchown:
6437 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6438 break;
thsccfa72b2007-09-24 09:23:34 +00006439#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6440 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00006441 if (!(p = lock_user_string(arg2)))
6442 goto efault;
6443 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6444 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00006445 break;
6446#endif
bellard67867302003-11-23 17:05:30 +00006447#ifdef TARGET_NR_setresuid
6448 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00006449 ret = get_errno(setresuid(low2highuid(arg1),
6450 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00006451 low2highuid(arg3)));
6452 break;
6453#endif
6454#ifdef TARGET_NR_getresuid
6455 case TARGET_NR_getresuid:
6456 {
pbrook53a59602006-03-25 19:31:22 +00006457 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00006458 ret = get_errno(getresuid(&ruid, &euid, &suid));
6459 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006460 if (put_user_u16(high2lowuid(ruid), arg1)
6461 || put_user_u16(high2lowuid(euid), arg2)
6462 || put_user_u16(high2lowuid(suid), arg3))
6463 goto efault;
bellard67867302003-11-23 17:05:30 +00006464 }
6465 }
6466 break;
6467#endif
6468#ifdef TARGET_NR_getresgid
6469 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00006470 ret = get_errno(setresgid(low2highgid(arg1),
6471 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00006472 low2highgid(arg3)));
6473 break;
6474#endif
6475#ifdef TARGET_NR_getresgid
6476 case TARGET_NR_getresgid:
6477 {
pbrook53a59602006-03-25 19:31:22 +00006478 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00006479 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6480 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006481 if (put_user_u16(high2lowgid(rgid), arg1)
6482 || put_user_u16(high2lowgid(egid), arg2)
6483 || put_user_u16(high2lowgid(sgid), arg3))
6484 goto efault;
bellard67867302003-11-23 17:05:30 +00006485 }
6486 }
6487 break;
6488#endif
6489 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00006490 if (!(p = lock_user_string(arg1)))
6491 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006492 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6493 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00006494 break;
6495 case TARGET_NR_setuid:
6496 ret = get_errno(setuid(low2highuid(arg1)));
6497 break;
6498 case TARGET_NR_setgid:
6499 ret = get_errno(setgid(low2highgid(arg1)));
6500 break;
6501 case TARGET_NR_setfsuid:
6502 ret = get_errno(setfsuid(arg1));
6503 break;
6504 case TARGET_NR_setfsgid:
6505 ret = get_errno(setfsgid(arg1));
6506 break;
6507#endif /* USE_UID16 */
6508
bellarda315a142005-01-30 22:59:18 +00006509#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00006510 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00006511 if (!(p = lock_user_string(arg1)))
6512 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006513 ret = get_errno(lchown(p, arg2, arg3));
6514 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006515 break;
bellarda315a142005-01-30 22:59:18 +00006516#endif
6517#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00006518 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00006519 ret = get_errno(getuid());
6520 break;
bellarda315a142005-01-30 22:59:18 +00006521#endif
aurel3264b4d282008-11-14 17:20:15 +00006522
6523#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6524 /* Alpha specific */
6525 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006526 {
6527 uid_t euid;
6528 euid=geteuid();
6529 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6530 }
aurel3264b4d282008-11-14 17:20:15 +00006531 ret = get_errno(getuid());
6532 break;
6533#endif
6534#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6535 /* Alpha specific */
6536 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08006537 {
6538 uid_t egid;
6539 egid=getegid();
6540 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6541 }
aurel3264b4d282008-11-14 17:20:15 +00006542 ret = get_errno(getgid());
6543 break;
6544#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08006545#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
6546 /* Alpha specific */
6547 case TARGET_NR_osf_getsysinfo:
6548 ret = -TARGET_EOPNOTSUPP;
6549 switch (arg1) {
6550 case TARGET_GSI_IEEE_FP_CONTROL:
6551 {
6552 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
6553
6554 /* Copied from linux ieee_fpcr_to_swcr. */
6555 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
6556 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
6557 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
6558 | SWCR_TRAP_ENABLE_DZE
6559 | SWCR_TRAP_ENABLE_OVF);
6560 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
6561 | SWCR_TRAP_ENABLE_INE);
6562 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
6563 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
6564
6565 if (put_user_u64 (swcr, arg2))
6566 goto efault;
6567 ret = 0;
6568 }
6569 break;
6570
6571 /* case GSI_IEEE_STATE_AT_SIGNAL:
6572 -- Not implemented in linux kernel.
6573 case GSI_UACPROC:
6574 -- Retrieves current unaligned access state; not much used.
6575 case GSI_PROC_TYPE:
6576 -- Retrieves implver information; surely not used.
6577 case GSI_GET_HWRPB:
6578 -- Grabs a copy of the HWRPB; surely not used.
6579 */
6580 }
6581 break;
6582#endif
6583#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
6584 /* Alpha specific */
6585 case TARGET_NR_osf_setsysinfo:
6586 ret = -TARGET_EOPNOTSUPP;
6587 switch (arg1) {
6588 case TARGET_SSI_IEEE_FP_CONTROL:
6589 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
6590 {
6591 uint64_t swcr, fpcr, orig_fpcr;
6592
6593 if (get_user_u64 (swcr, arg2))
6594 goto efault;
6595 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
6596 fpcr = orig_fpcr & FPCR_DYN_MASK;
6597
6598 /* Copied from linux ieee_swcr_to_fpcr. */
6599 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
6600 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
6601 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
6602 | SWCR_TRAP_ENABLE_DZE
6603 | SWCR_TRAP_ENABLE_OVF)) << 48;
6604 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
6605 | SWCR_TRAP_ENABLE_INE)) << 57;
6606 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
6607 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
6608
6609 cpu_alpha_store_fpcr (cpu_env, fpcr);
6610 ret = 0;
6611
6612 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
6613 /* Old exceptions are not signaled. */
6614 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
6615
6616 /* If any exceptions set by this call, and are unmasked,
6617 send a signal. */
6618 /* ??? FIXME */
6619 }
6620 }
6621 break;
6622
6623 /* case SSI_NVPAIRS:
6624 -- Used with SSIN_UACPROC to enable unaligned accesses.
6625 case SSI_IEEE_STATE_AT_SIGNAL:
6626 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
6627 -- Not implemented in linux kernel
6628 */
6629 }
6630 break;
6631#endif
6632#ifdef TARGET_NR_osf_sigprocmask
6633 /* Alpha specific. */
6634 case TARGET_NR_osf_sigprocmask:
6635 {
6636 abi_ulong mask;
6637 int how = arg1;
6638 sigset_t set, oldset;
6639
6640 switch(arg1) {
6641 case TARGET_SIG_BLOCK:
6642 how = SIG_BLOCK;
6643 break;
6644 case TARGET_SIG_UNBLOCK:
6645 how = SIG_UNBLOCK;
6646 break;
6647 case TARGET_SIG_SETMASK:
6648 how = SIG_SETMASK;
6649 break;
6650 default:
6651 ret = -TARGET_EINVAL;
6652 goto fail;
6653 }
6654 mask = arg2;
6655 target_to_host_old_sigset(&set, &mask);
6656 sigprocmask(arg1, &set, &oldset);
6657 host_to_target_old_sigset(&mask, &oldset);
6658 ret = mask;
6659 }
6660 break;
6661#endif
aurel3264b4d282008-11-14 17:20:15 +00006662
bellarda315a142005-01-30 22:59:18 +00006663#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00006664 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00006665 ret = get_errno(getgid());
6666 break;
bellarda315a142005-01-30 22:59:18 +00006667#endif
6668#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00006669 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00006670 ret = get_errno(geteuid());
6671 break;
bellarda315a142005-01-30 22:59:18 +00006672#endif
6673#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00006674 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00006675 ret = get_errno(getegid());
6676 break;
bellarda315a142005-01-30 22:59:18 +00006677#endif
6678#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00006679 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00006680 ret = get_errno(setreuid(arg1, arg2));
6681 break;
bellarda315a142005-01-30 22:59:18 +00006682#endif
6683#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00006684 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00006685 ret = get_errno(setregid(arg1, arg2));
6686 break;
bellarda315a142005-01-30 22:59:18 +00006687#endif
6688#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00006689 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00006690 {
6691 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006692 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006693 gid_t *grouplist;
6694 int i;
6695
6696 grouplist = alloca(gidsetsize * sizeof(gid_t));
6697 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006698 if (gidsetsize == 0)
6699 break;
bellard99c475a2005-01-31 20:45:13 +00006700 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006701 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6702 if (!target_grouplist) {
6703 ret = -TARGET_EFAULT;
6704 goto fail;
6705 }
balroga2155fc2008-09-20 02:12:08 +00006706 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00006707 target_grouplist[i] = tswap32(grouplist[i]);
6708 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00006709 }
6710 }
6711 break;
bellarda315a142005-01-30 22:59:18 +00006712#endif
6713#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00006714 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00006715 {
6716 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006717 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006718 gid_t *grouplist;
6719 int i;
ths3b46e622007-09-17 08:09:54 +00006720
bellard99c475a2005-01-31 20:45:13 +00006721 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006722 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6723 if (!target_grouplist) {
6724 ret = -TARGET_EFAULT;
6725 goto fail;
6726 }
bellard99c475a2005-01-31 20:45:13 +00006727 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00006728 grouplist[i] = tswap32(target_grouplist[i]);
6729 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00006730 ret = get_errno(setgroups(gidsetsize, grouplist));
6731 }
6732 break;
bellarda315a142005-01-30 22:59:18 +00006733#endif
6734#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00006735 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00006736 ret = get_errno(fchown(arg1, arg2, arg3));
6737 break;
bellarda315a142005-01-30 22:59:18 +00006738#endif
6739#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00006740 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006741 ret = get_errno(setresuid(arg1, arg2, arg3));
6742 break;
bellarda315a142005-01-30 22:59:18 +00006743#endif
6744#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00006745 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006746 {
pbrook53a59602006-03-25 19:31:22 +00006747 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00006748 ret = get_errno(getresuid(&ruid, &euid, &suid));
6749 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006750 if (put_user_u32(ruid, arg1)
6751 || put_user_u32(euid, arg2)
6752 || put_user_u32(suid, arg3))
6753 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006754 }
6755 }
6756 break;
bellarda315a142005-01-30 22:59:18 +00006757#endif
6758#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00006759 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006760 ret = get_errno(setresgid(arg1, arg2, arg3));
6761 break;
bellarda315a142005-01-30 22:59:18 +00006762#endif
6763#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00006764 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006765 {
pbrook53a59602006-03-25 19:31:22 +00006766 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00006767 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6768 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006769 if (put_user_u32(rgid, arg1)
6770 || put_user_u32(egid, arg2)
6771 || put_user_u32(sgid, arg3))
6772 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006773 }
6774 }
6775 break;
bellarda315a142005-01-30 22:59:18 +00006776#endif
6777#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00006778 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00006779 if (!(p = lock_user_string(arg1)))
6780 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006781 ret = get_errno(chown(p, arg2, arg3));
6782 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006783 break;
bellarda315a142005-01-30 22:59:18 +00006784#endif
6785#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00006786 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00006787 ret = get_errno(setuid(arg1));
6788 break;
bellarda315a142005-01-30 22:59:18 +00006789#endif
6790#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00006791 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00006792 ret = get_errno(setgid(arg1));
6793 break;
bellarda315a142005-01-30 22:59:18 +00006794#endif
6795#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00006796 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00006797 ret = get_errno(setfsuid(arg1));
6798 break;
bellarda315a142005-01-30 22:59:18 +00006799#endif
6800#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00006801 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00006802 ret = get_errno(setfsgid(arg1));
6803 break;
bellarda315a142005-01-30 22:59:18 +00006804#endif
bellard67867302003-11-23 17:05:30 +00006805
bellard31e31b82003-02-18 22:55:36 +00006806 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00006807 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00006808#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00006809 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00006810 {
6811 void *a;
6812 ret = -TARGET_EFAULT;
6813 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6814 goto efault;
6815 if (!(p = lock_user_string(arg3)))
6816 goto mincore_fail;
6817 ret = get_errno(mincore(a, arg2, p));
6818 unlock_user(p, arg3, ret);
6819 mincore_fail:
6820 unlock_user(a, arg1, 0);
6821 }
6822 break;
bellardffa65c32004-01-04 23:57:22 +00006823#endif
aurel32408321b2008-10-01 21:46:32 +00006824#ifdef TARGET_NR_arm_fadvise64_64
6825 case TARGET_NR_arm_fadvise64_64:
6826 {
6827 /*
6828 * arm_fadvise64_64 looks like fadvise64_64 but
6829 * with different argument order
6830 */
6831 abi_long temp;
6832 temp = arg3;
6833 arg3 = arg4;
6834 arg4 = temp;
6835 }
6836#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02006837#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00006838#ifdef TARGET_NR_fadvise64_64
6839 case TARGET_NR_fadvise64_64:
6840#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02006841#ifdef TARGET_NR_fadvise64
6842 case TARGET_NR_fadvise64:
6843#endif
6844#ifdef TARGET_S390X
6845 switch (arg4) {
6846 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
6847 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
6848 case 6: arg4 = POSIX_FADV_DONTNEED; break;
6849 case 7: arg4 = POSIX_FADV_NOREUSE; break;
6850 default: break;
6851 }
6852#endif
6853 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00006854 break;
6855#endif
bellardffa65c32004-01-04 23:57:22 +00006856#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00006857 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00006858 /* A straight passthrough may not be safe because qemu sometimes
6859 turns private flie-backed mappings into anonymous mappings.
6860 This will break MADV_DONTNEED.
6861 This is a hint, so ignoring and returning success is ok. */
6862 ret = get_errno(0);
6863 break;
bellardffa65c32004-01-04 23:57:22 +00006864#endif
blueswir1992f48a2007-10-14 16:27:31 +00006865#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00006866 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00006867 {
thsb1e341e2007-03-20 21:50:52 +00006868 int cmd;
bellard77e46722003-04-29 20:39:06 +00006869 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00006870 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00006871#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00006872 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00006873#endif
bellard77e46722003-04-29 20:39:06 +00006874
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006875 cmd = target_to_host_fcntl_cmd(arg2);
6876 if (cmd == -TARGET_EINVAL)
6877 return cmd;
thsb1e341e2007-03-20 21:50:52 +00006878
bellard60cd49d2003-03-16 22:53:56 +00006879 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00006880 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00006881#ifdef TARGET_ARM
6882 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006883 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6884 goto efault;
ths58134272007-03-31 18:59:32 +00006885 fl.l_type = tswap16(target_efl->l_type);
6886 fl.l_whence = tswap16(target_efl->l_whence);
6887 fl.l_start = tswap64(target_efl->l_start);
6888 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006889 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00006890 unlock_user_struct(target_efl, arg3, 0);
6891 } else
6892#endif
6893 {
bellard9ee1fa22007-11-11 15:11:19 +00006894 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6895 goto efault;
ths58134272007-03-31 18:59:32 +00006896 fl.l_type = tswap16(target_fl->l_type);
6897 fl.l_whence = tswap16(target_fl->l_whence);
6898 fl.l_start = tswap64(target_fl->l_start);
6899 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006900 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00006901 unlock_user_struct(target_fl, arg3, 0);
6902 }
thsb1e341e2007-03-20 21:50:52 +00006903 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006904 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00006905#ifdef TARGET_ARM
6906 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006907 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6908 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006909 target_efl->l_type = tswap16(fl.l_type);
6910 target_efl->l_whence = tswap16(fl.l_whence);
6911 target_efl->l_start = tswap64(fl.l_start);
6912 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006913 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006914 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006915 } else
6916#endif
6917 {
bellard9ee1fa22007-11-11 15:11:19 +00006918 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6919 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006920 target_fl->l_type = tswap16(fl.l_type);
6921 target_fl->l_whence = tswap16(fl.l_whence);
6922 target_fl->l_start = tswap64(fl.l_start);
6923 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006924 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006925 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006926 }
bellard77e46722003-04-29 20:39:06 +00006927 }
6928 break;
6929
thsb1e341e2007-03-20 21:50:52 +00006930 case TARGET_F_SETLK64:
6931 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00006932#ifdef TARGET_ARM
6933 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006934 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6935 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006936 fl.l_type = tswap16(target_efl->l_type);
6937 fl.l_whence = tswap16(target_efl->l_whence);
6938 fl.l_start = tswap64(target_efl->l_start);
6939 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006940 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00006941 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00006942 } else
6943#endif
6944 {
bellard9ee1fa22007-11-11 15:11:19 +00006945 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6946 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006947 fl.l_type = tswap16(target_fl->l_type);
6948 fl.l_whence = tswap16(target_fl->l_whence);
6949 fl.l_start = tswap64(target_fl->l_start);
6950 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02006951 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00006952 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00006953 }
thsb1e341e2007-03-20 21:50:52 +00006954 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006955 break;
bellard60cd49d2003-03-16 22:53:56 +00006956 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02006957 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00006958 break;
6959 }
bellard77e46722003-04-29 20:39:06 +00006960 break;
6961 }
bellard60cd49d2003-03-16 22:53:56 +00006962#endif
ths7d600c82006-12-08 01:32:58 +00006963#ifdef TARGET_NR_cacheflush
6964 case TARGET_NR_cacheflush:
6965 /* self-modifying code is handled automatically, so nothing needed */
6966 ret = 0;
6967 break;
6968#endif
bellardebc05482003-09-30 21:08:41 +00006969#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00006970 case TARGET_NR_security:
6971 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006972#endif
bellardc573ff62004-01-04 15:51:36 +00006973#ifdef TARGET_NR_getpagesize
6974 case TARGET_NR_getpagesize:
6975 ret = TARGET_PAGE_SIZE;
6976 break;
6977#endif
bellard31e31b82003-02-18 22:55:36 +00006978 case TARGET_NR_gettid:
6979 ret = get_errno(gettid());
6980 break;
thse5febef2007-04-01 18:31:35 +00006981#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00006982 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00006983#if TARGET_ABI_BITS == 32
6984#ifdef TARGET_ARM
6985 if (((CPUARMState *)cpu_env)->eabi)
6986 {
6987 arg2 = arg3;
6988 arg3 = arg4;
6989 arg4 = arg5;
6990 }
6991#endif
6992 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
6993#else
6994 ret = get_errno(readahead(arg1, arg2, arg3));
6995#endif
6996 break;
thse5febef2007-04-01 18:31:35 +00006997#endif
bellardebc05482003-09-30 21:08:41 +00006998#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00006999 case TARGET_NR_setxattr:
7000 case TARGET_NR_lsetxattr:
7001 case TARGET_NR_fsetxattr:
7002 case TARGET_NR_getxattr:
7003 case TARGET_NR_lgetxattr:
7004 case TARGET_NR_fgetxattr:
7005 case TARGET_NR_listxattr:
7006 case TARGET_NR_llistxattr:
7007 case TARGET_NR_flistxattr:
7008 case TARGET_NR_removexattr:
7009 case TARGET_NR_lremovexattr:
7010 case TARGET_NR_fremovexattr:
Arnaud Patard6f932f92009-04-21 21:04:18 +03007011 ret = -TARGET_EOPNOTSUPP;
7012 break;
bellardebc05482003-09-30 21:08:41 +00007013#endif
7014#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00007015 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007016#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00007017 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
7018 ret = 0;
7019 break;
edgar_iglef967792009-01-07 14:19:38 +00007020#elif defined(TARGET_CRIS)
7021 if (arg1 & 0xff)
7022 ret = -TARGET_EINVAL;
7023 else {
7024 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
7025 ret = 0;
7026 }
7027 break;
bellard8d18e892007-11-14 15:18:40 +00007028#elif defined(TARGET_I386) && defined(TARGET_ABI32)
7029 ret = do_set_thread_area(cpu_env, arg1);
7030 break;
ths6f5b89a2007-03-02 20:48:00 +00007031#else
7032 goto unimplemented_nowarn;
7033#endif
7034#endif
7035#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00007036 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00007037#if defined(TARGET_I386) && defined(TARGET_ABI32)
7038 ret = do_get_thread_area(cpu_env, arg1);
7039#else
bellard5cd43932003-03-29 16:54:36 +00007040 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00007041#endif
bellard8d18e892007-11-14 15:18:40 +00007042#endif
bellard48dc41e2006-06-21 18:15:50 +00007043#ifdef TARGET_NR_getdomainname
7044 case TARGET_NR_getdomainname:
7045 goto unimplemented_nowarn;
7046#endif
ths6f5b89a2007-03-02 20:48:00 +00007047
thsb5906f92007-03-19 13:32:45 +00007048#ifdef TARGET_NR_clock_gettime
7049 case TARGET_NR_clock_gettime:
7050 {
7051 struct timespec ts;
7052 ret = get_errno(clock_gettime(arg1, &ts));
7053 if (!is_error(ret)) {
7054 host_to_target_timespec(arg2, &ts);
7055 }
7056 break;
7057 }
7058#endif
7059#ifdef TARGET_NR_clock_getres
7060 case TARGET_NR_clock_getres:
7061 {
7062 struct timespec ts;
7063 ret = get_errno(clock_getres(arg1, &ts));
7064 if (!is_error(ret)) {
7065 host_to_target_timespec(arg2, &ts);
7066 }
7067 break;
7068 }
7069#endif
pbrook63d76512008-05-29 13:43:29 +00007070#ifdef TARGET_NR_clock_nanosleep
7071 case TARGET_NR_clock_nanosleep:
7072 {
7073 struct timespec ts;
7074 target_to_host_timespec(&ts, arg3);
7075 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7076 if (arg4)
7077 host_to_target_timespec(arg4, &ts);
7078 break;
7079 }
7080#endif
thsb5906f92007-03-19 13:32:45 +00007081
ths6f5b89a2007-03-02 20:48:00 +00007082#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7083 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00007084 ret = get_errno(set_tid_address((int *)g2h(arg1)));
7085 break;
ths6f5b89a2007-03-02 20:48:00 +00007086#endif
7087
ths3ae43202007-09-16 21:39:48 +00007088#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00007089 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00007090 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00007091 break;
7092#endif
7093
ths3ae43202007-09-16 21:39:48 +00007094#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00007095 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00007096 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7097 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00007098 break;
7099#endif
7100
ths4f2b1fe2007-06-21 21:57:12 +00007101#ifdef TARGET_NR_set_robust_list
7102 case TARGET_NR_set_robust_list:
7103 goto unimplemented_nowarn;
7104#endif
7105
ths9007f0e2007-09-25 17:50:37 +00007106#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7107 case TARGET_NR_utimensat:
7108 {
Riku Voipioebc996f2009-04-21 15:01:51 +03007109 struct timespec *tsp, ts[2];
7110 if (!arg3) {
7111 tsp = NULL;
7112 } else {
7113 target_to_host_timespec(ts, arg3);
7114 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7115 tsp = ts;
7116 }
ths9007f0e2007-09-25 17:50:37 +00007117 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03007118 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00007119 else {
bellard579a97f2007-11-11 14:26:47 +00007120 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00007121 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00007122 goto fail;
7123 }
Riku Voipioebc996f2009-04-21 15:01:51 +03007124 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00007125 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00007126 }
7127 }
7128 break;
7129#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02007130#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00007131 case TARGET_NR_futex:
7132 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7133 break;
7134#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007135#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00007136 case TARGET_NR_inotify_init:
7137 ret = get_errno(sys_inotify_init());
7138 break;
7139#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007140#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00007141#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7142 case TARGET_NR_inotify_init1:
7143 ret = get_errno(sys_inotify_init1(arg1));
7144 break;
7145#endif
Stefan Weila1606b02010-03-28 11:44:41 +02007146#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007147#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00007148 case TARGET_NR_inotify_add_watch:
7149 p = lock_user_string(arg2);
7150 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7151 unlock_user(p, arg2, 0);
7152 break;
7153#endif
aurel32dbfe4c32009-04-08 21:29:30 +00007154#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00007155 case TARGET_NR_inotify_rm_watch:
7156 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7157 break;
7158#endif
ths9007f0e2007-09-25 17:50:37 +00007159
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07007160#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00007161 case TARGET_NR_mq_open:
7162 {
7163 struct mq_attr posix_mq_attr;
7164
7165 p = lock_user_string(arg1 - 1);
7166 if (arg4 != 0)
7167 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7168 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7169 unlock_user (p, arg1, 0);
7170 }
7171 break;
7172
7173 case TARGET_NR_mq_unlink:
7174 p = lock_user_string(arg1 - 1);
7175 ret = get_errno(mq_unlink(p));
7176 unlock_user (p, arg1, 0);
7177 break;
7178
7179 case TARGET_NR_mq_timedsend:
7180 {
7181 struct timespec ts;
7182
7183 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7184 if (arg5 != 0) {
7185 target_to_host_timespec(&ts, arg5);
7186 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7187 host_to_target_timespec(arg5, &ts);
7188 }
7189 else
7190 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7191 unlock_user (p, arg2, arg3);
7192 }
7193 break;
7194
7195 case TARGET_NR_mq_timedreceive:
7196 {
7197 struct timespec ts;
7198 unsigned int prio;
7199
7200 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7201 if (arg5 != 0) {
7202 target_to_host_timespec(&ts, arg5);
7203 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7204 host_to_target_timespec(arg5, &ts);
7205 }
7206 else
7207 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7208 unlock_user (p, arg2, arg3);
7209 if (arg4 != 0)
7210 put_user_u32(prio, arg4);
7211 }
7212 break;
7213
7214 /* Not implemented for now... */
7215/* case TARGET_NR_mq_notify: */
7216/* break; */
7217
7218 case TARGET_NR_mq_getsetattr:
7219 {
7220 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7221 ret = 0;
7222 if (arg3 != 0) {
7223 ret = mq_getattr(arg1, &posix_mq_attr_out);
7224 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7225 }
7226 if (arg2 != 0) {
7227 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7228 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7229 }
7230
7231 }
7232 break;
7233#endif
7234
vibisreenivasan3ce34df2009-05-16 18:32:41 +05307235#ifdef CONFIG_SPLICE
7236#ifdef TARGET_NR_tee
7237 case TARGET_NR_tee:
7238 {
7239 ret = get_errno(tee(arg1,arg2,arg3,arg4));
7240 }
7241 break;
7242#endif
7243#ifdef TARGET_NR_splice
7244 case TARGET_NR_splice:
7245 {
7246 loff_t loff_in, loff_out;
7247 loff_t *ploff_in = NULL, *ploff_out = NULL;
7248 if(arg2) {
7249 get_user_u64(loff_in, arg2);
7250 ploff_in = &loff_in;
7251 }
7252 if(arg4) {
7253 get_user_u64(loff_out, arg2);
7254 ploff_out = &loff_out;
7255 }
7256 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7257 }
7258 break;
7259#endif
7260#ifdef TARGET_NR_vmsplice
7261 case TARGET_NR_vmsplice:
7262 {
7263 int count = arg3;
7264 struct iovec *vec;
7265
7266 vec = alloca(count * sizeof(struct iovec));
7267 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7268 goto efault;
7269 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7270 unlock_iovec(vec, arg2, count, 0);
7271 }
7272 break;
7273#endif
7274#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03007275#ifdef CONFIG_EVENTFD
7276#if defined(TARGET_NR_eventfd)
7277 case TARGET_NR_eventfd:
7278 ret = get_errno(eventfd(arg1, 0));
7279 break;
7280#endif
7281#if defined(TARGET_NR_eventfd2)
7282 case TARGET_NR_eventfd2:
7283 ret = get_errno(eventfd(arg1, arg2));
7284 break;
7285#endif
7286#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03007287#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7288 case TARGET_NR_fallocate:
7289 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7290 break;
7291#endif
bellard31e31b82003-02-18 22:55:36 +00007292 default:
7293 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00007294 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00007295#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 +00007296 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00007297#endif
ths0da46a62007-10-20 20:23:07 +00007298 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00007299 break;
7300 }
bellard579a97f2007-11-11 14:26:47 +00007301fail:
bellardc573ff62004-01-04 15:51:36 +00007302#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00007303 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00007304#endif
thsb92c47c2007-11-01 00:07:38 +00007305 if(do_strace)
7306 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00007307 return ret;
bellard579a97f2007-11-11 14:26:47 +00007308efault:
7309 ret = -TARGET_EFAULT;
7310 goto fail;
bellard31e31b82003-02-18 22:55:36 +00007311}