blob: c7fcfc02c6976d80bb8a1607bad224ec38d2b135 [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>
John Spencerc56dc772012-12-10 07:59:46 +010031#include <grp.h>
bellard31e31b82003-02-18 22:55:36 +000032#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000033#include <sys/ipc.h>
34#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000035#include <sys/wait.h>
36#include <sys/time.h>
37#include <sys/stat.h>
38#include <sys/mount.h>
John Spencer586b0be2012-12-26 00:49:49 +010039#include <sys/file.h>
40#include <sys/fsuid.h>
41#include <sys/personality.h>
ths39b9aae2007-02-11 18:36:44 +000042#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000043#include <sys/resource.h>
44#include <sys/mman.h>
45#include <sys/swap.h>
46#include <signal.h>
47#include <sched.h>
Aurelien Jarno60e99242010-03-29 02:12:51 +020048#ifdef __ia64__
49int __clone2(int (*fn)(void *), void *child_stack_base,
50 size_t stack_size, int flags, void *arg, ...);
51#endif
bellard31e31b82003-02-18 22:55:36 +000052#include <sys/socket.h>
aurel32607175e2009-04-15 16:11:59 +000053#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000054#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000055#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000056#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000057#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000058#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000059#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000060#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000061#include <sys/sysinfo.h>
aurel323b3f24a2009-04-15 16:12:13 +000062#include <sys/utsname.h>
bellard72f03902003-02-18 23:33:18 +000063//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000064#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000065#include <netinet/tcp.h>
Laurent Vivier86fcd942011-03-30 01:35:23 +020066#include <linux/wireless.h>
Jing Huang920394d2012-07-24 13:59:23 +000067#include <linux/icmp.h>
Stefan Weil5a61cb62011-09-08 17:55:32 +020068#include "qemu-common.h"
Juan Quintela9788c9c2009-07-27 16:13:02 +020069#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +000070#include <sys/gmon.h>
71#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030072#ifdef CONFIG_EVENTFD
73#include <sys/eventfd.h>
74#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000075#ifdef CONFIG_EPOLL
76#include <sys/epoll.h>
77#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070078#ifdef CONFIG_ATTR
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010079#include "qemu/xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070080#endif
bellard31e31b82003-02-18 22:55:36 +000081
82#define termios host_termios
83#define winsize host_winsize
84#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000085#define sgttyb host_sgttyb /* same as target */
86#define tchars host_tchars /* same as target */
87#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000088
89#include <linux/termios.h>
90#include <linux/unistd.h>
91#include <linux/utsname.h>
92#include <linux/cdrom.h>
93#include <linux/hdreg.h>
94#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000095#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000096#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +030097#include <linux/fs.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000098#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +000099#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000100#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +0200101#include <linux/fb.h>
102#include <linux/vt.h>
Alexander Graf56e904e2012-01-31 18:42:06 +0100103#include <linux/dm-ioctl.h>
Laurent Vivierc07ecc62013-01-07 11:40:06 +0000104#include <linux/reboot.h>
pbrookd7e40362008-05-23 16:06:43 +0000105#include "linux_loop.h"
Loïc Minierda790302009-12-29 22:06:13 +0100106#include "cpu-uname.h"
bellard31e31b82003-02-18 22:55:36 +0000107
bellard3ef693a2003-03-23 20:17:16 +0000108#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000109
Juan Quintela2f7bb872009-07-27 16:13:24 +0200110#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +0000111#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
112 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
113#else
114/* XXX: Hardcode the above values. */
115#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +0000116#endif
117
bellard72f03902003-02-18 23:33:18 +0000118//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000119
bellard1a9353d2003-03-16 20:28:50 +0000120//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000121#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
122#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000123
bellard70a194b2003-08-11 22:20:16 +0000124
bellard70a194b2003-08-11 22:20:16 +0000125#undef _syscall0
126#undef _syscall1
127#undef _syscall2
128#undef _syscall3
129#undef _syscall4
130#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000131#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000132
bellard83fcb512006-06-14 13:37:16 +0000133#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000134static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000135{ \
136 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000137}
138
bellard83fcb512006-06-14 13:37:16 +0000139#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000140static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000141{ \
142 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000143}
144
bellard83fcb512006-06-14 13:37:16 +0000145#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000146static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000147{ \
148 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000149}
150
bellard83fcb512006-06-14 13:37:16 +0000151#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000152static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000153{ \
154 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000155}
156
bellard83fcb512006-06-14 13:37:16 +0000157#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000158static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000159{ \
160 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000161}
162
bellard83fcb512006-06-14 13:37:16 +0000163#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
164 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000165static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000166{ \
167 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000168}
bellard83fcb512006-06-14 13:37:16 +0000169
170
171#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
172 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000173static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
174 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000175{ \
176 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
177}
178
bellard70a194b2003-08-11 22:20:16 +0000179
bellard31e31b82003-02-18 22:55:36 +0000180#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000181#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000182#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000183#define __NR_sys_fchownat __NR_fchownat
balrog6a24a772008-09-20 02:23:36 +0000184#define __NR_sys_fstatat64 __NR_fstatat64
balrogac8a6552008-09-20 02:25:39 +0000185#define __NR_sys_futimesat __NR_futimesat
bellard72f03902003-02-18 23:33:18 +0000186#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000187#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000188#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000189#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000190#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000191#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000192#define __NR_sys_mknodat __NR_mknodat
aurel329d33b762009-04-08 23:07:05 +0000193#define __NR_sys_newfstatat __NR_newfstatat
ths82424832007-09-24 09:21:55 +0000194#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000195#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000196#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000197#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000198#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000199#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000200#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000201#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000202#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000203#define __NR_sys_utimensat __NR_utimensat
pbrookbd0c5662008-05-29 14:34:11 +0000204#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000205#define __NR_sys_inotify_init __NR_inotify_init
206#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
207#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000208
Alexander Graf42a39fb2011-04-15 17:32:45 +0200209#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
210 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000211#define __NR__llseek __NR_lseek
212#endif
213
bellard72f03902003-02-18 23:33:18 +0000214#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000215_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000216#else
ths0da46a62007-10-20 20:23:07 +0000217/* This is a replacement for the host gettid() and must return a host
218 errno. */
bellard72f03902003-02-18 23:33:18 +0000219static int gettid(void) {
220 return -ENOSYS;
221}
222#endif
aurel323b3f24a2009-04-15 16:12:13 +0000223_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
aurel323b3f24a2009-04-15 16:12:13 +0000224#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
225_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
226#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700227#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000228_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
229 loff_t *, res, uint, wh);
230#endif
231_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
232_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
233#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
234_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
235#endif
236#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
237_syscall2(int,sys_tkill,int,tid,int,sig)
238#endif
239#ifdef __NR_exit_group
240_syscall1(int,exit_group,int,error_code)
241#endif
242#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
243_syscall1(int,set_tid_address,int *,tidptr)
244#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +0200245#if defined(CONFIG_USE_NPTL)
aurel323b3f24a2009-04-15 16:12:13 +0000246#if defined(TARGET_NR_futex) && defined(__NR_futex)
247_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
248 const struct timespec *,timeout,int *,uaddr2,int,val3)
249#endif
250#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500251#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
252_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
253 unsigned long *, user_mask_ptr);
254#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
255_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
256 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200257_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
258 void *, arg);
aurel323b3f24a2009-04-15 16:12:13 +0000259
260static bitmask_transtbl fcntl_flags_tbl[] = {
261 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
262 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
263 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
264 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
265 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
266 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
267 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
268 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700269 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000270 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
271 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
272 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
273 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000274#if defined(O_DIRECT)
275 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
276#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700277#if defined(O_NOATIME)
278 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
279#endif
280#if defined(O_CLOEXEC)
281 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
282#endif
283#if defined(O_PATH)
284 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
285#endif
286 /* Don't terminate the list prematurely on 64-bit host+guest. */
287#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
288 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
289#endif
aurel323b3f24a2009-04-15 16:12:13 +0000290 { 0, 0, 0, 0 }
291};
292
293#define COPY_UTSNAME_FIELD(dest, src) \
294 do { \
295 /* __NEW_UTS_LEN doesn't include terminating null */ \
296 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
297 (dest)[__NEW_UTS_LEN] = '\0'; \
298 } while (0)
299
300static int sys_uname(struct new_utsname *buf)
301{
302 struct utsname uts_buf;
303
304 if (uname(&uts_buf) < 0)
305 return (-1);
306
307 /*
308 * Just in case these have some differences, we
309 * translate utsname to new_utsname (which is the
310 * struct linux kernel uses).
311 */
312
Stefan Weil67bd9ed2011-05-07 22:32:25 +0200313 memset(buf, 0, sizeof(*buf));
aurel323b3f24a2009-04-15 16:12:13 +0000314 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
315 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
316 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
317 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
318 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
319#ifdef _GNU_SOURCE
320 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
321#endif
322 return (0);
323
324#undef COPY_UTSNAME_FIELD
325}
326
327static int sys_getcwd1(char *buf, size_t size)
328{
329 if (getcwd(buf, size) == NULL) {
330 /* getcwd() sets errno */
331 return (-1);
332 }
aurel32aaf4ad32009-04-16 14:17:14 +0000333 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000334}
335
336#ifdef CONFIG_ATFILE
337/*
338 * Host system seems to have atfile syscall stubs available. We
339 * now enable them one by one as specified by target syscall_nr.h.
340 */
341
342#ifdef TARGET_NR_faccessat
aurel32465c9f02009-04-19 08:52:17 +0000343static int sys_faccessat(int dirfd, const char *pathname, int mode)
aurel323b3f24a2009-04-15 16:12:13 +0000344{
aurel32465c9f02009-04-19 08:52:17 +0000345 return (faccessat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000346}
347#endif
348#ifdef TARGET_NR_fchmodat
aurel32465c9f02009-04-19 08:52:17 +0000349static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000350{
aurel32465c9f02009-04-19 08:52:17 +0000351 return (fchmodat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000352}
353#endif
Riku Voipio0c866a72011-04-18 15:23:06 +0300354#if defined(TARGET_NR_fchownat)
aurel323b3f24a2009-04-15 16:12:13 +0000355static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
356 gid_t group, int flags)
357{
358 return (fchownat(dirfd, pathname, owner, group, flags));
359}
360#endif
361#ifdef __NR_fstatat64
362static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
363 int flags)
364{
365 return (fstatat(dirfd, pathname, buf, flags));
366}
367#endif
368#ifdef __NR_newfstatat
369static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
370 int flags)
371{
372 return (fstatat(dirfd, pathname, buf, flags));
373}
374#endif
375#ifdef TARGET_NR_futimesat
376static int sys_futimesat(int dirfd, const char *pathname,
377 const struct timeval times[2])
378{
379 return (futimesat(dirfd, pathname, times));
380}
381#endif
382#ifdef TARGET_NR_linkat
383static int sys_linkat(int olddirfd, const char *oldpath,
384 int newdirfd, const char *newpath, int flags)
385{
386 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
387}
388#endif
389#ifdef TARGET_NR_mkdirat
390static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
391{
392 return (mkdirat(dirfd, pathname, mode));
393}
394#endif
395#ifdef TARGET_NR_mknodat
396static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
397 dev_t dev)
398{
399 return (mknodat(dirfd, pathname, mode, dev));
400}
401#endif
402#ifdef TARGET_NR_openat
Alexander Graff4c69012011-09-25 06:25:35 +0200403static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000404{
405 /*
406 * open(2) has extra parameter 'mode' when called with
407 * flag O_CREAT.
408 */
409 if ((flags & O_CREAT) != 0) {
aurel323b3f24a2009-04-15 16:12:13 +0000410 return (openat(dirfd, pathname, flags, mode));
411 }
412 return (openat(dirfd, pathname, flags));
413}
414#endif
415#ifdef TARGET_NR_readlinkat
416static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
417{
418 return (readlinkat(dirfd, pathname, buf, bufsiz));
419}
420#endif
421#ifdef TARGET_NR_renameat
422static int sys_renameat(int olddirfd, const char *oldpath,
423 int newdirfd, const char *newpath)
424{
425 return (renameat(olddirfd, oldpath, newdirfd, newpath));
426}
427#endif
428#ifdef TARGET_NR_symlinkat
429static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
430{
431 return (symlinkat(oldpath, newdirfd, newpath));
432}
433#endif
434#ifdef TARGET_NR_unlinkat
435static int sys_unlinkat(int dirfd, const char *pathname, int flags)
436{
437 return (unlinkat(dirfd, pathname, flags));
438}
439#endif
aurel323b3f24a2009-04-15 16:12:13 +0000440#else /* !CONFIG_ATFILE */
441
442/*
443 * Try direct syscalls instead
444 */
ths92a34c12007-09-24 09:27:49 +0000445#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
aurel32465c9f02009-04-19 08:52:17 +0000446_syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
ths92a34c12007-09-24 09:27:49 +0000447#endif
ths814d7972007-09-24 09:26:51 +0000448#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
aurel32465c9f02009-04-19 08:52:17 +0000449_syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
ths814d7972007-09-24 09:26:51 +0000450#endif
Riku Voipio0c866a72011-04-18 15:23:06 +0300451#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +0000452_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
453 uid_t,owner,gid_t,group,int,flags)
454#endif
aurel329d33b762009-04-08 23:07:05 +0000455#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
456 defined(__NR_fstatat64)
balrog6a24a772008-09-20 02:23:36 +0000457_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
458 struct stat *,buf,int,flags)
459#endif
balrogac8a6552008-09-20 02:25:39 +0000460#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
461_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
462 const struct timeval *,times)
463#endif
aurel323b3f24a2009-04-15 16:12:13 +0000464#if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
465 defined(__NR_newfstatat)
466_syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
467 struct stat *,buf,int,flags)
blueswir18fcd3692008-08-17 20:26:25 +0000468#endif
ths64f0ce42007-09-24 09:25:06 +0000469#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
470_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
aurel323b3f24a2009-04-15 16:12:13 +0000471 int,newdirfd,const char *,newpath,int,flags)
ths64f0ce42007-09-24 09:25:06 +0000472#endif
ths4472ad02007-09-24 09:22:32 +0000473#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
474_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
475#endif
ths75ac37a2007-09-24 09:23:05 +0000476#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
477_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
478 mode_t,mode,dev_t,dev)
479#endif
ths82424832007-09-24 09:21:55 +0000480#if defined(TARGET_NR_openat) && defined(__NR_openat)
481_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
482#endif
ths5e0ccb12007-09-24 09:26:10 +0000483#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
484_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
485 char *,buf,size_t,bufsize)
486#endif
ths722183f2007-09-24 09:24:37 +0000487#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
488_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
489 int,newdirfd,const char *,newpath)
490#endif
thsb51eaa82007-09-25 16:09:22 +0000491#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000492_syscall3(int,sys_symlinkat,const char *,oldpath,
493 int,newdirfd,const char *,newpath)
494#endif
ths8170f562007-09-24 09:24:11 +0000495#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
496_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
497#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300498
499#endif /* CONFIG_ATFILE */
500
501#ifdef CONFIG_UTIMENSAT
502static int sys_utimensat(int dirfd, const char *pathname,
503 const struct timespec times[2], int flags)
504{
505 if (pathname == NULL)
506 return futimens(dirfd, times);
507 else
508 return utimensat(dirfd, pathname, times, flags);
509}
510#else
ths9007f0e2007-09-25 17:50:37 +0000511#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
512_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
513 const struct timespec *,tsp,int,flags)
514#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300515#endif /* CONFIG_UTIMENSAT */
aurel323b3f24a2009-04-15 16:12:13 +0000516
517#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000518#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000519
aurel3239b59762008-10-01 21:46:50 +0000520#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000521static int sys_inotify_init(void)
522{
523 return (inotify_init());
524}
aurel3239b59762008-10-01 21:46:50 +0000525#endif
526#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000527static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
528{
529 return (inotify_add_watch(fd, pathname, mask));
530}
aurel3239b59762008-10-01 21:46:50 +0000531#endif
532#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000533static int sys_inotify_rm_watch(int fd, int32_t wd)
534{
aurel328690e422009-04-17 13:50:32 +0000535 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000536}
aurel3239b59762008-10-01 21:46:50 +0000537#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000538#ifdef CONFIG_INOTIFY1
539#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
540static int sys_inotify_init1(int flags)
541{
542 return (inotify_init1(flags));
543}
544#endif
545#endif
aurel323b3f24a2009-04-15 16:12:13 +0000546#else
547/* Userspace can usually survive runtime without inotify */
548#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000549#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000550#undef TARGET_NR_inotify_add_watch
551#undef TARGET_NR_inotify_rm_watch
552#endif /* CONFIG_INOTIFY */
553
Mike Frysingerd8035d42011-02-07 01:05:51 -0500554#if defined(TARGET_NR_ppoll)
555#ifndef __NR_ppoll
556# define __NR_ppoll -1
557#endif
558#define __NR_sys_ppoll __NR_ppoll
559_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
560 struct timespec *, timeout, const __sigset_t *, sigmask,
561 size_t, sigsetsize)
562#endif
bellard66fb9762003-03-23 01:06:05 +0000563
Mike Frysinger055e0902011-06-03 17:01:49 -0400564#if defined(TARGET_NR_pselect6)
565#ifndef __NR_pselect6
566# define __NR_pselect6 -1
567#endif
568#define __NR_sys_pselect6 __NR_pselect6
569_syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
570 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
571#endif
572
Peter Maydell163a05a2011-06-27 17:44:52 +0100573#if defined(TARGET_NR_prlimit64)
574#ifndef __NR_prlimit64
575# define __NR_prlimit64 -1
576#endif
577#define __NR_sys_prlimit64 __NR_prlimit64
578/* The glibc rlimit structure may not be that used by the underlying syscall */
579struct host_rlimit64 {
580 uint64_t rlim_cur;
581 uint64_t rlim_max;
582};
583_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
584 const struct host_rlimit64 *, new_limit,
585 struct host_rlimit64 *, old_limit)
586#endif
587
Riku Voipio48e515d2011-07-12 15:40:51 +0300588/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
Alexander Graf4a1def42012-09-29 15:32:38 +0000589#ifdef TARGET_ARM
Riku Voipio48e515d2011-07-12 15:40:51 +0300590static inline int regpairs_aligned(void *cpu_env) {
591 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
592}
593#elif defined(TARGET_MIPS)
594static inline int regpairs_aligned(void *cpu_env) { return 1; }
Alexander Graf4a1def42012-09-29 15:32:38 +0000595#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
596/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
597 * of registers which translates to the same as ARM/MIPS, because we start with
598 * r3 as arg1 */
599static inline int regpairs_aligned(void *cpu_env) { return 1; }
Riku Voipio48e515d2011-07-12 15:40:51 +0300600#else
601static inline int regpairs_aligned(void *cpu_env) { return 0; }
602#endif
603
thsb92c47c2007-11-01 00:07:38 +0000604#define ERRNO_TABLE_SIZE 1200
605
606/* target_to_host_errno_table[] is initialized from
607 * host_to_target_errno_table[] in syscall_init(). */
608static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
609};
610
ths637947f2007-06-01 12:09:19 +0000611/*
thsfe8f0962007-07-12 10:59:21 +0000612 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000613 * minus the errnos that are not actually generic to all archs.
614 */
thsb92c47c2007-11-01 00:07:38 +0000615static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000616 [EIDRM] = TARGET_EIDRM,
617 [ECHRNG] = TARGET_ECHRNG,
618 [EL2NSYNC] = TARGET_EL2NSYNC,
619 [EL3HLT] = TARGET_EL3HLT,
620 [EL3RST] = TARGET_EL3RST,
621 [ELNRNG] = TARGET_ELNRNG,
622 [EUNATCH] = TARGET_EUNATCH,
623 [ENOCSI] = TARGET_ENOCSI,
624 [EL2HLT] = TARGET_EL2HLT,
625 [EDEADLK] = TARGET_EDEADLK,
626 [ENOLCK] = TARGET_ENOLCK,
627 [EBADE] = TARGET_EBADE,
628 [EBADR] = TARGET_EBADR,
629 [EXFULL] = TARGET_EXFULL,
630 [ENOANO] = TARGET_ENOANO,
631 [EBADRQC] = TARGET_EBADRQC,
632 [EBADSLT] = TARGET_EBADSLT,
633 [EBFONT] = TARGET_EBFONT,
634 [ENOSTR] = TARGET_ENOSTR,
635 [ENODATA] = TARGET_ENODATA,
636 [ETIME] = TARGET_ETIME,
637 [ENOSR] = TARGET_ENOSR,
638 [ENONET] = TARGET_ENONET,
639 [ENOPKG] = TARGET_ENOPKG,
640 [EREMOTE] = TARGET_EREMOTE,
641 [ENOLINK] = TARGET_ENOLINK,
642 [EADV] = TARGET_EADV,
643 [ESRMNT] = TARGET_ESRMNT,
644 [ECOMM] = TARGET_ECOMM,
645 [EPROTO] = TARGET_EPROTO,
646 [EDOTDOT] = TARGET_EDOTDOT,
647 [EMULTIHOP] = TARGET_EMULTIHOP,
648 [EBADMSG] = TARGET_EBADMSG,
649 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
650 [EOVERFLOW] = TARGET_EOVERFLOW,
651 [ENOTUNIQ] = TARGET_ENOTUNIQ,
652 [EBADFD] = TARGET_EBADFD,
653 [EREMCHG] = TARGET_EREMCHG,
654 [ELIBACC] = TARGET_ELIBACC,
655 [ELIBBAD] = TARGET_ELIBBAD,
656 [ELIBSCN] = TARGET_ELIBSCN,
657 [ELIBMAX] = TARGET_ELIBMAX,
658 [ELIBEXEC] = TARGET_ELIBEXEC,
659 [EILSEQ] = TARGET_EILSEQ,
660 [ENOSYS] = TARGET_ENOSYS,
661 [ELOOP] = TARGET_ELOOP,
662 [ERESTART] = TARGET_ERESTART,
663 [ESTRPIPE] = TARGET_ESTRPIPE,
664 [ENOTEMPTY] = TARGET_ENOTEMPTY,
665 [EUSERS] = TARGET_EUSERS,
666 [ENOTSOCK] = TARGET_ENOTSOCK,
667 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
668 [EMSGSIZE] = TARGET_EMSGSIZE,
669 [EPROTOTYPE] = TARGET_EPROTOTYPE,
670 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
671 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
672 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
673 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
674 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
675 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
676 [EADDRINUSE] = TARGET_EADDRINUSE,
677 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
678 [ENETDOWN] = TARGET_ENETDOWN,
679 [ENETUNREACH] = TARGET_ENETUNREACH,
680 [ENETRESET] = TARGET_ENETRESET,
681 [ECONNABORTED] = TARGET_ECONNABORTED,
682 [ECONNRESET] = TARGET_ECONNRESET,
683 [ENOBUFS] = TARGET_ENOBUFS,
684 [EISCONN] = TARGET_EISCONN,
685 [ENOTCONN] = TARGET_ENOTCONN,
686 [EUCLEAN] = TARGET_EUCLEAN,
687 [ENOTNAM] = TARGET_ENOTNAM,
688 [ENAVAIL] = TARGET_ENAVAIL,
689 [EISNAM] = TARGET_EISNAM,
690 [EREMOTEIO] = TARGET_EREMOTEIO,
691 [ESHUTDOWN] = TARGET_ESHUTDOWN,
692 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
693 [ETIMEDOUT] = TARGET_ETIMEDOUT,
694 [ECONNREFUSED] = TARGET_ECONNREFUSED,
695 [EHOSTDOWN] = TARGET_EHOSTDOWN,
696 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
697 [EALREADY] = TARGET_EALREADY,
698 [EINPROGRESS] = TARGET_EINPROGRESS,
699 [ESTALE] = TARGET_ESTALE,
700 [ECANCELED] = TARGET_ECANCELED,
701 [ENOMEDIUM] = TARGET_ENOMEDIUM,
702 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000703#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000704 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000705#endif
706#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000707 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000708#endif
709#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000710 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000711#endif
712#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000713 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000714#endif
715#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000716 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000717#endif
718#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000719 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000720#endif
thsb92c47c2007-11-01 00:07:38 +0000721};
ths637947f2007-06-01 12:09:19 +0000722
723static inline int host_to_target_errno(int err)
724{
725 if(host_to_target_errno_table[err])
726 return host_to_target_errno_table[err];
727 return err;
728}
729
thsb92c47c2007-11-01 00:07:38 +0000730static inline int target_to_host_errno(int err)
731{
732 if (target_to_host_errno_table[err])
733 return target_to_host_errno_table[err];
734 return err;
735}
736
blueswir1992f48a2007-10-14 16:27:31 +0000737static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000738{
739 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000740 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000741 else
742 return ret;
743}
744
blueswir1992f48a2007-10-14 16:27:31 +0000745static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000746{
blueswir1992f48a2007-10-14 16:27:31 +0000747 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000748}
749
thsb92c47c2007-11-01 00:07:38 +0000750char *target_strerror(int err)
751{
Alexander Graf962b2892011-11-21 12:04:07 +0100752 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
753 return NULL;
754 }
thsb92c47c2007-11-01 00:07:38 +0000755 return strerror(target_to_host_errno(err));
756}
757
blueswir1992f48a2007-10-14 16:27:31 +0000758static abi_ulong target_brk;
759static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000760static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000761
blueswir1992f48a2007-10-14 16:27:31 +0000762void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000763{
blueswir14c1de732007-07-07 20:45:44 +0000764 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000765 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000766}
767
vincent4d1de872011-06-14 21:56:33 +0000768//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
769#define DEBUGF_BRK(message, args...)
770
ths0da46a62007-10-20 20:23:07 +0000771/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000772abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000773{
blueswir1992f48a2007-10-14 16:27:31 +0000774 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000775 int new_alloc_size;
776
Paul Brook3a0c6c42012-02-09 19:04:27 +0000777 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000778
vincent4d1de872011-06-14 21:56:33 +0000779 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000780 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000781 return target_brk;
782 }
783 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000784 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
785 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000786 return target_brk;
787 }
bellard31e31b82003-02-18 22:55:36 +0000788
vincent4d1de872011-06-14 21:56:33 +0000789 /* If the new brk is less than the highest page reserved to the
790 * target heap allocation, set it and we're almost done... */
791 if (new_brk <= brk_page) {
792 /* Heap contents are initialized to zero, as for anonymous
793 * mapped pages. */
794 if (new_brk > target_brk) {
795 memset(g2h(target_brk), 0, new_brk - target_brk);
796 }
bellard31e31b82003-02-18 22:55:36 +0000797 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000798 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000799 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000800 }
801
Peter Maydell00faf082011-04-18 16:34:24 +0100802 /* We need to allocate more memory after the brk... Note that
803 * we don't use MAP_FIXED because that will map over the top of
804 * any existing mapping (like the one with the host libc or qemu
805 * itself); instead we treat "mapped but at wrong address" as
806 * a failure and unmap again.
807 */
vincent4d1de872011-06-14 21:56:33 +0000808 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000809 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000810 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100811 MAP_ANON|MAP_PRIVATE, 0, 0));
812
813 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200814 /* Heap contents are initialized to zero, as for anonymous
815 * mapped pages. Technically the new pages are already
816 * initialized to zero since they *are* anonymous mapped
817 * pages, however we have to take care with the contents that
818 * come from the remaining part of the previous page: it may
819 * contains garbage data due to a previous heap usage (grown
820 * then shrunken). */
821 memset(g2h(target_brk), 0, brk_page - target_brk);
822
Peter Maydell00faf082011-04-18 16:34:24 +0100823 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000824 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000825 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
826 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100827 return target_brk;
828 } else if (mapped_addr != -1) {
829 /* Mapped but at wrong address, meaning there wasn't actually
830 * enough space for this brk.
831 */
832 target_munmap(mapped_addr, new_alloc_size);
833 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000834 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000835 }
836 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000837 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100838 }
balrog7ab240a2008-04-26 12:17:34 +0000839
Richard Henderson7dd46c02010-05-03 10:07:49 -0700840#if defined(TARGET_ALPHA)
841 /* We (partially) emulate OSF/1 on Alpha, which requires we
842 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100843 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700844#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100845 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000846 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000847}
848
ths26edcf42007-12-09 02:25:24 +0000849static inline abi_long copy_from_user_fdset(fd_set *fds,
850 abi_ulong target_fds_addr,
851 int n)
bellard31e31b82003-02-18 22:55:36 +0000852{
ths26edcf42007-12-09 02:25:24 +0000853 int i, nw, j, k;
854 abi_ulong b, *target_fds;
855
856 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
857 if (!(target_fds = lock_user(VERIFY_READ,
858 target_fds_addr,
859 sizeof(abi_ulong) * nw,
860 1)))
861 return -TARGET_EFAULT;
862
863 FD_ZERO(fds);
864 k = 0;
865 for (i = 0; i < nw; i++) {
866 /* grab the abi_ulong */
867 __get_user(b, &target_fds[i]);
868 for (j = 0; j < TARGET_ABI_BITS; j++) {
869 /* check the bit inside the abi_ulong */
870 if ((b >> j) & 1)
871 FD_SET(k, fds);
872 k++;
bellard31e31b82003-02-18 22:55:36 +0000873 }
bellard31e31b82003-02-18 22:55:36 +0000874 }
ths26edcf42007-12-09 02:25:24 +0000875
876 unlock_user(target_fds, target_fds_addr, 0);
877
878 return 0;
bellard31e31b82003-02-18 22:55:36 +0000879}
880
Mike Frysinger055e0902011-06-03 17:01:49 -0400881static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
882 abi_ulong target_fds_addr,
883 int n)
884{
885 if (target_fds_addr) {
886 if (copy_from_user_fdset(fds, target_fds_addr, n))
887 return -TARGET_EFAULT;
888 *fds_ptr = fds;
889 } else {
890 *fds_ptr = NULL;
891 }
892 return 0;
893}
894
ths26edcf42007-12-09 02:25:24 +0000895static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
896 const fd_set *fds,
897 int n)
bellard31e31b82003-02-18 22:55:36 +0000898{
bellard31e31b82003-02-18 22:55:36 +0000899 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000900 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000901 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000902
ths26edcf42007-12-09 02:25:24 +0000903 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
904 if (!(target_fds = lock_user(VERIFY_WRITE,
905 target_fds_addr,
906 sizeof(abi_ulong) * nw,
907 0)))
908 return -TARGET_EFAULT;
909
910 k = 0;
911 for (i = 0; i < nw; i++) {
912 v = 0;
913 for (j = 0; j < TARGET_ABI_BITS; j++) {
914 v |= ((FD_ISSET(k, fds) != 0) << j);
915 k++;
bellard31e31b82003-02-18 22:55:36 +0000916 }
ths26edcf42007-12-09 02:25:24 +0000917 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000918 }
ths26edcf42007-12-09 02:25:24 +0000919
920 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
921
922 return 0;
bellard31e31b82003-02-18 22:55:36 +0000923}
924
bellardc596ed12003-07-13 17:32:31 +0000925#if defined(__alpha__)
926#define HOST_HZ 1024
927#else
928#define HOST_HZ 100
929#endif
930
blueswir1992f48a2007-10-14 16:27:31 +0000931static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000932{
933#if HOST_HZ == TARGET_HZ
934 return ticks;
935#else
936 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
937#endif
938}
939
bellard579a97f2007-11-11 14:26:47 +0000940static inline abi_long host_to_target_rusage(abi_ulong target_addr,
941 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000942{
pbrook53a59602006-03-25 19:31:22 +0000943 struct target_rusage *target_rusage;
944
bellard579a97f2007-11-11 14:26:47 +0000945 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
946 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200947 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
948 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
949 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
950 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
951 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
952 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
953 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
954 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
955 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
956 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
957 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
958 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
959 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
960 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
961 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
962 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
963 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
964 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000965 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000966
967 return 0;
bellardb4091862003-05-16 15:39:34 +0000968}
969
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200970static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900971{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200972 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300973 rlim_t result;
974
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200975 target_rlim_swap = tswapal(target_rlim);
976 if (target_rlim_swap == TARGET_RLIM_INFINITY)
977 return RLIM_INFINITY;
978
979 result = target_rlim_swap;
980 if (target_rlim_swap != (rlim_t)result)
981 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300982
983 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900984}
985
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200986static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900987{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200988 abi_ulong target_rlim_swap;
989 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300990
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200991 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300992 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900993 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300994 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200995 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300996
997 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900998}
999
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03001000static inline int target_to_host_resource(int code)
1001{
1002 switch (code) {
1003 case TARGET_RLIMIT_AS:
1004 return RLIMIT_AS;
1005 case TARGET_RLIMIT_CORE:
1006 return RLIMIT_CORE;
1007 case TARGET_RLIMIT_CPU:
1008 return RLIMIT_CPU;
1009 case TARGET_RLIMIT_DATA:
1010 return RLIMIT_DATA;
1011 case TARGET_RLIMIT_FSIZE:
1012 return RLIMIT_FSIZE;
1013 case TARGET_RLIMIT_LOCKS:
1014 return RLIMIT_LOCKS;
1015 case TARGET_RLIMIT_MEMLOCK:
1016 return RLIMIT_MEMLOCK;
1017 case TARGET_RLIMIT_MSGQUEUE:
1018 return RLIMIT_MSGQUEUE;
1019 case TARGET_RLIMIT_NICE:
1020 return RLIMIT_NICE;
1021 case TARGET_RLIMIT_NOFILE:
1022 return RLIMIT_NOFILE;
1023 case TARGET_RLIMIT_NPROC:
1024 return RLIMIT_NPROC;
1025 case TARGET_RLIMIT_RSS:
1026 return RLIMIT_RSS;
1027 case TARGET_RLIMIT_RTPRIO:
1028 return RLIMIT_RTPRIO;
1029 case TARGET_RLIMIT_SIGPENDING:
1030 return RLIMIT_SIGPENDING;
1031 case TARGET_RLIMIT_STACK:
1032 return RLIMIT_STACK;
1033 default:
1034 return code;
1035 }
1036}
1037
ths788f5ec2007-12-09 02:37:05 +00001038static inline abi_long copy_from_user_timeval(struct timeval *tv,
1039 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001040{
pbrook53a59602006-03-25 19:31:22 +00001041 struct target_timeval *target_tv;
1042
ths788f5ec2007-12-09 02:37:05 +00001043 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +00001044 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001045
1046 __get_user(tv->tv_sec, &target_tv->tv_sec);
1047 __get_user(tv->tv_usec, &target_tv->tv_usec);
1048
1049 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001050
1051 return 0;
bellard31e31b82003-02-18 22:55:36 +00001052}
1053
ths788f5ec2007-12-09 02:37:05 +00001054static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1055 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +00001056{
pbrook53a59602006-03-25 19:31:22 +00001057 struct target_timeval *target_tv;
1058
ths788f5ec2007-12-09 02:37:05 +00001059 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +00001060 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001061
1062 __put_user(tv->tv_sec, &target_tv->tv_sec);
1063 __put_user(tv->tv_usec, &target_tv->tv_usec);
1064
1065 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001066
1067 return 0;
bellard31e31b82003-02-18 22:55:36 +00001068}
1069
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001070#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1071#include <mqueue.h>
1072
aurel3224e10032009-04-15 16:11:43 +00001073static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1074 abi_ulong target_mq_attr_addr)
1075{
1076 struct target_mq_attr *target_mq_attr;
1077
1078 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1079 target_mq_attr_addr, 1))
1080 return -TARGET_EFAULT;
1081
1082 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1083 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1084 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1085 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1086
1087 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1088
1089 return 0;
1090}
1091
1092static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1093 const struct mq_attr *attr)
1094{
1095 struct target_mq_attr *target_mq_attr;
1096
1097 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1098 target_mq_attr_addr, 0))
1099 return -TARGET_EFAULT;
1100
1101 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1102 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1103 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1104 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1105
1106 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1107
1108 return 0;
1109}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001110#endif
bellard31e31b82003-02-18 22:55:36 +00001111
Mike Frysinger055e0902011-06-03 17:01:49 -04001112#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001113/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001114static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001115 abi_ulong rfd_addr, abi_ulong wfd_addr,
1116 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001117{
1118 fd_set rfds, wfds, efds;
1119 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1120 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001121 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001122
Mike Frysinger055e0902011-06-03 17:01:49 -04001123 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1124 if (ret) {
1125 return ret;
pbrook53a59602006-03-25 19:31:22 +00001126 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001127 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1128 if (ret) {
1129 return ret;
pbrook53a59602006-03-25 19:31:22 +00001130 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001131 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1132 if (ret) {
1133 return ret;
pbrook53a59602006-03-25 19:31:22 +00001134 }
ths3b46e622007-09-17 08:09:54 +00001135
ths26edcf42007-12-09 02:25:24 +00001136 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001137 if (copy_from_user_timeval(&tv, target_tv_addr))
1138 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001139 tv_ptr = &tv;
1140 } else {
1141 tv_ptr = NULL;
1142 }
ths26edcf42007-12-09 02:25:24 +00001143
bellard31e31b82003-02-18 22:55:36 +00001144 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +00001145
ths26edcf42007-12-09 02:25:24 +00001146 if (!is_error(ret)) {
1147 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1148 return -TARGET_EFAULT;
1149 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1150 return -TARGET_EFAULT;
1151 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1152 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001153
ths788f5ec2007-12-09 02:37:05 +00001154 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1155 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001156 }
bellard579a97f2007-11-11 14:26:47 +00001157
bellard31e31b82003-02-18 22:55:36 +00001158 return ret;
1159}
Mike Frysinger055e0902011-06-03 17:01:49 -04001160#endif
bellard31e31b82003-02-18 22:55:36 +00001161
Riku Voipio099d6b02009-05-05 12:10:04 +03001162static abi_long do_pipe2(int host_pipe[], int flags)
1163{
1164#ifdef CONFIG_PIPE2
1165 return pipe2(host_pipe, flags);
1166#else
1167 return -ENOSYS;
1168#endif
1169}
1170
Richard Hendersonfb41a662010-05-03 10:07:52 -07001171static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1172 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001173{
1174 int host_pipe[2];
1175 abi_long ret;
1176 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1177
1178 if (is_error(ret))
1179 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001180
1181 /* Several targets have special calling conventions for the original
1182 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1183 if (!is_pipe2) {
1184#if defined(TARGET_ALPHA)
1185 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1186 return host_pipe[0];
1187#elif defined(TARGET_MIPS)
1188 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1189 return host_pipe[0];
1190#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001191 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001192 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001193#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001194 }
1195
Riku Voipio099d6b02009-05-05 12:10:04 +03001196 if (put_user_s32(host_pipe[0], pipedes)
1197 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1198 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001199 return get_errno(ret);
1200}
1201
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001202static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1203 abi_ulong target_addr,
1204 socklen_t len)
1205{
1206 struct target_ip_mreqn *target_smreqn;
1207
1208 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1209 if (!target_smreqn)
1210 return -TARGET_EFAULT;
1211 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1212 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1213 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001214 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001215 unlock_user(target_smreqn, target_addr, 0);
1216
1217 return 0;
1218}
1219
bellard579a97f2007-11-11 14:26:47 +00001220static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1221 abi_ulong target_addr,
1222 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001223{
aurel32607175e2009-04-15 16:11:59 +00001224 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1225 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001226 struct target_sockaddr *target_saddr;
1227
bellard579a97f2007-11-11 14:26:47 +00001228 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1229 if (!target_saddr)
1230 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001231
1232 sa_family = tswap16(target_saddr->sa_family);
1233
1234 /* Oops. The caller might send a incomplete sun_path; sun_path
1235 * must be terminated by \0 (see the manual page), but
1236 * unfortunately it is quite common to specify sockaddr_un
1237 * length as "strlen(x->sun_path)" while it should be
1238 * "strlen(...) + 1". We'll fix that here if needed.
1239 * Linux kernel has a similar feature.
1240 */
1241
1242 if (sa_family == AF_UNIX) {
1243 if (len < unix_maxlen && len > 0) {
1244 char *cp = (char*)target_saddr;
1245
1246 if ( cp[len-1] && !cp[len] )
1247 len++;
1248 }
1249 if (len > unix_maxlen)
1250 len = unix_maxlen;
1251 }
1252
pbrook53a59602006-03-25 19:31:22 +00001253 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001254 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001255 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001256
1257 return 0;
bellard7854b052003-03-29 17:22:23 +00001258}
1259
bellard579a97f2007-11-11 14:26:47 +00001260static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1261 struct sockaddr *addr,
1262 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001263{
pbrook53a59602006-03-25 19:31:22 +00001264 struct target_sockaddr *target_saddr;
1265
bellard579a97f2007-11-11 14:26:47 +00001266 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1267 if (!target_saddr)
1268 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001269 memcpy(target_saddr, addr, len);
1270 target_saddr->sa_family = tswap16(addr->sa_family);
1271 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001272
1273 return 0;
bellard7854b052003-03-29 17:22:23 +00001274}
1275
bellard5a4a8982007-11-11 17:39:18 +00001276static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1277 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001278{
1279 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001280 abi_long msg_controllen;
1281 abi_ulong target_cmsg_addr;
1282 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001283 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001284
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001285 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001286 if (msg_controllen < sizeof (struct target_cmsghdr))
1287 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001288 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001289 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1290 if (!target_cmsg)
1291 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001292
1293 while (cmsg && target_cmsg) {
1294 void *data = CMSG_DATA(cmsg);
1295 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1296
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001297 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001298 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1299
1300 space += CMSG_SPACE(len);
1301 if (space > msgh->msg_controllen) {
1302 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001303 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001304 break;
1305 }
1306
1307 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1308 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1309 cmsg->cmsg_len = CMSG_LEN(len);
1310
bellard3532fa72006-06-24 15:06:03 +00001311 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001312 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1313 memcpy(data, target_data, len);
1314 } else {
1315 int *fd = (int *)data;
1316 int *target_fd = (int *)target_data;
1317 int i, numfds = len / sizeof(int);
1318
1319 for (i = 0; i < numfds; i++)
1320 fd[i] = tswap32(target_fd[i]);
1321 }
1322
1323 cmsg = CMSG_NXTHDR(msgh, cmsg);
1324 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1325 }
bellard5a4a8982007-11-11 17:39:18 +00001326 unlock_user(target_cmsg, target_cmsg_addr, 0);
1327 the_end:
bellard7854b052003-03-29 17:22:23 +00001328 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001329 return 0;
bellard7854b052003-03-29 17:22:23 +00001330}
1331
bellard5a4a8982007-11-11 17:39:18 +00001332static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1333 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001334{
1335 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001336 abi_long msg_controllen;
1337 abi_ulong target_cmsg_addr;
1338 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001339 socklen_t space = 0;
1340
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001341 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001342 if (msg_controllen < sizeof (struct target_cmsghdr))
1343 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001344 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001345 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1346 if (!target_cmsg)
1347 return -TARGET_EFAULT;
1348
bellard7854b052003-03-29 17:22:23 +00001349 while (cmsg && target_cmsg) {
1350 void *data = CMSG_DATA(cmsg);
1351 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1352
1353 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1354
1355 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001356 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001357 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001358 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001359 break;
1360 }
1361
1362 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1363 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001364 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
bellard7854b052003-03-29 17:22:23 +00001365
Jing Huangaebf5bc2012-07-24 14:01:42 +00001366 if ((cmsg->cmsg_level == TARGET_SOL_SOCKET) &&
1367 (cmsg->cmsg_type == SCM_RIGHTS)) {
bellard7854b052003-03-29 17:22:23 +00001368 int *fd = (int *)data;
1369 int *target_fd = (int *)target_data;
1370 int i, numfds = len / sizeof(int);
1371
1372 for (i = 0; i < numfds; i++)
1373 target_fd[i] = tswap32(fd[i]);
Jing Huangaebf5bc2012-07-24 14:01:42 +00001374 } else if ((cmsg->cmsg_level == TARGET_SOL_SOCKET) &&
1375 (cmsg->cmsg_type == SO_TIMESTAMP) &&
1376 (len == sizeof(struct timeval))) {
1377 /* copy struct timeval to target */
1378 struct timeval *tv = (struct timeval *)data;
1379 struct target_timeval *target_tv =
1380 (struct target_timeval *)target_data;
1381
1382 target_tv->tv_sec = tswapal(tv->tv_sec);
1383 target_tv->tv_usec = tswapal(tv->tv_usec);
1384 } else {
1385 gemu_log("Unsupported ancillary data: %d/%d\n",
1386 cmsg->cmsg_level, cmsg->cmsg_type);
1387 memcpy(target_data, data, len);
bellard7854b052003-03-29 17:22:23 +00001388 }
1389
1390 cmsg = CMSG_NXTHDR(msgh, cmsg);
1391 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1392 }
bellard5a4a8982007-11-11 17:39:18 +00001393 unlock_user(target_cmsg, target_cmsg_addr, space);
1394 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001395 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001396 return 0;
bellard7854b052003-03-29 17:22:23 +00001397}
1398
ths0da46a62007-10-20 20:23:07 +00001399/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001400static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001401 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001402{
blueswir1992f48a2007-10-14 16:27:31 +00001403 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001404 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001405 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001406 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001407
bellard8853f862004-02-22 14:57:26 +00001408 switch(level) {
1409 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001410 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001411 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001412 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001413
bellard2f619692007-11-16 10:46:05 +00001414 if (get_user_u32(val, optval_addr))
1415 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001416 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1417 break;
1418 case SOL_IP:
1419 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001420 case IP_TOS:
1421 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001422 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001423 case IP_ROUTER_ALERT:
1424 case IP_RECVOPTS:
1425 case IP_RETOPTS:
1426 case IP_PKTINFO:
1427 case IP_MTU_DISCOVER:
1428 case IP_RECVERR:
1429 case IP_RECVTOS:
1430#ifdef IP_FREEBIND
1431 case IP_FREEBIND:
1432#endif
1433 case IP_MULTICAST_TTL:
1434 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001435 val = 0;
1436 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001437 if (get_user_u32(val, optval_addr))
1438 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001439 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001440 if (get_user_u8(val, optval_addr))
1441 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001442 }
1443 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1444 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001445 case IP_ADD_MEMBERSHIP:
1446 case IP_DROP_MEMBERSHIP:
1447 if (optlen < sizeof (struct target_ip_mreq) ||
1448 optlen > sizeof (struct target_ip_mreqn))
1449 return -TARGET_EINVAL;
1450
1451 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1452 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1453 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1454 break;
1455
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001456 case IP_BLOCK_SOURCE:
1457 case IP_UNBLOCK_SOURCE:
1458 case IP_ADD_SOURCE_MEMBERSHIP:
1459 case IP_DROP_SOURCE_MEMBERSHIP:
1460 if (optlen != sizeof (struct target_ip_mreq_source))
1461 return -TARGET_EINVAL;
1462
1463 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1464 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1465 unlock_user (ip_mreq_source, optval_addr, 0);
1466 break;
1467
bellard8853f862004-02-22 14:57:26 +00001468 default:
1469 goto unimplemented;
1470 }
1471 break;
Jing Huang920394d2012-07-24 13:59:23 +00001472 case SOL_RAW:
1473 switch (optname) {
1474 case ICMP_FILTER:
1475 /* struct icmp_filter takes an u32 value */
1476 if (optlen < sizeof(uint32_t)) {
1477 return -TARGET_EINVAL;
1478 }
1479
1480 if (get_user_u32(val, optval_addr)) {
1481 return -TARGET_EFAULT;
1482 }
1483 ret = get_errno(setsockopt(sockfd, level, optname,
1484 &val, sizeof(val)));
1485 break;
1486
1487 default:
1488 goto unimplemented;
1489 }
1490 break;
bellard3532fa72006-06-24 15:06:03 +00001491 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001492 switch (optname) {
Laurent Vivier1b09aeb2013-01-01 08:24:11 +00001493 case TARGET_SO_RCVTIMEO:
1494 {
1495 struct timeval tv;
1496
1497 optname = SO_RCVTIMEO;
1498
1499set_timeout:
1500 if (optlen != sizeof(struct target_timeval)) {
1501 return -TARGET_EINVAL;
1502 }
1503
1504 if (copy_from_user_timeval(&tv, optval_addr)) {
1505 return -TARGET_EFAULT;
1506 }
1507
1508 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1509 &tv, sizeof(tv)));
1510 return ret;
1511 }
1512 case TARGET_SO_SNDTIMEO:
1513 optname = SO_SNDTIMEO;
1514 goto set_timeout;
bellard8853f862004-02-22 14:57:26 +00001515 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001516 case TARGET_SO_DEBUG:
1517 optname = SO_DEBUG;
1518 break;
1519 case TARGET_SO_REUSEADDR:
1520 optname = SO_REUSEADDR;
1521 break;
1522 case TARGET_SO_TYPE:
1523 optname = SO_TYPE;
1524 break;
1525 case TARGET_SO_ERROR:
1526 optname = SO_ERROR;
1527 break;
1528 case TARGET_SO_DONTROUTE:
1529 optname = SO_DONTROUTE;
1530 break;
1531 case TARGET_SO_BROADCAST:
1532 optname = SO_BROADCAST;
1533 break;
1534 case TARGET_SO_SNDBUF:
1535 optname = SO_SNDBUF;
1536 break;
1537 case TARGET_SO_RCVBUF:
1538 optname = SO_RCVBUF;
1539 break;
1540 case TARGET_SO_KEEPALIVE:
1541 optname = SO_KEEPALIVE;
1542 break;
1543 case TARGET_SO_OOBINLINE:
1544 optname = SO_OOBINLINE;
1545 break;
1546 case TARGET_SO_NO_CHECK:
1547 optname = SO_NO_CHECK;
1548 break;
1549 case TARGET_SO_PRIORITY:
1550 optname = SO_PRIORITY;
1551 break;
bellard5e83e8e2005-03-01 22:32:06 +00001552#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001553 case TARGET_SO_BSDCOMPAT:
1554 optname = SO_BSDCOMPAT;
1555 break;
bellard5e83e8e2005-03-01 22:32:06 +00001556#endif
bellard3532fa72006-06-24 15:06:03 +00001557 case TARGET_SO_PASSCRED:
1558 optname = SO_PASSCRED;
1559 break;
1560 case TARGET_SO_TIMESTAMP:
1561 optname = SO_TIMESTAMP;
1562 break;
1563 case TARGET_SO_RCVLOWAT:
1564 optname = SO_RCVLOWAT;
1565 break;
bellard8853f862004-02-22 14:57:26 +00001566 break;
1567 default:
1568 goto unimplemented;
1569 }
bellard3532fa72006-06-24 15:06:03 +00001570 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001571 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001572
bellard2f619692007-11-16 10:46:05 +00001573 if (get_user_u32(val, optval_addr))
1574 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001575 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001576 break;
bellard7854b052003-03-29 17:22:23 +00001577 default:
bellard8853f862004-02-22 14:57:26 +00001578 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02001579 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001580 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001581 }
bellard8853f862004-02-22 14:57:26 +00001582 return ret;
bellard7854b052003-03-29 17:22:23 +00001583}
1584
ths0da46a62007-10-20 20:23:07 +00001585/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001586static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001587 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001588{
blueswir1992f48a2007-10-14 16:27:31 +00001589 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001590 int len, val;
1591 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001592
1593 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001594 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001595 level = SOL_SOCKET;
1596 switch (optname) {
1597 /* These don't just return a single integer */
1598 case TARGET_SO_LINGER:
1599 case TARGET_SO_RCVTIMEO:
1600 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001601 case TARGET_SO_PEERNAME:
1602 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02001603 case TARGET_SO_PEERCRED: {
1604 struct ucred cr;
1605 socklen_t crlen;
1606 struct target_ucred *tcr;
1607
1608 if (get_user_u32(len, optlen)) {
1609 return -TARGET_EFAULT;
1610 }
1611 if (len < 0) {
1612 return -TARGET_EINVAL;
1613 }
1614
1615 crlen = sizeof(cr);
1616 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1617 &cr, &crlen));
1618 if (ret < 0) {
1619 return ret;
1620 }
1621 if (len > crlen) {
1622 len = crlen;
1623 }
1624 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1625 return -TARGET_EFAULT;
1626 }
1627 __put_user(cr.pid, &tcr->pid);
1628 __put_user(cr.uid, &tcr->uid);
1629 __put_user(cr.gid, &tcr->gid);
1630 unlock_user_struct(tcr, optval_addr, 1);
1631 if (put_user_u32(len, optlen)) {
1632 return -TARGET_EFAULT;
1633 }
1634 break;
1635 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001636 /* Options with 'int' argument. */
1637 case TARGET_SO_DEBUG:
1638 optname = SO_DEBUG;
1639 goto int_case;
1640 case TARGET_SO_REUSEADDR:
1641 optname = SO_REUSEADDR;
1642 goto int_case;
1643 case TARGET_SO_TYPE:
1644 optname = SO_TYPE;
1645 goto int_case;
1646 case TARGET_SO_ERROR:
1647 optname = SO_ERROR;
1648 goto int_case;
1649 case TARGET_SO_DONTROUTE:
1650 optname = SO_DONTROUTE;
1651 goto int_case;
1652 case TARGET_SO_BROADCAST:
1653 optname = SO_BROADCAST;
1654 goto int_case;
1655 case TARGET_SO_SNDBUF:
1656 optname = SO_SNDBUF;
1657 goto int_case;
1658 case TARGET_SO_RCVBUF:
1659 optname = SO_RCVBUF;
1660 goto int_case;
1661 case TARGET_SO_KEEPALIVE:
1662 optname = SO_KEEPALIVE;
1663 goto int_case;
1664 case TARGET_SO_OOBINLINE:
1665 optname = SO_OOBINLINE;
1666 goto int_case;
1667 case TARGET_SO_NO_CHECK:
1668 optname = SO_NO_CHECK;
1669 goto int_case;
1670 case TARGET_SO_PRIORITY:
1671 optname = SO_PRIORITY;
1672 goto int_case;
1673#ifdef SO_BSDCOMPAT
1674 case TARGET_SO_BSDCOMPAT:
1675 optname = SO_BSDCOMPAT;
1676 goto int_case;
1677#endif
1678 case TARGET_SO_PASSCRED:
1679 optname = SO_PASSCRED;
1680 goto int_case;
1681 case TARGET_SO_TIMESTAMP:
1682 optname = SO_TIMESTAMP;
1683 goto int_case;
1684 case TARGET_SO_RCVLOWAT:
1685 optname = SO_RCVLOWAT;
1686 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001687 default:
bellard2efbe912005-07-23 15:10:20 +00001688 goto int_case;
1689 }
1690 break;
1691 case SOL_TCP:
1692 /* TCP options all take an 'int' value. */
1693 int_case:
bellard2f619692007-11-16 10:46:05 +00001694 if (get_user_u32(len, optlen))
1695 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001696 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001697 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001698 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00001699 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1700 if (ret < 0)
1701 return ret;
bellard2efbe912005-07-23 15:10:20 +00001702 if (len > lv)
1703 len = lv;
bellard2f619692007-11-16 10:46:05 +00001704 if (len == 4) {
1705 if (put_user_u32(val, optval_addr))
1706 return -TARGET_EFAULT;
1707 } else {
1708 if (put_user_u8(val, optval_addr))
1709 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001710 }
bellard2f619692007-11-16 10:46:05 +00001711 if (put_user_u32(len, optlen))
1712 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001713 break;
1714 case SOL_IP:
1715 switch(optname) {
1716 case IP_TOS:
1717 case IP_TTL:
1718 case IP_HDRINCL:
1719 case IP_ROUTER_ALERT:
1720 case IP_RECVOPTS:
1721 case IP_RETOPTS:
1722 case IP_PKTINFO:
1723 case IP_MTU_DISCOVER:
1724 case IP_RECVERR:
1725 case IP_RECVTOS:
1726#ifdef IP_FREEBIND
1727 case IP_FREEBIND:
1728#endif
1729 case IP_MULTICAST_TTL:
1730 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001731 if (get_user_u32(len, optlen))
1732 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001733 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001734 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001735 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001736 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1737 if (ret < 0)
1738 return ret;
bellard2efbe912005-07-23 15:10:20 +00001739 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001740 len = 1;
bellard2f619692007-11-16 10:46:05 +00001741 if (put_user_u32(len, optlen)
1742 || put_user_u8(val, optval_addr))
1743 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001744 } else {
bellard2efbe912005-07-23 15:10:20 +00001745 if (len > sizeof(int))
1746 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001747 if (put_user_u32(len, optlen)
1748 || put_user_u32(val, optval_addr))
1749 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001750 }
bellard8853f862004-02-22 14:57:26 +00001751 break;
bellard2efbe912005-07-23 15:10:20 +00001752 default:
thsc02f4992007-12-18 02:39:59 +00001753 ret = -TARGET_ENOPROTOOPT;
1754 break;
bellard8853f862004-02-22 14:57:26 +00001755 }
1756 break;
1757 default:
1758 unimplemented:
1759 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1760 level, optname);
thsc02f4992007-12-18 02:39:59 +00001761 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001762 break;
1763 }
1764 return ret;
bellard7854b052003-03-29 17:22:23 +00001765}
1766
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001767static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1768 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001769{
1770 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001771 struct iovec *vec;
1772 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00001773 int i;
pbrook53a59602006-03-25 19:31:22 +00001774
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001775 if (count == 0) {
1776 errno = 0;
1777 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001778 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001779 if (count > IOV_MAX) {
1780 errno = EINVAL;
1781 return NULL;
1782 }
1783
1784 vec = calloc(count, sizeof(struct iovec));
1785 if (vec == NULL) {
1786 errno = ENOMEM;
1787 return NULL;
1788 }
1789
1790 target_vec = lock_user(VERIFY_READ, target_addr,
1791 count * sizeof(struct target_iovec), 1);
1792 if (target_vec == NULL) {
1793 errno = EFAULT;
1794 goto fail2;
1795 }
1796
1797 /* ??? If host page size > target page size, this will result in a
1798 value larger than what we can actually support. */
1799 max_len = 0x7fffffff & TARGET_PAGE_MASK;
1800 total_len = 0;
1801
1802 for (i = 0; i < count; i++) {
1803 abi_ulong base = tswapal(target_vec[i].iov_base);
1804 abi_long len = tswapal(target_vec[i].iov_len);
1805
1806 if (len < 0) {
1807 errno = EINVAL;
1808 goto fail;
1809 } else if (len == 0) {
1810 /* Zero length pointer is ignored. */
1811 vec[i].iov_base = 0;
1812 } else {
1813 vec[i].iov_base = lock_user(type, base, len, copy);
1814 if (!vec[i].iov_base) {
1815 errno = EFAULT;
1816 goto fail;
1817 }
1818 if (len > max_len - total_len) {
1819 len = max_len - total_len;
1820 }
1821 }
1822 vec[i].iov_len = len;
1823 total_len += len;
1824 }
1825
1826 unlock_user(target_vec, target_addr, 0);
1827 return vec;
1828
1829 fail:
1830 free(vec);
1831 fail2:
1832 unlock_user(target_vec, target_addr, 0);
1833 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001834}
1835
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001836static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1837 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001838{
1839 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00001840 int i;
1841
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001842 target_vec = lock_user(VERIFY_READ, target_addr,
1843 count * sizeof(struct target_iovec), 1);
1844 if (target_vec) {
1845 for (i = 0; i < count; i++) {
1846 abi_ulong base = tswapal(target_vec[i].iov_base);
1847 abi_long len = tswapal(target_vec[i].iov_base);
1848 if (len < 0) {
1849 break;
1850 }
balrogd732dcb2008-10-28 10:21:03 +00001851 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1852 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001853 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00001854 }
bellard579a97f2007-11-11 14:26:47 +00001855
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001856 free(vec);
pbrook53a59602006-03-25 19:31:22 +00001857}
1858
ths0da46a62007-10-20 20:23:07 +00001859/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001860static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001861{
1862#if defined(TARGET_MIPS)
1863 switch(type) {
1864 case TARGET_SOCK_DGRAM:
1865 type = SOCK_DGRAM;
1866 break;
1867 case TARGET_SOCK_STREAM:
1868 type = SOCK_STREAM;
1869 break;
1870 case TARGET_SOCK_RAW:
1871 type = SOCK_RAW;
1872 break;
1873 case TARGET_SOCK_RDM:
1874 type = SOCK_RDM;
1875 break;
1876 case TARGET_SOCK_SEQPACKET:
1877 type = SOCK_SEQPACKET;
1878 break;
1879 case TARGET_SOCK_PACKET:
1880 type = SOCK_PACKET;
1881 break;
1882 }
1883#endif
balrog12bc92a2007-10-30 21:06:14 +00001884 if (domain == PF_NETLINK)
1885 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001886 return get_errno(socket(domain, type, protocol));
1887}
1888
ths0da46a62007-10-20 20:23:07 +00001889/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001890static abi_long do_bind(int sockfd, abi_ulong target_addr,
1891 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001892{
aurel328f7aeaf2009-01-30 19:47:57 +00001893 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001894 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001895
Blue Swirl38724252010-09-18 05:53:14 +00001896 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001897 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001898 }
aurel328f7aeaf2009-01-30 19:47:57 +00001899
aurel32607175e2009-04-15 16:11:59 +00001900 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001901
Arnaud Patard917507b2009-06-19 10:44:45 +03001902 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1903 if (ret)
1904 return ret;
1905
bellard3532fa72006-06-24 15:06:03 +00001906 return get_errno(bind(sockfd, addr, addrlen));
1907}
1908
ths0da46a62007-10-20 20:23:07 +00001909/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001910static abi_long do_connect(int sockfd, abi_ulong target_addr,
1911 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001912{
aurel328f7aeaf2009-01-30 19:47:57 +00001913 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001914 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001915
Blue Swirl38724252010-09-18 05:53:14 +00001916 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001917 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001918 }
aurel328f7aeaf2009-01-30 19:47:57 +00001919
1920 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001921
Arnaud Patard917507b2009-06-19 10:44:45 +03001922 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1923 if (ret)
1924 return ret;
1925
bellard3532fa72006-06-24 15:06:03 +00001926 return get_errno(connect(sockfd, addr, addrlen));
1927}
1928
ths0da46a62007-10-20 20:23:07 +00001929/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001930static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1931 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001932{
balrog6de645c2008-10-28 10:26:29 +00001933 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001934 struct target_msghdr *msgp;
1935 struct msghdr msg;
1936 int count;
1937 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001938 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001939
bellard579a97f2007-11-11 14:26:47 +00001940 /* FIXME */
1941 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1942 msgp,
1943 target_msg,
1944 send ? 1 : 0))
1945 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001946 if (msgp->msg_name) {
1947 msg.msg_namelen = tswap32(msgp->msg_namelen);
1948 msg.msg_name = alloca(msg.msg_namelen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001949 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001950 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001951 if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001952 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03001953 }
bellard3532fa72006-06-24 15:06:03 +00001954 } else {
1955 msg.msg_name = NULL;
1956 msg.msg_namelen = 0;
1957 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001958 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00001959 msg.msg_control = alloca(msg.msg_controllen);
1960 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001961
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001962 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001963 target_vec = tswapal(msgp->msg_iov);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001964 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
1965 target_vec, count, send);
1966 if (vec == NULL) {
1967 ret = -host_to_target_errno(errno);
1968 goto out2;
1969 }
bellard3532fa72006-06-24 15:06:03 +00001970 msg.msg_iovlen = count;
1971 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001972
bellard3532fa72006-06-24 15:06:03 +00001973 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001974 ret = target_to_host_cmsg(&msg, msgp);
1975 if (ret == 0)
1976 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001977 } else {
1978 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001979 if (!is_error(ret)) {
1980 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001981 ret = host_to_target_cmsg(msgp, &msg);
Jing Huangca619062012-07-24 13:58:02 +00001982 if (!is_error(ret)) {
1983 msgp->msg_namelen = tswap32(msg.msg_namelen);
1984 if (msg.msg_name != NULL) {
1985 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
1986 msg.msg_name, msg.msg_namelen);
1987 if (ret) {
1988 goto out;
1989 }
1990 }
1991
balrog6de645c2008-10-28 10:26:29 +00001992 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00001993 }
balrog6de645c2008-10-28 10:26:29 +00001994 }
bellard3532fa72006-06-24 15:06:03 +00001995 }
Jing Huangca619062012-07-24 13:58:02 +00001996
1997out:
bellard3532fa72006-06-24 15:06:03 +00001998 unlock_iovec(vec, target_vec, count, !send);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001999out2:
bellard579a97f2007-11-11 14:26:47 +00002000 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00002001 return ret;
2002}
2003
ths0da46a62007-10-20 20:23:07 +00002004/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002005static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002006 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002007{
bellard2f619692007-11-16 10:46:05 +00002008 socklen_t addrlen;
2009 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002010 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002011
Arnaud Patard917507b2009-06-19 10:44:45 +03002012 if (target_addr == 0)
2013 return get_errno(accept(fd, NULL, NULL));
2014
2015 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00002016 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03002017 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00002018
Blue Swirl38724252010-09-18 05:53:14 +00002019 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002020 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002021 }
aurel328f7aeaf2009-01-30 19:47:57 +00002022
Arnaud Patard917507b2009-06-19 10:44:45 +03002023 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2024 return -TARGET_EINVAL;
2025
bellard2f619692007-11-16 10:46:05 +00002026 addr = alloca(addrlen);
2027
pbrook1be9e1d2006-11-19 15:26:04 +00002028 ret = get_errno(accept(fd, addr, &addrlen));
2029 if (!is_error(ret)) {
2030 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002031 if (put_user_u32(addrlen, target_addrlen_addr))
2032 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002033 }
2034 return ret;
2035}
2036
ths0da46a62007-10-20 20:23:07 +00002037/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002038static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002039 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002040{
bellard2f619692007-11-16 10:46:05 +00002041 socklen_t addrlen;
2042 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002043 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002044
bellard2f619692007-11-16 10:46:05 +00002045 if (get_user_u32(addrlen, target_addrlen_addr))
2046 return -TARGET_EFAULT;
2047
Blue Swirl38724252010-09-18 05:53:14 +00002048 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002049 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002050 }
aurel328f7aeaf2009-01-30 19:47:57 +00002051
Arnaud Patard917507b2009-06-19 10:44:45 +03002052 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2053 return -TARGET_EFAULT;
2054
bellard2f619692007-11-16 10:46:05 +00002055 addr = alloca(addrlen);
2056
pbrook1be9e1d2006-11-19 15:26:04 +00002057 ret = get_errno(getpeername(fd, addr, &addrlen));
2058 if (!is_error(ret)) {
2059 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002060 if (put_user_u32(addrlen, target_addrlen_addr))
2061 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002062 }
2063 return ret;
2064}
2065
ths0da46a62007-10-20 20:23:07 +00002066/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002067static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002068 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002069{
bellard2f619692007-11-16 10:46:05 +00002070 socklen_t addrlen;
2071 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002072 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002073
bellard2f619692007-11-16 10:46:05 +00002074 if (get_user_u32(addrlen, target_addrlen_addr))
2075 return -TARGET_EFAULT;
2076
Blue Swirl38724252010-09-18 05:53:14 +00002077 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002078 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002079 }
aurel328f7aeaf2009-01-30 19:47:57 +00002080
Arnaud Patard917507b2009-06-19 10:44:45 +03002081 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2082 return -TARGET_EFAULT;
2083
bellard2f619692007-11-16 10:46:05 +00002084 addr = alloca(addrlen);
2085
pbrook1be9e1d2006-11-19 15:26:04 +00002086 ret = get_errno(getsockname(fd, addr, &addrlen));
2087 if (!is_error(ret)) {
2088 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002089 if (put_user_u32(addrlen, target_addrlen_addr))
2090 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002091 }
2092 return ret;
2093}
2094
ths0da46a62007-10-20 20:23:07 +00002095/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002096static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00002097 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002098{
2099 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00002100 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002101
2102 ret = get_errno(socketpair(domain, type, protocol, tab));
2103 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00002104 if (put_user_s32(tab[0], target_tab_addr)
2105 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2106 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002107 }
2108 return ret;
2109}
2110
ths0da46a62007-10-20 20:23:07 +00002111/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002112static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2113 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002114{
2115 void *addr;
2116 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002117 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002118
Blue Swirl38724252010-09-18 05:53:14 +00002119 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002120 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002121 }
aurel328f7aeaf2009-01-30 19:47:57 +00002122
bellard579a97f2007-11-11 14:26:47 +00002123 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2124 if (!host_msg)
2125 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002126 if (target_addr) {
2127 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002128 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2129 if (ret) {
2130 unlock_user(host_msg, msg, 0);
2131 return ret;
2132 }
pbrook1be9e1d2006-11-19 15:26:04 +00002133 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2134 } else {
2135 ret = get_errno(send(fd, host_msg, len, flags));
2136 }
2137 unlock_user(host_msg, msg, 0);
2138 return ret;
2139}
2140
ths0da46a62007-10-20 20:23:07 +00002141/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002142static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2143 abi_ulong target_addr,
2144 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002145{
2146 socklen_t addrlen;
2147 void *addr;
2148 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002149 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002150
bellard579a97f2007-11-11 14:26:47 +00002151 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2152 if (!host_msg)
2153 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002154 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00002155 if (get_user_u32(addrlen, target_addrlen)) {
2156 ret = -TARGET_EFAULT;
2157 goto fail;
2158 }
Blue Swirl38724252010-09-18 05:53:14 +00002159 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002160 ret = -TARGET_EINVAL;
2161 goto fail;
2162 }
pbrook1be9e1d2006-11-19 15:26:04 +00002163 addr = alloca(addrlen);
2164 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2165 } else {
2166 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00002167 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002168 }
2169 if (!is_error(ret)) {
2170 if (target_addr) {
2171 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002172 if (put_user_u32(addrlen, target_addrlen)) {
2173 ret = -TARGET_EFAULT;
2174 goto fail;
2175 }
pbrook1be9e1d2006-11-19 15:26:04 +00002176 }
2177 unlock_user(host_msg, msg, len);
2178 } else {
bellard2f619692007-11-16 10:46:05 +00002179fail:
pbrook1be9e1d2006-11-19 15:26:04 +00002180 unlock_user(host_msg, msg, 0);
2181 }
2182 return ret;
2183}
2184
j_mayer32407102007-09-26 23:01:49 +00002185#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00002186/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002187static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00002188{
blueswir1992f48a2007-10-14 16:27:31 +00002189 abi_long ret;
2190 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00002191
2192 switch(num) {
2193 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00002194 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002195 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002196
Ulrich Hecht98818182009-07-03 17:09:28 +02002197 if (get_user_ual(domain, vptr)
2198 || get_user_ual(type, vptr + n)
2199 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002200 return -TARGET_EFAULT;
2201
bellard3532fa72006-06-24 15:06:03 +00002202 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00002203 }
bellard31e31b82003-02-18 22:55:36 +00002204 break;
2205 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00002206 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002207 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002208 abi_ulong target_addr;
2209 socklen_t addrlen;
2210
Ulrich Hecht98818182009-07-03 17:09:28 +02002211 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002212 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002213 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002214 return -TARGET_EFAULT;
2215
bellard3532fa72006-06-24 15:06:03 +00002216 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002217 }
bellard31e31b82003-02-18 22:55:36 +00002218 break;
2219 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00002220 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002221 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002222 abi_ulong target_addr;
2223 socklen_t addrlen;
2224
Ulrich Hecht98818182009-07-03 17:09:28 +02002225 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002226 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002227 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002228 return -TARGET_EFAULT;
2229
bellard3532fa72006-06-24 15:06:03 +00002230 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002231 }
bellard31e31b82003-02-18 22:55:36 +00002232 break;
2233 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00002234 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002235 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00002236
Ulrich Hecht98818182009-07-03 17:09:28 +02002237 if (get_user_ual(sockfd, vptr)
2238 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002239 return -TARGET_EFAULT;
2240
bellard7854b052003-03-29 17:22:23 +00002241 ret = get_errno(listen(sockfd, backlog));
2242 }
bellard31e31b82003-02-18 22:55:36 +00002243 break;
2244 case SOCKOP_accept:
2245 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002246 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002247 abi_ulong target_addr, target_addrlen;
2248
Ulrich Hecht98818182009-07-03 17:09:28 +02002249 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002250 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002251 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002252 return -TARGET_EFAULT;
2253
pbrook1be9e1d2006-11-19 15:26:04 +00002254 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002255 }
2256 break;
2257 case SOCKOP_getsockname:
2258 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002259 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002260 abi_ulong target_addr, target_addrlen;
2261
Ulrich Hecht98818182009-07-03 17:09:28 +02002262 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002263 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002264 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002265 return -TARGET_EFAULT;
2266
pbrook1be9e1d2006-11-19 15:26:04 +00002267 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002268 }
2269 break;
2270 case SOCKOP_getpeername:
2271 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002272 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002273 abi_ulong target_addr, target_addrlen;
2274
Ulrich Hecht98818182009-07-03 17:09:28 +02002275 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002276 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002277 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002278 return -TARGET_EFAULT;
2279
pbrook1be9e1d2006-11-19 15:26:04 +00002280 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002281 }
2282 break;
2283 case SOCKOP_socketpair:
2284 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002285 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002286 abi_ulong tab;
2287
Ulrich Hecht98818182009-07-03 17:09:28 +02002288 if (get_user_ual(domain, vptr)
2289 || get_user_ual(type, vptr + n)
2290 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002291 || get_user_ual(tab, vptr + 3 * n))
2292 return -TARGET_EFAULT;
2293
pbrook1be9e1d2006-11-19 15:26:04 +00002294 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00002295 }
2296 break;
2297 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00002298 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002299 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002300 abi_ulong msg;
2301 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002302 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002303
Ulrich Hecht98818182009-07-03 17:09:28 +02002304 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002305 || get_user_ual(msg, vptr + n)
2306 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002307 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002308 return -TARGET_EFAULT;
2309
pbrook1be9e1d2006-11-19 15:26:04 +00002310 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002311 }
bellard31e31b82003-02-18 22:55:36 +00002312 break;
2313 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00002314 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002315 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002316 abi_ulong msg;
2317 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002318 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002319
Ulrich Hecht98818182009-07-03 17:09:28 +02002320 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002321 || get_user_ual(msg, vptr + n)
2322 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002323 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002324 return -TARGET_EFAULT;
2325
pbrook1be9e1d2006-11-19 15:26:04 +00002326 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002327 }
bellard31e31b82003-02-18 22:55:36 +00002328 break;
2329 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00002330 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002331 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002332 abi_ulong msg;
2333 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002334 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002335 abi_ulong addr;
2336 socklen_t addrlen;
2337
Ulrich Hecht98818182009-07-03 17:09:28 +02002338 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002339 || get_user_ual(msg, vptr + n)
2340 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002341 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002342 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002343 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002344 return -TARGET_EFAULT;
2345
pbrook1be9e1d2006-11-19 15:26:04 +00002346 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002347 }
bellard31e31b82003-02-18 22:55:36 +00002348 break;
2349 case SOCKOP_recvfrom:
2350 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002351 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002352 abi_ulong msg;
2353 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002354 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002355 abi_ulong addr;
2356 socklen_t addrlen;
2357
Ulrich Hecht98818182009-07-03 17:09:28 +02002358 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002359 || get_user_ual(msg, vptr + n)
2360 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002361 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002362 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002363 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002364 return -TARGET_EFAULT;
2365
pbrook1be9e1d2006-11-19 15:26:04 +00002366 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00002367 }
2368 break;
2369 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00002370 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002371 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00002372
Ulrich Hecht98818182009-07-03 17:09:28 +02002373 if (get_user_ual(sockfd, vptr)
2374 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002375 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002376
2377 ret = get_errno(shutdown(sockfd, how));
2378 }
bellard31e31b82003-02-18 22:55:36 +00002379 break;
2380 case SOCKOP_sendmsg:
2381 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002382 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002383 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002384 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002385 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002386
Ulrich Hecht98818182009-07-03 17:09:28 +02002387 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002388 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002389 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002390 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002391
ths5fafdf22007-09-16 21:08:06 +00002392 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002393 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002394 }
2395 break;
bellard31e31b82003-02-18 22:55:36 +00002396 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002397 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002398 abi_ulong sockfd;
2399 abi_ulong level;
2400 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002401 abi_ulong optval;
2402 socklen_t optlen;
2403
Ulrich Hecht98818182009-07-03 17:09:28 +02002404 if (get_user_ual(sockfd, vptr)
2405 || get_user_ual(level, vptr + n)
2406 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002407 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002408 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002409 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002410
2411 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2412 }
2413 break;
bellard31e31b82003-02-18 22:55:36 +00002414 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002415 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002416 abi_ulong sockfd;
2417 abi_ulong level;
2418 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002419 abi_ulong optval;
2420 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002421
Ulrich Hecht98818182009-07-03 17:09:28 +02002422 if (get_user_ual(sockfd, vptr)
2423 || get_user_ual(level, vptr + n)
2424 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002425 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002426 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002427 return -TARGET_EFAULT;
2428
2429 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002430 }
2431 break;
bellard31e31b82003-02-18 22:55:36 +00002432 default:
2433 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002434 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002435 break;
2436 }
2437 return ret;
2438}
j_mayer32407102007-09-26 23:01:49 +00002439#endif
bellard31e31b82003-02-18 22:55:36 +00002440
bellard8853f862004-02-22 14:57:26 +00002441#define N_SHM_REGIONS 32
2442
2443static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002444 abi_ulong start;
2445 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002446} shm_regions[N_SHM_REGIONS];
2447
ths3eb6b042007-06-03 14:26:27 +00002448struct target_ipc_perm
2449{
blueswir1992f48a2007-10-14 16:27:31 +00002450 abi_long __key;
2451 abi_ulong uid;
2452 abi_ulong gid;
2453 abi_ulong cuid;
2454 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002455 unsigned short int mode;
2456 unsigned short int __pad1;
2457 unsigned short int __seq;
2458 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002459 abi_ulong __unused1;
2460 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002461};
2462
2463struct target_semid_ds
2464{
2465 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002466 abi_ulong sem_otime;
2467 abi_ulong __unused1;
2468 abi_ulong sem_ctime;
2469 abi_ulong __unused2;
2470 abi_ulong sem_nsems;
2471 abi_ulong __unused3;
2472 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002473};
2474
bellard579a97f2007-11-11 14:26:47 +00002475static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2476 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002477{
2478 struct target_ipc_perm *target_ip;
2479 struct target_semid_ds *target_sd;
2480
bellard579a97f2007-11-11 14:26:47 +00002481 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2482 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002483 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002484 host_ip->__key = tswapal(target_ip->__key);
2485 host_ip->uid = tswapal(target_ip->uid);
2486 host_ip->gid = tswapal(target_ip->gid);
2487 host_ip->cuid = tswapal(target_ip->cuid);
2488 host_ip->cgid = tswapal(target_ip->cgid);
2489 host_ip->mode = tswap16(target_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002490 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002491 return 0;
ths3eb6b042007-06-03 14:26:27 +00002492}
2493
bellard579a97f2007-11-11 14:26:47 +00002494static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2495 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002496{
2497 struct target_ipc_perm *target_ip;
2498 struct target_semid_ds *target_sd;
2499
bellard579a97f2007-11-11 14:26:47 +00002500 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2501 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002502 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002503 target_ip->__key = tswapal(host_ip->__key);
2504 target_ip->uid = tswapal(host_ip->uid);
2505 target_ip->gid = tswapal(host_ip->gid);
2506 target_ip->cuid = tswapal(host_ip->cuid);
2507 target_ip->cgid = tswapal(host_ip->cgid);
2508 target_ip->mode = tswap16(host_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002509 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002510 return 0;
ths3eb6b042007-06-03 14:26:27 +00002511}
2512
bellard579a97f2007-11-11 14:26:47 +00002513static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2514 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002515{
2516 struct target_semid_ds *target_sd;
2517
bellard579a97f2007-11-11 14:26:47 +00002518 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2519 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002520 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2521 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002522 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2523 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2524 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002525 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002526 return 0;
ths3eb6b042007-06-03 14:26:27 +00002527}
2528
bellard579a97f2007-11-11 14:26:47 +00002529static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2530 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002531{
2532 struct target_semid_ds *target_sd;
2533
bellard579a97f2007-11-11 14:26:47 +00002534 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2535 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002536 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08002537 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002538 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2539 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2540 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002541 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002542 return 0;
ths3eb6b042007-06-03 14:26:27 +00002543}
2544
aurel32e5289082009-04-18 16:16:12 +00002545struct target_seminfo {
2546 int semmap;
2547 int semmni;
2548 int semmns;
2549 int semmnu;
2550 int semmsl;
2551 int semopm;
2552 int semume;
2553 int semusz;
2554 int semvmx;
2555 int semaem;
2556};
2557
2558static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2559 struct seminfo *host_seminfo)
2560{
2561 struct target_seminfo *target_seminfo;
2562 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2563 return -TARGET_EFAULT;
2564 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2565 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2566 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2567 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2568 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2569 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2570 __put_user(host_seminfo->semume, &target_seminfo->semume);
2571 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2572 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2573 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2574 unlock_user_struct(target_seminfo, target_addr, 1);
2575 return 0;
2576}
2577
thsfa294812007-02-02 22:05:00 +00002578union semun {
2579 int val;
ths3eb6b042007-06-03 14:26:27 +00002580 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002581 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002582 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002583};
2584
ths3eb6b042007-06-03 14:26:27 +00002585union target_semun {
2586 int val;
aurel32e5289082009-04-18 16:16:12 +00002587 abi_ulong buf;
2588 abi_ulong array;
2589 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002590};
2591
aurel32e5289082009-04-18 16:16:12 +00002592static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2593 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002594{
aurel32e5289082009-04-18 16:16:12 +00002595 int nsems;
2596 unsigned short *array;
2597 union semun semun;
2598 struct semid_ds semid_ds;
2599 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002600
aurel32e5289082009-04-18 16:16:12 +00002601 semun.buf = &semid_ds;
2602
2603 ret = semctl(semid, 0, IPC_STAT, semun);
2604 if (ret == -1)
2605 return get_errno(ret);
2606
2607 nsems = semid_ds.sem_nsems;
2608
2609 *host_array = malloc(nsems*sizeof(unsigned short));
2610 array = lock_user(VERIFY_READ, target_addr,
2611 nsems*sizeof(unsigned short), 1);
2612 if (!array)
2613 return -TARGET_EFAULT;
2614
2615 for(i=0; i<nsems; i++) {
2616 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002617 }
aurel32e5289082009-04-18 16:16:12 +00002618 unlock_user(array, target_addr, 0);
2619
bellard579a97f2007-11-11 14:26:47 +00002620 return 0;
ths3eb6b042007-06-03 14:26:27 +00002621}
2622
aurel32e5289082009-04-18 16:16:12 +00002623static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2624 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002625{
aurel32e5289082009-04-18 16:16:12 +00002626 int nsems;
2627 unsigned short *array;
2628 union semun semun;
2629 struct semid_ds semid_ds;
2630 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002631
aurel32e5289082009-04-18 16:16:12 +00002632 semun.buf = &semid_ds;
2633
2634 ret = semctl(semid, 0, IPC_STAT, semun);
2635 if (ret == -1)
2636 return get_errno(ret);
2637
2638 nsems = semid_ds.sem_nsems;
2639
2640 array = lock_user(VERIFY_WRITE, target_addr,
2641 nsems*sizeof(unsigned short), 0);
2642 if (!array)
2643 return -TARGET_EFAULT;
2644
2645 for(i=0; i<nsems; i++) {
2646 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002647 }
aurel32e5289082009-04-18 16:16:12 +00002648 free(*host_array);
2649 unlock_user(array, target_addr, 1);
2650
bellard579a97f2007-11-11 14:26:47 +00002651 return 0;
ths3eb6b042007-06-03 14:26:27 +00002652}
2653
aurel32e5289082009-04-18 16:16:12 +00002654static inline abi_long do_semctl(int semid, int semnum, int cmd,
2655 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002656{
2657 union semun arg;
2658 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302659 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002660 struct seminfo seminfo;
2661 abi_long ret = -TARGET_EINVAL;
2662 abi_long err;
2663 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002664
2665 switch( cmd ) {
2666 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002667 case SETVAL:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002668 arg.val = tswap32(target_su.val);
aurel32e5289082009-04-18 16:16:12 +00002669 ret = get_errno(semctl(semid, semnum, cmd, arg));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002670 target_su.val = tswap32(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002671 break;
2672 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002673 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002674 err = target_to_host_semarray(semid, &array, target_su.array);
2675 if (err)
2676 return err;
2677 arg.array = array;
2678 ret = get_errno(semctl(semid, semnum, cmd, arg));
2679 err = host_to_target_semarray(semid, target_su.array, &array);
2680 if (err)
2681 return err;
ths3eb6b042007-06-03 14:26:27 +00002682 break;
2683 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002684 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002685 case SEM_STAT:
2686 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2687 if (err)
2688 return err;
2689 arg.buf = &dsarg;
2690 ret = get_errno(semctl(semid, semnum, cmd, arg));
2691 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2692 if (err)
2693 return err;
ths3eb6b042007-06-03 14:26:27 +00002694 break;
aurel32e5289082009-04-18 16:16:12 +00002695 case IPC_INFO:
2696 case SEM_INFO:
2697 arg.__buf = &seminfo;
2698 ret = get_errno(semctl(semid, semnum, cmd, arg));
2699 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2700 if (err)
2701 return err;
2702 break;
2703 case IPC_RMID:
2704 case GETPID:
2705 case GETNCNT:
2706 case GETZCNT:
2707 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2708 break;
ths3eb6b042007-06-03 14:26:27 +00002709 }
2710
2711 return ret;
2712}
2713
aurel32e5289082009-04-18 16:16:12 +00002714struct target_sembuf {
2715 unsigned short sem_num;
2716 short sem_op;
2717 short sem_flg;
2718};
2719
2720static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2721 abi_ulong target_addr,
2722 unsigned nsops)
2723{
2724 struct target_sembuf *target_sembuf;
2725 int i;
2726
2727 target_sembuf = lock_user(VERIFY_READ, target_addr,
2728 nsops*sizeof(struct target_sembuf), 1);
2729 if (!target_sembuf)
2730 return -TARGET_EFAULT;
2731
2732 for(i=0; i<nsops; i++) {
2733 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2734 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2735 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2736 }
2737
2738 unlock_user(target_sembuf, target_addr, 0);
2739
2740 return 0;
2741}
2742
2743static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2744{
2745 struct sembuf sops[nsops];
2746
2747 if (target_to_host_sembuf(sops, ptr, nsops))
2748 return -TARGET_EFAULT;
2749
2750 return semop(semid, sops, nsops);
2751}
2752
ths1bc012f2007-06-03 14:27:49 +00002753struct target_msqid_ds
2754{
aurel321c54ff92008-10-13 21:08:44 +00002755 struct target_ipc_perm msg_perm;
2756 abi_ulong msg_stime;
2757#if TARGET_ABI_BITS == 32
2758 abi_ulong __unused1;
2759#endif
2760 abi_ulong msg_rtime;
2761#if TARGET_ABI_BITS == 32
2762 abi_ulong __unused2;
2763#endif
2764 abi_ulong msg_ctime;
2765#if TARGET_ABI_BITS == 32
2766 abi_ulong __unused3;
2767#endif
2768 abi_ulong __msg_cbytes;
2769 abi_ulong msg_qnum;
2770 abi_ulong msg_qbytes;
2771 abi_ulong msg_lspid;
2772 abi_ulong msg_lrpid;
2773 abi_ulong __unused4;
2774 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002775};
2776
bellard579a97f2007-11-11 14:26:47 +00002777static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2778 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002779{
2780 struct target_msqid_ds *target_md;
2781
bellard579a97f2007-11-11 14:26:47 +00002782 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2783 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002784 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2785 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002786 host_md->msg_stime = tswapal(target_md->msg_stime);
2787 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2788 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2789 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2790 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2791 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2792 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2793 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002794 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002795 return 0;
ths1bc012f2007-06-03 14:27:49 +00002796}
2797
bellard579a97f2007-11-11 14:26:47 +00002798static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2799 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002800{
2801 struct target_msqid_ds *target_md;
2802
bellard579a97f2007-11-11 14:26:47 +00002803 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2804 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002805 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2806 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002807 target_md->msg_stime = tswapal(host_md->msg_stime);
2808 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2809 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2810 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2811 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2812 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2813 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2814 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002815 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002816 return 0;
ths1bc012f2007-06-03 14:27:49 +00002817}
2818
aurel321c54ff92008-10-13 21:08:44 +00002819struct target_msginfo {
2820 int msgpool;
2821 int msgmap;
2822 int msgmax;
2823 int msgmnb;
2824 int msgmni;
2825 int msgssz;
2826 int msgtql;
2827 unsigned short int msgseg;
2828};
2829
2830static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2831 struct msginfo *host_msginfo)
2832{
2833 struct target_msginfo *target_msginfo;
2834 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2835 return -TARGET_EFAULT;
2836 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2837 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2838 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2839 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2840 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2841 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2842 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2843 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2844 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002845 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002846}
2847
2848static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002849{
2850 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002851 struct msginfo msginfo;
2852 abi_long ret = -TARGET_EINVAL;
2853
2854 cmd &= 0xff;
2855
2856 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002857 case IPC_STAT:
2858 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002859 case MSG_STAT:
2860 if (target_to_host_msqid_ds(&dsarg,ptr))
2861 return -TARGET_EFAULT;
2862 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2863 if (host_to_target_msqid_ds(ptr,&dsarg))
2864 return -TARGET_EFAULT;
2865 break;
2866 case IPC_RMID:
2867 ret = get_errno(msgctl(msgid, cmd, NULL));
2868 break;
2869 case IPC_INFO:
2870 case MSG_INFO:
2871 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2872 if (host_to_target_msginfo(ptr, &msginfo))
2873 return -TARGET_EFAULT;
2874 break;
ths1bc012f2007-06-03 14:27:49 +00002875 }
aurel321c54ff92008-10-13 21:08:44 +00002876
ths1bc012f2007-06-03 14:27:49 +00002877 return ret;
2878}
2879
2880struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002881 abi_long mtype;
2882 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002883};
2884
blueswir1992f48a2007-10-14 16:27:31 +00002885static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2886 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002887{
2888 struct target_msgbuf *target_mb;
2889 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002890 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002891
bellard579a97f2007-11-11 14:26:47 +00002892 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2893 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002894 host_mb = malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002895 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00002896 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002897 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2898 free(host_mb);
2899 unlock_user_struct(target_mb, msgp, 0);
2900
2901 return ret;
2902}
2903
blueswir1992f48a2007-10-14 16:27:31 +00002904static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002905 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002906 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002907{
2908 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002909 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002910 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002911 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002912
bellard579a97f2007-11-11 14:26:47 +00002913 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2914 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002915
Jim Meyering0d07fe42012-08-22 13:55:53 +02002916 host_mb = g_malloc(msgsz+sizeof(long));
Laurent Vivier79dd77d2012-12-20 11:00:11 +00002917 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
aurel321c54ff92008-10-13 21:08:44 +00002918
bellard579a97f2007-11-11 14:26:47 +00002919 if (ret > 0) {
2920 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2921 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2922 if (!target_mtext) {
2923 ret = -TARGET_EFAULT;
2924 goto end;
2925 }
aurel321c54ff92008-10-13 21:08:44 +00002926 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002927 unlock_user(target_mtext, target_mtext_addr, ret);
2928 }
aurel321c54ff92008-10-13 21:08:44 +00002929
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002930 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00002931
bellard579a97f2007-11-11 14:26:47 +00002932end:
2933 if (target_mb)
2934 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02002935 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002936 return ret;
2937}
2938
Riku Voipio88a8c982009-04-03 10:42:00 +03002939struct target_shmid_ds
2940{
2941 struct target_ipc_perm shm_perm;
2942 abi_ulong shm_segsz;
2943 abi_ulong shm_atime;
2944#if TARGET_ABI_BITS == 32
2945 abi_ulong __unused1;
2946#endif
2947 abi_ulong shm_dtime;
2948#if TARGET_ABI_BITS == 32
2949 abi_ulong __unused2;
2950#endif
2951 abi_ulong shm_ctime;
2952#if TARGET_ABI_BITS == 32
2953 abi_ulong __unused3;
2954#endif
2955 int shm_cpid;
2956 int shm_lpid;
2957 abi_ulong shm_nattch;
2958 unsigned long int __unused4;
2959 unsigned long int __unused5;
2960};
2961
2962static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2963 abi_ulong target_addr)
2964{
2965 struct target_shmid_ds *target_sd;
2966
2967 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2968 return -TARGET_EFAULT;
2969 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2970 return -TARGET_EFAULT;
2971 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2972 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2973 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2974 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2975 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2976 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2977 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2978 unlock_user_struct(target_sd, target_addr, 0);
2979 return 0;
2980}
2981
2982static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2983 struct shmid_ds *host_sd)
2984{
2985 struct target_shmid_ds *target_sd;
2986
2987 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2988 return -TARGET_EFAULT;
2989 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2990 return -TARGET_EFAULT;
2991 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2992 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2993 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2994 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2995 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2996 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2997 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2998 unlock_user_struct(target_sd, target_addr, 1);
2999 return 0;
3000}
3001
3002struct target_shminfo {
3003 abi_ulong shmmax;
3004 abi_ulong shmmin;
3005 abi_ulong shmmni;
3006 abi_ulong shmseg;
3007 abi_ulong shmall;
3008};
3009
3010static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
3011 struct shminfo *host_shminfo)
3012{
3013 struct target_shminfo *target_shminfo;
3014 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3015 return -TARGET_EFAULT;
3016 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3017 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3018 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3019 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3020 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3021 unlock_user_struct(target_shminfo, target_addr, 1);
3022 return 0;
3023}
3024
3025struct target_shm_info {
3026 int used_ids;
3027 abi_ulong shm_tot;
3028 abi_ulong shm_rss;
3029 abi_ulong shm_swp;
3030 abi_ulong swap_attempts;
3031 abi_ulong swap_successes;
3032};
3033
3034static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3035 struct shm_info *host_shm_info)
3036{
3037 struct target_shm_info *target_shm_info;
3038 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3039 return -TARGET_EFAULT;
3040 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3041 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3042 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3043 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3044 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3045 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3046 unlock_user_struct(target_shm_info, target_addr, 1);
3047 return 0;
3048}
3049
3050static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3051{
3052 struct shmid_ds dsarg;
3053 struct shminfo shminfo;
3054 struct shm_info shm_info;
3055 abi_long ret = -TARGET_EINVAL;
3056
3057 cmd &= 0xff;
3058
3059 switch(cmd) {
3060 case IPC_STAT:
3061 case IPC_SET:
3062 case SHM_STAT:
3063 if (target_to_host_shmid_ds(&dsarg, buf))
3064 return -TARGET_EFAULT;
3065 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3066 if (host_to_target_shmid_ds(buf, &dsarg))
3067 return -TARGET_EFAULT;
3068 break;
3069 case IPC_INFO:
3070 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3071 if (host_to_target_shminfo(buf, &shminfo))
3072 return -TARGET_EFAULT;
3073 break;
3074 case SHM_INFO:
3075 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3076 if (host_to_target_shm_info(buf, &shm_info))
3077 return -TARGET_EFAULT;
3078 break;
3079 case IPC_RMID:
3080 case SHM_LOCK:
3081 case SHM_UNLOCK:
3082 ret = get_errno(shmctl(shmid, cmd, NULL));
3083 break;
3084 }
3085
3086 return ret;
3087}
3088
3089static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3090{
3091 abi_long raddr;
3092 void *host_raddr;
3093 struct shmid_ds shm_info;
3094 int i,ret;
3095
3096 /* find out the length of the shared memory segment */
3097 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3098 if (is_error(ret)) {
3099 /* can't get length, bail out */
3100 return ret;
3101 }
3102
3103 mmap_lock();
3104
3105 if (shmaddr)
3106 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3107 else {
3108 abi_ulong mmap_start;
3109
3110 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3111
3112 if (mmap_start == -1) {
3113 errno = ENOMEM;
3114 host_raddr = (void *)-1;
3115 } else
3116 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3117 }
3118
3119 if (host_raddr == (void *)-1) {
3120 mmap_unlock();
3121 return get_errno((long)host_raddr);
3122 }
3123 raddr=h2g((unsigned long)host_raddr);
3124
3125 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3126 PAGE_VALID | PAGE_READ |
3127 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3128
3129 for (i = 0; i < N_SHM_REGIONS; i++) {
3130 if (shm_regions[i].start == 0) {
3131 shm_regions[i].start = raddr;
3132 shm_regions[i].size = shm_info.shm_segsz;
3133 break;
3134 }
3135 }
3136
3137 mmap_unlock();
3138 return raddr;
3139
3140}
3141
3142static inline abi_long do_shmdt(abi_ulong shmaddr)
3143{
3144 int i;
3145
3146 for (i = 0; i < N_SHM_REGIONS; ++i) {
3147 if (shm_regions[i].start == shmaddr) {
3148 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09003149 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03003150 break;
3151 }
3152 }
3153
3154 return get_errno(shmdt(g2h(shmaddr)));
3155}
3156
aurel321c54ff92008-10-13 21:08:44 +00003157#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00003158/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00003159/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003160static abi_long do_ipc(unsigned int call, int first,
3161 int second, int third,
3162 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00003163{
3164 int version;
blueswir1992f48a2007-10-14 16:27:31 +00003165 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00003166
3167 version = call >> 16;
3168 call &= 0xffff;
3169
3170 switch (call) {
thsfa294812007-02-02 22:05:00 +00003171 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00003172 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00003173 break;
3174
3175 case IPCOP_semget:
3176 ret = get_errno(semget(first, second, third));
3177 break;
3178
3179 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00003180 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00003181 break;
thsd96372e2007-02-02 22:05:44 +00003182
aurel321c54ff92008-10-13 21:08:44 +00003183 case IPCOP_msgget:
3184 ret = get_errno(msgget(first, second));
3185 break;
thsd96372e2007-02-02 22:05:44 +00003186
aurel321c54ff92008-10-13 21:08:44 +00003187 case IPCOP_msgsnd:
3188 ret = do_msgsnd(first, ptr, second, third);
3189 break;
thsd96372e2007-02-02 22:05:44 +00003190
aurel321c54ff92008-10-13 21:08:44 +00003191 case IPCOP_msgctl:
3192 ret = do_msgctl(first, second, ptr);
3193 break;
thsd96372e2007-02-02 22:05:44 +00003194
aurel321c54ff92008-10-13 21:08:44 +00003195 case IPCOP_msgrcv:
3196 switch (version) {
3197 case 0:
3198 {
3199 struct target_ipc_kludge {
3200 abi_long msgp;
3201 abi_long msgtyp;
3202 } *tmp;
thsd96372e2007-02-02 22:05:44 +00003203
aurel321c54ff92008-10-13 21:08:44 +00003204 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3205 ret = -TARGET_EFAULT;
3206 break;
ths1bc012f2007-06-03 14:27:49 +00003207 }
aurel321c54ff92008-10-13 21:08:44 +00003208
Laurent Vivier79dd77d2012-12-20 11:00:11 +00003209 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
aurel321c54ff92008-10-13 21:08:44 +00003210
3211 unlock_user_struct(tmp, ptr, 0);
3212 break;
3213 }
3214 default:
3215 ret = do_msgrcv(first, ptr, second, fifth, third);
3216 }
3217 break;
thsd96372e2007-02-02 22:05:44 +00003218
bellard8853f862004-02-22 14:57:26 +00003219 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03003220 switch (version) {
3221 default:
bellard5a4a8982007-11-11 17:39:18 +00003222 {
3223 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03003224 raddr = do_shmat(first, ptr, second);
3225 if (is_error(raddr))
3226 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00003227 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00003228 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03003229 break;
3230 }
3231 case 1:
3232 ret = -TARGET_EINVAL;
3233 break;
bellard5a4a8982007-11-11 17:39:18 +00003234 }
bellard8853f862004-02-22 14:57:26 +00003235 break;
3236 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03003237 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00003238 break;
3239
3240 case IPCOP_shmget:
3241 /* IPC_* flag values are the same on all linux platforms */
3242 ret = get_errno(shmget(first, second, third));
3243 break;
3244
3245 /* IPC_* and SHM_* command values are the same on all linux platforms */
3246 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03003247 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00003248 break;
3249 default:
j_mayer32407102007-09-26 23:01:49 +00003250 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00003251 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00003252 break;
3253 }
3254 return ret;
3255}
j_mayer32407102007-09-26 23:01:49 +00003256#endif
bellard8853f862004-02-22 14:57:26 +00003257
bellard31e31b82003-02-18 22:55:36 +00003258/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00003259
Blue Swirl001faf32009-05-13 17:53:17 +00003260#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00003261#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3262enum {
3263#include "syscall_types.h"
3264};
3265#undef STRUCT
3266#undef STRUCT_SPECIAL
3267
Blue Swirl001faf32009-05-13 17:53:17 +00003268#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00003269#define STRUCT_SPECIAL(name)
3270#include "syscall_types.h"
3271#undef STRUCT
3272#undef STRUCT_SPECIAL
3273
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003274typedef struct IOCTLEntry IOCTLEntry;
3275
3276typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3277 int fd, abi_long cmd, abi_long arg);
3278
3279struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00003280 unsigned int target_cmd;
3281 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00003282 const char *name;
3283 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003284 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003285 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003286};
bellard31e31b82003-02-18 22:55:36 +00003287
3288#define IOC_R 0x0001
3289#define IOC_W 0x0002
3290#define IOC_RW (IOC_R | IOC_W)
3291
3292#define MAX_STRUCT_SIZE 4096
3293
Peter Maydelldace20d2011-01-10 13:11:24 +00003294#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003295/* So fiemap access checks don't overflow on 32 bit systems.
3296 * This is very slightly smaller than the limit imposed by
3297 * the underlying kernel.
3298 */
3299#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3300 / sizeof(struct fiemap_extent))
3301
3302static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3303 int fd, abi_long cmd, abi_long arg)
3304{
3305 /* The parameter for this ioctl is a struct fiemap followed
3306 * by an array of struct fiemap_extent whose size is set
3307 * in fiemap->fm_extent_count. The array is filled in by the
3308 * ioctl.
3309 */
3310 int target_size_in, target_size_out;
3311 struct fiemap *fm;
3312 const argtype *arg_type = ie->arg_type;
3313 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3314 void *argptr, *p;
3315 abi_long ret;
3316 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3317 uint32_t outbufsz;
3318 int free_fm = 0;
3319
3320 assert(arg_type[0] == TYPE_PTR);
3321 assert(ie->access == IOC_RW);
3322 arg_type++;
3323 target_size_in = thunk_type_size(arg_type, 0);
3324 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3325 if (!argptr) {
3326 return -TARGET_EFAULT;
3327 }
3328 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3329 unlock_user(argptr, arg, 0);
3330 fm = (struct fiemap *)buf_temp;
3331 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3332 return -TARGET_EINVAL;
3333 }
3334
3335 outbufsz = sizeof (*fm) +
3336 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3337
3338 if (outbufsz > MAX_STRUCT_SIZE) {
3339 /* We can't fit all the extents into the fixed size buffer.
3340 * Allocate one that is large enough and use it instead.
3341 */
3342 fm = malloc(outbufsz);
3343 if (!fm) {
3344 return -TARGET_ENOMEM;
3345 }
3346 memcpy(fm, buf_temp, sizeof(struct fiemap));
3347 free_fm = 1;
3348 }
3349 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3350 if (!is_error(ret)) {
3351 target_size_out = target_size_in;
3352 /* An extent_count of 0 means we were only counting the extents
3353 * so there are no structs to copy
3354 */
3355 if (fm->fm_extent_count != 0) {
3356 target_size_out += fm->fm_mapped_extents * extent_size;
3357 }
3358 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3359 if (!argptr) {
3360 ret = -TARGET_EFAULT;
3361 } else {
3362 /* Convert the struct fiemap */
3363 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3364 if (fm->fm_extent_count != 0) {
3365 p = argptr + target_size_in;
3366 /* ...and then all the struct fiemap_extents */
3367 for (i = 0; i < fm->fm_mapped_extents; i++) {
3368 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3369 THUNK_TARGET);
3370 p += extent_size;
3371 }
3372 }
3373 unlock_user(argptr, arg, target_size_out);
3374 }
3375 }
3376 if (free_fm) {
3377 free(fm);
3378 }
3379 return ret;
3380}
Peter Maydelldace20d2011-01-10 13:11:24 +00003381#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003382
Laurent Vivier059c2f22011-03-30 00:12:12 +02003383static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3384 int fd, abi_long cmd, abi_long arg)
3385{
3386 const argtype *arg_type = ie->arg_type;
3387 int target_size;
3388 void *argptr;
3389 int ret;
3390 struct ifconf *host_ifconf;
3391 uint32_t outbufsz;
3392 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3393 int target_ifreq_size;
3394 int nb_ifreq;
3395 int free_buf = 0;
3396 int i;
3397 int target_ifc_len;
3398 abi_long target_ifc_buf;
3399 int host_ifc_len;
3400 char *host_ifc_buf;
3401
3402 assert(arg_type[0] == TYPE_PTR);
3403 assert(ie->access == IOC_RW);
3404
3405 arg_type++;
3406 target_size = thunk_type_size(arg_type, 0);
3407
3408 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3409 if (!argptr)
3410 return -TARGET_EFAULT;
3411 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3412 unlock_user(argptr, arg, 0);
3413
3414 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3415 target_ifc_len = host_ifconf->ifc_len;
3416 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3417
3418 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3419 nb_ifreq = target_ifc_len / target_ifreq_size;
3420 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3421
3422 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3423 if (outbufsz > MAX_STRUCT_SIZE) {
3424 /* We can't fit all the extents into the fixed size buffer.
3425 * Allocate one that is large enough and use it instead.
3426 */
3427 host_ifconf = malloc(outbufsz);
3428 if (!host_ifconf) {
3429 return -TARGET_ENOMEM;
3430 }
3431 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3432 free_buf = 1;
3433 }
3434 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3435
3436 host_ifconf->ifc_len = host_ifc_len;
3437 host_ifconf->ifc_buf = host_ifc_buf;
3438
3439 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3440 if (!is_error(ret)) {
3441 /* convert host ifc_len to target ifc_len */
3442
3443 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3444 target_ifc_len = nb_ifreq * target_ifreq_size;
3445 host_ifconf->ifc_len = target_ifc_len;
3446
3447 /* restore target ifc_buf */
3448
3449 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3450
3451 /* copy struct ifconf to target user */
3452
3453 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3454 if (!argptr)
3455 return -TARGET_EFAULT;
3456 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3457 unlock_user(argptr, arg, target_size);
3458
3459 /* copy ifreq[] to target user */
3460
3461 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3462 for (i = 0; i < nb_ifreq ; i++) {
3463 thunk_convert(argptr + i * target_ifreq_size,
3464 host_ifc_buf + i * sizeof(struct ifreq),
3465 ifreq_arg_type, THUNK_TARGET);
3466 }
3467 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3468 }
3469
3470 if (free_buf) {
3471 free(host_ifconf);
3472 }
3473
3474 return ret;
3475}
3476
Alexander Graf56e904e2012-01-31 18:42:06 +01003477static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3478 abi_long cmd, abi_long arg)
3479{
3480 void *argptr;
3481 struct dm_ioctl *host_dm;
3482 abi_long guest_data;
3483 uint32_t guest_data_size;
3484 int target_size;
3485 const argtype *arg_type = ie->arg_type;
3486 abi_long ret;
3487 void *big_buf = NULL;
3488 char *host_data;
3489
3490 arg_type++;
3491 target_size = thunk_type_size(arg_type, 0);
3492 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3493 if (!argptr) {
3494 ret = -TARGET_EFAULT;
3495 goto out;
3496 }
3497 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3498 unlock_user(argptr, arg, 0);
3499
3500 /* buf_temp is too small, so fetch things into a bigger buffer */
3501 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3502 memcpy(big_buf, buf_temp, target_size);
3503 buf_temp = big_buf;
3504 host_dm = big_buf;
3505
3506 guest_data = arg + host_dm->data_start;
3507 if ((guest_data - arg) < 0) {
3508 ret = -EINVAL;
3509 goto out;
3510 }
3511 guest_data_size = host_dm->data_size - host_dm->data_start;
3512 host_data = (char*)host_dm + host_dm->data_start;
3513
3514 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3515 switch (ie->host_cmd) {
3516 case DM_REMOVE_ALL:
3517 case DM_LIST_DEVICES:
3518 case DM_DEV_CREATE:
3519 case DM_DEV_REMOVE:
3520 case DM_DEV_SUSPEND:
3521 case DM_DEV_STATUS:
3522 case DM_DEV_WAIT:
3523 case DM_TABLE_STATUS:
3524 case DM_TABLE_CLEAR:
3525 case DM_TABLE_DEPS:
3526 case DM_LIST_VERSIONS:
3527 /* no input data */
3528 break;
3529 case DM_DEV_RENAME:
3530 case DM_DEV_SET_GEOMETRY:
3531 /* data contains only strings */
3532 memcpy(host_data, argptr, guest_data_size);
3533 break;
3534 case DM_TARGET_MSG:
3535 memcpy(host_data, argptr, guest_data_size);
3536 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3537 break;
3538 case DM_TABLE_LOAD:
3539 {
3540 void *gspec = argptr;
3541 void *cur_data = host_data;
3542 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3543 int spec_size = thunk_type_size(arg_type, 0);
3544 int i;
3545
3546 for (i = 0; i < host_dm->target_count; i++) {
3547 struct dm_target_spec *spec = cur_data;
3548 uint32_t next;
3549 int slen;
3550
3551 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3552 slen = strlen((char*)gspec + spec_size) + 1;
3553 next = spec->next;
3554 spec->next = sizeof(*spec) + slen;
3555 strcpy((char*)&spec[1], gspec + spec_size);
3556 gspec += next;
3557 cur_data += spec->next;
3558 }
3559 break;
3560 }
3561 default:
3562 ret = -TARGET_EINVAL;
3563 goto out;
3564 }
3565 unlock_user(argptr, guest_data, 0);
3566
3567 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3568 if (!is_error(ret)) {
3569 guest_data = arg + host_dm->data_start;
3570 guest_data_size = host_dm->data_size - host_dm->data_start;
3571 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3572 switch (ie->host_cmd) {
3573 case DM_REMOVE_ALL:
3574 case DM_DEV_CREATE:
3575 case DM_DEV_REMOVE:
3576 case DM_DEV_RENAME:
3577 case DM_DEV_SUSPEND:
3578 case DM_DEV_STATUS:
3579 case DM_TABLE_LOAD:
3580 case DM_TABLE_CLEAR:
3581 case DM_TARGET_MSG:
3582 case DM_DEV_SET_GEOMETRY:
3583 /* no return data */
3584 break;
3585 case DM_LIST_DEVICES:
3586 {
3587 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3588 uint32_t remaining_data = guest_data_size;
3589 void *cur_data = argptr;
3590 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3591 int nl_size = 12; /* can't use thunk_size due to alignment */
3592
3593 while (1) {
3594 uint32_t next = nl->next;
3595 if (next) {
3596 nl->next = nl_size + (strlen(nl->name) + 1);
3597 }
3598 if (remaining_data < nl->next) {
3599 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3600 break;
3601 }
3602 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3603 strcpy(cur_data + nl_size, nl->name);
3604 cur_data += nl->next;
3605 remaining_data -= nl->next;
3606 if (!next) {
3607 break;
3608 }
3609 nl = (void*)nl + next;
3610 }
3611 break;
3612 }
3613 case DM_DEV_WAIT:
3614 case DM_TABLE_STATUS:
3615 {
3616 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3617 void *cur_data = argptr;
3618 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3619 int spec_size = thunk_type_size(arg_type, 0);
3620 int i;
3621
3622 for (i = 0; i < host_dm->target_count; i++) {
3623 uint32_t next = spec->next;
3624 int slen = strlen((char*)&spec[1]) + 1;
3625 spec->next = (cur_data - argptr) + spec_size + slen;
3626 if (guest_data_size < spec->next) {
3627 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3628 break;
3629 }
3630 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3631 strcpy(cur_data + spec_size, (char*)&spec[1]);
3632 cur_data = argptr + spec->next;
3633 spec = (void*)host_dm + host_dm->data_start + next;
3634 }
3635 break;
3636 }
3637 case DM_TABLE_DEPS:
3638 {
3639 void *hdata = (void*)host_dm + host_dm->data_start;
3640 int count = *(uint32_t*)hdata;
3641 uint64_t *hdev = hdata + 8;
3642 uint64_t *gdev = argptr + 8;
3643 int i;
3644
3645 *(uint32_t*)argptr = tswap32(count);
3646 for (i = 0; i < count; i++) {
3647 *gdev = tswap64(*hdev);
3648 gdev++;
3649 hdev++;
3650 }
3651 break;
3652 }
3653 case DM_LIST_VERSIONS:
3654 {
3655 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3656 uint32_t remaining_data = guest_data_size;
3657 void *cur_data = argptr;
3658 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3659 int vers_size = thunk_type_size(arg_type, 0);
3660
3661 while (1) {
3662 uint32_t next = vers->next;
3663 if (next) {
3664 vers->next = vers_size + (strlen(vers->name) + 1);
3665 }
3666 if (remaining_data < vers->next) {
3667 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3668 break;
3669 }
3670 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3671 strcpy(cur_data + vers_size, vers->name);
3672 cur_data += vers->next;
3673 remaining_data -= vers->next;
3674 if (!next) {
3675 break;
3676 }
3677 vers = (void*)vers + next;
3678 }
3679 break;
3680 }
3681 default:
3682 ret = -TARGET_EINVAL;
3683 goto out;
3684 }
3685 unlock_user(argptr, guest_data, guest_data_size);
3686
3687 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3688 if (!argptr) {
3689 ret = -TARGET_EFAULT;
3690 goto out;
3691 }
3692 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3693 unlock_user(argptr, arg, target_size);
3694 }
3695out:
Stefan Weilad11ad72012-09-04 22:14:19 +02003696 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01003697 return ret;
3698}
3699
blueswir19f106a72008-10-05 10:52:52 +00003700static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00003701#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003702 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3703#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3704 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00003705#include "ioctls.h"
3706 { 0, 0, },
3707};
3708
pbrook53a59602006-03-25 19:31:22 +00003709/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00003710/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003711static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00003712{
3713 const IOCTLEntry *ie;
3714 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00003715 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003716 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00003717 int target_size;
3718 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00003719
3720 ie = ioctl_entries;
3721 for(;;) {
3722 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003723 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003724 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003725 }
3726 if (ie->target_cmd == cmd)
3727 break;
3728 ie++;
3729 }
3730 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003731#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003732 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003733#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003734 if (ie->do_ioctl) {
3735 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3736 }
3737
bellard31e31b82003-02-18 22:55:36 +00003738 switch(arg_type[0]) {
3739 case TYPE_NULL:
3740 /* no argument */
3741 ret = get_errno(ioctl(fd, ie->host_cmd));
3742 break;
3743 case TYPE_PTRVOID:
3744 case TYPE_INT:
3745 /* int argment */
3746 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3747 break;
3748 case TYPE_PTR:
3749 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00003750 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00003751 switch(ie->access) {
3752 case IOC_R:
3753 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3754 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003755 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3756 if (!argptr)
3757 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003758 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3759 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003760 }
3761 break;
3762 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00003763 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3764 if (!argptr)
3765 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003766 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3767 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003768 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3769 break;
3770 default:
3771 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00003772 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3773 if (!argptr)
3774 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003775 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3776 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003777 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3778 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003779 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3780 if (!argptr)
3781 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003782 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3783 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003784 }
3785 break;
3786 }
3787 break;
3788 default:
j_mayer32407102007-09-26 23:01:49 +00003789 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3790 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003791 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003792 break;
3793 }
3794 return ret;
3795}
3796
blueswir1b39bc502008-10-05 10:51:10 +00003797static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003798 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3799 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3800 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3801 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3802 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3803 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3804 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3805 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3806 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3807 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3808 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3809 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3810 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3811 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3812 { 0, 0, 0, 0 }
3813};
3814
blueswir1b39bc502008-10-05 10:51:10 +00003815static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003816 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3817 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3818 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3819 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3820 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3821 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3822 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3823 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3824 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3825 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3826 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3827 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3828 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3829 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3830 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3831 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3832 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3833 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3834 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3835 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3836 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3837 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3838 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3839 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3840 { 0, 0, 0, 0 }
3841};
3842
blueswir1b39bc502008-10-05 10:51:10 +00003843static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003844 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3845 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3846 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3847 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3848 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3849 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3850 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3851 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3852 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3853 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3854 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3855 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3856 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3857 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3858 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3859 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3860 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3861 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3862 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3863 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3864 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3865 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3866 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3867 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3868 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3869 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3870 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3871 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3872 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3873 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3874 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3875 { 0, 0, 0, 0 }
3876};
3877
blueswir1b39bc502008-10-05 10:51:10 +00003878static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003879 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3880 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3881 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3882 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3883 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3884 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3885 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3886 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3887 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3888 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3889 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3890 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3891 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3892 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3893 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3894 { 0, 0, 0, 0 }
3895};
3896
3897static void target_to_host_termios (void *dst, const void *src)
3898{
3899 struct host_termios *host = dst;
3900 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003901
ths5fafdf22007-09-16 21:08:06 +00003902 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003903 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003904 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003905 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003906 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003907 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003908 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003909 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3910 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003911
Arnaud Patard44607122009-04-21 17:39:08 +03003912 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003913 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3914 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003915 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003916 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003917 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003918 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003919 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003920 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003921 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003922 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3923 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003924 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3925 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3926 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3927 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3928 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003929 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003930}
ths3b46e622007-09-17 08:09:54 +00003931
bellard31e31b82003-02-18 22:55:36 +00003932static void host_to_target_termios (void *dst, const void *src)
3933{
3934 struct target_termios *target = dst;
3935 const struct host_termios *host = src;
3936
ths5fafdf22007-09-16 21:08:06 +00003937 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003938 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003939 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003940 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003941 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003942 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003943 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003944 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3945 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003946
Arnaud Patard44607122009-04-21 17:39:08 +03003947 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003948 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3949 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3950 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3951 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3952 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3953 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3954 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3955 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3956 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3957 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3958 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3959 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3960 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3961 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3962 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3963 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3964 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3965}
3966
blueswir18e853dc2008-10-05 10:49:32 +00003967static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003968 .convert = { host_to_target_termios, target_to_host_termios },
3969 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3970 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3971};
3972
bellard5286db72003-06-05 00:57:30 +00003973static bitmask_transtbl mmap_flags_tbl[] = {
3974 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3975 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3976 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3977 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3978 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3979 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3980 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3981 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3982 { 0, 0, 0, 0 }
3983};
3984
bellard2ab83ea2003-06-15 19:56:46 +00003985#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003986
3987/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003988static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003989
bellard03acab62007-11-11 14:57:14 +00003990static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003991{
3992 int size;
pbrook53a59602006-03-25 19:31:22 +00003993 void *p;
bellard6dbad632003-03-16 18:05:05 +00003994
3995 if (!ldt_table)
3996 return 0;
3997 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3998 if (size > bytecount)
3999 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00004000 p = lock_user(VERIFY_WRITE, ptr, size, 0);
4001 if (!p)
bellard03acab62007-11-11 14:57:14 +00004002 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00004003 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00004004 memcpy(p, ldt_table, size);
4005 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00004006 return size;
4007}
4008
4009/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00004010static abi_long write_ldt(CPUX86State *env,
4011 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00004012{
4013 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00004014 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00004015 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00004016 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00004017 uint32_t *lp, entry_1, entry_2;
4018
4019 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00004020 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00004021 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00004022 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004023 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004024 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00004025 ldt_info.limit = tswap32(target_ldt_info->limit);
4026 ldt_info.flags = tswap32(target_ldt_info->flags);
4027 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00004028
bellard6dbad632003-03-16 18:05:05 +00004029 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00004030 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004031 seg_32bit = ldt_info.flags & 1;
4032 contents = (ldt_info.flags >> 1) & 3;
4033 read_exec_only = (ldt_info.flags >> 3) & 1;
4034 limit_in_pages = (ldt_info.flags >> 4) & 1;
4035 seg_not_present = (ldt_info.flags >> 5) & 1;
4036 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00004037#ifdef TARGET_ABI32
4038 lm = 0;
4039#else
4040 lm = (ldt_info.flags >> 7) & 1;
4041#endif
bellard6dbad632003-03-16 18:05:05 +00004042 if (contents == 3) {
4043 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00004044 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004045 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00004046 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004047 }
4048 /* allocate the LDT */
4049 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00004050 env->ldt.base = target_mmap(0,
4051 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
4052 PROT_READ|PROT_WRITE,
4053 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4054 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00004055 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00004056 memset(g2h(env->ldt.base), 0,
4057 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00004058 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00004059 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00004060 }
4061
4062 /* NOTE: same code as Linux kernel */
4063 /* Allow LDTs to be cleared by the user. */
4064 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4065 if (oldmode ||
4066 (contents == 0 &&
4067 read_exec_only == 1 &&
4068 seg_32bit == 0 &&
4069 limit_in_pages == 0 &&
4070 seg_not_present == 1 &&
4071 useable == 0 )) {
4072 entry_1 = 0;
4073 entry_2 = 0;
4074 goto install;
4075 }
4076 }
ths3b46e622007-09-17 08:09:54 +00004077
bellard6dbad632003-03-16 18:05:05 +00004078 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4079 (ldt_info.limit & 0x0ffff);
4080 entry_2 = (ldt_info.base_addr & 0xff000000) |
4081 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4082 (ldt_info.limit & 0xf0000) |
4083 ((read_exec_only ^ 1) << 9) |
4084 (contents << 10) |
4085 ((seg_not_present ^ 1) << 15) |
4086 (seg_32bit << 22) |
4087 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00004088 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00004089 0x7000;
4090 if (!oldmode)
4091 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00004092
bellard6dbad632003-03-16 18:05:05 +00004093 /* Install the new entry ... */
4094install:
4095 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4096 lp[0] = tswap32(entry_1);
4097 lp[1] = tswap32(entry_2);
4098 return 0;
4099}
4100
4101/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00004102static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4103 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00004104{
bellard03acab62007-11-11 14:57:14 +00004105 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00004106
bellard6dbad632003-03-16 18:05:05 +00004107 switch (func) {
4108 case 0:
4109 ret = read_ldt(ptr, bytecount);
4110 break;
4111 case 1:
4112 ret = write_ldt(env, ptr, bytecount, 1);
4113 break;
4114 case 0x11:
4115 ret = write_ldt(env, ptr, bytecount, 0);
4116 break;
bellard03acab62007-11-11 14:57:14 +00004117 default:
4118 ret = -TARGET_ENOSYS;
4119 break;
bellard6dbad632003-03-16 18:05:05 +00004120 }
4121 return ret;
4122}
bellard1b6b0292003-03-22 17:31:38 +00004123
blueswir14583f582008-08-24 10:35:55 +00004124#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00004125static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004126{
4127 uint64_t *gdt_table = g2h(env->gdt.base);
4128 struct target_modify_ldt_ldt_s ldt_info;
4129 struct target_modify_ldt_ldt_s *target_ldt_info;
4130 int seg_32bit, contents, read_exec_only, limit_in_pages;
4131 int seg_not_present, useable, lm;
4132 uint32_t *lp, entry_1, entry_2;
4133 int i;
4134
4135 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4136 if (!target_ldt_info)
4137 return -TARGET_EFAULT;
4138 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004139 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00004140 ldt_info.limit = tswap32(target_ldt_info->limit);
4141 ldt_info.flags = tswap32(target_ldt_info->flags);
4142 if (ldt_info.entry_number == -1) {
4143 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4144 if (gdt_table[i] == 0) {
4145 ldt_info.entry_number = i;
4146 target_ldt_info->entry_number = tswap32(i);
4147 break;
4148 }
4149 }
4150 }
4151 unlock_user_struct(target_ldt_info, ptr, 1);
4152
4153 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4154 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4155 return -TARGET_EINVAL;
4156 seg_32bit = ldt_info.flags & 1;
4157 contents = (ldt_info.flags >> 1) & 3;
4158 read_exec_only = (ldt_info.flags >> 3) & 1;
4159 limit_in_pages = (ldt_info.flags >> 4) & 1;
4160 seg_not_present = (ldt_info.flags >> 5) & 1;
4161 useable = (ldt_info.flags >> 6) & 1;
4162#ifdef TARGET_ABI32
4163 lm = 0;
4164#else
4165 lm = (ldt_info.flags >> 7) & 1;
4166#endif
4167
4168 if (contents == 3) {
4169 if (seg_not_present == 0)
4170 return -TARGET_EINVAL;
4171 }
4172
4173 /* NOTE: same code as Linux kernel */
4174 /* Allow LDTs to be cleared by the user. */
4175 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4176 if ((contents == 0 &&
4177 read_exec_only == 1 &&
4178 seg_32bit == 0 &&
4179 limit_in_pages == 0 &&
4180 seg_not_present == 1 &&
4181 useable == 0 )) {
4182 entry_1 = 0;
4183 entry_2 = 0;
4184 goto install;
4185 }
4186 }
4187
4188 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4189 (ldt_info.limit & 0x0ffff);
4190 entry_2 = (ldt_info.base_addr & 0xff000000) |
4191 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4192 (ldt_info.limit & 0xf0000) |
4193 ((read_exec_only ^ 1) << 9) |
4194 (contents << 10) |
4195 ((seg_not_present ^ 1) << 15) |
4196 (seg_32bit << 22) |
4197 (limit_in_pages << 23) |
4198 (useable << 20) |
4199 (lm << 21) |
4200 0x7000;
4201
4202 /* Install the new entry ... */
4203install:
4204 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4205 lp[0] = tswap32(entry_1);
4206 lp[1] = tswap32(entry_2);
4207 return 0;
4208}
4209
blueswir18fcd3692008-08-17 20:26:25 +00004210static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004211{
4212 struct target_modify_ldt_ldt_s *target_ldt_info;
4213 uint64_t *gdt_table = g2h(env->gdt.base);
4214 uint32_t base_addr, limit, flags;
4215 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4216 int seg_not_present, useable, lm;
4217 uint32_t *lp, entry_1, entry_2;
4218
4219 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4220 if (!target_ldt_info)
4221 return -TARGET_EFAULT;
4222 idx = tswap32(target_ldt_info->entry_number);
4223 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4224 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4225 unlock_user_struct(target_ldt_info, ptr, 1);
4226 return -TARGET_EINVAL;
4227 }
4228 lp = (uint32_t *)(gdt_table + idx);
4229 entry_1 = tswap32(lp[0]);
4230 entry_2 = tswap32(lp[1]);
4231
4232 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4233 contents = (entry_2 >> 10) & 3;
4234 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4235 seg_32bit = (entry_2 >> 22) & 1;
4236 limit_in_pages = (entry_2 >> 23) & 1;
4237 useable = (entry_2 >> 20) & 1;
4238#ifdef TARGET_ABI32
4239 lm = 0;
4240#else
4241 lm = (entry_2 >> 21) & 1;
4242#endif
4243 flags = (seg_32bit << 0) | (contents << 1) |
4244 (read_exec_only << 3) | (limit_in_pages << 4) |
4245 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4246 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4247 base_addr = (entry_1 >> 16) |
4248 (entry_2 & 0xff000000) |
4249 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004250 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00004251 target_ldt_info->limit = tswap32(limit);
4252 target_ldt_info->flags = tswap32(flags);
4253 unlock_user_struct(target_ldt_info, ptr, 1);
4254 return 0;
4255}
blueswir14583f582008-08-24 10:35:55 +00004256#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00004257
bellardd2fd1af2007-11-14 18:08:56 +00004258#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00004259static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00004260{
Juan Quintela1add8692011-06-16 17:37:09 +01004261 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00004262 abi_ulong val;
4263 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01004264
bellardd2fd1af2007-11-14 18:08:56 +00004265 switch(code) {
4266 case TARGET_ARCH_SET_GS:
4267 case TARGET_ARCH_SET_FS:
4268 if (code == TARGET_ARCH_SET_GS)
4269 idx = R_GS;
4270 else
4271 idx = R_FS;
4272 cpu_x86_load_seg(env, idx, 0);
4273 env->segs[idx].base = addr;
4274 break;
4275 case TARGET_ARCH_GET_GS:
4276 case TARGET_ARCH_GET_FS:
4277 if (code == TARGET_ARCH_GET_GS)
4278 idx = R_GS;
4279 else
4280 idx = R_FS;
4281 val = env->segs[idx].base;
4282 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01004283 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00004284 break;
4285 default:
4286 ret = -TARGET_EINVAL;
4287 break;
4288 }
Juan Quintela1add8692011-06-16 17:37:09 +01004289 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00004290}
4291#endif
4292
bellard2ab83ea2003-06-15 19:56:46 +00004293#endif /* defined(TARGET_I386) */
4294
Riku Voipio05098a92011-03-04 15:27:29 +02004295#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00004296
Riku Voipio05098a92011-03-04 15:27:29 +02004297#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004298
4299static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4300typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01004301 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00004302 pthread_mutex_t mutex;
4303 pthread_cond_t cond;
4304 pthread_t thread;
4305 uint32_t tid;
4306 abi_ulong child_tidptr;
4307 abi_ulong parent_tidptr;
4308 sigset_t sigmask;
4309} new_thread_info;
4310
4311static void *clone_func(void *arg)
4312{
4313 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004314 CPUArchState *env;
Andreas Färber0d342822012-12-17 07:12:13 +01004315 CPUState *cpu;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004316 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00004317
4318 env = info->env;
Andreas Färber0d342822012-12-17 07:12:13 +01004319 cpu = ENV_GET_CPU(env);
pbrookd865bab2008-06-07 22:12:17 +00004320 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004321 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004322 info->tid = gettid();
Andreas Färber0d342822012-12-17 07:12:13 +01004323 cpu->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004324 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00004325 if (info->child_tidptr)
4326 put_user_u32(info->tid, info->child_tidptr);
4327 if (info->parent_tidptr)
4328 put_user_u32(info->tid, info->parent_tidptr);
4329 /* Enable signals. */
4330 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4331 /* Signal to the parent that we're ready. */
4332 pthread_mutex_lock(&info->mutex);
4333 pthread_cond_broadcast(&info->cond);
4334 pthread_mutex_unlock(&info->mutex);
4335 /* Wait until the parent has finshed initializing the tls state. */
4336 pthread_mutex_lock(&clone_lock);
4337 pthread_mutex_unlock(&clone_lock);
4338 cpu_loop(env);
4339 /* never exits */
4340 return NULL;
4341}
4342#else
bellard1b6b0292003-03-22 17:31:38 +00004343
4344static int clone_func(void *arg)
4345{
Andreas Färber9349b4f2012-03-14 01:38:32 +01004346 CPUArchState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00004347 cpu_loop(env);
4348 /* never exits */
4349 return 0;
4350}
pbrookd865bab2008-06-07 22:12:17 +00004351#endif
bellard1b6b0292003-03-22 17:31:38 +00004352
ths0da46a62007-10-20 20:23:07 +00004353/* do_fork() Must return host values and target errnos (unlike most
4354 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01004355static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00004356 abi_ulong parent_tidptr, target_ulong newtls,
4357 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00004358{
4359 int ret;
bellard5cd43932003-03-29 16:54:36 +00004360 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004361 CPUArchState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004362#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004363 unsigned int nptl_flags;
4364 sigset_t sigmask;
Riku Voipio91907492010-11-26 16:21:34 +02004365#else
4366 uint8_t *new_stack;
pbrookd865bab2008-06-07 22:12:17 +00004367#endif
ths3b46e622007-09-17 08:09:54 +00004368
balrog436d1242008-09-21 02:39:45 +00004369 /* Emulate vfork() with fork() */
4370 if (flags & CLONE_VFORK)
4371 flags &= ~(CLONE_VFORK | CLONE_VM);
4372
bellard1b6b0292003-03-22 17:31:38 +00004373 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004374 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004375#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004376 new_thread_info info;
4377 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00004378#endif
Anthony Liguori7267c092011-08-20 22:09:37 -05004379 ts = g_malloc0(sizeof(TaskState));
pbrook624f7972008-05-31 16:11:38 +00004380 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00004381 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00004382 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00004383#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
Andreas Färberff18b762012-05-05 14:47:34 +02004384 cpu_reset(ENV_GET_CPU(new_env));
Blue Swirlb4558d72009-11-07 16:34:12 +00004385#endif
pbrook6e68e072008-05-30 17:22:15 +00004386 /* Init regs that differ from the parent. */
4387 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00004388 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004389 ts->bprm = parent_ts->bprm;
4390 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004391#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004392 nptl_flags = flags;
4393 flags &= ~CLONE_NPTL_FLAGS2;
4394
pbrookc2764712009-03-07 15:24:59 +00004395 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4396 ts->child_tidptr = child_tidptr;
4397 }
4398
pbrookd865bab2008-06-07 22:12:17 +00004399 if (nptl_flags & CLONE_SETTLS)
4400 cpu_set_tls (new_env, newtls);
4401
4402 /* Grab a mutex so that thread setup appears atomic. */
4403 pthread_mutex_lock(&clone_lock);
4404
4405 memset(&info, 0, sizeof(info));
4406 pthread_mutex_init(&info.mutex, NULL);
4407 pthread_mutex_lock(&info.mutex);
4408 pthread_cond_init(&info.cond, NULL);
4409 info.env = new_env;
4410 if (nptl_flags & CLONE_CHILD_SETTID)
4411 info.child_tidptr = child_tidptr;
4412 if (nptl_flags & CLONE_PARENT_SETTID)
4413 info.parent_tidptr = parent_tidptr;
4414
4415 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004416 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4417 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00004418 /* It is not safe to deliver signals until the child has finished
4419 initializing, so temporarily block all signals. */
4420 sigfillset(&sigmask);
4421 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4422
4423 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00004424 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00004425
4426 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4427 pthread_attr_destroy(&attr);
4428 if (ret == 0) {
4429 /* Wait for the child to initialize. */
4430 pthread_cond_wait(&info.cond, &info.mutex);
4431 ret = info.tid;
4432 if (flags & CLONE_PARENT_SETTID)
4433 put_user_u32(ret, parent_tidptr);
4434 } else {
4435 ret = -1;
4436 }
4437 pthread_mutex_unlock(&info.mutex);
4438 pthread_cond_destroy(&info.cond);
4439 pthread_mutex_destroy(&info.mutex);
4440 pthread_mutex_unlock(&clone_lock);
4441#else
4442 if (flags & CLONE_NPTL_FLAGS2)
4443 return -EINVAL;
4444 /* This is probably going to die very quickly, but do it anyway. */
Anthony Liguori7267c092011-08-20 22:09:37 -05004445 new_stack = g_malloc0 (NEW_STACK_SIZE);
bellard27725c12003-04-29 21:08:18 +00004446#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02004447 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00004448#else
4449 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
4450#endif
pbrookd865bab2008-06-07 22:12:17 +00004451#endif
bellard1b6b0292003-03-22 17:31:38 +00004452 } else {
4453 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00004454 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00004455 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00004456 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00004457 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00004458 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00004459 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00004460 cpu_clone_regs(env, newsp);
4461 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02004462#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00004463 /* There is a race condition here. The parent process could
4464 theoretically read the TID in the child process before the child
4465 tid is set. This would require using either ptrace
4466 (not implemented) or having *_tidptr to point at a shared memory
4467 mapping. We can't repeat the spinlock hack used above because
4468 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00004469 if (flags & CLONE_CHILD_SETTID)
4470 put_user_u32(gettid(), child_tidptr);
4471 if (flags & CLONE_PARENT_SETTID)
4472 put_user_u32(gettid(), parent_tidptr);
4473 ts = (TaskState *)env->opaque;
4474 if (flags & CLONE_SETTLS)
4475 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00004476 if (flags & CLONE_CHILD_CLEARTID)
4477 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00004478#endif
pbrookd865bab2008-06-07 22:12:17 +00004479 } else {
4480 fork_end(0);
4481 }
bellard1b6b0292003-03-22 17:31:38 +00004482 }
4483 return ret;
4484}
4485
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004486/* warning : doesn't handle linux specific flags... */
4487static int target_to_host_fcntl_cmd(int cmd)
4488{
4489 switch(cmd) {
4490 case TARGET_F_DUPFD:
4491 case TARGET_F_GETFD:
4492 case TARGET_F_SETFD:
4493 case TARGET_F_GETFL:
4494 case TARGET_F_SETFL:
4495 return cmd;
4496 case TARGET_F_GETLK:
4497 return F_GETLK;
4498 case TARGET_F_SETLK:
4499 return F_SETLK;
4500 case TARGET_F_SETLKW:
4501 return F_SETLKW;
4502 case TARGET_F_GETOWN:
4503 return F_GETOWN;
4504 case TARGET_F_SETOWN:
4505 return F_SETOWN;
4506 case TARGET_F_GETSIG:
4507 return F_GETSIG;
4508 case TARGET_F_SETSIG:
4509 return F_SETSIG;
4510#if TARGET_ABI_BITS == 32
4511 case TARGET_F_GETLK64:
4512 return F_GETLK64;
4513 case TARGET_F_SETLK64:
4514 return F_SETLK64;
4515 case TARGET_F_SETLKW64:
4516 return F_SETLKW64;
4517#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004518 case TARGET_F_SETLEASE:
4519 return F_SETLEASE;
4520 case TARGET_F_GETLEASE:
4521 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004522#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004523 case TARGET_F_DUPFD_CLOEXEC:
4524 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004525#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004526 case TARGET_F_NOTIFY:
4527 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004528 default:
4529 return -TARGET_EINVAL;
4530 }
4531 return -TARGET_EINVAL;
4532}
4533
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004534#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4535static const bitmask_transtbl flock_tbl[] = {
4536 TRANSTBL_CONVERT(F_RDLCK),
4537 TRANSTBL_CONVERT(F_WRLCK),
4538 TRANSTBL_CONVERT(F_UNLCK),
4539 TRANSTBL_CONVERT(F_EXLCK),
4540 TRANSTBL_CONVERT(F_SHLCK),
4541 { 0, 0, 0, 0 }
4542};
4543
blueswir1992f48a2007-10-14 16:27:31 +00004544static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004545{
4546 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004547 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004548 struct flock64 fl64;
4549 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00004550 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004551 int host_cmd = target_to_host_fcntl_cmd(cmd);
4552
4553 if (host_cmd == -TARGET_EINVAL)
4554 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004555
bellard7775e9e2003-05-14 22:46:48 +00004556 switch(cmd) {
4557 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004558 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4559 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004560 fl.l_type =
4561 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
ths58134272007-03-31 18:59:32 +00004562 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004563 fl.l_start = tswapal(target_fl->l_start);
4564 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004565 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004566 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004567 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004568 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004569 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4570 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004571 target_fl->l_type =
4572 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004573 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004574 target_fl->l_start = tswapal(fl.l_start);
4575 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004576 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004577 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004578 }
4579 break;
ths3b46e622007-09-17 08:09:54 +00004580
bellard7775e9e2003-05-14 22:46:48 +00004581 case TARGET_F_SETLK:
4582 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004583 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4584 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004585 fl.l_type =
4586 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004587 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004588 fl.l_start = tswapal(target_fl->l_start);
4589 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004590 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004591 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004592 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004593 break;
ths3b46e622007-09-17 08:09:54 +00004594
bellard7775e9e2003-05-14 22:46:48 +00004595 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004596 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4597 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004598 fl64.l_type =
4599 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths58134272007-03-31 18:59:32 +00004600 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004601 fl64.l_start = tswap64(target_fl64->l_start);
4602 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004603 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004604 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004605 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004606 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004607 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4608 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004609 target_fl64->l_type =
4610 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004611 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004612 target_fl64->l_start = tswap64(fl64.l_start);
4613 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004614 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004615 unlock_user_struct(target_fl64, arg, 1);
4616 }
bellard9ee1fa22007-11-11 15:11:19 +00004617 break;
bellard7775e9e2003-05-14 22:46:48 +00004618 case TARGET_F_SETLK64:
4619 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004620 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4621 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004622 fl64.l_type =
4623 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004624 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004625 fl64.l_start = tswap64(target_fl64->l_start);
4626 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004627 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004628 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004629 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004630 break;
4631
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004632 case TARGET_F_GETFL:
4633 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004634 if (ret >= 0) {
4635 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4636 }
bellardffa65c32004-01-04 23:57:22 +00004637 break;
4638
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004639 case TARGET_F_SETFL:
4640 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4641 break;
4642
4643 case TARGET_F_SETOWN:
4644 case TARGET_F_GETOWN:
4645 case TARGET_F_SETSIG:
4646 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004647 case TARGET_F_SETLEASE:
4648 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004649 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00004650 break;
4651
bellard7775e9e2003-05-14 22:46:48 +00004652 default:
bellard9ee1fa22007-11-11 15:11:19 +00004653 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00004654 break;
4655 }
4656 return ret;
4657}
4658
bellard67867302003-11-23 17:05:30 +00004659#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00004660
bellard67867302003-11-23 17:05:30 +00004661static inline int high2lowuid(int uid)
4662{
4663 if (uid > 65535)
4664 return 65534;
4665 else
4666 return uid;
4667}
4668
4669static inline int high2lowgid(int gid)
4670{
4671 if (gid > 65535)
4672 return 65534;
4673 else
4674 return gid;
4675}
4676
4677static inline int low2highuid(int uid)
4678{
4679 if ((int16_t)uid == -1)
4680 return -1;
4681 else
4682 return uid;
4683}
4684
4685static inline int low2highgid(int gid)
4686{
4687 if ((int16_t)gid == -1)
4688 return -1;
4689 else
4690 return gid;
4691}
Riku Voipio0c866a72011-04-18 15:23:06 +03004692static inline int tswapid(int id)
4693{
4694 return tswap16(id);
4695}
4696#else /* !USE_UID16 */
4697static inline int high2lowuid(int uid)
4698{
4699 return uid;
4700}
4701static inline int high2lowgid(int gid)
4702{
4703 return gid;
4704}
4705static inline int low2highuid(int uid)
4706{
4707 return uid;
4708}
4709static inline int low2highgid(int gid)
4710{
4711 return gid;
4712}
4713static inline int tswapid(int id)
4714{
4715 return tswap32(id);
4716}
bellard67867302003-11-23 17:05:30 +00004717#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004718
bellard31e31b82003-02-18 22:55:36 +00004719void syscall_init(void)
4720{
bellard2ab83ea2003-06-15 19:56:46 +00004721 IOCTLEntry *ie;
4722 const argtype *arg_type;
4723 int size;
thsb92c47c2007-11-01 00:07:38 +00004724 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004725
Blue Swirl001faf32009-05-13 17:53:17 +00004726#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004727#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004728#include "syscall_types.h"
4729#undef STRUCT
4730#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00004731
Peter Maydelldd6e9572012-07-23 08:07:22 +00004732 /* Build target_to_host_errno_table[] table from
4733 * host_to_target_errno_table[]. */
4734 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4735 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4736 }
4737
bellard2ab83ea2003-06-15 19:56:46 +00004738 /* we patch the ioctl size if necessary. We rely on the fact that
4739 no ioctl has all the bits at '1' in the size field */
4740 ie = ioctl_entries;
4741 while (ie->target_cmd != 0) {
4742 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4743 TARGET_IOC_SIZEMASK) {
4744 arg_type = ie->arg_type;
4745 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00004746 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00004747 ie->target_cmd);
4748 exit(1);
4749 }
4750 arg_type++;
4751 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00004752 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00004753 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4754 (size << TARGET_IOC_SIZESHIFT);
4755 }
thsb92c47c2007-11-01 00:07:38 +00004756
bellard2ab83ea2003-06-15 19:56:46 +00004757 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00004758#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4759 (defined(__x86_64__) && defined(TARGET_X86_64))
4760 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4761 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4762 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00004763 }
4764#endif
4765 ie++;
4766 }
bellard31e31b82003-02-18 22:55:36 +00004767}
bellardc573ff62004-01-04 15:51:36 +00004768
blueswir1992f48a2007-10-14 16:27:31 +00004769#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004770static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4771{
thsaf325d32008-06-10 15:29:15 +00004772#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004773 return ((uint64_t)word0 << 32) | word1;
4774#else
4775 return ((uint64_t)word1 << 32) | word0;
4776#endif
4777}
blueswir1992f48a2007-10-14 16:27:31 +00004778#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004779static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4780{
4781 return word0;
4782}
blueswir1992f48a2007-10-14 16:27:31 +00004783#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004784
4785#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004786static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4787 abi_long arg2,
4788 abi_long arg3,
4789 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004790{
Riku Voipio48e515d2011-07-12 15:40:51 +03004791 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004792 arg2 = arg3;
4793 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004794 }
pbrookce4defa2006-02-09 16:49:55 +00004795 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4796}
4797#endif
4798
4799#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004800static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4801 abi_long arg2,
4802 abi_long arg3,
4803 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004804{
Riku Voipio48e515d2011-07-12 15:40:51 +03004805 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004806 arg2 = arg3;
4807 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004808 }
pbrookce4defa2006-02-09 16:49:55 +00004809 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4810}
4811#endif
4812
bellard579a97f2007-11-11 14:26:47 +00004813static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4814 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004815{
4816 struct target_timespec *target_ts;
4817
bellard579a97f2007-11-11 14:26:47 +00004818 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4819 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004820 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4821 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004822 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004823 return 0;
pbrook53a59602006-03-25 19:31:22 +00004824}
4825
bellard579a97f2007-11-11 14:26:47 +00004826static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4827 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004828{
4829 struct target_timespec *target_ts;
4830
bellard579a97f2007-11-11 14:26:47 +00004831 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4832 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004833 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4834 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004835 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004836 return 0;
pbrook53a59602006-03-25 19:31:22 +00004837}
4838
aurel329d33b762009-04-08 23:07:05 +00004839#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004840static inline abi_long host_to_target_stat64(void *cpu_env,
4841 abi_ulong target_addr,
4842 struct stat *host_st)
4843{
4844#ifdef TARGET_ARM
4845 if (((CPUARMState *)cpu_env)->eabi) {
4846 struct target_eabi_stat64 *target_st;
4847
4848 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4849 return -TARGET_EFAULT;
4850 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4851 __put_user(host_st->st_dev, &target_st->st_dev);
4852 __put_user(host_st->st_ino, &target_st->st_ino);
4853#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4854 __put_user(host_st->st_ino, &target_st->__st_ino);
4855#endif
4856 __put_user(host_st->st_mode, &target_st->st_mode);
4857 __put_user(host_st->st_nlink, &target_st->st_nlink);
4858 __put_user(host_st->st_uid, &target_st->st_uid);
4859 __put_user(host_st->st_gid, &target_st->st_gid);
4860 __put_user(host_st->st_rdev, &target_st->st_rdev);
4861 __put_user(host_st->st_size, &target_st->st_size);
4862 __put_user(host_st->st_blksize, &target_st->st_blksize);
4863 __put_user(host_st->st_blocks, &target_st->st_blocks);
4864 __put_user(host_st->st_atime, &target_st->target_st_atime);
4865 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4866 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4867 unlock_user_struct(target_st, target_addr, 1);
4868 } else
4869#endif
4870 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004871#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004872 struct target_stat *target_st;
4873#else
balrog6a24a772008-09-20 02:23:36 +00004874 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004875#endif
balrog6a24a772008-09-20 02:23:36 +00004876
4877 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4878 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004879 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004880 __put_user(host_st->st_dev, &target_st->st_dev);
4881 __put_user(host_st->st_ino, &target_st->st_ino);
4882#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4883 __put_user(host_st->st_ino, &target_st->__st_ino);
4884#endif
4885 __put_user(host_st->st_mode, &target_st->st_mode);
4886 __put_user(host_st->st_nlink, &target_st->st_nlink);
4887 __put_user(host_st->st_uid, &target_st->st_uid);
4888 __put_user(host_st->st_gid, &target_st->st_gid);
4889 __put_user(host_st->st_rdev, &target_st->st_rdev);
4890 /* XXX: better use of kernel struct */
4891 __put_user(host_st->st_size, &target_st->st_size);
4892 __put_user(host_st->st_blksize, &target_st->st_blksize);
4893 __put_user(host_st->st_blocks, &target_st->st_blocks);
4894 __put_user(host_st->st_atime, &target_st->target_st_atime);
4895 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4896 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4897 unlock_user_struct(target_st, target_addr, 1);
4898 }
4899
4900 return 0;
4901}
4902#endif
4903
Juan Quintela2f7bb872009-07-27 16:13:24 +02004904#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004905/* ??? Using host futex calls even when target atomic operations
4906 are not really atomic probably breaks things. However implementing
4907 futexes locally would make futexes shared between multiple processes
4908 tricky. However they're probably useless because guest atomic
4909 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004910static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4911 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004912{
4913 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004914 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004915
4916 /* ??? We assume FUTEX_* constants are the same on both host
4917 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004918#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004919 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004920#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004921 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004922#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004923 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004924 case FUTEX_WAIT:
John Rigbycce246e2013-02-23 16:14:07 -07004925 case FUTEX_WAIT_BITSET:
pbrookbd0c5662008-05-29 14:34:11 +00004926 if (timeout) {
4927 pts = &ts;
4928 target_to_host_timespec(pts, timeout);
4929 } else {
4930 pts = NULL;
4931 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004932 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
John Rigbycce246e2013-02-23 16:14:07 -07004933 pts, NULL, val3));
pbrookbd0c5662008-05-29 14:34:11 +00004934 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004935 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004936 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004937 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004938 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004939 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004940 case FUTEX_WAKE_OP:
4941 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4942 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4943 But the prototype takes a `struct timespec *'; insert casts
4944 to satisfy the compiler. We do not need to tswap TIMEOUT
4945 since it's not compared to guest memory. */
4946 pts = (struct timespec *)(uintptr_t) timeout;
4947 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4948 g2h(uaddr2),
4949 (base_op == FUTEX_CMP_REQUEUE
4950 ? tswap32(val3)
4951 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004952 default:
4953 return -TARGET_ENOSYS;
4954 }
4955}
4956#endif
4957
pbrook1d9d8b52009-04-16 15:17:02 +00004958/* Map host to target signal numbers for the wait family of syscalls.
4959 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07004960int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00004961{
4962 if (WIFSIGNALED(status)) {
4963 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4964 }
4965 if (WIFSTOPPED(status)) {
4966 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4967 | (status & 0xff);
4968 }
4969 return status;
4970}
4971
pbrooka745ec62008-05-06 15:36:17 +00004972int get_osversion(void)
4973{
4974 static int osversion;
4975 struct new_utsname buf;
4976 const char *s;
4977 int i, n, tmp;
4978 if (osversion)
4979 return osversion;
4980 if (qemu_uname_release && *qemu_uname_release) {
4981 s = qemu_uname_release;
4982 } else {
4983 if (sys_uname(&buf))
4984 return 0;
4985 s = buf.release;
4986 }
4987 tmp = 0;
4988 for (i = 0; i < 3; i++) {
4989 n = 0;
4990 while (*s >= '0' && *s <= '9') {
4991 n *= 10;
4992 n += *s - '0';
4993 s++;
4994 }
4995 tmp = (tmp << 8) + n;
4996 if (*s == '.')
4997 s++;
4998 }
4999 osversion = tmp;
5000 return osversion;
5001}
5002
Alexander Graf36c08d42011-11-02 20:23:24 +01005003
5004static int open_self_maps(void *cpu_env, int fd)
5005{
Alexander Graf1a49ef22012-05-01 16:30:28 +01005006#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Andreas Färber9349b4f2012-03-14 01:38:32 +01005007 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf1a49ef22012-05-01 16:30:28 +01005008#endif
5009 FILE *fp;
5010 char *line = NULL;
5011 size_t len = 0;
5012 ssize_t read;
Alexander Graf36c08d42011-11-02 20:23:24 +01005013
Alexander Graf1a49ef22012-05-01 16:30:28 +01005014 fp = fopen("/proc/self/maps", "r");
5015 if (fp == NULL) {
5016 return -EACCES;
5017 }
5018
5019 while ((read = getline(&line, &len, fp)) != -1) {
5020 int fields, dev_maj, dev_min, inode;
5021 uint64_t min, max, offset;
5022 char flag_r, flag_w, flag_x, flag_p;
5023 char path[512] = "";
5024 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
5025 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
5026 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
5027
5028 if ((fields < 10) || (fields > 11)) {
5029 continue;
5030 }
5031 if (!strncmp(path, "[stack]", 7)) {
5032 continue;
5033 }
5034 if (h2g_valid(min) && h2g_valid(max)) {
5035 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
5036 " %c%c%c%c %08" PRIx64 " %02x:%02x %d%s%s\n",
5037 h2g(min), h2g(max), flag_r, flag_w,
5038 flag_x, flag_p, offset, dev_maj, dev_min, inode,
5039 path[0] ? " " : "", path);
5040 }
5041 }
5042
5043 free(line);
5044 fclose(fp);
5045
5046#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Alexander Graf36c08d42011-11-02 20:23:24 +01005047 dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
5048 (unsigned long long)ts->info->stack_limit,
Alexander Graf1bdd7c72012-05-30 14:45:21 +02005049 (unsigned long long)(ts->info->start_stack +
5050 (TARGET_PAGE_SIZE - 1)) & TARGET_PAGE_MASK,
Alexander Graf1a49ef22012-05-01 16:30:28 +01005051 (unsigned long long)0);
5052#endif
Alexander Graf36c08d42011-11-02 20:23:24 +01005053
5054 return 0;
5055}
5056
Alexander Graf480b8e72011-11-02 20:23:25 +01005057static int open_self_stat(void *cpu_env, int fd)
5058{
Andreas Färber9349b4f2012-03-14 01:38:32 +01005059 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf480b8e72011-11-02 20:23:25 +01005060 abi_ulong start_stack = ts->info->start_stack;
5061 int i;
5062
5063 for (i = 0; i < 44; i++) {
5064 char buf[128];
5065 int len;
5066 uint64_t val = 0;
5067
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005068 if (i == 0) {
5069 /* pid */
5070 val = getpid();
5071 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5072 } else if (i == 1) {
5073 /* app name */
5074 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5075 } else if (i == 27) {
5076 /* stack bottom */
5077 val = start_stack;
5078 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5079 } else {
5080 /* for the rest, there is MasterCard */
5081 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
Alexander Graf480b8e72011-11-02 20:23:25 +01005082 }
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005083
Alexander Graf480b8e72011-11-02 20:23:25 +01005084 len = strlen(buf);
5085 if (write(fd, buf, len) != len) {
5086 return -1;
5087 }
5088 }
5089
5090 return 0;
5091}
5092
Alexander Graf257450e2011-11-02 20:23:26 +01005093static int open_self_auxv(void *cpu_env, int fd)
5094{
Andreas Färber9349b4f2012-03-14 01:38:32 +01005095 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01005096 abi_ulong auxv = ts->info->saved_auxv;
5097 abi_ulong len = ts->info->auxv_len;
5098 char *ptr;
5099
5100 /*
5101 * Auxiliary vector is stored in target process stack.
5102 * read in whole auxv vector and copy it to file
5103 */
5104 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5105 if (ptr != NULL) {
5106 while (len > 0) {
5107 ssize_t r;
5108 r = write(fd, ptr, len);
5109 if (r <= 0) {
5110 break;
5111 }
5112 len -= r;
5113 ptr += r;
5114 }
5115 lseek(fd, 0, SEEK_SET);
5116 unlock_user(ptr, auxv, len);
5117 }
5118
5119 return 0;
5120}
5121
Alexander Graf3be14d02011-11-02 20:23:23 +01005122static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
5123{
5124 struct fake_open {
5125 const char *filename;
5126 int (*fill)(void *cpu_env, int fd);
5127 };
5128 const struct fake_open *fake_open;
5129 static const struct fake_open fakes[] = {
Alexander Graf36c08d42011-11-02 20:23:24 +01005130 { "/proc/self/maps", open_self_maps },
Alexander Graf480b8e72011-11-02 20:23:25 +01005131 { "/proc/self/stat", open_self_stat },
Alexander Graf257450e2011-11-02 20:23:26 +01005132 { "/proc/self/auxv", open_self_auxv },
Alexander Graf3be14d02011-11-02 20:23:23 +01005133 { NULL, NULL }
5134 };
5135
5136 for (fake_open = fakes; fake_open->filename; fake_open++) {
5137 if (!strncmp(pathname, fake_open->filename,
5138 strlen(fake_open->filename))) {
5139 break;
5140 }
5141 }
5142
5143 if (fake_open->filename) {
5144 const char *tmpdir;
5145 char filename[PATH_MAX];
5146 int fd, r;
5147
5148 /* create temporary file to map stat to */
5149 tmpdir = getenv("TMPDIR");
5150 if (!tmpdir)
5151 tmpdir = "/tmp";
5152 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5153 fd = mkstemp(filename);
5154 if (fd < 0) {
5155 return fd;
5156 }
5157 unlink(filename);
5158
5159 if ((r = fake_open->fill(cpu_env, fd))) {
5160 close(fd);
5161 return r;
5162 }
5163 lseek(fd, 0, SEEK_SET);
5164
5165 return fd;
5166 }
5167
5168 return get_errno(open(path(pathname), flags, mode));
5169}
5170
ths0da46a62007-10-20 20:23:07 +00005171/* do_syscall() should always have a single exit point at the end so
5172 that actions, such as logging of syscall results, can be performed.
5173 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00005174abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5175 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01005176 abi_long arg5, abi_long arg6, abi_long arg7,
5177 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00005178{
blueswir1992f48a2007-10-14 16:27:31 +00005179 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005180 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00005181 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00005182 void *p;
ths3b46e622007-09-17 08:09:54 +00005183
bellard72f03902003-02-18 23:33:18 +00005184#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00005185 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00005186#endif
thsb92c47c2007-11-01 00:07:38 +00005187 if(do_strace)
5188 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5189
bellard31e31b82003-02-18 22:55:36 +00005190 switch(num) {
5191 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02005192#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00005193 /* In old applications this may be used to implement _exit(2).
5194 However in threaded applictions it is used for thread termination,
5195 and _exit_group is used for application termination.
5196 Do thread termination if we have more then one thread. */
5197 /* FIXME: This probably breaks if a signal arrives. We should probably
5198 be disabling signals. */
5199 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07005200 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005201 CPUArchState **lastp;
5202 CPUArchState *p;
pbrookc2764712009-03-07 15:24:59 +00005203
5204 cpu_list_lock();
5205 lastp = &first_cpu;
5206 p = first_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005207 while (p && p != (CPUArchState *)cpu_env) {
pbrookc2764712009-03-07 15:24:59 +00005208 lastp = &p->next_cpu;
5209 p = p->next_cpu;
5210 }
5211 /* If we didn't find the CPU for this thread then something is
5212 horribly wrong. */
5213 if (!p)
5214 abort();
5215 /* Remove the CPU from the list. */
5216 *lastp = p->next_cpu;
5217 cpu_list_unlock();
Andreas Färber9349b4f2012-03-14 01:38:32 +01005218 ts = ((CPUArchState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00005219 if (ts->child_tidptr) {
5220 put_user_u32(0, ts->child_tidptr);
5221 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5222 NULL, NULL, 0);
5223 }
Nathan Froyd48e15fc2010-10-29 07:48:57 -07005224 thread_env = NULL;
Paolo Bonzini5c099532013-01-25 14:12:38 +01005225 object_unref(OBJECT(ENV_GET_CPU(cpu_env)));
Anthony Liguori7267c092011-08-20 22:09:37 -05005226 g_free(ts);
pbrookc2764712009-03-07 15:24:59 +00005227 pthread_exit(NULL);
5228 }
5229#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02005230#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00005231 _mcleanup();
5232#endif
bellarde9009672005-04-26 20:42:36 +00005233 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00005234 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00005235 ret = 0; /* avoid warning */
5236 break;
5237 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00005238 if (arg3 == 0)
5239 ret = 0;
5240 else {
5241 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5242 goto efault;
5243 ret = get_errno(read(arg1, p, arg3));
5244 unlock_user(p, arg2, ret);
5245 }
bellard31e31b82003-02-18 22:55:36 +00005246 break;
5247 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00005248 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5249 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005250 ret = get_errno(write(arg1, p, arg3));
5251 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00005252 break;
5253 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00005254 if (!(p = lock_user_string(arg1)))
5255 goto efault;
Alexander Graf3be14d02011-11-02 20:23:23 +01005256 ret = get_errno(do_open(cpu_env, p,
5257 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5258 arg3));
pbrook53a59602006-03-25 19:31:22 +00005259 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005260 break;
ths82424832007-09-24 09:21:55 +00005261#if defined(TARGET_NR_openat) && defined(__NR_openat)
5262 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00005263 if (!(p = lock_user_string(arg2)))
5264 goto efault;
5265 ret = get_errno(sys_openat(arg1,
5266 path(p),
5267 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5268 arg4));
5269 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00005270 break;
5271#endif
bellard31e31b82003-02-18 22:55:36 +00005272 case TARGET_NR_close:
5273 ret = get_errno(close(arg1));
5274 break;
5275 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00005276 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00005277 break;
5278 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00005279 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00005280 break;
thse5febef2007-04-01 18:31:35 +00005281#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00005282 case TARGET_NR_waitpid:
5283 {
pbrook53a59602006-03-25 19:31:22 +00005284 int status;
5285 ret = get_errno(waitpid(arg1, &status, arg3));
Alexander Graf53795572011-11-24 00:44:43 +01005286 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00005287 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00005288 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005289 }
5290 break;
thse5febef2007-04-01 18:31:35 +00005291#endif
pbrookf0cbb612008-05-30 18:20:05 +00005292#ifdef TARGET_NR_waitid
5293 case TARGET_NR_waitid:
5294 {
5295 siginfo_t info;
5296 info.si_pid = 0;
5297 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5298 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005299 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00005300 goto efault;
5301 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05005302 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00005303 }
5304 }
5305 break;
5306#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005307#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005308 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00005309 if (!(p = lock_user_string(arg1)))
5310 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005311 ret = get_errno(creat(p, arg2));
5312 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005313 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005314#endif
bellard31e31b82003-02-18 22:55:36 +00005315 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00005316 {
5317 void * p2;
5318 p = lock_user_string(arg1);
5319 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005320 if (!p || !p2)
5321 ret = -TARGET_EFAULT;
5322 else
5323 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005324 unlock_user(p2, arg2, 0);
5325 unlock_user(p, arg1, 0);
5326 }
bellard31e31b82003-02-18 22:55:36 +00005327 break;
ths64f0ce42007-09-24 09:25:06 +00005328#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
5329 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00005330 {
5331 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00005332 if (!arg2 || !arg4)
5333 goto efault;
ths64f0ce42007-09-24 09:25:06 +00005334 p = lock_user_string(arg2);
5335 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005336 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005337 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00005338 else
5339 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00005340 unlock_user(p, arg2, 0);
5341 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00005342 }
5343 break;
5344#endif
bellard31e31b82003-02-18 22:55:36 +00005345 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00005346 if (!(p = lock_user_string(arg1)))
5347 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005348 ret = get_errno(unlink(p));
5349 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005350 break;
ths8170f562007-09-24 09:24:11 +00005351#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
5352 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00005353 if (!(p = lock_user_string(arg2)))
5354 goto efault;
5355 ret = get_errno(sys_unlinkat(arg1, p, arg3));
5356 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00005357 break;
balrogb7d35e62007-12-12 00:40:24 +00005358#endif
bellard31e31b82003-02-18 22:55:36 +00005359 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00005360 {
5361 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00005362 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00005363 abi_ulong gp;
5364 abi_ulong guest_argp;
5365 abi_ulong guest_envp;
5366 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00005367 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005368 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00005369
bellardf7341ff2003-03-30 21:00:25 +00005370 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00005371 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00005372 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005373 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005374 goto efault;
ths03aa1972007-12-02 06:28:08 +00005375 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005376 break;
bellard7854b052003-03-29 17:22:23 +00005377 argc++;
bellard2f619692007-11-16 10:46:05 +00005378 }
bellardf7341ff2003-03-30 21:00:25 +00005379 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00005380 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00005381 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005382 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005383 goto efault;
ths03aa1972007-12-02 06:28:08 +00005384 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005385 break;
bellard7854b052003-03-29 17:22:23 +00005386 envc++;
bellard2f619692007-11-16 10:46:05 +00005387 }
bellard7854b052003-03-29 17:22:23 +00005388
bellardf7341ff2003-03-30 21:00:25 +00005389 argp = alloca((argc + 1) * sizeof(void *));
5390 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00005391
pbrookda94d262008-05-30 18:24:00 +00005392 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005393 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005394 if (get_user_ual(addr, gp))
5395 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005396 if (!addr)
5397 break;
bellard2f619692007-11-16 10:46:05 +00005398 if (!(*q = lock_user_string(addr)))
5399 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005400 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005401 }
bellardf7341ff2003-03-30 21:00:25 +00005402 *q = NULL;
5403
pbrookda94d262008-05-30 18:24:00 +00005404 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005405 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005406 if (get_user_ual(addr, gp))
5407 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005408 if (!addr)
5409 break;
bellard2f619692007-11-16 10:46:05 +00005410 if (!(*q = lock_user_string(addr)))
5411 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005412 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005413 }
bellardf7341ff2003-03-30 21:00:25 +00005414 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00005415
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005416 /* This case will not be caught by the host's execve() if its
5417 page size is bigger than the target's. */
5418 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5419 ret = -TARGET_E2BIG;
5420 goto execve_end;
5421 }
bellard2f619692007-11-16 10:46:05 +00005422 if (!(p = lock_user_string(arg1)))
5423 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005424 ret = get_errno(execve(p, argp, envp));
5425 unlock_user(p, arg1, 0);
5426
bellard2f619692007-11-16 10:46:05 +00005427 goto execve_end;
5428
5429 execve_efault:
5430 ret = -TARGET_EFAULT;
5431
5432 execve_end:
pbrook53a59602006-03-25 19:31:22 +00005433 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005434 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005435 if (get_user_ual(addr, gp)
5436 || !addr)
5437 break;
pbrook53a59602006-03-25 19:31:22 +00005438 unlock_user(*q, addr, 0);
5439 }
5440 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005441 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005442 if (get_user_ual(addr, gp)
5443 || !addr)
5444 break;
pbrook53a59602006-03-25 19:31:22 +00005445 unlock_user(*q, addr, 0);
5446 }
bellard7854b052003-03-29 17:22:23 +00005447 }
bellard31e31b82003-02-18 22:55:36 +00005448 break;
5449 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00005450 if (!(p = lock_user_string(arg1)))
5451 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005452 ret = get_errno(chdir(p));
5453 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005454 break;
bellarda315a142005-01-30 22:59:18 +00005455#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00005456 case TARGET_NR_time:
5457 {
pbrook53a59602006-03-25 19:31:22 +00005458 time_t host_time;
5459 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00005460 if (!is_error(ret)
5461 && arg1
5462 && put_user_sal(host_time, arg1))
5463 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005464 }
5465 break;
bellarda315a142005-01-30 22:59:18 +00005466#endif
bellard31e31b82003-02-18 22:55:36 +00005467 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00005468 if (!(p = lock_user_string(arg1)))
5469 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005470 ret = get_errno(mknod(p, arg2, arg3));
5471 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005472 break;
ths75ac37a2007-09-24 09:23:05 +00005473#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
5474 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00005475 if (!(p = lock_user_string(arg2)))
5476 goto efault;
5477 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
5478 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00005479 break;
5480#endif
bellard31e31b82003-02-18 22:55:36 +00005481 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00005482 if (!(p = lock_user_string(arg1)))
5483 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005484 ret = get_errno(chmod(p, arg2));
5485 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005486 break;
bellardebc05482003-09-30 21:08:41 +00005487#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00005488 case TARGET_NR_break:
5489 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005490#endif
5491#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00005492 case TARGET_NR_oldstat:
5493 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005494#endif
bellard31e31b82003-02-18 22:55:36 +00005495 case TARGET_NR_lseek:
5496 ret = get_errno(lseek(arg1, arg2, arg3));
5497 break;
Richard Henderson92317332010-05-03 10:07:53 -07005498#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5499 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00005500 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07005501 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00005502 ret = get_errno(getpid());
5503 break;
Richard Henderson92317332010-05-03 10:07:53 -07005504#endif
5505#ifdef TARGET_NR_getpid
5506 case TARGET_NR_getpid:
5507 ret = get_errno(getpid());
5508 break;
5509#endif
bellard31e31b82003-02-18 22:55:36 +00005510 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00005511 {
5512 /* need to look at the data field */
5513 void *p2, *p3;
5514 p = lock_user_string(arg1);
5515 p2 = lock_user_string(arg2);
5516 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005517 if (!p || !p2 || !p3)
5518 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005519 else {
bellard579a97f2007-11-11 14:26:47 +00005520 /* FIXME - arg5 should be locked, but it isn't clear how to
5521 * do that since it's not guaranteed to be a NULL-terminated
5522 * string.
5523 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005524 if ( ! arg5 )
5525 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
5526 else
5527 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
5528 }
bellard579a97f2007-11-11 14:26:47 +00005529 unlock_user(p, arg1, 0);
5530 unlock_user(p2, arg2, 0);
5531 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00005532 break;
5533 }
thse5febef2007-04-01 18:31:35 +00005534#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00005535 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00005536 if (!(p = lock_user_string(arg1)))
5537 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005538 ret = get_errno(umount(p));
5539 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005540 break;
thse5febef2007-04-01 18:31:35 +00005541#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005542#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005543 case TARGET_NR_stime:
5544 {
pbrook53a59602006-03-25 19:31:22 +00005545 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00005546 if (get_user_sal(host_time, arg1))
5547 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005548 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00005549 }
5550 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005551#endif
bellard31e31b82003-02-18 22:55:36 +00005552 case TARGET_NR_ptrace:
5553 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00005554#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005555 case TARGET_NR_alarm:
5556 ret = alarm(arg1);
5557 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005558#endif
bellardebc05482003-09-30 21:08:41 +00005559#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00005560 case TARGET_NR_oldfstat:
5561 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005562#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005563#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005564 case TARGET_NR_pause:
5565 ret = get_errno(pause());
5566 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005567#endif
thse5febef2007-04-01 18:31:35 +00005568#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00005569 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00005570 {
pbrook53a59602006-03-25 19:31:22 +00005571 struct utimbuf tbuf, *host_tbuf;
5572 struct target_utimbuf *target_tbuf;
5573 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005574 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5575 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005576 tbuf.actime = tswapal(target_tbuf->actime);
5577 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00005578 unlock_user_struct(target_tbuf, arg2, 0);
5579 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00005580 } else {
pbrook53a59602006-03-25 19:31:22 +00005581 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00005582 }
bellard579a97f2007-11-11 14:26:47 +00005583 if (!(p = lock_user_string(arg1)))
5584 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005585 ret = get_errno(utime(p, host_tbuf));
5586 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00005587 }
5588 break;
thse5febef2007-04-01 18:31:35 +00005589#endif
bellard978a66f2004-12-06 22:58:05 +00005590 case TARGET_NR_utimes:
5591 {
bellard978a66f2004-12-06 22:58:05 +00005592 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00005593 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00005594 if (copy_from_user_timeval(&tv[0], arg2)
5595 || copy_from_user_timeval(&tv[1],
5596 arg2 + sizeof(struct target_timeval)))
5597 goto efault;
bellard978a66f2004-12-06 22:58:05 +00005598 tvp = tv;
5599 } else {
5600 tvp = NULL;
5601 }
bellard579a97f2007-11-11 14:26:47 +00005602 if (!(p = lock_user_string(arg1)))
5603 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005604 ret = get_errno(utimes(p, tvp));
5605 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00005606 }
5607 break;
balrogac8a6552008-09-20 02:25:39 +00005608#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
5609 case TARGET_NR_futimesat:
5610 {
5611 struct timeval *tvp, tv[2];
5612 if (arg3) {
5613 if (copy_from_user_timeval(&tv[0], arg3)
5614 || copy_from_user_timeval(&tv[1],
5615 arg3 + sizeof(struct target_timeval)))
5616 goto efault;
5617 tvp = tv;
5618 } else {
5619 tvp = NULL;
5620 }
5621 if (!(p = lock_user_string(arg2)))
5622 goto efault;
5623 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
5624 unlock_user(p, arg2, 0);
5625 }
5626 break;
5627#endif
bellardebc05482003-09-30 21:08:41 +00005628#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00005629 case TARGET_NR_stty:
5630 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005631#endif
5632#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00005633 case TARGET_NR_gtty:
5634 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005635#endif
bellard31e31b82003-02-18 22:55:36 +00005636 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00005637 if (!(p = lock_user_string(arg1)))
5638 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02005639 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00005640 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005641 break;
ths92a34c12007-09-24 09:27:49 +00005642#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5643 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00005644 if (!(p = lock_user_string(arg2)))
5645 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00005646 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005647 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00005648 break;
5649#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005650#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005651 case TARGET_NR_nice:
5652 ret = get_errno(nice(arg1));
5653 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005654#endif
bellardebc05482003-09-30 21:08:41 +00005655#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00005656 case TARGET_NR_ftime:
5657 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005658#endif
bellard31e31b82003-02-18 22:55:36 +00005659 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00005660 sync();
5661 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00005662 break;
5663 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00005664 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00005665 break;
5666 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00005667 {
5668 void *p2;
5669 p = lock_user_string(arg1);
5670 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005671 if (!p || !p2)
5672 ret = -TARGET_EFAULT;
5673 else
5674 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005675 unlock_user(p2, arg2, 0);
5676 unlock_user(p, arg1, 0);
5677 }
bellard31e31b82003-02-18 22:55:36 +00005678 break;
ths722183f2007-09-24 09:24:37 +00005679#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
5680 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00005681 {
bellard579a97f2007-11-11 14:26:47 +00005682 void *p2;
ths722183f2007-09-24 09:24:37 +00005683 p = lock_user_string(arg2);
5684 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005685 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005686 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00005687 else
5688 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00005689 unlock_user(p2, arg4, 0);
5690 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00005691 }
5692 break;
5693#endif
bellard31e31b82003-02-18 22:55:36 +00005694 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00005695 if (!(p = lock_user_string(arg1)))
5696 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005697 ret = get_errno(mkdir(p, arg2));
5698 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005699 break;
ths4472ad02007-09-24 09:22:32 +00005700#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
5701 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00005702 if (!(p = lock_user_string(arg2)))
5703 goto efault;
5704 ret = get_errno(sys_mkdirat(arg1, p, arg3));
5705 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00005706 break;
5707#endif
bellard31e31b82003-02-18 22:55:36 +00005708 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00005709 if (!(p = lock_user_string(arg1)))
5710 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005711 ret = get_errno(rmdir(p));
5712 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005713 break;
5714 case TARGET_NR_dup:
5715 ret = get_errno(dup(arg1));
5716 break;
5717 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005718 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00005719 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03005720#ifdef TARGET_NR_pipe2
5721 case TARGET_NR_pipe2:
Richard Hendersone7ea6cb2012-06-01 18:48:39 -07005722 ret = do_pipe(cpu_env, arg1,
5723 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03005724 break;
5725#endif
bellard31e31b82003-02-18 22:55:36 +00005726 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00005727 {
pbrook53a59602006-03-25 19:31:22 +00005728 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00005729 struct tms tms;
5730 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00005731 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00005732 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5733 if (!tmsp)
5734 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005735 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5736 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5737 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5738 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00005739 }
bellardc596ed12003-07-13 17:32:31 +00005740 if (!is_error(ret))
5741 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00005742 }
5743 break;
bellardebc05482003-09-30 21:08:41 +00005744#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00005745 case TARGET_NR_prof:
5746 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005747#endif
thse5febef2007-04-01 18:31:35 +00005748#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00005749 case TARGET_NR_signal:
5750 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005751#endif
bellard31e31b82003-02-18 22:55:36 +00005752 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00005753 if (arg1 == 0) {
5754 ret = get_errno(acct(NULL));
5755 } else {
5756 if (!(p = lock_user_string(arg1)))
5757 goto efault;
5758 ret = get_errno(acct(path(p)));
5759 unlock_user(p, arg1, 0);
5760 }
pbrook24836682006-04-16 14:14:53 +00005761 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005762#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005763 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00005764 if (!(p = lock_user_string(arg1)))
5765 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005766 ret = get_errno(umount2(p, arg2));
5767 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005768 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005769#endif
bellardebc05482003-09-30 21:08:41 +00005770#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00005771 case TARGET_NR_lock:
5772 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005773#endif
bellard31e31b82003-02-18 22:55:36 +00005774 case TARGET_NR_ioctl:
5775 ret = do_ioctl(arg1, arg2, arg3);
5776 break;
5777 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00005778 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00005779 break;
bellardebc05482003-09-30 21:08:41 +00005780#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00005781 case TARGET_NR_mpx:
5782 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005783#endif
bellard31e31b82003-02-18 22:55:36 +00005784 case TARGET_NR_setpgid:
5785 ret = get_errno(setpgid(arg1, arg2));
5786 break;
bellardebc05482003-09-30 21:08:41 +00005787#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00005788 case TARGET_NR_ulimit:
5789 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005790#endif
5791#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00005792 case TARGET_NR_oldolduname:
5793 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005794#endif
bellard31e31b82003-02-18 22:55:36 +00005795 case TARGET_NR_umask:
5796 ret = get_errno(umask(arg1));
5797 break;
5798 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00005799 if (!(p = lock_user_string(arg1)))
5800 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005801 ret = get_errno(chroot(p));
5802 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005803 break;
5804 case TARGET_NR_ustat:
5805 goto unimplemented;
5806 case TARGET_NR_dup2:
5807 ret = get_errno(dup2(arg1, arg2));
5808 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03005809#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5810 case TARGET_NR_dup3:
5811 ret = get_errno(dup3(arg1, arg2, arg3));
5812 break;
5813#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005814#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005815 case TARGET_NR_getppid:
5816 ret = get_errno(getppid());
5817 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005818#endif
bellard31e31b82003-02-18 22:55:36 +00005819 case TARGET_NR_getpgrp:
5820 ret = get_errno(getpgrp());
5821 break;
5822 case TARGET_NR_setsid:
5823 ret = get_errno(setsid());
5824 break;
thse5febef2007-04-01 18:31:35 +00005825#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00005826 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00005827 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005828#if defined(TARGET_ALPHA)
5829 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00005830 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00005831 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005832 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5833 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005834 act._sa_handler = old_act->_sa_handler;
5835 target_siginitset(&act.sa_mask, old_act->sa_mask);
5836 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005837 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00005838 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005839 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00005840 }
5841 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00005842 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005843 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5844 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005845 old_act->_sa_handler = oact._sa_handler;
5846 old_act->sa_mask = oact.sa_mask.sig[0];
5847 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00005848 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00005849 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005850#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00005851 struct target_sigaction act, oact, *pact, *old_act;
5852
5853 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005854 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5855 goto efault;
bellard106ec872006-06-27 21:08:10 +00005856 act._sa_handler = old_act->_sa_handler;
5857 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5858 act.sa_flags = old_act->sa_flags;
5859 unlock_user_struct(old_act, arg2, 0);
5860 pact = &act;
5861 } else {
5862 pact = NULL;
5863 }
5864
5865 ret = get_errno(do_sigaction(arg1, pact, &oact));
5866
5867 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005868 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5869 goto efault;
bellard106ec872006-06-27 21:08:10 +00005870 old_act->_sa_handler = oact._sa_handler;
5871 old_act->sa_flags = oact.sa_flags;
5872 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5873 old_act->sa_mask.sig[1] = 0;
5874 old_act->sa_mask.sig[2] = 0;
5875 old_act->sa_mask.sig[3] = 0;
5876 unlock_user_struct(old_act, arg3, 1);
5877 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005878#else
5879 struct target_old_sigaction *old_act;
5880 struct target_sigaction act, oact, *pact;
5881 if (arg2) {
5882 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5883 goto efault;
5884 act._sa_handler = old_act->_sa_handler;
5885 target_siginitset(&act.sa_mask, old_act->sa_mask);
5886 act.sa_flags = old_act->sa_flags;
5887 act.sa_restorer = old_act->sa_restorer;
5888 unlock_user_struct(old_act, arg2, 0);
5889 pact = &act;
5890 } else {
5891 pact = NULL;
5892 }
5893 ret = get_errno(do_sigaction(arg1, pact, &oact));
5894 if (!is_error(ret) && arg3) {
5895 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5896 goto efault;
5897 old_act->_sa_handler = oact._sa_handler;
5898 old_act->sa_mask = oact.sa_mask.sig[0];
5899 old_act->sa_flags = oact.sa_flags;
5900 old_act->sa_restorer = oact.sa_restorer;
5901 unlock_user_struct(old_act, arg3, 1);
5902 }
ths388bb212007-05-13 13:58:00 +00005903#endif
bellard31e31b82003-02-18 22:55:36 +00005904 }
5905 break;
thse5febef2007-04-01 18:31:35 +00005906#endif
bellard66fb9762003-03-23 01:06:05 +00005907 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00005908 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005909#if defined(TARGET_ALPHA)
5910 struct target_sigaction act, oact, *pact = 0;
5911 struct target_rt_sigaction *rt_act;
5912 /* ??? arg4 == sizeof(sigset_t). */
5913 if (arg2) {
5914 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5915 goto efault;
5916 act._sa_handler = rt_act->_sa_handler;
5917 act.sa_mask = rt_act->sa_mask;
5918 act.sa_flags = rt_act->sa_flags;
5919 act.sa_restorer = arg5;
5920 unlock_user_struct(rt_act, arg2, 0);
5921 pact = &act;
5922 }
5923 ret = get_errno(do_sigaction(arg1, pact, &oact));
5924 if (!is_error(ret) && arg3) {
5925 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5926 goto efault;
5927 rt_act->_sa_handler = oact._sa_handler;
5928 rt_act->sa_mask = oact.sa_mask;
5929 rt_act->sa_flags = oact.sa_flags;
5930 unlock_user_struct(rt_act, arg3, 1);
5931 }
5932#else
pbrook53a59602006-03-25 19:31:22 +00005933 struct target_sigaction *act;
5934 struct target_sigaction *oact;
5935
bellard579a97f2007-11-11 14:26:47 +00005936 if (arg2) {
5937 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5938 goto efault;
5939 } else
pbrook53a59602006-03-25 19:31:22 +00005940 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005941 if (arg3) {
5942 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5943 ret = -TARGET_EFAULT;
5944 goto rt_sigaction_fail;
5945 }
5946 } else
pbrook53a59602006-03-25 19:31:22 +00005947 oact = NULL;
5948 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005949 rt_sigaction_fail:
5950 if (act)
pbrook53a59602006-03-25 19:31:22 +00005951 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005952 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005953 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005954#endif
pbrook53a59602006-03-25 19:31:22 +00005955 }
bellard66fb9762003-03-23 01:06:05 +00005956 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005957#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005958 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005959 {
5960 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005961 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005962 sigprocmask(0, NULL, &cur_set);
5963 host_to_target_old_sigset(&target_set, &cur_set);
5964 ret = target_set;
5965 }
5966 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005967#endif
5968#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005969 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005970 {
5971 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005972 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00005973 sigprocmask(0, NULL, &cur_set);
5974 target_to_host_old_sigset(&set, &target_set);
5975 sigorset(&set, &set, &cur_set);
5976 sigprocmask(SIG_SETMASK, &set, &oset);
5977 host_to_target_old_sigset(&target_set, &oset);
5978 ret = target_set;
5979 }
5980 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005981#endif
thse5febef2007-04-01 18:31:35 +00005982#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00005983 case TARGET_NR_sigprocmask:
5984 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005985#if defined(TARGET_ALPHA)
5986 sigset_t set, oldset;
5987 abi_ulong mask;
5988 int how;
5989
5990 switch (arg1) {
5991 case TARGET_SIG_BLOCK:
5992 how = SIG_BLOCK;
5993 break;
5994 case TARGET_SIG_UNBLOCK:
5995 how = SIG_UNBLOCK;
5996 break;
5997 case TARGET_SIG_SETMASK:
5998 how = SIG_SETMASK;
5999 break;
6000 default:
6001 ret = -TARGET_EINVAL;
6002 goto fail;
6003 }
6004 mask = arg2;
6005 target_to_host_old_sigset(&set, &mask);
6006
6007 ret = get_errno(sigprocmask(how, &set, &oldset));
Richard Hendersona5b3b132010-05-03 10:07:55 -07006008 if (!is_error(ret)) {
6009 host_to_target_old_sigset(&mask, &oldset);
6010 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07006011 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07006012 }
6013#else
bellard66fb9762003-03-23 01:06:05 +00006014 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07006015 int how;
ths3b46e622007-09-17 08:09:54 +00006016
pbrook53a59602006-03-25 19:31:22 +00006017 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07006018 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00006019 case TARGET_SIG_BLOCK:
6020 how = SIG_BLOCK;
6021 break;
6022 case TARGET_SIG_UNBLOCK:
6023 how = SIG_UNBLOCK;
6024 break;
6025 case TARGET_SIG_SETMASK:
6026 how = SIG_SETMASK;
6027 break;
6028 default:
ths0da46a62007-10-20 20:23:07 +00006029 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006030 goto fail;
6031 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006032 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006033 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006034 target_to_host_old_sigset(&set, p);
6035 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006036 set_ptr = &set;
6037 } else {
6038 how = 0;
6039 set_ptr = NULL;
6040 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07006041 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006042 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006043 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006044 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006045 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006046 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006047 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07006048#endif
bellard66fb9762003-03-23 01:06:05 +00006049 }
6050 break;
thse5febef2007-04-01 18:31:35 +00006051#endif
bellard66fb9762003-03-23 01:06:05 +00006052 case TARGET_NR_rt_sigprocmask:
6053 {
6054 int how = arg1;
6055 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00006056
pbrook53a59602006-03-25 19:31:22 +00006057 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006058 switch(how) {
6059 case TARGET_SIG_BLOCK:
6060 how = SIG_BLOCK;
6061 break;
6062 case TARGET_SIG_UNBLOCK:
6063 how = SIG_UNBLOCK;
6064 break;
6065 case TARGET_SIG_SETMASK:
6066 how = SIG_SETMASK;
6067 break;
6068 default:
ths0da46a62007-10-20 20:23:07 +00006069 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006070 goto fail;
6071 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006072 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006073 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006074 target_to_host_sigset(&set, p);
6075 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006076 set_ptr = &set;
6077 } else {
6078 how = 0;
6079 set_ptr = NULL;
6080 }
6081 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006082 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006083 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006084 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006085 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006086 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006087 }
6088 }
6089 break;
thse5febef2007-04-01 18:31:35 +00006090#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00006091 case TARGET_NR_sigpending:
6092 {
6093 sigset_t set;
6094 ret = get_errno(sigpending(&set));
6095 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006096 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006097 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006098 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006099 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006100 }
6101 }
6102 break;
thse5febef2007-04-01 18:31:35 +00006103#endif
bellard66fb9762003-03-23 01:06:05 +00006104 case TARGET_NR_rt_sigpending:
6105 {
6106 sigset_t set;
6107 ret = get_errno(sigpending(&set));
6108 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006109 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006110 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006111 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006112 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006113 }
6114 }
6115 break;
thse5febef2007-04-01 18:31:35 +00006116#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00006117 case TARGET_NR_sigsuspend:
6118 {
6119 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07006120#if defined(TARGET_ALPHA)
6121 abi_ulong mask = arg1;
6122 target_to_host_old_sigset(&set, &mask);
6123#else
Anthony Liguoric227f092009-10-01 16:12:16 -05006124 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006125 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006126 target_to_host_old_sigset(&set, p);
6127 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07006128#endif
bellard66fb9762003-03-23 01:06:05 +00006129 ret = get_errno(sigsuspend(&set));
6130 }
6131 break;
thse5febef2007-04-01 18:31:35 +00006132#endif
bellard66fb9762003-03-23 01:06:05 +00006133 case TARGET_NR_rt_sigsuspend:
6134 {
6135 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05006136 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006137 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006138 target_to_host_sigset(&set, p);
6139 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006140 ret = get_errno(sigsuspend(&set));
6141 }
6142 break;
6143 case TARGET_NR_rt_sigtimedwait:
6144 {
bellard66fb9762003-03-23 01:06:05 +00006145 sigset_t set;
6146 struct timespec uts, *puts;
6147 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00006148
Anthony Liguoric227f092009-10-01 16:12:16 -05006149 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006150 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006151 target_to_host_sigset(&set, p);
6152 unlock_user(p, arg1, 0);
6153 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00006154 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00006155 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00006156 } else {
6157 puts = NULL;
6158 }
6159 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00006160 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006161 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006162 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006163 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05006164 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00006165 }
6166 }
6167 break;
6168 case TARGET_NR_rt_sigqueueinfo:
6169 {
6170 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05006171 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006172 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006173 target_to_host_siginfo(&uinfo, p);
6174 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006175 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6176 }
6177 break;
thse5febef2007-04-01 18:31:35 +00006178#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00006179 case TARGET_NR_sigreturn:
6180 /* NOTE: ret is eax, so not transcoding must be done */
6181 ret = do_sigreturn(cpu_env);
6182 break;
thse5febef2007-04-01 18:31:35 +00006183#endif
bellard66fb9762003-03-23 01:06:05 +00006184 case TARGET_NR_rt_sigreturn:
6185 /* NOTE: ret is eax, so not transcoding must be done */
6186 ret = do_rt_sigreturn(cpu_env);
6187 break;
bellard31e31b82003-02-18 22:55:36 +00006188 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00006189 if (!(p = lock_user_string(arg1)))
6190 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006191 ret = get_errno(sethostname(p, arg2));
6192 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006193 break;
6194 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00006195 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006196 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006197 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006198 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00006199 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6200 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006201 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6202 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006203 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00006204 ret = get_errno(setrlimit(resource, &rlim));
6205 }
6206 break;
bellard31e31b82003-02-18 22:55:36 +00006207 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00006208 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006209 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006210 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006211 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00006212
bellard9de5e442003-03-23 16:49:39 +00006213 ret = get_errno(getrlimit(resource, &rlim));
6214 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006215 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6216 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006217 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6218 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006219 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00006220 }
6221 }
6222 break;
bellard31e31b82003-02-18 22:55:36 +00006223 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00006224 {
6225 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00006226 ret = get_errno(getrusage(arg1, &rusage));
6227 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006228 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00006229 }
6230 }
6231 break;
bellard31e31b82003-02-18 22:55:36 +00006232 case TARGET_NR_gettimeofday:
6233 {
bellard31e31b82003-02-18 22:55:36 +00006234 struct timeval tv;
6235 ret = get_errno(gettimeofday(&tv, NULL));
6236 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00006237 if (copy_to_user_timeval(arg1, &tv))
6238 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006239 }
6240 }
6241 break;
6242 case TARGET_NR_settimeofday:
6243 {
bellard31e31b82003-02-18 22:55:36 +00006244 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00006245 if (copy_from_user_timeval(&tv, arg1))
6246 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006247 ret = get_errno(settimeofday(&tv, NULL));
6248 }
6249 break;
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006250#if defined(TARGET_NR_select)
bellard31e31b82003-02-18 22:55:36 +00006251 case TARGET_NR_select:
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006252#if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6253 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6254#else
bellardf2674e32003-07-09 12:26:09 +00006255 {
pbrook53a59602006-03-25 19:31:22 +00006256 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00006257 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00006258 long nsel;
6259
bellard579a97f2007-11-11 14:26:47 +00006260 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6261 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006262 nsel = tswapal(sel->n);
6263 inp = tswapal(sel->inp);
6264 outp = tswapal(sel->outp);
6265 exp = tswapal(sel->exp);
6266 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00006267 unlock_user_struct(sel, arg1, 0);
6268 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00006269 }
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006270#endif
bellardf2674e32003-07-09 12:26:09 +00006271 break;
bellard048f6b42005-11-26 18:47:20 +00006272#endif
Riku Voipio9e423822010-05-07 12:28:05 +00006273#ifdef TARGET_NR_pselect6
6274 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04006275 {
6276 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6277 fd_set rfds, wfds, efds;
6278 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6279 struct timespec ts, *ts_ptr;
6280
6281 /*
6282 * The 6th arg is actually two args smashed together,
6283 * so we cannot use the C library.
6284 */
6285 sigset_t set;
6286 struct {
6287 sigset_t *set;
6288 size_t size;
6289 } sig, *sig_ptr;
6290
6291 abi_ulong arg_sigset, arg_sigsize, *arg7;
6292 target_sigset_t *target_sigset;
6293
6294 n = arg1;
6295 rfd_addr = arg2;
6296 wfd_addr = arg3;
6297 efd_addr = arg4;
6298 ts_addr = arg5;
6299
6300 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6301 if (ret) {
6302 goto fail;
6303 }
6304 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6305 if (ret) {
6306 goto fail;
6307 }
6308 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6309 if (ret) {
6310 goto fail;
6311 }
6312
6313 /*
6314 * This takes a timespec, and not a timeval, so we cannot
6315 * use the do_select() helper ...
6316 */
6317 if (ts_addr) {
6318 if (target_to_host_timespec(&ts, ts_addr)) {
6319 goto efault;
6320 }
6321 ts_ptr = &ts;
6322 } else {
6323 ts_ptr = NULL;
6324 }
6325
6326 /* Extract the two packed args for the sigset */
6327 if (arg6) {
6328 sig_ptr = &sig;
6329 sig.size = _NSIG / 8;
6330
6331 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6332 if (!arg7) {
6333 goto efault;
6334 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006335 arg_sigset = tswapal(arg7[0]);
6336 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04006337 unlock_user(arg7, arg6, 0);
6338
6339 if (arg_sigset) {
6340 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01006341 if (arg_sigsize != sizeof(*target_sigset)) {
6342 /* Like the kernel, we enforce correct size sigsets */
6343 ret = -TARGET_EINVAL;
6344 goto fail;
6345 }
Mike Frysinger055e0902011-06-03 17:01:49 -04006346 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6347 sizeof(*target_sigset), 1);
6348 if (!target_sigset) {
6349 goto efault;
6350 }
6351 target_to_host_sigset(&set, target_sigset);
6352 unlock_user(target_sigset, arg_sigset, 0);
6353 } else {
6354 sig.set = NULL;
6355 }
6356 } else {
6357 sig_ptr = NULL;
6358 }
6359
6360 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6361 ts_ptr, sig_ptr));
6362
6363 if (!is_error(ret)) {
6364 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6365 goto efault;
6366 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6367 goto efault;
6368 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6369 goto efault;
6370
6371 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6372 goto efault;
6373 }
6374 }
6375 break;
Riku Voipio9e423822010-05-07 12:28:05 +00006376#endif
bellard31e31b82003-02-18 22:55:36 +00006377 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00006378 {
6379 void *p2;
6380 p = lock_user_string(arg1);
6381 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006382 if (!p || !p2)
6383 ret = -TARGET_EFAULT;
6384 else
6385 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006386 unlock_user(p2, arg2, 0);
6387 unlock_user(p, arg1, 0);
6388 }
bellard31e31b82003-02-18 22:55:36 +00006389 break;
thsf0b62432007-09-24 09:25:40 +00006390#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
6391 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00006392 {
bellard579a97f2007-11-11 14:26:47 +00006393 void *p2;
thsf0b62432007-09-24 09:25:40 +00006394 p = lock_user_string(arg1);
6395 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00006396 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006397 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00006398 else
6399 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00006400 unlock_user(p2, arg3, 0);
6401 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00006402 }
6403 break;
6404#endif
bellardebc05482003-09-30 21:08:41 +00006405#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00006406 case TARGET_NR_oldlstat:
6407 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006408#endif
bellard31e31b82003-02-18 22:55:36 +00006409 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00006410 {
aurel32d088d662009-01-30 20:09:01 +00006411 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00006412 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00006413 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
6414 if (!p || !p2)
6415 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00006416 else {
6417 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
6418 char real[PATH_MAX];
6419 temp = realpath(exec_path,real);
6420 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
6421 snprintf((char *)p2, arg3, "%s", real);
6422 }
6423 else
6424 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00006425 }
pbrook53a59602006-03-25 19:31:22 +00006426 unlock_user(p2, arg2, ret);
6427 unlock_user(p, arg1, 0);
6428 }
bellard31e31b82003-02-18 22:55:36 +00006429 break;
ths5e0ccb12007-09-24 09:26:10 +00006430#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
6431 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00006432 {
bellard579a97f2007-11-11 14:26:47 +00006433 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00006434 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006435 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
6436 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006437 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00006438 else
6439 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00006440 unlock_user(p2, arg3, ret);
6441 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00006442 }
6443 break;
6444#endif
thse5febef2007-04-01 18:31:35 +00006445#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00006446 case TARGET_NR_uselib:
6447 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006448#endif
6449#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00006450 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00006451 if (!(p = lock_user_string(arg1)))
6452 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006453 ret = get_errno(swapon(p, arg2));
6454 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006455 break;
thse5febef2007-04-01 18:31:35 +00006456#endif
bellard31e31b82003-02-18 22:55:36 +00006457 case TARGET_NR_reboot:
Laurent Vivierc07ecc62013-01-07 11:40:06 +00006458 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
6459 /* arg4 must be ignored in all other cases */
6460 p = lock_user_string(arg4);
6461 if (!p) {
6462 goto efault;
6463 }
6464 ret = get_errno(reboot(arg1, arg2, arg3, p));
6465 unlock_user(p, arg4, 0);
6466 } else {
6467 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
6468 }
Alexander Graf0f6b4d22011-09-27 14:39:42 +02006469 break;
thse5febef2007-04-01 18:31:35 +00006470#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00006471 case TARGET_NR_readdir:
6472 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006473#endif
6474#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00006475 case TARGET_NR_mmap:
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006476#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || \
6477 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6478 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00006479 {
blueswir1992f48a2007-10-14 16:27:31 +00006480 abi_ulong *v;
6481 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00006482 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6483 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006484 v1 = tswapal(v[0]);
6485 v2 = tswapal(v[1]);
6486 v3 = tswapal(v[2]);
6487 v4 = tswapal(v[3]);
6488 v5 = tswapal(v[4]);
6489 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00006490 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00006491 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00006492 target_to_host_bitmask(v4, mmap_flags_tbl),
6493 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00006494 }
bellard31e31b82003-02-18 22:55:36 +00006495#else
ths5fafdf22007-09-16 21:08:06 +00006496 ret = get_errno(target_mmap(arg1, arg2, arg3,
6497 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00006498 arg5,
6499 arg6));
bellard31e31b82003-02-18 22:55:36 +00006500#endif
bellard6fb883e2003-07-09 17:12:39 +00006501 break;
thse5febef2007-04-01 18:31:35 +00006502#endif
bellarda315a142005-01-30 22:59:18 +00006503#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00006504 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00006505#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00006506#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00006507#endif
ths5fafdf22007-09-16 21:08:06 +00006508 ret = get_errno(target_mmap(arg1, arg2, arg3,
6509 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00006510 arg5,
bellardc573ff62004-01-04 15:51:36 +00006511 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00006512 break;
bellarda315a142005-01-30 22:59:18 +00006513#endif
bellard31e31b82003-02-18 22:55:36 +00006514 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00006515 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006516 break;
bellard9de5e442003-03-23 16:49:39 +00006517 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01006518 {
Andreas Färber9349b4f2012-03-14 01:38:32 +01006519 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Paul Brook97374d32010-06-16 13:03:51 +01006520 /* Special hack to detect libc making the stack executable. */
6521 if ((arg3 & PROT_GROWSDOWN)
6522 && arg1 >= ts->info->stack_limit
6523 && arg1 <= ts->info->start_stack) {
6524 arg3 &= ~PROT_GROWSDOWN;
6525 arg2 = arg2 + arg1 - ts->info->stack_limit;
6526 arg1 = ts->info->stack_limit;
6527 }
6528 }
bellard54936002003-05-13 00:25:15 +00006529 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006530 break;
thse5febef2007-04-01 18:31:35 +00006531#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00006532 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00006533 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00006534 break;
thse5febef2007-04-01 18:31:35 +00006535#endif
pbrook53a59602006-03-25 19:31:22 +00006536 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00006537#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00006538 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00006539 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006540 break;
thse5febef2007-04-01 18:31:35 +00006541#endif
6542#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00006543 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00006544 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006545 break;
thse5febef2007-04-01 18:31:35 +00006546#endif
6547#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00006548 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00006549 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006550 break;
thse5febef2007-04-01 18:31:35 +00006551#endif
6552#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00006553 case TARGET_NR_mlockall:
6554 ret = get_errno(mlockall(arg1));
6555 break;
thse5febef2007-04-01 18:31:35 +00006556#endif
6557#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00006558 case TARGET_NR_munlockall:
6559 ret = get_errno(munlockall());
6560 break;
thse5febef2007-04-01 18:31:35 +00006561#endif
bellard31e31b82003-02-18 22:55:36 +00006562 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00006563 if (!(p = lock_user_string(arg1)))
6564 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006565 ret = get_errno(truncate(p, arg2));
6566 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006567 break;
6568 case TARGET_NR_ftruncate:
6569 ret = get_errno(ftruncate(arg1, arg2));
6570 break;
6571 case TARGET_NR_fchmod:
6572 ret = get_errno(fchmod(arg1, arg2));
6573 break;
ths814d7972007-09-24 09:26:51 +00006574#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
6575 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00006576 if (!(p = lock_user_string(arg2)))
6577 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00006578 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00006579 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00006580 break;
6581#endif
bellard31e31b82003-02-18 22:55:36 +00006582 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -07006583 /* Note that negative values are valid for getpriority, so we must
6584 differentiate based on errno settings. */
6585 errno = 0;
6586 ret = getpriority(arg1, arg2);
6587 if (ret == -1 && errno != 0) {
6588 ret = -host_to_target_errno(errno);
6589 break;
6590 }
6591#ifdef TARGET_ALPHA
6592 /* Return value is the unbiased priority. Signal no error. */
6593 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
6594#else
6595 /* Return value is a biased priority to avoid negative numbers. */
6596 ret = 20 - ret;
6597#endif
bellard31e31b82003-02-18 22:55:36 +00006598 break;
6599 case TARGET_NR_setpriority:
6600 ret = get_errno(setpriority(arg1, arg2, arg3));
6601 break;
bellardebc05482003-09-30 21:08:41 +00006602#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00006603 case TARGET_NR_profil:
6604 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006605#endif
bellard31e31b82003-02-18 22:55:36 +00006606 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00006607 if (!(p = lock_user_string(arg1)))
6608 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006609 ret = get_errno(statfs(path(p), &stfs));
6610 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006611 convert_statfs:
6612 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006613 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006614
bellard579a97f2007-11-11 14:26:47 +00006615 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6616 goto efault;
6617 __put_user(stfs.f_type, &target_stfs->f_type);
6618 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6619 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6620 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6621 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6622 __put_user(stfs.f_files, &target_stfs->f_files);
6623 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6624 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6625 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6626 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02006627 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6628 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +00006629 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006630 }
6631 break;
6632 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00006633 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00006634 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00006635#ifdef TARGET_NR_statfs64
6636 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00006637 if (!(p = lock_user_string(arg1)))
6638 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006639 ret = get_errno(statfs(path(p), &stfs));
6640 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00006641 convert_statfs64:
6642 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006643 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006644
bellard579a97f2007-11-11 14:26:47 +00006645 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6646 goto efault;
6647 __put_user(stfs.f_type, &target_stfs->f_type);
6648 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6649 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6650 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6651 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6652 __put_user(stfs.f_files, &target_stfs->f_files);
6653 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6654 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6655 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6656 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02006657 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6658 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +00006659 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00006660 }
6661 break;
6662 case TARGET_NR_fstatfs64:
6663 ret = get_errno(fstatfs(arg1, &stfs));
6664 goto convert_statfs64;
6665#endif
bellardebc05482003-09-30 21:08:41 +00006666#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00006667 case TARGET_NR_ioperm:
6668 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006669#endif
thse5febef2007-04-01 18:31:35 +00006670#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00006671 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00006672 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00006673 break;
thse5febef2007-04-01 18:31:35 +00006674#endif
bellard3532fa72006-06-24 15:06:03 +00006675#ifdef TARGET_NR_accept
6676 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00006677 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006678 break;
6679#endif
6680#ifdef TARGET_NR_bind
6681 case TARGET_NR_bind:
6682 ret = do_bind(arg1, arg2, arg3);
6683 break;
6684#endif
6685#ifdef TARGET_NR_connect
6686 case TARGET_NR_connect:
6687 ret = do_connect(arg1, arg2, arg3);
6688 break;
6689#endif
6690#ifdef TARGET_NR_getpeername
6691 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00006692 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006693 break;
6694#endif
6695#ifdef TARGET_NR_getsockname
6696 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00006697 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006698 break;
6699#endif
6700#ifdef TARGET_NR_getsockopt
6701 case TARGET_NR_getsockopt:
6702 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6703 break;
6704#endif
6705#ifdef TARGET_NR_listen
6706 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00006707 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006708 break;
6709#endif
6710#ifdef TARGET_NR_recv
6711 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00006712 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006713 break;
6714#endif
6715#ifdef TARGET_NR_recvfrom
6716 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00006717 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006718 break;
6719#endif
6720#ifdef TARGET_NR_recvmsg
6721 case TARGET_NR_recvmsg:
6722 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6723 break;
6724#endif
6725#ifdef TARGET_NR_send
6726 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00006727 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006728 break;
6729#endif
6730#ifdef TARGET_NR_sendmsg
6731 case TARGET_NR_sendmsg:
6732 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6733 break;
6734#endif
6735#ifdef TARGET_NR_sendto
6736 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00006737 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006738 break;
6739#endif
6740#ifdef TARGET_NR_shutdown
6741 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00006742 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006743 break;
6744#endif
6745#ifdef TARGET_NR_socket
6746 case TARGET_NR_socket:
6747 ret = do_socket(arg1, arg2, arg3);
6748 break;
6749#endif
6750#ifdef TARGET_NR_socketpair
6751 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00006752 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00006753 break;
6754#endif
6755#ifdef TARGET_NR_setsockopt
6756 case TARGET_NR_setsockopt:
6757 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6758 break;
6759#endif
ths7494b0f2007-02-11 18:26:53 +00006760
bellard31e31b82003-02-18 22:55:36 +00006761 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00006762 if (!(p = lock_user_string(arg2)))
6763 goto efault;
thse5574482007-02-11 20:03:13 +00006764 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6765 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00006766 break;
6767
bellard31e31b82003-02-18 22:55:36 +00006768 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00006769 {
bellard66fb9762003-03-23 01:06:05 +00006770 struct itimerval value, ovalue, *pvalue;
6771
pbrook53a59602006-03-25 19:31:22 +00006772 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006773 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00006774 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6775 || copy_from_user_timeval(&pvalue->it_value,
6776 arg2 + sizeof(struct target_timeval)))
6777 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006778 } else {
6779 pvalue = NULL;
6780 }
6781 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00006782 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00006783 if (copy_to_user_timeval(arg3,
6784 &ovalue.it_interval)
6785 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6786 &ovalue.it_value))
6787 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006788 }
6789 }
6790 break;
bellard31e31b82003-02-18 22:55:36 +00006791 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00006792 {
bellard66fb9762003-03-23 01:06:05 +00006793 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00006794
bellard66fb9762003-03-23 01:06:05 +00006795 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00006796 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00006797 if (copy_to_user_timeval(arg2,
6798 &value.it_interval)
6799 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6800 &value.it_value))
6801 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006802 }
6803 }
6804 break;
bellard31e31b82003-02-18 22:55:36 +00006805 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00006806 if (!(p = lock_user_string(arg1)))
6807 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006808 ret = get_errno(stat(path(p), &st));
6809 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006810 goto do_stat;
6811 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00006812 if (!(p = lock_user_string(arg1)))
6813 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006814 ret = get_errno(lstat(path(p), &st));
6815 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006816 goto do_stat;
6817 case TARGET_NR_fstat:
6818 {
6819 ret = get_errno(fstat(arg1, &st));
6820 do_stat:
6821 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006822 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00006823
bellard579a97f2007-11-11 14:26:47 +00006824 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6825 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02006826 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00006827 __put_user(st.st_dev, &target_st->st_dev);
6828 __put_user(st.st_ino, &target_st->st_ino);
6829 __put_user(st.st_mode, &target_st->st_mode);
6830 __put_user(st.st_uid, &target_st->st_uid);
6831 __put_user(st.st_gid, &target_st->st_gid);
6832 __put_user(st.st_nlink, &target_st->st_nlink);
6833 __put_user(st.st_rdev, &target_st->st_rdev);
6834 __put_user(st.st_size, &target_st->st_size);
6835 __put_user(st.st_blksize, &target_st->st_blksize);
6836 __put_user(st.st_blocks, &target_st->st_blocks);
6837 __put_user(st.st_atime, &target_st->target_st_atime);
6838 __put_user(st.st_mtime, &target_st->target_st_mtime);
6839 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00006840 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006841 }
6842 }
6843 break;
bellardebc05482003-09-30 21:08:41 +00006844#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00006845 case TARGET_NR_olduname:
6846 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006847#endif
6848#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00006849 case TARGET_NR_iopl:
6850 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006851#endif
bellard31e31b82003-02-18 22:55:36 +00006852 case TARGET_NR_vhangup:
6853 ret = get_errno(vhangup());
6854 break;
bellardebc05482003-09-30 21:08:41 +00006855#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00006856 case TARGET_NR_idle:
6857 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006858#endif
bellard42ad6ae2005-01-03 22:48:11 +00006859#ifdef TARGET_NR_syscall
6860 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01006861 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6862 arg6, arg7, arg8, 0);
6863 break;
bellard42ad6ae2005-01-03 22:48:11 +00006864#endif
bellard31e31b82003-02-18 22:55:36 +00006865 case TARGET_NR_wait4:
6866 {
6867 int status;
blueswir1992f48a2007-10-14 16:27:31 +00006868 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00006869 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00006870 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00006871 if (target_rusage)
6872 rusage_ptr = &rusage;
6873 else
6874 rusage_ptr = NULL;
6875 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6876 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01006877 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00006878 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00006879 if (put_user_s32(status, status_ptr))
6880 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006881 }
bellard2f619692007-11-16 10:46:05 +00006882 if (target_rusage)
6883 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00006884 }
6885 }
6886 break;
thse5febef2007-04-01 18:31:35 +00006887#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00006888 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00006889 if (!(p = lock_user_string(arg1)))
6890 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006891 ret = get_errno(swapoff(p));
6892 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006893 break;
thse5febef2007-04-01 18:31:35 +00006894#endif
bellard31e31b82003-02-18 22:55:36 +00006895 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00006896 {
pbrook53a59602006-03-25 19:31:22 +00006897 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00006898 struct sysinfo value;
6899 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00006900 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00006901 {
bellard579a97f2007-11-11 14:26:47 +00006902 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6903 goto efault;
bellarda5448a72004-06-19 16:59:03 +00006904 __put_user(value.uptime, &target_value->uptime);
6905 __put_user(value.loads[0], &target_value->loads[0]);
6906 __put_user(value.loads[1], &target_value->loads[1]);
6907 __put_user(value.loads[2], &target_value->loads[2]);
6908 __put_user(value.totalram, &target_value->totalram);
6909 __put_user(value.freeram, &target_value->freeram);
6910 __put_user(value.sharedram, &target_value->sharedram);
6911 __put_user(value.bufferram, &target_value->bufferram);
6912 __put_user(value.totalswap, &target_value->totalswap);
6913 __put_user(value.freeswap, &target_value->freeswap);
6914 __put_user(value.procs, &target_value->procs);
6915 __put_user(value.totalhigh, &target_value->totalhigh);
6916 __put_user(value.freehigh, &target_value->freehigh);
6917 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00006918 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00006919 }
6920 }
6921 break;
thse5febef2007-04-01 18:31:35 +00006922#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00006923 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00006924 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6925 break;
thse5febef2007-04-01 18:31:35 +00006926#endif
aurel32e5289082009-04-18 16:16:12 +00006927#ifdef TARGET_NR_semget
6928 case TARGET_NR_semget:
6929 ret = get_errno(semget(arg1, arg2, arg3));
6930 break;
6931#endif
6932#ifdef TARGET_NR_semop
6933 case TARGET_NR_semop:
6934 ret = get_errno(do_semop(arg1, arg2, arg3));
6935 break;
6936#endif
6937#ifdef TARGET_NR_semctl
6938 case TARGET_NR_semctl:
6939 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6940 break;
6941#endif
aurel32eeb438c2008-10-13 21:08:55 +00006942#ifdef TARGET_NR_msgctl
6943 case TARGET_NR_msgctl:
6944 ret = do_msgctl(arg1, arg2, arg3);
6945 break;
6946#endif
6947#ifdef TARGET_NR_msgget
6948 case TARGET_NR_msgget:
6949 ret = get_errno(msgget(arg1, arg2));
6950 break;
6951#endif
6952#ifdef TARGET_NR_msgrcv
6953 case TARGET_NR_msgrcv:
6954 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
6955 break;
6956#endif
6957#ifdef TARGET_NR_msgsnd
6958 case TARGET_NR_msgsnd:
6959 ret = do_msgsnd(arg1, arg2, arg3, arg4);
6960 break;
6961#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03006962#ifdef TARGET_NR_shmget
6963 case TARGET_NR_shmget:
6964 ret = get_errno(shmget(arg1, arg2, arg3));
6965 break;
6966#endif
6967#ifdef TARGET_NR_shmctl
6968 case TARGET_NR_shmctl:
6969 ret = do_shmctl(arg1, arg2, arg3);
6970 break;
6971#endif
6972#ifdef TARGET_NR_shmat
6973 case TARGET_NR_shmat:
6974 ret = do_shmat(arg1, arg2, arg3);
6975 break;
6976#endif
6977#ifdef TARGET_NR_shmdt
6978 case TARGET_NR_shmdt:
6979 ret = do_shmdt(arg1);
6980 break;
6981#endif
bellard31e31b82003-02-18 22:55:36 +00006982 case TARGET_NR_fsync:
6983 ret = get_errno(fsync(arg1));
6984 break;
bellard31e31b82003-02-18 22:55:36 +00006985 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07006986#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00006987 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00006988#elif defined(TARGET_CRIS)
6989 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
Edgar E. Iglesiasa5b3bdc2012-04-26 14:17:41 +02006990#elif defined(TARGET_MICROBLAZE)
6991 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006992#elif defined(TARGET_S390X)
6993 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00006994#else
pbrookd865bab2008-06-07 22:12:17 +00006995 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006996#endif
bellard1b6b0292003-03-22 17:31:38 +00006997 break;
bellardec86b0f2003-04-11 00:15:04 +00006998#ifdef __NR_exit_group
6999 /* new thread calls */
7000 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02007001#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00007002 _mcleanup();
7003#endif
bellarde9009672005-04-26 20:42:36 +00007004 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00007005 ret = get_errno(exit_group(arg1));
7006 break;
7007#endif
bellard31e31b82003-02-18 22:55:36 +00007008 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00007009 if (!(p = lock_user_string(arg1)))
7010 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007011 ret = get_errno(setdomainname(p, arg2));
7012 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007013 break;
7014 case TARGET_NR_uname:
7015 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00007016 {
7017 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00007018
bellard579a97f2007-11-11 14:26:47 +00007019 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
7020 goto efault;
bellard29e619b2004-09-13 21:41:04 +00007021 ret = get_errno(sys_uname(buf));
7022 if (!is_error(ret)) {
7023 /* Overrite the native machine name with whatever is being
7024 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01007025 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00007026 /* Allow the user to override the reported release. */
7027 if (qemu_uname_release && *qemu_uname_release)
7028 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00007029 }
pbrook53a59602006-03-25 19:31:22 +00007030 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00007031 }
bellard31e31b82003-02-18 22:55:36 +00007032 break;
bellard6dbad632003-03-16 18:05:05 +00007033#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00007034 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00007035 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00007036 break;
j_mayer84409dd2007-04-06 08:56:50 +00007037#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00007038 case TARGET_NR_vm86old:
7039 goto unimplemented;
7040 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00007041 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00007042 break;
7043#endif
j_mayer84409dd2007-04-06 08:56:50 +00007044#endif
bellard31e31b82003-02-18 22:55:36 +00007045 case TARGET_NR_adjtimex:
7046 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007047#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00007048 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00007049#endif
bellard31e31b82003-02-18 22:55:36 +00007050 case TARGET_NR_init_module:
7051 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00007052#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00007053 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00007054#endif
bellard31e31b82003-02-18 22:55:36 +00007055 goto unimplemented;
7056 case TARGET_NR_quotactl:
7057 goto unimplemented;
7058 case TARGET_NR_getpgid:
7059 ret = get_errno(getpgid(arg1));
7060 break;
7061 case TARGET_NR_fchdir:
7062 ret = get_errno(fchdir(arg1));
7063 break;
j_mayer84409dd2007-04-06 08:56:50 +00007064#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00007065 case TARGET_NR_bdflush:
7066 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00007067#endif
thse5febef2007-04-01 18:31:35 +00007068#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00007069 case TARGET_NR_sysfs:
7070 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007071#endif
bellard31e31b82003-02-18 22:55:36 +00007072 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00007073 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00007074 break;
thse5febef2007-04-01 18:31:35 +00007075#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00007076 case TARGET_NR_afs_syscall:
7077 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007078#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007079#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007080 case TARGET_NR__llseek:
7081 {
7082 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00007083#if !defined(__NR_llseek)
7084 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7085 if (res == -1) {
7086 ret = get_errno(res);
7087 } else {
7088 ret = 0;
7089 }
7090#else
bellard31e31b82003-02-18 22:55:36 +00007091 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00007092#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00007093 if ((ret == 0) && put_user_s64(res, arg4)) {
7094 goto efault;
7095 }
bellard31e31b82003-02-18 22:55:36 +00007096 }
7097 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007098#endif
bellard31e31b82003-02-18 22:55:36 +00007099 case TARGET_NR_getdents:
Ulrich Hechtd83c8732009-07-24 19:10:28 +02007100#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00007101 {
pbrook53a59602006-03-25 19:31:22 +00007102 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00007103 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007104 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00007105
7106 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00007107 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00007108 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00007109 goto fail;
7110 }
ths3b46e622007-09-17 08:09:54 +00007111
bellard4add45b2003-06-05 01:52:59 +00007112 ret = get_errno(sys_getdents(arg1, dirp, count));
7113 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007114 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00007115 struct target_dirent *tde;
7116 int len = ret;
7117 int reclen, treclen;
7118 int count1, tnamelen;
7119
7120 count1 = 0;
7121 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00007122 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7123 goto efault;
bellard4add45b2003-06-05 01:52:59 +00007124 tde = target_dirp;
7125 while (len > 0) {
7126 reclen = de->d_reclen;
Dmitry V. Levin333858b2012-08-21 02:13:12 +04007127 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
7128 assert(tnamelen >= 0);
7129 treclen = tnamelen + offsetof(struct target_dirent, d_name);
7130 assert(count1 + treclen <= count);
bellard4add45b2003-06-05 01:52:59 +00007131 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007132 tde->d_ino = tswapal(de->d_ino);
7133 tde->d_off = tswapal(de->d_off);
Dmitry V. Levin333858b2012-08-21 02:13:12 +04007134 memcpy(tde->d_name, de->d_name, tnamelen);
aurel326556a832008-10-13 21:08:17 +00007135 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00007136 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00007137 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00007138 count1 += treclen;
7139 }
7140 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00007141 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00007142 }
7143 free(dirp);
7144 }
7145#else
bellard31e31b82003-02-18 22:55:36 +00007146 {
aurel326556a832008-10-13 21:08:17 +00007147 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007148 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00007149
bellard579a97f2007-11-11 14:26:47 +00007150 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7151 goto efault;
bellard72f03902003-02-18 23:33:18 +00007152 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00007153 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007154 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00007155 int len = ret;
7156 int reclen;
7157 de = dirp;
7158 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007159 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00007160 if (reclen > len)
7161 break;
bellard8083a3e2003-03-24 23:12:16 +00007162 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00007163 tswapls(&de->d_ino);
7164 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007165 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00007166 len -= reclen;
7167 }
7168 }
pbrook53a59602006-03-25 19:31:22 +00007169 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00007170 }
bellard4add45b2003-06-05 01:52:59 +00007171#endif
bellard31e31b82003-02-18 22:55:36 +00007172 break;
ths3ae43202007-09-16 21:39:48 +00007173#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00007174 case TARGET_NR_getdents64:
7175 {
aurel326556a832008-10-13 21:08:17 +00007176 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007177 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00007178 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7179 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00007180 ret = get_errno(sys_getdents64(arg1, dirp, count));
7181 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007182 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00007183 int len = ret;
7184 int reclen;
7185 de = dirp;
7186 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007187 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00007188 if (reclen > len)
7189 break;
bellard8083a3e2003-03-24 23:12:16 +00007190 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00007191 tswap64s((uint64_t *)&de->d_ino);
7192 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007193 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00007194 len -= reclen;
7195 }
7196 }
pbrook53a59602006-03-25 19:31:22 +00007197 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00007198 }
7199 break;
bellarda541f292004-04-12 20:39:29 +00007200#endif /* TARGET_NR_getdents64 */
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007201#if defined(TARGET_NR__newselect)
bellard31e31b82003-02-18 22:55:36 +00007202 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00007203 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00007204 break;
thse5febef2007-04-01 18:31:35 +00007205#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05007206#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7207# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00007208 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05007209# endif
7210# ifdef TARGET_NR_ppoll
7211 case TARGET_NR_ppoll:
7212# endif
bellard9de5e442003-03-23 16:49:39 +00007213 {
pbrook53a59602006-03-25 19:31:22 +00007214 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00007215 unsigned int nfds = arg2;
7216 int timeout = arg3;
7217 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00007218 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00007219
bellard579a97f2007-11-11 14:26:47 +00007220 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7221 if (!target_pfd)
7222 goto efault;
Mike Frysingerd8035d42011-02-07 01:05:51 -05007223
bellard9de5e442003-03-23 16:49:39 +00007224 pfd = alloca(sizeof(struct pollfd) * nfds);
7225 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007226 pfd[i].fd = tswap32(target_pfd[i].fd);
7227 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00007228 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05007229
7230# ifdef TARGET_NR_ppoll
7231 if (num == TARGET_NR_ppoll) {
7232 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7233 target_sigset_t *target_set;
7234 sigset_t _set, *set = &_set;
7235
7236 if (arg3) {
7237 if (target_to_host_timespec(timeout_ts, arg3)) {
7238 unlock_user(target_pfd, arg1, 0);
7239 goto efault;
7240 }
7241 } else {
7242 timeout_ts = NULL;
7243 }
7244
7245 if (arg4) {
7246 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7247 if (!target_set) {
7248 unlock_user(target_pfd, arg1, 0);
7249 goto efault;
7250 }
7251 target_to_host_sigset(set, target_set);
7252 } else {
7253 set = NULL;
7254 }
7255
7256 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7257
7258 if (!is_error(ret) && arg3) {
7259 host_to_target_timespec(arg3, timeout_ts);
7260 }
7261 if (arg4) {
7262 unlock_user(target_set, arg4, 0);
7263 }
7264 } else
7265# endif
7266 ret = get_errno(poll(pfd, nfds, timeout));
7267
bellard9de5e442003-03-23 16:49:39 +00007268 if (!is_error(ret)) {
7269 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007270 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00007271 }
7272 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00007273 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00007274 }
7275 break;
thse5febef2007-04-01 18:31:35 +00007276#endif
bellard31e31b82003-02-18 22:55:36 +00007277 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00007278 /* NOTE: the flock constant seems to be the same for every
7279 Linux platform */
7280 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00007281 break;
7282 case TARGET_NR_readv:
7283 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07007284 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
7285 if (vec != NULL) {
7286 ret = get_errno(readv(arg1, vec, arg3));
7287 unlock_iovec(vec, arg2, arg3, 1);
7288 } else {
7289 ret = -host_to_target_errno(errno);
7290 }
bellard31e31b82003-02-18 22:55:36 +00007291 }
7292 break;
7293 case TARGET_NR_writev:
7294 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07007295 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
7296 if (vec != NULL) {
7297 ret = get_errno(writev(arg1, vec, arg3));
7298 unlock_iovec(vec, arg2, arg3, 0);
7299 } else {
7300 ret = -host_to_target_errno(errno);
7301 }
bellard31e31b82003-02-18 22:55:36 +00007302 }
7303 break;
7304 case TARGET_NR_getsid:
7305 ret = get_errno(getsid(arg1));
7306 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007307#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00007308 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00007309 ret = get_errno(fdatasync(arg1));
7310 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007311#endif
bellard31e31b82003-02-18 22:55:36 +00007312 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00007313 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00007314 return value. */
ths0da46a62007-10-20 20:23:07 +00007315 ret = -TARGET_ENOTDIR;
7316 break;
Mike Frysinger737de1d2011-02-07 01:05:55 -05007317 case TARGET_NR_sched_getaffinity:
7318 {
7319 unsigned int mask_size;
7320 unsigned long *mask;
7321
7322 /*
7323 * sched_getaffinity needs multiples of ulong, so need to take
7324 * care of mismatches between target ulong and host ulong sizes.
7325 */
7326 if (arg2 & (sizeof(abi_ulong) - 1)) {
7327 ret = -TARGET_EINVAL;
7328 break;
7329 }
7330 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7331
7332 mask = alloca(mask_size);
7333 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7334
7335 if (!is_error(ret)) {
Mike McCormackcd18f052011-04-18 14:43:36 +09007336 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05007337 goto efault;
7338 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05007339 }
7340 }
7341 break;
7342 case TARGET_NR_sched_setaffinity:
7343 {
7344 unsigned int mask_size;
7345 unsigned long *mask;
7346
7347 /*
7348 * sched_setaffinity needs multiples of ulong, so need to take
7349 * care of mismatches between target ulong and host ulong sizes.
7350 */
7351 if (arg2 & (sizeof(abi_ulong) - 1)) {
7352 ret = -TARGET_EINVAL;
7353 break;
7354 }
7355 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7356
7357 mask = alloca(mask_size);
7358 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7359 goto efault;
7360 }
7361 memcpy(mask, p, arg2);
7362 unlock_user_struct(p, arg2, 0);
7363
7364 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7365 }
7366 break;
bellard31e31b82003-02-18 22:55:36 +00007367 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00007368 {
pbrook53a59602006-03-25 19:31:22 +00007369 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007370 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00007371
bellard579a97f2007-11-11 14:26:47 +00007372 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7373 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007374 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007375 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00007376 ret = get_errno(sched_setparam(arg1, &schp));
7377 }
7378 break;
bellard31e31b82003-02-18 22:55:36 +00007379 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00007380 {
pbrook53a59602006-03-25 19:31:22 +00007381 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007382 struct sched_param schp;
7383 ret = get_errno(sched_getparam(arg1, &schp));
7384 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007385 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7386 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007387 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007388 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00007389 }
7390 }
7391 break;
bellard31e31b82003-02-18 22:55:36 +00007392 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00007393 {
pbrook53a59602006-03-25 19:31:22 +00007394 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007395 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00007396 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7397 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007398 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007399 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00007400 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7401 }
7402 break;
bellard31e31b82003-02-18 22:55:36 +00007403 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00007404 ret = get_errno(sched_getscheduler(arg1));
7405 break;
bellard31e31b82003-02-18 22:55:36 +00007406 case TARGET_NR_sched_yield:
7407 ret = get_errno(sched_yield());
7408 break;
7409 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00007410 ret = get_errno(sched_get_priority_max(arg1));
7411 break;
bellard31e31b82003-02-18 22:55:36 +00007412 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00007413 ret = get_errno(sched_get_priority_min(arg1));
7414 break;
bellard31e31b82003-02-18 22:55:36 +00007415 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00007416 {
bellard5cd43932003-03-29 16:54:36 +00007417 struct timespec ts;
7418 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7419 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007420 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00007421 }
7422 }
7423 break;
bellard31e31b82003-02-18 22:55:36 +00007424 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00007425 {
bellard1b6b0292003-03-22 17:31:38 +00007426 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00007427 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00007428 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00007429 if (is_error(ret) && arg2) {
7430 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00007431 }
7432 }
7433 break;
thse5febef2007-04-01 18:31:35 +00007434#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00007435 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00007436 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007437#endif
7438#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00007439 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00007440 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007441#endif
bellard31e31b82003-02-18 22:55:36 +00007442 case TARGET_NR_prctl:
Peter Maydell1e6722f2012-02-03 14:48:03 +00007443 switch (arg1) {
7444 case PR_GET_PDEATHSIG:
7445 {
7446 int deathsig;
7447 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
7448 if (!is_error(ret) && arg2
7449 && put_user_ual(deathsig, arg2)) {
7450 goto efault;
thse5574482007-02-11 20:03:13 +00007451 }
Peter Maydell1e6722f2012-02-03 14:48:03 +00007452 break;
7453 }
Peter Maydelldb9526b2012-02-03 14:48:03 +00007454#ifdef PR_GET_NAME
7455 case PR_GET_NAME:
7456 {
7457 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
7458 if (!name) {
7459 goto efault;
7460 }
7461 ret = get_errno(prctl(arg1, (unsigned long)name,
7462 arg3, arg4, arg5));
7463 unlock_user(name, arg2, 16);
7464 break;
7465 }
7466 case PR_SET_NAME:
7467 {
7468 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
7469 if (!name) {
7470 goto efault;
7471 }
7472 ret = get_errno(prctl(arg1, (unsigned long)name,
7473 arg3, arg4, arg5));
7474 unlock_user(name, arg2, 0);
7475 break;
7476 }
7477#endif
Peter Maydell1e6722f2012-02-03 14:48:03 +00007478 default:
7479 /* Most prctl options have no pointer arguments */
7480 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7481 break;
7482 }
ths39b9aae2007-02-11 18:36:44 +00007483 break;
bellardd2fd1af2007-11-14 18:08:56 +00007484#ifdef TARGET_NR_arch_prctl
7485 case TARGET_NR_arch_prctl:
7486#if defined(TARGET_I386) && !defined(TARGET_ABI32)
7487 ret = do_arch_prctl(cpu_env, arg1, arg2);
7488 break;
7489#else
7490 goto unimplemented;
7491#endif
7492#endif
aurel32f2c7ba12008-03-28 22:32:06 +00007493#ifdef TARGET_NR_pread64
7494 case TARGET_NR_pread64:
Alexander Grafae017a52012-09-29 15:32:39 +00007495 if (regpairs_aligned(cpu_env)) {
7496 arg4 = arg5;
7497 arg5 = arg6;
7498 }
aurel32f2c7ba12008-03-28 22:32:06 +00007499 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7500 goto efault;
7501 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7502 unlock_user(p, arg2, ret);
7503 break;
7504 case TARGET_NR_pwrite64:
Alexander Grafae017a52012-09-29 15:32:39 +00007505 if (regpairs_aligned(cpu_env)) {
7506 arg4 = arg5;
7507 arg5 = arg6;
7508 }
aurel32f2c7ba12008-03-28 22:32:06 +00007509 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7510 goto efault;
7511 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7512 unlock_user(p, arg2, 0);
7513 break;
7514#endif
bellard31e31b82003-02-18 22:55:36 +00007515 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00007516 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7517 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007518 ret = get_errno(sys_getcwd1(p, arg2));
7519 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00007520 break;
7521 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00007522 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007523 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00007524 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007525 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00007526#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02007527 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
Jia Liud9627832012-07-20 15:50:52 +08007528 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
Andreas Färber9349b4f2012-03-14 01:38:32 +01007529 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00007530 break;
7531#else
bellard5cd43932003-03-29 16:54:36 +00007532 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00007533#endif
bellard31e31b82003-02-18 22:55:36 +00007534 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00007535 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007536#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00007537 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00007538 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007539#endif
7540#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00007541 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00007542 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007543#endif
bellard048f6b42005-11-26 18:47:20 +00007544#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00007545 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00007546 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
7547 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00007548 break;
bellard048f6b42005-11-26 18:47:20 +00007549#endif
bellardebc05482003-09-30 21:08:41 +00007550#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00007551 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00007552 {
7553 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007554 int resource = target_to_host_resource(arg1);
7555 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00007556 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007557 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00007558 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7559 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007560 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7561 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007562 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00007563 }
7564 break;
7565 }
bellardebc05482003-09-30 21:08:41 +00007566#endif
bellarda315a142005-01-30 22:59:18 +00007567#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00007568 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00007569 if (!(p = lock_user_string(arg1)))
7570 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007571 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
7572 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00007573 break;
bellarda315a142005-01-30 22:59:18 +00007574#endif
7575#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00007576 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00007577 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00007578 break;
bellarda315a142005-01-30 22:59:18 +00007579#endif
7580#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00007581 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00007582 if (!(p = lock_user_string(arg1)))
7583 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007584 ret = get_errno(stat(path(p), &st));
7585 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007586 if (!is_error(ret))
7587 ret = host_to_target_stat64(cpu_env, arg2, &st);
7588 break;
bellarda315a142005-01-30 22:59:18 +00007589#endif
7590#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00007591 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00007592 if (!(p = lock_user_string(arg1)))
7593 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007594 ret = get_errno(lstat(path(p), &st));
7595 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007596 if (!is_error(ret))
7597 ret = host_to_target_stat64(cpu_env, arg2, &st);
7598 break;
bellarda315a142005-01-30 22:59:18 +00007599#endif
7600#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00007601 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00007602 ret = get_errno(fstat(arg1, &st));
7603 if (!is_error(ret))
7604 ret = host_to_target_stat64(cpu_env, arg2, &st);
7605 break;
bellardec86b0f2003-04-11 00:15:04 +00007606#endif
aurel329d33b762009-04-08 23:07:05 +00007607#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
7608 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
7609#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007610 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00007611#endif
7612#ifdef TARGET_NR_newfstatat
7613 case TARGET_NR_newfstatat:
7614#endif
balrog6a24a772008-09-20 02:23:36 +00007615 if (!(p = lock_user_string(arg2)))
7616 goto efault;
aurel329d33b762009-04-08 23:07:05 +00007617#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007618 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00007619#else
7620 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
7621#endif
balrog6a24a772008-09-20 02:23:36 +00007622 if (!is_error(ret))
7623 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00007624 break;
bellarda315a142005-01-30 22:59:18 +00007625#endif
bellard67867302003-11-23 17:05:30 +00007626 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00007627 if (!(p = lock_user_string(arg1)))
7628 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007629 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7630 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007631 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007632#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00007633 case TARGET_NR_getuid:
7634 ret = get_errno(high2lowuid(getuid()));
7635 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007636#endif
7637#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00007638 case TARGET_NR_getgid:
7639 ret = get_errno(high2lowgid(getgid()));
7640 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007641#endif
7642#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00007643 case TARGET_NR_geteuid:
7644 ret = get_errno(high2lowuid(geteuid()));
7645 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007646#endif
7647#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00007648 case TARGET_NR_getegid:
7649 ret = get_errno(high2lowgid(getegid()));
7650 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007651#endif
bellard67867302003-11-23 17:05:30 +00007652 case TARGET_NR_setreuid:
7653 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7654 break;
7655 case TARGET_NR_setregid:
7656 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7657 break;
7658 case TARGET_NR_getgroups:
7659 {
7660 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007661 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007662 gid_t *grouplist;
7663 int i;
7664
7665 grouplist = alloca(gidsetsize * sizeof(gid_t));
7666 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007667 if (gidsetsize == 0)
7668 break;
bellard67867302003-11-23 17:05:30 +00007669 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007670 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
7671 if (!target_grouplist)
7672 goto efault;
balroga2155fc2008-09-20 02:12:08 +00007673 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007674 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00007675 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00007676 }
7677 }
7678 break;
7679 case TARGET_NR_setgroups:
7680 {
7681 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007682 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007683 gid_t *grouplist;
7684 int i;
7685
7686 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007687 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
7688 if (!target_grouplist) {
7689 ret = -TARGET_EFAULT;
7690 goto fail;
7691 }
bellard67867302003-11-23 17:05:30 +00007692 for(i = 0;i < gidsetsize; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007693 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
pbrook53a59602006-03-25 19:31:22 +00007694 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00007695 ret = get_errno(setgroups(gidsetsize, grouplist));
7696 }
7697 break;
7698 case TARGET_NR_fchown:
7699 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7700 break;
thsccfa72b2007-09-24 09:23:34 +00007701#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
7702 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00007703 if (!(p = lock_user_string(arg2)))
7704 goto efault;
7705 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
7706 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00007707 break;
7708#endif
bellard67867302003-11-23 17:05:30 +00007709#ifdef TARGET_NR_setresuid
7710 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00007711 ret = get_errno(setresuid(low2highuid(arg1),
7712 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00007713 low2highuid(arg3)));
7714 break;
7715#endif
7716#ifdef TARGET_NR_getresuid
7717 case TARGET_NR_getresuid:
7718 {
pbrook53a59602006-03-25 19:31:22 +00007719 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00007720 ret = get_errno(getresuid(&ruid, &euid, &suid));
7721 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007722 if (put_user_u16(high2lowuid(ruid), arg1)
7723 || put_user_u16(high2lowuid(euid), arg2)
7724 || put_user_u16(high2lowuid(suid), arg3))
7725 goto efault;
bellard67867302003-11-23 17:05:30 +00007726 }
7727 }
7728 break;
7729#endif
7730#ifdef TARGET_NR_getresgid
7731 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00007732 ret = get_errno(setresgid(low2highgid(arg1),
7733 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00007734 low2highgid(arg3)));
7735 break;
7736#endif
7737#ifdef TARGET_NR_getresgid
7738 case TARGET_NR_getresgid:
7739 {
pbrook53a59602006-03-25 19:31:22 +00007740 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00007741 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7742 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007743 if (put_user_u16(high2lowgid(rgid), arg1)
7744 || put_user_u16(high2lowgid(egid), arg2)
7745 || put_user_u16(high2lowgid(sgid), arg3))
7746 goto efault;
bellard67867302003-11-23 17:05:30 +00007747 }
7748 }
7749 break;
7750#endif
7751 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00007752 if (!(p = lock_user_string(arg1)))
7753 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007754 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7755 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007756 break;
7757 case TARGET_NR_setuid:
7758 ret = get_errno(setuid(low2highuid(arg1)));
7759 break;
7760 case TARGET_NR_setgid:
7761 ret = get_errno(setgid(low2highgid(arg1)));
7762 break;
7763 case TARGET_NR_setfsuid:
7764 ret = get_errno(setfsuid(arg1));
7765 break;
7766 case TARGET_NR_setfsgid:
7767 ret = get_errno(setfsgid(arg1));
7768 break;
bellard67867302003-11-23 17:05:30 +00007769
bellarda315a142005-01-30 22:59:18 +00007770#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00007771 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00007772 if (!(p = lock_user_string(arg1)))
7773 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007774 ret = get_errno(lchown(p, arg2, arg3));
7775 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007776 break;
bellarda315a142005-01-30 22:59:18 +00007777#endif
7778#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00007779 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00007780 ret = get_errno(getuid());
7781 break;
bellarda315a142005-01-30 22:59:18 +00007782#endif
aurel3264b4d282008-11-14 17:20:15 +00007783
7784#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7785 /* Alpha specific */
7786 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007787 {
7788 uid_t euid;
7789 euid=geteuid();
7790 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7791 }
aurel3264b4d282008-11-14 17:20:15 +00007792 ret = get_errno(getuid());
7793 break;
7794#endif
7795#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7796 /* Alpha specific */
7797 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007798 {
7799 uid_t egid;
7800 egid=getegid();
7801 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7802 }
aurel3264b4d282008-11-14 17:20:15 +00007803 ret = get_errno(getgid());
7804 break;
7805#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08007806#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7807 /* Alpha specific */
7808 case TARGET_NR_osf_getsysinfo:
7809 ret = -TARGET_EOPNOTSUPP;
7810 switch (arg1) {
7811 case TARGET_GSI_IEEE_FP_CONTROL:
7812 {
7813 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7814
7815 /* Copied from linux ieee_fpcr_to_swcr. */
7816 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7817 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7818 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7819 | SWCR_TRAP_ENABLE_DZE
7820 | SWCR_TRAP_ENABLE_OVF);
7821 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
7822 | SWCR_TRAP_ENABLE_INE);
7823 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
7824 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
7825
7826 if (put_user_u64 (swcr, arg2))
7827 goto efault;
7828 ret = 0;
7829 }
7830 break;
7831
7832 /* case GSI_IEEE_STATE_AT_SIGNAL:
7833 -- Not implemented in linux kernel.
7834 case GSI_UACPROC:
7835 -- Retrieves current unaligned access state; not much used.
7836 case GSI_PROC_TYPE:
7837 -- Retrieves implver information; surely not used.
7838 case GSI_GET_HWRPB:
7839 -- Grabs a copy of the HWRPB; surely not used.
7840 */
7841 }
7842 break;
7843#endif
7844#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
7845 /* Alpha specific */
7846 case TARGET_NR_osf_setsysinfo:
7847 ret = -TARGET_EOPNOTSUPP;
7848 switch (arg1) {
7849 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007850 {
7851 uint64_t swcr, fpcr, orig_fpcr;
7852
Richard Henderson6e06d512012-06-01 09:08:21 -07007853 if (get_user_u64 (swcr, arg2)) {
Richard Hendersonba0e2762009-12-09 15:56:29 -08007854 goto efault;
Richard Henderson6e06d512012-06-01 09:08:21 -07007855 }
7856 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007857 fpcr = orig_fpcr & FPCR_DYN_MASK;
7858
7859 /* Copied from linux ieee_swcr_to_fpcr. */
7860 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
7861 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
7862 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
7863 | SWCR_TRAP_ENABLE_DZE
7864 | SWCR_TRAP_ENABLE_OVF)) << 48;
7865 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
7866 | SWCR_TRAP_ENABLE_INE)) << 57;
7867 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
7868 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
7869
Richard Henderson6e06d512012-06-01 09:08:21 -07007870 cpu_alpha_store_fpcr(cpu_env, fpcr);
7871 ret = 0;
7872 }
7873 break;
7874
7875 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
7876 {
7877 uint64_t exc, fpcr, orig_fpcr;
7878 int si_code;
7879
7880 if (get_user_u64(exc, arg2)) {
7881 goto efault;
7882 }
7883
7884 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
7885
7886 /* We only add to the exception status here. */
7887 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
7888
7889 cpu_alpha_store_fpcr(cpu_env, fpcr);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007890 ret = 0;
7891
Richard Henderson6e06d512012-06-01 09:08:21 -07007892 /* Old exceptions are not signaled. */
7893 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007894
Richard Henderson6e06d512012-06-01 09:08:21 -07007895 /* If any exceptions set by this call,
7896 and are unmasked, send a signal. */
7897 si_code = 0;
7898 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
7899 si_code = TARGET_FPE_FLTRES;
7900 }
7901 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
7902 si_code = TARGET_FPE_FLTUND;
7903 }
7904 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
7905 si_code = TARGET_FPE_FLTOVF;
7906 }
7907 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
7908 si_code = TARGET_FPE_FLTDIV;
7909 }
7910 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
7911 si_code = TARGET_FPE_FLTINV;
7912 }
7913 if (si_code != 0) {
7914 target_siginfo_t info;
7915 info.si_signo = SIGFPE;
7916 info.si_errno = 0;
7917 info.si_code = si_code;
7918 info._sifields._sigfault._addr
7919 = ((CPUArchState *)cpu_env)->pc;
7920 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007921 }
7922 }
7923 break;
7924
7925 /* case SSI_NVPAIRS:
7926 -- Used with SSIN_UACPROC to enable unaligned accesses.
7927 case SSI_IEEE_STATE_AT_SIGNAL:
7928 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
7929 -- Not implemented in linux kernel
7930 */
7931 }
7932 break;
7933#endif
7934#ifdef TARGET_NR_osf_sigprocmask
7935 /* Alpha specific. */
7936 case TARGET_NR_osf_sigprocmask:
7937 {
7938 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01007939 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08007940 sigset_t set, oldset;
7941
7942 switch(arg1) {
7943 case TARGET_SIG_BLOCK:
7944 how = SIG_BLOCK;
7945 break;
7946 case TARGET_SIG_UNBLOCK:
7947 how = SIG_UNBLOCK;
7948 break;
7949 case TARGET_SIG_SETMASK:
7950 how = SIG_SETMASK;
7951 break;
7952 default:
7953 ret = -TARGET_EINVAL;
7954 goto fail;
7955 }
7956 mask = arg2;
7957 target_to_host_old_sigset(&set, &mask);
Juan Quintelabc088ba2011-06-16 17:37:10 +01007958 sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007959 host_to_target_old_sigset(&mask, &oldset);
7960 ret = mask;
7961 }
7962 break;
7963#endif
aurel3264b4d282008-11-14 17:20:15 +00007964
bellarda315a142005-01-30 22:59:18 +00007965#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00007966 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00007967 ret = get_errno(getgid());
7968 break;
bellarda315a142005-01-30 22:59:18 +00007969#endif
7970#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00007971 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00007972 ret = get_errno(geteuid());
7973 break;
bellarda315a142005-01-30 22:59:18 +00007974#endif
7975#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00007976 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00007977 ret = get_errno(getegid());
7978 break;
bellarda315a142005-01-30 22:59:18 +00007979#endif
7980#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00007981 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00007982 ret = get_errno(setreuid(arg1, arg2));
7983 break;
bellarda315a142005-01-30 22:59:18 +00007984#endif
7985#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00007986 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00007987 ret = get_errno(setregid(arg1, arg2));
7988 break;
bellarda315a142005-01-30 22:59:18 +00007989#endif
7990#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00007991 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00007992 {
7993 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007994 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007995 gid_t *grouplist;
7996 int i;
7997
7998 grouplist = alloca(gidsetsize * sizeof(gid_t));
7999 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00008000 if (gidsetsize == 0)
8001 break;
bellard99c475a2005-01-31 20:45:13 +00008002 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00008003 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
8004 if (!target_grouplist) {
8005 ret = -TARGET_EFAULT;
8006 goto fail;
8007 }
balroga2155fc2008-09-20 02:12:08 +00008008 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00008009 target_grouplist[i] = tswap32(grouplist[i]);
8010 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00008011 }
8012 }
8013 break;
bellarda315a142005-01-30 22:59:18 +00008014#endif
8015#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00008016 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00008017 {
8018 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00008019 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00008020 gid_t *grouplist;
8021 int i;
ths3b46e622007-09-17 08:09:54 +00008022
bellard99c475a2005-01-31 20:45:13 +00008023 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00008024 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
8025 if (!target_grouplist) {
8026 ret = -TARGET_EFAULT;
8027 goto fail;
8028 }
bellard99c475a2005-01-31 20:45:13 +00008029 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00008030 grouplist[i] = tswap32(target_grouplist[i]);
8031 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00008032 ret = get_errno(setgroups(gidsetsize, grouplist));
8033 }
8034 break;
bellarda315a142005-01-30 22:59:18 +00008035#endif
8036#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00008037 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00008038 ret = get_errno(fchown(arg1, arg2, arg3));
8039 break;
bellarda315a142005-01-30 22:59:18 +00008040#endif
8041#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00008042 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00008043 ret = get_errno(setresuid(arg1, arg2, arg3));
8044 break;
bellarda315a142005-01-30 22:59:18 +00008045#endif
8046#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00008047 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00008048 {
pbrook53a59602006-03-25 19:31:22 +00008049 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00008050 ret = get_errno(getresuid(&ruid, &euid, &suid));
8051 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00008052 if (put_user_u32(ruid, arg1)
8053 || put_user_u32(euid, arg2)
8054 || put_user_u32(suid, arg3))
8055 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00008056 }
8057 }
8058 break;
bellarda315a142005-01-30 22:59:18 +00008059#endif
8060#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00008061 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00008062 ret = get_errno(setresgid(arg1, arg2, arg3));
8063 break;
bellarda315a142005-01-30 22:59:18 +00008064#endif
8065#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00008066 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00008067 {
pbrook53a59602006-03-25 19:31:22 +00008068 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00008069 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8070 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00008071 if (put_user_u32(rgid, arg1)
8072 || put_user_u32(egid, arg2)
8073 || put_user_u32(sgid, arg3))
8074 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00008075 }
8076 }
8077 break;
bellarda315a142005-01-30 22:59:18 +00008078#endif
8079#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00008080 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00008081 if (!(p = lock_user_string(arg1)))
8082 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008083 ret = get_errno(chown(p, arg2, arg3));
8084 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00008085 break;
bellarda315a142005-01-30 22:59:18 +00008086#endif
8087#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00008088 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00008089 ret = get_errno(setuid(arg1));
8090 break;
bellarda315a142005-01-30 22:59:18 +00008091#endif
8092#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00008093 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00008094 ret = get_errno(setgid(arg1));
8095 break;
bellarda315a142005-01-30 22:59:18 +00008096#endif
8097#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00008098 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00008099 ret = get_errno(setfsuid(arg1));
8100 break;
bellarda315a142005-01-30 22:59:18 +00008101#endif
8102#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00008103 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00008104 ret = get_errno(setfsgid(arg1));
8105 break;
bellarda315a142005-01-30 22:59:18 +00008106#endif
bellard67867302003-11-23 17:05:30 +00008107
bellard31e31b82003-02-18 22:55:36 +00008108 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00008109 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00008110#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00008111 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00008112 {
8113 void *a;
8114 ret = -TARGET_EFAULT;
8115 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8116 goto efault;
8117 if (!(p = lock_user_string(arg3)))
8118 goto mincore_fail;
8119 ret = get_errno(mincore(a, arg2, p));
8120 unlock_user(p, arg3, ret);
8121 mincore_fail:
8122 unlock_user(a, arg1, 0);
8123 }
8124 break;
bellardffa65c32004-01-04 23:57:22 +00008125#endif
aurel32408321b2008-10-01 21:46:32 +00008126#ifdef TARGET_NR_arm_fadvise64_64
8127 case TARGET_NR_arm_fadvise64_64:
8128 {
8129 /*
8130 * arm_fadvise64_64 looks like fadvise64_64 but
8131 * with different argument order
8132 */
8133 abi_long temp;
8134 temp = arg3;
8135 arg3 = arg4;
8136 arg4 = temp;
8137 }
8138#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008139#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00008140#ifdef TARGET_NR_fadvise64_64
8141 case TARGET_NR_fadvise64_64:
8142#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008143#ifdef TARGET_NR_fadvise64
8144 case TARGET_NR_fadvise64:
8145#endif
8146#ifdef TARGET_S390X
8147 switch (arg4) {
8148 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8149 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8150 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8151 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8152 default: break;
8153 }
8154#endif
8155 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00008156 break;
8157#endif
bellardffa65c32004-01-04 23:57:22 +00008158#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00008159 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00008160 /* A straight passthrough may not be safe because qemu sometimes
8161 turns private flie-backed mappings into anonymous mappings.
8162 This will break MADV_DONTNEED.
8163 This is a hint, so ignoring and returning success is ok. */
8164 ret = get_errno(0);
8165 break;
bellardffa65c32004-01-04 23:57:22 +00008166#endif
blueswir1992f48a2007-10-14 16:27:31 +00008167#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00008168 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00008169 {
thsb1e341e2007-03-20 21:50:52 +00008170 int cmd;
bellard77e46722003-04-29 20:39:06 +00008171 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00008172 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00008173#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00008174 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00008175#endif
bellard77e46722003-04-29 20:39:06 +00008176
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008177 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00008178 if (cmd == -TARGET_EINVAL) {
8179 ret = cmd;
8180 break;
8181 }
thsb1e341e2007-03-20 21:50:52 +00008182
bellard60cd49d2003-03-16 22:53:56 +00008183 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00008184 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00008185#ifdef TARGET_ARM
8186 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008187 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8188 goto efault;
ths58134272007-03-31 18:59:32 +00008189 fl.l_type = tswap16(target_efl->l_type);
8190 fl.l_whence = tswap16(target_efl->l_whence);
8191 fl.l_start = tswap64(target_efl->l_start);
8192 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008193 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00008194 unlock_user_struct(target_efl, arg3, 0);
8195 } else
8196#endif
8197 {
bellard9ee1fa22007-11-11 15:11:19 +00008198 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8199 goto efault;
ths58134272007-03-31 18:59:32 +00008200 fl.l_type = tswap16(target_fl->l_type);
8201 fl.l_whence = tswap16(target_fl->l_whence);
8202 fl.l_start = tswap64(target_fl->l_start);
8203 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008204 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00008205 unlock_user_struct(target_fl, arg3, 0);
8206 }
thsb1e341e2007-03-20 21:50:52 +00008207 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008208 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00008209#ifdef TARGET_ARM
8210 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008211 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8212 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008213 target_efl->l_type = tswap16(fl.l_type);
8214 target_efl->l_whence = tswap16(fl.l_whence);
8215 target_efl->l_start = tswap64(fl.l_start);
8216 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008217 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008218 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008219 } else
8220#endif
8221 {
bellard9ee1fa22007-11-11 15:11:19 +00008222 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8223 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008224 target_fl->l_type = tswap16(fl.l_type);
8225 target_fl->l_whence = tswap16(fl.l_whence);
8226 target_fl->l_start = tswap64(fl.l_start);
8227 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008228 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008229 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008230 }
bellard77e46722003-04-29 20:39:06 +00008231 }
8232 break;
8233
thsb1e341e2007-03-20 21:50:52 +00008234 case TARGET_F_SETLK64:
8235 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00008236#ifdef TARGET_ARM
8237 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008238 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8239 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008240 fl.l_type = tswap16(target_efl->l_type);
8241 fl.l_whence = tswap16(target_efl->l_whence);
8242 fl.l_start = tswap64(target_efl->l_start);
8243 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008244 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008245 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008246 } else
8247#endif
8248 {
bellard9ee1fa22007-11-11 15:11:19 +00008249 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8250 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008251 fl.l_type = tswap16(target_fl->l_type);
8252 fl.l_whence = tswap16(target_fl->l_whence);
8253 fl.l_start = tswap64(target_fl->l_start);
8254 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008255 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008256 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008257 }
thsb1e341e2007-03-20 21:50:52 +00008258 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008259 break;
bellard60cd49d2003-03-16 22:53:56 +00008260 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008261 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00008262 break;
8263 }
bellard77e46722003-04-29 20:39:06 +00008264 break;
8265 }
bellard60cd49d2003-03-16 22:53:56 +00008266#endif
ths7d600c82006-12-08 01:32:58 +00008267#ifdef TARGET_NR_cacheflush
8268 case TARGET_NR_cacheflush:
8269 /* self-modifying code is handled automatically, so nothing needed */
8270 ret = 0;
8271 break;
8272#endif
bellardebc05482003-09-30 21:08:41 +00008273#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00008274 case TARGET_NR_security:
8275 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008276#endif
bellardc573ff62004-01-04 15:51:36 +00008277#ifdef TARGET_NR_getpagesize
8278 case TARGET_NR_getpagesize:
8279 ret = TARGET_PAGE_SIZE;
8280 break;
8281#endif
bellard31e31b82003-02-18 22:55:36 +00008282 case TARGET_NR_gettid:
8283 ret = get_errno(gettid());
8284 break;
thse5febef2007-04-01 18:31:35 +00008285#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00008286 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00008287#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +03008288 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +00008289 arg2 = arg3;
8290 arg3 = arg4;
8291 arg4 = arg5;
8292 }
aurel322054ac92008-10-13 21:08:07 +00008293 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8294#else
8295 ret = get_errno(readahead(arg1, arg2, arg3));
8296#endif
8297 break;
thse5febef2007-04-01 18:31:35 +00008298#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008299#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +00008300#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00008301 case TARGET_NR_listxattr:
8302 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +00008303 {
8304 void *p, *b = 0;
8305 if (arg2) {
8306 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8307 if (!b) {
8308 ret = -TARGET_EFAULT;
8309 break;
8310 }
8311 }
8312 p = lock_user_string(arg1);
8313 if (p) {
8314 if (num == TARGET_NR_listxattr) {
8315 ret = get_errno(listxattr(p, b, arg3));
8316 } else {
8317 ret = get_errno(llistxattr(p, b, arg3));
8318 }
8319 } else {
8320 ret = -TARGET_EFAULT;
8321 }
8322 unlock_user(p, arg1, 0);
8323 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +03008324 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +00008325 }
8326 case TARGET_NR_flistxattr:
8327 {
8328 void *b = 0;
8329 if (arg2) {
8330 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8331 if (!b) {
8332 ret = -TARGET_EFAULT;
8333 break;
8334 }
8335 }
8336 ret = get_errno(flistxattr(arg1, b, arg3));
8337 unlock_user(b, arg2, arg3);
8338 break;
8339 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008340 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008341 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008342 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008343 void *p, *n, *v = 0;
8344 if (arg3) {
8345 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8346 if (!v) {
8347 ret = -TARGET_EFAULT;
8348 break;
8349 }
8350 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008351 p = lock_user_string(arg1);
8352 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008353 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008354 if (num == TARGET_NR_setxattr) {
8355 ret = get_errno(setxattr(p, n, v, arg4, arg5));
8356 } else {
8357 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
8358 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008359 } else {
8360 ret = -TARGET_EFAULT;
8361 }
8362 unlock_user(p, arg1, 0);
8363 unlock_user(n, arg2, 0);
8364 unlock_user(v, arg3, 0);
8365 }
8366 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008367 case TARGET_NR_fsetxattr:
8368 {
8369 void *n, *v = 0;
8370 if (arg3) {
8371 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8372 if (!v) {
8373 ret = -TARGET_EFAULT;
8374 break;
8375 }
8376 }
8377 n = lock_user_string(arg2);
8378 if (n) {
8379 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
8380 } else {
8381 ret = -TARGET_EFAULT;
8382 }
8383 unlock_user(n, arg2, 0);
8384 unlock_user(v, arg3, 0);
8385 }
8386 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008387 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008388 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008389 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008390 void *p, *n, *v = 0;
8391 if (arg3) {
8392 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8393 if (!v) {
8394 ret = -TARGET_EFAULT;
8395 break;
8396 }
8397 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008398 p = lock_user_string(arg1);
8399 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008400 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008401 if (num == TARGET_NR_getxattr) {
8402 ret = get_errno(getxattr(p, n, v, arg4));
8403 } else {
8404 ret = get_errno(lgetxattr(p, n, v, arg4));
8405 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008406 } else {
8407 ret = -TARGET_EFAULT;
8408 }
8409 unlock_user(p, arg1, 0);
8410 unlock_user(n, arg2, 0);
8411 unlock_user(v, arg3, arg4);
8412 }
8413 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008414 case TARGET_NR_fgetxattr:
8415 {
8416 void *n, *v = 0;
8417 if (arg3) {
8418 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8419 if (!v) {
8420 ret = -TARGET_EFAULT;
8421 break;
8422 }
8423 }
8424 n = lock_user_string(arg2);
8425 if (n) {
8426 ret = get_errno(fgetxattr(arg1, n, v, arg4));
8427 } else {
8428 ret = -TARGET_EFAULT;
8429 }
8430 unlock_user(n, arg2, 0);
8431 unlock_user(v, arg3, arg4);
8432 }
8433 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008434 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008435 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008436 {
8437 void *p, *n;
8438 p = lock_user_string(arg1);
8439 n = lock_user_string(arg2);
8440 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008441 if (num == TARGET_NR_removexattr) {
8442 ret = get_errno(removexattr(p, n));
8443 } else {
8444 ret = get_errno(lremovexattr(p, n));
8445 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008446 } else {
8447 ret = -TARGET_EFAULT;
8448 }
8449 unlock_user(p, arg1, 0);
8450 unlock_user(n, arg2, 0);
8451 }
8452 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008453 case TARGET_NR_fremovexattr:
8454 {
8455 void *n;
8456 n = lock_user_string(arg2);
8457 if (n) {
8458 ret = get_errno(fremovexattr(arg1, n));
8459 } else {
8460 ret = -TARGET_EFAULT;
8461 }
8462 unlock_user(n, arg2, 0);
8463 }
8464 break;
bellardebc05482003-09-30 21:08:41 +00008465#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008466#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +00008467#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00008468 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008469#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00008470 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
8471 ret = 0;
8472 break;
edgar_iglef967792009-01-07 14:19:38 +00008473#elif defined(TARGET_CRIS)
8474 if (arg1 & 0xff)
8475 ret = -TARGET_EINVAL;
8476 else {
8477 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
8478 ret = 0;
8479 }
8480 break;
bellard8d18e892007-11-14 15:18:40 +00008481#elif defined(TARGET_I386) && defined(TARGET_ABI32)
8482 ret = do_set_thread_area(cpu_env, arg1);
8483 break;
ths6f5b89a2007-03-02 20:48:00 +00008484#else
8485 goto unimplemented_nowarn;
8486#endif
8487#endif
8488#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00008489 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008490#if defined(TARGET_I386) && defined(TARGET_ABI32)
8491 ret = do_get_thread_area(cpu_env, arg1);
8492#else
bellard5cd43932003-03-29 16:54:36 +00008493 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00008494#endif
bellard8d18e892007-11-14 15:18:40 +00008495#endif
bellard48dc41e2006-06-21 18:15:50 +00008496#ifdef TARGET_NR_getdomainname
8497 case TARGET_NR_getdomainname:
8498 goto unimplemented_nowarn;
8499#endif
ths6f5b89a2007-03-02 20:48:00 +00008500
thsb5906f92007-03-19 13:32:45 +00008501#ifdef TARGET_NR_clock_gettime
8502 case TARGET_NR_clock_gettime:
8503 {
8504 struct timespec ts;
8505 ret = get_errno(clock_gettime(arg1, &ts));
8506 if (!is_error(ret)) {
8507 host_to_target_timespec(arg2, &ts);
8508 }
8509 break;
8510 }
8511#endif
8512#ifdef TARGET_NR_clock_getres
8513 case TARGET_NR_clock_getres:
8514 {
8515 struct timespec ts;
8516 ret = get_errno(clock_getres(arg1, &ts));
8517 if (!is_error(ret)) {
8518 host_to_target_timespec(arg2, &ts);
8519 }
8520 break;
8521 }
8522#endif
pbrook63d76512008-05-29 13:43:29 +00008523#ifdef TARGET_NR_clock_nanosleep
8524 case TARGET_NR_clock_nanosleep:
8525 {
8526 struct timespec ts;
8527 target_to_host_timespec(&ts, arg3);
8528 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
8529 if (arg4)
8530 host_to_target_timespec(arg4, &ts);
8531 break;
8532 }
8533#endif
thsb5906f92007-03-19 13:32:45 +00008534
ths6f5b89a2007-03-02 20:48:00 +00008535#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8536 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00008537 ret = get_errno(set_tid_address((int *)g2h(arg1)));
8538 break;
ths6f5b89a2007-03-02 20:48:00 +00008539#endif
8540
ths3ae43202007-09-16 21:39:48 +00008541#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00008542 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00008543 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00008544 break;
8545#endif
8546
ths3ae43202007-09-16 21:39:48 +00008547#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00008548 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00008549 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
8550 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00008551 break;
8552#endif
8553
ths4f2b1fe2007-06-21 21:57:12 +00008554#ifdef TARGET_NR_set_robust_list
8555 case TARGET_NR_set_robust_list:
8556 goto unimplemented_nowarn;
8557#endif
8558
ths9007f0e2007-09-25 17:50:37 +00008559#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
8560 case TARGET_NR_utimensat:
8561 {
Riku Voipioebc996f2009-04-21 15:01:51 +03008562 struct timespec *tsp, ts[2];
8563 if (!arg3) {
8564 tsp = NULL;
8565 } else {
8566 target_to_host_timespec(ts, arg3);
8567 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
8568 tsp = ts;
8569 }
ths9007f0e2007-09-25 17:50:37 +00008570 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03008571 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00008572 else {
bellard579a97f2007-11-11 14:26:47 +00008573 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00008574 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00008575 goto fail;
8576 }
Riku Voipioebc996f2009-04-21 15:01:51 +03008577 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00008578 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00008579 }
8580 }
8581 break;
8582#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02008583#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00008584 case TARGET_NR_futex:
8585 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
8586 break;
8587#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008588#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00008589 case TARGET_NR_inotify_init:
8590 ret = get_errno(sys_inotify_init());
8591 break;
8592#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008593#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00008594#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8595 case TARGET_NR_inotify_init1:
8596 ret = get_errno(sys_inotify_init1(arg1));
8597 break;
8598#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008599#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008600#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00008601 case TARGET_NR_inotify_add_watch:
8602 p = lock_user_string(arg2);
8603 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
8604 unlock_user(p, arg2, 0);
8605 break;
8606#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008607#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00008608 case TARGET_NR_inotify_rm_watch:
8609 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
8610 break;
8611#endif
ths9007f0e2007-09-25 17:50:37 +00008612
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07008613#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00008614 case TARGET_NR_mq_open:
8615 {
8616 struct mq_attr posix_mq_attr;
8617
8618 p = lock_user_string(arg1 - 1);
8619 if (arg4 != 0)
8620 copy_from_user_mq_attr (&posix_mq_attr, arg4);
8621 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
8622 unlock_user (p, arg1, 0);
8623 }
8624 break;
8625
8626 case TARGET_NR_mq_unlink:
8627 p = lock_user_string(arg1 - 1);
8628 ret = get_errno(mq_unlink(p));
8629 unlock_user (p, arg1, 0);
8630 break;
8631
8632 case TARGET_NR_mq_timedsend:
8633 {
8634 struct timespec ts;
8635
8636 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8637 if (arg5 != 0) {
8638 target_to_host_timespec(&ts, arg5);
8639 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
8640 host_to_target_timespec(arg5, &ts);
8641 }
8642 else
8643 ret = get_errno(mq_send(arg1, p, arg3, arg4));
8644 unlock_user (p, arg2, arg3);
8645 }
8646 break;
8647
8648 case TARGET_NR_mq_timedreceive:
8649 {
8650 struct timespec ts;
8651 unsigned int prio;
8652
8653 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8654 if (arg5 != 0) {
8655 target_to_host_timespec(&ts, arg5);
8656 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
8657 host_to_target_timespec(arg5, &ts);
8658 }
8659 else
8660 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
8661 unlock_user (p, arg2, arg3);
8662 if (arg4 != 0)
8663 put_user_u32(prio, arg4);
8664 }
8665 break;
8666
8667 /* Not implemented for now... */
8668/* case TARGET_NR_mq_notify: */
8669/* break; */
8670
8671 case TARGET_NR_mq_getsetattr:
8672 {
8673 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
8674 ret = 0;
8675 if (arg3 != 0) {
8676 ret = mq_getattr(arg1, &posix_mq_attr_out);
8677 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
8678 }
8679 if (arg2 != 0) {
8680 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
8681 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
8682 }
8683
8684 }
8685 break;
8686#endif
8687
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308688#ifdef CONFIG_SPLICE
8689#ifdef TARGET_NR_tee
8690 case TARGET_NR_tee:
8691 {
8692 ret = get_errno(tee(arg1,arg2,arg3,arg4));
8693 }
8694 break;
8695#endif
8696#ifdef TARGET_NR_splice
8697 case TARGET_NR_splice:
8698 {
8699 loff_t loff_in, loff_out;
8700 loff_t *ploff_in = NULL, *ploff_out = NULL;
8701 if(arg2) {
8702 get_user_u64(loff_in, arg2);
8703 ploff_in = &loff_in;
8704 }
8705 if(arg4) {
8706 get_user_u64(loff_out, arg2);
8707 ploff_out = &loff_out;
8708 }
8709 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
8710 }
8711 break;
8712#endif
8713#ifdef TARGET_NR_vmsplice
8714 case TARGET_NR_vmsplice:
8715 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008716 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8717 if (vec != NULL) {
8718 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
8719 unlock_iovec(vec, arg2, arg3, 0);
8720 } else {
8721 ret = -host_to_target_errno(errno);
8722 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308723 }
8724 break;
8725#endif
8726#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03008727#ifdef CONFIG_EVENTFD
8728#if defined(TARGET_NR_eventfd)
8729 case TARGET_NR_eventfd:
8730 ret = get_errno(eventfd(arg1, 0));
8731 break;
8732#endif
8733#if defined(TARGET_NR_eventfd2)
8734 case TARGET_NR_eventfd2:
8735 ret = get_errno(eventfd(arg1, arg2));
8736 break;
8737#endif
8738#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03008739#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8740 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +01008741#if TARGET_ABI_BITS == 32
8742 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
8743 target_offset64(arg5, arg6)));
8744#else
Ulrich Hechtd0927932009-09-17 20:22:14 +03008745 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +01008746#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03008747 break;
8748#endif
Peter Maydellc727f472011-01-06 11:05:10 +00008749#if defined(CONFIG_SYNC_FILE_RANGE)
8750#if defined(TARGET_NR_sync_file_range)
8751 case TARGET_NR_sync_file_range:
8752#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +03008753#if defined(TARGET_MIPS)
8754 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8755 target_offset64(arg5, arg6), arg7));
8756#else
Peter Maydellc727f472011-01-06 11:05:10 +00008757 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
8758 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +03008759#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +00008760#else
8761 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
8762#endif
8763 break;
8764#endif
8765#if defined(TARGET_NR_sync_file_range2)
8766 case TARGET_NR_sync_file_range2:
8767 /* This is like sync_file_range but the arguments are reordered */
8768#if TARGET_ABI_BITS == 32
8769 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8770 target_offset64(arg5, arg6), arg2));
8771#else
8772 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
8773#endif
8774 break;
8775#endif
8776#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00008777#if defined(CONFIG_EPOLL)
8778#if defined(TARGET_NR_epoll_create)
8779 case TARGET_NR_epoll_create:
8780 ret = get_errno(epoll_create(arg1));
8781 break;
8782#endif
8783#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
8784 case TARGET_NR_epoll_create1:
8785 ret = get_errno(epoll_create1(arg1));
8786 break;
8787#endif
8788#if defined(TARGET_NR_epoll_ctl)
8789 case TARGET_NR_epoll_ctl:
8790 {
8791 struct epoll_event ep;
8792 struct epoll_event *epp = 0;
8793 if (arg4) {
8794 struct target_epoll_event *target_ep;
8795 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
8796 goto efault;
8797 }
8798 ep.events = tswap32(target_ep->events);
8799 /* The epoll_data_t union is just opaque data to the kernel,
8800 * so we transfer all 64 bits across and need not worry what
8801 * actual data type it is.
8802 */
8803 ep.data.u64 = tswap64(target_ep->data.u64);
8804 unlock_user_struct(target_ep, arg4, 0);
8805 epp = &ep;
8806 }
8807 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
8808 break;
8809 }
8810#endif
8811
8812#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
8813#define IMPLEMENT_EPOLL_PWAIT
8814#endif
8815#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
8816#if defined(TARGET_NR_epoll_wait)
8817 case TARGET_NR_epoll_wait:
8818#endif
8819#if defined(IMPLEMENT_EPOLL_PWAIT)
8820 case TARGET_NR_epoll_pwait:
8821#endif
8822 {
8823 struct target_epoll_event *target_ep;
8824 struct epoll_event *ep;
8825 int epfd = arg1;
8826 int maxevents = arg3;
8827 int timeout = arg4;
8828
8829 target_ep = lock_user(VERIFY_WRITE, arg2,
8830 maxevents * sizeof(struct target_epoll_event), 1);
8831 if (!target_ep) {
8832 goto efault;
8833 }
8834
8835 ep = alloca(maxevents * sizeof(struct epoll_event));
8836
8837 switch (num) {
8838#if defined(IMPLEMENT_EPOLL_PWAIT)
8839 case TARGET_NR_epoll_pwait:
8840 {
8841 target_sigset_t *target_set;
8842 sigset_t _set, *set = &_set;
8843
8844 if (arg5) {
8845 target_set = lock_user(VERIFY_READ, arg5,
8846 sizeof(target_sigset_t), 1);
8847 if (!target_set) {
8848 unlock_user(target_ep, arg2, 0);
8849 goto efault;
8850 }
8851 target_to_host_sigset(set, target_set);
8852 unlock_user(target_set, arg5, 0);
8853 } else {
8854 set = NULL;
8855 }
8856
8857 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
8858 break;
8859 }
8860#endif
8861#if defined(TARGET_NR_epoll_wait)
8862 case TARGET_NR_epoll_wait:
8863 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
8864 break;
8865#endif
8866 default:
8867 ret = -TARGET_ENOSYS;
8868 }
8869 if (!is_error(ret)) {
8870 int i;
8871 for (i = 0; i < ret; i++) {
8872 target_ep[i].events = tswap32(ep[i].events);
8873 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
8874 }
8875 }
8876 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
8877 break;
8878 }
8879#endif
8880#endif
Peter Maydell163a05a2011-06-27 17:44:52 +01008881#ifdef TARGET_NR_prlimit64
8882 case TARGET_NR_prlimit64:
8883 {
8884 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
8885 struct target_rlimit64 *target_rnew, *target_rold;
8886 struct host_rlimit64 rnew, rold, *rnewp = 0;
8887 if (arg3) {
8888 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
8889 goto efault;
8890 }
8891 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
8892 rnew.rlim_max = tswap64(target_rnew->rlim_max);
8893 unlock_user_struct(target_rnew, arg3, 0);
8894 rnewp = &rnew;
8895 }
8896
8897 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
8898 if (!is_error(ret) && arg4) {
8899 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
8900 goto efault;
8901 }
8902 target_rold->rlim_cur = tswap64(rold.rlim_cur);
8903 target_rold->rlim_max = tswap64(rold.rlim_max);
8904 unlock_user_struct(target_rold, arg4, 1);
8905 }
8906 break;
8907 }
8908#endif
Richard Henderson3d21d292012-09-15 13:20:46 -07008909#ifdef TARGET_NR_gethostname
8910 case TARGET_NR_gethostname:
8911 {
8912 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
8913 if (name) {
8914 ret = get_errno(gethostname(name, arg2));
8915 unlock_user(name, arg1, arg2);
8916 } else {
8917 ret = -TARGET_EFAULT;
8918 }
8919 break;
8920 }
8921#endif
bellard31e31b82003-02-18 22:55:36 +00008922 default:
8923 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00008924 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00008925#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 +00008926 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00008927#endif
ths0da46a62007-10-20 20:23:07 +00008928 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00008929 break;
8930 }
bellard579a97f2007-11-11 14:26:47 +00008931fail:
bellardc573ff62004-01-04 15:51:36 +00008932#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00008933 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00008934#endif
thsb92c47c2007-11-01 00:07:38 +00008935 if(do_strace)
8936 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00008937 return ret;
bellard579a97f2007-11-11 14:26:47 +00008938efault:
8939 ret = -TARGET_EFAULT;
8940 goto fail;
bellard31e31b82003-02-18 22:55:36 +00008941}