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