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