blob: 1f64867ebabaf7691352c791ce2ba0f326faded2 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
2 * Linux syscalls
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard31e31b82003-02-18 22:55:36 +000018 */
Eduardo Habkostd5b3a9b2009-06-09 18:26:31 -030019#define _ATFILE_SOURCE
bellard31e31b82003-02-18 22:55:36 +000020#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
bellard04369ff2003-03-20 22:33:23 +000023#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000024#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
bellard7854b052003-03-29 17:22:23 +000029#include <time.h>
pbrook82e671d2008-06-09 12:10:22 +000030#include <limits.h>
John Spencerc56dc772012-12-10 07:59:46 +010031#include <grp.h>
bellard31e31b82003-02-18 22:55:36 +000032#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000033#include <sys/ipc.h>
34#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000035#include <sys/wait.h>
36#include <sys/time.h>
37#include <sys/stat.h>
38#include <sys/mount.h>
John Spencer586b0be2012-12-26 00:49:49 +010039#include <sys/file.h>
40#include <sys/fsuid.h>
41#include <sys/personality.h>
ths39b9aae2007-02-11 18:36:44 +000042#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000043#include <sys/resource.h>
44#include <sys/mman.h>
45#include <sys/swap.h>
46#include <signal.h>
47#include <sched.h>
Aurelien Jarno60e99242010-03-29 02:12:51 +020048#ifdef __ia64__
49int __clone2(int (*fn)(void *), void *child_stack_base,
50 size_t stack_size, int flags, void *arg, ...);
51#endif
bellard31e31b82003-02-18 22:55:36 +000052#include <sys/socket.h>
aurel32607175e2009-04-15 16:11:59 +000053#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000054#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000055#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000056#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000057#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000058#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000059#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000060#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000061#include <sys/sysinfo.h>
aurel323b3f24a2009-04-15 16:12:13 +000062#include <sys/utsname.h>
bellard72f03902003-02-18 23:33:18 +000063//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000064#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000065#include <netinet/tcp.h>
Laurent Vivier86fcd942011-03-30 01:35:23 +020066#include <linux/wireless.h>
Jing Huang920394d2012-07-24 13:59:23 +000067#include <linux/icmp.h>
Stefan Weil5a61cb62011-09-08 17:55:32 +020068#include "qemu-common.h"
Juan Quintela9788c9c2009-07-27 16:13:02 +020069#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +000070#include <sys/gmon.h>
71#endif
Riku Voipioc2882b92009-08-12 15:08:24 +030072#ifdef CONFIG_EVENTFD
73#include <sys/eventfd.h>
74#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +000075#ifdef CONFIG_EPOLL
76#include <sys/epoll.h>
77#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -070078#ifdef CONFIG_ATTR
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010079#include "qemu/xattr.h"
An-Cheng Huanga790ae32011-08-09 12:34:06 -070080#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +000081#ifdef CONFIG_SENDFILE
82#include <sys/sendfile.h>
83#endif
bellard31e31b82003-02-18 22:55:36 +000084
85#define termios host_termios
86#define winsize host_winsize
87#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000088#define sgttyb host_sgttyb /* same as target */
89#define tchars host_tchars /* same as target */
90#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000091
92#include <linux/termios.h>
93#include <linux/unistd.h>
94#include <linux/utsname.h>
95#include <linux/cdrom.h>
96#include <linux/hdreg.h>
97#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000098#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000099#include <linux/mtio.h>
Martin Mohring350d1772009-05-04 21:21:41 +0300100#include <linux/fs.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000101#if defined(CONFIG_FIEMAP)
Peter Maydell285da2b2011-01-06 15:04:18 +0000102#include <linux/fiemap.h>
Peter Maydelldace20d2011-01-10 13:11:24 +0000103#endif
Ulrich Hechtf7680a52009-10-16 17:00:44 +0200104#include <linux/fb.h>
105#include <linux/vt.h>
Alexander Graf56e904e2012-01-31 18:42:06 +0100106#include <linux/dm-ioctl.h>
Laurent Vivierc07ecc62013-01-07 11:40:06 +0000107#include <linux/reboot.h>
Laurent Vivier7ff7b662013-07-02 14:04:12 +0100108#include <linux/route.h>
Laurent Vivierf57d4192013-08-30 01:46:41 +0200109#include <linux/filter.h>
Andreas Färberfff8c532014-01-18 07:38:30 +0100110#include <linux/blkpg.h>
pbrookd7e40362008-05-23 16:06:43 +0000111#include "linux_loop.h"
Loïc Minierda790302009-12-29 22:06:13 +0100112#include "cpu-uname.h"
bellard31e31b82003-02-18 22:55:36 +0000113
bellard3ef693a2003-03-23 20:17:16 +0000114#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000115
pbrookd865bab2008-06-07 22:12:17 +0000116#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
117 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
pbrook30813ce2008-06-02 15:45:44 +0000118
bellard72f03902003-02-18 23:33:18 +0000119//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000120
bellard1a9353d2003-03-16 20:28:50 +0000121//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000122#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
123#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000124
bellard70a194b2003-08-11 22:20:16 +0000125
bellard70a194b2003-08-11 22:20:16 +0000126#undef _syscall0
127#undef _syscall1
128#undef _syscall2
129#undef _syscall3
130#undef _syscall4
131#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000132#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000133
bellard83fcb512006-06-14 13:37:16 +0000134#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000135static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000136{ \
137 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000138}
139
bellard83fcb512006-06-14 13:37:16 +0000140#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000141static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000142{ \
143 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000144}
145
bellard83fcb512006-06-14 13:37:16 +0000146#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000147static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000148{ \
149 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000150}
151
bellard83fcb512006-06-14 13:37:16 +0000152#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000153static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000154{ \
155 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000156}
157
bellard83fcb512006-06-14 13:37:16 +0000158#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000159static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000160{ \
161 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000162}
163
bellard83fcb512006-06-14 13:37:16 +0000164#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
165 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000166static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000167{ \
168 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000169}
bellard83fcb512006-06-14 13:37:16 +0000170
171
172#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
173 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000174static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
175 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000176{ \
177 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
178}
179
bellard70a194b2003-08-11 22:20:16 +0000180
bellard31e31b82003-02-18 22:55:36 +0000181#define __NR_sys_uname __NR_uname
bellard72f03902003-02-18 23:33:18 +0000182#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000183#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000184#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000185#define __NR_sys_getpriority __NR_getpriority
bellard66fb9762003-03-23 01:06:05 +0000186#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
ths7494b0f2007-02-11 18:26:53 +0000187#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000188#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000189#define __NR_sys_tkill __NR_tkill
pbrookbd0c5662008-05-29 14:34:11 +0000190#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000191#define __NR_sys_inotify_init __NR_inotify_init
192#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
193#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000194
Alexander Graf42a39fb2011-04-15 17:32:45 +0200195#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
196 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000197#define __NR__llseek __NR_lseek
198#endif
199
bellard72f03902003-02-18 23:33:18 +0000200#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000201_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000202#else
ths0da46a62007-10-20 20:23:07 +0000203/* This is a replacement for the host gettid() and must return a host
204 errno. */
bellard72f03902003-02-18 23:33:18 +0000205static int gettid(void) {
206 return -ENOSYS;
207}
208#endif
Peter Maydell3307e232013-06-12 16:20:21 +0100209#ifdef __NR_getdents
aurel323b3f24a2009-04-15 16:12:13 +0000210_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
Peter Maydell3307e232013-06-12 16:20:21 +0100211#endif
212#if !defined(__NR_getdents) || \
213 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
aurel323b3f24a2009-04-15 16:12:13 +0000214_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
215#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700216#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000217_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
218 loff_t *, res, uint, wh);
219#endif
220_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
221_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
222#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
223_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
224#endif
225#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
226_syscall2(int,sys_tkill,int,tid,int,sig)
227#endif
228#ifdef __NR_exit_group
229_syscall1(int,exit_group,int,error_code)
230#endif
231#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
232_syscall1(int,set_tid_address,int *,tidptr)
233#endif
aurel323b3f24a2009-04-15 16:12:13 +0000234#if defined(TARGET_NR_futex) && defined(__NR_futex)
235_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
236 const struct timespec *,timeout,int *,uaddr2,int,val3)
237#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500238#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
239_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
240 unsigned long *, user_mask_ptr);
241#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
242_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
243 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200244_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
245 void *, arg);
aurel323b3f24a2009-04-15 16:12:13 +0000246
247static bitmask_transtbl fcntl_flags_tbl[] = {
248 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
249 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
250 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
251 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
252 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
253 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
254 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
255 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700256 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000257 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
258 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
259 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
260 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000261#if defined(O_DIRECT)
262 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
263#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700264#if defined(O_NOATIME)
265 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
266#endif
267#if defined(O_CLOEXEC)
268 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
269#endif
270#if defined(O_PATH)
271 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
272#endif
273 /* Don't terminate the list prematurely on 64-bit host+guest. */
274#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
275 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
276#endif
aurel323b3f24a2009-04-15 16:12:13 +0000277 { 0, 0, 0, 0 }
278};
279
280#define COPY_UTSNAME_FIELD(dest, src) \
281 do { \
282 /* __NEW_UTS_LEN doesn't include terminating null */ \
283 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
284 (dest)[__NEW_UTS_LEN] = '\0'; \
285 } while (0)
286
287static int sys_uname(struct new_utsname *buf)
288{
289 struct utsname uts_buf;
290
291 if (uname(&uts_buf) < 0)
292 return (-1);
293
294 /*
295 * Just in case these have some differences, we
296 * translate utsname to new_utsname (which is the
297 * struct linux kernel uses).
298 */
299
Stefan Weil67bd9ed2011-05-07 22:32:25 +0200300 memset(buf, 0, sizeof(*buf));
aurel323b3f24a2009-04-15 16:12:13 +0000301 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
302 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
303 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
304 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
305 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
306#ifdef _GNU_SOURCE
307 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
308#endif
309 return (0);
310
311#undef COPY_UTSNAME_FIELD
312}
313
314static int sys_getcwd1(char *buf, size_t size)
315{
316 if (getcwd(buf, size) == NULL) {
317 /* getcwd() sets errno */
318 return (-1);
319 }
aurel32aaf4ad32009-04-16 14:17:14 +0000320 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000321}
322
aurel323b3f24a2009-04-15 16:12:13 +0000323#ifdef TARGET_NR_openat
Alexander Graff4c69012011-09-25 06:25:35 +0200324static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000325{
326 /*
327 * open(2) has extra parameter 'mode' when called with
328 * flag O_CREAT.
329 */
330 if ((flags & O_CREAT) != 0) {
aurel323b3f24a2009-04-15 16:12:13 +0000331 return (openat(dirfd, pathname, flags, mode));
332 }
333 return (openat(dirfd, pathname, flags));
334}
335#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300336
Peter Maydell1acae9f2013-07-02 14:04:12 +0100337#ifdef TARGET_NR_utimensat
Riku Voipioebc996f2009-04-21 15:01:51 +0300338#ifdef CONFIG_UTIMENSAT
339static int sys_utimensat(int dirfd, const char *pathname,
340 const struct timespec times[2], int flags)
341{
342 if (pathname == NULL)
343 return futimens(dirfd, times);
344 else
345 return utimensat(dirfd, pathname, times, flags);
346}
Peter Maydell1acae9f2013-07-02 14:04:12 +0100347#elif defined(__NR_utimensat)
348#define __NR_sys_utimensat __NR_utimensat
ths9007f0e2007-09-25 17:50:37 +0000349_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
350 const struct timespec *,tsp,int,flags)
Peter Maydell1acae9f2013-07-02 14:04:12 +0100351#else
352static int sys_utimensat(int dirfd, const char *pathname,
353 const struct timespec times[2], int flags)
354{
355 errno = ENOSYS;
356 return -1;
357}
ths9007f0e2007-09-25 17:50:37 +0000358#endif
Peter Maydell1acae9f2013-07-02 14:04:12 +0100359#endif /* TARGET_NR_utimensat */
aurel323b3f24a2009-04-15 16:12:13 +0000360
361#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000362#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000363
aurel3239b59762008-10-01 21:46:50 +0000364#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000365static int sys_inotify_init(void)
366{
367 return (inotify_init());
368}
aurel3239b59762008-10-01 21:46:50 +0000369#endif
370#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000371static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
372{
373 return (inotify_add_watch(fd, pathname, mask));
374}
aurel3239b59762008-10-01 21:46:50 +0000375#endif
376#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000377static int sys_inotify_rm_watch(int fd, int32_t wd)
378{
aurel328690e422009-04-17 13:50:32 +0000379 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000380}
aurel3239b59762008-10-01 21:46:50 +0000381#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000382#ifdef CONFIG_INOTIFY1
383#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
384static int sys_inotify_init1(int flags)
385{
386 return (inotify_init1(flags));
387}
388#endif
389#endif
aurel323b3f24a2009-04-15 16:12:13 +0000390#else
391/* Userspace can usually survive runtime without inotify */
392#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000393#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000394#undef TARGET_NR_inotify_add_watch
395#undef TARGET_NR_inotify_rm_watch
396#endif /* CONFIG_INOTIFY */
397
Mike Frysingerd8035d42011-02-07 01:05:51 -0500398#if defined(TARGET_NR_ppoll)
399#ifndef __NR_ppoll
400# define __NR_ppoll -1
401#endif
402#define __NR_sys_ppoll __NR_ppoll
403_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
404 struct timespec *, timeout, const __sigset_t *, sigmask,
405 size_t, sigsetsize)
406#endif
bellard66fb9762003-03-23 01:06:05 +0000407
Mike Frysinger055e0902011-06-03 17:01:49 -0400408#if defined(TARGET_NR_pselect6)
409#ifndef __NR_pselect6
410# define __NR_pselect6 -1
411#endif
412#define __NR_sys_pselect6 __NR_pselect6
413_syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
414 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
415#endif
416
Peter Maydell163a05a2011-06-27 17:44:52 +0100417#if defined(TARGET_NR_prlimit64)
418#ifndef __NR_prlimit64
419# define __NR_prlimit64 -1
420#endif
421#define __NR_sys_prlimit64 __NR_prlimit64
422/* The glibc rlimit structure may not be that used by the underlying syscall */
423struct host_rlimit64 {
424 uint64_t rlim_cur;
425 uint64_t rlim_max;
426};
427_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
428 const struct host_rlimit64 *, new_limit,
429 struct host_rlimit64 *, old_limit)
430#endif
431
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +1100432
433#if defined(TARGET_NR_timer_create)
434/* Maxiumum of 32 active POSIX timers allowed at any one time. */
435static timer_t g_posix_timers[32] = { 0, } ;
436
437static inline int next_free_host_timer(void)
438{
439 int k ;
440 /* FIXME: Does finding the next free slot require a lock? */
441 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
442 if (g_posix_timers[k] == 0) {
443 g_posix_timers[k] = (timer_t) 1;
444 return k;
445 }
446 }
447 return -1;
448}
449#endif
450
Riku Voipio48e515d2011-07-12 15:40:51 +0300451/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
Alexander Graf4a1def42012-09-29 15:32:38 +0000452#ifdef TARGET_ARM
Riku Voipio48e515d2011-07-12 15:40:51 +0300453static inline int regpairs_aligned(void *cpu_env) {
454 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
455}
456#elif defined(TARGET_MIPS)
457static inline int regpairs_aligned(void *cpu_env) { return 1; }
Alexander Graf4a1def42012-09-29 15:32:38 +0000458#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
459/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
460 * of registers which translates to the same as ARM/MIPS, because we start with
461 * r3 as arg1 */
462static inline int regpairs_aligned(void *cpu_env) { return 1; }
Riku Voipio48e515d2011-07-12 15:40:51 +0300463#else
464static inline int regpairs_aligned(void *cpu_env) { return 0; }
465#endif
466
thsb92c47c2007-11-01 00:07:38 +0000467#define ERRNO_TABLE_SIZE 1200
468
469/* target_to_host_errno_table[] is initialized from
470 * host_to_target_errno_table[] in syscall_init(). */
471static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
472};
473
ths637947f2007-06-01 12:09:19 +0000474/*
thsfe8f0962007-07-12 10:59:21 +0000475 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000476 * minus the errnos that are not actually generic to all archs.
477 */
thsb92c47c2007-11-01 00:07:38 +0000478static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000479 [EIDRM] = TARGET_EIDRM,
480 [ECHRNG] = TARGET_ECHRNG,
481 [EL2NSYNC] = TARGET_EL2NSYNC,
482 [EL3HLT] = TARGET_EL3HLT,
483 [EL3RST] = TARGET_EL3RST,
484 [ELNRNG] = TARGET_ELNRNG,
485 [EUNATCH] = TARGET_EUNATCH,
486 [ENOCSI] = TARGET_ENOCSI,
487 [EL2HLT] = TARGET_EL2HLT,
488 [EDEADLK] = TARGET_EDEADLK,
489 [ENOLCK] = TARGET_ENOLCK,
490 [EBADE] = TARGET_EBADE,
491 [EBADR] = TARGET_EBADR,
492 [EXFULL] = TARGET_EXFULL,
493 [ENOANO] = TARGET_ENOANO,
494 [EBADRQC] = TARGET_EBADRQC,
495 [EBADSLT] = TARGET_EBADSLT,
496 [EBFONT] = TARGET_EBFONT,
497 [ENOSTR] = TARGET_ENOSTR,
498 [ENODATA] = TARGET_ENODATA,
499 [ETIME] = TARGET_ETIME,
500 [ENOSR] = TARGET_ENOSR,
501 [ENONET] = TARGET_ENONET,
502 [ENOPKG] = TARGET_ENOPKG,
503 [EREMOTE] = TARGET_EREMOTE,
504 [ENOLINK] = TARGET_ENOLINK,
505 [EADV] = TARGET_EADV,
506 [ESRMNT] = TARGET_ESRMNT,
507 [ECOMM] = TARGET_ECOMM,
508 [EPROTO] = TARGET_EPROTO,
509 [EDOTDOT] = TARGET_EDOTDOT,
510 [EMULTIHOP] = TARGET_EMULTIHOP,
511 [EBADMSG] = TARGET_EBADMSG,
512 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
513 [EOVERFLOW] = TARGET_EOVERFLOW,
514 [ENOTUNIQ] = TARGET_ENOTUNIQ,
515 [EBADFD] = TARGET_EBADFD,
516 [EREMCHG] = TARGET_EREMCHG,
517 [ELIBACC] = TARGET_ELIBACC,
518 [ELIBBAD] = TARGET_ELIBBAD,
519 [ELIBSCN] = TARGET_ELIBSCN,
520 [ELIBMAX] = TARGET_ELIBMAX,
521 [ELIBEXEC] = TARGET_ELIBEXEC,
522 [EILSEQ] = TARGET_EILSEQ,
523 [ENOSYS] = TARGET_ENOSYS,
524 [ELOOP] = TARGET_ELOOP,
525 [ERESTART] = TARGET_ERESTART,
526 [ESTRPIPE] = TARGET_ESTRPIPE,
527 [ENOTEMPTY] = TARGET_ENOTEMPTY,
528 [EUSERS] = TARGET_EUSERS,
529 [ENOTSOCK] = TARGET_ENOTSOCK,
530 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
531 [EMSGSIZE] = TARGET_EMSGSIZE,
532 [EPROTOTYPE] = TARGET_EPROTOTYPE,
533 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
534 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
535 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
536 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
537 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
538 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
539 [EADDRINUSE] = TARGET_EADDRINUSE,
540 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
541 [ENETDOWN] = TARGET_ENETDOWN,
542 [ENETUNREACH] = TARGET_ENETUNREACH,
543 [ENETRESET] = TARGET_ENETRESET,
544 [ECONNABORTED] = TARGET_ECONNABORTED,
545 [ECONNRESET] = TARGET_ECONNRESET,
546 [ENOBUFS] = TARGET_ENOBUFS,
547 [EISCONN] = TARGET_EISCONN,
548 [ENOTCONN] = TARGET_ENOTCONN,
549 [EUCLEAN] = TARGET_EUCLEAN,
550 [ENOTNAM] = TARGET_ENOTNAM,
551 [ENAVAIL] = TARGET_ENAVAIL,
552 [EISNAM] = TARGET_EISNAM,
553 [EREMOTEIO] = TARGET_EREMOTEIO,
554 [ESHUTDOWN] = TARGET_ESHUTDOWN,
555 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
556 [ETIMEDOUT] = TARGET_ETIMEDOUT,
557 [ECONNREFUSED] = TARGET_ECONNREFUSED,
558 [EHOSTDOWN] = TARGET_EHOSTDOWN,
559 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
560 [EALREADY] = TARGET_EALREADY,
561 [EINPROGRESS] = TARGET_EINPROGRESS,
562 [ESTALE] = TARGET_ESTALE,
563 [ECANCELED] = TARGET_ECANCELED,
564 [ENOMEDIUM] = TARGET_ENOMEDIUM,
565 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000566#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000567 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000568#endif
569#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000570 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000571#endif
572#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000573 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000574#endif
575#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000576 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000577#endif
578#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000579 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000580#endif
581#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000582 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000583#endif
thsb92c47c2007-11-01 00:07:38 +0000584};
ths637947f2007-06-01 12:09:19 +0000585
586static inline int host_to_target_errno(int err)
587{
588 if(host_to_target_errno_table[err])
589 return host_to_target_errno_table[err];
590 return err;
591}
592
thsb92c47c2007-11-01 00:07:38 +0000593static inline int target_to_host_errno(int err)
594{
595 if (target_to_host_errno_table[err])
596 return target_to_host_errno_table[err];
597 return err;
598}
599
blueswir1992f48a2007-10-14 16:27:31 +0000600static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000601{
602 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000603 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000604 else
605 return ret;
606}
607
blueswir1992f48a2007-10-14 16:27:31 +0000608static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000609{
blueswir1992f48a2007-10-14 16:27:31 +0000610 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000611}
612
thsb92c47c2007-11-01 00:07:38 +0000613char *target_strerror(int err)
614{
Alexander Graf962b2892011-11-21 12:04:07 +0100615 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
616 return NULL;
617 }
thsb92c47c2007-11-01 00:07:38 +0000618 return strerror(target_to_host_errno(err));
619}
620
blueswir1992f48a2007-10-14 16:27:31 +0000621static abi_ulong target_brk;
622static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000623static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000624
blueswir1992f48a2007-10-14 16:27:31 +0000625void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000626{
blueswir14c1de732007-07-07 20:45:44 +0000627 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000628 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000629}
630
vincent4d1de872011-06-14 21:56:33 +0000631//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
632#define DEBUGF_BRK(message, args...)
633
ths0da46a62007-10-20 20:23:07 +0000634/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000635abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000636{
blueswir1992f48a2007-10-14 16:27:31 +0000637 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000638 int new_alloc_size;
639
Paul Brook3a0c6c42012-02-09 19:04:27 +0000640 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000641
vincent4d1de872011-06-14 21:56:33 +0000642 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000643 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000644 return target_brk;
645 }
646 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000647 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
648 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000649 return target_brk;
650 }
bellard31e31b82003-02-18 22:55:36 +0000651
vincent4d1de872011-06-14 21:56:33 +0000652 /* If the new brk is less than the highest page reserved to the
653 * target heap allocation, set it and we're almost done... */
654 if (new_brk <= brk_page) {
655 /* Heap contents are initialized to zero, as for anonymous
656 * mapped pages. */
657 if (new_brk > target_brk) {
658 memset(g2h(target_brk), 0, new_brk - target_brk);
659 }
bellard31e31b82003-02-18 22:55:36 +0000660 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000661 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000662 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000663 }
664
Peter Maydell00faf082011-04-18 16:34:24 +0100665 /* We need to allocate more memory after the brk... Note that
666 * we don't use MAP_FIXED because that will map over the top of
667 * any existing mapping (like the one with the host libc or qemu
668 * itself); instead we treat "mapped but at wrong address" as
669 * a failure and unmap again.
670 */
vincent4d1de872011-06-14 21:56:33 +0000671 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000672 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000673 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100674 MAP_ANON|MAP_PRIVATE, 0, 0));
675
676 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200677 /* Heap contents are initialized to zero, as for anonymous
678 * mapped pages. Technically the new pages are already
679 * initialized to zero since they *are* anonymous mapped
680 * pages, however we have to take care with the contents that
681 * come from the remaining part of the previous page: it may
682 * contains garbage data due to a previous heap usage (grown
683 * then shrunken). */
684 memset(g2h(target_brk), 0, brk_page - target_brk);
685
Peter Maydell00faf082011-04-18 16:34:24 +0100686 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000687 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000688 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
689 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100690 return target_brk;
691 } else if (mapped_addr != -1) {
692 /* Mapped but at wrong address, meaning there wasn't actually
693 * enough space for this brk.
694 */
695 target_munmap(mapped_addr, new_alloc_size);
696 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000697 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000698 }
699 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000700 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100701 }
balrog7ab240a2008-04-26 12:17:34 +0000702
Richard Henderson7dd46c02010-05-03 10:07:49 -0700703#if defined(TARGET_ALPHA)
704 /* We (partially) emulate OSF/1 on Alpha, which requires we
705 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100706 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700707#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100708 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000709 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000710}
711
ths26edcf42007-12-09 02:25:24 +0000712static inline abi_long copy_from_user_fdset(fd_set *fds,
713 abi_ulong target_fds_addr,
714 int n)
bellard31e31b82003-02-18 22:55:36 +0000715{
ths26edcf42007-12-09 02:25:24 +0000716 int i, nw, j, k;
717 abi_ulong b, *target_fds;
718
719 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
720 if (!(target_fds = lock_user(VERIFY_READ,
721 target_fds_addr,
722 sizeof(abi_ulong) * nw,
723 1)))
724 return -TARGET_EFAULT;
725
726 FD_ZERO(fds);
727 k = 0;
728 for (i = 0; i < nw; i++) {
729 /* grab the abi_ulong */
730 __get_user(b, &target_fds[i]);
731 for (j = 0; j < TARGET_ABI_BITS; j++) {
732 /* check the bit inside the abi_ulong */
733 if ((b >> j) & 1)
734 FD_SET(k, fds);
735 k++;
bellard31e31b82003-02-18 22:55:36 +0000736 }
bellard31e31b82003-02-18 22:55:36 +0000737 }
ths26edcf42007-12-09 02:25:24 +0000738
739 unlock_user(target_fds, target_fds_addr, 0);
740
741 return 0;
bellard31e31b82003-02-18 22:55:36 +0000742}
743
Mike Frysinger055e0902011-06-03 17:01:49 -0400744static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
745 abi_ulong target_fds_addr,
746 int n)
747{
748 if (target_fds_addr) {
749 if (copy_from_user_fdset(fds, target_fds_addr, n))
750 return -TARGET_EFAULT;
751 *fds_ptr = fds;
752 } else {
753 *fds_ptr = NULL;
754 }
755 return 0;
756}
757
ths26edcf42007-12-09 02:25:24 +0000758static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
759 const fd_set *fds,
760 int n)
bellard31e31b82003-02-18 22:55:36 +0000761{
bellard31e31b82003-02-18 22:55:36 +0000762 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000763 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000764 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000765
ths26edcf42007-12-09 02:25:24 +0000766 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
767 if (!(target_fds = lock_user(VERIFY_WRITE,
768 target_fds_addr,
769 sizeof(abi_ulong) * nw,
770 0)))
771 return -TARGET_EFAULT;
772
773 k = 0;
774 for (i = 0; i < nw; i++) {
775 v = 0;
776 for (j = 0; j < TARGET_ABI_BITS; j++) {
Andreas Schwab9ab709b2013-04-09 01:02:34 +0000777 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
ths26edcf42007-12-09 02:25:24 +0000778 k++;
bellard31e31b82003-02-18 22:55:36 +0000779 }
ths26edcf42007-12-09 02:25:24 +0000780 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000781 }
ths26edcf42007-12-09 02:25:24 +0000782
783 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
784
785 return 0;
bellard31e31b82003-02-18 22:55:36 +0000786}
787
bellardc596ed12003-07-13 17:32:31 +0000788#if defined(__alpha__)
789#define HOST_HZ 1024
790#else
791#define HOST_HZ 100
792#endif
793
blueswir1992f48a2007-10-14 16:27:31 +0000794static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000795{
796#if HOST_HZ == TARGET_HZ
797 return ticks;
798#else
799 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
800#endif
801}
802
bellard579a97f2007-11-11 14:26:47 +0000803static inline abi_long host_to_target_rusage(abi_ulong target_addr,
804 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000805{
pbrook53a59602006-03-25 19:31:22 +0000806 struct target_rusage *target_rusage;
807
bellard579a97f2007-11-11 14:26:47 +0000808 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
809 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200810 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
811 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
812 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
813 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
814 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
815 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
816 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
817 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
818 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
819 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
820 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
821 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
822 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
823 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
824 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
825 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
826 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
827 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000828 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000829
830 return 0;
bellardb4091862003-05-16 15:39:34 +0000831}
832
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200833static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900834{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200835 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300836 rlim_t result;
837
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200838 target_rlim_swap = tswapal(target_rlim);
839 if (target_rlim_swap == TARGET_RLIM_INFINITY)
840 return RLIM_INFINITY;
841
842 result = target_rlim_swap;
843 if (target_rlim_swap != (rlim_t)result)
844 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300845
846 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900847}
848
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200849static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900850{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200851 abi_ulong target_rlim_swap;
852 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300853
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200854 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300855 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900856 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300857 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200858 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300859
860 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900861}
862
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +0300863static inline int target_to_host_resource(int code)
864{
865 switch (code) {
866 case TARGET_RLIMIT_AS:
867 return RLIMIT_AS;
868 case TARGET_RLIMIT_CORE:
869 return RLIMIT_CORE;
870 case TARGET_RLIMIT_CPU:
871 return RLIMIT_CPU;
872 case TARGET_RLIMIT_DATA:
873 return RLIMIT_DATA;
874 case TARGET_RLIMIT_FSIZE:
875 return RLIMIT_FSIZE;
876 case TARGET_RLIMIT_LOCKS:
877 return RLIMIT_LOCKS;
878 case TARGET_RLIMIT_MEMLOCK:
879 return RLIMIT_MEMLOCK;
880 case TARGET_RLIMIT_MSGQUEUE:
881 return RLIMIT_MSGQUEUE;
882 case TARGET_RLIMIT_NICE:
883 return RLIMIT_NICE;
884 case TARGET_RLIMIT_NOFILE:
885 return RLIMIT_NOFILE;
886 case TARGET_RLIMIT_NPROC:
887 return RLIMIT_NPROC;
888 case TARGET_RLIMIT_RSS:
889 return RLIMIT_RSS;
890 case TARGET_RLIMIT_RTPRIO:
891 return RLIMIT_RTPRIO;
892 case TARGET_RLIMIT_SIGPENDING:
893 return RLIMIT_SIGPENDING;
894 case TARGET_RLIMIT_STACK:
895 return RLIMIT_STACK;
896 default:
897 return code;
898 }
899}
900
ths788f5ec2007-12-09 02:37:05 +0000901static inline abi_long copy_from_user_timeval(struct timeval *tv,
902 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000903{
pbrook53a59602006-03-25 19:31:22 +0000904 struct target_timeval *target_tv;
905
ths788f5ec2007-12-09 02:37:05 +0000906 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000907 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000908
909 __get_user(tv->tv_sec, &target_tv->tv_sec);
910 __get_user(tv->tv_usec, &target_tv->tv_usec);
911
912 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000913
914 return 0;
bellard31e31b82003-02-18 22:55:36 +0000915}
916
ths788f5ec2007-12-09 02:37:05 +0000917static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
918 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000919{
pbrook53a59602006-03-25 19:31:22 +0000920 struct target_timeval *target_tv;
921
ths788f5ec2007-12-09 02:37:05 +0000922 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000923 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000924
925 __put_user(tv->tv_sec, &target_tv->tv_sec);
926 __put_user(tv->tv_usec, &target_tv->tv_usec);
927
928 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000929
930 return 0;
bellard31e31b82003-02-18 22:55:36 +0000931}
932
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700933#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
934#include <mqueue.h>
935
aurel3224e10032009-04-15 16:11:43 +0000936static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
937 abi_ulong target_mq_attr_addr)
938{
939 struct target_mq_attr *target_mq_attr;
940
941 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
942 target_mq_attr_addr, 1))
943 return -TARGET_EFAULT;
944
945 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
946 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
947 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
948 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
949
950 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
951
952 return 0;
953}
954
955static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
956 const struct mq_attr *attr)
957{
958 struct target_mq_attr *target_mq_attr;
959
960 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
961 target_mq_attr_addr, 0))
962 return -TARGET_EFAULT;
963
964 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
965 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
966 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
967 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
968
969 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
970
971 return 0;
972}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700973#endif
bellard31e31b82003-02-18 22:55:36 +0000974
Mike Frysinger055e0902011-06-03 17:01:49 -0400975#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +0000976/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000977static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000978 abi_ulong rfd_addr, abi_ulong wfd_addr,
979 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000980{
981 fd_set rfds, wfds, efds;
982 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
983 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000984 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000985
Mike Frysinger055e0902011-06-03 17:01:49 -0400986 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
987 if (ret) {
988 return ret;
pbrook53a59602006-03-25 19:31:22 +0000989 }
Mike Frysinger055e0902011-06-03 17:01:49 -0400990 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
991 if (ret) {
992 return ret;
pbrook53a59602006-03-25 19:31:22 +0000993 }
Mike Frysinger055e0902011-06-03 17:01:49 -0400994 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
995 if (ret) {
996 return ret;
pbrook53a59602006-03-25 19:31:22 +0000997 }
ths3b46e622007-09-17 08:09:54 +0000998
ths26edcf42007-12-09 02:25:24 +0000999 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +00001000 if (copy_from_user_timeval(&tv, target_tv_addr))
1001 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001002 tv_ptr = &tv;
1003 } else {
1004 tv_ptr = NULL;
1005 }
ths26edcf42007-12-09 02:25:24 +00001006
bellard31e31b82003-02-18 22:55:36 +00001007 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +00001008
ths26edcf42007-12-09 02:25:24 +00001009 if (!is_error(ret)) {
1010 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1011 return -TARGET_EFAULT;
1012 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1013 return -TARGET_EFAULT;
1014 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1015 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001016
ths788f5ec2007-12-09 02:37:05 +00001017 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1018 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +00001019 }
bellard579a97f2007-11-11 14:26:47 +00001020
bellard31e31b82003-02-18 22:55:36 +00001021 return ret;
1022}
Mike Frysinger055e0902011-06-03 17:01:49 -04001023#endif
bellard31e31b82003-02-18 22:55:36 +00001024
Riku Voipio099d6b02009-05-05 12:10:04 +03001025static abi_long do_pipe2(int host_pipe[], int flags)
1026{
1027#ifdef CONFIG_PIPE2
1028 return pipe2(host_pipe, flags);
1029#else
1030 return -ENOSYS;
1031#endif
1032}
1033
Richard Hendersonfb41a662010-05-03 10:07:52 -07001034static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1035 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001036{
1037 int host_pipe[2];
1038 abi_long ret;
1039 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1040
1041 if (is_error(ret))
1042 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001043
1044 /* Several targets have special calling conventions for the original
1045 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1046 if (!is_pipe2) {
1047#if defined(TARGET_ALPHA)
1048 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1049 return host_pipe[0];
1050#elif defined(TARGET_MIPS)
1051 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1052 return host_pipe[0];
1053#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001054 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001055 return host_pipe[0];
Peter Maydell82f05b62013-07-06 17:39:48 +01001056#elif defined(TARGET_SPARC)
1057 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1058 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001059#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001060 }
1061
Riku Voipio099d6b02009-05-05 12:10:04 +03001062 if (put_user_s32(host_pipe[0], pipedes)
1063 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1064 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001065 return get_errno(ret);
1066}
1067
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001068static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1069 abi_ulong target_addr,
1070 socklen_t len)
1071{
1072 struct target_ip_mreqn *target_smreqn;
1073
1074 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1075 if (!target_smreqn)
1076 return -TARGET_EFAULT;
1077 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1078 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1079 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001080 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001081 unlock_user(target_smreqn, target_addr, 0);
1082
1083 return 0;
1084}
1085
bellard579a97f2007-11-11 14:26:47 +00001086static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1087 abi_ulong target_addr,
1088 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001089{
aurel32607175e2009-04-15 16:11:59 +00001090 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1091 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001092 struct target_sockaddr *target_saddr;
1093
bellard579a97f2007-11-11 14:26:47 +00001094 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1095 if (!target_saddr)
1096 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001097
1098 sa_family = tswap16(target_saddr->sa_family);
1099
1100 /* Oops. The caller might send a incomplete sun_path; sun_path
1101 * must be terminated by \0 (see the manual page), but
1102 * unfortunately it is quite common to specify sockaddr_un
1103 * length as "strlen(x->sun_path)" while it should be
1104 * "strlen(...) + 1". We'll fix that here if needed.
1105 * Linux kernel has a similar feature.
1106 */
1107
1108 if (sa_family == AF_UNIX) {
1109 if (len < unix_maxlen && len > 0) {
1110 char *cp = (char*)target_saddr;
1111
1112 if ( cp[len-1] && !cp[len] )
1113 len++;
1114 }
1115 if (len > unix_maxlen)
1116 len = unix_maxlen;
1117 }
1118
pbrook53a59602006-03-25 19:31:22 +00001119 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001120 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001121 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001122
1123 return 0;
bellard7854b052003-03-29 17:22:23 +00001124}
1125
bellard579a97f2007-11-11 14:26:47 +00001126static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1127 struct sockaddr *addr,
1128 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001129{
pbrook53a59602006-03-25 19:31:22 +00001130 struct target_sockaddr *target_saddr;
1131
bellard579a97f2007-11-11 14:26:47 +00001132 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1133 if (!target_saddr)
1134 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001135 memcpy(target_saddr, addr, len);
1136 target_saddr->sa_family = tswap16(addr->sa_family);
1137 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001138
1139 return 0;
bellard7854b052003-03-29 17:22:23 +00001140}
1141
bellard5a4a8982007-11-11 17:39:18 +00001142static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1143 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001144{
1145 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001146 abi_long msg_controllen;
1147 abi_ulong target_cmsg_addr;
1148 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001149 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001150
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001151 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001152 if (msg_controllen < sizeof (struct target_cmsghdr))
1153 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001154 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001155 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1156 if (!target_cmsg)
1157 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001158
1159 while (cmsg && target_cmsg) {
1160 void *data = CMSG_DATA(cmsg);
1161 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1162
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001163 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001164 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1165
1166 space += CMSG_SPACE(len);
1167 if (space > msgh->msg_controllen) {
1168 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001169 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001170 break;
1171 }
1172
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001173 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1174 cmsg->cmsg_level = SOL_SOCKET;
1175 } else {
1176 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1177 }
bellard7854b052003-03-29 17:22:23 +00001178 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1179 cmsg->cmsg_len = CMSG_LEN(len);
1180
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001181 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001182 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1183 memcpy(data, target_data, len);
1184 } else {
1185 int *fd = (int *)data;
1186 int *target_fd = (int *)target_data;
1187 int i, numfds = len / sizeof(int);
1188
1189 for (i = 0; i < numfds; i++)
1190 fd[i] = tswap32(target_fd[i]);
1191 }
1192
1193 cmsg = CMSG_NXTHDR(msgh, cmsg);
1194 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1195 }
bellard5a4a8982007-11-11 17:39:18 +00001196 unlock_user(target_cmsg, target_cmsg_addr, 0);
1197 the_end:
bellard7854b052003-03-29 17:22:23 +00001198 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001199 return 0;
bellard7854b052003-03-29 17:22:23 +00001200}
1201
bellard5a4a8982007-11-11 17:39:18 +00001202static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1203 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001204{
1205 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001206 abi_long msg_controllen;
1207 abi_ulong target_cmsg_addr;
1208 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001209 socklen_t space = 0;
1210
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001211 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001212 if (msg_controllen < sizeof (struct target_cmsghdr))
1213 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001214 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001215 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1216 if (!target_cmsg)
1217 return -TARGET_EFAULT;
1218
bellard7854b052003-03-29 17:22:23 +00001219 while (cmsg && target_cmsg) {
1220 void *data = CMSG_DATA(cmsg);
1221 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1222
1223 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1224
1225 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001226 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001227 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001228 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001229 break;
1230 }
1231
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001232 if (cmsg->cmsg_level == SOL_SOCKET) {
1233 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1234 } else {
1235 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1236 }
bellard7854b052003-03-29 17:22:23 +00001237 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001238 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
bellard7854b052003-03-29 17:22:23 +00001239
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001240 if ((cmsg->cmsg_level == SOL_SOCKET) &&
Jing Huangaebf5bc2012-07-24 14:01:42 +00001241 (cmsg->cmsg_type == SCM_RIGHTS)) {
bellard7854b052003-03-29 17:22:23 +00001242 int *fd = (int *)data;
1243 int *target_fd = (int *)target_data;
1244 int i, numfds = len / sizeof(int);
1245
1246 for (i = 0; i < numfds; i++)
1247 target_fd[i] = tswap32(fd[i]);
Petar Jovanovicdbf4f792013-09-13 19:27:29 +02001248 } else if ((cmsg->cmsg_level == SOL_SOCKET) &&
Jing Huangaebf5bc2012-07-24 14:01:42 +00001249 (cmsg->cmsg_type == SO_TIMESTAMP) &&
1250 (len == sizeof(struct timeval))) {
1251 /* copy struct timeval to target */
1252 struct timeval *tv = (struct timeval *)data;
1253 struct target_timeval *target_tv =
1254 (struct target_timeval *)target_data;
1255
1256 target_tv->tv_sec = tswapal(tv->tv_sec);
1257 target_tv->tv_usec = tswapal(tv->tv_usec);
1258 } else {
1259 gemu_log("Unsupported ancillary data: %d/%d\n",
1260 cmsg->cmsg_level, cmsg->cmsg_type);
1261 memcpy(target_data, data, len);
bellard7854b052003-03-29 17:22:23 +00001262 }
1263
1264 cmsg = CMSG_NXTHDR(msgh, cmsg);
1265 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1266 }
bellard5a4a8982007-11-11 17:39:18 +00001267 unlock_user(target_cmsg, target_cmsg_addr, space);
1268 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001269 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001270 return 0;
bellard7854b052003-03-29 17:22:23 +00001271}
1272
ths0da46a62007-10-20 20:23:07 +00001273/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001274static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001275 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001276{
blueswir1992f48a2007-10-14 16:27:31 +00001277 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001278 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001279 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001280 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001281
bellard8853f862004-02-22 14:57:26 +00001282 switch(level) {
1283 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001284 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001285 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001286 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001287
bellard2f619692007-11-16 10:46:05 +00001288 if (get_user_u32(val, optval_addr))
1289 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001290 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1291 break;
1292 case SOL_IP:
1293 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001294 case IP_TOS:
1295 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001296 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001297 case IP_ROUTER_ALERT:
1298 case IP_RECVOPTS:
1299 case IP_RETOPTS:
1300 case IP_PKTINFO:
1301 case IP_MTU_DISCOVER:
1302 case IP_RECVERR:
1303 case IP_RECVTOS:
1304#ifdef IP_FREEBIND
1305 case IP_FREEBIND:
1306#endif
1307 case IP_MULTICAST_TTL:
1308 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001309 val = 0;
1310 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001311 if (get_user_u32(val, optval_addr))
1312 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001313 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001314 if (get_user_u8(val, optval_addr))
1315 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001316 }
1317 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1318 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001319 case IP_ADD_MEMBERSHIP:
1320 case IP_DROP_MEMBERSHIP:
1321 if (optlen < sizeof (struct target_ip_mreq) ||
1322 optlen > sizeof (struct target_ip_mreqn))
1323 return -TARGET_EINVAL;
1324
1325 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1326 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1327 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1328 break;
1329
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001330 case IP_BLOCK_SOURCE:
1331 case IP_UNBLOCK_SOURCE:
1332 case IP_ADD_SOURCE_MEMBERSHIP:
1333 case IP_DROP_SOURCE_MEMBERSHIP:
1334 if (optlen != sizeof (struct target_ip_mreq_source))
1335 return -TARGET_EINVAL;
1336
1337 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1338 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1339 unlock_user (ip_mreq_source, optval_addr, 0);
1340 break;
1341
bellard8853f862004-02-22 14:57:26 +00001342 default:
1343 goto unimplemented;
1344 }
1345 break;
Laurent Vivier0d78b3b2013-08-30 01:46:43 +02001346 case SOL_IPV6:
1347 switch (optname) {
1348 case IPV6_MTU_DISCOVER:
1349 case IPV6_MTU:
1350 case IPV6_V6ONLY:
1351 case IPV6_RECVPKTINFO:
1352 val = 0;
1353 if (optlen < sizeof(uint32_t)) {
1354 return -TARGET_EINVAL;
1355 }
1356 if (get_user_u32(val, optval_addr)) {
1357 return -TARGET_EFAULT;
1358 }
1359 ret = get_errno(setsockopt(sockfd, level, optname,
1360 &val, sizeof(val)));
1361 break;
1362 default:
1363 goto unimplemented;
1364 }
1365 break;
Jing Huang920394d2012-07-24 13:59:23 +00001366 case SOL_RAW:
1367 switch (optname) {
1368 case ICMP_FILTER:
1369 /* struct icmp_filter takes an u32 value */
1370 if (optlen < sizeof(uint32_t)) {
1371 return -TARGET_EINVAL;
1372 }
1373
1374 if (get_user_u32(val, optval_addr)) {
1375 return -TARGET_EFAULT;
1376 }
1377 ret = get_errno(setsockopt(sockfd, level, optname,
1378 &val, sizeof(val)));
1379 break;
1380
1381 default:
1382 goto unimplemented;
1383 }
1384 break;
bellard3532fa72006-06-24 15:06:03 +00001385 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001386 switch (optname) {
Laurent Vivier1b09aeb2013-01-01 08:24:11 +00001387 case TARGET_SO_RCVTIMEO:
1388 {
1389 struct timeval tv;
1390
1391 optname = SO_RCVTIMEO;
1392
1393set_timeout:
1394 if (optlen != sizeof(struct target_timeval)) {
1395 return -TARGET_EINVAL;
1396 }
1397
1398 if (copy_from_user_timeval(&tv, optval_addr)) {
1399 return -TARGET_EFAULT;
1400 }
1401
1402 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1403 &tv, sizeof(tv)));
1404 return ret;
1405 }
1406 case TARGET_SO_SNDTIMEO:
1407 optname = SO_SNDTIMEO;
1408 goto set_timeout;
Laurent Vivierf57d4192013-08-30 01:46:41 +02001409 case TARGET_SO_ATTACH_FILTER:
1410 {
1411 struct target_sock_fprog *tfprog;
1412 struct target_sock_filter *tfilter;
1413 struct sock_fprog fprog;
1414 struct sock_filter *filter;
1415 int i;
1416
1417 if (optlen != sizeof(*tfprog)) {
1418 return -TARGET_EINVAL;
1419 }
1420 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
1421 return -TARGET_EFAULT;
1422 }
1423 if (!lock_user_struct(VERIFY_READ, tfilter,
1424 tswapal(tfprog->filter), 0)) {
1425 unlock_user_struct(tfprog, optval_addr, 1);
1426 return -TARGET_EFAULT;
1427 }
1428
1429 fprog.len = tswap16(tfprog->len);
1430 filter = malloc(fprog.len * sizeof(*filter));
1431 if (filter == NULL) {
1432 unlock_user_struct(tfilter, tfprog->filter, 1);
1433 unlock_user_struct(tfprog, optval_addr, 1);
1434 return -TARGET_ENOMEM;
1435 }
1436 for (i = 0; i < fprog.len; i++) {
1437 filter[i].code = tswap16(tfilter[i].code);
1438 filter[i].jt = tfilter[i].jt;
1439 filter[i].jf = tfilter[i].jf;
1440 filter[i].k = tswap32(tfilter[i].k);
1441 }
1442 fprog.filter = filter;
1443
1444 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
1445 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
1446 free(filter);
1447
1448 unlock_user_struct(tfilter, tfprog->filter, 1);
1449 unlock_user_struct(tfprog, optval_addr, 1);
1450 return ret;
1451 }
bellard8853f862004-02-22 14:57:26 +00001452 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001453 case TARGET_SO_DEBUG:
1454 optname = SO_DEBUG;
1455 break;
1456 case TARGET_SO_REUSEADDR:
1457 optname = SO_REUSEADDR;
1458 break;
1459 case TARGET_SO_TYPE:
1460 optname = SO_TYPE;
1461 break;
1462 case TARGET_SO_ERROR:
1463 optname = SO_ERROR;
1464 break;
1465 case TARGET_SO_DONTROUTE:
1466 optname = SO_DONTROUTE;
1467 break;
1468 case TARGET_SO_BROADCAST:
1469 optname = SO_BROADCAST;
1470 break;
1471 case TARGET_SO_SNDBUF:
1472 optname = SO_SNDBUF;
1473 break;
1474 case TARGET_SO_RCVBUF:
1475 optname = SO_RCVBUF;
1476 break;
1477 case TARGET_SO_KEEPALIVE:
1478 optname = SO_KEEPALIVE;
1479 break;
1480 case TARGET_SO_OOBINLINE:
1481 optname = SO_OOBINLINE;
1482 break;
1483 case TARGET_SO_NO_CHECK:
1484 optname = SO_NO_CHECK;
1485 break;
1486 case TARGET_SO_PRIORITY:
1487 optname = SO_PRIORITY;
1488 break;
bellard5e83e8e2005-03-01 22:32:06 +00001489#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001490 case TARGET_SO_BSDCOMPAT:
1491 optname = SO_BSDCOMPAT;
1492 break;
bellard5e83e8e2005-03-01 22:32:06 +00001493#endif
bellard3532fa72006-06-24 15:06:03 +00001494 case TARGET_SO_PASSCRED:
1495 optname = SO_PASSCRED;
1496 break;
1497 case TARGET_SO_TIMESTAMP:
1498 optname = SO_TIMESTAMP;
1499 break;
1500 case TARGET_SO_RCVLOWAT:
1501 optname = SO_RCVLOWAT;
1502 break;
bellard8853f862004-02-22 14:57:26 +00001503 break;
1504 default:
1505 goto unimplemented;
1506 }
bellard3532fa72006-06-24 15:06:03 +00001507 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001508 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001509
bellard2f619692007-11-16 10:46:05 +00001510 if (get_user_u32(val, optval_addr))
1511 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001512 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001513 break;
bellard7854b052003-03-29 17:22:23 +00001514 default:
bellard8853f862004-02-22 14:57:26 +00001515 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02001516 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001517 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001518 }
bellard8853f862004-02-22 14:57:26 +00001519 return ret;
bellard7854b052003-03-29 17:22:23 +00001520}
1521
ths0da46a62007-10-20 20:23:07 +00001522/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001523static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001524 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001525{
blueswir1992f48a2007-10-14 16:27:31 +00001526 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001527 int len, val;
1528 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001529
1530 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001531 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001532 level = SOL_SOCKET;
1533 switch (optname) {
1534 /* These don't just return a single integer */
1535 case TARGET_SO_LINGER:
1536 case TARGET_SO_RCVTIMEO:
1537 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001538 case TARGET_SO_PEERNAME:
1539 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02001540 case TARGET_SO_PEERCRED: {
1541 struct ucred cr;
1542 socklen_t crlen;
1543 struct target_ucred *tcr;
1544
1545 if (get_user_u32(len, optlen)) {
1546 return -TARGET_EFAULT;
1547 }
1548 if (len < 0) {
1549 return -TARGET_EINVAL;
1550 }
1551
1552 crlen = sizeof(cr);
1553 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1554 &cr, &crlen));
1555 if (ret < 0) {
1556 return ret;
1557 }
1558 if (len > crlen) {
1559 len = crlen;
1560 }
1561 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1562 return -TARGET_EFAULT;
1563 }
1564 __put_user(cr.pid, &tcr->pid);
1565 __put_user(cr.uid, &tcr->uid);
1566 __put_user(cr.gid, &tcr->gid);
1567 unlock_user_struct(tcr, optval_addr, 1);
1568 if (put_user_u32(len, optlen)) {
1569 return -TARGET_EFAULT;
1570 }
1571 break;
1572 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001573 /* Options with 'int' argument. */
1574 case TARGET_SO_DEBUG:
1575 optname = SO_DEBUG;
1576 goto int_case;
1577 case TARGET_SO_REUSEADDR:
1578 optname = SO_REUSEADDR;
1579 goto int_case;
1580 case TARGET_SO_TYPE:
1581 optname = SO_TYPE;
1582 goto int_case;
1583 case TARGET_SO_ERROR:
1584 optname = SO_ERROR;
1585 goto int_case;
1586 case TARGET_SO_DONTROUTE:
1587 optname = SO_DONTROUTE;
1588 goto int_case;
1589 case TARGET_SO_BROADCAST:
1590 optname = SO_BROADCAST;
1591 goto int_case;
1592 case TARGET_SO_SNDBUF:
1593 optname = SO_SNDBUF;
1594 goto int_case;
1595 case TARGET_SO_RCVBUF:
1596 optname = SO_RCVBUF;
1597 goto int_case;
1598 case TARGET_SO_KEEPALIVE:
1599 optname = SO_KEEPALIVE;
1600 goto int_case;
1601 case TARGET_SO_OOBINLINE:
1602 optname = SO_OOBINLINE;
1603 goto int_case;
1604 case TARGET_SO_NO_CHECK:
1605 optname = SO_NO_CHECK;
1606 goto int_case;
1607 case TARGET_SO_PRIORITY:
1608 optname = SO_PRIORITY;
1609 goto int_case;
1610#ifdef SO_BSDCOMPAT
1611 case TARGET_SO_BSDCOMPAT:
1612 optname = SO_BSDCOMPAT;
1613 goto int_case;
1614#endif
1615 case TARGET_SO_PASSCRED:
1616 optname = SO_PASSCRED;
1617 goto int_case;
1618 case TARGET_SO_TIMESTAMP:
1619 optname = SO_TIMESTAMP;
1620 goto int_case;
1621 case TARGET_SO_RCVLOWAT:
1622 optname = SO_RCVLOWAT;
1623 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001624 default:
bellard2efbe912005-07-23 15:10:20 +00001625 goto int_case;
1626 }
1627 break;
1628 case SOL_TCP:
1629 /* TCP options all take an 'int' value. */
1630 int_case:
bellard2f619692007-11-16 10:46:05 +00001631 if (get_user_u32(len, optlen))
1632 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001633 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001634 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001635 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00001636 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1637 if (ret < 0)
1638 return ret;
bellard2efbe912005-07-23 15:10:20 +00001639 if (len > lv)
1640 len = lv;
bellard2f619692007-11-16 10:46:05 +00001641 if (len == 4) {
1642 if (put_user_u32(val, optval_addr))
1643 return -TARGET_EFAULT;
1644 } else {
1645 if (put_user_u8(val, optval_addr))
1646 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001647 }
bellard2f619692007-11-16 10:46:05 +00001648 if (put_user_u32(len, optlen))
1649 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001650 break;
1651 case SOL_IP:
1652 switch(optname) {
1653 case IP_TOS:
1654 case IP_TTL:
1655 case IP_HDRINCL:
1656 case IP_ROUTER_ALERT:
1657 case IP_RECVOPTS:
1658 case IP_RETOPTS:
1659 case IP_PKTINFO:
1660 case IP_MTU_DISCOVER:
1661 case IP_RECVERR:
1662 case IP_RECVTOS:
1663#ifdef IP_FREEBIND
1664 case IP_FREEBIND:
1665#endif
1666 case IP_MULTICAST_TTL:
1667 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001668 if (get_user_u32(len, optlen))
1669 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001670 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001671 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001672 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001673 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1674 if (ret < 0)
1675 return ret;
bellard2efbe912005-07-23 15:10:20 +00001676 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001677 len = 1;
bellard2f619692007-11-16 10:46:05 +00001678 if (put_user_u32(len, optlen)
1679 || put_user_u8(val, optval_addr))
1680 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001681 } else {
bellard2efbe912005-07-23 15:10:20 +00001682 if (len > sizeof(int))
1683 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001684 if (put_user_u32(len, optlen)
1685 || put_user_u32(val, optval_addr))
1686 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001687 }
bellard8853f862004-02-22 14:57:26 +00001688 break;
bellard2efbe912005-07-23 15:10:20 +00001689 default:
thsc02f4992007-12-18 02:39:59 +00001690 ret = -TARGET_ENOPROTOOPT;
1691 break;
bellard8853f862004-02-22 14:57:26 +00001692 }
1693 break;
1694 default:
1695 unimplemented:
1696 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1697 level, optname);
thsc02f4992007-12-18 02:39:59 +00001698 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001699 break;
1700 }
1701 return ret;
bellard7854b052003-03-29 17:22:23 +00001702}
1703
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001704static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1705 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001706{
1707 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001708 struct iovec *vec;
1709 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00001710 int i;
Peter Maydell501bb4b2014-02-17 18:55:33 +00001711 int err = 0;
pbrook53a59602006-03-25 19:31:22 +00001712
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001713 if (count == 0) {
1714 errno = 0;
1715 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001716 }
Peter Maydelldfae8e02013-02-08 07:58:41 +00001717 if (count < 0 || count > IOV_MAX) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001718 errno = EINVAL;
1719 return NULL;
1720 }
1721
1722 vec = calloc(count, sizeof(struct iovec));
1723 if (vec == NULL) {
1724 errno = ENOMEM;
1725 return NULL;
1726 }
1727
1728 target_vec = lock_user(VERIFY_READ, target_addr,
1729 count * sizeof(struct target_iovec), 1);
1730 if (target_vec == NULL) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00001731 err = EFAULT;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001732 goto fail2;
1733 }
1734
1735 /* ??? If host page size > target page size, this will result in a
1736 value larger than what we can actually support. */
1737 max_len = 0x7fffffff & TARGET_PAGE_MASK;
1738 total_len = 0;
1739
1740 for (i = 0; i < count; i++) {
1741 abi_ulong base = tswapal(target_vec[i].iov_base);
1742 abi_long len = tswapal(target_vec[i].iov_len);
1743
1744 if (len < 0) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00001745 err = EINVAL;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001746 goto fail;
1747 } else if (len == 0) {
1748 /* Zero length pointer is ignored. */
1749 vec[i].iov_base = 0;
1750 } else {
1751 vec[i].iov_base = lock_user(type, base, len, copy);
1752 if (!vec[i].iov_base) {
Peter Maydell501bb4b2014-02-17 18:55:33 +00001753 err = EFAULT;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001754 goto fail;
1755 }
1756 if (len > max_len - total_len) {
1757 len = max_len - total_len;
1758 }
1759 }
1760 vec[i].iov_len = len;
1761 total_len += len;
1762 }
1763
1764 unlock_user(target_vec, target_addr, 0);
1765 return vec;
1766
1767 fail:
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001768 unlock_user(target_vec, target_addr, 0);
Peter Maydell501bb4b2014-02-17 18:55:33 +00001769 fail2:
1770 free(vec);
1771 errno = err;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001772 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001773}
1774
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001775static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1776 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001777{
1778 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00001779 int i;
1780
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001781 target_vec = lock_user(VERIFY_READ, target_addr,
1782 count * sizeof(struct target_iovec), 1);
1783 if (target_vec) {
1784 for (i = 0; i < count; i++) {
1785 abi_ulong base = tswapal(target_vec[i].iov_base);
1786 abi_long len = tswapal(target_vec[i].iov_base);
1787 if (len < 0) {
1788 break;
1789 }
balrogd732dcb2008-10-28 10:21:03 +00001790 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1791 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001792 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00001793 }
bellard579a97f2007-11-11 14:26:47 +00001794
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001795 free(vec);
pbrook53a59602006-03-25 19:31:22 +00001796}
1797
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02001798static inline int target_to_host_sock_type(int *type)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02001799{
1800 int host_type = 0;
1801 int target_type = *type;
1802
1803 switch (target_type & TARGET_SOCK_TYPE_MASK) {
1804 case TARGET_SOCK_DGRAM:
1805 host_type = SOCK_DGRAM;
1806 break;
1807 case TARGET_SOCK_STREAM:
1808 host_type = SOCK_STREAM;
1809 break;
1810 default:
1811 host_type = target_type & TARGET_SOCK_TYPE_MASK;
1812 break;
1813 }
1814 if (target_type & TARGET_SOCK_CLOEXEC) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02001815#if defined(SOCK_CLOEXEC)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02001816 host_type |= SOCK_CLOEXEC;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02001817#else
1818 return -TARGET_EINVAL;
1819#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02001820 }
1821 if (target_type & TARGET_SOCK_NONBLOCK) {
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02001822#if defined(SOCK_NONBLOCK)
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02001823 host_type |= SOCK_NONBLOCK;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02001824#elif !defined(O_NONBLOCK)
1825 return -TARGET_EINVAL;
1826#endif
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02001827 }
1828 *type = host_type;
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02001829 return 0;
1830}
1831
1832/* Try to emulate socket type flags after socket creation. */
1833static int sock_flags_fixup(int fd, int target_type)
1834{
1835#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
1836 if (target_type & TARGET_SOCK_NONBLOCK) {
1837 int flags = fcntl(fd, F_GETFL);
1838 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
1839 close(fd);
1840 return -TARGET_EINVAL;
1841 }
1842 }
1843#endif
1844 return fd;
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02001845}
1846
ths0da46a62007-10-20 20:23:07 +00001847/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001848static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001849{
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02001850 int target_type = type;
1851 int ret;
1852
1853 ret = target_to_host_sock_type(&type);
1854 if (ret) {
1855 return ret;
1856 }
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02001857
balrog12bc92a2007-10-30 21:06:14 +00001858 if (domain == PF_NETLINK)
1859 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
Edgar E. Iglesias53d09b72013-09-23 14:11:53 +02001860 ret = get_errno(socket(domain, type, protocol));
1861 if (ret >= 0) {
1862 ret = sock_flags_fixup(ret, target_type);
1863 }
1864 return ret;
bellard3532fa72006-06-24 15:06:03 +00001865}
1866
ths0da46a62007-10-20 20:23:07 +00001867/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001868static abi_long do_bind(int sockfd, abi_ulong target_addr,
1869 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001870{
aurel328f7aeaf2009-01-30 19:47:57 +00001871 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001872 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001873
Blue Swirl38724252010-09-18 05:53:14 +00001874 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001875 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001876 }
aurel328f7aeaf2009-01-30 19:47:57 +00001877
aurel32607175e2009-04-15 16:11:59 +00001878 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001879
Arnaud Patard917507b2009-06-19 10:44:45 +03001880 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1881 if (ret)
1882 return ret;
1883
bellard3532fa72006-06-24 15:06:03 +00001884 return get_errno(bind(sockfd, addr, addrlen));
1885}
1886
ths0da46a62007-10-20 20:23:07 +00001887/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001888static abi_long do_connect(int sockfd, abi_ulong target_addr,
1889 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001890{
aurel328f7aeaf2009-01-30 19:47:57 +00001891 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001892 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001893
Blue Swirl38724252010-09-18 05:53:14 +00001894 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001895 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001896 }
aurel328f7aeaf2009-01-30 19:47:57 +00001897
1898 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001899
Arnaud Patard917507b2009-06-19 10:44:45 +03001900 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1901 if (ret)
1902 return ret;
1903
bellard3532fa72006-06-24 15:06:03 +00001904 return get_errno(connect(sockfd, addr, addrlen));
1905}
1906
Alexander Graff19e00d2014-03-02 19:36:42 +00001907/* do_sendrecvmsg_locked() Must return target values and target errnos. */
1908static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
1909 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001910{
balrog6de645c2008-10-28 10:26:29 +00001911 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001912 struct msghdr msg;
1913 int count;
1914 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001915 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001916
bellard3532fa72006-06-24 15:06:03 +00001917 if (msgp->msg_name) {
1918 msg.msg_namelen = tswap32(msgp->msg_namelen);
1919 msg.msg_name = alloca(msg.msg_namelen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001920 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001921 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001922 if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001923 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03001924 }
bellard3532fa72006-06-24 15:06:03 +00001925 } else {
1926 msg.msg_name = NULL;
1927 msg.msg_namelen = 0;
1928 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001929 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00001930 msg.msg_control = alloca(msg.msg_controllen);
1931 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001932
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001933 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001934 target_vec = tswapal(msgp->msg_iov);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001935 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
1936 target_vec, count, send);
1937 if (vec == NULL) {
1938 ret = -host_to_target_errno(errno);
1939 goto out2;
1940 }
bellard3532fa72006-06-24 15:06:03 +00001941 msg.msg_iovlen = count;
1942 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001943
bellard3532fa72006-06-24 15:06:03 +00001944 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001945 ret = target_to_host_cmsg(&msg, msgp);
1946 if (ret == 0)
1947 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001948 } else {
1949 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001950 if (!is_error(ret)) {
1951 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001952 ret = host_to_target_cmsg(msgp, &msg);
Jing Huangca619062012-07-24 13:58:02 +00001953 if (!is_error(ret)) {
1954 msgp->msg_namelen = tswap32(msg.msg_namelen);
1955 if (msg.msg_name != NULL) {
1956 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
1957 msg.msg_name, msg.msg_namelen);
1958 if (ret) {
1959 goto out;
1960 }
1961 }
1962
balrog6de645c2008-10-28 10:26:29 +00001963 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00001964 }
balrog6de645c2008-10-28 10:26:29 +00001965 }
bellard3532fa72006-06-24 15:06:03 +00001966 }
Jing Huangca619062012-07-24 13:58:02 +00001967
1968out:
bellard3532fa72006-06-24 15:06:03 +00001969 unlock_iovec(vec, target_vec, count, !send);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001970out2:
Alexander Graff19e00d2014-03-02 19:36:42 +00001971 return ret;
1972}
1973
1974static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1975 int flags, int send)
1976{
1977 abi_long ret;
1978 struct target_msghdr *msgp;
1979
1980 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1981 msgp,
1982 target_msg,
1983 send ? 1 : 0)) {
1984 return -TARGET_EFAULT;
1985 }
1986 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
bellard579a97f2007-11-11 14:26:47 +00001987 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001988 return ret;
1989}
1990
Alexander Graff19e00d2014-03-02 19:36:42 +00001991#ifdef TARGET_NR_sendmmsg
1992/* We don't rely on the C library to have sendmmsg/recvmmsg support,
1993 * so it might not have this *mmsg-specific flag either.
1994 */
1995#ifndef MSG_WAITFORONE
1996#define MSG_WAITFORONE 0x10000
1997#endif
1998
1999static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
2000 unsigned int vlen, unsigned int flags,
2001 int send)
2002{
2003 struct target_mmsghdr *mmsgp;
2004 abi_long ret = 0;
2005 int i;
2006
2007 if (vlen > UIO_MAXIOV) {
2008 vlen = UIO_MAXIOV;
2009 }
2010
2011 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
2012 if (!mmsgp) {
2013 return -TARGET_EFAULT;
2014 }
2015
2016 for (i = 0; i < vlen; i++) {
2017 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
2018 if (is_error(ret)) {
2019 break;
2020 }
2021 mmsgp[i].msg_len = tswap32(ret);
2022 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2023 if (flags & MSG_WAITFORONE) {
2024 flags |= MSG_DONTWAIT;
2025 }
2026 }
2027
2028 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
2029
2030 /* Return number of datagrams sent if we sent any at all;
2031 * otherwise return the error.
2032 */
2033 if (i) {
2034 return i;
2035 }
2036 return ret;
2037}
2038#endif
2039
Peter Maydella94b4982013-02-08 04:35:04 +00002040/* If we don't have a system accept4() then just call accept.
2041 * The callsites to do_accept4() will ensure that they don't
2042 * pass a non-zero flags argument in this config.
2043 */
2044#ifndef CONFIG_ACCEPT4
2045static inline int accept4(int sockfd, struct sockaddr *addr,
2046 socklen_t *addrlen, int flags)
2047{
2048 assert(flags == 0);
2049 return accept(sockfd, addr, addrlen);
2050}
2051#endif
2052
2053/* do_accept4() Must return target values and target errnos. */
2054static abi_long do_accept4(int fd, abi_ulong target_addr,
2055 abi_ulong target_addrlen_addr, int flags)
pbrook1be9e1d2006-11-19 15:26:04 +00002056{
bellard2f619692007-11-16 10:46:05 +00002057 socklen_t addrlen;
2058 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002059 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002060
Peter Maydella94b4982013-02-08 04:35:04 +00002061 if (target_addr == 0) {
2062 return get_errno(accept4(fd, NULL, NULL, flags));
2063 }
Arnaud Patard917507b2009-06-19 10:44:45 +03002064
2065 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00002066 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03002067 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00002068
Blue Swirl38724252010-09-18 05:53:14 +00002069 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002070 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002071 }
aurel328f7aeaf2009-01-30 19:47:57 +00002072
Arnaud Patard917507b2009-06-19 10:44:45 +03002073 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2074 return -TARGET_EINVAL;
2075
bellard2f619692007-11-16 10:46:05 +00002076 addr = alloca(addrlen);
2077
Peter Maydella94b4982013-02-08 04:35:04 +00002078 ret = get_errno(accept4(fd, addr, &addrlen, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002079 if (!is_error(ret)) {
2080 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002081 if (put_user_u32(addrlen, target_addrlen_addr))
2082 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002083 }
2084 return ret;
2085}
2086
ths0da46a62007-10-20 20:23:07 +00002087/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002088static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002089 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002090{
bellard2f619692007-11-16 10:46:05 +00002091 socklen_t addrlen;
2092 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002093 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002094
bellard2f619692007-11-16 10:46:05 +00002095 if (get_user_u32(addrlen, target_addrlen_addr))
2096 return -TARGET_EFAULT;
2097
Blue Swirl38724252010-09-18 05:53:14 +00002098 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002099 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002100 }
aurel328f7aeaf2009-01-30 19:47:57 +00002101
Arnaud Patard917507b2009-06-19 10:44:45 +03002102 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2103 return -TARGET_EFAULT;
2104
bellard2f619692007-11-16 10:46:05 +00002105 addr = alloca(addrlen);
2106
pbrook1be9e1d2006-11-19 15:26:04 +00002107 ret = get_errno(getpeername(fd, addr, &addrlen));
2108 if (!is_error(ret)) {
2109 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002110 if (put_user_u32(addrlen, target_addrlen_addr))
2111 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002112 }
2113 return ret;
2114}
2115
ths0da46a62007-10-20 20:23:07 +00002116/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002117static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00002118 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002119{
bellard2f619692007-11-16 10:46:05 +00002120 socklen_t addrlen;
2121 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00002122 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002123
bellard2f619692007-11-16 10:46:05 +00002124 if (get_user_u32(addrlen, target_addrlen_addr))
2125 return -TARGET_EFAULT;
2126
Blue Swirl38724252010-09-18 05:53:14 +00002127 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002128 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002129 }
aurel328f7aeaf2009-01-30 19:47:57 +00002130
Arnaud Patard917507b2009-06-19 10:44:45 +03002131 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2132 return -TARGET_EFAULT;
2133
bellard2f619692007-11-16 10:46:05 +00002134 addr = alloca(addrlen);
2135
pbrook1be9e1d2006-11-19 15:26:04 +00002136 ret = get_errno(getsockname(fd, addr, &addrlen));
2137 if (!is_error(ret)) {
2138 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002139 if (put_user_u32(addrlen, target_addrlen_addr))
2140 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002141 }
2142 return ret;
2143}
2144
ths0da46a62007-10-20 20:23:07 +00002145/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002146static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00002147 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00002148{
2149 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00002150 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002151
Petar Jovanovicf651e6a2013-07-01 02:44:14 +02002152 target_to_host_sock_type(&type);
2153
pbrook1be9e1d2006-11-19 15:26:04 +00002154 ret = get_errno(socketpair(domain, type, protocol, tab));
2155 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00002156 if (put_user_s32(tab[0], target_tab_addr)
2157 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2158 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002159 }
2160 return ret;
2161}
2162
ths0da46a62007-10-20 20:23:07 +00002163/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002164static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2165 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002166{
2167 void *addr;
2168 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002169 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002170
Blue Swirl38724252010-09-18 05:53:14 +00002171 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002172 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00002173 }
aurel328f7aeaf2009-01-30 19:47:57 +00002174
bellard579a97f2007-11-11 14:26:47 +00002175 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2176 if (!host_msg)
2177 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002178 if (target_addr) {
2179 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03002180 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2181 if (ret) {
2182 unlock_user(host_msg, msg, 0);
2183 return ret;
2184 }
pbrook1be9e1d2006-11-19 15:26:04 +00002185 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2186 } else {
2187 ret = get_errno(send(fd, host_msg, len, flags));
2188 }
2189 unlock_user(host_msg, msg, 0);
2190 return ret;
2191}
2192
ths0da46a62007-10-20 20:23:07 +00002193/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002194static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2195 abi_ulong target_addr,
2196 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00002197{
2198 socklen_t addrlen;
2199 void *addr;
2200 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002201 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002202
bellard579a97f2007-11-11 14:26:47 +00002203 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2204 if (!host_msg)
2205 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002206 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00002207 if (get_user_u32(addrlen, target_addrlen)) {
2208 ret = -TARGET_EFAULT;
2209 goto fail;
2210 }
Blue Swirl38724252010-09-18 05:53:14 +00002211 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002212 ret = -TARGET_EINVAL;
2213 goto fail;
2214 }
pbrook1be9e1d2006-11-19 15:26:04 +00002215 addr = alloca(addrlen);
2216 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2217 } else {
2218 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00002219 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002220 }
2221 if (!is_error(ret)) {
2222 if (target_addr) {
2223 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002224 if (put_user_u32(addrlen, target_addrlen)) {
2225 ret = -TARGET_EFAULT;
2226 goto fail;
2227 }
pbrook1be9e1d2006-11-19 15:26:04 +00002228 }
2229 unlock_user(host_msg, msg, len);
2230 } else {
bellard2f619692007-11-16 10:46:05 +00002231fail:
pbrook1be9e1d2006-11-19 15:26:04 +00002232 unlock_user(host_msg, msg, 0);
2233 }
2234 return ret;
2235}
2236
j_mayer32407102007-09-26 23:01:49 +00002237#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00002238/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002239static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00002240{
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002241 static const unsigned ac[] = { /* number of arguments per call */
2242 [SOCKOP_socket] = 3, /* domain, type, protocol */
2243 [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
2244 [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
2245 [SOCKOP_listen] = 2, /* sockfd, backlog */
2246 [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
2247 [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
2248 [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
2249 [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
2250 [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
2251 [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
2252 [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
2253 [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2254 [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2255 [SOCKOP_shutdown] = 2, /* sockfd, how */
2256 [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
2257 [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
2258 [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2259 [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2260 };
2261 abi_long a[6]; /* max 6 args */
bellard31e31b82003-02-18 22:55:36 +00002262
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002263 /* first, collect the arguments in a[] according to ac[] */
2264 if (num >= 0 && num < ARRAY_SIZE(ac)) {
2265 unsigned i;
2266 assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */
2267 for (i = 0; i < ac[num]; ++i) {
2268 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
André Hentschelb9d36eb2014-01-06 20:18:52 +01002269 return -TARGET_EFAULT;
2270 }
André Hentschelb9d36eb2014-01-06 20:18:52 +01002271 }
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002272 }
bellard2f619692007-11-16 10:46:05 +00002273
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002274 /* now when we have the args, actually handle the call */
2275 switch (num) {
2276 case SOCKOP_socket: /* domain, type, protocol */
2277 return do_socket(a[0], a[1], a[2]);
2278 case SOCKOP_bind: /* sockfd, addr, addrlen */
2279 return do_bind(a[0], a[1], a[2]);
2280 case SOCKOP_connect: /* sockfd, addr, addrlen */
2281 return do_connect(a[0], a[1], a[2]);
2282 case SOCKOP_listen: /* sockfd, backlog */
2283 return get_errno(listen(a[0], a[1]));
2284 case SOCKOP_accept: /* sockfd, addr, addrlen */
2285 return do_accept4(a[0], a[1], a[2], 0);
2286 case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */
2287 return do_accept4(a[0], a[1], a[2], a[3]);
2288 case SOCKOP_getsockname: /* sockfd, addr, addrlen */
2289 return do_getsockname(a[0], a[1], a[2]);
2290 case SOCKOP_getpeername: /* sockfd, addr, addrlen */
2291 return do_getpeername(a[0], a[1], a[2]);
2292 case SOCKOP_socketpair: /* domain, type, protocol, tab */
2293 return do_socketpair(a[0], a[1], a[2], a[3]);
2294 case SOCKOP_send: /* sockfd, msg, len, flags */
2295 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
2296 case SOCKOP_recv: /* sockfd, msg, len, flags */
2297 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
2298 case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */
2299 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
2300 case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */
2301 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
2302 case SOCKOP_shutdown: /* sockfd, how */
2303 return get_errno(shutdown(a[0], a[1]));
2304 case SOCKOP_sendmsg: /* sockfd, msg, flags */
2305 return do_sendrecvmsg(a[0], a[1], a[2], 1);
2306 case SOCKOP_recvmsg: /* sockfd, msg, flags */
2307 return do_sendrecvmsg(a[0], a[1], a[2], 0);
2308 case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */
2309 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
2310 case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */
2311 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
bellard31e31b82003-02-18 22:55:36 +00002312 default:
2313 gemu_log("Unsupported socketcall: %d\n", num);
Michael Tokarev62dc90c2014-01-17 14:23:51 +04002314 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002315 }
bellard31e31b82003-02-18 22:55:36 +00002316}
j_mayer32407102007-09-26 23:01:49 +00002317#endif
bellard31e31b82003-02-18 22:55:36 +00002318
bellard8853f862004-02-22 14:57:26 +00002319#define N_SHM_REGIONS 32
2320
2321static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002322 abi_ulong start;
2323 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002324} shm_regions[N_SHM_REGIONS];
2325
ths3eb6b042007-06-03 14:26:27 +00002326struct target_semid_ds
2327{
2328 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002329 abi_ulong sem_otime;
2330 abi_ulong __unused1;
2331 abi_ulong sem_ctime;
2332 abi_ulong __unused2;
2333 abi_ulong sem_nsems;
2334 abi_ulong __unused3;
2335 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002336};
2337
bellard579a97f2007-11-11 14:26:47 +00002338static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2339 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002340{
2341 struct target_ipc_perm *target_ip;
2342 struct target_semid_ds *target_sd;
2343
bellard579a97f2007-11-11 14:26:47 +00002344 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2345 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002346 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002347 host_ip->__key = tswap32(target_ip->__key);
2348 host_ip->uid = tswap32(target_ip->uid);
2349 host_ip->gid = tswap32(target_ip->gid);
2350 host_ip->cuid = tswap32(target_ip->cuid);
2351 host_ip->cgid = tswap32(target_ip->cgid);
2352#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2353 host_ip->mode = tswap32(target_ip->mode);
2354#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002355 host_ip->mode = tswap16(target_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002356#endif
2357#if defined(TARGET_PPC)
2358 host_ip->__seq = tswap32(target_ip->__seq);
2359#else
2360 host_ip->__seq = tswap16(target_ip->__seq);
2361#endif
ths3eb6b042007-06-03 14:26:27 +00002362 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002363 return 0;
ths3eb6b042007-06-03 14:26:27 +00002364}
2365
bellard579a97f2007-11-11 14:26:47 +00002366static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2367 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002368{
2369 struct target_ipc_perm *target_ip;
2370 struct target_semid_ds *target_sd;
2371
bellard579a97f2007-11-11 14:26:47 +00002372 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2373 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002374 target_ip = &(target_sd->sem_perm);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002375 target_ip->__key = tswap32(host_ip->__key);
2376 target_ip->uid = tswap32(host_ip->uid);
2377 target_ip->gid = tswap32(host_ip->gid);
2378 target_ip->cuid = tswap32(host_ip->cuid);
2379 target_ip->cgid = tswap32(host_ip->cgid);
2380#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2381 target_ip->mode = tswap32(host_ip->mode);
2382#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002383 target_ip->mode = tswap16(host_ip->mode);
Petar Jovanovic55a2b162013-10-30 14:46:31 +01002384#endif
2385#if defined(TARGET_PPC)
2386 target_ip->__seq = tswap32(host_ip->__seq);
2387#else
2388 target_ip->__seq = tswap16(host_ip->__seq);
2389#endif
ths3eb6b042007-06-03 14:26:27 +00002390 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002391 return 0;
ths3eb6b042007-06-03 14:26:27 +00002392}
2393
bellard579a97f2007-11-11 14:26:47 +00002394static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2395 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002396{
2397 struct target_semid_ds *target_sd;
2398
bellard579a97f2007-11-11 14:26:47 +00002399 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2400 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002401 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2402 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002403 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2404 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2405 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002406 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002407 return 0;
ths3eb6b042007-06-03 14:26:27 +00002408}
2409
bellard579a97f2007-11-11 14:26:47 +00002410static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2411 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002412{
2413 struct target_semid_ds *target_sd;
2414
bellard579a97f2007-11-11 14:26:47 +00002415 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2416 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002417 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08002418 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002419 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2420 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2421 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002422 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002423 return 0;
ths3eb6b042007-06-03 14:26:27 +00002424}
2425
aurel32e5289082009-04-18 16:16:12 +00002426struct target_seminfo {
2427 int semmap;
2428 int semmni;
2429 int semmns;
2430 int semmnu;
2431 int semmsl;
2432 int semopm;
2433 int semume;
2434 int semusz;
2435 int semvmx;
2436 int semaem;
2437};
2438
2439static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2440 struct seminfo *host_seminfo)
2441{
2442 struct target_seminfo *target_seminfo;
2443 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2444 return -TARGET_EFAULT;
2445 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2446 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2447 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2448 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2449 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2450 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2451 __put_user(host_seminfo->semume, &target_seminfo->semume);
2452 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2453 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2454 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2455 unlock_user_struct(target_seminfo, target_addr, 1);
2456 return 0;
2457}
2458
thsfa294812007-02-02 22:05:00 +00002459union semun {
2460 int val;
ths3eb6b042007-06-03 14:26:27 +00002461 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002462 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002463 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002464};
2465
ths3eb6b042007-06-03 14:26:27 +00002466union target_semun {
2467 int val;
aurel32e5289082009-04-18 16:16:12 +00002468 abi_ulong buf;
2469 abi_ulong array;
2470 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002471};
2472
aurel32e5289082009-04-18 16:16:12 +00002473static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2474 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002475{
aurel32e5289082009-04-18 16:16:12 +00002476 int nsems;
2477 unsigned short *array;
2478 union semun semun;
2479 struct semid_ds semid_ds;
2480 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002481
aurel32e5289082009-04-18 16:16:12 +00002482 semun.buf = &semid_ds;
2483
2484 ret = semctl(semid, 0, IPC_STAT, semun);
2485 if (ret == -1)
2486 return get_errno(ret);
2487
2488 nsems = semid_ds.sem_nsems;
2489
2490 *host_array = malloc(nsems*sizeof(unsigned short));
Peter Maydell69d4c702014-02-17 18:55:34 +00002491 if (!*host_array) {
2492 return -TARGET_ENOMEM;
2493 }
aurel32e5289082009-04-18 16:16:12 +00002494 array = lock_user(VERIFY_READ, target_addr,
2495 nsems*sizeof(unsigned short), 1);
Peter Maydell69d4c702014-02-17 18:55:34 +00002496 if (!array) {
2497 free(*host_array);
aurel32e5289082009-04-18 16:16:12 +00002498 return -TARGET_EFAULT;
Peter Maydell69d4c702014-02-17 18:55:34 +00002499 }
aurel32e5289082009-04-18 16:16:12 +00002500
2501 for(i=0; i<nsems; i++) {
2502 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002503 }
aurel32e5289082009-04-18 16:16:12 +00002504 unlock_user(array, target_addr, 0);
2505
bellard579a97f2007-11-11 14:26:47 +00002506 return 0;
ths3eb6b042007-06-03 14:26:27 +00002507}
2508
aurel32e5289082009-04-18 16:16:12 +00002509static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2510 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002511{
aurel32e5289082009-04-18 16:16:12 +00002512 int nsems;
2513 unsigned short *array;
2514 union semun semun;
2515 struct semid_ds semid_ds;
2516 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002517
aurel32e5289082009-04-18 16:16:12 +00002518 semun.buf = &semid_ds;
2519
2520 ret = semctl(semid, 0, IPC_STAT, semun);
2521 if (ret == -1)
2522 return get_errno(ret);
2523
2524 nsems = semid_ds.sem_nsems;
2525
2526 array = lock_user(VERIFY_WRITE, target_addr,
2527 nsems*sizeof(unsigned short), 0);
2528 if (!array)
2529 return -TARGET_EFAULT;
2530
2531 for(i=0; i<nsems; i++) {
2532 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002533 }
aurel32e5289082009-04-18 16:16:12 +00002534 free(*host_array);
2535 unlock_user(array, target_addr, 1);
2536
bellard579a97f2007-11-11 14:26:47 +00002537 return 0;
ths3eb6b042007-06-03 14:26:27 +00002538}
2539
aurel32e5289082009-04-18 16:16:12 +00002540static inline abi_long do_semctl(int semid, int semnum, int cmd,
2541 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002542{
2543 union semun arg;
2544 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302545 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002546 struct seminfo seminfo;
2547 abi_long ret = -TARGET_EINVAL;
2548 abi_long err;
2549 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002550
2551 switch( cmd ) {
2552 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002553 case SETVAL:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002554 arg.val = tswap32(target_su.val);
aurel32e5289082009-04-18 16:16:12 +00002555 ret = get_errno(semctl(semid, semnum, cmd, arg));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002556 target_su.val = tswap32(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002557 break;
2558 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002559 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002560 err = target_to_host_semarray(semid, &array, target_su.array);
2561 if (err)
2562 return err;
2563 arg.array = array;
2564 ret = get_errno(semctl(semid, semnum, cmd, arg));
2565 err = host_to_target_semarray(semid, target_su.array, &array);
2566 if (err)
2567 return err;
ths3eb6b042007-06-03 14:26:27 +00002568 break;
2569 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002570 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002571 case SEM_STAT:
2572 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2573 if (err)
2574 return err;
2575 arg.buf = &dsarg;
2576 ret = get_errno(semctl(semid, semnum, cmd, arg));
2577 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2578 if (err)
2579 return err;
ths3eb6b042007-06-03 14:26:27 +00002580 break;
aurel32e5289082009-04-18 16:16:12 +00002581 case IPC_INFO:
2582 case SEM_INFO:
2583 arg.__buf = &seminfo;
2584 ret = get_errno(semctl(semid, semnum, cmd, arg));
2585 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2586 if (err)
2587 return err;
2588 break;
2589 case IPC_RMID:
2590 case GETPID:
2591 case GETNCNT:
2592 case GETZCNT:
2593 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2594 break;
ths3eb6b042007-06-03 14:26:27 +00002595 }
2596
2597 return ret;
2598}
2599
aurel32e5289082009-04-18 16:16:12 +00002600struct target_sembuf {
2601 unsigned short sem_num;
2602 short sem_op;
2603 short sem_flg;
2604};
2605
2606static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2607 abi_ulong target_addr,
2608 unsigned nsops)
2609{
2610 struct target_sembuf *target_sembuf;
2611 int i;
2612
2613 target_sembuf = lock_user(VERIFY_READ, target_addr,
2614 nsops*sizeof(struct target_sembuf), 1);
2615 if (!target_sembuf)
2616 return -TARGET_EFAULT;
2617
2618 for(i=0; i<nsops; i++) {
2619 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2620 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2621 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2622 }
2623
2624 unlock_user(target_sembuf, target_addr, 0);
2625
2626 return 0;
2627}
2628
2629static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2630{
2631 struct sembuf sops[nsops];
2632
2633 if (target_to_host_sembuf(sops, ptr, nsops))
2634 return -TARGET_EFAULT;
2635
Petar Jovanovicc7128c92013-03-21 07:57:36 +00002636 return get_errno(semop(semid, sops, nsops));
aurel32e5289082009-04-18 16:16:12 +00002637}
2638
ths1bc012f2007-06-03 14:27:49 +00002639struct target_msqid_ds
2640{
aurel321c54ff92008-10-13 21:08:44 +00002641 struct target_ipc_perm msg_perm;
2642 abi_ulong msg_stime;
2643#if TARGET_ABI_BITS == 32
2644 abi_ulong __unused1;
2645#endif
2646 abi_ulong msg_rtime;
2647#if TARGET_ABI_BITS == 32
2648 abi_ulong __unused2;
2649#endif
2650 abi_ulong msg_ctime;
2651#if TARGET_ABI_BITS == 32
2652 abi_ulong __unused3;
2653#endif
2654 abi_ulong __msg_cbytes;
2655 abi_ulong msg_qnum;
2656 abi_ulong msg_qbytes;
2657 abi_ulong msg_lspid;
2658 abi_ulong msg_lrpid;
2659 abi_ulong __unused4;
2660 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002661};
2662
bellard579a97f2007-11-11 14:26:47 +00002663static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2664 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002665{
2666 struct target_msqid_ds *target_md;
2667
bellard579a97f2007-11-11 14:26:47 +00002668 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2669 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002670 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2671 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002672 host_md->msg_stime = tswapal(target_md->msg_stime);
2673 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2674 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2675 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2676 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2677 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2678 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2679 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002680 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002681 return 0;
ths1bc012f2007-06-03 14:27:49 +00002682}
2683
bellard579a97f2007-11-11 14:26:47 +00002684static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2685 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002686{
2687 struct target_msqid_ds *target_md;
2688
bellard579a97f2007-11-11 14:26:47 +00002689 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2690 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002691 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2692 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002693 target_md->msg_stime = tswapal(host_md->msg_stime);
2694 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2695 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2696 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2697 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2698 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2699 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2700 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002701 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002702 return 0;
ths1bc012f2007-06-03 14:27:49 +00002703}
2704
aurel321c54ff92008-10-13 21:08:44 +00002705struct target_msginfo {
2706 int msgpool;
2707 int msgmap;
2708 int msgmax;
2709 int msgmnb;
2710 int msgmni;
2711 int msgssz;
2712 int msgtql;
2713 unsigned short int msgseg;
2714};
2715
2716static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2717 struct msginfo *host_msginfo)
2718{
2719 struct target_msginfo *target_msginfo;
2720 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2721 return -TARGET_EFAULT;
2722 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2723 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2724 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2725 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2726 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2727 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2728 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2729 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2730 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002731 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002732}
2733
2734static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002735{
2736 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002737 struct msginfo msginfo;
2738 abi_long ret = -TARGET_EINVAL;
2739
2740 cmd &= 0xff;
2741
2742 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002743 case IPC_STAT:
2744 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002745 case MSG_STAT:
2746 if (target_to_host_msqid_ds(&dsarg,ptr))
2747 return -TARGET_EFAULT;
2748 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2749 if (host_to_target_msqid_ds(ptr,&dsarg))
2750 return -TARGET_EFAULT;
2751 break;
2752 case IPC_RMID:
2753 ret = get_errno(msgctl(msgid, cmd, NULL));
2754 break;
2755 case IPC_INFO:
2756 case MSG_INFO:
2757 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2758 if (host_to_target_msginfo(ptr, &msginfo))
2759 return -TARGET_EFAULT;
2760 break;
ths1bc012f2007-06-03 14:27:49 +00002761 }
aurel321c54ff92008-10-13 21:08:44 +00002762
ths1bc012f2007-06-03 14:27:49 +00002763 return ret;
2764}
2765
2766struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002767 abi_long mtype;
2768 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002769};
2770
blueswir1992f48a2007-10-14 16:27:31 +00002771static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2772 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002773{
2774 struct target_msgbuf *target_mb;
2775 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002776 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002777
bellard579a97f2007-11-11 14:26:47 +00002778 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2779 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002780 host_mb = malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002781 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00002782 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002783 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2784 free(host_mb);
2785 unlock_user_struct(target_mb, msgp, 0);
2786
2787 return ret;
2788}
2789
blueswir1992f48a2007-10-14 16:27:31 +00002790static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002791 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002792 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002793{
2794 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002795 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002796 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002797 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002798
bellard579a97f2007-11-11 14:26:47 +00002799 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2800 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002801
Jim Meyering0d07fe42012-08-22 13:55:53 +02002802 host_mb = g_malloc(msgsz+sizeof(long));
Laurent Vivier79dd77d2012-12-20 11:00:11 +00002803 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
aurel321c54ff92008-10-13 21:08:44 +00002804
bellard579a97f2007-11-11 14:26:47 +00002805 if (ret > 0) {
2806 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2807 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2808 if (!target_mtext) {
2809 ret = -TARGET_EFAULT;
2810 goto end;
2811 }
aurel321c54ff92008-10-13 21:08:44 +00002812 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002813 unlock_user(target_mtext, target_mtext_addr, ret);
2814 }
aurel321c54ff92008-10-13 21:08:44 +00002815
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002816 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00002817
bellard579a97f2007-11-11 14:26:47 +00002818end:
2819 if (target_mb)
2820 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02002821 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002822 return ret;
2823}
2824
Riku Voipio88a8c982009-04-03 10:42:00 +03002825static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2826 abi_ulong target_addr)
2827{
2828 struct target_shmid_ds *target_sd;
2829
2830 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2831 return -TARGET_EFAULT;
2832 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2833 return -TARGET_EFAULT;
2834 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2835 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2836 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2837 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2838 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2839 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2840 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2841 unlock_user_struct(target_sd, target_addr, 0);
2842 return 0;
2843}
2844
2845static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2846 struct shmid_ds *host_sd)
2847{
2848 struct target_shmid_ds *target_sd;
2849
2850 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2851 return -TARGET_EFAULT;
2852 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2853 return -TARGET_EFAULT;
2854 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2855 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2856 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2857 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2858 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2859 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2860 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2861 unlock_user_struct(target_sd, target_addr, 1);
2862 return 0;
2863}
2864
2865struct target_shminfo {
2866 abi_ulong shmmax;
2867 abi_ulong shmmin;
2868 abi_ulong shmmni;
2869 abi_ulong shmseg;
2870 abi_ulong shmall;
2871};
2872
2873static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2874 struct shminfo *host_shminfo)
2875{
2876 struct target_shminfo *target_shminfo;
2877 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2878 return -TARGET_EFAULT;
2879 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2880 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2881 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2882 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2883 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2884 unlock_user_struct(target_shminfo, target_addr, 1);
2885 return 0;
2886}
2887
2888struct target_shm_info {
2889 int used_ids;
2890 abi_ulong shm_tot;
2891 abi_ulong shm_rss;
2892 abi_ulong shm_swp;
2893 abi_ulong swap_attempts;
2894 abi_ulong swap_successes;
2895};
2896
2897static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2898 struct shm_info *host_shm_info)
2899{
2900 struct target_shm_info *target_shm_info;
2901 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2902 return -TARGET_EFAULT;
2903 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2904 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2905 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2906 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2907 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2908 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2909 unlock_user_struct(target_shm_info, target_addr, 1);
2910 return 0;
2911}
2912
2913static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2914{
2915 struct shmid_ds dsarg;
2916 struct shminfo shminfo;
2917 struct shm_info shm_info;
2918 abi_long ret = -TARGET_EINVAL;
2919
2920 cmd &= 0xff;
2921
2922 switch(cmd) {
2923 case IPC_STAT:
2924 case IPC_SET:
2925 case SHM_STAT:
2926 if (target_to_host_shmid_ds(&dsarg, buf))
2927 return -TARGET_EFAULT;
2928 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2929 if (host_to_target_shmid_ds(buf, &dsarg))
2930 return -TARGET_EFAULT;
2931 break;
2932 case IPC_INFO:
2933 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2934 if (host_to_target_shminfo(buf, &shminfo))
2935 return -TARGET_EFAULT;
2936 break;
2937 case SHM_INFO:
2938 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2939 if (host_to_target_shm_info(buf, &shm_info))
2940 return -TARGET_EFAULT;
2941 break;
2942 case IPC_RMID:
2943 case SHM_LOCK:
2944 case SHM_UNLOCK:
2945 ret = get_errno(shmctl(shmid, cmd, NULL));
2946 break;
2947 }
2948
2949 return ret;
2950}
2951
2952static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2953{
2954 abi_long raddr;
2955 void *host_raddr;
2956 struct shmid_ds shm_info;
2957 int i,ret;
2958
2959 /* find out the length of the shared memory segment */
2960 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2961 if (is_error(ret)) {
2962 /* can't get length, bail out */
2963 return ret;
2964 }
2965
2966 mmap_lock();
2967
2968 if (shmaddr)
2969 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2970 else {
2971 abi_ulong mmap_start;
2972
2973 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2974
2975 if (mmap_start == -1) {
2976 errno = ENOMEM;
2977 host_raddr = (void *)-1;
2978 } else
2979 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2980 }
2981
2982 if (host_raddr == (void *)-1) {
2983 mmap_unlock();
2984 return get_errno((long)host_raddr);
2985 }
2986 raddr=h2g((unsigned long)host_raddr);
2987
2988 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2989 PAGE_VALID | PAGE_READ |
2990 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2991
2992 for (i = 0; i < N_SHM_REGIONS; i++) {
2993 if (shm_regions[i].start == 0) {
2994 shm_regions[i].start = raddr;
2995 shm_regions[i].size = shm_info.shm_segsz;
2996 break;
2997 }
2998 }
2999
3000 mmap_unlock();
3001 return raddr;
3002
3003}
3004
3005static inline abi_long do_shmdt(abi_ulong shmaddr)
3006{
3007 int i;
3008
3009 for (i = 0; i < N_SHM_REGIONS; ++i) {
3010 if (shm_regions[i].start == shmaddr) {
3011 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09003012 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03003013 break;
3014 }
3015 }
3016
3017 return get_errno(shmdt(g2h(shmaddr)));
3018}
3019
aurel321c54ff92008-10-13 21:08:44 +00003020#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00003021/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00003022/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003023static abi_long do_ipc(unsigned int call, int first,
3024 int second, int third,
3025 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00003026{
3027 int version;
blueswir1992f48a2007-10-14 16:27:31 +00003028 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00003029
3030 version = call >> 16;
3031 call &= 0xffff;
3032
3033 switch (call) {
thsfa294812007-02-02 22:05:00 +00003034 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00003035 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00003036 break;
3037
3038 case IPCOP_semget:
3039 ret = get_errno(semget(first, second, third));
3040 break;
3041
3042 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00003043 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00003044 break;
thsd96372e2007-02-02 22:05:44 +00003045
aurel321c54ff92008-10-13 21:08:44 +00003046 case IPCOP_msgget:
3047 ret = get_errno(msgget(first, second));
3048 break;
thsd96372e2007-02-02 22:05:44 +00003049
aurel321c54ff92008-10-13 21:08:44 +00003050 case IPCOP_msgsnd:
3051 ret = do_msgsnd(first, ptr, second, third);
3052 break;
thsd96372e2007-02-02 22:05:44 +00003053
aurel321c54ff92008-10-13 21:08:44 +00003054 case IPCOP_msgctl:
3055 ret = do_msgctl(first, second, ptr);
3056 break;
thsd96372e2007-02-02 22:05:44 +00003057
aurel321c54ff92008-10-13 21:08:44 +00003058 case IPCOP_msgrcv:
3059 switch (version) {
3060 case 0:
3061 {
3062 struct target_ipc_kludge {
3063 abi_long msgp;
3064 abi_long msgtyp;
3065 } *tmp;
thsd96372e2007-02-02 22:05:44 +00003066
aurel321c54ff92008-10-13 21:08:44 +00003067 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3068 ret = -TARGET_EFAULT;
3069 break;
ths1bc012f2007-06-03 14:27:49 +00003070 }
aurel321c54ff92008-10-13 21:08:44 +00003071
Laurent Vivier79dd77d2012-12-20 11:00:11 +00003072 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
aurel321c54ff92008-10-13 21:08:44 +00003073
3074 unlock_user_struct(tmp, ptr, 0);
3075 break;
3076 }
3077 default:
3078 ret = do_msgrcv(first, ptr, second, fifth, third);
3079 }
3080 break;
thsd96372e2007-02-02 22:05:44 +00003081
bellard8853f862004-02-22 14:57:26 +00003082 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03003083 switch (version) {
3084 default:
bellard5a4a8982007-11-11 17:39:18 +00003085 {
3086 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03003087 raddr = do_shmat(first, ptr, second);
3088 if (is_error(raddr))
3089 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00003090 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00003091 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03003092 break;
3093 }
3094 case 1:
3095 ret = -TARGET_EINVAL;
3096 break;
bellard5a4a8982007-11-11 17:39:18 +00003097 }
bellard8853f862004-02-22 14:57:26 +00003098 break;
3099 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03003100 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00003101 break;
3102
3103 case IPCOP_shmget:
3104 /* IPC_* flag values are the same on all linux platforms */
3105 ret = get_errno(shmget(first, second, third));
3106 break;
3107
3108 /* IPC_* and SHM_* command values are the same on all linux platforms */
3109 case IPCOP_shmctl:
Petar Jovanovica2926782013-10-30 14:46:32 +01003110 ret = do_shmctl(first, second, ptr);
bellard8853f862004-02-22 14:57:26 +00003111 break;
3112 default:
j_mayer32407102007-09-26 23:01:49 +00003113 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00003114 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00003115 break;
3116 }
3117 return ret;
3118}
j_mayer32407102007-09-26 23:01:49 +00003119#endif
bellard8853f862004-02-22 14:57:26 +00003120
bellard31e31b82003-02-18 22:55:36 +00003121/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00003122
Blue Swirl001faf32009-05-13 17:53:17 +00003123#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00003124#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3125enum {
3126#include "syscall_types.h"
3127};
3128#undef STRUCT
3129#undef STRUCT_SPECIAL
3130
Blue Swirl001faf32009-05-13 17:53:17 +00003131#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00003132#define STRUCT_SPECIAL(name)
3133#include "syscall_types.h"
3134#undef STRUCT
3135#undef STRUCT_SPECIAL
3136
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003137typedef struct IOCTLEntry IOCTLEntry;
3138
3139typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3140 int fd, abi_long cmd, abi_long arg);
3141
3142struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00003143 unsigned int target_cmd;
3144 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00003145 const char *name;
3146 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003147 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003148 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003149};
bellard31e31b82003-02-18 22:55:36 +00003150
3151#define IOC_R 0x0001
3152#define IOC_W 0x0002
3153#define IOC_RW (IOC_R | IOC_W)
3154
3155#define MAX_STRUCT_SIZE 4096
3156
Peter Maydelldace20d2011-01-10 13:11:24 +00003157#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003158/* So fiemap access checks don't overflow on 32 bit systems.
3159 * This is very slightly smaller than the limit imposed by
3160 * the underlying kernel.
3161 */
3162#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3163 / sizeof(struct fiemap_extent))
3164
3165static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3166 int fd, abi_long cmd, abi_long arg)
3167{
3168 /* The parameter for this ioctl is a struct fiemap followed
3169 * by an array of struct fiemap_extent whose size is set
3170 * in fiemap->fm_extent_count. The array is filled in by the
3171 * ioctl.
3172 */
3173 int target_size_in, target_size_out;
3174 struct fiemap *fm;
3175 const argtype *arg_type = ie->arg_type;
3176 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3177 void *argptr, *p;
3178 abi_long ret;
3179 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3180 uint32_t outbufsz;
3181 int free_fm = 0;
3182
3183 assert(arg_type[0] == TYPE_PTR);
3184 assert(ie->access == IOC_RW);
3185 arg_type++;
3186 target_size_in = thunk_type_size(arg_type, 0);
3187 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3188 if (!argptr) {
3189 return -TARGET_EFAULT;
3190 }
3191 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3192 unlock_user(argptr, arg, 0);
3193 fm = (struct fiemap *)buf_temp;
3194 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3195 return -TARGET_EINVAL;
3196 }
3197
3198 outbufsz = sizeof (*fm) +
3199 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3200
3201 if (outbufsz > MAX_STRUCT_SIZE) {
3202 /* We can't fit all the extents into the fixed size buffer.
3203 * Allocate one that is large enough and use it instead.
3204 */
3205 fm = malloc(outbufsz);
3206 if (!fm) {
3207 return -TARGET_ENOMEM;
3208 }
3209 memcpy(fm, buf_temp, sizeof(struct fiemap));
3210 free_fm = 1;
3211 }
3212 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3213 if (!is_error(ret)) {
3214 target_size_out = target_size_in;
3215 /* An extent_count of 0 means we were only counting the extents
3216 * so there are no structs to copy
3217 */
3218 if (fm->fm_extent_count != 0) {
3219 target_size_out += fm->fm_mapped_extents * extent_size;
3220 }
3221 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3222 if (!argptr) {
3223 ret = -TARGET_EFAULT;
3224 } else {
3225 /* Convert the struct fiemap */
3226 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3227 if (fm->fm_extent_count != 0) {
3228 p = argptr + target_size_in;
3229 /* ...and then all the struct fiemap_extents */
3230 for (i = 0; i < fm->fm_mapped_extents; i++) {
3231 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3232 THUNK_TARGET);
3233 p += extent_size;
3234 }
3235 }
3236 unlock_user(argptr, arg, target_size_out);
3237 }
3238 }
3239 if (free_fm) {
3240 free(fm);
3241 }
3242 return ret;
3243}
Peter Maydelldace20d2011-01-10 13:11:24 +00003244#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003245
Laurent Vivier059c2f22011-03-30 00:12:12 +02003246static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3247 int fd, abi_long cmd, abi_long arg)
3248{
3249 const argtype *arg_type = ie->arg_type;
3250 int target_size;
3251 void *argptr;
3252 int ret;
3253 struct ifconf *host_ifconf;
3254 uint32_t outbufsz;
3255 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3256 int target_ifreq_size;
3257 int nb_ifreq;
3258 int free_buf = 0;
3259 int i;
3260 int target_ifc_len;
3261 abi_long target_ifc_buf;
3262 int host_ifc_len;
3263 char *host_ifc_buf;
3264
3265 assert(arg_type[0] == TYPE_PTR);
3266 assert(ie->access == IOC_RW);
3267
3268 arg_type++;
3269 target_size = thunk_type_size(arg_type, 0);
3270
3271 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3272 if (!argptr)
3273 return -TARGET_EFAULT;
3274 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3275 unlock_user(argptr, arg, 0);
3276
3277 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3278 target_ifc_len = host_ifconf->ifc_len;
3279 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3280
3281 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3282 nb_ifreq = target_ifc_len / target_ifreq_size;
3283 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3284
3285 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3286 if (outbufsz > MAX_STRUCT_SIZE) {
3287 /* We can't fit all the extents into the fixed size buffer.
3288 * Allocate one that is large enough and use it instead.
3289 */
3290 host_ifconf = malloc(outbufsz);
3291 if (!host_ifconf) {
3292 return -TARGET_ENOMEM;
3293 }
3294 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3295 free_buf = 1;
3296 }
3297 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3298
3299 host_ifconf->ifc_len = host_ifc_len;
3300 host_ifconf->ifc_buf = host_ifc_buf;
3301
3302 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3303 if (!is_error(ret)) {
3304 /* convert host ifc_len to target ifc_len */
3305
3306 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3307 target_ifc_len = nb_ifreq * target_ifreq_size;
3308 host_ifconf->ifc_len = target_ifc_len;
3309
3310 /* restore target ifc_buf */
3311
3312 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3313
3314 /* copy struct ifconf to target user */
3315
3316 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3317 if (!argptr)
3318 return -TARGET_EFAULT;
3319 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3320 unlock_user(argptr, arg, target_size);
3321
3322 /* copy ifreq[] to target user */
3323
3324 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3325 for (i = 0; i < nb_ifreq ; i++) {
3326 thunk_convert(argptr + i * target_ifreq_size,
3327 host_ifc_buf + i * sizeof(struct ifreq),
3328 ifreq_arg_type, THUNK_TARGET);
3329 }
3330 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3331 }
3332
3333 if (free_buf) {
3334 free(host_ifconf);
3335 }
3336
3337 return ret;
3338}
3339
Alexander Graf56e904e2012-01-31 18:42:06 +01003340static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3341 abi_long cmd, abi_long arg)
3342{
3343 void *argptr;
3344 struct dm_ioctl *host_dm;
3345 abi_long guest_data;
3346 uint32_t guest_data_size;
3347 int target_size;
3348 const argtype *arg_type = ie->arg_type;
3349 abi_long ret;
3350 void *big_buf = NULL;
3351 char *host_data;
3352
3353 arg_type++;
3354 target_size = thunk_type_size(arg_type, 0);
3355 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3356 if (!argptr) {
3357 ret = -TARGET_EFAULT;
3358 goto out;
3359 }
3360 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3361 unlock_user(argptr, arg, 0);
3362
3363 /* buf_temp is too small, so fetch things into a bigger buffer */
3364 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3365 memcpy(big_buf, buf_temp, target_size);
3366 buf_temp = big_buf;
3367 host_dm = big_buf;
3368
3369 guest_data = arg + host_dm->data_start;
3370 if ((guest_data - arg) < 0) {
3371 ret = -EINVAL;
3372 goto out;
3373 }
3374 guest_data_size = host_dm->data_size - host_dm->data_start;
3375 host_data = (char*)host_dm + host_dm->data_start;
3376
3377 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3378 switch (ie->host_cmd) {
3379 case DM_REMOVE_ALL:
3380 case DM_LIST_DEVICES:
3381 case DM_DEV_CREATE:
3382 case DM_DEV_REMOVE:
3383 case DM_DEV_SUSPEND:
3384 case DM_DEV_STATUS:
3385 case DM_DEV_WAIT:
3386 case DM_TABLE_STATUS:
3387 case DM_TABLE_CLEAR:
3388 case DM_TABLE_DEPS:
3389 case DM_LIST_VERSIONS:
3390 /* no input data */
3391 break;
3392 case DM_DEV_RENAME:
3393 case DM_DEV_SET_GEOMETRY:
3394 /* data contains only strings */
3395 memcpy(host_data, argptr, guest_data_size);
3396 break;
3397 case DM_TARGET_MSG:
3398 memcpy(host_data, argptr, guest_data_size);
3399 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3400 break;
3401 case DM_TABLE_LOAD:
3402 {
3403 void *gspec = argptr;
3404 void *cur_data = host_data;
3405 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3406 int spec_size = thunk_type_size(arg_type, 0);
3407 int i;
3408
3409 for (i = 0; i < host_dm->target_count; i++) {
3410 struct dm_target_spec *spec = cur_data;
3411 uint32_t next;
3412 int slen;
3413
3414 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3415 slen = strlen((char*)gspec + spec_size) + 1;
3416 next = spec->next;
3417 spec->next = sizeof(*spec) + slen;
3418 strcpy((char*)&spec[1], gspec + spec_size);
3419 gspec += next;
3420 cur_data += spec->next;
3421 }
3422 break;
3423 }
3424 default:
3425 ret = -TARGET_EINVAL;
3426 goto out;
3427 }
3428 unlock_user(argptr, guest_data, 0);
3429
3430 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3431 if (!is_error(ret)) {
3432 guest_data = arg + host_dm->data_start;
3433 guest_data_size = host_dm->data_size - host_dm->data_start;
3434 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3435 switch (ie->host_cmd) {
3436 case DM_REMOVE_ALL:
3437 case DM_DEV_CREATE:
3438 case DM_DEV_REMOVE:
3439 case DM_DEV_RENAME:
3440 case DM_DEV_SUSPEND:
3441 case DM_DEV_STATUS:
3442 case DM_TABLE_LOAD:
3443 case DM_TABLE_CLEAR:
3444 case DM_TARGET_MSG:
3445 case DM_DEV_SET_GEOMETRY:
3446 /* no return data */
3447 break;
3448 case DM_LIST_DEVICES:
3449 {
3450 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3451 uint32_t remaining_data = guest_data_size;
3452 void *cur_data = argptr;
3453 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3454 int nl_size = 12; /* can't use thunk_size due to alignment */
3455
3456 while (1) {
3457 uint32_t next = nl->next;
3458 if (next) {
3459 nl->next = nl_size + (strlen(nl->name) + 1);
3460 }
3461 if (remaining_data < nl->next) {
3462 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3463 break;
3464 }
3465 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3466 strcpy(cur_data + nl_size, nl->name);
3467 cur_data += nl->next;
3468 remaining_data -= nl->next;
3469 if (!next) {
3470 break;
3471 }
3472 nl = (void*)nl + next;
3473 }
3474 break;
3475 }
3476 case DM_DEV_WAIT:
3477 case DM_TABLE_STATUS:
3478 {
3479 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3480 void *cur_data = argptr;
3481 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3482 int spec_size = thunk_type_size(arg_type, 0);
3483 int i;
3484
3485 for (i = 0; i < host_dm->target_count; i++) {
3486 uint32_t next = spec->next;
3487 int slen = strlen((char*)&spec[1]) + 1;
3488 spec->next = (cur_data - argptr) + spec_size + slen;
3489 if (guest_data_size < spec->next) {
3490 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3491 break;
3492 }
3493 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3494 strcpy(cur_data + spec_size, (char*)&spec[1]);
3495 cur_data = argptr + spec->next;
3496 spec = (void*)host_dm + host_dm->data_start + next;
3497 }
3498 break;
3499 }
3500 case DM_TABLE_DEPS:
3501 {
3502 void *hdata = (void*)host_dm + host_dm->data_start;
3503 int count = *(uint32_t*)hdata;
3504 uint64_t *hdev = hdata + 8;
3505 uint64_t *gdev = argptr + 8;
3506 int i;
3507
3508 *(uint32_t*)argptr = tswap32(count);
3509 for (i = 0; i < count; i++) {
3510 *gdev = tswap64(*hdev);
3511 gdev++;
3512 hdev++;
3513 }
3514 break;
3515 }
3516 case DM_LIST_VERSIONS:
3517 {
3518 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3519 uint32_t remaining_data = guest_data_size;
3520 void *cur_data = argptr;
3521 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3522 int vers_size = thunk_type_size(arg_type, 0);
3523
3524 while (1) {
3525 uint32_t next = vers->next;
3526 if (next) {
3527 vers->next = vers_size + (strlen(vers->name) + 1);
3528 }
3529 if (remaining_data < vers->next) {
3530 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3531 break;
3532 }
3533 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3534 strcpy(cur_data + vers_size, vers->name);
3535 cur_data += vers->next;
3536 remaining_data -= vers->next;
3537 if (!next) {
3538 break;
3539 }
3540 vers = (void*)vers + next;
3541 }
3542 break;
3543 }
3544 default:
3545 ret = -TARGET_EINVAL;
3546 goto out;
3547 }
3548 unlock_user(argptr, guest_data, guest_data_size);
3549
3550 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3551 if (!argptr) {
3552 ret = -TARGET_EFAULT;
3553 goto out;
3554 }
3555 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3556 unlock_user(argptr, arg, target_size);
3557 }
3558out:
Stefan Weilad11ad72012-09-04 22:14:19 +02003559 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01003560 return ret;
3561}
3562
Laurent Vivier7ff7b662013-07-02 14:04:12 +01003563static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
3564 int fd, abi_long cmd, abi_long arg)
3565{
3566 const argtype *arg_type = ie->arg_type;
3567 const StructEntry *se;
3568 const argtype *field_types;
3569 const int *dst_offsets, *src_offsets;
3570 int target_size;
3571 void *argptr;
3572 abi_ulong *target_rt_dev_ptr;
3573 unsigned long *host_rt_dev_ptr;
3574 abi_long ret;
3575 int i;
3576
3577 assert(ie->access == IOC_W);
3578 assert(*arg_type == TYPE_PTR);
3579 arg_type++;
3580 assert(*arg_type == TYPE_STRUCT);
3581 target_size = thunk_type_size(arg_type, 0);
3582 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3583 if (!argptr) {
3584 return -TARGET_EFAULT;
3585 }
3586 arg_type++;
3587 assert(*arg_type == (int)STRUCT_rtentry);
3588 se = struct_entries + *arg_type++;
3589 assert(se->convert[0] == NULL);
3590 /* convert struct here to be able to catch rt_dev string */
3591 field_types = se->field_types;
3592 dst_offsets = se->field_offsets[THUNK_HOST];
3593 src_offsets = se->field_offsets[THUNK_TARGET];
3594 for (i = 0; i < se->nb_fields; i++) {
3595 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
3596 assert(*field_types == TYPE_PTRVOID);
3597 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
3598 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
3599 if (*target_rt_dev_ptr != 0) {
3600 *host_rt_dev_ptr = (unsigned long)lock_user_string(
3601 tswapal(*target_rt_dev_ptr));
3602 if (!*host_rt_dev_ptr) {
3603 unlock_user(argptr, arg, 0);
3604 return -TARGET_EFAULT;
3605 }
3606 } else {
3607 *host_rt_dev_ptr = 0;
3608 }
3609 field_types++;
3610 continue;
3611 }
3612 field_types = thunk_convert(buf_temp + dst_offsets[i],
3613 argptr + src_offsets[i],
3614 field_types, THUNK_HOST);
3615 }
3616 unlock_user(argptr, arg, 0);
3617
3618 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3619 if (*host_rt_dev_ptr != 0) {
3620 unlock_user((void *)*host_rt_dev_ptr,
3621 *target_rt_dev_ptr, 0);
3622 }
3623 return ret;
3624}
3625
blueswir19f106a72008-10-05 10:52:52 +00003626static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00003627#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003628 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3629#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3630 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00003631#include "ioctls.h"
3632 { 0, 0, },
3633};
3634
pbrook53a59602006-03-25 19:31:22 +00003635/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00003636/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003637static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00003638{
3639 const IOCTLEntry *ie;
3640 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00003641 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003642 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00003643 int target_size;
3644 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00003645
3646 ie = ioctl_entries;
3647 for(;;) {
3648 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003649 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003650 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003651 }
3652 if (ie->target_cmd == cmd)
3653 break;
3654 ie++;
3655 }
3656 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003657#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003658 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003659#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003660 if (ie->do_ioctl) {
3661 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3662 }
3663
bellard31e31b82003-02-18 22:55:36 +00003664 switch(arg_type[0]) {
3665 case TYPE_NULL:
3666 /* no argument */
3667 ret = get_errno(ioctl(fd, ie->host_cmd));
3668 break;
3669 case TYPE_PTRVOID:
3670 case TYPE_INT:
3671 /* int argment */
3672 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3673 break;
3674 case TYPE_PTR:
3675 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00003676 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00003677 switch(ie->access) {
3678 case IOC_R:
3679 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3680 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003681 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3682 if (!argptr)
3683 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003684 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3685 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003686 }
3687 break;
3688 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00003689 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3690 if (!argptr)
3691 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003692 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3693 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003694 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3695 break;
3696 default:
3697 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00003698 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3699 if (!argptr)
3700 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003701 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3702 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003703 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3704 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003705 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3706 if (!argptr)
3707 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003708 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3709 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003710 }
3711 break;
3712 }
3713 break;
3714 default:
j_mayer32407102007-09-26 23:01:49 +00003715 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3716 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003717 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003718 break;
3719 }
3720 return ret;
3721}
3722
blueswir1b39bc502008-10-05 10:51:10 +00003723static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003724 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3725 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3726 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3727 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3728 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3729 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3730 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3731 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3732 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3733 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3734 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3735 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3736 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3737 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3738 { 0, 0, 0, 0 }
3739};
3740
blueswir1b39bc502008-10-05 10:51:10 +00003741static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003742 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3743 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3744 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3745 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3746 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3747 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3748 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3749 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3750 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3751 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3752 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3753 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3754 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3755 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3756 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3757 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3758 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3759 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3760 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3761 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3762 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3763 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3764 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3765 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3766 { 0, 0, 0, 0 }
3767};
3768
blueswir1b39bc502008-10-05 10:51:10 +00003769static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003770 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3771 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3772 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3773 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3774 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3775 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3776 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3777 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3778 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3779 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3780 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3781 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3782 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3783 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3784 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3785 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3786 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3787 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3788 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3789 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3790 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3791 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3792 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3793 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3794 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3795 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3796 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3797 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3798 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3799 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3800 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3801 { 0, 0, 0, 0 }
3802};
3803
blueswir1b39bc502008-10-05 10:51:10 +00003804static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003805 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3806 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3807 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3808 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3809 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3810 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3811 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3812 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3813 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3814 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3815 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3816 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3817 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3818 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3819 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3820 { 0, 0, 0, 0 }
3821};
3822
3823static void target_to_host_termios (void *dst, const void *src)
3824{
3825 struct host_termios *host = dst;
3826 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003827
ths5fafdf22007-09-16 21:08:06 +00003828 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003829 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003830 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003831 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003832 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003833 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003834 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003835 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3836 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003837
Arnaud Patard44607122009-04-21 17:39:08 +03003838 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003839 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3840 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003841 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003842 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003843 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003844 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003845 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003846 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003847 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003848 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3849 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003850 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3851 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3852 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3853 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3854 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003855 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003856}
ths3b46e622007-09-17 08:09:54 +00003857
bellard31e31b82003-02-18 22:55:36 +00003858static void host_to_target_termios (void *dst, const void *src)
3859{
3860 struct target_termios *target = dst;
3861 const struct host_termios *host = src;
3862
ths5fafdf22007-09-16 21:08:06 +00003863 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003864 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003865 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003866 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003867 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003868 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003869 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003870 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3871 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003872
Arnaud Patard44607122009-04-21 17:39:08 +03003873 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003874 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3875 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3876 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3877 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3878 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3879 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3880 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3881 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3882 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3883 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3884 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3885 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3886 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3887 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3888 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3889 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3890 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3891}
3892
blueswir18e853dc2008-10-05 10:49:32 +00003893static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003894 .convert = { host_to_target_termios, target_to_host_termios },
3895 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3896 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3897};
3898
bellard5286db72003-06-05 00:57:30 +00003899static bitmask_transtbl mmap_flags_tbl[] = {
3900 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3901 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3902 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3903 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3904 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3905 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3906 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3907 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3908 { 0, 0, 0, 0 }
3909};
3910
bellard2ab83ea2003-06-15 19:56:46 +00003911#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003912
3913/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003914static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003915
bellard03acab62007-11-11 14:57:14 +00003916static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003917{
3918 int size;
pbrook53a59602006-03-25 19:31:22 +00003919 void *p;
bellard6dbad632003-03-16 18:05:05 +00003920
3921 if (!ldt_table)
3922 return 0;
3923 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3924 if (size > bytecount)
3925 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003926 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3927 if (!p)
bellard03acab62007-11-11 14:57:14 +00003928 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003929 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003930 memcpy(p, ldt_table, size);
3931 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003932 return size;
3933}
3934
3935/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003936static abi_long write_ldt(CPUX86State *env,
3937 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003938{
3939 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003940 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003941 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003942 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003943 uint32_t *lp, entry_1, entry_2;
3944
3945 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003946 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003947 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003948 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003949 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003950 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00003951 ldt_info.limit = tswap32(target_ldt_info->limit);
3952 ldt_info.flags = tswap32(target_ldt_info->flags);
3953 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003954
bellard6dbad632003-03-16 18:05:05 +00003955 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003956 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003957 seg_32bit = ldt_info.flags & 1;
3958 contents = (ldt_info.flags >> 1) & 3;
3959 read_exec_only = (ldt_info.flags >> 3) & 1;
3960 limit_in_pages = (ldt_info.flags >> 4) & 1;
3961 seg_not_present = (ldt_info.flags >> 5) & 1;
3962 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003963#ifdef TARGET_ABI32
3964 lm = 0;
3965#else
3966 lm = (ldt_info.flags >> 7) & 1;
3967#endif
bellard6dbad632003-03-16 18:05:05 +00003968 if (contents == 3) {
3969 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003970 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003971 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003972 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003973 }
3974 /* allocate the LDT */
3975 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003976 env->ldt.base = target_mmap(0,
3977 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3978 PROT_READ|PROT_WRITE,
3979 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3980 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003981 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003982 memset(g2h(env->ldt.base), 0,
3983 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003984 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003985 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003986 }
3987
3988 /* NOTE: same code as Linux kernel */
3989 /* Allow LDTs to be cleared by the user. */
3990 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3991 if (oldmode ||
3992 (contents == 0 &&
3993 read_exec_only == 1 &&
3994 seg_32bit == 0 &&
3995 limit_in_pages == 0 &&
3996 seg_not_present == 1 &&
3997 useable == 0 )) {
3998 entry_1 = 0;
3999 entry_2 = 0;
4000 goto install;
4001 }
4002 }
ths3b46e622007-09-17 08:09:54 +00004003
bellard6dbad632003-03-16 18:05:05 +00004004 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4005 (ldt_info.limit & 0x0ffff);
4006 entry_2 = (ldt_info.base_addr & 0xff000000) |
4007 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4008 (ldt_info.limit & 0xf0000) |
4009 ((read_exec_only ^ 1) << 9) |
4010 (contents << 10) |
4011 ((seg_not_present ^ 1) << 15) |
4012 (seg_32bit << 22) |
4013 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00004014 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00004015 0x7000;
4016 if (!oldmode)
4017 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00004018
bellard6dbad632003-03-16 18:05:05 +00004019 /* Install the new entry ... */
4020install:
4021 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4022 lp[0] = tswap32(entry_1);
4023 lp[1] = tswap32(entry_2);
4024 return 0;
4025}
4026
4027/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00004028static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4029 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00004030{
bellard03acab62007-11-11 14:57:14 +00004031 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00004032
bellard6dbad632003-03-16 18:05:05 +00004033 switch (func) {
4034 case 0:
4035 ret = read_ldt(ptr, bytecount);
4036 break;
4037 case 1:
4038 ret = write_ldt(env, ptr, bytecount, 1);
4039 break;
4040 case 0x11:
4041 ret = write_ldt(env, ptr, bytecount, 0);
4042 break;
bellard03acab62007-11-11 14:57:14 +00004043 default:
4044 ret = -TARGET_ENOSYS;
4045 break;
bellard6dbad632003-03-16 18:05:05 +00004046 }
4047 return ret;
4048}
bellard1b6b0292003-03-22 17:31:38 +00004049
blueswir14583f582008-08-24 10:35:55 +00004050#if defined(TARGET_I386) && defined(TARGET_ABI32)
Alexander Grafbc22eb42013-07-16 18:44:58 +01004051abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004052{
4053 uint64_t *gdt_table = g2h(env->gdt.base);
4054 struct target_modify_ldt_ldt_s ldt_info;
4055 struct target_modify_ldt_ldt_s *target_ldt_info;
4056 int seg_32bit, contents, read_exec_only, limit_in_pages;
4057 int seg_not_present, useable, lm;
4058 uint32_t *lp, entry_1, entry_2;
4059 int i;
4060
4061 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4062 if (!target_ldt_info)
4063 return -TARGET_EFAULT;
4064 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004065 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00004066 ldt_info.limit = tswap32(target_ldt_info->limit);
4067 ldt_info.flags = tswap32(target_ldt_info->flags);
4068 if (ldt_info.entry_number == -1) {
4069 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4070 if (gdt_table[i] == 0) {
4071 ldt_info.entry_number = i;
4072 target_ldt_info->entry_number = tswap32(i);
4073 break;
4074 }
4075 }
4076 }
4077 unlock_user_struct(target_ldt_info, ptr, 1);
4078
4079 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4080 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4081 return -TARGET_EINVAL;
4082 seg_32bit = ldt_info.flags & 1;
4083 contents = (ldt_info.flags >> 1) & 3;
4084 read_exec_only = (ldt_info.flags >> 3) & 1;
4085 limit_in_pages = (ldt_info.flags >> 4) & 1;
4086 seg_not_present = (ldt_info.flags >> 5) & 1;
4087 useable = (ldt_info.flags >> 6) & 1;
4088#ifdef TARGET_ABI32
4089 lm = 0;
4090#else
4091 lm = (ldt_info.flags >> 7) & 1;
4092#endif
4093
4094 if (contents == 3) {
4095 if (seg_not_present == 0)
4096 return -TARGET_EINVAL;
4097 }
4098
4099 /* NOTE: same code as Linux kernel */
4100 /* Allow LDTs to be cleared by the user. */
4101 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4102 if ((contents == 0 &&
4103 read_exec_only == 1 &&
4104 seg_32bit == 0 &&
4105 limit_in_pages == 0 &&
4106 seg_not_present == 1 &&
4107 useable == 0 )) {
4108 entry_1 = 0;
4109 entry_2 = 0;
4110 goto install;
4111 }
4112 }
4113
4114 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4115 (ldt_info.limit & 0x0ffff);
4116 entry_2 = (ldt_info.base_addr & 0xff000000) |
4117 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4118 (ldt_info.limit & 0xf0000) |
4119 ((read_exec_only ^ 1) << 9) |
4120 (contents << 10) |
4121 ((seg_not_present ^ 1) << 15) |
4122 (seg_32bit << 22) |
4123 (limit_in_pages << 23) |
4124 (useable << 20) |
4125 (lm << 21) |
4126 0x7000;
4127
4128 /* Install the new entry ... */
4129install:
4130 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4131 lp[0] = tswap32(entry_1);
4132 lp[1] = tswap32(entry_2);
4133 return 0;
4134}
4135
blueswir18fcd3692008-08-17 20:26:25 +00004136static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004137{
4138 struct target_modify_ldt_ldt_s *target_ldt_info;
4139 uint64_t *gdt_table = g2h(env->gdt.base);
4140 uint32_t base_addr, limit, flags;
4141 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4142 int seg_not_present, useable, lm;
4143 uint32_t *lp, entry_1, entry_2;
4144
4145 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4146 if (!target_ldt_info)
4147 return -TARGET_EFAULT;
4148 idx = tswap32(target_ldt_info->entry_number);
4149 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4150 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4151 unlock_user_struct(target_ldt_info, ptr, 1);
4152 return -TARGET_EINVAL;
4153 }
4154 lp = (uint32_t *)(gdt_table + idx);
4155 entry_1 = tswap32(lp[0]);
4156 entry_2 = tswap32(lp[1]);
4157
4158 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4159 contents = (entry_2 >> 10) & 3;
4160 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4161 seg_32bit = (entry_2 >> 22) & 1;
4162 limit_in_pages = (entry_2 >> 23) & 1;
4163 useable = (entry_2 >> 20) & 1;
4164#ifdef TARGET_ABI32
4165 lm = 0;
4166#else
4167 lm = (entry_2 >> 21) & 1;
4168#endif
4169 flags = (seg_32bit << 0) | (contents << 1) |
4170 (read_exec_only << 3) | (limit_in_pages << 4) |
4171 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4172 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4173 base_addr = (entry_1 >> 16) |
4174 (entry_2 & 0xff000000) |
4175 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004176 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00004177 target_ldt_info->limit = tswap32(limit);
4178 target_ldt_info->flags = tswap32(flags);
4179 unlock_user_struct(target_ldt_info, ptr, 1);
4180 return 0;
4181}
blueswir14583f582008-08-24 10:35:55 +00004182#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00004183
bellardd2fd1af2007-11-14 18:08:56 +00004184#ifndef TARGET_ABI32
Peter Maydell2667e712013-07-16 18:44:59 +01004185abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00004186{
Juan Quintela1add8692011-06-16 17:37:09 +01004187 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00004188 abi_ulong val;
4189 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01004190
bellardd2fd1af2007-11-14 18:08:56 +00004191 switch(code) {
4192 case TARGET_ARCH_SET_GS:
4193 case TARGET_ARCH_SET_FS:
4194 if (code == TARGET_ARCH_SET_GS)
4195 idx = R_GS;
4196 else
4197 idx = R_FS;
4198 cpu_x86_load_seg(env, idx, 0);
4199 env->segs[idx].base = addr;
4200 break;
4201 case TARGET_ARCH_GET_GS:
4202 case TARGET_ARCH_GET_FS:
4203 if (code == TARGET_ARCH_GET_GS)
4204 idx = R_GS;
4205 else
4206 idx = R_FS;
4207 val = env->segs[idx].base;
4208 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01004209 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00004210 break;
4211 default:
4212 ret = -TARGET_EINVAL;
4213 break;
4214 }
Juan Quintela1add8692011-06-16 17:37:09 +01004215 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00004216}
4217#endif
4218
bellard2ab83ea2003-06-15 19:56:46 +00004219#endif /* defined(TARGET_I386) */
4220
Riku Voipio05098a92011-03-04 15:27:29 +02004221#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00004222
pbrookd865bab2008-06-07 22:12:17 +00004223
4224static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4225typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01004226 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00004227 pthread_mutex_t mutex;
4228 pthread_cond_t cond;
4229 pthread_t thread;
4230 uint32_t tid;
4231 abi_ulong child_tidptr;
4232 abi_ulong parent_tidptr;
4233 sigset_t sigmask;
4234} new_thread_info;
4235
4236static void *clone_func(void *arg)
4237{
4238 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004239 CPUArchState *env;
Andreas Färber0d342822012-12-17 07:12:13 +01004240 CPUState *cpu;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004241 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00004242
4243 env = info->env;
Andreas Färber0d342822012-12-17 07:12:13 +01004244 cpu = ENV_GET_CPU(env);
Andreas Färbera2247f82013-06-09 19:47:04 +02004245 thread_cpu = cpu;
4246 ts = (TaskState *)env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004247 info->tid = gettid();
Andreas Färber0d342822012-12-17 07:12:13 +01004248 cpu->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004249 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00004250 if (info->child_tidptr)
4251 put_user_u32(info->tid, info->child_tidptr);
4252 if (info->parent_tidptr)
4253 put_user_u32(info->tid, info->parent_tidptr);
4254 /* Enable signals. */
4255 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4256 /* Signal to the parent that we're ready. */
4257 pthread_mutex_lock(&info->mutex);
4258 pthread_cond_broadcast(&info->cond);
4259 pthread_mutex_unlock(&info->mutex);
4260 /* Wait until the parent has finshed initializing the tls state. */
4261 pthread_mutex_lock(&clone_lock);
4262 pthread_mutex_unlock(&clone_lock);
4263 cpu_loop(env);
4264 /* never exits */
4265 return NULL;
4266}
bellard1b6b0292003-03-22 17:31:38 +00004267
ths0da46a62007-10-20 20:23:07 +00004268/* do_fork() Must return host values and target errnos (unlike most
4269 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01004270static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00004271 abi_ulong parent_tidptr, target_ulong newtls,
4272 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00004273{
4274 int ret;
bellard5cd43932003-03-29 16:54:36 +00004275 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004276 CPUArchState *new_env;
pbrookd865bab2008-06-07 22:12:17 +00004277 unsigned int nptl_flags;
4278 sigset_t sigmask;
ths3b46e622007-09-17 08:09:54 +00004279
balrog436d1242008-09-21 02:39:45 +00004280 /* Emulate vfork() with fork() */
4281 if (flags & CLONE_VFORK)
4282 flags &= ~(CLONE_VFORK | CLONE_VM);
4283
bellard1b6b0292003-03-22 17:31:38 +00004284 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004285 TaskState *parent_ts = (TaskState *)env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004286 new_thread_info info;
4287 pthread_attr_t attr;
Peter Maydell24cb36a2013-07-16 18:45:00 +01004288
Anthony Liguori7267c092011-08-20 22:09:37 -05004289 ts = g_malloc0(sizeof(TaskState));
pbrook624f7972008-05-31 16:11:38 +00004290 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00004291 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00004292 new_env = cpu_copy(env);
pbrook6e68e072008-05-30 17:22:15 +00004293 /* Init regs that differ from the parent. */
4294 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00004295 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004296 ts->bprm = parent_ts->bprm;
4297 ts->info = parent_ts->info;
pbrookd865bab2008-06-07 22:12:17 +00004298 nptl_flags = flags;
4299 flags &= ~CLONE_NPTL_FLAGS2;
4300
pbrookc2764712009-03-07 15:24:59 +00004301 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4302 ts->child_tidptr = child_tidptr;
4303 }
4304
pbrookd865bab2008-06-07 22:12:17 +00004305 if (nptl_flags & CLONE_SETTLS)
4306 cpu_set_tls (new_env, newtls);
4307
4308 /* Grab a mutex so that thread setup appears atomic. */
4309 pthread_mutex_lock(&clone_lock);
4310
4311 memset(&info, 0, sizeof(info));
4312 pthread_mutex_init(&info.mutex, NULL);
4313 pthread_mutex_lock(&info.mutex);
4314 pthread_cond_init(&info.cond, NULL);
4315 info.env = new_env;
4316 if (nptl_flags & CLONE_CHILD_SETTID)
4317 info.child_tidptr = child_tidptr;
4318 if (nptl_flags & CLONE_PARENT_SETTID)
4319 info.parent_tidptr = parent_tidptr;
4320
4321 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004322 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4323 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00004324 /* It is not safe to deliver signals until the child has finished
4325 initializing, so temporarily block all signals. */
4326 sigfillset(&sigmask);
4327 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4328
4329 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00004330 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00004331
4332 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4333 pthread_attr_destroy(&attr);
4334 if (ret == 0) {
4335 /* Wait for the child to initialize. */
4336 pthread_cond_wait(&info.cond, &info.mutex);
4337 ret = info.tid;
4338 if (flags & CLONE_PARENT_SETTID)
4339 put_user_u32(ret, parent_tidptr);
4340 } else {
4341 ret = -1;
4342 }
4343 pthread_mutex_unlock(&info.mutex);
4344 pthread_cond_destroy(&info.cond);
4345 pthread_mutex_destroy(&info.mutex);
4346 pthread_mutex_unlock(&clone_lock);
bellard1b6b0292003-03-22 17:31:38 +00004347 } else {
4348 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00004349 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00004350 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00004351 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00004352 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00004353 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00004354 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00004355 cpu_clone_regs(env, newsp);
4356 fork_end(1);
aurel322b1319c2008-12-18 22:44:04 +00004357 /* There is a race condition here. The parent process could
4358 theoretically read the TID in the child process before the child
4359 tid is set. This would require using either ptrace
4360 (not implemented) or having *_tidptr to point at a shared memory
4361 mapping. We can't repeat the spinlock hack used above because
4362 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00004363 if (flags & CLONE_CHILD_SETTID)
4364 put_user_u32(gettid(), child_tidptr);
4365 if (flags & CLONE_PARENT_SETTID)
4366 put_user_u32(gettid(), parent_tidptr);
4367 ts = (TaskState *)env->opaque;
4368 if (flags & CLONE_SETTLS)
4369 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00004370 if (flags & CLONE_CHILD_CLEARTID)
4371 ts->child_tidptr = child_tidptr;
pbrookd865bab2008-06-07 22:12:17 +00004372 } else {
4373 fork_end(0);
4374 }
bellard1b6b0292003-03-22 17:31:38 +00004375 }
4376 return ret;
4377}
4378
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004379/* warning : doesn't handle linux specific flags... */
4380static int target_to_host_fcntl_cmd(int cmd)
4381{
4382 switch(cmd) {
4383 case TARGET_F_DUPFD:
4384 case TARGET_F_GETFD:
4385 case TARGET_F_SETFD:
4386 case TARGET_F_GETFL:
4387 case TARGET_F_SETFL:
4388 return cmd;
4389 case TARGET_F_GETLK:
4390 return F_GETLK;
4391 case TARGET_F_SETLK:
4392 return F_SETLK;
4393 case TARGET_F_SETLKW:
4394 return F_SETLKW;
4395 case TARGET_F_GETOWN:
4396 return F_GETOWN;
4397 case TARGET_F_SETOWN:
4398 return F_SETOWN;
4399 case TARGET_F_GETSIG:
4400 return F_GETSIG;
4401 case TARGET_F_SETSIG:
4402 return F_SETSIG;
4403#if TARGET_ABI_BITS == 32
4404 case TARGET_F_GETLK64:
4405 return F_GETLK64;
4406 case TARGET_F_SETLK64:
4407 return F_SETLK64;
4408 case TARGET_F_SETLKW64:
4409 return F_SETLKW64;
4410#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004411 case TARGET_F_SETLEASE:
4412 return F_SETLEASE;
4413 case TARGET_F_GETLEASE:
4414 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004415#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004416 case TARGET_F_DUPFD_CLOEXEC:
4417 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004418#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004419 case TARGET_F_NOTIFY:
4420 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004421 default:
4422 return -TARGET_EINVAL;
4423 }
4424 return -TARGET_EINVAL;
4425}
4426
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004427#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4428static const bitmask_transtbl flock_tbl[] = {
4429 TRANSTBL_CONVERT(F_RDLCK),
4430 TRANSTBL_CONVERT(F_WRLCK),
4431 TRANSTBL_CONVERT(F_UNLCK),
4432 TRANSTBL_CONVERT(F_EXLCK),
4433 TRANSTBL_CONVERT(F_SHLCK),
4434 { 0, 0, 0, 0 }
4435};
4436
blueswir1992f48a2007-10-14 16:27:31 +00004437static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004438{
4439 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004440 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004441 struct flock64 fl64;
4442 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00004443 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004444 int host_cmd = target_to_host_fcntl_cmd(cmd);
4445
4446 if (host_cmd == -TARGET_EINVAL)
4447 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004448
bellard7775e9e2003-05-14 22:46:48 +00004449 switch(cmd) {
4450 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004451 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4452 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004453 fl.l_type =
4454 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
ths58134272007-03-31 18:59:32 +00004455 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004456 fl.l_start = tswapal(target_fl->l_start);
4457 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004458 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004459 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004460 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004461 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004462 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4463 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004464 target_fl->l_type =
4465 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004466 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004467 target_fl->l_start = tswapal(fl.l_start);
4468 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004469 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004470 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004471 }
4472 break;
ths3b46e622007-09-17 08:09:54 +00004473
bellard7775e9e2003-05-14 22:46:48 +00004474 case TARGET_F_SETLK:
4475 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004476 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4477 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004478 fl.l_type =
4479 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004480 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004481 fl.l_start = tswapal(target_fl->l_start);
4482 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004483 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004484 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004485 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004486 break;
ths3b46e622007-09-17 08:09:54 +00004487
bellard7775e9e2003-05-14 22:46:48 +00004488 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004489 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4490 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004491 fl64.l_type =
4492 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths58134272007-03-31 18:59:32 +00004493 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004494 fl64.l_start = tswap64(target_fl64->l_start);
4495 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004496 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004497 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004498 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004499 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004500 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4501 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004502 target_fl64->l_type =
4503 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004504 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004505 target_fl64->l_start = tswap64(fl64.l_start);
4506 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004507 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004508 unlock_user_struct(target_fl64, arg, 1);
4509 }
bellard9ee1fa22007-11-11 15:11:19 +00004510 break;
bellard7775e9e2003-05-14 22:46:48 +00004511 case TARGET_F_SETLK64:
4512 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004513 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4514 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004515 fl64.l_type =
4516 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004517 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004518 fl64.l_start = tswap64(target_fl64->l_start);
4519 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004520 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004521 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004522 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004523 break;
4524
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004525 case TARGET_F_GETFL:
4526 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004527 if (ret >= 0) {
4528 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4529 }
bellardffa65c32004-01-04 23:57:22 +00004530 break;
4531
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004532 case TARGET_F_SETFL:
4533 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4534 break;
4535
4536 case TARGET_F_SETOWN:
4537 case TARGET_F_GETOWN:
4538 case TARGET_F_SETSIG:
4539 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004540 case TARGET_F_SETLEASE:
4541 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004542 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00004543 break;
4544
bellard7775e9e2003-05-14 22:46:48 +00004545 default:
bellard9ee1fa22007-11-11 15:11:19 +00004546 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00004547 break;
4548 }
4549 return ret;
4550}
4551
bellard67867302003-11-23 17:05:30 +00004552#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00004553
bellard67867302003-11-23 17:05:30 +00004554static inline int high2lowuid(int uid)
4555{
4556 if (uid > 65535)
4557 return 65534;
4558 else
4559 return uid;
4560}
4561
4562static inline int high2lowgid(int gid)
4563{
4564 if (gid > 65535)
4565 return 65534;
4566 else
4567 return gid;
4568}
4569
4570static inline int low2highuid(int uid)
4571{
4572 if ((int16_t)uid == -1)
4573 return -1;
4574 else
4575 return uid;
4576}
4577
4578static inline int low2highgid(int gid)
4579{
4580 if ((int16_t)gid == -1)
4581 return -1;
4582 else
4583 return gid;
4584}
Riku Voipio0c866a72011-04-18 15:23:06 +03004585static inline int tswapid(int id)
4586{
4587 return tswap16(id);
4588}
Peter Maydell76ca3102014-03-02 19:36:41 +00004589
4590#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
4591
Riku Voipio0c866a72011-04-18 15:23:06 +03004592#else /* !USE_UID16 */
4593static inline int high2lowuid(int uid)
4594{
4595 return uid;
4596}
4597static inline int high2lowgid(int gid)
4598{
4599 return gid;
4600}
4601static inline int low2highuid(int uid)
4602{
4603 return uid;
4604}
4605static inline int low2highgid(int gid)
4606{
4607 return gid;
4608}
4609static inline int tswapid(int id)
4610{
4611 return tswap32(id);
4612}
Peter Maydell76ca3102014-03-02 19:36:41 +00004613
4614#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
4615
bellard67867302003-11-23 17:05:30 +00004616#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004617
bellard31e31b82003-02-18 22:55:36 +00004618void syscall_init(void)
4619{
bellard2ab83ea2003-06-15 19:56:46 +00004620 IOCTLEntry *ie;
4621 const argtype *arg_type;
4622 int size;
thsb92c47c2007-11-01 00:07:38 +00004623 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004624
Blue Swirl001faf32009-05-13 17:53:17 +00004625#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004626#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004627#include "syscall_types.h"
4628#undef STRUCT
4629#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00004630
Peter Maydelldd6e9572012-07-23 08:07:22 +00004631 /* Build target_to_host_errno_table[] table from
4632 * host_to_target_errno_table[]. */
4633 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4634 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4635 }
4636
bellard2ab83ea2003-06-15 19:56:46 +00004637 /* we patch the ioctl size if necessary. We rely on the fact that
4638 no ioctl has all the bits at '1' in the size field */
4639 ie = ioctl_entries;
4640 while (ie->target_cmd != 0) {
4641 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4642 TARGET_IOC_SIZEMASK) {
4643 arg_type = ie->arg_type;
4644 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00004645 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00004646 ie->target_cmd);
4647 exit(1);
4648 }
4649 arg_type++;
4650 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00004651 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00004652 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4653 (size << TARGET_IOC_SIZESHIFT);
4654 }
thsb92c47c2007-11-01 00:07:38 +00004655
bellard2ab83ea2003-06-15 19:56:46 +00004656 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00004657#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4658 (defined(__x86_64__) && defined(TARGET_X86_64))
4659 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4660 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4661 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00004662 }
4663#endif
4664 ie++;
4665 }
bellard31e31b82003-02-18 22:55:36 +00004666}
bellardc573ff62004-01-04 15:51:36 +00004667
blueswir1992f48a2007-10-14 16:27:31 +00004668#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004669static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4670{
thsaf325d32008-06-10 15:29:15 +00004671#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004672 return ((uint64_t)word0 << 32) | word1;
4673#else
4674 return ((uint64_t)word1 << 32) | word0;
4675#endif
4676}
blueswir1992f48a2007-10-14 16:27:31 +00004677#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004678static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4679{
4680 return word0;
4681}
blueswir1992f48a2007-10-14 16:27:31 +00004682#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004683
4684#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004685static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4686 abi_long arg2,
4687 abi_long arg3,
4688 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004689{
Riku Voipio48e515d2011-07-12 15:40:51 +03004690 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004691 arg2 = arg3;
4692 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004693 }
pbrookce4defa2006-02-09 16:49:55 +00004694 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4695}
4696#endif
4697
4698#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004699static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4700 abi_long arg2,
4701 abi_long arg3,
4702 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004703{
Riku Voipio48e515d2011-07-12 15:40:51 +03004704 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004705 arg2 = arg3;
4706 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004707 }
pbrookce4defa2006-02-09 16:49:55 +00004708 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4709}
4710#endif
4711
bellard579a97f2007-11-11 14:26:47 +00004712static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4713 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004714{
4715 struct target_timespec *target_ts;
4716
bellard579a97f2007-11-11 14:26:47 +00004717 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4718 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004719 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4720 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004721 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004722 return 0;
pbrook53a59602006-03-25 19:31:22 +00004723}
4724
bellard579a97f2007-11-11 14:26:47 +00004725static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4726 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004727{
4728 struct target_timespec *target_ts;
4729
bellard579a97f2007-11-11 14:26:47 +00004730 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4731 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004732 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4733 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004734 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004735 return 0;
pbrook53a59602006-03-25 19:31:22 +00004736}
4737
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11004738static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
4739 abi_ulong target_addr)
4740{
4741 struct target_itimerspec *target_itspec;
4742
4743 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
4744 return -TARGET_EFAULT;
4745 }
4746
4747 host_itspec->it_interval.tv_sec =
4748 tswapal(target_itspec->it_interval.tv_sec);
4749 host_itspec->it_interval.tv_nsec =
4750 tswapal(target_itspec->it_interval.tv_nsec);
4751 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
4752 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
4753
4754 unlock_user_struct(target_itspec, target_addr, 1);
4755 return 0;
4756}
4757
4758static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
4759 struct itimerspec *host_its)
4760{
4761 struct target_itimerspec *target_itspec;
4762
4763 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
4764 return -TARGET_EFAULT;
4765 }
4766
4767 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
4768 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
4769
4770 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
4771 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
4772
4773 unlock_user_struct(target_itspec, target_addr, 0);
4774 return 0;
4775}
4776
aurel329d33b762009-04-08 23:07:05 +00004777#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004778static inline abi_long host_to_target_stat64(void *cpu_env,
4779 abi_ulong target_addr,
4780 struct stat *host_st)
4781{
Alexander Graf09701192013-09-03 20:12:15 +01004782#if defined(TARGET_ARM) && defined(TARGET_ABI32)
balrog6a24a772008-09-20 02:23:36 +00004783 if (((CPUARMState *)cpu_env)->eabi) {
4784 struct target_eabi_stat64 *target_st;
4785
4786 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4787 return -TARGET_EFAULT;
4788 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4789 __put_user(host_st->st_dev, &target_st->st_dev);
4790 __put_user(host_st->st_ino, &target_st->st_ino);
4791#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4792 __put_user(host_st->st_ino, &target_st->__st_ino);
4793#endif
4794 __put_user(host_st->st_mode, &target_st->st_mode);
4795 __put_user(host_st->st_nlink, &target_st->st_nlink);
4796 __put_user(host_st->st_uid, &target_st->st_uid);
4797 __put_user(host_st->st_gid, &target_st->st_gid);
4798 __put_user(host_st->st_rdev, &target_st->st_rdev);
4799 __put_user(host_st->st_size, &target_st->st_size);
4800 __put_user(host_st->st_blksize, &target_st->st_blksize);
4801 __put_user(host_st->st_blocks, &target_st->st_blocks);
4802 __put_user(host_st->st_atime, &target_st->target_st_atime);
4803 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4804 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4805 unlock_user_struct(target_st, target_addr, 1);
4806 } else
4807#endif
4808 {
Stefan Weil20d155b2013-10-30 22:52:24 +01004809#if defined(TARGET_HAS_STRUCT_STAT64)
balrog6a24a772008-09-20 02:23:36 +00004810 struct target_stat64 *target_st;
Stefan Weil20d155b2013-10-30 22:52:24 +01004811#else
4812 struct target_stat *target_st;
aurel329d33b762009-04-08 23:07:05 +00004813#endif
balrog6a24a772008-09-20 02:23:36 +00004814
4815 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4816 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004817 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004818 __put_user(host_st->st_dev, &target_st->st_dev);
4819 __put_user(host_st->st_ino, &target_st->st_ino);
4820#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4821 __put_user(host_st->st_ino, &target_st->__st_ino);
4822#endif
4823 __put_user(host_st->st_mode, &target_st->st_mode);
4824 __put_user(host_st->st_nlink, &target_st->st_nlink);
4825 __put_user(host_st->st_uid, &target_st->st_uid);
4826 __put_user(host_st->st_gid, &target_st->st_gid);
4827 __put_user(host_st->st_rdev, &target_st->st_rdev);
4828 /* XXX: better use of kernel struct */
4829 __put_user(host_st->st_size, &target_st->st_size);
4830 __put_user(host_st->st_blksize, &target_st->st_blksize);
4831 __put_user(host_st->st_blocks, &target_st->st_blocks);
4832 __put_user(host_st->st_atime, &target_st->target_st_atime);
4833 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4834 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4835 unlock_user_struct(target_st, target_addr, 1);
4836 }
4837
4838 return 0;
4839}
4840#endif
4841
pbrookbd0c5662008-05-29 14:34:11 +00004842/* ??? Using host futex calls even when target atomic operations
4843 are not really atomic probably breaks things. However implementing
4844 futexes locally would make futexes shared between multiple processes
4845 tricky. However they're probably useless because guest atomic
4846 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004847static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4848 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004849{
4850 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004851 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004852
4853 /* ??? We assume FUTEX_* constants are the same on both host
4854 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004855#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004856 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004857#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004858 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004859#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004860 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004861 case FUTEX_WAIT:
John Rigbycce246e2013-02-23 16:14:07 -07004862 case FUTEX_WAIT_BITSET:
pbrookbd0c5662008-05-29 14:34:11 +00004863 if (timeout) {
4864 pts = &ts;
4865 target_to_host_timespec(pts, timeout);
4866 } else {
4867 pts = NULL;
4868 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004869 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
John Rigbycce246e2013-02-23 16:14:07 -07004870 pts, NULL, val3));
pbrookbd0c5662008-05-29 14:34:11 +00004871 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004872 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004873 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004874 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004875 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004876 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004877 case FUTEX_WAKE_OP:
4878 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4879 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4880 But the prototype takes a `struct timespec *'; insert casts
4881 to satisfy the compiler. We do not need to tswap TIMEOUT
4882 since it's not compared to guest memory. */
4883 pts = (struct timespec *)(uintptr_t) timeout;
4884 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4885 g2h(uaddr2),
4886 (base_op == FUTEX_CMP_REQUEUE
4887 ? tswap32(val3)
4888 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004889 default:
4890 return -TARGET_ENOSYS;
4891 }
4892}
pbrookbd0c5662008-05-29 14:34:11 +00004893
pbrook1d9d8b52009-04-16 15:17:02 +00004894/* Map host to target signal numbers for the wait family of syscalls.
4895 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07004896int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00004897{
4898 if (WIFSIGNALED(status)) {
4899 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4900 }
4901 if (WIFSTOPPED(status)) {
4902 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4903 | (status & 0xff);
4904 }
4905 return status;
4906}
4907
Peter Maydell4a24a752013-09-03 20:12:20 +01004908static int relstr_to_int(const char *s)
pbrooka745ec62008-05-06 15:36:17 +00004909{
Peter Maydell4a24a752013-09-03 20:12:20 +01004910 /* Convert a uname release string like "2.6.18" to an integer
4911 * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
4912 */
pbrooka745ec62008-05-06 15:36:17 +00004913 int i, n, tmp;
Peter Maydell4a24a752013-09-03 20:12:20 +01004914
pbrooka745ec62008-05-06 15:36:17 +00004915 tmp = 0;
4916 for (i = 0; i < 3; i++) {
4917 n = 0;
4918 while (*s >= '0' && *s <= '9') {
4919 n *= 10;
4920 n += *s - '0';
4921 s++;
4922 }
4923 tmp = (tmp << 8) + n;
Peter Maydell4a24a752013-09-03 20:12:20 +01004924 if (*s == '.') {
pbrooka745ec62008-05-06 15:36:17 +00004925 s++;
Peter Maydell4a24a752013-09-03 20:12:20 +01004926 }
pbrooka745ec62008-05-06 15:36:17 +00004927 }
Peter Maydell4a24a752013-09-03 20:12:20 +01004928 return tmp;
4929}
4930
4931int get_osversion(void)
4932{
4933 static int osversion;
4934 struct new_utsname buf;
4935 const char *s;
4936
4937 if (osversion)
4938 return osversion;
4939 if (qemu_uname_release && *qemu_uname_release) {
4940 s = qemu_uname_release;
4941 } else {
4942 if (sys_uname(&buf))
4943 return 0;
4944 s = buf.release;
4945 }
4946 osversion = relstr_to_int(s);
pbrooka745ec62008-05-06 15:36:17 +00004947 return osversion;
4948}
4949
Peter Maydell4a24a752013-09-03 20:12:20 +01004950void init_qemu_uname_release(void)
4951{
4952 /* Initialize qemu_uname_release for later use.
4953 * If the host kernel is too old and the user hasn't asked for
4954 * a specific fake version number, we might want to fake a minimum
4955 * target kernel version.
4956 */
4957#ifdef UNAME_MINIMUM_RELEASE
4958 struct new_utsname buf;
4959
4960 if (qemu_uname_release && *qemu_uname_release) {
4961 return;
4962 }
4963
4964 if (sys_uname(&buf)) {
4965 return;
4966 }
4967
4968 if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
4969 qemu_uname_release = UNAME_MINIMUM_RELEASE;
4970 }
4971#endif
4972}
Alexander Graf36c08d42011-11-02 20:23:24 +01004973
4974static int open_self_maps(void *cpu_env, int fd)
4975{
Alexander Graf1a49ef22012-05-01 16:30:28 +01004976#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Andreas Färber9349b4f2012-03-14 01:38:32 +01004977 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf1a49ef22012-05-01 16:30:28 +01004978#endif
4979 FILE *fp;
4980 char *line = NULL;
4981 size_t len = 0;
4982 ssize_t read;
Alexander Graf36c08d42011-11-02 20:23:24 +01004983
Alexander Graf1a49ef22012-05-01 16:30:28 +01004984 fp = fopen("/proc/self/maps", "r");
4985 if (fp == NULL) {
4986 return -EACCES;
4987 }
4988
4989 while ((read = getline(&line, &len, fp)) != -1) {
4990 int fields, dev_maj, dev_min, inode;
4991 uint64_t min, max, offset;
4992 char flag_r, flag_w, flag_x, flag_p;
4993 char path[512] = "";
4994 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
4995 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
4996 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
4997
4998 if ((fields < 10) || (fields > 11)) {
4999 continue;
5000 }
5001 if (!strncmp(path, "[stack]", 7)) {
5002 continue;
5003 }
5004 if (h2g_valid(min) && h2g_valid(max)) {
5005 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
Christophe Lyone24fed42013-04-02 14:03:38 +02005006 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
Alexander Graf1a49ef22012-05-01 16:30:28 +01005007 h2g(min), h2g(max), flag_r, flag_w,
5008 flag_x, flag_p, offset, dev_maj, dev_min, inode,
Christophe Lyone24fed42013-04-02 14:03:38 +02005009 path[0] ? " " : "", path);
Alexander Graf1a49ef22012-05-01 16:30:28 +01005010 }
5011 }
5012
5013 free(line);
5014 fclose(fp);
5015
5016#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Alexander Graf36c08d42011-11-02 20:23:24 +01005017 dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
5018 (unsigned long long)ts->info->stack_limit,
Alexander Graf1bdd7c72012-05-30 14:45:21 +02005019 (unsigned long long)(ts->info->start_stack +
5020 (TARGET_PAGE_SIZE - 1)) & TARGET_PAGE_MASK,
Alexander Graf1a49ef22012-05-01 16:30:28 +01005021 (unsigned long long)0);
5022#endif
Alexander Graf36c08d42011-11-02 20:23:24 +01005023
5024 return 0;
5025}
5026
Alexander Graf480b8e72011-11-02 20:23:25 +01005027static int open_self_stat(void *cpu_env, int fd)
5028{
Andreas Färber9349b4f2012-03-14 01:38:32 +01005029 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf480b8e72011-11-02 20:23:25 +01005030 abi_ulong start_stack = ts->info->start_stack;
5031 int i;
5032
5033 for (i = 0; i < 44; i++) {
5034 char buf[128];
5035 int len;
5036 uint64_t val = 0;
5037
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005038 if (i == 0) {
5039 /* pid */
5040 val = getpid();
5041 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5042 } else if (i == 1) {
5043 /* app name */
5044 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5045 } else if (i == 27) {
5046 /* stack bottom */
5047 val = start_stack;
5048 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5049 } else {
5050 /* for the rest, there is MasterCard */
5051 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
Alexander Graf480b8e72011-11-02 20:23:25 +01005052 }
Fabio Erculianie0e65be2012-01-03 09:38:34 +00005053
Alexander Graf480b8e72011-11-02 20:23:25 +01005054 len = strlen(buf);
5055 if (write(fd, buf, len) != len) {
5056 return -1;
5057 }
5058 }
5059
5060 return 0;
5061}
5062
Alexander Graf257450e2011-11-02 20:23:26 +01005063static int open_self_auxv(void *cpu_env, int fd)
5064{
Andreas Färber9349b4f2012-03-14 01:38:32 +01005065 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01005066 abi_ulong auxv = ts->info->saved_auxv;
5067 abi_ulong len = ts->info->auxv_len;
5068 char *ptr;
5069
5070 /*
5071 * Auxiliary vector is stored in target process stack.
5072 * read in whole auxv vector and copy it to file
5073 */
5074 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5075 if (ptr != NULL) {
5076 while (len > 0) {
5077 ssize_t r;
5078 r = write(fd, ptr, len);
5079 if (r <= 0) {
5080 break;
5081 }
5082 len -= r;
5083 ptr += r;
5084 }
5085 lseek(fd, 0, SEEK_SET);
5086 unlock_user(ptr, auxv, len);
5087 }
5088
5089 return 0;
5090}
5091
Andreas Schwab463d8e72013-07-02 14:04:12 +01005092static int is_proc_myself(const char *filename, const char *entry)
5093{
5094 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
5095 filename += strlen("/proc/");
5096 if (!strncmp(filename, "self/", strlen("self/"))) {
5097 filename += strlen("self/");
5098 } else if (*filename >= '1' && *filename <= '9') {
5099 char myself[80];
5100 snprintf(myself, sizeof(myself), "%d/", getpid());
5101 if (!strncmp(filename, myself, strlen(myself))) {
5102 filename += strlen(myself);
5103 } else {
5104 return 0;
5105 }
5106 } else {
5107 return 0;
5108 }
5109 if (!strcmp(filename, entry)) {
5110 return 1;
5111 }
5112 }
5113 return 0;
5114}
5115
Laurent Vivierde6b9932013-08-30 01:46:40 +02005116#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5117static int is_proc(const char *filename, const char *entry)
5118{
5119 return strcmp(filename, entry) == 0;
5120}
5121
5122static int open_net_route(void *cpu_env, int fd)
5123{
5124 FILE *fp;
5125 char *line = NULL;
5126 size_t len = 0;
5127 ssize_t read;
5128
5129 fp = fopen("/proc/net/route", "r");
5130 if (fp == NULL) {
5131 return -EACCES;
5132 }
5133
5134 /* read header */
5135
5136 read = getline(&line, &len, fp);
5137 dprintf(fd, "%s", line);
5138
5139 /* read routes */
5140
5141 while ((read = getline(&line, &len, fp)) != -1) {
5142 char iface[16];
5143 uint32_t dest, gw, mask;
5144 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
5145 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5146 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
5147 &mask, &mtu, &window, &irtt);
5148 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5149 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
5150 metric, tswap32(mask), mtu, window, irtt);
5151 }
5152
5153 free(line);
5154 fclose(fp);
5155
5156 return 0;
5157}
5158#endif
5159
Alexander Graf3be14d02011-11-02 20:23:23 +01005160static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
5161{
5162 struct fake_open {
5163 const char *filename;
5164 int (*fill)(void *cpu_env, int fd);
Laurent Vivierde6b9932013-08-30 01:46:40 +02005165 int (*cmp)(const char *s1, const char *s2);
Alexander Graf3be14d02011-11-02 20:23:23 +01005166 };
5167 const struct fake_open *fake_open;
5168 static const struct fake_open fakes[] = {
Laurent Vivierde6b9932013-08-30 01:46:40 +02005169 { "maps", open_self_maps, is_proc_myself },
5170 { "stat", open_self_stat, is_proc_myself },
5171 { "auxv", open_self_auxv, is_proc_myself },
5172#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5173 { "/proc/net/route", open_net_route, is_proc },
5174#endif
5175 { NULL, NULL, NULL }
Alexander Graf3be14d02011-11-02 20:23:23 +01005176 };
5177
5178 for (fake_open = fakes; fake_open->filename; fake_open++) {
Laurent Vivierde6b9932013-08-30 01:46:40 +02005179 if (fake_open->cmp(pathname, fake_open->filename)) {
Alexander Graf3be14d02011-11-02 20:23:23 +01005180 break;
5181 }
5182 }
5183
5184 if (fake_open->filename) {
5185 const char *tmpdir;
5186 char filename[PATH_MAX];
5187 int fd, r;
5188
5189 /* create temporary file to map stat to */
5190 tmpdir = getenv("TMPDIR");
5191 if (!tmpdir)
5192 tmpdir = "/tmp";
5193 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5194 fd = mkstemp(filename);
5195 if (fd < 0) {
5196 return fd;
5197 }
5198 unlink(filename);
5199
5200 if ((r = fake_open->fill(cpu_env, fd))) {
5201 close(fd);
5202 return r;
5203 }
5204 lseek(fd, 0, SEEK_SET);
5205
5206 return fd;
5207 }
5208
5209 return get_errno(open(path(pathname), flags, mode));
5210}
5211
ths0da46a62007-10-20 20:23:07 +00005212/* do_syscall() should always have a single exit point at the end so
5213 that actions, such as logging of syscall results, can be performed.
5214 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00005215abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5216 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01005217 abi_long arg5, abi_long arg6, abi_long arg7,
5218 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00005219{
Andreas Färber182735e2013-05-29 22:29:20 +02005220 CPUState *cpu = ENV_GET_CPU(cpu_env);
blueswir1992f48a2007-10-14 16:27:31 +00005221 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005222 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00005223 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00005224 void *p;
ths3b46e622007-09-17 08:09:54 +00005225
bellard72f03902003-02-18 23:33:18 +00005226#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00005227 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00005228#endif
thsb92c47c2007-11-01 00:07:38 +00005229 if(do_strace)
5230 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5231
bellard31e31b82003-02-18 22:55:36 +00005232 switch(num) {
5233 case TARGET_NR_exit:
Andreas Färber9b056fc2013-06-24 23:53:10 +02005234 /* In old applications this may be used to implement _exit(2).
5235 However in threaded applictions it is used for thread termination,
5236 and _exit_group is used for application termination.
5237 Do thread termination if we have more then one thread. */
5238 /* FIXME: This probably breaks if a signal arrives. We should probably
5239 be disabling signals. */
Andreas Färberbdc44642013-06-24 23:50:24 +02005240 if (CPU_NEXT(first_cpu)) {
Andreas Färber9b056fc2013-06-24 23:53:10 +02005241 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00005242
Andreas Färber9b056fc2013-06-24 23:53:10 +02005243 cpu_list_lock();
Andreas Färber9b056fc2013-06-24 23:53:10 +02005244 /* Remove the CPU from the list. */
Andreas Färberbdc44642013-06-24 23:50:24 +02005245 QTAILQ_REMOVE(&cpus, cpu, node);
Andreas Färber9b056fc2013-06-24 23:53:10 +02005246 cpu_list_unlock();
5247 ts = ((CPUArchState *)cpu_env)->opaque;
5248 if (ts->child_tidptr) {
5249 put_user_u32(0, ts->child_tidptr);
5250 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5251 NULL, NULL, 0);
5252 }
Andreas Färbera2247f82013-06-09 19:47:04 +02005253 thread_cpu = NULL;
Andreas Färber9b056fc2013-06-24 23:53:10 +02005254 object_unref(OBJECT(ENV_GET_CPU(cpu_env)));
5255 g_free(ts);
5256 pthread_exit(NULL);
5257 }
Juan Quintela9788c9c2009-07-27 16:13:02 +02005258#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00005259 _mcleanup();
5260#endif
bellarde9009672005-04-26 20:42:36 +00005261 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00005262 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00005263 ret = 0; /* avoid warning */
5264 break;
5265 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00005266 if (arg3 == 0)
5267 ret = 0;
5268 else {
5269 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5270 goto efault;
5271 ret = get_errno(read(arg1, p, arg3));
5272 unlock_user(p, arg2, ret);
5273 }
bellard31e31b82003-02-18 22:55:36 +00005274 break;
5275 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00005276 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5277 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005278 ret = get_errno(write(arg1, p, arg3));
5279 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00005280 break;
5281 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00005282 if (!(p = lock_user_string(arg1)))
5283 goto efault;
Alexander Graf3be14d02011-11-02 20:23:23 +01005284 ret = get_errno(do_open(cpu_env, p,
5285 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5286 arg3));
pbrook53a59602006-03-25 19:31:22 +00005287 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005288 break;
ths82424832007-09-24 09:21:55 +00005289#if defined(TARGET_NR_openat) && defined(__NR_openat)
5290 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00005291 if (!(p = lock_user_string(arg2)))
5292 goto efault;
5293 ret = get_errno(sys_openat(arg1,
5294 path(p),
5295 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5296 arg4));
5297 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00005298 break;
5299#endif
bellard31e31b82003-02-18 22:55:36 +00005300 case TARGET_NR_close:
5301 ret = get_errno(close(arg1));
5302 break;
5303 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00005304 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00005305 break;
5306 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00005307 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00005308 break;
thse5febef2007-04-01 18:31:35 +00005309#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00005310 case TARGET_NR_waitpid:
5311 {
pbrook53a59602006-03-25 19:31:22 +00005312 int status;
5313 ret = get_errno(waitpid(arg1, &status, arg3));
Alexander Graf53795572011-11-24 00:44:43 +01005314 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00005315 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00005316 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005317 }
5318 break;
thse5febef2007-04-01 18:31:35 +00005319#endif
pbrookf0cbb612008-05-30 18:20:05 +00005320#ifdef TARGET_NR_waitid
5321 case TARGET_NR_waitid:
5322 {
5323 siginfo_t info;
5324 info.si_pid = 0;
5325 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5326 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005327 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00005328 goto efault;
5329 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05005330 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00005331 }
5332 }
5333 break;
5334#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005335#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005336 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00005337 if (!(p = lock_user_string(arg1)))
5338 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005339 ret = get_errno(creat(p, arg2));
5340 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005341 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005342#endif
bellard31e31b82003-02-18 22:55:36 +00005343 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00005344 {
5345 void * p2;
5346 p = lock_user_string(arg1);
5347 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005348 if (!p || !p2)
5349 ret = -TARGET_EFAULT;
5350 else
5351 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005352 unlock_user(p2, arg2, 0);
5353 unlock_user(p, arg1, 0);
5354 }
bellard31e31b82003-02-18 22:55:36 +00005355 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005356#if defined(TARGET_NR_linkat)
ths64f0ce42007-09-24 09:25:06 +00005357 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00005358 {
5359 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00005360 if (!arg2 || !arg4)
5361 goto efault;
ths64f0ce42007-09-24 09:25:06 +00005362 p = lock_user_string(arg2);
5363 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005364 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005365 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00005366 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01005367 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00005368 unlock_user(p, arg2, 0);
5369 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00005370 }
5371 break;
5372#endif
bellard31e31b82003-02-18 22:55:36 +00005373 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00005374 if (!(p = lock_user_string(arg1)))
5375 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005376 ret = get_errno(unlink(p));
5377 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005378 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005379#if defined(TARGET_NR_unlinkat)
ths8170f562007-09-24 09:24:11 +00005380 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00005381 if (!(p = lock_user_string(arg2)))
5382 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005383 ret = get_errno(unlinkat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005384 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00005385 break;
balrogb7d35e62007-12-12 00:40:24 +00005386#endif
bellard31e31b82003-02-18 22:55:36 +00005387 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00005388 {
5389 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00005390 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00005391 abi_ulong gp;
5392 abi_ulong guest_argp;
5393 abi_ulong guest_envp;
5394 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00005395 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005396 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00005397
bellardf7341ff2003-03-30 21:00:25 +00005398 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00005399 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00005400 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005401 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005402 goto efault;
ths03aa1972007-12-02 06:28:08 +00005403 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005404 break;
bellard7854b052003-03-29 17:22:23 +00005405 argc++;
bellard2f619692007-11-16 10:46:05 +00005406 }
bellardf7341ff2003-03-30 21:00:25 +00005407 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00005408 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00005409 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005410 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005411 goto efault;
ths03aa1972007-12-02 06:28:08 +00005412 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005413 break;
bellard7854b052003-03-29 17:22:23 +00005414 envc++;
bellard2f619692007-11-16 10:46:05 +00005415 }
bellard7854b052003-03-29 17:22:23 +00005416
bellardf7341ff2003-03-30 21:00:25 +00005417 argp = alloca((argc + 1) * sizeof(void *));
5418 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00005419
pbrookda94d262008-05-30 18:24:00 +00005420 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005421 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005422 if (get_user_ual(addr, gp))
5423 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005424 if (!addr)
5425 break;
bellard2f619692007-11-16 10:46:05 +00005426 if (!(*q = lock_user_string(addr)))
5427 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005428 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005429 }
bellardf7341ff2003-03-30 21:00:25 +00005430 *q = NULL;
5431
pbrookda94d262008-05-30 18:24:00 +00005432 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005433 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005434 if (get_user_ual(addr, gp))
5435 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005436 if (!addr)
5437 break;
bellard2f619692007-11-16 10:46:05 +00005438 if (!(*q = lock_user_string(addr)))
5439 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005440 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005441 }
bellardf7341ff2003-03-30 21:00:25 +00005442 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00005443
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005444 /* This case will not be caught by the host's execve() if its
5445 page size is bigger than the target's. */
5446 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5447 ret = -TARGET_E2BIG;
5448 goto execve_end;
5449 }
bellard2f619692007-11-16 10:46:05 +00005450 if (!(p = lock_user_string(arg1)))
5451 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005452 ret = get_errno(execve(p, argp, envp));
5453 unlock_user(p, arg1, 0);
5454
bellard2f619692007-11-16 10:46:05 +00005455 goto execve_end;
5456
5457 execve_efault:
5458 ret = -TARGET_EFAULT;
5459
5460 execve_end:
pbrook53a59602006-03-25 19:31:22 +00005461 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005462 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005463 if (get_user_ual(addr, gp)
5464 || !addr)
5465 break;
pbrook53a59602006-03-25 19:31:22 +00005466 unlock_user(*q, addr, 0);
5467 }
5468 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005469 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005470 if (get_user_ual(addr, gp)
5471 || !addr)
5472 break;
pbrook53a59602006-03-25 19:31:22 +00005473 unlock_user(*q, addr, 0);
5474 }
bellard7854b052003-03-29 17:22:23 +00005475 }
bellard31e31b82003-02-18 22:55:36 +00005476 break;
5477 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00005478 if (!(p = lock_user_string(arg1)))
5479 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005480 ret = get_errno(chdir(p));
5481 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005482 break;
bellarda315a142005-01-30 22:59:18 +00005483#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00005484 case TARGET_NR_time:
5485 {
pbrook53a59602006-03-25 19:31:22 +00005486 time_t host_time;
5487 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00005488 if (!is_error(ret)
5489 && arg1
5490 && put_user_sal(host_time, arg1))
5491 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005492 }
5493 break;
bellarda315a142005-01-30 22:59:18 +00005494#endif
bellard31e31b82003-02-18 22:55:36 +00005495 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00005496 if (!(p = lock_user_string(arg1)))
5497 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005498 ret = get_errno(mknod(p, arg2, arg3));
5499 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005500 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005501#if defined(TARGET_NR_mknodat)
ths75ac37a2007-09-24 09:23:05 +00005502 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00005503 if (!(p = lock_user_string(arg2)))
5504 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005505 ret = get_errno(mknodat(arg1, p, arg3, arg4));
bellard579a97f2007-11-11 14:26:47 +00005506 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00005507 break;
5508#endif
bellard31e31b82003-02-18 22:55:36 +00005509 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00005510 if (!(p = lock_user_string(arg1)))
5511 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005512 ret = get_errno(chmod(p, arg2));
5513 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005514 break;
bellardebc05482003-09-30 21:08:41 +00005515#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00005516 case TARGET_NR_break:
5517 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005518#endif
5519#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00005520 case TARGET_NR_oldstat:
5521 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005522#endif
bellard31e31b82003-02-18 22:55:36 +00005523 case TARGET_NR_lseek:
5524 ret = get_errno(lseek(arg1, arg2, arg3));
5525 break;
Richard Henderson92317332010-05-03 10:07:53 -07005526#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5527 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00005528 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07005529 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00005530 ret = get_errno(getpid());
5531 break;
Richard Henderson92317332010-05-03 10:07:53 -07005532#endif
5533#ifdef TARGET_NR_getpid
5534 case TARGET_NR_getpid:
5535 ret = get_errno(getpid());
5536 break;
5537#endif
bellard31e31b82003-02-18 22:55:36 +00005538 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00005539 {
5540 /* need to look at the data field */
5541 void *p2, *p3;
5542 p = lock_user_string(arg1);
5543 p2 = lock_user_string(arg2);
5544 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005545 if (!p || !p2 || !p3)
5546 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005547 else {
bellard579a97f2007-11-11 14:26:47 +00005548 /* FIXME - arg5 should be locked, but it isn't clear how to
5549 * do that since it's not guaranteed to be a NULL-terminated
5550 * string.
5551 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005552 if ( ! arg5 )
5553 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
5554 else
5555 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
5556 }
bellard579a97f2007-11-11 14:26:47 +00005557 unlock_user(p, arg1, 0);
5558 unlock_user(p2, arg2, 0);
5559 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00005560 break;
5561 }
thse5febef2007-04-01 18:31:35 +00005562#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00005563 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00005564 if (!(p = lock_user_string(arg1)))
5565 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005566 ret = get_errno(umount(p));
5567 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005568 break;
thse5febef2007-04-01 18:31:35 +00005569#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005570#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005571 case TARGET_NR_stime:
5572 {
pbrook53a59602006-03-25 19:31:22 +00005573 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00005574 if (get_user_sal(host_time, arg1))
5575 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005576 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00005577 }
5578 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005579#endif
bellard31e31b82003-02-18 22:55:36 +00005580 case TARGET_NR_ptrace:
5581 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00005582#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005583 case TARGET_NR_alarm:
5584 ret = alarm(arg1);
5585 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005586#endif
bellardebc05482003-09-30 21:08:41 +00005587#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00005588 case TARGET_NR_oldfstat:
5589 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005590#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005591#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005592 case TARGET_NR_pause:
5593 ret = get_errno(pause());
5594 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005595#endif
thse5febef2007-04-01 18:31:35 +00005596#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00005597 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00005598 {
pbrook53a59602006-03-25 19:31:22 +00005599 struct utimbuf tbuf, *host_tbuf;
5600 struct target_utimbuf *target_tbuf;
5601 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005602 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5603 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005604 tbuf.actime = tswapal(target_tbuf->actime);
5605 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00005606 unlock_user_struct(target_tbuf, arg2, 0);
5607 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00005608 } else {
pbrook53a59602006-03-25 19:31:22 +00005609 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00005610 }
bellard579a97f2007-11-11 14:26:47 +00005611 if (!(p = lock_user_string(arg1)))
5612 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005613 ret = get_errno(utime(p, host_tbuf));
5614 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00005615 }
5616 break;
thse5febef2007-04-01 18:31:35 +00005617#endif
bellard978a66f2004-12-06 22:58:05 +00005618 case TARGET_NR_utimes:
5619 {
bellard978a66f2004-12-06 22:58:05 +00005620 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00005621 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00005622 if (copy_from_user_timeval(&tv[0], arg2)
5623 || copy_from_user_timeval(&tv[1],
5624 arg2 + sizeof(struct target_timeval)))
5625 goto efault;
bellard978a66f2004-12-06 22:58:05 +00005626 tvp = tv;
5627 } else {
5628 tvp = NULL;
5629 }
bellard579a97f2007-11-11 14:26:47 +00005630 if (!(p = lock_user_string(arg1)))
5631 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005632 ret = get_errno(utimes(p, tvp));
5633 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00005634 }
5635 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005636#if defined(TARGET_NR_futimesat)
balrogac8a6552008-09-20 02:25:39 +00005637 case TARGET_NR_futimesat:
5638 {
5639 struct timeval *tvp, tv[2];
5640 if (arg3) {
5641 if (copy_from_user_timeval(&tv[0], arg3)
5642 || copy_from_user_timeval(&tv[1],
5643 arg3 + sizeof(struct target_timeval)))
5644 goto efault;
5645 tvp = tv;
5646 } else {
5647 tvp = NULL;
5648 }
5649 if (!(p = lock_user_string(arg2)))
5650 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005651 ret = get_errno(futimesat(arg1, path(p), tvp));
balrogac8a6552008-09-20 02:25:39 +00005652 unlock_user(p, arg2, 0);
5653 }
5654 break;
5655#endif
bellardebc05482003-09-30 21:08:41 +00005656#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00005657 case TARGET_NR_stty:
5658 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005659#endif
5660#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00005661 case TARGET_NR_gtty:
5662 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005663#endif
bellard31e31b82003-02-18 22:55:36 +00005664 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00005665 if (!(p = lock_user_string(arg1)))
5666 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02005667 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00005668 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005669 break;
ths92a34c12007-09-24 09:27:49 +00005670#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5671 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00005672 if (!(p = lock_user_string(arg2)))
5673 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005674 ret = get_errno(faccessat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00005675 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00005676 break;
5677#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005678#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005679 case TARGET_NR_nice:
5680 ret = get_errno(nice(arg1));
5681 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005682#endif
bellardebc05482003-09-30 21:08:41 +00005683#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00005684 case TARGET_NR_ftime:
5685 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005686#endif
bellard31e31b82003-02-18 22:55:36 +00005687 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00005688 sync();
5689 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00005690 break;
5691 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00005692 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00005693 break;
5694 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00005695 {
5696 void *p2;
5697 p = lock_user_string(arg1);
5698 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005699 if (!p || !p2)
5700 ret = -TARGET_EFAULT;
5701 else
5702 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005703 unlock_user(p2, arg2, 0);
5704 unlock_user(p, arg1, 0);
5705 }
bellard31e31b82003-02-18 22:55:36 +00005706 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005707#if defined(TARGET_NR_renameat)
ths722183f2007-09-24 09:24:37 +00005708 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00005709 {
bellard579a97f2007-11-11 14:26:47 +00005710 void *p2;
ths722183f2007-09-24 09:24:37 +00005711 p = lock_user_string(arg2);
5712 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005713 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005714 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00005715 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01005716 ret = get_errno(renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00005717 unlock_user(p2, arg4, 0);
5718 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00005719 }
5720 break;
5721#endif
bellard31e31b82003-02-18 22:55:36 +00005722 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00005723 if (!(p = lock_user_string(arg1)))
5724 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005725 ret = get_errno(mkdir(p, arg2));
5726 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005727 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005728#if defined(TARGET_NR_mkdirat)
ths4472ad02007-09-24 09:22:32 +00005729 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00005730 if (!(p = lock_user_string(arg2)))
5731 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005732 ret = get_errno(mkdirat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005733 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00005734 break;
5735#endif
bellard31e31b82003-02-18 22:55:36 +00005736 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00005737 if (!(p = lock_user_string(arg1)))
5738 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005739 ret = get_errno(rmdir(p));
5740 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005741 break;
5742 case TARGET_NR_dup:
5743 ret = get_errno(dup(arg1));
5744 break;
5745 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005746 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00005747 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03005748#ifdef TARGET_NR_pipe2
5749 case TARGET_NR_pipe2:
Richard Hendersone7ea6cb2012-06-01 18:48:39 -07005750 ret = do_pipe(cpu_env, arg1,
5751 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03005752 break;
5753#endif
bellard31e31b82003-02-18 22:55:36 +00005754 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00005755 {
pbrook53a59602006-03-25 19:31:22 +00005756 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00005757 struct tms tms;
5758 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00005759 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00005760 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5761 if (!tmsp)
5762 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005763 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5764 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5765 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5766 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00005767 }
bellardc596ed12003-07-13 17:32:31 +00005768 if (!is_error(ret))
5769 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00005770 }
5771 break;
bellardebc05482003-09-30 21:08:41 +00005772#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00005773 case TARGET_NR_prof:
5774 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005775#endif
thse5febef2007-04-01 18:31:35 +00005776#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00005777 case TARGET_NR_signal:
5778 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005779#endif
bellard31e31b82003-02-18 22:55:36 +00005780 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00005781 if (arg1 == 0) {
5782 ret = get_errno(acct(NULL));
5783 } else {
5784 if (!(p = lock_user_string(arg1)))
5785 goto efault;
5786 ret = get_errno(acct(path(p)));
5787 unlock_user(p, arg1, 0);
5788 }
pbrook24836682006-04-16 14:14:53 +00005789 break;
Richard Henderson8070e7b2013-07-24 09:50:00 -10005790#ifdef TARGET_NR_umount2
bellard31e31b82003-02-18 22:55:36 +00005791 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00005792 if (!(p = lock_user_string(arg1)))
5793 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005794 ret = get_errno(umount2(p, arg2));
5795 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005796 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005797#endif
bellardebc05482003-09-30 21:08:41 +00005798#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00005799 case TARGET_NR_lock:
5800 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005801#endif
bellard31e31b82003-02-18 22:55:36 +00005802 case TARGET_NR_ioctl:
5803 ret = do_ioctl(arg1, arg2, arg3);
5804 break;
5805 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00005806 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00005807 break;
bellardebc05482003-09-30 21:08:41 +00005808#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00005809 case TARGET_NR_mpx:
5810 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005811#endif
bellard31e31b82003-02-18 22:55:36 +00005812 case TARGET_NR_setpgid:
5813 ret = get_errno(setpgid(arg1, arg2));
5814 break;
bellardebc05482003-09-30 21:08:41 +00005815#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00005816 case TARGET_NR_ulimit:
5817 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005818#endif
5819#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00005820 case TARGET_NR_oldolduname:
5821 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005822#endif
bellard31e31b82003-02-18 22:55:36 +00005823 case TARGET_NR_umask:
5824 ret = get_errno(umask(arg1));
5825 break;
5826 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00005827 if (!(p = lock_user_string(arg1)))
5828 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005829 ret = get_errno(chroot(p));
5830 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005831 break;
5832 case TARGET_NR_ustat:
5833 goto unimplemented;
5834 case TARGET_NR_dup2:
5835 ret = get_errno(dup2(arg1, arg2));
5836 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03005837#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5838 case TARGET_NR_dup3:
5839 ret = get_errno(dup3(arg1, arg2, arg3));
5840 break;
5841#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005842#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005843 case TARGET_NR_getppid:
5844 ret = get_errno(getppid());
5845 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005846#endif
bellard31e31b82003-02-18 22:55:36 +00005847 case TARGET_NR_getpgrp:
5848 ret = get_errno(getpgrp());
5849 break;
5850 case TARGET_NR_setsid:
5851 ret = get_errno(setsid());
5852 break;
thse5febef2007-04-01 18:31:35 +00005853#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00005854 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00005855 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005856#if defined(TARGET_ALPHA)
5857 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00005858 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00005859 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005860 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5861 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005862 act._sa_handler = old_act->_sa_handler;
5863 target_siginitset(&act.sa_mask, old_act->sa_mask);
5864 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005865 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00005866 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005867 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00005868 }
5869 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00005870 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005871 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5872 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005873 old_act->_sa_handler = oact._sa_handler;
5874 old_act->sa_mask = oact.sa_mask.sig[0];
5875 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00005876 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00005877 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005878#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00005879 struct target_sigaction act, oact, *pact, *old_act;
5880
5881 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005882 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5883 goto efault;
bellard106ec872006-06-27 21:08:10 +00005884 act._sa_handler = old_act->_sa_handler;
5885 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5886 act.sa_flags = old_act->sa_flags;
5887 unlock_user_struct(old_act, arg2, 0);
5888 pact = &act;
5889 } else {
5890 pact = NULL;
5891 }
5892
5893 ret = get_errno(do_sigaction(arg1, pact, &oact));
5894
5895 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005896 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5897 goto efault;
bellard106ec872006-06-27 21:08:10 +00005898 old_act->_sa_handler = oact._sa_handler;
5899 old_act->sa_flags = oact.sa_flags;
5900 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5901 old_act->sa_mask.sig[1] = 0;
5902 old_act->sa_mask.sig[2] = 0;
5903 old_act->sa_mask.sig[3] = 0;
5904 unlock_user_struct(old_act, arg3, 1);
5905 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005906#else
5907 struct target_old_sigaction *old_act;
5908 struct target_sigaction act, oact, *pact;
5909 if (arg2) {
5910 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5911 goto efault;
5912 act._sa_handler = old_act->_sa_handler;
5913 target_siginitset(&act.sa_mask, old_act->sa_mask);
5914 act.sa_flags = old_act->sa_flags;
5915 act.sa_restorer = old_act->sa_restorer;
5916 unlock_user_struct(old_act, arg2, 0);
5917 pact = &act;
5918 } else {
5919 pact = NULL;
5920 }
5921 ret = get_errno(do_sigaction(arg1, pact, &oact));
5922 if (!is_error(ret) && arg3) {
5923 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5924 goto efault;
5925 old_act->_sa_handler = oact._sa_handler;
5926 old_act->sa_mask = oact.sa_mask.sig[0];
5927 old_act->sa_flags = oact.sa_flags;
5928 old_act->sa_restorer = oact.sa_restorer;
5929 unlock_user_struct(old_act, arg3, 1);
5930 }
ths388bb212007-05-13 13:58:00 +00005931#endif
bellard31e31b82003-02-18 22:55:36 +00005932 }
5933 break;
thse5febef2007-04-01 18:31:35 +00005934#endif
bellard66fb9762003-03-23 01:06:05 +00005935 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00005936 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005937#if defined(TARGET_ALPHA)
5938 struct target_sigaction act, oact, *pact = 0;
5939 struct target_rt_sigaction *rt_act;
5940 /* ??? arg4 == sizeof(sigset_t). */
5941 if (arg2) {
5942 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5943 goto efault;
5944 act._sa_handler = rt_act->_sa_handler;
5945 act.sa_mask = rt_act->sa_mask;
5946 act.sa_flags = rt_act->sa_flags;
5947 act.sa_restorer = arg5;
5948 unlock_user_struct(rt_act, arg2, 0);
5949 pact = &act;
5950 }
5951 ret = get_errno(do_sigaction(arg1, pact, &oact));
5952 if (!is_error(ret) && arg3) {
5953 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5954 goto efault;
5955 rt_act->_sa_handler = oact._sa_handler;
5956 rt_act->sa_mask = oact.sa_mask;
5957 rt_act->sa_flags = oact.sa_flags;
5958 unlock_user_struct(rt_act, arg3, 1);
5959 }
5960#else
pbrook53a59602006-03-25 19:31:22 +00005961 struct target_sigaction *act;
5962 struct target_sigaction *oact;
5963
bellard579a97f2007-11-11 14:26:47 +00005964 if (arg2) {
5965 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5966 goto efault;
5967 } else
pbrook53a59602006-03-25 19:31:22 +00005968 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005969 if (arg3) {
5970 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5971 ret = -TARGET_EFAULT;
5972 goto rt_sigaction_fail;
5973 }
5974 } else
pbrook53a59602006-03-25 19:31:22 +00005975 oact = NULL;
5976 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005977 rt_sigaction_fail:
5978 if (act)
pbrook53a59602006-03-25 19:31:22 +00005979 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005980 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005981 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005982#endif
pbrook53a59602006-03-25 19:31:22 +00005983 }
bellard66fb9762003-03-23 01:06:05 +00005984 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005985#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005986 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005987 {
5988 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005989 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005990 sigprocmask(0, NULL, &cur_set);
5991 host_to_target_old_sigset(&target_set, &cur_set);
5992 ret = target_set;
5993 }
5994 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005995#endif
5996#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005997 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005998 {
5999 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00006000 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00006001 sigprocmask(0, NULL, &cur_set);
6002 target_to_host_old_sigset(&set, &target_set);
6003 sigorset(&set, &set, &cur_set);
6004 sigprocmask(SIG_SETMASK, &set, &oset);
6005 host_to_target_old_sigset(&target_set, &oset);
6006 ret = target_set;
6007 }
6008 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006009#endif
thse5febef2007-04-01 18:31:35 +00006010#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00006011 case TARGET_NR_sigprocmask:
6012 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07006013#if defined(TARGET_ALPHA)
6014 sigset_t set, oldset;
6015 abi_ulong mask;
6016 int how;
6017
6018 switch (arg1) {
6019 case TARGET_SIG_BLOCK:
6020 how = SIG_BLOCK;
6021 break;
6022 case TARGET_SIG_UNBLOCK:
6023 how = SIG_UNBLOCK;
6024 break;
6025 case TARGET_SIG_SETMASK:
6026 how = SIG_SETMASK;
6027 break;
6028 default:
6029 ret = -TARGET_EINVAL;
6030 goto fail;
6031 }
6032 mask = arg2;
6033 target_to_host_old_sigset(&set, &mask);
6034
6035 ret = get_errno(sigprocmask(how, &set, &oldset));
Richard Hendersona5b3b132010-05-03 10:07:55 -07006036 if (!is_error(ret)) {
6037 host_to_target_old_sigset(&mask, &oldset);
6038 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07006039 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07006040 }
6041#else
bellard66fb9762003-03-23 01:06:05 +00006042 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07006043 int how;
ths3b46e622007-09-17 08:09:54 +00006044
pbrook53a59602006-03-25 19:31:22 +00006045 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07006046 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00006047 case TARGET_SIG_BLOCK:
6048 how = SIG_BLOCK;
6049 break;
6050 case TARGET_SIG_UNBLOCK:
6051 how = SIG_UNBLOCK;
6052 break;
6053 case TARGET_SIG_SETMASK:
6054 how = SIG_SETMASK;
6055 break;
6056 default:
ths0da46a62007-10-20 20:23:07 +00006057 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006058 goto fail;
6059 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006060 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006061 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006062 target_to_host_old_sigset(&set, p);
6063 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006064 set_ptr = &set;
6065 } else {
6066 how = 0;
6067 set_ptr = NULL;
6068 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07006069 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006070 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006071 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006072 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006073 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006074 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006075 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07006076#endif
bellard66fb9762003-03-23 01:06:05 +00006077 }
6078 break;
thse5febef2007-04-01 18:31:35 +00006079#endif
bellard66fb9762003-03-23 01:06:05 +00006080 case TARGET_NR_rt_sigprocmask:
6081 {
6082 int how = arg1;
6083 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00006084
pbrook53a59602006-03-25 19:31:22 +00006085 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006086 switch(how) {
6087 case TARGET_SIG_BLOCK:
6088 how = SIG_BLOCK;
6089 break;
6090 case TARGET_SIG_UNBLOCK:
6091 how = SIG_UNBLOCK;
6092 break;
6093 case TARGET_SIG_SETMASK:
6094 how = SIG_SETMASK;
6095 break;
6096 default:
ths0da46a62007-10-20 20:23:07 +00006097 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00006098 goto fail;
6099 }
Anthony Liguoric227f092009-10-01 16:12:16 -05006100 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006101 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006102 target_to_host_sigset(&set, p);
6103 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00006104 set_ptr = &set;
6105 } else {
6106 how = 0;
6107 set_ptr = NULL;
6108 }
6109 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00006110 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006111 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006112 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006113 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05006114 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006115 }
6116 }
6117 break;
thse5febef2007-04-01 18:31:35 +00006118#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00006119 case TARGET_NR_sigpending:
6120 {
6121 sigset_t set;
6122 ret = get_errno(sigpending(&set));
6123 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006124 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006125 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006126 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006127 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006128 }
6129 }
6130 break;
thse5febef2007-04-01 18:31:35 +00006131#endif
bellard66fb9762003-03-23 01:06:05 +00006132 case TARGET_NR_rt_sigpending:
6133 {
6134 sigset_t set;
6135 ret = get_errno(sigpending(&set));
6136 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006137 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006138 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006139 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05006140 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00006141 }
6142 }
6143 break;
thse5febef2007-04-01 18:31:35 +00006144#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00006145 case TARGET_NR_sigsuspend:
6146 {
6147 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07006148#if defined(TARGET_ALPHA)
6149 abi_ulong mask = arg1;
6150 target_to_host_old_sigset(&set, &mask);
6151#else
Anthony Liguoric227f092009-10-01 16:12:16 -05006152 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006153 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006154 target_to_host_old_sigset(&set, p);
6155 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07006156#endif
bellard66fb9762003-03-23 01:06:05 +00006157 ret = get_errno(sigsuspend(&set));
6158 }
6159 break;
thse5febef2007-04-01 18:31:35 +00006160#endif
bellard66fb9762003-03-23 01:06:05 +00006161 case TARGET_NR_rt_sigsuspend:
6162 {
6163 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05006164 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006165 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006166 target_to_host_sigset(&set, p);
6167 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006168 ret = get_errno(sigsuspend(&set));
6169 }
6170 break;
6171 case TARGET_NR_rt_sigtimedwait:
6172 {
bellard66fb9762003-03-23 01:06:05 +00006173 sigset_t set;
6174 struct timespec uts, *puts;
6175 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00006176
Anthony Liguoric227f092009-10-01 16:12:16 -05006177 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006178 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006179 target_to_host_sigset(&set, p);
6180 unlock_user(p, arg1, 0);
6181 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00006182 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00006183 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00006184 } else {
6185 puts = NULL;
6186 }
6187 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00006188 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006189 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006190 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006191 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05006192 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00006193 }
6194 }
6195 break;
6196 case TARGET_NR_rt_sigqueueinfo:
6197 {
6198 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05006199 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006200 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006201 target_to_host_siginfo(&uinfo, p);
6202 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006203 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6204 }
6205 break;
thse5febef2007-04-01 18:31:35 +00006206#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00006207 case TARGET_NR_sigreturn:
6208 /* NOTE: ret is eax, so not transcoding must be done */
6209 ret = do_sigreturn(cpu_env);
6210 break;
thse5febef2007-04-01 18:31:35 +00006211#endif
bellard66fb9762003-03-23 01:06:05 +00006212 case TARGET_NR_rt_sigreturn:
6213 /* NOTE: ret is eax, so not transcoding must be done */
6214 ret = do_rt_sigreturn(cpu_env);
6215 break;
bellard31e31b82003-02-18 22:55:36 +00006216 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00006217 if (!(p = lock_user_string(arg1)))
6218 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006219 ret = get_errno(sethostname(p, arg2));
6220 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006221 break;
6222 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00006223 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006224 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006225 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006226 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00006227 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6228 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006229 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6230 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006231 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00006232 ret = get_errno(setrlimit(resource, &rlim));
6233 }
6234 break;
bellard31e31b82003-02-18 22:55:36 +00006235 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00006236 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006237 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006238 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006239 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00006240
bellard9de5e442003-03-23 16:49:39 +00006241 ret = get_errno(getrlimit(resource, &rlim));
6242 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006243 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6244 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006245 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6246 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006247 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00006248 }
6249 }
6250 break;
bellard31e31b82003-02-18 22:55:36 +00006251 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00006252 {
6253 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00006254 ret = get_errno(getrusage(arg1, &rusage));
6255 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006256 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00006257 }
6258 }
6259 break;
bellard31e31b82003-02-18 22:55:36 +00006260 case TARGET_NR_gettimeofday:
6261 {
bellard31e31b82003-02-18 22:55:36 +00006262 struct timeval tv;
6263 ret = get_errno(gettimeofday(&tv, NULL));
6264 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00006265 if (copy_to_user_timeval(arg1, &tv))
6266 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006267 }
6268 }
6269 break;
6270 case TARGET_NR_settimeofday:
6271 {
bellard31e31b82003-02-18 22:55:36 +00006272 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00006273 if (copy_from_user_timeval(&tv, arg1))
6274 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006275 ret = get_errno(settimeofday(&tv, NULL));
6276 }
6277 break;
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006278#if defined(TARGET_NR_select)
bellard31e31b82003-02-18 22:55:36 +00006279 case TARGET_NR_select:
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006280#if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6281 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6282#else
bellardf2674e32003-07-09 12:26:09 +00006283 {
pbrook53a59602006-03-25 19:31:22 +00006284 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00006285 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00006286 long nsel;
6287
bellard579a97f2007-11-11 14:26:47 +00006288 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6289 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006290 nsel = tswapal(sel->n);
6291 inp = tswapal(sel->inp);
6292 outp = tswapal(sel->outp);
6293 exp = tswapal(sel->exp);
6294 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00006295 unlock_user_struct(sel, arg1, 0);
6296 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00006297 }
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006298#endif
bellardf2674e32003-07-09 12:26:09 +00006299 break;
bellard048f6b42005-11-26 18:47:20 +00006300#endif
Riku Voipio9e423822010-05-07 12:28:05 +00006301#ifdef TARGET_NR_pselect6
6302 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04006303 {
6304 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6305 fd_set rfds, wfds, efds;
6306 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6307 struct timespec ts, *ts_ptr;
6308
6309 /*
6310 * The 6th arg is actually two args smashed together,
6311 * so we cannot use the C library.
6312 */
6313 sigset_t set;
6314 struct {
6315 sigset_t *set;
6316 size_t size;
6317 } sig, *sig_ptr;
6318
6319 abi_ulong arg_sigset, arg_sigsize, *arg7;
6320 target_sigset_t *target_sigset;
6321
6322 n = arg1;
6323 rfd_addr = arg2;
6324 wfd_addr = arg3;
6325 efd_addr = arg4;
6326 ts_addr = arg5;
6327
6328 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6329 if (ret) {
6330 goto fail;
6331 }
6332 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6333 if (ret) {
6334 goto fail;
6335 }
6336 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6337 if (ret) {
6338 goto fail;
6339 }
6340
6341 /*
6342 * This takes a timespec, and not a timeval, so we cannot
6343 * use the do_select() helper ...
6344 */
6345 if (ts_addr) {
6346 if (target_to_host_timespec(&ts, ts_addr)) {
6347 goto efault;
6348 }
6349 ts_ptr = &ts;
6350 } else {
6351 ts_ptr = NULL;
6352 }
6353
6354 /* Extract the two packed args for the sigset */
6355 if (arg6) {
6356 sig_ptr = &sig;
6357 sig.size = _NSIG / 8;
6358
6359 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6360 if (!arg7) {
6361 goto efault;
6362 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006363 arg_sigset = tswapal(arg7[0]);
6364 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04006365 unlock_user(arg7, arg6, 0);
6366
6367 if (arg_sigset) {
6368 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01006369 if (arg_sigsize != sizeof(*target_sigset)) {
6370 /* Like the kernel, we enforce correct size sigsets */
6371 ret = -TARGET_EINVAL;
6372 goto fail;
6373 }
Mike Frysinger055e0902011-06-03 17:01:49 -04006374 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6375 sizeof(*target_sigset), 1);
6376 if (!target_sigset) {
6377 goto efault;
6378 }
6379 target_to_host_sigset(&set, target_sigset);
6380 unlock_user(target_sigset, arg_sigset, 0);
6381 } else {
6382 sig.set = NULL;
6383 }
6384 } else {
6385 sig_ptr = NULL;
6386 }
6387
6388 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6389 ts_ptr, sig_ptr));
6390
6391 if (!is_error(ret)) {
6392 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6393 goto efault;
6394 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6395 goto efault;
6396 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6397 goto efault;
6398
6399 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6400 goto efault;
6401 }
6402 }
6403 break;
Riku Voipio9e423822010-05-07 12:28:05 +00006404#endif
bellard31e31b82003-02-18 22:55:36 +00006405 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00006406 {
6407 void *p2;
6408 p = lock_user_string(arg1);
6409 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006410 if (!p || !p2)
6411 ret = -TARGET_EFAULT;
6412 else
6413 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006414 unlock_user(p2, arg2, 0);
6415 unlock_user(p, arg1, 0);
6416 }
bellard31e31b82003-02-18 22:55:36 +00006417 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006418#if defined(TARGET_NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +00006419 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00006420 {
bellard579a97f2007-11-11 14:26:47 +00006421 void *p2;
thsf0b62432007-09-24 09:25:40 +00006422 p = lock_user_string(arg1);
6423 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00006424 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006425 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00006426 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01006427 ret = get_errno(symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00006428 unlock_user(p2, arg3, 0);
6429 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00006430 }
6431 break;
6432#endif
bellardebc05482003-09-30 21:08:41 +00006433#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00006434 case TARGET_NR_oldlstat:
6435 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006436#endif
bellard31e31b82003-02-18 22:55:36 +00006437 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00006438 {
Andreas Schwab463d8e72013-07-02 14:04:12 +01006439 void *p2;
pbrook53a59602006-03-25 19:31:22 +00006440 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00006441 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01006442 if (!p || !p2) {
bellard579a97f2007-11-11 14:26:47 +00006443 ret = -TARGET_EFAULT;
Andreas Schwab463d8e72013-07-02 14:04:12 +01006444 } else if (is_proc_myself((const char *)p, "exe")) {
6445 char real[PATH_MAX], *temp;
6446 temp = realpath(exec_path, real);
6447 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
6448 snprintf((char *)p2, arg3, "%s", real);
6449 } else {
6450 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00006451 }
pbrook53a59602006-03-25 19:31:22 +00006452 unlock_user(p2, arg2, ret);
6453 unlock_user(p, arg1, 0);
6454 }
bellard31e31b82003-02-18 22:55:36 +00006455 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006456#if defined(TARGET_NR_readlinkat)
ths5e0ccb12007-09-24 09:26:10 +00006457 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00006458 {
bellard579a97f2007-11-11 14:26:47 +00006459 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00006460 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006461 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
Andreas Schwab463d8e72013-07-02 14:04:12 +01006462 if (!p || !p2) {
6463 ret = -TARGET_EFAULT;
6464 } else if (is_proc_myself((const char *)p, "exe")) {
6465 char real[PATH_MAX], *temp;
6466 temp = realpath(exec_path, real);
6467 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
6468 snprintf((char *)p2, arg4, "%s", real);
6469 } else {
Peter Maydellc0d472b2013-06-12 16:20:21 +01006470 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
Andreas Schwab463d8e72013-07-02 14:04:12 +01006471 }
bellard579a97f2007-11-11 14:26:47 +00006472 unlock_user(p2, arg3, ret);
6473 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00006474 }
6475 break;
6476#endif
thse5febef2007-04-01 18:31:35 +00006477#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00006478 case TARGET_NR_uselib:
6479 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006480#endif
6481#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00006482 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00006483 if (!(p = lock_user_string(arg1)))
6484 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006485 ret = get_errno(swapon(p, arg2));
6486 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006487 break;
thse5febef2007-04-01 18:31:35 +00006488#endif
bellard31e31b82003-02-18 22:55:36 +00006489 case TARGET_NR_reboot:
Laurent Vivierc07ecc62013-01-07 11:40:06 +00006490 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
6491 /* arg4 must be ignored in all other cases */
6492 p = lock_user_string(arg4);
6493 if (!p) {
6494 goto efault;
6495 }
6496 ret = get_errno(reboot(arg1, arg2, arg3, p));
6497 unlock_user(p, arg4, 0);
6498 } else {
6499 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
6500 }
Alexander Graf0f6b4d22011-09-27 14:39:42 +02006501 break;
thse5febef2007-04-01 18:31:35 +00006502#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00006503 case TARGET_NR_readdir:
6504 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006505#endif
6506#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00006507 case TARGET_NR_mmap:
Alexander Graf09701192013-09-03 20:12:15 +01006508#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6509 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006510 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6511 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00006512 {
blueswir1992f48a2007-10-14 16:27:31 +00006513 abi_ulong *v;
6514 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00006515 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6516 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006517 v1 = tswapal(v[0]);
6518 v2 = tswapal(v[1]);
6519 v3 = tswapal(v[2]);
6520 v4 = tswapal(v[3]);
6521 v5 = tswapal(v[4]);
6522 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00006523 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00006524 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00006525 target_to_host_bitmask(v4, mmap_flags_tbl),
6526 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00006527 }
bellard31e31b82003-02-18 22:55:36 +00006528#else
ths5fafdf22007-09-16 21:08:06 +00006529 ret = get_errno(target_mmap(arg1, arg2, arg3,
6530 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00006531 arg5,
6532 arg6));
bellard31e31b82003-02-18 22:55:36 +00006533#endif
bellard6fb883e2003-07-09 17:12:39 +00006534 break;
thse5febef2007-04-01 18:31:35 +00006535#endif
bellarda315a142005-01-30 22:59:18 +00006536#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00006537 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00006538#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00006539#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00006540#endif
ths5fafdf22007-09-16 21:08:06 +00006541 ret = get_errno(target_mmap(arg1, arg2, arg3,
6542 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00006543 arg5,
bellardc573ff62004-01-04 15:51:36 +00006544 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00006545 break;
bellarda315a142005-01-30 22:59:18 +00006546#endif
bellard31e31b82003-02-18 22:55:36 +00006547 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00006548 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006549 break;
bellard9de5e442003-03-23 16:49:39 +00006550 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01006551 {
Andreas Färber9349b4f2012-03-14 01:38:32 +01006552 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Paul Brook97374d32010-06-16 13:03:51 +01006553 /* Special hack to detect libc making the stack executable. */
6554 if ((arg3 & PROT_GROWSDOWN)
6555 && arg1 >= ts->info->stack_limit
6556 && arg1 <= ts->info->start_stack) {
6557 arg3 &= ~PROT_GROWSDOWN;
6558 arg2 = arg2 + arg1 - ts->info->stack_limit;
6559 arg1 = ts->info->stack_limit;
6560 }
6561 }
bellard54936002003-05-13 00:25:15 +00006562 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006563 break;
thse5febef2007-04-01 18:31:35 +00006564#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00006565 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00006566 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00006567 break;
thse5febef2007-04-01 18:31:35 +00006568#endif
pbrook53a59602006-03-25 19:31:22 +00006569 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00006570#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00006571 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00006572 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006573 break;
thse5febef2007-04-01 18:31:35 +00006574#endif
6575#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00006576 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00006577 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006578 break;
thse5febef2007-04-01 18:31:35 +00006579#endif
6580#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00006581 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00006582 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006583 break;
thse5febef2007-04-01 18:31:35 +00006584#endif
6585#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00006586 case TARGET_NR_mlockall:
6587 ret = get_errno(mlockall(arg1));
6588 break;
thse5febef2007-04-01 18:31:35 +00006589#endif
6590#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00006591 case TARGET_NR_munlockall:
6592 ret = get_errno(munlockall());
6593 break;
thse5febef2007-04-01 18:31:35 +00006594#endif
bellard31e31b82003-02-18 22:55:36 +00006595 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00006596 if (!(p = lock_user_string(arg1)))
6597 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006598 ret = get_errno(truncate(p, arg2));
6599 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006600 break;
6601 case TARGET_NR_ftruncate:
6602 ret = get_errno(ftruncate(arg1, arg2));
6603 break;
6604 case TARGET_NR_fchmod:
6605 ret = get_errno(fchmod(arg1, arg2));
6606 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006607#if defined(TARGET_NR_fchmodat)
ths814d7972007-09-24 09:26:51 +00006608 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00006609 if (!(p = lock_user_string(arg2)))
6610 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006611 ret = get_errno(fchmodat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00006612 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00006613 break;
6614#endif
bellard31e31b82003-02-18 22:55:36 +00006615 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -07006616 /* Note that negative values are valid for getpriority, so we must
6617 differentiate based on errno settings. */
6618 errno = 0;
6619 ret = getpriority(arg1, arg2);
6620 if (ret == -1 && errno != 0) {
6621 ret = -host_to_target_errno(errno);
6622 break;
6623 }
6624#ifdef TARGET_ALPHA
6625 /* Return value is the unbiased priority. Signal no error. */
6626 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
6627#else
6628 /* Return value is a biased priority to avoid negative numbers. */
6629 ret = 20 - ret;
6630#endif
bellard31e31b82003-02-18 22:55:36 +00006631 break;
6632 case TARGET_NR_setpriority:
6633 ret = get_errno(setpriority(arg1, arg2, arg3));
6634 break;
bellardebc05482003-09-30 21:08:41 +00006635#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00006636 case TARGET_NR_profil:
6637 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006638#endif
bellard31e31b82003-02-18 22:55:36 +00006639 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00006640 if (!(p = lock_user_string(arg1)))
6641 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006642 ret = get_errno(statfs(path(p), &stfs));
6643 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006644 convert_statfs:
6645 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006646 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006647
bellard579a97f2007-11-11 14:26:47 +00006648 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6649 goto efault;
6650 __put_user(stfs.f_type, &target_stfs->f_type);
6651 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6652 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6653 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6654 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6655 __put_user(stfs.f_files, &target_stfs->f_files);
6656 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6657 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6658 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6659 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02006660 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6661 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +00006662 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006663 }
6664 break;
6665 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00006666 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00006667 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00006668#ifdef TARGET_NR_statfs64
6669 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00006670 if (!(p = lock_user_string(arg1)))
6671 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006672 ret = get_errno(statfs(path(p), &stfs));
6673 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00006674 convert_statfs64:
6675 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006676 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006677
bellard579a97f2007-11-11 14:26:47 +00006678 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6679 goto efault;
6680 __put_user(stfs.f_type, &target_stfs->f_type);
6681 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6682 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6683 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6684 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6685 __put_user(stfs.f_files, &target_stfs->f_files);
6686 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6687 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6688 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6689 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02006690 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6691 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +00006692 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00006693 }
6694 break;
6695 case TARGET_NR_fstatfs64:
6696 ret = get_errno(fstatfs(arg1, &stfs));
6697 goto convert_statfs64;
6698#endif
bellardebc05482003-09-30 21:08:41 +00006699#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00006700 case TARGET_NR_ioperm:
6701 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006702#endif
thse5febef2007-04-01 18:31:35 +00006703#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00006704 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00006705 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00006706 break;
thse5febef2007-04-01 18:31:35 +00006707#endif
bellard3532fa72006-06-24 15:06:03 +00006708#ifdef TARGET_NR_accept
6709 case TARGET_NR_accept:
Peter Maydella94b4982013-02-08 04:35:04 +00006710 ret = do_accept4(arg1, arg2, arg3, 0);
6711 break;
6712#endif
6713#ifdef TARGET_NR_accept4
6714 case TARGET_NR_accept4:
6715#ifdef CONFIG_ACCEPT4
6716 ret = do_accept4(arg1, arg2, arg3, arg4);
6717#else
6718 goto unimplemented;
6719#endif
bellard3532fa72006-06-24 15:06:03 +00006720 break;
6721#endif
6722#ifdef TARGET_NR_bind
6723 case TARGET_NR_bind:
6724 ret = do_bind(arg1, arg2, arg3);
6725 break;
6726#endif
6727#ifdef TARGET_NR_connect
6728 case TARGET_NR_connect:
6729 ret = do_connect(arg1, arg2, arg3);
6730 break;
6731#endif
6732#ifdef TARGET_NR_getpeername
6733 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00006734 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006735 break;
6736#endif
6737#ifdef TARGET_NR_getsockname
6738 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00006739 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006740 break;
6741#endif
6742#ifdef TARGET_NR_getsockopt
6743 case TARGET_NR_getsockopt:
6744 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6745 break;
6746#endif
6747#ifdef TARGET_NR_listen
6748 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00006749 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006750 break;
6751#endif
6752#ifdef TARGET_NR_recv
6753 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00006754 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006755 break;
6756#endif
6757#ifdef TARGET_NR_recvfrom
6758 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00006759 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006760 break;
6761#endif
6762#ifdef TARGET_NR_recvmsg
6763 case TARGET_NR_recvmsg:
6764 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6765 break;
6766#endif
6767#ifdef TARGET_NR_send
6768 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00006769 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006770 break;
6771#endif
6772#ifdef TARGET_NR_sendmsg
6773 case TARGET_NR_sendmsg:
6774 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6775 break;
6776#endif
Alexander Graff19e00d2014-03-02 19:36:42 +00006777#ifdef TARGET_NR_sendmmsg
6778 case TARGET_NR_sendmmsg:
6779 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
6780 break;
6781 case TARGET_NR_recvmmsg:
6782 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
6783 break;
6784#endif
bellard3532fa72006-06-24 15:06:03 +00006785#ifdef TARGET_NR_sendto
6786 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00006787 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006788 break;
6789#endif
6790#ifdef TARGET_NR_shutdown
6791 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00006792 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006793 break;
6794#endif
6795#ifdef TARGET_NR_socket
6796 case TARGET_NR_socket:
6797 ret = do_socket(arg1, arg2, arg3);
6798 break;
6799#endif
6800#ifdef TARGET_NR_socketpair
6801 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00006802 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00006803 break;
6804#endif
6805#ifdef TARGET_NR_setsockopt
6806 case TARGET_NR_setsockopt:
6807 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6808 break;
6809#endif
ths7494b0f2007-02-11 18:26:53 +00006810
bellard31e31b82003-02-18 22:55:36 +00006811 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00006812 if (!(p = lock_user_string(arg2)))
6813 goto efault;
thse5574482007-02-11 20:03:13 +00006814 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6815 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00006816 break;
6817
bellard31e31b82003-02-18 22:55:36 +00006818 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00006819 {
bellard66fb9762003-03-23 01:06:05 +00006820 struct itimerval value, ovalue, *pvalue;
6821
pbrook53a59602006-03-25 19:31:22 +00006822 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006823 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00006824 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6825 || copy_from_user_timeval(&pvalue->it_value,
6826 arg2 + sizeof(struct target_timeval)))
6827 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006828 } else {
6829 pvalue = NULL;
6830 }
6831 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00006832 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00006833 if (copy_to_user_timeval(arg3,
6834 &ovalue.it_interval)
6835 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6836 &ovalue.it_value))
6837 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006838 }
6839 }
6840 break;
bellard31e31b82003-02-18 22:55:36 +00006841 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00006842 {
bellard66fb9762003-03-23 01:06:05 +00006843 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00006844
bellard66fb9762003-03-23 01:06:05 +00006845 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00006846 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00006847 if (copy_to_user_timeval(arg2,
6848 &value.it_interval)
6849 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6850 &value.it_value))
6851 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006852 }
6853 }
6854 break;
bellard31e31b82003-02-18 22:55:36 +00006855 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00006856 if (!(p = lock_user_string(arg1)))
6857 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006858 ret = get_errno(stat(path(p), &st));
6859 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006860 goto do_stat;
6861 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00006862 if (!(p = lock_user_string(arg1)))
6863 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006864 ret = get_errno(lstat(path(p), &st));
6865 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006866 goto do_stat;
6867 case TARGET_NR_fstat:
6868 {
6869 ret = get_errno(fstat(arg1, &st));
6870 do_stat:
6871 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006872 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00006873
bellard579a97f2007-11-11 14:26:47 +00006874 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6875 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02006876 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00006877 __put_user(st.st_dev, &target_st->st_dev);
6878 __put_user(st.st_ino, &target_st->st_ino);
6879 __put_user(st.st_mode, &target_st->st_mode);
6880 __put_user(st.st_uid, &target_st->st_uid);
6881 __put_user(st.st_gid, &target_st->st_gid);
6882 __put_user(st.st_nlink, &target_st->st_nlink);
6883 __put_user(st.st_rdev, &target_st->st_rdev);
6884 __put_user(st.st_size, &target_st->st_size);
6885 __put_user(st.st_blksize, &target_st->st_blksize);
6886 __put_user(st.st_blocks, &target_st->st_blocks);
6887 __put_user(st.st_atime, &target_st->target_st_atime);
6888 __put_user(st.st_mtime, &target_st->target_st_mtime);
6889 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00006890 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006891 }
6892 }
6893 break;
bellardebc05482003-09-30 21:08:41 +00006894#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00006895 case TARGET_NR_olduname:
6896 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006897#endif
6898#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00006899 case TARGET_NR_iopl:
6900 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006901#endif
bellard31e31b82003-02-18 22:55:36 +00006902 case TARGET_NR_vhangup:
6903 ret = get_errno(vhangup());
6904 break;
bellardebc05482003-09-30 21:08:41 +00006905#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00006906 case TARGET_NR_idle:
6907 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006908#endif
bellard42ad6ae2005-01-03 22:48:11 +00006909#ifdef TARGET_NR_syscall
6910 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01006911 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6912 arg6, arg7, arg8, 0);
6913 break;
bellard42ad6ae2005-01-03 22:48:11 +00006914#endif
bellard31e31b82003-02-18 22:55:36 +00006915 case TARGET_NR_wait4:
6916 {
6917 int status;
blueswir1992f48a2007-10-14 16:27:31 +00006918 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00006919 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00006920 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00006921 if (target_rusage)
6922 rusage_ptr = &rusage;
6923 else
6924 rusage_ptr = NULL;
6925 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6926 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01006927 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00006928 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00006929 if (put_user_s32(status, status_ptr))
6930 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006931 }
bellard2f619692007-11-16 10:46:05 +00006932 if (target_rusage)
6933 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00006934 }
6935 }
6936 break;
thse5febef2007-04-01 18:31:35 +00006937#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00006938 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00006939 if (!(p = lock_user_string(arg1)))
6940 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006941 ret = get_errno(swapoff(p));
6942 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006943 break;
thse5febef2007-04-01 18:31:35 +00006944#endif
bellard31e31b82003-02-18 22:55:36 +00006945 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00006946 {
pbrook53a59602006-03-25 19:31:22 +00006947 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00006948 struct sysinfo value;
6949 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00006950 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00006951 {
bellard579a97f2007-11-11 14:26:47 +00006952 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6953 goto efault;
bellarda5448a72004-06-19 16:59:03 +00006954 __put_user(value.uptime, &target_value->uptime);
6955 __put_user(value.loads[0], &target_value->loads[0]);
6956 __put_user(value.loads[1], &target_value->loads[1]);
6957 __put_user(value.loads[2], &target_value->loads[2]);
6958 __put_user(value.totalram, &target_value->totalram);
6959 __put_user(value.freeram, &target_value->freeram);
6960 __put_user(value.sharedram, &target_value->sharedram);
6961 __put_user(value.bufferram, &target_value->bufferram);
6962 __put_user(value.totalswap, &target_value->totalswap);
6963 __put_user(value.freeswap, &target_value->freeswap);
6964 __put_user(value.procs, &target_value->procs);
6965 __put_user(value.totalhigh, &target_value->totalhigh);
6966 __put_user(value.freehigh, &target_value->freehigh);
6967 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00006968 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00006969 }
6970 }
6971 break;
thse5febef2007-04-01 18:31:35 +00006972#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00006973 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00006974 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6975 break;
thse5febef2007-04-01 18:31:35 +00006976#endif
aurel32e5289082009-04-18 16:16:12 +00006977#ifdef TARGET_NR_semget
6978 case TARGET_NR_semget:
6979 ret = get_errno(semget(arg1, arg2, arg3));
6980 break;
6981#endif
6982#ifdef TARGET_NR_semop
6983 case TARGET_NR_semop:
Petar Jovanovicc7128c92013-03-21 07:57:36 +00006984 ret = do_semop(arg1, arg2, arg3);
aurel32e5289082009-04-18 16:16:12 +00006985 break;
6986#endif
6987#ifdef TARGET_NR_semctl
6988 case TARGET_NR_semctl:
6989 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6990 break;
6991#endif
aurel32eeb438c2008-10-13 21:08:55 +00006992#ifdef TARGET_NR_msgctl
6993 case TARGET_NR_msgctl:
6994 ret = do_msgctl(arg1, arg2, arg3);
6995 break;
6996#endif
6997#ifdef TARGET_NR_msgget
6998 case TARGET_NR_msgget:
6999 ret = get_errno(msgget(arg1, arg2));
7000 break;
7001#endif
7002#ifdef TARGET_NR_msgrcv
7003 case TARGET_NR_msgrcv:
7004 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
7005 break;
7006#endif
7007#ifdef TARGET_NR_msgsnd
7008 case TARGET_NR_msgsnd:
7009 ret = do_msgsnd(arg1, arg2, arg3, arg4);
7010 break;
7011#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03007012#ifdef TARGET_NR_shmget
7013 case TARGET_NR_shmget:
7014 ret = get_errno(shmget(arg1, arg2, arg3));
7015 break;
7016#endif
7017#ifdef TARGET_NR_shmctl
7018 case TARGET_NR_shmctl:
7019 ret = do_shmctl(arg1, arg2, arg3);
7020 break;
7021#endif
7022#ifdef TARGET_NR_shmat
7023 case TARGET_NR_shmat:
7024 ret = do_shmat(arg1, arg2, arg3);
7025 break;
7026#endif
7027#ifdef TARGET_NR_shmdt
7028 case TARGET_NR_shmdt:
7029 ret = do_shmdt(arg1);
7030 break;
7031#endif
bellard31e31b82003-02-18 22:55:36 +00007032 case TARGET_NR_fsync:
7033 ret = get_errno(fsync(arg1));
7034 break;
bellard31e31b82003-02-18 22:55:36 +00007035 case TARGET_NR_clone:
Peter Maydell4ce62432013-07-16 18:44:57 +01007036 /* Linux manages to have three different orderings for its
7037 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7038 * match the kernel's CONFIG_CLONE_* settings.
7039 * Microblaze is further special in that it uses a sixth
7040 * implicit argument to clone for the TLS pointer.
7041 */
7042#if defined(TARGET_MICROBLAZE)
Edgar E. Iglesiasa5b3bdc2012-04-26 14:17:41 +02007043 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
Peter Maydell4ce62432013-07-16 18:44:57 +01007044#elif defined(TARGET_CLONE_BACKWARDS)
7045 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
7046#elif defined(TARGET_CLONE_BACKWARDS2)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02007047 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00007048#else
Peter Maydell4ce62432013-07-16 18:44:57 +01007049 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00007050#endif
bellard1b6b0292003-03-22 17:31:38 +00007051 break;
bellardec86b0f2003-04-11 00:15:04 +00007052#ifdef __NR_exit_group
7053 /* new thread calls */
7054 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02007055#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00007056 _mcleanup();
7057#endif
bellarde9009672005-04-26 20:42:36 +00007058 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00007059 ret = get_errno(exit_group(arg1));
7060 break;
7061#endif
bellard31e31b82003-02-18 22:55:36 +00007062 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00007063 if (!(p = lock_user_string(arg1)))
7064 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007065 ret = get_errno(setdomainname(p, arg2));
7066 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00007067 break;
7068 case TARGET_NR_uname:
7069 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00007070 {
7071 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00007072
bellard579a97f2007-11-11 14:26:47 +00007073 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
7074 goto efault;
bellard29e619b2004-09-13 21:41:04 +00007075 ret = get_errno(sys_uname(buf));
7076 if (!is_error(ret)) {
7077 /* Overrite the native machine name with whatever is being
7078 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01007079 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00007080 /* Allow the user to override the reported release. */
7081 if (qemu_uname_release && *qemu_uname_release)
7082 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00007083 }
pbrook53a59602006-03-25 19:31:22 +00007084 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00007085 }
bellard31e31b82003-02-18 22:55:36 +00007086 break;
bellard6dbad632003-03-16 18:05:05 +00007087#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00007088 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00007089 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00007090 break;
j_mayer84409dd2007-04-06 08:56:50 +00007091#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00007092 case TARGET_NR_vm86old:
7093 goto unimplemented;
7094 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00007095 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00007096 break;
7097#endif
j_mayer84409dd2007-04-06 08:56:50 +00007098#endif
bellard31e31b82003-02-18 22:55:36 +00007099 case TARGET_NR_adjtimex:
7100 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007101#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00007102 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00007103#endif
bellard31e31b82003-02-18 22:55:36 +00007104 case TARGET_NR_init_module:
7105 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00007106#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00007107 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00007108#endif
bellard31e31b82003-02-18 22:55:36 +00007109 goto unimplemented;
7110 case TARGET_NR_quotactl:
7111 goto unimplemented;
7112 case TARGET_NR_getpgid:
7113 ret = get_errno(getpgid(arg1));
7114 break;
7115 case TARGET_NR_fchdir:
7116 ret = get_errno(fchdir(arg1));
7117 break;
j_mayer84409dd2007-04-06 08:56:50 +00007118#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00007119 case TARGET_NR_bdflush:
7120 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00007121#endif
thse5febef2007-04-01 18:31:35 +00007122#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00007123 case TARGET_NR_sysfs:
7124 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007125#endif
bellard31e31b82003-02-18 22:55:36 +00007126 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00007127 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00007128 break;
thse5febef2007-04-01 18:31:35 +00007129#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00007130 case TARGET_NR_afs_syscall:
7131 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007132#endif
j_mayer7a3148a2007-04-05 07:13:51 +00007133#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00007134 case TARGET_NR__llseek:
7135 {
7136 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00007137#if !defined(__NR_llseek)
7138 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7139 if (res == -1) {
7140 ret = get_errno(res);
7141 } else {
7142 ret = 0;
7143 }
7144#else
bellard31e31b82003-02-18 22:55:36 +00007145 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00007146#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00007147 if ((ret == 0) && put_user_s64(res, arg4)) {
7148 goto efault;
7149 }
bellard31e31b82003-02-18 22:55:36 +00007150 }
7151 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007152#endif
bellard31e31b82003-02-18 22:55:36 +00007153 case TARGET_NR_getdents:
Peter Maydell3307e232013-06-12 16:20:21 +01007154#ifdef __NR_getdents
Ulrich Hechtd83c8732009-07-24 19:10:28 +02007155#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00007156 {
pbrook53a59602006-03-25 19:31:22 +00007157 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00007158 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007159 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00007160
7161 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00007162 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00007163 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00007164 goto fail;
7165 }
ths3b46e622007-09-17 08:09:54 +00007166
bellard4add45b2003-06-05 01:52:59 +00007167 ret = get_errno(sys_getdents(arg1, dirp, count));
7168 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007169 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00007170 struct target_dirent *tde;
7171 int len = ret;
7172 int reclen, treclen;
7173 int count1, tnamelen;
7174
7175 count1 = 0;
7176 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00007177 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7178 goto efault;
bellard4add45b2003-06-05 01:52:59 +00007179 tde = target_dirp;
7180 while (len > 0) {
7181 reclen = de->d_reclen;
Dmitry V. Levin333858b2012-08-21 02:13:12 +04007182 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
7183 assert(tnamelen >= 0);
7184 treclen = tnamelen + offsetof(struct target_dirent, d_name);
7185 assert(count1 + treclen <= count);
bellard4add45b2003-06-05 01:52:59 +00007186 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02007187 tde->d_ino = tswapal(de->d_ino);
7188 tde->d_off = tswapal(de->d_off);
Dmitry V. Levin333858b2012-08-21 02:13:12 +04007189 memcpy(tde->d_name, de->d_name, tnamelen);
aurel326556a832008-10-13 21:08:17 +00007190 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00007191 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00007192 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00007193 count1 += treclen;
7194 }
7195 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00007196 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00007197 }
7198 free(dirp);
7199 }
7200#else
bellard31e31b82003-02-18 22:55:36 +00007201 {
aurel326556a832008-10-13 21:08:17 +00007202 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007203 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00007204
bellard579a97f2007-11-11 14:26:47 +00007205 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7206 goto efault;
bellard72f03902003-02-18 23:33:18 +00007207 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00007208 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007209 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00007210 int len = ret;
7211 int reclen;
7212 de = dirp;
7213 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007214 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00007215 if (reclen > len)
7216 break;
bellard8083a3e2003-03-24 23:12:16 +00007217 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00007218 tswapls(&de->d_ino);
7219 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007220 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00007221 len -= reclen;
7222 }
7223 }
pbrook53a59602006-03-25 19:31:22 +00007224 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00007225 }
bellard4add45b2003-06-05 01:52:59 +00007226#endif
Peter Maydell3307e232013-06-12 16:20:21 +01007227#else
7228 /* Implement getdents in terms of getdents64 */
7229 {
7230 struct linux_dirent64 *dirp;
7231 abi_long count = arg3;
7232
7233 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
7234 if (!dirp) {
7235 goto efault;
7236 }
7237 ret = get_errno(sys_getdents64(arg1, dirp, count));
7238 if (!is_error(ret)) {
7239 /* Convert the dirent64 structs to target dirent. We do this
7240 * in-place, since we can guarantee that a target_dirent is no
7241 * larger than a dirent64; however this means we have to be
7242 * careful to read everything before writing in the new format.
7243 */
7244 struct linux_dirent64 *de;
7245 struct target_dirent *tde;
7246 int len = ret;
7247 int tlen = 0;
7248
7249 de = dirp;
7250 tde = (struct target_dirent *)dirp;
7251 while (len > 0) {
7252 int namelen, treclen;
7253 int reclen = de->d_reclen;
7254 uint64_t ino = de->d_ino;
7255 int64_t off = de->d_off;
7256 uint8_t type = de->d_type;
7257
7258 namelen = strlen(de->d_name);
7259 treclen = offsetof(struct target_dirent, d_name)
7260 + namelen + 2;
7261 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
7262
7263 memmove(tde->d_name, de->d_name, namelen + 1);
7264 tde->d_ino = tswapal(ino);
7265 tde->d_off = tswapal(off);
7266 tde->d_reclen = tswap16(treclen);
7267 /* The target_dirent type is in what was formerly a padding
7268 * byte at the end of the structure:
7269 */
7270 *(((char *)tde) + treclen - 1) = type;
7271
7272 de = (struct linux_dirent64 *)((char *)de + reclen);
7273 tde = (struct target_dirent *)((char *)tde + treclen);
7274 len -= reclen;
7275 tlen += treclen;
7276 }
7277 ret = tlen;
7278 }
7279 unlock_user(dirp, arg2, ret);
7280 }
7281#endif
bellard31e31b82003-02-18 22:55:36 +00007282 break;
ths3ae43202007-09-16 21:39:48 +00007283#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00007284 case TARGET_NR_getdents64:
7285 {
aurel326556a832008-10-13 21:08:17 +00007286 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007287 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00007288 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7289 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00007290 ret = get_errno(sys_getdents64(arg1, dirp, count));
7291 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007292 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00007293 int len = ret;
7294 int reclen;
7295 de = dirp;
7296 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007297 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00007298 if (reclen > len)
7299 break;
bellard8083a3e2003-03-24 23:12:16 +00007300 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00007301 tswap64s((uint64_t *)&de->d_ino);
7302 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007303 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00007304 len -= reclen;
7305 }
7306 }
pbrook53a59602006-03-25 19:31:22 +00007307 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00007308 }
7309 break;
bellarda541f292004-04-12 20:39:29 +00007310#endif /* TARGET_NR_getdents64 */
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007311#if defined(TARGET_NR__newselect)
bellard31e31b82003-02-18 22:55:36 +00007312 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00007313 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00007314 break;
thse5febef2007-04-01 18:31:35 +00007315#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05007316#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7317# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00007318 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05007319# endif
7320# ifdef TARGET_NR_ppoll
7321 case TARGET_NR_ppoll:
7322# endif
bellard9de5e442003-03-23 16:49:39 +00007323 {
pbrook53a59602006-03-25 19:31:22 +00007324 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00007325 unsigned int nfds = arg2;
7326 int timeout = arg3;
7327 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00007328 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00007329
bellard579a97f2007-11-11 14:26:47 +00007330 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7331 if (!target_pfd)
7332 goto efault;
Mike Frysingerd8035d42011-02-07 01:05:51 -05007333
bellard9de5e442003-03-23 16:49:39 +00007334 pfd = alloca(sizeof(struct pollfd) * nfds);
7335 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007336 pfd[i].fd = tswap32(target_pfd[i].fd);
7337 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00007338 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05007339
7340# ifdef TARGET_NR_ppoll
7341 if (num == TARGET_NR_ppoll) {
7342 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7343 target_sigset_t *target_set;
7344 sigset_t _set, *set = &_set;
7345
7346 if (arg3) {
7347 if (target_to_host_timespec(timeout_ts, arg3)) {
7348 unlock_user(target_pfd, arg1, 0);
7349 goto efault;
7350 }
7351 } else {
7352 timeout_ts = NULL;
7353 }
7354
7355 if (arg4) {
7356 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7357 if (!target_set) {
7358 unlock_user(target_pfd, arg1, 0);
7359 goto efault;
7360 }
7361 target_to_host_sigset(set, target_set);
7362 } else {
7363 set = NULL;
7364 }
7365
7366 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7367
7368 if (!is_error(ret) && arg3) {
7369 host_to_target_timespec(arg3, timeout_ts);
7370 }
7371 if (arg4) {
7372 unlock_user(target_set, arg4, 0);
7373 }
7374 } else
7375# endif
7376 ret = get_errno(poll(pfd, nfds, timeout));
7377
bellard9de5e442003-03-23 16:49:39 +00007378 if (!is_error(ret)) {
7379 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007380 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00007381 }
7382 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00007383 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00007384 }
7385 break;
thse5febef2007-04-01 18:31:35 +00007386#endif
bellard31e31b82003-02-18 22:55:36 +00007387 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00007388 /* NOTE: the flock constant seems to be the same for every
7389 Linux platform */
7390 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00007391 break;
7392 case TARGET_NR_readv:
7393 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07007394 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
7395 if (vec != NULL) {
7396 ret = get_errno(readv(arg1, vec, arg3));
7397 unlock_iovec(vec, arg2, arg3, 1);
7398 } else {
7399 ret = -host_to_target_errno(errno);
7400 }
bellard31e31b82003-02-18 22:55:36 +00007401 }
7402 break;
7403 case TARGET_NR_writev:
7404 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07007405 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
7406 if (vec != NULL) {
7407 ret = get_errno(writev(arg1, vec, arg3));
7408 unlock_iovec(vec, arg2, arg3, 0);
7409 } else {
7410 ret = -host_to_target_errno(errno);
7411 }
bellard31e31b82003-02-18 22:55:36 +00007412 }
7413 break;
7414 case TARGET_NR_getsid:
7415 ret = get_errno(getsid(arg1));
7416 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007417#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00007418 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00007419 ret = get_errno(fdatasync(arg1));
7420 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007421#endif
bellard31e31b82003-02-18 22:55:36 +00007422 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00007423 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00007424 return value. */
ths0da46a62007-10-20 20:23:07 +00007425 ret = -TARGET_ENOTDIR;
7426 break;
Mike Frysinger737de1d2011-02-07 01:05:55 -05007427 case TARGET_NR_sched_getaffinity:
7428 {
7429 unsigned int mask_size;
7430 unsigned long *mask;
7431
7432 /*
7433 * sched_getaffinity needs multiples of ulong, so need to take
7434 * care of mismatches between target ulong and host ulong sizes.
7435 */
7436 if (arg2 & (sizeof(abi_ulong) - 1)) {
7437 ret = -TARGET_EINVAL;
7438 break;
7439 }
7440 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7441
7442 mask = alloca(mask_size);
7443 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7444
7445 if (!is_error(ret)) {
Mike McCormackcd18f052011-04-18 14:43:36 +09007446 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05007447 goto efault;
7448 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05007449 }
7450 }
7451 break;
7452 case TARGET_NR_sched_setaffinity:
7453 {
7454 unsigned int mask_size;
7455 unsigned long *mask;
7456
7457 /*
7458 * sched_setaffinity needs multiples of ulong, so need to take
7459 * care of mismatches between target ulong and host ulong sizes.
7460 */
7461 if (arg2 & (sizeof(abi_ulong) - 1)) {
7462 ret = -TARGET_EINVAL;
7463 break;
7464 }
7465 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7466
7467 mask = alloca(mask_size);
7468 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7469 goto efault;
7470 }
7471 memcpy(mask, p, arg2);
7472 unlock_user_struct(p, arg2, 0);
7473
7474 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7475 }
7476 break;
bellard31e31b82003-02-18 22:55:36 +00007477 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00007478 {
pbrook53a59602006-03-25 19:31:22 +00007479 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007480 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00007481
bellard579a97f2007-11-11 14:26:47 +00007482 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7483 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007484 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007485 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00007486 ret = get_errno(sched_setparam(arg1, &schp));
7487 }
7488 break;
bellard31e31b82003-02-18 22:55:36 +00007489 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00007490 {
pbrook53a59602006-03-25 19:31:22 +00007491 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007492 struct sched_param schp;
7493 ret = get_errno(sched_getparam(arg1, &schp));
7494 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007495 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7496 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007497 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007498 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00007499 }
7500 }
7501 break;
bellard31e31b82003-02-18 22:55:36 +00007502 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00007503 {
pbrook53a59602006-03-25 19:31:22 +00007504 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007505 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00007506 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7507 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007508 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007509 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00007510 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7511 }
7512 break;
bellard31e31b82003-02-18 22:55:36 +00007513 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00007514 ret = get_errno(sched_getscheduler(arg1));
7515 break;
bellard31e31b82003-02-18 22:55:36 +00007516 case TARGET_NR_sched_yield:
7517 ret = get_errno(sched_yield());
7518 break;
7519 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00007520 ret = get_errno(sched_get_priority_max(arg1));
7521 break;
bellard31e31b82003-02-18 22:55:36 +00007522 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00007523 ret = get_errno(sched_get_priority_min(arg1));
7524 break;
bellard31e31b82003-02-18 22:55:36 +00007525 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00007526 {
bellard5cd43932003-03-29 16:54:36 +00007527 struct timespec ts;
7528 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7529 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007530 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00007531 }
7532 }
7533 break;
bellard31e31b82003-02-18 22:55:36 +00007534 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00007535 {
bellard1b6b0292003-03-22 17:31:38 +00007536 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00007537 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00007538 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00007539 if (is_error(ret) && arg2) {
7540 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00007541 }
7542 }
7543 break;
thse5febef2007-04-01 18:31:35 +00007544#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00007545 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00007546 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007547#endif
7548#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00007549 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00007550 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007551#endif
bellard31e31b82003-02-18 22:55:36 +00007552 case TARGET_NR_prctl:
Peter Maydell1e6722f2012-02-03 14:48:03 +00007553 switch (arg1) {
7554 case PR_GET_PDEATHSIG:
7555 {
7556 int deathsig;
7557 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
7558 if (!is_error(ret) && arg2
7559 && put_user_ual(deathsig, arg2)) {
7560 goto efault;
thse5574482007-02-11 20:03:13 +00007561 }
Peter Maydell1e6722f2012-02-03 14:48:03 +00007562 break;
7563 }
Peter Maydelldb9526b2012-02-03 14:48:03 +00007564#ifdef PR_GET_NAME
7565 case PR_GET_NAME:
7566 {
7567 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
7568 if (!name) {
7569 goto efault;
7570 }
7571 ret = get_errno(prctl(arg1, (unsigned long)name,
7572 arg3, arg4, arg5));
7573 unlock_user(name, arg2, 16);
7574 break;
7575 }
7576 case PR_SET_NAME:
7577 {
7578 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
7579 if (!name) {
7580 goto efault;
7581 }
7582 ret = get_errno(prctl(arg1, (unsigned long)name,
7583 arg3, arg4, arg5));
7584 unlock_user(name, arg2, 0);
7585 break;
7586 }
7587#endif
Peter Maydell1e6722f2012-02-03 14:48:03 +00007588 default:
7589 /* Most prctl options have no pointer arguments */
7590 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7591 break;
7592 }
ths39b9aae2007-02-11 18:36:44 +00007593 break;
bellardd2fd1af2007-11-14 18:08:56 +00007594#ifdef TARGET_NR_arch_prctl
7595 case TARGET_NR_arch_prctl:
7596#if defined(TARGET_I386) && !defined(TARGET_ABI32)
7597 ret = do_arch_prctl(cpu_env, arg1, arg2);
7598 break;
7599#else
7600 goto unimplemented;
7601#endif
7602#endif
aurel32f2c7ba12008-03-28 22:32:06 +00007603#ifdef TARGET_NR_pread64
7604 case TARGET_NR_pread64:
Alexander Grafae017a52012-09-29 15:32:39 +00007605 if (regpairs_aligned(cpu_env)) {
7606 arg4 = arg5;
7607 arg5 = arg6;
7608 }
aurel32f2c7ba12008-03-28 22:32:06 +00007609 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7610 goto efault;
7611 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7612 unlock_user(p, arg2, ret);
7613 break;
7614 case TARGET_NR_pwrite64:
Alexander Grafae017a52012-09-29 15:32:39 +00007615 if (regpairs_aligned(cpu_env)) {
7616 arg4 = arg5;
7617 arg5 = arg6;
7618 }
aurel32f2c7ba12008-03-28 22:32:06 +00007619 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7620 goto efault;
7621 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7622 unlock_user(p, arg2, 0);
7623 break;
7624#endif
bellard31e31b82003-02-18 22:55:36 +00007625 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00007626 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7627 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007628 ret = get_errno(sys_getcwd1(p, arg2));
7629 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00007630 break;
7631 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00007632 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007633 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00007634 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007635 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00007636#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02007637 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
Jia Liud9627832012-07-20 15:50:52 +08007638 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
Andreas Färber9349b4f2012-03-14 01:38:32 +01007639 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00007640 break;
7641#else
bellard5cd43932003-03-29 16:54:36 +00007642 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00007643#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +00007644
7645#ifdef CONFIG_SENDFILE
bellard31e31b82003-02-18 22:55:36 +00007646 case TARGET_NR_sendfile:
Peter Maydella8fd1ab2013-02-08 07:31:55 +00007647 {
7648 off_t *offp = NULL;
7649 off_t off;
7650 if (arg3) {
7651 ret = get_user_sal(off, arg3);
7652 if (is_error(ret)) {
7653 break;
7654 }
7655 offp = &off;
7656 }
7657 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7658 if (!is_error(ret) && arg3) {
7659 abi_long ret2 = put_user_sal(off, arg3);
7660 if (is_error(ret2)) {
7661 ret = ret2;
7662 }
7663 }
7664 break;
7665 }
7666#ifdef TARGET_NR_sendfile64
7667 case TARGET_NR_sendfile64:
7668 {
7669 off_t *offp = NULL;
7670 off_t off;
7671 if (arg3) {
7672 ret = get_user_s64(off, arg3);
7673 if (is_error(ret)) {
7674 break;
7675 }
7676 offp = &off;
7677 }
7678 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7679 if (!is_error(ret) && arg3) {
7680 abi_long ret2 = put_user_s64(off, arg3);
7681 if (is_error(ret2)) {
7682 ret = ret2;
7683 }
7684 }
7685 break;
7686 }
7687#endif
7688#else
7689 case TARGET_NR_sendfile:
Peter Maydell7edd2cf2013-04-21 13:30:03 +01007690#ifdef TARGET_NR_sendfile64
Peter Maydella8fd1ab2013-02-08 07:31:55 +00007691 case TARGET_NR_sendfile64:
7692#endif
bellard5cd43932003-03-29 16:54:36 +00007693 goto unimplemented;
Peter Maydella8fd1ab2013-02-08 07:31:55 +00007694#endif
7695
bellardebc05482003-09-30 21:08:41 +00007696#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00007697 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00007698 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007699#endif
7700#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00007701 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00007702 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007703#endif
bellard048f6b42005-11-26 18:47:20 +00007704#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00007705 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00007706 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
7707 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00007708 break;
bellard048f6b42005-11-26 18:47:20 +00007709#endif
bellardebc05482003-09-30 21:08:41 +00007710#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00007711 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00007712 {
7713 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007714 int resource = target_to_host_resource(arg1);
7715 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00007716 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007717 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00007718 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7719 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007720 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7721 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007722 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00007723 }
7724 break;
7725 }
bellardebc05482003-09-30 21:08:41 +00007726#endif
bellarda315a142005-01-30 22:59:18 +00007727#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00007728 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00007729 if (!(p = lock_user_string(arg1)))
7730 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007731 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
7732 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00007733 break;
bellarda315a142005-01-30 22:59:18 +00007734#endif
7735#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00007736 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00007737 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00007738 break;
bellarda315a142005-01-30 22:59:18 +00007739#endif
7740#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00007741 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00007742 if (!(p = lock_user_string(arg1)))
7743 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007744 ret = get_errno(stat(path(p), &st));
7745 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007746 if (!is_error(ret))
7747 ret = host_to_target_stat64(cpu_env, arg2, &st);
7748 break;
bellarda315a142005-01-30 22:59:18 +00007749#endif
7750#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00007751 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00007752 if (!(p = lock_user_string(arg1)))
7753 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007754 ret = get_errno(lstat(path(p), &st));
7755 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007756 if (!is_error(ret))
7757 ret = host_to_target_stat64(cpu_env, arg2, &st);
7758 break;
bellarda315a142005-01-30 22:59:18 +00007759#endif
7760#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00007761 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00007762 ret = get_errno(fstat(arg1, &st));
7763 if (!is_error(ret))
7764 ret = host_to_target_stat64(cpu_env, arg2, &st);
7765 break;
bellardec86b0f2003-04-11 00:15:04 +00007766#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007767#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
aurel329d33b762009-04-08 23:07:05 +00007768#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007769 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00007770#endif
7771#ifdef TARGET_NR_newfstatat
7772 case TARGET_NR_newfstatat:
7773#endif
balrog6a24a772008-09-20 02:23:36 +00007774 if (!(p = lock_user_string(arg2)))
7775 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007776 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
balrog6a24a772008-09-20 02:23:36 +00007777 if (!is_error(ret))
7778 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00007779 break;
bellarda315a142005-01-30 22:59:18 +00007780#endif
bellard67867302003-11-23 17:05:30 +00007781 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00007782 if (!(p = lock_user_string(arg1)))
7783 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007784 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7785 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007786 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007787#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00007788 case TARGET_NR_getuid:
7789 ret = get_errno(high2lowuid(getuid()));
7790 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007791#endif
7792#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00007793 case TARGET_NR_getgid:
7794 ret = get_errno(high2lowgid(getgid()));
7795 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007796#endif
7797#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00007798 case TARGET_NR_geteuid:
7799 ret = get_errno(high2lowuid(geteuid()));
7800 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007801#endif
7802#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00007803 case TARGET_NR_getegid:
7804 ret = get_errno(high2lowgid(getegid()));
7805 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007806#endif
bellard67867302003-11-23 17:05:30 +00007807 case TARGET_NR_setreuid:
7808 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7809 break;
7810 case TARGET_NR_setregid:
7811 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7812 break;
7813 case TARGET_NR_getgroups:
7814 {
7815 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007816 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007817 gid_t *grouplist;
7818 int i;
7819
7820 grouplist = alloca(gidsetsize * sizeof(gid_t));
7821 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007822 if (gidsetsize == 0)
7823 break;
bellard67867302003-11-23 17:05:30 +00007824 if (!is_error(ret)) {
Andreas Schwab03903ff2013-04-09 05:41:33 +00007825 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
bellard579a97f2007-11-11 14:26:47 +00007826 if (!target_grouplist)
7827 goto efault;
balroga2155fc2008-09-20 02:12:08 +00007828 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007829 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
Andreas Schwab03903ff2013-04-09 05:41:33 +00007830 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
bellard67867302003-11-23 17:05:30 +00007831 }
7832 }
7833 break;
7834 case TARGET_NR_setgroups:
7835 {
7836 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007837 target_id *target_grouplist;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05007838 gid_t *grouplist = NULL;
bellard67867302003-11-23 17:05:30 +00007839 int i;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05007840 if (gidsetsize) {
7841 grouplist = alloca(gidsetsize * sizeof(gid_t));
Andreas Schwab03903ff2013-04-09 05:41:33 +00007842 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05007843 if (!target_grouplist) {
7844 ret = -TARGET_EFAULT;
7845 goto fail;
7846 }
7847 for (i = 0; i < gidsetsize; i++) {
7848 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
7849 }
7850 unlock_user(target_grouplist, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00007851 }
bellard67867302003-11-23 17:05:30 +00007852 ret = get_errno(setgroups(gidsetsize, grouplist));
7853 }
7854 break;
7855 case TARGET_NR_fchown:
7856 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7857 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007858#if defined(TARGET_NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +00007859 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00007860 if (!(p = lock_user_string(arg2)))
7861 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007862 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
7863 low2highgid(arg4), arg5));
bellard579a97f2007-11-11 14:26:47 +00007864 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00007865 break;
7866#endif
bellard67867302003-11-23 17:05:30 +00007867#ifdef TARGET_NR_setresuid
7868 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00007869 ret = get_errno(setresuid(low2highuid(arg1),
7870 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00007871 low2highuid(arg3)));
7872 break;
7873#endif
7874#ifdef TARGET_NR_getresuid
7875 case TARGET_NR_getresuid:
7876 {
pbrook53a59602006-03-25 19:31:22 +00007877 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00007878 ret = get_errno(getresuid(&ruid, &euid, &suid));
7879 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +00007880 if (put_user_id(high2lowuid(ruid), arg1)
7881 || put_user_id(high2lowuid(euid), arg2)
7882 || put_user_id(high2lowuid(suid), arg3))
bellard2f619692007-11-16 10:46:05 +00007883 goto efault;
bellard67867302003-11-23 17:05:30 +00007884 }
7885 }
7886 break;
7887#endif
7888#ifdef TARGET_NR_getresgid
7889 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00007890 ret = get_errno(setresgid(low2highgid(arg1),
7891 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00007892 low2highgid(arg3)));
7893 break;
7894#endif
7895#ifdef TARGET_NR_getresgid
7896 case TARGET_NR_getresgid:
7897 {
pbrook53a59602006-03-25 19:31:22 +00007898 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00007899 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7900 if (!is_error(ret)) {
Peter Maydell76ca3102014-03-02 19:36:41 +00007901 if (put_user_id(high2lowgid(rgid), arg1)
7902 || put_user_id(high2lowgid(egid), arg2)
7903 || put_user_id(high2lowgid(sgid), arg3))
bellard2f619692007-11-16 10:46:05 +00007904 goto efault;
bellard67867302003-11-23 17:05:30 +00007905 }
7906 }
7907 break;
7908#endif
7909 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00007910 if (!(p = lock_user_string(arg1)))
7911 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007912 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7913 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007914 break;
7915 case TARGET_NR_setuid:
7916 ret = get_errno(setuid(low2highuid(arg1)));
7917 break;
7918 case TARGET_NR_setgid:
7919 ret = get_errno(setgid(low2highgid(arg1)));
7920 break;
7921 case TARGET_NR_setfsuid:
7922 ret = get_errno(setfsuid(arg1));
7923 break;
7924 case TARGET_NR_setfsgid:
7925 ret = get_errno(setfsgid(arg1));
7926 break;
bellard67867302003-11-23 17:05:30 +00007927
bellarda315a142005-01-30 22:59:18 +00007928#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00007929 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00007930 if (!(p = lock_user_string(arg1)))
7931 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007932 ret = get_errno(lchown(p, arg2, arg3));
7933 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007934 break;
bellarda315a142005-01-30 22:59:18 +00007935#endif
7936#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00007937 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00007938 ret = get_errno(getuid());
7939 break;
bellarda315a142005-01-30 22:59:18 +00007940#endif
aurel3264b4d282008-11-14 17:20:15 +00007941
7942#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7943 /* Alpha specific */
7944 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007945 {
7946 uid_t euid;
7947 euid=geteuid();
7948 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7949 }
aurel3264b4d282008-11-14 17:20:15 +00007950 ret = get_errno(getuid());
7951 break;
7952#endif
7953#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7954 /* Alpha specific */
7955 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007956 {
7957 uid_t egid;
7958 egid=getegid();
7959 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7960 }
aurel3264b4d282008-11-14 17:20:15 +00007961 ret = get_errno(getgid());
7962 break;
7963#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08007964#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7965 /* Alpha specific */
7966 case TARGET_NR_osf_getsysinfo:
7967 ret = -TARGET_EOPNOTSUPP;
7968 switch (arg1) {
7969 case TARGET_GSI_IEEE_FP_CONTROL:
7970 {
7971 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7972
7973 /* Copied from linux ieee_fpcr_to_swcr. */
7974 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7975 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7976 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7977 | SWCR_TRAP_ENABLE_DZE
7978 | SWCR_TRAP_ENABLE_OVF);
7979 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
7980 | SWCR_TRAP_ENABLE_INE);
7981 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
7982 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
7983
7984 if (put_user_u64 (swcr, arg2))
7985 goto efault;
7986 ret = 0;
7987 }
7988 break;
7989
7990 /* case GSI_IEEE_STATE_AT_SIGNAL:
7991 -- Not implemented in linux kernel.
7992 case GSI_UACPROC:
7993 -- Retrieves current unaligned access state; not much used.
7994 case GSI_PROC_TYPE:
7995 -- Retrieves implver information; surely not used.
7996 case GSI_GET_HWRPB:
7997 -- Grabs a copy of the HWRPB; surely not used.
7998 */
7999 }
8000 break;
8001#endif
8002#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8003 /* Alpha specific */
8004 case TARGET_NR_osf_setsysinfo:
8005 ret = -TARGET_EOPNOTSUPP;
8006 switch (arg1) {
8007 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -08008008 {
8009 uint64_t swcr, fpcr, orig_fpcr;
8010
Richard Henderson6e06d512012-06-01 09:08:21 -07008011 if (get_user_u64 (swcr, arg2)) {
Richard Hendersonba0e2762009-12-09 15:56:29 -08008012 goto efault;
Richard Henderson6e06d512012-06-01 09:08:21 -07008013 }
8014 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008015 fpcr = orig_fpcr & FPCR_DYN_MASK;
8016
8017 /* Copied from linux ieee_swcr_to_fpcr. */
8018 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
8019 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
8020 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
8021 | SWCR_TRAP_ENABLE_DZE
8022 | SWCR_TRAP_ENABLE_OVF)) << 48;
8023 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
8024 | SWCR_TRAP_ENABLE_INE)) << 57;
8025 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
8026 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
8027
Richard Henderson6e06d512012-06-01 09:08:21 -07008028 cpu_alpha_store_fpcr(cpu_env, fpcr);
8029 ret = 0;
8030 }
8031 break;
8032
8033 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
8034 {
8035 uint64_t exc, fpcr, orig_fpcr;
8036 int si_code;
8037
8038 if (get_user_u64(exc, arg2)) {
8039 goto efault;
8040 }
8041
8042 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8043
8044 /* We only add to the exception status here. */
8045 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
8046
8047 cpu_alpha_store_fpcr(cpu_env, fpcr);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008048 ret = 0;
8049
Richard Henderson6e06d512012-06-01 09:08:21 -07008050 /* Old exceptions are not signaled. */
8051 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008052
Richard Henderson6e06d512012-06-01 09:08:21 -07008053 /* If any exceptions set by this call,
8054 and are unmasked, send a signal. */
8055 si_code = 0;
8056 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
8057 si_code = TARGET_FPE_FLTRES;
8058 }
8059 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
8060 si_code = TARGET_FPE_FLTUND;
8061 }
8062 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
8063 si_code = TARGET_FPE_FLTOVF;
8064 }
8065 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
8066 si_code = TARGET_FPE_FLTDIV;
8067 }
8068 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
8069 si_code = TARGET_FPE_FLTINV;
8070 }
8071 if (si_code != 0) {
8072 target_siginfo_t info;
8073 info.si_signo = SIGFPE;
8074 info.si_errno = 0;
8075 info.si_code = si_code;
8076 info._sifields._sigfault._addr
8077 = ((CPUArchState *)cpu_env)->pc;
8078 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008079 }
8080 }
8081 break;
8082
8083 /* case SSI_NVPAIRS:
8084 -- Used with SSIN_UACPROC to enable unaligned accesses.
8085 case SSI_IEEE_STATE_AT_SIGNAL:
8086 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8087 -- Not implemented in linux kernel
8088 */
8089 }
8090 break;
8091#endif
8092#ifdef TARGET_NR_osf_sigprocmask
8093 /* Alpha specific. */
8094 case TARGET_NR_osf_sigprocmask:
8095 {
8096 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01008097 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08008098 sigset_t set, oldset;
8099
8100 switch(arg1) {
8101 case TARGET_SIG_BLOCK:
8102 how = SIG_BLOCK;
8103 break;
8104 case TARGET_SIG_UNBLOCK:
8105 how = SIG_UNBLOCK;
8106 break;
8107 case TARGET_SIG_SETMASK:
8108 how = SIG_SETMASK;
8109 break;
8110 default:
8111 ret = -TARGET_EINVAL;
8112 goto fail;
8113 }
8114 mask = arg2;
8115 target_to_host_old_sigset(&set, &mask);
Juan Quintelabc088ba2011-06-16 17:37:10 +01008116 sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08008117 host_to_target_old_sigset(&mask, &oldset);
8118 ret = mask;
8119 }
8120 break;
8121#endif
aurel3264b4d282008-11-14 17:20:15 +00008122
bellarda315a142005-01-30 22:59:18 +00008123#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00008124 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00008125 ret = get_errno(getgid());
8126 break;
bellarda315a142005-01-30 22:59:18 +00008127#endif
8128#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00008129 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00008130 ret = get_errno(geteuid());
8131 break;
bellarda315a142005-01-30 22:59:18 +00008132#endif
8133#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00008134 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00008135 ret = get_errno(getegid());
8136 break;
bellarda315a142005-01-30 22:59:18 +00008137#endif
8138#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00008139 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00008140 ret = get_errno(setreuid(arg1, arg2));
8141 break;
bellarda315a142005-01-30 22:59:18 +00008142#endif
8143#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00008144 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00008145 ret = get_errno(setregid(arg1, arg2));
8146 break;
bellarda315a142005-01-30 22:59:18 +00008147#endif
8148#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00008149 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00008150 {
8151 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00008152 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00008153 gid_t *grouplist;
8154 int i;
8155
8156 grouplist = alloca(gidsetsize * sizeof(gid_t));
8157 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00008158 if (gidsetsize == 0)
8159 break;
bellard99c475a2005-01-31 20:45:13 +00008160 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00008161 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
8162 if (!target_grouplist) {
8163 ret = -TARGET_EFAULT;
8164 goto fail;
8165 }
balroga2155fc2008-09-20 02:12:08 +00008166 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00008167 target_grouplist[i] = tswap32(grouplist[i]);
8168 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00008169 }
8170 }
8171 break;
bellarda315a142005-01-30 22:59:18 +00008172#endif
8173#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00008174 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00008175 {
8176 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00008177 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00008178 gid_t *grouplist;
8179 int i;
ths3b46e622007-09-17 08:09:54 +00008180
bellard99c475a2005-01-31 20:45:13 +00008181 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00008182 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
8183 if (!target_grouplist) {
8184 ret = -TARGET_EFAULT;
8185 goto fail;
8186 }
bellard99c475a2005-01-31 20:45:13 +00008187 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00008188 grouplist[i] = tswap32(target_grouplist[i]);
8189 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00008190 ret = get_errno(setgroups(gidsetsize, grouplist));
8191 }
8192 break;
bellarda315a142005-01-30 22:59:18 +00008193#endif
8194#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00008195 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00008196 ret = get_errno(fchown(arg1, arg2, arg3));
8197 break;
bellarda315a142005-01-30 22:59:18 +00008198#endif
8199#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00008200 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00008201 ret = get_errno(setresuid(arg1, arg2, arg3));
8202 break;
bellarda315a142005-01-30 22:59:18 +00008203#endif
8204#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00008205 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00008206 {
pbrook53a59602006-03-25 19:31:22 +00008207 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00008208 ret = get_errno(getresuid(&ruid, &euid, &suid));
8209 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00008210 if (put_user_u32(ruid, arg1)
8211 || put_user_u32(euid, arg2)
8212 || put_user_u32(suid, arg3))
8213 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00008214 }
8215 }
8216 break;
bellarda315a142005-01-30 22:59:18 +00008217#endif
8218#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00008219 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00008220 ret = get_errno(setresgid(arg1, arg2, arg3));
8221 break;
bellarda315a142005-01-30 22:59:18 +00008222#endif
8223#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00008224 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00008225 {
pbrook53a59602006-03-25 19:31:22 +00008226 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00008227 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8228 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00008229 if (put_user_u32(rgid, arg1)
8230 || put_user_u32(egid, arg2)
8231 || put_user_u32(sgid, arg3))
8232 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00008233 }
8234 }
8235 break;
bellarda315a142005-01-30 22:59:18 +00008236#endif
8237#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00008238 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00008239 if (!(p = lock_user_string(arg1)))
8240 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008241 ret = get_errno(chown(p, arg2, arg3));
8242 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00008243 break;
bellarda315a142005-01-30 22:59:18 +00008244#endif
8245#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00008246 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00008247 ret = get_errno(setuid(arg1));
8248 break;
bellarda315a142005-01-30 22:59:18 +00008249#endif
8250#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00008251 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00008252 ret = get_errno(setgid(arg1));
8253 break;
bellarda315a142005-01-30 22:59:18 +00008254#endif
8255#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00008256 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00008257 ret = get_errno(setfsuid(arg1));
8258 break;
bellarda315a142005-01-30 22:59:18 +00008259#endif
8260#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00008261 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00008262 ret = get_errno(setfsgid(arg1));
8263 break;
bellarda315a142005-01-30 22:59:18 +00008264#endif
bellard67867302003-11-23 17:05:30 +00008265
bellard31e31b82003-02-18 22:55:36 +00008266 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00008267 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00008268#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00008269 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00008270 {
8271 void *a;
8272 ret = -TARGET_EFAULT;
8273 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8274 goto efault;
8275 if (!(p = lock_user_string(arg3)))
8276 goto mincore_fail;
8277 ret = get_errno(mincore(a, arg2, p));
8278 unlock_user(p, arg3, ret);
8279 mincore_fail:
8280 unlock_user(a, arg1, 0);
8281 }
8282 break;
bellardffa65c32004-01-04 23:57:22 +00008283#endif
aurel32408321b2008-10-01 21:46:32 +00008284#ifdef TARGET_NR_arm_fadvise64_64
8285 case TARGET_NR_arm_fadvise64_64:
8286 {
8287 /*
8288 * arm_fadvise64_64 looks like fadvise64_64 but
8289 * with different argument order
8290 */
8291 abi_long temp;
8292 temp = arg3;
8293 arg3 = arg4;
8294 arg4 = temp;
8295 }
8296#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008297#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00008298#ifdef TARGET_NR_fadvise64_64
8299 case TARGET_NR_fadvise64_64:
8300#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008301#ifdef TARGET_NR_fadvise64
8302 case TARGET_NR_fadvise64:
8303#endif
8304#ifdef TARGET_S390X
8305 switch (arg4) {
8306 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8307 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8308 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8309 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8310 default: break;
8311 }
8312#endif
8313 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00008314 break;
8315#endif
bellardffa65c32004-01-04 23:57:22 +00008316#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00008317 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00008318 /* A straight passthrough may not be safe because qemu sometimes
Lei Lid2d6b852013-05-20 17:20:50 +08008319 turns private file-backed mappings into anonymous mappings.
pbrook24836682006-04-16 14:14:53 +00008320 This will break MADV_DONTNEED.
8321 This is a hint, so ignoring and returning success is ok. */
8322 ret = get_errno(0);
8323 break;
bellardffa65c32004-01-04 23:57:22 +00008324#endif
blueswir1992f48a2007-10-14 16:27:31 +00008325#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00008326 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00008327 {
thsb1e341e2007-03-20 21:50:52 +00008328 int cmd;
bellard77e46722003-04-29 20:39:06 +00008329 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00008330 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00008331#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00008332 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00008333#endif
bellard77e46722003-04-29 20:39:06 +00008334
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008335 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00008336 if (cmd == -TARGET_EINVAL) {
8337 ret = cmd;
8338 break;
8339 }
thsb1e341e2007-03-20 21:50:52 +00008340
bellard60cd49d2003-03-16 22:53:56 +00008341 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00008342 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00008343#ifdef TARGET_ARM
8344 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008345 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8346 goto efault;
ths58134272007-03-31 18:59:32 +00008347 fl.l_type = tswap16(target_efl->l_type);
8348 fl.l_whence = tswap16(target_efl->l_whence);
8349 fl.l_start = tswap64(target_efl->l_start);
8350 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008351 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00008352 unlock_user_struct(target_efl, arg3, 0);
8353 } else
8354#endif
8355 {
bellard9ee1fa22007-11-11 15:11:19 +00008356 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8357 goto efault;
ths58134272007-03-31 18:59:32 +00008358 fl.l_type = tswap16(target_fl->l_type);
8359 fl.l_whence = tswap16(target_fl->l_whence);
8360 fl.l_start = tswap64(target_fl->l_start);
8361 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008362 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00008363 unlock_user_struct(target_fl, arg3, 0);
8364 }
thsb1e341e2007-03-20 21:50:52 +00008365 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008366 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00008367#ifdef TARGET_ARM
8368 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008369 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8370 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008371 target_efl->l_type = tswap16(fl.l_type);
8372 target_efl->l_whence = tswap16(fl.l_whence);
8373 target_efl->l_start = tswap64(fl.l_start);
8374 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008375 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008376 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008377 } else
8378#endif
8379 {
bellard9ee1fa22007-11-11 15:11:19 +00008380 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8381 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008382 target_fl->l_type = tswap16(fl.l_type);
8383 target_fl->l_whence = tswap16(fl.l_whence);
8384 target_fl->l_start = tswap64(fl.l_start);
8385 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008386 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008387 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008388 }
bellard77e46722003-04-29 20:39:06 +00008389 }
8390 break;
8391
thsb1e341e2007-03-20 21:50:52 +00008392 case TARGET_F_SETLK64:
8393 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00008394#ifdef TARGET_ARM
8395 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008396 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8397 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008398 fl.l_type = tswap16(target_efl->l_type);
8399 fl.l_whence = tswap16(target_efl->l_whence);
8400 fl.l_start = tswap64(target_efl->l_start);
8401 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008402 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008403 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008404 } else
8405#endif
8406 {
bellard9ee1fa22007-11-11 15:11:19 +00008407 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8408 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008409 fl.l_type = tswap16(target_fl->l_type);
8410 fl.l_whence = tswap16(target_fl->l_whence);
8411 fl.l_start = tswap64(target_fl->l_start);
8412 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008413 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008414 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008415 }
thsb1e341e2007-03-20 21:50:52 +00008416 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008417 break;
bellard60cd49d2003-03-16 22:53:56 +00008418 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008419 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00008420 break;
8421 }
bellard77e46722003-04-29 20:39:06 +00008422 break;
8423 }
bellard60cd49d2003-03-16 22:53:56 +00008424#endif
ths7d600c82006-12-08 01:32:58 +00008425#ifdef TARGET_NR_cacheflush
8426 case TARGET_NR_cacheflush:
8427 /* self-modifying code is handled automatically, so nothing needed */
8428 ret = 0;
8429 break;
8430#endif
bellardebc05482003-09-30 21:08:41 +00008431#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00008432 case TARGET_NR_security:
8433 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008434#endif
bellardc573ff62004-01-04 15:51:36 +00008435#ifdef TARGET_NR_getpagesize
8436 case TARGET_NR_getpagesize:
8437 ret = TARGET_PAGE_SIZE;
8438 break;
8439#endif
bellard31e31b82003-02-18 22:55:36 +00008440 case TARGET_NR_gettid:
8441 ret = get_errno(gettid());
8442 break;
thse5febef2007-04-01 18:31:35 +00008443#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00008444 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00008445#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +03008446 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +00008447 arg2 = arg3;
8448 arg3 = arg4;
8449 arg4 = arg5;
8450 }
aurel322054ac92008-10-13 21:08:07 +00008451 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8452#else
8453 ret = get_errno(readahead(arg1, arg2, arg3));
8454#endif
8455 break;
thse5febef2007-04-01 18:31:35 +00008456#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008457#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +00008458#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00008459 case TARGET_NR_listxattr:
8460 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +00008461 {
8462 void *p, *b = 0;
8463 if (arg2) {
8464 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8465 if (!b) {
8466 ret = -TARGET_EFAULT;
8467 break;
8468 }
8469 }
8470 p = lock_user_string(arg1);
8471 if (p) {
8472 if (num == TARGET_NR_listxattr) {
8473 ret = get_errno(listxattr(p, b, arg3));
8474 } else {
8475 ret = get_errno(llistxattr(p, b, arg3));
8476 }
8477 } else {
8478 ret = -TARGET_EFAULT;
8479 }
8480 unlock_user(p, arg1, 0);
8481 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +03008482 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +00008483 }
8484 case TARGET_NR_flistxattr:
8485 {
8486 void *b = 0;
8487 if (arg2) {
8488 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8489 if (!b) {
8490 ret = -TARGET_EFAULT;
8491 break;
8492 }
8493 }
8494 ret = get_errno(flistxattr(arg1, b, arg3));
8495 unlock_user(b, arg2, arg3);
8496 break;
8497 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008498 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008499 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008500 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008501 void *p, *n, *v = 0;
8502 if (arg3) {
8503 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8504 if (!v) {
8505 ret = -TARGET_EFAULT;
8506 break;
8507 }
8508 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008509 p = lock_user_string(arg1);
8510 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008511 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008512 if (num == TARGET_NR_setxattr) {
8513 ret = get_errno(setxattr(p, n, v, arg4, arg5));
8514 } else {
8515 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
8516 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008517 } else {
8518 ret = -TARGET_EFAULT;
8519 }
8520 unlock_user(p, arg1, 0);
8521 unlock_user(n, arg2, 0);
8522 unlock_user(v, arg3, 0);
8523 }
8524 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008525 case TARGET_NR_fsetxattr:
8526 {
8527 void *n, *v = 0;
8528 if (arg3) {
8529 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8530 if (!v) {
8531 ret = -TARGET_EFAULT;
8532 break;
8533 }
8534 }
8535 n = lock_user_string(arg2);
8536 if (n) {
8537 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
8538 } else {
8539 ret = -TARGET_EFAULT;
8540 }
8541 unlock_user(n, arg2, 0);
8542 unlock_user(v, arg3, 0);
8543 }
8544 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008545 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008546 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008547 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008548 void *p, *n, *v = 0;
8549 if (arg3) {
8550 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8551 if (!v) {
8552 ret = -TARGET_EFAULT;
8553 break;
8554 }
8555 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008556 p = lock_user_string(arg1);
8557 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008558 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008559 if (num == TARGET_NR_getxattr) {
8560 ret = get_errno(getxattr(p, n, v, arg4));
8561 } else {
8562 ret = get_errno(lgetxattr(p, n, v, arg4));
8563 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008564 } else {
8565 ret = -TARGET_EFAULT;
8566 }
8567 unlock_user(p, arg1, 0);
8568 unlock_user(n, arg2, 0);
8569 unlock_user(v, arg3, arg4);
8570 }
8571 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008572 case TARGET_NR_fgetxattr:
8573 {
8574 void *n, *v = 0;
8575 if (arg3) {
8576 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8577 if (!v) {
8578 ret = -TARGET_EFAULT;
8579 break;
8580 }
8581 }
8582 n = lock_user_string(arg2);
8583 if (n) {
8584 ret = get_errno(fgetxattr(arg1, n, v, arg4));
8585 } else {
8586 ret = -TARGET_EFAULT;
8587 }
8588 unlock_user(n, arg2, 0);
8589 unlock_user(v, arg3, arg4);
8590 }
8591 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008592 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008593 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008594 {
8595 void *p, *n;
8596 p = lock_user_string(arg1);
8597 n = lock_user_string(arg2);
8598 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008599 if (num == TARGET_NR_removexattr) {
8600 ret = get_errno(removexattr(p, n));
8601 } else {
8602 ret = get_errno(lremovexattr(p, n));
8603 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008604 } else {
8605 ret = -TARGET_EFAULT;
8606 }
8607 unlock_user(p, arg1, 0);
8608 unlock_user(n, arg2, 0);
8609 }
8610 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008611 case TARGET_NR_fremovexattr:
8612 {
8613 void *n;
8614 n = lock_user_string(arg2);
8615 if (n) {
8616 ret = get_errno(fremovexattr(arg1, n));
8617 } else {
8618 ret = -TARGET_EFAULT;
8619 }
8620 unlock_user(n, arg2, 0);
8621 }
8622 break;
bellardebc05482003-09-30 21:08:41 +00008623#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008624#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +00008625#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00008626 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008627#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00008628 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
8629 ret = 0;
8630 break;
edgar_iglef967792009-01-07 14:19:38 +00008631#elif defined(TARGET_CRIS)
8632 if (arg1 & 0xff)
8633 ret = -TARGET_EINVAL;
8634 else {
8635 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
8636 ret = 0;
8637 }
8638 break;
bellard8d18e892007-11-14 15:18:40 +00008639#elif defined(TARGET_I386) && defined(TARGET_ABI32)
8640 ret = do_set_thread_area(cpu_env, arg1);
8641 break;
Peter Maydell1ccd9372013-07-16 18:44:55 +01008642#elif defined(TARGET_M68K)
8643 {
8644 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
8645 ts->tp_value = arg1;
Peter Maydell95c1eb12013-07-29 11:31:49 +01008646 ret = 0;
Peter Maydell1ccd9372013-07-16 18:44:55 +01008647 break;
8648 }
ths6f5b89a2007-03-02 20:48:00 +00008649#else
8650 goto unimplemented_nowarn;
8651#endif
8652#endif
8653#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00008654 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008655#if defined(TARGET_I386) && defined(TARGET_ABI32)
8656 ret = do_get_thread_area(cpu_env, arg1);
Peter Maydelld312bbe2013-07-16 18:44:56 +01008657 break;
Peter Maydell1ccd9372013-07-16 18:44:55 +01008658#elif defined(TARGET_M68K)
8659 {
8660 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
8661 ret = ts->tp_value;
8662 break;
8663 }
bellard8d18e892007-11-14 15:18:40 +00008664#else
bellard5cd43932003-03-29 16:54:36 +00008665 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00008666#endif
bellard8d18e892007-11-14 15:18:40 +00008667#endif
bellard48dc41e2006-06-21 18:15:50 +00008668#ifdef TARGET_NR_getdomainname
8669 case TARGET_NR_getdomainname:
8670 goto unimplemented_nowarn;
8671#endif
ths6f5b89a2007-03-02 20:48:00 +00008672
thsb5906f92007-03-19 13:32:45 +00008673#ifdef TARGET_NR_clock_gettime
8674 case TARGET_NR_clock_gettime:
8675 {
8676 struct timespec ts;
8677 ret = get_errno(clock_gettime(arg1, &ts));
8678 if (!is_error(ret)) {
8679 host_to_target_timespec(arg2, &ts);
8680 }
8681 break;
8682 }
8683#endif
8684#ifdef TARGET_NR_clock_getres
8685 case TARGET_NR_clock_getres:
8686 {
8687 struct timespec ts;
8688 ret = get_errno(clock_getres(arg1, &ts));
8689 if (!is_error(ret)) {
8690 host_to_target_timespec(arg2, &ts);
8691 }
8692 break;
8693 }
8694#endif
pbrook63d76512008-05-29 13:43:29 +00008695#ifdef TARGET_NR_clock_nanosleep
8696 case TARGET_NR_clock_nanosleep:
8697 {
8698 struct timespec ts;
8699 target_to_host_timespec(&ts, arg3);
8700 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
8701 if (arg4)
8702 host_to_target_timespec(arg4, &ts);
8703 break;
8704 }
8705#endif
thsb5906f92007-03-19 13:32:45 +00008706
ths6f5b89a2007-03-02 20:48:00 +00008707#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8708 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00008709 ret = get_errno(set_tid_address((int *)g2h(arg1)));
8710 break;
ths6f5b89a2007-03-02 20:48:00 +00008711#endif
8712
ths3ae43202007-09-16 21:39:48 +00008713#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00008714 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00008715 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00008716 break;
8717#endif
8718
ths3ae43202007-09-16 21:39:48 +00008719#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00008720 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00008721 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
8722 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00008723 break;
8724#endif
8725
ths4f2b1fe2007-06-21 21:57:12 +00008726#ifdef TARGET_NR_set_robust_list
8727 case TARGET_NR_set_robust_list:
Peter Maydelle9a970a2013-02-08 04:34:54 +00008728 case TARGET_NR_get_robust_list:
8729 /* The ABI for supporting robust futexes has userspace pass
8730 * the kernel a pointer to a linked list which is updated by
8731 * userspace after the syscall; the list is walked by the kernel
8732 * when the thread exits. Since the linked list in QEMU guest
8733 * memory isn't a valid linked list for the host and we have
8734 * no way to reliably intercept the thread-death event, we can't
8735 * support these. Silently return ENOSYS so that guest userspace
8736 * falls back to a non-robust futex implementation (which should
8737 * be OK except in the corner case of the guest crashing while
8738 * holding a mutex that is shared with another process via
8739 * shared memory).
8740 */
8741 goto unimplemented_nowarn;
ths4f2b1fe2007-06-21 21:57:12 +00008742#endif
8743
Peter Maydell1acae9f2013-07-02 14:04:12 +01008744#if defined(TARGET_NR_utimensat)
ths9007f0e2007-09-25 17:50:37 +00008745 case TARGET_NR_utimensat:
8746 {
Riku Voipioebc996f2009-04-21 15:01:51 +03008747 struct timespec *tsp, ts[2];
8748 if (!arg3) {
8749 tsp = NULL;
8750 } else {
8751 target_to_host_timespec(ts, arg3);
8752 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
8753 tsp = ts;
8754 }
ths9007f0e2007-09-25 17:50:37 +00008755 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03008756 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00008757 else {
bellard579a97f2007-11-11 14:26:47 +00008758 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00008759 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00008760 goto fail;
8761 }
Riku Voipioebc996f2009-04-21 15:01:51 +03008762 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00008763 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00008764 }
8765 }
8766 break;
8767#endif
pbrookbd0c5662008-05-29 14:34:11 +00008768 case TARGET_NR_futex:
8769 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
8770 break;
aurel32dbfe4c32009-04-08 21:29:30 +00008771#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00008772 case TARGET_NR_inotify_init:
8773 ret = get_errno(sys_inotify_init());
8774 break;
8775#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008776#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00008777#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8778 case TARGET_NR_inotify_init1:
8779 ret = get_errno(sys_inotify_init1(arg1));
8780 break;
8781#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008782#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008783#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00008784 case TARGET_NR_inotify_add_watch:
8785 p = lock_user_string(arg2);
8786 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
8787 unlock_user(p, arg2, 0);
8788 break;
8789#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008790#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00008791 case TARGET_NR_inotify_rm_watch:
8792 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
8793 break;
8794#endif
ths9007f0e2007-09-25 17:50:37 +00008795
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07008796#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00008797 case TARGET_NR_mq_open:
8798 {
8799 struct mq_attr posix_mq_attr;
8800
8801 p = lock_user_string(arg1 - 1);
8802 if (arg4 != 0)
8803 copy_from_user_mq_attr (&posix_mq_attr, arg4);
8804 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
8805 unlock_user (p, arg1, 0);
8806 }
8807 break;
8808
8809 case TARGET_NR_mq_unlink:
8810 p = lock_user_string(arg1 - 1);
8811 ret = get_errno(mq_unlink(p));
8812 unlock_user (p, arg1, 0);
8813 break;
8814
8815 case TARGET_NR_mq_timedsend:
8816 {
8817 struct timespec ts;
8818
8819 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8820 if (arg5 != 0) {
8821 target_to_host_timespec(&ts, arg5);
8822 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
8823 host_to_target_timespec(arg5, &ts);
8824 }
8825 else
8826 ret = get_errno(mq_send(arg1, p, arg3, arg4));
8827 unlock_user (p, arg2, arg3);
8828 }
8829 break;
8830
8831 case TARGET_NR_mq_timedreceive:
8832 {
8833 struct timespec ts;
8834 unsigned int prio;
8835
8836 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8837 if (arg5 != 0) {
8838 target_to_host_timespec(&ts, arg5);
8839 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
8840 host_to_target_timespec(arg5, &ts);
8841 }
8842 else
8843 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
8844 unlock_user (p, arg2, arg3);
8845 if (arg4 != 0)
8846 put_user_u32(prio, arg4);
8847 }
8848 break;
8849
8850 /* Not implemented for now... */
8851/* case TARGET_NR_mq_notify: */
8852/* break; */
8853
8854 case TARGET_NR_mq_getsetattr:
8855 {
8856 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
8857 ret = 0;
8858 if (arg3 != 0) {
8859 ret = mq_getattr(arg1, &posix_mq_attr_out);
8860 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
8861 }
8862 if (arg2 != 0) {
8863 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
8864 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
8865 }
8866
8867 }
8868 break;
8869#endif
8870
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308871#ifdef CONFIG_SPLICE
8872#ifdef TARGET_NR_tee
8873 case TARGET_NR_tee:
8874 {
8875 ret = get_errno(tee(arg1,arg2,arg3,arg4));
8876 }
8877 break;
8878#endif
8879#ifdef TARGET_NR_splice
8880 case TARGET_NR_splice:
8881 {
8882 loff_t loff_in, loff_out;
8883 loff_t *ploff_in = NULL, *ploff_out = NULL;
8884 if(arg2) {
8885 get_user_u64(loff_in, arg2);
8886 ploff_in = &loff_in;
8887 }
8888 if(arg4) {
8889 get_user_u64(loff_out, arg2);
8890 ploff_out = &loff_out;
8891 }
8892 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
8893 }
8894 break;
8895#endif
8896#ifdef TARGET_NR_vmsplice
8897 case TARGET_NR_vmsplice:
8898 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008899 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8900 if (vec != NULL) {
8901 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
8902 unlock_iovec(vec, arg2, arg3, 0);
8903 } else {
8904 ret = -host_to_target_errno(errno);
8905 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308906 }
8907 break;
8908#endif
8909#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03008910#ifdef CONFIG_EVENTFD
8911#if defined(TARGET_NR_eventfd)
8912 case TARGET_NR_eventfd:
8913 ret = get_errno(eventfd(arg1, 0));
8914 break;
8915#endif
8916#if defined(TARGET_NR_eventfd2)
8917 case TARGET_NR_eventfd2:
Petar Jovanovic5947c692013-04-08 20:26:10 +02008918 {
8919 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
8920 if (arg2 & TARGET_O_NONBLOCK) {
8921 host_flags |= O_NONBLOCK;
8922 }
8923 if (arg2 & TARGET_O_CLOEXEC) {
8924 host_flags |= O_CLOEXEC;
8925 }
8926 ret = get_errno(eventfd(arg1, host_flags));
Riku Voipioc2882b92009-08-12 15:08:24 +03008927 break;
Petar Jovanovic5947c692013-04-08 20:26:10 +02008928 }
Riku Voipioc2882b92009-08-12 15:08:24 +03008929#endif
8930#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03008931#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8932 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +01008933#if TARGET_ABI_BITS == 32
8934 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
8935 target_offset64(arg5, arg6)));
8936#else
Ulrich Hechtd0927932009-09-17 20:22:14 +03008937 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +01008938#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03008939 break;
8940#endif
Peter Maydellc727f472011-01-06 11:05:10 +00008941#if defined(CONFIG_SYNC_FILE_RANGE)
8942#if defined(TARGET_NR_sync_file_range)
8943 case TARGET_NR_sync_file_range:
8944#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +03008945#if defined(TARGET_MIPS)
8946 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8947 target_offset64(arg5, arg6), arg7));
8948#else
Peter Maydellc727f472011-01-06 11:05:10 +00008949 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
8950 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +03008951#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +00008952#else
8953 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
8954#endif
8955 break;
8956#endif
8957#if defined(TARGET_NR_sync_file_range2)
8958 case TARGET_NR_sync_file_range2:
8959 /* This is like sync_file_range but the arguments are reordered */
8960#if TARGET_ABI_BITS == 32
8961 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8962 target_offset64(arg5, arg6), arg2));
8963#else
8964 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
8965#endif
8966 break;
8967#endif
8968#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00008969#if defined(CONFIG_EPOLL)
8970#if defined(TARGET_NR_epoll_create)
8971 case TARGET_NR_epoll_create:
8972 ret = get_errno(epoll_create(arg1));
8973 break;
8974#endif
8975#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
8976 case TARGET_NR_epoll_create1:
8977 ret = get_errno(epoll_create1(arg1));
8978 break;
8979#endif
8980#if defined(TARGET_NR_epoll_ctl)
8981 case TARGET_NR_epoll_ctl:
8982 {
8983 struct epoll_event ep;
8984 struct epoll_event *epp = 0;
8985 if (arg4) {
8986 struct target_epoll_event *target_ep;
8987 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
8988 goto efault;
8989 }
8990 ep.events = tswap32(target_ep->events);
8991 /* The epoll_data_t union is just opaque data to the kernel,
8992 * so we transfer all 64 bits across and need not worry what
8993 * actual data type it is.
8994 */
8995 ep.data.u64 = tswap64(target_ep->data.u64);
8996 unlock_user_struct(target_ep, arg4, 0);
8997 epp = &ep;
8998 }
8999 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
9000 break;
9001 }
9002#endif
9003
9004#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9005#define IMPLEMENT_EPOLL_PWAIT
9006#endif
9007#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9008#if defined(TARGET_NR_epoll_wait)
9009 case TARGET_NR_epoll_wait:
9010#endif
9011#if defined(IMPLEMENT_EPOLL_PWAIT)
9012 case TARGET_NR_epoll_pwait:
9013#endif
9014 {
9015 struct target_epoll_event *target_ep;
9016 struct epoll_event *ep;
9017 int epfd = arg1;
9018 int maxevents = arg3;
9019 int timeout = arg4;
9020
9021 target_ep = lock_user(VERIFY_WRITE, arg2,
9022 maxevents * sizeof(struct target_epoll_event), 1);
9023 if (!target_ep) {
9024 goto efault;
9025 }
9026
9027 ep = alloca(maxevents * sizeof(struct epoll_event));
9028
9029 switch (num) {
9030#if defined(IMPLEMENT_EPOLL_PWAIT)
9031 case TARGET_NR_epoll_pwait:
9032 {
9033 target_sigset_t *target_set;
9034 sigset_t _set, *set = &_set;
9035
9036 if (arg5) {
9037 target_set = lock_user(VERIFY_READ, arg5,
9038 sizeof(target_sigset_t), 1);
9039 if (!target_set) {
9040 unlock_user(target_ep, arg2, 0);
9041 goto efault;
9042 }
9043 target_to_host_sigset(set, target_set);
9044 unlock_user(target_set, arg5, 0);
9045 } else {
9046 set = NULL;
9047 }
9048
9049 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
9050 break;
9051 }
9052#endif
9053#if defined(TARGET_NR_epoll_wait)
9054 case TARGET_NR_epoll_wait:
9055 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
9056 break;
9057#endif
9058 default:
9059 ret = -TARGET_ENOSYS;
9060 }
9061 if (!is_error(ret)) {
9062 int i;
9063 for (i = 0; i < ret; i++) {
9064 target_ep[i].events = tswap32(ep[i].events);
9065 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
9066 }
9067 }
9068 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
9069 break;
9070 }
9071#endif
9072#endif
Peter Maydell163a05a2011-06-27 17:44:52 +01009073#ifdef TARGET_NR_prlimit64
9074 case TARGET_NR_prlimit64:
9075 {
9076 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9077 struct target_rlimit64 *target_rnew, *target_rold;
9078 struct host_rlimit64 rnew, rold, *rnewp = 0;
9079 if (arg3) {
9080 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
9081 goto efault;
9082 }
9083 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
9084 rnew.rlim_max = tswap64(target_rnew->rlim_max);
9085 unlock_user_struct(target_rnew, arg3, 0);
9086 rnewp = &rnew;
9087 }
9088
9089 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
9090 if (!is_error(ret) && arg4) {
9091 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
9092 goto efault;
9093 }
9094 target_rold->rlim_cur = tswap64(rold.rlim_cur);
9095 target_rold->rlim_max = tswap64(rold.rlim_max);
9096 unlock_user_struct(target_rold, arg4, 1);
9097 }
9098 break;
9099 }
9100#endif
Richard Henderson3d21d292012-09-15 13:20:46 -07009101#ifdef TARGET_NR_gethostname
9102 case TARGET_NR_gethostname:
9103 {
9104 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
9105 if (name) {
9106 ret = get_errno(gethostname(name, arg2));
9107 unlock_user(name, arg1, arg2);
9108 } else {
9109 ret = -TARGET_EFAULT;
9110 }
9111 break;
9112 }
9113#endif
Riku Voipio89aaf1a2013-07-24 09:44:26 +03009114#ifdef TARGET_NR_atomic_cmpxchg_32
9115 case TARGET_NR_atomic_cmpxchg_32:
9116 {
9117 /* should use start_exclusive from main.c */
9118 abi_ulong mem_value;
9119 if (get_user_u32(mem_value, arg6)) {
9120 target_siginfo_t info;
9121 info.si_signo = SIGSEGV;
9122 info.si_errno = 0;
9123 info.si_code = TARGET_SEGV_MAPERR;
9124 info._sifields._sigfault._addr = arg6;
9125 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
9126 ret = 0xdeadbeef;
9127
9128 }
9129 if (mem_value == arg2)
9130 put_user_u32(arg1, arg6);
9131 ret = mem_value;
9132 break;
9133 }
9134#endif
9135#ifdef TARGET_NR_atomic_barrier
9136 case TARGET_NR_atomic_barrier:
9137 {
9138 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
9139 break;
9140 }
9141#endif
Erik de Castro Lopof4f1e102013-11-29 18:39:23 +11009142
9143#ifdef TARGET_NR_timer_create
9144 case TARGET_NR_timer_create:
9145 {
9146 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
9147
9148 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
9149 struct target_sigevent *ptarget_sevp;
9150 struct target_timer_t *ptarget_timer;
9151
9152 int clkid = arg1;
9153 int timer_index = next_free_host_timer();
9154
9155 if (timer_index < 0) {
9156 ret = -TARGET_EAGAIN;
9157 } else {
9158 timer_t *phtimer = g_posix_timers + timer_index;
9159
9160 if (arg2) {
9161 if (!lock_user_struct(VERIFY_READ, ptarget_sevp, arg2, 1)) {
9162 goto efault;
9163 }
9164
9165 host_sevp.sigev_signo = tswap32(ptarget_sevp->sigev_signo);
9166 host_sevp.sigev_notify = tswap32(ptarget_sevp->sigev_notify);
9167
9168 phost_sevp = &host_sevp;
9169 }
9170
9171 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
9172 if (ret) {
9173 phtimer = NULL;
9174 } else {
9175 if (!lock_user_struct(VERIFY_WRITE, ptarget_timer, arg3, 1)) {
9176 goto efault;
9177 }
9178 ptarget_timer->ptr = tswap32(0xcafe0000 | timer_index);
9179 unlock_user_struct(ptarget_timer, arg3, 1);
9180 }
9181 }
9182 break;
9183 }
9184#endif
9185
9186#ifdef TARGET_NR_timer_settime
9187 case TARGET_NR_timer_settime:
9188 {
9189 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
9190 * struct itimerspec * old_value */
9191 arg1 &= 0xffff;
9192 if (arg3 == 0 || arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
9193 ret = -TARGET_EINVAL;
9194 } else {
9195 timer_t htimer = g_posix_timers[arg1];
9196 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
9197
9198 target_to_host_itimerspec(&hspec_new, arg3);
9199 ret = get_errno(
9200 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
9201 host_to_target_itimerspec(arg2, &hspec_old);
9202 }
9203 break;
9204 }
9205#endif
9206
9207#ifdef TARGET_NR_timer_gettime
9208 case TARGET_NR_timer_gettime:
9209 {
9210 /* args: timer_t timerid, struct itimerspec *curr_value */
9211 arg1 &= 0xffff;
9212 if (!arg2) {
9213 return -TARGET_EFAULT;
9214 } else if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
9215 ret = -TARGET_EINVAL;
9216 } else {
9217 timer_t htimer = g_posix_timers[arg1];
9218 struct itimerspec hspec;
9219 ret = get_errno(timer_gettime(htimer, &hspec));
9220
9221 if (host_to_target_itimerspec(arg2, &hspec)) {
9222 ret = -TARGET_EFAULT;
9223 }
9224 }
9225 break;
9226 }
9227#endif
9228
9229#ifdef TARGET_NR_timer_getoverrun
9230 case TARGET_NR_timer_getoverrun:
9231 {
9232 /* args: timer_t timerid */
9233 arg1 &= 0xffff;
9234 if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
9235 ret = -TARGET_EINVAL;
9236 } else {
9237 timer_t htimer = g_posix_timers[arg1];
9238 ret = get_errno(timer_getoverrun(htimer));
9239 }
9240 break;
9241 }
9242#endif
9243
9244#ifdef TARGET_NR_timer_delete
9245 case TARGET_NR_timer_delete:
9246 {
9247 /* args: timer_t timerid */
9248 arg1 &= 0xffff;
9249 if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
9250 ret = -TARGET_EINVAL;
9251 } else {
9252 timer_t htimer = g_posix_timers[arg1];
9253 ret = get_errno(timer_delete(htimer));
9254 g_posix_timers[arg1] = 0;
9255 }
9256 break;
9257 }
9258#endif
9259
bellard31e31b82003-02-18 22:55:36 +00009260 default:
9261 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00009262 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00009263#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 +00009264 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00009265#endif
ths0da46a62007-10-20 20:23:07 +00009266 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00009267 break;
9268 }
bellard579a97f2007-11-11 14:26:47 +00009269fail:
bellardc573ff62004-01-04 15:51:36 +00009270#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00009271 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00009272#endif
thsb92c47c2007-11-01 00:07:38 +00009273 if(do_strace)
9274 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00009275 return ret;
bellard579a97f2007-11-11 14:26:47 +00009276efault:
9277 ret = -TARGET_EFAULT;
9278 goto fail;
bellard31e31b82003-02-18 22:55:36 +00009279}