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