]> Git Repo - qemu.git/blame - linux-user/syscall.c
pcspk: fix KVM
[qemu.git] / linux-user / syscall.c
CommitLineData
31e31b8a
FB
1/*
2 * Linux syscalls
5fafdf24 3 *
31e31b8a
FB
4 * 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
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31e31b8a 18 */
d5b3a9b6 19#define _ATFILE_SOURCE
d39594e9 20#include "qemu/osdep.h"
f348b6d1
VB
21#include "qemu/cutils.h"
22#include "qemu/path.h"
31e31b8a
FB
23#include <elf.h>
24#include <endian.h>
c56dc774 25#include <grp.h>
d08d3bb8
TS
26#include <sys/ipc.h>
27#include <sys/msg.h>
31e31b8a 28#include <sys/wait.h>
31e31b8a 29#include <sys/mount.h>
586b0bef
JS
30#include <sys/file.h>
31#include <sys/fsuid.h>
32#include <sys/personality.h>
39b9aae1 33#include <sys/prctl.h>
31e31b8a 34#include <sys/resource.h>
31e31b8a 35#include <sys/swap.h>
e0eb210e 36#include <linux/capability.h>
31e31b8a 37#include <sched.h>
60e99246
AJ
38#ifdef __ia64__
39int __clone2(int (*fn)(void *), void *child_stack_base,
40 size_t stack_size, int flags, void *arg, ...);
41#endif
31e31b8a 42#include <sys/socket.h>
607175e0 43#include <sys/un.h>
31e31b8a 44#include <sys/uio.h>
9de5e440 45#include <sys/poll.h>
32f36bce 46#include <sys/times.h>
8853f86e 47#include <sys/shm.h>
fa294816 48#include <sys/sem.h>
56c8f68f 49#include <sys/statfs.h>
ebc05488 50#include <utime.h>
a5448a7d 51#include <sys/sysinfo.h>
e36800c9 52#include <sys/signalfd.h>
72f03900 53//#include <sys/user.h>
8853f86e 54#include <netinet/ip.h>
7854b056 55#include <netinet/tcp.h>
86fcd946 56#include <linux/wireless.h>
920394db 57#include <linux/icmp.h>
5a61cb60 58#include "qemu-common.h"
d80a1905
RV
59#ifdef CONFIG_TIMERFD
60#include <sys/timerfd.h>
61#endif
9788c9ca 62#ifdef TARGET_GPROF
6d946cda
AJ
63#include <sys/gmon.h>
64#endif
c2882b96
RV
65#ifdef CONFIG_EVENTFD
66#include <sys/eventfd.h>
67#endif
3b6edd16
PM
68#ifdef CONFIG_EPOLL
69#include <sys/epoll.h>
70#endif
a790ae38 71#ifdef CONFIG_ATTR
1de7afc9 72#include "qemu/xattr.h"
a790ae38 73#endif
a8fd1aba
PM
74#ifdef CONFIG_SENDFILE
75#include <sys/sendfile.h>
76#endif
31e31b8a
FB
77
78#define termios host_termios
79#define winsize host_winsize
80#define termio host_termio
04369ff2
FB
81#define sgttyb host_sgttyb /* same as target */
82#define tchars host_tchars /* same as target */
83#define ltchars host_ltchars /* same as target */
31e31b8a
FB
84
85#include <linux/termios.h>
86#include <linux/unistd.h>
31e31b8a
FB
87#include <linux/cdrom.h>
88#include <linux/hdreg.h>
89#include <linux/soundcard.h>
19b84f3c 90#include <linux/kd.h>
8fbd6b52 91#include <linux/mtio.h>
350d1779 92#include <linux/fs.h>
dace20dc 93#if defined(CONFIG_FIEMAP)
285da2b9 94#include <linux/fiemap.h>
dace20dc 95#endif
f7680a55
UH
96#include <linux/fb.h>
97#include <linux/vt.h>
56e904ec 98#include <linux/dm-ioctl.h>
c07ecc68 99#include <linux/reboot.h>
7ff7b666 100#include <linux/route.h>
f57d4192 101#include <linux/filter.h>
fff8c539 102#include <linux/blkpg.h>
6c5b5645 103#include <linux/netlink.h>
575b22b1 104#ifdef CONFIG_RTNETLINK
6c5b5645 105#include <linux/rtnetlink.h>
575b22b1 106#endif
5ce9bb59 107#include <linux/audit.h>
d7e4036e 108#include "linux_loop.h"
18cb0088 109#include "uname.h"
31e31b8a 110
3ef693a0 111#include "qemu.h"
31e31b8a 112
d865bab5
PB
113#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
114 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
30813cea 115
72f03900 116//#define DEBUG
71a8f7fe
TB
117/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
118 * once. This exercises the codepaths for restart.
119 */
120//#define DEBUG_ERESTARTSYS
31e31b8a 121
1a9353d2 122//#include <linux/msdos_fs.h>
6556a833
AJ
123#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
124#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
1a9353d2 125
70a194b9
FB
126#undef _syscall0
127#undef _syscall1
128#undef _syscall2
129#undef _syscall3
130#undef _syscall4
131#undef _syscall5
83fcb515 132#undef _syscall6
70a194b9 133
83fcb515 134#define _syscall0(type,name) \
8fcd3692 135static type name (void) \
83fcb515
FB
136{ \
137 return syscall(__NR_##name); \
138}
70a194b9 139
83fcb515 140#define _syscall1(type,name,type1,arg1) \
8fcd3692 141static type name (type1 arg1) \
83fcb515
FB
142{ \
143 return syscall(__NR_##name, arg1); \
70a194b9
FB
144}
145
83fcb515 146#define _syscall2(type,name,type1,arg1,type2,arg2) \
8fcd3692 147static type name (type1 arg1,type2 arg2) \
83fcb515
FB
148{ \
149 return syscall(__NR_##name, arg1, arg2); \
70a194b9
FB
150}
151
83fcb515 152#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
8fcd3692 153static type name (type1 arg1,type2 arg2,type3 arg3) \
83fcb515
FB
154{ \
155 return syscall(__NR_##name, arg1, arg2, arg3); \
70a194b9
FB
156}
157
83fcb515 158#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
8fcd3692 159static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
83fcb515
FB
160{ \
161 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
70a194b9
FB
162}
163
83fcb515
FB
164#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
165 type5,arg5) \
8fcd3692 166static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
83fcb515
FB
167{ \
168 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
70a194b9
FB
169}
170
83fcb515
FB
171
172#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
173 type5,arg5,type6,arg6) \
8fcd3692
BS
174static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
175 type6 arg6) \
83fcb515
FB
176{ \
177 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
70a194b9 178}
83fcb515 179
70a194b9 180
31e31b8a 181#define __NR_sys_uname __NR_uname
72f03900 182#define __NR_sys_getcwd1 __NR_getcwd
72f03900 183#define __NR_sys_getdents __NR_getdents
dab2ed99 184#define __NR_sys_getdents64 __NR_getdents64
c6cda17a 185#define __NR_sys_getpriority __NR_getpriority
66fb9763 186#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
7494b0f9 187#define __NR_sys_syslog __NR_syslog
bd0c5661 188#define __NR_sys_futex __NR_futex
39b59763
AJ
189#define __NR_sys_inotify_init __NR_inotify_init
190#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
191#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
31e31b8a 192
42a39fbe
AG
193#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
194 defined(__s390x__)
9af9eaaa
FB
195#define __NR__llseek __NR_lseek
196#endif
197
a29e5ba2
JH
198/* Newer kernel ports have llseek() instead of _llseek() */
199#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
200#define TARGET_NR__llseek TARGET_NR_llseek
201#endif
202
72f03900 203#ifdef __NR_gettid
31e31b8a 204_syscall0(int, gettid)
72f03900 205#else
0da46a6e
TS
206/* This is a replacement for the host gettid() and must return a host
207 errno. */
72f03900
FB
208static int gettid(void) {
209 return -ENOSYS;
210}
211#endif
704eff6c 212#if defined(TARGET_NR_getdents) && defined(__NR_getdents)
3b3f24ad 213_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
3307e236
PM
214#endif
215#if !defined(__NR_getdents) || \
216 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
3b3f24ad
AJ
217_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
218#endif
d35b261c 219#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
3b3f24ad
AJ
220_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
221 loff_t *, res, uint, wh);
222#endif
223_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
224_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
3b3f24ad
AJ
225#ifdef __NR_exit_group
226_syscall1(int,exit_group,int,error_code)
227#endif
228#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
229_syscall1(int,set_tid_address,int *,tidptr)
230#endif
3b3f24ad
AJ
231#if defined(TARGET_NR_futex) && defined(__NR_futex)
232_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
233 const struct timespec *,timeout,int *,uaddr2,int,val3)
234#endif
737de1d1
MF
235#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
236_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
237 unsigned long *, user_mask_ptr);
238#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
239_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
240 unsigned long *, user_mask_ptr);
0f6b4d21
AG
241_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
242 void *, arg);
e0eb210e
PM
243_syscall2(int, capget, struct __user_cap_header_struct *, header,
244 struct __user_cap_data_struct *, data);
245_syscall2(int, capset, struct __user_cap_header_struct *, header,
246 struct __user_cap_data_struct *, data);
ab31cda3
PB
247#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
248_syscall2(int, ioprio_get, int, which, int, who)
249#endif
250#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
251_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
252#endif
f894efd1
LV
253#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
254_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
255#endif
3b3f24ad
AJ
256
257static bitmask_transtbl fcntl_flags_tbl[] = {
258 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
259 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
260 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
261 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
262 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
263 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
264 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
265 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
afc8763f 266 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
3b3f24ad
AJ
267 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
268 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
269 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
270 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
3b3f24ad
AJ
271#if defined(O_DIRECT)
272 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
afc8763f
RH
273#endif
274#if defined(O_NOATIME)
275 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
276#endif
277#if defined(O_CLOEXEC)
278 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
279#endif
280#if defined(O_PATH)
281 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
282#endif
283 /* Don't terminate the list prematurely on 64-bit host+guest. */
284#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
285 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
3b3f24ad
AJ
286#endif
287 { 0, 0, 0, 0 }
288};
289
5d4d3665 290typedef abi_long (*TargetFdDataFunc)(void *, size_t);
7b36f782 291typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
e36800c9 292typedef struct TargetFdTrans {
5d4d3665
LV
293 TargetFdDataFunc host_to_target_data;
294 TargetFdDataFunc target_to_host_data;
7b36f782 295 TargetFdAddrFunc target_to_host_addr;
e36800c9
LV
296} TargetFdTrans;
297
298static TargetFdTrans **target_fd_trans;
299
300static unsigned int target_fd_max;
301
6c5b5645
LV
302static TargetFdDataFunc fd_trans_target_to_host_data(int fd)
303{
304 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
305 return target_fd_trans[fd]->target_to_host_data;
306 }
307 return NULL;
308}
309
5d4d3665 310static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
e36800c9 311{
861d72cd 312 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
5d4d3665 313 return target_fd_trans[fd]->host_to_target_data;
e36800c9
LV
314 }
315 return NULL;
316}
317
7b36f782
LV
318static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
319{
861d72cd 320 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
7b36f782
LV
321 return target_fd_trans[fd]->target_to_host_addr;
322 }
323 return NULL;
324}
325
e36800c9
LV
326static void fd_trans_register(int fd, TargetFdTrans *trans)
327{
328 unsigned int oldmax;
329
330 if (fd >= target_fd_max) {
331 oldmax = target_fd_max;
332 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
5089c7ce
LV
333 target_fd_trans = g_renew(TargetFdTrans *,
334 target_fd_trans, target_fd_max);
e36800c9
LV
335 memset((void *)(target_fd_trans + oldmax), 0,
336 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
337 }
338 target_fd_trans[fd] = trans;
339}
340
341static void fd_trans_unregister(int fd)
342{
343 if (fd >= 0 && fd < target_fd_max) {
344 target_fd_trans[fd] = NULL;
345 }
346}
347
348static void fd_trans_dup(int oldfd, int newfd)
349{
350 fd_trans_unregister(newfd);
351 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
352 fd_trans_register(newfd, target_fd_trans[oldfd]);
353 }
354}
355
3b3f24ad
AJ
356static int sys_getcwd1(char *buf, size_t size)
357{
358 if (getcwd(buf, size) == NULL) {
359 /* getcwd() sets errno */
360 return (-1);
361 }
aaf4ad39 362 return strlen(buf)+1;
3b3f24ad
AJ
363}
364
1acae9f2 365#ifdef TARGET_NR_utimensat
ebc996f3
RV
366#ifdef CONFIG_UTIMENSAT
367static int sys_utimensat(int dirfd, const char *pathname,
368 const struct timespec times[2], int flags)
369{
370 if (pathname == NULL)
371 return futimens(dirfd, times);
372 else
373 return utimensat(dirfd, pathname, times, flags);
374}
1acae9f2
PM
375#elif defined(__NR_utimensat)
376#define __NR_sys_utimensat __NR_utimensat
9007f0ef
TS
377_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
378 const struct timespec *,tsp,int,flags)
1acae9f2
PM
379#else
380static int sys_utimensat(int dirfd, const char *pathname,
381 const struct timespec times[2], int flags)
382{
383 errno = ENOSYS;
384 return -1;
385}
9007f0ef 386#endif
1acae9f2 387#endif /* TARGET_NR_utimensat */
3b3f24ad
AJ
388
389#ifdef CONFIG_INOTIFY
8690e420 390#include <sys/inotify.h>
3b3f24ad 391
39b59763 392#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
3b3f24ad
AJ
393static int sys_inotify_init(void)
394{
395 return (inotify_init());
396}
39b59763
AJ
397#endif
398#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
3b3f24ad
AJ
399static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
400{
401 return (inotify_add_watch(fd, pathname, mask));
402}
39b59763
AJ
403#endif
404#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
3b3f24ad
AJ
405static int sys_inotify_rm_watch(int fd, int32_t wd)
406{
8690e420 407 return (inotify_rm_watch(fd, wd));
3b3f24ad 408}
bd0c5661 409#endif
c05c7a73
RV
410#ifdef CONFIG_INOTIFY1
411#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
412static int sys_inotify_init1(int flags)
413{
414 return (inotify_init1(flags));
415}
416#endif
417#endif
3b3f24ad
AJ
418#else
419/* Userspace can usually survive runtime without inotify */
420#undef TARGET_NR_inotify_init
c05c7a73 421#undef TARGET_NR_inotify_init1
3b3f24ad
AJ
422#undef TARGET_NR_inotify_add_watch
423#undef TARGET_NR_inotify_rm_watch
424#endif /* CONFIG_INOTIFY */
425
163a05a8
PM
426#if defined(TARGET_NR_prlimit64)
427#ifndef __NR_prlimit64
428# define __NR_prlimit64 -1
429#endif
430#define __NR_sys_prlimit64 __NR_prlimit64
431/* The glibc rlimit structure may not be that used by the underlying syscall */
432struct host_rlimit64 {
433 uint64_t rlim_cur;
434 uint64_t rlim_max;
435};
436_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
437 const struct host_rlimit64 *, new_limit,
438 struct host_rlimit64 *, old_limit)
439#endif
440
f4f1e10a
ECL
441
442#if defined(TARGET_NR_timer_create)
443/* Maxiumum of 32 active POSIX timers allowed at any one time. */
444static timer_t g_posix_timers[32] = { 0, } ;
445
446static inline int next_free_host_timer(void)
447{
448 int k ;
449 /* FIXME: Does finding the next free slot require a lock? */
450 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
451 if (g_posix_timers[k] == 0) {
452 g_posix_timers[k] = (timer_t) 1;
453 return k;
454 }
455 }
456 return -1;
457}
458#endif
459
48e515d4 460/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
4a1def4e 461#ifdef TARGET_ARM
48e515d4
RV
462static inline int regpairs_aligned(void *cpu_env) {
463 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
464}
465#elif defined(TARGET_MIPS)
466static inline int regpairs_aligned(void *cpu_env) { return 1; }
4a1def4e
AG
467#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
468/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
469 * of registers which translates to the same as ARM/MIPS, because we start with
470 * r3 as arg1 */
471static inline int regpairs_aligned(void *cpu_env) { return 1; }
48e515d4
RV
472#else
473static inline int regpairs_aligned(void *cpu_env) { return 0; }
474#endif
475
b92c47c1
TS
476#define ERRNO_TABLE_SIZE 1200
477
478/* target_to_host_errno_table[] is initialized from
479 * host_to_target_errno_table[] in syscall_init(). */
480static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
481};
482
637947f1 483/*
fe8f096b 484 * This list is the union of errno values overridden in asm-<arch>/errno.h
637947f1
TS
485 * minus the errnos that are not actually generic to all archs.
486 */
b92c47c1 487static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
08703b9f 488 [EAGAIN] = TARGET_EAGAIN,
637947f1
TS
489 [EIDRM] = TARGET_EIDRM,
490 [ECHRNG] = TARGET_ECHRNG,
491 [EL2NSYNC] = TARGET_EL2NSYNC,
492 [EL3HLT] = TARGET_EL3HLT,
493 [EL3RST] = TARGET_EL3RST,
494 [ELNRNG] = TARGET_ELNRNG,
495 [EUNATCH] = TARGET_EUNATCH,
496 [ENOCSI] = TARGET_ENOCSI,
497 [EL2HLT] = TARGET_EL2HLT,
498 [EDEADLK] = TARGET_EDEADLK,
499 [ENOLCK] = TARGET_ENOLCK,
500 [EBADE] = TARGET_EBADE,
501 [EBADR] = TARGET_EBADR,
502 [EXFULL] = TARGET_EXFULL,
503 [ENOANO] = TARGET_ENOANO,
504 [EBADRQC] = TARGET_EBADRQC,
505 [EBADSLT] = TARGET_EBADSLT,
506 [EBFONT] = TARGET_EBFONT,
507 [ENOSTR] = TARGET_ENOSTR,
508 [ENODATA] = TARGET_ENODATA,
509 [ETIME] = TARGET_ETIME,
510 [ENOSR] = TARGET_ENOSR,
511 [ENONET] = TARGET_ENONET,
512 [ENOPKG] = TARGET_ENOPKG,
513 [EREMOTE] = TARGET_EREMOTE,
514 [ENOLINK] = TARGET_ENOLINK,
515 [EADV] = TARGET_EADV,
516 [ESRMNT] = TARGET_ESRMNT,
517 [ECOMM] = TARGET_ECOMM,
518 [EPROTO] = TARGET_EPROTO,
519 [EDOTDOT] = TARGET_EDOTDOT,
520 [EMULTIHOP] = TARGET_EMULTIHOP,
521 [EBADMSG] = TARGET_EBADMSG,
522 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
523 [EOVERFLOW] = TARGET_EOVERFLOW,
524 [ENOTUNIQ] = TARGET_ENOTUNIQ,
525 [EBADFD] = TARGET_EBADFD,
526 [EREMCHG] = TARGET_EREMCHG,
527 [ELIBACC] = TARGET_ELIBACC,
528 [ELIBBAD] = TARGET_ELIBBAD,
529 [ELIBSCN] = TARGET_ELIBSCN,
530 [ELIBMAX] = TARGET_ELIBMAX,
531 [ELIBEXEC] = TARGET_ELIBEXEC,
532 [EILSEQ] = TARGET_EILSEQ,
533 [ENOSYS] = TARGET_ENOSYS,
534 [ELOOP] = TARGET_ELOOP,
535 [ERESTART] = TARGET_ERESTART,
536 [ESTRPIPE] = TARGET_ESTRPIPE,
537 [ENOTEMPTY] = TARGET_ENOTEMPTY,
538 [EUSERS] = TARGET_EUSERS,
539 [ENOTSOCK] = TARGET_ENOTSOCK,
540 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
541 [EMSGSIZE] = TARGET_EMSGSIZE,
542 [EPROTOTYPE] = TARGET_EPROTOTYPE,
543 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
544 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
545 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
546 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
547 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
548 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
549 [EADDRINUSE] = TARGET_EADDRINUSE,
550 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
551 [ENETDOWN] = TARGET_ENETDOWN,
552 [ENETUNREACH] = TARGET_ENETUNREACH,
553 [ENETRESET] = TARGET_ENETRESET,
554 [ECONNABORTED] = TARGET_ECONNABORTED,
555 [ECONNRESET] = TARGET_ECONNRESET,
556 [ENOBUFS] = TARGET_ENOBUFS,
557 [EISCONN] = TARGET_EISCONN,
558 [ENOTCONN] = TARGET_ENOTCONN,
559 [EUCLEAN] = TARGET_EUCLEAN,
560 [ENOTNAM] = TARGET_ENOTNAM,
561 [ENAVAIL] = TARGET_ENAVAIL,
562 [EISNAM] = TARGET_EISNAM,
563 [EREMOTEIO] = TARGET_EREMOTEIO,
564 [ESHUTDOWN] = TARGET_ESHUTDOWN,
565 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
566 [ETIMEDOUT] = TARGET_ETIMEDOUT,
567 [ECONNREFUSED] = TARGET_ECONNREFUSED,
568 [EHOSTDOWN] = TARGET_EHOSTDOWN,
569 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
570 [EALREADY] = TARGET_EALREADY,
571 [EINPROGRESS] = TARGET_EINPROGRESS,
572 [ESTALE] = TARGET_ESTALE,
573 [ECANCELED] = TARGET_ECANCELED,
574 [ENOMEDIUM] = TARGET_ENOMEDIUM,
575 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
b7fe5db7 576#ifdef ENOKEY
637947f1 577 [ENOKEY] = TARGET_ENOKEY,
b7fe5db7
TS
578#endif
579#ifdef EKEYEXPIRED
637947f1 580 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
b7fe5db7
TS
581#endif
582#ifdef EKEYREVOKED
637947f1 583 [EKEYREVOKED] = TARGET_EKEYREVOKED,
b7fe5db7
TS
584#endif
585#ifdef EKEYREJECTED
637947f1 586 [EKEYREJECTED] = TARGET_EKEYREJECTED,
b7fe5db7
TS
587#endif
588#ifdef EOWNERDEAD
637947f1 589 [EOWNERDEAD] = TARGET_EOWNERDEAD,
b7fe5db7
TS
590#endif
591#ifdef ENOTRECOVERABLE
637947f1 592 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
b7fe5db7 593#endif
b92c47c1 594};
637947f1
TS
595
596static inline int host_to_target_errno(int err)
597{
2466119c
TB
598 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
599 host_to_target_errno_table[err]) {
637947f1 600 return host_to_target_errno_table[err];
2466119c 601 }
637947f1
TS
602 return err;
603}
604
b92c47c1
TS
605static inline int target_to_host_errno(int err)
606{
2466119c
TB
607 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
608 target_to_host_errno_table[err]) {
b92c47c1 609 return target_to_host_errno_table[err];
2466119c 610 }
b92c47c1
TS
611 return err;
612}
613
992f48a0 614static inline abi_long get_errno(abi_long ret)
31e31b8a
FB
615{
616 if (ret == -1)
637947f1 617 return -host_to_target_errno(errno);
31e31b8a
FB
618 else
619 return ret;
620}
621
992f48a0 622static inline int is_error(abi_long ret)
31e31b8a 623{
992f48a0 624 return (abi_ulong)ret >= (abi_ulong)(-4096);
31e31b8a
FB
625}
626
7dcdaeaf 627const char *target_strerror(int err)
b92c47c1 628{
da2a34f7
PM
629 if (err == TARGET_ERESTARTSYS) {
630 return "To be restarted";
631 }
632 if (err == TARGET_QEMU_ESIGRETURN) {
633 return "Successful exit from sigreturn";
634 }
635
962b289e
AG
636 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
637 return NULL;
638 }
b92c47c1
TS
639 return strerror(target_to_host_errno(err));
640}
641
4d330cee
TB
642#define safe_syscall0(type, name) \
643static type safe_##name(void) \
644{ \
645 return safe_syscall(__NR_##name); \
646}
647
648#define safe_syscall1(type, name, type1, arg1) \
649static type safe_##name(type1 arg1) \
650{ \
651 return safe_syscall(__NR_##name, arg1); \
652}
653
654#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
655static type safe_##name(type1 arg1, type2 arg2) \
656{ \
657 return safe_syscall(__NR_##name, arg1, arg2); \
658}
659
660#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
661static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
662{ \
663 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
664}
665
666#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
667 type4, arg4) \
668static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
669{ \
670 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
671}
672
673#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
674 type4, arg4, type5, arg5) \
675static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
676 type5 arg5) \
677{ \
678 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
679}
680
681#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
682 type4, arg4, type5, arg5, type6, arg6) \
683static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
684 type5 arg5, type6 arg6) \
685{ \
686 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
687}
688
50afd02b
TB
689safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
690safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
c10a0738
TB
691safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
692 int, flags, mode_t, mode)
4af80a37
TB
693safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
694 struct rusage *, rusage)
695safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
696 int, options, struct rusage *, rusage)
ffdcbe22 697safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
6df9d38d
PM
698safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
699 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
a6130237
PM
700safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
701 struct timespec *, tsp, const sigset_t *, sigmask,
702 size_t, sigsetsize)
227f0214
PM
703safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
704 int, maxevents, int, timeout, const sigset_t *, sigmask,
705 size_t, sigsetsize)
d509eeb1
PM
706safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
707 const struct timespec *,timeout,int *,uaddr2,int,val3)
2fe4fba1 708safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
bef653d9
PM
709safe_syscall2(int, kill, pid_t, pid, int, sig)
710safe_syscall2(int, tkill, int, tid, int, sig)
711safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
918c03ed
PM
712safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
713safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
2a3c7619
PM
714safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
715 socklen_t, addrlen)
66687530
PM
716safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
717 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
718safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
719 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
720safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
721safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
2a845989 722safe_syscall2(int, flock, int, fd, int, operation)
b3f82330
PM
723safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
724 const struct timespec *, uts, size_t, sigsetsize)
ff6dc130
PM
725safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
726 int, flags)
9e518226
PM
727safe_syscall2(int, nanosleep, const struct timespec *, req,
728 struct timespec *, rem)
729#ifdef TARGET_NR_clock_nanosleep
730safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
731 const struct timespec *, req, struct timespec *, rem)
732#endif
89f9fe44
PM
733#ifdef __NR_msgsnd
734safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
735 int, flags)
736safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
737 long, msgtype, int, flags)
ffb7ee79
PM
738safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
739 unsigned, nsops, const struct timespec *, timeout)
89f9fe44
PM
740#else
741/* This host kernel architecture uses a single ipc syscall; fake up
742 * wrappers for the sub-operations to hide this implementation detail.
743 * Annoyingly we can't include linux/ipc.h to get the constant definitions
744 * for the call parameter because some structs in there conflict with the
745 * sys/ipc.h ones. So we just define them here, and rely on them being
746 * the same for all host architectures.
747 */
ffb7ee79 748#define Q_SEMTIMEDOP 4
89f9fe44
PM
749#define Q_MSGSND 11
750#define Q_MSGRCV 12
751#define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP))
752
753safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
754 void *, ptr, long, fifth)
755static int safe_msgsnd(int msgid, const void *msgp, size_t sz, int flags)
756{
757 return safe_ipc(Q_IPCCALL(0, Q_MSGSND), msgid, sz, flags, (void *)msgp, 0);
758}
759static int safe_msgrcv(int msgid, void *msgp, size_t sz, long type, int flags)
760{
761 return safe_ipc(Q_IPCCALL(1, Q_MSGRCV), msgid, sz, flags, msgp, type);
762}
ffb7ee79
PM
763static int safe_semtimedop(int semid, struct sembuf *tsops, unsigned nsops,
764 const struct timespec *timeout)
765{
766 return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP), semid, nsops, 0, tsops,
767 (long)timeout);
768}
89f9fe44 769#endif
d40ecd66
PM
770#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
771safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
772 size_t, len, unsigned, prio, const struct timespec *, timeout)
773safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
774 size_t, len, unsigned *, prio, const struct timespec *, timeout)
775#endif
49ca6f3e
PM
776/* We do ioctl like this rather than via safe_syscall3 to preserve the
777 * "third argument might be integer or pointer or not present" behaviour of
778 * the libc function.
779 */
780#define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
435da5e7
PM
781/* Similarly for fcntl. Note that callers must always:
782 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
783 * use the flock64 struct rather than unsuffixed flock
784 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
785 */
786#ifdef __NR_fcntl64
787#define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
788#else
789#define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
790#endif
50afd02b 791
8289d112
PB
792static inline int host_to_target_sock_type(int host_type)
793{
794 int target_type;
795
796 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
797 case SOCK_DGRAM:
798 target_type = TARGET_SOCK_DGRAM;
799 break;
800 case SOCK_STREAM:
801 target_type = TARGET_SOCK_STREAM;
802 break;
803 default:
804 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
805 break;
806 }
807
808#if defined(SOCK_CLOEXEC)
809 if (host_type & SOCK_CLOEXEC) {
810 target_type |= TARGET_SOCK_CLOEXEC;
811 }
812#endif
813
814#if defined(SOCK_NONBLOCK)
815 if (host_type & SOCK_NONBLOCK) {
816 target_type |= TARGET_SOCK_NONBLOCK;
817 }
818#endif
819
820 return target_type;
821}
822
992f48a0
BS
823static abi_ulong target_brk;
824static abi_ulong target_original_brk;
4d1de87c 825static abi_ulong brk_page;
31e31b8a 826
992f48a0 827void target_set_brk(abi_ulong new_brk)
31e31b8a 828{
4c1de73d 829 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
4d1de87c 830 brk_page = HOST_PAGE_ALIGN(target_brk);
31e31b8a
FB
831}
832
4d1de87c
CV
833//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
834#define DEBUGF_BRK(message, args...)
835
0da46a6e 836/* do_brk() must return target values and target errnos. */
992f48a0 837abi_long do_brk(abi_ulong new_brk)
31e31b8a 838{
992f48a0 839 abi_long mapped_addr;
31e31b8a
FB
840 int new_alloc_size;
841
3a0c6c4a 842 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
4d1de87c
CV
843
844 if (!new_brk) {
3a0c6c4a 845 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
53a5960a 846 return target_brk;
4d1de87c
CV
847 }
848 if (new_brk < target_original_brk) {
3a0c6c4a
PB
849 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
850 target_brk);
7ab240ad 851 return target_brk;
4d1de87c 852 }
3b46e624 853
4d1de87c
CV
854 /* If the new brk is less than the highest page reserved to the
855 * target heap allocation, set it and we're almost done... */
856 if (new_brk <= brk_page) {
857 /* Heap contents are initialized to zero, as for anonymous
858 * mapped pages. */
859 if (new_brk > target_brk) {
860 memset(g2h(target_brk), 0, new_brk - target_brk);
861 }
31e31b8a 862 target_brk = new_brk;
3a0c6c4a 863 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
53a5960a 864 return target_brk;
31e31b8a
FB
865 }
866
00faf08c
PM
867 /* We need to allocate more memory after the brk... Note that
868 * we don't use MAP_FIXED because that will map over the top of
869 * any existing mapping (like the one with the host libc or qemu
870 * itself); instead we treat "mapped but at wrong address" as
871 * a failure and unmap again.
872 */
4d1de87c 873 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
5fafdf24 874 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
54936004 875 PROT_READ|PROT_WRITE,
00faf08c
PM
876 MAP_ANON|MAP_PRIVATE, 0, 0));
877
878 if (mapped_addr == brk_page) {
70afc343
CV
879 /* Heap contents are initialized to zero, as for anonymous
880 * mapped pages. Technically the new pages are already
881 * initialized to zero since they *are* anonymous mapped
882 * pages, however we have to take care with the contents that
883 * come from the remaining part of the previous page: it may
884 * contains garbage data due to a previous heap usage (grown
885 * then shrunken). */
886 memset(g2h(target_brk), 0, brk_page - target_brk);
887
00faf08c 888 target_brk = new_brk;
4d1de87c 889 brk_page = HOST_PAGE_ALIGN(target_brk);
3a0c6c4a
PB
890 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
891 target_brk);
00faf08c
PM
892 return target_brk;
893 } else if (mapped_addr != -1) {
894 /* Mapped but at wrong address, meaning there wasn't actually
895 * enough space for this brk.
896 */
897 target_munmap(mapped_addr, new_alloc_size);
898 mapped_addr = -1;
3a0c6c4a 899 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
4d1de87c
CV
900 }
901 else {
3a0c6c4a 902 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
00faf08c 903 }
7ab240ad 904
7dd46c02
RH
905#if defined(TARGET_ALPHA)
906 /* We (partially) emulate OSF/1 on Alpha, which requires we
907 return a proper errno, not an unchanged brk value. */
00faf08c 908 return -TARGET_ENOMEM;
7dd46c02 909#endif
00faf08c 910 /* For everything else, return the previous break. */
7ab240ad 911 return target_brk;
31e31b8a
FB
912}
913
26edcf41
TS
914static inline abi_long copy_from_user_fdset(fd_set *fds,
915 abi_ulong target_fds_addr,
916 int n)
31e31b8a 917{
26edcf41
TS
918 int i, nw, j, k;
919 abi_ulong b, *target_fds;
920
b1b2db29 921 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
26edcf41
TS
922 if (!(target_fds = lock_user(VERIFY_READ,
923 target_fds_addr,
924 sizeof(abi_ulong) * nw,
925 1)))
926 return -TARGET_EFAULT;
927
928 FD_ZERO(fds);
929 k = 0;
930 for (i = 0; i < nw; i++) {
931 /* grab the abi_ulong */
932 __get_user(b, &target_fds[i]);
933 for (j = 0; j < TARGET_ABI_BITS; j++) {
934 /* check the bit inside the abi_ulong */
935 if ((b >> j) & 1)
936 FD_SET(k, fds);
937 k++;
31e31b8a 938 }
31e31b8a 939 }
26edcf41
TS
940
941 unlock_user(target_fds, target_fds_addr, 0);
942
943 return 0;
31e31b8a
FB
944}
945
055e0906
MF
946static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
947 abi_ulong target_fds_addr,
948 int n)
949{
950 if (target_fds_addr) {
951 if (copy_from_user_fdset(fds, target_fds_addr, n))
952 return -TARGET_EFAULT;
953 *fds_ptr = fds;
954 } else {
955 *fds_ptr = NULL;
956 }
957 return 0;
958}
959
26edcf41
TS
960static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
961 const fd_set *fds,
962 int n)
31e31b8a 963{
31e31b8a 964 int i, nw, j, k;
992f48a0 965 abi_long v;
26edcf41 966 abi_ulong *target_fds;
31e31b8a 967
b1b2db29 968 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
26edcf41
TS
969 if (!(target_fds = lock_user(VERIFY_WRITE,
970 target_fds_addr,
971 sizeof(abi_ulong) * nw,
972 0)))
973 return -TARGET_EFAULT;
974
975 k = 0;
976 for (i = 0; i < nw; i++) {
977 v = 0;
978 for (j = 0; j < TARGET_ABI_BITS; j++) {
9ab709be 979 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
26edcf41 980 k++;
31e31b8a 981 }
26edcf41 982 __put_user(v, &target_fds[i]);
31e31b8a 983 }
26edcf41
TS
984
985 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
986
987 return 0;
31e31b8a
FB
988}
989
c596ed17
FB
990#if defined(__alpha__)
991#define HOST_HZ 1024
992#else
993#define HOST_HZ 100
994#endif
995
992f48a0 996static inline abi_long host_to_target_clock_t(long ticks)
c596ed17
FB
997{
998#if HOST_HZ == TARGET_HZ
999 return ticks;
1000#else
1001 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
1002#endif
1003}
1004
579a97f7
FB
1005static inline abi_long host_to_target_rusage(abi_ulong target_addr,
1006 const struct rusage *rusage)
b409186b 1007{
53a5960a
PB
1008 struct target_rusage *target_rusage;
1009
579a97f7
FB
1010 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
1011 return -TARGET_EFAULT;
cbb21eed
MB
1012 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
1013 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
1014 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
1015 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
1016 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
1017 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
1018 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
1019 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
1020 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
1021 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
1022 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
1023 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
1024 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
1025 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
1026 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
1027 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
1028 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
1029 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
53a5960a 1030 unlock_user_struct(target_rusage, target_addr, 1);
579a97f7
FB
1031
1032 return 0;
b409186b
FB
1033}
1034
cbb21eed 1035static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
81bbe906 1036{
cbb21eed 1037 abi_ulong target_rlim_swap;
95b33b2f
WT
1038 rlim_t result;
1039
cbb21eed
MB
1040 target_rlim_swap = tswapal(target_rlim);
1041 if (target_rlim_swap == TARGET_RLIM_INFINITY)
1042 return RLIM_INFINITY;
1043
1044 result = target_rlim_swap;
1045 if (target_rlim_swap != (rlim_t)result)
1046 return RLIM_INFINITY;
95b33b2f
WT
1047
1048 return result;
81bbe906
TY
1049}
1050
cbb21eed 1051static inline abi_ulong host_to_target_rlim(rlim_t rlim)
81bbe906 1052{
cbb21eed
MB
1053 abi_ulong target_rlim_swap;
1054 abi_ulong result;
95b33b2f 1055
cbb21eed 1056 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
95b33b2f 1057 target_rlim_swap = TARGET_RLIM_INFINITY;
81bbe906 1058 else
95b33b2f 1059 target_rlim_swap = rlim;
cbb21eed 1060 result = tswapal(target_rlim_swap);
95b33b2f
WT
1061
1062 return result;
81bbe906
TY
1063}
1064
e22b7015
WT
1065static inline int target_to_host_resource(int code)
1066{
1067 switch (code) {
1068 case TARGET_RLIMIT_AS:
1069 return RLIMIT_AS;
1070 case TARGET_RLIMIT_CORE:
1071 return RLIMIT_CORE;
1072 case TARGET_RLIMIT_CPU:
1073 return RLIMIT_CPU;
1074 case TARGET_RLIMIT_DATA:
1075 return RLIMIT_DATA;
1076 case TARGET_RLIMIT_FSIZE:
1077 return RLIMIT_FSIZE;
1078 case TARGET_RLIMIT_LOCKS:
1079 return RLIMIT_LOCKS;
1080 case TARGET_RLIMIT_MEMLOCK:
1081 return RLIMIT_MEMLOCK;
1082 case TARGET_RLIMIT_MSGQUEUE:
1083 return RLIMIT_MSGQUEUE;
1084 case TARGET_RLIMIT_NICE:
1085 return RLIMIT_NICE;
1086 case TARGET_RLIMIT_NOFILE:
1087 return RLIMIT_NOFILE;
1088 case TARGET_RLIMIT_NPROC:
1089 return RLIMIT_NPROC;
1090 case TARGET_RLIMIT_RSS:
1091 return RLIMIT_RSS;
1092 case TARGET_RLIMIT_RTPRIO:
1093 return RLIMIT_RTPRIO;
1094 case TARGET_RLIMIT_SIGPENDING:
1095 return RLIMIT_SIGPENDING;
1096 case TARGET_RLIMIT_STACK:
1097 return RLIMIT_STACK;
1098 default:
1099 return code;
1100 }
1101}
1102
788f5ec4
TS
1103static inline abi_long copy_from_user_timeval(struct timeval *tv,
1104 abi_ulong target_tv_addr)
31e31b8a 1105{
53a5960a
PB
1106 struct target_timeval *target_tv;
1107
788f5ec4 1108 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
579a97f7 1109 return -TARGET_EFAULT;
788f5ec4
TS
1110
1111 __get_user(tv->tv_sec, &target_tv->tv_sec);
1112 __get_user(tv->tv_usec, &target_tv->tv_usec);
1113
1114 unlock_user_struct(target_tv, target_tv_addr, 0);
579a97f7
FB
1115
1116 return 0;
31e31b8a
FB
1117}
1118
788f5ec4
TS
1119static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1120 const struct timeval *tv)
31e31b8a 1121{
53a5960a
PB
1122 struct target_timeval *target_tv;
1123
788f5ec4 1124 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
579a97f7 1125 return -TARGET_EFAULT;
788f5ec4
TS
1126
1127 __put_user(tv->tv_sec, &target_tv->tv_sec);
1128 __put_user(tv->tv_usec, &target_tv->tv_usec);
1129
1130 unlock_user_struct(target_tv, target_tv_addr, 1);
579a97f7
FB
1131
1132 return 0;
31e31b8a
FB
1133}
1134
ef4467e9
PB
1135static inline abi_long copy_from_user_timezone(struct timezone *tz,
1136 abi_ulong target_tz_addr)
1137{
1138 struct target_timezone *target_tz;
1139
1140 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1141 return -TARGET_EFAULT;
1142 }
1143
1144 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1145 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1146
1147 unlock_user_struct(target_tz, target_tz_addr, 0);
1148
1149 return 0;
1150}
1151
8ec9cf89
NF
1152#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1153#include <mqueue.h>
1154
24e1003a
AJ
1155static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1156 abi_ulong target_mq_attr_addr)
1157{
1158 struct target_mq_attr *target_mq_attr;
1159
1160 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1161 target_mq_attr_addr, 1))
1162 return -TARGET_EFAULT;
1163
1164 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1165 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1166 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1167 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1168
1169 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1170
1171 return 0;
1172}
1173
1174static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1175 const struct mq_attr *attr)
1176{
1177 struct target_mq_attr *target_mq_attr;
1178
1179 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1180 target_mq_attr_addr, 0))
1181 return -TARGET_EFAULT;
1182
1183 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1184 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1185 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1186 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1187
1188 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1189
1190 return 0;
1191}
8ec9cf89 1192#endif
31e31b8a 1193
055e0906 1194#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
0da46a6e 1195/* do_select() must return target values and target errnos. */
992f48a0 1196static abi_long do_select(int n,
26edcf41
TS
1197 abi_ulong rfd_addr, abi_ulong wfd_addr,
1198 abi_ulong efd_addr, abi_ulong target_tv_addr)
31e31b8a
FB
1199{
1200 fd_set rfds, wfds, efds;
1201 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6df9d38d
PM
1202 struct timeval tv;
1203 struct timespec ts, *ts_ptr;
992f48a0 1204 abi_long ret;
31e31b8a 1205
055e0906
MF
1206 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1207 if (ret) {
1208 return ret;
53a5960a 1209 }
055e0906
MF
1210 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1211 if (ret) {
1212 return ret;
53a5960a 1213 }
055e0906
MF
1214 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1215 if (ret) {
1216 return ret;
53a5960a 1217 }
3b46e624 1218
26edcf41 1219 if (target_tv_addr) {
788f5ec4
TS
1220 if (copy_from_user_timeval(&tv, target_tv_addr))
1221 return -TARGET_EFAULT;
6df9d38d
PM
1222 ts.tv_sec = tv.tv_sec;
1223 ts.tv_nsec = tv.tv_usec * 1000;
1224 ts_ptr = &ts;
31e31b8a 1225 } else {
6df9d38d 1226 ts_ptr = NULL;
31e31b8a 1227 }
26edcf41 1228
6df9d38d
PM
1229 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1230 ts_ptr, NULL));
53a5960a 1231
26edcf41
TS
1232 if (!is_error(ret)) {
1233 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1234 return -TARGET_EFAULT;
1235 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1236 return -TARGET_EFAULT;
1237 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1238 return -TARGET_EFAULT;
31e31b8a 1239
6df9d38d
PM
1240 if (target_tv_addr) {
1241 tv.tv_sec = ts.tv_sec;
1242 tv.tv_usec = ts.tv_nsec / 1000;
1243 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1244 return -TARGET_EFAULT;
1245 }
1246 }
31e31b8a 1247 }
579a97f7 1248
31e31b8a
FB
1249 return ret;
1250}
055e0906 1251#endif
31e31b8a 1252
099d6b0f
RV
1253static abi_long do_pipe2(int host_pipe[], int flags)
1254{
1255#ifdef CONFIG_PIPE2
1256 return pipe2(host_pipe, flags);
1257#else
1258 return -ENOSYS;
1259#endif
1260}
1261
fb41a66e
RH
1262static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1263 int flags, int is_pipe2)
099d6b0f
RV
1264{
1265 int host_pipe[2];
1266 abi_long ret;
1267 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1268
1269 if (is_error(ret))
1270 return get_errno(ret);
fb41a66e
RH
1271
1272 /* Several targets have special calling conventions for the original
1273 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1274 if (!is_pipe2) {
1275#if defined(TARGET_ALPHA)
1276 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1277 return host_pipe[0];
1278#elif defined(TARGET_MIPS)
1279 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1280 return host_pipe[0];
1281#elif defined(TARGET_SH4)
597c0212 1282 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
fb41a66e 1283 return host_pipe[0];
82f05b69
PM
1284#elif defined(TARGET_SPARC)
1285 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1286 return host_pipe[0];
597c0212 1287#endif
fb41a66e
RH
1288 }
1289
099d6b0f
RV
1290 if (put_user_s32(host_pipe[0], pipedes)
1291 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1292 return -TARGET_EFAULT;
099d6b0f
RV
1293 return get_errno(ret);
1294}
1295
b975b83b
LL
1296static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1297 abi_ulong target_addr,
1298 socklen_t len)
1299{
1300 struct target_ip_mreqn *target_smreqn;
1301
1302 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1303 if (!target_smreqn)
1304 return -TARGET_EFAULT;
1305 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1306 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1307 if (len == sizeof(struct target_ip_mreqn))
cbb21eed 1308 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
b975b83b
LL
1309 unlock_user(target_smreqn, target_addr, 0);
1310
1311 return 0;
1312}
1313
7b36f782 1314static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
579a97f7
FB
1315 abi_ulong target_addr,
1316 socklen_t len)
7854b056 1317{
607175e0
AJ
1318 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1319 sa_family_t sa_family;
53a5960a
PB
1320 struct target_sockaddr *target_saddr;
1321
7b36f782
LV
1322 if (fd_trans_target_to_host_addr(fd)) {
1323 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1324 }
1325
579a97f7
FB
1326 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1327 if (!target_saddr)
1328 return -TARGET_EFAULT;
607175e0
AJ
1329
1330 sa_family = tswap16(target_saddr->sa_family);
1331
1332 /* Oops. The caller might send a incomplete sun_path; sun_path
1333 * must be terminated by \0 (see the manual page), but
1334 * unfortunately it is quite common to specify sockaddr_un
1335 * length as "strlen(x->sun_path)" while it should be
1336 * "strlen(...) + 1". We'll fix that here if needed.
1337 * Linux kernel has a similar feature.
1338 */
1339
1340 if (sa_family == AF_UNIX) {
1341 if (len < unix_maxlen && len > 0) {
1342 char *cp = (char*)target_saddr;
1343
1344 if ( cp[len-1] && !cp[len] )
1345 len++;
1346 }
1347 if (len > unix_maxlen)
1348 len = unix_maxlen;
1349 }
1350
53a5960a 1351 memcpy(addr, target_saddr, len);
607175e0 1352 addr->sa_family = sa_family;
6c5b5645
LV
1353 if (sa_family == AF_NETLINK) {
1354 struct sockaddr_nl *nladdr;
1355
1356 nladdr = (struct sockaddr_nl *)addr;
1357 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1358 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1359 } else if (sa_family == AF_PACKET) {
33a29b51
JT
1360 struct target_sockaddr_ll *lladdr;
1361
1362 lladdr = (struct target_sockaddr_ll *)addr;
1363 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1364 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1365 }
53a5960a 1366 unlock_user(target_saddr, target_addr, 0);
579a97f7
FB
1367
1368 return 0;
7854b056
FB
1369}
1370
579a97f7
FB
1371static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1372 struct sockaddr *addr,
1373 socklen_t len)
7854b056 1374{
53a5960a
PB
1375 struct target_sockaddr *target_saddr;
1376
579a97f7
FB
1377 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1378 if (!target_saddr)
1379 return -TARGET_EFAULT;
53a5960a
PB
1380 memcpy(target_saddr, addr, len);
1381 target_saddr->sa_family = tswap16(addr->sa_family);
6c5b5645
LV
1382 if (addr->sa_family == AF_NETLINK) {
1383 struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
1384 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1385 target_nl->nl_groups = tswap32(target_nl->nl_groups);
1386 }
53a5960a 1387 unlock_user(target_saddr, target_addr, len);
579a97f7
FB
1388
1389 return 0;
7854b056
FB
1390}
1391
5a4a898d
FB
1392static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1393 struct target_msghdr *target_msgh)
7854b056
FB
1394{
1395 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1396 abi_long msg_controllen;
1397 abi_ulong target_cmsg_addr;
ee104587 1398 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
7854b056 1399 socklen_t space = 0;
5a4a898d 1400
cbb21eed 1401 msg_controllen = tswapal(target_msgh->msg_controllen);
5a4a898d
FB
1402 if (msg_controllen < sizeof (struct target_cmsghdr))
1403 goto the_end;
cbb21eed 1404 target_cmsg_addr = tswapal(target_msgh->msg_control);
5a4a898d 1405 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
ee104587 1406 target_cmsg_start = target_cmsg;
5a4a898d
FB
1407 if (!target_cmsg)
1408 return -TARGET_EFAULT;
7854b056
FB
1409
1410 while (cmsg && target_cmsg) {
1411 void *data = CMSG_DATA(cmsg);
1412 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1413
cbb21eed 1414 int len = tswapal(target_cmsg->cmsg_len)
7854b056
FB
1415 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1416
1417 space += CMSG_SPACE(len);
1418 if (space > msgh->msg_controllen) {
1419 space -= CMSG_SPACE(len);
c2aeb258
PM
1420 /* This is a QEMU bug, since we allocated the payload
1421 * area ourselves (unlike overflow in host-to-target
1422 * conversion, which is just the guest giving us a buffer
1423 * that's too small). It can't happen for the payload types
1424 * we currently support; if it becomes an issue in future
1425 * we would need to improve our allocation strategy to
1426 * something more intelligent than "twice the size of the
1427 * target buffer we're reading from".
1428 */
31febb71 1429 gemu_log("Host cmsg overflow\n");
7854b056
FB
1430 break;
1431 }
1432
dbf4f796
PJ
1433 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1434 cmsg->cmsg_level = SOL_SOCKET;
1435 } else {
1436 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1437 }
7854b056
FB
1438 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1439 cmsg->cmsg_len = CMSG_LEN(len);
1440
30b8b68e 1441 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
7854b056
FB
1442 int *fd = (int *)data;
1443 int *target_fd = (int *)target_data;
1444 int i, numfds = len / sizeof(int);
1445
876e23cb
PM
1446 for (i = 0; i < numfds; i++) {
1447 __get_user(fd[i], target_fd + i);
1448 }
30b8b68e
AS
1449 } else if (cmsg->cmsg_level == SOL_SOCKET
1450 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1451 struct ucred *cred = (struct ucred *)data;
1452 struct target_ucred *target_cred =
1453 (struct target_ucred *)target_data;
1454
876e23cb
PM
1455 __get_user(cred->pid, &target_cred->pid);
1456 __get_user(cred->uid, &target_cred->uid);
1457 __get_user(cred->gid, &target_cred->gid);
30b8b68e
AS
1458 } else {
1459 gemu_log("Unsupported ancillary data: %d/%d\n",
1460 cmsg->cmsg_level, cmsg->cmsg_type);
1461 memcpy(data, target_data, len);
7854b056
FB
1462 }
1463
1464 cmsg = CMSG_NXTHDR(msgh, cmsg);
ee104587
JN
1465 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1466 target_cmsg_start);
7854b056 1467 }
5a4a898d
FB
1468 unlock_user(target_cmsg, target_cmsg_addr, 0);
1469 the_end:
7854b056 1470 msgh->msg_controllen = space;
5a4a898d 1471 return 0;
7854b056
FB
1472}
1473
5a4a898d
FB
1474static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1475 struct msghdr *msgh)
7854b056
FB
1476{
1477 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1478 abi_long msg_controllen;
1479 abi_ulong target_cmsg_addr;
ee104587 1480 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
7854b056
FB
1481 socklen_t space = 0;
1482
cbb21eed 1483 msg_controllen = tswapal(target_msgh->msg_controllen);
5a4a898d
FB
1484 if (msg_controllen < sizeof (struct target_cmsghdr))
1485 goto the_end;
cbb21eed 1486 target_cmsg_addr = tswapal(target_msgh->msg_control);
5a4a898d 1487 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
ee104587 1488 target_cmsg_start = target_cmsg;
5a4a898d
FB
1489 if (!target_cmsg)
1490 return -TARGET_EFAULT;
1491
7854b056
FB
1492 while (cmsg && target_cmsg) {
1493 void *data = CMSG_DATA(cmsg);
1494 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1495
1496 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
c2aeb258 1497 int tgt_len, tgt_space;
7854b056 1498
c2aeb258
PM
1499 /* We never copy a half-header but may copy half-data;
1500 * this is Linux's behaviour in put_cmsg(). Note that
1501 * truncation here is a guest problem (which we report
1502 * to the guest via the CTRUNC bit), unlike truncation
1503 * in target_to_host_cmsg, which is a QEMU bug.
1504 */
1505 if (msg_controllen < sizeof(struct cmsghdr)) {
1506 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
7854b056
FB
1507 break;
1508 }
1509
dbf4f796
PJ
1510 if (cmsg->cmsg_level == SOL_SOCKET) {
1511 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1512 } else {
1513 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1514 }
7854b056 1515 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
7854b056 1516
c2aeb258
PM
1517 tgt_len = TARGET_CMSG_LEN(len);
1518
1519 /* Payload types which need a different size of payload on
1520 * the target must adjust tgt_len here.
1521 */
1522 switch (cmsg->cmsg_level) {
1523 case SOL_SOCKET:
1524 switch (cmsg->cmsg_type) {
1525 case SO_TIMESTAMP:
1526 tgt_len = sizeof(struct target_timeval);
1527 break;
1528 default:
1529 break;
1530 }
1531 default:
1532 break;
1533 }
1534
1535 if (msg_controllen < tgt_len) {
1536 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1537 tgt_len = msg_controllen;
1538 }
1539
1540 /* We must now copy-and-convert len bytes of payload
1541 * into tgt_len bytes of destination space. Bear in mind
1542 * that in both source and destination we may be dealing
1543 * with a truncated value!
1544 */
52b65494
HD
1545 switch (cmsg->cmsg_level) {
1546 case SOL_SOCKET:
1547 switch (cmsg->cmsg_type) {
1548 case SCM_RIGHTS:
1549 {
1550 int *fd = (int *)data;
1551 int *target_fd = (int *)target_data;
c2aeb258 1552 int i, numfds = tgt_len / sizeof(int);
52b65494 1553
876e23cb
PM
1554 for (i = 0; i < numfds; i++) {
1555 __put_user(fd[i], target_fd + i);
1556 }
52b65494
HD
1557 break;
1558 }
1559 case SO_TIMESTAMP:
1560 {
1561 struct timeval *tv = (struct timeval *)data;
1562 struct target_timeval *target_tv =
1563 (struct target_timeval *)target_data;
1564
c2aeb258
PM
1565 if (len != sizeof(struct timeval) ||
1566 tgt_len != sizeof(struct target_timeval)) {
52b65494 1567 goto unimplemented;
c2aeb258 1568 }
52b65494
HD
1569
1570 /* copy struct timeval to target */
876e23cb
PM
1571 __put_user(tv->tv_sec, &target_tv->tv_sec);
1572 __put_user(tv->tv_usec, &target_tv->tv_usec);
52b65494
HD
1573 break;
1574 }
4bc29756
HD
1575 case SCM_CREDENTIALS:
1576 {
1577 struct ucred *cred = (struct ucred *)data;
1578 struct target_ucred *target_cred =
1579 (struct target_ucred *)target_data;
1580
1581 __put_user(cred->pid, &target_cred->pid);
1582 __put_user(cred->uid, &target_cred->uid);
1583 __put_user(cred->gid, &target_cred->gid);
1584 break;
1585 }
52b65494
HD
1586 default:
1587 goto unimplemented;
1588 }
1589 break;
7854b056 1590
52b65494
HD
1591 default:
1592 unimplemented:
aebf5bc7
JH
1593 gemu_log("Unsupported ancillary data: %d/%d\n",
1594 cmsg->cmsg_level, cmsg->cmsg_type);
c2aeb258
PM
1595 memcpy(target_data, data, MIN(len, tgt_len));
1596 if (tgt_len > len) {
1597 memset(target_data + len, 0, tgt_len - len);
1598 }
7854b056
FB
1599 }
1600
c2aeb258 1601 target_cmsg->cmsg_len = tswapal(tgt_len);
ee104587 1602 tgt_space = TARGET_CMSG_SPACE(len);
c2aeb258
PM
1603 if (msg_controllen < tgt_space) {
1604 tgt_space = msg_controllen;
1605 }
1606 msg_controllen -= tgt_space;
1607 space += tgt_space;
7854b056 1608 cmsg = CMSG_NXTHDR(msgh, cmsg);
ee104587
JN
1609 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1610 target_cmsg_start);
7854b056 1611 }
5a4a898d
FB
1612 unlock_user(target_cmsg, target_cmsg_addr, space);
1613 the_end:
cbb21eed 1614 target_msgh->msg_controllen = tswapal(space);
5a4a898d 1615 return 0;
7854b056
FB
1616}
1617
6c5b5645
LV
1618static void tswap_nlmsghdr(struct nlmsghdr *nlh)
1619{
1620 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
1621 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
1622 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
1623 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
1624 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
1625}
1626
1627static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
1628 size_t len,
1629 abi_long (*host_to_target_nlmsg)
1630 (struct nlmsghdr *))
1631{
1632 uint32_t nlmsg_len;
1633 abi_long ret;
1634
1635 while (len > sizeof(struct nlmsghdr)) {
1636
1637 nlmsg_len = nlh->nlmsg_len;
1638 if (nlmsg_len < sizeof(struct nlmsghdr) ||
1639 nlmsg_len > len) {
1640 break;
1641 }
1642
1643 switch (nlh->nlmsg_type) {
1644 case NLMSG_DONE:
1645 tswap_nlmsghdr(nlh);
1646 return 0;
1647 case NLMSG_NOOP:
1648 break;
1649 case NLMSG_ERROR:
1650 {
1651 struct nlmsgerr *e = NLMSG_DATA(nlh);
1652 e->error = tswap32(e->error);
1653 tswap_nlmsghdr(&e->msg);
1654 tswap_nlmsghdr(nlh);
1655 return 0;
1656 }
1657 default:
1658 ret = host_to_target_nlmsg(nlh);
1659 if (ret < 0) {
1660 tswap_nlmsghdr(nlh);
1661 return ret;
1662 }
1663 break;
1664 }
1665 tswap_nlmsghdr(nlh);
1666 len -= NLMSG_ALIGN(nlmsg_len);
1667 nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len));
1668 }
1669 return 0;
1670}
1671
1672static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
1673 size_t len,
1674 abi_long (*target_to_host_nlmsg)
1675 (struct nlmsghdr *))
1676{
1677 int ret;
1678
1679 while (len > sizeof(struct nlmsghdr)) {
1680 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
1681 tswap32(nlh->nlmsg_len) > len) {
1682 break;
1683 }
1684 tswap_nlmsghdr(nlh);
1685 switch (nlh->nlmsg_type) {
1686 case NLMSG_DONE:
1687 return 0;
1688 case NLMSG_NOOP:
1689 break;
1690 case NLMSG_ERROR:
1691 {
1692 struct nlmsgerr *e = NLMSG_DATA(nlh);
1693 e->error = tswap32(e->error);
1694 tswap_nlmsghdr(&e->msg);
84f34b00 1695 return 0;
6c5b5645
LV
1696 }
1697 default:
1698 ret = target_to_host_nlmsg(nlh);
1699 if (ret < 0) {
1700 return ret;
1701 }
1702 }
1703 len -= NLMSG_ALIGN(nlh->nlmsg_len);
1704 nlh = (struct nlmsghdr *)(((char *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1705 }
1706 return 0;
1707}
1708
575b22b1 1709#ifdef CONFIG_RTNETLINK
6c5b5645
LV
1710static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
1711 size_t len,
1712 abi_long (*host_to_target_rtattr)
1713 (struct rtattr *))
1714{
1715 unsigned short rta_len;
1716 abi_long ret;
1717
1718 while (len > sizeof(struct rtattr)) {
1719 rta_len = rtattr->rta_len;
1720 if (rta_len < sizeof(struct rtattr) ||
1721 rta_len > len) {
1722 break;
1723 }
1724 ret = host_to_target_rtattr(rtattr);
1725 rtattr->rta_len = tswap16(rtattr->rta_len);
1726 rtattr->rta_type = tswap16(rtattr->rta_type);
1727 if (ret < 0) {
1728 return ret;
1729 }
1730 len -= RTA_ALIGN(rta_len);
1731 rtattr = (struct rtattr *)(((char *)rtattr) + RTA_ALIGN(rta_len));
1732 }
1733 return 0;
1734}
1735
1736static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
1737{
1738 uint32_t *u32;
1739 struct rtnl_link_stats *st;
1740 struct rtnl_link_stats64 *st64;
1741 struct rtnl_link_ifmap *map;
1742
1743 switch (rtattr->rta_type) {
1744 /* binary stream */
1745 case IFLA_ADDRESS:
1746 case IFLA_BROADCAST:
1747 /* string */
1748 case IFLA_IFNAME:
1749 case IFLA_QDISC:
1750 break;
1751 /* uin8_t */
1752 case IFLA_OPERSTATE:
1753 case IFLA_LINKMODE:
1754 case IFLA_CARRIER:
1755 case IFLA_PROTO_DOWN:
1756 break;
1757 /* uint32_t */
1758 case IFLA_MTU:
1759 case IFLA_LINK:
1760 case IFLA_WEIGHT:
1761 case IFLA_TXQLEN:
1762 case IFLA_CARRIER_CHANGES:
1763 case IFLA_NUM_RX_QUEUES:
1764 case IFLA_NUM_TX_QUEUES:
1765 case IFLA_PROMISCUITY:
1766 case IFLA_EXT_MASK:
1767 case IFLA_LINK_NETNSID:
1768 case IFLA_GROUP:
1769 case IFLA_MASTER:
1770 case IFLA_NUM_VF:
1771 u32 = RTA_DATA(rtattr);
1772 *u32 = tswap32(*u32);
1773 break;
1774 /* struct rtnl_link_stats */
1775 case IFLA_STATS:
1776 st = RTA_DATA(rtattr);
1777 st->rx_packets = tswap32(st->rx_packets);
1778 st->tx_packets = tswap32(st->tx_packets);
1779 st->rx_bytes = tswap32(st->rx_bytes);
1780 st->tx_bytes = tswap32(st->tx_bytes);
1781 st->rx_errors = tswap32(st->rx_errors);
1782 st->tx_errors = tswap32(st->tx_errors);
1783 st->rx_dropped = tswap32(st->rx_dropped);
1784 st->tx_dropped = tswap32(st->tx_dropped);
1785 st->multicast = tswap32(st->multicast);
1786 st->collisions = tswap32(st->collisions);
1787
1788 /* detailed rx_errors: */
1789 st->rx_length_errors = tswap32(st->rx_length_errors);
1790 st->rx_over_errors = tswap32(st->rx_over_errors);
1791 st->rx_crc_errors = tswap32(st->rx_crc_errors);
1792 st->rx_frame_errors = tswap32(st->rx_frame_errors);
1793 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
1794 st->rx_missed_errors = tswap32(st->rx_missed_errors);
1795
1796 /* detailed tx_errors */
1797 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
1798 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
1799 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
1800 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
1801 st->tx_window_errors = tswap32(st->tx_window_errors);
1802
1803 /* for cslip etc */
1804 st->rx_compressed = tswap32(st->rx_compressed);
1805 st->tx_compressed = tswap32(st->tx_compressed);
1806 break;
1807 /* struct rtnl_link_stats64 */
1808 case IFLA_STATS64:
1809 st64 = RTA_DATA(rtattr);
1810 st64->rx_packets = tswap64(st64->rx_packets);
1811 st64->tx_packets = tswap64(st64->tx_packets);
1812 st64->rx_bytes = tswap64(st64->rx_bytes);
1813 st64->tx_bytes = tswap64(st64->tx_bytes);
1814 st64->rx_errors = tswap64(st64->rx_errors);
1815 st64->tx_errors = tswap64(st64->tx_errors);
1816 st64->rx_dropped = tswap64(st64->rx_dropped);
1817 st64->tx_dropped = tswap64(st64->tx_dropped);
1818 st64->multicast = tswap64(st64->multicast);
1819 st64->collisions = tswap64(st64->collisions);
1820
1821 /* detailed rx_errors: */
1822 st64->rx_length_errors = tswap64(st64->rx_length_errors);
1823 st64->rx_over_errors = tswap64(st64->rx_over_errors);
1824 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
1825 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
1826 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
1827 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
1828
1829 /* detailed tx_errors */
1830 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
1831 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
1832 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
1833 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
1834 st64->tx_window_errors = tswap64(st64->tx_window_errors);
1835
1836 /* for cslip etc */
1837 st64->rx_compressed = tswap64(st64->rx_compressed);
1838 st64->tx_compressed = tswap64(st64->tx_compressed);
1839 break;
1840 /* struct rtnl_link_ifmap */
1841 case IFLA_MAP:
1842 map = RTA_DATA(rtattr);
1843 map->mem_start = tswap64(map->mem_start);
1844 map->mem_end = tswap64(map->mem_end);
1845 map->base_addr = tswap64(map->base_addr);
1846 map->irq = tswap16(map->irq);
1847 break;
1848 /* nested */
1849 case IFLA_AF_SPEC:
1850 case IFLA_LINKINFO:
1851 /* FIXME: implement nested type */
1852 gemu_log("Unimplemented nested type %d\n", rtattr->rta_type);
1853 break;
1854 default:
1855 gemu_log("Unknown host IFLA type: %d\n", rtattr->rta_type);
1856 break;
1857 }
1858 return 0;
1859}
1860
1861static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
1862{
1863 uint32_t *u32;
1864 struct ifa_cacheinfo *ci;
1865
1866 switch (rtattr->rta_type) {
1867 /* binary: depends on family type */
1868 case IFA_ADDRESS:
1869 case IFA_LOCAL:
1870 break;
1871 /* string */
1872 case IFA_LABEL:
1873 break;
1874 /* u32 */
1875 case IFA_FLAGS:
1876 case IFA_BROADCAST:
1877 u32 = RTA_DATA(rtattr);
1878 *u32 = tswap32(*u32);
1879 break;
1880 /* struct ifa_cacheinfo */
1881 case IFA_CACHEINFO:
1882 ci = RTA_DATA(rtattr);
1883 ci->ifa_prefered = tswap32(ci->ifa_prefered);
1884 ci->ifa_valid = tswap32(ci->ifa_valid);
1885 ci->cstamp = tswap32(ci->cstamp);
1886 ci->tstamp = tswap32(ci->tstamp);
1887 break;
1888 default:
1889 gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type);
1890 break;
1891 }
1892 return 0;
1893}
1894
1895static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
1896{
1897 uint32_t *u32;
1898 switch (rtattr->rta_type) {
1899 /* binary: depends on family type */
1900 case RTA_GATEWAY:
1901 case RTA_DST:
1902 case RTA_PREFSRC:
1903 break;
1904 /* u32 */
1905 case RTA_PRIORITY:
1906 case RTA_TABLE:
1907 case RTA_OIF:
1908 u32 = RTA_DATA(rtattr);
1909 *u32 = tswap32(*u32);
1910 break;
1911 default:
1912 gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type);
1913 break;
1914 }
1915 return 0;
1916}
1917
1918static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
1919 uint32_t rtattr_len)
1920{
1921 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1922 host_to_target_data_link_rtattr);
1923}
1924
1925static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
1926 uint32_t rtattr_len)
1927{
1928 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1929 host_to_target_data_addr_rtattr);
1930}
1931
1932static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
1933 uint32_t rtattr_len)
1934{
1935 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
1936 host_to_target_data_route_rtattr);
1937}
1938
1939static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
1940{
1941 uint32_t nlmsg_len;
1942 struct ifinfomsg *ifi;
1943 struct ifaddrmsg *ifa;
1944 struct rtmsg *rtm;
1945
1946 nlmsg_len = nlh->nlmsg_len;
1947 switch (nlh->nlmsg_type) {
1948 case RTM_NEWLINK:
1949 case RTM_DELLINK:
1950 case RTM_GETLINK:
b9403979
LV
1951 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
1952 ifi = NLMSG_DATA(nlh);
1953 ifi->ifi_type = tswap16(ifi->ifi_type);
1954 ifi->ifi_index = tswap32(ifi->ifi_index);
1955 ifi->ifi_flags = tswap32(ifi->ifi_flags);
1956 ifi->ifi_change = tswap32(ifi->ifi_change);
1957 host_to_target_link_rtattr(IFLA_RTA(ifi),
1958 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
1959 }
6c5b5645
LV
1960 break;
1961 case RTM_NEWADDR:
1962 case RTM_DELADDR:
1963 case RTM_GETADDR:
b9403979
LV
1964 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
1965 ifa = NLMSG_DATA(nlh);
1966 ifa->ifa_index = tswap32(ifa->ifa_index);
1967 host_to_target_addr_rtattr(IFA_RTA(ifa),
1968 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
1969 }
6c5b5645
LV
1970 break;
1971 case RTM_NEWROUTE:
1972 case RTM_DELROUTE:
1973 case RTM_GETROUTE:
b9403979
LV
1974 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
1975 rtm = NLMSG_DATA(nlh);
1976 rtm->rtm_flags = tswap32(rtm->rtm_flags);
1977 host_to_target_route_rtattr(RTM_RTA(rtm),
1978 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
1979 }
6c5b5645
LV
1980 break;
1981 default:
1982 return -TARGET_EINVAL;
1983 }
1984 return 0;
1985}
1986
1987static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
1988 size_t len)
1989{
1990 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
1991}
1992
1993static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
1994 size_t len,
1995 abi_long (*target_to_host_rtattr)
1996 (struct rtattr *))
1997{
1998 abi_long ret;
1999
2000 while (len >= sizeof(struct rtattr)) {
2001 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
2002 tswap16(rtattr->rta_len) > len) {
2003 break;
2004 }
2005 rtattr->rta_len = tswap16(rtattr->rta_len);
2006 rtattr->rta_type = tswap16(rtattr->rta_type);
2007 ret = target_to_host_rtattr(rtattr);
2008 if (ret < 0) {
2009 return ret;
2010 }
2011 len -= RTA_ALIGN(rtattr->rta_len);
2012 rtattr = (struct rtattr *)(((char *)rtattr) +
2013 RTA_ALIGN(rtattr->rta_len));
2014 }
2015 return 0;
2016}
2017
2018static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
2019{
2020 switch (rtattr->rta_type) {
2021 default:
2022 gemu_log("Unknown target IFLA type: %d\n", rtattr->rta_type);
2023 break;
2024 }
2025 return 0;
2026}
2027
2028static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
2029{
2030 switch (rtattr->rta_type) {
2031 /* binary: depends on family type */
2032 case IFA_LOCAL:
2033 case IFA_ADDRESS:
2034 break;
2035 default:
2036 gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type);
2037 break;
2038 }
2039 return 0;
2040}
2041
2042static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
2043{
2044 uint32_t *u32;
2045 switch (rtattr->rta_type) {
2046 /* binary: depends on family type */
2047 case RTA_DST:
2048 case RTA_SRC:
2049 case RTA_GATEWAY:
2050 break;
2051 /* u32 */
2052 case RTA_OIF:
2053 u32 = RTA_DATA(rtattr);
2054 *u32 = tswap32(*u32);
2055 break;
2056 default:
2057 gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type);
2058 break;
2059 }
2060 return 0;
2061}
2062
2063static void target_to_host_link_rtattr(struct rtattr *rtattr,
2064 uint32_t rtattr_len)
2065{
2066 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2067 target_to_host_data_link_rtattr);
2068}
2069
2070static void target_to_host_addr_rtattr(struct rtattr *rtattr,
2071 uint32_t rtattr_len)
2072{
2073 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2074 target_to_host_data_addr_rtattr);
2075}
2076
2077static void target_to_host_route_rtattr(struct rtattr *rtattr,
2078 uint32_t rtattr_len)
2079{
2080 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2081 target_to_host_data_route_rtattr);
2082}
2083
2084static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
2085{
2086 struct ifinfomsg *ifi;
2087 struct ifaddrmsg *ifa;
2088 struct rtmsg *rtm;
2089
2090 switch (nlh->nlmsg_type) {
2091 case RTM_GETLINK:
2092 break;
2093 case RTM_NEWLINK:
2094 case RTM_DELLINK:
b9403979
LV
2095 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
2096 ifi = NLMSG_DATA(nlh);
2097 ifi->ifi_type = tswap16(ifi->ifi_type);
2098 ifi->ifi_index = tswap32(ifi->ifi_index);
2099 ifi->ifi_flags = tswap32(ifi->ifi_flags);
2100 ifi->ifi_change = tswap32(ifi->ifi_change);
2101 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
2102 NLMSG_LENGTH(sizeof(*ifi)));
2103 }
6c5b5645
LV
2104 break;
2105 case RTM_GETADDR:
2106 case RTM_NEWADDR:
2107 case RTM_DELADDR:
b9403979
LV
2108 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
2109 ifa = NLMSG_DATA(nlh);
2110 ifa->ifa_index = tswap32(ifa->ifa_index);
2111 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
2112 NLMSG_LENGTH(sizeof(*ifa)));
2113 }
6c5b5645
LV
2114 break;
2115 case RTM_GETROUTE:
2116 break;
2117 case RTM_NEWROUTE:
2118 case RTM_DELROUTE:
b9403979
LV
2119 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
2120 rtm = NLMSG_DATA(nlh);
2121 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2122 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
2123 NLMSG_LENGTH(sizeof(*rtm)));
2124 }
6c5b5645
LV
2125 break;
2126 default:
2127 return -TARGET_EOPNOTSUPP;
2128 }
2129 return 0;
2130}
2131
2132static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
2133{
2134 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
2135}
575b22b1 2136#endif /* CONFIG_RTNETLINK */
6c5b5645 2137
5ce9bb59
LV
2138static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
2139{
2140 switch (nlh->nlmsg_type) {
2141 default:
2142 gemu_log("Unknown host audit message type %d\n",
2143 nlh->nlmsg_type);
2144 return -TARGET_EINVAL;
2145 }
2146 return 0;
2147}
2148
2149static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
2150 size_t len)
2151{
2152 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
2153}
2154
2155static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
2156{
2157 switch (nlh->nlmsg_type) {
2158 case AUDIT_USER:
2159 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
2160 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
2161 break;
2162 default:
2163 gemu_log("Unknown target audit message type %d\n",
2164 nlh->nlmsg_type);
2165 return -TARGET_EINVAL;
2166 }
2167
2168 return 0;
2169}
2170
2171static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
2172{
2173 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
2174}
2175
0da46a6e 2176/* do_setsockopt() Must return target values and target errnos. */
992f48a0 2177static abi_long do_setsockopt(int sockfd, int level, int optname,
2f619698 2178 abi_ulong optval_addr, socklen_t optlen)
7854b056 2179{
992f48a0 2180 abi_long ret;
32407103 2181 int val;
b975b83b 2182 struct ip_mreqn *ip_mreq;
6e3cb58f 2183 struct ip_mreq_source *ip_mreq_source;
3b46e624 2184
8853f86e
FB
2185 switch(level) {
2186 case SOL_TCP:
7854b056 2187 /* TCP options all take an 'int' value. */
7854b056 2188 if (optlen < sizeof(uint32_t))
0da46a6e 2189 return -TARGET_EINVAL;
3b46e624 2190
2f619698
FB
2191 if (get_user_u32(val, optval_addr))
2192 return -TARGET_EFAULT;
8853f86e
FB
2193 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2194 break;
2195 case SOL_IP:
2196 switch(optname) {
2efbe911
FB
2197 case IP_TOS:
2198 case IP_TTL:
8853f86e 2199 case IP_HDRINCL:
2efbe911
FB
2200 case IP_ROUTER_ALERT:
2201 case IP_RECVOPTS:
2202 case IP_RETOPTS:
2203 case IP_PKTINFO:
2204 case IP_MTU_DISCOVER:
2205 case IP_RECVERR:
2206 case IP_RECVTOS:
2207#ifdef IP_FREEBIND
2208 case IP_FREEBIND:
2209#endif
2210 case IP_MULTICAST_TTL:
2211 case IP_MULTICAST_LOOP:
8853f86e
FB
2212 val = 0;
2213 if (optlen >= sizeof(uint32_t)) {
2f619698
FB
2214 if (get_user_u32(val, optval_addr))
2215 return -TARGET_EFAULT;
8853f86e 2216 } else if (optlen >= 1) {
2f619698
FB
2217 if (get_user_u8(val, optval_addr))
2218 return -TARGET_EFAULT;
8853f86e
FB
2219 }
2220 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2221 break;
b975b83b
LL
2222 case IP_ADD_MEMBERSHIP:
2223 case IP_DROP_MEMBERSHIP:
2224 if (optlen < sizeof (struct target_ip_mreq) ||
2225 optlen > sizeof (struct target_ip_mreqn))
2226 return -TARGET_EINVAL;
2227
2228 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2229 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2230 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2231 break;
2232
6e3cb58f
LL
2233 case IP_BLOCK_SOURCE:
2234 case IP_UNBLOCK_SOURCE:
2235 case IP_ADD_SOURCE_MEMBERSHIP:
2236 case IP_DROP_SOURCE_MEMBERSHIP:
2237 if (optlen != sizeof (struct target_ip_mreq_source))
2238 return -TARGET_EINVAL;
2239
2240 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2241 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2242 unlock_user (ip_mreq_source, optval_addr, 0);
2243 break;
2244
920394db
JH
2245 default:
2246 goto unimplemented;
2247 }
2248 break;
0d78b3b5
LV
2249 case SOL_IPV6:
2250 switch (optname) {
2251 case IPV6_MTU_DISCOVER:
2252 case IPV6_MTU:
2253 case IPV6_V6ONLY:
2254 case IPV6_RECVPKTINFO:
2255 val = 0;
2256 if (optlen < sizeof(uint32_t)) {
2257 return -TARGET_EINVAL;
2258 }
2259 if (get_user_u32(val, optval_addr)) {
2260 return -TARGET_EFAULT;
2261 }
2262 ret = get_errno(setsockopt(sockfd, level, optname,
2263 &val, sizeof(val)));
2264 break;
2265 default:
2266 goto unimplemented;
2267 }
2268 break;
920394db
JH
2269 case SOL_RAW:
2270 switch (optname) {
2271 case ICMP_FILTER:
2272 /* struct icmp_filter takes an u32 value */
2273 if (optlen < sizeof(uint32_t)) {
2274 return -TARGET_EINVAL;
2275 }
2276
2277 if (get_user_u32(val, optval_addr)) {
2278 return -TARGET_EFAULT;
2279 }
2280 ret = get_errno(setsockopt(sockfd, level, optname,
2281 &val, sizeof(val)));
2282 break;
2283
8853f86e
FB
2284 default:
2285 goto unimplemented;
2286 }
2287 break;
3532fa74 2288 case TARGET_SOL_SOCKET:
8853f86e 2289 switch (optname) {
1b09aeb9
LV
2290 case TARGET_SO_RCVTIMEO:
2291 {
2292 struct timeval tv;
2293
2294 optname = SO_RCVTIMEO;
2295
2296set_timeout:
2297 if (optlen != sizeof(struct target_timeval)) {
2298 return -TARGET_EINVAL;
2299 }
2300
2301 if (copy_from_user_timeval(&tv, optval_addr)) {
2302 return -TARGET_EFAULT;
2303 }
2304
2305 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2306 &tv, sizeof(tv)));
2307 return ret;
2308 }
2309 case TARGET_SO_SNDTIMEO:
2310 optname = SO_SNDTIMEO;
2311 goto set_timeout;
f57d4192
LV
2312 case TARGET_SO_ATTACH_FILTER:
2313 {
2314 struct target_sock_fprog *tfprog;
2315 struct target_sock_filter *tfilter;
2316 struct sock_fprog fprog;
2317 struct sock_filter *filter;
2318 int i;
2319
2320 if (optlen != sizeof(*tfprog)) {
2321 return -TARGET_EINVAL;
2322 }
2323 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2324 return -TARGET_EFAULT;
2325 }
2326 if (!lock_user_struct(VERIFY_READ, tfilter,
2327 tswapal(tfprog->filter), 0)) {
2328 unlock_user_struct(tfprog, optval_addr, 1);
2329 return -TARGET_EFAULT;
2330 }
2331
2332 fprog.len = tswap16(tfprog->len);
0e173b24 2333 filter = g_try_new(struct sock_filter, fprog.len);
f57d4192
LV
2334 if (filter == NULL) {
2335 unlock_user_struct(tfilter, tfprog->filter, 1);
2336 unlock_user_struct(tfprog, optval_addr, 1);
2337 return -TARGET_ENOMEM;
2338 }
2339 for (i = 0; i < fprog.len; i++) {
2340 filter[i].code = tswap16(tfilter[i].code);
2341 filter[i].jt = tfilter[i].jt;
2342 filter[i].jf = tfilter[i].jf;
2343 filter[i].k = tswap32(tfilter[i].k);
2344 }
2345 fprog.filter = filter;
2346
2347 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2348 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
0e173b24 2349 g_free(filter);
f57d4192
LV
2350
2351 unlock_user_struct(tfilter, tfprog->filter, 1);
2352 unlock_user_struct(tfprog, optval_addr, 1);
2353 return ret;
2354 }
451aaf68
JT
2355 case TARGET_SO_BINDTODEVICE:
2356 {
2357 char *dev_ifname, *addr_ifname;
2358
2359 if (optlen > IFNAMSIZ - 1) {
2360 optlen = IFNAMSIZ - 1;
2361 }
2362 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2363 if (!dev_ifname) {
2364 return -TARGET_EFAULT;
2365 }
2366 optname = SO_BINDTODEVICE;
2367 addr_ifname = alloca(IFNAMSIZ);
2368 memcpy(addr_ifname, dev_ifname, optlen);
2369 addr_ifname[optlen] = 0;
fad6c58a
CG
2370 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2371 addr_ifname, optlen));
451aaf68
JT
2372 unlock_user (dev_ifname, optval_addr, 0);
2373 return ret;
2374 }
8853f86e 2375 /* Options with 'int' argument. */
3532fa74
FB
2376 case TARGET_SO_DEBUG:
2377 optname = SO_DEBUG;
2378 break;
2379 case TARGET_SO_REUSEADDR:
2380 optname = SO_REUSEADDR;
2381 break;
2382 case TARGET_SO_TYPE:
2383 optname = SO_TYPE;
2384 break;
2385 case TARGET_SO_ERROR:
2386 optname = SO_ERROR;
2387 break;
2388 case TARGET_SO_DONTROUTE:
2389 optname = SO_DONTROUTE;
2390 break;
2391 case TARGET_SO_BROADCAST:
2392 optname = SO_BROADCAST;
2393 break;
2394 case TARGET_SO_SNDBUF:
2395 optname = SO_SNDBUF;
2396 break;
d79b6cc4
PB
2397 case TARGET_SO_SNDBUFFORCE:
2398 optname = SO_SNDBUFFORCE;
2399 break;
3532fa74
FB
2400 case TARGET_SO_RCVBUF:
2401 optname = SO_RCVBUF;
2402 break;
d79b6cc4
PB
2403 case TARGET_SO_RCVBUFFORCE:
2404 optname = SO_RCVBUFFORCE;
2405 break;
3532fa74
FB
2406 case TARGET_SO_KEEPALIVE:
2407 optname = SO_KEEPALIVE;
2408 break;
2409 case TARGET_SO_OOBINLINE:
2410 optname = SO_OOBINLINE;
2411 break;
2412 case TARGET_SO_NO_CHECK:
2413 optname = SO_NO_CHECK;
2414 break;
2415 case TARGET_SO_PRIORITY:
2416 optname = SO_PRIORITY;
2417 break;
5e83e8e3 2418#ifdef SO_BSDCOMPAT
3532fa74
FB
2419 case TARGET_SO_BSDCOMPAT:
2420 optname = SO_BSDCOMPAT;
2421 break;
5e83e8e3 2422#endif
3532fa74
FB
2423 case TARGET_SO_PASSCRED:
2424 optname = SO_PASSCRED;
2425 break;
82d0fe6b
PB
2426 case TARGET_SO_PASSSEC:
2427 optname = SO_PASSSEC;
2428 break;
3532fa74
FB
2429 case TARGET_SO_TIMESTAMP:
2430 optname = SO_TIMESTAMP;
2431 break;
2432 case TARGET_SO_RCVLOWAT:
2433 optname = SO_RCVLOWAT;
2434 break;
8853f86e
FB
2435 break;
2436 default:
2437 goto unimplemented;
2438 }
3532fa74 2439 if (optlen < sizeof(uint32_t))
2f619698 2440 return -TARGET_EINVAL;
3532fa74 2441
2f619698
FB
2442 if (get_user_u32(val, optval_addr))
2443 return -TARGET_EFAULT;
3532fa74 2444 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
8853f86e 2445 break;
7854b056 2446 default:
8853f86e 2447 unimplemented:
b2bedb21 2448 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
6fa13c17 2449 ret = -TARGET_ENOPROTOOPT;
7854b056 2450 }
8853f86e 2451 return ret;
7854b056
FB
2452}
2453
0da46a6e 2454/* do_getsockopt() Must return target values and target errnos. */
992f48a0 2455static abi_long do_getsockopt(int sockfd, int level, int optname,
2f619698 2456 abi_ulong optval_addr, abi_ulong optlen)
7854b056 2457{
992f48a0 2458 abi_long ret;
b55266b5
BS
2459 int len, val;
2460 socklen_t lv;
8853f86e
FB
2461
2462 switch(level) {
3532fa74 2463 case TARGET_SOL_SOCKET:
f3b974cd
JL
2464 level = SOL_SOCKET;
2465 switch (optname) {
2466 /* These don't just return a single integer */
2467 case TARGET_SO_LINGER:
2468 case TARGET_SO_RCVTIMEO:
2469 case TARGET_SO_SNDTIMEO:
f3b974cd
JL
2470 case TARGET_SO_PEERNAME:
2471 goto unimplemented;
583359a6
AP
2472 case TARGET_SO_PEERCRED: {
2473 struct ucred cr;
2474 socklen_t crlen;
2475 struct target_ucred *tcr;
2476
2477 if (get_user_u32(len, optlen)) {
2478 return -TARGET_EFAULT;
2479 }
2480 if (len < 0) {
2481 return -TARGET_EINVAL;
2482 }
2483
2484 crlen = sizeof(cr);
2485 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2486 &cr, &crlen));
2487 if (ret < 0) {
2488 return ret;
2489 }
2490 if (len > crlen) {
2491 len = crlen;
2492 }
2493 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2494 return -TARGET_EFAULT;
2495 }
2496 __put_user(cr.pid, &tcr->pid);
2497 __put_user(cr.uid, &tcr->uid);
2498 __put_user(cr.gid, &tcr->gid);
2499 unlock_user_struct(tcr, optval_addr, 1);
2500 if (put_user_u32(len, optlen)) {
2501 return -TARGET_EFAULT;
2502 }
2503 break;
2504 }
f3b974cd
JL
2505 /* Options with 'int' argument. */
2506 case TARGET_SO_DEBUG:
2507 optname = SO_DEBUG;
2508 goto int_case;
2509 case TARGET_SO_REUSEADDR:
2510 optname = SO_REUSEADDR;
2511 goto int_case;
2512 case TARGET_SO_TYPE:
2513 optname = SO_TYPE;
2514 goto int_case;
2515 case TARGET_SO_ERROR:
2516 optname = SO_ERROR;
2517 goto int_case;
2518 case TARGET_SO_DONTROUTE:
2519 optname = SO_DONTROUTE;
2520 goto int_case;
2521 case TARGET_SO_BROADCAST:
2522 optname = SO_BROADCAST;
2523 goto int_case;
2524 case TARGET_SO_SNDBUF:
2525 optname = SO_SNDBUF;
2526 goto int_case;
2527 case TARGET_SO_RCVBUF:
2528 optname = SO_RCVBUF;
2529 goto int_case;
2530 case TARGET_SO_KEEPALIVE:
2531 optname = SO_KEEPALIVE;
2532 goto int_case;
2533 case TARGET_SO_OOBINLINE:
2534 optname = SO_OOBINLINE;
2535 goto int_case;
2536 case TARGET_SO_NO_CHECK:
2537 optname = SO_NO_CHECK;
2538 goto int_case;
2539 case TARGET_SO_PRIORITY:
2540 optname = SO_PRIORITY;
2541 goto int_case;
2542#ifdef SO_BSDCOMPAT
2543 case TARGET_SO_BSDCOMPAT:
2544 optname = SO_BSDCOMPAT;
2545 goto int_case;
2546#endif
2547 case TARGET_SO_PASSCRED:
2548 optname = SO_PASSCRED;
2549 goto int_case;
2550 case TARGET_SO_TIMESTAMP:
2551 optname = SO_TIMESTAMP;
2552 goto int_case;
2553 case TARGET_SO_RCVLOWAT:
2554 optname = SO_RCVLOWAT;
2555 goto int_case;
aec1ca41
PB
2556 case TARGET_SO_ACCEPTCONN:
2557 optname = SO_ACCEPTCONN;
2558 goto int_case;
8853f86e 2559 default:
2efbe911
FB
2560 goto int_case;
2561 }
2562 break;
2563 case SOL_TCP:
2564 /* TCP options all take an 'int' value. */
2565 int_case:
2f619698
FB
2566 if (get_user_u32(len, optlen))
2567 return -TARGET_EFAULT;
2efbe911 2568 if (len < 0)
0da46a6e 2569 return -TARGET_EINVAL;
73160d95 2570 lv = sizeof(lv);
2efbe911
FB
2571 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2572 if (ret < 0)
2573 return ret;
8289d112
PB
2574 if (optname == SO_TYPE) {
2575 val = host_to_target_sock_type(val);
2576 }
2efbe911
FB
2577 if (len > lv)
2578 len = lv;
2f619698
FB
2579 if (len == 4) {
2580 if (put_user_u32(val, optval_addr))
2581 return -TARGET_EFAULT;
2582 } else {
2583 if (put_user_u8(val, optval_addr))
2584 return -TARGET_EFAULT;
f3b974cd 2585 }
2f619698
FB
2586 if (put_user_u32(len, optlen))
2587 return -TARGET_EFAULT;
2efbe911
FB
2588 break;
2589 case SOL_IP:
2590 switch(optname) {
2591 case IP_TOS:
2592 case IP_TTL:
2593 case IP_HDRINCL:
2594 case IP_ROUTER_ALERT:
2595 case IP_RECVOPTS:
2596 case IP_RETOPTS:
2597 case IP_PKTINFO:
2598 case IP_MTU_DISCOVER:
2599 case IP_RECVERR:
2600 case IP_RECVTOS:
2601#ifdef IP_FREEBIND
2602 case IP_FREEBIND:
2603#endif
2604 case IP_MULTICAST_TTL:
2605 case IP_MULTICAST_LOOP:
2f619698
FB
2606 if (get_user_u32(len, optlen))
2607 return -TARGET_EFAULT;
8853f86e 2608 if (len < 0)
0da46a6e 2609 return -TARGET_EINVAL;
73160d95 2610 lv = sizeof(lv);
8853f86e
FB
2611 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2612 if (ret < 0)
2613 return ret;
2efbe911 2614 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2efbe911 2615 len = 1;
2f619698
FB
2616 if (put_user_u32(len, optlen)
2617 || put_user_u8(val, optval_addr))
2618 return -TARGET_EFAULT;
2efbe911 2619 } else {
2efbe911
FB
2620 if (len > sizeof(int))
2621 len = sizeof(int);
2f619698
FB
2622 if (put_user_u32(len, optlen)
2623 || put_user_u32(val, optval_addr))
2624 return -TARGET_EFAULT;
2efbe911 2625 }
8853f86e 2626 break;
2efbe911 2627 default:
c02f499e
TS
2628 ret = -TARGET_ENOPROTOOPT;
2629 break;
8853f86e
FB
2630 }
2631 break;
2632 default:
2633 unimplemented:
2634 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
2635 level, optname);
c02f499e 2636 ret = -TARGET_EOPNOTSUPP;
8853f86e
FB
2637 break;
2638 }
2639 return ret;
7854b056
FB
2640}
2641
f287b2c2
RH
2642static struct iovec *lock_iovec(int type, abi_ulong target_addr,
2643 int count, int copy)
53a5960a
PB
2644{
2645 struct target_iovec *target_vec;
f287b2c2
RH
2646 struct iovec *vec;
2647 abi_ulong total_len, max_len;
d732dcb4 2648 int i;
501bb4b0 2649 int err = 0;
29560a6c 2650 bool bad_address = false;
53a5960a 2651
f287b2c2
RH
2652 if (count == 0) {
2653 errno = 0;
2654 return NULL;
2655 }
dfae8e00 2656 if (count < 0 || count > IOV_MAX) {
f287b2c2
RH
2657 errno = EINVAL;
2658 return NULL;
2659 }
2660
0e173b24 2661 vec = g_try_new0(struct iovec, count);
f287b2c2
RH
2662 if (vec == NULL) {
2663 errno = ENOMEM;
2664 return NULL;
2665 }
2666
2667 target_vec = lock_user(VERIFY_READ, target_addr,
2668 count * sizeof(struct target_iovec), 1);
2669 if (target_vec == NULL) {
501bb4b0 2670 err = EFAULT;
f287b2c2
RH
2671 goto fail2;
2672 }
2673
2674 /* ??? If host page size > target page size, this will result in a
2675 value larger than what we can actually support. */
2676 max_len = 0x7fffffff & TARGET_PAGE_MASK;
2677 total_len = 0;
2678
2679 for (i = 0; i < count; i++) {
2680 abi_ulong base = tswapal(target_vec[i].iov_base);
2681 abi_long len = tswapal(target_vec[i].iov_len);
2682
2683 if (len < 0) {
501bb4b0 2684 err = EINVAL;
f287b2c2
RH
2685 goto fail;
2686 } else if (len == 0) {
2687 /* Zero length pointer is ignored. */
2688 vec[i].iov_base = 0;
41df8411 2689 } else {
f287b2c2 2690 vec[i].iov_base = lock_user(type, base, len, copy);
29560a6c
TM
2691 /* If the first buffer pointer is bad, this is a fault. But
2692 * subsequent bad buffers will result in a partial write; this
2693 * is realized by filling the vector with null pointers and
2694 * zero lengths. */
f287b2c2 2695 if (!vec[i].iov_base) {
29560a6c
TM
2696 if (i == 0) {
2697 err = EFAULT;
2698 goto fail;
2699 } else {
2700 bad_address = true;
2701 }
2702 }
2703 if (bad_address) {
2704 len = 0;
f287b2c2
RH
2705 }
2706 if (len > max_len - total_len) {
2707 len = max_len - total_len;
2708 }
41df8411 2709 }
f287b2c2
RH
2710 vec[i].iov_len = len;
2711 total_len += len;
579a97f7 2712 }
f287b2c2
RH
2713
2714 unlock_user(target_vec, target_addr, 0);
2715 return vec;
2716
2717 fail:
7eff518b
CG
2718 while (--i >= 0) {
2719 if (tswapal(target_vec[i].iov_len) > 0) {
2720 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
2721 }
2722 }
f287b2c2 2723 unlock_user(target_vec, target_addr, 0);
501bb4b0 2724 fail2:
0e173b24 2725 g_free(vec);
501bb4b0 2726 errno = err;
f287b2c2 2727 return NULL;
53a5960a
PB
2728}
2729
f287b2c2
RH
2730static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
2731 int count, int copy)
53a5960a
PB
2732{
2733 struct target_iovec *target_vec;
53a5960a
PB
2734 int i;
2735
f287b2c2
RH
2736 target_vec = lock_user(VERIFY_READ, target_addr,
2737 count * sizeof(struct target_iovec), 1);
2738 if (target_vec) {
2739 for (i = 0; i < count; i++) {
2740 abi_ulong base = tswapal(target_vec[i].iov_base);
71ec7cef 2741 abi_long len = tswapal(target_vec[i].iov_len);
f287b2c2
RH
2742 if (len < 0) {
2743 break;
2744 }
d732dcb4
AZ
2745 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
2746 }
f287b2c2 2747 unlock_user(target_vec, target_addr, 0);
53a5960a 2748 }
579a97f7 2749
0e173b24 2750 g_free(vec);
53a5960a
PB
2751}
2752
53d09b76 2753static inline int target_to_host_sock_type(int *type)
3532fa74 2754{
f651e6ae
PJ
2755 int host_type = 0;
2756 int target_type = *type;
2757
2758 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3532fa74 2759 case TARGET_SOCK_DGRAM:
f651e6ae 2760 host_type = SOCK_DGRAM;
3532fa74
FB
2761 break;
2762 case TARGET_SOCK_STREAM:
f651e6ae 2763 host_type = SOCK_STREAM;
3532fa74 2764 break;
f651e6ae
PJ
2765 default:
2766 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3532fa74
FB
2767 break;
2768 }
f651e6ae 2769 if (target_type & TARGET_SOCK_CLOEXEC) {
53d09b76 2770#if defined(SOCK_CLOEXEC)
f651e6ae 2771 host_type |= SOCK_CLOEXEC;
53d09b76
EI
2772#else
2773 return -TARGET_EINVAL;
2774#endif
f651e6ae
PJ
2775 }
2776 if (target_type & TARGET_SOCK_NONBLOCK) {
53d09b76 2777#if defined(SOCK_NONBLOCK)
f651e6ae 2778 host_type |= SOCK_NONBLOCK;
53d09b76
EI
2779#elif !defined(O_NONBLOCK)
2780 return -TARGET_EINVAL;
2781#endif
f651e6ae
PJ
2782 }
2783 *type = host_type;
53d09b76
EI
2784 return 0;
2785}
2786
2787/* Try to emulate socket type flags after socket creation. */
2788static int sock_flags_fixup(int fd, int target_type)
2789{
2790#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2791 if (target_type & TARGET_SOCK_NONBLOCK) {
2792 int flags = fcntl(fd, F_GETFL);
2793 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
2794 close(fd);
2795 return -TARGET_EINVAL;
2796 }
2797 }
2798#endif
2799 return fd;
f651e6ae
PJ
2800}
2801
0cf22722
LV
2802static abi_long packet_target_to_host_sockaddr(void *host_addr,
2803 abi_ulong target_addr,
2804 socklen_t len)
2805{
2806 struct sockaddr *addr = host_addr;
2807 struct target_sockaddr *target_saddr;
2808
2809 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
2810 if (!target_saddr) {
2811 return -TARGET_EFAULT;
2812 }
2813
2814 memcpy(addr, target_saddr, len);
2815 addr->sa_family = tswap16(target_saddr->sa_family);
2816 /* spkt_protocol is big-endian */
2817
2818 unlock_user(target_saddr, target_addr, 0);
2819 return 0;
2820}
2821
2822static TargetFdTrans target_packet_trans = {
2823 .target_to_host_addr = packet_target_to_host_sockaddr,
2824};
2825
575b22b1 2826#ifdef CONFIG_RTNETLINK
6c5b5645
LV
2827static abi_long netlink_route_target_to_host(void *buf, size_t len)
2828{
2829 return target_to_host_nlmsg_route(buf, len);
2830}
2831
2832static abi_long netlink_route_host_to_target(void *buf, size_t len)
2833{
2834 return host_to_target_nlmsg_route(buf, len);
2835}
2836
2837static TargetFdTrans target_netlink_route_trans = {
2838 .target_to_host_data = netlink_route_target_to_host,
2839 .host_to_target_data = netlink_route_host_to_target,
2840};
575b22b1 2841#endif /* CONFIG_RTNETLINK */
6c5b5645 2842
5ce9bb59
LV
2843static abi_long netlink_audit_target_to_host(void *buf, size_t len)
2844{
2845 return target_to_host_nlmsg_audit(buf, len);
2846}
2847
2848static abi_long netlink_audit_host_to_target(void *buf, size_t len)
2849{
2850 return host_to_target_nlmsg_audit(buf, len);
2851}
2852
2853static TargetFdTrans target_netlink_audit_trans = {
2854 .target_to_host_data = netlink_audit_target_to_host,
2855 .host_to_target_data = netlink_audit_host_to_target,
2856};
2857
f651e6ae
PJ
2858/* do_socket() Must return target values and target errnos. */
2859static abi_long do_socket(int domain, int type, int protocol)
2860{
53d09b76
EI
2861 int target_type = type;
2862 int ret;
2863
2864 ret = target_to_host_sock_type(&type);
2865 if (ret) {
2866 return ret;
2867 }
f651e6ae 2868
575b22b1
LV
2869 if (domain == PF_NETLINK && !(
2870#ifdef CONFIG_RTNETLINK
2871 protocol == NETLINK_ROUTE ||
2872#endif
2873 protocol == NETLINK_KOBJECT_UEVENT ||
2874 protocol == NETLINK_AUDIT)) {
6c5b5645
LV
2875 return -EPFNOSUPPORT;
2876 }
ff626f2d
LV
2877
2878 if (domain == AF_PACKET ||
2879 (domain == AF_INET && type == SOCK_PACKET)) {
2880 protocol = tswap16(protocol);
2881 }
2882
53d09b76
EI
2883 ret = get_errno(socket(domain, type, protocol));
2884 if (ret >= 0) {
2885 ret = sock_flags_fixup(ret, target_type);
0cf22722
LV
2886 if (type == SOCK_PACKET) {
2887 /* Manage an obsolete case :
2888 * if socket type is SOCK_PACKET, bind by name
2889 */
2890 fd_trans_register(ret, &target_packet_trans);
6c5b5645
LV
2891 } else if (domain == PF_NETLINK) {
2892 switch (protocol) {
575b22b1 2893#ifdef CONFIG_RTNETLINK
6c5b5645
LV
2894 case NETLINK_ROUTE:
2895 fd_trans_register(ret, &target_netlink_route_trans);
2896 break;
575b22b1 2897#endif
b265620b
LV
2898 case NETLINK_KOBJECT_UEVENT:
2899 /* nothing to do: messages are strings */
2900 break;
5ce9bb59
LV
2901 case NETLINK_AUDIT:
2902 fd_trans_register(ret, &target_netlink_audit_trans);
2903 break;
6c5b5645
LV
2904 default:
2905 g_assert_not_reached();
2906 }
0cf22722 2907 }
53d09b76
EI
2908 }
2909 return ret;
3532fa74
FB
2910}
2911
0da46a6e 2912/* do_bind() Must return target values and target errnos. */
992f48a0
BS
2913static abi_long do_bind(int sockfd, abi_ulong target_addr,
2914 socklen_t addrlen)
3532fa74 2915{
8f7aeaf6 2916 void *addr;
917507b0 2917 abi_long ret;
8f7aeaf6 2918
38724253 2919 if ((int)addrlen < 0) {
8f7aeaf6 2920 return -TARGET_EINVAL;
38724253 2921 }
8f7aeaf6 2922
607175e0 2923 addr = alloca(addrlen+1);
3b46e624 2924
7b36f782 2925 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
917507b0
AP
2926 if (ret)
2927 return ret;
2928
3532fa74
FB
2929 return get_errno(bind(sockfd, addr, addrlen));
2930}
2931
0da46a6e 2932/* do_connect() Must return target values and target errnos. */
992f48a0
BS
2933static abi_long do_connect(int sockfd, abi_ulong target_addr,
2934 socklen_t addrlen)
3532fa74 2935{
8f7aeaf6 2936 void *addr;
917507b0 2937 abi_long ret;
8f7aeaf6 2938
38724253 2939 if ((int)addrlen < 0) {
8f7aeaf6 2940 return -TARGET_EINVAL;
38724253 2941 }
8f7aeaf6 2942
2dd08dfd 2943 addr = alloca(addrlen+1);
3b46e624 2944
7b36f782 2945 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
917507b0
AP
2946 if (ret)
2947 return ret;
2948
2a3c7619 2949 return get_errno(safe_connect(sockfd, addr, addrlen));
3532fa74
FB
2950}
2951
f19e00d7
AG
2952/* do_sendrecvmsg_locked() Must return target values and target errnos. */
2953static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
2954 int flags, int send)
3532fa74 2955{
6de645c7 2956 abi_long ret, len;
3532fa74
FB
2957 struct msghdr msg;
2958 int count;
2959 struct iovec *vec;
992f48a0 2960 abi_ulong target_vec;
3532fa74 2961
3532fa74
FB
2962 if (msgp->msg_name) {
2963 msg.msg_namelen = tswap32(msgp->msg_namelen);
2dd08dfd 2964 msg.msg_name = alloca(msg.msg_namelen+1);
7b36f782
LV
2965 ret = target_to_host_sockaddr(fd, msg.msg_name,
2966 tswapal(msgp->msg_name),
2967 msg.msg_namelen);
917507b0 2968 if (ret) {
f287b2c2 2969 goto out2;
917507b0 2970 }
3532fa74
FB
2971 } else {
2972 msg.msg_name = NULL;
2973 msg.msg_namelen = 0;
2974 }
cbb21eed 2975 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
3532fa74
FB
2976 msg.msg_control = alloca(msg.msg_controllen);
2977 msg.msg_flags = tswap32(msgp->msg_flags);
3b46e624 2978
cbb21eed 2979 count = tswapal(msgp->msg_iovlen);
cbb21eed 2980 target_vec = tswapal(msgp->msg_iov);
f287b2c2
RH
2981 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2982 target_vec, count, send);
2983 if (vec == NULL) {
2984 ret = -host_to_target_errno(errno);
2985 goto out2;
2986 }
3532fa74
FB
2987 msg.msg_iovlen = count;
2988 msg.msg_iov = vec;
3b46e624 2989
3532fa74 2990 if (send) {
6c5b5645
LV
2991 if (fd_trans_target_to_host_data(fd)) {
2992 ret = fd_trans_target_to_host_data(fd)(msg.msg_iov->iov_base,
2993 msg.msg_iov->iov_len);
2994 } else {
2995 ret = target_to_host_cmsg(&msg, msgp);
2996 }
2997 if (ret == 0) {
66687530 2998 ret = get_errno(safe_sendmsg(fd, &msg, flags));
6c5b5645 2999 }
3532fa74 3000 } else {
66687530 3001 ret = get_errno(safe_recvmsg(fd, &msg, flags));
6de645c7
AZ
3002 if (!is_error(ret)) {
3003 len = ret;
6c5b5645
LV
3004 if (fd_trans_host_to_target_data(fd)) {
3005 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
48dc0f2c 3006 len);
6c5b5645
LV
3007 } else {
3008 ret = host_to_target_cmsg(msgp, &msg);
3009 }
ca619067
JH
3010 if (!is_error(ret)) {
3011 msgp->msg_namelen = tswap32(msg.msg_namelen);
3012 if (msg.msg_name != NULL) {
3013 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
3014 msg.msg_name, msg.msg_namelen);
3015 if (ret) {
3016 goto out;
3017 }
3018 }
3019
6de645c7 3020 ret = len;
ca619067 3021 }
6de645c7 3022 }
3532fa74 3023 }
ca619067
JH
3024
3025out:
3532fa74 3026 unlock_iovec(vec, target_vec, count, !send);
f287b2c2 3027out2:
f19e00d7
AG
3028 return ret;
3029}
3030
3031static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3032 int flags, int send)
3033{
3034 abi_long ret;
3035 struct target_msghdr *msgp;
3036
3037 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3038 msgp,
3039 target_msg,
3040 send ? 1 : 0)) {
3041 return -TARGET_EFAULT;
3042 }
3043 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
579a97f7 3044 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3532fa74
FB
3045 return ret;
3046}
3047
f19e00d7
AG
3048/* We don't rely on the C library to have sendmmsg/recvmmsg support,
3049 * so it might not have this *mmsg-specific flag either.
3050 */
3051#ifndef MSG_WAITFORONE
3052#define MSG_WAITFORONE 0x10000
3053#endif
3054
3055static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3056 unsigned int vlen, unsigned int flags,
3057 int send)
3058{
3059 struct target_mmsghdr *mmsgp;
3060 abi_long ret = 0;
3061 int i;
3062
3063 if (vlen > UIO_MAXIOV) {
3064 vlen = UIO_MAXIOV;
3065 }
3066
3067 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3068 if (!mmsgp) {
3069 return -TARGET_EFAULT;
3070 }
3071
3072 for (i = 0; i < vlen; i++) {
3073 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3074 if (is_error(ret)) {
3075 break;
3076 }
3077 mmsgp[i].msg_len = tswap32(ret);
3078 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3079 if (flags & MSG_WAITFORONE) {
3080 flags |= MSG_DONTWAIT;
3081 }
3082 }
3083
3084 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3085
3086 /* Return number of datagrams sent if we sent any at all;
3087 * otherwise return the error.
3088 */
3089 if (i) {
3090 return i;
3091 }
3092 return ret;
3093}
f19e00d7 3094
a94b4987
PM
3095/* do_accept4() Must return target values and target errnos. */
3096static abi_long do_accept4(int fd, abi_ulong target_addr,
3097 abi_ulong target_addrlen_addr, int flags)
1be9e1dc 3098{
2f619698
FB
3099 socklen_t addrlen;
3100 void *addr;
992f48a0 3101 abi_long ret;
d25295d4
PJ
3102 int host_flags;
3103
3104 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
1be9e1dc 3105
a94b4987 3106 if (target_addr == 0) {
ff6dc130 3107 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
a94b4987 3108 }
917507b0
AP
3109
3110 /* linux returns EINVAL if addrlen pointer is invalid */
2f619698 3111 if (get_user_u32(addrlen, target_addrlen_addr))
917507b0 3112 return -TARGET_EINVAL;
2f619698 3113
38724253 3114 if ((int)addrlen < 0) {
8f7aeaf6 3115 return -TARGET_EINVAL;
38724253 3116 }
8f7aeaf6 3117
917507b0
AP
3118 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3119 return -TARGET_EINVAL;
3120
2f619698
FB
3121 addr = alloca(addrlen);
3122
ff6dc130 3123 ret = get_errno(safe_accept4(fd, addr, &addrlen, host_flags));
1be9e1dc
PB
3124 if (!is_error(ret)) {
3125 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
3126 if (put_user_u32(addrlen, target_addrlen_addr))
3127 ret = -TARGET_EFAULT;
1be9e1dc
PB
3128 }
3129 return ret;
3130}
3131
0da46a6e 3132/* do_getpeername() Must return target values and target errnos. */
992f48a0 3133static abi_long do_getpeername(int fd, abi_ulong target_addr,
2f619698 3134 abi_ulong target_addrlen_addr)
1be9e1dc 3135{
2f619698
FB
3136 socklen_t addrlen;
3137 void *addr;
992f48a0 3138 abi_long ret;
1be9e1dc 3139
2f619698
FB
3140 if (get_user_u32(addrlen, target_addrlen_addr))
3141 return -TARGET_EFAULT;
3142
38724253 3143 if ((int)addrlen < 0) {
8f7aeaf6 3144 return -TARGET_EINVAL;
38724253 3145 }
8f7aeaf6 3146
917507b0
AP
3147 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3148 return -TARGET_EFAULT;
3149
2f619698
FB
3150 addr = alloca(addrlen);
3151
1be9e1dc
PB
3152 ret = get_errno(getpeername(fd, addr, &addrlen));
3153 if (!is_error(ret)) {
3154 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
3155 if (put_user_u32(addrlen, target_addrlen_addr))
3156 ret = -TARGET_EFAULT;
1be9e1dc
PB
3157 }
3158 return ret;
3159}
3160
0da46a6e 3161/* do_getsockname() Must return target values and target errnos. */
992f48a0 3162static abi_long do_getsockname(int fd, abi_ulong target_addr,
2f619698 3163 abi_ulong target_addrlen_addr)
1be9e1dc 3164{
2f619698
FB
3165 socklen_t addrlen;
3166 void *addr;
992f48a0 3167 abi_long ret;
1be9e1dc 3168
2f619698
FB
3169 if (get_user_u32(addrlen, target_addrlen_addr))
3170 return -TARGET_EFAULT;
3171
38724253 3172 if ((int)addrlen < 0) {
8f7aeaf6 3173 return -TARGET_EINVAL;
38724253 3174 }
8f7aeaf6 3175
917507b0
AP
3176 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3177 return -TARGET_EFAULT;
3178
2f619698
FB
3179 addr = alloca(addrlen);
3180
1be9e1dc
PB
3181 ret = get_errno(getsockname(fd, addr, &addrlen));
3182 if (!is_error(ret)) {
3183 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
3184 if (put_user_u32(addrlen, target_addrlen_addr))
3185 ret = -TARGET_EFAULT;
1be9e1dc
PB
3186 }
3187 return ret;
3188}
3189
0da46a6e 3190/* do_socketpair() Must return target values and target errnos. */
992f48a0 3191static abi_long do_socketpair(int domain, int type, int protocol,
2f619698 3192 abi_ulong target_tab_addr)
1be9e1dc
PB
3193{
3194 int tab[2];
992f48a0 3195 abi_long ret;
1be9e1dc 3196
f651e6ae
PJ
3197 target_to_host_sock_type(&type);
3198
1be9e1dc
PB
3199 ret = get_errno(socketpair(domain, type, protocol, tab));
3200 if (!is_error(ret)) {
2f619698
FB
3201 if (put_user_s32(tab[0], target_tab_addr)
3202 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3203 ret = -TARGET_EFAULT;
1be9e1dc
PB
3204 }
3205 return ret;
3206}
3207
0da46a6e 3208/* do_sendto() Must return target values and target errnos. */
992f48a0
BS
3209static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3210 abi_ulong target_addr, socklen_t addrlen)
1be9e1dc
PB
3211{
3212 void *addr;
3213 void *host_msg;
992f48a0 3214 abi_long ret;
1be9e1dc 3215
38724253 3216 if ((int)addrlen < 0) {
8f7aeaf6 3217 return -TARGET_EINVAL;
38724253 3218 }
8f7aeaf6 3219
579a97f7
FB
3220 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3221 if (!host_msg)
3222 return -TARGET_EFAULT;
6c5b5645
LV
3223 if (fd_trans_target_to_host_data(fd)) {
3224 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3225 if (ret < 0) {
3226 unlock_user(host_msg, msg, 0);
3227 return ret;
3228 }
3229 }
1be9e1dc 3230 if (target_addr) {
2dd08dfd 3231 addr = alloca(addrlen+1);
7b36f782 3232 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
917507b0
AP
3233 if (ret) {
3234 unlock_user(host_msg, msg, 0);
3235 return ret;
3236 }
66687530 3237 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
1be9e1dc 3238 } else {
66687530 3239 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
1be9e1dc
PB
3240 }
3241 unlock_user(host_msg, msg, 0);
3242 return ret;
3243}
3244
0da46a6e 3245/* do_recvfrom() Must return target values and target errnos. */
992f48a0
BS
3246static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3247 abi_ulong target_addr,
3248 abi_ulong target_addrlen)
1be9e1dc
PB
3249{
3250 socklen_t addrlen;
3251 void *addr;
3252 void *host_msg;
992f48a0 3253 abi_long ret;
1be9e1dc 3254
579a97f7
FB
3255 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3256 if (!host_msg)
3257 return -TARGET_EFAULT;
1be9e1dc 3258 if (target_addr) {
2f619698
FB
3259 if (get_user_u32(addrlen, target_addrlen)) {
3260 ret = -TARGET_EFAULT;
3261 goto fail;
3262 }
38724253 3263 if ((int)addrlen < 0) {
8f7aeaf6
AJ
3264 ret = -TARGET_EINVAL;
3265 goto fail;
3266 }
1be9e1dc 3267 addr = alloca(addrlen);
66687530
PM
3268 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
3269 addr, &addrlen));
1be9e1dc
PB
3270 } else {
3271 addr = NULL; /* To keep compiler quiet. */
66687530 3272 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
1be9e1dc
PB
3273 }
3274 if (!is_error(ret)) {
3275 if (target_addr) {
3276 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
3277 if (put_user_u32(addrlen, target_addrlen)) {
3278 ret = -TARGET_EFAULT;
3279 goto fail;
3280 }
1be9e1dc
PB
3281 }
3282 unlock_user(host_msg, msg, len);
3283 } else {
2f619698 3284fail:
1be9e1dc
PB
3285 unlock_user(host_msg, msg, 0);
3286 }
3287 return ret;
3288}
3289
32407103 3290#ifdef TARGET_NR_socketcall
0da46a6e 3291/* do_socketcall() Must return target values and target errnos. */
992f48a0 3292static abi_long do_socketcall(int num, abi_ulong vptr)
31e31b8a 3293{
62dc90c6
MT
3294 static const unsigned ac[] = { /* number of arguments per call */
3295 [SOCKOP_socket] = 3, /* domain, type, protocol */
3296 [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
3297 [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
3298 [SOCKOP_listen] = 2, /* sockfd, backlog */
3299 [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
3300 [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
3301 [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
3302 [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
3303 [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
3304 [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
3305 [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
3306 [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
3307 [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
3308 [SOCKOP_shutdown] = 2, /* sockfd, how */
3309 [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
3310 [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
5a53dc50
JPAG
3311 [SOCKOP_sendmmsg] = 4, /* sockfd, msgvec, vlen, flags */
3312 [SOCKOP_recvmmsg] = 4, /* sockfd, msgvec, vlen, flags */
62dc90c6
MT
3313 [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */
3314 [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */
3315 };
3316 abi_long a[6]; /* max 6 args */
3317
3318 /* first, collect the arguments in a[] according to ac[] */
3319 if (num >= 0 && num < ARRAY_SIZE(ac)) {
3320 unsigned i;
3321 assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */
3322 for (i = 0; i < ac[num]; ++i) {
3323 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
b9d36eb2
AH
3324 return -TARGET_EFAULT;
3325 }
31e31b8a 3326 }
62dc90c6 3327 }
7854b056 3328
62dc90c6
MT
3329 /* now when we have the args, actually handle the call */
3330 switch (num) {
3331 case SOCKOP_socket: /* domain, type, protocol */
3332 return do_socket(a[0], a[1], a[2]);
3333 case SOCKOP_bind: /* sockfd, addr, addrlen */
3334 return do_bind(a[0], a[1], a[2]);
3335 case SOCKOP_connect: /* sockfd, addr, addrlen */
3336 return do_connect(a[0], a[1], a[2]);
3337 case SOCKOP_listen: /* sockfd, backlog */
3338 return get_errno(listen(a[0], a[1]));
3339 case SOCKOP_accept: /* sockfd, addr, addrlen */
3340 return do_accept4(a[0], a[1], a[2], 0);
3341 case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */
3342 return do_accept4(a[0], a[1], a[2], a[3]);
3343 case SOCKOP_getsockname: /* sockfd, addr, addrlen */
3344 return do_getsockname(a[0], a[1], a[2]);
3345 case SOCKOP_getpeername: /* sockfd, addr, addrlen */
3346 return do_getpeername(a[0], a[1], a[2]);
3347 case SOCKOP_socketpair: /* domain, type, protocol, tab */
3348 return do_socketpair(a[0], a[1], a[2], a[3]);
3349 case SOCKOP_send: /* sockfd, msg, len, flags */
3350 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
3351 case SOCKOP_recv: /* sockfd, msg, len, flags */
3352 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
3353 case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */
3354 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
3355 case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */
3356 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
3357 case SOCKOP_shutdown: /* sockfd, how */
3358 return get_errno(shutdown(a[0], a[1]));
3359 case SOCKOP_sendmsg: /* sockfd, msg, flags */
3360 return do_sendrecvmsg(a[0], a[1], a[2], 1);
3361 case SOCKOP_recvmsg: /* sockfd, msg, flags */
3362 return do_sendrecvmsg(a[0], a[1], a[2], 0);
5a53dc50
JPAG
3363 case SOCKOP_sendmmsg: /* sockfd, msgvec, vlen, flags */
3364 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
3365 case SOCKOP_recvmmsg: /* sockfd, msgvec, vlen, flags */
3366 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
62dc90c6
MT
3367 case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */
3368 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
3369 case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */
3370 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
31e31b8a
FB
3371 default:
3372 gemu_log("Unsupported socketcall: %d\n", num);
62dc90c6 3373 return -TARGET_ENOSYS;
31e31b8a 3374 }
31e31b8a 3375}
32407103 3376#endif
31e31b8a 3377
8853f86e
FB
3378#define N_SHM_REGIONS 32
3379
3380static struct shm_region {
b6e17875
PM
3381 abi_ulong start;
3382 abi_ulong size;
3383 bool in_use;
8853f86e
FB
3384} shm_regions[N_SHM_REGIONS];
3385
3eb6b044
TS
3386struct target_semid_ds
3387{
3388 struct target_ipc_perm sem_perm;
992f48a0 3389 abi_ulong sem_otime;
03527344 3390#if !defined(TARGET_PPC64)
992f48a0 3391 abi_ulong __unused1;
03527344 3392#endif
992f48a0 3393 abi_ulong sem_ctime;
03527344 3394#if !defined(TARGET_PPC64)
992f48a0 3395 abi_ulong __unused2;
03527344 3396#endif
992f48a0
BS
3397 abi_ulong sem_nsems;
3398 abi_ulong __unused3;
3399 abi_ulong __unused4;
3eb6b044
TS
3400};
3401
579a97f7
FB
3402static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
3403 abi_ulong target_addr)
3eb6b044
TS
3404{
3405 struct target_ipc_perm *target_ip;
3406 struct target_semid_ds *target_sd;
3407
579a97f7
FB
3408 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3409 return -TARGET_EFAULT;
e8bbe36c 3410 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
3411 host_ip->__key = tswap32(target_ip->__key);
3412 host_ip->uid = tswap32(target_ip->uid);
3413 host_ip->gid = tswap32(target_ip->gid);
3414 host_ip->cuid = tswap32(target_ip->cuid);
3415 host_ip->cgid = tswap32(target_ip->cgid);
3416#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3417 host_ip->mode = tswap32(target_ip->mode);
3418#else
cbb21eed 3419 host_ip->mode = tswap16(target_ip->mode);
55a2b163
PJ
3420#endif
3421#if defined(TARGET_PPC)
3422 host_ip->__seq = tswap32(target_ip->__seq);
3423#else
3424 host_ip->__seq = tswap16(target_ip->__seq);
3425#endif
3eb6b044 3426 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 3427 return 0;
3eb6b044
TS
3428}
3429
579a97f7
FB
3430static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
3431 struct ipc_perm *host_ip)
3eb6b044
TS
3432{
3433 struct target_ipc_perm *target_ip;
3434 struct target_semid_ds *target_sd;
3435
579a97f7
FB
3436 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3437 return -TARGET_EFAULT;
3eb6b044 3438 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
3439 target_ip->__key = tswap32(host_ip->__key);
3440 target_ip->uid = tswap32(host_ip->uid);
3441 target_ip->gid = tswap32(host_ip->gid);
3442 target_ip->cuid = tswap32(host_ip->cuid);
3443 target_ip->cgid = tswap32(host_ip->cgid);
3444#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3445 target_ip->mode = tswap32(host_ip->mode);
3446#else
cbb21eed 3447 target_ip->mode = tswap16(host_ip->mode);
55a2b163
PJ
3448#endif
3449#if defined(TARGET_PPC)
3450 target_ip->__seq = tswap32(host_ip->__seq);
3451#else
3452 target_ip->__seq = tswap16(host_ip->__seq);
3453#endif
3eb6b044 3454 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 3455 return 0;
3eb6b044
TS
3456}
3457
579a97f7
FB
3458static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
3459 abi_ulong target_addr)
3eb6b044
TS
3460{
3461 struct target_semid_ds *target_sd;
3462
579a97f7
FB
3463 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3464 return -TARGET_EFAULT;
e5289087
AJ
3465 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
3466 return -TARGET_EFAULT;
cbb21eed
MB
3467 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
3468 host_sd->sem_otime = tswapal(target_sd->sem_otime);
3469 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
3eb6b044 3470 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 3471 return 0;
3eb6b044
TS
3472}
3473
579a97f7
FB
3474static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
3475 struct semid_ds *host_sd)
3eb6b044
TS
3476{
3477 struct target_semid_ds *target_sd;
3478
579a97f7
FB
3479 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3480 return -TARGET_EFAULT;
e5289087 3481 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
3a93113a 3482 return -TARGET_EFAULT;
cbb21eed
MB
3483 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
3484 target_sd->sem_otime = tswapal(host_sd->sem_otime);
3485 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
3eb6b044 3486 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 3487 return 0;
3eb6b044
TS
3488}
3489
e5289087
AJ
3490struct target_seminfo {
3491 int semmap;
3492 int semmni;
3493 int semmns;
3494 int semmnu;
3495 int semmsl;
3496 int semopm;
3497 int semume;
3498 int semusz;
3499 int semvmx;
3500 int semaem;
3501};
3502
3503static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
3504 struct seminfo *host_seminfo)
3505{
3506 struct target_seminfo *target_seminfo;
3507 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
3508 return -TARGET_EFAULT;
3509 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
3510 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
3511 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
3512 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
3513 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
3514 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
3515 __put_user(host_seminfo->semume, &target_seminfo->semume);
3516 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
3517 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
3518 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
3519 unlock_user_struct(target_seminfo, target_addr, 1);
3520 return 0;
3521}
3522
fa294816
TS
3523union semun {
3524 int val;
3eb6b044 3525 struct semid_ds *buf;
fa294816 3526 unsigned short *array;
e5289087 3527 struct seminfo *__buf;
fa294816
TS
3528};
3529
3eb6b044
TS
3530union target_semun {
3531 int val;
e5289087
AJ
3532 abi_ulong buf;
3533 abi_ulong array;
3534 abi_ulong __buf;
3eb6b044
TS
3535};
3536
e5289087
AJ
3537static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3538 abi_ulong target_addr)
3eb6b044 3539{
e5289087
AJ
3540 int nsems;
3541 unsigned short *array;
3542 union semun semun;
3543 struct semid_ds semid_ds;
3544 int i, ret;
3eb6b044 3545
e5289087
AJ
3546 semun.buf = &semid_ds;
3547
3548 ret = semctl(semid, 0, IPC_STAT, semun);
3549 if (ret == -1)
3550 return get_errno(ret);
3551
3552 nsems = semid_ds.sem_nsems;
3553
0e173b24 3554 *host_array = g_try_new(unsigned short, nsems);
69d4c703
PM
3555 if (!*host_array) {
3556 return -TARGET_ENOMEM;
3557 }
e5289087
AJ
3558 array = lock_user(VERIFY_READ, target_addr,
3559 nsems*sizeof(unsigned short), 1);
69d4c703 3560 if (!array) {
0e173b24 3561 g_free(*host_array);
e5289087 3562 return -TARGET_EFAULT;
69d4c703 3563 }
e5289087
AJ
3564
3565 for(i=0; i<nsems; i++) {
3566 __get_user((*host_array)[i], &array[i]);
3eb6b044 3567 }
e5289087
AJ
3568 unlock_user(array, target_addr, 0);
3569
579a97f7 3570 return 0;
3eb6b044
TS
3571}
3572
e5289087
AJ
3573static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3574 unsigned short **host_array)
3eb6b044 3575{
e5289087
AJ
3576 int nsems;
3577 unsigned short *array;
3578 union semun semun;
3579 struct semid_ds semid_ds;
3580 int i, ret;
3eb6b044 3581
e5289087
AJ
3582 semun.buf = &semid_ds;
3583
3584 ret = semctl(semid, 0, IPC_STAT, semun);
3585 if (ret == -1)
3586 return get_errno(ret);
3587
3588 nsems = semid_ds.sem_nsems;
3589
3590 array = lock_user(VERIFY_WRITE, target_addr,
3591 nsems*sizeof(unsigned short), 0);
3592 if (!array)
3593 return -TARGET_EFAULT;
3594
3595 for(i=0; i<nsems; i++) {
3596 __put_user((*host_array)[i], &array[i]);
3eb6b044 3597 }
0e173b24 3598 g_free(*host_array);
e5289087
AJ
3599 unlock_user(array, target_addr, 1);
3600
579a97f7 3601 return 0;
3eb6b044
TS
3602}
3603
e5289087 3604static inline abi_long do_semctl(int semid, int semnum, int cmd,
d1c002b6 3605 abi_ulong target_arg)
3eb6b044 3606{
d1c002b6 3607 union target_semun target_su = { .buf = target_arg };
3eb6b044
TS
3608 union semun arg;
3609 struct semid_ds dsarg;
7b8118e8 3610 unsigned short *array = NULL;
e5289087
AJ
3611 struct seminfo seminfo;
3612 abi_long ret = -TARGET_EINVAL;
3613 abi_long err;
3614 cmd &= 0xff;
3eb6b044
TS
3615
3616 switch( cmd ) {
3617 case GETVAL:
3eb6b044 3618 case SETVAL:
5464baec
TM
3619 /* In 64 bit cross-endian situations, we will erroneously pick up
3620 * the wrong half of the union for the "val" element. To rectify
3621 * this, the entire 8-byte structure is byteswapped, followed by
3622 * a swap of the 4 byte val field. In other cases, the data is
3623 * already in proper host byte order. */
3624 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
3625 target_su.buf = tswapal(target_su.buf);
3626 arg.val = tswap32(target_su.val);
3627 } else {
3628 arg.val = target_su.val;
3629 }
e5289087 3630 ret = get_errno(semctl(semid, semnum, cmd, arg));
3eb6b044
TS
3631 break;
3632 case GETALL:
3eb6b044 3633 case SETALL:
e5289087
AJ
3634 err = target_to_host_semarray(semid, &array, target_su.array);
3635 if (err)
3636 return err;
3637 arg.array = array;
3638 ret = get_errno(semctl(semid, semnum, cmd, arg));
3639 err = host_to_target_semarray(semid, target_su.array, &array);
3640 if (err)
3641 return err;
3eb6b044
TS
3642 break;
3643 case IPC_STAT:
3eb6b044 3644 case IPC_SET:
e5289087
AJ
3645 case SEM_STAT:
3646 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3647 if (err)
3648 return err;
3649 arg.buf = &dsarg;
3650 ret = get_errno(semctl(semid, semnum, cmd, arg));
3651 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3652 if (err)
3653 return err;
3654 break;
3655 case IPC_INFO:
3656 case SEM_INFO:
3657 arg.__buf = &seminfo;
3658 ret = get_errno(semctl(semid, semnum, cmd, arg));
3659 err = host_to_target_seminfo(target_su.__buf, &seminfo);
3660 if (err)
3661 return err;
3662 break;
3663 case IPC_RMID:
3664 case GETPID:
3665 case GETNCNT:
3666 case GETZCNT:
3667 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3eb6b044 3668 break;
3eb6b044
TS
3669 }
3670
3671 return ret;
3672}
3673
e5289087
AJ
3674struct target_sembuf {
3675 unsigned short sem_num;
3676 short sem_op;
3677 short sem_flg;
3678};
3679
3680static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
3681 abi_ulong target_addr,
3682 unsigned nsops)
3683{
3684 struct target_sembuf *target_sembuf;
3685 int i;
3686
3687 target_sembuf = lock_user(VERIFY_READ, target_addr,
3688 nsops*sizeof(struct target_sembuf), 1);
3689 if (!target_sembuf)
3690 return -TARGET_EFAULT;
3691
3692 for(i=0; i<nsops; i++) {
3693 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
3694 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
3695 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
3696 }
3697
3698 unlock_user(target_sembuf, target_addr, 0);
3699
3700 return 0;
3701}
3702
3703static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
3704{
3705 struct sembuf sops[nsops];
3706
3707 if (target_to_host_sembuf(sops, ptr, nsops))
3708 return -TARGET_EFAULT;
3709
ffb7ee79 3710 return get_errno(safe_semtimedop(semid, sops, nsops, NULL));
e5289087
AJ
3711}
3712
1bc012f6
TS
3713struct target_msqid_ds
3714{
1c54ff97
AJ
3715 struct target_ipc_perm msg_perm;
3716 abi_ulong msg_stime;
3717#if TARGET_ABI_BITS == 32
3718 abi_ulong __unused1;
3719#endif
3720 abi_ulong msg_rtime;
3721#if TARGET_ABI_BITS == 32
3722 abi_ulong __unused2;
3723#endif
3724 abi_ulong msg_ctime;
3725#if TARGET_ABI_BITS == 32
3726 abi_ulong __unused3;
3727#endif
3728 abi_ulong __msg_cbytes;
3729 abi_ulong msg_qnum;
3730 abi_ulong msg_qbytes;
3731 abi_ulong msg_lspid;
3732 abi_ulong msg_lrpid;
3733 abi_ulong __unused4;
3734 abi_ulong __unused5;
1bc012f6
TS
3735};
3736
579a97f7
FB
3737static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
3738 abi_ulong target_addr)
1bc012f6
TS
3739{
3740 struct target_msqid_ds *target_md;
3741
579a97f7
FB
3742 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
3743 return -TARGET_EFAULT;
1c54ff97
AJ
3744 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
3745 return -TARGET_EFAULT;
cbb21eed
MB
3746 host_md->msg_stime = tswapal(target_md->msg_stime);
3747 host_md->msg_rtime = tswapal(target_md->msg_rtime);
3748 host_md->msg_ctime = tswapal(target_md->msg_ctime);
3749 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
3750 host_md->msg_qnum = tswapal(target_md->msg_qnum);
3751 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
3752 host_md->msg_lspid = tswapal(target_md->msg_lspid);
3753 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
1bc012f6 3754 unlock_user_struct(target_md, target_addr, 0);
579a97f7 3755 return 0;
1bc012f6
TS
3756}
3757
579a97f7
FB
3758static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
3759 struct msqid_ds *host_md)
1bc012f6
TS
3760{
3761 struct target_msqid_ds *target_md;
3762
579a97f7
FB
3763 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
3764 return -TARGET_EFAULT;
1c54ff97
AJ
3765 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
3766 return -TARGET_EFAULT;
cbb21eed
MB
3767 target_md->msg_stime = tswapal(host_md->msg_stime);
3768 target_md->msg_rtime = tswapal(host_md->msg_rtime);
3769 target_md->msg_ctime = tswapal(host_md->msg_ctime);
3770 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
3771 target_md->msg_qnum = tswapal(host_md->msg_qnum);
3772 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
3773 target_md->msg_lspid = tswapal(host_md->msg_lspid);
3774 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
1bc012f6 3775 unlock_user_struct(target_md, target_addr, 1);
579a97f7 3776 return 0;
1bc012f6
TS
3777}
3778
1c54ff97
AJ
3779struct target_msginfo {
3780 int msgpool;
3781 int msgmap;
3782 int msgmax;
3783 int msgmnb;
3784 int msgmni;
3785 int msgssz;
3786 int msgtql;
3787 unsigned short int msgseg;
3788};
3789
3790static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
3791 struct msginfo *host_msginfo)
3792{
3793 struct target_msginfo *target_msginfo;
3794 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
3795 return -TARGET_EFAULT;
3796 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
3797 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
3798 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
3799 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
3800 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
3801 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
3802 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
3803 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
3804 unlock_user_struct(target_msginfo, target_addr, 1);
00b229ac 3805 return 0;
1c54ff97
AJ
3806}
3807
3808static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
1bc012f6
TS
3809{
3810 struct msqid_ds dsarg;
1c54ff97
AJ
3811 struct msginfo msginfo;
3812 abi_long ret = -TARGET_EINVAL;
3813
3814 cmd &= 0xff;
3815
3816 switch (cmd) {
1bc012f6
TS
3817 case IPC_STAT:
3818 case IPC_SET:
1c54ff97
AJ
3819 case MSG_STAT:
3820 if (target_to_host_msqid_ds(&dsarg,ptr))
3821 return -TARGET_EFAULT;
3822 ret = get_errno(msgctl(msgid, cmd, &dsarg));
3823 if (host_to_target_msqid_ds(ptr,&dsarg))
3824 return -TARGET_EFAULT;
3825 break;
3826 case IPC_RMID:
3827 ret = get_errno(msgctl(msgid, cmd, NULL));
3828 break;
3829 case IPC_INFO:
3830 case MSG_INFO:
3831 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
3832 if (host_to_target_msginfo(ptr, &msginfo))
3833 return -TARGET_EFAULT;
3834 break;
1bc012f6 3835 }
1c54ff97 3836
1bc012f6
TS
3837 return ret;
3838}
3839
3840struct target_msgbuf {
1c54ff97
AJ
3841 abi_long mtype;
3842 char mtext[1];
1bc012f6
TS
3843};
3844
992f48a0 3845static inline abi_long do_msgsnd(int msqid, abi_long msgp,
edcc5f9d 3846 ssize_t msgsz, int msgflg)
1bc012f6
TS
3847{
3848 struct target_msgbuf *target_mb;
3849 struct msgbuf *host_mb;
992f48a0 3850 abi_long ret = 0;
1bc012f6 3851
edcc5f9d
TM
3852 if (msgsz < 0) {
3853 return -TARGET_EINVAL;
3854 }
3855
579a97f7
FB
3856 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
3857 return -TARGET_EFAULT;
0e173b24 3858 host_mb = g_try_malloc(msgsz + sizeof(long));
29e03fcb
HZ
3859 if (!host_mb) {
3860 unlock_user_struct(target_mb, msgp, 0);
3861 return -TARGET_ENOMEM;
3862 }
cbb21eed 3863 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
1c54ff97 3864 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
89f9fe44 3865 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
0e173b24 3866 g_free(host_mb);
1bc012f6
TS
3867 unlock_user_struct(target_mb, msgp, 0);
3868
3869 return ret;
3870}
3871
992f48a0 3872static inline abi_long do_msgrcv(int msqid, abi_long msgp,
99874f65 3873 ssize_t msgsz, abi_long msgtyp,
992f48a0 3874 int msgflg)
1bc012f6
TS
3875{
3876 struct target_msgbuf *target_mb;
579a97f7 3877 char *target_mtext;
1bc012f6 3878 struct msgbuf *host_mb;
992f48a0 3879 abi_long ret = 0;
1bc012f6 3880
99874f65
PM
3881 if (msgsz < 0) {
3882 return -TARGET_EINVAL;
3883 }
3884
579a97f7
FB
3885 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
3886 return -TARGET_EFAULT;
1c54ff97 3887
415d8471
PM
3888 host_mb = g_try_malloc(msgsz + sizeof(long));
3889 if (!host_mb) {
3890 ret = -TARGET_ENOMEM;
3891 goto end;
3892 }
89f9fe44 3893 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
1c54ff97 3894
579a97f7
FB
3895 if (ret > 0) {
3896 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
3897 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
3898 if (!target_mtext) {
3899 ret = -TARGET_EFAULT;
3900 goto end;
3901 }
1c54ff97 3902 memcpy(target_mb->mtext, host_mb->mtext, ret);
579a97f7
FB
3903 unlock_user(target_mtext, target_mtext_addr, ret);
3904 }
1c54ff97 3905
cbb21eed 3906 target_mb->mtype = tswapal(host_mb->mtype);
1bc012f6 3907
579a97f7
FB
3908end:
3909 if (target_mb)
3910 unlock_user_struct(target_mb, msgp, 1);
0d07fe47 3911 g_free(host_mb);
1bc012f6
TS
3912 return ret;
3913}
3914
88a8c984
RV
3915static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
3916 abi_ulong target_addr)
3917{
3918 struct target_shmid_ds *target_sd;
3919
3920 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3921 return -TARGET_EFAULT;
3922 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
3923 return -TARGET_EFAULT;
3924 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3925 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
3926 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3927 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3928 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3929 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3930 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3931 unlock_user_struct(target_sd, target_addr, 0);
3932 return 0;
3933}
3934
3935static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
3936 struct shmid_ds *host_sd)
3937{
3938 struct target_shmid_ds *target_sd;
3939
3940 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3941 return -TARGET_EFAULT;
3942 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
3943 return -TARGET_EFAULT;
3944 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3945 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
3946 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3947 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3948 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3949 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3950 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3951 unlock_user_struct(target_sd, target_addr, 1);
3952 return 0;
3953}
3954
3955struct target_shminfo {
3956 abi_ulong shmmax;
3957 abi_ulong shmmin;
3958 abi_ulong shmmni;
3959 abi_ulong shmseg;
3960 abi_ulong shmall;
3961};
3962
3963static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
3964 struct shminfo *host_shminfo)
3965{
3966 struct target_shminfo *target_shminfo;
3967 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3968 return -TARGET_EFAULT;
3969 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3970 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3971 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3972 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3973 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3974 unlock_user_struct(target_shminfo, target_addr, 1);
3975 return 0;
3976}
3977
3978struct target_shm_info {
3979 int used_ids;
3980 abi_ulong shm_tot;
3981 abi_ulong shm_rss;
3982 abi_ulong shm_swp;
3983 abi_ulong swap_attempts;
3984 abi_ulong swap_successes;
3985};
3986
3987static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3988 struct shm_info *host_shm_info)
3989{
3990 struct target_shm_info *target_shm_info;
3991 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3992 return -TARGET_EFAULT;
3993 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3994 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3995 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3996 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3997 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3998 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3999 unlock_user_struct(target_shm_info, target_addr, 1);
4000 return 0;
4001}
4002
4003static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
4004{
4005 struct shmid_ds dsarg;
4006 struct shminfo shminfo;
4007 struct shm_info shm_info;
4008 abi_long ret = -TARGET_EINVAL;
4009
4010 cmd &= 0xff;
4011
4012 switch(cmd) {
4013 case IPC_STAT:
4014 case IPC_SET:
4015 case SHM_STAT:
4016 if (target_to_host_shmid_ds(&dsarg, buf))
4017 return -TARGET_EFAULT;
4018 ret = get_errno(shmctl(shmid, cmd, &dsarg));
4019 if (host_to_target_shmid_ds(buf, &dsarg))
4020 return -TARGET_EFAULT;
4021 break;
4022 case IPC_INFO:
4023 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
4024 if (host_to_target_shminfo(buf, &shminfo))
4025 return -TARGET_EFAULT;
4026 break;
4027 case SHM_INFO:
4028 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
4029 if (host_to_target_shm_info(buf, &shm_info))
4030 return -TARGET_EFAULT;
4031 break;
4032 case IPC_RMID:
4033 case SHM_LOCK:
4034 case SHM_UNLOCK:
4035 ret = get_errno(shmctl(shmid, cmd, NULL));
4036 break;
4037 }
4038
4039 return ret;
4040}
4041
4042static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
4043{
4044 abi_long raddr;
4045 void *host_raddr;
4046 struct shmid_ds shm_info;
4047 int i,ret;
4048
4049 /* find out the length of the shared memory segment */
4050 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
4051 if (is_error(ret)) {
4052 /* can't get length, bail out */
4053 return ret;
4054 }
4055
4056 mmap_lock();
4057
4058 if (shmaddr)
4059 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
4060 else {
4061 abi_ulong mmap_start;
4062
4063 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
4064
4065 if (mmap_start == -1) {
4066 errno = ENOMEM;
4067 host_raddr = (void *)-1;
4068 } else
4069 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
4070 }
4071
4072 if (host_raddr == (void *)-1) {
4073 mmap_unlock();
4074 return get_errno((long)host_raddr);
4075 }
4076 raddr=h2g((unsigned long)host_raddr);
4077
4078 page_set_flags(raddr, raddr + shm_info.shm_segsz,
4079 PAGE_VALID | PAGE_READ |
4080 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
4081
4082 for (i = 0; i < N_SHM_REGIONS; i++) {
b6e17875
PM
4083 if (!shm_regions[i].in_use) {
4084 shm_regions[i].in_use = true;
88a8c984
RV
4085 shm_regions[i].start = raddr;
4086 shm_regions[i].size = shm_info.shm_segsz;
4087 break;
4088 }
4089 }
4090
4091 mmap_unlock();
4092 return raddr;
4093
4094}
4095
4096static inline abi_long do_shmdt(abi_ulong shmaddr)
4097{
4098 int i;
4099
4100 for (i = 0; i < N_SHM_REGIONS; ++i) {
b6e17875
PM
4101 if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
4102 shm_regions[i].in_use = false;
e00ac249 4103 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
88a8c984
RV
4104 break;
4105 }
4106 }
4107
4108 return get_errno(shmdt(g2h(shmaddr)));
4109}
4110
1c54ff97 4111#ifdef TARGET_NR_ipc
53a5960a 4112/* ??? This only works with linear mappings. */
0da46a6e 4113/* do_ipc() must return target values and target errnos. */
37ed0956
TM
4114static abi_long do_ipc(unsigned int call, abi_long first,
4115 abi_long second, abi_long third,
992f48a0 4116 abi_long ptr, abi_long fifth)
8853f86e
FB
4117{
4118 int version;
992f48a0 4119 abi_long ret = 0;
8853f86e
FB
4120
4121 version = call >> 16;
4122 call &= 0xffff;
4123
4124 switch (call) {
fa294816 4125 case IPCOP_semop:
e5289087 4126 ret = do_semop(first, ptr, second);
fa294816
TS
4127 break;
4128
4129 case IPCOP_semget:
4130 ret = get_errno(semget(first, second, third));
4131 break;
4132
5d2fa8eb
TM
4133 case IPCOP_semctl: {
4134 /* The semun argument to semctl is passed by value, so dereference the
4135 * ptr argument. */
4136 abi_ulong atptr;
37ed0956 4137 get_user_ual(atptr, ptr);
d1c002b6 4138 ret = do_semctl(first, second, third, atptr);
fa294816 4139 break;
5d2fa8eb 4140 }
d96372ef 4141
1c54ff97
AJ
4142 case IPCOP_msgget:
4143 ret = get_errno(msgget(first, second));
4144 break;
d96372ef 4145
1c54ff97
AJ
4146 case IPCOP_msgsnd:
4147 ret = do_msgsnd(first, ptr, second, third);
4148 break;
d96372ef 4149
1c54ff97
AJ
4150 case IPCOP_msgctl:
4151 ret = do_msgctl(first, second, ptr);
4152 break;
d96372ef 4153
1c54ff97
AJ
4154 case IPCOP_msgrcv:
4155 switch (version) {
4156 case 0:
4157 {
4158 struct target_ipc_kludge {
4159 abi_long msgp;
4160 abi_long msgtyp;
4161 } *tmp;
4162
4163 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4164 ret = -TARGET_EFAULT;
4165 break;
4166 }
d96372ef 4167
79dd77de 4168 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
d96372ef 4169
1c54ff97
AJ
4170 unlock_user_struct(tmp, ptr, 0);
4171 break;
4172 }
4173 default:
4174 ret = do_msgrcv(first, ptr, second, fifth, third);
4175 }
4176 break;
d96372ef 4177
8853f86e 4178 case IPCOP_shmat:
88a8c984
RV
4179 switch (version) {
4180 default:
5a4a898d
FB
4181 {
4182 abi_ulong raddr;
88a8c984
RV
4183 raddr = do_shmat(first, ptr, second);
4184 if (is_error(raddr))
4185 return get_errno(raddr);
2f619698 4186 if (put_user_ual(raddr, third))
5a4a898d 4187 return -TARGET_EFAULT;
88a8c984
RV
4188 break;
4189 }
4190 case 1:
4191 ret = -TARGET_EINVAL;
4192 break;
5a4a898d 4193 }
8853f86e
FB
4194 break;
4195 case IPCOP_shmdt:
88a8c984 4196 ret = do_shmdt(ptr);
8853f86e
FB
4197 break;
4198
4199 case IPCOP_shmget:
4200 /* IPC_* flag values are the same on all linux platforms */
4201 ret = get_errno(shmget(first, second, third));
4202 break;
4203
4204 /* IPC_* and SHM_* command values are the same on all linux platforms */
4205 case IPCOP_shmctl:
a2926784 4206 ret = do_shmctl(first, second, ptr);
8853f86e
FB
4207 break;
4208 default:
32407103 4209 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
0da46a6e 4210 ret = -TARGET_ENOSYS;
8853f86e
FB
4211 break;
4212 }
4213 return ret;
4214}
32407103 4215#endif
8853f86e 4216
31e31b8a 4217/* kernel structure types definitions */
31e31b8a 4218
001faf32 4219#define STRUCT(name, ...) STRUCT_ ## name,
31e31b8a
FB
4220#define STRUCT_SPECIAL(name) STRUCT_ ## name,
4221enum {
4222#include "syscall_types.h"
8be656b8 4223STRUCT_MAX
31e31b8a
FB
4224};
4225#undef STRUCT
4226#undef STRUCT_SPECIAL
4227
001faf32 4228#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
31e31b8a
FB
4229#define STRUCT_SPECIAL(name)
4230#include "syscall_types.h"
4231#undef STRUCT
4232#undef STRUCT_SPECIAL
4233
d2ef05bb
PM
4234typedef struct IOCTLEntry IOCTLEntry;
4235
4236typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4237 int fd, int cmd, abi_long arg);
d2ef05bb
PM
4238
4239struct IOCTLEntry {
9c6bf9c7 4240 int target_cmd;
2ab83ea7 4241 unsigned int host_cmd;
31e31b8a
FB
4242 const char *name;
4243 int access;
d2ef05bb 4244 do_ioctl_fn *do_ioctl;
1a9353d2 4245 const argtype arg_type[5];
d2ef05bb 4246};
31e31b8a
FB
4247
4248#define IOC_R 0x0001
4249#define IOC_W 0x0002
4250#define IOC_RW (IOC_R | IOC_W)
4251
4252#define MAX_STRUCT_SIZE 4096
4253
dace20dc 4254#ifdef CONFIG_FIEMAP
285da2b9
PM
4255/* So fiemap access checks don't overflow on 32 bit systems.
4256 * This is very slightly smaller than the limit imposed by
4257 * the underlying kernel.
4258 */
4259#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4260 / sizeof(struct fiemap_extent))
4261
4262static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4263 int fd, int cmd, abi_long arg)
285da2b9
PM
4264{
4265 /* The parameter for this ioctl is a struct fiemap followed
4266 * by an array of struct fiemap_extent whose size is set
4267 * in fiemap->fm_extent_count. The array is filled in by the
4268 * ioctl.
4269 */
4270 int target_size_in, target_size_out;
4271 struct fiemap *fm;
4272 const argtype *arg_type = ie->arg_type;
4273 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4274 void *argptr, *p;
4275 abi_long ret;
4276 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4277 uint32_t outbufsz;
4278 int free_fm = 0;
4279
4280 assert(arg_type[0] == TYPE_PTR);
4281 assert(ie->access == IOC_RW);
4282 arg_type++;
4283 target_size_in = thunk_type_size(arg_type, 0);
4284 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4285 if (!argptr) {
4286 return -TARGET_EFAULT;
4287 }
4288 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4289 unlock_user(argptr, arg, 0);
4290 fm = (struct fiemap *)buf_temp;
4291 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
4292 return -TARGET_EINVAL;
4293 }
4294
4295 outbufsz = sizeof (*fm) +
4296 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
4297
4298 if (outbufsz > MAX_STRUCT_SIZE) {
4299 /* We can't fit all the extents into the fixed size buffer.
4300 * Allocate one that is large enough and use it instead.
4301 */
0e173b24 4302 fm = g_try_malloc(outbufsz);
285da2b9
PM
4303 if (!fm) {
4304 return -TARGET_ENOMEM;
4305 }
4306 memcpy(fm, buf_temp, sizeof(struct fiemap));
4307 free_fm = 1;
4308 }
49ca6f3e 4309 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
285da2b9
PM
4310 if (!is_error(ret)) {
4311 target_size_out = target_size_in;
4312 /* An extent_count of 0 means we were only counting the extents
4313 * so there are no structs to copy
4314 */
4315 if (fm->fm_extent_count != 0) {
4316 target_size_out += fm->fm_mapped_extents * extent_size;
4317 }
4318 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
4319 if (!argptr) {
4320 ret = -TARGET_EFAULT;
4321 } else {
4322 /* Convert the struct fiemap */
4323 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
4324 if (fm->fm_extent_count != 0) {
4325 p = argptr + target_size_in;
4326 /* ...and then all the struct fiemap_extents */
4327 for (i = 0; i < fm->fm_mapped_extents; i++) {
4328 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
4329 THUNK_TARGET);
4330 p += extent_size;
4331 }
4332 }
4333 unlock_user(argptr, arg, target_size_out);
4334 }
4335 }
4336 if (free_fm) {
0e173b24 4337 g_free(fm);
285da2b9
PM
4338 }
4339 return ret;
4340}
dace20dc 4341#endif
285da2b9 4342
059c2f2c 4343static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4344 int fd, int cmd, abi_long arg)
059c2f2c
LV
4345{
4346 const argtype *arg_type = ie->arg_type;
4347 int target_size;
4348 void *argptr;
4349 int ret;
4350 struct ifconf *host_ifconf;
4351 uint32_t outbufsz;
4352 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
4353 int target_ifreq_size;
4354 int nb_ifreq;
4355 int free_buf = 0;
4356 int i;
4357 int target_ifc_len;
4358 abi_long target_ifc_buf;
4359 int host_ifc_len;
4360 char *host_ifc_buf;
4361
4362 assert(arg_type[0] == TYPE_PTR);
4363 assert(ie->access == IOC_RW);
4364
4365 arg_type++;
4366 target_size = thunk_type_size(arg_type, 0);
4367
4368 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4369 if (!argptr)
4370 return -TARGET_EFAULT;
4371 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4372 unlock_user(argptr, arg, 0);
4373
4374 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
4375 target_ifc_len = host_ifconf->ifc_len;
4376 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
4377
4378 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
4379 nb_ifreq = target_ifc_len / target_ifreq_size;
4380 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
4381
4382 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
4383 if (outbufsz > MAX_STRUCT_SIZE) {
4384 /* We can't fit all the extents into the fixed size buffer.
4385 * Allocate one that is large enough and use it instead.
4386 */
4387 host_ifconf = malloc(outbufsz);
4388 if (!host_ifconf) {
4389 return -TARGET_ENOMEM;
4390 }
4391 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
4392 free_buf = 1;
4393 }
4394 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
4395
4396 host_ifconf->ifc_len = host_ifc_len;
4397 host_ifconf->ifc_buf = host_ifc_buf;
4398
49ca6f3e 4399 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
059c2f2c
LV
4400 if (!is_error(ret)) {
4401 /* convert host ifc_len to target ifc_len */
4402
4403 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
4404 target_ifc_len = nb_ifreq * target_ifreq_size;
4405 host_ifconf->ifc_len = target_ifc_len;
4406
4407 /* restore target ifc_buf */
4408
4409 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
4410
4411 /* copy struct ifconf to target user */
4412
4413 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4414 if (!argptr)
4415 return -TARGET_EFAULT;
4416 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
4417 unlock_user(argptr, arg, target_size);
4418
4419 /* copy ifreq[] to target user */
4420
4421 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
4422 for (i = 0; i < nb_ifreq ; i++) {
4423 thunk_convert(argptr + i * target_ifreq_size,
4424 host_ifc_buf + i * sizeof(struct ifreq),
4425 ifreq_arg_type, THUNK_TARGET);
4426 }
4427 unlock_user(argptr, target_ifc_buf, target_ifc_len);
4428 }
4429
4430 if (free_buf) {
4431 free(host_ifconf);
4432 }
4433
4434 return ret;
4435}
4436
56e904ec 4437static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
45c874eb 4438 int cmd, abi_long arg)
56e904ec
AG
4439{
4440 void *argptr;
4441 struct dm_ioctl *host_dm;
4442 abi_long guest_data;
4443 uint32_t guest_data_size;
4444 int target_size;
4445 const argtype *arg_type = ie->arg_type;
4446 abi_long ret;
4447 void *big_buf = NULL;
4448 char *host_data;
4449
4450 arg_type++;
4451 target_size = thunk_type_size(arg_type, 0);
4452 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4453 if (!argptr) {
4454 ret = -TARGET_EFAULT;
4455 goto out;
4456 }
4457 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4458 unlock_user(argptr, arg, 0);
4459
4460 /* buf_temp is too small, so fetch things into a bigger buffer */
4461 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
4462 memcpy(big_buf, buf_temp, target_size);
4463 buf_temp = big_buf;
4464 host_dm = big_buf;
4465
4466 guest_data = arg + host_dm->data_start;
4467 if ((guest_data - arg) < 0) {
4468 ret = -EINVAL;
4469 goto out;
4470 }
4471 guest_data_size = host_dm->data_size - host_dm->data_start;
4472 host_data = (char*)host_dm + host_dm->data_start;
4473
4474 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
4475 switch (ie->host_cmd) {
4476 case DM_REMOVE_ALL:
4477 case DM_LIST_DEVICES:
4478 case DM_DEV_CREATE:
4479 case DM_DEV_REMOVE:
4480 case DM_DEV_SUSPEND:
4481 case DM_DEV_STATUS:
4482 case DM_DEV_WAIT:
4483 case DM_TABLE_STATUS:
4484 case DM_TABLE_CLEAR:
4485 case DM_TABLE_DEPS:
4486 case DM_LIST_VERSIONS:
4487 /* no input data */
4488 break;
4489 case DM_DEV_RENAME:
4490 case DM_DEV_SET_GEOMETRY:
4491 /* data contains only strings */
4492 memcpy(host_data, argptr, guest_data_size);
4493 break;
4494 case DM_TARGET_MSG:
4495 memcpy(host_data, argptr, guest_data_size);
4496 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
4497 break;
4498 case DM_TABLE_LOAD:
4499 {
4500 void *gspec = argptr;
4501 void *cur_data = host_data;
4502 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
4503 int spec_size = thunk_type_size(arg_type, 0);
4504 int i;
4505
4506 for (i = 0; i < host_dm->target_count; i++) {
4507 struct dm_target_spec *spec = cur_data;
4508 uint32_t next;
4509 int slen;
4510
4511 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
4512 slen = strlen((char*)gspec + spec_size) + 1;
4513 next = spec->next;
4514 spec->next = sizeof(*spec) + slen;
4515 strcpy((char*)&spec[1], gspec + spec_size);
4516 gspec += next;
4517 cur_data += spec->next;
4518 }
4519 break;
4520 }
4521 default:
4522 ret = -TARGET_EINVAL;
dec0473d 4523 unlock_user(argptr, guest_data, 0);
56e904ec
AG
4524 goto out;
4525 }
4526 unlock_user(argptr, guest_data, 0);
4527
49ca6f3e 4528 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
56e904ec
AG
4529 if (!is_error(ret)) {
4530 guest_data = arg + host_dm->data_start;
4531 guest_data_size = host_dm->data_size - host_dm->data_start;
4532 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
4533 switch (ie->host_cmd) {
4534 case DM_REMOVE_ALL:
4535 case DM_DEV_CREATE:
4536 case DM_DEV_REMOVE:
4537 case DM_DEV_RENAME:
4538 case DM_DEV_SUSPEND:
4539 case DM_DEV_STATUS:
4540 case DM_TABLE_LOAD:
4541 case DM_TABLE_CLEAR:
4542 case DM_TARGET_MSG:
4543 case DM_DEV_SET_GEOMETRY:
4544 /* no return data */
4545 break;
4546 case DM_LIST_DEVICES:
4547 {
4548 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
4549 uint32_t remaining_data = guest_data_size;
4550 void *cur_data = argptr;
4551 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
4552 int nl_size = 12; /* can't use thunk_size due to alignment */
4553
4554 while (1) {
4555 uint32_t next = nl->next;
4556 if (next) {
4557 nl->next = nl_size + (strlen(nl->name) + 1);
4558 }
4559 if (remaining_data < nl->next) {
4560 host_dm->flags |= DM_BUFFER_FULL_FLAG;
4561 break;
4562 }
4563 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
4564 strcpy(cur_data + nl_size, nl->name);
4565 cur_data += nl->next;
4566 remaining_data -= nl->next;
4567 if (!next) {
4568 break;
4569 }
4570 nl = (void*)nl + next;
4571 }
4572 break;
4573 }
4574 case DM_DEV_WAIT:
4575 case DM_TABLE_STATUS:
4576 {
4577 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
4578 void *cur_data = argptr;
4579 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
4580 int spec_size = thunk_type_size(arg_type, 0);
4581 int i;
4582
4583 for (i = 0; i < host_dm->target_count; i++) {
4584 uint32_t next = spec->next;
4585 int slen = strlen((char*)&spec[1]) + 1;
4586 spec->next = (cur_data - argptr) + spec_size + slen;
4587 if (guest_data_size < spec->next) {
4588 host_dm->flags |= DM_BUFFER_FULL_FLAG;
4589 break;
4590 }
4591 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
4592 strcpy(cur_data + spec_size, (char*)&spec[1]);
4593 cur_data = argptr + spec->next;
4594 spec = (void*)host_dm + host_dm->data_start + next;
4595 }
4596 break;
4597 }
4598 case DM_TABLE_DEPS:
4599 {
4600 void *hdata = (void*)host_dm + host_dm->data_start;
4601 int count = *(uint32_t*)hdata;
4602 uint64_t *hdev = hdata + 8;
4603 uint64_t *gdev = argptr + 8;
4604 int i;
4605
4606 *(uint32_t*)argptr = tswap32(count);
4607 for (i = 0; i < count; i++) {
4608 *gdev = tswap64(*hdev);
4609 gdev++;
4610 hdev++;
4611 }
4612 break;
4613 }
4614 case DM_LIST_VERSIONS:
4615 {
4616 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
4617 uint32_t remaining_data = guest_data_size;
4618 void *cur_data = argptr;
4619 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
4620 int vers_size = thunk_type_size(arg_type, 0);
4621
4622 while (1) {
4623 uint32_t next = vers->next;
4624 if (next) {
4625 vers->next = vers_size + (strlen(vers->name) + 1);
4626 }
4627 if (remaining_data < vers->next) {
4628 host_dm->flags |= DM_BUFFER_FULL_FLAG;
4629 break;
4630 }
4631 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
4632 strcpy(cur_data + vers_size, vers->name);
4633 cur_data += vers->next;
4634 remaining_data -= vers->next;
4635 if (!next) {
4636 break;
4637 }
4638 vers = (void*)vers + next;
4639 }
4640 break;
4641 }
4642 default:
dec0473d 4643 unlock_user(argptr, guest_data, 0);
56e904ec
AG
4644 ret = -TARGET_EINVAL;
4645 goto out;
4646 }
4647 unlock_user(argptr, guest_data, guest_data_size);
4648
4649 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4650 if (!argptr) {
4651 ret = -TARGET_EFAULT;
4652 goto out;
4653 }
4654 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4655 unlock_user(argptr, arg, target_size);
4656 }
4657out:
ad11ad77 4658 g_free(big_buf);
56e904ec
AG
4659 return ret;
4660}
4661
a59b5e35 4662static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
45c874eb 4663 int cmd, abi_long arg)
a59b5e35
AG
4664{
4665 void *argptr;
4666 int target_size;
4667 const argtype *arg_type = ie->arg_type;
4668 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
4669 abi_long ret;
4670
4671 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
4672 struct blkpg_partition host_part;
4673
4674 /* Read and convert blkpg */
4675 arg_type++;
4676 target_size = thunk_type_size(arg_type, 0);
4677 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4678 if (!argptr) {
4679 ret = -TARGET_EFAULT;
4680 goto out;
4681 }
4682 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4683 unlock_user(argptr, arg, 0);
4684
4685 switch (host_blkpg->op) {
4686 case BLKPG_ADD_PARTITION:
4687 case BLKPG_DEL_PARTITION:
4688 /* payload is struct blkpg_partition */
4689 break;
4690 default:
4691 /* Unknown opcode */
4692 ret = -TARGET_EINVAL;
4693 goto out;
4694 }
4695
4696 /* Read and convert blkpg->data */
4697 arg = (abi_long)(uintptr_t)host_blkpg->data;
4698 target_size = thunk_type_size(part_arg_type, 0);
4699 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4700 if (!argptr) {
4701 ret = -TARGET_EFAULT;
4702 goto out;
4703 }
4704 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
4705 unlock_user(argptr, arg, 0);
4706
4707 /* Swizzle the data pointer to our local copy and call! */
4708 host_blkpg->data = &host_part;
49ca6f3e 4709 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
a59b5e35
AG
4710
4711out:
4712 return ret;
4713}
4714
7ff7b666 4715static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4716 int fd, int cmd, abi_long arg)
7ff7b666
LV
4717{
4718 const argtype *arg_type = ie->arg_type;
4719 const StructEntry *se;
4720 const argtype *field_types;
4721 const int *dst_offsets, *src_offsets;
4722 int target_size;
4723 void *argptr;
4724 abi_ulong *target_rt_dev_ptr;
4725 unsigned long *host_rt_dev_ptr;
4726 abi_long ret;
4727 int i;
4728
4729 assert(ie->access == IOC_W);
4730 assert(*arg_type == TYPE_PTR);
4731 arg_type++;
4732 assert(*arg_type == TYPE_STRUCT);
4733 target_size = thunk_type_size(arg_type, 0);
4734 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4735 if (!argptr) {
4736 return -TARGET_EFAULT;
4737 }
4738 arg_type++;
4739 assert(*arg_type == (int)STRUCT_rtentry);
4740 se = struct_entries + *arg_type++;
4741 assert(se->convert[0] == NULL);
4742 /* convert struct here to be able to catch rt_dev string */
4743 field_types = se->field_types;
4744 dst_offsets = se->field_offsets[THUNK_HOST];
4745 src_offsets = se->field_offsets[THUNK_TARGET];
4746 for (i = 0; i < se->nb_fields; i++) {
4747 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
4748 assert(*field_types == TYPE_PTRVOID);
4749 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
4750 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
4751 if (*target_rt_dev_ptr != 0) {
4752 *host_rt_dev_ptr = (unsigned long)lock_user_string(
4753 tswapal(*target_rt_dev_ptr));
4754 if (!*host_rt_dev_ptr) {
4755 unlock_user(argptr, arg, 0);
4756 return -TARGET_EFAULT;
4757 }
4758 } else {
4759 *host_rt_dev_ptr = 0;
4760 }
4761 field_types++;
4762 continue;
4763 }
4764 field_types = thunk_convert(buf_temp + dst_offsets[i],
4765 argptr + src_offsets[i],
4766 field_types, THUNK_HOST);
4767 }
4768 unlock_user(argptr, arg, 0);
4769
49ca6f3e 4770 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
7ff7b666
LV
4771 if (*host_rt_dev_ptr != 0) {
4772 unlock_user((void *)*host_rt_dev_ptr,
4773 *target_rt_dev_ptr, 0);
4774 }
4775 return ret;
4776}
4777
ca56f5b5 4778static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4779 int fd, int cmd, abi_long arg)
ca56f5b5
PB
4780{
4781 int sig = target_to_host_signal(arg);
49ca6f3e 4782 return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
ca56f5b5
PB
4783}
4784
9f106a75 4785static IOCTLEntry ioctl_entries[] = {
001faf32 4786#define IOCTL(cmd, access, ...) \
d2ef05bb
PM
4787 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
4788#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
4789 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
31e31b8a
FB
4790#include "ioctls.h"
4791 { 0, 0, },
4792};
4793
53a5960a 4794/* ??? Implement proper locking for ioctls. */
0da46a6e 4795/* do_ioctl() Must return target values and target errnos. */
45c874eb 4796static abi_long do_ioctl(int fd, int cmd, abi_long arg)
31e31b8a
FB
4797{
4798 const IOCTLEntry *ie;
4799 const argtype *arg_type;
992f48a0 4800 abi_long ret;
31e31b8a 4801 uint8_t buf_temp[MAX_STRUCT_SIZE];
53a5960a
PB
4802 int target_size;
4803 void *argptr;
31e31b8a
FB
4804
4805 ie = ioctl_entries;
4806 for(;;) {
4807 if (ie->target_cmd == 0) {
32407103 4808 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
0da46a6e 4809 return -TARGET_ENOSYS;
31e31b8a
FB
4810 }
4811 if (ie->target_cmd == cmd)
4812 break;
4813 ie++;
4814 }
4815 arg_type = ie->arg_type;
9de5e440 4816#if defined(DEBUG)
32407103 4817 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
72f03900 4818#endif
d2ef05bb
PM
4819 if (ie->do_ioctl) {
4820 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
4821 }
4822
31e31b8a
FB
4823 switch(arg_type[0]) {
4824 case TYPE_NULL:
4825 /* no argument */
49ca6f3e 4826 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
31e31b8a
FB
4827 break;
4828 case TYPE_PTRVOID:
4829 case TYPE_INT:
49ca6f3e 4830 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
31e31b8a
FB
4831 break;
4832 case TYPE_PTR:
4833 arg_type++;
53a5960a 4834 target_size = thunk_type_size(arg_type, 0);
31e31b8a
FB
4835 switch(ie->access) {
4836 case IOC_R:
49ca6f3e 4837 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a 4838 if (!is_error(ret)) {
579a97f7
FB
4839 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4840 if (!argptr)
4841 return -TARGET_EFAULT;
53a5960a
PB
4842 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4843 unlock_user(argptr, arg, target_size);
31e31b8a
FB
4844 }
4845 break;
4846 case IOC_W:
579a97f7
FB
4847 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4848 if (!argptr)
4849 return -TARGET_EFAULT;
53a5960a
PB
4850 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4851 unlock_user(argptr, arg, 0);
49ca6f3e 4852 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a
FB
4853 break;
4854 default:
4855 case IOC_RW:
579a97f7
FB
4856 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4857 if (!argptr)
4858 return -TARGET_EFAULT;
53a5960a
PB
4859 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4860 unlock_user(argptr, arg, 0);
49ca6f3e 4861 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a 4862 if (!is_error(ret)) {
579a97f7
FB
4863 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4864 if (!argptr)
4865 return -TARGET_EFAULT;
53a5960a
PB
4866 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
4867 unlock_user(argptr, arg, target_size);
31e31b8a
FB
4868 }
4869 break;
4870 }
4871 break;
4872 default:
32407103
JM
4873 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
4874 (long)cmd, arg_type[0]);
0da46a6e 4875 ret = -TARGET_ENOSYS;
31e31b8a
FB
4876 break;
4877 }
4878 return ret;
4879}
4880
b39bc503 4881static const bitmask_transtbl iflag_tbl[] = {
31e31b8a
FB
4882 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
4883 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
4884 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
4885 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
4886 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
4887 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
4888 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
4889 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
4890 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
4891 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
4892 { TARGET_IXON, TARGET_IXON, IXON, IXON },
4893 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
4894 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
4895 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
4896 { 0, 0, 0, 0 }
4897};
4898
b39bc503 4899static const bitmask_transtbl oflag_tbl[] = {
31e31b8a
FB
4900 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
4901 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
4902 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
4903 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
4904 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
4905 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
4906 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
4907 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
4908 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
4909 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
4910 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
4911 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
4912 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
4913 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
4914 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
4915 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
4916 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
4917 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
4918 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
4919 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
4920 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
4921 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
4922 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
4923 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
4924 { 0, 0, 0, 0 }
4925};
4926
b39bc503 4927static const bitmask_transtbl cflag_tbl[] = {
31e31b8a
FB
4928 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
4929 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
4930 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
4931 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
4932 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
4933 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
4934 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
4935 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
4936 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
4937 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
4938 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
4939 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
4940 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
4941 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
4942 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
4943 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
4944 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
4945 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
4946 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
4947 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
4948 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
4949 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
4950 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
4951 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
4952 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
4953 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
4954 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
4955 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
4956 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
4957 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
4958 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
4959 { 0, 0, 0, 0 }
4960};
4961
b39bc503 4962static const bitmask_transtbl lflag_tbl[] = {
31e31b8a
FB
4963 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
4964 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
4965 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
4966 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
4967 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
4968 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
4969 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
4970 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
4971 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
4972 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
4973 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
4974 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
4975 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
4976 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
4977 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
4978 { 0, 0, 0, 0 }
4979};
4980
4981static void target_to_host_termios (void *dst, const void *src)
4982{
4983 struct host_termios *host = dst;
4984 const struct target_termios *target = src;
3b46e624 4985
5fafdf24 4986 host->c_iflag =
31e31b8a 4987 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5fafdf24 4988 host->c_oflag =
31e31b8a 4989 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5fafdf24 4990 host->c_cflag =
31e31b8a 4991 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5fafdf24 4992 host->c_lflag =
31e31b8a
FB
4993 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
4994 host->c_line = target->c_line;
3b46e624 4995
44607123 4996 memset(host->c_cc, 0, sizeof(host->c_cc));
5fafdf24
TS
4997 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
4998 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3b46e624 4999 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
5fafdf24 5000 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3b46e624 5001 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
5fafdf24 5002 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3b46e624 5003 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
5fafdf24 5004 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3b46e624 5005 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
5fafdf24
TS
5006 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
5007 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3b46e624
TS
5008 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
5009 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
5010 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
5011 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
5012 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
5fafdf24 5013 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
31e31b8a 5014}
3b46e624 5015
31e31b8a
FB
5016static void host_to_target_termios (void *dst, const void *src)
5017{
5018 struct target_termios *target = dst;
5019 const struct host_termios *host = src;
5020
5fafdf24 5021 target->c_iflag =
31e31b8a 5022 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5fafdf24 5023 target->c_oflag =
31e31b8a 5024 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5fafdf24 5025 target->c_cflag =
31e31b8a 5026 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5fafdf24 5027 target->c_lflag =
31e31b8a
FB
5028 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
5029 target->c_line = host->c_line;
3b46e624 5030
44607123 5031 memset(target->c_cc, 0, sizeof(target->c_cc));
31e31b8a
FB
5032 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
5033 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
5034 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
5035 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
5036 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
5037 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
5038 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
5039 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
5040 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
5041 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
5042 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
5043 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
5044 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
5045 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
5046 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
5047 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
5048 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
5049}
5050
8e853dc7 5051static const StructEntry struct_termios_def = {
31e31b8a
FB
5052 .convert = { host_to_target_termios, target_to_host_termios },
5053 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
5054 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
5055};
5056
5286db75
FB
5057static bitmask_transtbl mmap_flags_tbl[] = {
5058 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
5059 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
5060 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
5061 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
5062 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
5063 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
5064 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
5065 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
e8efd8e7
CL
5066 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, MAP_NORESERVE,
5067 MAP_NORESERVE },
5286db75
FB
5068 { 0, 0, 0, 0 }
5069};
5070
2ab83ea7 5071#if defined(TARGET_I386)
6dbad63e
FB
5072
5073/* NOTE: there is really one LDT for all the threads */
b1d8e52e 5074static uint8_t *ldt_table;
6dbad63e 5075
03acab66 5076static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
6dbad63e
FB
5077{
5078 int size;
53a5960a 5079 void *p;
6dbad63e
FB
5080
5081 if (!ldt_table)
5082 return 0;
5083 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
5084 if (size > bytecount)
5085 size = bytecount;
579a97f7
FB
5086 p = lock_user(VERIFY_WRITE, ptr, size, 0);
5087 if (!p)
03acab66 5088 return -TARGET_EFAULT;
579a97f7 5089 /* ??? Should this by byteswapped? */
53a5960a
PB
5090 memcpy(p, ldt_table, size);
5091 unlock_user(p, ptr, size);
6dbad63e
FB
5092 return size;
5093}
5094
5095/* XXX: add locking support */
03acab66
FB
5096static abi_long write_ldt(CPUX86State *env,
5097 abi_ulong ptr, unsigned long bytecount, int oldmode)
6dbad63e
FB
5098{
5099 struct target_modify_ldt_ldt_s ldt_info;
53a5960a 5100 struct target_modify_ldt_ldt_s *target_ldt_info;
6dbad63e 5101 int seg_32bit, contents, read_exec_only, limit_in_pages;
8d18e893 5102 int seg_not_present, useable, lm;
6dbad63e
FB
5103 uint32_t *lp, entry_1, entry_2;
5104
5105 if (bytecount != sizeof(ldt_info))
03acab66 5106 return -TARGET_EINVAL;
579a97f7 5107 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
03acab66 5108 return -TARGET_EFAULT;
53a5960a 5109 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 5110 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
53a5960a
PB
5111 ldt_info.limit = tswap32(target_ldt_info->limit);
5112 ldt_info.flags = tswap32(target_ldt_info->flags);
5113 unlock_user_struct(target_ldt_info, ptr, 0);
3b46e624 5114
6dbad63e 5115 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
03acab66 5116 return -TARGET_EINVAL;
6dbad63e
FB
5117 seg_32bit = ldt_info.flags & 1;
5118 contents = (ldt_info.flags >> 1) & 3;
5119 read_exec_only = (ldt_info.flags >> 3) & 1;
5120 limit_in_pages = (ldt_info.flags >> 4) & 1;
5121 seg_not_present = (ldt_info.flags >> 5) & 1;
5122 useable = (ldt_info.flags >> 6) & 1;
8d18e893
FB
5123#ifdef TARGET_ABI32
5124 lm = 0;
5125#else
5126 lm = (ldt_info.flags >> 7) & 1;
5127#endif
6dbad63e
FB
5128 if (contents == 3) {
5129 if (oldmode)
03acab66 5130 return -TARGET_EINVAL;
6dbad63e 5131 if (seg_not_present == 0)
03acab66 5132 return -TARGET_EINVAL;
6dbad63e
FB
5133 }
5134 /* allocate the LDT */
5135 if (!ldt_table) {
e441570f
AZ
5136 env->ldt.base = target_mmap(0,
5137 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
5138 PROT_READ|PROT_WRITE,
5139 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
5140 if (env->ldt.base == -1)
03acab66 5141 return -TARGET_ENOMEM;
e441570f
AZ
5142 memset(g2h(env->ldt.base), 0,
5143 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
6dbad63e 5144 env->ldt.limit = 0xffff;
e441570f 5145 ldt_table = g2h(env->ldt.base);
6dbad63e
FB
5146 }
5147
5148 /* NOTE: same code as Linux kernel */
5149 /* Allow LDTs to be cleared by the user. */
5150 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
5151 if (oldmode ||
5152 (contents == 0 &&
5153 read_exec_only == 1 &&
5154 seg_32bit == 0 &&
5155 limit_in_pages == 0 &&
5156 seg_not_present == 1 &&
5157 useable == 0 )) {
5158 entry_1 = 0;
5159 entry_2 = 0;
5160 goto install;
5161 }
5162 }
3b46e624 5163
6dbad63e
FB
5164 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
5165 (ldt_info.limit & 0x0ffff);
5166 entry_2 = (ldt_info.base_addr & 0xff000000) |
5167 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
5168 (ldt_info.limit & 0xf0000) |
5169 ((read_exec_only ^ 1) << 9) |
5170 (contents << 10) |
5171 ((seg_not_present ^ 1) << 15) |
5172 (seg_32bit << 22) |
5173 (limit_in_pages << 23) |
8d18e893 5174 (lm << 21) |
6dbad63e
FB
5175 0x7000;
5176 if (!oldmode)
5177 entry_2 |= (useable << 20);
14ae3ba7 5178
6dbad63e
FB
5179 /* Install the new entry ... */
5180install:
5181 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
5182 lp[0] = tswap32(entry_1);
5183 lp[1] = tswap32(entry_2);
5184 return 0;
5185}
5186
5187/* specific and weird i386 syscalls */
8fcd3692
BS
5188static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
5189 unsigned long bytecount)
6dbad63e 5190{
03acab66 5191 abi_long ret;
3b46e624 5192
6dbad63e
FB
5193 switch (func) {
5194 case 0:
5195 ret = read_ldt(ptr, bytecount);
5196 break;
5197 case 1:
5198 ret = write_ldt(env, ptr, bytecount, 1);
5199 break;
5200 case 0x11:
5201 ret = write_ldt(env, ptr, bytecount, 0);
5202 break;
03acab66
FB
5203 default:
5204 ret = -TARGET_ENOSYS;
5205 break;
6dbad63e
FB
5206 }
5207 return ret;
5208}
1b6b029e 5209
4583f589 5210#if defined(TARGET_I386) && defined(TARGET_ABI32)
bc22eb44 5211abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
5212{
5213 uint64_t *gdt_table = g2h(env->gdt.base);
5214 struct target_modify_ldt_ldt_s ldt_info;
5215 struct target_modify_ldt_ldt_s *target_ldt_info;
5216 int seg_32bit, contents, read_exec_only, limit_in_pages;
5217 int seg_not_present, useable, lm;
5218 uint32_t *lp, entry_1, entry_2;
5219 int i;
5220
5221 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
5222 if (!target_ldt_info)
5223 return -TARGET_EFAULT;
5224 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 5225 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
8d18e893
FB
5226 ldt_info.limit = tswap32(target_ldt_info->limit);
5227 ldt_info.flags = tswap32(target_ldt_info->flags);
5228 if (ldt_info.entry_number == -1) {
5229 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
5230 if (gdt_table[i] == 0) {
5231 ldt_info.entry_number = i;
5232 target_ldt_info->entry_number = tswap32(i);
5233 break;
5234 }
5235 }
5236 }
5237 unlock_user_struct(target_ldt_info, ptr, 1);
5238
5239 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
5240 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
5241 return -TARGET_EINVAL;
5242 seg_32bit = ldt_info.flags & 1;
5243 contents = (ldt_info.flags >> 1) & 3;
5244 read_exec_only = (ldt_info.flags >> 3) & 1;
5245 limit_in_pages = (ldt_info.flags >> 4) & 1;
5246 seg_not_present = (ldt_info.flags >> 5) & 1;
5247 useable = (ldt_info.flags >> 6) & 1;
5248#ifdef TARGET_ABI32
5249 lm = 0;
5250#else
5251 lm = (ldt_info.flags >> 7) & 1;
5252#endif
5253
5254 if (contents == 3) {
5255 if (seg_not_present == 0)
5256 return -TARGET_EINVAL;
5257 }
5258
5259 /* NOTE: same code as Linux kernel */
5260 /* Allow LDTs to be cleared by the user. */
5261 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
5262 if ((contents == 0 &&
5263 read_exec_only == 1 &&
5264 seg_32bit == 0 &&
5265 limit_in_pages == 0 &&
5266 seg_not_present == 1 &&
5267 useable == 0 )) {
5268 entry_1 = 0;
5269 entry_2 = 0;
5270 goto install;
5271 }
5272 }
5273
5274 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
5275 (ldt_info.limit & 0x0ffff);
5276 entry_2 = (ldt_info.base_addr & 0xff000000) |
5277 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
5278 (ldt_info.limit & 0xf0000) |
5279 ((read_exec_only ^ 1) << 9) |
5280 (contents << 10) |
5281 ((seg_not_present ^ 1) << 15) |
5282 (seg_32bit << 22) |
5283 (limit_in_pages << 23) |
5284 (useable << 20) |
5285 (lm << 21) |
5286 0x7000;
5287
5288 /* Install the new entry ... */
5289install:
5290 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
5291 lp[0] = tswap32(entry_1);
5292 lp[1] = tswap32(entry_2);
5293 return 0;
5294}
5295
8fcd3692 5296static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
5297{
5298 struct target_modify_ldt_ldt_s *target_ldt_info;
5299 uint64_t *gdt_table = g2h(env->gdt.base);
5300 uint32_t base_addr, limit, flags;
5301 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
5302 int seg_not_present, useable, lm;
5303 uint32_t *lp, entry_1, entry_2;
5304
5305 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
5306 if (!target_ldt_info)
5307 return -TARGET_EFAULT;
5308 idx = tswap32(target_ldt_info->entry_number);
5309 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
5310 idx > TARGET_GDT_ENTRY_TLS_MAX) {
5311 unlock_user_struct(target_ldt_info, ptr, 1);
5312 return -TARGET_EINVAL;
5313 }
5314 lp = (uint32_t *)(gdt_table + idx);
5315 entry_1 = tswap32(lp[0]);
5316 entry_2 = tswap32(lp[1]);
5317
5318 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
5319 contents = (entry_2 >> 10) & 3;
5320 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
5321 seg_32bit = (entry_2 >> 22) & 1;
5322 limit_in_pages = (entry_2 >> 23) & 1;
5323 useable = (entry_2 >> 20) & 1;
5324#ifdef TARGET_ABI32
5325 lm = 0;
5326#else
5327 lm = (entry_2 >> 21) & 1;
5328#endif
5329 flags = (seg_32bit << 0) | (contents << 1) |
5330 (read_exec_only << 3) | (limit_in_pages << 4) |
5331 (seg_not_present << 5) | (useable << 6) | (lm << 7);
5332 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
5333 base_addr = (entry_1 >> 16) |
5334 (entry_2 & 0xff000000) |
5335 ((entry_2 & 0xff) << 16);
cbb21eed 5336 target_ldt_info->base_addr = tswapal(base_addr);
8d18e893
FB
5337 target_ldt_info->limit = tswap32(limit);
5338 target_ldt_info->flags = tswap32(flags);
5339 unlock_user_struct(target_ldt_info, ptr, 1);
5340 return 0;
5341}
4583f589 5342#endif /* TARGET_I386 && TARGET_ABI32 */
8d18e893 5343
d2fd1af7 5344#ifndef TARGET_ABI32
2667e71c 5345abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
d2fd1af7 5346{
1add8698 5347 abi_long ret = 0;
d2fd1af7
FB
5348 abi_ulong val;
5349 int idx;
1add8698 5350
d2fd1af7
FB
5351 switch(code) {
5352 case TARGET_ARCH_SET_GS:
5353 case TARGET_ARCH_SET_FS:
5354 if (code == TARGET_ARCH_SET_GS)
5355 idx = R_GS;
5356 else
5357 idx = R_FS;
5358 cpu_x86_load_seg(env, idx, 0);
5359 env->segs[idx].base = addr;
5360 break;
5361 case TARGET_ARCH_GET_GS:
5362 case TARGET_ARCH_GET_FS:
5363 if (code == TARGET_ARCH_GET_GS)
5364 idx = R_GS;
5365 else
5366 idx = R_FS;
5367 val = env->segs[idx].base;
5368 if (put_user(val, addr, abi_ulong))
1add8698 5369 ret = -TARGET_EFAULT;
d2fd1af7
FB
5370 break;
5371 default:
5372 ret = -TARGET_EINVAL;
5373 break;
5374 }
1add8698 5375 return ret;
d2fd1af7
FB
5376}
5377#endif
5378
2ab83ea7
FB
5379#endif /* defined(TARGET_I386) */
5380
05098a93 5381#define NEW_STACK_SIZE 0x40000
d865bab5 5382
d865bab5
PB
5383
5384static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
5385typedef struct {
9349b4f9 5386 CPUArchState *env;
d865bab5
PB
5387 pthread_mutex_t mutex;
5388 pthread_cond_t cond;
5389 pthread_t thread;
5390 uint32_t tid;
5391 abi_ulong child_tidptr;
5392 abi_ulong parent_tidptr;
5393 sigset_t sigmask;
5394} new_thread_info;
5395
5396static void *clone_func(void *arg)
5397{
5398 new_thread_info *info = arg;
9349b4f9 5399 CPUArchState *env;
0d34282f 5400 CPUState *cpu;
edf8e2af 5401 TaskState *ts;
d865bab5 5402
70903763 5403 rcu_register_thread();
d865bab5 5404 env = info->env;
0d34282f 5405 cpu = ENV_GET_CPU(env);
a2247f8e 5406 thread_cpu = cpu;
0429a971 5407 ts = (TaskState *)cpu->opaque;
d865bab5 5408 info->tid = gettid();
0d34282f 5409 cpu->host_tid = info->tid;
edf8e2af 5410 task_settid(ts);
d865bab5
PB
5411 if (info->child_tidptr)
5412 put_user_u32(info->tid, info->child_tidptr);
5413 if (info->parent_tidptr)
5414 put_user_u32(info->tid, info->parent_tidptr);
5415 /* Enable signals. */
5416 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
5417 /* Signal to the parent that we're ready. */
5418 pthread_mutex_lock(&info->mutex);
5419 pthread_cond_broadcast(&info->cond);
5420 pthread_mutex_unlock(&info->mutex);
5421 /* Wait until the parent has finshed initializing the tls state. */
5422 pthread_mutex_lock(&clone_lock);
5423 pthread_mutex_unlock(&clone_lock);
5424 cpu_loop(env);
5425 /* never exits */
5426 return NULL;
5427}
1b6b029e 5428
0da46a6e
TS
5429/* do_fork() Must return host values and target errnos (unlike most
5430 do_*() functions). */
9349b4f9 5431static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
d865bab5
PB
5432 abi_ulong parent_tidptr, target_ulong newtls,
5433 abi_ulong child_tidptr)
1b6b029e 5434{
0429a971 5435 CPUState *cpu = ENV_GET_CPU(env);
1b6b029e 5436 int ret;
5cd4393b 5437 TaskState *ts;
0429a971 5438 CPUState *new_cpu;
9349b4f9 5439 CPUArchState *new_env;
d865bab5
PB
5440 unsigned int nptl_flags;
5441 sigset_t sigmask;
3b46e624 5442
436d124b
AZ
5443 /* Emulate vfork() with fork() */
5444 if (flags & CLONE_VFORK)
5445 flags &= ~(CLONE_VFORK | CLONE_VM);
5446
1b6b029e 5447 if (flags & CLONE_VM) {
0429a971 5448 TaskState *parent_ts = (TaskState *)cpu->opaque;
d865bab5
PB
5449 new_thread_info info;
5450 pthread_attr_t attr;
24cb36a6 5451
c78d65e8 5452 ts = g_new0(TaskState, 1);
624f7979 5453 init_task_state(ts);
1b6b029e 5454 /* we create a new CPU instance. */
c5be9f08 5455 new_env = cpu_copy(env);
6e68e076
PB
5456 /* Init regs that differ from the parent. */
5457 cpu_clone_regs(new_env, newsp);
0429a971
AF
5458 new_cpu = ENV_GET_CPU(new_env);
5459 new_cpu->opaque = ts;
edf8e2af
MW
5460 ts->bprm = parent_ts->bprm;
5461 ts->info = parent_ts->info;
3d3efba0 5462 ts->signal_mask = parent_ts->signal_mask;
d865bab5
PB
5463 nptl_flags = flags;
5464 flags &= ~CLONE_NPTL_FLAGS2;
5465
c2764719
PB
5466 if (nptl_flags & CLONE_CHILD_CLEARTID) {
5467 ts->child_tidptr = child_tidptr;
5468 }
5469
d865bab5
PB
5470 if (nptl_flags & CLONE_SETTLS)
5471 cpu_set_tls (new_env, newtls);
5472
5473 /* Grab a mutex so that thread setup appears atomic. */
5474 pthread_mutex_lock(&clone_lock);
5475
5476 memset(&info, 0, sizeof(info));
5477 pthread_mutex_init(&info.mutex, NULL);
5478 pthread_mutex_lock(&info.mutex);
5479 pthread_cond_init(&info.cond, NULL);
5480 info.env = new_env;
5481 if (nptl_flags & CLONE_CHILD_SETTID)
5482 info.child_tidptr = child_tidptr;
5483 if (nptl_flags & CLONE_PARENT_SETTID)
5484 info.parent_tidptr = parent_tidptr;
5485
5486 ret = pthread_attr_init(&attr);
48e15fc2
NF
5487 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
5488 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
d865bab5
PB
5489 /* It is not safe to deliver signals until the child has finished
5490 initializing, so temporarily block all signals. */
5491 sigfillset(&sigmask);
5492 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
5493
5494 ret = pthread_create(&info.thread, &attr, clone_func, &info);
c2764719 5495 /* TODO: Free new CPU state if thread creation failed. */
d865bab5
PB
5496
5497 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
5498 pthread_attr_destroy(&attr);
5499 if (ret == 0) {
5500 /* Wait for the child to initialize. */
5501 pthread_cond_wait(&info.cond, &info.mutex);
5502 ret = info.tid;
5503 if (flags & CLONE_PARENT_SETTID)
5504 put_user_u32(ret, parent_tidptr);
5505 } else {
5506 ret = -1;
5507 }
5508 pthread_mutex_unlock(&info.mutex);
5509 pthread_cond_destroy(&info.cond);
5510 pthread_mutex_destroy(&info.mutex);
5511 pthread_mutex_unlock(&clone_lock);
1b6b029e
FB
5512 } else {
5513 /* if no CLONE_VM, we consider it is a fork */
93b4eff8
TB
5514 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0) {
5515 return -TARGET_EINVAL;
5516 }
7d92d34e
TB
5517
5518 if (block_signals()) {
5519 return -TARGET_ERESTARTSYS;
5520 }
5521
d865bab5 5522 fork_start();
1b6b029e 5523 ret = fork();
d865bab5 5524 if (ret == 0) {
2b1319c8 5525 /* Child Process. */
a59629fc 5526 rcu_after_fork();
d865bab5
PB
5527 cpu_clone_regs(env, newsp);
5528 fork_end(1);
2b1319c8
AJ
5529 /* There is a race condition here. The parent process could
5530 theoretically read the TID in the child process before the child
5531 tid is set. This would require using either ptrace
5532 (not implemented) or having *_tidptr to point at a shared memory
5533 mapping. We can't repeat the spinlock hack used above because
5534 the child process gets its own copy of the lock. */
d865bab5
PB
5535 if (flags & CLONE_CHILD_SETTID)
5536 put_user_u32(gettid(), child_tidptr);
5537 if (flags & CLONE_PARENT_SETTID)
5538 put_user_u32(gettid(), parent_tidptr);
0429a971 5539 ts = (TaskState *)cpu->opaque;
d865bab5
PB
5540 if (flags & CLONE_SETTLS)
5541 cpu_set_tls (env, newtls);
c2764719
PB
5542 if (flags & CLONE_CHILD_CLEARTID)
5543 ts->child_tidptr = child_tidptr;
d865bab5
PB
5544 } else {
5545 fork_end(0);
5546 }
1b6b029e
FB
5547 }
5548 return ret;
5549}
5550
5f106811
APR
5551/* warning : doesn't handle linux specific flags... */
5552static int target_to_host_fcntl_cmd(int cmd)
5553{
5554 switch(cmd) {
5555 case TARGET_F_DUPFD:
5556 case TARGET_F_GETFD:
5557 case TARGET_F_SETFD:
5558 case TARGET_F_GETFL:
5559 case TARGET_F_SETFL:
5560 return cmd;
5561 case TARGET_F_GETLK:
213d3e9e
PM
5562 return F_GETLK64;
5563 case TARGET_F_SETLK:
5564 return F_SETLK64;
5565 case TARGET_F_SETLKW:
5566 return F_SETLKW64;
5f106811
APR
5567 case TARGET_F_GETOWN:
5568 return F_GETOWN;
5569 case TARGET_F_SETOWN:
5570 return F_SETOWN;
5571 case TARGET_F_GETSIG:
5572 return F_GETSIG;
5573 case TARGET_F_SETSIG:
5574 return F_SETSIG;
5575#if TARGET_ABI_BITS == 32
5576 case TARGET_F_GETLK64:
5577 return F_GETLK64;
5578 case TARGET_F_SETLK64:
5579 return F_SETLK64;
5580 case TARGET_F_SETLKW64:
5581 return F_SETLKW64;
5582#endif
7e22e546
UH
5583 case TARGET_F_SETLEASE:
5584 return F_SETLEASE;
5585 case TARGET_F_GETLEASE:
5586 return F_GETLEASE;
fbd5de9b 5587#ifdef F_DUPFD_CLOEXEC
7e22e546
UH
5588 case TARGET_F_DUPFD_CLOEXEC:
5589 return F_DUPFD_CLOEXEC;
fbd5de9b 5590#endif
7e22e546
UH
5591 case TARGET_F_NOTIFY:
5592 return F_NOTIFY;
8d5d3004
AS
5593#ifdef F_GETOWN_EX
5594 case TARGET_F_GETOWN_EX:
5595 return F_GETOWN_EX;
5596#endif
5597#ifdef F_SETOWN_EX
5598 case TARGET_F_SETOWN_EX:
5599 return F_SETOWN_EX;
5600#endif
7e3b92ec
PM
5601 case TARGET_F_SETPIPE_SZ:
5602 return F_SETPIPE_SZ;
5603 case TARGET_F_GETPIPE_SZ:
5604 return F_GETPIPE_SZ;
5f106811
APR
5605 default:
5606 return -TARGET_EINVAL;
5607 }
5608 return -TARGET_EINVAL;
5609}
5610
2ba7f730
LV
5611#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
5612static const bitmask_transtbl flock_tbl[] = {
5613 TRANSTBL_CONVERT(F_RDLCK),
5614 TRANSTBL_CONVERT(F_WRLCK),
5615 TRANSTBL_CONVERT(F_UNLCK),
5616 TRANSTBL_CONVERT(F_EXLCK),
5617 TRANSTBL_CONVERT(F_SHLCK),
5618 { 0, 0, 0, 0 }
5619};
5620
213d3e9e
PM
5621static inline abi_long copy_from_user_flock(struct flock64 *fl,
5622 abi_ulong target_flock_addr)
7775e9ec 5623{
53a5960a 5624 struct target_flock *target_fl;
213d3e9e
PM
5625 short l_type;
5626
5627 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
5628 return -TARGET_EFAULT;
5629 }
5630
5631 __get_user(l_type, &target_fl->l_type);
5632 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
5633 __get_user(fl->l_whence, &target_fl->l_whence);
5634 __get_user(fl->l_start, &target_fl->l_start);
5635 __get_user(fl->l_len, &target_fl->l_len);
5636 __get_user(fl->l_pid, &target_fl->l_pid);
5637 unlock_user_struct(target_fl, target_flock_addr, 0);
5638 return 0;
5639}
5640
5641static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
5642 const struct flock64 *fl)
5643{
5644 struct target_flock *target_fl;
5645 short l_type;
5646
5647 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
5648 return -TARGET_EFAULT;
5649 }
5650
5651 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
5652 __put_user(l_type, &target_fl->l_type);
5653 __put_user(fl->l_whence, &target_fl->l_whence);
5654 __put_user(fl->l_start, &target_fl->l_start);
5655 __put_user(fl->l_len, &target_fl->l_len);
5656 __put_user(fl->l_pid, &target_fl->l_pid);
5657 unlock_user_struct(target_fl, target_flock_addr, 1);
5658 return 0;
5659}
5660
5661typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
5662typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
5663
5664#if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
5665static inline abi_long copy_from_user_eabi_flock64(struct flock64 *fl,
5666 abi_ulong target_flock_addr)
5667{
5668 struct target_eabi_flock64 *target_fl;
5669 short l_type;
5670
5671 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
5672 return -TARGET_EFAULT;
5673 }
5674
5675 __get_user(l_type, &target_fl->l_type);
5676 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
5677 __get_user(fl->l_whence, &target_fl->l_whence);
5678 __get_user(fl->l_start, &target_fl->l_start);
5679 __get_user(fl->l_len, &target_fl->l_len);
5680 __get_user(fl->l_pid, &target_fl->l_pid);
5681 unlock_user_struct(target_fl, target_flock_addr, 0);
5682 return 0;
5683}
5684
5685static inline abi_long copy_to_user_eabi_flock64(abi_ulong target_flock_addr,
5686 const struct flock64 *fl)
5687{
5688 struct target_eabi_flock64 *target_fl;
5689 short l_type;
5690
5691 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
5692 return -TARGET_EFAULT;
5693 }
5694
5695 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
5696 __put_user(l_type, &target_fl->l_type);
5697 __put_user(fl->l_whence, &target_fl->l_whence);
5698 __put_user(fl->l_start, &target_fl->l_start);
5699 __put_user(fl->l_len, &target_fl->l_len);
5700 __put_user(fl->l_pid, &target_fl->l_pid);
5701 unlock_user_struct(target_fl, target_flock_addr, 1);
5702 return 0;
5703}
5704#endif
5705
5706static inline abi_long copy_from_user_flock64(struct flock64 *fl,
5707 abi_ulong target_flock_addr)
5708{
5709 struct target_flock64 *target_fl;
5710 short l_type;
5711
5712 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
5713 return -TARGET_EFAULT;
5714 }
5715
5716 __get_user(l_type, &target_fl->l_type);
5717 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
5718 __get_user(fl->l_whence, &target_fl->l_whence);
5719 __get_user(fl->l_start, &target_fl->l_start);
5720 __get_user(fl->l_len, &target_fl->l_len);
5721 __get_user(fl->l_pid, &target_fl->l_pid);
5722 unlock_user_struct(target_fl, target_flock_addr, 0);
5723 return 0;
5724}
5725
5726static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
5727 const struct flock64 *fl)
5728{
5729 struct target_flock64 *target_fl;
5730 short l_type;
5731
5732 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
5733 return -TARGET_EFAULT;
5734 }
5735
5736 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
5737 __put_user(l_type, &target_fl->l_type);
5738 __put_user(fl->l_whence, &target_fl->l_whence);
5739 __put_user(fl->l_start, &target_fl->l_start);
5740 __put_user(fl->l_len, &target_fl->l_len);
5741 __put_user(fl->l_pid, &target_fl->l_pid);
5742 unlock_user_struct(target_fl, target_flock_addr, 1);
5743 return 0;
5744}
5745
5746static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
5747{
43f238d7 5748 struct flock64 fl64;
8d5d3004
AS
5749#ifdef F_GETOWN_EX
5750 struct f_owner_ex fox;
5751 struct target_f_owner_ex *target_fox;
5752#endif
992f48a0 5753 abi_long ret;
5f106811
APR
5754 int host_cmd = target_to_host_fcntl_cmd(cmd);
5755
5756 if (host_cmd == -TARGET_EINVAL)
5757 return host_cmd;
53a5960a 5758
7775e9ec
FB
5759 switch(cmd) {
5760 case TARGET_F_GETLK:
213d3e9e
PM
5761 ret = copy_from_user_flock(&fl64, arg);
5762 if (ret) {
5763 return ret;
5764 }
435da5e7 5765 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec 5766 if (ret == 0) {
213d3e9e 5767 ret = copy_to_user_flock(arg, &fl64);
7775e9ec
FB
5768 }
5769 break;
3b46e624 5770
7775e9ec
FB
5771 case TARGET_F_SETLK:
5772 case TARGET_F_SETLKW:
213d3e9e
PM
5773 ret = copy_from_user_flock(&fl64, arg);
5774 if (ret) {
5775 return ret;
5776 }
435da5e7 5777 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec 5778 break;
3b46e624 5779
7775e9ec 5780 case TARGET_F_GETLK64:
213d3e9e
PM
5781 ret = copy_from_user_flock64(&fl64, arg);
5782 if (ret) {
5783 return ret;
5784 }
435da5e7 5785 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
43f238d7 5786 if (ret == 0) {
213d3e9e 5787 ret = copy_to_user_flock64(arg, &fl64);
43f238d7 5788 }
9ee1fa2c 5789 break;
7775e9ec
FB
5790 case TARGET_F_SETLK64:
5791 case TARGET_F_SETLKW64:
213d3e9e
PM
5792 ret = copy_from_user_flock64(&fl64, arg);
5793 if (ret) {
5794 return ret;
5795 }
435da5e7 5796 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec
FB
5797 break;
5798
5f106811 5799 case TARGET_F_GETFL:
435da5e7 5800 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
9ee1fa2c
FB
5801 if (ret >= 0) {
5802 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
5803 }
ffa65c3b
FB
5804 break;
5805
5f106811 5806 case TARGET_F_SETFL:
435da5e7
PM
5807 ret = get_errno(safe_fcntl(fd, host_cmd,
5808 target_to_host_bitmask(arg,
5809 fcntl_flags_tbl)));
5f106811
APR
5810 break;
5811
8d5d3004
AS
5812#ifdef F_GETOWN_EX
5813 case TARGET_F_GETOWN_EX:
435da5e7 5814 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
8d5d3004
AS
5815 if (ret >= 0) {
5816 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
5817 return -TARGET_EFAULT;
5818 target_fox->type = tswap32(fox.type);
5819 target_fox->pid = tswap32(fox.pid);
5820 unlock_user_struct(target_fox, arg, 1);
5821 }
5822 break;
5823#endif
5824
5825#ifdef F_SETOWN_EX
5826 case TARGET_F_SETOWN_EX:
5827 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
5828 return -TARGET_EFAULT;
5829 fox.type = tswap32(target_fox->type);
5830 fox.pid = tswap32(target_fox->pid);
5831 unlock_user_struct(target_fox, arg, 0);
435da5e7 5832 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
8d5d3004
AS
5833 break;
5834#endif
5835
5f106811
APR
5836 case TARGET_F_SETOWN:
5837 case TARGET_F_GETOWN:
5838 case TARGET_F_SETSIG:
5839 case TARGET_F_GETSIG:
7e22e546
UH
5840 case TARGET_F_SETLEASE:
5841 case TARGET_F_GETLEASE:
7e3b92ec
PM
5842 case TARGET_F_SETPIPE_SZ:
5843 case TARGET_F_GETPIPE_SZ:
435da5e7 5844 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
ffa65c3b
FB
5845 break;
5846
7775e9ec 5847 default:
435da5e7 5848 ret = get_errno(safe_fcntl(fd, cmd, arg));
7775e9ec
FB
5849 break;
5850 }
5851 return ret;
5852}
5853
67867308 5854#ifdef USE_UID16
7775e9ec 5855
67867308
FB
5856static inline int high2lowuid(int uid)
5857{
5858 if (uid > 65535)
5859 return 65534;
5860 else
5861 return uid;
5862}
5863
5864static inline int high2lowgid(int gid)
5865{
5866 if (gid > 65535)
5867 return 65534;
5868 else
5869 return gid;
5870}
5871
5872static inline int low2highuid(int uid)
5873{
5874 if ((int16_t)uid == -1)
5875 return -1;
5876 else
5877 return uid;
5878}
5879
5880static inline int low2highgid(int gid)
5881{
5882 if ((int16_t)gid == -1)
5883 return -1;
5884 else
5885 return gid;
5886}
0c866a7e
RV
5887static inline int tswapid(int id)
5888{
5889 return tswap16(id);
5890}
76ca310a
PM
5891
5892#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
5893
0c866a7e
RV
5894#else /* !USE_UID16 */
5895static inline int high2lowuid(int uid)
5896{
5897 return uid;
5898}
5899static inline int high2lowgid(int gid)
5900{
5901 return gid;
5902}
5903static inline int low2highuid(int uid)
5904{
5905 return uid;
5906}
5907static inline int low2highgid(int gid)
5908{
5909 return gid;
5910}
5911static inline int tswapid(int id)
5912{
5913 return tswap32(id);
5914}
76ca310a
PM
5915
5916#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
5917
67867308 5918#endif /* USE_UID16 */
1b6b029e 5919
fd6f7798
PM
5920/* We must do direct syscalls for setting UID/GID, because we want to
5921 * implement the Linux system call semantics of "change only for this thread",
5922 * not the libc/POSIX semantics of "change for all threads in process".
5923 * (See http://ewontfix.com/17/ for more details.)
5924 * We use the 32-bit version of the syscalls if present; if it is not
5925 * then either the host architecture supports 32-bit UIDs natively with
5926 * the standard syscall, or the 16-bit UID is the best we can do.
5927 */
5928#ifdef __NR_setuid32
5929#define __NR_sys_setuid __NR_setuid32
5930#else
5931#define __NR_sys_setuid __NR_setuid
5932#endif
5933#ifdef __NR_setgid32
5934#define __NR_sys_setgid __NR_setgid32
5935#else
5936#define __NR_sys_setgid __NR_setgid
5937#endif
5938#ifdef __NR_setresuid32
5939#define __NR_sys_setresuid __NR_setresuid32
5940#else
5941#define __NR_sys_setresuid __NR_setresuid
5942#endif
5943#ifdef __NR_setresgid32
5944#define __NR_sys_setresgid __NR_setresgid32
5945#else
5946#define __NR_sys_setresgid __NR_setresgid
5947#endif
5948
5949_syscall1(int, sys_setuid, uid_t, uid)
5950_syscall1(int, sys_setgid, gid_t, gid)
5951_syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
5952_syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
5953
31e31b8a
FB
5954void syscall_init(void)
5955{
2ab83ea7
FB
5956 IOCTLEntry *ie;
5957 const argtype *arg_type;
5958 int size;
b92c47c1 5959 int i;
2ab83ea7 5960
8be656b8
AG
5961 thunk_init(STRUCT_MAX);
5962
001faf32 5963#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
5fafdf24 5964#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
31e31b8a
FB
5965#include "syscall_types.h"
5966#undef STRUCT
5967#undef STRUCT_SPECIAL
2ab83ea7 5968
dd6e957a
PM
5969 /* Build target_to_host_errno_table[] table from
5970 * host_to_target_errno_table[]. */
5971 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
5972 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
5973 }
5974
2ab83ea7
FB
5975 /* we patch the ioctl size if necessary. We rely on the fact that
5976 no ioctl has all the bits at '1' in the size field */
5977 ie = ioctl_entries;
5978 while (ie->target_cmd != 0) {
5979 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
5980 TARGET_IOC_SIZEMASK) {
5981 arg_type = ie->arg_type;
5982 if (arg_type[0] != TYPE_PTR) {
5fafdf24 5983 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2ab83ea7
FB
5984 ie->target_cmd);
5985 exit(1);
5986 }
5987 arg_type++;
5988 size = thunk_type_size(arg_type, 0);
5fafdf24 5989 ie->target_cmd = (ie->target_cmd &
2ab83ea7
FB
5990 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
5991 (size << TARGET_IOC_SIZESHIFT);
5992 }
b92c47c1 5993
2ab83ea7 5994 /* automatic consistency check if same arch */
872ea0c0
AZ
5995#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
5996 (defined(__x86_64__) && defined(TARGET_X86_64))
5997 if (unlikely(ie->target_cmd != ie->host_cmd)) {
5998 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
5999 ie->name, ie->target_cmd, ie->host_cmd);
2ab83ea7
FB
6000 }
6001#endif
6002 ie++;
6003 }
31e31b8a 6004}
c573ff67 6005
992f48a0 6006#if TARGET_ABI_BITS == 32
ce4defa0
PB
6007static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
6008{
af325d36 6009#ifdef TARGET_WORDS_BIGENDIAN
ce4defa0
PB
6010 return ((uint64_t)word0 << 32) | word1;
6011#else
6012 return ((uint64_t)word1 << 32) | word0;
6013#endif
6014}
992f48a0 6015#else /* TARGET_ABI_BITS == 32 */
32407103
JM
6016static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
6017{
6018 return word0;
6019}
992f48a0 6020#endif /* TARGET_ABI_BITS != 32 */
ce4defa0
PB
6021
6022#ifdef TARGET_NR_truncate64
992f48a0
BS
6023static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
6024 abi_long arg2,
6025 abi_long arg3,
6026 abi_long arg4)
ce4defa0 6027{
48e515d4 6028 if (regpairs_aligned(cpu_env)) {
ce4defa0
PB
6029 arg2 = arg3;
6030 arg3 = arg4;
48e515d4 6031 }
ce4defa0
PB
6032 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
6033}
6034#endif
6035
6036#ifdef TARGET_NR_ftruncate64
992f48a0
BS
6037static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
6038 abi_long arg2,
6039 abi_long arg3,
6040 abi_long arg4)
ce4defa0 6041{
48e515d4 6042 if (regpairs_aligned(cpu_env)) {
ce4defa0
PB
6043 arg2 = arg3;
6044 arg3 = arg4;
48e515d4 6045 }
ce4defa0
PB
6046 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
6047}
6048#endif
6049
579a97f7
FB
6050static inline abi_long target_to_host_timespec(struct timespec *host_ts,
6051 abi_ulong target_addr)
53a5960a
PB
6052{
6053 struct target_timespec *target_ts;
6054
579a97f7
FB
6055 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
6056 return -TARGET_EFAULT;
c7e35da3
PM
6057 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
6058 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
53a5960a 6059 unlock_user_struct(target_ts, target_addr, 0);
b255bfa8 6060 return 0;
53a5960a
PB
6061}
6062
579a97f7
FB
6063static inline abi_long host_to_target_timespec(abi_ulong target_addr,
6064 struct timespec *host_ts)
53a5960a
PB
6065{
6066 struct target_timespec *target_ts;
6067
579a97f7
FB
6068 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
6069 return -TARGET_EFAULT;
c7e35da3
PM
6070 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
6071 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
53a5960a 6072 unlock_user_struct(target_ts, target_addr, 1);
b255bfa8 6073 return 0;
53a5960a
PB
6074}
6075
f4f1e10a
ECL
6076static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
6077 abi_ulong target_addr)
6078{
6079 struct target_itimerspec *target_itspec;
6080
6081 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
6082 return -TARGET_EFAULT;
6083 }
6084
6085 host_itspec->it_interval.tv_sec =
6086 tswapal(target_itspec->it_interval.tv_sec);
6087 host_itspec->it_interval.tv_nsec =
6088 tswapal(target_itspec->it_interval.tv_nsec);
6089 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
6090 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
6091
6092 unlock_user_struct(target_itspec, target_addr, 1);
6093 return 0;
6094}
6095
6096static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
6097 struct itimerspec *host_its)
6098{
6099 struct target_itimerspec *target_itspec;
6100
6101 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
6102 return -TARGET_EFAULT;
6103 }
6104
6105 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
6106 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
6107
6108 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
6109 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
6110
6111 unlock_user_struct(target_itspec, target_addr, 0);
6112 return 0;
6113}
6114
c065976f
PM
6115static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
6116 abi_ulong target_addr)
6117{
6118 struct target_sigevent *target_sevp;
6119
6120 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
6121 return -TARGET_EFAULT;
6122 }
6123
6124 /* This union is awkward on 64 bit systems because it has a 32 bit
6125 * integer and a pointer in it; we follow the conversion approach
6126 * used for handling sigval types in signal.c so the guest should get
6127 * the correct value back even if we did a 64 bit byteswap and it's
6128 * using the 32 bit integer.
6129 */
6130 host_sevp->sigev_value.sival_ptr =
6131 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
6132 host_sevp->sigev_signo =
6133 target_to_host_signal(tswap32(target_sevp->sigev_signo));
6134 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
6135 host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
6136
6137 unlock_user_struct(target_sevp, target_addr, 1);
6138 return 0;
6139}
6140
6f6a4032
TM
6141#if defined(TARGET_NR_mlockall)
6142static inline int target_to_host_mlockall_arg(int arg)
6143{
6144 int result = 0;
6145
6146 if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
6147 result |= MCL_CURRENT;
6148 }
6149 if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
6150 result |= MCL_FUTURE;
6151 }
6152 return result;
6153}
6154#endif
6155
6a24a778
AZ
6156static inline abi_long host_to_target_stat64(void *cpu_env,
6157 abi_ulong target_addr,
6158 struct stat *host_st)
6159{
09701199 6160#if defined(TARGET_ARM) && defined(TARGET_ABI32)
6a24a778
AZ
6161 if (((CPUARMState *)cpu_env)->eabi) {
6162 struct target_eabi_stat64 *target_st;
6163
6164 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
6165 return -TARGET_EFAULT;
6166 memset(target_st, 0, sizeof(struct target_eabi_stat64));
6167 __put_user(host_st->st_dev, &target_st->st_dev);
6168 __put_user(host_st->st_ino, &target_st->st_ino);
6169#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6170 __put_user(host_st->st_ino, &target_st->__st_ino);
6171#endif
6172 __put_user(host_st->st_mode, &target_st->st_mode);
6173 __put_user(host_st->st_nlink, &target_st->st_nlink);
6174 __put_user(host_st->st_uid, &target_st->st_uid);
6175 __put_user(host_st->st_gid, &target_st->st_gid);
6176 __put_user(host_st->st_rdev, &target_st->st_rdev);
6177 __put_user(host_st->st_size, &target_st->st_size);
6178 __put_user(host_st->st_blksize, &target_st->st_blksize);
6179 __put_user(host_st->st_blocks, &target_st->st_blocks);
6180 __put_user(host_st->st_atime, &target_st->target_st_atime);
6181 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
6182 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
6183 unlock_user_struct(target_st, target_addr, 1);
6184 } else
6185#endif
6186 {
20d155bc 6187#if defined(TARGET_HAS_STRUCT_STAT64)
6a24a778 6188 struct target_stat64 *target_st;
20d155bc
SW
6189#else
6190 struct target_stat *target_st;
9d33b76b 6191#endif
6a24a778
AZ
6192
6193 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
6194 return -TARGET_EFAULT;
9d33b76b 6195 memset(target_st, 0, sizeof(*target_st));
6a24a778
AZ
6196 __put_user(host_st->st_dev, &target_st->st_dev);
6197 __put_user(host_st->st_ino, &target_st->st_ino);
6198#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6199 __put_user(host_st->st_ino, &target_st->__st_ino);
6200#endif
6201 __put_user(host_st->st_mode, &target_st->st_mode);
6202 __put_user(host_st->st_nlink, &target_st->st_nlink);
6203 __put_user(host_st->st_uid, &target_st->st_uid);
6204 __put_user(host_st->st_gid, &target_st->st_gid);
6205 __put_user(host_st->st_rdev, &target_st->st_rdev);
6206 /* XXX: better use of kernel struct */
6207 __put_user(host_st->st_size, &target_st->st_size);
6208 __put_user(host_st->st_blksize, &target_st->st_blksize);
6209 __put_user(host_st->st_blocks, &target_st->st_blocks);
6210 __put_user(host_st->st_atime, &target_st->target_st_atime);
6211 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
6212 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
6213 unlock_user_struct(target_st, target_addr, 1);
6214 }
6215
6216 return 0;
6217}
6a24a778 6218
bd0c5661
PB
6219/* ??? Using host futex calls even when target atomic operations
6220 are not really atomic probably breaks things. However implementing
6221 futexes locally would make futexes shared between multiple processes
6222 tricky. However they're probably useless because guest atomic
6223 operations won't work either. */
8fcd3692
BS
6224static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
6225 target_ulong uaddr2, int val3)
bd0c5661
PB
6226{
6227 struct timespec ts, *pts;
a16aae0c 6228 int base_op;
bd0c5661
PB
6229
6230 /* ??? We assume FUTEX_* constants are the same on both host
6231 and target. */
a29ccd63 6232#ifdef FUTEX_CMD_MASK
a16aae0c 6233 base_op = op & FUTEX_CMD_MASK;
a29ccd63 6234#else
a16aae0c 6235 base_op = op;
a29ccd63 6236#endif
a16aae0c 6237 switch (base_op) {
bd0c5661 6238 case FUTEX_WAIT:
cce246e0 6239 case FUTEX_WAIT_BITSET:
bd0c5661
PB
6240 if (timeout) {
6241 pts = &ts;
6242 target_to_host_timespec(pts, timeout);
6243 } else {
6244 pts = NULL;
6245 }
d509eeb1 6246 return get_errno(safe_futex(g2h(uaddr), op, tswap32(val),
cce246e0 6247 pts, NULL, val3));
bd0c5661 6248 case FUTEX_WAKE:
d509eeb1 6249 return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
bd0c5661 6250 case FUTEX_FD:
d509eeb1 6251 return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
bd0c5661 6252 case FUTEX_REQUEUE:
bd0c5661 6253 case FUTEX_CMP_REQUEUE:
a16aae0c
NF
6254 case FUTEX_WAKE_OP:
6255 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
6256 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
6257 But the prototype takes a `struct timespec *'; insert casts
6258 to satisfy the compiler. We do not need to tswap TIMEOUT
6259 since it's not compared to guest memory. */
6260 pts = (struct timespec *)(uintptr_t) timeout;
d509eeb1
PM
6261 return get_errno(safe_futex(g2h(uaddr), op, val, pts,
6262 g2h(uaddr2),
6263 (base_op == FUTEX_CMP_REQUEUE
6264 ? tswap32(val3)
6265 : val3)));
bd0c5661
PB
6266 default:
6267 return -TARGET_ENOSYS;
6268 }
6269}
0f0426f3
LV
6270#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6271static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
6272 abi_long handle, abi_long mount_id,
6273 abi_long flags)
6274{
6275 struct file_handle *target_fh;
6276 struct file_handle *fh;
6277 int mid = 0;
6278 abi_long ret;
6279 char *name;
6280 unsigned int size, total_size;
6281
6282 if (get_user_s32(size, handle)) {
6283 return -TARGET_EFAULT;
6284 }
6285
6286 name = lock_user_string(pathname);
6287 if (!name) {
6288 return -TARGET_EFAULT;
6289 }
6290
6291 total_size = sizeof(struct file_handle) + size;
6292 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
6293 if (!target_fh) {
6294 unlock_user(name, pathname, 0);
6295 return -TARGET_EFAULT;
6296 }
6297
6298 fh = g_malloc0(total_size);
6299 fh->handle_bytes = size;
6300
6301 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
6302 unlock_user(name, pathname, 0);
6303
6304 /* man name_to_handle_at(2):
6305 * Other than the use of the handle_bytes field, the caller should treat
6306 * the file_handle structure as an opaque data type
6307 */
6308
6309 memcpy(target_fh, fh, total_size);
6310 target_fh->handle_bytes = tswap32(fh->handle_bytes);
6311 target_fh->handle_type = tswap32(fh->handle_type);
6312 g_free(fh);
6313 unlock_user(target_fh, handle, total_size);
6314
6315 if (put_user_s32(mid, mount_id)) {
6316 return -TARGET_EFAULT;
6317 }
6318
6319 return ret;
6320
6321}
6322#endif
6323
6324#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6325static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
6326 abi_long flags)
6327{
6328 struct file_handle *target_fh;
6329 struct file_handle *fh;
6330 unsigned int size, total_size;
6331 abi_long ret;
6332
6333 if (get_user_s32(size, handle)) {
6334 return -TARGET_EFAULT;
6335 }
6336
6337 total_size = sizeof(struct file_handle) + size;
6338 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
6339 if (!target_fh) {
6340 return -TARGET_EFAULT;
6341 }
6342
e9d49d51 6343 fh = g_memdup(target_fh, total_size);
0f0426f3
LV
6344 fh->handle_bytes = size;
6345 fh->handle_type = tswap32(target_fh->handle_type);
6346
6347 ret = get_errno(open_by_handle_at(mount_fd, fh,
6348 target_to_host_bitmask(flags, fcntl_flags_tbl)));
6349
6350 g_free(fh);
6351
6352 unlock_user(target_fh, handle, total_size);
6353
6354 return ret;
6355}
6356#endif
bd0c5661 6357
e36800c9
LV
6358#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
6359
6360/* signalfd siginfo conversion */
6361
6362static void
6363host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
6364 const struct signalfd_siginfo *info)
6365{
6366 int sig = host_to_target_signal(info->ssi_signo);
6367
6368 /* linux/signalfd.h defines a ssi_addr_lsb
6369 * not defined in sys/signalfd.h but used by some kernels
6370 */
6371
6372#ifdef BUS_MCEERR_AO
6373 if (tinfo->ssi_signo == SIGBUS &&
6374 (tinfo->ssi_code == BUS_MCEERR_AR ||
6375 tinfo->ssi_code == BUS_MCEERR_AO)) {
6376 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
6377 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
6378 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
6379 }
6380#endif
6381
6382 tinfo->ssi_signo = tswap32(sig);
6383 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
6384 tinfo->ssi_code = tswap32(info->ssi_code);
6385 tinfo->ssi_pid = tswap32(info->ssi_pid);
6386 tinfo->ssi_uid = tswap32(info->ssi_uid);
6387 tinfo->ssi_fd = tswap32(info->ssi_fd);
6388 tinfo->ssi_tid = tswap32(info->ssi_tid);
6389 tinfo->ssi_band = tswap32(info->ssi_band);
6390 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
6391 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
6392 tinfo->ssi_status = tswap32(info->ssi_status);
6393 tinfo->ssi_int = tswap32(info->ssi_int);
6394 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
6395 tinfo->ssi_utime = tswap64(info->ssi_utime);
6396 tinfo->ssi_stime = tswap64(info->ssi_stime);
6397 tinfo->ssi_addr = tswap64(info->ssi_addr);
6398}
6399
5d4d3665 6400static abi_long host_to_target_data_signalfd(void *buf, size_t len)
e36800c9
LV
6401{
6402 int i;
6403
6404 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
6405 host_to_target_signalfd_siginfo(buf + i, buf + i);
6406 }
6407
6408 return len;
6409}
6410
6411static TargetFdTrans target_signalfd_trans = {
5d4d3665 6412 .host_to_target_data = host_to_target_data_signalfd,
e36800c9
LV
6413};
6414
6415static abi_long do_signalfd4(int fd, abi_long mask, int flags)
6416{
6417 int host_flags;
6418 target_sigset_t *target_mask;
6419 sigset_t host_mask;
6420 abi_long ret;
6421
6422 if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
6423 return -TARGET_EINVAL;
6424 }
6425 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
6426 return -TARGET_EFAULT;
6427 }
6428
6429 target_to_host_sigset(&host_mask, target_mask);
6430
6431 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
6432
6433 ret = get_errno(signalfd(fd, &host_mask, host_flags));
6434 if (ret >= 0) {
6435 fd_trans_register(ret, &target_signalfd_trans);
6436 }
6437
6438 unlock_user_struct(target_mask, mask, 0);
6439
6440 return ret;
6441}
6442#endif
6443
1d9d8b55
PB
6444/* Map host to target signal numbers for the wait family of syscalls.
6445 Assume all other status bits are the same. */
a05c6409 6446int host_to_target_waitstatus(int status)
1d9d8b55
PB
6447{
6448 if (WIFSIGNALED(status)) {
6449 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
6450 }
6451 if (WIFSTOPPED(status)) {
6452 return (host_to_target_signal(WSTOPSIG(status)) << 8)
6453 | (status & 0xff);
6454 }
6455 return status;
6456}
6457
76b94245
WVS
6458static int open_self_cmdline(void *cpu_env, int fd)
6459{
6460 int fd_orig = -1;
6461 bool word_skipped = false;
6462
6463 fd_orig = open("/proc/self/cmdline", O_RDONLY);
6464 if (fd_orig < 0) {
6465 return fd_orig;
6466 }
6467
6468 while (true) {
6469 ssize_t nb_read;
6470 char buf[128];
6471 char *cp_buf = buf;
6472
6473 nb_read = read(fd_orig, buf, sizeof(buf));
6474 if (nb_read < 0) {
a3ca7bb2 6475 int e = errno;
76b94245 6476 fd_orig = close(fd_orig);
a3ca7bb2 6477 errno = e;
76b94245
WVS
6478 return -1;
6479 } else if (nb_read == 0) {
6480 break;
6481 }
6482
6483 if (!word_skipped) {
6484 /* Skip the first string, which is the path to qemu-*-static
6485 instead of the actual command. */
6486 cp_buf = memchr(buf, 0, sizeof(buf));
6487 if (cp_buf) {
6488 /* Null byte found, skip one string */
6489 cp_buf++;
6490 nb_read -= cp_buf - buf;
6491 word_skipped = true;
6492 }
6493 }
6494
6495 if (word_skipped) {
6496 if (write(fd, cp_buf, nb_read) != nb_read) {
a3ca7bb2 6497 int e = errno;
680dfde9 6498 close(fd_orig);
a3ca7bb2 6499 errno = e;
76b94245
WVS
6500 return -1;
6501 }
6502 }
6503 }
6504
6505 return close(fd_orig);
6506}
6507
36c08d49
AG
6508static int open_self_maps(void *cpu_env, int fd)
6509{
0429a971
AF
6510 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
6511 TaskState *ts = cpu->opaque;
1a49ef2a
AG
6512 FILE *fp;
6513 char *line = NULL;
6514 size_t len = 0;
6515 ssize_t read;
6516
6517 fp = fopen("/proc/self/maps", "r");
6518 if (fp == NULL) {
a3ca7bb2 6519 return -1;
1a49ef2a 6520 }
36c08d49 6521
1a49ef2a
AG
6522 while ((read = getline(&line, &len, fp)) != -1) {
6523 int fields, dev_maj, dev_min, inode;
6524 uint64_t min, max, offset;
6525 char flag_r, flag_w, flag_x, flag_p;
6526 char path[512] = "";
6527 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
6528 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
6529 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
6530
6531 if ((fields < 10) || (fields > 11)) {
6532 continue;
6533 }
d67f4aaa
MI
6534 if (h2g_valid(min)) {
6535 int flags = page_get_flags(h2g(min));
6536 max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX);
6537 if (page_check_range(h2g(min), max - min, flags) == -1) {
6538 continue;
6539 }
6540 if (h2g(min) == ts->info->stack_limit) {
6541 pstrcpy(path, sizeof(path), " [stack]");
6542 }
1a49ef2a 6543 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
e24fed4e 6544 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
d67f4aaa 6545 h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
1a49ef2a 6546 flag_x, flag_p, offset, dev_maj, dev_min, inode,
e24fed4e 6547 path[0] ? " " : "", path);
1a49ef2a
AG
6548 }
6549 }
6550
6551 free(line);
6552 fclose(fp);
6553
36c08d49
AG
6554 return 0;
6555}
6556
480b8e7d
AG
6557static int open_self_stat(void *cpu_env, int fd)
6558{
0429a971
AF
6559 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
6560 TaskState *ts = cpu->opaque;
480b8e7d
AG
6561 abi_ulong start_stack = ts->info->start_stack;
6562 int i;
6563
6564 for (i = 0; i < 44; i++) {
6565 char buf[128];
6566 int len;
6567 uint64_t val = 0;
6568
e0e65bee
FE
6569 if (i == 0) {
6570 /* pid */
6571 val = getpid();
6572 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
6573 } else if (i == 1) {
6574 /* app name */
6575 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
6576 } else if (i == 27) {
6577 /* stack bottom */
6578 val = start_stack;
6579 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
6580 } else {
6581 /* for the rest, there is MasterCard */
6582 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
480b8e7d 6583 }
e0e65bee 6584
480b8e7d
AG
6585 len = strlen(buf);
6586 if (write(fd, buf, len) != len) {
6587 return -1;
6588 }
6589 }
6590
6591 return 0;
6592}
6593
257450ee
AG
6594static int open_self_auxv(void *cpu_env, int fd)
6595{
0429a971
AF
6596 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
6597 TaskState *ts = cpu->opaque;
257450ee
AG
6598 abi_ulong auxv = ts->info->saved_auxv;
6599 abi_ulong len = ts->info->auxv_len;
6600 char *ptr;
6601
6602 /*
6603 * Auxiliary vector is stored in target process stack.
6604 * read in whole auxv vector and copy it to file
6605 */
6606 ptr = lock_user(VERIFY_READ, auxv, len, 0);
6607 if (ptr != NULL) {
6608 while (len > 0) {
6609 ssize_t r;
6610 r = write(fd, ptr, len);
6611 if (r <= 0) {
6612 break;
6613 }
6614 len -= r;
6615 ptr += r;
6616 }
6617 lseek(fd, 0, SEEK_SET);
6618 unlock_user(ptr, auxv, len);
6619 }
6620
6621 return 0;
6622}
6623
463d8e73
AS
6624static int is_proc_myself(const char *filename, const char *entry)
6625{
6626 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
6627 filename += strlen("/proc/");
6628 if (!strncmp(filename, "self/", strlen("self/"))) {
6629 filename += strlen("self/");
6630 } else if (*filename >= '1' && *filename <= '9') {
6631 char myself[80];
6632 snprintf(myself, sizeof(myself), "%d/", getpid());
6633 if (!strncmp(filename, myself, strlen(myself))) {
6634 filename += strlen(myself);
6635 } else {
6636 return 0;
6637 }
6638 } else {
6639 return 0;
6640 }
6641 if (!strcmp(filename, entry)) {
6642 return 1;
6643 }
6644 }
6645 return 0;
6646}
6647
de6b9933
LV
6648#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
6649static int is_proc(const char *filename, const char *entry)
6650{
6651 return strcmp(filename, entry) == 0;
6652}
6653
6654static int open_net_route(void *cpu_env, int fd)
6655{
6656 FILE *fp;
6657 char *line = NULL;
6658 size_t len = 0;
6659 ssize_t read;
6660
6661 fp = fopen("/proc/net/route", "r");
6662 if (fp == NULL) {
a3ca7bb2 6663 return -1;
de6b9933
LV
6664 }
6665
6666 /* read header */
6667
6668 read = getline(&line, &len, fp);
6669 dprintf(fd, "%s", line);
6670
6671 /* read routes */
6672
6673 while ((read = getline(&line, &len, fp)) != -1) {
6674 char iface[16];
6675 uint32_t dest, gw, mask;
6676 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
6677 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
6678 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
6679 &mask, &mtu, &window, &irtt);
6680 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
6681 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
6682 metric, tswap32(mask), mtu, window, irtt);
6683 }
6684
6685 free(line);
6686 fclose(fp);
6687
6688 return 0;
6689}
6690#endif
6691
0b2effd7 6692static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
3be14d05
AG
6693{
6694 struct fake_open {
6695 const char *filename;
6696 int (*fill)(void *cpu_env, int fd);
de6b9933 6697 int (*cmp)(const char *s1, const char *s2);
3be14d05
AG
6698 };
6699 const struct fake_open *fake_open;
6700 static const struct fake_open fakes[] = {
de6b9933
LV
6701 { "maps", open_self_maps, is_proc_myself },
6702 { "stat", open_self_stat, is_proc_myself },
6703 { "auxv", open_self_auxv, is_proc_myself },
76b94245 6704 { "cmdline", open_self_cmdline, is_proc_myself },
de6b9933
LV
6705#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
6706 { "/proc/net/route", open_net_route, is_proc },
6707#endif
6708 { NULL, NULL, NULL }
3be14d05
AG
6709 };
6710
aa07f5ec
MO
6711 if (is_proc_myself(pathname, "exe")) {
6712 int execfd = qemu_getauxval(AT_EXECFD);
c10a0738 6713 return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
aa07f5ec
MO
6714 }
6715
3be14d05 6716 for (fake_open = fakes; fake_open->filename; fake_open++) {
de6b9933 6717 if (fake_open->cmp(pathname, fake_open->filename)) {
3be14d05
AG
6718 break;
6719 }
6720 }
6721
6722 if (fake_open->filename) {
6723 const char *tmpdir;
6724 char filename[PATH_MAX];
6725 int fd, r;
6726
6727 /* create temporary file to map stat to */
6728 tmpdir = getenv("TMPDIR");
6729 if (!tmpdir)
6730 tmpdir = "/tmp";
6731 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
6732 fd = mkstemp(filename);
6733 if (fd < 0) {
6734 return fd;
6735 }
6736 unlink(filename);
6737
6738 if ((r = fake_open->fill(cpu_env, fd))) {
a3ca7bb2 6739 int e = errno;
3be14d05 6740 close(fd);
a3ca7bb2 6741 errno = e;
3be14d05
AG
6742 return r;
6743 }
6744 lseek(fd, 0, SEEK_SET);
6745
6746 return fd;
6747 }
6748
c10a0738 6749 return safe_openat(dirfd, path(pathname), flags, mode);
3be14d05
AG
6750}
6751
aecc8861
AG
6752#define TIMER_MAGIC 0x0caf0000
6753#define TIMER_MAGIC_MASK 0xffff0000
6754
6755/* Convert QEMU provided timer ID back to internal 16bit index format */
6756static target_timer_t get_timer_id(abi_long arg)
6757{
6758 target_timer_t timerid = arg;
6759
6760 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
6761 return -TARGET_EINVAL;
6762 }
6763
6764 timerid &= 0xffff;
6765
6766 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
6767 return -TARGET_EINVAL;
6768 }
6769
6770 return timerid;
6771}
6772
0da46a6e
TS
6773/* do_syscall() should always have a single exit point at the end so
6774 that actions, such as logging of syscall results, can be performed.
6775 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
992f48a0
BS
6776abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
6777 abi_long arg2, abi_long arg3, abi_long arg4,
5945cfcb
PM
6778 abi_long arg5, abi_long arg6, abi_long arg7,
6779 abi_long arg8)
31e31b8a 6780{
182735ef 6781 CPUState *cpu = ENV_GET_CPU(cpu_env);
992f48a0 6782 abi_long ret;
31e31b8a 6783 struct stat st;
56c8f68f 6784 struct statfs stfs;
53a5960a 6785 void *p;
3b46e624 6786
71a8f7fe
TB
6787#if defined(DEBUG_ERESTARTSYS)
6788 /* Debug-only code for exercising the syscall-restart code paths
6789 * in the per-architecture cpu main loops: restart every syscall
6790 * the guest makes once before letting it through.
6791 */
6792 {
6793 static int flag;
6794
6795 flag = !flag;
6796 if (flag) {
6797 return -TARGET_ERESTARTSYS;
6798 }
6799 }
6800#endif
6801
72f03900 6802#ifdef DEBUG
c573ff67 6803 gemu_log("syscall %d", num);
72f03900 6804#endif
9c15e700 6805 trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
b92c47c1
TS
6806 if(do_strace)
6807 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
6808
31e31b8a
FB
6809 switch(num) {
6810 case TARGET_NR_exit:
9b056fcc
AF
6811 /* In old applications this may be used to implement _exit(2).
6812 However in threaded applictions it is used for thread termination,
6813 and _exit_group is used for application termination.
6814 Do thread termination if we have more then one thread. */
a0995886
TB
6815
6816 if (block_signals()) {
6817 ret = -TARGET_ERESTARTSYS;
6818 break;
6819 }
6820
bdc44640 6821 if (CPU_NEXT(first_cpu)) {
9b056fcc 6822 TaskState *ts;
9b056fcc
AF
6823
6824 cpu_list_lock();
9b056fcc 6825 /* Remove the CPU from the list. */
bdc44640 6826 QTAILQ_REMOVE(&cpus, cpu, node);
9b056fcc 6827 cpu_list_unlock();
0429a971 6828 ts = cpu->opaque;
9b056fcc
AF
6829 if (ts->child_tidptr) {
6830 put_user_u32(0, ts->child_tidptr);
6831 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
6832 NULL, NULL, 0);
6833 }
a2247f8e 6834 thread_cpu = NULL;
0429a971 6835 object_unref(OBJECT(cpu));
9b056fcc 6836 g_free(ts);
70903763 6837 rcu_unregister_thread();
9b056fcc
AF
6838 pthread_exit(NULL);
6839 }
9788c9ca 6840#ifdef TARGET_GPROF
7d13299d
FB
6841 _mcleanup();
6842#endif
e9009676 6843 gdb_exit(cpu_env, arg1);
c2764719 6844 _exit(arg1);
31e31b8a
FB
6845 ret = 0; /* avoid warning */
6846 break;
6847 case TARGET_NR_read:
38d840e6
AJ
6848 if (arg3 == 0)
6849 ret = 0;
6850 else {
6851 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6852 goto efault;
50afd02b 6853 ret = get_errno(safe_read(arg1, p, arg3));
e36800c9 6854 if (ret >= 0 &&
5d4d3665
LV
6855 fd_trans_host_to_target_data(arg1)) {
6856 ret = fd_trans_host_to_target_data(arg1)(p, ret);
e36800c9 6857 }
38d840e6
AJ
6858 unlock_user(p, arg2, ret);
6859 }
31e31b8a
FB
6860 break;
6861 case TARGET_NR_write:
579a97f7
FB
6862 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6863 goto efault;
50afd02b 6864 ret = get_errno(safe_write(arg1, p, arg3));
53a5960a 6865 unlock_user(p, arg2, 0);
31e31b8a 6866 break;
704eff6c 6867#ifdef TARGET_NR_open
31e31b8a 6868 case TARGET_NR_open:
2f619698
FB
6869 if (!(p = lock_user_string(arg1)))
6870 goto efault;
0b2effd7
RV
6871 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
6872 target_to_host_bitmask(arg2, fcntl_flags_tbl),
6873 arg3));
e36800c9 6874 fd_trans_unregister(ret);
53a5960a 6875 unlock_user(p, arg1, 0);
31e31b8a 6876 break;
704eff6c 6877#endif
82424832 6878 case TARGET_NR_openat:
579a97f7
FB
6879 if (!(p = lock_user_string(arg2)))
6880 goto efault;
0b2effd7
RV
6881 ret = get_errno(do_openat(cpu_env, arg1, p,
6882 target_to_host_bitmask(arg3, fcntl_flags_tbl),
6883 arg4));
e36800c9 6884 fd_trans_unregister(ret);
579a97f7 6885 unlock_user(p, arg2, 0);
82424832 6886 break;
0f0426f3
LV
6887#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6888 case TARGET_NR_name_to_handle_at:
6889 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
6890 break;
6891#endif
6892#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6893 case TARGET_NR_open_by_handle_at:
6894 ret = do_open_by_handle_at(arg1, arg2, arg3);
e36800c9 6895 fd_trans_unregister(ret);
0f0426f3
LV
6896 break;
6897#endif
31e31b8a 6898 case TARGET_NR_close:
e36800c9 6899 fd_trans_unregister(arg1);
31e31b8a
FB
6900 ret = get_errno(close(arg1));
6901 break;
6902 case TARGET_NR_brk:
53a5960a 6903 ret = do_brk(arg1);
31e31b8a 6904 break;
704eff6c 6905#ifdef TARGET_NR_fork
31e31b8a 6906 case TARGET_NR_fork:
d865bab5 6907 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
31e31b8a 6908 break;
704eff6c 6909#endif
e5febef5 6910#ifdef TARGET_NR_waitpid
31e31b8a
FB
6911 case TARGET_NR_waitpid:
6912 {
53a5960a 6913 int status;
4af80a37 6914 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
5379557b 6915 if (!is_error(ret) && arg2 && ret
1d9d8b55 6916 && put_user_s32(host_to_target_waitstatus(status), arg2))
2f619698 6917 goto efault;
31e31b8a
FB
6918 }
6919 break;
e5febef5 6920#endif
f0cbb613
PB
6921#ifdef TARGET_NR_waitid
6922 case TARGET_NR_waitid:
6923 {
6924 siginfo_t info;
6925 info.si_pid = 0;
4af80a37 6926 ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
f0cbb613 6927 if (!is_error(ret) && arg3 && info.si_pid != 0) {
c227f099 6928 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
f0cbb613
PB
6929 goto efault;
6930 host_to_target_siginfo(p, &info);
c227f099 6931 unlock_user(p, arg3, sizeof(target_siginfo_t));
f0cbb613
PB
6932 }
6933 }
6934 break;
6935#endif
7a3148a9 6936#ifdef TARGET_NR_creat /* not on alpha */
31e31b8a 6937 case TARGET_NR_creat:
579a97f7
FB
6938 if (!(p = lock_user_string(arg1)))
6939 goto efault;
53a5960a 6940 ret = get_errno(creat(p, arg2));
e36800c9 6941 fd_trans_unregister(ret);
53a5960a 6942 unlock_user(p, arg1, 0);
31e31b8a 6943 break;
7a3148a9 6944#endif
704eff6c 6945#ifdef TARGET_NR_link
31e31b8a 6946 case TARGET_NR_link:
53a5960a
PB
6947 {
6948 void * p2;
6949 p = lock_user_string(arg1);
6950 p2 = lock_user_string(arg2);
579a97f7
FB
6951 if (!p || !p2)
6952 ret = -TARGET_EFAULT;
6953 else
6954 ret = get_errno(link(p, p2));
53a5960a
PB
6955 unlock_user(p2, arg2, 0);
6956 unlock_user(p, arg1, 0);
6957 }
31e31b8a 6958 break;
704eff6c 6959#endif
c0d472b1 6960#if defined(TARGET_NR_linkat)
64f0ce4c 6961 case TARGET_NR_linkat:
64f0ce4c
TS
6962 {
6963 void * p2 = NULL;
579a97f7
FB
6964 if (!arg2 || !arg4)
6965 goto efault;
64f0ce4c
TS
6966 p = lock_user_string(arg2);
6967 p2 = lock_user_string(arg4);
579a97f7 6968 if (!p || !p2)
0da46a6e 6969 ret = -TARGET_EFAULT;
64f0ce4c 6970 else
c0d472b1 6971 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
579a97f7
FB
6972 unlock_user(p, arg2, 0);
6973 unlock_user(p2, arg4, 0);
64f0ce4c
TS
6974 }
6975 break;
6976#endif
704eff6c 6977#ifdef TARGET_NR_unlink
31e31b8a 6978 case TARGET_NR_unlink:
579a97f7
FB
6979 if (!(p = lock_user_string(arg1)))
6980 goto efault;
53a5960a
PB
6981 ret = get_errno(unlink(p));
6982 unlock_user(p, arg1, 0);
31e31b8a 6983 break;
704eff6c 6984#endif
c0d472b1 6985#if defined(TARGET_NR_unlinkat)
8170f56b 6986 case TARGET_NR_unlinkat:
579a97f7
FB
6987 if (!(p = lock_user_string(arg2)))
6988 goto efault;
c0d472b1 6989 ret = get_errno(unlinkat(arg1, p, arg3));
579a97f7 6990 unlock_user(p, arg2, 0);
ed494d87 6991 break;
b7d35e65 6992#endif
31e31b8a 6993 case TARGET_NR_execve:
7854b056
FB
6994 {
6995 char **argp, **envp;
f7341ff4 6996 int argc, envc;
992f48a0
BS
6997 abi_ulong gp;
6998 abi_ulong guest_argp;
6999 abi_ulong guest_envp;
7000 abi_ulong addr;
7854b056 7001 char **q;
a6f79cc9 7002 int total_size = 0;
7854b056 7003
f7341ff4 7004 argc = 0;
53a5960a 7005 guest_argp = arg2;
da94d263 7006 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
03aa1976 7007 if (get_user_ual(addr, gp))
2f619698 7008 goto efault;
03aa1976 7009 if (!addr)
2f619698 7010 break;
7854b056 7011 argc++;
2f619698 7012 }
f7341ff4 7013 envc = 0;
53a5960a 7014 guest_envp = arg3;
da94d263 7015 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
03aa1976 7016 if (get_user_ual(addr, gp))
2f619698 7017 goto efault;
03aa1976 7018 if (!addr)
2f619698 7019 break;
7854b056 7020 envc++;
2f619698 7021 }
7854b056 7022
f7341ff4
FB
7023 argp = alloca((argc + 1) * sizeof(void *));
7024 envp = alloca((envc + 1) * sizeof(void *));
7854b056 7025
da94d263 7026 for (gp = guest_argp, q = argp; gp;
992f48a0 7027 gp += sizeof(abi_ulong), q++) {
2f619698
FB
7028 if (get_user_ual(addr, gp))
7029 goto execve_efault;
53a5960a
PB
7030 if (!addr)
7031 break;
2f619698
FB
7032 if (!(*q = lock_user_string(addr)))
7033 goto execve_efault;
a6f79cc9 7034 total_size += strlen(*q) + 1;
53a5960a 7035 }
f7341ff4
FB
7036 *q = NULL;
7037
da94d263 7038 for (gp = guest_envp, q = envp; gp;
992f48a0 7039 gp += sizeof(abi_ulong), q++) {
2f619698
FB
7040 if (get_user_ual(addr, gp))
7041 goto execve_efault;
53a5960a
PB
7042 if (!addr)
7043 break;
2f619698
FB
7044 if (!(*q = lock_user_string(addr)))
7045 goto execve_efault;
a6f79cc9 7046 total_size += strlen(*q) + 1;
53a5960a 7047 }
f7341ff4 7048 *q = NULL;
7854b056 7049
2f619698
FB
7050 if (!(p = lock_user_string(arg1)))
7051 goto execve_efault;
ffdcbe22
TB
7052 /* Although execve() is not an interruptible syscall it is
7053 * a special case where we must use the safe_syscall wrapper:
7054 * if we allow a signal to happen before we make the host
7055 * syscall then we will 'lose' it, because at the point of
7056 * execve the process leaves QEMU's control. So we use the
7057 * safe syscall wrapper to ensure that we either take the
7058 * signal as a guest signal, or else it does not happen
7059 * before the execve completes and makes it the other
7060 * program's problem.
7061 */
7062 ret = get_errno(safe_execve(p, argp, envp));
53a5960a
PB
7063 unlock_user(p, arg1, 0);
7064
2f619698
FB
7065 goto execve_end;
7066
7067 execve_efault:
7068 ret = -TARGET_EFAULT;
7069
7070 execve_end:
53a5960a 7071 for (gp = guest_argp, q = argp; *q;
992f48a0 7072 gp += sizeof(abi_ulong), q++) {
2f619698
FB
7073 if (get_user_ual(addr, gp)
7074 || !addr)
7075 break;
53a5960a
PB
7076 unlock_user(*q, addr, 0);
7077 }
7078 for (gp = guest_envp, q = envp; *q;
992f48a0 7079 gp += sizeof(abi_ulong), q++) {
2f619698
FB
7080 if (get_user_ual(addr, gp)
7081 || !addr)
7082 break;
53a5960a
PB
7083 unlock_user(*q, addr, 0);
7084 }
7854b056 7085 }
31e31b8a
FB
7086 break;
7087 case TARGET_NR_chdir:
579a97f7
FB
7088 if (!(p = lock_user_string(arg1)))
7089 goto efault;
53a5960a
PB
7090 ret = get_errno(chdir(p));
7091 unlock_user(p, arg1, 0);
31e31b8a 7092 break;
a315a145 7093#ifdef TARGET_NR_time
31e31b8a
FB
7094 case TARGET_NR_time:
7095 {
53a5960a
PB
7096 time_t host_time;
7097 ret = get_errno(time(&host_time));
2f619698
FB
7098 if (!is_error(ret)
7099 && arg1
7100 && put_user_sal(host_time, arg1))
7101 goto efault;
31e31b8a
FB
7102 }
7103 break;
a315a145 7104#endif
704eff6c 7105#ifdef TARGET_NR_mknod
31e31b8a 7106 case TARGET_NR_mknod:
579a97f7
FB
7107 if (!(p = lock_user_string(arg1)))
7108 goto efault;
53a5960a
PB
7109 ret = get_errno(mknod(p, arg2, arg3));
7110 unlock_user(p, arg1, 0);
31e31b8a 7111 break;
704eff6c 7112#endif
c0d472b1 7113#if defined(TARGET_NR_mknodat)
75ac37a0 7114 case TARGET_NR_mknodat:
579a97f7
FB
7115 if (!(p = lock_user_string(arg2)))
7116 goto efault;
c0d472b1 7117 ret = get_errno(mknodat(arg1, p, arg3, arg4));
579a97f7 7118 unlock_user(p, arg2, 0);
75ac37a0
TS
7119 break;
7120#endif
704eff6c 7121#ifdef TARGET_NR_chmod
31e31b8a 7122 case TARGET_NR_chmod:
579a97f7
FB
7123 if (!(p = lock_user_string(arg1)))
7124 goto efault;
53a5960a
PB
7125 ret = get_errno(chmod(p, arg2));
7126 unlock_user(p, arg1, 0);
31e31b8a 7127 break;
704eff6c 7128#endif
ebc05488 7129#ifdef TARGET_NR_break
31e31b8a
FB
7130 case TARGET_NR_break:
7131 goto unimplemented;
ebc05488
FB
7132#endif
7133#ifdef TARGET_NR_oldstat
31e31b8a
FB
7134 case TARGET_NR_oldstat:
7135 goto unimplemented;
ebc05488 7136#endif
31e31b8a
FB
7137 case TARGET_NR_lseek:
7138 ret = get_errno(lseek(arg1, arg2, arg3));
7139 break;
9231733a
RH
7140#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
7141 /* Alpha specific */
7a3148a9 7142 case TARGET_NR_getxpid:
9231733a
RH
7143 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
7144 ret = get_errno(getpid());
7145 break;
7a3148a9 7146#endif
9231733a
RH
7147#ifdef TARGET_NR_getpid
7148 case TARGET_NR_getpid:
31e31b8a
FB
7149 ret = get_errno(getpid());
7150 break;
9231733a 7151#endif
31e31b8a 7152 case TARGET_NR_mount:
356d771b
PB
7153 {
7154 /* need to look at the data field */
7155 void *p2, *p3;
7156
7157 if (arg1) {
7158 p = lock_user_string(arg1);
7159 if (!p) {
7160 goto efault;
7161 }
7162 } else {
7163 p = NULL;
7164 }
7165
7166 p2 = lock_user_string(arg2);
7167 if (!p2) {
7168 if (arg1) {
7169 unlock_user(p, arg1, 0);
7170 }
7171 goto efault;
7172 }
7173
7174 if (arg3) {
7175 p3 = lock_user_string(arg3);
7176 if (!p3) {
7177 if (arg1) {
579a97f7 7178 unlock_user(p, arg1, 0);
356d771b
PB
7179 }
7180 unlock_user(p2, arg2, 0);
7181 goto efault;
7182 }
7183 } else {
7184 p3 = NULL;
7185 }
7186
7187 /* FIXME - arg5 should be locked, but it isn't clear how to
7188 * do that since it's not guaranteed to be a NULL-terminated
7189 * string.
7190 */
7191 if (!arg5) {
7192 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
7193 } else {
7194 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
7195 }
7196 ret = get_errno(ret);
7197
7198 if (arg1) {
7199 unlock_user(p, arg1, 0);
7200 }
7201 unlock_user(p2, arg2, 0);
7202 if (arg3) {
7203 unlock_user(p3, arg3, 0);
7204 }
7205 }
7206 break;
e5febef5 7207#ifdef TARGET_NR_umount
31e31b8a 7208 case TARGET_NR_umount:
579a97f7
FB
7209 if (!(p = lock_user_string(arg1)))
7210 goto efault;
53a5960a
PB
7211 ret = get_errno(umount(p));
7212 unlock_user(p, arg1, 0);
31e31b8a 7213 break;
e5febef5 7214#endif
7a3148a9 7215#ifdef TARGET_NR_stime /* not on alpha */
31e31b8a
FB
7216 case TARGET_NR_stime:
7217 {
53a5960a 7218 time_t host_time;
2f619698
FB
7219 if (get_user_sal(host_time, arg1))
7220 goto efault;
53a5960a 7221 ret = get_errno(stime(&host_time));
31e31b8a
FB
7222 }
7223 break;
7a3148a9 7224#endif
31e31b8a
FB
7225 case TARGET_NR_ptrace:
7226 goto unimplemented;
7a3148a9 7227#ifdef TARGET_NR_alarm /* not on alpha */
31e31b8a
FB
7228 case TARGET_NR_alarm:
7229 ret = alarm(arg1);
7230 break;
7a3148a9 7231#endif
ebc05488 7232#ifdef TARGET_NR_oldfstat
31e31b8a
FB
7233 case TARGET_NR_oldfstat:
7234 goto unimplemented;
ebc05488 7235#endif
7a3148a9 7236#ifdef TARGET_NR_pause /* not on alpha */
31e31b8a 7237 case TARGET_NR_pause:
f59ec606
TB
7238 if (!block_signals()) {
7239 sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
7240 }
7241 ret = -TARGET_EINTR;
31e31b8a 7242 break;
7a3148a9 7243#endif
e5febef5 7244#ifdef TARGET_NR_utime
31e31b8a 7245 case TARGET_NR_utime:
ebc05488 7246 {
53a5960a
PB
7247 struct utimbuf tbuf, *host_tbuf;
7248 struct target_utimbuf *target_tbuf;
7249 if (arg2) {
579a97f7
FB
7250 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
7251 goto efault;
cbb21eed
MB
7252 tbuf.actime = tswapal(target_tbuf->actime);
7253 tbuf.modtime = tswapal(target_tbuf->modtime);
53a5960a
PB
7254 unlock_user_struct(target_tbuf, arg2, 0);
7255 host_tbuf = &tbuf;
f72e8ff4 7256 } else {
53a5960a 7257 host_tbuf = NULL;
f72e8ff4 7258 }
579a97f7
FB
7259 if (!(p = lock_user_string(arg1)))
7260 goto efault;
53a5960a
PB
7261 ret = get_errno(utime(p, host_tbuf));
7262 unlock_user(p, arg1, 0);
ebc05488
FB
7263 }
7264 break;
e5febef5 7265#endif
704eff6c 7266#ifdef TARGET_NR_utimes
978a66ff
FB
7267 case TARGET_NR_utimes:
7268 {
978a66ff 7269 struct timeval *tvp, tv[2];
53a5960a 7270 if (arg2) {
788f5ec4
TS
7271 if (copy_from_user_timeval(&tv[0], arg2)
7272 || copy_from_user_timeval(&tv[1],
7273 arg2 + sizeof(struct target_timeval)))
7274 goto efault;
978a66ff
FB
7275 tvp = tv;
7276 } else {
7277 tvp = NULL;
7278 }
579a97f7
FB
7279 if (!(p = lock_user_string(arg1)))
7280 goto efault;
53a5960a
PB
7281 ret = get_errno(utimes(p, tvp));
7282 unlock_user(p, arg1, 0);
978a66ff
FB
7283 }
7284 break;
704eff6c 7285#endif
c0d472b1 7286#if defined(TARGET_NR_futimesat)
ac8a6556
AZ
7287 case TARGET_NR_futimesat:
7288 {
7289 struct timeval *tvp, tv[2];
7290 if (arg3) {
7291 if (copy_from_user_timeval(&tv[0], arg3)
7292 || copy_from_user_timeval(&tv[1],
7293 arg3 + sizeof(struct target_timeval)))
7294 goto efault;
7295 tvp = tv;
7296 } else {
7297 tvp = NULL;
7298 }
7299 if (!(p = lock_user_string(arg2)))
7300 goto efault;
c0d472b1 7301 ret = get_errno(futimesat(arg1, path(p), tvp));
ac8a6556
AZ
7302 unlock_user(p, arg2, 0);
7303 }
7304 break;
7305#endif
ebc05488 7306#ifdef TARGET_NR_stty
31e31b8a
FB
7307 case TARGET_NR_stty:
7308 goto unimplemented;
ebc05488
FB
7309#endif
7310#ifdef TARGET_NR_gtty
31e31b8a
FB
7311 case TARGET_NR_gtty:
7312 goto unimplemented;
ebc05488 7313#endif
704eff6c 7314#ifdef TARGET_NR_access
31e31b8a 7315 case TARGET_NR_access:
579a97f7
FB
7316 if (!(p = lock_user_string(arg1)))
7317 goto efault;
719f908e 7318 ret = get_errno(access(path(p), arg2));
53a5960a 7319 unlock_user(p, arg1, 0);
31e31b8a 7320 break;
704eff6c 7321#endif
92a34c10
TS
7322#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
7323 case TARGET_NR_faccessat:
579a97f7
FB
7324 if (!(p = lock_user_string(arg2)))
7325 goto efault;
c0d472b1 7326 ret = get_errno(faccessat(arg1, p, arg3, 0));
579a97f7 7327 unlock_user(p, arg2, 0);
92a34c10
TS
7328 break;
7329#endif
7a3148a9 7330#ifdef TARGET_NR_nice /* not on alpha */
31e31b8a
FB
7331 case TARGET_NR_nice:
7332 ret = get_errno(nice(arg1));
7333 break;
7a3148a9 7334#endif
ebc05488 7335#ifdef TARGET_NR_ftime
31e31b8a
FB
7336 case TARGET_NR_ftime:
7337 goto unimplemented;
ebc05488 7338#endif
31e31b8a 7339 case TARGET_NR_sync:
04369ff2
FB
7340 sync();
7341 ret = 0;
31e31b8a
FB
7342 break;
7343 case TARGET_NR_kill:
bef653d9 7344 ret = get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
31e31b8a 7345 break;
704eff6c 7346#ifdef TARGET_NR_rename
31e31b8a 7347 case TARGET_NR_rename:
53a5960a
PB
7348 {
7349 void *p2;
7350 p = lock_user_string(arg1);
7351 p2 = lock_user_string(arg2);
579a97f7
FB
7352 if (!p || !p2)
7353 ret = -TARGET_EFAULT;
7354 else
7355 ret = get_errno(rename(p, p2));
53a5960a
PB
7356 unlock_user(p2, arg2, 0);
7357 unlock_user(p, arg1, 0);
7358 }
31e31b8a 7359 break;
704eff6c 7360#endif
c0d472b1 7361#if defined(TARGET_NR_renameat)
722183f6 7362 case TARGET_NR_renameat:
722183f6 7363 {
579a97f7 7364 void *p2;
722183f6
TS
7365 p = lock_user_string(arg2);
7366 p2 = lock_user_string(arg4);
579a97f7 7367 if (!p || !p2)
0da46a6e 7368 ret = -TARGET_EFAULT;
722183f6 7369 else
c0d472b1 7370 ret = get_errno(renameat(arg1, p, arg3, p2));
579a97f7
FB
7371 unlock_user(p2, arg4, 0);
7372 unlock_user(p, arg2, 0);
722183f6
TS
7373 }
7374 break;
7375#endif
704eff6c 7376#ifdef TARGET_NR_mkdir
31e31b8a 7377 case TARGET_NR_mkdir:
579a97f7
FB
7378 if (!(p = lock_user_string(arg1)))
7379 goto efault;
53a5960a
PB
7380 ret = get_errno(mkdir(p, arg2));
7381 unlock_user(p, arg1, 0);
31e31b8a 7382 break;
704eff6c 7383#endif
c0d472b1 7384#if defined(TARGET_NR_mkdirat)
4472ad0d 7385 case TARGET_NR_mkdirat:
579a97f7
FB
7386 if (!(p = lock_user_string(arg2)))
7387 goto efault;
c0d472b1 7388 ret = get_errno(mkdirat(arg1, p, arg3));
579a97f7 7389 unlock_user(p, arg2, 0);
4472ad0d
TS
7390 break;
7391#endif
704eff6c 7392#ifdef TARGET_NR_rmdir
31e31b8a 7393 case TARGET_NR_rmdir:
579a97f7
FB
7394 if (!(p = lock_user_string(arg1)))
7395 goto efault;
53a5960a
PB
7396 ret = get_errno(rmdir(p));
7397 unlock_user(p, arg1, 0);
31e31b8a 7398 break;
704eff6c 7399#endif
31e31b8a
FB
7400 case TARGET_NR_dup:
7401 ret = get_errno(dup(arg1));
e36800c9
LV
7402 if (ret >= 0) {
7403 fd_trans_dup(arg1, ret);
7404 }
31e31b8a 7405 break;
704eff6c 7406#ifdef TARGET_NR_pipe
31e31b8a 7407 case TARGET_NR_pipe:
fb41a66e 7408 ret = do_pipe(cpu_env, arg1, 0, 0);
099d6b0f 7409 break;
704eff6c 7410#endif
099d6b0f
RV
7411#ifdef TARGET_NR_pipe2
7412 case TARGET_NR_pipe2:
e7ea6cbe
RH
7413 ret = do_pipe(cpu_env, arg1,
7414 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
31e31b8a 7415 break;
099d6b0f 7416#endif
31e31b8a 7417 case TARGET_NR_times:
32f36bce 7418 {
53a5960a 7419 struct target_tms *tmsp;
32f36bce
FB
7420 struct tms tms;
7421 ret = get_errno(times(&tms));
53a5960a 7422 if (arg1) {
579a97f7
FB
7423 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
7424 if (!tmsp)
7425 goto efault;
cbb21eed
MB
7426 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
7427 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
7428 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
7429 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
32f36bce 7430 }
c596ed17
FB
7431 if (!is_error(ret))
7432 ret = host_to_target_clock_t(ret);
32f36bce
FB
7433 }
7434 break;
ebc05488 7435#ifdef TARGET_NR_prof
31e31b8a
FB
7436 case TARGET_NR_prof:
7437 goto unimplemented;
ebc05488 7438#endif
e5febef5 7439#ifdef TARGET_NR_signal
31e31b8a
FB
7440 case TARGET_NR_signal:
7441 goto unimplemented;
e5febef5 7442#endif
31e31b8a 7443 case TARGET_NR_acct:
38d840e6
AJ
7444 if (arg1 == 0) {
7445 ret = get_errno(acct(NULL));
7446 } else {
7447 if (!(p = lock_user_string(arg1)))
7448 goto efault;
7449 ret = get_errno(acct(path(p)));
7450 unlock_user(p, arg1, 0);
7451 }
24836689 7452 break;
8070e7be 7453#ifdef TARGET_NR_umount2
31e31b8a 7454 case TARGET_NR_umount2:
579a97f7
FB
7455 if (!(p = lock_user_string(arg1)))
7456 goto efault;
53a5960a
PB
7457 ret = get_errno(umount2(p, arg2));
7458 unlock_user(p, arg1, 0);
31e31b8a 7459 break;
7a3148a9 7460#endif
ebc05488 7461#ifdef TARGET_NR_lock
31e31b8a
FB
7462 case TARGET_NR_lock:
7463 goto unimplemented;
ebc05488 7464#endif
31e31b8a
FB
7465 case TARGET_NR_ioctl:
7466 ret = do_ioctl(arg1, arg2, arg3);
7467 break;
7468 case TARGET_NR_fcntl:
9ee1fa2c 7469 ret = do_fcntl(arg1, arg2, arg3);
31e31b8a 7470 break;
ebc05488 7471#ifdef TARGET_NR_mpx
31e31b8a
FB
7472 case TARGET_NR_mpx:
7473 goto unimplemented;
ebc05488 7474#endif
31e31b8a
FB
7475 case TARGET_NR_setpgid:
7476 ret = get_errno(setpgid(arg1, arg2));
7477 break;
ebc05488 7478#ifdef TARGET_NR_ulimit
31e31b8a
FB
7479 case TARGET_NR_ulimit:
7480 goto unimplemented;
ebc05488
FB
7481#endif
7482#ifdef TARGET_NR_oldolduname
31e31b8a
FB
7483 case TARGET_NR_oldolduname:
7484 goto unimplemented;
ebc05488 7485#endif
31e31b8a
FB
7486 case TARGET_NR_umask:
7487 ret = get_errno(umask(arg1));
7488 break;
7489 case TARGET_NR_chroot:
579a97f7
FB
7490 if (!(p = lock_user_string(arg1)))
7491 goto efault;
53a5960a
PB
7492 ret = get_errno(chroot(p));
7493 unlock_user(p, arg1, 0);
31e31b8a 7494 break;
704eff6c 7495#ifdef TARGET_NR_ustat
31e31b8a
FB
7496 case TARGET_NR_ustat:
7497 goto unimplemented;
704eff6c
CG
7498#endif
7499#ifdef TARGET_NR_dup2
31e31b8a
FB
7500 case TARGET_NR_dup2:
7501 ret = get_errno(dup2(arg1, arg2));
e36800c9
LV
7502 if (ret >= 0) {
7503 fd_trans_dup(arg1, arg2);
7504 }
31e31b8a 7505 break;
704eff6c 7506#endif
d0927938
UH
7507#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
7508 case TARGET_NR_dup3:
7509 ret = get_errno(dup3(arg1, arg2, arg3));
e36800c9
LV
7510 if (ret >= 0) {
7511 fd_trans_dup(arg1, arg2);
7512 }
d0927938
UH
7513 break;
7514#endif
7a3148a9 7515#ifdef TARGET_NR_getppid /* not on alpha */
31e31b8a
FB
7516 case TARGET_NR_getppid:
7517 ret = get_errno(getppid());
7518 break;
7a3148a9 7519#endif
704eff6c 7520#ifdef TARGET_NR_getpgrp
31e31b8a
FB
7521 case TARGET_NR_getpgrp:
7522 ret = get_errno(getpgrp());
7523 break;
704eff6c 7524#endif
31e31b8a
FB
7525 case TARGET_NR_setsid:
7526 ret = get_errno(setsid());
7527 break;
e5febef5 7528#ifdef TARGET_NR_sigaction
31e31b8a 7529 case TARGET_NR_sigaction:
31e31b8a 7530 {
6049f4f8
RH
7531#if defined(TARGET_ALPHA)
7532 struct target_sigaction act, oact, *pact = 0;
53a5960a 7533 struct target_old_sigaction *old_act;
53a5960a 7534 if (arg2) {
579a97f7
FB
7535 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
7536 goto efault;
66fb9763
FB
7537 act._sa_handler = old_act->_sa_handler;
7538 target_siginitset(&act.sa_mask, old_act->sa_mask);
7539 act.sa_flags = old_act->sa_flags;
6049f4f8 7540 act.sa_restorer = 0;
53a5960a 7541 unlock_user_struct(old_act, arg2, 0);
66fb9763 7542 pact = &act;
66fb9763
FB
7543 }
7544 ret = get_errno(do_sigaction(arg1, pact, &oact));
53a5960a 7545 if (!is_error(ret) && arg3) {
579a97f7
FB
7546 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
7547 goto efault;
53a5960a
PB
7548 old_act->_sa_handler = oact._sa_handler;
7549 old_act->sa_mask = oact.sa_mask.sig[0];
7550 old_act->sa_flags = oact.sa_flags;
53a5960a 7551 unlock_user_struct(old_act, arg3, 1);
66fb9763 7552 }
6049f4f8 7553#elif defined(TARGET_MIPS)
106ec879
FB
7554 struct target_sigaction act, oact, *pact, *old_act;
7555
7556 if (arg2) {
579a97f7
FB
7557 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
7558 goto efault;
106ec879
FB
7559 act._sa_handler = old_act->_sa_handler;
7560 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
7561 act.sa_flags = old_act->sa_flags;
7562 unlock_user_struct(old_act, arg2, 0);
7563 pact = &act;
7564 } else {
7565 pact = NULL;
7566 }
7567
7568 ret = get_errno(do_sigaction(arg1, pact, &oact));
7569
7570 if (!is_error(ret) && arg3) {
579a97f7
FB
7571 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
7572 goto efault;
106ec879
FB
7573 old_act->_sa_handler = oact._sa_handler;
7574 old_act->sa_flags = oact.sa_flags;
7575 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
7576 old_act->sa_mask.sig[1] = 0;
7577 old_act->sa_mask.sig[2] = 0;
7578 old_act->sa_mask.sig[3] = 0;
7579 unlock_user_struct(old_act, arg3, 1);
7580 }
6049f4f8
RH
7581#else
7582 struct target_old_sigaction *old_act;
7583 struct target_sigaction act, oact, *pact;
7584 if (arg2) {
7585 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
7586 goto efault;
7587 act._sa_handler = old_act->_sa_handler;
7588 target_siginitset(&act.sa_mask, old_act->sa_mask);
7589 act.sa_flags = old_act->sa_flags;
7590 act.sa_restorer = old_act->sa_restorer;
7591 unlock_user_struct(old_act, arg2, 0);
7592 pact = &act;
7593 } else {
7594 pact = NULL;
7595 }
7596 ret = get_errno(do_sigaction(arg1, pact, &oact));
7597 if (!is_error(ret) && arg3) {
7598 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
7599 goto efault;
7600 old_act->_sa_handler = oact._sa_handler;
7601 old_act->sa_mask = oact.sa_mask.sig[0];
7602 old_act->sa_flags = oact.sa_flags;
7603 old_act->sa_restorer = oact.sa_restorer;
7604 unlock_user_struct(old_act, arg3, 1);
7605 }
388bb21a 7606#endif
31e31b8a
FB
7607 }
7608 break;
e5febef5 7609#endif
66fb9763 7610 case TARGET_NR_rt_sigaction:
53a5960a 7611 {
6049f4f8
RH
7612#if defined(TARGET_ALPHA)
7613 struct target_sigaction act, oact, *pact = 0;
7614 struct target_rt_sigaction *rt_act;
7615 /* ??? arg4 == sizeof(sigset_t). */
7616 if (arg2) {
7617 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
7618 goto efault;
7619 act._sa_handler = rt_act->_sa_handler;
7620 act.sa_mask = rt_act->sa_mask;
7621 act.sa_flags = rt_act->sa_flags;
7622 act.sa_restorer = arg5;
7623 unlock_user_struct(rt_act, arg2, 0);
7624 pact = &act;
7625 }
7626 ret = get_errno(do_sigaction(arg1, pact, &oact));
7627 if (!is_error(ret) && arg3) {
7628 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
7629 goto efault;
7630 rt_act->_sa_handler = oact._sa_handler;
7631 rt_act->sa_mask = oact.sa_mask;
7632 rt_act->sa_flags = oact.sa_flags;
7633 unlock_user_struct(rt_act, arg3, 1);
7634 }
7635#else
53a5960a
PB
7636 struct target_sigaction *act;
7637 struct target_sigaction *oact;
7638
579a97f7
FB
7639 if (arg2) {
7640 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
7641 goto efault;
7642 } else
53a5960a 7643 act = NULL;
579a97f7
FB
7644 if (arg3) {
7645 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
7646 ret = -TARGET_EFAULT;
7647 goto rt_sigaction_fail;
7648 }
7649 } else
53a5960a
PB
7650 oact = NULL;
7651 ret = get_errno(do_sigaction(arg1, act, oact));
579a97f7
FB
7652 rt_sigaction_fail:
7653 if (act)
53a5960a 7654 unlock_user_struct(act, arg2, 0);
579a97f7 7655 if (oact)
53a5960a 7656 unlock_user_struct(oact, arg3, 1);
6049f4f8 7657#endif
53a5960a 7658 }
66fb9763 7659 break;
7a3148a9 7660#ifdef TARGET_NR_sgetmask /* not on alpha */
31e31b8a 7661 case TARGET_NR_sgetmask:
66fb9763
FB
7662 {
7663 sigset_t cur_set;
992f48a0 7664 abi_ulong target_set;
3d3efba0
PM
7665 ret = do_sigprocmask(0, NULL, &cur_set);
7666 if (!ret) {
7667 host_to_target_old_sigset(&target_set, &cur_set);
7668 ret = target_set;
7669 }
66fb9763
FB
7670 }
7671 break;
7a3148a9
JM
7672#endif
7673#ifdef TARGET_NR_ssetmask /* not on alpha */
31e31b8a 7674 case TARGET_NR_ssetmask:
66fb9763
FB
7675 {
7676 sigset_t set, oset, cur_set;
992f48a0 7677 abi_ulong target_set = arg1;
3d3efba0
PM
7678 /* We only have one word of the new mask so we must read
7679 * the rest of it with do_sigprocmask() and OR in this word.
7680 * We are guaranteed that a do_sigprocmask() that only queries
7681 * the signal mask will not fail.
7682 */
7683 ret = do_sigprocmask(0, NULL, &cur_set);
7684 assert(!ret);
66fb9763
FB
7685 target_to_host_old_sigset(&set, &target_set);
7686 sigorset(&set, &set, &cur_set);
3d3efba0
PM
7687 ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
7688 if (!ret) {
7689 host_to_target_old_sigset(&target_set, &oset);
7690 ret = target_set;
7691 }
66fb9763
FB
7692 }
7693 break;
7a3148a9 7694#endif
e5febef5 7695#ifdef TARGET_NR_sigprocmask
66fb9763
FB
7696 case TARGET_NR_sigprocmask:
7697 {
a5b3b13b
RH
7698#if defined(TARGET_ALPHA)
7699 sigset_t set, oldset;
7700 abi_ulong mask;
7701 int how;
7702
7703 switch (arg1) {
7704 case TARGET_SIG_BLOCK:
7705 how = SIG_BLOCK;
7706 break;
7707 case TARGET_SIG_UNBLOCK:
7708 how = SIG_UNBLOCK;
7709 break;
7710 case TARGET_SIG_SETMASK:
7711 how = SIG_SETMASK;
7712 break;
7713 default:
7714 ret = -TARGET_EINVAL;
7715 goto fail;
7716 }
7717 mask = arg2;
7718 target_to_host_old_sigset(&set, &mask);
7719
3d3efba0 7720 ret = do_sigprocmask(how, &set, &oldset);
a5b3b13b
RH
7721 if (!is_error(ret)) {
7722 host_to_target_old_sigset(&mask, &oldset);
7723 ret = mask;
0229f5a3 7724 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
a5b3b13b
RH
7725 }
7726#else
66fb9763 7727 sigset_t set, oldset, *set_ptr;
a5b3b13b 7728 int how;
3b46e624 7729
53a5960a 7730 if (arg2) {
a5b3b13b 7731 switch (arg1) {
66fb9763
FB
7732 case TARGET_SIG_BLOCK:
7733 how = SIG_BLOCK;
7734 break;
7735 case TARGET_SIG_UNBLOCK:
7736 how = SIG_UNBLOCK;
7737 break;
7738 case TARGET_SIG_SETMASK:
7739 how = SIG_SETMASK;
7740 break;
7741 default:
0da46a6e 7742 ret = -TARGET_EINVAL;
66fb9763
FB
7743 goto fail;
7744 }
c227f099 7745 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
579a97f7 7746 goto efault;
53a5960a
PB
7747 target_to_host_old_sigset(&set, p);
7748 unlock_user(p, arg2, 0);
66fb9763
FB
7749 set_ptr = &set;
7750 } else {
7751 how = 0;
7752 set_ptr = NULL;
7753 }
3d3efba0 7754 ret = do_sigprocmask(how, set_ptr, &oldset);
53a5960a 7755 if (!is_error(ret) && arg3) {
c227f099 7756 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
579a97f7 7757 goto efault;
53a5960a 7758 host_to_target_old_sigset(p, &oldset);
c227f099 7759 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763 7760 }
a5b3b13b 7761#endif
66fb9763
FB
7762 }
7763 break;
e5febef5 7764#endif
66fb9763
FB
7765 case TARGET_NR_rt_sigprocmask:
7766 {
7767 int how = arg1;
7768 sigset_t set, oldset, *set_ptr;
3b46e624 7769
53a5960a 7770 if (arg2) {
66fb9763
FB
7771 switch(how) {
7772 case TARGET_SIG_BLOCK:
7773 how = SIG_BLOCK;
7774 break;
7775 case TARGET_SIG_UNBLOCK:
7776 how = SIG_UNBLOCK;
7777 break;
7778 case TARGET_SIG_SETMASK:
7779 how = SIG_SETMASK;
7780 break;
7781 default:
0da46a6e 7782 ret = -TARGET_EINVAL;
66fb9763
FB
7783 goto fail;
7784 }
c227f099 7785 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
579a97f7 7786 goto efault;
53a5960a
PB
7787 target_to_host_sigset(&set, p);
7788 unlock_user(p, arg2, 0);
66fb9763
FB
7789 set_ptr = &set;
7790 } else {
7791 how = 0;
7792 set_ptr = NULL;
7793 }
3d3efba0 7794 ret = do_sigprocmask(how, set_ptr, &oldset);
53a5960a 7795 if (!is_error(ret) && arg3) {
c227f099 7796 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
579a97f7 7797 goto efault;
53a5960a 7798 host_to_target_sigset(p, &oldset);
c227f099 7799 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763
FB
7800 }
7801 }
7802 break;
e5febef5 7803#ifdef TARGET_NR_sigpending
66fb9763
FB
7804 case TARGET_NR_sigpending:
7805 {
7806 sigset_t set;
7807 ret = get_errno(sigpending(&set));
7808 if (!is_error(ret)) {
c227f099 7809 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
579a97f7 7810 goto efault;
53a5960a 7811 host_to_target_old_sigset(p, &set);
c227f099 7812 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
7813 }
7814 }
7815 break;
e5febef5 7816#endif
66fb9763
FB
7817 case TARGET_NR_rt_sigpending:
7818 {
7819 sigset_t set;
7820 ret = get_errno(sigpending(&set));
7821 if (!is_error(ret)) {
c227f099 7822 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
579a97f7 7823 goto efault;
53a5960a 7824 host_to_target_sigset(p, &set);
c227f099 7825 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
7826 }
7827 }
7828 break;
e5febef5 7829#ifdef TARGET_NR_sigsuspend
66fb9763
FB
7830 case TARGET_NR_sigsuspend:
7831 {
3d3efba0 7832 TaskState *ts = cpu->opaque;
f43ce12b
RH
7833#if defined(TARGET_ALPHA)
7834 abi_ulong mask = arg1;
3d3efba0 7835 target_to_host_old_sigset(&ts->sigsuspend_mask, &mask);
f43ce12b 7836#else
c227f099 7837 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 7838 goto efault;
3d3efba0 7839 target_to_host_old_sigset(&ts->sigsuspend_mask, p);
53a5960a 7840 unlock_user(p, arg1, 0);
f43ce12b 7841#endif
3d3efba0
PM
7842 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
7843 SIGSET_T_SIZE));
7844 if (ret != -TARGET_ERESTARTSYS) {
7845 ts->in_sigsuspend = 1;
7846 }
66fb9763
FB
7847 }
7848 break;
e5febef5 7849#endif
66fb9763
FB
7850 case TARGET_NR_rt_sigsuspend:
7851 {
3d3efba0 7852 TaskState *ts = cpu->opaque;
c227f099 7853 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 7854 goto efault;
3d3efba0 7855 target_to_host_sigset(&ts->sigsuspend_mask, p);
53a5960a 7856 unlock_user(p, arg1, 0);
3d3efba0
PM
7857 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
7858 SIGSET_T_SIZE));
7859 if (ret != -TARGET_ERESTARTSYS) {
7860 ts->in_sigsuspend = 1;
7861 }
66fb9763
FB
7862 }
7863 break;
7864 case TARGET_NR_rt_sigtimedwait:
7865 {
66fb9763
FB
7866 sigset_t set;
7867 struct timespec uts, *puts;
7868 siginfo_t uinfo;
3b46e624 7869
c227f099 7870 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 7871 goto efault;
53a5960a
PB
7872 target_to_host_sigset(&set, p);
7873 unlock_user(p, arg1, 0);
7874 if (arg3) {
66fb9763 7875 puts = &uts;
53a5960a 7876 target_to_host_timespec(puts, arg3);
66fb9763
FB
7877 } else {
7878 puts = NULL;
7879 }
b3f82330
PM
7880 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
7881 SIGSET_T_SIZE));
974a196d
PJ
7882 if (!is_error(ret)) {
7883 if (arg2) {
7884 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
7885 0);
7886 if (!p) {
7887 goto efault;
7888 }
7889 host_to_target_siginfo(p, &uinfo);
7890 unlock_user(p, arg2, sizeof(target_siginfo_t));
7891 }
7892 ret = host_to_target_signal(ret);
66fb9763
FB
7893 }
7894 }
7895 break;
7896 case TARGET_NR_rt_sigqueueinfo:
7897 {
7898 siginfo_t uinfo;
4debae6f
PM
7899
7900 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
7901 if (!p) {
579a97f7 7902 goto efault;
4debae6f 7903 }
53a5960a
PB
7904 target_to_host_siginfo(&uinfo, p);
7905 unlock_user(p, arg1, 0);
66fb9763
FB
7906 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
7907 }
7908 break;
e5febef5 7909#ifdef TARGET_NR_sigreturn
66fb9763 7910 case TARGET_NR_sigreturn:
3d3efba0
PM
7911 if (block_signals()) {
7912 ret = -TARGET_ERESTARTSYS;
7913 } else {
7914 ret = do_sigreturn(cpu_env);
7915 }
66fb9763 7916 break;
e5febef5 7917#endif
66fb9763 7918 case TARGET_NR_rt_sigreturn:
3d3efba0
PM
7919 if (block_signals()) {
7920 ret = -TARGET_ERESTARTSYS;
7921 } else {
7922 ret = do_rt_sigreturn(cpu_env);
7923 }
66fb9763 7924 break;
31e31b8a 7925 case TARGET_NR_sethostname:
579a97f7
FB
7926 if (!(p = lock_user_string(arg1)))
7927 goto efault;
53a5960a
PB
7928 ret = get_errno(sethostname(p, arg2));
7929 unlock_user(p, arg1, 0);
31e31b8a
FB
7930 break;
7931 case TARGET_NR_setrlimit:
9de5e440 7932 {
e22b7015 7933 int resource = target_to_host_resource(arg1);
53a5960a 7934 struct target_rlimit *target_rlim;
9de5e440 7935 struct rlimit rlim;
579a97f7
FB
7936 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
7937 goto efault;
81bbe906
TY
7938 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
7939 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
53a5960a 7940 unlock_user_struct(target_rlim, arg2, 0);
9de5e440
FB
7941 ret = get_errno(setrlimit(resource, &rlim));
7942 }
7943 break;
31e31b8a 7944 case TARGET_NR_getrlimit:
9de5e440 7945 {
e22b7015 7946 int resource = target_to_host_resource(arg1);
53a5960a 7947 struct target_rlimit *target_rlim;
9de5e440 7948 struct rlimit rlim;
3b46e624 7949
9de5e440
FB
7950 ret = get_errno(getrlimit(resource, &rlim));
7951 if (!is_error(ret)) {
579a97f7
FB
7952 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7953 goto efault;
81bbe906
TY
7954 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7955 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 7956 unlock_user_struct(target_rlim, arg2, 1);
9de5e440
FB
7957 }
7958 }
7959 break;
31e31b8a 7960 case TARGET_NR_getrusage:
b409186b
FB
7961 {
7962 struct rusage rusage;
b409186b
FB
7963 ret = get_errno(getrusage(arg1, &rusage));
7964 if (!is_error(ret)) {
a39fb273 7965 ret = host_to_target_rusage(arg2, &rusage);
b409186b
FB
7966 }
7967 }
7968 break;
31e31b8a
FB
7969 case TARGET_NR_gettimeofday:
7970 {
31e31b8a
FB
7971 struct timeval tv;
7972 ret = get_errno(gettimeofday(&tv, NULL));
7973 if (!is_error(ret)) {
788f5ec4
TS
7974 if (copy_to_user_timeval(arg1, &tv))
7975 goto efault;
31e31b8a
FB
7976 }
7977 }
7978 break;
7979 case TARGET_NR_settimeofday:
7980 {
b67d8031 7981 struct timeval tv, *ptv = NULL;
ef4467e9
PB
7982 struct timezone tz, *ptz = NULL;
7983
b67d8031
PB
7984 if (arg1) {
7985 if (copy_from_user_timeval(&tv, arg1)) {
7986 goto efault;
7987 }
7988 ptv = &tv;
7989 }
ef4467e9
PB
7990
7991 if (arg2) {
7992 if (copy_from_user_timezone(&tz, arg2)) {
7993 goto efault;
7994 }
7995 ptz = &tz;
7996 }
7997
b67d8031 7998 ret = get_errno(settimeofday(ptv, ptz));
31e31b8a
FB
7999 }
8000 break;
9468a5d4 8001#if defined(TARGET_NR_select)
31e31b8a 8002 case TARGET_NR_select:
9468a5d4
LV
8003#if defined(TARGET_S390X) || defined(TARGET_ALPHA)
8004 ret = do_select(arg1, arg2, arg3, arg4, arg5);
8005#else
f2674e31 8006 {
53a5960a 8007 struct target_sel_arg_struct *sel;
992f48a0 8008 abi_ulong inp, outp, exp, tvp;
53a5960a
PB
8009 long nsel;
8010
579a97f7
FB
8011 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
8012 goto efault;
cbb21eed
MB
8013 nsel = tswapal(sel->n);
8014 inp = tswapal(sel->inp);
8015 outp = tswapal(sel->outp);
8016 exp = tswapal(sel->exp);
8017 tvp = tswapal(sel->tvp);
53a5960a
PB
8018 unlock_user_struct(sel, arg1, 0);
8019 ret = do_select(nsel, inp, outp, exp, tvp);
f2674e31 8020 }
9468a5d4 8021#endif
f2674e31 8022 break;
9e42382f
RV
8023#endif
8024#ifdef TARGET_NR_pselect6
8025 case TARGET_NR_pselect6:
055e0906
MF
8026 {
8027 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
8028 fd_set rfds, wfds, efds;
8029 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
8030 struct timespec ts, *ts_ptr;
8031
8032 /*
8033 * The 6th arg is actually two args smashed together,
8034 * so we cannot use the C library.
8035 */
8036 sigset_t set;
8037 struct {
8038 sigset_t *set;
8039 size_t size;
8040 } sig, *sig_ptr;
8041
8042 abi_ulong arg_sigset, arg_sigsize, *arg7;
8043 target_sigset_t *target_sigset;
8044
8045 n = arg1;
8046 rfd_addr = arg2;
8047 wfd_addr = arg3;
8048 efd_addr = arg4;
8049 ts_addr = arg5;
8050
8051 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
8052 if (ret) {
8053 goto fail;
8054 }
8055 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
8056 if (ret) {
8057 goto fail;
8058 }
8059 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
8060 if (ret) {
8061 goto fail;
8062 }
8063
8064 /*
8065 * This takes a timespec, and not a timeval, so we cannot
8066 * use the do_select() helper ...
8067 */
8068 if (ts_addr) {
8069 if (target_to_host_timespec(&ts, ts_addr)) {
8070 goto efault;
8071 }
8072 ts_ptr = &ts;
8073 } else {
8074 ts_ptr = NULL;
8075 }
8076
8077 /* Extract the two packed args for the sigset */
8078 if (arg6) {
8079 sig_ptr = &sig;
b28a1f33 8080 sig.size = SIGSET_T_SIZE;
055e0906
MF
8081
8082 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
8083 if (!arg7) {
8084 goto efault;
8085 }
cbb21eed
MB
8086 arg_sigset = tswapal(arg7[0]);
8087 arg_sigsize = tswapal(arg7[1]);
055e0906
MF
8088 unlock_user(arg7, arg6, 0);
8089
8090 if (arg_sigset) {
8091 sig.set = &set;
8f04eeb3
PM
8092 if (arg_sigsize != sizeof(*target_sigset)) {
8093 /* Like the kernel, we enforce correct size sigsets */
8094 ret = -TARGET_EINVAL;
8095 goto fail;
8096 }
055e0906
MF
8097 target_sigset = lock_user(VERIFY_READ, arg_sigset,
8098 sizeof(*target_sigset), 1);
8099 if (!target_sigset) {
8100 goto efault;
8101 }
8102 target_to_host_sigset(&set, target_sigset);
8103 unlock_user(target_sigset, arg_sigset, 0);
8104 } else {
8105 sig.set = NULL;
8106 }
8107 } else {
8108 sig_ptr = NULL;
8109 }
8110
6df9d38d
PM
8111 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
8112 ts_ptr, sig_ptr));
055e0906
MF
8113
8114 if (!is_error(ret)) {
8115 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
8116 goto efault;
8117 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
8118 goto efault;
8119 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
8120 goto efault;
8121
8122 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
8123 goto efault;
8124 }
8125 }
8126 break;
048f6b4d 8127#endif
704eff6c 8128#ifdef TARGET_NR_symlink
31e31b8a 8129 case TARGET_NR_symlink:
53a5960a
PB
8130 {
8131 void *p2;
8132 p = lock_user_string(arg1);
8133 p2 = lock_user_string(arg2);
579a97f7
FB
8134 if (!p || !p2)
8135 ret = -TARGET_EFAULT;
8136 else
8137 ret = get_errno(symlink(p, p2));
53a5960a
PB
8138 unlock_user(p2, arg2, 0);
8139 unlock_user(p, arg1, 0);
8140 }
31e31b8a 8141 break;
704eff6c 8142#endif
c0d472b1 8143#if defined(TARGET_NR_symlinkat)
f0b6243d 8144 case TARGET_NR_symlinkat:
f0b6243d 8145 {
579a97f7 8146 void *p2;
f0b6243d
TS
8147 p = lock_user_string(arg1);
8148 p2 = lock_user_string(arg3);
579a97f7 8149 if (!p || !p2)
0da46a6e 8150 ret = -TARGET_EFAULT;
f0b6243d 8151 else
c0d472b1 8152 ret = get_errno(symlinkat(p, arg2, p2));
579a97f7
FB
8153 unlock_user(p2, arg3, 0);
8154 unlock_user(p, arg1, 0);
f0b6243d
TS
8155 }
8156 break;
8157#endif
ebc05488 8158#ifdef TARGET_NR_oldlstat
31e31b8a
FB
8159 case TARGET_NR_oldlstat:
8160 goto unimplemented;
ebc05488 8161#endif
704eff6c 8162#ifdef TARGET_NR_readlink
31e31b8a 8163 case TARGET_NR_readlink:
53a5960a 8164 {
463d8e73 8165 void *p2;
53a5960a 8166 p = lock_user_string(arg1);
579a97f7 8167 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
463d8e73 8168 if (!p || !p2) {
579a97f7 8169 ret = -TARGET_EFAULT;
f17f4989
MF
8170 } else if (!arg3) {
8171 /* Short circuit this for the magic exe check. */
8172 ret = -TARGET_EINVAL;
463d8e73
AS
8173 } else if (is_proc_myself((const char *)p, "exe")) {
8174 char real[PATH_MAX], *temp;
8175 temp = realpath(exec_path, real);
f17f4989
MF
8176 /* Return value is # of bytes that we wrote to the buffer. */
8177 if (temp == NULL) {
8178 ret = get_errno(-1);
8179 } else {
8180 /* Don't worry about sign mismatch as earlier mapping
8181 * logic would have thrown a bad address error. */
8182 ret = MIN(strlen(real), arg3);
8183 /* We cannot NUL terminate the string. */
8184 memcpy(p2, real, ret);
8185 }
463d8e73
AS
8186 } else {
8187 ret = get_errno(readlink(path(p), p2, arg3));
d088d664 8188 }
53a5960a
PB
8189 unlock_user(p2, arg2, ret);
8190 unlock_user(p, arg1, 0);
8191 }
31e31b8a 8192 break;
704eff6c 8193#endif
c0d472b1 8194#if defined(TARGET_NR_readlinkat)
5e0ccb18 8195 case TARGET_NR_readlinkat:
5e0ccb18 8196 {
579a97f7 8197 void *p2;
5e0ccb18 8198 p = lock_user_string(arg2);
579a97f7 8199 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
463d8e73
AS
8200 if (!p || !p2) {
8201 ret = -TARGET_EFAULT;
8202 } else if (is_proc_myself((const char *)p, "exe")) {
8203 char real[PATH_MAX], *temp;
8204 temp = realpath(exec_path, real);
8205 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
8206 snprintf((char *)p2, arg4, "%s", real);
8207 } else {
c0d472b1 8208 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
463d8e73 8209 }
579a97f7
FB
8210 unlock_user(p2, arg3, ret);
8211 unlock_user(p, arg2, 0);
5e0ccb18
TS
8212 }
8213 break;
8214#endif
e5febef5 8215#ifdef TARGET_NR_uselib
31e31b8a
FB
8216 case TARGET_NR_uselib:
8217 goto unimplemented;
e5febef5
TS
8218#endif
8219#ifdef TARGET_NR_swapon
31e31b8a 8220 case TARGET_NR_swapon:
579a97f7
FB
8221 if (!(p = lock_user_string(arg1)))
8222 goto efault;
53a5960a
PB
8223 ret = get_errno(swapon(p, arg2));
8224 unlock_user(p, arg1, 0);
31e31b8a 8225 break;
e5febef5 8226#endif
31e31b8a 8227 case TARGET_NR_reboot:
c07ecc68
LV
8228 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
8229 /* arg4 must be ignored in all other cases */
8230 p = lock_user_string(arg4);
8231 if (!p) {
8232 goto efault;
8233 }
8234 ret = get_errno(reboot(arg1, arg2, arg3, p));
8235 unlock_user(p, arg4, 0);
8236 } else {
8237 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
8238 }
0f6b4d21 8239 break;
e5febef5 8240#ifdef TARGET_NR_readdir
31e31b8a
FB
8241 case TARGET_NR_readdir:
8242 goto unimplemented;
e5febef5
TS
8243#endif
8244#ifdef TARGET_NR_mmap
31e31b8a 8245 case TARGET_NR_mmap:
09701199
AG
8246#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
8247 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
a4c075f1
UH
8248 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
8249 || defined(TARGET_S390X)
31e31b8a 8250 {
992f48a0
BS
8251 abi_ulong *v;
8252 abi_ulong v1, v2, v3, v4, v5, v6;
579a97f7
FB
8253 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
8254 goto efault;
cbb21eed
MB
8255 v1 = tswapal(v[0]);
8256 v2 = tswapal(v[1]);
8257 v3 = tswapal(v[2]);
8258 v4 = tswapal(v[3]);
8259 v5 = tswapal(v[4]);
8260 v6 = tswapal(v[5]);
53a5960a 8261 unlock_user(v, arg1, 0);
5fafdf24 8262 ret = get_errno(target_mmap(v1, v2, v3,
5286db75
FB
8263 target_to_host_bitmask(v4, mmap_flags_tbl),
8264 v5, v6));
31e31b8a 8265 }
31e31b8a 8266#else
5fafdf24
TS
8267 ret = get_errno(target_mmap(arg1, arg2, arg3,
8268 target_to_host_bitmask(arg4, mmap_flags_tbl),
6fb883e8
FB
8269 arg5,
8270 arg6));
31e31b8a 8271#endif
6fb883e8 8272 break;
e5febef5 8273#endif
a315a145 8274#ifdef TARGET_NR_mmap2
6fb883e8 8275 case TARGET_NR_mmap2:
bb7ec043 8276#ifndef MMAP_SHIFT
c573ff67 8277#define MMAP_SHIFT 12
c573ff67 8278#endif
5fafdf24
TS
8279 ret = get_errno(target_mmap(arg1, arg2, arg3,
8280 target_to_host_bitmask(arg4, mmap_flags_tbl),
5286db75 8281 arg5,
c573ff67 8282 arg6 << MMAP_SHIFT));
31e31b8a 8283 break;
a315a145 8284#endif
31e31b8a 8285 case TARGET_NR_munmap:
54936004 8286 ret = get_errno(target_munmap(arg1, arg2));
31e31b8a 8287 break;
9de5e440 8288 case TARGET_NR_mprotect:
97374d38 8289 {
0429a971 8290 TaskState *ts = cpu->opaque;
97374d38
PB
8291 /* Special hack to detect libc making the stack executable. */
8292 if ((arg3 & PROT_GROWSDOWN)
8293 && arg1 >= ts->info->stack_limit
8294 && arg1 <= ts->info->start_stack) {
8295 arg3 &= ~PROT_GROWSDOWN;
8296 arg2 = arg2 + arg1 - ts->info->stack_limit;
8297 arg1 = ts->info->stack_limit;
8298 }
8299 }
54936004 8300 ret = get_errno(target_mprotect(arg1, arg2, arg3));
9de5e440 8301 break;
e5febef5 8302#ifdef TARGET_NR_mremap
9de5e440 8303 case TARGET_NR_mremap:
54936004 8304 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
9de5e440 8305 break;
e5febef5 8306#endif
53a5960a 8307 /* ??? msync/mlock/munlock are broken for softmmu. */
e5febef5 8308#ifdef TARGET_NR_msync
9de5e440 8309 case TARGET_NR_msync:
53a5960a 8310 ret = get_errno(msync(g2h(arg1), arg2, arg3));
9de5e440 8311 break;
e5febef5
TS
8312#endif
8313#ifdef TARGET_NR_mlock
9de5e440 8314 case TARGET_NR_mlock:
53a5960a 8315 ret = get_errno(mlock(g2h(arg1), arg2));
9de5e440 8316 break;
e5febef5
TS
8317#endif
8318#ifdef TARGET_NR_munlock
9de5e440 8319 case TARGET_NR_munlock:
53a5960a 8320 ret = get_errno(munlock(g2h(arg1), arg2));
9de5e440 8321 break;
e5febef5
TS
8322#endif
8323#ifdef TARGET_NR_mlockall
9de5e440 8324 case TARGET_NR_mlockall:
6f6a4032 8325 ret = get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
9de5e440 8326 break;
e5febef5
TS
8327#endif
8328#ifdef TARGET_NR_munlockall
9de5e440
FB
8329 case TARGET_NR_munlockall:
8330 ret = get_errno(munlockall());
8331 break;
e5febef5 8332#endif
31e31b8a 8333 case TARGET_NR_truncate:
579a97f7
FB
8334 if (!(p = lock_user_string(arg1)))
8335 goto efault;
53a5960a
PB
8336 ret = get_errno(truncate(p, arg2));
8337 unlock_user(p, arg1, 0);
31e31b8a
FB
8338 break;
8339 case TARGET_NR_ftruncate:
8340 ret = get_errno(ftruncate(arg1, arg2));
8341 break;
8342 case TARGET_NR_fchmod:
8343 ret = get_errno(fchmod(arg1, arg2));
8344 break;
c0d472b1 8345#if defined(TARGET_NR_fchmodat)
814d7977 8346 case TARGET_NR_fchmodat:
579a97f7
FB
8347 if (!(p = lock_user_string(arg2)))
8348 goto efault;
c0d472b1 8349 ret = get_errno(fchmodat(arg1, p, arg3, 0));
579a97f7 8350 unlock_user(p, arg2, 0);
814d7977
TS
8351 break;
8352#endif
31e31b8a 8353 case TARGET_NR_getpriority:
95c09828
RH
8354 /* Note that negative values are valid for getpriority, so we must
8355 differentiate based on errno settings. */
8356 errno = 0;
8357 ret = getpriority(arg1, arg2);
8358 if (ret == -1 && errno != 0) {
8359 ret = -host_to_target_errno(errno);
8360 break;
8361 }
8362#ifdef TARGET_ALPHA
8363 /* Return value is the unbiased priority. Signal no error. */
8364 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
8365#else
8366 /* Return value is a biased priority to avoid negative numbers. */
8367 ret = 20 - ret;
8368#endif
31e31b8a
FB
8369 break;
8370 case TARGET_NR_setpriority:
8371 ret = get_errno(setpriority(arg1, arg2, arg3));
8372 break;
ebc05488 8373#ifdef TARGET_NR_profil
31e31b8a
FB
8374 case TARGET_NR_profil:
8375 goto unimplemented;
ebc05488 8376#endif
31e31b8a 8377 case TARGET_NR_statfs:
579a97f7
FB
8378 if (!(p = lock_user_string(arg1)))
8379 goto efault;
53a5960a
PB
8380 ret = get_errno(statfs(path(p), &stfs));
8381 unlock_user(p, arg1, 0);
31e31b8a
FB
8382 convert_statfs:
8383 if (!is_error(ret)) {
53a5960a 8384 struct target_statfs *target_stfs;
3b46e624 8385
579a97f7
FB
8386 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
8387 goto efault;
8388 __put_user(stfs.f_type, &target_stfs->f_type);
8389 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
8390 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
8391 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
8392 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
8393 __put_user(stfs.f_files, &target_stfs->f_files);
8394 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
8395 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
8396 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
8397 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376
AG
8398 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
8399 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
53a5960a 8400 unlock_user_struct(target_stfs, arg2, 1);
31e31b8a
FB
8401 }
8402 break;
8403 case TARGET_NR_fstatfs:
56c8f68f 8404 ret = get_errno(fstatfs(arg1, &stfs));
31e31b8a 8405 goto convert_statfs;
56c8f68f
FB
8406#ifdef TARGET_NR_statfs64
8407 case TARGET_NR_statfs64:
579a97f7
FB
8408 if (!(p = lock_user_string(arg1)))
8409 goto efault;
53a5960a
PB
8410 ret = get_errno(statfs(path(p), &stfs));
8411 unlock_user(p, arg1, 0);
56c8f68f
FB
8412 convert_statfs64:
8413 if (!is_error(ret)) {
53a5960a 8414 struct target_statfs64 *target_stfs;
3b46e624 8415
579a97f7
FB
8416 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
8417 goto efault;
8418 __put_user(stfs.f_type, &target_stfs->f_type);
8419 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
8420 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
8421 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
8422 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
8423 __put_user(stfs.f_files, &target_stfs->f_files);
8424 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
8425 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
8426 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
8427 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376
AG
8428 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
8429 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
579a97f7 8430 unlock_user_struct(target_stfs, arg3, 1);
56c8f68f
FB
8431 }
8432 break;
8433 case TARGET_NR_fstatfs64:
8434 ret = get_errno(fstatfs(arg1, &stfs));
8435 goto convert_statfs64;
8436#endif
ebc05488 8437#ifdef TARGET_NR_ioperm
31e31b8a
FB
8438 case TARGET_NR_ioperm:
8439 goto unimplemented;
ebc05488 8440#endif
e5febef5 8441#ifdef TARGET_NR_socketcall
31e31b8a 8442 case TARGET_NR_socketcall:
53a5960a 8443 ret = do_socketcall(arg1, arg2);
31e31b8a 8444 break;
e5febef5 8445#endif
3532fa74
FB
8446#ifdef TARGET_NR_accept
8447 case TARGET_NR_accept:
a94b4987
PM
8448 ret = do_accept4(arg1, arg2, arg3, 0);
8449 break;
8450#endif
8451#ifdef TARGET_NR_accept4
8452 case TARGET_NR_accept4:
a94b4987 8453 ret = do_accept4(arg1, arg2, arg3, arg4);
3532fa74
FB
8454 break;
8455#endif
8456#ifdef TARGET_NR_bind
8457 case TARGET_NR_bind:
8458 ret = do_bind(arg1, arg2, arg3);
8459 break;
8460#endif
8461#ifdef TARGET_NR_connect
8462 case TARGET_NR_connect:
8463 ret = do_connect(arg1, arg2, arg3);
8464 break;
8465#endif
8466#ifdef TARGET_NR_getpeername
8467 case TARGET_NR_getpeername:
1be9e1dc 8468 ret = do_getpeername(arg1, arg2, arg3);
3532fa74
FB
8469 break;
8470#endif
8471#ifdef TARGET_NR_getsockname
8472 case TARGET_NR_getsockname:
1be9e1dc 8473 ret = do_getsockname(arg1, arg2, arg3);
3532fa74
FB
8474 break;
8475#endif
8476#ifdef TARGET_NR_getsockopt
8477 case TARGET_NR_getsockopt:
8478 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
8479 break;
8480#endif
8481#ifdef TARGET_NR_listen
8482 case TARGET_NR_listen:
1be9e1dc 8483 ret = get_errno(listen(arg1, arg2));
3532fa74
FB
8484 break;
8485#endif
8486#ifdef TARGET_NR_recv
8487 case TARGET_NR_recv:
214201bd 8488 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
8489 break;
8490#endif
8491#ifdef TARGET_NR_recvfrom
8492 case TARGET_NR_recvfrom:
214201bd 8493 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
8494 break;
8495#endif
8496#ifdef TARGET_NR_recvmsg
8497 case TARGET_NR_recvmsg:
8498 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
8499 break;
8500#endif
8501#ifdef TARGET_NR_send
8502 case TARGET_NR_send:
1be9e1dc 8503 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
8504 break;
8505#endif
8506#ifdef TARGET_NR_sendmsg
8507 case TARGET_NR_sendmsg:
8508 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
8509 break;
8510#endif
f19e00d7
AG
8511#ifdef TARGET_NR_sendmmsg
8512 case TARGET_NR_sendmmsg:
8513 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
8514 break;
8515 case TARGET_NR_recvmmsg:
8516 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
8517 break;
8518#endif
3532fa74
FB
8519#ifdef TARGET_NR_sendto
8520 case TARGET_NR_sendto:
1be9e1dc 8521 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
8522 break;
8523#endif
8524#ifdef TARGET_NR_shutdown
8525 case TARGET_NR_shutdown:
1be9e1dc 8526 ret = get_errno(shutdown(arg1, arg2));
3532fa74
FB
8527 break;
8528#endif
f894efd1
LV
8529#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
8530 case TARGET_NR_getrandom:
8531 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
8532 if (!p) {
8533 goto efault;
8534 }
8535 ret = get_errno(getrandom(p, arg2, arg3));
8536 unlock_user(p, arg1, ret);
8537 break;
8538#endif
3532fa74
FB
8539#ifdef TARGET_NR_socket
8540 case TARGET_NR_socket:
8541 ret = do_socket(arg1, arg2, arg3);
e36800c9 8542 fd_trans_unregister(ret);
3532fa74
FB
8543 break;
8544#endif
8545#ifdef TARGET_NR_socketpair
8546 case TARGET_NR_socketpair:
1be9e1dc 8547 ret = do_socketpair(arg1, arg2, arg3, arg4);
3532fa74
FB
8548 break;
8549#endif
8550#ifdef TARGET_NR_setsockopt
8551 case TARGET_NR_setsockopt:
8552 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
8553 break;
8554#endif
7494b0f9 8555
31e31b8a 8556 case TARGET_NR_syslog:
579a97f7
FB
8557 if (!(p = lock_user_string(arg2)))
8558 goto efault;
e5574487
TS
8559 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
8560 unlock_user(p, arg2, 0);
7494b0f9
TS
8561 break;
8562
31e31b8a 8563 case TARGET_NR_setitimer:
66fb9763 8564 {
66fb9763
FB
8565 struct itimerval value, ovalue, *pvalue;
8566
53a5960a 8567 if (arg2) {
66fb9763 8568 pvalue = &value;
788f5ec4
TS
8569 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
8570 || copy_from_user_timeval(&pvalue->it_value,
8571 arg2 + sizeof(struct target_timeval)))
8572 goto efault;
66fb9763
FB
8573 } else {
8574 pvalue = NULL;
8575 }
8576 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
53a5960a 8577 if (!is_error(ret) && arg3) {
788f5ec4
TS
8578 if (copy_to_user_timeval(arg3,
8579 &ovalue.it_interval)
8580 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
8581 &ovalue.it_value))
8582 goto efault;
66fb9763
FB
8583 }
8584 }
8585 break;
31e31b8a 8586 case TARGET_NR_getitimer:
66fb9763 8587 {
66fb9763 8588 struct itimerval value;
3b46e624 8589
66fb9763 8590 ret = get_errno(getitimer(arg1, &value));
53a5960a 8591 if (!is_error(ret) && arg2) {
788f5ec4
TS
8592 if (copy_to_user_timeval(arg2,
8593 &value.it_interval)
8594 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
8595 &value.it_value))
8596 goto efault;
66fb9763
FB
8597 }
8598 }
8599 break;
704eff6c 8600#ifdef TARGET_NR_stat
31e31b8a 8601 case TARGET_NR_stat:
579a97f7
FB
8602 if (!(p = lock_user_string(arg1)))
8603 goto efault;
53a5960a
PB
8604 ret = get_errno(stat(path(p), &st));
8605 unlock_user(p, arg1, 0);
31e31b8a 8606 goto do_stat;
704eff6c
CG
8607#endif
8608#ifdef TARGET_NR_lstat
31e31b8a 8609 case TARGET_NR_lstat:
579a97f7
FB
8610 if (!(p = lock_user_string(arg1)))
8611 goto efault;
53a5960a
PB
8612 ret = get_errno(lstat(path(p), &st));
8613 unlock_user(p, arg1, 0);
31e31b8a 8614 goto do_stat;
704eff6c 8615#endif
31e31b8a
FB
8616 case TARGET_NR_fstat:
8617 {
8618 ret = get_errno(fstat(arg1, &st));
704eff6c 8619#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
31e31b8a 8620 do_stat:
704eff6c 8621#endif
31e31b8a 8622 if (!is_error(ret)) {
53a5960a 8623 struct target_stat *target_st;
e3584658 8624
579a97f7
FB
8625 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
8626 goto efault;
12727917 8627 memset(target_st, 0, sizeof(*target_st));
d2fd1af7
FB
8628 __put_user(st.st_dev, &target_st->st_dev);
8629 __put_user(st.st_ino, &target_st->st_ino);
8630 __put_user(st.st_mode, &target_st->st_mode);
8631 __put_user(st.st_uid, &target_st->st_uid);
8632 __put_user(st.st_gid, &target_st->st_gid);
8633 __put_user(st.st_nlink, &target_st->st_nlink);
8634 __put_user(st.st_rdev, &target_st->st_rdev);
8635 __put_user(st.st_size, &target_st->st_size);
8636 __put_user(st.st_blksize, &target_st->st_blksize);
8637 __put_user(st.st_blocks, &target_st->st_blocks);
8638 __put_user(st.st_atime, &target_st->target_st_atime);
8639 __put_user(st.st_mtime, &target_st->target_st_mtime);
8640 __put_user(st.st_ctime, &target_st->target_st_ctime);
53a5960a 8641 unlock_user_struct(target_st, arg2, 1);
31e31b8a
FB
8642 }
8643 }
8644 break;
ebc05488 8645#ifdef TARGET_NR_olduname
31e31b8a
FB
8646 case TARGET_NR_olduname:
8647 goto unimplemented;
ebc05488
FB
8648#endif
8649#ifdef TARGET_NR_iopl
31e31b8a
FB
8650 case TARGET_NR_iopl:
8651 goto unimplemented;
ebc05488 8652#endif
31e31b8a
FB
8653 case TARGET_NR_vhangup:
8654 ret = get_errno(vhangup());
8655 break;
ebc05488 8656#ifdef TARGET_NR_idle
31e31b8a
FB
8657 case TARGET_NR_idle:
8658 goto unimplemented;
42ad6ae9
FB
8659#endif
8660#ifdef TARGET_NR_syscall
8661 case TARGET_NR_syscall:
5945cfcb
PM
8662 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
8663 arg6, arg7, arg8, 0);
8664 break;
ebc05488 8665#endif
31e31b8a
FB
8666 case TARGET_NR_wait4:
8667 {
8668 int status;
992f48a0 8669 abi_long status_ptr = arg2;
31e31b8a 8670 struct rusage rusage, *rusage_ptr;
992f48a0 8671 abi_ulong target_rusage = arg4;
a39fb273 8672 abi_long rusage_err;
31e31b8a
FB
8673 if (target_rusage)
8674 rusage_ptr = &rusage;
8675 else
8676 rusage_ptr = NULL;
4af80a37 8677 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
31e31b8a 8678 if (!is_error(ret)) {
5379557b 8679 if (status_ptr && ret) {
1d9d8b55 8680 status = host_to_target_waitstatus(status);
2f619698
FB
8681 if (put_user_s32(status, status_ptr))
8682 goto efault;
31e31b8a 8683 }
a39fb273
PJ
8684 if (target_rusage) {
8685 rusage_err = host_to_target_rusage(target_rusage, &rusage);
8686 if (rusage_err) {
8687 ret = rusage_err;
8688 }
8689 }
31e31b8a
FB
8690 }
8691 }
8692 break;
e5febef5 8693#ifdef TARGET_NR_swapoff
31e31b8a 8694 case TARGET_NR_swapoff:
579a97f7
FB
8695 if (!(p = lock_user_string(arg1)))
8696 goto efault;
53a5960a
PB
8697 ret = get_errno(swapoff(p));
8698 unlock_user(p, arg1, 0);
31e31b8a 8699 break;
e5febef5 8700#endif
31e31b8a 8701 case TARGET_NR_sysinfo:
a5448a7d 8702 {
53a5960a 8703 struct target_sysinfo *target_value;
a5448a7d
FB
8704 struct sysinfo value;
8705 ret = get_errno(sysinfo(&value));
53a5960a 8706 if (!is_error(ret) && arg1)
a5448a7d 8707 {
579a97f7
FB
8708 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
8709 goto efault;
a5448a7d
FB
8710 __put_user(value.uptime, &target_value->uptime);
8711 __put_user(value.loads[0], &target_value->loads[0]);
8712 __put_user(value.loads[1], &target_value->loads[1]);
8713 __put_user(value.loads[2], &target_value->loads[2]);
8714 __put_user(value.totalram, &target_value->totalram);
8715 __put_user(value.freeram, &target_value->freeram);
8716 __put_user(value.sharedram, &target_value->sharedram);
8717 __put_user(value.bufferram, &target_value->bufferram);
8718 __put_user(value.totalswap, &target_value->totalswap);
8719 __put_user(value.freeswap, &target_value->freeswap);
8720 __put_user(value.procs, &target_value->procs);
8721 __put_user(value.totalhigh, &target_value->totalhigh);
8722 __put_user(value.freehigh, &target_value->freehigh);
8723 __put_user(value.mem_unit, &target_value->mem_unit);
53a5960a 8724 unlock_user_struct(target_value, arg1, 1);
a5448a7d
FB
8725 }
8726 }
8727 break;
e5febef5 8728#ifdef TARGET_NR_ipc
31e31b8a 8729 case TARGET_NR_ipc:
8853f86e
FB
8730 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
8731 break;
e5febef5 8732#endif
e5289087
AJ
8733#ifdef TARGET_NR_semget
8734 case TARGET_NR_semget:
8735 ret = get_errno(semget(arg1, arg2, arg3));
8736 break;
8737#endif
8738#ifdef TARGET_NR_semop
8739 case TARGET_NR_semop:
c7128c9f 8740 ret = do_semop(arg1, arg2, arg3);
e5289087
AJ
8741 break;
8742#endif
8743#ifdef TARGET_NR_semctl
8744 case TARGET_NR_semctl:
d1c002b6 8745 ret = do_semctl(arg1, arg2, arg3, arg4);
e5289087
AJ
8746 break;
8747#endif
eeb438c1
AJ
8748#ifdef TARGET_NR_msgctl
8749 case TARGET_NR_msgctl:
8750 ret = do_msgctl(arg1, arg2, arg3);
8751 break;
8752#endif
8753#ifdef TARGET_NR_msgget
8754 case TARGET_NR_msgget:
8755 ret = get_errno(msgget(arg1, arg2));
8756 break;
8757#endif
8758#ifdef TARGET_NR_msgrcv
8759 case TARGET_NR_msgrcv:
8760 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
8761 break;
8762#endif
8763#ifdef TARGET_NR_msgsnd
8764 case TARGET_NR_msgsnd:
8765 ret = do_msgsnd(arg1, arg2, arg3, arg4);
8766 break;
88a8c984
RV
8767#endif
8768#ifdef TARGET_NR_shmget
8769 case TARGET_NR_shmget:
8770 ret = get_errno(shmget(arg1, arg2, arg3));
8771 break;
8772#endif
8773#ifdef TARGET_NR_shmctl
8774 case TARGET_NR_shmctl:
8775 ret = do_shmctl(arg1, arg2, arg3);
8776 break;
8777#endif
8778#ifdef TARGET_NR_shmat
8779 case TARGET_NR_shmat:
8780 ret = do_shmat(arg1, arg2, arg3);
8781 break;
8782#endif
8783#ifdef TARGET_NR_shmdt
8784 case TARGET_NR_shmdt:
8785 ret = do_shmdt(arg1);
8786 break;
eeb438c1 8787#endif
31e31b8a
FB
8788 case TARGET_NR_fsync:
8789 ret = get_errno(fsync(arg1));
8790 break;
31e31b8a 8791 case TARGET_NR_clone:
4ce6243d
PM
8792 /* Linux manages to have three different orderings for its
8793 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
8794 * match the kernel's CONFIG_CLONE_* settings.
8795 * Microblaze is further special in that it uses a sixth
8796 * implicit argument to clone for the TLS pointer.
8797 */
8798#if defined(TARGET_MICROBLAZE)
a5b3bdcb 8799 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
4ce6243d
PM
8800#elif defined(TARGET_CLONE_BACKWARDS)
8801 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
8802#elif defined(TARGET_CLONE_BACKWARDS2)
a4c075f1 8803 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
0b6d3ae0 8804#else
4ce6243d 8805 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
0b6d3ae0 8806#endif
1b6b029e 8807 break;
ec86b0fb
FB
8808#ifdef __NR_exit_group
8809 /* new thread calls */
8810 case TARGET_NR_exit_group:
9788c9ca 8811#ifdef TARGET_GPROF
6d946cda
AJ
8812 _mcleanup();
8813#endif
e9009676 8814 gdb_exit(cpu_env, arg1);
ec86b0fb
FB
8815 ret = get_errno(exit_group(arg1));
8816 break;
8817#endif
31e31b8a 8818 case TARGET_NR_setdomainname:
579a97f7
FB
8819 if (!(p = lock_user_string(arg1)))
8820 goto efault;
53a5960a
PB
8821 ret = get_errno(setdomainname(p, arg2));
8822 unlock_user(p, arg1, 0);
31e31b8a
FB
8823 break;
8824 case TARGET_NR_uname:
8825 /* no need to transcode because we use the linux syscall */
29e619b1
FB
8826 {
8827 struct new_utsname * buf;
3b46e624 8828
579a97f7
FB
8829 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
8830 goto efault;
29e619b1
FB
8831 ret = get_errno(sys_uname(buf));
8832 if (!is_error(ret)) {
8833 /* Overrite the native machine name with whatever is being
8834 emulated. */
da79030f 8835 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
c5937220
PB
8836 /* Allow the user to override the reported release. */
8837 if (qemu_uname_release && *qemu_uname_release)
8838 strcpy (buf->release, qemu_uname_release);
29e619b1 8839 }
53a5960a 8840 unlock_user_struct(buf, arg1, 1);
29e619b1 8841 }
31e31b8a 8842 break;
6dbad63e 8843#ifdef TARGET_I386
31e31b8a 8844 case TARGET_NR_modify_ldt:
03acab66 8845 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5cd4393b 8846 break;
84409ddb 8847#if !defined(TARGET_X86_64)
5cd4393b
FB
8848 case TARGET_NR_vm86old:
8849 goto unimplemented;
8850 case TARGET_NR_vm86:
53a5960a 8851 ret = do_vm86(cpu_env, arg1, arg2);
6dbad63e 8852 break;
84409ddb 8853#endif
6dbad63e 8854#endif
31e31b8a
FB
8855 case TARGET_NR_adjtimex:
8856 goto unimplemented;
e5febef5 8857#ifdef TARGET_NR_create_module
31e31b8a 8858 case TARGET_NR_create_module:
e5febef5 8859#endif
31e31b8a
FB
8860 case TARGET_NR_init_module:
8861 case TARGET_NR_delete_module:
e5febef5 8862#ifdef TARGET_NR_get_kernel_syms
31e31b8a 8863 case TARGET_NR_get_kernel_syms:
e5febef5 8864#endif
31e31b8a
FB
8865 goto unimplemented;
8866 case TARGET_NR_quotactl:
8867 goto unimplemented;
8868 case TARGET_NR_getpgid:
8869 ret = get_errno(getpgid(arg1));
8870 break;
8871 case TARGET_NR_fchdir:
8872 ret = get_errno(fchdir(arg1));
8873 break;
84409ddb 8874#ifdef TARGET_NR_bdflush /* not on x86_64 */
31e31b8a
FB
8875 case TARGET_NR_bdflush:
8876 goto unimplemented;
84409ddb 8877#endif
e5febef5 8878#ifdef TARGET_NR_sysfs
31e31b8a
FB
8879 case TARGET_NR_sysfs:
8880 goto unimplemented;
e5febef5 8881#endif
31e31b8a 8882 case TARGET_NR_personality:
1b6b029e 8883 ret = get_errno(personality(arg1));
31e31b8a 8884 break;
e5febef5 8885#ifdef TARGET_NR_afs_syscall
31e31b8a
FB
8886 case TARGET_NR_afs_syscall:
8887 goto unimplemented;
e5febef5 8888#endif
7a3148a9 8889#ifdef TARGET_NR__llseek /* Not on alpha */
31e31b8a
FB
8890 case TARGET_NR__llseek:
8891 {
0c1592d9 8892 int64_t res;
d35b261c 8893#if !defined(__NR_llseek)
0c1592d9
PM
8894 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
8895 if (res == -1) {
8896 ret = get_errno(res);
8897 } else {
8898 ret = 0;
8899 }
4f2ac237 8900#else
31e31b8a 8901 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4f2ac237 8902#endif
0c1592d9
PM
8903 if ((ret == 0) && put_user_s64(res, arg4)) {
8904 goto efault;
8905 }
31e31b8a
FB
8906 }
8907 break;
7a3148a9 8908#endif
704eff6c 8909#ifdef TARGET_NR_getdents
31e31b8a 8910 case TARGET_NR_getdents:
3307e236 8911#ifdef __NR_getdents
d83c8733 8912#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4add45b4 8913 {
53a5960a 8914 struct target_dirent *target_dirp;
6556a833 8915 struct linux_dirent *dirp;
992f48a0 8916 abi_long count = arg3;
4add45b4 8917
0e173b24
HK
8918 dirp = g_try_malloc(count);
8919 if (!dirp) {
579a97f7 8920 ret = -TARGET_ENOMEM;
0da46a6e
TS
8921 goto fail;
8922 }
3b46e624 8923
4add45b4
FB
8924 ret = get_errno(sys_getdents(arg1, dirp, count));
8925 if (!is_error(ret)) {
6556a833 8926 struct linux_dirent *de;
4add45b4
FB
8927 struct target_dirent *tde;
8928 int len = ret;
8929 int reclen, treclen;
8930 int count1, tnamelen;
8931
8932 count1 = 0;
8933 de = dirp;
579a97f7
FB
8934 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8935 goto efault;
4add45b4
FB
8936 tde = target_dirp;
8937 while (len > 0) {
8938 reclen = de->d_reclen;
333858b7
DL
8939 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
8940 assert(tnamelen >= 0);
8941 treclen = tnamelen + offsetof(struct target_dirent, d_name);
8942 assert(count1 + treclen <= count);
4add45b4 8943 tde->d_reclen = tswap16(treclen);
cbb21eed
MB
8944 tde->d_ino = tswapal(de->d_ino);
8945 tde->d_off = tswapal(de->d_off);
333858b7 8946 memcpy(tde->d_name, de->d_name, tnamelen);
6556a833 8947 de = (struct linux_dirent *)((char *)de + reclen);
4add45b4 8948 len -= reclen;
1c5bf3bf 8949 tde = (struct target_dirent *)((char *)tde + treclen);
4add45b4
FB
8950 count1 += treclen;
8951 }
8952 ret = count1;
579a97f7 8953 unlock_user(target_dirp, arg2, ret);
4add45b4 8954 }
0e173b24 8955 g_free(dirp);
4add45b4
FB
8956 }
8957#else
31e31b8a 8958 {
6556a833 8959 struct linux_dirent *dirp;
992f48a0 8960 abi_long count = arg3;
dab2ed99 8961
579a97f7
FB
8962 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
8963 goto efault;
72f03900 8964 ret = get_errno(sys_getdents(arg1, dirp, count));
31e31b8a 8965 if (!is_error(ret)) {
6556a833 8966 struct linux_dirent *de;
31e31b8a
FB
8967 int len = ret;
8968 int reclen;
8969 de = dirp;
8970 while (len > 0) {
8083a3e5 8971 reclen = de->d_reclen;
31e31b8a
FB
8972 if (reclen > len)
8973 break;
8083a3e5 8974 de->d_reclen = tswap16(reclen);
31e31b8a
FB
8975 tswapls(&de->d_ino);
8976 tswapls(&de->d_off);
6556a833 8977 de = (struct linux_dirent *)((char *)de + reclen);
31e31b8a
FB
8978 len -= reclen;
8979 }
8980 }
53a5960a 8981 unlock_user(dirp, arg2, ret);
3307e236
PM
8982 }
8983#endif
8984#else
8985 /* Implement getdents in terms of getdents64 */
8986 {
8987 struct linux_dirent64 *dirp;
8988 abi_long count = arg3;
8989
8990 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8991 if (!dirp) {
8992 goto efault;
8993 }
8994 ret = get_errno(sys_getdents64(arg1, dirp, count));
8995 if (!is_error(ret)) {
8996 /* Convert the dirent64 structs to target dirent. We do this
8997 * in-place, since we can guarantee that a target_dirent is no
8998 * larger than a dirent64; however this means we have to be
8999 * careful to read everything before writing in the new format.
9000 */
9001 struct linux_dirent64 *de;
9002 struct target_dirent *tde;
9003 int len = ret;
9004 int tlen = 0;
9005
9006 de = dirp;
9007 tde = (struct target_dirent *)dirp;
9008 while (len > 0) {
9009 int namelen, treclen;
9010 int reclen = de->d_reclen;
9011 uint64_t ino = de->d_ino;
9012 int64_t off = de->d_off;
9013 uint8_t type = de->d_type;
9014
9015 namelen = strlen(de->d_name);
9016 treclen = offsetof(struct target_dirent, d_name)
9017 + namelen + 2;
9018 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
9019
9020 memmove(tde->d_name, de->d_name, namelen + 1);
9021 tde->d_ino = tswapal(ino);
9022 tde->d_off = tswapal(off);
9023 tde->d_reclen = tswap16(treclen);
9024 /* The target_dirent type is in what was formerly a padding
9025 * byte at the end of the structure:
9026 */
9027 *(((char *)tde) + treclen - 1) = type;
9028
9029 de = (struct linux_dirent64 *)((char *)de + reclen);
9030 tde = (struct target_dirent *)((char *)tde + treclen);
9031 len -= reclen;
9032 tlen += treclen;
9033 }
9034 ret = tlen;
9035 }
9036 unlock_user(dirp, arg2, ret);
31e31b8a 9037 }
4add45b4 9038#endif
31e31b8a 9039 break;
704eff6c 9040#endif /* TARGET_NR_getdents */
3ae43202 9041#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
dab2ed99
FB
9042 case TARGET_NR_getdents64:
9043 {
6556a833 9044 struct linux_dirent64 *dirp;
992f48a0 9045 abi_long count = arg3;
579a97f7
FB
9046 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
9047 goto efault;
dab2ed99
FB
9048 ret = get_errno(sys_getdents64(arg1, dirp, count));
9049 if (!is_error(ret)) {
6556a833 9050 struct linux_dirent64 *de;
dab2ed99
FB
9051 int len = ret;
9052 int reclen;
9053 de = dirp;
9054 while (len > 0) {
8083a3e5 9055 reclen = de->d_reclen;
dab2ed99
FB
9056 if (reclen > len)
9057 break;
8083a3e5 9058 de->d_reclen = tswap16(reclen);
8582a53a
FB
9059 tswap64s((uint64_t *)&de->d_ino);
9060 tswap64s((uint64_t *)&de->d_off);
6556a833 9061 de = (struct linux_dirent64 *)((char *)de + reclen);
dab2ed99
FB
9062 len -= reclen;
9063 }
9064 }
53a5960a 9065 unlock_user(dirp, arg2, ret);
dab2ed99
FB
9066 }
9067 break;
a541f297 9068#endif /* TARGET_NR_getdents64 */
9468a5d4 9069#if defined(TARGET_NR__newselect)
31e31b8a 9070 case TARGET_NR__newselect:
53a5960a 9071 ret = do_select(arg1, arg2, arg3, arg4, arg5);
31e31b8a 9072 break;
e5febef5 9073#endif
d8035d4c
MF
9074#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
9075# ifdef TARGET_NR_poll
9de5e440 9076 case TARGET_NR_poll:
d8035d4c
MF
9077# endif
9078# ifdef TARGET_NR_ppoll
9079 case TARGET_NR_ppoll:
9080# endif
9de5e440 9081 {
53a5960a 9082 struct target_pollfd *target_pfd;
9de5e440 9083 unsigned int nfds = arg2;
9de5e440 9084 struct pollfd *pfd;
7854b056 9085 unsigned int i;
9de5e440 9086
3e24bb3f
LV
9087 pfd = NULL;
9088 target_pfd = NULL;
9089 if (nfds) {
9090 target_pfd = lock_user(VERIFY_WRITE, arg1,
9091 sizeof(struct target_pollfd) * nfds, 1);
9092 if (!target_pfd) {
9093 goto efault;
9094 }
d8035d4c 9095
3e24bb3f
LV
9096 pfd = alloca(sizeof(struct pollfd) * nfds);
9097 for (i = 0; i < nfds; i++) {
9098 pfd[i].fd = tswap32(target_pfd[i].fd);
9099 pfd[i].events = tswap16(target_pfd[i].events);
9100 }
9de5e440 9101 }
d8035d4c 9102
a6130237 9103 switch (num) {
d8035d4c 9104# ifdef TARGET_NR_ppoll
a6130237
PM
9105 case TARGET_NR_ppoll:
9106 {
d8035d4c
MF
9107 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
9108 target_sigset_t *target_set;
9109 sigset_t _set, *set = &_set;
9110
9111 if (arg3) {
9112 if (target_to_host_timespec(timeout_ts, arg3)) {
9113 unlock_user(target_pfd, arg1, 0);
9114 goto efault;
9115 }
9116 } else {
9117 timeout_ts = NULL;
9118 }
9119
9120 if (arg4) {
9121 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
9122 if (!target_set) {
9123 unlock_user(target_pfd, arg1, 0);
9124 goto efault;
9125 }
9126 target_to_host_sigset(set, target_set);
9127 } else {
9128 set = NULL;
9129 }
9130
a6130237
PM
9131 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
9132 set, SIGSET_T_SIZE));
d8035d4c
MF
9133
9134 if (!is_error(ret) && arg3) {
9135 host_to_target_timespec(arg3, timeout_ts);
9136 }
9137 if (arg4) {
9138 unlock_user(target_set, arg4, 0);
9139 }
a6130237
PM
9140 break;
9141 }
d8035d4c 9142# endif
a6130237
PM
9143# ifdef TARGET_NR_poll
9144 case TARGET_NR_poll:
9145 {
9146 struct timespec ts, *pts;
9147
9148 if (arg3 >= 0) {
9149 /* Convert ms to secs, ns */
9150 ts.tv_sec = arg3 / 1000;
9151 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
9152 pts = &ts;
9153 } else {
9154 /* -ve poll() timeout means "infinite" */
9155 pts = NULL;
9156 }
9157 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
9158 break;
9159 }
d8035d4c 9160# endif
a6130237
PM
9161 default:
9162 g_assert_not_reached();
9163 }
d8035d4c 9164
9de5e440
FB
9165 if (!is_error(ret)) {
9166 for(i = 0; i < nfds; i++) {
5cd4393b 9167 target_pfd[i].revents = tswap16(pfd[i].revents);
9de5e440
FB
9168 }
9169 }
30cb4cde 9170 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
9de5e440
FB
9171 }
9172 break;
e5febef5 9173#endif
31e31b8a 9174 case TARGET_NR_flock:
9de5e440
FB
9175 /* NOTE: the flock constant seems to be the same for every
9176 Linux platform */
2a845989 9177 ret = get_errno(safe_flock(arg1, arg2));
31e31b8a
FB
9178 break;
9179 case TARGET_NR_readv:
9180 {
f287b2c2
RH
9181 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
9182 if (vec != NULL) {
918c03ed 9183 ret = get_errno(safe_readv(arg1, vec, arg3));
f287b2c2
RH
9184 unlock_iovec(vec, arg2, arg3, 1);
9185 } else {
9186 ret = -host_to_target_errno(errno);
9187 }
31e31b8a
FB
9188 }
9189 break;
9190 case TARGET_NR_writev:
9191 {
f287b2c2
RH
9192 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
9193 if (vec != NULL) {
918c03ed 9194 ret = get_errno(safe_writev(arg1, vec, arg3));
f287b2c2
RH
9195 unlock_iovec(vec, arg2, arg3, 0);
9196 } else {
9197 ret = -host_to_target_errno(errno);
9198 }
31e31b8a
FB
9199 }
9200 break;
9201 case TARGET_NR_getsid:
9202 ret = get_errno(getsid(arg1));
9203 break;
7a3148a9 9204#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
31e31b8a 9205 case TARGET_NR_fdatasync:
5cd4393b
FB
9206 ret = get_errno(fdatasync(arg1));
9207 break;
7a3148a9 9208#endif
704eff6c 9209#ifdef TARGET_NR__sysctl
31e31b8a 9210 case TARGET_NR__sysctl:
0da46a6e 9211 /* We don't implement this, but ENOTDIR is always a safe
29e619b1 9212 return value. */
0da46a6e
TS
9213 ret = -TARGET_ENOTDIR;
9214 break;
704eff6c 9215#endif
737de1d1
MF
9216 case TARGET_NR_sched_getaffinity:
9217 {
9218 unsigned int mask_size;
9219 unsigned long *mask;
9220
9221 /*
9222 * sched_getaffinity needs multiples of ulong, so need to take
9223 * care of mismatches between target ulong and host ulong sizes.
9224 */
9225 if (arg2 & (sizeof(abi_ulong) - 1)) {
9226 ret = -TARGET_EINVAL;
9227 break;
9228 }
9229 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
9230
9231 mask = alloca(mask_size);
9232 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
9233
9234 if (!is_error(ret)) {
be3bd286
PM
9235 if (ret > arg2) {
9236 /* More data returned than the caller's buffer will fit.
9237 * This only happens if sizeof(abi_long) < sizeof(long)
9238 * and the caller passed us a buffer holding an odd number
9239 * of abi_longs. If the host kernel is actually using the
9240 * extra 4 bytes then fail EINVAL; otherwise we can just
9241 * ignore them and only copy the interesting part.
9242 */
9243 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
9244 if (numcpus > arg2 * 8) {
9245 ret = -TARGET_EINVAL;
9246 break;
9247 }
9248 ret = arg2;
9249 }
9250
cd18f05e 9251 if (copy_to_user(arg3, mask, ret)) {
737de1d1
MF
9252 goto efault;
9253 }
737de1d1
MF
9254 }
9255 }
9256 break;
9257 case TARGET_NR_sched_setaffinity:
9258 {
9259 unsigned int mask_size;
9260 unsigned long *mask;
9261
9262 /*
9263 * sched_setaffinity needs multiples of ulong, so need to take
9264 * care of mismatches between target ulong and host ulong sizes.
9265 */
9266 if (arg2 & (sizeof(abi_ulong) - 1)) {
9267 ret = -TARGET_EINVAL;
9268 break;
9269 }
9270 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
9271
9272 mask = alloca(mask_size);
9273 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
9274 goto efault;
9275 }
9276 memcpy(mask, p, arg2);
9277 unlock_user_struct(p, arg2, 0);
9278
9279 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
9280 }
9281 break;
31e31b8a 9282 case TARGET_NR_sched_setparam:
5cd4393b 9283 {
53a5960a 9284 struct sched_param *target_schp;
5cd4393b 9285 struct sched_param schp;
53a5960a 9286
a1d5c5b2
TM
9287 if (arg2 == 0) {
9288 return -TARGET_EINVAL;
9289 }
579a97f7
FB
9290 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
9291 goto efault;
5cd4393b 9292 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 9293 unlock_user_struct(target_schp, arg2, 0);
5cd4393b
FB
9294 ret = get_errno(sched_setparam(arg1, &schp));
9295 }
9296 break;
31e31b8a 9297 case TARGET_NR_sched_getparam:
5cd4393b 9298 {
53a5960a 9299 struct sched_param *target_schp;
5cd4393b 9300 struct sched_param schp;
a1d5c5b2
TM
9301
9302 if (arg2 == 0) {
9303 return -TARGET_EINVAL;
9304 }
5cd4393b
FB
9305 ret = get_errno(sched_getparam(arg1, &schp));
9306 if (!is_error(ret)) {
579a97f7
FB
9307 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
9308 goto efault;
5cd4393b 9309 target_schp->sched_priority = tswap32(schp.sched_priority);
53a5960a 9310 unlock_user_struct(target_schp, arg2, 1);
5cd4393b
FB
9311 }
9312 }
9313 break;
31e31b8a 9314 case TARGET_NR_sched_setscheduler:
5cd4393b 9315 {
53a5960a 9316 struct sched_param *target_schp;
5cd4393b 9317 struct sched_param schp;
a1d5c5b2
TM
9318 if (arg3 == 0) {
9319 return -TARGET_EINVAL;
9320 }
579a97f7
FB
9321 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
9322 goto efault;
5cd4393b 9323 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 9324 unlock_user_struct(target_schp, arg3, 0);
5cd4393b
FB
9325 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
9326 }
9327 break;
31e31b8a 9328 case TARGET_NR_sched_getscheduler:
5cd4393b
FB
9329 ret = get_errno(sched_getscheduler(arg1));
9330 break;
31e31b8a
FB
9331 case TARGET_NR_sched_yield:
9332 ret = get_errno(sched_yield());
9333 break;
9334 case TARGET_NR_sched_get_priority_max:
5cd4393b
FB
9335 ret = get_errno(sched_get_priority_max(arg1));
9336 break;
31e31b8a 9337 case TARGET_NR_sched_get_priority_min:
5cd4393b
FB
9338 ret = get_errno(sched_get_priority_min(arg1));
9339 break;
31e31b8a 9340 case TARGET_NR_sched_rr_get_interval:
5cd4393b 9341 {
5cd4393b
FB
9342 struct timespec ts;
9343 ret = get_errno(sched_rr_get_interval(arg1, &ts));
9344 if (!is_error(ret)) {
d4290c40 9345 ret = host_to_target_timespec(arg2, &ts);
5cd4393b
FB
9346 }
9347 }
9348 break;
31e31b8a 9349 case TARGET_NR_nanosleep:
1b6b029e 9350 {
1b6b029e 9351 struct timespec req, rem;
53a5960a 9352 target_to_host_timespec(&req, arg1);
9e518226 9353 ret = get_errno(safe_nanosleep(&req, &rem));
53a5960a
PB
9354 if (is_error(ret) && arg2) {
9355 host_to_target_timespec(arg2, &rem);
1b6b029e
FB
9356 }
9357 }
9358 break;
e5febef5 9359#ifdef TARGET_NR_query_module
31e31b8a 9360 case TARGET_NR_query_module:
5cd4393b 9361 goto unimplemented;
e5febef5
TS
9362#endif
9363#ifdef TARGET_NR_nfsservctl
31e31b8a 9364 case TARGET_NR_nfsservctl:
5cd4393b 9365 goto unimplemented;
e5febef5 9366#endif
31e31b8a 9367 case TARGET_NR_prctl:
1e6722f8
PM
9368 switch (arg1) {
9369 case PR_GET_PDEATHSIG:
9370 {
9371 int deathsig;
9372 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
9373 if (!is_error(ret) && arg2
9374 && put_user_ual(deathsig, arg2)) {
9375 goto efault;
e5574487 9376 }
1e6722f8
PM
9377 break;
9378 }
db9526b1
PM
9379#ifdef PR_GET_NAME
9380 case PR_GET_NAME:
9381 {
9382 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
9383 if (!name) {
9384 goto efault;
9385 }
9386 ret = get_errno(prctl(arg1, (unsigned long)name,
9387 arg3, arg4, arg5));
9388 unlock_user(name, arg2, 16);
9389 break;
9390 }
9391 case PR_SET_NAME:
9392 {
9393 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
9394 if (!name) {
9395 goto efault;
9396 }
9397 ret = get_errno(prctl(arg1, (unsigned long)name,
9398 arg3, arg4, arg5));
9399 unlock_user(name, arg2, 0);
9400 break;
9401 }
9402#endif
1e6722f8
PM
9403 default:
9404 /* Most prctl options have no pointer arguments */
9405 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
9406 break;
9407 }
39b9aae1 9408 break;
d2fd1af7
FB
9409#ifdef TARGET_NR_arch_prctl
9410 case TARGET_NR_arch_prctl:
9411#if defined(TARGET_I386) && !defined(TARGET_ABI32)
9412 ret = do_arch_prctl(cpu_env, arg1, arg2);
9413 break;
9414#else
9415 goto unimplemented;
9416#endif
9417#endif
f2c7ba15
AJ
9418#ifdef TARGET_NR_pread64
9419 case TARGET_NR_pread64:
ae017a5b
AG
9420 if (regpairs_aligned(cpu_env)) {
9421 arg4 = arg5;
9422 arg5 = arg6;
9423 }
f2c7ba15
AJ
9424 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
9425 goto efault;
9426 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
9427 unlock_user(p, arg2, ret);
9428 break;
9429 case TARGET_NR_pwrite64:
ae017a5b
AG
9430 if (regpairs_aligned(cpu_env)) {
9431 arg4 = arg5;
9432 arg5 = arg6;
9433 }
f2c7ba15
AJ
9434 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
9435 goto efault;
9436 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
9437 unlock_user(p, arg2, 0);
9438 break;
67867308 9439#endif
31e31b8a 9440 case TARGET_NR_getcwd:
579a97f7
FB
9441 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
9442 goto efault;
53a5960a
PB
9443 ret = get_errno(sys_getcwd1(p, arg2));
9444 unlock_user(p, arg1, ret);
31e31b8a
FB
9445 break;
9446 case TARGET_NR_capget:
9447 case TARGET_NR_capset:
e0eb210e
PM
9448 {
9449 struct target_user_cap_header *target_header;
9450 struct target_user_cap_data *target_data = NULL;
9451 struct __user_cap_header_struct header;
9452 struct __user_cap_data_struct data[2];
9453 struct __user_cap_data_struct *dataptr = NULL;
9454 int i, target_datalen;
9455 int data_items = 1;
9456
9457 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
9458 goto efault;
9459 }
9460 header.version = tswap32(target_header->version);
9461 header.pid = tswap32(target_header->pid);
9462
ec864874 9463 if (header.version != _LINUX_CAPABILITY_VERSION) {
e0eb210e
PM
9464 /* Version 2 and up takes pointer to two user_data structs */
9465 data_items = 2;
9466 }
9467
9468 target_datalen = sizeof(*target_data) * data_items;
9469
9470 if (arg2) {
9471 if (num == TARGET_NR_capget) {
9472 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
9473 } else {
9474 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
9475 }
9476 if (!target_data) {
9477 unlock_user_struct(target_header, arg1, 0);
9478 goto efault;
9479 }
9480
9481 if (num == TARGET_NR_capset) {
9482 for (i = 0; i < data_items; i++) {
9483 data[i].effective = tswap32(target_data[i].effective);
9484 data[i].permitted = tswap32(target_data[i].permitted);
9485 data[i].inheritable = tswap32(target_data[i].inheritable);
9486 }
9487 }
9488
9489 dataptr = data;
9490 }
9491
9492 if (num == TARGET_NR_capget) {
9493 ret = get_errno(capget(&header, dataptr));
9494 } else {
9495 ret = get_errno(capset(&header, dataptr));
9496 }
9497
9498 /* The kernel always updates version for both capget and capset */
9499 target_header->version = tswap32(header.version);
9500 unlock_user_struct(target_header, arg1, 1);
9501
9502 if (arg2) {
9503 if (num == TARGET_NR_capget) {
9504 for (i = 0; i < data_items; i++) {
9505 target_data[i].effective = tswap32(data[i].effective);
9506 target_data[i].permitted = tswap32(data[i].permitted);
9507 target_data[i].inheritable = tswap32(data[i].inheritable);
9508 }
9509 unlock_user(target_data, arg2, target_datalen);
9510 } else {
9511 unlock_user(target_data, arg2, 0);
9512 }
9513 }
9514 break;
9515 }
31e31b8a 9516 case TARGET_NR_sigaltstack:
9349b4f9 9517 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
a04e134a 9518 break;
a8fd1aba
PM
9519
9520#ifdef CONFIG_SENDFILE
9521 case TARGET_NR_sendfile:
9522 {
9523 off_t *offp = NULL;
9524 off_t off;
9525 if (arg3) {
9526 ret = get_user_sal(off, arg3);
9527 if (is_error(ret)) {
9528 break;
9529 }
9530 offp = &off;
9531 }
9532 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
9533 if (!is_error(ret) && arg3) {
9534 abi_long ret2 = put_user_sal(off, arg3);
9535 if (is_error(ret2)) {
9536 ret = ret2;
9537 }
9538 }
9539 break;
9540 }
9541#ifdef TARGET_NR_sendfile64
9542 case TARGET_NR_sendfile64:
9543 {
9544 off_t *offp = NULL;
9545 off_t off;
9546 if (arg3) {
9547 ret = get_user_s64(off, arg3);
9548 if (is_error(ret)) {
9549 break;
9550 }
9551 offp = &off;
9552 }
9553 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
9554 if (!is_error(ret) && arg3) {
9555 abi_long ret2 = put_user_s64(off, arg3);
9556 if (is_error(ret2)) {
9557 ret = ret2;
9558 }
9559 }
9560 break;
9561 }
9562#endif
9563#else
31e31b8a 9564 case TARGET_NR_sendfile:
7edd2cf1 9565#ifdef TARGET_NR_sendfile64
a8fd1aba
PM
9566 case TARGET_NR_sendfile64:
9567#endif
5cd4393b 9568 goto unimplemented;
a8fd1aba
PM
9569#endif
9570
ebc05488 9571#ifdef TARGET_NR_getpmsg
31e31b8a 9572 case TARGET_NR_getpmsg:
5cd4393b 9573 goto unimplemented;
ebc05488
FB
9574#endif
9575#ifdef TARGET_NR_putpmsg
31e31b8a 9576 case TARGET_NR_putpmsg:
5cd4393b 9577 goto unimplemented;
ebc05488 9578#endif
048f6b4d 9579#ifdef TARGET_NR_vfork
31e31b8a 9580 case TARGET_NR_vfork:
d865bab5
PB
9581 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
9582 0, 0, 0, 0));
31e31b8a 9583 break;
048f6b4d 9584#endif
ebc05488 9585#ifdef TARGET_NR_ugetrlimit
31e31b8a 9586 case TARGET_NR_ugetrlimit:
728584be
FB
9587 {
9588 struct rlimit rlim;
e22b7015
WT
9589 int resource = target_to_host_resource(arg1);
9590 ret = get_errno(getrlimit(resource, &rlim));
728584be 9591 if (!is_error(ret)) {
53a5960a 9592 struct target_rlimit *target_rlim;
579a97f7
FB
9593 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
9594 goto efault;
81bbe906
TY
9595 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
9596 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 9597 unlock_user_struct(target_rlim, arg2, 1);
728584be
FB
9598 }
9599 break;
9600 }
ebc05488 9601#endif
a315a145 9602#ifdef TARGET_NR_truncate64
31e31b8a 9603 case TARGET_NR_truncate64:
579a97f7
FB
9604 if (!(p = lock_user_string(arg1)))
9605 goto efault;
53a5960a
PB
9606 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
9607 unlock_user(p, arg1, 0);
667f38b1 9608 break;
a315a145
FB
9609#endif
9610#ifdef TARGET_NR_ftruncate64
31e31b8a 9611 case TARGET_NR_ftruncate64:
ce4defa0 9612 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
667f38b1 9613 break;
a315a145
FB
9614#endif
9615#ifdef TARGET_NR_stat64
31e31b8a 9616 case TARGET_NR_stat64:
579a97f7
FB
9617 if (!(p = lock_user_string(arg1)))
9618 goto efault;
53a5960a
PB
9619 ret = get_errno(stat(path(p), &st));
9620 unlock_user(p, arg1, 0);
6a24a778
AZ
9621 if (!is_error(ret))
9622 ret = host_to_target_stat64(cpu_env, arg2, &st);
9623 break;
a315a145
FB
9624#endif
9625#ifdef TARGET_NR_lstat64
31e31b8a 9626 case TARGET_NR_lstat64:
579a97f7
FB
9627 if (!(p = lock_user_string(arg1)))
9628 goto efault;
53a5960a
PB
9629 ret = get_errno(lstat(path(p), &st));
9630 unlock_user(p, arg1, 0);
6a24a778
AZ
9631 if (!is_error(ret))
9632 ret = host_to_target_stat64(cpu_env, arg2, &st);
9633 break;
a315a145
FB
9634#endif
9635#ifdef TARGET_NR_fstat64
31e31b8a 9636 case TARGET_NR_fstat64:
6a24a778
AZ
9637 ret = get_errno(fstat(arg1, &st));
9638 if (!is_error(ret))
9639 ret = host_to_target_stat64(cpu_env, arg2, &st);
9640 break;
ce4defa0 9641#endif
c0d472b1 9642#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
9d33b76b 9643#ifdef TARGET_NR_fstatat64
6a24a778 9644 case TARGET_NR_fstatat64:
9d33b76b
AJ
9645#endif
9646#ifdef TARGET_NR_newfstatat
9647 case TARGET_NR_newfstatat:
9648#endif
6a24a778
AZ
9649 if (!(p = lock_user_string(arg2)))
9650 goto efault;
c0d472b1 9651 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
6a24a778
AZ
9652 if (!is_error(ret))
9653 ret = host_to_target_stat64(cpu_env, arg3, &st);
60cd49d5 9654 break;
a315a145 9655#endif
704eff6c 9656#ifdef TARGET_NR_lchown
67867308 9657 case TARGET_NR_lchown:
579a97f7
FB
9658 if (!(p = lock_user_string(arg1)))
9659 goto efault;
53a5960a
PB
9660 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
9661 unlock_user(p, arg1, 0);
67867308 9662 break;
704eff6c 9663#endif
0c866a7e 9664#ifdef TARGET_NR_getuid
67867308
FB
9665 case TARGET_NR_getuid:
9666 ret = get_errno(high2lowuid(getuid()));
9667 break;
0c866a7e
RV
9668#endif
9669#ifdef TARGET_NR_getgid
67867308
FB
9670 case TARGET_NR_getgid:
9671 ret = get_errno(high2lowgid(getgid()));
9672 break;
0c866a7e
RV
9673#endif
9674#ifdef TARGET_NR_geteuid
67867308
FB
9675 case TARGET_NR_geteuid:
9676 ret = get_errno(high2lowuid(geteuid()));
9677 break;
0c866a7e
RV
9678#endif
9679#ifdef TARGET_NR_getegid
67867308
FB
9680 case TARGET_NR_getegid:
9681 ret = get_errno(high2lowgid(getegid()));
9682 break;
0c866a7e 9683#endif
67867308
FB
9684 case TARGET_NR_setreuid:
9685 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
9686 break;
9687 case TARGET_NR_setregid:
9688 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
9689 break;
9690 case TARGET_NR_getgroups:
9691 {
9692 int gidsetsize = arg1;
0c866a7e 9693 target_id *target_grouplist;
67867308
FB
9694 gid_t *grouplist;
9695 int i;
9696
9697 grouplist = alloca(gidsetsize * sizeof(gid_t));
9698 ret = get_errno(getgroups(gidsetsize, grouplist));
cb3bc233
AZ
9699 if (gidsetsize == 0)
9700 break;
67867308 9701 if (!is_error(ret)) {
03903ffc 9702 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
579a97f7
FB
9703 if (!target_grouplist)
9704 goto efault;
a2155fcc 9705 for(i = 0;i < ret; i++)
0c866a7e 9706 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
03903ffc 9707 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
67867308
FB
9708 }
9709 }
9710 break;
9711 case TARGET_NR_setgroups:
9712 {
9713 int gidsetsize = arg1;
0c866a7e 9714 target_id *target_grouplist;
f2b79ce9 9715 gid_t *grouplist = NULL;
67867308 9716 int i;
f2b79ce9
DA
9717 if (gidsetsize) {
9718 grouplist = alloca(gidsetsize * sizeof(gid_t));
03903ffc 9719 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
f2b79ce9
DA
9720 if (!target_grouplist) {
9721 ret = -TARGET_EFAULT;
9722 goto fail;
9723 }
9724 for (i = 0; i < gidsetsize; i++) {
9725 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
9726 }
9727 unlock_user(target_grouplist, arg2, 0);
579a97f7 9728 }
67867308
FB
9729 ret = get_errno(setgroups(gidsetsize, grouplist));
9730 }
9731 break;
9732 case TARGET_NR_fchown:
9733 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
9734 break;
c0d472b1 9735#if defined(TARGET_NR_fchownat)
ccfa72b7 9736 case TARGET_NR_fchownat:
579a97f7
FB
9737 if (!(p = lock_user_string(arg2)))
9738 goto efault;
c0d472b1
PM
9739 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
9740 low2highgid(arg4), arg5));
579a97f7 9741 unlock_user(p, arg2, 0);
ccfa72b7
TS
9742 break;
9743#endif
67867308
FB
9744#ifdef TARGET_NR_setresuid
9745 case TARGET_NR_setresuid:
fd6f7798
PM
9746 ret = get_errno(sys_setresuid(low2highuid(arg1),
9747 low2highuid(arg2),
9748 low2highuid(arg3)));
67867308
FB
9749 break;
9750#endif
9751#ifdef TARGET_NR_getresuid
9752 case TARGET_NR_getresuid:
9753 {
53a5960a 9754 uid_t ruid, euid, suid;
67867308
FB
9755 ret = get_errno(getresuid(&ruid, &euid, &suid));
9756 if (!is_error(ret)) {
76ca310a
PM
9757 if (put_user_id(high2lowuid(ruid), arg1)
9758 || put_user_id(high2lowuid(euid), arg2)
9759 || put_user_id(high2lowuid(suid), arg3))
2f619698 9760 goto efault;
67867308
FB
9761 }
9762 }
9763 break;
9764#endif
9765#ifdef TARGET_NR_getresgid
9766 case TARGET_NR_setresgid:
fd6f7798
PM
9767 ret = get_errno(sys_setresgid(low2highgid(arg1),
9768 low2highgid(arg2),
9769 low2highgid(arg3)));
67867308
FB
9770 break;
9771#endif
9772#ifdef TARGET_NR_getresgid
9773 case TARGET_NR_getresgid:
9774 {
53a5960a 9775 gid_t rgid, egid, sgid;
67867308
FB
9776 ret = get_errno(getresgid(&rgid, &egid, &sgid));
9777 if (!is_error(ret)) {
76ca310a
PM
9778 if (put_user_id(high2lowgid(rgid), arg1)
9779 || put_user_id(high2lowgid(egid), arg2)
9780 || put_user_id(high2lowgid(sgid), arg3))
2f619698 9781 goto efault;
67867308
FB
9782 }
9783 }
9784 break;
9785#endif
704eff6c 9786#ifdef TARGET_NR_chown
67867308 9787 case TARGET_NR_chown:
579a97f7
FB
9788 if (!(p = lock_user_string(arg1)))
9789 goto efault;
53a5960a
PB
9790 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
9791 unlock_user(p, arg1, 0);
67867308 9792 break;
704eff6c 9793#endif
67867308 9794 case TARGET_NR_setuid:
fd6f7798 9795 ret = get_errno(sys_setuid(low2highuid(arg1)));
67867308
FB
9796 break;
9797 case TARGET_NR_setgid:
fd6f7798 9798 ret = get_errno(sys_setgid(low2highgid(arg1)));
67867308
FB
9799 break;
9800 case TARGET_NR_setfsuid:
9801 ret = get_errno(setfsuid(arg1));
9802 break;
9803 case TARGET_NR_setfsgid:
9804 ret = get_errno(setfsgid(arg1));
9805 break;
67867308 9806
a315a145 9807#ifdef TARGET_NR_lchown32
31e31b8a 9808 case TARGET_NR_lchown32:
579a97f7
FB
9809 if (!(p = lock_user_string(arg1)))
9810 goto efault;
53a5960a
PB
9811 ret = get_errno(lchown(p, arg2, arg3));
9812 unlock_user(p, arg1, 0);
b03c60f3 9813 break;
a315a145
FB
9814#endif
9815#ifdef TARGET_NR_getuid32
31e31b8a 9816 case TARGET_NR_getuid32:
b03c60f3
FB
9817 ret = get_errno(getuid());
9818 break;
a315a145 9819#endif
64b4d28c
AJ
9820
9821#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
9822 /* Alpha specific */
9823 case TARGET_NR_getxuid:
ba0e276d
RH
9824 {
9825 uid_t euid;
9826 euid=geteuid();
9827 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
9828 }
64b4d28c
AJ
9829 ret = get_errno(getuid());
9830 break;
9831#endif
9832#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
9833 /* Alpha specific */
9834 case TARGET_NR_getxgid:
ba0e276d
RH
9835 {
9836 uid_t egid;
9837 egid=getegid();
9838 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
9839 }
64b4d28c
AJ
9840 ret = get_errno(getgid());
9841 break;
9842#endif
ba0e276d
RH
9843#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
9844 /* Alpha specific */
9845 case TARGET_NR_osf_getsysinfo:
9846 ret = -TARGET_EOPNOTSUPP;
9847 switch (arg1) {
9848 case TARGET_GSI_IEEE_FP_CONTROL:
9849 {
9850 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
9851
9852 /* Copied from linux ieee_fpcr_to_swcr. */
9853 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
9854 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
9855 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
9856 | SWCR_TRAP_ENABLE_DZE
9857 | SWCR_TRAP_ENABLE_OVF);
9858 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
9859 | SWCR_TRAP_ENABLE_INE);
9860 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
9861 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
9862
9863 if (put_user_u64 (swcr, arg2))
9864 goto efault;
9865 ret = 0;
9866 }
9867 break;
9868
9869 /* case GSI_IEEE_STATE_AT_SIGNAL:
9870 -- Not implemented in linux kernel.
9871 case GSI_UACPROC:
9872 -- Retrieves current unaligned access state; not much used.
9873 case GSI_PROC_TYPE:
9874 -- Retrieves implver information; surely not used.
9875 case GSI_GET_HWRPB:
9876 -- Grabs a copy of the HWRPB; surely not used.
9877 */
9878 }
9879 break;
9880#endif
9881#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
9882 /* Alpha specific */
9883 case TARGET_NR_osf_setsysinfo:
9884 ret = -TARGET_EOPNOTSUPP;
9885 switch (arg1) {
9886 case TARGET_SSI_IEEE_FP_CONTROL:
ba0e276d
RH
9887 {
9888 uint64_t swcr, fpcr, orig_fpcr;
9889
6e06d515 9890 if (get_user_u64 (swcr, arg2)) {
ba0e276d 9891 goto efault;
6e06d515
RH
9892 }
9893 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
ba0e276d
RH
9894 fpcr = orig_fpcr & FPCR_DYN_MASK;
9895
9896 /* Copied from linux ieee_swcr_to_fpcr. */
9897 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
9898 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
9899 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
9900 | SWCR_TRAP_ENABLE_DZE
9901 | SWCR_TRAP_ENABLE_OVF)) << 48;
9902 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
9903 | SWCR_TRAP_ENABLE_INE)) << 57;
9904 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
9905 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
9906
6e06d515 9907 cpu_alpha_store_fpcr(cpu_env, fpcr);
ba0e276d 9908 ret = 0;
6e06d515
RH
9909 }
9910 break;
9911
9912 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
9913 {
9914 uint64_t exc, fpcr, orig_fpcr;
9915 int si_code;
9916
9917 if (get_user_u64(exc, arg2)) {
9918 goto efault;
9919 }
ba0e276d 9920
6e06d515 9921 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
ba0e276d 9922
6e06d515
RH
9923 /* We only add to the exception status here. */
9924 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
9925
9926 cpu_alpha_store_fpcr(cpu_env, fpcr);
9927 ret = 0;
9928
9929 /* Old exceptions are not signaled. */
9930 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
9931
9932 /* If any exceptions set by this call,
9933 and are unmasked, send a signal. */
9934 si_code = 0;
9935 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
9936 si_code = TARGET_FPE_FLTRES;
9937 }
9938 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
9939 si_code = TARGET_FPE_FLTUND;
9940 }
9941 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
9942 si_code = TARGET_FPE_FLTOVF;
9943 }
9944 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
9945 si_code = TARGET_FPE_FLTDIV;
9946 }
9947 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
9948 si_code = TARGET_FPE_FLTINV;
9949 }
9950 if (si_code != 0) {
9951 target_siginfo_t info;
9952 info.si_signo = SIGFPE;
9953 info.si_errno = 0;
9954 info.si_code = si_code;
9955 info._sifields._sigfault._addr
9956 = ((CPUArchState *)cpu_env)->pc;
9957 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
ba0e276d
RH
9958 }
9959 }
9960 break;
9961
9962 /* case SSI_NVPAIRS:
9963 -- Used with SSIN_UACPROC to enable unaligned accesses.
9964 case SSI_IEEE_STATE_AT_SIGNAL:
9965 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
9966 -- Not implemented in linux kernel
9967 */
9968 }
9969 break;
9970#endif
9971#ifdef TARGET_NR_osf_sigprocmask
9972 /* Alpha specific. */
9973 case TARGET_NR_osf_sigprocmask:
9974 {
9975 abi_ulong mask;
bc088ba1 9976 int how;
ba0e276d
RH
9977 sigset_t set, oldset;
9978
9979 switch(arg1) {
9980 case TARGET_SIG_BLOCK:
9981 how = SIG_BLOCK;
9982 break;
9983 case TARGET_SIG_UNBLOCK:
9984 how = SIG_UNBLOCK;
9985 break;
9986 case TARGET_SIG_SETMASK:
9987 how = SIG_SETMASK;
9988 break;
9989 default:
9990 ret = -TARGET_EINVAL;
9991 goto fail;
9992 }
9993 mask = arg2;
9994 target_to_host_old_sigset(&set, &mask);
3d3efba0
PM
9995 ret = do_sigprocmask(how, &set, &oldset);
9996 if (!ret) {
9997 host_to_target_old_sigset(&mask, &oldset);
9998 ret = mask;
9999 }
ba0e276d
RH
10000 }
10001 break;
10002#endif
64b4d28c 10003
a315a145 10004#ifdef TARGET_NR_getgid32
31e31b8a 10005 case TARGET_NR_getgid32:
b03c60f3
FB
10006 ret = get_errno(getgid());
10007 break;
a315a145
FB
10008#endif
10009#ifdef TARGET_NR_geteuid32
31e31b8a 10010 case TARGET_NR_geteuid32:
b03c60f3
FB
10011 ret = get_errno(geteuid());
10012 break;
a315a145
FB
10013#endif
10014#ifdef TARGET_NR_getegid32
31e31b8a 10015 case TARGET_NR_getegid32:
b03c60f3
FB
10016 ret = get_errno(getegid());
10017 break;
a315a145
FB
10018#endif
10019#ifdef TARGET_NR_setreuid32
31e31b8a 10020 case TARGET_NR_setreuid32:
b03c60f3
FB
10021 ret = get_errno(setreuid(arg1, arg2));
10022 break;
a315a145
FB
10023#endif
10024#ifdef TARGET_NR_setregid32
31e31b8a 10025 case TARGET_NR_setregid32:
b03c60f3
FB
10026 ret = get_errno(setregid(arg1, arg2));
10027 break;
a315a145
FB
10028#endif
10029#ifdef TARGET_NR_getgroups32
31e31b8a 10030 case TARGET_NR_getgroups32:
99c475ab
FB
10031 {
10032 int gidsetsize = arg1;
53a5960a 10033 uint32_t *target_grouplist;
99c475ab
FB
10034 gid_t *grouplist;
10035 int i;
10036
10037 grouplist = alloca(gidsetsize * sizeof(gid_t));
10038 ret = get_errno(getgroups(gidsetsize, grouplist));
cb3bc233
AZ
10039 if (gidsetsize == 0)
10040 break;
99c475ab 10041 if (!is_error(ret)) {
579a97f7
FB
10042 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
10043 if (!target_grouplist) {
10044 ret = -TARGET_EFAULT;
10045 goto fail;
10046 }
a2155fcc 10047 for(i = 0;i < ret; i++)
53a5960a
PB
10048 target_grouplist[i] = tswap32(grouplist[i]);
10049 unlock_user(target_grouplist, arg2, gidsetsize * 4);
99c475ab
FB
10050 }
10051 }
10052 break;
a315a145
FB
10053#endif
10054#ifdef TARGET_NR_setgroups32
31e31b8a 10055 case TARGET_NR_setgroups32:
99c475ab
FB
10056 {
10057 int gidsetsize = arg1;
53a5960a 10058 uint32_t *target_grouplist;
99c475ab
FB
10059 gid_t *grouplist;
10060 int i;
3b46e624 10061
99c475ab 10062 grouplist = alloca(gidsetsize * sizeof(gid_t));
579a97f7
FB
10063 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
10064 if (!target_grouplist) {
10065 ret = -TARGET_EFAULT;
10066 goto fail;
10067 }
99c475ab 10068 for(i = 0;i < gidsetsize; i++)
53a5960a
PB
10069 grouplist[i] = tswap32(target_grouplist[i]);
10070 unlock_user(target_grouplist, arg2, 0);
99c475ab
FB
10071 ret = get_errno(setgroups(gidsetsize, grouplist));
10072 }
10073 break;
a315a145
FB
10074#endif
10075#ifdef TARGET_NR_fchown32
31e31b8a 10076 case TARGET_NR_fchown32:
b03c60f3
FB
10077 ret = get_errno(fchown(arg1, arg2, arg3));
10078 break;
a315a145
FB
10079#endif
10080#ifdef TARGET_NR_setresuid32
31e31b8a 10081 case TARGET_NR_setresuid32:
fd6f7798 10082 ret = get_errno(sys_setresuid(arg1, arg2, arg3));
b03c60f3 10083 break;
a315a145
FB
10084#endif
10085#ifdef TARGET_NR_getresuid32
31e31b8a 10086 case TARGET_NR_getresuid32:
b03c60f3 10087 {
53a5960a 10088 uid_t ruid, euid, suid;
b03c60f3
FB
10089 ret = get_errno(getresuid(&ruid, &euid, &suid));
10090 if (!is_error(ret)) {
2f619698
FB
10091 if (put_user_u32(ruid, arg1)
10092 || put_user_u32(euid, arg2)
10093 || put_user_u32(suid, arg3))
10094 goto efault;
b03c60f3
FB
10095 }
10096 }
10097 break;
a315a145
FB
10098#endif
10099#ifdef TARGET_NR_setresgid32
31e31b8a 10100 case TARGET_NR_setresgid32:
fd6f7798 10101 ret = get_errno(sys_setresgid(arg1, arg2, arg3));
b03c60f3 10102 break;
a315a145
FB
10103#endif
10104#ifdef TARGET_NR_getresgid32
31e31b8a 10105 case TARGET_NR_getresgid32:
b03c60f3 10106 {
53a5960a 10107 gid_t rgid, egid, sgid;
b03c60f3
FB
10108 ret = get_errno(getresgid(&rgid, &egid, &sgid));
10109 if (!is_error(ret)) {
2f619698
FB
10110 if (put_user_u32(rgid, arg1)
10111 || put_user_u32(egid, arg2)
10112 || put_user_u32(sgid, arg3))
10113 goto efault;
b03c60f3
FB
10114 }
10115 }
10116 break;
a315a145
FB
10117#endif
10118#ifdef TARGET_NR_chown32
31e31b8a 10119 case TARGET_NR_chown32:
579a97f7
FB
10120 if (!(p = lock_user_string(arg1)))
10121 goto efault;
53a5960a
PB
10122 ret = get_errno(chown(p, arg2, arg3));
10123 unlock_user(p, arg1, 0);
b03c60f3 10124 break;
a315a145
FB
10125#endif
10126#ifdef TARGET_NR_setuid32
31e31b8a 10127 case TARGET_NR_setuid32:
fd6f7798 10128 ret = get_errno(sys_setuid(arg1));
b03c60f3 10129 break;
a315a145
FB
10130#endif
10131#ifdef TARGET_NR_setgid32
31e31b8a 10132 case TARGET_NR_setgid32:
fd6f7798 10133 ret = get_errno(sys_setgid(arg1));
b03c60f3 10134 break;
a315a145
FB
10135#endif
10136#ifdef TARGET_NR_setfsuid32
31e31b8a 10137 case TARGET_NR_setfsuid32:
b03c60f3
FB
10138 ret = get_errno(setfsuid(arg1));
10139 break;
a315a145
FB
10140#endif
10141#ifdef TARGET_NR_setfsgid32
31e31b8a 10142 case TARGET_NR_setfsgid32:
b03c60f3
FB
10143 ret = get_errno(setfsgid(arg1));
10144 break;
a315a145 10145#endif
67867308 10146
31e31b8a 10147 case TARGET_NR_pivot_root:
b03c60f3 10148 goto unimplemented;
ffa65c3b 10149#ifdef TARGET_NR_mincore
31e31b8a 10150 case TARGET_NR_mincore:
04bb9ace
AJ
10151 {
10152 void *a;
10153 ret = -TARGET_EFAULT;
10154 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
10155 goto efault;
10156 if (!(p = lock_user_string(arg3)))
10157 goto mincore_fail;
10158 ret = get_errno(mincore(a, arg2, p));
10159 unlock_user(p, arg3, ret);
10160 mincore_fail:
10161 unlock_user(a, arg1, 0);
10162 }
10163 break;
ffa65c3b 10164#endif
408321b6
AJ
10165#ifdef TARGET_NR_arm_fadvise64_64
10166 case TARGET_NR_arm_fadvise64_64:
e0156a9d
PM
10167 /* arm_fadvise64_64 looks like fadvise64_64 but
10168 * with different argument order: fd, advice, offset, len
10169 * rather than the usual fd, offset, len, advice.
10170 * Note that offset and len are both 64-bit so appear as
10171 * pairs of 32-bit registers.
10172 */
10173 ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
10174 target_offset64(arg5, arg6), arg2);
10175 ret = -host_to_target_errno(ret);
10176 break;
408321b6 10177#endif
badd3cd8
PM
10178
10179#if TARGET_ABI_BITS == 32
10180
10181#ifdef TARGET_NR_fadvise64_64
10182 case TARGET_NR_fadvise64_64:
10183 /* 6 args: fd, offset (high, low), len (high, low), advice */
10184 if (regpairs_aligned(cpu_env)) {
10185 /* offset is in (3,4), len in (5,6) and advice in 7 */
10186 arg2 = arg3;
10187 arg3 = arg4;
10188 arg4 = arg5;
10189 arg5 = arg6;
10190 arg6 = arg7;
10191 }
10192 ret = -host_to_target_errno(posix_fadvise(arg1,
10193 target_offset64(arg2, arg3),
10194 target_offset64(arg4, arg5),
10195 arg6));
10196 break;
10197#endif
10198
10199#ifdef TARGET_NR_fadvise64
10200 case TARGET_NR_fadvise64:
10201 /* 5 args: fd, offset (high, low), len, advice */
10202 if (regpairs_aligned(cpu_env)) {
10203 /* offset is in (3,4), len in 5 and advice in 6 */
10204 arg2 = arg3;
10205 arg3 = arg4;
10206 arg4 = arg5;
10207 arg5 = arg6;
10208 }
10209 ret = -host_to_target_errno(posix_fadvise(arg1,
10210 target_offset64(arg2, arg3),
10211 arg4, arg5));
10212 break;
408321b6 10213#endif
badd3cd8
PM
10214
10215#else /* not a 32-bit ABI */
e0156a9d 10216#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
408321b6
AJ
10217#ifdef TARGET_NR_fadvise64_64
10218 case TARGET_NR_fadvise64_64:
10219#endif
e72d2cc7
UH
10220#ifdef TARGET_NR_fadvise64
10221 case TARGET_NR_fadvise64:
10222#endif
10223#ifdef TARGET_S390X
10224 switch (arg4) {
10225 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
10226 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
10227 case 6: arg4 = POSIX_FADV_DONTNEED; break;
10228 case 7: arg4 = POSIX_FADV_NOREUSE; break;
10229 default: break;
10230 }
10231#endif
977d8241
PM
10232 ret = -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
10233 break;
408321b6 10234#endif
badd3cd8
PM
10235#endif /* end of 64-bit ABI fadvise handling */
10236
ffa65c3b 10237#ifdef TARGET_NR_madvise
31e31b8a 10238 case TARGET_NR_madvise:
24836689 10239 /* A straight passthrough may not be safe because qemu sometimes
d2d6b857 10240 turns private file-backed mappings into anonymous mappings.
24836689
PB
10241 This will break MADV_DONTNEED.
10242 This is a hint, so ignoring and returning success is ok. */
10243 ret = get_errno(0);
10244 break;
ffa65c3b 10245#endif
992f48a0 10246#if TARGET_ABI_BITS == 32
31e31b8a 10247 case TARGET_NR_fcntl64:
77e4672d 10248 {
b1e341eb 10249 int cmd;
77e4672d 10250 struct flock64 fl;
213d3e9e
PM
10251 from_flock64_fn *copyfrom = copy_from_user_flock64;
10252 to_flock64_fn *copyto = copy_to_user_flock64;
10253
ce4defa0 10254#ifdef TARGET_ARM
213d3e9e
PM
10255 if (((CPUARMState *)cpu_env)->eabi) {
10256 copyfrom = copy_from_user_eabi_flock64;
10257 copyto = copy_to_user_eabi_flock64;
10258 }
ce4defa0 10259#endif
77e4672d 10260
5f106811 10261 cmd = target_to_host_fcntl_cmd(arg2);
31b63193
PM
10262 if (cmd == -TARGET_EINVAL) {
10263 ret = cmd;
10264 break;
10265 }
b1e341eb 10266
60cd49d5 10267 switch(arg2) {
b1e341eb 10268 case TARGET_F_GETLK64:
213d3e9e
PM
10269 ret = copyfrom(&fl, arg3);
10270 if (ret) {
10271 break;
5813427b 10272 }
b1e341eb 10273 ret = get_errno(fcntl(arg1, cmd, &fl));
213d3e9e
PM
10274 if (ret == 0) {
10275 ret = copyto(arg3, &fl);
10276 }
77e4672d
FB
10277 break;
10278
b1e341eb
TS
10279 case TARGET_F_SETLK64:
10280 case TARGET_F_SETLKW64:
213d3e9e
PM
10281 ret = copyfrom(&fl, arg3);
10282 if (ret) {
10283 break;
ce4defa0 10284 }
435da5e7 10285 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
77e4672d 10286 break;
60cd49d5 10287 default:
5f106811 10288 ret = do_fcntl(arg1, arg2, arg3);
60cd49d5
FB
10289 break;
10290 }
77e4672d
FB
10291 break;
10292 }
60cd49d5 10293#endif
7d600c80
TS
10294#ifdef TARGET_NR_cacheflush
10295 case TARGET_NR_cacheflush:
10296 /* self-modifying code is handled automatically, so nothing needed */
10297 ret = 0;
10298 break;
10299#endif
ebc05488 10300#ifdef TARGET_NR_security
31e31b8a
FB
10301 case TARGET_NR_security:
10302 goto unimplemented;
c573ff67
FB
10303#endif
10304#ifdef TARGET_NR_getpagesize
10305 case TARGET_NR_getpagesize:
10306 ret = TARGET_PAGE_SIZE;
10307 break;
ebc05488 10308#endif
31e31b8a
FB
10309 case TARGET_NR_gettid:
10310 ret = get_errno(gettid());
10311 break;
e5febef5 10312#ifdef TARGET_NR_readahead
31e31b8a 10313 case TARGET_NR_readahead:
2054ac9b 10314#if TARGET_ABI_BITS == 32
48e515d4 10315 if (regpairs_aligned(cpu_env)) {
2054ac9b
AJ
10316 arg2 = arg3;
10317 arg3 = arg4;
10318 arg4 = arg5;
10319 }
2054ac9b
AJ
10320 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
10321#else
10322 ret = get_errno(readahead(arg1, arg2, arg3));
10323#endif
10324 break;
e5febef5 10325#endif
a790ae38 10326#ifdef CONFIG_ATTR
ebc05488 10327#ifdef TARGET_NR_setxattr
31e31b8a
FB
10328 case TARGET_NR_listxattr:
10329 case TARGET_NR_llistxattr:
fb5590f7
PM
10330 {
10331 void *p, *b = 0;
10332 if (arg2) {
10333 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10334 if (!b) {
10335 ret = -TARGET_EFAULT;
10336 break;
10337 }
10338 }
10339 p = lock_user_string(arg1);
10340 if (p) {
10341 if (num == TARGET_NR_listxattr) {
10342 ret = get_errno(listxattr(p, b, arg3));
10343 } else {
10344 ret = get_errno(llistxattr(p, b, arg3));
10345 }
10346 } else {
10347 ret = -TARGET_EFAULT;
10348 }
10349 unlock_user(p, arg1, 0);
10350 unlock_user(b, arg2, arg3);
10351 break;
10352 }
31e31b8a 10353 case TARGET_NR_flistxattr:
fb5590f7
PM
10354 {
10355 void *b = 0;
10356 if (arg2) {
10357 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10358 if (!b) {
10359 ret = -TARGET_EFAULT;
10360 break;
10361 }
10362 }
10363 ret = get_errno(flistxattr(arg1, b, arg3));
10364 unlock_user(b, arg2, arg3);
6f932f91 10365 break;
fb5590f7 10366 }
a790ae38 10367 case TARGET_NR_setxattr:
30297b55 10368 case TARGET_NR_lsetxattr:
a790ae38 10369 {
e3c33ec6
PM
10370 void *p, *n, *v = 0;
10371 if (arg3) {
10372 v = lock_user(VERIFY_READ, arg3, arg4, 1);
10373 if (!v) {
10374 ret = -TARGET_EFAULT;
10375 break;
10376 }
10377 }
a790ae38
ACH
10378 p = lock_user_string(arg1);
10379 n = lock_user_string(arg2);
e3c33ec6 10380 if (p && n) {
30297b55
PM
10381 if (num == TARGET_NR_setxattr) {
10382 ret = get_errno(setxattr(p, n, v, arg4, arg5));
10383 } else {
10384 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
10385 }
a790ae38
ACH
10386 } else {
10387 ret = -TARGET_EFAULT;
10388 }
10389 unlock_user(p, arg1, 0);
10390 unlock_user(n, arg2, 0);
10391 unlock_user(v, arg3, 0);
10392 }
10393 break;
30297b55
PM
10394 case TARGET_NR_fsetxattr:
10395 {
10396 void *n, *v = 0;
10397 if (arg3) {
10398 v = lock_user(VERIFY_READ, arg3, arg4, 1);
10399 if (!v) {
10400 ret = -TARGET_EFAULT;
10401 break;
10402 }
10403 }
10404 n = lock_user_string(arg2);
10405 if (n) {
10406 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
10407 } else {
10408 ret = -TARGET_EFAULT;
10409 }
10410 unlock_user(n, arg2, 0);
10411 unlock_user(v, arg3, 0);
10412 }
10413 break;
a790ae38 10414 case TARGET_NR_getxattr:
30297b55 10415 case TARGET_NR_lgetxattr:
a790ae38 10416 {
e3c33ec6
PM
10417 void *p, *n, *v = 0;
10418 if (arg3) {
10419 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
10420 if (!v) {
10421 ret = -TARGET_EFAULT;
10422 break;
10423 }
10424 }
a790ae38
ACH
10425 p = lock_user_string(arg1);
10426 n = lock_user_string(arg2);
e3c33ec6 10427 if (p && n) {
30297b55
PM
10428 if (num == TARGET_NR_getxattr) {
10429 ret = get_errno(getxattr(p, n, v, arg4));
10430 } else {
10431 ret = get_errno(lgetxattr(p, n, v, arg4));
10432 }
a790ae38
ACH
10433 } else {
10434 ret = -TARGET_EFAULT;
10435 }
10436 unlock_user(p, arg1, 0);
10437 unlock_user(n, arg2, 0);
10438 unlock_user(v, arg3, arg4);
10439 }
10440 break;
30297b55
PM
10441 case TARGET_NR_fgetxattr:
10442 {
10443 void *n, *v = 0;
10444 if (arg3) {
10445 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
10446 if (!v) {
10447 ret = -TARGET_EFAULT;
10448 break;
10449 }
10450 }
10451 n = lock_user_string(arg2);
10452 if (n) {
10453 ret = get_errno(fgetxattr(arg1, n, v, arg4));
10454 } else {
10455 ret = -TARGET_EFAULT;
10456 }
10457 unlock_user(n, arg2, 0);
10458 unlock_user(v, arg3, arg4);
10459 }
10460 break;
a790ae38 10461 case TARGET_NR_removexattr:
30297b55 10462 case TARGET_NR_lremovexattr:
a790ae38
ACH
10463 {
10464 void *p, *n;
10465 p = lock_user_string(arg1);
10466 n = lock_user_string(arg2);
10467 if (p && n) {
30297b55
PM
10468 if (num == TARGET_NR_removexattr) {
10469 ret = get_errno(removexattr(p, n));
10470 } else {
10471 ret = get_errno(lremovexattr(p, n));
10472 }
a790ae38
ACH
10473 } else {
10474 ret = -TARGET_EFAULT;
10475 }
10476 unlock_user(p, arg1, 0);
10477 unlock_user(n, arg2, 0);
10478 }
10479 break;
30297b55
PM
10480 case TARGET_NR_fremovexattr:
10481 {
10482 void *n;
10483 n = lock_user_string(arg2);
10484 if (n) {
10485 ret = get_errno(fremovexattr(arg1, n));
10486 } else {
10487 ret = -TARGET_EFAULT;
10488 }
10489 unlock_user(n, arg2, 0);
10490 }
10491 break;
ebc05488 10492#endif
a790ae38 10493#endif /* CONFIG_ATTR */
ebc05488 10494#ifdef TARGET_NR_set_thread_area
5cd4393b 10495 case TARGET_NR_set_thread_area:
8d18e893 10496#if defined(TARGET_MIPS)
d279279e 10497 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
6f5b89a0
TS
10498 ret = 0;
10499 break;
ef96779b
EI
10500#elif defined(TARGET_CRIS)
10501 if (arg1 & 0xff)
10502 ret = -TARGET_EINVAL;
10503 else {
10504 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
10505 ret = 0;
10506 }
10507 break;
8d18e893
FB
10508#elif defined(TARGET_I386) && defined(TARGET_ABI32)
10509 ret = do_set_thread_area(cpu_env, arg1);
10510 break;
1ccd9374
PM
10511#elif defined(TARGET_M68K)
10512 {
0429a971 10513 TaskState *ts = cpu->opaque;
1ccd9374 10514 ts->tp_value = arg1;
95c1eb19 10515 ret = 0;
1ccd9374
PM
10516 break;
10517 }
6f5b89a0
TS
10518#else
10519 goto unimplemented_nowarn;
10520#endif
10521#endif
10522#ifdef TARGET_NR_get_thread_area
5cd4393b 10523 case TARGET_NR_get_thread_area:
8d18e893
FB
10524#if defined(TARGET_I386) && defined(TARGET_ABI32)
10525 ret = do_get_thread_area(cpu_env, arg1);
d312bbe1 10526 break;
1ccd9374
PM
10527#elif defined(TARGET_M68K)
10528 {
0429a971 10529 TaskState *ts = cpu->opaque;
1ccd9374
PM
10530 ret = ts->tp_value;
10531 break;
10532 }
8d18e893 10533#else
5cd4393b 10534 goto unimplemented_nowarn;
48dc41eb 10535#endif
8d18e893 10536#endif
48dc41eb
FB
10537#ifdef TARGET_NR_getdomainname
10538 case TARGET_NR_getdomainname:
10539 goto unimplemented_nowarn;
ebc05488 10540#endif
6f5b89a0 10541
b5906f95
TS
10542#ifdef TARGET_NR_clock_gettime
10543 case TARGET_NR_clock_gettime:
10544 {
10545 struct timespec ts;
10546 ret = get_errno(clock_gettime(arg1, &ts));
10547 if (!is_error(ret)) {
10548 host_to_target_timespec(arg2, &ts);
10549 }
10550 break;
10551 }
10552#endif
10553#ifdef TARGET_NR_clock_getres
10554 case TARGET_NR_clock_getres:
10555 {
10556 struct timespec ts;
10557 ret = get_errno(clock_getres(arg1, &ts));
10558 if (!is_error(ret)) {
10559 host_to_target_timespec(arg2, &ts);
10560 }
10561 break;
10562 }
10563#endif
63d7651b
PB
10564#ifdef TARGET_NR_clock_nanosleep
10565 case TARGET_NR_clock_nanosleep:
10566 {
10567 struct timespec ts;
10568 target_to_host_timespec(&ts, arg3);
9e518226
PM
10569 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
10570 &ts, arg4 ? &ts : NULL));
63d7651b
PB
10571 if (arg4)
10572 host_to_target_timespec(arg4, &ts);
8fbe8fdf
TM
10573
10574#if defined(TARGET_PPC)
10575 /* clock_nanosleep is odd in that it returns positive errno values.
10576 * On PPC, CR0 bit 3 should be set in such a situation. */
9e518226 10577 if (ret && ret != -TARGET_ERESTARTSYS) {
8fbe8fdf
TM
10578 ((CPUPPCState *)cpu_env)->crf[0] |= 1;
10579 }
10580#endif
63d7651b
PB
10581 break;
10582 }
10583#endif
b5906f95 10584
6f5b89a0
TS
10585#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
10586 case TARGET_NR_set_tid_address:
579a97f7
FB
10587 ret = get_errno(set_tid_address((int *)g2h(arg1)));
10588 break;
6f5b89a0
TS
10589#endif
10590
4cae1d16 10591 case TARGET_NR_tkill:
bef653d9 10592 ret = get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
4cae1d16 10593 break;
4cae1d16 10594
71455574 10595 case TARGET_NR_tgkill:
bef653d9 10596 ret = get_errno(safe_tgkill((int)arg1, (int)arg2,
4cb05961 10597 target_to_host_signal(arg3)));
bef653d9 10598 break;
71455574 10599
4f2b1fe8
TS
10600#ifdef TARGET_NR_set_robust_list
10601 case TARGET_NR_set_robust_list:
e9a970a8
PM
10602 case TARGET_NR_get_robust_list:
10603 /* The ABI for supporting robust futexes has userspace pass
10604 * the kernel a pointer to a linked list which is updated by
10605 * userspace after the syscall; the list is walked by the kernel
10606 * when the thread exits. Since the linked list in QEMU guest
10607 * memory isn't a valid linked list for the host and we have
10608 * no way to reliably intercept the thread-death event, we can't
10609 * support these. Silently return ENOSYS so that guest userspace
10610 * falls back to a non-robust futex implementation (which should
10611 * be OK except in the corner case of the guest crashing while
10612 * holding a mutex that is shared with another process via
10613 * shared memory).
10614 */
10615 goto unimplemented_nowarn;
4f2b1fe8
TS
10616#endif
10617
1acae9f2 10618#if defined(TARGET_NR_utimensat)
9007f0ef
TS
10619 case TARGET_NR_utimensat:
10620 {
ebc996f3
RV
10621 struct timespec *tsp, ts[2];
10622 if (!arg3) {
10623 tsp = NULL;
10624 } else {
10625 target_to_host_timespec(ts, arg3);
10626 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
10627 tsp = ts;
10628 }
9007f0ef 10629 if (!arg2)
ebc996f3 10630 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
9007f0ef 10631 else {
579a97f7 10632 if (!(p = lock_user_string(arg2))) {
0da46a6e 10633 ret = -TARGET_EFAULT;
579a97f7
FB
10634 goto fail;
10635 }
ebc996f3 10636 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
579a97f7 10637 unlock_user(p, arg2, 0);
9007f0ef
TS
10638 }
10639 }
10640 break;
10641#endif
bd0c5661
PB
10642 case TARGET_NR_futex:
10643 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
10644 break;
dbfe4c36 10645#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
39b59763
AJ
10646 case TARGET_NR_inotify_init:
10647 ret = get_errno(sys_inotify_init());
10648 break;
10649#endif
a1606b0b 10650#ifdef CONFIG_INOTIFY1
c05c7a73
RV
10651#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
10652 case TARGET_NR_inotify_init1:
10653 ret = get_errno(sys_inotify_init1(arg1));
10654 break;
10655#endif
a1606b0b 10656#endif
dbfe4c36 10657#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
39b59763
AJ
10658 case TARGET_NR_inotify_add_watch:
10659 p = lock_user_string(arg2);
10660 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
10661 unlock_user(p, arg2, 0);
10662 break;
10663#endif
dbfe4c36 10664#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
39b59763
AJ
10665 case TARGET_NR_inotify_rm_watch:
10666 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
10667 break;
10668#endif
9007f0ef 10669
8ec9cf89 10670#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
24e1003a
AJ
10671 case TARGET_NR_mq_open:
10672 {
b6ce1f6b 10673 struct mq_attr posix_mq_attr, *attrp;
24e1003a
AJ
10674
10675 p = lock_user_string(arg1 - 1);
b6ce1f6b 10676 if (arg4 != 0) {
24e1003a 10677 copy_from_user_mq_attr (&posix_mq_attr, arg4);
b6ce1f6b
TM
10678 attrp = &posix_mq_attr;
10679 } else {
10680 attrp = 0;
10681 }
10682 ret = get_errno(mq_open(p, arg2, arg3, attrp));
24e1003a
AJ
10683 unlock_user (p, arg1, 0);
10684 }
10685 break;
10686
10687 case TARGET_NR_mq_unlink:
10688 p = lock_user_string(arg1 - 1);
10689 ret = get_errno(mq_unlink(p));
10690 unlock_user (p, arg1, 0);
10691 break;
10692
10693 case TARGET_NR_mq_timedsend:
10694 {
10695 struct timespec ts;
10696
10697 p = lock_user (VERIFY_READ, arg2, arg3, 1);
10698 if (arg5 != 0) {
10699 target_to_host_timespec(&ts, arg5);
d40ecd66 10700 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
24e1003a 10701 host_to_target_timespec(arg5, &ts);
d40ecd66
PM
10702 } else {
10703 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
24e1003a 10704 }
24e1003a
AJ
10705 unlock_user (p, arg2, arg3);
10706 }
10707 break;
10708
10709 case TARGET_NR_mq_timedreceive:
10710 {
10711 struct timespec ts;
10712 unsigned int prio;
10713
10714 p = lock_user (VERIFY_READ, arg2, arg3, 1);
10715 if (arg5 != 0) {
10716 target_to_host_timespec(&ts, arg5);
d40ecd66
PM
10717 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
10718 &prio, &ts));
24e1003a 10719 host_to_target_timespec(arg5, &ts);
d40ecd66
PM
10720 } else {
10721 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
10722 &prio, NULL));
24e1003a 10723 }
24e1003a
AJ
10724 unlock_user (p, arg2, arg3);
10725 if (arg4 != 0)
10726 put_user_u32(prio, arg4);
10727 }
10728 break;
10729
10730 /* Not implemented for now... */
10731/* case TARGET_NR_mq_notify: */
10732/* break; */
10733
10734 case TARGET_NR_mq_getsetattr:
10735 {
10736 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
10737 ret = 0;
10738 if (arg3 != 0) {
10739 ret = mq_getattr(arg1, &posix_mq_attr_out);
10740 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
10741 }
10742 if (arg2 != 0) {
10743 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
10744 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
10745 }
10746
10747 }
10748 break;
10749#endif
10750
3ce34dfb
VS
10751#ifdef CONFIG_SPLICE
10752#ifdef TARGET_NR_tee
10753 case TARGET_NR_tee:
10754 {
10755 ret = get_errno(tee(arg1,arg2,arg3,arg4));
10756 }
10757 break;
10758#endif
10759#ifdef TARGET_NR_splice
10760 case TARGET_NR_splice:
10761 {
10762 loff_t loff_in, loff_out;
10763 loff_t *ploff_in = NULL, *ploff_out = NULL;
17644b36
AS
10764 if (arg2) {
10765 if (get_user_u64(loff_in, arg2)) {
10766 goto efault;
10767 }
3ce34dfb
VS
10768 ploff_in = &loff_in;
10769 }
17644b36
AS
10770 if (arg4) {
10771 if (get_user_u64(loff_out, arg4)) {
10772 goto efault;
10773 }
3ce34dfb
VS
10774 ploff_out = &loff_out;
10775 }
10776 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
17644b36
AS
10777 if (arg2) {
10778 if (put_user_u64(loff_in, arg2)) {
10779 goto efault;
10780 }
10781 }
10782 if (arg4) {
10783 if (put_user_u64(loff_out, arg4)) {
10784 goto efault;
10785 }
10786 }
3ce34dfb
VS
10787 }
10788 break;
10789#endif
10790#ifdef TARGET_NR_vmsplice
10791 case TARGET_NR_vmsplice:
10792 {
f287b2c2
RH
10793 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
10794 if (vec != NULL) {
10795 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
10796 unlock_iovec(vec, arg2, arg3, 0);
10797 } else {
10798 ret = -host_to_target_errno(errno);
10799 }
3ce34dfb
VS
10800 }
10801 break;
10802#endif
10803#endif /* CONFIG_SPLICE */
c2882b96
RV
10804#ifdef CONFIG_EVENTFD
10805#if defined(TARGET_NR_eventfd)
10806 case TARGET_NR_eventfd:
10807 ret = get_errno(eventfd(arg1, 0));
e36800c9 10808 fd_trans_unregister(ret);
c2882b96
RV
10809 break;
10810#endif
10811#if defined(TARGET_NR_eventfd2)
10812 case TARGET_NR_eventfd2:
5947c697
PJ
10813 {
10814 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
10815 if (arg2 & TARGET_O_NONBLOCK) {
10816 host_flags |= O_NONBLOCK;
10817 }
10818 if (arg2 & TARGET_O_CLOEXEC) {
10819 host_flags |= O_CLOEXEC;
10820 }
10821 ret = get_errno(eventfd(arg1, host_flags));
e36800c9 10822 fd_trans_unregister(ret);
c2882b96 10823 break;
5947c697 10824 }
c2882b96
RV
10825#endif
10826#endif /* CONFIG_EVENTFD */
d0927938
UH
10827#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
10828 case TARGET_NR_fallocate:
20249ae1
AG
10829#if TARGET_ABI_BITS == 32
10830 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
10831 target_offset64(arg5, arg6)));
10832#else
d0927938 10833 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
20249ae1 10834#endif
d0927938 10835 break;
c727f47d
PM
10836#endif
10837#if defined(CONFIG_SYNC_FILE_RANGE)
10838#if defined(TARGET_NR_sync_file_range)
10839 case TARGET_NR_sync_file_range:
10840#if TARGET_ABI_BITS == 32
bfcedc57
RV
10841#if defined(TARGET_MIPS)
10842 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
10843 target_offset64(arg5, arg6), arg7));
10844#else
c727f47d
PM
10845 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
10846 target_offset64(arg4, arg5), arg6));
bfcedc57 10847#endif /* !TARGET_MIPS */
c727f47d
PM
10848#else
10849 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
10850#endif
10851 break;
10852#endif
10853#if defined(TARGET_NR_sync_file_range2)
10854 case TARGET_NR_sync_file_range2:
10855 /* This is like sync_file_range but the arguments are reordered */
10856#if TARGET_ABI_BITS == 32
10857 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
10858 target_offset64(arg5, arg6), arg2));
10859#else
10860 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
10861#endif
10862 break;
10863#endif
3b6edd16 10864#endif
e36800c9
LV
10865#if defined(TARGET_NR_signalfd4)
10866 case TARGET_NR_signalfd4:
10867 ret = do_signalfd4(arg1, arg2, arg4);
10868 break;
10869#endif
10870#if defined(TARGET_NR_signalfd)
10871 case TARGET_NR_signalfd:
10872 ret = do_signalfd4(arg1, arg2, 0);
10873 break;
10874#endif
3b6edd16
PM
10875#if defined(CONFIG_EPOLL)
10876#if defined(TARGET_NR_epoll_create)
10877 case TARGET_NR_epoll_create:
10878 ret = get_errno(epoll_create(arg1));
10879 break;
10880#endif
10881#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
10882 case TARGET_NR_epoll_create1:
10883 ret = get_errno(epoll_create1(arg1));
10884 break;
10885#endif
10886#if defined(TARGET_NR_epoll_ctl)
10887 case TARGET_NR_epoll_ctl:
10888 {
10889 struct epoll_event ep;
10890 struct epoll_event *epp = 0;
10891 if (arg4) {
10892 struct target_epoll_event *target_ep;
10893 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
10894 goto efault;
10895 }
10896 ep.events = tswap32(target_ep->events);
10897 /* The epoll_data_t union is just opaque data to the kernel,
10898 * so we transfer all 64 bits across and need not worry what
10899 * actual data type it is.
10900 */
10901 ep.data.u64 = tswap64(target_ep->data.u64);
10902 unlock_user_struct(target_ep, arg4, 0);
10903 epp = &ep;
10904 }
10905 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
10906 break;
10907 }
10908#endif
10909
227f0214 10910#if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
10911#if defined(TARGET_NR_epoll_wait)
10912 case TARGET_NR_epoll_wait:
10913#endif
227f0214 10914#if defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
10915 case TARGET_NR_epoll_pwait:
10916#endif
10917 {
10918 struct target_epoll_event *target_ep;
10919 struct epoll_event *ep;
10920 int epfd = arg1;
10921 int maxevents = arg3;
10922 int timeout = arg4;
10923
10924 target_ep = lock_user(VERIFY_WRITE, arg2,
10925 maxevents * sizeof(struct target_epoll_event), 1);
10926 if (!target_ep) {
10927 goto efault;
10928 }
10929
10930 ep = alloca(maxevents * sizeof(struct epoll_event));
10931
10932 switch (num) {
227f0214 10933#if defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
10934 case TARGET_NR_epoll_pwait:
10935 {
10936 target_sigset_t *target_set;
10937 sigset_t _set, *set = &_set;
10938
10939 if (arg5) {
10940 target_set = lock_user(VERIFY_READ, arg5,
10941 sizeof(target_sigset_t), 1);
10942 if (!target_set) {
10943 unlock_user(target_ep, arg2, 0);
10944 goto efault;
10945 }
10946 target_to_host_sigset(set, target_set);
10947 unlock_user(target_set, arg5, 0);
10948 } else {
10949 set = NULL;
10950 }
10951
227f0214
PM
10952 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
10953 set, SIGSET_T_SIZE));
3b6edd16
PM
10954 break;
10955 }
10956#endif
10957#if defined(TARGET_NR_epoll_wait)
10958 case TARGET_NR_epoll_wait:
227f0214
PM
10959 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
10960 NULL, 0));
3b6edd16
PM
10961 break;
10962#endif
10963 default:
10964 ret = -TARGET_ENOSYS;
10965 }
10966 if (!is_error(ret)) {
10967 int i;
10968 for (i = 0; i < ret; i++) {
10969 target_ep[i].events = tswap32(ep[i].events);
10970 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
10971 }
10972 }
10973 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
10974 break;
10975 }
10976#endif
163a05a8
PM
10977#endif
10978#ifdef TARGET_NR_prlimit64
10979 case TARGET_NR_prlimit64:
10980 {
10981 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
10982 struct target_rlimit64 *target_rnew, *target_rold;
10983 struct host_rlimit64 rnew, rold, *rnewp = 0;
95018018 10984 int resource = target_to_host_resource(arg2);
163a05a8
PM
10985 if (arg3) {
10986 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
10987 goto efault;
10988 }
10989 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
10990 rnew.rlim_max = tswap64(target_rnew->rlim_max);
10991 unlock_user_struct(target_rnew, arg3, 0);
10992 rnewp = &rnew;
10993 }
10994
95018018 10995 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
163a05a8
PM
10996 if (!is_error(ret) && arg4) {
10997 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
10998 goto efault;
10999 }
11000 target_rold->rlim_cur = tswap64(rold.rlim_cur);
11001 target_rold->rlim_max = tswap64(rold.rlim_max);
11002 unlock_user_struct(target_rold, arg4, 1);
11003 }
11004 break;
11005 }
3d21d29c
RH
11006#endif
11007#ifdef TARGET_NR_gethostname
11008 case TARGET_NR_gethostname:
11009 {
11010 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
11011 if (name) {
11012 ret = get_errno(gethostname(name, arg2));
11013 unlock_user(name, arg1, arg2);
11014 } else {
11015 ret = -TARGET_EFAULT;
11016 }
11017 break;
11018 }
89aaf1a6
RV
11019#endif
11020#ifdef TARGET_NR_atomic_cmpxchg_32
11021 case TARGET_NR_atomic_cmpxchg_32:
11022 {
11023 /* should use start_exclusive from main.c */
11024 abi_ulong mem_value;
11025 if (get_user_u32(mem_value, arg6)) {
11026 target_siginfo_t info;
11027 info.si_signo = SIGSEGV;
11028 info.si_errno = 0;
11029 info.si_code = TARGET_SEGV_MAPERR;
11030 info._sifields._sigfault._addr = arg6;
11031 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
11032 ret = 0xdeadbeef;
11033
11034 }
11035 if (mem_value == arg2)
11036 put_user_u32(arg1, arg6);
11037 ret = mem_value;
11038 break;
11039 }
11040#endif
11041#ifdef TARGET_NR_atomic_barrier
11042 case TARGET_NR_atomic_barrier:
11043 {
11044 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
3b899ea7 11045 ret = 0;
89aaf1a6
RV
11046 break;
11047 }
d0927938 11048#endif
f4f1e10a
ECL
11049
11050#ifdef TARGET_NR_timer_create
11051 case TARGET_NR_timer_create:
11052 {
11053 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
11054
11055 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
f4f1e10a
ECL
11056
11057 int clkid = arg1;
11058 int timer_index = next_free_host_timer();
11059
11060 if (timer_index < 0) {
11061 ret = -TARGET_EAGAIN;
11062 } else {
11063 timer_t *phtimer = g_posix_timers + timer_index;
11064
11065 if (arg2) {
f4f1e10a 11066 phost_sevp = &host_sevp;
c065976f
PM
11067 ret = target_to_host_sigevent(phost_sevp, arg2);
11068 if (ret != 0) {
11069 break;
11070 }
f4f1e10a
ECL
11071 }
11072
11073 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
11074 if (ret) {
11075 phtimer = NULL;
11076 } else {
aecc8861 11077 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
f4f1e10a
ECL
11078 goto efault;
11079 }
f4f1e10a
ECL
11080 }
11081 }
11082 break;
11083 }
11084#endif
11085
11086#ifdef TARGET_NR_timer_settime
11087 case TARGET_NR_timer_settime:
11088 {
11089 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
11090 * struct itimerspec * old_value */
aecc8861 11091 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 11092
aecc8861
AG
11093 if (timerid < 0) {
11094 ret = timerid;
11095 } else if (arg3 == 0) {
f4f1e10a
ECL
11096 ret = -TARGET_EINVAL;
11097 } else {
e52a99f7 11098 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
11099 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
11100
11101 target_to_host_itimerspec(&hspec_new, arg3);
11102 ret = get_errno(
11103 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
11104 host_to_target_itimerspec(arg2, &hspec_old);
11105 }
11106 break;
11107 }
11108#endif
11109
11110#ifdef TARGET_NR_timer_gettime
11111 case TARGET_NR_timer_gettime:
11112 {
11113 /* args: timer_t timerid, struct itimerspec *curr_value */
aecc8861 11114 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 11115
aecc8861
AG
11116 if (timerid < 0) {
11117 ret = timerid;
11118 } else if (!arg2) {
11119 ret = -TARGET_EFAULT;
f4f1e10a 11120 } else {
e52a99f7 11121 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
11122 struct itimerspec hspec;
11123 ret = get_errno(timer_gettime(htimer, &hspec));
11124
11125 if (host_to_target_itimerspec(arg2, &hspec)) {
11126 ret = -TARGET_EFAULT;
11127 }
11128 }
11129 break;
11130 }
11131#endif
11132
11133#ifdef TARGET_NR_timer_getoverrun
11134 case TARGET_NR_timer_getoverrun:
11135 {
11136 /* args: timer_t timerid */
aecc8861 11137 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 11138
aecc8861
AG
11139 if (timerid < 0) {
11140 ret = timerid;
f4f1e10a 11141 } else {
e52a99f7 11142 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
11143 ret = get_errno(timer_getoverrun(htimer));
11144 }
e36800c9 11145 fd_trans_unregister(ret);
f4f1e10a
ECL
11146 break;
11147 }
11148#endif
11149
11150#ifdef TARGET_NR_timer_delete
11151 case TARGET_NR_timer_delete:
11152 {
11153 /* args: timer_t timerid */
aecc8861 11154 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 11155
aecc8861
AG
11156 if (timerid < 0) {
11157 ret = timerid;
f4f1e10a 11158 } else {
e52a99f7 11159 timer_t htimer = g_posix_timers[timerid];
f4f1e10a 11160 ret = get_errno(timer_delete(htimer));
e52a99f7 11161 g_posix_timers[timerid] = 0;
f4f1e10a
ECL
11162 }
11163 break;
11164 }
11165#endif
11166
51834341
RV
11167#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
11168 case TARGET_NR_timerfd_create:
11169 ret = get_errno(timerfd_create(arg1,
11170 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
11171 break;
11172#endif
11173
11174#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
11175 case TARGET_NR_timerfd_gettime:
11176 {
11177 struct itimerspec its_curr;
11178
11179 ret = get_errno(timerfd_gettime(arg1, &its_curr));
11180
11181 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
11182 goto efault;
11183 }
11184 }
11185 break;
11186#endif
11187
11188#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
11189 case TARGET_NR_timerfd_settime:
11190 {
11191 struct itimerspec its_new, its_old, *p_new;
11192
11193 if (arg3) {
11194 if (target_to_host_itimerspec(&its_new, arg3)) {
11195 goto efault;
11196 }
11197 p_new = &its_new;
11198 } else {
11199 p_new = NULL;
11200 }
11201
11202 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
11203
11204 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
11205 goto efault;
11206 }
11207 }
11208 break;
11209#endif
11210
ab31cda3
PB
11211#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
11212 case TARGET_NR_ioprio_get:
11213 ret = get_errno(ioprio_get(arg1, arg2));
11214 break;
11215#endif
11216
11217#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
11218 case TARGET_NR_ioprio_set:
11219 ret = get_errno(ioprio_set(arg1, arg2, arg3));
11220 break;
11221#endif
11222
9af5c906
RV
11223#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
11224 case TARGET_NR_setns:
11225 ret = get_errno(setns(arg1, arg2));
11226 break;
11227#endif
11228#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
11229 case TARGET_NR_unshare:
11230 ret = get_errno(unshare(arg1));
11231 break;
11232#endif
11233
31e31b8a
FB
11234 default:
11235 unimplemented:
5cd4393b 11236 gemu_log("qemu: Unsupported syscall: %d\n", num);
4f2b1fe8 11237#if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5cd4393b 11238 unimplemented_nowarn:
80a9d035 11239#endif
0da46a6e 11240 ret = -TARGET_ENOSYS;
31e31b8a
FB
11241 break;
11242 }
579a97f7 11243fail:
c573ff67 11244#ifdef DEBUG
0bf9e31a 11245 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
c573ff67 11246#endif
b92c47c1
TS
11247 if(do_strace)
11248 print_syscall_ret(num, ret);
9c15e700 11249 trace_guest_user_syscall_ret(cpu, num, ret);
31e31b8a 11250 return ret;
579a97f7
FB
11251efault:
11252 ret = -TARGET_EFAULT;
11253 goto fail;
31e31b8a 11254}
This page took 3.080304 seconds and 4 git commands to generate.