]> git.ipfire.org Git - thirdparty/qemu.git/blame - linux-user/syscall.c
linux-user: Properly Handle semun Structure In Cross-Endian Situations
[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 2426 abi_ulong sem_otime;
03527344 2427#if !defined(TARGET_PPC64)
992f48a0 2428 abi_ulong __unused1;
03527344 2429#endif
992f48a0 2430 abi_ulong sem_ctime;
03527344 2431#if !defined(TARGET_PPC64)
992f48a0 2432 abi_ulong __unused2;
03527344 2433#endif
992f48a0
BS
2434 abi_ulong sem_nsems;
2435 abi_ulong __unused3;
2436 abi_ulong __unused4;
3eb6b044
TS
2437};
2438
579a97f7
FB
2439static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2440 abi_ulong target_addr)
3eb6b044
TS
2441{
2442 struct target_ipc_perm *target_ip;
2443 struct target_semid_ds *target_sd;
2444
579a97f7
FB
2445 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2446 return -TARGET_EFAULT;
e8bbe36c 2447 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
2448 host_ip->__key = tswap32(target_ip->__key);
2449 host_ip->uid = tswap32(target_ip->uid);
2450 host_ip->gid = tswap32(target_ip->gid);
2451 host_ip->cuid = tswap32(target_ip->cuid);
2452 host_ip->cgid = tswap32(target_ip->cgid);
2453#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2454 host_ip->mode = tswap32(target_ip->mode);
2455#else
cbb21eed 2456 host_ip->mode = tswap16(target_ip->mode);
55a2b163
PJ
2457#endif
2458#if defined(TARGET_PPC)
2459 host_ip->__seq = tswap32(target_ip->__seq);
2460#else
2461 host_ip->__seq = tswap16(target_ip->__seq);
2462#endif
3eb6b044 2463 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 2464 return 0;
3eb6b044
TS
2465}
2466
579a97f7
FB
2467static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2468 struct ipc_perm *host_ip)
3eb6b044
TS
2469{
2470 struct target_ipc_perm *target_ip;
2471 struct target_semid_ds *target_sd;
2472
579a97f7
FB
2473 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2474 return -TARGET_EFAULT;
3eb6b044 2475 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
2476 target_ip->__key = tswap32(host_ip->__key);
2477 target_ip->uid = tswap32(host_ip->uid);
2478 target_ip->gid = tswap32(host_ip->gid);
2479 target_ip->cuid = tswap32(host_ip->cuid);
2480 target_ip->cgid = tswap32(host_ip->cgid);
2481#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2482 target_ip->mode = tswap32(host_ip->mode);
2483#else
cbb21eed 2484 target_ip->mode = tswap16(host_ip->mode);
55a2b163
PJ
2485#endif
2486#if defined(TARGET_PPC)
2487 target_ip->__seq = tswap32(host_ip->__seq);
2488#else
2489 target_ip->__seq = tswap16(host_ip->__seq);
2490#endif
3eb6b044 2491 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 2492 return 0;
3eb6b044
TS
2493}
2494
579a97f7
FB
2495static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2496 abi_ulong target_addr)
3eb6b044
TS
2497{
2498 struct target_semid_ds *target_sd;
2499
579a97f7
FB
2500 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2501 return -TARGET_EFAULT;
e5289087
AJ
2502 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2503 return -TARGET_EFAULT;
cbb21eed
MB
2504 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2505 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2506 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
3eb6b044 2507 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 2508 return 0;
3eb6b044
TS
2509}
2510
579a97f7
FB
2511static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2512 struct semid_ds *host_sd)
3eb6b044
TS
2513{
2514 struct target_semid_ds *target_sd;
2515
579a97f7
FB
2516 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2517 return -TARGET_EFAULT;
e5289087 2518 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
3a93113a 2519 return -TARGET_EFAULT;
cbb21eed
MB
2520 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2521 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2522 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
3eb6b044 2523 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 2524 return 0;
3eb6b044
TS
2525}
2526
e5289087
AJ
2527struct target_seminfo {
2528 int semmap;
2529 int semmni;
2530 int semmns;
2531 int semmnu;
2532 int semmsl;
2533 int semopm;
2534 int semume;
2535 int semusz;
2536 int semvmx;
2537 int semaem;
2538};
2539
2540static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2541 struct seminfo *host_seminfo)
2542{
2543 struct target_seminfo *target_seminfo;
2544 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2545 return -TARGET_EFAULT;
2546 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2547 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2548 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2549 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2550 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2551 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2552 __put_user(host_seminfo->semume, &target_seminfo->semume);
2553 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2554 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2555 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2556 unlock_user_struct(target_seminfo, target_addr, 1);
2557 return 0;
2558}
2559
fa294816
TS
2560union semun {
2561 int val;
3eb6b044 2562 struct semid_ds *buf;
fa294816 2563 unsigned short *array;
e5289087 2564 struct seminfo *__buf;
fa294816
TS
2565};
2566
3eb6b044
TS
2567union target_semun {
2568 int val;
e5289087
AJ
2569 abi_ulong buf;
2570 abi_ulong array;
2571 abi_ulong __buf;
3eb6b044
TS
2572};
2573
e5289087
AJ
2574static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2575 abi_ulong target_addr)
3eb6b044 2576{
e5289087
AJ
2577 int nsems;
2578 unsigned short *array;
2579 union semun semun;
2580 struct semid_ds semid_ds;
2581 int i, ret;
3eb6b044 2582
e5289087
AJ
2583 semun.buf = &semid_ds;
2584
2585 ret = semctl(semid, 0, IPC_STAT, semun);
2586 if (ret == -1)
2587 return get_errno(ret);
2588
2589 nsems = semid_ds.sem_nsems;
2590
2591 *host_array = malloc(nsems*sizeof(unsigned short));
69d4c703
PM
2592 if (!*host_array) {
2593 return -TARGET_ENOMEM;
2594 }
e5289087
AJ
2595 array = lock_user(VERIFY_READ, target_addr,
2596 nsems*sizeof(unsigned short), 1);
69d4c703
PM
2597 if (!array) {
2598 free(*host_array);
e5289087 2599 return -TARGET_EFAULT;
69d4c703 2600 }
e5289087
AJ
2601
2602 for(i=0; i<nsems; i++) {
2603 __get_user((*host_array)[i], &array[i]);
3eb6b044 2604 }
e5289087
AJ
2605 unlock_user(array, target_addr, 0);
2606
579a97f7 2607 return 0;
3eb6b044
TS
2608}
2609
e5289087
AJ
2610static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2611 unsigned short **host_array)
3eb6b044 2612{
e5289087
AJ
2613 int nsems;
2614 unsigned short *array;
2615 union semun semun;
2616 struct semid_ds semid_ds;
2617 int i, ret;
3eb6b044 2618
e5289087
AJ
2619 semun.buf = &semid_ds;
2620
2621 ret = semctl(semid, 0, IPC_STAT, semun);
2622 if (ret == -1)
2623 return get_errno(ret);
2624
2625 nsems = semid_ds.sem_nsems;
2626
2627 array = lock_user(VERIFY_WRITE, target_addr,
2628 nsems*sizeof(unsigned short), 0);
2629 if (!array)
2630 return -TARGET_EFAULT;
2631
2632 for(i=0; i<nsems; i++) {
2633 __put_user((*host_array)[i], &array[i]);
3eb6b044 2634 }
e5289087
AJ
2635 free(*host_array);
2636 unlock_user(array, target_addr, 1);
2637
579a97f7 2638 return 0;
3eb6b044
TS
2639}
2640
e5289087
AJ
2641static inline abi_long do_semctl(int semid, int semnum, int cmd,
2642 union target_semun target_su)
3eb6b044
TS
2643{
2644 union semun arg;
2645 struct semid_ds dsarg;
7b8118e8 2646 unsigned short *array = NULL;
e5289087
AJ
2647 struct seminfo seminfo;
2648 abi_long ret = -TARGET_EINVAL;
2649 abi_long err;
2650 cmd &= 0xff;
3eb6b044
TS
2651
2652 switch( cmd ) {
2653 case GETVAL:
3eb6b044 2654 case SETVAL:
5464baec
TM
2655 /* In 64 bit cross-endian situations, we will erroneously pick up
2656 * the wrong half of the union for the "val" element. To rectify
2657 * this, the entire 8-byte structure is byteswapped, followed by
2658 * a swap of the 4 byte val field. In other cases, the data is
2659 * already in proper host byte order. */
2660 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
2661 target_su.buf = tswapal(target_su.buf);
2662 arg.val = tswap32(target_su.val);
2663 } else {
2664 arg.val = target_su.val;
2665 }
e5289087 2666 ret = get_errno(semctl(semid, semnum, cmd, arg));
3eb6b044
TS
2667 break;
2668 case GETALL:
3eb6b044 2669 case SETALL:
e5289087
AJ
2670 err = target_to_host_semarray(semid, &array, target_su.array);
2671 if (err)
2672 return err;
2673 arg.array = array;
2674 ret = get_errno(semctl(semid, semnum, cmd, arg));
2675 err = host_to_target_semarray(semid, target_su.array, &array);
2676 if (err)
2677 return err;
3eb6b044
TS
2678 break;
2679 case IPC_STAT:
3eb6b044 2680 case IPC_SET:
e5289087
AJ
2681 case SEM_STAT:
2682 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2683 if (err)
2684 return err;
2685 arg.buf = &dsarg;
2686 ret = get_errno(semctl(semid, semnum, cmd, arg));
2687 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2688 if (err)
2689 return err;
2690 break;
2691 case IPC_INFO:
2692 case SEM_INFO:
2693 arg.__buf = &seminfo;
2694 ret = get_errno(semctl(semid, semnum, cmd, arg));
2695 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2696 if (err)
2697 return err;
2698 break;
2699 case IPC_RMID:
2700 case GETPID:
2701 case GETNCNT:
2702 case GETZCNT:
2703 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3eb6b044 2704 break;
3eb6b044
TS
2705 }
2706
2707 return ret;
2708}
2709
e5289087
AJ
2710struct target_sembuf {
2711 unsigned short sem_num;
2712 short sem_op;
2713 short sem_flg;
2714};
2715
2716static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2717 abi_ulong target_addr,
2718 unsigned nsops)
2719{
2720 struct target_sembuf *target_sembuf;
2721 int i;
2722
2723 target_sembuf = lock_user(VERIFY_READ, target_addr,
2724 nsops*sizeof(struct target_sembuf), 1);
2725 if (!target_sembuf)
2726 return -TARGET_EFAULT;
2727
2728 for(i=0; i<nsops; i++) {
2729 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2730 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2731 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2732 }
2733
2734 unlock_user(target_sembuf, target_addr, 0);
2735
2736 return 0;
2737}
2738
2739static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2740{
2741 struct sembuf sops[nsops];
2742
2743 if (target_to_host_sembuf(sops, ptr, nsops))
2744 return -TARGET_EFAULT;
2745
c7128c9f 2746 return get_errno(semop(semid, sops, nsops));
e5289087
AJ
2747}
2748
1bc012f6
TS
2749struct target_msqid_ds
2750{
1c54ff97
AJ
2751 struct target_ipc_perm msg_perm;
2752 abi_ulong msg_stime;
2753#if TARGET_ABI_BITS == 32
2754 abi_ulong __unused1;
2755#endif
2756 abi_ulong msg_rtime;
2757#if TARGET_ABI_BITS == 32
2758 abi_ulong __unused2;
2759#endif
2760 abi_ulong msg_ctime;
2761#if TARGET_ABI_BITS == 32
2762 abi_ulong __unused3;
2763#endif
2764 abi_ulong __msg_cbytes;
2765 abi_ulong msg_qnum;
2766 abi_ulong msg_qbytes;
2767 abi_ulong msg_lspid;
2768 abi_ulong msg_lrpid;
2769 abi_ulong __unused4;
2770 abi_ulong __unused5;
1bc012f6
TS
2771};
2772
579a97f7
FB
2773static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2774 abi_ulong target_addr)
1bc012f6
TS
2775{
2776 struct target_msqid_ds *target_md;
2777
579a97f7
FB
2778 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2779 return -TARGET_EFAULT;
1c54ff97
AJ
2780 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2781 return -TARGET_EFAULT;
cbb21eed
MB
2782 host_md->msg_stime = tswapal(target_md->msg_stime);
2783 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2784 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2785 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2786 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2787 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2788 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2789 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
1bc012f6 2790 unlock_user_struct(target_md, target_addr, 0);
579a97f7 2791 return 0;
1bc012f6
TS
2792}
2793
579a97f7
FB
2794static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2795 struct msqid_ds *host_md)
1bc012f6
TS
2796{
2797 struct target_msqid_ds *target_md;
2798
579a97f7
FB
2799 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2800 return -TARGET_EFAULT;
1c54ff97
AJ
2801 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2802 return -TARGET_EFAULT;
cbb21eed
MB
2803 target_md->msg_stime = tswapal(host_md->msg_stime);
2804 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2805 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2806 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2807 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2808 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2809 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2810 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
1bc012f6 2811 unlock_user_struct(target_md, target_addr, 1);
579a97f7 2812 return 0;
1bc012f6
TS
2813}
2814
1c54ff97
AJ
2815struct target_msginfo {
2816 int msgpool;
2817 int msgmap;
2818 int msgmax;
2819 int msgmnb;
2820 int msgmni;
2821 int msgssz;
2822 int msgtql;
2823 unsigned short int msgseg;
2824};
2825
2826static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2827 struct msginfo *host_msginfo)
2828{
2829 struct target_msginfo *target_msginfo;
2830 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2831 return -TARGET_EFAULT;
2832 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2833 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2834 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2835 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2836 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2837 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2838 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2839 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2840 unlock_user_struct(target_msginfo, target_addr, 1);
00b229ac 2841 return 0;
1c54ff97
AJ
2842}
2843
2844static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
1bc012f6
TS
2845{
2846 struct msqid_ds dsarg;
1c54ff97
AJ
2847 struct msginfo msginfo;
2848 abi_long ret = -TARGET_EINVAL;
2849
2850 cmd &= 0xff;
2851
2852 switch (cmd) {
1bc012f6
TS
2853 case IPC_STAT:
2854 case IPC_SET:
1c54ff97
AJ
2855 case MSG_STAT:
2856 if (target_to_host_msqid_ds(&dsarg,ptr))
2857 return -TARGET_EFAULT;
2858 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2859 if (host_to_target_msqid_ds(ptr,&dsarg))
2860 return -TARGET_EFAULT;
2861 break;
2862 case IPC_RMID:
2863 ret = get_errno(msgctl(msgid, cmd, NULL));
2864 break;
2865 case IPC_INFO:
2866 case MSG_INFO:
2867 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2868 if (host_to_target_msginfo(ptr, &msginfo))
2869 return -TARGET_EFAULT;
2870 break;
1bc012f6 2871 }
1c54ff97 2872
1bc012f6
TS
2873 return ret;
2874}
2875
2876struct target_msgbuf {
1c54ff97
AJ
2877 abi_long mtype;
2878 char mtext[1];
1bc012f6
TS
2879};
2880
992f48a0
BS
2881static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2882 unsigned int msgsz, int msgflg)
1bc012f6
TS
2883{
2884 struct target_msgbuf *target_mb;
2885 struct msgbuf *host_mb;
992f48a0 2886 abi_long ret = 0;
1bc012f6 2887
579a97f7
FB
2888 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2889 return -TARGET_EFAULT;
1bc012f6 2890 host_mb = malloc(msgsz+sizeof(long));
cbb21eed 2891 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
1c54ff97 2892 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
1bc012f6
TS
2893 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2894 free(host_mb);
2895 unlock_user_struct(target_mb, msgp, 0);
2896
2897 return ret;
2898}
2899
992f48a0 2900static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1c54ff97 2901 unsigned int msgsz, abi_long msgtyp,
992f48a0 2902 int msgflg)
1bc012f6
TS
2903{
2904 struct target_msgbuf *target_mb;
579a97f7 2905 char *target_mtext;
1bc012f6 2906 struct msgbuf *host_mb;
992f48a0 2907 abi_long ret = 0;
1bc012f6 2908
579a97f7
FB
2909 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2910 return -TARGET_EFAULT;
1c54ff97 2911
0d07fe47 2912 host_mb = g_malloc(msgsz+sizeof(long));
79dd77de 2913 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
1c54ff97 2914
579a97f7
FB
2915 if (ret > 0) {
2916 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2917 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2918 if (!target_mtext) {
2919 ret = -TARGET_EFAULT;
2920 goto end;
2921 }
1c54ff97 2922 memcpy(target_mb->mtext, host_mb->mtext, ret);
579a97f7
FB
2923 unlock_user(target_mtext, target_mtext_addr, ret);
2924 }
1c54ff97 2925
cbb21eed 2926 target_mb->mtype = tswapal(host_mb->mtype);
1bc012f6 2927
579a97f7
FB
2928end:
2929 if (target_mb)
2930 unlock_user_struct(target_mb, msgp, 1);
0d07fe47 2931 g_free(host_mb);
1bc012f6
TS
2932 return ret;
2933}
2934
88a8c984
RV
2935static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2936 abi_ulong target_addr)
2937{
2938 struct target_shmid_ds *target_sd;
2939
2940 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2941 return -TARGET_EFAULT;
2942 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2943 return -TARGET_EFAULT;
2944 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2945 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2946 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2947 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2948 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2949 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2950 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2951 unlock_user_struct(target_sd, target_addr, 0);
2952 return 0;
2953}
2954
2955static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2956 struct shmid_ds *host_sd)
2957{
2958 struct target_shmid_ds *target_sd;
2959
2960 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2961 return -TARGET_EFAULT;
2962 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2963 return -TARGET_EFAULT;
2964 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2965 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2966 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2967 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2968 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2969 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2970 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2971 unlock_user_struct(target_sd, target_addr, 1);
2972 return 0;
2973}
2974
2975struct target_shminfo {
2976 abi_ulong shmmax;
2977 abi_ulong shmmin;
2978 abi_ulong shmmni;
2979 abi_ulong shmseg;
2980 abi_ulong shmall;
2981};
2982
2983static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2984 struct shminfo *host_shminfo)
2985{
2986 struct target_shminfo *target_shminfo;
2987 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2988 return -TARGET_EFAULT;
2989 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2990 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2991 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2992 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2993 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2994 unlock_user_struct(target_shminfo, target_addr, 1);
2995 return 0;
2996}
2997
2998struct target_shm_info {
2999 int used_ids;
3000 abi_ulong shm_tot;
3001 abi_ulong shm_rss;
3002 abi_ulong shm_swp;
3003 abi_ulong swap_attempts;
3004 abi_ulong swap_successes;
3005};
3006
3007static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3008 struct shm_info *host_shm_info)
3009{
3010 struct target_shm_info *target_shm_info;
3011 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3012 return -TARGET_EFAULT;
3013 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3014 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3015 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3016 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3017 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3018 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3019 unlock_user_struct(target_shm_info, target_addr, 1);
3020 return 0;
3021}
3022
3023static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3024{
3025 struct shmid_ds dsarg;
3026 struct shminfo shminfo;
3027 struct shm_info shm_info;
3028 abi_long ret = -TARGET_EINVAL;
3029
3030 cmd &= 0xff;
3031
3032 switch(cmd) {
3033 case IPC_STAT:
3034 case IPC_SET:
3035 case SHM_STAT:
3036 if (target_to_host_shmid_ds(&dsarg, buf))
3037 return -TARGET_EFAULT;
3038 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3039 if (host_to_target_shmid_ds(buf, &dsarg))
3040 return -TARGET_EFAULT;
3041 break;
3042 case IPC_INFO:
3043 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3044 if (host_to_target_shminfo(buf, &shminfo))
3045 return -TARGET_EFAULT;
3046 break;
3047 case SHM_INFO:
3048 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3049 if (host_to_target_shm_info(buf, &shm_info))
3050 return -TARGET_EFAULT;
3051 break;
3052 case IPC_RMID:
3053 case SHM_LOCK:
3054 case SHM_UNLOCK:
3055 ret = get_errno(shmctl(shmid, cmd, NULL));
3056 break;
3057 }
3058
3059 return ret;
3060}
3061
3062static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3063{
3064 abi_long raddr;
3065 void *host_raddr;
3066 struct shmid_ds shm_info;
3067 int i,ret;
3068
3069 /* find out the length of the shared memory segment */
3070 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3071 if (is_error(ret)) {
3072 /* can't get length, bail out */
3073 return ret;
3074 }
3075
3076 mmap_lock();
3077
3078 if (shmaddr)
3079 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3080 else {
3081 abi_ulong mmap_start;
3082
3083 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3084
3085 if (mmap_start == -1) {
3086 errno = ENOMEM;
3087 host_raddr = (void *)-1;
3088 } else
3089 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3090 }
3091
3092 if (host_raddr == (void *)-1) {
3093 mmap_unlock();
3094 return get_errno((long)host_raddr);
3095 }
3096 raddr=h2g((unsigned long)host_raddr);
3097
3098 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3099 PAGE_VALID | PAGE_READ |
3100 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3101
3102 for (i = 0; i < N_SHM_REGIONS; i++) {
3103 if (shm_regions[i].start == 0) {
3104 shm_regions[i].start = raddr;
3105 shm_regions[i].size = shm_info.shm_segsz;
3106 break;
3107 }
3108 }
3109
3110 mmap_unlock();
3111 return raddr;
3112
3113}
3114
3115static inline abi_long do_shmdt(abi_ulong shmaddr)
3116{
3117 int i;
3118
3119 for (i = 0; i < N_SHM_REGIONS; ++i) {
3120 if (shm_regions[i].start == shmaddr) {
3121 shm_regions[i].start = 0;
e00ac249 3122 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
88a8c984
RV
3123 break;
3124 }
3125 }
3126
3127 return get_errno(shmdt(g2h(shmaddr)));
3128}
3129
1c54ff97 3130#ifdef TARGET_NR_ipc
53a5960a 3131/* ??? This only works with linear mappings. */
0da46a6e 3132/* do_ipc() must return target values and target errnos. */
992f48a0
BS
3133static abi_long do_ipc(unsigned int call, int first,
3134 int second, int third,
3135 abi_long ptr, abi_long fifth)
8853f86e
FB
3136{
3137 int version;
992f48a0 3138 abi_long ret = 0;
8853f86e
FB
3139
3140 version = call >> 16;
3141 call &= 0xffff;
3142
3143 switch (call) {
fa294816 3144 case IPCOP_semop:
e5289087 3145 ret = do_semop(first, ptr, second);
fa294816
TS
3146 break;
3147
3148 case IPCOP_semget:
3149 ret = get_errno(semget(first, second, third));
3150 break;
3151
5d2fa8eb
TM
3152 case IPCOP_semctl: {
3153 /* The semun argument to semctl is passed by value, so dereference the
3154 * ptr argument. */
3155 abi_ulong atptr;
3156 get_user_ual(atptr, (abi_ulong)ptr);
3157 ret = do_semctl(first, second, third,
3158 (union target_semun)(abi_ulong) atptr);
fa294816 3159 break;
5d2fa8eb 3160 }
d96372ef 3161
1c54ff97
AJ
3162 case IPCOP_msgget:
3163 ret = get_errno(msgget(first, second));
3164 break;
d96372ef 3165
1c54ff97
AJ
3166 case IPCOP_msgsnd:
3167 ret = do_msgsnd(first, ptr, second, third);
3168 break;
d96372ef 3169
1c54ff97
AJ
3170 case IPCOP_msgctl:
3171 ret = do_msgctl(first, second, ptr);
3172 break;
d96372ef 3173
1c54ff97
AJ
3174 case IPCOP_msgrcv:
3175 switch (version) {
3176 case 0:
3177 {
3178 struct target_ipc_kludge {
3179 abi_long msgp;
3180 abi_long msgtyp;
3181 } *tmp;
3182
3183 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3184 ret = -TARGET_EFAULT;
3185 break;
3186 }
d96372ef 3187
79dd77de 3188 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
d96372ef 3189
1c54ff97
AJ
3190 unlock_user_struct(tmp, ptr, 0);
3191 break;
3192 }
3193 default:
3194 ret = do_msgrcv(first, ptr, second, fifth, third);
3195 }
3196 break;
d96372ef 3197
8853f86e 3198 case IPCOP_shmat:
88a8c984
RV
3199 switch (version) {
3200 default:
5a4a898d
FB
3201 {
3202 abi_ulong raddr;
88a8c984
RV
3203 raddr = do_shmat(first, ptr, second);
3204 if (is_error(raddr))
3205 return get_errno(raddr);
2f619698 3206 if (put_user_ual(raddr, third))
5a4a898d 3207 return -TARGET_EFAULT;
88a8c984
RV
3208 break;
3209 }
3210 case 1:
3211 ret = -TARGET_EINVAL;
3212 break;
5a4a898d 3213 }
8853f86e
FB
3214 break;
3215 case IPCOP_shmdt:
88a8c984 3216 ret = do_shmdt(ptr);
8853f86e
FB
3217 break;
3218
3219 case IPCOP_shmget:
3220 /* IPC_* flag values are the same on all linux platforms */
3221 ret = get_errno(shmget(first, second, third));
3222 break;
3223
3224 /* IPC_* and SHM_* command values are the same on all linux platforms */
3225 case IPCOP_shmctl:
a2926784 3226 ret = do_shmctl(first, second, ptr);
8853f86e
FB
3227 break;
3228 default:
32407103 3229 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
0da46a6e 3230 ret = -TARGET_ENOSYS;
8853f86e
FB
3231 break;
3232 }
3233 return ret;
3234}
32407103 3235#endif
8853f86e 3236
31e31b8a 3237/* kernel structure types definitions */
31e31b8a 3238
001faf32 3239#define STRUCT(name, ...) STRUCT_ ## name,
31e31b8a
FB
3240#define STRUCT_SPECIAL(name) STRUCT_ ## name,
3241enum {
3242#include "syscall_types.h"
3243};
3244#undef STRUCT
3245#undef STRUCT_SPECIAL
3246
001faf32 3247#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
31e31b8a
FB
3248#define STRUCT_SPECIAL(name)
3249#include "syscall_types.h"
3250#undef STRUCT
3251#undef STRUCT_SPECIAL
3252
d2ef05bb
PM
3253typedef struct IOCTLEntry IOCTLEntry;
3254
3255typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3256 int fd, abi_long cmd, abi_long arg);
3257
3258struct IOCTLEntry {
2ab83ea7
FB
3259 unsigned int target_cmd;
3260 unsigned int host_cmd;
31e31b8a
FB
3261 const char *name;
3262 int access;
d2ef05bb 3263 do_ioctl_fn *do_ioctl;
1a9353d2 3264 const argtype arg_type[5];
d2ef05bb 3265};
31e31b8a
FB
3266
3267#define IOC_R 0x0001
3268#define IOC_W 0x0002
3269#define IOC_RW (IOC_R | IOC_W)
3270
3271#define MAX_STRUCT_SIZE 4096
3272
dace20dc 3273#ifdef CONFIG_FIEMAP
285da2b9
PM
3274/* So fiemap access checks don't overflow on 32 bit systems.
3275 * This is very slightly smaller than the limit imposed by
3276 * the underlying kernel.
3277 */
3278#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3279 / sizeof(struct fiemap_extent))
3280
3281static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3282 int fd, abi_long cmd, abi_long arg)
3283{
3284 /* The parameter for this ioctl is a struct fiemap followed
3285 * by an array of struct fiemap_extent whose size is set
3286 * in fiemap->fm_extent_count. The array is filled in by the
3287 * ioctl.
3288 */
3289 int target_size_in, target_size_out;
3290 struct fiemap *fm;
3291 const argtype *arg_type = ie->arg_type;
3292 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3293 void *argptr, *p;
3294 abi_long ret;
3295 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3296 uint32_t outbufsz;
3297 int free_fm = 0;
3298
3299 assert(arg_type[0] == TYPE_PTR);
3300 assert(ie->access == IOC_RW);
3301 arg_type++;
3302 target_size_in = thunk_type_size(arg_type, 0);
3303 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3304 if (!argptr) {
3305 return -TARGET_EFAULT;
3306 }
3307 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3308 unlock_user(argptr, arg, 0);
3309 fm = (struct fiemap *)buf_temp;
3310 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3311 return -TARGET_EINVAL;
3312 }
3313
3314 outbufsz = sizeof (*fm) +
3315 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3316
3317 if (outbufsz > MAX_STRUCT_SIZE) {
3318 /* We can't fit all the extents into the fixed size buffer.
3319 * Allocate one that is large enough and use it instead.
3320 */
3321 fm = malloc(outbufsz);
3322 if (!fm) {
3323 return -TARGET_ENOMEM;
3324 }
3325 memcpy(fm, buf_temp, sizeof(struct fiemap));
3326 free_fm = 1;
3327 }
3328 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3329 if (!is_error(ret)) {
3330 target_size_out = target_size_in;
3331 /* An extent_count of 0 means we were only counting the extents
3332 * so there are no structs to copy
3333 */
3334 if (fm->fm_extent_count != 0) {
3335 target_size_out += fm->fm_mapped_extents * extent_size;
3336 }
3337 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3338 if (!argptr) {
3339 ret = -TARGET_EFAULT;
3340 } else {
3341 /* Convert the struct fiemap */
3342 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3343 if (fm->fm_extent_count != 0) {
3344 p = argptr + target_size_in;
3345 /* ...and then all the struct fiemap_extents */
3346 for (i = 0; i < fm->fm_mapped_extents; i++) {
3347 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3348 THUNK_TARGET);
3349 p += extent_size;
3350 }
3351 }
3352 unlock_user(argptr, arg, target_size_out);
3353 }
3354 }
3355 if (free_fm) {
3356 free(fm);
3357 }
3358 return ret;
3359}
dace20dc 3360#endif
285da2b9 3361
059c2f2c
LV
3362static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3363 int fd, abi_long cmd, abi_long arg)
3364{
3365 const argtype *arg_type = ie->arg_type;
3366 int target_size;
3367 void *argptr;
3368 int ret;
3369 struct ifconf *host_ifconf;
3370 uint32_t outbufsz;
3371 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3372 int target_ifreq_size;
3373 int nb_ifreq;
3374 int free_buf = 0;
3375 int i;
3376 int target_ifc_len;
3377 abi_long target_ifc_buf;
3378 int host_ifc_len;
3379 char *host_ifc_buf;
3380
3381 assert(arg_type[0] == TYPE_PTR);
3382 assert(ie->access == IOC_RW);
3383
3384 arg_type++;
3385 target_size = thunk_type_size(arg_type, 0);
3386
3387 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3388 if (!argptr)
3389 return -TARGET_EFAULT;
3390 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3391 unlock_user(argptr, arg, 0);
3392
3393 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3394 target_ifc_len = host_ifconf->ifc_len;
3395 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3396
3397 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3398 nb_ifreq = target_ifc_len / target_ifreq_size;
3399 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3400
3401 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3402 if (outbufsz > MAX_STRUCT_SIZE) {
3403 /* We can't fit all the extents into the fixed size buffer.
3404 * Allocate one that is large enough and use it instead.
3405 */
3406 host_ifconf = malloc(outbufsz);
3407 if (!host_ifconf) {
3408 return -TARGET_ENOMEM;
3409 }
3410 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3411 free_buf = 1;
3412 }
3413 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3414
3415 host_ifconf->ifc_len = host_ifc_len;
3416 host_ifconf->ifc_buf = host_ifc_buf;
3417
3418 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3419 if (!is_error(ret)) {
3420 /* convert host ifc_len to target ifc_len */
3421
3422 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3423 target_ifc_len = nb_ifreq * target_ifreq_size;
3424 host_ifconf->ifc_len = target_ifc_len;
3425
3426 /* restore target ifc_buf */
3427
3428 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3429
3430 /* copy struct ifconf to target user */
3431
3432 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3433 if (!argptr)
3434 return -TARGET_EFAULT;
3435 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3436 unlock_user(argptr, arg, target_size);
3437
3438 /* copy ifreq[] to target user */
3439
3440 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3441 for (i = 0; i < nb_ifreq ; i++) {
3442 thunk_convert(argptr + i * target_ifreq_size,
3443 host_ifc_buf + i * sizeof(struct ifreq),
3444 ifreq_arg_type, THUNK_TARGET);
3445 }
3446 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3447 }
3448
3449 if (free_buf) {
3450 free(host_ifconf);
3451 }
3452
3453 return ret;
3454}
3455
56e904ec
AG
3456static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3457 abi_long cmd, abi_long arg)
3458{
3459 void *argptr;
3460 struct dm_ioctl *host_dm;
3461 abi_long guest_data;
3462 uint32_t guest_data_size;
3463 int target_size;
3464 const argtype *arg_type = ie->arg_type;
3465 abi_long ret;
3466 void *big_buf = NULL;
3467 char *host_data;
3468
3469 arg_type++;
3470 target_size = thunk_type_size(arg_type, 0);
3471 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3472 if (!argptr) {
3473 ret = -TARGET_EFAULT;
3474 goto out;
3475 }
3476 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3477 unlock_user(argptr, arg, 0);
3478
3479 /* buf_temp is too small, so fetch things into a bigger buffer */
3480 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3481 memcpy(big_buf, buf_temp, target_size);
3482 buf_temp = big_buf;
3483 host_dm = big_buf;
3484
3485 guest_data = arg + host_dm->data_start;
3486 if ((guest_data - arg) < 0) {
3487 ret = -EINVAL;
3488 goto out;
3489 }
3490 guest_data_size = host_dm->data_size - host_dm->data_start;
3491 host_data = (char*)host_dm + host_dm->data_start;
3492
3493 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3494 switch (ie->host_cmd) {
3495 case DM_REMOVE_ALL:
3496 case DM_LIST_DEVICES:
3497 case DM_DEV_CREATE:
3498 case DM_DEV_REMOVE:
3499 case DM_DEV_SUSPEND:
3500 case DM_DEV_STATUS:
3501 case DM_DEV_WAIT:
3502 case DM_TABLE_STATUS:
3503 case DM_TABLE_CLEAR:
3504 case DM_TABLE_DEPS:
3505 case DM_LIST_VERSIONS:
3506 /* no input data */
3507 break;
3508 case DM_DEV_RENAME:
3509 case DM_DEV_SET_GEOMETRY:
3510 /* data contains only strings */
3511 memcpy(host_data, argptr, guest_data_size);
3512 break;
3513 case DM_TARGET_MSG:
3514 memcpy(host_data, argptr, guest_data_size);
3515 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3516 break;
3517 case DM_TABLE_LOAD:
3518 {
3519 void *gspec = argptr;
3520 void *cur_data = host_data;
3521 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3522 int spec_size = thunk_type_size(arg_type, 0);
3523 int i;
3524
3525 for (i = 0; i < host_dm->target_count; i++) {
3526 struct dm_target_spec *spec = cur_data;
3527 uint32_t next;
3528 int slen;
3529
3530 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3531 slen = strlen((char*)gspec + spec_size) + 1;
3532 next = spec->next;
3533 spec->next = sizeof(*spec) + slen;
3534 strcpy((char*)&spec[1], gspec + spec_size);
3535 gspec += next;
3536 cur_data += spec->next;
3537 }
3538 break;
3539 }
3540 default:
3541 ret = -TARGET_EINVAL;
3542 goto out;
3543 }
3544 unlock_user(argptr, guest_data, 0);
3545
3546 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3547 if (!is_error(ret)) {
3548 guest_data = arg + host_dm->data_start;
3549 guest_data_size = host_dm->data_size - host_dm->data_start;
3550 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3551 switch (ie->host_cmd) {
3552 case DM_REMOVE_ALL:
3553 case DM_DEV_CREATE:
3554 case DM_DEV_REMOVE:
3555 case DM_DEV_RENAME:
3556 case DM_DEV_SUSPEND:
3557 case DM_DEV_STATUS:
3558 case DM_TABLE_LOAD:
3559 case DM_TABLE_CLEAR:
3560 case DM_TARGET_MSG:
3561 case DM_DEV_SET_GEOMETRY:
3562 /* no return data */
3563 break;
3564 case DM_LIST_DEVICES:
3565 {
3566 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3567 uint32_t remaining_data = guest_data_size;
3568 void *cur_data = argptr;
3569 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3570 int nl_size = 12; /* can't use thunk_size due to alignment */
3571
3572 while (1) {
3573 uint32_t next = nl->next;
3574 if (next) {
3575 nl->next = nl_size + (strlen(nl->name) + 1);
3576 }
3577 if (remaining_data < nl->next) {
3578 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3579 break;
3580 }
3581 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3582 strcpy(cur_data + nl_size, nl->name);
3583 cur_data += nl->next;
3584 remaining_data -= nl->next;
3585 if (!next) {
3586 break;
3587 }
3588 nl = (void*)nl + next;
3589 }
3590 break;
3591 }
3592 case DM_DEV_WAIT:
3593 case DM_TABLE_STATUS:
3594 {
3595 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3596 void *cur_data = argptr;
3597 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3598 int spec_size = thunk_type_size(arg_type, 0);
3599 int i;
3600
3601 for (i = 0; i < host_dm->target_count; i++) {
3602 uint32_t next = spec->next;
3603 int slen = strlen((char*)&spec[1]) + 1;
3604 spec->next = (cur_data - argptr) + spec_size + slen;
3605 if (guest_data_size < spec->next) {
3606 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3607 break;
3608 }
3609 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3610 strcpy(cur_data + spec_size, (char*)&spec[1]);
3611 cur_data = argptr + spec->next;
3612 spec = (void*)host_dm + host_dm->data_start + next;
3613 }
3614 break;
3615 }
3616 case DM_TABLE_DEPS:
3617 {
3618 void *hdata = (void*)host_dm + host_dm->data_start;
3619 int count = *(uint32_t*)hdata;
3620 uint64_t *hdev = hdata + 8;
3621 uint64_t *gdev = argptr + 8;
3622 int i;
3623
3624 *(uint32_t*)argptr = tswap32(count);
3625 for (i = 0; i < count; i++) {
3626 *gdev = tswap64(*hdev);
3627 gdev++;
3628 hdev++;
3629 }
3630 break;
3631 }
3632 case DM_LIST_VERSIONS:
3633 {
3634 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3635 uint32_t remaining_data = guest_data_size;
3636 void *cur_data = argptr;
3637 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3638 int vers_size = thunk_type_size(arg_type, 0);
3639
3640 while (1) {
3641 uint32_t next = vers->next;
3642 if (next) {
3643 vers->next = vers_size + (strlen(vers->name) + 1);
3644 }
3645 if (remaining_data < vers->next) {
3646 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3647 break;
3648 }
3649 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3650 strcpy(cur_data + vers_size, vers->name);
3651 cur_data += vers->next;
3652 remaining_data -= vers->next;
3653 if (!next) {
3654 break;
3655 }
3656 vers = (void*)vers + next;
3657 }
3658 break;
3659 }
3660 default:
3661 ret = -TARGET_EINVAL;
3662 goto out;
3663 }
3664 unlock_user(argptr, guest_data, guest_data_size);
3665
3666 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3667 if (!argptr) {
3668 ret = -TARGET_EFAULT;
3669 goto out;
3670 }
3671 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3672 unlock_user(argptr, arg, target_size);
3673 }
3674out:
ad11ad77 3675 g_free(big_buf);
56e904ec
AG
3676 return ret;
3677}
3678
7ff7b666
LV
3679static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
3680 int fd, abi_long cmd, abi_long arg)
3681{
3682 const argtype *arg_type = ie->arg_type;
3683 const StructEntry *se;
3684 const argtype *field_types;
3685 const int *dst_offsets, *src_offsets;
3686 int target_size;
3687 void *argptr;
3688 abi_ulong *target_rt_dev_ptr;
3689 unsigned long *host_rt_dev_ptr;
3690 abi_long ret;
3691 int i;
3692
3693 assert(ie->access == IOC_W);
3694 assert(*arg_type == TYPE_PTR);
3695 arg_type++;
3696 assert(*arg_type == TYPE_STRUCT);
3697 target_size = thunk_type_size(arg_type, 0);
3698 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3699 if (!argptr) {
3700 return -TARGET_EFAULT;
3701 }
3702 arg_type++;
3703 assert(*arg_type == (int)STRUCT_rtentry);
3704 se = struct_entries + *arg_type++;
3705 assert(se->convert[0] == NULL);
3706 /* convert struct here to be able to catch rt_dev string */
3707 field_types = se->field_types;
3708 dst_offsets = se->field_offsets[THUNK_HOST];
3709 src_offsets = se->field_offsets[THUNK_TARGET];
3710 for (i = 0; i < se->nb_fields; i++) {
3711 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
3712 assert(*field_types == TYPE_PTRVOID);
3713 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
3714 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
3715 if (*target_rt_dev_ptr != 0) {
3716 *host_rt_dev_ptr = (unsigned long)lock_user_string(
3717 tswapal(*target_rt_dev_ptr));
3718 if (!*host_rt_dev_ptr) {
3719 unlock_user(argptr, arg, 0);
3720 return -TARGET_EFAULT;
3721 }
3722 } else {
3723 *host_rt_dev_ptr = 0;
3724 }
3725 field_types++;
3726 continue;
3727 }
3728 field_types = thunk_convert(buf_temp + dst_offsets[i],
3729 argptr + src_offsets[i],
3730 field_types, THUNK_HOST);
3731 }
3732 unlock_user(argptr, arg, 0);
3733
3734 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3735 if (*host_rt_dev_ptr != 0) {
3736 unlock_user((void *)*host_rt_dev_ptr,
3737 *target_rt_dev_ptr, 0);
3738 }
3739 return ret;
3740}
3741
ca56f5b5
PB
3742static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
3743 int fd, abi_long cmd, abi_long arg)
3744{
3745 int sig = target_to_host_signal(arg);
3746 return get_errno(ioctl(fd, ie->host_cmd, sig));
3747}
3748
9f106a75 3749static IOCTLEntry ioctl_entries[] = {
001faf32 3750#define IOCTL(cmd, access, ...) \
d2ef05bb
PM
3751 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3752#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3753 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
31e31b8a
FB
3754#include "ioctls.h"
3755 { 0, 0, },
3756};
3757
53a5960a 3758/* ??? Implement proper locking for ioctls. */
0da46a6e 3759/* do_ioctl() Must return target values and target errnos. */
992f48a0 3760static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
31e31b8a
FB
3761{
3762 const IOCTLEntry *ie;
3763 const argtype *arg_type;
992f48a0 3764 abi_long ret;
31e31b8a 3765 uint8_t buf_temp[MAX_STRUCT_SIZE];
53a5960a
PB
3766 int target_size;
3767 void *argptr;
31e31b8a
FB
3768
3769 ie = ioctl_entries;
3770 for(;;) {
3771 if (ie->target_cmd == 0) {
32407103 3772 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
0da46a6e 3773 return -TARGET_ENOSYS;
31e31b8a
FB
3774 }
3775 if (ie->target_cmd == cmd)
3776 break;
3777 ie++;
3778 }
3779 arg_type = ie->arg_type;
9de5e440 3780#if defined(DEBUG)
32407103 3781 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
72f03900 3782#endif
d2ef05bb
PM
3783 if (ie->do_ioctl) {
3784 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3785 }
3786
31e31b8a
FB
3787 switch(arg_type[0]) {
3788 case TYPE_NULL:
3789 /* no argument */
3790 ret = get_errno(ioctl(fd, ie->host_cmd));
3791 break;
3792 case TYPE_PTRVOID:
3793 case TYPE_INT:
3794 /* int argment */
3795 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3796 break;
3797 case TYPE_PTR:
3798 arg_type++;
53a5960a 3799 target_size = thunk_type_size(arg_type, 0);
31e31b8a
FB
3800 switch(ie->access) {
3801 case IOC_R:
3802 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3803 if (!is_error(ret)) {
579a97f7
FB
3804 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3805 if (!argptr)
3806 return -TARGET_EFAULT;
53a5960a
PB
3807 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3808 unlock_user(argptr, arg, target_size);
31e31b8a
FB
3809 }
3810 break;
3811 case IOC_W:
579a97f7
FB
3812 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3813 if (!argptr)
3814 return -TARGET_EFAULT;
53a5960a
PB
3815 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3816 unlock_user(argptr, arg, 0);
31e31b8a
FB
3817 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3818 break;
3819 default:
3820 case IOC_RW:
579a97f7
FB
3821 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3822 if (!argptr)
3823 return -TARGET_EFAULT;
53a5960a
PB
3824 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3825 unlock_user(argptr, arg, 0);
31e31b8a
FB
3826 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3827 if (!is_error(ret)) {
579a97f7
FB
3828 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3829 if (!argptr)
3830 return -TARGET_EFAULT;
53a5960a
PB
3831 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3832 unlock_user(argptr, arg, target_size);
31e31b8a
FB
3833 }
3834 break;
3835 }
3836 break;
3837 default:
32407103
JM
3838 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3839 (long)cmd, arg_type[0]);
0da46a6e 3840 ret = -TARGET_ENOSYS;
31e31b8a
FB
3841 break;
3842 }
3843 return ret;
3844}
3845
b39bc503 3846static const bitmask_transtbl iflag_tbl[] = {
31e31b8a
FB
3847 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3848 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3849 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3850 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3851 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3852 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3853 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3854 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3855 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3856 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3857 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3858 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3859 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3860 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3861 { 0, 0, 0, 0 }
3862};
3863
b39bc503 3864static const bitmask_transtbl oflag_tbl[] = {
31e31b8a
FB
3865 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3866 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3867 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3868 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3869 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3870 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3871 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3872 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3873 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3874 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3875 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3876 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3877 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3878 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3879 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3880 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3881 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3882 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3883 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3884 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3885 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3886 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3887 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3888 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3889 { 0, 0, 0, 0 }
3890};
3891
b39bc503 3892static const bitmask_transtbl cflag_tbl[] = {
31e31b8a
FB
3893 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3894 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3895 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3896 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3897 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3898 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3899 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3900 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3901 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3902 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3903 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3904 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3905 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3906 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3907 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3908 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3909 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3910 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3911 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3912 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3913 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3914 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3915 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3916 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3917 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3918 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3919 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3920 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3921 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3922 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3923 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3924 { 0, 0, 0, 0 }
3925};
3926
b39bc503 3927static const bitmask_transtbl lflag_tbl[] = {
31e31b8a
FB
3928 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3929 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3930 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3931 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3932 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3933 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3934 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3935 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3936 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3937 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3938 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3939 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3940 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3941 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3942 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3943 { 0, 0, 0, 0 }
3944};
3945
3946static void target_to_host_termios (void *dst, const void *src)
3947{
3948 struct host_termios *host = dst;
3949 const struct target_termios *target = src;
3b46e624 3950
5fafdf24 3951 host->c_iflag =
31e31b8a 3952 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5fafdf24 3953 host->c_oflag =
31e31b8a 3954 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5fafdf24 3955 host->c_cflag =
31e31b8a 3956 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5fafdf24 3957 host->c_lflag =
31e31b8a
FB
3958 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3959 host->c_line = target->c_line;
3b46e624 3960
44607123 3961 memset(host->c_cc, 0, sizeof(host->c_cc));
5fafdf24
TS
3962 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3963 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3b46e624 3964 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
5fafdf24 3965 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3b46e624 3966 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
5fafdf24 3967 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3b46e624 3968 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
5fafdf24 3969 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3b46e624 3970 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
5fafdf24
TS
3971 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3972 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3b46e624
TS
3973 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3974 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3975 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3976 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3977 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
5fafdf24 3978 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
31e31b8a 3979}
3b46e624 3980
31e31b8a
FB
3981static void host_to_target_termios (void *dst, const void *src)
3982{
3983 struct target_termios *target = dst;
3984 const struct host_termios *host = src;
3985
5fafdf24 3986 target->c_iflag =
31e31b8a 3987 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5fafdf24 3988 target->c_oflag =
31e31b8a 3989 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5fafdf24 3990 target->c_cflag =
31e31b8a 3991 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5fafdf24 3992 target->c_lflag =
31e31b8a
FB
3993 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3994 target->c_line = host->c_line;
3b46e624 3995
44607123 3996 memset(target->c_cc, 0, sizeof(target->c_cc));
31e31b8a
FB
3997 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3998 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3999 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
4000 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
4001 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
4002 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
4003 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
4004 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
4005 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
4006 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
4007 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
4008 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
4009 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
4010 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
4011 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
4012 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
4013 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
4014}
4015
8e853dc7 4016static const StructEntry struct_termios_def = {
31e31b8a
FB
4017 .convert = { host_to_target_termios, target_to_host_termios },
4018 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
4019 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
4020};
4021
5286db75
FB
4022static bitmask_transtbl mmap_flags_tbl[] = {
4023 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
4024 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
4025 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
4026 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
4027 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
4028 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
4029 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
4030 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
e8efd8e7
CL
4031 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE, MAP_NORESERVE,
4032 MAP_NORESERVE },
5286db75
FB
4033 { 0, 0, 0, 0 }
4034};
4035
2ab83ea7 4036#if defined(TARGET_I386)
6dbad63e
FB
4037
4038/* NOTE: there is really one LDT for all the threads */
b1d8e52e 4039static uint8_t *ldt_table;
6dbad63e 4040
03acab66 4041static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
6dbad63e
FB
4042{
4043 int size;
53a5960a 4044 void *p;
6dbad63e
FB
4045
4046 if (!ldt_table)
4047 return 0;
4048 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
4049 if (size > bytecount)
4050 size = bytecount;
579a97f7
FB
4051 p = lock_user(VERIFY_WRITE, ptr, size, 0);
4052 if (!p)
03acab66 4053 return -TARGET_EFAULT;
579a97f7 4054 /* ??? Should this by byteswapped? */
53a5960a
PB
4055 memcpy(p, ldt_table, size);
4056 unlock_user(p, ptr, size);
6dbad63e
FB
4057 return size;
4058}
4059
4060/* XXX: add locking support */
03acab66
FB
4061static abi_long write_ldt(CPUX86State *env,
4062 abi_ulong ptr, unsigned long bytecount, int oldmode)
6dbad63e
FB
4063{
4064 struct target_modify_ldt_ldt_s ldt_info;
53a5960a 4065 struct target_modify_ldt_ldt_s *target_ldt_info;
6dbad63e 4066 int seg_32bit, contents, read_exec_only, limit_in_pages;
8d18e893 4067 int seg_not_present, useable, lm;
6dbad63e
FB
4068 uint32_t *lp, entry_1, entry_2;
4069
4070 if (bytecount != sizeof(ldt_info))
03acab66 4071 return -TARGET_EINVAL;
579a97f7 4072 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
03acab66 4073 return -TARGET_EFAULT;
53a5960a 4074 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 4075 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
53a5960a
PB
4076 ldt_info.limit = tswap32(target_ldt_info->limit);
4077 ldt_info.flags = tswap32(target_ldt_info->flags);
4078 unlock_user_struct(target_ldt_info, ptr, 0);
3b46e624 4079
6dbad63e 4080 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
03acab66 4081 return -TARGET_EINVAL;
6dbad63e
FB
4082 seg_32bit = ldt_info.flags & 1;
4083 contents = (ldt_info.flags >> 1) & 3;
4084 read_exec_only = (ldt_info.flags >> 3) & 1;
4085 limit_in_pages = (ldt_info.flags >> 4) & 1;
4086 seg_not_present = (ldt_info.flags >> 5) & 1;
4087 useable = (ldt_info.flags >> 6) & 1;
8d18e893
FB
4088#ifdef TARGET_ABI32
4089 lm = 0;
4090#else
4091 lm = (ldt_info.flags >> 7) & 1;
4092#endif
6dbad63e
FB
4093 if (contents == 3) {
4094 if (oldmode)
03acab66 4095 return -TARGET_EINVAL;
6dbad63e 4096 if (seg_not_present == 0)
03acab66 4097 return -TARGET_EINVAL;
6dbad63e
FB
4098 }
4099 /* allocate the LDT */
4100 if (!ldt_table) {
e441570f
AZ
4101 env->ldt.base = target_mmap(0,
4102 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
4103 PROT_READ|PROT_WRITE,
4104 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4105 if (env->ldt.base == -1)
03acab66 4106 return -TARGET_ENOMEM;
e441570f
AZ
4107 memset(g2h(env->ldt.base), 0,
4108 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
6dbad63e 4109 env->ldt.limit = 0xffff;
e441570f 4110 ldt_table = g2h(env->ldt.base);
6dbad63e
FB
4111 }
4112
4113 /* NOTE: same code as Linux kernel */
4114 /* Allow LDTs to be cleared by the user. */
4115 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4116 if (oldmode ||
4117 (contents == 0 &&
4118 read_exec_only == 1 &&
4119 seg_32bit == 0 &&
4120 limit_in_pages == 0 &&
4121 seg_not_present == 1 &&
4122 useable == 0 )) {
4123 entry_1 = 0;
4124 entry_2 = 0;
4125 goto install;
4126 }
4127 }
3b46e624 4128
6dbad63e
FB
4129 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4130 (ldt_info.limit & 0x0ffff);
4131 entry_2 = (ldt_info.base_addr & 0xff000000) |
4132 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4133 (ldt_info.limit & 0xf0000) |
4134 ((read_exec_only ^ 1) << 9) |
4135 (contents << 10) |
4136 ((seg_not_present ^ 1) << 15) |
4137 (seg_32bit << 22) |
4138 (limit_in_pages << 23) |
8d18e893 4139 (lm << 21) |
6dbad63e
FB
4140 0x7000;
4141 if (!oldmode)
4142 entry_2 |= (useable << 20);
14ae3ba7 4143
6dbad63e
FB
4144 /* Install the new entry ... */
4145install:
4146 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4147 lp[0] = tswap32(entry_1);
4148 lp[1] = tswap32(entry_2);
4149 return 0;
4150}
4151
4152/* specific and weird i386 syscalls */
8fcd3692
BS
4153static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4154 unsigned long bytecount)
6dbad63e 4155{
03acab66 4156 abi_long ret;
3b46e624 4157
6dbad63e
FB
4158 switch (func) {
4159 case 0:
4160 ret = read_ldt(ptr, bytecount);
4161 break;
4162 case 1:
4163 ret = write_ldt(env, ptr, bytecount, 1);
4164 break;
4165 case 0x11:
4166 ret = write_ldt(env, ptr, bytecount, 0);
4167 break;
03acab66
FB
4168 default:
4169 ret = -TARGET_ENOSYS;
4170 break;
6dbad63e
FB
4171 }
4172 return ret;
4173}
1b6b029e 4174
4583f589 4175#if defined(TARGET_I386) && defined(TARGET_ABI32)
bc22eb44 4176abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
4177{
4178 uint64_t *gdt_table = g2h(env->gdt.base);
4179 struct target_modify_ldt_ldt_s ldt_info;
4180 struct target_modify_ldt_ldt_s *target_ldt_info;
4181 int seg_32bit, contents, read_exec_only, limit_in_pages;
4182 int seg_not_present, useable, lm;
4183 uint32_t *lp, entry_1, entry_2;
4184 int i;
4185
4186 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4187 if (!target_ldt_info)
4188 return -TARGET_EFAULT;
4189 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 4190 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
8d18e893
FB
4191 ldt_info.limit = tswap32(target_ldt_info->limit);
4192 ldt_info.flags = tswap32(target_ldt_info->flags);
4193 if (ldt_info.entry_number == -1) {
4194 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4195 if (gdt_table[i] == 0) {
4196 ldt_info.entry_number = i;
4197 target_ldt_info->entry_number = tswap32(i);
4198 break;
4199 }
4200 }
4201 }
4202 unlock_user_struct(target_ldt_info, ptr, 1);
4203
4204 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4205 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4206 return -TARGET_EINVAL;
4207 seg_32bit = ldt_info.flags & 1;
4208 contents = (ldt_info.flags >> 1) & 3;
4209 read_exec_only = (ldt_info.flags >> 3) & 1;
4210 limit_in_pages = (ldt_info.flags >> 4) & 1;
4211 seg_not_present = (ldt_info.flags >> 5) & 1;
4212 useable = (ldt_info.flags >> 6) & 1;
4213#ifdef TARGET_ABI32
4214 lm = 0;
4215#else
4216 lm = (ldt_info.flags >> 7) & 1;
4217#endif
4218
4219 if (contents == 3) {
4220 if (seg_not_present == 0)
4221 return -TARGET_EINVAL;
4222 }
4223
4224 /* NOTE: same code as Linux kernel */
4225 /* Allow LDTs to be cleared by the user. */
4226 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4227 if ((contents == 0 &&
4228 read_exec_only == 1 &&
4229 seg_32bit == 0 &&
4230 limit_in_pages == 0 &&
4231 seg_not_present == 1 &&
4232 useable == 0 )) {
4233 entry_1 = 0;
4234 entry_2 = 0;
4235 goto install;
4236 }
4237 }
4238
4239 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4240 (ldt_info.limit & 0x0ffff);
4241 entry_2 = (ldt_info.base_addr & 0xff000000) |
4242 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4243 (ldt_info.limit & 0xf0000) |
4244 ((read_exec_only ^ 1) << 9) |
4245 (contents << 10) |
4246 ((seg_not_present ^ 1) << 15) |
4247 (seg_32bit << 22) |
4248 (limit_in_pages << 23) |
4249 (useable << 20) |
4250 (lm << 21) |
4251 0x7000;
4252
4253 /* Install the new entry ... */
4254install:
4255 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4256 lp[0] = tswap32(entry_1);
4257 lp[1] = tswap32(entry_2);
4258 return 0;
4259}
4260
8fcd3692 4261static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
4262{
4263 struct target_modify_ldt_ldt_s *target_ldt_info;
4264 uint64_t *gdt_table = g2h(env->gdt.base);
4265 uint32_t base_addr, limit, flags;
4266 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4267 int seg_not_present, useable, lm;
4268 uint32_t *lp, entry_1, entry_2;
4269
4270 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4271 if (!target_ldt_info)
4272 return -TARGET_EFAULT;
4273 idx = tswap32(target_ldt_info->entry_number);
4274 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4275 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4276 unlock_user_struct(target_ldt_info, ptr, 1);
4277 return -TARGET_EINVAL;
4278 }
4279 lp = (uint32_t *)(gdt_table + idx);
4280 entry_1 = tswap32(lp[0]);
4281 entry_2 = tswap32(lp[1]);
4282
4283 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4284 contents = (entry_2 >> 10) & 3;
4285 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4286 seg_32bit = (entry_2 >> 22) & 1;
4287 limit_in_pages = (entry_2 >> 23) & 1;
4288 useable = (entry_2 >> 20) & 1;
4289#ifdef TARGET_ABI32
4290 lm = 0;
4291#else
4292 lm = (entry_2 >> 21) & 1;
4293#endif
4294 flags = (seg_32bit << 0) | (contents << 1) |
4295 (read_exec_only << 3) | (limit_in_pages << 4) |
4296 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4297 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4298 base_addr = (entry_1 >> 16) |
4299 (entry_2 & 0xff000000) |
4300 ((entry_2 & 0xff) << 16);
cbb21eed 4301 target_ldt_info->base_addr = tswapal(base_addr);
8d18e893
FB
4302 target_ldt_info->limit = tswap32(limit);
4303 target_ldt_info->flags = tswap32(flags);
4304 unlock_user_struct(target_ldt_info, ptr, 1);
4305 return 0;
4306}
4583f589 4307#endif /* TARGET_I386 && TARGET_ABI32 */
8d18e893 4308
d2fd1af7 4309#ifndef TARGET_ABI32
2667e71c 4310abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
d2fd1af7 4311{
1add8698 4312 abi_long ret = 0;
d2fd1af7
FB
4313 abi_ulong val;
4314 int idx;
1add8698 4315
d2fd1af7
FB
4316 switch(code) {
4317 case TARGET_ARCH_SET_GS:
4318 case TARGET_ARCH_SET_FS:
4319 if (code == TARGET_ARCH_SET_GS)
4320 idx = R_GS;
4321 else
4322 idx = R_FS;
4323 cpu_x86_load_seg(env, idx, 0);
4324 env->segs[idx].base = addr;
4325 break;
4326 case TARGET_ARCH_GET_GS:
4327 case TARGET_ARCH_GET_FS:
4328 if (code == TARGET_ARCH_GET_GS)
4329 idx = R_GS;
4330 else
4331 idx = R_FS;
4332 val = env->segs[idx].base;
4333 if (put_user(val, addr, abi_ulong))
1add8698 4334 ret = -TARGET_EFAULT;
d2fd1af7
FB
4335 break;
4336 default:
4337 ret = -TARGET_EINVAL;
4338 break;
4339 }
1add8698 4340 return ret;
d2fd1af7
FB
4341}
4342#endif
4343
2ab83ea7
FB
4344#endif /* defined(TARGET_I386) */
4345
05098a93 4346#define NEW_STACK_SIZE 0x40000
d865bab5 4347
d865bab5
PB
4348
4349static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4350typedef struct {
9349b4f9 4351 CPUArchState *env;
d865bab5
PB
4352 pthread_mutex_t mutex;
4353 pthread_cond_t cond;
4354 pthread_t thread;
4355 uint32_t tid;
4356 abi_ulong child_tidptr;
4357 abi_ulong parent_tidptr;
4358 sigset_t sigmask;
4359} new_thread_info;
4360
4361static void *clone_func(void *arg)
4362{
4363 new_thread_info *info = arg;
9349b4f9 4364 CPUArchState *env;
0d34282f 4365 CPUState *cpu;
edf8e2af 4366 TaskState *ts;
d865bab5
PB
4367
4368 env = info->env;
0d34282f 4369 cpu = ENV_GET_CPU(env);
a2247f8e 4370 thread_cpu = cpu;
0429a971 4371 ts = (TaskState *)cpu->opaque;
d865bab5 4372 info->tid = gettid();
0d34282f 4373 cpu->host_tid = info->tid;
edf8e2af 4374 task_settid(ts);
d865bab5
PB
4375 if (info->child_tidptr)
4376 put_user_u32(info->tid, info->child_tidptr);
4377 if (info->parent_tidptr)
4378 put_user_u32(info->tid, info->parent_tidptr);
4379 /* Enable signals. */
4380 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4381 /* Signal to the parent that we're ready. */
4382 pthread_mutex_lock(&info->mutex);
4383 pthread_cond_broadcast(&info->cond);
4384 pthread_mutex_unlock(&info->mutex);
4385 /* Wait until the parent has finshed initializing the tls state. */
4386 pthread_mutex_lock(&clone_lock);
4387 pthread_mutex_unlock(&clone_lock);
4388 cpu_loop(env);
4389 /* never exits */
4390 return NULL;
4391}
1b6b029e 4392
0da46a6e
TS
4393/* do_fork() Must return host values and target errnos (unlike most
4394 do_*() functions). */
9349b4f9 4395static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
d865bab5
PB
4396 abi_ulong parent_tidptr, target_ulong newtls,
4397 abi_ulong child_tidptr)
1b6b029e 4398{
0429a971 4399 CPUState *cpu = ENV_GET_CPU(env);
1b6b029e 4400 int ret;
5cd4393b 4401 TaskState *ts;
0429a971 4402 CPUState *new_cpu;
9349b4f9 4403 CPUArchState *new_env;
d865bab5
PB
4404 unsigned int nptl_flags;
4405 sigset_t sigmask;
3b46e624 4406
436d124b
AZ
4407 /* Emulate vfork() with fork() */
4408 if (flags & CLONE_VFORK)
4409 flags &= ~(CLONE_VFORK | CLONE_VM);
4410
1b6b029e 4411 if (flags & CLONE_VM) {
0429a971 4412 TaskState *parent_ts = (TaskState *)cpu->opaque;
d865bab5
PB
4413 new_thread_info info;
4414 pthread_attr_t attr;
24cb36a6 4415
7267c094 4416 ts = g_malloc0(sizeof(TaskState));
624f7979 4417 init_task_state(ts);
1b6b029e 4418 /* we create a new CPU instance. */
c5be9f08 4419 new_env = cpu_copy(env);
6e68e076
PB
4420 /* Init regs that differ from the parent. */
4421 cpu_clone_regs(new_env, newsp);
0429a971
AF
4422 new_cpu = ENV_GET_CPU(new_env);
4423 new_cpu->opaque = ts;
edf8e2af
MW
4424 ts->bprm = parent_ts->bprm;
4425 ts->info = parent_ts->info;
d865bab5
PB
4426 nptl_flags = flags;
4427 flags &= ~CLONE_NPTL_FLAGS2;
4428
c2764719
PB
4429 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4430 ts->child_tidptr = child_tidptr;
4431 }
4432
d865bab5
PB
4433 if (nptl_flags & CLONE_SETTLS)
4434 cpu_set_tls (new_env, newtls);
4435
4436 /* Grab a mutex so that thread setup appears atomic. */
4437 pthread_mutex_lock(&clone_lock);
4438
4439 memset(&info, 0, sizeof(info));
4440 pthread_mutex_init(&info.mutex, NULL);
4441 pthread_mutex_lock(&info.mutex);
4442 pthread_cond_init(&info.cond, NULL);
4443 info.env = new_env;
4444 if (nptl_flags & CLONE_CHILD_SETTID)
4445 info.child_tidptr = child_tidptr;
4446 if (nptl_flags & CLONE_PARENT_SETTID)
4447 info.parent_tidptr = parent_tidptr;
4448
4449 ret = pthread_attr_init(&attr);
48e15fc2
NF
4450 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4451 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
d865bab5
PB
4452 /* It is not safe to deliver signals until the child has finished
4453 initializing, so temporarily block all signals. */
4454 sigfillset(&sigmask);
4455 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4456
4457 ret = pthread_create(&info.thread, &attr, clone_func, &info);
c2764719 4458 /* TODO: Free new CPU state if thread creation failed. */
d865bab5
PB
4459
4460 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4461 pthread_attr_destroy(&attr);
4462 if (ret == 0) {
4463 /* Wait for the child to initialize. */
4464 pthread_cond_wait(&info.cond, &info.mutex);
4465 ret = info.tid;
4466 if (flags & CLONE_PARENT_SETTID)
4467 put_user_u32(ret, parent_tidptr);
4468 } else {
4469 ret = -1;
4470 }
4471 pthread_mutex_unlock(&info.mutex);
4472 pthread_cond_destroy(&info.cond);
4473 pthread_mutex_destroy(&info.mutex);
4474 pthread_mutex_unlock(&clone_lock);
1b6b029e
FB
4475 } else {
4476 /* if no CLONE_VM, we consider it is a fork */
d865bab5 4477 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
1b6b029e 4478 return -EINVAL;
d865bab5 4479 fork_start();
1b6b029e 4480 ret = fork();
d865bab5 4481 if (ret == 0) {
2b1319c8 4482 /* Child Process. */
d865bab5
PB
4483 cpu_clone_regs(env, newsp);
4484 fork_end(1);
2b1319c8
AJ
4485 /* There is a race condition here. The parent process could
4486 theoretically read the TID in the child process before the child
4487 tid is set. This would require using either ptrace
4488 (not implemented) or having *_tidptr to point at a shared memory
4489 mapping. We can't repeat the spinlock hack used above because
4490 the child process gets its own copy of the lock. */
d865bab5
PB
4491 if (flags & CLONE_CHILD_SETTID)
4492 put_user_u32(gettid(), child_tidptr);
4493 if (flags & CLONE_PARENT_SETTID)
4494 put_user_u32(gettid(), parent_tidptr);
0429a971 4495 ts = (TaskState *)cpu->opaque;
d865bab5
PB
4496 if (flags & CLONE_SETTLS)
4497 cpu_set_tls (env, newtls);
c2764719
PB
4498 if (flags & CLONE_CHILD_CLEARTID)
4499 ts->child_tidptr = child_tidptr;
d865bab5
PB
4500 } else {
4501 fork_end(0);
4502 }
1b6b029e
FB
4503 }
4504 return ret;
4505}
4506
5f106811
APR
4507/* warning : doesn't handle linux specific flags... */
4508static int target_to_host_fcntl_cmd(int cmd)
4509{
4510 switch(cmd) {
4511 case TARGET_F_DUPFD:
4512 case TARGET_F_GETFD:
4513 case TARGET_F_SETFD:
4514 case TARGET_F_GETFL:
4515 case TARGET_F_SETFL:
4516 return cmd;
4517 case TARGET_F_GETLK:
4518 return F_GETLK;
4519 case TARGET_F_SETLK:
4520 return F_SETLK;
4521 case TARGET_F_SETLKW:
4522 return F_SETLKW;
4523 case TARGET_F_GETOWN:
4524 return F_GETOWN;
4525 case TARGET_F_SETOWN:
4526 return F_SETOWN;
4527 case TARGET_F_GETSIG:
4528 return F_GETSIG;
4529 case TARGET_F_SETSIG:
4530 return F_SETSIG;
4531#if TARGET_ABI_BITS == 32
4532 case TARGET_F_GETLK64:
4533 return F_GETLK64;
4534 case TARGET_F_SETLK64:
4535 return F_SETLK64;
4536 case TARGET_F_SETLKW64:
4537 return F_SETLKW64;
4538#endif
7e22e546
UH
4539 case TARGET_F_SETLEASE:
4540 return F_SETLEASE;
4541 case TARGET_F_GETLEASE:
4542 return F_GETLEASE;
fbd5de9b 4543#ifdef F_DUPFD_CLOEXEC
7e22e546
UH
4544 case TARGET_F_DUPFD_CLOEXEC:
4545 return F_DUPFD_CLOEXEC;
fbd5de9b 4546#endif
7e22e546
UH
4547 case TARGET_F_NOTIFY:
4548 return F_NOTIFY;
8d5d3004
AS
4549#ifdef F_GETOWN_EX
4550 case TARGET_F_GETOWN_EX:
4551 return F_GETOWN_EX;
4552#endif
4553#ifdef F_SETOWN_EX
4554 case TARGET_F_SETOWN_EX:
4555 return F_SETOWN_EX;
4556#endif
5f106811
APR
4557 default:
4558 return -TARGET_EINVAL;
4559 }
4560 return -TARGET_EINVAL;
4561}
4562
2ba7f730
LV
4563#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4564static const bitmask_transtbl flock_tbl[] = {
4565 TRANSTBL_CONVERT(F_RDLCK),
4566 TRANSTBL_CONVERT(F_WRLCK),
4567 TRANSTBL_CONVERT(F_UNLCK),
4568 TRANSTBL_CONVERT(F_EXLCK),
4569 TRANSTBL_CONVERT(F_SHLCK),
4570 { 0, 0, 0, 0 }
4571};
4572
992f48a0 4573static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
7775e9ec
FB
4574{
4575 struct flock fl;
53a5960a 4576 struct target_flock *target_fl;
43f238d7
TS
4577 struct flock64 fl64;
4578 struct target_flock64 *target_fl64;
8d5d3004
AS
4579#ifdef F_GETOWN_EX
4580 struct f_owner_ex fox;
4581 struct target_f_owner_ex *target_fox;
4582#endif
992f48a0 4583 abi_long ret;
5f106811
APR
4584 int host_cmd = target_to_host_fcntl_cmd(cmd);
4585
4586 if (host_cmd == -TARGET_EINVAL)
4587 return host_cmd;
53a5960a 4588
7775e9ec
FB
4589 switch(cmd) {
4590 case TARGET_F_GETLK:
579a97f7
FB
4591 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4592 return -TARGET_EFAULT;
2ba7f730
LV
4593 fl.l_type =
4594 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
5813427b 4595 fl.l_whence = tswap16(target_fl->l_whence);
cbb21eed
MB
4596 fl.l_start = tswapal(target_fl->l_start);
4597 fl.l_len = tswapal(target_fl->l_len);
7e22e546 4598 fl.l_pid = tswap32(target_fl->l_pid);
5813427b 4599 unlock_user_struct(target_fl, arg, 0);
5f106811 4600 ret = get_errno(fcntl(fd, host_cmd, &fl));
7775e9ec 4601 if (ret == 0) {
579a97f7
FB
4602 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4603 return -TARGET_EFAULT;
2ba7f730
LV
4604 target_fl->l_type =
4605 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
7775e9ec 4606 target_fl->l_whence = tswap16(fl.l_whence);
cbb21eed
MB
4607 target_fl->l_start = tswapal(fl.l_start);
4608 target_fl->l_len = tswapal(fl.l_len);
7e22e546 4609 target_fl->l_pid = tswap32(fl.l_pid);
53a5960a 4610 unlock_user_struct(target_fl, arg, 1);
7775e9ec
FB
4611 }
4612 break;
3b46e624 4613
7775e9ec
FB
4614 case TARGET_F_SETLK:
4615 case TARGET_F_SETLKW:
579a97f7
FB
4616 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4617 return -TARGET_EFAULT;
2ba7f730
LV
4618 fl.l_type =
4619 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
7775e9ec 4620 fl.l_whence = tswap16(target_fl->l_whence);
cbb21eed
MB
4621 fl.l_start = tswapal(target_fl->l_start);
4622 fl.l_len = tswapal(target_fl->l_len);
7e22e546 4623 fl.l_pid = tswap32(target_fl->l_pid);
53a5960a 4624 unlock_user_struct(target_fl, arg, 0);
5f106811 4625 ret = get_errno(fcntl(fd, host_cmd, &fl));
7775e9ec 4626 break;
3b46e624 4627
7775e9ec 4628 case TARGET_F_GETLK64:
579a97f7
FB
4629 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4630 return -TARGET_EFAULT;
2ba7f730
LV
4631 fl64.l_type =
4632 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
5813427b 4633 fl64.l_whence = tswap16(target_fl64->l_whence);
cbb21eed
MB
4634 fl64.l_start = tswap64(target_fl64->l_start);
4635 fl64.l_len = tswap64(target_fl64->l_len);
7e22e546 4636 fl64.l_pid = tswap32(target_fl64->l_pid);
5813427b 4637 unlock_user_struct(target_fl64, arg, 0);
5f106811 4638 ret = get_errno(fcntl(fd, host_cmd, &fl64));
43f238d7 4639 if (ret == 0) {
579a97f7
FB
4640 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4641 return -TARGET_EFAULT;
2ba7f730
LV
4642 target_fl64->l_type =
4643 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
43f238d7 4644 target_fl64->l_whence = tswap16(fl64.l_whence);
cbb21eed
MB
4645 target_fl64->l_start = tswap64(fl64.l_start);
4646 target_fl64->l_len = tswap64(fl64.l_len);
7e22e546 4647 target_fl64->l_pid = tswap32(fl64.l_pid);
43f238d7
TS
4648 unlock_user_struct(target_fl64, arg, 1);
4649 }
9ee1fa2c 4650 break;
7775e9ec
FB
4651 case TARGET_F_SETLK64:
4652 case TARGET_F_SETLKW64:
579a97f7
FB
4653 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4654 return -TARGET_EFAULT;
2ba7f730
LV
4655 fl64.l_type =
4656 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
43f238d7 4657 fl64.l_whence = tswap16(target_fl64->l_whence);
cbb21eed
MB
4658 fl64.l_start = tswap64(target_fl64->l_start);
4659 fl64.l_len = tswap64(target_fl64->l_len);
7e22e546 4660 fl64.l_pid = tswap32(target_fl64->l_pid);
43f238d7 4661 unlock_user_struct(target_fl64, arg, 0);
5f106811 4662 ret = get_errno(fcntl(fd, host_cmd, &fl64));
7775e9ec
FB
4663 break;
4664
5f106811
APR
4665 case TARGET_F_GETFL:
4666 ret = get_errno(fcntl(fd, host_cmd, arg));
9ee1fa2c
FB
4667 if (ret >= 0) {
4668 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4669 }
ffa65c3b
FB
4670 break;
4671
5f106811
APR
4672 case TARGET_F_SETFL:
4673 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4674 break;
4675
8d5d3004
AS
4676#ifdef F_GETOWN_EX
4677 case TARGET_F_GETOWN_EX:
4678 ret = get_errno(fcntl(fd, host_cmd, &fox));
4679 if (ret >= 0) {
4680 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
4681 return -TARGET_EFAULT;
4682 target_fox->type = tswap32(fox.type);
4683 target_fox->pid = tswap32(fox.pid);
4684 unlock_user_struct(target_fox, arg, 1);
4685 }
4686 break;
4687#endif
4688
4689#ifdef F_SETOWN_EX
4690 case TARGET_F_SETOWN_EX:
4691 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
4692 return -TARGET_EFAULT;
4693 fox.type = tswap32(target_fox->type);
4694 fox.pid = tswap32(target_fox->pid);
4695 unlock_user_struct(target_fox, arg, 0);
4696 ret = get_errno(fcntl(fd, host_cmd, &fox));
4697 break;
4698#endif
4699
5f106811
APR
4700 case TARGET_F_SETOWN:
4701 case TARGET_F_GETOWN:
4702 case TARGET_F_SETSIG:
4703 case TARGET_F_GETSIG:
7e22e546
UH
4704 case TARGET_F_SETLEASE:
4705 case TARGET_F_GETLEASE:
5f106811 4706 ret = get_errno(fcntl(fd, host_cmd, arg));
ffa65c3b
FB
4707 break;
4708
7775e9ec 4709 default:
9ee1fa2c 4710 ret = get_errno(fcntl(fd, cmd, arg));
7775e9ec
FB
4711 break;
4712 }
4713 return ret;
4714}
4715
67867308 4716#ifdef USE_UID16
7775e9ec 4717
67867308
FB
4718static inline int high2lowuid(int uid)
4719{
4720 if (uid > 65535)
4721 return 65534;
4722 else
4723 return uid;
4724}
4725
4726static inline int high2lowgid(int gid)
4727{
4728 if (gid > 65535)
4729 return 65534;
4730 else
4731 return gid;
4732}
4733
4734static inline int low2highuid(int uid)
4735{
4736 if ((int16_t)uid == -1)
4737 return -1;
4738 else
4739 return uid;
4740}
4741
4742static inline int low2highgid(int gid)
4743{
4744 if ((int16_t)gid == -1)
4745 return -1;
4746 else
4747 return gid;
4748}
0c866a7e
RV
4749static inline int tswapid(int id)
4750{
4751 return tswap16(id);
4752}
76ca310a
PM
4753
4754#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
4755
0c866a7e
RV
4756#else /* !USE_UID16 */
4757static inline int high2lowuid(int uid)
4758{
4759 return uid;
4760}
4761static inline int high2lowgid(int gid)
4762{
4763 return gid;
4764}
4765static inline int low2highuid(int uid)
4766{
4767 return uid;
4768}
4769static inline int low2highgid(int gid)
4770{
4771 return gid;
4772}
4773static inline int tswapid(int id)
4774{
4775 return tswap32(id);
4776}
76ca310a
PM
4777
4778#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
4779
67867308 4780#endif /* USE_UID16 */
1b6b029e 4781
31e31b8a
FB
4782void syscall_init(void)
4783{
2ab83ea7
FB
4784 IOCTLEntry *ie;
4785 const argtype *arg_type;
4786 int size;
b92c47c1 4787 int i;
2ab83ea7 4788
001faf32 4789#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
5fafdf24 4790#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
31e31b8a
FB
4791#include "syscall_types.h"
4792#undef STRUCT
4793#undef STRUCT_SPECIAL
2ab83ea7 4794
dd6e957a
PM
4795 /* Build target_to_host_errno_table[] table from
4796 * host_to_target_errno_table[]. */
4797 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4798 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4799 }
4800
2ab83ea7
FB
4801 /* we patch the ioctl size if necessary. We rely on the fact that
4802 no ioctl has all the bits at '1' in the size field */
4803 ie = ioctl_entries;
4804 while (ie->target_cmd != 0) {
4805 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4806 TARGET_IOC_SIZEMASK) {
4807 arg_type = ie->arg_type;
4808 if (arg_type[0] != TYPE_PTR) {
5fafdf24 4809 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2ab83ea7
FB
4810 ie->target_cmd);
4811 exit(1);
4812 }
4813 arg_type++;
4814 size = thunk_type_size(arg_type, 0);
5fafdf24 4815 ie->target_cmd = (ie->target_cmd &
2ab83ea7
FB
4816 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4817 (size << TARGET_IOC_SIZESHIFT);
4818 }
b92c47c1 4819
2ab83ea7 4820 /* automatic consistency check if same arch */
872ea0c0
AZ
4821#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4822 (defined(__x86_64__) && defined(TARGET_X86_64))
4823 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4824 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4825 ie->name, ie->target_cmd, ie->host_cmd);
2ab83ea7
FB
4826 }
4827#endif
4828 ie++;
4829 }
31e31b8a 4830}
c573ff67 4831
992f48a0 4832#if TARGET_ABI_BITS == 32
ce4defa0
PB
4833static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4834{
af325d36 4835#ifdef TARGET_WORDS_BIGENDIAN
ce4defa0
PB
4836 return ((uint64_t)word0 << 32) | word1;
4837#else
4838 return ((uint64_t)word1 << 32) | word0;
4839#endif
4840}
992f48a0 4841#else /* TARGET_ABI_BITS == 32 */
32407103
JM
4842static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4843{
4844 return word0;
4845}
992f48a0 4846#endif /* TARGET_ABI_BITS != 32 */
ce4defa0
PB
4847
4848#ifdef TARGET_NR_truncate64
992f48a0
BS
4849static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4850 abi_long arg2,
4851 abi_long arg3,
4852 abi_long arg4)
ce4defa0 4853{
48e515d4 4854 if (regpairs_aligned(cpu_env)) {
ce4defa0
PB
4855 arg2 = arg3;
4856 arg3 = arg4;
48e515d4 4857 }
ce4defa0
PB
4858 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4859}
4860#endif
4861
4862#ifdef TARGET_NR_ftruncate64
992f48a0
BS
4863static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4864 abi_long arg2,
4865 abi_long arg3,
4866 abi_long arg4)
ce4defa0 4867{
48e515d4 4868 if (regpairs_aligned(cpu_env)) {
ce4defa0
PB
4869 arg2 = arg3;
4870 arg3 = arg4;
48e515d4 4871 }
ce4defa0
PB
4872 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4873}
4874#endif
4875
579a97f7
FB
4876static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4877 abi_ulong target_addr)
53a5960a
PB
4878{
4879 struct target_timespec *target_ts;
4880
579a97f7
FB
4881 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4882 return -TARGET_EFAULT;
cbb21eed
MB
4883 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4884 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
53a5960a 4885 unlock_user_struct(target_ts, target_addr, 0);
b255bfa8 4886 return 0;
53a5960a
PB
4887}
4888
579a97f7
FB
4889static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4890 struct timespec *host_ts)
53a5960a
PB
4891{
4892 struct target_timespec *target_ts;
4893
579a97f7
FB
4894 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4895 return -TARGET_EFAULT;
cbb21eed
MB
4896 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4897 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
53a5960a 4898 unlock_user_struct(target_ts, target_addr, 1);
b255bfa8 4899 return 0;
53a5960a
PB
4900}
4901
f4f1e10a
ECL
4902static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
4903 abi_ulong target_addr)
4904{
4905 struct target_itimerspec *target_itspec;
4906
4907 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
4908 return -TARGET_EFAULT;
4909 }
4910
4911 host_itspec->it_interval.tv_sec =
4912 tswapal(target_itspec->it_interval.tv_sec);
4913 host_itspec->it_interval.tv_nsec =
4914 tswapal(target_itspec->it_interval.tv_nsec);
4915 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
4916 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
4917
4918 unlock_user_struct(target_itspec, target_addr, 1);
4919 return 0;
4920}
4921
4922static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
4923 struct itimerspec *host_its)
4924{
4925 struct target_itimerspec *target_itspec;
4926
4927 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
4928 return -TARGET_EFAULT;
4929 }
4930
4931 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
4932 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
4933
4934 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
4935 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
4936
4937 unlock_user_struct(target_itspec, target_addr, 0);
4938 return 0;
4939}
4940
c065976f
PM
4941static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
4942 abi_ulong target_addr)
4943{
4944 struct target_sigevent *target_sevp;
4945
4946 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
4947 return -TARGET_EFAULT;
4948 }
4949
4950 /* This union is awkward on 64 bit systems because it has a 32 bit
4951 * integer and a pointer in it; we follow the conversion approach
4952 * used for handling sigval types in signal.c so the guest should get
4953 * the correct value back even if we did a 64 bit byteswap and it's
4954 * using the 32 bit integer.
4955 */
4956 host_sevp->sigev_value.sival_ptr =
4957 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
4958 host_sevp->sigev_signo =
4959 target_to_host_signal(tswap32(target_sevp->sigev_signo));
4960 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
4961 host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
4962
4963 unlock_user_struct(target_sevp, target_addr, 1);
4964 return 0;
4965}
4966
9d33b76b 4967#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
6a24a778
AZ
4968static inline abi_long host_to_target_stat64(void *cpu_env,
4969 abi_ulong target_addr,
4970 struct stat *host_st)
4971{
09701199 4972#if defined(TARGET_ARM) && defined(TARGET_ABI32)
6a24a778
AZ
4973 if (((CPUARMState *)cpu_env)->eabi) {
4974 struct target_eabi_stat64 *target_st;
4975
4976 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4977 return -TARGET_EFAULT;
4978 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4979 __put_user(host_st->st_dev, &target_st->st_dev);
4980 __put_user(host_st->st_ino, &target_st->st_ino);
4981#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4982 __put_user(host_st->st_ino, &target_st->__st_ino);
4983#endif
4984 __put_user(host_st->st_mode, &target_st->st_mode);
4985 __put_user(host_st->st_nlink, &target_st->st_nlink);
4986 __put_user(host_st->st_uid, &target_st->st_uid);
4987 __put_user(host_st->st_gid, &target_st->st_gid);
4988 __put_user(host_st->st_rdev, &target_st->st_rdev);
4989 __put_user(host_st->st_size, &target_st->st_size);
4990 __put_user(host_st->st_blksize, &target_st->st_blksize);
4991 __put_user(host_st->st_blocks, &target_st->st_blocks);
4992 __put_user(host_st->st_atime, &target_st->target_st_atime);
4993 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4994 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4995 unlock_user_struct(target_st, target_addr, 1);
4996 } else
4997#endif
4998 {
20d155bc 4999#if defined(TARGET_HAS_STRUCT_STAT64)
6a24a778 5000 struct target_stat64 *target_st;
20d155bc
SW
5001#else
5002 struct target_stat *target_st;
9d33b76b 5003#endif
6a24a778
AZ
5004
5005 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
5006 return -TARGET_EFAULT;
9d33b76b 5007 memset(target_st, 0, sizeof(*target_st));
6a24a778
AZ
5008 __put_user(host_st->st_dev, &target_st->st_dev);
5009 __put_user(host_st->st_ino, &target_st->st_ino);
5010#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5011 __put_user(host_st->st_ino, &target_st->__st_ino);
5012#endif
5013 __put_user(host_st->st_mode, &target_st->st_mode);
5014 __put_user(host_st->st_nlink, &target_st->st_nlink);
5015 __put_user(host_st->st_uid, &target_st->st_uid);
5016 __put_user(host_st->st_gid, &target_st->st_gid);
5017 __put_user(host_st->st_rdev, &target_st->st_rdev);
5018 /* XXX: better use of kernel struct */
5019 __put_user(host_st->st_size, &target_st->st_size);
5020 __put_user(host_st->st_blksize, &target_st->st_blksize);
5021 __put_user(host_st->st_blocks, &target_st->st_blocks);
5022 __put_user(host_st->st_atime, &target_st->target_st_atime);
5023 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
5024 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
5025 unlock_user_struct(target_st, target_addr, 1);
5026 }
5027
5028 return 0;
5029}
5030#endif
5031
bd0c5661
PB
5032/* ??? Using host futex calls even when target atomic operations
5033 are not really atomic probably breaks things. However implementing
5034 futexes locally would make futexes shared between multiple processes
5035 tricky. However they're probably useless because guest atomic
5036 operations won't work either. */
8fcd3692
BS
5037static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
5038 target_ulong uaddr2, int val3)
bd0c5661
PB
5039{
5040 struct timespec ts, *pts;
a16aae0c 5041 int base_op;
bd0c5661
PB
5042
5043 /* ??? We assume FUTEX_* constants are the same on both host
5044 and target. */
a29ccd63 5045#ifdef FUTEX_CMD_MASK
a16aae0c 5046 base_op = op & FUTEX_CMD_MASK;
a29ccd63 5047#else
a16aae0c 5048 base_op = op;
a29ccd63 5049#endif
a16aae0c 5050 switch (base_op) {
bd0c5661 5051 case FUTEX_WAIT:
cce246e0 5052 case FUTEX_WAIT_BITSET:
bd0c5661
PB
5053 if (timeout) {
5054 pts = &ts;
5055 target_to_host_timespec(pts, timeout);
5056 } else {
5057 pts = NULL;
5058 }
a29ccd63 5059 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
cce246e0 5060 pts, NULL, val3));
bd0c5661 5061 case FUTEX_WAKE:
a29ccd63 5062 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
bd0c5661 5063 case FUTEX_FD:
a29ccd63 5064 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
bd0c5661 5065 case FUTEX_REQUEUE:
bd0c5661 5066 case FUTEX_CMP_REQUEUE:
a16aae0c
NF
5067 case FUTEX_WAKE_OP:
5068 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
5069 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
5070 But the prototype takes a `struct timespec *'; insert casts
5071 to satisfy the compiler. We do not need to tswap TIMEOUT
5072 since it's not compared to guest memory. */
5073 pts = (struct timespec *)(uintptr_t) timeout;
5074 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
5075 g2h(uaddr2),
5076 (base_op == FUTEX_CMP_REQUEUE
5077 ? tswap32(val3)
5078 : val3)));
bd0c5661
PB
5079 default:
5080 return -TARGET_ENOSYS;
5081 }
5082}
bd0c5661 5083
1d9d8b55
PB
5084/* Map host to target signal numbers for the wait family of syscalls.
5085 Assume all other status bits are the same. */
a05c6409 5086int host_to_target_waitstatus(int status)
1d9d8b55
PB
5087{
5088 if (WIFSIGNALED(status)) {
5089 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
5090 }
5091 if (WIFSTOPPED(status)) {
5092 return (host_to_target_signal(WSTOPSIG(status)) << 8)
5093 | (status & 0xff);
5094 }
5095 return status;
5096}
5097
76b94245
WVS
5098static int open_self_cmdline(void *cpu_env, int fd)
5099{
5100 int fd_orig = -1;
5101 bool word_skipped = false;
5102
5103 fd_orig = open("/proc/self/cmdline", O_RDONLY);
5104 if (fd_orig < 0) {
5105 return fd_orig;
5106 }
5107
5108 while (true) {
5109 ssize_t nb_read;
5110 char buf[128];
5111 char *cp_buf = buf;
5112
5113 nb_read = read(fd_orig, buf, sizeof(buf));
5114 if (nb_read < 0) {
5115 fd_orig = close(fd_orig);
5116 return -1;
5117 } else if (nb_read == 0) {
5118 break;
5119 }
5120
5121 if (!word_skipped) {
5122 /* Skip the first string, which is the path to qemu-*-static
5123 instead of the actual command. */
5124 cp_buf = memchr(buf, 0, sizeof(buf));
5125 if (cp_buf) {
5126 /* Null byte found, skip one string */
5127 cp_buf++;
5128 nb_read -= cp_buf - buf;
5129 word_skipped = true;
5130 }
5131 }
5132
5133 if (word_skipped) {
5134 if (write(fd, cp_buf, nb_read) != nb_read) {
5135 return -1;
5136 }
5137 }
5138 }
5139
5140 return close(fd_orig);
5141}
5142
36c08d49
AG
5143static int open_self_maps(void *cpu_env, int fd)
5144{
0429a971
AF
5145 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5146 TaskState *ts = cpu->opaque;
1a49ef2a
AG
5147 FILE *fp;
5148 char *line = NULL;
5149 size_t len = 0;
5150 ssize_t read;
5151
5152 fp = fopen("/proc/self/maps", "r");
5153 if (fp == NULL) {
5154 return -EACCES;
5155 }
36c08d49 5156
1a49ef2a
AG
5157 while ((read = getline(&line, &len, fp)) != -1) {
5158 int fields, dev_maj, dev_min, inode;
5159 uint64_t min, max, offset;
5160 char flag_r, flag_w, flag_x, flag_p;
5161 char path[512] = "";
5162 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
5163 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
5164 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
5165
5166 if ((fields < 10) || (fields > 11)) {
5167 continue;
5168 }
d67f4aaa
MI
5169 if (h2g_valid(min)) {
5170 int flags = page_get_flags(h2g(min));
5171 max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX);
5172 if (page_check_range(h2g(min), max - min, flags) == -1) {
5173 continue;
5174 }
5175 if (h2g(min) == ts->info->stack_limit) {
5176 pstrcpy(path, sizeof(path), " [stack]");
5177 }
1a49ef2a 5178 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
e24fed4e 5179 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
d67f4aaa 5180 h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
1a49ef2a 5181 flag_x, flag_p, offset, dev_maj, dev_min, inode,
e24fed4e 5182 path[0] ? " " : "", path);
1a49ef2a
AG
5183 }
5184 }
5185
5186 free(line);
5187 fclose(fp);
5188
36c08d49
AG
5189 return 0;
5190}
5191
480b8e7d
AG
5192static int open_self_stat(void *cpu_env, int fd)
5193{
0429a971
AF
5194 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5195 TaskState *ts = cpu->opaque;
480b8e7d
AG
5196 abi_ulong start_stack = ts->info->start_stack;
5197 int i;
5198
5199 for (i = 0; i < 44; i++) {
5200 char buf[128];
5201 int len;
5202 uint64_t val = 0;
5203
e0e65bee
FE
5204 if (i == 0) {
5205 /* pid */
5206 val = getpid();
5207 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5208 } else if (i == 1) {
5209 /* app name */
5210 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5211 } else if (i == 27) {
5212 /* stack bottom */
5213 val = start_stack;
5214 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5215 } else {
5216 /* for the rest, there is MasterCard */
5217 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
480b8e7d 5218 }
e0e65bee 5219
480b8e7d
AG
5220 len = strlen(buf);
5221 if (write(fd, buf, len) != len) {
5222 return -1;
5223 }
5224 }
5225
5226 return 0;
5227}
5228
257450ee
AG
5229static int open_self_auxv(void *cpu_env, int fd)
5230{
0429a971
AF
5231 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
5232 TaskState *ts = cpu->opaque;
257450ee
AG
5233 abi_ulong auxv = ts->info->saved_auxv;
5234 abi_ulong len = ts->info->auxv_len;
5235 char *ptr;
5236
5237 /*
5238 * Auxiliary vector is stored in target process stack.
5239 * read in whole auxv vector and copy it to file
5240 */
5241 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5242 if (ptr != NULL) {
5243 while (len > 0) {
5244 ssize_t r;
5245 r = write(fd, ptr, len);
5246 if (r <= 0) {
5247 break;
5248 }
5249 len -= r;
5250 ptr += r;
5251 }
5252 lseek(fd, 0, SEEK_SET);
5253 unlock_user(ptr, auxv, len);
5254 }
5255
5256 return 0;
5257}
5258
463d8e73
AS
5259static int is_proc_myself(const char *filename, const char *entry)
5260{
5261 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
5262 filename += strlen("/proc/");
5263 if (!strncmp(filename, "self/", strlen("self/"))) {
5264 filename += strlen("self/");
5265 } else if (*filename >= '1' && *filename <= '9') {
5266 char myself[80];
5267 snprintf(myself, sizeof(myself), "%d/", getpid());
5268 if (!strncmp(filename, myself, strlen(myself))) {
5269 filename += strlen(myself);
5270 } else {
5271 return 0;
5272 }
5273 } else {
5274 return 0;
5275 }
5276 if (!strcmp(filename, entry)) {
5277 return 1;
5278 }
5279 }
5280 return 0;
5281}
5282
de6b9933
LV
5283#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5284static int is_proc(const char *filename, const char *entry)
5285{
5286 return strcmp(filename, entry) == 0;
5287}
5288
5289static int open_net_route(void *cpu_env, int fd)
5290{
5291 FILE *fp;
5292 char *line = NULL;
5293 size_t len = 0;
5294 ssize_t read;
5295
5296 fp = fopen("/proc/net/route", "r");
5297 if (fp == NULL) {
5298 return -EACCES;
5299 }
5300
5301 /* read header */
5302
5303 read = getline(&line, &len, fp);
5304 dprintf(fd, "%s", line);
5305
5306 /* read routes */
5307
5308 while ((read = getline(&line, &len, fp)) != -1) {
5309 char iface[16];
5310 uint32_t dest, gw, mask;
5311 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
5312 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5313 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
5314 &mask, &mtu, &window, &irtt);
5315 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5316 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
5317 metric, tswap32(mask), mtu, window, irtt);
5318 }
5319
5320 free(line);
5321 fclose(fp);
5322
5323 return 0;
5324}
5325#endif
5326
0b2effd7 5327static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
3be14d05
AG
5328{
5329 struct fake_open {
5330 const char *filename;
5331 int (*fill)(void *cpu_env, int fd);
de6b9933 5332 int (*cmp)(const char *s1, const char *s2);
3be14d05
AG
5333 };
5334 const struct fake_open *fake_open;
5335 static const struct fake_open fakes[] = {
de6b9933
LV
5336 { "maps", open_self_maps, is_proc_myself },
5337 { "stat", open_self_stat, is_proc_myself },
5338 { "auxv", open_self_auxv, is_proc_myself },
76b94245 5339 { "cmdline", open_self_cmdline, is_proc_myself },
de6b9933
LV
5340#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5341 { "/proc/net/route", open_net_route, is_proc },
5342#endif
5343 { NULL, NULL, NULL }
3be14d05
AG
5344 };
5345
aa07f5ec
MO
5346 if (is_proc_myself(pathname, "exe")) {
5347 int execfd = qemu_getauxval(AT_EXECFD);
0b2effd7 5348 return execfd ? execfd : get_errno(sys_openat(dirfd, exec_path, flags, mode));
aa07f5ec
MO
5349 }
5350
3be14d05 5351 for (fake_open = fakes; fake_open->filename; fake_open++) {
de6b9933 5352 if (fake_open->cmp(pathname, fake_open->filename)) {
3be14d05
AG
5353 break;
5354 }
5355 }
5356
5357 if (fake_open->filename) {
5358 const char *tmpdir;
5359 char filename[PATH_MAX];
5360 int fd, r;
5361
5362 /* create temporary file to map stat to */
5363 tmpdir = getenv("TMPDIR");
5364 if (!tmpdir)
5365 tmpdir = "/tmp";
5366 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5367 fd = mkstemp(filename);
5368 if (fd < 0) {
5369 return fd;
5370 }
5371 unlink(filename);
5372
5373 if ((r = fake_open->fill(cpu_env, fd))) {
5374 close(fd);
5375 return r;
5376 }
5377 lseek(fd, 0, SEEK_SET);
5378
5379 return fd;
5380 }
5381
0b2effd7 5382 return get_errno(sys_openat(dirfd, path(pathname), flags, mode));
3be14d05
AG
5383}
5384
0da46a6e
TS
5385/* do_syscall() should always have a single exit point at the end so
5386 that actions, such as logging of syscall results, can be performed.
5387 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
992f48a0
BS
5388abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5389 abi_long arg2, abi_long arg3, abi_long arg4,
5945cfcb
PM
5390 abi_long arg5, abi_long arg6, abi_long arg7,
5391 abi_long arg8)
31e31b8a 5392{
182735ef 5393 CPUState *cpu = ENV_GET_CPU(cpu_env);
992f48a0 5394 abi_long ret;
31e31b8a 5395 struct stat st;
56c8f68f 5396 struct statfs stfs;
53a5960a 5397 void *p;
3b46e624 5398
72f03900 5399#ifdef DEBUG
c573ff67 5400 gemu_log("syscall %d", num);
72f03900 5401#endif
b92c47c1
TS
5402 if(do_strace)
5403 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5404
31e31b8a
FB
5405 switch(num) {
5406 case TARGET_NR_exit:
9b056fcc
AF
5407 /* In old applications this may be used to implement _exit(2).
5408 However in threaded applictions it is used for thread termination,
5409 and _exit_group is used for application termination.
5410 Do thread termination if we have more then one thread. */
5411 /* FIXME: This probably breaks if a signal arrives. We should probably
5412 be disabling signals. */
bdc44640 5413 if (CPU_NEXT(first_cpu)) {
9b056fcc 5414 TaskState *ts;
9b056fcc
AF
5415
5416 cpu_list_lock();
9b056fcc 5417 /* Remove the CPU from the list. */
bdc44640 5418 QTAILQ_REMOVE(&cpus, cpu, node);
9b056fcc 5419 cpu_list_unlock();
0429a971 5420 ts = cpu->opaque;
9b056fcc
AF
5421 if (ts->child_tidptr) {
5422 put_user_u32(0, ts->child_tidptr);
5423 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5424 NULL, NULL, 0);
5425 }
a2247f8e 5426 thread_cpu = NULL;
0429a971 5427 object_unref(OBJECT(cpu));
9b056fcc
AF
5428 g_free(ts);
5429 pthread_exit(NULL);
5430 }
9788c9ca 5431#ifdef TARGET_GPROF
7d13299d
FB
5432 _mcleanup();
5433#endif
e9009676 5434 gdb_exit(cpu_env, arg1);
c2764719 5435 _exit(arg1);
31e31b8a
FB
5436 ret = 0; /* avoid warning */
5437 break;
5438 case TARGET_NR_read:
38d840e6
AJ
5439 if (arg3 == 0)
5440 ret = 0;
5441 else {
5442 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5443 goto efault;
5444 ret = get_errno(read(arg1, p, arg3));
5445 unlock_user(p, arg2, ret);
5446 }
31e31b8a
FB
5447 break;
5448 case TARGET_NR_write:
579a97f7
FB
5449 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5450 goto efault;
53a5960a
PB
5451 ret = get_errno(write(arg1, p, arg3));
5452 unlock_user(p, arg2, 0);
31e31b8a
FB
5453 break;
5454 case TARGET_NR_open:
2f619698
FB
5455 if (!(p = lock_user_string(arg1)))
5456 goto efault;
0b2effd7
RV
5457 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
5458 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5459 arg3));
53a5960a 5460 unlock_user(p, arg1, 0);
31e31b8a 5461 break;
82424832 5462 case TARGET_NR_openat:
579a97f7
FB
5463 if (!(p = lock_user_string(arg2)))
5464 goto efault;
0b2effd7
RV
5465 ret = get_errno(do_openat(cpu_env, arg1, p,
5466 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5467 arg4));
579a97f7 5468 unlock_user(p, arg2, 0);
82424832 5469 break;
31e31b8a
FB
5470 case TARGET_NR_close:
5471 ret = get_errno(close(arg1));
5472 break;
5473 case TARGET_NR_brk:
53a5960a 5474 ret = do_brk(arg1);
31e31b8a
FB
5475 break;
5476 case TARGET_NR_fork:
d865bab5 5477 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
31e31b8a 5478 break;
e5febef5 5479#ifdef TARGET_NR_waitpid
31e31b8a
FB
5480 case TARGET_NR_waitpid:
5481 {
53a5960a
PB
5482 int status;
5483 ret = get_errno(waitpid(arg1, &status, arg3));
5379557b 5484 if (!is_error(ret) && arg2 && ret
1d9d8b55 5485 && put_user_s32(host_to_target_waitstatus(status), arg2))
2f619698 5486 goto efault;
31e31b8a
FB
5487 }
5488 break;
e5febef5 5489#endif
f0cbb613
PB
5490#ifdef TARGET_NR_waitid
5491 case TARGET_NR_waitid:
5492 {
5493 siginfo_t info;
5494 info.si_pid = 0;
5495 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5496 if (!is_error(ret) && arg3 && info.si_pid != 0) {
c227f099 5497 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
f0cbb613
PB
5498 goto efault;
5499 host_to_target_siginfo(p, &info);
c227f099 5500 unlock_user(p, arg3, sizeof(target_siginfo_t));
f0cbb613
PB
5501 }
5502 }
5503 break;
5504#endif
7a3148a9 5505#ifdef TARGET_NR_creat /* not on alpha */
31e31b8a 5506 case TARGET_NR_creat:
579a97f7
FB
5507 if (!(p = lock_user_string(arg1)))
5508 goto efault;
53a5960a
PB
5509 ret = get_errno(creat(p, arg2));
5510 unlock_user(p, arg1, 0);
31e31b8a 5511 break;
7a3148a9 5512#endif
31e31b8a 5513 case TARGET_NR_link:
53a5960a
PB
5514 {
5515 void * p2;
5516 p = lock_user_string(arg1);
5517 p2 = lock_user_string(arg2);
579a97f7
FB
5518 if (!p || !p2)
5519 ret = -TARGET_EFAULT;
5520 else
5521 ret = get_errno(link(p, p2));
53a5960a
PB
5522 unlock_user(p2, arg2, 0);
5523 unlock_user(p, arg1, 0);
5524 }
31e31b8a 5525 break;
c0d472b1 5526#if defined(TARGET_NR_linkat)
64f0ce4c 5527 case TARGET_NR_linkat:
64f0ce4c
TS
5528 {
5529 void * p2 = NULL;
579a97f7
FB
5530 if (!arg2 || !arg4)
5531 goto efault;
64f0ce4c
TS
5532 p = lock_user_string(arg2);
5533 p2 = lock_user_string(arg4);
579a97f7 5534 if (!p || !p2)
0da46a6e 5535 ret = -TARGET_EFAULT;
64f0ce4c 5536 else
c0d472b1 5537 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
579a97f7
FB
5538 unlock_user(p, arg2, 0);
5539 unlock_user(p2, arg4, 0);
64f0ce4c
TS
5540 }
5541 break;
5542#endif
31e31b8a 5543 case TARGET_NR_unlink:
579a97f7
FB
5544 if (!(p = lock_user_string(arg1)))
5545 goto efault;
53a5960a
PB
5546 ret = get_errno(unlink(p));
5547 unlock_user(p, arg1, 0);
31e31b8a 5548 break;
c0d472b1 5549#if defined(TARGET_NR_unlinkat)
8170f56b 5550 case TARGET_NR_unlinkat:
579a97f7
FB
5551 if (!(p = lock_user_string(arg2)))
5552 goto efault;
c0d472b1 5553 ret = get_errno(unlinkat(arg1, p, arg3));
579a97f7 5554 unlock_user(p, arg2, 0);
ed494d87 5555 break;
b7d35e65 5556#endif
31e31b8a 5557 case TARGET_NR_execve:
7854b056
FB
5558 {
5559 char **argp, **envp;
f7341ff4 5560 int argc, envc;
992f48a0
BS
5561 abi_ulong gp;
5562 abi_ulong guest_argp;
5563 abi_ulong guest_envp;
5564 abi_ulong addr;
7854b056 5565 char **q;
a6f79cc9 5566 int total_size = 0;
7854b056 5567
f7341ff4 5568 argc = 0;
53a5960a 5569 guest_argp = arg2;
da94d263 5570 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
03aa1976 5571 if (get_user_ual(addr, gp))
2f619698 5572 goto efault;
03aa1976 5573 if (!addr)
2f619698 5574 break;
7854b056 5575 argc++;
2f619698 5576 }
f7341ff4 5577 envc = 0;
53a5960a 5578 guest_envp = arg3;
da94d263 5579 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
03aa1976 5580 if (get_user_ual(addr, gp))
2f619698 5581 goto efault;
03aa1976 5582 if (!addr)
2f619698 5583 break;
7854b056 5584 envc++;
2f619698 5585 }
7854b056 5586
f7341ff4
FB
5587 argp = alloca((argc + 1) * sizeof(void *));
5588 envp = alloca((envc + 1) * sizeof(void *));
7854b056 5589
da94d263 5590 for (gp = guest_argp, q = argp; gp;
992f48a0 5591 gp += sizeof(abi_ulong), q++) {
2f619698
FB
5592 if (get_user_ual(addr, gp))
5593 goto execve_efault;
53a5960a
PB
5594 if (!addr)
5595 break;
2f619698
FB
5596 if (!(*q = lock_user_string(addr)))
5597 goto execve_efault;
a6f79cc9 5598 total_size += strlen(*q) + 1;
53a5960a 5599 }
f7341ff4
FB
5600 *q = NULL;
5601
da94d263 5602 for (gp = guest_envp, q = envp; gp;
992f48a0 5603 gp += sizeof(abi_ulong), q++) {
2f619698
FB
5604 if (get_user_ual(addr, gp))
5605 goto execve_efault;
53a5960a
PB
5606 if (!addr)
5607 break;
2f619698
FB
5608 if (!(*q = lock_user_string(addr)))
5609 goto execve_efault;
a6f79cc9 5610 total_size += strlen(*q) + 1;
53a5960a 5611 }
f7341ff4 5612 *q = NULL;
7854b056 5613
a6f79cc9
UH
5614 /* This case will not be caught by the host's execve() if its
5615 page size is bigger than the target's. */
5616 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5617 ret = -TARGET_E2BIG;
5618 goto execve_end;
5619 }
2f619698
FB
5620 if (!(p = lock_user_string(arg1)))
5621 goto execve_efault;
53a5960a
PB
5622 ret = get_errno(execve(p, argp, envp));
5623 unlock_user(p, arg1, 0);
5624
2f619698
FB
5625 goto execve_end;
5626
5627 execve_efault:
5628 ret = -TARGET_EFAULT;
5629
5630 execve_end:
53a5960a 5631 for (gp = guest_argp, q = argp; *q;
992f48a0 5632 gp += sizeof(abi_ulong), q++) {
2f619698
FB
5633 if (get_user_ual(addr, gp)
5634 || !addr)
5635 break;
53a5960a
PB
5636 unlock_user(*q, addr, 0);
5637 }
5638 for (gp = guest_envp, q = envp; *q;
992f48a0 5639 gp += sizeof(abi_ulong), q++) {
2f619698
FB
5640 if (get_user_ual(addr, gp)
5641 || !addr)
5642 break;
53a5960a
PB
5643 unlock_user(*q, addr, 0);
5644 }
7854b056 5645 }
31e31b8a
FB
5646 break;
5647 case TARGET_NR_chdir:
579a97f7
FB
5648 if (!(p = lock_user_string(arg1)))
5649 goto efault;
53a5960a
PB
5650 ret = get_errno(chdir(p));
5651 unlock_user(p, arg1, 0);
31e31b8a 5652 break;
a315a145 5653#ifdef TARGET_NR_time
31e31b8a
FB
5654 case TARGET_NR_time:
5655 {
53a5960a
PB
5656 time_t host_time;
5657 ret = get_errno(time(&host_time));
2f619698
FB
5658 if (!is_error(ret)
5659 && arg1
5660 && put_user_sal(host_time, arg1))
5661 goto efault;
31e31b8a
FB
5662 }
5663 break;
a315a145 5664#endif
31e31b8a 5665 case TARGET_NR_mknod:
579a97f7
FB
5666 if (!(p = lock_user_string(arg1)))
5667 goto efault;
53a5960a
PB
5668 ret = get_errno(mknod(p, arg2, arg3));
5669 unlock_user(p, arg1, 0);
31e31b8a 5670 break;
c0d472b1 5671#if defined(TARGET_NR_mknodat)
75ac37a0 5672 case TARGET_NR_mknodat:
579a97f7
FB
5673 if (!(p = lock_user_string(arg2)))
5674 goto efault;
c0d472b1 5675 ret = get_errno(mknodat(arg1, p, arg3, arg4));
579a97f7 5676 unlock_user(p, arg2, 0);
75ac37a0
TS
5677 break;
5678#endif
31e31b8a 5679 case TARGET_NR_chmod:
579a97f7
FB
5680 if (!(p = lock_user_string(arg1)))
5681 goto efault;
53a5960a
PB
5682 ret = get_errno(chmod(p, arg2));
5683 unlock_user(p, arg1, 0);
31e31b8a 5684 break;
ebc05488 5685#ifdef TARGET_NR_break
31e31b8a
FB
5686 case TARGET_NR_break:
5687 goto unimplemented;
ebc05488
FB
5688#endif
5689#ifdef TARGET_NR_oldstat
31e31b8a
FB
5690 case TARGET_NR_oldstat:
5691 goto unimplemented;
ebc05488 5692#endif
31e31b8a
FB
5693 case TARGET_NR_lseek:
5694 ret = get_errno(lseek(arg1, arg2, arg3));
5695 break;
9231733a
RH
5696#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5697 /* Alpha specific */
7a3148a9 5698 case TARGET_NR_getxpid:
9231733a
RH
5699 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
5700 ret = get_errno(getpid());
5701 break;
7a3148a9 5702#endif
9231733a
RH
5703#ifdef TARGET_NR_getpid
5704 case TARGET_NR_getpid:
31e31b8a
FB
5705 ret = get_errno(getpid());
5706 break;
9231733a 5707#endif
31e31b8a 5708 case TARGET_NR_mount:
356d771b
PB
5709 {
5710 /* need to look at the data field */
5711 void *p2, *p3;
5712
5713 if (arg1) {
5714 p = lock_user_string(arg1);
5715 if (!p) {
5716 goto efault;
5717 }
5718 } else {
5719 p = NULL;
5720 }
5721
5722 p2 = lock_user_string(arg2);
5723 if (!p2) {
5724 if (arg1) {
5725 unlock_user(p, arg1, 0);
5726 }
5727 goto efault;
5728 }
5729
5730 if (arg3) {
5731 p3 = lock_user_string(arg3);
5732 if (!p3) {
5733 if (arg1) {
579a97f7 5734 unlock_user(p, arg1, 0);
356d771b
PB
5735 }
5736 unlock_user(p2, arg2, 0);
5737 goto efault;
5738 }
5739 } else {
5740 p3 = NULL;
5741 }
5742
5743 /* FIXME - arg5 should be locked, but it isn't clear how to
5744 * do that since it's not guaranteed to be a NULL-terminated
5745 * string.
5746 */
5747 if (!arg5) {
5748 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
5749 } else {
5750 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
5751 }
5752 ret = get_errno(ret);
5753
5754 if (arg1) {
5755 unlock_user(p, arg1, 0);
5756 }
5757 unlock_user(p2, arg2, 0);
5758 if (arg3) {
5759 unlock_user(p3, arg3, 0);
5760 }
5761 }
5762 break;
e5febef5 5763#ifdef TARGET_NR_umount
31e31b8a 5764 case TARGET_NR_umount:
579a97f7
FB
5765 if (!(p = lock_user_string(arg1)))
5766 goto efault;
53a5960a
PB
5767 ret = get_errno(umount(p));
5768 unlock_user(p, arg1, 0);
31e31b8a 5769 break;
e5febef5 5770#endif
7a3148a9 5771#ifdef TARGET_NR_stime /* not on alpha */
31e31b8a
FB
5772 case TARGET_NR_stime:
5773 {
53a5960a 5774 time_t host_time;
2f619698
FB
5775 if (get_user_sal(host_time, arg1))
5776 goto efault;
53a5960a 5777 ret = get_errno(stime(&host_time));
31e31b8a
FB
5778 }
5779 break;
7a3148a9 5780#endif
31e31b8a
FB
5781 case TARGET_NR_ptrace:
5782 goto unimplemented;
7a3148a9 5783#ifdef TARGET_NR_alarm /* not on alpha */
31e31b8a
FB
5784 case TARGET_NR_alarm:
5785 ret = alarm(arg1);
5786 break;
7a3148a9 5787#endif
ebc05488 5788#ifdef TARGET_NR_oldfstat
31e31b8a
FB
5789 case TARGET_NR_oldfstat:
5790 goto unimplemented;
ebc05488 5791#endif
7a3148a9 5792#ifdef TARGET_NR_pause /* not on alpha */
31e31b8a
FB
5793 case TARGET_NR_pause:
5794 ret = get_errno(pause());
5795 break;
7a3148a9 5796#endif
e5febef5 5797#ifdef TARGET_NR_utime
31e31b8a 5798 case TARGET_NR_utime:
ebc05488 5799 {
53a5960a
PB
5800 struct utimbuf tbuf, *host_tbuf;
5801 struct target_utimbuf *target_tbuf;
5802 if (arg2) {
579a97f7
FB
5803 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5804 goto efault;
cbb21eed
MB
5805 tbuf.actime = tswapal(target_tbuf->actime);
5806 tbuf.modtime = tswapal(target_tbuf->modtime);
53a5960a
PB
5807 unlock_user_struct(target_tbuf, arg2, 0);
5808 host_tbuf = &tbuf;
f72e8ff4 5809 } else {
53a5960a 5810 host_tbuf = NULL;
f72e8ff4 5811 }
579a97f7
FB
5812 if (!(p = lock_user_string(arg1)))
5813 goto efault;
53a5960a
PB
5814 ret = get_errno(utime(p, host_tbuf));
5815 unlock_user(p, arg1, 0);
ebc05488
FB
5816 }
5817 break;
e5febef5 5818#endif
978a66ff
FB
5819 case TARGET_NR_utimes:
5820 {
978a66ff 5821 struct timeval *tvp, tv[2];
53a5960a 5822 if (arg2) {
788f5ec4
TS
5823 if (copy_from_user_timeval(&tv[0], arg2)
5824 || copy_from_user_timeval(&tv[1],
5825 arg2 + sizeof(struct target_timeval)))
5826 goto efault;
978a66ff
FB
5827 tvp = tv;
5828 } else {
5829 tvp = NULL;
5830 }
579a97f7
FB
5831 if (!(p = lock_user_string(arg1)))
5832 goto efault;
53a5960a
PB
5833 ret = get_errno(utimes(p, tvp));
5834 unlock_user(p, arg1, 0);
978a66ff
FB
5835 }
5836 break;
c0d472b1 5837#if defined(TARGET_NR_futimesat)
ac8a6556
AZ
5838 case TARGET_NR_futimesat:
5839 {
5840 struct timeval *tvp, tv[2];
5841 if (arg3) {
5842 if (copy_from_user_timeval(&tv[0], arg3)
5843 || copy_from_user_timeval(&tv[1],
5844 arg3 + sizeof(struct target_timeval)))
5845 goto efault;
5846 tvp = tv;
5847 } else {
5848 tvp = NULL;
5849 }
5850 if (!(p = lock_user_string(arg2)))
5851 goto efault;
c0d472b1 5852 ret = get_errno(futimesat(arg1, path(p), tvp));
ac8a6556
AZ
5853 unlock_user(p, arg2, 0);
5854 }
5855 break;
5856#endif
ebc05488 5857#ifdef TARGET_NR_stty
31e31b8a
FB
5858 case TARGET_NR_stty:
5859 goto unimplemented;
ebc05488
FB
5860#endif
5861#ifdef TARGET_NR_gtty
31e31b8a
FB
5862 case TARGET_NR_gtty:
5863 goto unimplemented;
ebc05488 5864#endif
31e31b8a 5865 case TARGET_NR_access:
579a97f7
FB
5866 if (!(p = lock_user_string(arg1)))
5867 goto efault;
719f908e 5868 ret = get_errno(access(path(p), arg2));
53a5960a 5869 unlock_user(p, arg1, 0);
31e31b8a 5870 break;
92a34c10
TS
5871#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5872 case TARGET_NR_faccessat:
579a97f7
FB
5873 if (!(p = lock_user_string(arg2)))
5874 goto efault;
c0d472b1 5875 ret = get_errno(faccessat(arg1, p, arg3, 0));
579a97f7 5876 unlock_user(p, arg2, 0);
92a34c10
TS
5877 break;
5878#endif
7a3148a9 5879#ifdef TARGET_NR_nice /* not on alpha */
31e31b8a
FB
5880 case TARGET_NR_nice:
5881 ret = get_errno(nice(arg1));
5882 break;
7a3148a9 5883#endif
ebc05488 5884#ifdef TARGET_NR_ftime
31e31b8a
FB
5885 case TARGET_NR_ftime:
5886 goto unimplemented;
ebc05488 5887#endif
31e31b8a 5888 case TARGET_NR_sync:
04369ff2
FB
5889 sync();
5890 ret = 0;
31e31b8a
FB
5891 break;
5892 case TARGET_NR_kill:
4cb05961 5893 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
31e31b8a
FB
5894 break;
5895 case TARGET_NR_rename:
53a5960a
PB
5896 {
5897 void *p2;
5898 p = lock_user_string(arg1);
5899 p2 = lock_user_string(arg2);
579a97f7
FB
5900 if (!p || !p2)
5901 ret = -TARGET_EFAULT;
5902 else
5903 ret = get_errno(rename(p, p2));
53a5960a
PB
5904 unlock_user(p2, arg2, 0);
5905 unlock_user(p, arg1, 0);
5906 }
31e31b8a 5907 break;
c0d472b1 5908#if defined(TARGET_NR_renameat)
722183f6 5909 case TARGET_NR_renameat:
722183f6 5910 {
579a97f7 5911 void *p2;
722183f6
TS
5912 p = lock_user_string(arg2);
5913 p2 = lock_user_string(arg4);
579a97f7 5914 if (!p || !p2)
0da46a6e 5915 ret = -TARGET_EFAULT;
722183f6 5916 else
c0d472b1 5917 ret = get_errno(renameat(arg1, p, arg3, p2));
579a97f7
FB
5918 unlock_user(p2, arg4, 0);
5919 unlock_user(p, arg2, 0);
722183f6
TS
5920 }
5921 break;
5922#endif
31e31b8a 5923 case TARGET_NR_mkdir:
579a97f7
FB
5924 if (!(p = lock_user_string(arg1)))
5925 goto efault;
53a5960a
PB
5926 ret = get_errno(mkdir(p, arg2));
5927 unlock_user(p, arg1, 0);
31e31b8a 5928 break;
c0d472b1 5929#if defined(TARGET_NR_mkdirat)
4472ad0d 5930 case TARGET_NR_mkdirat:
579a97f7
FB
5931 if (!(p = lock_user_string(arg2)))
5932 goto efault;
c0d472b1 5933 ret = get_errno(mkdirat(arg1, p, arg3));
579a97f7 5934 unlock_user(p, arg2, 0);
4472ad0d
TS
5935 break;
5936#endif
31e31b8a 5937 case TARGET_NR_rmdir:
579a97f7
FB
5938 if (!(p = lock_user_string(arg1)))
5939 goto efault;
53a5960a
PB
5940 ret = get_errno(rmdir(p));
5941 unlock_user(p, arg1, 0);
31e31b8a
FB
5942 break;
5943 case TARGET_NR_dup:
5944 ret = get_errno(dup(arg1));
5945 break;
5946 case TARGET_NR_pipe:
fb41a66e 5947 ret = do_pipe(cpu_env, arg1, 0, 0);
099d6b0f
RV
5948 break;
5949#ifdef TARGET_NR_pipe2
5950 case TARGET_NR_pipe2:
e7ea6cbe
RH
5951 ret = do_pipe(cpu_env, arg1,
5952 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
31e31b8a 5953 break;
099d6b0f 5954#endif
31e31b8a 5955 case TARGET_NR_times:
32f36bce 5956 {
53a5960a 5957 struct target_tms *tmsp;
32f36bce
FB
5958 struct tms tms;
5959 ret = get_errno(times(&tms));
53a5960a 5960 if (arg1) {
579a97f7
FB
5961 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5962 if (!tmsp)
5963 goto efault;
cbb21eed
MB
5964 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5965 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5966 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5967 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
32f36bce 5968 }
c596ed17
FB
5969 if (!is_error(ret))
5970 ret = host_to_target_clock_t(ret);
32f36bce
FB
5971 }
5972 break;
ebc05488 5973#ifdef TARGET_NR_prof
31e31b8a
FB
5974 case TARGET_NR_prof:
5975 goto unimplemented;
ebc05488 5976#endif
e5febef5 5977#ifdef TARGET_NR_signal
31e31b8a
FB
5978 case TARGET_NR_signal:
5979 goto unimplemented;
e5febef5 5980#endif
31e31b8a 5981 case TARGET_NR_acct:
38d840e6
AJ
5982 if (arg1 == 0) {
5983 ret = get_errno(acct(NULL));
5984 } else {
5985 if (!(p = lock_user_string(arg1)))
5986 goto efault;
5987 ret = get_errno(acct(path(p)));
5988 unlock_user(p, arg1, 0);
5989 }
24836689 5990 break;
8070e7be 5991#ifdef TARGET_NR_umount2
31e31b8a 5992 case TARGET_NR_umount2:
579a97f7
FB
5993 if (!(p = lock_user_string(arg1)))
5994 goto efault;
53a5960a
PB
5995 ret = get_errno(umount2(p, arg2));
5996 unlock_user(p, arg1, 0);
31e31b8a 5997 break;
7a3148a9 5998#endif
ebc05488 5999#ifdef TARGET_NR_lock
31e31b8a
FB
6000 case TARGET_NR_lock:
6001 goto unimplemented;
ebc05488 6002#endif
31e31b8a
FB
6003 case TARGET_NR_ioctl:
6004 ret = do_ioctl(arg1, arg2, arg3);
6005 break;
6006 case TARGET_NR_fcntl:
9ee1fa2c 6007 ret = do_fcntl(arg1, arg2, arg3);
31e31b8a 6008 break;
ebc05488 6009#ifdef TARGET_NR_mpx
31e31b8a
FB
6010 case TARGET_NR_mpx:
6011 goto unimplemented;
ebc05488 6012#endif
31e31b8a
FB
6013 case TARGET_NR_setpgid:
6014 ret = get_errno(setpgid(arg1, arg2));
6015 break;
ebc05488 6016#ifdef TARGET_NR_ulimit
31e31b8a
FB
6017 case TARGET_NR_ulimit:
6018 goto unimplemented;
ebc05488
FB
6019#endif
6020#ifdef TARGET_NR_oldolduname
31e31b8a
FB
6021 case TARGET_NR_oldolduname:
6022 goto unimplemented;
ebc05488 6023#endif
31e31b8a
FB
6024 case TARGET_NR_umask:
6025 ret = get_errno(umask(arg1));
6026 break;
6027 case TARGET_NR_chroot:
579a97f7
FB
6028 if (!(p = lock_user_string(arg1)))
6029 goto efault;
53a5960a
PB
6030 ret = get_errno(chroot(p));
6031 unlock_user(p, arg1, 0);
31e31b8a
FB
6032 break;
6033 case TARGET_NR_ustat:
6034 goto unimplemented;
6035 case TARGET_NR_dup2:
6036 ret = get_errno(dup2(arg1, arg2));
6037 break;
d0927938
UH
6038#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
6039 case TARGET_NR_dup3:
6040 ret = get_errno(dup3(arg1, arg2, arg3));
6041 break;
6042#endif
7a3148a9 6043#ifdef TARGET_NR_getppid /* not on alpha */
31e31b8a
FB
6044 case TARGET_NR_getppid:
6045 ret = get_errno(getppid());
6046 break;
7a3148a9 6047#endif
31e31b8a
FB
6048 case TARGET_NR_getpgrp:
6049 ret = get_errno(getpgrp());
6050 break;
6051 case TARGET_NR_setsid:
6052 ret = get_errno(setsid());
6053 break;
e5febef5 6054#ifdef TARGET_NR_sigaction
31e31b8a 6055 case TARGET_NR_sigaction:
31e31b8a 6056 {
6049f4f8
RH
6057#if defined(TARGET_ALPHA)
6058 struct target_sigaction act, oact, *pact = 0;
53a5960a 6059 struct target_old_sigaction *old_act;
53a5960a 6060 if (arg2) {
579a97f7
FB
6061 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6062 goto efault;
66fb9763
FB
6063 act._sa_handler = old_act->_sa_handler;
6064 target_siginitset(&act.sa_mask, old_act->sa_mask);
6065 act.sa_flags = old_act->sa_flags;
6049f4f8 6066 act.sa_restorer = 0;
53a5960a 6067 unlock_user_struct(old_act, arg2, 0);
66fb9763 6068 pact = &act;
66fb9763
FB
6069 }
6070 ret = get_errno(do_sigaction(arg1, pact, &oact));
53a5960a 6071 if (!is_error(ret) && arg3) {
579a97f7
FB
6072 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6073 goto efault;
53a5960a
PB
6074 old_act->_sa_handler = oact._sa_handler;
6075 old_act->sa_mask = oact.sa_mask.sig[0];
6076 old_act->sa_flags = oact.sa_flags;
53a5960a 6077 unlock_user_struct(old_act, arg3, 1);
66fb9763 6078 }
6049f4f8 6079#elif defined(TARGET_MIPS)
106ec879
FB
6080 struct target_sigaction act, oact, *pact, *old_act;
6081
6082 if (arg2) {
579a97f7
FB
6083 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6084 goto efault;
106ec879
FB
6085 act._sa_handler = old_act->_sa_handler;
6086 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
6087 act.sa_flags = old_act->sa_flags;
6088 unlock_user_struct(old_act, arg2, 0);
6089 pact = &act;
6090 } else {
6091 pact = NULL;
6092 }
6093
6094 ret = get_errno(do_sigaction(arg1, pact, &oact));
6095
6096 if (!is_error(ret) && arg3) {
579a97f7
FB
6097 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6098 goto efault;
106ec879
FB
6099 old_act->_sa_handler = oact._sa_handler;
6100 old_act->sa_flags = oact.sa_flags;
6101 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
6102 old_act->sa_mask.sig[1] = 0;
6103 old_act->sa_mask.sig[2] = 0;
6104 old_act->sa_mask.sig[3] = 0;
6105 unlock_user_struct(old_act, arg3, 1);
6106 }
6049f4f8
RH
6107#else
6108 struct target_old_sigaction *old_act;
6109 struct target_sigaction act, oact, *pact;
6110 if (arg2) {
6111 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
6112 goto efault;
6113 act._sa_handler = old_act->_sa_handler;
6114 target_siginitset(&act.sa_mask, old_act->sa_mask);
6115 act.sa_flags = old_act->sa_flags;
6116 act.sa_restorer = old_act->sa_restorer;
6117 unlock_user_struct(old_act, arg2, 0);
6118 pact = &act;
6119 } else {
6120 pact = NULL;
6121 }
6122 ret = get_errno(do_sigaction(arg1, pact, &oact));
6123 if (!is_error(ret) && arg3) {
6124 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
6125 goto efault;
6126 old_act->_sa_handler = oact._sa_handler;
6127 old_act->sa_mask = oact.sa_mask.sig[0];
6128 old_act->sa_flags = oact.sa_flags;
6129 old_act->sa_restorer = oact.sa_restorer;
6130 unlock_user_struct(old_act, arg3, 1);
6131 }
388bb21a 6132#endif
31e31b8a
FB
6133 }
6134 break;
e5febef5 6135#endif
66fb9763 6136 case TARGET_NR_rt_sigaction:
53a5960a 6137 {
6049f4f8
RH
6138#if defined(TARGET_ALPHA)
6139 struct target_sigaction act, oact, *pact = 0;
6140 struct target_rt_sigaction *rt_act;
6141 /* ??? arg4 == sizeof(sigset_t). */
6142 if (arg2) {
6143 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
6144 goto efault;
6145 act._sa_handler = rt_act->_sa_handler;
6146 act.sa_mask = rt_act->sa_mask;
6147 act.sa_flags = rt_act->sa_flags;
6148 act.sa_restorer = arg5;
6149 unlock_user_struct(rt_act, arg2, 0);
6150 pact = &act;
6151 }
6152 ret = get_errno(do_sigaction(arg1, pact, &oact));
6153 if (!is_error(ret) && arg3) {
6154 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
6155 goto efault;
6156 rt_act->_sa_handler = oact._sa_handler;
6157 rt_act->sa_mask = oact.sa_mask;
6158 rt_act->sa_flags = oact.sa_flags;
6159 unlock_user_struct(rt_act, arg3, 1);
6160 }
6161#else
53a5960a
PB
6162 struct target_sigaction *act;
6163 struct target_sigaction *oact;
6164
579a97f7
FB
6165 if (arg2) {
6166 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
6167 goto efault;
6168 } else
53a5960a 6169 act = NULL;
579a97f7
FB
6170 if (arg3) {
6171 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
6172 ret = -TARGET_EFAULT;
6173 goto rt_sigaction_fail;
6174 }
6175 } else
53a5960a
PB
6176 oact = NULL;
6177 ret = get_errno(do_sigaction(arg1, act, oact));
579a97f7
FB
6178 rt_sigaction_fail:
6179 if (act)
53a5960a 6180 unlock_user_struct(act, arg2, 0);
579a97f7 6181 if (oact)
53a5960a 6182 unlock_user_struct(oact, arg3, 1);
6049f4f8 6183#endif
53a5960a 6184 }
66fb9763 6185 break;
7a3148a9 6186#ifdef TARGET_NR_sgetmask /* not on alpha */
31e31b8a 6187 case TARGET_NR_sgetmask:
66fb9763
FB
6188 {
6189 sigset_t cur_set;
992f48a0 6190 abi_ulong target_set;
1c275925 6191 do_sigprocmask(0, NULL, &cur_set);
66fb9763
FB
6192 host_to_target_old_sigset(&target_set, &cur_set);
6193 ret = target_set;
6194 }
6195 break;
7a3148a9
JM
6196#endif
6197#ifdef TARGET_NR_ssetmask /* not on alpha */
31e31b8a 6198 case TARGET_NR_ssetmask:
66fb9763
FB
6199 {
6200 sigset_t set, oset, cur_set;
992f48a0 6201 abi_ulong target_set = arg1;
1c275925 6202 do_sigprocmask(0, NULL, &cur_set);
66fb9763
FB
6203 target_to_host_old_sigset(&set, &target_set);
6204 sigorset(&set, &set, &cur_set);
1c275925 6205 do_sigprocmask(SIG_SETMASK, &set, &oset);
66fb9763
FB
6206 host_to_target_old_sigset(&target_set, &oset);
6207 ret = target_set;
6208 }
6209 break;
7a3148a9 6210#endif
e5febef5 6211#ifdef TARGET_NR_sigprocmask
66fb9763
FB
6212 case TARGET_NR_sigprocmask:
6213 {
a5b3b13b
RH
6214#if defined(TARGET_ALPHA)
6215 sigset_t set, oldset;
6216 abi_ulong mask;
6217 int how;
6218
6219 switch (arg1) {
6220 case TARGET_SIG_BLOCK:
6221 how = SIG_BLOCK;
6222 break;
6223 case TARGET_SIG_UNBLOCK:
6224 how = SIG_UNBLOCK;
6225 break;
6226 case TARGET_SIG_SETMASK:
6227 how = SIG_SETMASK;
6228 break;
6229 default:
6230 ret = -TARGET_EINVAL;
6231 goto fail;
6232 }
6233 mask = arg2;
6234 target_to_host_old_sigset(&set, &mask);
6235
1c275925 6236 ret = get_errno(do_sigprocmask(how, &set, &oldset));
a5b3b13b
RH
6237 if (!is_error(ret)) {
6238 host_to_target_old_sigset(&mask, &oldset);
6239 ret = mask;
0229f5a3 6240 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
a5b3b13b
RH
6241 }
6242#else
66fb9763 6243 sigset_t set, oldset, *set_ptr;
a5b3b13b 6244 int how;
3b46e624 6245
53a5960a 6246 if (arg2) {
a5b3b13b 6247 switch (arg1) {
66fb9763
FB
6248 case TARGET_SIG_BLOCK:
6249 how = SIG_BLOCK;
6250 break;
6251 case TARGET_SIG_UNBLOCK:
6252 how = SIG_UNBLOCK;
6253 break;
6254 case TARGET_SIG_SETMASK:
6255 how = SIG_SETMASK;
6256 break;
6257 default:
0da46a6e 6258 ret = -TARGET_EINVAL;
66fb9763
FB
6259 goto fail;
6260 }
c227f099 6261 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
579a97f7 6262 goto efault;
53a5960a
PB
6263 target_to_host_old_sigset(&set, p);
6264 unlock_user(p, arg2, 0);
66fb9763
FB
6265 set_ptr = &set;
6266 } else {
6267 how = 0;
6268 set_ptr = NULL;
6269 }
1c275925 6270 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
53a5960a 6271 if (!is_error(ret) && arg3) {
c227f099 6272 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
579a97f7 6273 goto efault;
53a5960a 6274 host_to_target_old_sigset(p, &oldset);
c227f099 6275 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763 6276 }
a5b3b13b 6277#endif
66fb9763
FB
6278 }
6279 break;
e5febef5 6280#endif
66fb9763
FB
6281 case TARGET_NR_rt_sigprocmask:
6282 {
6283 int how = arg1;
6284 sigset_t set, oldset, *set_ptr;
3b46e624 6285
53a5960a 6286 if (arg2) {
66fb9763
FB
6287 switch(how) {
6288 case TARGET_SIG_BLOCK:
6289 how = SIG_BLOCK;
6290 break;
6291 case TARGET_SIG_UNBLOCK:
6292 how = SIG_UNBLOCK;
6293 break;
6294 case TARGET_SIG_SETMASK:
6295 how = SIG_SETMASK;
6296 break;
6297 default:
0da46a6e 6298 ret = -TARGET_EINVAL;
66fb9763
FB
6299 goto fail;
6300 }
c227f099 6301 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
579a97f7 6302 goto efault;
53a5960a
PB
6303 target_to_host_sigset(&set, p);
6304 unlock_user(p, arg2, 0);
66fb9763
FB
6305 set_ptr = &set;
6306 } else {
6307 how = 0;
6308 set_ptr = NULL;
6309 }
1c275925 6310 ret = get_errno(do_sigprocmask(how, set_ptr, &oldset));
53a5960a 6311 if (!is_error(ret) && arg3) {
c227f099 6312 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
579a97f7 6313 goto efault;
53a5960a 6314 host_to_target_sigset(p, &oldset);
c227f099 6315 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763
FB
6316 }
6317 }
6318 break;
e5febef5 6319#ifdef TARGET_NR_sigpending
66fb9763
FB
6320 case TARGET_NR_sigpending:
6321 {
6322 sigset_t set;
6323 ret = get_errno(sigpending(&set));
6324 if (!is_error(ret)) {
c227f099 6325 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
579a97f7 6326 goto efault;
53a5960a 6327 host_to_target_old_sigset(p, &set);
c227f099 6328 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
6329 }
6330 }
6331 break;
e5febef5 6332#endif
66fb9763
FB
6333 case TARGET_NR_rt_sigpending:
6334 {
6335 sigset_t set;
6336 ret = get_errno(sigpending(&set));
6337 if (!is_error(ret)) {
c227f099 6338 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
579a97f7 6339 goto efault;
53a5960a 6340 host_to_target_sigset(p, &set);
c227f099 6341 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
6342 }
6343 }
6344 break;
e5febef5 6345#ifdef TARGET_NR_sigsuspend
66fb9763
FB
6346 case TARGET_NR_sigsuspend:
6347 {
6348 sigset_t set;
f43ce12b
RH
6349#if defined(TARGET_ALPHA)
6350 abi_ulong mask = arg1;
6351 target_to_host_old_sigset(&set, &mask);
6352#else
c227f099 6353 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 6354 goto efault;
53a5960a
PB
6355 target_to_host_old_sigset(&set, p);
6356 unlock_user(p, arg1, 0);
f43ce12b 6357#endif
66fb9763
FB
6358 ret = get_errno(sigsuspend(&set));
6359 }
6360 break;
e5febef5 6361#endif
66fb9763
FB
6362 case TARGET_NR_rt_sigsuspend:
6363 {
6364 sigset_t set;
c227f099 6365 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 6366 goto efault;
53a5960a
PB
6367 target_to_host_sigset(&set, p);
6368 unlock_user(p, arg1, 0);
66fb9763
FB
6369 ret = get_errno(sigsuspend(&set));
6370 }
6371 break;
6372 case TARGET_NR_rt_sigtimedwait:
6373 {
66fb9763
FB
6374 sigset_t set;
6375 struct timespec uts, *puts;
6376 siginfo_t uinfo;
3b46e624 6377
c227f099 6378 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 6379 goto efault;
53a5960a
PB
6380 target_to_host_sigset(&set, p);
6381 unlock_user(p, arg1, 0);
6382 if (arg3) {
66fb9763 6383 puts = &uts;
53a5960a 6384 target_to_host_timespec(puts, arg3);
66fb9763
FB
6385 } else {
6386 puts = NULL;
6387 }
6388 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
974a196d
PJ
6389 if (!is_error(ret)) {
6390 if (arg2) {
6391 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
6392 0);
6393 if (!p) {
6394 goto efault;
6395 }
6396 host_to_target_siginfo(p, &uinfo);
6397 unlock_user(p, arg2, sizeof(target_siginfo_t));
6398 }
6399 ret = host_to_target_signal(ret);
66fb9763
FB
6400 }
6401 }
6402 break;
6403 case TARGET_NR_rt_sigqueueinfo:
6404 {
6405 siginfo_t uinfo;
c227f099 6406 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
579a97f7 6407 goto efault;
53a5960a
PB
6408 target_to_host_siginfo(&uinfo, p);
6409 unlock_user(p, arg1, 0);
66fb9763
FB
6410 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6411 }
6412 break;
e5febef5 6413#ifdef TARGET_NR_sigreturn
66fb9763
FB
6414 case TARGET_NR_sigreturn:
6415 /* NOTE: ret is eax, so not transcoding must be done */
6416 ret = do_sigreturn(cpu_env);
6417 break;
e5febef5 6418#endif
66fb9763
FB
6419 case TARGET_NR_rt_sigreturn:
6420 /* NOTE: ret is eax, so not transcoding must be done */
6421 ret = do_rt_sigreturn(cpu_env);
6422 break;
31e31b8a 6423 case TARGET_NR_sethostname:
579a97f7
FB
6424 if (!(p = lock_user_string(arg1)))
6425 goto efault;
53a5960a
PB
6426 ret = get_errno(sethostname(p, arg2));
6427 unlock_user(p, arg1, 0);
31e31b8a
FB
6428 break;
6429 case TARGET_NR_setrlimit:
9de5e440 6430 {
e22b7015 6431 int resource = target_to_host_resource(arg1);
53a5960a 6432 struct target_rlimit *target_rlim;
9de5e440 6433 struct rlimit rlim;
579a97f7
FB
6434 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6435 goto efault;
81bbe906
TY
6436 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6437 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
53a5960a 6438 unlock_user_struct(target_rlim, arg2, 0);
9de5e440
FB
6439 ret = get_errno(setrlimit(resource, &rlim));
6440 }
6441 break;
31e31b8a 6442 case TARGET_NR_getrlimit:
9de5e440 6443 {
e22b7015 6444 int resource = target_to_host_resource(arg1);
53a5960a 6445 struct target_rlimit *target_rlim;
9de5e440 6446 struct rlimit rlim;
3b46e624 6447
9de5e440
FB
6448 ret = get_errno(getrlimit(resource, &rlim));
6449 if (!is_error(ret)) {
579a97f7
FB
6450 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6451 goto efault;
81bbe906
TY
6452 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6453 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 6454 unlock_user_struct(target_rlim, arg2, 1);
9de5e440
FB
6455 }
6456 }
6457 break;
31e31b8a 6458 case TARGET_NR_getrusage:
b409186b
FB
6459 {
6460 struct rusage rusage;
b409186b
FB
6461 ret = get_errno(getrusage(arg1, &rusage));
6462 if (!is_error(ret)) {
a39fb273 6463 ret = host_to_target_rusage(arg2, &rusage);
b409186b
FB
6464 }
6465 }
6466 break;
31e31b8a
FB
6467 case TARGET_NR_gettimeofday:
6468 {
31e31b8a
FB
6469 struct timeval tv;
6470 ret = get_errno(gettimeofday(&tv, NULL));
6471 if (!is_error(ret)) {
788f5ec4
TS
6472 if (copy_to_user_timeval(arg1, &tv))
6473 goto efault;
31e31b8a
FB
6474 }
6475 }
6476 break;
6477 case TARGET_NR_settimeofday:
6478 {
b67d8031 6479 struct timeval tv, *ptv = NULL;
ef4467e9
PB
6480 struct timezone tz, *ptz = NULL;
6481
b67d8031
PB
6482 if (arg1) {
6483 if (copy_from_user_timeval(&tv, arg1)) {
6484 goto efault;
6485 }
6486 ptv = &tv;
6487 }
ef4467e9
PB
6488
6489 if (arg2) {
6490 if (copy_from_user_timezone(&tz, arg2)) {
6491 goto efault;
6492 }
6493 ptz = &tz;
6494 }
6495
b67d8031 6496 ret = get_errno(settimeofday(ptv, ptz));
31e31b8a
FB
6497 }
6498 break;
9468a5d4 6499#if defined(TARGET_NR_select)
31e31b8a 6500 case TARGET_NR_select:
9468a5d4
LV
6501#if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6502 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6503#else
f2674e31 6504 {
53a5960a 6505 struct target_sel_arg_struct *sel;
992f48a0 6506 abi_ulong inp, outp, exp, tvp;
53a5960a
PB
6507 long nsel;
6508
579a97f7
FB
6509 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6510 goto efault;
cbb21eed
MB
6511 nsel = tswapal(sel->n);
6512 inp = tswapal(sel->inp);
6513 outp = tswapal(sel->outp);
6514 exp = tswapal(sel->exp);
6515 tvp = tswapal(sel->tvp);
53a5960a
PB
6516 unlock_user_struct(sel, arg1, 0);
6517 ret = do_select(nsel, inp, outp, exp, tvp);
f2674e31 6518 }
9468a5d4 6519#endif
f2674e31 6520 break;
9e42382f
RV
6521#endif
6522#ifdef TARGET_NR_pselect6
6523 case TARGET_NR_pselect6:
055e0906
MF
6524 {
6525 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6526 fd_set rfds, wfds, efds;
6527 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6528 struct timespec ts, *ts_ptr;
6529
6530 /*
6531 * The 6th arg is actually two args smashed together,
6532 * so we cannot use the C library.
6533 */
6534 sigset_t set;
6535 struct {
6536 sigset_t *set;
6537 size_t size;
6538 } sig, *sig_ptr;
6539
6540 abi_ulong arg_sigset, arg_sigsize, *arg7;
6541 target_sigset_t *target_sigset;
6542
6543 n = arg1;
6544 rfd_addr = arg2;
6545 wfd_addr = arg3;
6546 efd_addr = arg4;
6547 ts_addr = arg5;
6548
6549 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6550 if (ret) {
6551 goto fail;
6552 }
6553 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6554 if (ret) {
6555 goto fail;
6556 }
6557 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6558 if (ret) {
6559 goto fail;
6560 }
6561
6562 /*
6563 * This takes a timespec, and not a timeval, so we cannot
6564 * use the do_select() helper ...
6565 */
6566 if (ts_addr) {
6567 if (target_to_host_timespec(&ts, ts_addr)) {
6568 goto efault;
6569 }
6570 ts_ptr = &ts;
6571 } else {
6572 ts_ptr = NULL;
6573 }
6574
6575 /* Extract the two packed args for the sigset */
6576 if (arg6) {
6577 sig_ptr = &sig;
6578 sig.size = _NSIG / 8;
6579
6580 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6581 if (!arg7) {
6582 goto efault;
6583 }
cbb21eed
MB
6584 arg_sigset = tswapal(arg7[0]);
6585 arg_sigsize = tswapal(arg7[1]);
055e0906
MF
6586 unlock_user(arg7, arg6, 0);
6587
6588 if (arg_sigset) {
6589 sig.set = &set;
8f04eeb3
PM
6590 if (arg_sigsize != sizeof(*target_sigset)) {
6591 /* Like the kernel, we enforce correct size sigsets */
6592 ret = -TARGET_EINVAL;
6593 goto fail;
6594 }
055e0906
MF
6595 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6596 sizeof(*target_sigset), 1);
6597 if (!target_sigset) {
6598 goto efault;
6599 }
6600 target_to_host_sigset(&set, target_sigset);
6601 unlock_user(target_sigset, arg_sigset, 0);
6602 } else {
6603 sig.set = NULL;
6604 }
6605 } else {
6606 sig_ptr = NULL;
6607 }
6608
6609 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6610 ts_ptr, sig_ptr));
6611
6612 if (!is_error(ret)) {
6613 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6614 goto efault;
6615 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6616 goto efault;
6617 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6618 goto efault;
6619
6620 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6621 goto efault;
6622 }
6623 }
6624 break;
048f6b4d 6625#endif
31e31b8a 6626 case TARGET_NR_symlink:
53a5960a
PB
6627 {
6628 void *p2;
6629 p = lock_user_string(arg1);
6630 p2 = lock_user_string(arg2);
579a97f7
FB
6631 if (!p || !p2)
6632 ret = -TARGET_EFAULT;
6633 else
6634 ret = get_errno(symlink(p, p2));
53a5960a
PB
6635 unlock_user(p2, arg2, 0);
6636 unlock_user(p, arg1, 0);
6637 }
31e31b8a 6638 break;
c0d472b1 6639#if defined(TARGET_NR_symlinkat)
f0b6243d 6640 case TARGET_NR_symlinkat:
f0b6243d 6641 {
579a97f7 6642 void *p2;
f0b6243d
TS
6643 p = lock_user_string(arg1);
6644 p2 = lock_user_string(arg3);
579a97f7 6645 if (!p || !p2)
0da46a6e 6646 ret = -TARGET_EFAULT;
f0b6243d 6647 else
c0d472b1 6648 ret = get_errno(symlinkat(p, arg2, p2));
579a97f7
FB
6649 unlock_user(p2, arg3, 0);
6650 unlock_user(p, arg1, 0);
f0b6243d
TS
6651 }
6652 break;
6653#endif
ebc05488 6654#ifdef TARGET_NR_oldlstat
31e31b8a
FB
6655 case TARGET_NR_oldlstat:
6656 goto unimplemented;
ebc05488 6657#endif
31e31b8a 6658 case TARGET_NR_readlink:
53a5960a 6659 {
463d8e73 6660 void *p2;
53a5960a 6661 p = lock_user_string(arg1);
579a97f7 6662 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
463d8e73 6663 if (!p || !p2) {
579a97f7 6664 ret = -TARGET_EFAULT;
f17f4989
MF
6665 } else if (!arg3) {
6666 /* Short circuit this for the magic exe check. */
6667 ret = -TARGET_EINVAL;
463d8e73
AS
6668 } else if (is_proc_myself((const char *)p, "exe")) {
6669 char real[PATH_MAX], *temp;
6670 temp = realpath(exec_path, real);
f17f4989
MF
6671 /* Return value is # of bytes that we wrote to the buffer. */
6672 if (temp == NULL) {
6673 ret = get_errno(-1);
6674 } else {
6675 /* Don't worry about sign mismatch as earlier mapping
6676 * logic would have thrown a bad address error. */
6677 ret = MIN(strlen(real), arg3);
6678 /* We cannot NUL terminate the string. */
6679 memcpy(p2, real, ret);
6680 }
463d8e73
AS
6681 } else {
6682 ret = get_errno(readlink(path(p), p2, arg3));
d088d664 6683 }
53a5960a
PB
6684 unlock_user(p2, arg2, ret);
6685 unlock_user(p, arg1, 0);
6686 }
31e31b8a 6687 break;
c0d472b1 6688#if defined(TARGET_NR_readlinkat)
5e0ccb18 6689 case TARGET_NR_readlinkat:
5e0ccb18 6690 {
579a97f7 6691 void *p2;
5e0ccb18 6692 p = lock_user_string(arg2);
579a97f7 6693 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
463d8e73
AS
6694 if (!p || !p2) {
6695 ret = -TARGET_EFAULT;
6696 } else if (is_proc_myself((const char *)p, "exe")) {
6697 char real[PATH_MAX], *temp;
6698 temp = realpath(exec_path, real);
6699 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
6700 snprintf((char *)p2, arg4, "%s", real);
6701 } else {
c0d472b1 6702 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
463d8e73 6703 }
579a97f7
FB
6704 unlock_user(p2, arg3, ret);
6705 unlock_user(p, arg2, 0);
5e0ccb18
TS
6706 }
6707 break;
6708#endif
e5febef5 6709#ifdef TARGET_NR_uselib
31e31b8a
FB
6710 case TARGET_NR_uselib:
6711 goto unimplemented;
e5febef5
TS
6712#endif
6713#ifdef TARGET_NR_swapon
31e31b8a 6714 case TARGET_NR_swapon:
579a97f7
FB
6715 if (!(p = lock_user_string(arg1)))
6716 goto efault;
53a5960a
PB
6717 ret = get_errno(swapon(p, arg2));
6718 unlock_user(p, arg1, 0);
31e31b8a 6719 break;
e5febef5 6720#endif
31e31b8a 6721 case TARGET_NR_reboot:
c07ecc68
LV
6722 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
6723 /* arg4 must be ignored in all other cases */
6724 p = lock_user_string(arg4);
6725 if (!p) {
6726 goto efault;
6727 }
6728 ret = get_errno(reboot(arg1, arg2, arg3, p));
6729 unlock_user(p, arg4, 0);
6730 } else {
6731 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
6732 }
0f6b4d21 6733 break;
e5febef5 6734#ifdef TARGET_NR_readdir
31e31b8a
FB
6735 case TARGET_NR_readdir:
6736 goto unimplemented;
e5febef5
TS
6737#endif
6738#ifdef TARGET_NR_mmap
31e31b8a 6739 case TARGET_NR_mmap:
09701199
AG
6740#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6741 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
a4c075f1
UH
6742 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6743 || defined(TARGET_S390X)
31e31b8a 6744 {
992f48a0
BS
6745 abi_ulong *v;
6746 abi_ulong v1, v2, v3, v4, v5, v6;
579a97f7
FB
6747 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6748 goto efault;
cbb21eed
MB
6749 v1 = tswapal(v[0]);
6750 v2 = tswapal(v[1]);
6751 v3 = tswapal(v[2]);
6752 v4 = tswapal(v[3]);
6753 v5 = tswapal(v[4]);
6754 v6 = tswapal(v[5]);
53a5960a 6755 unlock_user(v, arg1, 0);
5fafdf24 6756 ret = get_errno(target_mmap(v1, v2, v3,
5286db75
FB
6757 target_to_host_bitmask(v4, mmap_flags_tbl),
6758 v5, v6));
31e31b8a 6759 }
31e31b8a 6760#else
5fafdf24
TS
6761 ret = get_errno(target_mmap(arg1, arg2, arg3,
6762 target_to_host_bitmask(arg4, mmap_flags_tbl),
6fb883e8
FB
6763 arg5,
6764 arg6));
31e31b8a 6765#endif
6fb883e8 6766 break;
e5febef5 6767#endif
a315a145 6768#ifdef TARGET_NR_mmap2
6fb883e8 6769 case TARGET_NR_mmap2:
bb7ec043 6770#ifndef MMAP_SHIFT
c573ff67 6771#define MMAP_SHIFT 12
c573ff67 6772#endif
5fafdf24
TS
6773 ret = get_errno(target_mmap(arg1, arg2, arg3,
6774 target_to_host_bitmask(arg4, mmap_flags_tbl),
5286db75 6775 arg5,
c573ff67 6776 arg6 << MMAP_SHIFT));
31e31b8a 6777 break;
a315a145 6778#endif
31e31b8a 6779 case TARGET_NR_munmap:
54936004 6780 ret = get_errno(target_munmap(arg1, arg2));
31e31b8a 6781 break;
9de5e440 6782 case TARGET_NR_mprotect:
97374d38 6783 {
0429a971 6784 TaskState *ts = cpu->opaque;
97374d38
PB
6785 /* Special hack to detect libc making the stack executable. */
6786 if ((arg3 & PROT_GROWSDOWN)
6787 && arg1 >= ts->info->stack_limit
6788 && arg1 <= ts->info->start_stack) {
6789 arg3 &= ~PROT_GROWSDOWN;
6790 arg2 = arg2 + arg1 - ts->info->stack_limit;
6791 arg1 = ts->info->stack_limit;
6792 }
6793 }
54936004 6794 ret = get_errno(target_mprotect(arg1, arg2, arg3));
9de5e440 6795 break;
e5febef5 6796#ifdef TARGET_NR_mremap
9de5e440 6797 case TARGET_NR_mremap:
54936004 6798 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
9de5e440 6799 break;
e5febef5 6800#endif
53a5960a 6801 /* ??? msync/mlock/munlock are broken for softmmu. */
e5febef5 6802#ifdef TARGET_NR_msync
9de5e440 6803 case TARGET_NR_msync:
53a5960a 6804 ret = get_errno(msync(g2h(arg1), arg2, arg3));
9de5e440 6805 break;
e5febef5
TS
6806#endif
6807#ifdef TARGET_NR_mlock
9de5e440 6808 case TARGET_NR_mlock:
53a5960a 6809 ret = get_errno(mlock(g2h(arg1), arg2));
9de5e440 6810 break;
e5febef5
TS
6811#endif
6812#ifdef TARGET_NR_munlock
9de5e440 6813 case TARGET_NR_munlock:
53a5960a 6814 ret = get_errno(munlock(g2h(arg1), arg2));
9de5e440 6815 break;
e5febef5
TS
6816#endif
6817#ifdef TARGET_NR_mlockall
9de5e440
FB
6818 case TARGET_NR_mlockall:
6819 ret = get_errno(mlockall(arg1));
6820 break;
e5febef5
TS
6821#endif
6822#ifdef TARGET_NR_munlockall
9de5e440
FB
6823 case TARGET_NR_munlockall:
6824 ret = get_errno(munlockall());
6825 break;
e5febef5 6826#endif
31e31b8a 6827 case TARGET_NR_truncate:
579a97f7
FB
6828 if (!(p = lock_user_string(arg1)))
6829 goto efault;
53a5960a
PB
6830 ret = get_errno(truncate(p, arg2));
6831 unlock_user(p, arg1, 0);
31e31b8a
FB
6832 break;
6833 case TARGET_NR_ftruncate:
6834 ret = get_errno(ftruncate(arg1, arg2));
6835 break;
6836 case TARGET_NR_fchmod:
6837 ret = get_errno(fchmod(arg1, arg2));
6838 break;
c0d472b1 6839#if defined(TARGET_NR_fchmodat)
814d7977 6840 case TARGET_NR_fchmodat:
579a97f7
FB
6841 if (!(p = lock_user_string(arg2)))
6842 goto efault;
c0d472b1 6843 ret = get_errno(fchmodat(arg1, p, arg3, 0));
579a97f7 6844 unlock_user(p, arg2, 0);
814d7977
TS
6845 break;
6846#endif
31e31b8a 6847 case TARGET_NR_getpriority:
95c09828
RH
6848 /* Note that negative values are valid for getpriority, so we must
6849 differentiate based on errno settings. */
6850 errno = 0;
6851 ret = getpriority(arg1, arg2);
6852 if (ret == -1 && errno != 0) {
6853 ret = -host_to_target_errno(errno);
6854 break;
6855 }
6856#ifdef TARGET_ALPHA
6857 /* Return value is the unbiased priority. Signal no error. */
6858 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
6859#else
6860 /* Return value is a biased priority to avoid negative numbers. */
6861 ret = 20 - ret;
6862#endif
31e31b8a
FB
6863 break;
6864 case TARGET_NR_setpriority:
6865 ret = get_errno(setpriority(arg1, arg2, arg3));
6866 break;
ebc05488 6867#ifdef TARGET_NR_profil
31e31b8a
FB
6868 case TARGET_NR_profil:
6869 goto unimplemented;
ebc05488 6870#endif
31e31b8a 6871 case TARGET_NR_statfs:
579a97f7
FB
6872 if (!(p = lock_user_string(arg1)))
6873 goto efault;
53a5960a
PB
6874 ret = get_errno(statfs(path(p), &stfs));
6875 unlock_user(p, arg1, 0);
31e31b8a
FB
6876 convert_statfs:
6877 if (!is_error(ret)) {
53a5960a 6878 struct target_statfs *target_stfs;
3b46e624 6879
579a97f7
FB
6880 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6881 goto efault;
6882 __put_user(stfs.f_type, &target_stfs->f_type);
6883 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6884 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6885 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6886 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6887 __put_user(stfs.f_files, &target_stfs->f_files);
6888 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6889 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6890 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6891 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376
AG
6892 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6893 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
53a5960a 6894 unlock_user_struct(target_stfs, arg2, 1);
31e31b8a
FB
6895 }
6896 break;
6897 case TARGET_NR_fstatfs:
56c8f68f 6898 ret = get_errno(fstatfs(arg1, &stfs));
31e31b8a 6899 goto convert_statfs;
56c8f68f
FB
6900#ifdef TARGET_NR_statfs64
6901 case TARGET_NR_statfs64:
579a97f7
FB
6902 if (!(p = lock_user_string(arg1)))
6903 goto efault;
53a5960a
PB
6904 ret = get_errno(statfs(path(p), &stfs));
6905 unlock_user(p, arg1, 0);
56c8f68f
FB
6906 convert_statfs64:
6907 if (!is_error(ret)) {
53a5960a 6908 struct target_statfs64 *target_stfs;
3b46e624 6909
579a97f7
FB
6910 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6911 goto efault;
6912 __put_user(stfs.f_type, &target_stfs->f_type);
6913 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6914 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6915 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6916 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6917 __put_user(stfs.f_files, &target_stfs->f_files);
6918 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6919 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6920 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6921 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376
AG
6922 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6923 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
579a97f7 6924 unlock_user_struct(target_stfs, arg3, 1);
56c8f68f
FB
6925 }
6926 break;
6927 case TARGET_NR_fstatfs64:
6928 ret = get_errno(fstatfs(arg1, &stfs));
6929 goto convert_statfs64;
6930#endif
ebc05488 6931#ifdef TARGET_NR_ioperm
31e31b8a
FB
6932 case TARGET_NR_ioperm:
6933 goto unimplemented;
ebc05488 6934#endif
e5febef5 6935#ifdef TARGET_NR_socketcall
31e31b8a 6936 case TARGET_NR_socketcall:
53a5960a 6937 ret = do_socketcall(arg1, arg2);
31e31b8a 6938 break;
e5febef5 6939#endif
3532fa74
FB
6940#ifdef TARGET_NR_accept
6941 case TARGET_NR_accept:
a94b4987
PM
6942 ret = do_accept4(arg1, arg2, arg3, 0);
6943 break;
6944#endif
6945#ifdef TARGET_NR_accept4
6946 case TARGET_NR_accept4:
6947#ifdef CONFIG_ACCEPT4
6948 ret = do_accept4(arg1, arg2, arg3, arg4);
6949#else
6950 goto unimplemented;
6951#endif
3532fa74
FB
6952 break;
6953#endif
6954#ifdef TARGET_NR_bind
6955 case TARGET_NR_bind:
6956 ret = do_bind(arg1, arg2, arg3);
6957 break;
6958#endif
6959#ifdef TARGET_NR_connect
6960 case TARGET_NR_connect:
6961 ret = do_connect(arg1, arg2, arg3);
6962 break;
6963#endif
6964#ifdef TARGET_NR_getpeername
6965 case TARGET_NR_getpeername:
1be9e1dc 6966 ret = do_getpeername(arg1, arg2, arg3);
3532fa74
FB
6967 break;
6968#endif
6969#ifdef TARGET_NR_getsockname
6970 case TARGET_NR_getsockname:
1be9e1dc 6971 ret = do_getsockname(arg1, arg2, arg3);
3532fa74
FB
6972 break;
6973#endif
6974#ifdef TARGET_NR_getsockopt
6975 case TARGET_NR_getsockopt:
6976 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6977 break;
6978#endif
6979#ifdef TARGET_NR_listen
6980 case TARGET_NR_listen:
1be9e1dc 6981 ret = get_errno(listen(arg1, arg2));
3532fa74
FB
6982 break;
6983#endif
6984#ifdef TARGET_NR_recv
6985 case TARGET_NR_recv:
214201bd 6986 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
6987 break;
6988#endif
6989#ifdef TARGET_NR_recvfrom
6990 case TARGET_NR_recvfrom:
214201bd 6991 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
6992 break;
6993#endif
6994#ifdef TARGET_NR_recvmsg
6995 case TARGET_NR_recvmsg:
6996 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6997 break;
6998#endif
6999#ifdef TARGET_NR_send
7000 case TARGET_NR_send:
1be9e1dc 7001 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
7002 break;
7003#endif
7004#ifdef TARGET_NR_sendmsg
7005 case TARGET_NR_sendmsg:
7006 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
7007 break;
7008#endif
f19e00d7
AG
7009#ifdef TARGET_NR_sendmmsg
7010 case TARGET_NR_sendmmsg:
7011 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
7012 break;
7013 case TARGET_NR_recvmmsg:
7014 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
7015 break;
7016#endif
3532fa74
FB
7017#ifdef TARGET_NR_sendto
7018 case TARGET_NR_sendto:
1be9e1dc 7019 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
7020 break;
7021#endif
7022#ifdef TARGET_NR_shutdown
7023 case TARGET_NR_shutdown:
1be9e1dc 7024 ret = get_errno(shutdown(arg1, arg2));
3532fa74
FB
7025 break;
7026#endif
7027#ifdef TARGET_NR_socket
7028 case TARGET_NR_socket:
7029 ret = do_socket(arg1, arg2, arg3);
7030 break;
7031#endif
7032#ifdef TARGET_NR_socketpair
7033 case TARGET_NR_socketpair:
1be9e1dc 7034 ret = do_socketpair(arg1, arg2, arg3, arg4);
3532fa74
FB
7035 break;
7036#endif
7037#ifdef TARGET_NR_setsockopt
7038 case TARGET_NR_setsockopt:
7039 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
7040 break;
7041#endif
7494b0f9 7042
31e31b8a 7043 case TARGET_NR_syslog:
579a97f7
FB
7044 if (!(p = lock_user_string(arg2)))
7045 goto efault;
e5574487
TS
7046 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
7047 unlock_user(p, arg2, 0);
7494b0f9
TS
7048 break;
7049
31e31b8a 7050 case TARGET_NR_setitimer:
66fb9763 7051 {
66fb9763
FB
7052 struct itimerval value, ovalue, *pvalue;
7053
53a5960a 7054 if (arg2) {
66fb9763 7055 pvalue = &value;
788f5ec4
TS
7056 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
7057 || copy_from_user_timeval(&pvalue->it_value,
7058 arg2 + sizeof(struct target_timeval)))
7059 goto efault;
66fb9763
FB
7060 } else {
7061 pvalue = NULL;
7062 }
7063 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
53a5960a 7064 if (!is_error(ret) && arg3) {
788f5ec4
TS
7065 if (copy_to_user_timeval(arg3,
7066 &ovalue.it_interval)
7067 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
7068 &ovalue.it_value))
7069 goto efault;
66fb9763
FB
7070 }
7071 }
7072 break;
31e31b8a 7073 case TARGET_NR_getitimer:
66fb9763 7074 {
66fb9763 7075 struct itimerval value;
3b46e624 7076
66fb9763 7077 ret = get_errno(getitimer(arg1, &value));
53a5960a 7078 if (!is_error(ret) && arg2) {
788f5ec4
TS
7079 if (copy_to_user_timeval(arg2,
7080 &value.it_interval)
7081 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
7082 &value.it_value))
7083 goto efault;
66fb9763
FB
7084 }
7085 }
7086 break;
31e31b8a 7087 case TARGET_NR_stat:
579a97f7
FB
7088 if (!(p = lock_user_string(arg1)))
7089 goto efault;
53a5960a
PB
7090 ret = get_errno(stat(path(p), &st));
7091 unlock_user(p, arg1, 0);
31e31b8a
FB
7092 goto do_stat;
7093 case TARGET_NR_lstat:
579a97f7
FB
7094 if (!(p = lock_user_string(arg1)))
7095 goto efault;
53a5960a
PB
7096 ret = get_errno(lstat(path(p), &st));
7097 unlock_user(p, arg1, 0);
31e31b8a
FB
7098 goto do_stat;
7099 case TARGET_NR_fstat:
7100 {
7101 ret = get_errno(fstat(arg1, &st));
7102 do_stat:
7103 if (!is_error(ret)) {
53a5960a 7104 struct target_stat *target_st;
e3584658 7105
579a97f7
FB
7106 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
7107 goto efault;
12727917 7108 memset(target_st, 0, sizeof(*target_st));
d2fd1af7
FB
7109 __put_user(st.st_dev, &target_st->st_dev);
7110 __put_user(st.st_ino, &target_st->st_ino);
7111 __put_user(st.st_mode, &target_st->st_mode);
7112 __put_user(st.st_uid, &target_st->st_uid);
7113 __put_user(st.st_gid, &target_st->st_gid);
7114 __put_user(st.st_nlink, &target_st->st_nlink);
7115 __put_user(st.st_rdev, &target_st->st_rdev);
7116 __put_user(st.st_size, &target_st->st_size);
7117 __put_user(st.st_blksize, &target_st->st_blksize);
7118 __put_user(st.st_blocks, &target_st->st_blocks);
7119 __put_user(st.st_atime, &target_st->target_st_atime);
7120 __put_user(st.st_mtime, &target_st->target_st_mtime);
7121 __put_user(st.st_ctime, &target_st->target_st_ctime);
53a5960a 7122 unlock_user_struct(target_st, arg2, 1);
31e31b8a
FB
7123 }
7124 }
7125 break;
ebc05488 7126#ifdef TARGET_NR_olduname
31e31b8a
FB
7127 case TARGET_NR_olduname:
7128 goto unimplemented;
ebc05488
FB
7129#endif
7130#ifdef TARGET_NR_iopl
31e31b8a
FB
7131 case TARGET_NR_iopl:
7132 goto unimplemented;
ebc05488 7133#endif
31e31b8a
FB
7134 case TARGET_NR_vhangup:
7135 ret = get_errno(vhangup());
7136 break;
ebc05488 7137#ifdef TARGET_NR_idle
31e31b8a
FB
7138 case TARGET_NR_idle:
7139 goto unimplemented;
42ad6ae9
FB
7140#endif
7141#ifdef TARGET_NR_syscall
7142 case TARGET_NR_syscall:
5945cfcb
PM
7143 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
7144 arg6, arg7, arg8, 0);
7145 break;
ebc05488 7146#endif
31e31b8a
FB
7147 case TARGET_NR_wait4:
7148 {
7149 int status;
992f48a0 7150 abi_long status_ptr = arg2;
31e31b8a 7151 struct rusage rusage, *rusage_ptr;
992f48a0 7152 abi_ulong target_rusage = arg4;
a39fb273 7153 abi_long rusage_err;
31e31b8a
FB
7154 if (target_rusage)
7155 rusage_ptr = &rusage;
7156 else
7157 rusage_ptr = NULL;
7158 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
7159 if (!is_error(ret)) {
5379557b 7160 if (status_ptr && ret) {
1d9d8b55 7161 status = host_to_target_waitstatus(status);
2f619698
FB
7162 if (put_user_s32(status, status_ptr))
7163 goto efault;
31e31b8a 7164 }
a39fb273
PJ
7165 if (target_rusage) {
7166 rusage_err = host_to_target_rusage(target_rusage, &rusage);
7167 if (rusage_err) {
7168 ret = rusage_err;
7169 }
7170 }
31e31b8a
FB
7171 }
7172 }
7173 break;
e5febef5 7174#ifdef TARGET_NR_swapoff
31e31b8a 7175 case TARGET_NR_swapoff:
579a97f7
FB
7176 if (!(p = lock_user_string(arg1)))
7177 goto efault;
53a5960a
PB
7178 ret = get_errno(swapoff(p));
7179 unlock_user(p, arg1, 0);
31e31b8a 7180 break;
e5febef5 7181#endif
31e31b8a 7182 case TARGET_NR_sysinfo:
a5448a7d 7183 {
53a5960a 7184 struct target_sysinfo *target_value;
a5448a7d
FB
7185 struct sysinfo value;
7186 ret = get_errno(sysinfo(&value));
53a5960a 7187 if (!is_error(ret) && arg1)
a5448a7d 7188 {
579a97f7
FB
7189 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
7190 goto efault;
a5448a7d
FB
7191 __put_user(value.uptime, &target_value->uptime);
7192 __put_user(value.loads[0], &target_value->loads[0]);
7193 __put_user(value.loads[1], &target_value->loads[1]);
7194 __put_user(value.loads[2], &target_value->loads[2]);
7195 __put_user(value.totalram, &target_value->totalram);
7196 __put_user(value.freeram, &target_value->freeram);
7197 __put_user(value.sharedram, &target_value->sharedram);
7198 __put_user(value.bufferram, &target_value->bufferram);
7199 __put_user(value.totalswap, &target_value->totalswap);
7200 __put_user(value.freeswap, &target_value->freeswap);
7201 __put_user(value.procs, &target_value->procs);
7202 __put_user(value.totalhigh, &target_value->totalhigh);
7203 __put_user(value.freehigh, &target_value->freehigh);
7204 __put_user(value.mem_unit, &target_value->mem_unit);
53a5960a 7205 unlock_user_struct(target_value, arg1, 1);
a5448a7d
FB
7206 }
7207 }
7208 break;
e5febef5 7209#ifdef TARGET_NR_ipc
31e31b8a 7210 case TARGET_NR_ipc:
8853f86e
FB
7211 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
7212 break;
e5febef5 7213#endif
e5289087
AJ
7214#ifdef TARGET_NR_semget
7215 case TARGET_NR_semget:
7216 ret = get_errno(semget(arg1, arg2, arg3));
7217 break;
7218#endif
7219#ifdef TARGET_NR_semop
7220 case TARGET_NR_semop:
c7128c9f 7221 ret = do_semop(arg1, arg2, arg3);
e5289087
AJ
7222 break;
7223#endif
7224#ifdef TARGET_NR_semctl
7225 case TARGET_NR_semctl:
7226 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
7227 break;
7228#endif
eeb438c1
AJ
7229#ifdef TARGET_NR_msgctl
7230 case TARGET_NR_msgctl:
7231 ret = do_msgctl(arg1, arg2, arg3);
7232 break;
7233#endif
7234#ifdef TARGET_NR_msgget
7235 case TARGET_NR_msgget:
7236 ret = get_errno(msgget(arg1, arg2));
7237 break;
7238#endif
7239#ifdef TARGET_NR_msgrcv
7240 case TARGET_NR_msgrcv:
7241 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
7242 break;
7243#endif
7244#ifdef TARGET_NR_msgsnd
7245 case TARGET_NR_msgsnd:
7246 ret = do_msgsnd(arg1, arg2, arg3, arg4);
7247 break;
88a8c984
RV
7248#endif
7249#ifdef TARGET_NR_shmget
7250 case TARGET_NR_shmget:
7251 ret = get_errno(shmget(arg1, arg2, arg3));
7252 break;
7253#endif
7254#ifdef TARGET_NR_shmctl
7255 case TARGET_NR_shmctl:
7256 ret = do_shmctl(arg1, arg2, arg3);
7257 break;
7258#endif
7259#ifdef TARGET_NR_shmat
7260 case TARGET_NR_shmat:
7261 ret = do_shmat(arg1, arg2, arg3);
7262 break;
7263#endif
7264#ifdef TARGET_NR_shmdt
7265 case TARGET_NR_shmdt:
7266 ret = do_shmdt(arg1);
7267 break;
eeb438c1 7268#endif
31e31b8a
FB
7269 case TARGET_NR_fsync:
7270 ret = get_errno(fsync(arg1));
7271 break;
31e31b8a 7272 case TARGET_NR_clone:
4ce6243d
PM
7273 /* Linux manages to have three different orderings for its
7274 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7275 * match the kernel's CONFIG_CLONE_* settings.
7276 * Microblaze is further special in that it uses a sixth
7277 * implicit argument to clone for the TLS pointer.
7278 */
7279#if defined(TARGET_MICROBLAZE)
a5b3bdcb 7280 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
4ce6243d
PM
7281#elif defined(TARGET_CLONE_BACKWARDS)
7282 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
7283#elif defined(TARGET_CLONE_BACKWARDS2)
a4c075f1 7284 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
0b6d3ae0 7285#else
4ce6243d 7286 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
0b6d3ae0 7287#endif
1b6b029e 7288 break;
ec86b0fb
FB
7289#ifdef __NR_exit_group
7290 /* new thread calls */
7291 case TARGET_NR_exit_group:
9788c9ca 7292#ifdef TARGET_GPROF
6d946cda
AJ
7293 _mcleanup();
7294#endif
e9009676 7295 gdb_exit(cpu_env, arg1);
ec86b0fb
FB
7296 ret = get_errno(exit_group(arg1));
7297 break;
7298#endif
31e31b8a 7299 case TARGET_NR_setdomainname:
579a97f7
FB
7300 if (!(p = lock_user_string(arg1)))
7301 goto efault;
53a5960a
PB
7302 ret = get_errno(setdomainname(p, arg2));
7303 unlock_user(p, arg1, 0);
31e31b8a
FB
7304 break;
7305 case TARGET_NR_uname:
7306 /* no need to transcode because we use the linux syscall */
29e619b1
FB
7307 {
7308 struct new_utsname * buf;
3b46e624 7309
579a97f7
FB
7310 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
7311 goto efault;
29e619b1
FB
7312 ret = get_errno(sys_uname(buf));
7313 if (!is_error(ret)) {
7314 /* Overrite the native machine name with whatever is being
7315 emulated. */
da79030f 7316 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
c5937220
PB
7317 /* Allow the user to override the reported release. */
7318 if (qemu_uname_release && *qemu_uname_release)
7319 strcpy (buf->release, qemu_uname_release);
29e619b1 7320 }
53a5960a 7321 unlock_user_struct(buf, arg1, 1);
29e619b1 7322 }
31e31b8a 7323 break;
6dbad63e 7324#ifdef TARGET_I386
31e31b8a 7325 case TARGET_NR_modify_ldt:
03acab66 7326 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5cd4393b 7327 break;
84409ddb 7328#if !defined(TARGET_X86_64)
5cd4393b
FB
7329 case TARGET_NR_vm86old:
7330 goto unimplemented;
7331 case TARGET_NR_vm86:
53a5960a 7332 ret = do_vm86(cpu_env, arg1, arg2);
6dbad63e 7333 break;
84409ddb 7334#endif
6dbad63e 7335#endif
31e31b8a
FB
7336 case TARGET_NR_adjtimex:
7337 goto unimplemented;
e5febef5 7338#ifdef TARGET_NR_create_module
31e31b8a 7339 case TARGET_NR_create_module:
e5febef5 7340#endif
31e31b8a
FB
7341 case TARGET_NR_init_module:
7342 case TARGET_NR_delete_module:
e5febef5 7343#ifdef TARGET_NR_get_kernel_syms
31e31b8a 7344 case TARGET_NR_get_kernel_syms:
e5febef5 7345#endif
31e31b8a
FB
7346 goto unimplemented;
7347 case TARGET_NR_quotactl:
7348 goto unimplemented;
7349 case TARGET_NR_getpgid:
7350 ret = get_errno(getpgid(arg1));
7351 break;
7352 case TARGET_NR_fchdir:
7353 ret = get_errno(fchdir(arg1));
7354 break;
84409ddb 7355#ifdef TARGET_NR_bdflush /* not on x86_64 */
31e31b8a
FB
7356 case TARGET_NR_bdflush:
7357 goto unimplemented;
84409ddb 7358#endif
e5febef5 7359#ifdef TARGET_NR_sysfs
31e31b8a
FB
7360 case TARGET_NR_sysfs:
7361 goto unimplemented;
e5febef5 7362#endif
31e31b8a 7363 case TARGET_NR_personality:
1b6b029e 7364 ret = get_errno(personality(arg1));
31e31b8a 7365 break;
e5febef5 7366#ifdef TARGET_NR_afs_syscall
31e31b8a
FB
7367 case TARGET_NR_afs_syscall:
7368 goto unimplemented;
e5febef5 7369#endif
7a3148a9 7370#ifdef TARGET_NR__llseek /* Not on alpha */
31e31b8a
FB
7371 case TARGET_NR__llseek:
7372 {
0c1592d9 7373 int64_t res;
d35b261c 7374#if !defined(__NR_llseek)
0c1592d9
PM
7375 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7376 if (res == -1) {
7377 ret = get_errno(res);
7378 } else {
7379 ret = 0;
7380 }
4f2ac237 7381#else
31e31b8a 7382 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4f2ac237 7383#endif
0c1592d9
PM
7384 if ((ret == 0) && put_user_s64(res, arg4)) {
7385 goto efault;
7386 }
31e31b8a
FB
7387 }
7388 break;
7a3148a9 7389#endif
31e31b8a 7390 case TARGET_NR_getdents:
3307e236 7391#ifdef __NR_getdents
d83c8733 7392#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4add45b4 7393 {
53a5960a 7394 struct target_dirent *target_dirp;
6556a833 7395 struct linux_dirent *dirp;
992f48a0 7396 abi_long count = arg3;
4add45b4
FB
7397
7398 dirp = malloc(count);
0da46a6e 7399 if (!dirp) {
579a97f7 7400 ret = -TARGET_ENOMEM;
0da46a6e
TS
7401 goto fail;
7402 }
3b46e624 7403
4add45b4
FB
7404 ret = get_errno(sys_getdents(arg1, dirp, count));
7405 if (!is_error(ret)) {
6556a833 7406 struct linux_dirent *de;
4add45b4
FB
7407 struct target_dirent *tde;
7408 int len = ret;
7409 int reclen, treclen;
7410 int count1, tnamelen;
7411
7412 count1 = 0;
7413 de = dirp;
579a97f7
FB
7414 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7415 goto efault;
4add45b4
FB
7416 tde = target_dirp;
7417 while (len > 0) {
7418 reclen = de->d_reclen;
333858b7
DL
7419 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
7420 assert(tnamelen >= 0);
7421 treclen = tnamelen + offsetof(struct target_dirent, d_name);
7422 assert(count1 + treclen <= count);
4add45b4 7423 tde->d_reclen = tswap16(treclen);
cbb21eed
MB
7424 tde->d_ino = tswapal(de->d_ino);
7425 tde->d_off = tswapal(de->d_off);
333858b7 7426 memcpy(tde->d_name, de->d_name, tnamelen);
6556a833 7427 de = (struct linux_dirent *)((char *)de + reclen);
4add45b4 7428 len -= reclen;
1c5bf3bf 7429 tde = (struct target_dirent *)((char *)tde + treclen);
4add45b4
FB
7430 count1 += treclen;
7431 }
7432 ret = count1;
579a97f7 7433 unlock_user(target_dirp, arg2, ret);
4add45b4
FB
7434 }
7435 free(dirp);
7436 }
7437#else
31e31b8a 7438 {
6556a833 7439 struct linux_dirent *dirp;
992f48a0 7440 abi_long count = arg3;
dab2ed99 7441
579a97f7
FB
7442 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7443 goto efault;
72f03900 7444 ret = get_errno(sys_getdents(arg1, dirp, count));
31e31b8a 7445 if (!is_error(ret)) {
6556a833 7446 struct linux_dirent *de;
31e31b8a
FB
7447 int len = ret;
7448 int reclen;
7449 de = dirp;
7450 while (len > 0) {
8083a3e5 7451 reclen = de->d_reclen;
31e31b8a
FB
7452 if (reclen > len)
7453 break;
8083a3e5 7454 de->d_reclen = tswap16(reclen);
31e31b8a
FB
7455 tswapls(&de->d_ino);
7456 tswapls(&de->d_off);
6556a833 7457 de = (struct linux_dirent *)((char *)de + reclen);
31e31b8a
FB
7458 len -= reclen;
7459 }
7460 }
53a5960a 7461 unlock_user(dirp, arg2, ret);
3307e236
PM
7462 }
7463#endif
7464#else
7465 /* Implement getdents in terms of getdents64 */
7466 {
7467 struct linux_dirent64 *dirp;
7468 abi_long count = arg3;
7469
7470 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
7471 if (!dirp) {
7472 goto efault;
7473 }
7474 ret = get_errno(sys_getdents64(arg1, dirp, count));
7475 if (!is_error(ret)) {
7476 /* Convert the dirent64 structs to target dirent. We do this
7477 * in-place, since we can guarantee that a target_dirent is no
7478 * larger than a dirent64; however this means we have to be
7479 * careful to read everything before writing in the new format.
7480 */
7481 struct linux_dirent64 *de;
7482 struct target_dirent *tde;
7483 int len = ret;
7484 int tlen = 0;
7485
7486 de = dirp;
7487 tde = (struct target_dirent *)dirp;
7488 while (len > 0) {
7489 int namelen, treclen;
7490 int reclen = de->d_reclen;
7491 uint64_t ino = de->d_ino;
7492 int64_t off = de->d_off;
7493 uint8_t type = de->d_type;
7494
7495 namelen = strlen(de->d_name);
7496 treclen = offsetof(struct target_dirent, d_name)
7497 + namelen + 2;
7498 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
7499
7500 memmove(tde->d_name, de->d_name, namelen + 1);
7501 tde->d_ino = tswapal(ino);
7502 tde->d_off = tswapal(off);
7503 tde->d_reclen = tswap16(treclen);
7504 /* The target_dirent type is in what was formerly a padding
7505 * byte at the end of the structure:
7506 */
7507 *(((char *)tde) + treclen - 1) = type;
7508
7509 de = (struct linux_dirent64 *)((char *)de + reclen);
7510 tde = (struct target_dirent *)((char *)tde + treclen);
7511 len -= reclen;
7512 tlen += treclen;
7513 }
7514 ret = tlen;
7515 }
7516 unlock_user(dirp, arg2, ret);
31e31b8a 7517 }
4add45b4 7518#endif
31e31b8a 7519 break;
3ae43202 7520#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
dab2ed99
FB
7521 case TARGET_NR_getdents64:
7522 {
6556a833 7523 struct linux_dirent64 *dirp;
992f48a0 7524 abi_long count = arg3;
579a97f7
FB
7525 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7526 goto efault;
dab2ed99
FB
7527 ret = get_errno(sys_getdents64(arg1, dirp, count));
7528 if (!is_error(ret)) {
6556a833 7529 struct linux_dirent64 *de;
dab2ed99
FB
7530 int len = ret;
7531 int reclen;
7532 de = dirp;
7533 while (len > 0) {
8083a3e5 7534 reclen = de->d_reclen;
dab2ed99
FB
7535 if (reclen > len)
7536 break;
8083a3e5 7537 de->d_reclen = tswap16(reclen);
8582a53a
FB
7538 tswap64s((uint64_t *)&de->d_ino);
7539 tswap64s((uint64_t *)&de->d_off);
6556a833 7540 de = (struct linux_dirent64 *)((char *)de + reclen);
dab2ed99
FB
7541 len -= reclen;
7542 }
7543 }
53a5960a 7544 unlock_user(dirp, arg2, ret);
dab2ed99
FB
7545 }
7546 break;
a541f297 7547#endif /* TARGET_NR_getdents64 */
9468a5d4 7548#if defined(TARGET_NR__newselect)
31e31b8a 7549 case TARGET_NR__newselect:
53a5960a 7550 ret = do_select(arg1, arg2, arg3, arg4, arg5);
31e31b8a 7551 break;
e5febef5 7552#endif
d8035d4c
MF
7553#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7554# ifdef TARGET_NR_poll
9de5e440 7555 case TARGET_NR_poll:
d8035d4c
MF
7556# endif
7557# ifdef TARGET_NR_ppoll
7558 case TARGET_NR_ppoll:
7559# endif
9de5e440 7560 {
53a5960a 7561 struct target_pollfd *target_pfd;
9de5e440
FB
7562 unsigned int nfds = arg2;
7563 int timeout = arg3;
7564 struct pollfd *pfd;
7854b056 7565 unsigned int i;
9de5e440 7566
579a97f7
FB
7567 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7568 if (!target_pfd)
7569 goto efault;
d8035d4c 7570
9de5e440
FB
7571 pfd = alloca(sizeof(struct pollfd) * nfds);
7572 for(i = 0; i < nfds; i++) {
5cd4393b
FB
7573 pfd[i].fd = tswap32(target_pfd[i].fd);
7574 pfd[i].events = tswap16(target_pfd[i].events);
9de5e440 7575 }
d8035d4c
MF
7576
7577# ifdef TARGET_NR_ppoll
7578 if (num == TARGET_NR_ppoll) {
7579 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7580 target_sigset_t *target_set;
7581 sigset_t _set, *set = &_set;
7582
7583 if (arg3) {
7584 if (target_to_host_timespec(timeout_ts, arg3)) {
7585 unlock_user(target_pfd, arg1, 0);
7586 goto efault;
7587 }
7588 } else {
7589 timeout_ts = NULL;
7590 }
7591
7592 if (arg4) {
7593 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7594 if (!target_set) {
7595 unlock_user(target_pfd, arg1, 0);
7596 goto efault;
7597 }
7598 target_to_host_sigset(set, target_set);
7599 } else {
7600 set = NULL;
7601 }
7602
7603 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7604
7605 if (!is_error(ret) && arg3) {
7606 host_to_target_timespec(arg3, timeout_ts);
7607 }
7608 if (arg4) {
7609 unlock_user(target_set, arg4, 0);
7610 }
7611 } else
7612# endif
7613 ret = get_errno(poll(pfd, nfds, timeout));
7614
9de5e440
FB
7615 if (!is_error(ret)) {
7616 for(i = 0; i < nfds; i++) {
5cd4393b 7617 target_pfd[i].revents = tswap16(pfd[i].revents);
9de5e440
FB
7618 }
7619 }
30cb4cde 7620 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
9de5e440
FB
7621 }
7622 break;
e5febef5 7623#endif
31e31b8a 7624 case TARGET_NR_flock:
9de5e440
FB
7625 /* NOTE: the flock constant seems to be the same for every
7626 Linux platform */
7627 ret = get_errno(flock(arg1, arg2));
31e31b8a
FB
7628 break;
7629 case TARGET_NR_readv:
7630 {
f287b2c2
RH
7631 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
7632 if (vec != NULL) {
7633 ret = get_errno(readv(arg1, vec, arg3));
7634 unlock_iovec(vec, arg2, arg3, 1);
7635 } else {
7636 ret = -host_to_target_errno(errno);
7637 }
31e31b8a
FB
7638 }
7639 break;
7640 case TARGET_NR_writev:
7641 {
f287b2c2
RH
7642 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
7643 if (vec != NULL) {
7644 ret = get_errno(writev(arg1, vec, arg3));
7645 unlock_iovec(vec, arg2, arg3, 0);
7646 } else {
7647 ret = -host_to_target_errno(errno);
7648 }
31e31b8a
FB
7649 }
7650 break;
7651 case TARGET_NR_getsid:
7652 ret = get_errno(getsid(arg1));
7653 break;
7a3148a9 7654#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
31e31b8a 7655 case TARGET_NR_fdatasync:
5cd4393b
FB
7656 ret = get_errno(fdatasync(arg1));
7657 break;
7a3148a9 7658#endif
31e31b8a 7659 case TARGET_NR__sysctl:
0da46a6e 7660 /* We don't implement this, but ENOTDIR is always a safe
29e619b1 7661 return value. */
0da46a6e
TS
7662 ret = -TARGET_ENOTDIR;
7663 break;
737de1d1
MF
7664 case TARGET_NR_sched_getaffinity:
7665 {
7666 unsigned int mask_size;
7667 unsigned long *mask;
7668
7669 /*
7670 * sched_getaffinity needs multiples of ulong, so need to take
7671 * care of mismatches between target ulong and host ulong sizes.
7672 */
7673 if (arg2 & (sizeof(abi_ulong) - 1)) {
7674 ret = -TARGET_EINVAL;
7675 break;
7676 }
7677 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7678
7679 mask = alloca(mask_size);
7680 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7681
7682 if (!is_error(ret)) {
be3bd286
PM
7683 if (ret > arg2) {
7684 /* More data returned than the caller's buffer will fit.
7685 * This only happens if sizeof(abi_long) < sizeof(long)
7686 * and the caller passed us a buffer holding an odd number
7687 * of abi_longs. If the host kernel is actually using the
7688 * extra 4 bytes then fail EINVAL; otherwise we can just
7689 * ignore them and only copy the interesting part.
7690 */
7691 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
7692 if (numcpus > arg2 * 8) {
7693 ret = -TARGET_EINVAL;
7694 break;
7695 }
7696 ret = arg2;
7697 }
7698
cd18f05e 7699 if (copy_to_user(arg3, mask, ret)) {
737de1d1
MF
7700 goto efault;
7701 }
737de1d1
MF
7702 }
7703 }
7704 break;
7705 case TARGET_NR_sched_setaffinity:
7706 {
7707 unsigned int mask_size;
7708 unsigned long *mask;
7709
7710 /*
7711 * sched_setaffinity needs multiples of ulong, so need to take
7712 * care of mismatches between target ulong and host ulong sizes.
7713 */
7714 if (arg2 & (sizeof(abi_ulong) - 1)) {
7715 ret = -TARGET_EINVAL;
7716 break;
7717 }
7718 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7719
7720 mask = alloca(mask_size);
7721 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7722 goto efault;
7723 }
7724 memcpy(mask, p, arg2);
7725 unlock_user_struct(p, arg2, 0);
7726
7727 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7728 }
7729 break;
31e31b8a 7730 case TARGET_NR_sched_setparam:
5cd4393b 7731 {
53a5960a 7732 struct sched_param *target_schp;
5cd4393b 7733 struct sched_param schp;
53a5960a 7734
579a97f7
FB
7735 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7736 goto efault;
5cd4393b 7737 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 7738 unlock_user_struct(target_schp, arg2, 0);
5cd4393b
FB
7739 ret = get_errno(sched_setparam(arg1, &schp));
7740 }
7741 break;
31e31b8a 7742 case TARGET_NR_sched_getparam:
5cd4393b 7743 {
53a5960a 7744 struct sched_param *target_schp;
5cd4393b
FB
7745 struct sched_param schp;
7746 ret = get_errno(sched_getparam(arg1, &schp));
7747 if (!is_error(ret)) {
579a97f7
FB
7748 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7749 goto efault;
5cd4393b 7750 target_schp->sched_priority = tswap32(schp.sched_priority);
53a5960a 7751 unlock_user_struct(target_schp, arg2, 1);
5cd4393b
FB
7752 }
7753 }
7754 break;
31e31b8a 7755 case TARGET_NR_sched_setscheduler:
5cd4393b 7756 {
53a5960a 7757 struct sched_param *target_schp;
5cd4393b 7758 struct sched_param schp;
579a97f7
FB
7759 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7760 goto efault;
5cd4393b 7761 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 7762 unlock_user_struct(target_schp, arg3, 0);
5cd4393b
FB
7763 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7764 }
7765 break;
31e31b8a 7766 case TARGET_NR_sched_getscheduler:
5cd4393b
FB
7767 ret = get_errno(sched_getscheduler(arg1));
7768 break;
31e31b8a
FB
7769 case TARGET_NR_sched_yield:
7770 ret = get_errno(sched_yield());
7771 break;
7772 case TARGET_NR_sched_get_priority_max:
5cd4393b
FB
7773 ret = get_errno(sched_get_priority_max(arg1));
7774 break;
31e31b8a 7775 case TARGET_NR_sched_get_priority_min:
5cd4393b
FB
7776 ret = get_errno(sched_get_priority_min(arg1));
7777 break;
31e31b8a 7778 case TARGET_NR_sched_rr_get_interval:
5cd4393b 7779 {
5cd4393b
FB
7780 struct timespec ts;
7781 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7782 if (!is_error(ret)) {
53a5960a 7783 host_to_target_timespec(arg2, &ts);
5cd4393b
FB
7784 }
7785 }
7786 break;
31e31b8a 7787 case TARGET_NR_nanosleep:
1b6b029e 7788 {
1b6b029e 7789 struct timespec req, rem;
53a5960a 7790 target_to_host_timespec(&req, arg1);
1b6b029e 7791 ret = get_errno(nanosleep(&req, &rem));
53a5960a
PB
7792 if (is_error(ret) && arg2) {
7793 host_to_target_timespec(arg2, &rem);
1b6b029e
FB
7794 }
7795 }
7796 break;
e5febef5 7797#ifdef TARGET_NR_query_module
31e31b8a 7798 case TARGET_NR_query_module:
5cd4393b 7799 goto unimplemented;
e5febef5
TS
7800#endif
7801#ifdef TARGET_NR_nfsservctl
31e31b8a 7802 case TARGET_NR_nfsservctl:
5cd4393b 7803 goto unimplemented;
e5febef5 7804#endif
31e31b8a 7805 case TARGET_NR_prctl:
1e6722f8
PM
7806 switch (arg1) {
7807 case PR_GET_PDEATHSIG:
7808 {
7809 int deathsig;
7810 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
7811 if (!is_error(ret) && arg2
7812 && put_user_ual(deathsig, arg2)) {
7813 goto efault;
e5574487 7814 }
1e6722f8
PM
7815 break;
7816 }
db9526b1
PM
7817#ifdef PR_GET_NAME
7818 case PR_GET_NAME:
7819 {
7820 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
7821 if (!name) {
7822 goto efault;
7823 }
7824 ret = get_errno(prctl(arg1, (unsigned long)name,
7825 arg3, arg4, arg5));
7826 unlock_user(name, arg2, 16);
7827 break;
7828 }
7829 case PR_SET_NAME:
7830 {
7831 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
7832 if (!name) {
7833 goto efault;
7834 }
7835 ret = get_errno(prctl(arg1, (unsigned long)name,
7836 arg3, arg4, arg5));
7837 unlock_user(name, arg2, 0);
7838 break;
7839 }
7840#endif
1e6722f8
PM
7841 default:
7842 /* Most prctl options have no pointer arguments */
7843 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7844 break;
7845 }
39b9aae1 7846 break;
d2fd1af7
FB
7847#ifdef TARGET_NR_arch_prctl
7848 case TARGET_NR_arch_prctl:
7849#if defined(TARGET_I386) && !defined(TARGET_ABI32)
7850 ret = do_arch_prctl(cpu_env, arg1, arg2);
7851 break;
7852#else
7853 goto unimplemented;
7854#endif
7855#endif
f2c7ba15
AJ
7856#ifdef TARGET_NR_pread64
7857 case TARGET_NR_pread64:
ae017a5b
AG
7858 if (regpairs_aligned(cpu_env)) {
7859 arg4 = arg5;
7860 arg5 = arg6;
7861 }
f2c7ba15
AJ
7862 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7863 goto efault;
7864 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7865 unlock_user(p, arg2, ret);
7866 break;
7867 case TARGET_NR_pwrite64:
ae017a5b
AG
7868 if (regpairs_aligned(cpu_env)) {
7869 arg4 = arg5;
7870 arg5 = arg6;
7871 }
f2c7ba15
AJ
7872 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7873 goto efault;
7874 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7875 unlock_user(p, arg2, 0);
7876 break;
67867308 7877#endif
31e31b8a 7878 case TARGET_NR_getcwd:
579a97f7
FB
7879 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7880 goto efault;
53a5960a
PB
7881 ret = get_errno(sys_getcwd1(p, arg2));
7882 unlock_user(p, arg1, ret);
31e31b8a
FB
7883 break;
7884 case TARGET_NR_capget:
7885 case TARGET_NR_capset:
e0eb210e
PM
7886 {
7887 struct target_user_cap_header *target_header;
7888 struct target_user_cap_data *target_data = NULL;
7889 struct __user_cap_header_struct header;
7890 struct __user_cap_data_struct data[2];
7891 struct __user_cap_data_struct *dataptr = NULL;
7892 int i, target_datalen;
7893 int data_items = 1;
7894
7895 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
7896 goto efault;
7897 }
7898 header.version = tswap32(target_header->version);
7899 header.pid = tswap32(target_header->pid);
7900
ec864874 7901 if (header.version != _LINUX_CAPABILITY_VERSION) {
e0eb210e
PM
7902 /* Version 2 and up takes pointer to two user_data structs */
7903 data_items = 2;
7904 }
7905
7906 target_datalen = sizeof(*target_data) * data_items;
7907
7908 if (arg2) {
7909 if (num == TARGET_NR_capget) {
7910 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
7911 } else {
7912 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
7913 }
7914 if (!target_data) {
7915 unlock_user_struct(target_header, arg1, 0);
7916 goto efault;
7917 }
7918
7919 if (num == TARGET_NR_capset) {
7920 for (i = 0; i < data_items; i++) {
7921 data[i].effective = tswap32(target_data[i].effective);
7922 data[i].permitted = tswap32(target_data[i].permitted);
7923 data[i].inheritable = tswap32(target_data[i].inheritable);
7924 }
7925 }
7926
7927 dataptr = data;
7928 }
7929
7930 if (num == TARGET_NR_capget) {
7931 ret = get_errno(capget(&header, dataptr));
7932 } else {
7933 ret = get_errno(capset(&header, dataptr));
7934 }
7935
7936 /* The kernel always updates version for both capget and capset */
7937 target_header->version = tswap32(header.version);
7938 unlock_user_struct(target_header, arg1, 1);
7939
7940 if (arg2) {
7941 if (num == TARGET_NR_capget) {
7942 for (i = 0; i < data_items; i++) {
7943 target_data[i].effective = tswap32(data[i].effective);
7944 target_data[i].permitted = tswap32(data[i].permitted);
7945 target_data[i].inheritable = tswap32(data[i].inheritable);
7946 }
7947 unlock_user(target_data, arg2, target_datalen);
7948 } else {
7949 unlock_user(target_data, arg2, 0);
7950 }
7951 }
7952 break;
7953 }
31e31b8a 7954 case TARGET_NR_sigaltstack:
198a74de 7955#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
c761c154 7956 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
d962783e 7957 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
9349b4f9 7958 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
a04e134a
TS
7959 break;
7960#else
5cd4393b 7961 goto unimplemented;
a04e134a 7962#endif
a8fd1aba
PM
7963
7964#ifdef CONFIG_SENDFILE
7965 case TARGET_NR_sendfile:
7966 {
7967 off_t *offp = NULL;
7968 off_t off;
7969 if (arg3) {
7970 ret = get_user_sal(off, arg3);
7971 if (is_error(ret)) {
7972 break;
7973 }
7974 offp = &off;
7975 }
7976 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7977 if (!is_error(ret) && arg3) {
7978 abi_long ret2 = put_user_sal(off, arg3);
7979 if (is_error(ret2)) {
7980 ret = ret2;
7981 }
7982 }
7983 break;
7984 }
7985#ifdef TARGET_NR_sendfile64
7986 case TARGET_NR_sendfile64:
7987 {
7988 off_t *offp = NULL;
7989 off_t off;
7990 if (arg3) {
7991 ret = get_user_s64(off, arg3);
7992 if (is_error(ret)) {
7993 break;
7994 }
7995 offp = &off;
7996 }
7997 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7998 if (!is_error(ret) && arg3) {
7999 abi_long ret2 = put_user_s64(off, arg3);
8000 if (is_error(ret2)) {
8001 ret = ret2;
8002 }
8003 }
8004 break;
8005 }
8006#endif
8007#else
31e31b8a 8008 case TARGET_NR_sendfile:
7edd2cf1 8009#ifdef TARGET_NR_sendfile64
a8fd1aba
PM
8010 case TARGET_NR_sendfile64:
8011#endif
5cd4393b 8012 goto unimplemented;
a8fd1aba
PM
8013#endif
8014
ebc05488 8015#ifdef TARGET_NR_getpmsg
31e31b8a 8016 case TARGET_NR_getpmsg:
5cd4393b 8017 goto unimplemented;
ebc05488
FB
8018#endif
8019#ifdef TARGET_NR_putpmsg
31e31b8a 8020 case TARGET_NR_putpmsg:
5cd4393b 8021 goto unimplemented;
ebc05488 8022#endif
048f6b4d 8023#ifdef TARGET_NR_vfork
31e31b8a 8024 case TARGET_NR_vfork:
d865bab5
PB
8025 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
8026 0, 0, 0, 0));
31e31b8a 8027 break;
048f6b4d 8028#endif
ebc05488 8029#ifdef TARGET_NR_ugetrlimit
31e31b8a 8030 case TARGET_NR_ugetrlimit:
728584be
FB
8031 {
8032 struct rlimit rlim;
e22b7015
WT
8033 int resource = target_to_host_resource(arg1);
8034 ret = get_errno(getrlimit(resource, &rlim));
728584be 8035 if (!is_error(ret)) {
53a5960a 8036 struct target_rlimit *target_rlim;
579a97f7
FB
8037 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
8038 goto efault;
81bbe906
TY
8039 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
8040 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 8041 unlock_user_struct(target_rlim, arg2, 1);
728584be
FB
8042 }
8043 break;
8044 }
ebc05488 8045#endif
a315a145 8046#ifdef TARGET_NR_truncate64
31e31b8a 8047 case TARGET_NR_truncate64:
579a97f7
FB
8048 if (!(p = lock_user_string(arg1)))
8049 goto efault;
53a5960a
PB
8050 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
8051 unlock_user(p, arg1, 0);
667f38b1 8052 break;
a315a145
FB
8053#endif
8054#ifdef TARGET_NR_ftruncate64
31e31b8a 8055 case TARGET_NR_ftruncate64:
ce4defa0 8056 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
667f38b1 8057 break;
a315a145
FB
8058#endif
8059#ifdef TARGET_NR_stat64
31e31b8a 8060 case TARGET_NR_stat64:
579a97f7
FB
8061 if (!(p = lock_user_string(arg1)))
8062 goto efault;
53a5960a
PB
8063 ret = get_errno(stat(path(p), &st));
8064 unlock_user(p, arg1, 0);
6a24a778
AZ
8065 if (!is_error(ret))
8066 ret = host_to_target_stat64(cpu_env, arg2, &st);
8067 break;
a315a145
FB
8068#endif
8069#ifdef TARGET_NR_lstat64
31e31b8a 8070 case TARGET_NR_lstat64:
579a97f7
FB
8071 if (!(p = lock_user_string(arg1)))
8072 goto efault;
53a5960a
PB
8073 ret = get_errno(lstat(path(p), &st));
8074 unlock_user(p, arg1, 0);
6a24a778
AZ
8075 if (!is_error(ret))
8076 ret = host_to_target_stat64(cpu_env, arg2, &st);
8077 break;
a315a145
FB
8078#endif
8079#ifdef TARGET_NR_fstat64
31e31b8a 8080 case TARGET_NR_fstat64:
6a24a778
AZ
8081 ret = get_errno(fstat(arg1, &st));
8082 if (!is_error(ret))
8083 ret = host_to_target_stat64(cpu_env, arg2, &st);
8084 break;
ce4defa0 8085#endif
c0d472b1 8086#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
9d33b76b 8087#ifdef TARGET_NR_fstatat64
6a24a778 8088 case TARGET_NR_fstatat64:
9d33b76b
AJ
8089#endif
8090#ifdef TARGET_NR_newfstatat
8091 case TARGET_NR_newfstatat:
8092#endif
6a24a778
AZ
8093 if (!(p = lock_user_string(arg2)))
8094 goto efault;
c0d472b1 8095 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
6a24a778
AZ
8096 if (!is_error(ret))
8097 ret = host_to_target_stat64(cpu_env, arg3, &st);
60cd49d5 8098 break;
a315a145 8099#endif
67867308 8100 case TARGET_NR_lchown:
579a97f7
FB
8101 if (!(p = lock_user_string(arg1)))
8102 goto efault;
53a5960a
PB
8103 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
8104 unlock_user(p, arg1, 0);
67867308 8105 break;
0c866a7e 8106#ifdef TARGET_NR_getuid
67867308
FB
8107 case TARGET_NR_getuid:
8108 ret = get_errno(high2lowuid(getuid()));
8109 break;
0c866a7e
RV
8110#endif
8111#ifdef TARGET_NR_getgid
67867308
FB
8112 case TARGET_NR_getgid:
8113 ret = get_errno(high2lowgid(getgid()));
8114 break;
0c866a7e
RV
8115#endif
8116#ifdef TARGET_NR_geteuid
67867308
FB
8117 case TARGET_NR_geteuid:
8118 ret = get_errno(high2lowuid(geteuid()));
8119 break;
0c866a7e
RV
8120#endif
8121#ifdef TARGET_NR_getegid
67867308
FB
8122 case TARGET_NR_getegid:
8123 ret = get_errno(high2lowgid(getegid()));
8124 break;
0c866a7e 8125#endif
67867308
FB
8126 case TARGET_NR_setreuid:
8127 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
8128 break;
8129 case TARGET_NR_setregid:
8130 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
8131 break;
8132 case TARGET_NR_getgroups:
8133 {
8134 int gidsetsize = arg1;
0c866a7e 8135 target_id *target_grouplist;
67867308
FB
8136 gid_t *grouplist;
8137 int i;
8138
8139 grouplist = alloca(gidsetsize * sizeof(gid_t));
8140 ret = get_errno(getgroups(gidsetsize, grouplist));
cb3bc233
AZ
8141 if (gidsetsize == 0)
8142 break;
67867308 8143 if (!is_error(ret)) {
03903ffc 8144 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
579a97f7
FB
8145 if (!target_grouplist)
8146 goto efault;
a2155fcc 8147 for(i = 0;i < ret; i++)
0c866a7e 8148 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
03903ffc 8149 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
67867308
FB
8150 }
8151 }
8152 break;
8153 case TARGET_NR_setgroups:
8154 {
8155 int gidsetsize = arg1;
0c866a7e 8156 target_id *target_grouplist;
f2b79ce9 8157 gid_t *grouplist = NULL;
67867308 8158 int i;
f2b79ce9
DA
8159 if (gidsetsize) {
8160 grouplist = alloca(gidsetsize * sizeof(gid_t));
03903ffc 8161 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
f2b79ce9
DA
8162 if (!target_grouplist) {
8163 ret = -TARGET_EFAULT;
8164 goto fail;
8165 }
8166 for (i = 0; i < gidsetsize; i++) {
8167 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
8168 }
8169 unlock_user(target_grouplist, arg2, 0);
579a97f7 8170 }
67867308
FB
8171 ret = get_errno(setgroups(gidsetsize, grouplist));
8172 }
8173 break;
8174 case TARGET_NR_fchown:
8175 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
8176 break;
c0d472b1 8177#if defined(TARGET_NR_fchownat)
ccfa72b7 8178 case TARGET_NR_fchownat:
579a97f7
FB
8179 if (!(p = lock_user_string(arg2)))
8180 goto efault;
c0d472b1
PM
8181 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
8182 low2highgid(arg4), arg5));
579a97f7 8183 unlock_user(p, arg2, 0);
ccfa72b7
TS
8184 break;
8185#endif
67867308
FB
8186#ifdef TARGET_NR_setresuid
8187 case TARGET_NR_setresuid:
5fafdf24
TS
8188 ret = get_errno(setresuid(low2highuid(arg1),
8189 low2highuid(arg2),
67867308
FB
8190 low2highuid(arg3)));
8191 break;
8192#endif
8193#ifdef TARGET_NR_getresuid
8194 case TARGET_NR_getresuid:
8195 {
53a5960a 8196 uid_t ruid, euid, suid;
67867308
FB
8197 ret = get_errno(getresuid(&ruid, &euid, &suid));
8198 if (!is_error(ret)) {
76ca310a
PM
8199 if (put_user_id(high2lowuid(ruid), arg1)
8200 || put_user_id(high2lowuid(euid), arg2)
8201 || put_user_id(high2lowuid(suid), arg3))
2f619698 8202 goto efault;
67867308
FB
8203 }
8204 }
8205 break;
8206#endif
8207#ifdef TARGET_NR_getresgid
8208 case TARGET_NR_setresgid:
5fafdf24
TS
8209 ret = get_errno(setresgid(low2highgid(arg1),
8210 low2highgid(arg2),
67867308
FB
8211 low2highgid(arg3)));
8212 break;
8213#endif
8214#ifdef TARGET_NR_getresgid
8215 case TARGET_NR_getresgid:
8216 {
53a5960a 8217 gid_t rgid, egid, sgid;
67867308
FB
8218 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8219 if (!is_error(ret)) {
76ca310a
PM
8220 if (put_user_id(high2lowgid(rgid), arg1)
8221 || put_user_id(high2lowgid(egid), arg2)
8222 || put_user_id(high2lowgid(sgid), arg3))
2f619698 8223 goto efault;
67867308
FB
8224 }
8225 }
8226 break;
8227#endif
8228 case TARGET_NR_chown:
579a97f7
FB
8229 if (!(p = lock_user_string(arg1)))
8230 goto efault;
53a5960a
PB
8231 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
8232 unlock_user(p, arg1, 0);
67867308
FB
8233 break;
8234 case TARGET_NR_setuid:
8235 ret = get_errno(setuid(low2highuid(arg1)));
8236 break;
8237 case TARGET_NR_setgid:
8238 ret = get_errno(setgid(low2highgid(arg1)));
8239 break;
8240 case TARGET_NR_setfsuid:
8241 ret = get_errno(setfsuid(arg1));
8242 break;
8243 case TARGET_NR_setfsgid:
8244 ret = get_errno(setfsgid(arg1));
8245 break;
67867308 8246
a315a145 8247#ifdef TARGET_NR_lchown32
31e31b8a 8248 case TARGET_NR_lchown32:
579a97f7
FB
8249 if (!(p = lock_user_string(arg1)))
8250 goto efault;
53a5960a
PB
8251 ret = get_errno(lchown(p, arg2, arg3));
8252 unlock_user(p, arg1, 0);
b03c60f3 8253 break;
a315a145
FB
8254#endif
8255#ifdef TARGET_NR_getuid32
31e31b8a 8256 case TARGET_NR_getuid32:
b03c60f3
FB
8257 ret = get_errno(getuid());
8258 break;
a315a145 8259#endif
64b4d28c
AJ
8260
8261#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
8262 /* Alpha specific */
8263 case TARGET_NR_getxuid:
ba0e276d
RH
8264 {
8265 uid_t euid;
8266 euid=geteuid();
8267 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
8268 }
64b4d28c
AJ
8269 ret = get_errno(getuid());
8270 break;
8271#endif
8272#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
8273 /* Alpha specific */
8274 case TARGET_NR_getxgid:
ba0e276d
RH
8275 {
8276 uid_t egid;
8277 egid=getegid();
8278 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
8279 }
64b4d28c
AJ
8280 ret = get_errno(getgid());
8281 break;
8282#endif
ba0e276d
RH
8283#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
8284 /* Alpha specific */
8285 case TARGET_NR_osf_getsysinfo:
8286 ret = -TARGET_EOPNOTSUPP;
8287 switch (arg1) {
8288 case TARGET_GSI_IEEE_FP_CONTROL:
8289 {
8290 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
8291
8292 /* Copied from linux ieee_fpcr_to_swcr. */
8293 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
8294 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
8295 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
8296 | SWCR_TRAP_ENABLE_DZE
8297 | SWCR_TRAP_ENABLE_OVF);
8298 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
8299 | SWCR_TRAP_ENABLE_INE);
8300 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
8301 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
8302
8303 if (put_user_u64 (swcr, arg2))
8304 goto efault;
8305 ret = 0;
8306 }
8307 break;
8308
8309 /* case GSI_IEEE_STATE_AT_SIGNAL:
8310 -- Not implemented in linux kernel.
8311 case GSI_UACPROC:
8312 -- Retrieves current unaligned access state; not much used.
8313 case GSI_PROC_TYPE:
8314 -- Retrieves implver information; surely not used.
8315 case GSI_GET_HWRPB:
8316 -- Grabs a copy of the HWRPB; surely not used.
8317 */
8318 }
8319 break;
8320#endif
8321#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8322 /* Alpha specific */
8323 case TARGET_NR_osf_setsysinfo:
8324 ret = -TARGET_EOPNOTSUPP;
8325 switch (arg1) {
8326 case TARGET_SSI_IEEE_FP_CONTROL:
ba0e276d
RH
8327 {
8328 uint64_t swcr, fpcr, orig_fpcr;
8329
6e06d515 8330 if (get_user_u64 (swcr, arg2)) {
ba0e276d 8331 goto efault;
6e06d515
RH
8332 }
8333 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
ba0e276d
RH
8334 fpcr = orig_fpcr & FPCR_DYN_MASK;
8335
8336 /* Copied from linux ieee_swcr_to_fpcr. */
8337 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
8338 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
8339 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
8340 | SWCR_TRAP_ENABLE_DZE
8341 | SWCR_TRAP_ENABLE_OVF)) << 48;
8342 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
8343 | SWCR_TRAP_ENABLE_INE)) << 57;
8344 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
8345 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
8346
6e06d515 8347 cpu_alpha_store_fpcr(cpu_env, fpcr);
ba0e276d 8348 ret = 0;
6e06d515
RH
8349 }
8350 break;
8351
8352 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
8353 {
8354 uint64_t exc, fpcr, orig_fpcr;
8355 int si_code;
8356
8357 if (get_user_u64(exc, arg2)) {
8358 goto efault;
8359 }
ba0e276d 8360
6e06d515 8361 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
ba0e276d 8362
6e06d515
RH
8363 /* We only add to the exception status here. */
8364 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
8365
8366 cpu_alpha_store_fpcr(cpu_env, fpcr);
8367 ret = 0;
8368
8369 /* Old exceptions are not signaled. */
8370 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
8371
8372 /* If any exceptions set by this call,
8373 and are unmasked, send a signal. */
8374 si_code = 0;
8375 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
8376 si_code = TARGET_FPE_FLTRES;
8377 }
8378 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
8379 si_code = TARGET_FPE_FLTUND;
8380 }
8381 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
8382 si_code = TARGET_FPE_FLTOVF;
8383 }
8384 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
8385 si_code = TARGET_FPE_FLTDIV;
8386 }
8387 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
8388 si_code = TARGET_FPE_FLTINV;
8389 }
8390 if (si_code != 0) {
8391 target_siginfo_t info;
8392 info.si_signo = SIGFPE;
8393 info.si_errno = 0;
8394 info.si_code = si_code;
8395 info._sifields._sigfault._addr
8396 = ((CPUArchState *)cpu_env)->pc;
8397 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
ba0e276d
RH
8398 }
8399 }
8400 break;
8401
8402 /* case SSI_NVPAIRS:
8403 -- Used with SSIN_UACPROC to enable unaligned accesses.
8404 case SSI_IEEE_STATE_AT_SIGNAL:
8405 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8406 -- Not implemented in linux kernel
8407 */
8408 }
8409 break;
8410#endif
8411#ifdef TARGET_NR_osf_sigprocmask
8412 /* Alpha specific. */
8413 case TARGET_NR_osf_sigprocmask:
8414 {
8415 abi_ulong mask;
bc088ba1 8416 int how;
ba0e276d
RH
8417 sigset_t set, oldset;
8418
8419 switch(arg1) {
8420 case TARGET_SIG_BLOCK:
8421 how = SIG_BLOCK;
8422 break;
8423 case TARGET_SIG_UNBLOCK:
8424 how = SIG_UNBLOCK;
8425 break;
8426 case TARGET_SIG_SETMASK:
8427 how = SIG_SETMASK;
8428 break;
8429 default:
8430 ret = -TARGET_EINVAL;
8431 goto fail;
8432 }
8433 mask = arg2;
8434 target_to_host_old_sigset(&set, &mask);
1c275925 8435 do_sigprocmask(how, &set, &oldset);
ba0e276d
RH
8436 host_to_target_old_sigset(&mask, &oldset);
8437 ret = mask;
8438 }
8439 break;
8440#endif
64b4d28c 8441
a315a145 8442#ifdef TARGET_NR_getgid32
31e31b8a 8443 case TARGET_NR_getgid32:
b03c60f3
FB
8444 ret = get_errno(getgid());
8445 break;
a315a145
FB
8446#endif
8447#ifdef TARGET_NR_geteuid32
31e31b8a 8448 case TARGET_NR_geteuid32:
b03c60f3
FB
8449 ret = get_errno(geteuid());
8450 break;
a315a145
FB
8451#endif
8452#ifdef TARGET_NR_getegid32
31e31b8a 8453 case TARGET_NR_getegid32:
b03c60f3
FB
8454 ret = get_errno(getegid());
8455 break;
a315a145
FB
8456#endif
8457#ifdef TARGET_NR_setreuid32
31e31b8a 8458 case TARGET_NR_setreuid32:
b03c60f3
FB
8459 ret = get_errno(setreuid(arg1, arg2));
8460 break;
a315a145
FB
8461#endif
8462#ifdef TARGET_NR_setregid32
31e31b8a 8463 case TARGET_NR_setregid32:
b03c60f3
FB
8464 ret = get_errno(setregid(arg1, arg2));
8465 break;
a315a145
FB
8466#endif
8467#ifdef TARGET_NR_getgroups32
31e31b8a 8468 case TARGET_NR_getgroups32:
99c475ab
FB
8469 {
8470 int gidsetsize = arg1;
53a5960a 8471 uint32_t *target_grouplist;
99c475ab
FB
8472 gid_t *grouplist;
8473 int i;
8474
8475 grouplist = alloca(gidsetsize * sizeof(gid_t));
8476 ret = get_errno(getgroups(gidsetsize, grouplist));
cb3bc233
AZ
8477 if (gidsetsize == 0)
8478 break;
99c475ab 8479 if (!is_error(ret)) {
579a97f7
FB
8480 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
8481 if (!target_grouplist) {
8482 ret = -TARGET_EFAULT;
8483 goto fail;
8484 }
a2155fcc 8485 for(i = 0;i < ret; i++)
53a5960a
PB
8486 target_grouplist[i] = tswap32(grouplist[i]);
8487 unlock_user(target_grouplist, arg2, gidsetsize * 4);
99c475ab
FB
8488 }
8489 }
8490 break;
a315a145
FB
8491#endif
8492#ifdef TARGET_NR_setgroups32
31e31b8a 8493 case TARGET_NR_setgroups32:
99c475ab
FB
8494 {
8495 int gidsetsize = arg1;
53a5960a 8496 uint32_t *target_grouplist;
99c475ab
FB
8497 gid_t *grouplist;
8498 int i;
3b46e624 8499
99c475ab 8500 grouplist = alloca(gidsetsize * sizeof(gid_t));
579a97f7
FB
8501 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
8502 if (!target_grouplist) {
8503 ret = -TARGET_EFAULT;
8504 goto fail;
8505 }
99c475ab 8506 for(i = 0;i < gidsetsize; i++)
53a5960a
PB
8507 grouplist[i] = tswap32(target_grouplist[i]);
8508 unlock_user(target_grouplist, arg2, 0);
99c475ab
FB
8509 ret = get_errno(setgroups(gidsetsize, grouplist));
8510 }
8511 break;
a315a145
FB
8512#endif
8513#ifdef TARGET_NR_fchown32
31e31b8a 8514 case TARGET_NR_fchown32:
b03c60f3
FB
8515 ret = get_errno(fchown(arg1, arg2, arg3));
8516 break;
a315a145
FB
8517#endif
8518#ifdef TARGET_NR_setresuid32
31e31b8a 8519 case TARGET_NR_setresuid32:
b03c60f3
FB
8520 ret = get_errno(setresuid(arg1, arg2, arg3));
8521 break;
a315a145
FB
8522#endif
8523#ifdef TARGET_NR_getresuid32
31e31b8a 8524 case TARGET_NR_getresuid32:
b03c60f3 8525 {
53a5960a 8526 uid_t ruid, euid, suid;
b03c60f3
FB
8527 ret = get_errno(getresuid(&ruid, &euid, &suid));
8528 if (!is_error(ret)) {
2f619698
FB
8529 if (put_user_u32(ruid, arg1)
8530 || put_user_u32(euid, arg2)
8531 || put_user_u32(suid, arg3))
8532 goto efault;
b03c60f3
FB
8533 }
8534 }
8535 break;
a315a145
FB
8536#endif
8537#ifdef TARGET_NR_setresgid32
31e31b8a 8538 case TARGET_NR_setresgid32:
b03c60f3
FB
8539 ret = get_errno(setresgid(arg1, arg2, arg3));
8540 break;
a315a145
FB
8541#endif
8542#ifdef TARGET_NR_getresgid32
31e31b8a 8543 case TARGET_NR_getresgid32:
b03c60f3 8544 {
53a5960a 8545 gid_t rgid, egid, sgid;
b03c60f3
FB
8546 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8547 if (!is_error(ret)) {
2f619698
FB
8548 if (put_user_u32(rgid, arg1)
8549 || put_user_u32(egid, arg2)
8550 || put_user_u32(sgid, arg3))
8551 goto efault;
b03c60f3
FB
8552 }
8553 }
8554 break;
a315a145
FB
8555#endif
8556#ifdef TARGET_NR_chown32
31e31b8a 8557 case TARGET_NR_chown32:
579a97f7
FB
8558 if (!(p = lock_user_string(arg1)))
8559 goto efault;
53a5960a
PB
8560 ret = get_errno(chown(p, arg2, arg3));
8561 unlock_user(p, arg1, 0);
b03c60f3 8562 break;
a315a145
FB
8563#endif
8564#ifdef TARGET_NR_setuid32
31e31b8a 8565 case TARGET_NR_setuid32:
b03c60f3
FB
8566 ret = get_errno(setuid(arg1));
8567 break;
a315a145
FB
8568#endif
8569#ifdef TARGET_NR_setgid32
31e31b8a 8570 case TARGET_NR_setgid32:
b03c60f3
FB
8571 ret = get_errno(setgid(arg1));
8572 break;
a315a145
FB
8573#endif
8574#ifdef TARGET_NR_setfsuid32
31e31b8a 8575 case TARGET_NR_setfsuid32:
b03c60f3
FB
8576 ret = get_errno(setfsuid(arg1));
8577 break;
a315a145
FB
8578#endif
8579#ifdef TARGET_NR_setfsgid32
31e31b8a 8580 case TARGET_NR_setfsgid32:
b03c60f3
FB
8581 ret = get_errno(setfsgid(arg1));
8582 break;
a315a145 8583#endif
67867308 8584
31e31b8a 8585 case TARGET_NR_pivot_root:
b03c60f3 8586 goto unimplemented;
ffa65c3b 8587#ifdef TARGET_NR_mincore
31e31b8a 8588 case TARGET_NR_mincore:
04bb9ace
AJ
8589 {
8590 void *a;
8591 ret = -TARGET_EFAULT;
8592 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8593 goto efault;
8594 if (!(p = lock_user_string(arg3)))
8595 goto mincore_fail;
8596 ret = get_errno(mincore(a, arg2, p));
8597 unlock_user(p, arg3, ret);
8598 mincore_fail:
8599 unlock_user(a, arg1, 0);
8600 }
8601 break;
ffa65c3b 8602#endif
408321b6
AJ
8603#ifdef TARGET_NR_arm_fadvise64_64
8604 case TARGET_NR_arm_fadvise64_64:
8605 {
8606 /*
8607 * arm_fadvise64_64 looks like fadvise64_64 but
8608 * with different argument order
8609 */
8610 abi_long temp;
8611 temp = arg3;
8612 arg3 = arg4;
8613 arg4 = temp;
8614 }
8615#endif
e72d2cc7 8616#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
408321b6
AJ
8617#ifdef TARGET_NR_fadvise64_64
8618 case TARGET_NR_fadvise64_64:
8619#endif
e72d2cc7
UH
8620#ifdef TARGET_NR_fadvise64
8621 case TARGET_NR_fadvise64:
8622#endif
8623#ifdef TARGET_S390X
8624 switch (arg4) {
8625 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8626 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8627 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8628 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8629 default: break;
8630 }
8631#endif
8632 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
408321b6
AJ
8633 break;
8634#endif
ffa65c3b 8635#ifdef TARGET_NR_madvise
31e31b8a 8636 case TARGET_NR_madvise:
24836689 8637 /* A straight passthrough may not be safe because qemu sometimes
d2d6b857 8638 turns private file-backed mappings into anonymous mappings.
24836689
PB
8639 This will break MADV_DONTNEED.
8640 This is a hint, so ignoring and returning success is ok. */
8641 ret = get_errno(0);
8642 break;
ffa65c3b 8643#endif
992f48a0 8644#if TARGET_ABI_BITS == 32
31e31b8a 8645 case TARGET_NR_fcntl64:
77e4672d 8646 {
b1e341eb 8647 int cmd;
77e4672d 8648 struct flock64 fl;
53a5960a 8649 struct target_flock64 *target_fl;
ce4defa0 8650#ifdef TARGET_ARM
53a5960a 8651 struct target_eabi_flock64 *target_efl;
ce4defa0 8652#endif
77e4672d 8653
5f106811 8654 cmd = target_to_host_fcntl_cmd(arg2);
31b63193
PM
8655 if (cmd == -TARGET_EINVAL) {
8656 ret = cmd;
8657 break;
8658 }
b1e341eb 8659
60cd49d5 8660 switch(arg2) {
b1e341eb 8661 case TARGET_F_GETLK64:
5813427b
TS
8662#ifdef TARGET_ARM
8663 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
8664 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8665 goto efault;
5813427b
TS
8666 fl.l_type = tswap16(target_efl->l_type);
8667 fl.l_whence = tswap16(target_efl->l_whence);
8668 fl.l_start = tswap64(target_efl->l_start);
8669 fl.l_len = tswap64(target_efl->l_len);
7e22e546 8670 fl.l_pid = tswap32(target_efl->l_pid);
5813427b
TS
8671 unlock_user_struct(target_efl, arg3, 0);
8672 } else
8673#endif
8674 {
9ee1fa2c
FB
8675 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8676 goto efault;
5813427b
TS
8677 fl.l_type = tswap16(target_fl->l_type);
8678 fl.l_whence = tswap16(target_fl->l_whence);
8679 fl.l_start = tswap64(target_fl->l_start);
8680 fl.l_len = tswap64(target_fl->l_len);
7e22e546 8681 fl.l_pid = tswap32(target_fl->l_pid);
5813427b
TS
8682 unlock_user_struct(target_fl, arg3, 0);
8683 }
b1e341eb 8684 ret = get_errno(fcntl(arg1, cmd, &fl));
77e4672d 8685 if (ret == 0) {
ce4defa0
PB
8686#ifdef TARGET_ARM
8687 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
8688 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8689 goto efault;
ce4defa0
PB
8690 target_efl->l_type = tswap16(fl.l_type);
8691 target_efl->l_whence = tswap16(fl.l_whence);
8692 target_efl->l_start = tswap64(fl.l_start);
8693 target_efl->l_len = tswap64(fl.l_len);
7e22e546 8694 target_efl->l_pid = tswap32(fl.l_pid);
53a5960a 8695 unlock_user_struct(target_efl, arg3, 1);
ce4defa0
PB
8696 } else
8697#endif
8698 {
9ee1fa2c
FB
8699 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8700 goto efault;
ce4defa0
PB
8701 target_fl->l_type = tswap16(fl.l_type);
8702 target_fl->l_whence = tswap16(fl.l_whence);
8703 target_fl->l_start = tswap64(fl.l_start);
8704 target_fl->l_len = tswap64(fl.l_len);
7e22e546 8705 target_fl->l_pid = tswap32(fl.l_pid);
53a5960a 8706 unlock_user_struct(target_fl, arg3, 1);
ce4defa0 8707 }
77e4672d
FB
8708 }
8709 break;
8710
b1e341eb
TS
8711 case TARGET_F_SETLK64:
8712 case TARGET_F_SETLKW64:
ce4defa0
PB
8713#ifdef TARGET_ARM
8714 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
8715 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8716 goto efault;
ce4defa0
PB
8717 fl.l_type = tswap16(target_efl->l_type);
8718 fl.l_whence = tswap16(target_efl->l_whence);
8719 fl.l_start = tswap64(target_efl->l_start);
8720 fl.l_len = tswap64(target_efl->l_len);
7e22e546 8721 fl.l_pid = tswap32(target_efl->l_pid);
53a5960a 8722 unlock_user_struct(target_efl, arg3, 0);
ce4defa0
PB
8723 } else
8724#endif
8725 {
9ee1fa2c
FB
8726 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8727 goto efault;
ce4defa0
PB
8728 fl.l_type = tswap16(target_fl->l_type);
8729 fl.l_whence = tswap16(target_fl->l_whence);
8730 fl.l_start = tswap64(target_fl->l_start);
8731 fl.l_len = tswap64(target_fl->l_len);
7e22e546 8732 fl.l_pid = tswap32(target_fl->l_pid);
53a5960a 8733 unlock_user_struct(target_fl, arg3, 0);
ce4defa0 8734 }
b1e341eb 8735 ret = get_errno(fcntl(arg1, cmd, &fl));
77e4672d 8736 break;
60cd49d5 8737 default:
5f106811 8738 ret = do_fcntl(arg1, arg2, arg3);
60cd49d5
FB
8739 break;
8740 }
77e4672d
FB
8741 break;
8742 }
60cd49d5 8743#endif
7d600c80
TS
8744#ifdef TARGET_NR_cacheflush
8745 case TARGET_NR_cacheflush:
8746 /* self-modifying code is handled automatically, so nothing needed */
8747 ret = 0;
8748 break;
8749#endif
ebc05488 8750#ifdef TARGET_NR_security
31e31b8a
FB
8751 case TARGET_NR_security:
8752 goto unimplemented;
c573ff67
FB
8753#endif
8754#ifdef TARGET_NR_getpagesize
8755 case TARGET_NR_getpagesize:
8756 ret = TARGET_PAGE_SIZE;
8757 break;
ebc05488 8758#endif
31e31b8a
FB
8759 case TARGET_NR_gettid:
8760 ret = get_errno(gettid());
8761 break;
e5febef5 8762#ifdef TARGET_NR_readahead
31e31b8a 8763 case TARGET_NR_readahead:
2054ac9b 8764#if TARGET_ABI_BITS == 32
48e515d4 8765 if (regpairs_aligned(cpu_env)) {
2054ac9b
AJ
8766 arg2 = arg3;
8767 arg3 = arg4;
8768 arg4 = arg5;
8769 }
2054ac9b
AJ
8770 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8771#else
8772 ret = get_errno(readahead(arg1, arg2, arg3));
8773#endif
8774 break;
e5febef5 8775#endif
a790ae38 8776#ifdef CONFIG_ATTR
ebc05488 8777#ifdef TARGET_NR_setxattr
31e31b8a
FB
8778 case TARGET_NR_listxattr:
8779 case TARGET_NR_llistxattr:
fb5590f7
PM
8780 {
8781 void *p, *b = 0;
8782 if (arg2) {
8783 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8784 if (!b) {
8785 ret = -TARGET_EFAULT;
8786 break;
8787 }
8788 }
8789 p = lock_user_string(arg1);
8790 if (p) {
8791 if (num == TARGET_NR_listxattr) {
8792 ret = get_errno(listxattr(p, b, arg3));
8793 } else {
8794 ret = get_errno(llistxattr(p, b, arg3));
8795 }
8796 } else {
8797 ret = -TARGET_EFAULT;
8798 }
8799 unlock_user(p, arg1, 0);
8800 unlock_user(b, arg2, arg3);
8801 break;
8802 }
31e31b8a 8803 case TARGET_NR_flistxattr:
fb5590f7
PM
8804 {
8805 void *b = 0;
8806 if (arg2) {
8807 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8808 if (!b) {
8809 ret = -TARGET_EFAULT;
8810 break;
8811 }
8812 }
8813 ret = get_errno(flistxattr(arg1, b, arg3));
8814 unlock_user(b, arg2, arg3);
6f932f91 8815 break;
fb5590f7 8816 }
a790ae38 8817 case TARGET_NR_setxattr:
30297b55 8818 case TARGET_NR_lsetxattr:
a790ae38 8819 {
e3c33ec6
PM
8820 void *p, *n, *v = 0;
8821 if (arg3) {
8822 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8823 if (!v) {
8824 ret = -TARGET_EFAULT;
8825 break;
8826 }
8827 }
a790ae38
ACH
8828 p = lock_user_string(arg1);
8829 n = lock_user_string(arg2);
e3c33ec6 8830 if (p && n) {
30297b55
PM
8831 if (num == TARGET_NR_setxattr) {
8832 ret = get_errno(setxattr(p, n, v, arg4, arg5));
8833 } else {
8834 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
8835 }
a790ae38
ACH
8836 } else {
8837 ret = -TARGET_EFAULT;
8838 }
8839 unlock_user(p, arg1, 0);
8840 unlock_user(n, arg2, 0);
8841 unlock_user(v, arg3, 0);
8842 }
8843 break;
30297b55
PM
8844 case TARGET_NR_fsetxattr:
8845 {
8846 void *n, *v = 0;
8847 if (arg3) {
8848 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8849 if (!v) {
8850 ret = -TARGET_EFAULT;
8851 break;
8852 }
8853 }
8854 n = lock_user_string(arg2);
8855 if (n) {
8856 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
8857 } else {
8858 ret = -TARGET_EFAULT;
8859 }
8860 unlock_user(n, arg2, 0);
8861 unlock_user(v, arg3, 0);
8862 }
8863 break;
a790ae38 8864 case TARGET_NR_getxattr:
30297b55 8865 case TARGET_NR_lgetxattr:
a790ae38 8866 {
e3c33ec6
PM
8867 void *p, *n, *v = 0;
8868 if (arg3) {
8869 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8870 if (!v) {
8871 ret = -TARGET_EFAULT;
8872 break;
8873 }
8874 }
a790ae38
ACH
8875 p = lock_user_string(arg1);
8876 n = lock_user_string(arg2);
e3c33ec6 8877 if (p && n) {
30297b55
PM
8878 if (num == TARGET_NR_getxattr) {
8879 ret = get_errno(getxattr(p, n, v, arg4));
8880 } else {
8881 ret = get_errno(lgetxattr(p, n, v, arg4));
8882 }
a790ae38
ACH
8883 } else {
8884 ret = -TARGET_EFAULT;
8885 }
8886 unlock_user(p, arg1, 0);
8887 unlock_user(n, arg2, 0);
8888 unlock_user(v, arg3, arg4);
8889 }
8890 break;
30297b55
PM
8891 case TARGET_NR_fgetxattr:
8892 {
8893 void *n, *v = 0;
8894 if (arg3) {
8895 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8896 if (!v) {
8897 ret = -TARGET_EFAULT;
8898 break;
8899 }
8900 }
8901 n = lock_user_string(arg2);
8902 if (n) {
8903 ret = get_errno(fgetxattr(arg1, n, v, arg4));
8904 } else {
8905 ret = -TARGET_EFAULT;
8906 }
8907 unlock_user(n, arg2, 0);
8908 unlock_user(v, arg3, arg4);
8909 }
8910 break;
a790ae38 8911 case TARGET_NR_removexattr:
30297b55 8912 case TARGET_NR_lremovexattr:
a790ae38
ACH
8913 {
8914 void *p, *n;
8915 p = lock_user_string(arg1);
8916 n = lock_user_string(arg2);
8917 if (p && n) {
30297b55
PM
8918 if (num == TARGET_NR_removexattr) {
8919 ret = get_errno(removexattr(p, n));
8920 } else {
8921 ret = get_errno(lremovexattr(p, n));
8922 }
a790ae38
ACH
8923 } else {
8924 ret = -TARGET_EFAULT;
8925 }
8926 unlock_user(p, arg1, 0);
8927 unlock_user(n, arg2, 0);
8928 }
8929 break;
30297b55
PM
8930 case TARGET_NR_fremovexattr:
8931 {
8932 void *n;
8933 n = lock_user_string(arg2);
8934 if (n) {
8935 ret = get_errno(fremovexattr(arg1, n));
8936 } else {
8937 ret = -TARGET_EFAULT;
8938 }
8939 unlock_user(n, arg2, 0);
8940 }
8941 break;
ebc05488 8942#endif
a790ae38 8943#endif /* CONFIG_ATTR */
ebc05488 8944#ifdef TARGET_NR_set_thread_area
5cd4393b 8945 case TARGET_NR_set_thread_area:
8d18e893 8946#if defined(TARGET_MIPS)
d279279e 8947 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
6f5b89a0
TS
8948 ret = 0;
8949 break;
ef96779b
EI
8950#elif defined(TARGET_CRIS)
8951 if (arg1 & 0xff)
8952 ret = -TARGET_EINVAL;
8953 else {
8954 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
8955 ret = 0;
8956 }
8957 break;
8d18e893
FB
8958#elif defined(TARGET_I386) && defined(TARGET_ABI32)
8959 ret = do_set_thread_area(cpu_env, arg1);
8960 break;
1ccd9374
PM
8961#elif defined(TARGET_M68K)
8962 {
0429a971 8963 TaskState *ts = cpu->opaque;
1ccd9374 8964 ts->tp_value = arg1;
95c1eb19 8965 ret = 0;
1ccd9374
PM
8966 break;
8967 }
6f5b89a0
TS
8968#else
8969 goto unimplemented_nowarn;
8970#endif
8971#endif
8972#ifdef TARGET_NR_get_thread_area
5cd4393b 8973 case TARGET_NR_get_thread_area:
8d18e893
FB
8974#if defined(TARGET_I386) && defined(TARGET_ABI32)
8975 ret = do_get_thread_area(cpu_env, arg1);
d312bbe1 8976 break;
1ccd9374
PM
8977#elif defined(TARGET_M68K)
8978 {
0429a971 8979 TaskState *ts = cpu->opaque;
1ccd9374
PM
8980 ret = ts->tp_value;
8981 break;
8982 }
8d18e893 8983#else
5cd4393b 8984 goto unimplemented_nowarn;
48dc41eb 8985#endif
8d18e893 8986#endif
48dc41eb
FB
8987#ifdef TARGET_NR_getdomainname
8988 case TARGET_NR_getdomainname:
8989 goto unimplemented_nowarn;
ebc05488 8990#endif
6f5b89a0 8991
b5906f95
TS
8992#ifdef TARGET_NR_clock_gettime
8993 case TARGET_NR_clock_gettime:
8994 {
8995 struct timespec ts;
8996 ret = get_errno(clock_gettime(arg1, &ts));
8997 if (!is_error(ret)) {
8998 host_to_target_timespec(arg2, &ts);
8999 }
9000 break;
9001 }
9002#endif
9003#ifdef TARGET_NR_clock_getres
9004 case TARGET_NR_clock_getres:
9005 {
9006 struct timespec ts;
9007 ret = get_errno(clock_getres(arg1, &ts));
9008 if (!is_error(ret)) {
9009 host_to_target_timespec(arg2, &ts);
9010 }
9011 break;
9012 }
9013#endif
63d7651b
PB
9014#ifdef TARGET_NR_clock_nanosleep
9015 case TARGET_NR_clock_nanosleep:
9016 {
9017 struct timespec ts;
9018 target_to_host_timespec(&ts, arg3);
9019 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
9020 if (arg4)
9021 host_to_target_timespec(arg4, &ts);
9022 break;
9023 }
9024#endif
b5906f95 9025
6f5b89a0
TS
9026#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
9027 case TARGET_NR_set_tid_address:
579a97f7
FB
9028 ret = get_errno(set_tid_address((int *)g2h(arg1)));
9029 break;
6f5b89a0
TS
9030#endif
9031
3ae43202 9032#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
4cae1d16 9033 case TARGET_NR_tkill:
4cb05961 9034 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
4cae1d16
TS
9035 break;
9036#endif
9037
3ae43202 9038#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
71455574 9039 case TARGET_NR_tgkill:
4cb05961
PB
9040 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
9041 target_to_host_signal(arg3)));
71455574
TS
9042 break;
9043#endif
9044
4f2b1fe8
TS
9045#ifdef TARGET_NR_set_robust_list
9046 case TARGET_NR_set_robust_list:
e9a970a8
PM
9047 case TARGET_NR_get_robust_list:
9048 /* The ABI for supporting robust futexes has userspace pass
9049 * the kernel a pointer to a linked list which is updated by
9050 * userspace after the syscall; the list is walked by the kernel
9051 * when the thread exits. Since the linked list in QEMU guest
9052 * memory isn't a valid linked list for the host and we have
9053 * no way to reliably intercept the thread-death event, we can't
9054 * support these. Silently return ENOSYS so that guest userspace
9055 * falls back to a non-robust futex implementation (which should
9056 * be OK except in the corner case of the guest crashing while
9057 * holding a mutex that is shared with another process via
9058 * shared memory).
9059 */
9060 goto unimplemented_nowarn;
4f2b1fe8
TS
9061#endif
9062
1acae9f2 9063#if defined(TARGET_NR_utimensat)
9007f0ef
TS
9064 case TARGET_NR_utimensat:
9065 {
ebc996f3
RV
9066 struct timespec *tsp, ts[2];
9067 if (!arg3) {
9068 tsp = NULL;
9069 } else {
9070 target_to_host_timespec(ts, arg3);
9071 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
9072 tsp = ts;
9073 }
9007f0ef 9074 if (!arg2)
ebc996f3 9075 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
9007f0ef 9076 else {
579a97f7 9077 if (!(p = lock_user_string(arg2))) {
0da46a6e 9078 ret = -TARGET_EFAULT;
579a97f7
FB
9079 goto fail;
9080 }
ebc996f3 9081 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
579a97f7 9082 unlock_user(p, arg2, 0);
9007f0ef
TS
9083 }
9084 }
9085 break;
9086#endif
bd0c5661
PB
9087 case TARGET_NR_futex:
9088 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
9089 break;
dbfe4c36 9090#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
39b59763
AJ
9091 case TARGET_NR_inotify_init:
9092 ret = get_errno(sys_inotify_init());
9093 break;
9094#endif
a1606b0b 9095#ifdef CONFIG_INOTIFY1
c05c7a73
RV
9096#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
9097 case TARGET_NR_inotify_init1:
9098 ret = get_errno(sys_inotify_init1(arg1));
9099 break;
9100#endif
a1606b0b 9101#endif
dbfe4c36 9102#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
39b59763
AJ
9103 case TARGET_NR_inotify_add_watch:
9104 p = lock_user_string(arg2);
9105 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
9106 unlock_user(p, arg2, 0);
9107 break;
9108#endif
dbfe4c36 9109#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
39b59763
AJ
9110 case TARGET_NR_inotify_rm_watch:
9111 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
9112 break;
9113#endif
9007f0ef 9114
8ec9cf89 9115#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
24e1003a
AJ
9116 case TARGET_NR_mq_open:
9117 {
9118 struct mq_attr posix_mq_attr;
9119
9120 p = lock_user_string(arg1 - 1);
9121 if (arg4 != 0)
9122 copy_from_user_mq_attr (&posix_mq_attr, arg4);
9123 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
9124 unlock_user (p, arg1, 0);
9125 }
9126 break;
9127
9128 case TARGET_NR_mq_unlink:
9129 p = lock_user_string(arg1 - 1);
9130 ret = get_errno(mq_unlink(p));
9131 unlock_user (p, arg1, 0);
9132 break;
9133
9134 case TARGET_NR_mq_timedsend:
9135 {
9136 struct timespec ts;
9137
9138 p = lock_user (VERIFY_READ, arg2, arg3, 1);
9139 if (arg5 != 0) {
9140 target_to_host_timespec(&ts, arg5);
9141 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
9142 host_to_target_timespec(arg5, &ts);
9143 }
9144 else
9145 ret = get_errno(mq_send(arg1, p, arg3, arg4));
9146 unlock_user (p, arg2, arg3);
9147 }
9148 break;
9149
9150 case TARGET_NR_mq_timedreceive:
9151 {
9152 struct timespec ts;
9153 unsigned int prio;
9154
9155 p = lock_user (VERIFY_READ, arg2, arg3, 1);
9156 if (arg5 != 0) {
9157 target_to_host_timespec(&ts, arg5);
9158 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
9159 host_to_target_timespec(arg5, &ts);
9160 }
9161 else
9162 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
9163 unlock_user (p, arg2, arg3);
9164 if (arg4 != 0)
9165 put_user_u32(prio, arg4);
9166 }
9167 break;
9168
9169 /* Not implemented for now... */
9170/* case TARGET_NR_mq_notify: */
9171/* break; */
9172
9173 case TARGET_NR_mq_getsetattr:
9174 {
9175 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
9176 ret = 0;
9177 if (arg3 != 0) {
9178 ret = mq_getattr(arg1, &posix_mq_attr_out);
9179 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
9180 }
9181 if (arg2 != 0) {
9182 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
9183 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
9184 }
9185
9186 }
9187 break;
9188#endif
9189
3ce34dfb
VS
9190#ifdef CONFIG_SPLICE
9191#ifdef TARGET_NR_tee
9192 case TARGET_NR_tee:
9193 {
9194 ret = get_errno(tee(arg1,arg2,arg3,arg4));
9195 }
9196 break;
9197#endif
9198#ifdef TARGET_NR_splice
9199 case TARGET_NR_splice:
9200 {
9201 loff_t loff_in, loff_out;
9202 loff_t *ploff_in = NULL, *ploff_out = NULL;
9203 if(arg2) {
9204 get_user_u64(loff_in, arg2);
9205 ploff_in = &loff_in;
9206 }
9207 if(arg4) {
9208 get_user_u64(loff_out, arg2);
9209 ploff_out = &loff_out;
9210 }
9211 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
9212 }
9213 break;
9214#endif
9215#ifdef TARGET_NR_vmsplice
9216 case TARGET_NR_vmsplice:
9217 {
f287b2c2
RH
9218 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
9219 if (vec != NULL) {
9220 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
9221 unlock_iovec(vec, arg2, arg3, 0);
9222 } else {
9223 ret = -host_to_target_errno(errno);
9224 }
3ce34dfb
VS
9225 }
9226 break;
9227#endif
9228#endif /* CONFIG_SPLICE */
c2882b96
RV
9229#ifdef CONFIG_EVENTFD
9230#if defined(TARGET_NR_eventfd)
9231 case TARGET_NR_eventfd:
9232 ret = get_errno(eventfd(arg1, 0));
9233 break;
9234#endif
9235#if defined(TARGET_NR_eventfd2)
9236 case TARGET_NR_eventfd2:
5947c697
PJ
9237 {
9238 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
9239 if (arg2 & TARGET_O_NONBLOCK) {
9240 host_flags |= O_NONBLOCK;
9241 }
9242 if (arg2 & TARGET_O_CLOEXEC) {
9243 host_flags |= O_CLOEXEC;
9244 }
9245 ret = get_errno(eventfd(arg1, host_flags));
c2882b96 9246 break;
5947c697 9247 }
c2882b96
RV
9248#endif
9249#endif /* CONFIG_EVENTFD */
d0927938
UH
9250#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
9251 case TARGET_NR_fallocate:
20249ae1
AG
9252#if TARGET_ABI_BITS == 32
9253 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
9254 target_offset64(arg5, arg6)));
9255#else
d0927938 9256 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
20249ae1 9257#endif
d0927938 9258 break;
c727f47d
PM
9259#endif
9260#if defined(CONFIG_SYNC_FILE_RANGE)
9261#if defined(TARGET_NR_sync_file_range)
9262 case TARGET_NR_sync_file_range:
9263#if TARGET_ABI_BITS == 32
bfcedc57
RV
9264#if defined(TARGET_MIPS)
9265 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
9266 target_offset64(arg5, arg6), arg7));
9267#else
c727f47d
PM
9268 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
9269 target_offset64(arg4, arg5), arg6));
bfcedc57 9270#endif /* !TARGET_MIPS */
c727f47d
PM
9271#else
9272 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
9273#endif
9274 break;
9275#endif
9276#if defined(TARGET_NR_sync_file_range2)
9277 case TARGET_NR_sync_file_range2:
9278 /* This is like sync_file_range but the arguments are reordered */
9279#if TARGET_ABI_BITS == 32
9280 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
9281 target_offset64(arg5, arg6), arg2));
9282#else
9283 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
9284#endif
9285 break;
9286#endif
3b6edd16
PM
9287#endif
9288#if defined(CONFIG_EPOLL)
9289#if defined(TARGET_NR_epoll_create)
9290 case TARGET_NR_epoll_create:
9291 ret = get_errno(epoll_create(arg1));
9292 break;
9293#endif
9294#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
9295 case TARGET_NR_epoll_create1:
9296 ret = get_errno(epoll_create1(arg1));
9297 break;
9298#endif
9299#if defined(TARGET_NR_epoll_ctl)
9300 case TARGET_NR_epoll_ctl:
9301 {
9302 struct epoll_event ep;
9303 struct epoll_event *epp = 0;
9304 if (arg4) {
9305 struct target_epoll_event *target_ep;
9306 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
9307 goto efault;
9308 }
9309 ep.events = tswap32(target_ep->events);
9310 /* The epoll_data_t union is just opaque data to the kernel,
9311 * so we transfer all 64 bits across and need not worry what
9312 * actual data type it is.
9313 */
9314 ep.data.u64 = tswap64(target_ep->data.u64);
9315 unlock_user_struct(target_ep, arg4, 0);
9316 epp = &ep;
9317 }
9318 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
9319 break;
9320 }
9321#endif
9322
9323#if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9324#define IMPLEMENT_EPOLL_PWAIT
9325#endif
9326#if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9327#if defined(TARGET_NR_epoll_wait)
9328 case TARGET_NR_epoll_wait:
9329#endif
9330#if defined(IMPLEMENT_EPOLL_PWAIT)
9331 case TARGET_NR_epoll_pwait:
9332#endif
9333 {
9334 struct target_epoll_event *target_ep;
9335 struct epoll_event *ep;
9336 int epfd = arg1;
9337 int maxevents = arg3;
9338 int timeout = arg4;
9339
9340 target_ep = lock_user(VERIFY_WRITE, arg2,
9341 maxevents * sizeof(struct target_epoll_event), 1);
9342 if (!target_ep) {
9343 goto efault;
9344 }
9345
9346 ep = alloca(maxevents * sizeof(struct epoll_event));
9347
9348 switch (num) {
9349#if defined(IMPLEMENT_EPOLL_PWAIT)
9350 case TARGET_NR_epoll_pwait:
9351 {
9352 target_sigset_t *target_set;
9353 sigset_t _set, *set = &_set;
9354
9355 if (arg5) {
9356 target_set = lock_user(VERIFY_READ, arg5,
9357 sizeof(target_sigset_t), 1);
9358 if (!target_set) {
9359 unlock_user(target_ep, arg2, 0);
9360 goto efault;
9361 }
9362 target_to_host_sigset(set, target_set);
9363 unlock_user(target_set, arg5, 0);
9364 } else {
9365 set = NULL;
9366 }
9367
9368 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
9369 break;
9370 }
9371#endif
9372#if defined(TARGET_NR_epoll_wait)
9373 case TARGET_NR_epoll_wait:
9374 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
9375 break;
9376#endif
9377 default:
9378 ret = -TARGET_ENOSYS;
9379 }
9380 if (!is_error(ret)) {
9381 int i;
9382 for (i = 0; i < ret; i++) {
9383 target_ep[i].events = tswap32(ep[i].events);
9384 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
9385 }
9386 }
9387 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
9388 break;
9389 }
9390#endif
163a05a8
PM
9391#endif
9392#ifdef TARGET_NR_prlimit64
9393 case TARGET_NR_prlimit64:
9394 {
9395 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9396 struct target_rlimit64 *target_rnew, *target_rold;
9397 struct host_rlimit64 rnew, rold, *rnewp = 0;
9398 if (arg3) {
9399 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
9400 goto efault;
9401 }
9402 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
9403 rnew.rlim_max = tswap64(target_rnew->rlim_max);
9404 unlock_user_struct(target_rnew, arg3, 0);
9405 rnewp = &rnew;
9406 }
9407
9408 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
9409 if (!is_error(ret) && arg4) {
9410 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
9411 goto efault;
9412 }
9413 target_rold->rlim_cur = tswap64(rold.rlim_cur);
9414 target_rold->rlim_max = tswap64(rold.rlim_max);
9415 unlock_user_struct(target_rold, arg4, 1);
9416 }
9417 break;
9418 }
3d21d29c
RH
9419#endif
9420#ifdef TARGET_NR_gethostname
9421 case TARGET_NR_gethostname:
9422 {
9423 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
9424 if (name) {
9425 ret = get_errno(gethostname(name, arg2));
9426 unlock_user(name, arg1, arg2);
9427 } else {
9428 ret = -TARGET_EFAULT;
9429 }
9430 break;
9431 }
89aaf1a6
RV
9432#endif
9433#ifdef TARGET_NR_atomic_cmpxchg_32
9434 case TARGET_NR_atomic_cmpxchg_32:
9435 {
9436 /* should use start_exclusive from main.c */
9437 abi_ulong mem_value;
9438 if (get_user_u32(mem_value, arg6)) {
9439 target_siginfo_t info;
9440 info.si_signo = SIGSEGV;
9441 info.si_errno = 0;
9442 info.si_code = TARGET_SEGV_MAPERR;
9443 info._sifields._sigfault._addr = arg6;
9444 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
9445 ret = 0xdeadbeef;
9446
9447 }
9448 if (mem_value == arg2)
9449 put_user_u32(arg1, arg6);
9450 ret = mem_value;
9451 break;
9452 }
9453#endif
9454#ifdef TARGET_NR_atomic_barrier
9455 case TARGET_NR_atomic_barrier:
9456 {
9457 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
3b899ea7 9458 ret = 0;
89aaf1a6
RV
9459 break;
9460 }
d0927938 9461#endif
f4f1e10a
ECL
9462
9463#ifdef TARGET_NR_timer_create
9464 case TARGET_NR_timer_create:
9465 {
9466 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
9467
9468 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
f4f1e10a
ECL
9469 struct target_timer_t *ptarget_timer;
9470
9471 int clkid = arg1;
9472 int timer_index = next_free_host_timer();
9473
9474 if (timer_index < 0) {
9475 ret = -TARGET_EAGAIN;
9476 } else {
9477 timer_t *phtimer = g_posix_timers + timer_index;
9478
9479 if (arg2) {
f4f1e10a 9480 phost_sevp = &host_sevp;
c065976f
PM
9481 ret = target_to_host_sigevent(phost_sevp, arg2);
9482 if (ret != 0) {
9483 break;
9484 }
f4f1e10a
ECL
9485 }
9486
9487 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
9488 if (ret) {
9489 phtimer = NULL;
9490 } else {
9491 if (!lock_user_struct(VERIFY_WRITE, ptarget_timer, arg3, 1)) {
9492 goto efault;
9493 }
9494 ptarget_timer->ptr = tswap32(0xcafe0000 | timer_index);
9495 unlock_user_struct(ptarget_timer, arg3, 1);
9496 }
9497 }
9498 break;
9499 }
9500#endif
9501
9502#ifdef TARGET_NR_timer_settime
9503 case TARGET_NR_timer_settime:
9504 {
9505 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
9506 * struct itimerspec * old_value */
9507 arg1 &= 0xffff;
9508 if (arg3 == 0 || arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
9509 ret = -TARGET_EINVAL;
9510 } else {
9511 timer_t htimer = g_posix_timers[arg1];
9512 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
9513
9514 target_to_host_itimerspec(&hspec_new, arg3);
9515 ret = get_errno(
9516 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
9517 host_to_target_itimerspec(arg2, &hspec_old);
9518 }
9519 break;
9520 }
9521#endif
9522
9523#ifdef TARGET_NR_timer_gettime
9524 case TARGET_NR_timer_gettime:
9525 {
9526 /* args: timer_t timerid, struct itimerspec *curr_value */
9527 arg1 &= 0xffff;
9528 if (!arg2) {
9529 return -TARGET_EFAULT;
9530 } else if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
9531 ret = -TARGET_EINVAL;
9532 } else {
9533 timer_t htimer = g_posix_timers[arg1];
9534 struct itimerspec hspec;
9535 ret = get_errno(timer_gettime(htimer, &hspec));
9536
9537 if (host_to_target_itimerspec(arg2, &hspec)) {
9538 ret = -TARGET_EFAULT;
9539 }
9540 }
9541 break;
9542 }
9543#endif
9544
9545#ifdef TARGET_NR_timer_getoverrun
9546 case TARGET_NR_timer_getoverrun:
9547 {
9548 /* args: timer_t timerid */
9549 arg1 &= 0xffff;
9550 if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
9551 ret = -TARGET_EINVAL;
9552 } else {
9553 timer_t htimer = g_posix_timers[arg1];
9554 ret = get_errno(timer_getoverrun(htimer));
9555 }
9556 break;
9557 }
9558#endif
9559
9560#ifdef TARGET_NR_timer_delete
9561 case TARGET_NR_timer_delete:
9562 {
9563 /* args: timer_t timerid */
9564 arg1 &= 0xffff;
9565 if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
9566 ret = -TARGET_EINVAL;
9567 } else {
9568 timer_t htimer = g_posix_timers[arg1];
9569 ret = get_errno(timer_delete(htimer));
9570 g_posix_timers[arg1] = 0;
9571 }
9572 break;
9573 }
9574#endif
9575
51834341
RV
9576#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
9577 case TARGET_NR_timerfd_create:
9578 ret = get_errno(timerfd_create(arg1,
9579 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
9580 break;
9581#endif
9582
9583#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
9584 case TARGET_NR_timerfd_gettime:
9585 {
9586 struct itimerspec its_curr;
9587
9588 ret = get_errno(timerfd_gettime(arg1, &its_curr));
9589
9590 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
9591 goto efault;
9592 }
9593 }
9594 break;
9595#endif
9596
9597#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
9598 case TARGET_NR_timerfd_settime:
9599 {
9600 struct itimerspec its_new, its_old, *p_new;
9601
9602 if (arg3) {
9603 if (target_to_host_itimerspec(&its_new, arg3)) {
9604 goto efault;
9605 }
9606 p_new = &its_new;
9607 } else {
9608 p_new = NULL;
9609 }
9610
9611 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
9612
9613 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
9614 goto efault;
9615 }
9616 }
9617 break;
9618#endif
9619
ab31cda3
PB
9620#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
9621 case TARGET_NR_ioprio_get:
9622 ret = get_errno(ioprio_get(arg1, arg2));
9623 break;
9624#endif
9625
9626#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
9627 case TARGET_NR_ioprio_set:
9628 ret = get_errno(ioprio_set(arg1, arg2, arg3));
9629 break;
9630#endif
9631
9af5c906
RV
9632#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
9633 case TARGET_NR_setns:
9634 ret = get_errno(setns(arg1, arg2));
9635 break;
9636#endif
9637#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
9638 case TARGET_NR_unshare:
9639 ret = get_errno(unshare(arg1));
9640 break;
9641#endif
9642
31e31b8a
FB
9643 default:
9644 unimplemented:
5cd4393b 9645 gemu_log("qemu: Unsupported syscall: %d\n", num);
4f2b1fe8 9646#if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5cd4393b 9647 unimplemented_nowarn:
80a9d035 9648#endif
0da46a6e 9649 ret = -TARGET_ENOSYS;
31e31b8a
FB
9650 break;
9651 }
579a97f7 9652fail:
c573ff67 9653#ifdef DEBUG
0bf9e31a 9654 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
c573ff67 9655#endif
b92c47c1
TS
9656 if(do_strace)
9657 print_syscall_ret(num, ret);
31e31b8a 9658 return ret;
579a97f7
FB
9659efault:
9660 ret = -TARGET_EFAULT;
9661 goto fail;
31e31b8a 9662}