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