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