blob: cdd0c28fffe37d3da578a69773a62e68af8d2dcd [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>
pbrookd7e40362008-05-23 16:06:43 +0000108#include "linux_loop.h"
Loïc Minierda790302009-12-29 22:06:13 +0100109#include "cpu-uname.h"
bellard31e31b82003-02-18 22:55:36 +0000110
bellard3ef693a2003-03-23 20:17:16 +0000111#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +0000112
Juan Quintela2f7bb872009-07-27 16:13:24 +0200113#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +0000114#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
115 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
116#else
117/* XXX: Hardcode the above values. */
118#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +0000119#endif
120
bellard72f03902003-02-18 23:33:18 +0000121//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +0000122
bellard1a9353d2003-03-16 20:28:50 +0000123//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000124#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
125#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000126
bellard70a194b2003-08-11 22:20:16 +0000127
bellard70a194b2003-08-11 22:20:16 +0000128#undef _syscall0
129#undef _syscall1
130#undef _syscall2
131#undef _syscall3
132#undef _syscall4
133#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000134#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000135
bellard83fcb512006-06-14 13:37:16 +0000136#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000137static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000138{ \
139 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000140}
141
bellard83fcb512006-06-14 13:37:16 +0000142#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000143static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000144{ \
145 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000146}
147
bellard83fcb512006-06-14 13:37:16 +0000148#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000149static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000150{ \
151 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000152}
153
bellard83fcb512006-06-14 13:37:16 +0000154#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000155static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000156{ \
157 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000158}
159
bellard83fcb512006-06-14 13:37:16 +0000160#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000161static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000162{ \
163 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000164}
165
bellard83fcb512006-06-14 13:37:16 +0000166#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
167 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000168static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000169{ \
170 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000171}
bellard83fcb512006-06-14 13:37:16 +0000172
173
174#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
175 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000176static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
177 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000178{ \
179 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
180}
181
bellard70a194b2003-08-11 22:20:16 +0000182
bellard31e31b82003-02-18 22:55:36 +0000183#define __NR_sys_uname __NR_uname
bellard72f03902003-02-18 23:33:18 +0000184#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000185#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000186#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000187#define __NR_sys_getpriority __NR_getpriority
bellard66fb9762003-03-23 01:06:05 +0000188#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
ths7494b0f2007-02-11 18:26:53 +0000189#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000190#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000191#define __NR_sys_tkill __NR_tkill
pbrookbd0c5662008-05-29 14:34:11 +0000192#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000193#define __NR_sys_inotify_init __NR_inotify_init
194#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
195#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000196
Alexander Graf42a39fb2011-04-15 17:32:45 +0200197#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
198 defined(__s390x__)
bellard9af9eaa2003-04-07 21:34:41 +0000199#define __NR__llseek __NR_lseek
200#endif
201
bellard72f03902003-02-18 23:33:18 +0000202#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000203_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000204#else
ths0da46a62007-10-20 20:23:07 +0000205/* This is a replacement for the host gettid() and must return a host
206 errno. */
bellard72f03902003-02-18 23:33:18 +0000207static int gettid(void) {
208 return -ENOSYS;
209}
210#endif
Peter Maydell3307e232013-06-12 16:20:21 +0100211#ifdef __NR_getdents
aurel323b3f24a2009-04-15 16:12:13 +0000212_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
Peter Maydell3307e232013-06-12 16:20:21 +0100213#endif
214#if !defined(__NR_getdents) || \
215 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
aurel323b3f24a2009-04-15 16:12:13 +0000216_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
217#endif
Richard Hendersond35b2612010-06-04 12:14:10 -0700218#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
aurel323b3f24a2009-04-15 16:12:13 +0000219_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
220 loff_t *, res, uint, wh);
221#endif
222_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
223_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
224#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
225_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
226#endif
227#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
228_syscall2(int,sys_tkill,int,tid,int,sig)
229#endif
230#ifdef __NR_exit_group
231_syscall1(int,exit_group,int,error_code)
232#endif
233#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234_syscall1(int,set_tid_address,int *,tidptr)
235#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +0200236#if defined(CONFIG_USE_NPTL)
aurel323b3f24a2009-04-15 16:12:13 +0000237#if defined(TARGET_NR_futex) && defined(__NR_futex)
238_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
239 const struct timespec *,timeout,int *,uaddr2,int,val3)
240#endif
241#endif
Mike Frysinger737de1d2011-02-07 01:05:55 -0500242#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
243_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
244 unsigned long *, user_mask_ptr);
245#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
246_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
247 unsigned long *, user_mask_ptr);
Alexander Graf0f6b4d22011-09-27 14:39:42 +0200248_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
249 void *, arg);
aurel323b3f24a2009-04-15 16:12:13 +0000250
251static bitmask_transtbl fcntl_flags_tbl[] = {
252 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
253 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
254 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
255 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
256 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
257 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
258 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
259 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
Richard Hendersonafc87632012-07-25 14:30:34 -0700260 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
aurel323b3f24a2009-04-15 16:12:13 +0000261 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
262 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
263 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
264 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
aurel323b3f24a2009-04-15 16:12:13 +0000265#if defined(O_DIRECT)
266 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
267#endif
Richard Hendersonafc87632012-07-25 14:30:34 -0700268#if defined(O_NOATIME)
269 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
270#endif
271#if defined(O_CLOEXEC)
272 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
273#endif
274#if defined(O_PATH)
275 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
276#endif
277 /* Don't terminate the list prematurely on 64-bit host+guest. */
278#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
279 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
280#endif
aurel323b3f24a2009-04-15 16:12:13 +0000281 { 0, 0, 0, 0 }
282};
283
284#define COPY_UTSNAME_FIELD(dest, src) \
285 do { \
286 /* __NEW_UTS_LEN doesn't include terminating null */ \
287 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
288 (dest)[__NEW_UTS_LEN] = '\0'; \
289 } while (0)
290
291static int sys_uname(struct new_utsname *buf)
292{
293 struct utsname uts_buf;
294
295 if (uname(&uts_buf) < 0)
296 return (-1);
297
298 /*
299 * Just in case these have some differences, we
300 * translate utsname to new_utsname (which is the
301 * struct linux kernel uses).
302 */
303
Stefan Weil67bd9ed2011-05-07 22:32:25 +0200304 memset(buf, 0, sizeof(*buf));
aurel323b3f24a2009-04-15 16:12:13 +0000305 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
306 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
307 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
308 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
309 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
310#ifdef _GNU_SOURCE
311 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
312#endif
313 return (0);
314
315#undef COPY_UTSNAME_FIELD
316}
317
318static int sys_getcwd1(char *buf, size_t size)
319{
320 if (getcwd(buf, size) == NULL) {
321 /* getcwd() sets errno */
322 return (-1);
323 }
aurel32aaf4ad32009-04-16 14:17:14 +0000324 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000325}
326
aurel323b3f24a2009-04-15 16:12:13 +0000327#ifdef TARGET_NR_openat
Alexander Graff4c69012011-09-25 06:25:35 +0200328static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000329{
330 /*
331 * open(2) has extra parameter 'mode' when called with
332 * flag O_CREAT.
333 */
334 if ((flags & O_CREAT) != 0) {
aurel323b3f24a2009-04-15 16:12:13 +0000335 return (openat(dirfd, pathname, flags, mode));
336 }
337 return (openat(dirfd, pathname, flags));
338}
339#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300340
341#ifdef CONFIG_UTIMENSAT
342static int sys_utimensat(int dirfd, const char *pathname,
343 const struct timespec times[2], int flags)
344{
345 if (pathname == NULL)
346 return futimens(dirfd, times);
347 else
348 return utimensat(dirfd, pathname, times, flags);
349}
350#else
ths9007f0e2007-09-25 17:50:37 +0000351#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
352_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
353 const struct timespec *,tsp,int,flags)
354#endif
Riku Voipioebc996f2009-04-21 15:01:51 +0300355#endif /* CONFIG_UTIMENSAT */
aurel323b3f24a2009-04-15 16:12:13 +0000356
357#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000358#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000359
aurel3239b59762008-10-01 21:46:50 +0000360#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000361static int sys_inotify_init(void)
362{
363 return (inotify_init());
364}
aurel3239b59762008-10-01 21:46:50 +0000365#endif
366#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000367static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
368{
369 return (inotify_add_watch(fd, pathname, mask));
370}
aurel3239b59762008-10-01 21:46:50 +0000371#endif
372#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000373static int sys_inotify_rm_watch(int fd, int32_t wd)
374{
aurel328690e422009-04-17 13:50:32 +0000375 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000376}
aurel3239b59762008-10-01 21:46:50 +0000377#endif
Riku Voipioc05c7a72010-03-26 15:25:11 +0000378#ifdef CONFIG_INOTIFY1
379#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
380static int sys_inotify_init1(int flags)
381{
382 return (inotify_init1(flags));
383}
384#endif
385#endif
aurel323b3f24a2009-04-15 16:12:13 +0000386#else
387/* Userspace can usually survive runtime without inotify */
388#undef TARGET_NR_inotify_init
Riku Voipioc05c7a72010-03-26 15:25:11 +0000389#undef TARGET_NR_inotify_init1
aurel323b3f24a2009-04-15 16:12:13 +0000390#undef TARGET_NR_inotify_add_watch
391#undef TARGET_NR_inotify_rm_watch
392#endif /* CONFIG_INOTIFY */
393
Mike Frysingerd8035d42011-02-07 01:05:51 -0500394#if defined(TARGET_NR_ppoll)
395#ifndef __NR_ppoll
396# define __NR_ppoll -1
397#endif
398#define __NR_sys_ppoll __NR_ppoll
399_syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
400 struct timespec *, timeout, const __sigset_t *, sigmask,
401 size_t, sigsetsize)
402#endif
bellard66fb9762003-03-23 01:06:05 +0000403
Mike Frysinger055e0902011-06-03 17:01:49 -0400404#if defined(TARGET_NR_pselect6)
405#ifndef __NR_pselect6
406# define __NR_pselect6 -1
407#endif
408#define __NR_sys_pselect6 __NR_pselect6
409_syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
410 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
411#endif
412
Peter Maydell163a05a2011-06-27 17:44:52 +0100413#if defined(TARGET_NR_prlimit64)
414#ifndef __NR_prlimit64
415# define __NR_prlimit64 -1
416#endif
417#define __NR_sys_prlimit64 __NR_prlimit64
418/* The glibc rlimit structure may not be that used by the underlying syscall */
419struct host_rlimit64 {
420 uint64_t rlim_cur;
421 uint64_t rlim_max;
422};
423_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
424 const struct host_rlimit64 *, new_limit,
425 struct host_rlimit64 *, old_limit)
426#endif
427
Riku Voipio48e515d2011-07-12 15:40:51 +0300428/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
Alexander Graf4a1def42012-09-29 15:32:38 +0000429#ifdef TARGET_ARM
Riku Voipio48e515d2011-07-12 15:40:51 +0300430static inline int regpairs_aligned(void *cpu_env) {
431 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
432}
433#elif defined(TARGET_MIPS)
434static inline int regpairs_aligned(void *cpu_env) { return 1; }
Alexander Graf4a1def42012-09-29 15:32:38 +0000435#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
436/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
437 * of registers which translates to the same as ARM/MIPS, because we start with
438 * r3 as arg1 */
439static inline int regpairs_aligned(void *cpu_env) { return 1; }
Riku Voipio48e515d2011-07-12 15:40:51 +0300440#else
441static inline int regpairs_aligned(void *cpu_env) { return 0; }
442#endif
443
thsb92c47c2007-11-01 00:07:38 +0000444#define ERRNO_TABLE_SIZE 1200
445
446/* target_to_host_errno_table[] is initialized from
447 * host_to_target_errno_table[] in syscall_init(). */
448static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
449};
450
ths637947f2007-06-01 12:09:19 +0000451/*
thsfe8f0962007-07-12 10:59:21 +0000452 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000453 * minus the errnos that are not actually generic to all archs.
454 */
thsb92c47c2007-11-01 00:07:38 +0000455static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000456 [EIDRM] = TARGET_EIDRM,
457 [ECHRNG] = TARGET_ECHRNG,
458 [EL2NSYNC] = TARGET_EL2NSYNC,
459 [EL3HLT] = TARGET_EL3HLT,
460 [EL3RST] = TARGET_EL3RST,
461 [ELNRNG] = TARGET_ELNRNG,
462 [EUNATCH] = TARGET_EUNATCH,
463 [ENOCSI] = TARGET_ENOCSI,
464 [EL2HLT] = TARGET_EL2HLT,
465 [EDEADLK] = TARGET_EDEADLK,
466 [ENOLCK] = TARGET_ENOLCK,
467 [EBADE] = TARGET_EBADE,
468 [EBADR] = TARGET_EBADR,
469 [EXFULL] = TARGET_EXFULL,
470 [ENOANO] = TARGET_ENOANO,
471 [EBADRQC] = TARGET_EBADRQC,
472 [EBADSLT] = TARGET_EBADSLT,
473 [EBFONT] = TARGET_EBFONT,
474 [ENOSTR] = TARGET_ENOSTR,
475 [ENODATA] = TARGET_ENODATA,
476 [ETIME] = TARGET_ETIME,
477 [ENOSR] = TARGET_ENOSR,
478 [ENONET] = TARGET_ENONET,
479 [ENOPKG] = TARGET_ENOPKG,
480 [EREMOTE] = TARGET_EREMOTE,
481 [ENOLINK] = TARGET_ENOLINK,
482 [EADV] = TARGET_EADV,
483 [ESRMNT] = TARGET_ESRMNT,
484 [ECOMM] = TARGET_ECOMM,
485 [EPROTO] = TARGET_EPROTO,
486 [EDOTDOT] = TARGET_EDOTDOT,
487 [EMULTIHOP] = TARGET_EMULTIHOP,
488 [EBADMSG] = TARGET_EBADMSG,
489 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
490 [EOVERFLOW] = TARGET_EOVERFLOW,
491 [ENOTUNIQ] = TARGET_ENOTUNIQ,
492 [EBADFD] = TARGET_EBADFD,
493 [EREMCHG] = TARGET_EREMCHG,
494 [ELIBACC] = TARGET_ELIBACC,
495 [ELIBBAD] = TARGET_ELIBBAD,
496 [ELIBSCN] = TARGET_ELIBSCN,
497 [ELIBMAX] = TARGET_ELIBMAX,
498 [ELIBEXEC] = TARGET_ELIBEXEC,
499 [EILSEQ] = TARGET_EILSEQ,
500 [ENOSYS] = TARGET_ENOSYS,
501 [ELOOP] = TARGET_ELOOP,
502 [ERESTART] = TARGET_ERESTART,
503 [ESTRPIPE] = TARGET_ESTRPIPE,
504 [ENOTEMPTY] = TARGET_ENOTEMPTY,
505 [EUSERS] = TARGET_EUSERS,
506 [ENOTSOCK] = TARGET_ENOTSOCK,
507 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
508 [EMSGSIZE] = TARGET_EMSGSIZE,
509 [EPROTOTYPE] = TARGET_EPROTOTYPE,
510 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
511 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
512 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
513 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
514 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
515 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
516 [EADDRINUSE] = TARGET_EADDRINUSE,
517 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
518 [ENETDOWN] = TARGET_ENETDOWN,
519 [ENETUNREACH] = TARGET_ENETUNREACH,
520 [ENETRESET] = TARGET_ENETRESET,
521 [ECONNABORTED] = TARGET_ECONNABORTED,
522 [ECONNRESET] = TARGET_ECONNRESET,
523 [ENOBUFS] = TARGET_ENOBUFS,
524 [EISCONN] = TARGET_EISCONN,
525 [ENOTCONN] = TARGET_ENOTCONN,
526 [EUCLEAN] = TARGET_EUCLEAN,
527 [ENOTNAM] = TARGET_ENOTNAM,
528 [ENAVAIL] = TARGET_ENAVAIL,
529 [EISNAM] = TARGET_EISNAM,
530 [EREMOTEIO] = TARGET_EREMOTEIO,
531 [ESHUTDOWN] = TARGET_ESHUTDOWN,
532 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
533 [ETIMEDOUT] = TARGET_ETIMEDOUT,
534 [ECONNREFUSED] = TARGET_ECONNREFUSED,
535 [EHOSTDOWN] = TARGET_EHOSTDOWN,
536 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
537 [EALREADY] = TARGET_EALREADY,
538 [EINPROGRESS] = TARGET_EINPROGRESS,
539 [ESTALE] = TARGET_ESTALE,
540 [ECANCELED] = TARGET_ECANCELED,
541 [ENOMEDIUM] = TARGET_ENOMEDIUM,
542 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000543#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000544 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000545#endif
546#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000547 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000548#endif
549#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000550 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000551#endif
552#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000553 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000554#endif
555#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000556 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000557#endif
558#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000559 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000560#endif
thsb92c47c2007-11-01 00:07:38 +0000561};
ths637947f2007-06-01 12:09:19 +0000562
563static inline int host_to_target_errno(int err)
564{
565 if(host_to_target_errno_table[err])
566 return host_to_target_errno_table[err];
567 return err;
568}
569
thsb92c47c2007-11-01 00:07:38 +0000570static inline int target_to_host_errno(int err)
571{
572 if (target_to_host_errno_table[err])
573 return target_to_host_errno_table[err];
574 return err;
575}
576
blueswir1992f48a2007-10-14 16:27:31 +0000577static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000578{
579 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000580 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000581 else
582 return ret;
583}
584
blueswir1992f48a2007-10-14 16:27:31 +0000585static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000586{
blueswir1992f48a2007-10-14 16:27:31 +0000587 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000588}
589
thsb92c47c2007-11-01 00:07:38 +0000590char *target_strerror(int err)
591{
Alexander Graf962b2892011-11-21 12:04:07 +0100592 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
593 return NULL;
594 }
thsb92c47c2007-11-01 00:07:38 +0000595 return strerror(target_to_host_errno(err));
596}
597
blueswir1992f48a2007-10-14 16:27:31 +0000598static abi_ulong target_brk;
599static abi_ulong target_original_brk;
vincent4d1de872011-06-14 21:56:33 +0000600static abi_ulong brk_page;
bellard31e31b82003-02-18 22:55:36 +0000601
blueswir1992f48a2007-10-14 16:27:31 +0000602void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000603{
blueswir14c1de732007-07-07 20:45:44 +0000604 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
vincent4d1de872011-06-14 21:56:33 +0000605 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000606}
607
vincent4d1de872011-06-14 21:56:33 +0000608//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
609#define DEBUGF_BRK(message, args...)
610
ths0da46a62007-10-20 20:23:07 +0000611/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000612abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000613{
blueswir1992f48a2007-10-14 16:27:31 +0000614 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000615 int new_alloc_size;
616
Paul Brook3a0c6c42012-02-09 19:04:27 +0000617 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
ths3b46e622007-09-17 08:09:54 +0000618
vincent4d1de872011-06-14 21:56:33 +0000619 if (!new_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000620 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000621 return target_brk;
622 }
623 if (new_brk < target_original_brk) {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000624 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
625 target_brk);
vincent4d1de872011-06-14 21:56:33 +0000626 return target_brk;
627 }
bellard31e31b82003-02-18 22:55:36 +0000628
vincent4d1de872011-06-14 21:56:33 +0000629 /* If the new brk is less than the highest page reserved to the
630 * target heap allocation, set it and we're almost done... */
631 if (new_brk <= brk_page) {
632 /* Heap contents are initialized to zero, as for anonymous
633 * mapped pages. */
634 if (new_brk > target_brk) {
635 memset(g2h(target_brk), 0, new_brk - target_brk);
636 }
bellard31e31b82003-02-18 22:55:36 +0000637 target_brk = new_brk;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000638 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
pbrook53a59602006-03-25 19:31:22 +0000639 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000640 }
641
Peter Maydell00faf082011-04-18 16:34:24 +0100642 /* We need to allocate more memory after the brk... Note that
643 * we don't use MAP_FIXED because that will map over the top of
644 * any existing mapping (like the one with the host libc or qemu
645 * itself); instead we treat "mapped but at wrong address" as
646 * a failure and unmap again.
647 */
vincent4d1de872011-06-14 21:56:33 +0000648 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
ths5fafdf22007-09-16 21:08:06 +0000649 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000650 PROT_READ|PROT_WRITE,
Peter Maydell00faf082011-04-18 16:34:24 +0100651 MAP_ANON|MAP_PRIVATE, 0, 0));
652
653 if (mapped_addr == brk_page) {
Cédric VINCENT70afc342011-08-26 10:56:50 +0200654 /* Heap contents are initialized to zero, as for anonymous
655 * mapped pages. Technically the new pages are already
656 * initialized to zero since they *are* anonymous mapped
657 * pages, however we have to take care with the contents that
658 * come from the remaining part of the previous page: it may
659 * contains garbage data due to a previous heap usage (grown
660 * then shrunken). */
661 memset(g2h(target_brk), 0, brk_page - target_brk);
662
Peter Maydell00faf082011-04-18 16:34:24 +0100663 target_brk = new_brk;
vincent4d1de872011-06-14 21:56:33 +0000664 brk_page = HOST_PAGE_ALIGN(target_brk);
Paul Brook3a0c6c42012-02-09 19:04:27 +0000665 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
666 target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100667 return target_brk;
668 } else if (mapped_addr != -1) {
669 /* Mapped but at wrong address, meaning there wasn't actually
670 * enough space for this brk.
671 */
672 target_munmap(mapped_addr, new_alloc_size);
673 mapped_addr = -1;
Paul Brook3a0c6c42012-02-09 19:04:27 +0000674 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
vincent4d1de872011-06-14 21:56:33 +0000675 }
676 else {
Paul Brook3a0c6c42012-02-09 19:04:27 +0000677 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
Peter Maydell00faf082011-04-18 16:34:24 +0100678 }
balrog7ab240a2008-04-26 12:17:34 +0000679
Richard Henderson7dd46c02010-05-03 10:07:49 -0700680#if defined(TARGET_ALPHA)
681 /* We (partially) emulate OSF/1 on Alpha, which requires we
682 return a proper errno, not an unchanged brk value. */
Peter Maydell00faf082011-04-18 16:34:24 +0100683 return -TARGET_ENOMEM;
Richard Henderson7dd46c02010-05-03 10:07:49 -0700684#endif
Peter Maydell00faf082011-04-18 16:34:24 +0100685 /* For everything else, return the previous break. */
balrog7ab240a2008-04-26 12:17:34 +0000686 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000687}
688
ths26edcf42007-12-09 02:25:24 +0000689static inline abi_long copy_from_user_fdset(fd_set *fds,
690 abi_ulong target_fds_addr,
691 int n)
bellard31e31b82003-02-18 22:55:36 +0000692{
ths26edcf42007-12-09 02:25:24 +0000693 int i, nw, j, k;
694 abi_ulong b, *target_fds;
695
696 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
697 if (!(target_fds = lock_user(VERIFY_READ,
698 target_fds_addr,
699 sizeof(abi_ulong) * nw,
700 1)))
701 return -TARGET_EFAULT;
702
703 FD_ZERO(fds);
704 k = 0;
705 for (i = 0; i < nw; i++) {
706 /* grab the abi_ulong */
707 __get_user(b, &target_fds[i]);
708 for (j = 0; j < TARGET_ABI_BITS; j++) {
709 /* check the bit inside the abi_ulong */
710 if ((b >> j) & 1)
711 FD_SET(k, fds);
712 k++;
bellard31e31b82003-02-18 22:55:36 +0000713 }
bellard31e31b82003-02-18 22:55:36 +0000714 }
ths26edcf42007-12-09 02:25:24 +0000715
716 unlock_user(target_fds, target_fds_addr, 0);
717
718 return 0;
bellard31e31b82003-02-18 22:55:36 +0000719}
720
Mike Frysinger055e0902011-06-03 17:01:49 -0400721static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
722 abi_ulong target_fds_addr,
723 int n)
724{
725 if (target_fds_addr) {
726 if (copy_from_user_fdset(fds, target_fds_addr, n))
727 return -TARGET_EFAULT;
728 *fds_ptr = fds;
729 } else {
730 *fds_ptr = NULL;
731 }
732 return 0;
733}
734
ths26edcf42007-12-09 02:25:24 +0000735static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
736 const fd_set *fds,
737 int n)
bellard31e31b82003-02-18 22:55:36 +0000738{
bellard31e31b82003-02-18 22:55:36 +0000739 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000740 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000741 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000742
ths26edcf42007-12-09 02:25:24 +0000743 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
744 if (!(target_fds = lock_user(VERIFY_WRITE,
745 target_fds_addr,
746 sizeof(abi_ulong) * nw,
747 0)))
748 return -TARGET_EFAULT;
749
750 k = 0;
751 for (i = 0; i < nw; i++) {
752 v = 0;
753 for (j = 0; j < TARGET_ABI_BITS; j++) {
Andreas Schwab9ab709b2013-04-09 01:02:34 +0000754 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
ths26edcf42007-12-09 02:25:24 +0000755 k++;
bellard31e31b82003-02-18 22:55:36 +0000756 }
ths26edcf42007-12-09 02:25:24 +0000757 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000758 }
ths26edcf42007-12-09 02:25:24 +0000759
760 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
761
762 return 0;
bellard31e31b82003-02-18 22:55:36 +0000763}
764
bellardc596ed12003-07-13 17:32:31 +0000765#if defined(__alpha__)
766#define HOST_HZ 1024
767#else
768#define HOST_HZ 100
769#endif
770
blueswir1992f48a2007-10-14 16:27:31 +0000771static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000772{
773#if HOST_HZ == TARGET_HZ
774 return ticks;
775#else
776 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
777#endif
778}
779
bellard579a97f2007-11-11 14:26:47 +0000780static inline abi_long host_to_target_rusage(abi_ulong target_addr,
781 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000782{
pbrook53a59602006-03-25 19:31:22 +0000783 struct target_rusage *target_rusage;
784
bellard579a97f2007-11-11 14:26:47 +0000785 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
786 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200787 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
788 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
789 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
790 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
791 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
792 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
793 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
794 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
795 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
796 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
797 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
798 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
799 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
800 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
801 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
802 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
803 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
804 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000805 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000806
807 return 0;
bellardb4091862003-05-16 15:39:34 +0000808}
809
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200810static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900811{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200812 abi_ulong target_rlim_swap;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300813 rlim_t result;
814
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200815 target_rlim_swap = tswapal(target_rlim);
816 if (target_rlim_swap == TARGET_RLIM_INFINITY)
817 return RLIM_INFINITY;
818
819 result = target_rlim_swap;
820 if (target_rlim_swap != (rlim_t)result)
821 return RLIM_INFINITY;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300822
823 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900824}
825
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200826static inline abi_ulong host_to_target_rlim(rlim_t rlim)
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900827{
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200828 abi_ulong target_rlim_swap;
829 abi_ulong result;
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300830
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200831 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300832 target_rlim_swap = TARGET_RLIM_INFINITY;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900833 else
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300834 target_rlim_swap = rlim;
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200835 result = tswapal(target_rlim_swap);
Wesley W. Terpstra95b33b22011-07-12 14:38:22 +0300836
837 return result;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900838}
839
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +0300840static inline int target_to_host_resource(int code)
841{
842 switch (code) {
843 case TARGET_RLIMIT_AS:
844 return RLIMIT_AS;
845 case TARGET_RLIMIT_CORE:
846 return RLIMIT_CORE;
847 case TARGET_RLIMIT_CPU:
848 return RLIMIT_CPU;
849 case TARGET_RLIMIT_DATA:
850 return RLIMIT_DATA;
851 case TARGET_RLIMIT_FSIZE:
852 return RLIMIT_FSIZE;
853 case TARGET_RLIMIT_LOCKS:
854 return RLIMIT_LOCKS;
855 case TARGET_RLIMIT_MEMLOCK:
856 return RLIMIT_MEMLOCK;
857 case TARGET_RLIMIT_MSGQUEUE:
858 return RLIMIT_MSGQUEUE;
859 case TARGET_RLIMIT_NICE:
860 return RLIMIT_NICE;
861 case TARGET_RLIMIT_NOFILE:
862 return RLIMIT_NOFILE;
863 case TARGET_RLIMIT_NPROC:
864 return RLIMIT_NPROC;
865 case TARGET_RLIMIT_RSS:
866 return RLIMIT_RSS;
867 case TARGET_RLIMIT_RTPRIO:
868 return RLIMIT_RTPRIO;
869 case TARGET_RLIMIT_SIGPENDING:
870 return RLIMIT_SIGPENDING;
871 case TARGET_RLIMIT_STACK:
872 return RLIMIT_STACK;
873 default:
874 return code;
875 }
876}
877
ths788f5ec2007-12-09 02:37:05 +0000878static inline abi_long copy_from_user_timeval(struct timeval *tv,
879 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000880{
pbrook53a59602006-03-25 19:31:22 +0000881 struct target_timeval *target_tv;
882
ths788f5ec2007-12-09 02:37:05 +0000883 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000884 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000885
886 __get_user(tv->tv_sec, &target_tv->tv_sec);
887 __get_user(tv->tv_usec, &target_tv->tv_usec);
888
889 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000890
891 return 0;
bellard31e31b82003-02-18 22:55:36 +0000892}
893
ths788f5ec2007-12-09 02:37:05 +0000894static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
895 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000896{
pbrook53a59602006-03-25 19:31:22 +0000897 struct target_timeval *target_tv;
898
ths788f5ec2007-12-09 02:37:05 +0000899 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000900 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000901
902 __put_user(tv->tv_sec, &target_tv->tv_sec);
903 __put_user(tv->tv_usec, &target_tv->tv_usec);
904
905 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000906
907 return 0;
bellard31e31b82003-02-18 22:55:36 +0000908}
909
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700910#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
911#include <mqueue.h>
912
aurel3224e10032009-04-15 16:11:43 +0000913static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
914 abi_ulong target_mq_attr_addr)
915{
916 struct target_mq_attr *target_mq_attr;
917
918 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
919 target_mq_attr_addr, 1))
920 return -TARGET_EFAULT;
921
922 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
923 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
924 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
925 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
926
927 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
928
929 return 0;
930}
931
932static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
933 const struct mq_attr *attr)
934{
935 struct target_mq_attr *target_mq_attr;
936
937 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
938 target_mq_attr_addr, 0))
939 return -TARGET_EFAULT;
940
941 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
942 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
943 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
944 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
945
946 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
947
948 return 0;
949}
Nathan Froyd8ec9cf82009-07-22 09:14:36 -0700950#endif
bellard31e31b82003-02-18 22:55:36 +0000951
Mike Frysinger055e0902011-06-03 17:01:49 -0400952#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
ths0da46a62007-10-20 20:23:07 +0000953/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000954static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000955 abi_ulong rfd_addr, abi_ulong wfd_addr,
956 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000957{
958 fd_set rfds, wfds, efds;
959 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
960 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000961 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000962
Mike Frysinger055e0902011-06-03 17:01:49 -0400963 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
964 if (ret) {
965 return ret;
pbrook53a59602006-03-25 19:31:22 +0000966 }
Mike Frysinger055e0902011-06-03 17:01:49 -0400967 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
968 if (ret) {
969 return ret;
pbrook53a59602006-03-25 19:31:22 +0000970 }
Mike Frysinger055e0902011-06-03 17:01:49 -0400971 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
972 if (ret) {
973 return ret;
pbrook53a59602006-03-25 19:31:22 +0000974 }
ths3b46e622007-09-17 08:09:54 +0000975
ths26edcf42007-12-09 02:25:24 +0000976 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000977 if (copy_from_user_timeval(&tv, target_tv_addr))
978 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000979 tv_ptr = &tv;
980 } else {
981 tv_ptr = NULL;
982 }
ths26edcf42007-12-09 02:25:24 +0000983
bellard31e31b82003-02-18 22:55:36 +0000984 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000985
ths26edcf42007-12-09 02:25:24 +0000986 if (!is_error(ret)) {
987 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
988 return -TARGET_EFAULT;
989 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
990 return -TARGET_EFAULT;
991 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
992 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000993
ths788f5ec2007-12-09 02:37:05 +0000994 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
995 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000996 }
bellard579a97f2007-11-11 14:26:47 +0000997
bellard31e31b82003-02-18 22:55:36 +0000998 return ret;
999}
Mike Frysinger055e0902011-06-03 17:01:49 -04001000#endif
bellard31e31b82003-02-18 22:55:36 +00001001
Riku Voipio099d6b02009-05-05 12:10:04 +03001002static abi_long do_pipe2(int host_pipe[], int flags)
1003{
1004#ifdef CONFIG_PIPE2
1005 return pipe2(host_pipe, flags);
1006#else
1007 return -ENOSYS;
1008#endif
1009}
1010
Richard Hendersonfb41a662010-05-03 10:07:52 -07001011static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1012 int flags, int is_pipe2)
Riku Voipio099d6b02009-05-05 12:10:04 +03001013{
1014 int host_pipe[2];
1015 abi_long ret;
1016 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1017
1018 if (is_error(ret))
1019 return get_errno(ret);
Richard Hendersonfb41a662010-05-03 10:07:52 -07001020
1021 /* Several targets have special calling conventions for the original
1022 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1023 if (!is_pipe2) {
1024#if defined(TARGET_ALPHA)
1025 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1026 return host_pipe[0];
1027#elif defined(TARGET_MIPS)
1028 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1029 return host_pipe[0];
1030#elif defined(TARGET_SH4)
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001031 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
Richard Hendersonfb41a662010-05-03 10:07:52 -07001032 return host_pipe[0];
takasi-y@ops.dti.ne.jp597c0212010-02-18 00:35:03 +09001033#endif
Richard Hendersonfb41a662010-05-03 10:07:52 -07001034 }
1035
Riku Voipio099d6b02009-05-05 12:10:04 +03001036 if (put_user_s32(host_pipe[0], pipedes)
1037 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1038 return -TARGET_EFAULT;
Riku Voipio099d6b02009-05-05 12:10:04 +03001039 return get_errno(ret);
1040}
1041
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001042static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1043 abi_ulong target_addr,
1044 socklen_t len)
1045{
1046 struct target_ip_mreqn *target_smreqn;
1047
1048 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1049 if (!target_smreqn)
1050 return -TARGET_EFAULT;
1051 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1052 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1053 if (len == sizeof(struct target_ip_mreqn))
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001054 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001055 unlock_user(target_smreqn, target_addr, 0);
1056
1057 return 0;
1058}
1059
bellard579a97f2007-11-11 14:26:47 +00001060static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1061 abi_ulong target_addr,
1062 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001063{
aurel32607175e2009-04-15 16:11:59 +00001064 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1065 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +00001066 struct target_sockaddr *target_saddr;
1067
bellard579a97f2007-11-11 14:26:47 +00001068 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1069 if (!target_saddr)
1070 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +00001071
1072 sa_family = tswap16(target_saddr->sa_family);
1073
1074 /* Oops. The caller might send a incomplete sun_path; sun_path
1075 * must be terminated by \0 (see the manual page), but
1076 * unfortunately it is quite common to specify sockaddr_un
1077 * length as "strlen(x->sun_path)" while it should be
1078 * "strlen(...) + 1". We'll fix that here if needed.
1079 * Linux kernel has a similar feature.
1080 */
1081
1082 if (sa_family == AF_UNIX) {
1083 if (len < unix_maxlen && len > 0) {
1084 char *cp = (char*)target_saddr;
1085
1086 if ( cp[len-1] && !cp[len] )
1087 len++;
1088 }
1089 if (len > unix_maxlen)
1090 len = unix_maxlen;
1091 }
1092
pbrook53a59602006-03-25 19:31:22 +00001093 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +00001094 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +00001095 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001096
1097 return 0;
bellard7854b052003-03-29 17:22:23 +00001098}
1099
bellard579a97f2007-11-11 14:26:47 +00001100static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1101 struct sockaddr *addr,
1102 socklen_t len)
bellard7854b052003-03-29 17:22:23 +00001103{
pbrook53a59602006-03-25 19:31:22 +00001104 struct target_sockaddr *target_saddr;
1105
bellard579a97f2007-11-11 14:26:47 +00001106 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1107 if (!target_saddr)
1108 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001109 memcpy(target_saddr, addr, len);
1110 target_saddr->sa_family = tswap16(addr->sa_family);
1111 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +00001112
1113 return 0;
bellard7854b052003-03-29 17:22:23 +00001114}
1115
bellard5a4a8982007-11-11 17:39:18 +00001116static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1117 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001118{
1119 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001120 abi_long msg_controllen;
1121 abi_ulong target_cmsg_addr;
1122 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001123 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001124
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001125 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001126 if (msg_controllen < sizeof (struct target_cmsghdr))
1127 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001128 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001129 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1130 if (!target_cmsg)
1131 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001132
1133 while (cmsg && target_cmsg) {
1134 void *data = CMSG_DATA(cmsg);
1135 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1136
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001137 int len = tswapal(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001138 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1139
1140 space += CMSG_SPACE(len);
1141 if (space > msgh->msg_controllen) {
1142 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001143 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001144 break;
1145 }
1146
1147 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1148 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1149 cmsg->cmsg_len = CMSG_LEN(len);
1150
bellard3532fa72006-06-24 15:06:03 +00001151 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001152 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1153 memcpy(data, target_data, len);
1154 } else {
1155 int *fd = (int *)data;
1156 int *target_fd = (int *)target_data;
1157 int i, numfds = len / sizeof(int);
1158
1159 for (i = 0; i < numfds; i++)
1160 fd[i] = tswap32(target_fd[i]);
1161 }
1162
1163 cmsg = CMSG_NXTHDR(msgh, cmsg);
1164 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1165 }
bellard5a4a8982007-11-11 17:39:18 +00001166 unlock_user(target_cmsg, target_cmsg_addr, 0);
1167 the_end:
bellard7854b052003-03-29 17:22:23 +00001168 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001169 return 0;
bellard7854b052003-03-29 17:22:23 +00001170}
1171
bellard5a4a8982007-11-11 17:39:18 +00001172static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1173 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001174{
1175 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001176 abi_long msg_controllen;
1177 abi_ulong target_cmsg_addr;
1178 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001179 socklen_t space = 0;
1180
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001181 msg_controllen = tswapal(target_msgh->msg_controllen);
bellard5a4a8982007-11-11 17:39:18 +00001182 if (msg_controllen < sizeof (struct target_cmsghdr))
1183 goto the_end;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001184 target_cmsg_addr = tswapal(target_msgh->msg_control);
bellard5a4a8982007-11-11 17:39:18 +00001185 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1186 if (!target_cmsg)
1187 return -TARGET_EFAULT;
1188
bellard7854b052003-03-29 17:22:23 +00001189 while (cmsg && target_cmsg) {
1190 void *data = CMSG_DATA(cmsg);
1191 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1192
1193 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1194
1195 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001196 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001197 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001198 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001199 break;
1200 }
1201
1202 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1203 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001204 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
bellard7854b052003-03-29 17:22:23 +00001205
Jing Huangaebf5bc2012-07-24 14:01:42 +00001206 if ((cmsg->cmsg_level == TARGET_SOL_SOCKET) &&
1207 (cmsg->cmsg_type == SCM_RIGHTS)) {
bellard7854b052003-03-29 17:22:23 +00001208 int *fd = (int *)data;
1209 int *target_fd = (int *)target_data;
1210 int i, numfds = len / sizeof(int);
1211
1212 for (i = 0; i < numfds; i++)
1213 target_fd[i] = tswap32(fd[i]);
Jing Huangaebf5bc2012-07-24 14:01:42 +00001214 } else if ((cmsg->cmsg_level == TARGET_SOL_SOCKET) &&
1215 (cmsg->cmsg_type == SO_TIMESTAMP) &&
1216 (len == sizeof(struct timeval))) {
1217 /* copy struct timeval to target */
1218 struct timeval *tv = (struct timeval *)data;
1219 struct target_timeval *target_tv =
1220 (struct target_timeval *)target_data;
1221
1222 target_tv->tv_sec = tswapal(tv->tv_sec);
1223 target_tv->tv_usec = tswapal(tv->tv_usec);
1224 } else {
1225 gemu_log("Unsupported ancillary data: %d/%d\n",
1226 cmsg->cmsg_level, cmsg->cmsg_type);
1227 memcpy(target_data, data, len);
bellard7854b052003-03-29 17:22:23 +00001228 }
1229
1230 cmsg = CMSG_NXTHDR(msgh, cmsg);
1231 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1232 }
bellard5a4a8982007-11-11 17:39:18 +00001233 unlock_user(target_cmsg, target_cmsg_addr, space);
1234 the_end:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001235 target_msgh->msg_controllen = tswapal(space);
bellard5a4a8982007-11-11 17:39:18 +00001236 return 0;
bellard7854b052003-03-29 17:22:23 +00001237}
1238
ths0da46a62007-10-20 20:23:07 +00001239/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001240static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001241 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001242{
blueswir1992f48a2007-10-14 16:27:31 +00001243 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001244 int val;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001245 struct ip_mreqn *ip_mreq;
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001246 struct ip_mreq_source *ip_mreq_source;
ths3b46e622007-09-17 08:09:54 +00001247
bellard8853f862004-02-22 14:57:26 +00001248 switch(level) {
1249 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001250 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001251 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001252 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001253
bellard2f619692007-11-16 10:46:05 +00001254 if (get_user_u32(val, optval_addr))
1255 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001256 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1257 break;
1258 case SOL_IP:
1259 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001260 case IP_TOS:
1261 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001262 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001263 case IP_ROUTER_ALERT:
1264 case IP_RECVOPTS:
1265 case IP_RETOPTS:
1266 case IP_PKTINFO:
1267 case IP_MTU_DISCOVER:
1268 case IP_RECVERR:
1269 case IP_RECVTOS:
1270#ifdef IP_FREEBIND
1271 case IP_FREEBIND:
1272#endif
1273 case IP_MULTICAST_TTL:
1274 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001275 val = 0;
1276 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001277 if (get_user_u32(val, optval_addr))
1278 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001279 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001280 if (get_user_u8(val, optval_addr))
1281 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001282 }
1283 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1284 break;
Lionel Landwerlinb975b832009-04-25 23:30:19 +02001285 case IP_ADD_MEMBERSHIP:
1286 case IP_DROP_MEMBERSHIP:
1287 if (optlen < sizeof (struct target_ip_mreq) ||
1288 optlen > sizeof (struct target_ip_mreqn))
1289 return -TARGET_EINVAL;
1290
1291 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1292 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1293 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1294 break;
1295
Lionel Landwerlin6e3cb582009-04-25 23:31:18 +02001296 case IP_BLOCK_SOURCE:
1297 case IP_UNBLOCK_SOURCE:
1298 case IP_ADD_SOURCE_MEMBERSHIP:
1299 case IP_DROP_SOURCE_MEMBERSHIP:
1300 if (optlen != sizeof (struct target_ip_mreq_source))
1301 return -TARGET_EINVAL;
1302
1303 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1304 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1305 unlock_user (ip_mreq_source, optval_addr, 0);
1306 break;
1307
bellard8853f862004-02-22 14:57:26 +00001308 default:
1309 goto unimplemented;
1310 }
1311 break;
Jing Huang920394d2012-07-24 13:59:23 +00001312 case SOL_RAW:
1313 switch (optname) {
1314 case ICMP_FILTER:
1315 /* struct icmp_filter takes an u32 value */
1316 if (optlen < sizeof(uint32_t)) {
1317 return -TARGET_EINVAL;
1318 }
1319
1320 if (get_user_u32(val, optval_addr)) {
1321 return -TARGET_EFAULT;
1322 }
1323 ret = get_errno(setsockopt(sockfd, level, optname,
1324 &val, sizeof(val)));
1325 break;
1326
1327 default:
1328 goto unimplemented;
1329 }
1330 break;
bellard3532fa72006-06-24 15:06:03 +00001331 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001332 switch (optname) {
Laurent Vivier1b09aeb2013-01-01 08:24:11 +00001333 case TARGET_SO_RCVTIMEO:
1334 {
1335 struct timeval tv;
1336
1337 optname = SO_RCVTIMEO;
1338
1339set_timeout:
1340 if (optlen != sizeof(struct target_timeval)) {
1341 return -TARGET_EINVAL;
1342 }
1343
1344 if (copy_from_user_timeval(&tv, optval_addr)) {
1345 return -TARGET_EFAULT;
1346 }
1347
1348 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1349 &tv, sizeof(tv)));
1350 return ret;
1351 }
1352 case TARGET_SO_SNDTIMEO:
1353 optname = SO_SNDTIMEO;
1354 goto set_timeout;
bellard8853f862004-02-22 14:57:26 +00001355 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001356 case TARGET_SO_DEBUG:
1357 optname = SO_DEBUG;
1358 break;
1359 case TARGET_SO_REUSEADDR:
1360 optname = SO_REUSEADDR;
1361 break;
1362 case TARGET_SO_TYPE:
1363 optname = SO_TYPE;
1364 break;
1365 case TARGET_SO_ERROR:
1366 optname = SO_ERROR;
1367 break;
1368 case TARGET_SO_DONTROUTE:
1369 optname = SO_DONTROUTE;
1370 break;
1371 case TARGET_SO_BROADCAST:
1372 optname = SO_BROADCAST;
1373 break;
1374 case TARGET_SO_SNDBUF:
1375 optname = SO_SNDBUF;
1376 break;
1377 case TARGET_SO_RCVBUF:
1378 optname = SO_RCVBUF;
1379 break;
1380 case TARGET_SO_KEEPALIVE:
1381 optname = SO_KEEPALIVE;
1382 break;
1383 case TARGET_SO_OOBINLINE:
1384 optname = SO_OOBINLINE;
1385 break;
1386 case TARGET_SO_NO_CHECK:
1387 optname = SO_NO_CHECK;
1388 break;
1389 case TARGET_SO_PRIORITY:
1390 optname = SO_PRIORITY;
1391 break;
bellard5e83e8e2005-03-01 22:32:06 +00001392#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001393 case TARGET_SO_BSDCOMPAT:
1394 optname = SO_BSDCOMPAT;
1395 break;
bellard5e83e8e2005-03-01 22:32:06 +00001396#endif
bellard3532fa72006-06-24 15:06:03 +00001397 case TARGET_SO_PASSCRED:
1398 optname = SO_PASSCRED;
1399 break;
1400 case TARGET_SO_TIMESTAMP:
1401 optname = SO_TIMESTAMP;
1402 break;
1403 case TARGET_SO_RCVLOWAT:
1404 optname = SO_RCVLOWAT;
1405 break;
bellard8853f862004-02-22 14:57:26 +00001406 break;
1407 default:
1408 goto unimplemented;
1409 }
bellard3532fa72006-06-24 15:06:03 +00001410 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001411 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001412
bellard2f619692007-11-16 10:46:05 +00001413 if (get_user_u32(val, optval_addr))
1414 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001415 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001416 break;
bellard7854b052003-03-29 17:22:23 +00001417 default:
bellard8853f862004-02-22 14:57:26 +00001418 unimplemented:
Stefan Weilb2bedb22011-09-12 22:33:01 +02001419 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001420 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001421 }
bellard8853f862004-02-22 14:57:26 +00001422 return ret;
bellard7854b052003-03-29 17:22:23 +00001423}
1424
ths0da46a62007-10-20 20:23:07 +00001425/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001426static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001427 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001428{
blueswir1992f48a2007-10-14 16:27:31 +00001429 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001430 int len, val;
1431 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001432
1433 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001434 case TARGET_SOL_SOCKET:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001435 level = SOL_SOCKET;
1436 switch (optname) {
1437 /* These don't just return a single integer */
1438 case TARGET_SO_LINGER:
1439 case TARGET_SO_RCVTIMEO:
1440 case TARGET_SO_SNDTIMEO:
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001441 case TARGET_SO_PEERNAME:
1442 goto unimplemented;
Akos PASZTORY583359a2011-11-14 15:09:49 +02001443 case TARGET_SO_PEERCRED: {
1444 struct ucred cr;
1445 socklen_t crlen;
1446 struct target_ucred *tcr;
1447
1448 if (get_user_u32(len, optlen)) {
1449 return -TARGET_EFAULT;
1450 }
1451 if (len < 0) {
1452 return -TARGET_EINVAL;
1453 }
1454
1455 crlen = sizeof(cr);
1456 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1457 &cr, &crlen));
1458 if (ret < 0) {
1459 return ret;
1460 }
1461 if (len > crlen) {
1462 len = crlen;
1463 }
1464 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1465 return -TARGET_EFAULT;
1466 }
1467 __put_user(cr.pid, &tcr->pid);
1468 __put_user(cr.uid, &tcr->uid);
1469 __put_user(cr.gid, &tcr->gid);
1470 unlock_user_struct(tcr, optval_addr, 1);
1471 if (put_user_u32(len, optlen)) {
1472 return -TARGET_EFAULT;
1473 }
1474 break;
1475 }
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001476 /* Options with 'int' argument. */
1477 case TARGET_SO_DEBUG:
1478 optname = SO_DEBUG;
1479 goto int_case;
1480 case TARGET_SO_REUSEADDR:
1481 optname = SO_REUSEADDR;
1482 goto int_case;
1483 case TARGET_SO_TYPE:
1484 optname = SO_TYPE;
1485 goto int_case;
1486 case TARGET_SO_ERROR:
1487 optname = SO_ERROR;
1488 goto int_case;
1489 case TARGET_SO_DONTROUTE:
1490 optname = SO_DONTROUTE;
1491 goto int_case;
1492 case TARGET_SO_BROADCAST:
1493 optname = SO_BROADCAST;
1494 goto int_case;
1495 case TARGET_SO_SNDBUF:
1496 optname = SO_SNDBUF;
1497 goto int_case;
1498 case TARGET_SO_RCVBUF:
1499 optname = SO_RCVBUF;
1500 goto int_case;
1501 case TARGET_SO_KEEPALIVE:
1502 optname = SO_KEEPALIVE;
1503 goto int_case;
1504 case TARGET_SO_OOBINLINE:
1505 optname = SO_OOBINLINE;
1506 goto int_case;
1507 case TARGET_SO_NO_CHECK:
1508 optname = SO_NO_CHECK;
1509 goto int_case;
1510 case TARGET_SO_PRIORITY:
1511 optname = SO_PRIORITY;
1512 goto int_case;
1513#ifdef SO_BSDCOMPAT
1514 case TARGET_SO_BSDCOMPAT:
1515 optname = SO_BSDCOMPAT;
1516 goto int_case;
1517#endif
1518 case TARGET_SO_PASSCRED:
1519 optname = SO_PASSCRED;
1520 goto int_case;
1521 case TARGET_SO_TIMESTAMP:
1522 optname = SO_TIMESTAMP;
1523 goto int_case;
1524 case TARGET_SO_RCVLOWAT:
1525 optname = SO_RCVLOWAT;
1526 goto int_case;
bellard8853f862004-02-22 14:57:26 +00001527 default:
bellard2efbe912005-07-23 15:10:20 +00001528 goto int_case;
1529 }
1530 break;
1531 case SOL_TCP:
1532 /* TCP options all take an 'int' value. */
1533 int_case:
bellard2f619692007-11-16 10:46:05 +00001534 if (get_user_u32(len, optlen))
1535 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001536 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001537 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001538 lv = sizeof(lv);
bellard2efbe912005-07-23 15:10:20 +00001539 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1540 if (ret < 0)
1541 return ret;
bellard2efbe912005-07-23 15:10:20 +00001542 if (len > lv)
1543 len = lv;
bellard2f619692007-11-16 10:46:05 +00001544 if (len == 4) {
1545 if (put_user_u32(val, optval_addr))
1546 return -TARGET_EFAULT;
1547 } else {
1548 if (put_user_u8(val, optval_addr))
1549 return -TARGET_EFAULT;
Jamie Lentinf3b974c2010-11-26 15:04:08 +02001550 }
bellard2f619692007-11-16 10:46:05 +00001551 if (put_user_u32(len, optlen))
1552 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001553 break;
1554 case SOL_IP:
1555 switch(optname) {
1556 case IP_TOS:
1557 case IP_TTL:
1558 case IP_HDRINCL:
1559 case IP_ROUTER_ALERT:
1560 case IP_RECVOPTS:
1561 case IP_RETOPTS:
1562 case IP_PKTINFO:
1563 case IP_MTU_DISCOVER:
1564 case IP_RECVERR:
1565 case IP_RECVTOS:
1566#ifdef IP_FREEBIND
1567 case IP_FREEBIND:
1568#endif
1569 case IP_MULTICAST_TTL:
1570 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001571 if (get_user_u32(len, optlen))
1572 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001573 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001574 return -TARGET_EINVAL;
Mike Frysinger73160d92011-02-07 01:05:49 -05001575 lv = sizeof(lv);
bellard8853f862004-02-22 14:57:26 +00001576 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1577 if (ret < 0)
1578 return ret;
bellard2efbe912005-07-23 15:10:20 +00001579 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001580 len = 1;
bellard2f619692007-11-16 10:46:05 +00001581 if (put_user_u32(len, optlen)
1582 || put_user_u8(val, optval_addr))
1583 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001584 } else {
bellard2efbe912005-07-23 15:10:20 +00001585 if (len > sizeof(int))
1586 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001587 if (put_user_u32(len, optlen)
1588 || put_user_u32(val, optval_addr))
1589 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001590 }
bellard8853f862004-02-22 14:57:26 +00001591 break;
bellard2efbe912005-07-23 15:10:20 +00001592 default:
thsc02f4992007-12-18 02:39:59 +00001593 ret = -TARGET_ENOPROTOOPT;
1594 break;
bellard8853f862004-02-22 14:57:26 +00001595 }
1596 break;
1597 default:
1598 unimplemented:
1599 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1600 level, optname);
thsc02f4992007-12-18 02:39:59 +00001601 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001602 break;
1603 }
1604 return ret;
bellard7854b052003-03-29 17:22:23 +00001605}
1606
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001607static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1608 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001609{
1610 struct target_iovec *target_vec;
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001611 struct iovec *vec;
1612 abi_ulong total_len, max_len;
balrogd732dcb2008-10-28 10:21:03 +00001613 int i;
pbrook53a59602006-03-25 19:31:22 +00001614
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001615 if (count == 0) {
1616 errno = 0;
1617 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001618 }
Peter Maydelldfae8e02013-02-08 07:58:41 +00001619 if (count < 0 || count > IOV_MAX) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001620 errno = EINVAL;
1621 return NULL;
1622 }
1623
1624 vec = calloc(count, sizeof(struct iovec));
1625 if (vec == NULL) {
1626 errno = ENOMEM;
1627 return NULL;
1628 }
1629
1630 target_vec = lock_user(VERIFY_READ, target_addr,
1631 count * sizeof(struct target_iovec), 1);
1632 if (target_vec == NULL) {
1633 errno = EFAULT;
1634 goto fail2;
1635 }
1636
1637 /* ??? If host page size > target page size, this will result in a
1638 value larger than what we can actually support. */
1639 max_len = 0x7fffffff & TARGET_PAGE_MASK;
1640 total_len = 0;
1641
1642 for (i = 0; i < count; i++) {
1643 abi_ulong base = tswapal(target_vec[i].iov_base);
1644 abi_long len = tswapal(target_vec[i].iov_len);
1645
1646 if (len < 0) {
1647 errno = EINVAL;
1648 goto fail;
1649 } else if (len == 0) {
1650 /* Zero length pointer is ignored. */
1651 vec[i].iov_base = 0;
1652 } else {
1653 vec[i].iov_base = lock_user(type, base, len, copy);
1654 if (!vec[i].iov_base) {
1655 errno = EFAULT;
1656 goto fail;
1657 }
1658 if (len > max_len - total_len) {
1659 len = max_len - total_len;
1660 }
1661 }
1662 vec[i].iov_len = len;
1663 total_len += len;
1664 }
1665
1666 unlock_user(target_vec, target_addr, 0);
1667 return vec;
1668
1669 fail:
1670 free(vec);
1671 fail2:
1672 unlock_user(target_vec, target_addr, 0);
1673 return NULL;
pbrook53a59602006-03-25 19:31:22 +00001674}
1675
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001676static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1677 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001678{
1679 struct target_iovec *target_vec;
pbrook53a59602006-03-25 19:31:22 +00001680 int i;
1681
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001682 target_vec = lock_user(VERIFY_READ, target_addr,
1683 count * sizeof(struct target_iovec), 1);
1684 if (target_vec) {
1685 for (i = 0; i < count; i++) {
1686 abi_ulong base = tswapal(target_vec[i].iov_base);
1687 abi_long len = tswapal(target_vec[i].iov_base);
1688 if (len < 0) {
1689 break;
1690 }
balrogd732dcb2008-10-28 10:21:03 +00001691 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1692 }
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001693 unlock_user(target_vec, target_addr, 0);
pbrook53a59602006-03-25 19:31:22 +00001694 }
bellard579a97f2007-11-11 14:26:47 +00001695
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001696 free(vec);
pbrook53a59602006-03-25 19:31:22 +00001697}
1698
ths0da46a62007-10-20 20:23:07 +00001699/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001700static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001701{
1702#if defined(TARGET_MIPS)
1703 switch(type) {
1704 case TARGET_SOCK_DGRAM:
1705 type = SOCK_DGRAM;
1706 break;
1707 case TARGET_SOCK_STREAM:
1708 type = SOCK_STREAM;
1709 break;
1710 case TARGET_SOCK_RAW:
1711 type = SOCK_RAW;
1712 break;
1713 case TARGET_SOCK_RDM:
1714 type = SOCK_RDM;
1715 break;
1716 case TARGET_SOCK_SEQPACKET:
1717 type = SOCK_SEQPACKET;
1718 break;
1719 case TARGET_SOCK_PACKET:
1720 type = SOCK_PACKET;
1721 break;
1722 }
1723#endif
balrog12bc92a2007-10-30 21:06:14 +00001724 if (domain == PF_NETLINK)
1725 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001726 return get_errno(socket(domain, type, protocol));
1727}
1728
ths0da46a62007-10-20 20:23:07 +00001729/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001730static abi_long do_bind(int sockfd, abi_ulong target_addr,
1731 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001732{
aurel328f7aeaf2009-01-30 19:47:57 +00001733 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001734 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001735
Blue Swirl38724252010-09-18 05:53:14 +00001736 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001737 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001738 }
aurel328f7aeaf2009-01-30 19:47:57 +00001739
aurel32607175e2009-04-15 16:11:59 +00001740 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001741
Arnaud Patard917507b2009-06-19 10:44:45 +03001742 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1743 if (ret)
1744 return ret;
1745
bellard3532fa72006-06-24 15:06:03 +00001746 return get_errno(bind(sockfd, addr, addrlen));
1747}
1748
ths0da46a62007-10-20 20:23:07 +00001749/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001750static abi_long do_connect(int sockfd, abi_ulong target_addr,
1751 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001752{
aurel328f7aeaf2009-01-30 19:47:57 +00001753 void *addr;
Arnaud Patard917507b2009-06-19 10:44:45 +03001754 abi_long ret;
aurel328f7aeaf2009-01-30 19:47:57 +00001755
Blue Swirl38724252010-09-18 05:53:14 +00001756 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001757 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001758 }
aurel328f7aeaf2009-01-30 19:47:57 +00001759
1760 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001761
Arnaud Patard917507b2009-06-19 10:44:45 +03001762 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1763 if (ret)
1764 return ret;
1765
bellard3532fa72006-06-24 15:06:03 +00001766 return get_errno(connect(sockfd, addr, addrlen));
1767}
1768
ths0da46a62007-10-20 20:23:07 +00001769/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001770static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1771 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001772{
balrog6de645c2008-10-28 10:26:29 +00001773 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001774 struct target_msghdr *msgp;
1775 struct msghdr msg;
1776 int count;
1777 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001778 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001779
bellard579a97f2007-11-11 14:26:47 +00001780 /* FIXME */
1781 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1782 msgp,
1783 target_msg,
1784 send ? 1 : 0))
1785 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001786 if (msgp->msg_name) {
1787 msg.msg_namelen = tswap32(msgp->msg_namelen);
1788 msg.msg_name = alloca(msg.msg_namelen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001789 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
bellard3532fa72006-06-24 15:06:03 +00001790 msg.msg_namelen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001791 if (ret) {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001792 goto out2;
Arnaud Patard917507b2009-06-19 10:44:45 +03001793 }
bellard3532fa72006-06-24 15:06:03 +00001794 } else {
1795 msg.msg_name = NULL;
1796 msg.msg_namelen = 0;
1797 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001798 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
bellard3532fa72006-06-24 15:06:03 +00001799 msg.msg_control = alloca(msg.msg_controllen);
1800 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001801
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001802 count = tswapal(msgp->msg_iovlen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02001803 target_vec = tswapal(msgp->msg_iov);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001804 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
1805 target_vec, count, send);
1806 if (vec == NULL) {
1807 ret = -host_to_target_errno(errno);
1808 goto out2;
1809 }
bellard3532fa72006-06-24 15:06:03 +00001810 msg.msg_iovlen = count;
1811 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001812
bellard3532fa72006-06-24 15:06:03 +00001813 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001814 ret = target_to_host_cmsg(&msg, msgp);
1815 if (ret == 0)
1816 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001817 } else {
1818 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001819 if (!is_error(ret)) {
1820 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001821 ret = host_to_target_cmsg(msgp, &msg);
Jing Huangca619062012-07-24 13:58:02 +00001822 if (!is_error(ret)) {
1823 msgp->msg_namelen = tswap32(msg.msg_namelen);
1824 if (msg.msg_name != NULL) {
1825 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
1826 msg.msg_name, msg.msg_namelen);
1827 if (ret) {
1828 goto out;
1829 }
1830 }
1831
balrog6de645c2008-10-28 10:26:29 +00001832 ret = len;
Jing Huangca619062012-07-24 13:58:02 +00001833 }
balrog6de645c2008-10-28 10:26:29 +00001834 }
bellard3532fa72006-06-24 15:06:03 +00001835 }
Jing Huangca619062012-07-24 13:58:02 +00001836
1837out:
bellard3532fa72006-06-24 15:06:03 +00001838 unlock_iovec(vec, target_vec, count, !send);
Richard Hendersonf287b2c2012-09-15 13:20:25 -07001839out2:
bellard579a97f2007-11-11 14:26:47 +00001840 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001841 return ret;
1842}
1843
Peter Maydella94b4982013-02-08 04:35:04 +00001844/* If we don't have a system accept4() then just call accept.
1845 * The callsites to do_accept4() will ensure that they don't
1846 * pass a non-zero flags argument in this config.
1847 */
1848#ifndef CONFIG_ACCEPT4
1849static inline int accept4(int sockfd, struct sockaddr *addr,
1850 socklen_t *addrlen, int flags)
1851{
1852 assert(flags == 0);
1853 return accept(sockfd, addr, addrlen);
1854}
1855#endif
1856
1857/* do_accept4() Must return target values and target errnos. */
1858static abi_long do_accept4(int fd, abi_ulong target_addr,
1859 abi_ulong target_addrlen_addr, int flags)
pbrook1be9e1d2006-11-19 15:26:04 +00001860{
bellard2f619692007-11-16 10:46:05 +00001861 socklen_t addrlen;
1862 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001863 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001864
Peter Maydella94b4982013-02-08 04:35:04 +00001865 if (target_addr == 0) {
1866 return get_errno(accept4(fd, NULL, NULL, flags));
1867 }
Arnaud Patard917507b2009-06-19 10:44:45 +03001868
1869 /* linux returns EINVAL if addrlen pointer is invalid */
bellard2f619692007-11-16 10:46:05 +00001870 if (get_user_u32(addrlen, target_addrlen_addr))
Arnaud Patard917507b2009-06-19 10:44:45 +03001871 return -TARGET_EINVAL;
bellard2f619692007-11-16 10:46:05 +00001872
Blue Swirl38724252010-09-18 05:53:14 +00001873 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001874 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001875 }
aurel328f7aeaf2009-01-30 19:47:57 +00001876
Arnaud Patard917507b2009-06-19 10:44:45 +03001877 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1878 return -TARGET_EINVAL;
1879
bellard2f619692007-11-16 10:46:05 +00001880 addr = alloca(addrlen);
1881
Peter Maydella94b4982013-02-08 04:35:04 +00001882 ret = get_errno(accept4(fd, addr, &addrlen, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00001883 if (!is_error(ret)) {
1884 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001885 if (put_user_u32(addrlen, target_addrlen_addr))
1886 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001887 }
1888 return ret;
1889}
1890
ths0da46a62007-10-20 20:23:07 +00001891/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001892static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001893 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001894{
bellard2f619692007-11-16 10:46:05 +00001895 socklen_t addrlen;
1896 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001897 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001898
bellard2f619692007-11-16 10:46:05 +00001899 if (get_user_u32(addrlen, target_addrlen_addr))
1900 return -TARGET_EFAULT;
1901
Blue Swirl38724252010-09-18 05:53:14 +00001902 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001903 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001904 }
aurel328f7aeaf2009-01-30 19:47:57 +00001905
Arnaud Patard917507b2009-06-19 10:44:45 +03001906 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1907 return -TARGET_EFAULT;
1908
bellard2f619692007-11-16 10:46:05 +00001909 addr = alloca(addrlen);
1910
pbrook1be9e1d2006-11-19 15:26:04 +00001911 ret = get_errno(getpeername(fd, addr, &addrlen));
1912 if (!is_error(ret)) {
1913 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001914 if (put_user_u32(addrlen, target_addrlen_addr))
1915 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001916 }
1917 return ret;
1918}
1919
ths0da46a62007-10-20 20:23:07 +00001920/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001921static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001922 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001923{
bellard2f619692007-11-16 10:46:05 +00001924 socklen_t addrlen;
1925 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001926 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001927
bellard2f619692007-11-16 10:46:05 +00001928 if (get_user_u32(addrlen, target_addrlen_addr))
1929 return -TARGET_EFAULT;
1930
Blue Swirl38724252010-09-18 05:53:14 +00001931 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001932 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001933 }
aurel328f7aeaf2009-01-30 19:47:57 +00001934
Arnaud Patard917507b2009-06-19 10:44:45 +03001935 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1936 return -TARGET_EFAULT;
1937
bellard2f619692007-11-16 10:46:05 +00001938 addr = alloca(addrlen);
1939
pbrook1be9e1d2006-11-19 15:26:04 +00001940 ret = get_errno(getsockname(fd, addr, &addrlen));
1941 if (!is_error(ret)) {
1942 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001943 if (put_user_u32(addrlen, target_addrlen_addr))
1944 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001945 }
1946 return ret;
1947}
1948
ths0da46a62007-10-20 20:23:07 +00001949/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001950static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001951 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001952{
1953 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001954 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001955
1956 ret = get_errno(socketpair(domain, type, protocol, tab));
1957 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001958 if (put_user_s32(tab[0], target_tab_addr)
1959 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1960 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001961 }
1962 return ret;
1963}
1964
ths0da46a62007-10-20 20:23:07 +00001965/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001966static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1967 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001968{
1969 void *addr;
1970 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001971 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001972
Blue Swirl38724252010-09-18 05:53:14 +00001973 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001974 return -TARGET_EINVAL;
Blue Swirl38724252010-09-18 05:53:14 +00001975 }
aurel328f7aeaf2009-01-30 19:47:57 +00001976
bellard579a97f2007-11-11 14:26:47 +00001977 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1978 if (!host_msg)
1979 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001980 if (target_addr) {
1981 addr = alloca(addrlen);
Arnaud Patard917507b2009-06-19 10:44:45 +03001982 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1983 if (ret) {
1984 unlock_user(host_msg, msg, 0);
1985 return ret;
1986 }
pbrook1be9e1d2006-11-19 15:26:04 +00001987 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1988 } else {
1989 ret = get_errno(send(fd, host_msg, len, flags));
1990 }
1991 unlock_user(host_msg, msg, 0);
1992 return ret;
1993}
1994
ths0da46a62007-10-20 20:23:07 +00001995/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001996static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1997 abi_ulong target_addr,
1998 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001999{
2000 socklen_t addrlen;
2001 void *addr;
2002 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00002003 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00002004
bellard579a97f2007-11-11 14:26:47 +00002005 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2006 if (!host_msg)
2007 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00002008 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00002009 if (get_user_u32(addrlen, target_addrlen)) {
2010 ret = -TARGET_EFAULT;
2011 goto fail;
2012 }
Blue Swirl38724252010-09-18 05:53:14 +00002013 if ((int)addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00002014 ret = -TARGET_EINVAL;
2015 goto fail;
2016 }
pbrook1be9e1d2006-11-19 15:26:04 +00002017 addr = alloca(addrlen);
2018 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2019 } else {
2020 addr = NULL; /* To keep compiler quiet. */
Blue Swirl00aa0042011-07-23 20:04:29 +00002021 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
pbrook1be9e1d2006-11-19 15:26:04 +00002022 }
2023 if (!is_error(ret)) {
2024 if (target_addr) {
2025 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00002026 if (put_user_u32(addrlen, target_addrlen)) {
2027 ret = -TARGET_EFAULT;
2028 goto fail;
2029 }
pbrook1be9e1d2006-11-19 15:26:04 +00002030 }
2031 unlock_user(host_msg, msg, len);
2032 } else {
bellard2f619692007-11-16 10:46:05 +00002033fail:
pbrook1be9e1d2006-11-19 15:26:04 +00002034 unlock_user(host_msg, msg, 0);
2035 }
2036 return ret;
2037}
2038
j_mayer32407102007-09-26 23:01:49 +00002039#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00002040/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002041static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00002042{
blueswir1992f48a2007-10-14 16:27:31 +00002043 abi_long ret;
2044 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00002045
2046 switch(num) {
2047 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00002048 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002049 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002050
Ulrich Hecht98818182009-07-03 17:09:28 +02002051 if (get_user_ual(domain, vptr)
2052 || get_user_ual(type, vptr + n)
2053 || get_user_ual(protocol, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002054 return -TARGET_EFAULT;
2055
bellard3532fa72006-06-24 15:06:03 +00002056 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00002057 }
bellard31e31b82003-02-18 22:55:36 +00002058 break;
2059 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00002060 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002061 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002062 abi_ulong target_addr;
2063 socklen_t addrlen;
2064
Ulrich Hecht98818182009-07-03 17:09:28 +02002065 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002066 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002067 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002068 return -TARGET_EFAULT;
2069
bellard3532fa72006-06-24 15:06:03 +00002070 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002071 }
bellard31e31b82003-02-18 22:55:36 +00002072 break;
2073 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00002074 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002075 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002076 abi_ulong target_addr;
2077 socklen_t addrlen;
2078
Ulrich Hecht98818182009-07-03 17:09:28 +02002079 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002080 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002081 || get_user_ual(addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002082 return -TARGET_EFAULT;
2083
bellard3532fa72006-06-24 15:06:03 +00002084 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002085 }
bellard31e31b82003-02-18 22:55:36 +00002086 break;
2087 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00002088 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002089 abi_ulong sockfd, backlog;
bellard2f619692007-11-16 10:46:05 +00002090
Ulrich Hecht98818182009-07-03 17:09:28 +02002091 if (get_user_ual(sockfd, vptr)
2092 || get_user_ual(backlog, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002093 return -TARGET_EFAULT;
2094
bellard7854b052003-03-29 17:22:23 +00002095 ret = get_errno(listen(sockfd, backlog));
2096 }
bellard31e31b82003-02-18 22:55:36 +00002097 break;
2098 case SOCKOP_accept:
2099 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002100 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002101 abi_ulong target_addr, target_addrlen;
2102
Ulrich Hecht98818182009-07-03 17:09:28 +02002103 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002104 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002105 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002106 return -TARGET_EFAULT;
2107
Peter Maydella94b4982013-02-08 04:35:04 +00002108 ret = do_accept4(sockfd, target_addr, target_addrlen, 0);
bellard31e31b82003-02-18 22:55:36 +00002109 }
2110 break;
2111 case SOCKOP_getsockname:
2112 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002113 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002114 abi_ulong target_addr, target_addrlen;
2115
Ulrich Hecht98818182009-07-03 17:09:28 +02002116 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002117 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002118 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002119 return -TARGET_EFAULT;
2120
pbrook1be9e1d2006-11-19 15:26:04 +00002121 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002122 }
2123 break;
2124 case SOCKOP_getpeername:
2125 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002126 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002127 abi_ulong target_addr, target_addrlen;
2128
Ulrich Hecht98818182009-07-03 17:09:28 +02002129 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002130 || get_user_ual(target_addr, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002131 || get_user_ual(target_addrlen, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002132 return -TARGET_EFAULT;
2133
pbrook1be9e1d2006-11-19 15:26:04 +00002134 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00002135 }
2136 break;
2137 case SOCKOP_socketpair:
2138 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002139 abi_ulong domain, type, protocol;
bellard2f619692007-11-16 10:46:05 +00002140 abi_ulong tab;
2141
Ulrich Hecht98818182009-07-03 17:09:28 +02002142 if (get_user_ual(domain, vptr)
2143 || get_user_ual(type, vptr + n)
2144 || get_user_ual(protocol, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002145 || get_user_ual(tab, vptr + 3 * n))
2146 return -TARGET_EFAULT;
2147
pbrook1be9e1d2006-11-19 15:26:04 +00002148 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00002149 }
2150 break;
2151 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00002152 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002153 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002154 abi_ulong msg;
2155 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002156 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002157
Ulrich Hecht98818182009-07-03 17:09:28 +02002158 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002159 || get_user_ual(msg, vptr + n)
2160 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002161 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002162 return -TARGET_EFAULT;
2163
pbrook1be9e1d2006-11-19 15:26:04 +00002164 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002165 }
bellard31e31b82003-02-18 22:55:36 +00002166 break;
2167 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00002168 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002169 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002170 abi_ulong msg;
2171 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002172 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002173
Ulrich Hecht98818182009-07-03 17:09:28 +02002174 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002175 || get_user_ual(msg, vptr + n)
2176 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002177 || get_user_ual(flags, vptr + 3 * n))
bellard2f619692007-11-16 10:46:05 +00002178 return -TARGET_EFAULT;
2179
pbrook1be9e1d2006-11-19 15:26:04 +00002180 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00002181 }
bellard31e31b82003-02-18 22:55:36 +00002182 break;
2183 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00002184 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002185 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002186 abi_ulong msg;
2187 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002188 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002189 abi_ulong addr;
2190 socklen_t addrlen;
2191
Ulrich Hecht98818182009-07-03 17:09:28 +02002192 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002193 || get_user_ual(msg, vptr + n)
2194 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002195 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002196 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002197 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002198 return -TARGET_EFAULT;
2199
pbrook1be9e1d2006-11-19 15:26:04 +00002200 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00002201 }
bellard31e31b82003-02-18 22:55:36 +00002202 break;
2203 case SOCKOP_recvfrom:
2204 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002205 abi_ulong sockfd;
bellard2f619692007-11-16 10:46:05 +00002206 abi_ulong msg;
2207 size_t len;
Ulrich Hecht98818182009-07-03 17:09:28 +02002208 abi_ulong flags;
bellard2f619692007-11-16 10:46:05 +00002209 abi_ulong addr;
2210 socklen_t addrlen;
2211
Ulrich Hecht98818182009-07-03 17:09:28 +02002212 if (get_user_ual(sockfd, vptr)
bellard2f619692007-11-16 10:46:05 +00002213 || get_user_ual(msg, vptr + n)
2214 || get_user_ual(len, vptr + 2 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002215 || get_user_ual(flags, vptr + 3 * n)
bellard2f619692007-11-16 10:46:05 +00002216 || get_user_ual(addr, vptr + 4 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002217 || get_user_ual(addrlen, vptr + 5 * n))
bellard2f619692007-11-16 10:46:05 +00002218 return -TARGET_EFAULT;
2219
pbrook1be9e1d2006-11-19 15:26:04 +00002220 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00002221 }
2222 break;
2223 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00002224 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002225 abi_ulong sockfd, how;
bellard2f619692007-11-16 10:46:05 +00002226
Ulrich Hecht98818182009-07-03 17:09:28 +02002227 if (get_user_ual(sockfd, vptr)
2228 || get_user_ual(how, vptr + n))
bellard2f619692007-11-16 10:46:05 +00002229 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002230
2231 ret = get_errno(shutdown(sockfd, how));
2232 }
bellard31e31b82003-02-18 22:55:36 +00002233 break;
2234 case SOCKOP_sendmsg:
2235 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00002236 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002237 abi_ulong fd;
blueswir1992f48a2007-10-14 16:27:31 +00002238 abi_ulong target_msg;
Ulrich Hecht98818182009-07-03 17:09:28 +02002239 abi_ulong flags;
bellard1a9353d2003-03-16 20:28:50 +00002240
Ulrich Hecht98818182009-07-03 17:09:28 +02002241 if (get_user_ual(fd, vptr)
bellard2f619692007-11-16 10:46:05 +00002242 || get_user_ual(target_msg, vptr + n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002243 || get_user_ual(flags, vptr + 2 * n))
bellard2f619692007-11-16 10:46:05 +00002244 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00002245
ths5fafdf22007-09-16 21:08:06 +00002246 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00002247 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00002248 }
2249 break;
bellard31e31b82003-02-18 22:55:36 +00002250 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00002251 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002252 abi_ulong sockfd;
2253 abi_ulong level;
2254 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002255 abi_ulong optval;
2256 socklen_t optlen;
2257
Ulrich Hecht98818182009-07-03 17:09:28 +02002258 if (get_user_ual(sockfd, vptr)
2259 || get_user_ual(level, vptr + n)
2260 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002261 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002262 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002263 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00002264
2265 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2266 }
2267 break;
bellard31e31b82003-02-18 22:55:36 +00002268 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00002269 {
Ulrich Hecht98818182009-07-03 17:09:28 +02002270 abi_ulong sockfd;
2271 abi_ulong level;
2272 abi_ulong optname;
bellard2f619692007-11-16 10:46:05 +00002273 abi_ulong optval;
2274 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00002275
Ulrich Hecht98818182009-07-03 17:09:28 +02002276 if (get_user_ual(sockfd, vptr)
2277 || get_user_ual(level, vptr + n)
2278 || get_user_ual(optname, vptr + 2 * n)
bellard2f619692007-11-16 10:46:05 +00002279 || get_user_ual(optval, vptr + 3 * n)
Ulrich Hecht98818182009-07-03 17:09:28 +02002280 || get_user_ual(optlen, vptr + 4 * n))
bellard2f619692007-11-16 10:46:05 +00002281 return -TARGET_EFAULT;
2282
2283 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00002284 }
2285 break;
bellard31e31b82003-02-18 22:55:36 +00002286 default:
2287 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00002288 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002289 break;
2290 }
2291 return ret;
2292}
j_mayer32407102007-09-26 23:01:49 +00002293#endif
bellard31e31b82003-02-18 22:55:36 +00002294
bellard8853f862004-02-22 14:57:26 +00002295#define N_SHM_REGIONS 32
2296
2297static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00002298 abi_ulong start;
2299 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00002300} shm_regions[N_SHM_REGIONS];
2301
ths3eb6b042007-06-03 14:26:27 +00002302struct target_ipc_perm
2303{
blueswir1992f48a2007-10-14 16:27:31 +00002304 abi_long __key;
2305 abi_ulong uid;
2306 abi_ulong gid;
2307 abi_ulong cuid;
2308 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00002309 unsigned short int mode;
2310 unsigned short int __pad1;
2311 unsigned short int __seq;
2312 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00002313 abi_ulong __unused1;
2314 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00002315};
2316
2317struct target_semid_ds
2318{
2319 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00002320 abi_ulong sem_otime;
2321 abi_ulong __unused1;
2322 abi_ulong sem_ctime;
2323 abi_ulong __unused2;
2324 abi_ulong sem_nsems;
2325 abi_ulong __unused3;
2326 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00002327};
2328
bellard579a97f2007-11-11 14:26:47 +00002329static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2330 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002331{
2332 struct target_ipc_perm *target_ip;
2333 struct target_semid_ds *target_sd;
2334
bellard579a97f2007-11-11 14:26:47 +00002335 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2336 return -TARGET_EFAULT;
Michael S. Tsirkine8bbe362009-09-30 18:56:44 +00002337 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002338 host_ip->__key = tswapal(target_ip->__key);
2339 host_ip->uid = tswapal(target_ip->uid);
2340 host_ip->gid = tswapal(target_ip->gid);
2341 host_ip->cuid = tswapal(target_ip->cuid);
2342 host_ip->cgid = tswapal(target_ip->cgid);
2343 host_ip->mode = tswap16(target_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002344 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002345 return 0;
ths3eb6b042007-06-03 14:26:27 +00002346}
2347
bellard579a97f2007-11-11 14:26:47 +00002348static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2349 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00002350{
2351 struct target_ipc_perm *target_ip;
2352 struct target_semid_ds *target_sd;
2353
bellard579a97f2007-11-11 14:26:47 +00002354 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2355 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002356 target_ip = &(target_sd->sem_perm);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002357 target_ip->__key = tswapal(host_ip->__key);
2358 target_ip->uid = tswapal(host_ip->uid);
2359 target_ip->gid = tswapal(host_ip->gid);
2360 target_ip->cuid = tswapal(host_ip->cuid);
2361 target_ip->cgid = tswapal(host_ip->cgid);
2362 target_ip->mode = tswap16(host_ip->mode);
ths3eb6b042007-06-03 14:26:27 +00002363 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002364 return 0;
ths3eb6b042007-06-03 14:26:27 +00002365}
2366
bellard579a97f2007-11-11 14:26:47 +00002367static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2368 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002369{
2370 struct target_semid_ds *target_sd;
2371
bellard579a97f2007-11-11 14:26:47 +00002372 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2373 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002374 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2375 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002376 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2377 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2378 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002379 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002380 return 0;
ths3eb6b042007-06-03 14:26:27 +00002381}
2382
bellard579a97f2007-11-11 14:26:47 +00002383static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2384 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002385{
2386 struct target_semid_ds *target_sd;
2387
bellard579a97f2007-11-11 14:26:47 +00002388 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2389 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002390 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
Dong Xu Wang3a931132011-11-29 16:52:38 +08002391 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002392 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2393 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2394 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
ths3eb6b042007-06-03 14:26:27 +00002395 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002396 return 0;
ths3eb6b042007-06-03 14:26:27 +00002397}
2398
aurel32e5289082009-04-18 16:16:12 +00002399struct target_seminfo {
2400 int semmap;
2401 int semmni;
2402 int semmns;
2403 int semmnu;
2404 int semmsl;
2405 int semopm;
2406 int semume;
2407 int semusz;
2408 int semvmx;
2409 int semaem;
2410};
2411
2412static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2413 struct seminfo *host_seminfo)
2414{
2415 struct target_seminfo *target_seminfo;
2416 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2417 return -TARGET_EFAULT;
2418 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2419 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2420 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2421 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2422 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2423 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2424 __put_user(host_seminfo->semume, &target_seminfo->semume);
2425 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2426 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2427 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2428 unlock_user_struct(target_seminfo, target_addr, 1);
2429 return 0;
2430}
2431
thsfa294812007-02-02 22:05:00 +00002432union semun {
2433 int val;
ths3eb6b042007-06-03 14:26:27 +00002434 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002435 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002436 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002437};
2438
ths3eb6b042007-06-03 14:26:27 +00002439union target_semun {
2440 int val;
aurel32e5289082009-04-18 16:16:12 +00002441 abi_ulong buf;
2442 abi_ulong array;
2443 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002444};
2445
aurel32e5289082009-04-18 16:16:12 +00002446static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2447 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002448{
aurel32e5289082009-04-18 16:16:12 +00002449 int nsems;
2450 unsigned short *array;
2451 union semun semun;
2452 struct semid_ds semid_ds;
2453 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002454
aurel32e5289082009-04-18 16:16:12 +00002455 semun.buf = &semid_ds;
2456
2457 ret = semctl(semid, 0, IPC_STAT, semun);
2458 if (ret == -1)
2459 return get_errno(ret);
2460
2461 nsems = semid_ds.sem_nsems;
2462
2463 *host_array = malloc(nsems*sizeof(unsigned short));
2464 array = lock_user(VERIFY_READ, target_addr,
2465 nsems*sizeof(unsigned short), 1);
2466 if (!array)
2467 return -TARGET_EFAULT;
2468
2469 for(i=0; i<nsems; i++) {
2470 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002471 }
aurel32e5289082009-04-18 16:16:12 +00002472 unlock_user(array, target_addr, 0);
2473
bellard579a97f2007-11-11 14:26:47 +00002474 return 0;
ths3eb6b042007-06-03 14:26:27 +00002475}
2476
aurel32e5289082009-04-18 16:16:12 +00002477static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2478 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002479{
aurel32e5289082009-04-18 16:16:12 +00002480 int nsems;
2481 unsigned short *array;
2482 union semun semun;
2483 struct semid_ds semid_ds;
2484 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002485
aurel32e5289082009-04-18 16:16:12 +00002486 semun.buf = &semid_ds;
2487
2488 ret = semctl(semid, 0, IPC_STAT, semun);
2489 if (ret == -1)
2490 return get_errno(ret);
2491
2492 nsems = semid_ds.sem_nsems;
2493
2494 array = lock_user(VERIFY_WRITE, target_addr,
2495 nsems*sizeof(unsigned short), 0);
2496 if (!array)
2497 return -TARGET_EFAULT;
2498
2499 for(i=0; i<nsems; i++) {
2500 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002501 }
aurel32e5289082009-04-18 16:16:12 +00002502 free(*host_array);
2503 unlock_user(array, target_addr, 1);
2504
bellard579a97f2007-11-11 14:26:47 +00002505 return 0;
ths3eb6b042007-06-03 14:26:27 +00002506}
2507
aurel32e5289082009-04-18 16:16:12 +00002508static inline abi_long do_semctl(int semid, int semnum, int cmd,
2509 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002510{
2511 union semun arg;
2512 struct semid_ds dsarg;
vibi sreenivasan7b8118e2009-06-19 13:34:39 +05302513 unsigned short *array = NULL;
aurel32e5289082009-04-18 16:16:12 +00002514 struct seminfo seminfo;
2515 abi_long ret = -TARGET_EINVAL;
2516 abi_long err;
2517 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002518
2519 switch( cmd ) {
2520 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002521 case SETVAL:
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002522 arg.val = tswap32(target_su.val);
aurel32e5289082009-04-18 16:16:12 +00002523 ret = get_errno(semctl(semid, semnum, cmd, arg));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002524 target_su.val = tswap32(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002525 break;
2526 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002527 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002528 err = target_to_host_semarray(semid, &array, target_su.array);
2529 if (err)
2530 return err;
2531 arg.array = array;
2532 ret = get_errno(semctl(semid, semnum, cmd, arg));
2533 err = host_to_target_semarray(semid, target_su.array, &array);
2534 if (err)
2535 return err;
ths3eb6b042007-06-03 14:26:27 +00002536 break;
2537 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002538 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002539 case SEM_STAT:
2540 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2541 if (err)
2542 return err;
2543 arg.buf = &dsarg;
2544 ret = get_errno(semctl(semid, semnum, cmd, arg));
2545 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2546 if (err)
2547 return err;
ths3eb6b042007-06-03 14:26:27 +00002548 break;
aurel32e5289082009-04-18 16:16:12 +00002549 case IPC_INFO:
2550 case SEM_INFO:
2551 arg.__buf = &seminfo;
2552 ret = get_errno(semctl(semid, semnum, cmd, arg));
2553 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2554 if (err)
2555 return err;
2556 break;
2557 case IPC_RMID:
2558 case GETPID:
2559 case GETNCNT:
2560 case GETZCNT:
2561 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2562 break;
ths3eb6b042007-06-03 14:26:27 +00002563 }
2564
2565 return ret;
2566}
2567
aurel32e5289082009-04-18 16:16:12 +00002568struct target_sembuf {
2569 unsigned short sem_num;
2570 short sem_op;
2571 short sem_flg;
2572};
2573
2574static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2575 abi_ulong target_addr,
2576 unsigned nsops)
2577{
2578 struct target_sembuf *target_sembuf;
2579 int i;
2580
2581 target_sembuf = lock_user(VERIFY_READ, target_addr,
2582 nsops*sizeof(struct target_sembuf), 1);
2583 if (!target_sembuf)
2584 return -TARGET_EFAULT;
2585
2586 for(i=0; i<nsops; i++) {
2587 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2588 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2589 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2590 }
2591
2592 unlock_user(target_sembuf, target_addr, 0);
2593
2594 return 0;
2595}
2596
2597static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2598{
2599 struct sembuf sops[nsops];
2600
2601 if (target_to_host_sembuf(sops, ptr, nsops))
2602 return -TARGET_EFAULT;
2603
Petar Jovanovicc7128c92013-03-21 07:57:36 +00002604 return get_errno(semop(semid, sops, nsops));
aurel32e5289082009-04-18 16:16:12 +00002605}
2606
ths1bc012f2007-06-03 14:27:49 +00002607struct target_msqid_ds
2608{
aurel321c54ff92008-10-13 21:08:44 +00002609 struct target_ipc_perm msg_perm;
2610 abi_ulong msg_stime;
2611#if TARGET_ABI_BITS == 32
2612 abi_ulong __unused1;
2613#endif
2614 abi_ulong msg_rtime;
2615#if TARGET_ABI_BITS == 32
2616 abi_ulong __unused2;
2617#endif
2618 abi_ulong msg_ctime;
2619#if TARGET_ABI_BITS == 32
2620 abi_ulong __unused3;
2621#endif
2622 abi_ulong __msg_cbytes;
2623 abi_ulong msg_qnum;
2624 abi_ulong msg_qbytes;
2625 abi_ulong msg_lspid;
2626 abi_ulong msg_lrpid;
2627 abi_ulong __unused4;
2628 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002629};
2630
bellard579a97f2007-11-11 14:26:47 +00002631static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2632 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002633{
2634 struct target_msqid_ds *target_md;
2635
bellard579a97f2007-11-11 14:26:47 +00002636 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2637 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002638 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2639 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002640 host_md->msg_stime = tswapal(target_md->msg_stime);
2641 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2642 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2643 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2644 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2645 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2646 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2647 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002648 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002649 return 0;
ths1bc012f2007-06-03 14:27:49 +00002650}
2651
bellard579a97f2007-11-11 14:26:47 +00002652static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2653 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002654{
2655 struct target_msqid_ds *target_md;
2656
bellard579a97f2007-11-11 14:26:47 +00002657 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2658 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002659 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2660 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002661 target_md->msg_stime = tswapal(host_md->msg_stime);
2662 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2663 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2664 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2665 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2666 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2667 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2668 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
ths1bc012f2007-06-03 14:27:49 +00002669 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002670 return 0;
ths1bc012f2007-06-03 14:27:49 +00002671}
2672
aurel321c54ff92008-10-13 21:08:44 +00002673struct target_msginfo {
2674 int msgpool;
2675 int msgmap;
2676 int msgmax;
2677 int msgmnb;
2678 int msgmni;
2679 int msgssz;
2680 int msgtql;
2681 unsigned short int msgseg;
2682};
2683
2684static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2685 struct msginfo *host_msginfo)
2686{
2687 struct target_msginfo *target_msginfo;
2688 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2689 return -TARGET_EFAULT;
2690 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2691 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2692 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2693 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2694 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2695 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2696 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2697 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2698 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002699 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002700}
2701
2702static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002703{
2704 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002705 struct msginfo msginfo;
2706 abi_long ret = -TARGET_EINVAL;
2707
2708 cmd &= 0xff;
2709
2710 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002711 case IPC_STAT:
2712 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002713 case MSG_STAT:
2714 if (target_to_host_msqid_ds(&dsarg,ptr))
2715 return -TARGET_EFAULT;
2716 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2717 if (host_to_target_msqid_ds(ptr,&dsarg))
2718 return -TARGET_EFAULT;
2719 break;
2720 case IPC_RMID:
2721 ret = get_errno(msgctl(msgid, cmd, NULL));
2722 break;
2723 case IPC_INFO:
2724 case MSG_INFO:
2725 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2726 if (host_to_target_msginfo(ptr, &msginfo))
2727 return -TARGET_EFAULT;
2728 break;
ths1bc012f2007-06-03 14:27:49 +00002729 }
aurel321c54ff92008-10-13 21:08:44 +00002730
ths1bc012f2007-06-03 14:27:49 +00002731 return ret;
2732}
2733
2734struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002735 abi_long mtype;
2736 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002737};
2738
blueswir1992f48a2007-10-14 16:27:31 +00002739static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2740 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002741{
2742 struct target_msgbuf *target_mb;
2743 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002744 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002745
bellard579a97f2007-11-11 14:26:47 +00002746 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2747 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002748 host_mb = malloc(msgsz+sizeof(long));
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002749 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
aurel321c54ff92008-10-13 21:08:44 +00002750 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002751 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2752 free(host_mb);
2753 unlock_user_struct(target_mb, msgp, 0);
2754
2755 return ret;
2756}
2757
blueswir1992f48a2007-10-14 16:27:31 +00002758static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002759 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002760 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002761{
2762 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002763 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002764 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002765 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002766
bellard579a97f2007-11-11 14:26:47 +00002767 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2768 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002769
Jim Meyering0d07fe42012-08-22 13:55:53 +02002770 host_mb = g_malloc(msgsz+sizeof(long));
Laurent Vivier79dd77d2012-12-20 11:00:11 +00002771 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
aurel321c54ff92008-10-13 21:08:44 +00002772
bellard579a97f2007-11-11 14:26:47 +00002773 if (ret > 0) {
2774 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2775 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2776 if (!target_mtext) {
2777 ret = -TARGET_EFAULT;
2778 goto end;
2779 }
aurel321c54ff92008-10-13 21:08:44 +00002780 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002781 unlock_user(target_mtext, target_mtext_addr, ret);
2782 }
aurel321c54ff92008-10-13 21:08:44 +00002783
Matthias Brauncbb21ee2011-08-12 19:57:41 +02002784 target_mb->mtype = tswapal(host_mb->mtype);
ths1bc012f2007-06-03 14:27:49 +00002785
bellard579a97f2007-11-11 14:26:47 +00002786end:
2787 if (target_mb)
2788 unlock_user_struct(target_mb, msgp, 1);
Jim Meyering0d07fe42012-08-22 13:55:53 +02002789 g_free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002790 return ret;
2791}
2792
Riku Voipio88a8c982009-04-03 10:42:00 +03002793struct target_shmid_ds
2794{
2795 struct target_ipc_perm shm_perm;
2796 abi_ulong shm_segsz;
2797 abi_ulong shm_atime;
2798#if TARGET_ABI_BITS == 32
2799 abi_ulong __unused1;
2800#endif
2801 abi_ulong shm_dtime;
2802#if TARGET_ABI_BITS == 32
2803 abi_ulong __unused2;
2804#endif
2805 abi_ulong shm_ctime;
2806#if TARGET_ABI_BITS == 32
2807 abi_ulong __unused3;
2808#endif
2809 int shm_cpid;
2810 int shm_lpid;
2811 abi_ulong shm_nattch;
2812 unsigned long int __unused4;
2813 unsigned long int __unused5;
2814};
2815
2816static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2817 abi_ulong target_addr)
2818{
2819 struct target_shmid_ds *target_sd;
2820
2821 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2822 return -TARGET_EFAULT;
2823 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2824 return -TARGET_EFAULT;
2825 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2826 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2827 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2828 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2829 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2830 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2831 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2832 unlock_user_struct(target_sd, target_addr, 0);
2833 return 0;
2834}
2835
2836static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2837 struct shmid_ds *host_sd)
2838{
2839 struct target_shmid_ds *target_sd;
2840
2841 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2842 return -TARGET_EFAULT;
2843 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2844 return -TARGET_EFAULT;
2845 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2846 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2847 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2848 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2849 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2850 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2851 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2852 unlock_user_struct(target_sd, target_addr, 1);
2853 return 0;
2854}
2855
2856struct target_shminfo {
2857 abi_ulong shmmax;
2858 abi_ulong shmmin;
2859 abi_ulong shmmni;
2860 abi_ulong shmseg;
2861 abi_ulong shmall;
2862};
2863
2864static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2865 struct shminfo *host_shminfo)
2866{
2867 struct target_shminfo *target_shminfo;
2868 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2869 return -TARGET_EFAULT;
2870 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2871 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2872 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2873 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2874 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2875 unlock_user_struct(target_shminfo, target_addr, 1);
2876 return 0;
2877}
2878
2879struct target_shm_info {
2880 int used_ids;
2881 abi_ulong shm_tot;
2882 abi_ulong shm_rss;
2883 abi_ulong shm_swp;
2884 abi_ulong swap_attempts;
2885 abi_ulong swap_successes;
2886};
2887
2888static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2889 struct shm_info *host_shm_info)
2890{
2891 struct target_shm_info *target_shm_info;
2892 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2893 return -TARGET_EFAULT;
2894 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2895 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2896 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2897 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2898 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2899 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2900 unlock_user_struct(target_shm_info, target_addr, 1);
2901 return 0;
2902}
2903
2904static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2905{
2906 struct shmid_ds dsarg;
2907 struct shminfo shminfo;
2908 struct shm_info shm_info;
2909 abi_long ret = -TARGET_EINVAL;
2910
2911 cmd &= 0xff;
2912
2913 switch(cmd) {
2914 case IPC_STAT:
2915 case IPC_SET:
2916 case SHM_STAT:
2917 if (target_to_host_shmid_ds(&dsarg, buf))
2918 return -TARGET_EFAULT;
2919 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2920 if (host_to_target_shmid_ds(buf, &dsarg))
2921 return -TARGET_EFAULT;
2922 break;
2923 case IPC_INFO:
2924 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2925 if (host_to_target_shminfo(buf, &shminfo))
2926 return -TARGET_EFAULT;
2927 break;
2928 case SHM_INFO:
2929 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2930 if (host_to_target_shm_info(buf, &shm_info))
2931 return -TARGET_EFAULT;
2932 break;
2933 case IPC_RMID:
2934 case SHM_LOCK:
2935 case SHM_UNLOCK:
2936 ret = get_errno(shmctl(shmid, cmd, NULL));
2937 break;
2938 }
2939
2940 return ret;
2941}
2942
2943static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2944{
2945 abi_long raddr;
2946 void *host_raddr;
2947 struct shmid_ds shm_info;
2948 int i,ret;
2949
2950 /* find out the length of the shared memory segment */
2951 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2952 if (is_error(ret)) {
2953 /* can't get length, bail out */
2954 return ret;
2955 }
2956
2957 mmap_lock();
2958
2959 if (shmaddr)
2960 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2961 else {
2962 abi_ulong mmap_start;
2963
2964 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2965
2966 if (mmap_start == -1) {
2967 errno = ENOMEM;
2968 host_raddr = (void *)-1;
2969 } else
2970 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2971 }
2972
2973 if (host_raddr == (void *)-1) {
2974 mmap_unlock();
2975 return get_errno((long)host_raddr);
2976 }
2977 raddr=h2g((unsigned long)host_raddr);
2978
2979 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2980 PAGE_VALID | PAGE_READ |
2981 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2982
2983 for (i = 0; i < N_SHM_REGIONS; i++) {
2984 if (shm_regions[i].start == 0) {
2985 shm_regions[i].start = raddr;
2986 shm_regions[i].size = shm_info.shm_segsz;
2987 break;
2988 }
2989 }
2990
2991 mmap_unlock();
2992 return raddr;
2993
2994}
2995
2996static inline abi_long do_shmdt(abi_ulong shmaddr)
2997{
2998 int i;
2999
3000 for (i = 0; i < N_SHM_REGIONS; ++i) {
3001 if (shm_regions[i].start == shmaddr) {
3002 shm_regions[i].start = 0;
takasi-y@ops.dti.ne.jpe00ac242010-04-11 02:09:57 +09003003 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
Riku Voipio88a8c982009-04-03 10:42:00 +03003004 break;
3005 }
3006 }
3007
3008 return get_errno(shmdt(g2h(shmaddr)));
3009}
3010
aurel321c54ff92008-10-13 21:08:44 +00003011#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00003012/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00003013/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003014static abi_long do_ipc(unsigned int call, int first,
3015 int second, int third,
3016 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00003017{
3018 int version;
blueswir1992f48a2007-10-14 16:27:31 +00003019 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00003020
3021 version = call >> 16;
3022 call &= 0xffff;
3023
3024 switch (call) {
thsfa294812007-02-02 22:05:00 +00003025 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00003026 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00003027 break;
3028
3029 case IPCOP_semget:
3030 ret = get_errno(semget(first, second, third));
3031 break;
3032
3033 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00003034 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00003035 break;
thsd96372e2007-02-02 22:05:44 +00003036
aurel321c54ff92008-10-13 21:08:44 +00003037 case IPCOP_msgget:
3038 ret = get_errno(msgget(first, second));
3039 break;
thsd96372e2007-02-02 22:05:44 +00003040
aurel321c54ff92008-10-13 21:08:44 +00003041 case IPCOP_msgsnd:
3042 ret = do_msgsnd(first, ptr, second, third);
3043 break;
thsd96372e2007-02-02 22:05:44 +00003044
aurel321c54ff92008-10-13 21:08:44 +00003045 case IPCOP_msgctl:
3046 ret = do_msgctl(first, second, ptr);
3047 break;
thsd96372e2007-02-02 22:05:44 +00003048
aurel321c54ff92008-10-13 21:08:44 +00003049 case IPCOP_msgrcv:
3050 switch (version) {
3051 case 0:
3052 {
3053 struct target_ipc_kludge {
3054 abi_long msgp;
3055 abi_long msgtyp;
3056 } *tmp;
thsd96372e2007-02-02 22:05:44 +00003057
aurel321c54ff92008-10-13 21:08:44 +00003058 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3059 ret = -TARGET_EFAULT;
3060 break;
ths1bc012f2007-06-03 14:27:49 +00003061 }
aurel321c54ff92008-10-13 21:08:44 +00003062
Laurent Vivier79dd77d2012-12-20 11:00:11 +00003063 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
aurel321c54ff92008-10-13 21:08:44 +00003064
3065 unlock_user_struct(tmp, ptr, 0);
3066 break;
3067 }
3068 default:
3069 ret = do_msgrcv(first, ptr, second, fifth, third);
3070 }
3071 break;
thsd96372e2007-02-02 22:05:44 +00003072
bellard8853f862004-02-22 14:57:26 +00003073 case IPCOP_shmat:
Riku Voipio88a8c982009-04-03 10:42:00 +03003074 switch (version) {
3075 default:
bellard5a4a8982007-11-11 17:39:18 +00003076 {
3077 abi_ulong raddr;
Riku Voipio88a8c982009-04-03 10:42:00 +03003078 raddr = do_shmat(first, ptr, second);
3079 if (is_error(raddr))
3080 return get_errno(raddr);
bellard2f619692007-11-16 10:46:05 +00003081 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00003082 return -TARGET_EFAULT;
Riku Voipio88a8c982009-04-03 10:42:00 +03003083 break;
3084 }
3085 case 1:
3086 ret = -TARGET_EINVAL;
3087 break;
bellard5a4a8982007-11-11 17:39:18 +00003088 }
bellard8853f862004-02-22 14:57:26 +00003089 break;
3090 case IPCOP_shmdt:
Riku Voipio88a8c982009-04-03 10:42:00 +03003091 ret = do_shmdt(ptr);
bellard8853f862004-02-22 14:57:26 +00003092 break;
3093
3094 case IPCOP_shmget:
3095 /* IPC_* flag values are the same on all linux platforms */
3096 ret = get_errno(shmget(first, second, third));
3097 break;
3098
3099 /* IPC_* and SHM_* command values are the same on all linux platforms */
3100 case IPCOP_shmctl:
Riku Voipio88a8c982009-04-03 10:42:00 +03003101 ret = do_shmctl(first, second, third);
bellard8853f862004-02-22 14:57:26 +00003102 break;
3103 default:
j_mayer32407102007-09-26 23:01:49 +00003104 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00003105 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00003106 break;
3107 }
3108 return ret;
3109}
j_mayer32407102007-09-26 23:01:49 +00003110#endif
bellard8853f862004-02-22 14:57:26 +00003111
bellard31e31b82003-02-18 22:55:36 +00003112/* kernel structure types definitions */
bellard31e31b82003-02-18 22:55:36 +00003113
Blue Swirl001faf32009-05-13 17:53:17 +00003114#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00003115#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3116enum {
3117#include "syscall_types.h"
3118};
3119#undef STRUCT
3120#undef STRUCT_SPECIAL
3121
Blue Swirl001faf32009-05-13 17:53:17 +00003122#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00003123#define STRUCT_SPECIAL(name)
3124#include "syscall_types.h"
3125#undef STRUCT
3126#undef STRUCT_SPECIAL
3127
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003128typedef struct IOCTLEntry IOCTLEntry;
3129
3130typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3131 int fd, abi_long cmd, abi_long arg);
3132
3133struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00003134 unsigned int target_cmd;
3135 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00003136 const char *name;
3137 int access;
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003138 do_ioctl_fn *do_ioctl;
bellard1a9353d2003-03-16 20:28:50 +00003139 const argtype arg_type[5];
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003140};
bellard31e31b82003-02-18 22:55:36 +00003141
3142#define IOC_R 0x0001
3143#define IOC_W 0x0002
3144#define IOC_RW (IOC_R | IOC_W)
3145
3146#define MAX_STRUCT_SIZE 4096
3147
Peter Maydelldace20d2011-01-10 13:11:24 +00003148#ifdef CONFIG_FIEMAP
Peter Maydell285da2b2011-01-06 15:04:18 +00003149/* So fiemap access checks don't overflow on 32 bit systems.
3150 * This is very slightly smaller than the limit imposed by
3151 * the underlying kernel.
3152 */
3153#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3154 / sizeof(struct fiemap_extent))
3155
3156static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3157 int fd, abi_long cmd, abi_long arg)
3158{
3159 /* The parameter for this ioctl is a struct fiemap followed
3160 * by an array of struct fiemap_extent whose size is set
3161 * in fiemap->fm_extent_count. The array is filled in by the
3162 * ioctl.
3163 */
3164 int target_size_in, target_size_out;
3165 struct fiemap *fm;
3166 const argtype *arg_type = ie->arg_type;
3167 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3168 void *argptr, *p;
3169 abi_long ret;
3170 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3171 uint32_t outbufsz;
3172 int free_fm = 0;
3173
3174 assert(arg_type[0] == TYPE_PTR);
3175 assert(ie->access == IOC_RW);
3176 arg_type++;
3177 target_size_in = thunk_type_size(arg_type, 0);
3178 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3179 if (!argptr) {
3180 return -TARGET_EFAULT;
3181 }
3182 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3183 unlock_user(argptr, arg, 0);
3184 fm = (struct fiemap *)buf_temp;
3185 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3186 return -TARGET_EINVAL;
3187 }
3188
3189 outbufsz = sizeof (*fm) +
3190 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3191
3192 if (outbufsz > MAX_STRUCT_SIZE) {
3193 /* We can't fit all the extents into the fixed size buffer.
3194 * Allocate one that is large enough and use it instead.
3195 */
3196 fm = malloc(outbufsz);
3197 if (!fm) {
3198 return -TARGET_ENOMEM;
3199 }
3200 memcpy(fm, buf_temp, sizeof(struct fiemap));
3201 free_fm = 1;
3202 }
3203 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3204 if (!is_error(ret)) {
3205 target_size_out = target_size_in;
3206 /* An extent_count of 0 means we were only counting the extents
3207 * so there are no structs to copy
3208 */
3209 if (fm->fm_extent_count != 0) {
3210 target_size_out += fm->fm_mapped_extents * extent_size;
3211 }
3212 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3213 if (!argptr) {
3214 ret = -TARGET_EFAULT;
3215 } else {
3216 /* Convert the struct fiemap */
3217 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3218 if (fm->fm_extent_count != 0) {
3219 p = argptr + target_size_in;
3220 /* ...and then all the struct fiemap_extents */
3221 for (i = 0; i < fm->fm_mapped_extents; i++) {
3222 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3223 THUNK_TARGET);
3224 p += extent_size;
3225 }
3226 }
3227 unlock_user(argptr, arg, target_size_out);
3228 }
3229 }
3230 if (free_fm) {
3231 free(fm);
3232 }
3233 return ret;
3234}
Peter Maydelldace20d2011-01-10 13:11:24 +00003235#endif
Peter Maydell285da2b2011-01-06 15:04:18 +00003236
Laurent Vivier059c2f22011-03-30 00:12:12 +02003237static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3238 int fd, abi_long cmd, abi_long arg)
3239{
3240 const argtype *arg_type = ie->arg_type;
3241 int target_size;
3242 void *argptr;
3243 int ret;
3244 struct ifconf *host_ifconf;
3245 uint32_t outbufsz;
3246 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3247 int target_ifreq_size;
3248 int nb_ifreq;
3249 int free_buf = 0;
3250 int i;
3251 int target_ifc_len;
3252 abi_long target_ifc_buf;
3253 int host_ifc_len;
3254 char *host_ifc_buf;
3255
3256 assert(arg_type[0] == TYPE_PTR);
3257 assert(ie->access == IOC_RW);
3258
3259 arg_type++;
3260 target_size = thunk_type_size(arg_type, 0);
3261
3262 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3263 if (!argptr)
3264 return -TARGET_EFAULT;
3265 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3266 unlock_user(argptr, arg, 0);
3267
3268 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3269 target_ifc_len = host_ifconf->ifc_len;
3270 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3271
3272 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3273 nb_ifreq = target_ifc_len / target_ifreq_size;
3274 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3275
3276 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3277 if (outbufsz > MAX_STRUCT_SIZE) {
3278 /* We can't fit all the extents into the fixed size buffer.
3279 * Allocate one that is large enough and use it instead.
3280 */
3281 host_ifconf = malloc(outbufsz);
3282 if (!host_ifconf) {
3283 return -TARGET_ENOMEM;
3284 }
3285 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3286 free_buf = 1;
3287 }
3288 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3289
3290 host_ifconf->ifc_len = host_ifc_len;
3291 host_ifconf->ifc_buf = host_ifc_buf;
3292
3293 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3294 if (!is_error(ret)) {
3295 /* convert host ifc_len to target ifc_len */
3296
3297 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3298 target_ifc_len = nb_ifreq * target_ifreq_size;
3299 host_ifconf->ifc_len = target_ifc_len;
3300
3301 /* restore target ifc_buf */
3302
3303 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3304
3305 /* copy struct ifconf to target user */
3306
3307 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3308 if (!argptr)
3309 return -TARGET_EFAULT;
3310 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3311 unlock_user(argptr, arg, target_size);
3312
3313 /* copy ifreq[] to target user */
3314
3315 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3316 for (i = 0; i < nb_ifreq ; i++) {
3317 thunk_convert(argptr + i * target_ifreq_size,
3318 host_ifc_buf + i * sizeof(struct ifreq),
3319 ifreq_arg_type, THUNK_TARGET);
3320 }
3321 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3322 }
3323
3324 if (free_buf) {
3325 free(host_ifconf);
3326 }
3327
3328 return ret;
3329}
3330
Alexander Graf56e904e2012-01-31 18:42:06 +01003331static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3332 abi_long cmd, abi_long arg)
3333{
3334 void *argptr;
3335 struct dm_ioctl *host_dm;
3336 abi_long guest_data;
3337 uint32_t guest_data_size;
3338 int target_size;
3339 const argtype *arg_type = ie->arg_type;
3340 abi_long ret;
3341 void *big_buf = NULL;
3342 char *host_data;
3343
3344 arg_type++;
3345 target_size = thunk_type_size(arg_type, 0);
3346 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3347 if (!argptr) {
3348 ret = -TARGET_EFAULT;
3349 goto out;
3350 }
3351 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3352 unlock_user(argptr, arg, 0);
3353
3354 /* buf_temp is too small, so fetch things into a bigger buffer */
3355 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3356 memcpy(big_buf, buf_temp, target_size);
3357 buf_temp = big_buf;
3358 host_dm = big_buf;
3359
3360 guest_data = arg + host_dm->data_start;
3361 if ((guest_data - arg) < 0) {
3362 ret = -EINVAL;
3363 goto out;
3364 }
3365 guest_data_size = host_dm->data_size - host_dm->data_start;
3366 host_data = (char*)host_dm + host_dm->data_start;
3367
3368 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3369 switch (ie->host_cmd) {
3370 case DM_REMOVE_ALL:
3371 case DM_LIST_DEVICES:
3372 case DM_DEV_CREATE:
3373 case DM_DEV_REMOVE:
3374 case DM_DEV_SUSPEND:
3375 case DM_DEV_STATUS:
3376 case DM_DEV_WAIT:
3377 case DM_TABLE_STATUS:
3378 case DM_TABLE_CLEAR:
3379 case DM_TABLE_DEPS:
3380 case DM_LIST_VERSIONS:
3381 /* no input data */
3382 break;
3383 case DM_DEV_RENAME:
3384 case DM_DEV_SET_GEOMETRY:
3385 /* data contains only strings */
3386 memcpy(host_data, argptr, guest_data_size);
3387 break;
3388 case DM_TARGET_MSG:
3389 memcpy(host_data, argptr, guest_data_size);
3390 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3391 break;
3392 case DM_TABLE_LOAD:
3393 {
3394 void *gspec = argptr;
3395 void *cur_data = host_data;
3396 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3397 int spec_size = thunk_type_size(arg_type, 0);
3398 int i;
3399
3400 for (i = 0; i < host_dm->target_count; i++) {
3401 struct dm_target_spec *spec = cur_data;
3402 uint32_t next;
3403 int slen;
3404
3405 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3406 slen = strlen((char*)gspec + spec_size) + 1;
3407 next = spec->next;
3408 spec->next = sizeof(*spec) + slen;
3409 strcpy((char*)&spec[1], gspec + spec_size);
3410 gspec += next;
3411 cur_data += spec->next;
3412 }
3413 break;
3414 }
3415 default:
3416 ret = -TARGET_EINVAL;
3417 goto out;
3418 }
3419 unlock_user(argptr, guest_data, 0);
3420
3421 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3422 if (!is_error(ret)) {
3423 guest_data = arg + host_dm->data_start;
3424 guest_data_size = host_dm->data_size - host_dm->data_start;
3425 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3426 switch (ie->host_cmd) {
3427 case DM_REMOVE_ALL:
3428 case DM_DEV_CREATE:
3429 case DM_DEV_REMOVE:
3430 case DM_DEV_RENAME:
3431 case DM_DEV_SUSPEND:
3432 case DM_DEV_STATUS:
3433 case DM_TABLE_LOAD:
3434 case DM_TABLE_CLEAR:
3435 case DM_TARGET_MSG:
3436 case DM_DEV_SET_GEOMETRY:
3437 /* no return data */
3438 break;
3439 case DM_LIST_DEVICES:
3440 {
3441 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3442 uint32_t remaining_data = guest_data_size;
3443 void *cur_data = argptr;
3444 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3445 int nl_size = 12; /* can't use thunk_size due to alignment */
3446
3447 while (1) {
3448 uint32_t next = nl->next;
3449 if (next) {
3450 nl->next = nl_size + (strlen(nl->name) + 1);
3451 }
3452 if (remaining_data < nl->next) {
3453 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3454 break;
3455 }
3456 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3457 strcpy(cur_data + nl_size, nl->name);
3458 cur_data += nl->next;
3459 remaining_data -= nl->next;
3460 if (!next) {
3461 break;
3462 }
3463 nl = (void*)nl + next;
3464 }
3465 break;
3466 }
3467 case DM_DEV_WAIT:
3468 case DM_TABLE_STATUS:
3469 {
3470 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3471 void *cur_data = argptr;
3472 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3473 int spec_size = thunk_type_size(arg_type, 0);
3474 int i;
3475
3476 for (i = 0; i < host_dm->target_count; i++) {
3477 uint32_t next = spec->next;
3478 int slen = strlen((char*)&spec[1]) + 1;
3479 spec->next = (cur_data - argptr) + spec_size + slen;
3480 if (guest_data_size < spec->next) {
3481 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3482 break;
3483 }
3484 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3485 strcpy(cur_data + spec_size, (char*)&spec[1]);
3486 cur_data = argptr + spec->next;
3487 spec = (void*)host_dm + host_dm->data_start + next;
3488 }
3489 break;
3490 }
3491 case DM_TABLE_DEPS:
3492 {
3493 void *hdata = (void*)host_dm + host_dm->data_start;
3494 int count = *(uint32_t*)hdata;
3495 uint64_t *hdev = hdata + 8;
3496 uint64_t *gdev = argptr + 8;
3497 int i;
3498
3499 *(uint32_t*)argptr = tswap32(count);
3500 for (i = 0; i < count; i++) {
3501 *gdev = tswap64(*hdev);
3502 gdev++;
3503 hdev++;
3504 }
3505 break;
3506 }
3507 case DM_LIST_VERSIONS:
3508 {
3509 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3510 uint32_t remaining_data = guest_data_size;
3511 void *cur_data = argptr;
3512 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3513 int vers_size = thunk_type_size(arg_type, 0);
3514
3515 while (1) {
3516 uint32_t next = vers->next;
3517 if (next) {
3518 vers->next = vers_size + (strlen(vers->name) + 1);
3519 }
3520 if (remaining_data < vers->next) {
3521 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3522 break;
3523 }
3524 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3525 strcpy(cur_data + vers_size, vers->name);
3526 cur_data += vers->next;
3527 remaining_data -= vers->next;
3528 if (!next) {
3529 break;
3530 }
3531 vers = (void*)vers + next;
3532 }
3533 break;
3534 }
3535 default:
3536 ret = -TARGET_EINVAL;
3537 goto out;
3538 }
3539 unlock_user(argptr, guest_data, guest_data_size);
3540
3541 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3542 if (!argptr) {
3543 ret = -TARGET_EFAULT;
3544 goto out;
3545 }
3546 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3547 unlock_user(argptr, arg, target_size);
3548 }
3549out:
Stefan Weilad11ad72012-09-04 22:14:19 +02003550 g_free(big_buf);
Alexander Graf56e904e2012-01-31 18:42:06 +01003551 return ret;
3552}
3553
blueswir19f106a72008-10-05 10:52:52 +00003554static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00003555#define IOCTL(cmd, access, ...) \
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003556 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3557#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3558 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00003559#include "ioctls.h"
3560 { 0, 0, },
3561};
3562
pbrook53a59602006-03-25 19:31:22 +00003563/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00003564/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00003565static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00003566{
3567 const IOCTLEntry *ie;
3568 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00003569 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003570 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00003571 int target_size;
3572 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00003573
3574 ie = ioctl_entries;
3575 for(;;) {
3576 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00003577 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00003578 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003579 }
3580 if (ie->target_cmd == cmd)
3581 break;
3582 ie++;
3583 }
3584 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00003585#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00003586 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00003587#endif
Peter Maydelld2ef05b2011-01-06 15:04:17 +00003588 if (ie->do_ioctl) {
3589 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3590 }
3591
bellard31e31b82003-02-18 22:55:36 +00003592 switch(arg_type[0]) {
3593 case TYPE_NULL:
3594 /* no argument */
3595 ret = get_errno(ioctl(fd, ie->host_cmd));
3596 break;
3597 case TYPE_PTRVOID:
3598 case TYPE_INT:
3599 /* int argment */
3600 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3601 break;
3602 case TYPE_PTR:
3603 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00003604 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00003605 switch(ie->access) {
3606 case IOC_R:
3607 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3608 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003609 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3610 if (!argptr)
3611 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003612 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3613 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003614 }
3615 break;
3616 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00003617 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3618 if (!argptr)
3619 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003620 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3621 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003622 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3623 break;
3624 default:
3625 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00003626 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3627 if (!argptr)
3628 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003629 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3630 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00003631 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3632 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003633 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3634 if (!argptr)
3635 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003636 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3637 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00003638 }
3639 break;
3640 }
3641 break;
3642 default:
j_mayer32407102007-09-26 23:01:49 +00003643 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3644 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00003645 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00003646 break;
3647 }
3648 return ret;
3649}
3650
blueswir1b39bc502008-10-05 10:51:10 +00003651static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003652 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3653 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3654 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3655 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3656 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3657 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3658 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3659 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3660 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3661 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3662 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3663 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3664 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3665 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3666 { 0, 0, 0, 0 }
3667};
3668
blueswir1b39bc502008-10-05 10:51:10 +00003669static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003670 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3671 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3672 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3673 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3674 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3675 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3676 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3677 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3678 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3679 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3680 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3681 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3682 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3683 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3684 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3685 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3686 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3687 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3688 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3689 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3690 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3691 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3692 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3693 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3694 { 0, 0, 0, 0 }
3695};
3696
blueswir1b39bc502008-10-05 10:51:10 +00003697static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003698 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3699 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3700 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3701 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3702 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3703 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3704 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3705 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3706 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3707 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3708 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3709 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3710 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3711 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3712 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3713 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3714 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3715 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3716 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3717 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3718 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3719 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3720 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3721 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3722 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3723 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3724 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3725 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3726 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3727 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3728 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3729 { 0, 0, 0, 0 }
3730};
3731
blueswir1b39bc502008-10-05 10:51:10 +00003732static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00003733 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3734 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3735 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3736 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3737 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3738 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3739 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3740 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3741 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3742 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3743 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3744 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3745 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3746 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3747 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3748 { 0, 0, 0, 0 }
3749};
3750
3751static void target_to_host_termios (void *dst, const void *src)
3752{
3753 struct host_termios *host = dst;
3754 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00003755
ths5fafdf22007-09-16 21:08:06 +00003756 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003757 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003758 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003759 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003760 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003761 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00003762 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003763 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3764 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00003765
Arnaud Patard44607122009-04-21 17:39:08 +03003766 memset(host->c_cc, 0, sizeof(host->c_cc));
ths5fafdf22007-09-16 21:08:06 +00003767 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3768 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00003769 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00003770 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00003771 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00003772 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00003773 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00003774 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00003775 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00003776 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3777 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00003778 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3779 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3780 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3781 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3782 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00003783 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00003784}
ths3b46e622007-09-17 08:09:54 +00003785
bellard31e31b82003-02-18 22:55:36 +00003786static void host_to_target_termios (void *dst, const void *src)
3787{
3788 struct target_termios *target = dst;
3789 const struct host_termios *host = src;
3790
ths5fafdf22007-09-16 21:08:06 +00003791 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00003792 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003793 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00003794 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003795 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00003796 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00003797 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00003798 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3799 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00003800
Arnaud Patard44607122009-04-21 17:39:08 +03003801 memset(target->c_cc, 0, sizeof(target->c_cc));
bellard31e31b82003-02-18 22:55:36 +00003802 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3803 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3804 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3805 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3806 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3807 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3808 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3809 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3810 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3811 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3812 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3813 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3814 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3815 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3816 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3817 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3818 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3819}
3820
blueswir18e853dc2008-10-05 10:49:32 +00003821static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00003822 .convert = { host_to_target_termios, target_to_host_termios },
3823 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3824 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3825};
3826
bellard5286db72003-06-05 00:57:30 +00003827static bitmask_transtbl mmap_flags_tbl[] = {
3828 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3829 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3830 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3831 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3832 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3833 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3834 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3835 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3836 { 0, 0, 0, 0 }
3837};
3838
bellard2ab83ea2003-06-15 19:56:46 +00003839#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00003840
3841/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00003842static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00003843
bellard03acab62007-11-11 14:57:14 +00003844static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003845{
3846 int size;
pbrook53a59602006-03-25 19:31:22 +00003847 void *p;
bellard6dbad632003-03-16 18:05:05 +00003848
3849 if (!ldt_table)
3850 return 0;
3851 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3852 if (size > bytecount)
3853 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00003854 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3855 if (!p)
bellard03acab62007-11-11 14:57:14 +00003856 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00003857 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00003858 memcpy(p, ldt_table, size);
3859 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00003860 return size;
3861}
3862
3863/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00003864static abi_long write_ldt(CPUX86State *env,
3865 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00003866{
3867 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00003868 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00003869 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00003870 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00003871 uint32_t *lp, entry_1, entry_2;
3872
3873 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00003874 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00003875 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00003876 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003877 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003878 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
pbrook53a59602006-03-25 19:31:22 +00003879 ldt_info.limit = tswap32(target_ldt_info->limit);
3880 ldt_info.flags = tswap32(target_ldt_info->flags);
3881 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00003882
bellard6dbad632003-03-16 18:05:05 +00003883 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00003884 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003885 seg_32bit = ldt_info.flags & 1;
3886 contents = (ldt_info.flags >> 1) & 3;
3887 read_exec_only = (ldt_info.flags >> 3) & 1;
3888 limit_in_pages = (ldt_info.flags >> 4) & 1;
3889 seg_not_present = (ldt_info.flags >> 5) & 1;
3890 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00003891#ifdef TARGET_ABI32
3892 lm = 0;
3893#else
3894 lm = (ldt_info.flags >> 7) & 1;
3895#endif
bellard6dbad632003-03-16 18:05:05 +00003896 if (contents == 3) {
3897 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00003898 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003899 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00003900 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00003901 }
3902 /* allocate the LDT */
3903 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00003904 env->ldt.base = target_mmap(0,
3905 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3906 PROT_READ|PROT_WRITE,
3907 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3908 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00003909 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00003910 memset(g2h(env->ldt.base), 0,
3911 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00003912 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00003913 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00003914 }
3915
3916 /* NOTE: same code as Linux kernel */
3917 /* Allow LDTs to be cleared by the user. */
3918 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3919 if (oldmode ||
3920 (contents == 0 &&
3921 read_exec_only == 1 &&
3922 seg_32bit == 0 &&
3923 limit_in_pages == 0 &&
3924 seg_not_present == 1 &&
3925 useable == 0 )) {
3926 entry_1 = 0;
3927 entry_2 = 0;
3928 goto install;
3929 }
3930 }
ths3b46e622007-09-17 08:09:54 +00003931
bellard6dbad632003-03-16 18:05:05 +00003932 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3933 (ldt_info.limit & 0x0ffff);
3934 entry_2 = (ldt_info.base_addr & 0xff000000) |
3935 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3936 (ldt_info.limit & 0xf0000) |
3937 ((read_exec_only ^ 1) << 9) |
3938 (contents << 10) |
3939 ((seg_not_present ^ 1) << 15) |
3940 (seg_32bit << 22) |
3941 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00003942 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00003943 0x7000;
3944 if (!oldmode)
3945 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00003946
bellard6dbad632003-03-16 18:05:05 +00003947 /* Install the new entry ... */
3948install:
3949 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3950 lp[0] = tswap32(entry_1);
3951 lp[1] = tswap32(entry_2);
3952 return 0;
3953}
3954
3955/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00003956static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3957 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00003958{
bellard03acab62007-11-11 14:57:14 +00003959 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00003960
bellard6dbad632003-03-16 18:05:05 +00003961 switch (func) {
3962 case 0:
3963 ret = read_ldt(ptr, bytecount);
3964 break;
3965 case 1:
3966 ret = write_ldt(env, ptr, bytecount, 1);
3967 break;
3968 case 0x11:
3969 ret = write_ldt(env, ptr, bytecount, 0);
3970 break;
bellard03acab62007-11-11 14:57:14 +00003971 default:
3972 ret = -TARGET_ENOSYS;
3973 break;
bellard6dbad632003-03-16 18:05:05 +00003974 }
3975 return ret;
3976}
bellard1b6b0292003-03-22 17:31:38 +00003977
blueswir14583f582008-08-24 10:35:55 +00003978#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00003979static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003980{
3981 uint64_t *gdt_table = g2h(env->gdt.base);
3982 struct target_modify_ldt_ldt_s ldt_info;
3983 struct target_modify_ldt_ldt_s *target_ldt_info;
3984 int seg_32bit, contents, read_exec_only, limit_in_pages;
3985 int seg_not_present, useable, lm;
3986 uint32_t *lp, entry_1, entry_2;
3987 int i;
3988
3989 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3990 if (!target_ldt_info)
3991 return -TARGET_EFAULT;
3992 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02003993 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
bellard8d18e892007-11-14 15:18:40 +00003994 ldt_info.limit = tswap32(target_ldt_info->limit);
3995 ldt_info.flags = tswap32(target_ldt_info->flags);
3996 if (ldt_info.entry_number == -1) {
3997 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3998 if (gdt_table[i] == 0) {
3999 ldt_info.entry_number = i;
4000 target_ldt_info->entry_number = tswap32(i);
4001 break;
4002 }
4003 }
4004 }
4005 unlock_user_struct(target_ldt_info, ptr, 1);
4006
4007 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4008 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4009 return -TARGET_EINVAL;
4010 seg_32bit = ldt_info.flags & 1;
4011 contents = (ldt_info.flags >> 1) & 3;
4012 read_exec_only = (ldt_info.flags >> 3) & 1;
4013 limit_in_pages = (ldt_info.flags >> 4) & 1;
4014 seg_not_present = (ldt_info.flags >> 5) & 1;
4015 useable = (ldt_info.flags >> 6) & 1;
4016#ifdef TARGET_ABI32
4017 lm = 0;
4018#else
4019 lm = (ldt_info.flags >> 7) & 1;
4020#endif
4021
4022 if (contents == 3) {
4023 if (seg_not_present == 0)
4024 return -TARGET_EINVAL;
4025 }
4026
4027 /* NOTE: same code as Linux kernel */
4028 /* Allow LDTs to be cleared by the user. */
4029 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4030 if ((contents == 0 &&
4031 read_exec_only == 1 &&
4032 seg_32bit == 0 &&
4033 limit_in_pages == 0 &&
4034 seg_not_present == 1 &&
4035 useable == 0 )) {
4036 entry_1 = 0;
4037 entry_2 = 0;
4038 goto install;
4039 }
4040 }
4041
4042 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4043 (ldt_info.limit & 0x0ffff);
4044 entry_2 = (ldt_info.base_addr & 0xff000000) |
4045 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4046 (ldt_info.limit & 0xf0000) |
4047 ((read_exec_only ^ 1) << 9) |
4048 (contents << 10) |
4049 ((seg_not_present ^ 1) << 15) |
4050 (seg_32bit << 22) |
4051 (limit_in_pages << 23) |
4052 (useable << 20) |
4053 (lm << 21) |
4054 0x7000;
4055
4056 /* Install the new entry ... */
4057install:
4058 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4059 lp[0] = tswap32(entry_1);
4060 lp[1] = tswap32(entry_2);
4061 return 0;
4062}
4063
blueswir18fcd3692008-08-17 20:26:25 +00004064static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00004065{
4066 struct target_modify_ldt_ldt_s *target_ldt_info;
4067 uint64_t *gdt_table = g2h(env->gdt.base);
4068 uint32_t base_addr, limit, flags;
4069 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4070 int seg_not_present, useable, lm;
4071 uint32_t *lp, entry_1, entry_2;
4072
4073 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4074 if (!target_ldt_info)
4075 return -TARGET_EFAULT;
4076 idx = tswap32(target_ldt_info->entry_number);
4077 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4078 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4079 unlock_user_struct(target_ldt_info, ptr, 1);
4080 return -TARGET_EINVAL;
4081 }
4082 lp = (uint32_t *)(gdt_table + idx);
4083 entry_1 = tswap32(lp[0]);
4084 entry_2 = tswap32(lp[1]);
4085
4086 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4087 contents = (entry_2 >> 10) & 3;
4088 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4089 seg_32bit = (entry_2 >> 22) & 1;
4090 limit_in_pages = (entry_2 >> 23) & 1;
4091 useable = (entry_2 >> 20) & 1;
4092#ifdef TARGET_ABI32
4093 lm = 0;
4094#else
4095 lm = (entry_2 >> 21) & 1;
4096#endif
4097 flags = (seg_32bit << 0) | (contents << 1) |
4098 (read_exec_only << 3) | (limit_in_pages << 4) |
4099 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4100 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4101 base_addr = (entry_1 >> 16) |
4102 (entry_2 & 0xff000000) |
4103 ((entry_2 & 0xff) << 16);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004104 target_ldt_info->base_addr = tswapal(base_addr);
bellard8d18e892007-11-14 15:18:40 +00004105 target_ldt_info->limit = tswap32(limit);
4106 target_ldt_info->flags = tswap32(flags);
4107 unlock_user_struct(target_ldt_info, ptr, 1);
4108 return 0;
4109}
blueswir14583f582008-08-24 10:35:55 +00004110#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00004111
bellardd2fd1af2007-11-14 18:08:56 +00004112#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00004113static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00004114{
Juan Quintela1add8692011-06-16 17:37:09 +01004115 abi_long ret = 0;
bellardd2fd1af2007-11-14 18:08:56 +00004116 abi_ulong val;
4117 int idx;
Juan Quintela1add8692011-06-16 17:37:09 +01004118
bellardd2fd1af2007-11-14 18:08:56 +00004119 switch(code) {
4120 case TARGET_ARCH_SET_GS:
4121 case TARGET_ARCH_SET_FS:
4122 if (code == TARGET_ARCH_SET_GS)
4123 idx = R_GS;
4124 else
4125 idx = R_FS;
4126 cpu_x86_load_seg(env, idx, 0);
4127 env->segs[idx].base = addr;
4128 break;
4129 case TARGET_ARCH_GET_GS:
4130 case TARGET_ARCH_GET_FS:
4131 if (code == TARGET_ARCH_GET_GS)
4132 idx = R_GS;
4133 else
4134 idx = R_FS;
4135 val = env->segs[idx].base;
4136 if (put_user(val, addr, abi_ulong))
Juan Quintela1add8692011-06-16 17:37:09 +01004137 ret = -TARGET_EFAULT;
bellardd2fd1af2007-11-14 18:08:56 +00004138 break;
4139 default:
4140 ret = -TARGET_EINVAL;
4141 break;
4142 }
Juan Quintela1add8692011-06-16 17:37:09 +01004143 return ret;
bellardd2fd1af2007-11-14 18:08:56 +00004144}
4145#endif
4146
bellard2ab83ea2003-06-15 19:56:46 +00004147#endif /* defined(TARGET_I386) */
4148
Riku Voipio05098a92011-03-04 15:27:29 +02004149#define NEW_STACK_SIZE 0x40000
pbrookd865bab2008-06-07 22:12:17 +00004150
Riku Voipio05098a92011-03-04 15:27:29 +02004151#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004152
4153static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4154typedef struct {
Andreas Färber9349b4f2012-03-14 01:38:32 +01004155 CPUArchState *env;
pbrookd865bab2008-06-07 22:12:17 +00004156 pthread_mutex_t mutex;
4157 pthread_cond_t cond;
4158 pthread_t thread;
4159 uint32_t tid;
4160 abi_ulong child_tidptr;
4161 abi_ulong parent_tidptr;
4162 sigset_t sigmask;
4163} new_thread_info;
4164
4165static void *clone_func(void *arg)
4166{
4167 new_thread_info *info = arg;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004168 CPUArchState *env;
Andreas Färber0d342822012-12-17 07:12:13 +01004169 CPUState *cpu;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004170 TaskState *ts;
pbrookd865bab2008-06-07 22:12:17 +00004171
4172 env = info->env;
Andreas Färber0d342822012-12-17 07:12:13 +01004173 cpu = ENV_GET_CPU(env);
pbrookd865bab2008-06-07 22:12:17 +00004174 thread_env = env;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004175 ts = (TaskState *)thread_env->opaque;
pbrookd865bab2008-06-07 22:12:17 +00004176 info->tid = gettid();
Andreas Färber0d342822012-12-17 07:12:13 +01004177 cpu->host_tid = info->tid;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004178 task_settid(ts);
pbrookd865bab2008-06-07 22:12:17 +00004179 if (info->child_tidptr)
4180 put_user_u32(info->tid, info->child_tidptr);
4181 if (info->parent_tidptr)
4182 put_user_u32(info->tid, info->parent_tidptr);
4183 /* Enable signals. */
4184 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4185 /* Signal to the parent that we're ready. */
4186 pthread_mutex_lock(&info->mutex);
4187 pthread_cond_broadcast(&info->cond);
4188 pthread_mutex_unlock(&info->mutex);
4189 /* Wait until the parent has finshed initializing the tls state. */
4190 pthread_mutex_lock(&clone_lock);
4191 pthread_mutex_unlock(&clone_lock);
4192 cpu_loop(env);
4193 /* never exits */
4194 return NULL;
4195}
4196#else
bellard1b6b0292003-03-22 17:31:38 +00004197
4198static int clone_func(void *arg)
4199{
Andreas Färber9349b4f2012-03-14 01:38:32 +01004200 CPUArchState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00004201 cpu_loop(env);
4202 /* never exits */
4203 return 0;
4204}
pbrookd865bab2008-06-07 22:12:17 +00004205#endif
bellard1b6b0292003-03-22 17:31:38 +00004206
ths0da46a62007-10-20 20:23:07 +00004207/* do_fork() Must return host values and target errnos (unlike most
4208 do_*() functions). */
Andreas Färber9349b4f2012-03-14 01:38:32 +01004209static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
pbrookd865bab2008-06-07 22:12:17 +00004210 abi_ulong parent_tidptr, target_ulong newtls,
4211 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00004212{
4213 int ret;
bellard5cd43932003-03-29 16:54:36 +00004214 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01004215 CPUArchState *new_env;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004216#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004217 unsigned int nptl_flags;
4218 sigset_t sigmask;
Riku Voipio91907492010-11-26 16:21:34 +02004219#else
4220 uint8_t *new_stack;
pbrookd865bab2008-06-07 22:12:17 +00004221#endif
ths3b46e622007-09-17 08:09:54 +00004222
balrog436d1242008-09-21 02:39:45 +00004223 /* Emulate vfork() with fork() */
4224 if (flags & CLONE_VFORK)
4225 flags &= ~(CLONE_VFORK | CLONE_VM);
4226
bellard1b6b0292003-03-22 17:31:38 +00004227 if (flags & CLONE_VM) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004228 TaskState *parent_ts = (TaskState *)env->opaque;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004229#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004230 new_thread_info info;
4231 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00004232#endif
Anthony Liguori7267c092011-08-20 22:09:37 -05004233 ts = g_malloc0(sizeof(TaskState));
pbrook624f7972008-05-31 16:11:38 +00004234 init_task_state(ts);
bellard1b6b0292003-03-22 17:31:38 +00004235 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00004236 new_env = cpu_copy(env);
Blue Swirlb4558d72009-11-07 16:34:12 +00004237#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
Andreas Färberff18b762012-05-05 14:47:34 +02004238 cpu_reset(ENV_GET_CPU(new_env));
Blue Swirlb4558d72009-11-07 16:34:12 +00004239#endif
pbrook6e68e072008-05-30 17:22:15 +00004240 /* Init regs that differ from the parent. */
4241 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00004242 new_env->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03004243 ts->bprm = parent_ts->bprm;
4244 ts->info = parent_ts->info;
Juan Quintela2f7bb872009-07-27 16:13:24 +02004245#if defined(CONFIG_USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00004246 nptl_flags = flags;
4247 flags &= ~CLONE_NPTL_FLAGS2;
4248
pbrookc2764712009-03-07 15:24:59 +00004249 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4250 ts->child_tidptr = child_tidptr;
4251 }
4252
pbrookd865bab2008-06-07 22:12:17 +00004253 if (nptl_flags & CLONE_SETTLS)
4254 cpu_set_tls (new_env, newtls);
4255
4256 /* Grab a mutex so that thread setup appears atomic. */
4257 pthread_mutex_lock(&clone_lock);
4258
4259 memset(&info, 0, sizeof(info));
4260 pthread_mutex_init(&info.mutex, NULL);
4261 pthread_mutex_lock(&info.mutex);
4262 pthread_cond_init(&info.cond, NULL);
4263 info.env = new_env;
4264 if (nptl_flags & CLONE_CHILD_SETTID)
4265 info.child_tidptr = child_tidptr;
4266 if (nptl_flags & CLONE_PARENT_SETTID)
4267 info.parent_tidptr = parent_tidptr;
4268
4269 ret = pthread_attr_init(&attr);
Nathan Froyd48e15fc2010-10-29 07:48:57 -07004270 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4271 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pbrookd865bab2008-06-07 22:12:17 +00004272 /* It is not safe to deliver signals until the child has finished
4273 initializing, so temporarily block all signals. */
4274 sigfillset(&sigmask);
4275 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4276
4277 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00004278 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00004279
4280 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4281 pthread_attr_destroy(&attr);
4282 if (ret == 0) {
4283 /* Wait for the child to initialize. */
4284 pthread_cond_wait(&info.cond, &info.mutex);
4285 ret = info.tid;
4286 if (flags & CLONE_PARENT_SETTID)
4287 put_user_u32(ret, parent_tidptr);
4288 } else {
4289 ret = -1;
4290 }
4291 pthread_mutex_unlock(&info.mutex);
4292 pthread_cond_destroy(&info.cond);
4293 pthread_mutex_destroy(&info.mutex);
4294 pthread_mutex_unlock(&clone_lock);
4295#else
4296 if (flags & CLONE_NPTL_FLAGS2)
4297 return -EINVAL;
4298 /* This is probably going to die very quickly, but do it anyway. */
Anthony Liguori7267c092011-08-20 22:09:37 -05004299 new_stack = g_malloc0 (NEW_STACK_SIZE);
bellard27725c12003-04-29 21:08:18 +00004300#ifdef __ia64__
Aurelien Jarno60e99242010-03-29 02:12:51 +02004301 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00004302#else
4303 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
4304#endif
pbrookd865bab2008-06-07 22:12:17 +00004305#endif
bellard1b6b0292003-03-22 17:31:38 +00004306 } else {
4307 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00004308 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00004309 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00004310 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00004311 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00004312 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00004313 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00004314 cpu_clone_regs(env, newsp);
4315 fork_end(1);
Juan Quintela2f7bb872009-07-27 16:13:24 +02004316#if defined(CONFIG_USE_NPTL)
aurel322b1319c2008-12-18 22:44:04 +00004317 /* There is a race condition here. The parent process could
4318 theoretically read the TID in the child process before the child
4319 tid is set. This would require using either ptrace
4320 (not implemented) or having *_tidptr to point at a shared memory
4321 mapping. We can't repeat the spinlock hack used above because
4322 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00004323 if (flags & CLONE_CHILD_SETTID)
4324 put_user_u32(gettid(), child_tidptr);
4325 if (flags & CLONE_PARENT_SETTID)
4326 put_user_u32(gettid(), parent_tidptr);
4327 ts = (TaskState *)env->opaque;
4328 if (flags & CLONE_SETTLS)
4329 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00004330 if (flags & CLONE_CHILD_CLEARTID)
4331 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00004332#endif
pbrookd865bab2008-06-07 22:12:17 +00004333 } else {
4334 fork_end(0);
4335 }
bellard1b6b0292003-03-22 17:31:38 +00004336 }
4337 return ret;
4338}
4339
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004340/* warning : doesn't handle linux specific flags... */
4341static int target_to_host_fcntl_cmd(int cmd)
4342{
4343 switch(cmd) {
4344 case TARGET_F_DUPFD:
4345 case TARGET_F_GETFD:
4346 case TARGET_F_SETFD:
4347 case TARGET_F_GETFL:
4348 case TARGET_F_SETFL:
4349 return cmd;
4350 case TARGET_F_GETLK:
4351 return F_GETLK;
4352 case TARGET_F_SETLK:
4353 return F_SETLK;
4354 case TARGET_F_SETLKW:
4355 return F_SETLKW;
4356 case TARGET_F_GETOWN:
4357 return F_GETOWN;
4358 case TARGET_F_SETOWN:
4359 return F_SETOWN;
4360 case TARGET_F_GETSIG:
4361 return F_GETSIG;
4362 case TARGET_F_SETSIG:
4363 return F_SETSIG;
4364#if TARGET_ABI_BITS == 32
4365 case TARGET_F_GETLK64:
4366 return F_GETLK64;
4367 case TARGET_F_SETLK64:
4368 return F_SETLK64;
4369 case TARGET_F_SETLKW64:
4370 return F_SETLKW64;
4371#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004372 case TARGET_F_SETLEASE:
4373 return F_SETLEASE;
4374 case TARGET_F_GETLEASE:
4375 return F_GETLEASE;
malcfbd5de92009-09-06 06:31:59 +04004376#ifdef F_DUPFD_CLOEXEC
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004377 case TARGET_F_DUPFD_CLOEXEC:
4378 return F_DUPFD_CLOEXEC;
malcfbd5de92009-09-06 06:31:59 +04004379#endif
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004380 case TARGET_F_NOTIFY:
4381 return F_NOTIFY;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004382 default:
4383 return -TARGET_EINVAL;
4384 }
4385 return -TARGET_EINVAL;
4386}
4387
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004388#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4389static const bitmask_transtbl flock_tbl[] = {
4390 TRANSTBL_CONVERT(F_RDLCK),
4391 TRANSTBL_CONVERT(F_WRLCK),
4392 TRANSTBL_CONVERT(F_UNLCK),
4393 TRANSTBL_CONVERT(F_EXLCK),
4394 TRANSTBL_CONVERT(F_SHLCK),
4395 { 0, 0, 0, 0 }
4396};
4397
blueswir1992f48a2007-10-14 16:27:31 +00004398static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00004399{
4400 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00004401 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00004402 struct flock64 fl64;
4403 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00004404 abi_long ret;
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004405 int host_cmd = target_to_host_fcntl_cmd(cmd);
4406
4407 if (host_cmd == -TARGET_EINVAL)
4408 return host_cmd;
pbrook53a59602006-03-25 19:31:22 +00004409
bellard7775e9e2003-05-14 22:46:48 +00004410 switch(cmd) {
4411 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00004412 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4413 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004414 fl.l_type =
4415 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
ths58134272007-03-31 18:59:32 +00004416 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004417 fl.l_start = tswapal(target_fl->l_start);
4418 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004419 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00004420 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004421 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004422 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004423 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4424 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004425 target_fl->l_type =
4426 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004427 target_fl->l_whence = tswap16(fl.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004428 target_fl->l_start = tswapal(fl.l_start);
4429 target_fl->l_len = tswapal(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004430 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004431 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00004432 }
4433 break;
ths3b46e622007-09-17 08:09:54 +00004434
bellard7775e9e2003-05-14 22:46:48 +00004435 case TARGET_F_SETLK:
4436 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00004437 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4438 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004439 fl.l_type =
4440 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
bellard7775e9e2003-05-14 22:46:48 +00004441 fl.l_whence = tswap16(target_fl->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004442 fl.l_start = tswapal(target_fl->l_start);
4443 fl.l_len = tswapal(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004444 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004445 unlock_user_struct(target_fl, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004446 ret = get_errno(fcntl(fd, host_cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00004447 break;
ths3b46e622007-09-17 08:09:54 +00004448
bellard7775e9e2003-05-14 22:46:48 +00004449 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00004450 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4451 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004452 fl64.l_type =
4453 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths58134272007-03-31 18:59:32 +00004454 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004455 fl64.l_start = tswap64(target_fl64->l_start);
4456 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004457 fl64.l_pid = tswap32(target_fl64->l_pid);
ths58134272007-03-31 18:59:32 +00004458 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004459 ret = get_errno(fcntl(fd, host_cmd, &fl64));
ths43f238d2007-01-05 20:55:49 +00004460 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00004461 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4462 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004463 target_fl64->l_type =
4464 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004465 target_fl64->l_whence = tswap16(fl64.l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004466 target_fl64->l_start = tswap64(fl64.l_start);
4467 target_fl64->l_len = tswap64(fl64.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004468 target_fl64->l_pid = tswap32(fl64.l_pid);
ths43f238d2007-01-05 20:55:49 +00004469 unlock_user_struct(target_fl64, arg, 1);
4470 }
bellard9ee1fa22007-11-11 15:11:19 +00004471 break;
bellard7775e9e2003-05-14 22:46:48 +00004472 case TARGET_F_SETLK64:
4473 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00004474 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4475 return -TARGET_EFAULT;
Laurent Vivier2ba7f732013-01-10 21:42:48 +01004476 fl64.l_type =
4477 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
ths43f238d2007-01-05 20:55:49 +00004478 fl64.l_whence = tswap16(target_fl64->l_whence);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004479 fl64.l_start = tswap64(target_fl64->l_start);
4480 fl64.l_len = tswap64(target_fl64->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004481 fl64.l_pid = tswap32(target_fl64->l_pid);
ths43f238d2007-01-05 20:55:49 +00004482 unlock_user_struct(target_fl64, arg, 0);
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004483 ret = get_errno(fcntl(fd, host_cmd, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00004484 break;
4485
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004486 case TARGET_F_GETFL:
4487 ret = get_errno(fcntl(fd, host_cmd, arg));
bellard9ee1fa22007-11-11 15:11:19 +00004488 if (ret >= 0) {
4489 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4490 }
bellardffa65c32004-01-04 23:57:22 +00004491 break;
4492
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004493 case TARGET_F_SETFL:
4494 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4495 break;
4496
4497 case TARGET_F_SETOWN:
4498 case TARGET_F_GETOWN:
4499 case TARGET_F_SETSIG:
4500 case TARGET_F_GETSIG:
Ulrich Hecht7e22e542009-07-24 19:10:27 +02004501 case TARGET_F_SETLEASE:
4502 case TARGET_F_GETLEASE:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02004503 ret = get_errno(fcntl(fd, host_cmd, arg));
bellardffa65c32004-01-04 23:57:22 +00004504 break;
4505
bellard7775e9e2003-05-14 22:46:48 +00004506 default:
bellard9ee1fa22007-11-11 15:11:19 +00004507 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00004508 break;
4509 }
4510 return ret;
4511}
4512
bellard67867302003-11-23 17:05:30 +00004513#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00004514
bellard67867302003-11-23 17:05:30 +00004515static inline int high2lowuid(int uid)
4516{
4517 if (uid > 65535)
4518 return 65534;
4519 else
4520 return uid;
4521}
4522
4523static inline int high2lowgid(int gid)
4524{
4525 if (gid > 65535)
4526 return 65534;
4527 else
4528 return gid;
4529}
4530
4531static inline int low2highuid(int uid)
4532{
4533 if ((int16_t)uid == -1)
4534 return -1;
4535 else
4536 return uid;
4537}
4538
4539static inline int low2highgid(int gid)
4540{
4541 if ((int16_t)gid == -1)
4542 return -1;
4543 else
4544 return gid;
4545}
Riku Voipio0c866a72011-04-18 15:23:06 +03004546static inline int tswapid(int id)
4547{
4548 return tswap16(id);
4549}
4550#else /* !USE_UID16 */
4551static inline int high2lowuid(int uid)
4552{
4553 return uid;
4554}
4555static inline int high2lowgid(int gid)
4556{
4557 return gid;
4558}
4559static inline int low2highuid(int uid)
4560{
4561 return uid;
4562}
4563static inline int low2highgid(int gid)
4564{
4565 return gid;
4566}
4567static inline int tswapid(int id)
4568{
4569 return tswap32(id);
4570}
bellard67867302003-11-23 17:05:30 +00004571#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00004572
bellard31e31b82003-02-18 22:55:36 +00004573void syscall_init(void)
4574{
bellard2ab83ea2003-06-15 19:56:46 +00004575 IOCTLEntry *ie;
4576 const argtype *arg_type;
4577 int size;
thsb92c47c2007-11-01 00:07:38 +00004578 int i;
bellard2ab83ea2003-06-15 19:56:46 +00004579
Blue Swirl001faf32009-05-13 17:53:17 +00004580#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00004581#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00004582#include "syscall_types.h"
4583#undef STRUCT
4584#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00004585
Peter Maydelldd6e9572012-07-23 08:07:22 +00004586 /* Build target_to_host_errno_table[] table from
4587 * host_to_target_errno_table[]. */
4588 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4589 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4590 }
4591
bellard2ab83ea2003-06-15 19:56:46 +00004592 /* we patch the ioctl size if necessary. We rely on the fact that
4593 no ioctl has all the bits at '1' in the size field */
4594 ie = ioctl_entries;
4595 while (ie->target_cmd != 0) {
4596 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4597 TARGET_IOC_SIZEMASK) {
4598 arg_type = ie->arg_type;
4599 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00004600 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00004601 ie->target_cmd);
4602 exit(1);
4603 }
4604 arg_type++;
4605 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00004606 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00004607 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4608 (size << TARGET_IOC_SIZESHIFT);
4609 }
thsb92c47c2007-11-01 00:07:38 +00004610
bellard2ab83ea2003-06-15 19:56:46 +00004611 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00004612#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4613 (defined(__x86_64__) && defined(TARGET_X86_64))
4614 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4615 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4616 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00004617 }
4618#endif
4619 ie++;
4620 }
bellard31e31b82003-02-18 22:55:36 +00004621}
bellardc573ff62004-01-04 15:51:36 +00004622
blueswir1992f48a2007-10-14 16:27:31 +00004623#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00004624static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4625{
thsaf325d32008-06-10 15:29:15 +00004626#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00004627 return ((uint64_t)word0 << 32) | word1;
4628#else
4629 return ((uint64_t)word1 << 32) | word0;
4630#endif
4631}
blueswir1992f48a2007-10-14 16:27:31 +00004632#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00004633static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4634{
4635 return word0;
4636}
blueswir1992f48a2007-10-14 16:27:31 +00004637#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00004638
4639#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00004640static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4641 abi_long arg2,
4642 abi_long arg3,
4643 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004644{
Riku Voipio48e515d2011-07-12 15:40:51 +03004645 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004646 arg2 = arg3;
4647 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004648 }
pbrookce4defa2006-02-09 16:49:55 +00004649 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4650}
4651#endif
4652
4653#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00004654static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4655 abi_long arg2,
4656 abi_long arg3,
4657 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00004658{
Riku Voipio48e515d2011-07-12 15:40:51 +03004659 if (regpairs_aligned(cpu_env)) {
pbrookce4defa2006-02-09 16:49:55 +00004660 arg2 = arg3;
4661 arg3 = arg4;
Riku Voipio48e515d2011-07-12 15:40:51 +03004662 }
pbrookce4defa2006-02-09 16:49:55 +00004663 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4664}
4665#endif
4666
bellard579a97f2007-11-11 14:26:47 +00004667static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4668 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00004669{
4670 struct target_timespec *target_ts;
4671
bellard579a97f2007-11-11 14:26:47 +00004672 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4673 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004674 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4675 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004676 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00004677 return 0;
pbrook53a59602006-03-25 19:31:22 +00004678}
4679
bellard579a97f2007-11-11 14:26:47 +00004680static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4681 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00004682{
4683 struct target_timespec *target_ts;
4684
bellard579a97f2007-11-11 14:26:47 +00004685 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4686 return -TARGET_EFAULT;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02004687 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4688 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
pbrook53a59602006-03-25 19:31:22 +00004689 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00004690 return 0;
pbrook53a59602006-03-25 19:31:22 +00004691}
4692
aurel329d33b762009-04-08 23:07:05 +00004693#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00004694static inline abi_long host_to_target_stat64(void *cpu_env,
4695 abi_ulong target_addr,
4696 struct stat *host_st)
4697{
4698#ifdef TARGET_ARM
4699 if (((CPUARMState *)cpu_env)->eabi) {
4700 struct target_eabi_stat64 *target_st;
4701
4702 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4703 return -TARGET_EFAULT;
4704 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4705 __put_user(host_st->st_dev, &target_st->st_dev);
4706 __put_user(host_st->st_ino, &target_st->st_ino);
4707#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4708 __put_user(host_st->st_ino, &target_st->__st_ino);
4709#endif
4710 __put_user(host_st->st_mode, &target_st->st_mode);
4711 __put_user(host_st->st_nlink, &target_st->st_nlink);
4712 __put_user(host_st->st_uid, &target_st->st_uid);
4713 __put_user(host_st->st_gid, &target_st->st_gid);
4714 __put_user(host_st->st_rdev, &target_st->st_rdev);
4715 __put_user(host_st->st_size, &target_st->st_size);
4716 __put_user(host_st->st_blksize, &target_st->st_blksize);
4717 __put_user(host_st->st_blocks, &target_st->st_blocks);
4718 __put_user(host_st->st_atime, &target_st->target_st_atime);
4719 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4720 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4721 unlock_user_struct(target_st, target_addr, 1);
4722 } else
4723#endif
4724 {
Richard Hendersoned18c5c2010-04-26 10:27:22 -07004725#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
aurel329d33b762009-04-08 23:07:05 +00004726 struct target_stat *target_st;
4727#else
balrog6a24a772008-09-20 02:23:36 +00004728 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00004729#endif
balrog6a24a772008-09-20 02:23:36 +00004730
4731 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4732 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00004733 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00004734 __put_user(host_st->st_dev, &target_st->st_dev);
4735 __put_user(host_st->st_ino, &target_st->st_ino);
4736#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4737 __put_user(host_st->st_ino, &target_st->__st_ino);
4738#endif
4739 __put_user(host_st->st_mode, &target_st->st_mode);
4740 __put_user(host_st->st_nlink, &target_st->st_nlink);
4741 __put_user(host_st->st_uid, &target_st->st_uid);
4742 __put_user(host_st->st_gid, &target_st->st_gid);
4743 __put_user(host_st->st_rdev, &target_st->st_rdev);
4744 /* XXX: better use of kernel struct */
4745 __put_user(host_st->st_size, &target_st->st_size);
4746 __put_user(host_st->st_blksize, &target_st->st_blksize);
4747 __put_user(host_st->st_blocks, &target_st->st_blocks);
4748 __put_user(host_st->st_atime, &target_st->target_st_atime);
4749 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4750 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4751 unlock_user_struct(target_st, target_addr, 1);
4752 }
4753
4754 return 0;
4755}
4756#endif
4757
Juan Quintela2f7bb872009-07-27 16:13:24 +02004758#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00004759/* ??? Using host futex calls even when target atomic operations
4760 are not really atomic probably breaks things. However implementing
4761 futexes locally would make futexes shared between multiple processes
4762 tricky. However they're probably useless because guest atomic
4763 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00004764static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4765 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00004766{
4767 struct timespec ts, *pts;
Nathan Froyda16aae02009-08-03 08:43:29 -07004768 int base_op;
pbrookbd0c5662008-05-29 14:34:11 +00004769
4770 /* ??? We assume FUTEX_* constants are the same on both host
4771 and target. */
Martin Mohringa29ccd62009-05-04 21:34:56 +03004772#ifdef FUTEX_CMD_MASK
Nathan Froyda16aae02009-08-03 08:43:29 -07004773 base_op = op & FUTEX_CMD_MASK;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004774#else
Nathan Froyda16aae02009-08-03 08:43:29 -07004775 base_op = op;
Martin Mohringa29ccd62009-05-04 21:34:56 +03004776#endif
Nathan Froyda16aae02009-08-03 08:43:29 -07004777 switch (base_op) {
pbrookbd0c5662008-05-29 14:34:11 +00004778 case FUTEX_WAIT:
John Rigbycce246e2013-02-23 16:14:07 -07004779 case FUTEX_WAIT_BITSET:
pbrookbd0c5662008-05-29 14:34:11 +00004780 if (timeout) {
4781 pts = &ts;
4782 target_to_host_timespec(pts, timeout);
4783 } else {
4784 pts = NULL;
4785 }
Martin Mohringa29ccd62009-05-04 21:34:56 +03004786 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
John Rigbycce246e2013-02-23 16:14:07 -07004787 pts, NULL, val3));
pbrookbd0c5662008-05-29 14:34:11 +00004788 case FUTEX_WAKE:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004789 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004790 case FUTEX_FD:
Martin Mohringa29ccd62009-05-04 21:34:56 +03004791 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
pbrookbd0c5662008-05-29 14:34:11 +00004792 case FUTEX_REQUEUE:
pbrookbd0c5662008-05-29 14:34:11 +00004793 case FUTEX_CMP_REQUEUE:
Nathan Froyda16aae02009-08-03 08:43:29 -07004794 case FUTEX_WAKE_OP:
4795 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4796 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4797 But the prototype takes a `struct timespec *'; insert casts
4798 to satisfy the compiler. We do not need to tswap TIMEOUT
4799 since it's not compared to guest memory. */
4800 pts = (struct timespec *)(uintptr_t) timeout;
4801 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4802 g2h(uaddr2),
4803 (base_op == FUTEX_CMP_REQUEUE
4804 ? tswap32(val3)
4805 : val3)));
pbrookbd0c5662008-05-29 14:34:11 +00004806 default:
4807 return -TARGET_ENOSYS;
4808 }
4809}
4810#endif
4811
pbrook1d9d8b52009-04-16 15:17:02 +00004812/* Map host to target signal numbers for the wait family of syscalls.
4813 Assume all other status bits are the same. */
Richard Hendersona05c6402012-09-15 11:34:20 -07004814int host_to_target_waitstatus(int status)
pbrook1d9d8b52009-04-16 15:17:02 +00004815{
4816 if (WIFSIGNALED(status)) {
4817 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4818 }
4819 if (WIFSTOPPED(status)) {
4820 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4821 | (status & 0xff);
4822 }
4823 return status;
4824}
4825
pbrooka745ec62008-05-06 15:36:17 +00004826int get_osversion(void)
4827{
4828 static int osversion;
4829 struct new_utsname buf;
4830 const char *s;
4831 int i, n, tmp;
4832 if (osversion)
4833 return osversion;
4834 if (qemu_uname_release && *qemu_uname_release) {
4835 s = qemu_uname_release;
4836 } else {
4837 if (sys_uname(&buf))
4838 return 0;
4839 s = buf.release;
4840 }
4841 tmp = 0;
4842 for (i = 0; i < 3; i++) {
4843 n = 0;
4844 while (*s >= '0' && *s <= '9') {
4845 n *= 10;
4846 n += *s - '0';
4847 s++;
4848 }
4849 tmp = (tmp << 8) + n;
4850 if (*s == '.')
4851 s++;
4852 }
4853 osversion = tmp;
4854 return osversion;
4855}
4856
Alexander Graf36c08d42011-11-02 20:23:24 +01004857
4858static int open_self_maps(void *cpu_env, int fd)
4859{
Alexander Graf1a49ef22012-05-01 16:30:28 +01004860#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Andreas Färber9349b4f2012-03-14 01:38:32 +01004861 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf1a49ef22012-05-01 16:30:28 +01004862#endif
4863 FILE *fp;
4864 char *line = NULL;
4865 size_t len = 0;
4866 ssize_t read;
Alexander Graf36c08d42011-11-02 20:23:24 +01004867
Alexander Graf1a49ef22012-05-01 16:30:28 +01004868 fp = fopen("/proc/self/maps", "r");
4869 if (fp == NULL) {
4870 return -EACCES;
4871 }
4872
4873 while ((read = getline(&line, &len, fp)) != -1) {
4874 int fields, dev_maj, dev_min, inode;
4875 uint64_t min, max, offset;
4876 char flag_r, flag_w, flag_x, flag_p;
4877 char path[512] = "";
4878 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
4879 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
4880 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
4881
4882 if ((fields < 10) || (fields > 11)) {
4883 continue;
4884 }
4885 if (!strncmp(path, "[stack]", 7)) {
4886 continue;
4887 }
4888 if (h2g_valid(min) && h2g_valid(max)) {
4889 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
Christophe Lyone24fed42013-04-02 14:03:38 +02004890 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
Alexander Graf1a49ef22012-05-01 16:30:28 +01004891 h2g(min), h2g(max), flag_r, flag_w,
4892 flag_x, flag_p, offset, dev_maj, dev_min, inode,
Christophe Lyone24fed42013-04-02 14:03:38 +02004893 path[0] ? " " : "", path);
Alexander Graf1a49ef22012-05-01 16:30:28 +01004894 }
4895 }
4896
4897 free(line);
4898 fclose(fp);
4899
4900#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
Alexander Graf36c08d42011-11-02 20:23:24 +01004901 dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
4902 (unsigned long long)ts->info->stack_limit,
Alexander Graf1bdd7c72012-05-30 14:45:21 +02004903 (unsigned long long)(ts->info->start_stack +
4904 (TARGET_PAGE_SIZE - 1)) & TARGET_PAGE_MASK,
Alexander Graf1a49ef22012-05-01 16:30:28 +01004905 (unsigned long long)0);
4906#endif
Alexander Graf36c08d42011-11-02 20:23:24 +01004907
4908 return 0;
4909}
4910
Alexander Graf480b8e72011-11-02 20:23:25 +01004911static int open_self_stat(void *cpu_env, int fd)
4912{
Andreas Färber9349b4f2012-03-14 01:38:32 +01004913 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf480b8e72011-11-02 20:23:25 +01004914 abi_ulong start_stack = ts->info->start_stack;
4915 int i;
4916
4917 for (i = 0; i < 44; i++) {
4918 char buf[128];
4919 int len;
4920 uint64_t val = 0;
4921
Fabio Erculianie0e65be2012-01-03 09:38:34 +00004922 if (i == 0) {
4923 /* pid */
4924 val = getpid();
4925 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
4926 } else if (i == 1) {
4927 /* app name */
4928 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
4929 } else if (i == 27) {
4930 /* stack bottom */
4931 val = start_stack;
4932 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
4933 } else {
4934 /* for the rest, there is MasterCard */
4935 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
Alexander Graf480b8e72011-11-02 20:23:25 +01004936 }
Fabio Erculianie0e65be2012-01-03 09:38:34 +00004937
Alexander Graf480b8e72011-11-02 20:23:25 +01004938 len = strlen(buf);
4939 if (write(fd, buf, len) != len) {
4940 return -1;
4941 }
4942 }
4943
4944 return 0;
4945}
4946
Alexander Graf257450e2011-11-02 20:23:26 +01004947static int open_self_auxv(void *cpu_env, int fd)
4948{
Andreas Färber9349b4f2012-03-14 01:38:32 +01004949 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Alexander Graf257450e2011-11-02 20:23:26 +01004950 abi_ulong auxv = ts->info->saved_auxv;
4951 abi_ulong len = ts->info->auxv_len;
4952 char *ptr;
4953
4954 /*
4955 * Auxiliary vector is stored in target process stack.
4956 * read in whole auxv vector and copy it to file
4957 */
4958 ptr = lock_user(VERIFY_READ, auxv, len, 0);
4959 if (ptr != NULL) {
4960 while (len > 0) {
4961 ssize_t r;
4962 r = write(fd, ptr, len);
4963 if (r <= 0) {
4964 break;
4965 }
4966 len -= r;
4967 ptr += r;
4968 }
4969 lseek(fd, 0, SEEK_SET);
4970 unlock_user(ptr, auxv, len);
4971 }
4972
4973 return 0;
4974}
4975
Alexander Graf3be14d02011-11-02 20:23:23 +01004976static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
4977{
4978 struct fake_open {
4979 const char *filename;
4980 int (*fill)(void *cpu_env, int fd);
4981 };
4982 const struct fake_open *fake_open;
4983 static const struct fake_open fakes[] = {
Alexander Graf36c08d42011-11-02 20:23:24 +01004984 { "/proc/self/maps", open_self_maps },
Alexander Graf480b8e72011-11-02 20:23:25 +01004985 { "/proc/self/stat", open_self_stat },
Alexander Graf257450e2011-11-02 20:23:26 +01004986 { "/proc/self/auxv", open_self_auxv },
Alexander Graf3be14d02011-11-02 20:23:23 +01004987 { NULL, NULL }
4988 };
4989
4990 for (fake_open = fakes; fake_open->filename; fake_open++) {
4991 if (!strncmp(pathname, fake_open->filename,
4992 strlen(fake_open->filename))) {
4993 break;
4994 }
4995 }
4996
4997 if (fake_open->filename) {
4998 const char *tmpdir;
4999 char filename[PATH_MAX];
5000 int fd, r;
5001
5002 /* create temporary file to map stat to */
5003 tmpdir = getenv("TMPDIR");
5004 if (!tmpdir)
5005 tmpdir = "/tmp";
5006 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5007 fd = mkstemp(filename);
5008 if (fd < 0) {
5009 return fd;
5010 }
5011 unlink(filename);
5012
5013 if ((r = fake_open->fill(cpu_env, fd))) {
5014 close(fd);
5015 return r;
5016 }
5017 lseek(fd, 0, SEEK_SET);
5018
5019 return fd;
5020 }
5021
5022 return get_errno(open(path(pathname), flags, mode));
5023}
5024
ths0da46a62007-10-20 20:23:07 +00005025/* do_syscall() should always have a single exit point at the end so
5026 that actions, such as logging of syscall results, can be performed.
5027 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00005028abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5029 abi_long arg2, abi_long arg3, abi_long arg4,
Peter Maydell5945cfc2011-06-16 17:37:13 +01005030 abi_long arg5, abi_long arg6, abi_long arg7,
5031 abi_long arg8)
bellard31e31b82003-02-18 22:55:36 +00005032{
blueswir1992f48a2007-10-14 16:27:31 +00005033 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00005034 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00005035 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00005036 void *p;
ths3b46e622007-09-17 08:09:54 +00005037
bellard72f03902003-02-18 23:33:18 +00005038#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00005039 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00005040#endif
thsb92c47c2007-11-01 00:07:38 +00005041 if(do_strace)
5042 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5043
bellard31e31b82003-02-18 22:55:36 +00005044 switch(num) {
5045 case TARGET_NR_exit:
Juan Quintela2f7bb872009-07-27 16:13:24 +02005046#ifdef CONFIG_USE_NPTL
pbrookc2764712009-03-07 15:24:59 +00005047 /* In old applications this may be used to implement _exit(2).
5048 However in threaded applictions it is used for thread termination,
5049 and _exit_group is used for application termination.
5050 Do thread termination if we have more then one thread. */
5051 /* FIXME: This probably breaks if a signal arrives. We should probably
5052 be disabling signals. */
5053 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07005054 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005055 CPUArchState **lastp;
5056 CPUArchState *p;
pbrookc2764712009-03-07 15:24:59 +00005057
5058 cpu_list_lock();
5059 lastp = &first_cpu;
5060 p = first_cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +01005061 while (p && p != (CPUArchState *)cpu_env) {
pbrookc2764712009-03-07 15:24:59 +00005062 lastp = &p->next_cpu;
5063 p = p->next_cpu;
5064 }
5065 /* If we didn't find the CPU for this thread then something is
5066 horribly wrong. */
5067 if (!p)
5068 abort();
5069 /* Remove the CPU from the list. */
5070 *lastp = p->next_cpu;
5071 cpu_list_unlock();
Andreas Färber9349b4f2012-03-14 01:38:32 +01005072 ts = ((CPUArchState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00005073 if (ts->child_tidptr) {
5074 put_user_u32(0, ts->child_tidptr);
5075 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5076 NULL, NULL, 0);
5077 }
Nathan Froyd48e15fc2010-10-29 07:48:57 -07005078 thread_env = NULL;
Paolo Bonzini5c099532013-01-25 14:12:38 +01005079 object_unref(OBJECT(ENV_GET_CPU(cpu_env)));
Anthony Liguori7267c092011-08-20 22:09:37 -05005080 g_free(ts);
pbrookc2764712009-03-07 15:24:59 +00005081 pthread_exit(NULL);
5082 }
5083#endif
Juan Quintela9788c9c2009-07-27 16:13:02 +02005084#ifdef TARGET_GPROF
bellard7d132992003-03-06 23:23:54 +00005085 _mcleanup();
5086#endif
bellarde9009672005-04-26 20:42:36 +00005087 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00005088 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00005089 ret = 0; /* avoid warning */
5090 break;
5091 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00005092 if (arg3 == 0)
5093 ret = 0;
5094 else {
5095 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5096 goto efault;
5097 ret = get_errno(read(arg1, p, arg3));
5098 unlock_user(p, arg2, ret);
5099 }
bellard31e31b82003-02-18 22:55:36 +00005100 break;
5101 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00005102 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5103 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005104 ret = get_errno(write(arg1, p, arg3));
5105 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00005106 break;
5107 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00005108 if (!(p = lock_user_string(arg1)))
5109 goto efault;
Alexander Graf3be14d02011-11-02 20:23:23 +01005110 ret = get_errno(do_open(cpu_env, p,
5111 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5112 arg3));
pbrook53a59602006-03-25 19:31:22 +00005113 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005114 break;
ths82424832007-09-24 09:21:55 +00005115#if defined(TARGET_NR_openat) && defined(__NR_openat)
5116 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00005117 if (!(p = lock_user_string(arg2)))
5118 goto efault;
5119 ret = get_errno(sys_openat(arg1,
5120 path(p),
5121 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5122 arg4));
5123 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00005124 break;
5125#endif
bellard31e31b82003-02-18 22:55:36 +00005126 case TARGET_NR_close:
5127 ret = get_errno(close(arg1));
5128 break;
5129 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00005130 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00005131 break;
5132 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00005133 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00005134 break;
thse5febef2007-04-01 18:31:35 +00005135#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00005136 case TARGET_NR_waitpid:
5137 {
pbrook53a59602006-03-25 19:31:22 +00005138 int status;
5139 ret = get_errno(waitpid(arg1, &status, arg3));
Alexander Graf53795572011-11-24 00:44:43 +01005140 if (!is_error(ret) && arg2 && ret
pbrook1d9d8b52009-04-16 15:17:02 +00005141 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00005142 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005143 }
5144 break;
thse5febef2007-04-01 18:31:35 +00005145#endif
pbrookf0cbb612008-05-30 18:20:05 +00005146#ifdef TARGET_NR_waitid
5147 case TARGET_NR_waitid:
5148 {
5149 siginfo_t info;
5150 info.si_pid = 0;
5151 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5152 if (!is_error(ret) && arg3 && info.si_pid != 0) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005153 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
pbrookf0cbb612008-05-30 18:20:05 +00005154 goto efault;
5155 host_to_target_siginfo(p, &info);
Anthony Liguoric227f092009-10-01 16:12:16 -05005156 unlock_user(p, arg3, sizeof(target_siginfo_t));
pbrookf0cbb612008-05-30 18:20:05 +00005157 }
5158 }
5159 break;
5160#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005161#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005162 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00005163 if (!(p = lock_user_string(arg1)))
5164 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005165 ret = get_errno(creat(p, arg2));
5166 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005167 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005168#endif
bellard31e31b82003-02-18 22:55:36 +00005169 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00005170 {
5171 void * p2;
5172 p = lock_user_string(arg1);
5173 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005174 if (!p || !p2)
5175 ret = -TARGET_EFAULT;
5176 else
5177 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005178 unlock_user(p2, arg2, 0);
5179 unlock_user(p, arg1, 0);
5180 }
bellard31e31b82003-02-18 22:55:36 +00005181 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005182#if defined(TARGET_NR_linkat)
ths64f0ce42007-09-24 09:25:06 +00005183 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00005184 {
5185 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00005186 if (!arg2 || !arg4)
5187 goto efault;
ths64f0ce42007-09-24 09:25:06 +00005188 p = lock_user_string(arg2);
5189 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005190 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005191 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00005192 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01005193 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00005194 unlock_user(p, arg2, 0);
5195 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00005196 }
5197 break;
5198#endif
bellard31e31b82003-02-18 22:55:36 +00005199 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00005200 if (!(p = lock_user_string(arg1)))
5201 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005202 ret = get_errno(unlink(p));
5203 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005204 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005205#if defined(TARGET_NR_unlinkat)
ths8170f562007-09-24 09:24:11 +00005206 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00005207 if (!(p = lock_user_string(arg2)))
5208 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005209 ret = get_errno(unlinkat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005210 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00005211 break;
balrogb7d35e62007-12-12 00:40:24 +00005212#endif
bellard31e31b82003-02-18 22:55:36 +00005213 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00005214 {
5215 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00005216 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00005217 abi_ulong gp;
5218 abi_ulong guest_argp;
5219 abi_ulong guest_envp;
5220 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00005221 char **q;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005222 int total_size = 0;
bellard7854b052003-03-29 17:22:23 +00005223
bellardf7341ff2003-03-30 21:00:25 +00005224 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00005225 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00005226 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005227 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005228 goto efault;
ths03aa1972007-12-02 06:28:08 +00005229 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005230 break;
bellard7854b052003-03-29 17:22:23 +00005231 argc++;
bellard2f619692007-11-16 10:46:05 +00005232 }
bellardf7341ff2003-03-30 21:00:25 +00005233 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00005234 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00005235 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00005236 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00005237 goto efault;
ths03aa1972007-12-02 06:28:08 +00005238 if (!addr)
bellard2f619692007-11-16 10:46:05 +00005239 break;
bellard7854b052003-03-29 17:22:23 +00005240 envc++;
bellard2f619692007-11-16 10:46:05 +00005241 }
bellard7854b052003-03-29 17:22:23 +00005242
bellardf7341ff2003-03-30 21:00:25 +00005243 argp = alloca((argc + 1) * sizeof(void *));
5244 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00005245
pbrookda94d262008-05-30 18:24:00 +00005246 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005247 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005248 if (get_user_ual(addr, gp))
5249 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005250 if (!addr)
5251 break;
bellard2f619692007-11-16 10:46:05 +00005252 if (!(*q = lock_user_string(addr)))
5253 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005254 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005255 }
bellardf7341ff2003-03-30 21:00:25 +00005256 *q = NULL;
5257
pbrookda94d262008-05-30 18:24:00 +00005258 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00005259 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005260 if (get_user_ual(addr, gp))
5261 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005262 if (!addr)
5263 break;
bellard2f619692007-11-16 10:46:05 +00005264 if (!(*q = lock_user_string(addr)))
5265 goto execve_efault;
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005266 total_size += strlen(*q) + 1;
pbrook53a59602006-03-25 19:31:22 +00005267 }
bellardf7341ff2003-03-30 21:00:25 +00005268 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00005269
Ulrich Hechta6f79cc2012-01-31 12:43:16 +01005270 /* This case will not be caught by the host's execve() if its
5271 page size is bigger than the target's. */
5272 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5273 ret = -TARGET_E2BIG;
5274 goto execve_end;
5275 }
bellard2f619692007-11-16 10:46:05 +00005276 if (!(p = lock_user_string(arg1)))
5277 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00005278 ret = get_errno(execve(p, argp, envp));
5279 unlock_user(p, arg1, 0);
5280
bellard2f619692007-11-16 10:46:05 +00005281 goto execve_end;
5282
5283 execve_efault:
5284 ret = -TARGET_EFAULT;
5285
5286 execve_end:
pbrook53a59602006-03-25 19:31:22 +00005287 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005288 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005289 if (get_user_ual(addr, gp)
5290 || !addr)
5291 break;
pbrook53a59602006-03-25 19:31:22 +00005292 unlock_user(*q, addr, 0);
5293 }
5294 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00005295 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00005296 if (get_user_ual(addr, gp)
5297 || !addr)
5298 break;
pbrook53a59602006-03-25 19:31:22 +00005299 unlock_user(*q, addr, 0);
5300 }
bellard7854b052003-03-29 17:22:23 +00005301 }
bellard31e31b82003-02-18 22:55:36 +00005302 break;
5303 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00005304 if (!(p = lock_user_string(arg1)))
5305 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005306 ret = get_errno(chdir(p));
5307 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005308 break;
bellarda315a142005-01-30 22:59:18 +00005309#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00005310 case TARGET_NR_time:
5311 {
pbrook53a59602006-03-25 19:31:22 +00005312 time_t host_time;
5313 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00005314 if (!is_error(ret)
5315 && arg1
5316 && put_user_sal(host_time, arg1))
5317 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005318 }
5319 break;
bellarda315a142005-01-30 22:59:18 +00005320#endif
bellard31e31b82003-02-18 22:55:36 +00005321 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00005322 if (!(p = lock_user_string(arg1)))
5323 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005324 ret = get_errno(mknod(p, arg2, arg3));
5325 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005326 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005327#if defined(TARGET_NR_mknodat)
ths75ac37a2007-09-24 09:23:05 +00005328 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00005329 if (!(p = lock_user_string(arg2)))
5330 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005331 ret = get_errno(mknodat(arg1, p, arg3, arg4));
bellard579a97f2007-11-11 14:26:47 +00005332 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00005333 break;
5334#endif
bellard31e31b82003-02-18 22:55:36 +00005335 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00005336 if (!(p = lock_user_string(arg1)))
5337 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005338 ret = get_errno(chmod(p, arg2));
5339 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005340 break;
bellardebc05482003-09-30 21:08:41 +00005341#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00005342 case TARGET_NR_break:
5343 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005344#endif
5345#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00005346 case TARGET_NR_oldstat:
5347 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005348#endif
bellard31e31b82003-02-18 22:55:36 +00005349 case TARGET_NR_lseek:
5350 ret = get_errno(lseek(arg1, arg2, arg3));
5351 break;
Richard Henderson92317332010-05-03 10:07:53 -07005352#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5353 /* Alpha specific */
j_mayer7a3148a2007-04-05 07:13:51 +00005354 case TARGET_NR_getxpid:
Richard Henderson92317332010-05-03 10:07:53 -07005355 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
bellard31e31b82003-02-18 22:55:36 +00005356 ret = get_errno(getpid());
5357 break;
Richard Henderson92317332010-05-03 10:07:53 -07005358#endif
5359#ifdef TARGET_NR_getpid
5360 case TARGET_NR_getpid:
5361 ret = get_errno(getpid());
5362 break;
5363#endif
bellard31e31b82003-02-18 22:55:36 +00005364 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00005365 {
5366 /* need to look at the data field */
5367 void *p2, *p3;
5368 p = lock_user_string(arg1);
5369 p2 = lock_user_string(arg2);
5370 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00005371 if (!p || !p2 || !p3)
5372 ret = -TARGET_EFAULT;
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005373 else {
bellard579a97f2007-11-11 14:26:47 +00005374 /* FIXME - arg5 should be locked, but it isn't clear how to
5375 * do that since it's not guaranteed to be a NULL-terminated
5376 * string.
5377 */
Jan-Simon Möllerdab46402009-10-17 21:52:43 +03005378 if ( ! arg5 )
5379 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
5380 else
5381 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
5382 }
bellard579a97f2007-11-11 14:26:47 +00005383 unlock_user(p, arg1, 0);
5384 unlock_user(p2, arg2, 0);
5385 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00005386 break;
5387 }
thse5febef2007-04-01 18:31:35 +00005388#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00005389 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00005390 if (!(p = lock_user_string(arg1)))
5391 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005392 ret = get_errno(umount(p));
5393 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005394 break;
thse5febef2007-04-01 18:31:35 +00005395#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005396#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005397 case TARGET_NR_stime:
5398 {
pbrook53a59602006-03-25 19:31:22 +00005399 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00005400 if (get_user_sal(host_time, arg1))
5401 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005402 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00005403 }
5404 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005405#endif
bellard31e31b82003-02-18 22:55:36 +00005406 case TARGET_NR_ptrace:
5407 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00005408#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005409 case TARGET_NR_alarm:
5410 ret = alarm(arg1);
5411 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005412#endif
bellardebc05482003-09-30 21:08:41 +00005413#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00005414 case TARGET_NR_oldfstat:
5415 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005416#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005417#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005418 case TARGET_NR_pause:
5419 ret = get_errno(pause());
5420 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005421#endif
thse5febef2007-04-01 18:31:35 +00005422#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00005423 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00005424 {
pbrook53a59602006-03-25 19:31:22 +00005425 struct utimbuf tbuf, *host_tbuf;
5426 struct target_utimbuf *target_tbuf;
5427 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005428 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5429 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005430 tbuf.actime = tswapal(target_tbuf->actime);
5431 tbuf.modtime = tswapal(target_tbuf->modtime);
pbrook53a59602006-03-25 19:31:22 +00005432 unlock_user_struct(target_tbuf, arg2, 0);
5433 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00005434 } else {
pbrook53a59602006-03-25 19:31:22 +00005435 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00005436 }
bellard579a97f2007-11-11 14:26:47 +00005437 if (!(p = lock_user_string(arg1)))
5438 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005439 ret = get_errno(utime(p, host_tbuf));
5440 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00005441 }
5442 break;
thse5febef2007-04-01 18:31:35 +00005443#endif
bellard978a66f2004-12-06 22:58:05 +00005444 case TARGET_NR_utimes:
5445 {
bellard978a66f2004-12-06 22:58:05 +00005446 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00005447 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00005448 if (copy_from_user_timeval(&tv[0], arg2)
5449 || copy_from_user_timeval(&tv[1],
5450 arg2 + sizeof(struct target_timeval)))
5451 goto efault;
bellard978a66f2004-12-06 22:58:05 +00005452 tvp = tv;
5453 } else {
5454 tvp = NULL;
5455 }
bellard579a97f2007-11-11 14:26:47 +00005456 if (!(p = lock_user_string(arg1)))
5457 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005458 ret = get_errno(utimes(p, tvp));
5459 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00005460 }
5461 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005462#if defined(TARGET_NR_futimesat)
balrogac8a6552008-09-20 02:25:39 +00005463 case TARGET_NR_futimesat:
5464 {
5465 struct timeval *tvp, tv[2];
5466 if (arg3) {
5467 if (copy_from_user_timeval(&tv[0], arg3)
5468 || copy_from_user_timeval(&tv[1],
5469 arg3 + sizeof(struct target_timeval)))
5470 goto efault;
5471 tvp = tv;
5472 } else {
5473 tvp = NULL;
5474 }
5475 if (!(p = lock_user_string(arg2)))
5476 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005477 ret = get_errno(futimesat(arg1, path(p), tvp));
balrogac8a6552008-09-20 02:25:39 +00005478 unlock_user(p, arg2, 0);
5479 }
5480 break;
5481#endif
bellardebc05482003-09-30 21:08:41 +00005482#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00005483 case TARGET_NR_stty:
5484 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005485#endif
5486#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00005487 case TARGET_NR_gtty:
5488 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005489#endif
bellard31e31b82003-02-18 22:55:36 +00005490 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00005491 if (!(p = lock_user_string(arg1)))
5492 goto efault;
Ulrich Hecht719f9082009-07-03 17:09:29 +02005493 ret = get_errno(access(path(p), arg2));
pbrook53a59602006-03-25 19:31:22 +00005494 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005495 break;
ths92a34c12007-09-24 09:27:49 +00005496#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5497 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00005498 if (!(p = lock_user_string(arg2)))
5499 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005500 ret = get_errno(faccessat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00005501 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00005502 break;
5503#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005504#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005505 case TARGET_NR_nice:
5506 ret = get_errno(nice(arg1));
5507 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005508#endif
bellardebc05482003-09-30 21:08:41 +00005509#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00005510 case TARGET_NR_ftime:
5511 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005512#endif
bellard31e31b82003-02-18 22:55:36 +00005513 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00005514 sync();
5515 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00005516 break;
5517 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00005518 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00005519 break;
5520 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00005521 {
5522 void *p2;
5523 p = lock_user_string(arg1);
5524 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00005525 if (!p || !p2)
5526 ret = -TARGET_EFAULT;
5527 else
5528 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00005529 unlock_user(p2, arg2, 0);
5530 unlock_user(p, arg1, 0);
5531 }
bellard31e31b82003-02-18 22:55:36 +00005532 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005533#if defined(TARGET_NR_renameat)
ths722183f2007-09-24 09:24:37 +00005534 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00005535 {
bellard579a97f2007-11-11 14:26:47 +00005536 void *p2;
ths722183f2007-09-24 09:24:37 +00005537 p = lock_user_string(arg2);
5538 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00005539 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00005540 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00005541 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01005542 ret = get_errno(renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00005543 unlock_user(p2, arg4, 0);
5544 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00005545 }
5546 break;
5547#endif
bellard31e31b82003-02-18 22:55:36 +00005548 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00005549 if (!(p = lock_user_string(arg1)))
5550 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005551 ret = get_errno(mkdir(p, arg2));
5552 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005553 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005554#if defined(TARGET_NR_mkdirat)
ths4472ad02007-09-24 09:22:32 +00005555 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00005556 if (!(p = lock_user_string(arg2)))
5557 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01005558 ret = get_errno(mkdirat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00005559 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00005560 break;
5561#endif
bellard31e31b82003-02-18 22:55:36 +00005562 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00005563 if (!(p = lock_user_string(arg1)))
5564 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005565 ret = get_errno(rmdir(p));
5566 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005567 break;
5568 case TARGET_NR_dup:
5569 ret = get_errno(dup(arg1));
5570 break;
5571 case TARGET_NR_pipe:
Richard Hendersonfb41a662010-05-03 10:07:52 -07005572 ret = do_pipe(cpu_env, arg1, 0, 0);
bellard31e31b82003-02-18 22:55:36 +00005573 break;
Riku Voipio099d6b02009-05-05 12:10:04 +03005574#ifdef TARGET_NR_pipe2
5575 case TARGET_NR_pipe2:
Richard Hendersone7ea6cb2012-06-01 18:48:39 -07005576 ret = do_pipe(cpu_env, arg1,
5577 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
Riku Voipio099d6b02009-05-05 12:10:04 +03005578 break;
5579#endif
bellard31e31b82003-02-18 22:55:36 +00005580 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00005581 {
pbrook53a59602006-03-25 19:31:22 +00005582 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00005583 struct tms tms;
5584 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00005585 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00005586 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5587 if (!tmsp)
5588 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02005589 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5590 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5591 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5592 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00005593 }
bellardc596ed12003-07-13 17:32:31 +00005594 if (!is_error(ret))
5595 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00005596 }
5597 break;
bellardebc05482003-09-30 21:08:41 +00005598#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00005599 case TARGET_NR_prof:
5600 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005601#endif
thse5febef2007-04-01 18:31:35 +00005602#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00005603 case TARGET_NR_signal:
5604 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005605#endif
bellard31e31b82003-02-18 22:55:36 +00005606 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00005607 if (arg1 == 0) {
5608 ret = get_errno(acct(NULL));
5609 } else {
5610 if (!(p = lock_user_string(arg1)))
5611 goto efault;
5612 ret = get_errno(acct(path(p)));
5613 unlock_user(p, arg1, 0);
5614 }
pbrook24836682006-04-16 14:14:53 +00005615 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005616#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005617 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00005618 if (!(p = lock_user_string(arg1)))
5619 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005620 ret = get_errno(umount2(p, arg2));
5621 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005622 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005623#endif
bellardebc05482003-09-30 21:08:41 +00005624#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00005625 case TARGET_NR_lock:
5626 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005627#endif
bellard31e31b82003-02-18 22:55:36 +00005628 case TARGET_NR_ioctl:
5629 ret = do_ioctl(arg1, arg2, arg3);
5630 break;
5631 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00005632 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00005633 break;
bellardebc05482003-09-30 21:08:41 +00005634#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00005635 case TARGET_NR_mpx:
5636 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005637#endif
bellard31e31b82003-02-18 22:55:36 +00005638 case TARGET_NR_setpgid:
5639 ret = get_errno(setpgid(arg1, arg2));
5640 break;
bellardebc05482003-09-30 21:08:41 +00005641#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00005642 case TARGET_NR_ulimit:
5643 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005644#endif
5645#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00005646 case TARGET_NR_oldolduname:
5647 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005648#endif
bellard31e31b82003-02-18 22:55:36 +00005649 case TARGET_NR_umask:
5650 ret = get_errno(umask(arg1));
5651 break;
5652 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00005653 if (!(p = lock_user_string(arg1)))
5654 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005655 ret = get_errno(chroot(p));
5656 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005657 break;
5658 case TARGET_NR_ustat:
5659 goto unimplemented;
5660 case TARGET_NR_dup2:
5661 ret = get_errno(dup2(arg1, arg2));
5662 break;
Ulrich Hechtd0927932009-09-17 20:22:14 +03005663#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5664 case TARGET_NR_dup3:
5665 ret = get_errno(dup3(arg1, arg2, arg3));
5666 break;
5667#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005668#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005669 case TARGET_NR_getppid:
5670 ret = get_errno(getppid());
5671 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005672#endif
bellard31e31b82003-02-18 22:55:36 +00005673 case TARGET_NR_getpgrp:
5674 ret = get_errno(getpgrp());
5675 break;
5676 case TARGET_NR_setsid:
5677 ret = get_errno(setsid());
5678 break;
thse5febef2007-04-01 18:31:35 +00005679#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00005680 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00005681 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005682#if defined(TARGET_ALPHA)
5683 struct target_sigaction act, oact, *pact = 0;
pbrook53a59602006-03-25 19:31:22 +00005684 struct target_old_sigaction *old_act;
pbrook53a59602006-03-25 19:31:22 +00005685 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005686 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5687 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005688 act._sa_handler = old_act->_sa_handler;
5689 target_siginitset(&act.sa_mask, old_act->sa_mask);
5690 act.sa_flags = old_act->sa_flags;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005691 act.sa_restorer = 0;
pbrook53a59602006-03-25 19:31:22 +00005692 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005693 pact = &act;
bellard66fb9762003-03-23 01:06:05 +00005694 }
5695 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00005696 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005697 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5698 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005699 old_act->_sa_handler = oact._sa_handler;
5700 old_act->sa_mask = oact.sa_mask.sig[0];
5701 old_act->sa_flags = oact.sa_flags;
pbrook53a59602006-03-25 19:31:22 +00005702 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00005703 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005704#elif defined(TARGET_MIPS)
bellard106ec872006-06-27 21:08:10 +00005705 struct target_sigaction act, oact, *pact, *old_act;
5706
5707 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00005708 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5709 goto efault;
bellard106ec872006-06-27 21:08:10 +00005710 act._sa_handler = old_act->_sa_handler;
5711 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5712 act.sa_flags = old_act->sa_flags;
5713 unlock_user_struct(old_act, arg2, 0);
5714 pact = &act;
5715 } else {
5716 pact = NULL;
5717 }
5718
5719 ret = get_errno(do_sigaction(arg1, pact, &oact));
5720
5721 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00005722 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5723 goto efault;
bellard106ec872006-06-27 21:08:10 +00005724 old_act->_sa_handler = oact._sa_handler;
5725 old_act->sa_flags = oact.sa_flags;
5726 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5727 old_act->sa_mask.sig[1] = 0;
5728 old_act->sa_mask.sig[2] = 0;
5729 old_act->sa_mask.sig[3] = 0;
5730 unlock_user_struct(old_act, arg3, 1);
5731 }
Richard Henderson6049f4f2009-12-27 18:30:03 -08005732#else
5733 struct target_old_sigaction *old_act;
5734 struct target_sigaction act, oact, *pact;
5735 if (arg2) {
5736 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5737 goto efault;
5738 act._sa_handler = old_act->_sa_handler;
5739 target_siginitset(&act.sa_mask, old_act->sa_mask);
5740 act.sa_flags = old_act->sa_flags;
5741 act.sa_restorer = old_act->sa_restorer;
5742 unlock_user_struct(old_act, arg2, 0);
5743 pact = &act;
5744 } else {
5745 pact = NULL;
5746 }
5747 ret = get_errno(do_sigaction(arg1, pact, &oact));
5748 if (!is_error(ret) && arg3) {
5749 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5750 goto efault;
5751 old_act->_sa_handler = oact._sa_handler;
5752 old_act->sa_mask = oact.sa_mask.sig[0];
5753 old_act->sa_flags = oact.sa_flags;
5754 old_act->sa_restorer = oact.sa_restorer;
5755 unlock_user_struct(old_act, arg3, 1);
5756 }
ths388bb212007-05-13 13:58:00 +00005757#endif
bellard31e31b82003-02-18 22:55:36 +00005758 }
5759 break;
thse5febef2007-04-01 18:31:35 +00005760#endif
bellard66fb9762003-03-23 01:06:05 +00005761 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00005762 {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005763#if defined(TARGET_ALPHA)
5764 struct target_sigaction act, oact, *pact = 0;
5765 struct target_rt_sigaction *rt_act;
5766 /* ??? arg4 == sizeof(sigset_t). */
5767 if (arg2) {
5768 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5769 goto efault;
5770 act._sa_handler = rt_act->_sa_handler;
5771 act.sa_mask = rt_act->sa_mask;
5772 act.sa_flags = rt_act->sa_flags;
5773 act.sa_restorer = arg5;
5774 unlock_user_struct(rt_act, arg2, 0);
5775 pact = &act;
5776 }
5777 ret = get_errno(do_sigaction(arg1, pact, &oact));
5778 if (!is_error(ret) && arg3) {
5779 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5780 goto efault;
5781 rt_act->_sa_handler = oact._sa_handler;
5782 rt_act->sa_mask = oact.sa_mask;
5783 rt_act->sa_flags = oact.sa_flags;
5784 unlock_user_struct(rt_act, arg3, 1);
5785 }
5786#else
pbrook53a59602006-03-25 19:31:22 +00005787 struct target_sigaction *act;
5788 struct target_sigaction *oact;
5789
bellard579a97f2007-11-11 14:26:47 +00005790 if (arg2) {
5791 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5792 goto efault;
5793 } else
pbrook53a59602006-03-25 19:31:22 +00005794 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00005795 if (arg3) {
5796 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5797 ret = -TARGET_EFAULT;
5798 goto rt_sigaction_fail;
5799 }
5800 } else
pbrook53a59602006-03-25 19:31:22 +00005801 oact = NULL;
5802 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00005803 rt_sigaction_fail:
5804 if (act)
pbrook53a59602006-03-25 19:31:22 +00005805 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00005806 if (oact)
pbrook53a59602006-03-25 19:31:22 +00005807 unlock_user_struct(oact, arg3, 1);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005808#endif
pbrook53a59602006-03-25 19:31:22 +00005809 }
bellard66fb9762003-03-23 01:06:05 +00005810 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005811#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005812 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00005813 {
5814 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005815 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00005816 sigprocmask(0, NULL, &cur_set);
5817 host_to_target_old_sigset(&target_set, &cur_set);
5818 ret = target_set;
5819 }
5820 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005821#endif
5822#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005823 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00005824 {
5825 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00005826 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00005827 sigprocmask(0, NULL, &cur_set);
5828 target_to_host_old_sigset(&set, &target_set);
5829 sigorset(&set, &set, &cur_set);
5830 sigprocmask(SIG_SETMASK, &set, &oset);
5831 host_to_target_old_sigset(&target_set, &oset);
5832 ret = target_set;
5833 }
5834 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005835#endif
thse5febef2007-04-01 18:31:35 +00005836#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00005837 case TARGET_NR_sigprocmask:
5838 {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005839#if defined(TARGET_ALPHA)
5840 sigset_t set, oldset;
5841 abi_ulong mask;
5842 int how;
5843
5844 switch (arg1) {
5845 case TARGET_SIG_BLOCK:
5846 how = SIG_BLOCK;
5847 break;
5848 case TARGET_SIG_UNBLOCK:
5849 how = SIG_UNBLOCK;
5850 break;
5851 case TARGET_SIG_SETMASK:
5852 how = SIG_SETMASK;
5853 break;
5854 default:
5855 ret = -TARGET_EINVAL;
5856 goto fail;
5857 }
5858 mask = arg2;
5859 target_to_host_old_sigset(&set, &mask);
5860
5861 ret = get_errno(sigprocmask(how, &set, &oldset));
Richard Hendersona5b3b132010-05-03 10:07:55 -07005862 if (!is_error(ret)) {
5863 host_to_target_old_sigset(&mask, &oldset);
5864 ret = mask;
Richard Henderson0229f5a2012-06-07 15:02:49 -07005865 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
Richard Hendersona5b3b132010-05-03 10:07:55 -07005866 }
5867#else
bellard66fb9762003-03-23 01:06:05 +00005868 sigset_t set, oldset, *set_ptr;
Richard Hendersona5b3b132010-05-03 10:07:55 -07005869 int how;
ths3b46e622007-09-17 08:09:54 +00005870
pbrook53a59602006-03-25 19:31:22 +00005871 if (arg2) {
Richard Hendersona5b3b132010-05-03 10:07:55 -07005872 switch (arg1) {
bellard66fb9762003-03-23 01:06:05 +00005873 case TARGET_SIG_BLOCK:
5874 how = SIG_BLOCK;
5875 break;
5876 case TARGET_SIG_UNBLOCK:
5877 how = SIG_UNBLOCK;
5878 break;
5879 case TARGET_SIG_SETMASK:
5880 how = SIG_SETMASK;
5881 break;
5882 default:
ths0da46a62007-10-20 20:23:07 +00005883 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005884 goto fail;
5885 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005886 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005887 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005888 target_to_host_old_sigset(&set, p);
5889 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005890 set_ptr = &set;
5891 } else {
5892 how = 0;
5893 set_ptr = NULL;
5894 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005895 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005896 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005897 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005898 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005899 host_to_target_old_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005900 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005901 }
Richard Hendersona5b3b132010-05-03 10:07:55 -07005902#endif
bellard66fb9762003-03-23 01:06:05 +00005903 }
5904 break;
thse5febef2007-04-01 18:31:35 +00005905#endif
bellard66fb9762003-03-23 01:06:05 +00005906 case TARGET_NR_rt_sigprocmask:
5907 {
5908 int how = arg1;
5909 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00005910
pbrook53a59602006-03-25 19:31:22 +00005911 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005912 switch(how) {
5913 case TARGET_SIG_BLOCK:
5914 how = SIG_BLOCK;
5915 break;
5916 case TARGET_SIG_UNBLOCK:
5917 how = SIG_UNBLOCK;
5918 break;
5919 case TARGET_SIG_SETMASK:
5920 how = SIG_SETMASK;
5921 break;
5922 default:
ths0da46a62007-10-20 20:23:07 +00005923 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00005924 goto fail;
5925 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005926 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005927 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005928 target_to_host_sigset(&set, p);
5929 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00005930 set_ptr = &set;
5931 } else {
5932 how = 0;
5933 set_ptr = NULL;
5934 }
5935 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00005936 if (!is_error(ret) && arg3) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005937 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005938 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005939 host_to_target_sigset(p, &oldset);
Anthony Liguoric227f092009-10-01 16:12:16 -05005940 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005941 }
5942 }
5943 break;
thse5febef2007-04-01 18:31:35 +00005944#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00005945 case TARGET_NR_sigpending:
5946 {
5947 sigset_t set;
5948 ret = get_errno(sigpending(&set));
5949 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005950 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005951 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005952 host_to_target_old_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005953 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005954 }
5955 }
5956 break;
thse5febef2007-04-01 18:31:35 +00005957#endif
bellard66fb9762003-03-23 01:06:05 +00005958 case TARGET_NR_rt_sigpending:
5959 {
5960 sigset_t set;
5961 ret = get_errno(sigpending(&set));
5962 if (!is_error(ret)) {
Anthony Liguoric227f092009-10-01 16:12:16 -05005963 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00005964 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005965 host_to_target_sigset(p, &set);
Anthony Liguoric227f092009-10-01 16:12:16 -05005966 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00005967 }
5968 }
5969 break;
thse5febef2007-04-01 18:31:35 +00005970#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00005971 case TARGET_NR_sigsuspend:
5972 {
5973 sigset_t set;
Richard Hendersonf43ce122010-05-03 10:07:54 -07005974#if defined(TARGET_ALPHA)
5975 abi_ulong mask = arg1;
5976 target_to_host_old_sigset(&set, &mask);
5977#else
Anthony Liguoric227f092009-10-01 16:12:16 -05005978 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005979 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005980 target_to_host_old_sigset(&set, p);
5981 unlock_user(p, arg1, 0);
Richard Hendersonf43ce122010-05-03 10:07:54 -07005982#endif
bellard66fb9762003-03-23 01:06:05 +00005983 ret = get_errno(sigsuspend(&set));
5984 }
5985 break;
thse5febef2007-04-01 18:31:35 +00005986#endif
bellard66fb9762003-03-23 01:06:05 +00005987 case TARGET_NR_rt_sigsuspend:
5988 {
5989 sigset_t set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005990 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00005991 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005992 target_to_host_sigset(&set, p);
5993 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00005994 ret = get_errno(sigsuspend(&set));
5995 }
5996 break;
5997 case TARGET_NR_rt_sigtimedwait:
5998 {
bellard66fb9762003-03-23 01:06:05 +00005999 sigset_t set;
6000 struct timespec uts, *puts;
6001 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00006002
Anthony Liguoric227f092009-10-01 16:12:16 -05006003 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006004 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006005 target_to_host_sigset(&set, p);
6006 unlock_user(p, arg1, 0);
6007 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00006008 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00006009 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00006010 } else {
6011 puts = NULL;
6012 }
6013 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00006014 if (!is_error(ret) && arg2) {
Anthony Liguoric227f092009-10-01 16:12:16 -05006015 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00006016 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006017 host_to_target_siginfo(p, &uinfo);
Anthony Liguoric227f092009-10-01 16:12:16 -05006018 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00006019 }
6020 }
6021 break;
6022 case TARGET_NR_rt_sigqueueinfo:
6023 {
6024 siginfo_t uinfo;
Anthony Liguoric227f092009-10-01 16:12:16 -05006025 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
bellard579a97f2007-11-11 14:26:47 +00006026 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006027 target_to_host_siginfo(&uinfo, p);
6028 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00006029 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6030 }
6031 break;
thse5febef2007-04-01 18:31:35 +00006032#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00006033 case TARGET_NR_sigreturn:
6034 /* NOTE: ret is eax, so not transcoding must be done */
6035 ret = do_sigreturn(cpu_env);
6036 break;
thse5febef2007-04-01 18:31:35 +00006037#endif
bellard66fb9762003-03-23 01:06:05 +00006038 case TARGET_NR_rt_sigreturn:
6039 /* NOTE: ret is eax, so not transcoding must be done */
6040 ret = do_rt_sigreturn(cpu_env);
6041 break;
bellard31e31b82003-02-18 22:55:36 +00006042 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00006043 if (!(p = lock_user_string(arg1)))
6044 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006045 ret = get_errno(sethostname(p, arg2));
6046 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006047 break;
6048 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00006049 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006050 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006051 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006052 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00006053 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6054 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006055 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6056 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006057 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00006058 ret = get_errno(setrlimit(resource, &rlim));
6059 }
6060 break;
bellard31e31b82003-02-18 22:55:36 +00006061 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00006062 {
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03006063 int resource = target_to_host_resource(arg1);
pbrook53a59602006-03-25 19:31:22 +00006064 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00006065 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00006066
bellard9de5e442003-03-23 16:49:39 +00006067 ret = get_errno(getrlimit(resource, &rlim));
6068 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006069 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6070 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09006071 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6072 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00006073 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00006074 }
6075 }
6076 break;
bellard31e31b82003-02-18 22:55:36 +00006077 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00006078 {
6079 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00006080 ret = get_errno(getrusage(arg1, &rusage));
6081 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006082 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00006083 }
6084 }
6085 break;
bellard31e31b82003-02-18 22:55:36 +00006086 case TARGET_NR_gettimeofday:
6087 {
bellard31e31b82003-02-18 22:55:36 +00006088 struct timeval tv;
6089 ret = get_errno(gettimeofday(&tv, NULL));
6090 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00006091 if (copy_to_user_timeval(arg1, &tv))
6092 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006093 }
6094 }
6095 break;
6096 case TARGET_NR_settimeofday:
6097 {
bellard31e31b82003-02-18 22:55:36 +00006098 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00006099 if (copy_from_user_timeval(&tv, arg1))
6100 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006101 ret = get_errno(settimeofday(&tv, NULL));
6102 }
6103 break;
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006104#if defined(TARGET_NR_select)
bellard31e31b82003-02-18 22:55:36 +00006105 case TARGET_NR_select:
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006106#if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6107 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6108#else
bellardf2674e32003-07-09 12:26:09 +00006109 {
pbrook53a59602006-03-25 19:31:22 +00006110 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00006111 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00006112 long nsel;
6113
bellard579a97f2007-11-11 14:26:47 +00006114 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6115 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006116 nsel = tswapal(sel->n);
6117 inp = tswapal(sel->inp);
6118 outp = tswapal(sel->outp);
6119 exp = tswapal(sel->exp);
6120 tvp = tswapal(sel->tvp);
pbrook53a59602006-03-25 19:31:22 +00006121 unlock_user_struct(sel, arg1, 0);
6122 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00006123 }
Laurent Vivier9468a5d2013-01-10 22:30:50 +01006124#endif
bellardf2674e32003-07-09 12:26:09 +00006125 break;
bellard048f6b42005-11-26 18:47:20 +00006126#endif
Riku Voipio9e423822010-05-07 12:28:05 +00006127#ifdef TARGET_NR_pselect6
6128 case TARGET_NR_pselect6:
Mike Frysinger055e0902011-06-03 17:01:49 -04006129 {
6130 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6131 fd_set rfds, wfds, efds;
6132 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6133 struct timespec ts, *ts_ptr;
6134
6135 /*
6136 * The 6th arg is actually two args smashed together,
6137 * so we cannot use the C library.
6138 */
6139 sigset_t set;
6140 struct {
6141 sigset_t *set;
6142 size_t size;
6143 } sig, *sig_ptr;
6144
6145 abi_ulong arg_sigset, arg_sigsize, *arg7;
6146 target_sigset_t *target_sigset;
6147
6148 n = arg1;
6149 rfd_addr = arg2;
6150 wfd_addr = arg3;
6151 efd_addr = arg4;
6152 ts_addr = arg5;
6153
6154 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6155 if (ret) {
6156 goto fail;
6157 }
6158 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6159 if (ret) {
6160 goto fail;
6161 }
6162 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6163 if (ret) {
6164 goto fail;
6165 }
6166
6167 /*
6168 * This takes a timespec, and not a timeval, so we cannot
6169 * use the do_select() helper ...
6170 */
6171 if (ts_addr) {
6172 if (target_to_host_timespec(&ts, ts_addr)) {
6173 goto efault;
6174 }
6175 ts_ptr = &ts;
6176 } else {
6177 ts_ptr = NULL;
6178 }
6179
6180 /* Extract the two packed args for the sigset */
6181 if (arg6) {
6182 sig_ptr = &sig;
6183 sig.size = _NSIG / 8;
6184
6185 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6186 if (!arg7) {
6187 goto efault;
6188 }
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006189 arg_sigset = tswapal(arg7[0]);
6190 arg_sigsize = tswapal(arg7[1]);
Mike Frysinger055e0902011-06-03 17:01:49 -04006191 unlock_user(arg7, arg6, 0);
6192
6193 if (arg_sigset) {
6194 sig.set = &set;
Peter Maydell8f04eeb2011-06-28 12:21:57 +01006195 if (arg_sigsize != sizeof(*target_sigset)) {
6196 /* Like the kernel, we enforce correct size sigsets */
6197 ret = -TARGET_EINVAL;
6198 goto fail;
6199 }
Mike Frysinger055e0902011-06-03 17:01:49 -04006200 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6201 sizeof(*target_sigset), 1);
6202 if (!target_sigset) {
6203 goto efault;
6204 }
6205 target_to_host_sigset(&set, target_sigset);
6206 unlock_user(target_sigset, arg_sigset, 0);
6207 } else {
6208 sig.set = NULL;
6209 }
6210 } else {
6211 sig_ptr = NULL;
6212 }
6213
6214 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6215 ts_ptr, sig_ptr));
6216
6217 if (!is_error(ret)) {
6218 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6219 goto efault;
6220 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6221 goto efault;
6222 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6223 goto efault;
6224
6225 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6226 goto efault;
6227 }
6228 }
6229 break;
Riku Voipio9e423822010-05-07 12:28:05 +00006230#endif
bellard31e31b82003-02-18 22:55:36 +00006231 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00006232 {
6233 void *p2;
6234 p = lock_user_string(arg1);
6235 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006236 if (!p || !p2)
6237 ret = -TARGET_EFAULT;
6238 else
6239 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00006240 unlock_user(p2, arg2, 0);
6241 unlock_user(p, arg1, 0);
6242 }
bellard31e31b82003-02-18 22:55:36 +00006243 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006244#if defined(TARGET_NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +00006245 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00006246 {
bellard579a97f2007-11-11 14:26:47 +00006247 void *p2;
thsf0b62432007-09-24 09:25:40 +00006248 p = lock_user_string(arg1);
6249 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00006250 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006251 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00006252 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01006253 ret = get_errno(symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00006254 unlock_user(p2, arg3, 0);
6255 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00006256 }
6257 break;
6258#endif
bellardebc05482003-09-30 21:08:41 +00006259#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00006260 case TARGET_NR_oldlstat:
6261 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006262#endif
bellard31e31b82003-02-18 22:55:36 +00006263 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00006264 {
aurel32d088d662009-01-30 20:09:01 +00006265 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00006266 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00006267 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
6268 if (!p || !p2)
6269 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00006270 else {
6271 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
6272 char real[PATH_MAX];
6273 temp = realpath(exec_path,real);
6274 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
6275 snprintf((char *)p2, arg3, "%s", real);
6276 }
6277 else
6278 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00006279 }
pbrook53a59602006-03-25 19:31:22 +00006280 unlock_user(p2, arg2, ret);
6281 unlock_user(p, arg1, 0);
6282 }
bellard31e31b82003-02-18 22:55:36 +00006283 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006284#if defined(TARGET_NR_readlinkat)
ths5e0ccb12007-09-24 09:26:10 +00006285 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00006286 {
bellard579a97f2007-11-11 14:26:47 +00006287 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00006288 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00006289 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
6290 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00006291 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00006292 else
Peter Maydellc0d472b2013-06-12 16:20:21 +01006293 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00006294 unlock_user(p2, arg3, ret);
6295 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00006296 }
6297 break;
6298#endif
thse5febef2007-04-01 18:31:35 +00006299#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00006300 case TARGET_NR_uselib:
6301 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006302#endif
6303#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00006304 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00006305 if (!(p = lock_user_string(arg1)))
6306 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006307 ret = get_errno(swapon(p, arg2));
6308 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006309 break;
thse5febef2007-04-01 18:31:35 +00006310#endif
bellard31e31b82003-02-18 22:55:36 +00006311 case TARGET_NR_reboot:
Laurent Vivierc07ecc62013-01-07 11:40:06 +00006312 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
6313 /* arg4 must be ignored in all other cases */
6314 p = lock_user_string(arg4);
6315 if (!p) {
6316 goto efault;
6317 }
6318 ret = get_errno(reboot(arg1, arg2, arg3, p));
6319 unlock_user(p, arg4, 0);
6320 } else {
6321 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
6322 }
Alexander Graf0f6b4d22011-09-27 14:39:42 +02006323 break;
thse5febef2007-04-01 18:31:35 +00006324#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00006325 case TARGET_NR_readdir:
6326 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006327#endif
6328#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00006329 case TARGET_NR_mmap:
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006330#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || \
6331 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6332 || defined(TARGET_S390X)
bellard31e31b82003-02-18 22:55:36 +00006333 {
blueswir1992f48a2007-10-14 16:27:31 +00006334 abi_ulong *v;
6335 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00006336 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6337 goto efault;
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006338 v1 = tswapal(v[0]);
6339 v2 = tswapal(v[1]);
6340 v3 = tswapal(v[2]);
6341 v4 = tswapal(v[3]);
6342 v5 = tswapal(v[4]);
6343 v6 = tswapal(v[5]);
pbrook53a59602006-03-25 19:31:22 +00006344 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00006345 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00006346 target_to_host_bitmask(v4, mmap_flags_tbl),
6347 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00006348 }
bellard31e31b82003-02-18 22:55:36 +00006349#else
ths5fafdf22007-09-16 21:08:06 +00006350 ret = get_errno(target_mmap(arg1, arg2, arg3,
6351 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00006352 arg5,
6353 arg6));
bellard31e31b82003-02-18 22:55:36 +00006354#endif
bellard6fb883e2003-07-09 17:12:39 +00006355 break;
thse5febef2007-04-01 18:31:35 +00006356#endif
bellarda315a142005-01-30 22:59:18 +00006357#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00006358 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00006359#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00006360#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00006361#endif
ths5fafdf22007-09-16 21:08:06 +00006362 ret = get_errno(target_mmap(arg1, arg2, arg3,
6363 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00006364 arg5,
bellardc573ff62004-01-04 15:51:36 +00006365 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00006366 break;
bellarda315a142005-01-30 22:59:18 +00006367#endif
bellard31e31b82003-02-18 22:55:36 +00006368 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00006369 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00006370 break;
bellard9de5e442003-03-23 16:49:39 +00006371 case TARGET_NR_mprotect:
Paul Brook97374d32010-06-16 13:03:51 +01006372 {
Andreas Färber9349b4f2012-03-14 01:38:32 +01006373 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
Paul Brook97374d32010-06-16 13:03:51 +01006374 /* Special hack to detect libc making the stack executable. */
6375 if ((arg3 & PROT_GROWSDOWN)
6376 && arg1 >= ts->info->stack_limit
6377 && arg1 <= ts->info->start_stack) {
6378 arg3 &= ~PROT_GROWSDOWN;
6379 arg2 = arg2 + arg1 - ts->info->stack_limit;
6380 arg1 = ts->info->stack_limit;
6381 }
6382 }
bellard54936002003-05-13 00:25:15 +00006383 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006384 break;
thse5febef2007-04-01 18:31:35 +00006385#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00006386 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00006387 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00006388 break;
thse5febef2007-04-01 18:31:35 +00006389#endif
pbrook53a59602006-03-25 19:31:22 +00006390 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00006391#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00006392 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00006393 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00006394 break;
thse5febef2007-04-01 18:31:35 +00006395#endif
6396#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00006397 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00006398 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006399 break;
thse5febef2007-04-01 18:31:35 +00006400#endif
6401#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00006402 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00006403 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00006404 break;
thse5febef2007-04-01 18:31:35 +00006405#endif
6406#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00006407 case TARGET_NR_mlockall:
6408 ret = get_errno(mlockall(arg1));
6409 break;
thse5febef2007-04-01 18:31:35 +00006410#endif
6411#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00006412 case TARGET_NR_munlockall:
6413 ret = get_errno(munlockall());
6414 break;
thse5febef2007-04-01 18:31:35 +00006415#endif
bellard31e31b82003-02-18 22:55:36 +00006416 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00006417 if (!(p = lock_user_string(arg1)))
6418 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006419 ret = get_errno(truncate(p, arg2));
6420 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006421 break;
6422 case TARGET_NR_ftruncate:
6423 ret = get_errno(ftruncate(arg1, arg2));
6424 break;
6425 case TARGET_NR_fchmod:
6426 ret = get_errno(fchmod(arg1, arg2));
6427 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006428#if defined(TARGET_NR_fchmodat)
ths814d7972007-09-24 09:26:51 +00006429 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00006430 if (!(p = lock_user_string(arg2)))
6431 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01006432 ret = get_errno(fchmodat(arg1, p, arg3, 0));
bellard579a97f2007-11-11 14:26:47 +00006433 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00006434 break;
6435#endif
bellard31e31b82003-02-18 22:55:36 +00006436 case TARGET_NR_getpriority:
Richard Henderson95c09822012-06-07 15:14:50 -07006437 /* Note that negative values are valid for getpriority, so we must
6438 differentiate based on errno settings. */
6439 errno = 0;
6440 ret = getpriority(arg1, arg2);
6441 if (ret == -1 && errno != 0) {
6442 ret = -host_to_target_errno(errno);
6443 break;
6444 }
6445#ifdef TARGET_ALPHA
6446 /* Return value is the unbiased priority. Signal no error. */
6447 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
6448#else
6449 /* Return value is a biased priority to avoid negative numbers. */
6450 ret = 20 - ret;
6451#endif
bellard31e31b82003-02-18 22:55:36 +00006452 break;
6453 case TARGET_NR_setpriority:
6454 ret = get_errno(setpriority(arg1, arg2, arg3));
6455 break;
bellardebc05482003-09-30 21:08:41 +00006456#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00006457 case TARGET_NR_profil:
6458 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006459#endif
bellard31e31b82003-02-18 22:55:36 +00006460 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00006461 if (!(p = lock_user_string(arg1)))
6462 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006463 ret = get_errno(statfs(path(p), &stfs));
6464 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006465 convert_statfs:
6466 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006467 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006468
bellard579a97f2007-11-11 14:26:47 +00006469 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6470 goto efault;
6471 __put_user(stfs.f_type, &target_stfs->f_type);
6472 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6473 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6474 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6475 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6476 __put_user(stfs.f_files, &target_stfs->f_files);
6477 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6478 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6479 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6480 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02006481 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6482 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
pbrook53a59602006-03-25 19:31:22 +00006483 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006484 }
6485 break;
6486 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00006487 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00006488 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00006489#ifdef TARGET_NR_statfs64
6490 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00006491 if (!(p = lock_user_string(arg1)))
6492 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006493 ret = get_errno(statfs(path(p), &stfs));
6494 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00006495 convert_statfs64:
6496 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006497 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00006498
bellard579a97f2007-11-11 14:26:47 +00006499 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6500 goto efault;
6501 __put_user(stfs.f_type, &target_stfs->f_type);
6502 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6503 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6504 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6505 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6506 __put_user(stfs.f_files, &target_stfs->f_files);
6507 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6508 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6509 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6510 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
Alexander Graf229d3372012-09-19 04:39:53 +02006511 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6512 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
bellard579a97f2007-11-11 14:26:47 +00006513 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00006514 }
6515 break;
6516 case TARGET_NR_fstatfs64:
6517 ret = get_errno(fstatfs(arg1, &stfs));
6518 goto convert_statfs64;
6519#endif
bellardebc05482003-09-30 21:08:41 +00006520#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00006521 case TARGET_NR_ioperm:
6522 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006523#endif
thse5febef2007-04-01 18:31:35 +00006524#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00006525 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00006526 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00006527 break;
thse5febef2007-04-01 18:31:35 +00006528#endif
bellard3532fa72006-06-24 15:06:03 +00006529#ifdef TARGET_NR_accept
6530 case TARGET_NR_accept:
Peter Maydella94b4982013-02-08 04:35:04 +00006531 ret = do_accept4(arg1, arg2, arg3, 0);
6532 break;
6533#endif
6534#ifdef TARGET_NR_accept4
6535 case TARGET_NR_accept4:
6536#ifdef CONFIG_ACCEPT4
6537 ret = do_accept4(arg1, arg2, arg3, arg4);
6538#else
6539 goto unimplemented;
6540#endif
bellard3532fa72006-06-24 15:06:03 +00006541 break;
6542#endif
6543#ifdef TARGET_NR_bind
6544 case TARGET_NR_bind:
6545 ret = do_bind(arg1, arg2, arg3);
6546 break;
6547#endif
6548#ifdef TARGET_NR_connect
6549 case TARGET_NR_connect:
6550 ret = do_connect(arg1, arg2, arg3);
6551 break;
6552#endif
6553#ifdef TARGET_NR_getpeername
6554 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00006555 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006556 break;
6557#endif
6558#ifdef TARGET_NR_getsockname
6559 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00006560 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00006561 break;
6562#endif
6563#ifdef TARGET_NR_getsockopt
6564 case TARGET_NR_getsockopt:
6565 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6566 break;
6567#endif
6568#ifdef TARGET_NR_listen
6569 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00006570 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006571 break;
6572#endif
6573#ifdef TARGET_NR_recv
6574 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00006575 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006576 break;
6577#endif
6578#ifdef TARGET_NR_recvfrom
6579 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00006580 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006581 break;
6582#endif
6583#ifdef TARGET_NR_recvmsg
6584 case TARGET_NR_recvmsg:
6585 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6586 break;
6587#endif
6588#ifdef TARGET_NR_send
6589 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00006590 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00006591 break;
6592#endif
6593#ifdef TARGET_NR_sendmsg
6594 case TARGET_NR_sendmsg:
6595 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6596 break;
6597#endif
6598#ifdef TARGET_NR_sendto
6599 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00006600 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00006601 break;
6602#endif
6603#ifdef TARGET_NR_shutdown
6604 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00006605 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00006606 break;
6607#endif
6608#ifdef TARGET_NR_socket
6609 case TARGET_NR_socket:
6610 ret = do_socket(arg1, arg2, arg3);
6611 break;
6612#endif
6613#ifdef TARGET_NR_socketpair
6614 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00006615 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00006616 break;
6617#endif
6618#ifdef TARGET_NR_setsockopt
6619 case TARGET_NR_setsockopt:
6620 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6621 break;
6622#endif
ths7494b0f2007-02-11 18:26:53 +00006623
bellard31e31b82003-02-18 22:55:36 +00006624 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00006625 if (!(p = lock_user_string(arg2)))
6626 goto efault;
thse5574482007-02-11 20:03:13 +00006627 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6628 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00006629 break;
6630
bellard31e31b82003-02-18 22:55:36 +00006631 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00006632 {
bellard66fb9762003-03-23 01:06:05 +00006633 struct itimerval value, ovalue, *pvalue;
6634
pbrook53a59602006-03-25 19:31:22 +00006635 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00006636 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00006637 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6638 || copy_from_user_timeval(&pvalue->it_value,
6639 arg2 + sizeof(struct target_timeval)))
6640 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006641 } else {
6642 pvalue = NULL;
6643 }
6644 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00006645 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00006646 if (copy_to_user_timeval(arg3,
6647 &ovalue.it_interval)
6648 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6649 &ovalue.it_value))
6650 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006651 }
6652 }
6653 break;
bellard31e31b82003-02-18 22:55:36 +00006654 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00006655 {
bellard66fb9762003-03-23 01:06:05 +00006656 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00006657
bellard66fb9762003-03-23 01:06:05 +00006658 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00006659 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00006660 if (copy_to_user_timeval(arg2,
6661 &value.it_interval)
6662 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6663 &value.it_value))
6664 goto efault;
bellard66fb9762003-03-23 01:06:05 +00006665 }
6666 }
6667 break;
bellard31e31b82003-02-18 22:55:36 +00006668 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00006669 if (!(p = lock_user_string(arg1)))
6670 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006671 ret = get_errno(stat(path(p), &st));
6672 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006673 goto do_stat;
6674 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00006675 if (!(p = lock_user_string(arg1)))
6676 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006677 ret = get_errno(lstat(path(p), &st));
6678 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006679 goto do_stat;
6680 case TARGET_NR_fstat:
6681 {
6682 ret = get_errno(fstat(arg1, &st));
6683 do_stat:
6684 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00006685 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00006686
bellard579a97f2007-11-11 14:26:47 +00006687 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6688 goto efault;
Ulrich Hecht12727912009-07-24 19:10:32 +02006689 memset(target_st, 0, sizeof(*target_st));
bellardd2fd1af2007-11-14 18:08:56 +00006690 __put_user(st.st_dev, &target_st->st_dev);
6691 __put_user(st.st_ino, &target_st->st_ino);
6692 __put_user(st.st_mode, &target_st->st_mode);
6693 __put_user(st.st_uid, &target_st->st_uid);
6694 __put_user(st.st_gid, &target_st->st_gid);
6695 __put_user(st.st_nlink, &target_st->st_nlink);
6696 __put_user(st.st_rdev, &target_st->st_rdev);
6697 __put_user(st.st_size, &target_st->st_size);
6698 __put_user(st.st_blksize, &target_st->st_blksize);
6699 __put_user(st.st_blocks, &target_st->st_blocks);
6700 __put_user(st.st_atime, &target_st->target_st_atime);
6701 __put_user(st.st_mtime, &target_st->target_st_mtime);
6702 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00006703 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00006704 }
6705 }
6706 break;
bellardebc05482003-09-30 21:08:41 +00006707#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00006708 case TARGET_NR_olduname:
6709 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006710#endif
6711#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00006712 case TARGET_NR_iopl:
6713 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006714#endif
bellard31e31b82003-02-18 22:55:36 +00006715 case TARGET_NR_vhangup:
6716 ret = get_errno(vhangup());
6717 break;
bellardebc05482003-09-30 21:08:41 +00006718#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00006719 case TARGET_NR_idle:
6720 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006721#endif
bellard42ad6ae2005-01-03 22:48:11 +00006722#ifdef TARGET_NR_syscall
6723 case TARGET_NR_syscall:
Peter Maydell5945cfc2011-06-16 17:37:13 +01006724 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6725 arg6, arg7, arg8, 0);
6726 break;
bellard42ad6ae2005-01-03 22:48:11 +00006727#endif
bellard31e31b82003-02-18 22:55:36 +00006728 case TARGET_NR_wait4:
6729 {
6730 int status;
blueswir1992f48a2007-10-14 16:27:31 +00006731 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00006732 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00006733 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00006734 if (target_rusage)
6735 rusage_ptr = &rusage;
6736 else
6737 rusage_ptr = NULL;
6738 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6739 if (!is_error(ret)) {
Alexander Graf53795572011-11-24 00:44:43 +01006740 if (status_ptr && ret) {
pbrook1d9d8b52009-04-16 15:17:02 +00006741 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00006742 if (put_user_s32(status, status_ptr))
6743 goto efault;
bellard31e31b82003-02-18 22:55:36 +00006744 }
bellard2f619692007-11-16 10:46:05 +00006745 if (target_rusage)
6746 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00006747 }
6748 }
6749 break;
thse5febef2007-04-01 18:31:35 +00006750#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00006751 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00006752 if (!(p = lock_user_string(arg1)))
6753 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006754 ret = get_errno(swapoff(p));
6755 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006756 break;
thse5febef2007-04-01 18:31:35 +00006757#endif
bellard31e31b82003-02-18 22:55:36 +00006758 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00006759 {
pbrook53a59602006-03-25 19:31:22 +00006760 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00006761 struct sysinfo value;
6762 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00006763 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00006764 {
bellard579a97f2007-11-11 14:26:47 +00006765 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6766 goto efault;
bellarda5448a72004-06-19 16:59:03 +00006767 __put_user(value.uptime, &target_value->uptime);
6768 __put_user(value.loads[0], &target_value->loads[0]);
6769 __put_user(value.loads[1], &target_value->loads[1]);
6770 __put_user(value.loads[2], &target_value->loads[2]);
6771 __put_user(value.totalram, &target_value->totalram);
6772 __put_user(value.freeram, &target_value->freeram);
6773 __put_user(value.sharedram, &target_value->sharedram);
6774 __put_user(value.bufferram, &target_value->bufferram);
6775 __put_user(value.totalswap, &target_value->totalswap);
6776 __put_user(value.freeswap, &target_value->freeswap);
6777 __put_user(value.procs, &target_value->procs);
6778 __put_user(value.totalhigh, &target_value->totalhigh);
6779 __put_user(value.freehigh, &target_value->freehigh);
6780 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00006781 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00006782 }
6783 }
6784 break;
thse5febef2007-04-01 18:31:35 +00006785#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00006786 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00006787 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6788 break;
thse5febef2007-04-01 18:31:35 +00006789#endif
aurel32e5289082009-04-18 16:16:12 +00006790#ifdef TARGET_NR_semget
6791 case TARGET_NR_semget:
6792 ret = get_errno(semget(arg1, arg2, arg3));
6793 break;
6794#endif
6795#ifdef TARGET_NR_semop
6796 case TARGET_NR_semop:
Petar Jovanovicc7128c92013-03-21 07:57:36 +00006797 ret = do_semop(arg1, arg2, arg3);
aurel32e5289082009-04-18 16:16:12 +00006798 break;
6799#endif
6800#ifdef TARGET_NR_semctl
6801 case TARGET_NR_semctl:
6802 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
6803 break;
6804#endif
aurel32eeb438c2008-10-13 21:08:55 +00006805#ifdef TARGET_NR_msgctl
6806 case TARGET_NR_msgctl:
6807 ret = do_msgctl(arg1, arg2, arg3);
6808 break;
6809#endif
6810#ifdef TARGET_NR_msgget
6811 case TARGET_NR_msgget:
6812 ret = get_errno(msgget(arg1, arg2));
6813 break;
6814#endif
6815#ifdef TARGET_NR_msgrcv
6816 case TARGET_NR_msgrcv:
6817 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
6818 break;
6819#endif
6820#ifdef TARGET_NR_msgsnd
6821 case TARGET_NR_msgsnd:
6822 ret = do_msgsnd(arg1, arg2, arg3, arg4);
6823 break;
6824#endif
Riku Voipio88a8c982009-04-03 10:42:00 +03006825#ifdef TARGET_NR_shmget
6826 case TARGET_NR_shmget:
6827 ret = get_errno(shmget(arg1, arg2, arg3));
6828 break;
6829#endif
6830#ifdef TARGET_NR_shmctl
6831 case TARGET_NR_shmctl:
6832 ret = do_shmctl(arg1, arg2, arg3);
6833 break;
6834#endif
6835#ifdef TARGET_NR_shmat
6836 case TARGET_NR_shmat:
6837 ret = do_shmat(arg1, arg2, arg3);
6838 break;
6839#endif
6840#ifdef TARGET_NR_shmdt
6841 case TARGET_NR_shmdt:
6842 ret = do_shmdt(arg1);
6843 break;
6844#endif
bellard31e31b82003-02-18 22:55:36 +00006845 case TARGET_NR_fsync:
6846 ret = get_errno(fsync(arg1));
6847 break;
bellard31e31b82003-02-18 22:55:36 +00006848 case TARGET_NR_clone:
Richard Hendersona4b388f2010-04-12 16:17:22 -07006849#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
aurel320b6d3ae2008-09-15 07:43:43 +00006850 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00006851#elif defined(TARGET_CRIS)
6852 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
Edgar E. Iglesiasa5b3bdc2012-04-26 14:17:41 +02006853#elif defined(TARGET_MICROBLAZE)
6854 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
Ulrich Hechta4c075f2009-07-24 16:57:31 +02006855#elif defined(TARGET_S390X)
6856 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
aurel320b6d3ae2008-09-15 07:43:43 +00006857#else
pbrookd865bab2008-06-07 22:12:17 +00006858 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00006859#endif
bellard1b6b0292003-03-22 17:31:38 +00006860 break;
bellardec86b0f2003-04-11 00:15:04 +00006861#ifdef __NR_exit_group
6862 /* new thread calls */
6863 case TARGET_NR_exit_group:
Juan Quintela9788c9c2009-07-27 16:13:02 +02006864#ifdef TARGET_GPROF
aurel326d946cd2008-11-06 16:15:18 +00006865 _mcleanup();
6866#endif
bellarde9009672005-04-26 20:42:36 +00006867 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00006868 ret = get_errno(exit_group(arg1));
6869 break;
6870#endif
bellard31e31b82003-02-18 22:55:36 +00006871 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00006872 if (!(p = lock_user_string(arg1)))
6873 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006874 ret = get_errno(setdomainname(p, arg2));
6875 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00006876 break;
6877 case TARGET_NR_uname:
6878 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00006879 {
6880 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00006881
bellard579a97f2007-11-11 14:26:47 +00006882 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
6883 goto efault;
bellard29e619b2004-09-13 21:41:04 +00006884 ret = get_errno(sys_uname(buf));
6885 if (!is_error(ret)) {
6886 /* Overrite the native machine name with whatever is being
6887 emulated. */
Loïc Minierda790302009-12-29 22:06:13 +01006888 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
pbrookc5937222006-05-14 11:30:38 +00006889 /* Allow the user to override the reported release. */
6890 if (qemu_uname_release && *qemu_uname_release)
6891 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00006892 }
pbrook53a59602006-03-25 19:31:22 +00006893 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00006894 }
bellard31e31b82003-02-18 22:55:36 +00006895 break;
bellard6dbad632003-03-16 18:05:05 +00006896#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00006897 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00006898 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00006899 break;
j_mayer84409dd2007-04-06 08:56:50 +00006900#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00006901 case TARGET_NR_vm86old:
6902 goto unimplemented;
6903 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00006904 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00006905 break;
6906#endif
j_mayer84409dd2007-04-06 08:56:50 +00006907#endif
bellard31e31b82003-02-18 22:55:36 +00006908 case TARGET_NR_adjtimex:
6909 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006910#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00006911 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00006912#endif
bellard31e31b82003-02-18 22:55:36 +00006913 case TARGET_NR_init_module:
6914 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00006915#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00006916 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00006917#endif
bellard31e31b82003-02-18 22:55:36 +00006918 goto unimplemented;
6919 case TARGET_NR_quotactl:
6920 goto unimplemented;
6921 case TARGET_NR_getpgid:
6922 ret = get_errno(getpgid(arg1));
6923 break;
6924 case TARGET_NR_fchdir:
6925 ret = get_errno(fchdir(arg1));
6926 break;
j_mayer84409dd2007-04-06 08:56:50 +00006927#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00006928 case TARGET_NR_bdflush:
6929 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00006930#endif
thse5febef2007-04-01 18:31:35 +00006931#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00006932 case TARGET_NR_sysfs:
6933 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006934#endif
bellard31e31b82003-02-18 22:55:36 +00006935 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00006936 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00006937 break;
thse5febef2007-04-01 18:31:35 +00006938#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00006939 case TARGET_NR_afs_syscall:
6940 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00006941#endif
j_mayer7a3148a2007-04-05 07:13:51 +00006942#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00006943 case TARGET_NR__llseek:
6944 {
6945 int64_t res;
Peter Maydell0c1592d2011-02-22 13:02:26 +00006946#if !defined(__NR_llseek)
6947 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
6948 if (res == -1) {
6949 ret = get_errno(res);
6950 } else {
6951 ret = 0;
6952 }
6953#else
bellard31e31b82003-02-18 22:55:36 +00006954 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard4f2ac232004-04-26 19:44:02 +00006955#endif
Peter Maydell0c1592d2011-02-22 13:02:26 +00006956 if ((ret == 0) && put_user_s64(res, arg4)) {
6957 goto efault;
6958 }
bellard31e31b82003-02-18 22:55:36 +00006959 }
6960 break;
j_mayer7a3148a2007-04-05 07:13:51 +00006961#endif
bellard31e31b82003-02-18 22:55:36 +00006962 case TARGET_NR_getdents:
Peter Maydell3307e232013-06-12 16:20:21 +01006963#ifdef __NR_getdents
Ulrich Hechtd83c8732009-07-24 19:10:28 +02006964#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00006965 {
pbrook53a59602006-03-25 19:31:22 +00006966 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00006967 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00006968 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00006969
6970 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00006971 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00006972 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00006973 goto fail;
6974 }
ths3b46e622007-09-17 08:09:54 +00006975
bellard4add45b2003-06-05 01:52:59 +00006976 ret = get_errno(sys_getdents(arg1, dirp, count));
6977 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00006978 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00006979 struct target_dirent *tde;
6980 int len = ret;
6981 int reclen, treclen;
6982 int count1, tnamelen;
6983
6984 count1 = 0;
6985 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00006986 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6987 goto efault;
bellard4add45b2003-06-05 01:52:59 +00006988 tde = target_dirp;
6989 while (len > 0) {
6990 reclen = de->d_reclen;
Dmitry V. Levin333858b2012-08-21 02:13:12 +04006991 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
6992 assert(tnamelen >= 0);
6993 treclen = tnamelen + offsetof(struct target_dirent, d_name);
6994 assert(count1 + treclen <= count);
bellard4add45b2003-06-05 01:52:59 +00006995 tde->d_reclen = tswap16(treclen);
Matthias Brauncbb21ee2011-08-12 19:57:41 +02006996 tde->d_ino = tswapal(de->d_ino);
6997 tde->d_off = tswapal(de->d_off);
Dmitry V. Levin333858b2012-08-21 02:13:12 +04006998 memcpy(tde->d_name, de->d_name, tnamelen);
aurel326556a832008-10-13 21:08:17 +00006999 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00007000 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00007001 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00007002 count1 += treclen;
7003 }
7004 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00007005 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00007006 }
7007 free(dirp);
7008 }
7009#else
bellard31e31b82003-02-18 22:55:36 +00007010 {
aurel326556a832008-10-13 21:08:17 +00007011 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007012 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00007013
bellard579a97f2007-11-11 14:26:47 +00007014 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7015 goto efault;
bellard72f03902003-02-18 23:33:18 +00007016 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00007017 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007018 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00007019 int len = ret;
7020 int reclen;
7021 de = dirp;
7022 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007023 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00007024 if (reclen > len)
7025 break;
bellard8083a3e2003-03-24 23:12:16 +00007026 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00007027 tswapls(&de->d_ino);
7028 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007029 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00007030 len -= reclen;
7031 }
7032 }
pbrook53a59602006-03-25 19:31:22 +00007033 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00007034 }
bellard4add45b2003-06-05 01:52:59 +00007035#endif
Peter Maydell3307e232013-06-12 16:20:21 +01007036#else
7037 /* Implement getdents in terms of getdents64 */
7038 {
7039 struct linux_dirent64 *dirp;
7040 abi_long count = arg3;
7041
7042 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
7043 if (!dirp) {
7044 goto efault;
7045 }
7046 ret = get_errno(sys_getdents64(arg1, dirp, count));
7047 if (!is_error(ret)) {
7048 /* Convert the dirent64 structs to target dirent. We do this
7049 * in-place, since we can guarantee that a target_dirent is no
7050 * larger than a dirent64; however this means we have to be
7051 * careful to read everything before writing in the new format.
7052 */
7053 struct linux_dirent64 *de;
7054 struct target_dirent *tde;
7055 int len = ret;
7056 int tlen = 0;
7057
7058 de = dirp;
7059 tde = (struct target_dirent *)dirp;
7060 while (len > 0) {
7061 int namelen, treclen;
7062 int reclen = de->d_reclen;
7063 uint64_t ino = de->d_ino;
7064 int64_t off = de->d_off;
7065 uint8_t type = de->d_type;
7066
7067 namelen = strlen(de->d_name);
7068 treclen = offsetof(struct target_dirent, d_name)
7069 + namelen + 2;
7070 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
7071
7072 memmove(tde->d_name, de->d_name, namelen + 1);
7073 tde->d_ino = tswapal(ino);
7074 tde->d_off = tswapal(off);
7075 tde->d_reclen = tswap16(treclen);
7076 /* The target_dirent type is in what was formerly a padding
7077 * byte at the end of the structure:
7078 */
7079 *(((char *)tde) + treclen - 1) = type;
7080
7081 de = (struct linux_dirent64 *)((char *)de + reclen);
7082 tde = (struct target_dirent *)((char *)tde + treclen);
7083 len -= reclen;
7084 tlen += treclen;
7085 }
7086 ret = tlen;
7087 }
7088 unlock_user(dirp, arg2, ret);
7089 }
7090#endif
bellard31e31b82003-02-18 22:55:36 +00007091 break;
ths3ae43202007-09-16 21:39:48 +00007092#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00007093 case TARGET_NR_getdents64:
7094 {
aurel326556a832008-10-13 21:08:17 +00007095 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00007096 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00007097 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7098 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00007099 ret = get_errno(sys_getdents64(arg1, dirp, count));
7100 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00007101 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00007102 int len = ret;
7103 int reclen;
7104 de = dirp;
7105 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00007106 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00007107 if (reclen > len)
7108 break;
bellard8083a3e2003-03-24 23:12:16 +00007109 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00007110 tswap64s((uint64_t *)&de->d_ino);
7111 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00007112 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00007113 len -= reclen;
7114 }
7115 }
pbrook53a59602006-03-25 19:31:22 +00007116 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00007117 }
7118 break;
bellarda541f292004-04-12 20:39:29 +00007119#endif /* TARGET_NR_getdents64 */
Laurent Vivier9468a5d2013-01-10 22:30:50 +01007120#if defined(TARGET_NR__newselect)
bellard31e31b82003-02-18 22:55:36 +00007121 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00007122 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00007123 break;
thse5febef2007-04-01 18:31:35 +00007124#endif
Mike Frysingerd8035d42011-02-07 01:05:51 -05007125#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7126# ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00007127 case TARGET_NR_poll:
Mike Frysingerd8035d42011-02-07 01:05:51 -05007128# endif
7129# ifdef TARGET_NR_ppoll
7130 case TARGET_NR_ppoll:
7131# endif
bellard9de5e442003-03-23 16:49:39 +00007132 {
pbrook53a59602006-03-25 19:31:22 +00007133 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00007134 unsigned int nfds = arg2;
7135 int timeout = arg3;
7136 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00007137 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00007138
bellard579a97f2007-11-11 14:26:47 +00007139 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7140 if (!target_pfd)
7141 goto efault;
Mike Frysingerd8035d42011-02-07 01:05:51 -05007142
bellard9de5e442003-03-23 16:49:39 +00007143 pfd = alloca(sizeof(struct pollfd) * nfds);
7144 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007145 pfd[i].fd = tswap32(target_pfd[i].fd);
7146 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00007147 }
Mike Frysingerd8035d42011-02-07 01:05:51 -05007148
7149# ifdef TARGET_NR_ppoll
7150 if (num == TARGET_NR_ppoll) {
7151 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7152 target_sigset_t *target_set;
7153 sigset_t _set, *set = &_set;
7154
7155 if (arg3) {
7156 if (target_to_host_timespec(timeout_ts, arg3)) {
7157 unlock_user(target_pfd, arg1, 0);
7158 goto efault;
7159 }
7160 } else {
7161 timeout_ts = NULL;
7162 }
7163
7164 if (arg4) {
7165 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7166 if (!target_set) {
7167 unlock_user(target_pfd, arg1, 0);
7168 goto efault;
7169 }
7170 target_to_host_sigset(set, target_set);
7171 } else {
7172 set = NULL;
7173 }
7174
7175 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7176
7177 if (!is_error(ret) && arg3) {
7178 host_to_target_timespec(arg3, timeout_ts);
7179 }
7180 if (arg4) {
7181 unlock_user(target_set, arg4, 0);
7182 }
7183 } else
7184# endif
7185 ret = get_errno(poll(pfd, nfds, timeout));
7186
bellard9de5e442003-03-23 16:49:39 +00007187 if (!is_error(ret)) {
7188 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00007189 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00007190 }
7191 }
Peter Maydell30cb4cd2011-02-25 10:27:40 +00007192 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
bellard9de5e442003-03-23 16:49:39 +00007193 }
7194 break;
thse5febef2007-04-01 18:31:35 +00007195#endif
bellard31e31b82003-02-18 22:55:36 +00007196 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00007197 /* NOTE: the flock constant seems to be the same for every
7198 Linux platform */
7199 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00007200 break;
7201 case TARGET_NR_readv:
7202 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07007203 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
7204 if (vec != NULL) {
7205 ret = get_errno(readv(arg1, vec, arg3));
7206 unlock_iovec(vec, arg2, arg3, 1);
7207 } else {
7208 ret = -host_to_target_errno(errno);
7209 }
bellard31e31b82003-02-18 22:55:36 +00007210 }
7211 break;
7212 case TARGET_NR_writev:
7213 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07007214 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
7215 if (vec != NULL) {
7216 ret = get_errno(writev(arg1, vec, arg3));
7217 unlock_iovec(vec, arg2, arg3, 0);
7218 } else {
7219 ret = -host_to_target_errno(errno);
7220 }
bellard31e31b82003-02-18 22:55:36 +00007221 }
7222 break;
7223 case TARGET_NR_getsid:
7224 ret = get_errno(getsid(arg1));
7225 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007226#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00007227 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00007228 ret = get_errno(fdatasync(arg1));
7229 break;
j_mayer7a3148a2007-04-05 07:13:51 +00007230#endif
bellard31e31b82003-02-18 22:55:36 +00007231 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00007232 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00007233 return value. */
ths0da46a62007-10-20 20:23:07 +00007234 ret = -TARGET_ENOTDIR;
7235 break;
Mike Frysinger737de1d2011-02-07 01:05:55 -05007236 case TARGET_NR_sched_getaffinity:
7237 {
7238 unsigned int mask_size;
7239 unsigned long *mask;
7240
7241 /*
7242 * sched_getaffinity needs multiples of ulong, so need to take
7243 * care of mismatches between target ulong and host ulong sizes.
7244 */
7245 if (arg2 & (sizeof(abi_ulong) - 1)) {
7246 ret = -TARGET_EINVAL;
7247 break;
7248 }
7249 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7250
7251 mask = alloca(mask_size);
7252 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7253
7254 if (!is_error(ret)) {
Mike McCormackcd18f052011-04-18 14:43:36 +09007255 if (copy_to_user(arg3, mask, ret)) {
Mike Frysinger737de1d2011-02-07 01:05:55 -05007256 goto efault;
7257 }
Mike Frysinger737de1d2011-02-07 01:05:55 -05007258 }
7259 }
7260 break;
7261 case TARGET_NR_sched_setaffinity:
7262 {
7263 unsigned int mask_size;
7264 unsigned long *mask;
7265
7266 /*
7267 * sched_setaffinity needs multiples of ulong, so need to take
7268 * care of mismatches between target ulong and host ulong sizes.
7269 */
7270 if (arg2 & (sizeof(abi_ulong) - 1)) {
7271 ret = -TARGET_EINVAL;
7272 break;
7273 }
7274 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7275
7276 mask = alloca(mask_size);
7277 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7278 goto efault;
7279 }
7280 memcpy(mask, p, arg2);
7281 unlock_user_struct(p, arg2, 0);
7282
7283 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7284 }
7285 break;
bellard31e31b82003-02-18 22:55:36 +00007286 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00007287 {
pbrook53a59602006-03-25 19:31:22 +00007288 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007289 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00007290
bellard579a97f2007-11-11 14:26:47 +00007291 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7292 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007293 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007294 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00007295 ret = get_errno(sched_setparam(arg1, &schp));
7296 }
7297 break;
bellard31e31b82003-02-18 22:55:36 +00007298 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00007299 {
pbrook53a59602006-03-25 19:31:22 +00007300 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007301 struct sched_param schp;
7302 ret = get_errno(sched_getparam(arg1, &schp));
7303 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007304 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7305 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007306 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007307 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00007308 }
7309 }
7310 break;
bellard31e31b82003-02-18 22:55:36 +00007311 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00007312 {
pbrook53a59602006-03-25 19:31:22 +00007313 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00007314 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00007315 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7316 goto efault;
bellard5cd43932003-03-29 16:54:36 +00007317 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00007318 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00007319 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7320 }
7321 break;
bellard31e31b82003-02-18 22:55:36 +00007322 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00007323 ret = get_errno(sched_getscheduler(arg1));
7324 break;
bellard31e31b82003-02-18 22:55:36 +00007325 case TARGET_NR_sched_yield:
7326 ret = get_errno(sched_yield());
7327 break;
7328 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00007329 ret = get_errno(sched_get_priority_max(arg1));
7330 break;
bellard31e31b82003-02-18 22:55:36 +00007331 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00007332 ret = get_errno(sched_get_priority_min(arg1));
7333 break;
bellard31e31b82003-02-18 22:55:36 +00007334 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00007335 {
bellard5cd43932003-03-29 16:54:36 +00007336 struct timespec ts;
7337 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7338 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007339 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00007340 }
7341 }
7342 break;
bellard31e31b82003-02-18 22:55:36 +00007343 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00007344 {
bellard1b6b0292003-03-22 17:31:38 +00007345 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00007346 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00007347 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00007348 if (is_error(ret) && arg2) {
7349 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00007350 }
7351 }
7352 break;
thse5febef2007-04-01 18:31:35 +00007353#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00007354 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00007355 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007356#endif
7357#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00007358 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00007359 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00007360#endif
bellard31e31b82003-02-18 22:55:36 +00007361 case TARGET_NR_prctl:
Peter Maydell1e6722f2012-02-03 14:48:03 +00007362 switch (arg1) {
7363 case PR_GET_PDEATHSIG:
7364 {
7365 int deathsig;
7366 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
7367 if (!is_error(ret) && arg2
7368 && put_user_ual(deathsig, arg2)) {
7369 goto efault;
thse5574482007-02-11 20:03:13 +00007370 }
Peter Maydell1e6722f2012-02-03 14:48:03 +00007371 break;
7372 }
Peter Maydelldb9526b2012-02-03 14:48:03 +00007373#ifdef PR_GET_NAME
7374 case PR_GET_NAME:
7375 {
7376 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
7377 if (!name) {
7378 goto efault;
7379 }
7380 ret = get_errno(prctl(arg1, (unsigned long)name,
7381 arg3, arg4, arg5));
7382 unlock_user(name, arg2, 16);
7383 break;
7384 }
7385 case PR_SET_NAME:
7386 {
7387 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
7388 if (!name) {
7389 goto efault;
7390 }
7391 ret = get_errno(prctl(arg1, (unsigned long)name,
7392 arg3, arg4, arg5));
7393 unlock_user(name, arg2, 0);
7394 break;
7395 }
7396#endif
Peter Maydell1e6722f2012-02-03 14:48:03 +00007397 default:
7398 /* Most prctl options have no pointer arguments */
7399 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7400 break;
7401 }
ths39b9aae2007-02-11 18:36:44 +00007402 break;
bellardd2fd1af2007-11-14 18:08:56 +00007403#ifdef TARGET_NR_arch_prctl
7404 case TARGET_NR_arch_prctl:
7405#if defined(TARGET_I386) && !defined(TARGET_ABI32)
7406 ret = do_arch_prctl(cpu_env, arg1, arg2);
7407 break;
7408#else
7409 goto unimplemented;
7410#endif
7411#endif
aurel32f2c7ba12008-03-28 22:32:06 +00007412#ifdef TARGET_NR_pread64
7413 case TARGET_NR_pread64:
Alexander Grafae017a52012-09-29 15:32:39 +00007414 if (regpairs_aligned(cpu_env)) {
7415 arg4 = arg5;
7416 arg5 = arg6;
7417 }
aurel32f2c7ba12008-03-28 22:32:06 +00007418 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7419 goto efault;
7420 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7421 unlock_user(p, arg2, ret);
7422 break;
7423 case TARGET_NR_pwrite64:
Alexander Grafae017a52012-09-29 15:32:39 +00007424 if (regpairs_aligned(cpu_env)) {
7425 arg4 = arg5;
7426 arg5 = arg6;
7427 }
aurel32f2c7ba12008-03-28 22:32:06 +00007428 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7429 goto efault;
7430 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7431 unlock_user(p, arg2, 0);
7432 break;
7433#endif
bellard31e31b82003-02-18 22:55:36 +00007434 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00007435 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7436 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007437 ret = get_errno(sys_getcwd1(p, arg2));
7438 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00007439 break;
7440 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00007441 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007442 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00007443 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00007444 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00007445#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
Laurent Vivierc761c152009-08-03 16:12:19 +02007446 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
Jia Liud9627832012-07-20 15:50:52 +08007447 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
Andreas Färber9349b4f2012-03-14 01:38:32 +01007448 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00007449 break;
7450#else
bellard5cd43932003-03-29 16:54:36 +00007451 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00007452#endif
Peter Maydella8fd1ab2013-02-08 07:31:55 +00007453
7454#ifdef CONFIG_SENDFILE
bellard31e31b82003-02-18 22:55:36 +00007455 case TARGET_NR_sendfile:
Peter Maydella8fd1ab2013-02-08 07:31:55 +00007456 {
7457 off_t *offp = NULL;
7458 off_t off;
7459 if (arg3) {
7460 ret = get_user_sal(off, arg3);
7461 if (is_error(ret)) {
7462 break;
7463 }
7464 offp = &off;
7465 }
7466 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7467 if (!is_error(ret) && arg3) {
7468 abi_long ret2 = put_user_sal(off, arg3);
7469 if (is_error(ret2)) {
7470 ret = ret2;
7471 }
7472 }
7473 break;
7474 }
7475#ifdef TARGET_NR_sendfile64
7476 case TARGET_NR_sendfile64:
7477 {
7478 off_t *offp = NULL;
7479 off_t off;
7480 if (arg3) {
7481 ret = get_user_s64(off, arg3);
7482 if (is_error(ret)) {
7483 break;
7484 }
7485 offp = &off;
7486 }
7487 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7488 if (!is_error(ret) && arg3) {
7489 abi_long ret2 = put_user_s64(off, arg3);
7490 if (is_error(ret2)) {
7491 ret = ret2;
7492 }
7493 }
7494 break;
7495 }
7496#endif
7497#else
7498 case TARGET_NR_sendfile:
Peter Maydell7edd2cf2013-04-21 13:30:03 +01007499#ifdef TARGET_NR_sendfile64
Peter Maydella8fd1ab2013-02-08 07:31:55 +00007500 case TARGET_NR_sendfile64:
7501#endif
bellard5cd43932003-03-29 16:54:36 +00007502 goto unimplemented;
Peter Maydella8fd1ab2013-02-08 07:31:55 +00007503#endif
7504
bellardebc05482003-09-30 21:08:41 +00007505#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00007506 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00007507 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007508#endif
7509#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00007510 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00007511 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00007512#endif
bellard048f6b42005-11-26 18:47:20 +00007513#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00007514 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00007515 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
7516 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00007517 break;
bellard048f6b42005-11-26 18:47:20 +00007518#endif
bellardebc05482003-09-30 21:08:41 +00007519#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00007520 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00007521 {
7522 struct rlimit rlim;
Wesley W. Terpstrae22b7012011-07-12 14:42:00 +03007523 int resource = target_to_host_resource(arg1);
7524 ret = get_errno(getrlimit(resource, &rlim));
bellard728584b2003-04-29 20:43:36 +00007525 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00007526 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00007527 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7528 goto efault;
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +09007529 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7530 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00007531 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00007532 }
7533 break;
7534 }
bellardebc05482003-09-30 21:08:41 +00007535#endif
bellarda315a142005-01-30 22:59:18 +00007536#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00007537 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00007538 if (!(p = lock_user_string(arg1)))
7539 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007540 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
7541 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00007542 break;
bellarda315a142005-01-30 22:59:18 +00007543#endif
7544#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00007545 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00007546 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00007547 break;
bellarda315a142005-01-30 22:59:18 +00007548#endif
7549#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00007550 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00007551 if (!(p = lock_user_string(arg1)))
7552 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007553 ret = get_errno(stat(path(p), &st));
7554 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007555 if (!is_error(ret))
7556 ret = host_to_target_stat64(cpu_env, arg2, &st);
7557 break;
bellarda315a142005-01-30 22:59:18 +00007558#endif
7559#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00007560 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00007561 if (!(p = lock_user_string(arg1)))
7562 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007563 ret = get_errno(lstat(path(p), &st));
7564 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00007565 if (!is_error(ret))
7566 ret = host_to_target_stat64(cpu_env, arg2, &st);
7567 break;
bellarda315a142005-01-30 22:59:18 +00007568#endif
7569#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00007570 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00007571 ret = get_errno(fstat(arg1, &st));
7572 if (!is_error(ret))
7573 ret = host_to_target_stat64(cpu_env, arg2, &st);
7574 break;
bellardec86b0f2003-04-11 00:15:04 +00007575#endif
Peter Maydellc0d472b2013-06-12 16:20:21 +01007576#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
aurel329d33b762009-04-08 23:07:05 +00007577#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00007578 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00007579#endif
7580#ifdef TARGET_NR_newfstatat
7581 case TARGET_NR_newfstatat:
7582#endif
balrog6a24a772008-09-20 02:23:36 +00007583 if (!(p = lock_user_string(arg2)))
7584 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007585 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
balrog6a24a772008-09-20 02:23:36 +00007586 if (!is_error(ret))
7587 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00007588 break;
bellarda315a142005-01-30 22:59:18 +00007589#endif
bellard67867302003-11-23 17:05:30 +00007590 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00007591 if (!(p = lock_user_string(arg1)))
7592 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007593 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7594 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007595 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007596#ifdef TARGET_NR_getuid
bellard67867302003-11-23 17:05:30 +00007597 case TARGET_NR_getuid:
7598 ret = get_errno(high2lowuid(getuid()));
7599 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007600#endif
7601#ifdef TARGET_NR_getgid
bellard67867302003-11-23 17:05:30 +00007602 case TARGET_NR_getgid:
7603 ret = get_errno(high2lowgid(getgid()));
7604 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007605#endif
7606#ifdef TARGET_NR_geteuid
bellard67867302003-11-23 17:05:30 +00007607 case TARGET_NR_geteuid:
7608 ret = get_errno(high2lowuid(geteuid()));
7609 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007610#endif
7611#ifdef TARGET_NR_getegid
bellard67867302003-11-23 17:05:30 +00007612 case TARGET_NR_getegid:
7613 ret = get_errno(high2lowgid(getegid()));
7614 break;
Riku Voipio0c866a72011-04-18 15:23:06 +03007615#endif
bellard67867302003-11-23 17:05:30 +00007616 case TARGET_NR_setreuid:
7617 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7618 break;
7619 case TARGET_NR_setregid:
7620 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7621 break;
7622 case TARGET_NR_getgroups:
7623 {
7624 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007625 target_id *target_grouplist;
bellard67867302003-11-23 17:05:30 +00007626 gid_t *grouplist;
7627 int i;
7628
7629 grouplist = alloca(gidsetsize * sizeof(gid_t));
7630 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007631 if (gidsetsize == 0)
7632 break;
bellard67867302003-11-23 17:05:30 +00007633 if (!is_error(ret)) {
Andreas Schwab03903ff2013-04-09 05:41:33 +00007634 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
bellard579a97f2007-11-11 14:26:47 +00007635 if (!target_grouplist)
7636 goto efault;
balroga2155fc2008-09-20 02:12:08 +00007637 for(i = 0;i < ret; i++)
Riku Voipio0c866a72011-04-18 15:23:06 +03007638 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
Andreas Schwab03903ff2013-04-09 05:41:33 +00007639 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
bellard67867302003-11-23 17:05:30 +00007640 }
7641 }
7642 break;
7643 case TARGET_NR_setgroups:
7644 {
7645 int gidsetsize = arg1;
Riku Voipio0c866a72011-04-18 15:23:06 +03007646 target_id *target_grouplist;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05007647 gid_t *grouplist = NULL;
bellard67867302003-11-23 17:05:30 +00007648 int i;
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05007649 if (gidsetsize) {
7650 grouplist = alloca(gidsetsize * sizeof(gid_t));
Andreas Schwab03903ff2013-04-09 05:41:33 +00007651 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
Dillon Amburgeyf2b79ce2013-02-02 18:04:48 -05007652 if (!target_grouplist) {
7653 ret = -TARGET_EFAULT;
7654 goto fail;
7655 }
7656 for (i = 0; i < gidsetsize; i++) {
7657 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
7658 }
7659 unlock_user(target_grouplist, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00007660 }
bellard67867302003-11-23 17:05:30 +00007661 ret = get_errno(setgroups(gidsetsize, grouplist));
7662 }
7663 break;
7664 case TARGET_NR_fchown:
7665 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7666 break;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007667#if defined(TARGET_NR_fchownat)
thsccfa72b2007-09-24 09:23:34 +00007668 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00007669 if (!(p = lock_user_string(arg2)))
7670 goto efault;
Peter Maydellc0d472b2013-06-12 16:20:21 +01007671 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
7672 low2highgid(arg4), arg5));
bellard579a97f2007-11-11 14:26:47 +00007673 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00007674 break;
7675#endif
bellard67867302003-11-23 17:05:30 +00007676#ifdef TARGET_NR_setresuid
7677 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00007678 ret = get_errno(setresuid(low2highuid(arg1),
7679 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00007680 low2highuid(arg3)));
7681 break;
7682#endif
7683#ifdef TARGET_NR_getresuid
7684 case TARGET_NR_getresuid:
7685 {
pbrook53a59602006-03-25 19:31:22 +00007686 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00007687 ret = get_errno(getresuid(&ruid, &euid, &suid));
7688 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007689 if (put_user_u16(high2lowuid(ruid), arg1)
7690 || put_user_u16(high2lowuid(euid), arg2)
7691 || put_user_u16(high2lowuid(suid), arg3))
7692 goto efault;
bellard67867302003-11-23 17:05:30 +00007693 }
7694 }
7695 break;
7696#endif
7697#ifdef TARGET_NR_getresgid
7698 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00007699 ret = get_errno(setresgid(low2highgid(arg1),
7700 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00007701 low2highgid(arg3)));
7702 break;
7703#endif
7704#ifdef TARGET_NR_getresgid
7705 case TARGET_NR_getresgid:
7706 {
pbrook53a59602006-03-25 19:31:22 +00007707 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00007708 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7709 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00007710 if (put_user_u16(high2lowgid(rgid), arg1)
7711 || put_user_u16(high2lowgid(egid), arg2)
7712 || put_user_u16(high2lowgid(sgid), arg3))
7713 goto efault;
bellard67867302003-11-23 17:05:30 +00007714 }
7715 }
7716 break;
7717#endif
7718 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00007719 if (!(p = lock_user_string(arg1)))
7720 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007721 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7722 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00007723 break;
7724 case TARGET_NR_setuid:
7725 ret = get_errno(setuid(low2highuid(arg1)));
7726 break;
7727 case TARGET_NR_setgid:
7728 ret = get_errno(setgid(low2highgid(arg1)));
7729 break;
7730 case TARGET_NR_setfsuid:
7731 ret = get_errno(setfsuid(arg1));
7732 break;
7733 case TARGET_NR_setfsgid:
7734 ret = get_errno(setfsgid(arg1));
7735 break;
bellard67867302003-11-23 17:05:30 +00007736
bellarda315a142005-01-30 22:59:18 +00007737#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00007738 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00007739 if (!(p = lock_user_string(arg1)))
7740 goto efault;
pbrook53a59602006-03-25 19:31:22 +00007741 ret = get_errno(lchown(p, arg2, arg3));
7742 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00007743 break;
bellarda315a142005-01-30 22:59:18 +00007744#endif
7745#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00007746 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00007747 ret = get_errno(getuid());
7748 break;
bellarda315a142005-01-30 22:59:18 +00007749#endif
aurel3264b4d282008-11-14 17:20:15 +00007750
7751#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7752 /* Alpha specific */
7753 case TARGET_NR_getxuid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007754 {
7755 uid_t euid;
7756 euid=geteuid();
7757 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7758 }
aurel3264b4d282008-11-14 17:20:15 +00007759 ret = get_errno(getuid());
7760 break;
7761#endif
7762#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7763 /* Alpha specific */
7764 case TARGET_NR_getxgid:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007765 {
7766 uid_t egid;
7767 egid=getegid();
7768 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7769 }
aurel3264b4d282008-11-14 17:20:15 +00007770 ret = get_errno(getgid());
7771 break;
7772#endif
Richard Hendersonba0e2762009-12-09 15:56:29 -08007773#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7774 /* Alpha specific */
7775 case TARGET_NR_osf_getsysinfo:
7776 ret = -TARGET_EOPNOTSUPP;
7777 switch (arg1) {
7778 case TARGET_GSI_IEEE_FP_CONTROL:
7779 {
7780 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7781
7782 /* Copied from linux ieee_fpcr_to_swcr. */
7783 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7784 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7785 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7786 | SWCR_TRAP_ENABLE_DZE
7787 | SWCR_TRAP_ENABLE_OVF);
7788 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
7789 | SWCR_TRAP_ENABLE_INE);
7790 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
7791 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
7792
7793 if (put_user_u64 (swcr, arg2))
7794 goto efault;
7795 ret = 0;
7796 }
7797 break;
7798
7799 /* case GSI_IEEE_STATE_AT_SIGNAL:
7800 -- Not implemented in linux kernel.
7801 case GSI_UACPROC:
7802 -- Retrieves current unaligned access state; not much used.
7803 case GSI_PROC_TYPE:
7804 -- Retrieves implver information; surely not used.
7805 case GSI_GET_HWRPB:
7806 -- Grabs a copy of the HWRPB; surely not used.
7807 */
7808 }
7809 break;
7810#endif
7811#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
7812 /* Alpha specific */
7813 case TARGET_NR_osf_setsysinfo:
7814 ret = -TARGET_EOPNOTSUPP;
7815 switch (arg1) {
7816 case TARGET_SSI_IEEE_FP_CONTROL:
Richard Hendersonba0e2762009-12-09 15:56:29 -08007817 {
7818 uint64_t swcr, fpcr, orig_fpcr;
7819
Richard Henderson6e06d512012-06-01 09:08:21 -07007820 if (get_user_u64 (swcr, arg2)) {
Richard Hendersonba0e2762009-12-09 15:56:29 -08007821 goto efault;
Richard Henderson6e06d512012-06-01 09:08:21 -07007822 }
7823 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007824 fpcr = orig_fpcr & FPCR_DYN_MASK;
7825
7826 /* Copied from linux ieee_swcr_to_fpcr. */
7827 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
7828 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
7829 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
7830 | SWCR_TRAP_ENABLE_DZE
7831 | SWCR_TRAP_ENABLE_OVF)) << 48;
7832 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
7833 | SWCR_TRAP_ENABLE_INE)) << 57;
7834 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
7835 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
7836
Richard Henderson6e06d512012-06-01 09:08:21 -07007837 cpu_alpha_store_fpcr(cpu_env, fpcr);
7838 ret = 0;
7839 }
7840 break;
7841
7842 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
7843 {
7844 uint64_t exc, fpcr, orig_fpcr;
7845 int si_code;
7846
7847 if (get_user_u64(exc, arg2)) {
7848 goto efault;
7849 }
7850
7851 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
7852
7853 /* We only add to the exception status here. */
7854 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
7855
7856 cpu_alpha_store_fpcr(cpu_env, fpcr);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007857 ret = 0;
7858
Richard Henderson6e06d512012-06-01 09:08:21 -07007859 /* Old exceptions are not signaled. */
7860 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007861
Richard Henderson6e06d512012-06-01 09:08:21 -07007862 /* If any exceptions set by this call,
7863 and are unmasked, send a signal. */
7864 si_code = 0;
7865 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
7866 si_code = TARGET_FPE_FLTRES;
7867 }
7868 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
7869 si_code = TARGET_FPE_FLTUND;
7870 }
7871 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
7872 si_code = TARGET_FPE_FLTOVF;
7873 }
7874 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
7875 si_code = TARGET_FPE_FLTDIV;
7876 }
7877 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
7878 si_code = TARGET_FPE_FLTINV;
7879 }
7880 if (si_code != 0) {
7881 target_siginfo_t info;
7882 info.si_signo = SIGFPE;
7883 info.si_errno = 0;
7884 info.si_code = si_code;
7885 info._sifields._sigfault._addr
7886 = ((CPUArchState *)cpu_env)->pc;
7887 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007888 }
7889 }
7890 break;
7891
7892 /* case SSI_NVPAIRS:
7893 -- Used with SSIN_UACPROC to enable unaligned accesses.
7894 case SSI_IEEE_STATE_AT_SIGNAL:
7895 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
7896 -- Not implemented in linux kernel
7897 */
7898 }
7899 break;
7900#endif
7901#ifdef TARGET_NR_osf_sigprocmask
7902 /* Alpha specific. */
7903 case TARGET_NR_osf_sigprocmask:
7904 {
7905 abi_ulong mask;
Juan Quintelabc088ba2011-06-16 17:37:10 +01007906 int how;
Richard Hendersonba0e2762009-12-09 15:56:29 -08007907 sigset_t set, oldset;
7908
7909 switch(arg1) {
7910 case TARGET_SIG_BLOCK:
7911 how = SIG_BLOCK;
7912 break;
7913 case TARGET_SIG_UNBLOCK:
7914 how = SIG_UNBLOCK;
7915 break;
7916 case TARGET_SIG_SETMASK:
7917 how = SIG_SETMASK;
7918 break;
7919 default:
7920 ret = -TARGET_EINVAL;
7921 goto fail;
7922 }
7923 mask = arg2;
7924 target_to_host_old_sigset(&set, &mask);
Juan Quintelabc088ba2011-06-16 17:37:10 +01007925 sigprocmask(how, &set, &oldset);
Richard Hendersonba0e2762009-12-09 15:56:29 -08007926 host_to_target_old_sigset(&mask, &oldset);
7927 ret = mask;
7928 }
7929 break;
7930#endif
aurel3264b4d282008-11-14 17:20:15 +00007931
bellarda315a142005-01-30 22:59:18 +00007932#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00007933 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00007934 ret = get_errno(getgid());
7935 break;
bellarda315a142005-01-30 22:59:18 +00007936#endif
7937#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00007938 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00007939 ret = get_errno(geteuid());
7940 break;
bellarda315a142005-01-30 22:59:18 +00007941#endif
7942#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00007943 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00007944 ret = get_errno(getegid());
7945 break;
bellarda315a142005-01-30 22:59:18 +00007946#endif
7947#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00007948 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00007949 ret = get_errno(setreuid(arg1, arg2));
7950 break;
bellarda315a142005-01-30 22:59:18 +00007951#endif
7952#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00007953 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00007954 ret = get_errno(setregid(arg1, arg2));
7955 break;
bellarda315a142005-01-30 22:59:18 +00007956#endif
7957#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00007958 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00007959 {
7960 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007961 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007962 gid_t *grouplist;
7963 int i;
7964
7965 grouplist = alloca(gidsetsize * sizeof(gid_t));
7966 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00007967 if (gidsetsize == 0)
7968 break;
bellard99c475a2005-01-31 20:45:13 +00007969 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00007970 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
7971 if (!target_grouplist) {
7972 ret = -TARGET_EFAULT;
7973 goto fail;
7974 }
balroga2155fc2008-09-20 02:12:08 +00007975 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00007976 target_grouplist[i] = tswap32(grouplist[i]);
7977 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00007978 }
7979 }
7980 break;
bellarda315a142005-01-30 22:59:18 +00007981#endif
7982#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00007983 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00007984 {
7985 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00007986 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00007987 gid_t *grouplist;
7988 int i;
ths3b46e622007-09-17 08:09:54 +00007989
bellard99c475a2005-01-31 20:45:13 +00007990 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00007991 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
7992 if (!target_grouplist) {
7993 ret = -TARGET_EFAULT;
7994 goto fail;
7995 }
bellard99c475a2005-01-31 20:45:13 +00007996 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00007997 grouplist[i] = tswap32(target_grouplist[i]);
7998 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00007999 ret = get_errno(setgroups(gidsetsize, grouplist));
8000 }
8001 break;
bellarda315a142005-01-30 22:59:18 +00008002#endif
8003#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00008004 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00008005 ret = get_errno(fchown(arg1, arg2, arg3));
8006 break;
bellarda315a142005-01-30 22:59:18 +00008007#endif
8008#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00008009 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00008010 ret = get_errno(setresuid(arg1, arg2, arg3));
8011 break;
bellarda315a142005-01-30 22:59:18 +00008012#endif
8013#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00008014 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00008015 {
pbrook53a59602006-03-25 19:31:22 +00008016 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00008017 ret = get_errno(getresuid(&ruid, &euid, &suid));
8018 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00008019 if (put_user_u32(ruid, arg1)
8020 || put_user_u32(euid, arg2)
8021 || put_user_u32(suid, arg3))
8022 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00008023 }
8024 }
8025 break;
bellarda315a142005-01-30 22:59:18 +00008026#endif
8027#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00008028 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00008029 ret = get_errno(setresgid(arg1, arg2, arg3));
8030 break;
bellarda315a142005-01-30 22:59:18 +00008031#endif
8032#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00008033 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00008034 {
pbrook53a59602006-03-25 19:31:22 +00008035 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00008036 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8037 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00008038 if (put_user_u32(rgid, arg1)
8039 || put_user_u32(egid, arg2)
8040 || put_user_u32(sgid, arg3))
8041 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00008042 }
8043 }
8044 break;
bellarda315a142005-01-30 22:59:18 +00008045#endif
8046#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00008047 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00008048 if (!(p = lock_user_string(arg1)))
8049 goto efault;
pbrook53a59602006-03-25 19:31:22 +00008050 ret = get_errno(chown(p, arg2, arg3));
8051 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00008052 break;
bellarda315a142005-01-30 22:59:18 +00008053#endif
8054#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00008055 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00008056 ret = get_errno(setuid(arg1));
8057 break;
bellarda315a142005-01-30 22:59:18 +00008058#endif
8059#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00008060 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00008061 ret = get_errno(setgid(arg1));
8062 break;
bellarda315a142005-01-30 22:59:18 +00008063#endif
8064#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00008065 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00008066 ret = get_errno(setfsuid(arg1));
8067 break;
bellarda315a142005-01-30 22:59:18 +00008068#endif
8069#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00008070 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00008071 ret = get_errno(setfsgid(arg1));
8072 break;
bellarda315a142005-01-30 22:59:18 +00008073#endif
bellard67867302003-11-23 17:05:30 +00008074
bellard31e31b82003-02-18 22:55:36 +00008075 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00008076 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00008077#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00008078 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00008079 {
8080 void *a;
8081 ret = -TARGET_EFAULT;
8082 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8083 goto efault;
8084 if (!(p = lock_user_string(arg3)))
8085 goto mincore_fail;
8086 ret = get_errno(mincore(a, arg2, p));
8087 unlock_user(p, arg3, ret);
8088 mincore_fail:
8089 unlock_user(a, arg1, 0);
8090 }
8091 break;
bellardffa65c32004-01-04 23:57:22 +00008092#endif
aurel32408321b2008-10-01 21:46:32 +00008093#ifdef TARGET_NR_arm_fadvise64_64
8094 case TARGET_NR_arm_fadvise64_64:
8095 {
8096 /*
8097 * arm_fadvise64_64 looks like fadvise64_64 but
8098 * with different argument order
8099 */
8100 abi_long temp;
8101 temp = arg3;
8102 arg3 = arg4;
8103 arg4 = temp;
8104 }
8105#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008106#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
aurel32408321b2008-10-01 21:46:32 +00008107#ifdef TARGET_NR_fadvise64_64
8108 case TARGET_NR_fadvise64_64:
8109#endif
Ulrich Hechte72d2cc2009-07-24 19:10:31 +02008110#ifdef TARGET_NR_fadvise64
8111 case TARGET_NR_fadvise64:
8112#endif
8113#ifdef TARGET_S390X
8114 switch (arg4) {
8115 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8116 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8117 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8118 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8119 default: break;
8120 }
8121#endif
8122 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
aurel32408321b2008-10-01 21:46:32 +00008123 break;
8124#endif
bellardffa65c32004-01-04 23:57:22 +00008125#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00008126 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00008127 /* A straight passthrough may not be safe because qemu sometimes
Lei Lid2d6b852013-05-20 17:20:50 +08008128 turns private file-backed mappings into anonymous mappings.
pbrook24836682006-04-16 14:14:53 +00008129 This will break MADV_DONTNEED.
8130 This is a hint, so ignoring and returning success is ok. */
8131 ret = get_errno(0);
8132 break;
bellardffa65c32004-01-04 23:57:22 +00008133#endif
blueswir1992f48a2007-10-14 16:27:31 +00008134#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00008135 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00008136 {
thsb1e341e2007-03-20 21:50:52 +00008137 int cmd;
bellard77e46722003-04-29 20:39:06 +00008138 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00008139 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00008140#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00008141 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00008142#endif
bellard77e46722003-04-29 20:39:06 +00008143
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008144 cmd = target_to_host_fcntl_cmd(arg2);
Peter Maydell31b63192011-12-05 23:11:50 +00008145 if (cmd == -TARGET_EINVAL) {
8146 ret = cmd;
8147 break;
8148 }
thsb1e341e2007-03-20 21:50:52 +00008149
bellard60cd49d2003-03-16 22:53:56 +00008150 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00008151 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00008152#ifdef TARGET_ARM
8153 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008154 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8155 goto efault;
ths58134272007-03-31 18:59:32 +00008156 fl.l_type = tswap16(target_efl->l_type);
8157 fl.l_whence = tswap16(target_efl->l_whence);
8158 fl.l_start = tswap64(target_efl->l_start);
8159 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008160 fl.l_pid = tswap32(target_efl->l_pid);
ths58134272007-03-31 18:59:32 +00008161 unlock_user_struct(target_efl, arg3, 0);
8162 } else
8163#endif
8164 {
bellard9ee1fa22007-11-11 15:11:19 +00008165 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8166 goto efault;
ths58134272007-03-31 18:59:32 +00008167 fl.l_type = tswap16(target_fl->l_type);
8168 fl.l_whence = tswap16(target_fl->l_whence);
8169 fl.l_start = tswap64(target_fl->l_start);
8170 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008171 fl.l_pid = tswap32(target_fl->l_pid);
ths58134272007-03-31 18:59:32 +00008172 unlock_user_struct(target_fl, arg3, 0);
8173 }
thsb1e341e2007-03-20 21:50:52 +00008174 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008175 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00008176#ifdef TARGET_ARM
8177 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008178 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8179 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008180 target_efl->l_type = tswap16(fl.l_type);
8181 target_efl->l_whence = tswap16(fl.l_whence);
8182 target_efl->l_start = tswap64(fl.l_start);
8183 target_efl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008184 target_efl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008185 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008186 } else
8187#endif
8188 {
bellard9ee1fa22007-11-11 15:11:19 +00008189 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8190 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008191 target_fl->l_type = tswap16(fl.l_type);
8192 target_fl->l_whence = tswap16(fl.l_whence);
8193 target_fl->l_start = tswap64(fl.l_start);
8194 target_fl->l_len = tswap64(fl.l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008195 target_fl->l_pid = tswap32(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00008196 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00008197 }
bellard77e46722003-04-29 20:39:06 +00008198 }
8199 break;
8200
thsb1e341e2007-03-20 21:50:52 +00008201 case TARGET_F_SETLK64:
8202 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00008203#ifdef TARGET_ARM
8204 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00008205 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8206 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008207 fl.l_type = tswap16(target_efl->l_type);
8208 fl.l_whence = tswap16(target_efl->l_whence);
8209 fl.l_start = tswap64(target_efl->l_start);
8210 fl.l_len = tswap64(target_efl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008211 fl.l_pid = tswap32(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008212 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008213 } else
8214#endif
8215 {
bellard9ee1fa22007-11-11 15:11:19 +00008216 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8217 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00008218 fl.l_type = tswap16(target_fl->l_type);
8219 fl.l_whence = tswap16(target_fl->l_whence);
8220 fl.l_start = tswap64(target_fl->l_start);
8221 fl.l_len = tswap64(target_fl->l_len);
Ulrich Hecht7e22e542009-07-24 19:10:27 +02008222 fl.l_pid = tswap32(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00008223 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00008224 }
thsb1e341e2007-03-20 21:50:52 +00008225 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00008226 break;
bellard60cd49d2003-03-16 22:53:56 +00008227 default:
Arnaud Patard (Rtp)5f106812009-06-03 14:35:04 +02008228 ret = do_fcntl(arg1, arg2, arg3);
bellard60cd49d2003-03-16 22:53:56 +00008229 break;
8230 }
bellard77e46722003-04-29 20:39:06 +00008231 break;
8232 }
bellard60cd49d2003-03-16 22:53:56 +00008233#endif
ths7d600c82006-12-08 01:32:58 +00008234#ifdef TARGET_NR_cacheflush
8235 case TARGET_NR_cacheflush:
8236 /* self-modifying code is handled automatically, so nothing needed */
8237 ret = 0;
8238 break;
8239#endif
bellardebc05482003-09-30 21:08:41 +00008240#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00008241 case TARGET_NR_security:
8242 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00008243#endif
bellardc573ff62004-01-04 15:51:36 +00008244#ifdef TARGET_NR_getpagesize
8245 case TARGET_NR_getpagesize:
8246 ret = TARGET_PAGE_SIZE;
8247 break;
8248#endif
bellard31e31b82003-02-18 22:55:36 +00008249 case TARGET_NR_gettid:
8250 ret = get_errno(gettid());
8251 break;
thse5febef2007-04-01 18:31:35 +00008252#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00008253 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00008254#if TARGET_ABI_BITS == 32
Riku Voipio48e515d2011-07-12 15:40:51 +03008255 if (regpairs_aligned(cpu_env)) {
aurel322054ac92008-10-13 21:08:07 +00008256 arg2 = arg3;
8257 arg3 = arg4;
8258 arg4 = arg5;
8259 }
aurel322054ac92008-10-13 21:08:07 +00008260 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8261#else
8262 ret = get_errno(readahead(arg1, arg2, arg3));
8263#endif
8264 break;
thse5febef2007-04-01 18:31:35 +00008265#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008266#ifdef CONFIG_ATTR
bellardebc05482003-09-30 21:08:41 +00008267#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00008268 case TARGET_NR_listxattr:
8269 case TARGET_NR_llistxattr:
Peter Maydellfb5590f2011-12-14 15:37:19 +00008270 {
8271 void *p, *b = 0;
8272 if (arg2) {
8273 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8274 if (!b) {
8275 ret = -TARGET_EFAULT;
8276 break;
8277 }
8278 }
8279 p = lock_user_string(arg1);
8280 if (p) {
8281 if (num == TARGET_NR_listxattr) {
8282 ret = get_errno(listxattr(p, b, arg3));
8283 } else {
8284 ret = get_errno(llistxattr(p, b, arg3));
8285 }
8286 } else {
8287 ret = -TARGET_EFAULT;
8288 }
8289 unlock_user(p, arg1, 0);
8290 unlock_user(b, arg2, arg3);
Arnaud Patard6f932f92009-04-21 21:04:18 +03008291 break;
Peter Maydellfb5590f2011-12-14 15:37:19 +00008292 }
8293 case TARGET_NR_flistxattr:
8294 {
8295 void *b = 0;
8296 if (arg2) {
8297 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8298 if (!b) {
8299 ret = -TARGET_EFAULT;
8300 break;
8301 }
8302 }
8303 ret = get_errno(flistxattr(arg1, b, arg3));
8304 unlock_user(b, arg2, arg3);
8305 break;
8306 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008307 case TARGET_NR_setxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008308 case TARGET_NR_lsetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008309 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008310 void *p, *n, *v = 0;
8311 if (arg3) {
8312 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8313 if (!v) {
8314 ret = -TARGET_EFAULT;
8315 break;
8316 }
8317 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008318 p = lock_user_string(arg1);
8319 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008320 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008321 if (num == TARGET_NR_setxattr) {
8322 ret = get_errno(setxattr(p, n, v, arg4, arg5));
8323 } else {
8324 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
8325 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008326 } else {
8327 ret = -TARGET_EFAULT;
8328 }
8329 unlock_user(p, arg1, 0);
8330 unlock_user(n, arg2, 0);
8331 unlock_user(v, arg3, 0);
8332 }
8333 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008334 case TARGET_NR_fsetxattr:
8335 {
8336 void *n, *v = 0;
8337 if (arg3) {
8338 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8339 if (!v) {
8340 ret = -TARGET_EFAULT;
8341 break;
8342 }
8343 }
8344 n = lock_user_string(arg2);
8345 if (n) {
8346 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
8347 } else {
8348 ret = -TARGET_EFAULT;
8349 }
8350 unlock_user(n, arg2, 0);
8351 unlock_user(v, arg3, 0);
8352 }
8353 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008354 case TARGET_NR_getxattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008355 case TARGET_NR_lgetxattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008356 {
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008357 void *p, *n, *v = 0;
8358 if (arg3) {
8359 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8360 if (!v) {
8361 ret = -TARGET_EFAULT;
8362 break;
8363 }
8364 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008365 p = lock_user_string(arg1);
8366 n = lock_user_string(arg2);
Peter Maydelle3c33ec2011-12-14 15:37:17 +00008367 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008368 if (num == TARGET_NR_getxattr) {
8369 ret = get_errno(getxattr(p, n, v, arg4));
8370 } else {
8371 ret = get_errno(lgetxattr(p, n, v, arg4));
8372 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008373 } else {
8374 ret = -TARGET_EFAULT;
8375 }
8376 unlock_user(p, arg1, 0);
8377 unlock_user(n, arg2, 0);
8378 unlock_user(v, arg3, arg4);
8379 }
8380 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008381 case TARGET_NR_fgetxattr:
8382 {
8383 void *n, *v = 0;
8384 if (arg3) {
8385 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8386 if (!v) {
8387 ret = -TARGET_EFAULT;
8388 break;
8389 }
8390 }
8391 n = lock_user_string(arg2);
8392 if (n) {
8393 ret = get_errno(fgetxattr(arg1, n, v, arg4));
8394 } else {
8395 ret = -TARGET_EFAULT;
8396 }
8397 unlock_user(n, arg2, 0);
8398 unlock_user(v, arg3, arg4);
8399 }
8400 break;
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008401 case TARGET_NR_removexattr:
Peter Maydell30297b52011-12-14 15:37:18 +00008402 case TARGET_NR_lremovexattr:
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008403 {
8404 void *p, *n;
8405 p = lock_user_string(arg1);
8406 n = lock_user_string(arg2);
8407 if (p && n) {
Peter Maydell30297b52011-12-14 15:37:18 +00008408 if (num == TARGET_NR_removexattr) {
8409 ret = get_errno(removexattr(p, n));
8410 } else {
8411 ret = get_errno(lremovexattr(p, n));
8412 }
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008413 } else {
8414 ret = -TARGET_EFAULT;
8415 }
8416 unlock_user(p, arg1, 0);
8417 unlock_user(n, arg2, 0);
8418 }
8419 break;
Peter Maydell30297b52011-12-14 15:37:18 +00008420 case TARGET_NR_fremovexattr:
8421 {
8422 void *n;
8423 n = lock_user_string(arg2);
8424 if (n) {
8425 ret = get_errno(fremovexattr(arg1, n));
8426 } else {
8427 ret = -TARGET_EFAULT;
8428 }
8429 unlock_user(n, arg2, 0);
8430 }
8431 break;
bellardebc05482003-09-30 21:08:41 +00008432#endif
An-Cheng Huanga790ae32011-08-09 12:34:06 -07008433#endif /* CONFIG_ATTR */
bellardebc05482003-09-30 21:08:41 +00008434#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00008435 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008436#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00008437 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
8438 ret = 0;
8439 break;
edgar_iglef967792009-01-07 14:19:38 +00008440#elif defined(TARGET_CRIS)
8441 if (arg1 & 0xff)
8442 ret = -TARGET_EINVAL;
8443 else {
8444 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
8445 ret = 0;
8446 }
8447 break;
bellard8d18e892007-11-14 15:18:40 +00008448#elif defined(TARGET_I386) && defined(TARGET_ABI32)
8449 ret = do_set_thread_area(cpu_env, arg1);
8450 break;
ths6f5b89a2007-03-02 20:48:00 +00008451#else
8452 goto unimplemented_nowarn;
8453#endif
8454#endif
8455#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00008456 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00008457#if defined(TARGET_I386) && defined(TARGET_ABI32)
8458 ret = do_get_thread_area(cpu_env, arg1);
8459#else
bellard5cd43932003-03-29 16:54:36 +00008460 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00008461#endif
bellard8d18e892007-11-14 15:18:40 +00008462#endif
bellard48dc41e2006-06-21 18:15:50 +00008463#ifdef TARGET_NR_getdomainname
8464 case TARGET_NR_getdomainname:
8465 goto unimplemented_nowarn;
8466#endif
ths6f5b89a2007-03-02 20:48:00 +00008467
thsb5906f92007-03-19 13:32:45 +00008468#ifdef TARGET_NR_clock_gettime
8469 case TARGET_NR_clock_gettime:
8470 {
8471 struct timespec ts;
8472 ret = get_errno(clock_gettime(arg1, &ts));
8473 if (!is_error(ret)) {
8474 host_to_target_timespec(arg2, &ts);
8475 }
8476 break;
8477 }
8478#endif
8479#ifdef TARGET_NR_clock_getres
8480 case TARGET_NR_clock_getres:
8481 {
8482 struct timespec ts;
8483 ret = get_errno(clock_getres(arg1, &ts));
8484 if (!is_error(ret)) {
8485 host_to_target_timespec(arg2, &ts);
8486 }
8487 break;
8488 }
8489#endif
pbrook63d76512008-05-29 13:43:29 +00008490#ifdef TARGET_NR_clock_nanosleep
8491 case TARGET_NR_clock_nanosleep:
8492 {
8493 struct timespec ts;
8494 target_to_host_timespec(&ts, arg3);
8495 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
8496 if (arg4)
8497 host_to_target_timespec(arg4, &ts);
8498 break;
8499 }
8500#endif
thsb5906f92007-03-19 13:32:45 +00008501
ths6f5b89a2007-03-02 20:48:00 +00008502#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8503 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00008504 ret = get_errno(set_tid_address((int *)g2h(arg1)));
8505 break;
ths6f5b89a2007-03-02 20:48:00 +00008506#endif
8507
ths3ae43202007-09-16 21:39:48 +00008508#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00008509 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00008510 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00008511 break;
8512#endif
8513
ths3ae43202007-09-16 21:39:48 +00008514#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00008515 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00008516 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
8517 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00008518 break;
8519#endif
8520
ths4f2b1fe2007-06-21 21:57:12 +00008521#ifdef TARGET_NR_set_robust_list
8522 case TARGET_NR_set_robust_list:
Peter Maydelle9a970a2013-02-08 04:34:54 +00008523 case TARGET_NR_get_robust_list:
8524 /* The ABI for supporting robust futexes has userspace pass
8525 * the kernel a pointer to a linked list which is updated by
8526 * userspace after the syscall; the list is walked by the kernel
8527 * when the thread exits. Since the linked list in QEMU guest
8528 * memory isn't a valid linked list for the host and we have
8529 * no way to reliably intercept the thread-death event, we can't
8530 * support these. Silently return ENOSYS so that guest userspace
8531 * falls back to a non-robust futex implementation (which should
8532 * be OK except in the corner case of the guest crashing while
8533 * holding a mutex that is shared with another process via
8534 * shared memory).
8535 */
8536 goto unimplemented_nowarn;
ths4f2b1fe2007-06-21 21:57:12 +00008537#endif
8538
ths9007f0e2007-09-25 17:50:37 +00008539#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
8540 case TARGET_NR_utimensat:
8541 {
Riku Voipioebc996f2009-04-21 15:01:51 +03008542 struct timespec *tsp, ts[2];
8543 if (!arg3) {
8544 tsp = NULL;
8545 } else {
8546 target_to_host_timespec(ts, arg3);
8547 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
8548 tsp = ts;
8549 }
ths9007f0e2007-09-25 17:50:37 +00008550 if (!arg2)
Riku Voipioebc996f2009-04-21 15:01:51 +03008551 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
ths9007f0e2007-09-25 17:50:37 +00008552 else {
bellard579a97f2007-11-11 14:26:47 +00008553 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00008554 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00008555 goto fail;
8556 }
Riku Voipioebc996f2009-04-21 15:01:51 +03008557 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
bellard579a97f2007-11-11 14:26:47 +00008558 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00008559 }
8560 }
8561 break;
8562#endif
Juan Quintela2f7bb872009-07-27 16:13:24 +02008563#if defined(CONFIG_USE_NPTL)
pbrookbd0c5662008-05-29 14:34:11 +00008564 case TARGET_NR_futex:
8565 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
8566 break;
8567#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008568#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00008569 case TARGET_NR_inotify_init:
8570 ret = get_errno(sys_inotify_init());
8571 break;
8572#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008573#ifdef CONFIG_INOTIFY1
Riku Voipioc05c7a72010-03-26 15:25:11 +00008574#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8575 case TARGET_NR_inotify_init1:
8576 ret = get_errno(sys_inotify_init1(arg1));
8577 break;
8578#endif
Stefan Weila1606b02010-03-28 11:44:41 +02008579#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008580#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00008581 case TARGET_NR_inotify_add_watch:
8582 p = lock_user_string(arg2);
8583 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
8584 unlock_user(p, arg2, 0);
8585 break;
8586#endif
aurel32dbfe4c32009-04-08 21:29:30 +00008587#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00008588 case TARGET_NR_inotify_rm_watch:
8589 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
8590 break;
8591#endif
ths9007f0e2007-09-25 17:50:37 +00008592
Nathan Froyd8ec9cf82009-07-22 09:14:36 -07008593#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
aurel3224e10032009-04-15 16:11:43 +00008594 case TARGET_NR_mq_open:
8595 {
8596 struct mq_attr posix_mq_attr;
8597
8598 p = lock_user_string(arg1 - 1);
8599 if (arg4 != 0)
8600 copy_from_user_mq_attr (&posix_mq_attr, arg4);
8601 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
8602 unlock_user (p, arg1, 0);
8603 }
8604 break;
8605
8606 case TARGET_NR_mq_unlink:
8607 p = lock_user_string(arg1 - 1);
8608 ret = get_errno(mq_unlink(p));
8609 unlock_user (p, arg1, 0);
8610 break;
8611
8612 case TARGET_NR_mq_timedsend:
8613 {
8614 struct timespec ts;
8615
8616 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8617 if (arg5 != 0) {
8618 target_to_host_timespec(&ts, arg5);
8619 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
8620 host_to_target_timespec(arg5, &ts);
8621 }
8622 else
8623 ret = get_errno(mq_send(arg1, p, arg3, arg4));
8624 unlock_user (p, arg2, arg3);
8625 }
8626 break;
8627
8628 case TARGET_NR_mq_timedreceive:
8629 {
8630 struct timespec ts;
8631 unsigned int prio;
8632
8633 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8634 if (arg5 != 0) {
8635 target_to_host_timespec(&ts, arg5);
8636 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
8637 host_to_target_timespec(arg5, &ts);
8638 }
8639 else
8640 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
8641 unlock_user (p, arg2, arg3);
8642 if (arg4 != 0)
8643 put_user_u32(prio, arg4);
8644 }
8645 break;
8646
8647 /* Not implemented for now... */
8648/* case TARGET_NR_mq_notify: */
8649/* break; */
8650
8651 case TARGET_NR_mq_getsetattr:
8652 {
8653 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
8654 ret = 0;
8655 if (arg3 != 0) {
8656 ret = mq_getattr(arg1, &posix_mq_attr_out);
8657 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
8658 }
8659 if (arg2 != 0) {
8660 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
8661 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
8662 }
8663
8664 }
8665 break;
8666#endif
8667
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308668#ifdef CONFIG_SPLICE
8669#ifdef TARGET_NR_tee
8670 case TARGET_NR_tee:
8671 {
8672 ret = get_errno(tee(arg1,arg2,arg3,arg4));
8673 }
8674 break;
8675#endif
8676#ifdef TARGET_NR_splice
8677 case TARGET_NR_splice:
8678 {
8679 loff_t loff_in, loff_out;
8680 loff_t *ploff_in = NULL, *ploff_out = NULL;
8681 if(arg2) {
8682 get_user_u64(loff_in, arg2);
8683 ploff_in = &loff_in;
8684 }
8685 if(arg4) {
8686 get_user_u64(loff_out, arg2);
8687 ploff_out = &loff_out;
8688 }
8689 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
8690 }
8691 break;
8692#endif
8693#ifdef TARGET_NR_vmsplice
8694 case TARGET_NR_vmsplice:
8695 {
Richard Hendersonf287b2c2012-09-15 13:20:25 -07008696 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8697 if (vec != NULL) {
8698 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
8699 unlock_iovec(vec, arg2, arg3, 0);
8700 } else {
8701 ret = -host_to_target_errno(errno);
8702 }
vibisreenivasan3ce34df2009-05-16 18:32:41 +05308703 }
8704 break;
8705#endif
8706#endif /* CONFIG_SPLICE */
Riku Voipioc2882b92009-08-12 15:08:24 +03008707#ifdef CONFIG_EVENTFD
8708#if defined(TARGET_NR_eventfd)
8709 case TARGET_NR_eventfd:
8710 ret = get_errno(eventfd(arg1, 0));
8711 break;
8712#endif
8713#if defined(TARGET_NR_eventfd2)
8714 case TARGET_NR_eventfd2:
Petar Jovanovic5947c692013-04-08 20:26:10 +02008715 {
8716 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
8717 if (arg2 & TARGET_O_NONBLOCK) {
8718 host_flags |= O_NONBLOCK;
8719 }
8720 if (arg2 & TARGET_O_CLOEXEC) {
8721 host_flags |= O_CLOEXEC;
8722 }
8723 ret = get_errno(eventfd(arg1, host_flags));
Riku Voipioc2882b92009-08-12 15:08:24 +03008724 break;
Petar Jovanovic5947c692013-04-08 20:26:10 +02008725 }
Riku Voipioc2882b92009-08-12 15:08:24 +03008726#endif
8727#endif /* CONFIG_EVENTFD */
Ulrich Hechtd0927932009-09-17 20:22:14 +03008728#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8729 case TARGET_NR_fallocate:
Alexander Graf20249ae2012-02-06 21:37:07 +01008730#if TARGET_ABI_BITS == 32
8731 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
8732 target_offset64(arg5, arg6)));
8733#else
Ulrich Hechtd0927932009-09-17 20:22:14 +03008734 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
Alexander Graf20249ae2012-02-06 21:37:07 +01008735#endif
Ulrich Hechtd0927932009-09-17 20:22:14 +03008736 break;
8737#endif
Peter Maydellc727f472011-01-06 11:05:10 +00008738#if defined(CONFIG_SYNC_FILE_RANGE)
8739#if defined(TARGET_NR_sync_file_range)
8740 case TARGET_NR_sync_file_range:
8741#if TARGET_ABI_BITS == 32
Riku Voipiobfcedc52011-06-20 16:24:39 +03008742#if defined(TARGET_MIPS)
8743 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8744 target_offset64(arg5, arg6), arg7));
8745#else
Peter Maydellc727f472011-01-06 11:05:10 +00008746 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
8747 target_offset64(arg4, arg5), arg6));
Riku Voipiobfcedc52011-06-20 16:24:39 +03008748#endif /* !TARGET_MIPS */
Peter Maydellc727f472011-01-06 11:05:10 +00008749#else
8750 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
8751#endif
8752 break;
8753#endif
8754#if defined(TARGET_NR_sync_file_range2)
8755 case TARGET_NR_sync_file_range2:
8756 /* This is like sync_file_range but the arguments are reordered */
8757#if TARGET_ABI_BITS == 32
8758 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8759 target_offset64(arg5, arg6), arg2));
8760#else
8761 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
8762#endif
8763 break;
8764#endif
8765#endif
Peter Maydell3b6edd12011-02-15 18:35:05 +00008766#if defined(CONFIG_EPOLL)
8767#if defined(TARGET_NR_epoll_create)
8768 case TARGET_NR_epoll_create:
8769 ret = get_errno(epoll_create(arg1));
8770 break;
8771#endif
8772#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
8773 case TARGET_NR_epoll_create1:
8774 ret = get_errno(epoll_create1(arg1));
8775 break;
8776#endif
8777#if defined(TARGET_NR_epoll_ctl)
8778 case TARGET_NR_epoll_ctl:
8779 {
8780 struct epoll_event ep;
8781 struct epoll_event *epp = 0;
8782 if (arg4) {
8783 struct target_epoll_event *target_ep;
8784 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
8785 goto efault;
8786 }
8787 ep.events = tswap32(target_ep->events);
8788 /* The epoll_data_t union is just opaque data to the kernel,
8789 * so we transfer all 64 bits across and need not worry what
8790 * actual data type it is.
8791 */
8792 ep.data.u64 = tswap64(target_ep->data.u64);
8793 unlock_user_struct(target_ep, arg4, 0);
8794 epp = &ep;
8795 }
8796 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
8797 break;
8798 }
8799#endif
8800
8801#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
8802#define IMPLEMENT_EPOLL_PWAIT
8803#endif
8804#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
8805#if defined(TARGET_NR_epoll_wait)
8806 case TARGET_NR_epoll_wait:
8807#endif
8808#if defined(IMPLEMENT_EPOLL_PWAIT)
8809 case TARGET_NR_epoll_pwait:
8810#endif
8811 {
8812 struct target_epoll_event *target_ep;
8813 struct epoll_event *ep;
8814 int epfd = arg1;
8815 int maxevents = arg3;
8816 int timeout = arg4;
8817
8818 target_ep = lock_user(VERIFY_WRITE, arg2,
8819 maxevents * sizeof(struct target_epoll_event), 1);
8820 if (!target_ep) {
8821 goto efault;
8822 }
8823
8824 ep = alloca(maxevents * sizeof(struct epoll_event));
8825
8826 switch (num) {
8827#if defined(IMPLEMENT_EPOLL_PWAIT)
8828 case TARGET_NR_epoll_pwait:
8829 {
8830 target_sigset_t *target_set;
8831 sigset_t _set, *set = &_set;
8832
8833 if (arg5) {
8834 target_set = lock_user(VERIFY_READ, arg5,
8835 sizeof(target_sigset_t), 1);
8836 if (!target_set) {
8837 unlock_user(target_ep, arg2, 0);
8838 goto efault;
8839 }
8840 target_to_host_sigset(set, target_set);
8841 unlock_user(target_set, arg5, 0);
8842 } else {
8843 set = NULL;
8844 }
8845
8846 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
8847 break;
8848 }
8849#endif
8850#if defined(TARGET_NR_epoll_wait)
8851 case TARGET_NR_epoll_wait:
8852 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
8853 break;
8854#endif
8855 default:
8856 ret = -TARGET_ENOSYS;
8857 }
8858 if (!is_error(ret)) {
8859 int i;
8860 for (i = 0; i < ret; i++) {
8861 target_ep[i].events = tswap32(ep[i].events);
8862 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
8863 }
8864 }
8865 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
8866 break;
8867 }
8868#endif
8869#endif
Peter Maydell163a05a2011-06-27 17:44:52 +01008870#ifdef TARGET_NR_prlimit64
8871 case TARGET_NR_prlimit64:
8872 {
8873 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
8874 struct target_rlimit64 *target_rnew, *target_rold;
8875 struct host_rlimit64 rnew, rold, *rnewp = 0;
8876 if (arg3) {
8877 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
8878 goto efault;
8879 }
8880 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
8881 rnew.rlim_max = tswap64(target_rnew->rlim_max);
8882 unlock_user_struct(target_rnew, arg3, 0);
8883 rnewp = &rnew;
8884 }
8885
8886 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
8887 if (!is_error(ret) && arg4) {
8888 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
8889 goto efault;
8890 }
8891 target_rold->rlim_cur = tswap64(rold.rlim_cur);
8892 target_rold->rlim_max = tswap64(rold.rlim_max);
8893 unlock_user_struct(target_rold, arg4, 1);
8894 }
8895 break;
8896 }
8897#endif
Richard Henderson3d21d292012-09-15 13:20:46 -07008898#ifdef TARGET_NR_gethostname
8899 case TARGET_NR_gethostname:
8900 {
8901 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
8902 if (name) {
8903 ret = get_errno(gethostname(name, arg2));
8904 unlock_user(name, arg1, arg2);
8905 } else {
8906 ret = -TARGET_EFAULT;
8907 }
8908 break;
8909 }
8910#endif
bellard31e31b82003-02-18 22:55:36 +00008911 default:
8912 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00008913 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00008914#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 +00008915 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00008916#endif
ths0da46a62007-10-20 20:23:07 +00008917 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00008918 break;
8919 }
bellard579a97f2007-11-11 14:26:47 +00008920fail:
bellardc573ff62004-01-04 15:51:36 +00008921#ifdef DEBUG
Blue Swirl0bf9e312009-07-20 17:19:25 +00008922 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
bellardc573ff62004-01-04 15:51:36 +00008923#endif
thsb92c47c2007-11-01 00:07:38 +00008924 if(do_strace)
8925 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00008926 return ret;
bellard579a97f2007-11-11 14:26:47 +00008927efault:
8928 ret = -TARGET_EFAULT;
8929 goto fail;
bellard31e31b82003-02-18 22:55:36 +00008930}