blob: c2169664d5d4e298e5486cb8b9f50ba497ac34b4 [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>
Peter Maydelle0eb2102014-03-17 12:15:35 +000046#include <linux/capability.h>
bellard31e31b82003-02-18 22:55:36 +000047#include <signal.h>
48#include <sched.h>
Aurelien Jarno60e99242010-03-29 02:12:51 +020049#ifdef __ia64__
50int __clone2(int (*fn)(void *), void *child_stack_base,
51 size_t stack_size, int flags, void *arg, ...);
52#endif
bellard31e31b82003-02-18 22:55:36 +000053#include <sys/socket.h>
aurel32607175e2009-04-15 16:11:59 +000054#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000055#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000056#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000057#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000058#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000059#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000060#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000061#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000062#include <sys/sysinfo.h>
Laurent Viviere36800c2015-10-02 14:48:09 +020063#include <sys/signalfd.h>
bellard72f03902003-02-18 23:33:18 +000064//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000065#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000066#include <netinet/tcp.h>
Laurent Vivier86fcd942011-03-30 01:35:23 +020067#include <linux/wireless.h>
Jing Huang920394d2012-07-24 13:59:23 +000068#include <linux/icmp.h>
Stefan Weil5a61cb62011-09-08 17:55:32 +020069#include "qemu-common.h"
Riku Voipiod80a1902014-10-01 16:05:46 +030070#ifdef CONFIG_TIMERFD
71#include <sys/timerfd.h>
72#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +020073#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +000074#include <sys/gmon.h>
75#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030076#ifdef CONFIG_EVENTFD
77#include <sys/eventfd.h>
78#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000079#ifdef CONFIG_EPOLL
80#include <sys/epoll.h>
81#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070082#ifdef CONFIG_ATTR
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010083#include "qemu/xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070084#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +000085#ifdef CONFIG_SENDFILE
86#include <sys/sendfile.h>
87#endif
bellard31e31b82003-02-18 22:55:36 +000088
89#define termios host_termios
90#define winsize host_winsize
91#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000092#define sgttyb host_sgttyb /* same as target */
93#define tchars host_tchars /* same as target */
94#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000095
96#include <linux/termios.h>
97#include <linux/unistd.h>
bellard31e31b82003-02-18 22:55:36 +000098#include <linux/cdrom.h>
99#include <linux/hdreg.h>
100#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +0000101#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +0000102#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +0300103#include <linux/fs.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000104#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +0000105#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000106#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +0200107#include <linux/fb.h>
108#include <linux/vt.h>
Alexander Graf56e904e2012-01-31 18:42:06 +0100109#include <linux/dm-ioctl.h>
Laurent Vivierc07ecc62013-01-07 11:40:06 +0000110#include <linux/reboot.h>
Laurent Vivier7ff7b662013-07-02 14:04:12 +0100111#include <linux/route.h>
Laurent Vivierf57d4192013-08-30 01:46:41 +0200112#include <linux/filter.h>
Andreas Färberfff8c532014-01-18 07:38:30 +0100113#include <linux/blkpg.h>
pbrookd7e40362008-05-23 16:06:43 +0000114#include "linux_loop.h"
Riku Voipio18cb0082014-02-19 12:59:58 +0200115#include "uname.h"
bellard31e31b82003-02-18 22:55:36 +0000116
bellard3ef693a2003-03-23 20:17:16 +0000117#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000118
pbrookd865bab2008-06-07 22:12:17 +0000119#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
120 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
pbrook30813ce2008-06-02 15:45:44 +0000121
bellard72f03902003-02-18 23:33:18 +0000122//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000123
bellard1a9353d2003-03-16 20:28:50 +0000124//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000125#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
126#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000127
bellard70a194b2003-08-11 22:20:16 +0000128
bellard70a194b2003-08-11 22:20:16 +0000129#undef _syscall0
130#undef _syscall1
131#undef _syscall2
132#undef _syscall3
133#undef _syscall4
134#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000135#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000136
bellard83fcb512006-06-14 13:37:16 +0000137#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000138static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000139{ \
140 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000141}
142
bellard83fcb512006-06-14 13:37:16 +0000143#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000144static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000145{ \
146 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000147}
148
bellard83fcb512006-06-14 13:37:16 +0000149#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000150static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000151{ \
152 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000153}
154
bellard83fcb512006-06-14 13:37:16 +0000155#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000156static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000157{ \
158 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000159}
160
bellard83fcb512006-06-14 13:37:16 +0000161#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000162static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000163{ \
164 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000165}
166
bellard83fcb512006-06-14 13:37:16 +0000167#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
168 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000169static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000170{ \
171 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000172}
bellard83fcb512006-06-14 13:37:16 +0000173
174
175#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
176 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000177static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
178 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000179{ \
180 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
181}
182
bellard70a194b2003-08-11 22:20:16 +0000183
bellard31e31b82003-02-18 22:55:36 +0000184#define __NR_sys_uname __NR_uname
bellard72f03902003-02-18 23:33:18 +0000185#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000186#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000187#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000188#define __NR_sys_getpriority __NR_getpriority
bellard66fb9762003-03-23 01:06:05 +0000189#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
ths7494b0f2007-02-11 18:26:53 +0000190#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000191#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000192#define __NR_sys_tkill __NR_tkill
pbrookbd0c5662008-05-29 14:34:11 +0000193#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000194#define __NR_sys_inotify_init __NR_inotify_init
195#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
196#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000197
Alexander Graf42a39fb2011-04-15 17:32:45 +0200198#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
199 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000200#define __NR__llseek __NR_lseek
201#endif
202
James Hogana29e5ba2014-03-25 21:51:08 +0000203/* Newer kernel ports have llseek() instead of _llseek() */
204#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
205#define TARGET_NR__llseek TARGET_NR_llseek
206#endif
207
bellard72f03902003-02-18 23:33:18 +0000208#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000209_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000210#else
ths0da46a62007-10-20 20:23:07 +0000211/* This is a replacement for the host gettid() and must return a host
212 errno. */
bellard72f03902003-02-18 23:33:18 +0000213static int gettid(void) {
214 return -ENOSYS;
215}
216#endif
Chen Gang704eff62015-08-21 05:37:33 +0800217#if defined(TARGET_NR_getdents) && defined(__NR_getdents)
aurel323b3f24a2009-04-15 16:12:13 +0000218_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
Peter Maydell3307e232013-06-12 16:20:21 +0100219#endif
220#if !defined(__NR_getdents) || \
221 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
aurel323b3f24a2009-04-15 16:12:13 +0000222_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
223#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700224#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000225_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
226 loff_t *, res, uint, wh);
227#endif
228_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
229_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
230#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
231_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
232#endif
233#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
234_syscall2(int,sys_tkill,int,tid,int,sig)
235#endif
236#ifdef __NR_exit_group
237_syscall1(int,exit_group,int,error_code)
238#endif
239#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
240_syscall1(int,set_tid_address,int *,tidptr)
241#endif
aurel323b3f24a2009-04-15 16:12:13 +0000242#if defined(TARGET_NR_futex) && defined(__NR_futex)
243_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
244 const struct timespec *,timeout,int *,uaddr2,int,val3)
245#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500246#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
247_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
248 unsigned long *, user_mask_ptr);
249#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
250_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
251 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200252_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
253 void *, arg);
Peter Maydelle0eb2102014-03-17 12:15:35 +0000254_syscall2(int, capget, struct __user_cap_header_struct *, header,
255 struct __user_cap_data_struct *, data);
256_syscall2(int, capset, struct __user_cap_header_struct *, header,
257 struct __user_cap_data_struct *, data);
Paul Burtonab31cda2014-06-22 11:25:43 +0100258#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
259_syscall2(int, ioprio_get, int, which, int, who)
260#endif
261#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
262_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
263#endif
aurel323b3f24a2009-04-15 16:12:13 +0000264
265static bitmask_transtbl fcntl_flags_tbl[] = {
266 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
267 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
268 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
269 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
270 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
271 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
272 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
273 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700274 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000275 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
276 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
277 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
278 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000279#if defined(O_DIRECT)
280 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
281#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700282#if defined(O_NOATIME)
283 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
284#endif
285#if defined(O_CLOEXEC)
286 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
287#endif
288#if defined(O_PATH)
289 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
290#endif
291 /* Don't terminate the list prematurely on 64-bit host+guest. */
292#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
293 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
294#endif
aurel323b3f24a2009-04-15 16:12:13 +0000295 { 0, 0, 0, 0 }
296};
297
Laurent Viviere36800c2015-10-02 14:48:09 +0200298typedef abi_long (*TargetFdFunc)(void *, size_t);
299typedef struct TargetFdTrans {
300 TargetFdFunc host_to_target;
301 TargetFdFunc target_to_host;
302} TargetFdTrans;
303
304static TargetFdTrans **target_fd_trans;
305
306static unsigned int target_fd_max;
307
308static TargetFdFunc fd_trans_host_to_target(int fd)
309{
310 if (fd < target_fd_max && target_fd_trans[fd]) {
311 return target_fd_trans[fd]->host_to_target;
312 }
313 return NULL;
314}
315
316static void fd_trans_register(int fd, TargetFdTrans *trans)
317{
318 unsigned int oldmax;
319
320 if (fd >= target_fd_max) {
321 oldmax = target_fd_max;
322 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
323 target_fd_trans = g_realloc(target_fd_trans,
324 target_fd_max * sizeof(TargetFdTrans));
325 memset((void *)(target_fd_trans + oldmax), 0,
326 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
327 }
328 target_fd_trans[fd] = trans;
329}
330
331static void fd_trans_unregister(int fd)
332{
333 if (fd >= 0 && fd < target_fd_max) {
334 target_fd_trans[fd] = NULL;
335 }
336}
337
338static void fd_trans_dup(int oldfd, int newfd)
339{
340 fd_trans_unregister(newfd);
341 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
342 fd_trans_register(newfd, target_fd_trans[oldfd]);
343 }
344}
345
aurel323b3f24a2009-04-15 16:12:13 +0000346static int sys_getcwd1(char *buf, size_t size)
347{
348 if (getcwd(buf, size) == NULL) {
349 /* getcwd() sets errno */
350 return (-1);
351 }
aurel32aaf4ad32009-04-16 14:17:14 +0000352 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000353}
354
Alexander Graff4c69012011-09-25 06:25:35 +0200355static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000356{
357 /*
358 * open(2) has extra parameter 'mode' when called with
359 * flag O_CREAT.
360 */
361 if ((flags & O_CREAT) != 0) {
aurel323b3f24a2009-04-15 16:12:13 +0000362 return (openat(dirfd, pathname, flags, mode));
363 }
364 return (openat(dirfd, pathname, flags));
365}
Riku Voipioebc996f2009-04-21 15:01:51 +0300366
Peter Maydell1acae9f2013-07-02 14:04:12 +0100367#ifdef TARGET_NR_utimensat
Riku Voipioebc996f2009-04-21 15:01:51 +0300368#ifdef CONFIG_UTIMENSAT
369static int sys_utimensat(int dirfd, const char *pathname,
370 const struct timespec times[2], int flags)
371{
372 if (pathname == NULL)
373 return futimens(dirfd, times);
374 else
375 return utimensat(dirfd, pathname, times, flags);
376}
Peter Maydell1acae9f2013-07-02 14:04:12 +0100377#elif defined(__NR_utimensat)
378#define __NR_sys_utimensat __NR_utimensat
ths9007f0e2007-09-25 17:50:37 +0000379_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
380 const struct timespec *,tsp,int,flags)
Peter Maydell1acae9f2013-07-02 14:04:12 +0100381#else
382static int sys_utimensat(int dirfd, const char *pathname,
383 const struct timespec times[2], int flags)
384{
385 errno = ENOSYS;
386 return -1;
387}
ths9007f0e2007-09-25 17:50:37 +0000388#endif
Peter Maydell1acae9f2013-07-02 14:04:12 +0100389#endif /* TARGET_NR_utimensat */
aurel323b3f24a2009-04-15 16:12:13 +0000390
391#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000392#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000393
aurel3239b59762008-10-01 21:46:50 +0000394#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000395static int sys_inotify_init(void)
396{
397 return (inotify_init());
398}
aurel3239b59762008-10-01 21:46:50 +0000399#endif
400#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000401static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
402{
403 return (inotify_add_watch(fd, pathname, mask));
404}
aurel3239b59762008-10-01 21:46:50 +0000405#endif
406#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000407static int sys_inotify_rm_watch(int fd, int32_t wd)
408{
aurel328690e422009-04-17 13:50:32 +0000409 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000410}
aurel3239b59762008-10-01 21:46:50 +0000411#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000412#ifdef CONFIG_INOTIFY1
413#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
414static int sys_inotify_init1(int flags)
415{
416 return (inotify_init1(flags));
417}
418#endif
419#endif
aurel323b3f24a2009-04-15 16:12:13 +0000420#else
421/* Userspace can usually survive runtime without inotify */
422#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000423#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000424#undef TARGET_NR_inotify_add_watch
425#undef TARGET_NR_inotify_rm_watch
426#endif /* CONFIG_INOTIFY */
427
Mike Frysingerd8035d42011-02-07 01:05:51 -0500428#if defined(TARGET_NR_ppoll)
429#ifndef __NR_ppoll
430# define __NR_ppoll -1
431#endif
432#define __NR_sys_ppoll __NR_ppoll
433_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
Natanael Copa34d60862014-04-29 13:11:20 +0200434 struct timespec *, timeout, const sigset_t *, sigmask,
Mike Frysingerd8035d42011-02-07 01:05:51 -0500435 size_t, sigsetsize)
436#endif
bellard66fb9762003-03-23 01:06:05 +0000437
Mike Frysinger055e0902011-06-03 17:01:49 -0400438#if defined(TARGET_NR_pselect6)
439#ifndef __NR_pselect6
440# define __NR_pselect6 -1
441#endif
442#define __NR_sys_pselect6 __NR_pselect6
443_syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
444 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
445#endif
446
Peter Maydell163a05a2011-06-27 17:44:52 +0100447#if defined(TARGET_NR_prlimit64)
448#ifndef __NR_prlimit64
449# define __NR_prlimit64 -1
450#endif
451#define __NR_sys_prlimit64 __NR_prlimit64
452/* The glibc rlimit structure may not be that used by the underlying syscall */
453struct host_rlimit64 {
454 uint64_t rlim_cur;
455 uint64_t rlim_max;
456};
457_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
458 const struct host_rlimit64 *, new_limit,
459 struct host_rlimit64 *, old_limit)
460#endif
461
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100462
463#if defined(TARGET_NR_timer_create)
464/* Maxiumum of 32 active POSIX timers allowed at any one time. */
465static timer_t g_posix_timers[32] = { 0, } ;
466
467static inline int next_free_host_timer(void)
468{
469 int k ;
470 /* FIXME: Does finding the next free slot require a lock? */
471 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
472 if (g_posix_timers[k] == 0) {
473 g_posix_timers[k] = (timer_t) 1;
474 return k;
475 }
476 }
477 return -1;
478}
479#endif
480
Riku Voipio48e515d2011-07-12 15:40:51 +0300481/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
Alexander Graf4a1def42012-09-29 15:32:38 +0000482#ifdef TARGET_ARM
Riku Voipio48e515d2011-07-12 15:40:51 +0300483static inline int regpairs_aligned(void *cpu_env) {
484 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
485}
486#elif defined(TARGET_MIPS)
487static inline int regpairs_aligned(void *cpu_env) { return 1; }
Alexander Graf4a1def42012-09-29 15:32:38 +0000488#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
489/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
490 * of registers which translates to the same as ARM/MIPS, because we start with
491 * r3 as arg1 */
492static inline int regpairs_aligned(void *cpu_env) { return 1; }
Riku Voipio48e515d2011-07-12 15:40:51 +0300493#else
494static inline int regpairs_aligned(void *cpu_env) { return 0; }
495#endif
496
thsb92c47c2007-11-01 00:07:38 +0000497#define ERRNO_TABLE_SIZE 1200
498
499/* target_to_host_errno_table[] is initialized from
500 * host_to_target_errno_table[] in syscall_init(). */
501static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
502};
503
ths637947f2007-06-01 12:09:19 +0000504/*
thsfe8f0962007-07-12 10:59:21 +0000505 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000506 * minus the errnos that are not actually generic to all archs.
507 */
thsb92c47c2007-11-01 00:07:38 +0000508static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
Chen Gang08703b92015-09-07 10:35:06 +0800509 [EAGAIN] = TARGET_EAGAIN,
ths637947f2007-06-01 12:09:19 +0000510 [EIDRM] = TARGET_EIDRM,
511 [ECHRNG] = TARGET_ECHRNG,
512 [EL2NSYNC] = TARGET_EL2NSYNC,
513 [EL3HLT] = TARGET_EL3HLT,
514 [EL3RST] = TARGET_EL3RST,
515 [ELNRNG] = TARGET_ELNRNG,
516 [EUNATCH] = TARGET_EUNATCH,
517 [ENOCSI] = TARGET_ENOCSI,
518 [EL2HLT] = TARGET_EL2HLT,
519 [EDEADLK] = TARGET_EDEADLK,
520 [ENOLCK] = TARGET_ENOLCK,
521 [EBADE] = TARGET_EBADE,
522 [EBADR] = TARGET_EBADR,
523 [EXFULL] = TARGET_EXFULL,
524 [ENOANO] = TARGET_ENOANO,
525 [EBADRQC] = TARGET_EBADRQC,
526 [EBADSLT] = TARGET_EBADSLT,
527 [EBFONT] = TARGET_EBFONT,
528 [ENOSTR] = TARGET_ENOSTR,
529 [ENODATA] = TARGET_ENODATA,
530 [ETIME] = TARGET_ETIME,
531 [ENOSR] = TARGET_ENOSR,
532 [ENONET] = TARGET_ENONET,
533 [ENOPKG] = TARGET_ENOPKG,
534 [EREMOTE] = TARGET_EREMOTE,
535 [ENOLINK] = TARGET_ENOLINK,
536 [EADV] = TARGET_EADV,
537 [ESRMNT] = TARGET_ESRMNT,
538 [ECOMM] = TARGET_ECOMM,
539 [EPROTO] = TARGET_EPROTO,
540 [EDOTDOT] = TARGET_EDOTDOT,
541 [EMULTIHOP] = TARGET_EMULTIHOP,
542 [EBADMSG] = TARGET_EBADMSG,
543 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
544 [EOVERFLOW] = TARGET_EOVERFLOW,
545 [ENOTUNIQ] = TARGET_ENOTUNIQ,
546 [EBADFD] = TARGET_EBADFD,
547 [EREMCHG] = TARGET_EREMCHG,
548 [ELIBACC] = TARGET_ELIBACC,
549 [ELIBBAD] = TARGET_ELIBBAD,
550 [ELIBSCN] = TARGET_ELIBSCN,
551 [ELIBMAX] = TARGET_ELIBMAX,
552 [ELIBEXEC] = TARGET_ELIBEXEC,
553 [EILSEQ] = TARGET_EILSEQ,
554 [ENOSYS] = TARGET_ENOSYS,
555 [ELOOP] = TARGET_ELOOP,
556 [ERESTART] = TARGET_ERESTART,
557 [ESTRPIPE] = TARGET_ESTRPIPE,
558 [ENOTEMPTY] = TARGET_ENOTEMPTY,
559 [EUSERS] = TARGET_EUSERS,
560 [ENOTSOCK] = TARGET_ENOTSOCK,
561 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
562 [EMSGSIZE] = TARGET_EMSGSIZE,
563 [EPROTOTYPE] = TARGET_EPROTOTYPE,
564 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
565 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
566 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
567 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
568 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
569 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
570 [EADDRINUSE] = TARGET_EADDRINUSE,
571 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
572 [ENETDOWN] = TARGET_ENETDOWN,
573 [ENETUNREACH] = TARGET_ENETUNREACH,
574 [ENETRESET] = TARGET_ENETRESET,
575 [ECONNABORTED] = TARGET_ECONNABORTED,
576 [ECONNRESET] = TARGET_ECONNRESET,
577 [ENOBUFS] = TARGET_ENOBUFS,
578 [EISCONN] = TARGET_EISCONN,
579 [ENOTCONN] = TARGET_ENOTCONN,
580 [EUCLEAN] = TARGET_EUCLEAN,
581 [ENOTNAM] = TARGET_ENOTNAM,
582 [ENAVAIL] = TARGET_ENAVAIL,
583 [EISNAM] = TARGET_EISNAM,
584 [EREMOTEIO] = TARGET_EREMOTEIO,
585 [ESHUTDOWN] = TARGET_ESHUTDOWN,
586 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
587 [ETIMEDOUT] = TARGET_ETIMEDOUT,
588 [ECONNREFUSED] = TARGET_ECONNREFUSED,
589 [EHOSTDOWN] = TARGET_EHOSTDOWN,
590 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
591 [EALREADY] = TARGET_EALREADY,
592 [EINPROGRESS] = TARGET_EINPROGRESS,
593 [ESTALE] = TARGET_ESTALE,
594 [ECANCELED] = TARGET_ECANCELED,
595 [ENOMEDIUM] = TARGET_ENOMEDIUM,
596 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000597#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000598 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000599#endif
600#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000601 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000602#endif
603#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000604 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000605#endif
606#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000607 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000608#endif
609#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000610 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000611#endif
612#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000613 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000614#endif
thsb92c47c2007-11-01 00:07:38 +0000615};
ths637947f2007-06-01 12:09:19 +0000616
617static inline int host_to_target_errno(int err)
618{
619 if(host_to_target_errno_table[err])
620 return host_to_target_errno_table[err];
621 return err;
622}
623
thsb92c47c2007-11-01 00:07:38 +0000624static inline int target_to_host_errno(int err)
625{
626 if (target_to_host_errno_table[err])
627 return target_to_host_errno_table[err];
628 return err;
629}
630
blueswir1992f48a2007-10-14 16:27:31 +0000631static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000632{
633 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000634 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000635 else
636 return ret;
637}
638
blueswir1992f48a2007-10-14 16:27:31 +0000639static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000640{
blueswir1992f48a2007-10-14 16:27:31 +0000641 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000642}
643
thsb92c47c2007-11-01 00:07:38 +0000644char *target_strerror(int err)
645{
Alexander Graf962b2892011-11-21 12:04:07 +0100646 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
647 return NULL;
648 }
thsb92c47c2007-11-01 00:07:38 +0000649 return strerror(target_to_host_errno(err));
650}
651
Paul Burton8289d112014-06-22 11:25:33 +0100652static inline int host_to_target_sock_type(int host_type)
653{
654 int target_type;
655
656 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
657 case SOCK_DGRAM:
658 target_type = TARGET_SOCK_DGRAM;
659 break;
660 case SOCK_STREAM:
661 target_type = TARGET_SOCK_STREAM;
662 break;
663 default:
664 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
665 break;
666 }
667
668#if defined(SOCK_CLOEXEC)
669 if (host_type & SOCK_CLOEXEC) {
670 target_type |= TARGET_SOCK_CLOEXEC;
671 }
672#endif
673
674#if defined(SOCK_NONBLOCK)
675 if (host_type & SOCK_NONBLOCK) {
676 target_type |= TARGET_SOCK_NONBLOCK;
677 }
678#endif
679
680 return target_type;
681}
682
blueswir1992f48a2007-10-14 16:27:31 +0000683static abi_ulong target_brk;
684static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000685static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000686
blueswir1992f48a2007-10-14 16:27:31 +0000687void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000688{
blueswir14c1de732007-07-07 20:45:44 +0000689 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000690 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000691}
692
vincent4d1de872011-06-14 21:56:33 +0000693//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
694#define DEBUGF_BRK(message, args...)
695
ths0da46a62007-10-20 20:23:07 +0000696/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000697abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000698{
blueswir1992f48a2007-10-14 16:27:31 +0000699 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000700 int new_alloc_size;
701
Paul Brook3a0c6c42012-02-09 19:04:27 +0000702 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000703
vincent4d1de872011-06-14 21:56:33 +0000704 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000705 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000706 return target_brk;
707 }
708 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000709 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
710 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000711 return target_brk;
712 }
bellard31e31b82003-02-18 22:55:36 +0000713
vincent4d1de872011-06-14 21:56:33 +0000714 /* If the new brk is less than the highest page reserved to the
715 * target heap allocation, set it and we're almost done... */
716 if (new_brk <= brk_page) {
717 /* Heap contents are initialized to zero, as for anonymous
718 * mapped pages. */
719 if (new_brk > target_brk) {
720 memset(g2h(target_brk), 0, new_brk - target_brk);
721 }
bellard31e31b82003-02-18 22:55:36 +0000722 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000723 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000724 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000725 }
726
Peter Maydell00faf082011-04-18 16:34:24 +0100727 /* We need to allocate more memory after the brk... Note that
728 * we don't use MAP_FIXED because that will map over the top of
729 * any existing mapping (like the one with the host libc or qemu
730 * itself); instead we treat "mapped but at wrong address" as
731 * a failure and unmap again.
732 */
vincent4d1de872011-06-14 21:56:33 +0000733 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000734 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000735 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100736 MAP_ANON|MAP_PRIVATE, 0, 0));
737
738 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200739 /* Heap contents are initialized to zero, as for anonymous
740 * mapped pages. Technically the new pages are already
741 * initialized to zero since they *are* anonymous mapped
742 * pages, however we have to take care with the contents that
743 * come from the remaining part of the previous page: it may
744 * contains garbage data due to a previous heap usage (grown
745 * then shrunken). */
746 memset(g2h(target_brk), 0, brk_page - target_brk);
747
Peter Maydell00faf082011-04-18 16:34:24 +0100748 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000749 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000750 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
751 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100752 return target_brk;
753 } else if (mapped_addr != -1) {
754 /* Mapped but at wrong address, meaning there wasn't actually
755 * enough space for this brk.
756 */
757 target_munmap(mapped_addr, new_alloc_size);
758 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000759 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000760 }
761 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000762 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100763 }
balrog7ab240a2008-04-26 12:17:34 +0000764
Richard Henderson7dd46c02010-05-03 10:07:49 -0700765#if defined(TARGET_ALPHA)
766 /* We (partially) emulate OSF/1 on Alpha, which requires we
767 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100768 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700769#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100770 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000771 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000772}
773
ths26edcf42007-12-09 02:25:24 +0000774static inline abi_long copy_from_user_fdset(fd_set *fds,
775 abi_ulong target_fds_addr,
776 int n)
bellard31e31b82003-02-18 22:55:36 +0000777{
ths26edcf42007-12-09 02:25:24 +0000778 int i, nw, j, k;
779 abi_ulong b, *target_fds;
780
781 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
782 if (!(target_fds = lock_user(VERIFY_READ,
783 target_fds_addr,
784 sizeof(abi_ulong) * nw,
785 1)))
786 return -TARGET_EFAULT;
787
788 FD_ZERO(fds);
789 k = 0;
790 for (i = 0; i < nw; i++) {
791 /* grab the abi_ulong */
792 __get_user(b, &target_fds[i]);
793 for (j = 0; j < TARGET_ABI_BITS; j++) {
794 /* check the bit inside the abi_ulong */
795 if ((b >> j) & 1)
796 FD_SET(k, fds);
797 k++;
bellard31e31b82003-02-18 22:55:36 +0000798 }
bellard31e31b82003-02-18 22:55:36 +0000799 }
ths26edcf42007-12-09 02:25:24 +0000800
801 unlock_user(target_fds, target_fds_addr, 0);
802
803 return 0;
bellard31e31b82003-02-18 22:55:36 +0000804}
805
Mike Frysinger055e0902011-06-03 17:01:49 -0400806static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
807 abi_ulong target_fds_addr,
808 int n)
809{
810 if (target_fds_addr) {
811 if (copy_from_user_fdset(fds, target_fds_addr, n))
812 return -TARGET_EFAULT;
813 *fds_ptr = fds;
814 } else {
815 *fds_ptr = NULL;
816 }
817 return 0;
818}
819
ths26edcf42007-12-09 02:25:24 +0000820static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
821 const fd_set *fds,
822 int n)
bellard31e31b82003-02-18 22:55:36 +0000823{
bellard31e31b82003-02-18 22:55:36 +0000824 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000825 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000826 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000827
ths26edcf42007-12-09 02:25:24 +0000828 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
829 if (!(target_fds = lock_user(VERIFY_WRITE,
830 target_fds_addr,
831 sizeof(abi_ulong) * nw,
832 0)))
833 return -TARGET_EFAULT;
834
835 k = 0;
836 for (i = 0; i < nw; i++) {
837 v = 0;
838 for (j = 0; j < TARGET_ABI_BITS; j++) {
Andreas Schwab9ab709b2013-04-09 01:02:34 +0000839 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
ths26edcf42007-12-09 02:25:24 +0000840 k++;
bellard31e31b82003-02-18 22:55:36 +0000841 }
ths26edcf42007-12-09 02:25:24 +0000842 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000843 }
ths26edcf42007-12-09 02:25:24 +0000844
845 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
846
847 return 0;
bellard31e31b82003-02-18 22:55:36 +0000848}
849
bellardc596ed12003-07-13 17:32:31 +0000850#if defined(__alpha__)
851#define HOST_HZ 1024
852#else
853#define HOST_HZ 100
854#endif
855
blueswir1992f48a2007-10-14 16:27:31 +0000856static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000857{
858#if HOST_HZ == TARGET_HZ
859 return ticks;
860#else
861 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
862#endif
863}
864
bellard579a97f2007-11-11 14:26:47 +0000865static inline abi_long host_to_target_rusage(abi_ulong target_addr,
866 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000867{
pbrook53a59602006-03-25 19:31:22 +0000868 struct target_rusage *target_rusage;
869
bellard579a97f2007-11-11 14:26:47 +0000870 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
871 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200872 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
873 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
874 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
875 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
876 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
877 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
878 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
879 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
880 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
881 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
882 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
883 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
884 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
885 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
886 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
887 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
888 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
889 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000890 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000891
892 return 0;
bellardb4091862003-05-16 15:39:34 +0000893}
894
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200895static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900896{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200897 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300898 rlim_t result;
899
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200900 target_rlim_swap = tswapal(target_rlim);
901 if (target_rlim_swap == TARGET_RLIM_INFINITY)
902 return RLIM_INFINITY;
903
904 result = target_rlim_swap;
905 if (target_rlim_swap != (rlim_t)result)
906 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300907
908 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900909}
910
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200911static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900912{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200913 abi_ulong target_rlim_swap;
914 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300915
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200916 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300917 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900918 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300919 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200920 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300921
922 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900923}
924
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +0300925static inline int target_to_host_resource(int code)
926{
927 switch (code) {
928 case TARGET_RLIMIT_AS:
929 return RLIMIT_AS;
930 case TARGET_RLIMIT_CORE:
931 return RLIMIT_CORE;
932 case TARGET_RLIMIT_CPU:
933 return RLIMIT_CPU;
934 case TARGET_RLIMIT_DATA:
935 return RLIMIT_DATA;
936 case TARGET_RLIMIT_FSIZE:
937 return RLIMIT_FSIZE;
938 case TARGET_RLIMIT_LOCKS:
939 return RLIMIT_LOCKS;
940 case TARGET_RLIMIT_MEMLOCK:
941 return RLIMIT_MEMLOCK;
942 case TARGET_RLIMIT_MSGQUEUE:
943 return RLIMIT_MSGQUEUE;
944 case TARGET_RLIMIT_NICE:
945 return RLIMIT_NICE;
946 case TARGET_RLIMIT_NOFILE:
947 return RLIMIT_NOFILE;
948 case TARGET_RLIMIT_NPROC:
949 return RLIMIT_NPROC;
950 case TARGET_RLIMIT_RSS:
951 return RLIMIT_RSS;
952 case TARGET_RLIMIT_RTPRIO:
953 return RLIMIT_RTPRIO;
954 case TARGET_RLIMIT_SIGPENDING:
955 return RLIMIT_SIGPENDING;
956 case TARGET_RLIMIT_STACK:
957 return RLIMIT_STACK;
958 default:
959 return code;
960 }
961}
962
ths788f5ec2007-12-09 02:37:05 +0000963static inline abi_long copy_from_user_timeval(struct timeval *tv,
964 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000965{
pbrook53a59602006-03-25 19:31:22 +0000966 struct target_timeval *target_tv;
967
ths788f5ec2007-12-09 02:37:05 +0000968 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000969 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000970
971 __get_user(tv->tv_sec, &target_tv->tv_sec);
972 __get_user(tv->tv_usec, &target_tv->tv_usec);
973
974 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000975
976 return 0;
bellard31e31b82003-02-18 22:55:36 +0000977}
978
ths788f5ec2007-12-09 02:37:05 +0000979static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
980 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000981{
pbrook53a59602006-03-25 19:31:22 +0000982 struct target_timeval *target_tv;
983
ths788f5ec2007-12-09 02:37:05 +0000984 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000985 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000986
987 __put_user(tv->tv_sec, &target_tv->tv_sec);
988 __put_user(tv->tv_usec, &target_tv->tv_usec);
989
990 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000991
992 return 0;
bellard31e31b82003-02-18 22:55:36 +0000993}
994
Paul Burtonef4467e2014-06-22 11:25:40 +0100995static inline abi_long copy_from_user_timezone(struct timezone *tz,
996 abi_ulong target_tz_addr)
997{
998 struct target_timezone *target_tz;
999
1000 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1001 return -TARGET_EFAULT;
1002 }
1003
1004 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1005 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1006
1007 unlock_user_struct(target_tz, target_tz_addr, 0);
1008
1009 return 0;
1010}
1011
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001012#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1013#include <mqueue.h>
1014
aurel3224e10032009-04-15 16:11:43 +00001015static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1016 abi_ulong target_mq_attr_addr)
1017{
1018 struct target_mq_attr *target_mq_attr;
1019
1020 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1021 target_mq_attr_addr, 1))
1022 return -TARGET_EFAULT;
1023
1024 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1025 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1026 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1027 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1028
1029 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1030
1031 return 0;
1032}
1033
1034static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1035 const struct mq_attr *attr)
1036{
1037 struct target_mq_attr *target_mq_attr;
1038
1039 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1040 target_mq_attr_addr, 0))
1041 return -TARGET_EFAULT;
1042
1043 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1044 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1045 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1046 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1047
1048 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1049
1050 return 0;
1051}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001052#endif
bellard31e31b82003-02-18 22:55:36 +00001053
Mike Frysinger055e0902011-06-03 17:01:49 -04001054#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001055/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001056static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001057 abi_ulong rfd_addr, abi_ulong wfd_addr,
1058 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001059{
1060 fd_set rfds, wfds, efds;
1061 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1062 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001063 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001064
Mike Frysinger055e0902011-06-03 17:01:49 -04001065 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1066 if (ret) {
1067 return ret;
pbrook53a59602006-03-25 19:31:22 +00001068 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001069 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1070 if (ret) {
1071 return ret;
pbrook53a59602006-03-25 19:31:22 +00001072 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001073 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1074 if (ret) {
1075 return ret;
pbrook53a59602006-03-25 19:31:22 +00001076 }
ths3b46e622007-09-17 08:09:54 +00001077
ths26edcf42007-12-09 02:25:24 +00001078 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001079 if (copy_from_user_timeval(&tv, target_tv_addr))
1080 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001081 tv_ptr = &tv;
1082 } else {
1083 tv_ptr = NULL;
1084 }
ths26edcf42007-12-09 02:25:24 +00001085
bellard31e31b82003-02-18 22:55:36 +00001086 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +00001087
ths26edcf42007-12-09 02:25:24 +00001088 if (!is_error(ret)) {
1089 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1090 return -TARGET_EFAULT;
1091 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1092 return -TARGET_EFAULT;
1093 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1094 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001095
ths788f5ec2007-12-09 02:37:05 +00001096 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1097 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001098 }
bellard579a97f2007-11-11 14:26:47 +00001099
bellard31e31b82003-02-18 22:55:36 +00001100 return ret;
1101}
Mike Frysinger055e0902011-06-03 17:01:49 -04001102#endif
bellard31e31b82003-02-18 22:55:36 +00001103
Riku Voipio099d6b02009-05-05 12:10:04 +03001104static abi_long do_pipe2(int host_pipe[], int flags)
1105{
1106#ifdef CONFIG_PIPE2
1107 return pipe2(host_pipe, flags);
1108#else
1109 return -ENOSYS;
1110#endif
1111}
1112
Richard Hendersonfb41a662010-05-03 10:07:52 -07001113static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1114 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001115{
1116 int host_pipe[2];
1117 abi_long ret;
1118 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1119
1120 if (is_error(ret))
1121 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001122
1123 /* Several targets have special calling conventions for the original
1124 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1125 if (!is_pipe2) {
1126#if defined(TARGET_ALPHA)
1127 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1128 return host_pipe[0];
1129#elif defined(TARGET_MIPS)
1130 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1131 return host_pipe[0];
1132#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001133 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001134 return host_pipe[0];
Peter Maydell82f05b62013-07-06 17:39:48 +01001135#elif defined(TARGET_SPARC)
1136 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1137 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001138#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001139 }
1140
Riku Voipio099d6b02009-05-05 12:10:04 +03001141 if (put_user_s32(host_pipe[0], pipedes)
1142 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1143 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001144 return get_errno(ret);
1145}
1146
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001147static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1148 abi_ulong target_addr,
1149 socklen_t len)
1150{
1151 struct target_ip_mreqn *target_smreqn;
1152
1153 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1154 if (!target_smreqn)
1155 return -TARGET_EFAULT;
1156 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1157 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1158 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001159 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001160 unlock_user(target_smreqn, target_addr, 0);
1161
1162 return 0;
1163}
1164
bellard579a97f2007-11-11 14:26:47 +00001165static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1166 abi_ulong target_addr,
1167 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001168{
aurel32607175e2009-04-15 16:11:59 +00001169 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1170 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001171 struct target_sockaddr *target_saddr;
1172
bellard579a97f2007-11-11 14:26:47 +00001173 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1174 if (!target_saddr)
1175 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001176
1177 sa_family = tswap16(target_saddr->sa_family);
1178
1179 /* Oops. The caller might send a incomplete sun_path; sun_path
1180 * must be terminated by \0 (see the manual page), but
1181 * unfortunately it is quite common to specify sockaddr_un
1182 * length as "strlen(x->sun_path)" while it should be
1183 * "strlen(...) + 1". We'll fix that here if needed.
1184 * Linux kernel has a similar feature.
1185 */
1186
1187 if (sa_family == AF_UNIX) {
1188 if (len < unix_maxlen && len > 0) {
1189 char *cp = (char*)target_saddr;
1190
1191 if ( cp[len-1] && !cp[len] )
1192 len++;
1193 }
1194 if (len > unix_maxlen)
1195 len = unix_maxlen;
1196 }
1197
pbrook53a59602006-03-25 19:31:22 +00001198 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001199 addr->sa_family = sa_family;
Joakim Tjernlund33a29b52014-07-12 15:47:07 +02001200 if (sa_family == AF_PACKET) {
1201 struct target_sockaddr_ll *lladdr;
1202
1203 lladdr = (struct target_sockaddr_ll *)addr;
1204 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1205 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1206 }
pbrook53a59602006-03-25 19:31:22 +00001207 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001208
1209 return 0;
bellard7854b052003-03-29 17:22:23 +00001210}
1211
bellard579a97f2007-11-11 14:26:47 +00001212static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1213 struct sockaddr *addr,
1214 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001215{
pbrook53a59602006-03-25 19:31:22 +00001216 struct target_sockaddr *target_saddr;
1217
bellard579a97f2007-11-11 14:26:47 +00001218 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1219 if (!target_saddr)
1220 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001221 memcpy(target_saddr, addr, len);
1222 target_saddr->sa_family = tswap16(addr->sa_family);
1223 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001224
1225 return 0;
bellard7854b052003-03-29 17:22:23 +00001226}
1227
bellard5a4a8982007-11-11 17:39:18 +00001228static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1229 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001230{
1231 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001232 abi_long msg_controllen;
1233 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001234 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001235 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001236
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001237 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001238 if (msg_controllen < sizeof (struct target_cmsghdr))
1239 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001240 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001241 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001242 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001243 if (!target_cmsg)
1244 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001245
1246 while (cmsg && target_cmsg) {
1247 void *data = CMSG_DATA(cmsg);
1248 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1249
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001250 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001251 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1252
1253 space += CMSG_SPACE(len);
1254 if (space > msgh->msg_controllen) {
1255 space -= CMSG_SPACE(len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001256 /* This is a QEMU bug, since we allocated the payload
1257 * area ourselves (unlike overflow in host-to-target
1258 * conversion, which is just the guest giving us a buffer
1259 * that's too small). It can't happen for the payload types
1260 * we currently support; if it becomes an issue in future
1261 * we would need to improve our allocation strategy to
1262 * something more intelligent than "twice the size of the
1263 * target buffer we're reading from".
1264 */
bellard31febb72005-12-18 20:03:27 +00001265 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001266 break;
1267 }
1268
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001269 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1270 cmsg->cmsg_level = SOL_SOCKET;
1271 } else {
1272 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1273 }
bellard7854b052003-03-29 17:22:23 +00001274 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1275 cmsg->cmsg_len = CMSG_LEN(len);
1276
Alex Suykov30b8b682014-12-23 07:52:58 +02001277 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001278 int *fd = (int *)data;
1279 int *target_fd = (int *)target_data;
1280 int i, numfds = len / sizeof(int);
1281
Peter Maydell876e23c2015-05-26 19:46:32 +01001282 for (i = 0; i < numfds; i++) {
1283 __get_user(fd[i], target_fd + i);
1284 }
Alex Suykov30b8b682014-12-23 07:52:58 +02001285 } else if (cmsg->cmsg_level == SOL_SOCKET
1286 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1287 struct ucred *cred = (struct ucred *)data;
1288 struct target_ucred *target_cred =
1289 (struct target_ucred *)target_data;
1290
Peter Maydell876e23c2015-05-26 19:46:32 +01001291 __get_user(cred->pid, &target_cred->pid);
1292 __get_user(cred->uid, &target_cred->uid);
1293 __get_user(cred->gid, &target_cred->gid);
Alex Suykov30b8b682014-12-23 07:52:58 +02001294 } else {
1295 gemu_log("Unsupported ancillary data: %d/%d\n",
1296 cmsg->cmsg_level, cmsg->cmsg_type);
1297 memcpy(data, target_data, len);
bellard7854b052003-03-29 17:22:23 +00001298 }
1299
1300 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001301 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1302 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00001303 }
bellard5a4a8982007-11-11 17:39:18 +00001304 unlock_user(target_cmsg, target_cmsg_addr, 0);
1305 the_end:
bellard7854b052003-03-29 17:22:23 +00001306 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001307 return 0;
bellard7854b052003-03-29 17:22:23 +00001308}
1309
bellard5a4a8982007-11-11 17:39:18 +00001310static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1311 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001312{
1313 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001314 abi_long msg_controllen;
1315 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001316 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001317 socklen_t space = 0;
1318
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001319 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001320 if (msg_controllen < sizeof (struct target_cmsghdr))
1321 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001322 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001323 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001324 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001325 if (!target_cmsg)
1326 return -TARGET_EFAULT;
1327
bellard7854b052003-03-29 17:22:23 +00001328 while (cmsg && target_cmsg) {
1329 void *data = CMSG_DATA(cmsg);
1330 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1331
1332 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
Peter Maydellc2aeb252015-05-26 19:46:31 +01001333 int tgt_len, tgt_space;
bellard7854b052003-03-29 17:22:23 +00001334
Peter Maydellc2aeb252015-05-26 19:46:31 +01001335 /* We never copy a half-header but may copy half-data;
1336 * this is Linux's behaviour in put_cmsg(). Note that
1337 * truncation here is a guest problem (which we report
1338 * to the guest via the CTRUNC bit), unlike truncation
1339 * in target_to_host_cmsg, which is a QEMU bug.
1340 */
1341 if (msg_controllen < sizeof(struct cmsghdr)) {
1342 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
bellard7854b052003-03-29 17:22:23 +00001343 break;
1344 }
1345
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001346 if (cmsg->cmsg_level == SOL_SOCKET) {
1347 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1348 } else {
1349 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1350 }
bellard7854b052003-03-29 17:22:23 +00001351 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
bellard7854b052003-03-29 17:22:23 +00001352
Peter Maydellc2aeb252015-05-26 19:46:31 +01001353 tgt_len = TARGET_CMSG_LEN(len);
1354
1355 /* Payload types which need a different size of payload on
1356 * the target must adjust tgt_len here.
1357 */
1358 switch (cmsg->cmsg_level) {
1359 case SOL_SOCKET:
1360 switch (cmsg->cmsg_type) {
1361 case SO_TIMESTAMP:
1362 tgt_len = sizeof(struct target_timeval);
1363 break;
1364 default:
1365 break;
1366 }
1367 default:
1368 break;
1369 }
1370
1371 if (msg_controllen < tgt_len) {
1372 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1373 tgt_len = msg_controllen;
1374 }
1375
1376 /* We must now copy-and-convert len bytes of payload
1377 * into tgt_len bytes of destination space. Bear in mind
1378 * that in both source and destination we may be dealing
1379 * with a truncated value!
1380 */
Huw Davies52b65492014-04-17 14:02:47 +01001381 switch (cmsg->cmsg_level) {
1382 case SOL_SOCKET:
1383 switch (cmsg->cmsg_type) {
1384 case SCM_RIGHTS:
1385 {
1386 int *fd = (int *)data;
1387 int *target_fd = (int *)target_data;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001388 int i, numfds = tgt_len / sizeof(int);
bellard7854b052003-03-29 17:22:23 +00001389
Peter Maydell876e23c2015-05-26 19:46:32 +01001390 for (i = 0; i < numfds; i++) {
1391 __put_user(fd[i], target_fd + i);
1392 }
Huw Davies52b65492014-04-17 14:02:47 +01001393 break;
1394 }
1395 case SO_TIMESTAMP:
1396 {
1397 struct timeval *tv = (struct timeval *)data;
1398 struct target_timeval *target_tv =
1399 (struct target_timeval *)target_data;
Jing Huangaebf5bc2012-07-24 14:01:42 +00001400
Peter Maydellc2aeb252015-05-26 19:46:31 +01001401 if (len != sizeof(struct timeval) ||
1402 tgt_len != sizeof(struct target_timeval)) {
Huw Davies52b65492014-04-17 14:02:47 +01001403 goto unimplemented;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001404 }
Huw Davies52b65492014-04-17 14:02:47 +01001405
1406 /* copy struct timeval to target */
Peter Maydell876e23c2015-05-26 19:46:32 +01001407 __put_user(tv->tv_sec, &target_tv->tv_sec);
1408 __put_user(tv->tv_usec, &target_tv->tv_usec);
Huw Davies52b65492014-04-17 14:02:47 +01001409 break;
1410 }
Huw Davies4bc29752014-04-17 14:02:48 +01001411 case SCM_CREDENTIALS:
1412 {
1413 struct ucred *cred = (struct ucred *)data;
1414 struct target_ucred *target_cred =
1415 (struct target_ucred *)target_data;
1416
1417 __put_user(cred->pid, &target_cred->pid);
1418 __put_user(cred->uid, &target_cred->uid);
1419 __put_user(cred->gid, &target_cred->gid);
1420 break;
1421 }
Huw Davies52b65492014-04-17 14:02:47 +01001422 default:
1423 goto unimplemented;
1424 }
1425 break;
1426
1427 default:
1428 unimplemented:
Jing Huangaebf5bc2012-07-24 14:01:42 +00001429 gemu_log("Unsupported ancillary data: %d/%d\n",
1430 cmsg->cmsg_level, cmsg->cmsg_type);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001431 memcpy(target_data, data, MIN(len, tgt_len));
1432 if (tgt_len > len) {
1433 memset(target_data + len, 0, tgt_len - len);
1434 }
bellard7854b052003-03-29 17:22:23 +00001435 }
1436
Peter Maydellc2aeb252015-05-26 19:46:31 +01001437 target_cmsg->cmsg_len = tswapal(tgt_len);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001438 tgt_space = TARGET_CMSG_SPACE(len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001439 if (msg_controllen < tgt_space) {
1440 tgt_space = msg_controllen;
1441 }
1442 msg_controllen -= tgt_space;
1443 space += tgt_space;
bellard7854b052003-03-29 17:22:23 +00001444 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001445 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1446 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00001447 }
bellard5a4a8982007-11-11 17:39:18 +00001448 unlock_user(target_cmsg, target_cmsg_addr, space);
1449 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001450 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001451 return 0;
bellard7854b052003-03-29 17:22:23 +00001452}
1453
ths0da46a62007-10-20 20:23:07 +00001454/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001455static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001456 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001457{
blueswir1992f48a2007-10-14 16:27:31 +00001458 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001459 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001460 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001461 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001462
bellard8853f862004-02-22 14:57:26 +00001463 switch(level) {
1464 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001465 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001466 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001467 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001468
bellard2f619692007-11-16 10:46:05 +00001469 if (get_user_u32(val, optval_addr))
1470 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001471 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1472 break;
1473 case SOL_IP:
1474 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001475 case IP_TOS:
1476 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001477 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001478 case IP_ROUTER_ALERT:
1479 case IP_RECVOPTS:
1480 case IP_RETOPTS:
1481 case IP_PKTINFO:
1482 case IP_MTU_DISCOVER:
1483 case IP_RECVERR:
1484 case IP_RECVTOS:
1485#ifdef IP_FREEBIND
1486 case IP_FREEBIND:
1487#endif
1488 case IP_MULTICAST_TTL:
1489 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001490 val = 0;
1491 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001492 if (get_user_u32(val, optval_addr))
1493 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001494 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001495 if (get_user_u8(val, optval_addr))
1496 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001497 }
1498 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1499 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001500 case IP_ADD_MEMBERSHIP:
1501 case IP_DROP_MEMBERSHIP:
1502 if (optlen < sizeof (struct target_ip_mreq) ||
1503 optlen > sizeof (struct target_ip_mreqn))
1504 return -TARGET_EINVAL;
1505
1506 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1507 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1508 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1509 break;
1510
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001511 case IP_BLOCK_SOURCE:
1512 case IP_UNBLOCK_SOURCE:
1513 case IP_ADD_SOURCE_MEMBERSHIP:
1514 case IP_DROP_SOURCE_MEMBERSHIP:
1515 if (optlen != sizeof (struct target_ip_mreq_source))
1516 return -TARGET_EINVAL;
1517
1518 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1519 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1520 unlock_user (ip_mreq_source, optval_addr, 0);
1521 break;
1522
bellard8853f862004-02-22 14:57:26 +00001523 default:
1524 goto unimplemented;
1525 }
1526 break;
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02001527 case SOL_IPV6:
1528 switch (optname) {
1529 case IPV6_MTU_DISCOVER:
1530 case IPV6_MTU:
1531 case IPV6_V6ONLY:
1532 case IPV6_RECVPKTINFO:
1533 val = 0;
1534 if (optlen < sizeof(uint32_t)) {
1535 return -TARGET_EINVAL;
1536 }
1537 if (get_user_u32(val, optval_addr)) {
1538 return -TARGET_EFAULT;
1539 }
1540 ret = get_errno(setsockopt(sockfd, level, optname,
1541 &val, sizeof(val)));
1542 break;
1543 default:
1544 goto unimplemented;
1545 }
1546 break;
Jing Huang920394d2012-07-24 13:59:23 +00001547 case SOL_RAW:
1548 switch (optname) {
1549 case ICMP_FILTER:
1550 /* struct icmp_filter takes an u32 value */
1551 if (optlen < sizeof(uint32_t)) {
1552 return -TARGET_EINVAL;
1553 }
1554
1555 if (get_user_u32(val, optval_addr)) {
1556 return -TARGET_EFAULT;
1557 }
1558 ret = get_errno(setsockopt(sockfd, level, optname,
1559 &val, sizeof(val)));
1560 break;
1561
1562 default:
1563 goto unimplemented;
1564 }
1565 break;
bellard3532fa72006-06-24 15:06:03 +00001566 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001567 switch (optname) {
Laurent Vivier1b09aeb2013-01-01 08:24:11 +00001568 case TARGET_SO_RCVTIMEO:
1569 {
1570 struct timeval tv;
1571
1572 optname = SO_RCVTIMEO;
1573
1574set_timeout:
1575 if (optlen != sizeof(struct target_timeval)) {
1576 return -TARGET_EINVAL;
1577 }
1578
1579 if (copy_from_user_timeval(&tv, optval_addr)) {
1580 return -TARGET_EFAULT;
1581 }
1582
1583 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1584 &tv, sizeof(tv)));
1585 return ret;
1586 }
1587 case TARGET_SO_SNDTIMEO:
1588 optname = SO_SNDTIMEO;
1589 goto set_timeout;
Laurent Vivierf57d4192013-08-30 01:46:41 +02001590 case TARGET_SO_ATTACH_FILTER:
1591 {
1592 struct target_sock_fprog *tfprog;
1593 struct target_sock_filter *tfilter;
1594 struct sock_fprog fprog;
1595 struct sock_filter *filter;
1596 int i;
1597
1598 if (optlen != sizeof(*tfprog)) {
1599 return -TARGET_EINVAL;
1600 }
1601 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
1602 return -TARGET_EFAULT;
1603 }
1604 if (!lock_user_struct(VERIFY_READ, tfilter,
1605 tswapal(tfprog->filter), 0)) {
1606 unlock_user_struct(tfprog, optval_addr, 1);
1607 return -TARGET_EFAULT;
1608 }
1609
1610 fprog.len = tswap16(tfprog->len);
1611 filter = malloc(fprog.len * sizeof(*filter));
1612 if (filter == NULL) {
1613 unlock_user_struct(tfilter, tfprog->filter, 1);
1614 unlock_user_struct(tfprog, optval_addr, 1);
1615 return -TARGET_ENOMEM;
1616 }
1617 for (i = 0; i < fprog.len; i++) {
1618 filter[i].code = tswap16(tfilter[i].code);
1619 filter[i].jt = tfilter[i].jt;
1620 filter[i].jf = tfilter[i].jf;
1621 filter[i].k = tswap32(tfilter[i].k);
1622 }
1623 fprog.filter = filter;
1624
1625 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
1626 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
1627 free(filter);
1628
1629 unlock_user_struct(tfilter, tfprog->filter, 1);
1630 unlock_user_struct(tfprog, optval_addr, 1);
1631 return ret;
1632 }
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02001633 case TARGET_SO_BINDTODEVICE:
1634 {
1635 char *dev_ifname, *addr_ifname;
1636
1637 if (optlen > IFNAMSIZ - 1) {
1638 optlen = IFNAMSIZ - 1;
1639 }
1640 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1641 if (!dev_ifname) {
1642 return -TARGET_EFAULT;
1643 }
1644 optname = SO_BINDTODEVICE;
1645 addr_ifname = alloca(IFNAMSIZ);
1646 memcpy(addr_ifname, dev_ifname, optlen);
1647 addr_ifname[optlen] = 0;
1648 ret = get_errno(setsockopt(sockfd, level, optname, addr_ifname, optlen));
1649 unlock_user (dev_ifname, optval_addr, 0);
1650 return ret;
1651 }
bellard8853f862004-02-22 14:57:26 +00001652 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001653 case TARGET_SO_DEBUG:
1654 optname = SO_DEBUG;
1655 break;
1656 case TARGET_SO_REUSEADDR:
1657 optname = SO_REUSEADDR;
1658 break;
1659 case TARGET_SO_TYPE:
1660 optname = SO_TYPE;
1661 break;
1662 case TARGET_SO_ERROR:
1663 optname = SO_ERROR;
1664 break;
1665 case TARGET_SO_DONTROUTE:
1666 optname = SO_DONTROUTE;
1667 break;
1668 case TARGET_SO_BROADCAST:
1669 optname = SO_BROADCAST;
1670 break;
1671 case TARGET_SO_SNDBUF:
1672 optname = SO_SNDBUF;
1673 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01001674 case TARGET_SO_SNDBUFFORCE:
1675 optname = SO_SNDBUFFORCE;
1676 break;
bellard3532fa72006-06-24 15:06:03 +00001677 case TARGET_SO_RCVBUF:
1678 optname = SO_RCVBUF;
1679 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01001680 case TARGET_SO_RCVBUFFORCE:
1681 optname = SO_RCVBUFFORCE;
1682 break;
bellard3532fa72006-06-24 15:06:03 +00001683 case TARGET_SO_KEEPALIVE:
1684 optname = SO_KEEPALIVE;
1685 break;
1686 case TARGET_SO_OOBINLINE:
1687 optname = SO_OOBINLINE;
1688 break;
1689 case TARGET_SO_NO_CHECK:
1690 optname = SO_NO_CHECK;
1691 break;
1692 case TARGET_SO_PRIORITY:
1693 optname = SO_PRIORITY;
1694 break;
bellard5e83e8e2005-03-01 22:32:06 +00001695#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001696 case TARGET_SO_BSDCOMPAT:
1697 optname = SO_BSDCOMPAT;
1698 break;
bellard5e83e8e2005-03-01 22:32:06 +00001699#endif
bellard3532fa72006-06-24 15:06:03 +00001700 case TARGET_SO_PASSCRED:
1701 optname = SO_PASSCRED;
1702 break;
Paul Burton82d0fe62014-06-22 11:25:36 +01001703 case TARGET_SO_PASSSEC:
1704 optname = SO_PASSSEC;
1705 break;
bellard3532fa72006-06-24 15:06:03 +00001706 case TARGET_SO_TIMESTAMP:
1707 optname = SO_TIMESTAMP;
1708 break;
1709 case TARGET_SO_RCVLOWAT:
1710 optname = SO_RCVLOWAT;
1711 break;
bellard8853f862004-02-22 14:57:26 +00001712 break;
1713 default:
1714 goto unimplemented;
1715 }
bellard3532fa72006-06-24 15:06:03 +00001716 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001717 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001718
bellard2f619692007-11-16 10:46:05 +00001719 if (get_user_u32(val, optval_addr))
1720 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001721 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001722 break;
bellard7854b052003-03-29 17:22:23 +00001723 default:
bellard8853f862004-02-22 14:57:26 +00001724 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02001725 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001726 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001727 }
bellard8853f862004-02-22 14:57:26 +00001728 return ret;
bellard7854b052003-03-29 17:22:23 +00001729}
1730
ths0da46a62007-10-20 20:23:07 +00001731/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001732static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001733 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001734{
blueswir1992f48a2007-10-14 16:27:31 +00001735 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001736 int len, val;
1737 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001738
1739 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001740 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001741 level = SOL_SOCKET;
1742 switch (optname) {
1743 /* These don't just return a single integer */
1744 case TARGET_SO_LINGER:
1745 case TARGET_SO_RCVTIMEO:
1746 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001747 case TARGET_SO_PEERNAME:
1748 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02001749 case TARGET_SO_PEERCRED: {
1750 struct ucred cr;
1751 socklen_t crlen;
1752 struct target_ucred *tcr;
1753
1754 if (get_user_u32(len, optlen)) {
1755 return -TARGET_EFAULT;
1756 }
1757 if (len < 0) {
1758 return -TARGET_EINVAL;
1759 }
1760
1761 crlen = sizeof(cr);
1762 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1763 &cr, &crlen));
1764 if (ret < 0) {
1765 return ret;
1766 }
1767 if (len > crlen) {
1768 len = crlen;
1769 }
1770 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1771 return -TARGET_EFAULT;
1772 }
1773 __put_user(cr.pid, &tcr->pid);
1774 __put_user(cr.uid, &tcr->uid);
1775 __put_user(cr.gid, &tcr->gid);
1776 unlock_user_struct(tcr, optval_addr, 1);
1777 if (put_user_u32(len, optlen)) {
1778 return -TARGET_EFAULT;
1779 }
1780 break;
1781 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001782 /* Options with 'int' argument. */
1783 case TARGET_SO_DEBUG:
1784 optname = SO_DEBUG;
1785 goto int_case;
1786 case TARGET_SO_REUSEADDR:
1787 optname = SO_REUSEADDR;
1788 goto int_case;
1789 case TARGET_SO_TYPE:
1790 optname = SO_TYPE;
1791 goto int_case;
1792 case TARGET_SO_ERROR:
1793 optname = SO_ERROR;
1794 goto int_case;
1795 case TARGET_SO_DONTROUTE:
1796 optname = SO_DONTROUTE;
1797 goto int_case;
1798 case TARGET_SO_BROADCAST:
1799 optname = SO_BROADCAST;
1800 goto int_case;
1801 case TARGET_SO_SNDBUF:
1802 optname = SO_SNDBUF;
1803 goto int_case;
1804 case TARGET_SO_RCVBUF:
1805 optname = SO_RCVBUF;
1806 goto int_case;
1807 case TARGET_SO_KEEPALIVE:
1808 optname = SO_KEEPALIVE;
1809 goto int_case;
1810 case TARGET_SO_OOBINLINE:
1811 optname = SO_OOBINLINE;
1812 goto int_case;
1813 case TARGET_SO_NO_CHECK:
1814 optname = SO_NO_CHECK;
1815 goto int_case;
1816 case TARGET_SO_PRIORITY:
1817 optname = SO_PRIORITY;
1818 goto int_case;
1819#ifdef SO_BSDCOMPAT
1820 case TARGET_SO_BSDCOMPAT:
1821 optname = SO_BSDCOMPAT;
1822 goto int_case;
1823#endif
1824 case TARGET_SO_PASSCRED:
1825 optname = SO_PASSCRED;
1826 goto int_case;
1827 case TARGET_SO_TIMESTAMP:
1828 optname = SO_TIMESTAMP;
1829 goto int_case;
1830 case TARGET_SO_RCVLOWAT:
1831 optname = SO_RCVLOWAT;
1832 goto int_case;
Paul Burtonaec1ca42014-06-22 11:25:34 +01001833 case TARGET_SO_ACCEPTCONN:
1834 optname = SO_ACCEPTCONN;
1835 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001836 default:
bellard2efbe912005-07-23 15:10:20 +00001837 goto int_case;
1838 }
1839 break;
1840 case SOL_TCP:
1841 /* TCP options all take an 'int' value. */
1842 int_case:
bellard2f619692007-11-16 10:46:05 +00001843 if (get_user_u32(len, optlen))
1844 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001845 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001846 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001847 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00001848 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1849 if (ret < 0)
1850 return ret;
Paul Burton8289d112014-06-22 11:25:33 +01001851 if (optname == SO_TYPE) {
1852 val = host_to_target_sock_type(val);
1853 }
bellard2efbe912005-07-23 15:10:20 +00001854 if (len > lv)
1855 len = lv;
bellard2f619692007-11-16 10:46:05 +00001856 if (len == 4) {
1857 if (put_user_u32(val, optval_addr))
1858 return -TARGET_EFAULT;
1859 } else {
1860 if (put_user_u8(val, optval_addr))
1861 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001862 }
bellard2f619692007-11-16 10:46:05 +00001863 if (put_user_u32(len, optlen))
1864 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001865 break;
1866 case SOL_IP:
1867 switch(optname) {
1868 case IP_TOS:
1869 case IP_TTL:
1870 case IP_HDRINCL:
1871 case IP_ROUTER_ALERT:
1872 case IP_RECVOPTS:
1873 case IP_RETOPTS:
1874 case IP_PKTINFO:
1875 case IP_MTU_DISCOVER:
1876 case IP_RECVERR:
1877 case IP_RECVTOS:
1878#ifdef IP_FREEBIND
1879 case IP_FREEBIND:
1880#endif
1881 case IP_MULTICAST_TTL:
1882 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001883 if (get_user_u32(len, optlen))
1884 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001885 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001886 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001887 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001888 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1889 if (ret < 0)
1890 return ret;
bellard2efbe912005-07-23 15:10:20 +00001891 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001892 len = 1;
bellard2f619692007-11-16 10:46:05 +00001893 if (put_user_u32(len, optlen)
1894 || put_user_u8(val, optval_addr))
1895 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001896 } else {
bellard2efbe912005-07-23 15:10:20 +00001897 if (len > sizeof(int))
1898 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001899 if (put_user_u32(len, optlen)
1900 || put_user_u32(val, optval_addr))
1901 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001902 }
bellard8853f862004-02-22 14:57:26 +00001903 break;
bellard2efbe912005-07-23 15:10:20 +00001904 default:
thsc02f4992007-12-18 02:39:59 +00001905 ret = -TARGET_ENOPROTOOPT;
1906 break;
bellard8853f862004-02-22 14:57:26 +00001907 }
1908 break;
1909 default:
1910 unimplemented:
1911 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1912 level, optname);
thsc02f4992007-12-18 02:39:59 +00001913 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001914 break;
1915 }
1916 return ret;
bellard7854b052003-03-29 17:22:23 +00001917}
1918
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001919static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1920 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001921{
1922 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001923 struct iovec *vec;
1924 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00001925 int i;
Peter Maydell501bb4b2014-02-17 18:55:33 +00001926 int err = 0;
Tom Musta29560a62014-08-12 13:53:43 -05001927 bool bad_address = false;
pbrook53a59602006-03-25 19:31:22 +00001928
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001929 if (count == 0) {
1930 errno = 0;
1931 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001932 }
Peter Maydelldfae8e02013-02-08 07:58:41 +00001933 if (count < 0 || count > IOV_MAX) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001934 errno = EINVAL;
1935 return NULL;
1936 }
1937
1938 vec = calloc(count, sizeof(struct iovec));
1939 if (vec == NULL) {
1940 errno = ENOMEM;
1941 return NULL;
1942 }
1943
1944 target_vec = lock_user(VERIFY_READ, target_addr,
1945 count * sizeof(struct target_iovec), 1);
1946 if (target_vec == NULL) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00001947 err = EFAULT;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001948 goto fail2;
1949 }
1950
1951 /* ??? If host page size > target page size, this will result in a
1952 value larger than what we can actually support. */
1953 max_len = 0x7fffffff & TARGET_PAGE_MASK;
1954 total_len = 0;
1955
1956 for (i = 0; i < count; i++) {
1957 abi_ulong base = tswapal(target_vec[i].iov_base);
1958 abi_long len = tswapal(target_vec[i].iov_len);
1959
1960 if (len < 0) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00001961 err = EINVAL;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001962 goto fail;
1963 } else if (len == 0) {
1964 /* Zero length pointer is ignored. */
1965 vec[i].iov_base = 0;
1966 } else {
1967 vec[i].iov_base = lock_user(type, base, len, copy);
Tom Musta29560a62014-08-12 13:53:43 -05001968 /* If the first buffer pointer is bad, this is a fault. But
1969 * subsequent bad buffers will result in a partial write; this
1970 * is realized by filling the vector with null pointers and
1971 * zero lengths. */
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001972 if (!vec[i].iov_base) {
Tom Musta29560a62014-08-12 13:53:43 -05001973 if (i == 0) {
1974 err = EFAULT;
1975 goto fail;
1976 } else {
1977 bad_address = true;
1978 }
1979 }
1980 if (bad_address) {
1981 len = 0;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001982 }
1983 if (len > max_len - total_len) {
1984 len = max_len - total_len;
1985 }
1986 }
1987 vec[i].iov_len = len;
1988 total_len += len;
1989 }
1990
1991 unlock_user(target_vec, target_addr, 0);
1992 return vec;
1993
1994 fail:
Chen Gang S7eff5182015-01-23 18:01:09 +08001995 while (--i >= 0) {
1996 if (tswapal(target_vec[i].iov_len) > 0) {
1997 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
1998 }
1999 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002000 unlock_user(target_vec, target_addr, 0);
Peter Maydell501bb4b2014-02-17 18:55:33 +00002001 fail2:
2002 free(vec);
2003 errno = err;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002004 return NULL;
pbrook53a59602006-03-25 19:31:22 +00002005}
2006
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002007static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
2008 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00002009{
2010 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00002011 int i;
2012
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002013 target_vec = lock_user(VERIFY_READ, target_addr,
2014 count * sizeof(struct target_iovec), 1);
2015 if (target_vec) {
2016 for (i = 0; i < count; i++) {
2017 abi_ulong base = tswapal(target_vec[i].iov_base);
Chen Gang S71ec7ce2015-01-23 18:07:50 +08002018 abi_long len = tswapal(target_vec[i].iov_len);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002019 if (len < 0) {
2020 break;
2021 }
balrogd732dcb2008-10-28 10:21:03 +00002022 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
2023 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002024 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00002025 }
bellard579a97f2007-11-11 14:26:47 +00002026
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002027 free(vec);
pbrook53a59602006-03-25 19:31:22 +00002028}
2029
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002030static inline int target_to_host_sock_type(int *type)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002031{
2032 int host_type = 0;
2033 int target_type = *type;
2034
2035 switch (target_type & TARGET_SOCK_TYPE_MASK) {
2036 case TARGET_SOCK_DGRAM:
2037 host_type = SOCK_DGRAM;
2038 break;
2039 case TARGET_SOCK_STREAM:
2040 host_type = SOCK_STREAM;
2041 break;
2042 default:
2043 host_type = target_type & TARGET_SOCK_TYPE_MASK;
2044 break;
2045 }
2046 if (target_type & TARGET_SOCK_CLOEXEC) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002047#if defined(SOCK_CLOEXEC)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002048 host_type |= SOCK_CLOEXEC;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002049#else
2050 return -TARGET_EINVAL;
2051#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002052 }
2053 if (target_type & TARGET_SOCK_NONBLOCK) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002054#if defined(SOCK_NONBLOCK)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002055 host_type |= SOCK_NONBLOCK;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002056#elif !defined(O_NONBLOCK)
2057 return -TARGET_EINVAL;
2058#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002059 }
2060 *type = host_type;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002061 return 0;
2062}
2063
2064/* Try to emulate socket type flags after socket creation. */
2065static int sock_flags_fixup(int fd, int target_type)
2066{
2067#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2068 if (target_type & TARGET_SOCK_NONBLOCK) {
2069 int flags = fcntl(fd, F_GETFL);
2070 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
2071 close(fd);
2072 return -TARGET_EINVAL;
2073 }
2074 }
2075#endif
2076 return fd;
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002077}
2078
ths0da46a62007-10-20 20:23:07 +00002079/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002080static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00002081{
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002082 int target_type = type;
2083 int ret;
2084
2085 ret = target_to_host_sock_type(&type);
2086 if (ret) {
2087 return ret;
2088 }
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002089
balrog12bc92a2007-10-30 21:06:14 +00002090 if (domain == PF_NETLINK)
Ed Swierk480eda22014-05-05 20:04:45 -07002091 return -TARGET_EAFNOSUPPORT;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002092 ret = get_errno(socket(domain, type, protocol));
2093 if (ret >= 0) {
2094 ret = sock_flags_fixup(ret, target_type);
2095 }
2096 return ret;
bellard3532fa72006-06-24 15:06:03 +00002097}
2098
ths0da46a62007-10-20 20:23:07 +00002099/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002100static abi_long do_bind(int sockfd, abi_ulong target_addr,
2101 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00002102{
aurel328f7aeaf2009-01-30 19:47:57 +00002103 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03002104 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00002105
Blue Swirl38724252010-09-18 05:53:14 +00002106 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002107 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002108 }
aurel328f7aeaf2009-01-30 19:47:57 +00002109
aurel32607175e2009-04-15 16:11:59 +00002110 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00002111
Arnaud Patard917507b2009-06-19 10:44:45 +03002112 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2113 if (ret)
2114 return ret;
2115
bellard3532fa72006-06-24 15:06:03 +00002116 return get_errno(bind(sockfd, addr, addrlen));
2117}
2118
ths0da46a62007-10-20 20:23:07 +00002119/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002120static abi_long do_connect(int sockfd, abi_ulong target_addr,
2121 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00002122{
aurel328f7aeaf2009-01-30 19:47:57 +00002123 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03002124 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00002125
Blue Swirl38724252010-09-18 05:53:14 +00002126 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002127 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002128 }
aurel328f7aeaf2009-01-30 19:47:57 +00002129
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02002130 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00002131
Arnaud Patard917507b2009-06-19 10:44:45 +03002132 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2133 if (ret)
2134 return ret;
2135
bellard3532fa72006-06-24 15:06:03 +00002136 return get_errno(connect(sockfd, addr, addrlen));
2137}
2138
Alexander Graff19e00d2014-03-02 19:36:42 +00002139/* do_sendrecvmsg_locked() Must return target values and target errnos. */
2140static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
2141 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00002142{
balrog6de645c2008-10-28 10:26:29 +00002143 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00002144 struct msghdr msg;
2145 int count;
2146 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00002147 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00002148
bellard3532fa72006-06-24 15:06:03 +00002149 if (msgp->msg_name) {
2150 msg.msg_namelen = tswap32(msgp->msg_namelen);
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02002151 msg.msg_name = alloca(msg.msg_namelen+1);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002152 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00002153 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002154 if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002155 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03002156 }
bellard3532fa72006-06-24 15:06:03 +00002157 } else {
2158 msg.msg_name = NULL;
2159 msg.msg_namelen = 0;
2160 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002161 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00002162 msg.msg_control = alloca(msg.msg_controllen);
2163 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00002164
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002165 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002166 target_vec = tswapal(msgp->msg_iov);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002167 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2168 target_vec, count, send);
2169 if (vec == NULL) {
2170 ret = -host_to_target_errno(errno);
2171 goto out2;
2172 }
bellard3532fa72006-06-24 15:06:03 +00002173 msg.msg_iovlen = count;
2174 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00002175
bellard3532fa72006-06-24 15:06:03 +00002176 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00002177 ret = target_to_host_cmsg(&msg, msgp);
2178 if (ret == 0)
2179 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00002180 } else {
2181 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00002182 if (!is_error(ret)) {
2183 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00002184 ret = host_to_target_cmsg(msgp, &msg);
Jing Huangca619062012-07-24 13:58:02 +00002185 if (!is_error(ret)) {
2186 msgp->msg_namelen = tswap32(msg.msg_namelen);
2187 if (msg.msg_name != NULL) {
2188 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
2189 msg.msg_name, msg.msg_namelen);
2190 if (ret) {
2191 goto out;
2192 }
2193 }
2194
balrog6de645c2008-10-28 10:26:29 +00002195 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00002196 }
balrog6de645c2008-10-28 10:26:29 +00002197 }
bellard3532fa72006-06-24 15:06:03 +00002198 }
Jing Huangca619062012-07-24 13:58:02 +00002199
2200out:
bellard3532fa72006-06-24 15:06:03 +00002201 unlock_iovec(vec, target_vec, count, !send);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002202out2:
Alexander Graff19e00d2014-03-02 19:36:42 +00002203 return ret;
2204}
2205
2206static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
2207 int flags, int send)
2208{
2209 abi_long ret;
2210 struct target_msghdr *msgp;
2211
2212 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
2213 msgp,
2214 target_msg,
2215 send ? 1 : 0)) {
2216 return -TARGET_EFAULT;
2217 }
2218 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
bellard579a97f2007-11-11 14:26:47 +00002219 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00002220 return ret;
2221}
2222
Alexander Graff19e00d2014-03-02 19:36:42 +00002223#ifdef TARGET_NR_sendmmsg
2224/* We don't rely on the C library to have sendmmsg/recvmmsg support,
2225 * so it might not have this *mmsg-specific flag either.
2226 */
2227#ifndef MSG_WAITFORONE
2228#define MSG_WAITFORONE 0x10000
2229#endif
2230
2231static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
2232 unsigned int vlen, unsigned int flags,
2233 int send)
2234{
2235 struct target_mmsghdr *mmsgp;
2236 abi_long ret = 0;
2237 int i;
2238
2239 if (vlen > UIO_MAXIOV) {
2240 vlen = UIO_MAXIOV;
2241 }
2242
2243 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
2244 if (!mmsgp) {
2245 return -TARGET_EFAULT;
2246 }
2247
2248 for (i = 0; i < vlen; i++) {
2249 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
2250 if (is_error(ret)) {
2251 break;
2252 }
2253 mmsgp[i].msg_len = tswap32(ret);
2254 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2255 if (flags & MSG_WAITFORONE) {
2256 flags |= MSG_DONTWAIT;
2257 }
2258 }
2259
2260 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
2261
2262 /* Return number of datagrams sent if we sent any at all;
2263 * otherwise return the error.
2264 */
2265 if (i) {
2266 return i;
2267 }
2268 return ret;
2269}
2270#endif
2271
Peter Maydella94b4982013-02-08 04:35:04 +00002272/* If we don't have a system accept4() then just call accept.
2273 * The callsites to do_accept4() will ensure that they don't
2274 * pass a non-zero flags argument in this config.
2275 */
2276#ifndef CONFIG_ACCEPT4
2277static inline int accept4(int sockfd, struct sockaddr *addr,
2278 socklen_t *addrlen, int flags)
2279{
2280 assert(flags == 0);
2281 return accept(sockfd, addr, addrlen);
2282}
2283#endif
2284
2285/* do_accept4() Must return target values and target errnos. */
2286static abi_long do_accept4(int fd, abi_ulong target_addr,
2287 abi_ulong target_addrlen_addr, int flags)
pbrook1be9e1d2006-11-19 15:26:04 +00002288{
bellard2f619692007-11-16 10:46:05 +00002289 socklen_t addrlen;
2290 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002291 abi_long ret;
Petar Jovanovicd25295d2014-03-31 17:41:23 +02002292 int host_flags;
2293
2294 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
pbrook1be9e1d2006-11-19 15:26:04 +00002295
Peter Maydella94b4982013-02-08 04:35:04 +00002296 if (target_addr == 0) {
Petar Jovanovicd25295d2014-03-31 17:41:23 +02002297 return get_errno(accept4(fd, NULL, NULL, host_flags));
Peter Maydella94b4982013-02-08 04:35:04 +00002298 }
Arnaud Patard917507b2009-06-19 10:44:45 +03002299
2300 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00002301 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03002302 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00002303
Blue Swirl38724252010-09-18 05:53:14 +00002304 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002305 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002306 }
aurel328f7aeaf2009-01-30 19:47:57 +00002307
Arnaud Patard917507b2009-06-19 10:44:45 +03002308 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2309 return -TARGET_EINVAL;
2310
bellard2f619692007-11-16 10:46:05 +00002311 addr = alloca(addrlen);
2312
Petar Jovanovicd25295d2014-03-31 17:41:23 +02002313 ret = get_errno(accept4(fd, addr, &addrlen, host_flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002314 if (!is_error(ret)) {
2315 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002316 if (put_user_u32(addrlen, target_addrlen_addr))
2317 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002318 }
2319 return ret;
2320}
2321
ths0da46a62007-10-20 20:23:07 +00002322/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002323static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002324 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002325{
bellard2f619692007-11-16 10:46:05 +00002326 socklen_t addrlen;
2327 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002328 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002329
bellard2f619692007-11-16 10:46:05 +00002330 if (get_user_u32(addrlen, target_addrlen_addr))
2331 return -TARGET_EFAULT;
2332
Blue Swirl38724252010-09-18 05:53:14 +00002333 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002334 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002335 }
aurel328f7aeaf2009-01-30 19:47:57 +00002336
Arnaud Patard917507b2009-06-19 10:44:45 +03002337 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2338 return -TARGET_EFAULT;
2339
bellard2f619692007-11-16 10:46:05 +00002340 addr = alloca(addrlen);
2341
pbrook1be9e1d2006-11-19 15:26:04 +00002342 ret = get_errno(getpeername(fd, addr, &addrlen));
2343 if (!is_error(ret)) {
2344 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002345 if (put_user_u32(addrlen, target_addrlen_addr))
2346 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002347 }
2348 return ret;
2349}
2350
ths0da46a62007-10-20 20:23:07 +00002351/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002352static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002353 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002354{
bellard2f619692007-11-16 10:46:05 +00002355 socklen_t addrlen;
2356 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002357 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002358
bellard2f619692007-11-16 10:46:05 +00002359 if (get_user_u32(addrlen, target_addrlen_addr))
2360 return -TARGET_EFAULT;
2361
Blue Swirl38724252010-09-18 05:53:14 +00002362 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002363 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002364 }
aurel328f7aeaf2009-01-30 19:47:57 +00002365
Arnaud Patard917507b2009-06-19 10:44:45 +03002366 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2367 return -TARGET_EFAULT;
2368
bellard2f619692007-11-16 10:46:05 +00002369 addr = alloca(addrlen);
2370
pbrook1be9e1d2006-11-19 15:26:04 +00002371 ret = get_errno(getsockname(fd, addr, &addrlen));
2372 if (!is_error(ret)) {
2373 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002374 if (put_user_u32(addrlen, target_addrlen_addr))
2375 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002376 }
2377 return ret;
2378}
2379
ths0da46a62007-10-20 20:23:07 +00002380/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002381static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00002382 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002383{
2384 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00002385 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002386
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002387 target_to_host_sock_type(&type);
2388
pbrook1be9e1d2006-11-19 15:26:04 +00002389 ret = get_errno(socketpair(domain, type, protocol, tab));
2390 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00002391 if (put_user_s32(tab[0], target_tab_addr)
2392 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2393 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002394 }
2395 return ret;
2396}
2397
ths0da46a62007-10-20 20:23:07 +00002398/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002399static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2400 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002401{
2402 void *addr;
2403 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002404 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002405
Blue Swirl38724252010-09-18 05:53:14 +00002406 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002407 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002408 }
aurel328f7aeaf2009-01-30 19:47:57 +00002409
bellard579a97f2007-11-11 14:26:47 +00002410 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2411 if (!host_msg)
2412 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002413 if (target_addr) {
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02002414 addr = alloca(addrlen+1);
Arnaud Patard917507b2009-06-19 10:44:45 +03002415 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2416 if (ret) {
2417 unlock_user(host_msg, msg, 0);
2418 return ret;
2419 }
pbrook1be9e1d2006-11-19 15:26:04 +00002420 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2421 } else {
2422 ret = get_errno(send(fd, host_msg, len, flags));
2423 }
2424 unlock_user(host_msg, msg, 0);
2425 return ret;
2426}
2427
ths0da46a62007-10-20 20:23:07 +00002428/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002429static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2430 abi_ulong target_addr,
2431 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002432{
2433 socklen_t addrlen;
2434 void *addr;
2435 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002436 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002437
bellard579a97f2007-11-11 14:26:47 +00002438 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2439 if (!host_msg)
2440 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002441 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00002442 if (get_user_u32(addrlen, target_addrlen)) {
2443 ret = -TARGET_EFAULT;
2444 goto fail;
2445 }
Blue Swirl38724252010-09-18 05:53:14 +00002446 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002447 ret = -TARGET_EINVAL;
2448 goto fail;
2449 }
pbrook1be9e1d2006-11-19 15:26:04 +00002450 addr = alloca(addrlen);
2451 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2452 } else {
2453 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00002454 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002455 }
2456 if (!is_error(ret)) {
2457 if (target_addr) {
2458 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002459 if (put_user_u32(addrlen, target_addrlen)) {
2460 ret = -TARGET_EFAULT;
2461 goto fail;
2462 }
pbrook1be9e1d2006-11-19 15:26:04 +00002463 }
2464 unlock_user(host_msg, msg, len);
2465 } else {
bellard2f619692007-11-16 10:46:05 +00002466fail:
pbrook1be9e1d2006-11-19 15:26:04 +00002467 unlock_user(host_msg, msg, 0);
2468 }
2469 return ret;
2470}
2471
j_mayer32407102007-09-26 23:01:49 +00002472#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00002473/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002474static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00002475{
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002476 static const unsigned ac[] = { /* number of arguments per call */
2477 [SOCKOP_socket] = 3, /* domain, type, protocol */
2478 [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
2479 [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
2480 [SOCKOP_listen] = 2, /* sockfd, backlog */
2481 [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
2482 [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
2483 [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
2484 [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
2485 [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
2486 [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
2487 [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
2488 [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2489 [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2490 [SOCKOP_shutdown] = 2, /* sockfd, how */
2491 [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
2492 [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
2493 [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2494 [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2495 };
2496 abi_long a[6]; /* max 6 args */
bellard31e31b82003-02-18 22:55:36 +00002497
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002498 /* first, collect the arguments in a[] according to ac[] */
2499 if (num >= 0 && num < ARRAY_SIZE(ac)) {
2500 unsigned i;
2501 assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */
2502 for (i = 0; i < ac[num]; ++i) {
2503 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
André Hentschelb9d36eb2014-01-06 20:18:52 +01002504 return -TARGET_EFAULT;
2505 }
André Hentschelb9d36eb2014-01-06 20:18:52 +01002506 }
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002507 }
bellard2f619692007-11-16 10:46:05 +00002508
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002509 /* now when we have the args, actually handle the call */
2510 switch (num) {
2511 case SOCKOP_socket: /* domain, type, protocol */
2512 return do_socket(a[0], a[1], a[2]);
2513 case SOCKOP_bind: /* sockfd, addr, addrlen */
2514 return do_bind(a[0], a[1], a[2]);
2515 case SOCKOP_connect: /* sockfd, addr, addrlen */
2516 return do_connect(a[0], a[1], a[2]);
2517 case SOCKOP_listen: /* sockfd, backlog */
2518 return get_errno(listen(a[0], a[1]));
2519 case SOCKOP_accept: /* sockfd, addr, addrlen */
2520 return do_accept4(a[0], a[1], a[2], 0);
2521 case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */
2522 return do_accept4(a[0], a[1], a[2], a[3]);
2523 case SOCKOP_getsockname: /* sockfd, addr, addrlen */
2524 return do_getsockname(a[0], a[1], a[2]);
2525 case SOCKOP_getpeername: /* sockfd, addr, addrlen */
2526 return do_getpeername(a[0], a[1], a[2]);
2527 case SOCKOP_socketpair: /* domain, type, protocol, tab */
2528 return do_socketpair(a[0], a[1], a[2], a[3]);
2529 case SOCKOP_send: /* sockfd, msg, len, flags */
2530 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
2531 case SOCKOP_recv: /* sockfd, msg, len, flags */
2532 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
2533 case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */
2534 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
2535 case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */
2536 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
2537 case SOCKOP_shutdown: /* sockfd, how */
2538 return get_errno(shutdown(a[0], a[1]));
2539 case SOCKOP_sendmsg: /* sockfd, msg, flags */
2540 return do_sendrecvmsg(a[0], a[1], a[2], 1);
2541 case SOCKOP_recvmsg: /* sockfd, msg, flags */
2542 return do_sendrecvmsg(a[0], a[1], a[2], 0);
2543 case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */
2544 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
2545 case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */
2546 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
bellard31e31b82003-02-18 22:55:36 +00002547 default:
2548 gemu_log("Unsupported socketcall: %d\n", num);
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002549 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002550 }
bellard31e31b82003-02-18 22:55:36 +00002551}
j_mayer32407102007-09-26 23:01:49 +00002552#endif
bellard31e31b82003-02-18 22:55:36 +00002553
bellard8853f862004-02-22 14:57:26 +00002554#define N_SHM_REGIONS 32
2555
2556static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002557 abi_ulong start;
2558 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002559} shm_regions[N_SHM_REGIONS];
2560
ths3eb6b042007-06-03 14:26:27 +00002561struct target_semid_ds
2562{
2563 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002564 abi_ulong sem_otime;
Tom Musta03527342014-08-12 13:53:32 -05002565#if !defined(TARGET_PPC64)
blueswir1992f48a2007-10-14 16:27:31 +00002566 abi_ulong __unused1;
Tom Musta03527342014-08-12 13:53:32 -05002567#endif
blueswir1992f48a2007-10-14 16:27:31 +00002568 abi_ulong sem_ctime;
Tom Musta03527342014-08-12 13:53:32 -05002569#if !defined(TARGET_PPC64)
blueswir1992f48a2007-10-14 16:27:31 +00002570 abi_ulong __unused2;
Tom Musta03527342014-08-12 13:53:32 -05002571#endif
blueswir1992f48a2007-10-14 16:27:31 +00002572 abi_ulong sem_nsems;
2573 abi_ulong __unused3;
2574 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002575};
2576
bellard579a97f2007-11-11 14:26:47 +00002577static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2578 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002579{
2580 struct target_ipc_perm *target_ip;
2581 struct target_semid_ds *target_sd;
2582
bellard579a97f2007-11-11 14:26:47 +00002583 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2584 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002585 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002586 host_ip->__key = tswap32(target_ip->__key);
2587 host_ip->uid = tswap32(target_ip->uid);
2588 host_ip->gid = tswap32(target_ip->gid);
2589 host_ip->cuid = tswap32(target_ip->cuid);
2590 host_ip->cgid = tswap32(target_ip->cgid);
2591#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2592 host_ip->mode = tswap32(target_ip->mode);
2593#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002594 host_ip->mode = tswap16(target_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002595#endif
2596#if defined(TARGET_PPC)
2597 host_ip->__seq = tswap32(target_ip->__seq);
2598#else
2599 host_ip->__seq = tswap16(target_ip->__seq);
2600#endif
ths3eb6b042007-06-03 14:26:27 +00002601 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002602 return 0;
ths3eb6b042007-06-03 14:26:27 +00002603}
2604
bellard579a97f2007-11-11 14:26:47 +00002605static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2606 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002607{
2608 struct target_ipc_perm *target_ip;
2609 struct target_semid_ds *target_sd;
2610
bellard579a97f2007-11-11 14:26:47 +00002611 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2612 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002613 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002614 target_ip->__key = tswap32(host_ip->__key);
2615 target_ip->uid = tswap32(host_ip->uid);
2616 target_ip->gid = tswap32(host_ip->gid);
2617 target_ip->cuid = tswap32(host_ip->cuid);
2618 target_ip->cgid = tswap32(host_ip->cgid);
2619#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2620 target_ip->mode = tswap32(host_ip->mode);
2621#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002622 target_ip->mode = tswap16(host_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002623#endif
2624#if defined(TARGET_PPC)
2625 target_ip->__seq = tswap32(host_ip->__seq);
2626#else
2627 target_ip->__seq = tswap16(host_ip->__seq);
2628#endif
ths3eb6b042007-06-03 14:26:27 +00002629 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002630 return 0;
ths3eb6b042007-06-03 14:26:27 +00002631}
2632
bellard579a97f2007-11-11 14:26:47 +00002633static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2634 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002635{
2636 struct target_semid_ds *target_sd;
2637
bellard579a97f2007-11-11 14:26:47 +00002638 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2639 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002640 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2641 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002642 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2643 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2644 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002645 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002646 return 0;
ths3eb6b042007-06-03 14:26:27 +00002647}
2648
bellard579a97f2007-11-11 14:26:47 +00002649static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2650 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002651{
2652 struct target_semid_ds *target_sd;
2653
bellard579a97f2007-11-11 14:26:47 +00002654 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2655 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002656 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08002657 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002658 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2659 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2660 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002661 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002662 return 0;
ths3eb6b042007-06-03 14:26:27 +00002663}
2664
aurel32e5289082009-04-18 16:16:12 +00002665struct target_seminfo {
2666 int semmap;
2667 int semmni;
2668 int semmns;
2669 int semmnu;
2670 int semmsl;
2671 int semopm;
2672 int semume;
2673 int semusz;
2674 int semvmx;
2675 int semaem;
2676};
2677
2678static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2679 struct seminfo *host_seminfo)
2680{
2681 struct target_seminfo *target_seminfo;
2682 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2683 return -TARGET_EFAULT;
2684 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2685 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2686 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2687 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2688 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2689 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2690 __put_user(host_seminfo->semume, &target_seminfo->semume);
2691 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2692 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2693 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2694 unlock_user_struct(target_seminfo, target_addr, 1);
2695 return 0;
2696}
2697
thsfa294812007-02-02 22:05:00 +00002698union semun {
2699 int val;
ths3eb6b042007-06-03 14:26:27 +00002700 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002701 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002702 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002703};
2704
ths3eb6b042007-06-03 14:26:27 +00002705union target_semun {
2706 int val;
aurel32e5289082009-04-18 16:16:12 +00002707 abi_ulong buf;
2708 abi_ulong array;
2709 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002710};
2711
aurel32e5289082009-04-18 16:16:12 +00002712static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2713 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002714{
aurel32e5289082009-04-18 16:16:12 +00002715 int nsems;
2716 unsigned short *array;
2717 union semun semun;
2718 struct semid_ds semid_ds;
2719 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002720
aurel32e5289082009-04-18 16:16:12 +00002721 semun.buf = &semid_ds;
2722
2723 ret = semctl(semid, 0, IPC_STAT, semun);
2724 if (ret == -1)
2725 return get_errno(ret);
2726
2727 nsems = semid_ds.sem_nsems;
2728
2729 *host_array = malloc(nsems*sizeof(unsigned short));
Peter Maydell69d4c702014-02-17 18:55:34 +00002730 if (!*host_array) {
2731 return -TARGET_ENOMEM;
2732 }
aurel32e5289082009-04-18 16:16:12 +00002733 array = lock_user(VERIFY_READ, target_addr,
2734 nsems*sizeof(unsigned short), 1);
Peter Maydell69d4c702014-02-17 18:55:34 +00002735 if (!array) {
2736 free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00002737 return -TARGET_EFAULT;
Peter Maydell69d4c702014-02-17 18:55:34 +00002738 }
aurel32e5289082009-04-18 16:16:12 +00002739
2740 for(i=0; i<nsems; i++) {
2741 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002742 }
aurel32e5289082009-04-18 16:16:12 +00002743 unlock_user(array, target_addr, 0);
2744
bellard579a97f2007-11-11 14:26:47 +00002745 return 0;
ths3eb6b042007-06-03 14:26:27 +00002746}
2747
aurel32e5289082009-04-18 16:16:12 +00002748static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2749 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002750{
aurel32e5289082009-04-18 16:16:12 +00002751 int nsems;
2752 unsigned short *array;
2753 union semun semun;
2754 struct semid_ds semid_ds;
2755 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002756
aurel32e5289082009-04-18 16:16:12 +00002757 semun.buf = &semid_ds;
2758
2759 ret = semctl(semid, 0, IPC_STAT, semun);
2760 if (ret == -1)
2761 return get_errno(ret);
2762
2763 nsems = semid_ds.sem_nsems;
2764
2765 array = lock_user(VERIFY_WRITE, target_addr,
2766 nsems*sizeof(unsigned short), 0);
2767 if (!array)
2768 return -TARGET_EFAULT;
2769
2770 for(i=0; i<nsems; i++) {
2771 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002772 }
aurel32e5289082009-04-18 16:16:12 +00002773 free(*host_array);
2774 unlock_user(array, target_addr, 1);
2775
bellard579a97f2007-11-11 14:26:47 +00002776 return 0;
ths3eb6b042007-06-03 14:26:27 +00002777}
2778
aurel32e5289082009-04-18 16:16:12 +00002779static inline abi_long do_semctl(int semid, int semnum, int cmd,
Stefan Weild1c002b2015-02-08 15:40:58 +01002780 abi_ulong target_arg)
ths3eb6b042007-06-03 14:26:27 +00002781{
Stefan Weild1c002b2015-02-08 15:40:58 +01002782 union target_semun target_su = { .buf = target_arg };
ths3eb6b042007-06-03 14:26:27 +00002783 union semun arg;
2784 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302785 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002786 struct seminfo seminfo;
2787 abi_long ret = -TARGET_EINVAL;
2788 abi_long err;
2789 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002790
2791 switch( cmd ) {
2792 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002793 case SETVAL:
Tom Musta5464bae2014-08-12 13:53:34 -05002794 /* In 64 bit cross-endian situations, we will erroneously pick up
2795 * the wrong half of the union for the "val" element. To rectify
2796 * this, the entire 8-byte structure is byteswapped, followed by
2797 * a swap of the 4 byte val field. In other cases, the data is
2798 * already in proper host byte order. */
2799 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
2800 target_su.buf = tswapal(target_su.buf);
2801 arg.val = tswap32(target_su.val);
2802 } else {
2803 arg.val = target_su.val;
2804 }
aurel32e5289082009-04-18 16:16:12 +00002805 ret = get_errno(semctl(semid, semnum, cmd, arg));
ths3eb6b042007-06-03 14:26:27 +00002806 break;
2807 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002808 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002809 err = target_to_host_semarray(semid, &array, target_su.array);
2810 if (err)
2811 return err;
2812 arg.array = array;
2813 ret = get_errno(semctl(semid, semnum, cmd, arg));
2814 err = host_to_target_semarray(semid, target_su.array, &array);
2815 if (err)
2816 return err;
ths3eb6b042007-06-03 14:26:27 +00002817 break;
2818 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002819 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002820 case SEM_STAT:
2821 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2822 if (err)
2823 return err;
2824 arg.buf = &dsarg;
2825 ret = get_errno(semctl(semid, semnum, cmd, arg));
2826 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2827 if (err)
2828 return err;
ths3eb6b042007-06-03 14:26:27 +00002829 break;
aurel32e5289082009-04-18 16:16:12 +00002830 case IPC_INFO:
2831 case SEM_INFO:
2832 arg.__buf = &seminfo;
2833 ret = get_errno(semctl(semid, semnum, cmd, arg));
2834 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2835 if (err)
2836 return err;
2837 break;
2838 case IPC_RMID:
2839 case GETPID:
2840 case GETNCNT:
2841 case GETZCNT:
2842 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2843 break;
ths3eb6b042007-06-03 14:26:27 +00002844 }
2845
2846 return ret;
2847}
2848
aurel32e5289082009-04-18 16:16:12 +00002849struct target_sembuf {
2850 unsigned short sem_num;
2851 short sem_op;
2852 short sem_flg;
2853};
2854
2855static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2856 abi_ulong target_addr,
2857 unsigned nsops)
2858{
2859 struct target_sembuf *target_sembuf;
2860 int i;
2861
2862 target_sembuf = lock_user(VERIFY_READ, target_addr,
2863 nsops*sizeof(struct target_sembuf), 1);
2864 if (!target_sembuf)
2865 return -TARGET_EFAULT;
2866
2867 for(i=0; i<nsops; i++) {
2868 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2869 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2870 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2871 }
2872
2873 unlock_user(target_sembuf, target_addr, 0);
2874
2875 return 0;
2876}
2877
2878static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2879{
2880 struct sembuf sops[nsops];
2881
2882 if (target_to_host_sembuf(sops, ptr, nsops))
2883 return -TARGET_EFAULT;
2884
Petar Jovanovicc7128c92013-03-21 07:57:36 +00002885 return get_errno(semop(semid, sops, nsops));
aurel32e5289082009-04-18 16:16:12 +00002886}
2887
ths1bc012f2007-06-03 14:27:49 +00002888struct target_msqid_ds
2889{
aurel321c54ff92008-10-13 21:08:44 +00002890 struct target_ipc_perm msg_perm;
2891 abi_ulong msg_stime;
2892#if TARGET_ABI_BITS == 32
2893 abi_ulong __unused1;
2894#endif
2895 abi_ulong msg_rtime;
2896#if TARGET_ABI_BITS == 32
2897 abi_ulong __unused2;
2898#endif
2899 abi_ulong msg_ctime;
2900#if TARGET_ABI_BITS == 32
2901 abi_ulong __unused3;
2902#endif
2903 abi_ulong __msg_cbytes;
2904 abi_ulong msg_qnum;
2905 abi_ulong msg_qbytes;
2906 abi_ulong msg_lspid;
2907 abi_ulong msg_lrpid;
2908 abi_ulong __unused4;
2909 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002910};
2911
bellard579a97f2007-11-11 14:26:47 +00002912static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2913 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002914{
2915 struct target_msqid_ds *target_md;
2916
bellard579a97f2007-11-11 14:26:47 +00002917 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2918 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002919 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2920 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002921 host_md->msg_stime = tswapal(target_md->msg_stime);
2922 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2923 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2924 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2925 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2926 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2927 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2928 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002929 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002930 return 0;
ths1bc012f2007-06-03 14:27:49 +00002931}
2932
bellard579a97f2007-11-11 14:26:47 +00002933static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2934 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002935{
2936 struct target_msqid_ds *target_md;
2937
bellard579a97f2007-11-11 14:26:47 +00002938 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2939 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002940 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2941 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002942 target_md->msg_stime = tswapal(host_md->msg_stime);
2943 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2944 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2945 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2946 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2947 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2948 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2949 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002950 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002951 return 0;
ths1bc012f2007-06-03 14:27:49 +00002952}
2953
aurel321c54ff92008-10-13 21:08:44 +00002954struct target_msginfo {
2955 int msgpool;
2956 int msgmap;
2957 int msgmax;
2958 int msgmnb;
2959 int msgmni;
2960 int msgssz;
2961 int msgtql;
2962 unsigned short int msgseg;
2963};
2964
2965static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2966 struct msginfo *host_msginfo)
2967{
2968 struct target_msginfo *target_msginfo;
2969 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2970 return -TARGET_EFAULT;
2971 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2972 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2973 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2974 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2975 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2976 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2977 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2978 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2979 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002980 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002981}
2982
2983static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002984{
2985 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002986 struct msginfo msginfo;
2987 abi_long ret = -TARGET_EINVAL;
2988
2989 cmd &= 0xff;
2990
2991 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002992 case IPC_STAT:
2993 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002994 case MSG_STAT:
2995 if (target_to_host_msqid_ds(&dsarg,ptr))
2996 return -TARGET_EFAULT;
2997 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2998 if (host_to_target_msqid_ds(ptr,&dsarg))
2999 return -TARGET_EFAULT;
3000 break;
3001 case IPC_RMID:
3002 ret = get_errno(msgctl(msgid, cmd, NULL));
3003 break;
3004 case IPC_INFO:
3005 case MSG_INFO:
3006 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
3007 if (host_to_target_msginfo(ptr, &msginfo))
3008 return -TARGET_EFAULT;
3009 break;
ths1bc012f2007-06-03 14:27:49 +00003010 }
aurel321c54ff92008-10-13 21:08:44 +00003011
ths1bc012f2007-06-03 14:27:49 +00003012 return ret;
3013}
3014
3015struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00003016 abi_long mtype;
3017 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00003018};
3019
blueswir1992f48a2007-10-14 16:27:31 +00003020static inline abi_long do_msgsnd(int msqid, abi_long msgp,
Tom Mustaedcc5f92014-08-12 13:53:37 -05003021 ssize_t msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00003022{
3023 struct target_msgbuf *target_mb;
3024 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00003025 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00003026
Tom Mustaedcc5f92014-08-12 13:53:37 -05003027 if (msgsz < 0) {
3028 return -TARGET_EINVAL;
3029 }
3030
bellard579a97f2007-11-11 14:26:47 +00003031 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
3032 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00003033 host_mb = malloc(msgsz+sizeof(long));
zhanghailiang29e03fc2014-08-14 15:29:18 +08003034 if (!host_mb) {
3035 unlock_user_struct(target_mb, msgp, 0);
3036 return -TARGET_ENOMEM;
3037 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003038 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00003039 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00003040 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
3041 free(host_mb);
3042 unlock_user_struct(target_mb, msgp, 0);
3043
3044 return ret;
3045}
3046
blueswir1992f48a2007-10-14 16:27:31 +00003047static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00003048 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00003049 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00003050{
3051 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00003052 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00003053 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00003054 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00003055
bellard579a97f2007-11-11 14:26:47 +00003056 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
3057 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00003058
Jim Meyering0d07fe42012-08-22 13:55:53 +02003059 host_mb = g_malloc(msgsz+sizeof(long));
Laurent Vivier79dd77d2012-12-20 11:00:11 +00003060 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
aurel321c54ff92008-10-13 21:08:44 +00003061
bellard579a97f2007-11-11 14:26:47 +00003062 if (ret > 0) {
3063 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
3064 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
3065 if (!target_mtext) {
3066 ret = -TARGET_EFAULT;
3067 goto end;
3068 }
aurel321c54ff92008-10-13 21:08:44 +00003069 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00003070 unlock_user(target_mtext, target_mtext_addr, ret);
3071 }
aurel321c54ff92008-10-13 21:08:44 +00003072
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003073 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00003074
bellard579a97f2007-11-11 14:26:47 +00003075end:
3076 if (target_mb)
3077 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02003078 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00003079 return ret;
3080}
3081
Riku Voipio88a8c982009-04-03 10:42:00 +03003082static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
3083 abi_ulong target_addr)
3084{
3085 struct target_shmid_ds *target_sd;
3086
3087 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3088 return -TARGET_EFAULT;
3089 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
3090 return -TARGET_EFAULT;
3091 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3092 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
3093 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3094 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3095 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3096 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3097 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3098 unlock_user_struct(target_sd, target_addr, 0);
3099 return 0;
3100}
3101
3102static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
3103 struct shmid_ds *host_sd)
3104{
3105 struct target_shmid_ds *target_sd;
3106
3107 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3108 return -TARGET_EFAULT;
3109 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
3110 return -TARGET_EFAULT;
3111 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3112 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
3113 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3114 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3115 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3116 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3117 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3118 unlock_user_struct(target_sd, target_addr, 1);
3119 return 0;
3120}
3121
3122struct target_shminfo {
3123 abi_ulong shmmax;
3124 abi_ulong shmmin;
3125 abi_ulong shmmni;
3126 abi_ulong shmseg;
3127 abi_ulong shmall;
3128};
3129
3130static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
3131 struct shminfo *host_shminfo)
3132{
3133 struct target_shminfo *target_shminfo;
3134 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3135 return -TARGET_EFAULT;
3136 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3137 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3138 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3139 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3140 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3141 unlock_user_struct(target_shminfo, target_addr, 1);
3142 return 0;
3143}
3144
3145struct target_shm_info {
3146 int used_ids;
3147 abi_ulong shm_tot;
3148 abi_ulong shm_rss;
3149 abi_ulong shm_swp;
3150 abi_ulong swap_attempts;
3151 abi_ulong swap_successes;
3152};
3153
3154static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3155 struct shm_info *host_shm_info)
3156{
3157 struct target_shm_info *target_shm_info;
3158 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3159 return -TARGET_EFAULT;
3160 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3161 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3162 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3163 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3164 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3165 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3166 unlock_user_struct(target_shm_info, target_addr, 1);
3167 return 0;
3168}
3169
3170static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3171{
3172 struct shmid_ds dsarg;
3173 struct shminfo shminfo;
3174 struct shm_info shm_info;
3175 abi_long ret = -TARGET_EINVAL;
3176
3177 cmd &= 0xff;
3178
3179 switch(cmd) {
3180 case IPC_STAT:
3181 case IPC_SET:
3182 case SHM_STAT:
3183 if (target_to_host_shmid_ds(&dsarg, buf))
3184 return -TARGET_EFAULT;
3185 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3186 if (host_to_target_shmid_ds(buf, &dsarg))
3187 return -TARGET_EFAULT;
3188 break;
3189 case IPC_INFO:
3190 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3191 if (host_to_target_shminfo(buf, &shminfo))
3192 return -TARGET_EFAULT;
3193 break;
3194 case SHM_INFO:
3195 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3196 if (host_to_target_shm_info(buf, &shm_info))
3197 return -TARGET_EFAULT;
3198 break;
3199 case IPC_RMID:
3200 case SHM_LOCK:
3201 case SHM_UNLOCK:
3202 ret = get_errno(shmctl(shmid, cmd, NULL));
3203 break;
3204 }
3205
3206 return ret;
3207}
3208
3209static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3210{
3211 abi_long raddr;
3212 void *host_raddr;
3213 struct shmid_ds shm_info;
3214 int i,ret;
3215
3216 /* find out the length of the shared memory segment */
3217 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3218 if (is_error(ret)) {
3219 /* can't get length, bail out */
3220 return ret;
3221 }
3222
3223 mmap_lock();
3224
3225 if (shmaddr)
3226 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3227 else {
3228 abi_ulong mmap_start;
3229
3230 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3231
3232 if (mmap_start == -1) {
3233 errno = ENOMEM;
3234 host_raddr = (void *)-1;
3235 } else
3236 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3237 }
3238
3239 if (host_raddr == (void *)-1) {
3240 mmap_unlock();
3241 return get_errno((long)host_raddr);
3242 }
3243 raddr=h2g((unsigned long)host_raddr);
3244
3245 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3246 PAGE_VALID | PAGE_READ |
3247 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3248
3249 for (i = 0; i < N_SHM_REGIONS; i++) {
3250 if (shm_regions[i].start == 0) {
3251 shm_regions[i].start = raddr;
3252 shm_regions[i].size = shm_info.shm_segsz;
3253 break;
3254 }
3255 }
3256
3257 mmap_unlock();
3258 return raddr;
3259
3260}
3261
3262static inline abi_long do_shmdt(abi_ulong shmaddr)
3263{
3264 int i;
3265
3266 for (i = 0; i < N_SHM_REGIONS; ++i) {
3267 if (shm_regions[i].start == shmaddr) {
3268 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09003269 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03003270 break;
3271 }
3272 }
3273
3274 return get_errno(shmdt(g2h(shmaddr)));
3275}
3276
aurel321c54ff92008-10-13 21:08:44 +00003277#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00003278/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00003279/* do_ipc() must return target values and target errnos. */
Tom Musta37ed0952014-08-12 13:53:35 -05003280static abi_long do_ipc(unsigned int call, abi_long first,
3281 abi_long second, abi_long third,
blueswir1992f48a2007-10-14 16:27:31 +00003282 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00003283{
3284 int version;
blueswir1992f48a2007-10-14 16:27:31 +00003285 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00003286
3287 version = call >> 16;
3288 call &= 0xffff;
3289
3290 switch (call) {
thsfa294812007-02-02 22:05:00 +00003291 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00003292 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00003293 break;
3294
3295 case IPCOP_semget:
3296 ret = get_errno(semget(first, second, third));
3297 break;
3298
Tom Musta5d2fa8e2014-08-12 13:53:33 -05003299 case IPCOP_semctl: {
3300 /* The semun argument to semctl is passed by value, so dereference the
3301 * ptr argument. */
3302 abi_ulong atptr;
Tom Musta37ed0952014-08-12 13:53:35 -05003303 get_user_ual(atptr, ptr);
Stefan Weild1c002b2015-02-08 15:40:58 +01003304 ret = do_semctl(first, second, third, atptr);
thsfa294812007-02-02 22:05:00 +00003305 break;
Tom Musta5d2fa8e2014-08-12 13:53:33 -05003306 }
thsd96372e2007-02-02 22:05:44 +00003307
aurel321c54ff92008-10-13 21:08:44 +00003308 case IPCOP_msgget:
3309 ret = get_errno(msgget(first, second));
3310 break;
thsd96372e2007-02-02 22:05:44 +00003311
aurel321c54ff92008-10-13 21:08:44 +00003312 case IPCOP_msgsnd:
3313 ret = do_msgsnd(first, ptr, second, third);
3314 break;
thsd96372e2007-02-02 22:05:44 +00003315
aurel321c54ff92008-10-13 21:08:44 +00003316 case IPCOP_msgctl:
3317 ret = do_msgctl(first, second, ptr);
3318 break;
thsd96372e2007-02-02 22:05:44 +00003319
aurel321c54ff92008-10-13 21:08:44 +00003320 case IPCOP_msgrcv:
3321 switch (version) {
3322 case 0:
3323 {
3324 struct target_ipc_kludge {
3325 abi_long msgp;
3326 abi_long msgtyp;
3327 } *tmp;
thsd96372e2007-02-02 22:05:44 +00003328
aurel321c54ff92008-10-13 21:08:44 +00003329 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3330 ret = -TARGET_EFAULT;
3331 break;
ths1bc012f2007-06-03 14:27:49 +00003332 }
aurel321c54ff92008-10-13 21:08:44 +00003333
Laurent Vivier79dd77d2012-12-20 11:00:11 +00003334 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
aurel321c54ff92008-10-13 21:08:44 +00003335
3336 unlock_user_struct(tmp, ptr, 0);
3337 break;
3338 }
3339 default:
3340 ret = do_msgrcv(first, ptr, second, fifth, third);
3341 }
3342 break;
thsd96372e2007-02-02 22:05:44 +00003343
bellard8853f862004-02-22 14:57:26 +00003344 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03003345 switch (version) {
3346 default:
bellard5a4a8982007-11-11 17:39:18 +00003347 {
3348 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03003349 raddr = do_shmat(first, ptr, second);
3350 if (is_error(raddr))
3351 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00003352 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00003353 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03003354 break;
3355 }
3356 case 1:
3357 ret = -TARGET_EINVAL;
3358 break;
bellard5a4a8982007-11-11 17:39:18 +00003359 }
bellard8853f862004-02-22 14:57:26 +00003360 break;
3361 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03003362 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00003363 break;
3364
3365 case IPCOP_shmget:
3366 /* IPC_* flag values are the same on all linux platforms */
3367 ret = get_errno(shmget(first, second, third));
3368 break;
3369
3370 /* IPC_* and SHM_* command values are the same on all linux platforms */
3371 case IPCOP_shmctl:
Petar Jovanovica2926782013-10-30 14:46:32 +01003372 ret = do_shmctl(first, second, ptr);
bellard8853f862004-02-22 14:57:26 +00003373 break;
3374 default:
j_mayer32407102007-09-26 23:01:49 +00003375 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00003376 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00003377 break;
3378 }
3379 return ret;
3380}
j_mayer32407102007-09-26 23:01:49 +00003381#endif
bellard8853f862004-02-22 14:57:26 +00003382
bellard31e31b82003-02-18 22:55:36 +00003383/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00003384
Blue Swirl001faf32009-05-13 17:53:17 +00003385#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00003386#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3387enum {
3388#include "syscall_types.h"
Alexander Graf8be656b2015-05-06 23:47:32 +02003389STRUCT_MAX
bellard31e31b82003-02-18 22:55:36 +00003390};
3391#undef STRUCT
3392#undef STRUCT_SPECIAL
3393
Blue Swirl001faf32009-05-13 17:53:17 +00003394#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00003395#define STRUCT_SPECIAL(name)
3396#include "syscall_types.h"
3397#undef STRUCT
3398#undef STRUCT_SPECIAL
3399
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003400typedef struct IOCTLEntry IOCTLEntry;
3401
3402typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003403 int fd, int cmd, abi_long arg);
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003404
3405struct IOCTLEntry {
Ed Swierk9c6bf9c2014-12-16 12:55:31 -08003406 int target_cmd;
bellard2ab83ea2003-06-15 19:56:46 +00003407 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00003408 const char *name;
3409 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003410 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003411 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003412};
bellard31e31b82003-02-18 22:55:36 +00003413
3414#define IOC_R 0x0001
3415#define IOC_W 0x0002
3416#define IOC_RW (IOC_R | IOC_W)
3417
3418#define MAX_STRUCT_SIZE 4096
3419
Peter Maydelldace20d2011-01-10 13:11:24 +00003420#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003421/* So fiemap access checks don't overflow on 32 bit systems.
3422 * This is very slightly smaller than the limit imposed by
3423 * the underlying kernel.
3424 */
3425#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3426 / sizeof(struct fiemap_extent))
3427
3428static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003429 int fd, int cmd, abi_long arg)
Peter Maydell285da2b2011-01-06 15:04:18 +00003430{
3431 /* The parameter for this ioctl is a struct fiemap followed
3432 * by an array of struct fiemap_extent whose size is set
3433 * in fiemap->fm_extent_count. The array is filled in by the
3434 * ioctl.
3435 */
3436 int target_size_in, target_size_out;
3437 struct fiemap *fm;
3438 const argtype *arg_type = ie->arg_type;
3439 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3440 void *argptr, *p;
3441 abi_long ret;
3442 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3443 uint32_t outbufsz;
3444 int free_fm = 0;
3445
3446 assert(arg_type[0] == TYPE_PTR);
3447 assert(ie->access == IOC_RW);
3448 arg_type++;
3449 target_size_in = thunk_type_size(arg_type, 0);
3450 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3451 if (!argptr) {
3452 return -TARGET_EFAULT;
3453 }
3454 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3455 unlock_user(argptr, arg, 0);
3456 fm = (struct fiemap *)buf_temp;
3457 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3458 return -TARGET_EINVAL;
3459 }
3460
3461 outbufsz = sizeof (*fm) +
3462 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3463
3464 if (outbufsz > MAX_STRUCT_SIZE) {
3465 /* We can't fit all the extents into the fixed size buffer.
3466 * Allocate one that is large enough and use it instead.
3467 */
3468 fm = malloc(outbufsz);
3469 if (!fm) {
3470 return -TARGET_ENOMEM;
3471 }
3472 memcpy(fm, buf_temp, sizeof(struct fiemap));
3473 free_fm = 1;
3474 }
3475 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3476 if (!is_error(ret)) {
3477 target_size_out = target_size_in;
3478 /* An extent_count of 0 means we were only counting the extents
3479 * so there are no structs to copy
3480 */
3481 if (fm->fm_extent_count != 0) {
3482 target_size_out += fm->fm_mapped_extents * extent_size;
3483 }
3484 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3485 if (!argptr) {
3486 ret = -TARGET_EFAULT;
3487 } else {
3488 /* Convert the struct fiemap */
3489 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3490 if (fm->fm_extent_count != 0) {
3491 p = argptr + target_size_in;
3492 /* ...and then all the struct fiemap_extents */
3493 for (i = 0; i < fm->fm_mapped_extents; i++) {
3494 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3495 THUNK_TARGET);
3496 p += extent_size;
3497 }
3498 }
3499 unlock_user(argptr, arg, target_size_out);
3500 }
3501 }
3502 if (free_fm) {
3503 free(fm);
3504 }
3505 return ret;
3506}
Peter Maydelldace20d2011-01-10 13:11:24 +00003507#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003508
Laurent Vivier059c2f22011-03-30 00:12:12 +02003509static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003510 int fd, int cmd, abi_long arg)
Laurent Vivier059c2f22011-03-30 00:12:12 +02003511{
3512 const argtype *arg_type = ie->arg_type;
3513 int target_size;
3514 void *argptr;
3515 int ret;
3516 struct ifconf *host_ifconf;
3517 uint32_t outbufsz;
3518 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3519 int target_ifreq_size;
3520 int nb_ifreq;
3521 int free_buf = 0;
3522 int i;
3523 int target_ifc_len;
3524 abi_long target_ifc_buf;
3525 int host_ifc_len;
3526 char *host_ifc_buf;
3527
3528 assert(arg_type[0] == TYPE_PTR);
3529 assert(ie->access == IOC_RW);
3530
3531 arg_type++;
3532 target_size = thunk_type_size(arg_type, 0);
3533
3534 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3535 if (!argptr)
3536 return -TARGET_EFAULT;
3537 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3538 unlock_user(argptr, arg, 0);
3539
3540 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3541 target_ifc_len = host_ifconf->ifc_len;
3542 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3543
3544 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3545 nb_ifreq = target_ifc_len / target_ifreq_size;
3546 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3547
3548 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3549 if (outbufsz > MAX_STRUCT_SIZE) {
3550 /* We can't fit all the extents into the fixed size buffer.
3551 * Allocate one that is large enough and use it instead.
3552 */
3553 host_ifconf = malloc(outbufsz);
3554 if (!host_ifconf) {
3555 return -TARGET_ENOMEM;
3556 }
3557 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3558 free_buf = 1;
3559 }
3560 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3561
3562 host_ifconf->ifc_len = host_ifc_len;
3563 host_ifconf->ifc_buf = host_ifc_buf;
3564
3565 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3566 if (!is_error(ret)) {
3567 /* convert host ifc_len to target ifc_len */
3568
3569 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3570 target_ifc_len = nb_ifreq * target_ifreq_size;
3571 host_ifconf->ifc_len = target_ifc_len;
3572
3573 /* restore target ifc_buf */
3574
3575 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3576
3577 /* copy struct ifconf to target user */
3578
3579 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3580 if (!argptr)
3581 return -TARGET_EFAULT;
3582 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3583 unlock_user(argptr, arg, target_size);
3584
3585 /* copy ifreq[] to target user */
3586
3587 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3588 for (i = 0; i < nb_ifreq ; i++) {
3589 thunk_convert(argptr + i * target_ifreq_size,
3590 host_ifc_buf + i * sizeof(struct ifreq),
3591 ifreq_arg_type, THUNK_TARGET);
3592 }
3593 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3594 }
3595
3596 if (free_buf) {
3597 free(host_ifconf);
3598 }
3599
3600 return ret;
3601}
3602
Alexander Graf56e904e2012-01-31 18:42:06 +01003603static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003604 int cmd, abi_long arg)
Alexander Graf56e904e2012-01-31 18:42:06 +01003605{
3606 void *argptr;
3607 struct dm_ioctl *host_dm;
3608 abi_long guest_data;
3609 uint32_t guest_data_size;
3610 int target_size;
3611 const argtype *arg_type = ie->arg_type;
3612 abi_long ret;
3613 void *big_buf = NULL;
3614 char *host_data;
3615
3616 arg_type++;
3617 target_size = thunk_type_size(arg_type, 0);
3618 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3619 if (!argptr) {
3620 ret = -TARGET_EFAULT;
3621 goto out;
3622 }
3623 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3624 unlock_user(argptr, arg, 0);
3625
3626 /* buf_temp is too small, so fetch things into a bigger buffer */
3627 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3628 memcpy(big_buf, buf_temp, target_size);
3629 buf_temp = big_buf;
3630 host_dm = big_buf;
3631
3632 guest_data = arg + host_dm->data_start;
3633 if ((guest_data - arg) < 0) {
3634 ret = -EINVAL;
3635 goto out;
3636 }
3637 guest_data_size = host_dm->data_size - host_dm->data_start;
3638 host_data = (char*)host_dm + host_dm->data_start;
3639
3640 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3641 switch (ie->host_cmd) {
3642 case DM_REMOVE_ALL:
3643 case DM_LIST_DEVICES:
3644 case DM_DEV_CREATE:
3645 case DM_DEV_REMOVE:
3646 case DM_DEV_SUSPEND:
3647 case DM_DEV_STATUS:
3648 case DM_DEV_WAIT:
3649 case DM_TABLE_STATUS:
3650 case DM_TABLE_CLEAR:
3651 case DM_TABLE_DEPS:
3652 case DM_LIST_VERSIONS:
3653 /* no input data */
3654 break;
3655 case DM_DEV_RENAME:
3656 case DM_DEV_SET_GEOMETRY:
3657 /* data contains only strings */
3658 memcpy(host_data, argptr, guest_data_size);
3659 break;
3660 case DM_TARGET_MSG:
3661 memcpy(host_data, argptr, guest_data_size);
3662 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3663 break;
3664 case DM_TABLE_LOAD:
3665 {
3666 void *gspec = argptr;
3667 void *cur_data = host_data;
3668 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3669 int spec_size = thunk_type_size(arg_type, 0);
3670 int i;
3671
3672 for (i = 0; i < host_dm->target_count; i++) {
3673 struct dm_target_spec *spec = cur_data;
3674 uint32_t next;
3675 int slen;
3676
3677 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3678 slen = strlen((char*)gspec + spec_size) + 1;
3679 next = spec->next;
3680 spec->next = sizeof(*spec) + slen;
3681 strcpy((char*)&spec[1], gspec + spec_size);
3682 gspec += next;
3683 cur_data += spec->next;
3684 }
3685 break;
3686 }
3687 default:
3688 ret = -TARGET_EINVAL;
Chen Gang Sdec04732015-01-25 08:00:42 +08003689 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01003690 goto out;
3691 }
3692 unlock_user(argptr, guest_data, 0);
3693
3694 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3695 if (!is_error(ret)) {
3696 guest_data = arg + host_dm->data_start;
3697 guest_data_size = host_dm->data_size - host_dm->data_start;
3698 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3699 switch (ie->host_cmd) {
3700 case DM_REMOVE_ALL:
3701 case DM_DEV_CREATE:
3702 case DM_DEV_REMOVE:
3703 case DM_DEV_RENAME:
3704 case DM_DEV_SUSPEND:
3705 case DM_DEV_STATUS:
3706 case DM_TABLE_LOAD:
3707 case DM_TABLE_CLEAR:
3708 case DM_TARGET_MSG:
3709 case DM_DEV_SET_GEOMETRY:
3710 /* no return data */
3711 break;
3712 case DM_LIST_DEVICES:
3713 {
3714 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3715 uint32_t remaining_data = guest_data_size;
3716 void *cur_data = argptr;
3717 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3718 int nl_size = 12; /* can't use thunk_size due to alignment */
3719
3720 while (1) {
3721 uint32_t next = nl->next;
3722 if (next) {
3723 nl->next = nl_size + (strlen(nl->name) + 1);
3724 }
3725 if (remaining_data < nl->next) {
3726 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3727 break;
3728 }
3729 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3730 strcpy(cur_data + nl_size, nl->name);
3731 cur_data += nl->next;
3732 remaining_data -= nl->next;
3733 if (!next) {
3734 break;
3735 }
3736 nl = (void*)nl + next;
3737 }
3738 break;
3739 }
3740 case DM_DEV_WAIT:
3741 case DM_TABLE_STATUS:
3742 {
3743 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3744 void *cur_data = argptr;
3745 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3746 int spec_size = thunk_type_size(arg_type, 0);
3747 int i;
3748
3749 for (i = 0; i < host_dm->target_count; i++) {
3750 uint32_t next = spec->next;
3751 int slen = strlen((char*)&spec[1]) + 1;
3752 spec->next = (cur_data - argptr) + spec_size + slen;
3753 if (guest_data_size < spec->next) {
3754 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3755 break;
3756 }
3757 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3758 strcpy(cur_data + spec_size, (char*)&spec[1]);
3759 cur_data = argptr + spec->next;
3760 spec = (void*)host_dm + host_dm->data_start + next;
3761 }
3762 break;
3763 }
3764 case DM_TABLE_DEPS:
3765 {
3766 void *hdata = (void*)host_dm + host_dm->data_start;
3767 int count = *(uint32_t*)hdata;
3768 uint64_t *hdev = hdata + 8;
3769 uint64_t *gdev = argptr + 8;
3770 int i;
3771
3772 *(uint32_t*)argptr = tswap32(count);
3773 for (i = 0; i < count; i++) {
3774 *gdev = tswap64(*hdev);
3775 gdev++;
3776 hdev++;
3777 }
3778 break;
3779 }
3780 case DM_LIST_VERSIONS:
3781 {
3782 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3783 uint32_t remaining_data = guest_data_size;
3784 void *cur_data = argptr;
3785 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3786 int vers_size = thunk_type_size(arg_type, 0);
3787
3788 while (1) {
3789 uint32_t next = vers->next;
3790 if (next) {
3791 vers->next = vers_size + (strlen(vers->name) + 1);
3792 }
3793 if (remaining_data < vers->next) {
3794 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3795 break;
3796 }
3797 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3798 strcpy(cur_data + vers_size, vers->name);
3799 cur_data += vers->next;
3800 remaining_data -= vers->next;
3801 if (!next) {
3802 break;
3803 }
3804 vers = (void*)vers + next;
3805 }
3806 break;
3807 }
3808 default:
Chen Gang Sdec04732015-01-25 08:00:42 +08003809 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01003810 ret = -TARGET_EINVAL;
3811 goto out;
3812 }
3813 unlock_user(argptr, guest_data, guest_data_size);
3814
3815 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3816 if (!argptr) {
3817 ret = -TARGET_EFAULT;
3818 goto out;
3819 }
3820 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3821 unlock_user(argptr, arg, target_size);
3822 }
3823out:
Stefan Weilad11ad72012-09-04 22:14:19 +02003824 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01003825 return ret;
3826}
3827
Alexander Grafa59b5e32014-08-22 13:15:50 +02003828static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003829 int cmd, abi_long arg)
Alexander Grafa59b5e32014-08-22 13:15:50 +02003830{
3831 void *argptr;
3832 int target_size;
3833 const argtype *arg_type = ie->arg_type;
3834 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
3835 abi_long ret;
3836
3837 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
3838 struct blkpg_partition host_part;
3839
3840 /* Read and convert blkpg */
3841 arg_type++;
3842 target_size = thunk_type_size(arg_type, 0);
3843 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3844 if (!argptr) {
3845 ret = -TARGET_EFAULT;
3846 goto out;
3847 }
3848 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3849 unlock_user(argptr, arg, 0);
3850
3851 switch (host_blkpg->op) {
3852 case BLKPG_ADD_PARTITION:
3853 case BLKPG_DEL_PARTITION:
3854 /* payload is struct blkpg_partition */
3855 break;
3856 default:
3857 /* Unknown opcode */
3858 ret = -TARGET_EINVAL;
3859 goto out;
3860 }
3861
3862 /* Read and convert blkpg->data */
3863 arg = (abi_long)(uintptr_t)host_blkpg->data;
3864 target_size = thunk_type_size(part_arg_type, 0);
3865 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3866 if (!argptr) {
3867 ret = -TARGET_EFAULT;
3868 goto out;
3869 }
3870 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
3871 unlock_user(argptr, arg, 0);
3872
3873 /* Swizzle the data pointer to our local copy and call! */
3874 host_blkpg->data = &host_part;
3875 ret = get_errno(ioctl(fd, ie->host_cmd, host_blkpg));
3876
3877out:
3878 return ret;
3879}
3880
Laurent Vivier7ff7b662013-07-02 14:04:12 +01003881static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003882 int fd, int cmd, abi_long arg)
Laurent Vivier7ff7b662013-07-02 14:04:12 +01003883{
3884 const argtype *arg_type = ie->arg_type;
3885 const StructEntry *se;
3886 const argtype *field_types;
3887 const int *dst_offsets, *src_offsets;
3888 int target_size;
3889 void *argptr;
3890 abi_ulong *target_rt_dev_ptr;
3891 unsigned long *host_rt_dev_ptr;
3892 abi_long ret;
3893 int i;
3894
3895 assert(ie->access == IOC_W);
3896 assert(*arg_type == TYPE_PTR);
3897 arg_type++;
3898 assert(*arg_type == TYPE_STRUCT);
3899 target_size = thunk_type_size(arg_type, 0);
3900 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3901 if (!argptr) {
3902 return -TARGET_EFAULT;
3903 }
3904 arg_type++;
3905 assert(*arg_type == (int)STRUCT_rtentry);
3906 se = struct_entries + *arg_type++;
3907 assert(se->convert[0] == NULL);
3908 /* convert struct here to be able to catch rt_dev string */
3909 field_types = se->field_types;
3910 dst_offsets = se->field_offsets[THUNK_HOST];
3911 src_offsets = se->field_offsets[THUNK_TARGET];
3912 for (i = 0; i < se->nb_fields; i++) {
3913 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
3914 assert(*field_types == TYPE_PTRVOID);
3915 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
3916 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
3917 if (*target_rt_dev_ptr != 0) {
3918 *host_rt_dev_ptr = (unsigned long)lock_user_string(
3919 tswapal(*target_rt_dev_ptr));
3920 if (!*host_rt_dev_ptr) {
3921 unlock_user(argptr, arg, 0);
3922 return -TARGET_EFAULT;
3923 }
3924 } else {
3925 *host_rt_dev_ptr = 0;
3926 }
3927 field_types++;
3928 continue;
3929 }
3930 field_types = thunk_convert(buf_temp + dst_offsets[i],
3931 argptr + src_offsets[i],
3932 field_types, THUNK_HOST);
3933 }
3934 unlock_user(argptr, arg, 0);
3935
3936 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3937 if (*host_rt_dev_ptr != 0) {
3938 unlock_user((void *)*host_rt_dev_ptr,
3939 *target_rt_dev_ptr, 0);
3940 }
3941 return ret;
3942}
3943
Paul Burtonca56f5b2014-06-22 11:25:47 +01003944static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003945 int fd, int cmd, abi_long arg)
Paul Burtonca56f5b2014-06-22 11:25:47 +01003946{
3947 int sig = target_to_host_signal(arg);
3948 return get_errno(ioctl(fd, ie->host_cmd, sig));
3949}
3950
blueswir19f106a72008-10-05 10:52:52 +00003951static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00003952#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003953 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3954#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3955 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00003956#include "ioctls.h"
3957 { 0, 0, },
3958};
3959
pbrook53a59602006-03-25 19:31:22 +00003960/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00003961/* do_ioctl() Must return target values and target errnos. */
Laurent Vivier45c874e2015-06-16 00:35:28 +02003962static abi_long do_ioctl(int fd, int cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00003963{
3964 const IOCTLEntry *ie;
3965 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00003966 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003967 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00003968 int target_size;
3969 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00003970
3971 ie = ioctl_entries;
3972 for(;;) {
3973 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003974 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003975 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003976 }
3977 if (ie->target_cmd == cmd)
3978 break;
3979 ie++;
3980 }
3981 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003982#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003983 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003984#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003985 if (ie->do_ioctl) {
3986 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3987 }
3988
bellard31e31b82003-02-18 22:55:36 +00003989 switch(arg_type[0]) {
3990 case TYPE_NULL:
3991 /* no argument */
3992 ret = get_errno(ioctl(fd, ie->host_cmd));
3993 break;
3994 case TYPE_PTRVOID:
3995 case TYPE_INT:
bellard31e31b82003-02-18 22:55:36 +00003996 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3997 break;
3998 case TYPE_PTR:
3999 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00004000 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00004001 switch(ie->access) {
4002 case IOC_R:
4003 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4004 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004005 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4006 if (!argptr)
4007 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004008 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4009 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00004010 }
4011 break;
4012 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00004013 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4014 if (!argptr)
4015 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004016 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4017 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00004018 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4019 break;
4020 default:
4021 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00004022 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4023 if (!argptr)
4024 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004025 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4026 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00004027 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4028 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004029 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4030 if (!argptr)
4031 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004032 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4033 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00004034 }
4035 break;
4036 }
4037 break;
4038 default:
j_mayer32407102007-09-26 23:01:49 +00004039 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
4040 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00004041 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00004042 break;
4043 }
4044 return ret;
4045}
4046
blueswir1b39bc502008-10-05 10:51:10 +00004047static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004048 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
4049 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
4050 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
4051 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
4052 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
4053 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
4054 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
4055 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
4056 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
4057 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
4058 { TARGET_IXON, TARGET_IXON, IXON, IXON },
4059 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
4060 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
4061 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
4062 { 0, 0, 0, 0 }
4063};
4064
blueswir1b39bc502008-10-05 10:51:10 +00004065static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004066 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
4067 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
4068 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
4069 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
4070 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
4071 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
4072 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
4073 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
4074 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
4075 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
4076 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
4077 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
4078 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
4079 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
4080 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
4081 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
4082 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
4083 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
4084 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
4085 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
4086 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
4087 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
4088 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
4089 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
4090 { 0, 0, 0, 0 }
4091};
4092
blueswir1b39bc502008-10-05 10:51:10 +00004093static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004094 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
4095 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
4096 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
4097 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
4098 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
4099 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
4100 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
4101 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
4102 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
4103 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
4104 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
4105 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
4106 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
4107 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
4108 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
4109 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
4110 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
4111 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
4112 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
4113 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
4114 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
4115 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
4116 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
4117 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
4118 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
4119 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
4120 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
4121 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
4122 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
4123 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
4124 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
4125 { 0, 0, 0, 0 }
4126};
4127
blueswir1b39bc502008-10-05 10:51:10 +00004128static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004129 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
4130 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
4131 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
4132 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
4133 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
4134 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
4135 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
4136 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
4137 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
4138 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
4139 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
4140 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
4141 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
4142 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
4143 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
4144 { 0, 0, 0, 0 }
4145};
4146
4147static void target_to_host_termios (void *dst, const void *src)
4148{
4149 struct host_termios *host = dst;
4150 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00004151
ths5fafdf22007-09-16 21:08:06 +00004152 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00004153 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004154 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00004155 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004156 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00004157 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004158 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00004159 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
4160 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00004161
Arnaud Patard44607122009-04-21 17:39:08 +03004162 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00004163 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
4164 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00004165 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00004166 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00004167 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00004168 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00004169 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00004170 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00004171 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00004172 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
4173 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00004174 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
4175 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
4176 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
4177 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
4178 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00004179 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00004180}
ths3b46e622007-09-17 08:09:54 +00004181
bellard31e31b82003-02-18 22:55:36 +00004182static void host_to_target_termios (void *dst, const void *src)
4183{
4184 struct target_termios *target = dst;
4185 const struct host_termios *host = src;
4186
ths5fafdf22007-09-16 21:08:06 +00004187 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00004188 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004189 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00004190 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004191 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00004192 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004193 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00004194 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
4195 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00004196
Arnaud Patard44607122009-04-21 17:39:08 +03004197 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00004198 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
4199 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
4200 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
4201 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
4202 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
4203 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
4204 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
4205 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
4206 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
4207 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
4208 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
4209 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
4210 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
4211 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
4212 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
4213 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
4214 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
4215}
4216
blueswir18e853dc2008-10-05 10:49:32 +00004217static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00004218 .convert = { host_to_target_termios, target_to_host_termios },
4219 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
4220 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
4221};
4222
bellard5286db72003-06-05 00:57:30 +00004223static bitmask_transtbl mmap_flags_tbl[] = {
4224 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
4225 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
4226 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
4227 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
4228 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
4229 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
4230 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
4231 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
Christophe Lyone8efd8e2014-02-03 17:04:32 +01004232 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, MAP_NORESERVE,
4233 MAP_NORESERVE },
bellard5286db72003-06-05 00:57:30 +00004234 { 0, 0, 0, 0 }
4235};
4236
bellard2ab83ea2003-06-15 19:56:46 +00004237#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00004238
4239/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00004240static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00004241
bellard03acab62007-11-11 14:57:14 +00004242static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00004243{
4244 int size;
pbrook53a59602006-03-25 19:31:22 +00004245 void *p;
bellard6dbad632003-03-16 18:05:05 +00004246
4247 if (!ldt_table)
4248 return 0;
4249 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
4250 if (size > bytecount)
4251 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00004252 p = lock_user(VERIFY_WRITE, ptr, size, 0);
4253 if (!p)
bellard03acab62007-11-11 14:57:14 +00004254 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00004255 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00004256 memcpy(p, ldt_table, size);
4257 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00004258 return size;
4259}
4260
4261/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00004262static abi_long write_ldt(CPUX86State *env,
4263 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00004264{
4265 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00004266 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00004267 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00004268 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00004269 uint32_t *lp, entry_1, entry_2;
4270
4271 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00004272 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00004273 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00004274 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004275 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004276 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00004277 ldt_info.limit = tswap32(target_ldt_info->limit);
4278 ldt_info.flags = tswap32(target_ldt_info->flags);
4279 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00004280
bellard6dbad632003-03-16 18:05:05 +00004281 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00004282 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004283 seg_32bit = ldt_info.flags & 1;
4284 contents = (ldt_info.flags >> 1) & 3;
4285 read_exec_only = (ldt_info.flags >> 3) & 1;
4286 limit_in_pages = (ldt_info.flags >> 4) & 1;
4287 seg_not_present = (ldt_info.flags >> 5) & 1;
4288 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00004289#ifdef TARGET_ABI32
4290 lm = 0;
4291#else
4292 lm = (ldt_info.flags >> 7) & 1;
4293#endif
bellard6dbad632003-03-16 18:05:05 +00004294 if (contents == 3) {
4295 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00004296 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004297 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00004298 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004299 }
4300 /* allocate the LDT */
4301 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00004302 env->ldt.base = target_mmap(0,
4303 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
4304 PROT_READ|PROT_WRITE,
4305 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4306 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00004307 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00004308 memset(g2h(env->ldt.base), 0,
4309 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00004310 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00004311 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00004312 }
4313
4314 /* NOTE: same code as Linux kernel */
4315 /* Allow LDTs to be cleared by the user. */
4316 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4317 if (oldmode ||
4318 (contents == 0 &&
4319 read_exec_only == 1 &&
4320 seg_32bit == 0 &&
4321 limit_in_pages == 0 &&
4322 seg_not_present == 1 &&
4323 useable == 0 )) {
4324 entry_1 = 0;
4325 entry_2 = 0;
4326 goto install;
4327 }
4328 }
ths3b46e622007-09-17 08:09:54 +00004329
bellard6dbad632003-03-16 18:05:05 +00004330 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4331 (ldt_info.limit & 0x0ffff);
4332 entry_2 = (ldt_info.base_addr & 0xff000000) |
4333 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4334 (ldt_info.limit & 0xf0000) |
4335 ((read_exec_only ^ 1) << 9) |
4336 (contents << 10) |
4337 ((seg_not_present ^ 1) << 15) |
4338 (seg_32bit << 22) |
4339 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00004340 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00004341 0x7000;
4342 if (!oldmode)
4343 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00004344
bellard6dbad632003-03-16 18:05:05 +00004345 /* Install the new entry ... */
4346install:
4347 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4348 lp[0] = tswap32(entry_1);
4349 lp[1] = tswap32(entry_2);
4350 return 0;
4351}
4352
4353/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00004354static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4355 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00004356{
bellard03acab62007-11-11 14:57:14 +00004357 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00004358
bellard6dbad632003-03-16 18:05:05 +00004359 switch (func) {
4360 case 0:
4361 ret = read_ldt(ptr, bytecount);
4362 break;
4363 case 1:
4364 ret = write_ldt(env, ptr, bytecount, 1);
4365 break;
4366 case 0x11:
4367 ret = write_ldt(env, ptr, bytecount, 0);
4368 break;
bellard03acab62007-11-11 14:57:14 +00004369 default:
4370 ret = -TARGET_ENOSYS;
4371 break;
bellard6dbad632003-03-16 18:05:05 +00004372 }
4373 return ret;
4374}
bellard1b6b0292003-03-22 17:31:38 +00004375
blueswir14583f582008-08-24 10:35:55 +00004376#if defined(TARGET_I386) && defined(TARGET_ABI32)
Alexander Grafbc22eb42013-07-16 18:44:58 +01004377abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004378{
4379 uint64_t *gdt_table = g2h(env->gdt.base);
4380 struct target_modify_ldt_ldt_s ldt_info;
4381 struct target_modify_ldt_ldt_s *target_ldt_info;
4382 int seg_32bit, contents, read_exec_only, limit_in_pages;
4383 int seg_not_present, useable, lm;
4384 uint32_t *lp, entry_1, entry_2;
4385 int i;
4386
4387 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4388 if (!target_ldt_info)
4389 return -TARGET_EFAULT;
4390 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004391 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00004392 ldt_info.limit = tswap32(target_ldt_info->limit);
4393 ldt_info.flags = tswap32(target_ldt_info->flags);
4394 if (ldt_info.entry_number == -1) {
4395 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4396 if (gdt_table[i] == 0) {
4397 ldt_info.entry_number = i;
4398 target_ldt_info->entry_number = tswap32(i);
4399 break;
4400 }
4401 }
4402 }
4403 unlock_user_struct(target_ldt_info, ptr, 1);
4404
4405 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4406 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4407 return -TARGET_EINVAL;
4408 seg_32bit = ldt_info.flags & 1;
4409 contents = (ldt_info.flags >> 1) & 3;
4410 read_exec_only = (ldt_info.flags >> 3) & 1;
4411 limit_in_pages = (ldt_info.flags >> 4) & 1;
4412 seg_not_present = (ldt_info.flags >> 5) & 1;
4413 useable = (ldt_info.flags >> 6) & 1;
4414#ifdef TARGET_ABI32
4415 lm = 0;
4416#else
4417 lm = (ldt_info.flags >> 7) & 1;
4418#endif
4419
4420 if (contents == 3) {
4421 if (seg_not_present == 0)
4422 return -TARGET_EINVAL;
4423 }
4424
4425 /* NOTE: same code as Linux kernel */
4426 /* Allow LDTs to be cleared by the user. */
4427 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4428 if ((contents == 0 &&
4429 read_exec_only == 1 &&
4430 seg_32bit == 0 &&
4431 limit_in_pages == 0 &&
4432 seg_not_present == 1 &&
4433 useable == 0 )) {
4434 entry_1 = 0;
4435 entry_2 = 0;
4436 goto install;
4437 }
4438 }
4439
4440 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4441 (ldt_info.limit & 0x0ffff);
4442 entry_2 = (ldt_info.base_addr & 0xff000000) |
4443 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4444 (ldt_info.limit & 0xf0000) |
4445 ((read_exec_only ^ 1) << 9) |
4446 (contents << 10) |
4447 ((seg_not_present ^ 1) << 15) |
4448 (seg_32bit << 22) |
4449 (limit_in_pages << 23) |
4450 (useable << 20) |
4451 (lm << 21) |
4452 0x7000;
4453
4454 /* Install the new entry ... */
4455install:
4456 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4457 lp[0] = tswap32(entry_1);
4458 lp[1] = tswap32(entry_2);
4459 return 0;
4460}
4461
blueswir18fcd3692008-08-17 20:26:25 +00004462static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004463{
4464 struct target_modify_ldt_ldt_s *target_ldt_info;
4465 uint64_t *gdt_table = g2h(env->gdt.base);
4466 uint32_t base_addr, limit, flags;
4467 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4468 int seg_not_present, useable, lm;
4469 uint32_t *lp, entry_1, entry_2;
4470
4471 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4472 if (!target_ldt_info)
4473 return -TARGET_EFAULT;
4474 idx = tswap32(target_ldt_info->entry_number);
4475 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4476 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4477 unlock_user_struct(target_ldt_info, ptr, 1);
4478 return -TARGET_EINVAL;
4479 }
4480 lp = (uint32_t *)(gdt_table + idx);
4481 entry_1 = tswap32(lp[0]);
4482 entry_2 = tswap32(lp[1]);
4483
4484 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4485 contents = (entry_2 >> 10) & 3;
4486 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4487 seg_32bit = (entry_2 >> 22) & 1;
4488 limit_in_pages = (entry_2 >> 23) & 1;
4489 useable = (entry_2 >> 20) & 1;
4490#ifdef TARGET_ABI32
4491 lm = 0;
4492#else
4493 lm = (entry_2 >> 21) & 1;
4494#endif
4495 flags = (seg_32bit << 0) | (contents << 1) |
4496 (read_exec_only << 3) | (limit_in_pages << 4) |
4497 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4498 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4499 base_addr = (entry_1 >> 16) |
4500 (entry_2 & 0xff000000) |
4501 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004502 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00004503 target_ldt_info->limit = tswap32(limit);
4504 target_ldt_info->flags = tswap32(flags);
4505 unlock_user_struct(target_ldt_info, ptr, 1);
4506 return 0;
4507}
blueswir14583f582008-08-24 10:35:55 +00004508#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00004509
bellardd2fd1af2007-11-14 18:08:56 +00004510#ifndef TARGET_ABI32
Peter Maydell2667e712013-07-16 18:44:59 +01004511abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00004512{
Juan Quintela1add8692011-06-16 17:37:09 +01004513 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00004514 abi_ulong val;
4515 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01004516
bellardd2fd1af2007-11-14 18:08:56 +00004517 switch(code) {
4518 case TARGET_ARCH_SET_GS:
4519 case TARGET_ARCH_SET_FS:
4520 if (code == TARGET_ARCH_SET_GS)
4521 idx = R_GS;
4522 else
4523 idx = R_FS;
4524 cpu_x86_load_seg(env, idx, 0);
4525 env->segs[idx].base = addr;
4526 break;
4527 case TARGET_ARCH_GET_GS:
4528 case TARGET_ARCH_GET_FS:
4529 if (code == TARGET_ARCH_GET_GS)
4530 idx = R_GS;
4531 else
4532 idx = R_FS;
4533 val = env->segs[idx].base;
4534 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01004535 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00004536 break;
4537 default:
4538 ret = -TARGET_EINVAL;
4539 break;
4540 }
Juan Quintela1add8692011-06-16 17:37:09 +01004541 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00004542}
4543#endif
4544
bellard2ab83ea2003-06-15 19:56:46 +00004545#endif /* defined(TARGET_I386) */
4546
Riku Voipio05098a92011-03-04 15:27:29 +02004547#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00004548
pbrookd865bab2008-06-07 22:12:17 +00004549
4550static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4551typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01004552 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00004553 pthread_mutex_t mutex;
4554 pthread_cond_t cond;
4555 pthread_t thread;
4556 uint32_t tid;
4557 abi_ulong child_tidptr;
4558 abi_ulong parent_tidptr;
4559 sigset_t sigmask;
4560} new_thread_info;
4561
4562static void *clone_func(void *arg)
4563{
4564 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004565 CPUArchState *env;
Andreas Färber0d342822012-12-17 07:12:13 +01004566 CPUState *cpu;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004567 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00004568
Emilio G. Cota70903762015-08-23 20:23:41 -04004569 rcu_register_thread();
pbrookd865bab2008-06-07 22:12:17 +00004570 env = info->env;
Andreas Färber0d342822012-12-17 07:12:13 +01004571 cpu = ENV_GET_CPU(env);
Andreas Färbera2247f82013-06-09 19:47:04 +02004572 thread_cpu = cpu;
Andreas Färber0429a972013-08-26 18:14:44 +02004573 ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004574 info->tid = gettid();
Andreas Färber0d342822012-12-17 07:12:13 +01004575 cpu->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004576 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00004577 if (info->child_tidptr)
4578 put_user_u32(info->tid, info->child_tidptr);
4579 if (info->parent_tidptr)
4580 put_user_u32(info->tid, info->parent_tidptr);
4581 /* Enable signals. */
4582 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4583 /* Signal to the parent that we're ready. */
4584 pthread_mutex_lock(&info->mutex);
4585 pthread_cond_broadcast(&info->cond);
4586 pthread_mutex_unlock(&info->mutex);
4587 /* Wait until the parent has finshed initializing the tls state. */
4588 pthread_mutex_lock(&clone_lock);
4589 pthread_mutex_unlock(&clone_lock);
4590 cpu_loop(env);
4591 /* never exits */
4592 return NULL;
4593}
bellard1b6b0292003-03-22 17:31:38 +00004594
ths0da46a62007-10-20 20:23:07 +00004595/* do_fork() Must return host values and target errnos (unlike most
4596 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01004597static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00004598 abi_ulong parent_tidptr, target_ulong newtls,
4599 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00004600{
Andreas Färber0429a972013-08-26 18:14:44 +02004601 CPUState *cpu = ENV_GET_CPU(env);
bellard1b6b0292003-03-22 17:31:38 +00004602 int ret;
bellard5cd43932003-03-29 16:54:36 +00004603 TaskState *ts;
Andreas Färber0429a972013-08-26 18:14:44 +02004604 CPUState *new_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004605 CPUArchState *new_env;
pbrookd865bab2008-06-07 22:12:17 +00004606 unsigned int nptl_flags;
4607 sigset_t sigmask;
ths3b46e622007-09-17 08:09:54 +00004608
balrog436d1242008-09-21 02:39:45 +00004609 /* Emulate vfork() with fork() */
4610 if (flags & CLONE_VFORK)
4611 flags &= ~(CLONE_VFORK | CLONE_VM);
4612
bellard1b6b0292003-03-22 17:31:38 +00004613 if (flags & CLONE_VM) {
Andreas Färber0429a972013-08-26 18:14:44 +02004614 TaskState *parent_ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004615 new_thread_info info;
4616 pthread_attr_t attr;
Peter Maydell24cb36a2013-07-16 18:45:00 +01004617
Markus Armbrusterc78d65e2015-09-14 13:53:03 +02004618 ts = g_new0(TaskState, 1);
pbrook624f7972008-05-31 16:11:38 +00004619 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00004620 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00004621 new_env = cpu_copy(env);
pbrook6e68e072008-05-30 17:22:15 +00004622 /* Init regs that differ from the parent. */
4623 cpu_clone_regs(new_env, newsp);
Andreas Färber0429a972013-08-26 18:14:44 +02004624 new_cpu = ENV_GET_CPU(new_env);
4625 new_cpu->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004626 ts->bprm = parent_ts->bprm;
4627 ts->info = parent_ts->info;
pbrookd865bab2008-06-07 22:12:17 +00004628 nptl_flags = flags;
4629 flags &= ~CLONE_NPTL_FLAGS2;
4630
pbrookc2764712009-03-07 15:24:59 +00004631 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4632 ts->child_tidptr = child_tidptr;
4633 }
4634
pbrookd865bab2008-06-07 22:12:17 +00004635 if (nptl_flags & CLONE_SETTLS)
4636 cpu_set_tls (new_env, newtls);
4637
4638 /* Grab a mutex so that thread setup appears atomic. */
4639 pthread_mutex_lock(&clone_lock);
4640
4641 memset(&info, 0, sizeof(info));
4642 pthread_mutex_init(&info.mutex, NULL);
4643 pthread_mutex_lock(&info.mutex);
4644 pthread_cond_init(&info.cond, NULL);
4645 info.env = new_env;
4646 if (nptl_flags & CLONE_CHILD_SETTID)
4647 info.child_tidptr = child_tidptr;
4648 if (nptl_flags & CLONE_PARENT_SETTID)
4649 info.parent_tidptr = parent_tidptr;
4650
4651 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004652 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4653 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00004654 /* It is not safe to deliver signals until the child has finished
4655 initializing, so temporarily block all signals. */
4656 sigfillset(&sigmask);
4657 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4658
4659 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00004660 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00004661
4662 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4663 pthread_attr_destroy(&attr);
4664 if (ret == 0) {
4665 /* Wait for the child to initialize. */
4666 pthread_cond_wait(&info.cond, &info.mutex);
4667 ret = info.tid;
4668 if (flags & CLONE_PARENT_SETTID)
4669 put_user_u32(ret, parent_tidptr);
4670 } else {
4671 ret = -1;
4672 }
4673 pthread_mutex_unlock(&info.mutex);
4674 pthread_cond_destroy(&info.cond);
4675 pthread_mutex_destroy(&info.mutex);
4676 pthread_mutex_unlock(&clone_lock);
bellard1b6b0292003-03-22 17:31:38 +00004677 } else {
4678 /* if no CLONE_VM, we consider it is a fork */
Timothy E Baldwin93b4eff2015-08-31 00:26:21 +01004679 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0) {
4680 return -TARGET_EINVAL;
4681 }
pbrookd865bab2008-06-07 22:12:17 +00004682 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00004683 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00004684 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00004685 /* Child Process. */
Paolo Bonzinia59629f2015-03-31 13:01:05 +02004686 rcu_after_fork();
pbrookd865bab2008-06-07 22:12:17 +00004687 cpu_clone_regs(env, newsp);
4688 fork_end(1);
aurel322b1319c2008-12-18 22:44:04 +00004689 /* There is a race condition here. The parent process could
4690 theoretically read the TID in the child process before the child
4691 tid is set. This would require using either ptrace
4692 (not implemented) or having *_tidptr to point at a shared memory
4693 mapping. We can't repeat the spinlock hack used above because
4694 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00004695 if (flags & CLONE_CHILD_SETTID)
4696 put_user_u32(gettid(), child_tidptr);
4697 if (flags & CLONE_PARENT_SETTID)
4698 put_user_u32(gettid(), parent_tidptr);
Andreas Färber0429a972013-08-26 18:14:44 +02004699 ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004700 if (flags & CLONE_SETTLS)
4701 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00004702 if (flags & CLONE_CHILD_CLEARTID)
4703 ts->child_tidptr = child_tidptr;
pbrookd865bab2008-06-07 22:12:17 +00004704 } else {
4705 fork_end(0);
4706 }
bellard1b6b0292003-03-22 17:31:38 +00004707 }
4708 return ret;
4709}
4710
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004711/* warning : doesn't handle linux specific flags... */
4712static int target_to_host_fcntl_cmd(int cmd)
4713{
4714 switch(cmd) {
4715 case TARGET_F_DUPFD:
4716 case TARGET_F_GETFD:
4717 case TARGET_F_SETFD:
4718 case TARGET_F_GETFL:
4719 case TARGET_F_SETFL:
4720 return cmd;
4721 case TARGET_F_GETLK:
4722 return F_GETLK;
4723 case TARGET_F_SETLK:
4724 return F_SETLK;
4725 case TARGET_F_SETLKW:
4726 return F_SETLKW;
4727 case TARGET_F_GETOWN:
4728 return F_GETOWN;
4729 case TARGET_F_SETOWN:
4730 return F_SETOWN;
4731 case TARGET_F_GETSIG:
4732 return F_GETSIG;
4733 case TARGET_F_SETSIG:
4734 return F_SETSIG;
4735#if TARGET_ABI_BITS == 32
4736 case TARGET_F_GETLK64:
4737 return F_GETLK64;
4738 case TARGET_F_SETLK64:
4739 return F_SETLK64;
4740 case TARGET_F_SETLKW64:
4741 return F_SETLKW64;
4742#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004743 case TARGET_F_SETLEASE:
4744 return F_SETLEASE;
4745 case TARGET_F_GETLEASE:
4746 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004747#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004748 case TARGET_F_DUPFD_CLOEXEC:
4749 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004750#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004751 case TARGET_F_NOTIFY:
4752 return F_NOTIFY;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01004753#ifdef F_GETOWN_EX
4754 case TARGET_F_GETOWN_EX:
4755 return F_GETOWN_EX;
4756#endif
4757#ifdef F_SETOWN_EX
4758 case TARGET_F_SETOWN_EX:
4759 return F_SETOWN_EX;
4760#endif
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004761 default:
4762 return -TARGET_EINVAL;
4763 }
4764 return -TARGET_EINVAL;
4765}
4766
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004767#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4768static const bitmask_transtbl flock_tbl[] = {
4769 TRANSTBL_CONVERT(F_RDLCK),
4770 TRANSTBL_CONVERT(F_WRLCK),
4771 TRANSTBL_CONVERT(F_UNLCK),
4772 TRANSTBL_CONVERT(F_EXLCK),
4773 TRANSTBL_CONVERT(F_SHLCK),
4774 { 0, 0, 0, 0 }
4775};
4776
blueswir1992f48a2007-10-14 16:27:31 +00004777static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004778{
4779 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004780 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004781 struct flock64 fl64;
4782 struct target_flock64 *target_fl64;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01004783#ifdef F_GETOWN_EX
4784 struct f_owner_ex fox;
4785 struct target_f_owner_ex *target_fox;
4786#endif
blueswir1992f48a2007-10-14 16:27:31 +00004787 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004788 int host_cmd = target_to_host_fcntl_cmd(cmd);
4789
4790 if (host_cmd == -TARGET_EINVAL)
4791 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004792
bellard7775e9e2003-05-14 22:46:48 +00004793 switch(cmd) {
4794 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004795 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4796 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004797 fl.l_type =
4798 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
ths58134272007-03-31 18:59:32 +00004799 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004800 fl.l_start = tswapal(target_fl->l_start);
4801 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004802 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004803 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004804 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004805 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004806 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4807 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004808 target_fl->l_type =
4809 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004810 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004811 target_fl->l_start = tswapal(fl.l_start);
4812 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004813 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004814 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004815 }
4816 break;
ths3b46e622007-09-17 08:09:54 +00004817
bellard7775e9e2003-05-14 22:46:48 +00004818 case TARGET_F_SETLK:
4819 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004820 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4821 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004822 fl.l_type =
4823 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004824 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004825 fl.l_start = tswapal(target_fl->l_start);
4826 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004827 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004828 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004829 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004830 break;
ths3b46e622007-09-17 08:09:54 +00004831
bellard7775e9e2003-05-14 22:46:48 +00004832 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004833 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4834 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004835 fl64.l_type =
4836 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths58134272007-03-31 18:59:32 +00004837 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004838 fl64.l_start = tswap64(target_fl64->l_start);
4839 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004840 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004841 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004842 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004843 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004844 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4845 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004846 target_fl64->l_type =
4847 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004848 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004849 target_fl64->l_start = tswap64(fl64.l_start);
4850 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004851 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004852 unlock_user_struct(target_fl64, arg, 1);
4853 }
bellard9ee1fa22007-11-11 15:11:19 +00004854 break;
bellard7775e9e2003-05-14 22:46:48 +00004855 case TARGET_F_SETLK64:
4856 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004857 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4858 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004859 fl64.l_type =
4860 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004861 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004862 fl64.l_start = tswap64(target_fl64->l_start);
4863 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004864 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004865 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004866 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004867 break;
4868
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004869 case TARGET_F_GETFL:
4870 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004871 if (ret >= 0) {
4872 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4873 }
bellardffa65c32004-01-04 23:57:22 +00004874 break;
4875
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004876 case TARGET_F_SETFL:
4877 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4878 break;
4879
Andreas Schwab8d5d3002014-03-07 15:24:08 +01004880#ifdef F_GETOWN_EX
4881 case TARGET_F_GETOWN_EX:
4882 ret = get_errno(fcntl(fd, host_cmd, &fox));
4883 if (ret >= 0) {
4884 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
4885 return -TARGET_EFAULT;
4886 target_fox->type = tswap32(fox.type);
4887 target_fox->pid = tswap32(fox.pid);
4888 unlock_user_struct(target_fox, arg, 1);
4889 }
4890 break;
4891#endif
4892
4893#ifdef F_SETOWN_EX
4894 case TARGET_F_SETOWN_EX:
4895 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
4896 return -TARGET_EFAULT;
4897 fox.type = tswap32(target_fox->type);
4898 fox.pid = tswap32(target_fox->pid);
4899 unlock_user_struct(target_fox, arg, 0);
4900 ret = get_errno(fcntl(fd, host_cmd, &fox));
4901 break;
4902#endif
4903
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004904 case TARGET_F_SETOWN:
4905 case TARGET_F_GETOWN:
4906 case TARGET_F_SETSIG:
4907 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004908 case TARGET_F_SETLEASE:
4909 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004910 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00004911 break;
4912
bellard7775e9e2003-05-14 22:46:48 +00004913 default:
bellard9ee1fa22007-11-11 15:11:19 +00004914 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00004915 break;
4916 }
4917 return ret;
4918}
4919
bellard67867302003-11-23 17:05:30 +00004920#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00004921
bellard67867302003-11-23 17:05:30 +00004922static inline int high2lowuid(int uid)
4923{
4924 if (uid > 65535)
4925 return 65534;
4926 else
4927 return uid;
4928}
4929
4930static inline int high2lowgid(int gid)
4931{
4932 if (gid > 65535)
4933 return 65534;
4934 else
4935 return gid;
4936}
4937
4938static inline int low2highuid(int uid)
4939{
4940 if ((int16_t)uid == -1)
4941 return -1;
4942 else
4943 return uid;
4944}
4945
4946static inline int low2highgid(int gid)
4947{
4948 if ((int16_t)gid == -1)
4949 return -1;
4950 else
4951 return gid;
4952}
Riku Voipio0c866a72011-04-18 15:23:06 +03004953static inline int tswapid(int id)
4954{
4955 return tswap16(id);
4956}
Peter Maydell76ca3102014-03-02 19:36:41 +00004957
4958#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
4959
Riku Voipio0c866a72011-04-18 15:23:06 +03004960#else /* !USE_UID16 */
4961static inline int high2lowuid(int uid)
4962{
4963 return uid;
4964}
4965static inline int high2lowgid(int gid)
4966{
4967 return gid;
4968}
4969static inline int low2highuid(int uid)
4970{
4971 return uid;
4972}
4973static inline int low2highgid(int gid)
4974{
4975 return gid;
4976}
4977static inline int tswapid(int id)
4978{
4979 return tswap32(id);
4980}
Peter Maydell76ca3102014-03-02 19:36:41 +00004981
4982#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
4983
bellard67867302003-11-23 17:05:30 +00004984#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004985
bellard31e31b82003-02-18 22:55:36 +00004986void syscall_init(void)
4987{
bellard2ab83ea2003-06-15 19:56:46 +00004988 IOCTLEntry *ie;
4989 const argtype *arg_type;
4990 int size;
thsb92c47c2007-11-01 00:07:38 +00004991 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004992
Alexander Graf8be656b2015-05-06 23:47:32 +02004993 thunk_init(STRUCT_MAX);
4994
Blue Swirl001faf32009-05-13 17:53:17 +00004995#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004996#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004997#include "syscall_types.h"
4998#undef STRUCT
4999#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00005000
Peter Maydelldd6e9572012-07-23 08:07:22 +00005001 /* Build target_to_host_errno_table[] table from
5002 * host_to_target_errno_table[]. */
5003 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
5004 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
5005 }
5006
bellard2ab83ea2003-06-15 19:56:46 +00005007 /* we patch the ioctl size if necessary. We rely on the fact that
5008 no ioctl has all the bits at '1' in the size field */
5009 ie = ioctl_entries;
5010 while (ie->target_cmd != 0) {
5011 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
5012 TARGET_IOC_SIZEMASK) {
5013 arg_type = ie->arg_type;
5014 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00005015 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00005016 ie->target_cmd);
5017 exit(1);
5018 }
5019 arg_type++;
5020 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00005021 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00005022 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
5023 (size << TARGET_IOC_SIZESHIFT);
5024 }
thsb92c47c2007-11-01 00:07:38 +00005025
bellard2ab83ea2003-06-15 19:56:46 +00005026 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00005027#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
5028 (defined(__x86_64__) && defined(TARGET_X86_64))
5029 if (unlikely(ie->target_cmd != ie->host_cmd)) {
5030 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
5031 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00005032 }
5033#endif
5034 ie++;
5035 }
bellard31e31b82003-02-18 22:55:36 +00005036}
bellardc573ff62004-01-04 15:51:36 +00005037
blueswir1992f48a2007-10-14 16:27:31 +00005038#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00005039static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
5040{
thsaf325d32008-06-10 15:29:15 +00005041#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00005042 return ((uint64_t)word0 << 32) | word1;
5043#else
5044 return ((uint64_t)word1 << 32) | word0;
5045#endif
5046}
blueswir1992f48a2007-10-14 16:27:31 +00005047#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00005048static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
5049{
5050 return word0;
5051}
blueswir1992f48a2007-10-14 16:27:31 +00005052#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00005053
5054#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00005055static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
5056 abi_long arg2,
5057 abi_long arg3,
5058 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00005059{
Riku Voipio48e515d2011-07-12 15:40:51 +03005060 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00005061 arg2 = arg3;
5062 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03005063 }
pbrookce4defa2006-02-09 16:49:55 +00005064 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
5065}
5066#endif
5067
5068#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00005069static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
5070 abi_long arg2,
5071 abi_long arg3,
5072 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00005073{
Riku Voipio48e515d2011-07-12 15:40:51 +03005074 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00005075 arg2 = arg3;
5076 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03005077 }
pbrookce4defa2006-02-09 16:49:55 +00005078 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
5079}
5080#endif
5081
bellard579a97f2007-11-11 14:26:47 +00005082static inline abi_long target_to_host_timespec(struct timespec *host_ts,
5083 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00005084{
5085 struct target_timespec *target_ts;
5086
bellard579a97f2007-11-11 14:26:47 +00005087 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
5088 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005089 host_ts->tv_sec = tswapal(target_ts->tv_sec);
5090 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00005091 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00005092 return 0;
pbrook53a59602006-03-25 19:31:22 +00005093}
5094
bellard579a97f2007-11-11 14:26:47 +00005095static inline abi_long host_to_target_timespec(abi_ulong target_addr,
5096 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00005097{
5098 struct target_timespec *target_ts;
5099
bellard579a97f2007-11-11 14:26:47 +00005100 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
5101 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005102 target_ts->tv_sec = tswapal(host_ts->tv_sec);
5103 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00005104 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00005105 return 0;
pbrook53a59602006-03-25 19:31:22 +00005106}
5107
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11005108static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
5109 abi_ulong target_addr)
5110{
5111 struct target_itimerspec *target_itspec;
5112
5113 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
5114 return -TARGET_EFAULT;
5115 }
5116
5117 host_itspec->it_interval.tv_sec =
5118 tswapal(target_itspec->it_interval.tv_sec);
5119 host_itspec->it_interval.tv_nsec =
5120 tswapal(target_itspec->it_interval.tv_nsec);
5121 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
5122 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
5123
5124 unlock_user_struct(target_itspec, target_addr, 1);
5125 return 0;
5126}
5127
5128static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
5129 struct itimerspec *host_its)
5130{
5131 struct target_itimerspec *target_itspec;
5132
5133 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
5134 return -TARGET_EFAULT;
5135 }
5136
5137 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
5138 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
5139
5140 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
5141 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
5142
5143 unlock_user_struct(target_itspec, target_addr, 0);
5144 return 0;
5145}
5146
Peter Maydellc0659762014-08-09 15:42:32 +01005147static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
5148 abi_ulong target_addr)
5149{
5150 struct target_sigevent *target_sevp;
5151
5152 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
5153 return -TARGET_EFAULT;
5154 }
5155
5156 /* This union is awkward on 64 bit systems because it has a 32 bit
5157 * integer and a pointer in it; we follow the conversion approach
5158 * used for handling sigval types in signal.c so the guest should get
5159 * the correct value back even if we did a 64 bit byteswap and it's
5160 * using the 32 bit integer.
5161 */
5162 host_sevp->sigev_value.sival_ptr =
5163 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
5164 host_sevp->sigev_signo =
5165 target_to_host_signal(tswap32(target_sevp->sigev_signo));
5166 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
5167 host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
5168
5169 unlock_user_struct(target_sevp, target_addr, 1);
5170 return 0;
5171}
5172
Tom Musta6f6a4032014-08-12 13:53:42 -05005173#if defined(TARGET_NR_mlockall)
5174static inline int target_to_host_mlockall_arg(int arg)
5175{
5176 int result = 0;
5177
5178 if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
5179 result |= MCL_CURRENT;
5180 }
5181 if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
5182 result |= MCL_FUTURE;
5183 }
5184 return result;
5185}
5186#endif
5187
aurel329d33b762009-04-08 23:07:05 +00005188#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00005189static inline abi_long host_to_target_stat64(void *cpu_env,
5190 abi_ulong target_addr,
5191 struct stat *host_st)
5192{
Alexander Graf09701192013-09-03 20:12:15 +01005193#if defined(TARGET_ARM) && defined(TARGET_ABI32)
balrog6a24a772008-09-20 02:23:36 +00005194 if (((CPUARMState *)cpu_env)->eabi) {
5195 struct target_eabi_stat64 *target_st;
5196
5197 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
5198 return -TARGET_EFAULT;
5199 memset(target_st, 0, sizeof(struct target_eabi_stat64));
5200 __put_user(host_st->st_dev, &target_st->st_dev);
5201 __put_user(host_st->st_ino, &target_st->st_ino);
5202#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5203 __put_user(host_st->st_ino, &target_st->__st_ino);
5204#endif
5205 __put_user(host_st->st_mode, &target_st->st_mode);
5206 __put_user(host_st->st_nlink, &target_st->st_nlink);
5207 __put_user(host_st->st_uid, &target_st->st_uid);
5208 __put_user(host_st->st_gid, &target_st->st_gid);
5209 __put_user(host_st->st_rdev, &target_st->st_rdev);
5210 __put_user(host_st->st_size, &target_st->st_size);
5211 __put_user(host_st->st_blksize, &target_st->st_blksize);
5212 __put_user(host_st->st_blocks, &target_st->st_blocks);
5213 __put_user(host_st->st_atime, &target_st->target_st_atime);
5214 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
5215 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
5216 unlock_user_struct(target_st, target_addr, 1);
5217 } else
5218#endif
5219 {
Stefan Weil20d155b2013-10-30 22:52:24 +01005220#if defined(TARGET_HAS_STRUCT_STAT64)
balrog6a24a772008-09-20 02:23:36 +00005221 struct target_stat64 *target_st;
Stefan Weil20d155b2013-10-30 22:52:24 +01005222#else
5223 struct target_stat *target_st;
aurel329d33b762009-04-08 23:07:05 +00005224#endif
balrog6a24a772008-09-20 02:23:36 +00005225
5226 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
5227 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00005228 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00005229 __put_user(host_st->st_dev, &target_st->st_dev);
5230 __put_user(host_st->st_ino, &target_st->st_ino);
5231#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5232 __put_user(host_st->st_ino, &target_st->__st_ino);
5233#endif
5234 __put_user(host_st->st_mode, &target_st->st_mode);
5235 __put_user(host_st->st_nlink, &target_st->st_nlink);
5236 __put_user(host_st->st_uid, &target_st->st_uid);
5237 __put_user(host_st->st_gid, &target_st->st_gid);
5238 __put_user(host_st->st_rdev, &target_st->st_rdev);
5239 /* XXX: better use of kernel struct */
5240 __put_user(host_st->st_size, &target_st->st_size);
5241 __put_user(host_st->st_blksize, &target_st->st_blksize);
5242 __put_user(host_st->st_blocks, &target_st->st_blocks);
5243 __put_user(host_st->st_atime, &target_st->target_st_atime);
5244 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
5245 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
5246 unlock_user_struct(target_st, target_addr, 1);
5247 }
5248
5249 return 0;
5250}
5251#endif
5252
pbrookbd0c5662008-05-29 14:34:11 +00005253/* ??? Using host futex calls even when target atomic operations
5254 are not really atomic probably breaks things. However implementing
5255 futexes locally would make futexes shared between multiple processes
5256 tricky. However they're probably useless because guest atomic
5257 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00005258static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
5259 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00005260{
5261 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07005262 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00005263
5264 /* ??? We assume FUTEX_* constants are the same on both host
5265 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03005266#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07005267 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03005268#else
Nathan Froyda16aae02009-08-03 08:43:29 -07005269 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03005270#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07005271 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00005272 case FUTEX_WAIT:
John Rigbycce246e2013-02-23 16:14:07 -07005273 case FUTEX_WAIT_BITSET:
pbrookbd0c5662008-05-29 14:34:11 +00005274 if (timeout) {
5275 pts = &ts;
5276 target_to_host_timespec(pts, timeout);
5277 } else {
5278 pts = NULL;
5279 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03005280 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
John Rigbycce246e2013-02-23 16:14:07 -07005281 pts, NULL, val3));
pbrookbd0c5662008-05-29 14:34:11 +00005282 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03005283 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00005284 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03005285 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00005286 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00005287 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07005288 case FUTEX_WAKE_OP:
5289 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
5290 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
5291 But the prototype takes a `struct timespec *'; insert casts
5292 to satisfy the compiler. We do not need to tswap TIMEOUT
5293 since it's not compared to guest memory. */
5294 pts = (struct timespec *)(uintptr_t) timeout;
5295 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
5296 g2h(uaddr2),
5297 (base_op == FUTEX_CMP_REQUEUE
5298 ? tswap32(val3)
5299 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00005300 default:
5301 return -TARGET_ENOSYS;
5302 }
5303}
Laurent Vivier0f0426f2015-09-01 22:27:33 +02005304#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5305static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
5306 abi_long handle, abi_long mount_id,
5307 abi_long flags)
5308{
5309 struct file_handle *target_fh;
5310 struct file_handle *fh;
5311 int mid = 0;
5312 abi_long ret;
5313 char *name;
5314 unsigned int size, total_size;
5315
5316 if (get_user_s32(size, handle)) {
5317 return -TARGET_EFAULT;
5318 }
5319
5320 name = lock_user_string(pathname);
5321 if (!name) {
5322 return -TARGET_EFAULT;
5323 }
5324
5325 total_size = sizeof(struct file_handle) + size;
5326 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
5327 if (!target_fh) {
5328 unlock_user(name, pathname, 0);
5329 return -TARGET_EFAULT;
5330 }
5331
5332 fh = g_malloc0(total_size);
5333 fh->handle_bytes = size;
5334
5335 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
5336 unlock_user(name, pathname, 0);
5337
5338 /* man name_to_handle_at(2):
5339 * Other than the use of the handle_bytes field, the caller should treat
5340 * the file_handle structure as an opaque data type
5341 */
5342
5343 memcpy(target_fh, fh, total_size);
5344 target_fh->handle_bytes = tswap32(fh->handle_bytes);
5345 target_fh->handle_type = tswap32(fh->handle_type);
5346 g_free(fh);
5347 unlock_user(target_fh, handle, total_size);
5348
5349 if (put_user_s32(mid, mount_id)) {
5350 return -TARGET_EFAULT;
5351 }
5352
5353 return ret;
5354
5355}
5356#endif
5357
5358#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5359static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
5360 abi_long flags)
5361{
5362 struct file_handle *target_fh;
5363 struct file_handle *fh;
5364 unsigned int size, total_size;
5365 abi_long ret;
5366
5367 if (get_user_s32(size, handle)) {
5368 return -TARGET_EFAULT;
5369 }
5370
5371 total_size = sizeof(struct file_handle) + size;
5372 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
5373 if (!target_fh) {
5374 return -TARGET_EFAULT;
5375 }
5376
Thomas Huthe9d49d52015-10-09 17:56:38 +02005377 fh = g_memdup(target_fh, total_size);
Laurent Vivier0f0426f2015-09-01 22:27:33 +02005378 fh->handle_bytes = size;
5379 fh->handle_type = tswap32(target_fh->handle_type);
5380
5381 ret = get_errno(open_by_handle_at(mount_fd, fh,
5382 target_to_host_bitmask(flags, fcntl_flags_tbl)));
5383
5384 g_free(fh);
5385
5386 unlock_user(target_fh, handle, total_size);
5387
5388 return ret;
5389}
5390#endif
pbrookbd0c5662008-05-29 14:34:11 +00005391
Laurent Viviere36800c2015-10-02 14:48:09 +02005392#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
5393
5394/* signalfd siginfo conversion */
5395
5396static void
5397host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
5398 const struct signalfd_siginfo *info)
5399{
5400 int sig = host_to_target_signal(info->ssi_signo);
5401
5402 /* linux/signalfd.h defines a ssi_addr_lsb
5403 * not defined in sys/signalfd.h but used by some kernels
5404 */
5405
5406#ifdef BUS_MCEERR_AO
5407 if (tinfo->ssi_signo == SIGBUS &&
5408 (tinfo->ssi_code == BUS_MCEERR_AR ||
5409 tinfo->ssi_code == BUS_MCEERR_AO)) {
5410 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
5411 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
5412 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
5413 }
5414#endif
5415
5416 tinfo->ssi_signo = tswap32(sig);
5417 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
5418 tinfo->ssi_code = tswap32(info->ssi_code);
5419 tinfo->ssi_pid = tswap32(info->ssi_pid);
5420 tinfo->ssi_uid = tswap32(info->ssi_uid);
5421 tinfo->ssi_fd = tswap32(info->ssi_fd);
5422 tinfo->ssi_tid = tswap32(info->ssi_tid);
5423 tinfo->ssi_band = tswap32(info->ssi_band);
5424 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
5425 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
5426 tinfo->ssi_status = tswap32(info->ssi_status);
5427 tinfo->ssi_int = tswap32(info->ssi_int);
5428 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
5429 tinfo->ssi_utime = tswap64(info->ssi_utime);
5430 tinfo->ssi_stime = tswap64(info->ssi_stime);
5431 tinfo->ssi_addr = tswap64(info->ssi_addr);
5432}
5433
5434static abi_long host_to_target_signalfd(void *buf, size_t len)
5435{
5436 int i;
5437
5438 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
5439 host_to_target_signalfd_siginfo(buf + i, buf + i);
5440 }
5441
5442 return len;
5443}
5444
5445static TargetFdTrans target_signalfd_trans = {
5446 .host_to_target = host_to_target_signalfd,
5447};
5448
5449static abi_long do_signalfd4(int fd, abi_long mask, int flags)
5450{
5451 int host_flags;
5452 target_sigset_t *target_mask;
5453 sigset_t host_mask;
5454 abi_long ret;
5455
5456 if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
5457 return -TARGET_EINVAL;
5458 }
5459 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
5460 return -TARGET_EFAULT;
5461 }
5462
5463 target_to_host_sigset(&host_mask, target_mask);
5464
5465 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
5466
5467 ret = get_errno(signalfd(fd, &host_mask, host_flags));
5468 if (ret >= 0) {
5469 fd_trans_register(ret, &target_signalfd_trans);
5470 }
5471
5472 unlock_user_struct(target_mask, mask, 0);
5473
5474 return ret;
5475}
5476#endif
5477
pbrook1d9d8b52009-04-16 15:17:02 +00005478/* Map host to target signal numbers for the wait family of syscalls.
5479 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07005480int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00005481{
5482 if (WIFSIGNALED(status)) {
5483 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
5484 }
5485 if (WIFSTOPPED(status)) {
5486 return (host_to_target_signal(WSTOPSIG(status)) << 8)
5487 | (status & 0xff);
5488 }
5489 return status;
5490}
5491
Wim Vander Schelden76b94242014-06-18 11:02:39 +02005492static int open_self_cmdline(void *cpu_env, int fd)
5493{
5494 int fd_orig = -1;
5495 bool word_skipped = false;
5496
5497 fd_orig = open("/proc/self/cmdline", O_RDONLY);
5498 if (fd_orig < 0) {
5499 return fd_orig;
5500 }
5501
5502 while (true) {
5503 ssize_t nb_read;
5504 char buf[128];
5505 char *cp_buf = buf;
5506
5507 nb_read = read(fd_orig, buf, sizeof(buf));
5508 if (nb_read < 0) {
5509 fd_orig = close(fd_orig);
5510 return -1;
5511 } else if (nb_read == 0) {
5512 break;
5513 }
5514
5515 if (!word_skipped) {
5516 /* Skip the first string, which is the path to qemu-*-static
5517 instead of the actual command. */
5518 cp_buf = memchr(buf, 0, sizeof(buf));
5519 if (cp_buf) {
5520 /* Null byte found, skip one string */
5521 cp_buf++;
5522 nb_read -= cp_buf - buf;
5523 word_skipped = true;
5524 }
5525 }
5526
5527 if (word_skipped) {
5528 if (write(fd, cp_buf, nb_read) != nb_read) {
zhanghailiang680dfde2014-08-22 16:23:51 +08005529 close(fd_orig);
Wim Vander Schelden76b94242014-06-18 11:02:39 +02005530 return -1;
5531 }
5532 }
5533 }
5534
5535 return close(fd_orig);
5536}
5537
Alexander Graf36c08d42011-11-02 20:23:24 +01005538static int open_self_maps(void *cpu_env, int fd)
5539{
Andreas Färber0429a972013-08-26 18:14:44 +02005540 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5541 TaskState *ts = cpu->opaque;
Alexander Graf1a49ef22012-05-01 16:30:28 +01005542 FILE *fp;
5543 char *line = NULL;
5544 size_t len = 0;
5545 ssize_t read;
Alexander Graf36c08d42011-11-02 20:23:24 +01005546
Alexander Graf1a49ef22012-05-01 16:30:28 +01005547 fp = fopen("/proc/self/maps", "r");
5548 if (fp == NULL) {
5549 return -EACCES;
5550 }
5551
5552 while ((read = getline(&line, &len, fp)) != -1) {
5553 int fields, dev_maj, dev_min, inode;
5554 uint64_t min, max, offset;
5555 char flag_r, flag_w, flag_x, flag_p;
5556 char path[512] = "";
5557 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
5558 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
5559 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
5560
5561 if ((fields < 10) || (fields > 11)) {
5562 continue;
5563 }
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04005564 if (h2g_valid(min)) {
5565 int flags = page_get_flags(h2g(min));
5566 max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX);
5567 if (page_check_range(h2g(min), max - min, flags) == -1) {
5568 continue;
5569 }
5570 if (h2g(min) == ts->info->stack_limit) {
5571 pstrcpy(path, sizeof(path), " [stack]");
5572 }
Alexander Graf1a49ef22012-05-01 16:30:28 +01005573 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
Christophe Lyone24fed42013-04-02 14:03:38 +02005574 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04005575 h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
Alexander Graf1a49ef22012-05-01 16:30:28 +01005576 flag_x, flag_p, offset, dev_maj, dev_min, inode,
Christophe Lyone24fed42013-04-02 14:03:38 +02005577 path[0] ? " " : "", path);
Alexander Graf1a49ef22012-05-01 16:30:28 +01005578 }
5579 }
5580
5581 free(line);
5582 fclose(fp);
5583
Alexander Graf36c08d42011-11-02 20:23:24 +01005584 return 0;
5585}
5586
Alexander Graf480b8e72011-11-02 20:23:25 +01005587static int open_self_stat(void *cpu_env, int fd)
5588{
Andreas Färber0429a972013-08-26 18:14:44 +02005589 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5590 TaskState *ts = cpu->opaque;
Alexander Graf480b8e72011-11-02 20:23:25 +01005591 abi_ulong start_stack = ts->info->start_stack;
5592 int i;
5593
5594 for (i = 0; i < 44; i++) {
5595 char buf[128];
5596 int len;
5597 uint64_t val = 0;
5598
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005599 if (i == 0) {
5600 /* pid */
5601 val = getpid();
5602 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5603 } else if (i == 1) {
5604 /* app name */
5605 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5606 } else if (i == 27) {
5607 /* stack bottom */
5608 val = start_stack;
5609 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5610 } else {
5611 /* for the rest, there is MasterCard */
5612 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
Alexander Graf480b8e72011-11-02 20:23:25 +01005613 }
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005614
Alexander Graf480b8e72011-11-02 20:23:25 +01005615 len = strlen(buf);
5616 if (write(fd, buf, len) != len) {
5617 return -1;
5618 }
5619 }
5620
5621 return 0;
5622}
5623
Alexander Graf257450e2011-11-02 20:23:26 +01005624static int open_self_auxv(void *cpu_env, int fd)
5625{
Andreas Färber0429a972013-08-26 18:14:44 +02005626 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5627 TaskState *ts = cpu->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01005628 abi_ulong auxv = ts->info->saved_auxv;
5629 abi_ulong len = ts->info->auxv_len;
5630 char *ptr;
5631
5632 /*
5633 * Auxiliary vector is stored in target process stack.
5634 * read in whole auxv vector and copy it to file
5635 */
5636 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5637 if (ptr != NULL) {
5638 while (len > 0) {
5639 ssize_t r;
5640 r = write(fd, ptr, len);
5641 if (r <= 0) {
5642 break;
5643 }
5644 len -= r;
5645 ptr += r;
5646 }
5647 lseek(fd, 0, SEEK_SET);
5648 unlock_user(ptr, auxv, len);
5649 }
5650
5651 return 0;
5652}
5653
Andreas Schwab463d8e72013-07-02 14:04:12 +01005654static int is_proc_myself(const char *filename, const char *entry)
5655{
5656 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
5657 filename += strlen("/proc/");
5658 if (!strncmp(filename, "self/", strlen("self/"))) {
5659 filename += strlen("self/");
5660 } else if (*filename >= '1' && *filename <= '9') {
5661 char myself[80];
5662 snprintf(myself, sizeof(myself), "%d/", getpid());
5663 if (!strncmp(filename, myself, strlen(myself))) {
5664 filename += strlen(myself);
5665 } else {
5666 return 0;
5667 }
5668 } else {
5669 return 0;
5670 }
5671 if (!strcmp(filename, entry)) {
5672 return 1;
5673 }
5674 }
5675 return 0;
5676}
5677
Laurent Vivierde6b9932013-08-30 01:46:40 +02005678#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5679static int is_proc(const char *filename, const char *entry)
5680{
5681 return strcmp(filename, entry) == 0;
5682}
5683
5684static int open_net_route(void *cpu_env, int fd)
5685{
5686 FILE *fp;
5687 char *line = NULL;
5688 size_t len = 0;
5689 ssize_t read;
5690
5691 fp = fopen("/proc/net/route", "r");
5692 if (fp == NULL) {
5693 return -EACCES;
5694 }
5695
5696 /* read header */
5697
5698 read = getline(&line, &len, fp);
5699 dprintf(fd, "%s", line);
5700
5701 /* read routes */
5702
5703 while ((read = getline(&line, &len, fp)) != -1) {
5704 char iface[16];
5705 uint32_t dest, gw, mask;
5706 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
5707 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5708 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
5709 &mask, &mtu, &window, &irtt);
5710 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5711 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
5712 metric, tswap32(mask), mtu, window, irtt);
5713 }
5714
5715 free(line);
5716 fclose(fp);
5717
5718 return 0;
5719}
5720#endif
5721
Riku Voipio0b2effd2014-08-06 10:36:37 +03005722static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
Alexander Graf3be14d02011-11-02 20:23:23 +01005723{
5724 struct fake_open {
5725 const char *filename;
5726 int (*fill)(void *cpu_env, int fd);
Laurent Vivierde6b9932013-08-30 01:46:40 +02005727 int (*cmp)(const char *s1, const char *s2);
Alexander Graf3be14d02011-11-02 20:23:23 +01005728 };
5729 const struct fake_open *fake_open;
5730 static const struct fake_open fakes[] = {
Laurent Vivierde6b9932013-08-30 01:46:40 +02005731 { "maps", open_self_maps, is_proc_myself },
5732 { "stat", open_self_stat, is_proc_myself },
5733 { "auxv", open_self_auxv, is_proc_myself },
Wim Vander Schelden76b94242014-06-18 11:02:39 +02005734 { "cmdline", open_self_cmdline, is_proc_myself },
Laurent Vivierde6b9932013-08-30 01:46:40 +02005735#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5736 { "/proc/net/route", open_net_route, is_proc },
5737#endif
5738 { NULL, NULL, NULL }
Alexander Graf3be14d02011-11-02 20:23:23 +01005739 };
5740
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03005741 if (is_proc_myself(pathname, "exe")) {
5742 int execfd = qemu_getauxval(AT_EXECFD);
Riku Voipio0b2effd2014-08-06 10:36:37 +03005743 return execfd ? execfd : get_errno(sys_openat(dirfd, exec_path, flags, mode));
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03005744 }
5745
Alexander Graf3be14d02011-11-02 20:23:23 +01005746 for (fake_open = fakes; fake_open->filename; fake_open++) {
Laurent Vivierde6b9932013-08-30 01:46:40 +02005747 if (fake_open->cmp(pathname, fake_open->filename)) {
Alexander Graf3be14d02011-11-02 20:23:23 +01005748 break;
5749 }
5750 }
5751
5752 if (fake_open->filename) {
5753 const char *tmpdir;
5754 char filename[PATH_MAX];
5755 int fd, r;
5756
5757 /* create temporary file to map stat to */
5758 tmpdir = getenv("TMPDIR");
5759 if (!tmpdir)
5760 tmpdir = "/tmp";
5761 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5762 fd = mkstemp(filename);
5763 if (fd < 0) {
5764 return fd;
5765 }
5766 unlink(filename);
5767
5768 if ((r = fake_open->fill(cpu_env, fd))) {
5769 close(fd);
5770 return r;
5771 }
5772 lseek(fd, 0, SEEK_SET);
5773
5774 return fd;
5775 }
5776
Riku Voipio0b2effd2014-08-06 10:36:37 +03005777 return get_errno(sys_openat(dirfd, path(pathname), flags, mode));
Alexander Graf3be14d02011-11-02 20:23:23 +01005778}
5779
Alexander Grafaecc8862014-11-10 21:33:03 +01005780#define TIMER_MAGIC 0x0caf0000
5781#define TIMER_MAGIC_MASK 0xffff0000
5782
5783/* Convert QEMU provided timer ID back to internal 16bit index format */
5784static target_timer_t get_timer_id(abi_long arg)
5785{
5786 target_timer_t timerid = arg;
5787
5788 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
5789 return -TARGET_EINVAL;
5790 }
5791
5792 timerid &= 0xffff;
5793
5794 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
5795 return -TARGET_EINVAL;
5796 }
5797
5798 return timerid;
5799}
5800
ths0da46a62007-10-20 20:23:07 +00005801/* do_syscall() should always have a single exit point at the end so
5802 that actions, such as logging of syscall results, can be performed.
5803 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00005804abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5805 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01005806 abi_long arg5, abi_long arg6, abi_long arg7,
5807 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00005808{
Andreas Färber182735e2013-05-29 22:29:20 +02005809 CPUState *cpu = ENV_GET_CPU(cpu_env);
blueswir1992f48a2007-10-14 16:27:31 +00005810 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005811 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00005812 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00005813 void *p;
ths3b46e622007-09-17 08:09:54 +00005814
bellard72f03902003-02-18 23:33:18 +00005815#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00005816 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00005817#endif
thsb92c47c2007-11-01 00:07:38 +00005818 if(do_strace)
5819 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5820
bellard31e31b82003-02-18 22:55:36 +00005821 switch(num) {
5822 case TARGET_NR_exit:
Andreas Färber9b056fc2013-06-24 23:53:10 +02005823 /* In old applications this may be used to implement _exit(2).
5824 However in threaded applictions it is used for thread termination,
5825 and _exit_group is used for application termination.
5826 Do thread termination if we have more then one thread. */
5827 /* FIXME: This probably breaks if a signal arrives. We should probably
5828 be disabling signals. */
Andreas Färberbdc44642013-06-24 23:50:24 +02005829 if (CPU_NEXT(first_cpu)) {
Andreas Färber9b056fc2013-06-24 23:53:10 +02005830 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00005831
Andreas Färber9b056fc2013-06-24 23:53:10 +02005832 cpu_list_lock();
Andreas Färber9b056fc2013-06-24 23:53:10 +02005833 /* Remove the CPU from the list. */
Andreas Färberbdc44642013-06-24 23:50:24 +02005834 QTAILQ_REMOVE(&cpus, cpu, node);
Andreas Färber9b056fc2013-06-24 23:53:10 +02005835 cpu_list_unlock();
Andreas Färber0429a972013-08-26 18:14:44 +02005836 ts = cpu->opaque;
Andreas Färber9b056fc2013-06-24 23:53:10 +02005837 if (ts->child_tidptr) {
5838 put_user_u32(0, ts->child_tidptr);
5839 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5840 NULL, NULL, 0);
5841 }
Andreas Färbera2247f82013-06-09 19:47:04 +02005842 thread_cpu = NULL;
Andreas Färber0429a972013-08-26 18:14:44 +02005843 object_unref(OBJECT(cpu));
Andreas Färber9b056fc2013-06-24 23:53:10 +02005844 g_free(ts);
Emilio G. Cota70903762015-08-23 20:23:41 -04005845 rcu_unregister_thread();
Andreas Färber9b056fc2013-06-24 23:53:10 +02005846 pthread_exit(NULL);
5847 }
Juan Quintela9788c9c2009-07-27 16:13:02 +02005848#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00005849 _mcleanup();
5850#endif
bellarde9009672005-04-26 20:42:36 +00005851 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00005852 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00005853 ret = 0; /* avoid warning */
5854 break;
5855 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00005856 if (arg3 == 0)
5857 ret = 0;
5858 else {
5859 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5860 goto efault;
5861 ret = get_errno(read(arg1, p, arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02005862 if (ret >= 0 &&
5863 fd_trans_host_to_target(arg1)) {
5864 ret = fd_trans_host_to_target(arg1)(p, ret);
5865 }
aurel3238d840e2009-01-30 19:48:17 +00005866 unlock_user(p, arg2, ret);
5867 }
bellard31e31b82003-02-18 22:55:36 +00005868 break;
5869 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00005870 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5871 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005872 ret = get_errno(write(arg1, p, arg3));
5873 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00005874 break;
Chen Gang704eff62015-08-21 05:37:33 +08005875#ifdef TARGET_NR_open
bellard31e31b82003-02-18 22:55:36 +00005876 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00005877 if (!(p = lock_user_string(arg1)))
5878 goto efault;
Riku Voipio0b2effd2014-08-06 10:36:37 +03005879 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
5880 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5881 arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02005882 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00005883 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005884 break;
Chen Gang704eff62015-08-21 05:37:33 +08005885#endif
ths82424832007-09-24 09:21:55 +00005886 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00005887 if (!(p = lock_user_string(arg2)))
5888 goto efault;
Riku Voipio0b2effd2014-08-06 10:36:37 +03005889 ret = get_errno(do_openat(cpu_env, arg1, p,
5890 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5891 arg4));
Laurent Viviere36800c2015-10-02 14:48:09 +02005892 fd_trans_unregister(ret);
bellard579a97f2007-11-11 14:26:47 +00005893 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00005894 break;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02005895#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5896 case TARGET_NR_name_to_handle_at:
5897 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
5898 break;
5899#endif
5900#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5901 case TARGET_NR_open_by_handle_at:
5902 ret = do_open_by_handle_at(arg1, arg2, arg3);
Laurent Viviere36800c2015-10-02 14:48:09 +02005903 fd_trans_unregister(ret);
Laurent Vivier0f0426f2015-09-01 22:27:33 +02005904 break;
5905#endif
bellard31e31b82003-02-18 22:55:36 +00005906 case TARGET_NR_close:
Laurent Viviere36800c2015-10-02 14:48:09 +02005907 fd_trans_unregister(arg1);
bellard31e31b82003-02-18 22:55:36 +00005908 ret = get_errno(close(arg1));
5909 break;
5910 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00005911 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00005912 break;
Chen Gang704eff62015-08-21 05:37:33 +08005913#ifdef TARGET_NR_fork
bellard31e31b82003-02-18 22:55:36 +00005914 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00005915 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00005916 break;
Chen Gang704eff62015-08-21 05:37:33 +08005917#endif
thse5febef2007-04-01 18:31:35 +00005918#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00005919 case TARGET_NR_waitpid:
5920 {
pbrook53a59602006-03-25 19:31:22 +00005921 int status;
5922 ret = get_errno(waitpid(arg1, &status, arg3));
Alexander Graf53795572011-11-24 00:44:43 +01005923 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00005924 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00005925 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005926 }
5927 break;
thse5febef2007-04-01 18:31:35 +00005928#endif
pbrookf0cbb612008-05-30 18:20:05 +00005929#ifdef TARGET_NR_waitid
5930 case TARGET_NR_waitid:
5931 {
5932 siginfo_t info;
5933 info.si_pid = 0;
5934 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5935 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005936 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00005937 goto efault;
5938 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05005939 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00005940 }
5941 }
5942 break;
5943#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005944#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005945 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00005946 if (!(p = lock_user_string(arg1)))
5947 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005948 ret = get_errno(creat(p, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02005949 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00005950 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005951 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005952#endif
Chen Gang704eff62015-08-21 05:37:33 +08005953#ifdef TARGET_NR_link
bellard31e31b82003-02-18 22:55:36 +00005954 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00005955 {
5956 void * p2;
5957 p = lock_user_string(arg1);
5958 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005959 if (!p || !p2)
5960 ret = -TARGET_EFAULT;
5961 else
5962 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005963 unlock_user(p2, arg2, 0);
5964 unlock_user(p, arg1, 0);
5965 }
bellard31e31b82003-02-18 22:55:36 +00005966 break;
Chen Gang704eff62015-08-21 05:37:33 +08005967#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01005968#if defined(TARGET_NR_linkat)
ths64f0ce42007-09-24 09:25:06 +00005969 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00005970 {
5971 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00005972 if (!arg2 || !arg4)
5973 goto efault;
ths64f0ce42007-09-24 09:25:06 +00005974 p = lock_user_string(arg2);
5975 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005976 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005977 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00005978 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01005979 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00005980 unlock_user(p, arg2, 0);
5981 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00005982 }
5983 break;
5984#endif
Chen Gang704eff62015-08-21 05:37:33 +08005985#ifdef TARGET_NR_unlink
bellard31e31b82003-02-18 22:55:36 +00005986 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00005987 if (!(p = lock_user_string(arg1)))
5988 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005989 ret = get_errno(unlink(p));
5990 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005991 break;
Chen Gang704eff62015-08-21 05:37:33 +08005992#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01005993#if defined(TARGET_NR_unlinkat)
ths8170f562007-09-24 09:24:11 +00005994 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00005995 if (!(p = lock_user_string(arg2)))
5996 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005997 ret = get_errno(unlinkat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005998 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00005999 break;
balrogb7d35e62007-12-12 00:40:24 +00006000#endif
bellard31e31b82003-02-18 22:55:36 +00006001 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00006002 {
6003 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00006004 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00006005 abi_ulong gp;
6006 abi_ulong guest_argp;
6007 abi_ulong guest_envp;
6008 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00006009 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006010 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00006011
bellardf7341ff2003-03-30 21:00:25 +00006012 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00006013 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00006014 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00006015 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00006016 goto efault;
ths03aa1972007-12-02 06:28:08 +00006017 if (!addr)
bellard2f619692007-11-16 10:46:05 +00006018 break;
bellard7854b052003-03-29 17:22:23 +00006019 argc++;
bellard2f619692007-11-16 10:46:05 +00006020 }
bellardf7341ff2003-03-30 21:00:25 +00006021 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00006022 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00006023 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00006024 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00006025 goto efault;
ths03aa1972007-12-02 06:28:08 +00006026 if (!addr)
bellard2f619692007-11-16 10:46:05 +00006027 break;
bellard7854b052003-03-29 17:22:23 +00006028 envc++;
bellard2f619692007-11-16 10:46:05 +00006029 }
bellard7854b052003-03-29 17:22:23 +00006030
bellardf7341ff2003-03-30 21:00:25 +00006031 argp = alloca((argc + 1) * sizeof(void *));
6032 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00006033
pbrookda94d262008-05-30 18:24:00 +00006034 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00006035 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006036 if (get_user_ual(addr, gp))
6037 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00006038 if (!addr)
6039 break;
bellard2f619692007-11-16 10:46:05 +00006040 if (!(*q = lock_user_string(addr)))
6041 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006042 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00006043 }
bellardf7341ff2003-03-30 21:00:25 +00006044 *q = NULL;
6045
pbrookda94d262008-05-30 18:24:00 +00006046 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00006047 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006048 if (get_user_ual(addr, gp))
6049 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00006050 if (!addr)
6051 break;
bellard2f619692007-11-16 10:46:05 +00006052 if (!(*q = lock_user_string(addr)))
6053 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006054 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00006055 }
bellardf7341ff2003-03-30 21:00:25 +00006056 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00006057
bellard2f619692007-11-16 10:46:05 +00006058 if (!(p = lock_user_string(arg1)))
6059 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00006060 ret = get_errno(execve(p, argp, envp));
6061 unlock_user(p, arg1, 0);
6062
bellard2f619692007-11-16 10:46:05 +00006063 goto execve_end;
6064
6065 execve_efault:
6066 ret = -TARGET_EFAULT;
6067
6068 execve_end:
pbrook53a59602006-03-25 19:31:22 +00006069 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00006070 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006071 if (get_user_ual(addr, gp)
6072 || !addr)
6073 break;
pbrook53a59602006-03-25 19:31:22 +00006074 unlock_user(*q, addr, 0);
6075 }
6076 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00006077 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006078 if (get_user_ual(addr, gp)
6079 || !addr)
6080 break;
pbrook53a59602006-03-25 19:31:22 +00006081 unlock_user(*q, addr, 0);
6082 }
bellard7854b052003-03-29 17:22:23 +00006083 }
bellard31e31b82003-02-18 22:55:36 +00006084 break;
6085 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00006086 if (!(p = lock_user_string(arg1)))
6087 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006088 ret = get_errno(chdir(p));
6089 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006090 break;
bellarda315a142005-01-30 22:59:18 +00006091#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00006092 case TARGET_NR_time:
6093 {
pbrook53a59602006-03-25 19:31:22 +00006094 time_t host_time;
6095 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00006096 if (!is_error(ret)
6097 && arg1
6098 && put_user_sal(host_time, arg1))
6099 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006100 }
6101 break;
bellarda315a142005-01-30 22:59:18 +00006102#endif
Chen Gang704eff62015-08-21 05:37:33 +08006103#ifdef TARGET_NR_mknod
bellard31e31b82003-02-18 22:55:36 +00006104 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00006105 if (!(p = lock_user_string(arg1)))
6106 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006107 ret = get_errno(mknod(p, arg2, arg3));
6108 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006109 break;
Chen Gang704eff62015-08-21 05:37:33 +08006110#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006111#if defined(TARGET_NR_mknodat)
ths75ac37a2007-09-24 09:23:05 +00006112 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00006113 if (!(p = lock_user_string(arg2)))
6114 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006115 ret = get_errno(mknodat(arg1, p, arg3, arg4));
bellard579a97f2007-11-11 14:26:47 +00006116 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00006117 break;
6118#endif
Chen Gang704eff62015-08-21 05:37:33 +08006119#ifdef TARGET_NR_chmod
bellard31e31b82003-02-18 22:55:36 +00006120 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00006121 if (!(p = lock_user_string(arg1)))
6122 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006123 ret = get_errno(chmod(p, arg2));
6124 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006125 break;
Chen Gang704eff62015-08-21 05:37:33 +08006126#endif
bellardebc05482003-09-30 21:08:41 +00006127#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00006128 case TARGET_NR_break:
6129 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006130#endif
6131#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00006132 case TARGET_NR_oldstat:
6133 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006134#endif
bellard31e31b82003-02-18 22:55:36 +00006135 case TARGET_NR_lseek:
6136 ret = get_errno(lseek(arg1, arg2, arg3));
6137 break;
Richard Henderson92317332010-05-03 10:07:53 -07006138#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
6139 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00006140 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07006141 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00006142 ret = get_errno(getpid());
6143 break;
Richard Henderson92317332010-05-03 10:07:53 -07006144#endif
6145#ifdef TARGET_NR_getpid
6146 case TARGET_NR_getpid:
6147 ret = get_errno(getpid());
6148 break;
6149#endif
bellard31e31b82003-02-18 22:55:36 +00006150 case TARGET_NR_mount:
Paul Burton356d7712014-06-22 11:25:37 +01006151 {
6152 /* need to look at the data field */
6153 void *p2, *p3;
6154
6155 if (arg1) {
6156 p = lock_user_string(arg1);
6157 if (!p) {
6158 goto efault;
6159 }
6160 } else {
6161 p = NULL;
6162 }
6163
6164 p2 = lock_user_string(arg2);
6165 if (!p2) {
6166 if (arg1) {
6167 unlock_user(p, arg1, 0);
6168 }
6169 goto efault;
6170 }
6171
6172 if (arg3) {
6173 p3 = lock_user_string(arg3);
6174 if (!p3) {
6175 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00006176 unlock_user(p, arg1, 0);
Paul Burton356d7712014-06-22 11:25:37 +01006177 }
6178 unlock_user(p2, arg2, 0);
6179 goto efault;
6180 }
6181 } else {
6182 p3 = NULL;
6183 }
6184
6185 /* FIXME - arg5 should be locked, but it isn't clear how to
6186 * do that since it's not guaranteed to be a NULL-terminated
6187 * string.
6188 */
6189 if (!arg5) {
6190 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
6191 } else {
6192 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
6193 }
6194 ret = get_errno(ret);
6195
6196 if (arg1) {
6197 unlock_user(p, arg1, 0);
6198 }
6199 unlock_user(p2, arg2, 0);
6200 if (arg3) {
6201 unlock_user(p3, arg3, 0);
6202 }
6203 }
6204 break;
thse5febef2007-04-01 18:31:35 +00006205#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00006206 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00006207 if (!(p = lock_user_string(arg1)))
6208 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006209 ret = get_errno(umount(p));
6210 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006211 break;
thse5febef2007-04-01 18:31:35 +00006212#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006213#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006214 case TARGET_NR_stime:
6215 {
pbrook53a59602006-03-25 19:31:22 +00006216 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00006217 if (get_user_sal(host_time, arg1))
6218 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006219 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00006220 }
6221 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006222#endif
bellard31e31b82003-02-18 22:55:36 +00006223 case TARGET_NR_ptrace:
6224 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00006225#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006226 case TARGET_NR_alarm:
6227 ret = alarm(arg1);
6228 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006229#endif
bellardebc05482003-09-30 21:08:41 +00006230#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00006231 case TARGET_NR_oldfstat:
6232 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006233#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006234#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006235 case TARGET_NR_pause:
6236 ret = get_errno(pause());
6237 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006238#endif
thse5febef2007-04-01 18:31:35 +00006239#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00006240 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00006241 {
pbrook53a59602006-03-25 19:31:22 +00006242 struct utimbuf tbuf, *host_tbuf;
6243 struct target_utimbuf *target_tbuf;
6244 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00006245 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
6246 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006247 tbuf.actime = tswapal(target_tbuf->actime);
6248 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00006249 unlock_user_struct(target_tbuf, arg2, 0);
6250 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00006251 } else {
pbrook53a59602006-03-25 19:31:22 +00006252 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00006253 }
bellard579a97f2007-11-11 14:26:47 +00006254 if (!(p = lock_user_string(arg1)))
6255 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006256 ret = get_errno(utime(p, host_tbuf));
6257 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00006258 }
6259 break;
thse5febef2007-04-01 18:31:35 +00006260#endif
Chen Gang704eff62015-08-21 05:37:33 +08006261#ifdef TARGET_NR_utimes
bellard978a66f2004-12-06 22:58:05 +00006262 case TARGET_NR_utimes:
6263 {
bellard978a66f2004-12-06 22:58:05 +00006264 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00006265 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00006266 if (copy_from_user_timeval(&tv[0], arg2)
6267 || copy_from_user_timeval(&tv[1],
6268 arg2 + sizeof(struct target_timeval)))
6269 goto efault;
bellard978a66f2004-12-06 22:58:05 +00006270 tvp = tv;
6271 } else {
6272 tvp = NULL;
6273 }
bellard579a97f2007-11-11 14:26:47 +00006274 if (!(p = lock_user_string(arg1)))
6275 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006276 ret = get_errno(utimes(p, tvp));
6277 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00006278 }
6279 break;
Chen Gang704eff62015-08-21 05:37:33 +08006280#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006281#if defined(TARGET_NR_futimesat)
balrogac8a6552008-09-20 02:25:39 +00006282 case TARGET_NR_futimesat:
6283 {
6284 struct timeval *tvp, tv[2];
6285 if (arg3) {
6286 if (copy_from_user_timeval(&tv[0], arg3)
6287 || copy_from_user_timeval(&tv[1],
6288 arg3 + sizeof(struct target_timeval)))
6289 goto efault;
6290 tvp = tv;
6291 } else {
6292 tvp = NULL;
6293 }
6294 if (!(p = lock_user_string(arg2)))
6295 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006296 ret = get_errno(futimesat(arg1, path(p), tvp));
balrogac8a6552008-09-20 02:25:39 +00006297 unlock_user(p, arg2, 0);
6298 }
6299 break;
6300#endif
bellardebc05482003-09-30 21:08:41 +00006301#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00006302 case TARGET_NR_stty:
6303 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006304#endif
6305#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00006306 case TARGET_NR_gtty:
6307 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006308#endif
Chen Gang704eff62015-08-21 05:37:33 +08006309#ifdef TARGET_NR_access
bellard31e31b82003-02-18 22:55:36 +00006310 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00006311 if (!(p = lock_user_string(arg1)))
6312 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02006313 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00006314 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006315 break;
Chen Gang704eff62015-08-21 05:37:33 +08006316#endif
ths92a34c12007-09-24 09:27:49 +00006317#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
6318 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00006319 if (!(p = lock_user_string(arg2)))
6320 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006321 ret = get_errno(faccessat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00006322 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00006323 break;
6324#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006325#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006326 case TARGET_NR_nice:
6327 ret = get_errno(nice(arg1));
6328 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006329#endif
bellardebc05482003-09-30 21:08:41 +00006330#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00006331 case TARGET_NR_ftime:
6332 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006333#endif
bellard31e31b82003-02-18 22:55:36 +00006334 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00006335 sync();
6336 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00006337 break;
6338 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00006339 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00006340 break;
Chen Gang704eff62015-08-21 05:37:33 +08006341#ifdef TARGET_NR_rename
bellard31e31b82003-02-18 22:55:36 +00006342 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00006343 {
6344 void *p2;
6345 p = lock_user_string(arg1);
6346 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006347 if (!p || !p2)
6348 ret = -TARGET_EFAULT;
6349 else
6350 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006351 unlock_user(p2, arg2, 0);
6352 unlock_user(p, arg1, 0);
6353 }
bellard31e31b82003-02-18 22:55:36 +00006354 break;
Chen Gang704eff62015-08-21 05:37:33 +08006355#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006356#if defined(TARGET_NR_renameat)
ths722183f2007-09-24 09:24:37 +00006357 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00006358 {
bellard579a97f2007-11-11 14:26:47 +00006359 void *p2;
ths722183f2007-09-24 09:24:37 +00006360 p = lock_user_string(arg2);
6361 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00006362 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006363 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00006364 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01006365 ret = get_errno(renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00006366 unlock_user(p2, arg4, 0);
6367 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00006368 }
6369 break;
6370#endif
Chen Gang704eff62015-08-21 05:37:33 +08006371#ifdef TARGET_NR_mkdir
bellard31e31b82003-02-18 22:55:36 +00006372 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00006373 if (!(p = lock_user_string(arg1)))
6374 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006375 ret = get_errno(mkdir(p, arg2));
6376 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006377 break;
Chen Gang704eff62015-08-21 05:37:33 +08006378#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006379#if defined(TARGET_NR_mkdirat)
ths4472ad02007-09-24 09:22:32 +00006380 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00006381 if (!(p = lock_user_string(arg2)))
6382 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006383 ret = get_errno(mkdirat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00006384 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00006385 break;
6386#endif
Chen Gang704eff62015-08-21 05:37:33 +08006387#ifdef TARGET_NR_rmdir
bellard31e31b82003-02-18 22:55:36 +00006388 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00006389 if (!(p = lock_user_string(arg1)))
6390 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006391 ret = get_errno(rmdir(p));
6392 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006393 break;
Chen Gang704eff62015-08-21 05:37:33 +08006394#endif
bellard31e31b82003-02-18 22:55:36 +00006395 case TARGET_NR_dup:
6396 ret = get_errno(dup(arg1));
Laurent Viviere36800c2015-10-02 14:48:09 +02006397 if (ret >= 0) {
6398 fd_trans_dup(arg1, ret);
6399 }
bellard31e31b82003-02-18 22:55:36 +00006400 break;
Chen Gang704eff62015-08-21 05:37:33 +08006401#ifdef TARGET_NR_pipe
bellard31e31b82003-02-18 22:55:36 +00006402 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07006403 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00006404 break;
Chen Gang704eff62015-08-21 05:37:33 +08006405#endif
Riku Voipio099d6b02009-05-05 12:10:04 +03006406#ifdef TARGET_NR_pipe2
6407 case TARGET_NR_pipe2:
Richard Hendersone7ea6cb2012-06-01 18:48:39 -07006408 ret = do_pipe(cpu_env, arg1,
6409 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03006410 break;
6411#endif
bellard31e31b82003-02-18 22:55:36 +00006412 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00006413 {
pbrook53a59602006-03-25 19:31:22 +00006414 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00006415 struct tms tms;
6416 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00006417 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00006418 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
6419 if (!tmsp)
6420 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006421 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
6422 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
6423 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
6424 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00006425 }
bellardc596ed12003-07-13 17:32:31 +00006426 if (!is_error(ret))
6427 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00006428 }
6429 break;
bellardebc05482003-09-30 21:08:41 +00006430#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00006431 case TARGET_NR_prof:
6432 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006433#endif
thse5febef2007-04-01 18:31:35 +00006434#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00006435 case TARGET_NR_signal:
6436 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006437#endif
bellard31e31b82003-02-18 22:55:36 +00006438 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00006439 if (arg1 == 0) {
6440 ret = get_errno(acct(NULL));
6441 } else {
6442 if (!(p = lock_user_string(arg1)))
6443 goto efault;
6444 ret = get_errno(acct(path(p)));
6445 unlock_user(p, arg1, 0);
6446 }
pbrook24836682006-04-16 14:14:53 +00006447 break;
Richard Henderson8070e7b2013-07-24 09:50:00 -10006448#ifdef TARGET_NR_umount2
bellard31e31b82003-02-18 22:55:36 +00006449 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00006450 if (!(p = lock_user_string(arg1)))
6451 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006452 ret = get_errno(umount2(p, arg2));
6453 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006454 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006455#endif
bellardebc05482003-09-30 21:08:41 +00006456#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00006457 case TARGET_NR_lock:
6458 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006459#endif
bellard31e31b82003-02-18 22:55:36 +00006460 case TARGET_NR_ioctl:
6461 ret = do_ioctl(arg1, arg2, arg3);
6462 break;
6463 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00006464 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00006465 break;
bellardebc05482003-09-30 21:08:41 +00006466#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00006467 case TARGET_NR_mpx:
6468 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006469#endif
bellard31e31b82003-02-18 22:55:36 +00006470 case TARGET_NR_setpgid:
6471 ret = get_errno(setpgid(arg1, arg2));
6472 break;
bellardebc05482003-09-30 21:08:41 +00006473#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00006474 case TARGET_NR_ulimit:
6475 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006476#endif
6477#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00006478 case TARGET_NR_oldolduname:
6479 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006480#endif
bellard31e31b82003-02-18 22:55:36 +00006481 case TARGET_NR_umask:
6482 ret = get_errno(umask(arg1));
6483 break;
6484 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00006485 if (!(p = lock_user_string(arg1)))
6486 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006487 ret = get_errno(chroot(p));
6488 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006489 break;
Chen Gang704eff62015-08-21 05:37:33 +08006490#ifdef TARGET_NR_ustat
bellard31e31b82003-02-18 22:55:36 +00006491 case TARGET_NR_ustat:
6492 goto unimplemented;
Chen Gang704eff62015-08-21 05:37:33 +08006493#endif
6494#ifdef TARGET_NR_dup2
bellard31e31b82003-02-18 22:55:36 +00006495 case TARGET_NR_dup2:
6496 ret = get_errno(dup2(arg1, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02006497 if (ret >= 0) {
6498 fd_trans_dup(arg1, arg2);
6499 }
bellard31e31b82003-02-18 22:55:36 +00006500 break;
Chen Gang704eff62015-08-21 05:37:33 +08006501#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03006502#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
6503 case TARGET_NR_dup3:
6504 ret = get_errno(dup3(arg1, arg2, arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02006505 if (ret >= 0) {
6506 fd_trans_dup(arg1, arg2);
6507 }
Ulrich Hechtd0927932009-09-17 20:22:14 +03006508 break;
6509#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006510#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006511 case TARGET_NR_getppid:
6512 ret = get_errno(getppid());
6513 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006514#endif
Chen Gang704eff62015-08-21 05:37:33 +08006515#ifdef TARGET_NR_getpgrp
bellard31e31b82003-02-18 22:55:36 +00006516 case TARGET_NR_getpgrp:
6517 ret = get_errno(getpgrp());
6518 break;
Chen Gang704eff62015-08-21 05:37:33 +08006519#endif
bellard31e31b82003-02-18 22:55:36 +00006520 case TARGET_NR_setsid:
6521 ret = get_errno(setsid());
6522 break;
thse5febef2007-04-01 18:31:35 +00006523#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00006524 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00006525 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08006526#if defined(TARGET_ALPHA)
6527 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00006528 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00006529 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00006530 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6531 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006532 act._sa_handler = old_act->_sa_handler;
6533 target_siginitset(&act.sa_mask, old_act->sa_mask);
6534 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08006535 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00006536 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006537 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00006538 }
6539 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00006540 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00006541 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6542 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006543 old_act->_sa_handler = oact._sa_handler;
6544 old_act->sa_mask = oact.sa_mask.sig[0];
6545 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00006546 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00006547 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08006548#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00006549 struct target_sigaction act, oact, *pact, *old_act;
6550
6551 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00006552 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6553 goto efault;
bellard106ec872006-06-27 21:08:10 +00006554 act._sa_handler = old_act->_sa_handler;
6555 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
6556 act.sa_flags = old_act->sa_flags;
6557 unlock_user_struct(old_act, arg2, 0);
6558 pact = &act;
6559 } else {
6560 pact = NULL;
6561 }
6562
6563 ret = get_errno(do_sigaction(arg1, pact, &oact));
6564
6565 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00006566 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6567 goto efault;
bellard106ec872006-06-27 21:08:10 +00006568 old_act->_sa_handler = oact._sa_handler;
6569 old_act->sa_flags = oact.sa_flags;
6570 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
6571 old_act->sa_mask.sig[1] = 0;
6572 old_act->sa_mask.sig[2] = 0;
6573 old_act->sa_mask.sig[3] = 0;
6574 unlock_user_struct(old_act, arg3, 1);
6575 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08006576#else
6577 struct target_old_sigaction *old_act;
6578 struct target_sigaction act, oact, *pact;
6579 if (arg2) {
6580 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6581 goto efault;
6582 act._sa_handler = old_act->_sa_handler;
6583 target_siginitset(&act.sa_mask, old_act->sa_mask);
6584 act.sa_flags = old_act->sa_flags;
6585 act.sa_restorer = old_act->sa_restorer;
6586 unlock_user_struct(old_act, arg2, 0);
6587 pact = &act;
6588 } else {
6589 pact = NULL;
6590 }
6591 ret = get_errno(do_sigaction(arg1, pact, &oact));
6592 if (!is_error(ret) && arg3) {
6593 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6594 goto efault;
6595 old_act->_sa_handler = oact._sa_handler;
6596 old_act->sa_mask = oact.sa_mask.sig[0];
6597 old_act->sa_flags = oact.sa_flags;
6598 old_act->sa_restorer = oact.sa_restorer;
6599 unlock_user_struct(old_act, arg3, 1);
6600 }
ths388bb212007-05-13 13:58:00 +00006601#endif
bellard31e31b82003-02-18 22:55:36 +00006602 }
6603 break;
thse5febef2007-04-01 18:31:35 +00006604#endif
bellard66fb9762003-03-23 01:06:05 +00006605 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00006606 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08006607#if defined(TARGET_ALPHA)
6608 struct target_sigaction act, oact, *pact = 0;
6609 struct target_rt_sigaction *rt_act;
6610 /* ??? arg4 == sizeof(sigset_t). */
6611 if (arg2) {
6612 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
6613 goto efault;
6614 act._sa_handler = rt_act->_sa_handler;
6615 act.sa_mask = rt_act->sa_mask;
6616 act.sa_flags = rt_act->sa_flags;
6617 act.sa_restorer = arg5;
6618 unlock_user_struct(rt_act, arg2, 0);
6619 pact = &act;
6620 }
6621 ret = get_errno(do_sigaction(arg1, pact, &oact));
6622 if (!is_error(ret) && arg3) {
6623 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
6624 goto efault;
6625 rt_act->_sa_handler = oact._sa_handler;
6626 rt_act->sa_mask = oact.sa_mask;
6627 rt_act->sa_flags = oact.sa_flags;
6628 unlock_user_struct(rt_act, arg3, 1);
6629 }
6630#else
pbrook53a59602006-03-25 19:31:22 +00006631 struct target_sigaction *act;
6632 struct target_sigaction *oact;
6633
bellard579a97f2007-11-11 14:26:47 +00006634 if (arg2) {
6635 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
6636 goto efault;
6637 } else
pbrook53a59602006-03-25 19:31:22 +00006638 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00006639 if (arg3) {
6640 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
6641 ret = -TARGET_EFAULT;
6642 goto rt_sigaction_fail;
6643 }
6644 } else
pbrook53a59602006-03-25 19:31:22 +00006645 oact = NULL;
6646 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00006647 rt_sigaction_fail:
6648 if (act)
pbrook53a59602006-03-25 19:31:22 +00006649 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00006650 if (oact)
pbrook53a59602006-03-25 19:31:22 +00006651 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08006652#endif
pbrook53a59602006-03-25 19:31:22 +00006653 }
bellard66fb9762003-03-23 01:06:05 +00006654 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006655#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006656 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00006657 {
6658 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00006659 abi_ulong target_set;
Alex Barcelo1c275922014-03-14 14:36:55 +00006660 do_sigprocmask(0, NULL, &cur_set);
bellard66fb9762003-03-23 01:06:05 +00006661 host_to_target_old_sigset(&target_set, &cur_set);
6662 ret = target_set;
6663 }
6664 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006665#endif
6666#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006667 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00006668 {
6669 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00006670 abi_ulong target_set = arg1;
Alex Barcelo1c275922014-03-14 14:36:55 +00006671 do_sigprocmask(0, NULL, &cur_set);
bellard66fb9762003-03-23 01:06:05 +00006672 target_to_host_old_sigset(&set, &target_set);
6673 sigorset(&set, &set, &cur_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00006674 do_sigprocmask(SIG_SETMASK, &set, &oset);
bellard66fb9762003-03-23 01:06:05 +00006675 host_to_target_old_sigset(&target_set, &oset);
6676 ret = target_set;
6677 }
6678 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006679#endif
thse5febef2007-04-01 18:31:35 +00006680#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00006681 case TARGET_NR_sigprocmask:
6682 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07006683#if defined(TARGET_ALPHA)
6684 sigset_t set, oldset;
6685 abi_ulong mask;
6686 int how;
6687
6688 switch (arg1) {
6689 case TARGET_SIG_BLOCK:
6690 how = SIG_BLOCK;
6691 break;
6692 case TARGET_SIG_UNBLOCK:
6693 how = SIG_UNBLOCK;
6694 break;
6695 case TARGET_SIG_SETMASK:
6696 how = SIG_SETMASK;
6697 break;
6698 default:
6699 ret = -TARGET_EINVAL;
6700 goto fail;
6701 }
6702 mask = arg2;
6703 target_to_host_old_sigset(&set, &mask);
6704
Alex Barcelo1c275922014-03-14 14:36:55 +00006705 ret = get_errno(do_sigprocmask(how, &set, &oldset));
Richard Hendersona5b3b132010-05-03 10:07:55 -07006706 if (!is_error(ret)) {
6707 host_to_target_old_sigset(&mask, &oldset);
6708 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07006709 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07006710 }
6711#else
bellard66fb9762003-03-23 01:06:05 +00006712 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07006713 int how;
ths3b46e622007-09-17 08:09:54 +00006714
pbrook53a59602006-03-25 19:31:22 +00006715 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07006716 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00006717 case TARGET_SIG_BLOCK:
6718 how = SIG_BLOCK;
6719 break;
6720 case TARGET_SIG_UNBLOCK:
6721 how = SIG_UNBLOCK;
6722 break;
6723 case TARGET_SIG_SETMASK:
6724 how = SIG_SETMASK;
6725 break;
6726 default:
ths0da46a62007-10-20 20:23:07 +00006727 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006728 goto fail;
6729 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006730 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006731 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006732 target_to_host_old_sigset(&set, p);
6733 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006734 set_ptr = &set;
6735 } else {
6736 how = 0;
6737 set_ptr = NULL;
6738 }
Alex Barcelo1c275922014-03-14 14:36:55 +00006739 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006740 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006741 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006742 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006743 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006744 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006745 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07006746#endif
bellard66fb9762003-03-23 01:06:05 +00006747 }
6748 break;
thse5febef2007-04-01 18:31:35 +00006749#endif
bellard66fb9762003-03-23 01:06:05 +00006750 case TARGET_NR_rt_sigprocmask:
6751 {
6752 int how = arg1;
6753 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00006754
pbrook53a59602006-03-25 19:31:22 +00006755 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006756 switch(how) {
6757 case TARGET_SIG_BLOCK:
6758 how = SIG_BLOCK;
6759 break;
6760 case TARGET_SIG_UNBLOCK:
6761 how = SIG_UNBLOCK;
6762 break;
6763 case TARGET_SIG_SETMASK:
6764 how = SIG_SETMASK;
6765 break;
6766 default:
ths0da46a62007-10-20 20:23:07 +00006767 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006768 goto fail;
6769 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006770 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006771 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006772 target_to_host_sigset(&set, p);
6773 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006774 set_ptr = &set;
6775 } else {
6776 how = 0;
6777 set_ptr = NULL;
6778 }
Alex Barcelo1c275922014-03-14 14:36:55 +00006779 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006780 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006781 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006782 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006783 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006784 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006785 }
6786 }
6787 break;
thse5febef2007-04-01 18:31:35 +00006788#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00006789 case TARGET_NR_sigpending:
6790 {
6791 sigset_t set;
6792 ret = get_errno(sigpending(&set));
6793 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006794 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006795 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006796 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006797 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006798 }
6799 }
6800 break;
thse5febef2007-04-01 18:31:35 +00006801#endif
bellard66fb9762003-03-23 01:06:05 +00006802 case TARGET_NR_rt_sigpending:
6803 {
6804 sigset_t set;
6805 ret = get_errno(sigpending(&set));
6806 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006807 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006808 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006809 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006810 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006811 }
6812 }
6813 break;
thse5febef2007-04-01 18:31:35 +00006814#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00006815 case TARGET_NR_sigsuspend:
6816 {
6817 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07006818#if defined(TARGET_ALPHA)
6819 abi_ulong mask = arg1;
6820 target_to_host_old_sigset(&set, &mask);
6821#else
Anthony Liguoric227f092009-10-01 16:12:16 -05006822 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006823 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006824 target_to_host_old_sigset(&set, p);
6825 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07006826#endif
bellard66fb9762003-03-23 01:06:05 +00006827 ret = get_errno(sigsuspend(&set));
6828 }
6829 break;
thse5febef2007-04-01 18:31:35 +00006830#endif
bellard66fb9762003-03-23 01:06:05 +00006831 case TARGET_NR_rt_sigsuspend:
6832 {
6833 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05006834 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006835 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006836 target_to_host_sigset(&set, p);
6837 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006838 ret = get_errno(sigsuspend(&set));
6839 }
6840 break;
6841 case TARGET_NR_rt_sigtimedwait:
6842 {
bellard66fb9762003-03-23 01:06:05 +00006843 sigset_t set;
6844 struct timespec uts, *puts;
6845 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00006846
Anthony Liguoric227f092009-10-01 16:12:16 -05006847 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006848 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006849 target_to_host_sigset(&set, p);
6850 unlock_user(p, arg1, 0);
6851 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00006852 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00006853 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00006854 } else {
6855 puts = NULL;
6856 }
6857 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
Petar Jovanovic974a1962014-03-03 15:07:41 +01006858 if (!is_error(ret)) {
6859 if (arg2) {
6860 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
6861 0);
6862 if (!p) {
6863 goto efault;
6864 }
6865 host_to_target_siginfo(p, &uinfo);
6866 unlock_user(p, arg2, sizeof(target_siginfo_t));
6867 }
6868 ret = host_to_target_signal(ret);
bellard66fb9762003-03-23 01:06:05 +00006869 }
6870 }
6871 break;
6872 case TARGET_NR_rt_sigqueueinfo:
6873 {
6874 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05006875 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006876 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006877 target_to_host_siginfo(&uinfo, p);
6878 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006879 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6880 }
6881 break;
thse5febef2007-04-01 18:31:35 +00006882#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00006883 case TARGET_NR_sigreturn:
6884 /* NOTE: ret is eax, so not transcoding must be done */
6885 ret = do_sigreturn(cpu_env);
6886 break;
thse5febef2007-04-01 18:31:35 +00006887#endif
bellard66fb9762003-03-23 01:06:05 +00006888 case TARGET_NR_rt_sigreturn:
6889 /* NOTE: ret is eax, so not transcoding must be done */
6890 ret = do_rt_sigreturn(cpu_env);
6891 break;
bellard31e31b82003-02-18 22:55:36 +00006892 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00006893 if (!(p = lock_user_string(arg1)))
6894 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006895 ret = get_errno(sethostname(p, arg2));
6896 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006897 break;
6898 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00006899 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006900 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006901 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006902 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00006903 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6904 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006905 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6906 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006907 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00006908 ret = get_errno(setrlimit(resource, &rlim));
6909 }
6910 break;
bellard31e31b82003-02-18 22:55:36 +00006911 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00006912 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006913 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006914 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006915 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00006916
bellard9de5e442003-03-23 16:49:39 +00006917 ret = get_errno(getrlimit(resource, &rlim));
6918 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006919 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6920 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006921 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6922 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006923 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00006924 }
6925 }
6926 break;
bellard31e31b82003-02-18 22:55:36 +00006927 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00006928 {
6929 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00006930 ret = get_errno(getrusage(arg1, &rusage));
6931 if (!is_error(ret)) {
Petar Jovanovica39fb272014-04-08 19:24:30 +02006932 ret = host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00006933 }
6934 }
6935 break;
bellard31e31b82003-02-18 22:55:36 +00006936 case TARGET_NR_gettimeofday:
6937 {
bellard31e31b82003-02-18 22:55:36 +00006938 struct timeval tv;
6939 ret = get_errno(gettimeofday(&tv, NULL));
6940 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00006941 if (copy_to_user_timeval(arg1, &tv))
6942 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006943 }
6944 }
6945 break;
6946 case TARGET_NR_settimeofday:
6947 {
Paul Burtonb67d8032014-06-22 11:25:41 +01006948 struct timeval tv, *ptv = NULL;
Paul Burtonef4467e2014-06-22 11:25:40 +01006949 struct timezone tz, *ptz = NULL;
6950
Paul Burtonb67d8032014-06-22 11:25:41 +01006951 if (arg1) {
6952 if (copy_from_user_timeval(&tv, arg1)) {
6953 goto efault;
6954 }
6955 ptv = &tv;
6956 }
Paul Burtonef4467e2014-06-22 11:25:40 +01006957
6958 if (arg2) {
6959 if (copy_from_user_timezone(&tz, arg2)) {
6960 goto efault;
6961 }
6962 ptz = &tz;
6963 }
6964
Paul Burtonb67d8032014-06-22 11:25:41 +01006965 ret = get_errno(settimeofday(ptv, ptz));
bellard31e31b82003-02-18 22:55:36 +00006966 }
6967 break;
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006968#if defined(TARGET_NR_select)
bellard31e31b82003-02-18 22:55:36 +00006969 case TARGET_NR_select:
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006970#if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6971 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6972#else
bellardf2674e32003-07-09 12:26:09 +00006973 {
pbrook53a59602006-03-25 19:31:22 +00006974 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00006975 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00006976 long nsel;
6977
bellard579a97f2007-11-11 14:26:47 +00006978 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6979 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006980 nsel = tswapal(sel->n);
6981 inp = tswapal(sel->inp);
6982 outp = tswapal(sel->outp);
6983 exp = tswapal(sel->exp);
6984 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00006985 unlock_user_struct(sel, arg1, 0);
6986 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00006987 }
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006988#endif
bellardf2674e32003-07-09 12:26:09 +00006989 break;
bellard048f6b42005-11-26 18:47:20 +00006990#endif
Riku Voipio9e423822010-05-07 12:28:05 +00006991#ifdef TARGET_NR_pselect6
6992 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04006993 {
6994 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6995 fd_set rfds, wfds, efds;
6996 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6997 struct timespec ts, *ts_ptr;
6998
6999 /*
7000 * The 6th arg is actually two args smashed together,
7001 * so we cannot use the C library.
7002 */
7003 sigset_t set;
7004 struct {
7005 sigset_t *set;
7006 size_t size;
7007 } sig, *sig_ptr;
7008
7009 abi_ulong arg_sigset, arg_sigsize, *arg7;
7010 target_sigset_t *target_sigset;
7011
7012 n = arg1;
7013 rfd_addr = arg2;
7014 wfd_addr = arg3;
7015 efd_addr = arg4;
7016 ts_addr = arg5;
7017
7018 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
7019 if (ret) {
7020 goto fail;
7021 }
7022 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
7023 if (ret) {
7024 goto fail;
7025 }
7026 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
7027 if (ret) {
7028 goto fail;
7029 }
7030
7031 /*
7032 * This takes a timespec, and not a timeval, so we cannot
7033 * use the do_select() helper ...
7034 */
7035 if (ts_addr) {
7036 if (target_to_host_timespec(&ts, ts_addr)) {
7037 goto efault;
7038 }
7039 ts_ptr = &ts;
7040 } else {
7041 ts_ptr = NULL;
7042 }
7043
7044 /* Extract the two packed args for the sigset */
7045 if (arg6) {
7046 sig_ptr = &sig;
7047 sig.size = _NSIG / 8;
7048
7049 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
7050 if (!arg7) {
7051 goto efault;
7052 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007053 arg_sigset = tswapal(arg7[0]);
7054 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04007055 unlock_user(arg7, arg6, 0);
7056
7057 if (arg_sigset) {
7058 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01007059 if (arg_sigsize != sizeof(*target_sigset)) {
7060 /* Like the kernel, we enforce correct size sigsets */
7061 ret = -TARGET_EINVAL;
7062 goto fail;
7063 }
Mike Frysinger055e0902011-06-03 17:01:49 -04007064 target_sigset = lock_user(VERIFY_READ, arg_sigset,
7065 sizeof(*target_sigset), 1);
7066 if (!target_sigset) {
7067 goto efault;
7068 }
7069 target_to_host_sigset(&set, target_sigset);
7070 unlock_user(target_sigset, arg_sigset, 0);
7071 } else {
7072 sig.set = NULL;
7073 }
7074 } else {
7075 sig_ptr = NULL;
7076 }
7077
7078 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
7079 ts_ptr, sig_ptr));
7080
7081 if (!is_error(ret)) {
7082 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
7083 goto efault;
7084 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
7085 goto efault;
7086 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
7087 goto efault;
7088
7089 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
7090 goto efault;
7091 }
7092 }
7093 break;
Riku Voipio9e423822010-05-07 12:28:05 +00007094#endif
Chen Gang704eff62015-08-21 05:37:33 +08007095#ifdef TARGET_NR_symlink
bellard31e31b82003-02-18 22:55:36 +00007096 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00007097 {
7098 void *p2;
7099 p = lock_user_string(arg1);
7100 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00007101 if (!p || !p2)
7102 ret = -TARGET_EFAULT;
7103 else
7104 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00007105 unlock_user(p2, arg2, 0);
7106 unlock_user(p, arg1, 0);
7107 }
bellard31e31b82003-02-18 22:55:36 +00007108 break;
Chen Gang704eff62015-08-21 05:37:33 +08007109#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007110#if defined(TARGET_NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +00007111 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00007112 {
bellard579a97f2007-11-11 14:26:47 +00007113 void *p2;
thsf0b62432007-09-24 09:25:40 +00007114 p = lock_user_string(arg1);
7115 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00007116 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00007117 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00007118 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01007119 ret = get_errno(symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00007120 unlock_user(p2, arg3, 0);
7121 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00007122 }
7123 break;
7124#endif
bellardebc05482003-09-30 21:08:41 +00007125#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00007126 case TARGET_NR_oldlstat:
7127 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007128#endif
Chen Gang704eff62015-08-21 05:37:33 +08007129#ifdef TARGET_NR_readlink
bellard31e31b82003-02-18 22:55:36 +00007130 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00007131 {
Andreas Schwab463d8e72013-07-02 14:04:12 +01007132 void *p2;
pbrook53a59602006-03-25 19:31:22 +00007133 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00007134 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01007135 if (!p || !p2) {
bellard579a97f2007-11-11 14:26:47 +00007136 ret = -TARGET_EFAULT;
Mike Frysingerf17f4982014-08-08 09:40:25 +09007137 } else if (!arg3) {
7138 /* Short circuit this for the magic exe check. */
7139 ret = -TARGET_EINVAL;
Andreas Schwab463d8e72013-07-02 14:04:12 +01007140 } else if (is_proc_myself((const char *)p, "exe")) {
7141 char real[PATH_MAX], *temp;
7142 temp = realpath(exec_path, real);
Mike Frysingerf17f4982014-08-08 09:40:25 +09007143 /* Return value is # of bytes that we wrote to the buffer. */
7144 if (temp == NULL) {
7145 ret = get_errno(-1);
7146 } else {
7147 /* Don't worry about sign mismatch as earlier mapping
7148 * logic would have thrown a bad address error. */
7149 ret = MIN(strlen(real), arg3);
7150 /* We cannot NUL terminate the string. */
7151 memcpy(p2, real, ret);
7152 }
Andreas Schwab463d8e72013-07-02 14:04:12 +01007153 } else {
7154 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00007155 }
pbrook53a59602006-03-25 19:31:22 +00007156 unlock_user(p2, arg2, ret);
7157 unlock_user(p, arg1, 0);
7158 }
bellard31e31b82003-02-18 22:55:36 +00007159 break;
Chen Gang704eff62015-08-21 05:37:33 +08007160#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007161#if defined(TARGET_NR_readlinkat)
ths5e0ccb12007-09-24 09:26:10 +00007162 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00007163 {
bellard579a97f2007-11-11 14:26:47 +00007164 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00007165 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00007166 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01007167 if (!p || !p2) {
7168 ret = -TARGET_EFAULT;
7169 } else if (is_proc_myself((const char *)p, "exe")) {
7170 char real[PATH_MAX], *temp;
7171 temp = realpath(exec_path, real);
7172 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
7173 snprintf((char *)p2, arg4, "%s", real);
7174 } else {
Peter Maydellc0d472b2013-06-12 16:20:21 +01007175 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
Andreas Schwab463d8e72013-07-02 14:04:12 +01007176 }
bellard579a97f2007-11-11 14:26:47 +00007177 unlock_user(p2, arg3, ret);
7178 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00007179 }
7180 break;
7181#endif
thse5febef2007-04-01 18:31:35 +00007182#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00007183 case TARGET_NR_uselib:
7184 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007185#endif
7186#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00007187 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00007188 if (!(p = lock_user_string(arg1)))
7189 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007190 ret = get_errno(swapon(p, arg2));
7191 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007192 break;
thse5febef2007-04-01 18:31:35 +00007193#endif
bellard31e31b82003-02-18 22:55:36 +00007194 case TARGET_NR_reboot:
Laurent Vivierc07ecc62013-01-07 11:40:06 +00007195 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
7196 /* arg4 must be ignored in all other cases */
7197 p = lock_user_string(arg4);
7198 if (!p) {
7199 goto efault;
7200 }
7201 ret = get_errno(reboot(arg1, arg2, arg3, p));
7202 unlock_user(p, arg4, 0);
7203 } else {
7204 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
7205 }
Alexander Graf0f6b4d22011-09-27 14:39:42 +02007206 break;
thse5febef2007-04-01 18:31:35 +00007207#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00007208 case TARGET_NR_readdir:
7209 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007210#endif
7211#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00007212 case TARGET_NR_mmap:
Alexander Graf09701192013-09-03 20:12:15 +01007213#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7214 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007215 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
7216 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00007217 {
blueswir1992f48a2007-10-14 16:27:31 +00007218 abi_ulong *v;
7219 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00007220 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
7221 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007222 v1 = tswapal(v[0]);
7223 v2 = tswapal(v[1]);
7224 v3 = tswapal(v[2]);
7225 v4 = tswapal(v[3]);
7226 v5 = tswapal(v[4]);
7227 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00007228 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00007229 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00007230 target_to_host_bitmask(v4, mmap_flags_tbl),
7231 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00007232 }
bellard31e31b82003-02-18 22:55:36 +00007233#else
ths5fafdf22007-09-16 21:08:06 +00007234 ret = get_errno(target_mmap(arg1, arg2, arg3,
7235 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00007236 arg5,
7237 arg6));
bellard31e31b82003-02-18 22:55:36 +00007238#endif
bellard6fb883e2003-07-09 17:12:39 +00007239 break;
thse5febef2007-04-01 18:31:35 +00007240#endif
bellarda315a142005-01-30 22:59:18 +00007241#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00007242 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00007243#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00007244#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00007245#endif
ths5fafdf22007-09-16 21:08:06 +00007246 ret = get_errno(target_mmap(arg1, arg2, arg3,
7247 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00007248 arg5,
bellardc573ff62004-01-04 15:51:36 +00007249 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00007250 break;
bellarda315a142005-01-30 22:59:18 +00007251#endif
bellard31e31b82003-02-18 22:55:36 +00007252 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00007253 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00007254 break;
bellard9de5e442003-03-23 16:49:39 +00007255 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01007256 {
Andreas Färber0429a972013-08-26 18:14:44 +02007257 TaskState *ts = cpu->opaque;
Paul Brook97374d32010-06-16 13:03:51 +01007258 /* Special hack to detect libc making the stack executable. */
7259 if ((arg3 & PROT_GROWSDOWN)
7260 && arg1 >= ts->info->stack_limit
7261 && arg1 <= ts->info->start_stack) {
7262 arg3 &= ~PROT_GROWSDOWN;
7263 arg2 = arg2 + arg1 - ts->info->stack_limit;
7264 arg1 = ts->info->stack_limit;
7265 }
7266 }
bellard54936002003-05-13 00:25:15 +00007267 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00007268 break;
thse5febef2007-04-01 18:31:35 +00007269#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00007270 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00007271 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00007272 break;
thse5febef2007-04-01 18:31:35 +00007273#endif
pbrook53a59602006-03-25 19:31:22 +00007274 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00007275#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00007276 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00007277 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00007278 break;
thse5febef2007-04-01 18:31:35 +00007279#endif
7280#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00007281 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00007282 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00007283 break;
thse5febef2007-04-01 18:31:35 +00007284#endif
7285#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00007286 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00007287 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00007288 break;
thse5febef2007-04-01 18:31:35 +00007289#endif
7290#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00007291 case TARGET_NR_mlockall:
Tom Musta6f6a4032014-08-12 13:53:42 -05007292 ret = get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
bellard9de5e442003-03-23 16:49:39 +00007293 break;
thse5febef2007-04-01 18:31:35 +00007294#endif
7295#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00007296 case TARGET_NR_munlockall:
7297 ret = get_errno(munlockall());
7298 break;
thse5febef2007-04-01 18:31:35 +00007299#endif
bellard31e31b82003-02-18 22:55:36 +00007300 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00007301 if (!(p = lock_user_string(arg1)))
7302 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007303 ret = get_errno(truncate(p, arg2));
7304 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007305 break;
7306 case TARGET_NR_ftruncate:
7307 ret = get_errno(ftruncate(arg1, arg2));
7308 break;
7309 case TARGET_NR_fchmod:
7310 ret = get_errno(fchmod(arg1, arg2));
7311 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007312#if defined(TARGET_NR_fchmodat)
ths814d7972007-09-24 09:26:51 +00007313 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00007314 if (!(p = lock_user_string(arg2)))
7315 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007316 ret = get_errno(fchmodat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00007317 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00007318 break;
7319#endif
bellard31e31b82003-02-18 22:55:36 +00007320 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -07007321 /* Note that negative values are valid for getpriority, so we must
7322 differentiate based on errno settings. */
7323 errno = 0;
7324 ret = getpriority(arg1, arg2);
7325 if (ret == -1 && errno != 0) {
7326 ret = -host_to_target_errno(errno);
7327 break;
7328 }
7329#ifdef TARGET_ALPHA
7330 /* Return value is the unbiased priority. Signal no error. */
7331 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
7332#else
7333 /* Return value is a biased priority to avoid negative numbers. */
7334 ret = 20 - ret;
7335#endif
bellard31e31b82003-02-18 22:55:36 +00007336 break;
7337 case TARGET_NR_setpriority:
7338 ret = get_errno(setpriority(arg1, arg2, arg3));
7339 break;
bellardebc05482003-09-30 21:08:41 +00007340#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00007341 case TARGET_NR_profil:
7342 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007343#endif
bellard31e31b82003-02-18 22:55:36 +00007344 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00007345 if (!(p = lock_user_string(arg1)))
7346 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007347 ret = get_errno(statfs(path(p), &stfs));
7348 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007349 convert_statfs:
7350 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007351 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00007352
bellard579a97f2007-11-11 14:26:47 +00007353 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
7354 goto efault;
7355 __put_user(stfs.f_type, &target_stfs->f_type);
7356 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
7357 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
7358 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
7359 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
7360 __put_user(stfs.f_files, &target_stfs->f_files);
7361 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
7362 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
7363 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
7364 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02007365 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
7366 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +00007367 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00007368 }
7369 break;
7370 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00007371 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00007372 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00007373#ifdef TARGET_NR_statfs64
7374 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00007375 if (!(p = lock_user_string(arg1)))
7376 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007377 ret = get_errno(statfs(path(p), &stfs));
7378 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00007379 convert_statfs64:
7380 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007381 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00007382
bellard579a97f2007-11-11 14:26:47 +00007383 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
7384 goto efault;
7385 __put_user(stfs.f_type, &target_stfs->f_type);
7386 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
7387 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
7388 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
7389 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
7390 __put_user(stfs.f_files, &target_stfs->f_files);
7391 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
7392 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
7393 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
7394 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02007395 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
7396 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +00007397 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00007398 }
7399 break;
7400 case TARGET_NR_fstatfs64:
7401 ret = get_errno(fstatfs(arg1, &stfs));
7402 goto convert_statfs64;
7403#endif
bellardebc05482003-09-30 21:08:41 +00007404#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00007405 case TARGET_NR_ioperm:
7406 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007407#endif
thse5febef2007-04-01 18:31:35 +00007408#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00007409 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00007410 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00007411 break;
thse5febef2007-04-01 18:31:35 +00007412#endif
bellard3532fa72006-06-24 15:06:03 +00007413#ifdef TARGET_NR_accept
7414 case TARGET_NR_accept:
Peter Maydella94b4982013-02-08 04:35:04 +00007415 ret = do_accept4(arg1, arg2, arg3, 0);
7416 break;
7417#endif
7418#ifdef TARGET_NR_accept4
7419 case TARGET_NR_accept4:
7420#ifdef CONFIG_ACCEPT4
7421 ret = do_accept4(arg1, arg2, arg3, arg4);
7422#else
7423 goto unimplemented;
7424#endif
bellard3532fa72006-06-24 15:06:03 +00007425 break;
7426#endif
7427#ifdef TARGET_NR_bind
7428 case TARGET_NR_bind:
7429 ret = do_bind(arg1, arg2, arg3);
7430 break;
7431#endif
7432#ifdef TARGET_NR_connect
7433 case TARGET_NR_connect:
7434 ret = do_connect(arg1, arg2, arg3);
7435 break;
7436#endif
7437#ifdef TARGET_NR_getpeername
7438 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00007439 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00007440 break;
7441#endif
7442#ifdef TARGET_NR_getsockname
7443 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00007444 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00007445 break;
7446#endif
7447#ifdef TARGET_NR_getsockopt
7448 case TARGET_NR_getsockopt:
7449 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
7450 break;
7451#endif
7452#ifdef TARGET_NR_listen
7453 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00007454 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00007455 break;
7456#endif
7457#ifdef TARGET_NR_recv
7458 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00007459 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00007460 break;
7461#endif
7462#ifdef TARGET_NR_recvfrom
7463 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00007464 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00007465 break;
7466#endif
7467#ifdef TARGET_NR_recvmsg
7468 case TARGET_NR_recvmsg:
7469 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
7470 break;
7471#endif
7472#ifdef TARGET_NR_send
7473 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00007474 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00007475 break;
7476#endif
7477#ifdef TARGET_NR_sendmsg
7478 case TARGET_NR_sendmsg:
7479 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
7480 break;
7481#endif
Alexander Graff19e00d2014-03-02 19:36:42 +00007482#ifdef TARGET_NR_sendmmsg
7483 case TARGET_NR_sendmmsg:
7484 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
7485 break;
7486 case TARGET_NR_recvmmsg:
7487 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
7488 break;
7489#endif
bellard3532fa72006-06-24 15:06:03 +00007490#ifdef TARGET_NR_sendto
7491 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00007492 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00007493 break;
7494#endif
7495#ifdef TARGET_NR_shutdown
7496 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00007497 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00007498 break;
7499#endif
7500#ifdef TARGET_NR_socket
7501 case TARGET_NR_socket:
7502 ret = do_socket(arg1, arg2, arg3);
Laurent Viviere36800c2015-10-02 14:48:09 +02007503 fd_trans_unregister(ret);
bellard3532fa72006-06-24 15:06:03 +00007504 break;
7505#endif
7506#ifdef TARGET_NR_socketpair
7507 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00007508 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00007509 break;
7510#endif
7511#ifdef TARGET_NR_setsockopt
7512 case TARGET_NR_setsockopt:
7513 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
7514 break;
7515#endif
ths7494b0f2007-02-11 18:26:53 +00007516
bellard31e31b82003-02-18 22:55:36 +00007517 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00007518 if (!(p = lock_user_string(arg2)))
7519 goto efault;
thse5574482007-02-11 20:03:13 +00007520 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
7521 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00007522 break;
7523
bellard31e31b82003-02-18 22:55:36 +00007524 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00007525 {
bellard66fb9762003-03-23 01:06:05 +00007526 struct itimerval value, ovalue, *pvalue;
7527
pbrook53a59602006-03-25 19:31:22 +00007528 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00007529 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00007530 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
7531 || copy_from_user_timeval(&pvalue->it_value,
7532 arg2 + sizeof(struct target_timeval)))
7533 goto efault;
bellard66fb9762003-03-23 01:06:05 +00007534 } else {
7535 pvalue = NULL;
7536 }
7537 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00007538 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00007539 if (copy_to_user_timeval(arg3,
7540 &ovalue.it_interval)
7541 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
7542 &ovalue.it_value))
7543 goto efault;
bellard66fb9762003-03-23 01:06:05 +00007544 }
7545 }
7546 break;
bellard31e31b82003-02-18 22:55:36 +00007547 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00007548 {
bellard66fb9762003-03-23 01:06:05 +00007549 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00007550
bellard66fb9762003-03-23 01:06:05 +00007551 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00007552 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00007553 if (copy_to_user_timeval(arg2,
7554 &value.it_interval)
7555 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
7556 &value.it_value))
7557 goto efault;
bellard66fb9762003-03-23 01:06:05 +00007558 }
7559 }
7560 break;
Chen Gang704eff62015-08-21 05:37:33 +08007561#ifdef TARGET_NR_stat
bellard31e31b82003-02-18 22:55:36 +00007562 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00007563 if (!(p = lock_user_string(arg1)))
7564 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007565 ret = get_errno(stat(path(p), &st));
7566 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007567 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +08007568#endif
7569#ifdef TARGET_NR_lstat
bellard31e31b82003-02-18 22:55:36 +00007570 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00007571 if (!(p = lock_user_string(arg1)))
7572 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007573 ret = get_errno(lstat(path(p), &st));
7574 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007575 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +08007576#endif
bellard31e31b82003-02-18 22:55:36 +00007577 case TARGET_NR_fstat:
7578 {
7579 ret = get_errno(fstat(arg1, &st));
Chen Gang704eff62015-08-21 05:37:33 +08007580#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
bellard31e31b82003-02-18 22:55:36 +00007581 do_stat:
Chen Gang704eff62015-08-21 05:37:33 +08007582#endif
bellard31e31b82003-02-18 22:55:36 +00007583 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007584 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00007585
bellard579a97f2007-11-11 14:26:47 +00007586 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
7587 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02007588 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00007589 __put_user(st.st_dev, &target_st->st_dev);
7590 __put_user(st.st_ino, &target_st->st_ino);
7591 __put_user(st.st_mode, &target_st->st_mode);
7592 __put_user(st.st_uid, &target_st->st_uid);
7593 __put_user(st.st_gid, &target_st->st_gid);
7594 __put_user(st.st_nlink, &target_st->st_nlink);
7595 __put_user(st.st_rdev, &target_st->st_rdev);
7596 __put_user(st.st_size, &target_st->st_size);
7597 __put_user(st.st_blksize, &target_st->st_blksize);
7598 __put_user(st.st_blocks, &target_st->st_blocks);
7599 __put_user(st.st_atime, &target_st->target_st_atime);
7600 __put_user(st.st_mtime, &target_st->target_st_mtime);
7601 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00007602 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00007603 }
7604 }
7605 break;
bellardebc05482003-09-30 21:08:41 +00007606#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00007607 case TARGET_NR_olduname:
7608 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007609#endif
7610#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00007611 case TARGET_NR_iopl:
7612 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007613#endif
bellard31e31b82003-02-18 22:55:36 +00007614 case TARGET_NR_vhangup:
7615 ret = get_errno(vhangup());
7616 break;
bellardebc05482003-09-30 21:08:41 +00007617#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00007618 case TARGET_NR_idle:
7619 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007620#endif
bellard42ad6ae2005-01-03 22:48:11 +00007621#ifdef TARGET_NR_syscall
7622 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01007623 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
7624 arg6, arg7, arg8, 0);
7625 break;
bellard42ad6ae2005-01-03 22:48:11 +00007626#endif
bellard31e31b82003-02-18 22:55:36 +00007627 case TARGET_NR_wait4:
7628 {
7629 int status;
blueswir1992f48a2007-10-14 16:27:31 +00007630 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00007631 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00007632 abi_ulong target_rusage = arg4;
Petar Jovanovica39fb272014-04-08 19:24:30 +02007633 abi_long rusage_err;
bellard31e31b82003-02-18 22:55:36 +00007634 if (target_rusage)
7635 rusage_ptr = &rusage;
7636 else
7637 rusage_ptr = NULL;
7638 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
7639 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01007640 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00007641 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00007642 if (put_user_s32(status, status_ptr))
7643 goto efault;
bellard31e31b82003-02-18 22:55:36 +00007644 }
Petar Jovanovica39fb272014-04-08 19:24:30 +02007645 if (target_rusage) {
7646 rusage_err = host_to_target_rusage(target_rusage, &rusage);
7647 if (rusage_err) {
7648 ret = rusage_err;
7649 }
7650 }
bellard31e31b82003-02-18 22:55:36 +00007651 }
7652 }
7653 break;
thse5febef2007-04-01 18:31:35 +00007654#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00007655 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00007656 if (!(p = lock_user_string(arg1)))
7657 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007658 ret = get_errno(swapoff(p));
7659 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007660 break;
thse5febef2007-04-01 18:31:35 +00007661#endif
bellard31e31b82003-02-18 22:55:36 +00007662 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00007663 {
pbrook53a59602006-03-25 19:31:22 +00007664 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00007665 struct sysinfo value;
7666 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00007667 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00007668 {
bellard579a97f2007-11-11 14:26:47 +00007669 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
7670 goto efault;
bellarda5448a72004-06-19 16:59:03 +00007671 __put_user(value.uptime, &target_value->uptime);
7672 __put_user(value.loads[0], &target_value->loads[0]);
7673 __put_user(value.loads[1], &target_value->loads[1]);
7674 __put_user(value.loads[2], &target_value->loads[2]);
7675 __put_user(value.totalram, &target_value->totalram);
7676 __put_user(value.freeram, &target_value->freeram);
7677 __put_user(value.sharedram, &target_value->sharedram);
7678 __put_user(value.bufferram, &target_value->bufferram);
7679 __put_user(value.totalswap, &target_value->totalswap);
7680 __put_user(value.freeswap, &target_value->freeswap);
7681 __put_user(value.procs, &target_value->procs);
7682 __put_user(value.totalhigh, &target_value->totalhigh);
7683 __put_user(value.freehigh, &target_value->freehigh);
7684 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00007685 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00007686 }
7687 }
7688 break;
thse5febef2007-04-01 18:31:35 +00007689#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00007690 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00007691 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
7692 break;
thse5febef2007-04-01 18:31:35 +00007693#endif
aurel32e5289082009-04-18 16:16:12 +00007694#ifdef TARGET_NR_semget
7695 case TARGET_NR_semget:
7696 ret = get_errno(semget(arg1, arg2, arg3));
7697 break;
7698#endif
7699#ifdef TARGET_NR_semop
7700 case TARGET_NR_semop:
Petar Jovanovicc7128c92013-03-21 07:57:36 +00007701 ret = do_semop(arg1, arg2, arg3);
aurel32e5289082009-04-18 16:16:12 +00007702 break;
7703#endif
7704#ifdef TARGET_NR_semctl
7705 case TARGET_NR_semctl:
Stefan Weild1c002b2015-02-08 15:40:58 +01007706 ret = do_semctl(arg1, arg2, arg3, arg4);
aurel32e5289082009-04-18 16:16:12 +00007707 break;
7708#endif
aurel32eeb438c2008-10-13 21:08:55 +00007709#ifdef TARGET_NR_msgctl
7710 case TARGET_NR_msgctl:
7711 ret = do_msgctl(arg1, arg2, arg3);
7712 break;
7713#endif
7714#ifdef TARGET_NR_msgget
7715 case TARGET_NR_msgget:
7716 ret = get_errno(msgget(arg1, arg2));
7717 break;
7718#endif
7719#ifdef TARGET_NR_msgrcv
7720 case TARGET_NR_msgrcv:
7721 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
7722 break;
7723#endif
7724#ifdef TARGET_NR_msgsnd
7725 case TARGET_NR_msgsnd:
7726 ret = do_msgsnd(arg1, arg2, arg3, arg4);
7727 break;
7728#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03007729#ifdef TARGET_NR_shmget
7730 case TARGET_NR_shmget:
7731 ret = get_errno(shmget(arg1, arg2, arg3));
7732 break;
7733#endif
7734#ifdef TARGET_NR_shmctl
7735 case TARGET_NR_shmctl:
7736 ret = do_shmctl(arg1, arg2, arg3);
7737 break;
7738#endif
7739#ifdef TARGET_NR_shmat
7740 case TARGET_NR_shmat:
7741 ret = do_shmat(arg1, arg2, arg3);
7742 break;
7743#endif
7744#ifdef TARGET_NR_shmdt
7745 case TARGET_NR_shmdt:
7746 ret = do_shmdt(arg1);
7747 break;
7748#endif
bellard31e31b82003-02-18 22:55:36 +00007749 case TARGET_NR_fsync:
7750 ret = get_errno(fsync(arg1));
7751 break;
bellard31e31b82003-02-18 22:55:36 +00007752 case TARGET_NR_clone:
Peter Maydell4ce62432013-07-16 18:44:57 +01007753 /* Linux manages to have three different orderings for its
7754 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7755 * match the kernel's CONFIG_CLONE_* settings.
7756 * Microblaze is further special in that it uses a sixth
7757 * implicit argument to clone for the TLS pointer.
7758 */
7759#if defined(TARGET_MICROBLAZE)
Edgar E. Iglesiasa5b3bdc2012-04-26 14:17:41 +02007760 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
Peter Maydell4ce62432013-07-16 18:44:57 +01007761#elif defined(TARGET_CLONE_BACKWARDS)
7762 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
7763#elif defined(TARGET_CLONE_BACKWARDS2)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007764 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00007765#else
Peter Maydell4ce62432013-07-16 18:44:57 +01007766 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00007767#endif
bellard1b6b0292003-03-22 17:31:38 +00007768 break;
bellardec86b0f2003-04-11 00:15:04 +00007769#ifdef __NR_exit_group
7770 /* new thread calls */
7771 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02007772#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00007773 _mcleanup();
7774#endif
bellarde9009672005-04-26 20:42:36 +00007775 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00007776 ret = get_errno(exit_group(arg1));
7777 break;
7778#endif
bellard31e31b82003-02-18 22:55:36 +00007779 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00007780 if (!(p = lock_user_string(arg1)))
7781 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007782 ret = get_errno(setdomainname(p, arg2));
7783 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007784 break;
7785 case TARGET_NR_uname:
7786 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00007787 {
7788 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00007789
bellard579a97f2007-11-11 14:26:47 +00007790 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
7791 goto efault;
bellard29e619b2004-09-13 21:41:04 +00007792 ret = get_errno(sys_uname(buf));
7793 if (!is_error(ret)) {
7794 /* Overrite the native machine name with whatever is being
7795 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01007796 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00007797 /* Allow the user to override the reported release. */
7798 if (qemu_uname_release && *qemu_uname_release)
7799 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00007800 }
pbrook53a59602006-03-25 19:31:22 +00007801 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00007802 }
bellard31e31b82003-02-18 22:55:36 +00007803 break;
bellard6dbad632003-03-16 18:05:05 +00007804#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00007805 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00007806 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00007807 break;
j_mayer84409dd2007-04-06 08:56:50 +00007808#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00007809 case TARGET_NR_vm86old:
7810 goto unimplemented;
7811 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00007812 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00007813 break;
7814#endif
j_mayer84409dd2007-04-06 08:56:50 +00007815#endif
bellard31e31b82003-02-18 22:55:36 +00007816 case TARGET_NR_adjtimex:
7817 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007818#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00007819 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00007820#endif
bellard31e31b82003-02-18 22:55:36 +00007821 case TARGET_NR_init_module:
7822 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00007823#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00007824 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00007825#endif
bellard31e31b82003-02-18 22:55:36 +00007826 goto unimplemented;
7827 case TARGET_NR_quotactl:
7828 goto unimplemented;
7829 case TARGET_NR_getpgid:
7830 ret = get_errno(getpgid(arg1));
7831 break;
7832 case TARGET_NR_fchdir:
7833 ret = get_errno(fchdir(arg1));
7834 break;
j_mayer84409dd2007-04-06 08:56:50 +00007835#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00007836 case TARGET_NR_bdflush:
7837 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00007838#endif
thse5febef2007-04-01 18:31:35 +00007839#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00007840 case TARGET_NR_sysfs:
7841 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007842#endif
bellard31e31b82003-02-18 22:55:36 +00007843 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00007844 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00007845 break;
thse5febef2007-04-01 18:31:35 +00007846#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00007847 case TARGET_NR_afs_syscall:
7848 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007849#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007850#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007851 case TARGET_NR__llseek:
7852 {
7853 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00007854#if !defined(__NR_llseek)
7855 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7856 if (res == -1) {
7857 ret = get_errno(res);
7858 } else {
7859 ret = 0;
7860 }
7861#else
bellard31e31b82003-02-18 22:55:36 +00007862 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00007863#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00007864 if ((ret == 0) && put_user_s64(res, arg4)) {
7865 goto efault;
7866 }
bellard31e31b82003-02-18 22:55:36 +00007867 }
7868 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007869#endif
Chen Gang704eff62015-08-21 05:37:33 +08007870#ifdef TARGET_NR_getdents
bellard31e31b82003-02-18 22:55:36 +00007871 case TARGET_NR_getdents:
Peter Maydell3307e232013-06-12 16:20:21 +01007872#ifdef __NR_getdents
Ulrich Hechtd83c8732009-07-24 19:10:28 +02007873#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00007874 {
pbrook53a59602006-03-25 19:31:22 +00007875 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00007876 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007877 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00007878
7879 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00007880 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00007881 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00007882 goto fail;
7883 }
ths3b46e622007-09-17 08:09:54 +00007884
bellard4add45b2003-06-05 01:52:59 +00007885 ret = get_errno(sys_getdents(arg1, dirp, count));
7886 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007887 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00007888 struct target_dirent *tde;
7889 int len = ret;
7890 int reclen, treclen;
7891 int count1, tnamelen;
7892
7893 count1 = 0;
7894 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00007895 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7896 goto efault;
bellard4add45b2003-06-05 01:52:59 +00007897 tde = target_dirp;
7898 while (len > 0) {
7899 reclen = de->d_reclen;
Dmitry V. Levin333858b2012-08-21 02:13:12 +04007900 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
7901 assert(tnamelen >= 0);
7902 treclen = tnamelen + offsetof(struct target_dirent, d_name);
7903 assert(count1 + treclen <= count);
bellard4add45b2003-06-05 01:52:59 +00007904 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007905 tde->d_ino = tswapal(de->d_ino);
7906 tde->d_off = tswapal(de->d_off);
Dmitry V. Levin333858b2012-08-21 02:13:12 +04007907 memcpy(tde->d_name, de->d_name, tnamelen);
aurel326556a832008-10-13 21:08:17 +00007908 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00007909 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00007910 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00007911 count1 += treclen;
7912 }
7913 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00007914 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00007915 }
7916 free(dirp);
7917 }
7918#else
bellard31e31b82003-02-18 22:55:36 +00007919 {
aurel326556a832008-10-13 21:08:17 +00007920 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007921 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00007922
bellard579a97f2007-11-11 14:26:47 +00007923 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7924 goto efault;
bellard72f03902003-02-18 23:33:18 +00007925 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00007926 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007927 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00007928 int len = ret;
7929 int reclen;
7930 de = dirp;
7931 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007932 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00007933 if (reclen > len)
7934 break;
bellard8083a3e2003-03-24 23:12:16 +00007935 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00007936 tswapls(&de->d_ino);
7937 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007938 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00007939 len -= reclen;
7940 }
7941 }
pbrook53a59602006-03-25 19:31:22 +00007942 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00007943 }
bellard4add45b2003-06-05 01:52:59 +00007944#endif
Peter Maydell3307e232013-06-12 16:20:21 +01007945#else
7946 /* Implement getdents in terms of getdents64 */
7947 {
7948 struct linux_dirent64 *dirp;
7949 abi_long count = arg3;
7950
7951 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
7952 if (!dirp) {
7953 goto efault;
7954 }
7955 ret = get_errno(sys_getdents64(arg1, dirp, count));
7956 if (!is_error(ret)) {
7957 /* Convert the dirent64 structs to target dirent. We do this
7958 * in-place, since we can guarantee that a target_dirent is no
7959 * larger than a dirent64; however this means we have to be
7960 * careful to read everything before writing in the new format.
7961 */
7962 struct linux_dirent64 *de;
7963 struct target_dirent *tde;
7964 int len = ret;
7965 int tlen = 0;
7966
7967 de = dirp;
7968 tde = (struct target_dirent *)dirp;
7969 while (len > 0) {
7970 int namelen, treclen;
7971 int reclen = de->d_reclen;
7972 uint64_t ino = de->d_ino;
7973 int64_t off = de->d_off;
7974 uint8_t type = de->d_type;
7975
7976 namelen = strlen(de->d_name);
7977 treclen = offsetof(struct target_dirent, d_name)
7978 + namelen + 2;
7979 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
7980
7981 memmove(tde->d_name, de->d_name, namelen + 1);
7982 tde->d_ino = tswapal(ino);
7983 tde->d_off = tswapal(off);
7984 tde->d_reclen = tswap16(treclen);
7985 /* The target_dirent type is in what was formerly a padding
7986 * byte at the end of the structure:
7987 */
7988 *(((char *)tde) + treclen - 1) = type;
7989
7990 de = (struct linux_dirent64 *)((char *)de + reclen);
7991 tde = (struct target_dirent *)((char *)tde + treclen);
7992 len -= reclen;
7993 tlen += treclen;
7994 }
7995 ret = tlen;
7996 }
7997 unlock_user(dirp, arg2, ret);
7998 }
7999#endif
bellard31e31b82003-02-18 22:55:36 +00008000 break;
Chen Gang704eff62015-08-21 05:37:33 +08008001#endif /* TARGET_NR_getdents */
ths3ae43202007-09-16 21:39:48 +00008002#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00008003 case TARGET_NR_getdents64:
8004 {
aurel326556a832008-10-13 21:08:17 +00008005 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00008006 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00008007 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8008 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00008009 ret = get_errno(sys_getdents64(arg1, dirp, count));
8010 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00008011 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00008012 int len = ret;
8013 int reclen;
8014 de = dirp;
8015 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00008016 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00008017 if (reclen > len)
8018 break;
bellard8083a3e2003-03-24 23:12:16 +00008019 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00008020 tswap64s((uint64_t *)&de->d_ino);
8021 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00008022 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00008023 len -= reclen;
8024 }
8025 }
pbrook53a59602006-03-25 19:31:22 +00008026 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00008027 }
8028 break;
bellarda541f292004-04-12 20:39:29 +00008029#endif /* TARGET_NR_getdents64 */
Laurent Vivier9468a5d2013-01-10 22:30:50 +01008030#if defined(TARGET_NR__newselect)
bellard31e31b82003-02-18 22:55:36 +00008031 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00008032 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00008033 break;
thse5febef2007-04-01 18:31:35 +00008034#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05008035#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
8036# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00008037 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05008038# endif
8039# ifdef TARGET_NR_ppoll
8040 case TARGET_NR_ppoll:
8041# endif
bellard9de5e442003-03-23 16:49:39 +00008042 {
pbrook53a59602006-03-25 19:31:22 +00008043 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00008044 unsigned int nfds = arg2;
8045 int timeout = arg3;
8046 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00008047 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00008048
Laurent Vivier3e24bb32015-10-06 01:20:48 +02008049 pfd = NULL;
8050 target_pfd = NULL;
8051 if (nfds) {
8052 target_pfd = lock_user(VERIFY_WRITE, arg1,
8053 sizeof(struct target_pollfd) * nfds, 1);
8054 if (!target_pfd) {
8055 goto efault;
8056 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05008057
Laurent Vivier3e24bb32015-10-06 01:20:48 +02008058 pfd = alloca(sizeof(struct pollfd) * nfds);
8059 for (i = 0; i < nfds; i++) {
8060 pfd[i].fd = tswap32(target_pfd[i].fd);
8061 pfd[i].events = tswap16(target_pfd[i].events);
8062 }
bellard9de5e442003-03-23 16:49:39 +00008063 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05008064
8065# ifdef TARGET_NR_ppoll
8066 if (num == TARGET_NR_ppoll) {
8067 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
8068 target_sigset_t *target_set;
8069 sigset_t _set, *set = &_set;
8070
8071 if (arg3) {
8072 if (target_to_host_timespec(timeout_ts, arg3)) {
8073 unlock_user(target_pfd, arg1, 0);
8074 goto efault;
8075 }
8076 } else {
8077 timeout_ts = NULL;
8078 }
8079
8080 if (arg4) {
8081 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
8082 if (!target_set) {
8083 unlock_user(target_pfd, arg1, 0);
8084 goto efault;
8085 }
8086 target_to_host_sigset(set, target_set);
8087 } else {
8088 set = NULL;
8089 }
8090
8091 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
8092
8093 if (!is_error(ret) && arg3) {
8094 host_to_target_timespec(arg3, timeout_ts);
8095 }
8096 if (arg4) {
8097 unlock_user(target_set, arg4, 0);
8098 }
8099 } else
8100# endif
8101 ret = get_errno(poll(pfd, nfds, timeout));
8102
bellard9de5e442003-03-23 16:49:39 +00008103 if (!is_error(ret)) {
8104 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00008105 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00008106 }
8107 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00008108 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00008109 }
8110 break;
thse5febef2007-04-01 18:31:35 +00008111#endif
bellard31e31b82003-02-18 22:55:36 +00008112 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00008113 /* NOTE: the flock constant seems to be the same for every
8114 Linux platform */
8115 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00008116 break;
8117 case TARGET_NR_readv:
8118 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008119 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
8120 if (vec != NULL) {
8121 ret = get_errno(readv(arg1, vec, arg3));
8122 unlock_iovec(vec, arg2, arg3, 1);
8123 } else {
8124 ret = -host_to_target_errno(errno);
8125 }
bellard31e31b82003-02-18 22:55:36 +00008126 }
8127 break;
8128 case TARGET_NR_writev:
8129 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008130 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8131 if (vec != NULL) {
8132 ret = get_errno(writev(arg1, vec, arg3));
8133 unlock_iovec(vec, arg2, arg3, 0);
8134 } else {
8135 ret = -host_to_target_errno(errno);
8136 }
bellard31e31b82003-02-18 22:55:36 +00008137 }
8138 break;
8139 case TARGET_NR_getsid:
8140 ret = get_errno(getsid(arg1));
8141 break;
j_mayer7a3148a2007-04-05 07:13:51 +00008142#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00008143 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00008144 ret = get_errno(fdatasync(arg1));
8145 break;
j_mayer7a3148a2007-04-05 07:13:51 +00008146#endif
Chen Gang704eff62015-08-21 05:37:33 +08008147#ifdef TARGET_NR__sysctl
bellard31e31b82003-02-18 22:55:36 +00008148 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00008149 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00008150 return value. */
ths0da46a62007-10-20 20:23:07 +00008151 ret = -TARGET_ENOTDIR;
8152 break;
Chen Gang704eff62015-08-21 05:37:33 +08008153#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -05008154 case TARGET_NR_sched_getaffinity:
8155 {
8156 unsigned int mask_size;
8157 unsigned long *mask;
8158
8159 /*
8160 * sched_getaffinity needs multiples of ulong, so need to take
8161 * care of mismatches between target ulong and host ulong sizes.
8162 */
8163 if (arg2 & (sizeof(abi_ulong) - 1)) {
8164 ret = -TARGET_EINVAL;
8165 break;
8166 }
8167 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
8168
8169 mask = alloca(mask_size);
8170 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
8171
8172 if (!is_error(ret)) {
Peter Maydellbe3bd282014-05-15 14:40:23 +01008173 if (ret > arg2) {
8174 /* More data returned than the caller's buffer will fit.
8175 * This only happens if sizeof(abi_long) < sizeof(long)
8176 * and the caller passed us a buffer holding an odd number
8177 * of abi_longs. If the host kernel is actually using the
8178 * extra 4 bytes then fail EINVAL; otherwise we can just
8179 * ignore them and only copy the interesting part.
8180 */
8181 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
8182 if (numcpus > arg2 * 8) {
8183 ret = -TARGET_EINVAL;
8184 break;
8185 }
8186 ret = arg2;
8187 }
8188
Mike McCormackcd18f052011-04-18 14:43:36 +09008189 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05008190 goto efault;
8191 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05008192 }
8193 }
8194 break;
8195 case TARGET_NR_sched_setaffinity:
8196 {
8197 unsigned int mask_size;
8198 unsigned long *mask;
8199
8200 /*
8201 * sched_setaffinity needs multiples of ulong, so need to take
8202 * care of mismatches between target ulong and host ulong sizes.
8203 */
8204 if (arg2 & (sizeof(abi_ulong) - 1)) {
8205 ret = -TARGET_EINVAL;
8206 break;
8207 }
8208 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
8209
8210 mask = alloca(mask_size);
8211 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
8212 goto efault;
8213 }
8214 memcpy(mask, p, arg2);
8215 unlock_user_struct(p, arg2, 0);
8216
8217 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
8218 }
8219 break;
bellard31e31b82003-02-18 22:55:36 +00008220 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00008221 {
pbrook53a59602006-03-25 19:31:22 +00008222 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00008223 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00008224
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05008225 if (arg2 == 0) {
8226 return -TARGET_EINVAL;
8227 }
bellard579a97f2007-11-11 14:26:47 +00008228 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
8229 goto efault;
bellard5cd43932003-03-29 16:54:36 +00008230 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00008231 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00008232 ret = get_errno(sched_setparam(arg1, &schp));
8233 }
8234 break;
bellard31e31b82003-02-18 22:55:36 +00008235 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00008236 {
pbrook53a59602006-03-25 19:31:22 +00008237 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00008238 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05008239
8240 if (arg2 == 0) {
8241 return -TARGET_EINVAL;
8242 }
bellard5cd43932003-03-29 16:54:36 +00008243 ret = get_errno(sched_getparam(arg1, &schp));
8244 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00008245 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
8246 goto efault;
bellard5cd43932003-03-29 16:54:36 +00008247 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00008248 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00008249 }
8250 }
8251 break;
bellard31e31b82003-02-18 22:55:36 +00008252 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00008253 {
pbrook53a59602006-03-25 19:31:22 +00008254 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00008255 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05008256 if (arg3 == 0) {
8257 return -TARGET_EINVAL;
8258 }
bellard579a97f2007-11-11 14:26:47 +00008259 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
8260 goto efault;
bellard5cd43932003-03-29 16:54:36 +00008261 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00008262 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00008263 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
8264 }
8265 break;
bellard31e31b82003-02-18 22:55:36 +00008266 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00008267 ret = get_errno(sched_getscheduler(arg1));
8268 break;
bellard31e31b82003-02-18 22:55:36 +00008269 case TARGET_NR_sched_yield:
8270 ret = get_errno(sched_yield());
8271 break;
8272 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00008273 ret = get_errno(sched_get_priority_max(arg1));
8274 break;
bellard31e31b82003-02-18 22:55:36 +00008275 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00008276 ret = get_errno(sched_get_priority_min(arg1));
8277 break;
bellard31e31b82003-02-18 22:55:36 +00008278 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00008279 {
bellard5cd43932003-03-29 16:54:36 +00008280 struct timespec ts;
8281 ret = get_errno(sched_rr_get_interval(arg1, &ts));
8282 if (!is_error(ret)) {
Tom Mustad4290c42014-08-12 13:53:39 -05008283 ret = host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00008284 }
8285 }
8286 break;
bellard31e31b82003-02-18 22:55:36 +00008287 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00008288 {
bellard1b6b0292003-03-22 17:31:38 +00008289 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00008290 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00008291 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00008292 if (is_error(ret) && arg2) {
8293 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00008294 }
8295 }
8296 break;
thse5febef2007-04-01 18:31:35 +00008297#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00008298 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00008299 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008300#endif
8301#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00008302 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00008303 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008304#endif
bellard31e31b82003-02-18 22:55:36 +00008305 case TARGET_NR_prctl:
Peter Maydell1e6722f2012-02-03 14:48:03 +00008306 switch (arg1) {
8307 case PR_GET_PDEATHSIG:
8308 {
8309 int deathsig;
8310 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
8311 if (!is_error(ret) && arg2
8312 && put_user_ual(deathsig, arg2)) {
8313 goto efault;
thse5574482007-02-11 20:03:13 +00008314 }
Peter Maydell1e6722f2012-02-03 14:48:03 +00008315 break;
8316 }
Peter Maydelldb9526b2012-02-03 14:48:03 +00008317#ifdef PR_GET_NAME
8318 case PR_GET_NAME:
8319 {
8320 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
8321 if (!name) {
8322 goto efault;
8323 }
8324 ret = get_errno(prctl(arg1, (unsigned long)name,
8325 arg3, arg4, arg5));
8326 unlock_user(name, arg2, 16);
8327 break;
8328 }
8329 case PR_SET_NAME:
8330 {
8331 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
8332 if (!name) {
8333 goto efault;
8334 }
8335 ret = get_errno(prctl(arg1, (unsigned long)name,
8336 arg3, arg4, arg5));
8337 unlock_user(name, arg2, 0);
8338 break;
8339 }
8340#endif
Peter Maydell1e6722f2012-02-03 14:48:03 +00008341 default:
8342 /* Most prctl options have no pointer arguments */
8343 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
8344 break;
8345 }
ths39b9aae2007-02-11 18:36:44 +00008346 break;
bellardd2fd1af2007-11-14 18:08:56 +00008347#ifdef TARGET_NR_arch_prctl
8348 case TARGET_NR_arch_prctl:
8349#if defined(TARGET_I386) && !defined(TARGET_ABI32)
8350 ret = do_arch_prctl(cpu_env, arg1, arg2);
8351 break;
8352#else
8353 goto unimplemented;
8354#endif
8355#endif
aurel32f2c7ba12008-03-28 22:32:06 +00008356#ifdef TARGET_NR_pread64
8357 case TARGET_NR_pread64:
Alexander Grafae017a52012-09-29 15:32:39 +00008358 if (regpairs_aligned(cpu_env)) {
8359 arg4 = arg5;
8360 arg5 = arg6;
8361 }
aurel32f2c7ba12008-03-28 22:32:06 +00008362 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
8363 goto efault;
8364 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
8365 unlock_user(p, arg2, ret);
8366 break;
8367 case TARGET_NR_pwrite64:
Alexander Grafae017a52012-09-29 15:32:39 +00008368 if (regpairs_aligned(cpu_env)) {
8369 arg4 = arg5;
8370 arg5 = arg6;
8371 }
aurel32f2c7ba12008-03-28 22:32:06 +00008372 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
8373 goto efault;
8374 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
8375 unlock_user(p, arg2, 0);
8376 break;
8377#endif
bellard31e31b82003-02-18 22:55:36 +00008378 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00008379 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
8380 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008381 ret = get_errno(sys_getcwd1(p, arg2));
8382 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00008383 break;
8384 case TARGET_NR_capget:
8385 case TARGET_NR_capset:
Peter Maydelle0eb2102014-03-17 12:15:35 +00008386 {
8387 struct target_user_cap_header *target_header;
8388 struct target_user_cap_data *target_data = NULL;
8389 struct __user_cap_header_struct header;
8390 struct __user_cap_data_struct data[2];
8391 struct __user_cap_data_struct *dataptr = NULL;
8392 int i, target_datalen;
8393 int data_items = 1;
8394
8395 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
8396 goto efault;
8397 }
8398 header.version = tswap32(target_header->version);
8399 header.pid = tswap32(target_header->pid);
8400
Peter Maydellec864872014-03-19 16:07:30 +00008401 if (header.version != _LINUX_CAPABILITY_VERSION) {
Peter Maydelle0eb2102014-03-17 12:15:35 +00008402 /* Version 2 and up takes pointer to two user_data structs */
8403 data_items = 2;
8404 }
8405
8406 target_datalen = sizeof(*target_data) * data_items;
8407
8408 if (arg2) {
8409 if (num == TARGET_NR_capget) {
8410 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
8411 } else {
8412 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
8413 }
8414 if (!target_data) {
8415 unlock_user_struct(target_header, arg1, 0);
8416 goto efault;
8417 }
8418
8419 if (num == TARGET_NR_capset) {
8420 for (i = 0; i < data_items; i++) {
8421 data[i].effective = tswap32(target_data[i].effective);
8422 data[i].permitted = tswap32(target_data[i].permitted);
8423 data[i].inheritable = tswap32(target_data[i].inheritable);
8424 }
8425 }
8426
8427 dataptr = data;
8428 }
8429
8430 if (num == TARGET_NR_capget) {
8431 ret = get_errno(capget(&header, dataptr));
8432 } else {
8433 ret = get_errno(capset(&header, dataptr));
8434 }
8435
8436 /* The kernel always updates version for both capget and capset */
8437 target_header->version = tswap32(header.version);
8438 unlock_user_struct(target_header, arg1, 1);
8439
8440 if (arg2) {
8441 if (num == TARGET_NR_capget) {
8442 for (i = 0; i < data_items; i++) {
8443 target_data[i].effective = tswap32(data[i].effective);
8444 target_data[i].permitted = tswap32(data[i].permitted);
8445 target_data[i].inheritable = tswap32(data[i].inheritable);
8446 }
8447 unlock_user(target_data, arg2, target_datalen);
8448 } else {
8449 unlock_user(target_data, arg2, 0);
8450 }
8451 }
8452 break;
8453 }
bellard31e31b82003-02-18 22:55:36 +00008454 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00008455#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02008456 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
Jia Liud9627832012-07-20 15:50:52 +08008457 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
Andreas Färber9349b4f2012-03-14 01:38:32 +01008458 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00008459 break;
8460#else
bellard5cd43932003-03-29 16:54:36 +00008461 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00008462#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +00008463
8464#ifdef CONFIG_SENDFILE
bellard31e31b82003-02-18 22:55:36 +00008465 case TARGET_NR_sendfile:
Peter Maydella8fd1ab2013-02-08 07:31:55 +00008466 {
8467 off_t *offp = NULL;
8468 off_t off;
8469 if (arg3) {
8470 ret = get_user_sal(off, arg3);
8471 if (is_error(ret)) {
8472 break;
8473 }
8474 offp = &off;
8475 }
8476 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
8477 if (!is_error(ret) && arg3) {
8478 abi_long ret2 = put_user_sal(off, arg3);
8479 if (is_error(ret2)) {
8480 ret = ret2;
8481 }
8482 }
8483 break;
8484 }
8485#ifdef TARGET_NR_sendfile64
8486 case TARGET_NR_sendfile64:
8487 {
8488 off_t *offp = NULL;
8489 off_t off;
8490 if (arg3) {
8491 ret = get_user_s64(off, arg3);
8492 if (is_error(ret)) {
8493 break;
8494 }
8495 offp = &off;
8496 }
8497 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
8498 if (!is_error(ret) && arg3) {
8499 abi_long ret2 = put_user_s64(off, arg3);
8500 if (is_error(ret2)) {
8501 ret = ret2;
8502 }
8503 }
8504 break;
8505 }
8506#endif
8507#else
8508 case TARGET_NR_sendfile:
Peter Maydell7edd2cf2013-04-21 13:30:03 +01008509#ifdef TARGET_NR_sendfile64
Peter Maydella8fd1ab2013-02-08 07:31:55 +00008510 case TARGET_NR_sendfile64:
8511#endif
bellard5cd43932003-03-29 16:54:36 +00008512 goto unimplemented;
Peter Maydella8fd1ab2013-02-08 07:31:55 +00008513#endif
8514
bellardebc05482003-09-30 21:08:41 +00008515#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00008516 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00008517 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008518#endif
8519#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00008520 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00008521 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008522#endif
bellard048f6b42005-11-26 18:47:20 +00008523#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00008524 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00008525 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
8526 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00008527 break;
bellard048f6b42005-11-26 18:47:20 +00008528#endif
bellardebc05482003-09-30 21:08:41 +00008529#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00008530 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00008531 {
8532 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03008533 int resource = target_to_host_resource(arg1);
8534 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00008535 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00008536 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00008537 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
8538 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09008539 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
8540 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00008541 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00008542 }
8543 break;
8544 }
bellardebc05482003-09-30 21:08:41 +00008545#endif
bellarda315a142005-01-30 22:59:18 +00008546#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00008547 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00008548 if (!(p = lock_user_string(arg1)))
8549 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008550 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
8551 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00008552 break;
bellarda315a142005-01-30 22:59:18 +00008553#endif
8554#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00008555 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00008556 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00008557 break;
bellarda315a142005-01-30 22:59:18 +00008558#endif
8559#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00008560 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00008561 if (!(p = lock_user_string(arg1)))
8562 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008563 ret = get_errno(stat(path(p), &st));
8564 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00008565 if (!is_error(ret))
8566 ret = host_to_target_stat64(cpu_env, arg2, &st);
8567 break;
bellarda315a142005-01-30 22:59:18 +00008568#endif
8569#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00008570 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00008571 if (!(p = lock_user_string(arg1)))
8572 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008573 ret = get_errno(lstat(path(p), &st));
8574 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00008575 if (!is_error(ret))
8576 ret = host_to_target_stat64(cpu_env, arg2, &st);
8577 break;
bellarda315a142005-01-30 22:59:18 +00008578#endif
8579#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00008580 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00008581 ret = get_errno(fstat(arg1, &st));
8582 if (!is_error(ret))
8583 ret = host_to_target_stat64(cpu_env, arg2, &st);
8584 break;
bellardec86b0f2003-04-11 00:15:04 +00008585#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01008586#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
aurel329d33b762009-04-08 23:07:05 +00008587#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00008588 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00008589#endif
8590#ifdef TARGET_NR_newfstatat
8591 case TARGET_NR_newfstatat:
8592#endif
balrog6a24a772008-09-20 02:23:36 +00008593 if (!(p = lock_user_string(arg2)))
8594 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008595 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
balrog6a24a772008-09-20 02:23:36 +00008596 if (!is_error(ret))
8597 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00008598 break;
bellarda315a142005-01-30 22:59:18 +00008599#endif
Chen Gang704eff62015-08-21 05:37:33 +08008600#ifdef TARGET_NR_lchown
bellard67867302003-11-23 17:05:30 +00008601 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00008602 if (!(p = lock_user_string(arg1)))
8603 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008604 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
8605 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00008606 break;
Chen Gang704eff62015-08-21 05:37:33 +08008607#endif
Riku Voipio0c866a72011-04-18 15:23:06 +03008608#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00008609 case TARGET_NR_getuid:
8610 ret = get_errno(high2lowuid(getuid()));
8611 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03008612#endif
8613#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00008614 case TARGET_NR_getgid:
8615 ret = get_errno(high2lowgid(getgid()));
8616 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03008617#endif
8618#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00008619 case TARGET_NR_geteuid:
8620 ret = get_errno(high2lowuid(geteuid()));
8621 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03008622#endif
8623#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00008624 case TARGET_NR_getegid:
8625 ret = get_errno(high2lowgid(getegid()));
8626 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03008627#endif
bellard67867302003-11-23 17:05:30 +00008628 case TARGET_NR_setreuid:
8629 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
8630 break;
8631 case TARGET_NR_setregid:
8632 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
8633 break;
8634 case TARGET_NR_getgroups:
8635 {
8636 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03008637 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00008638 gid_t *grouplist;
8639 int i;
8640
8641 grouplist = alloca(gidsetsize * sizeof(gid_t));
8642 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00008643 if (gidsetsize == 0)
8644 break;
bellard67867302003-11-23 17:05:30 +00008645 if (!is_error(ret)) {
Andreas Schwab03903ff2013-04-09 05:41:33 +00008646 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
bellard579a97f2007-11-11 14:26:47 +00008647 if (!target_grouplist)
8648 goto efault;
balroga2155fc2008-09-20 02:12:08 +00008649 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03008650 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
Andreas Schwab03903ff2013-04-09 05:41:33 +00008651 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
bellard67867302003-11-23 17:05:30 +00008652 }
8653 }
8654 break;
8655 case TARGET_NR_setgroups:
8656 {
8657 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03008658 target_id *target_grouplist;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05008659 gid_t *grouplist = NULL;
bellard67867302003-11-23 17:05:30 +00008660 int i;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05008661 if (gidsetsize) {
8662 grouplist = alloca(gidsetsize * sizeof(gid_t));
Andreas Schwab03903ff2013-04-09 05:41:33 +00008663 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05008664 if (!target_grouplist) {
8665 ret = -TARGET_EFAULT;
8666 goto fail;
8667 }
8668 for (i = 0; i < gidsetsize; i++) {
8669 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
8670 }
8671 unlock_user(target_grouplist, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00008672 }
bellard67867302003-11-23 17:05:30 +00008673 ret = get_errno(setgroups(gidsetsize, grouplist));
8674 }
8675 break;
8676 case TARGET_NR_fchown:
8677 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
8678 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008679#if defined(TARGET_NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +00008680 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00008681 if (!(p = lock_user_string(arg2)))
8682 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008683 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
8684 low2highgid(arg4), arg5));
bellard579a97f2007-11-11 14:26:47 +00008685 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00008686 break;
8687#endif
bellard67867302003-11-23 17:05:30 +00008688#ifdef TARGET_NR_setresuid
8689 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00008690 ret = get_errno(setresuid(low2highuid(arg1),
8691 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00008692 low2highuid(arg3)));
8693 break;
8694#endif
8695#ifdef TARGET_NR_getresuid
8696 case TARGET_NR_getresuid:
8697 {
pbrook53a59602006-03-25 19:31:22 +00008698 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00008699 ret = get_errno(getresuid(&ruid, &euid, &suid));
8700 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +00008701 if (put_user_id(high2lowuid(ruid), arg1)
8702 || put_user_id(high2lowuid(euid), arg2)
8703 || put_user_id(high2lowuid(suid), arg3))
bellard2f619692007-11-16 10:46:05 +00008704 goto efault;
bellard67867302003-11-23 17:05:30 +00008705 }
8706 }
8707 break;
8708#endif
8709#ifdef TARGET_NR_getresgid
8710 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00008711 ret = get_errno(setresgid(low2highgid(arg1),
8712 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00008713 low2highgid(arg3)));
8714 break;
8715#endif
8716#ifdef TARGET_NR_getresgid
8717 case TARGET_NR_getresgid:
8718 {
pbrook53a59602006-03-25 19:31:22 +00008719 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00008720 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8721 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +00008722 if (put_user_id(high2lowgid(rgid), arg1)
8723 || put_user_id(high2lowgid(egid), arg2)
8724 || put_user_id(high2lowgid(sgid), arg3))
bellard2f619692007-11-16 10:46:05 +00008725 goto efault;
bellard67867302003-11-23 17:05:30 +00008726 }
8727 }
8728 break;
8729#endif
Chen Gang704eff62015-08-21 05:37:33 +08008730#ifdef TARGET_NR_chown
bellard67867302003-11-23 17:05:30 +00008731 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00008732 if (!(p = lock_user_string(arg1)))
8733 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008734 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
8735 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00008736 break;
Chen Gang704eff62015-08-21 05:37:33 +08008737#endif
bellard67867302003-11-23 17:05:30 +00008738 case TARGET_NR_setuid:
8739 ret = get_errno(setuid(low2highuid(arg1)));
8740 break;
8741 case TARGET_NR_setgid:
8742 ret = get_errno(setgid(low2highgid(arg1)));
8743 break;
8744 case TARGET_NR_setfsuid:
8745 ret = get_errno(setfsuid(arg1));
8746 break;
8747 case TARGET_NR_setfsgid:
8748 ret = get_errno(setfsgid(arg1));
8749 break;
bellard67867302003-11-23 17:05:30 +00008750
bellarda315a142005-01-30 22:59:18 +00008751#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00008752 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00008753 if (!(p = lock_user_string(arg1)))
8754 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008755 ret = get_errno(lchown(p, arg2, arg3));
8756 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00008757 break;
bellarda315a142005-01-30 22:59:18 +00008758#endif
8759#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00008760 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00008761 ret = get_errno(getuid());
8762 break;
bellarda315a142005-01-30 22:59:18 +00008763#endif
aurel3264b4d282008-11-14 17:20:15 +00008764
8765#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
8766 /* Alpha specific */
8767 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08008768 {
8769 uid_t euid;
8770 euid=geteuid();
8771 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
8772 }
aurel3264b4d282008-11-14 17:20:15 +00008773 ret = get_errno(getuid());
8774 break;
8775#endif
8776#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
8777 /* Alpha specific */
8778 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08008779 {
8780 uid_t egid;
8781 egid=getegid();
8782 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
8783 }
aurel3264b4d282008-11-14 17:20:15 +00008784 ret = get_errno(getgid());
8785 break;
8786#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08008787#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
8788 /* Alpha specific */
8789 case TARGET_NR_osf_getsysinfo:
8790 ret = -TARGET_EOPNOTSUPP;
8791 switch (arg1) {
8792 case TARGET_GSI_IEEE_FP_CONTROL:
8793 {
8794 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
8795
8796 /* Copied from linux ieee_fpcr_to_swcr. */
8797 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
8798 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
8799 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
8800 | SWCR_TRAP_ENABLE_DZE
8801 | SWCR_TRAP_ENABLE_OVF);
8802 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
8803 | SWCR_TRAP_ENABLE_INE);
8804 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
8805 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
8806
8807 if (put_user_u64 (swcr, arg2))
8808 goto efault;
8809 ret = 0;
8810 }
8811 break;
8812
8813 /* case GSI_IEEE_STATE_AT_SIGNAL:
8814 -- Not implemented in linux kernel.
8815 case GSI_UACPROC:
8816 -- Retrieves current unaligned access state; not much used.
8817 case GSI_PROC_TYPE:
8818 -- Retrieves implver information; surely not used.
8819 case GSI_GET_HWRPB:
8820 -- Grabs a copy of the HWRPB; surely not used.
8821 */
8822 }
8823 break;
8824#endif
8825#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8826 /* Alpha specific */
8827 case TARGET_NR_osf_setsysinfo:
8828 ret = -TARGET_EOPNOTSUPP;
8829 switch (arg1) {
8830 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -08008831 {
8832 uint64_t swcr, fpcr, orig_fpcr;
8833
Richard Henderson6e06d512012-06-01 09:08:21 -07008834 if (get_user_u64 (swcr, arg2)) {
Richard Hendersonba0e2762009-12-09 15:56:29 -08008835 goto efault;
Richard Henderson6e06d512012-06-01 09:08:21 -07008836 }
8837 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008838 fpcr = orig_fpcr & FPCR_DYN_MASK;
8839
8840 /* Copied from linux ieee_swcr_to_fpcr. */
8841 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
8842 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
8843 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
8844 | SWCR_TRAP_ENABLE_DZE
8845 | SWCR_TRAP_ENABLE_OVF)) << 48;
8846 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
8847 | SWCR_TRAP_ENABLE_INE)) << 57;
8848 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
8849 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
8850
Richard Henderson6e06d512012-06-01 09:08:21 -07008851 cpu_alpha_store_fpcr(cpu_env, fpcr);
8852 ret = 0;
8853 }
8854 break;
8855
8856 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
8857 {
8858 uint64_t exc, fpcr, orig_fpcr;
8859 int si_code;
8860
8861 if (get_user_u64(exc, arg2)) {
8862 goto efault;
8863 }
8864
8865 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8866
8867 /* We only add to the exception status here. */
8868 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
8869
8870 cpu_alpha_store_fpcr(cpu_env, fpcr);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008871 ret = 0;
8872
Richard Henderson6e06d512012-06-01 09:08:21 -07008873 /* Old exceptions are not signaled. */
8874 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008875
Richard Henderson6e06d512012-06-01 09:08:21 -07008876 /* If any exceptions set by this call,
8877 and are unmasked, send a signal. */
8878 si_code = 0;
8879 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
8880 si_code = TARGET_FPE_FLTRES;
8881 }
8882 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
8883 si_code = TARGET_FPE_FLTUND;
8884 }
8885 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
8886 si_code = TARGET_FPE_FLTOVF;
8887 }
8888 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
8889 si_code = TARGET_FPE_FLTDIV;
8890 }
8891 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
8892 si_code = TARGET_FPE_FLTINV;
8893 }
8894 if (si_code != 0) {
8895 target_siginfo_t info;
8896 info.si_signo = SIGFPE;
8897 info.si_errno = 0;
8898 info.si_code = si_code;
8899 info._sifields._sigfault._addr
8900 = ((CPUArchState *)cpu_env)->pc;
8901 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008902 }
8903 }
8904 break;
8905
8906 /* case SSI_NVPAIRS:
8907 -- Used with SSIN_UACPROC to enable unaligned accesses.
8908 case SSI_IEEE_STATE_AT_SIGNAL:
8909 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8910 -- Not implemented in linux kernel
8911 */
8912 }
8913 break;
8914#endif
8915#ifdef TARGET_NR_osf_sigprocmask
8916 /* Alpha specific. */
8917 case TARGET_NR_osf_sigprocmask:
8918 {
8919 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01008920 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08008921 sigset_t set, oldset;
8922
8923 switch(arg1) {
8924 case TARGET_SIG_BLOCK:
8925 how = SIG_BLOCK;
8926 break;
8927 case TARGET_SIG_UNBLOCK:
8928 how = SIG_UNBLOCK;
8929 break;
8930 case TARGET_SIG_SETMASK:
8931 how = SIG_SETMASK;
8932 break;
8933 default:
8934 ret = -TARGET_EINVAL;
8935 goto fail;
8936 }
8937 mask = arg2;
8938 target_to_host_old_sigset(&set, &mask);
Alex Barcelo1c275922014-03-14 14:36:55 +00008939 do_sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008940 host_to_target_old_sigset(&mask, &oldset);
8941 ret = mask;
8942 }
8943 break;
8944#endif
aurel3264b4d282008-11-14 17:20:15 +00008945
bellarda315a142005-01-30 22:59:18 +00008946#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00008947 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00008948 ret = get_errno(getgid());
8949 break;
bellarda315a142005-01-30 22:59:18 +00008950#endif
8951#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00008952 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00008953 ret = get_errno(geteuid());
8954 break;
bellarda315a142005-01-30 22:59:18 +00008955#endif
8956#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00008957 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00008958 ret = get_errno(getegid());
8959 break;
bellarda315a142005-01-30 22:59:18 +00008960#endif
8961#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00008962 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00008963 ret = get_errno(setreuid(arg1, arg2));
8964 break;
bellarda315a142005-01-30 22:59:18 +00008965#endif
8966#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00008967 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00008968 ret = get_errno(setregid(arg1, arg2));
8969 break;
bellarda315a142005-01-30 22:59:18 +00008970#endif
8971#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00008972 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00008973 {
8974 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00008975 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00008976 gid_t *grouplist;
8977 int i;
8978
8979 grouplist = alloca(gidsetsize * sizeof(gid_t));
8980 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00008981 if (gidsetsize == 0)
8982 break;
bellard99c475a2005-01-31 20:45:13 +00008983 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00008984 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
8985 if (!target_grouplist) {
8986 ret = -TARGET_EFAULT;
8987 goto fail;
8988 }
balroga2155fc2008-09-20 02:12:08 +00008989 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00008990 target_grouplist[i] = tswap32(grouplist[i]);
8991 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00008992 }
8993 }
8994 break;
bellarda315a142005-01-30 22:59:18 +00008995#endif
8996#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00008997 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00008998 {
8999 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00009000 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00009001 gid_t *grouplist;
9002 int i;
ths3b46e622007-09-17 08:09:54 +00009003
bellard99c475a2005-01-31 20:45:13 +00009004 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00009005 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
9006 if (!target_grouplist) {
9007 ret = -TARGET_EFAULT;
9008 goto fail;
9009 }
bellard99c475a2005-01-31 20:45:13 +00009010 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00009011 grouplist[i] = tswap32(target_grouplist[i]);
9012 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00009013 ret = get_errno(setgroups(gidsetsize, grouplist));
9014 }
9015 break;
bellarda315a142005-01-30 22:59:18 +00009016#endif
9017#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00009018 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00009019 ret = get_errno(fchown(arg1, arg2, arg3));
9020 break;
bellarda315a142005-01-30 22:59:18 +00009021#endif
9022#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00009023 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00009024 ret = get_errno(setresuid(arg1, arg2, arg3));
9025 break;
bellarda315a142005-01-30 22:59:18 +00009026#endif
9027#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00009028 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00009029 {
pbrook53a59602006-03-25 19:31:22 +00009030 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00009031 ret = get_errno(getresuid(&ruid, &euid, &suid));
9032 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00009033 if (put_user_u32(ruid, arg1)
9034 || put_user_u32(euid, arg2)
9035 || put_user_u32(suid, arg3))
9036 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00009037 }
9038 }
9039 break;
bellarda315a142005-01-30 22:59:18 +00009040#endif
9041#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00009042 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00009043 ret = get_errno(setresgid(arg1, arg2, arg3));
9044 break;
bellarda315a142005-01-30 22:59:18 +00009045#endif
9046#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00009047 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00009048 {
pbrook53a59602006-03-25 19:31:22 +00009049 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00009050 ret = get_errno(getresgid(&rgid, &egid, &sgid));
9051 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00009052 if (put_user_u32(rgid, arg1)
9053 || put_user_u32(egid, arg2)
9054 || put_user_u32(sgid, arg3))
9055 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00009056 }
9057 }
9058 break;
bellarda315a142005-01-30 22:59:18 +00009059#endif
9060#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00009061 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00009062 if (!(p = lock_user_string(arg1)))
9063 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009064 ret = get_errno(chown(p, arg2, arg3));
9065 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00009066 break;
bellarda315a142005-01-30 22:59:18 +00009067#endif
9068#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00009069 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00009070 ret = get_errno(setuid(arg1));
9071 break;
bellarda315a142005-01-30 22:59:18 +00009072#endif
9073#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00009074 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00009075 ret = get_errno(setgid(arg1));
9076 break;
bellarda315a142005-01-30 22:59:18 +00009077#endif
9078#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00009079 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00009080 ret = get_errno(setfsuid(arg1));
9081 break;
bellarda315a142005-01-30 22:59:18 +00009082#endif
9083#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00009084 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00009085 ret = get_errno(setfsgid(arg1));
9086 break;
bellarda315a142005-01-30 22:59:18 +00009087#endif
bellard67867302003-11-23 17:05:30 +00009088
bellard31e31b82003-02-18 22:55:36 +00009089 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00009090 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00009091#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00009092 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00009093 {
9094 void *a;
9095 ret = -TARGET_EFAULT;
9096 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
9097 goto efault;
9098 if (!(p = lock_user_string(arg3)))
9099 goto mincore_fail;
9100 ret = get_errno(mincore(a, arg2, p));
9101 unlock_user(p, arg3, ret);
9102 mincore_fail:
9103 unlock_user(a, arg1, 0);
9104 }
9105 break;
bellardffa65c32004-01-04 23:57:22 +00009106#endif
aurel32408321b2008-10-01 21:46:32 +00009107#ifdef TARGET_NR_arm_fadvise64_64
9108 case TARGET_NR_arm_fadvise64_64:
9109 {
9110 /*
9111 * arm_fadvise64_64 looks like fadvise64_64 but
9112 * with different argument order
9113 */
9114 abi_long temp;
9115 temp = arg3;
9116 arg3 = arg4;
9117 arg4 = temp;
9118 }
9119#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02009120#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00009121#ifdef TARGET_NR_fadvise64_64
9122 case TARGET_NR_fadvise64_64:
9123#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02009124#ifdef TARGET_NR_fadvise64
9125 case TARGET_NR_fadvise64:
9126#endif
9127#ifdef TARGET_S390X
9128 switch (arg4) {
9129 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
9130 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
9131 case 6: arg4 = POSIX_FADV_DONTNEED; break;
9132 case 7: arg4 = POSIX_FADV_NOREUSE; break;
9133 default: break;
9134 }
9135#endif
9136 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00009137 break;
9138#endif
bellardffa65c32004-01-04 23:57:22 +00009139#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00009140 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00009141 /* A straight passthrough may not be safe because qemu sometimes
Lei Lid2d6b852013-05-20 17:20:50 +08009142 turns private file-backed mappings into anonymous mappings.
pbrook24836682006-04-16 14:14:53 +00009143 This will break MADV_DONTNEED.
9144 This is a hint, so ignoring and returning success is ok. */
9145 ret = get_errno(0);
9146 break;
bellardffa65c32004-01-04 23:57:22 +00009147#endif
blueswir1992f48a2007-10-14 16:27:31 +00009148#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00009149 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00009150 {
thsb1e341e2007-03-20 21:50:52 +00009151 int cmd;
bellard77e46722003-04-29 20:39:06 +00009152 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00009153 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00009154#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00009155 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00009156#endif
bellard77e46722003-04-29 20:39:06 +00009157
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02009158 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00009159 if (cmd == -TARGET_EINVAL) {
9160 ret = cmd;
9161 break;
9162 }
thsb1e341e2007-03-20 21:50:52 +00009163
bellard60cd49d2003-03-16 22:53:56 +00009164 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00009165 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00009166#ifdef TARGET_ARM
9167 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00009168 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
9169 goto efault;
ths58134272007-03-31 18:59:32 +00009170 fl.l_type = tswap16(target_efl->l_type);
9171 fl.l_whence = tswap16(target_efl->l_whence);
9172 fl.l_start = tswap64(target_efl->l_start);
9173 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009174 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00009175 unlock_user_struct(target_efl, arg3, 0);
9176 } else
9177#endif
9178 {
bellard9ee1fa22007-11-11 15:11:19 +00009179 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
9180 goto efault;
ths58134272007-03-31 18:59:32 +00009181 fl.l_type = tswap16(target_fl->l_type);
9182 fl.l_whence = tswap16(target_fl->l_whence);
9183 fl.l_start = tswap64(target_fl->l_start);
9184 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009185 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00009186 unlock_user_struct(target_fl, arg3, 0);
9187 }
thsb1e341e2007-03-20 21:50:52 +00009188 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00009189 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00009190#ifdef TARGET_ARM
9191 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00009192 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
9193 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00009194 target_efl->l_type = tswap16(fl.l_type);
9195 target_efl->l_whence = tswap16(fl.l_whence);
9196 target_efl->l_start = tswap64(fl.l_start);
9197 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009198 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00009199 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00009200 } else
9201#endif
9202 {
bellard9ee1fa22007-11-11 15:11:19 +00009203 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
9204 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00009205 target_fl->l_type = tswap16(fl.l_type);
9206 target_fl->l_whence = tswap16(fl.l_whence);
9207 target_fl->l_start = tswap64(fl.l_start);
9208 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009209 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00009210 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00009211 }
bellard77e46722003-04-29 20:39:06 +00009212 }
9213 break;
9214
thsb1e341e2007-03-20 21:50:52 +00009215 case TARGET_F_SETLK64:
9216 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00009217#ifdef TARGET_ARM
9218 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00009219 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
9220 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00009221 fl.l_type = tswap16(target_efl->l_type);
9222 fl.l_whence = tswap16(target_efl->l_whence);
9223 fl.l_start = tswap64(target_efl->l_start);
9224 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009225 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00009226 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00009227 } else
9228#endif
9229 {
bellard9ee1fa22007-11-11 15:11:19 +00009230 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
9231 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00009232 fl.l_type = tswap16(target_fl->l_type);
9233 fl.l_whence = tswap16(target_fl->l_whence);
9234 fl.l_start = tswap64(target_fl->l_start);
9235 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009236 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00009237 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00009238 }
thsb1e341e2007-03-20 21:50:52 +00009239 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00009240 break;
bellard60cd49d2003-03-16 22:53:56 +00009241 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02009242 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00009243 break;
9244 }
bellard77e46722003-04-29 20:39:06 +00009245 break;
9246 }
bellard60cd49d2003-03-16 22:53:56 +00009247#endif
ths7d600c82006-12-08 01:32:58 +00009248#ifdef TARGET_NR_cacheflush
9249 case TARGET_NR_cacheflush:
9250 /* self-modifying code is handled automatically, so nothing needed */
9251 ret = 0;
9252 break;
9253#endif
bellardebc05482003-09-30 21:08:41 +00009254#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00009255 case TARGET_NR_security:
9256 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00009257#endif
bellardc573ff62004-01-04 15:51:36 +00009258#ifdef TARGET_NR_getpagesize
9259 case TARGET_NR_getpagesize:
9260 ret = TARGET_PAGE_SIZE;
9261 break;
9262#endif
bellard31e31b82003-02-18 22:55:36 +00009263 case TARGET_NR_gettid:
9264 ret = get_errno(gettid());
9265 break;
thse5febef2007-04-01 18:31:35 +00009266#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00009267 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00009268#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +03009269 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +00009270 arg2 = arg3;
9271 arg3 = arg4;
9272 arg4 = arg5;
9273 }
aurel322054ac92008-10-13 21:08:07 +00009274 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
9275#else
9276 ret = get_errno(readahead(arg1, arg2, arg3));
9277#endif
9278 break;
thse5febef2007-04-01 18:31:35 +00009279#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009280#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +00009281#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00009282 case TARGET_NR_listxattr:
9283 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +00009284 {
9285 void *p, *b = 0;
9286 if (arg2) {
9287 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
9288 if (!b) {
9289 ret = -TARGET_EFAULT;
9290 break;
9291 }
9292 }
9293 p = lock_user_string(arg1);
9294 if (p) {
9295 if (num == TARGET_NR_listxattr) {
9296 ret = get_errno(listxattr(p, b, arg3));
9297 } else {
9298 ret = get_errno(llistxattr(p, b, arg3));
9299 }
9300 } else {
9301 ret = -TARGET_EFAULT;
9302 }
9303 unlock_user(p, arg1, 0);
9304 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +03009305 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +00009306 }
9307 case TARGET_NR_flistxattr:
9308 {
9309 void *b = 0;
9310 if (arg2) {
9311 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
9312 if (!b) {
9313 ret = -TARGET_EFAULT;
9314 break;
9315 }
9316 }
9317 ret = get_errno(flistxattr(arg1, b, arg3));
9318 unlock_user(b, arg2, arg3);
9319 break;
9320 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009321 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00009322 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009323 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00009324 void *p, *n, *v = 0;
9325 if (arg3) {
9326 v = lock_user(VERIFY_READ, arg3, arg4, 1);
9327 if (!v) {
9328 ret = -TARGET_EFAULT;
9329 break;
9330 }
9331 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009332 p = lock_user_string(arg1);
9333 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00009334 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00009335 if (num == TARGET_NR_setxattr) {
9336 ret = get_errno(setxattr(p, n, v, arg4, arg5));
9337 } else {
9338 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
9339 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009340 } else {
9341 ret = -TARGET_EFAULT;
9342 }
9343 unlock_user(p, arg1, 0);
9344 unlock_user(n, arg2, 0);
9345 unlock_user(v, arg3, 0);
9346 }
9347 break;
Peter Maydell30297b52011-12-14 15:37:18 +00009348 case TARGET_NR_fsetxattr:
9349 {
9350 void *n, *v = 0;
9351 if (arg3) {
9352 v = lock_user(VERIFY_READ, arg3, arg4, 1);
9353 if (!v) {
9354 ret = -TARGET_EFAULT;
9355 break;
9356 }
9357 }
9358 n = lock_user_string(arg2);
9359 if (n) {
9360 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
9361 } else {
9362 ret = -TARGET_EFAULT;
9363 }
9364 unlock_user(n, arg2, 0);
9365 unlock_user(v, arg3, 0);
9366 }
9367 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009368 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00009369 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009370 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00009371 void *p, *n, *v = 0;
9372 if (arg3) {
9373 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
9374 if (!v) {
9375 ret = -TARGET_EFAULT;
9376 break;
9377 }
9378 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009379 p = lock_user_string(arg1);
9380 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00009381 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00009382 if (num == TARGET_NR_getxattr) {
9383 ret = get_errno(getxattr(p, n, v, arg4));
9384 } else {
9385 ret = get_errno(lgetxattr(p, n, v, arg4));
9386 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009387 } else {
9388 ret = -TARGET_EFAULT;
9389 }
9390 unlock_user(p, arg1, 0);
9391 unlock_user(n, arg2, 0);
9392 unlock_user(v, arg3, arg4);
9393 }
9394 break;
Peter Maydell30297b52011-12-14 15:37:18 +00009395 case TARGET_NR_fgetxattr:
9396 {
9397 void *n, *v = 0;
9398 if (arg3) {
9399 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
9400 if (!v) {
9401 ret = -TARGET_EFAULT;
9402 break;
9403 }
9404 }
9405 n = lock_user_string(arg2);
9406 if (n) {
9407 ret = get_errno(fgetxattr(arg1, n, v, arg4));
9408 } else {
9409 ret = -TARGET_EFAULT;
9410 }
9411 unlock_user(n, arg2, 0);
9412 unlock_user(v, arg3, arg4);
9413 }
9414 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009415 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +00009416 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009417 {
9418 void *p, *n;
9419 p = lock_user_string(arg1);
9420 n = lock_user_string(arg2);
9421 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00009422 if (num == TARGET_NR_removexattr) {
9423 ret = get_errno(removexattr(p, n));
9424 } else {
9425 ret = get_errno(lremovexattr(p, n));
9426 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009427 } else {
9428 ret = -TARGET_EFAULT;
9429 }
9430 unlock_user(p, arg1, 0);
9431 unlock_user(n, arg2, 0);
9432 }
9433 break;
Peter Maydell30297b52011-12-14 15:37:18 +00009434 case TARGET_NR_fremovexattr:
9435 {
9436 void *n;
9437 n = lock_user_string(arg2);
9438 if (n) {
9439 ret = get_errno(fremovexattr(arg1, n));
9440 } else {
9441 ret = -TARGET_EFAULT;
9442 }
9443 unlock_user(n, arg2, 0);
9444 }
9445 break;
bellardebc05482003-09-30 21:08:41 +00009446#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009447#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +00009448#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00009449 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00009450#if defined(TARGET_MIPS)
Petar Jovanovicd2792792014-06-18 17:48:20 +02009451 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
ths6f5b89a2007-03-02 20:48:00 +00009452 ret = 0;
9453 break;
edgar_iglef967792009-01-07 14:19:38 +00009454#elif defined(TARGET_CRIS)
9455 if (arg1 & 0xff)
9456 ret = -TARGET_EINVAL;
9457 else {
9458 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
9459 ret = 0;
9460 }
9461 break;
bellard8d18e892007-11-14 15:18:40 +00009462#elif defined(TARGET_I386) && defined(TARGET_ABI32)
9463 ret = do_set_thread_area(cpu_env, arg1);
9464 break;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009465#elif defined(TARGET_M68K)
9466 {
Andreas Färber0429a972013-08-26 18:14:44 +02009467 TaskState *ts = cpu->opaque;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009468 ts->tp_value = arg1;
Peter Maydell95c1eb12013-07-29 11:31:49 +01009469 ret = 0;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009470 break;
9471 }
ths6f5b89a2007-03-02 20:48:00 +00009472#else
9473 goto unimplemented_nowarn;
9474#endif
9475#endif
9476#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00009477 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00009478#if defined(TARGET_I386) && defined(TARGET_ABI32)
9479 ret = do_get_thread_area(cpu_env, arg1);
Peter Maydelld312bbe2013-07-16 18:44:56 +01009480 break;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009481#elif defined(TARGET_M68K)
9482 {
Andreas Färber0429a972013-08-26 18:14:44 +02009483 TaskState *ts = cpu->opaque;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009484 ret = ts->tp_value;
9485 break;
9486 }
bellard8d18e892007-11-14 15:18:40 +00009487#else
bellard5cd43932003-03-29 16:54:36 +00009488 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00009489#endif
bellard8d18e892007-11-14 15:18:40 +00009490#endif
bellard48dc41e2006-06-21 18:15:50 +00009491#ifdef TARGET_NR_getdomainname
9492 case TARGET_NR_getdomainname:
9493 goto unimplemented_nowarn;
9494#endif
ths6f5b89a2007-03-02 20:48:00 +00009495
thsb5906f92007-03-19 13:32:45 +00009496#ifdef TARGET_NR_clock_gettime
9497 case TARGET_NR_clock_gettime:
9498 {
9499 struct timespec ts;
9500 ret = get_errno(clock_gettime(arg1, &ts));
9501 if (!is_error(ret)) {
9502 host_to_target_timespec(arg2, &ts);
9503 }
9504 break;
9505 }
9506#endif
9507#ifdef TARGET_NR_clock_getres
9508 case TARGET_NR_clock_getres:
9509 {
9510 struct timespec ts;
9511 ret = get_errno(clock_getres(arg1, &ts));
9512 if (!is_error(ret)) {
9513 host_to_target_timespec(arg2, &ts);
9514 }
9515 break;
9516 }
9517#endif
pbrook63d76512008-05-29 13:43:29 +00009518#ifdef TARGET_NR_clock_nanosleep
9519 case TARGET_NR_clock_nanosleep:
9520 {
9521 struct timespec ts;
9522 target_to_host_timespec(&ts, arg3);
9523 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
9524 if (arg4)
9525 host_to_target_timespec(arg4, &ts);
Tom Musta8fbe8fd2014-08-12 13:53:41 -05009526
9527#if defined(TARGET_PPC)
9528 /* clock_nanosleep is odd in that it returns positive errno values.
9529 * On PPC, CR0 bit 3 should be set in such a situation. */
9530 if (ret) {
9531 ((CPUPPCState *)cpu_env)->crf[0] |= 1;
9532 }
9533#endif
pbrook63d76512008-05-29 13:43:29 +00009534 break;
9535 }
9536#endif
thsb5906f92007-03-19 13:32:45 +00009537
ths6f5b89a2007-03-02 20:48:00 +00009538#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
9539 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00009540 ret = get_errno(set_tid_address((int *)g2h(arg1)));
9541 break;
ths6f5b89a2007-03-02 20:48:00 +00009542#endif
9543
ths3ae43202007-09-16 21:39:48 +00009544#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00009545 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00009546 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00009547 break;
9548#endif
9549
ths3ae43202007-09-16 21:39:48 +00009550#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00009551 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00009552 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
9553 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00009554 break;
9555#endif
9556
ths4f2b1fe2007-06-21 21:57:12 +00009557#ifdef TARGET_NR_set_robust_list
9558 case TARGET_NR_set_robust_list:
Peter Maydelle9a970a2013-02-08 04:34:54 +00009559 case TARGET_NR_get_robust_list:
9560 /* The ABI for supporting robust futexes has userspace pass
9561 * the kernel a pointer to a linked list which is updated by
9562 * userspace after the syscall; the list is walked by the kernel
9563 * when the thread exits. Since the linked list in QEMU guest
9564 * memory isn't a valid linked list for the host and we have
9565 * no way to reliably intercept the thread-death event, we can't
9566 * support these. Silently return ENOSYS so that guest userspace
9567 * falls back to a non-robust futex implementation (which should
9568 * be OK except in the corner case of the guest crashing while
9569 * holding a mutex that is shared with another process via
9570 * shared memory).
9571 */
9572 goto unimplemented_nowarn;
ths4f2b1fe2007-06-21 21:57:12 +00009573#endif
9574
Peter Maydell1acae9f2013-07-02 14:04:12 +01009575#if defined(TARGET_NR_utimensat)
ths9007f0e2007-09-25 17:50:37 +00009576 case TARGET_NR_utimensat:
9577 {
Riku Voipioebc996f2009-04-21 15:01:51 +03009578 struct timespec *tsp, ts[2];
9579 if (!arg3) {
9580 tsp = NULL;
9581 } else {
9582 target_to_host_timespec(ts, arg3);
9583 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
9584 tsp = ts;
9585 }
ths9007f0e2007-09-25 17:50:37 +00009586 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03009587 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00009588 else {
bellard579a97f2007-11-11 14:26:47 +00009589 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00009590 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00009591 goto fail;
9592 }
Riku Voipioebc996f2009-04-21 15:01:51 +03009593 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00009594 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00009595 }
9596 }
9597 break;
9598#endif
pbrookbd0c5662008-05-29 14:34:11 +00009599 case TARGET_NR_futex:
9600 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
9601 break;
aurel32dbfe4c32009-04-08 21:29:30 +00009602#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00009603 case TARGET_NR_inotify_init:
9604 ret = get_errno(sys_inotify_init());
9605 break;
9606#endif
Stefan Weila1606b02010-03-28 11:44:41 +02009607#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00009608#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
9609 case TARGET_NR_inotify_init1:
9610 ret = get_errno(sys_inotify_init1(arg1));
9611 break;
9612#endif
Stefan Weila1606b02010-03-28 11:44:41 +02009613#endif
aurel32dbfe4c32009-04-08 21:29:30 +00009614#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00009615 case TARGET_NR_inotify_add_watch:
9616 p = lock_user_string(arg2);
9617 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
9618 unlock_user(p, arg2, 0);
9619 break;
9620#endif
aurel32dbfe4c32009-04-08 21:29:30 +00009621#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00009622 case TARGET_NR_inotify_rm_watch:
9623 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
9624 break;
9625#endif
ths9007f0e2007-09-25 17:50:37 +00009626
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07009627#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00009628 case TARGET_NR_mq_open:
9629 {
Tom Mustab6ce1f62014-08-12 13:53:36 -05009630 struct mq_attr posix_mq_attr, *attrp;
aurel3224e10032009-04-15 16:11:43 +00009631
9632 p = lock_user_string(arg1 - 1);
Tom Mustab6ce1f62014-08-12 13:53:36 -05009633 if (arg4 != 0) {
aurel3224e10032009-04-15 16:11:43 +00009634 copy_from_user_mq_attr (&posix_mq_attr, arg4);
Tom Mustab6ce1f62014-08-12 13:53:36 -05009635 attrp = &posix_mq_attr;
9636 } else {
9637 attrp = 0;
9638 }
9639 ret = get_errno(mq_open(p, arg2, arg3, attrp));
aurel3224e10032009-04-15 16:11:43 +00009640 unlock_user (p, arg1, 0);
9641 }
9642 break;
9643
9644 case TARGET_NR_mq_unlink:
9645 p = lock_user_string(arg1 - 1);
9646 ret = get_errno(mq_unlink(p));
9647 unlock_user (p, arg1, 0);
9648 break;
9649
9650 case TARGET_NR_mq_timedsend:
9651 {
9652 struct timespec ts;
9653
9654 p = lock_user (VERIFY_READ, arg2, arg3, 1);
9655 if (arg5 != 0) {
9656 target_to_host_timespec(&ts, arg5);
9657 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
9658 host_to_target_timespec(arg5, &ts);
9659 }
9660 else
9661 ret = get_errno(mq_send(arg1, p, arg3, arg4));
9662 unlock_user (p, arg2, arg3);
9663 }
9664 break;
9665
9666 case TARGET_NR_mq_timedreceive:
9667 {
9668 struct timespec ts;
9669 unsigned int prio;
9670
9671 p = lock_user (VERIFY_READ, arg2, arg3, 1);
9672 if (arg5 != 0) {
9673 target_to_host_timespec(&ts, arg5);
9674 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
9675 host_to_target_timespec(arg5, &ts);
9676 }
9677 else
9678 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
9679 unlock_user (p, arg2, arg3);
9680 if (arg4 != 0)
9681 put_user_u32(prio, arg4);
9682 }
9683 break;
9684
9685 /* Not implemented for now... */
9686/* case TARGET_NR_mq_notify: */
9687/* break; */
9688
9689 case TARGET_NR_mq_getsetattr:
9690 {
9691 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
9692 ret = 0;
9693 if (arg3 != 0) {
9694 ret = mq_getattr(arg1, &posix_mq_attr_out);
9695 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
9696 }
9697 if (arg2 != 0) {
9698 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
9699 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
9700 }
9701
9702 }
9703 break;
9704#endif
9705
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309706#ifdef CONFIG_SPLICE
9707#ifdef TARGET_NR_tee
9708 case TARGET_NR_tee:
9709 {
9710 ret = get_errno(tee(arg1,arg2,arg3,arg4));
9711 }
9712 break;
9713#endif
9714#ifdef TARGET_NR_splice
9715 case TARGET_NR_splice:
9716 {
9717 loff_t loff_in, loff_out;
9718 loff_t *ploff_in = NULL, *ploff_out = NULL;
Andreas Schwab17644b32015-03-10 17:11:35 +01009719 if (arg2) {
9720 if (get_user_u64(loff_in, arg2)) {
9721 goto efault;
9722 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309723 ploff_in = &loff_in;
9724 }
Andreas Schwab17644b32015-03-10 17:11:35 +01009725 if (arg4) {
9726 if (get_user_u64(loff_out, arg4)) {
9727 goto efault;
9728 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309729 ploff_out = &loff_out;
9730 }
9731 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
Andreas Schwab17644b32015-03-10 17:11:35 +01009732 if (arg2) {
9733 if (put_user_u64(loff_in, arg2)) {
9734 goto efault;
9735 }
9736 }
9737 if (arg4) {
9738 if (put_user_u64(loff_out, arg4)) {
9739 goto efault;
9740 }
9741 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309742 }
9743 break;
9744#endif
9745#ifdef TARGET_NR_vmsplice
9746 case TARGET_NR_vmsplice:
9747 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07009748 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
9749 if (vec != NULL) {
9750 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
9751 unlock_iovec(vec, arg2, arg3, 0);
9752 } else {
9753 ret = -host_to_target_errno(errno);
9754 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309755 }
9756 break;
9757#endif
9758#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03009759#ifdef CONFIG_EVENTFD
9760#if defined(TARGET_NR_eventfd)
9761 case TARGET_NR_eventfd:
9762 ret = get_errno(eventfd(arg1, 0));
Laurent Viviere36800c2015-10-02 14:48:09 +02009763 fd_trans_unregister(ret);
Riku Voipioc2882b92009-08-12 15:08:24 +03009764 break;
9765#endif
9766#if defined(TARGET_NR_eventfd2)
9767 case TARGET_NR_eventfd2:
Petar Jovanovic5947c692013-04-08 20:26:10 +02009768 {
9769 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
9770 if (arg2 & TARGET_O_NONBLOCK) {
9771 host_flags |= O_NONBLOCK;
9772 }
9773 if (arg2 & TARGET_O_CLOEXEC) {
9774 host_flags |= O_CLOEXEC;
9775 }
9776 ret = get_errno(eventfd(arg1, host_flags));
Laurent Viviere36800c2015-10-02 14:48:09 +02009777 fd_trans_unregister(ret);
Riku Voipioc2882b92009-08-12 15:08:24 +03009778 break;
Petar Jovanovic5947c692013-04-08 20:26:10 +02009779 }
Riku Voipioc2882b92009-08-12 15:08:24 +03009780#endif
9781#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03009782#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
9783 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +01009784#if TARGET_ABI_BITS == 32
9785 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
9786 target_offset64(arg5, arg6)));
9787#else
Ulrich Hechtd0927932009-09-17 20:22:14 +03009788 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +01009789#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03009790 break;
9791#endif
Peter Maydellc727f472011-01-06 11:05:10 +00009792#if defined(CONFIG_SYNC_FILE_RANGE)
9793#if defined(TARGET_NR_sync_file_range)
9794 case TARGET_NR_sync_file_range:
9795#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +03009796#if defined(TARGET_MIPS)
9797 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
9798 target_offset64(arg5, arg6), arg7));
9799#else
Peter Maydellc727f472011-01-06 11:05:10 +00009800 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
9801 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +03009802#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +00009803#else
9804 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
9805#endif
9806 break;
9807#endif
9808#if defined(TARGET_NR_sync_file_range2)
9809 case TARGET_NR_sync_file_range2:
9810 /* This is like sync_file_range but the arguments are reordered */
9811#if TARGET_ABI_BITS == 32
9812 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
9813 target_offset64(arg5, arg6), arg2));
9814#else
9815 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
9816#endif
9817 break;
9818#endif
9819#endif
Laurent Viviere36800c2015-10-02 14:48:09 +02009820#if defined(TARGET_NR_signalfd4)
9821 case TARGET_NR_signalfd4:
9822 ret = do_signalfd4(arg1, arg2, arg4);
9823 break;
9824#endif
9825#if defined(TARGET_NR_signalfd)
9826 case TARGET_NR_signalfd:
9827 ret = do_signalfd4(arg1, arg2, 0);
9828 break;
9829#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00009830#if defined(CONFIG_EPOLL)
9831#if defined(TARGET_NR_epoll_create)
9832 case TARGET_NR_epoll_create:
9833 ret = get_errno(epoll_create(arg1));
9834 break;
9835#endif
9836#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
9837 case TARGET_NR_epoll_create1:
9838 ret = get_errno(epoll_create1(arg1));
9839 break;
9840#endif
9841#if defined(TARGET_NR_epoll_ctl)
9842 case TARGET_NR_epoll_ctl:
9843 {
9844 struct epoll_event ep;
9845 struct epoll_event *epp = 0;
9846 if (arg4) {
9847 struct target_epoll_event *target_ep;
9848 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
9849 goto efault;
9850 }
9851 ep.events = tswap32(target_ep->events);
9852 /* The epoll_data_t union is just opaque data to the kernel,
9853 * so we transfer all 64 bits across and need not worry what
9854 * actual data type it is.
9855 */
9856 ep.data.u64 = tswap64(target_ep->data.u64);
9857 unlock_user_struct(target_ep, arg4, 0);
9858 epp = &ep;
9859 }
9860 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
9861 break;
9862 }
9863#endif
9864
9865#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9866#define IMPLEMENT_EPOLL_PWAIT
9867#endif
9868#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9869#if defined(TARGET_NR_epoll_wait)
9870 case TARGET_NR_epoll_wait:
9871#endif
9872#if defined(IMPLEMENT_EPOLL_PWAIT)
9873 case TARGET_NR_epoll_pwait:
9874#endif
9875 {
9876 struct target_epoll_event *target_ep;
9877 struct epoll_event *ep;
9878 int epfd = arg1;
9879 int maxevents = arg3;
9880 int timeout = arg4;
9881
9882 target_ep = lock_user(VERIFY_WRITE, arg2,
9883 maxevents * sizeof(struct target_epoll_event), 1);
9884 if (!target_ep) {
9885 goto efault;
9886 }
9887
9888 ep = alloca(maxevents * sizeof(struct epoll_event));
9889
9890 switch (num) {
9891#if defined(IMPLEMENT_EPOLL_PWAIT)
9892 case TARGET_NR_epoll_pwait:
9893 {
9894 target_sigset_t *target_set;
9895 sigset_t _set, *set = &_set;
9896
9897 if (arg5) {
9898 target_set = lock_user(VERIFY_READ, arg5,
9899 sizeof(target_sigset_t), 1);
9900 if (!target_set) {
9901 unlock_user(target_ep, arg2, 0);
9902 goto efault;
9903 }
9904 target_to_host_sigset(set, target_set);
9905 unlock_user(target_set, arg5, 0);
9906 } else {
9907 set = NULL;
9908 }
9909
9910 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
9911 break;
9912 }
9913#endif
9914#if defined(TARGET_NR_epoll_wait)
9915 case TARGET_NR_epoll_wait:
9916 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
9917 break;
9918#endif
9919 default:
9920 ret = -TARGET_ENOSYS;
9921 }
9922 if (!is_error(ret)) {
9923 int i;
9924 for (i = 0; i < ret; i++) {
9925 target_ep[i].events = tswap32(ep[i].events);
9926 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
9927 }
9928 }
9929 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
9930 break;
9931 }
9932#endif
9933#endif
Peter Maydell163a05a2011-06-27 17:44:52 +01009934#ifdef TARGET_NR_prlimit64
9935 case TARGET_NR_prlimit64:
9936 {
9937 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9938 struct target_rlimit64 *target_rnew, *target_rold;
9939 struct host_rlimit64 rnew, rold, *rnewp = 0;
Felix Janda95018012014-12-02 22:11:17 +01009940 int resource = target_to_host_resource(arg2);
Peter Maydell163a05a2011-06-27 17:44:52 +01009941 if (arg3) {
9942 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
9943 goto efault;
9944 }
9945 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
9946 rnew.rlim_max = tswap64(target_rnew->rlim_max);
9947 unlock_user_struct(target_rnew, arg3, 0);
9948 rnewp = &rnew;
9949 }
9950
Felix Janda95018012014-12-02 22:11:17 +01009951 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
Peter Maydell163a05a2011-06-27 17:44:52 +01009952 if (!is_error(ret) && arg4) {
9953 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
9954 goto efault;
9955 }
9956 target_rold->rlim_cur = tswap64(rold.rlim_cur);
9957 target_rold->rlim_max = tswap64(rold.rlim_max);
9958 unlock_user_struct(target_rold, arg4, 1);
9959 }
9960 break;
9961 }
9962#endif
Richard Henderson3d21d292012-09-15 13:20:46 -07009963#ifdef TARGET_NR_gethostname
9964 case TARGET_NR_gethostname:
9965 {
9966 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
9967 if (name) {
9968 ret = get_errno(gethostname(name, arg2));
9969 unlock_user(name, arg1, arg2);
9970 } else {
9971 ret = -TARGET_EFAULT;
9972 }
9973 break;
9974 }
9975#endif
Riku Voipio89aaf1a2013-07-24 09:44:26 +03009976#ifdef TARGET_NR_atomic_cmpxchg_32
9977 case TARGET_NR_atomic_cmpxchg_32:
9978 {
9979 /* should use start_exclusive from main.c */
9980 abi_ulong mem_value;
9981 if (get_user_u32(mem_value, arg6)) {
9982 target_siginfo_t info;
9983 info.si_signo = SIGSEGV;
9984 info.si_errno = 0;
9985 info.si_code = TARGET_SEGV_MAPERR;
9986 info._sifields._sigfault._addr = arg6;
9987 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
9988 ret = 0xdeadbeef;
9989
9990 }
9991 if (mem_value == arg2)
9992 put_user_u32(arg1, arg6);
9993 ret = mem_value;
9994 break;
9995 }
9996#endif
9997#ifdef TARGET_NR_atomic_barrier
9998 case TARGET_NR_atomic_barrier:
9999 {
10000 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
Peter Maydell3b899ea2014-03-12 13:06:01 +000010001 ret = 0;
Riku Voipio89aaf1a2013-07-24 09:44:26 +030010002 break;
10003 }
10004#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010005
10006#ifdef TARGET_NR_timer_create
10007 case TARGET_NR_timer_create:
10008 {
10009 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
10010
10011 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010012
10013 int clkid = arg1;
10014 int timer_index = next_free_host_timer();
10015
10016 if (timer_index < 0) {
10017 ret = -TARGET_EAGAIN;
10018 } else {
10019 timer_t *phtimer = g_posix_timers + timer_index;
10020
10021 if (arg2) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010022 phost_sevp = &host_sevp;
Peter Maydellc0659762014-08-09 15:42:32 +010010023 ret = target_to_host_sigevent(phost_sevp, arg2);
10024 if (ret != 0) {
10025 break;
10026 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010027 }
10028
10029 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
10030 if (ret) {
10031 phtimer = NULL;
10032 } else {
Alexander Grafaecc8862014-11-10 21:33:03 +010010033 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010034 goto efault;
10035 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010036 }
10037 }
10038 break;
10039 }
10040#endif
10041
10042#ifdef TARGET_NR_timer_settime
10043 case TARGET_NR_timer_settime:
10044 {
10045 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
10046 * struct itimerspec * old_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010010047 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010048
Alexander Grafaecc8862014-11-10 21:33:03 +010010049 if (timerid < 0) {
10050 ret = timerid;
10051 } else if (arg3 == 0) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010052 ret = -TARGET_EINVAL;
10053 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010054 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010055 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
10056
10057 target_to_host_itimerspec(&hspec_new, arg3);
10058 ret = get_errno(
10059 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
10060 host_to_target_itimerspec(arg2, &hspec_old);
10061 }
10062 break;
10063 }
10064#endif
10065
10066#ifdef TARGET_NR_timer_gettime
10067 case TARGET_NR_timer_gettime:
10068 {
10069 /* args: timer_t timerid, struct itimerspec *curr_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010010070 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010071
Alexander Grafaecc8862014-11-10 21:33:03 +010010072 if (timerid < 0) {
10073 ret = timerid;
10074 } else if (!arg2) {
10075 ret = -TARGET_EFAULT;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010076 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010077 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010078 struct itimerspec hspec;
10079 ret = get_errno(timer_gettime(htimer, &hspec));
10080
10081 if (host_to_target_itimerspec(arg2, &hspec)) {
10082 ret = -TARGET_EFAULT;
10083 }
10084 }
10085 break;
10086 }
10087#endif
10088
10089#ifdef TARGET_NR_timer_getoverrun
10090 case TARGET_NR_timer_getoverrun:
10091 {
10092 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010010093 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010094
Alexander Grafaecc8862014-11-10 21:33:03 +010010095 if (timerid < 0) {
10096 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010097 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010098 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010099 ret = get_errno(timer_getoverrun(htimer));
10100 }
Laurent Viviere36800c2015-10-02 14:48:09 +020010101 fd_trans_unregister(ret);
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010102 break;
10103 }
10104#endif
10105
10106#ifdef TARGET_NR_timer_delete
10107 case TARGET_NR_timer_delete:
10108 {
10109 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010010110 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010111
Alexander Grafaecc8862014-11-10 21:33:03 +010010112 if (timerid < 0) {
10113 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010114 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010115 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010116 ret = get_errno(timer_delete(htimer));
Alexander Grafe52a99f2014-08-22 13:56:18 +020010117 g_posix_timers[timerid] = 0;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010118 }
10119 break;
10120 }
10121#endif
10122
Riku Voipio51834342014-06-22 11:25:42 +010010123#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
10124 case TARGET_NR_timerfd_create:
10125 ret = get_errno(timerfd_create(arg1,
10126 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
10127 break;
10128#endif
10129
10130#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
10131 case TARGET_NR_timerfd_gettime:
10132 {
10133 struct itimerspec its_curr;
10134
10135 ret = get_errno(timerfd_gettime(arg1, &its_curr));
10136
10137 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
10138 goto efault;
10139 }
10140 }
10141 break;
10142#endif
10143
10144#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
10145 case TARGET_NR_timerfd_settime:
10146 {
10147 struct itimerspec its_new, its_old, *p_new;
10148
10149 if (arg3) {
10150 if (target_to_host_itimerspec(&its_new, arg3)) {
10151 goto efault;
10152 }
10153 p_new = &its_new;
10154 } else {
10155 p_new = NULL;
10156 }
10157
10158 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
10159
10160 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
10161 goto efault;
10162 }
10163 }
10164 break;
10165#endif
10166
Paul Burtonab31cda2014-06-22 11:25:43 +010010167#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
10168 case TARGET_NR_ioprio_get:
10169 ret = get_errno(ioprio_get(arg1, arg2));
10170 break;
10171#endif
10172
10173#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
10174 case TARGET_NR_ioprio_set:
10175 ret = get_errno(ioprio_set(arg1, arg2, arg3));
10176 break;
10177#endif
10178
Riku Voipio9af5c902014-08-12 15:58:57 +030010179#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
10180 case TARGET_NR_setns:
10181 ret = get_errno(setns(arg1, arg2));
10182 break;
10183#endif
10184#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
10185 case TARGET_NR_unshare:
10186 ret = get_errno(unshare(arg1));
10187 break;
10188#endif
10189
bellard31e31b82003-02-18 22:55:36 +000010190 default:
10191 unimplemented:
bellard5cd43932003-03-29 16:54:36 +000010192 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +000010193#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 +000010194 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +000010195#endif
ths0da46a62007-10-20 20:23:07 +000010196 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +000010197 break;
10198 }
bellard579a97f2007-11-11 14:26:47 +000010199fail:
bellardc573ff62004-01-04 15:51:36 +000010200#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +000010201 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +000010202#endif
thsb92c47c2007-11-01 00:07:38 +000010203 if(do_strace)
10204 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +000010205 return ret;
bellard579a97f2007-11-11 14:26:47 +000010206efault:
10207 ret = -TARGET_EFAULT;
10208 goto fail;
bellard31e31b82003-02-18 22:55:36 +000010209}