blob: 9cc9a35cf5e45efccf1e4f3cea1c5a90d2c407ac [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>
Laurent Vivier6c5b5642016-05-22 18:56:19 +0200104#include <linux/netlink.h>
Laurent Vivier575b22b2016-06-02 22:14:15 +0200105#ifdef CONFIG_RTNETLINK
Laurent Vivier6c5b5642016-05-22 18:56:19 +0200106#include <linux/rtnetlink.h>
Laurent Vivier575b22b2016-06-02 22:14:15 +0200107#endif
Laurent Vivier5ce9bb52016-05-22 18:56:21 +0200108#include <linux/audit.h>
pbrookd7e40362008-05-23 16:06:43 +0000109#include "linux_loop.h"
Riku Voipio18cb0082014-02-19 12:59:58 +0200110#include "uname.h"
bellard31e31b82003-02-18 22:55:36 +0000111
bellard3ef693a2003-03-23 20:17:16 +0000112#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000113
pbrookd865bab2008-06-07 22:12:17 +0000114#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
115 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
pbrook30813ce2008-06-02 15:45:44 +0000116
bellard72f03902003-02-18 23:33:18 +0000117//#define DEBUG
Timothy E Baldwin71a8f7f2016-05-12 18:47:45 +0100118/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
119 * once. This exercises the codepaths for restart.
120 */
121//#define DEBUG_ERESTARTSYS
bellard31e31b82003-02-18 22:55:36 +0000122
bellard1a9353d2003-03-16 20:28:50 +0000123//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000124#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
125#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000126
bellard70a194b2003-08-11 22:20:16 +0000127
bellard70a194b2003-08-11 22:20:16 +0000128#undef _syscall0
129#undef _syscall1
130#undef _syscall2
131#undef _syscall3
132#undef _syscall4
133#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000134#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000135
bellard83fcb512006-06-14 13:37:16 +0000136#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000137static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000138{ \
139 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000140}
141
bellard83fcb512006-06-14 13:37:16 +0000142#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000143static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000144{ \
145 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000146}
147
bellard83fcb512006-06-14 13:37:16 +0000148#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000149static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000150{ \
151 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000152}
153
bellard83fcb512006-06-14 13:37:16 +0000154#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000155static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000156{ \
157 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000158}
159
bellard83fcb512006-06-14 13:37:16 +0000160#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000161static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000162{ \
163 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000164}
165
bellard83fcb512006-06-14 13:37:16 +0000166#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
167 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000168static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000169{ \
170 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000171}
bellard83fcb512006-06-14 13:37:16 +0000172
173
174#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
175 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000176static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
177 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000178{ \
179 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
180}
181
bellard70a194b2003-08-11 22:20:16 +0000182
bellard31e31b82003-02-18 22:55:36 +0000183#define __NR_sys_uname __NR_uname
bellard72f03902003-02-18 23:33:18 +0000184#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000185#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000186#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000187#define __NR_sys_getpriority __NR_getpriority
bellard66fb9762003-03-23 01:06:05 +0000188#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
ths7494b0f2007-02-11 18:26:53 +0000189#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000190#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000191#define __NR_sys_tkill __NR_tkill
pbrookbd0c5662008-05-29 14:34:11 +0000192#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000193#define __NR_sys_inotify_init __NR_inotify_init
194#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
195#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000196
Alexander Graf42a39fb2011-04-15 17:32:45 +0200197#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
198 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000199#define __NR__llseek __NR_lseek
200#endif
201
James Hogana29e5ba2014-03-25 21:51:08 +0000202/* Newer kernel ports have llseek() instead of _llseek() */
203#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
204#define TARGET_NR__llseek TARGET_NR_llseek
205#endif
206
bellard72f03902003-02-18 23:33:18 +0000207#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000208_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000209#else
ths0da46a62007-10-20 20:23:07 +0000210/* This is a replacement for the host gettid() and must return a host
211 errno. */
bellard72f03902003-02-18 23:33:18 +0000212static int gettid(void) {
213 return -ENOSYS;
214}
215#endif
Chen Gang704eff62015-08-21 05:37:33 +0800216#if defined(TARGET_NR_getdents) && defined(__NR_getdents)
aurel323b3f24a2009-04-15 16:12:13 +0000217_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
Peter Maydell3307e232013-06-12 16:20:21 +0100218#endif
219#if !defined(__NR_getdents) || \
220 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
aurel323b3f24a2009-04-15 16:12:13 +0000221_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
222#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700223#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000224_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
225 loff_t *, res, uint, wh);
226#endif
227_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
228_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
229#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
230_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
231#endif
232#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
233_syscall2(int,sys_tkill,int,tid,int,sig)
234#endif
235#ifdef __NR_exit_group
236_syscall1(int,exit_group,int,error_code)
237#endif
238#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
239_syscall1(int,set_tid_address,int *,tidptr)
240#endif
aurel323b3f24a2009-04-15 16:12:13 +0000241#if defined(TARGET_NR_futex) && defined(__NR_futex)
242_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
243 const struct timespec *,timeout,int *,uaddr2,int,val3)
244#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500245#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
246_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
247 unsigned long *, user_mask_ptr);
248#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
249_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
250 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200251_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
252 void *, arg);
Peter Maydelle0eb2102014-03-17 12:15:35 +0000253_syscall2(int, capget, struct __user_cap_header_struct *, header,
254 struct __user_cap_data_struct *, data);
255_syscall2(int, capset, struct __user_cap_header_struct *, header,
256 struct __user_cap_data_struct *, data);
Paul Burtonab31cda2014-06-22 11:25:43 +0100257#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
258_syscall2(int, ioprio_get, int, which, int, who)
259#endif
260#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
261_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
262#endif
Laurent Vivierf894efd2016-02-21 10:56:23 +0100263#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
264_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
265#endif
aurel323b3f24a2009-04-15 16:12:13 +0000266
267static bitmask_transtbl fcntl_flags_tbl[] = {
268 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
269 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
270 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
271 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
272 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
273 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
274 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
275 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700276 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000277 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
278 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
279 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
280 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000281#if defined(O_DIRECT)
282 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
283#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700284#if defined(O_NOATIME)
285 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
286#endif
287#if defined(O_CLOEXEC)
288 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
289#endif
290#if defined(O_PATH)
291 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
292#endif
293 /* Don't terminate the list prematurely on 64-bit host+guest. */
294#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
295 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
296#endif
aurel323b3f24a2009-04-15 16:12:13 +0000297 { 0, 0, 0, 0 }
298};
299
Laurent Vivier5d4d3662015-10-28 21:40:43 +0100300typedef abi_long (*TargetFdDataFunc)(void *, size_t);
Laurent Vivier7b36f782015-10-28 21:40:44 +0100301typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
Laurent Viviere36800c2015-10-02 14:48:09 +0200302typedef struct TargetFdTrans {
Laurent Vivier5d4d3662015-10-28 21:40:43 +0100303 TargetFdDataFunc host_to_target_data;
304 TargetFdDataFunc target_to_host_data;
Laurent Vivier7b36f782015-10-28 21:40:44 +0100305 TargetFdAddrFunc target_to_host_addr;
Laurent Viviere36800c2015-10-02 14:48:09 +0200306} TargetFdTrans;
307
308static TargetFdTrans **target_fd_trans;
309
310static unsigned int target_fd_max;
311
Laurent Vivier6c5b5642016-05-22 18:56:19 +0200312static TargetFdDataFunc fd_trans_target_to_host_data(int fd)
313{
314 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
315 return target_fd_trans[fd]->target_to_host_data;
316 }
317 return NULL;
318}
319
Laurent Vivier5d4d3662015-10-28 21:40:43 +0100320static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
Laurent Viviere36800c2015-10-02 14:48:09 +0200321{
Laurent Vivier861d72c2015-10-28 21:40:46 +0100322 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
Laurent Vivier5d4d3662015-10-28 21:40:43 +0100323 return target_fd_trans[fd]->host_to_target_data;
Laurent Viviere36800c2015-10-02 14:48:09 +0200324 }
325 return NULL;
326}
327
Laurent Vivier7b36f782015-10-28 21:40:44 +0100328static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
329{
Laurent Vivier861d72c2015-10-28 21:40:46 +0100330 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
Laurent Vivier7b36f782015-10-28 21:40:44 +0100331 return target_fd_trans[fd]->target_to_host_addr;
332 }
333 return NULL;
334}
335
Laurent Viviere36800c2015-10-02 14:48:09 +0200336static void fd_trans_register(int fd, TargetFdTrans *trans)
337{
338 unsigned int oldmax;
339
340 if (fd >= target_fd_max) {
341 oldmax = target_fd_max;
342 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
Laurent Vivier5089c7c2016-01-18 23:50:45 +0100343 target_fd_trans = g_renew(TargetFdTrans *,
344 target_fd_trans, target_fd_max);
Laurent Viviere36800c2015-10-02 14:48:09 +0200345 memset((void *)(target_fd_trans + oldmax), 0,
346 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
347 }
348 target_fd_trans[fd] = trans;
349}
350
351static void fd_trans_unregister(int fd)
352{
353 if (fd >= 0 && fd < target_fd_max) {
354 target_fd_trans[fd] = NULL;
355 }
356}
357
358static void fd_trans_dup(int oldfd, int newfd)
359{
360 fd_trans_unregister(newfd);
361 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
362 fd_trans_register(newfd, target_fd_trans[oldfd]);
363 }
364}
365
aurel323b3f24a2009-04-15 16:12:13 +0000366static int sys_getcwd1(char *buf, size_t size)
367{
368 if (getcwd(buf, size) == NULL) {
369 /* getcwd() sets errno */
370 return (-1);
371 }
aurel32aaf4ad32009-04-16 14:17:14 +0000372 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000373}
374
Peter Maydell1acae9f2013-07-02 14:04:12 +0100375#ifdef TARGET_NR_utimensat
Riku Voipioebc996f2009-04-21 15:01:51 +0300376#ifdef CONFIG_UTIMENSAT
377static int sys_utimensat(int dirfd, const char *pathname,
378 const struct timespec times[2], int flags)
379{
380 if (pathname == NULL)
381 return futimens(dirfd, times);
382 else
383 return utimensat(dirfd, pathname, times, flags);
384}
Peter Maydell1acae9f2013-07-02 14:04:12 +0100385#elif defined(__NR_utimensat)
386#define __NR_sys_utimensat __NR_utimensat
ths9007f0e2007-09-25 17:50:37 +0000387_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
388 const struct timespec *,tsp,int,flags)
Peter Maydell1acae9f2013-07-02 14:04:12 +0100389#else
390static int sys_utimensat(int dirfd, const char *pathname,
391 const struct timespec times[2], int flags)
392{
393 errno = ENOSYS;
394 return -1;
395}
ths9007f0e2007-09-25 17:50:37 +0000396#endif
Peter Maydell1acae9f2013-07-02 14:04:12 +0100397#endif /* TARGET_NR_utimensat */
aurel323b3f24a2009-04-15 16:12:13 +0000398
399#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000400#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000401
aurel3239b59762008-10-01 21:46:50 +0000402#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000403static int sys_inotify_init(void)
404{
405 return (inotify_init());
406}
aurel3239b59762008-10-01 21:46:50 +0000407#endif
408#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000409static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
410{
411 return (inotify_add_watch(fd, pathname, mask));
412}
aurel3239b59762008-10-01 21:46:50 +0000413#endif
414#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000415static int sys_inotify_rm_watch(int fd, int32_t wd)
416{
aurel328690e422009-04-17 13:50:32 +0000417 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000418}
aurel3239b59762008-10-01 21:46:50 +0000419#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000420#ifdef CONFIG_INOTIFY1
421#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
422static int sys_inotify_init1(int flags)
423{
424 return (inotify_init1(flags));
425}
426#endif
427#endif
aurel323b3f24a2009-04-15 16:12:13 +0000428#else
429/* Userspace can usually survive runtime without inotify */
430#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000431#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000432#undef TARGET_NR_inotify_add_watch
433#undef TARGET_NR_inotify_rm_watch
434#endif /* CONFIG_INOTIFY */
435
Mike Frysingerd8035d42011-02-07 01:05:51 -0500436#if defined(TARGET_NR_ppoll)
437#ifndef __NR_ppoll
438# define __NR_ppoll -1
439#endif
440#define __NR_sys_ppoll __NR_ppoll
441_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
Natanael Copa34d60862014-04-29 13:11:20 +0200442 struct timespec *, timeout, const sigset_t *, sigmask,
Mike Frysingerd8035d42011-02-07 01:05:51 -0500443 size_t, sigsetsize)
444#endif
bellard66fb9762003-03-23 01:06:05 +0000445
Peter Maydell163a05a2011-06-27 17:44:52 +0100446#if defined(TARGET_NR_prlimit64)
447#ifndef __NR_prlimit64
448# define __NR_prlimit64 -1
449#endif
450#define __NR_sys_prlimit64 __NR_prlimit64
451/* The glibc rlimit structure may not be that used by the underlying syscall */
452struct host_rlimit64 {
453 uint64_t rlim_cur;
454 uint64_t rlim_max;
455};
456_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
457 const struct host_rlimit64 *, new_limit,
458 struct host_rlimit64 *, old_limit)
459#endif
460
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100461
462#if defined(TARGET_NR_timer_create)
463/* Maxiumum of 32 active POSIX timers allowed at any one time. */
464static timer_t g_posix_timers[32] = { 0, } ;
465
466static inline int next_free_host_timer(void)
467{
468 int k ;
469 /* FIXME: Does finding the next free slot require a lock? */
470 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
471 if (g_posix_timers[k] == 0) {
472 g_posix_timers[k] = (timer_t) 1;
473 return k;
474 }
475 }
476 return -1;
477}
478#endif
479
Riku Voipio48e515d2011-07-12 15:40:51 +0300480/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
Alexander Graf4a1def42012-09-29 15:32:38 +0000481#ifdef TARGET_ARM
Riku Voipio48e515d2011-07-12 15:40:51 +0300482static inline int regpairs_aligned(void *cpu_env) {
483 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
484}
485#elif defined(TARGET_MIPS)
486static inline int regpairs_aligned(void *cpu_env) { return 1; }
Alexander Graf4a1def42012-09-29 15:32:38 +0000487#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
488/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
489 * of registers which translates to the same as ARM/MIPS, because we start with
490 * r3 as arg1 */
491static inline int regpairs_aligned(void *cpu_env) { return 1; }
Riku Voipio48e515d2011-07-12 15:40:51 +0300492#else
493static inline int regpairs_aligned(void *cpu_env) { return 0; }
494#endif
495
thsb92c47c2007-11-01 00:07:38 +0000496#define ERRNO_TABLE_SIZE 1200
497
498/* target_to_host_errno_table[] is initialized from
499 * host_to_target_errno_table[] in syscall_init(). */
500static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
501};
502
ths637947f2007-06-01 12:09:19 +0000503/*
thsfe8f0962007-07-12 10:59:21 +0000504 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000505 * minus the errnos that are not actually generic to all archs.
506 */
thsb92c47c2007-11-01 00:07:38 +0000507static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
Chen Gang08703b92015-09-07 10:35:06 +0800508 [EAGAIN] = TARGET_EAGAIN,
ths637947f2007-06-01 12:09:19 +0000509 [EIDRM] = TARGET_EIDRM,
510 [ECHRNG] = TARGET_ECHRNG,
511 [EL2NSYNC] = TARGET_EL2NSYNC,
512 [EL3HLT] = TARGET_EL3HLT,
513 [EL3RST] = TARGET_EL3RST,
514 [ELNRNG] = TARGET_ELNRNG,
515 [EUNATCH] = TARGET_EUNATCH,
516 [ENOCSI] = TARGET_ENOCSI,
517 [EL2HLT] = TARGET_EL2HLT,
518 [EDEADLK] = TARGET_EDEADLK,
519 [ENOLCK] = TARGET_ENOLCK,
520 [EBADE] = TARGET_EBADE,
521 [EBADR] = TARGET_EBADR,
522 [EXFULL] = TARGET_EXFULL,
523 [ENOANO] = TARGET_ENOANO,
524 [EBADRQC] = TARGET_EBADRQC,
525 [EBADSLT] = TARGET_EBADSLT,
526 [EBFONT] = TARGET_EBFONT,
527 [ENOSTR] = TARGET_ENOSTR,
528 [ENODATA] = TARGET_ENODATA,
529 [ETIME] = TARGET_ETIME,
530 [ENOSR] = TARGET_ENOSR,
531 [ENONET] = TARGET_ENONET,
532 [ENOPKG] = TARGET_ENOPKG,
533 [EREMOTE] = TARGET_EREMOTE,
534 [ENOLINK] = TARGET_ENOLINK,
535 [EADV] = TARGET_EADV,
536 [ESRMNT] = TARGET_ESRMNT,
537 [ECOMM] = TARGET_ECOMM,
538 [EPROTO] = TARGET_EPROTO,
539 [EDOTDOT] = TARGET_EDOTDOT,
540 [EMULTIHOP] = TARGET_EMULTIHOP,
541 [EBADMSG] = TARGET_EBADMSG,
542 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
543 [EOVERFLOW] = TARGET_EOVERFLOW,
544 [ENOTUNIQ] = TARGET_ENOTUNIQ,
545 [EBADFD] = TARGET_EBADFD,
546 [EREMCHG] = TARGET_EREMCHG,
547 [ELIBACC] = TARGET_ELIBACC,
548 [ELIBBAD] = TARGET_ELIBBAD,
549 [ELIBSCN] = TARGET_ELIBSCN,
550 [ELIBMAX] = TARGET_ELIBMAX,
551 [ELIBEXEC] = TARGET_ELIBEXEC,
552 [EILSEQ] = TARGET_EILSEQ,
553 [ENOSYS] = TARGET_ENOSYS,
554 [ELOOP] = TARGET_ELOOP,
555 [ERESTART] = TARGET_ERESTART,
556 [ESTRPIPE] = TARGET_ESTRPIPE,
557 [ENOTEMPTY] = TARGET_ENOTEMPTY,
558 [EUSERS] = TARGET_EUSERS,
559 [ENOTSOCK] = TARGET_ENOTSOCK,
560 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
561 [EMSGSIZE] = TARGET_EMSGSIZE,
562 [EPROTOTYPE] = TARGET_EPROTOTYPE,
563 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
564 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
565 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
566 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
567 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
568 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
569 [EADDRINUSE] = TARGET_EADDRINUSE,
570 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
571 [ENETDOWN] = TARGET_ENETDOWN,
572 [ENETUNREACH] = TARGET_ENETUNREACH,
573 [ENETRESET] = TARGET_ENETRESET,
574 [ECONNABORTED] = TARGET_ECONNABORTED,
575 [ECONNRESET] = TARGET_ECONNRESET,
576 [ENOBUFS] = TARGET_ENOBUFS,
577 [EISCONN] = TARGET_EISCONN,
578 [ENOTCONN] = TARGET_ENOTCONN,
579 [EUCLEAN] = TARGET_EUCLEAN,
580 [ENOTNAM] = TARGET_ENOTNAM,
581 [ENAVAIL] = TARGET_ENAVAIL,
582 [EISNAM] = TARGET_EISNAM,
583 [EREMOTEIO] = TARGET_EREMOTEIO,
584 [ESHUTDOWN] = TARGET_ESHUTDOWN,
585 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
586 [ETIMEDOUT] = TARGET_ETIMEDOUT,
587 [ECONNREFUSED] = TARGET_ECONNREFUSED,
588 [EHOSTDOWN] = TARGET_EHOSTDOWN,
589 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
590 [EALREADY] = TARGET_EALREADY,
591 [EINPROGRESS] = TARGET_EINPROGRESS,
592 [ESTALE] = TARGET_ESTALE,
593 [ECANCELED] = TARGET_ECANCELED,
594 [ENOMEDIUM] = TARGET_ENOMEDIUM,
595 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000596#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000597 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000598#endif
599#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000600 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000601#endif
602#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000603 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000604#endif
605#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000606 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000607#endif
608#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000609 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000610#endif
611#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000612 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000613#endif
thsb92c47c2007-11-01 00:07:38 +0000614};
ths637947f2007-06-01 12:09:19 +0000615
616static inline int host_to_target_errno(int err)
617{
Timothy E Baldwin24661192016-05-12 18:47:25 +0100618 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
619 host_to_target_errno_table[err]) {
ths637947f2007-06-01 12:09:19 +0000620 return host_to_target_errno_table[err];
Timothy E Baldwin24661192016-05-12 18:47:25 +0100621 }
ths637947f2007-06-01 12:09:19 +0000622 return err;
623}
624
thsb92c47c2007-11-01 00:07:38 +0000625static inline int target_to_host_errno(int err)
626{
Timothy E Baldwin24661192016-05-12 18:47:25 +0100627 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
628 target_to_host_errno_table[err]) {
thsb92c47c2007-11-01 00:07:38 +0000629 return target_to_host_errno_table[err];
Timothy E Baldwin24661192016-05-12 18:47:25 +0100630 }
thsb92c47c2007-11-01 00:07:38 +0000631 return err;
632}
633
blueswir1992f48a2007-10-14 16:27:31 +0000634static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000635{
636 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000637 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000638 else
639 return ret;
640}
641
blueswir1992f48a2007-10-14 16:27:31 +0000642static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000643{
blueswir1992f48a2007-10-14 16:27:31 +0000644 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000645}
646
thsb92c47c2007-11-01 00:07:38 +0000647char *target_strerror(int err)
648{
Alexander Graf962b2892011-11-21 12:04:07 +0100649 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
650 return NULL;
651 }
thsb92c47c2007-11-01 00:07:38 +0000652 return strerror(target_to_host_errno(err));
653}
654
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +0100655#define safe_syscall0(type, name) \
656static type safe_##name(void) \
657{ \
658 return safe_syscall(__NR_##name); \
659}
660
661#define safe_syscall1(type, name, type1, arg1) \
662static type safe_##name(type1 arg1) \
663{ \
664 return safe_syscall(__NR_##name, arg1); \
665}
666
667#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
668static type safe_##name(type1 arg1, type2 arg2) \
669{ \
670 return safe_syscall(__NR_##name, arg1, arg2); \
671}
672
673#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
674static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
675{ \
676 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
677}
678
679#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
680 type4, arg4) \
681static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
682{ \
683 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
684}
685
686#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
687 type4, arg4, type5, arg5) \
688static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
689 type5 arg5) \
690{ \
691 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
692}
693
694#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
695 type4, arg4, type5, arg5, type6, arg6) \
696static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
697 type5 arg5, type6 arg6) \
698{ \
699 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
700}
701
Timothy E Baldwin50afd022016-05-12 18:47:47 +0100702safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
703safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
Timothy E Baldwinc10a0732016-05-12 18:47:48 +0100704safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
705 int, flags, mode_t, mode)
Timothy E Baldwin4af80a32016-05-12 18:47:49 +0100706safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
707 struct rusage *, rusage)
708safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
709 int, options, struct rusage *, rusage)
Timothy E Baldwinffdcbe22016-05-12 18:47:50 +0100710safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
Peter Maydell6df9d382016-05-12 18:47:51 +0100711safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
712 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
Peter Maydelld509eeb2016-05-12 18:47:52 +0100713safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
714 const struct timespec *,timeout,int *,uaddr2,int,val3)
Timothy E Baldwin50afd022016-05-12 18:47:47 +0100715
Paul Burton8289d112014-06-22 11:25:33 +0100716static inline int host_to_target_sock_type(int host_type)
717{
718 int target_type;
719
720 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
721 case SOCK_DGRAM:
722 target_type = TARGET_SOCK_DGRAM;
723 break;
724 case SOCK_STREAM:
725 target_type = TARGET_SOCK_STREAM;
726 break;
727 default:
728 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
729 break;
730 }
731
732#if defined(SOCK_CLOEXEC)
733 if (host_type & SOCK_CLOEXEC) {
734 target_type |= TARGET_SOCK_CLOEXEC;
735 }
736#endif
737
738#if defined(SOCK_NONBLOCK)
739 if (host_type & SOCK_NONBLOCK) {
740 target_type |= TARGET_SOCK_NONBLOCK;
741 }
742#endif
743
744 return target_type;
745}
746
blueswir1992f48a2007-10-14 16:27:31 +0000747static abi_ulong target_brk;
748static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000749static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000750
blueswir1992f48a2007-10-14 16:27:31 +0000751void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000752{
blueswir14c1de732007-07-07 20:45:44 +0000753 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000754 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000755}
756
vincent4d1de872011-06-14 21:56:33 +0000757//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
758#define DEBUGF_BRK(message, args...)
759
ths0da46a62007-10-20 20:23:07 +0000760/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000761abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000762{
blueswir1992f48a2007-10-14 16:27:31 +0000763 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000764 int new_alloc_size;
765
Paul Brook3a0c6c42012-02-09 19:04:27 +0000766 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000767
vincent4d1de872011-06-14 21:56:33 +0000768 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000769 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000770 return target_brk;
771 }
772 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000773 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
774 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000775 return target_brk;
776 }
bellard31e31b82003-02-18 22:55:36 +0000777
vincent4d1de872011-06-14 21:56:33 +0000778 /* If the new brk is less than the highest page reserved to the
779 * target heap allocation, set it and we're almost done... */
780 if (new_brk <= brk_page) {
781 /* Heap contents are initialized to zero, as for anonymous
782 * mapped pages. */
783 if (new_brk > target_brk) {
784 memset(g2h(target_brk), 0, new_brk - target_brk);
785 }
bellard31e31b82003-02-18 22:55:36 +0000786 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000787 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000788 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000789 }
790
Peter Maydell00faf082011-04-18 16:34:24 +0100791 /* We need to allocate more memory after the brk... Note that
792 * we don't use MAP_FIXED because that will map over the top of
793 * any existing mapping (like the one with the host libc or qemu
794 * itself); instead we treat "mapped but at wrong address" as
795 * a failure and unmap again.
796 */
vincent4d1de872011-06-14 21:56:33 +0000797 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000798 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000799 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100800 MAP_ANON|MAP_PRIVATE, 0, 0));
801
802 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200803 /* Heap contents are initialized to zero, as for anonymous
804 * mapped pages. Technically the new pages are already
805 * initialized to zero since they *are* anonymous mapped
806 * pages, however we have to take care with the contents that
807 * come from the remaining part of the previous page: it may
808 * contains garbage data due to a previous heap usage (grown
809 * then shrunken). */
810 memset(g2h(target_brk), 0, brk_page - target_brk);
811
Peter Maydell00faf082011-04-18 16:34:24 +0100812 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000813 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000814 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
815 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100816 return target_brk;
817 } else if (mapped_addr != -1) {
818 /* Mapped but at wrong address, meaning there wasn't actually
819 * enough space for this brk.
820 */
821 target_munmap(mapped_addr, new_alloc_size);
822 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000823 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000824 }
825 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000826 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100827 }
balrog7ab240a2008-04-26 12:17:34 +0000828
Richard Henderson7dd46c02010-05-03 10:07:49 -0700829#if defined(TARGET_ALPHA)
830 /* We (partially) emulate OSF/1 on Alpha, which requires we
831 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100832 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700833#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100834 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000835 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000836}
837
ths26edcf42007-12-09 02:25:24 +0000838static inline abi_long copy_from_user_fdset(fd_set *fds,
839 abi_ulong target_fds_addr,
840 int n)
bellard31e31b82003-02-18 22:55:36 +0000841{
ths26edcf42007-12-09 02:25:24 +0000842 int i, nw, j, k;
843 abi_ulong b, *target_fds;
844
845 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
846 if (!(target_fds = lock_user(VERIFY_READ,
847 target_fds_addr,
848 sizeof(abi_ulong) * nw,
849 1)))
850 return -TARGET_EFAULT;
851
852 FD_ZERO(fds);
853 k = 0;
854 for (i = 0; i < nw; i++) {
855 /* grab the abi_ulong */
856 __get_user(b, &target_fds[i]);
857 for (j = 0; j < TARGET_ABI_BITS; j++) {
858 /* check the bit inside the abi_ulong */
859 if ((b >> j) & 1)
860 FD_SET(k, fds);
861 k++;
bellard31e31b82003-02-18 22:55:36 +0000862 }
bellard31e31b82003-02-18 22:55:36 +0000863 }
ths26edcf42007-12-09 02:25:24 +0000864
865 unlock_user(target_fds, target_fds_addr, 0);
866
867 return 0;
bellard31e31b82003-02-18 22:55:36 +0000868}
869
Mike Frysinger055e0902011-06-03 17:01:49 -0400870static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
871 abi_ulong target_fds_addr,
872 int n)
873{
874 if (target_fds_addr) {
875 if (copy_from_user_fdset(fds, target_fds_addr, n))
876 return -TARGET_EFAULT;
877 *fds_ptr = fds;
878 } else {
879 *fds_ptr = NULL;
880 }
881 return 0;
882}
883
ths26edcf42007-12-09 02:25:24 +0000884static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
885 const fd_set *fds,
886 int n)
bellard31e31b82003-02-18 22:55:36 +0000887{
bellard31e31b82003-02-18 22:55:36 +0000888 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000889 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000890 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000891
ths26edcf42007-12-09 02:25:24 +0000892 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
893 if (!(target_fds = lock_user(VERIFY_WRITE,
894 target_fds_addr,
895 sizeof(abi_ulong) * nw,
896 0)))
897 return -TARGET_EFAULT;
898
899 k = 0;
900 for (i = 0; i < nw; i++) {
901 v = 0;
902 for (j = 0; j < TARGET_ABI_BITS; j++) {
Andreas Schwab9ab709b2013-04-09 01:02:34 +0000903 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
ths26edcf42007-12-09 02:25:24 +0000904 k++;
bellard31e31b82003-02-18 22:55:36 +0000905 }
ths26edcf42007-12-09 02:25:24 +0000906 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000907 }
ths26edcf42007-12-09 02:25:24 +0000908
909 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
910
911 return 0;
bellard31e31b82003-02-18 22:55:36 +0000912}
913
bellardc596ed12003-07-13 17:32:31 +0000914#if defined(__alpha__)
915#define HOST_HZ 1024
916#else
917#define HOST_HZ 100
918#endif
919
blueswir1992f48a2007-10-14 16:27:31 +0000920static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000921{
922#if HOST_HZ == TARGET_HZ
923 return ticks;
924#else
925 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
926#endif
927}
928
bellard579a97f2007-11-11 14:26:47 +0000929static inline abi_long host_to_target_rusage(abi_ulong target_addr,
930 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000931{
pbrook53a59602006-03-25 19:31:22 +0000932 struct target_rusage *target_rusage;
933
bellard579a97f2007-11-11 14:26:47 +0000934 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
935 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200936 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
937 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
938 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
939 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
940 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
941 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
942 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
943 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
944 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
945 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
946 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
947 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
948 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
949 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
950 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
951 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
952 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
953 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000954 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000955
956 return 0;
bellardb4091862003-05-16 15:39:34 +0000957}
958
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200959static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900960{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200961 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300962 rlim_t result;
963
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200964 target_rlim_swap = tswapal(target_rlim);
965 if (target_rlim_swap == TARGET_RLIM_INFINITY)
966 return RLIM_INFINITY;
967
968 result = target_rlim_swap;
969 if (target_rlim_swap != (rlim_t)result)
970 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300971
972 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900973}
974
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200975static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900976{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200977 abi_ulong target_rlim_swap;
978 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300979
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200980 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300981 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900982 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300983 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200984 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300985
986 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900987}
988
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +0300989static inline int target_to_host_resource(int code)
990{
991 switch (code) {
992 case TARGET_RLIMIT_AS:
993 return RLIMIT_AS;
994 case TARGET_RLIMIT_CORE:
995 return RLIMIT_CORE;
996 case TARGET_RLIMIT_CPU:
997 return RLIMIT_CPU;
998 case TARGET_RLIMIT_DATA:
999 return RLIMIT_DATA;
1000 case TARGET_RLIMIT_FSIZE:
1001 return RLIMIT_FSIZE;
1002 case TARGET_RLIMIT_LOCKS:
1003 return RLIMIT_LOCKS;
1004 case TARGET_RLIMIT_MEMLOCK:
1005 return RLIMIT_MEMLOCK;
1006 case TARGET_RLIMIT_MSGQUEUE:
1007 return RLIMIT_MSGQUEUE;
1008 case TARGET_RLIMIT_NICE:
1009 return RLIMIT_NICE;
1010 case TARGET_RLIMIT_NOFILE:
1011 return RLIMIT_NOFILE;
1012 case TARGET_RLIMIT_NPROC:
1013 return RLIMIT_NPROC;
1014 case TARGET_RLIMIT_RSS:
1015 return RLIMIT_RSS;
1016 case TARGET_RLIMIT_RTPRIO:
1017 return RLIMIT_RTPRIO;
1018 case TARGET_RLIMIT_SIGPENDING:
1019 return RLIMIT_SIGPENDING;
1020 case TARGET_RLIMIT_STACK:
1021 return RLIMIT_STACK;
1022 default:
1023 return code;
1024 }
1025}
1026
ths788f5ec2007-12-09 02:37:05 +00001027static inline abi_long copy_from_user_timeval(struct timeval *tv,
1028 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001029{
pbrook53a59602006-03-25 19:31:22 +00001030 struct target_timeval *target_tv;
1031
ths788f5ec2007-12-09 02:37:05 +00001032 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +00001033 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001034
1035 __get_user(tv->tv_sec, &target_tv->tv_sec);
1036 __get_user(tv->tv_usec, &target_tv->tv_usec);
1037
1038 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001039
1040 return 0;
bellard31e31b82003-02-18 22:55:36 +00001041}
1042
ths788f5ec2007-12-09 02:37:05 +00001043static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1044 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +00001045{
pbrook53a59602006-03-25 19:31:22 +00001046 struct target_timeval *target_tv;
1047
ths788f5ec2007-12-09 02:37:05 +00001048 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +00001049 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +00001050
1051 __put_user(tv->tv_sec, &target_tv->tv_sec);
1052 __put_user(tv->tv_usec, &target_tv->tv_usec);
1053
1054 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001055
1056 return 0;
bellard31e31b82003-02-18 22:55:36 +00001057}
1058
Paul Burtonef4467e2014-06-22 11:25:40 +01001059static inline abi_long copy_from_user_timezone(struct timezone *tz,
1060 abi_ulong target_tz_addr)
1061{
1062 struct target_timezone *target_tz;
1063
1064 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1065 return -TARGET_EFAULT;
1066 }
1067
1068 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1069 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1070
1071 unlock_user_struct(target_tz, target_tz_addr, 0);
1072
1073 return 0;
1074}
1075
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001076#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1077#include <mqueue.h>
1078
aurel3224e10032009-04-15 16:11:43 +00001079static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1080 abi_ulong target_mq_attr_addr)
1081{
1082 struct target_mq_attr *target_mq_attr;
1083
1084 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1085 target_mq_attr_addr, 1))
1086 return -TARGET_EFAULT;
1087
1088 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1089 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1090 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1091 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1092
1093 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1094
1095 return 0;
1096}
1097
1098static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1099 const struct mq_attr *attr)
1100{
1101 struct target_mq_attr *target_mq_attr;
1102
1103 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1104 target_mq_attr_addr, 0))
1105 return -TARGET_EFAULT;
1106
1107 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1108 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1109 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1110 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1111
1112 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1113
1114 return 0;
1115}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07001116#endif
bellard31e31b82003-02-18 22:55:36 +00001117
Mike Frysinger055e0902011-06-03 17:01:49 -04001118#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +00001119/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001120static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +00001121 abi_ulong rfd_addr, abi_ulong wfd_addr,
1122 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +00001123{
1124 fd_set rfds, wfds, efds;
1125 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
Peter Maydell6df9d382016-05-12 18:47:51 +01001126 struct timeval tv;
1127 struct timespec ts, *ts_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00001128 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001129
Mike Frysinger055e0902011-06-03 17:01:49 -04001130 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1131 if (ret) {
1132 return ret;
pbrook53a59602006-03-25 19:31:22 +00001133 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001134 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1135 if (ret) {
1136 return ret;
pbrook53a59602006-03-25 19:31:22 +00001137 }
Mike Frysinger055e0902011-06-03 17:01:49 -04001138 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1139 if (ret) {
1140 return ret;
pbrook53a59602006-03-25 19:31:22 +00001141 }
ths3b46e622007-09-17 08:09:54 +00001142
ths26edcf42007-12-09 02:25:24 +00001143 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001144 if (copy_from_user_timeval(&tv, target_tv_addr))
1145 return -TARGET_EFAULT;
Peter Maydell6df9d382016-05-12 18:47:51 +01001146 ts.tv_sec = tv.tv_sec;
1147 ts.tv_nsec = tv.tv_usec * 1000;
1148 ts_ptr = &ts;
bellard31e31b82003-02-18 22:55:36 +00001149 } else {
Peter Maydell6df9d382016-05-12 18:47:51 +01001150 ts_ptr = NULL;
bellard31e31b82003-02-18 22:55:36 +00001151 }
ths26edcf42007-12-09 02:25:24 +00001152
Peter Maydell6df9d382016-05-12 18:47:51 +01001153 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1154 ts_ptr, NULL));
pbrook53a59602006-03-25 19:31:22 +00001155
ths26edcf42007-12-09 02:25:24 +00001156 if (!is_error(ret)) {
1157 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1158 return -TARGET_EFAULT;
1159 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1160 return -TARGET_EFAULT;
1161 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1162 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001163
Peter Maydell6df9d382016-05-12 18:47:51 +01001164 if (target_tv_addr) {
1165 tv.tv_sec = ts.tv_sec;
1166 tv.tv_usec = ts.tv_nsec / 1000;
1167 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1168 return -TARGET_EFAULT;
1169 }
1170 }
bellard31e31b82003-02-18 22:55:36 +00001171 }
bellard579a97f2007-11-11 14:26:47 +00001172
bellard31e31b82003-02-18 22:55:36 +00001173 return ret;
1174}
Mike Frysinger055e0902011-06-03 17:01:49 -04001175#endif
bellard31e31b82003-02-18 22:55:36 +00001176
Riku Voipio099d6b02009-05-05 12:10:04 +03001177static abi_long do_pipe2(int host_pipe[], int flags)
1178{
1179#ifdef CONFIG_PIPE2
1180 return pipe2(host_pipe, flags);
1181#else
1182 return -ENOSYS;
1183#endif
1184}
1185
Richard Hendersonfb41a662010-05-03 10:07:52 -07001186static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1187 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001188{
1189 int host_pipe[2];
1190 abi_long ret;
1191 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1192
1193 if (is_error(ret))
1194 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001195
1196 /* Several targets have special calling conventions for the original
1197 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1198 if (!is_pipe2) {
1199#if defined(TARGET_ALPHA)
1200 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1201 return host_pipe[0];
1202#elif defined(TARGET_MIPS)
1203 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1204 return host_pipe[0];
1205#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001206 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001207 return host_pipe[0];
Peter Maydell82f05b62013-07-06 17:39:48 +01001208#elif defined(TARGET_SPARC)
1209 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1210 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001211#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001212 }
1213
Riku Voipio099d6b02009-05-05 12:10:04 +03001214 if (put_user_s32(host_pipe[0], pipedes)
1215 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1216 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001217 return get_errno(ret);
1218}
1219
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001220static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1221 abi_ulong target_addr,
1222 socklen_t len)
1223{
1224 struct target_ip_mreqn *target_smreqn;
1225
1226 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1227 if (!target_smreqn)
1228 return -TARGET_EFAULT;
1229 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1230 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1231 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001232 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001233 unlock_user(target_smreqn, target_addr, 0);
1234
1235 return 0;
1236}
1237
Laurent Vivier7b36f782015-10-28 21:40:44 +01001238static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
bellard579a97f2007-11-11 14:26:47 +00001239 abi_ulong target_addr,
1240 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001241{
aurel32607175e2009-04-15 16:11:59 +00001242 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1243 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001244 struct target_sockaddr *target_saddr;
1245
Laurent Vivier7b36f782015-10-28 21:40:44 +01001246 if (fd_trans_target_to_host_addr(fd)) {
1247 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1248 }
1249
bellard579a97f2007-11-11 14:26:47 +00001250 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1251 if (!target_saddr)
1252 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001253
1254 sa_family = tswap16(target_saddr->sa_family);
1255
1256 /* Oops. The caller might send a incomplete sun_path; sun_path
1257 * must be terminated by \0 (see the manual page), but
1258 * unfortunately it is quite common to specify sockaddr_un
1259 * length as "strlen(x->sun_path)" while it should be
1260 * "strlen(...) + 1". We'll fix that here if needed.
1261 * Linux kernel has a similar feature.
1262 */
1263
1264 if (sa_family == AF_UNIX) {
1265 if (len < unix_maxlen && len > 0) {
1266 char *cp = (char*)target_saddr;
1267
1268 if ( cp[len-1] && !cp[len] )
1269 len++;
1270 }
1271 if (len > unix_maxlen)
1272 len = unix_maxlen;
1273 }
1274
pbrook53a59602006-03-25 19:31:22 +00001275 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001276 addr->sa_family = sa_family;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001277 if (sa_family == AF_NETLINK) {
1278 struct sockaddr_nl *nladdr;
1279
1280 nladdr = (struct sockaddr_nl *)addr;
1281 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1282 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1283 } else if (sa_family == AF_PACKET) {
Joakim Tjernlund33a29b52014-07-12 15:47:07 +02001284 struct target_sockaddr_ll *lladdr;
1285
1286 lladdr = (struct target_sockaddr_ll *)addr;
1287 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1288 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1289 }
pbrook53a59602006-03-25 19:31:22 +00001290 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001291
1292 return 0;
bellard7854b052003-03-29 17:22:23 +00001293}
1294
bellard579a97f2007-11-11 14:26:47 +00001295static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1296 struct sockaddr *addr,
1297 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001298{
pbrook53a59602006-03-25 19:31:22 +00001299 struct target_sockaddr *target_saddr;
1300
bellard579a97f2007-11-11 14:26:47 +00001301 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1302 if (!target_saddr)
1303 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001304 memcpy(target_saddr, addr, len);
1305 target_saddr->sa_family = tswap16(addr->sa_family);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001306 if (addr->sa_family == AF_NETLINK) {
1307 struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
1308 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1309 target_nl->nl_groups = tswap32(target_nl->nl_groups);
1310 }
pbrook53a59602006-03-25 19:31:22 +00001311 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001312
1313 return 0;
bellard7854b052003-03-29 17:22:23 +00001314}
1315
bellard5a4a8982007-11-11 17:39:18 +00001316static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1317 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001318{
1319 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001320 abi_long msg_controllen;
1321 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001322 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001323 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001324
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001325 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001326 if (msg_controllen < sizeof (struct target_cmsghdr))
1327 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001328 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001329 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001330 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001331 if (!target_cmsg)
1332 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001333
1334 while (cmsg && target_cmsg) {
1335 void *data = CMSG_DATA(cmsg);
1336 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1337
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001338 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001339 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1340
1341 space += CMSG_SPACE(len);
1342 if (space > msgh->msg_controllen) {
1343 space -= CMSG_SPACE(len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001344 /* This is a QEMU bug, since we allocated the payload
1345 * area ourselves (unlike overflow in host-to-target
1346 * conversion, which is just the guest giving us a buffer
1347 * that's too small). It can't happen for the payload types
1348 * we currently support; if it becomes an issue in future
1349 * we would need to improve our allocation strategy to
1350 * something more intelligent than "twice the size of the
1351 * target buffer we're reading from".
1352 */
bellard31febb72005-12-18 20:03:27 +00001353 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001354 break;
1355 }
1356
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001357 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1358 cmsg->cmsg_level = SOL_SOCKET;
1359 } else {
1360 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1361 }
bellard7854b052003-03-29 17:22:23 +00001362 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1363 cmsg->cmsg_len = CMSG_LEN(len);
1364
Alex Suykov30b8b682014-12-23 07:52:58 +02001365 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001366 int *fd = (int *)data;
1367 int *target_fd = (int *)target_data;
1368 int i, numfds = len / sizeof(int);
1369
Peter Maydell876e23c2015-05-26 19:46:32 +01001370 for (i = 0; i < numfds; i++) {
1371 __get_user(fd[i], target_fd + i);
1372 }
Alex Suykov30b8b682014-12-23 07:52:58 +02001373 } else if (cmsg->cmsg_level == SOL_SOCKET
1374 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1375 struct ucred *cred = (struct ucred *)data;
1376 struct target_ucred *target_cred =
1377 (struct target_ucred *)target_data;
1378
Peter Maydell876e23c2015-05-26 19:46:32 +01001379 __get_user(cred->pid, &target_cred->pid);
1380 __get_user(cred->uid, &target_cred->uid);
1381 __get_user(cred->gid, &target_cred->gid);
Alex Suykov30b8b682014-12-23 07:52:58 +02001382 } else {
1383 gemu_log("Unsupported ancillary data: %d/%d\n",
1384 cmsg->cmsg_level, cmsg->cmsg_type);
1385 memcpy(data, target_data, len);
bellard7854b052003-03-29 17:22:23 +00001386 }
1387
1388 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001389 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1390 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00001391 }
bellard5a4a8982007-11-11 17:39:18 +00001392 unlock_user(target_cmsg, target_cmsg_addr, 0);
1393 the_end:
bellard7854b052003-03-29 17:22:23 +00001394 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001395 return 0;
bellard7854b052003-03-29 17:22:23 +00001396}
1397
bellard5a4a8982007-11-11 17:39:18 +00001398static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1399 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001400{
1401 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001402 abi_long msg_controllen;
1403 abi_ulong target_cmsg_addr;
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001404 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
bellard7854b052003-03-29 17:22:23 +00001405 socklen_t space = 0;
1406
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001407 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001408 if (msg_controllen < sizeof (struct target_cmsghdr))
1409 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001410 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001411 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001412 target_cmsg_start = target_cmsg;
bellard5a4a8982007-11-11 17:39:18 +00001413 if (!target_cmsg)
1414 return -TARGET_EFAULT;
1415
bellard7854b052003-03-29 17:22:23 +00001416 while (cmsg && target_cmsg) {
1417 void *data = CMSG_DATA(cmsg);
1418 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1419
1420 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
Peter Maydellc2aeb252015-05-26 19:46:31 +01001421 int tgt_len, tgt_space;
bellard7854b052003-03-29 17:22:23 +00001422
Peter Maydellc2aeb252015-05-26 19:46:31 +01001423 /* We never copy a half-header but may copy half-data;
1424 * this is Linux's behaviour in put_cmsg(). Note that
1425 * truncation here is a guest problem (which we report
1426 * to the guest via the CTRUNC bit), unlike truncation
1427 * in target_to_host_cmsg, which is a QEMU bug.
1428 */
1429 if (msg_controllen < sizeof(struct cmsghdr)) {
1430 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
bellard7854b052003-03-29 17:22:23 +00001431 break;
1432 }
1433
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001434 if (cmsg->cmsg_level == SOL_SOCKET) {
1435 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1436 } else {
1437 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1438 }
bellard7854b052003-03-29 17:22:23 +00001439 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
bellard7854b052003-03-29 17:22:23 +00001440
Peter Maydellc2aeb252015-05-26 19:46:31 +01001441 tgt_len = TARGET_CMSG_LEN(len);
1442
1443 /* Payload types which need a different size of payload on
1444 * the target must adjust tgt_len here.
1445 */
1446 switch (cmsg->cmsg_level) {
1447 case SOL_SOCKET:
1448 switch (cmsg->cmsg_type) {
1449 case SO_TIMESTAMP:
1450 tgt_len = sizeof(struct target_timeval);
1451 break;
1452 default:
1453 break;
1454 }
1455 default:
1456 break;
1457 }
1458
1459 if (msg_controllen < tgt_len) {
1460 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1461 tgt_len = msg_controllen;
1462 }
1463
1464 /* We must now copy-and-convert len bytes of payload
1465 * into tgt_len bytes of destination space. Bear in mind
1466 * that in both source and destination we may be dealing
1467 * with a truncated value!
1468 */
Huw Davies52b65492014-04-17 14:02:47 +01001469 switch (cmsg->cmsg_level) {
1470 case SOL_SOCKET:
1471 switch (cmsg->cmsg_type) {
1472 case SCM_RIGHTS:
1473 {
1474 int *fd = (int *)data;
1475 int *target_fd = (int *)target_data;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001476 int i, numfds = tgt_len / sizeof(int);
bellard7854b052003-03-29 17:22:23 +00001477
Peter Maydell876e23c2015-05-26 19:46:32 +01001478 for (i = 0; i < numfds; i++) {
1479 __put_user(fd[i], target_fd + i);
1480 }
Huw Davies52b65492014-04-17 14:02:47 +01001481 break;
1482 }
1483 case SO_TIMESTAMP:
1484 {
1485 struct timeval *tv = (struct timeval *)data;
1486 struct target_timeval *target_tv =
1487 (struct target_timeval *)target_data;
Jing Huangaebf5bc2012-07-24 14:01:42 +00001488
Peter Maydellc2aeb252015-05-26 19:46:31 +01001489 if (len != sizeof(struct timeval) ||
1490 tgt_len != sizeof(struct target_timeval)) {
Huw Davies52b65492014-04-17 14:02:47 +01001491 goto unimplemented;
Peter Maydellc2aeb252015-05-26 19:46:31 +01001492 }
Huw Davies52b65492014-04-17 14:02:47 +01001493
1494 /* copy struct timeval to target */
Peter Maydell876e23c2015-05-26 19:46:32 +01001495 __put_user(tv->tv_sec, &target_tv->tv_sec);
1496 __put_user(tv->tv_usec, &target_tv->tv_usec);
Huw Davies52b65492014-04-17 14:02:47 +01001497 break;
1498 }
Huw Davies4bc29752014-04-17 14:02:48 +01001499 case SCM_CREDENTIALS:
1500 {
1501 struct ucred *cred = (struct ucred *)data;
1502 struct target_ucred *target_cred =
1503 (struct target_ucred *)target_data;
1504
1505 __put_user(cred->pid, &target_cred->pid);
1506 __put_user(cred->uid, &target_cred->uid);
1507 __put_user(cred->gid, &target_cred->gid);
1508 break;
1509 }
Huw Davies52b65492014-04-17 14:02:47 +01001510 default:
1511 goto unimplemented;
1512 }
1513 break;
1514
1515 default:
1516 unimplemented:
Jing Huangaebf5bc2012-07-24 14:01:42 +00001517 gemu_log("Unsupported ancillary data: %d/%d\n",
1518 cmsg->cmsg_level, cmsg->cmsg_type);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001519 memcpy(target_data, data, MIN(len, tgt_len));
1520 if (tgt_len > len) {
1521 memset(target_data + len, 0, tgt_len - len);
1522 }
bellard7854b052003-03-29 17:22:23 +00001523 }
1524
Peter Maydellc2aeb252015-05-26 19:46:31 +01001525 target_cmsg->cmsg_len = tswapal(tgt_len);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001526 tgt_space = TARGET_CMSG_SPACE(len);
Peter Maydellc2aeb252015-05-26 19:46:31 +01001527 if (msg_controllen < tgt_space) {
1528 tgt_space = msg_controllen;
1529 }
1530 msg_controllen -= tgt_space;
1531 space += tgt_space;
bellard7854b052003-03-29 17:22:23 +00001532 cmsg = CMSG_NXTHDR(msgh, cmsg);
Jonathan Neuschäferee104582015-09-03 07:27:26 +02001533 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1534 target_cmsg_start);
bellard7854b052003-03-29 17:22:23 +00001535 }
bellard5a4a8982007-11-11 17:39:18 +00001536 unlock_user(target_cmsg, target_cmsg_addr, space);
1537 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001538 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001539 return 0;
bellard7854b052003-03-29 17:22:23 +00001540}
1541
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001542static void tswap_nlmsghdr(struct nlmsghdr *nlh)
1543{
1544 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
1545 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
1546 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
1547 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
1548 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
1549}
1550
1551static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
1552 size_t len,
1553 abi_long (*host_to_target_nlmsg)
1554 (struct nlmsghdr *))
1555{
1556 uint32_t nlmsg_len;
1557 abi_long ret;
1558
1559 while (len > sizeof(struct nlmsghdr)) {
1560
1561 nlmsg_len = nlh->nlmsg_len;
1562 if (nlmsg_len < sizeof(struct nlmsghdr) ||
1563 nlmsg_len > len) {
1564 break;
1565 }
1566
1567 switch (nlh->nlmsg_type) {
1568 case NLMSG_DONE:
1569 tswap_nlmsghdr(nlh);
1570 return 0;
1571 case NLMSG_NOOP:
1572 break;
1573 case NLMSG_ERROR:
1574 {
1575 struct nlmsgerr *e = NLMSG_DATA(nlh);
1576 e->error = tswap32(e->error);
1577 tswap_nlmsghdr(&e->msg);
1578 tswap_nlmsghdr(nlh);
1579 return 0;
1580 }
1581 default:
1582 ret = host_to_target_nlmsg(nlh);
1583 if (ret < 0) {
1584 tswap_nlmsghdr(nlh);
1585 return ret;
1586 }
1587 break;
1588 }
1589 tswap_nlmsghdr(nlh);
1590 len -= NLMSG_ALIGN(nlmsg_len);
1591 nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len));
1592 }
1593 return 0;
1594}
1595
1596static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
1597 size_t len,
1598 abi_long (*target_to_host_nlmsg)
1599 (struct nlmsghdr *))
1600{
1601 int ret;
1602
1603 while (len > sizeof(struct nlmsghdr)) {
1604 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
1605 tswap32(nlh->nlmsg_len) > len) {
1606 break;
1607 }
1608 tswap_nlmsghdr(nlh);
1609 switch (nlh->nlmsg_type) {
1610 case NLMSG_DONE:
1611 return 0;
1612 case NLMSG_NOOP:
1613 break;
1614 case NLMSG_ERROR:
1615 {
1616 struct nlmsgerr *e = NLMSG_DATA(nlh);
1617 e->error = tswap32(e->error);
1618 tswap_nlmsghdr(&e->msg);
1619 }
1620 default:
1621 ret = target_to_host_nlmsg(nlh);
1622 if (ret < 0) {
1623 return ret;
1624 }
1625 }
1626 len -= NLMSG_ALIGN(nlh->nlmsg_len);
1627 nlh = (struct nlmsghdr *)(((char *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1628 }
1629 return 0;
1630}
1631
Laurent Vivier575b22b2016-06-02 22:14:15 +02001632#ifdef CONFIG_RTNETLINK
Laurent Vivier6c5b5642016-05-22 18:56:19 +02001633static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
1634 size_t len,
1635 abi_long (*host_to_target_rtattr)
1636 (struct rtattr *))
1637{
1638 unsigned short rta_len;
1639 abi_long ret;
1640
1641 while (len > sizeof(struct rtattr)) {
1642 rta_len = rtattr->rta_len;
1643 if (rta_len < sizeof(struct rtattr) ||
1644 rta_len > len) {
1645 break;
1646 }
1647 ret = host_to_target_rtattr(rtattr);
1648 rtattr->rta_len = tswap16(rtattr->rta_len);
1649 rtattr->rta_type = tswap16(rtattr->rta_type);
1650 if (ret < 0) {
1651 return ret;
1652 }
1653 len -= RTA_ALIGN(rta_len);
1654 rtattr = (struct rtattr *)(((char *)rtattr) + RTA_ALIGN(rta_len));
1655 }
1656 return 0;
1657}
1658
1659static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
1660{
1661 uint32_t *u32;
1662 struct rtnl_link_stats *st;
1663 struct rtnl_link_stats64 *st64;
1664 struct rtnl_link_ifmap *map;
1665
1666 switch (rtattr->rta_type) {
1667 /* binary stream */
1668 case IFLA_ADDRESS:
1669 case IFLA_BROADCAST:
1670 /* string */
1671 case IFLA_IFNAME:
1672 case IFLA_QDISC:
1673 break;
1674 /* uin8_t */
1675 case IFLA_OPERSTATE:
1676 case IFLA_LINKMODE:
1677 case IFLA_CARRIER:
1678 case IFLA_PROTO_DOWN:
1679 break;
1680 /* uint32_t */
1681 case IFLA_MTU:
1682 case IFLA_LINK:
1683 case IFLA_WEIGHT:
1684 case IFLA_TXQLEN:
1685 case IFLA_CARRIER_CHANGES:
1686 case IFLA_NUM_RX_QUEUES:
1687 case IFLA_NUM_TX_QUEUES:
1688 case IFLA_PROMISCUITY:
1689 case IFLA_EXT_MASK:
1690 case IFLA_LINK_NETNSID:
1691 case IFLA_GROUP:
1692 case IFLA_MASTER:
1693 case IFLA_NUM_VF:
1694 u32 = RTA_DATA(rtattr);
1695 *u32 = tswap32(*u32);
1696 break;
1697 /* struct rtnl_link_stats */
1698 case IFLA_STATS:
1699 st = RTA_DATA(rtattr);
1700 st->rx_packets = tswap32(st->rx_packets);
1701 st->tx_packets = tswap32(st->tx_packets);
1702 st->rx_bytes = tswap32(st->rx_bytes);
1703 st->tx_bytes = tswap32(st->tx_bytes);
1704 st->rx_errors = tswap32(st->rx_errors);
1705 st->tx_errors = tswap32(st->tx_errors);
1706 st->rx_dropped = tswap32(st->rx_dropped);
1707 st->tx_dropped = tswap32(st->tx_dropped);
1708 st->multicast = tswap32(st->multicast);
1709 st->collisions = tswap32(st->collisions);
1710
1711 /* detailed rx_errors: */
1712 st->rx_length_errors = tswap32(st->rx_length_errors);
1713 st->rx_over_errors = tswap32(st->rx_over_errors);
1714 st->rx_crc_errors = tswap32(st->rx_crc_errors);
1715 st->rx_frame_errors = tswap32(st->rx_frame_errors);
1716 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
1717 st->rx_missed_errors = tswap32(st->rx_missed_errors);
1718
1719 /* detailed tx_errors */
1720 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
1721 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
1722 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
1723 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
1724 st->tx_window_errors = tswap32(st->tx_window_errors);
1725
1726 /* for cslip etc */
1727 st->rx_compressed = tswap32(st->rx_compressed);
1728 st->tx_compressed = tswap32(st->tx_compressed);
1729 break;
1730 /* struct rtnl_link_stats64 */
1731 case IFLA_STATS64:
1732 st64 = RTA_DATA(rtattr);
1733 st64->rx_packets = tswap64(st64->rx_packets);
1734 st64->tx_packets = tswap64(st64->tx_packets);
1735 st64->rx_bytes = tswap64(st64->rx_bytes);
1736 st64->tx_bytes = tswap64(st64->tx_bytes);
1737 st64->rx_errors = tswap64(st64->rx_errors);
1738 st64->tx_errors = tswap64(st64->tx_errors);
1739 st64->rx_dropped = tswap64(st64->rx_dropped);
1740 st64->tx_dropped = tswap64(st64->tx_dropped);
1741 st64->multicast = tswap64(st64->multicast);
1742 st64->collisions = tswap64(st64->collisions);
1743
1744 /* detailed rx_errors: */
1745 st64->rx_length_errors = tswap64(st64->rx_length_errors);
1746 st64->rx_over_errors = tswap64(st64->rx_over_errors);
1747 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
1748 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
1749 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
1750 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
1751
1752 /* detailed tx_errors */
1753 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
1754 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
1755 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
1756 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
1757 st64->tx_window_errors = tswap64(st64->tx_window_errors);
1758
1759 /* for cslip etc */
1760 st64->rx_compressed = tswap64(st64->rx_compressed);
1761 st64->tx_compressed = tswap64(st64->tx_compressed);
1762 break;
1763 /* struct rtnl_link_ifmap */
1764 case IFLA_MAP:
1765 map = RTA_DATA(rtattr);
1766 map->mem_start = tswap64(map->mem_start);
1767 map->mem_end = tswap64(map->mem_end);
1768 map->base_addr = tswap64(map->base_addr);
1769 map->irq = tswap16(map->irq);
1770 break;
1771 /* nested */
1772 case IFLA_AF_SPEC:
1773 case IFLA_LINKINFO:
1774 /* FIXME: implement nested type */
1775 gemu_log("Unimplemented nested type %d\n", rtattr->rta_type);
1776 break;
1777 default:
1778 gemu_log("Unknown host IFLA type: %d\n", rtattr->rta_type);
1779 break;
1780 }
1781 return 0;
1782}
1783
1784static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
1785{
1786 uint32_t *u32;
1787 struct ifa_cacheinfo *ci;
1788
1789 switch (rtattr->rta_type) {
1790 /* binary: depends on family type */
1791 case IFA_ADDRESS:
1792 case IFA_LOCAL:
1793 break;
1794 /* string */
1795 case IFA_LABEL:
1796 break;
1797 /* u32 */
1798 case IFA_FLAGS:
1799 case IFA_BROADCAST:
1800 u32 = RTA_DATA(rtattr);
1801 *u32 = tswap32(*u32);
1802 break;
1803 /* struct ifa_cacheinfo */
1804 case IFA_CACHEINFO:
1805 ci = RTA_DATA(rtattr);
1806 ci->ifa_prefered = tswap32(ci->ifa_prefered);
1807 ci->ifa_valid = tswap32(ci->ifa_valid);
1808 ci->cstamp = tswap32(ci->cstamp);
1809 ci->tstamp = tswap32(ci->tstamp);
1810 break;
1811 default:
1812 gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type);
1813 break;
1814 }
1815 return 0;
1816}
1817
1818static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
1819{
1820 uint32_t *u32;
1821 switch (rtattr->rta_type) {
1822 /* binary: depends on family type */
1823 case RTA_GATEWAY:
1824 case RTA_DST:
1825 case RTA_PREFSRC:
1826 break;
1827 /* u32 */
1828 case RTA_PRIORITY:
1829 case RTA_TABLE:
1830 case RTA_OIF:
1831 u32 = RTA_DATA(rtattr);
1832 *u32 = tswap32(*u32);
1833 break;
1834 default:
1835 gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type);
1836 break;
1837 }
1838 return 0;
1839}
1840
1841static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
1842 uint32_t rtattr_len)
1843{
1844 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1845 host_to_target_data_link_rtattr);
1846}
1847
1848static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
1849 uint32_t rtattr_len)
1850{
1851 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1852 host_to_target_data_addr_rtattr);
1853}
1854
1855static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
1856 uint32_t rtattr_len)
1857{
1858 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1859 host_to_target_data_route_rtattr);
1860}
1861
1862static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
1863{
1864 uint32_t nlmsg_len;
1865 struct ifinfomsg *ifi;
1866 struct ifaddrmsg *ifa;
1867 struct rtmsg *rtm;
1868
1869 nlmsg_len = nlh->nlmsg_len;
1870 switch (nlh->nlmsg_type) {
1871 case RTM_NEWLINK:
1872 case RTM_DELLINK:
1873 case RTM_GETLINK:
1874 ifi = NLMSG_DATA(nlh);
1875 ifi->ifi_type = tswap16(ifi->ifi_type);
1876 ifi->ifi_index = tswap32(ifi->ifi_index);
1877 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1878 ifi->ifi_change = tswap32(ifi->ifi_change);
1879 host_to_target_link_rtattr(IFLA_RTA(ifi),
1880 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
1881 break;
1882 case RTM_NEWADDR:
1883 case RTM_DELADDR:
1884 case RTM_GETADDR:
1885 ifa = NLMSG_DATA(nlh);
1886 ifa->ifa_index = tswap32(ifa->ifa_index);
1887 host_to_target_addr_rtattr(IFA_RTA(ifa),
1888 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
1889 break;
1890 case RTM_NEWROUTE:
1891 case RTM_DELROUTE:
1892 case RTM_GETROUTE:
1893 rtm = NLMSG_DATA(nlh);
1894 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1895 host_to_target_route_rtattr(RTM_RTA(rtm),
1896 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
1897 break;
1898 default:
1899 return -TARGET_EINVAL;
1900 }
1901 return 0;
1902}
1903
1904static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
1905 size_t len)
1906{
1907 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
1908}
1909
1910static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
1911 size_t len,
1912 abi_long (*target_to_host_rtattr)
1913 (struct rtattr *))
1914{
1915 abi_long ret;
1916
1917 while (len >= sizeof(struct rtattr)) {
1918 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
1919 tswap16(rtattr->rta_len) > len) {
1920 break;
1921 }
1922 rtattr->rta_len = tswap16(rtattr->rta_len);
1923 rtattr->rta_type = tswap16(rtattr->rta_type);
1924 ret = target_to_host_rtattr(rtattr);
1925 if (ret < 0) {
1926 return ret;
1927 }
1928 len -= RTA_ALIGN(rtattr->rta_len);
1929 rtattr = (struct rtattr *)(((char *)rtattr) +
1930 RTA_ALIGN(rtattr->rta_len));
1931 }
1932 return 0;
1933}
1934
1935static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
1936{
1937 switch (rtattr->rta_type) {
1938 default:
1939 gemu_log("Unknown target IFLA type: %d\n", rtattr->rta_type);
1940 break;
1941 }
1942 return 0;
1943}
1944
1945static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
1946{
1947 switch (rtattr->rta_type) {
1948 /* binary: depends on family type */
1949 case IFA_LOCAL:
1950 case IFA_ADDRESS:
1951 break;
1952 default:
1953 gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type);
1954 break;
1955 }
1956 return 0;
1957}
1958
1959static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
1960{
1961 uint32_t *u32;
1962 switch (rtattr->rta_type) {
1963 /* binary: depends on family type */
1964 case RTA_DST:
1965 case RTA_SRC:
1966 case RTA_GATEWAY:
1967 break;
1968 /* u32 */
1969 case RTA_OIF:
1970 u32 = RTA_DATA(rtattr);
1971 *u32 = tswap32(*u32);
1972 break;
1973 default:
1974 gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type);
1975 break;
1976 }
1977 return 0;
1978}
1979
1980static void target_to_host_link_rtattr(struct rtattr *rtattr,
1981 uint32_t rtattr_len)
1982{
1983 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1984 target_to_host_data_link_rtattr);
1985}
1986
1987static void target_to_host_addr_rtattr(struct rtattr *rtattr,
1988 uint32_t rtattr_len)
1989{
1990 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1991 target_to_host_data_addr_rtattr);
1992}
1993
1994static void target_to_host_route_rtattr(struct rtattr *rtattr,
1995 uint32_t rtattr_len)
1996{
1997 target_to_host_for_each_rtattr(rtattr, rtattr_len,
1998 target_to_host_data_route_rtattr);
1999}
2000
2001static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
2002{
2003 struct ifinfomsg *ifi;
2004 struct ifaddrmsg *ifa;
2005 struct rtmsg *rtm;
2006
2007 switch (nlh->nlmsg_type) {
2008 case RTM_GETLINK:
2009 break;
2010 case RTM_NEWLINK:
2011 case RTM_DELLINK:
2012 ifi = NLMSG_DATA(nlh);
2013 ifi->ifi_type = tswap16(ifi->ifi_type);
2014 ifi->ifi_index = tswap32(ifi->ifi_index);
2015 ifi->ifi_flags = tswap32(ifi->ifi_flags);
2016 ifi->ifi_change = tswap32(ifi->ifi_change);
2017 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
2018 NLMSG_LENGTH(sizeof(*ifi)));
2019 break;
2020 case RTM_GETADDR:
2021 case RTM_NEWADDR:
2022 case RTM_DELADDR:
2023 ifa = NLMSG_DATA(nlh);
2024 ifa->ifa_index = tswap32(ifa->ifa_index);
2025 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
2026 NLMSG_LENGTH(sizeof(*ifa)));
2027 break;
2028 case RTM_GETROUTE:
2029 break;
2030 case RTM_NEWROUTE:
2031 case RTM_DELROUTE:
2032 rtm = NLMSG_DATA(nlh);
2033 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2034 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
2035 NLMSG_LENGTH(sizeof(*rtm)));
2036 break;
2037 default:
2038 return -TARGET_EOPNOTSUPP;
2039 }
2040 return 0;
2041}
2042
2043static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
2044{
2045 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
2046}
Laurent Vivier575b22b2016-06-02 22:14:15 +02002047#endif /* CONFIG_RTNETLINK */
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002048
Laurent Vivier5ce9bb52016-05-22 18:56:21 +02002049static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
2050{
2051 switch (nlh->nlmsg_type) {
2052 default:
2053 gemu_log("Unknown host audit message type %d\n",
2054 nlh->nlmsg_type);
2055 return -TARGET_EINVAL;
2056 }
2057 return 0;
2058}
2059
2060static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
2061 size_t len)
2062{
2063 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
2064}
2065
2066static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
2067{
2068 switch (nlh->nlmsg_type) {
2069 case AUDIT_USER:
2070 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
2071 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
2072 break;
2073 default:
2074 gemu_log("Unknown target audit message type %d\n",
2075 nlh->nlmsg_type);
2076 return -TARGET_EINVAL;
2077 }
2078
2079 return 0;
2080}
2081
2082static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
2083{
2084 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
2085}
2086
ths0da46a62007-10-20 20:23:07 +00002087/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002088static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00002089 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00002090{
blueswir1992f48a2007-10-14 16:27:31 +00002091 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00002092 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02002093 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02002094 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00002095
bellard8853f862004-02-22 14:57:26 +00002096 switch(level) {
2097 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00002098 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00002099 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00002100 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00002101
bellard2f619692007-11-16 10:46:05 +00002102 if (get_user_u32(val, optval_addr))
2103 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002104 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2105 break;
2106 case SOL_IP:
2107 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00002108 case IP_TOS:
2109 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00002110 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00002111 case IP_ROUTER_ALERT:
2112 case IP_RECVOPTS:
2113 case IP_RETOPTS:
2114 case IP_PKTINFO:
2115 case IP_MTU_DISCOVER:
2116 case IP_RECVERR:
2117 case IP_RECVTOS:
2118#ifdef IP_FREEBIND
2119 case IP_FREEBIND:
2120#endif
2121 case IP_MULTICAST_TTL:
2122 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00002123 val = 0;
2124 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00002125 if (get_user_u32(val, optval_addr))
2126 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002127 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00002128 if (get_user_u8(val, optval_addr))
2129 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002130 }
2131 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2132 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02002133 case IP_ADD_MEMBERSHIP:
2134 case IP_DROP_MEMBERSHIP:
2135 if (optlen < sizeof (struct target_ip_mreq) ||
2136 optlen > sizeof (struct target_ip_mreqn))
2137 return -TARGET_EINVAL;
2138
2139 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2140 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2141 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2142 break;
2143
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02002144 case IP_BLOCK_SOURCE:
2145 case IP_UNBLOCK_SOURCE:
2146 case IP_ADD_SOURCE_MEMBERSHIP:
2147 case IP_DROP_SOURCE_MEMBERSHIP:
2148 if (optlen != sizeof (struct target_ip_mreq_source))
2149 return -TARGET_EINVAL;
2150
2151 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2152 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2153 unlock_user (ip_mreq_source, optval_addr, 0);
2154 break;
2155
bellard8853f862004-02-22 14:57:26 +00002156 default:
2157 goto unimplemented;
2158 }
2159 break;
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02002160 case SOL_IPV6:
2161 switch (optname) {
2162 case IPV6_MTU_DISCOVER:
2163 case IPV6_MTU:
2164 case IPV6_V6ONLY:
2165 case IPV6_RECVPKTINFO:
2166 val = 0;
2167 if (optlen < sizeof(uint32_t)) {
2168 return -TARGET_EINVAL;
2169 }
2170 if (get_user_u32(val, optval_addr)) {
2171 return -TARGET_EFAULT;
2172 }
2173 ret = get_errno(setsockopt(sockfd, level, optname,
2174 &val, sizeof(val)));
2175 break;
2176 default:
2177 goto unimplemented;
2178 }
2179 break;
Jing Huang920394d2012-07-24 13:59:23 +00002180 case SOL_RAW:
2181 switch (optname) {
2182 case ICMP_FILTER:
2183 /* struct icmp_filter takes an u32 value */
2184 if (optlen < sizeof(uint32_t)) {
2185 return -TARGET_EINVAL;
2186 }
2187
2188 if (get_user_u32(val, optval_addr)) {
2189 return -TARGET_EFAULT;
2190 }
2191 ret = get_errno(setsockopt(sockfd, level, optname,
2192 &val, sizeof(val)));
2193 break;
2194
2195 default:
2196 goto unimplemented;
2197 }
2198 break;
bellard3532fa72006-06-24 15:06:03 +00002199 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00002200 switch (optname) {
Laurent Vivier1b09aeb2013-01-01 08:24:11 +00002201 case TARGET_SO_RCVTIMEO:
2202 {
2203 struct timeval tv;
2204
2205 optname = SO_RCVTIMEO;
2206
2207set_timeout:
2208 if (optlen != sizeof(struct target_timeval)) {
2209 return -TARGET_EINVAL;
2210 }
2211
2212 if (copy_from_user_timeval(&tv, optval_addr)) {
2213 return -TARGET_EFAULT;
2214 }
2215
2216 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2217 &tv, sizeof(tv)));
2218 return ret;
2219 }
2220 case TARGET_SO_SNDTIMEO:
2221 optname = SO_SNDTIMEO;
2222 goto set_timeout;
Laurent Vivierf57d4192013-08-30 01:46:41 +02002223 case TARGET_SO_ATTACH_FILTER:
2224 {
2225 struct target_sock_fprog *tfprog;
2226 struct target_sock_filter *tfilter;
2227 struct sock_fprog fprog;
2228 struct sock_filter *filter;
2229 int i;
2230
2231 if (optlen != sizeof(*tfprog)) {
2232 return -TARGET_EINVAL;
2233 }
2234 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2235 return -TARGET_EFAULT;
2236 }
2237 if (!lock_user_struct(VERIFY_READ, tfilter,
2238 tswapal(tfprog->filter), 0)) {
2239 unlock_user_struct(tfprog, optval_addr, 1);
2240 return -TARGET_EFAULT;
2241 }
2242
2243 fprog.len = tswap16(tfprog->len);
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302244 filter = g_try_new(struct sock_filter, fprog.len);
Laurent Vivierf57d4192013-08-30 01:46:41 +02002245 if (filter == NULL) {
2246 unlock_user_struct(tfilter, tfprog->filter, 1);
2247 unlock_user_struct(tfprog, optval_addr, 1);
2248 return -TARGET_ENOMEM;
2249 }
2250 for (i = 0; i < fprog.len; i++) {
2251 filter[i].code = tswap16(tfilter[i].code);
2252 filter[i].jt = tfilter[i].jt;
2253 filter[i].jf = tfilter[i].jf;
2254 filter[i].k = tswap32(tfilter[i].k);
2255 }
2256 fprog.filter = filter;
2257
2258 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2259 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302260 g_free(filter);
Laurent Vivierf57d4192013-08-30 01:46:41 +02002261
2262 unlock_user_struct(tfilter, tfprog->filter, 1);
2263 unlock_user_struct(tfprog, optval_addr, 1);
2264 return ret;
2265 }
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02002266 case TARGET_SO_BINDTODEVICE:
2267 {
2268 char *dev_ifname, *addr_ifname;
2269
2270 if (optlen > IFNAMSIZ - 1) {
2271 optlen = IFNAMSIZ - 1;
2272 }
2273 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2274 if (!dev_ifname) {
2275 return -TARGET_EFAULT;
2276 }
2277 optname = SO_BINDTODEVICE;
2278 addr_ifname = alloca(IFNAMSIZ);
2279 memcpy(addr_ifname, dev_ifname, optlen);
2280 addr_ifname[optlen] = 0;
Chen Gangfad6c582016-01-11 13:58:50 +08002281 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2282 addr_ifname, optlen));
Joakim Tjernlund451aaf62014-07-12 15:47:06 +02002283 unlock_user (dev_ifname, optval_addr, 0);
2284 return ret;
2285 }
bellard8853f862004-02-22 14:57:26 +00002286 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00002287 case TARGET_SO_DEBUG:
2288 optname = SO_DEBUG;
2289 break;
2290 case TARGET_SO_REUSEADDR:
2291 optname = SO_REUSEADDR;
2292 break;
2293 case TARGET_SO_TYPE:
2294 optname = SO_TYPE;
2295 break;
2296 case TARGET_SO_ERROR:
2297 optname = SO_ERROR;
2298 break;
2299 case TARGET_SO_DONTROUTE:
2300 optname = SO_DONTROUTE;
2301 break;
2302 case TARGET_SO_BROADCAST:
2303 optname = SO_BROADCAST;
2304 break;
2305 case TARGET_SO_SNDBUF:
2306 optname = SO_SNDBUF;
2307 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01002308 case TARGET_SO_SNDBUFFORCE:
2309 optname = SO_SNDBUFFORCE;
2310 break;
bellard3532fa72006-06-24 15:06:03 +00002311 case TARGET_SO_RCVBUF:
2312 optname = SO_RCVBUF;
2313 break;
Paul Burtond79b6cc2014-06-22 11:25:35 +01002314 case TARGET_SO_RCVBUFFORCE:
2315 optname = SO_RCVBUFFORCE;
2316 break;
bellard3532fa72006-06-24 15:06:03 +00002317 case TARGET_SO_KEEPALIVE:
2318 optname = SO_KEEPALIVE;
2319 break;
2320 case TARGET_SO_OOBINLINE:
2321 optname = SO_OOBINLINE;
2322 break;
2323 case TARGET_SO_NO_CHECK:
2324 optname = SO_NO_CHECK;
2325 break;
2326 case TARGET_SO_PRIORITY:
2327 optname = SO_PRIORITY;
2328 break;
bellard5e83e8e2005-03-01 22:32:06 +00002329#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00002330 case TARGET_SO_BSDCOMPAT:
2331 optname = SO_BSDCOMPAT;
2332 break;
bellard5e83e8e2005-03-01 22:32:06 +00002333#endif
bellard3532fa72006-06-24 15:06:03 +00002334 case TARGET_SO_PASSCRED:
2335 optname = SO_PASSCRED;
2336 break;
Paul Burton82d0fe62014-06-22 11:25:36 +01002337 case TARGET_SO_PASSSEC:
2338 optname = SO_PASSSEC;
2339 break;
bellard3532fa72006-06-24 15:06:03 +00002340 case TARGET_SO_TIMESTAMP:
2341 optname = SO_TIMESTAMP;
2342 break;
2343 case TARGET_SO_RCVLOWAT:
2344 optname = SO_RCVLOWAT;
2345 break;
bellard8853f862004-02-22 14:57:26 +00002346 break;
2347 default:
2348 goto unimplemented;
2349 }
bellard3532fa72006-06-24 15:06:03 +00002350 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00002351 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00002352
bellard2f619692007-11-16 10:46:05 +00002353 if (get_user_u32(val, optval_addr))
2354 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002355 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00002356 break;
bellard7854b052003-03-29 17:22:23 +00002357 default:
bellard8853f862004-02-22 14:57:26 +00002358 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02002359 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00002360 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00002361 }
bellard8853f862004-02-22 14:57:26 +00002362 return ret;
bellard7854b052003-03-29 17:22:23 +00002363}
2364
ths0da46a62007-10-20 20:23:07 +00002365/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002366static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00002367 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00002368{
blueswir1992f48a2007-10-14 16:27:31 +00002369 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00002370 int len, val;
2371 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00002372
2373 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00002374 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002375 level = SOL_SOCKET;
2376 switch (optname) {
2377 /* These don't just return a single integer */
2378 case TARGET_SO_LINGER:
2379 case TARGET_SO_RCVTIMEO:
2380 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002381 case TARGET_SO_PEERNAME:
2382 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02002383 case TARGET_SO_PEERCRED: {
2384 struct ucred cr;
2385 socklen_t crlen;
2386 struct target_ucred *tcr;
2387
2388 if (get_user_u32(len, optlen)) {
2389 return -TARGET_EFAULT;
2390 }
2391 if (len < 0) {
2392 return -TARGET_EINVAL;
2393 }
2394
2395 crlen = sizeof(cr);
2396 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2397 &cr, &crlen));
2398 if (ret < 0) {
2399 return ret;
2400 }
2401 if (len > crlen) {
2402 len = crlen;
2403 }
2404 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2405 return -TARGET_EFAULT;
2406 }
2407 __put_user(cr.pid, &tcr->pid);
2408 __put_user(cr.uid, &tcr->uid);
2409 __put_user(cr.gid, &tcr->gid);
2410 unlock_user_struct(tcr, optval_addr, 1);
2411 if (put_user_u32(len, optlen)) {
2412 return -TARGET_EFAULT;
2413 }
2414 break;
2415 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002416 /* Options with 'int' argument. */
2417 case TARGET_SO_DEBUG:
2418 optname = SO_DEBUG;
2419 goto int_case;
2420 case TARGET_SO_REUSEADDR:
2421 optname = SO_REUSEADDR;
2422 goto int_case;
2423 case TARGET_SO_TYPE:
2424 optname = SO_TYPE;
2425 goto int_case;
2426 case TARGET_SO_ERROR:
2427 optname = SO_ERROR;
2428 goto int_case;
2429 case TARGET_SO_DONTROUTE:
2430 optname = SO_DONTROUTE;
2431 goto int_case;
2432 case TARGET_SO_BROADCAST:
2433 optname = SO_BROADCAST;
2434 goto int_case;
2435 case TARGET_SO_SNDBUF:
2436 optname = SO_SNDBUF;
2437 goto int_case;
2438 case TARGET_SO_RCVBUF:
2439 optname = SO_RCVBUF;
2440 goto int_case;
2441 case TARGET_SO_KEEPALIVE:
2442 optname = SO_KEEPALIVE;
2443 goto int_case;
2444 case TARGET_SO_OOBINLINE:
2445 optname = SO_OOBINLINE;
2446 goto int_case;
2447 case TARGET_SO_NO_CHECK:
2448 optname = SO_NO_CHECK;
2449 goto int_case;
2450 case TARGET_SO_PRIORITY:
2451 optname = SO_PRIORITY;
2452 goto int_case;
2453#ifdef SO_BSDCOMPAT
2454 case TARGET_SO_BSDCOMPAT:
2455 optname = SO_BSDCOMPAT;
2456 goto int_case;
2457#endif
2458 case TARGET_SO_PASSCRED:
2459 optname = SO_PASSCRED;
2460 goto int_case;
2461 case TARGET_SO_TIMESTAMP:
2462 optname = SO_TIMESTAMP;
2463 goto int_case;
2464 case TARGET_SO_RCVLOWAT:
2465 optname = SO_RCVLOWAT;
2466 goto int_case;
Paul Burtonaec1ca42014-06-22 11:25:34 +01002467 case TARGET_SO_ACCEPTCONN:
2468 optname = SO_ACCEPTCONN;
2469 goto int_case;
bellard8853f862004-02-22 14:57:26 +00002470 default:
bellard2efbe912005-07-23 15:10:20 +00002471 goto int_case;
2472 }
2473 break;
2474 case SOL_TCP:
2475 /* TCP options all take an 'int' value. */
2476 int_case:
bellard2f619692007-11-16 10:46:05 +00002477 if (get_user_u32(len, optlen))
2478 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002479 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00002480 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05002481 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00002482 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2483 if (ret < 0)
2484 return ret;
Paul Burton8289d112014-06-22 11:25:33 +01002485 if (optname == SO_TYPE) {
2486 val = host_to_target_sock_type(val);
2487 }
bellard2efbe912005-07-23 15:10:20 +00002488 if (len > lv)
2489 len = lv;
bellard2f619692007-11-16 10:46:05 +00002490 if (len == 4) {
2491 if (put_user_u32(val, optval_addr))
2492 return -TARGET_EFAULT;
2493 } else {
2494 if (put_user_u8(val, optval_addr))
2495 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02002496 }
bellard2f619692007-11-16 10:46:05 +00002497 if (put_user_u32(len, optlen))
2498 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002499 break;
2500 case SOL_IP:
2501 switch(optname) {
2502 case IP_TOS:
2503 case IP_TTL:
2504 case IP_HDRINCL:
2505 case IP_ROUTER_ALERT:
2506 case IP_RECVOPTS:
2507 case IP_RETOPTS:
2508 case IP_PKTINFO:
2509 case IP_MTU_DISCOVER:
2510 case IP_RECVERR:
2511 case IP_RECVTOS:
2512#ifdef IP_FREEBIND
2513 case IP_FREEBIND:
2514#endif
2515 case IP_MULTICAST_TTL:
2516 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00002517 if (get_user_u32(len, optlen))
2518 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00002519 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00002520 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05002521 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00002522 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2523 if (ret < 0)
2524 return ret;
bellard2efbe912005-07-23 15:10:20 +00002525 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00002526 len = 1;
bellard2f619692007-11-16 10:46:05 +00002527 if (put_user_u32(len, optlen)
2528 || put_user_u8(val, optval_addr))
2529 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002530 } else {
bellard2efbe912005-07-23 15:10:20 +00002531 if (len > sizeof(int))
2532 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00002533 if (put_user_u32(len, optlen)
2534 || put_user_u32(val, optval_addr))
2535 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00002536 }
bellard8853f862004-02-22 14:57:26 +00002537 break;
bellard2efbe912005-07-23 15:10:20 +00002538 default:
thsc02f4992007-12-18 02:39:59 +00002539 ret = -TARGET_ENOPROTOOPT;
2540 break;
bellard8853f862004-02-22 14:57:26 +00002541 }
2542 break;
2543 default:
2544 unimplemented:
2545 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
2546 level, optname);
thsc02f4992007-12-18 02:39:59 +00002547 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00002548 break;
2549 }
2550 return ret;
bellard7854b052003-03-29 17:22:23 +00002551}
2552
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002553static struct iovec *lock_iovec(int type, abi_ulong target_addr,
2554 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00002555{
2556 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002557 struct iovec *vec;
2558 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00002559 int i;
Peter Maydell501bb4b2014-02-17 18:55:33 +00002560 int err = 0;
Tom Musta29560a62014-08-12 13:53:43 -05002561 bool bad_address = false;
pbrook53a59602006-03-25 19:31:22 +00002562
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002563 if (count == 0) {
2564 errno = 0;
2565 return NULL;
pbrook53a59602006-03-25 19:31:22 +00002566 }
Peter Maydelldfae8e02013-02-08 07:58:41 +00002567 if (count < 0 || count > IOV_MAX) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002568 errno = EINVAL;
2569 return NULL;
2570 }
2571
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302572 vec = g_try_new0(struct iovec, count);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002573 if (vec == NULL) {
2574 errno = ENOMEM;
2575 return NULL;
2576 }
2577
2578 target_vec = lock_user(VERIFY_READ, target_addr,
2579 count * sizeof(struct target_iovec), 1);
2580 if (target_vec == NULL) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00002581 err = EFAULT;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002582 goto fail2;
2583 }
2584
2585 /* ??? If host page size > target page size, this will result in a
2586 value larger than what we can actually support. */
2587 max_len = 0x7fffffff & TARGET_PAGE_MASK;
2588 total_len = 0;
2589
2590 for (i = 0; i < count; i++) {
2591 abi_ulong base = tswapal(target_vec[i].iov_base);
2592 abi_long len = tswapal(target_vec[i].iov_len);
2593
2594 if (len < 0) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00002595 err = EINVAL;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002596 goto fail;
2597 } else if (len == 0) {
2598 /* Zero length pointer is ignored. */
2599 vec[i].iov_base = 0;
2600 } else {
2601 vec[i].iov_base = lock_user(type, base, len, copy);
Tom Musta29560a62014-08-12 13:53:43 -05002602 /* If the first buffer pointer is bad, this is a fault. But
2603 * subsequent bad buffers will result in a partial write; this
2604 * is realized by filling the vector with null pointers and
2605 * zero lengths. */
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002606 if (!vec[i].iov_base) {
Tom Musta29560a62014-08-12 13:53:43 -05002607 if (i == 0) {
2608 err = EFAULT;
2609 goto fail;
2610 } else {
2611 bad_address = true;
2612 }
2613 }
2614 if (bad_address) {
2615 len = 0;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002616 }
2617 if (len > max_len - total_len) {
2618 len = max_len - total_len;
2619 }
2620 }
2621 vec[i].iov_len = len;
2622 total_len += len;
2623 }
2624
2625 unlock_user(target_vec, target_addr, 0);
2626 return vec;
2627
2628 fail:
Chen Gang S7eff5182015-01-23 18:01:09 +08002629 while (--i >= 0) {
2630 if (tswapal(target_vec[i].iov_len) > 0) {
2631 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
2632 }
2633 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002634 unlock_user(target_vec, target_addr, 0);
Peter Maydell501bb4b2014-02-17 18:55:33 +00002635 fail2:
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302636 g_free(vec);
Peter Maydell501bb4b2014-02-17 18:55:33 +00002637 errno = err;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002638 return NULL;
pbrook53a59602006-03-25 19:31:22 +00002639}
2640
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002641static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
2642 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00002643{
2644 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00002645 int i;
2646
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002647 target_vec = lock_user(VERIFY_READ, target_addr,
2648 count * sizeof(struct target_iovec), 1);
2649 if (target_vec) {
2650 for (i = 0; i < count; i++) {
2651 abi_ulong base = tswapal(target_vec[i].iov_base);
Chen Gang S71ec7ce2015-01-23 18:07:50 +08002652 abi_long len = tswapal(target_vec[i].iov_len);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002653 if (len < 0) {
2654 break;
2655 }
balrogd732dcb2008-10-28 10:21:03 +00002656 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
2657 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002658 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00002659 }
bellard579a97f2007-11-11 14:26:47 +00002660
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05302661 g_free(vec);
pbrook53a59602006-03-25 19:31:22 +00002662}
2663
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002664static inline int target_to_host_sock_type(int *type)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002665{
2666 int host_type = 0;
2667 int target_type = *type;
2668
2669 switch (target_type & TARGET_SOCK_TYPE_MASK) {
2670 case TARGET_SOCK_DGRAM:
2671 host_type = SOCK_DGRAM;
2672 break;
2673 case TARGET_SOCK_STREAM:
2674 host_type = SOCK_STREAM;
2675 break;
2676 default:
2677 host_type = target_type & TARGET_SOCK_TYPE_MASK;
2678 break;
2679 }
2680 if (target_type & TARGET_SOCK_CLOEXEC) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002681#if defined(SOCK_CLOEXEC)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002682 host_type |= SOCK_CLOEXEC;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002683#else
2684 return -TARGET_EINVAL;
2685#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002686 }
2687 if (target_type & TARGET_SOCK_NONBLOCK) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002688#if defined(SOCK_NONBLOCK)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002689 host_type |= SOCK_NONBLOCK;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002690#elif !defined(O_NONBLOCK)
2691 return -TARGET_EINVAL;
2692#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002693 }
2694 *type = host_type;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002695 return 0;
2696}
2697
2698/* Try to emulate socket type flags after socket creation. */
2699static int sock_flags_fixup(int fd, int target_type)
2700{
2701#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2702 if (target_type & TARGET_SOCK_NONBLOCK) {
2703 int flags = fcntl(fd, F_GETFL);
2704 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
2705 close(fd);
2706 return -TARGET_EINVAL;
2707 }
2708 }
2709#endif
2710 return fd;
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002711}
2712
Laurent Vivier0cf22722015-10-28 21:40:45 +01002713static abi_long packet_target_to_host_sockaddr(void *host_addr,
2714 abi_ulong target_addr,
2715 socklen_t len)
2716{
2717 struct sockaddr *addr = host_addr;
2718 struct target_sockaddr *target_saddr;
2719
2720 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
2721 if (!target_saddr) {
2722 return -TARGET_EFAULT;
2723 }
2724
2725 memcpy(addr, target_saddr, len);
2726 addr->sa_family = tswap16(target_saddr->sa_family);
2727 /* spkt_protocol is big-endian */
2728
2729 unlock_user(target_saddr, target_addr, 0);
2730 return 0;
2731}
2732
2733static TargetFdTrans target_packet_trans = {
2734 .target_to_host_addr = packet_target_to_host_sockaddr,
2735};
2736
Laurent Vivier575b22b2016-06-02 22:14:15 +02002737#ifdef CONFIG_RTNETLINK
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002738static abi_long netlink_route_target_to_host(void *buf, size_t len)
2739{
2740 return target_to_host_nlmsg_route(buf, len);
2741}
2742
2743static abi_long netlink_route_host_to_target(void *buf, size_t len)
2744{
2745 return host_to_target_nlmsg_route(buf, len);
2746}
2747
2748static TargetFdTrans target_netlink_route_trans = {
2749 .target_to_host_data = netlink_route_target_to_host,
2750 .host_to_target_data = netlink_route_host_to_target,
2751};
Laurent Vivier575b22b2016-06-02 22:14:15 +02002752#endif /* CONFIG_RTNETLINK */
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002753
Laurent Vivier5ce9bb52016-05-22 18:56:21 +02002754static abi_long netlink_audit_target_to_host(void *buf, size_t len)
2755{
2756 return target_to_host_nlmsg_audit(buf, len);
2757}
2758
2759static abi_long netlink_audit_host_to_target(void *buf, size_t len)
2760{
2761 return host_to_target_nlmsg_audit(buf, len);
2762}
2763
2764static TargetFdTrans target_netlink_audit_trans = {
2765 .target_to_host_data = netlink_audit_target_to_host,
2766 .host_to_target_data = netlink_audit_host_to_target,
2767};
2768
ths0da46a62007-10-20 20:23:07 +00002769/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002770static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00002771{
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002772 int target_type = type;
2773 int ret;
2774
2775 ret = target_to_host_sock_type(&type);
2776 if (ret) {
2777 return ret;
2778 }
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002779
Laurent Vivier575b22b2016-06-02 22:14:15 +02002780 if (domain == PF_NETLINK && !(
2781#ifdef CONFIG_RTNETLINK
2782 protocol == NETLINK_ROUTE ||
2783#endif
2784 protocol == NETLINK_KOBJECT_UEVENT ||
2785 protocol == NETLINK_AUDIT)) {
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002786 return -EPFNOSUPPORT;
2787 }
Laurent Vivierff626f22015-10-28 21:40:42 +01002788
2789 if (domain == AF_PACKET ||
2790 (domain == AF_INET && type == SOCK_PACKET)) {
2791 protocol = tswap16(protocol);
2792 }
2793
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002794 ret = get_errno(socket(domain, type, protocol));
2795 if (ret >= 0) {
2796 ret = sock_flags_fixup(ret, target_type);
Laurent Vivier0cf22722015-10-28 21:40:45 +01002797 if (type == SOCK_PACKET) {
2798 /* Manage an obsolete case :
2799 * if socket type is SOCK_PACKET, bind by name
2800 */
2801 fd_trans_register(ret, &target_packet_trans);
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002802 } else if (domain == PF_NETLINK) {
2803 switch (protocol) {
Laurent Vivier575b22b2016-06-02 22:14:15 +02002804#ifdef CONFIG_RTNETLINK
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002805 case NETLINK_ROUTE:
2806 fd_trans_register(ret, &target_netlink_route_trans);
2807 break;
Laurent Vivier575b22b2016-06-02 22:14:15 +02002808#endif
Laurent Vivierb2656202016-05-22 18:56:20 +02002809 case NETLINK_KOBJECT_UEVENT:
2810 /* nothing to do: messages are strings */
2811 break;
Laurent Vivier5ce9bb52016-05-22 18:56:21 +02002812 case NETLINK_AUDIT:
2813 fd_trans_register(ret, &target_netlink_audit_trans);
2814 break;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002815 default:
2816 g_assert_not_reached();
2817 }
Laurent Vivier0cf22722015-10-28 21:40:45 +01002818 }
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02002819 }
2820 return ret;
bellard3532fa72006-06-24 15:06:03 +00002821}
2822
ths0da46a62007-10-20 20:23:07 +00002823/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002824static abi_long do_bind(int sockfd, abi_ulong target_addr,
2825 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00002826{
aurel328f7aeaf2009-01-30 19:47:57 +00002827 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03002828 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00002829
Blue Swirl38724252010-09-18 05:53:14 +00002830 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002831 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002832 }
aurel328f7aeaf2009-01-30 19:47:57 +00002833
aurel32607175e2009-04-15 16:11:59 +00002834 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00002835
Laurent Vivier7b36f782015-10-28 21:40:44 +01002836 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002837 if (ret)
2838 return ret;
2839
bellard3532fa72006-06-24 15:06:03 +00002840 return get_errno(bind(sockfd, addr, addrlen));
2841}
2842
ths0da46a62007-10-20 20:23:07 +00002843/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002844static abi_long do_connect(int sockfd, abi_ulong target_addr,
2845 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00002846{
aurel328f7aeaf2009-01-30 19:47:57 +00002847 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03002848 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00002849
Blue Swirl38724252010-09-18 05:53:14 +00002850 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002851 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002852 }
aurel328f7aeaf2009-01-30 19:47:57 +00002853
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02002854 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00002855
Laurent Vivier7b36f782015-10-28 21:40:44 +01002856 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002857 if (ret)
2858 return ret;
2859
bellard3532fa72006-06-24 15:06:03 +00002860 return get_errno(connect(sockfd, addr, addrlen));
2861}
2862
Alexander Graff19e00d2014-03-02 19:36:42 +00002863/* do_sendrecvmsg_locked() Must return target values and target errnos. */
2864static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
2865 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00002866{
balrog6de645c2008-10-28 10:26:29 +00002867 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00002868 struct msghdr msg;
2869 int count;
2870 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00002871 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00002872
bellard3532fa72006-06-24 15:06:03 +00002873 if (msgp->msg_name) {
2874 msg.msg_namelen = tswap32(msgp->msg_namelen);
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02002875 msg.msg_name = alloca(msg.msg_namelen+1);
Laurent Vivier7b36f782015-10-28 21:40:44 +01002876 ret = target_to_host_sockaddr(fd, msg.msg_name,
2877 tswapal(msgp->msg_name),
2878 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002879 if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002880 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03002881 }
bellard3532fa72006-06-24 15:06:03 +00002882 } else {
2883 msg.msg_name = NULL;
2884 msg.msg_namelen = 0;
2885 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002886 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00002887 msg.msg_control = alloca(msg.msg_controllen);
2888 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00002889
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002890 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002891 target_vec = tswapal(msgp->msg_iov);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002892 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2893 target_vec, count, send);
2894 if (vec == NULL) {
2895 ret = -host_to_target_errno(errno);
2896 goto out2;
2897 }
bellard3532fa72006-06-24 15:06:03 +00002898 msg.msg_iovlen = count;
2899 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00002900
bellard3532fa72006-06-24 15:06:03 +00002901 if (send) {
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002902 if (fd_trans_target_to_host_data(fd)) {
2903 ret = fd_trans_target_to_host_data(fd)(msg.msg_iov->iov_base,
2904 msg.msg_iov->iov_len);
2905 } else {
2906 ret = target_to_host_cmsg(&msg, msgp);
2907 }
2908 if (ret == 0) {
bellard5a4a8982007-11-11 17:39:18 +00002909 ret = get_errno(sendmsg(fd, &msg, flags));
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002910 }
bellard3532fa72006-06-24 15:06:03 +00002911 } else {
2912 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00002913 if (!is_error(ret)) {
2914 len = ret;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02002915 if (fd_trans_host_to_target_data(fd)) {
2916 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
2917 msg.msg_iov->iov_len);
2918 } else {
2919 ret = host_to_target_cmsg(msgp, &msg);
2920 }
Jing Huangca619062012-07-24 13:58:02 +00002921 if (!is_error(ret)) {
2922 msgp->msg_namelen = tswap32(msg.msg_namelen);
2923 if (msg.msg_name != NULL) {
2924 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
2925 msg.msg_name, msg.msg_namelen);
2926 if (ret) {
2927 goto out;
2928 }
2929 }
2930
balrog6de645c2008-10-28 10:26:29 +00002931 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00002932 }
balrog6de645c2008-10-28 10:26:29 +00002933 }
bellard3532fa72006-06-24 15:06:03 +00002934 }
Jing Huangca619062012-07-24 13:58:02 +00002935
2936out:
bellard3532fa72006-06-24 15:06:03 +00002937 unlock_iovec(vec, target_vec, count, !send);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07002938out2:
Alexander Graff19e00d2014-03-02 19:36:42 +00002939 return ret;
2940}
2941
2942static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
2943 int flags, int send)
2944{
2945 abi_long ret;
2946 struct target_msghdr *msgp;
2947
2948 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
2949 msgp,
2950 target_msg,
2951 send ? 1 : 0)) {
2952 return -TARGET_EFAULT;
2953 }
2954 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
bellard579a97f2007-11-11 14:26:47 +00002955 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00002956 return ret;
2957}
2958
Alexander Graff19e00d2014-03-02 19:36:42 +00002959/* We don't rely on the C library to have sendmmsg/recvmmsg support,
2960 * so it might not have this *mmsg-specific flag either.
2961 */
2962#ifndef MSG_WAITFORONE
2963#define MSG_WAITFORONE 0x10000
2964#endif
2965
2966static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
2967 unsigned int vlen, unsigned int flags,
2968 int send)
2969{
2970 struct target_mmsghdr *mmsgp;
2971 abi_long ret = 0;
2972 int i;
2973
2974 if (vlen > UIO_MAXIOV) {
2975 vlen = UIO_MAXIOV;
2976 }
2977
2978 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
2979 if (!mmsgp) {
2980 return -TARGET_EFAULT;
2981 }
2982
2983 for (i = 0; i < vlen; i++) {
2984 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
2985 if (is_error(ret)) {
2986 break;
2987 }
2988 mmsgp[i].msg_len = tswap32(ret);
2989 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2990 if (flags & MSG_WAITFORONE) {
2991 flags |= MSG_DONTWAIT;
2992 }
2993 }
2994
2995 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
2996
2997 /* Return number of datagrams sent if we sent any at all;
2998 * otherwise return the error.
2999 */
3000 if (i) {
3001 return i;
3002 }
3003 return ret;
3004}
Alexander Graff19e00d2014-03-02 19:36:42 +00003005
Peter Maydella94b4982013-02-08 04:35:04 +00003006/* If we don't have a system accept4() then just call accept.
3007 * The callsites to do_accept4() will ensure that they don't
3008 * pass a non-zero flags argument in this config.
3009 */
3010#ifndef CONFIG_ACCEPT4
3011static inline int accept4(int sockfd, struct sockaddr *addr,
3012 socklen_t *addrlen, int flags)
3013{
3014 assert(flags == 0);
3015 return accept(sockfd, addr, addrlen);
3016}
3017#endif
3018
3019/* do_accept4() Must return target values and target errnos. */
3020static abi_long do_accept4(int fd, abi_ulong target_addr,
3021 abi_ulong target_addrlen_addr, int flags)
pbrook1be9e1d2006-11-19 15:26:04 +00003022{
bellard2f619692007-11-16 10:46:05 +00003023 socklen_t addrlen;
3024 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003025 abi_long ret;
Petar Jovanovicd25295d2014-03-31 17:41:23 +02003026 int host_flags;
3027
3028 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
pbrook1be9e1d2006-11-19 15:26:04 +00003029
Peter Maydella94b4982013-02-08 04:35:04 +00003030 if (target_addr == 0) {
Petar Jovanovicd25295d2014-03-31 17:41:23 +02003031 return get_errno(accept4(fd, NULL, NULL, host_flags));
Peter Maydella94b4982013-02-08 04:35:04 +00003032 }
Arnaud Patard917507b2009-06-19 10:44:45 +03003033
3034 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00003035 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03003036 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00003037
Blue Swirl38724252010-09-18 05:53:14 +00003038 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003039 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003040 }
aurel328f7aeaf2009-01-30 19:47:57 +00003041
Arnaud Patard917507b2009-06-19 10:44:45 +03003042 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3043 return -TARGET_EINVAL;
3044
bellard2f619692007-11-16 10:46:05 +00003045 addr = alloca(addrlen);
3046
Petar Jovanovicd25295d2014-03-31 17:41:23 +02003047 ret = get_errno(accept4(fd, addr, &addrlen, host_flags));
pbrook1be9e1d2006-11-19 15:26:04 +00003048 if (!is_error(ret)) {
3049 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00003050 if (put_user_u32(addrlen, target_addrlen_addr))
3051 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00003052 }
3053 return ret;
3054}
3055
ths0da46a62007-10-20 20:23:07 +00003056/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003057static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00003058 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003059{
bellard2f619692007-11-16 10:46:05 +00003060 socklen_t addrlen;
3061 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003062 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003063
bellard2f619692007-11-16 10:46:05 +00003064 if (get_user_u32(addrlen, target_addrlen_addr))
3065 return -TARGET_EFAULT;
3066
Blue Swirl38724252010-09-18 05:53:14 +00003067 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003068 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003069 }
aurel328f7aeaf2009-01-30 19:47:57 +00003070
Arnaud Patard917507b2009-06-19 10:44:45 +03003071 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3072 return -TARGET_EFAULT;
3073
bellard2f619692007-11-16 10:46:05 +00003074 addr = alloca(addrlen);
3075
pbrook1be9e1d2006-11-19 15:26:04 +00003076 ret = get_errno(getpeername(fd, addr, &addrlen));
3077 if (!is_error(ret)) {
3078 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00003079 if (put_user_u32(addrlen, target_addrlen_addr))
3080 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00003081 }
3082 return ret;
3083}
3084
ths0da46a62007-10-20 20:23:07 +00003085/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003086static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00003087 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003088{
bellard2f619692007-11-16 10:46:05 +00003089 socklen_t addrlen;
3090 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00003091 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003092
bellard2f619692007-11-16 10:46:05 +00003093 if (get_user_u32(addrlen, target_addrlen_addr))
3094 return -TARGET_EFAULT;
3095
Blue Swirl38724252010-09-18 05:53:14 +00003096 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003097 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003098 }
aurel328f7aeaf2009-01-30 19:47:57 +00003099
Arnaud Patard917507b2009-06-19 10:44:45 +03003100 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3101 return -TARGET_EFAULT;
3102
bellard2f619692007-11-16 10:46:05 +00003103 addr = alloca(addrlen);
3104
pbrook1be9e1d2006-11-19 15:26:04 +00003105 ret = get_errno(getsockname(fd, addr, &addrlen));
3106 if (!is_error(ret)) {
3107 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00003108 if (put_user_u32(addrlen, target_addrlen_addr))
3109 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00003110 }
3111 return ret;
3112}
3113
ths0da46a62007-10-20 20:23:07 +00003114/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003115static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00003116 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00003117{
3118 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00003119 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003120
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02003121 target_to_host_sock_type(&type);
3122
pbrook1be9e1d2006-11-19 15:26:04 +00003123 ret = get_errno(socketpair(domain, type, protocol, tab));
3124 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00003125 if (put_user_s32(tab[0], target_tab_addr)
3126 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3127 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00003128 }
3129 return ret;
3130}
3131
ths0da46a62007-10-20 20:23:07 +00003132/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003133static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3134 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00003135{
3136 void *addr;
3137 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00003138 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003139
Blue Swirl38724252010-09-18 05:53:14 +00003140 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003141 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00003142 }
aurel328f7aeaf2009-01-30 19:47:57 +00003143
bellard579a97f2007-11-11 14:26:47 +00003144 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3145 if (!host_msg)
3146 return -TARGET_EFAULT;
Laurent Vivier6c5b5642016-05-22 18:56:19 +02003147 if (fd_trans_target_to_host_data(fd)) {
3148 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3149 if (ret < 0) {
3150 unlock_user(host_msg, msg, 0);
3151 return ret;
3152 }
3153 }
pbrook1be9e1d2006-11-19 15:26:04 +00003154 if (target_addr) {
Joakim Tjernlund2dd08df2014-07-11 17:18:03 +02003155 addr = alloca(addrlen+1);
Laurent Vivier7b36f782015-10-28 21:40:44 +01003156 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03003157 if (ret) {
3158 unlock_user(host_msg, msg, 0);
3159 return ret;
3160 }
pbrook1be9e1d2006-11-19 15:26:04 +00003161 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
3162 } else {
3163 ret = get_errno(send(fd, host_msg, len, flags));
3164 }
3165 unlock_user(host_msg, msg, 0);
3166 return ret;
3167}
3168
ths0da46a62007-10-20 20:23:07 +00003169/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003170static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3171 abi_ulong target_addr,
3172 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00003173{
3174 socklen_t addrlen;
3175 void *addr;
3176 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00003177 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00003178
bellard579a97f2007-11-11 14:26:47 +00003179 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3180 if (!host_msg)
3181 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00003182 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00003183 if (get_user_u32(addrlen, target_addrlen)) {
3184 ret = -TARGET_EFAULT;
3185 goto fail;
3186 }
Blue Swirl38724252010-09-18 05:53:14 +00003187 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00003188 ret = -TARGET_EINVAL;
3189 goto fail;
3190 }
pbrook1be9e1d2006-11-19 15:26:04 +00003191 addr = alloca(addrlen);
3192 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
3193 } else {
3194 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00003195 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00003196 }
3197 if (!is_error(ret)) {
3198 if (target_addr) {
3199 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00003200 if (put_user_u32(addrlen, target_addrlen)) {
3201 ret = -TARGET_EFAULT;
3202 goto fail;
3203 }
pbrook1be9e1d2006-11-19 15:26:04 +00003204 }
3205 unlock_user(host_msg, msg, len);
3206 } else {
bellard2f619692007-11-16 10:46:05 +00003207fail:
pbrook1be9e1d2006-11-19 15:26:04 +00003208 unlock_user(host_msg, msg, 0);
3209 }
3210 return ret;
3211}
3212
j_mayer32407102007-09-26 23:01:49 +00003213#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00003214/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003215static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00003216{
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003217 static const unsigned ac[] = { /* number of arguments per call */
3218 [SOCKOP_socket] = 3, /* domain, type, protocol */
3219 [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
3220 [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
3221 [SOCKOP_listen] = 2, /* sockfd, backlog */
3222 [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
3223 [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
3224 [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
3225 [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
3226 [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
3227 [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
3228 [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
3229 [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
3230 [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
3231 [SOCKOP_shutdown] = 2, /* sockfd, how */
3232 [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
3233 [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
John Paul Adrian Glaubitz5a53dc52015-12-24 19:59:59 +01003234 [SOCKOP_sendmmsg] = 4, /* sockfd, msgvec, vlen, flags */
3235 [SOCKOP_recvmmsg] = 4, /* sockfd, msgvec, vlen, flags */
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003236 [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */
3237 [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */
3238 };
3239 abi_long a[6]; /* max 6 args */
bellard31e31b82003-02-18 22:55:36 +00003240
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003241 /* first, collect the arguments in a[] according to ac[] */
3242 if (num >= 0 && num < ARRAY_SIZE(ac)) {
3243 unsigned i;
3244 assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */
3245 for (i = 0; i < ac[num]; ++i) {
3246 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
André Hentschelb9d36eb2014-01-06 20:18:52 +01003247 return -TARGET_EFAULT;
3248 }
André Hentschelb9d36eb2014-01-06 20:18:52 +01003249 }
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003250 }
bellard2f619692007-11-16 10:46:05 +00003251
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003252 /* now when we have the args, actually handle the call */
3253 switch (num) {
3254 case SOCKOP_socket: /* domain, type, protocol */
3255 return do_socket(a[0], a[1], a[2]);
3256 case SOCKOP_bind: /* sockfd, addr, addrlen */
3257 return do_bind(a[0], a[1], a[2]);
3258 case SOCKOP_connect: /* sockfd, addr, addrlen */
3259 return do_connect(a[0], a[1], a[2]);
3260 case SOCKOP_listen: /* sockfd, backlog */
3261 return get_errno(listen(a[0], a[1]));
3262 case SOCKOP_accept: /* sockfd, addr, addrlen */
3263 return do_accept4(a[0], a[1], a[2], 0);
3264 case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */
3265 return do_accept4(a[0], a[1], a[2], a[3]);
3266 case SOCKOP_getsockname: /* sockfd, addr, addrlen */
3267 return do_getsockname(a[0], a[1], a[2]);
3268 case SOCKOP_getpeername: /* sockfd, addr, addrlen */
3269 return do_getpeername(a[0], a[1], a[2]);
3270 case SOCKOP_socketpair: /* domain, type, protocol, tab */
3271 return do_socketpair(a[0], a[1], a[2], a[3]);
3272 case SOCKOP_send: /* sockfd, msg, len, flags */
3273 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
3274 case SOCKOP_recv: /* sockfd, msg, len, flags */
3275 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
3276 case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */
3277 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
3278 case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */
3279 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
3280 case SOCKOP_shutdown: /* sockfd, how */
3281 return get_errno(shutdown(a[0], a[1]));
3282 case SOCKOP_sendmsg: /* sockfd, msg, flags */
3283 return do_sendrecvmsg(a[0], a[1], a[2], 1);
3284 case SOCKOP_recvmsg: /* sockfd, msg, flags */
3285 return do_sendrecvmsg(a[0], a[1], a[2], 0);
John Paul Adrian Glaubitz5a53dc52015-12-24 19:59:59 +01003286 case SOCKOP_sendmmsg: /* sockfd, msgvec, vlen, flags */
3287 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
3288 case SOCKOP_recvmmsg: /* sockfd, msgvec, vlen, flags */
3289 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003290 case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */
3291 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
3292 case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */
3293 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
bellard31e31b82003-02-18 22:55:36 +00003294 default:
3295 gemu_log("Unsupported socketcall: %d\n", num);
Michael Tokarev62dc90c2014-01-17 14:23:51 +04003296 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003297 }
bellard31e31b82003-02-18 22:55:36 +00003298}
j_mayer32407102007-09-26 23:01:49 +00003299#endif
bellard31e31b82003-02-18 22:55:36 +00003300
bellard8853f862004-02-22 14:57:26 +00003301#define N_SHM_REGIONS 32
3302
3303static struct shm_region {
Peter Maydellb6e17872016-02-09 15:57:11 +00003304 abi_ulong start;
3305 abi_ulong size;
3306 bool in_use;
bellard8853f862004-02-22 14:57:26 +00003307} shm_regions[N_SHM_REGIONS];
3308
ths3eb6b042007-06-03 14:26:27 +00003309struct target_semid_ds
3310{
3311 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00003312 abi_ulong sem_otime;
Tom Musta03527342014-08-12 13:53:32 -05003313#if !defined(TARGET_PPC64)
blueswir1992f48a2007-10-14 16:27:31 +00003314 abi_ulong __unused1;
Tom Musta03527342014-08-12 13:53:32 -05003315#endif
blueswir1992f48a2007-10-14 16:27:31 +00003316 abi_ulong sem_ctime;
Tom Musta03527342014-08-12 13:53:32 -05003317#if !defined(TARGET_PPC64)
blueswir1992f48a2007-10-14 16:27:31 +00003318 abi_ulong __unused2;
Tom Musta03527342014-08-12 13:53:32 -05003319#endif
blueswir1992f48a2007-10-14 16:27:31 +00003320 abi_ulong sem_nsems;
3321 abi_ulong __unused3;
3322 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00003323};
3324
bellard579a97f2007-11-11 14:26:47 +00003325static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
3326 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003327{
3328 struct target_ipc_perm *target_ip;
3329 struct target_semid_ds *target_sd;
3330
bellard579a97f2007-11-11 14:26:47 +00003331 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3332 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00003333 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003334 host_ip->__key = tswap32(target_ip->__key);
3335 host_ip->uid = tswap32(target_ip->uid);
3336 host_ip->gid = tswap32(target_ip->gid);
3337 host_ip->cuid = tswap32(target_ip->cuid);
3338 host_ip->cgid = tswap32(target_ip->cgid);
3339#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3340 host_ip->mode = tswap32(target_ip->mode);
3341#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003342 host_ip->mode = tswap16(target_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003343#endif
3344#if defined(TARGET_PPC)
3345 host_ip->__seq = tswap32(target_ip->__seq);
3346#else
3347 host_ip->__seq = tswap16(target_ip->__seq);
3348#endif
ths3eb6b042007-06-03 14:26:27 +00003349 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00003350 return 0;
ths3eb6b042007-06-03 14:26:27 +00003351}
3352
bellard579a97f2007-11-11 14:26:47 +00003353static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
3354 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00003355{
3356 struct target_ipc_perm *target_ip;
3357 struct target_semid_ds *target_sd;
3358
bellard579a97f2007-11-11 14:26:47 +00003359 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3360 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00003361 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003362 target_ip->__key = tswap32(host_ip->__key);
3363 target_ip->uid = tswap32(host_ip->uid);
3364 target_ip->gid = tswap32(host_ip->gid);
3365 target_ip->cuid = tswap32(host_ip->cuid);
3366 target_ip->cgid = tswap32(host_ip->cgid);
3367#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3368 target_ip->mode = tswap32(host_ip->mode);
3369#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003370 target_ip->mode = tswap16(host_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01003371#endif
3372#if defined(TARGET_PPC)
3373 target_ip->__seq = tswap32(host_ip->__seq);
3374#else
3375 target_ip->__seq = tswap16(host_ip->__seq);
3376#endif
ths3eb6b042007-06-03 14:26:27 +00003377 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00003378 return 0;
ths3eb6b042007-06-03 14:26:27 +00003379}
3380
bellard579a97f2007-11-11 14:26:47 +00003381static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
3382 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003383{
3384 struct target_semid_ds *target_sd;
3385
bellard579a97f2007-11-11 14:26:47 +00003386 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3387 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00003388 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
3389 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003390 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
3391 host_sd->sem_otime = tswapal(target_sd->sem_otime);
3392 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00003393 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00003394 return 0;
ths3eb6b042007-06-03 14:26:27 +00003395}
3396
bellard579a97f2007-11-11 14:26:47 +00003397static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
3398 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00003399{
3400 struct target_semid_ds *target_sd;
3401
bellard579a97f2007-11-11 14:26:47 +00003402 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3403 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00003404 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08003405 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003406 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
3407 target_sd->sem_otime = tswapal(host_sd->sem_otime);
3408 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00003409 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00003410 return 0;
ths3eb6b042007-06-03 14:26:27 +00003411}
3412
aurel32e5289082009-04-18 16:16:12 +00003413struct target_seminfo {
3414 int semmap;
3415 int semmni;
3416 int semmns;
3417 int semmnu;
3418 int semmsl;
3419 int semopm;
3420 int semume;
3421 int semusz;
3422 int semvmx;
3423 int semaem;
3424};
3425
3426static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
3427 struct seminfo *host_seminfo)
3428{
3429 struct target_seminfo *target_seminfo;
3430 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
3431 return -TARGET_EFAULT;
3432 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
3433 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
3434 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
3435 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
3436 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
3437 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
3438 __put_user(host_seminfo->semume, &target_seminfo->semume);
3439 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
3440 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
3441 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
3442 unlock_user_struct(target_seminfo, target_addr, 1);
3443 return 0;
3444}
3445
thsfa294812007-02-02 22:05:00 +00003446union semun {
3447 int val;
ths3eb6b042007-06-03 14:26:27 +00003448 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00003449 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00003450 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00003451};
3452
ths3eb6b042007-06-03 14:26:27 +00003453union target_semun {
3454 int val;
aurel32e5289082009-04-18 16:16:12 +00003455 abi_ulong buf;
3456 abi_ulong array;
3457 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00003458};
3459
aurel32e5289082009-04-18 16:16:12 +00003460static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3461 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00003462{
aurel32e5289082009-04-18 16:16:12 +00003463 int nsems;
3464 unsigned short *array;
3465 union semun semun;
3466 struct semid_ds semid_ds;
3467 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00003468
aurel32e5289082009-04-18 16:16:12 +00003469 semun.buf = &semid_ds;
3470
3471 ret = semctl(semid, 0, IPC_STAT, semun);
3472 if (ret == -1)
3473 return get_errno(ret);
3474
3475 nsems = semid_ds.sem_nsems;
3476
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303477 *host_array = g_try_new(unsigned short, nsems);
Peter Maydell69d4c702014-02-17 18:55:34 +00003478 if (!*host_array) {
3479 return -TARGET_ENOMEM;
3480 }
aurel32e5289082009-04-18 16:16:12 +00003481 array = lock_user(VERIFY_READ, target_addr,
3482 nsems*sizeof(unsigned short), 1);
Peter Maydell69d4c702014-02-17 18:55:34 +00003483 if (!array) {
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303484 g_free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00003485 return -TARGET_EFAULT;
Peter Maydell69d4c702014-02-17 18:55:34 +00003486 }
aurel32e5289082009-04-18 16:16:12 +00003487
3488 for(i=0; i<nsems; i++) {
3489 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00003490 }
aurel32e5289082009-04-18 16:16:12 +00003491 unlock_user(array, target_addr, 0);
3492
bellard579a97f2007-11-11 14:26:47 +00003493 return 0;
ths3eb6b042007-06-03 14:26:27 +00003494}
3495
aurel32e5289082009-04-18 16:16:12 +00003496static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3497 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00003498{
aurel32e5289082009-04-18 16:16:12 +00003499 int nsems;
3500 unsigned short *array;
3501 union semun semun;
3502 struct semid_ds semid_ds;
3503 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00003504
aurel32e5289082009-04-18 16:16:12 +00003505 semun.buf = &semid_ds;
3506
3507 ret = semctl(semid, 0, IPC_STAT, semun);
3508 if (ret == -1)
3509 return get_errno(ret);
3510
3511 nsems = semid_ds.sem_nsems;
3512
3513 array = lock_user(VERIFY_WRITE, target_addr,
3514 nsems*sizeof(unsigned short), 0);
3515 if (!array)
3516 return -TARGET_EFAULT;
3517
3518 for(i=0; i<nsems; i++) {
3519 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00003520 }
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303521 g_free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00003522 unlock_user(array, target_addr, 1);
3523
bellard579a97f2007-11-11 14:26:47 +00003524 return 0;
ths3eb6b042007-06-03 14:26:27 +00003525}
3526
aurel32e5289082009-04-18 16:16:12 +00003527static inline abi_long do_semctl(int semid, int semnum, int cmd,
Stefan Weild1c002b2015-02-08 15:40:58 +01003528 abi_ulong target_arg)
ths3eb6b042007-06-03 14:26:27 +00003529{
Stefan Weild1c002b2015-02-08 15:40:58 +01003530 union target_semun target_su = { .buf = target_arg };
ths3eb6b042007-06-03 14:26:27 +00003531 union semun arg;
3532 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05303533 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00003534 struct seminfo seminfo;
3535 abi_long ret = -TARGET_EINVAL;
3536 abi_long err;
3537 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00003538
3539 switch( cmd ) {
3540 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00003541 case SETVAL:
Tom Musta5464bae2014-08-12 13:53:34 -05003542 /* In 64 bit cross-endian situations, we will erroneously pick up
3543 * the wrong half of the union for the "val" element. To rectify
3544 * this, the entire 8-byte structure is byteswapped, followed by
3545 * a swap of the 4 byte val field. In other cases, the data is
3546 * already in proper host byte order. */
3547 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
3548 target_su.buf = tswapal(target_su.buf);
3549 arg.val = tswap32(target_su.val);
3550 } else {
3551 arg.val = target_su.val;
3552 }
aurel32e5289082009-04-18 16:16:12 +00003553 ret = get_errno(semctl(semid, semnum, cmd, arg));
ths3eb6b042007-06-03 14:26:27 +00003554 break;
3555 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00003556 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00003557 err = target_to_host_semarray(semid, &array, target_su.array);
3558 if (err)
3559 return err;
3560 arg.array = array;
3561 ret = get_errno(semctl(semid, semnum, cmd, arg));
3562 err = host_to_target_semarray(semid, target_su.array, &array);
3563 if (err)
3564 return err;
ths3eb6b042007-06-03 14:26:27 +00003565 break;
3566 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00003567 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00003568 case SEM_STAT:
3569 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3570 if (err)
3571 return err;
3572 arg.buf = &dsarg;
3573 ret = get_errno(semctl(semid, semnum, cmd, arg));
3574 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3575 if (err)
3576 return err;
ths3eb6b042007-06-03 14:26:27 +00003577 break;
aurel32e5289082009-04-18 16:16:12 +00003578 case IPC_INFO:
3579 case SEM_INFO:
3580 arg.__buf = &seminfo;
3581 ret = get_errno(semctl(semid, semnum, cmd, arg));
3582 err = host_to_target_seminfo(target_su.__buf, &seminfo);
3583 if (err)
3584 return err;
3585 break;
3586 case IPC_RMID:
3587 case GETPID:
3588 case GETNCNT:
3589 case GETZCNT:
3590 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3591 break;
ths3eb6b042007-06-03 14:26:27 +00003592 }
3593
3594 return ret;
3595}
3596
aurel32e5289082009-04-18 16:16:12 +00003597struct target_sembuf {
3598 unsigned short sem_num;
3599 short sem_op;
3600 short sem_flg;
3601};
3602
3603static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
3604 abi_ulong target_addr,
3605 unsigned nsops)
3606{
3607 struct target_sembuf *target_sembuf;
3608 int i;
3609
3610 target_sembuf = lock_user(VERIFY_READ, target_addr,
3611 nsops*sizeof(struct target_sembuf), 1);
3612 if (!target_sembuf)
3613 return -TARGET_EFAULT;
3614
3615 for(i=0; i<nsops; i++) {
3616 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
3617 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
3618 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
3619 }
3620
3621 unlock_user(target_sembuf, target_addr, 0);
3622
3623 return 0;
3624}
3625
3626static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
3627{
3628 struct sembuf sops[nsops];
3629
3630 if (target_to_host_sembuf(sops, ptr, nsops))
3631 return -TARGET_EFAULT;
3632
Petar Jovanovicc7128c92013-03-21 07:57:36 +00003633 return get_errno(semop(semid, sops, nsops));
aurel32e5289082009-04-18 16:16:12 +00003634}
3635
ths1bc012f2007-06-03 14:27:49 +00003636struct target_msqid_ds
3637{
aurel321c54ff92008-10-13 21:08:44 +00003638 struct target_ipc_perm msg_perm;
3639 abi_ulong msg_stime;
3640#if TARGET_ABI_BITS == 32
3641 abi_ulong __unused1;
3642#endif
3643 abi_ulong msg_rtime;
3644#if TARGET_ABI_BITS == 32
3645 abi_ulong __unused2;
3646#endif
3647 abi_ulong msg_ctime;
3648#if TARGET_ABI_BITS == 32
3649 abi_ulong __unused3;
3650#endif
3651 abi_ulong __msg_cbytes;
3652 abi_ulong msg_qnum;
3653 abi_ulong msg_qbytes;
3654 abi_ulong msg_lspid;
3655 abi_ulong msg_lrpid;
3656 abi_ulong __unused4;
3657 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00003658};
3659
bellard579a97f2007-11-11 14:26:47 +00003660static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
3661 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00003662{
3663 struct target_msqid_ds *target_md;
3664
bellard579a97f2007-11-11 14:26:47 +00003665 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
3666 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00003667 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
3668 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003669 host_md->msg_stime = tswapal(target_md->msg_stime);
3670 host_md->msg_rtime = tswapal(target_md->msg_rtime);
3671 host_md->msg_ctime = tswapal(target_md->msg_ctime);
3672 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
3673 host_md->msg_qnum = tswapal(target_md->msg_qnum);
3674 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
3675 host_md->msg_lspid = tswapal(target_md->msg_lspid);
3676 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00003677 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00003678 return 0;
ths1bc012f2007-06-03 14:27:49 +00003679}
3680
bellard579a97f2007-11-11 14:26:47 +00003681static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
3682 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00003683{
3684 struct target_msqid_ds *target_md;
3685
bellard579a97f2007-11-11 14:26:47 +00003686 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
3687 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00003688 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
3689 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003690 target_md->msg_stime = tswapal(host_md->msg_stime);
3691 target_md->msg_rtime = tswapal(host_md->msg_rtime);
3692 target_md->msg_ctime = tswapal(host_md->msg_ctime);
3693 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
3694 target_md->msg_qnum = tswapal(host_md->msg_qnum);
3695 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
3696 target_md->msg_lspid = tswapal(host_md->msg_lspid);
3697 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00003698 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00003699 return 0;
ths1bc012f2007-06-03 14:27:49 +00003700}
3701
aurel321c54ff92008-10-13 21:08:44 +00003702struct target_msginfo {
3703 int msgpool;
3704 int msgmap;
3705 int msgmax;
3706 int msgmnb;
3707 int msgmni;
3708 int msgssz;
3709 int msgtql;
3710 unsigned short int msgseg;
3711};
3712
3713static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
3714 struct msginfo *host_msginfo)
3715{
3716 struct target_msginfo *target_msginfo;
3717 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
3718 return -TARGET_EFAULT;
3719 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
3720 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
3721 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
3722 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
3723 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
3724 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
3725 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
3726 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
3727 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00003728 return 0;
aurel321c54ff92008-10-13 21:08:44 +00003729}
3730
3731static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00003732{
3733 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00003734 struct msginfo msginfo;
3735 abi_long ret = -TARGET_EINVAL;
3736
3737 cmd &= 0xff;
3738
3739 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00003740 case IPC_STAT:
3741 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00003742 case MSG_STAT:
3743 if (target_to_host_msqid_ds(&dsarg,ptr))
3744 return -TARGET_EFAULT;
3745 ret = get_errno(msgctl(msgid, cmd, &dsarg));
3746 if (host_to_target_msqid_ds(ptr,&dsarg))
3747 return -TARGET_EFAULT;
3748 break;
3749 case IPC_RMID:
3750 ret = get_errno(msgctl(msgid, cmd, NULL));
3751 break;
3752 case IPC_INFO:
3753 case MSG_INFO:
3754 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
3755 if (host_to_target_msginfo(ptr, &msginfo))
3756 return -TARGET_EFAULT;
3757 break;
ths1bc012f2007-06-03 14:27:49 +00003758 }
aurel321c54ff92008-10-13 21:08:44 +00003759
ths1bc012f2007-06-03 14:27:49 +00003760 return ret;
3761}
3762
3763struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00003764 abi_long mtype;
3765 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00003766};
3767
blueswir1992f48a2007-10-14 16:27:31 +00003768static inline abi_long do_msgsnd(int msqid, abi_long msgp,
Tom Mustaedcc5f92014-08-12 13:53:37 -05003769 ssize_t msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00003770{
3771 struct target_msgbuf *target_mb;
3772 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00003773 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00003774
Tom Mustaedcc5f92014-08-12 13:53:37 -05003775 if (msgsz < 0) {
3776 return -TARGET_EINVAL;
3777 }
3778
bellard579a97f2007-11-11 14:26:47 +00003779 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
3780 return -TARGET_EFAULT;
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303781 host_mb = g_try_malloc(msgsz + sizeof(long));
zhanghailiang29e03fc2014-08-14 15:29:18 +08003782 if (!host_mb) {
3783 unlock_user_struct(target_mb, msgp, 0);
3784 return -TARGET_ENOMEM;
3785 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003786 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00003787 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00003788 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05303789 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00003790 unlock_user_struct(target_mb, msgp, 0);
3791
3792 return ret;
3793}
3794
blueswir1992f48a2007-10-14 16:27:31 +00003795static inline abi_long do_msgrcv(int msqid, abi_long msgp,
Peter Maydell99874f62016-05-20 19:00:56 +01003796 ssize_t msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00003797 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00003798{
3799 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00003800 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00003801 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00003802 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00003803
Peter Maydell99874f62016-05-20 19:00:56 +01003804 if (msgsz < 0) {
3805 return -TARGET_EINVAL;
3806 }
3807
bellard579a97f2007-11-11 14:26:47 +00003808 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
3809 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00003810
Peter Maydell415d8472016-05-20 19:00:57 +01003811 host_mb = g_try_malloc(msgsz + sizeof(long));
3812 if (!host_mb) {
3813 ret = -TARGET_ENOMEM;
3814 goto end;
3815 }
Laurent Vivier79dd77d2012-12-20 11:00:11 +00003816 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
aurel321c54ff92008-10-13 21:08:44 +00003817
bellard579a97f2007-11-11 14:26:47 +00003818 if (ret > 0) {
3819 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
3820 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
3821 if (!target_mtext) {
3822 ret = -TARGET_EFAULT;
3823 goto end;
3824 }
aurel321c54ff92008-10-13 21:08:44 +00003825 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00003826 unlock_user(target_mtext, target_mtext_addr, ret);
3827 }
aurel321c54ff92008-10-13 21:08:44 +00003828
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003829 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00003830
bellard579a97f2007-11-11 14:26:47 +00003831end:
3832 if (target_mb)
3833 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02003834 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00003835 return ret;
3836}
3837
Riku Voipio88a8c982009-04-03 10:42:00 +03003838static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
3839 abi_ulong target_addr)
3840{
3841 struct target_shmid_ds *target_sd;
3842
3843 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3844 return -TARGET_EFAULT;
3845 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
3846 return -TARGET_EFAULT;
3847 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3848 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
3849 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3850 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3851 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3852 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3853 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3854 unlock_user_struct(target_sd, target_addr, 0);
3855 return 0;
3856}
3857
3858static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
3859 struct shmid_ds *host_sd)
3860{
3861 struct target_shmid_ds *target_sd;
3862
3863 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3864 return -TARGET_EFAULT;
3865 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
3866 return -TARGET_EFAULT;
3867 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3868 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
3869 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3870 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3871 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3872 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3873 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3874 unlock_user_struct(target_sd, target_addr, 1);
3875 return 0;
3876}
3877
3878struct target_shminfo {
3879 abi_ulong shmmax;
3880 abi_ulong shmmin;
3881 abi_ulong shmmni;
3882 abi_ulong shmseg;
3883 abi_ulong shmall;
3884};
3885
3886static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
3887 struct shminfo *host_shminfo)
3888{
3889 struct target_shminfo *target_shminfo;
3890 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3891 return -TARGET_EFAULT;
3892 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3893 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3894 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3895 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3896 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3897 unlock_user_struct(target_shminfo, target_addr, 1);
3898 return 0;
3899}
3900
3901struct target_shm_info {
3902 int used_ids;
3903 abi_ulong shm_tot;
3904 abi_ulong shm_rss;
3905 abi_ulong shm_swp;
3906 abi_ulong swap_attempts;
3907 abi_ulong swap_successes;
3908};
3909
3910static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3911 struct shm_info *host_shm_info)
3912{
3913 struct target_shm_info *target_shm_info;
3914 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3915 return -TARGET_EFAULT;
3916 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3917 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3918 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3919 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3920 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3921 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3922 unlock_user_struct(target_shm_info, target_addr, 1);
3923 return 0;
3924}
3925
3926static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3927{
3928 struct shmid_ds dsarg;
3929 struct shminfo shminfo;
3930 struct shm_info shm_info;
3931 abi_long ret = -TARGET_EINVAL;
3932
3933 cmd &= 0xff;
3934
3935 switch(cmd) {
3936 case IPC_STAT:
3937 case IPC_SET:
3938 case SHM_STAT:
3939 if (target_to_host_shmid_ds(&dsarg, buf))
3940 return -TARGET_EFAULT;
3941 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3942 if (host_to_target_shmid_ds(buf, &dsarg))
3943 return -TARGET_EFAULT;
3944 break;
3945 case IPC_INFO:
3946 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3947 if (host_to_target_shminfo(buf, &shminfo))
3948 return -TARGET_EFAULT;
3949 break;
3950 case SHM_INFO:
3951 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3952 if (host_to_target_shm_info(buf, &shm_info))
3953 return -TARGET_EFAULT;
3954 break;
3955 case IPC_RMID:
3956 case SHM_LOCK:
3957 case SHM_UNLOCK:
3958 ret = get_errno(shmctl(shmid, cmd, NULL));
3959 break;
3960 }
3961
3962 return ret;
3963}
3964
3965static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3966{
3967 abi_long raddr;
3968 void *host_raddr;
3969 struct shmid_ds shm_info;
3970 int i,ret;
3971
3972 /* find out the length of the shared memory segment */
3973 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3974 if (is_error(ret)) {
3975 /* can't get length, bail out */
3976 return ret;
3977 }
3978
3979 mmap_lock();
3980
3981 if (shmaddr)
3982 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3983 else {
3984 abi_ulong mmap_start;
3985
3986 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3987
3988 if (mmap_start == -1) {
3989 errno = ENOMEM;
3990 host_raddr = (void *)-1;
3991 } else
3992 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3993 }
3994
3995 if (host_raddr == (void *)-1) {
3996 mmap_unlock();
3997 return get_errno((long)host_raddr);
3998 }
3999 raddr=h2g((unsigned long)host_raddr);
4000
4001 page_set_flags(raddr, raddr + shm_info.shm_segsz,
4002 PAGE_VALID | PAGE_READ |
4003 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
4004
4005 for (i = 0; i < N_SHM_REGIONS; i++) {
Peter Maydellb6e17872016-02-09 15:57:11 +00004006 if (!shm_regions[i].in_use) {
4007 shm_regions[i].in_use = true;
Riku Voipio88a8c982009-04-03 10:42:00 +03004008 shm_regions[i].start = raddr;
4009 shm_regions[i].size = shm_info.shm_segsz;
4010 break;
4011 }
4012 }
4013
4014 mmap_unlock();
4015 return raddr;
4016
4017}
4018
4019static inline abi_long do_shmdt(abi_ulong shmaddr)
4020{
4021 int i;
4022
4023 for (i = 0; i < N_SHM_REGIONS; ++i) {
Peter Maydellb6e17872016-02-09 15:57:11 +00004024 if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
4025 shm_regions[i].in_use = false;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09004026 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03004027 break;
4028 }
4029 }
4030
4031 return get_errno(shmdt(g2h(shmaddr)));
4032}
4033
aurel321c54ff92008-10-13 21:08:44 +00004034#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00004035/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00004036/* do_ipc() must return target values and target errnos. */
Tom Musta37ed0952014-08-12 13:53:35 -05004037static abi_long do_ipc(unsigned int call, abi_long first,
4038 abi_long second, abi_long third,
blueswir1992f48a2007-10-14 16:27:31 +00004039 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00004040{
4041 int version;
blueswir1992f48a2007-10-14 16:27:31 +00004042 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00004043
4044 version = call >> 16;
4045 call &= 0xffff;
4046
4047 switch (call) {
thsfa294812007-02-02 22:05:00 +00004048 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00004049 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00004050 break;
4051
4052 case IPCOP_semget:
4053 ret = get_errno(semget(first, second, third));
4054 break;
4055
Tom Musta5d2fa8e2014-08-12 13:53:33 -05004056 case IPCOP_semctl: {
4057 /* The semun argument to semctl is passed by value, so dereference the
4058 * ptr argument. */
4059 abi_ulong atptr;
Tom Musta37ed0952014-08-12 13:53:35 -05004060 get_user_ual(atptr, ptr);
Stefan Weild1c002b2015-02-08 15:40:58 +01004061 ret = do_semctl(first, second, third, atptr);
thsfa294812007-02-02 22:05:00 +00004062 break;
Tom Musta5d2fa8e2014-08-12 13:53:33 -05004063 }
thsd96372e2007-02-02 22:05:44 +00004064
aurel321c54ff92008-10-13 21:08:44 +00004065 case IPCOP_msgget:
4066 ret = get_errno(msgget(first, second));
4067 break;
thsd96372e2007-02-02 22:05:44 +00004068
aurel321c54ff92008-10-13 21:08:44 +00004069 case IPCOP_msgsnd:
4070 ret = do_msgsnd(first, ptr, second, third);
4071 break;
thsd96372e2007-02-02 22:05:44 +00004072
aurel321c54ff92008-10-13 21:08:44 +00004073 case IPCOP_msgctl:
4074 ret = do_msgctl(first, second, ptr);
4075 break;
thsd96372e2007-02-02 22:05:44 +00004076
aurel321c54ff92008-10-13 21:08:44 +00004077 case IPCOP_msgrcv:
4078 switch (version) {
4079 case 0:
4080 {
4081 struct target_ipc_kludge {
4082 abi_long msgp;
4083 abi_long msgtyp;
4084 } *tmp;
thsd96372e2007-02-02 22:05:44 +00004085
aurel321c54ff92008-10-13 21:08:44 +00004086 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4087 ret = -TARGET_EFAULT;
4088 break;
ths1bc012f2007-06-03 14:27:49 +00004089 }
aurel321c54ff92008-10-13 21:08:44 +00004090
Laurent Vivier79dd77d2012-12-20 11:00:11 +00004091 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
aurel321c54ff92008-10-13 21:08:44 +00004092
4093 unlock_user_struct(tmp, ptr, 0);
4094 break;
4095 }
4096 default:
4097 ret = do_msgrcv(first, ptr, second, fifth, third);
4098 }
4099 break;
thsd96372e2007-02-02 22:05:44 +00004100
bellard8853f862004-02-22 14:57:26 +00004101 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03004102 switch (version) {
4103 default:
bellard5a4a8982007-11-11 17:39:18 +00004104 {
4105 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03004106 raddr = do_shmat(first, ptr, second);
4107 if (is_error(raddr))
4108 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00004109 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00004110 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03004111 break;
4112 }
4113 case 1:
4114 ret = -TARGET_EINVAL;
4115 break;
bellard5a4a8982007-11-11 17:39:18 +00004116 }
bellard8853f862004-02-22 14:57:26 +00004117 break;
4118 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03004119 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00004120 break;
4121
4122 case IPCOP_shmget:
4123 /* IPC_* flag values are the same on all linux platforms */
4124 ret = get_errno(shmget(first, second, third));
4125 break;
4126
4127 /* IPC_* and SHM_* command values are the same on all linux platforms */
4128 case IPCOP_shmctl:
Petar Jovanovica2926782013-10-30 14:46:32 +01004129 ret = do_shmctl(first, second, ptr);
bellard8853f862004-02-22 14:57:26 +00004130 break;
4131 default:
j_mayer32407102007-09-26 23:01:49 +00004132 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00004133 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00004134 break;
4135 }
4136 return ret;
4137}
j_mayer32407102007-09-26 23:01:49 +00004138#endif
bellard8853f862004-02-22 14:57:26 +00004139
bellard31e31b82003-02-18 22:55:36 +00004140/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00004141
Blue Swirl001faf32009-05-13 17:53:17 +00004142#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00004143#define STRUCT_SPECIAL(name) STRUCT_ ## name,
4144enum {
4145#include "syscall_types.h"
Alexander Graf8be656b2015-05-06 23:47:32 +02004146STRUCT_MAX
bellard31e31b82003-02-18 22:55:36 +00004147};
4148#undef STRUCT
4149#undef STRUCT_SPECIAL
4150
Blue Swirl001faf32009-05-13 17:53:17 +00004151#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00004152#define STRUCT_SPECIAL(name)
4153#include "syscall_types.h"
4154#undef STRUCT
4155#undef STRUCT_SPECIAL
4156
Peter Maydelld2ef05b2011-01-06 15:04:17 +00004157typedef struct IOCTLEntry IOCTLEntry;
4158
4159typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004160 int fd, int cmd, abi_long arg);
Peter Maydelld2ef05b2011-01-06 15:04:17 +00004161
4162struct IOCTLEntry {
Ed Swierk9c6bf9c2014-12-16 12:55:31 -08004163 int target_cmd;
bellard2ab83ea2003-06-15 19:56:46 +00004164 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00004165 const char *name;
4166 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00004167 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00004168 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00004169};
bellard31e31b82003-02-18 22:55:36 +00004170
4171#define IOC_R 0x0001
4172#define IOC_W 0x0002
4173#define IOC_RW (IOC_R | IOC_W)
4174
4175#define MAX_STRUCT_SIZE 4096
4176
Peter Maydelldace20d2011-01-10 13:11:24 +00004177#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00004178/* So fiemap access checks don't overflow on 32 bit systems.
4179 * This is very slightly smaller than the limit imposed by
4180 * the underlying kernel.
4181 */
4182#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4183 / sizeof(struct fiemap_extent))
4184
4185static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004186 int fd, int cmd, abi_long arg)
Peter Maydell285da2b2011-01-06 15:04:18 +00004187{
4188 /* The parameter for this ioctl is a struct fiemap followed
4189 * by an array of struct fiemap_extent whose size is set
4190 * in fiemap->fm_extent_count. The array is filled in by the
4191 * ioctl.
4192 */
4193 int target_size_in, target_size_out;
4194 struct fiemap *fm;
4195 const argtype *arg_type = ie->arg_type;
4196 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4197 void *argptr, *p;
4198 abi_long ret;
4199 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4200 uint32_t outbufsz;
4201 int free_fm = 0;
4202
4203 assert(arg_type[0] == TYPE_PTR);
4204 assert(ie->access == IOC_RW);
4205 arg_type++;
4206 target_size_in = thunk_type_size(arg_type, 0);
4207 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4208 if (!argptr) {
4209 return -TARGET_EFAULT;
4210 }
4211 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4212 unlock_user(argptr, arg, 0);
4213 fm = (struct fiemap *)buf_temp;
4214 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
4215 return -TARGET_EINVAL;
4216 }
4217
4218 outbufsz = sizeof (*fm) +
4219 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
4220
4221 if (outbufsz > MAX_STRUCT_SIZE) {
4222 /* We can't fit all the extents into the fixed size buffer.
4223 * Allocate one that is large enough and use it instead.
4224 */
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304225 fm = g_try_malloc(outbufsz);
Peter Maydell285da2b2011-01-06 15:04:18 +00004226 if (!fm) {
4227 return -TARGET_ENOMEM;
4228 }
4229 memcpy(fm, buf_temp, sizeof(struct fiemap));
4230 free_fm = 1;
4231 }
4232 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
4233 if (!is_error(ret)) {
4234 target_size_out = target_size_in;
4235 /* An extent_count of 0 means we were only counting the extents
4236 * so there are no structs to copy
4237 */
4238 if (fm->fm_extent_count != 0) {
4239 target_size_out += fm->fm_mapped_extents * extent_size;
4240 }
4241 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
4242 if (!argptr) {
4243 ret = -TARGET_EFAULT;
4244 } else {
4245 /* Convert the struct fiemap */
4246 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
4247 if (fm->fm_extent_count != 0) {
4248 p = argptr + target_size_in;
4249 /* ...and then all the struct fiemap_extents */
4250 for (i = 0; i < fm->fm_mapped_extents; i++) {
4251 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
4252 THUNK_TARGET);
4253 p += extent_size;
4254 }
4255 }
4256 unlock_user(argptr, arg, target_size_out);
4257 }
4258 }
4259 if (free_fm) {
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05304260 g_free(fm);
Peter Maydell285da2b2011-01-06 15:04:18 +00004261 }
4262 return ret;
4263}
Peter Maydelldace20d2011-01-10 13:11:24 +00004264#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00004265
Laurent Vivier059c2f22011-03-30 00:12:12 +02004266static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004267 int fd, int cmd, abi_long arg)
Laurent Vivier059c2f22011-03-30 00:12:12 +02004268{
4269 const argtype *arg_type = ie->arg_type;
4270 int target_size;
4271 void *argptr;
4272 int ret;
4273 struct ifconf *host_ifconf;
4274 uint32_t outbufsz;
4275 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
4276 int target_ifreq_size;
4277 int nb_ifreq;
4278 int free_buf = 0;
4279 int i;
4280 int target_ifc_len;
4281 abi_long target_ifc_buf;
4282 int host_ifc_len;
4283 char *host_ifc_buf;
4284
4285 assert(arg_type[0] == TYPE_PTR);
4286 assert(ie->access == IOC_RW);
4287
4288 arg_type++;
4289 target_size = thunk_type_size(arg_type, 0);
4290
4291 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4292 if (!argptr)
4293 return -TARGET_EFAULT;
4294 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4295 unlock_user(argptr, arg, 0);
4296
4297 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
4298 target_ifc_len = host_ifconf->ifc_len;
4299 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
4300
4301 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
4302 nb_ifreq = target_ifc_len / target_ifreq_size;
4303 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
4304
4305 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
4306 if (outbufsz > MAX_STRUCT_SIZE) {
4307 /* We can't fit all the extents into the fixed size buffer.
4308 * Allocate one that is large enough and use it instead.
4309 */
4310 host_ifconf = malloc(outbufsz);
4311 if (!host_ifconf) {
4312 return -TARGET_ENOMEM;
4313 }
4314 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
4315 free_buf = 1;
4316 }
4317 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
4318
4319 host_ifconf->ifc_len = host_ifc_len;
4320 host_ifconf->ifc_buf = host_ifc_buf;
4321
4322 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
4323 if (!is_error(ret)) {
4324 /* convert host ifc_len to target ifc_len */
4325
4326 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
4327 target_ifc_len = nb_ifreq * target_ifreq_size;
4328 host_ifconf->ifc_len = target_ifc_len;
4329
4330 /* restore target ifc_buf */
4331
4332 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
4333
4334 /* copy struct ifconf to target user */
4335
4336 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4337 if (!argptr)
4338 return -TARGET_EFAULT;
4339 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
4340 unlock_user(argptr, arg, target_size);
4341
4342 /* copy ifreq[] to target user */
4343
4344 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
4345 for (i = 0; i < nb_ifreq ; i++) {
4346 thunk_convert(argptr + i * target_ifreq_size,
4347 host_ifc_buf + i * sizeof(struct ifreq),
4348 ifreq_arg_type, THUNK_TARGET);
4349 }
4350 unlock_user(argptr, target_ifc_buf, target_ifc_len);
4351 }
4352
4353 if (free_buf) {
4354 free(host_ifconf);
4355 }
4356
4357 return ret;
4358}
4359
Alexander Graf56e904e2012-01-31 18:42:06 +01004360static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004361 int cmd, abi_long arg)
Alexander Graf56e904e2012-01-31 18:42:06 +01004362{
4363 void *argptr;
4364 struct dm_ioctl *host_dm;
4365 abi_long guest_data;
4366 uint32_t guest_data_size;
4367 int target_size;
4368 const argtype *arg_type = ie->arg_type;
4369 abi_long ret;
4370 void *big_buf = NULL;
4371 char *host_data;
4372
4373 arg_type++;
4374 target_size = thunk_type_size(arg_type, 0);
4375 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4376 if (!argptr) {
4377 ret = -TARGET_EFAULT;
4378 goto out;
4379 }
4380 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4381 unlock_user(argptr, arg, 0);
4382
4383 /* buf_temp is too small, so fetch things into a bigger buffer */
4384 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
4385 memcpy(big_buf, buf_temp, target_size);
4386 buf_temp = big_buf;
4387 host_dm = big_buf;
4388
4389 guest_data = arg + host_dm->data_start;
4390 if ((guest_data - arg) < 0) {
4391 ret = -EINVAL;
4392 goto out;
4393 }
4394 guest_data_size = host_dm->data_size - host_dm->data_start;
4395 host_data = (char*)host_dm + host_dm->data_start;
4396
4397 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
4398 switch (ie->host_cmd) {
4399 case DM_REMOVE_ALL:
4400 case DM_LIST_DEVICES:
4401 case DM_DEV_CREATE:
4402 case DM_DEV_REMOVE:
4403 case DM_DEV_SUSPEND:
4404 case DM_DEV_STATUS:
4405 case DM_DEV_WAIT:
4406 case DM_TABLE_STATUS:
4407 case DM_TABLE_CLEAR:
4408 case DM_TABLE_DEPS:
4409 case DM_LIST_VERSIONS:
4410 /* no input data */
4411 break;
4412 case DM_DEV_RENAME:
4413 case DM_DEV_SET_GEOMETRY:
4414 /* data contains only strings */
4415 memcpy(host_data, argptr, guest_data_size);
4416 break;
4417 case DM_TARGET_MSG:
4418 memcpy(host_data, argptr, guest_data_size);
4419 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
4420 break;
4421 case DM_TABLE_LOAD:
4422 {
4423 void *gspec = argptr;
4424 void *cur_data = host_data;
4425 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
4426 int spec_size = thunk_type_size(arg_type, 0);
4427 int i;
4428
4429 for (i = 0; i < host_dm->target_count; i++) {
4430 struct dm_target_spec *spec = cur_data;
4431 uint32_t next;
4432 int slen;
4433
4434 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
4435 slen = strlen((char*)gspec + spec_size) + 1;
4436 next = spec->next;
4437 spec->next = sizeof(*spec) + slen;
4438 strcpy((char*)&spec[1], gspec + spec_size);
4439 gspec += next;
4440 cur_data += spec->next;
4441 }
4442 break;
4443 }
4444 default:
4445 ret = -TARGET_EINVAL;
Chen Gang Sdec04732015-01-25 08:00:42 +08004446 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01004447 goto out;
4448 }
4449 unlock_user(argptr, guest_data, 0);
4450
4451 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4452 if (!is_error(ret)) {
4453 guest_data = arg + host_dm->data_start;
4454 guest_data_size = host_dm->data_size - host_dm->data_start;
4455 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
4456 switch (ie->host_cmd) {
4457 case DM_REMOVE_ALL:
4458 case DM_DEV_CREATE:
4459 case DM_DEV_REMOVE:
4460 case DM_DEV_RENAME:
4461 case DM_DEV_SUSPEND:
4462 case DM_DEV_STATUS:
4463 case DM_TABLE_LOAD:
4464 case DM_TABLE_CLEAR:
4465 case DM_TARGET_MSG:
4466 case DM_DEV_SET_GEOMETRY:
4467 /* no return data */
4468 break;
4469 case DM_LIST_DEVICES:
4470 {
4471 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
4472 uint32_t remaining_data = guest_data_size;
4473 void *cur_data = argptr;
4474 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
4475 int nl_size = 12; /* can't use thunk_size due to alignment */
4476
4477 while (1) {
4478 uint32_t next = nl->next;
4479 if (next) {
4480 nl->next = nl_size + (strlen(nl->name) + 1);
4481 }
4482 if (remaining_data < nl->next) {
4483 host_dm->flags |= DM_BUFFER_FULL_FLAG;
4484 break;
4485 }
4486 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
4487 strcpy(cur_data + nl_size, nl->name);
4488 cur_data += nl->next;
4489 remaining_data -= nl->next;
4490 if (!next) {
4491 break;
4492 }
4493 nl = (void*)nl + next;
4494 }
4495 break;
4496 }
4497 case DM_DEV_WAIT:
4498 case DM_TABLE_STATUS:
4499 {
4500 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
4501 void *cur_data = argptr;
4502 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
4503 int spec_size = thunk_type_size(arg_type, 0);
4504 int i;
4505
4506 for (i = 0; i < host_dm->target_count; i++) {
4507 uint32_t next = spec->next;
4508 int slen = strlen((char*)&spec[1]) + 1;
4509 spec->next = (cur_data - argptr) + spec_size + slen;
4510 if (guest_data_size < spec->next) {
4511 host_dm->flags |= DM_BUFFER_FULL_FLAG;
4512 break;
4513 }
4514 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
4515 strcpy(cur_data + spec_size, (char*)&spec[1]);
4516 cur_data = argptr + spec->next;
4517 spec = (void*)host_dm + host_dm->data_start + next;
4518 }
4519 break;
4520 }
4521 case DM_TABLE_DEPS:
4522 {
4523 void *hdata = (void*)host_dm + host_dm->data_start;
4524 int count = *(uint32_t*)hdata;
4525 uint64_t *hdev = hdata + 8;
4526 uint64_t *gdev = argptr + 8;
4527 int i;
4528
4529 *(uint32_t*)argptr = tswap32(count);
4530 for (i = 0; i < count; i++) {
4531 *gdev = tswap64(*hdev);
4532 gdev++;
4533 hdev++;
4534 }
4535 break;
4536 }
4537 case DM_LIST_VERSIONS:
4538 {
4539 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
4540 uint32_t remaining_data = guest_data_size;
4541 void *cur_data = argptr;
4542 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
4543 int vers_size = thunk_type_size(arg_type, 0);
4544
4545 while (1) {
4546 uint32_t next = vers->next;
4547 if (next) {
4548 vers->next = vers_size + (strlen(vers->name) + 1);
4549 }
4550 if (remaining_data < vers->next) {
4551 host_dm->flags |= DM_BUFFER_FULL_FLAG;
4552 break;
4553 }
4554 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
4555 strcpy(cur_data + vers_size, vers->name);
4556 cur_data += vers->next;
4557 remaining_data -= vers->next;
4558 if (!next) {
4559 break;
4560 }
4561 vers = (void*)vers + next;
4562 }
4563 break;
4564 }
4565 default:
Chen Gang Sdec04732015-01-25 08:00:42 +08004566 unlock_user(argptr, guest_data, 0);
Alexander Graf56e904e2012-01-31 18:42:06 +01004567 ret = -TARGET_EINVAL;
4568 goto out;
4569 }
4570 unlock_user(argptr, guest_data, guest_data_size);
4571
4572 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4573 if (!argptr) {
4574 ret = -TARGET_EFAULT;
4575 goto out;
4576 }
4577 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4578 unlock_user(argptr, arg, target_size);
4579 }
4580out:
Stefan Weilad11ad72012-09-04 22:14:19 +02004581 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01004582 return ret;
4583}
4584
Alexander Grafa59b5e32014-08-22 13:15:50 +02004585static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004586 int cmd, abi_long arg)
Alexander Grafa59b5e32014-08-22 13:15:50 +02004587{
4588 void *argptr;
4589 int target_size;
4590 const argtype *arg_type = ie->arg_type;
4591 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
4592 abi_long ret;
4593
4594 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
4595 struct blkpg_partition host_part;
4596
4597 /* Read and convert blkpg */
4598 arg_type++;
4599 target_size = thunk_type_size(arg_type, 0);
4600 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4601 if (!argptr) {
4602 ret = -TARGET_EFAULT;
4603 goto out;
4604 }
4605 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4606 unlock_user(argptr, arg, 0);
4607
4608 switch (host_blkpg->op) {
4609 case BLKPG_ADD_PARTITION:
4610 case BLKPG_DEL_PARTITION:
4611 /* payload is struct blkpg_partition */
4612 break;
4613 default:
4614 /* Unknown opcode */
4615 ret = -TARGET_EINVAL;
4616 goto out;
4617 }
4618
4619 /* Read and convert blkpg->data */
4620 arg = (abi_long)(uintptr_t)host_blkpg->data;
4621 target_size = thunk_type_size(part_arg_type, 0);
4622 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4623 if (!argptr) {
4624 ret = -TARGET_EFAULT;
4625 goto out;
4626 }
4627 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
4628 unlock_user(argptr, arg, 0);
4629
4630 /* Swizzle the data pointer to our local copy and call! */
4631 host_blkpg->data = &host_part;
4632 ret = get_errno(ioctl(fd, ie->host_cmd, host_blkpg));
4633
4634out:
4635 return ret;
4636}
4637
Laurent Vivier7ff7b662013-07-02 14:04:12 +01004638static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004639 int fd, int cmd, abi_long arg)
Laurent Vivier7ff7b662013-07-02 14:04:12 +01004640{
4641 const argtype *arg_type = ie->arg_type;
4642 const StructEntry *se;
4643 const argtype *field_types;
4644 const int *dst_offsets, *src_offsets;
4645 int target_size;
4646 void *argptr;
4647 abi_ulong *target_rt_dev_ptr;
4648 unsigned long *host_rt_dev_ptr;
4649 abi_long ret;
4650 int i;
4651
4652 assert(ie->access == IOC_W);
4653 assert(*arg_type == TYPE_PTR);
4654 arg_type++;
4655 assert(*arg_type == TYPE_STRUCT);
4656 target_size = thunk_type_size(arg_type, 0);
4657 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4658 if (!argptr) {
4659 return -TARGET_EFAULT;
4660 }
4661 arg_type++;
4662 assert(*arg_type == (int)STRUCT_rtentry);
4663 se = struct_entries + *arg_type++;
4664 assert(se->convert[0] == NULL);
4665 /* convert struct here to be able to catch rt_dev string */
4666 field_types = se->field_types;
4667 dst_offsets = se->field_offsets[THUNK_HOST];
4668 src_offsets = se->field_offsets[THUNK_TARGET];
4669 for (i = 0; i < se->nb_fields; i++) {
4670 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
4671 assert(*field_types == TYPE_PTRVOID);
4672 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
4673 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
4674 if (*target_rt_dev_ptr != 0) {
4675 *host_rt_dev_ptr = (unsigned long)lock_user_string(
4676 tswapal(*target_rt_dev_ptr));
4677 if (!*host_rt_dev_ptr) {
4678 unlock_user(argptr, arg, 0);
4679 return -TARGET_EFAULT;
4680 }
4681 } else {
4682 *host_rt_dev_ptr = 0;
4683 }
4684 field_types++;
4685 continue;
4686 }
4687 field_types = thunk_convert(buf_temp + dst_offsets[i],
4688 argptr + src_offsets[i],
4689 field_types, THUNK_HOST);
4690 }
4691 unlock_user(argptr, arg, 0);
4692
4693 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4694 if (*host_rt_dev_ptr != 0) {
4695 unlock_user((void *)*host_rt_dev_ptr,
4696 *target_rt_dev_ptr, 0);
4697 }
4698 return ret;
4699}
4700
Paul Burtonca56f5b2014-06-22 11:25:47 +01004701static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
Laurent Vivier45c874e2015-06-16 00:35:28 +02004702 int fd, int cmd, abi_long arg)
Paul Burtonca56f5b2014-06-22 11:25:47 +01004703{
4704 int sig = target_to_host_signal(arg);
4705 return get_errno(ioctl(fd, ie->host_cmd, sig));
4706}
4707
blueswir19f106a72008-10-05 10:52:52 +00004708static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00004709#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00004710 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
4711#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
4712 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00004713#include "ioctls.h"
4714 { 0, 0, },
4715};
4716
pbrook53a59602006-03-25 19:31:22 +00004717/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00004718/* do_ioctl() Must return target values and target errnos. */
Laurent Vivier45c874e2015-06-16 00:35:28 +02004719static abi_long do_ioctl(int fd, int cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00004720{
4721 const IOCTLEntry *ie;
4722 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00004723 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00004724 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00004725 int target_size;
4726 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00004727
4728 ie = ioctl_entries;
4729 for(;;) {
4730 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00004731 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00004732 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00004733 }
4734 if (ie->target_cmd == cmd)
4735 break;
4736 ie++;
4737 }
4738 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00004739#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00004740 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00004741#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00004742 if (ie->do_ioctl) {
4743 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
4744 }
4745
bellard31e31b82003-02-18 22:55:36 +00004746 switch(arg_type[0]) {
4747 case TYPE_NULL:
4748 /* no argument */
4749 ret = get_errno(ioctl(fd, ie->host_cmd));
4750 break;
4751 case TYPE_PTRVOID:
4752 case TYPE_INT:
bellard31e31b82003-02-18 22:55:36 +00004753 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
4754 break;
4755 case TYPE_PTR:
4756 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00004757 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00004758 switch(ie->access) {
4759 case IOC_R:
4760 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4761 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004762 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4763 if (!argptr)
4764 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004765 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4766 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00004767 }
4768 break;
4769 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00004770 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4771 if (!argptr)
4772 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004773 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4774 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00004775 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4776 break;
4777 default:
4778 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00004779 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4780 if (!argptr)
4781 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004782 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4783 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00004784 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
4785 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004786 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4787 if (!argptr)
4788 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00004789 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4790 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00004791 }
4792 break;
4793 }
4794 break;
4795 default:
j_mayer32407102007-09-26 23:01:49 +00004796 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
4797 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00004798 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00004799 break;
4800 }
4801 return ret;
4802}
4803
blueswir1b39bc502008-10-05 10:51:10 +00004804static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004805 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
4806 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
4807 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
4808 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
4809 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
4810 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
4811 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
4812 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
4813 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
4814 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
4815 { TARGET_IXON, TARGET_IXON, IXON, IXON },
4816 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
4817 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
4818 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
4819 { 0, 0, 0, 0 }
4820};
4821
blueswir1b39bc502008-10-05 10:51:10 +00004822static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004823 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
4824 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
4825 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
4826 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
4827 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
4828 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
4829 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
4830 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
4831 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
4832 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
4833 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
4834 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
4835 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
4836 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
4837 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
4838 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
4839 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
4840 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
4841 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
4842 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
4843 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
4844 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
4845 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
4846 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
4847 { 0, 0, 0, 0 }
4848};
4849
blueswir1b39bc502008-10-05 10:51:10 +00004850static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004851 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
4852 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
4853 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
4854 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
4855 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
4856 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
4857 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
4858 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
4859 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
4860 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
4861 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
4862 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
4863 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
4864 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
4865 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
4866 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
4867 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
4868 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
4869 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
4870 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
4871 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
4872 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
4873 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
4874 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
4875 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
4876 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
4877 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
4878 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
4879 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
4880 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
4881 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
4882 { 0, 0, 0, 0 }
4883};
4884
blueswir1b39bc502008-10-05 10:51:10 +00004885static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00004886 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
4887 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
4888 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
4889 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
4890 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
4891 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
4892 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
4893 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
4894 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
4895 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
4896 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
4897 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
4898 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
4899 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
4900 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
4901 { 0, 0, 0, 0 }
4902};
4903
4904static void target_to_host_termios (void *dst, const void *src)
4905{
4906 struct host_termios *host = dst;
4907 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00004908
ths5fafdf22007-09-16 21:08:06 +00004909 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00004910 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004911 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00004912 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004913 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00004914 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00004915 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00004916 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
4917 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00004918
Arnaud Patard44607122009-04-21 17:39:08 +03004919 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00004920 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
4921 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00004922 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00004923 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00004924 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00004925 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00004926 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00004927 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00004928 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00004929 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
4930 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00004931 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
4932 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
4933 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
4934 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
4935 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00004936 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00004937}
ths3b46e622007-09-17 08:09:54 +00004938
bellard31e31b82003-02-18 22:55:36 +00004939static void host_to_target_termios (void *dst, const void *src)
4940{
4941 struct target_termios *target = dst;
4942 const struct host_termios *host = src;
4943
ths5fafdf22007-09-16 21:08:06 +00004944 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00004945 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004946 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00004947 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004948 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00004949 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00004950 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00004951 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
4952 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00004953
Arnaud Patard44607122009-04-21 17:39:08 +03004954 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00004955 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
4956 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
4957 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
4958 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
4959 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
4960 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
4961 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
4962 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
4963 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
4964 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
4965 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
4966 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
4967 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
4968 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
4969 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
4970 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
4971 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
4972}
4973
blueswir18e853dc2008-10-05 10:49:32 +00004974static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00004975 .convert = { host_to_target_termios, target_to_host_termios },
4976 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
4977 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
4978};
4979
bellard5286db72003-06-05 00:57:30 +00004980static bitmask_transtbl mmap_flags_tbl[] = {
4981 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
4982 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
4983 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
4984 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
4985 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
4986 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
4987 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
4988 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
Christophe Lyone8efd8e2014-02-03 17:04:32 +01004989 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, MAP_NORESERVE,
4990 MAP_NORESERVE },
bellard5286db72003-06-05 00:57:30 +00004991 { 0, 0, 0, 0 }
4992};
4993
bellard2ab83ea2003-06-15 19:56:46 +00004994#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00004995
4996/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00004997static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00004998
bellard03acab62007-11-11 14:57:14 +00004999static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00005000{
5001 int size;
pbrook53a59602006-03-25 19:31:22 +00005002 void *p;
bellard6dbad632003-03-16 18:05:05 +00005003
5004 if (!ldt_table)
5005 return 0;
5006 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
5007 if (size > bytecount)
5008 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00005009 p = lock_user(VERIFY_WRITE, ptr, size, 0);
5010 if (!p)
bellard03acab62007-11-11 14:57:14 +00005011 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00005012 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00005013 memcpy(p, ldt_table, size);
5014 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00005015 return size;
5016}
5017
5018/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00005019static abi_long write_ldt(CPUX86State *env,
5020 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00005021{
5022 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00005023 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00005024 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00005025 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00005026 uint32_t *lp, entry_1, entry_2;
5027
5028 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00005029 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00005030 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00005031 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00005032 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005033 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00005034 ldt_info.limit = tswap32(target_ldt_info->limit);
5035 ldt_info.flags = tswap32(target_ldt_info->flags);
5036 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00005037
bellard6dbad632003-03-16 18:05:05 +00005038 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00005039 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00005040 seg_32bit = ldt_info.flags & 1;
5041 contents = (ldt_info.flags >> 1) & 3;
5042 read_exec_only = (ldt_info.flags >> 3) & 1;
5043 limit_in_pages = (ldt_info.flags >> 4) & 1;
5044 seg_not_present = (ldt_info.flags >> 5) & 1;
5045 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00005046#ifdef TARGET_ABI32
5047 lm = 0;
5048#else
5049 lm = (ldt_info.flags >> 7) & 1;
5050#endif
bellard6dbad632003-03-16 18:05:05 +00005051 if (contents == 3) {
5052 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00005053 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00005054 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00005055 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00005056 }
5057 /* allocate the LDT */
5058 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00005059 env->ldt.base = target_mmap(0,
5060 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
5061 PROT_READ|PROT_WRITE,
5062 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
5063 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00005064 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00005065 memset(g2h(env->ldt.base), 0,
5066 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00005067 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00005068 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00005069 }
5070
5071 /* NOTE: same code as Linux kernel */
5072 /* Allow LDTs to be cleared by the user. */
5073 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
5074 if (oldmode ||
5075 (contents == 0 &&
5076 read_exec_only == 1 &&
5077 seg_32bit == 0 &&
5078 limit_in_pages == 0 &&
5079 seg_not_present == 1 &&
5080 useable == 0 )) {
5081 entry_1 = 0;
5082 entry_2 = 0;
5083 goto install;
5084 }
5085 }
ths3b46e622007-09-17 08:09:54 +00005086
bellard6dbad632003-03-16 18:05:05 +00005087 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
5088 (ldt_info.limit & 0x0ffff);
5089 entry_2 = (ldt_info.base_addr & 0xff000000) |
5090 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
5091 (ldt_info.limit & 0xf0000) |
5092 ((read_exec_only ^ 1) << 9) |
5093 (contents << 10) |
5094 ((seg_not_present ^ 1) << 15) |
5095 (seg_32bit << 22) |
5096 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00005097 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00005098 0x7000;
5099 if (!oldmode)
5100 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00005101
bellard6dbad632003-03-16 18:05:05 +00005102 /* Install the new entry ... */
5103install:
5104 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
5105 lp[0] = tswap32(entry_1);
5106 lp[1] = tswap32(entry_2);
5107 return 0;
5108}
5109
5110/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00005111static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
5112 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00005113{
bellard03acab62007-11-11 14:57:14 +00005114 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00005115
bellard6dbad632003-03-16 18:05:05 +00005116 switch (func) {
5117 case 0:
5118 ret = read_ldt(ptr, bytecount);
5119 break;
5120 case 1:
5121 ret = write_ldt(env, ptr, bytecount, 1);
5122 break;
5123 case 0x11:
5124 ret = write_ldt(env, ptr, bytecount, 0);
5125 break;
bellard03acab62007-11-11 14:57:14 +00005126 default:
5127 ret = -TARGET_ENOSYS;
5128 break;
bellard6dbad632003-03-16 18:05:05 +00005129 }
5130 return ret;
5131}
bellard1b6b0292003-03-22 17:31:38 +00005132
blueswir14583f582008-08-24 10:35:55 +00005133#if defined(TARGET_I386) && defined(TARGET_ABI32)
Alexander Grafbc22eb42013-07-16 18:44:58 +01005134abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00005135{
5136 uint64_t *gdt_table = g2h(env->gdt.base);
5137 struct target_modify_ldt_ldt_s ldt_info;
5138 struct target_modify_ldt_ldt_s *target_ldt_info;
5139 int seg_32bit, contents, read_exec_only, limit_in_pages;
5140 int seg_not_present, useable, lm;
5141 uint32_t *lp, entry_1, entry_2;
5142 int i;
5143
5144 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
5145 if (!target_ldt_info)
5146 return -TARGET_EFAULT;
5147 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005148 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00005149 ldt_info.limit = tswap32(target_ldt_info->limit);
5150 ldt_info.flags = tswap32(target_ldt_info->flags);
5151 if (ldt_info.entry_number == -1) {
5152 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
5153 if (gdt_table[i] == 0) {
5154 ldt_info.entry_number = i;
5155 target_ldt_info->entry_number = tswap32(i);
5156 break;
5157 }
5158 }
5159 }
5160 unlock_user_struct(target_ldt_info, ptr, 1);
5161
5162 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
5163 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
5164 return -TARGET_EINVAL;
5165 seg_32bit = ldt_info.flags & 1;
5166 contents = (ldt_info.flags >> 1) & 3;
5167 read_exec_only = (ldt_info.flags >> 3) & 1;
5168 limit_in_pages = (ldt_info.flags >> 4) & 1;
5169 seg_not_present = (ldt_info.flags >> 5) & 1;
5170 useable = (ldt_info.flags >> 6) & 1;
5171#ifdef TARGET_ABI32
5172 lm = 0;
5173#else
5174 lm = (ldt_info.flags >> 7) & 1;
5175#endif
5176
5177 if (contents == 3) {
5178 if (seg_not_present == 0)
5179 return -TARGET_EINVAL;
5180 }
5181
5182 /* NOTE: same code as Linux kernel */
5183 /* Allow LDTs to be cleared by the user. */
5184 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
5185 if ((contents == 0 &&
5186 read_exec_only == 1 &&
5187 seg_32bit == 0 &&
5188 limit_in_pages == 0 &&
5189 seg_not_present == 1 &&
5190 useable == 0 )) {
5191 entry_1 = 0;
5192 entry_2 = 0;
5193 goto install;
5194 }
5195 }
5196
5197 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
5198 (ldt_info.limit & 0x0ffff);
5199 entry_2 = (ldt_info.base_addr & 0xff000000) |
5200 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
5201 (ldt_info.limit & 0xf0000) |
5202 ((read_exec_only ^ 1) << 9) |
5203 (contents << 10) |
5204 ((seg_not_present ^ 1) << 15) |
5205 (seg_32bit << 22) |
5206 (limit_in_pages << 23) |
5207 (useable << 20) |
5208 (lm << 21) |
5209 0x7000;
5210
5211 /* Install the new entry ... */
5212install:
5213 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
5214 lp[0] = tswap32(entry_1);
5215 lp[1] = tswap32(entry_2);
5216 return 0;
5217}
5218
blueswir18fcd3692008-08-17 20:26:25 +00005219static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00005220{
5221 struct target_modify_ldt_ldt_s *target_ldt_info;
5222 uint64_t *gdt_table = g2h(env->gdt.base);
5223 uint32_t base_addr, limit, flags;
5224 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
5225 int seg_not_present, useable, lm;
5226 uint32_t *lp, entry_1, entry_2;
5227
5228 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
5229 if (!target_ldt_info)
5230 return -TARGET_EFAULT;
5231 idx = tswap32(target_ldt_info->entry_number);
5232 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
5233 idx > TARGET_GDT_ENTRY_TLS_MAX) {
5234 unlock_user_struct(target_ldt_info, ptr, 1);
5235 return -TARGET_EINVAL;
5236 }
5237 lp = (uint32_t *)(gdt_table + idx);
5238 entry_1 = tswap32(lp[0]);
5239 entry_2 = tswap32(lp[1]);
5240
5241 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
5242 contents = (entry_2 >> 10) & 3;
5243 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
5244 seg_32bit = (entry_2 >> 22) & 1;
5245 limit_in_pages = (entry_2 >> 23) & 1;
5246 useable = (entry_2 >> 20) & 1;
5247#ifdef TARGET_ABI32
5248 lm = 0;
5249#else
5250 lm = (entry_2 >> 21) & 1;
5251#endif
5252 flags = (seg_32bit << 0) | (contents << 1) |
5253 (read_exec_only << 3) | (limit_in_pages << 4) |
5254 (seg_not_present << 5) | (useable << 6) | (lm << 7);
5255 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
5256 base_addr = (entry_1 >> 16) |
5257 (entry_2 & 0xff000000) |
5258 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005259 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00005260 target_ldt_info->limit = tswap32(limit);
5261 target_ldt_info->flags = tswap32(flags);
5262 unlock_user_struct(target_ldt_info, ptr, 1);
5263 return 0;
5264}
blueswir14583f582008-08-24 10:35:55 +00005265#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00005266
bellardd2fd1af2007-11-14 18:08:56 +00005267#ifndef TARGET_ABI32
Peter Maydell2667e712013-07-16 18:44:59 +01005268abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00005269{
Juan Quintela1add8692011-06-16 17:37:09 +01005270 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00005271 abi_ulong val;
5272 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01005273
bellardd2fd1af2007-11-14 18:08:56 +00005274 switch(code) {
5275 case TARGET_ARCH_SET_GS:
5276 case TARGET_ARCH_SET_FS:
5277 if (code == TARGET_ARCH_SET_GS)
5278 idx = R_GS;
5279 else
5280 idx = R_FS;
5281 cpu_x86_load_seg(env, idx, 0);
5282 env->segs[idx].base = addr;
5283 break;
5284 case TARGET_ARCH_GET_GS:
5285 case TARGET_ARCH_GET_FS:
5286 if (code == TARGET_ARCH_GET_GS)
5287 idx = R_GS;
5288 else
5289 idx = R_FS;
5290 val = env->segs[idx].base;
5291 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01005292 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00005293 break;
5294 default:
5295 ret = -TARGET_EINVAL;
5296 break;
5297 }
Juan Quintela1add8692011-06-16 17:37:09 +01005298 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00005299}
5300#endif
5301
bellard2ab83ea2003-06-15 19:56:46 +00005302#endif /* defined(TARGET_I386) */
5303
Riku Voipio05098a92011-03-04 15:27:29 +02005304#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00005305
pbrookd865bab2008-06-07 22:12:17 +00005306
5307static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
5308typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01005309 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00005310 pthread_mutex_t mutex;
5311 pthread_cond_t cond;
5312 pthread_t thread;
5313 uint32_t tid;
5314 abi_ulong child_tidptr;
5315 abi_ulong parent_tidptr;
5316 sigset_t sigmask;
5317} new_thread_info;
5318
5319static void *clone_func(void *arg)
5320{
5321 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005322 CPUArchState *env;
Andreas Färber0d342822012-12-17 07:12:13 +01005323 CPUState *cpu;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03005324 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00005325
Emilio G. Cota70903762015-08-23 20:23:41 -04005326 rcu_register_thread();
pbrookd865bab2008-06-07 22:12:17 +00005327 env = info->env;
Andreas Färber0d342822012-12-17 07:12:13 +01005328 cpu = ENV_GET_CPU(env);
Andreas Färbera2247f82013-06-09 19:47:04 +02005329 thread_cpu = cpu;
Andreas Färber0429a972013-08-26 18:14:44 +02005330 ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00005331 info->tid = gettid();
Andreas Färber0d342822012-12-17 07:12:13 +01005332 cpu->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03005333 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00005334 if (info->child_tidptr)
5335 put_user_u32(info->tid, info->child_tidptr);
5336 if (info->parent_tidptr)
5337 put_user_u32(info->tid, info->parent_tidptr);
5338 /* Enable signals. */
5339 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
5340 /* Signal to the parent that we're ready. */
5341 pthread_mutex_lock(&info->mutex);
5342 pthread_cond_broadcast(&info->cond);
5343 pthread_mutex_unlock(&info->mutex);
5344 /* Wait until the parent has finshed initializing the tls state. */
5345 pthread_mutex_lock(&clone_lock);
5346 pthread_mutex_unlock(&clone_lock);
5347 cpu_loop(env);
5348 /* never exits */
5349 return NULL;
5350}
bellard1b6b0292003-03-22 17:31:38 +00005351
ths0da46a62007-10-20 20:23:07 +00005352/* do_fork() Must return host values and target errnos (unlike most
5353 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01005354static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00005355 abi_ulong parent_tidptr, target_ulong newtls,
5356 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00005357{
Andreas Färber0429a972013-08-26 18:14:44 +02005358 CPUState *cpu = ENV_GET_CPU(env);
bellard1b6b0292003-03-22 17:31:38 +00005359 int ret;
bellard5cd43932003-03-29 16:54:36 +00005360 TaskState *ts;
Andreas Färber0429a972013-08-26 18:14:44 +02005361 CPUState *new_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005362 CPUArchState *new_env;
pbrookd865bab2008-06-07 22:12:17 +00005363 unsigned int nptl_flags;
5364 sigset_t sigmask;
ths3b46e622007-09-17 08:09:54 +00005365
balrog436d1242008-09-21 02:39:45 +00005366 /* Emulate vfork() with fork() */
5367 if (flags & CLONE_VFORK)
5368 flags &= ~(CLONE_VFORK | CLONE_VM);
5369
bellard1b6b0292003-03-22 17:31:38 +00005370 if (flags & CLONE_VM) {
Andreas Färber0429a972013-08-26 18:14:44 +02005371 TaskState *parent_ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00005372 new_thread_info info;
5373 pthread_attr_t attr;
Peter Maydell24cb36a2013-07-16 18:45:00 +01005374
Markus Armbrusterc78d65e2015-09-14 13:53:03 +02005375 ts = g_new0(TaskState, 1);
pbrook624f7972008-05-31 16:11:38 +00005376 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00005377 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00005378 new_env = cpu_copy(env);
pbrook6e68e072008-05-30 17:22:15 +00005379 /* Init regs that differ from the parent. */
5380 cpu_clone_regs(new_env, newsp);
Andreas Färber0429a972013-08-26 18:14:44 +02005381 new_cpu = ENV_GET_CPU(new_env);
5382 new_cpu->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03005383 ts->bprm = parent_ts->bprm;
5384 ts->info = parent_ts->info;
pbrookd865bab2008-06-07 22:12:17 +00005385 nptl_flags = flags;
5386 flags &= ~CLONE_NPTL_FLAGS2;
5387
pbrookc2764712009-03-07 15:24:59 +00005388 if (nptl_flags & CLONE_CHILD_CLEARTID) {
5389 ts->child_tidptr = child_tidptr;
5390 }
5391
pbrookd865bab2008-06-07 22:12:17 +00005392 if (nptl_flags & CLONE_SETTLS)
5393 cpu_set_tls (new_env, newtls);
5394
5395 /* Grab a mutex so that thread setup appears atomic. */
5396 pthread_mutex_lock(&clone_lock);
5397
5398 memset(&info, 0, sizeof(info));
5399 pthread_mutex_init(&info.mutex, NULL);
5400 pthread_mutex_lock(&info.mutex);
5401 pthread_cond_init(&info.cond, NULL);
5402 info.env = new_env;
5403 if (nptl_flags & CLONE_CHILD_SETTID)
5404 info.child_tidptr = child_tidptr;
5405 if (nptl_flags & CLONE_PARENT_SETTID)
5406 info.parent_tidptr = parent_tidptr;
5407
5408 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07005409 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
5410 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00005411 /* It is not safe to deliver signals until the child has finished
5412 initializing, so temporarily block all signals. */
5413 sigfillset(&sigmask);
5414 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
5415
5416 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00005417 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00005418
5419 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
5420 pthread_attr_destroy(&attr);
5421 if (ret == 0) {
5422 /* Wait for the child to initialize. */
5423 pthread_cond_wait(&info.cond, &info.mutex);
5424 ret = info.tid;
5425 if (flags & CLONE_PARENT_SETTID)
5426 put_user_u32(ret, parent_tidptr);
5427 } else {
5428 ret = -1;
5429 }
5430 pthread_mutex_unlock(&info.mutex);
5431 pthread_cond_destroy(&info.cond);
5432 pthread_mutex_destroy(&info.mutex);
5433 pthread_mutex_unlock(&clone_lock);
bellard1b6b0292003-03-22 17:31:38 +00005434 } else {
5435 /* if no CLONE_VM, we consider it is a fork */
Timothy E Baldwin93b4eff2015-08-31 00:26:21 +01005436 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0) {
5437 return -TARGET_EINVAL;
5438 }
pbrookd865bab2008-06-07 22:12:17 +00005439 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00005440 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00005441 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00005442 /* Child Process. */
Paolo Bonzinia59629f2015-03-31 13:01:05 +02005443 rcu_after_fork();
pbrookd865bab2008-06-07 22:12:17 +00005444 cpu_clone_regs(env, newsp);
5445 fork_end(1);
aurel322b1319c2008-12-18 22:44:04 +00005446 /* There is a race condition here. The parent process could
5447 theoretically read the TID in the child process before the child
5448 tid is set. This would require using either ptrace
5449 (not implemented) or having *_tidptr to point at a shared memory
5450 mapping. We can't repeat the spinlock hack used above because
5451 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00005452 if (flags & CLONE_CHILD_SETTID)
5453 put_user_u32(gettid(), child_tidptr);
5454 if (flags & CLONE_PARENT_SETTID)
5455 put_user_u32(gettid(), parent_tidptr);
Andreas Färber0429a972013-08-26 18:14:44 +02005456 ts = (TaskState *)cpu->opaque;
pbrookd865bab2008-06-07 22:12:17 +00005457 if (flags & CLONE_SETTLS)
5458 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00005459 if (flags & CLONE_CHILD_CLEARTID)
5460 ts->child_tidptr = child_tidptr;
pbrookd865bab2008-06-07 22:12:17 +00005461 } else {
5462 fork_end(0);
5463 }
bellard1b6b0292003-03-22 17:31:38 +00005464 }
5465 return ret;
5466}
5467
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005468/* warning : doesn't handle linux specific flags... */
5469static int target_to_host_fcntl_cmd(int cmd)
5470{
5471 switch(cmd) {
5472 case TARGET_F_DUPFD:
5473 case TARGET_F_GETFD:
5474 case TARGET_F_SETFD:
5475 case TARGET_F_GETFL:
5476 case TARGET_F_SETFL:
5477 return cmd;
5478 case TARGET_F_GETLK:
5479 return F_GETLK;
5480 case TARGET_F_SETLK:
5481 return F_SETLK;
5482 case TARGET_F_SETLKW:
5483 return F_SETLKW;
5484 case TARGET_F_GETOWN:
5485 return F_GETOWN;
5486 case TARGET_F_SETOWN:
5487 return F_SETOWN;
5488 case TARGET_F_GETSIG:
5489 return F_GETSIG;
5490 case TARGET_F_SETSIG:
5491 return F_SETSIG;
5492#if TARGET_ABI_BITS == 32
5493 case TARGET_F_GETLK64:
5494 return F_GETLK64;
5495 case TARGET_F_SETLK64:
5496 return F_SETLK64;
5497 case TARGET_F_SETLKW64:
5498 return F_SETLKW64;
5499#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005500 case TARGET_F_SETLEASE:
5501 return F_SETLEASE;
5502 case TARGET_F_GETLEASE:
5503 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04005504#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005505 case TARGET_F_DUPFD_CLOEXEC:
5506 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04005507#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005508 case TARGET_F_NOTIFY:
5509 return F_NOTIFY;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01005510#ifdef F_GETOWN_EX
5511 case TARGET_F_GETOWN_EX:
5512 return F_GETOWN_EX;
5513#endif
5514#ifdef F_SETOWN_EX
5515 case TARGET_F_SETOWN_EX:
5516 return F_SETOWN_EX;
5517#endif
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005518 default:
5519 return -TARGET_EINVAL;
5520 }
5521 return -TARGET_EINVAL;
5522}
5523
Laurent Vivier2ba7f732013-01-10 21:42:48 +01005524#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
5525static const bitmask_transtbl flock_tbl[] = {
5526 TRANSTBL_CONVERT(F_RDLCK),
5527 TRANSTBL_CONVERT(F_WRLCK),
5528 TRANSTBL_CONVERT(F_UNLCK),
5529 TRANSTBL_CONVERT(F_EXLCK),
5530 TRANSTBL_CONVERT(F_SHLCK),
5531 { 0, 0, 0, 0 }
5532};
5533
blueswir1992f48a2007-10-14 16:27:31 +00005534static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00005535{
5536 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00005537 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00005538 struct flock64 fl64;
5539 struct target_flock64 *target_fl64;
Andreas Schwab8d5d3002014-03-07 15:24:08 +01005540#ifdef F_GETOWN_EX
5541 struct f_owner_ex fox;
5542 struct target_f_owner_ex *target_fox;
5543#endif
blueswir1992f48a2007-10-14 16:27:31 +00005544 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005545 int host_cmd = target_to_host_fcntl_cmd(cmd);
5546
5547 if (host_cmd == -TARGET_EINVAL)
5548 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00005549
bellard7775e9e2003-05-14 22:46:48 +00005550 switch(cmd) {
5551 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00005552 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
5553 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01005554 fl.l_type =
5555 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
ths58134272007-03-31 18:59:32 +00005556 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005557 fl.l_start = tswapal(target_fl->l_start);
5558 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005559 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00005560 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005561 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00005562 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00005563 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
5564 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01005565 target_fl->l_type =
5566 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00005567 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005568 target_fl->l_start = tswapal(fl.l_start);
5569 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005570 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005571 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00005572 }
5573 break;
ths3b46e622007-09-17 08:09:54 +00005574
bellard7775e9e2003-05-14 22:46:48 +00005575 case TARGET_F_SETLK:
5576 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00005577 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
5578 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01005579 fl.l_type =
5580 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00005581 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005582 fl.l_start = tswapal(target_fl->l_start);
5583 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005584 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005585 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005586 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00005587 break;
ths3b46e622007-09-17 08:09:54 +00005588
bellard7775e9e2003-05-14 22:46:48 +00005589 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00005590 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
5591 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01005592 fl64.l_type =
5593 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths58134272007-03-31 18:59:32 +00005594 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005595 fl64.l_start = tswap64(target_fl64->l_start);
5596 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005597 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00005598 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005599 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00005600 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00005601 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
5602 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01005603 target_fl64->l_type =
5604 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00005605 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005606 target_fl64->l_start = tswap64(fl64.l_start);
5607 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005608 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00005609 unlock_user_struct(target_fl64, arg, 1);
5610 }
bellard9ee1fa22007-11-11 15:11:19 +00005611 break;
bellard7775e9e2003-05-14 22:46:48 +00005612 case TARGET_F_SETLK64:
5613 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00005614 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
5615 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01005616 fl64.l_type =
5617 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00005618 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005619 fl64.l_start = tswap64(target_fl64->l_start);
5620 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005621 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00005622 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005623 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00005624 break;
5625
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005626 case TARGET_F_GETFL:
5627 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00005628 if (ret >= 0) {
5629 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
5630 }
bellardffa65c32004-01-04 23:57:22 +00005631 break;
5632
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005633 case TARGET_F_SETFL:
5634 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
5635 break;
5636
Andreas Schwab8d5d3002014-03-07 15:24:08 +01005637#ifdef F_GETOWN_EX
5638 case TARGET_F_GETOWN_EX:
5639 ret = get_errno(fcntl(fd, host_cmd, &fox));
5640 if (ret >= 0) {
5641 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
5642 return -TARGET_EFAULT;
5643 target_fox->type = tswap32(fox.type);
5644 target_fox->pid = tswap32(fox.pid);
5645 unlock_user_struct(target_fox, arg, 1);
5646 }
5647 break;
5648#endif
5649
5650#ifdef F_SETOWN_EX
5651 case TARGET_F_SETOWN_EX:
5652 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
5653 return -TARGET_EFAULT;
5654 fox.type = tswap32(target_fox->type);
5655 fox.pid = tswap32(target_fox->pid);
5656 unlock_user_struct(target_fox, arg, 0);
5657 ret = get_errno(fcntl(fd, host_cmd, &fox));
5658 break;
5659#endif
5660
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005661 case TARGET_F_SETOWN:
5662 case TARGET_F_GETOWN:
5663 case TARGET_F_SETSIG:
5664 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02005665 case TARGET_F_SETLEASE:
5666 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02005667 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00005668 break;
5669
bellard7775e9e2003-05-14 22:46:48 +00005670 default:
bellard9ee1fa22007-11-11 15:11:19 +00005671 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00005672 break;
5673 }
5674 return ret;
5675}
5676
bellard67867302003-11-23 17:05:30 +00005677#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00005678
bellard67867302003-11-23 17:05:30 +00005679static inline int high2lowuid(int uid)
5680{
5681 if (uid > 65535)
5682 return 65534;
5683 else
5684 return uid;
5685}
5686
5687static inline int high2lowgid(int gid)
5688{
5689 if (gid > 65535)
5690 return 65534;
5691 else
5692 return gid;
5693}
5694
5695static inline int low2highuid(int uid)
5696{
5697 if ((int16_t)uid == -1)
5698 return -1;
5699 else
5700 return uid;
5701}
5702
5703static inline int low2highgid(int gid)
5704{
5705 if ((int16_t)gid == -1)
5706 return -1;
5707 else
5708 return gid;
5709}
Riku Voipio0c866a72011-04-18 15:23:06 +03005710static inline int tswapid(int id)
5711{
5712 return tswap16(id);
5713}
Peter Maydell76ca3102014-03-02 19:36:41 +00005714
5715#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
5716
Riku Voipio0c866a72011-04-18 15:23:06 +03005717#else /* !USE_UID16 */
5718static inline int high2lowuid(int uid)
5719{
5720 return uid;
5721}
5722static inline int high2lowgid(int gid)
5723{
5724 return gid;
5725}
5726static inline int low2highuid(int uid)
5727{
5728 return uid;
5729}
5730static inline int low2highgid(int gid)
5731{
5732 return gid;
5733}
5734static inline int tswapid(int id)
5735{
5736 return tswap32(id);
5737}
Peter Maydell76ca3102014-03-02 19:36:41 +00005738
5739#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
5740
bellard67867302003-11-23 17:05:30 +00005741#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00005742
Peter Maydellfd6f7792016-03-01 16:33:02 +00005743/* We must do direct syscalls for setting UID/GID, because we want to
5744 * implement the Linux system call semantics of "change only for this thread",
5745 * not the libc/POSIX semantics of "change for all threads in process".
5746 * (See http://ewontfix.com/17/ for more details.)
5747 * We use the 32-bit version of the syscalls if present; if it is not
5748 * then either the host architecture supports 32-bit UIDs natively with
5749 * the standard syscall, or the 16-bit UID is the best we can do.
5750 */
5751#ifdef __NR_setuid32
5752#define __NR_sys_setuid __NR_setuid32
5753#else
5754#define __NR_sys_setuid __NR_setuid
5755#endif
5756#ifdef __NR_setgid32
5757#define __NR_sys_setgid __NR_setgid32
5758#else
5759#define __NR_sys_setgid __NR_setgid
5760#endif
5761#ifdef __NR_setresuid32
5762#define __NR_sys_setresuid __NR_setresuid32
5763#else
5764#define __NR_sys_setresuid __NR_setresuid
5765#endif
5766#ifdef __NR_setresgid32
5767#define __NR_sys_setresgid __NR_setresgid32
5768#else
5769#define __NR_sys_setresgid __NR_setresgid
5770#endif
5771
5772_syscall1(int, sys_setuid, uid_t, uid)
5773_syscall1(int, sys_setgid, gid_t, gid)
5774_syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
5775_syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
5776
bellard31e31b82003-02-18 22:55:36 +00005777void syscall_init(void)
5778{
bellard2ab83ea2003-06-15 19:56:46 +00005779 IOCTLEntry *ie;
5780 const argtype *arg_type;
5781 int size;
thsb92c47c2007-11-01 00:07:38 +00005782 int i;
bellard2ab83ea2003-06-15 19:56:46 +00005783
Alexander Graf8be656b2015-05-06 23:47:32 +02005784 thunk_init(STRUCT_MAX);
5785
Blue Swirl001faf32009-05-13 17:53:17 +00005786#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00005787#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00005788#include "syscall_types.h"
5789#undef STRUCT
5790#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00005791
Peter Maydelldd6e9572012-07-23 08:07:22 +00005792 /* Build target_to_host_errno_table[] table from
5793 * host_to_target_errno_table[]. */
5794 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
5795 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
5796 }
5797
bellard2ab83ea2003-06-15 19:56:46 +00005798 /* we patch the ioctl size if necessary. We rely on the fact that
5799 no ioctl has all the bits at '1' in the size field */
5800 ie = ioctl_entries;
5801 while (ie->target_cmd != 0) {
5802 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
5803 TARGET_IOC_SIZEMASK) {
5804 arg_type = ie->arg_type;
5805 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00005806 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00005807 ie->target_cmd);
5808 exit(1);
5809 }
5810 arg_type++;
5811 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00005812 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00005813 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
5814 (size << TARGET_IOC_SIZESHIFT);
5815 }
thsb92c47c2007-11-01 00:07:38 +00005816
bellard2ab83ea2003-06-15 19:56:46 +00005817 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00005818#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
5819 (defined(__x86_64__) && defined(TARGET_X86_64))
5820 if (unlikely(ie->target_cmd != ie->host_cmd)) {
5821 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
5822 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00005823 }
5824#endif
5825 ie++;
5826 }
bellard31e31b82003-02-18 22:55:36 +00005827}
bellardc573ff62004-01-04 15:51:36 +00005828
blueswir1992f48a2007-10-14 16:27:31 +00005829#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00005830static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
5831{
thsaf325d32008-06-10 15:29:15 +00005832#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00005833 return ((uint64_t)word0 << 32) | word1;
5834#else
5835 return ((uint64_t)word1 << 32) | word0;
5836#endif
5837}
blueswir1992f48a2007-10-14 16:27:31 +00005838#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00005839static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
5840{
5841 return word0;
5842}
blueswir1992f48a2007-10-14 16:27:31 +00005843#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00005844
5845#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00005846static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
5847 abi_long arg2,
5848 abi_long arg3,
5849 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00005850{
Riku Voipio48e515d2011-07-12 15:40:51 +03005851 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00005852 arg2 = arg3;
5853 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03005854 }
pbrookce4defa2006-02-09 16:49:55 +00005855 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
5856}
5857#endif
5858
5859#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00005860static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
5861 abi_long arg2,
5862 abi_long arg3,
5863 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00005864{
Riku Voipio48e515d2011-07-12 15:40:51 +03005865 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00005866 arg2 = arg3;
5867 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03005868 }
pbrookce4defa2006-02-09 16:49:55 +00005869 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
5870}
5871#endif
5872
bellard579a97f2007-11-11 14:26:47 +00005873static inline abi_long target_to_host_timespec(struct timespec *host_ts,
5874 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00005875{
5876 struct target_timespec *target_ts;
5877
bellard579a97f2007-11-11 14:26:47 +00005878 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
5879 return -TARGET_EFAULT;
Peter Maydellc7e35da2016-05-19 12:01:40 +01005880 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
5881 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00005882 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00005883 return 0;
pbrook53a59602006-03-25 19:31:22 +00005884}
5885
bellard579a97f2007-11-11 14:26:47 +00005886static inline abi_long host_to_target_timespec(abi_ulong target_addr,
5887 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00005888{
5889 struct target_timespec *target_ts;
5890
bellard579a97f2007-11-11 14:26:47 +00005891 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
5892 return -TARGET_EFAULT;
Peter Maydellc7e35da2016-05-19 12:01:40 +01005893 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
5894 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00005895 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00005896 return 0;
pbrook53a59602006-03-25 19:31:22 +00005897}
5898
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11005899static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
5900 abi_ulong target_addr)
5901{
5902 struct target_itimerspec *target_itspec;
5903
5904 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
5905 return -TARGET_EFAULT;
5906 }
5907
5908 host_itspec->it_interval.tv_sec =
5909 tswapal(target_itspec->it_interval.tv_sec);
5910 host_itspec->it_interval.tv_nsec =
5911 tswapal(target_itspec->it_interval.tv_nsec);
5912 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
5913 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
5914
5915 unlock_user_struct(target_itspec, target_addr, 1);
5916 return 0;
5917}
5918
5919static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
5920 struct itimerspec *host_its)
5921{
5922 struct target_itimerspec *target_itspec;
5923
5924 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
5925 return -TARGET_EFAULT;
5926 }
5927
5928 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
5929 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
5930
5931 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
5932 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
5933
5934 unlock_user_struct(target_itspec, target_addr, 0);
5935 return 0;
5936}
5937
Peter Maydellc0659762014-08-09 15:42:32 +01005938static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
5939 abi_ulong target_addr)
5940{
5941 struct target_sigevent *target_sevp;
5942
5943 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
5944 return -TARGET_EFAULT;
5945 }
5946
5947 /* This union is awkward on 64 bit systems because it has a 32 bit
5948 * integer and a pointer in it; we follow the conversion approach
5949 * used for handling sigval types in signal.c so the guest should get
5950 * the correct value back even if we did a 64 bit byteswap and it's
5951 * using the 32 bit integer.
5952 */
5953 host_sevp->sigev_value.sival_ptr =
5954 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
5955 host_sevp->sigev_signo =
5956 target_to_host_signal(tswap32(target_sevp->sigev_signo));
5957 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
5958 host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
5959
5960 unlock_user_struct(target_sevp, target_addr, 1);
5961 return 0;
5962}
5963
Tom Musta6f6a4032014-08-12 13:53:42 -05005964#if defined(TARGET_NR_mlockall)
5965static inline int target_to_host_mlockall_arg(int arg)
5966{
5967 int result = 0;
5968
5969 if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
5970 result |= MCL_CURRENT;
5971 }
5972 if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
5973 result |= MCL_FUTURE;
5974 }
5975 return result;
5976}
5977#endif
5978
balrog6a24a772008-09-20 02:23:36 +00005979static inline abi_long host_to_target_stat64(void *cpu_env,
5980 abi_ulong target_addr,
5981 struct stat *host_st)
5982{
Alexander Graf09701192013-09-03 20:12:15 +01005983#if defined(TARGET_ARM) && defined(TARGET_ABI32)
balrog6a24a772008-09-20 02:23:36 +00005984 if (((CPUARMState *)cpu_env)->eabi) {
5985 struct target_eabi_stat64 *target_st;
5986
5987 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
5988 return -TARGET_EFAULT;
5989 memset(target_st, 0, sizeof(struct target_eabi_stat64));
5990 __put_user(host_st->st_dev, &target_st->st_dev);
5991 __put_user(host_st->st_ino, &target_st->st_ino);
5992#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5993 __put_user(host_st->st_ino, &target_st->__st_ino);
5994#endif
5995 __put_user(host_st->st_mode, &target_st->st_mode);
5996 __put_user(host_st->st_nlink, &target_st->st_nlink);
5997 __put_user(host_st->st_uid, &target_st->st_uid);
5998 __put_user(host_st->st_gid, &target_st->st_gid);
5999 __put_user(host_st->st_rdev, &target_st->st_rdev);
6000 __put_user(host_st->st_size, &target_st->st_size);
6001 __put_user(host_st->st_blksize, &target_st->st_blksize);
6002 __put_user(host_st->st_blocks, &target_st->st_blocks);
6003 __put_user(host_st->st_atime, &target_st->target_st_atime);
6004 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
6005 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
6006 unlock_user_struct(target_st, target_addr, 1);
6007 } else
6008#endif
6009 {
Stefan Weil20d155b2013-10-30 22:52:24 +01006010#if defined(TARGET_HAS_STRUCT_STAT64)
balrog6a24a772008-09-20 02:23:36 +00006011 struct target_stat64 *target_st;
Stefan Weil20d155b2013-10-30 22:52:24 +01006012#else
6013 struct target_stat *target_st;
aurel329d33b762009-04-08 23:07:05 +00006014#endif
balrog6a24a772008-09-20 02:23:36 +00006015
6016 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
6017 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00006018 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00006019 __put_user(host_st->st_dev, &target_st->st_dev);
6020 __put_user(host_st->st_ino, &target_st->st_ino);
6021#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6022 __put_user(host_st->st_ino, &target_st->__st_ino);
6023#endif
6024 __put_user(host_st->st_mode, &target_st->st_mode);
6025 __put_user(host_st->st_nlink, &target_st->st_nlink);
6026 __put_user(host_st->st_uid, &target_st->st_uid);
6027 __put_user(host_st->st_gid, &target_st->st_gid);
6028 __put_user(host_st->st_rdev, &target_st->st_rdev);
6029 /* XXX: better use of kernel struct */
6030 __put_user(host_st->st_size, &target_st->st_size);
6031 __put_user(host_st->st_blksize, &target_st->st_blksize);
6032 __put_user(host_st->st_blocks, &target_st->st_blocks);
6033 __put_user(host_st->st_atime, &target_st->target_st_atime);
6034 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
6035 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
6036 unlock_user_struct(target_st, target_addr, 1);
6037 }
6038
6039 return 0;
6040}
balrog6a24a772008-09-20 02:23:36 +00006041
pbrookbd0c5662008-05-29 14:34:11 +00006042/* ??? Using host futex calls even when target atomic operations
6043 are not really atomic probably breaks things. However implementing
6044 futexes locally would make futexes shared between multiple processes
6045 tricky. However they're probably useless because guest atomic
6046 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00006047static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
6048 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00006049{
6050 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07006051 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00006052
6053 /* ??? We assume FUTEX_* constants are the same on both host
6054 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03006055#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07006056 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03006057#else
Nathan Froyda16aae02009-08-03 08:43:29 -07006058 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03006059#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07006060 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00006061 case FUTEX_WAIT:
John Rigbycce246e2013-02-23 16:14:07 -07006062 case FUTEX_WAIT_BITSET:
pbrookbd0c5662008-05-29 14:34:11 +00006063 if (timeout) {
6064 pts = &ts;
6065 target_to_host_timespec(pts, timeout);
6066 } else {
6067 pts = NULL;
6068 }
Peter Maydelld509eeb2016-05-12 18:47:52 +01006069 return get_errno(safe_futex(g2h(uaddr), op, tswap32(val),
John Rigbycce246e2013-02-23 16:14:07 -07006070 pts, NULL, val3));
pbrookbd0c5662008-05-29 14:34:11 +00006071 case FUTEX_WAKE:
Peter Maydelld509eeb2016-05-12 18:47:52 +01006072 return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00006073 case FUTEX_FD:
Peter Maydelld509eeb2016-05-12 18:47:52 +01006074 return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00006075 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00006076 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07006077 case FUTEX_WAKE_OP:
6078 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
6079 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
6080 But the prototype takes a `struct timespec *'; insert casts
6081 to satisfy the compiler. We do not need to tswap TIMEOUT
6082 since it's not compared to guest memory. */
6083 pts = (struct timespec *)(uintptr_t) timeout;
Peter Maydelld509eeb2016-05-12 18:47:52 +01006084 return get_errno(safe_futex(g2h(uaddr), op, val, pts,
6085 g2h(uaddr2),
6086 (base_op == FUTEX_CMP_REQUEUE
6087 ? tswap32(val3)
6088 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00006089 default:
6090 return -TARGET_ENOSYS;
6091 }
6092}
Laurent Vivier0f0426f2015-09-01 22:27:33 +02006093#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6094static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
6095 abi_long handle, abi_long mount_id,
6096 abi_long flags)
6097{
6098 struct file_handle *target_fh;
6099 struct file_handle *fh;
6100 int mid = 0;
6101 abi_long ret;
6102 char *name;
6103 unsigned int size, total_size;
6104
6105 if (get_user_s32(size, handle)) {
6106 return -TARGET_EFAULT;
6107 }
6108
6109 name = lock_user_string(pathname);
6110 if (!name) {
6111 return -TARGET_EFAULT;
6112 }
6113
6114 total_size = sizeof(struct file_handle) + size;
6115 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
6116 if (!target_fh) {
6117 unlock_user(name, pathname, 0);
6118 return -TARGET_EFAULT;
6119 }
6120
6121 fh = g_malloc0(total_size);
6122 fh->handle_bytes = size;
6123
6124 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
6125 unlock_user(name, pathname, 0);
6126
6127 /* man name_to_handle_at(2):
6128 * Other than the use of the handle_bytes field, the caller should treat
6129 * the file_handle structure as an opaque data type
6130 */
6131
6132 memcpy(target_fh, fh, total_size);
6133 target_fh->handle_bytes = tswap32(fh->handle_bytes);
6134 target_fh->handle_type = tswap32(fh->handle_type);
6135 g_free(fh);
6136 unlock_user(target_fh, handle, total_size);
6137
6138 if (put_user_s32(mid, mount_id)) {
6139 return -TARGET_EFAULT;
6140 }
6141
6142 return ret;
6143
6144}
6145#endif
6146
6147#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6148static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
6149 abi_long flags)
6150{
6151 struct file_handle *target_fh;
6152 struct file_handle *fh;
6153 unsigned int size, total_size;
6154 abi_long ret;
6155
6156 if (get_user_s32(size, handle)) {
6157 return -TARGET_EFAULT;
6158 }
6159
6160 total_size = sizeof(struct file_handle) + size;
6161 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
6162 if (!target_fh) {
6163 return -TARGET_EFAULT;
6164 }
6165
Thomas Huthe9d49d52015-10-09 17:56:38 +02006166 fh = g_memdup(target_fh, total_size);
Laurent Vivier0f0426f2015-09-01 22:27:33 +02006167 fh->handle_bytes = size;
6168 fh->handle_type = tswap32(target_fh->handle_type);
6169
6170 ret = get_errno(open_by_handle_at(mount_fd, fh,
6171 target_to_host_bitmask(flags, fcntl_flags_tbl)));
6172
6173 g_free(fh);
6174
6175 unlock_user(target_fh, handle, total_size);
6176
6177 return ret;
6178}
6179#endif
pbrookbd0c5662008-05-29 14:34:11 +00006180
Laurent Viviere36800c2015-10-02 14:48:09 +02006181#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
6182
6183/* signalfd siginfo conversion */
6184
6185static void
6186host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
6187 const struct signalfd_siginfo *info)
6188{
6189 int sig = host_to_target_signal(info->ssi_signo);
6190
6191 /* linux/signalfd.h defines a ssi_addr_lsb
6192 * not defined in sys/signalfd.h but used by some kernels
6193 */
6194
6195#ifdef BUS_MCEERR_AO
6196 if (tinfo->ssi_signo == SIGBUS &&
6197 (tinfo->ssi_code == BUS_MCEERR_AR ||
6198 tinfo->ssi_code == BUS_MCEERR_AO)) {
6199 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
6200 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
6201 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
6202 }
6203#endif
6204
6205 tinfo->ssi_signo = tswap32(sig);
6206 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
6207 tinfo->ssi_code = tswap32(info->ssi_code);
6208 tinfo->ssi_pid = tswap32(info->ssi_pid);
6209 tinfo->ssi_uid = tswap32(info->ssi_uid);
6210 tinfo->ssi_fd = tswap32(info->ssi_fd);
6211 tinfo->ssi_tid = tswap32(info->ssi_tid);
6212 tinfo->ssi_band = tswap32(info->ssi_band);
6213 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
6214 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
6215 tinfo->ssi_status = tswap32(info->ssi_status);
6216 tinfo->ssi_int = tswap32(info->ssi_int);
6217 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
6218 tinfo->ssi_utime = tswap64(info->ssi_utime);
6219 tinfo->ssi_stime = tswap64(info->ssi_stime);
6220 tinfo->ssi_addr = tswap64(info->ssi_addr);
6221}
6222
Laurent Vivier5d4d3662015-10-28 21:40:43 +01006223static abi_long host_to_target_data_signalfd(void *buf, size_t len)
Laurent Viviere36800c2015-10-02 14:48:09 +02006224{
6225 int i;
6226
6227 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
6228 host_to_target_signalfd_siginfo(buf + i, buf + i);
6229 }
6230
6231 return len;
6232}
6233
6234static TargetFdTrans target_signalfd_trans = {
Laurent Vivier5d4d3662015-10-28 21:40:43 +01006235 .host_to_target_data = host_to_target_data_signalfd,
Laurent Viviere36800c2015-10-02 14:48:09 +02006236};
6237
6238static abi_long do_signalfd4(int fd, abi_long mask, int flags)
6239{
6240 int host_flags;
6241 target_sigset_t *target_mask;
6242 sigset_t host_mask;
6243 abi_long ret;
6244
6245 if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
6246 return -TARGET_EINVAL;
6247 }
6248 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
6249 return -TARGET_EFAULT;
6250 }
6251
6252 target_to_host_sigset(&host_mask, target_mask);
6253
6254 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
6255
6256 ret = get_errno(signalfd(fd, &host_mask, host_flags));
6257 if (ret >= 0) {
6258 fd_trans_register(ret, &target_signalfd_trans);
6259 }
6260
6261 unlock_user_struct(target_mask, mask, 0);
6262
6263 return ret;
6264}
6265#endif
6266
pbrook1d9d8b52009-04-16 15:17:02 +00006267/* Map host to target signal numbers for the wait family of syscalls.
6268 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07006269int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00006270{
6271 if (WIFSIGNALED(status)) {
6272 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
6273 }
6274 if (WIFSTOPPED(status)) {
6275 return (host_to_target_signal(WSTOPSIG(status)) << 8)
6276 | (status & 0xff);
6277 }
6278 return status;
6279}
6280
Wim Vander Schelden76b94242014-06-18 11:02:39 +02006281static int open_self_cmdline(void *cpu_env, int fd)
6282{
6283 int fd_orig = -1;
6284 bool word_skipped = false;
6285
6286 fd_orig = open("/proc/self/cmdline", O_RDONLY);
6287 if (fd_orig < 0) {
6288 return fd_orig;
6289 }
6290
6291 while (true) {
6292 ssize_t nb_read;
6293 char buf[128];
6294 char *cp_buf = buf;
6295
6296 nb_read = read(fd_orig, buf, sizeof(buf));
6297 if (nb_read < 0) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01006298 int e = errno;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02006299 fd_orig = close(fd_orig);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01006300 errno = e;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02006301 return -1;
6302 } else if (nb_read == 0) {
6303 break;
6304 }
6305
6306 if (!word_skipped) {
6307 /* Skip the first string, which is the path to qemu-*-static
6308 instead of the actual command. */
6309 cp_buf = memchr(buf, 0, sizeof(buf));
6310 if (cp_buf) {
6311 /* Null byte found, skip one string */
6312 cp_buf++;
6313 nb_read -= cp_buf - buf;
6314 word_skipped = true;
6315 }
6316 }
6317
6318 if (word_skipped) {
6319 if (write(fd, cp_buf, nb_read) != nb_read) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01006320 int e = errno;
zhanghailiang680dfde2014-08-22 16:23:51 +08006321 close(fd_orig);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01006322 errno = e;
Wim Vander Schelden76b94242014-06-18 11:02:39 +02006323 return -1;
6324 }
6325 }
6326 }
6327
6328 return close(fd_orig);
6329}
6330
Alexander Graf36c08d42011-11-02 20:23:24 +01006331static int open_self_maps(void *cpu_env, int fd)
6332{
Andreas Färber0429a972013-08-26 18:14:44 +02006333 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
6334 TaskState *ts = cpu->opaque;
Alexander Graf1a49ef22012-05-01 16:30:28 +01006335 FILE *fp;
6336 char *line = NULL;
6337 size_t len = 0;
6338 ssize_t read;
Alexander Graf36c08d42011-11-02 20:23:24 +01006339
Alexander Graf1a49ef22012-05-01 16:30:28 +01006340 fp = fopen("/proc/self/maps", "r");
6341 if (fp == NULL) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01006342 return -1;
Alexander Graf1a49ef22012-05-01 16:30:28 +01006343 }
6344
6345 while ((read = getline(&line, &len, fp)) != -1) {
6346 int fields, dev_maj, dev_min, inode;
6347 uint64_t min, max, offset;
6348 char flag_r, flag_w, flag_x, flag_p;
6349 char path[512] = "";
6350 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
6351 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
6352 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
6353
6354 if ((fields < 10) || (fields > 11)) {
6355 continue;
6356 }
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04006357 if (h2g_valid(min)) {
6358 int flags = page_get_flags(h2g(min));
6359 max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX);
6360 if (page_check_range(h2g(min), max - min, flags) == -1) {
6361 continue;
6362 }
6363 if (h2g(min) == ts->info->stack_limit) {
6364 pstrcpy(path, sizeof(path), " [stack]");
6365 }
Alexander Graf1a49ef22012-05-01 16:30:28 +01006366 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
Christophe Lyone24fed42013-04-02 14:03:38 +02006367 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
Mikhail Ilyind67f4aa2014-08-05 17:33:51 +04006368 h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
Alexander Graf1a49ef22012-05-01 16:30:28 +01006369 flag_x, flag_p, offset, dev_maj, dev_min, inode,
Christophe Lyone24fed42013-04-02 14:03:38 +02006370 path[0] ? " " : "", path);
Alexander Graf1a49ef22012-05-01 16:30:28 +01006371 }
6372 }
6373
6374 free(line);
6375 fclose(fp);
6376
Alexander Graf36c08d42011-11-02 20:23:24 +01006377 return 0;
6378}
6379
Alexander Graf480b8e72011-11-02 20:23:25 +01006380static int open_self_stat(void *cpu_env, int fd)
6381{
Andreas Färber0429a972013-08-26 18:14:44 +02006382 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
6383 TaskState *ts = cpu->opaque;
Alexander Graf480b8e72011-11-02 20:23:25 +01006384 abi_ulong start_stack = ts->info->start_stack;
6385 int i;
6386
6387 for (i = 0; i < 44; i++) {
6388 char buf[128];
6389 int len;
6390 uint64_t val = 0;
6391
Fabio Erculianie0e65be2012-01-03 09:38:34 +00006392 if (i == 0) {
6393 /* pid */
6394 val = getpid();
6395 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
6396 } else if (i == 1) {
6397 /* app name */
6398 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
6399 } else if (i == 27) {
6400 /* stack bottom */
6401 val = start_stack;
6402 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
6403 } else {
6404 /* for the rest, there is MasterCard */
6405 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
Alexander Graf480b8e72011-11-02 20:23:25 +01006406 }
Fabio Erculianie0e65be2012-01-03 09:38:34 +00006407
Alexander Graf480b8e72011-11-02 20:23:25 +01006408 len = strlen(buf);
6409 if (write(fd, buf, len) != len) {
6410 return -1;
6411 }
6412 }
6413
6414 return 0;
6415}
6416
Alexander Graf257450e2011-11-02 20:23:26 +01006417static int open_self_auxv(void *cpu_env, int fd)
6418{
Andreas Färber0429a972013-08-26 18:14:44 +02006419 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
6420 TaskState *ts = cpu->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01006421 abi_ulong auxv = ts->info->saved_auxv;
6422 abi_ulong len = ts->info->auxv_len;
6423 char *ptr;
6424
6425 /*
6426 * Auxiliary vector is stored in target process stack.
6427 * read in whole auxv vector and copy it to file
6428 */
6429 ptr = lock_user(VERIFY_READ, auxv, len, 0);
6430 if (ptr != NULL) {
6431 while (len > 0) {
6432 ssize_t r;
6433 r = write(fd, ptr, len);
6434 if (r <= 0) {
6435 break;
6436 }
6437 len -= r;
6438 ptr += r;
6439 }
6440 lseek(fd, 0, SEEK_SET);
6441 unlock_user(ptr, auxv, len);
6442 }
6443
6444 return 0;
6445}
6446
Andreas Schwab463d8e72013-07-02 14:04:12 +01006447static int is_proc_myself(const char *filename, const char *entry)
6448{
6449 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
6450 filename += strlen("/proc/");
6451 if (!strncmp(filename, "self/", strlen("self/"))) {
6452 filename += strlen("self/");
6453 } else if (*filename >= '1' && *filename <= '9') {
6454 char myself[80];
6455 snprintf(myself, sizeof(myself), "%d/", getpid());
6456 if (!strncmp(filename, myself, strlen(myself))) {
6457 filename += strlen(myself);
6458 } else {
6459 return 0;
6460 }
6461 } else {
6462 return 0;
6463 }
6464 if (!strcmp(filename, entry)) {
6465 return 1;
6466 }
6467 }
6468 return 0;
6469}
6470
Laurent Vivierde6b9932013-08-30 01:46:40 +02006471#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
6472static int is_proc(const char *filename, const char *entry)
6473{
6474 return strcmp(filename, entry) == 0;
6475}
6476
6477static int open_net_route(void *cpu_env, int fd)
6478{
6479 FILE *fp;
6480 char *line = NULL;
6481 size_t len = 0;
6482 ssize_t read;
6483
6484 fp = fopen("/proc/net/route", "r");
6485 if (fp == NULL) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01006486 return -1;
Laurent Vivierde6b9932013-08-30 01:46:40 +02006487 }
6488
6489 /* read header */
6490
6491 read = getline(&line, &len, fp);
6492 dprintf(fd, "%s", line);
6493
6494 /* read routes */
6495
6496 while ((read = getline(&line, &len, fp)) != -1) {
6497 char iface[16];
6498 uint32_t dest, gw, mask;
6499 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
6500 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
6501 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
6502 &mask, &mtu, &window, &irtt);
6503 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
6504 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
6505 metric, tswap32(mask), mtu, window, irtt);
6506 }
6507
6508 free(line);
6509 fclose(fp);
6510
6511 return 0;
6512}
6513#endif
6514
Riku Voipio0b2effd2014-08-06 10:36:37 +03006515static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
Alexander Graf3be14d02011-11-02 20:23:23 +01006516{
6517 struct fake_open {
6518 const char *filename;
6519 int (*fill)(void *cpu_env, int fd);
Laurent Vivierde6b9932013-08-30 01:46:40 +02006520 int (*cmp)(const char *s1, const char *s2);
Alexander Graf3be14d02011-11-02 20:23:23 +01006521 };
6522 const struct fake_open *fake_open;
6523 static const struct fake_open fakes[] = {
Laurent Vivierde6b9932013-08-30 01:46:40 +02006524 { "maps", open_self_maps, is_proc_myself },
6525 { "stat", open_self_stat, is_proc_myself },
6526 { "auxv", open_self_auxv, is_proc_myself },
Wim Vander Schelden76b94242014-06-18 11:02:39 +02006527 { "cmdline", open_self_cmdline, is_proc_myself },
Laurent Vivierde6b9932013-08-30 01:46:40 +02006528#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
6529 { "/proc/net/route", open_net_route, is_proc },
6530#endif
6531 { NULL, NULL, NULL }
Alexander Graf3be14d02011-11-02 20:23:23 +01006532 };
6533
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03006534 if (is_proc_myself(pathname, "exe")) {
6535 int execfd = qemu_getauxval(AT_EXECFD);
Timothy E Baldwinc10a0732016-05-12 18:47:48 +01006536 return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
Maxim Ostapenkoaa07f5e2014-05-02 11:17:07 +03006537 }
6538
Alexander Graf3be14d02011-11-02 20:23:23 +01006539 for (fake_open = fakes; fake_open->filename; fake_open++) {
Laurent Vivierde6b9932013-08-30 01:46:40 +02006540 if (fake_open->cmp(pathname, fake_open->filename)) {
Alexander Graf3be14d02011-11-02 20:23:23 +01006541 break;
6542 }
6543 }
6544
6545 if (fake_open->filename) {
6546 const char *tmpdir;
6547 char filename[PATH_MAX];
6548 int fd, r;
6549
6550 /* create temporary file to map stat to */
6551 tmpdir = getenv("TMPDIR");
6552 if (!tmpdir)
6553 tmpdir = "/tmp";
6554 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
6555 fd = mkstemp(filename);
6556 if (fd < 0) {
6557 return fd;
6558 }
6559 unlink(filename);
6560
6561 if ((r = fake_open->fill(cpu_env, fd))) {
Peter Maydella3ca7bb2016-05-12 18:47:26 +01006562 int e = errno;
Alexander Graf3be14d02011-11-02 20:23:23 +01006563 close(fd);
Peter Maydella3ca7bb2016-05-12 18:47:26 +01006564 errno = e;
Alexander Graf3be14d02011-11-02 20:23:23 +01006565 return r;
6566 }
6567 lseek(fd, 0, SEEK_SET);
6568
6569 return fd;
6570 }
6571
Timothy E Baldwinc10a0732016-05-12 18:47:48 +01006572 return safe_openat(dirfd, path(pathname), flags, mode);
Alexander Graf3be14d02011-11-02 20:23:23 +01006573}
6574
Alexander Grafaecc8862014-11-10 21:33:03 +01006575#define TIMER_MAGIC 0x0caf0000
6576#define TIMER_MAGIC_MASK 0xffff0000
6577
6578/* Convert QEMU provided timer ID back to internal 16bit index format */
6579static target_timer_t get_timer_id(abi_long arg)
6580{
6581 target_timer_t timerid = arg;
6582
6583 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
6584 return -TARGET_EINVAL;
6585 }
6586
6587 timerid &= 0xffff;
6588
6589 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
6590 return -TARGET_EINVAL;
6591 }
6592
6593 return timerid;
6594}
6595
ths0da46a62007-10-20 20:23:07 +00006596/* do_syscall() should always have a single exit point at the end so
6597 that actions, such as logging of syscall results, can be performed.
6598 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00006599abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
6600 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01006601 abi_long arg5, abi_long arg6, abi_long arg7,
6602 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00006603{
Andreas Färber182735e2013-05-29 22:29:20 +02006604 CPUState *cpu = ENV_GET_CPU(cpu_env);
blueswir1992f48a2007-10-14 16:27:31 +00006605 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00006606 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00006607 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00006608 void *p;
ths3b46e622007-09-17 08:09:54 +00006609
Timothy E Baldwin71a8f7f2016-05-12 18:47:45 +01006610#if defined(DEBUG_ERESTARTSYS)
6611 /* Debug-only code for exercising the syscall-restart code paths
6612 * in the per-architecture cpu main loops: restart every syscall
6613 * the guest makes once before letting it through.
6614 */
6615 {
6616 static int flag;
6617
6618 flag = !flag;
6619 if (flag) {
6620 return -TARGET_ERESTARTSYS;
6621 }
6622 }
6623#endif
6624
bellard72f03902003-02-18 23:33:18 +00006625#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00006626 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00006627#endif
thsb92c47c2007-11-01 00:07:38 +00006628 if(do_strace)
6629 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
6630
bellard31e31b82003-02-18 22:55:36 +00006631 switch(num) {
6632 case TARGET_NR_exit:
Andreas Färber9b056fc2013-06-24 23:53:10 +02006633 /* In old applications this may be used to implement _exit(2).
6634 However in threaded applictions it is used for thread termination,
6635 and _exit_group is used for application termination.
6636 Do thread termination if we have more then one thread. */
6637 /* FIXME: This probably breaks if a signal arrives. We should probably
6638 be disabling signals. */
Andreas Färberbdc44642013-06-24 23:50:24 +02006639 if (CPU_NEXT(first_cpu)) {
Andreas Färber9b056fc2013-06-24 23:53:10 +02006640 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00006641
Andreas Färber9b056fc2013-06-24 23:53:10 +02006642 cpu_list_lock();
Andreas Färber9b056fc2013-06-24 23:53:10 +02006643 /* Remove the CPU from the list. */
Andreas Färberbdc44642013-06-24 23:50:24 +02006644 QTAILQ_REMOVE(&cpus, cpu, node);
Andreas Färber9b056fc2013-06-24 23:53:10 +02006645 cpu_list_unlock();
Andreas Färber0429a972013-08-26 18:14:44 +02006646 ts = cpu->opaque;
Andreas Färber9b056fc2013-06-24 23:53:10 +02006647 if (ts->child_tidptr) {
6648 put_user_u32(0, ts->child_tidptr);
6649 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
6650 NULL, NULL, 0);
6651 }
Andreas Färbera2247f82013-06-09 19:47:04 +02006652 thread_cpu = NULL;
Andreas Färber0429a972013-08-26 18:14:44 +02006653 object_unref(OBJECT(cpu));
Andreas Färber9b056fc2013-06-24 23:53:10 +02006654 g_free(ts);
Emilio G. Cota70903762015-08-23 20:23:41 -04006655 rcu_unregister_thread();
Andreas Färber9b056fc2013-06-24 23:53:10 +02006656 pthread_exit(NULL);
6657 }
Juan Quintela9788c9c2009-07-27 16:13:02 +02006658#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00006659 _mcleanup();
6660#endif
bellarde9009672005-04-26 20:42:36 +00006661 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00006662 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00006663 ret = 0; /* avoid warning */
6664 break;
6665 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00006666 if (arg3 == 0)
6667 ret = 0;
6668 else {
6669 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6670 goto efault;
Timothy E Baldwin50afd022016-05-12 18:47:47 +01006671 ret = get_errno(safe_read(arg1, p, arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02006672 if (ret >= 0 &&
Laurent Vivier5d4d3662015-10-28 21:40:43 +01006673 fd_trans_host_to_target_data(arg1)) {
6674 ret = fd_trans_host_to_target_data(arg1)(p, ret);
Laurent Viviere36800c2015-10-02 14:48:09 +02006675 }
aurel3238d840e2009-01-30 19:48:17 +00006676 unlock_user(p, arg2, ret);
6677 }
bellard31e31b82003-02-18 22:55:36 +00006678 break;
6679 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00006680 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6681 goto efault;
Timothy E Baldwin50afd022016-05-12 18:47:47 +01006682 ret = get_errno(safe_write(arg1, p, arg3));
pbrook53a59602006-03-25 19:31:22 +00006683 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00006684 break;
Chen Gang704eff62015-08-21 05:37:33 +08006685#ifdef TARGET_NR_open
bellard31e31b82003-02-18 22:55:36 +00006686 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00006687 if (!(p = lock_user_string(arg1)))
6688 goto efault;
Riku Voipio0b2effd2014-08-06 10:36:37 +03006689 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
6690 target_to_host_bitmask(arg2, fcntl_flags_tbl),
6691 arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02006692 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00006693 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006694 break;
Chen Gang704eff62015-08-21 05:37:33 +08006695#endif
ths82424832007-09-24 09:21:55 +00006696 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00006697 if (!(p = lock_user_string(arg2)))
6698 goto efault;
Riku Voipio0b2effd2014-08-06 10:36:37 +03006699 ret = get_errno(do_openat(cpu_env, arg1, p,
6700 target_to_host_bitmask(arg3, fcntl_flags_tbl),
6701 arg4));
Laurent Viviere36800c2015-10-02 14:48:09 +02006702 fd_trans_unregister(ret);
bellard579a97f2007-11-11 14:26:47 +00006703 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00006704 break;
Laurent Vivier0f0426f2015-09-01 22:27:33 +02006705#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6706 case TARGET_NR_name_to_handle_at:
6707 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
6708 break;
6709#endif
6710#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6711 case TARGET_NR_open_by_handle_at:
6712 ret = do_open_by_handle_at(arg1, arg2, arg3);
Laurent Viviere36800c2015-10-02 14:48:09 +02006713 fd_trans_unregister(ret);
Laurent Vivier0f0426f2015-09-01 22:27:33 +02006714 break;
6715#endif
bellard31e31b82003-02-18 22:55:36 +00006716 case TARGET_NR_close:
Laurent Viviere36800c2015-10-02 14:48:09 +02006717 fd_trans_unregister(arg1);
bellard31e31b82003-02-18 22:55:36 +00006718 ret = get_errno(close(arg1));
6719 break;
6720 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00006721 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00006722 break;
Chen Gang704eff62015-08-21 05:37:33 +08006723#ifdef TARGET_NR_fork
bellard31e31b82003-02-18 22:55:36 +00006724 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00006725 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00006726 break;
Chen Gang704eff62015-08-21 05:37:33 +08006727#endif
thse5febef2007-04-01 18:31:35 +00006728#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00006729 case TARGET_NR_waitpid:
6730 {
pbrook53a59602006-03-25 19:31:22 +00006731 int status;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +01006732 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
Alexander Graf53795572011-11-24 00:44:43 +01006733 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00006734 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00006735 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006736 }
6737 break;
thse5febef2007-04-01 18:31:35 +00006738#endif
pbrookf0cbb612008-05-30 18:20:05 +00006739#ifdef TARGET_NR_waitid
6740 case TARGET_NR_waitid:
6741 {
6742 siginfo_t info;
6743 info.si_pid = 0;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +01006744 ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
pbrookf0cbb612008-05-30 18:20:05 +00006745 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006746 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00006747 goto efault;
6748 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05006749 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00006750 }
6751 }
6752 break;
6753#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006754#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006755 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00006756 if (!(p = lock_user_string(arg1)))
6757 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006758 ret = get_errno(creat(p, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02006759 fd_trans_unregister(ret);
pbrook53a59602006-03-25 19:31:22 +00006760 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006761 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006762#endif
Chen Gang704eff62015-08-21 05:37:33 +08006763#ifdef TARGET_NR_link
bellard31e31b82003-02-18 22:55:36 +00006764 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00006765 {
6766 void * p2;
6767 p = lock_user_string(arg1);
6768 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006769 if (!p || !p2)
6770 ret = -TARGET_EFAULT;
6771 else
6772 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006773 unlock_user(p2, arg2, 0);
6774 unlock_user(p, arg1, 0);
6775 }
bellard31e31b82003-02-18 22:55:36 +00006776 break;
Chen Gang704eff62015-08-21 05:37:33 +08006777#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006778#if defined(TARGET_NR_linkat)
ths64f0ce42007-09-24 09:25:06 +00006779 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00006780 {
6781 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00006782 if (!arg2 || !arg4)
6783 goto efault;
ths64f0ce42007-09-24 09:25:06 +00006784 p = lock_user_string(arg2);
6785 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00006786 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006787 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00006788 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01006789 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00006790 unlock_user(p, arg2, 0);
6791 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00006792 }
6793 break;
6794#endif
Chen Gang704eff62015-08-21 05:37:33 +08006795#ifdef TARGET_NR_unlink
bellard31e31b82003-02-18 22:55:36 +00006796 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00006797 if (!(p = lock_user_string(arg1)))
6798 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006799 ret = get_errno(unlink(p));
6800 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006801 break;
Chen Gang704eff62015-08-21 05:37:33 +08006802#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006803#if defined(TARGET_NR_unlinkat)
ths8170f562007-09-24 09:24:11 +00006804 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00006805 if (!(p = lock_user_string(arg2)))
6806 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006807 ret = get_errno(unlinkat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00006808 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00006809 break;
balrogb7d35e62007-12-12 00:40:24 +00006810#endif
bellard31e31b82003-02-18 22:55:36 +00006811 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00006812 {
6813 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00006814 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00006815 abi_ulong gp;
6816 abi_ulong guest_argp;
6817 abi_ulong guest_envp;
6818 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00006819 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006820 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00006821
bellardf7341ff2003-03-30 21:00:25 +00006822 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00006823 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00006824 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00006825 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00006826 goto efault;
ths03aa1972007-12-02 06:28:08 +00006827 if (!addr)
bellard2f619692007-11-16 10:46:05 +00006828 break;
bellard7854b052003-03-29 17:22:23 +00006829 argc++;
bellard2f619692007-11-16 10:46:05 +00006830 }
bellardf7341ff2003-03-30 21:00:25 +00006831 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00006832 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00006833 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00006834 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00006835 goto efault;
ths03aa1972007-12-02 06:28:08 +00006836 if (!addr)
bellard2f619692007-11-16 10:46:05 +00006837 break;
bellard7854b052003-03-29 17:22:23 +00006838 envc++;
bellard2f619692007-11-16 10:46:05 +00006839 }
bellard7854b052003-03-29 17:22:23 +00006840
bellardf7341ff2003-03-30 21:00:25 +00006841 argp = alloca((argc + 1) * sizeof(void *));
6842 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00006843
pbrookda94d262008-05-30 18:24:00 +00006844 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00006845 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006846 if (get_user_ual(addr, gp))
6847 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00006848 if (!addr)
6849 break;
bellard2f619692007-11-16 10:46:05 +00006850 if (!(*q = lock_user_string(addr)))
6851 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006852 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00006853 }
bellardf7341ff2003-03-30 21:00:25 +00006854 *q = NULL;
6855
pbrookda94d262008-05-30 18:24:00 +00006856 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00006857 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006858 if (get_user_ual(addr, gp))
6859 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00006860 if (!addr)
6861 break;
bellard2f619692007-11-16 10:46:05 +00006862 if (!(*q = lock_user_string(addr)))
6863 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01006864 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00006865 }
bellardf7341ff2003-03-30 21:00:25 +00006866 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00006867
bellard2f619692007-11-16 10:46:05 +00006868 if (!(p = lock_user_string(arg1)))
6869 goto execve_efault;
Timothy E Baldwinffdcbe22016-05-12 18:47:50 +01006870 /* Although execve() is not an interruptible syscall it is
6871 * a special case where we must use the safe_syscall wrapper:
6872 * if we allow a signal to happen before we make the host
6873 * syscall then we will 'lose' it, because at the point of
6874 * execve the process leaves QEMU's control. So we use the
6875 * safe syscall wrapper to ensure that we either take the
6876 * signal as a guest signal, or else it does not happen
6877 * before the execve completes and makes it the other
6878 * program's problem.
6879 */
6880 ret = get_errno(safe_execve(p, argp, envp));
pbrook53a59602006-03-25 19:31:22 +00006881 unlock_user(p, arg1, 0);
6882
bellard2f619692007-11-16 10:46:05 +00006883 goto execve_end;
6884
6885 execve_efault:
6886 ret = -TARGET_EFAULT;
6887
6888 execve_end:
pbrook53a59602006-03-25 19:31:22 +00006889 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00006890 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006891 if (get_user_ual(addr, gp)
6892 || !addr)
6893 break;
pbrook53a59602006-03-25 19:31:22 +00006894 unlock_user(*q, addr, 0);
6895 }
6896 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00006897 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00006898 if (get_user_ual(addr, gp)
6899 || !addr)
6900 break;
pbrook53a59602006-03-25 19:31:22 +00006901 unlock_user(*q, addr, 0);
6902 }
bellard7854b052003-03-29 17:22:23 +00006903 }
bellard31e31b82003-02-18 22:55:36 +00006904 break;
6905 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00006906 if (!(p = lock_user_string(arg1)))
6907 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006908 ret = get_errno(chdir(p));
6909 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006910 break;
bellarda315a142005-01-30 22:59:18 +00006911#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00006912 case TARGET_NR_time:
6913 {
pbrook53a59602006-03-25 19:31:22 +00006914 time_t host_time;
6915 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00006916 if (!is_error(ret)
6917 && arg1
6918 && put_user_sal(host_time, arg1))
6919 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006920 }
6921 break;
bellarda315a142005-01-30 22:59:18 +00006922#endif
Chen Gang704eff62015-08-21 05:37:33 +08006923#ifdef TARGET_NR_mknod
bellard31e31b82003-02-18 22:55:36 +00006924 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00006925 if (!(p = lock_user_string(arg1)))
6926 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006927 ret = get_errno(mknod(p, arg2, arg3));
6928 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006929 break;
Chen Gang704eff62015-08-21 05:37:33 +08006930#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01006931#if defined(TARGET_NR_mknodat)
ths75ac37a2007-09-24 09:23:05 +00006932 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00006933 if (!(p = lock_user_string(arg2)))
6934 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006935 ret = get_errno(mknodat(arg1, p, arg3, arg4));
bellard579a97f2007-11-11 14:26:47 +00006936 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00006937 break;
6938#endif
Chen Gang704eff62015-08-21 05:37:33 +08006939#ifdef TARGET_NR_chmod
bellard31e31b82003-02-18 22:55:36 +00006940 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00006941 if (!(p = lock_user_string(arg1)))
6942 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006943 ret = get_errno(chmod(p, arg2));
6944 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006945 break;
Chen Gang704eff62015-08-21 05:37:33 +08006946#endif
bellardebc05482003-09-30 21:08:41 +00006947#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00006948 case TARGET_NR_break:
6949 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006950#endif
6951#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00006952 case TARGET_NR_oldstat:
6953 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006954#endif
bellard31e31b82003-02-18 22:55:36 +00006955 case TARGET_NR_lseek:
6956 ret = get_errno(lseek(arg1, arg2, arg3));
6957 break;
Richard Henderson92317332010-05-03 10:07:53 -07006958#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
6959 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00006960 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07006961 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00006962 ret = get_errno(getpid());
6963 break;
Richard Henderson92317332010-05-03 10:07:53 -07006964#endif
6965#ifdef TARGET_NR_getpid
6966 case TARGET_NR_getpid:
6967 ret = get_errno(getpid());
6968 break;
6969#endif
bellard31e31b82003-02-18 22:55:36 +00006970 case TARGET_NR_mount:
Paul Burton356d7712014-06-22 11:25:37 +01006971 {
6972 /* need to look at the data field */
6973 void *p2, *p3;
6974
6975 if (arg1) {
6976 p = lock_user_string(arg1);
6977 if (!p) {
6978 goto efault;
6979 }
6980 } else {
6981 p = NULL;
6982 }
6983
6984 p2 = lock_user_string(arg2);
6985 if (!p2) {
6986 if (arg1) {
6987 unlock_user(p, arg1, 0);
6988 }
6989 goto efault;
6990 }
6991
6992 if (arg3) {
6993 p3 = lock_user_string(arg3);
6994 if (!p3) {
6995 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00006996 unlock_user(p, arg1, 0);
Paul Burton356d7712014-06-22 11:25:37 +01006997 }
6998 unlock_user(p2, arg2, 0);
6999 goto efault;
7000 }
7001 } else {
7002 p3 = NULL;
7003 }
7004
7005 /* FIXME - arg5 should be locked, but it isn't clear how to
7006 * do that since it's not guaranteed to be a NULL-terminated
7007 * string.
7008 */
7009 if (!arg5) {
7010 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
7011 } else {
7012 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
7013 }
7014 ret = get_errno(ret);
7015
7016 if (arg1) {
7017 unlock_user(p, arg1, 0);
7018 }
7019 unlock_user(p2, arg2, 0);
7020 if (arg3) {
7021 unlock_user(p3, arg3, 0);
7022 }
7023 }
7024 break;
thse5febef2007-04-01 18:31:35 +00007025#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00007026 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00007027 if (!(p = lock_user_string(arg1)))
7028 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007029 ret = get_errno(umount(p));
7030 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007031 break;
thse5febef2007-04-01 18:31:35 +00007032#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007033#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007034 case TARGET_NR_stime:
7035 {
pbrook53a59602006-03-25 19:31:22 +00007036 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00007037 if (get_user_sal(host_time, arg1))
7038 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007039 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00007040 }
7041 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007042#endif
bellard31e31b82003-02-18 22:55:36 +00007043 case TARGET_NR_ptrace:
7044 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00007045#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007046 case TARGET_NR_alarm:
7047 ret = alarm(arg1);
7048 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007049#endif
bellardebc05482003-09-30 21:08:41 +00007050#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00007051 case TARGET_NR_oldfstat:
7052 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007053#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007054#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007055 case TARGET_NR_pause:
7056 ret = get_errno(pause());
7057 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007058#endif
thse5febef2007-04-01 18:31:35 +00007059#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00007060 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00007061 {
pbrook53a59602006-03-25 19:31:22 +00007062 struct utimbuf tbuf, *host_tbuf;
7063 struct target_utimbuf *target_tbuf;
7064 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00007065 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
7066 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007067 tbuf.actime = tswapal(target_tbuf->actime);
7068 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00007069 unlock_user_struct(target_tbuf, arg2, 0);
7070 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00007071 } else {
pbrook53a59602006-03-25 19:31:22 +00007072 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00007073 }
bellard579a97f2007-11-11 14:26:47 +00007074 if (!(p = lock_user_string(arg1)))
7075 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007076 ret = get_errno(utime(p, host_tbuf));
7077 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00007078 }
7079 break;
thse5febef2007-04-01 18:31:35 +00007080#endif
Chen Gang704eff62015-08-21 05:37:33 +08007081#ifdef TARGET_NR_utimes
bellard978a66f2004-12-06 22:58:05 +00007082 case TARGET_NR_utimes:
7083 {
bellard978a66f2004-12-06 22:58:05 +00007084 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00007085 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00007086 if (copy_from_user_timeval(&tv[0], arg2)
7087 || copy_from_user_timeval(&tv[1],
7088 arg2 + sizeof(struct target_timeval)))
7089 goto efault;
bellard978a66f2004-12-06 22:58:05 +00007090 tvp = tv;
7091 } else {
7092 tvp = NULL;
7093 }
bellard579a97f2007-11-11 14:26:47 +00007094 if (!(p = lock_user_string(arg1)))
7095 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007096 ret = get_errno(utimes(p, tvp));
7097 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00007098 }
7099 break;
Chen Gang704eff62015-08-21 05:37:33 +08007100#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007101#if defined(TARGET_NR_futimesat)
balrogac8a6552008-09-20 02:25:39 +00007102 case TARGET_NR_futimesat:
7103 {
7104 struct timeval *tvp, tv[2];
7105 if (arg3) {
7106 if (copy_from_user_timeval(&tv[0], arg3)
7107 || copy_from_user_timeval(&tv[1],
7108 arg3 + sizeof(struct target_timeval)))
7109 goto efault;
7110 tvp = tv;
7111 } else {
7112 tvp = NULL;
7113 }
7114 if (!(p = lock_user_string(arg2)))
7115 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007116 ret = get_errno(futimesat(arg1, path(p), tvp));
balrogac8a6552008-09-20 02:25:39 +00007117 unlock_user(p, arg2, 0);
7118 }
7119 break;
7120#endif
bellardebc05482003-09-30 21:08:41 +00007121#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00007122 case TARGET_NR_stty:
7123 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007124#endif
7125#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00007126 case TARGET_NR_gtty:
7127 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007128#endif
Chen Gang704eff62015-08-21 05:37:33 +08007129#ifdef TARGET_NR_access
bellard31e31b82003-02-18 22:55:36 +00007130 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00007131 if (!(p = lock_user_string(arg1)))
7132 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02007133 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00007134 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007135 break;
Chen Gang704eff62015-08-21 05:37:33 +08007136#endif
ths92a34c12007-09-24 09:27:49 +00007137#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
7138 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00007139 if (!(p = lock_user_string(arg2)))
7140 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007141 ret = get_errno(faccessat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00007142 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00007143 break;
7144#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007145#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007146 case TARGET_NR_nice:
7147 ret = get_errno(nice(arg1));
7148 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007149#endif
bellardebc05482003-09-30 21:08:41 +00007150#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00007151 case TARGET_NR_ftime:
7152 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007153#endif
bellard31e31b82003-02-18 22:55:36 +00007154 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00007155 sync();
7156 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00007157 break;
7158 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00007159 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00007160 break;
Chen Gang704eff62015-08-21 05:37:33 +08007161#ifdef TARGET_NR_rename
bellard31e31b82003-02-18 22:55:36 +00007162 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00007163 {
7164 void *p2;
7165 p = lock_user_string(arg1);
7166 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00007167 if (!p || !p2)
7168 ret = -TARGET_EFAULT;
7169 else
7170 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00007171 unlock_user(p2, arg2, 0);
7172 unlock_user(p, arg1, 0);
7173 }
bellard31e31b82003-02-18 22:55:36 +00007174 break;
Chen Gang704eff62015-08-21 05:37:33 +08007175#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007176#if defined(TARGET_NR_renameat)
ths722183f2007-09-24 09:24:37 +00007177 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00007178 {
bellard579a97f2007-11-11 14:26:47 +00007179 void *p2;
ths722183f2007-09-24 09:24:37 +00007180 p = lock_user_string(arg2);
7181 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00007182 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00007183 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00007184 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01007185 ret = get_errno(renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00007186 unlock_user(p2, arg4, 0);
7187 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00007188 }
7189 break;
7190#endif
Chen Gang704eff62015-08-21 05:37:33 +08007191#ifdef TARGET_NR_mkdir
bellard31e31b82003-02-18 22:55:36 +00007192 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00007193 if (!(p = lock_user_string(arg1)))
7194 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007195 ret = get_errno(mkdir(p, arg2));
7196 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007197 break;
Chen Gang704eff62015-08-21 05:37:33 +08007198#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007199#if defined(TARGET_NR_mkdirat)
ths4472ad02007-09-24 09:22:32 +00007200 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00007201 if (!(p = lock_user_string(arg2)))
7202 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007203 ret = get_errno(mkdirat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00007204 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00007205 break;
7206#endif
Chen Gang704eff62015-08-21 05:37:33 +08007207#ifdef TARGET_NR_rmdir
bellard31e31b82003-02-18 22:55:36 +00007208 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00007209 if (!(p = lock_user_string(arg1)))
7210 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007211 ret = get_errno(rmdir(p));
7212 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007213 break;
Chen Gang704eff62015-08-21 05:37:33 +08007214#endif
bellard31e31b82003-02-18 22:55:36 +00007215 case TARGET_NR_dup:
7216 ret = get_errno(dup(arg1));
Laurent Viviere36800c2015-10-02 14:48:09 +02007217 if (ret >= 0) {
7218 fd_trans_dup(arg1, ret);
7219 }
bellard31e31b82003-02-18 22:55:36 +00007220 break;
Chen Gang704eff62015-08-21 05:37:33 +08007221#ifdef TARGET_NR_pipe
bellard31e31b82003-02-18 22:55:36 +00007222 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07007223 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00007224 break;
Chen Gang704eff62015-08-21 05:37:33 +08007225#endif
Riku Voipio099d6b02009-05-05 12:10:04 +03007226#ifdef TARGET_NR_pipe2
7227 case TARGET_NR_pipe2:
Richard Hendersone7ea6cb2012-06-01 18:48:39 -07007228 ret = do_pipe(cpu_env, arg1,
7229 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03007230 break;
7231#endif
bellard31e31b82003-02-18 22:55:36 +00007232 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00007233 {
pbrook53a59602006-03-25 19:31:22 +00007234 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00007235 struct tms tms;
7236 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00007237 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00007238 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
7239 if (!tmsp)
7240 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007241 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
7242 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
7243 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
7244 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00007245 }
bellardc596ed12003-07-13 17:32:31 +00007246 if (!is_error(ret))
7247 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00007248 }
7249 break;
bellardebc05482003-09-30 21:08:41 +00007250#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00007251 case TARGET_NR_prof:
7252 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007253#endif
thse5febef2007-04-01 18:31:35 +00007254#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00007255 case TARGET_NR_signal:
7256 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007257#endif
bellard31e31b82003-02-18 22:55:36 +00007258 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00007259 if (arg1 == 0) {
7260 ret = get_errno(acct(NULL));
7261 } else {
7262 if (!(p = lock_user_string(arg1)))
7263 goto efault;
7264 ret = get_errno(acct(path(p)));
7265 unlock_user(p, arg1, 0);
7266 }
pbrook24836682006-04-16 14:14:53 +00007267 break;
Richard Henderson8070e7b2013-07-24 09:50:00 -10007268#ifdef TARGET_NR_umount2
bellard31e31b82003-02-18 22:55:36 +00007269 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00007270 if (!(p = lock_user_string(arg1)))
7271 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007272 ret = get_errno(umount2(p, arg2));
7273 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007274 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007275#endif
bellardebc05482003-09-30 21:08:41 +00007276#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00007277 case TARGET_NR_lock:
7278 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007279#endif
bellard31e31b82003-02-18 22:55:36 +00007280 case TARGET_NR_ioctl:
7281 ret = do_ioctl(arg1, arg2, arg3);
7282 break;
7283 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00007284 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00007285 break;
bellardebc05482003-09-30 21:08:41 +00007286#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00007287 case TARGET_NR_mpx:
7288 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007289#endif
bellard31e31b82003-02-18 22:55:36 +00007290 case TARGET_NR_setpgid:
7291 ret = get_errno(setpgid(arg1, arg2));
7292 break;
bellardebc05482003-09-30 21:08:41 +00007293#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00007294 case TARGET_NR_ulimit:
7295 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007296#endif
7297#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00007298 case TARGET_NR_oldolduname:
7299 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007300#endif
bellard31e31b82003-02-18 22:55:36 +00007301 case TARGET_NR_umask:
7302 ret = get_errno(umask(arg1));
7303 break;
7304 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00007305 if (!(p = lock_user_string(arg1)))
7306 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007307 ret = get_errno(chroot(p));
7308 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007309 break;
Chen Gang704eff62015-08-21 05:37:33 +08007310#ifdef TARGET_NR_ustat
bellard31e31b82003-02-18 22:55:36 +00007311 case TARGET_NR_ustat:
7312 goto unimplemented;
Chen Gang704eff62015-08-21 05:37:33 +08007313#endif
7314#ifdef TARGET_NR_dup2
bellard31e31b82003-02-18 22:55:36 +00007315 case TARGET_NR_dup2:
7316 ret = get_errno(dup2(arg1, arg2));
Laurent Viviere36800c2015-10-02 14:48:09 +02007317 if (ret >= 0) {
7318 fd_trans_dup(arg1, arg2);
7319 }
bellard31e31b82003-02-18 22:55:36 +00007320 break;
Chen Gang704eff62015-08-21 05:37:33 +08007321#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03007322#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
7323 case TARGET_NR_dup3:
7324 ret = get_errno(dup3(arg1, arg2, arg3));
Laurent Viviere36800c2015-10-02 14:48:09 +02007325 if (ret >= 0) {
7326 fd_trans_dup(arg1, arg2);
7327 }
Ulrich Hechtd0927932009-09-17 20:22:14 +03007328 break;
7329#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007330#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007331 case TARGET_NR_getppid:
7332 ret = get_errno(getppid());
7333 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007334#endif
Chen Gang704eff62015-08-21 05:37:33 +08007335#ifdef TARGET_NR_getpgrp
bellard31e31b82003-02-18 22:55:36 +00007336 case TARGET_NR_getpgrp:
7337 ret = get_errno(getpgrp());
7338 break;
Chen Gang704eff62015-08-21 05:37:33 +08007339#endif
bellard31e31b82003-02-18 22:55:36 +00007340 case TARGET_NR_setsid:
7341 ret = get_errno(setsid());
7342 break;
thse5febef2007-04-01 18:31:35 +00007343#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00007344 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00007345 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08007346#if defined(TARGET_ALPHA)
7347 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00007348 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00007349 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00007350 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
7351 goto efault;
bellard66fb9762003-03-23 01:06:05 +00007352 act._sa_handler = old_act->_sa_handler;
7353 target_siginitset(&act.sa_mask, old_act->sa_mask);
7354 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08007355 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00007356 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00007357 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00007358 }
7359 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00007360 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00007361 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
7362 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007363 old_act->_sa_handler = oact._sa_handler;
7364 old_act->sa_mask = oact.sa_mask.sig[0];
7365 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00007366 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00007367 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08007368#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00007369 struct target_sigaction act, oact, *pact, *old_act;
7370
7371 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00007372 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
7373 goto efault;
bellard106ec872006-06-27 21:08:10 +00007374 act._sa_handler = old_act->_sa_handler;
7375 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
7376 act.sa_flags = old_act->sa_flags;
7377 unlock_user_struct(old_act, arg2, 0);
7378 pact = &act;
7379 } else {
7380 pact = NULL;
7381 }
7382
7383 ret = get_errno(do_sigaction(arg1, pact, &oact));
7384
7385 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00007386 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
7387 goto efault;
bellard106ec872006-06-27 21:08:10 +00007388 old_act->_sa_handler = oact._sa_handler;
7389 old_act->sa_flags = oact.sa_flags;
7390 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
7391 old_act->sa_mask.sig[1] = 0;
7392 old_act->sa_mask.sig[2] = 0;
7393 old_act->sa_mask.sig[3] = 0;
7394 unlock_user_struct(old_act, arg3, 1);
7395 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08007396#else
7397 struct target_old_sigaction *old_act;
7398 struct target_sigaction act, oact, *pact;
7399 if (arg2) {
7400 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
7401 goto efault;
7402 act._sa_handler = old_act->_sa_handler;
7403 target_siginitset(&act.sa_mask, old_act->sa_mask);
7404 act.sa_flags = old_act->sa_flags;
7405 act.sa_restorer = old_act->sa_restorer;
7406 unlock_user_struct(old_act, arg2, 0);
7407 pact = &act;
7408 } else {
7409 pact = NULL;
7410 }
7411 ret = get_errno(do_sigaction(arg1, pact, &oact));
7412 if (!is_error(ret) && arg3) {
7413 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
7414 goto efault;
7415 old_act->_sa_handler = oact._sa_handler;
7416 old_act->sa_mask = oact.sa_mask.sig[0];
7417 old_act->sa_flags = oact.sa_flags;
7418 old_act->sa_restorer = oact.sa_restorer;
7419 unlock_user_struct(old_act, arg3, 1);
7420 }
ths388bb212007-05-13 13:58:00 +00007421#endif
bellard31e31b82003-02-18 22:55:36 +00007422 }
7423 break;
thse5febef2007-04-01 18:31:35 +00007424#endif
bellard66fb9762003-03-23 01:06:05 +00007425 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00007426 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08007427#if defined(TARGET_ALPHA)
7428 struct target_sigaction act, oact, *pact = 0;
7429 struct target_rt_sigaction *rt_act;
7430 /* ??? arg4 == sizeof(sigset_t). */
7431 if (arg2) {
7432 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
7433 goto efault;
7434 act._sa_handler = rt_act->_sa_handler;
7435 act.sa_mask = rt_act->sa_mask;
7436 act.sa_flags = rt_act->sa_flags;
7437 act.sa_restorer = arg5;
7438 unlock_user_struct(rt_act, arg2, 0);
7439 pact = &act;
7440 }
7441 ret = get_errno(do_sigaction(arg1, pact, &oact));
7442 if (!is_error(ret) && arg3) {
7443 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
7444 goto efault;
7445 rt_act->_sa_handler = oact._sa_handler;
7446 rt_act->sa_mask = oact.sa_mask;
7447 rt_act->sa_flags = oact.sa_flags;
7448 unlock_user_struct(rt_act, arg3, 1);
7449 }
7450#else
pbrook53a59602006-03-25 19:31:22 +00007451 struct target_sigaction *act;
7452 struct target_sigaction *oact;
7453
bellard579a97f2007-11-11 14:26:47 +00007454 if (arg2) {
7455 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
7456 goto efault;
7457 } else
pbrook53a59602006-03-25 19:31:22 +00007458 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00007459 if (arg3) {
7460 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
7461 ret = -TARGET_EFAULT;
7462 goto rt_sigaction_fail;
7463 }
7464 } else
pbrook53a59602006-03-25 19:31:22 +00007465 oact = NULL;
7466 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00007467 rt_sigaction_fail:
7468 if (act)
pbrook53a59602006-03-25 19:31:22 +00007469 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00007470 if (oact)
pbrook53a59602006-03-25 19:31:22 +00007471 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08007472#endif
pbrook53a59602006-03-25 19:31:22 +00007473 }
bellard66fb9762003-03-23 01:06:05 +00007474 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007475#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007476 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00007477 {
7478 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00007479 abi_ulong target_set;
Alex Barcelo1c275922014-03-14 14:36:55 +00007480 do_sigprocmask(0, NULL, &cur_set);
bellard66fb9762003-03-23 01:06:05 +00007481 host_to_target_old_sigset(&target_set, &cur_set);
7482 ret = target_set;
7483 }
7484 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007485#endif
7486#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007487 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00007488 {
7489 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00007490 abi_ulong target_set = arg1;
Alex Barcelo1c275922014-03-14 14:36:55 +00007491 do_sigprocmask(0, NULL, &cur_set);
bellard66fb9762003-03-23 01:06:05 +00007492 target_to_host_old_sigset(&set, &target_set);
7493 sigorset(&set, &set, &cur_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00007494 do_sigprocmask(SIG_SETMASK, &set, &oset);
bellard66fb9762003-03-23 01:06:05 +00007495 host_to_target_old_sigset(&target_set, &oset);
7496 ret = target_set;
7497 }
7498 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007499#endif
thse5febef2007-04-01 18:31:35 +00007500#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00007501 case TARGET_NR_sigprocmask:
7502 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07007503#if defined(TARGET_ALPHA)
7504 sigset_t set, oldset;
7505 abi_ulong mask;
7506 int how;
7507
7508 switch (arg1) {
7509 case TARGET_SIG_BLOCK:
7510 how = SIG_BLOCK;
7511 break;
7512 case TARGET_SIG_UNBLOCK:
7513 how = SIG_UNBLOCK;
7514 break;
7515 case TARGET_SIG_SETMASK:
7516 how = SIG_SETMASK;
7517 break;
7518 default:
7519 ret = -TARGET_EINVAL;
7520 goto fail;
7521 }
7522 mask = arg2;
7523 target_to_host_old_sigset(&set, &mask);
7524
Alex Barcelo1c275922014-03-14 14:36:55 +00007525 ret = get_errno(do_sigprocmask(how, &set, &oldset));
Richard Hendersona5b3b132010-05-03 10:07:55 -07007526 if (!is_error(ret)) {
7527 host_to_target_old_sigset(&mask, &oldset);
7528 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07007529 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07007530 }
7531#else
bellard66fb9762003-03-23 01:06:05 +00007532 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07007533 int how;
ths3b46e622007-09-17 08:09:54 +00007534
pbrook53a59602006-03-25 19:31:22 +00007535 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07007536 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00007537 case TARGET_SIG_BLOCK:
7538 how = SIG_BLOCK;
7539 break;
7540 case TARGET_SIG_UNBLOCK:
7541 how = SIG_UNBLOCK;
7542 break;
7543 case TARGET_SIG_SETMASK:
7544 how = SIG_SETMASK;
7545 break;
7546 default:
ths0da46a62007-10-20 20:23:07 +00007547 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00007548 goto fail;
7549 }
Anthony Liguoric227f092009-10-01 16:12:16 -05007550 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00007551 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007552 target_to_host_old_sigset(&set, p);
7553 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00007554 set_ptr = &set;
7555 } else {
7556 how = 0;
7557 set_ptr = NULL;
7558 }
Alex Barcelo1c275922014-03-14 14:36:55 +00007559 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00007560 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05007561 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00007562 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007563 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05007564 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00007565 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07007566#endif
bellard66fb9762003-03-23 01:06:05 +00007567 }
7568 break;
thse5febef2007-04-01 18:31:35 +00007569#endif
bellard66fb9762003-03-23 01:06:05 +00007570 case TARGET_NR_rt_sigprocmask:
7571 {
7572 int how = arg1;
7573 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00007574
pbrook53a59602006-03-25 19:31:22 +00007575 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00007576 switch(how) {
7577 case TARGET_SIG_BLOCK:
7578 how = SIG_BLOCK;
7579 break;
7580 case TARGET_SIG_UNBLOCK:
7581 how = SIG_UNBLOCK;
7582 break;
7583 case TARGET_SIG_SETMASK:
7584 how = SIG_SETMASK;
7585 break;
7586 default:
ths0da46a62007-10-20 20:23:07 +00007587 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00007588 goto fail;
7589 }
Anthony Liguoric227f092009-10-01 16:12:16 -05007590 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00007591 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007592 target_to_host_sigset(&set, p);
7593 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00007594 set_ptr = &set;
7595 } else {
7596 how = 0;
7597 set_ptr = NULL;
7598 }
Alex Barcelo1c275922014-03-14 14:36:55 +00007599 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00007600 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05007601 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00007602 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007603 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05007604 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00007605 }
7606 }
7607 break;
thse5febef2007-04-01 18:31:35 +00007608#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00007609 case TARGET_NR_sigpending:
7610 {
7611 sigset_t set;
7612 ret = get_errno(sigpending(&set));
7613 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05007614 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00007615 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007616 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05007617 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00007618 }
7619 }
7620 break;
thse5febef2007-04-01 18:31:35 +00007621#endif
bellard66fb9762003-03-23 01:06:05 +00007622 case TARGET_NR_rt_sigpending:
7623 {
7624 sigset_t set;
7625 ret = get_errno(sigpending(&set));
7626 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05007627 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00007628 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007629 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05007630 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00007631 }
7632 }
7633 break;
thse5febef2007-04-01 18:31:35 +00007634#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00007635 case TARGET_NR_sigsuspend:
7636 {
7637 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07007638#if defined(TARGET_ALPHA)
7639 abi_ulong mask = arg1;
7640 target_to_host_old_sigset(&set, &mask);
7641#else
Anthony Liguoric227f092009-10-01 16:12:16 -05007642 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00007643 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007644 target_to_host_old_sigset(&set, p);
7645 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07007646#endif
bellard66fb9762003-03-23 01:06:05 +00007647 ret = get_errno(sigsuspend(&set));
7648 }
7649 break;
thse5febef2007-04-01 18:31:35 +00007650#endif
bellard66fb9762003-03-23 01:06:05 +00007651 case TARGET_NR_rt_sigsuspend:
7652 {
7653 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05007654 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00007655 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007656 target_to_host_sigset(&set, p);
7657 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00007658 ret = get_errno(sigsuspend(&set));
7659 }
7660 break;
7661 case TARGET_NR_rt_sigtimedwait:
7662 {
bellard66fb9762003-03-23 01:06:05 +00007663 sigset_t set;
7664 struct timespec uts, *puts;
7665 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00007666
Anthony Liguoric227f092009-10-01 16:12:16 -05007667 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00007668 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007669 target_to_host_sigset(&set, p);
7670 unlock_user(p, arg1, 0);
7671 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00007672 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00007673 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00007674 } else {
7675 puts = NULL;
7676 }
7677 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
Petar Jovanovic974a1962014-03-03 15:07:41 +01007678 if (!is_error(ret)) {
7679 if (arg2) {
7680 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
7681 0);
7682 if (!p) {
7683 goto efault;
7684 }
7685 host_to_target_siginfo(p, &uinfo);
7686 unlock_user(p, arg2, sizeof(target_siginfo_t));
7687 }
7688 ret = host_to_target_signal(ret);
bellard66fb9762003-03-23 01:06:05 +00007689 }
7690 }
7691 break;
7692 case TARGET_NR_rt_sigqueueinfo:
7693 {
7694 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05007695 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00007696 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007697 target_to_host_siginfo(&uinfo, p);
7698 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00007699 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
7700 }
7701 break;
thse5febef2007-04-01 18:31:35 +00007702#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00007703 case TARGET_NR_sigreturn:
bellard66fb9762003-03-23 01:06:05 +00007704 ret = do_sigreturn(cpu_env);
7705 break;
thse5febef2007-04-01 18:31:35 +00007706#endif
bellard66fb9762003-03-23 01:06:05 +00007707 case TARGET_NR_rt_sigreturn:
bellard66fb9762003-03-23 01:06:05 +00007708 ret = do_rt_sigreturn(cpu_env);
7709 break;
bellard31e31b82003-02-18 22:55:36 +00007710 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00007711 if (!(p = lock_user_string(arg1)))
7712 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007713 ret = get_errno(sethostname(p, arg2));
7714 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007715 break;
7716 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00007717 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007718 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00007719 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00007720 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00007721 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
7722 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007723 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
7724 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007725 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00007726 ret = get_errno(setrlimit(resource, &rlim));
7727 }
7728 break;
bellard31e31b82003-02-18 22:55:36 +00007729 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00007730 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007731 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00007732 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00007733 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00007734
bellard9de5e442003-03-23 16:49:39 +00007735 ret = get_errno(getrlimit(resource, &rlim));
7736 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007737 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7738 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007739 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7740 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007741 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00007742 }
7743 }
7744 break;
bellard31e31b82003-02-18 22:55:36 +00007745 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00007746 {
7747 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00007748 ret = get_errno(getrusage(arg1, &rusage));
7749 if (!is_error(ret)) {
Petar Jovanovica39fb272014-04-08 19:24:30 +02007750 ret = host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00007751 }
7752 }
7753 break;
bellard31e31b82003-02-18 22:55:36 +00007754 case TARGET_NR_gettimeofday:
7755 {
bellard31e31b82003-02-18 22:55:36 +00007756 struct timeval tv;
7757 ret = get_errno(gettimeofday(&tv, NULL));
7758 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00007759 if (copy_to_user_timeval(arg1, &tv))
7760 goto efault;
bellard31e31b82003-02-18 22:55:36 +00007761 }
7762 }
7763 break;
7764 case TARGET_NR_settimeofday:
7765 {
Paul Burtonb67d8032014-06-22 11:25:41 +01007766 struct timeval tv, *ptv = NULL;
Paul Burtonef4467e2014-06-22 11:25:40 +01007767 struct timezone tz, *ptz = NULL;
7768
Paul Burtonb67d8032014-06-22 11:25:41 +01007769 if (arg1) {
7770 if (copy_from_user_timeval(&tv, arg1)) {
7771 goto efault;
7772 }
7773 ptv = &tv;
7774 }
Paul Burtonef4467e2014-06-22 11:25:40 +01007775
7776 if (arg2) {
7777 if (copy_from_user_timezone(&tz, arg2)) {
7778 goto efault;
7779 }
7780 ptz = &tz;
7781 }
7782
Paul Burtonb67d8032014-06-22 11:25:41 +01007783 ret = get_errno(settimeofday(ptv, ptz));
bellard31e31b82003-02-18 22:55:36 +00007784 }
7785 break;
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007786#if defined(TARGET_NR_select)
bellard31e31b82003-02-18 22:55:36 +00007787 case TARGET_NR_select:
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007788#if defined(TARGET_S390X) || defined(TARGET_ALPHA)
7789 ret = do_select(arg1, arg2, arg3, arg4, arg5);
7790#else
bellardf2674e32003-07-09 12:26:09 +00007791 {
pbrook53a59602006-03-25 19:31:22 +00007792 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00007793 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00007794 long nsel;
7795
bellard579a97f2007-11-11 14:26:47 +00007796 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
7797 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007798 nsel = tswapal(sel->n);
7799 inp = tswapal(sel->inp);
7800 outp = tswapal(sel->outp);
7801 exp = tswapal(sel->exp);
7802 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00007803 unlock_user_struct(sel, arg1, 0);
7804 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00007805 }
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007806#endif
bellardf2674e32003-07-09 12:26:09 +00007807 break;
bellard048f6b42005-11-26 18:47:20 +00007808#endif
Riku Voipio9e423822010-05-07 12:28:05 +00007809#ifdef TARGET_NR_pselect6
7810 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04007811 {
7812 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
7813 fd_set rfds, wfds, efds;
7814 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
7815 struct timespec ts, *ts_ptr;
7816
7817 /*
7818 * The 6th arg is actually two args smashed together,
7819 * so we cannot use the C library.
7820 */
7821 sigset_t set;
7822 struct {
7823 sigset_t *set;
7824 size_t size;
7825 } sig, *sig_ptr;
7826
7827 abi_ulong arg_sigset, arg_sigsize, *arg7;
7828 target_sigset_t *target_sigset;
7829
7830 n = arg1;
7831 rfd_addr = arg2;
7832 wfd_addr = arg3;
7833 efd_addr = arg4;
7834 ts_addr = arg5;
7835
7836 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
7837 if (ret) {
7838 goto fail;
7839 }
7840 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
7841 if (ret) {
7842 goto fail;
7843 }
7844 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
7845 if (ret) {
7846 goto fail;
7847 }
7848
7849 /*
7850 * This takes a timespec, and not a timeval, so we cannot
7851 * use the do_select() helper ...
7852 */
7853 if (ts_addr) {
7854 if (target_to_host_timespec(&ts, ts_addr)) {
7855 goto efault;
7856 }
7857 ts_ptr = &ts;
7858 } else {
7859 ts_ptr = NULL;
7860 }
7861
7862 /* Extract the two packed args for the sigset */
7863 if (arg6) {
7864 sig_ptr = &sig;
7865 sig.size = _NSIG / 8;
7866
7867 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
7868 if (!arg7) {
7869 goto efault;
7870 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007871 arg_sigset = tswapal(arg7[0]);
7872 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04007873 unlock_user(arg7, arg6, 0);
7874
7875 if (arg_sigset) {
7876 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01007877 if (arg_sigsize != sizeof(*target_sigset)) {
7878 /* Like the kernel, we enforce correct size sigsets */
7879 ret = -TARGET_EINVAL;
7880 goto fail;
7881 }
Mike Frysinger055e0902011-06-03 17:01:49 -04007882 target_sigset = lock_user(VERIFY_READ, arg_sigset,
7883 sizeof(*target_sigset), 1);
7884 if (!target_sigset) {
7885 goto efault;
7886 }
7887 target_to_host_sigset(&set, target_sigset);
7888 unlock_user(target_sigset, arg_sigset, 0);
7889 } else {
7890 sig.set = NULL;
7891 }
7892 } else {
7893 sig_ptr = NULL;
7894 }
7895
Peter Maydell6df9d382016-05-12 18:47:51 +01007896 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
7897 ts_ptr, sig_ptr));
Mike Frysinger055e0902011-06-03 17:01:49 -04007898
7899 if (!is_error(ret)) {
7900 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
7901 goto efault;
7902 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
7903 goto efault;
7904 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
7905 goto efault;
7906
7907 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
7908 goto efault;
7909 }
7910 }
7911 break;
Riku Voipio9e423822010-05-07 12:28:05 +00007912#endif
Chen Gang704eff62015-08-21 05:37:33 +08007913#ifdef TARGET_NR_symlink
bellard31e31b82003-02-18 22:55:36 +00007914 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00007915 {
7916 void *p2;
7917 p = lock_user_string(arg1);
7918 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00007919 if (!p || !p2)
7920 ret = -TARGET_EFAULT;
7921 else
7922 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00007923 unlock_user(p2, arg2, 0);
7924 unlock_user(p, arg1, 0);
7925 }
bellard31e31b82003-02-18 22:55:36 +00007926 break;
Chen Gang704eff62015-08-21 05:37:33 +08007927#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007928#if defined(TARGET_NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +00007929 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00007930 {
bellard579a97f2007-11-11 14:26:47 +00007931 void *p2;
thsf0b62432007-09-24 09:25:40 +00007932 p = lock_user_string(arg1);
7933 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00007934 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00007935 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00007936 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01007937 ret = get_errno(symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00007938 unlock_user(p2, arg3, 0);
7939 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00007940 }
7941 break;
7942#endif
bellardebc05482003-09-30 21:08:41 +00007943#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00007944 case TARGET_NR_oldlstat:
7945 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007946#endif
Chen Gang704eff62015-08-21 05:37:33 +08007947#ifdef TARGET_NR_readlink
bellard31e31b82003-02-18 22:55:36 +00007948 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00007949 {
Andreas Schwab463d8e72013-07-02 14:04:12 +01007950 void *p2;
pbrook53a59602006-03-25 19:31:22 +00007951 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00007952 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01007953 if (!p || !p2) {
bellard579a97f2007-11-11 14:26:47 +00007954 ret = -TARGET_EFAULT;
Mike Frysingerf17f4982014-08-08 09:40:25 +09007955 } else if (!arg3) {
7956 /* Short circuit this for the magic exe check. */
7957 ret = -TARGET_EINVAL;
Andreas Schwab463d8e72013-07-02 14:04:12 +01007958 } else if (is_proc_myself((const char *)p, "exe")) {
7959 char real[PATH_MAX], *temp;
7960 temp = realpath(exec_path, real);
Mike Frysingerf17f4982014-08-08 09:40:25 +09007961 /* Return value is # of bytes that we wrote to the buffer. */
7962 if (temp == NULL) {
7963 ret = get_errno(-1);
7964 } else {
7965 /* Don't worry about sign mismatch as earlier mapping
7966 * logic would have thrown a bad address error. */
7967 ret = MIN(strlen(real), arg3);
7968 /* We cannot NUL terminate the string. */
7969 memcpy(p2, real, ret);
7970 }
Andreas Schwab463d8e72013-07-02 14:04:12 +01007971 } else {
7972 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00007973 }
pbrook53a59602006-03-25 19:31:22 +00007974 unlock_user(p2, arg2, ret);
7975 unlock_user(p, arg1, 0);
7976 }
bellard31e31b82003-02-18 22:55:36 +00007977 break;
Chen Gang704eff62015-08-21 05:37:33 +08007978#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007979#if defined(TARGET_NR_readlinkat)
ths5e0ccb12007-09-24 09:26:10 +00007980 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00007981 {
bellard579a97f2007-11-11 14:26:47 +00007982 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00007983 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00007984 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01007985 if (!p || !p2) {
7986 ret = -TARGET_EFAULT;
7987 } else if (is_proc_myself((const char *)p, "exe")) {
7988 char real[PATH_MAX], *temp;
7989 temp = realpath(exec_path, real);
7990 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
7991 snprintf((char *)p2, arg4, "%s", real);
7992 } else {
Peter Maydellc0d472b2013-06-12 16:20:21 +01007993 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
Andreas Schwab463d8e72013-07-02 14:04:12 +01007994 }
bellard579a97f2007-11-11 14:26:47 +00007995 unlock_user(p2, arg3, ret);
7996 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00007997 }
7998 break;
7999#endif
thse5febef2007-04-01 18:31:35 +00008000#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00008001 case TARGET_NR_uselib:
8002 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008003#endif
8004#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00008005 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00008006 if (!(p = lock_user_string(arg1)))
8007 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008008 ret = get_errno(swapon(p, arg2));
8009 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00008010 break;
thse5febef2007-04-01 18:31:35 +00008011#endif
bellard31e31b82003-02-18 22:55:36 +00008012 case TARGET_NR_reboot:
Laurent Vivierc07ecc62013-01-07 11:40:06 +00008013 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
8014 /* arg4 must be ignored in all other cases */
8015 p = lock_user_string(arg4);
8016 if (!p) {
8017 goto efault;
8018 }
8019 ret = get_errno(reboot(arg1, arg2, arg3, p));
8020 unlock_user(p, arg4, 0);
8021 } else {
8022 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
8023 }
Alexander Graf0f6b4d22011-09-27 14:39:42 +02008024 break;
thse5febef2007-04-01 18:31:35 +00008025#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00008026 case TARGET_NR_readdir:
8027 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008028#endif
8029#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00008030 case TARGET_NR_mmap:
Alexander Graf09701192013-09-03 20:12:15 +01008031#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
8032 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
Ulrich Hechta4c075f2009-07-24 16:57:31 +02008033 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
8034 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00008035 {
blueswir1992f48a2007-10-14 16:27:31 +00008036 abi_ulong *v;
8037 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00008038 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
8039 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02008040 v1 = tswapal(v[0]);
8041 v2 = tswapal(v[1]);
8042 v3 = tswapal(v[2]);
8043 v4 = tswapal(v[3]);
8044 v5 = tswapal(v[4]);
8045 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00008046 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00008047 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00008048 target_to_host_bitmask(v4, mmap_flags_tbl),
8049 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00008050 }
bellard31e31b82003-02-18 22:55:36 +00008051#else
ths5fafdf22007-09-16 21:08:06 +00008052 ret = get_errno(target_mmap(arg1, arg2, arg3,
8053 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00008054 arg5,
8055 arg6));
bellard31e31b82003-02-18 22:55:36 +00008056#endif
bellard6fb883e2003-07-09 17:12:39 +00008057 break;
thse5febef2007-04-01 18:31:35 +00008058#endif
bellarda315a142005-01-30 22:59:18 +00008059#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00008060 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00008061#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00008062#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00008063#endif
ths5fafdf22007-09-16 21:08:06 +00008064 ret = get_errno(target_mmap(arg1, arg2, arg3,
8065 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00008066 arg5,
bellardc573ff62004-01-04 15:51:36 +00008067 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00008068 break;
bellarda315a142005-01-30 22:59:18 +00008069#endif
bellard31e31b82003-02-18 22:55:36 +00008070 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00008071 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00008072 break;
bellard9de5e442003-03-23 16:49:39 +00008073 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01008074 {
Andreas Färber0429a972013-08-26 18:14:44 +02008075 TaskState *ts = cpu->opaque;
Paul Brook97374d32010-06-16 13:03:51 +01008076 /* Special hack to detect libc making the stack executable. */
8077 if ((arg3 & PROT_GROWSDOWN)
8078 && arg1 >= ts->info->stack_limit
8079 && arg1 <= ts->info->start_stack) {
8080 arg3 &= ~PROT_GROWSDOWN;
8081 arg2 = arg2 + arg1 - ts->info->stack_limit;
8082 arg1 = ts->info->stack_limit;
8083 }
8084 }
bellard54936002003-05-13 00:25:15 +00008085 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00008086 break;
thse5febef2007-04-01 18:31:35 +00008087#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00008088 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00008089 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00008090 break;
thse5febef2007-04-01 18:31:35 +00008091#endif
pbrook53a59602006-03-25 19:31:22 +00008092 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00008093#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00008094 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00008095 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00008096 break;
thse5febef2007-04-01 18:31:35 +00008097#endif
8098#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00008099 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00008100 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00008101 break;
thse5febef2007-04-01 18:31:35 +00008102#endif
8103#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00008104 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00008105 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00008106 break;
thse5febef2007-04-01 18:31:35 +00008107#endif
8108#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00008109 case TARGET_NR_mlockall:
Tom Musta6f6a4032014-08-12 13:53:42 -05008110 ret = get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
bellard9de5e442003-03-23 16:49:39 +00008111 break;
thse5febef2007-04-01 18:31:35 +00008112#endif
8113#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00008114 case TARGET_NR_munlockall:
8115 ret = get_errno(munlockall());
8116 break;
thse5febef2007-04-01 18:31:35 +00008117#endif
bellard31e31b82003-02-18 22:55:36 +00008118 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00008119 if (!(p = lock_user_string(arg1)))
8120 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008121 ret = get_errno(truncate(p, arg2));
8122 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00008123 break;
8124 case TARGET_NR_ftruncate:
8125 ret = get_errno(ftruncate(arg1, arg2));
8126 break;
8127 case TARGET_NR_fchmod:
8128 ret = get_errno(fchmod(arg1, arg2));
8129 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008130#if defined(TARGET_NR_fchmodat)
ths814d7972007-09-24 09:26:51 +00008131 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00008132 if (!(p = lock_user_string(arg2)))
8133 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01008134 ret = get_errno(fchmodat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00008135 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00008136 break;
8137#endif
bellard31e31b82003-02-18 22:55:36 +00008138 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -07008139 /* Note that negative values are valid for getpriority, so we must
8140 differentiate based on errno settings. */
8141 errno = 0;
8142 ret = getpriority(arg1, arg2);
8143 if (ret == -1 && errno != 0) {
8144 ret = -host_to_target_errno(errno);
8145 break;
8146 }
8147#ifdef TARGET_ALPHA
8148 /* Return value is the unbiased priority. Signal no error. */
8149 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
8150#else
8151 /* Return value is a biased priority to avoid negative numbers. */
8152 ret = 20 - ret;
8153#endif
bellard31e31b82003-02-18 22:55:36 +00008154 break;
8155 case TARGET_NR_setpriority:
8156 ret = get_errno(setpriority(arg1, arg2, arg3));
8157 break;
bellardebc05482003-09-30 21:08:41 +00008158#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00008159 case TARGET_NR_profil:
8160 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008161#endif
bellard31e31b82003-02-18 22:55:36 +00008162 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00008163 if (!(p = lock_user_string(arg1)))
8164 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008165 ret = get_errno(statfs(path(p), &stfs));
8166 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00008167 convert_statfs:
8168 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00008169 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00008170
bellard579a97f2007-11-11 14:26:47 +00008171 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
8172 goto efault;
8173 __put_user(stfs.f_type, &target_stfs->f_type);
8174 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
8175 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
8176 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
8177 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
8178 __put_user(stfs.f_files, &target_stfs->f_files);
8179 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
8180 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
8181 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
8182 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02008183 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
8184 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +00008185 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00008186 }
8187 break;
8188 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00008189 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00008190 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00008191#ifdef TARGET_NR_statfs64
8192 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00008193 if (!(p = lock_user_string(arg1)))
8194 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008195 ret = get_errno(statfs(path(p), &stfs));
8196 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00008197 convert_statfs64:
8198 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00008199 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00008200
bellard579a97f2007-11-11 14:26:47 +00008201 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
8202 goto efault;
8203 __put_user(stfs.f_type, &target_stfs->f_type);
8204 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
8205 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
8206 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
8207 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
8208 __put_user(stfs.f_files, &target_stfs->f_files);
8209 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
8210 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
8211 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
8212 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02008213 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
8214 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +00008215 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00008216 }
8217 break;
8218 case TARGET_NR_fstatfs64:
8219 ret = get_errno(fstatfs(arg1, &stfs));
8220 goto convert_statfs64;
8221#endif
bellardebc05482003-09-30 21:08:41 +00008222#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00008223 case TARGET_NR_ioperm:
8224 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008225#endif
thse5febef2007-04-01 18:31:35 +00008226#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00008227 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00008228 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00008229 break;
thse5febef2007-04-01 18:31:35 +00008230#endif
bellard3532fa72006-06-24 15:06:03 +00008231#ifdef TARGET_NR_accept
8232 case TARGET_NR_accept:
Peter Maydella94b4982013-02-08 04:35:04 +00008233 ret = do_accept4(arg1, arg2, arg3, 0);
8234 break;
8235#endif
8236#ifdef TARGET_NR_accept4
8237 case TARGET_NR_accept4:
8238#ifdef CONFIG_ACCEPT4
8239 ret = do_accept4(arg1, arg2, arg3, arg4);
8240#else
8241 goto unimplemented;
8242#endif
bellard3532fa72006-06-24 15:06:03 +00008243 break;
8244#endif
8245#ifdef TARGET_NR_bind
8246 case TARGET_NR_bind:
8247 ret = do_bind(arg1, arg2, arg3);
8248 break;
8249#endif
8250#ifdef TARGET_NR_connect
8251 case TARGET_NR_connect:
8252 ret = do_connect(arg1, arg2, arg3);
8253 break;
8254#endif
8255#ifdef TARGET_NR_getpeername
8256 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00008257 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00008258 break;
8259#endif
8260#ifdef TARGET_NR_getsockname
8261 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00008262 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00008263 break;
8264#endif
8265#ifdef TARGET_NR_getsockopt
8266 case TARGET_NR_getsockopt:
8267 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
8268 break;
8269#endif
8270#ifdef TARGET_NR_listen
8271 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00008272 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00008273 break;
8274#endif
8275#ifdef TARGET_NR_recv
8276 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00008277 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00008278 break;
8279#endif
8280#ifdef TARGET_NR_recvfrom
8281 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00008282 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00008283 break;
8284#endif
8285#ifdef TARGET_NR_recvmsg
8286 case TARGET_NR_recvmsg:
8287 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
8288 break;
8289#endif
8290#ifdef TARGET_NR_send
8291 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00008292 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00008293 break;
8294#endif
8295#ifdef TARGET_NR_sendmsg
8296 case TARGET_NR_sendmsg:
8297 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
8298 break;
8299#endif
Alexander Graff19e00d2014-03-02 19:36:42 +00008300#ifdef TARGET_NR_sendmmsg
8301 case TARGET_NR_sendmmsg:
8302 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
8303 break;
8304 case TARGET_NR_recvmmsg:
8305 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
8306 break;
8307#endif
bellard3532fa72006-06-24 15:06:03 +00008308#ifdef TARGET_NR_sendto
8309 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00008310 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00008311 break;
8312#endif
8313#ifdef TARGET_NR_shutdown
8314 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00008315 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00008316 break;
8317#endif
Laurent Vivierf894efd2016-02-21 10:56:23 +01008318#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
8319 case TARGET_NR_getrandom:
8320 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
8321 if (!p) {
8322 goto efault;
8323 }
8324 ret = get_errno(getrandom(p, arg2, arg3));
8325 unlock_user(p, arg1, ret);
8326 break;
8327#endif
bellard3532fa72006-06-24 15:06:03 +00008328#ifdef TARGET_NR_socket
8329 case TARGET_NR_socket:
8330 ret = do_socket(arg1, arg2, arg3);
Laurent Viviere36800c2015-10-02 14:48:09 +02008331 fd_trans_unregister(ret);
bellard3532fa72006-06-24 15:06:03 +00008332 break;
8333#endif
8334#ifdef TARGET_NR_socketpair
8335 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00008336 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00008337 break;
8338#endif
8339#ifdef TARGET_NR_setsockopt
8340 case TARGET_NR_setsockopt:
8341 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
8342 break;
8343#endif
ths7494b0f2007-02-11 18:26:53 +00008344
bellard31e31b82003-02-18 22:55:36 +00008345 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00008346 if (!(p = lock_user_string(arg2)))
8347 goto efault;
thse5574482007-02-11 20:03:13 +00008348 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
8349 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00008350 break;
8351
bellard31e31b82003-02-18 22:55:36 +00008352 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00008353 {
bellard66fb9762003-03-23 01:06:05 +00008354 struct itimerval value, ovalue, *pvalue;
8355
pbrook53a59602006-03-25 19:31:22 +00008356 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00008357 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00008358 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
8359 || copy_from_user_timeval(&pvalue->it_value,
8360 arg2 + sizeof(struct target_timeval)))
8361 goto efault;
bellard66fb9762003-03-23 01:06:05 +00008362 } else {
8363 pvalue = NULL;
8364 }
8365 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00008366 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00008367 if (copy_to_user_timeval(arg3,
8368 &ovalue.it_interval)
8369 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
8370 &ovalue.it_value))
8371 goto efault;
bellard66fb9762003-03-23 01:06:05 +00008372 }
8373 }
8374 break;
bellard31e31b82003-02-18 22:55:36 +00008375 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00008376 {
bellard66fb9762003-03-23 01:06:05 +00008377 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00008378
bellard66fb9762003-03-23 01:06:05 +00008379 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00008380 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00008381 if (copy_to_user_timeval(arg2,
8382 &value.it_interval)
8383 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
8384 &value.it_value))
8385 goto efault;
bellard66fb9762003-03-23 01:06:05 +00008386 }
8387 }
8388 break;
Chen Gang704eff62015-08-21 05:37:33 +08008389#ifdef TARGET_NR_stat
bellard31e31b82003-02-18 22:55:36 +00008390 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00008391 if (!(p = lock_user_string(arg1)))
8392 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008393 ret = get_errno(stat(path(p), &st));
8394 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00008395 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +08008396#endif
8397#ifdef TARGET_NR_lstat
bellard31e31b82003-02-18 22:55:36 +00008398 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00008399 if (!(p = lock_user_string(arg1)))
8400 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008401 ret = get_errno(lstat(path(p), &st));
8402 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00008403 goto do_stat;
Chen Gang704eff62015-08-21 05:37:33 +08008404#endif
bellard31e31b82003-02-18 22:55:36 +00008405 case TARGET_NR_fstat:
8406 {
8407 ret = get_errno(fstat(arg1, &st));
Chen Gang704eff62015-08-21 05:37:33 +08008408#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
bellard31e31b82003-02-18 22:55:36 +00008409 do_stat:
Chen Gang704eff62015-08-21 05:37:33 +08008410#endif
bellard31e31b82003-02-18 22:55:36 +00008411 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00008412 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00008413
bellard579a97f2007-11-11 14:26:47 +00008414 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
8415 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02008416 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00008417 __put_user(st.st_dev, &target_st->st_dev);
8418 __put_user(st.st_ino, &target_st->st_ino);
8419 __put_user(st.st_mode, &target_st->st_mode);
8420 __put_user(st.st_uid, &target_st->st_uid);
8421 __put_user(st.st_gid, &target_st->st_gid);
8422 __put_user(st.st_nlink, &target_st->st_nlink);
8423 __put_user(st.st_rdev, &target_st->st_rdev);
8424 __put_user(st.st_size, &target_st->st_size);
8425 __put_user(st.st_blksize, &target_st->st_blksize);
8426 __put_user(st.st_blocks, &target_st->st_blocks);
8427 __put_user(st.st_atime, &target_st->target_st_atime);
8428 __put_user(st.st_mtime, &target_st->target_st_mtime);
8429 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00008430 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00008431 }
8432 }
8433 break;
bellardebc05482003-09-30 21:08:41 +00008434#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00008435 case TARGET_NR_olduname:
8436 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008437#endif
8438#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00008439 case TARGET_NR_iopl:
8440 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008441#endif
bellard31e31b82003-02-18 22:55:36 +00008442 case TARGET_NR_vhangup:
8443 ret = get_errno(vhangup());
8444 break;
bellardebc05482003-09-30 21:08:41 +00008445#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00008446 case TARGET_NR_idle:
8447 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008448#endif
bellard42ad6ae2005-01-03 22:48:11 +00008449#ifdef TARGET_NR_syscall
8450 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01008451 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
8452 arg6, arg7, arg8, 0);
8453 break;
bellard42ad6ae2005-01-03 22:48:11 +00008454#endif
bellard31e31b82003-02-18 22:55:36 +00008455 case TARGET_NR_wait4:
8456 {
8457 int status;
blueswir1992f48a2007-10-14 16:27:31 +00008458 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00008459 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00008460 abi_ulong target_rusage = arg4;
Petar Jovanovica39fb272014-04-08 19:24:30 +02008461 abi_long rusage_err;
bellard31e31b82003-02-18 22:55:36 +00008462 if (target_rusage)
8463 rusage_ptr = &rusage;
8464 else
8465 rusage_ptr = NULL;
Timothy E Baldwin4af80a32016-05-12 18:47:49 +01008466 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
bellard31e31b82003-02-18 22:55:36 +00008467 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01008468 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00008469 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00008470 if (put_user_s32(status, status_ptr))
8471 goto efault;
bellard31e31b82003-02-18 22:55:36 +00008472 }
Petar Jovanovica39fb272014-04-08 19:24:30 +02008473 if (target_rusage) {
8474 rusage_err = host_to_target_rusage(target_rusage, &rusage);
8475 if (rusage_err) {
8476 ret = rusage_err;
8477 }
8478 }
bellard31e31b82003-02-18 22:55:36 +00008479 }
8480 }
8481 break;
thse5febef2007-04-01 18:31:35 +00008482#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00008483 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00008484 if (!(p = lock_user_string(arg1)))
8485 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008486 ret = get_errno(swapoff(p));
8487 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00008488 break;
thse5febef2007-04-01 18:31:35 +00008489#endif
bellard31e31b82003-02-18 22:55:36 +00008490 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00008491 {
pbrook53a59602006-03-25 19:31:22 +00008492 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00008493 struct sysinfo value;
8494 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00008495 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00008496 {
bellard579a97f2007-11-11 14:26:47 +00008497 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
8498 goto efault;
bellarda5448a72004-06-19 16:59:03 +00008499 __put_user(value.uptime, &target_value->uptime);
8500 __put_user(value.loads[0], &target_value->loads[0]);
8501 __put_user(value.loads[1], &target_value->loads[1]);
8502 __put_user(value.loads[2], &target_value->loads[2]);
8503 __put_user(value.totalram, &target_value->totalram);
8504 __put_user(value.freeram, &target_value->freeram);
8505 __put_user(value.sharedram, &target_value->sharedram);
8506 __put_user(value.bufferram, &target_value->bufferram);
8507 __put_user(value.totalswap, &target_value->totalswap);
8508 __put_user(value.freeswap, &target_value->freeswap);
8509 __put_user(value.procs, &target_value->procs);
8510 __put_user(value.totalhigh, &target_value->totalhigh);
8511 __put_user(value.freehigh, &target_value->freehigh);
8512 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00008513 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00008514 }
8515 }
8516 break;
thse5febef2007-04-01 18:31:35 +00008517#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00008518 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00008519 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
8520 break;
thse5febef2007-04-01 18:31:35 +00008521#endif
aurel32e5289082009-04-18 16:16:12 +00008522#ifdef TARGET_NR_semget
8523 case TARGET_NR_semget:
8524 ret = get_errno(semget(arg1, arg2, arg3));
8525 break;
8526#endif
8527#ifdef TARGET_NR_semop
8528 case TARGET_NR_semop:
Petar Jovanovicc7128c92013-03-21 07:57:36 +00008529 ret = do_semop(arg1, arg2, arg3);
aurel32e5289082009-04-18 16:16:12 +00008530 break;
8531#endif
8532#ifdef TARGET_NR_semctl
8533 case TARGET_NR_semctl:
Stefan Weild1c002b2015-02-08 15:40:58 +01008534 ret = do_semctl(arg1, arg2, arg3, arg4);
aurel32e5289082009-04-18 16:16:12 +00008535 break;
8536#endif
aurel32eeb438c2008-10-13 21:08:55 +00008537#ifdef TARGET_NR_msgctl
8538 case TARGET_NR_msgctl:
8539 ret = do_msgctl(arg1, arg2, arg3);
8540 break;
8541#endif
8542#ifdef TARGET_NR_msgget
8543 case TARGET_NR_msgget:
8544 ret = get_errno(msgget(arg1, arg2));
8545 break;
8546#endif
8547#ifdef TARGET_NR_msgrcv
8548 case TARGET_NR_msgrcv:
8549 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
8550 break;
8551#endif
8552#ifdef TARGET_NR_msgsnd
8553 case TARGET_NR_msgsnd:
8554 ret = do_msgsnd(arg1, arg2, arg3, arg4);
8555 break;
8556#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03008557#ifdef TARGET_NR_shmget
8558 case TARGET_NR_shmget:
8559 ret = get_errno(shmget(arg1, arg2, arg3));
8560 break;
8561#endif
8562#ifdef TARGET_NR_shmctl
8563 case TARGET_NR_shmctl:
8564 ret = do_shmctl(arg1, arg2, arg3);
8565 break;
8566#endif
8567#ifdef TARGET_NR_shmat
8568 case TARGET_NR_shmat:
8569 ret = do_shmat(arg1, arg2, arg3);
8570 break;
8571#endif
8572#ifdef TARGET_NR_shmdt
8573 case TARGET_NR_shmdt:
8574 ret = do_shmdt(arg1);
8575 break;
8576#endif
bellard31e31b82003-02-18 22:55:36 +00008577 case TARGET_NR_fsync:
8578 ret = get_errno(fsync(arg1));
8579 break;
bellard31e31b82003-02-18 22:55:36 +00008580 case TARGET_NR_clone:
Peter Maydell4ce62432013-07-16 18:44:57 +01008581 /* Linux manages to have three different orderings for its
8582 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
8583 * match the kernel's CONFIG_CLONE_* settings.
8584 * Microblaze is further special in that it uses a sixth
8585 * implicit argument to clone for the TLS pointer.
8586 */
8587#if defined(TARGET_MICROBLAZE)
Edgar E. Iglesiasa5b3bdc2012-04-26 14:17:41 +02008588 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
Peter Maydell4ce62432013-07-16 18:44:57 +01008589#elif defined(TARGET_CLONE_BACKWARDS)
8590 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
8591#elif defined(TARGET_CLONE_BACKWARDS2)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02008592 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00008593#else
Peter Maydell4ce62432013-07-16 18:44:57 +01008594 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00008595#endif
bellard1b6b0292003-03-22 17:31:38 +00008596 break;
bellardec86b0f2003-04-11 00:15:04 +00008597#ifdef __NR_exit_group
8598 /* new thread calls */
8599 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02008600#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00008601 _mcleanup();
8602#endif
bellarde9009672005-04-26 20:42:36 +00008603 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00008604 ret = get_errno(exit_group(arg1));
8605 break;
8606#endif
bellard31e31b82003-02-18 22:55:36 +00008607 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00008608 if (!(p = lock_user_string(arg1)))
8609 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008610 ret = get_errno(setdomainname(p, arg2));
8611 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00008612 break;
8613 case TARGET_NR_uname:
8614 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00008615 {
8616 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00008617
bellard579a97f2007-11-11 14:26:47 +00008618 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
8619 goto efault;
bellard29e619b2004-09-13 21:41:04 +00008620 ret = get_errno(sys_uname(buf));
8621 if (!is_error(ret)) {
8622 /* Overrite the native machine name with whatever is being
8623 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01008624 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00008625 /* Allow the user to override the reported release. */
8626 if (qemu_uname_release && *qemu_uname_release)
8627 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00008628 }
pbrook53a59602006-03-25 19:31:22 +00008629 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00008630 }
bellard31e31b82003-02-18 22:55:36 +00008631 break;
bellard6dbad632003-03-16 18:05:05 +00008632#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00008633 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00008634 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00008635 break;
j_mayer84409dd2007-04-06 08:56:50 +00008636#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00008637 case TARGET_NR_vm86old:
8638 goto unimplemented;
8639 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00008640 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00008641 break;
8642#endif
j_mayer84409dd2007-04-06 08:56:50 +00008643#endif
bellard31e31b82003-02-18 22:55:36 +00008644 case TARGET_NR_adjtimex:
8645 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008646#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00008647 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00008648#endif
bellard31e31b82003-02-18 22:55:36 +00008649 case TARGET_NR_init_module:
8650 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00008651#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00008652 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00008653#endif
bellard31e31b82003-02-18 22:55:36 +00008654 goto unimplemented;
8655 case TARGET_NR_quotactl:
8656 goto unimplemented;
8657 case TARGET_NR_getpgid:
8658 ret = get_errno(getpgid(arg1));
8659 break;
8660 case TARGET_NR_fchdir:
8661 ret = get_errno(fchdir(arg1));
8662 break;
j_mayer84409dd2007-04-06 08:56:50 +00008663#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00008664 case TARGET_NR_bdflush:
8665 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00008666#endif
thse5febef2007-04-01 18:31:35 +00008667#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00008668 case TARGET_NR_sysfs:
8669 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008670#endif
bellard31e31b82003-02-18 22:55:36 +00008671 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00008672 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00008673 break;
thse5febef2007-04-01 18:31:35 +00008674#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00008675 case TARGET_NR_afs_syscall:
8676 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00008677#endif
j_mayer7a3148a2007-04-05 07:13:51 +00008678#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00008679 case TARGET_NR__llseek:
8680 {
8681 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00008682#if !defined(__NR_llseek)
8683 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
8684 if (res == -1) {
8685 ret = get_errno(res);
8686 } else {
8687 ret = 0;
8688 }
8689#else
bellard31e31b82003-02-18 22:55:36 +00008690 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00008691#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00008692 if ((ret == 0) && put_user_s64(res, arg4)) {
8693 goto efault;
8694 }
bellard31e31b82003-02-18 22:55:36 +00008695 }
8696 break;
j_mayer7a3148a2007-04-05 07:13:51 +00008697#endif
Chen Gang704eff62015-08-21 05:37:33 +08008698#ifdef TARGET_NR_getdents
bellard31e31b82003-02-18 22:55:36 +00008699 case TARGET_NR_getdents:
Peter Maydell3307e232013-06-12 16:20:21 +01008700#ifdef __NR_getdents
Ulrich Hechtd83c8732009-07-24 19:10:28 +02008701#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00008702 {
pbrook53a59602006-03-25 19:31:22 +00008703 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00008704 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00008705 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00008706
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05308707 dirp = g_try_malloc(count);
8708 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00008709 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00008710 goto fail;
8711 }
ths3b46e622007-09-17 08:09:54 +00008712
bellard4add45b2003-06-05 01:52:59 +00008713 ret = get_errno(sys_getdents(arg1, dirp, count));
8714 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00008715 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00008716 struct target_dirent *tde;
8717 int len = ret;
8718 int reclen, treclen;
8719 int count1, tnamelen;
8720
8721 count1 = 0;
8722 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00008723 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8724 goto efault;
bellard4add45b2003-06-05 01:52:59 +00008725 tde = target_dirp;
8726 while (len > 0) {
8727 reclen = de->d_reclen;
Dmitry V. Levin333858b2012-08-21 02:13:12 +04008728 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
8729 assert(tnamelen >= 0);
8730 treclen = tnamelen + offsetof(struct target_dirent, d_name);
8731 assert(count1 + treclen <= count);
bellard4add45b2003-06-05 01:52:59 +00008732 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02008733 tde->d_ino = tswapal(de->d_ino);
8734 tde->d_off = tswapal(de->d_off);
Dmitry V. Levin333858b2012-08-21 02:13:12 +04008735 memcpy(tde->d_name, de->d_name, tnamelen);
aurel326556a832008-10-13 21:08:17 +00008736 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00008737 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00008738 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00008739 count1 += treclen;
8740 }
8741 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00008742 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00008743 }
Harmandeep Kaur0e173b22015-10-06 21:47:12 +05308744 g_free(dirp);
bellard4add45b2003-06-05 01:52:59 +00008745 }
8746#else
bellard31e31b82003-02-18 22:55:36 +00008747 {
aurel326556a832008-10-13 21:08:17 +00008748 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00008749 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00008750
bellard579a97f2007-11-11 14:26:47 +00008751 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8752 goto efault;
bellard72f03902003-02-18 23:33:18 +00008753 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00008754 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00008755 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00008756 int len = ret;
8757 int reclen;
8758 de = dirp;
8759 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00008760 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00008761 if (reclen > len)
8762 break;
bellard8083a3e2003-03-24 23:12:16 +00008763 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00008764 tswapls(&de->d_ino);
8765 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00008766 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00008767 len -= reclen;
8768 }
8769 }
pbrook53a59602006-03-25 19:31:22 +00008770 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00008771 }
bellard4add45b2003-06-05 01:52:59 +00008772#endif
Peter Maydell3307e232013-06-12 16:20:21 +01008773#else
8774 /* Implement getdents in terms of getdents64 */
8775 {
8776 struct linux_dirent64 *dirp;
8777 abi_long count = arg3;
8778
8779 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8780 if (!dirp) {
8781 goto efault;
8782 }
8783 ret = get_errno(sys_getdents64(arg1, dirp, count));
8784 if (!is_error(ret)) {
8785 /* Convert the dirent64 structs to target dirent. We do this
8786 * in-place, since we can guarantee that a target_dirent is no
8787 * larger than a dirent64; however this means we have to be
8788 * careful to read everything before writing in the new format.
8789 */
8790 struct linux_dirent64 *de;
8791 struct target_dirent *tde;
8792 int len = ret;
8793 int tlen = 0;
8794
8795 de = dirp;
8796 tde = (struct target_dirent *)dirp;
8797 while (len > 0) {
8798 int namelen, treclen;
8799 int reclen = de->d_reclen;
8800 uint64_t ino = de->d_ino;
8801 int64_t off = de->d_off;
8802 uint8_t type = de->d_type;
8803
8804 namelen = strlen(de->d_name);
8805 treclen = offsetof(struct target_dirent, d_name)
8806 + namelen + 2;
8807 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
8808
8809 memmove(tde->d_name, de->d_name, namelen + 1);
8810 tde->d_ino = tswapal(ino);
8811 tde->d_off = tswapal(off);
8812 tde->d_reclen = tswap16(treclen);
8813 /* The target_dirent type is in what was formerly a padding
8814 * byte at the end of the structure:
8815 */
8816 *(((char *)tde) + treclen - 1) = type;
8817
8818 de = (struct linux_dirent64 *)((char *)de + reclen);
8819 tde = (struct target_dirent *)((char *)tde + treclen);
8820 len -= reclen;
8821 tlen += treclen;
8822 }
8823 ret = tlen;
8824 }
8825 unlock_user(dirp, arg2, ret);
8826 }
8827#endif
bellard31e31b82003-02-18 22:55:36 +00008828 break;
Chen Gang704eff62015-08-21 05:37:33 +08008829#endif /* TARGET_NR_getdents */
ths3ae43202007-09-16 21:39:48 +00008830#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00008831 case TARGET_NR_getdents64:
8832 {
aurel326556a832008-10-13 21:08:17 +00008833 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00008834 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00008835 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8836 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00008837 ret = get_errno(sys_getdents64(arg1, dirp, count));
8838 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00008839 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00008840 int len = ret;
8841 int reclen;
8842 de = dirp;
8843 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00008844 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00008845 if (reclen > len)
8846 break;
bellard8083a3e2003-03-24 23:12:16 +00008847 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00008848 tswap64s((uint64_t *)&de->d_ino);
8849 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00008850 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00008851 len -= reclen;
8852 }
8853 }
pbrook53a59602006-03-25 19:31:22 +00008854 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00008855 }
8856 break;
bellarda541f292004-04-12 20:39:29 +00008857#endif /* TARGET_NR_getdents64 */
Laurent Vivier9468a5d2013-01-10 22:30:50 +01008858#if defined(TARGET_NR__newselect)
bellard31e31b82003-02-18 22:55:36 +00008859 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00008860 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00008861 break;
thse5febef2007-04-01 18:31:35 +00008862#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05008863#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
8864# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00008865 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05008866# endif
8867# ifdef TARGET_NR_ppoll
8868 case TARGET_NR_ppoll:
8869# endif
bellard9de5e442003-03-23 16:49:39 +00008870 {
pbrook53a59602006-03-25 19:31:22 +00008871 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00008872 unsigned int nfds = arg2;
8873 int timeout = arg3;
8874 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00008875 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00008876
Laurent Vivier3e24bb32015-10-06 01:20:48 +02008877 pfd = NULL;
8878 target_pfd = NULL;
8879 if (nfds) {
8880 target_pfd = lock_user(VERIFY_WRITE, arg1,
8881 sizeof(struct target_pollfd) * nfds, 1);
8882 if (!target_pfd) {
8883 goto efault;
8884 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05008885
Laurent Vivier3e24bb32015-10-06 01:20:48 +02008886 pfd = alloca(sizeof(struct pollfd) * nfds);
8887 for (i = 0; i < nfds; i++) {
8888 pfd[i].fd = tswap32(target_pfd[i].fd);
8889 pfd[i].events = tswap16(target_pfd[i].events);
8890 }
bellard9de5e442003-03-23 16:49:39 +00008891 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05008892
8893# ifdef TARGET_NR_ppoll
8894 if (num == TARGET_NR_ppoll) {
8895 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
8896 target_sigset_t *target_set;
8897 sigset_t _set, *set = &_set;
8898
8899 if (arg3) {
8900 if (target_to_host_timespec(timeout_ts, arg3)) {
8901 unlock_user(target_pfd, arg1, 0);
8902 goto efault;
8903 }
8904 } else {
8905 timeout_ts = NULL;
8906 }
8907
8908 if (arg4) {
8909 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
8910 if (!target_set) {
8911 unlock_user(target_pfd, arg1, 0);
8912 goto efault;
8913 }
8914 target_to_host_sigset(set, target_set);
8915 } else {
8916 set = NULL;
8917 }
8918
8919 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
8920
8921 if (!is_error(ret) && arg3) {
8922 host_to_target_timespec(arg3, timeout_ts);
8923 }
8924 if (arg4) {
8925 unlock_user(target_set, arg4, 0);
8926 }
8927 } else
8928# endif
8929 ret = get_errno(poll(pfd, nfds, timeout));
8930
bellard9de5e442003-03-23 16:49:39 +00008931 if (!is_error(ret)) {
8932 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00008933 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00008934 }
8935 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00008936 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00008937 }
8938 break;
thse5febef2007-04-01 18:31:35 +00008939#endif
bellard31e31b82003-02-18 22:55:36 +00008940 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00008941 /* NOTE: the flock constant seems to be the same for every
8942 Linux platform */
8943 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00008944 break;
8945 case TARGET_NR_readv:
8946 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008947 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
8948 if (vec != NULL) {
8949 ret = get_errno(readv(arg1, vec, arg3));
8950 unlock_iovec(vec, arg2, arg3, 1);
8951 } else {
8952 ret = -host_to_target_errno(errno);
8953 }
bellard31e31b82003-02-18 22:55:36 +00008954 }
8955 break;
8956 case TARGET_NR_writev:
8957 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008958 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8959 if (vec != NULL) {
8960 ret = get_errno(writev(arg1, vec, arg3));
8961 unlock_iovec(vec, arg2, arg3, 0);
8962 } else {
8963 ret = -host_to_target_errno(errno);
8964 }
bellard31e31b82003-02-18 22:55:36 +00008965 }
8966 break;
8967 case TARGET_NR_getsid:
8968 ret = get_errno(getsid(arg1));
8969 break;
j_mayer7a3148a2007-04-05 07:13:51 +00008970#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00008971 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00008972 ret = get_errno(fdatasync(arg1));
8973 break;
j_mayer7a3148a2007-04-05 07:13:51 +00008974#endif
Chen Gang704eff62015-08-21 05:37:33 +08008975#ifdef TARGET_NR__sysctl
bellard31e31b82003-02-18 22:55:36 +00008976 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00008977 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00008978 return value. */
ths0da46a62007-10-20 20:23:07 +00008979 ret = -TARGET_ENOTDIR;
8980 break;
Chen Gang704eff62015-08-21 05:37:33 +08008981#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -05008982 case TARGET_NR_sched_getaffinity:
8983 {
8984 unsigned int mask_size;
8985 unsigned long *mask;
8986
8987 /*
8988 * sched_getaffinity needs multiples of ulong, so need to take
8989 * care of mismatches between target ulong and host ulong sizes.
8990 */
8991 if (arg2 & (sizeof(abi_ulong) - 1)) {
8992 ret = -TARGET_EINVAL;
8993 break;
8994 }
8995 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
8996
8997 mask = alloca(mask_size);
8998 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
8999
9000 if (!is_error(ret)) {
Peter Maydellbe3bd282014-05-15 14:40:23 +01009001 if (ret > arg2) {
9002 /* More data returned than the caller's buffer will fit.
9003 * This only happens if sizeof(abi_long) < sizeof(long)
9004 * and the caller passed us a buffer holding an odd number
9005 * of abi_longs. If the host kernel is actually using the
9006 * extra 4 bytes then fail EINVAL; otherwise we can just
9007 * ignore them and only copy the interesting part.
9008 */
9009 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
9010 if (numcpus > arg2 * 8) {
9011 ret = -TARGET_EINVAL;
9012 break;
9013 }
9014 ret = arg2;
9015 }
9016
Mike McCormackcd18f052011-04-18 14:43:36 +09009017 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05009018 goto efault;
9019 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05009020 }
9021 }
9022 break;
9023 case TARGET_NR_sched_setaffinity:
9024 {
9025 unsigned int mask_size;
9026 unsigned long *mask;
9027
9028 /*
9029 * sched_setaffinity needs multiples of ulong, so need to take
9030 * care of mismatches between target ulong and host ulong sizes.
9031 */
9032 if (arg2 & (sizeof(abi_ulong) - 1)) {
9033 ret = -TARGET_EINVAL;
9034 break;
9035 }
9036 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
9037
9038 mask = alloca(mask_size);
9039 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
9040 goto efault;
9041 }
9042 memcpy(mask, p, arg2);
9043 unlock_user_struct(p, arg2, 0);
9044
9045 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
9046 }
9047 break;
bellard31e31b82003-02-18 22:55:36 +00009048 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00009049 {
pbrook53a59602006-03-25 19:31:22 +00009050 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00009051 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00009052
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05009053 if (arg2 == 0) {
9054 return -TARGET_EINVAL;
9055 }
bellard579a97f2007-11-11 14:26:47 +00009056 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
9057 goto efault;
bellard5cd43932003-03-29 16:54:36 +00009058 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00009059 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00009060 ret = get_errno(sched_setparam(arg1, &schp));
9061 }
9062 break;
bellard31e31b82003-02-18 22:55:36 +00009063 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00009064 {
pbrook53a59602006-03-25 19:31:22 +00009065 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00009066 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05009067
9068 if (arg2 == 0) {
9069 return -TARGET_EINVAL;
9070 }
bellard5cd43932003-03-29 16:54:36 +00009071 ret = get_errno(sched_getparam(arg1, &schp));
9072 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00009073 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
9074 goto efault;
bellard5cd43932003-03-29 16:54:36 +00009075 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00009076 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00009077 }
9078 }
9079 break;
bellard31e31b82003-02-18 22:55:36 +00009080 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00009081 {
pbrook53a59602006-03-25 19:31:22 +00009082 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00009083 struct sched_param schp;
Tom Mustaa1d5c5b2014-08-12 13:53:38 -05009084 if (arg3 == 0) {
9085 return -TARGET_EINVAL;
9086 }
bellard579a97f2007-11-11 14:26:47 +00009087 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
9088 goto efault;
bellard5cd43932003-03-29 16:54:36 +00009089 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00009090 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00009091 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
9092 }
9093 break;
bellard31e31b82003-02-18 22:55:36 +00009094 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00009095 ret = get_errno(sched_getscheduler(arg1));
9096 break;
bellard31e31b82003-02-18 22:55:36 +00009097 case TARGET_NR_sched_yield:
9098 ret = get_errno(sched_yield());
9099 break;
9100 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00009101 ret = get_errno(sched_get_priority_max(arg1));
9102 break;
bellard31e31b82003-02-18 22:55:36 +00009103 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00009104 ret = get_errno(sched_get_priority_min(arg1));
9105 break;
bellard31e31b82003-02-18 22:55:36 +00009106 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00009107 {
bellard5cd43932003-03-29 16:54:36 +00009108 struct timespec ts;
9109 ret = get_errno(sched_rr_get_interval(arg1, &ts));
9110 if (!is_error(ret)) {
Tom Mustad4290c42014-08-12 13:53:39 -05009111 ret = host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00009112 }
9113 }
9114 break;
bellard31e31b82003-02-18 22:55:36 +00009115 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00009116 {
bellard1b6b0292003-03-22 17:31:38 +00009117 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00009118 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00009119 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00009120 if (is_error(ret) && arg2) {
9121 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00009122 }
9123 }
9124 break;
thse5febef2007-04-01 18:31:35 +00009125#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00009126 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00009127 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00009128#endif
9129#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00009130 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00009131 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00009132#endif
bellard31e31b82003-02-18 22:55:36 +00009133 case TARGET_NR_prctl:
Peter Maydell1e6722f2012-02-03 14:48:03 +00009134 switch (arg1) {
9135 case PR_GET_PDEATHSIG:
9136 {
9137 int deathsig;
9138 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
9139 if (!is_error(ret) && arg2
9140 && put_user_ual(deathsig, arg2)) {
9141 goto efault;
thse5574482007-02-11 20:03:13 +00009142 }
Peter Maydell1e6722f2012-02-03 14:48:03 +00009143 break;
9144 }
Peter Maydelldb9526b2012-02-03 14:48:03 +00009145#ifdef PR_GET_NAME
9146 case PR_GET_NAME:
9147 {
9148 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
9149 if (!name) {
9150 goto efault;
9151 }
9152 ret = get_errno(prctl(arg1, (unsigned long)name,
9153 arg3, arg4, arg5));
9154 unlock_user(name, arg2, 16);
9155 break;
9156 }
9157 case PR_SET_NAME:
9158 {
9159 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
9160 if (!name) {
9161 goto efault;
9162 }
9163 ret = get_errno(prctl(arg1, (unsigned long)name,
9164 arg3, arg4, arg5));
9165 unlock_user(name, arg2, 0);
9166 break;
9167 }
9168#endif
Peter Maydell1e6722f2012-02-03 14:48:03 +00009169 default:
9170 /* Most prctl options have no pointer arguments */
9171 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
9172 break;
9173 }
ths39b9aae2007-02-11 18:36:44 +00009174 break;
bellardd2fd1af2007-11-14 18:08:56 +00009175#ifdef TARGET_NR_arch_prctl
9176 case TARGET_NR_arch_prctl:
9177#if defined(TARGET_I386) && !defined(TARGET_ABI32)
9178 ret = do_arch_prctl(cpu_env, arg1, arg2);
9179 break;
9180#else
9181 goto unimplemented;
9182#endif
9183#endif
aurel32f2c7ba12008-03-28 22:32:06 +00009184#ifdef TARGET_NR_pread64
9185 case TARGET_NR_pread64:
Alexander Grafae017a52012-09-29 15:32:39 +00009186 if (regpairs_aligned(cpu_env)) {
9187 arg4 = arg5;
9188 arg5 = arg6;
9189 }
aurel32f2c7ba12008-03-28 22:32:06 +00009190 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
9191 goto efault;
9192 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
9193 unlock_user(p, arg2, ret);
9194 break;
9195 case TARGET_NR_pwrite64:
Alexander Grafae017a52012-09-29 15:32:39 +00009196 if (regpairs_aligned(cpu_env)) {
9197 arg4 = arg5;
9198 arg5 = arg6;
9199 }
aurel32f2c7ba12008-03-28 22:32:06 +00009200 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
9201 goto efault;
9202 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
9203 unlock_user(p, arg2, 0);
9204 break;
9205#endif
bellard31e31b82003-02-18 22:55:36 +00009206 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00009207 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
9208 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009209 ret = get_errno(sys_getcwd1(p, arg2));
9210 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00009211 break;
9212 case TARGET_NR_capget:
9213 case TARGET_NR_capset:
Peter Maydelle0eb2102014-03-17 12:15:35 +00009214 {
9215 struct target_user_cap_header *target_header;
9216 struct target_user_cap_data *target_data = NULL;
9217 struct __user_cap_header_struct header;
9218 struct __user_cap_data_struct data[2];
9219 struct __user_cap_data_struct *dataptr = NULL;
9220 int i, target_datalen;
9221 int data_items = 1;
9222
9223 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
9224 goto efault;
9225 }
9226 header.version = tswap32(target_header->version);
9227 header.pid = tswap32(target_header->pid);
9228
Peter Maydellec864872014-03-19 16:07:30 +00009229 if (header.version != _LINUX_CAPABILITY_VERSION) {
Peter Maydelle0eb2102014-03-17 12:15:35 +00009230 /* Version 2 and up takes pointer to two user_data structs */
9231 data_items = 2;
9232 }
9233
9234 target_datalen = sizeof(*target_data) * data_items;
9235
9236 if (arg2) {
9237 if (num == TARGET_NR_capget) {
9238 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
9239 } else {
9240 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
9241 }
9242 if (!target_data) {
9243 unlock_user_struct(target_header, arg1, 0);
9244 goto efault;
9245 }
9246
9247 if (num == TARGET_NR_capset) {
9248 for (i = 0; i < data_items; i++) {
9249 data[i].effective = tswap32(target_data[i].effective);
9250 data[i].permitted = tswap32(target_data[i].permitted);
9251 data[i].inheritable = tswap32(target_data[i].inheritable);
9252 }
9253 }
9254
9255 dataptr = data;
9256 }
9257
9258 if (num == TARGET_NR_capget) {
9259 ret = get_errno(capget(&header, dataptr));
9260 } else {
9261 ret = get_errno(capset(&header, dataptr));
9262 }
9263
9264 /* The kernel always updates version for both capget and capset */
9265 target_header->version = tswap32(header.version);
9266 unlock_user_struct(target_header, arg1, 1);
9267
9268 if (arg2) {
9269 if (num == TARGET_NR_capget) {
9270 for (i = 0; i < data_items; i++) {
9271 target_data[i].effective = tswap32(data[i].effective);
9272 target_data[i].permitted = tswap32(data[i].permitted);
9273 target_data[i].inheritable = tswap32(data[i].inheritable);
9274 }
9275 unlock_user(target_data, arg2, target_datalen);
9276 } else {
9277 unlock_user(target_data, arg2, 0);
9278 }
9279 }
9280 break;
9281 }
bellard31e31b82003-02-18 22:55:36 +00009282 case TARGET_NR_sigaltstack:
Andreas Färber9349b4f2012-03-14 01:38:32 +01009283 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00009284 break;
Peter Maydella8fd1ab2013-02-08 07:31:55 +00009285
9286#ifdef CONFIG_SENDFILE
bellard31e31b82003-02-18 22:55:36 +00009287 case TARGET_NR_sendfile:
Peter Maydella8fd1ab2013-02-08 07:31:55 +00009288 {
9289 off_t *offp = NULL;
9290 off_t off;
9291 if (arg3) {
9292 ret = get_user_sal(off, arg3);
9293 if (is_error(ret)) {
9294 break;
9295 }
9296 offp = &off;
9297 }
9298 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
9299 if (!is_error(ret) && arg3) {
9300 abi_long ret2 = put_user_sal(off, arg3);
9301 if (is_error(ret2)) {
9302 ret = ret2;
9303 }
9304 }
9305 break;
9306 }
9307#ifdef TARGET_NR_sendfile64
9308 case TARGET_NR_sendfile64:
9309 {
9310 off_t *offp = NULL;
9311 off_t off;
9312 if (arg3) {
9313 ret = get_user_s64(off, arg3);
9314 if (is_error(ret)) {
9315 break;
9316 }
9317 offp = &off;
9318 }
9319 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
9320 if (!is_error(ret) && arg3) {
9321 abi_long ret2 = put_user_s64(off, arg3);
9322 if (is_error(ret2)) {
9323 ret = ret2;
9324 }
9325 }
9326 break;
9327 }
9328#endif
9329#else
9330 case TARGET_NR_sendfile:
Peter Maydell7edd2cf2013-04-21 13:30:03 +01009331#ifdef TARGET_NR_sendfile64
Peter Maydella8fd1ab2013-02-08 07:31:55 +00009332 case TARGET_NR_sendfile64:
9333#endif
bellard5cd43932003-03-29 16:54:36 +00009334 goto unimplemented;
Peter Maydella8fd1ab2013-02-08 07:31:55 +00009335#endif
9336
bellardebc05482003-09-30 21:08:41 +00009337#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00009338 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00009339 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00009340#endif
9341#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00009342 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00009343 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00009344#endif
bellard048f6b42005-11-26 18:47:20 +00009345#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00009346 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00009347 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
9348 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00009349 break;
bellard048f6b42005-11-26 18:47:20 +00009350#endif
bellardebc05482003-09-30 21:08:41 +00009351#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00009352 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00009353 {
9354 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03009355 int resource = target_to_host_resource(arg1);
9356 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00009357 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00009358 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00009359 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
9360 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09009361 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
9362 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00009363 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00009364 }
9365 break;
9366 }
bellardebc05482003-09-30 21:08:41 +00009367#endif
bellarda315a142005-01-30 22:59:18 +00009368#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00009369 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00009370 if (!(p = lock_user_string(arg1)))
9371 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009372 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
9373 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00009374 break;
bellarda315a142005-01-30 22:59:18 +00009375#endif
9376#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00009377 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00009378 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00009379 break;
bellarda315a142005-01-30 22:59:18 +00009380#endif
9381#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00009382 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00009383 if (!(p = lock_user_string(arg1)))
9384 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009385 ret = get_errno(stat(path(p), &st));
9386 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00009387 if (!is_error(ret))
9388 ret = host_to_target_stat64(cpu_env, arg2, &st);
9389 break;
bellarda315a142005-01-30 22:59:18 +00009390#endif
9391#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00009392 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00009393 if (!(p = lock_user_string(arg1)))
9394 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009395 ret = get_errno(lstat(path(p), &st));
9396 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00009397 if (!is_error(ret))
9398 ret = host_to_target_stat64(cpu_env, arg2, &st);
9399 break;
bellarda315a142005-01-30 22:59:18 +00009400#endif
9401#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00009402 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00009403 ret = get_errno(fstat(arg1, &st));
9404 if (!is_error(ret))
9405 ret = host_to_target_stat64(cpu_env, arg2, &st);
9406 break;
bellardec86b0f2003-04-11 00:15:04 +00009407#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01009408#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
aurel329d33b762009-04-08 23:07:05 +00009409#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00009410 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00009411#endif
9412#ifdef TARGET_NR_newfstatat
9413 case TARGET_NR_newfstatat:
9414#endif
balrog6a24a772008-09-20 02:23:36 +00009415 if (!(p = lock_user_string(arg2)))
9416 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01009417 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
balrog6a24a772008-09-20 02:23:36 +00009418 if (!is_error(ret))
9419 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00009420 break;
bellarda315a142005-01-30 22:59:18 +00009421#endif
Chen Gang704eff62015-08-21 05:37:33 +08009422#ifdef TARGET_NR_lchown
bellard67867302003-11-23 17:05:30 +00009423 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00009424 if (!(p = lock_user_string(arg1)))
9425 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009426 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
9427 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00009428 break;
Chen Gang704eff62015-08-21 05:37:33 +08009429#endif
Riku Voipio0c866a72011-04-18 15:23:06 +03009430#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00009431 case TARGET_NR_getuid:
9432 ret = get_errno(high2lowuid(getuid()));
9433 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03009434#endif
9435#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00009436 case TARGET_NR_getgid:
9437 ret = get_errno(high2lowgid(getgid()));
9438 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03009439#endif
9440#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00009441 case TARGET_NR_geteuid:
9442 ret = get_errno(high2lowuid(geteuid()));
9443 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03009444#endif
9445#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00009446 case TARGET_NR_getegid:
9447 ret = get_errno(high2lowgid(getegid()));
9448 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03009449#endif
bellard67867302003-11-23 17:05:30 +00009450 case TARGET_NR_setreuid:
9451 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
9452 break;
9453 case TARGET_NR_setregid:
9454 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
9455 break;
9456 case TARGET_NR_getgroups:
9457 {
9458 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03009459 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00009460 gid_t *grouplist;
9461 int i;
9462
9463 grouplist = alloca(gidsetsize * sizeof(gid_t));
9464 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00009465 if (gidsetsize == 0)
9466 break;
bellard67867302003-11-23 17:05:30 +00009467 if (!is_error(ret)) {
Andreas Schwab03903ff2013-04-09 05:41:33 +00009468 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
bellard579a97f2007-11-11 14:26:47 +00009469 if (!target_grouplist)
9470 goto efault;
balroga2155fc2008-09-20 02:12:08 +00009471 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03009472 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
Andreas Schwab03903ff2013-04-09 05:41:33 +00009473 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
bellard67867302003-11-23 17:05:30 +00009474 }
9475 }
9476 break;
9477 case TARGET_NR_setgroups:
9478 {
9479 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03009480 target_id *target_grouplist;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05009481 gid_t *grouplist = NULL;
bellard67867302003-11-23 17:05:30 +00009482 int i;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05009483 if (gidsetsize) {
9484 grouplist = alloca(gidsetsize * sizeof(gid_t));
Andreas Schwab03903ff2013-04-09 05:41:33 +00009485 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05009486 if (!target_grouplist) {
9487 ret = -TARGET_EFAULT;
9488 goto fail;
9489 }
9490 for (i = 0; i < gidsetsize; i++) {
9491 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
9492 }
9493 unlock_user(target_grouplist, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00009494 }
bellard67867302003-11-23 17:05:30 +00009495 ret = get_errno(setgroups(gidsetsize, grouplist));
9496 }
9497 break;
9498 case TARGET_NR_fchown:
9499 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
9500 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01009501#if defined(TARGET_NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +00009502 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00009503 if (!(p = lock_user_string(arg2)))
9504 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01009505 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
9506 low2highgid(arg4), arg5));
bellard579a97f2007-11-11 14:26:47 +00009507 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00009508 break;
9509#endif
bellard67867302003-11-23 17:05:30 +00009510#ifdef TARGET_NR_setresuid
9511 case TARGET_NR_setresuid:
Peter Maydellfd6f7792016-03-01 16:33:02 +00009512 ret = get_errno(sys_setresuid(low2highuid(arg1),
9513 low2highuid(arg2),
9514 low2highuid(arg3)));
bellard67867302003-11-23 17:05:30 +00009515 break;
9516#endif
9517#ifdef TARGET_NR_getresuid
9518 case TARGET_NR_getresuid:
9519 {
pbrook53a59602006-03-25 19:31:22 +00009520 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00009521 ret = get_errno(getresuid(&ruid, &euid, &suid));
9522 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +00009523 if (put_user_id(high2lowuid(ruid), arg1)
9524 || put_user_id(high2lowuid(euid), arg2)
9525 || put_user_id(high2lowuid(suid), arg3))
bellard2f619692007-11-16 10:46:05 +00009526 goto efault;
bellard67867302003-11-23 17:05:30 +00009527 }
9528 }
9529 break;
9530#endif
9531#ifdef TARGET_NR_getresgid
9532 case TARGET_NR_setresgid:
Peter Maydellfd6f7792016-03-01 16:33:02 +00009533 ret = get_errno(sys_setresgid(low2highgid(arg1),
9534 low2highgid(arg2),
9535 low2highgid(arg3)));
bellard67867302003-11-23 17:05:30 +00009536 break;
9537#endif
9538#ifdef TARGET_NR_getresgid
9539 case TARGET_NR_getresgid:
9540 {
pbrook53a59602006-03-25 19:31:22 +00009541 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00009542 ret = get_errno(getresgid(&rgid, &egid, &sgid));
9543 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +00009544 if (put_user_id(high2lowgid(rgid), arg1)
9545 || put_user_id(high2lowgid(egid), arg2)
9546 || put_user_id(high2lowgid(sgid), arg3))
bellard2f619692007-11-16 10:46:05 +00009547 goto efault;
bellard67867302003-11-23 17:05:30 +00009548 }
9549 }
9550 break;
9551#endif
Chen Gang704eff62015-08-21 05:37:33 +08009552#ifdef TARGET_NR_chown
bellard67867302003-11-23 17:05:30 +00009553 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00009554 if (!(p = lock_user_string(arg1)))
9555 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009556 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
9557 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00009558 break;
Chen Gang704eff62015-08-21 05:37:33 +08009559#endif
bellard67867302003-11-23 17:05:30 +00009560 case TARGET_NR_setuid:
Peter Maydellfd6f7792016-03-01 16:33:02 +00009561 ret = get_errno(sys_setuid(low2highuid(arg1)));
bellard67867302003-11-23 17:05:30 +00009562 break;
9563 case TARGET_NR_setgid:
Peter Maydellfd6f7792016-03-01 16:33:02 +00009564 ret = get_errno(sys_setgid(low2highgid(arg1)));
bellard67867302003-11-23 17:05:30 +00009565 break;
9566 case TARGET_NR_setfsuid:
9567 ret = get_errno(setfsuid(arg1));
9568 break;
9569 case TARGET_NR_setfsgid:
9570 ret = get_errno(setfsgid(arg1));
9571 break;
bellard67867302003-11-23 17:05:30 +00009572
bellarda315a142005-01-30 22:59:18 +00009573#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00009574 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00009575 if (!(p = lock_user_string(arg1)))
9576 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009577 ret = get_errno(lchown(p, arg2, arg3));
9578 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00009579 break;
bellarda315a142005-01-30 22:59:18 +00009580#endif
9581#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00009582 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00009583 ret = get_errno(getuid());
9584 break;
bellarda315a142005-01-30 22:59:18 +00009585#endif
aurel3264b4d282008-11-14 17:20:15 +00009586
9587#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
9588 /* Alpha specific */
9589 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08009590 {
9591 uid_t euid;
9592 euid=geteuid();
9593 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
9594 }
aurel3264b4d282008-11-14 17:20:15 +00009595 ret = get_errno(getuid());
9596 break;
9597#endif
9598#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
9599 /* Alpha specific */
9600 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08009601 {
9602 uid_t egid;
9603 egid=getegid();
9604 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
9605 }
aurel3264b4d282008-11-14 17:20:15 +00009606 ret = get_errno(getgid());
9607 break;
9608#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08009609#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
9610 /* Alpha specific */
9611 case TARGET_NR_osf_getsysinfo:
9612 ret = -TARGET_EOPNOTSUPP;
9613 switch (arg1) {
9614 case TARGET_GSI_IEEE_FP_CONTROL:
9615 {
9616 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
9617
9618 /* Copied from linux ieee_fpcr_to_swcr. */
9619 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
9620 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
9621 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
9622 | SWCR_TRAP_ENABLE_DZE
9623 | SWCR_TRAP_ENABLE_OVF);
9624 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
9625 | SWCR_TRAP_ENABLE_INE);
9626 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
9627 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
9628
9629 if (put_user_u64 (swcr, arg2))
9630 goto efault;
9631 ret = 0;
9632 }
9633 break;
9634
9635 /* case GSI_IEEE_STATE_AT_SIGNAL:
9636 -- Not implemented in linux kernel.
9637 case GSI_UACPROC:
9638 -- Retrieves current unaligned access state; not much used.
9639 case GSI_PROC_TYPE:
9640 -- Retrieves implver information; surely not used.
9641 case GSI_GET_HWRPB:
9642 -- Grabs a copy of the HWRPB; surely not used.
9643 */
9644 }
9645 break;
9646#endif
9647#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
9648 /* Alpha specific */
9649 case TARGET_NR_osf_setsysinfo:
9650 ret = -TARGET_EOPNOTSUPP;
9651 switch (arg1) {
9652 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -08009653 {
9654 uint64_t swcr, fpcr, orig_fpcr;
9655
Richard Henderson6e06d512012-06-01 09:08:21 -07009656 if (get_user_u64 (swcr, arg2)) {
Richard Hendersonba0e2762009-12-09 15:56:29 -08009657 goto efault;
Richard Henderson6e06d512012-06-01 09:08:21 -07009658 }
9659 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -08009660 fpcr = orig_fpcr & FPCR_DYN_MASK;
9661
9662 /* Copied from linux ieee_swcr_to_fpcr. */
9663 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
9664 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
9665 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
9666 | SWCR_TRAP_ENABLE_DZE
9667 | SWCR_TRAP_ENABLE_OVF)) << 48;
9668 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
9669 | SWCR_TRAP_ENABLE_INE)) << 57;
9670 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
9671 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
9672
Richard Henderson6e06d512012-06-01 09:08:21 -07009673 cpu_alpha_store_fpcr(cpu_env, fpcr);
9674 ret = 0;
9675 }
9676 break;
9677
9678 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
9679 {
9680 uint64_t exc, fpcr, orig_fpcr;
9681 int si_code;
9682
9683 if (get_user_u64(exc, arg2)) {
9684 goto efault;
9685 }
9686
9687 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
9688
9689 /* We only add to the exception status here. */
9690 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
9691
9692 cpu_alpha_store_fpcr(cpu_env, fpcr);
Richard Hendersonba0e2762009-12-09 15:56:29 -08009693 ret = 0;
9694
Richard Henderson6e06d512012-06-01 09:08:21 -07009695 /* Old exceptions are not signaled. */
9696 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -08009697
Richard Henderson6e06d512012-06-01 09:08:21 -07009698 /* If any exceptions set by this call,
9699 and are unmasked, send a signal. */
9700 si_code = 0;
9701 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
9702 si_code = TARGET_FPE_FLTRES;
9703 }
9704 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
9705 si_code = TARGET_FPE_FLTUND;
9706 }
9707 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
9708 si_code = TARGET_FPE_FLTOVF;
9709 }
9710 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
9711 si_code = TARGET_FPE_FLTDIV;
9712 }
9713 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
9714 si_code = TARGET_FPE_FLTINV;
9715 }
9716 if (si_code != 0) {
9717 target_siginfo_t info;
9718 info.si_signo = SIGFPE;
9719 info.si_errno = 0;
9720 info.si_code = si_code;
9721 info._sifields._sigfault._addr
9722 = ((CPUArchState *)cpu_env)->pc;
9723 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -08009724 }
9725 }
9726 break;
9727
9728 /* case SSI_NVPAIRS:
9729 -- Used with SSIN_UACPROC to enable unaligned accesses.
9730 case SSI_IEEE_STATE_AT_SIGNAL:
9731 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
9732 -- Not implemented in linux kernel
9733 */
9734 }
9735 break;
9736#endif
9737#ifdef TARGET_NR_osf_sigprocmask
9738 /* Alpha specific. */
9739 case TARGET_NR_osf_sigprocmask:
9740 {
9741 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01009742 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08009743 sigset_t set, oldset;
9744
9745 switch(arg1) {
9746 case TARGET_SIG_BLOCK:
9747 how = SIG_BLOCK;
9748 break;
9749 case TARGET_SIG_UNBLOCK:
9750 how = SIG_UNBLOCK;
9751 break;
9752 case TARGET_SIG_SETMASK:
9753 how = SIG_SETMASK;
9754 break;
9755 default:
9756 ret = -TARGET_EINVAL;
9757 goto fail;
9758 }
9759 mask = arg2;
9760 target_to_host_old_sigset(&set, &mask);
Alex Barcelo1c275922014-03-14 14:36:55 +00009761 do_sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08009762 host_to_target_old_sigset(&mask, &oldset);
9763 ret = mask;
9764 }
9765 break;
9766#endif
aurel3264b4d282008-11-14 17:20:15 +00009767
bellarda315a142005-01-30 22:59:18 +00009768#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00009769 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00009770 ret = get_errno(getgid());
9771 break;
bellarda315a142005-01-30 22:59:18 +00009772#endif
9773#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00009774 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00009775 ret = get_errno(geteuid());
9776 break;
bellarda315a142005-01-30 22:59:18 +00009777#endif
9778#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00009779 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00009780 ret = get_errno(getegid());
9781 break;
bellarda315a142005-01-30 22:59:18 +00009782#endif
9783#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00009784 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00009785 ret = get_errno(setreuid(arg1, arg2));
9786 break;
bellarda315a142005-01-30 22:59:18 +00009787#endif
9788#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00009789 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00009790 ret = get_errno(setregid(arg1, arg2));
9791 break;
bellarda315a142005-01-30 22:59:18 +00009792#endif
9793#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00009794 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00009795 {
9796 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00009797 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00009798 gid_t *grouplist;
9799 int i;
9800
9801 grouplist = alloca(gidsetsize * sizeof(gid_t));
9802 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00009803 if (gidsetsize == 0)
9804 break;
bellard99c475a2005-01-31 20:45:13 +00009805 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00009806 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
9807 if (!target_grouplist) {
9808 ret = -TARGET_EFAULT;
9809 goto fail;
9810 }
balroga2155fc2008-09-20 02:12:08 +00009811 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00009812 target_grouplist[i] = tswap32(grouplist[i]);
9813 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00009814 }
9815 }
9816 break;
bellarda315a142005-01-30 22:59:18 +00009817#endif
9818#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00009819 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00009820 {
9821 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00009822 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00009823 gid_t *grouplist;
9824 int i;
ths3b46e622007-09-17 08:09:54 +00009825
bellard99c475a2005-01-31 20:45:13 +00009826 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00009827 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
9828 if (!target_grouplist) {
9829 ret = -TARGET_EFAULT;
9830 goto fail;
9831 }
bellard99c475a2005-01-31 20:45:13 +00009832 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00009833 grouplist[i] = tswap32(target_grouplist[i]);
9834 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00009835 ret = get_errno(setgroups(gidsetsize, grouplist));
9836 }
9837 break;
bellarda315a142005-01-30 22:59:18 +00009838#endif
9839#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00009840 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00009841 ret = get_errno(fchown(arg1, arg2, arg3));
9842 break;
bellarda315a142005-01-30 22:59:18 +00009843#endif
9844#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00009845 case TARGET_NR_setresuid32:
Peter Maydellfd6f7792016-03-01 16:33:02 +00009846 ret = get_errno(sys_setresuid(arg1, arg2, arg3));
bellardb03c60f2003-03-23 17:19:56 +00009847 break;
bellarda315a142005-01-30 22:59:18 +00009848#endif
9849#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00009850 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00009851 {
pbrook53a59602006-03-25 19:31:22 +00009852 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00009853 ret = get_errno(getresuid(&ruid, &euid, &suid));
9854 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00009855 if (put_user_u32(ruid, arg1)
9856 || put_user_u32(euid, arg2)
9857 || put_user_u32(suid, arg3))
9858 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00009859 }
9860 }
9861 break;
bellarda315a142005-01-30 22:59:18 +00009862#endif
9863#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00009864 case TARGET_NR_setresgid32:
Peter Maydellfd6f7792016-03-01 16:33:02 +00009865 ret = get_errno(sys_setresgid(arg1, arg2, arg3));
bellardb03c60f2003-03-23 17:19:56 +00009866 break;
bellarda315a142005-01-30 22:59:18 +00009867#endif
9868#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00009869 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00009870 {
pbrook53a59602006-03-25 19:31:22 +00009871 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00009872 ret = get_errno(getresgid(&rgid, &egid, &sgid));
9873 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00009874 if (put_user_u32(rgid, arg1)
9875 || put_user_u32(egid, arg2)
9876 || put_user_u32(sgid, arg3))
9877 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00009878 }
9879 }
9880 break;
bellarda315a142005-01-30 22:59:18 +00009881#endif
9882#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00009883 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00009884 if (!(p = lock_user_string(arg1)))
9885 goto efault;
pbrook53a59602006-03-25 19:31:22 +00009886 ret = get_errno(chown(p, arg2, arg3));
9887 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00009888 break;
bellarda315a142005-01-30 22:59:18 +00009889#endif
9890#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00009891 case TARGET_NR_setuid32:
Peter Maydellfd6f7792016-03-01 16:33:02 +00009892 ret = get_errno(sys_setuid(arg1));
bellardb03c60f2003-03-23 17:19:56 +00009893 break;
bellarda315a142005-01-30 22:59:18 +00009894#endif
9895#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00009896 case TARGET_NR_setgid32:
Peter Maydellfd6f7792016-03-01 16:33:02 +00009897 ret = get_errno(sys_setgid(arg1));
bellardb03c60f2003-03-23 17:19:56 +00009898 break;
bellarda315a142005-01-30 22:59:18 +00009899#endif
9900#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00009901 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00009902 ret = get_errno(setfsuid(arg1));
9903 break;
bellarda315a142005-01-30 22:59:18 +00009904#endif
9905#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00009906 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00009907 ret = get_errno(setfsgid(arg1));
9908 break;
bellarda315a142005-01-30 22:59:18 +00009909#endif
bellard67867302003-11-23 17:05:30 +00009910
bellard31e31b82003-02-18 22:55:36 +00009911 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00009912 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00009913#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00009914 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00009915 {
9916 void *a;
9917 ret = -TARGET_EFAULT;
9918 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
9919 goto efault;
9920 if (!(p = lock_user_string(arg3)))
9921 goto mincore_fail;
9922 ret = get_errno(mincore(a, arg2, p));
9923 unlock_user(p, arg3, ret);
9924 mincore_fail:
9925 unlock_user(a, arg1, 0);
9926 }
9927 break;
bellardffa65c32004-01-04 23:57:22 +00009928#endif
aurel32408321b2008-10-01 21:46:32 +00009929#ifdef TARGET_NR_arm_fadvise64_64
9930 case TARGET_NR_arm_fadvise64_64:
9931 {
9932 /*
9933 * arm_fadvise64_64 looks like fadvise64_64 but
9934 * with different argument order
9935 */
9936 abi_long temp;
9937 temp = arg3;
9938 arg3 = arg4;
9939 arg4 = temp;
9940 }
9941#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02009942#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00009943#ifdef TARGET_NR_fadvise64_64
9944 case TARGET_NR_fadvise64_64:
9945#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02009946#ifdef TARGET_NR_fadvise64
9947 case TARGET_NR_fadvise64:
9948#endif
9949#ifdef TARGET_S390X
9950 switch (arg4) {
9951 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
9952 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
9953 case 6: arg4 = POSIX_FADV_DONTNEED; break;
9954 case 7: arg4 = POSIX_FADV_NOREUSE; break;
9955 default: break;
9956 }
9957#endif
9958 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00009959 break;
9960#endif
bellardffa65c32004-01-04 23:57:22 +00009961#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00009962 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00009963 /* A straight passthrough may not be safe because qemu sometimes
Lei Lid2d6b852013-05-20 17:20:50 +08009964 turns private file-backed mappings into anonymous mappings.
pbrook24836682006-04-16 14:14:53 +00009965 This will break MADV_DONTNEED.
9966 This is a hint, so ignoring and returning success is ok. */
9967 ret = get_errno(0);
9968 break;
bellardffa65c32004-01-04 23:57:22 +00009969#endif
blueswir1992f48a2007-10-14 16:27:31 +00009970#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00009971 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00009972 {
thsb1e341e2007-03-20 21:50:52 +00009973 int cmd;
bellard77e46722003-04-29 20:39:06 +00009974 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00009975 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00009976#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00009977 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00009978#endif
bellard77e46722003-04-29 20:39:06 +00009979
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02009980 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00009981 if (cmd == -TARGET_EINVAL) {
9982 ret = cmd;
9983 break;
9984 }
thsb1e341e2007-03-20 21:50:52 +00009985
bellard60cd49d2003-03-16 22:53:56 +00009986 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00009987 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00009988#ifdef TARGET_ARM
9989 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00009990 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
9991 goto efault;
ths58134272007-03-31 18:59:32 +00009992 fl.l_type = tswap16(target_efl->l_type);
9993 fl.l_whence = tswap16(target_efl->l_whence);
9994 fl.l_start = tswap64(target_efl->l_start);
9995 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02009996 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00009997 unlock_user_struct(target_efl, arg3, 0);
9998 } else
9999#endif
10000 {
bellard9ee1fa22007-11-11 15:11:19 +000010001 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
10002 goto efault;
ths58134272007-03-31 18:59:32 +000010003 fl.l_type = tswap16(target_fl->l_type);
10004 fl.l_whence = tswap16(target_fl->l_whence);
10005 fl.l_start = tswap64(target_fl->l_start);
10006 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +020010007 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +000010008 unlock_user_struct(target_fl, arg3, 0);
10009 }
thsb1e341e2007-03-20 21:50:52 +000010010 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +000010011 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +000010012#ifdef TARGET_ARM
10013 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +000010014 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
10015 goto efault;
pbrookce4defa2006-02-09 16:49:55 +000010016 target_efl->l_type = tswap16(fl.l_type);
10017 target_efl->l_whence = tswap16(fl.l_whence);
10018 target_efl->l_start = tswap64(fl.l_start);
10019 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +020010020 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +000010021 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +000010022 } else
10023#endif
10024 {
bellard9ee1fa22007-11-11 15:11:19 +000010025 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
10026 goto efault;
pbrookce4defa2006-02-09 16:49:55 +000010027 target_fl->l_type = tswap16(fl.l_type);
10028 target_fl->l_whence = tswap16(fl.l_whence);
10029 target_fl->l_start = tswap64(fl.l_start);
10030 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +020010031 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +000010032 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +000010033 }
bellard77e46722003-04-29 20:39:06 +000010034 }
10035 break;
10036
thsb1e341e2007-03-20 21:50:52 +000010037 case TARGET_F_SETLK64:
10038 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +000010039#ifdef TARGET_ARM
10040 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +000010041 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
10042 goto efault;
pbrookce4defa2006-02-09 16:49:55 +000010043 fl.l_type = tswap16(target_efl->l_type);
10044 fl.l_whence = tswap16(target_efl->l_whence);
10045 fl.l_start = tswap64(target_efl->l_start);
10046 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +020010047 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +000010048 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +000010049 } else
10050#endif
10051 {
bellard9ee1fa22007-11-11 15:11:19 +000010052 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
10053 goto efault;
pbrookce4defa2006-02-09 16:49:55 +000010054 fl.l_type = tswap16(target_fl->l_type);
10055 fl.l_whence = tswap16(target_fl->l_whence);
10056 fl.l_start = tswap64(target_fl->l_start);
10057 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +020010058 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +000010059 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +000010060 }
thsb1e341e2007-03-20 21:50:52 +000010061 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +000010062 break;
bellard60cd49d2003-03-16 22:53:56 +000010063 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +020010064 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +000010065 break;
10066 }
bellard77e46722003-04-29 20:39:06 +000010067 break;
10068 }
bellard60cd49d2003-03-16 22:53:56 +000010069#endif
ths7d600c82006-12-08 01:32:58 +000010070#ifdef TARGET_NR_cacheflush
10071 case TARGET_NR_cacheflush:
10072 /* self-modifying code is handled automatically, so nothing needed */
10073 ret = 0;
10074 break;
10075#endif
bellardebc05482003-09-30 21:08:41 +000010076#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +000010077 case TARGET_NR_security:
10078 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +000010079#endif
bellardc573ff62004-01-04 15:51:36 +000010080#ifdef TARGET_NR_getpagesize
10081 case TARGET_NR_getpagesize:
10082 ret = TARGET_PAGE_SIZE;
10083 break;
10084#endif
bellard31e31b82003-02-18 22:55:36 +000010085 case TARGET_NR_gettid:
10086 ret = get_errno(gettid());
10087 break;
thse5febef2007-04-01 18:31:35 +000010088#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +000010089 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +000010090#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +030010091 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +000010092 arg2 = arg3;
10093 arg3 = arg4;
10094 arg4 = arg5;
10095 }
aurel322054ac92008-10-13 21:08:07 +000010096 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
10097#else
10098 ret = get_errno(readahead(arg1, arg2, arg3));
10099#endif
10100 break;
thse5febef2007-04-01 18:31:35 +000010101#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010102#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +000010103#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +000010104 case TARGET_NR_listxattr:
10105 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +000010106 {
10107 void *p, *b = 0;
10108 if (arg2) {
10109 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10110 if (!b) {
10111 ret = -TARGET_EFAULT;
10112 break;
10113 }
10114 }
10115 p = lock_user_string(arg1);
10116 if (p) {
10117 if (num == TARGET_NR_listxattr) {
10118 ret = get_errno(listxattr(p, b, arg3));
10119 } else {
10120 ret = get_errno(llistxattr(p, b, arg3));
10121 }
10122 } else {
10123 ret = -TARGET_EFAULT;
10124 }
10125 unlock_user(p, arg1, 0);
10126 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +030010127 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +000010128 }
10129 case TARGET_NR_flistxattr:
10130 {
10131 void *b = 0;
10132 if (arg2) {
10133 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10134 if (!b) {
10135 ret = -TARGET_EFAULT;
10136 break;
10137 }
10138 }
10139 ret = get_errno(flistxattr(arg1, b, arg3));
10140 unlock_user(b, arg2, arg3);
10141 break;
10142 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010143 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +000010144 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010145 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +000010146 void *p, *n, *v = 0;
10147 if (arg3) {
10148 v = lock_user(VERIFY_READ, arg3, arg4, 1);
10149 if (!v) {
10150 ret = -TARGET_EFAULT;
10151 break;
10152 }
10153 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010154 p = lock_user_string(arg1);
10155 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +000010156 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000010157 if (num == TARGET_NR_setxattr) {
10158 ret = get_errno(setxattr(p, n, v, arg4, arg5));
10159 } else {
10160 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
10161 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010162 } else {
10163 ret = -TARGET_EFAULT;
10164 }
10165 unlock_user(p, arg1, 0);
10166 unlock_user(n, arg2, 0);
10167 unlock_user(v, arg3, 0);
10168 }
10169 break;
Peter Maydell30297b52011-12-14 15:37:18 +000010170 case TARGET_NR_fsetxattr:
10171 {
10172 void *n, *v = 0;
10173 if (arg3) {
10174 v = lock_user(VERIFY_READ, arg3, arg4, 1);
10175 if (!v) {
10176 ret = -TARGET_EFAULT;
10177 break;
10178 }
10179 }
10180 n = lock_user_string(arg2);
10181 if (n) {
10182 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
10183 } else {
10184 ret = -TARGET_EFAULT;
10185 }
10186 unlock_user(n, arg2, 0);
10187 unlock_user(v, arg3, 0);
10188 }
10189 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010190 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +000010191 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010192 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +000010193 void *p, *n, *v = 0;
10194 if (arg3) {
10195 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
10196 if (!v) {
10197 ret = -TARGET_EFAULT;
10198 break;
10199 }
10200 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010201 p = lock_user_string(arg1);
10202 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +000010203 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000010204 if (num == TARGET_NR_getxattr) {
10205 ret = get_errno(getxattr(p, n, v, arg4));
10206 } else {
10207 ret = get_errno(lgetxattr(p, n, v, arg4));
10208 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010209 } else {
10210 ret = -TARGET_EFAULT;
10211 }
10212 unlock_user(p, arg1, 0);
10213 unlock_user(n, arg2, 0);
10214 unlock_user(v, arg3, arg4);
10215 }
10216 break;
Peter Maydell30297b52011-12-14 15:37:18 +000010217 case TARGET_NR_fgetxattr:
10218 {
10219 void *n, *v = 0;
10220 if (arg3) {
10221 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
10222 if (!v) {
10223 ret = -TARGET_EFAULT;
10224 break;
10225 }
10226 }
10227 n = lock_user_string(arg2);
10228 if (n) {
10229 ret = get_errno(fgetxattr(arg1, n, v, arg4));
10230 } else {
10231 ret = -TARGET_EFAULT;
10232 }
10233 unlock_user(n, arg2, 0);
10234 unlock_user(v, arg3, arg4);
10235 }
10236 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010237 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +000010238 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010239 {
10240 void *p, *n;
10241 p = lock_user_string(arg1);
10242 n = lock_user_string(arg2);
10243 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +000010244 if (num == TARGET_NR_removexattr) {
10245 ret = get_errno(removexattr(p, n));
10246 } else {
10247 ret = get_errno(lremovexattr(p, n));
10248 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010249 } else {
10250 ret = -TARGET_EFAULT;
10251 }
10252 unlock_user(p, arg1, 0);
10253 unlock_user(n, arg2, 0);
10254 }
10255 break;
Peter Maydell30297b52011-12-14 15:37:18 +000010256 case TARGET_NR_fremovexattr:
10257 {
10258 void *n;
10259 n = lock_user_string(arg2);
10260 if (n) {
10261 ret = get_errno(fremovexattr(arg1, n));
10262 } else {
10263 ret = -TARGET_EFAULT;
10264 }
10265 unlock_user(n, arg2, 0);
10266 }
10267 break;
bellardebc05482003-09-30 21:08:41 +000010268#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070010269#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +000010270#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +000010271 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +000010272#if defined(TARGET_MIPS)
Petar Jovanovicd2792792014-06-18 17:48:20 +020010273 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
ths6f5b89a2007-03-02 20:48:00 +000010274 ret = 0;
10275 break;
edgar_iglef967792009-01-07 14:19:38 +000010276#elif defined(TARGET_CRIS)
10277 if (arg1 & 0xff)
10278 ret = -TARGET_EINVAL;
10279 else {
10280 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
10281 ret = 0;
10282 }
10283 break;
bellard8d18e892007-11-14 15:18:40 +000010284#elif defined(TARGET_I386) && defined(TARGET_ABI32)
10285 ret = do_set_thread_area(cpu_env, arg1);
10286 break;
Peter Maydell1ccd9372013-07-16 18:44:55 +010010287#elif defined(TARGET_M68K)
10288 {
Andreas Färber0429a972013-08-26 18:14:44 +020010289 TaskState *ts = cpu->opaque;
Peter Maydell1ccd9372013-07-16 18:44:55 +010010290 ts->tp_value = arg1;
Peter Maydell95c1eb12013-07-29 11:31:49 +010010291 ret = 0;
Peter Maydell1ccd9372013-07-16 18:44:55 +010010292 break;
10293 }
ths6f5b89a2007-03-02 20:48:00 +000010294#else
10295 goto unimplemented_nowarn;
10296#endif
10297#endif
10298#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +000010299 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +000010300#if defined(TARGET_I386) && defined(TARGET_ABI32)
10301 ret = do_get_thread_area(cpu_env, arg1);
Peter Maydelld312bbe2013-07-16 18:44:56 +010010302 break;
Peter Maydell1ccd9372013-07-16 18:44:55 +010010303#elif defined(TARGET_M68K)
10304 {
Andreas Färber0429a972013-08-26 18:14:44 +020010305 TaskState *ts = cpu->opaque;
Peter Maydell1ccd9372013-07-16 18:44:55 +010010306 ret = ts->tp_value;
10307 break;
10308 }
bellard8d18e892007-11-14 15:18:40 +000010309#else
bellard5cd43932003-03-29 16:54:36 +000010310 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +000010311#endif
bellard8d18e892007-11-14 15:18:40 +000010312#endif
bellard48dc41e2006-06-21 18:15:50 +000010313#ifdef TARGET_NR_getdomainname
10314 case TARGET_NR_getdomainname:
10315 goto unimplemented_nowarn;
10316#endif
ths6f5b89a2007-03-02 20:48:00 +000010317
thsb5906f92007-03-19 13:32:45 +000010318#ifdef TARGET_NR_clock_gettime
10319 case TARGET_NR_clock_gettime:
10320 {
10321 struct timespec ts;
10322 ret = get_errno(clock_gettime(arg1, &ts));
10323 if (!is_error(ret)) {
10324 host_to_target_timespec(arg2, &ts);
10325 }
10326 break;
10327 }
10328#endif
10329#ifdef TARGET_NR_clock_getres
10330 case TARGET_NR_clock_getres:
10331 {
10332 struct timespec ts;
10333 ret = get_errno(clock_getres(arg1, &ts));
10334 if (!is_error(ret)) {
10335 host_to_target_timespec(arg2, &ts);
10336 }
10337 break;
10338 }
10339#endif
pbrook63d76512008-05-29 13:43:29 +000010340#ifdef TARGET_NR_clock_nanosleep
10341 case TARGET_NR_clock_nanosleep:
10342 {
10343 struct timespec ts;
10344 target_to_host_timespec(&ts, arg3);
10345 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
10346 if (arg4)
10347 host_to_target_timespec(arg4, &ts);
Tom Musta8fbe8fd2014-08-12 13:53:41 -050010348
10349#if defined(TARGET_PPC)
10350 /* clock_nanosleep is odd in that it returns positive errno values.
10351 * On PPC, CR0 bit 3 should be set in such a situation. */
10352 if (ret) {
10353 ((CPUPPCState *)cpu_env)->crf[0] |= 1;
10354 }
10355#endif
pbrook63d76512008-05-29 13:43:29 +000010356 break;
10357 }
10358#endif
thsb5906f92007-03-19 13:32:45 +000010359
ths6f5b89a2007-03-02 20:48:00 +000010360#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
10361 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +000010362 ret = get_errno(set_tid_address((int *)g2h(arg1)));
10363 break;
ths6f5b89a2007-03-02 20:48:00 +000010364#endif
10365
ths3ae43202007-09-16 21:39:48 +000010366#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +000010367 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +000010368 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +000010369 break;
10370#endif
10371
ths3ae43202007-09-16 21:39:48 +000010372#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +000010373 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +000010374 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
10375 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +000010376 break;
10377#endif
10378
ths4f2b1fe2007-06-21 21:57:12 +000010379#ifdef TARGET_NR_set_robust_list
10380 case TARGET_NR_set_robust_list:
Peter Maydelle9a970a2013-02-08 04:34:54 +000010381 case TARGET_NR_get_robust_list:
10382 /* The ABI for supporting robust futexes has userspace pass
10383 * the kernel a pointer to a linked list which is updated by
10384 * userspace after the syscall; the list is walked by the kernel
10385 * when the thread exits. Since the linked list in QEMU guest
10386 * memory isn't a valid linked list for the host and we have
10387 * no way to reliably intercept the thread-death event, we can't
10388 * support these. Silently return ENOSYS so that guest userspace
10389 * falls back to a non-robust futex implementation (which should
10390 * be OK except in the corner case of the guest crashing while
10391 * holding a mutex that is shared with another process via
10392 * shared memory).
10393 */
10394 goto unimplemented_nowarn;
ths4f2b1fe2007-06-21 21:57:12 +000010395#endif
10396
Peter Maydell1acae9f2013-07-02 14:04:12 +010010397#if defined(TARGET_NR_utimensat)
ths9007f0e2007-09-25 17:50:37 +000010398 case TARGET_NR_utimensat:
10399 {
Riku Voipioebc996f2009-04-21 15:01:51 +030010400 struct timespec *tsp, ts[2];
10401 if (!arg3) {
10402 tsp = NULL;
10403 } else {
10404 target_to_host_timespec(ts, arg3);
10405 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
10406 tsp = ts;
10407 }
ths9007f0e2007-09-25 17:50:37 +000010408 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +030010409 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +000010410 else {
bellard579a97f2007-11-11 14:26:47 +000010411 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +000010412 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +000010413 goto fail;
10414 }
Riku Voipioebc996f2009-04-21 15:01:51 +030010415 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +000010416 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +000010417 }
10418 }
10419 break;
10420#endif
pbrookbd0c5662008-05-29 14:34:11 +000010421 case TARGET_NR_futex:
10422 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
10423 break;
aurel32dbfe4c32009-04-08 21:29:30 +000010424#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +000010425 case TARGET_NR_inotify_init:
10426 ret = get_errno(sys_inotify_init());
10427 break;
10428#endif
Stefan Weila1606b02010-03-28 11:44:41 +020010429#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +000010430#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
10431 case TARGET_NR_inotify_init1:
10432 ret = get_errno(sys_inotify_init1(arg1));
10433 break;
10434#endif
Stefan Weila1606b02010-03-28 11:44:41 +020010435#endif
aurel32dbfe4c32009-04-08 21:29:30 +000010436#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +000010437 case TARGET_NR_inotify_add_watch:
10438 p = lock_user_string(arg2);
10439 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
10440 unlock_user(p, arg2, 0);
10441 break;
10442#endif
aurel32dbfe4c32009-04-08 21:29:30 +000010443#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +000010444 case TARGET_NR_inotify_rm_watch:
10445 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
10446 break;
10447#endif
ths9007f0e2007-09-25 17:50:37 +000010448
Nathan Froyd8ec9cf82009-07-22 09:14:36 -070010449#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +000010450 case TARGET_NR_mq_open:
10451 {
Tom Mustab6ce1f62014-08-12 13:53:36 -050010452 struct mq_attr posix_mq_attr, *attrp;
aurel3224e10032009-04-15 16:11:43 +000010453
10454 p = lock_user_string(arg1 - 1);
Tom Mustab6ce1f62014-08-12 13:53:36 -050010455 if (arg4 != 0) {
aurel3224e10032009-04-15 16:11:43 +000010456 copy_from_user_mq_attr (&posix_mq_attr, arg4);
Tom Mustab6ce1f62014-08-12 13:53:36 -050010457 attrp = &posix_mq_attr;
10458 } else {
10459 attrp = 0;
10460 }
10461 ret = get_errno(mq_open(p, arg2, arg3, attrp));
aurel3224e10032009-04-15 16:11:43 +000010462 unlock_user (p, arg1, 0);
10463 }
10464 break;
10465
10466 case TARGET_NR_mq_unlink:
10467 p = lock_user_string(arg1 - 1);
10468 ret = get_errno(mq_unlink(p));
10469 unlock_user (p, arg1, 0);
10470 break;
10471
10472 case TARGET_NR_mq_timedsend:
10473 {
10474 struct timespec ts;
10475
10476 p = lock_user (VERIFY_READ, arg2, arg3, 1);
10477 if (arg5 != 0) {
10478 target_to_host_timespec(&ts, arg5);
10479 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
10480 host_to_target_timespec(arg5, &ts);
10481 }
10482 else
10483 ret = get_errno(mq_send(arg1, p, arg3, arg4));
10484 unlock_user (p, arg2, arg3);
10485 }
10486 break;
10487
10488 case TARGET_NR_mq_timedreceive:
10489 {
10490 struct timespec ts;
10491 unsigned int prio;
10492
10493 p = lock_user (VERIFY_READ, arg2, arg3, 1);
10494 if (arg5 != 0) {
10495 target_to_host_timespec(&ts, arg5);
10496 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
10497 host_to_target_timespec(arg5, &ts);
10498 }
10499 else
10500 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
10501 unlock_user (p, arg2, arg3);
10502 if (arg4 != 0)
10503 put_user_u32(prio, arg4);
10504 }
10505 break;
10506
10507 /* Not implemented for now... */
10508/* case TARGET_NR_mq_notify: */
10509/* break; */
10510
10511 case TARGET_NR_mq_getsetattr:
10512 {
10513 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
10514 ret = 0;
10515 if (arg3 != 0) {
10516 ret = mq_getattr(arg1, &posix_mq_attr_out);
10517 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
10518 }
10519 if (arg2 != 0) {
10520 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
10521 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
10522 }
10523
10524 }
10525 break;
10526#endif
10527
vibisreenivasan3ce34df2009-05-16 18:32:41 +053010528#ifdef CONFIG_SPLICE
10529#ifdef TARGET_NR_tee
10530 case TARGET_NR_tee:
10531 {
10532 ret = get_errno(tee(arg1,arg2,arg3,arg4));
10533 }
10534 break;
10535#endif
10536#ifdef TARGET_NR_splice
10537 case TARGET_NR_splice:
10538 {
10539 loff_t loff_in, loff_out;
10540 loff_t *ploff_in = NULL, *ploff_out = NULL;
Andreas Schwab17644b32015-03-10 17:11:35 +010010541 if (arg2) {
10542 if (get_user_u64(loff_in, arg2)) {
10543 goto efault;
10544 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053010545 ploff_in = &loff_in;
10546 }
Andreas Schwab17644b32015-03-10 17:11:35 +010010547 if (arg4) {
10548 if (get_user_u64(loff_out, arg4)) {
10549 goto efault;
10550 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053010551 ploff_out = &loff_out;
10552 }
10553 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
Andreas Schwab17644b32015-03-10 17:11:35 +010010554 if (arg2) {
10555 if (put_user_u64(loff_in, arg2)) {
10556 goto efault;
10557 }
10558 }
10559 if (arg4) {
10560 if (put_user_u64(loff_out, arg4)) {
10561 goto efault;
10562 }
10563 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053010564 }
10565 break;
10566#endif
10567#ifdef TARGET_NR_vmsplice
10568 case TARGET_NR_vmsplice:
10569 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -070010570 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
10571 if (vec != NULL) {
10572 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
10573 unlock_iovec(vec, arg2, arg3, 0);
10574 } else {
10575 ret = -host_to_target_errno(errno);
10576 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +053010577 }
10578 break;
10579#endif
10580#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +030010581#ifdef CONFIG_EVENTFD
10582#if defined(TARGET_NR_eventfd)
10583 case TARGET_NR_eventfd:
10584 ret = get_errno(eventfd(arg1, 0));
Laurent Viviere36800c2015-10-02 14:48:09 +020010585 fd_trans_unregister(ret);
Riku Voipioc2882b92009-08-12 15:08:24 +030010586 break;
10587#endif
10588#if defined(TARGET_NR_eventfd2)
10589 case TARGET_NR_eventfd2:
Petar Jovanovic5947c692013-04-08 20:26:10 +020010590 {
10591 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
10592 if (arg2 & TARGET_O_NONBLOCK) {
10593 host_flags |= O_NONBLOCK;
10594 }
10595 if (arg2 & TARGET_O_CLOEXEC) {
10596 host_flags |= O_CLOEXEC;
10597 }
10598 ret = get_errno(eventfd(arg1, host_flags));
Laurent Viviere36800c2015-10-02 14:48:09 +020010599 fd_trans_unregister(ret);
Riku Voipioc2882b92009-08-12 15:08:24 +030010600 break;
Petar Jovanovic5947c692013-04-08 20:26:10 +020010601 }
Riku Voipioc2882b92009-08-12 15:08:24 +030010602#endif
10603#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +030010604#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
10605 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +010010606#if TARGET_ABI_BITS == 32
10607 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
10608 target_offset64(arg5, arg6)));
10609#else
Ulrich Hechtd0927932009-09-17 20:22:14 +030010610 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +010010611#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +030010612 break;
10613#endif
Peter Maydellc727f472011-01-06 11:05:10 +000010614#if defined(CONFIG_SYNC_FILE_RANGE)
10615#if defined(TARGET_NR_sync_file_range)
10616 case TARGET_NR_sync_file_range:
10617#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +030010618#if defined(TARGET_MIPS)
10619 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
10620 target_offset64(arg5, arg6), arg7));
10621#else
Peter Maydellc727f472011-01-06 11:05:10 +000010622 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
10623 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +030010624#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +000010625#else
10626 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
10627#endif
10628 break;
10629#endif
10630#if defined(TARGET_NR_sync_file_range2)
10631 case TARGET_NR_sync_file_range2:
10632 /* This is like sync_file_range but the arguments are reordered */
10633#if TARGET_ABI_BITS == 32
10634 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
10635 target_offset64(arg5, arg6), arg2));
10636#else
10637 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
10638#endif
10639 break;
10640#endif
10641#endif
Laurent Viviere36800c2015-10-02 14:48:09 +020010642#if defined(TARGET_NR_signalfd4)
10643 case TARGET_NR_signalfd4:
10644 ret = do_signalfd4(arg1, arg2, arg4);
10645 break;
10646#endif
10647#if defined(TARGET_NR_signalfd)
10648 case TARGET_NR_signalfd:
10649 ret = do_signalfd4(arg1, arg2, 0);
10650 break;
10651#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000010652#if defined(CONFIG_EPOLL)
10653#if defined(TARGET_NR_epoll_create)
10654 case TARGET_NR_epoll_create:
10655 ret = get_errno(epoll_create(arg1));
10656 break;
10657#endif
10658#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
10659 case TARGET_NR_epoll_create1:
10660 ret = get_errno(epoll_create1(arg1));
10661 break;
10662#endif
10663#if defined(TARGET_NR_epoll_ctl)
10664 case TARGET_NR_epoll_ctl:
10665 {
10666 struct epoll_event ep;
10667 struct epoll_event *epp = 0;
10668 if (arg4) {
10669 struct target_epoll_event *target_ep;
10670 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
10671 goto efault;
10672 }
10673 ep.events = tswap32(target_ep->events);
10674 /* The epoll_data_t union is just opaque data to the kernel,
10675 * so we transfer all 64 bits across and need not worry what
10676 * actual data type it is.
10677 */
10678 ep.data.u64 = tswap64(target_ep->data.u64);
10679 unlock_user_struct(target_ep, arg4, 0);
10680 epp = &ep;
10681 }
10682 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
10683 break;
10684 }
10685#endif
10686
10687#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
10688#define IMPLEMENT_EPOLL_PWAIT
10689#endif
10690#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
10691#if defined(TARGET_NR_epoll_wait)
10692 case TARGET_NR_epoll_wait:
10693#endif
10694#if defined(IMPLEMENT_EPOLL_PWAIT)
10695 case TARGET_NR_epoll_pwait:
10696#endif
10697 {
10698 struct target_epoll_event *target_ep;
10699 struct epoll_event *ep;
10700 int epfd = arg1;
10701 int maxevents = arg3;
10702 int timeout = arg4;
10703
10704 target_ep = lock_user(VERIFY_WRITE, arg2,
10705 maxevents * sizeof(struct target_epoll_event), 1);
10706 if (!target_ep) {
10707 goto efault;
10708 }
10709
10710 ep = alloca(maxevents * sizeof(struct epoll_event));
10711
10712 switch (num) {
10713#if defined(IMPLEMENT_EPOLL_PWAIT)
10714 case TARGET_NR_epoll_pwait:
10715 {
10716 target_sigset_t *target_set;
10717 sigset_t _set, *set = &_set;
10718
10719 if (arg5) {
10720 target_set = lock_user(VERIFY_READ, arg5,
10721 sizeof(target_sigset_t), 1);
10722 if (!target_set) {
10723 unlock_user(target_ep, arg2, 0);
10724 goto efault;
10725 }
10726 target_to_host_sigset(set, target_set);
10727 unlock_user(target_set, arg5, 0);
10728 } else {
10729 set = NULL;
10730 }
10731
10732 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
10733 break;
10734 }
10735#endif
10736#if defined(TARGET_NR_epoll_wait)
10737 case TARGET_NR_epoll_wait:
10738 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
10739 break;
10740#endif
10741 default:
10742 ret = -TARGET_ENOSYS;
10743 }
10744 if (!is_error(ret)) {
10745 int i;
10746 for (i = 0; i < ret; i++) {
10747 target_ep[i].events = tswap32(ep[i].events);
10748 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
10749 }
10750 }
10751 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
10752 break;
10753 }
10754#endif
10755#endif
Peter Maydell163a05a2011-06-27 17:44:52 +010010756#ifdef TARGET_NR_prlimit64
10757 case TARGET_NR_prlimit64:
10758 {
10759 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
10760 struct target_rlimit64 *target_rnew, *target_rold;
10761 struct host_rlimit64 rnew, rold, *rnewp = 0;
Felix Janda95018012014-12-02 22:11:17 +010010762 int resource = target_to_host_resource(arg2);
Peter Maydell163a05a2011-06-27 17:44:52 +010010763 if (arg3) {
10764 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
10765 goto efault;
10766 }
10767 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
10768 rnew.rlim_max = tswap64(target_rnew->rlim_max);
10769 unlock_user_struct(target_rnew, arg3, 0);
10770 rnewp = &rnew;
10771 }
10772
Felix Janda95018012014-12-02 22:11:17 +010010773 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
Peter Maydell163a05a2011-06-27 17:44:52 +010010774 if (!is_error(ret) && arg4) {
10775 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
10776 goto efault;
10777 }
10778 target_rold->rlim_cur = tswap64(rold.rlim_cur);
10779 target_rold->rlim_max = tswap64(rold.rlim_max);
10780 unlock_user_struct(target_rold, arg4, 1);
10781 }
10782 break;
10783 }
10784#endif
Richard Henderson3d21d292012-09-15 13:20:46 -070010785#ifdef TARGET_NR_gethostname
10786 case TARGET_NR_gethostname:
10787 {
10788 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
10789 if (name) {
10790 ret = get_errno(gethostname(name, arg2));
10791 unlock_user(name, arg1, arg2);
10792 } else {
10793 ret = -TARGET_EFAULT;
10794 }
10795 break;
10796 }
10797#endif
Riku Voipio89aaf1a2013-07-24 09:44:26 +030010798#ifdef TARGET_NR_atomic_cmpxchg_32
10799 case TARGET_NR_atomic_cmpxchg_32:
10800 {
10801 /* should use start_exclusive from main.c */
10802 abi_ulong mem_value;
10803 if (get_user_u32(mem_value, arg6)) {
10804 target_siginfo_t info;
10805 info.si_signo = SIGSEGV;
10806 info.si_errno = 0;
10807 info.si_code = TARGET_SEGV_MAPERR;
10808 info._sifields._sigfault._addr = arg6;
10809 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
10810 ret = 0xdeadbeef;
10811
10812 }
10813 if (mem_value == arg2)
10814 put_user_u32(arg1, arg6);
10815 ret = mem_value;
10816 break;
10817 }
10818#endif
10819#ifdef TARGET_NR_atomic_barrier
10820 case TARGET_NR_atomic_barrier:
10821 {
10822 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
Peter Maydell3b899ea2014-03-12 13:06:01 +000010823 ret = 0;
Riku Voipio89aaf1a2013-07-24 09:44:26 +030010824 break;
10825 }
10826#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010827
10828#ifdef TARGET_NR_timer_create
10829 case TARGET_NR_timer_create:
10830 {
10831 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
10832
10833 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010834
10835 int clkid = arg1;
10836 int timer_index = next_free_host_timer();
10837
10838 if (timer_index < 0) {
10839 ret = -TARGET_EAGAIN;
10840 } else {
10841 timer_t *phtimer = g_posix_timers + timer_index;
10842
10843 if (arg2) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010844 phost_sevp = &host_sevp;
Peter Maydellc0659762014-08-09 15:42:32 +010010845 ret = target_to_host_sigevent(phost_sevp, arg2);
10846 if (ret != 0) {
10847 break;
10848 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010849 }
10850
10851 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
10852 if (ret) {
10853 phtimer = NULL;
10854 } else {
Alexander Grafaecc8862014-11-10 21:33:03 +010010855 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010856 goto efault;
10857 }
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010858 }
10859 }
10860 break;
10861 }
10862#endif
10863
10864#ifdef TARGET_NR_timer_settime
10865 case TARGET_NR_timer_settime:
10866 {
10867 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
10868 * struct itimerspec * old_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010010869 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010870
Alexander Grafaecc8862014-11-10 21:33:03 +010010871 if (timerid < 0) {
10872 ret = timerid;
10873 } else if (arg3 == 0) {
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010874 ret = -TARGET_EINVAL;
10875 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010876 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010877 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
10878
10879 target_to_host_itimerspec(&hspec_new, arg3);
10880 ret = get_errno(
10881 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
10882 host_to_target_itimerspec(arg2, &hspec_old);
10883 }
10884 break;
10885 }
10886#endif
10887
10888#ifdef TARGET_NR_timer_gettime
10889 case TARGET_NR_timer_gettime:
10890 {
10891 /* args: timer_t timerid, struct itimerspec *curr_value */
Alexander Grafaecc8862014-11-10 21:33:03 +010010892 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010893
Alexander Grafaecc8862014-11-10 21:33:03 +010010894 if (timerid < 0) {
10895 ret = timerid;
10896 } else if (!arg2) {
10897 ret = -TARGET_EFAULT;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010898 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010899 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010900 struct itimerspec hspec;
10901 ret = get_errno(timer_gettime(htimer, &hspec));
10902
10903 if (host_to_target_itimerspec(arg2, &hspec)) {
10904 ret = -TARGET_EFAULT;
10905 }
10906 }
10907 break;
10908 }
10909#endif
10910
10911#ifdef TARGET_NR_timer_getoverrun
10912 case TARGET_NR_timer_getoverrun:
10913 {
10914 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010010915 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010916
Alexander Grafaecc8862014-11-10 21:33:03 +010010917 if (timerid < 0) {
10918 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010919 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010920 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010921 ret = get_errno(timer_getoverrun(htimer));
10922 }
Laurent Viviere36800c2015-10-02 14:48:09 +020010923 fd_trans_unregister(ret);
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010924 break;
10925 }
10926#endif
10927
10928#ifdef TARGET_NR_timer_delete
10929 case TARGET_NR_timer_delete:
10930 {
10931 /* args: timer_t timerid */
Alexander Grafaecc8862014-11-10 21:33:03 +010010932 target_timer_t timerid = get_timer_id(arg1);
Alexander Grafe52a99f2014-08-22 13:56:18 +020010933
Alexander Grafaecc8862014-11-10 21:33:03 +010010934 if (timerid < 0) {
10935 ret = timerid;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010936 } else {
Alexander Grafe52a99f2014-08-22 13:56:18 +020010937 timer_t htimer = g_posix_timers[timerid];
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010938 ret = get_errno(timer_delete(htimer));
Alexander Grafe52a99f2014-08-22 13:56:18 +020010939 g_posix_timers[timerid] = 0;
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +110010940 }
10941 break;
10942 }
10943#endif
10944
Riku Voipio51834342014-06-22 11:25:42 +010010945#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
10946 case TARGET_NR_timerfd_create:
10947 ret = get_errno(timerfd_create(arg1,
10948 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
10949 break;
10950#endif
10951
10952#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
10953 case TARGET_NR_timerfd_gettime:
10954 {
10955 struct itimerspec its_curr;
10956
10957 ret = get_errno(timerfd_gettime(arg1, &its_curr));
10958
10959 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
10960 goto efault;
10961 }
10962 }
10963 break;
10964#endif
10965
10966#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
10967 case TARGET_NR_timerfd_settime:
10968 {
10969 struct itimerspec its_new, its_old, *p_new;
10970
10971 if (arg3) {
10972 if (target_to_host_itimerspec(&its_new, arg3)) {
10973 goto efault;
10974 }
10975 p_new = &its_new;
10976 } else {
10977 p_new = NULL;
10978 }
10979
10980 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
10981
10982 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
10983 goto efault;
10984 }
10985 }
10986 break;
10987#endif
10988
Paul Burtonab31cda2014-06-22 11:25:43 +010010989#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
10990 case TARGET_NR_ioprio_get:
10991 ret = get_errno(ioprio_get(arg1, arg2));
10992 break;
10993#endif
10994
10995#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
10996 case TARGET_NR_ioprio_set:
10997 ret = get_errno(ioprio_set(arg1, arg2, arg3));
10998 break;
10999#endif
11000
Riku Voipio9af5c902014-08-12 15:58:57 +030011001#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
11002 case TARGET_NR_setns:
11003 ret = get_errno(setns(arg1, arg2));
11004 break;
11005#endif
11006#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
11007 case TARGET_NR_unshare:
11008 ret = get_errno(unshare(arg1));
11009 break;
11010#endif
11011
bellard31e31b82003-02-18 22:55:36 +000011012 default:
11013 unimplemented:
bellard5cd43932003-03-29 16:54:36 +000011014 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +000011015#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 +000011016 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +000011017#endif
ths0da46a62007-10-20 20:23:07 +000011018 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +000011019 break;
11020 }
bellard579a97f2007-11-11 14:26:47 +000011021fail:
bellardc573ff62004-01-04 15:51:36 +000011022#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +000011023 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +000011024#endif
thsb92c47c2007-11-01 00:07:38 +000011025 if(do_strace)
11026 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +000011027 return ret;
bellard579a97f2007-11-11 14:26:47 +000011028efault:
11029 ret = -TARGET_EFAULT;
11030 goto fail;
bellard31e31b82003-02-18 22:55:36 +000011031}