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