blob: b6a8ed6c7ab1f2a4c2ad8f92ec8609aca421382a [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
Peter Maydelld39594e2016-01-26 18:17:02 +000020#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020021#include "qemu/cutils.h"
22#include "qemu/path.h"
bellard31e31b82003-02-18 22:55:36 +000023#include <elf.h>
24#include <endian.h>
John Spencerc56dc772012-12-10 07:59:46 +010025#include <grp.h>
thsd08d3bb2007-03-19 13:09:22 +000026#include <sys/ipc.h>
27#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000028#include <sys/wait.h>
bellard31e31b82003-02-18 22:55:36 +000029#include <sys/mount.h>
John Spencer586b0be2012-12-26 00:49:49 +010030#include <sys/file.h>
31#include <sys/fsuid.h>
32#include <sys/personality.h>
ths39b9aae2007-02-11 18:36:44 +000033#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000034#include <sys/resource.h>
35#include <sys/mman.h>
36#include <sys/swap.h>
Peter Maydelle0eb2102014-03-17 12:15:35 +000037#include <linux/capability.h>
bellard31e31b82003-02-18 22:55:36 +000038#include <sched.h>
Aurelien Jarno60e99242010-03-29 02:12:51 +020039#ifdef __ia64__
40int __clone2(int (*fn)(void *), void *child_stack_base,
41 size_t stack_size, int flags, void *arg, ...);
42#endif
bellard31e31b82003-02-18 22:55:36 +000043#include <sys/socket.h>
aurel32607175e2009-04-15 16:11:59 +000044#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000045#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000046#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000047#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000048#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000049#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000050#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000051#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000052#include <sys/sysinfo.h>
Laurent Viviere36800c2015-10-02 14:48:09 +020053#include <sys/signalfd.h>
bellard72f03902003-02-18 23:33:18 +000054//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000055#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000056#include <netinet/tcp.h>
Laurent Vivier86fcd942011-03-30 01:35:23 +020057#include <linux/wireless.h>
Jing Huang920394d2012-07-24 13:59:23 +000058#include <linux/icmp.h>
Stefan Weil5a61cb62011-09-08 17:55:32 +020059#include "qemu-common.h"
Riku Voipiod80a1902014-10-01 16:05:46 +030060#ifdef CONFIG_TIMERFD
61#include <sys/timerfd.h>
62#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +020063#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +000064#include <sys/gmon.h>
65#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030066#ifdef CONFIG_EVENTFD
67#include <sys/eventfd.h>
68#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000069#ifdef CONFIG_EPOLL
70#include <sys/epoll.h>
71#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070072#ifdef CONFIG_ATTR
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010073#include "qemu/xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070074#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +000075#ifdef CONFIG_SENDFILE
76#include <sys/sendfile.h>
77#endif
bellard31e31b82003-02-18 22:55:36 +000078
79#define termios host_termios
80#define winsize host_winsize
81#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000082#define sgttyb host_sgttyb /* same as target */
83#define tchars host_tchars /* same as target */
84#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000085
86#include <linux/termios.h>
87#include <linux/unistd.h>
bellard31e31b82003-02-18 22:55:36 +000088#include <linux/cdrom.h>
89#include <linux/hdreg.h>
90#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000091#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000092#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +030093#include <linux/fs.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000094#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +000095#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +000096#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +020097#include <linux/fb.h>
98#include <linux/vt.h>
Alexander Graf56e904e2012-01-31 18:42:06 +010099#include <linux/dm-ioctl.h>
Laurent Vivierc07ecc62013-01-07 11:40:06 +0000100#include <linux/reboot.h>
Laurent Vivier7ff7b662013-07-02 14:04:12 +0100101#include <linux/route.h>
Laurent Vivierf57d4192013-08-30 01:46:41 +0200102#include <linux/filter.h>
Andreas Färberfff8c532014-01-18 07:38:30 +0100103#include <linux/blkpg.h>
pbrookd7e40362008-05-23 16:06:43 +0000104#include "linux_loop.h"
Riku Voipio18cb0082014-02-19 12:59:58 +0200105#include "uname.h"
bellard31e31b82003-02-18 22:55:36 +0000106
bellard3ef693a2003-03-23 20:17:16 +0000107#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000108
pbrookd865bab2008-06-07 22:12:17 +0000109#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
110 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
pbrook30813ce2008-06-02 15:45:44 +0000111
bellard72f03902003-02-18 23:33:18 +0000112//#define DEBUG
Timothy E Baldwin71a8f7f2016-05-12 18:47:45 +0100113/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
114 * once. This exercises the codepaths for restart.
115 */
116//#define DEBUG_ERESTARTSYS
bellard31e31b82003-02-18 22:55:36 +0000117
bellard1a9353d2003-03-16 20:28:50 +0000118//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000119#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
120#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000121
bellard70a194b2003-08-11 22:20:16 +0000122
bellard70a194b2003-08-11 22:20:16 +0000123#undef _syscall0
124#undef _syscall1
125#undef _syscall2
126#undef _syscall3
127#undef _syscall4
128#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000129#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000130
bellard83fcb512006-06-14 13:37:16 +0000131#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000132static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000133{ \
134 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000135}
136
bellard83fcb512006-06-14 13:37:16 +0000137#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000138static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000139{ \
140 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000141}
142
bellard83fcb512006-06-14 13:37:16 +0000143#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000144static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000145{ \
146 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000147}
148
bellard83fcb512006-06-14 13:37:16 +0000149#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000150static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000151{ \
152 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000153}
154
bellard83fcb512006-06-14 13:37:16 +0000155#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000156static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000157{ \
158 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000159}
160
bellard83fcb512006-06-14 13:37:16 +0000161#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
162 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000163static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000164{ \
165 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000166}
bellard83fcb512006-06-14 13:37:16 +0000167
168
169#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
170 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000171static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
172 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000173{ \
174 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
175}
176
bellard70a194b2003-08-11 22:20:16 +0000177
bellard31e31b82003-02-18 22:55:36 +0000178#define __NR_sys_uname __NR_uname
bellard72f03902003-02-18 23:33:18 +0000179#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000180#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000181#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000182#define __NR_sys_getpriority __NR_getpriority
bellard66fb9762003-03-23 01:06:05 +0000183#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
ths7494b0f2007-02-11 18:26:53 +0000184#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000185#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000186#define __NR_sys_tkill __NR_tkill
pbrookbd0c5662008-05-29 14:34:11 +0000187#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000188#define __NR_sys_inotify_init __NR_inotify_init
189#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
190#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000191
Alexander Graf42a39fb2011-04-15 17:32:45 +0200192#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
193 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000194#define __NR__llseek __NR_lseek
195#endif
196
James Hogana29e5ba2014-03-25 21:51:08 +0000197/* Newer kernel ports have llseek() instead of _llseek() */
198#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
199#define TARGET_NR__llseek TARGET_NR_llseek
200#endif
201
bellard72f03902003-02-18 23:33:18 +0000202#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000203_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000204#else
ths0da46a62007-10-20 20:23:07 +0000205/* This is a replacement for the host gettid() and must return a host
206 errno. */
bellard72f03902003-02-18 23:33:18 +0000207static int gettid(void) {
208 return -ENOSYS;
209}
210#endif
Chen Gang704eff62015-08-21 05:37:33 +0800211#if defined(TARGET_NR_getdents) && defined(__NR_getdents)
aurel323b3f24a2009-04-15 16:12:13 +0000212_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
Peter Maydell3307e232013-06-12 16:20:21 +0100213#endif
214#if !defined(__NR_getdents) || \
215 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
aurel323b3f24a2009-04-15 16:12:13 +0000216_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
217#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700218#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000219_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
220 loff_t *, res, uint, wh);
221#endif
222_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
223_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
224#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
225_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
226#endif
227#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
228_syscall2(int,sys_tkill,int,tid,int,sig)
229#endif
230#ifdef __NR_exit_group
231_syscall1(int,exit_group,int,error_code)
232#endif
233#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234_syscall1(int,set_tid_address,int *,tidptr)
235#endif
aurel323b3f24a2009-04-15 16:12:13 +0000236#if defined(TARGET_NR_futex) && defined(__NR_futex)
237_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
238 const struct timespec *,timeout,int *,uaddr2,int,val3)
239#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500240#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
241_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
242 unsigned long *, user_mask_ptr);
243#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
244_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
245 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200246_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
247 void *, arg);
Peter Maydelle0eb2102014-03-17 12:15:35 +0000248_syscall2(int, capget, struct __user_cap_header_struct *, header,
249 struct __user_cap_data_struct *, data);
250_syscall2(int, capset, struct __user_cap_header_struct *, header,
251 struct __user_cap_data_struct *, data);
Paul Burtonab31cda2014-06-22 11:25:43 +0100252#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
253_syscall2(int, ioprio_get, int, which, int, who)
254#endif
255#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
256_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
257#endif
Laurent Vivierf894efd2016-02-21 10:56:23 +0100258#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
259_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
260#endif
aurel323b3f24a2009-04-15 16:12:13 +0000261
262static bitmask_transtbl fcntl_flags_tbl[] = {
263 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
264 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
265 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
266 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
267 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
268 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
269 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
270 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700271 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000272 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
273 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
274 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
275 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000276#if defined(O_DIRECT)
277 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
278#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700279#if defined(O_NOATIME)
280 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
281#endif
282#if defined(O_CLOEXEC)
283 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
284#endif
285#if defined(O_PATH)
286 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
287#endif
288 /* Don't terminate the list prematurely on 64-bit host+guest. */
289#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
290 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
291#endif
aurel323b3f24a2009-04-15 16:12:13 +0000292 { 0, 0, 0, 0 }
293};
294
Laurent Vivier5d4d3662015-10-28 21:40:43 +0100295typedef abi_long (*TargetFdDataFunc)(void *, size_t);
Laurent Vivier7b36f782015-10-28 21:40:44 +0100296typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
Laurent Viviere36800c2015-10-02 14:48:09 +0200297typedef struct TargetFdTrans {
Laurent Vivier5d4d3662015-10-28 21:40:43 +0100298 TargetFdDataFunc host_to_target_data;
299 TargetFdDataFunc target_to_host_data;
Laurent Vivier7b36f782015-10-28 21:40:44 +0100300 TargetFdAddrFunc target_to_host_addr;
Laurent Viviere36800c2015-10-02 14:48:09 +0200301} TargetFdTrans;
302
303static TargetFdTrans **target_fd_trans;
304
305static unsigned int target_fd_max;
306
Laurent Vivier5d4d3662015-10-28 21:40:43 +0100307static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
Laurent Viviere36800c2015-10-02 14:48:09 +0200308{
Laurent Vivier861d72c2015-10-28 21:40:46 +0100309 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
Laurent Vivier5d4d3662015-10-28 21:40:43 +0100310 return target_fd_trans[fd]->host_to_target_data;
Laurent Viviere36800c2015-10-02 14:48:09 +0200311 }
312 return NULL;
313}
314
Laurent Vivier7b36f782015-10-28 21:40:44 +0100315static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
316{
Laurent Vivier861d72c2015-10-28 21:40:46 +0100317 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
Laurent Vivier7b36f782015-10-28 21:40:44 +0100318 return target_fd_trans[fd]->target_to_host_addr;
319 }
320 return NULL;
321}
322
Laurent Viviere36800c2015-10-02 14:48:09 +0200323static void fd_trans_register(int fd, TargetFdTrans *trans)
324{
325 unsigned int oldmax;
326
327 if (fd >= target_fd_max) {
328 oldmax = target_fd_max;
329 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
Laurent Vivier5089c7c2016-01-18 23:50:45 +0100330 target_fd_trans = g_renew(TargetFdTrans *,
331 target_fd_trans, target_fd_max);
Laurent Viviere36800c2015-10-02 14:48:09 +0200332 memset((void *)(target_fd_trans + oldmax), 0,
333 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
334 }
335 target_fd_trans[fd] = trans;
336}
337
338static void fd_trans_unregister(int fd)
339{
340 if (fd >= 0 && fd < target_fd_max) {
341 target_fd_trans[fd] = NULL;
342 }
343}
344
345static void fd_trans_dup(int oldfd, int newfd)
346{
347 fd_trans_unregister(newfd);
348 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
349 fd_trans_register(newfd, target_fd_trans[oldfd]);
350 }
351}
352
aurel323b3f24a2009-04-15 16:12:13 +0000353static int sys_getcwd1(char *buf, size_t size)
354{
355 if (getcwd(buf, size) == NULL) {
356 /* getcwd() sets errno */
357 return (-1);
358 }
aurel32aaf4ad32009-04-16 14:17:14 +0000359 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000360}
361
Alexander Graff4c69012011-09-25 06:25:35 +0200362static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000363{
364 /*
365 * open(2) has extra parameter 'mode' when called with
366 * flag O_CREAT.
367 */
368 if ((flags & O_CREAT) != 0) {
aurel323b3f24a2009-04-15 16:12:13 +0000369 return (openat(dirfd, pathname, flags, mode));
370 }
371 return (openat(dirfd, pathname, flags));
372}
Riku Voipioebc996f2009-04-21 15:01:51 +0300373
Peter Maydell1acae9f2013-07-02 14:04:12 +0100374#ifdef TARGET_NR_utimensat
Riku Voipioebc996f2009-04-21 15:01:51 +0300375#ifdef CONFIG_UTIMENSAT
376static int sys_utimensat(int dirfd, const char *pathname,
377 const struct timespec times[2], int flags)
378{
379 if (pathname == NULL)
380 return futimens(dirfd, times);
381 else
382 return utimensat(dirfd, pathname, times, flags);
383}
Peter Maydell1acae9f2013-07-02 14:04:12 +0100384#elif defined(__NR_utimensat)
385#define __NR_sys_utimensat __NR_utimensat
ths9007f0e2007-09-25 17:50:37 +0000386_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
387 const struct timespec *,tsp,int,flags)
Peter Maydell1acae9f2013-07-02 14:04:12 +0100388#else
389static int sys_utimensat(int dirfd, const char *pathname,
390 const struct timespec times[2], int flags)
391{
392 errno = ENOSYS;
393 return -1;
394}
ths9007f0e2007-09-25 17:50:37 +0000395#endif
Peter Maydell1acae9f2013-07-02 14:04:12 +0100396#endif /* TARGET_NR_utimensat */
aurel323b3f24a2009-04-15 16:12:13 +0000397
398#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000399#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000400
aurel3239b59762008-10-01 21:46:50 +0000401#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000402static int sys_inotify_init(void)
403{
404 return (inotify_init());
405}
aurel3239b59762008-10-01 21:46:50 +0000406#endif
407#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000408static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
409{
410 return (inotify_add_watch(fd, pathname, mask));
411}
aurel3239b59762008-10-01 21:46:50 +0000412#endif
413#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000414static int sys_inotify_rm_watch(int fd, int32_t wd)
415{
aurel328690e422009-04-17 13:50:32 +0000416 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000417}
aurel3239b59762008-10-01 21:46:50 +0000418#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000419#ifdef CONFIG_INOTIFY1
420#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
421static int sys_inotify_init1(int flags)
422{
423 return (inotify_init1(flags));
424}
425#endif
426#endif
aurel323b3f24a2009-04-15 16:12:13 +0000427#else
428/* Userspace can usually survive runtime without inotify */
429#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000430#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000431#undef TARGET_NR_inotify_add_watch
432#undef TARGET_NR_inotify_rm_watch
433#endif /* CONFIG_INOTIFY */
434
Mike Frysingerd8035d42011-02-07 01:05:51 -0500435#if defined(TARGET_NR_ppoll)
436#ifndef __NR_ppoll
437# define __NR_ppoll -1
438#endif
439#define __NR_sys_ppoll __NR_ppoll
440_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
Natanael Copa34d60862014-04-29 13:11:20 +0200441 struct timespec *, timeout, const sigset_t *, sigmask,
Mike Frysingerd8035d42011-02-07 01:05:51 -0500442 size_t, sigsetsize)
443#endif
bellard66fb9762003-03-23 01:06:05 +0000444
Mike Frysinger055e0902011-06-03 17:01:49 -0400445#if defined(TARGET_NR_pselect6)
446#ifndef __NR_pselect6
447# define __NR_pselect6 -1
448#endif
449#define __NR_sys_pselect6 __NR_pselect6
450_syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
451 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
452#endif
453
Peter Maydell163a05a2011-06-27 17:44:52 +0100454#if defined(TARGET_NR_prlimit64)
455#ifndef __NR_prlimit64
456# define __NR_prlimit64 -1
457#endif
458#define __NR_sys_prlimit64 __NR_prlimit64
459/* The glibc rlimit structure may not be that used by the underlying syscall */
460struct host_rlimit64 {
461 uint64_t rlim_cur;
462 uint64_t rlim_max;
463};
464_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
465 const struct host_rlimit64 *, new_limit,
466 struct host_rlimit64 *, old_limit)
467#endif
468
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100469
470#if defined(TARGET_NR_timer_create)
471/* Maxiumum of 32 active POSIX timers allowed at any one time. */
472static timer_t g_posix_timers[32] = { 0, } ;
473
474static inline int next_free_host_timer(void)
475{
476 int k ;
477 /* FIXME: Does finding the next free slot require a lock? */
478 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
479 if (g_posix_timers[k] == 0) {
480 g_posix_timers[k] = (timer_t) 1;
481 return k;
482 }
483 }
484 return -1;
485}
486#endif
487
Riku Voipio48e515d2011-07-12 15:40:51 +0300488/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
Alexander Graf4a1def42012-09-29 15:32:38 +0000489#ifdef TARGET_ARM
Riku Voipio48e515d2011-07-12 15:40:51 +0300490static inline int regpairs_aligned(void *cpu_env) {
491 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
492}
493#elif defined(TARGET_MIPS)
494static inline int regpairs_aligned(void *cpu_env) { return 1; }
Alexander Graf4a1def42012-09-29 15:32:38 +0000495#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
496/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
497 * of registers which translates to the same as ARM/MIPS, because we start with
498 * r3 as arg1 */
499static inline int regpairs_aligned(void *cpu_env) { return 1; }
Riku Voipio48e515d2011-07-12 15:40:51 +0300500#else
501static inline int regpairs_aligned(void *cpu_env) { return 0; }
502#endif
503
thsb92c47c2007-11-01 00:07:38 +0000504#define ERRNO_TABLE_SIZE 1200
505
506/* target_to_host_errno_table[] is initialized from
507 * host_to_target_errno_table[] in syscall_init(). */
508static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
509};
510
ths637947f2007-06-01 12:09:19 +0000511/*
thsfe8f0962007-07-12 10:59:21 +0000512 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000513 * minus the errnos that are not actually generic to all archs.
514 */
thsb92c47c2007-11-01 00:07:38 +0000515static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
Chen Gang08703b92015-09-07 10:35:06 +0800516 [EAGAIN] = TARGET_EAGAIN,
ths637947f2007-06-01 12:09:19 +0000517 [EIDRM] = TARGET_EIDRM,
518 [ECHRNG] = TARGET_ECHRNG,
519 [EL2NSYNC] = TARGET_EL2NSYNC,
520 [EL3HLT] = TARGET_EL3HLT,
521 [EL3RST] = TARGET_EL3RST,
522 [ELNRNG] = TARGET_ELNRNG,
523 [EUNATCH] = TARGET_EUNATCH,
524 [ENOCSI] = TARGET_ENOCSI,
525 [EL2HLT] = TARGET_EL2HLT,
526 [EDEADLK] = TARGET_EDEADLK,
527 [ENOLCK] = TARGET_ENOLCK,
528 [EBADE] = TARGET_EBADE,
529 [EBADR] = TARGET_EBADR,
530 [EXFULL] = TARGET_EXFULL,
531 [ENOANO] = TARGET_ENOANO,
532 [EBADRQC] = TARGET_EBADRQC,
533 [EBADSLT] = TARGET_EBADSLT,
534 [EBFONT] = TARGET_EBFONT,
535 [ENOSTR] = TARGET_ENOSTR,
536 [ENODATA] = TARGET_ENODATA,
537 [ETIME] = TARGET_ETIME,
538 [ENOSR] = TARGET_ENOSR,
539 [ENONET] = TARGET_ENONET,
540 [ENOPKG] = TARGET_ENOPKG,
541 [EREMOTE] = TARGET_EREMOTE,
542 [ENOLINK] = TARGET_ENOLINK,
543 [EADV] = TARGET_EADV,
544 [ESRMNT] = TARGET_ESRMNT,
545 [ECOMM] = TARGET_ECOMM,
546 [EPROTO] = TARGET_EPROTO,
547 [EDOTDOT] = TARGET_EDOTDOT,
548 [EMULTIHOP] = TARGET_EMULTIHOP,
549 [EBADMSG] = TARGET_EBADMSG,
550 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
551 [EOVERFLOW] = TARGET_EOVERFLOW,
552 [ENOTUNIQ] = TARGET_ENOTUNIQ,
553 [EBADFD] = TARGET_EBADFD,
554 [EREMCHG] = TARGET_EREMCHG,
555 [ELIBACC] = TARGET_ELIBACC,
556 [ELIBBAD] = TARGET_ELIBBAD,
557 [ELIBSCN] = TARGET_ELIBSCN,
558 [ELIBMAX] = TARGET_ELIBMAX,
559 [ELIBEXEC] = TARGET_ELIBEXEC,
560 [EILSEQ] = TARGET_EILSEQ,
561 [ENOSYS] = TARGET_ENOSYS,
562 [ELOOP] = TARGET_ELOOP,
563 [ERESTART] = TARGET_ERESTART,
564 [ESTRPIPE] = TARGET_ESTRPIPE,
565 [ENOTEMPTY] = TARGET_ENOTEMPTY,
566 [EUSERS] = TARGET_EUSERS,
567 [ENOTSOCK] = TARGET_ENOTSOCK,
568 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
569 [EMSGSIZE] = TARGET_EMSGSIZE,
570 [EPROTOTYPE] = TARGET_EPROTOTYPE,
571 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
572 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
573 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
574 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
575 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
576 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
577 [EADDRINUSE] = TARGET_EADDRINUSE,
578 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
579 [ENETDOWN] = TARGET_ENETDOWN,
580 [ENETUNREACH] = TARGET_ENETUNREACH,
581 [ENETRESET] = TARGET_ENETRESET,
582 [ECONNABORTED] = TARGET_ECONNABORTED,
583 [ECONNRESET] = TARGET_ECONNRESET,
584 [ENOBUFS] = TARGET_ENOBUFS,
585 [EISCONN] = TARGET_EISCONN,
586 [ENOTCONN] = TARGET_ENOTCONN,
587 [EUCLEAN] = TARGET_EUCLEAN,
588 [ENOTNAM] = TARGET_ENOTNAM,
589 [ENAVAIL] = TARGET_ENAVAIL,
590 [EISNAM] = TARGET_EISNAM,
591 [EREMOTEIO] = TARGET_EREMOTEIO,
592 [ESHUTDOWN] = TARGET_ESHUTDOWN,
593 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
594 [ETIMEDOUT] = TARGET_ETIMEDOUT,
595 [ECONNREFUSED] = TARGET_ECONNREFUSED,
596 [EHOSTDOWN] = TARGET_EHOSTDOWN,
597 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
598 [EALREADY] = TARGET_EALREADY,
599 [EINPROGRESS] = TARGET_EINPROGRESS,
600 [ESTALE] = TARGET_ESTALE,
601 [ECANCELED] = TARGET_ECANCELED,
602 [ENOMEDIUM] = TARGET_ENOMEDIUM,
603 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000604#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000605 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000606#endif
607#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000608 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000609#endif
610#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000611 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000612#endif
613#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000614 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000615#endif
616#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000617 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000618#endif
619#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000620 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000621#endif
thsb92c47c2007-11-01 00:07:38 +0000622};
ths637947f2007-06-01 12:09:19 +0000623
624static inline int host_to_target_errno(int err)
625{
Timothy E Baldwin24661192016-05-12 18:47:25 +0100626 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
627 host_to_target_errno_table[err]) {
ths637947f2007-06-01 12:09:19 +0000628 return host_to_target_errno_table[err];
Timothy E Baldwin24661192016-05-12 18:47:25 +0100629 }
ths637947f2007-06-01 12:09:19 +0000630 return err;
631}
632
thsb92c47c2007-11-01 00:07:38 +0000633static inline int target_to_host_errno(int err)
634{
Timothy E Baldwin24661192016-05-12 18:47:25 +0100635 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
636 target_to_host_errno_table[err]) {
thsb92c47c2007-11-01 00:07:38 +0000637 return target_to_host_errno_table[err];
Timothy E Baldwin24661192016-05-12 18:47:25 +0100638 }
thsb92c47c2007-11-01 00:07:38 +0000639 return err;
640}
641
blueswir1992f48a2007-10-14 16:27:31 +0000642static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000643{
644 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000645 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000646 else
647 return ret;
648}
649
blueswir1992f48a2007-10-14 16:27:31 +0000650static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000651{
blueswir1992f48a2007-10-14 16:27:31 +0000652 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000653}
654
thsb92c47c2007-11-01 00:07:38 +0000655char *target_strerror(int err)
656{
Alexander Graf962b2892011-11-21 12:04:07 +0100657 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
658 return NULL;
659 }
thsb92c47c2007-11-01 00:07:38 +0000660 return strerror(target_to_host_errno(err));
661}
662
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +0100663#define safe_syscall0(type, name) \
664static type safe_##name(void) \
665{ \
666 return safe_syscall(__NR_##name); \
667}
668
669#define safe_syscall1(type, name, type1, arg1) \
670static type safe_##name(type1 arg1) \
671{ \
672 return safe_syscall(__NR_##name, arg1); \
673}
674
675#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
676static type safe_##name(type1 arg1, type2 arg2) \
677{ \
678 return safe_syscall(__NR_##name, arg1, arg2); \
679}
680
681#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
682static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
683{ \
684 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
685}
686
687#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
688 type4, arg4) \
689static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
690{ \
691 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
692}
693
694#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
695 type4, arg4, type5, arg5) \
696static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
697 type5 arg5) \
698{ \
699 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
700}
701
702#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
703 type4, arg4, type5, arg5, type6, arg6) \
704static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
705 type5 arg5, type6 arg6) \
706{ \
707 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
708}
709
Paul Burton8289d112014-06-22 11:25:33 +0100710static inline int host_to_target_sock_type(int host_type)
711{
712 int target_type;
713
714 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
715 case SOCK_DGRAM:
716 target_type = TARGET_SOCK_DGRAM;
717 break;
718 case SOCK_STREAM:
719 target_type = TARGET_SOCK_STREAM;
720 break;
721 default:
722 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
723 break;
724 }
725
726#if defined(SOCK_CLOEXEC)
727 if (host_type & SOCK_CLOEXEC) {
728 target_type |= TARGET_SOCK_CLOEXEC;
729 }
730#endif
731
732#if defined(SOCK_NONBLOCK)
733 if (host_type & SOCK_NONBLOCK) {
734 target_type |= TARGET_SOCK_NONBLOCK;
735 }
736#endif
737
738 return target_type;
739}
740
blueswir1992f48a2007-10-14 16:27:31 +0000741static abi_ulong target_brk;
742static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000743static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000744
blueswir1992f48a2007-10-14 16:27:31 +0000745void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000746{
blueswir14c1de732007-07-07 20:45:44 +0000747 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000748 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000749}
750
vincent4d1de872011-06-14 21:56:33 +0000751//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
752#define DEBUGF_BRK(message, args...)
753
ths0da46a62007-10-20 20:23:07 +0000754/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000755abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000756{
blueswir1992f48a2007-10-14 16:27:31 +0000757 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000758 int new_alloc_size;
759
Paul Brook3a0c6c42012-02-09 19:04:27 +0000760 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000761
vincent4d1de872011-06-14 21:56:33 +0000762 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000763 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000764 return target_brk;
765 }
766 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000767 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
768 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000769 return target_brk;
770 }
bellard31e31b82003-02-18 22:55:36 +0000771
vincent4d1de872011-06-14 21:56:33 +0000772 /* If the new brk is less than the highest page reserved to the
773 * target heap allocation, set it and we're almost done... */
774 if (new_brk <= brk_page) {
775 /* Heap contents are initialized to zero, as for anonymous
776 * mapped pages. */
777 if (new_brk > target_brk) {
778 memset(g2h(target_brk), 0, new_brk - target_brk);
779 }
bellard31e31b82003-02-18 22:55:36 +0000780 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000781 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000782 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000783 }
784
Peter Maydell00faf082011-04-18 16:34:24 +0100785 /* We need to allocate more memory after the brk... Note that
786 * we don't use MAP_FIXED because that will map over the top of
787 * any existing mapping (like the one with the host libc or qemu
788 * itself); instead we treat "mapped but at wrong address" as
789 * a failure and unmap again.
790 */
vincent4d1de872011-06-14 21:56:33 +0000791 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000792 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000793 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100794 MAP_ANON|MAP_PRIVATE, 0, 0));
795
796 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200797 /* Heap contents are initialized to zero, as for anonymous
798 * mapped pages. Technically the new pages are already
799 * initialized to zero since they *are* anonymous mapped
800 * pages, however we have to take care with the contents that
801 * come from the remaining part of the previous page: it may
802 * contains garbage data due to a previous heap usage (grown
803 * then shrunken). */
804 memset(g2h(target_brk), 0, brk_page - target_brk);
805
Peter Maydell00faf082011-04-18 16:34:24 +0100806 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000807 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000808 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
809 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100810 return target_brk;
811 } else if (mapped_addr != -1) {
812 /* Mapped but at wrong address, meaning there wasn't actually
813 * enough space for this brk.
814 */
815 target_munmap(mapped_addr, new_alloc_size);
816 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000817 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000818 }
819 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000820 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100821 }
balrog7ab240a2008-04-26 12:17:34 +0000822
Richard Henderson7dd46c02010-05-03 10:07:49 -0700823#if defined(TARGET_ALPHA)
824 /* We (partially) emulate OSF/1 on Alpha, which requires we
825 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100826 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700827#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100828 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000829 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000830}
831
ths26edcf42007-12-09 02:25:24 +0000832static inline abi_long copy_from_user_fdset(fd_set *fds,
833 abi_ulong target_fds_addr,
834 int n)
bellard31e31b82003-02-18 22:55:36 +0000835{
ths26edcf42007-12-09 02:25:24 +0000836 int i, nw, j, k;
837 abi_ulong b, *target_fds;
838
839 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
840 if (!(target_fds = lock_user(VERIFY_READ,
841 target_fds_addr,
842 sizeof(abi_ulong) * nw,
843 1)))
844 return -TARGET_EFAULT;
845
846 FD_ZERO(fds);
847 k = 0;
848 for (i = 0; i < nw; i++) {
849 /* grab the abi_ulong */
850 __get_user(b, &target_fds[i]);
851 for (j = 0; j < TARGET_ABI_BITS; j++) {
852 /* check the bit inside the abi_ulong */
853 if ((b >> j) & 1)
854 FD_SET(k, fds);
855 k++;
bellard31e31b82003-02-18 22:55:36 +0000856 }
bellard31e31b82003-02-18 22:55:36 +0000857 }
ths26edcf42007-12-09 02:25:24 +0000858
859 unlock_user(target_fds, target_fds_addr, 0);
860
861 return 0;
bellard31e31b82003-02-18 22:55:36 +0000862}
863
Mike Frysinger055e0902011-06-03 17:01:49 -0400864static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
865 abi_ulong target_fds_addr,
866 int n)
867{
868 if (target_fds_addr) {
869 if (copy_from_user_fdset(fds, target_fds_addr, n))
870 return -TARGET_EFAULT;
871 *fds_ptr = fds;
872 } else {
873 *fds_ptr = NULL;
874 }
875 return 0;
876}
877
ths26edcf42007-12-09 02:25:24 +0000878static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
879 const fd_set *fds,
880 int n)
bellard31e31b82003-02-18 22:55:36 +0000881{
bellard31e31b82003-02-18 22:55:36 +0000882 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000883 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000884 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000885
ths26edcf42007-12-09 02:25:24 +0000886 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
887 if (!(target_fds = lock_user(VERIFY_WRITE,
888 target_fds_addr,
889 sizeof(abi_ulong) * nw,
890 0)))
891 return -TARGET_EFAULT;
892
893 k = 0;
894 for (i = 0; i < nw; i++) {
895 v = 0;
896 for (j = 0; j < TARGET_ABI_BITS; j++) {
Andreas Schwab9ab709b2013-04-09 01:02:34 +0000897 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
ths26edcf42007-12-09 02:25:24 +0000898 k++;
bellard31e31b82003-02-18 22:55:36 +0000899 }
ths26edcf42007-12-09 02:25:24 +0000900 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000901 }
ths26edcf42007-12-09 02:25:24 +0000902
903 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
904
905 return 0;
bellard31e31b82003-02-18 22:55:36 +0000906}
907
bellardc596ed12003-07-13 17:32:31 +0000908#if defined(__alpha__)
909#define HOST_HZ 1024
910#else
911#define HOST_HZ 100
912#endif
913
blueswir1992f48a2007-10-14 16:27:31 +0000914static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000915{
916#if HOST_HZ == TARGET_HZ
917 return ticks;
918#else
919 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
920#endif
921}
922
bellard579a97f2007-11-11 14:26:47 +0000923static inline abi_long host_to_target_rusage(abi_ulong target_addr,
924 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000925{
pbrook53a59602006-03-25 19:31:22 +0000926 struct target_rusage *target_rusage;
927
bellard579a97f2007-11-11 14:26:47 +0000928 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
929 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200930 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
931 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
932 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
933 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
934 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
935 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
936 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
937 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
938 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
939 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
940 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
941 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
942 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
943 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
944 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
945 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
946 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
947 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000948 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000949
950 return 0;
bellardb4091862003-05-16 15:39:34 +0000951}
952
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200953static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900954{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200955 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300956 rlim_t result;
957
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200958 target_rlim_swap = tswapal(target_rlim);
959 if (target_rlim_swap == TARGET_RLIM_INFINITY)
960 return RLIM_INFINITY;
961
962 result = target_rlim_swap;
963 if (target_rlim_swap != (rlim_t)result)
964 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300965
966 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900967}
968
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200969static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900970{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200971 abi_ulong target_rlim_swap;
972 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300973
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200974 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300975 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900976 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300977 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200978 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300979
980 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900981}
982
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +0300983static inline int target_to_host_resource(int code)
984{
985 switch (code) {
986 case TARGET_RLIMIT_AS:
987 return RLIMIT_AS;
988 case TARGET_RLIMIT_CORE:
989 return RLIMIT_CORE;
990 case TARGET_RLIMIT_CPU:
991 return RLIMIT_CPU;
992 case TARGET_RLIMIT_DATA:
993 return RLIMIT_DATA;
994 case TARGET_RLIMIT_FSIZE:
995 return RLIMIT_FSIZE;
996 case TARGET_RLIMIT_LOCKS:
997 return RLIMIT_LOCKS;
998 case TARGET_RLIMIT_MEMLOCK:
999 return RLIMIT_MEMLOCK;
1000 case TARGET_RLIMIT_MSGQUEUE:
1001 return RLIMIT_MSGQUEUE;
1002 case TARGET_RLIMIT_NICE:
1003 return RLIMIT_NICE;
1004 case TARGET_RLIMIT_NOFILE:
1005 return RLIMIT_NOFILE;
1006 case TARGET_RLIMIT_NPROC:
1007 return RLIMIT_NPROC;
1008 case TARGET_RLIMIT_RSS:
1009 return RLIMIT_RSS;
1010 case TARGET_RLIMIT_RTPRIO:
1011 return RLIMIT_RTPRIO;
1012 case TARGET_RLIMIT_SIGPENDING:
1013 return RLIMIT_SIGPENDING;
1014 case TARGET_RLIMIT_STACK:
1015 return RLIMIT_STACK;
1016 default:
1017 return code;
1018 }
1019}
1020
ths788f5ec2007-12-09 02:37:05 +00001021static inline abi_long copy_from_user_timeval(struct timeval *tv,
1022 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001023{
pbrook53a59602006-03-25 19:31:22 +00001024 struct target_timeval *target_tv;
1025
ths788f5ec2007-12-09 02:37:05 +00001026 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +00001027 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001028
1029 __get_user(tv->tv_sec, &target_tv->tv_sec);
1030 __get_user(tv->tv_usec, &target_tv->tv_usec);
1031
1032 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001033
1034 return 0;
bellard31e31b82003-02-18 22:55:36 +00001035}
1036
ths788f5ec2007-12-09 02:37:05 +00001037static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1038 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +00001039{
pbrook53a59602006-03-25 19:31:22 +00001040 struct target_timeval *target_tv;
1041
ths788f5ec2007-12-09 02:37:05 +00001042 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +00001043 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001044
1045 __put_user(tv->tv_sec, &target_tv->tv_sec);
1046 __put_user(tv->tv_usec, &target_tv->tv_usec);
1047
1048 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001049
1050 return 0;
bellard31e31b82003-02-18 22:55:36 +00001051}
1052
Paul Burtonef4467e2014-06-22 11:25:40 +01001053static inline abi_long copy_from_user_timezone(struct timezone *tz,
1054 abi_ulong target_tz_addr)
1055{
1056 struct target_timezone *target_tz;
1057
1058 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1059 return -TARGET_EFAULT;
1060 }
1061
1062 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1063 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1064
1065 unlock_user_struct(target_tz, target_tz_addr, 0);
1066
1067 return 0;
1068}
1069
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001070#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1071#include <mqueue.h>
1072
aurel3224e10032009-04-15 16:11:43 +00001073static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1074 abi_ulong target_mq_attr_addr)
1075{
1076 struct target_mq_attr *target_mq_attr;
1077
1078 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1079 target_mq_attr_addr, 1))
1080 return -TARGET_EFAULT;
1081
1082 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1083 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1084 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1085 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1086
1087 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1088
1089 return 0;
1090}
1091
1092static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1093 const struct mq_attr *attr)
1094{
1095 struct target_mq_attr *target_mq_attr;
1096
1097 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1098 target_mq_attr_addr, 0))
1099 return -TARGET_EFAULT;
1100
1101 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1102 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1103 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1104 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1105
1106 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1107
1108 return 0;
1109}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001110#endif
bellard31e31b82003-02-18 22:55:36 +00001111
Mike Frysinger055e0902011-06-03 17:01:49 -04001112#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001113/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001114static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001115 abi_ulong rfd_addr, abi_ulong wfd_addr,
1116 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001117{
1118 fd_set rfds, wfds, efds;
1119 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1120 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001121 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001122
Mike Frysinger055e0902011-06-03 17:01:49 -04001123 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1124 if (ret) {
1125 return ret;
pbrook53a59602006-03-25 19:31:22 +00001126 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001127 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1128 if (ret) {
1129 return ret;
pbrook53a59602006-03-25 19:31:22 +00001130 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001131 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1132 if (ret) {
1133 return ret;
pbrook53a59602006-03-25 19:31:22 +00001134 }
ths3b46e622007-09-17 08:09:54 +00001135
ths26edcf42007-12-09 02:25:24 +00001136 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001137 if (copy_from_user_timeval(&tv, target_tv_addr))
1138 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001139 tv_ptr = &tv;
1140 } else {
1141 tv_ptr = NULL;
1142 }
ths26edcf42007-12-09 02:25:24 +00001143
bellard31e31b82003-02-18 22:55:36 +00001144 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +00001145
ths26edcf42007-12-09 02:25:24 +00001146 if (!is_error(ret)) {
1147 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1148 return -TARGET_EFAULT;
1149 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1150 return -TARGET_EFAULT;
1151 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1152 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001153
ths788f5ec2007-12-09 02:37:05 +00001154 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1155 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001156 }
bellard579a97f2007-11-11 14:26:47 +00001157
bellard31e31b82003-02-18 22:55:36 +00001158 return ret;
1159}
Mike Frysinger055e0902011-06-03 17:01:49 -04001160#endif
bellard31e31b82003-02-18 22:55:36 +00001161
Riku Voipio099d6b02009-05-05 12:10:04 +03001162static abi_long do_pipe2(int host_pipe[], int flags)
1163{
1164#ifdef CONFIG_PIPE2
1165 return pipe2(host_pipe, flags);
1166#else
1167 return -ENOSYS;
1168#endif
1169}
1170
Richard Hendersonfb41a662010-05-03 10:07:52 -07001171static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1172 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001173{
1174 int host_pipe[2];
1175 abi_long ret;
1176 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1177
1178 if (is_error(ret))
1179 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001180
1181 /* Several targets have special calling conventions for the original
1182 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1183 if (!is_pipe2) {
1184#if defined(TARGET_ALPHA)
1185 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1186 return host_pipe[0];
1187#elif defined(TARGET_MIPS)
1188 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1189 return host_pipe[0];
1190#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001191 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001192 return host_pipe[0];
Peter Maydell82f05b62013-07-06 17:39:48 +01001193#elif defined(TARGET_SPARC)
1194 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1195 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001196#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001197 }
1198
Riku Voipio099d6b02009-05-05 12:10:04 +03001199 if (put_user_s32(host_pipe[0], pipedes)
1200 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1201 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001202 return get_errno(ret);
1203}
1204
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001205static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1206 abi_ulong target_addr,
1207 socklen_t len)
1208{
1209 struct target_ip_mreqn *target_smreqn;
1210
1211 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1212 if (!target_smreqn)
1213 return -TARGET_EFAULT;
1214 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1215 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1216 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001217 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001218 unlock_user(target_smreqn, target_addr, 0);
1219
1220 return 0;
1221}
1222
Laurent Vivier7b36f782015-10-28 21:40:44 +01001223static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
bellard579a97f2007-11-11 14:26:47 +00001224 abi_ulong target_addr,
1225 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001226{
aurel32607175e2009-04-15 16:11:59 +00001227 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1228 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001229 struct target_sockaddr *target_saddr;
1230
Laurent Vivier7b36f782015-10-28 21:40:44 +01001231 if (fd_trans_target_to_host_addr(fd)) {
1232 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1233 }
1234
bellard579a97f2007-11-11 14:26:47 +00001235 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1236 if (!target_saddr)
1237 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001238
1239 sa_family = tswap16(target_saddr->sa_family);
1240
1241 /* Oops. The caller might send a incomplete sun_path; sun_path
1242 * must be terminated by \0 (see the manual page), but
1243 * unfortunately it is quite common to specify sockaddr_un
1244 * length as "strlen(x->sun_path)" while it should be
1245 * "strlen(...) + 1". We'll fix that here if needed.
1246 * Linux kernel has a similar feature.
1247 */
1248
1249 if (sa_family == AF_UNIX) {
1250 if (len < unix_maxlen && len > 0) {
1251 char *cp = (char*)target_saddr;
1252
1253 if ( cp[len-1] && !cp[len] )
1254 len++;
1255 }
1256 if (len > unix_maxlen)
1257 len = unix_maxlen;
1258 }
1259
pbrook53a59602006-03-25 19:31:22 +00001260 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001261 addr->sa_family = sa_family;
Joakim Tjernlund33a29b52014-07-12 15:47:07 +02001262 if (sa_family == AF_PACKET) {
1263 struct target_sockaddr_ll *lladdr;
1264
1265 lladdr = (struct target_sockaddr_ll *)addr;
1266 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1267 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1268 }
pbrook53a59602006-03-25 19:31:22 +00001269 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001270
1271 return 0;
bellard7854b052003-03-29 17:22:23 +00001272}
1273
bellard579a97f2007-11-11 14:26:47 +00001274static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1275 struct sockaddr *addr,
1276 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001277{
pbrook53a59602006-03-25 19:31:22 +00001278 struct target_sockaddr *target_saddr;
1279
bellard579a97f2007-11-11 14:26:47 +00001280 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1281 if (!target_saddr)
1282 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001283 memcpy(target_saddr, addr, len);
1284 target_saddr->sa_family = tswap16(addr->sa_family);
1285 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001286
1287 return 0;
bellard7854b052003-03-29 17:22:23 +00001288}
1289
bellard5a4a8982007-11-11 17:39:18 +00001290static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1291 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001292{
1293 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001294 abi_long msg_controllen;
1295 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001296 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001297 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001298
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001299 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001300 if (msg_controllen < sizeof (struct target_cmsghdr))
1301 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001302 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001303 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001304 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001305 if (!target_cmsg)
1306 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001307
1308 while (cmsg && target_cmsg) {
1309 void *data = CMSG_DATA(cmsg);
1310 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1311
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001312 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001313 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1314
1315 space += CMSG_SPACE(len);
1316 if (space > msgh->msg_controllen) {
1317 space -= CMSG_SPACE(len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001318 /* This is a QEMU bug, since we allocated the payload
1319 * area ourselves (unlike overflow in host-to-target
1320 * conversion, which is just the guest giving us a buffer
1321 * that's too small). It can't happen for the payload types
1322 * we currently support; if it becomes an issue in future
1323 * we would need to improve our allocation strategy to
1324 * something more intelligent than "twice the size of the
1325 * target buffer we're reading from".
1326 */
bellard31febb72005-12-18 20:03:27 +00001327 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001328 break;
1329 }
1330
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001331 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1332 cmsg->cmsg_level = SOL_SOCKET;
1333 } else {
1334 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1335 }
bellard7854b052003-03-29 17:22:23 +00001336 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1337 cmsg->cmsg_len = CMSG_LEN(len);
1338
Alex Suykov30b8b682014-12-23 07:52:58 +02001339 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001340 int *fd = (int *)data;
1341 int *target_fd = (int *)target_data;
1342 int i, numfds = len / sizeof(int);
1343
Peter Maydell876e23c2015-05-26 19:46:32 +01001344 for (i = 0; i < numfds; i++) {
1345 __get_user(fd[i], target_fd + i);
1346 }
Alex Suykov30b8b682014-12-23 07:52:58 +02001347 } else if (cmsg->cmsg_level == SOL_SOCKET
1348 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1349 struct ucred *cred = (struct ucred *)data;
1350 struct target_ucred *target_cred =
1351 (struct target_ucred *)target_data;
1352
Peter Maydell876e23c2015-05-26 19:46:32 +01001353 __get_user(cred->pid, &target_cred->pid);
1354 __get_user(cred->uid, &target_cred->uid);
1355 __get_user(cred->gid, &target_cred->gid);
Alex Suykov30b8b682014-12-23 07:52:58 +02001356 } else {
1357 gemu_log("Unsupported ancillary data: %d/%d\n",
1358 cmsg->cmsg_level, cmsg->cmsg_type);
1359 memcpy(data, target_data, len);
bellard7854b052003-03-29 17:22:23 +00001360 }
1361
1362 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001363 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1364 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00001365 }
bellard5a4a8982007-11-11 17:39:18 +00001366 unlock_user(target_cmsg, target_cmsg_addr, 0);
1367 the_end:
bellard7854b052003-03-29 17:22:23 +00001368 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001369 return 0;
bellard7854b052003-03-29 17:22:23 +00001370}
1371
bellard5a4a8982007-11-11 17:39:18 +00001372static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1373 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001374{
1375 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001376 abi_long msg_controllen;
1377 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001378 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001379 socklen_t space = 0;
1380
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001381 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001382 if (msg_controllen < sizeof (struct target_cmsghdr))
1383 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001384 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001385 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001386 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001387 if (!target_cmsg)
1388 return -TARGET_EFAULT;
1389
bellard7854b052003-03-29 17:22:23 +00001390 while (cmsg && target_cmsg) {
1391 void *data = CMSG_DATA(cmsg);
1392 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1393
1394 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
Peter Maydellc2aeb252015-05-26 19:46:31 +01001395 int tgt_len, tgt_space;
bellard7854b052003-03-29 17:22:23 +00001396
Peter Maydellc2aeb252015-05-26 19:46:31 +01001397 /* We never copy a half-header but may copy half-data;
1398 * this is Linux's behaviour in put_cmsg(). Note that
1399 * truncation here is a guest problem (which we report
1400 * to the guest via the CTRUNC bit), unlike truncation
1401 * in target_to_host_cmsg, which is a QEMU bug.
1402 */
1403 if (msg_controllen < sizeof(struct cmsghdr)) {
1404 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
bellard7854b052003-03-29 17:22:23 +00001405 break;
1406 }
1407
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001408 if (cmsg->cmsg_level == SOL_SOCKET) {
1409 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1410 } else {
1411 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1412 }
bellard7854b052003-03-29 17:22:23 +00001413 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
bellard7854b052003-03-29 17:22:23 +00001414
Peter Maydellc2aeb252015-05-26 19:46:31 +01001415 tgt_len = TARGET_CMSG_LEN(len);
1416
1417 /* Payload types which need a different size of payload on
1418 * the target must adjust tgt_len here.
1419 */
1420 switch (cmsg->cmsg_level) {
1421 case SOL_SOCKET:
1422 switch (cmsg->cmsg_type) {
1423 case SO_TIMESTAMP:
1424 tgt_len = sizeof(struct target_timeval);
1425 break;
1426 default:
1427 break;
1428 }
1429 default:
1430 break;
1431 }
1432
1433 if (msg_controllen < tgt_len) {
1434 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1435 tgt_len = msg_controllen;
1436 }
1437
1438 /* We must now copy-and-convert len bytes of payload
1439 * into tgt_len bytes of destination space. Bear in mind
1440 * that in both source and destination we may be dealing
1441 * with a truncated value!
1442 */
Huw Davies52b65492014-04-17 14:02:47 +01001443 switch (cmsg->cmsg_level) {
1444 case SOL_SOCKET:
1445 switch (cmsg->cmsg_type) {
1446 case SCM_RIGHTS:
1447 {
1448 int *fd = (int *)data;
1449 int *target_fd = (int *)target_data;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001450 int i, numfds = tgt_len / sizeof(int);
bellard7854b052003-03-29 17:22:23 +00001451
Peter Maydell876e23c2015-05-26 19:46:32 +01001452 for (i = 0; i < numfds; i++) {
1453 __put_user(fd[i], target_fd + i);
1454 }
Huw Davies52b65492014-04-17 14:02:47 +01001455 break;
1456 }
1457 case SO_TIMESTAMP:
1458 {
1459 struct timeval *tv = (struct timeval *)data;
1460 struct target_timeval *target_tv =
1461 (struct target_timeval *)target_data;
Jing Huangaebf5bc2012-07-24 14:01:42 +00001462
Peter Maydellc2aeb252015-05-26 19:46:31 +01001463 if (len != sizeof(struct timeval) ||
1464 tgt_len != sizeof(struct target_timeval)) {
Huw Davies52b65492014-04-17 14:02:47 +01001465 goto unimplemented;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001466 }
Huw Davies52b65492014-04-17 14:02:47 +01001467
1468 /* copy struct timeval to target */
Peter Maydell876e23c2015-05-26 19:46:32 +01001469 __put_user(tv->tv_sec, &target_tv->tv_sec);
1470 __put_user(tv->tv_usec, &target_tv->tv_usec);
Huw Davies52b65492014-04-17 14:02:47 +01001471 break;
1472 }
Huw Davies4bc29752014-04-17 14:02:48 +01001473 case SCM_CREDENTIALS:
1474 {
1475 struct ucred *cred = (struct ucred *)data;
1476 struct target_ucred *target_cred =
1477 (struct target_ucred *)target_data;
1478
1479 __put_user(cred->pid, &target_cred->pid);
1480 __put_user(cred->uid, &target_cred->uid);
1481 __put_user(cred->gid, &target_cred->gid);
1482 break;
1483 }
Huw Davies52b65492014-04-17 14:02:47 +01001484 default:
1485 goto unimplemented;
1486 }
1487 break;
1488
1489 default:
1490 unimplemented:
Jing Huangaebf5bc2012-07-24 14:01:42 +00001491 gemu_log("Unsupported ancillary data: %d/%d\n",
1492 cmsg->cmsg_level, cmsg->cmsg_type);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001493 memcpy(target_data, data, MIN(len, tgt_len));
1494 if (tgt_len > len) {
1495 memset(target_data + len, 0, tgt_len - len);
1496 }
bellard7854b052003-03-29 17:22:23 +00001497 }
1498
Peter Maydellc2aeb252015-05-26 19:46:31 +01001499 target_cmsg->cmsg_len = tswapal(tgt_len);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001500 tgt_space = TARGET_CMSG_SPACE(len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001501 if (msg_controllen < tgt_space) {
1502 tgt_space = msg_controllen;
1503 }
1504 msg_controllen -= tgt_space;
1505 space += tgt_space;
bellard7854b052003-03-29 17:22:23 +00001506 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001507 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1508 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00001509 }
bellard5a4a8982007-11-11 17:39:18 +00001510 unlock_user(target_cmsg, target_cmsg_addr, space);
1511 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001512 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001513 return 0;
bellard7854b052003-03-29 17:22:23 +00001514}
1515
ths0da46a62007-10-20 20:23:07 +00001516/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001517static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001518 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001519{
blueswir1992f48a2007-10-14 16:27:31 +00001520 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001521 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001522 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001523 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001524
bellard8853f862004-02-22 14:57:26 +00001525 switch(level) {
1526 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001527 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001528 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001529 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001530
bellard2f619692007-11-16 10:46:05 +00001531 if (get_user_u32(val, optval_addr))
1532 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001533 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1534 break;
1535 case SOL_IP:
1536 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001537 case IP_TOS:
1538 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001539 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001540 case IP_ROUTER_ALERT:
1541 case IP_RECVOPTS:
1542 case IP_RETOPTS:
1543 case IP_PKTINFO:
1544 case IP_MTU_DISCOVER:
1545 case IP_RECVERR:
1546 case IP_RECVTOS:
1547#ifdef IP_FREEBIND
1548 case IP_FREEBIND:
1549#endif
1550 case IP_MULTICAST_TTL:
1551 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001552 val = 0;
1553 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001554 if (get_user_u32(val, optval_addr))
1555 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001556 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001557 if (get_user_u8(val, optval_addr))
1558 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001559 }
1560 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1561 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001562 case IP_ADD_MEMBERSHIP:
1563 case IP_DROP_MEMBERSHIP:
1564 if (optlen < sizeof (struct target_ip_mreq) ||
1565 optlen > sizeof (struct target_ip_mreqn))
1566 return -TARGET_EINVAL;
1567
1568 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1569 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1570 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1571 break;
1572
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001573 case IP_BLOCK_SOURCE:
1574 case IP_UNBLOCK_SOURCE:
1575 case IP_ADD_SOURCE_MEMBERSHIP:
1576 case IP_DROP_SOURCE_MEMBERSHIP:
1577 if (optlen != sizeof (struct target_ip_mreq_source))
1578 return -TARGET_EINVAL;
1579
1580 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1581 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1582 unlock_user (ip_mreq_source, optval_addr, 0);
1583 break;
1584
bellard8853f862004-02-22 14:57:26 +00001585 default:
1586 goto unimplemented;
1587 }
1588 break;
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02001589 case SOL_IPV6:
1590 switch (optname) {
1591 case IPV6_MTU_DISCOVER:
1592 case IPV6_MTU:
1593 case IPV6_V6ONLY:
1594 case IPV6_RECVPKTINFO:
1595 val = 0;
1596 if (optlen < sizeof(uint32_t)) {
1597 return -TARGET_EINVAL;
1598 }
1599 if (get_user_u32(val, optval_addr)) {
1600 return -TARGET_EFAULT;
1601 }
1602 ret = get_errno(setsockopt(sockfd, level, optname,
1603 &val, sizeof(val)));
1604 break;
1605 default:
1606 goto unimplemented;
1607 }
1608 break;
Jing Huang920394d2012-07-24 13:59:23 +00001609 case SOL_RAW:
1610 switch (optname) {
1611 case ICMP_FILTER:
1612 /* struct icmp_filter takes an u32 value */
1613 if (optlen < sizeof(uint32_t)) {
1614 return -TARGET_EINVAL;
1615 }
1616
1617 if (get_user_u32(val, optval_addr)) {
1618 return -TARGET_EFAULT;
1619 }
1620 ret = get_errno(setsockopt(sockfd, level, optname,
1621 &val, sizeof(val)));
1622 break;
1623
1624 default:
1625 goto unimplemented;
1626 }
1627 break;
bellard3532fa72006-06-24 15:06:03 +00001628 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001629 switch (optname) {
Laurent Vivier1b09aeb2013-01-01 08:24:11 +00001630 case TARGET_SO_RCVTIMEO:
1631 {
1632 struct timeval tv;
1633
1634 optname = SO_RCVTIMEO;
1635
1636set_timeout:
1637 if (optlen != sizeof(struct target_timeval)) {
1638 return -TARGET_EINVAL;
1639 }
1640
1641 if (copy_from_user_timeval(&tv, optval_addr)) {
1642 return -TARGET_EFAULT;
1643 }
1644
1645 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1646 &tv, sizeof(tv)));
1647 return ret;
1648 }
1649 case TARGET_SO_SNDTIMEO:
1650 optname = SO_SNDTIMEO;
1651 goto set_timeout;
Laurent Vivierf57d4192013-08-30 01:46:41 +02001652 case TARGET_SO_ATTACH_FILTER:
1653 {
1654 struct target_sock_fprog *tfprog;
1655 struct target_sock_filter *tfilter;
1656 struct sock_fprog fprog;
1657 struct sock_filter *filter;
1658 int i;
1659
1660 if (optlen != sizeof(*tfprog)) {
1661 return -TARGET_EINVAL;
1662 }
1663 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
1664 return -TARGET_EFAULT;
1665 }
1666 if (!lock_user_struct(VERIFY_READ, tfilter,
1667 tswapal(tfprog->filter), 0)) {
1668 unlock_user_struct(tfprog, optval_addr, 1);
1669 return -TARGET_EFAULT;
1670 }
1671
1672 fprog.len = tswap16(tfprog->len);
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05301673 filter = g_try_new(struct sock_filter, fprog.len);
Laurent Vivierf57d4192013-08-30 01:46:41 +02001674 if (filter == NULL) {
1675 unlock_user_struct(tfilter, tfprog->filter, 1);
1676 unlock_user_struct(tfprog, optval_addr, 1);
1677 return -TARGET_ENOMEM;
1678 }
1679 for (i = 0; i < fprog.len; i++) {
1680 filter[i].code = tswap16(tfilter[i].code);
1681 filter[i].jt = tfilter[i].jt;
1682 filter[i].jf = tfilter[i].jf;
1683 filter[i].k = tswap32(tfilter[i].k);
1684 }
1685 fprog.filter = filter;
1686
1687 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
1688 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05301689 g_free(filter);
Laurent Vivierf57d4192013-08-30 01:46:41 +02001690
1691 unlock_user_struct(tfilter, tfprog->filter, 1);
1692 unlock_user_struct(tfprog, optval_addr, 1);
1693 return ret;
1694 }
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02001695 case TARGET_SO_BINDTODEVICE:
1696 {
1697 char *dev_ifname, *addr_ifname;
1698
1699 if (optlen > IFNAMSIZ - 1) {
1700 optlen = IFNAMSIZ - 1;
1701 }
1702 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1703 if (!dev_ifname) {
1704 return -TARGET_EFAULT;
1705 }
1706 optname = SO_BINDTODEVICE;
1707 addr_ifname = alloca(IFNAMSIZ);
1708 memcpy(addr_ifname, dev_ifname, optlen);
1709 addr_ifname[optlen] = 0;
Chen Gangfad6c582016-01-11 13:58:50 +08001710 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1711 addr_ifname, optlen));
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02001712 unlock_user (dev_ifname, optval_addr, 0);
1713 return ret;
1714 }
bellard8853f862004-02-22 14:57:26 +00001715 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001716 case TARGET_SO_DEBUG:
1717 optname = SO_DEBUG;
1718 break;
1719 case TARGET_SO_REUSEADDR:
1720 optname = SO_REUSEADDR;
1721 break;
1722 case TARGET_SO_TYPE:
1723 optname = SO_TYPE;
1724 break;
1725 case TARGET_SO_ERROR:
1726 optname = SO_ERROR;
1727 break;
1728 case TARGET_SO_DONTROUTE:
1729 optname = SO_DONTROUTE;
1730 break;
1731 case TARGET_SO_BROADCAST:
1732 optname = SO_BROADCAST;
1733 break;
1734 case TARGET_SO_SNDBUF:
1735 optname = SO_SNDBUF;
1736 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01001737 case TARGET_SO_SNDBUFFORCE:
1738 optname = SO_SNDBUFFORCE;
1739 break;
bellard3532fa72006-06-24 15:06:03 +00001740 case TARGET_SO_RCVBUF:
1741 optname = SO_RCVBUF;
1742 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01001743 case TARGET_SO_RCVBUFFORCE:
1744 optname = SO_RCVBUFFORCE;
1745 break;
bellard3532fa72006-06-24 15:06:03 +00001746 case TARGET_SO_KEEPALIVE:
1747 optname = SO_KEEPALIVE;
1748 break;
1749 case TARGET_SO_OOBINLINE:
1750 optname = SO_OOBINLINE;
1751 break;
1752 case TARGET_SO_NO_CHECK:
1753 optname = SO_NO_CHECK;
1754 break;
1755 case TARGET_SO_PRIORITY:
1756 optname = SO_PRIORITY;
1757 break;
bellard5e83e8e2005-03-01 22:32:06 +00001758#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001759 case TARGET_SO_BSDCOMPAT:
1760 optname = SO_BSDCOMPAT;
1761 break;
bellard5e83e8e2005-03-01 22:32:06 +00001762#endif
bellard3532fa72006-06-24 15:06:03 +00001763 case TARGET_SO_PASSCRED:
1764 optname = SO_PASSCRED;
1765 break;
Paul Burton82d0fe62014-06-22 11:25:36 +01001766 case TARGET_SO_PASSSEC:
1767 optname = SO_PASSSEC;
1768 break;
bellard3532fa72006-06-24 15:06:03 +00001769 case TARGET_SO_TIMESTAMP:
1770 optname = SO_TIMESTAMP;
1771 break;
1772 case TARGET_SO_RCVLOWAT:
1773 optname = SO_RCVLOWAT;
1774 break;
bellard8853f862004-02-22 14:57:26 +00001775 break;
1776 default:
1777 goto unimplemented;
1778 }
bellard3532fa72006-06-24 15:06:03 +00001779 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001780 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001781
bellard2f619692007-11-16 10:46:05 +00001782 if (get_user_u32(val, optval_addr))
1783 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001784 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001785 break;
bellard7854b052003-03-29 17:22:23 +00001786 default:
bellard8853f862004-02-22 14:57:26 +00001787 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02001788 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001789 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001790 }
bellard8853f862004-02-22 14:57:26 +00001791 return ret;
bellard7854b052003-03-29 17:22:23 +00001792}
1793
ths0da46a62007-10-20 20:23:07 +00001794/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001795static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001796 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001797{
blueswir1992f48a2007-10-14 16:27:31 +00001798 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001799 int len, val;
1800 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001801
1802 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001803 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001804 level = SOL_SOCKET;
1805 switch (optname) {
1806 /* These don't just return a single integer */
1807 case TARGET_SO_LINGER:
1808 case TARGET_SO_RCVTIMEO:
1809 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001810 case TARGET_SO_PEERNAME:
1811 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02001812 case TARGET_SO_PEERCRED: {
1813 struct ucred cr;
1814 socklen_t crlen;
1815 struct target_ucred *tcr;
1816
1817 if (get_user_u32(len, optlen)) {
1818 return -TARGET_EFAULT;
1819 }
1820 if (len < 0) {
1821 return -TARGET_EINVAL;
1822 }
1823
1824 crlen = sizeof(cr);
1825 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1826 &cr, &crlen));
1827 if (ret < 0) {
1828 return ret;
1829 }
1830 if (len > crlen) {
1831 len = crlen;
1832 }
1833 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1834 return -TARGET_EFAULT;
1835 }
1836 __put_user(cr.pid, &tcr->pid);
1837 __put_user(cr.uid, &tcr->uid);
1838 __put_user(cr.gid, &tcr->gid);
1839 unlock_user_struct(tcr, optval_addr, 1);
1840 if (put_user_u32(len, optlen)) {
1841 return -TARGET_EFAULT;
1842 }
1843 break;
1844 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001845 /* Options with 'int' argument. */
1846 case TARGET_SO_DEBUG:
1847 optname = SO_DEBUG;
1848 goto int_case;
1849 case TARGET_SO_REUSEADDR:
1850 optname = SO_REUSEADDR;
1851 goto int_case;
1852 case TARGET_SO_TYPE:
1853 optname = SO_TYPE;
1854 goto int_case;
1855 case TARGET_SO_ERROR:
1856 optname = SO_ERROR;
1857 goto int_case;
1858 case TARGET_SO_DONTROUTE:
1859 optname = SO_DONTROUTE;
1860 goto int_case;
1861 case TARGET_SO_BROADCAST:
1862 optname = SO_BROADCAST;
1863 goto int_case;
1864 case TARGET_SO_SNDBUF:
1865 optname = SO_SNDBUF;
1866 goto int_case;
1867 case TARGET_SO_RCVBUF:
1868 optname = SO_RCVBUF;
1869 goto int_case;
1870 case TARGET_SO_KEEPALIVE:
1871 optname = SO_KEEPALIVE;
1872 goto int_case;
1873 case TARGET_SO_OOBINLINE:
1874 optname = SO_OOBINLINE;
1875 goto int_case;
1876 case TARGET_SO_NO_CHECK:
1877 optname = SO_NO_CHECK;
1878 goto int_case;
1879 case TARGET_SO_PRIORITY:
1880 optname = SO_PRIORITY;
1881 goto int_case;
1882#ifdef SO_BSDCOMPAT
1883 case TARGET_SO_BSDCOMPAT:
1884 optname = SO_BSDCOMPAT;
1885 goto int_case;
1886#endif
1887 case TARGET_SO_PASSCRED:
1888 optname = SO_PASSCRED;
1889 goto int_case;
1890 case TARGET_SO_TIMESTAMP:
1891 optname = SO_TIMESTAMP;
1892 goto int_case;
1893 case TARGET_SO_RCVLOWAT:
1894 optname = SO_RCVLOWAT;
1895 goto int_case;
Paul Burtonaec1ca42014-06-22 11:25:34 +01001896 case TARGET_SO_ACCEPTCONN:
1897 optname = SO_ACCEPTCONN;
1898 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001899 default:
bellard2efbe912005-07-23 15:10:20 +00001900 goto int_case;
1901 }
1902 break;
1903 case SOL_TCP:
1904 /* TCP options all take an 'int' value. */
1905 int_case:
bellard2f619692007-11-16 10:46:05 +00001906 if (get_user_u32(len, optlen))
1907 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001908 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001909 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001910 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00001911 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1912 if (ret < 0)
1913 return ret;
Paul Burton8289d112014-06-22 11:25:33 +01001914 if (optname == SO_TYPE) {
1915 val = host_to_target_sock_type(val);
1916 }
bellard2efbe912005-07-23 15:10:20 +00001917 if (len > lv)
1918 len = lv;
bellard2f619692007-11-16 10:46:05 +00001919 if (len == 4) {
1920 if (put_user_u32(val, optval_addr))
1921 return -TARGET_EFAULT;
1922 } else {
1923 if (put_user_u8(val, optval_addr))
1924 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001925 }
bellard2f619692007-11-16 10:46:05 +00001926 if (put_user_u32(len, optlen))
1927 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001928 break;
1929 case SOL_IP:
1930 switch(optname) {
1931 case IP_TOS:
1932 case IP_TTL:
1933 case IP_HDRINCL:
1934 case IP_ROUTER_ALERT:
1935 case IP_RECVOPTS:
1936 case IP_RETOPTS:
1937 case IP_PKTINFO:
1938 case IP_MTU_DISCOVER:
1939 case IP_RECVERR:
1940 case IP_RECVTOS:
1941#ifdef IP_FREEBIND
1942 case IP_FREEBIND:
1943#endif
1944 case IP_MULTICAST_TTL:
1945 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001946 if (get_user_u32(len, optlen))
1947 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001948 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001949 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001950 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001951 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1952 if (ret < 0)
1953 return ret;
bellard2efbe912005-07-23 15:10:20 +00001954 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001955 len = 1;
bellard2f619692007-11-16 10:46:05 +00001956 if (put_user_u32(len, optlen)
1957 || put_user_u8(val, optval_addr))
1958 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001959 } else {
bellard2efbe912005-07-23 15:10:20 +00001960 if (len > sizeof(int))
1961 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001962 if (put_user_u32(len, optlen)
1963 || put_user_u32(val, optval_addr))
1964 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001965 }
bellard8853f862004-02-22 14:57:26 +00001966 break;
bellard2efbe912005-07-23 15:10:20 +00001967 default:
thsc02f4992007-12-18 02:39:59 +00001968 ret = -TARGET_ENOPROTOOPT;
1969 break;
bellard8853f862004-02-22 14:57:26 +00001970 }
1971 break;
1972 default:
1973 unimplemented:
1974 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1975 level, optname);
thsc02f4992007-12-18 02:39:59 +00001976 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001977 break;
1978 }
1979 return ret;
bellard7854b052003-03-29 17:22:23 +00001980}
1981
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001982static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1983 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001984{
1985 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001986 struct iovec *vec;
1987 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00001988 int i;
Peter Maydell501bb4b2014-02-17 18:55:33 +00001989 int err = 0;
Tom Musta29560a62014-08-12 13:53:43 -05001990 bool bad_address = false;
pbrook53a59602006-03-25 19:31:22 +00001991
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001992 if (count == 0) {
1993 errno = 0;
1994 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001995 }
Peter Maydelldfae8e02013-02-08 07:58:41 +00001996 if (count < 0 || count > IOV_MAX) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001997 errno = EINVAL;
1998 return NULL;
1999 }
2000
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302001 vec = g_try_new0(struct iovec, count);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002002 if (vec == NULL) {
2003 errno = ENOMEM;
2004 return NULL;
2005 }
2006
2007 target_vec = lock_user(VERIFY_READ, target_addr,
2008 count * sizeof(struct target_iovec), 1);
2009 if (target_vec == NULL) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00002010 err = EFAULT;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002011 goto fail2;
2012 }
2013
2014 /* ??? If host page size > target page size, this will result in a
2015 value larger than what we can actually support. */
2016 max_len = 0x7fffffff & TARGET_PAGE_MASK;
2017 total_len = 0;
2018
2019 for (i = 0; i < count; i++) {
2020 abi_ulong base = tswapal(target_vec[i].iov_base);
2021 abi_long len = tswapal(target_vec[i].iov_len);
2022
2023 if (len < 0) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00002024 err = EINVAL;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002025 goto fail;
2026 } else if (len == 0) {
2027 /* Zero length pointer is ignored. */
2028 vec[i].iov_base = 0;
2029 } else {
2030 vec[i].iov_base = lock_user(type, base, len, copy);
Tom Musta29560a62014-08-12 13:53:43 -05002031 /* If the first buffer pointer is bad, this is a fault. But
2032 * subsequent bad buffers will result in a partial write; this
2033 * is realized by filling the vector with null pointers and
2034 * zero lengths. */
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002035 if (!vec[i].iov_base) {
Tom Musta29560a62014-08-12 13:53:43 -05002036 if (i == 0) {
2037 err = EFAULT;
2038 goto fail;
2039 } else {
2040 bad_address = true;
2041 }
2042 }
2043 if (bad_address) {
2044 len = 0;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002045 }
2046 if (len > max_len - total_len) {
2047 len = max_len - total_len;
2048 }
2049 }
2050 vec[i].iov_len = len;
2051 total_len += len;
2052 }
2053
2054 unlock_user(target_vec, target_addr, 0);
2055 return vec;
2056
2057 fail:
Chen Gang S7eff5182015-01-23 18:01:09 +08002058 while (--i >= 0) {
2059 if (tswapal(target_vec[i].iov_len) > 0) {
2060 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
2061 }
2062 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002063 unlock_user(target_vec, target_addr, 0);
Peter Maydell501bb4b2014-02-17 18:55:33 +00002064 fail2:
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302065 g_free(vec);
Peter Maydell501bb4b2014-02-17 18:55:33 +00002066 errno = err;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002067 return NULL;
pbrook53a59602006-03-25 19:31:22 +00002068}
2069
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002070static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
2071 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00002072{
2073 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00002074 int i;
2075
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002076 target_vec = lock_user(VERIFY_READ, target_addr,
2077 count * sizeof(struct target_iovec), 1);
2078 if (target_vec) {
2079 for (i = 0; i < count; i++) {
2080 abi_ulong base = tswapal(target_vec[i].iov_base);
Chen Gang S71ec7ce2015-01-23 18:07:50 +08002081 abi_long len = tswapal(target_vec[i].iov_len);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002082 if (len < 0) {
2083 break;
2084 }
balrogd732dcb2008-10-28 10:21:03 +00002085 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
2086 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002087 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00002088 }
bellard579a97f2007-11-11 14:26:47 +00002089
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302090 g_free(vec);
pbrook53a59602006-03-25 19:31:22 +00002091}
2092
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002093static inline int target_to_host_sock_type(int *type)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002094{
2095 int host_type = 0;
2096 int target_type = *type;
2097
2098 switch (target_type & TARGET_SOCK_TYPE_MASK) {
2099 case TARGET_SOCK_DGRAM:
2100 host_type = SOCK_DGRAM;
2101 break;
2102 case TARGET_SOCK_STREAM:
2103 host_type = SOCK_STREAM;
2104 break;
2105 default:
2106 host_type = target_type & TARGET_SOCK_TYPE_MASK;
2107 break;
2108 }
2109 if (target_type & TARGET_SOCK_CLOEXEC) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002110#if defined(SOCK_CLOEXEC)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002111 host_type |= SOCK_CLOEXEC;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002112#else
2113 return -TARGET_EINVAL;
2114#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002115 }
2116 if (target_type & TARGET_SOCK_NONBLOCK) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002117#if defined(SOCK_NONBLOCK)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002118 host_type |= SOCK_NONBLOCK;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002119#elif !defined(O_NONBLOCK)
2120 return -TARGET_EINVAL;
2121#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002122 }
2123 *type = host_type;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002124 return 0;
2125}
2126
2127/* Try to emulate socket type flags after socket creation. */
2128static int sock_flags_fixup(int fd, int target_type)
2129{
2130#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2131 if (target_type & TARGET_SOCK_NONBLOCK) {
2132 int flags = fcntl(fd, F_GETFL);
2133 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
2134 close(fd);
2135 return -TARGET_EINVAL;
2136 }
2137 }
2138#endif
2139 return fd;
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002140}
2141
Laurent Vivier0cf22722015-10-28 21:40:45 +01002142static abi_long packet_target_to_host_sockaddr(void *host_addr,
2143 abi_ulong target_addr,
2144 socklen_t len)
2145{
2146 struct sockaddr *addr = host_addr;
2147 struct target_sockaddr *target_saddr;
2148
2149 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
2150 if (!target_saddr) {
2151 return -TARGET_EFAULT;
2152 }
2153
2154 memcpy(addr, target_saddr, len);
2155 addr->sa_family = tswap16(target_saddr->sa_family);
2156 /* spkt_protocol is big-endian */
2157
2158 unlock_user(target_saddr, target_addr, 0);
2159 return 0;
2160}
2161
2162static TargetFdTrans target_packet_trans = {
2163 .target_to_host_addr = packet_target_to_host_sockaddr,
2164};
2165
ths0da46a62007-10-20 20:23:07 +00002166/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002167static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00002168{
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002169 int target_type = type;
2170 int ret;
2171
2172 ret = target_to_host_sock_type(&type);
2173 if (ret) {
2174 return ret;
2175 }
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002176
balrog12bc92a2007-10-30 21:06:14 +00002177 if (domain == PF_NETLINK)
Ed Swierk480eda22014-05-05 20:04:45 -07002178 return -TARGET_EAFNOSUPPORT;
Laurent Vivierff626f22015-10-28 21:40:42 +01002179
2180 if (domain == AF_PACKET ||
2181 (domain == AF_INET && type == SOCK_PACKET)) {
2182 protocol = tswap16(protocol);
2183 }
2184
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002185 ret = get_errno(socket(domain, type, protocol));
2186 if (ret >= 0) {
2187 ret = sock_flags_fixup(ret, target_type);
Laurent Vivier0cf22722015-10-28 21:40:45 +01002188 if (type == SOCK_PACKET) {
2189 /* Manage an obsolete case :
2190 * if socket type is SOCK_PACKET, bind by name
2191 */
2192 fd_trans_register(ret, &target_packet_trans);
2193 }
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002194 }
2195 return ret;
bellard3532fa72006-06-24 15:06:03 +00002196}
2197
ths0da46a62007-10-20 20:23:07 +00002198/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002199static abi_long do_bind(int sockfd, abi_ulong target_addr,
2200 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00002201{
aurel328f7aeaf2009-01-30 19:47:57 +00002202 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03002203 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00002204
Blue Swirl38724252010-09-18 05:53:14 +00002205 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002206 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002207 }
aurel328f7aeaf2009-01-30 19:47:57 +00002208
aurel32607175e2009-04-15 16:11:59 +00002209 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00002210
Laurent Vivier7b36f782015-10-28 21:40:44 +01002211 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002212 if (ret)
2213 return ret;
2214
bellard3532fa72006-06-24 15:06:03 +00002215 return get_errno(bind(sockfd, addr, addrlen));
2216}
2217
ths0da46a62007-10-20 20:23:07 +00002218/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002219static abi_long do_connect(int sockfd, abi_ulong target_addr,
2220 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00002221{
aurel328f7aeaf2009-01-30 19:47:57 +00002222 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03002223 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00002224
Blue Swirl38724252010-09-18 05:53:14 +00002225 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002226 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002227 }
aurel328f7aeaf2009-01-30 19:47:57 +00002228
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02002229 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00002230
Laurent Vivier7b36f782015-10-28 21:40:44 +01002231 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002232 if (ret)
2233 return ret;
2234
bellard3532fa72006-06-24 15:06:03 +00002235 return get_errno(connect(sockfd, addr, addrlen));
2236}
2237
Alexander Graff19e00d2014-03-02 19:36:42 +00002238/* do_sendrecvmsg_locked() Must return target values and target errnos. */
2239static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
2240 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00002241{
balrog6de645c2008-10-28 10:26:29 +00002242 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00002243 struct msghdr msg;
2244 int count;
2245 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00002246 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00002247
bellard3532fa72006-06-24 15:06:03 +00002248 if (msgp->msg_name) {
2249 msg.msg_namelen = tswap32(msgp->msg_namelen);
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02002250 msg.msg_name = alloca(msg.msg_namelen+1);
Laurent Vivier7b36f782015-10-28 21:40:44 +01002251 ret = target_to_host_sockaddr(fd, msg.msg_name,
2252 tswapal(msgp->msg_name),
2253 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002254 if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002255 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03002256 }
bellard3532fa72006-06-24 15:06:03 +00002257 } else {
2258 msg.msg_name = NULL;
2259 msg.msg_namelen = 0;
2260 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002261 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00002262 msg.msg_control = alloca(msg.msg_controllen);
2263 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00002264
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002265 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002266 target_vec = tswapal(msgp->msg_iov);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002267 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2268 target_vec, count, send);
2269 if (vec == NULL) {
2270 ret = -host_to_target_errno(errno);
2271 goto out2;
2272 }
bellard3532fa72006-06-24 15:06:03 +00002273 msg.msg_iovlen = count;
2274 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00002275
bellard3532fa72006-06-24 15:06:03 +00002276 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00002277 ret = target_to_host_cmsg(&msg, msgp);
2278 if (ret == 0)
2279 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00002280 } else {
2281 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00002282 if (!is_error(ret)) {
2283 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00002284 ret = host_to_target_cmsg(msgp, &msg);
Jing Huangca619062012-07-24 13:58:02 +00002285 if (!is_error(ret)) {
2286 msgp->msg_namelen = tswap32(msg.msg_namelen);
2287 if (msg.msg_name != NULL) {
2288 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
2289 msg.msg_name, msg.msg_namelen);
2290 if (ret) {
2291 goto out;
2292 }
2293 }
2294
balrog6de645c2008-10-28 10:26:29 +00002295 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00002296 }
balrog6de645c2008-10-28 10:26:29 +00002297 }
bellard3532fa72006-06-24 15:06:03 +00002298 }
Jing Huangca619062012-07-24 13:58:02 +00002299
2300out:
bellard3532fa72006-06-24 15:06:03 +00002301 unlock_iovec(vec, target_vec, count, !send);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002302out2:
Alexander Graff19e00d2014-03-02 19:36:42 +00002303 return ret;
2304}
2305
2306static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
2307 int flags, int send)
2308{
2309 abi_long ret;
2310 struct target_msghdr *msgp;
2311
2312 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
2313 msgp,
2314 target_msg,
2315 send ? 1 : 0)) {
2316 return -TARGET_EFAULT;
2317 }
2318 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
bellard579a97f2007-11-11 14:26:47 +00002319 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00002320 return ret;
2321}
2322
Alexander Graff19e00d2014-03-02 19:36:42 +00002323/* We don't rely on the C library to have sendmmsg/recvmmsg support,
2324 * so it might not have this *mmsg-specific flag either.
2325 */
2326#ifndef MSG_WAITFORONE
2327#define MSG_WAITFORONE 0x10000
2328#endif
2329
2330static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
2331 unsigned int vlen, unsigned int flags,
2332 int send)
2333{
2334 struct target_mmsghdr *mmsgp;
2335 abi_long ret = 0;
2336 int i;
2337
2338 if (vlen > UIO_MAXIOV) {
2339 vlen = UIO_MAXIOV;
2340 }
2341
2342 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
2343 if (!mmsgp) {
2344 return -TARGET_EFAULT;
2345 }
2346
2347 for (i = 0; i < vlen; i++) {
2348 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
2349 if (is_error(ret)) {
2350 break;
2351 }
2352 mmsgp[i].msg_len = tswap32(ret);
2353 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2354 if (flags & MSG_WAITFORONE) {
2355 flags |= MSG_DONTWAIT;
2356 }
2357 }
2358
2359 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
2360
2361 /* Return number of datagrams sent if we sent any at all;
2362 * otherwise return the error.
2363 */
2364 if (i) {
2365 return i;
2366 }
2367 return ret;
2368}
Alexander Graff19e00d2014-03-02 19:36:42 +00002369
Peter Maydella94b4982013-02-08 04:35:04 +00002370/* If we don't have a system accept4() then just call accept.
2371 * The callsites to do_accept4() will ensure that they don't
2372 * pass a non-zero flags argument in this config.
2373 */
2374#ifndef CONFIG_ACCEPT4
2375static inline int accept4(int sockfd, struct sockaddr *addr,
2376 socklen_t *addrlen, int flags)
2377{
2378 assert(flags == 0);
2379 return accept(sockfd, addr, addrlen);
2380}
2381#endif
2382
2383/* do_accept4() Must return target values and target errnos. */
2384static abi_long do_accept4(int fd, abi_ulong target_addr,
2385 abi_ulong target_addrlen_addr, int flags)
pbrook1be9e1d2006-11-19 15:26:04 +00002386{
bellard2f619692007-11-16 10:46:05 +00002387 socklen_t addrlen;
2388 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002389 abi_long ret;
Petar Jovanovicd25295d2014-03-31 17:41:23 +02002390 int host_flags;
2391
2392 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
pbrook1be9e1d2006-11-19 15:26:04 +00002393
Peter Maydella94b4982013-02-08 04:35:04 +00002394 if (target_addr == 0) {
Petar Jovanovicd25295d2014-03-31 17:41:23 +02002395 return get_errno(accept4(fd, NULL, NULL, host_flags));
Peter Maydella94b4982013-02-08 04:35:04 +00002396 }
Arnaud Patard917507b2009-06-19 10:44:45 +03002397
2398 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00002399 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03002400 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00002401
Blue Swirl38724252010-09-18 05:53:14 +00002402 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002403 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002404 }
aurel328f7aeaf2009-01-30 19:47:57 +00002405
Arnaud Patard917507b2009-06-19 10:44:45 +03002406 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2407 return -TARGET_EINVAL;
2408
bellard2f619692007-11-16 10:46:05 +00002409 addr = alloca(addrlen);
2410
Petar Jovanovicd25295d2014-03-31 17:41:23 +02002411 ret = get_errno(accept4(fd, addr, &addrlen, host_flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002412 if (!is_error(ret)) {
2413 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002414 if (put_user_u32(addrlen, target_addrlen_addr))
2415 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002416 }
2417 return ret;
2418}
2419
ths0da46a62007-10-20 20:23:07 +00002420/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002421static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002422 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002423{
bellard2f619692007-11-16 10:46:05 +00002424 socklen_t addrlen;
2425 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002426 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002427
bellard2f619692007-11-16 10:46:05 +00002428 if (get_user_u32(addrlen, target_addrlen_addr))
2429 return -TARGET_EFAULT;
2430
Blue Swirl38724252010-09-18 05:53:14 +00002431 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002432 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002433 }
aurel328f7aeaf2009-01-30 19:47:57 +00002434
Arnaud Patard917507b2009-06-19 10:44:45 +03002435 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2436 return -TARGET_EFAULT;
2437
bellard2f619692007-11-16 10:46:05 +00002438 addr = alloca(addrlen);
2439
pbrook1be9e1d2006-11-19 15:26:04 +00002440 ret = get_errno(getpeername(fd, addr, &addrlen));
2441 if (!is_error(ret)) {
2442 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002443 if (put_user_u32(addrlen, target_addrlen_addr))
2444 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002445 }
2446 return ret;
2447}
2448
ths0da46a62007-10-20 20:23:07 +00002449/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002450static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002451 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002452{
bellard2f619692007-11-16 10:46:05 +00002453 socklen_t addrlen;
2454 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002455 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002456
bellard2f619692007-11-16 10:46:05 +00002457 if (get_user_u32(addrlen, target_addrlen_addr))
2458 return -TARGET_EFAULT;
2459
Blue Swirl38724252010-09-18 05:53:14 +00002460 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002461 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002462 }
aurel328f7aeaf2009-01-30 19:47:57 +00002463
Arnaud Patard917507b2009-06-19 10:44:45 +03002464 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2465 return -TARGET_EFAULT;
2466
bellard2f619692007-11-16 10:46:05 +00002467 addr = alloca(addrlen);
2468
pbrook1be9e1d2006-11-19 15:26:04 +00002469 ret = get_errno(getsockname(fd, addr, &addrlen));
2470 if (!is_error(ret)) {
2471 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002472 if (put_user_u32(addrlen, target_addrlen_addr))
2473 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002474 }
2475 return ret;
2476}
2477
ths0da46a62007-10-20 20:23:07 +00002478/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002479static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00002480 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002481{
2482 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00002483 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002484
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002485 target_to_host_sock_type(&type);
2486
pbrook1be9e1d2006-11-19 15:26:04 +00002487 ret = get_errno(socketpair(domain, type, protocol, tab));
2488 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00002489 if (put_user_s32(tab[0], target_tab_addr)
2490 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2491 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002492 }
2493 return ret;
2494}
2495
ths0da46a62007-10-20 20:23:07 +00002496/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002497static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2498 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002499{
2500 void *addr;
2501 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002502 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002503
Blue Swirl38724252010-09-18 05:53:14 +00002504 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002505 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002506 }
aurel328f7aeaf2009-01-30 19:47:57 +00002507
bellard579a97f2007-11-11 14:26:47 +00002508 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2509 if (!host_msg)
2510 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002511 if (target_addr) {
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02002512 addr = alloca(addrlen+1);
Laurent Vivier7b36f782015-10-28 21:40:44 +01002513 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002514 if (ret) {
2515 unlock_user(host_msg, msg, 0);
2516 return ret;
2517 }
pbrook1be9e1d2006-11-19 15:26:04 +00002518 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2519 } else {
2520 ret = get_errno(send(fd, host_msg, len, flags));
2521 }
2522 unlock_user(host_msg, msg, 0);
2523 return ret;
2524}
2525
ths0da46a62007-10-20 20:23:07 +00002526/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002527static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2528 abi_ulong target_addr,
2529 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002530{
2531 socklen_t addrlen;
2532 void *addr;
2533 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002534 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002535
bellard579a97f2007-11-11 14:26:47 +00002536 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2537 if (!host_msg)
2538 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002539 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00002540 if (get_user_u32(addrlen, target_addrlen)) {
2541 ret = -TARGET_EFAULT;
2542 goto fail;
2543 }
Blue Swirl38724252010-09-18 05:53:14 +00002544 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002545 ret = -TARGET_EINVAL;
2546 goto fail;
2547 }
pbrook1be9e1d2006-11-19 15:26:04 +00002548 addr = alloca(addrlen);
2549 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2550 } else {
2551 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00002552 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002553 }
2554 if (!is_error(ret)) {
2555 if (target_addr) {
2556 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002557 if (put_user_u32(addrlen, target_addrlen)) {
2558 ret = -TARGET_EFAULT;
2559 goto fail;
2560 }
pbrook1be9e1d2006-11-19 15:26:04 +00002561 }
2562 unlock_user(host_msg, msg, len);
2563 } else {
bellard2f619692007-11-16 10:46:05 +00002564fail:
pbrook1be9e1d2006-11-19 15:26:04 +00002565 unlock_user(host_msg, msg, 0);
2566 }
2567 return ret;
2568}
2569
j_mayer32407102007-09-26 23:01:49 +00002570#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00002571/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002572static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00002573{
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002574 static const unsigned ac[] = { /* number of arguments per call */
2575 [SOCKOP_socket] = 3, /* domain, type, protocol */
2576 [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
2577 [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
2578 [SOCKOP_listen] = 2, /* sockfd, backlog */
2579 [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
2580 [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
2581 [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
2582 [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
2583 [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
2584 [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
2585 [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
2586 [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2587 [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2588 [SOCKOP_shutdown] = 2, /* sockfd, how */
2589 [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
2590 [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
John Paul Adrian Glaubitz5a53dc52015-12-24 19:59:59 +01002591 [SOCKOP_sendmmsg] = 4, /* sockfd, msgvec, vlen, flags */
2592 [SOCKOP_recvmmsg] = 4, /* sockfd, msgvec, vlen, flags */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002593 [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2594 [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2595 };
2596 abi_long a[6]; /* max 6 args */
bellard31e31b82003-02-18 22:55:36 +00002597
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002598 /* first, collect the arguments in a[] according to ac[] */
2599 if (num >= 0 && num < ARRAY_SIZE(ac)) {
2600 unsigned i;
2601 assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */
2602 for (i = 0; i < ac[num]; ++i) {
2603 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
André Hentschelb9d36eb2014-01-06 20:18:52 +01002604 return -TARGET_EFAULT;
2605 }
André Hentschelb9d36eb2014-01-06 20:18:52 +01002606 }
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002607 }
bellard2f619692007-11-16 10:46:05 +00002608
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002609 /* now when we have the args, actually handle the call */
2610 switch (num) {
2611 case SOCKOP_socket: /* domain, type, protocol */
2612 return do_socket(a[0], a[1], a[2]);
2613 case SOCKOP_bind: /* sockfd, addr, addrlen */
2614 return do_bind(a[0], a[1], a[2]);
2615 case SOCKOP_connect: /* sockfd, addr, addrlen */
2616 return do_connect(a[0], a[1], a[2]);
2617 case SOCKOP_listen: /* sockfd, backlog */
2618 return get_errno(listen(a[0], a[1]));
2619 case SOCKOP_accept: /* sockfd, addr, addrlen */
2620 return do_accept4(a[0], a[1], a[2], 0);
2621 case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */
2622 return do_accept4(a[0], a[1], a[2], a[3]);
2623 case SOCKOP_getsockname: /* sockfd, addr, addrlen */
2624 return do_getsockname(a[0], a[1], a[2]);
2625 case SOCKOP_getpeername: /* sockfd, addr, addrlen */
2626 return do_getpeername(a[0], a[1], a[2]);
2627 case SOCKOP_socketpair: /* domain, type, protocol, tab */
2628 return do_socketpair(a[0], a[1], a[2], a[3]);
2629 case SOCKOP_send: /* sockfd, msg, len, flags */
2630 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
2631 case SOCKOP_recv: /* sockfd, msg, len, flags */
2632 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
2633 case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */
2634 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
2635 case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */
2636 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
2637 case SOCKOP_shutdown: /* sockfd, how */
2638 return get_errno(shutdown(a[0], a[1]));
2639 case SOCKOP_sendmsg: /* sockfd, msg, flags */
2640 return do_sendrecvmsg(a[0], a[1], a[2], 1);
2641 case SOCKOP_recvmsg: /* sockfd, msg, flags */
2642 return do_sendrecvmsg(a[0], a[1], a[2], 0);
John Paul Adrian Glaubitz5a53dc52015-12-24 19:59:59 +01002643 case SOCKOP_sendmmsg: /* sockfd, msgvec, vlen, flags */
2644 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
2645 case SOCKOP_recvmmsg: /* sockfd, msgvec, vlen, flags */
2646 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002647 case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */
2648 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
2649 case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */
2650 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
bellard31e31b82003-02-18 22:55:36 +00002651 default:
2652 gemu_log("Unsupported socketcall: %d\n", num);
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002653 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002654 }
bellard31e31b82003-02-18 22:55:36 +00002655}
j_mayer32407102007-09-26 23:01:49 +00002656#endif
bellard31e31b82003-02-18 22:55:36 +00002657
bellard8853f862004-02-22 14:57:26 +00002658#define N_SHM_REGIONS 32
2659
2660static struct shm_region {
Peter Maydellb6e17872016-02-09 15:57:11 +00002661 abi_ulong start;
2662 abi_ulong size;
2663 bool in_use;
bellard8853f862004-02-22 14:57:26 +00002664} shm_regions[N_SHM_REGIONS];
2665
ths3eb6b042007-06-03 14:26:27 +00002666struct target_semid_ds
2667{
2668 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002669 abi_ulong sem_otime;
Tom Musta03527342014-08-12 13:53:32 -05002670#if !defined(TARGET_PPC64)
blueswir1992f48a2007-10-14 16:27:31 +00002671 abi_ulong __unused1;
Tom Musta03527342014-08-12 13:53:32 -05002672#endif
blueswir1992f48a2007-10-14 16:27:31 +00002673 abi_ulong sem_ctime;
Tom Musta03527342014-08-12 13:53:32 -05002674#if !defined(TARGET_PPC64)
blueswir1992f48a2007-10-14 16:27:31 +00002675 abi_ulong __unused2;
Tom Musta03527342014-08-12 13:53:32 -05002676#endif
blueswir1992f48a2007-10-14 16:27:31 +00002677 abi_ulong sem_nsems;
2678 abi_ulong __unused3;
2679 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002680};
2681
bellard579a97f2007-11-11 14:26:47 +00002682static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2683 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002684{
2685 struct target_ipc_perm *target_ip;
2686 struct target_semid_ds *target_sd;
2687
bellard579a97f2007-11-11 14:26:47 +00002688 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2689 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002690 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002691 host_ip->__key = tswap32(target_ip->__key);
2692 host_ip->uid = tswap32(target_ip->uid);
2693 host_ip->gid = tswap32(target_ip->gid);
2694 host_ip->cuid = tswap32(target_ip->cuid);
2695 host_ip->cgid = tswap32(target_ip->cgid);
2696#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2697 host_ip->mode = tswap32(target_ip->mode);
2698#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002699 host_ip->mode = tswap16(target_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002700#endif
2701#if defined(TARGET_PPC)
2702 host_ip->__seq = tswap32(target_ip->__seq);
2703#else
2704 host_ip->__seq = tswap16(target_ip->__seq);
2705#endif
ths3eb6b042007-06-03 14:26:27 +00002706 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002707 return 0;
ths3eb6b042007-06-03 14:26:27 +00002708}
2709
bellard579a97f2007-11-11 14:26:47 +00002710static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2711 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002712{
2713 struct target_ipc_perm *target_ip;
2714 struct target_semid_ds *target_sd;
2715
bellard579a97f2007-11-11 14:26:47 +00002716 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2717 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002718 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002719 target_ip->__key = tswap32(host_ip->__key);
2720 target_ip->uid = tswap32(host_ip->uid);
2721 target_ip->gid = tswap32(host_ip->gid);
2722 target_ip->cuid = tswap32(host_ip->cuid);
2723 target_ip->cgid = tswap32(host_ip->cgid);
2724#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2725 target_ip->mode = tswap32(host_ip->mode);
2726#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002727 target_ip->mode = tswap16(host_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002728#endif
2729#if defined(TARGET_PPC)
2730 target_ip->__seq = tswap32(host_ip->__seq);
2731#else
2732 target_ip->__seq = tswap16(host_ip->__seq);
2733#endif
ths3eb6b042007-06-03 14:26:27 +00002734 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002735 return 0;
ths3eb6b042007-06-03 14:26:27 +00002736}
2737
bellard579a97f2007-11-11 14:26:47 +00002738static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2739 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002740{
2741 struct target_semid_ds *target_sd;
2742
bellard579a97f2007-11-11 14:26:47 +00002743 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2744 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002745 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2746 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002747 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2748 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2749 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002750 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002751 return 0;
ths3eb6b042007-06-03 14:26:27 +00002752}
2753
bellard579a97f2007-11-11 14:26:47 +00002754static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2755 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002756{
2757 struct target_semid_ds *target_sd;
2758
bellard579a97f2007-11-11 14:26:47 +00002759 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2760 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002761 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08002762 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002763 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2764 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2765 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002766 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002767 return 0;
ths3eb6b042007-06-03 14:26:27 +00002768}
2769
aurel32e5289082009-04-18 16:16:12 +00002770struct target_seminfo {
2771 int semmap;
2772 int semmni;
2773 int semmns;
2774 int semmnu;
2775 int semmsl;
2776 int semopm;
2777 int semume;
2778 int semusz;
2779 int semvmx;
2780 int semaem;
2781};
2782
2783static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2784 struct seminfo *host_seminfo)
2785{
2786 struct target_seminfo *target_seminfo;
2787 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2788 return -TARGET_EFAULT;
2789 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2790 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2791 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2792 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2793 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2794 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2795 __put_user(host_seminfo->semume, &target_seminfo->semume);
2796 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2797 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2798 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2799 unlock_user_struct(target_seminfo, target_addr, 1);
2800 return 0;
2801}
2802
thsfa294812007-02-02 22:05:00 +00002803union semun {
2804 int val;
ths3eb6b042007-06-03 14:26:27 +00002805 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002806 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002807 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002808};
2809
ths3eb6b042007-06-03 14:26:27 +00002810union target_semun {
2811 int val;
aurel32e5289082009-04-18 16:16:12 +00002812 abi_ulong buf;
2813 abi_ulong array;
2814 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002815};
2816
aurel32e5289082009-04-18 16:16:12 +00002817static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2818 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002819{
aurel32e5289082009-04-18 16:16:12 +00002820 int nsems;
2821 unsigned short *array;
2822 union semun semun;
2823 struct semid_ds semid_ds;
2824 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002825
aurel32e5289082009-04-18 16:16:12 +00002826 semun.buf = &semid_ds;
2827
2828 ret = semctl(semid, 0, IPC_STAT, semun);
2829 if (ret == -1)
2830 return get_errno(ret);
2831
2832 nsems = semid_ds.sem_nsems;
2833
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302834 *host_array = g_try_new(unsigned short, nsems);
Peter Maydell69d4c702014-02-17 18:55:34 +00002835 if (!*host_array) {
2836 return -TARGET_ENOMEM;
2837 }
aurel32e5289082009-04-18 16:16:12 +00002838 array = lock_user(VERIFY_READ, target_addr,
2839 nsems*sizeof(unsigned short), 1);
Peter Maydell69d4c702014-02-17 18:55:34 +00002840 if (!array) {
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302841 g_free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00002842 return -TARGET_EFAULT;
Peter Maydell69d4c702014-02-17 18:55:34 +00002843 }
aurel32e5289082009-04-18 16:16:12 +00002844
2845 for(i=0; i<nsems; i++) {
2846 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002847 }
aurel32e5289082009-04-18 16:16:12 +00002848 unlock_user(array, target_addr, 0);
2849
bellard579a97f2007-11-11 14:26:47 +00002850 return 0;
ths3eb6b042007-06-03 14:26:27 +00002851}
2852
aurel32e5289082009-04-18 16:16:12 +00002853static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2854 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002855{
aurel32e5289082009-04-18 16:16:12 +00002856 int nsems;
2857 unsigned short *array;
2858 union semun semun;
2859 struct semid_ds semid_ds;
2860 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002861
aurel32e5289082009-04-18 16:16:12 +00002862 semun.buf = &semid_ds;
2863
2864 ret = semctl(semid, 0, IPC_STAT, semun);
2865 if (ret == -1)
2866 return get_errno(ret);
2867
2868 nsems = semid_ds.sem_nsems;
2869
2870 array = lock_user(VERIFY_WRITE, target_addr,
2871 nsems*sizeof(unsigned short), 0);
2872 if (!array)
2873 return -TARGET_EFAULT;
2874
2875 for(i=0; i<nsems; i++) {
2876 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002877 }
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302878 g_free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00002879 unlock_user(array, target_addr, 1);
2880
bellard579a97f2007-11-11 14:26:47 +00002881 return 0;
ths3eb6b042007-06-03 14:26:27 +00002882}
2883
aurel32e5289082009-04-18 16:16:12 +00002884static inline abi_long do_semctl(int semid, int semnum, int cmd,
Stefan Weild1c002b2015-02-08 15:40:58 +01002885 abi_ulong target_arg)
ths3eb6b042007-06-03 14:26:27 +00002886{
Stefan Weild1c002b2015-02-08 15:40:58 +01002887 union target_semun target_su = { .buf = target_arg };
ths3eb6b042007-06-03 14:26:27 +00002888 union semun arg;
2889 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302890 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002891 struct seminfo seminfo;
2892 abi_long ret = -TARGET_EINVAL;
2893 abi_long err;
2894 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002895
2896 switch( cmd ) {
2897 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002898 case SETVAL:
Tom Musta5464bae2014-08-12 13:53:34 -05002899 /* In 64 bit cross-endian situations, we will erroneously pick up
2900 * the wrong half of the union for the "val" element. To rectify
2901 * this, the entire 8-byte structure is byteswapped, followed by
2902 * a swap of the 4 byte val field. In other cases, the data is
2903 * already in proper host byte order. */
2904 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
2905 target_su.buf = tswapal(target_su.buf);
2906 arg.val = tswap32(target_su.val);
2907 } else {
2908 arg.val = target_su.val;
2909 }
aurel32e5289082009-04-18 16:16:12 +00002910 ret = get_errno(semctl(semid, semnum, cmd, arg));
ths3eb6b042007-06-03 14:26:27 +00002911 break;
2912 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002913 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002914 err = target_to_host_semarray(semid, &array, target_su.array);
2915 if (err)
2916 return err;
2917 arg.array = array;
2918 ret = get_errno(semctl(semid, semnum, cmd, arg));
2919 err = host_to_target_semarray(semid, target_su.array, &array);
2920 if (err)
2921 return err;
ths3eb6b042007-06-03 14:26:27 +00002922 break;
2923 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002924 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002925 case SEM_STAT:
2926 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2927 if (err)
2928 return err;
2929 arg.buf = &dsarg;
2930 ret = get_errno(semctl(semid, semnum, cmd, arg));
2931 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2932 if (err)
2933 return err;
ths3eb6b042007-06-03 14:26:27 +00002934 break;
aurel32e5289082009-04-18 16:16:12 +00002935 case IPC_INFO:
2936 case SEM_INFO:
2937 arg.__buf = &seminfo;
2938 ret = get_errno(semctl(semid, semnum, cmd, arg));
2939 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2940 if (err)
2941 return err;
2942 break;
2943 case IPC_RMID:
2944 case GETPID:
2945 case GETNCNT:
2946 case GETZCNT:
2947 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2948 break;
ths3eb6b042007-06-03 14:26:27 +00002949 }
2950
2951 return ret;
2952}
2953
aurel32e5289082009-04-18 16:16:12 +00002954struct target_sembuf {
2955 unsigned short sem_num;
2956 short sem_op;
2957 short sem_flg;
2958};
2959
2960static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2961 abi_ulong target_addr,
2962 unsigned nsops)
2963{
2964 struct target_sembuf *target_sembuf;
2965 int i;
2966
2967 target_sembuf = lock_user(VERIFY_READ, target_addr,
2968 nsops*sizeof(struct target_sembuf), 1);
2969 if (!target_sembuf)
2970 return -TARGET_EFAULT;
2971
2972 for(i=0; i<nsops; i++) {
2973 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2974 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2975 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2976 }
2977
2978 unlock_user(target_sembuf, target_addr, 0);
2979
2980 return 0;
2981}
2982
2983static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2984{
2985 struct sembuf sops[nsops];
2986
2987 if (target_to_host_sembuf(sops, ptr, nsops))
2988 return -TARGET_EFAULT;
2989
Petar Jovanovicc7128c92013-03-21 07:57:36 +00002990 return get_errno(semop(semid, sops, nsops));
aurel32e5289082009-04-18 16:16:12 +00002991}
2992
ths1bc012f2007-06-03 14:27:49 +00002993struct target_msqid_ds
2994{
aurel321c54ff92008-10-13 21:08:44 +00002995 struct target_ipc_perm msg_perm;
2996 abi_ulong msg_stime;
2997#if TARGET_ABI_BITS == 32
2998 abi_ulong __unused1;
2999#endif
3000 abi_ulong msg_rtime;
3001#if TARGET_ABI_BITS == 32
3002 abi_ulong __unused2;
3003#endif
3004 abi_ulong msg_ctime;
3005#if TARGET_ABI_BITS == 32
3006 abi_ulong __unused3;
3007#endif
3008 abi_ulong __msg_cbytes;
3009 abi_ulong msg_qnum;
3010 abi_ulong msg_qbytes;
3011 abi_ulong msg_lspid;
3012 abi_ulong msg_lrpid;
3013 abi_ulong __unused4;
3014 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00003015};
3016
bellard579a97f2007-11-11 14:26:47 +00003017static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
3018 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00003019{
3020 struct target_msqid_ds *target_md;
3021
bellard579a97f2007-11-11 14:26:47 +00003022 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
3023 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00003024 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
3025 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003026 host_md->msg_stime = tswapal(target_md->msg_stime);
3027 host_md->msg_rtime = tswapal(target_md->msg_rtime);
3028 host_md->msg_ctime = tswapal(target_md->msg_ctime);
3029 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
3030 host_md->msg_qnum = tswapal(target_md->msg_qnum);
3031 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
3032 host_md->msg_lspid = tswapal(target_md->msg_lspid);
3033 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00003034 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00003035 return 0;
ths1bc012f2007-06-03 14:27:49 +00003036}
3037
bellard579a97f2007-11-11 14:26:47 +00003038static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
3039 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00003040{
3041 struct target_msqid_ds *target_md;
3042
bellard579a97f2007-11-11 14:26:47 +00003043 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
3044 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00003045 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
3046 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003047 target_md->msg_stime = tswapal(host_md->msg_stime);
3048 target_md->msg_rtime = tswapal(host_md->msg_rtime);
3049 target_md->msg_ctime = tswapal(host_md->msg_ctime);
3050 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
3051 target_md->msg_qnum = tswapal(host_md->msg_qnum);
3052 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
3053 target_md->msg_lspid = tswapal(host_md->msg_lspid);
3054 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00003055 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00003056 return 0;
ths1bc012f2007-06-03 14:27:49 +00003057}
3058
aurel321c54ff92008-10-13 21:08:44 +00003059struct target_msginfo {
3060 int msgpool;
3061 int msgmap;
3062 int msgmax;
3063 int msgmnb;
3064 int msgmni;
3065 int msgssz;
3066 int msgtql;
3067 unsigned short int msgseg;
3068};
3069
3070static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
3071 struct msginfo *host_msginfo)
3072{
3073 struct target_msginfo *target_msginfo;
3074 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
3075 return -TARGET_EFAULT;
3076 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
3077 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
3078 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
3079 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
3080 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
3081 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
3082 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
3083 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
3084 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00003085 return 0;
aurel321c54ff92008-10-13 21:08:44 +00003086}
3087
3088static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00003089{
3090 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00003091 struct msginfo msginfo;
3092 abi_long ret = -TARGET_EINVAL;
3093
3094 cmd &= 0xff;
3095
3096 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00003097 case IPC_STAT:
3098 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00003099 case MSG_STAT:
3100 if (target_to_host_msqid_ds(&dsarg,ptr))
3101 return -TARGET_EFAULT;
3102 ret = get_errno(msgctl(msgid, cmd, &dsarg));
3103 if (host_to_target_msqid_ds(ptr,&dsarg))
3104 return -TARGET_EFAULT;
3105 break;
3106 case IPC_RMID:
3107 ret = get_errno(msgctl(msgid, cmd, NULL));
3108 break;
3109 case IPC_INFO:
3110 case MSG_INFO:
3111 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
3112 if (host_to_target_msginfo(ptr, &msginfo))
3113 return -TARGET_EFAULT;
3114 break;
ths1bc012f2007-06-03 14:27:49 +00003115 }
aurel321c54ff92008-10-13 21:08:44 +00003116
ths1bc012f2007-06-03 14:27:49 +00003117 return ret;
3118}
3119
3120struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00003121 abi_long mtype;
3122 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00003123};
3124
blueswir1992f48a2007-10-14 16:27:31 +00003125static inline abi_long do_msgsnd(int msqid, abi_long msgp,
Tom Mustaedcc5f92014-08-12 13:53:37 -05003126 ssize_t msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00003127{
3128 struct target_msgbuf *target_mb;
3129 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00003130 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00003131
Tom Mustaedcc5f92014-08-12 13:53:37 -05003132 if (msgsz < 0) {
3133 return -TARGET_EINVAL;
3134 }
3135
bellard579a97f2007-11-11 14:26:47 +00003136 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
3137 return -TARGET_EFAULT;
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303138 host_mb = g_try_malloc(msgsz + sizeof(long));
zhanghailiang29e03fc2014-08-14 15:29:18 +08003139 if (!host_mb) {
3140 unlock_user_struct(target_mb, msgp, 0);
3141 return -TARGET_ENOMEM;
3142 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003143 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00003144 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00003145 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303146 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00003147 unlock_user_struct(target_mb, msgp, 0);
3148
3149 return ret;
3150}
3151
blueswir1992f48a2007-10-14 16:27:31 +00003152static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00003153 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00003154 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00003155{
3156 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00003157 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00003158 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00003159 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00003160
bellard579a97f2007-11-11 14:26:47 +00003161 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
3162 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00003163
Jim Meyering0d07fe42012-08-22 13:55:53 +02003164 host_mb = g_malloc(msgsz+sizeof(long));
Laurent Vivier79dd77d2012-12-20 11:00:11 +00003165 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
aurel321c54ff92008-10-13 21:08:44 +00003166
bellard579a97f2007-11-11 14:26:47 +00003167 if (ret > 0) {
3168 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
3169 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
3170 if (!target_mtext) {
3171 ret = -TARGET_EFAULT;
3172 goto end;
3173 }
aurel321c54ff92008-10-13 21:08:44 +00003174 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00003175 unlock_user(target_mtext, target_mtext_addr, ret);
3176 }
aurel321c54ff92008-10-13 21:08:44 +00003177
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003178 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00003179
bellard579a97f2007-11-11 14:26:47 +00003180end:
3181 if (target_mb)
3182 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02003183 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00003184 return ret;
3185}
3186
Riku Voipio88a8c982009-04-03 10:42:00 +03003187static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
3188 abi_ulong target_addr)
3189{
3190 struct target_shmid_ds *target_sd;
3191
3192 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3193 return -TARGET_EFAULT;
3194 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
3195 return -TARGET_EFAULT;
3196 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3197 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
3198 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3199 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3200 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3201 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3202 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3203 unlock_user_struct(target_sd, target_addr, 0);
3204 return 0;
3205}
3206
3207static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
3208 struct shmid_ds *host_sd)
3209{
3210 struct target_shmid_ds *target_sd;
3211
3212 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3213 return -TARGET_EFAULT;
3214 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
3215 return -TARGET_EFAULT;
3216 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3217 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
3218 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3219 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3220 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3221 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3222 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3223 unlock_user_struct(target_sd, target_addr, 1);
3224 return 0;
3225}
3226
3227struct target_shminfo {
3228 abi_ulong shmmax;
3229 abi_ulong shmmin;
3230 abi_ulong shmmni;
3231 abi_ulong shmseg;
3232 abi_ulong shmall;
3233};
3234
3235static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
3236 struct shminfo *host_shminfo)
3237{
3238 struct target_shminfo *target_shminfo;
3239 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3240 return -TARGET_EFAULT;
3241 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3242 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3243 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3244 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3245 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3246 unlock_user_struct(target_shminfo, target_addr, 1);
3247 return 0;
3248}
3249
3250struct target_shm_info {
3251 int used_ids;
3252 abi_ulong shm_tot;
3253 abi_ulong shm_rss;
3254 abi_ulong shm_swp;
3255 abi_ulong swap_attempts;
3256 abi_ulong swap_successes;
3257};
3258
3259static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3260 struct shm_info *host_shm_info)
3261{
3262 struct target_shm_info *target_shm_info;
3263 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3264 return -TARGET_EFAULT;
3265 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3266 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3267 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3268 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3269 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3270 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3271 unlock_user_struct(target_shm_info, target_addr, 1);
3272 return 0;
3273}
3274
3275static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3276{
3277 struct shmid_ds dsarg;
3278 struct shminfo shminfo;
3279 struct shm_info shm_info;
3280 abi_long ret = -TARGET_EINVAL;
3281
3282 cmd &= 0xff;
3283
3284 switch(cmd) {
3285 case IPC_STAT:
3286 case IPC_SET:
3287 case SHM_STAT:
3288 if (target_to_host_shmid_ds(&dsarg, buf))
3289 return -TARGET_EFAULT;
3290 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3291 if (host_to_target_shmid_ds(buf, &dsarg))
3292 return -TARGET_EFAULT;
3293 break;
3294 case IPC_INFO:
3295 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3296 if (host_to_target_shminfo(buf, &shminfo))
3297 return -TARGET_EFAULT;
3298 break;
3299 case SHM_INFO:
3300 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3301 if (host_to_target_shm_info(buf, &shm_info))
3302 return -TARGET_EFAULT;
3303 break;
3304 case IPC_RMID:
3305 case SHM_LOCK:
3306 case SHM_UNLOCK:
3307 ret = get_errno(shmctl(shmid, cmd, NULL));
3308 break;
3309 }
3310
3311 return ret;
3312}
3313
3314static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3315{
3316 abi_long raddr;
3317 void *host_raddr;
3318 struct shmid_ds shm_info;
3319 int i,ret;
3320
3321 /* find out the length of the shared memory segment */
3322 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3323 if (is_error(ret)) {
3324 /* can't get length, bail out */
3325 return ret;
3326 }
3327
3328 mmap_lock();
3329
3330 if (shmaddr)
3331 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3332 else {
3333 abi_ulong mmap_start;
3334
3335 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3336
3337 if (mmap_start == -1) {
3338 errno = ENOMEM;
3339 host_raddr = (void *)-1;
3340 } else
3341 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3342 }
3343
3344 if (host_raddr == (void *)-1) {
3345 mmap_unlock();
3346 return get_errno((long)host_raddr);
3347 }
3348 raddr=h2g((unsigned long)host_raddr);
3349
3350 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3351 PAGE_VALID | PAGE_READ |
3352 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3353
3354 for (i = 0; i < N_SHM_REGIONS; i++) {
Peter Maydellb6e17872016-02-09 15:57:11 +00003355 if (!shm_regions[i].in_use) {
3356 shm_regions[i].in_use = true;
Riku Voipio88a8c982009-04-03 10:42:00 +03003357 shm_regions[i].start = raddr;
3358 shm_regions[i].size = shm_info.shm_segsz;
3359 break;
3360 }
3361 }
3362
3363 mmap_unlock();
3364 return raddr;
3365
3366}
3367
3368static inline abi_long do_shmdt(abi_ulong shmaddr)
3369{
3370 int i;
3371
3372 for (i = 0; i < N_SHM_REGIONS; ++i) {
Peter Maydellb6e17872016-02-09 15:57:11 +00003373 if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
3374 shm_regions[i].in_use = false;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09003375 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03003376 break;
3377 }
3378 }
3379
3380 return get_errno(shmdt(g2h(shmaddr)));
3381}
3382
aurel321c54ff92008-10-13 21:08:44 +00003383#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00003384/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00003385/* do_ipc() must return target values and target errnos. */
Tom Musta37ed0952014-08-12 13:53:35 -05003386static abi_long do_ipc(unsigned int call, abi_long first,
3387 abi_long second, abi_long third,
blueswir1992f48a2007-10-14 16:27:31 +00003388 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00003389{
3390 int version;
blueswir1992f48a2007-10-14 16:27:31 +00003391 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00003392
3393 version = call >> 16;
3394 call &= 0xffff;
3395
3396 switch (call) {
thsfa294812007-02-02 22:05:00 +00003397 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00003398 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00003399 break;
3400
3401 case IPCOP_semget:
3402 ret = get_errno(semget(first, second, third));
3403 break;
3404
Tom Musta5d2fa8e2014-08-12 13:53:33 -05003405 case IPCOP_semctl: {
3406 /* The semun argument to semctl is passed by value, so dereference the
3407 * ptr argument. */
3408 abi_ulong atptr;
Tom Musta37ed0952014-08-12 13:53:35 -05003409 get_user_ual(atptr, ptr);
Stefan Weild1c002b2015-02-08 15:40:58 +01003410 ret = do_semctl(first, second, third, atptr);
thsfa294812007-02-02 22:05:00 +00003411 break;
Tom Musta5d2fa8e2014-08-12 13:53:33 -05003412 }
thsd96372e2007-02-02 22:05:44 +00003413
aurel321c54ff92008-10-13 21:08:44 +00003414 case IPCOP_msgget:
3415 ret = get_errno(msgget(first, second));
3416 break;
thsd96372e2007-02-02 22:05:44 +00003417
aurel321c54ff92008-10-13 21:08:44 +00003418 case IPCOP_msgsnd:
3419 ret = do_msgsnd(first, ptr, second, third);
3420 break;
thsd96372e2007-02-02 22:05:44 +00003421
aurel321c54ff92008-10-13 21:08:44 +00003422 case IPCOP_msgctl:
3423 ret = do_msgctl(first, second, ptr);
3424 break;
thsd96372e2007-02-02 22:05:44 +00003425
aurel321c54ff92008-10-13 21:08:44 +00003426 case IPCOP_msgrcv:
3427 switch (version) {
3428 case 0:
3429 {
3430 struct target_ipc_kludge {
3431 abi_long msgp;
3432 abi_long msgtyp;
3433 } *tmp;
thsd96372e2007-02-02 22:05:44 +00003434
aurel321c54ff92008-10-13 21:08:44 +00003435 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3436 ret = -TARGET_EFAULT;
3437 break;
ths1bc012f2007-06-03 14:27:49 +00003438 }
aurel321c54ff92008-10-13 21:08:44 +00003439
Laurent Vivier79dd77d2012-12-20 11:00:11 +00003440 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
aurel321c54ff92008-10-13 21:08:44 +00003441
3442 unlock_user_struct(tmp, ptr, 0);
3443 break;
3444 }
3445 default:
3446 ret = do_msgrcv(first, ptr, second, fifth, third);
3447 }
3448 break;
thsd96372e2007-02-02 22:05:44 +00003449
bellard8853f862004-02-22 14:57:26 +00003450 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03003451 switch (version) {
3452 default:
bellard5a4a8982007-11-11 17:39:18 +00003453 {
3454 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03003455 raddr = do_shmat(first, ptr, second);
3456 if (is_error(raddr))
3457 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00003458 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00003459 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03003460 break;
3461 }
3462 case 1:
3463 ret = -TARGET_EINVAL;
3464 break;
bellard5a4a8982007-11-11 17:39:18 +00003465 }
bellard8853f862004-02-22 14:57:26 +00003466 break;
3467 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03003468 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00003469 break;
3470
3471 case IPCOP_shmget:
3472 /* IPC_* flag values are the same on all linux platforms */
3473 ret = get_errno(shmget(first, second, third));
3474 break;
3475
3476 /* IPC_* and SHM_* command values are the same on all linux platforms */
3477 case IPCOP_shmctl:
Petar Jovanovica2926782013-10-30 14:46:32 +01003478 ret = do_shmctl(first, second, ptr);
bellard8853f862004-02-22 14:57:26 +00003479 break;
3480 default:
j_mayer32407102007-09-26 23:01:49 +00003481 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00003482 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00003483 break;
3484 }
3485 return ret;
3486}
j_mayer32407102007-09-26 23:01:49 +00003487#endif
bellard8853f862004-02-22 14:57:26 +00003488
bellard31e31b82003-02-18 22:55:36 +00003489/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00003490
Blue Swirl001faf32009-05-13 17:53:17 +00003491#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00003492#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3493enum {
3494#include "syscall_types.h"
Alexander Graf8be656b2015-05-06 23:47:32 +02003495STRUCT_MAX
bellard31e31b82003-02-18 22:55:36 +00003496};
3497#undef STRUCT
3498#undef STRUCT_SPECIAL
3499
Blue Swirl001faf32009-05-13 17:53:17 +00003500#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00003501#define STRUCT_SPECIAL(name)
3502#include "syscall_types.h"
3503#undef STRUCT
3504#undef STRUCT_SPECIAL
3505
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003506typedef struct IOCTLEntry IOCTLEntry;
3507
3508typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003509 int fd, int cmd, abi_long arg);
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003510
3511struct IOCTLEntry {
Ed Swierk9c6bf9c2014-12-16 12:55:31 -08003512 int target_cmd;
bellard2ab83ea2003-06-15 19:56:46 +00003513 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00003514 const char *name;
3515 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003516 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003517 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003518};
bellard31e31b82003-02-18 22:55:36 +00003519
3520#define IOC_R 0x0001
3521#define IOC_W 0x0002
3522#define IOC_RW (IOC_R | IOC_W)
3523
3524#define MAX_STRUCT_SIZE 4096
3525
Peter Maydelldace20d2011-01-10 13:11:24 +00003526#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003527/* So fiemap access checks don't overflow on 32 bit systems.
3528 * This is very slightly smaller than the limit imposed by
3529 * the underlying kernel.
3530 */
3531#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3532 / sizeof(struct fiemap_extent))
3533
3534static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003535 int fd, int cmd, abi_long arg)
Peter Maydell285da2b2011-01-06 15:04:18 +00003536{
3537 /* The parameter for this ioctl is a struct fiemap followed
3538 * by an array of struct fiemap_extent whose size is set
3539 * in fiemap->fm_extent_count. The array is filled in by the
3540 * ioctl.
3541 */
3542 int target_size_in, target_size_out;
3543 struct fiemap *fm;
3544 const argtype *arg_type = ie->arg_type;
3545 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3546 void *argptr, *p;
3547 abi_long ret;
3548 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3549 uint32_t outbufsz;
3550 int free_fm = 0;
3551
3552 assert(arg_type[0] == TYPE_PTR);
3553 assert(ie->access == IOC_RW);
3554 arg_type++;
3555 target_size_in = thunk_type_size(arg_type, 0);
3556 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3557 if (!argptr) {
3558 return -TARGET_EFAULT;
3559 }
3560 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3561 unlock_user(argptr, arg, 0);
3562 fm = (struct fiemap *)buf_temp;
3563 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3564 return -TARGET_EINVAL;
3565 }
3566
3567 outbufsz = sizeof (*fm) +
3568 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3569
3570 if (outbufsz > MAX_STRUCT_SIZE) {
3571 /* We can't fit all the extents into the fixed size buffer.
3572 * Allocate one that is large enough and use it instead.
3573 */
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303574 fm = g_try_malloc(outbufsz);
Peter Maydell285da2b2011-01-06 15:04:18 +00003575 if (!fm) {
3576 return -TARGET_ENOMEM;
3577 }
3578 memcpy(fm, buf_temp, sizeof(struct fiemap));
3579 free_fm = 1;
3580 }
3581 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3582 if (!is_error(ret)) {
3583 target_size_out = target_size_in;
3584 /* An extent_count of 0 means we were only counting the extents
3585 * so there are no structs to copy
3586 */
3587 if (fm->fm_extent_count != 0) {
3588 target_size_out += fm->fm_mapped_extents * extent_size;
3589 }
3590 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3591 if (!argptr) {
3592 ret = -TARGET_EFAULT;
3593 } else {
3594 /* Convert the struct fiemap */
3595 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3596 if (fm->fm_extent_count != 0) {
3597 p = argptr + target_size_in;
3598 /* ...and then all the struct fiemap_extents */
3599 for (i = 0; i < fm->fm_mapped_extents; i++) {
3600 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3601 THUNK_TARGET);
3602 p += extent_size;
3603 }
3604 }
3605 unlock_user(argptr, arg, target_size_out);
3606 }
3607 }
3608 if (free_fm) {
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303609 g_free(fm);
Peter Maydell285da2b2011-01-06 15:04:18 +00003610 }
3611 return ret;
3612}
Peter Maydelldace20d2011-01-10 13:11:24 +00003613#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003614
Laurent Vivier059c2f22011-03-30 00:12:12 +02003615static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003616 int fd, int cmd, abi_long arg)
Laurent Vivier059c2f22011-03-30 00:12:12 +02003617{
3618 const argtype *arg_type = ie->arg_type;
3619 int target_size;
3620 void *argptr;
3621 int ret;
3622 struct ifconf *host_ifconf;
3623 uint32_t outbufsz;
3624 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3625 int target_ifreq_size;
3626 int nb_ifreq;
3627 int free_buf = 0;
3628 int i;
3629 int target_ifc_len;
3630 abi_long target_ifc_buf;
3631 int host_ifc_len;
3632 char *host_ifc_buf;
3633
3634 assert(arg_type[0] == TYPE_PTR);
3635 assert(ie->access == IOC_RW);
3636
3637 arg_type++;
3638 target_size = thunk_type_size(arg_type, 0);
3639
3640 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3641 if (!argptr)
3642 return -TARGET_EFAULT;
3643 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3644 unlock_user(argptr, arg, 0);
3645
3646 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3647 target_ifc_len = host_ifconf->ifc_len;
3648 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3649
3650 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3651 nb_ifreq = target_ifc_len / target_ifreq_size;
3652 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3653
3654 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3655 if (outbufsz > MAX_STRUCT_SIZE) {
3656 /* We can't fit all the extents into the fixed size buffer.
3657 * Allocate one that is large enough and use it instead.
3658 */
3659 host_ifconf = malloc(outbufsz);
3660 if (!host_ifconf) {
3661 return -TARGET_ENOMEM;
3662 }
3663 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3664 free_buf = 1;
3665 }
3666 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3667
3668 host_ifconf->ifc_len = host_ifc_len;
3669 host_ifconf->ifc_buf = host_ifc_buf;
3670
3671 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3672 if (!is_error(ret)) {
3673 /* convert host ifc_len to target ifc_len */
3674
3675 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3676 target_ifc_len = nb_ifreq * target_ifreq_size;
3677 host_ifconf->ifc_len = target_ifc_len;
3678
3679 /* restore target ifc_buf */
3680
3681 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3682
3683 /* copy struct ifconf to target user */
3684
3685 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3686 if (!argptr)
3687 return -TARGET_EFAULT;
3688 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3689 unlock_user(argptr, arg, target_size);
3690
3691 /* copy ifreq[] to target user */
3692
3693 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3694 for (i = 0; i < nb_ifreq ; i++) {
3695 thunk_convert(argptr + i * target_ifreq_size,
3696 host_ifc_buf + i * sizeof(struct ifreq),
3697 ifreq_arg_type, THUNK_TARGET);
3698 }
3699 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3700 }
3701
3702 if (free_buf) {
3703 free(host_ifconf);
3704 }
3705
3706 return ret;
3707}
3708
Alexander Graf56e904e2012-01-31 18:42:06 +01003709static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003710 int cmd, abi_long arg)
Alexander Graf56e904e2012-01-31 18:42:06 +01003711{
3712 void *argptr;
3713 struct dm_ioctl *host_dm;
3714 abi_long guest_data;
3715 uint32_t guest_data_size;
3716 int target_size;
3717 const argtype *arg_type = ie->arg_type;
3718 abi_long ret;
3719 void *big_buf = NULL;
3720 char *host_data;
3721
3722 arg_type++;
3723 target_size = thunk_type_size(arg_type, 0);
3724 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3725 if (!argptr) {
3726 ret = -TARGET_EFAULT;
3727 goto out;
3728 }
3729 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3730 unlock_user(argptr, arg, 0);
3731
3732 /* buf_temp is too small, so fetch things into a bigger buffer */
3733 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3734 memcpy(big_buf, buf_temp, target_size);
3735 buf_temp = big_buf;
3736 host_dm = big_buf;
3737
3738 guest_data = arg + host_dm->data_start;
3739 if ((guest_data - arg) < 0) {
3740 ret = -EINVAL;
3741 goto out;
3742 }
3743 guest_data_size = host_dm->data_size - host_dm->data_start;
3744 host_data = (char*)host_dm + host_dm->data_start;
3745
3746 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3747 switch (ie->host_cmd) {
3748 case DM_REMOVE_ALL:
3749 case DM_LIST_DEVICES:
3750 case DM_DEV_CREATE:
3751 case DM_DEV_REMOVE:
3752 case DM_DEV_SUSPEND:
3753 case DM_DEV_STATUS:
3754 case DM_DEV_WAIT:
3755 case DM_TABLE_STATUS:
3756 case DM_TABLE_CLEAR:
3757 case DM_TABLE_DEPS:
3758 case DM_LIST_VERSIONS:
3759 /* no input data */
3760 break;
3761 case DM_DEV_RENAME:
3762 case DM_DEV_SET_GEOMETRY:
3763 /* data contains only strings */
3764 memcpy(host_data, argptr, guest_data_size);
3765 break;
3766 case DM_TARGET_MSG:
3767 memcpy(host_data, argptr, guest_data_size);
3768 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3769 break;
3770 case DM_TABLE_LOAD:
3771 {
3772 void *gspec = argptr;
3773 void *cur_data = host_data;
3774 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3775 int spec_size = thunk_type_size(arg_type, 0);
3776 int i;
3777
3778 for (i = 0; i < host_dm->target_count; i++) {
3779 struct dm_target_spec *spec = cur_data;
3780 uint32_t next;
3781 int slen;
3782
3783 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3784 slen = strlen((char*)gspec + spec_size) + 1;
3785 next = spec->next;
3786 spec->next = sizeof(*spec) + slen;
3787 strcpy((char*)&spec[1], gspec + spec_size);
3788 gspec += next;
3789 cur_data += spec->next;
3790 }
3791 break;
3792 }
3793 default:
3794 ret = -TARGET_EINVAL;
Chen Gang Sdec04732015-01-25 08:00:42 +08003795 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01003796 goto out;
3797 }
3798 unlock_user(argptr, guest_data, 0);
3799
3800 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3801 if (!is_error(ret)) {
3802 guest_data = arg + host_dm->data_start;
3803 guest_data_size = host_dm->data_size - host_dm->data_start;
3804 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3805 switch (ie->host_cmd) {
3806 case DM_REMOVE_ALL:
3807 case DM_DEV_CREATE:
3808 case DM_DEV_REMOVE:
3809 case DM_DEV_RENAME:
3810 case DM_DEV_SUSPEND:
3811 case DM_DEV_STATUS:
3812 case DM_TABLE_LOAD:
3813 case DM_TABLE_CLEAR:
3814 case DM_TARGET_MSG:
3815 case DM_DEV_SET_GEOMETRY:
3816 /* no return data */
3817 break;
3818 case DM_LIST_DEVICES:
3819 {
3820 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3821 uint32_t remaining_data = guest_data_size;
3822 void *cur_data = argptr;
3823 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3824 int nl_size = 12; /* can't use thunk_size due to alignment */
3825
3826 while (1) {
3827 uint32_t next = nl->next;
3828 if (next) {
3829 nl->next = nl_size + (strlen(nl->name) + 1);
3830 }
3831 if (remaining_data < nl->next) {
3832 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3833 break;
3834 }
3835 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3836 strcpy(cur_data + nl_size, nl->name);
3837 cur_data += nl->next;
3838 remaining_data -= nl->next;
3839 if (!next) {
3840 break;
3841 }
3842 nl = (void*)nl + next;
3843 }
3844 break;
3845 }
3846 case DM_DEV_WAIT:
3847 case DM_TABLE_STATUS:
3848 {
3849 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3850 void *cur_data = argptr;
3851 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3852 int spec_size = thunk_type_size(arg_type, 0);
3853 int i;
3854
3855 for (i = 0; i < host_dm->target_count; i++) {
3856 uint32_t next = spec->next;
3857 int slen = strlen((char*)&spec[1]) + 1;
3858 spec->next = (cur_data - argptr) + spec_size + slen;
3859 if (guest_data_size < spec->next) {
3860 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3861 break;
3862 }
3863 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3864 strcpy(cur_data + spec_size, (char*)&spec[1]);
3865 cur_data = argptr + spec->next;
3866 spec = (void*)host_dm + host_dm->data_start + next;
3867 }
3868 break;
3869 }
3870 case DM_TABLE_DEPS:
3871 {
3872 void *hdata = (void*)host_dm + host_dm->data_start;
3873 int count = *(uint32_t*)hdata;
3874 uint64_t *hdev = hdata + 8;
3875 uint64_t *gdev = argptr + 8;
3876 int i;
3877
3878 *(uint32_t*)argptr = tswap32(count);
3879 for (i = 0; i < count; i++) {
3880 *gdev = tswap64(*hdev);
3881 gdev++;
3882 hdev++;
3883 }
3884 break;
3885 }
3886 case DM_LIST_VERSIONS:
3887 {
3888 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3889 uint32_t remaining_data = guest_data_size;
3890 void *cur_data = argptr;
3891 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3892 int vers_size = thunk_type_size(arg_type, 0);
3893
3894 while (1) {
3895 uint32_t next = vers->next;
3896 if (next) {
3897 vers->next = vers_size + (strlen(vers->name) + 1);
3898 }
3899 if (remaining_data < vers->next) {
3900 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3901 break;
3902 }
3903 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3904 strcpy(cur_data + vers_size, vers->name);
3905 cur_data += vers->next;
3906 remaining_data -= vers->next;
3907 if (!next) {
3908 break;
3909 }
3910 vers = (void*)vers + next;
3911 }
3912 break;
3913 }
3914 default:
Chen Gang Sdec04732015-01-25 08:00:42 +08003915 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01003916 ret = -TARGET_EINVAL;
3917 goto out;
3918 }
3919 unlock_user(argptr, guest_data, guest_data_size);
3920
3921 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3922 if (!argptr) {
3923 ret = -TARGET_EFAULT;
3924 goto out;
3925 }
3926 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3927 unlock_user(argptr, arg, target_size);
3928 }
3929out:
Stefan Weilad11ad72012-09-04 22:14:19 +02003930 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01003931 return ret;
3932}
3933
Alexander Grafa59b5e32014-08-22 13:15:50 +02003934static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003935 int cmd, abi_long arg)
Alexander Grafa59b5e32014-08-22 13:15:50 +02003936{
3937 void *argptr;
3938 int target_size;
3939 const argtype *arg_type = ie->arg_type;
3940 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
3941 abi_long ret;
3942
3943 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
3944 struct blkpg_partition host_part;
3945
3946 /* Read and convert blkpg */
3947 arg_type++;
3948 target_size = thunk_type_size(arg_type, 0);
3949 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3950 if (!argptr) {
3951 ret = -TARGET_EFAULT;
3952 goto out;
3953 }
3954 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3955 unlock_user(argptr, arg, 0);
3956
3957 switch (host_blkpg->op) {
3958 case BLKPG_ADD_PARTITION:
3959 case BLKPG_DEL_PARTITION:
3960 /* payload is struct blkpg_partition */
3961 break;
3962 default:
3963 /* Unknown opcode */
3964 ret = -TARGET_EINVAL;
3965 goto out;
3966 }
3967
3968 /* Read and convert blkpg->data */
3969 arg = (abi_long)(uintptr_t)host_blkpg->data;
3970 target_size = thunk_type_size(part_arg_type, 0);
3971 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3972 if (!argptr) {
3973 ret = -TARGET_EFAULT;
3974 goto out;
3975 }
3976 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
3977 unlock_user(argptr, arg, 0);
3978
3979 /* Swizzle the data pointer to our local copy and call! */
3980 host_blkpg->data = &host_part;
3981 ret = get_errno(ioctl(fd, ie->host_cmd, host_blkpg));
3982
3983out:
3984 return ret;
3985}
3986
Laurent Vivier7ff7b662013-07-02 14:04:12 +01003987static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02003988 int fd, int cmd, abi_long arg)
Laurent Vivier7ff7b662013-07-02 14:04:12 +01003989{
3990 const argtype *arg_type = ie->arg_type;
3991 const StructEntry *se;
3992 const argtype *field_types;
3993 const int *dst_offsets, *src_offsets;
3994 int target_size;
3995 void *argptr;
3996 abi_ulong *target_rt_dev_ptr;
3997 unsigned long *host_rt_dev_ptr;
3998 abi_long ret;
3999 int i;
4000
4001 assert(ie->access == IOC_W);
4002 assert(*arg_type == TYPE_PTR);
4003 arg_type++;
4004 assert(*arg_type == TYPE_STRUCT);
4005 target_size = thunk_type_size(arg_type, 0);
4006 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4007 if (!argptr) {
4008 return -TARGET_EFAULT;
4009 }
4010 arg_type++;
4011 assert(*arg_type == (int)STRUCT_rtentry);
4012 se = struct_entries + *arg_type++;
4013 assert(se->convert[0] == NULL);
4014 /* convert struct here to be able to catch rt_dev string */
4015 field_types = se->field_types;
4016 dst_offsets = se->field_offsets[THUNK_HOST];
4017 src_offsets = se->field_offsets[THUNK_TARGET];
4018 for (i = 0; i < se->nb_fields; i++) {
4019 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
4020 assert(*field_types == TYPE_PTRVOID);
4021 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
4022 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
4023 if (*target_rt_dev_ptr != 0) {
4024 *host_rt_dev_ptr = (unsigned long)lock_user_string(
4025 tswapal(*target_rt_dev_ptr));
4026 if (!*host_rt_dev_ptr) {
4027 unlock_user(argptr, arg, 0);
4028 return -TARGET_EFAULT;
4029 }
4030 } else {
4031 *host_rt_dev_ptr = 0;
4032 }
4033 field_types++;
4034 continue;
4035 }
4036 field_types = thunk_convert(buf_temp + dst_offsets[i],
4037 argptr + src_offsets[i],
4038 field_types, THUNK_HOST);
4039 }
4040 unlock_user(argptr, arg, 0);
4041
4042 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4043 if (*host_rt_dev_ptr != 0) {
4044 unlock_user((void *)*host_rt_dev_ptr,
4045 *target_rt_dev_ptr, 0);
4046 }
4047 return ret;
4048}
4049
Paul Burtonca56f5b2014-06-22 11:25:47 +01004050static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004051 int fd, int cmd, abi_long arg)
Paul Burtonca56f5b2014-06-22 11:25:47 +01004052{
4053 int sig = target_to_host_signal(arg);
4054 return get_errno(ioctl(fd, ie->host_cmd, sig));
4055}
4056
blueswir19f106a72008-10-05 10:52:52 +00004057static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00004058#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00004059 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
4060#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
4061 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00004062#include "ioctls.h"
4063 { 0, 0, },
4064};
4065
pbrook53a59602006-03-25 19:31:22 +00004066/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00004067/* do_ioctl() Must return target values and target errnos. */
Laurent Vivier45c874e2015-06-16 00:35:28 +02004068static abi_long do_ioctl(int fd, int cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00004069{
4070 const IOCTLEntry *ie;
4071 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00004072 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004073 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00004074 int target_size;
4075 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00004076
4077 ie = ioctl_entries;
4078 for(;;) {
4079 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00004080 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00004081 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00004082 }
4083 if (ie->target_cmd == cmd)
4084 break;
4085 ie++;
4086 }
4087 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00004088#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00004089 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00004090#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00004091 if (ie->do_ioctl) {
4092 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
4093 }
4094
bellard31e31b82003-02-18 22:55:36 +00004095 switch(arg_type[0]) {
4096 case TYPE_NULL:
4097 /* no argument */
4098 ret = get_errno(ioctl(fd, ie->host_cmd));
4099 break;
4100 case TYPE_PTRVOID:
4101 case TYPE_INT:
bellard31e31b82003-02-18 22:55:36 +00004102 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
4103 break;
4104 case TYPE_PTR:
4105 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00004106 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00004107 switch(ie->access) {
4108 case IOC_R:
4109 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4110 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004111 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4112 if (!argptr)
4113 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004114 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4115 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00004116 }
4117 break;
4118 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00004119 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4120 if (!argptr)
4121 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004122 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4123 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00004124 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4125 break;
4126 default:
4127 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00004128 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4129 if (!argptr)
4130 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004131 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4132 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00004133 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4134 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004135 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4136 if (!argptr)
4137 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004138 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4139 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00004140 }
4141 break;
4142 }
4143 break;
4144 default:
j_mayer32407102007-09-26 23:01:49 +00004145 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
4146 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00004147 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00004148 break;
4149 }
4150 return ret;
4151}
4152
blueswir1b39bc502008-10-05 10:51:10 +00004153static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004154 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
4155 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
4156 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
4157 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
4158 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
4159 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
4160 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
4161 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
4162 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
4163 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
4164 { TARGET_IXON, TARGET_IXON, IXON, IXON },
4165 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
4166 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
4167 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
4168 { 0, 0, 0, 0 }
4169};
4170
blueswir1b39bc502008-10-05 10:51:10 +00004171static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004172 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
4173 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
4174 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
4175 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
4176 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
4177 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
4178 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
4179 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
4180 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
4181 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
4182 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
4183 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
4184 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
4185 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
4186 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
4187 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
4188 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
4189 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
4190 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
4191 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
4192 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
4193 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
4194 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
4195 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
4196 { 0, 0, 0, 0 }
4197};
4198
blueswir1b39bc502008-10-05 10:51:10 +00004199static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004200 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
4201 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
4202 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
4203 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
4204 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
4205 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
4206 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
4207 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
4208 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
4209 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
4210 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
4211 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
4212 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
4213 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
4214 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
4215 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
4216 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
4217 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
4218 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
4219 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
4220 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
4221 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
4222 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
4223 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
4224 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
4225 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
4226 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
4227 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
4228 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
4229 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
4230 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
4231 { 0, 0, 0, 0 }
4232};
4233
blueswir1b39bc502008-10-05 10:51:10 +00004234static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004235 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
4236 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
4237 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
4238 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
4239 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
4240 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
4241 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
4242 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
4243 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
4244 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
4245 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
4246 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
4247 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
4248 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
4249 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
4250 { 0, 0, 0, 0 }
4251};
4252
4253static void target_to_host_termios (void *dst, const void *src)
4254{
4255 struct host_termios *host = dst;
4256 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00004257
ths5fafdf22007-09-16 21:08:06 +00004258 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00004259 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004260 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00004261 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004262 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00004263 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004264 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00004265 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
4266 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00004267
Arnaud Patard44607122009-04-21 17:39:08 +03004268 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00004269 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
4270 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00004271 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00004272 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00004273 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00004274 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00004275 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00004276 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00004277 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00004278 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
4279 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00004280 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
4281 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
4282 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
4283 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
4284 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00004285 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00004286}
ths3b46e622007-09-17 08:09:54 +00004287
bellard31e31b82003-02-18 22:55:36 +00004288static void host_to_target_termios (void *dst, const void *src)
4289{
4290 struct target_termios *target = dst;
4291 const struct host_termios *host = src;
4292
ths5fafdf22007-09-16 21:08:06 +00004293 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00004294 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004295 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00004296 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004297 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00004298 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004299 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00004300 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
4301 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00004302
Arnaud Patard44607122009-04-21 17:39:08 +03004303 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00004304 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
4305 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
4306 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
4307 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
4308 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
4309 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
4310 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
4311 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
4312 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
4313 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
4314 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
4315 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
4316 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
4317 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
4318 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
4319 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
4320 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
4321}
4322
blueswir18e853dc2008-10-05 10:49:32 +00004323static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00004324 .convert = { host_to_target_termios, target_to_host_termios },
4325 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
4326 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
4327};
4328
bellard5286db72003-06-05 00:57:30 +00004329static bitmask_transtbl mmap_flags_tbl[] = {
4330 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
4331 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
4332 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
4333 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
4334 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
4335 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
4336 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
4337 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
Christophe Lyone8efd8e2014-02-03 17:04:32 +01004338 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, MAP_NORESERVE,
4339 MAP_NORESERVE },
bellard5286db72003-06-05 00:57:30 +00004340 { 0, 0, 0, 0 }
4341};
4342
bellard2ab83ea2003-06-15 19:56:46 +00004343#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00004344
4345/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00004346static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00004347
bellard03acab62007-11-11 14:57:14 +00004348static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00004349{
4350 int size;
pbrook53a59602006-03-25 19:31:22 +00004351 void *p;
bellard6dbad632003-03-16 18:05:05 +00004352
4353 if (!ldt_table)
4354 return 0;
4355 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
4356 if (size > bytecount)
4357 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00004358 p = lock_user(VERIFY_WRITE, ptr, size, 0);
4359 if (!p)
bellard03acab62007-11-11 14:57:14 +00004360 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00004361 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00004362 memcpy(p, ldt_table, size);
4363 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00004364 return size;
4365}
4366
4367/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00004368static abi_long write_ldt(CPUX86State *env,
4369 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00004370{
4371 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00004372 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00004373 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00004374 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00004375 uint32_t *lp, entry_1, entry_2;
4376
4377 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00004378 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00004379 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00004380 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004381 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004382 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00004383 ldt_info.limit = tswap32(target_ldt_info->limit);
4384 ldt_info.flags = tswap32(target_ldt_info->flags);
4385 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00004386
bellard6dbad632003-03-16 18:05:05 +00004387 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00004388 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004389 seg_32bit = ldt_info.flags & 1;
4390 contents = (ldt_info.flags >> 1) & 3;
4391 read_exec_only = (ldt_info.flags >> 3) & 1;
4392 limit_in_pages = (ldt_info.flags >> 4) & 1;
4393 seg_not_present = (ldt_info.flags >> 5) & 1;
4394 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00004395#ifdef TARGET_ABI32
4396 lm = 0;
4397#else
4398 lm = (ldt_info.flags >> 7) & 1;
4399#endif
bellard6dbad632003-03-16 18:05:05 +00004400 if (contents == 3) {
4401 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00004402 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004403 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00004404 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00004405 }
4406 /* allocate the LDT */
4407 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00004408 env->ldt.base = target_mmap(0,
4409 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
4410 PROT_READ|PROT_WRITE,
4411 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4412 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00004413 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00004414 memset(g2h(env->ldt.base), 0,
4415 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00004416 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00004417 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00004418 }
4419
4420 /* NOTE: same code as Linux kernel */
4421 /* Allow LDTs to be cleared by the user. */
4422 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4423 if (oldmode ||
4424 (contents == 0 &&
4425 read_exec_only == 1 &&
4426 seg_32bit == 0 &&
4427 limit_in_pages == 0 &&
4428 seg_not_present == 1 &&
4429 useable == 0 )) {
4430 entry_1 = 0;
4431 entry_2 = 0;
4432 goto install;
4433 }
4434 }
ths3b46e622007-09-17 08:09:54 +00004435
bellard6dbad632003-03-16 18:05:05 +00004436 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4437 (ldt_info.limit & 0x0ffff);
4438 entry_2 = (ldt_info.base_addr & 0xff000000) |
4439 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4440 (ldt_info.limit & 0xf0000) |
4441 ((read_exec_only ^ 1) << 9) |
4442 (contents << 10) |
4443 ((seg_not_present ^ 1) << 15) |
4444 (seg_32bit << 22) |
4445 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00004446 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00004447 0x7000;
4448 if (!oldmode)
4449 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00004450
bellard6dbad632003-03-16 18:05:05 +00004451 /* Install the new entry ... */
4452install:
4453 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4454 lp[0] = tswap32(entry_1);
4455 lp[1] = tswap32(entry_2);
4456 return 0;
4457}
4458
4459/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00004460static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4461 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00004462{
bellard03acab62007-11-11 14:57:14 +00004463 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00004464
bellard6dbad632003-03-16 18:05:05 +00004465 switch (func) {
4466 case 0:
4467 ret = read_ldt(ptr, bytecount);
4468 break;
4469 case 1:
4470 ret = write_ldt(env, ptr, bytecount, 1);
4471 break;
4472 case 0x11:
4473 ret = write_ldt(env, ptr, bytecount, 0);
4474 break;
bellard03acab62007-11-11 14:57:14 +00004475 default:
4476 ret = -TARGET_ENOSYS;
4477 break;
bellard6dbad632003-03-16 18:05:05 +00004478 }
4479 return ret;
4480}
bellard1b6b0292003-03-22 17:31:38 +00004481
blueswir14583f582008-08-24 10:35:55 +00004482#if defined(TARGET_I386) && defined(TARGET_ABI32)
Alexander Grafbc22eb42013-07-16 18:44:58 +01004483abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004484{
4485 uint64_t *gdt_table = g2h(env->gdt.base);
4486 struct target_modify_ldt_ldt_s ldt_info;
4487 struct target_modify_ldt_ldt_s *target_ldt_info;
4488 int seg_32bit, contents, read_exec_only, limit_in_pages;
4489 int seg_not_present, useable, lm;
4490 uint32_t *lp, entry_1, entry_2;
4491 int i;
4492
4493 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4494 if (!target_ldt_info)
4495 return -TARGET_EFAULT;
4496 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004497 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00004498 ldt_info.limit = tswap32(target_ldt_info->limit);
4499 ldt_info.flags = tswap32(target_ldt_info->flags);
4500 if (ldt_info.entry_number == -1) {
4501 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4502 if (gdt_table[i] == 0) {
4503 ldt_info.entry_number = i;
4504 target_ldt_info->entry_number = tswap32(i);
4505 break;
4506 }
4507 }
4508 }
4509 unlock_user_struct(target_ldt_info, ptr, 1);
4510
4511 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4512 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4513 return -TARGET_EINVAL;
4514 seg_32bit = ldt_info.flags & 1;
4515 contents = (ldt_info.flags >> 1) & 3;
4516 read_exec_only = (ldt_info.flags >> 3) & 1;
4517 limit_in_pages = (ldt_info.flags >> 4) & 1;
4518 seg_not_present = (ldt_info.flags >> 5) & 1;
4519 useable = (ldt_info.flags >> 6) & 1;
4520#ifdef TARGET_ABI32
4521 lm = 0;
4522#else
4523 lm = (ldt_info.flags >> 7) & 1;
4524#endif
4525
4526 if (contents == 3) {
4527 if (seg_not_present == 0)
4528 return -TARGET_EINVAL;
4529 }
4530
4531 /* NOTE: same code as Linux kernel */
4532 /* Allow LDTs to be cleared by the user. */
4533 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4534 if ((contents == 0 &&
4535 read_exec_only == 1 &&
4536 seg_32bit == 0 &&
4537 limit_in_pages == 0 &&
4538 seg_not_present == 1 &&
4539 useable == 0 )) {
4540 entry_1 = 0;
4541 entry_2 = 0;
4542 goto install;
4543 }
4544 }
4545
4546 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4547 (ldt_info.limit & 0x0ffff);
4548 entry_2 = (ldt_info.base_addr & 0xff000000) |
4549 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4550 (ldt_info.limit & 0xf0000) |
4551 ((read_exec_only ^ 1) << 9) |
4552 (contents << 10) |
4553 ((seg_not_present ^ 1) << 15) |
4554 (seg_32bit << 22) |
4555 (limit_in_pages << 23) |
4556 (useable << 20) |
4557 (lm << 21) |
4558 0x7000;
4559
4560 /* Install the new entry ... */
4561install:
4562 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4563 lp[0] = tswap32(entry_1);
4564 lp[1] = tswap32(entry_2);
4565 return 0;
4566}
4567
blueswir18fcd3692008-08-17 20:26:25 +00004568static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004569{
4570 struct target_modify_ldt_ldt_s *target_ldt_info;
4571 uint64_t *gdt_table = g2h(env->gdt.base);
4572 uint32_t base_addr, limit, flags;
4573 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4574 int seg_not_present, useable, lm;
4575 uint32_t *lp, entry_1, entry_2;
4576
4577 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4578 if (!target_ldt_info)
4579 return -TARGET_EFAULT;
4580 idx = tswap32(target_ldt_info->entry_number);
4581 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4582 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4583 unlock_user_struct(target_ldt_info, ptr, 1);
4584 return -TARGET_EINVAL;
4585 }
4586 lp = (uint32_t *)(gdt_table + idx);
4587 entry_1 = tswap32(lp[0]);
4588 entry_2 = tswap32(lp[1]);
4589
4590 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4591 contents = (entry_2 >> 10) & 3;
4592 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4593 seg_32bit = (entry_2 >> 22) & 1;
4594 limit_in_pages = (entry_2 >> 23) & 1;
4595 useable = (entry_2 >> 20) & 1;
4596#ifdef TARGET_ABI32
4597 lm = 0;
4598#else
4599 lm = (entry_2 >> 21) & 1;
4600#endif
4601 flags = (seg_32bit << 0) | (contents << 1) |
4602 (read_exec_only << 3) | (limit_in_pages << 4) |
4603 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4604 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4605 base_addr = (entry_1 >> 16) |
4606 (entry_2 & 0xff000000) |
4607 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004608 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00004609 target_ldt_info->limit = tswap32(limit);
4610 target_ldt_info->flags = tswap32(flags);
4611 unlock_user_struct(target_ldt_info, ptr, 1);
4612 return 0;
4613}
blueswir14583f582008-08-24 10:35:55 +00004614#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00004615
bellardd2fd1af2007-11-14 18:08:56 +00004616#ifndef TARGET_ABI32
Peter Maydell2667e712013-07-16 18:44:59 +01004617abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00004618{
Juan Quintela1add8692011-06-16 17:37:09 +01004619 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00004620 abi_ulong val;
4621 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01004622
bellardd2fd1af2007-11-14 18:08:56 +00004623 switch(code) {
4624 case TARGET_ARCH_SET_GS:
4625 case TARGET_ARCH_SET_FS:
4626 if (code == TARGET_ARCH_SET_GS)
4627 idx = R_GS;
4628 else
4629 idx = R_FS;
4630 cpu_x86_load_seg(env, idx, 0);
4631 env->segs[idx].base = addr;
4632 break;
4633 case TARGET_ARCH_GET_GS:
4634 case TARGET_ARCH_GET_FS:
4635 if (code == TARGET_ARCH_GET_GS)
4636 idx = R_GS;
4637 else
4638 idx = R_FS;
4639 val = env->segs[idx].base;
4640 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01004641 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00004642 break;
4643 default:
4644 ret = -TARGET_EINVAL;
4645 break;
4646 }
Juan Quintela1add8692011-06-16 17:37:09 +01004647 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00004648}
4649#endif
4650
bellard2ab83ea2003-06-15 19:56:46 +00004651#endif /* defined(TARGET_I386) */
4652
Riku Voipio05098a92011-03-04 15:27:29 +02004653#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00004654
pbrookd865bab2008-06-07 22:12:17 +00004655
4656static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4657typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01004658 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00004659 pthread_mutex_t mutex;
4660 pthread_cond_t cond;
4661 pthread_t thread;
4662 uint32_t tid;
4663 abi_ulong child_tidptr;
4664 abi_ulong parent_tidptr;
4665 sigset_t sigmask;
4666} new_thread_info;
4667
4668static void *clone_func(void *arg)
4669{
4670 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004671 CPUArchState *env;
Andreas Färber0d342822012-12-17 07:12:13 +01004672 CPUState *cpu;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004673 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00004674
Emilio G. Cota70903762015-08-23 20:23:41 -04004675 rcu_register_thread();
pbrookd865bab2008-06-07 22:12:17 +00004676 env = info->env;
Andreas Färber0d342822012-12-17 07:12:13 +01004677 cpu = ENV_GET_CPU(env);
Andreas Färbera2247f82013-06-09 19:47:04 +02004678 thread_cpu = cpu;
Andreas Färber0429a972013-08-26 18:14:44 +02004679 ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004680 info->tid = gettid();
Andreas Färber0d342822012-12-17 07:12:13 +01004681 cpu->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004682 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00004683 if (info->child_tidptr)
4684 put_user_u32(info->tid, info->child_tidptr);
4685 if (info->parent_tidptr)
4686 put_user_u32(info->tid, info->parent_tidptr);
4687 /* Enable signals. */
4688 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4689 /* Signal to the parent that we're ready. */
4690 pthread_mutex_lock(&info->mutex);
4691 pthread_cond_broadcast(&info->cond);
4692 pthread_mutex_unlock(&info->mutex);
4693 /* Wait until the parent has finshed initializing the tls state. */
4694 pthread_mutex_lock(&clone_lock);
4695 pthread_mutex_unlock(&clone_lock);
4696 cpu_loop(env);
4697 /* never exits */
4698 return NULL;
4699}
bellard1b6b0292003-03-22 17:31:38 +00004700
ths0da46a62007-10-20 20:23:07 +00004701/* do_fork() Must return host values and target errnos (unlike most
4702 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01004703static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00004704 abi_ulong parent_tidptr, target_ulong newtls,
4705 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00004706{
Andreas Färber0429a972013-08-26 18:14:44 +02004707 CPUState *cpu = ENV_GET_CPU(env);
bellard1b6b0292003-03-22 17:31:38 +00004708 int ret;
bellard5cd43932003-03-29 16:54:36 +00004709 TaskState *ts;
Andreas Färber0429a972013-08-26 18:14:44 +02004710 CPUState *new_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004711 CPUArchState *new_env;
pbrookd865bab2008-06-07 22:12:17 +00004712 unsigned int nptl_flags;
4713 sigset_t sigmask;
ths3b46e622007-09-17 08:09:54 +00004714
balrog436d1242008-09-21 02:39:45 +00004715 /* Emulate vfork() with fork() */
4716 if (flags & CLONE_VFORK)
4717 flags &= ~(CLONE_VFORK | CLONE_VM);
4718
bellard1b6b0292003-03-22 17:31:38 +00004719 if (flags & CLONE_VM) {
Andreas Färber0429a972013-08-26 18:14:44 +02004720 TaskState *parent_ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004721 new_thread_info info;
4722 pthread_attr_t attr;
Peter Maydell24cb36a2013-07-16 18:45:00 +01004723
Markus Armbrusterc78d65e2015-09-14 13:53:03 +02004724 ts = g_new0(TaskState, 1);
pbrook624f7972008-05-31 16:11:38 +00004725 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00004726 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00004727 new_env = cpu_copy(env);
pbrook6e68e072008-05-30 17:22:15 +00004728 /* Init regs that differ from the parent. */
4729 cpu_clone_regs(new_env, newsp);
Andreas Färber0429a972013-08-26 18:14:44 +02004730 new_cpu = ENV_GET_CPU(new_env);
4731 new_cpu->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004732 ts->bprm = parent_ts->bprm;
4733 ts->info = parent_ts->info;
pbrookd865bab2008-06-07 22:12:17 +00004734 nptl_flags = flags;
4735 flags &= ~CLONE_NPTL_FLAGS2;
4736
pbrookc2764712009-03-07 15:24:59 +00004737 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4738 ts->child_tidptr = child_tidptr;
4739 }
4740
pbrookd865bab2008-06-07 22:12:17 +00004741 if (nptl_flags & CLONE_SETTLS)
4742 cpu_set_tls (new_env, newtls);
4743
4744 /* Grab a mutex so that thread setup appears atomic. */
4745 pthread_mutex_lock(&clone_lock);
4746
4747 memset(&info, 0, sizeof(info));
4748 pthread_mutex_init(&info.mutex, NULL);
4749 pthread_mutex_lock(&info.mutex);
4750 pthread_cond_init(&info.cond, NULL);
4751 info.env = new_env;
4752 if (nptl_flags & CLONE_CHILD_SETTID)
4753 info.child_tidptr = child_tidptr;
4754 if (nptl_flags & CLONE_PARENT_SETTID)
4755 info.parent_tidptr = parent_tidptr;
4756
4757 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004758 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4759 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00004760 /* It is not safe to deliver signals until the child has finished
4761 initializing, so temporarily block all signals. */
4762 sigfillset(&sigmask);
4763 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4764
4765 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00004766 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00004767
4768 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4769 pthread_attr_destroy(&attr);
4770 if (ret == 0) {
4771 /* Wait for the child to initialize. */
4772 pthread_cond_wait(&info.cond, &info.mutex);
4773 ret = info.tid;
4774 if (flags & CLONE_PARENT_SETTID)
4775 put_user_u32(ret, parent_tidptr);
4776 } else {
4777 ret = -1;
4778 }
4779 pthread_mutex_unlock(&info.mutex);
4780 pthread_cond_destroy(&info.cond);
4781 pthread_mutex_destroy(&info.mutex);
4782 pthread_mutex_unlock(&clone_lock);
bellard1b6b0292003-03-22 17:31:38 +00004783 } else {
4784 /* if no CLONE_VM, we consider it is a fork */
Timothy E Baldwin93b4eff2015-08-31 00:26:21 +01004785 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0) {
4786 return -TARGET_EINVAL;
4787 }
pbrookd865bab2008-06-07 22:12:17 +00004788 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00004789 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00004790 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00004791 /* Child Process. */
Paolo Bonzinia59629f2015-03-31 13:01:05 +02004792 rcu_after_fork();
pbrookd865bab2008-06-07 22:12:17 +00004793 cpu_clone_regs(env, newsp);
4794 fork_end(1);
aurel322b1319c2008-12-18 22:44:04 +00004795 /* There is a race condition here. The parent process could
4796 theoretically read the TID in the child process before the child
4797 tid is set. This would require using either ptrace
4798 (not implemented) or having *_tidptr to point at a shared memory
4799 mapping. We can't repeat the spinlock hack used above because
4800 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00004801 if (flags & CLONE_CHILD_SETTID)
4802 put_user_u32(gettid(), child_tidptr);
4803 if (flags & CLONE_PARENT_SETTID)
4804 put_user_u32(gettid(), parent_tidptr);
Andreas Färber0429a972013-08-26 18:14:44 +02004805 ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004806 if (flags & CLONE_SETTLS)
4807 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00004808 if (flags & CLONE_CHILD_CLEARTID)
4809 ts->child_tidptr = child_tidptr;
pbrookd865bab2008-06-07 22:12:17 +00004810 } else {
4811 fork_end(0);
4812 }
bellard1b6b0292003-03-22 17:31:38 +00004813 }
4814 return ret;
4815}
4816
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004817/* warning : doesn't handle linux specific flags... */
4818static int target_to_host_fcntl_cmd(int cmd)
4819{
4820 switch(cmd) {
4821 case TARGET_F_DUPFD:
4822 case TARGET_F_GETFD:
4823 case TARGET_F_SETFD:
4824 case TARGET_F_GETFL:
4825 case TARGET_F_SETFL:
4826 return cmd;
4827 case TARGET_F_GETLK:
4828 return F_GETLK;
4829 case TARGET_F_SETLK:
4830 return F_SETLK;
4831 case TARGET_F_SETLKW:
4832 return F_SETLKW;
4833 case TARGET_F_GETOWN:
4834 return F_GETOWN;
4835 case TARGET_F_SETOWN:
4836 return F_SETOWN;
4837 case TARGET_F_GETSIG:
4838 return F_GETSIG;
4839 case TARGET_F_SETSIG:
4840 return F_SETSIG;
4841#if TARGET_ABI_BITS == 32
4842 case TARGET_F_GETLK64:
4843 return F_GETLK64;
4844 case TARGET_F_SETLK64:
4845 return F_SETLK64;
4846 case TARGET_F_SETLKW64:
4847 return F_SETLKW64;
4848#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004849 case TARGET_F_SETLEASE:
4850 return F_SETLEASE;
4851 case TARGET_F_GETLEASE:
4852 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004853#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004854 case TARGET_F_DUPFD_CLOEXEC:
4855 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004856#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004857 case TARGET_F_NOTIFY:
4858 return F_NOTIFY;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01004859#ifdef F_GETOWN_EX
4860 case TARGET_F_GETOWN_EX:
4861 return F_GETOWN_EX;
4862#endif
4863#ifdef F_SETOWN_EX
4864 case TARGET_F_SETOWN_EX:
4865 return F_SETOWN_EX;
4866#endif
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004867 default:
4868 return -TARGET_EINVAL;
4869 }
4870 return -TARGET_EINVAL;
4871}
4872
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004873#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4874static const bitmask_transtbl flock_tbl[] = {
4875 TRANSTBL_CONVERT(F_RDLCK),
4876 TRANSTBL_CONVERT(F_WRLCK),
4877 TRANSTBL_CONVERT(F_UNLCK),
4878 TRANSTBL_CONVERT(F_EXLCK),
4879 TRANSTBL_CONVERT(F_SHLCK),
4880 { 0, 0, 0, 0 }
4881};
4882
blueswir1992f48a2007-10-14 16:27:31 +00004883static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004884{
4885 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004886 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004887 struct flock64 fl64;
4888 struct target_flock64 *target_fl64;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01004889#ifdef F_GETOWN_EX
4890 struct f_owner_ex fox;
4891 struct target_f_owner_ex *target_fox;
4892#endif
blueswir1992f48a2007-10-14 16:27:31 +00004893 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004894 int host_cmd = target_to_host_fcntl_cmd(cmd);
4895
4896 if (host_cmd == -TARGET_EINVAL)
4897 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004898
bellard7775e9e2003-05-14 22:46:48 +00004899 switch(cmd) {
4900 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004901 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4902 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004903 fl.l_type =
4904 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
ths58134272007-03-31 18:59:32 +00004905 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004906 fl.l_start = tswapal(target_fl->l_start);
4907 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004908 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004909 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004910 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004911 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004912 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4913 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004914 target_fl->l_type =
4915 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004916 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004917 target_fl->l_start = tswapal(fl.l_start);
4918 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004919 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004920 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004921 }
4922 break;
ths3b46e622007-09-17 08:09:54 +00004923
bellard7775e9e2003-05-14 22:46:48 +00004924 case TARGET_F_SETLK:
4925 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004926 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4927 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004928 fl.l_type =
4929 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004930 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004931 fl.l_start = tswapal(target_fl->l_start);
4932 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004933 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004934 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004935 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004936 break;
ths3b46e622007-09-17 08:09:54 +00004937
bellard7775e9e2003-05-14 22:46:48 +00004938 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004939 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4940 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004941 fl64.l_type =
4942 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths58134272007-03-31 18:59:32 +00004943 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004944 fl64.l_start = tswap64(target_fl64->l_start);
4945 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004946 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004947 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004948 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004949 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004950 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4951 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004952 target_fl64->l_type =
4953 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004954 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004955 target_fl64->l_start = tswap64(fl64.l_start);
4956 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004957 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004958 unlock_user_struct(target_fl64, arg, 1);
4959 }
bellard9ee1fa22007-11-11 15:11:19 +00004960 break;
bellard7775e9e2003-05-14 22:46:48 +00004961 case TARGET_F_SETLK64:
4962 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004963 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4964 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004965 fl64.l_type =
4966 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004967 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004968 fl64.l_start = tswap64(target_fl64->l_start);
4969 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004970 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004971 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004972 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004973 break;
4974
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004975 case TARGET_F_GETFL:
4976 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004977 if (ret >= 0) {
4978 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4979 }
bellardffa65c32004-01-04 23:57:22 +00004980 break;
4981
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004982 case TARGET_F_SETFL:
4983 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4984 break;
4985
Andreas Schwab8d5d3002014-03-07 15:24:08 +01004986#ifdef F_GETOWN_EX
4987 case TARGET_F_GETOWN_EX:
4988 ret = get_errno(fcntl(fd, host_cmd, &fox));
4989 if (ret >= 0) {
4990 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
4991 return -TARGET_EFAULT;
4992 target_fox->type = tswap32(fox.type);
4993 target_fox->pid = tswap32(fox.pid);
4994 unlock_user_struct(target_fox, arg, 1);
4995 }
4996 break;
4997#endif
4998
4999#ifdef F_SETOWN_EX
5000 case TARGET_F_SETOWN_EX:
5001 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
5002 return -TARGET_EFAULT;
5003 fox.type = tswap32(target_fox->type);
5004 fox.pid = tswap32(target_fox->pid);
5005 unlock_user_struct(target_fox, arg, 0);
5006 ret = get_errno(fcntl(fd, host_cmd, &fox));
5007 break;
5008#endif
5009
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005010 case TARGET_F_SETOWN:
5011 case TARGET_F_GETOWN:
5012 case TARGET_F_SETSIG:
5013 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005014 case TARGET_F_SETLEASE:
5015 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005016 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00005017 break;
5018
bellard7775e9e2003-05-14 22:46:48 +00005019 default:
bellard9ee1fa22007-11-11 15:11:19 +00005020 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00005021 break;
5022 }
5023 return ret;
5024}
5025
bellard67867302003-11-23 17:05:30 +00005026#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00005027
bellard67867302003-11-23 17:05:30 +00005028static inline int high2lowuid(int uid)
5029{
5030 if (uid > 65535)
5031 return 65534;
5032 else
5033 return uid;
5034}
5035
5036static inline int high2lowgid(int gid)
5037{
5038 if (gid > 65535)
5039 return 65534;
5040 else
5041 return gid;
5042}
5043
5044static inline int low2highuid(int uid)
5045{
5046 if ((int16_t)uid == -1)
5047 return -1;
5048 else
5049 return uid;
5050}
5051
5052static inline int low2highgid(int gid)
5053{
5054 if ((int16_t)gid == -1)
5055 return -1;
5056 else
5057 return gid;
5058}
Riku Voipio0c866a72011-04-18 15:23:06 +03005059static inline int tswapid(int id)
5060{
5061 return tswap16(id);
5062}
Peter Maydell76ca3102014-03-02 19:36:41 +00005063
5064#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
5065
Riku Voipio0c866a72011-04-18 15:23:06 +03005066#else /* !USE_UID16 */
5067static inline int high2lowuid(int uid)
5068{
5069 return uid;
5070}
5071static inline int high2lowgid(int gid)
5072{
5073 return gid;
5074}
5075static inline int low2highuid(int uid)
5076{
5077 return uid;
5078}
5079static inline int low2highgid(int gid)
5080{
5081 return gid;
5082}
5083static inline int tswapid(int id)
5084{
5085 return tswap32(id);
5086}
Peter Maydell76ca3102014-03-02 19:36:41 +00005087
5088#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
5089
bellard67867302003-11-23 17:05:30 +00005090#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00005091
bellard31e31b82003-02-18 22:55:36 +00005092void syscall_init(void)
5093{
bellard2ab83ea2003-06-15 19:56:46 +00005094 IOCTLEntry *ie;
5095 const argtype *arg_type;
5096 int size;
thsb92c47c2007-11-01 00:07:38 +00005097 int i;
bellard2ab83ea2003-06-15 19:56:46 +00005098
Alexander Graf8be656b2015-05-06 23:47:32 +02005099 thunk_init(STRUCT_MAX);
5100
Blue Swirl001faf32009-05-13 17:53:17 +00005101#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00005102#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00005103#include "syscall_types.h"
5104#undef STRUCT
5105#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00005106
Peter Maydelldd6e9572012-07-23 08:07:22 +00005107 /* Build target_to_host_errno_table[] table from
5108 * host_to_target_errno_table[]. */
5109 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
5110 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
5111 }
5112
bellard2ab83ea2003-06-15 19:56:46 +00005113 /* we patch the ioctl size if necessary. We rely on the fact that
5114 no ioctl has all the bits at '1' in the size field */
5115 ie = ioctl_entries;
5116 while (ie->target_cmd != 0) {
5117 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
5118 TARGET_IOC_SIZEMASK) {
5119 arg_type = ie->arg_type;
5120 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00005121 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00005122 ie->target_cmd);
5123 exit(1);
5124 }
5125 arg_type++;
5126 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00005127 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00005128 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
5129 (size << TARGET_IOC_SIZESHIFT);
5130 }
thsb92c47c2007-11-01 00:07:38 +00005131
bellard2ab83ea2003-06-15 19:56:46 +00005132 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00005133#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
5134 (defined(__x86_64__) && defined(TARGET_X86_64))
5135 if (unlikely(ie->target_cmd != ie->host_cmd)) {
5136 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
5137 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00005138 }
5139#endif
5140 ie++;
5141 }
bellard31e31b82003-02-18 22:55:36 +00005142}
bellardc573ff62004-01-04 15:51:36 +00005143
blueswir1992f48a2007-10-14 16:27:31 +00005144#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00005145static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
5146{
thsaf325d32008-06-10 15:29:15 +00005147#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00005148 return ((uint64_t)word0 << 32) | word1;
5149#else
5150 return ((uint64_t)word1 << 32) | word0;
5151#endif
5152}
blueswir1992f48a2007-10-14 16:27:31 +00005153#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00005154static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
5155{
5156 return word0;
5157}
blueswir1992f48a2007-10-14 16:27:31 +00005158#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00005159
5160#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00005161static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
5162 abi_long arg2,
5163 abi_long arg3,
5164 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00005165{
Riku Voipio48e515d2011-07-12 15:40:51 +03005166 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00005167 arg2 = arg3;
5168 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03005169 }
pbrookce4defa2006-02-09 16:49:55 +00005170 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
5171}
5172#endif
5173
5174#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00005175static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
5176 abi_long arg2,
5177 abi_long arg3,
5178 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00005179{
Riku Voipio48e515d2011-07-12 15:40:51 +03005180 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00005181 arg2 = arg3;
5182 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03005183 }
pbrookce4defa2006-02-09 16:49:55 +00005184 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
5185}
5186#endif
5187
bellard579a97f2007-11-11 14:26:47 +00005188static inline abi_long target_to_host_timespec(struct timespec *host_ts,
5189 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00005190{
5191 struct target_timespec *target_ts;
5192
bellard579a97f2007-11-11 14:26:47 +00005193 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
5194 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005195 host_ts->tv_sec = tswapal(target_ts->tv_sec);
5196 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00005197 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00005198 return 0;
pbrook53a59602006-03-25 19:31:22 +00005199}
5200
bellard579a97f2007-11-11 14:26:47 +00005201static inline abi_long host_to_target_timespec(abi_ulong target_addr,
5202 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00005203{
5204 struct target_timespec *target_ts;
5205
bellard579a97f2007-11-11 14:26:47 +00005206 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
5207 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005208 target_ts->tv_sec = tswapal(host_ts->tv_sec);
5209 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00005210 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00005211 return 0;
pbrook53a59602006-03-25 19:31:22 +00005212}
5213
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11005214static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
5215 abi_ulong target_addr)
5216{
5217 struct target_itimerspec *target_itspec;
5218
5219 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
5220 return -TARGET_EFAULT;
5221 }
5222
5223 host_itspec->it_interval.tv_sec =
5224 tswapal(target_itspec->it_interval.tv_sec);
5225 host_itspec->it_interval.tv_nsec =
5226 tswapal(target_itspec->it_interval.tv_nsec);
5227 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
5228 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
5229
5230 unlock_user_struct(target_itspec, target_addr, 1);
5231 return 0;
5232}
5233
5234static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
5235 struct itimerspec *host_its)
5236{
5237 struct target_itimerspec *target_itspec;
5238
5239 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
5240 return -TARGET_EFAULT;
5241 }
5242
5243 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
5244 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
5245
5246 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
5247 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
5248
5249 unlock_user_struct(target_itspec, target_addr, 0);
5250 return 0;
5251}
5252
Peter Maydellc0659762014-08-09 15:42:32 +01005253static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
5254 abi_ulong target_addr)
5255{
5256 struct target_sigevent *target_sevp;
5257
5258 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
5259 return -TARGET_EFAULT;
5260 }
5261
5262 /* This union is awkward on 64 bit systems because it has a 32 bit
5263 * integer and a pointer in it; we follow the conversion approach
5264 * used for handling sigval types in signal.c so the guest should get
5265 * the correct value back even if we did a 64 bit byteswap and it's
5266 * using the 32 bit integer.
5267 */
5268 host_sevp->sigev_value.sival_ptr =
5269 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
5270 host_sevp->sigev_signo =
5271 target_to_host_signal(tswap32(target_sevp->sigev_signo));
5272 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
5273 host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
5274
5275 unlock_user_struct(target_sevp, target_addr, 1);
5276 return 0;
5277}
5278
Tom Musta6f6a4032014-08-12 13:53:42 -05005279#if defined(TARGET_NR_mlockall)
5280static inline int target_to_host_mlockall_arg(int arg)
5281{
5282 int result = 0;
5283
5284 if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
5285 result |= MCL_CURRENT;
5286 }
5287 if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
5288 result |= MCL_FUTURE;
5289 }
5290 return result;
5291}
5292#endif
5293
balrog6a24a772008-09-20 02:23:36 +00005294static inline abi_long host_to_target_stat64(void *cpu_env,
5295 abi_ulong target_addr,
5296 struct stat *host_st)
5297{
Alexander Graf09701192013-09-03 20:12:15 +01005298#if defined(TARGET_ARM) && defined(TARGET_ABI32)
balrog6a24a772008-09-20 02:23:36 +00005299 if (((CPUARMState *)cpu_env)->eabi) {
5300 struct target_eabi_stat64 *target_st;
5301
5302 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
5303 return -TARGET_EFAULT;
5304 memset(target_st, 0, sizeof(struct target_eabi_stat64));
5305 __put_user(host_st->st_dev, &target_st->st_dev);
5306 __put_user(host_st->st_ino, &target_st->st_ino);
5307#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5308 __put_user(host_st->st_ino, &target_st->__st_ino);
5309#endif
5310 __put_user(host_st->st_mode, &target_st->st_mode);
5311 __put_user(host_st->st_nlink, &target_st->st_nlink);
5312 __put_user(host_st->st_uid, &target_st->st_uid);
5313 __put_user(host_st->st_gid, &target_st->st_gid);
5314 __put_user(host_st->st_rdev, &target_st->st_rdev);
5315 __put_user(host_st->st_size, &target_st->st_size);
5316 __put_user(host_st->st_blksize, &target_st->st_blksize);
5317 __put_user(host_st->st_blocks, &target_st->st_blocks);
5318 __put_user(host_st->st_atime, &target_st->target_st_atime);
5319 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
5320 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
5321 unlock_user_struct(target_st, target_addr, 1);
5322 } else
5323#endif
5324 {
Stefan Weil20d155b2013-10-30 22:52:24 +01005325#if defined(TARGET_HAS_STRUCT_STAT64)
balrog6a24a772008-09-20 02:23:36 +00005326 struct target_stat64 *target_st;
Stefan Weil20d155b2013-10-30 22:52:24 +01005327#else
5328 struct target_stat *target_st;
aurel329d33b762009-04-08 23:07:05 +00005329#endif
balrog6a24a772008-09-20 02:23:36 +00005330
5331 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
5332 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00005333 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00005334 __put_user(host_st->st_dev, &target_st->st_dev);
5335 __put_user(host_st->st_ino, &target_st->st_ino);
5336#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5337 __put_user(host_st->st_ino, &target_st->__st_ino);
5338#endif
5339 __put_user(host_st->st_mode, &target_st->st_mode);
5340 __put_user(host_st->st_nlink, &target_st->st_nlink);
5341 __put_user(host_st->st_uid, &target_st->st_uid);
5342 __put_user(host_st->st_gid, &target_st->st_gid);
5343 __put_user(host_st->st_rdev, &target_st->st_rdev);
5344 /* XXX: better use of kernel struct */
5345 __put_user(host_st->st_size, &target_st->st_size);
5346 __put_user(host_st->st_blksize, &target_st->st_blksize);
5347 __put_user(host_st->st_blocks, &target_st->st_blocks);
5348 __put_user(host_st->st_atime, &target_st->target_st_atime);
5349 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
5350 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
5351 unlock_user_struct(target_st, target_addr, 1);
5352 }
5353
5354 return 0;
5355}
balrog6a24a772008-09-20 02:23:36 +00005356
pbrookbd0c5662008-05-29 14:34:11 +00005357/* ??? Using host futex calls even when target atomic operations
5358 are not really atomic probably breaks things. However implementing
5359 futexes locally would make futexes shared between multiple processes
5360 tricky. However they're probably useless because guest atomic
5361 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00005362static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
5363 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00005364{
5365 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07005366 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00005367
5368 /* ??? We assume FUTEX_* constants are the same on both host
5369 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03005370#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07005371 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03005372#else
Nathan Froyda16aae02009-08-03 08:43:29 -07005373 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03005374#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07005375 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00005376 case FUTEX_WAIT:
John Rigbycce246e2013-02-23 16:14:07 -07005377 case FUTEX_WAIT_BITSET:
pbrookbd0c5662008-05-29 14:34:11 +00005378 if (timeout) {
5379 pts = &ts;
5380 target_to_host_timespec(pts, timeout);
5381 } else {
5382 pts = NULL;
5383 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03005384 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
John Rigbycce246e2013-02-23 16:14:07 -07005385 pts, NULL, val3));
pbrookbd0c5662008-05-29 14:34:11 +00005386 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03005387 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00005388 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03005389 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00005390 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00005391 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07005392 case FUTEX_WAKE_OP:
5393 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
5394 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
5395 But the prototype takes a `struct timespec *'; insert casts
5396 to satisfy the compiler. We do not need to tswap TIMEOUT
5397 since it's not compared to guest memory. */
5398 pts = (struct timespec *)(uintptr_t) timeout;
5399 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
5400 g2h(uaddr2),
5401 (base_op == FUTEX_CMP_REQUEUE
5402 ? tswap32(val3)
5403 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00005404 default:
5405 return -TARGET_ENOSYS;
5406 }
5407}
Laurent Vivier0f0426f2015-09-01 22:27:33 +02005408#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5409static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
5410 abi_long handle, abi_long mount_id,
5411 abi_long flags)
5412{
5413 struct file_handle *target_fh;
5414 struct file_handle *fh;
5415 int mid = 0;
5416 abi_long ret;
5417 char *name;
5418 unsigned int size, total_size;
5419
5420 if (get_user_s32(size, handle)) {
5421 return -TARGET_EFAULT;
5422 }
5423
5424 name = lock_user_string(pathname);
5425 if (!name) {
5426 return -TARGET_EFAULT;
5427 }
5428
5429 total_size = sizeof(struct file_handle) + size;
5430 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
5431 if (!target_fh) {
5432 unlock_user(name, pathname, 0);
5433 return -TARGET_EFAULT;
5434 }
5435
5436 fh = g_malloc0(total_size);
5437 fh->handle_bytes = size;
5438
5439 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
5440 unlock_user(name, pathname, 0);
5441
5442 /* man name_to_handle_at(2):
5443 * Other than the use of the handle_bytes field, the caller should treat
5444 * the file_handle structure as an opaque data type
5445 */
5446
5447 memcpy(target_fh, fh, total_size);
5448 target_fh->handle_bytes = tswap32(fh->handle_bytes);
5449 target_fh->handle_type = tswap32(fh->handle_type);
5450 g_free(fh);
5451 unlock_user(target_fh, handle, total_size);
5452
5453 if (put_user_s32(mid, mount_id)) {
5454 return -TARGET_EFAULT;
5455 }
5456
5457 return ret;
5458
5459}
5460#endif
5461
5462#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5463static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
5464 abi_long flags)
5465{
5466 struct file_handle *target_fh;
5467 struct file_handle *fh;
5468 unsigned int size, total_size;
5469 abi_long ret;
5470
5471 if (get_user_s32(size, handle)) {
5472 return -TARGET_EFAULT;
5473 }
5474
5475 total_size = sizeof(struct file_handle) + size;
5476 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
5477 if (!target_fh) {
5478 return -TARGET_EFAULT;
5479 }
5480
Thomas Huthe9d49d52015-10-09 17:56:38 +02005481 fh = g_memdup(target_fh, total_size);
Laurent Vivier0f0426f2015-09-01 22:27:33 +02005482 fh->handle_bytes = size;
5483 fh->handle_type = tswap32(target_fh->handle_type);
5484
5485 ret = get_errno(open_by_handle_at(mount_fd, fh,
5486 target_to_host_bitmask(flags, fcntl_flags_tbl)));
5487
5488 g_free(fh);
5489
5490 unlock_user(target_fh, handle, total_size);
5491
5492 return ret;
5493}
5494#endif
pbrookbd0c5662008-05-29 14:34:11 +00005495
Laurent Viviere36800c2015-10-02 14:48:09 +02005496#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
5497
5498/* signalfd siginfo conversion */
5499
5500static void
5501host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
5502 const struct signalfd_siginfo *info)
5503{
5504 int sig = host_to_target_signal(info->ssi_signo);
5505
5506 /* linux/signalfd.h defines a ssi_addr_lsb
5507 * not defined in sys/signalfd.h but used by some kernels
5508 */
5509
5510#ifdef BUS_MCEERR_AO
5511 if (tinfo->ssi_signo == SIGBUS &&
5512 (tinfo->ssi_code == BUS_MCEERR_AR ||
5513 tinfo->ssi_code == BUS_MCEERR_AO)) {
5514 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
5515 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
5516 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
5517 }
5518#endif
5519
5520 tinfo->ssi_signo = tswap32(sig);
5521 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
5522 tinfo->ssi_code = tswap32(info->ssi_code);
5523 tinfo->ssi_pid = tswap32(info->ssi_pid);
5524 tinfo->ssi_uid = tswap32(info->ssi_uid);
5525 tinfo->ssi_fd = tswap32(info->ssi_fd);
5526 tinfo->ssi_tid = tswap32(info->ssi_tid);
5527 tinfo->ssi_band = tswap32(info->ssi_band);
5528 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
5529 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
5530 tinfo->ssi_status = tswap32(info->ssi_status);
5531 tinfo->ssi_int = tswap32(info->ssi_int);
5532 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
5533 tinfo->ssi_utime = tswap64(info->ssi_utime);
5534 tinfo->ssi_stime = tswap64(info->ssi_stime);
5535 tinfo->ssi_addr = tswap64(info->ssi_addr);
5536}
5537
Laurent Vivier5d4d3662015-10-28 21:40:43 +01005538static abi_long host_to_target_data_signalfd(void *buf, size_t len)
Laurent Viviere36800c2015-10-02 14:48:09 +02005539{
5540 int i;
5541
5542 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
5543 host_to_target_signalfd_siginfo(buf + i, buf + i);
5544 }
5545
5546 return len;
5547}
5548
5549static TargetFdTrans target_signalfd_trans = {
Laurent Vivier5d4d3662015-10-28 21:40:43 +01005550 .host_to_target_data = host_to_target_data_signalfd,
Laurent Viviere36800c2015-10-02 14:48:09 +02005551};
5552
5553static abi_long do_signalfd4(int fd, abi_long mask, int flags)
5554{
5555 int host_flags;
5556 target_sigset_t *target_mask;
5557 sigset_t host_mask;
5558 abi_long ret;
5559
5560 if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
5561 return -TARGET_EINVAL;
5562 }
5563 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
5564 return -TARGET_EFAULT;
5565 }
5566
5567 target_to_host_sigset(&host_mask, target_mask);
5568
5569 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
5570
5571 ret = get_errno(signalfd(fd, &host_mask, host_flags));
5572 if (ret >= 0) {
5573 fd_trans_register(ret, &target_signalfd_trans);
5574 }
5575
5576 unlock_user_struct(target_mask, mask, 0);
5577
5578 return ret;
5579}
5580#endif
5581
pbrook1d9d8b52009-04-16 15:17:02 +00005582/* Map host to target signal numbers for the wait family of syscalls.
5583 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07005584int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00005585{
5586 if (WIFSIGNALED(status)) {
5587 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
5588 }
5589 if (WIFSTOPPED(status)) {
5590 return (host_to_target_signal(WSTOPSIG(status)) << 8)
5591 | (status & 0xff);
5592 }
5593 return status;
5594}
5595
Wim Vander Schelden76b94242014-06-18 11:02:39 +02005596static int open_self_cmdline(void *cpu_env, int fd)
5597{
5598 int fd_orig = -1;
5599 bool word_skipped = false;
5600
5601 fd_orig = open("/proc/self/cmdline", O_RDONLY);
5602 if (fd_orig < 0) {
5603 return fd_orig;
5604 }
5605
5606 while (true) {
5607 ssize_t nb_read;
5608 char buf[128];
5609 char *cp_buf = buf;
5610
5611 nb_read = read(fd_orig, buf, sizeof(buf));
5612 if (nb_read < 0) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005613 int e = errno;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02005614 fd_orig = close(fd_orig);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005615 errno = e;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02005616 return -1;
5617 } else if (nb_read == 0) {
5618 break;
5619 }
5620
5621 if (!word_skipped) {
5622 /* Skip the first string, which is the path to qemu-*-static
5623 instead of the actual command. */
5624 cp_buf = memchr(buf, 0, sizeof(buf));
5625 if (cp_buf) {
5626 /* Null byte found, skip one string */
5627 cp_buf++;
5628 nb_read -= cp_buf - buf;
5629 word_skipped = true;
5630 }
5631 }
5632
5633 if (word_skipped) {
5634 if (write(fd, cp_buf, nb_read) != nb_read) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005635 int e = errno;
zhanghailiang680dfde2014-08-22 16:23:51 +08005636 close(fd_orig);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005637 errno = e;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02005638 return -1;
5639 }
5640 }
5641 }
5642
5643 return close(fd_orig);
5644}
5645
Alexander Graf36c08d42011-11-02 20:23:24 +01005646static int open_self_maps(void *cpu_env, int fd)
5647{
Andreas Färber0429a972013-08-26 18:14:44 +02005648 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5649 TaskState *ts = cpu->opaque;
Alexander Graf1a49ef22012-05-01 16:30:28 +01005650 FILE *fp;
5651 char *line = NULL;
5652 size_t len = 0;
5653 ssize_t read;
Alexander Graf36c08d42011-11-02 20:23:24 +01005654
Alexander Graf1a49ef22012-05-01 16:30:28 +01005655 fp = fopen("/proc/self/maps", "r");
5656 if (fp == NULL) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005657 return -1;
Alexander Graf1a49ef22012-05-01 16:30:28 +01005658 }
5659
5660 while ((read = getline(&line, &len, fp)) != -1) {
5661 int fields, dev_maj, dev_min, inode;
5662 uint64_t min, max, offset;
5663 char flag_r, flag_w, flag_x, flag_p;
5664 char path[512] = "";
5665 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
5666 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
5667 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
5668
5669 if ((fields < 10) || (fields > 11)) {
5670 continue;
5671 }
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04005672 if (h2g_valid(min)) {
5673 int flags = page_get_flags(h2g(min));
5674 max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX);
5675 if (page_check_range(h2g(min), max - min, flags) == -1) {
5676 continue;
5677 }
5678 if (h2g(min) == ts->info->stack_limit) {
5679 pstrcpy(path, sizeof(path), " [stack]");
5680 }
Alexander Graf1a49ef22012-05-01 16:30:28 +01005681 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
Christophe Lyone24fed42013-04-02 14:03:38 +02005682 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04005683 h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
Alexander Graf1a49ef22012-05-01 16:30:28 +01005684 flag_x, flag_p, offset, dev_maj, dev_min, inode,
Christophe Lyone24fed42013-04-02 14:03:38 +02005685 path[0] ? " " : "", path);
Alexander Graf1a49ef22012-05-01 16:30:28 +01005686 }
5687 }
5688
5689 free(line);
5690 fclose(fp);
5691
Alexander Graf36c08d42011-11-02 20:23:24 +01005692 return 0;
5693}
5694
Alexander Graf480b8e72011-11-02 20:23:25 +01005695static int open_self_stat(void *cpu_env, int fd)
5696{
Andreas Färber0429a972013-08-26 18:14:44 +02005697 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5698 TaskState *ts = cpu->opaque;
Alexander Graf480b8e72011-11-02 20:23:25 +01005699 abi_ulong start_stack = ts->info->start_stack;
5700 int i;
5701
5702 for (i = 0; i < 44; i++) {
5703 char buf[128];
5704 int len;
5705 uint64_t val = 0;
5706
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005707 if (i == 0) {
5708 /* pid */
5709 val = getpid();
5710 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5711 } else if (i == 1) {
5712 /* app name */
5713 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5714 } else if (i == 27) {
5715 /* stack bottom */
5716 val = start_stack;
5717 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5718 } else {
5719 /* for the rest, there is MasterCard */
5720 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
Alexander Graf480b8e72011-11-02 20:23:25 +01005721 }
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005722
Alexander Graf480b8e72011-11-02 20:23:25 +01005723 len = strlen(buf);
5724 if (write(fd, buf, len) != len) {
5725 return -1;
5726 }
5727 }
5728
5729 return 0;
5730}
5731
Alexander Graf257450e2011-11-02 20:23:26 +01005732static int open_self_auxv(void *cpu_env, int fd)
5733{
Andreas Färber0429a972013-08-26 18:14:44 +02005734 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5735 TaskState *ts = cpu->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01005736 abi_ulong auxv = ts->info->saved_auxv;
5737 abi_ulong len = ts->info->auxv_len;
5738 char *ptr;
5739
5740 /*
5741 * Auxiliary vector is stored in target process stack.
5742 * read in whole auxv vector and copy it to file
5743 */
5744 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5745 if (ptr != NULL) {
5746 while (len > 0) {
5747 ssize_t r;
5748 r = write(fd, ptr, len);
5749 if (r <= 0) {
5750 break;
5751 }
5752 len -= r;
5753 ptr += r;
5754 }
5755 lseek(fd, 0, SEEK_SET);
5756 unlock_user(ptr, auxv, len);
5757 }
5758
5759 return 0;
5760}
5761
Andreas Schwab463d8e72013-07-02 14:04:12 +01005762static int is_proc_myself(const char *filename, const char *entry)
5763{
5764 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
5765 filename += strlen("/proc/");
5766 if (!strncmp(filename, "self/", strlen("self/"))) {
5767 filename += strlen("self/");
5768 } else if (*filename >= '1' && *filename <= '9') {
5769 char myself[80];
5770 snprintf(myself, sizeof(myself), "%d/", getpid());
5771 if (!strncmp(filename, myself, strlen(myself))) {
5772 filename += strlen(myself);
5773 } else {
5774 return 0;
5775 }
5776 } else {
5777 return 0;
5778 }
5779 if (!strcmp(filename, entry)) {
5780 return 1;
5781 }
5782 }
5783 return 0;
5784}
5785
Laurent Vivierde6b9932013-08-30 01:46:40 +02005786#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5787static int is_proc(const char *filename, const char *entry)
5788{
5789 return strcmp(filename, entry) == 0;
5790}
5791
5792static int open_net_route(void *cpu_env, int fd)
5793{
5794 FILE *fp;
5795 char *line = NULL;
5796 size_t len = 0;
5797 ssize_t read;
5798
5799 fp = fopen("/proc/net/route", "r");
5800 if (fp == NULL) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005801 return -1;
Laurent Vivierde6b9932013-08-30 01:46:40 +02005802 }
5803
5804 /* read header */
5805
5806 read = getline(&line, &len, fp);
5807 dprintf(fd, "%s", line);
5808
5809 /* read routes */
5810
5811 while ((read = getline(&line, &len, fp)) != -1) {
5812 char iface[16];
5813 uint32_t dest, gw, mask;
5814 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
5815 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5816 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
5817 &mask, &mtu, &window, &irtt);
5818 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5819 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
5820 metric, tswap32(mask), mtu, window, irtt);
5821 }
5822
5823 free(line);
5824 fclose(fp);
5825
5826 return 0;
5827}
5828#endif
5829
Riku Voipio0b2effd2014-08-06 10:36:37 +03005830static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
Alexander Graf3be14d02011-11-02 20:23:23 +01005831{
5832 struct fake_open {
5833 const char *filename;
5834 int (*fill)(void *cpu_env, int fd);
Laurent Vivierde6b9932013-08-30 01:46:40 +02005835 int (*cmp)(const char *s1, const char *s2);
Alexander Graf3be14d02011-11-02 20:23:23 +01005836 };
5837 const struct fake_open *fake_open;
5838 static const struct fake_open fakes[] = {
Laurent Vivierde6b9932013-08-30 01:46:40 +02005839 { "maps", open_self_maps, is_proc_myself },
5840 { "stat", open_self_stat, is_proc_myself },
5841 { "auxv", open_self_auxv, is_proc_myself },
Wim Vander Schelden76b94242014-06-18 11:02:39 +02005842 { "cmdline", open_self_cmdline, is_proc_myself },
Laurent Vivierde6b9932013-08-30 01:46:40 +02005843#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5844 { "/proc/net/route", open_net_route, is_proc },
5845#endif
5846 { NULL, NULL, NULL }
Alexander Graf3be14d02011-11-02 20:23:23 +01005847 };
5848
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03005849 if (is_proc_myself(pathname, "exe")) {
5850 int execfd = qemu_getauxval(AT_EXECFD);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005851 return execfd ? execfd : sys_openat(dirfd, exec_path, flags, mode);
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03005852 }
5853
Alexander Graf3be14d02011-11-02 20:23:23 +01005854 for (fake_open = fakes; fake_open->filename; fake_open++) {
Laurent Vivierde6b9932013-08-30 01:46:40 +02005855 if (fake_open->cmp(pathname, fake_open->filename)) {
Alexander Graf3be14d02011-11-02 20:23:23 +01005856 break;
5857 }
5858 }
5859
5860 if (fake_open->filename) {
5861 const char *tmpdir;
5862 char filename[PATH_MAX];
5863 int fd, r;
5864
5865 /* create temporary file to map stat to */
5866 tmpdir = getenv("TMPDIR");
5867 if (!tmpdir)
5868 tmpdir = "/tmp";
5869 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5870 fd = mkstemp(filename);
5871 if (fd < 0) {
5872 return fd;
5873 }
5874 unlink(filename);
5875
5876 if ((r = fake_open->fill(cpu_env, fd))) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005877 int e = errno;
Alexander Graf3be14d02011-11-02 20:23:23 +01005878 close(fd);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005879 errno = e;
Alexander Graf3be14d02011-11-02 20:23:23 +01005880 return r;
5881 }
5882 lseek(fd, 0, SEEK_SET);
5883
5884 return fd;
5885 }
5886
Peter Maydella3ca7bb2016-05-12 18:47:26 +01005887 return sys_openat(dirfd, path(pathname), flags, mode);
Alexander Graf3be14d02011-11-02 20:23:23 +01005888}
5889
Alexander Grafaecc8862014-11-10 21:33:03 +01005890#define TIMER_MAGIC 0x0caf0000
5891#define TIMER_MAGIC_MASK 0xffff0000
5892
5893/* Convert QEMU provided timer ID back to internal 16bit index format */
5894static target_timer_t get_timer_id(abi_long arg)
5895{
5896 target_timer_t timerid = arg;
5897
5898 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
5899 return -TARGET_EINVAL;
5900 }
5901
5902 timerid &= 0xffff;
5903
5904 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
5905 return -TARGET_EINVAL;
5906 }
5907
5908 return timerid;
5909}
5910
ths0da46a62007-10-20 20:23:07 +00005911/* do_syscall() should always have a single exit point at the end so
5912 that actions, such as logging of syscall results, can be performed.
5913 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00005914abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5915 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01005916 abi_long arg5, abi_long arg6, abi_long arg7,
5917 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00005918{
Andreas Färber182735e2013-05-29 22:29:20 +02005919 CPUState *cpu = ENV_GET_CPU(cpu_env);
blueswir1992f48a2007-10-14 16:27:31 +00005920 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005921 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00005922 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00005923 void *p;
ths3b46e622007-09-17 08:09:54 +00005924
Timothy E Baldwin71a8f7f2016-05-12 18:47:45 +01005925#if defined(DEBUG_ERESTARTSYS)
5926 /* Debug-only code for exercising the syscall-restart code paths
5927 * in the per-architecture cpu main loops: restart every syscall
5928 * the guest makes once before letting it through.
5929 */
5930 {
5931 static int flag;
5932
5933 flag = !flag;
5934 if (flag) {
5935 return -TARGET_ERESTARTSYS;
5936 }
5937 }
5938#endif
5939
bellard72f03902003-02-18 23:33:18 +00005940#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00005941 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00005942#endif
thsb92c47c2007-11-01 00:07:38 +00005943 if(do_strace)
5944 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5945
bellard31e31b82003-02-18 22:55:36 +00005946 switch(num) {
5947 case TARGET_NR_exit:
Andreas Färber9b056fc2013-06-24 23:53:10 +02005948 /* In old applications this may be used to implement _exit(2).
5949 However in threaded applictions it is used for thread termination,
5950 and _exit_group is used for application termination.
5951 Do thread termination if we have more then one thread. */
5952 /* FIXME: This probably breaks if a signal arrives. We should probably
5953 be disabling signals. */
Andreas Färberbdc44642013-06-24 23:50:24 +02005954 if (CPU_NEXT(first_cpu)) {
Andreas Färber9b056fc2013-06-24 23:53:10 +02005955 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00005956
Andreas Färber9b056fc2013-06-24 23:53:10 +02005957 cpu_list_lock();
Andreas Färber9b056fc2013-06-24 23:53:10 +02005958 /* Remove the CPU from the list. */
Andreas Färberbdc44642013-06-24 23:50:24 +02005959 QTAILQ_REMOVE(&cpus, cpu, node);
Andreas Färber9b056fc2013-06-24 23:53:10 +02005960 cpu_list_unlock();
Andreas Färber0429a972013-08-26 18:14:44 +02005961 ts = cpu->opaque;
Andreas Färber9b056fc2013-06-24 23:53:10 +02005962 if (ts->child_tidptr) {
5963 put_user_u32(0, ts->child_tidptr);
5964 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5965 NULL, NULL, 0);
5966 }
Andreas Färbera2247f82013-06-09 19:47:04 +02005967 thread_cpu = NULL;
Andreas Färber0429a972013-08-26 18:14:44 +02005968 object_unref(OBJECT(cpu));
Andreas Färber9b056fc2013-06-24 23:53:10 +02005969 g_free(ts);
Emilio G. Cota70903762015-08-23 20:23:41 -04005970 rcu_unregister_thread();
Andreas Färber9b056fc2013-06-24 23:53:10 +02005971 pthread_exit(NULL);
5972 }
Juan Quintela9788c9c2009-07-27 16:13:02 +02005973#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00005974 _mcleanup();
5975#endif
bellarde9009672005-04-26 20:42:36 +00005976 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00005977 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00005978 ret = 0; /* avoid warning */
5979 break;
5980 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00005981 if (arg3 == 0)
5982 ret = 0;
5983 else {
5984 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5985 goto efault;
5986 ret = get_errno(read(arg1, p, arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02005987 if (ret >= 0 &&
Laurent Vivier5d4d3662015-10-28 21:40:43 +01005988 fd_trans_host_to_target_data(arg1)) {
5989 ret = fd_trans_host_to_target_data(arg1)(p, ret);
Laurent Viviere36800c2015-10-02 14:48:09 +02005990 }
aurel3238d840e2009-01-30 19:48:17 +00005991 unlock_user(p, arg2, ret);
5992 }
bellard31e31b82003-02-18 22:55:36 +00005993 break;
5994 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00005995 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5996 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005997 ret = get_errno(write(arg1, p, arg3));
5998 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00005999 break;
Chen Gang704eff62015-08-21 05:37:33 +08006000#ifdef TARGET_NR_open
bellard31e31b82003-02-18 22:55:36 +00006001 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00006002 if (!(p = lock_user_string(arg1)))
6003 goto efault;
Riku Voipio0b2effd2014-08-06 10:36:37 +03006004 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
6005 target_to_host_bitmask(arg2, fcntl_flags_tbl),
6006 arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02006007 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00006008 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006009 break;
Chen Gang704eff62015-08-21 05:37:33 +08006010#endif
ths82424832007-09-24 09:21:55 +00006011 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00006012 if (!(p = lock_user_string(arg2)))
6013 goto efault;
Riku Voipio0b2effd2014-08-06 10:36:37 +03006014 ret = get_errno(do_openat(cpu_env, arg1, p,
6015 target_to_host_bitmask(arg3, fcntl_flags_tbl),
6016 arg4));
Laurent Viviere36800c2015-10-02 14:48:09 +02006017 fd_trans_unregister(ret);
bellard579a97f2007-11-11 14:26:47 +00006018 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00006019 break;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02006020#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6021 case TARGET_NR_name_to_handle_at:
6022 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
6023 break;
6024#endif
6025#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6026 case TARGET_NR_open_by_handle_at:
6027 ret = do_open_by_handle_at(arg1, arg2, arg3);
Laurent Viviere36800c2015-10-02 14:48:09 +02006028 fd_trans_unregister(ret);
Laurent Vivier0f0426f2015-09-01 22:27:33 +02006029 break;
6030#endif
bellard31e31b82003-02-18 22:55:36 +00006031 case TARGET_NR_close:
Laurent Viviere36800c2015-10-02 14:48:09 +02006032 fd_trans_unregister(arg1);
bellard31e31b82003-02-18 22:55:36 +00006033 ret = get_errno(close(arg1));
6034 break;
6035 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00006036 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00006037 break;
Chen Gang704eff62015-08-21 05:37:33 +08006038#ifdef TARGET_NR_fork
bellard31e31b82003-02-18 22:55:36 +00006039 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00006040 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00006041 break;
Chen Gang704eff62015-08-21 05:37:33 +08006042#endif
thse5febef2007-04-01 18:31:35 +00006043#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00006044 case TARGET_NR_waitpid:
6045 {
pbrook53a59602006-03-25 19:31:22 +00006046 int status;
6047 ret = get_errno(waitpid(arg1, &status, arg3));
Alexander Graf53795572011-11-24 00:44:43 +01006048 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00006049 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00006050 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006051 }
6052 break;
thse5febef2007-04-01 18:31:35 +00006053#endif
pbrookf0cbb612008-05-30 18:20:05 +00006054#ifdef TARGET_NR_waitid
6055 case TARGET_NR_waitid:
6056 {
6057 siginfo_t info;
6058 info.si_pid = 0;
6059 ret = get_errno(waitid(arg1, arg2, &info, arg4));
6060 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006061 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00006062 goto efault;
6063 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05006064 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00006065 }
6066 }
6067 break;
6068#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006069#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006070 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00006071 if (!(p = lock_user_string(arg1)))
6072 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006073 ret = get_errno(creat(p, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02006074 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00006075 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006076 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006077#endif
Chen Gang704eff62015-08-21 05:37:33 +08006078#ifdef TARGET_NR_link
bellard31e31b82003-02-18 22:55:36 +00006079 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00006080 {
6081 void * p2;
6082 p = lock_user_string(arg1);
6083 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006084 if (!p || !p2)
6085 ret = -TARGET_EFAULT;
6086 else
6087 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006088 unlock_user(p2, arg2, 0);
6089 unlock_user(p, arg1, 0);
6090 }
bellard31e31b82003-02-18 22:55:36 +00006091 break;
Chen Gang704eff62015-08-21 05:37:33 +08006092#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006093#if defined(TARGET_NR_linkat)
ths64f0ce42007-09-24 09:25:06 +00006094 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00006095 {
6096 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00006097 if (!arg2 || !arg4)
6098 goto efault;
ths64f0ce42007-09-24 09:25:06 +00006099 p = lock_user_string(arg2);
6100 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00006101 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006102 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00006103 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01006104 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00006105 unlock_user(p, arg2, 0);
6106 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00006107 }
6108 break;
6109#endif
Chen Gang704eff62015-08-21 05:37:33 +08006110#ifdef TARGET_NR_unlink
bellard31e31b82003-02-18 22:55:36 +00006111 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00006112 if (!(p = lock_user_string(arg1)))
6113 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006114 ret = get_errno(unlink(p));
6115 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006116 break;
Chen Gang704eff62015-08-21 05:37:33 +08006117#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006118#if defined(TARGET_NR_unlinkat)
ths8170f562007-09-24 09:24:11 +00006119 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00006120 if (!(p = lock_user_string(arg2)))
6121 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006122 ret = get_errno(unlinkat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00006123 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00006124 break;
balrogb7d35e62007-12-12 00:40:24 +00006125#endif
bellard31e31b82003-02-18 22:55:36 +00006126 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00006127 {
6128 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00006129 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00006130 abi_ulong gp;
6131 abi_ulong guest_argp;
6132 abi_ulong guest_envp;
6133 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00006134 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006135 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00006136
bellardf7341ff2003-03-30 21:00:25 +00006137 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00006138 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00006139 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00006140 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00006141 goto efault;
ths03aa1972007-12-02 06:28:08 +00006142 if (!addr)
bellard2f619692007-11-16 10:46:05 +00006143 break;
bellard7854b052003-03-29 17:22:23 +00006144 argc++;
bellard2f619692007-11-16 10:46:05 +00006145 }
bellardf7341ff2003-03-30 21:00:25 +00006146 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00006147 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00006148 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00006149 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00006150 goto efault;
ths03aa1972007-12-02 06:28:08 +00006151 if (!addr)
bellard2f619692007-11-16 10:46:05 +00006152 break;
bellard7854b052003-03-29 17:22:23 +00006153 envc++;
bellard2f619692007-11-16 10:46:05 +00006154 }
bellard7854b052003-03-29 17:22:23 +00006155
bellardf7341ff2003-03-30 21:00:25 +00006156 argp = alloca((argc + 1) * sizeof(void *));
6157 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00006158
pbrookda94d262008-05-30 18:24:00 +00006159 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00006160 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006161 if (get_user_ual(addr, gp))
6162 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00006163 if (!addr)
6164 break;
bellard2f619692007-11-16 10:46:05 +00006165 if (!(*q = lock_user_string(addr)))
6166 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006167 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00006168 }
bellardf7341ff2003-03-30 21:00:25 +00006169 *q = NULL;
6170
pbrookda94d262008-05-30 18:24:00 +00006171 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00006172 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006173 if (get_user_ual(addr, gp))
6174 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00006175 if (!addr)
6176 break;
bellard2f619692007-11-16 10:46:05 +00006177 if (!(*q = lock_user_string(addr)))
6178 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006179 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00006180 }
bellardf7341ff2003-03-30 21:00:25 +00006181 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00006182
bellard2f619692007-11-16 10:46:05 +00006183 if (!(p = lock_user_string(arg1)))
6184 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00006185 ret = get_errno(execve(p, argp, envp));
6186 unlock_user(p, arg1, 0);
6187
bellard2f619692007-11-16 10:46:05 +00006188 goto execve_end;
6189
6190 execve_efault:
6191 ret = -TARGET_EFAULT;
6192
6193 execve_end:
pbrook53a59602006-03-25 19:31:22 +00006194 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00006195 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006196 if (get_user_ual(addr, gp)
6197 || !addr)
6198 break;
pbrook53a59602006-03-25 19:31:22 +00006199 unlock_user(*q, addr, 0);
6200 }
6201 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00006202 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006203 if (get_user_ual(addr, gp)
6204 || !addr)
6205 break;
pbrook53a59602006-03-25 19:31:22 +00006206 unlock_user(*q, addr, 0);
6207 }
bellard7854b052003-03-29 17:22:23 +00006208 }
bellard31e31b82003-02-18 22:55:36 +00006209 break;
6210 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00006211 if (!(p = lock_user_string(arg1)))
6212 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006213 ret = get_errno(chdir(p));
6214 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006215 break;
bellarda315a142005-01-30 22:59:18 +00006216#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00006217 case TARGET_NR_time:
6218 {
pbrook53a59602006-03-25 19:31:22 +00006219 time_t host_time;
6220 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00006221 if (!is_error(ret)
6222 && arg1
6223 && put_user_sal(host_time, arg1))
6224 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006225 }
6226 break;
bellarda315a142005-01-30 22:59:18 +00006227#endif
Chen Gang704eff62015-08-21 05:37:33 +08006228#ifdef TARGET_NR_mknod
bellard31e31b82003-02-18 22:55:36 +00006229 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00006230 if (!(p = lock_user_string(arg1)))
6231 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006232 ret = get_errno(mknod(p, arg2, arg3));
6233 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006234 break;
Chen Gang704eff62015-08-21 05:37:33 +08006235#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006236#if defined(TARGET_NR_mknodat)
ths75ac37a2007-09-24 09:23:05 +00006237 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00006238 if (!(p = lock_user_string(arg2)))
6239 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006240 ret = get_errno(mknodat(arg1, p, arg3, arg4));
bellard579a97f2007-11-11 14:26:47 +00006241 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00006242 break;
6243#endif
Chen Gang704eff62015-08-21 05:37:33 +08006244#ifdef TARGET_NR_chmod
bellard31e31b82003-02-18 22:55:36 +00006245 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00006246 if (!(p = lock_user_string(arg1)))
6247 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006248 ret = get_errno(chmod(p, arg2));
6249 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006250 break;
Chen Gang704eff62015-08-21 05:37:33 +08006251#endif
bellardebc05482003-09-30 21:08:41 +00006252#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00006253 case TARGET_NR_break:
6254 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006255#endif
6256#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00006257 case TARGET_NR_oldstat:
6258 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006259#endif
bellard31e31b82003-02-18 22:55:36 +00006260 case TARGET_NR_lseek:
6261 ret = get_errno(lseek(arg1, arg2, arg3));
6262 break;
Richard Henderson92317332010-05-03 10:07:53 -07006263#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
6264 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00006265 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07006266 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00006267 ret = get_errno(getpid());
6268 break;
Richard Henderson92317332010-05-03 10:07:53 -07006269#endif
6270#ifdef TARGET_NR_getpid
6271 case TARGET_NR_getpid:
6272 ret = get_errno(getpid());
6273 break;
6274#endif
bellard31e31b82003-02-18 22:55:36 +00006275 case TARGET_NR_mount:
Paul Burton356d7712014-06-22 11:25:37 +01006276 {
6277 /* need to look at the data field */
6278 void *p2, *p3;
6279
6280 if (arg1) {
6281 p = lock_user_string(arg1);
6282 if (!p) {
6283 goto efault;
6284 }
6285 } else {
6286 p = NULL;
6287 }
6288
6289 p2 = lock_user_string(arg2);
6290 if (!p2) {
6291 if (arg1) {
6292 unlock_user(p, arg1, 0);
6293 }
6294 goto efault;
6295 }
6296
6297 if (arg3) {
6298 p3 = lock_user_string(arg3);
6299 if (!p3) {
6300 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00006301 unlock_user(p, arg1, 0);
Paul Burton356d7712014-06-22 11:25:37 +01006302 }
6303 unlock_user(p2, arg2, 0);
6304 goto efault;
6305 }
6306 } else {
6307 p3 = NULL;
6308 }
6309
6310 /* FIXME - arg5 should be locked, but it isn't clear how to
6311 * do that since it's not guaranteed to be a NULL-terminated
6312 * string.
6313 */
6314 if (!arg5) {
6315 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
6316 } else {
6317 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
6318 }
6319 ret = get_errno(ret);
6320
6321 if (arg1) {
6322 unlock_user(p, arg1, 0);
6323 }
6324 unlock_user(p2, arg2, 0);
6325 if (arg3) {
6326 unlock_user(p3, arg3, 0);
6327 }
6328 }
6329 break;
thse5febef2007-04-01 18:31:35 +00006330#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00006331 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00006332 if (!(p = lock_user_string(arg1)))
6333 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006334 ret = get_errno(umount(p));
6335 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006336 break;
thse5febef2007-04-01 18:31:35 +00006337#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006338#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006339 case TARGET_NR_stime:
6340 {
pbrook53a59602006-03-25 19:31:22 +00006341 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00006342 if (get_user_sal(host_time, arg1))
6343 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006344 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00006345 }
6346 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006347#endif
bellard31e31b82003-02-18 22:55:36 +00006348 case TARGET_NR_ptrace:
6349 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00006350#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006351 case TARGET_NR_alarm:
6352 ret = alarm(arg1);
6353 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006354#endif
bellardebc05482003-09-30 21:08:41 +00006355#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00006356 case TARGET_NR_oldfstat:
6357 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006358#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006359#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006360 case TARGET_NR_pause:
6361 ret = get_errno(pause());
6362 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006363#endif
thse5febef2007-04-01 18:31:35 +00006364#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00006365 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00006366 {
pbrook53a59602006-03-25 19:31:22 +00006367 struct utimbuf tbuf, *host_tbuf;
6368 struct target_utimbuf *target_tbuf;
6369 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00006370 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
6371 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006372 tbuf.actime = tswapal(target_tbuf->actime);
6373 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00006374 unlock_user_struct(target_tbuf, arg2, 0);
6375 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00006376 } else {
pbrook53a59602006-03-25 19:31:22 +00006377 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00006378 }
bellard579a97f2007-11-11 14:26:47 +00006379 if (!(p = lock_user_string(arg1)))
6380 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006381 ret = get_errno(utime(p, host_tbuf));
6382 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00006383 }
6384 break;
thse5febef2007-04-01 18:31:35 +00006385#endif
Chen Gang704eff62015-08-21 05:37:33 +08006386#ifdef TARGET_NR_utimes
bellard978a66f2004-12-06 22:58:05 +00006387 case TARGET_NR_utimes:
6388 {
bellard978a66f2004-12-06 22:58:05 +00006389 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00006390 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00006391 if (copy_from_user_timeval(&tv[0], arg2)
6392 || copy_from_user_timeval(&tv[1],
6393 arg2 + sizeof(struct target_timeval)))
6394 goto efault;
bellard978a66f2004-12-06 22:58:05 +00006395 tvp = tv;
6396 } else {
6397 tvp = NULL;
6398 }
bellard579a97f2007-11-11 14:26:47 +00006399 if (!(p = lock_user_string(arg1)))
6400 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006401 ret = get_errno(utimes(p, tvp));
6402 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00006403 }
6404 break;
Chen Gang704eff62015-08-21 05:37:33 +08006405#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006406#if defined(TARGET_NR_futimesat)
balrogac8a6552008-09-20 02:25:39 +00006407 case TARGET_NR_futimesat:
6408 {
6409 struct timeval *tvp, tv[2];
6410 if (arg3) {
6411 if (copy_from_user_timeval(&tv[0], arg3)
6412 || copy_from_user_timeval(&tv[1],
6413 arg3 + sizeof(struct target_timeval)))
6414 goto efault;
6415 tvp = tv;
6416 } else {
6417 tvp = NULL;
6418 }
6419 if (!(p = lock_user_string(arg2)))
6420 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006421 ret = get_errno(futimesat(arg1, path(p), tvp));
balrogac8a6552008-09-20 02:25:39 +00006422 unlock_user(p, arg2, 0);
6423 }
6424 break;
6425#endif
bellardebc05482003-09-30 21:08:41 +00006426#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00006427 case TARGET_NR_stty:
6428 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006429#endif
6430#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00006431 case TARGET_NR_gtty:
6432 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006433#endif
Chen Gang704eff62015-08-21 05:37:33 +08006434#ifdef TARGET_NR_access
bellard31e31b82003-02-18 22:55:36 +00006435 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00006436 if (!(p = lock_user_string(arg1)))
6437 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02006438 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00006439 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006440 break;
Chen Gang704eff62015-08-21 05:37:33 +08006441#endif
ths92a34c12007-09-24 09:27:49 +00006442#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
6443 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00006444 if (!(p = lock_user_string(arg2)))
6445 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006446 ret = get_errno(faccessat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00006447 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00006448 break;
6449#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006450#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006451 case TARGET_NR_nice:
6452 ret = get_errno(nice(arg1));
6453 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006454#endif
bellardebc05482003-09-30 21:08:41 +00006455#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00006456 case TARGET_NR_ftime:
6457 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006458#endif
bellard31e31b82003-02-18 22:55:36 +00006459 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00006460 sync();
6461 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00006462 break;
6463 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00006464 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00006465 break;
Chen Gang704eff62015-08-21 05:37:33 +08006466#ifdef TARGET_NR_rename
bellard31e31b82003-02-18 22:55:36 +00006467 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00006468 {
6469 void *p2;
6470 p = lock_user_string(arg1);
6471 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006472 if (!p || !p2)
6473 ret = -TARGET_EFAULT;
6474 else
6475 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006476 unlock_user(p2, arg2, 0);
6477 unlock_user(p, arg1, 0);
6478 }
bellard31e31b82003-02-18 22:55:36 +00006479 break;
Chen Gang704eff62015-08-21 05:37:33 +08006480#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006481#if defined(TARGET_NR_renameat)
ths722183f2007-09-24 09:24:37 +00006482 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00006483 {
bellard579a97f2007-11-11 14:26:47 +00006484 void *p2;
ths722183f2007-09-24 09:24:37 +00006485 p = lock_user_string(arg2);
6486 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00006487 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006488 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00006489 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01006490 ret = get_errno(renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00006491 unlock_user(p2, arg4, 0);
6492 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00006493 }
6494 break;
6495#endif
Chen Gang704eff62015-08-21 05:37:33 +08006496#ifdef TARGET_NR_mkdir
bellard31e31b82003-02-18 22:55:36 +00006497 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00006498 if (!(p = lock_user_string(arg1)))
6499 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006500 ret = get_errno(mkdir(p, arg2));
6501 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006502 break;
Chen Gang704eff62015-08-21 05:37:33 +08006503#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006504#if defined(TARGET_NR_mkdirat)
ths4472ad02007-09-24 09:22:32 +00006505 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00006506 if (!(p = lock_user_string(arg2)))
6507 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006508 ret = get_errno(mkdirat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00006509 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00006510 break;
6511#endif
Chen Gang704eff62015-08-21 05:37:33 +08006512#ifdef TARGET_NR_rmdir
bellard31e31b82003-02-18 22:55:36 +00006513 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00006514 if (!(p = lock_user_string(arg1)))
6515 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006516 ret = get_errno(rmdir(p));
6517 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006518 break;
Chen Gang704eff62015-08-21 05:37:33 +08006519#endif
bellard31e31b82003-02-18 22:55:36 +00006520 case TARGET_NR_dup:
6521 ret = get_errno(dup(arg1));
Laurent Viviere36800c2015-10-02 14:48:09 +02006522 if (ret >= 0) {
6523 fd_trans_dup(arg1, ret);
6524 }
bellard31e31b82003-02-18 22:55:36 +00006525 break;
Chen Gang704eff62015-08-21 05:37:33 +08006526#ifdef TARGET_NR_pipe
bellard31e31b82003-02-18 22:55:36 +00006527 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07006528 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00006529 break;
Chen Gang704eff62015-08-21 05:37:33 +08006530#endif
Riku Voipio099d6b02009-05-05 12:10:04 +03006531#ifdef TARGET_NR_pipe2
6532 case TARGET_NR_pipe2:
Richard Hendersone7ea6cb2012-06-01 18:48:39 -07006533 ret = do_pipe(cpu_env, arg1,
6534 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03006535 break;
6536#endif
bellard31e31b82003-02-18 22:55:36 +00006537 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00006538 {
pbrook53a59602006-03-25 19:31:22 +00006539 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00006540 struct tms tms;
6541 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00006542 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00006543 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
6544 if (!tmsp)
6545 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006546 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
6547 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
6548 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
6549 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00006550 }
bellardc596ed12003-07-13 17:32:31 +00006551 if (!is_error(ret))
6552 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00006553 }
6554 break;
bellardebc05482003-09-30 21:08:41 +00006555#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00006556 case TARGET_NR_prof:
6557 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006558#endif
thse5febef2007-04-01 18:31:35 +00006559#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00006560 case TARGET_NR_signal:
6561 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006562#endif
bellard31e31b82003-02-18 22:55:36 +00006563 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00006564 if (arg1 == 0) {
6565 ret = get_errno(acct(NULL));
6566 } else {
6567 if (!(p = lock_user_string(arg1)))
6568 goto efault;
6569 ret = get_errno(acct(path(p)));
6570 unlock_user(p, arg1, 0);
6571 }
pbrook24836682006-04-16 14:14:53 +00006572 break;
Richard Henderson8070e7b2013-07-24 09:50:00 -10006573#ifdef TARGET_NR_umount2
bellard31e31b82003-02-18 22:55:36 +00006574 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00006575 if (!(p = lock_user_string(arg1)))
6576 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006577 ret = get_errno(umount2(p, arg2));
6578 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006579 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006580#endif
bellardebc05482003-09-30 21:08:41 +00006581#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00006582 case TARGET_NR_lock:
6583 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006584#endif
bellard31e31b82003-02-18 22:55:36 +00006585 case TARGET_NR_ioctl:
6586 ret = do_ioctl(arg1, arg2, arg3);
6587 break;
6588 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00006589 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00006590 break;
bellardebc05482003-09-30 21:08:41 +00006591#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00006592 case TARGET_NR_mpx:
6593 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006594#endif
bellard31e31b82003-02-18 22:55:36 +00006595 case TARGET_NR_setpgid:
6596 ret = get_errno(setpgid(arg1, arg2));
6597 break;
bellardebc05482003-09-30 21:08:41 +00006598#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00006599 case TARGET_NR_ulimit:
6600 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006601#endif
6602#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00006603 case TARGET_NR_oldolduname:
6604 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006605#endif
bellard31e31b82003-02-18 22:55:36 +00006606 case TARGET_NR_umask:
6607 ret = get_errno(umask(arg1));
6608 break;
6609 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00006610 if (!(p = lock_user_string(arg1)))
6611 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006612 ret = get_errno(chroot(p));
6613 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006614 break;
Chen Gang704eff62015-08-21 05:37:33 +08006615#ifdef TARGET_NR_ustat
bellard31e31b82003-02-18 22:55:36 +00006616 case TARGET_NR_ustat:
6617 goto unimplemented;
Chen Gang704eff62015-08-21 05:37:33 +08006618#endif
6619#ifdef TARGET_NR_dup2
bellard31e31b82003-02-18 22:55:36 +00006620 case TARGET_NR_dup2:
6621 ret = get_errno(dup2(arg1, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02006622 if (ret >= 0) {
6623 fd_trans_dup(arg1, arg2);
6624 }
bellard31e31b82003-02-18 22:55:36 +00006625 break;
Chen Gang704eff62015-08-21 05:37:33 +08006626#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03006627#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
6628 case TARGET_NR_dup3:
6629 ret = get_errno(dup3(arg1, arg2, arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02006630 if (ret >= 0) {
6631 fd_trans_dup(arg1, arg2);
6632 }
Ulrich Hechtd0927932009-09-17 20:22:14 +03006633 break;
6634#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006635#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006636 case TARGET_NR_getppid:
6637 ret = get_errno(getppid());
6638 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006639#endif
Chen Gang704eff62015-08-21 05:37:33 +08006640#ifdef TARGET_NR_getpgrp
bellard31e31b82003-02-18 22:55:36 +00006641 case TARGET_NR_getpgrp:
6642 ret = get_errno(getpgrp());
6643 break;
Chen Gang704eff62015-08-21 05:37:33 +08006644#endif
bellard31e31b82003-02-18 22:55:36 +00006645 case TARGET_NR_setsid:
6646 ret = get_errno(setsid());
6647 break;
thse5febef2007-04-01 18:31:35 +00006648#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00006649 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00006650 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08006651#if defined(TARGET_ALPHA)
6652 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00006653 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00006654 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00006655 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6656 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006657 act._sa_handler = old_act->_sa_handler;
6658 target_siginitset(&act.sa_mask, old_act->sa_mask);
6659 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08006660 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00006661 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006662 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00006663 }
6664 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00006665 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00006666 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6667 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006668 old_act->_sa_handler = oact._sa_handler;
6669 old_act->sa_mask = oact.sa_mask.sig[0];
6670 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00006671 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00006672 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08006673#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00006674 struct target_sigaction act, oact, *pact, *old_act;
6675
6676 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00006677 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6678 goto efault;
bellard106ec872006-06-27 21:08:10 +00006679 act._sa_handler = old_act->_sa_handler;
6680 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
6681 act.sa_flags = old_act->sa_flags;
6682 unlock_user_struct(old_act, arg2, 0);
6683 pact = &act;
6684 } else {
6685 pact = NULL;
6686 }
6687
6688 ret = get_errno(do_sigaction(arg1, pact, &oact));
6689
6690 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00006691 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6692 goto efault;
bellard106ec872006-06-27 21:08:10 +00006693 old_act->_sa_handler = oact._sa_handler;
6694 old_act->sa_flags = oact.sa_flags;
6695 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
6696 old_act->sa_mask.sig[1] = 0;
6697 old_act->sa_mask.sig[2] = 0;
6698 old_act->sa_mask.sig[3] = 0;
6699 unlock_user_struct(old_act, arg3, 1);
6700 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08006701#else
6702 struct target_old_sigaction *old_act;
6703 struct target_sigaction act, oact, *pact;
6704 if (arg2) {
6705 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6706 goto efault;
6707 act._sa_handler = old_act->_sa_handler;
6708 target_siginitset(&act.sa_mask, old_act->sa_mask);
6709 act.sa_flags = old_act->sa_flags;
6710 act.sa_restorer = old_act->sa_restorer;
6711 unlock_user_struct(old_act, arg2, 0);
6712 pact = &act;
6713 } else {
6714 pact = NULL;
6715 }
6716 ret = get_errno(do_sigaction(arg1, pact, &oact));
6717 if (!is_error(ret) && arg3) {
6718 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6719 goto efault;
6720 old_act->_sa_handler = oact._sa_handler;
6721 old_act->sa_mask = oact.sa_mask.sig[0];
6722 old_act->sa_flags = oact.sa_flags;
6723 old_act->sa_restorer = oact.sa_restorer;
6724 unlock_user_struct(old_act, arg3, 1);
6725 }
ths388bb212007-05-13 13:58:00 +00006726#endif
bellard31e31b82003-02-18 22:55:36 +00006727 }
6728 break;
thse5febef2007-04-01 18:31:35 +00006729#endif
bellard66fb9762003-03-23 01:06:05 +00006730 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00006731 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08006732#if defined(TARGET_ALPHA)
6733 struct target_sigaction act, oact, *pact = 0;
6734 struct target_rt_sigaction *rt_act;
6735 /* ??? arg4 == sizeof(sigset_t). */
6736 if (arg2) {
6737 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
6738 goto efault;
6739 act._sa_handler = rt_act->_sa_handler;
6740 act.sa_mask = rt_act->sa_mask;
6741 act.sa_flags = rt_act->sa_flags;
6742 act.sa_restorer = arg5;
6743 unlock_user_struct(rt_act, arg2, 0);
6744 pact = &act;
6745 }
6746 ret = get_errno(do_sigaction(arg1, pact, &oact));
6747 if (!is_error(ret) && arg3) {
6748 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
6749 goto efault;
6750 rt_act->_sa_handler = oact._sa_handler;
6751 rt_act->sa_mask = oact.sa_mask;
6752 rt_act->sa_flags = oact.sa_flags;
6753 unlock_user_struct(rt_act, arg3, 1);
6754 }
6755#else
pbrook53a59602006-03-25 19:31:22 +00006756 struct target_sigaction *act;
6757 struct target_sigaction *oact;
6758
bellard579a97f2007-11-11 14:26:47 +00006759 if (arg2) {
6760 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
6761 goto efault;
6762 } else
pbrook53a59602006-03-25 19:31:22 +00006763 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00006764 if (arg3) {
6765 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
6766 ret = -TARGET_EFAULT;
6767 goto rt_sigaction_fail;
6768 }
6769 } else
pbrook53a59602006-03-25 19:31:22 +00006770 oact = NULL;
6771 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00006772 rt_sigaction_fail:
6773 if (act)
pbrook53a59602006-03-25 19:31:22 +00006774 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00006775 if (oact)
pbrook53a59602006-03-25 19:31:22 +00006776 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08006777#endif
pbrook53a59602006-03-25 19:31:22 +00006778 }
bellard66fb9762003-03-23 01:06:05 +00006779 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006780#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006781 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00006782 {
6783 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00006784 abi_ulong target_set;
Alex Barcelo1c275922014-03-14 14:36:55 +00006785 do_sigprocmask(0, NULL, &cur_set);
bellard66fb9762003-03-23 01:06:05 +00006786 host_to_target_old_sigset(&target_set, &cur_set);
6787 ret = target_set;
6788 }
6789 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006790#endif
6791#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006792 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00006793 {
6794 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00006795 abi_ulong target_set = arg1;
Alex Barcelo1c275922014-03-14 14:36:55 +00006796 do_sigprocmask(0, NULL, &cur_set);
bellard66fb9762003-03-23 01:06:05 +00006797 target_to_host_old_sigset(&set, &target_set);
6798 sigorset(&set, &set, &cur_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00006799 do_sigprocmask(SIG_SETMASK, &set, &oset);
bellard66fb9762003-03-23 01:06:05 +00006800 host_to_target_old_sigset(&target_set, &oset);
6801 ret = target_set;
6802 }
6803 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006804#endif
thse5febef2007-04-01 18:31:35 +00006805#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00006806 case TARGET_NR_sigprocmask:
6807 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07006808#if defined(TARGET_ALPHA)
6809 sigset_t set, oldset;
6810 abi_ulong mask;
6811 int how;
6812
6813 switch (arg1) {
6814 case TARGET_SIG_BLOCK:
6815 how = SIG_BLOCK;
6816 break;
6817 case TARGET_SIG_UNBLOCK:
6818 how = SIG_UNBLOCK;
6819 break;
6820 case TARGET_SIG_SETMASK:
6821 how = SIG_SETMASK;
6822 break;
6823 default:
6824 ret = -TARGET_EINVAL;
6825 goto fail;
6826 }
6827 mask = arg2;
6828 target_to_host_old_sigset(&set, &mask);
6829
Alex Barcelo1c275922014-03-14 14:36:55 +00006830 ret = get_errno(do_sigprocmask(how, &set, &oldset));
Richard Hendersona5b3b132010-05-03 10:07:55 -07006831 if (!is_error(ret)) {
6832 host_to_target_old_sigset(&mask, &oldset);
6833 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07006834 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07006835 }
6836#else
bellard66fb9762003-03-23 01:06:05 +00006837 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07006838 int how;
ths3b46e622007-09-17 08:09:54 +00006839
pbrook53a59602006-03-25 19:31:22 +00006840 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07006841 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00006842 case TARGET_SIG_BLOCK:
6843 how = SIG_BLOCK;
6844 break;
6845 case TARGET_SIG_UNBLOCK:
6846 how = SIG_UNBLOCK;
6847 break;
6848 case TARGET_SIG_SETMASK:
6849 how = SIG_SETMASK;
6850 break;
6851 default:
ths0da46a62007-10-20 20:23:07 +00006852 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006853 goto fail;
6854 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006855 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006856 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006857 target_to_host_old_sigset(&set, p);
6858 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006859 set_ptr = &set;
6860 } else {
6861 how = 0;
6862 set_ptr = NULL;
6863 }
Alex Barcelo1c275922014-03-14 14:36:55 +00006864 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006865 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006866 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006867 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006868 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006869 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006870 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07006871#endif
bellard66fb9762003-03-23 01:06:05 +00006872 }
6873 break;
thse5febef2007-04-01 18:31:35 +00006874#endif
bellard66fb9762003-03-23 01:06:05 +00006875 case TARGET_NR_rt_sigprocmask:
6876 {
6877 int how = arg1;
6878 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00006879
pbrook53a59602006-03-25 19:31:22 +00006880 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006881 switch(how) {
6882 case TARGET_SIG_BLOCK:
6883 how = SIG_BLOCK;
6884 break;
6885 case TARGET_SIG_UNBLOCK:
6886 how = SIG_UNBLOCK;
6887 break;
6888 case TARGET_SIG_SETMASK:
6889 how = SIG_SETMASK;
6890 break;
6891 default:
ths0da46a62007-10-20 20:23:07 +00006892 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006893 goto fail;
6894 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006895 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006896 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006897 target_to_host_sigset(&set, p);
6898 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006899 set_ptr = &set;
6900 } else {
6901 how = 0;
6902 set_ptr = NULL;
6903 }
Alex Barcelo1c275922014-03-14 14:36:55 +00006904 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006905 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006906 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006907 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006908 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006909 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006910 }
6911 }
6912 break;
thse5febef2007-04-01 18:31:35 +00006913#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00006914 case TARGET_NR_sigpending:
6915 {
6916 sigset_t set;
6917 ret = get_errno(sigpending(&set));
6918 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006919 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006920 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006921 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006922 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006923 }
6924 }
6925 break;
thse5febef2007-04-01 18:31:35 +00006926#endif
bellard66fb9762003-03-23 01:06:05 +00006927 case TARGET_NR_rt_sigpending:
6928 {
6929 sigset_t set;
6930 ret = get_errno(sigpending(&set));
6931 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006932 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006933 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006934 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006935 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006936 }
6937 }
6938 break;
thse5febef2007-04-01 18:31:35 +00006939#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00006940 case TARGET_NR_sigsuspend:
6941 {
6942 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07006943#if defined(TARGET_ALPHA)
6944 abi_ulong mask = arg1;
6945 target_to_host_old_sigset(&set, &mask);
6946#else
Anthony Liguoric227f092009-10-01 16:12:16 -05006947 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006948 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006949 target_to_host_old_sigset(&set, p);
6950 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07006951#endif
bellard66fb9762003-03-23 01:06:05 +00006952 ret = get_errno(sigsuspend(&set));
6953 }
6954 break;
thse5febef2007-04-01 18:31:35 +00006955#endif
bellard66fb9762003-03-23 01:06:05 +00006956 case TARGET_NR_rt_sigsuspend:
6957 {
6958 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05006959 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006960 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006961 target_to_host_sigset(&set, p);
6962 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006963 ret = get_errno(sigsuspend(&set));
6964 }
6965 break;
6966 case TARGET_NR_rt_sigtimedwait:
6967 {
bellard66fb9762003-03-23 01:06:05 +00006968 sigset_t set;
6969 struct timespec uts, *puts;
6970 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00006971
Anthony Liguoric227f092009-10-01 16:12:16 -05006972 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006973 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006974 target_to_host_sigset(&set, p);
6975 unlock_user(p, arg1, 0);
6976 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00006977 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00006978 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00006979 } else {
6980 puts = NULL;
6981 }
6982 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
Petar Jovanovic974a1962014-03-03 15:07:41 +01006983 if (!is_error(ret)) {
6984 if (arg2) {
6985 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
6986 0);
6987 if (!p) {
6988 goto efault;
6989 }
6990 host_to_target_siginfo(p, &uinfo);
6991 unlock_user(p, arg2, sizeof(target_siginfo_t));
6992 }
6993 ret = host_to_target_signal(ret);
bellard66fb9762003-03-23 01:06:05 +00006994 }
6995 }
6996 break;
6997 case TARGET_NR_rt_sigqueueinfo:
6998 {
6999 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05007000 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00007001 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007002 target_to_host_siginfo(&uinfo, p);
7003 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00007004 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
7005 }
7006 break;
thse5febef2007-04-01 18:31:35 +00007007#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00007008 case TARGET_NR_sigreturn:
bellard66fb9762003-03-23 01:06:05 +00007009 ret = do_sigreturn(cpu_env);
7010 break;
thse5febef2007-04-01 18:31:35 +00007011#endif
bellard66fb9762003-03-23 01:06:05 +00007012 case TARGET_NR_rt_sigreturn:
bellard66fb9762003-03-23 01:06:05 +00007013 ret = do_rt_sigreturn(cpu_env);
7014 break;
bellard31e31b82003-02-18 22:55:36 +00007015 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00007016 if (!(p = lock_user_string(arg1)))
7017 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007018 ret = get_errno(sethostname(p, arg2));
7019 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007020 break;
7021 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00007022 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007023 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00007024 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00007025 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00007026 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
7027 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007028 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
7029 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007030 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00007031 ret = get_errno(setrlimit(resource, &rlim));
7032 }
7033 break;
bellard31e31b82003-02-18 22:55:36 +00007034 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00007035 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007036 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00007037 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00007038 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00007039
bellard9de5e442003-03-23 16:49:39 +00007040 ret = get_errno(getrlimit(resource, &rlim));
7041 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007042 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7043 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007044 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7045 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007046 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00007047 }
7048 }
7049 break;
bellard31e31b82003-02-18 22:55:36 +00007050 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00007051 {
7052 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00007053 ret = get_errno(getrusage(arg1, &rusage));
7054 if (!is_error(ret)) {
Petar Jovanovica39fb272014-04-08 19:24:30 +02007055 ret = host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00007056 }
7057 }
7058 break;
bellard31e31b82003-02-18 22:55:36 +00007059 case TARGET_NR_gettimeofday:
7060 {
bellard31e31b82003-02-18 22:55:36 +00007061 struct timeval tv;
7062 ret = get_errno(gettimeofday(&tv, NULL));
7063 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00007064 if (copy_to_user_timeval(arg1, &tv))
7065 goto efault;
bellard31e31b82003-02-18 22:55:36 +00007066 }
7067 }
7068 break;
7069 case TARGET_NR_settimeofday:
7070 {
Paul Burtonb67d8032014-06-22 11:25:41 +01007071 struct timeval tv, *ptv = NULL;
Paul Burtonef4467e2014-06-22 11:25:40 +01007072 struct timezone tz, *ptz = NULL;
7073
Paul Burtonb67d8032014-06-22 11:25:41 +01007074 if (arg1) {
7075 if (copy_from_user_timeval(&tv, arg1)) {
7076 goto efault;
7077 }
7078 ptv = &tv;
7079 }
Paul Burtonef4467e2014-06-22 11:25:40 +01007080
7081 if (arg2) {
7082 if (copy_from_user_timezone(&tz, arg2)) {
7083 goto efault;
7084 }
7085 ptz = &tz;
7086 }
7087
Paul Burtonb67d8032014-06-22 11:25:41 +01007088 ret = get_errno(settimeofday(ptv, ptz));
bellard31e31b82003-02-18 22:55:36 +00007089 }
7090 break;
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007091#if defined(TARGET_NR_select)
bellard31e31b82003-02-18 22:55:36 +00007092 case TARGET_NR_select:
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007093#if defined(TARGET_S390X) || defined(TARGET_ALPHA)
7094 ret = do_select(arg1, arg2, arg3, arg4, arg5);
7095#else
bellardf2674e32003-07-09 12:26:09 +00007096 {
pbrook53a59602006-03-25 19:31:22 +00007097 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00007098 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00007099 long nsel;
7100
bellard579a97f2007-11-11 14:26:47 +00007101 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
7102 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007103 nsel = tswapal(sel->n);
7104 inp = tswapal(sel->inp);
7105 outp = tswapal(sel->outp);
7106 exp = tswapal(sel->exp);
7107 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00007108 unlock_user_struct(sel, arg1, 0);
7109 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00007110 }
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007111#endif
bellardf2674e32003-07-09 12:26:09 +00007112 break;
bellard048f6b42005-11-26 18:47:20 +00007113#endif
Riku Voipio9e423822010-05-07 12:28:05 +00007114#ifdef TARGET_NR_pselect6
7115 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04007116 {
7117 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
7118 fd_set rfds, wfds, efds;
7119 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
7120 struct timespec ts, *ts_ptr;
7121
7122 /*
7123 * The 6th arg is actually two args smashed together,
7124 * so we cannot use the C library.
7125 */
7126 sigset_t set;
7127 struct {
7128 sigset_t *set;
7129 size_t size;
7130 } sig, *sig_ptr;
7131
7132 abi_ulong arg_sigset, arg_sigsize, *arg7;
7133 target_sigset_t *target_sigset;
7134
7135 n = arg1;
7136 rfd_addr = arg2;
7137 wfd_addr = arg3;
7138 efd_addr = arg4;
7139 ts_addr = arg5;
7140
7141 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
7142 if (ret) {
7143 goto fail;
7144 }
7145 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
7146 if (ret) {
7147 goto fail;
7148 }
7149 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
7150 if (ret) {
7151 goto fail;
7152 }
7153
7154 /*
7155 * This takes a timespec, and not a timeval, so we cannot
7156 * use the do_select() helper ...
7157 */
7158 if (ts_addr) {
7159 if (target_to_host_timespec(&ts, ts_addr)) {
7160 goto efault;
7161 }
7162 ts_ptr = &ts;
7163 } else {
7164 ts_ptr = NULL;
7165 }
7166
7167 /* Extract the two packed args for the sigset */
7168 if (arg6) {
7169 sig_ptr = &sig;
7170 sig.size = _NSIG / 8;
7171
7172 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
7173 if (!arg7) {
7174 goto efault;
7175 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007176 arg_sigset = tswapal(arg7[0]);
7177 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04007178 unlock_user(arg7, arg6, 0);
7179
7180 if (arg_sigset) {
7181 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01007182 if (arg_sigsize != sizeof(*target_sigset)) {
7183 /* Like the kernel, we enforce correct size sigsets */
7184 ret = -TARGET_EINVAL;
7185 goto fail;
7186 }
Mike Frysinger055e0902011-06-03 17:01:49 -04007187 target_sigset = lock_user(VERIFY_READ, arg_sigset,
7188 sizeof(*target_sigset), 1);
7189 if (!target_sigset) {
7190 goto efault;
7191 }
7192 target_to_host_sigset(&set, target_sigset);
7193 unlock_user(target_sigset, arg_sigset, 0);
7194 } else {
7195 sig.set = NULL;
7196 }
7197 } else {
7198 sig_ptr = NULL;
7199 }
7200
7201 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
7202 ts_ptr, sig_ptr));
7203
7204 if (!is_error(ret)) {
7205 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
7206 goto efault;
7207 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
7208 goto efault;
7209 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
7210 goto efault;
7211
7212 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
7213 goto efault;
7214 }
7215 }
7216 break;
Riku Voipio9e423822010-05-07 12:28:05 +00007217#endif
Chen Gang704eff62015-08-21 05:37:33 +08007218#ifdef TARGET_NR_symlink
bellard31e31b82003-02-18 22:55:36 +00007219 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00007220 {
7221 void *p2;
7222 p = lock_user_string(arg1);
7223 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00007224 if (!p || !p2)
7225 ret = -TARGET_EFAULT;
7226 else
7227 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00007228 unlock_user(p2, arg2, 0);
7229 unlock_user(p, arg1, 0);
7230 }
bellard31e31b82003-02-18 22:55:36 +00007231 break;
Chen Gang704eff62015-08-21 05:37:33 +08007232#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007233#if defined(TARGET_NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +00007234 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00007235 {
bellard579a97f2007-11-11 14:26:47 +00007236 void *p2;
thsf0b62432007-09-24 09:25:40 +00007237 p = lock_user_string(arg1);
7238 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00007239 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00007240 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00007241 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01007242 ret = get_errno(symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00007243 unlock_user(p2, arg3, 0);
7244 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00007245 }
7246 break;
7247#endif
bellardebc05482003-09-30 21:08:41 +00007248#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00007249 case TARGET_NR_oldlstat:
7250 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007251#endif
Chen Gang704eff62015-08-21 05:37:33 +08007252#ifdef TARGET_NR_readlink
bellard31e31b82003-02-18 22:55:36 +00007253 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00007254 {
Andreas Schwab463d8e72013-07-02 14:04:12 +01007255 void *p2;
pbrook53a59602006-03-25 19:31:22 +00007256 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00007257 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01007258 if (!p || !p2) {
bellard579a97f2007-11-11 14:26:47 +00007259 ret = -TARGET_EFAULT;
Mike Frysingerf17f4982014-08-08 09:40:25 +09007260 } else if (!arg3) {
7261 /* Short circuit this for the magic exe check. */
7262 ret = -TARGET_EINVAL;
Andreas Schwab463d8e72013-07-02 14:04:12 +01007263 } else if (is_proc_myself((const char *)p, "exe")) {
7264 char real[PATH_MAX], *temp;
7265 temp = realpath(exec_path, real);
Mike Frysingerf17f4982014-08-08 09:40:25 +09007266 /* Return value is # of bytes that we wrote to the buffer. */
7267 if (temp == NULL) {
7268 ret = get_errno(-1);
7269 } else {
7270 /* Don't worry about sign mismatch as earlier mapping
7271 * logic would have thrown a bad address error. */
7272 ret = MIN(strlen(real), arg3);
7273 /* We cannot NUL terminate the string. */
7274 memcpy(p2, real, ret);
7275 }
Andreas Schwab463d8e72013-07-02 14:04:12 +01007276 } else {
7277 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00007278 }
pbrook53a59602006-03-25 19:31:22 +00007279 unlock_user(p2, arg2, ret);
7280 unlock_user(p, arg1, 0);
7281 }
bellard31e31b82003-02-18 22:55:36 +00007282 break;
Chen Gang704eff62015-08-21 05:37:33 +08007283#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007284#if defined(TARGET_NR_readlinkat)
ths5e0ccb12007-09-24 09:26:10 +00007285 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00007286 {
bellard579a97f2007-11-11 14:26:47 +00007287 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00007288 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00007289 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01007290 if (!p || !p2) {
7291 ret = -TARGET_EFAULT;
7292 } else if (is_proc_myself((const char *)p, "exe")) {
7293 char real[PATH_MAX], *temp;
7294 temp = realpath(exec_path, real);
7295 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
7296 snprintf((char *)p2, arg4, "%s", real);
7297 } else {
Peter Maydellc0d472b2013-06-12 16:20:21 +01007298 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
Andreas Schwab463d8e72013-07-02 14:04:12 +01007299 }
bellard579a97f2007-11-11 14:26:47 +00007300 unlock_user(p2, arg3, ret);
7301 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00007302 }
7303 break;
7304#endif
thse5febef2007-04-01 18:31:35 +00007305#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00007306 case TARGET_NR_uselib:
7307 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007308#endif
7309#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00007310 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00007311 if (!(p = lock_user_string(arg1)))
7312 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007313 ret = get_errno(swapon(p, arg2));
7314 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007315 break;
thse5febef2007-04-01 18:31:35 +00007316#endif
bellard31e31b82003-02-18 22:55:36 +00007317 case TARGET_NR_reboot:
Laurent Vivierc07ecc62013-01-07 11:40:06 +00007318 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
7319 /* arg4 must be ignored in all other cases */
7320 p = lock_user_string(arg4);
7321 if (!p) {
7322 goto efault;
7323 }
7324 ret = get_errno(reboot(arg1, arg2, arg3, p));
7325 unlock_user(p, arg4, 0);
7326 } else {
7327 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
7328 }
Alexander Graf0f6b4d22011-09-27 14:39:42 +02007329 break;
thse5febef2007-04-01 18:31:35 +00007330#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00007331 case TARGET_NR_readdir:
7332 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007333#endif
7334#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00007335 case TARGET_NR_mmap:
Alexander Graf09701192013-09-03 20:12:15 +01007336#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7337 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007338 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
7339 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00007340 {
blueswir1992f48a2007-10-14 16:27:31 +00007341 abi_ulong *v;
7342 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00007343 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
7344 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007345 v1 = tswapal(v[0]);
7346 v2 = tswapal(v[1]);
7347 v3 = tswapal(v[2]);
7348 v4 = tswapal(v[3]);
7349 v5 = tswapal(v[4]);
7350 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00007351 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00007352 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00007353 target_to_host_bitmask(v4, mmap_flags_tbl),
7354 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00007355 }
bellard31e31b82003-02-18 22:55:36 +00007356#else
ths5fafdf22007-09-16 21:08:06 +00007357 ret = get_errno(target_mmap(arg1, arg2, arg3,
7358 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00007359 arg5,
7360 arg6));
bellard31e31b82003-02-18 22:55:36 +00007361#endif
bellard6fb883e2003-07-09 17:12:39 +00007362 break;
thse5febef2007-04-01 18:31:35 +00007363#endif
bellarda315a142005-01-30 22:59:18 +00007364#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00007365 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00007366#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00007367#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00007368#endif
ths5fafdf22007-09-16 21:08:06 +00007369 ret = get_errno(target_mmap(arg1, arg2, arg3,
7370 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00007371 arg5,
bellardc573ff62004-01-04 15:51:36 +00007372 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00007373 break;
bellarda315a142005-01-30 22:59:18 +00007374#endif
bellard31e31b82003-02-18 22:55:36 +00007375 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00007376 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00007377 break;
bellard9de5e442003-03-23 16:49:39 +00007378 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01007379 {
Andreas Färber0429a972013-08-26 18:14:44 +02007380 TaskState *ts = cpu->opaque;
Paul Brook97374d32010-06-16 13:03:51 +01007381 /* Special hack to detect libc making the stack executable. */
7382 if ((arg3 & PROT_GROWSDOWN)
7383 && arg1 >= ts->info->stack_limit
7384 && arg1 <= ts->info->start_stack) {
7385 arg3 &= ~PROT_GROWSDOWN;
7386 arg2 = arg2 + arg1 - ts->info->stack_limit;
7387 arg1 = ts->info->stack_limit;
7388 }
7389 }
bellard54936002003-05-13 00:25:15 +00007390 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00007391 break;
thse5febef2007-04-01 18:31:35 +00007392#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00007393 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00007394 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00007395 break;
thse5febef2007-04-01 18:31:35 +00007396#endif
pbrook53a59602006-03-25 19:31:22 +00007397 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00007398#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00007399 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00007400 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00007401 break;
thse5febef2007-04-01 18:31:35 +00007402#endif
7403#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00007404 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00007405 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00007406 break;
thse5febef2007-04-01 18:31:35 +00007407#endif
7408#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00007409 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00007410 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00007411 break;
thse5febef2007-04-01 18:31:35 +00007412#endif
7413#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00007414 case TARGET_NR_mlockall:
Tom Musta6f6a4032014-08-12 13:53:42 -05007415 ret = get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
bellard9de5e442003-03-23 16:49:39 +00007416 break;
thse5febef2007-04-01 18:31:35 +00007417#endif
7418#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00007419 case TARGET_NR_munlockall:
7420 ret = get_errno(munlockall());
7421 break;
thse5febef2007-04-01 18:31:35 +00007422#endif
bellard31e31b82003-02-18 22:55:36 +00007423 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00007424 if (!(p = lock_user_string(arg1)))
7425 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007426 ret = get_errno(truncate(p, arg2));
7427 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007428 break;
7429 case TARGET_NR_ftruncate:
7430 ret = get_errno(ftruncate(arg1, arg2));
7431 break;
7432 case TARGET_NR_fchmod:
7433 ret = get_errno(fchmod(arg1, arg2));
7434 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007435#if defined(TARGET_NR_fchmodat)
ths814d7972007-09-24 09:26:51 +00007436 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00007437 if (!(p = lock_user_string(arg2)))
7438 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007439 ret = get_errno(fchmodat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00007440 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00007441 break;
7442#endif
bellard31e31b82003-02-18 22:55:36 +00007443 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -07007444 /* Note that negative values are valid for getpriority, so we must
7445 differentiate based on errno settings. */
7446 errno = 0;
7447 ret = getpriority(arg1, arg2);
7448 if (ret == -1 && errno != 0) {
7449 ret = -host_to_target_errno(errno);
7450 break;
7451 }
7452#ifdef TARGET_ALPHA
7453 /* Return value is the unbiased priority. Signal no error. */
7454 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
7455#else
7456 /* Return value is a biased priority to avoid negative numbers. */
7457 ret = 20 - ret;
7458#endif
bellard31e31b82003-02-18 22:55:36 +00007459 break;
7460 case TARGET_NR_setpriority:
7461 ret = get_errno(setpriority(arg1, arg2, arg3));
7462 break;
bellardebc05482003-09-30 21:08:41 +00007463#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00007464 case TARGET_NR_profil:
7465 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007466#endif
bellard31e31b82003-02-18 22:55:36 +00007467 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00007468 if (!(p = lock_user_string(arg1)))
7469 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007470 ret = get_errno(statfs(path(p), &stfs));
7471 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007472 convert_statfs:
7473 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007474 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00007475
bellard579a97f2007-11-11 14:26:47 +00007476 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
7477 goto efault;
7478 __put_user(stfs.f_type, &target_stfs->f_type);
7479 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
7480 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
7481 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
7482 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
7483 __put_user(stfs.f_files, &target_stfs->f_files);
7484 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
7485 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
7486 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
7487 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02007488 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
7489 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +00007490 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00007491 }
7492 break;
7493 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00007494 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00007495 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00007496#ifdef TARGET_NR_statfs64
7497 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00007498 if (!(p = lock_user_string(arg1)))
7499 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007500 ret = get_errno(statfs(path(p), &stfs));
7501 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00007502 convert_statfs64:
7503 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007504 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00007505
bellard579a97f2007-11-11 14:26:47 +00007506 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
7507 goto efault;
7508 __put_user(stfs.f_type, &target_stfs->f_type);
7509 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
7510 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
7511 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
7512 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
7513 __put_user(stfs.f_files, &target_stfs->f_files);
7514 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
7515 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
7516 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
7517 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02007518 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
7519 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +00007520 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00007521 }
7522 break;
7523 case TARGET_NR_fstatfs64:
7524 ret = get_errno(fstatfs(arg1, &stfs));
7525 goto convert_statfs64;
7526#endif
bellardebc05482003-09-30 21:08:41 +00007527#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00007528 case TARGET_NR_ioperm:
7529 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007530#endif
thse5febef2007-04-01 18:31:35 +00007531#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00007532 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00007533 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00007534 break;
thse5febef2007-04-01 18:31:35 +00007535#endif
bellard3532fa72006-06-24 15:06:03 +00007536#ifdef TARGET_NR_accept
7537 case TARGET_NR_accept:
Peter Maydella94b4982013-02-08 04:35:04 +00007538 ret = do_accept4(arg1, arg2, arg3, 0);
7539 break;
7540#endif
7541#ifdef TARGET_NR_accept4
7542 case TARGET_NR_accept4:
7543#ifdef CONFIG_ACCEPT4
7544 ret = do_accept4(arg1, arg2, arg3, arg4);
7545#else
7546 goto unimplemented;
7547#endif
bellard3532fa72006-06-24 15:06:03 +00007548 break;
7549#endif
7550#ifdef TARGET_NR_bind
7551 case TARGET_NR_bind:
7552 ret = do_bind(arg1, arg2, arg3);
7553 break;
7554#endif
7555#ifdef TARGET_NR_connect
7556 case TARGET_NR_connect:
7557 ret = do_connect(arg1, arg2, arg3);
7558 break;
7559#endif
7560#ifdef TARGET_NR_getpeername
7561 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00007562 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00007563 break;
7564#endif
7565#ifdef TARGET_NR_getsockname
7566 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00007567 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00007568 break;
7569#endif
7570#ifdef TARGET_NR_getsockopt
7571 case TARGET_NR_getsockopt:
7572 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
7573 break;
7574#endif
7575#ifdef TARGET_NR_listen
7576 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00007577 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00007578 break;
7579#endif
7580#ifdef TARGET_NR_recv
7581 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00007582 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00007583 break;
7584#endif
7585#ifdef TARGET_NR_recvfrom
7586 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00007587 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00007588 break;
7589#endif
7590#ifdef TARGET_NR_recvmsg
7591 case TARGET_NR_recvmsg:
7592 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
7593 break;
7594#endif
7595#ifdef TARGET_NR_send
7596 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00007597 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00007598 break;
7599#endif
7600#ifdef TARGET_NR_sendmsg
7601 case TARGET_NR_sendmsg:
7602 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
7603 break;
7604#endif
Alexander Graff19e00d2014-03-02 19:36:42 +00007605#ifdef TARGET_NR_sendmmsg
7606 case TARGET_NR_sendmmsg:
7607 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
7608 break;
7609 case TARGET_NR_recvmmsg:
7610 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
7611 break;
7612#endif
bellard3532fa72006-06-24 15:06:03 +00007613#ifdef TARGET_NR_sendto
7614 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00007615 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00007616 break;
7617#endif
7618#ifdef TARGET_NR_shutdown
7619 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00007620 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00007621 break;
7622#endif
Laurent Vivierf894efd2016-02-21 10:56:23 +01007623#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
7624 case TARGET_NR_getrandom:
7625 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
7626 if (!p) {
7627 goto efault;
7628 }
7629 ret = get_errno(getrandom(p, arg2, arg3));
7630 unlock_user(p, arg1, ret);
7631 break;
7632#endif
bellard3532fa72006-06-24 15:06:03 +00007633#ifdef TARGET_NR_socket
7634 case TARGET_NR_socket:
7635 ret = do_socket(arg1, arg2, arg3);
Laurent Viviere36800c2015-10-02 14:48:09 +02007636 fd_trans_unregister(ret);
bellard3532fa72006-06-24 15:06:03 +00007637 break;
7638#endif
7639#ifdef TARGET_NR_socketpair
7640 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00007641 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00007642 break;
7643#endif
7644#ifdef TARGET_NR_setsockopt
7645 case TARGET_NR_setsockopt:
7646 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
7647 break;
7648#endif
ths7494b0f2007-02-11 18:26:53 +00007649
bellard31e31b82003-02-18 22:55:36 +00007650 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00007651 if (!(p = lock_user_string(arg2)))
7652 goto efault;
thse5574482007-02-11 20:03:13 +00007653 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
7654 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00007655 break;
7656
bellard31e31b82003-02-18 22:55:36 +00007657 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00007658 {
bellard66fb9762003-03-23 01:06:05 +00007659 struct itimerval value, ovalue, *pvalue;
7660
pbrook53a59602006-03-25 19:31:22 +00007661 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00007662 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00007663 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
7664 || copy_from_user_timeval(&pvalue->it_value,
7665 arg2 + sizeof(struct target_timeval)))
7666 goto efault;
bellard66fb9762003-03-23 01:06:05 +00007667 } else {
7668 pvalue = NULL;
7669 }
7670 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00007671 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00007672 if (copy_to_user_timeval(arg3,
7673 &ovalue.it_interval)
7674 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
7675 &ovalue.it_value))
7676 goto efault;
bellard66fb9762003-03-23 01:06:05 +00007677 }
7678 }
7679 break;
bellard31e31b82003-02-18 22:55:36 +00007680 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00007681 {
bellard66fb9762003-03-23 01:06:05 +00007682 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00007683
bellard66fb9762003-03-23 01:06:05 +00007684 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00007685 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00007686 if (copy_to_user_timeval(arg2,
7687 &value.it_interval)
7688 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
7689 &value.it_value))
7690 goto efault;
bellard66fb9762003-03-23 01:06:05 +00007691 }
7692 }
7693 break;
Chen Gang704eff62015-08-21 05:37:33 +08007694#ifdef TARGET_NR_stat
bellard31e31b82003-02-18 22:55:36 +00007695 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00007696 if (!(p = lock_user_string(arg1)))
7697 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007698 ret = get_errno(stat(path(p), &st));
7699 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007700 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +08007701#endif
7702#ifdef TARGET_NR_lstat
bellard31e31b82003-02-18 22:55:36 +00007703 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00007704 if (!(p = lock_user_string(arg1)))
7705 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007706 ret = get_errno(lstat(path(p), &st));
7707 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007708 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +08007709#endif
bellard31e31b82003-02-18 22:55:36 +00007710 case TARGET_NR_fstat:
7711 {
7712 ret = get_errno(fstat(arg1, &st));
Chen Gang704eff62015-08-21 05:37:33 +08007713#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
bellard31e31b82003-02-18 22:55:36 +00007714 do_stat:
Chen Gang704eff62015-08-21 05:37:33 +08007715#endif
bellard31e31b82003-02-18 22:55:36 +00007716 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007717 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00007718
bellard579a97f2007-11-11 14:26:47 +00007719 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
7720 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02007721 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00007722 __put_user(st.st_dev, &target_st->st_dev);
7723 __put_user(st.st_ino, &target_st->st_ino);
7724 __put_user(st.st_mode, &target_st->st_mode);
7725 __put_user(st.st_uid, &target_st->st_uid);
7726 __put_user(st.st_gid, &target_st->st_gid);
7727 __put_user(st.st_nlink, &target_st->st_nlink);
7728 __put_user(st.st_rdev, &target_st->st_rdev);
7729 __put_user(st.st_size, &target_st->st_size);
7730 __put_user(st.st_blksize, &target_st->st_blksize);
7731 __put_user(st.st_blocks, &target_st->st_blocks);
7732 __put_user(st.st_atime, &target_st->target_st_atime);
7733 __put_user(st.st_mtime, &target_st->target_st_mtime);
7734 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00007735 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00007736 }
7737 }
7738 break;
bellardebc05482003-09-30 21:08:41 +00007739#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00007740 case TARGET_NR_olduname:
7741 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007742#endif
7743#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00007744 case TARGET_NR_iopl:
7745 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007746#endif
bellard31e31b82003-02-18 22:55:36 +00007747 case TARGET_NR_vhangup:
7748 ret = get_errno(vhangup());
7749 break;
bellardebc05482003-09-30 21:08:41 +00007750#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00007751 case TARGET_NR_idle:
7752 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007753#endif
bellard42ad6ae2005-01-03 22:48:11 +00007754#ifdef TARGET_NR_syscall
7755 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01007756 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
7757 arg6, arg7, arg8, 0);
7758 break;
bellard42ad6ae2005-01-03 22:48:11 +00007759#endif
bellard31e31b82003-02-18 22:55:36 +00007760 case TARGET_NR_wait4:
7761 {
7762 int status;
blueswir1992f48a2007-10-14 16:27:31 +00007763 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00007764 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00007765 abi_ulong target_rusage = arg4;
Petar Jovanovica39fb272014-04-08 19:24:30 +02007766 abi_long rusage_err;
bellard31e31b82003-02-18 22:55:36 +00007767 if (target_rusage)
7768 rusage_ptr = &rusage;
7769 else
7770 rusage_ptr = NULL;
7771 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
7772 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01007773 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00007774 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00007775 if (put_user_s32(status, status_ptr))
7776 goto efault;
bellard31e31b82003-02-18 22:55:36 +00007777 }
Petar Jovanovica39fb272014-04-08 19:24:30 +02007778 if (target_rusage) {
7779 rusage_err = host_to_target_rusage(target_rusage, &rusage);
7780 if (rusage_err) {
7781 ret = rusage_err;
7782 }
7783 }
bellard31e31b82003-02-18 22:55:36 +00007784 }
7785 }
7786 break;
thse5febef2007-04-01 18:31:35 +00007787#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00007788 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00007789 if (!(p = lock_user_string(arg1)))
7790 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007791 ret = get_errno(swapoff(p));
7792 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007793 break;
thse5febef2007-04-01 18:31:35 +00007794#endif
bellard31e31b82003-02-18 22:55:36 +00007795 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00007796 {
pbrook53a59602006-03-25 19:31:22 +00007797 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00007798 struct sysinfo value;
7799 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00007800 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00007801 {
bellard579a97f2007-11-11 14:26:47 +00007802 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
7803 goto efault;
bellarda5448a72004-06-19 16:59:03 +00007804 __put_user(value.uptime, &target_value->uptime);
7805 __put_user(value.loads[0], &target_value->loads[0]);
7806 __put_user(value.loads[1], &target_value->loads[1]);
7807 __put_user(value.loads[2], &target_value->loads[2]);
7808 __put_user(value.totalram, &target_value->totalram);
7809 __put_user(value.freeram, &target_value->freeram);
7810 __put_user(value.sharedram, &target_value->sharedram);
7811 __put_user(value.bufferram, &target_value->bufferram);
7812 __put_user(value.totalswap, &target_value->totalswap);
7813 __put_user(value.freeswap, &target_value->freeswap);
7814 __put_user(value.procs, &target_value->procs);
7815 __put_user(value.totalhigh, &target_value->totalhigh);
7816 __put_user(value.freehigh, &target_value->freehigh);
7817 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00007818 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00007819 }
7820 }
7821 break;
thse5febef2007-04-01 18:31:35 +00007822#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00007823 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00007824 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
7825 break;
thse5febef2007-04-01 18:31:35 +00007826#endif
aurel32e5289082009-04-18 16:16:12 +00007827#ifdef TARGET_NR_semget
7828 case TARGET_NR_semget:
7829 ret = get_errno(semget(arg1, arg2, arg3));
7830 break;
7831#endif
7832#ifdef TARGET_NR_semop
7833 case TARGET_NR_semop:
Petar Jovanovicc7128c92013-03-21 07:57:36 +00007834 ret = do_semop(arg1, arg2, arg3);
aurel32e5289082009-04-18 16:16:12 +00007835 break;
7836#endif
7837#ifdef TARGET_NR_semctl
7838 case TARGET_NR_semctl:
Stefan Weild1c002b2015-02-08 15:40:58 +01007839 ret = do_semctl(arg1, arg2, arg3, arg4);
aurel32e5289082009-04-18 16:16:12 +00007840 break;
7841#endif
aurel32eeb438c2008-10-13 21:08:55 +00007842#ifdef TARGET_NR_msgctl
7843 case TARGET_NR_msgctl:
7844 ret = do_msgctl(arg1, arg2, arg3);
7845 break;
7846#endif
7847#ifdef TARGET_NR_msgget
7848 case TARGET_NR_msgget:
7849 ret = get_errno(msgget(arg1, arg2));
7850 break;
7851#endif
7852#ifdef TARGET_NR_msgrcv
7853 case TARGET_NR_msgrcv:
7854 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
7855 break;
7856#endif
7857#ifdef TARGET_NR_msgsnd
7858 case TARGET_NR_msgsnd:
7859 ret = do_msgsnd(arg1, arg2, arg3, arg4);
7860 break;
7861#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03007862#ifdef TARGET_NR_shmget
7863 case TARGET_NR_shmget:
7864 ret = get_errno(shmget(arg1, arg2, arg3));
7865 break;
7866#endif
7867#ifdef TARGET_NR_shmctl
7868 case TARGET_NR_shmctl:
7869 ret = do_shmctl(arg1, arg2, arg3);
7870 break;
7871#endif
7872#ifdef TARGET_NR_shmat
7873 case TARGET_NR_shmat:
7874 ret = do_shmat(arg1, arg2, arg3);
7875 break;
7876#endif
7877#ifdef TARGET_NR_shmdt
7878 case TARGET_NR_shmdt:
7879 ret = do_shmdt(arg1);
7880 break;
7881#endif
bellard31e31b82003-02-18 22:55:36 +00007882 case TARGET_NR_fsync:
7883 ret = get_errno(fsync(arg1));
7884 break;
bellard31e31b82003-02-18 22:55:36 +00007885 case TARGET_NR_clone:
Peter Maydell4ce62432013-07-16 18:44:57 +01007886 /* Linux manages to have three different orderings for its
7887 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7888 * match the kernel's CONFIG_CLONE_* settings.
7889 * Microblaze is further special in that it uses a sixth
7890 * implicit argument to clone for the TLS pointer.
7891 */
7892#if defined(TARGET_MICROBLAZE)
Edgar E. Iglesiasa5b3bdc2012-04-26 14:17:41 +02007893 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
Peter Maydell4ce62432013-07-16 18:44:57 +01007894#elif defined(TARGET_CLONE_BACKWARDS)
7895 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
7896#elif defined(TARGET_CLONE_BACKWARDS2)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007897 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00007898#else
Peter Maydell4ce62432013-07-16 18:44:57 +01007899 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00007900#endif
bellard1b6b0292003-03-22 17:31:38 +00007901 break;
bellardec86b0f2003-04-11 00:15:04 +00007902#ifdef __NR_exit_group
7903 /* new thread calls */
7904 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02007905#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00007906 _mcleanup();
7907#endif
bellarde9009672005-04-26 20:42:36 +00007908 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00007909 ret = get_errno(exit_group(arg1));
7910 break;
7911#endif
bellard31e31b82003-02-18 22:55:36 +00007912 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00007913 if (!(p = lock_user_string(arg1)))
7914 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007915 ret = get_errno(setdomainname(p, arg2));
7916 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007917 break;
7918 case TARGET_NR_uname:
7919 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00007920 {
7921 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00007922
bellard579a97f2007-11-11 14:26:47 +00007923 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
7924 goto efault;
bellard29e619b2004-09-13 21:41:04 +00007925 ret = get_errno(sys_uname(buf));
7926 if (!is_error(ret)) {
7927 /* Overrite the native machine name with whatever is being
7928 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01007929 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00007930 /* Allow the user to override the reported release. */
7931 if (qemu_uname_release && *qemu_uname_release)
7932 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00007933 }
pbrook53a59602006-03-25 19:31:22 +00007934 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00007935 }
bellard31e31b82003-02-18 22:55:36 +00007936 break;
bellard6dbad632003-03-16 18:05:05 +00007937#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00007938 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00007939 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00007940 break;
j_mayer84409dd2007-04-06 08:56:50 +00007941#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00007942 case TARGET_NR_vm86old:
7943 goto unimplemented;
7944 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00007945 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00007946 break;
7947#endif
j_mayer84409dd2007-04-06 08:56:50 +00007948#endif
bellard31e31b82003-02-18 22:55:36 +00007949 case TARGET_NR_adjtimex:
7950 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007951#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00007952 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00007953#endif
bellard31e31b82003-02-18 22:55:36 +00007954 case TARGET_NR_init_module:
7955 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00007956#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00007957 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00007958#endif
bellard31e31b82003-02-18 22:55:36 +00007959 goto unimplemented;
7960 case TARGET_NR_quotactl:
7961 goto unimplemented;
7962 case TARGET_NR_getpgid:
7963 ret = get_errno(getpgid(arg1));
7964 break;
7965 case TARGET_NR_fchdir:
7966 ret = get_errno(fchdir(arg1));
7967 break;
j_mayer84409dd2007-04-06 08:56:50 +00007968#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00007969 case TARGET_NR_bdflush:
7970 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00007971#endif
thse5febef2007-04-01 18:31:35 +00007972#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00007973 case TARGET_NR_sysfs:
7974 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007975#endif
bellard31e31b82003-02-18 22:55:36 +00007976 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00007977 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00007978 break;
thse5febef2007-04-01 18:31:35 +00007979#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00007980 case TARGET_NR_afs_syscall:
7981 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007982#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007983#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007984 case TARGET_NR__llseek:
7985 {
7986 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00007987#if !defined(__NR_llseek)
7988 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7989 if (res == -1) {
7990 ret = get_errno(res);
7991 } else {
7992 ret = 0;
7993 }
7994#else
bellard31e31b82003-02-18 22:55:36 +00007995 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00007996#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00007997 if ((ret == 0) && put_user_s64(res, arg4)) {
7998 goto efault;
7999 }
bellard31e31b82003-02-18 22:55:36 +00008000 }
8001 break;
j_mayer7a3148a2007-04-05 07:13:51 +00008002#endif
Chen Gang704eff62015-08-21 05:37:33 +08008003#ifdef TARGET_NR_getdents
bellard31e31b82003-02-18 22:55:36 +00008004 case TARGET_NR_getdents:
Peter Maydell3307e232013-06-12 16:20:21 +01008005#ifdef __NR_getdents
Ulrich Hechtd83c8732009-07-24 19:10:28 +02008006#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00008007 {
pbrook53a59602006-03-25 19:31:22 +00008008 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00008009 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00008010 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00008011
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05308012 dirp = g_try_malloc(count);
8013 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00008014 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00008015 goto fail;
8016 }
ths3b46e622007-09-17 08:09:54 +00008017
bellard4add45b2003-06-05 01:52:59 +00008018 ret = get_errno(sys_getdents(arg1, dirp, count));
8019 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00008020 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00008021 struct target_dirent *tde;
8022 int len = ret;
8023 int reclen, treclen;
8024 int count1, tnamelen;
8025
8026 count1 = 0;
8027 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00008028 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8029 goto efault;
bellard4add45b2003-06-05 01:52:59 +00008030 tde = target_dirp;
8031 while (len > 0) {
8032 reclen = de->d_reclen;
Dmitry V. Levin333858b2012-08-21 02:13:12 +04008033 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
8034 assert(tnamelen >= 0);
8035 treclen = tnamelen + offsetof(struct target_dirent, d_name);
8036 assert(count1 + treclen <= count);
bellard4add45b2003-06-05 01:52:59 +00008037 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02008038 tde->d_ino = tswapal(de->d_ino);
8039 tde->d_off = tswapal(de->d_off);
Dmitry V. Levin333858b2012-08-21 02:13:12 +04008040 memcpy(tde->d_name, de->d_name, tnamelen);
aurel326556a832008-10-13 21:08:17 +00008041 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00008042 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00008043 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00008044 count1 += treclen;
8045 }
8046 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00008047 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00008048 }
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05308049 g_free(dirp);
bellard4add45b2003-06-05 01:52:59 +00008050 }
8051#else
bellard31e31b82003-02-18 22:55:36 +00008052 {
aurel326556a832008-10-13 21:08:17 +00008053 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00008054 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00008055
bellard579a97f2007-11-11 14:26:47 +00008056 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8057 goto efault;
bellard72f03902003-02-18 23:33:18 +00008058 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00008059 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00008060 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00008061 int len = ret;
8062 int reclen;
8063 de = dirp;
8064 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00008065 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00008066 if (reclen > len)
8067 break;
bellard8083a3e2003-03-24 23:12:16 +00008068 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00008069 tswapls(&de->d_ino);
8070 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00008071 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00008072 len -= reclen;
8073 }
8074 }
pbrook53a59602006-03-25 19:31:22 +00008075 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00008076 }
bellard4add45b2003-06-05 01:52:59 +00008077#endif
Peter Maydell3307e232013-06-12 16:20:21 +01008078#else
8079 /* Implement getdents in terms of getdents64 */
8080 {
8081 struct linux_dirent64 *dirp;
8082 abi_long count = arg3;
8083
8084 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8085 if (!dirp) {
8086 goto efault;
8087 }
8088 ret = get_errno(sys_getdents64(arg1, dirp, count));
8089 if (!is_error(ret)) {
8090 /* Convert the dirent64 structs to target dirent. We do this
8091 * in-place, since we can guarantee that a target_dirent is no
8092 * larger than a dirent64; however this means we have to be
8093 * careful to read everything before writing in the new format.
8094 */
8095 struct linux_dirent64 *de;
8096 struct target_dirent *tde;
8097 int len = ret;
8098 int tlen = 0;
8099
8100 de = dirp;
8101 tde = (struct target_dirent *)dirp;
8102 while (len > 0) {
8103 int namelen, treclen;
8104 int reclen = de->d_reclen;
8105 uint64_t ino = de->d_ino;
8106 int64_t off = de->d_off;
8107 uint8_t type = de->d_type;
8108
8109 namelen = strlen(de->d_name);
8110 treclen = offsetof(struct target_dirent, d_name)
8111 + namelen + 2;
8112 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
8113
8114 memmove(tde->d_name, de->d_name, namelen + 1);
8115 tde->d_ino = tswapal(ino);
8116 tde->d_off = tswapal(off);
8117 tde->d_reclen = tswap16(treclen);
8118 /* The target_dirent type is in what was formerly a padding
8119 * byte at the end of the structure:
8120 */
8121 *(((char *)tde) + treclen - 1) = type;
8122
8123 de = (struct linux_dirent64 *)((char *)de + reclen);
8124 tde = (struct target_dirent *)((char *)tde + treclen);
8125 len -= reclen;
8126 tlen += treclen;
8127 }
8128 ret = tlen;
8129 }
8130 unlock_user(dirp, arg2, ret);
8131 }
8132#endif
bellard31e31b82003-02-18 22:55:36 +00008133 break;
Chen Gang704eff62015-08-21 05:37:33 +08008134#endif /* TARGET_NR_getdents */
ths3ae43202007-09-16 21:39:48 +00008135#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00008136 case TARGET_NR_getdents64:
8137 {
aurel326556a832008-10-13 21:08:17 +00008138 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00008139 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00008140 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8141 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00008142 ret = get_errno(sys_getdents64(arg1, dirp, count));
8143 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00008144 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00008145 int len = ret;
8146 int reclen;
8147 de = dirp;
8148 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00008149 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00008150 if (reclen > len)
8151 break;
bellard8083a3e2003-03-24 23:12:16 +00008152 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00008153 tswap64s((uint64_t *)&de->d_ino);
8154 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00008155 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00008156 len -= reclen;
8157 }
8158 }
pbrook53a59602006-03-25 19:31:22 +00008159 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00008160 }
8161 break;
bellarda541f292004-04-12 20:39:29 +00008162#endif /* TARGET_NR_getdents64 */
Laurent Vivier9468a5d2013-01-10 22:30:50 +01008163#if defined(TARGET_NR__newselect)
bellard31e31b82003-02-18 22:55:36 +00008164 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00008165 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00008166 break;
thse5febef2007-04-01 18:31:35 +00008167#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05008168#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
8169# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00008170 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05008171# endif
8172# ifdef TARGET_NR_ppoll
8173 case TARGET_NR_ppoll:
8174# endif
bellard9de5e442003-03-23 16:49:39 +00008175 {
pbrook53a59602006-03-25 19:31:22 +00008176 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00008177 unsigned int nfds = arg2;
8178 int timeout = arg3;
8179 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00008180 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00008181
Laurent Vivier3e24bb32015-10-06 01:20:48 +02008182 pfd = NULL;
8183 target_pfd = NULL;
8184 if (nfds) {
8185 target_pfd = lock_user(VERIFY_WRITE, arg1,
8186 sizeof(struct target_pollfd) * nfds, 1);
8187 if (!target_pfd) {
8188 goto efault;
8189 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05008190
Laurent Vivier3e24bb32015-10-06 01:20:48 +02008191 pfd = alloca(sizeof(struct pollfd) * nfds);
8192 for (i = 0; i < nfds; i++) {
8193 pfd[i].fd = tswap32(target_pfd[i].fd);
8194 pfd[i].events = tswap16(target_pfd[i].events);
8195 }
bellard9de5e442003-03-23 16:49:39 +00008196 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05008197
8198# ifdef TARGET_NR_ppoll
8199 if (num == TARGET_NR_ppoll) {
8200 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
8201 target_sigset_t *target_set;
8202 sigset_t _set, *set = &_set;
8203
8204 if (arg3) {
8205 if (target_to_host_timespec(timeout_ts, arg3)) {
8206 unlock_user(target_pfd, arg1, 0);
8207 goto efault;
8208 }
8209 } else {
8210 timeout_ts = NULL;
8211 }
8212
8213 if (arg4) {
8214 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
8215 if (!target_set) {
8216 unlock_user(target_pfd, arg1, 0);
8217 goto efault;
8218 }
8219 target_to_host_sigset(set, target_set);
8220 } else {
8221 set = NULL;
8222 }
8223
8224 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
8225
8226 if (!is_error(ret) && arg3) {
8227 host_to_target_timespec(arg3, timeout_ts);
8228 }
8229 if (arg4) {
8230 unlock_user(target_set, arg4, 0);
8231 }
8232 } else
8233# endif
8234 ret = get_errno(poll(pfd, nfds, timeout));
8235
bellard9de5e442003-03-23 16:49:39 +00008236 if (!is_error(ret)) {
8237 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00008238 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00008239 }
8240 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00008241 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00008242 }
8243 break;
thse5febef2007-04-01 18:31:35 +00008244#endif
bellard31e31b82003-02-18 22:55:36 +00008245 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00008246 /* NOTE: the flock constant seems to be the same for every
8247 Linux platform */
8248 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00008249 break;
8250 case TARGET_NR_readv:
8251 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008252 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
8253 if (vec != NULL) {
8254 ret = get_errno(readv(arg1, vec, arg3));
8255 unlock_iovec(vec, arg2, arg3, 1);
8256 } else {
8257 ret = -host_to_target_errno(errno);
8258 }
bellard31e31b82003-02-18 22:55:36 +00008259 }
8260 break;
8261 case TARGET_NR_writev:
8262 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008263 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8264 if (vec != NULL) {
8265 ret = get_errno(writev(arg1, vec, arg3));
8266 unlock_iovec(vec, arg2, arg3, 0);
8267 } else {
8268 ret = -host_to_target_errno(errno);
8269 }
bellard31e31b82003-02-18 22:55:36 +00008270 }
8271 break;
8272 case TARGET_NR_getsid:
8273 ret = get_errno(getsid(arg1));
8274 break;
j_mayer7a3148a2007-04-05 07:13:51 +00008275#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00008276 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00008277 ret = get_errno(fdatasync(arg1));
8278 break;
j_mayer7a3148a2007-04-05 07:13:51 +00008279#endif
Chen Gang704eff62015-08-21 05:37:33 +08008280#ifdef TARGET_NR__sysctl
bellard31e31b82003-02-18 22:55:36 +00008281 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00008282 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00008283 return value. */
ths0da46a62007-10-20 20:23:07 +00008284 ret = -TARGET_ENOTDIR;
8285 break;
Chen Gang704eff62015-08-21 05:37:33 +08008286#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -05008287 case TARGET_NR_sched_getaffinity:
8288 {
8289 unsigned int mask_size;
8290 unsigned long *mask;
8291
8292 /*
8293 * sched_getaffinity needs multiples of ulong, so need to take
8294 * care of mismatches between target ulong and host ulong sizes.
8295 */
8296 if (arg2 & (sizeof(abi_ulong) - 1)) {
8297 ret = -TARGET_EINVAL;
8298 break;
8299 }
8300 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
8301
8302 mask = alloca(mask_size);
8303 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
8304
8305 if (!is_error(ret)) {
Peter Maydellbe3bd282014-05-15 14:40:23 +01008306 if (ret > arg2) {
8307 /* More data returned than the caller's buffer will fit.
8308 * This only happens if sizeof(abi_long) < sizeof(long)
8309 * and the caller passed us a buffer holding an odd number
8310 * of abi_longs. If the host kernel is actually using the
8311 * extra 4 bytes then fail EINVAL; otherwise we can just
8312 * ignore them and only copy the interesting part.
8313 */
8314 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
8315 if (numcpus > arg2 * 8) {
8316 ret = -TARGET_EINVAL;
8317 break;
8318 }
8319 ret = arg2;
8320 }
8321
Mike McCormackcd18f052011-04-18 14:43:36 +09008322 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05008323 goto efault;
8324 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05008325 }
8326 }
8327 break;
8328 case TARGET_NR_sched_setaffinity:
8329 {
8330 unsigned int mask_size;
8331 unsigned long *mask;
8332
8333 /*
8334 * sched_setaffinity needs multiples of ulong, so need to take
8335 * care of mismatches between target ulong and host ulong sizes.
8336 */
8337 if (arg2 & (sizeof(abi_ulong) - 1)) {
8338 ret = -TARGET_EINVAL;
8339 break;
8340 }
8341 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
8342
8343 mask = alloca(mask_size);
8344 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
8345 goto efault;
8346 }
8347 memcpy(mask, p, arg2);
8348 unlock_user_struct(p, arg2, 0);
8349
8350 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
8351 }
8352 break;
bellard31e31b82003-02-18 22:55:36 +00008353 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00008354 {
pbrook53a59602006-03-25 19:31:22 +00008355 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00008356 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00008357
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05008358 if (arg2 == 0) {
8359 return -TARGET_EINVAL;
8360 }
bellard579a97f2007-11-11 14:26:47 +00008361 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
8362 goto efault;
bellard5cd43932003-03-29 16:54:36 +00008363 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00008364 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00008365 ret = get_errno(sched_setparam(arg1, &schp));
8366 }
8367 break;
bellard31e31b82003-02-18 22:55:36 +00008368 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00008369 {
pbrook53a59602006-03-25 19:31:22 +00008370 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00008371 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05008372
8373 if (arg2 == 0) {
8374 return -TARGET_EINVAL;
8375 }
bellard5cd43932003-03-29 16:54:36 +00008376 ret = get_errno(sched_getparam(arg1, &schp));
8377 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00008378 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
8379 goto efault;
bellard5cd43932003-03-29 16:54:36 +00008380 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00008381 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00008382 }
8383 }
8384 break;
bellard31e31b82003-02-18 22:55:36 +00008385 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00008386 {
pbrook53a59602006-03-25 19:31:22 +00008387 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00008388 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05008389 if (arg3 == 0) {
8390 return -TARGET_EINVAL;
8391 }
bellard579a97f2007-11-11 14:26:47 +00008392 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
8393 goto efault;
bellard5cd43932003-03-29 16:54:36 +00008394 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00008395 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00008396 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
8397 }
8398 break;
bellard31e31b82003-02-18 22:55:36 +00008399 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00008400 ret = get_errno(sched_getscheduler(arg1));
8401 break;
bellard31e31b82003-02-18 22:55:36 +00008402 case TARGET_NR_sched_yield:
8403 ret = get_errno(sched_yield());
8404 break;
8405 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00008406 ret = get_errno(sched_get_priority_max(arg1));
8407 break;
bellard31e31b82003-02-18 22:55:36 +00008408 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00008409 ret = get_errno(sched_get_priority_min(arg1));
8410 break;
bellard31e31b82003-02-18 22:55:36 +00008411 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00008412 {
bellard5cd43932003-03-29 16:54:36 +00008413 struct timespec ts;
8414 ret = get_errno(sched_rr_get_interval(arg1, &ts));
8415 if (!is_error(ret)) {
Tom Mustad4290c42014-08-12 13:53:39 -05008416 ret = host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00008417 }
8418 }
8419 break;
bellard31e31b82003-02-18 22:55:36 +00008420 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00008421 {
bellard1b6b0292003-03-22 17:31:38 +00008422 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00008423 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00008424 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00008425 if (is_error(ret) && arg2) {
8426 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00008427 }
8428 }
8429 break;
thse5febef2007-04-01 18:31:35 +00008430#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00008431 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00008432 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008433#endif
8434#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00008435 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00008436 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008437#endif
bellard31e31b82003-02-18 22:55:36 +00008438 case TARGET_NR_prctl:
Peter Maydell1e6722f2012-02-03 14:48:03 +00008439 switch (arg1) {
8440 case PR_GET_PDEATHSIG:
8441 {
8442 int deathsig;
8443 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
8444 if (!is_error(ret) && arg2
8445 && put_user_ual(deathsig, arg2)) {
8446 goto efault;
thse5574482007-02-11 20:03:13 +00008447 }
Peter Maydell1e6722f2012-02-03 14:48:03 +00008448 break;
8449 }
Peter Maydelldb9526b2012-02-03 14:48:03 +00008450#ifdef PR_GET_NAME
8451 case PR_GET_NAME:
8452 {
8453 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
8454 if (!name) {
8455 goto efault;
8456 }
8457 ret = get_errno(prctl(arg1, (unsigned long)name,
8458 arg3, arg4, arg5));
8459 unlock_user(name, arg2, 16);
8460 break;
8461 }
8462 case PR_SET_NAME:
8463 {
8464 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
8465 if (!name) {
8466 goto efault;
8467 }
8468 ret = get_errno(prctl(arg1, (unsigned long)name,
8469 arg3, arg4, arg5));
8470 unlock_user(name, arg2, 0);
8471 break;
8472 }
8473#endif
Peter Maydell1e6722f2012-02-03 14:48:03 +00008474 default:
8475 /* Most prctl options have no pointer arguments */
8476 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
8477 break;
8478 }
ths39b9aae2007-02-11 18:36:44 +00008479 break;
bellardd2fd1af2007-11-14 18:08:56 +00008480#ifdef TARGET_NR_arch_prctl
8481 case TARGET_NR_arch_prctl:
8482#if defined(TARGET_I386) && !defined(TARGET_ABI32)
8483 ret = do_arch_prctl(cpu_env, arg1, arg2);
8484 break;
8485#else
8486 goto unimplemented;
8487#endif
8488#endif
aurel32f2c7ba12008-03-28 22:32:06 +00008489#ifdef TARGET_NR_pread64
8490 case TARGET_NR_pread64:
Alexander Grafae017a52012-09-29 15:32:39 +00008491 if (regpairs_aligned(cpu_env)) {
8492 arg4 = arg5;
8493 arg5 = arg6;
8494 }
aurel32f2c7ba12008-03-28 22:32:06 +00008495 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
8496 goto efault;
8497 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
8498 unlock_user(p, arg2, ret);
8499 break;
8500 case TARGET_NR_pwrite64:
Alexander Grafae017a52012-09-29 15:32:39 +00008501 if (regpairs_aligned(cpu_env)) {
8502 arg4 = arg5;
8503 arg5 = arg6;
8504 }
aurel32f2c7ba12008-03-28 22:32:06 +00008505 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
8506 goto efault;
8507 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
8508 unlock_user(p, arg2, 0);
8509 break;
8510#endif
bellard31e31b82003-02-18 22:55:36 +00008511 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00008512 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
8513 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008514 ret = get_errno(sys_getcwd1(p, arg2));
8515 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00008516 break;
8517 case TARGET_NR_capget:
8518 case TARGET_NR_capset:
Peter Maydelle0eb2102014-03-17 12:15:35 +00008519 {
8520 struct target_user_cap_header *target_header;
8521 struct target_user_cap_data *target_data = NULL;
8522 struct __user_cap_header_struct header;
8523 struct __user_cap_data_struct data[2];
8524 struct __user_cap_data_struct *dataptr = NULL;
8525 int i, target_datalen;
8526 int data_items = 1;
8527
8528 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
8529 goto efault;
8530 }
8531 header.version = tswap32(target_header->version);
8532 header.pid = tswap32(target_header->pid);
8533
Peter Maydellec864872014-03-19 16:07:30 +00008534 if (header.version != _LINUX_CAPABILITY_VERSION) {
Peter Maydelle0eb2102014-03-17 12:15:35 +00008535 /* Version 2 and up takes pointer to two user_data structs */
8536 data_items = 2;
8537 }
8538
8539 target_datalen = sizeof(*target_data) * data_items;
8540
8541 if (arg2) {
8542 if (num == TARGET_NR_capget) {
8543 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
8544 } else {
8545 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
8546 }
8547 if (!target_data) {
8548 unlock_user_struct(target_header, arg1, 0);
8549 goto efault;
8550 }
8551
8552 if (num == TARGET_NR_capset) {
8553 for (i = 0; i < data_items; i++) {
8554 data[i].effective = tswap32(target_data[i].effective);
8555 data[i].permitted = tswap32(target_data[i].permitted);
8556 data[i].inheritable = tswap32(target_data[i].inheritable);
8557 }
8558 }
8559
8560 dataptr = data;
8561 }
8562
8563 if (num == TARGET_NR_capget) {
8564 ret = get_errno(capget(&header, dataptr));
8565 } else {
8566 ret = get_errno(capset(&header, dataptr));
8567 }
8568
8569 /* The kernel always updates version for both capget and capset */
8570 target_header->version = tswap32(header.version);
8571 unlock_user_struct(target_header, arg1, 1);
8572
8573 if (arg2) {
8574 if (num == TARGET_NR_capget) {
8575 for (i = 0; i < data_items; i++) {
8576 target_data[i].effective = tswap32(data[i].effective);
8577 target_data[i].permitted = tswap32(data[i].permitted);
8578 target_data[i].inheritable = tswap32(data[i].inheritable);
8579 }
8580 unlock_user(target_data, arg2, target_datalen);
8581 } else {
8582 unlock_user(target_data, arg2, 0);
8583 }
8584 }
8585 break;
8586 }
bellard31e31b82003-02-18 22:55:36 +00008587 case TARGET_NR_sigaltstack:
Andreas Färber9349b4f2012-03-14 01:38:32 +01008588 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00008589 break;
Peter Maydella8fd1ab2013-02-08 07:31:55 +00008590
8591#ifdef CONFIG_SENDFILE
bellard31e31b82003-02-18 22:55:36 +00008592 case TARGET_NR_sendfile:
Peter Maydella8fd1ab2013-02-08 07:31:55 +00008593 {
8594 off_t *offp = NULL;
8595 off_t off;
8596 if (arg3) {
8597 ret = get_user_sal(off, arg3);
8598 if (is_error(ret)) {
8599 break;
8600 }
8601 offp = &off;
8602 }
8603 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
8604 if (!is_error(ret) && arg3) {
8605 abi_long ret2 = put_user_sal(off, arg3);
8606 if (is_error(ret2)) {
8607 ret = ret2;
8608 }
8609 }
8610 break;
8611 }
8612#ifdef TARGET_NR_sendfile64
8613 case TARGET_NR_sendfile64:
8614 {
8615 off_t *offp = NULL;
8616 off_t off;
8617 if (arg3) {
8618 ret = get_user_s64(off, arg3);
8619 if (is_error(ret)) {
8620 break;
8621 }
8622 offp = &off;
8623 }
8624 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
8625 if (!is_error(ret) && arg3) {
8626 abi_long ret2 = put_user_s64(off, arg3);
8627 if (is_error(ret2)) {
8628 ret = ret2;
8629 }
8630 }
8631 break;
8632 }
8633#endif
8634#else
8635 case TARGET_NR_sendfile:
Peter Maydell7edd2cf2013-04-21 13:30:03 +01008636#ifdef TARGET_NR_sendfile64
Peter Maydella8fd1ab2013-02-08 07:31:55 +00008637 case TARGET_NR_sendfile64:
8638#endif
bellard5cd43932003-03-29 16:54:36 +00008639 goto unimplemented;
Peter Maydella8fd1ab2013-02-08 07:31:55 +00008640#endif
8641
bellardebc05482003-09-30 21:08:41 +00008642#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00008643 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00008644 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008645#endif
8646#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00008647 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00008648 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008649#endif
bellard048f6b42005-11-26 18:47:20 +00008650#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00008651 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00008652 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
8653 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00008654 break;
bellard048f6b42005-11-26 18:47:20 +00008655#endif
bellardebc05482003-09-30 21:08:41 +00008656#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00008657 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00008658 {
8659 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03008660 int resource = target_to_host_resource(arg1);
8661 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00008662 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00008663 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00008664 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
8665 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09008666 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
8667 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00008668 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00008669 }
8670 break;
8671 }
bellardebc05482003-09-30 21:08:41 +00008672#endif
bellarda315a142005-01-30 22:59:18 +00008673#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00008674 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00008675 if (!(p = lock_user_string(arg1)))
8676 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008677 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
8678 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00008679 break;
bellarda315a142005-01-30 22:59:18 +00008680#endif
8681#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00008682 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00008683 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00008684 break;
bellarda315a142005-01-30 22:59:18 +00008685#endif
8686#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00008687 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00008688 if (!(p = lock_user_string(arg1)))
8689 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008690 ret = get_errno(stat(path(p), &st));
8691 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00008692 if (!is_error(ret))
8693 ret = host_to_target_stat64(cpu_env, arg2, &st);
8694 break;
bellarda315a142005-01-30 22:59:18 +00008695#endif
8696#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00008697 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00008698 if (!(p = lock_user_string(arg1)))
8699 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008700 ret = get_errno(lstat(path(p), &st));
8701 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00008702 if (!is_error(ret))
8703 ret = host_to_target_stat64(cpu_env, arg2, &st);
8704 break;
bellarda315a142005-01-30 22:59:18 +00008705#endif
8706#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00008707 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00008708 ret = get_errno(fstat(arg1, &st));
8709 if (!is_error(ret))
8710 ret = host_to_target_stat64(cpu_env, arg2, &st);
8711 break;
bellardec86b0f2003-04-11 00:15:04 +00008712#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01008713#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
aurel329d33b762009-04-08 23:07:05 +00008714#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00008715 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00008716#endif
8717#ifdef TARGET_NR_newfstatat
8718 case TARGET_NR_newfstatat:
8719#endif
balrog6a24a772008-09-20 02:23:36 +00008720 if (!(p = lock_user_string(arg2)))
8721 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008722 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
balrog6a24a772008-09-20 02:23:36 +00008723 if (!is_error(ret))
8724 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00008725 break;
bellarda315a142005-01-30 22:59:18 +00008726#endif
Chen Gang704eff62015-08-21 05:37:33 +08008727#ifdef TARGET_NR_lchown
bellard67867302003-11-23 17:05:30 +00008728 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00008729 if (!(p = lock_user_string(arg1)))
8730 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008731 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
8732 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00008733 break;
Chen Gang704eff62015-08-21 05:37:33 +08008734#endif
Riku Voipio0c866a72011-04-18 15:23:06 +03008735#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00008736 case TARGET_NR_getuid:
8737 ret = get_errno(high2lowuid(getuid()));
8738 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03008739#endif
8740#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00008741 case TARGET_NR_getgid:
8742 ret = get_errno(high2lowgid(getgid()));
8743 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03008744#endif
8745#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00008746 case TARGET_NR_geteuid:
8747 ret = get_errno(high2lowuid(geteuid()));
8748 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03008749#endif
8750#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00008751 case TARGET_NR_getegid:
8752 ret = get_errno(high2lowgid(getegid()));
8753 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03008754#endif
bellard67867302003-11-23 17:05:30 +00008755 case TARGET_NR_setreuid:
8756 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
8757 break;
8758 case TARGET_NR_setregid:
8759 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
8760 break;
8761 case TARGET_NR_getgroups:
8762 {
8763 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03008764 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00008765 gid_t *grouplist;
8766 int i;
8767
8768 grouplist = alloca(gidsetsize * sizeof(gid_t));
8769 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00008770 if (gidsetsize == 0)
8771 break;
bellard67867302003-11-23 17:05:30 +00008772 if (!is_error(ret)) {
Andreas Schwab03903ff2013-04-09 05:41:33 +00008773 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
bellard579a97f2007-11-11 14:26:47 +00008774 if (!target_grouplist)
8775 goto efault;
balroga2155fc2008-09-20 02:12:08 +00008776 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03008777 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
Andreas Schwab03903ff2013-04-09 05:41:33 +00008778 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
bellard67867302003-11-23 17:05:30 +00008779 }
8780 }
8781 break;
8782 case TARGET_NR_setgroups:
8783 {
8784 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03008785 target_id *target_grouplist;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05008786 gid_t *grouplist = NULL;
bellard67867302003-11-23 17:05:30 +00008787 int i;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05008788 if (gidsetsize) {
8789 grouplist = alloca(gidsetsize * sizeof(gid_t));
Andreas Schwab03903ff2013-04-09 05:41:33 +00008790 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05008791 if (!target_grouplist) {
8792 ret = -TARGET_EFAULT;
8793 goto fail;
8794 }
8795 for (i = 0; i < gidsetsize; i++) {
8796 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
8797 }
8798 unlock_user(target_grouplist, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00008799 }
bellard67867302003-11-23 17:05:30 +00008800 ret = get_errno(setgroups(gidsetsize, grouplist));
8801 }
8802 break;
8803 case TARGET_NR_fchown:
8804 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
8805 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008806#if defined(TARGET_NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +00008807 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00008808 if (!(p = lock_user_string(arg2)))
8809 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008810 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
8811 low2highgid(arg4), arg5));
bellard579a97f2007-11-11 14:26:47 +00008812 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00008813 break;
8814#endif
bellard67867302003-11-23 17:05:30 +00008815#ifdef TARGET_NR_setresuid
8816 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00008817 ret = get_errno(setresuid(low2highuid(arg1),
8818 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00008819 low2highuid(arg3)));
8820 break;
8821#endif
8822#ifdef TARGET_NR_getresuid
8823 case TARGET_NR_getresuid:
8824 {
pbrook53a59602006-03-25 19:31:22 +00008825 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00008826 ret = get_errno(getresuid(&ruid, &euid, &suid));
8827 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +00008828 if (put_user_id(high2lowuid(ruid), arg1)
8829 || put_user_id(high2lowuid(euid), arg2)
8830 || put_user_id(high2lowuid(suid), arg3))
bellard2f619692007-11-16 10:46:05 +00008831 goto efault;
bellard67867302003-11-23 17:05:30 +00008832 }
8833 }
8834 break;
8835#endif
8836#ifdef TARGET_NR_getresgid
8837 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00008838 ret = get_errno(setresgid(low2highgid(arg1),
8839 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00008840 low2highgid(arg3)));
8841 break;
8842#endif
8843#ifdef TARGET_NR_getresgid
8844 case TARGET_NR_getresgid:
8845 {
pbrook53a59602006-03-25 19:31:22 +00008846 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00008847 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8848 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +00008849 if (put_user_id(high2lowgid(rgid), arg1)
8850 || put_user_id(high2lowgid(egid), arg2)
8851 || put_user_id(high2lowgid(sgid), arg3))
bellard2f619692007-11-16 10:46:05 +00008852 goto efault;
bellard67867302003-11-23 17:05:30 +00008853 }
8854 }
8855 break;
8856#endif
Chen Gang704eff62015-08-21 05:37:33 +08008857#ifdef TARGET_NR_chown
bellard67867302003-11-23 17:05:30 +00008858 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00008859 if (!(p = lock_user_string(arg1)))
8860 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008861 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
8862 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00008863 break;
Chen Gang704eff62015-08-21 05:37:33 +08008864#endif
bellard67867302003-11-23 17:05:30 +00008865 case TARGET_NR_setuid:
8866 ret = get_errno(setuid(low2highuid(arg1)));
8867 break;
8868 case TARGET_NR_setgid:
8869 ret = get_errno(setgid(low2highgid(arg1)));
8870 break;
8871 case TARGET_NR_setfsuid:
8872 ret = get_errno(setfsuid(arg1));
8873 break;
8874 case TARGET_NR_setfsgid:
8875 ret = get_errno(setfsgid(arg1));
8876 break;
bellard67867302003-11-23 17:05:30 +00008877
bellarda315a142005-01-30 22:59:18 +00008878#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00008879 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00008880 if (!(p = lock_user_string(arg1)))
8881 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008882 ret = get_errno(lchown(p, arg2, arg3));
8883 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00008884 break;
bellarda315a142005-01-30 22:59:18 +00008885#endif
8886#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00008887 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00008888 ret = get_errno(getuid());
8889 break;
bellarda315a142005-01-30 22:59:18 +00008890#endif
aurel3264b4d282008-11-14 17:20:15 +00008891
8892#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
8893 /* Alpha specific */
8894 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08008895 {
8896 uid_t euid;
8897 euid=geteuid();
8898 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
8899 }
aurel3264b4d282008-11-14 17:20:15 +00008900 ret = get_errno(getuid());
8901 break;
8902#endif
8903#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
8904 /* Alpha specific */
8905 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08008906 {
8907 uid_t egid;
8908 egid=getegid();
8909 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
8910 }
aurel3264b4d282008-11-14 17:20:15 +00008911 ret = get_errno(getgid());
8912 break;
8913#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08008914#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
8915 /* Alpha specific */
8916 case TARGET_NR_osf_getsysinfo:
8917 ret = -TARGET_EOPNOTSUPP;
8918 switch (arg1) {
8919 case TARGET_GSI_IEEE_FP_CONTROL:
8920 {
8921 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
8922
8923 /* Copied from linux ieee_fpcr_to_swcr. */
8924 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
8925 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
8926 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
8927 | SWCR_TRAP_ENABLE_DZE
8928 | SWCR_TRAP_ENABLE_OVF);
8929 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
8930 | SWCR_TRAP_ENABLE_INE);
8931 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
8932 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
8933
8934 if (put_user_u64 (swcr, arg2))
8935 goto efault;
8936 ret = 0;
8937 }
8938 break;
8939
8940 /* case GSI_IEEE_STATE_AT_SIGNAL:
8941 -- Not implemented in linux kernel.
8942 case GSI_UACPROC:
8943 -- Retrieves current unaligned access state; not much used.
8944 case GSI_PROC_TYPE:
8945 -- Retrieves implver information; surely not used.
8946 case GSI_GET_HWRPB:
8947 -- Grabs a copy of the HWRPB; surely not used.
8948 */
8949 }
8950 break;
8951#endif
8952#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8953 /* Alpha specific */
8954 case TARGET_NR_osf_setsysinfo:
8955 ret = -TARGET_EOPNOTSUPP;
8956 switch (arg1) {
8957 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -08008958 {
8959 uint64_t swcr, fpcr, orig_fpcr;
8960
Richard Henderson6e06d512012-06-01 09:08:21 -07008961 if (get_user_u64 (swcr, arg2)) {
Richard Hendersonba0e2762009-12-09 15:56:29 -08008962 goto efault;
Richard Henderson6e06d512012-06-01 09:08:21 -07008963 }
8964 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008965 fpcr = orig_fpcr & FPCR_DYN_MASK;
8966
8967 /* Copied from linux ieee_swcr_to_fpcr. */
8968 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
8969 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
8970 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
8971 | SWCR_TRAP_ENABLE_DZE
8972 | SWCR_TRAP_ENABLE_OVF)) << 48;
8973 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
8974 | SWCR_TRAP_ENABLE_INE)) << 57;
8975 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
8976 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
8977
Richard Henderson6e06d512012-06-01 09:08:21 -07008978 cpu_alpha_store_fpcr(cpu_env, fpcr);
8979 ret = 0;
8980 }
8981 break;
8982
8983 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
8984 {
8985 uint64_t exc, fpcr, orig_fpcr;
8986 int si_code;
8987
8988 if (get_user_u64(exc, arg2)) {
8989 goto efault;
8990 }
8991
8992 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8993
8994 /* We only add to the exception status here. */
8995 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
8996
8997 cpu_alpha_store_fpcr(cpu_env, fpcr);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008998 ret = 0;
8999
Richard Henderson6e06d512012-06-01 09:08:21 -07009000 /* Old exceptions are not signaled. */
9001 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -08009002
Richard Henderson6e06d512012-06-01 09:08:21 -07009003 /* If any exceptions set by this call,
9004 and are unmasked, send a signal. */
9005 si_code = 0;
9006 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
9007 si_code = TARGET_FPE_FLTRES;
9008 }
9009 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
9010 si_code = TARGET_FPE_FLTUND;
9011 }
9012 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
9013 si_code = TARGET_FPE_FLTOVF;
9014 }
9015 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
9016 si_code = TARGET_FPE_FLTDIV;
9017 }
9018 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
9019 si_code = TARGET_FPE_FLTINV;
9020 }
9021 if (si_code != 0) {
9022 target_siginfo_t info;
9023 info.si_signo = SIGFPE;
9024 info.si_errno = 0;
9025 info.si_code = si_code;
9026 info._sifields._sigfault._addr
9027 = ((CPUArchState *)cpu_env)->pc;
9028 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -08009029 }
9030 }
9031 break;
9032
9033 /* case SSI_NVPAIRS:
9034 -- Used with SSIN_UACPROC to enable unaligned accesses.
9035 case SSI_IEEE_STATE_AT_SIGNAL:
9036 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
9037 -- Not implemented in linux kernel
9038 */
9039 }
9040 break;
9041#endif
9042#ifdef TARGET_NR_osf_sigprocmask
9043 /* Alpha specific. */
9044 case TARGET_NR_osf_sigprocmask:
9045 {
9046 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01009047 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08009048 sigset_t set, oldset;
9049
9050 switch(arg1) {
9051 case TARGET_SIG_BLOCK:
9052 how = SIG_BLOCK;
9053 break;
9054 case TARGET_SIG_UNBLOCK:
9055 how = SIG_UNBLOCK;
9056 break;
9057 case TARGET_SIG_SETMASK:
9058 how = SIG_SETMASK;
9059 break;
9060 default:
9061 ret = -TARGET_EINVAL;
9062 goto fail;
9063 }
9064 mask = arg2;
9065 target_to_host_old_sigset(&set, &mask);
Alex Barcelo1c275922014-03-14 14:36:55 +00009066 do_sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08009067 host_to_target_old_sigset(&mask, &oldset);
9068 ret = mask;
9069 }
9070 break;
9071#endif
aurel3264b4d282008-11-14 17:20:15 +00009072
bellarda315a142005-01-30 22:59:18 +00009073#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00009074 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00009075 ret = get_errno(getgid());
9076 break;
bellarda315a142005-01-30 22:59:18 +00009077#endif
9078#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00009079 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00009080 ret = get_errno(geteuid());
9081 break;
bellarda315a142005-01-30 22:59:18 +00009082#endif
9083#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00009084 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00009085 ret = get_errno(getegid());
9086 break;
bellarda315a142005-01-30 22:59:18 +00009087#endif
9088#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00009089 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00009090 ret = get_errno(setreuid(arg1, arg2));
9091 break;
bellarda315a142005-01-30 22:59:18 +00009092#endif
9093#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00009094 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00009095 ret = get_errno(setregid(arg1, arg2));
9096 break;
bellarda315a142005-01-30 22:59:18 +00009097#endif
9098#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00009099 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00009100 {
9101 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00009102 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00009103 gid_t *grouplist;
9104 int i;
9105
9106 grouplist = alloca(gidsetsize * sizeof(gid_t));
9107 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00009108 if (gidsetsize == 0)
9109 break;
bellard99c475a2005-01-31 20:45:13 +00009110 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00009111 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
9112 if (!target_grouplist) {
9113 ret = -TARGET_EFAULT;
9114 goto fail;
9115 }
balroga2155fc2008-09-20 02:12:08 +00009116 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00009117 target_grouplist[i] = tswap32(grouplist[i]);
9118 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00009119 }
9120 }
9121 break;
bellarda315a142005-01-30 22:59:18 +00009122#endif
9123#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00009124 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00009125 {
9126 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00009127 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00009128 gid_t *grouplist;
9129 int i;
ths3b46e622007-09-17 08:09:54 +00009130
bellard99c475a2005-01-31 20:45:13 +00009131 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00009132 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
9133 if (!target_grouplist) {
9134 ret = -TARGET_EFAULT;
9135 goto fail;
9136 }
bellard99c475a2005-01-31 20:45:13 +00009137 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00009138 grouplist[i] = tswap32(target_grouplist[i]);
9139 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00009140 ret = get_errno(setgroups(gidsetsize, grouplist));
9141 }
9142 break;
bellarda315a142005-01-30 22:59:18 +00009143#endif
9144#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00009145 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00009146 ret = get_errno(fchown(arg1, arg2, arg3));
9147 break;
bellarda315a142005-01-30 22:59:18 +00009148#endif
9149#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00009150 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00009151 ret = get_errno(setresuid(arg1, arg2, arg3));
9152 break;
bellarda315a142005-01-30 22:59:18 +00009153#endif
9154#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00009155 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00009156 {
pbrook53a59602006-03-25 19:31:22 +00009157 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00009158 ret = get_errno(getresuid(&ruid, &euid, &suid));
9159 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00009160 if (put_user_u32(ruid, arg1)
9161 || put_user_u32(euid, arg2)
9162 || put_user_u32(suid, arg3))
9163 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00009164 }
9165 }
9166 break;
bellarda315a142005-01-30 22:59:18 +00009167#endif
9168#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00009169 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00009170 ret = get_errno(setresgid(arg1, arg2, arg3));
9171 break;
bellarda315a142005-01-30 22:59:18 +00009172#endif
9173#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00009174 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00009175 {
pbrook53a59602006-03-25 19:31:22 +00009176 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00009177 ret = get_errno(getresgid(&rgid, &egid, &sgid));
9178 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00009179 if (put_user_u32(rgid, arg1)
9180 || put_user_u32(egid, arg2)
9181 || put_user_u32(sgid, arg3))
9182 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00009183 }
9184 }
9185 break;
bellarda315a142005-01-30 22:59:18 +00009186#endif
9187#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00009188 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00009189 if (!(p = lock_user_string(arg1)))
9190 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009191 ret = get_errno(chown(p, arg2, arg3));
9192 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00009193 break;
bellarda315a142005-01-30 22:59:18 +00009194#endif
9195#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00009196 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00009197 ret = get_errno(setuid(arg1));
9198 break;
bellarda315a142005-01-30 22:59:18 +00009199#endif
9200#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00009201 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00009202 ret = get_errno(setgid(arg1));
9203 break;
bellarda315a142005-01-30 22:59:18 +00009204#endif
9205#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00009206 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00009207 ret = get_errno(setfsuid(arg1));
9208 break;
bellarda315a142005-01-30 22:59:18 +00009209#endif
9210#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00009211 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00009212 ret = get_errno(setfsgid(arg1));
9213 break;
bellarda315a142005-01-30 22:59:18 +00009214#endif
bellard67867302003-11-23 17:05:30 +00009215
bellard31e31b82003-02-18 22:55:36 +00009216 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00009217 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00009218#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00009219 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00009220 {
9221 void *a;
9222 ret = -TARGET_EFAULT;
9223 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
9224 goto efault;
9225 if (!(p = lock_user_string(arg3)))
9226 goto mincore_fail;
9227 ret = get_errno(mincore(a, arg2, p));
9228 unlock_user(p, arg3, ret);
9229 mincore_fail:
9230 unlock_user(a, arg1, 0);
9231 }
9232 break;
bellardffa65c32004-01-04 23:57:22 +00009233#endif
aurel32408321b2008-10-01 21:46:32 +00009234#ifdef TARGET_NR_arm_fadvise64_64
9235 case TARGET_NR_arm_fadvise64_64:
9236 {
9237 /*
9238 * arm_fadvise64_64 looks like fadvise64_64 but
9239 * with different argument order
9240 */
9241 abi_long temp;
9242 temp = arg3;
9243 arg3 = arg4;
9244 arg4 = temp;
9245 }
9246#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02009247#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00009248#ifdef TARGET_NR_fadvise64_64
9249 case TARGET_NR_fadvise64_64:
9250#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02009251#ifdef TARGET_NR_fadvise64
9252 case TARGET_NR_fadvise64:
9253#endif
9254#ifdef TARGET_S390X
9255 switch (arg4) {
9256 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
9257 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
9258 case 6: arg4 = POSIX_FADV_DONTNEED; break;
9259 case 7: arg4 = POSIX_FADV_NOREUSE; break;
9260 default: break;
9261 }
9262#endif
9263 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00009264 break;
9265#endif
bellardffa65c32004-01-04 23:57:22 +00009266#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00009267 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00009268 /* A straight passthrough may not be safe because qemu sometimes
Lei Lid2d6b852013-05-20 17:20:50 +08009269 turns private file-backed mappings into anonymous mappings.
pbrook24836682006-04-16 14:14:53 +00009270 This will break MADV_DONTNEED.
9271 This is a hint, so ignoring and returning success is ok. */
9272 ret = get_errno(0);
9273 break;
bellardffa65c32004-01-04 23:57:22 +00009274#endif
blueswir1992f48a2007-10-14 16:27:31 +00009275#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00009276 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00009277 {
thsb1e341e2007-03-20 21:50:52 +00009278 int cmd;
bellard77e46722003-04-29 20:39:06 +00009279 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00009280 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00009281#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00009282 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00009283#endif
bellard77e46722003-04-29 20:39:06 +00009284
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02009285 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00009286 if (cmd == -TARGET_EINVAL) {
9287 ret = cmd;
9288 break;
9289 }
thsb1e341e2007-03-20 21:50:52 +00009290
bellard60cd49d2003-03-16 22:53:56 +00009291 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00009292 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00009293#ifdef TARGET_ARM
9294 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00009295 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
9296 goto efault;
ths58134272007-03-31 18:59:32 +00009297 fl.l_type = tswap16(target_efl->l_type);
9298 fl.l_whence = tswap16(target_efl->l_whence);
9299 fl.l_start = tswap64(target_efl->l_start);
9300 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009301 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00009302 unlock_user_struct(target_efl, arg3, 0);
9303 } else
9304#endif
9305 {
bellard9ee1fa22007-11-11 15:11:19 +00009306 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
9307 goto efault;
ths58134272007-03-31 18:59:32 +00009308 fl.l_type = tswap16(target_fl->l_type);
9309 fl.l_whence = tswap16(target_fl->l_whence);
9310 fl.l_start = tswap64(target_fl->l_start);
9311 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009312 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00009313 unlock_user_struct(target_fl, arg3, 0);
9314 }
thsb1e341e2007-03-20 21:50:52 +00009315 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00009316 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00009317#ifdef TARGET_ARM
9318 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00009319 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
9320 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00009321 target_efl->l_type = tswap16(fl.l_type);
9322 target_efl->l_whence = tswap16(fl.l_whence);
9323 target_efl->l_start = tswap64(fl.l_start);
9324 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009325 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00009326 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00009327 } else
9328#endif
9329 {
bellard9ee1fa22007-11-11 15:11:19 +00009330 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
9331 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00009332 target_fl->l_type = tswap16(fl.l_type);
9333 target_fl->l_whence = tswap16(fl.l_whence);
9334 target_fl->l_start = tswap64(fl.l_start);
9335 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009336 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00009337 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00009338 }
bellard77e46722003-04-29 20:39:06 +00009339 }
9340 break;
9341
thsb1e341e2007-03-20 21:50:52 +00009342 case TARGET_F_SETLK64:
9343 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00009344#ifdef TARGET_ARM
9345 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00009346 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
9347 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00009348 fl.l_type = tswap16(target_efl->l_type);
9349 fl.l_whence = tswap16(target_efl->l_whence);
9350 fl.l_start = tswap64(target_efl->l_start);
9351 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009352 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00009353 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00009354 } else
9355#endif
9356 {
bellard9ee1fa22007-11-11 15:11:19 +00009357 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
9358 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00009359 fl.l_type = tswap16(target_fl->l_type);
9360 fl.l_whence = tswap16(target_fl->l_whence);
9361 fl.l_start = tswap64(target_fl->l_start);
9362 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009363 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00009364 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00009365 }
thsb1e341e2007-03-20 21:50:52 +00009366 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00009367 break;
bellard60cd49d2003-03-16 22:53:56 +00009368 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02009369 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00009370 break;
9371 }
bellard77e46722003-04-29 20:39:06 +00009372 break;
9373 }
bellard60cd49d2003-03-16 22:53:56 +00009374#endif
ths7d600c82006-12-08 01:32:58 +00009375#ifdef TARGET_NR_cacheflush
9376 case TARGET_NR_cacheflush:
9377 /* self-modifying code is handled automatically, so nothing needed */
9378 ret = 0;
9379 break;
9380#endif
bellardebc05482003-09-30 21:08:41 +00009381#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00009382 case TARGET_NR_security:
9383 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00009384#endif
bellardc573ff62004-01-04 15:51:36 +00009385#ifdef TARGET_NR_getpagesize
9386 case TARGET_NR_getpagesize:
9387 ret = TARGET_PAGE_SIZE;
9388 break;
9389#endif
bellard31e31b82003-02-18 22:55:36 +00009390 case TARGET_NR_gettid:
9391 ret = get_errno(gettid());
9392 break;
thse5febef2007-04-01 18:31:35 +00009393#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00009394 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00009395#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +03009396 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +00009397 arg2 = arg3;
9398 arg3 = arg4;
9399 arg4 = arg5;
9400 }
aurel322054ac92008-10-13 21:08:07 +00009401 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
9402#else
9403 ret = get_errno(readahead(arg1, arg2, arg3));
9404#endif
9405 break;
thse5febef2007-04-01 18:31:35 +00009406#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009407#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +00009408#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00009409 case TARGET_NR_listxattr:
9410 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +00009411 {
9412 void *p, *b = 0;
9413 if (arg2) {
9414 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
9415 if (!b) {
9416 ret = -TARGET_EFAULT;
9417 break;
9418 }
9419 }
9420 p = lock_user_string(arg1);
9421 if (p) {
9422 if (num == TARGET_NR_listxattr) {
9423 ret = get_errno(listxattr(p, b, arg3));
9424 } else {
9425 ret = get_errno(llistxattr(p, b, arg3));
9426 }
9427 } else {
9428 ret = -TARGET_EFAULT;
9429 }
9430 unlock_user(p, arg1, 0);
9431 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +03009432 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +00009433 }
9434 case TARGET_NR_flistxattr:
9435 {
9436 void *b = 0;
9437 if (arg2) {
9438 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
9439 if (!b) {
9440 ret = -TARGET_EFAULT;
9441 break;
9442 }
9443 }
9444 ret = get_errno(flistxattr(arg1, b, arg3));
9445 unlock_user(b, arg2, arg3);
9446 break;
9447 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009448 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00009449 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009450 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00009451 void *p, *n, *v = 0;
9452 if (arg3) {
9453 v = lock_user(VERIFY_READ, arg3, arg4, 1);
9454 if (!v) {
9455 ret = -TARGET_EFAULT;
9456 break;
9457 }
9458 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009459 p = lock_user_string(arg1);
9460 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00009461 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00009462 if (num == TARGET_NR_setxattr) {
9463 ret = get_errno(setxattr(p, n, v, arg4, arg5));
9464 } else {
9465 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
9466 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009467 } else {
9468 ret = -TARGET_EFAULT;
9469 }
9470 unlock_user(p, arg1, 0);
9471 unlock_user(n, arg2, 0);
9472 unlock_user(v, arg3, 0);
9473 }
9474 break;
Peter Maydell30297b52011-12-14 15:37:18 +00009475 case TARGET_NR_fsetxattr:
9476 {
9477 void *n, *v = 0;
9478 if (arg3) {
9479 v = lock_user(VERIFY_READ, arg3, arg4, 1);
9480 if (!v) {
9481 ret = -TARGET_EFAULT;
9482 break;
9483 }
9484 }
9485 n = lock_user_string(arg2);
9486 if (n) {
9487 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
9488 } else {
9489 ret = -TARGET_EFAULT;
9490 }
9491 unlock_user(n, arg2, 0);
9492 unlock_user(v, arg3, 0);
9493 }
9494 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009495 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00009496 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009497 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00009498 void *p, *n, *v = 0;
9499 if (arg3) {
9500 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
9501 if (!v) {
9502 ret = -TARGET_EFAULT;
9503 break;
9504 }
9505 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009506 p = lock_user_string(arg1);
9507 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00009508 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00009509 if (num == TARGET_NR_getxattr) {
9510 ret = get_errno(getxattr(p, n, v, arg4));
9511 } else {
9512 ret = get_errno(lgetxattr(p, n, v, arg4));
9513 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009514 } else {
9515 ret = -TARGET_EFAULT;
9516 }
9517 unlock_user(p, arg1, 0);
9518 unlock_user(n, arg2, 0);
9519 unlock_user(v, arg3, arg4);
9520 }
9521 break;
Peter Maydell30297b52011-12-14 15:37:18 +00009522 case TARGET_NR_fgetxattr:
9523 {
9524 void *n, *v = 0;
9525 if (arg3) {
9526 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
9527 if (!v) {
9528 ret = -TARGET_EFAULT;
9529 break;
9530 }
9531 }
9532 n = lock_user_string(arg2);
9533 if (n) {
9534 ret = get_errno(fgetxattr(arg1, n, v, arg4));
9535 } else {
9536 ret = -TARGET_EFAULT;
9537 }
9538 unlock_user(n, arg2, 0);
9539 unlock_user(v, arg3, arg4);
9540 }
9541 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009542 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +00009543 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009544 {
9545 void *p, *n;
9546 p = lock_user_string(arg1);
9547 n = lock_user_string(arg2);
9548 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00009549 if (num == TARGET_NR_removexattr) {
9550 ret = get_errno(removexattr(p, n));
9551 } else {
9552 ret = get_errno(lremovexattr(p, n));
9553 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009554 } else {
9555 ret = -TARGET_EFAULT;
9556 }
9557 unlock_user(p, arg1, 0);
9558 unlock_user(n, arg2, 0);
9559 }
9560 break;
Peter Maydell30297b52011-12-14 15:37:18 +00009561 case TARGET_NR_fremovexattr:
9562 {
9563 void *n;
9564 n = lock_user_string(arg2);
9565 if (n) {
9566 ret = get_errno(fremovexattr(arg1, n));
9567 } else {
9568 ret = -TARGET_EFAULT;
9569 }
9570 unlock_user(n, arg2, 0);
9571 }
9572 break;
bellardebc05482003-09-30 21:08:41 +00009573#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07009574#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +00009575#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00009576 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00009577#if defined(TARGET_MIPS)
Petar Jovanovicd2792792014-06-18 17:48:20 +02009578 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
ths6f5b89a2007-03-02 20:48:00 +00009579 ret = 0;
9580 break;
edgar_iglef967792009-01-07 14:19:38 +00009581#elif defined(TARGET_CRIS)
9582 if (arg1 & 0xff)
9583 ret = -TARGET_EINVAL;
9584 else {
9585 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
9586 ret = 0;
9587 }
9588 break;
bellard8d18e892007-11-14 15:18:40 +00009589#elif defined(TARGET_I386) && defined(TARGET_ABI32)
9590 ret = do_set_thread_area(cpu_env, arg1);
9591 break;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009592#elif defined(TARGET_M68K)
9593 {
Andreas Färber0429a972013-08-26 18:14:44 +02009594 TaskState *ts = cpu->opaque;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009595 ts->tp_value = arg1;
Peter Maydell95c1eb12013-07-29 11:31:49 +01009596 ret = 0;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009597 break;
9598 }
ths6f5b89a2007-03-02 20:48:00 +00009599#else
9600 goto unimplemented_nowarn;
9601#endif
9602#endif
9603#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00009604 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00009605#if defined(TARGET_I386) && defined(TARGET_ABI32)
9606 ret = do_get_thread_area(cpu_env, arg1);
Peter Maydelld312bbe2013-07-16 18:44:56 +01009607 break;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009608#elif defined(TARGET_M68K)
9609 {
Andreas Färber0429a972013-08-26 18:14:44 +02009610 TaskState *ts = cpu->opaque;
Peter Maydell1ccd9372013-07-16 18:44:55 +01009611 ret = ts->tp_value;
9612 break;
9613 }
bellard8d18e892007-11-14 15:18:40 +00009614#else
bellard5cd43932003-03-29 16:54:36 +00009615 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00009616#endif
bellard8d18e892007-11-14 15:18:40 +00009617#endif
bellard48dc41e2006-06-21 18:15:50 +00009618#ifdef TARGET_NR_getdomainname
9619 case TARGET_NR_getdomainname:
9620 goto unimplemented_nowarn;
9621#endif
ths6f5b89a2007-03-02 20:48:00 +00009622
thsb5906f92007-03-19 13:32:45 +00009623#ifdef TARGET_NR_clock_gettime
9624 case TARGET_NR_clock_gettime:
9625 {
9626 struct timespec ts;
9627 ret = get_errno(clock_gettime(arg1, &ts));
9628 if (!is_error(ret)) {
9629 host_to_target_timespec(arg2, &ts);
9630 }
9631 break;
9632 }
9633#endif
9634#ifdef TARGET_NR_clock_getres
9635 case TARGET_NR_clock_getres:
9636 {
9637 struct timespec ts;
9638 ret = get_errno(clock_getres(arg1, &ts));
9639 if (!is_error(ret)) {
9640 host_to_target_timespec(arg2, &ts);
9641 }
9642 break;
9643 }
9644#endif
pbrook63d76512008-05-29 13:43:29 +00009645#ifdef TARGET_NR_clock_nanosleep
9646 case TARGET_NR_clock_nanosleep:
9647 {
9648 struct timespec ts;
9649 target_to_host_timespec(&ts, arg3);
9650 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
9651 if (arg4)
9652 host_to_target_timespec(arg4, &ts);
Tom Musta8fbe8fd2014-08-12 13:53:41 -05009653
9654#if defined(TARGET_PPC)
9655 /* clock_nanosleep is odd in that it returns positive errno values.
9656 * On PPC, CR0 bit 3 should be set in such a situation. */
9657 if (ret) {
9658 ((CPUPPCState *)cpu_env)->crf[0] |= 1;
9659 }
9660#endif
pbrook63d76512008-05-29 13:43:29 +00009661 break;
9662 }
9663#endif
thsb5906f92007-03-19 13:32:45 +00009664
ths6f5b89a2007-03-02 20:48:00 +00009665#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
9666 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00009667 ret = get_errno(set_tid_address((int *)g2h(arg1)));
9668 break;
ths6f5b89a2007-03-02 20:48:00 +00009669#endif
9670
ths3ae43202007-09-16 21:39:48 +00009671#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00009672 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00009673 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00009674 break;
9675#endif
9676
ths3ae43202007-09-16 21:39:48 +00009677#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00009678 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00009679 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
9680 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00009681 break;
9682#endif
9683
ths4f2b1fe2007-06-21 21:57:12 +00009684#ifdef TARGET_NR_set_robust_list
9685 case TARGET_NR_set_robust_list:
Peter Maydelle9a970a2013-02-08 04:34:54 +00009686 case TARGET_NR_get_robust_list:
9687 /* The ABI for supporting robust futexes has userspace pass
9688 * the kernel a pointer to a linked list which is updated by
9689 * userspace after the syscall; the list is walked by the kernel
9690 * when the thread exits. Since the linked list in QEMU guest
9691 * memory isn't a valid linked list for the host and we have
9692 * no way to reliably intercept the thread-death event, we can't
9693 * support these. Silently return ENOSYS so that guest userspace
9694 * falls back to a non-robust futex implementation (which should
9695 * be OK except in the corner case of the guest crashing while
9696 * holding a mutex that is shared with another process via
9697 * shared memory).
9698 */
9699 goto unimplemented_nowarn;
ths4f2b1fe2007-06-21 21:57:12 +00009700#endif
9701
Peter Maydell1acae9f2013-07-02 14:04:12 +01009702#if defined(TARGET_NR_utimensat)
ths9007f0e2007-09-25 17:50:37 +00009703 case TARGET_NR_utimensat:
9704 {
Riku Voipioebc996f2009-04-21 15:01:51 +03009705 struct timespec *tsp, ts[2];
9706 if (!arg3) {
9707 tsp = NULL;
9708 } else {
9709 target_to_host_timespec(ts, arg3);
9710 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
9711 tsp = ts;
9712 }
ths9007f0e2007-09-25 17:50:37 +00009713 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03009714 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00009715 else {
bellard579a97f2007-11-11 14:26:47 +00009716 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00009717 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00009718 goto fail;
9719 }
Riku Voipioebc996f2009-04-21 15:01:51 +03009720 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00009721 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00009722 }
9723 }
9724 break;
9725#endif
pbrookbd0c5662008-05-29 14:34:11 +00009726 case TARGET_NR_futex:
9727 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
9728 break;
aurel32dbfe4c32009-04-08 21:29:30 +00009729#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00009730 case TARGET_NR_inotify_init:
9731 ret = get_errno(sys_inotify_init());
9732 break;
9733#endif
Stefan Weila1606b02010-03-28 11:44:41 +02009734#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00009735#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
9736 case TARGET_NR_inotify_init1:
9737 ret = get_errno(sys_inotify_init1(arg1));
9738 break;
9739#endif
Stefan Weila1606b02010-03-28 11:44:41 +02009740#endif
aurel32dbfe4c32009-04-08 21:29:30 +00009741#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00009742 case TARGET_NR_inotify_add_watch:
9743 p = lock_user_string(arg2);
9744 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
9745 unlock_user(p, arg2, 0);
9746 break;
9747#endif
aurel32dbfe4c32009-04-08 21:29:30 +00009748#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00009749 case TARGET_NR_inotify_rm_watch:
9750 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
9751 break;
9752#endif
ths9007f0e2007-09-25 17:50:37 +00009753
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07009754#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00009755 case TARGET_NR_mq_open:
9756 {
Tom Mustab6ce1f62014-08-12 13:53:36 -05009757 struct mq_attr posix_mq_attr, *attrp;
aurel3224e10032009-04-15 16:11:43 +00009758
9759 p = lock_user_string(arg1 - 1);
Tom Mustab6ce1f62014-08-12 13:53:36 -05009760 if (arg4 != 0) {
aurel3224e10032009-04-15 16:11:43 +00009761 copy_from_user_mq_attr (&posix_mq_attr, arg4);
Tom Mustab6ce1f62014-08-12 13:53:36 -05009762 attrp = &posix_mq_attr;
9763 } else {
9764 attrp = 0;
9765 }
9766 ret = get_errno(mq_open(p, arg2, arg3, attrp));
aurel3224e10032009-04-15 16:11:43 +00009767 unlock_user (p, arg1, 0);
9768 }
9769 break;
9770
9771 case TARGET_NR_mq_unlink:
9772 p = lock_user_string(arg1 - 1);
9773 ret = get_errno(mq_unlink(p));
9774 unlock_user (p, arg1, 0);
9775 break;
9776
9777 case TARGET_NR_mq_timedsend:
9778 {
9779 struct timespec ts;
9780
9781 p = lock_user (VERIFY_READ, arg2, arg3, 1);
9782 if (arg5 != 0) {
9783 target_to_host_timespec(&ts, arg5);
9784 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
9785 host_to_target_timespec(arg5, &ts);
9786 }
9787 else
9788 ret = get_errno(mq_send(arg1, p, arg3, arg4));
9789 unlock_user (p, arg2, arg3);
9790 }
9791 break;
9792
9793 case TARGET_NR_mq_timedreceive:
9794 {
9795 struct timespec ts;
9796 unsigned int prio;
9797
9798 p = lock_user (VERIFY_READ, arg2, arg3, 1);
9799 if (arg5 != 0) {
9800 target_to_host_timespec(&ts, arg5);
9801 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
9802 host_to_target_timespec(arg5, &ts);
9803 }
9804 else
9805 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
9806 unlock_user (p, arg2, arg3);
9807 if (arg4 != 0)
9808 put_user_u32(prio, arg4);
9809 }
9810 break;
9811
9812 /* Not implemented for now... */
9813/* case TARGET_NR_mq_notify: */
9814/* break; */
9815
9816 case TARGET_NR_mq_getsetattr:
9817 {
9818 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
9819 ret = 0;
9820 if (arg3 != 0) {
9821 ret = mq_getattr(arg1, &posix_mq_attr_out);
9822 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
9823 }
9824 if (arg2 != 0) {
9825 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
9826 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
9827 }
9828
9829 }
9830 break;
9831#endif
9832
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309833#ifdef CONFIG_SPLICE
9834#ifdef TARGET_NR_tee
9835 case TARGET_NR_tee:
9836 {
9837 ret = get_errno(tee(arg1,arg2,arg3,arg4));
9838 }
9839 break;
9840#endif
9841#ifdef TARGET_NR_splice
9842 case TARGET_NR_splice:
9843 {
9844 loff_t loff_in, loff_out;
9845 loff_t *ploff_in = NULL, *ploff_out = NULL;
Andreas Schwab17644b32015-03-10 17:11:35 +01009846 if (arg2) {
9847 if (get_user_u64(loff_in, arg2)) {
9848 goto efault;
9849 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309850 ploff_in = &loff_in;
9851 }
Andreas Schwab17644b32015-03-10 17:11:35 +01009852 if (arg4) {
9853 if (get_user_u64(loff_out, arg4)) {
9854 goto efault;
9855 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309856 ploff_out = &loff_out;
9857 }
9858 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
Andreas Schwab17644b32015-03-10 17:11:35 +01009859 if (arg2) {
9860 if (put_user_u64(loff_in, arg2)) {
9861 goto efault;
9862 }
9863 }
9864 if (arg4) {
9865 if (put_user_u64(loff_out, arg4)) {
9866 goto efault;
9867 }
9868 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309869 }
9870 break;
9871#endif
9872#ifdef TARGET_NR_vmsplice
9873 case TARGET_NR_vmsplice:
9874 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07009875 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
9876 if (vec != NULL) {
9877 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
9878 unlock_iovec(vec, arg2, arg3, 0);
9879 } else {
9880 ret = -host_to_target_errno(errno);
9881 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05309882 }
9883 break;
9884#endif
9885#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03009886#ifdef CONFIG_EVENTFD
9887#if defined(TARGET_NR_eventfd)
9888 case TARGET_NR_eventfd:
9889 ret = get_errno(eventfd(arg1, 0));
Laurent Viviere36800c2015-10-02 14:48:09 +02009890 fd_trans_unregister(ret);
Riku Voipioc2882b92009-08-12 15:08:24 +03009891 break;
9892#endif
9893#if defined(TARGET_NR_eventfd2)
9894 case TARGET_NR_eventfd2:
Petar Jovanovic5947c692013-04-08 20:26:10 +02009895 {
9896 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
9897 if (arg2 & TARGET_O_NONBLOCK) {
9898 host_flags |= O_NONBLOCK;
9899 }
9900 if (arg2 & TARGET_O_CLOEXEC) {
9901 host_flags |= O_CLOEXEC;
9902 }
9903 ret = get_errno(eventfd(arg1, host_flags));
Laurent Viviere36800c2015-10-02 14:48:09 +02009904 fd_trans_unregister(ret);
Riku Voipioc2882b92009-08-12 15:08:24 +03009905 break;
Petar Jovanovic5947c692013-04-08 20:26:10 +02009906 }
Riku Voipioc2882b92009-08-12 15:08:24 +03009907#endif
9908#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03009909#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
9910 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +01009911#if TARGET_ABI_BITS == 32
9912 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
9913 target_offset64(arg5, arg6)));
9914#else
Ulrich Hechtd0927932009-09-17 20:22:14 +03009915 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +01009916#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03009917 break;
9918#endif
Peter Maydellc727f472011-01-06 11:05:10 +00009919#if defined(CONFIG_SYNC_FILE_RANGE)
9920#if defined(TARGET_NR_sync_file_range)
9921 case TARGET_NR_sync_file_range:
9922#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +03009923#if defined(TARGET_MIPS)
9924 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
9925 target_offset64(arg5, arg6), arg7));
9926#else
Peter Maydellc727f472011-01-06 11:05:10 +00009927 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
9928 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +03009929#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +00009930#else
9931 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
9932#endif
9933 break;
9934#endif
9935#if defined(TARGET_NR_sync_file_range2)
9936 case TARGET_NR_sync_file_range2:
9937 /* This is like sync_file_range but the arguments are reordered */
9938#if TARGET_ABI_BITS == 32
9939 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
9940 target_offset64(arg5, arg6), arg2));
9941#else
9942 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
9943#endif
9944 break;
9945#endif
9946#endif
Laurent Viviere36800c2015-10-02 14:48:09 +02009947#if defined(TARGET_NR_signalfd4)
9948 case TARGET_NR_signalfd4:
9949 ret = do_signalfd4(arg1, arg2, arg4);
9950 break;
9951#endif
9952#if defined(TARGET_NR_signalfd)
9953 case TARGET_NR_signalfd:
9954 ret = do_signalfd4(arg1, arg2, 0);
9955 break;
9956#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00009957#if defined(CONFIG_EPOLL)
9958#if defined(TARGET_NR_epoll_create)
9959 case TARGET_NR_epoll_create:
9960 ret = get_errno(epoll_create(arg1));
9961 break;
9962#endif
9963#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
9964 case TARGET_NR_epoll_create1:
9965 ret = get_errno(epoll_create1(arg1));
9966 break;
9967#endif
9968#if defined(TARGET_NR_epoll_ctl)
9969 case TARGET_NR_epoll_ctl:
9970 {
9971 struct epoll_event ep;
9972 struct epoll_event *epp = 0;
9973 if (arg4) {
9974 struct target_epoll_event *target_ep;
9975 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
9976 goto efault;
9977 }
9978 ep.events = tswap32(target_ep->events);
9979 /* The epoll_data_t union is just opaque data to the kernel,
9980 * so we transfer all 64 bits across and need not worry what
9981 * actual data type it is.
9982 */
9983 ep.data.u64 = tswap64(target_ep->data.u64);
9984 unlock_user_struct(target_ep, arg4, 0);
9985 epp = &ep;
9986 }
9987 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
9988 break;
9989 }
9990#endif
9991
9992#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9993#define IMPLEMENT_EPOLL_PWAIT
9994#endif
9995#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9996#if defined(TARGET_NR_epoll_wait)
9997 case TARGET_NR_epoll_wait:
9998#endif
9999#if defined(IMPLEMENT_EPOLL_PWAIT)
10000 case TARGET_NR_epoll_pwait:
10001#endif
10002 {
10003 struct target_epoll_event *target_ep;
10004 struct epoll_event *ep;
10005 int epfd = arg1;
10006 int maxevents = arg3;
10007 int timeout = arg4;
10008
10009 target_ep = lock_user(VERIFY_WRITE, arg2,
10010 maxevents * sizeof(struct target_epoll_event), 1);
10011 if (!target_ep) {
10012 goto efault;
10013 }
10014
10015 ep = alloca(maxevents * sizeof(struct epoll_event));
10016
10017 switch (num) {
10018#if defined(IMPLEMENT_EPOLL_PWAIT)
10019 case TARGET_NR_epoll_pwait:
10020 {
10021 target_sigset_t *target_set;
10022 sigset_t _set, *set = &_set;
10023
10024 if (arg5) {
10025 target_set = lock_user(VERIFY_READ, arg5,
10026 sizeof(target_sigset_t), 1);
10027 if (!target_set) {
10028 unlock_user(target_ep, arg2, 0);
10029 goto efault;
10030 }
10031 target_to_host_sigset(set, target_set);
10032 unlock_user(target_set, arg5, 0);
10033 } else {
10034 set = NULL;
10035 }
10036
10037 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
10038 break;
10039 }
10040#endif
10041#if defined(TARGET_NR_epoll_wait)
10042 case TARGET_NR_epoll_wait:
10043 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
10044 break;
10045#endif
10046 default:
10047 ret = -TARGET_ENOSYS;
10048 }
10049 if (!is_error(ret)) {
10050 int i;
10051 for (i = 0; i < ret; i++) {
10052 target_ep[i].events = tswap32(ep[i].events);
10053 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
10054 }
10055 }
10056 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
10057 break;
10058 }
10059#endif
10060#endif
Peter Maydell163a05a2011-06-27 17:44:52 +010010061#ifdef TARGET_NR_prlimit64
10062 case TARGET_NR_prlimit64:
10063 {
10064 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
10065 struct target_rlimit64 *target_rnew, *target_rold;
10066 struct host_rlimit64 rnew, rold, *rnewp = 0;
Felix Janda95018012014-12-02 22:11:17 +010010067 int resource = target_to_host_resource(arg2);
Peter Maydell163a05a2011-06-27 17:44:52 +010010068 if (arg3) {
10069 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
10070 goto efault;
10071 }
10072 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
10073 rnew.rlim_max = tswap64(target_rnew->rlim_max);
10074 unlock_user_struct(target_rnew, arg3, 0);
10075 rnewp = &rnew;
10076 }
10077
Felix Janda95018012014-12-02 22:11:17 +010010078 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
Peter Maydell163a05a2011-06-27 17:44:52 +010010079 if (!is_error(ret) && arg4) {
10080 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
10081 goto efault;
10082 }
10083 target_rold->rlim_cur = tswap64(rold.rlim_cur);
10084 target_rold->rlim_max = tswap64(rold.rlim_max);
10085 unlock_user_struct(target_rold, arg4, 1);
10086 }
10087 break;
10088 }
10089#endif
Richard Henderson3d21d292012-09-15 13:20:46 -070010090#ifdef TARGET_NR_gethostname
10091 case TARGET_NR_gethostname:
10092 {
10093 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
10094 if (name) {
10095 ret = get_errno(gethostname(name, arg2));
10096 unlock_user(name, arg1, arg2);
10097 } else {
10098 ret = -TARGET_EFAULT;
10099 }
10100 break;
10101 }
10102#endif
Riku Voipio89aaf1a2013-07-24 09:44:26 +030010103#ifdef TARGET_NR_atomic_cmpxchg_32
10104 case TARGET_NR_atomic_cmpxchg_32:
10105 {
10106 /* should use start_exclusive from main.c */
10107 abi_ulong mem_value;
10108 if (get_user_u32(mem_value, arg6)) {
10109 target_siginfo_t info;
10110 info.si_signo = SIGSEGV;
10111 info.si_errno = 0;
10112 info.si_code = TARGET_SEGV_MAPERR;
10113 info._sifields._sigfault._addr = arg6;
10114 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
10115 ret = 0xdeadbeef;
10116
10117 }
10118 if (mem_value == arg2)
10119 put_user_u32(arg1, arg6);
10120 ret = mem_value;
10121 break;
10122 }
10123#endif
10124#ifdef TARGET_NR_atomic_barrier
10125 case TARGET_NR_atomic_barrier:
10126 {
10127 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
Peter Maydell3b899ea2014-03-12 13:06:01 +000010128 ret = 0;
Riku Voipio89aaf1a2013-07-24 09:44:26 +030010129 break;
10130 }
10131#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010132
10133#ifdef TARGET_NR_timer_create
10134 case TARGET_NR_timer_create:
10135 {
10136 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
10137
10138 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010139
10140 int clkid = arg1;
10141 int timer_index = next_free_host_timer();
10142
10143 if (timer_index < 0) {
10144 ret = -TARGET_EAGAIN;
10145 } else {
10146 timer_t *phtimer = g_posix_timers + timer_index;
10147
10148 if (arg2) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010149 phost_sevp = &host_sevp;
Peter Maydellc0659762014-08-09 15:42:32 +010010150 ret = target_to_host_sigevent(phost_sevp, arg2);
10151 if (ret != 0) {
10152 break;
10153 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010154 }
10155
10156 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
10157 if (ret) {
10158 phtimer = NULL;
10159 } else {
Alexander Grafaecc8862014-11-10 21:33:03 +010010160 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010161 goto efault;
10162 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010163 }
10164 }
10165 break;
10166 }
10167#endif
10168
10169#ifdef TARGET_NR_timer_settime
10170 case TARGET_NR_timer_settime:
10171 {
10172 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
10173 * struct itimerspec * old_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010010174 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010175
Alexander Grafaecc8862014-11-10 21:33:03 +010010176 if (timerid < 0) {
10177 ret = timerid;
10178 } else if (arg3 == 0) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010179 ret = -TARGET_EINVAL;
10180 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010181 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010182 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
10183
10184 target_to_host_itimerspec(&hspec_new, arg3);
10185 ret = get_errno(
10186 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
10187 host_to_target_itimerspec(arg2, &hspec_old);
10188 }
10189 break;
10190 }
10191#endif
10192
10193#ifdef TARGET_NR_timer_gettime
10194 case TARGET_NR_timer_gettime:
10195 {
10196 /* args: timer_t timerid, struct itimerspec *curr_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010010197 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010198
Alexander Grafaecc8862014-11-10 21:33:03 +010010199 if (timerid < 0) {
10200 ret = timerid;
10201 } else if (!arg2) {
10202 ret = -TARGET_EFAULT;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010203 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010204 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010205 struct itimerspec hspec;
10206 ret = get_errno(timer_gettime(htimer, &hspec));
10207
10208 if (host_to_target_itimerspec(arg2, &hspec)) {
10209 ret = -TARGET_EFAULT;
10210 }
10211 }
10212 break;
10213 }
10214#endif
10215
10216#ifdef TARGET_NR_timer_getoverrun
10217 case TARGET_NR_timer_getoverrun:
10218 {
10219 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010010220 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010221
Alexander Grafaecc8862014-11-10 21:33:03 +010010222 if (timerid < 0) {
10223 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010224 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010225 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010226 ret = get_errno(timer_getoverrun(htimer));
10227 }
Laurent Viviere36800c2015-10-02 14:48:09 +020010228 fd_trans_unregister(ret);
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010229 break;
10230 }
10231#endif
10232
10233#ifdef TARGET_NR_timer_delete
10234 case TARGET_NR_timer_delete:
10235 {
10236 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010010237 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010238
Alexander Grafaecc8862014-11-10 21:33:03 +010010239 if (timerid < 0) {
10240 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010241 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010242 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010243 ret = get_errno(timer_delete(htimer));
Alexander Grafe52a99f2014-08-22 13:56:18 +020010244 g_posix_timers[timerid] = 0;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010245 }
10246 break;
10247 }
10248#endif
10249
Riku Voipio51834342014-06-22 11:25:42 +010010250#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
10251 case TARGET_NR_timerfd_create:
10252 ret = get_errno(timerfd_create(arg1,
10253 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
10254 break;
10255#endif
10256
10257#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
10258 case TARGET_NR_timerfd_gettime:
10259 {
10260 struct itimerspec its_curr;
10261
10262 ret = get_errno(timerfd_gettime(arg1, &its_curr));
10263
10264 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
10265 goto efault;
10266 }
10267 }
10268 break;
10269#endif
10270
10271#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
10272 case TARGET_NR_timerfd_settime:
10273 {
10274 struct itimerspec its_new, its_old, *p_new;
10275
10276 if (arg3) {
10277 if (target_to_host_itimerspec(&its_new, arg3)) {
10278 goto efault;
10279 }
10280 p_new = &its_new;
10281 } else {
10282 p_new = NULL;
10283 }
10284
10285 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
10286
10287 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
10288 goto efault;
10289 }
10290 }
10291 break;
10292#endif
10293
Paul Burtonab31cda2014-06-22 11:25:43 +010010294#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
10295 case TARGET_NR_ioprio_get:
10296 ret = get_errno(ioprio_get(arg1, arg2));
10297 break;
10298#endif
10299
10300#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
10301 case TARGET_NR_ioprio_set:
10302 ret = get_errno(ioprio_set(arg1, arg2, arg3));
10303 break;
10304#endif
10305
Riku Voipio9af5c902014-08-12 15:58:57 +030010306#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
10307 case TARGET_NR_setns:
10308 ret = get_errno(setns(arg1, arg2));
10309 break;
10310#endif
10311#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
10312 case TARGET_NR_unshare:
10313 ret = get_errno(unshare(arg1));
10314 break;
10315#endif
10316
bellard31e31b82003-02-18 22:55:36 +000010317 default:
10318 unimplemented:
bellard5cd43932003-03-29 16:54:36 +000010319 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +000010320#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 +000010321 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +000010322#endif
ths0da46a62007-10-20 20:23:07 +000010323 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +000010324 break;
10325 }
bellard579a97f2007-11-11 14:26:47 +000010326fail:
bellardc573ff62004-01-04 15:51:36 +000010327#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +000010328 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +000010329#endif
thsb92c47c2007-11-01 00:07:38 +000010330 if(do_strace)
10331 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +000010332 return ret;
bellard579a97f2007-11-11 14:26:47 +000010333efault:
10334 ret = -TARGET_EFAULT;
10335 goto fail;
bellard31e31b82003-02-18 22:55:36 +000010336}