]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/syscall.c
linux-user: Support ioctls whose parameter size is not constant
[mirror_qemu.git] / linux-user / syscall.c
CommitLineData
31e31b8a
FB
1/*
2 * Linux syscalls
5fafdf24 3 *
31e31b8a
FB
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31e31b8a 18 */
d5b3a9b6 19#define _ATFILE_SOURCE
31e31b8a
FB
20#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
04369ff2 23#include <string.h>
31e31b8a
FB
24#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
7854b056 29#include <time.h>
82e671d9 30#include <limits.h>
31e31b8a 31#include <sys/types.h>
d08d3bb8
TS
32#include <sys/ipc.h>
33#include <sys/msg.h>
31e31b8a
FB
34#include <sys/wait.h>
35#include <sys/time.h>
36#include <sys/stat.h>
37#include <sys/mount.h>
39b9aae1 38#include <sys/prctl.h>
31e31b8a
FB
39#include <sys/resource.h>
40#include <sys/mman.h>
41#include <sys/swap.h>
42#include <signal.h>
43#include <sched.h>
60e99246
AJ
44#ifdef __ia64__
45int __clone2(int (*fn)(void *), void *child_stack_base,
46 size_t stack_size, int flags, void *arg, ...);
47#endif
31e31b8a 48#include <sys/socket.h>
607175e0 49#include <sys/un.h>
31e31b8a 50#include <sys/uio.h>
9de5e440 51#include <sys/poll.h>
32f36bce 52#include <sys/times.h>
8853f86e 53#include <sys/shm.h>
fa294816 54#include <sys/sem.h>
56c8f68f 55#include <sys/statfs.h>
ebc05488 56#include <utime.h>
a5448a7d 57#include <sys/sysinfo.h>
3b3f24ad 58#include <sys/utsname.h>
72f03900 59//#include <sys/user.h>
8853f86e 60#include <netinet/ip.h>
7854b056 61#include <netinet/tcp.h>
0b6d3ae0 62#include <qemu-common.h>
9788c9ca 63#ifdef TARGET_GPROF
6d946cda
AJ
64#include <sys/gmon.h>
65#endif
c2882b96
RV
66#ifdef CONFIG_EVENTFD
67#include <sys/eventfd.h>
68#endif
31e31b8a
FB
69
70#define termios host_termios
71#define winsize host_winsize
72#define termio host_termio
04369ff2
FB
73#define sgttyb host_sgttyb /* same as target */
74#define tchars host_tchars /* same as target */
75#define ltchars host_ltchars /* same as target */
31e31b8a
FB
76
77#include <linux/termios.h>
78#include <linux/unistd.h>
79#include <linux/utsname.h>
80#include <linux/cdrom.h>
81#include <linux/hdreg.h>
82#include <linux/soundcard.h>
19b84f3c 83#include <linux/kd.h>
8fbd6b52 84#include <linux/mtio.h>
350d1779 85#include <linux/fs.h>
f7680a55
UH
86#include <linux/fb.h>
87#include <linux/vt.h>
d7e4036e 88#include "linux_loop.h"
da79030f 89#include "cpu-uname.h"
31e31b8a 90
3ef693a0 91#include "qemu.h"
526ccb7a 92#include "qemu-common.h"
31e31b8a 93
2f7bb878 94#if defined(CONFIG_USE_NPTL)
d865bab5
PB
95#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
96 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
97#else
98/* XXX: Hardcode the above values. */
99#define CLONE_NPTL_FLAGS2 0
30813cea
PB
100#endif
101
72f03900 102//#define DEBUG
31e31b8a 103
1a9353d2 104//#include <linux/msdos_fs.h>
6556a833
AJ
105#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
106#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
1a9353d2 107
70a194b9 108
70a194b9
FB
109#undef _syscall0
110#undef _syscall1
111#undef _syscall2
112#undef _syscall3
113#undef _syscall4
114#undef _syscall5
83fcb515 115#undef _syscall6
70a194b9 116
83fcb515 117#define _syscall0(type,name) \
8fcd3692 118static type name (void) \
83fcb515
FB
119{ \
120 return syscall(__NR_##name); \
121}
70a194b9 122
83fcb515 123#define _syscall1(type,name,type1,arg1) \
8fcd3692 124static type name (type1 arg1) \
83fcb515
FB
125{ \
126 return syscall(__NR_##name, arg1); \
70a194b9
FB
127}
128
83fcb515 129#define _syscall2(type,name,type1,arg1,type2,arg2) \
8fcd3692 130static type name (type1 arg1,type2 arg2) \
83fcb515
FB
131{ \
132 return syscall(__NR_##name, arg1, arg2); \
70a194b9
FB
133}
134
83fcb515 135#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
8fcd3692 136static type name (type1 arg1,type2 arg2,type3 arg3) \
83fcb515
FB
137{ \
138 return syscall(__NR_##name, arg1, arg2, arg3); \
70a194b9
FB
139}
140
83fcb515 141#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
8fcd3692 142static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
83fcb515
FB
143{ \
144 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
70a194b9
FB
145}
146
83fcb515
FB
147#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
148 type5,arg5) \
8fcd3692 149static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
83fcb515
FB
150{ \
151 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
70a194b9
FB
152}
153
83fcb515
FB
154
155#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
156 type5,arg5,type6,arg6) \
8fcd3692
BS
157static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
158 type6 arg6) \
83fcb515
FB
159{ \
160 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
70a194b9 161}
83fcb515 162
70a194b9 163
31e31b8a 164#define __NR_sys_uname __NR_uname
92a34c10 165#define __NR_sys_faccessat __NR_faccessat
814d7977 166#define __NR_sys_fchmodat __NR_fchmodat
ccfa72b7 167#define __NR_sys_fchownat __NR_fchownat
6a24a778 168#define __NR_sys_fstatat64 __NR_fstatat64
ac8a6556 169#define __NR_sys_futimesat __NR_futimesat
72f03900 170#define __NR_sys_getcwd1 __NR_getcwd
72f03900 171#define __NR_sys_getdents __NR_getdents
dab2ed99 172#define __NR_sys_getdents64 __NR_getdents64
c6cda17a 173#define __NR_sys_getpriority __NR_getpriority
64f0ce4c 174#define __NR_sys_linkat __NR_linkat
4472ad0d 175#define __NR_sys_mkdirat __NR_mkdirat
75ac37a0 176#define __NR_sys_mknodat __NR_mknodat
9d33b76b 177#define __NR_sys_newfstatat __NR_newfstatat
82424832 178#define __NR_sys_openat __NR_openat
5e0ccb18 179#define __NR_sys_readlinkat __NR_readlinkat
722183f6 180#define __NR_sys_renameat __NR_renameat
66fb9763 181#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
f0b6243d 182#define __NR_sys_symlinkat __NR_symlinkat
7494b0f9 183#define __NR_sys_syslog __NR_syslog
71455574 184#define __NR_sys_tgkill __NR_tgkill
4cae1d16 185#define __NR_sys_tkill __NR_tkill
8170f56b 186#define __NR_sys_unlinkat __NR_unlinkat
9007f0ef 187#define __NR_sys_utimensat __NR_utimensat
bd0c5661 188#define __NR_sys_futex __NR_futex
39b59763
AJ
189#define __NR_sys_inotify_init __NR_inotify_init
190#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
191#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
31e31b8a 192
bc51c5c9 193#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
9af9eaaa
FB
194#define __NR__llseek __NR_lseek
195#endif
196
72f03900 197#ifdef __NR_gettid
31e31b8a 198_syscall0(int, gettid)
72f03900 199#else
0da46a6e
TS
200/* This is a replacement for the host gettid() and must return a host
201 errno. */
72f03900
FB
202static int gettid(void) {
203 return -ENOSYS;
204}
205#endif
3b3f24ad 206_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
3b3f24ad
AJ
207#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
208_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
209#endif
210_syscall2(int, sys_getpriority, int, which, int, who);
d35b261c 211#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
3b3f24ad
AJ
212_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
213 loff_t *, res, uint, wh);
214#endif
215_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
216_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
217#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
218_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
219#endif
220#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
221_syscall2(int,sys_tkill,int,tid,int,sig)
222#endif
223#ifdef __NR_exit_group
224_syscall1(int,exit_group,int,error_code)
225#endif
226#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
227_syscall1(int,set_tid_address,int *,tidptr)
228#endif
2f7bb878 229#if defined(CONFIG_USE_NPTL)
3b3f24ad
AJ
230#if defined(TARGET_NR_futex) && defined(__NR_futex)
231_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
232 const struct timespec *,timeout,int *,uaddr2,int,val3)
233#endif
234#endif
235
236static bitmask_transtbl fcntl_flags_tbl[] = {
237 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
238 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
239 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
240 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
241 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
242 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
243 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
244 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
245 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
246 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
247 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
248 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
249 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
250#if defined(O_DIRECT)
251 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
252#endif
253 { 0, 0, 0, 0 }
254};
255
256#define COPY_UTSNAME_FIELD(dest, src) \
257 do { \
258 /* __NEW_UTS_LEN doesn't include terminating null */ \
259 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
260 (dest)[__NEW_UTS_LEN] = '\0'; \
261 } while (0)
262
263static int sys_uname(struct new_utsname *buf)
264{
265 struct utsname uts_buf;
266
267 if (uname(&uts_buf) < 0)
268 return (-1);
269
270 /*
271 * Just in case these have some differences, we
272 * translate utsname to new_utsname (which is the
273 * struct linux kernel uses).
274 */
275
276 bzero(buf, sizeof (*buf));
277 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
278 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
279 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
280 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
281 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
282#ifdef _GNU_SOURCE
283 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
284#endif
285 return (0);
286
287#undef COPY_UTSNAME_FIELD
288}
289
290static int sys_getcwd1(char *buf, size_t size)
291{
292 if (getcwd(buf, size) == NULL) {
293 /* getcwd() sets errno */
294 return (-1);
295 }
aaf4ad39 296 return strlen(buf)+1;
3b3f24ad
AJ
297}
298
299#ifdef CONFIG_ATFILE
300/*
301 * Host system seems to have atfile syscall stubs available. We
302 * now enable them one by one as specified by target syscall_nr.h.
303 */
304
305#ifdef TARGET_NR_faccessat
465c9f06 306static int sys_faccessat(int dirfd, const char *pathname, int mode)
3b3f24ad 307{
465c9f06 308 return (faccessat(dirfd, pathname, mode, 0));
3b3f24ad
AJ
309}
310#endif
311#ifdef TARGET_NR_fchmodat
465c9f06 312static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
3b3f24ad 313{
465c9f06 314 return (fchmodat(dirfd, pathname, mode, 0));
3b3f24ad
AJ
315}
316#endif
fda33744 317#if defined(TARGET_NR_fchownat) && defined(USE_UID16)
3b3f24ad
AJ
318static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
319 gid_t group, int flags)
320{
321 return (fchownat(dirfd, pathname, owner, group, flags));
322}
323#endif
324#ifdef __NR_fstatat64
325static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
326 int flags)
327{
328 return (fstatat(dirfd, pathname, buf, flags));
329}
330#endif
331#ifdef __NR_newfstatat
332static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
333 int flags)
334{
335 return (fstatat(dirfd, pathname, buf, flags));
336}
337#endif
338#ifdef TARGET_NR_futimesat
339static int sys_futimesat(int dirfd, const char *pathname,
340 const struct timeval times[2])
341{
342 return (futimesat(dirfd, pathname, times));
343}
344#endif
345#ifdef TARGET_NR_linkat
346static int sys_linkat(int olddirfd, const char *oldpath,
347 int newdirfd, const char *newpath, int flags)
348{
349 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
350}
351#endif
352#ifdef TARGET_NR_mkdirat
353static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
354{
355 return (mkdirat(dirfd, pathname, mode));
356}
357#endif
358#ifdef TARGET_NR_mknodat
359static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
360 dev_t dev)
361{
362 return (mknodat(dirfd, pathname, mode, dev));
363}
364#endif
365#ifdef TARGET_NR_openat
366static int sys_openat(int dirfd, const char *pathname, int flags, ...)
367{
368 /*
369 * open(2) has extra parameter 'mode' when called with
370 * flag O_CREAT.
371 */
372 if ((flags & O_CREAT) != 0) {
373 va_list ap;
374 mode_t mode;
375
376 /*
377 * Get the 'mode' parameter and translate it to
378 * host bits.
379 */
380 va_start(ap, flags);
381 mode = va_arg(ap, mode_t);
382 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
383 va_end(ap);
384
385 return (openat(dirfd, pathname, flags, mode));
386 }
387 return (openat(dirfd, pathname, flags));
388}
389#endif
390#ifdef TARGET_NR_readlinkat
391static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
392{
393 return (readlinkat(dirfd, pathname, buf, bufsiz));
394}
395#endif
396#ifdef TARGET_NR_renameat
397static int sys_renameat(int olddirfd, const char *oldpath,
398 int newdirfd, const char *newpath)
399{
400 return (renameat(olddirfd, oldpath, newdirfd, newpath));
401}
402#endif
403#ifdef TARGET_NR_symlinkat
404static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
405{
406 return (symlinkat(oldpath, newdirfd, newpath));
407}
408#endif
409#ifdef TARGET_NR_unlinkat
410static int sys_unlinkat(int dirfd, const char *pathname, int flags)
411{
412 return (unlinkat(dirfd, pathname, flags));
413}
414#endif
3b3f24ad
AJ
415#else /* !CONFIG_ATFILE */
416
417/*
418 * Try direct syscalls instead
419 */
92a34c10 420#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
465c9f06 421_syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
92a34c10 422#endif
814d7977 423#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
465c9f06 424_syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
814d7977 425#endif
4583f589 426#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
ccfa72b7
TS
427_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
428 uid_t,owner,gid_t,group,int,flags)
429#endif
9d33b76b
AJ
430#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
431 defined(__NR_fstatat64)
6a24a778
AZ
432_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
433 struct stat *,buf,int,flags)
434#endif
ac8a6556
AZ
435#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
436_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
437 const struct timeval *,times)
438#endif
3b3f24ad
AJ
439#if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
440 defined(__NR_newfstatat)
441_syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
442 struct stat *,buf,int,flags)
8fcd3692 443#endif
64f0ce4c
TS
444#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
445_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
3b3f24ad 446 int,newdirfd,const char *,newpath,int,flags)
64f0ce4c 447#endif
4472ad0d
TS
448#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
449_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
450#endif
75ac37a0
TS
451#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
452_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
453 mode_t,mode,dev_t,dev)
454#endif
82424832
TS
455#if defined(TARGET_NR_openat) && defined(__NR_openat)
456_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
457#endif
5e0ccb18
TS
458#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
459_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
460 char *,buf,size_t,bufsize)
461#endif
722183f6
TS
462#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
463_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
464 int,newdirfd,const char *,newpath)
465#endif
b51eaa82 466#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
f0b6243d
TS
467_syscall3(int,sys_symlinkat,const char *,oldpath,
468 int,newdirfd,const char *,newpath)
469#endif
8170f56b
TS
470#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
471_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
472#endif
ebc996f3
RV
473
474#endif /* CONFIG_ATFILE */
475
476#ifdef CONFIG_UTIMENSAT
477static int sys_utimensat(int dirfd, const char *pathname,
478 const struct timespec times[2], int flags)
479{
480 if (pathname == NULL)
481 return futimens(dirfd, times);
482 else
483 return utimensat(dirfd, pathname, times, flags);
484}
485#else
9007f0ef
TS
486#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
487_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
488 const struct timespec *,tsp,int,flags)
489#endif
ebc996f3 490#endif /* CONFIG_UTIMENSAT */
3b3f24ad
AJ
491
492#ifdef CONFIG_INOTIFY
8690e420 493#include <sys/inotify.h>
3b3f24ad 494
39b59763 495#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
3b3f24ad
AJ
496static int sys_inotify_init(void)
497{
498 return (inotify_init());
499}
39b59763
AJ
500#endif
501#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
3b3f24ad
AJ
502static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
503{
504 return (inotify_add_watch(fd, pathname, mask));
505}
39b59763
AJ
506#endif
507#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
3b3f24ad
AJ
508static int sys_inotify_rm_watch(int fd, int32_t wd)
509{
8690e420 510 return (inotify_rm_watch(fd, wd));
3b3f24ad 511}
bd0c5661 512#endif
c05c7a73
RV
513#ifdef CONFIG_INOTIFY1
514#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
515static int sys_inotify_init1(int flags)
516{
517 return (inotify_init1(flags));
518}
519#endif
520#endif
3b3f24ad
AJ
521#else
522/* Userspace can usually survive runtime without inotify */
523#undef TARGET_NR_inotify_init
c05c7a73 524#undef TARGET_NR_inotify_init1
3b3f24ad
AJ
525#undef TARGET_NR_inotify_add_watch
526#undef TARGET_NR_inotify_rm_watch
527#endif /* CONFIG_INOTIFY */
528
66fb9763
FB
529
530extern int personality(int);
9de5e440
FB
531extern int flock(int, int);
532extern int setfsuid(int);
533extern int setfsgid(int);
19b84f3c 534extern int setgroups(int, gid_t *);
31e31b8a 535
b92c47c1
TS
536#define ERRNO_TABLE_SIZE 1200
537
538/* target_to_host_errno_table[] is initialized from
539 * host_to_target_errno_table[] in syscall_init(). */
540static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
541};
542
637947f1 543/*
fe8f096b 544 * This list is the union of errno values overridden in asm-<arch>/errno.h
637947f1
TS
545 * minus the errnos that are not actually generic to all archs.
546 */
b92c47c1 547static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
637947f1
TS
548 [EIDRM] = TARGET_EIDRM,
549 [ECHRNG] = TARGET_ECHRNG,
550 [EL2NSYNC] = TARGET_EL2NSYNC,
551 [EL3HLT] = TARGET_EL3HLT,
552 [EL3RST] = TARGET_EL3RST,
553 [ELNRNG] = TARGET_ELNRNG,
554 [EUNATCH] = TARGET_EUNATCH,
555 [ENOCSI] = TARGET_ENOCSI,
556 [EL2HLT] = TARGET_EL2HLT,
557 [EDEADLK] = TARGET_EDEADLK,
558 [ENOLCK] = TARGET_ENOLCK,
559 [EBADE] = TARGET_EBADE,
560 [EBADR] = TARGET_EBADR,
561 [EXFULL] = TARGET_EXFULL,
562 [ENOANO] = TARGET_ENOANO,
563 [EBADRQC] = TARGET_EBADRQC,
564 [EBADSLT] = TARGET_EBADSLT,
565 [EBFONT] = TARGET_EBFONT,
566 [ENOSTR] = TARGET_ENOSTR,
567 [ENODATA] = TARGET_ENODATA,
568 [ETIME] = TARGET_ETIME,
569 [ENOSR] = TARGET_ENOSR,
570 [ENONET] = TARGET_ENONET,
571 [ENOPKG] = TARGET_ENOPKG,
572 [EREMOTE] = TARGET_EREMOTE,
573 [ENOLINK] = TARGET_ENOLINK,
574 [EADV] = TARGET_EADV,
575 [ESRMNT] = TARGET_ESRMNT,
576 [ECOMM] = TARGET_ECOMM,
577 [EPROTO] = TARGET_EPROTO,
578 [EDOTDOT] = TARGET_EDOTDOT,
579 [EMULTIHOP] = TARGET_EMULTIHOP,
580 [EBADMSG] = TARGET_EBADMSG,
581 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
582 [EOVERFLOW] = TARGET_EOVERFLOW,
583 [ENOTUNIQ] = TARGET_ENOTUNIQ,
584 [EBADFD] = TARGET_EBADFD,
585 [EREMCHG] = TARGET_EREMCHG,
586 [ELIBACC] = TARGET_ELIBACC,
587 [ELIBBAD] = TARGET_ELIBBAD,
588 [ELIBSCN] = TARGET_ELIBSCN,
589 [ELIBMAX] = TARGET_ELIBMAX,
590 [ELIBEXEC] = TARGET_ELIBEXEC,
591 [EILSEQ] = TARGET_EILSEQ,
592 [ENOSYS] = TARGET_ENOSYS,
593 [ELOOP] = TARGET_ELOOP,
594 [ERESTART] = TARGET_ERESTART,
595 [ESTRPIPE] = TARGET_ESTRPIPE,
596 [ENOTEMPTY] = TARGET_ENOTEMPTY,
597 [EUSERS] = TARGET_EUSERS,
598 [ENOTSOCK] = TARGET_ENOTSOCK,
599 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
600 [EMSGSIZE] = TARGET_EMSGSIZE,
601 [EPROTOTYPE] = TARGET_EPROTOTYPE,
602 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
603 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
604 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
605 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
606 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
607 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
608 [EADDRINUSE] = TARGET_EADDRINUSE,
609 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
610 [ENETDOWN] = TARGET_ENETDOWN,
611 [ENETUNREACH] = TARGET_ENETUNREACH,
612 [ENETRESET] = TARGET_ENETRESET,
613 [ECONNABORTED] = TARGET_ECONNABORTED,
614 [ECONNRESET] = TARGET_ECONNRESET,
615 [ENOBUFS] = TARGET_ENOBUFS,
616 [EISCONN] = TARGET_EISCONN,
617 [ENOTCONN] = TARGET_ENOTCONN,
618 [EUCLEAN] = TARGET_EUCLEAN,
619 [ENOTNAM] = TARGET_ENOTNAM,
620 [ENAVAIL] = TARGET_ENAVAIL,
621 [EISNAM] = TARGET_EISNAM,
622 [EREMOTEIO] = TARGET_EREMOTEIO,
623 [ESHUTDOWN] = TARGET_ESHUTDOWN,
624 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
625 [ETIMEDOUT] = TARGET_ETIMEDOUT,
626 [ECONNREFUSED] = TARGET_ECONNREFUSED,
627 [EHOSTDOWN] = TARGET_EHOSTDOWN,
628 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
629 [EALREADY] = TARGET_EALREADY,
630 [EINPROGRESS] = TARGET_EINPROGRESS,
631 [ESTALE] = TARGET_ESTALE,
632 [ECANCELED] = TARGET_ECANCELED,
633 [ENOMEDIUM] = TARGET_ENOMEDIUM,
634 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
b7fe5db7 635#ifdef ENOKEY
637947f1 636 [ENOKEY] = TARGET_ENOKEY,
b7fe5db7
TS
637#endif
638#ifdef EKEYEXPIRED
637947f1 639 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
b7fe5db7
TS
640#endif
641#ifdef EKEYREVOKED
637947f1 642 [EKEYREVOKED] = TARGET_EKEYREVOKED,
b7fe5db7
TS
643#endif
644#ifdef EKEYREJECTED
637947f1 645 [EKEYREJECTED] = TARGET_EKEYREJECTED,
b7fe5db7
TS
646#endif
647#ifdef EOWNERDEAD
637947f1 648 [EOWNERDEAD] = TARGET_EOWNERDEAD,
b7fe5db7
TS
649#endif
650#ifdef ENOTRECOVERABLE
637947f1 651 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
b7fe5db7 652#endif
b92c47c1 653};
637947f1
TS
654
655static inline int host_to_target_errno(int err)
656{
657 if(host_to_target_errno_table[err])
658 return host_to_target_errno_table[err];
659 return err;
660}
661
b92c47c1
TS
662static inline int target_to_host_errno(int err)
663{
664 if (target_to_host_errno_table[err])
665 return target_to_host_errno_table[err];
666 return err;
667}
668
992f48a0 669static inline abi_long get_errno(abi_long ret)
31e31b8a
FB
670{
671 if (ret == -1)
637947f1 672 return -host_to_target_errno(errno);
31e31b8a
FB
673 else
674 return ret;
675}
676
992f48a0 677static inline int is_error(abi_long ret)
31e31b8a 678{
992f48a0 679 return (abi_ulong)ret >= (abi_ulong)(-4096);
31e31b8a
FB
680}
681
b92c47c1
TS
682char *target_strerror(int err)
683{
684 return strerror(target_to_host_errno(err));
685}
686
992f48a0
BS
687static abi_ulong target_brk;
688static abi_ulong target_original_brk;
31e31b8a 689
992f48a0 690void target_set_brk(abi_ulong new_brk)
31e31b8a 691{
4c1de73d 692 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
31e31b8a
FB
693}
694
0da46a6e 695/* do_brk() must return target values and target errnos. */
992f48a0 696abi_long do_brk(abi_ulong new_brk)
31e31b8a 697{
992f48a0
BS
698 abi_ulong brk_page;
699 abi_long mapped_addr;
31e31b8a
FB
700 int new_alloc_size;
701
702 if (!new_brk)
53a5960a 703 return target_brk;
31e31b8a 704 if (new_brk < target_original_brk)
7ab240ad 705 return target_brk;
3b46e624 706
53a5960a 707 brk_page = HOST_PAGE_ALIGN(target_brk);
31e31b8a
FB
708
709 /* If the new brk is less than this, set it and we're done... */
710 if (new_brk < brk_page) {
711 target_brk = new_brk;
53a5960a 712 return target_brk;
31e31b8a
FB
713 }
714
715 /* We need to allocate more memory after the brk... */
54936004 716 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
5fafdf24 717 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
54936004
FB
718 PROT_READ|PROT_WRITE,
719 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
7ab240ad 720
7dd46c02
RH
721#if defined(TARGET_ALPHA)
722 /* We (partially) emulate OSF/1 on Alpha, which requires we
723 return a proper errno, not an unchanged brk value. */
724 if (is_error(mapped_addr)) {
725 return -TARGET_ENOMEM;
726 }
727#endif
728
729 if (!is_error(mapped_addr)) {
31e31b8a 730 target_brk = new_brk;
7dd46c02 731 }
7ab240ad 732 return target_brk;
31e31b8a
FB
733}
734
26edcf41
TS
735static inline abi_long copy_from_user_fdset(fd_set *fds,
736 abi_ulong target_fds_addr,
737 int n)
31e31b8a 738{
26edcf41
TS
739 int i, nw, j, k;
740 abi_ulong b, *target_fds;
741
742 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
743 if (!(target_fds = lock_user(VERIFY_READ,
744 target_fds_addr,
745 sizeof(abi_ulong) * nw,
746 1)))
747 return -TARGET_EFAULT;
748
749 FD_ZERO(fds);
750 k = 0;
751 for (i = 0; i < nw; i++) {
752 /* grab the abi_ulong */
753 __get_user(b, &target_fds[i]);
754 for (j = 0; j < TARGET_ABI_BITS; j++) {
755 /* check the bit inside the abi_ulong */
756 if ((b >> j) & 1)
757 FD_SET(k, fds);
758 k++;
31e31b8a 759 }
31e31b8a 760 }
26edcf41
TS
761
762 unlock_user(target_fds, target_fds_addr, 0);
763
764 return 0;
31e31b8a
FB
765}
766
26edcf41
TS
767static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
768 const fd_set *fds,
769 int n)
31e31b8a 770{
31e31b8a 771 int i, nw, j, k;
992f48a0 772 abi_long v;
26edcf41 773 abi_ulong *target_fds;
31e31b8a 774
26edcf41
TS
775 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
776 if (!(target_fds = lock_user(VERIFY_WRITE,
777 target_fds_addr,
778 sizeof(abi_ulong) * nw,
779 0)))
780 return -TARGET_EFAULT;
781
782 k = 0;
783 for (i = 0; i < nw; i++) {
784 v = 0;
785 for (j = 0; j < TARGET_ABI_BITS; j++) {
786 v |= ((FD_ISSET(k, fds) != 0) << j);
787 k++;
31e31b8a 788 }
26edcf41 789 __put_user(v, &target_fds[i]);
31e31b8a 790 }
26edcf41
TS
791
792 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
793
794 return 0;
31e31b8a
FB
795}
796
c596ed17
FB
797#if defined(__alpha__)
798#define HOST_HZ 1024
799#else
800#define HOST_HZ 100
801#endif
802
992f48a0 803static inline abi_long host_to_target_clock_t(long ticks)
c596ed17
FB
804{
805#if HOST_HZ == TARGET_HZ
806 return ticks;
807#else
808 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
809#endif
810}
811
579a97f7
FB
812static inline abi_long host_to_target_rusage(abi_ulong target_addr,
813 const struct rusage *rusage)
b409186b 814{
53a5960a
PB
815 struct target_rusage *target_rusage;
816
579a97f7
FB
817 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
818 return -TARGET_EFAULT;
b409186b
FB
819 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
820 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
821 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
822 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
823 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
824 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
825 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
826 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
827 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
828 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
829 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
830 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
831 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
832 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
833 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
834 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
835 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
836 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
53a5960a 837 unlock_user_struct(target_rusage, target_addr, 1);
579a97f7
FB
838
839 return 0;
b409186b
FB
840}
841
81bbe906
TY
842static inline rlim_t target_to_host_rlim(target_ulong target_rlim)
843{
844 if (target_rlim == TARGET_RLIM_INFINITY)
845 return RLIM_INFINITY;
846 else
847 return tswapl(target_rlim);
848}
849
850static inline target_ulong host_to_target_rlim(rlim_t rlim)
851{
852 if (rlim == RLIM_INFINITY || rlim != (target_long)rlim)
853 return TARGET_RLIM_INFINITY;
854 else
855 return tswapl(rlim);
856}
857
788f5ec4
TS
858static inline abi_long copy_from_user_timeval(struct timeval *tv,
859 abi_ulong target_tv_addr)
31e31b8a 860{
53a5960a
PB
861 struct target_timeval *target_tv;
862
788f5ec4 863 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
579a97f7 864 return -TARGET_EFAULT;
788f5ec4
TS
865
866 __get_user(tv->tv_sec, &target_tv->tv_sec);
867 __get_user(tv->tv_usec, &target_tv->tv_usec);
868
869 unlock_user_struct(target_tv, target_tv_addr, 0);
579a97f7
FB
870
871 return 0;
31e31b8a
FB
872}
873
788f5ec4
TS
874static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
875 const struct timeval *tv)
31e31b8a 876{
53a5960a
PB
877 struct target_timeval *target_tv;
878
788f5ec4 879 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
579a97f7 880 return -TARGET_EFAULT;
788f5ec4
TS
881
882 __put_user(tv->tv_sec, &target_tv->tv_sec);
883 __put_user(tv->tv_usec, &target_tv->tv_usec);
884
885 unlock_user_struct(target_tv, target_tv_addr, 1);
579a97f7
FB
886
887 return 0;
31e31b8a
FB
888}
889
8ec9cf89
NF
890#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
891#include <mqueue.h>
892
24e1003a
AJ
893static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
894 abi_ulong target_mq_attr_addr)
895{
896 struct target_mq_attr *target_mq_attr;
897
898 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
899 target_mq_attr_addr, 1))
900 return -TARGET_EFAULT;
901
902 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
903 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
904 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
905 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
906
907 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
908
909 return 0;
910}
911
912static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
913 const struct mq_attr *attr)
914{
915 struct target_mq_attr *target_mq_attr;
916
917 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
918 target_mq_attr_addr, 0))
919 return -TARGET_EFAULT;
920
921 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
922 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
923 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
924 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
925
926 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
927
928 return 0;
929}
8ec9cf89 930#endif
31e31b8a 931
0da46a6e 932/* do_select() must return target values and target errnos. */
992f48a0 933static abi_long do_select(int n,
26edcf41
TS
934 abi_ulong rfd_addr, abi_ulong wfd_addr,
935 abi_ulong efd_addr, abi_ulong target_tv_addr)
31e31b8a
FB
936{
937 fd_set rfds, wfds, efds;
938 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
939 struct timeval tv, *tv_ptr;
992f48a0 940 abi_long ret;
31e31b8a 941
26edcf41
TS
942 if (rfd_addr) {
943 if (copy_from_user_fdset(&rfds, rfd_addr, n))
944 return -TARGET_EFAULT;
945 rfds_ptr = &rfds;
53a5960a 946 } else {
53a5960a
PB
947 rfds_ptr = NULL;
948 }
26edcf41
TS
949 if (wfd_addr) {
950 if (copy_from_user_fdset(&wfds, wfd_addr, n))
951 return -TARGET_EFAULT;
952 wfds_ptr = &wfds;
53a5960a 953 } else {
53a5960a
PB
954 wfds_ptr = NULL;
955 }
26edcf41
TS
956 if (efd_addr) {
957 if (copy_from_user_fdset(&efds, efd_addr, n))
958 return -TARGET_EFAULT;
959 efds_ptr = &efds;
53a5960a 960 } else {
53a5960a
PB
961 efds_ptr = NULL;
962 }
3b46e624 963
26edcf41 964 if (target_tv_addr) {
788f5ec4
TS
965 if (copy_from_user_timeval(&tv, target_tv_addr))
966 return -TARGET_EFAULT;
31e31b8a
FB
967 tv_ptr = &tv;
968 } else {
969 tv_ptr = NULL;
970 }
26edcf41 971
31e31b8a 972 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
53a5960a 973
26edcf41
TS
974 if (!is_error(ret)) {
975 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
976 return -TARGET_EFAULT;
977 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
978 return -TARGET_EFAULT;
979 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
980 return -TARGET_EFAULT;
31e31b8a 981
788f5ec4
TS
982 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
983 return -TARGET_EFAULT;
31e31b8a 984 }
579a97f7 985
31e31b8a
FB
986 return ret;
987}
988
099d6b0f
RV
989static abi_long do_pipe2(int host_pipe[], int flags)
990{
991#ifdef CONFIG_PIPE2
992 return pipe2(host_pipe, flags);
993#else
994 return -ENOSYS;
995#endif
996}
997
fb41a66e
RH
998static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
999 int flags, int is_pipe2)
099d6b0f
RV
1000{
1001 int host_pipe[2];
1002 abi_long ret;
1003 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1004
1005 if (is_error(ret))
1006 return get_errno(ret);
fb41a66e
RH
1007
1008 /* Several targets have special calling conventions for the original
1009 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1010 if (!is_pipe2) {
1011#if defined(TARGET_ALPHA)
1012 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1013 return host_pipe[0];
1014#elif defined(TARGET_MIPS)
1015 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1016 return host_pipe[0];
1017#elif defined(TARGET_SH4)
597c0212 1018 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
fb41a66e 1019 return host_pipe[0];
597c0212 1020#endif
fb41a66e
RH
1021 }
1022
099d6b0f
RV
1023 if (put_user_s32(host_pipe[0], pipedes)
1024 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1025 return -TARGET_EFAULT;
099d6b0f
RV
1026 return get_errno(ret);
1027}
1028
b975b83b
LL
1029static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1030 abi_ulong target_addr,
1031 socklen_t len)
1032{
1033 struct target_ip_mreqn *target_smreqn;
1034
1035 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1036 if (!target_smreqn)
1037 return -TARGET_EFAULT;
1038 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1039 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1040 if (len == sizeof(struct target_ip_mreqn))
1041 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
1042 unlock_user(target_smreqn, target_addr, 0);
1043
1044 return 0;
1045}
1046
579a97f7
FB
1047static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1048 abi_ulong target_addr,
1049 socklen_t len)
7854b056 1050{
607175e0
AJ
1051 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1052 sa_family_t sa_family;
53a5960a
PB
1053 struct target_sockaddr *target_saddr;
1054
579a97f7
FB
1055 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1056 if (!target_saddr)
1057 return -TARGET_EFAULT;
607175e0
AJ
1058
1059 sa_family = tswap16(target_saddr->sa_family);
1060
1061 /* Oops. The caller might send a incomplete sun_path; sun_path
1062 * must be terminated by \0 (see the manual page), but
1063 * unfortunately it is quite common to specify sockaddr_un
1064 * length as "strlen(x->sun_path)" while it should be
1065 * "strlen(...) + 1". We'll fix that here if needed.
1066 * Linux kernel has a similar feature.
1067 */
1068
1069 if (sa_family == AF_UNIX) {
1070 if (len < unix_maxlen && len > 0) {
1071 char *cp = (char*)target_saddr;
1072
1073 if ( cp[len-1] && !cp[len] )
1074 len++;
1075 }
1076 if (len > unix_maxlen)
1077 len = unix_maxlen;
1078 }
1079
53a5960a 1080 memcpy(addr, target_saddr, len);
607175e0 1081 addr->sa_family = sa_family;
53a5960a 1082 unlock_user(target_saddr, target_addr, 0);
579a97f7
FB
1083
1084 return 0;
7854b056
FB
1085}
1086
579a97f7
FB
1087static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1088 struct sockaddr *addr,
1089 socklen_t len)
7854b056 1090{
53a5960a
PB
1091 struct target_sockaddr *target_saddr;
1092
579a97f7
FB
1093 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1094 if (!target_saddr)
1095 return -TARGET_EFAULT;
53a5960a
PB
1096 memcpy(target_saddr, addr, len);
1097 target_saddr->sa_family = tswap16(addr->sa_family);
1098 unlock_user(target_saddr, target_addr, len);
579a97f7
FB
1099
1100 return 0;
7854b056
FB
1101}
1102
53a5960a 1103/* ??? Should this also swap msgh->name? */
5a4a898d
FB
1104static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1105 struct target_msghdr *target_msgh)
7854b056
FB
1106{
1107 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1108 abi_long msg_controllen;
1109 abi_ulong target_cmsg_addr;
1110 struct target_cmsghdr *target_cmsg;
7854b056 1111 socklen_t space = 0;
5a4a898d
FB
1112
1113 msg_controllen = tswapl(target_msgh->msg_controllen);
1114 if (msg_controllen < sizeof (struct target_cmsghdr))
1115 goto the_end;
1116 target_cmsg_addr = tswapl(target_msgh->msg_control);
1117 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1118 if (!target_cmsg)
1119 return -TARGET_EFAULT;
7854b056
FB
1120
1121 while (cmsg && target_cmsg) {
1122 void *data = CMSG_DATA(cmsg);
1123 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1124
5fafdf24 1125 int len = tswapl(target_cmsg->cmsg_len)
7854b056
FB
1126 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1127
1128 space += CMSG_SPACE(len);
1129 if (space > msgh->msg_controllen) {
1130 space -= CMSG_SPACE(len);
31febb71 1131 gemu_log("Host cmsg overflow\n");
7854b056
FB
1132 break;
1133 }
1134
1135 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1136 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1137 cmsg->cmsg_len = CMSG_LEN(len);
1138
3532fa74 1139 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
7854b056
FB
1140 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1141 memcpy(data, target_data, len);
1142 } else {
1143 int *fd = (int *)data;
1144 int *target_fd = (int *)target_data;
1145 int i, numfds = len / sizeof(int);
1146
1147 for (i = 0; i < numfds; i++)
1148 fd[i] = tswap32(target_fd[i]);
1149 }
1150
1151 cmsg = CMSG_NXTHDR(msgh, cmsg);
1152 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1153 }
5a4a898d
FB
1154 unlock_user(target_cmsg, target_cmsg_addr, 0);
1155 the_end:
7854b056 1156 msgh->msg_controllen = space;
5a4a898d 1157 return 0;
7854b056
FB
1158}
1159
53a5960a 1160/* ??? Should this also swap msgh->name? */
5a4a898d
FB
1161static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1162 struct msghdr *msgh)
7854b056
FB
1163{
1164 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1165 abi_long msg_controllen;
1166 abi_ulong target_cmsg_addr;
1167 struct target_cmsghdr *target_cmsg;
7854b056
FB
1168 socklen_t space = 0;
1169
5a4a898d
FB
1170 msg_controllen = tswapl(target_msgh->msg_controllen);
1171 if (msg_controllen < sizeof (struct target_cmsghdr))
1172 goto the_end;
1173 target_cmsg_addr = tswapl(target_msgh->msg_control);
1174 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1175 if (!target_cmsg)
1176 return -TARGET_EFAULT;
1177
7854b056
FB
1178 while (cmsg && target_cmsg) {
1179 void *data = CMSG_DATA(cmsg);
1180 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1181
1182 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1183
1184 space += TARGET_CMSG_SPACE(len);
5a4a898d 1185 if (space > msg_controllen) {
7854b056 1186 space -= TARGET_CMSG_SPACE(len);
31febb71 1187 gemu_log("Target cmsg overflow\n");
7854b056
FB
1188 break;
1189 }
1190
1191 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1192 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1193 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1194
3532fa74 1195 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
7854b056
FB
1196 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1197 memcpy(target_data, data, len);
1198 } else {
1199 int *fd = (int *)data;
1200 int *target_fd = (int *)target_data;
1201 int i, numfds = len / sizeof(int);
1202
1203 for (i = 0; i < numfds; i++)
1204 target_fd[i] = tswap32(fd[i]);
1205 }
1206
1207 cmsg = CMSG_NXTHDR(msgh, cmsg);
1208 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1209 }
5a4a898d
FB
1210 unlock_user(target_cmsg, target_cmsg_addr, space);
1211 the_end:
1212 target_msgh->msg_controllen = tswapl(space);
1213 return 0;
7854b056
FB
1214}
1215
0da46a6e 1216/* do_setsockopt() Must return target values and target errnos. */
992f48a0 1217static abi_long do_setsockopt(int sockfd, int level, int optname,
2f619698 1218 abi_ulong optval_addr, socklen_t optlen)
7854b056 1219{
992f48a0 1220 abi_long ret;
32407103 1221 int val;
b975b83b 1222 struct ip_mreqn *ip_mreq;
6e3cb58f 1223 struct ip_mreq_source *ip_mreq_source;
3b46e624 1224
8853f86e
FB
1225 switch(level) {
1226 case SOL_TCP:
7854b056 1227 /* TCP options all take an 'int' value. */
7854b056 1228 if (optlen < sizeof(uint32_t))
0da46a6e 1229 return -TARGET_EINVAL;
3b46e624 1230
2f619698
FB
1231 if (get_user_u32(val, optval_addr))
1232 return -TARGET_EFAULT;
8853f86e
FB
1233 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1234 break;
1235 case SOL_IP:
1236 switch(optname) {
2efbe911
FB
1237 case IP_TOS:
1238 case IP_TTL:
8853f86e 1239 case IP_HDRINCL:
2efbe911
FB
1240 case IP_ROUTER_ALERT:
1241 case IP_RECVOPTS:
1242 case IP_RETOPTS:
1243 case IP_PKTINFO:
1244 case IP_MTU_DISCOVER:
1245 case IP_RECVERR:
1246 case IP_RECVTOS:
1247#ifdef IP_FREEBIND
1248 case IP_FREEBIND:
1249#endif
1250 case IP_MULTICAST_TTL:
1251 case IP_MULTICAST_LOOP:
8853f86e
FB
1252 val = 0;
1253 if (optlen >= sizeof(uint32_t)) {
2f619698
FB
1254 if (get_user_u32(val, optval_addr))
1255 return -TARGET_EFAULT;
8853f86e 1256 } else if (optlen >= 1) {
2f619698
FB
1257 if (get_user_u8(val, optval_addr))
1258 return -TARGET_EFAULT;
8853f86e
FB
1259 }
1260 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1261 break;
b975b83b
LL
1262 case IP_ADD_MEMBERSHIP:
1263 case IP_DROP_MEMBERSHIP:
1264 if (optlen < sizeof (struct target_ip_mreq) ||
1265 optlen > sizeof (struct target_ip_mreqn))
1266 return -TARGET_EINVAL;
1267
1268 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1269 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1270 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1271 break;
1272
6e3cb58f
LL
1273 case IP_BLOCK_SOURCE:
1274 case IP_UNBLOCK_SOURCE:
1275 case IP_ADD_SOURCE_MEMBERSHIP:
1276 case IP_DROP_SOURCE_MEMBERSHIP:
1277 if (optlen != sizeof (struct target_ip_mreq_source))
1278 return -TARGET_EINVAL;
1279
1280 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1281 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1282 unlock_user (ip_mreq_source, optval_addr, 0);
1283 break;
1284
8853f86e
FB
1285 default:
1286 goto unimplemented;
1287 }
1288 break;
3532fa74 1289 case TARGET_SOL_SOCKET:
8853f86e
FB
1290 switch (optname) {
1291 /* Options with 'int' argument. */
3532fa74
FB
1292 case TARGET_SO_DEBUG:
1293 optname = SO_DEBUG;
1294 break;
1295 case TARGET_SO_REUSEADDR:
1296 optname = SO_REUSEADDR;
1297 break;
1298 case TARGET_SO_TYPE:
1299 optname = SO_TYPE;
1300 break;
1301 case TARGET_SO_ERROR:
1302 optname = SO_ERROR;
1303 break;
1304 case TARGET_SO_DONTROUTE:
1305 optname = SO_DONTROUTE;
1306 break;
1307 case TARGET_SO_BROADCAST:
1308 optname = SO_BROADCAST;
1309 break;
1310 case TARGET_SO_SNDBUF:
1311 optname = SO_SNDBUF;
1312 break;
1313 case TARGET_SO_RCVBUF:
1314 optname = SO_RCVBUF;
1315 break;
1316 case TARGET_SO_KEEPALIVE:
1317 optname = SO_KEEPALIVE;
1318 break;
1319 case TARGET_SO_OOBINLINE:
1320 optname = SO_OOBINLINE;
1321 break;
1322 case TARGET_SO_NO_CHECK:
1323 optname = SO_NO_CHECK;
1324 break;
1325 case TARGET_SO_PRIORITY:
1326 optname = SO_PRIORITY;
1327 break;
5e83e8e3 1328#ifdef SO_BSDCOMPAT
3532fa74
FB
1329 case TARGET_SO_BSDCOMPAT:
1330 optname = SO_BSDCOMPAT;
1331 break;
5e83e8e3 1332#endif
3532fa74
FB
1333 case TARGET_SO_PASSCRED:
1334 optname = SO_PASSCRED;
1335 break;
1336 case TARGET_SO_TIMESTAMP:
1337 optname = SO_TIMESTAMP;
1338 break;
1339 case TARGET_SO_RCVLOWAT:
1340 optname = SO_RCVLOWAT;
1341 break;
1342 case TARGET_SO_RCVTIMEO:
1343 optname = SO_RCVTIMEO;
1344 break;
1345 case TARGET_SO_SNDTIMEO:
1346 optname = SO_SNDTIMEO;
1347 break;
8853f86e
FB
1348 break;
1349 default:
1350 goto unimplemented;
1351 }
3532fa74 1352 if (optlen < sizeof(uint32_t))
2f619698 1353 return -TARGET_EINVAL;
3532fa74 1354
2f619698
FB
1355 if (get_user_u32(val, optval_addr))
1356 return -TARGET_EFAULT;
3532fa74 1357 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
8853f86e 1358 break;
7854b056 1359 default:
8853f86e
FB
1360 unimplemented:
1361 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
6fa13c17 1362 ret = -TARGET_ENOPROTOOPT;
7854b056 1363 }
8853f86e 1364 return ret;
7854b056
FB
1365}
1366
0da46a6e 1367/* do_getsockopt() Must return target values and target errnos. */
992f48a0 1368static abi_long do_getsockopt(int sockfd, int level, int optname,
2f619698 1369 abi_ulong optval_addr, abi_ulong optlen)
7854b056 1370{
992f48a0 1371 abi_long ret;
b55266b5
BS
1372 int len, val;
1373 socklen_t lv;
8853f86e
FB
1374
1375 switch(level) {
3532fa74 1376 case TARGET_SOL_SOCKET:
f3b974cd
JL
1377 level = SOL_SOCKET;
1378 switch (optname) {
1379 /* These don't just return a single integer */
1380 case TARGET_SO_LINGER:
1381 case TARGET_SO_RCVTIMEO:
1382 case TARGET_SO_SNDTIMEO:
1383 case TARGET_SO_PEERCRED:
1384 case TARGET_SO_PEERNAME:
1385 goto unimplemented;
1386 /* Options with 'int' argument. */
1387 case TARGET_SO_DEBUG:
1388 optname = SO_DEBUG;
1389 goto int_case;
1390 case TARGET_SO_REUSEADDR:
1391 optname = SO_REUSEADDR;
1392 goto int_case;
1393 case TARGET_SO_TYPE:
1394 optname = SO_TYPE;
1395 goto int_case;
1396 case TARGET_SO_ERROR:
1397 optname = SO_ERROR;
1398 goto int_case;
1399 case TARGET_SO_DONTROUTE:
1400 optname = SO_DONTROUTE;
1401 goto int_case;
1402 case TARGET_SO_BROADCAST:
1403 optname = SO_BROADCAST;
1404 goto int_case;
1405 case TARGET_SO_SNDBUF:
1406 optname = SO_SNDBUF;
1407 goto int_case;
1408 case TARGET_SO_RCVBUF:
1409 optname = SO_RCVBUF;
1410 goto int_case;
1411 case TARGET_SO_KEEPALIVE:
1412 optname = SO_KEEPALIVE;
1413 goto int_case;
1414 case TARGET_SO_OOBINLINE:
1415 optname = SO_OOBINLINE;
1416 goto int_case;
1417 case TARGET_SO_NO_CHECK:
1418 optname = SO_NO_CHECK;
1419 goto int_case;
1420 case TARGET_SO_PRIORITY:
1421 optname = SO_PRIORITY;
1422 goto int_case;
1423#ifdef SO_BSDCOMPAT
1424 case TARGET_SO_BSDCOMPAT:
1425 optname = SO_BSDCOMPAT;
1426 goto int_case;
1427#endif
1428 case TARGET_SO_PASSCRED:
1429 optname = SO_PASSCRED;
1430 goto int_case;
1431 case TARGET_SO_TIMESTAMP:
1432 optname = SO_TIMESTAMP;
1433 goto int_case;
1434 case TARGET_SO_RCVLOWAT:
1435 optname = SO_RCVLOWAT;
1436 goto int_case;
8853f86e 1437 default:
2efbe911
FB
1438 goto int_case;
1439 }
1440 break;
1441 case SOL_TCP:
1442 /* TCP options all take an 'int' value. */
1443 int_case:
2f619698
FB
1444 if (get_user_u32(len, optlen))
1445 return -TARGET_EFAULT;
2efbe911 1446 if (len < 0)
0da46a6e 1447 return -TARGET_EINVAL;
2efbe911
FB
1448 lv = sizeof(int);
1449 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1450 if (ret < 0)
1451 return ret;
2efbe911
FB
1452 if (len > lv)
1453 len = lv;
2f619698
FB
1454 if (len == 4) {
1455 if (put_user_u32(val, optval_addr))
1456 return -TARGET_EFAULT;
1457 } else {
1458 if (put_user_u8(val, optval_addr))
1459 return -TARGET_EFAULT;
f3b974cd 1460 }
2f619698
FB
1461 if (put_user_u32(len, optlen))
1462 return -TARGET_EFAULT;
2efbe911
FB
1463 break;
1464 case SOL_IP:
1465 switch(optname) {
1466 case IP_TOS:
1467 case IP_TTL:
1468 case IP_HDRINCL:
1469 case IP_ROUTER_ALERT:
1470 case IP_RECVOPTS:
1471 case IP_RETOPTS:
1472 case IP_PKTINFO:
1473 case IP_MTU_DISCOVER:
1474 case IP_RECVERR:
1475 case IP_RECVTOS:
1476#ifdef IP_FREEBIND
1477 case IP_FREEBIND:
1478#endif
1479 case IP_MULTICAST_TTL:
1480 case IP_MULTICAST_LOOP:
2f619698
FB
1481 if (get_user_u32(len, optlen))
1482 return -TARGET_EFAULT;
8853f86e 1483 if (len < 0)
0da46a6e 1484 return -TARGET_EINVAL;
8853f86e
FB
1485 lv = sizeof(int);
1486 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1487 if (ret < 0)
1488 return ret;
2efbe911 1489 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2efbe911 1490 len = 1;
2f619698
FB
1491 if (put_user_u32(len, optlen)
1492 || put_user_u8(val, optval_addr))
1493 return -TARGET_EFAULT;
2efbe911 1494 } else {
2efbe911
FB
1495 if (len > sizeof(int))
1496 len = sizeof(int);
2f619698
FB
1497 if (put_user_u32(len, optlen)
1498 || put_user_u32(val, optval_addr))
1499 return -TARGET_EFAULT;
2efbe911 1500 }
8853f86e 1501 break;
2efbe911 1502 default:
c02f499e
TS
1503 ret = -TARGET_ENOPROTOOPT;
1504 break;
8853f86e
FB
1505 }
1506 break;
1507 default:
1508 unimplemented:
1509 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1510 level, optname);
c02f499e 1511 ret = -TARGET_EOPNOTSUPP;
8853f86e
FB
1512 break;
1513 }
1514 return ret;
7854b056
FB
1515}
1516
579a97f7
FB
1517/* FIXME
1518 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1519 * other lock functions have a return code of 0 for failure.
1520 */
1521static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1522 int count, int copy)
53a5960a
PB
1523{
1524 struct target_iovec *target_vec;
992f48a0 1525 abi_ulong base;
d732dcb4 1526 int i;
53a5960a 1527
579a97f7
FB
1528 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1529 if (!target_vec)
1530 return -TARGET_EFAULT;
53a5960a
PB
1531 for(i = 0;i < count; i++) {
1532 base = tswapl(target_vec[i].iov_base);
1533 vec[i].iov_len = tswapl(target_vec[i].iov_len);
41df8411
FB
1534 if (vec[i].iov_len != 0) {
1535 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
d732dcb4
AZ
1536 /* Don't check lock_user return value. We must call writev even
1537 if a element has invalid base address. */
41df8411
FB
1538 } else {
1539 /* zero length pointer is ignored */
1540 vec[i].iov_base = NULL;
1541 }
579a97f7
FB
1542 }
1543 unlock_user (target_vec, target_addr, 0);
1544 return 0;
53a5960a
PB
1545}
1546
579a97f7
FB
1547static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1548 int count, int copy)
53a5960a
PB
1549{
1550 struct target_iovec *target_vec;
992f48a0 1551 abi_ulong base;
53a5960a
PB
1552 int i;
1553
579a97f7
FB
1554 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1555 if (!target_vec)
1556 return -TARGET_EFAULT;
53a5960a 1557 for(i = 0;i < count; i++) {
d732dcb4
AZ
1558 if (target_vec[i].iov_base) {
1559 base = tswapl(target_vec[i].iov_base);
1560 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1561 }
53a5960a
PB
1562 }
1563 unlock_user (target_vec, target_addr, 0);
579a97f7
FB
1564
1565 return 0;
53a5960a
PB
1566}
1567
0da46a6e 1568/* do_socket() Must return target values and target errnos. */
992f48a0 1569static abi_long do_socket(int domain, int type, int protocol)
3532fa74
FB
1570{
1571#if defined(TARGET_MIPS)
1572 switch(type) {
1573 case TARGET_SOCK_DGRAM:
1574 type = SOCK_DGRAM;
1575 break;
1576 case TARGET_SOCK_STREAM:
1577 type = SOCK_STREAM;
1578 break;
1579 case TARGET_SOCK_RAW:
1580 type = SOCK_RAW;
1581 break;
1582 case TARGET_SOCK_RDM:
1583 type = SOCK_RDM;
1584 break;
1585 case TARGET_SOCK_SEQPACKET:
1586 type = SOCK_SEQPACKET;
1587 break;
1588 case TARGET_SOCK_PACKET:
1589 type = SOCK_PACKET;
1590 break;
1591 }
1592#endif
12bc92ab
AZ
1593 if (domain == PF_NETLINK)
1594 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
3532fa74
FB
1595 return get_errno(socket(domain, type, protocol));
1596}
1597
0da46a6e 1598/* do_bind() Must return target values and target errnos. */
992f48a0
BS
1599static abi_long do_bind(int sockfd, abi_ulong target_addr,
1600 socklen_t addrlen)
3532fa74 1601{
8f7aeaf6 1602 void *addr;
917507b0 1603 abi_long ret;
8f7aeaf6 1604
38724253 1605 if ((int)addrlen < 0) {
8f7aeaf6 1606 return -TARGET_EINVAL;
38724253 1607 }
8f7aeaf6 1608
607175e0 1609 addr = alloca(addrlen+1);
3b46e624 1610
917507b0
AP
1611 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1612 if (ret)
1613 return ret;
1614
3532fa74
FB
1615 return get_errno(bind(sockfd, addr, addrlen));
1616}
1617
0da46a6e 1618/* do_connect() Must return target values and target errnos. */
992f48a0
BS
1619static abi_long do_connect(int sockfd, abi_ulong target_addr,
1620 socklen_t addrlen)
3532fa74 1621{
8f7aeaf6 1622 void *addr;
917507b0 1623 abi_long ret;
8f7aeaf6 1624
38724253 1625 if ((int)addrlen < 0) {
8f7aeaf6 1626 return -TARGET_EINVAL;
38724253 1627 }
8f7aeaf6
AJ
1628
1629 addr = alloca(addrlen);
3b46e624 1630
917507b0
AP
1631 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1632 if (ret)
1633 return ret;
1634
3532fa74
FB
1635 return get_errno(connect(sockfd, addr, addrlen));
1636}
1637
0da46a6e 1638/* do_sendrecvmsg() Must return target values and target errnos. */
992f48a0
BS
1639static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1640 int flags, int send)
3532fa74 1641{
6de645c7 1642 abi_long ret, len;
3532fa74
FB
1643 struct target_msghdr *msgp;
1644 struct msghdr msg;
1645 int count;
1646 struct iovec *vec;
992f48a0 1647 abi_ulong target_vec;
3532fa74 1648
579a97f7
FB
1649 /* FIXME */
1650 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1651 msgp,
1652 target_msg,
1653 send ? 1 : 0))
1654 return -TARGET_EFAULT;
3532fa74
FB
1655 if (msgp->msg_name) {
1656 msg.msg_namelen = tswap32(msgp->msg_namelen);
1657 msg.msg_name = alloca(msg.msg_namelen);
917507b0 1658 ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
3532fa74 1659 msg.msg_namelen);
917507b0
AP
1660 if (ret) {
1661 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1662 return ret;
1663 }
3532fa74
FB
1664 } else {
1665 msg.msg_name = NULL;
1666 msg.msg_namelen = 0;
1667 }
1668 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1669 msg.msg_control = alloca(msg.msg_controllen);
1670 msg.msg_flags = tswap32(msgp->msg_flags);
3b46e624 1671
3532fa74
FB
1672 count = tswapl(msgp->msg_iovlen);
1673 vec = alloca(count * sizeof(struct iovec));
1674 target_vec = tswapl(msgp->msg_iov);
579a97f7 1675 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
3532fa74
FB
1676 msg.msg_iovlen = count;
1677 msg.msg_iov = vec;
3b46e624 1678
3532fa74 1679 if (send) {
5a4a898d
FB
1680 ret = target_to_host_cmsg(&msg, msgp);
1681 if (ret == 0)
1682 ret = get_errno(sendmsg(fd, &msg, flags));
3532fa74
FB
1683 } else {
1684 ret = get_errno(recvmsg(fd, &msg, flags));
6de645c7
AZ
1685 if (!is_error(ret)) {
1686 len = ret;
5a4a898d 1687 ret = host_to_target_cmsg(msgp, &msg);
6de645c7
AZ
1688 if (!is_error(ret))
1689 ret = len;
1690 }
3532fa74
FB
1691 }
1692 unlock_iovec(vec, target_vec, count, !send);
579a97f7 1693 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3532fa74
FB
1694 return ret;
1695}
1696
0da46a6e 1697/* do_accept() Must return target values and target errnos. */
992f48a0 1698static abi_long do_accept(int fd, abi_ulong target_addr,
2f619698 1699 abi_ulong target_addrlen_addr)
1be9e1dc 1700{
2f619698
FB
1701 socklen_t addrlen;
1702 void *addr;
992f48a0 1703 abi_long ret;
1be9e1dc 1704
917507b0
AP
1705 if (target_addr == 0)
1706 return get_errno(accept(fd, NULL, NULL));
1707
1708 /* linux returns EINVAL if addrlen pointer is invalid */
2f619698 1709 if (get_user_u32(addrlen, target_addrlen_addr))
917507b0 1710 return -TARGET_EINVAL;
2f619698 1711
38724253 1712 if ((int)addrlen < 0) {
8f7aeaf6 1713 return -TARGET_EINVAL;
38724253 1714 }
8f7aeaf6 1715
917507b0
AP
1716 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1717 return -TARGET_EINVAL;
1718
2f619698
FB
1719 addr = alloca(addrlen);
1720
1be9e1dc
PB
1721 ret = get_errno(accept(fd, addr, &addrlen));
1722 if (!is_error(ret)) {
1723 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
1724 if (put_user_u32(addrlen, target_addrlen_addr))
1725 ret = -TARGET_EFAULT;
1be9e1dc
PB
1726 }
1727 return ret;
1728}
1729
0da46a6e 1730/* do_getpeername() Must return target values and target errnos. */
992f48a0 1731static abi_long do_getpeername(int fd, abi_ulong target_addr,
2f619698 1732 abi_ulong target_addrlen_addr)
1be9e1dc 1733{
2f619698
FB
1734 socklen_t addrlen;
1735 void *addr;
992f48a0 1736 abi_long ret;
1be9e1dc 1737
2f619698
FB
1738 if (get_user_u32(addrlen, target_addrlen_addr))
1739 return -TARGET_EFAULT;
1740
38724253 1741 if ((int)addrlen < 0) {
8f7aeaf6 1742 return -TARGET_EINVAL;
38724253 1743 }
8f7aeaf6 1744
917507b0
AP
1745 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1746 return -TARGET_EFAULT;
1747
2f619698
FB
1748 addr = alloca(addrlen);
1749
1be9e1dc
PB
1750 ret = get_errno(getpeername(fd, addr, &addrlen));
1751 if (!is_error(ret)) {
1752 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
1753 if (put_user_u32(addrlen, target_addrlen_addr))
1754 ret = -TARGET_EFAULT;
1be9e1dc
PB
1755 }
1756 return ret;
1757}
1758
0da46a6e 1759/* do_getsockname() Must return target values and target errnos. */
992f48a0 1760static abi_long do_getsockname(int fd, abi_ulong target_addr,
2f619698 1761 abi_ulong target_addrlen_addr)
1be9e1dc 1762{
2f619698
FB
1763 socklen_t addrlen;
1764 void *addr;
992f48a0 1765 abi_long ret;
1be9e1dc 1766
2f619698
FB
1767 if (get_user_u32(addrlen, target_addrlen_addr))
1768 return -TARGET_EFAULT;
1769
38724253 1770 if ((int)addrlen < 0) {
8f7aeaf6 1771 return -TARGET_EINVAL;
38724253 1772 }
8f7aeaf6 1773
917507b0
AP
1774 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1775 return -TARGET_EFAULT;
1776
2f619698
FB
1777 addr = alloca(addrlen);
1778
1be9e1dc
PB
1779 ret = get_errno(getsockname(fd, addr, &addrlen));
1780 if (!is_error(ret)) {
1781 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
1782 if (put_user_u32(addrlen, target_addrlen_addr))
1783 ret = -TARGET_EFAULT;
1be9e1dc
PB
1784 }
1785 return ret;
1786}
1787
0da46a6e 1788/* do_socketpair() Must return target values and target errnos. */
992f48a0 1789static abi_long do_socketpair(int domain, int type, int protocol,
2f619698 1790 abi_ulong target_tab_addr)
1be9e1dc
PB
1791{
1792 int tab[2];
992f48a0 1793 abi_long ret;
1be9e1dc
PB
1794
1795 ret = get_errno(socketpair(domain, type, protocol, tab));
1796 if (!is_error(ret)) {
2f619698
FB
1797 if (put_user_s32(tab[0], target_tab_addr)
1798 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1799 ret = -TARGET_EFAULT;
1be9e1dc
PB
1800 }
1801 return ret;
1802}
1803
0da46a6e 1804/* do_sendto() Must return target values and target errnos. */
992f48a0
BS
1805static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1806 abi_ulong target_addr, socklen_t addrlen)
1be9e1dc
PB
1807{
1808 void *addr;
1809 void *host_msg;
992f48a0 1810 abi_long ret;
1be9e1dc 1811
38724253 1812 if ((int)addrlen < 0) {
8f7aeaf6 1813 return -TARGET_EINVAL;
38724253 1814 }
8f7aeaf6 1815
579a97f7
FB
1816 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1817 if (!host_msg)
1818 return -TARGET_EFAULT;
1be9e1dc
PB
1819 if (target_addr) {
1820 addr = alloca(addrlen);
917507b0
AP
1821 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1822 if (ret) {
1823 unlock_user(host_msg, msg, 0);
1824 return ret;
1825 }
1be9e1dc
PB
1826 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1827 } else {
1828 ret = get_errno(send(fd, host_msg, len, flags));
1829 }
1830 unlock_user(host_msg, msg, 0);
1831 return ret;
1832}
1833
0da46a6e 1834/* do_recvfrom() Must return target values and target errnos. */
992f48a0
BS
1835static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1836 abi_ulong target_addr,
1837 abi_ulong target_addrlen)
1be9e1dc
PB
1838{
1839 socklen_t addrlen;
1840 void *addr;
1841 void *host_msg;
992f48a0 1842 abi_long ret;
1be9e1dc 1843
579a97f7
FB
1844 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1845 if (!host_msg)
1846 return -TARGET_EFAULT;
1be9e1dc 1847 if (target_addr) {
2f619698
FB
1848 if (get_user_u32(addrlen, target_addrlen)) {
1849 ret = -TARGET_EFAULT;
1850 goto fail;
1851 }
38724253 1852 if ((int)addrlen < 0) {
8f7aeaf6
AJ
1853 ret = -TARGET_EINVAL;
1854 goto fail;
1855 }
1be9e1dc
PB
1856 addr = alloca(addrlen);
1857 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1858 } else {
1859 addr = NULL; /* To keep compiler quiet. */
1860 ret = get_errno(recv(fd, host_msg, len, flags));
1861 }
1862 if (!is_error(ret)) {
1863 if (target_addr) {
1864 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
1865 if (put_user_u32(addrlen, target_addrlen)) {
1866 ret = -TARGET_EFAULT;
1867 goto fail;
1868 }
1be9e1dc
PB
1869 }
1870 unlock_user(host_msg, msg, len);
1871 } else {
2f619698 1872fail:
1be9e1dc
PB
1873 unlock_user(host_msg, msg, 0);
1874 }
1875 return ret;
1876}
1877
32407103 1878#ifdef TARGET_NR_socketcall
0da46a6e 1879/* do_socketcall() Must return target values and target errnos. */
992f48a0 1880static abi_long do_socketcall(int num, abi_ulong vptr)
31e31b8a 1881{
992f48a0
BS
1882 abi_long ret;
1883 const int n = sizeof(abi_ulong);
31e31b8a
FB
1884
1885 switch(num) {
1886 case SOCKOP_socket:
7854b056 1887 {
98818189 1888 abi_ulong domain, type, protocol;
2f619698 1889
98818189
UH
1890 if (get_user_ual(domain, vptr)
1891 || get_user_ual(type, vptr + n)
1892 || get_user_ual(protocol, vptr + 2 * n))
2f619698
FB
1893 return -TARGET_EFAULT;
1894
3532fa74 1895 ret = do_socket(domain, type, protocol);
7854b056 1896 }
31e31b8a
FB
1897 break;
1898 case SOCKOP_bind:
7854b056 1899 {
98818189 1900 abi_ulong sockfd;
2f619698
FB
1901 abi_ulong target_addr;
1902 socklen_t addrlen;
1903
98818189 1904 if (get_user_ual(sockfd, vptr)
2f619698 1905 || get_user_ual(target_addr, vptr + n)
98818189 1906 || get_user_ual(addrlen, vptr + 2 * n))
2f619698
FB
1907 return -TARGET_EFAULT;
1908
3532fa74 1909 ret = do_bind(sockfd, target_addr, addrlen);
7854b056 1910 }
31e31b8a
FB
1911 break;
1912 case SOCKOP_connect:
7854b056 1913 {
98818189 1914 abi_ulong sockfd;
2f619698
FB
1915 abi_ulong target_addr;
1916 socklen_t addrlen;
1917
98818189 1918 if (get_user_ual(sockfd, vptr)
2f619698 1919 || get_user_ual(target_addr, vptr + n)
98818189 1920 || get_user_ual(addrlen, vptr + 2 * n))
2f619698
FB
1921 return -TARGET_EFAULT;
1922
3532fa74 1923 ret = do_connect(sockfd, target_addr, addrlen);
7854b056 1924 }
31e31b8a
FB
1925 break;
1926 case SOCKOP_listen:
7854b056 1927 {
98818189 1928 abi_ulong sockfd, backlog;
2f619698 1929
98818189
UH
1930 if (get_user_ual(sockfd, vptr)
1931 || get_user_ual(backlog, vptr + n))
2f619698
FB
1932 return -TARGET_EFAULT;
1933
7854b056
FB
1934 ret = get_errno(listen(sockfd, backlog));
1935 }
31e31b8a
FB
1936 break;
1937 case SOCKOP_accept:
1938 {
98818189 1939 abi_ulong sockfd;
2f619698
FB
1940 abi_ulong target_addr, target_addrlen;
1941
98818189 1942 if (get_user_ual(sockfd, vptr)
2f619698 1943 || get_user_ual(target_addr, vptr + n)
98818189 1944 || get_user_ual(target_addrlen, vptr + 2 * n))
2f619698
FB
1945 return -TARGET_EFAULT;
1946
1be9e1dc 1947 ret = do_accept(sockfd, target_addr, target_addrlen);
31e31b8a
FB
1948 }
1949 break;
1950 case SOCKOP_getsockname:
1951 {
98818189 1952 abi_ulong sockfd;
2f619698
FB
1953 abi_ulong target_addr, target_addrlen;
1954
98818189 1955 if (get_user_ual(sockfd, vptr)
2f619698 1956 || get_user_ual(target_addr, vptr + n)
98818189 1957 || get_user_ual(target_addrlen, vptr + 2 * n))
2f619698
FB
1958 return -TARGET_EFAULT;
1959
1be9e1dc 1960 ret = do_getsockname(sockfd, target_addr, target_addrlen);
31e31b8a
FB
1961 }
1962 break;
1963 case SOCKOP_getpeername:
1964 {
98818189 1965 abi_ulong sockfd;
2f619698
FB
1966 abi_ulong target_addr, target_addrlen;
1967
98818189 1968 if (get_user_ual(sockfd, vptr)
2f619698 1969 || get_user_ual(target_addr, vptr + n)
98818189 1970 || get_user_ual(target_addrlen, vptr + 2 * n))
2f619698
FB
1971 return -TARGET_EFAULT;
1972
1be9e1dc 1973 ret = do_getpeername(sockfd, target_addr, target_addrlen);
31e31b8a
FB
1974 }
1975 break;
1976 case SOCKOP_socketpair:
1977 {
98818189 1978 abi_ulong domain, type, protocol;
2f619698
FB
1979 abi_ulong tab;
1980
98818189
UH
1981 if (get_user_ual(domain, vptr)
1982 || get_user_ual(type, vptr + n)
1983 || get_user_ual(protocol, vptr + 2 * n)
2f619698
FB
1984 || get_user_ual(tab, vptr + 3 * n))
1985 return -TARGET_EFAULT;
1986
1be9e1dc 1987 ret = do_socketpair(domain, type, protocol, tab);
31e31b8a
FB
1988 }
1989 break;
1990 case SOCKOP_send:
7854b056 1991 {
98818189 1992 abi_ulong sockfd;
2f619698
FB
1993 abi_ulong msg;
1994 size_t len;
98818189 1995 abi_ulong flags;
2f619698 1996
98818189 1997 if (get_user_ual(sockfd, vptr)
2f619698
FB
1998 || get_user_ual(msg, vptr + n)
1999 || get_user_ual(len, vptr + 2 * n)
98818189 2000 || get_user_ual(flags, vptr + 3 * n))
2f619698
FB
2001 return -TARGET_EFAULT;
2002
1be9e1dc 2003 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
7854b056 2004 }
31e31b8a
FB
2005 break;
2006 case SOCKOP_recv:
7854b056 2007 {
98818189 2008 abi_ulong sockfd;
2f619698
FB
2009 abi_ulong msg;
2010 size_t len;
98818189 2011 abi_ulong flags;
2f619698 2012
98818189 2013 if (get_user_ual(sockfd, vptr)
2f619698
FB
2014 || get_user_ual(msg, vptr + n)
2015 || get_user_ual(len, vptr + 2 * n)
98818189 2016 || get_user_ual(flags, vptr + 3 * n))
2f619698
FB
2017 return -TARGET_EFAULT;
2018
1be9e1dc 2019 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
7854b056 2020 }
31e31b8a
FB
2021 break;
2022 case SOCKOP_sendto:
7854b056 2023 {
98818189 2024 abi_ulong sockfd;
2f619698
FB
2025 abi_ulong msg;
2026 size_t len;
98818189 2027 abi_ulong flags;
2f619698
FB
2028 abi_ulong addr;
2029 socklen_t addrlen;
2030
98818189 2031 if (get_user_ual(sockfd, vptr)
2f619698
FB
2032 || get_user_ual(msg, vptr + n)
2033 || get_user_ual(len, vptr + 2 * n)
98818189 2034 || get_user_ual(flags, vptr + 3 * n)
2f619698 2035 || get_user_ual(addr, vptr + 4 * n)
98818189 2036 || get_user_ual(addrlen, vptr + 5 * n))
2f619698
FB
2037 return -TARGET_EFAULT;
2038
1be9e1dc 2039 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
7854b056 2040 }
31e31b8a
FB
2041 break;
2042 case SOCKOP_recvfrom:
2043 {
98818189 2044 abi_ulong sockfd;
2f619698
FB
2045 abi_ulong msg;
2046 size_t len;
98818189 2047 abi_ulong flags;
2f619698
FB
2048 abi_ulong addr;
2049 socklen_t addrlen;
2050
98818189 2051 if (get_user_ual(sockfd, vptr)
2f619698
FB
2052 || get_user_ual(msg, vptr + n)
2053 || get_user_ual(len, vptr + 2 * n)
98818189 2054 || get_user_ual(flags, vptr + 3 * n)
2f619698 2055 || get_user_ual(addr, vptr + 4 * n)
98818189 2056 || get_user_ual(addrlen, vptr + 5 * n))
2f619698
FB
2057 return -TARGET_EFAULT;
2058
1be9e1dc 2059 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
31e31b8a
FB
2060 }
2061 break;
2062 case SOCKOP_shutdown:
7854b056 2063 {
98818189 2064 abi_ulong sockfd, how;
2f619698 2065
98818189
UH
2066 if (get_user_ual(sockfd, vptr)
2067 || get_user_ual(how, vptr + n))
2f619698 2068 return -TARGET_EFAULT;
7854b056
FB
2069
2070 ret = get_errno(shutdown(sockfd, how));
2071 }
31e31b8a
FB
2072 break;
2073 case SOCKOP_sendmsg:
2074 case SOCKOP_recvmsg:
1a9353d2 2075 {
98818189 2076 abi_ulong fd;
992f48a0 2077 abi_ulong target_msg;
98818189 2078 abi_ulong flags;
1a9353d2 2079
98818189 2080 if (get_user_ual(fd, vptr)
2f619698 2081 || get_user_ual(target_msg, vptr + n)
98818189 2082 || get_user_ual(flags, vptr + 2 * n))
2f619698 2083 return -TARGET_EFAULT;
3532fa74 2084
5fafdf24 2085 ret = do_sendrecvmsg(fd, target_msg, flags,
3532fa74 2086 (num == SOCKOP_sendmsg));
1a9353d2
FB
2087 }
2088 break;
31e31b8a 2089 case SOCKOP_setsockopt:
7854b056 2090 {
98818189
UH
2091 abi_ulong sockfd;
2092 abi_ulong level;
2093 abi_ulong optname;
2f619698
FB
2094 abi_ulong optval;
2095 socklen_t optlen;
2096
98818189
UH
2097 if (get_user_ual(sockfd, vptr)
2098 || get_user_ual(level, vptr + n)
2099 || get_user_ual(optname, vptr + 2 * n)
2f619698 2100 || get_user_ual(optval, vptr + 3 * n)
98818189 2101 || get_user_ual(optlen, vptr + 4 * n))
2f619698 2102 return -TARGET_EFAULT;
7854b056
FB
2103
2104 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2105 }
2106 break;
31e31b8a 2107 case SOCKOP_getsockopt:
7854b056 2108 {
98818189
UH
2109 abi_ulong sockfd;
2110 abi_ulong level;
2111 abi_ulong optname;
2f619698
FB
2112 abi_ulong optval;
2113 socklen_t optlen;
2114
98818189
UH
2115 if (get_user_ual(sockfd, vptr)
2116 || get_user_ual(level, vptr + n)
2117 || get_user_ual(optname, vptr + 2 * n)
2f619698 2118 || get_user_ual(optval, vptr + 3 * n)
98818189 2119 || get_user_ual(optlen, vptr + 4 * n))
2f619698 2120 return -TARGET_EFAULT;
7854b056 2121
2f619698 2122 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
7854b056
FB
2123 }
2124 break;
31e31b8a
FB
2125 default:
2126 gemu_log("Unsupported socketcall: %d\n", num);
0da46a6e 2127 ret = -TARGET_ENOSYS;
31e31b8a
FB
2128 break;
2129 }
2130 return ret;
2131}
32407103 2132#endif
31e31b8a 2133
8853f86e
FB
2134#define N_SHM_REGIONS 32
2135
2136static struct shm_region {
5a4a898d
FB
2137 abi_ulong start;
2138 abi_ulong size;
8853f86e
FB
2139} shm_regions[N_SHM_REGIONS];
2140
3eb6b044
TS
2141struct target_ipc_perm
2142{
992f48a0
BS
2143 abi_long __key;
2144 abi_ulong uid;
2145 abi_ulong gid;
2146 abi_ulong cuid;
2147 abi_ulong cgid;
3eb6b044
TS
2148 unsigned short int mode;
2149 unsigned short int __pad1;
2150 unsigned short int __seq;
2151 unsigned short int __pad2;
992f48a0
BS
2152 abi_ulong __unused1;
2153 abi_ulong __unused2;
3eb6b044
TS
2154};
2155
2156struct target_semid_ds
2157{
2158 struct target_ipc_perm sem_perm;
992f48a0
BS
2159 abi_ulong sem_otime;
2160 abi_ulong __unused1;
2161 abi_ulong sem_ctime;
2162 abi_ulong __unused2;
2163 abi_ulong sem_nsems;
2164 abi_ulong __unused3;
2165 abi_ulong __unused4;
3eb6b044
TS
2166};
2167
579a97f7
FB
2168static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2169 abi_ulong target_addr)
3eb6b044
TS
2170{
2171 struct target_ipc_perm *target_ip;
2172 struct target_semid_ds *target_sd;
2173
579a97f7
FB
2174 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2175 return -TARGET_EFAULT;
e8bbe36c 2176 target_ip = &(target_sd->sem_perm);
3eb6b044
TS
2177 host_ip->__key = tswapl(target_ip->__key);
2178 host_ip->uid = tswapl(target_ip->uid);
2179 host_ip->gid = tswapl(target_ip->gid);
2180 host_ip->cuid = tswapl(target_ip->cuid);
2181 host_ip->cgid = tswapl(target_ip->cgid);
2182 host_ip->mode = tswapl(target_ip->mode);
2183 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 2184 return 0;
3eb6b044
TS
2185}
2186
579a97f7
FB
2187static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2188 struct ipc_perm *host_ip)
3eb6b044
TS
2189{
2190 struct target_ipc_perm *target_ip;
2191 struct target_semid_ds *target_sd;
2192
579a97f7
FB
2193 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2194 return -TARGET_EFAULT;
3eb6b044
TS
2195 target_ip = &(target_sd->sem_perm);
2196 target_ip->__key = tswapl(host_ip->__key);
2197 target_ip->uid = tswapl(host_ip->uid);
2198 target_ip->gid = tswapl(host_ip->gid);
2199 target_ip->cuid = tswapl(host_ip->cuid);
2200 target_ip->cgid = tswapl(host_ip->cgid);
2201 target_ip->mode = tswapl(host_ip->mode);
2202 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 2203 return 0;
3eb6b044
TS
2204}
2205
579a97f7
FB
2206static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2207 abi_ulong target_addr)
3eb6b044
TS
2208{
2209 struct target_semid_ds *target_sd;
2210
579a97f7
FB
2211 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2212 return -TARGET_EFAULT;
e5289087
AJ
2213 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2214 return -TARGET_EFAULT;
3eb6b044
TS
2215 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2216 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2217 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2218 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 2219 return 0;
3eb6b044
TS
2220}
2221
579a97f7
FB
2222static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2223 struct semid_ds *host_sd)
3eb6b044
TS
2224{
2225 struct target_semid_ds *target_sd;
2226
579a97f7
FB
2227 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2228 return -TARGET_EFAULT;
e5289087
AJ
2229 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2230 return -TARGET_EFAULT;;
3eb6b044
TS
2231 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2232 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2233 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2234 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 2235 return 0;
3eb6b044
TS
2236}
2237
e5289087
AJ
2238struct target_seminfo {
2239 int semmap;
2240 int semmni;
2241 int semmns;
2242 int semmnu;
2243 int semmsl;
2244 int semopm;
2245 int semume;
2246 int semusz;
2247 int semvmx;
2248 int semaem;
2249};
2250
2251static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2252 struct seminfo *host_seminfo)
2253{
2254 struct target_seminfo *target_seminfo;
2255 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2256 return -TARGET_EFAULT;
2257 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2258 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2259 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2260 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2261 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2262 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2263 __put_user(host_seminfo->semume, &target_seminfo->semume);
2264 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2265 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2266 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2267 unlock_user_struct(target_seminfo, target_addr, 1);
2268 return 0;
2269}
2270
fa294816
TS
2271union semun {
2272 int val;
3eb6b044 2273 struct semid_ds *buf;
fa294816 2274 unsigned short *array;
e5289087 2275 struct seminfo *__buf;
fa294816
TS
2276};
2277
3eb6b044
TS
2278union target_semun {
2279 int val;
e5289087
AJ
2280 abi_ulong buf;
2281 abi_ulong array;
2282 abi_ulong __buf;
3eb6b044
TS
2283};
2284
e5289087
AJ
2285static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2286 abi_ulong target_addr)
3eb6b044 2287{
e5289087
AJ
2288 int nsems;
2289 unsigned short *array;
2290 union semun semun;
2291 struct semid_ds semid_ds;
2292 int i, ret;
3eb6b044 2293
e5289087
AJ
2294 semun.buf = &semid_ds;
2295
2296 ret = semctl(semid, 0, IPC_STAT, semun);
2297 if (ret == -1)
2298 return get_errno(ret);
2299
2300 nsems = semid_ds.sem_nsems;
2301
2302 *host_array = malloc(nsems*sizeof(unsigned short));
2303 array = lock_user(VERIFY_READ, target_addr,
2304 nsems*sizeof(unsigned short), 1);
2305 if (!array)
2306 return -TARGET_EFAULT;
2307
2308 for(i=0; i<nsems; i++) {
2309 __get_user((*host_array)[i], &array[i]);
3eb6b044 2310 }
e5289087
AJ
2311 unlock_user(array, target_addr, 0);
2312
579a97f7 2313 return 0;
3eb6b044
TS
2314}
2315
e5289087
AJ
2316static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2317 unsigned short **host_array)
3eb6b044 2318{
e5289087
AJ
2319 int nsems;
2320 unsigned short *array;
2321 union semun semun;
2322 struct semid_ds semid_ds;
2323 int i, ret;
3eb6b044 2324
e5289087
AJ
2325 semun.buf = &semid_ds;
2326
2327 ret = semctl(semid, 0, IPC_STAT, semun);
2328 if (ret == -1)
2329 return get_errno(ret);
2330
2331 nsems = semid_ds.sem_nsems;
2332
2333 array = lock_user(VERIFY_WRITE, target_addr,
2334 nsems*sizeof(unsigned short), 0);
2335 if (!array)
2336 return -TARGET_EFAULT;
2337
2338 for(i=0; i<nsems; i++) {
2339 __put_user((*host_array)[i], &array[i]);
3eb6b044 2340 }
e5289087
AJ
2341 free(*host_array);
2342 unlock_user(array, target_addr, 1);
2343
579a97f7 2344 return 0;
3eb6b044
TS
2345}
2346
e5289087
AJ
2347static inline abi_long do_semctl(int semid, int semnum, int cmd,
2348 union target_semun target_su)
3eb6b044
TS
2349{
2350 union semun arg;
2351 struct semid_ds dsarg;
7b8118e8 2352 unsigned short *array = NULL;
e5289087
AJ
2353 struct seminfo seminfo;
2354 abi_long ret = -TARGET_EINVAL;
2355 abi_long err;
2356 cmd &= 0xff;
3eb6b044
TS
2357
2358 switch( cmd ) {
2359 case GETVAL:
3eb6b044 2360 case SETVAL:
e5289087
AJ
2361 arg.val = tswapl(target_su.val);
2362 ret = get_errno(semctl(semid, semnum, cmd, arg));
2363 target_su.val = tswapl(arg.val);
3eb6b044
TS
2364 break;
2365 case GETALL:
3eb6b044 2366 case SETALL:
e5289087
AJ
2367 err = target_to_host_semarray(semid, &array, target_su.array);
2368 if (err)
2369 return err;
2370 arg.array = array;
2371 ret = get_errno(semctl(semid, semnum, cmd, arg));
2372 err = host_to_target_semarray(semid, target_su.array, &array);
2373 if (err)
2374 return err;
3eb6b044
TS
2375 break;
2376 case IPC_STAT:
3eb6b044 2377 case IPC_SET:
e5289087
AJ
2378 case SEM_STAT:
2379 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2380 if (err)
2381 return err;
2382 arg.buf = &dsarg;
2383 ret = get_errno(semctl(semid, semnum, cmd, arg));
2384 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2385 if (err)
2386 return err;
2387 break;
2388 case IPC_INFO:
2389 case SEM_INFO:
2390 arg.__buf = &seminfo;
2391 ret = get_errno(semctl(semid, semnum, cmd, arg));
2392 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2393 if (err)
2394 return err;
2395 break;
2396 case IPC_RMID:
2397 case GETPID:
2398 case GETNCNT:
2399 case GETZCNT:
2400 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3eb6b044 2401 break;
3eb6b044
TS
2402 }
2403
2404 return ret;
2405}
2406
e5289087
AJ
2407struct target_sembuf {
2408 unsigned short sem_num;
2409 short sem_op;
2410 short sem_flg;
2411};
2412
2413static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2414 abi_ulong target_addr,
2415 unsigned nsops)
2416{
2417 struct target_sembuf *target_sembuf;
2418 int i;
2419
2420 target_sembuf = lock_user(VERIFY_READ, target_addr,
2421 nsops*sizeof(struct target_sembuf), 1);
2422 if (!target_sembuf)
2423 return -TARGET_EFAULT;
2424
2425 for(i=0; i<nsops; i++) {
2426 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2427 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2428 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2429 }
2430
2431 unlock_user(target_sembuf, target_addr, 0);
2432
2433 return 0;
2434}
2435
2436static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2437{
2438 struct sembuf sops[nsops];
2439
2440 if (target_to_host_sembuf(sops, ptr, nsops))
2441 return -TARGET_EFAULT;
2442
2443 return semop(semid, sops, nsops);
2444}
2445
1bc012f6
TS
2446struct target_msqid_ds
2447{
1c54ff97
AJ
2448 struct target_ipc_perm msg_perm;
2449 abi_ulong msg_stime;
2450#if TARGET_ABI_BITS == 32
2451 abi_ulong __unused1;
2452#endif
2453 abi_ulong msg_rtime;
2454#if TARGET_ABI_BITS == 32
2455 abi_ulong __unused2;
2456#endif
2457 abi_ulong msg_ctime;
2458#if TARGET_ABI_BITS == 32
2459 abi_ulong __unused3;
2460#endif
2461 abi_ulong __msg_cbytes;
2462 abi_ulong msg_qnum;
2463 abi_ulong msg_qbytes;
2464 abi_ulong msg_lspid;
2465 abi_ulong msg_lrpid;
2466 abi_ulong __unused4;
2467 abi_ulong __unused5;
1bc012f6
TS
2468};
2469
579a97f7
FB
2470static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2471 abi_ulong target_addr)
1bc012f6
TS
2472{
2473 struct target_msqid_ds *target_md;
2474
579a97f7
FB
2475 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2476 return -TARGET_EFAULT;
1c54ff97
AJ
2477 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2478 return -TARGET_EFAULT;
1bc012f6
TS
2479 host_md->msg_stime = tswapl(target_md->msg_stime);
2480 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2481 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2482 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2483 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2484 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2485 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2486 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2487 unlock_user_struct(target_md, target_addr, 0);
579a97f7 2488 return 0;
1bc012f6
TS
2489}
2490
579a97f7
FB
2491static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2492 struct msqid_ds *host_md)
1bc012f6
TS
2493{
2494 struct target_msqid_ds *target_md;
2495
579a97f7
FB
2496 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2497 return -TARGET_EFAULT;
1c54ff97
AJ
2498 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2499 return -TARGET_EFAULT;
1bc012f6
TS
2500 target_md->msg_stime = tswapl(host_md->msg_stime);
2501 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2502 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2503 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2504 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2505 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2506 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2507 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2508 unlock_user_struct(target_md, target_addr, 1);
579a97f7 2509 return 0;
1bc012f6
TS
2510}
2511
1c54ff97
AJ
2512struct target_msginfo {
2513 int msgpool;
2514 int msgmap;
2515 int msgmax;
2516 int msgmnb;
2517 int msgmni;
2518 int msgssz;
2519 int msgtql;
2520 unsigned short int msgseg;
2521};
2522
2523static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2524 struct msginfo *host_msginfo)
2525{
2526 struct target_msginfo *target_msginfo;
2527 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2528 return -TARGET_EFAULT;
2529 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2530 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2531 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2532 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2533 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2534 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2535 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2536 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2537 unlock_user_struct(target_msginfo, target_addr, 1);
00b229ac 2538 return 0;
1c54ff97
AJ
2539}
2540
2541static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
1bc012f6
TS
2542{
2543 struct msqid_ds dsarg;
1c54ff97
AJ
2544 struct msginfo msginfo;
2545 abi_long ret = -TARGET_EINVAL;
2546
2547 cmd &= 0xff;
2548
2549 switch (cmd) {
1bc012f6
TS
2550 case IPC_STAT:
2551 case IPC_SET:
1c54ff97
AJ
2552 case MSG_STAT:
2553 if (target_to_host_msqid_ds(&dsarg,ptr))
2554 return -TARGET_EFAULT;
2555 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2556 if (host_to_target_msqid_ds(ptr,&dsarg))
2557 return -TARGET_EFAULT;
2558 break;
2559 case IPC_RMID:
2560 ret = get_errno(msgctl(msgid, cmd, NULL));
2561 break;
2562 case IPC_INFO:
2563 case MSG_INFO:
2564 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2565 if (host_to_target_msginfo(ptr, &msginfo))
2566 return -TARGET_EFAULT;
2567 break;
1bc012f6 2568 }
1c54ff97 2569
1bc012f6
TS
2570 return ret;
2571}
2572
2573struct target_msgbuf {
1c54ff97
AJ
2574 abi_long mtype;
2575 char mtext[1];
1bc012f6
TS
2576};
2577
992f48a0
BS
2578static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2579 unsigned int msgsz, int msgflg)
1bc012f6
TS
2580{
2581 struct target_msgbuf *target_mb;
2582 struct msgbuf *host_mb;
992f48a0 2583 abi_long ret = 0;
1bc012f6 2584
579a97f7
FB
2585 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2586 return -TARGET_EFAULT;
1bc012f6 2587 host_mb = malloc(msgsz+sizeof(long));
1c54ff97
AJ
2588 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2589 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
1bc012f6
TS
2590 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2591 free(host_mb);
2592 unlock_user_struct(target_mb, msgp, 0);
2593
2594 return ret;
2595}
2596
992f48a0 2597static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1c54ff97 2598 unsigned int msgsz, abi_long msgtyp,
992f48a0 2599 int msgflg)
1bc012f6
TS
2600{
2601 struct target_msgbuf *target_mb;
579a97f7 2602 char *target_mtext;
1bc012f6 2603 struct msgbuf *host_mb;
992f48a0 2604 abi_long ret = 0;
1bc012f6 2605
579a97f7
FB
2606 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2607 return -TARGET_EFAULT;
1c54ff97 2608
1bc012f6 2609 host_mb = malloc(msgsz+sizeof(long));
1c54ff97
AJ
2610 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2611
579a97f7
FB
2612 if (ret > 0) {
2613 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2614 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2615 if (!target_mtext) {
2616 ret = -TARGET_EFAULT;
2617 goto end;
2618 }
1c54ff97 2619 memcpy(target_mb->mtext, host_mb->mtext, ret);
579a97f7
FB
2620 unlock_user(target_mtext, target_mtext_addr, ret);
2621 }
1c54ff97 2622
1bc012f6
TS
2623 target_mb->mtype = tswapl(host_mb->mtype);
2624 free(host_mb);
1bc012f6 2625
579a97f7
FB
2626end:
2627 if (target_mb)
2628 unlock_user_struct(target_mb, msgp, 1);
1bc012f6
TS
2629 return ret;
2630}
2631
88a8c984
RV
2632struct target_shmid_ds
2633{
2634 struct target_ipc_perm shm_perm;
2635 abi_ulong shm_segsz;
2636 abi_ulong shm_atime;
2637#if TARGET_ABI_BITS == 32
2638 abi_ulong __unused1;
2639#endif
2640 abi_ulong shm_dtime;
2641#if TARGET_ABI_BITS == 32
2642 abi_ulong __unused2;
2643#endif
2644 abi_ulong shm_ctime;
2645#if TARGET_ABI_BITS == 32
2646 abi_ulong __unused3;
2647#endif
2648 int shm_cpid;
2649 int shm_lpid;
2650 abi_ulong shm_nattch;
2651 unsigned long int __unused4;
2652 unsigned long int __unused5;
2653};
2654
2655static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2656 abi_ulong target_addr)
2657{
2658 struct target_shmid_ds *target_sd;
2659
2660 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2661 return -TARGET_EFAULT;
2662 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2663 return -TARGET_EFAULT;
2664 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2665 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2666 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2667 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2668 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2669 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2670 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2671 unlock_user_struct(target_sd, target_addr, 0);
2672 return 0;
2673}
2674
2675static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2676 struct shmid_ds *host_sd)
2677{
2678 struct target_shmid_ds *target_sd;
2679
2680 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2681 return -TARGET_EFAULT;
2682 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2683 return -TARGET_EFAULT;
2684 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2685 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2686 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2687 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2688 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2689 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2690 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2691 unlock_user_struct(target_sd, target_addr, 1);
2692 return 0;
2693}
2694
2695struct target_shminfo {
2696 abi_ulong shmmax;
2697 abi_ulong shmmin;
2698 abi_ulong shmmni;
2699 abi_ulong shmseg;
2700 abi_ulong shmall;
2701};
2702
2703static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2704 struct shminfo *host_shminfo)
2705{
2706 struct target_shminfo *target_shminfo;
2707 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2708 return -TARGET_EFAULT;
2709 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2710 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2711 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2712 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2713 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2714 unlock_user_struct(target_shminfo, target_addr, 1);
2715 return 0;
2716}
2717
2718struct target_shm_info {
2719 int used_ids;
2720 abi_ulong shm_tot;
2721 abi_ulong shm_rss;
2722 abi_ulong shm_swp;
2723 abi_ulong swap_attempts;
2724 abi_ulong swap_successes;
2725};
2726
2727static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2728 struct shm_info *host_shm_info)
2729{
2730 struct target_shm_info *target_shm_info;
2731 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2732 return -TARGET_EFAULT;
2733 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2734 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2735 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2736 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2737 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2738 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2739 unlock_user_struct(target_shm_info, target_addr, 1);
2740 return 0;
2741}
2742
2743static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2744{
2745 struct shmid_ds dsarg;
2746 struct shminfo shminfo;
2747 struct shm_info shm_info;
2748 abi_long ret = -TARGET_EINVAL;
2749
2750 cmd &= 0xff;
2751
2752 switch(cmd) {
2753 case IPC_STAT:
2754 case IPC_SET:
2755 case SHM_STAT:
2756 if (target_to_host_shmid_ds(&dsarg, buf))
2757 return -TARGET_EFAULT;
2758 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2759 if (host_to_target_shmid_ds(buf, &dsarg))
2760 return -TARGET_EFAULT;
2761 break;
2762 case IPC_INFO:
2763 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2764 if (host_to_target_shminfo(buf, &shminfo))
2765 return -TARGET_EFAULT;
2766 break;
2767 case SHM_INFO:
2768 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2769 if (host_to_target_shm_info(buf, &shm_info))
2770 return -TARGET_EFAULT;
2771 break;
2772 case IPC_RMID:
2773 case SHM_LOCK:
2774 case SHM_UNLOCK:
2775 ret = get_errno(shmctl(shmid, cmd, NULL));
2776 break;
2777 }
2778
2779 return ret;
2780}
2781
2782static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2783{
2784 abi_long raddr;
2785 void *host_raddr;
2786 struct shmid_ds shm_info;
2787 int i,ret;
2788
2789 /* find out the length of the shared memory segment */
2790 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2791 if (is_error(ret)) {
2792 /* can't get length, bail out */
2793 return ret;
2794 }
2795
2796 mmap_lock();
2797
2798 if (shmaddr)
2799 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2800 else {
2801 abi_ulong mmap_start;
2802
2803 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2804
2805 if (mmap_start == -1) {
2806 errno = ENOMEM;
2807 host_raddr = (void *)-1;
2808 } else
2809 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2810 }
2811
2812 if (host_raddr == (void *)-1) {
2813 mmap_unlock();
2814 return get_errno((long)host_raddr);
2815 }
2816 raddr=h2g((unsigned long)host_raddr);
2817
2818 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2819 PAGE_VALID | PAGE_READ |
2820 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2821
2822 for (i = 0; i < N_SHM_REGIONS; i++) {
2823 if (shm_regions[i].start == 0) {
2824 shm_regions[i].start = raddr;
2825 shm_regions[i].size = shm_info.shm_segsz;
2826 break;
2827 }
2828 }
2829
2830 mmap_unlock();
2831 return raddr;
2832
2833}
2834
2835static inline abi_long do_shmdt(abi_ulong shmaddr)
2836{
2837 int i;
2838
2839 for (i = 0; i < N_SHM_REGIONS; ++i) {
2840 if (shm_regions[i].start == shmaddr) {
2841 shm_regions[i].start = 0;
e00ac249 2842 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
88a8c984
RV
2843 break;
2844 }
2845 }
2846
2847 return get_errno(shmdt(g2h(shmaddr)));
2848}
2849
1c54ff97 2850#ifdef TARGET_NR_ipc
53a5960a 2851/* ??? This only works with linear mappings. */
0da46a6e 2852/* do_ipc() must return target values and target errnos. */
992f48a0
BS
2853static abi_long do_ipc(unsigned int call, int first,
2854 int second, int third,
2855 abi_long ptr, abi_long fifth)
8853f86e
FB
2856{
2857 int version;
992f48a0 2858 abi_long ret = 0;
8853f86e
FB
2859
2860 version = call >> 16;
2861 call &= 0xffff;
2862
2863 switch (call) {
fa294816 2864 case IPCOP_semop:
e5289087 2865 ret = do_semop(first, ptr, second);
fa294816
TS
2866 break;
2867
2868 case IPCOP_semget:
2869 ret = get_errno(semget(first, second, third));
2870 break;
2871
2872 case IPCOP_semctl:
e5289087 2873 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
fa294816 2874 break;
d96372ef 2875
1c54ff97
AJ
2876 case IPCOP_msgget:
2877 ret = get_errno(msgget(first, second));
2878 break;
d96372ef 2879
1c54ff97
AJ
2880 case IPCOP_msgsnd:
2881 ret = do_msgsnd(first, ptr, second, third);
2882 break;
d96372ef 2883
1c54ff97
AJ
2884 case IPCOP_msgctl:
2885 ret = do_msgctl(first, second, ptr);
2886 break;
d96372ef 2887
1c54ff97
AJ
2888 case IPCOP_msgrcv:
2889 switch (version) {
2890 case 0:
2891 {
2892 struct target_ipc_kludge {
2893 abi_long msgp;
2894 abi_long msgtyp;
2895 } *tmp;
2896
2897 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2898 ret = -TARGET_EFAULT;
2899 break;
2900 }
d96372ef 2901
1c54ff97 2902 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
d96372ef 2903
1c54ff97
AJ
2904 unlock_user_struct(tmp, ptr, 0);
2905 break;
2906 }
2907 default:
2908 ret = do_msgrcv(first, ptr, second, fifth, third);
2909 }
2910 break;
d96372ef 2911
8853f86e 2912 case IPCOP_shmat:
88a8c984
RV
2913 switch (version) {
2914 default:
5a4a898d
FB
2915 {
2916 abi_ulong raddr;
88a8c984
RV
2917 raddr = do_shmat(first, ptr, second);
2918 if (is_error(raddr))
2919 return get_errno(raddr);
2f619698 2920 if (put_user_ual(raddr, third))
5a4a898d 2921 return -TARGET_EFAULT;
88a8c984
RV
2922 break;
2923 }
2924 case 1:
2925 ret = -TARGET_EINVAL;
2926 break;
5a4a898d 2927 }
8853f86e
FB
2928 break;
2929 case IPCOP_shmdt:
88a8c984 2930 ret = do_shmdt(ptr);
8853f86e
FB
2931 break;
2932
2933 case IPCOP_shmget:
2934 /* IPC_* flag values are the same on all linux platforms */
2935 ret = get_errno(shmget(first, second, third));
2936 break;
2937
2938 /* IPC_* and SHM_* command values are the same on all linux platforms */
2939 case IPCOP_shmctl:
88a8c984 2940 ret = do_shmctl(first, second, third);
8853f86e
FB
2941 break;
2942 default:
32407103 2943 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
0da46a6e 2944 ret = -TARGET_ENOSYS;
8853f86e
FB
2945 break;
2946 }
2947 return ret;
2948}
32407103 2949#endif
8853f86e 2950
31e31b8a
FB
2951/* kernel structure types definitions */
2952#define IFNAMSIZ 16
2953
001faf32 2954#define STRUCT(name, ...) STRUCT_ ## name,
31e31b8a
FB
2955#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2956enum {
2957#include "syscall_types.h"
2958};
2959#undef STRUCT
2960#undef STRUCT_SPECIAL
2961
001faf32 2962#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
31e31b8a
FB
2963#define STRUCT_SPECIAL(name)
2964#include "syscall_types.h"
2965#undef STRUCT
2966#undef STRUCT_SPECIAL
2967
d2ef05bb
PM
2968typedef struct IOCTLEntry IOCTLEntry;
2969
2970typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
2971 int fd, abi_long cmd, abi_long arg);
2972
2973struct IOCTLEntry {
2ab83ea7
FB
2974 unsigned int target_cmd;
2975 unsigned int host_cmd;
31e31b8a
FB
2976 const char *name;
2977 int access;
d2ef05bb 2978 do_ioctl_fn *do_ioctl;
1a9353d2 2979 const argtype arg_type[5];
d2ef05bb 2980};
31e31b8a
FB
2981
2982#define IOC_R 0x0001
2983#define IOC_W 0x0002
2984#define IOC_RW (IOC_R | IOC_W)
2985
2986#define MAX_STRUCT_SIZE 4096
2987
9f106a75 2988static IOCTLEntry ioctl_entries[] = {
001faf32 2989#define IOCTL(cmd, access, ...) \
d2ef05bb
PM
2990 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
2991#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
2992 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
31e31b8a
FB
2993#include "ioctls.h"
2994 { 0, 0, },
2995};
2996
53a5960a 2997/* ??? Implement proper locking for ioctls. */
0da46a6e 2998/* do_ioctl() Must return target values and target errnos. */
992f48a0 2999static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
31e31b8a
FB
3000{
3001 const IOCTLEntry *ie;
3002 const argtype *arg_type;
992f48a0 3003 abi_long ret;
31e31b8a 3004 uint8_t buf_temp[MAX_STRUCT_SIZE];
53a5960a
PB
3005 int target_size;
3006 void *argptr;
31e31b8a
FB
3007
3008 ie = ioctl_entries;
3009 for(;;) {
3010 if (ie->target_cmd == 0) {
32407103 3011 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
0da46a6e 3012 return -TARGET_ENOSYS;
31e31b8a
FB
3013 }
3014 if (ie->target_cmd == cmd)
3015 break;
3016 ie++;
3017 }
3018 arg_type = ie->arg_type;
9de5e440 3019#if defined(DEBUG)
32407103 3020 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
72f03900 3021#endif
d2ef05bb
PM
3022 if (ie->do_ioctl) {
3023 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3024 }
3025
31e31b8a
FB
3026 switch(arg_type[0]) {
3027 case TYPE_NULL:
3028 /* no argument */
3029 ret = get_errno(ioctl(fd, ie->host_cmd));
3030 break;
3031 case TYPE_PTRVOID:
3032 case TYPE_INT:
3033 /* int argment */
3034 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3035 break;
3036 case TYPE_PTR:
3037 arg_type++;
53a5960a 3038 target_size = thunk_type_size(arg_type, 0);
31e31b8a
FB
3039 switch(ie->access) {
3040 case IOC_R:
3041 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3042 if (!is_error(ret)) {
579a97f7
FB
3043 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3044 if (!argptr)
3045 return -TARGET_EFAULT;
53a5960a
PB
3046 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3047 unlock_user(argptr, arg, target_size);
31e31b8a
FB
3048 }
3049 break;
3050 case IOC_W:
579a97f7
FB
3051 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3052 if (!argptr)
3053 return -TARGET_EFAULT;
53a5960a
PB
3054 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3055 unlock_user(argptr, arg, 0);
31e31b8a
FB
3056 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3057 break;
3058 default:
3059 case IOC_RW:
579a97f7
FB
3060 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3061 if (!argptr)
3062 return -TARGET_EFAULT;
53a5960a
PB
3063 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3064 unlock_user(argptr, arg, 0);
31e31b8a
FB
3065 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3066 if (!is_error(ret)) {
579a97f7
FB
3067 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3068 if (!argptr)
3069 return -TARGET_EFAULT;
53a5960a
PB
3070 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3071 unlock_user(argptr, arg, target_size);
31e31b8a
FB
3072 }
3073 break;
3074 }
3075 break;
3076 default:
32407103
JM
3077 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3078 (long)cmd, arg_type[0]);
0da46a6e 3079 ret = -TARGET_ENOSYS;
31e31b8a
FB
3080 break;
3081 }
3082 return ret;
3083}
3084
b39bc503 3085static const bitmask_transtbl iflag_tbl[] = {
31e31b8a
FB
3086 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3087 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3088 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3089 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3090 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3091 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3092 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3093 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3094 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3095 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3096 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3097 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3098 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3099 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3100 { 0, 0, 0, 0 }
3101};
3102
b39bc503 3103static const bitmask_transtbl oflag_tbl[] = {
31e31b8a
FB
3104 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3105 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3106 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3107 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3108 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3109 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3110 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3111 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3112 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3113 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3114 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3115 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3116 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3117 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3118 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3119 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3120 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3121 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3122 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3123 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3124 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3125 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3126 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3127 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3128 { 0, 0, 0, 0 }
3129};
3130
b39bc503 3131static const bitmask_transtbl cflag_tbl[] = {
31e31b8a
FB
3132 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3133 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3134 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3135 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3136 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3137 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3138 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3139 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3140 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3141 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3142 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3143 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3144 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3145 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3146 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3147 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3148 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3149 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3150 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3151 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3152 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3153 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3154 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3155 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3156 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3157 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3158 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3159 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3160 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3161 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3162 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3163 { 0, 0, 0, 0 }
3164};
3165
b39bc503 3166static const bitmask_transtbl lflag_tbl[] = {
31e31b8a
FB
3167 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3168 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3169 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3170 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3171 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3172 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3173 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3174 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3175 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3176 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3177 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3178 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3179 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3180 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3181 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3182 { 0, 0, 0, 0 }
3183};
3184
3185static void target_to_host_termios (void *dst, const void *src)
3186{
3187 struct host_termios *host = dst;
3188 const struct target_termios *target = src;
3b46e624 3189
5fafdf24 3190 host->c_iflag =
31e31b8a 3191 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5fafdf24 3192 host->c_oflag =
31e31b8a 3193 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5fafdf24 3194 host->c_cflag =
31e31b8a 3195 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5fafdf24 3196 host->c_lflag =
31e31b8a
FB
3197 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3198 host->c_line = target->c_line;
3b46e624 3199
44607123 3200 memset(host->c_cc, 0, sizeof(host->c_cc));
5fafdf24
TS
3201 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3202 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3b46e624 3203 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
5fafdf24 3204 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3b46e624 3205 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
5fafdf24 3206 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3b46e624 3207 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
5fafdf24 3208 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3b46e624 3209 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
5fafdf24
TS
3210 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3211 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3b46e624
TS
3212 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3213 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3214 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3215 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3216 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
5fafdf24 3217 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
31e31b8a 3218}
3b46e624 3219
31e31b8a
FB
3220static void host_to_target_termios (void *dst, const void *src)
3221{
3222 struct target_termios *target = dst;
3223 const struct host_termios *host = src;
3224
5fafdf24 3225 target->c_iflag =
31e31b8a 3226 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5fafdf24 3227 target->c_oflag =
31e31b8a 3228 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5fafdf24 3229 target->c_cflag =
31e31b8a 3230 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5fafdf24 3231 target->c_lflag =
31e31b8a
FB
3232 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3233 target->c_line = host->c_line;
3b46e624 3234
44607123 3235 memset(target->c_cc, 0, sizeof(target->c_cc));
31e31b8a
FB
3236 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3237 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3238 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3239 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3240 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3241 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3242 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3243 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3244 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3245 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3246 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3247 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3248 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3249 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3250 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3251 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3252 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3253}
3254
8e853dc7 3255static const StructEntry struct_termios_def = {
31e31b8a
FB
3256 .convert = { host_to_target_termios, target_to_host_termios },
3257 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3258 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3259};
3260
5286db75
FB
3261static bitmask_transtbl mmap_flags_tbl[] = {
3262 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3263 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3264 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3265 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3266 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3267 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3268 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3269 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3270 { 0, 0, 0, 0 }
3271};
3272
2ab83ea7 3273#if defined(TARGET_I386)
6dbad63e
FB
3274
3275/* NOTE: there is really one LDT for all the threads */
b1d8e52e 3276static uint8_t *ldt_table;
6dbad63e 3277
03acab66 3278static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
6dbad63e
FB
3279{
3280 int size;
53a5960a 3281 void *p;
6dbad63e
FB
3282
3283 if (!ldt_table)
3284 return 0;
3285 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3286 if (size > bytecount)
3287 size = bytecount;
579a97f7
FB
3288 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3289 if (!p)
03acab66 3290 return -TARGET_EFAULT;
579a97f7 3291 /* ??? Should this by byteswapped? */
53a5960a
PB
3292 memcpy(p, ldt_table, size);
3293 unlock_user(p, ptr, size);
6dbad63e
FB
3294 return size;
3295}
3296
3297/* XXX: add locking support */
03acab66
FB
3298static abi_long write_ldt(CPUX86State *env,
3299 abi_ulong ptr, unsigned long bytecount, int oldmode)
6dbad63e
FB
3300{
3301 struct target_modify_ldt_ldt_s ldt_info;
53a5960a 3302 struct target_modify_ldt_ldt_s *target_ldt_info;
6dbad63e 3303 int seg_32bit, contents, read_exec_only, limit_in_pages;
8d18e893 3304 int seg_not_present, useable, lm;
6dbad63e
FB
3305 uint32_t *lp, entry_1, entry_2;
3306
3307 if (bytecount != sizeof(ldt_info))
03acab66 3308 return -TARGET_EINVAL;
579a97f7 3309 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
03acab66 3310 return -TARGET_EFAULT;
53a5960a
PB
3311 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3312 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3313 ldt_info.limit = tswap32(target_ldt_info->limit);
3314 ldt_info.flags = tswap32(target_ldt_info->flags);
3315 unlock_user_struct(target_ldt_info, ptr, 0);
3b46e624 3316
6dbad63e 3317 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
03acab66 3318 return -TARGET_EINVAL;
6dbad63e
FB
3319 seg_32bit = ldt_info.flags & 1;
3320 contents = (ldt_info.flags >> 1) & 3;
3321 read_exec_only = (ldt_info.flags >> 3) & 1;
3322 limit_in_pages = (ldt_info.flags >> 4) & 1;
3323 seg_not_present = (ldt_info.flags >> 5) & 1;
3324 useable = (ldt_info.flags >> 6) & 1;
8d18e893
FB
3325#ifdef TARGET_ABI32
3326 lm = 0;
3327#else
3328 lm = (ldt_info.flags >> 7) & 1;
3329#endif
6dbad63e
FB
3330 if (contents == 3) {
3331 if (oldmode)
03acab66 3332 return -TARGET_EINVAL;
6dbad63e 3333 if (seg_not_present == 0)
03acab66 3334 return -TARGET_EINVAL;
6dbad63e
FB
3335 }
3336 /* allocate the LDT */
3337 if (!ldt_table) {
e441570f
AZ
3338 env->ldt.base = target_mmap(0,
3339 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3340 PROT_READ|PROT_WRITE,
3341 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3342 if (env->ldt.base == -1)
03acab66 3343 return -TARGET_ENOMEM;
e441570f
AZ
3344 memset(g2h(env->ldt.base), 0,
3345 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
6dbad63e 3346 env->ldt.limit = 0xffff;
e441570f 3347 ldt_table = g2h(env->ldt.base);
6dbad63e
FB
3348 }
3349
3350 /* NOTE: same code as Linux kernel */
3351 /* Allow LDTs to be cleared by the user. */
3352 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3353 if (oldmode ||
3354 (contents == 0 &&
3355 read_exec_only == 1 &&
3356 seg_32bit == 0 &&
3357 limit_in_pages == 0 &&
3358 seg_not_present == 1 &&
3359 useable == 0 )) {
3360 entry_1 = 0;
3361 entry_2 = 0;
3362 goto install;
3363 }
3364 }
3b46e624 3365
6dbad63e
FB
3366 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3367 (ldt_info.limit & 0x0ffff);
3368 entry_2 = (ldt_info.base_addr & 0xff000000) |
3369 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3370 (ldt_info.limit & 0xf0000) |
3371 ((read_exec_only ^ 1) << 9) |
3372 (contents << 10) |
3373 ((seg_not_present ^ 1) << 15) |
3374 (seg_32bit << 22) |
3375 (limit_in_pages << 23) |
8d18e893 3376 (lm << 21) |
6dbad63e
FB
3377 0x7000;
3378 if (!oldmode)
3379 entry_2 |= (useable << 20);
14ae3ba7 3380
6dbad63e
FB
3381 /* Install the new entry ... */
3382install:
3383 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3384 lp[0] = tswap32(entry_1);
3385 lp[1] = tswap32(entry_2);
3386 return 0;
3387}
3388
3389/* specific and weird i386 syscalls */
8fcd3692
BS
3390static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3391 unsigned long bytecount)
6dbad63e 3392{
03acab66 3393 abi_long ret;
3b46e624 3394
6dbad63e
FB
3395 switch (func) {
3396 case 0:
3397 ret = read_ldt(ptr, bytecount);
3398 break;
3399 case 1:
3400 ret = write_ldt(env, ptr, bytecount, 1);
3401 break;
3402 case 0x11:
3403 ret = write_ldt(env, ptr, bytecount, 0);
3404 break;
03acab66
FB
3405 default:
3406 ret = -TARGET_ENOSYS;
3407 break;
6dbad63e
FB
3408 }
3409 return ret;
3410}
1b6b029e 3411
4583f589 3412#if defined(TARGET_I386) && defined(TARGET_ABI32)
8fcd3692 3413static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
3414{
3415 uint64_t *gdt_table = g2h(env->gdt.base);
3416 struct target_modify_ldt_ldt_s ldt_info;
3417 struct target_modify_ldt_ldt_s *target_ldt_info;
3418 int seg_32bit, contents, read_exec_only, limit_in_pages;
3419 int seg_not_present, useable, lm;
3420 uint32_t *lp, entry_1, entry_2;
3421 int i;
3422
3423 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3424 if (!target_ldt_info)
3425 return -TARGET_EFAULT;
3426 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3427 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3428 ldt_info.limit = tswap32(target_ldt_info->limit);
3429 ldt_info.flags = tswap32(target_ldt_info->flags);
3430 if (ldt_info.entry_number == -1) {
3431 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3432 if (gdt_table[i] == 0) {
3433 ldt_info.entry_number = i;
3434 target_ldt_info->entry_number = tswap32(i);
3435 break;
3436 }
3437 }
3438 }
3439 unlock_user_struct(target_ldt_info, ptr, 1);
3440
3441 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3442 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3443 return -TARGET_EINVAL;
3444 seg_32bit = ldt_info.flags & 1;
3445 contents = (ldt_info.flags >> 1) & 3;
3446 read_exec_only = (ldt_info.flags >> 3) & 1;
3447 limit_in_pages = (ldt_info.flags >> 4) & 1;
3448 seg_not_present = (ldt_info.flags >> 5) & 1;
3449 useable = (ldt_info.flags >> 6) & 1;
3450#ifdef TARGET_ABI32
3451 lm = 0;
3452#else
3453 lm = (ldt_info.flags >> 7) & 1;
3454#endif
3455
3456 if (contents == 3) {
3457 if (seg_not_present == 0)
3458 return -TARGET_EINVAL;
3459 }
3460
3461 /* NOTE: same code as Linux kernel */
3462 /* Allow LDTs to be cleared by the user. */
3463 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3464 if ((contents == 0 &&
3465 read_exec_only == 1 &&
3466 seg_32bit == 0 &&
3467 limit_in_pages == 0 &&
3468 seg_not_present == 1 &&
3469 useable == 0 )) {
3470 entry_1 = 0;
3471 entry_2 = 0;
3472 goto install;
3473 }
3474 }
3475
3476 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3477 (ldt_info.limit & 0x0ffff);
3478 entry_2 = (ldt_info.base_addr & 0xff000000) |
3479 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3480 (ldt_info.limit & 0xf0000) |
3481 ((read_exec_only ^ 1) << 9) |
3482 (contents << 10) |
3483 ((seg_not_present ^ 1) << 15) |
3484 (seg_32bit << 22) |
3485 (limit_in_pages << 23) |
3486 (useable << 20) |
3487 (lm << 21) |
3488 0x7000;
3489
3490 /* Install the new entry ... */
3491install:
3492 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3493 lp[0] = tswap32(entry_1);
3494 lp[1] = tswap32(entry_2);
3495 return 0;
3496}
3497
8fcd3692 3498static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
3499{
3500 struct target_modify_ldt_ldt_s *target_ldt_info;
3501 uint64_t *gdt_table = g2h(env->gdt.base);
3502 uint32_t base_addr, limit, flags;
3503 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3504 int seg_not_present, useable, lm;
3505 uint32_t *lp, entry_1, entry_2;
3506
3507 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3508 if (!target_ldt_info)
3509 return -TARGET_EFAULT;
3510 idx = tswap32(target_ldt_info->entry_number);
3511 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3512 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3513 unlock_user_struct(target_ldt_info, ptr, 1);
3514 return -TARGET_EINVAL;
3515 }
3516 lp = (uint32_t *)(gdt_table + idx);
3517 entry_1 = tswap32(lp[0]);
3518 entry_2 = tswap32(lp[1]);
3519
3520 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3521 contents = (entry_2 >> 10) & 3;
3522 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3523 seg_32bit = (entry_2 >> 22) & 1;
3524 limit_in_pages = (entry_2 >> 23) & 1;
3525 useable = (entry_2 >> 20) & 1;
3526#ifdef TARGET_ABI32
3527 lm = 0;
3528#else
3529 lm = (entry_2 >> 21) & 1;
3530#endif
3531 flags = (seg_32bit << 0) | (contents << 1) |
3532 (read_exec_only << 3) | (limit_in_pages << 4) |
3533 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3534 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3535 base_addr = (entry_1 >> 16) |
3536 (entry_2 & 0xff000000) |
3537 ((entry_2 & 0xff) << 16);
3538 target_ldt_info->base_addr = tswapl(base_addr);
3539 target_ldt_info->limit = tswap32(limit);
3540 target_ldt_info->flags = tswap32(flags);
3541 unlock_user_struct(target_ldt_info, ptr, 1);
3542 return 0;
3543}
4583f589 3544#endif /* TARGET_I386 && TARGET_ABI32 */
8d18e893 3545
d2fd1af7 3546#ifndef TARGET_ABI32
8fcd3692 3547static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
d2fd1af7
FB
3548{
3549 abi_long ret;
3550 abi_ulong val;
3551 int idx;
3552
3553 switch(code) {
3554 case TARGET_ARCH_SET_GS:
3555 case TARGET_ARCH_SET_FS:
3556 if (code == TARGET_ARCH_SET_GS)
3557 idx = R_GS;
3558 else
3559 idx = R_FS;
3560 cpu_x86_load_seg(env, idx, 0);
3561 env->segs[idx].base = addr;
3562 break;
3563 case TARGET_ARCH_GET_GS:
3564 case TARGET_ARCH_GET_FS:
3565 if (code == TARGET_ARCH_GET_GS)
3566 idx = R_GS;
3567 else
3568 idx = R_FS;
3569 val = env->segs[idx].base;
3570 if (put_user(val, addr, abi_ulong))
3571 return -TARGET_EFAULT;
3572 break;
3573 default:
3574 ret = -TARGET_EINVAL;
3575 break;
3576 }
3577 return 0;
3578}
3579#endif
3580
2ab83ea7
FB
3581#endif /* defined(TARGET_I386) */
3582
2f7bb878 3583#if defined(CONFIG_USE_NPTL)
d865bab5
PB
3584
3585#define NEW_STACK_SIZE PTHREAD_STACK_MIN
3586
3587static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3588typedef struct {
3589 CPUState *env;
3590 pthread_mutex_t mutex;
3591 pthread_cond_t cond;
3592 pthread_t thread;
3593 uint32_t tid;
3594 abi_ulong child_tidptr;
3595 abi_ulong parent_tidptr;
3596 sigset_t sigmask;
3597} new_thread_info;
3598
3599static void *clone_func(void *arg)
3600{
3601 new_thread_info *info = arg;
3602 CPUState *env;
edf8e2af 3603 TaskState *ts;
d865bab5
PB
3604
3605 env = info->env;
3606 thread_env = env;
edf8e2af 3607 ts = (TaskState *)thread_env->opaque;
d865bab5 3608 info->tid = gettid();
1e9fa730 3609 env->host_tid = info->tid;
edf8e2af 3610 task_settid(ts);
d865bab5
PB
3611 if (info->child_tidptr)
3612 put_user_u32(info->tid, info->child_tidptr);
3613 if (info->parent_tidptr)
3614 put_user_u32(info->tid, info->parent_tidptr);
3615 /* Enable signals. */
3616 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3617 /* Signal to the parent that we're ready. */
3618 pthread_mutex_lock(&info->mutex);
3619 pthread_cond_broadcast(&info->cond);
3620 pthread_mutex_unlock(&info->mutex);
3621 /* Wait until the parent has finshed initializing the tls state. */
3622 pthread_mutex_lock(&clone_lock);
3623 pthread_mutex_unlock(&clone_lock);
3624 cpu_loop(env);
3625 /* never exits */
3626 return NULL;
3627}
3628#else
1b6b029e
FB
3629/* this stack is the equivalent of the kernel stack associated with a
3630 thread/process */
3631#define NEW_STACK_SIZE 8192
3632
3633static int clone_func(void *arg)
3634{
2ab83ea7 3635 CPUState *env = arg;
1b6b029e
FB
3636 cpu_loop(env);
3637 /* never exits */
3638 return 0;
3639}
d865bab5 3640#endif
1b6b029e 3641
0da46a6e
TS
3642/* do_fork() Must return host values and target errnos (unlike most
3643 do_*() functions). */
d865bab5
PB
3644static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3645 abi_ulong parent_tidptr, target_ulong newtls,
3646 abi_ulong child_tidptr)
1b6b029e
FB
3647{
3648 int ret;
5cd4393b 3649 TaskState *ts;
2ab83ea7 3650 CPUState *new_env;
2f7bb878 3651#if defined(CONFIG_USE_NPTL)
d865bab5
PB
3652 unsigned int nptl_flags;
3653 sigset_t sigmask;
9190749f
RV
3654#else
3655 uint8_t *new_stack;
d865bab5 3656#endif
3b46e624 3657
436d124b
AZ
3658 /* Emulate vfork() with fork() */
3659 if (flags & CLONE_VFORK)
3660 flags &= ~(CLONE_VFORK | CLONE_VM);
3661
1b6b029e 3662 if (flags & CLONE_VM) {
edf8e2af 3663 TaskState *parent_ts = (TaskState *)env->opaque;
2f7bb878 3664#if defined(CONFIG_USE_NPTL)
d865bab5
PB
3665 new_thread_info info;
3666 pthread_attr_t attr;
bd0c5661 3667#endif
48e15fc2 3668 ts = qemu_mallocz(sizeof(TaskState));
624f7979 3669 init_task_state(ts);
1b6b029e 3670 /* we create a new CPU instance. */
c5be9f08 3671 new_env = cpu_copy(env);
b4558d74
BS
3672#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3673 cpu_reset(new_env);
3674#endif
6e68e076
PB
3675 /* Init regs that differ from the parent. */
3676 cpu_clone_regs(new_env, newsp);
5cd4393b 3677 new_env->opaque = ts;
edf8e2af
MW
3678 ts->bprm = parent_ts->bprm;
3679 ts->info = parent_ts->info;
2f7bb878 3680#if defined(CONFIG_USE_NPTL)
d865bab5
PB
3681 nptl_flags = flags;
3682 flags &= ~CLONE_NPTL_FLAGS2;
3683
c2764719
PB
3684 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3685 ts->child_tidptr = child_tidptr;
3686 }
3687
d865bab5
PB
3688 if (nptl_flags & CLONE_SETTLS)
3689 cpu_set_tls (new_env, newtls);
3690
3691 /* Grab a mutex so that thread setup appears atomic. */
3692 pthread_mutex_lock(&clone_lock);
3693
3694 memset(&info, 0, sizeof(info));
3695 pthread_mutex_init(&info.mutex, NULL);
3696 pthread_mutex_lock(&info.mutex);
3697 pthread_cond_init(&info.cond, NULL);
3698 info.env = new_env;
3699 if (nptl_flags & CLONE_CHILD_SETTID)
3700 info.child_tidptr = child_tidptr;
3701 if (nptl_flags & CLONE_PARENT_SETTID)
3702 info.parent_tidptr = parent_tidptr;
3703
3704 ret = pthread_attr_init(&attr);
48e15fc2
NF
3705 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
3706 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
d865bab5
PB
3707 /* It is not safe to deliver signals until the child has finished
3708 initializing, so temporarily block all signals. */
3709 sigfillset(&sigmask);
3710 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3711
3712 ret = pthread_create(&info.thread, &attr, clone_func, &info);
c2764719 3713 /* TODO: Free new CPU state if thread creation failed. */
d865bab5
PB
3714
3715 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3716 pthread_attr_destroy(&attr);
3717 if (ret == 0) {
3718 /* Wait for the child to initialize. */
3719 pthread_cond_wait(&info.cond, &info.mutex);
3720 ret = info.tid;
3721 if (flags & CLONE_PARENT_SETTID)
3722 put_user_u32(ret, parent_tidptr);
3723 } else {
3724 ret = -1;
3725 }
3726 pthread_mutex_unlock(&info.mutex);
3727 pthread_cond_destroy(&info.cond);
3728 pthread_mutex_destroy(&info.mutex);
3729 pthread_mutex_unlock(&clone_lock);
3730#else
3731 if (flags & CLONE_NPTL_FLAGS2)
3732 return -EINVAL;
3733 /* This is probably going to die very quickly, but do it anyway. */
48e15fc2 3734 new_stack = qemu_mallocz (NEW_STACK_SIZE);
27725c1d 3735#ifdef __ia64__
60e99246 3736 ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
27725c1d
FB
3737#else
3738 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
d865bab5 3739#endif
27725c1d 3740#endif
1b6b029e
FB
3741 } else {
3742 /* if no CLONE_VM, we consider it is a fork */
d865bab5 3743 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
1b6b029e 3744 return -EINVAL;
d865bab5 3745 fork_start();
1b6b029e 3746 ret = fork();
d865bab5 3747 if (ret == 0) {
2b1319c8 3748 /* Child Process. */
d865bab5
PB
3749 cpu_clone_regs(env, newsp);
3750 fork_end(1);
2f7bb878 3751#if defined(CONFIG_USE_NPTL)
2b1319c8
AJ
3752 /* There is a race condition here. The parent process could
3753 theoretically read the TID in the child process before the child
3754 tid is set. This would require using either ptrace
3755 (not implemented) or having *_tidptr to point at a shared memory
3756 mapping. We can't repeat the spinlock hack used above because
3757 the child process gets its own copy of the lock. */
d865bab5
PB
3758 if (flags & CLONE_CHILD_SETTID)
3759 put_user_u32(gettid(), child_tidptr);
3760 if (flags & CLONE_PARENT_SETTID)
3761 put_user_u32(gettid(), parent_tidptr);
3762 ts = (TaskState *)env->opaque;
3763 if (flags & CLONE_SETTLS)
3764 cpu_set_tls (env, newtls);
c2764719
PB
3765 if (flags & CLONE_CHILD_CLEARTID)
3766 ts->child_tidptr = child_tidptr;
2b1319c8 3767#endif
d865bab5
PB
3768 } else {
3769 fork_end(0);
3770 }
1b6b029e
FB
3771 }
3772 return ret;
3773}
3774
5f106811
APR
3775/* warning : doesn't handle linux specific flags... */
3776static int target_to_host_fcntl_cmd(int cmd)
3777{
3778 switch(cmd) {
3779 case TARGET_F_DUPFD:
3780 case TARGET_F_GETFD:
3781 case TARGET_F_SETFD:
3782 case TARGET_F_GETFL:
3783 case TARGET_F_SETFL:
3784 return cmd;
3785 case TARGET_F_GETLK:
3786 return F_GETLK;
3787 case TARGET_F_SETLK:
3788 return F_SETLK;
3789 case TARGET_F_SETLKW:
3790 return F_SETLKW;
3791 case TARGET_F_GETOWN:
3792 return F_GETOWN;
3793 case TARGET_F_SETOWN:
3794 return F_SETOWN;
3795 case TARGET_F_GETSIG:
3796 return F_GETSIG;
3797 case TARGET_F_SETSIG:
3798 return F_SETSIG;
3799#if TARGET_ABI_BITS == 32
3800 case TARGET_F_GETLK64:
3801 return F_GETLK64;
3802 case TARGET_F_SETLK64:
3803 return F_SETLK64;
3804 case TARGET_F_SETLKW64:
3805 return F_SETLKW64;
3806#endif
7e22e546
UH
3807 case TARGET_F_SETLEASE:
3808 return F_SETLEASE;
3809 case TARGET_F_GETLEASE:
3810 return F_GETLEASE;
fbd5de9b 3811#ifdef F_DUPFD_CLOEXEC
7e22e546
UH
3812 case TARGET_F_DUPFD_CLOEXEC:
3813 return F_DUPFD_CLOEXEC;
fbd5de9b 3814#endif
7e22e546
UH
3815 case TARGET_F_NOTIFY:
3816 return F_NOTIFY;
5f106811
APR
3817 default:
3818 return -TARGET_EINVAL;
3819 }
3820 return -TARGET_EINVAL;
3821}
3822
992f48a0 3823static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
7775e9ec
FB
3824{
3825 struct flock fl;
53a5960a 3826 struct target_flock *target_fl;
43f238d7
TS
3827 struct flock64 fl64;
3828 struct target_flock64 *target_fl64;
992f48a0 3829 abi_long ret;
5f106811
APR
3830 int host_cmd = target_to_host_fcntl_cmd(cmd);
3831
3832 if (host_cmd == -TARGET_EINVAL)
3833 return host_cmd;
53a5960a 3834
7775e9ec
FB
3835 switch(cmd) {
3836 case TARGET_F_GETLK:
579a97f7
FB
3837 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3838 return -TARGET_EFAULT;
5813427b
TS
3839 fl.l_type = tswap16(target_fl->l_type);
3840 fl.l_whence = tswap16(target_fl->l_whence);
3841 fl.l_start = tswapl(target_fl->l_start);
3842 fl.l_len = tswapl(target_fl->l_len);
7e22e546 3843 fl.l_pid = tswap32(target_fl->l_pid);
5813427b 3844 unlock_user_struct(target_fl, arg, 0);
5f106811 3845 ret = get_errno(fcntl(fd, host_cmd, &fl));
7775e9ec 3846 if (ret == 0) {
579a97f7
FB
3847 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3848 return -TARGET_EFAULT;
7775e9ec
FB
3849 target_fl->l_type = tswap16(fl.l_type);
3850 target_fl->l_whence = tswap16(fl.l_whence);
3851 target_fl->l_start = tswapl(fl.l_start);
3852 target_fl->l_len = tswapl(fl.l_len);
7e22e546 3853 target_fl->l_pid = tswap32(fl.l_pid);
53a5960a 3854 unlock_user_struct(target_fl, arg, 1);
7775e9ec
FB
3855 }
3856 break;
3b46e624 3857
7775e9ec
FB
3858 case TARGET_F_SETLK:
3859 case TARGET_F_SETLKW:
579a97f7
FB
3860 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3861 return -TARGET_EFAULT;
7775e9ec
FB
3862 fl.l_type = tswap16(target_fl->l_type);
3863 fl.l_whence = tswap16(target_fl->l_whence);
3864 fl.l_start = tswapl(target_fl->l_start);
3865 fl.l_len = tswapl(target_fl->l_len);
7e22e546 3866 fl.l_pid = tswap32(target_fl->l_pid);
53a5960a 3867 unlock_user_struct(target_fl, arg, 0);
5f106811 3868 ret = get_errno(fcntl(fd, host_cmd, &fl));
7775e9ec 3869 break;
3b46e624 3870
7775e9ec 3871 case TARGET_F_GETLK64:
579a97f7
FB
3872 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3873 return -TARGET_EFAULT;
5813427b
TS
3874 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3875 fl64.l_whence = tswap16(target_fl64->l_whence);
3876 fl64.l_start = tswapl(target_fl64->l_start);
3877 fl64.l_len = tswapl(target_fl64->l_len);
7e22e546 3878 fl64.l_pid = tswap32(target_fl64->l_pid);
5813427b 3879 unlock_user_struct(target_fl64, arg, 0);
5f106811 3880 ret = get_errno(fcntl(fd, host_cmd, &fl64));
43f238d7 3881 if (ret == 0) {
579a97f7
FB
3882 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3883 return -TARGET_EFAULT;
43f238d7
TS
3884 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3885 target_fl64->l_whence = tswap16(fl64.l_whence);
3886 target_fl64->l_start = tswapl(fl64.l_start);
3887 target_fl64->l_len = tswapl(fl64.l_len);
7e22e546 3888 target_fl64->l_pid = tswap32(fl64.l_pid);
43f238d7
TS
3889 unlock_user_struct(target_fl64, arg, 1);
3890 }
9ee1fa2c 3891 break;
7775e9ec
FB
3892 case TARGET_F_SETLK64:
3893 case TARGET_F_SETLKW64:
579a97f7
FB
3894 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3895 return -TARGET_EFAULT;
43f238d7
TS
3896 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3897 fl64.l_whence = tswap16(target_fl64->l_whence);
3898 fl64.l_start = tswapl(target_fl64->l_start);
3899 fl64.l_len = tswapl(target_fl64->l_len);
7e22e546 3900 fl64.l_pid = tswap32(target_fl64->l_pid);
43f238d7 3901 unlock_user_struct(target_fl64, arg, 0);
5f106811 3902 ret = get_errno(fcntl(fd, host_cmd, &fl64));
7775e9ec
FB
3903 break;
3904
5f106811
APR
3905 case TARGET_F_GETFL:
3906 ret = get_errno(fcntl(fd, host_cmd, arg));
9ee1fa2c
FB
3907 if (ret >= 0) {
3908 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3909 }
ffa65c3b
FB
3910 break;
3911
5f106811
APR
3912 case TARGET_F_SETFL:
3913 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3914 break;
3915
3916 case TARGET_F_SETOWN:
3917 case TARGET_F_GETOWN:
3918 case TARGET_F_SETSIG:
3919 case TARGET_F_GETSIG:
7e22e546
UH
3920 case TARGET_F_SETLEASE:
3921 case TARGET_F_GETLEASE:
5f106811 3922 ret = get_errno(fcntl(fd, host_cmd, arg));
ffa65c3b
FB
3923 break;
3924
7775e9ec 3925 default:
9ee1fa2c 3926 ret = get_errno(fcntl(fd, cmd, arg));
7775e9ec
FB
3927 break;
3928 }
3929 return ret;
3930}
3931
67867308 3932#ifdef USE_UID16
7775e9ec 3933
67867308
FB
3934static inline int high2lowuid(int uid)
3935{
3936 if (uid > 65535)
3937 return 65534;
3938 else
3939 return uid;
3940}
3941
3942static inline int high2lowgid(int gid)
3943{
3944 if (gid > 65535)
3945 return 65534;
3946 else
3947 return gid;
3948}
3949
3950static inline int low2highuid(int uid)
3951{
3952 if ((int16_t)uid == -1)
3953 return -1;
3954 else
3955 return uid;
3956}
3957
3958static inline int low2highgid(int gid)
3959{
3960 if ((int16_t)gid == -1)
3961 return -1;
3962 else
3963 return gid;
3964}
3965
3966#endif /* USE_UID16 */
1b6b029e 3967
31e31b8a
FB
3968void syscall_init(void)
3969{
2ab83ea7
FB
3970 IOCTLEntry *ie;
3971 const argtype *arg_type;
3972 int size;
b92c47c1 3973 int i;
2ab83ea7 3974
001faf32 3975#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
5fafdf24 3976#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
31e31b8a
FB
3977#include "syscall_types.h"
3978#undef STRUCT
3979#undef STRUCT_SPECIAL
2ab83ea7
FB
3980
3981 /* we patch the ioctl size if necessary. We rely on the fact that
3982 no ioctl has all the bits at '1' in the size field */
3983 ie = ioctl_entries;
3984 while (ie->target_cmd != 0) {
3985 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3986 TARGET_IOC_SIZEMASK) {
3987 arg_type = ie->arg_type;
3988 if (arg_type[0] != TYPE_PTR) {
5fafdf24 3989 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2ab83ea7
FB
3990 ie->target_cmd);
3991 exit(1);
3992 }
3993 arg_type++;
3994 size = thunk_type_size(arg_type, 0);
5fafdf24 3995 ie->target_cmd = (ie->target_cmd &
2ab83ea7
FB
3996 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3997 (size << TARGET_IOC_SIZESHIFT);
3998 }
b92c47c1
TS
3999
4000 /* Build target_to_host_errno_table[] table from
4001 * host_to_target_errno_table[]. */
4002 for (i=0; i < ERRNO_TABLE_SIZE; i++)
4003 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4004
2ab83ea7 4005 /* automatic consistency check if same arch */
872ea0c0
AZ
4006#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4007 (defined(__x86_64__) && defined(TARGET_X86_64))
4008 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4009 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4010 ie->name, ie->target_cmd, ie->host_cmd);
2ab83ea7
FB
4011 }
4012#endif
4013 ie++;
4014 }
31e31b8a 4015}
c573ff67 4016
992f48a0 4017#if TARGET_ABI_BITS == 32
ce4defa0
PB
4018static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4019{
af325d36 4020#ifdef TARGET_WORDS_BIGENDIAN
ce4defa0
PB
4021 return ((uint64_t)word0 << 32) | word1;
4022#else
4023 return ((uint64_t)word1 << 32) | word0;
4024#endif
4025}
992f48a0 4026#else /* TARGET_ABI_BITS == 32 */
32407103
JM
4027static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4028{
4029 return word0;
4030}
992f48a0 4031#endif /* TARGET_ABI_BITS != 32 */
ce4defa0
PB
4032
4033#ifdef TARGET_NR_truncate64
992f48a0
BS
4034static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4035 abi_long arg2,
4036 abi_long arg3,
4037 abi_long arg4)
ce4defa0
PB
4038{
4039#ifdef TARGET_ARM
4040 if (((CPUARMState *)cpu_env)->eabi)
4041 {
4042 arg2 = arg3;
4043 arg3 = arg4;
4044 }
4045#endif
4046 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4047}
4048#endif
4049
4050#ifdef TARGET_NR_ftruncate64
992f48a0
BS
4051static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4052 abi_long arg2,
4053 abi_long arg3,
4054 abi_long arg4)
ce4defa0
PB
4055{
4056#ifdef TARGET_ARM
4057 if (((CPUARMState *)cpu_env)->eabi)
4058 {
4059 arg2 = arg3;
4060 arg3 = arg4;
4061 }
4062#endif
4063 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4064}
4065#endif
4066
579a97f7
FB
4067static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4068 abi_ulong target_addr)
53a5960a
PB
4069{
4070 struct target_timespec *target_ts;
4071
579a97f7
FB
4072 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4073 return -TARGET_EFAULT;
53a5960a
PB
4074 host_ts->tv_sec = tswapl(target_ts->tv_sec);
4075 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
4076 unlock_user_struct(target_ts, target_addr, 0);
b255bfa8 4077 return 0;
53a5960a
PB
4078}
4079
579a97f7
FB
4080static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4081 struct timespec *host_ts)
53a5960a
PB
4082{
4083 struct target_timespec *target_ts;
4084
579a97f7
FB
4085 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4086 return -TARGET_EFAULT;
53a5960a
PB
4087 target_ts->tv_sec = tswapl(host_ts->tv_sec);
4088 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
4089 unlock_user_struct(target_ts, target_addr, 1);
b255bfa8 4090 return 0;
53a5960a
PB
4091}
4092
9d33b76b 4093#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
6a24a778
AZ
4094static inline abi_long host_to_target_stat64(void *cpu_env,
4095 abi_ulong target_addr,
4096 struct stat *host_st)
4097{
4098#ifdef TARGET_ARM
4099 if (((CPUARMState *)cpu_env)->eabi) {
4100 struct target_eabi_stat64 *target_st;
4101
4102 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4103 return -TARGET_EFAULT;
4104 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4105 __put_user(host_st->st_dev, &target_st->st_dev);
4106 __put_user(host_st->st_ino, &target_st->st_ino);
4107#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4108 __put_user(host_st->st_ino, &target_st->__st_ino);
4109#endif
4110 __put_user(host_st->st_mode, &target_st->st_mode);
4111 __put_user(host_st->st_nlink, &target_st->st_nlink);
4112 __put_user(host_st->st_uid, &target_st->st_uid);
4113 __put_user(host_st->st_gid, &target_st->st_gid);
4114 __put_user(host_st->st_rdev, &target_st->st_rdev);
4115 __put_user(host_st->st_size, &target_st->st_size);
4116 __put_user(host_st->st_blksize, &target_st->st_blksize);
4117 __put_user(host_st->st_blocks, &target_st->st_blocks);
4118 __put_user(host_st->st_atime, &target_st->target_st_atime);
4119 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4120 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4121 unlock_user_struct(target_st, target_addr, 1);
4122 } else
4123#endif
4124 {
ed18c5ce 4125#if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
9d33b76b
AJ
4126 struct target_stat *target_st;
4127#else
6a24a778 4128 struct target_stat64 *target_st;
9d33b76b 4129#endif
6a24a778
AZ
4130
4131 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4132 return -TARGET_EFAULT;
9d33b76b 4133 memset(target_st, 0, sizeof(*target_st));
6a24a778
AZ
4134 __put_user(host_st->st_dev, &target_st->st_dev);
4135 __put_user(host_st->st_ino, &target_st->st_ino);
4136#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4137 __put_user(host_st->st_ino, &target_st->__st_ino);
4138#endif
4139 __put_user(host_st->st_mode, &target_st->st_mode);
4140 __put_user(host_st->st_nlink, &target_st->st_nlink);
4141 __put_user(host_st->st_uid, &target_st->st_uid);
4142 __put_user(host_st->st_gid, &target_st->st_gid);
4143 __put_user(host_st->st_rdev, &target_st->st_rdev);
4144 /* XXX: better use of kernel struct */
4145 __put_user(host_st->st_size, &target_st->st_size);
4146 __put_user(host_st->st_blksize, &target_st->st_blksize);
4147 __put_user(host_st->st_blocks, &target_st->st_blocks);
4148 __put_user(host_st->st_atime, &target_st->target_st_atime);
4149 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4150 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4151 unlock_user_struct(target_st, target_addr, 1);
4152 }
4153
4154 return 0;
4155}
4156#endif
4157
2f7bb878 4158#if defined(CONFIG_USE_NPTL)
bd0c5661
PB
4159/* ??? Using host futex calls even when target atomic operations
4160 are not really atomic probably breaks things. However implementing
4161 futexes locally would make futexes shared between multiple processes
4162 tricky. However they're probably useless because guest atomic
4163 operations won't work either. */
8fcd3692
BS
4164static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4165 target_ulong uaddr2, int val3)
bd0c5661
PB
4166{
4167 struct timespec ts, *pts;
a16aae0c 4168 int base_op;
bd0c5661
PB
4169
4170 /* ??? We assume FUTEX_* constants are the same on both host
4171 and target. */
a29ccd63 4172#ifdef FUTEX_CMD_MASK
a16aae0c 4173 base_op = op & FUTEX_CMD_MASK;
a29ccd63 4174#else
a16aae0c 4175 base_op = op;
a29ccd63 4176#endif
a16aae0c 4177 switch (base_op) {
bd0c5661
PB
4178 case FUTEX_WAIT:
4179 if (timeout) {
4180 pts = &ts;
4181 target_to_host_timespec(pts, timeout);
4182 } else {
4183 pts = NULL;
4184 }
a29ccd63 4185 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
bd0c5661
PB
4186 pts, NULL, 0));
4187 case FUTEX_WAKE:
a29ccd63 4188 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
bd0c5661 4189 case FUTEX_FD:
a29ccd63 4190 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
bd0c5661 4191 case FUTEX_REQUEUE:
bd0c5661 4192 case FUTEX_CMP_REQUEUE:
a16aae0c
NF
4193 case FUTEX_WAKE_OP:
4194 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4195 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4196 But the prototype takes a `struct timespec *'; insert casts
4197 to satisfy the compiler. We do not need to tswap TIMEOUT
4198 since it's not compared to guest memory. */
4199 pts = (struct timespec *)(uintptr_t) timeout;
4200 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4201 g2h(uaddr2),
4202 (base_op == FUTEX_CMP_REQUEUE
4203 ? tswap32(val3)
4204 : val3)));
bd0c5661
PB
4205 default:
4206 return -TARGET_ENOSYS;
4207 }
4208}
4209#endif
4210
1d9d8b55
PB
4211/* Map host to target signal numbers for the wait family of syscalls.
4212 Assume all other status bits are the same. */
4213static int host_to_target_waitstatus(int status)
4214{
4215 if (WIFSIGNALED(status)) {
4216 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4217 }
4218 if (WIFSTOPPED(status)) {
4219 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4220 | (status & 0xff);
4221 }
4222 return status;
4223}
4224
a745ec6d
PB
4225int get_osversion(void)
4226{
4227 static int osversion;
4228 struct new_utsname buf;
4229 const char *s;
4230 int i, n, tmp;
4231 if (osversion)
4232 return osversion;
4233 if (qemu_uname_release && *qemu_uname_release) {
4234 s = qemu_uname_release;
4235 } else {
4236 if (sys_uname(&buf))
4237 return 0;
4238 s = buf.release;
4239 }
4240 tmp = 0;
4241 for (i = 0; i < 3; i++) {
4242 n = 0;
4243 while (*s >= '0' && *s <= '9') {
4244 n *= 10;
4245 n += *s - '0';
4246 s++;
4247 }
4248 tmp = (tmp << 8) + n;
4249 if (*s == '.')
4250 s++;
4251 }
4252 osversion = tmp;
4253 return osversion;
4254}
4255
0da46a6e
TS
4256/* do_syscall() should always have a single exit point at the end so
4257 that actions, such as logging of syscall results, can be performed.
4258 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
992f48a0
BS
4259abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4260 abi_long arg2, abi_long arg3, abi_long arg4,
4261 abi_long arg5, abi_long arg6)
31e31b8a 4262{
992f48a0 4263 abi_long ret;
31e31b8a 4264 struct stat st;
56c8f68f 4265 struct statfs stfs;
53a5960a 4266 void *p;
3b46e624 4267
72f03900 4268#ifdef DEBUG
c573ff67 4269 gemu_log("syscall %d", num);
72f03900 4270#endif
b92c47c1
TS
4271 if(do_strace)
4272 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4273
31e31b8a
FB
4274 switch(num) {
4275 case TARGET_NR_exit:
2f7bb878 4276#ifdef CONFIG_USE_NPTL
c2764719
PB
4277 /* In old applications this may be used to implement _exit(2).
4278 However in threaded applictions it is used for thread termination,
4279 and _exit_group is used for application termination.
4280 Do thread termination if we have more then one thread. */
4281 /* FIXME: This probably breaks if a signal arrives. We should probably
4282 be disabling signals. */
4283 if (first_cpu->next_cpu) {
1e9fa730 4284 TaskState *ts;
c2764719
PB
4285 CPUState **lastp;
4286 CPUState *p;
4287
4288 cpu_list_lock();
4289 lastp = &first_cpu;
4290 p = first_cpu;
4291 while (p && p != (CPUState *)cpu_env) {
4292 lastp = &p->next_cpu;
4293 p = p->next_cpu;
4294 }
4295 /* If we didn't find the CPU for this thread then something is
4296 horribly wrong. */
4297 if (!p)
4298 abort();
4299 /* Remove the CPU from the list. */
4300 *lastp = p->next_cpu;
4301 cpu_list_unlock();
1e9fa730 4302 ts = ((CPUState *)cpu_env)->opaque;
c2764719
PB
4303 if (ts->child_tidptr) {
4304 put_user_u32(0, ts->child_tidptr);
4305 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4306 NULL, NULL, 0);
4307 }
48e15fc2
NF
4308 thread_env = NULL;
4309 qemu_free(cpu_env);
4310 qemu_free(ts);
c2764719
PB
4311 pthread_exit(NULL);
4312 }
4313#endif
9788c9ca 4314#ifdef TARGET_GPROF
7d13299d
FB
4315 _mcleanup();
4316#endif
e9009676 4317 gdb_exit(cpu_env, arg1);
c2764719 4318 _exit(arg1);
31e31b8a
FB
4319 ret = 0; /* avoid warning */
4320 break;
4321 case TARGET_NR_read:
38d840e6
AJ
4322 if (arg3 == 0)
4323 ret = 0;
4324 else {
4325 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4326 goto efault;
4327 ret = get_errno(read(arg1, p, arg3));
4328 unlock_user(p, arg2, ret);
4329 }
31e31b8a
FB
4330 break;
4331 case TARGET_NR_write:
579a97f7
FB
4332 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4333 goto efault;
53a5960a
PB
4334 ret = get_errno(write(arg1, p, arg3));
4335 unlock_user(p, arg2, 0);
31e31b8a
FB
4336 break;
4337 case TARGET_NR_open:
2f619698
FB
4338 if (!(p = lock_user_string(arg1)))
4339 goto efault;
53a5960a 4340 ret = get_errno(open(path(p),
ffa65c3b
FB
4341 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4342 arg3));
53a5960a 4343 unlock_user(p, arg1, 0);
31e31b8a 4344 break;
82424832
TS
4345#if defined(TARGET_NR_openat) && defined(__NR_openat)
4346 case TARGET_NR_openat:
579a97f7
FB
4347 if (!(p = lock_user_string(arg2)))
4348 goto efault;
4349 ret = get_errno(sys_openat(arg1,
4350 path(p),
4351 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4352 arg4));
4353 unlock_user(p, arg2, 0);
82424832
TS
4354 break;
4355#endif
31e31b8a
FB
4356 case TARGET_NR_close:
4357 ret = get_errno(close(arg1));
4358 break;
4359 case TARGET_NR_brk:
53a5960a 4360 ret = do_brk(arg1);
31e31b8a
FB
4361 break;
4362 case TARGET_NR_fork:
d865bab5 4363 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
31e31b8a 4364 break;
e5febef5 4365#ifdef TARGET_NR_waitpid
31e31b8a
FB
4366 case TARGET_NR_waitpid:
4367 {
53a5960a
PB
4368 int status;
4369 ret = get_errno(waitpid(arg1, &status, arg3));
2f619698 4370 if (!is_error(ret) && arg2
1d9d8b55 4371 && put_user_s32(host_to_target_waitstatus(status), arg2))
2f619698 4372 goto efault;
31e31b8a
FB
4373 }
4374 break;
e5febef5 4375#endif
f0cbb613
PB
4376#ifdef TARGET_NR_waitid
4377 case TARGET_NR_waitid:
4378 {
4379 siginfo_t info;
4380 info.si_pid = 0;
4381 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4382 if (!is_error(ret) && arg3 && info.si_pid != 0) {
c227f099 4383 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
f0cbb613
PB
4384 goto efault;
4385 host_to_target_siginfo(p, &info);
c227f099 4386 unlock_user(p, arg3, sizeof(target_siginfo_t));
f0cbb613
PB
4387 }
4388 }
4389 break;
4390#endif
7a3148a9 4391#ifdef TARGET_NR_creat /* not on alpha */
31e31b8a 4392 case TARGET_NR_creat:
579a97f7
FB
4393 if (!(p = lock_user_string(arg1)))
4394 goto efault;
53a5960a
PB
4395 ret = get_errno(creat(p, arg2));
4396 unlock_user(p, arg1, 0);
31e31b8a 4397 break;
7a3148a9 4398#endif
31e31b8a 4399 case TARGET_NR_link:
53a5960a
PB
4400 {
4401 void * p2;
4402 p = lock_user_string(arg1);
4403 p2 = lock_user_string(arg2);
579a97f7
FB
4404 if (!p || !p2)
4405 ret = -TARGET_EFAULT;
4406 else
4407 ret = get_errno(link(p, p2));
53a5960a
PB
4408 unlock_user(p2, arg2, 0);
4409 unlock_user(p, arg1, 0);
4410 }
31e31b8a 4411 break;
64f0ce4c
TS
4412#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4413 case TARGET_NR_linkat:
64f0ce4c
TS
4414 {
4415 void * p2 = NULL;
579a97f7
FB
4416 if (!arg2 || !arg4)
4417 goto efault;
64f0ce4c
TS
4418 p = lock_user_string(arg2);
4419 p2 = lock_user_string(arg4);
579a97f7 4420 if (!p || !p2)
0da46a6e 4421 ret = -TARGET_EFAULT;
64f0ce4c
TS
4422 else
4423 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
579a97f7
FB
4424 unlock_user(p, arg2, 0);
4425 unlock_user(p2, arg4, 0);
64f0ce4c
TS
4426 }
4427 break;
4428#endif
31e31b8a 4429 case TARGET_NR_unlink:
579a97f7
FB
4430 if (!(p = lock_user_string(arg1)))
4431 goto efault;
53a5960a
PB
4432 ret = get_errno(unlink(p));
4433 unlock_user(p, arg1, 0);
31e31b8a 4434 break;
8170f56b
TS
4435#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4436 case TARGET_NR_unlinkat:
579a97f7
FB
4437 if (!(p = lock_user_string(arg2)))
4438 goto efault;
4439 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4440 unlock_user(p, arg2, 0);
ed494d87 4441 break;
b7d35e65 4442#endif
31e31b8a 4443 case TARGET_NR_execve:
7854b056
FB
4444 {
4445 char **argp, **envp;
f7341ff4 4446 int argc, envc;
992f48a0
BS
4447 abi_ulong gp;
4448 abi_ulong guest_argp;
4449 abi_ulong guest_envp;
4450 abi_ulong addr;
7854b056
FB
4451 char **q;
4452
f7341ff4 4453 argc = 0;
53a5960a 4454 guest_argp = arg2;
da94d263 4455 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
03aa1976 4456 if (get_user_ual(addr, gp))
2f619698 4457 goto efault;
03aa1976 4458 if (!addr)
2f619698 4459 break;
7854b056 4460 argc++;
2f619698 4461 }
f7341ff4 4462 envc = 0;
53a5960a 4463 guest_envp = arg3;
da94d263 4464 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
03aa1976 4465 if (get_user_ual(addr, gp))
2f619698 4466 goto efault;
03aa1976 4467 if (!addr)
2f619698 4468 break;
7854b056 4469 envc++;
2f619698 4470 }
7854b056 4471
f7341ff4
FB
4472 argp = alloca((argc + 1) * sizeof(void *));
4473 envp = alloca((envc + 1) * sizeof(void *));
7854b056 4474
da94d263 4475 for (gp = guest_argp, q = argp; gp;
992f48a0 4476 gp += sizeof(abi_ulong), q++) {
2f619698
FB
4477 if (get_user_ual(addr, gp))
4478 goto execve_efault;
53a5960a
PB
4479 if (!addr)
4480 break;
2f619698
FB
4481 if (!(*q = lock_user_string(addr)))
4482 goto execve_efault;
53a5960a 4483 }
f7341ff4
FB
4484 *q = NULL;
4485
da94d263 4486 for (gp = guest_envp, q = envp; gp;
992f48a0 4487 gp += sizeof(abi_ulong), q++) {
2f619698
FB
4488 if (get_user_ual(addr, gp))
4489 goto execve_efault;
53a5960a
PB
4490 if (!addr)
4491 break;
2f619698
FB
4492 if (!(*q = lock_user_string(addr)))
4493 goto execve_efault;
53a5960a 4494 }
f7341ff4 4495 *q = NULL;
7854b056 4496
2f619698
FB
4497 if (!(p = lock_user_string(arg1)))
4498 goto execve_efault;
53a5960a
PB
4499 ret = get_errno(execve(p, argp, envp));
4500 unlock_user(p, arg1, 0);
4501
2f619698
FB
4502 goto execve_end;
4503
4504 execve_efault:
4505 ret = -TARGET_EFAULT;
4506
4507 execve_end:
53a5960a 4508 for (gp = guest_argp, q = argp; *q;
992f48a0 4509 gp += sizeof(abi_ulong), q++) {
2f619698
FB
4510 if (get_user_ual(addr, gp)
4511 || !addr)
4512 break;
53a5960a
PB
4513 unlock_user(*q, addr, 0);
4514 }
4515 for (gp = guest_envp, q = envp; *q;
992f48a0 4516 gp += sizeof(abi_ulong), q++) {
2f619698
FB
4517 if (get_user_ual(addr, gp)
4518 || !addr)
4519 break;
53a5960a
PB
4520 unlock_user(*q, addr, 0);
4521 }
7854b056 4522 }
31e31b8a
FB
4523 break;
4524 case TARGET_NR_chdir:
579a97f7
FB
4525 if (!(p = lock_user_string(arg1)))
4526 goto efault;
53a5960a
PB
4527 ret = get_errno(chdir(p));
4528 unlock_user(p, arg1, 0);
31e31b8a 4529 break;
a315a145 4530#ifdef TARGET_NR_time
31e31b8a
FB
4531 case TARGET_NR_time:
4532 {
53a5960a
PB
4533 time_t host_time;
4534 ret = get_errno(time(&host_time));
2f619698
FB
4535 if (!is_error(ret)
4536 && arg1
4537 && put_user_sal(host_time, arg1))
4538 goto efault;
31e31b8a
FB
4539 }
4540 break;
a315a145 4541#endif
31e31b8a 4542 case TARGET_NR_mknod:
579a97f7
FB
4543 if (!(p = lock_user_string(arg1)))
4544 goto efault;
53a5960a
PB
4545 ret = get_errno(mknod(p, arg2, arg3));
4546 unlock_user(p, arg1, 0);
31e31b8a 4547 break;
75ac37a0
TS
4548#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4549 case TARGET_NR_mknodat:
579a97f7
FB
4550 if (!(p = lock_user_string(arg2)))
4551 goto efault;
4552 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4553 unlock_user(p, arg2, 0);
75ac37a0
TS
4554 break;
4555#endif
31e31b8a 4556 case TARGET_NR_chmod:
579a97f7
FB
4557 if (!(p = lock_user_string(arg1)))
4558 goto efault;
53a5960a
PB
4559 ret = get_errno(chmod(p, arg2));
4560 unlock_user(p, arg1, 0);
31e31b8a 4561 break;
ebc05488 4562#ifdef TARGET_NR_break
31e31b8a
FB
4563 case TARGET_NR_break:
4564 goto unimplemented;
ebc05488
FB
4565#endif
4566#ifdef TARGET_NR_oldstat
31e31b8a
FB
4567 case TARGET_NR_oldstat:
4568 goto unimplemented;
ebc05488 4569#endif
31e31b8a
FB
4570 case TARGET_NR_lseek:
4571 ret = get_errno(lseek(arg1, arg2, arg3));
4572 break;
9231733a
RH
4573#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
4574 /* Alpha specific */
7a3148a9 4575 case TARGET_NR_getxpid:
9231733a
RH
4576 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
4577 ret = get_errno(getpid());
4578 break;
7a3148a9 4579#endif
9231733a
RH
4580#ifdef TARGET_NR_getpid
4581 case TARGET_NR_getpid:
31e31b8a
FB
4582 ret = get_errno(getpid());
4583 break;
9231733a 4584#endif
31e31b8a 4585 case TARGET_NR_mount:
80265918
TS
4586 {
4587 /* need to look at the data field */
4588 void *p2, *p3;
4589 p = lock_user_string(arg1);
4590 p2 = lock_user_string(arg2);
4591 p3 = lock_user_string(arg3);
579a97f7
FB
4592 if (!p || !p2 || !p3)
4593 ret = -TARGET_EFAULT;
dab46405 4594 else {
579a97f7
FB
4595 /* FIXME - arg5 should be locked, but it isn't clear how to
4596 * do that since it's not guaranteed to be a NULL-terminated
4597 * string.
4598 */
dab46405
JSM
4599 if ( ! arg5 )
4600 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4601 else
4602 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4603 }
579a97f7
FB
4604 unlock_user(p, arg1, 0);
4605 unlock_user(p2, arg2, 0);
4606 unlock_user(p3, arg3, 0);
80265918
TS
4607 break;
4608 }
e5febef5 4609#ifdef TARGET_NR_umount
31e31b8a 4610 case TARGET_NR_umount:
579a97f7
FB
4611 if (!(p = lock_user_string(arg1)))
4612 goto efault;
53a5960a
PB
4613 ret = get_errno(umount(p));
4614 unlock_user(p, arg1, 0);
31e31b8a 4615 break;
e5febef5 4616#endif
7a3148a9 4617#ifdef TARGET_NR_stime /* not on alpha */
31e31b8a
FB
4618 case TARGET_NR_stime:
4619 {
53a5960a 4620 time_t host_time;
2f619698
FB
4621 if (get_user_sal(host_time, arg1))
4622 goto efault;
53a5960a 4623 ret = get_errno(stime(&host_time));
31e31b8a
FB
4624 }
4625 break;
7a3148a9 4626#endif
31e31b8a
FB
4627 case TARGET_NR_ptrace:
4628 goto unimplemented;
7a3148a9 4629#ifdef TARGET_NR_alarm /* not on alpha */
31e31b8a
FB
4630 case TARGET_NR_alarm:
4631 ret = alarm(arg1);
4632 break;
7a3148a9 4633#endif
ebc05488 4634#ifdef TARGET_NR_oldfstat
31e31b8a
FB
4635 case TARGET_NR_oldfstat:
4636 goto unimplemented;
ebc05488 4637#endif
7a3148a9 4638#ifdef TARGET_NR_pause /* not on alpha */
31e31b8a
FB
4639 case TARGET_NR_pause:
4640 ret = get_errno(pause());
4641 break;
7a3148a9 4642#endif
e5febef5 4643#ifdef TARGET_NR_utime
31e31b8a 4644 case TARGET_NR_utime:
ebc05488 4645 {
53a5960a
PB
4646 struct utimbuf tbuf, *host_tbuf;
4647 struct target_utimbuf *target_tbuf;
4648 if (arg2) {
579a97f7
FB
4649 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4650 goto efault;
53a5960a
PB
4651 tbuf.actime = tswapl(target_tbuf->actime);
4652 tbuf.modtime = tswapl(target_tbuf->modtime);
4653 unlock_user_struct(target_tbuf, arg2, 0);
4654 host_tbuf = &tbuf;
f72e8ff4 4655 } else {
53a5960a 4656 host_tbuf = NULL;
f72e8ff4 4657 }
579a97f7
FB
4658 if (!(p = lock_user_string(arg1)))
4659 goto efault;
53a5960a
PB
4660 ret = get_errno(utime(p, host_tbuf));
4661 unlock_user(p, arg1, 0);
ebc05488
FB
4662 }
4663 break;
e5febef5 4664#endif
978a66ff
FB
4665 case TARGET_NR_utimes:
4666 {
978a66ff 4667 struct timeval *tvp, tv[2];
53a5960a 4668 if (arg2) {
788f5ec4
TS
4669 if (copy_from_user_timeval(&tv[0], arg2)
4670 || copy_from_user_timeval(&tv[1],
4671 arg2 + sizeof(struct target_timeval)))
4672 goto efault;
978a66ff
FB
4673 tvp = tv;
4674 } else {
4675 tvp = NULL;
4676 }
579a97f7
FB
4677 if (!(p = lock_user_string(arg1)))
4678 goto efault;
53a5960a
PB
4679 ret = get_errno(utimes(p, tvp));
4680 unlock_user(p, arg1, 0);
978a66ff
FB
4681 }
4682 break;
ac8a6556
AZ
4683#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4684 case TARGET_NR_futimesat:
4685 {
4686 struct timeval *tvp, tv[2];
4687 if (arg3) {
4688 if (copy_from_user_timeval(&tv[0], arg3)
4689 || copy_from_user_timeval(&tv[1],
4690 arg3 + sizeof(struct target_timeval)))
4691 goto efault;
4692 tvp = tv;
4693 } else {
4694 tvp = NULL;
4695 }
4696 if (!(p = lock_user_string(arg2)))
4697 goto efault;
4698 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4699 unlock_user(p, arg2, 0);
4700 }
4701 break;
4702#endif
ebc05488 4703#ifdef TARGET_NR_stty
31e31b8a
FB
4704 case TARGET_NR_stty:
4705 goto unimplemented;
ebc05488
FB
4706#endif
4707#ifdef TARGET_NR_gtty
31e31b8a
FB
4708 case TARGET_NR_gtty:
4709 goto unimplemented;
ebc05488 4710#endif
31e31b8a 4711 case TARGET_NR_access:
579a97f7
FB
4712 if (!(p = lock_user_string(arg1)))
4713 goto efault;
719f908e 4714 ret = get_errno(access(path(p), arg2));
53a5960a 4715 unlock_user(p, arg1, 0);
31e31b8a 4716 break;
92a34c10
TS
4717#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4718 case TARGET_NR_faccessat:
579a97f7
FB
4719 if (!(p = lock_user_string(arg2)))
4720 goto efault;
465c9f06 4721 ret = get_errno(sys_faccessat(arg1, p, arg3));
579a97f7 4722 unlock_user(p, arg2, 0);
92a34c10
TS
4723 break;
4724#endif
7a3148a9 4725#ifdef TARGET_NR_nice /* not on alpha */
31e31b8a
FB
4726 case TARGET_NR_nice:
4727 ret = get_errno(nice(arg1));
4728 break;
7a3148a9 4729#endif
ebc05488 4730#ifdef TARGET_NR_ftime
31e31b8a
FB
4731 case TARGET_NR_ftime:
4732 goto unimplemented;
ebc05488 4733#endif
31e31b8a 4734 case TARGET_NR_sync:
04369ff2
FB
4735 sync();
4736 ret = 0;
31e31b8a
FB
4737 break;
4738 case TARGET_NR_kill:
4cb05961 4739 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
31e31b8a
FB
4740 break;
4741 case TARGET_NR_rename:
53a5960a
PB
4742 {
4743 void *p2;
4744 p = lock_user_string(arg1);
4745 p2 = lock_user_string(arg2);
579a97f7
FB
4746 if (!p || !p2)
4747 ret = -TARGET_EFAULT;
4748 else
4749 ret = get_errno(rename(p, p2));
53a5960a
PB
4750 unlock_user(p2, arg2, 0);
4751 unlock_user(p, arg1, 0);
4752 }
31e31b8a 4753 break;
722183f6
TS
4754#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4755 case TARGET_NR_renameat:
722183f6 4756 {
579a97f7 4757 void *p2;
722183f6
TS
4758 p = lock_user_string(arg2);
4759 p2 = lock_user_string(arg4);
579a97f7 4760 if (!p || !p2)
0da46a6e 4761 ret = -TARGET_EFAULT;
722183f6
TS
4762 else
4763 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
579a97f7
FB
4764 unlock_user(p2, arg4, 0);
4765 unlock_user(p, arg2, 0);
722183f6
TS
4766 }
4767 break;
4768#endif
31e31b8a 4769 case TARGET_NR_mkdir:
579a97f7
FB
4770 if (!(p = lock_user_string(arg1)))
4771 goto efault;
53a5960a
PB
4772 ret = get_errno(mkdir(p, arg2));
4773 unlock_user(p, arg1, 0);
31e31b8a 4774 break;
4472ad0d
TS
4775#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4776 case TARGET_NR_mkdirat:
579a97f7
FB
4777 if (!(p = lock_user_string(arg2)))
4778 goto efault;
4779 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4780 unlock_user(p, arg2, 0);
4472ad0d
TS
4781 break;
4782#endif
31e31b8a 4783 case TARGET_NR_rmdir:
579a97f7
FB
4784 if (!(p = lock_user_string(arg1)))
4785 goto efault;
53a5960a
PB
4786 ret = get_errno(rmdir(p));
4787 unlock_user(p, arg1, 0);
31e31b8a
FB
4788 break;
4789 case TARGET_NR_dup:
4790 ret = get_errno(dup(arg1));
4791 break;
4792 case TARGET_NR_pipe:
fb41a66e 4793 ret = do_pipe(cpu_env, arg1, 0, 0);
099d6b0f
RV
4794 break;
4795#ifdef TARGET_NR_pipe2
4796 case TARGET_NR_pipe2:
fb41a66e 4797 ret = do_pipe(cpu_env, arg1, arg2, 1);
31e31b8a 4798 break;
099d6b0f 4799#endif
31e31b8a 4800 case TARGET_NR_times:
32f36bce 4801 {
53a5960a 4802 struct target_tms *tmsp;
32f36bce
FB
4803 struct tms tms;
4804 ret = get_errno(times(&tms));
53a5960a 4805 if (arg1) {
579a97f7
FB
4806 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4807 if (!tmsp)
4808 goto efault;
c596ed17
FB
4809 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4810 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4811 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4812 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
32f36bce 4813 }
c596ed17
FB
4814 if (!is_error(ret))
4815 ret = host_to_target_clock_t(ret);
32f36bce
FB
4816 }
4817 break;
ebc05488 4818#ifdef TARGET_NR_prof
31e31b8a
FB
4819 case TARGET_NR_prof:
4820 goto unimplemented;
ebc05488 4821#endif
e5febef5 4822#ifdef TARGET_NR_signal
31e31b8a
FB
4823 case TARGET_NR_signal:
4824 goto unimplemented;
e5febef5 4825#endif
31e31b8a 4826 case TARGET_NR_acct:
38d840e6
AJ
4827 if (arg1 == 0) {
4828 ret = get_errno(acct(NULL));
4829 } else {
4830 if (!(p = lock_user_string(arg1)))
4831 goto efault;
4832 ret = get_errno(acct(path(p)));
4833 unlock_user(p, arg1, 0);
4834 }
24836689 4835 break;
7a3148a9 4836#ifdef TARGET_NR_umount2 /* not on alpha */
31e31b8a 4837 case TARGET_NR_umount2:
579a97f7
FB
4838 if (!(p = lock_user_string(arg1)))
4839 goto efault;
53a5960a
PB
4840 ret = get_errno(umount2(p, arg2));
4841 unlock_user(p, arg1, 0);
31e31b8a 4842 break;
7a3148a9 4843#endif
ebc05488 4844#ifdef TARGET_NR_lock
31e31b8a
FB
4845 case TARGET_NR_lock:
4846 goto unimplemented;
ebc05488 4847#endif
31e31b8a
FB
4848 case TARGET_NR_ioctl:
4849 ret = do_ioctl(arg1, arg2, arg3);
4850 break;
4851 case TARGET_NR_fcntl:
9ee1fa2c 4852 ret = do_fcntl(arg1, arg2, arg3);
31e31b8a 4853 break;
ebc05488 4854#ifdef TARGET_NR_mpx
31e31b8a
FB
4855 case TARGET_NR_mpx:
4856 goto unimplemented;
ebc05488 4857#endif
31e31b8a
FB
4858 case TARGET_NR_setpgid:
4859 ret = get_errno(setpgid(arg1, arg2));
4860 break;
ebc05488 4861#ifdef TARGET_NR_ulimit
31e31b8a
FB
4862 case TARGET_NR_ulimit:
4863 goto unimplemented;
ebc05488
FB
4864#endif
4865#ifdef TARGET_NR_oldolduname
31e31b8a
FB
4866 case TARGET_NR_oldolduname:
4867 goto unimplemented;
ebc05488 4868#endif
31e31b8a
FB
4869 case TARGET_NR_umask:
4870 ret = get_errno(umask(arg1));
4871 break;
4872 case TARGET_NR_chroot:
579a97f7
FB
4873 if (!(p = lock_user_string(arg1)))
4874 goto efault;
53a5960a
PB
4875 ret = get_errno(chroot(p));
4876 unlock_user(p, arg1, 0);
31e31b8a
FB
4877 break;
4878 case TARGET_NR_ustat:
4879 goto unimplemented;
4880 case TARGET_NR_dup2:
4881 ret = get_errno(dup2(arg1, arg2));
4882 break;
d0927938
UH
4883#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
4884 case TARGET_NR_dup3:
4885 ret = get_errno(dup3(arg1, arg2, arg3));
4886 break;
4887#endif
7a3148a9 4888#ifdef TARGET_NR_getppid /* not on alpha */
31e31b8a
FB
4889 case TARGET_NR_getppid:
4890 ret = get_errno(getppid());
4891 break;
7a3148a9 4892#endif
31e31b8a
FB
4893 case TARGET_NR_getpgrp:
4894 ret = get_errno(getpgrp());
4895 break;
4896 case TARGET_NR_setsid:
4897 ret = get_errno(setsid());
4898 break;
e5febef5 4899#ifdef TARGET_NR_sigaction
31e31b8a 4900 case TARGET_NR_sigaction:
31e31b8a 4901 {
6049f4f8
RH
4902#if defined(TARGET_ALPHA)
4903 struct target_sigaction act, oact, *pact = 0;
53a5960a 4904 struct target_old_sigaction *old_act;
53a5960a 4905 if (arg2) {
579a97f7
FB
4906 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4907 goto efault;
66fb9763
FB
4908 act._sa_handler = old_act->_sa_handler;
4909 target_siginitset(&act.sa_mask, old_act->sa_mask);
4910 act.sa_flags = old_act->sa_flags;
6049f4f8 4911 act.sa_restorer = 0;
53a5960a 4912 unlock_user_struct(old_act, arg2, 0);
66fb9763 4913 pact = &act;
66fb9763
FB
4914 }
4915 ret = get_errno(do_sigaction(arg1, pact, &oact));
53a5960a 4916 if (!is_error(ret) && arg3) {
579a97f7
FB
4917 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4918 goto efault;
53a5960a
PB
4919 old_act->_sa_handler = oact._sa_handler;
4920 old_act->sa_mask = oact.sa_mask.sig[0];
4921 old_act->sa_flags = oact.sa_flags;
53a5960a 4922 unlock_user_struct(old_act, arg3, 1);
66fb9763 4923 }
6049f4f8 4924#elif defined(TARGET_MIPS)
106ec879
FB
4925 struct target_sigaction act, oact, *pact, *old_act;
4926
4927 if (arg2) {
579a97f7
FB
4928 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4929 goto efault;
106ec879
FB
4930 act._sa_handler = old_act->_sa_handler;
4931 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4932 act.sa_flags = old_act->sa_flags;
4933 unlock_user_struct(old_act, arg2, 0);
4934 pact = &act;
4935 } else {
4936 pact = NULL;
4937 }
4938
4939 ret = get_errno(do_sigaction(arg1, pact, &oact));
4940
4941 if (!is_error(ret) && arg3) {
579a97f7
FB
4942 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4943 goto efault;
106ec879
FB
4944 old_act->_sa_handler = oact._sa_handler;
4945 old_act->sa_flags = oact.sa_flags;
4946 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4947 old_act->sa_mask.sig[1] = 0;
4948 old_act->sa_mask.sig[2] = 0;
4949 old_act->sa_mask.sig[3] = 0;
4950 unlock_user_struct(old_act, arg3, 1);
4951 }
6049f4f8
RH
4952#else
4953 struct target_old_sigaction *old_act;
4954 struct target_sigaction act, oact, *pact;
4955 if (arg2) {
4956 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4957 goto efault;
4958 act._sa_handler = old_act->_sa_handler;
4959 target_siginitset(&act.sa_mask, old_act->sa_mask);
4960 act.sa_flags = old_act->sa_flags;
4961 act.sa_restorer = old_act->sa_restorer;
4962 unlock_user_struct(old_act, arg2, 0);
4963 pact = &act;
4964 } else {
4965 pact = NULL;
4966 }
4967 ret = get_errno(do_sigaction(arg1, pact, &oact));
4968 if (!is_error(ret) && arg3) {
4969 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4970 goto efault;
4971 old_act->_sa_handler = oact._sa_handler;
4972 old_act->sa_mask = oact.sa_mask.sig[0];
4973 old_act->sa_flags = oact.sa_flags;
4974 old_act->sa_restorer = oact.sa_restorer;
4975 unlock_user_struct(old_act, arg3, 1);
4976 }
388bb21a 4977#endif
31e31b8a
FB
4978 }
4979 break;
e5febef5 4980#endif
66fb9763 4981 case TARGET_NR_rt_sigaction:
53a5960a 4982 {
6049f4f8
RH
4983#if defined(TARGET_ALPHA)
4984 struct target_sigaction act, oact, *pact = 0;
4985 struct target_rt_sigaction *rt_act;
4986 /* ??? arg4 == sizeof(sigset_t). */
4987 if (arg2) {
4988 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
4989 goto efault;
4990 act._sa_handler = rt_act->_sa_handler;
4991 act.sa_mask = rt_act->sa_mask;
4992 act.sa_flags = rt_act->sa_flags;
4993 act.sa_restorer = arg5;
4994 unlock_user_struct(rt_act, arg2, 0);
4995 pact = &act;
4996 }
4997 ret = get_errno(do_sigaction(arg1, pact, &oact));
4998 if (!is_error(ret) && arg3) {
4999 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5000 goto efault;
5001 rt_act->_sa_handler = oact._sa_handler;
5002 rt_act->sa_mask = oact.sa_mask;
5003 rt_act->sa_flags = oact.sa_flags;
5004 unlock_user_struct(rt_act, arg3, 1);
5005 }
5006#else
53a5960a
PB
5007 struct target_sigaction *act;
5008 struct target_sigaction *oact;
5009
579a97f7
FB
5010 if (arg2) {
5011 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5012 goto efault;
5013 } else
53a5960a 5014 act = NULL;
579a97f7
FB
5015 if (arg3) {
5016 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5017 ret = -TARGET_EFAULT;
5018 goto rt_sigaction_fail;
5019 }
5020 } else
53a5960a
PB
5021 oact = NULL;
5022 ret = get_errno(do_sigaction(arg1, act, oact));
579a97f7
FB
5023 rt_sigaction_fail:
5024 if (act)
53a5960a 5025 unlock_user_struct(act, arg2, 0);
579a97f7 5026 if (oact)
53a5960a 5027 unlock_user_struct(oact, arg3, 1);
6049f4f8 5028#endif
53a5960a 5029 }
66fb9763 5030 break;
7a3148a9 5031#ifdef TARGET_NR_sgetmask /* not on alpha */
31e31b8a 5032 case TARGET_NR_sgetmask:
66fb9763
FB
5033 {
5034 sigset_t cur_set;
992f48a0 5035 abi_ulong target_set;
66fb9763
FB
5036 sigprocmask(0, NULL, &cur_set);
5037 host_to_target_old_sigset(&target_set, &cur_set);
5038 ret = target_set;
5039 }
5040 break;
7a3148a9
JM
5041#endif
5042#ifdef TARGET_NR_ssetmask /* not on alpha */
31e31b8a 5043 case TARGET_NR_ssetmask:
66fb9763
FB
5044 {
5045 sigset_t set, oset, cur_set;
992f48a0 5046 abi_ulong target_set = arg1;
66fb9763
FB
5047 sigprocmask(0, NULL, &cur_set);
5048 target_to_host_old_sigset(&set, &target_set);
5049 sigorset(&set, &set, &cur_set);
5050 sigprocmask(SIG_SETMASK, &set, &oset);
5051 host_to_target_old_sigset(&target_set, &oset);
5052 ret = target_set;
5053 }
5054 break;
7a3148a9 5055#endif
e5febef5 5056#ifdef TARGET_NR_sigprocmask
66fb9763
FB
5057 case TARGET_NR_sigprocmask:
5058 {
a5b3b13b
RH
5059#if defined(TARGET_ALPHA)
5060 sigset_t set, oldset;
5061 abi_ulong mask;
5062 int how;
5063
5064 switch (arg1) {
5065 case TARGET_SIG_BLOCK:
5066 how = SIG_BLOCK;
5067 break;
5068 case TARGET_SIG_UNBLOCK:
5069 how = SIG_UNBLOCK;
5070 break;
5071 case TARGET_SIG_SETMASK:
5072 how = SIG_SETMASK;
5073 break;
5074 default:
5075 ret = -TARGET_EINVAL;
5076 goto fail;
5077 }
5078 mask = arg2;
5079 target_to_host_old_sigset(&set, &mask);
5080
5081 ret = get_errno(sigprocmask(how, &set, &oldset));
5082
5083 if (!is_error(ret)) {
5084 host_to_target_old_sigset(&mask, &oldset);
5085 ret = mask;
5086 ((CPUAlphaState *)cpu_env)->[IR_V0] = 0; /* force no error */
5087 }
5088#else
66fb9763 5089 sigset_t set, oldset, *set_ptr;
a5b3b13b 5090 int how;
3b46e624 5091
53a5960a 5092 if (arg2) {
a5b3b13b 5093 switch (arg1) {
66fb9763
FB
5094 case TARGET_SIG_BLOCK:
5095 how = SIG_BLOCK;
5096 break;
5097 case TARGET_SIG_UNBLOCK:
5098 how = SIG_UNBLOCK;
5099 break;
5100 case TARGET_SIG_SETMASK:
5101 how = SIG_SETMASK;
5102 break;
5103 default:
0da46a6e 5104 ret = -TARGET_EINVAL;
66fb9763
FB
5105 goto fail;
5106 }
c227f099 5107 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
579a97f7 5108 goto efault;
53a5960a
PB
5109 target_to_host_old_sigset(&set, p);
5110 unlock_user(p, arg2, 0);
66fb9763
FB
5111 set_ptr = &set;
5112 } else {
5113 how = 0;
5114 set_ptr = NULL;
5115 }
a5b3b13b 5116 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
53a5960a 5117 if (!is_error(ret) && arg3) {
c227f099 5118 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
579a97f7 5119 goto efault;
53a5960a 5120 host_to_target_old_sigset(p, &oldset);
c227f099 5121 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763 5122 }
a5b3b13b 5123#endif
66fb9763
FB
5124 }
5125 break;
e5febef5 5126#endif
66fb9763
FB
5127 case TARGET_NR_rt_sigprocmask:
5128 {
5129 int how = arg1;
5130 sigset_t set, oldset, *set_ptr;
3b46e624 5131
53a5960a 5132 if (arg2) {
66fb9763
FB
5133 switch(how) {
5134 case TARGET_SIG_BLOCK:
5135 how = SIG_BLOCK;
5136 break;
5137 case TARGET_SIG_UNBLOCK:
5138 how = SIG_UNBLOCK;
5139 break;
5140 case TARGET_SIG_SETMASK:
5141 how = SIG_SETMASK;
5142 break;
5143 default:
0da46a6e 5144 ret = -TARGET_EINVAL;
66fb9763
FB
5145 goto fail;
5146 }
c227f099 5147 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
579a97f7 5148 goto efault;
53a5960a
PB
5149 target_to_host_sigset(&set, p);
5150 unlock_user(p, arg2, 0);
66fb9763
FB
5151 set_ptr = &set;
5152 } else {
5153 how = 0;
5154 set_ptr = NULL;
5155 }
5156 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
53a5960a 5157 if (!is_error(ret) && arg3) {
c227f099 5158 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
579a97f7 5159 goto efault;
53a5960a 5160 host_to_target_sigset(p, &oldset);
c227f099 5161 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763
FB
5162 }
5163 }
5164 break;
e5febef5 5165#ifdef TARGET_NR_sigpending
66fb9763
FB
5166 case TARGET_NR_sigpending:
5167 {
5168 sigset_t set;
5169 ret = get_errno(sigpending(&set));
5170 if (!is_error(ret)) {
c227f099 5171 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
579a97f7 5172 goto efault;
53a5960a 5173 host_to_target_old_sigset(p, &set);
c227f099 5174 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
5175 }
5176 }
5177 break;
e5febef5 5178#endif
66fb9763
FB
5179 case TARGET_NR_rt_sigpending:
5180 {
5181 sigset_t set;
5182 ret = get_errno(sigpending(&set));
5183 if (!is_error(ret)) {
c227f099 5184 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
579a97f7 5185 goto efault;
53a5960a 5186 host_to_target_sigset(p, &set);
c227f099 5187 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
5188 }
5189 }
5190 break;
e5febef5 5191#ifdef TARGET_NR_sigsuspend
66fb9763
FB
5192 case TARGET_NR_sigsuspend:
5193 {
5194 sigset_t set;
f43ce12b
RH
5195#if defined(TARGET_ALPHA)
5196 abi_ulong mask = arg1;
5197 target_to_host_old_sigset(&set, &mask);
5198#else
c227f099 5199 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 5200 goto efault;
53a5960a
PB
5201 target_to_host_old_sigset(&set, p);
5202 unlock_user(p, arg1, 0);
f43ce12b 5203#endif
66fb9763
FB
5204 ret = get_errno(sigsuspend(&set));
5205 }
5206 break;
e5febef5 5207#endif
66fb9763
FB
5208 case TARGET_NR_rt_sigsuspend:
5209 {
5210 sigset_t set;
c227f099 5211 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 5212 goto efault;
53a5960a
PB
5213 target_to_host_sigset(&set, p);
5214 unlock_user(p, arg1, 0);
66fb9763
FB
5215 ret = get_errno(sigsuspend(&set));
5216 }
5217 break;
5218 case TARGET_NR_rt_sigtimedwait:
5219 {
66fb9763
FB
5220 sigset_t set;
5221 struct timespec uts, *puts;
5222 siginfo_t uinfo;
3b46e624 5223
c227f099 5224 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 5225 goto efault;
53a5960a
PB
5226 target_to_host_sigset(&set, p);
5227 unlock_user(p, arg1, 0);
5228 if (arg3) {
66fb9763 5229 puts = &uts;
53a5960a 5230 target_to_host_timespec(puts, arg3);
66fb9763
FB
5231 } else {
5232 puts = NULL;
5233 }
5234 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
53a5960a 5235 if (!is_error(ret) && arg2) {
c227f099 5236 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
579a97f7 5237 goto efault;
53a5960a 5238 host_to_target_siginfo(p, &uinfo);
c227f099 5239 unlock_user(p, arg2, sizeof(target_siginfo_t));
66fb9763
FB
5240 }
5241 }
5242 break;
5243 case TARGET_NR_rt_sigqueueinfo:
5244 {
5245 siginfo_t uinfo;
c227f099 5246 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
579a97f7 5247 goto efault;
53a5960a
PB
5248 target_to_host_siginfo(&uinfo, p);
5249 unlock_user(p, arg1, 0);
66fb9763
FB
5250 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5251 }
5252 break;
e5febef5 5253#ifdef TARGET_NR_sigreturn
66fb9763
FB
5254 case TARGET_NR_sigreturn:
5255 /* NOTE: ret is eax, so not transcoding must be done */
5256 ret = do_sigreturn(cpu_env);
5257 break;
e5febef5 5258#endif
66fb9763
FB
5259 case TARGET_NR_rt_sigreturn:
5260 /* NOTE: ret is eax, so not transcoding must be done */
5261 ret = do_rt_sigreturn(cpu_env);
5262 break;
31e31b8a 5263 case TARGET_NR_sethostname:
579a97f7
FB
5264 if (!(p = lock_user_string(arg1)))
5265 goto efault;
53a5960a
PB
5266 ret = get_errno(sethostname(p, arg2));
5267 unlock_user(p, arg1, 0);
31e31b8a
FB
5268 break;
5269 case TARGET_NR_setrlimit:
9de5e440 5270 {
9de5e440 5271 int resource = arg1;
53a5960a 5272 struct target_rlimit *target_rlim;
9de5e440 5273 struct rlimit rlim;
579a97f7
FB
5274 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5275 goto efault;
81bbe906
TY
5276 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5277 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
53a5960a 5278 unlock_user_struct(target_rlim, arg2, 0);
9de5e440
FB
5279 ret = get_errno(setrlimit(resource, &rlim));
5280 }
5281 break;
31e31b8a 5282 case TARGET_NR_getrlimit:
9de5e440 5283 {
9de5e440 5284 int resource = arg1;
53a5960a 5285 struct target_rlimit *target_rlim;
9de5e440 5286 struct rlimit rlim;
3b46e624 5287
9de5e440
FB
5288 ret = get_errno(getrlimit(resource, &rlim));
5289 if (!is_error(ret)) {
579a97f7
FB
5290 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5291 goto efault;
81bbe906
TY
5292 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5293 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 5294 unlock_user_struct(target_rlim, arg2, 1);
9de5e440
FB
5295 }
5296 }
5297 break;
31e31b8a 5298 case TARGET_NR_getrusage:
b409186b
FB
5299 {
5300 struct rusage rusage;
b409186b
FB
5301 ret = get_errno(getrusage(arg1, &rusage));
5302 if (!is_error(ret)) {
53a5960a 5303 host_to_target_rusage(arg2, &rusage);
b409186b
FB
5304 }
5305 }
5306 break;
31e31b8a
FB
5307 case TARGET_NR_gettimeofday:
5308 {
31e31b8a
FB
5309 struct timeval tv;
5310 ret = get_errno(gettimeofday(&tv, NULL));
5311 if (!is_error(ret)) {
788f5ec4
TS
5312 if (copy_to_user_timeval(arg1, &tv))
5313 goto efault;
31e31b8a
FB
5314 }
5315 }
5316 break;
5317 case TARGET_NR_settimeofday:
5318 {
31e31b8a 5319 struct timeval tv;
788f5ec4
TS
5320 if (copy_from_user_timeval(&tv, arg1))
5321 goto efault;
31e31b8a
FB
5322 ret = get_errno(settimeofday(&tv, NULL));
5323 }
5324 break;
048f6b4d 5325#ifdef TARGET_NR_select
31e31b8a 5326 case TARGET_NR_select:
f2674e31 5327 {
53a5960a 5328 struct target_sel_arg_struct *sel;
992f48a0 5329 abi_ulong inp, outp, exp, tvp;
53a5960a
PB
5330 long nsel;
5331
579a97f7
FB
5332 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5333 goto efault;
53a5960a
PB
5334 nsel = tswapl(sel->n);
5335 inp = tswapl(sel->inp);
5336 outp = tswapl(sel->outp);
5337 exp = tswapl(sel->exp);
5338 tvp = tswapl(sel->tvp);
5339 unlock_user_struct(sel, arg1, 0);
5340 ret = do_select(nsel, inp, outp, exp, tvp);
f2674e31
FB
5341 }
5342 break;
9e42382f
RV
5343#endif
5344#ifdef TARGET_NR_pselect6
5345 case TARGET_NR_pselect6:
5346 goto unimplemented_nowarn;
048f6b4d 5347#endif
31e31b8a 5348 case TARGET_NR_symlink:
53a5960a
PB
5349 {
5350 void *p2;
5351 p = lock_user_string(arg1);
5352 p2 = lock_user_string(arg2);
579a97f7
FB
5353 if (!p || !p2)
5354 ret = -TARGET_EFAULT;
5355 else
5356 ret = get_errno(symlink(p, p2));
53a5960a
PB
5357 unlock_user(p2, arg2, 0);
5358 unlock_user(p, arg1, 0);
5359 }
31e31b8a 5360 break;
f0b6243d
TS
5361#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5362 case TARGET_NR_symlinkat:
f0b6243d 5363 {
579a97f7 5364 void *p2;
f0b6243d
TS
5365 p = lock_user_string(arg1);
5366 p2 = lock_user_string(arg3);
579a97f7 5367 if (!p || !p2)
0da46a6e 5368 ret = -TARGET_EFAULT;
f0b6243d
TS
5369 else
5370 ret = get_errno(sys_symlinkat(p, arg2, p2));
579a97f7
FB
5371 unlock_user(p2, arg3, 0);
5372 unlock_user(p, arg1, 0);
f0b6243d
TS
5373 }
5374 break;
5375#endif
ebc05488 5376#ifdef TARGET_NR_oldlstat
31e31b8a
FB
5377 case TARGET_NR_oldlstat:
5378 goto unimplemented;
ebc05488 5379#endif
31e31b8a 5380 case TARGET_NR_readlink:
53a5960a 5381 {
d088d664 5382 void *p2, *temp;
53a5960a 5383 p = lock_user_string(arg1);
579a97f7
FB
5384 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5385 if (!p || !p2)
5386 ret = -TARGET_EFAULT;
d088d664
AJ
5387 else {
5388 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5389 char real[PATH_MAX];
5390 temp = realpath(exec_path,real);
5391 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5392 snprintf((char *)p2, arg3, "%s", real);
5393 }
5394 else
5395 ret = get_errno(readlink(path(p), p2, arg3));
d088d664 5396 }
53a5960a
PB
5397 unlock_user(p2, arg2, ret);
5398 unlock_user(p, arg1, 0);
5399 }
31e31b8a 5400 break;
5e0ccb18
TS
5401#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5402 case TARGET_NR_readlinkat:
5e0ccb18 5403 {
579a97f7 5404 void *p2;
5e0ccb18 5405 p = lock_user_string(arg2);
579a97f7
FB
5406 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5407 if (!p || !p2)
0da46a6e 5408 ret = -TARGET_EFAULT;
5e0ccb18
TS
5409 else
5410 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
579a97f7
FB
5411 unlock_user(p2, arg3, ret);
5412 unlock_user(p, arg2, 0);
5e0ccb18
TS
5413 }
5414 break;
5415#endif
e5febef5 5416#ifdef TARGET_NR_uselib
31e31b8a
FB
5417 case TARGET_NR_uselib:
5418 goto unimplemented;
e5febef5
TS
5419#endif
5420#ifdef TARGET_NR_swapon
31e31b8a 5421 case TARGET_NR_swapon:
579a97f7
FB
5422 if (!(p = lock_user_string(arg1)))
5423 goto efault;
53a5960a
PB
5424 ret = get_errno(swapon(p, arg2));
5425 unlock_user(p, arg1, 0);
31e31b8a 5426 break;
e5febef5 5427#endif
31e31b8a
FB
5428 case TARGET_NR_reboot:
5429 goto unimplemented;
e5febef5 5430#ifdef TARGET_NR_readdir
31e31b8a
FB
5431 case TARGET_NR_readdir:
5432 goto unimplemented;
e5febef5
TS
5433#endif
5434#ifdef TARGET_NR_mmap
31e31b8a 5435 case TARGET_NR_mmap:
b779e29e 5436#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE)
31e31b8a 5437 {
992f48a0
BS
5438 abi_ulong *v;
5439 abi_ulong v1, v2, v3, v4, v5, v6;
579a97f7
FB
5440 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5441 goto efault;
53a5960a
PB
5442 v1 = tswapl(v[0]);
5443 v2 = tswapl(v[1]);
5444 v3 = tswapl(v[2]);
5445 v4 = tswapl(v[3]);
5446 v5 = tswapl(v[4]);
5447 v6 = tswapl(v[5]);
5448 unlock_user(v, arg1, 0);
5fafdf24 5449 ret = get_errno(target_mmap(v1, v2, v3,
5286db75
FB
5450 target_to_host_bitmask(v4, mmap_flags_tbl),
5451 v5, v6));
31e31b8a 5452 }
31e31b8a 5453#else
5fafdf24
TS
5454 ret = get_errno(target_mmap(arg1, arg2, arg3,
5455 target_to_host_bitmask(arg4, mmap_flags_tbl),
6fb883e8
FB
5456 arg5,
5457 arg6));
31e31b8a 5458#endif
6fb883e8 5459 break;
e5febef5 5460#endif
a315a145 5461#ifdef TARGET_NR_mmap2
6fb883e8 5462 case TARGET_NR_mmap2:
bb7ec043 5463#ifndef MMAP_SHIFT
c573ff67 5464#define MMAP_SHIFT 12
c573ff67 5465#endif
5fafdf24
TS
5466 ret = get_errno(target_mmap(arg1, arg2, arg3,
5467 target_to_host_bitmask(arg4, mmap_flags_tbl),
5286db75 5468 arg5,
c573ff67 5469 arg6 << MMAP_SHIFT));
31e31b8a 5470 break;
a315a145 5471#endif
31e31b8a 5472 case TARGET_NR_munmap:
54936004 5473 ret = get_errno(target_munmap(arg1, arg2));
31e31b8a 5474 break;
9de5e440 5475 case TARGET_NR_mprotect:
97374d38
PB
5476 {
5477 TaskState *ts = ((CPUState *)cpu_env)->opaque;
5478 /* Special hack to detect libc making the stack executable. */
5479 if ((arg3 & PROT_GROWSDOWN)
5480 && arg1 >= ts->info->stack_limit
5481 && arg1 <= ts->info->start_stack) {
5482 arg3 &= ~PROT_GROWSDOWN;
5483 arg2 = arg2 + arg1 - ts->info->stack_limit;
5484 arg1 = ts->info->stack_limit;
5485 }
5486 }
54936004 5487 ret = get_errno(target_mprotect(arg1, arg2, arg3));
9de5e440 5488 break;
e5febef5 5489#ifdef TARGET_NR_mremap
9de5e440 5490 case TARGET_NR_mremap:
54936004 5491 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
9de5e440 5492 break;
e5febef5 5493#endif
53a5960a 5494 /* ??? msync/mlock/munlock are broken for softmmu. */
e5febef5 5495#ifdef TARGET_NR_msync
9de5e440 5496 case TARGET_NR_msync:
53a5960a 5497 ret = get_errno(msync(g2h(arg1), arg2, arg3));
9de5e440 5498 break;
e5febef5
TS
5499#endif
5500#ifdef TARGET_NR_mlock
9de5e440 5501 case TARGET_NR_mlock:
53a5960a 5502 ret = get_errno(mlock(g2h(arg1), arg2));
9de5e440 5503 break;
e5febef5
TS
5504#endif
5505#ifdef TARGET_NR_munlock
9de5e440 5506 case TARGET_NR_munlock:
53a5960a 5507 ret = get_errno(munlock(g2h(arg1), arg2));
9de5e440 5508 break;
e5febef5
TS
5509#endif
5510#ifdef TARGET_NR_mlockall
9de5e440
FB
5511 case TARGET_NR_mlockall:
5512 ret = get_errno(mlockall(arg1));
5513 break;
e5febef5
TS
5514#endif
5515#ifdef TARGET_NR_munlockall
9de5e440
FB
5516 case TARGET_NR_munlockall:
5517 ret = get_errno(munlockall());
5518 break;
e5febef5 5519#endif
31e31b8a 5520 case TARGET_NR_truncate:
579a97f7
FB
5521 if (!(p = lock_user_string(arg1)))
5522 goto efault;
53a5960a
PB
5523 ret = get_errno(truncate(p, arg2));
5524 unlock_user(p, arg1, 0);
31e31b8a
FB
5525 break;
5526 case TARGET_NR_ftruncate:
5527 ret = get_errno(ftruncate(arg1, arg2));
5528 break;
5529 case TARGET_NR_fchmod:
5530 ret = get_errno(fchmod(arg1, arg2));
5531 break;
814d7977
TS
5532#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5533 case TARGET_NR_fchmodat:
579a97f7
FB
5534 if (!(p = lock_user_string(arg2)))
5535 goto efault;
465c9f06 5536 ret = get_errno(sys_fchmodat(arg1, p, arg3));
579a97f7 5537 unlock_user(p, arg2, 0);
814d7977
TS
5538 break;
5539#endif
31e31b8a 5540 case TARGET_NR_getpriority:
c6cda17a
TS
5541 /* libc does special remapping of the return value of
5542 * sys_getpriority() so it's just easiest to call
5543 * sys_getpriority() directly rather than through libc. */
69137206 5544 ret = get_errno(sys_getpriority(arg1, arg2));
31e31b8a
FB
5545 break;
5546 case TARGET_NR_setpriority:
5547 ret = get_errno(setpriority(arg1, arg2, arg3));
5548 break;
ebc05488 5549#ifdef TARGET_NR_profil
31e31b8a
FB
5550 case TARGET_NR_profil:
5551 goto unimplemented;
ebc05488 5552#endif
31e31b8a 5553 case TARGET_NR_statfs:
579a97f7
FB
5554 if (!(p = lock_user_string(arg1)))
5555 goto efault;
53a5960a
PB
5556 ret = get_errno(statfs(path(p), &stfs));
5557 unlock_user(p, arg1, 0);
31e31b8a
FB
5558 convert_statfs:
5559 if (!is_error(ret)) {
53a5960a 5560 struct target_statfs *target_stfs;
3b46e624 5561
579a97f7
FB
5562 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5563 goto efault;
5564 __put_user(stfs.f_type, &target_stfs->f_type);
5565 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5566 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5567 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5568 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5569 __put_user(stfs.f_files, &target_stfs->f_files);
5570 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5571 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5572 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5573 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
53a5960a 5574 unlock_user_struct(target_stfs, arg2, 1);
31e31b8a
FB
5575 }
5576 break;
5577 case TARGET_NR_fstatfs:
56c8f68f 5578 ret = get_errno(fstatfs(arg1, &stfs));
31e31b8a 5579 goto convert_statfs;
56c8f68f
FB
5580#ifdef TARGET_NR_statfs64
5581 case TARGET_NR_statfs64:
579a97f7
FB
5582 if (!(p = lock_user_string(arg1)))
5583 goto efault;
53a5960a
PB
5584 ret = get_errno(statfs(path(p), &stfs));
5585 unlock_user(p, arg1, 0);
56c8f68f
FB
5586 convert_statfs64:
5587 if (!is_error(ret)) {
53a5960a 5588 struct target_statfs64 *target_stfs;
3b46e624 5589
579a97f7
FB
5590 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5591 goto efault;
5592 __put_user(stfs.f_type, &target_stfs->f_type);
5593 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5594 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5595 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5596 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5597 __put_user(stfs.f_files, &target_stfs->f_files);
5598 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5599 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5600 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5601 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5602 unlock_user_struct(target_stfs, arg3, 1);
56c8f68f
FB
5603 }
5604 break;
5605 case TARGET_NR_fstatfs64:
5606 ret = get_errno(fstatfs(arg1, &stfs));
5607 goto convert_statfs64;
5608#endif
ebc05488 5609#ifdef TARGET_NR_ioperm
31e31b8a
FB
5610 case TARGET_NR_ioperm:
5611 goto unimplemented;
ebc05488 5612#endif
e5febef5 5613#ifdef TARGET_NR_socketcall
31e31b8a 5614 case TARGET_NR_socketcall:
53a5960a 5615 ret = do_socketcall(arg1, arg2);
31e31b8a 5616 break;
e5febef5 5617#endif
3532fa74
FB
5618#ifdef TARGET_NR_accept
5619 case TARGET_NR_accept:
1be9e1dc 5620 ret = do_accept(arg1, arg2, arg3);
3532fa74
FB
5621 break;
5622#endif
5623#ifdef TARGET_NR_bind
5624 case TARGET_NR_bind:
5625 ret = do_bind(arg1, arg2, arg3);
5626 break;
5627#endif
5628#ifdef TARGET_NR_connect
5629 case TARGET_NR_connect:
5630 ret = do_connect(arg1, arg2, arg3);
5631 break;
5632#endif
5633#ifdef TARGET_NR_getpeername
5634 case TARGET_NR_getpeername:
1be9e1dc 5635 ret = do_getpeername(arg1, arg2, arg3);
3532fa74
FB
5636 break;
5637#endif
5638#ifdef TARGET_NR_getsockname
5639 case TARGET_NR_getsockname:
1be9e1dc 5640 ret = do_getsockname(arg1, arg2, arg3);
3532fa74
FB
5641 break;
5642#endif
5643#ifdef TARGET_NR_getsockopt
5644 case TARGET_NR_getsockopt:
5645 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5646 break;
5647#endif
5648#ifdef TARGET_NR_listen
5649 case TARGET_NR_listen:
1be9e1dc 5650 ret = get_errno(listen(arg1, arg2));
3532fa74
FB
5651 break;
5652#endif
5653#ifdef TARGET_NR_recv
5654 case TARGET_NR_recv:
214201bd 5655 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
5656 break;
5657#endif
5658#ifdef TARGET_NR_recvfrom
5659 case TARGET_NR_recvfrom:
214201bd 5660 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
5661 break;
5662#endif
5663#ifdef TARGET_NR_recvmsg
5664 case TARGET_NR_recvmsg:
5665 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5666 break;
5667#endif
5668#ifdef TARGET_NR_send
5669 case TARGET_NR_send:
1be9e1dc 5670 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
5671 break;
5672#endif
5673#ifdef TARGET_NR_sendmsg
5674 case TARGET_NR_sendmsg:
5675 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5676 break;
5677#endif
5678#ifdef TARGET_NR_sendto
5679 case TARGET_NR_sendto:
1be9e1dc 5680 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
5681 break;
5682#endif
5683#ifdef TARGET_NR_shutdown
5684 case TARGET_NR_shutdown:
1be9e1dc 5685 ret = get_errno(shutdown(arg1, arg2));
3532fa74
FB
5686 break;
5687#endif
5688#ifdef TARGET_NR_socket
5689 case TARGET_NR_socket:
5690 ret = do_socket(arg1, arg2, arg3);
5691 break;
5692#endif
5693#ifdef TARGET_NR_socketpair
5694 case TARGET_NR_socketpair:
1be9e1dc 5695 ret = do_socketpair(arg1, arg2, arg3, arg4);
3532fa74
FB
5696 break;
5697#endif
5698#ifdef TARGET_NR_setsockopt
5699 case TARGET_NR_setsockopt:
5700 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5701 break;
5702#endif
7494b0f9 5703
31e31b8a 5704 case TARGET_NR_syslog:
579a97f7
FB
5705 if (!(p = lock_user_string(arg2)))
5706 goto efault;
e5574487
TS
5707 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5708 unlock_user(p, arg2, 0);
7494b0f9
TS
5709 break;
5710
31e31b8a 5711 case TARGET_NR_setitimer:
66fb9763 5712 {
66fb9763
FB
5713 struct itimerval value, ovalue, *pvalue;
5714
53a5960a 5715 if (arg2) {
66fb9763 5716 pvalue = &value;
788f5ec4
TS
5717 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5718 || copy_from_user_timeval(&pvalue->it_value,
5719 arg2 + sizeof(struct target_timeval)))
5720 goto efault;
66fb9763
FB
5721 } else {
5722 pvalue = NULL;
5723 }
5724 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
53a5960a 5725 if (!is_error(ret) && arg3) {
788f5ec4
TS
5726 if (copy_to_user_timeval(arg3,
5727 &ovalue.it_interval)
5728 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5729 &ovalue.it_value))
5730 goto efault;
66fb9763
FB
5731 }
5732 }
5733 break;
31e31b8a 5734 case TARGET_NR_getitimer:
66fb9763 5735 {
66fb9763 5736 struct itimerval value;
3b46e624 5737
66fb9763 5738 ret = get_errno(getitimer(arg1, &value));
53a5960a 5739 if (!is_error(ret) && arg2) {
788f5ec4
TS
5740 if (copy_to_user_timeval(arg2,
5741 &value.it_interval)
5742 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5743 &value.it_value))
5744 goto efault;
66fb9763
FB
5745 }
5746 }
5747 break;
31e31b8a 5748 case TARGET_NR_stat:
579a97f7
FB
5749 if (!(p = lock_user_string(arg1)))
5750 goto efault;
53a5960a
PB
5751 ret = get_errno(stat(path(p), &st));
5752 unlock_user(p, arg1, 0);
31e31b8a
FB
5753 goto do_stat;
5754 case TARGET_NR_lstat:
579a97f7
FB
5755 if (!(p = lock_user_string(arg1)))
5756 goto efault;
53a5960a
PB
5757 ret = get_errno(lstat(path(p), &st));
5758 unlock_user(p, arg1, 0);
31e31b8a
FB
5759 goto do_stat;
5760 case TARGET_NR_fstat:
5761 {
5762 ret = get_errno(fstat(arg1, &st));
5763 do_stat:
5764 if (!is_error(ret)) {
53a5960a 5765 struct target_stat *target_st;
e3584658 5766
579a97f7
FB
5767 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5768 goto efault;
12727917 5769 memset(target_st, 0, sizeof(*target_st));
d2fd1af7
FB
5770 __put_user(st.st_dev, &target_st->st_dev);
5771 __put_user(st.st_ino, &target_st->st_ino);
5772 __put_user(st.st_mode, &target_st->st_mode);
5773 __put_user(st.st_uid, &target_st->st_uid);
5774 __put_user(st.st_gid, &target_st->st_gid);
5775 __put_user(st.st_nlink, &target_st->st_nlink);
5776 __put_user(st.st_rdev, &target_st->st_rdev);
5777 __put_user(st.st_size, &target_st->st_size);
5778 __put_user(st.st_blksize, &target_st->st_blksize);
5779 __put_user(st.st_blocks, &target_st->st_blocks);
5780 __put_user(st.st_atime, &target_st->target_st_atime);
5781 __put_user(st.st_mtime, &target_st->target_st_mtime);
5782 __put_user(st.st_ctime, &target_st->target_st_ctime);
53a5960a 5783 unlock_user_struct(target_st, arg2, 1);
31e31b8a
FB
5784 }
5785 }
5786 break;
ebc05488 5787#ifdef TARGET_NR_olduname
31e31b8a
FB
5788 case TARGET_NR_olduname:
5789 goto unimplemented;
ebc05488
FB
5790#endif
5791#ifdef TARGET_NR_iopl
31e31b8a
FB
5792 case TARGET_NR_iopl:
5793 goto unimplemented;
ebc05488 5794#endif
31e31b8a
FB
5795 case TARGET_NR_vhangup:
5796 ret = get_errno(vhangup());
5797 break;
ebc05488 5798#ifdef TARGET_NR_idle
31e31b8a
FB
5799 case TARGET_NR_idle:
5800 goto unimplemented;
42ad6ae9
FB
5801#endif
5802#ifdef TARGET_NR_syscall
5803 case TARGET_NR_syscall:
5804 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5805 break;
ebc05488 5806#endif
31e31b8a
FB
5807 case TARGET_NR_wait4:
5808 {
5809 int status;
992f48a0 5810 abi_long status_ptr = arg2;
31e31b8a 5811 struct rusage rusage, *rusage_ptr;
992f48a0 5812 abi_ulong target_rusage = arg4;
31e31b8a
FB
5813 if (target_rusage)
5814 rusage_ptr = &rusage;
5815 else
5816 rusage_ptr = NULL;
5817 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5818 if (!is_error(ret)) {
2f619698 5819 if (status_ptr) {
1d9d8b55 5820 status = host_to_target_waitstatus(status);
2f619698
FB
5821 if (put_user_s32(status, status_ptr))
5822 goto efault;
31e31b8a 5823 }
2f619698
FB
5824 if (target_rusage)
5825 host_to_target_rusage(target_rusage, &rusage);
31e31b8a
FB
5826 }
5827 }
5828 break;
e5febef5 5829#ifdef TARGET_NR_swapoff
31e31b8a 5830 case TARGET_NR_swapoff:
579a97f7
FB
5831 if (!(p = lock_user_string(arg1)))
5832 goto efault;
53a5960a
PB
5833 ret = get_errno(swapoff(p));
5834 unlock_user(p, arg1, 0);
31e31b8a 5835 break;
e5febef5 5836#endif
31e31b8a 5837 case TARGET_NR_sysinfo:
a5448a7d 5838 {
53a5960a 5839 struct target_sysinfo *target_value;
a5448a7d
FB
5840 struct sysinfo value;
5841 ret = get_errno(sysinfo(&value));
53a5960a 5842 if (!is_error(ret) && arg1)
a5448a7d 5843 {
579a97f7
FB
5844 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5845 goto efault;
a5448a7d
FB
5846 __put_user(value.uptime, &target_value->uptime);
5847 __put_user(value.loads[0], &target_value->loads[0]);
5848 __put_user(value.loads[1], &target_value->loads[1]);
5849 __put_user(value.loads[2], &target_value->loads[2]);
5850 __put_user(value.totalram, &target_value->totalram);
5851 __put_user(value.freeram, &target_value->freeram);
5852 __put_user(value.sharedram, &target_value->sharedram);
5853 __put_user(value.bufferram, &target_value->bufferram);
5854 __put_user(value.totalswap, &target_value->totalswap);
5855 __put_user(value.freeswap, &target_value->freeswap);
5856 __put_user(value.procs, &target_value->procs);
5857 __put_user(value.totalhigh, &target_value->totalhigh);
5858 __put_user(value.freehigh, &target_value->freehigh);
5859 __put_user(value.mem_unit, &target_value->mem_unit);
53a5960a 5860 unlock_user_struct(target_value, arg1, 1);
a5448a7d
FB
5861 }
5862 }
5863 break;
e5febef5 5864#ifdef TARGET_NR_ipc
31e31b8a 5865 case TARGET_NR_ipc:
8853f86e
FB
5866 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5867 break;
e5febef5 5868#endif
e5289087
AJ
5869#ifdef TARGET_NR_semget
5870 case TARGET_NR_semget:
5871 ret = get_errno(semget(arg1, arg2, arg3));
5872 break;
5873#endif
5874#ifdef TARGET_NR_semop
5875 case TARGET_NR_semop:
5876 ret = get_errno(do_semop(arg1, arg2, arg3));
5877 break;
5878#endif
5879#ifdef TARGET_NR_semctl
5880 case TARGET_NR_semctl:
5881 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5882 break;
5883#endif
eeb438c1
AJ
5884#ifdef TARGET_NR_msgctl
5885 case TARGET_NR_msgctl:
5886 ret = do_msgctl(arg1, arg2, arg3);
5887 break;
5888#endif
5889#ifdef TARGET_NR_msgget
5890 case TARGET_NR_msgget:
5891 ret = get_errno(msgget(arg1, arg2));
5892 break;
5893#endif
5894#ifdef TARGET_NR_msgrcv
5895 case TARGET_NR_msgrcv:
5896 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5897 break;
5898#endif
5899#ifdef TARGET_NR_msgsnd
5900 case TARGET_NR_msgsnd:
5901 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5902 break;
88a8c984
RV
5903#endif
5904#ifdef TARGET_NR_shmget
5905 case TARGET_NR_shmget:
5906 ret = get_errno(shmget(arg1, arg2, arg3));
5907 break;
5908#endif
5909#ifdef TARGET_NR_shmctl
5910 case TARGET_NR_shmctl:
5911 ret = do_shmctl(arg1, arg2, arg3);
5912 break;
5913#endif
5914#ifdef TARGET_NR_shmat
5915 case TARGET_NR_shmat:
5916 ret = do_shmat(arg1, arg2, arg3);
5917 break;
5918#endif
5919#ifdef TARGET_NR_shmdt
5920 case TARGET_NR_shmdt:
5921 ret = do_shmdt(arg1);
5922 break;
eeb438c1 5923#endif
31e31b8a
FB
5924 case TARGET_NR_fsync:
5925 ret = get_errno(fsync(arg1));
5926 break;
31e31b8a 5927 case TARGET_NR_clone:
a4b388ff 5928#if defined(TARGET_SH4) || defined(TARGET_ALPHA)
0b6d3ae0 5929 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
b15ad61c
EI
5930#elif defined(TARGET_CRIS)
5931 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
0b6d3ae0 5932#else
d865bab5 5933 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
0b6d3ae0 5934#endif
1b6b029e 5935 break;
ec86b0fb
FB
5936#ifdef __NR_exit_group
5937 /* new thread calls */
5938 case TARGET_NR_exit_group:
9788c9ca 5939#ifdef TARGET_GPROF
6d946cda
AJ
5940 _mcleanup();
5941#endif
e9009676 5942 gdb_exit(cpu_env, arg1);
ec86b0fb
FB
5943 ret = get_errno(exit_group(arg1));
5944 break;
5945#endif
31e31b8a 5946 case TARGET_NR_setdomainname:
579a97f7
FB
5947 if (!(p = lock_user_string(arg1)))
5948 goto efault;
53a5960a
PB
5949 ret = get_errno(setdomainname(p, arg2));
5950 unlock_user(p, arg1, 0);
31e31b8a
FB
5951 break;
5952 case TARGET_NR_uname:
5953 /* no need to transcode because we use the linux syscall */
29e619b1
FB
5954 {
5955 struct new_utsname * buf;
3b46e624 5956
579a97f7
FB
5957 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5958 goto efault;
29e619b1
FB
5959 ret = get_errno(sys_uname(buf));
5960 if (!is_error(ret)) {
5961 /* Overrite the native machine name with whatever is being
5962 emulated. */
da79030f 5963 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
c5937220
PB
5964 /* Allow the user to override the reported release. */
5965 if (qemu_uname_release && *qemu_uname_release)
5966 strcpy (buf->release, qemu_uname_release);
29e619b1 5967 }
53a5960a 5968 unlock_user_struct(buf, arg1, 1);
29e619b1 5969 }
31e31b8a 5970 break;
6dbad63e 5971#ifdef TARGET_I386
31e31b8a 5972 case TARGET_NR_modify_ldt:
03acab66 5973 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5cd4393b 5974 break;
84409ddb 5975#if !defined(TARGET_X86_64)
5cd4393b
FB
5976 case TARGET_NR_vm86old:
5977 goto unimplemented;
5978 case TARGET_NR_vm86:
53a5960a 5979 ret = do_vm86(cpu_env, arg1, arg2);
6dbad63e 5980 break;
84409ddb 5981#endif
6dbad63e 5982#endif
31e31b8a
FB
5983 case TARGET_NR_adjtimex:
5984 goto unimplemented;
e5febef5 5985#ifdef TARGET_NR_create_module
31e31b8a 5986 case TARGET_NR_create_module:
e5febef5 5987#endif
31e31b8a
FB
5988 case TARGET_NR_init_module:
5989 case TARGET_NR_delete_module:
e5febef5 5990#ifdef TARGET_NR_get_kernel_syms
31e31b8a 5991 case TARGET_NR_get_kernel_syms:
e5febef5 5992#endif
31e31b8a
FB
5993 goto unimplemented;
5994 case TARGET_NR_quotactl:
5995 goto unimplemented;
5996 case TARGET_NR_getpgid:
5997 ret = get_errno(getpgid(arg1));
5998 break;
5999 case TARGET_NR_fchdir:
6000 ret = get_errno(fchdir(arg1));
6001 break;
84409ddb 6002#ifdef TARGET_NR_bdflush /* not on x86_64 */
31e31b8a
FB
6003 case TARGET_NR_bdflush:
6004 goto unimplemented;
84409ddb 6005#endif
e5febef5 6006#ifdef TARGET_NR_sysfs
31e31b8a
FB
6007 case TARGET_NR_sysfs:
6008 goto unimplemented;
e5febef5 6009#endif
31e31b8a 6010 case TARGET_NR_personality:
1b6b029e 6011 ret = get_errno(personality(arg1));
31e31b8a 6012 break;
e5febef5 6013#ifdef TARGET_NR_afs_syscall
31e31b8a
FB
6014 case TARGET_NR_afs_syscall:
6015 goto unimplemented;
e5febef5 6016#endif
7a3148a9 6017#ifdef TARGET_NR__llseek /* Not on alpha */
31e31b8a
FB
6018 case TARGET_NR__llseek:
6019 {
d35b261c 6020#if !defined(__NR_llseek)
4f2ac237 6021 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
2f619698
FB
6022 if (put_user_s64(ret, arg4))
6023 goto efault;
4f2ac237 6024#else
31e31b8a
FB
6025 int64_t res;
6026 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
2f619698
FB
6027 if (put_user_s64(res, arg4))
6028 goto efault;
4f2ac237 6029#endif
31e31b8a
FB
6030 }
6031 break;
7a3148a9 6032#endif
31e31b8a 6033 case TARGET_NR_getdents:
d83c8733 6034#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4add45b4 6035 {
53a5960a 6036 struct target_dirent *target_dirp;
6556a833 6037 struct linux_dirent *dirp;
992f48a0 6038 abi_long count = arg3;
4add45b4
FB
6039
6040 dirp = malloc(count);
0da46a6e 6041 if (!dirp) {
579a97f7 6042 ret = -TARGET_ENOMEM;
0da46a6e
TS
6043 goto fail;
6044 }
3b46e624 6045
4add45b4
FB
6046 ret = get_errno(sys_getdents(arg1, dirp, count));
6047 if (!is_error(ret)) {
6556a833 6048 struct linux_dirent *de;
4add45b4
FB
6049 struct target_dirent *tde;
6050 int len = ret;
6051 int reclen, treclen;
6052 int count1, tnamelen;
6053
6054 count1 = 0;
6055 de = dirp;
579a97f7
FB
6056 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6057 goto efault;
4add45b4
FB
6058 tde = target_dirp;
6059 while (len > 0) {
6060 reclen = de->d_reclen;
992f48a0 6061 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4add45b4
FB
6062 tde->d_reclen = tswap16(treclen);
6063 tde->d_ino = tswapl(de->d_ino);
6064 tde->d_off = tswapl(de->d_off);
992f48a0 6065 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4add45b4
FB
6066 if (tnamelen > 256)
6067 tnamelen = 256;
80a9d035 6068 /* XXX: may not be correct */
be15b141 6069 pstrcpy(tde->d_name, tnamelen, de->d_name);
6556a833 6070 de = (struct linux_dirent *)((char *)de + reclen);
4add45b4 6071 len -= reclen;
1c5bf3bf 6072 tde = (struct target_dirent *)((char *)tde + treclen);
4add45b4
FB
6073 count1 += treclen;
6074 }
6075 ret = count1;
579a97f7 6076 unlock_user(target_dirp, arg2, ret);
4add45b4
FB
6077 }
6078 free(dirp);
6079 }
6080#else
31e31b8a 6081 {
6556a833 6082 struct linux_dirent *dirp;
992f48a0 6083 abi_long count = arg3;
dab2ed99 6084
579a97f7
FB
6085 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6086 goto efault;
72f03900 6087 ret = get_errno(sys_getdents(arg1, dirp, count));
31e31b8a 6088 if (!is_error(ret)) {
6556a833 6089 struct linux_dirent *de;
31e31b8a
FB
6090 int len = ret;
6091 int reclen;
6092 de = dirp;
6093 while (len > 0) {
8083a3e5 6094 reclen = de->d_reclen;
31e31b8a
FB
6095 if (reclen > len)
6096 break;
8083a3e5 6097 de->d_reclen = tswap16(reclen);
31e31b8a
FB
6098 tswapls(&de->d_ino);
6099 tswapls(&de->d_off);
6556a833 6100 de = (struct linux_dirent *)((char *)de + reclen);
31e31b8a
FB
6101 len -= reclen;
6102 }
6103 }
53a5960a 6104 unlock_user(dirp, arg2, ret);
31e31b8a 6105 }
4add45b4 6106#endif
31e31b8a 6107 break;
3ae43202 6108#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
dab2ed99
FB
6109 case TARGET_NR_getdents64:
6110 {
6556a833 6111 struct linux_dirent64 *dirp;
992f48a0 6112 abi_long count = arg3;
579a97f7
FB
6113 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6114 goto efault;
dab2ed99
FB
6115 ret = get_errno(sys_getdents64(arg1, dirp, count));
6116 if (!is_error(ret)) {
6556a833 6117 struct linux_dirent64 *de;
dab2ed99
FB
6118 int len = ret;
6119 int reclen;
6120 de = dirp;
6121 while (len > 0) {
8083a3e5 6122 reclen = de->d_reclen;
dab2ed99
FB
6123 if (reclen > len)
6124 break;
8083a3e5 6125 de->d_reclen = tswap16(reclen);
8582a53a
FB
6126 tswap64s((uint64_t *)&de->d_ino);
6127 tswap64s((uint64_t *)&de->d_off);
6556a833 6128 de = (struct linux_dirent64 *)((char *)de + reclen);
dab2ed99
FB
6129 len -= reclen;
6130 }
6131 }
53a5960a 6132 unlock_user(dirp, arg2, ret);
dab2ed99
FB
6133 }
6134 break;
a541f297 6135#endif /* TARGET_NR_getdents64 */
e5febef5 6136#ifdef TARGET_NR__newselect
31e31b8a 6137 case TARGET_NR__newselect:
53a5960a 6138 ret = do_select(arg1, arg2, arg3, arg4, arg5);
31e31b8a 6139 break;
e5febef5
TS
6140#endif
6141#ifdef TARGET_NR_poll
9de5e440
FB
6142 case TARGET_NR_poll:
6143 {
53a5960a 6144 struct target_pollfd *target_pfd;
9de5e440
FB
6145 unsigned int nfds = arg2;
6146 int timeout = arg3;
6147 struct pollfd *pfd;
7854b056 6148 unsigned int i;
9de5e440 6149
579a97f7
FB
6150 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6151 if (!target_pfd)
6152 goto efault;
9de5e440
FB
6153 pfd = alloca(sizeof(struct pollfd) * nfds);
6154 for(i = 0; i < nfds; i++) {
5cd4393b
FB
6155 pfd[i].fd = tswap32(target_pfd[i].fd);
6156 pfd[i].events = tswap16(target_pfd[i].events);
9de5e440
FB
6157 }
6158 ret = get_errno(poll(pfd, nfds, timeout));
6159 if (!is_error(ret)) {
6160 for(i = 0; i < nfds; i++) {
5cd4393b 6161 target_pfd[i].revents = tswap16(pfd[i].revents);
9de5e440 6162 }
53a5960a
PB
6163 ret += nfds * (sizeof(struct target_pollfd)
6164 - sizeof(struct pollfd));
9de5e440 6165 }
53a5960a 6166 unlock_user(target_pfd, arg1, ret);
9de5e440
FB
6167 }
6168 break;
e5febef5 6169#endif
31e31b8a 6170 case TARGET_NR_flock:
9de5e440
FB
6171 /* NOTE: the flock constant seems to be the same for every
6172 Linux platform */
6173 ret = get_errno(flock(arg1, arg2));
31e31b8a
FB
6174 break;
6175 case TARGET_NR_readv:
6176 {
6177 int count = arg3;
31e31b8a 6178 struct iovec *vec;
31e31b8a
FB
6179
6180 vec = alloca(count * sizeof(struct iovec));
41df8411
FB
6181 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6182 goto efault;
31e31b8a 6183 ret = get_errno(readv(arg1, vec, count));
53a5960a 6184 unlock_iovec(vec, arg2, count, 1);
31e31b8a
FB
6185 }
6186 break;
6187 case TARGET_NR_writev:
6188 {
6189 int count = arg3;
31e31b8a 6190 struct iovec *vec;
31e31b8a
FB
6191
6192 vec = alloca(count * sizeof(struct iovec));
41df8411
FB
6193 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6194 goto efault;
31e31b8a 6195 ret = get_errno(writev(arg1, vec, count));
53a5960a 6196 unlock_iovec(vec, arg2, count, 0);
31e31b8a
FB
6197 }
6198 break;
6199 case TARGET_NR_getsid:
6200 ret = get_errno(getsid(arg1));
6201 break;
7a3148a9 6202#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
31e31b8a 6203 case TARGET_NR_fdatasync:
5cd4393b
FB
6204 ret = get_errno(fdatasync(arg1));
6205 break;
7a3148a9 6206#endif
31e31b8a 6207 case TARGET_NR__sysctl:
0da46a6e 6208 /* We don't implement this, but ENOTDIR is always a safe
29e619b1 6209 return value. */
0da46a6e
TS
6210 ret = -TARGET_ENOTDIR;
6211 break;
31e31b8a 6212 case TARGET_NR_sched_setparam:
5cd4393b 6213 {
53a5960a 6214 struct sched_param *target_schp;
5cd4393b 6215 struct sched_param schp;
53a5960a 6216
579a97f7
FB
6217 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6218 goto efault;
5cd4393b 6219 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 6220 unlock_user_struct(target_schp, arg2, 0);
5cd4393b
FB
6221 ret = get_errno(sched_setparam(arg1, &schp));
6222 }
6223 break;
31e31b8a 6224 case TARGET_NR_sched_getparam:
5cd4393b 6225 {
53a5960a 6226 struct sched_param *target_schp;
5cd4393b
FB
6227 struct sched_param schp;
6228 ret = get_errno(sched_getparam(arg1, &schp));
6229 if (!is_error(ret)) {
579a97f7
FB
6230 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6231 goto efault;
5cd4393b 6232 target_schp->sched_priority = tswap32(schp.sched_priority);
53a5960a 6233 unlock_user_struct(target_schp, arg2, 1);
5cd4393b
FB
6234 }
6235 }
6236 break;
31e31b8a 6237 case TARGET_NR_sched_setscheduler:
5cd4393b 6238 {
53a5960a 6239 struct sched_param *target_schp;
5cd4393b 6240 struct sched_param schp;
579a97f7
FB
6241 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6242 goto efault;
5cd4393b 6243 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 6244 unlock_user_struct(target_schp, arg3, 0);
5cd4393b
FB
6245 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6246 }
6247 break;
31e31b8a 6248 case TARGET_NR_sched_getscheduler:
5cd4393b
FB
6249 ret = get_errno(sched_getscheduler(arg1));
6250 break;
31e31b8a
FB
6251 case TARGET_NR_sched_yield:
6252 ret = get_errno(sched_yield());
6253 break;
6254 case TARGET_NR_sched_get_priority_max:
5cd4393b
FB
6255 ret = get_errno(sched_get_priority_max(arg1));
6256 break;
31e31b8a 6257 case TARGET_NR_sched_get_priority_min:
5cd4393b
FB
6258 ret = get_errno(sched_get_priority_min(arg1));
6259 break;
31e31b8a 6260 case TARGET_NR_sched_rr_get_interval:
5cd4393b 6261 {
5cd4393b
FB
6262 struct timespec ts;
6263 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6264 if (!is_error(ret)) {
53a5960a 6265 host_to_target_timespec(arg2, &ts);
5cd4393b
FB
6266 }
6267 }
6268 break;
31e31b8a 6269 case TARGET_NR_nanosleep:
1b6b029e 6270 {
1b6b029e 6271 struct timespec req, rem;
53a5960a 6272 target_to_host_timespec(&req, arg1);
1b6b029e 6273 ret = get_errno(nanosleep(&req, &rem));
53a5960a
PB
6274 if (is_error(ret) && arg2) {
6275 host_to_target_timespec(arg2, &rem);
1b6b029e
FB
6276 }
6277 }
6278 break;
e5febef5 6279#ifdef TARGET_NR_query_module
31e31b8a 6280 case TARGET_NR_query_module:
5cd4393b 6281 goto unimplemented;
e5febef5
TS
6282#endif
6283#ifdef TARGET_NR_nfsservctl
31e31b8a 6284 case TARGET_NR_nfsservctl:
5cd4393b 6285 goto unimplemented;
e5febef5 6286#endif
31e31b8a 6287 case TARGET_NR_prctl:
e5574487
TS
6288 switch (arg1)
6289 {
6290 case PR_GET_PDEATHSIG:
6291 {
6292 int deathsig;
6293 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
2f619698
FB
6294 if (!is_error(ret) && arg2
6295 && put_user_ual(deathsig, arg2))
6296 goto efault;
e5574487
TS
6297 }
6298 break;
6299 default:
6300 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6301 break;
6302 }
39b9aae1 6303 break;
d2fd1af7
FB
6304#ifdef TARGET_NR_arch_prctl
6305 case TARGET_NR_arch_prctl:
6306#if defined(TARGET_I386) && !defined(TARGET_ABI32)
6307 ret = do_arch_prctl(cpu_env, arg1, arg2);
6308 break;
6309#else
6310 goto unimplemented;
6311#endif
6312#endif
67867308 6313#ifdef TARGET_NR_pread
31e31b8a 6314 case TARGET_NR_pread:
a4ae00bc
AZ
6315#ifdef TARGET_ARM
6316 if (((CPUARMState *)cpu_env)->eabi)
6317 arg4 = arg5;
6318#endif
579a97f7
FB
6319 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6320 goto efault;
53a5960a
PB
6321 ret = get_errno(pread(arg1, p, arg3, arg4));
6322 unlock_user(p, arg2, ret);
206f0fa7 6323 break;
31e31b8a 6324 case TARGET_NR_pwrite:
a4ae00bc
AZ
6325#ifdef TARGET_ARM
6326 if (((CPUARMState *)cpu_env)->eabi)
6327 arg4 = arg5;
6328#endif
579a97f7
FB
6329 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6330 goto efault;
53a5960a
PB
6331 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6332 unlock_user(p, arg2, 0);
206f0fa7 6333 break;
f2c7ba15
AJ
6334#endif
6335#ifdef TARGET_NR_pread64
6336 case TARGET_NR_pread64:
6337 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6338 goto efault;
6339 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6340 unlock_user(p, arg2, ret);
6341 break;
6342 case TARGET_NR_pwrite64:
6343 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6344 goto efault;
6345 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6346 unlock_user(p, arg2, 0);
6347 break;
67867308 6348#endif
31e31b8a 6349 case TARGET_NR_getcwd:
579a97f7
FB
6350 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6351 goto efault;
53a5960a
PB
6352 ret = get_errno(sys_getcwd1(p, arg2));
6353 unlock_user(p, arg1, ret);
31e31b8a
FB
6354 break;
6355 case TARGET_NR_capget:
5cd4393b 6356 goto unimplemented;
31e31b8a 6357 case TARGET_NR_capset:
5cd4393b 6358 goto unimplemented;
31e31b8a 6359 case TARGET_NR_sigaltstack:
198a74de 6360#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
c761c154
LV
6361 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6362 defined(TARGET_M68K)
579a97f7 6363 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
a04e134a
TS
6364 break;
6365#else
5cd4393b 6366 goto unimplemented;
a04e134a 6367#endif
31e31b8a 6368 case TARGET_NR_sendfile:
5cd4393b 6369 goto unimplemented;
ebc05488 6370#ifdef TARGET_NR_getpmsg
31e31b8a 6371 case TARGET_NR_getpmsg:
5cd4393b 6372 goto unimplemented;
ebc05488
FB
6373#endif
6374#ifdef TARGET_NR_putpmsg
31e31b8a 6375 case TARGET_NR_putpmsg:
5cd4393b 6376 goto unimplemented;
ebc05488 6377#endif
048f6b4d 6378#ifdef TARGET_NR_vfork
31e31b8a 6379 case TARGET_NR_vfork:
d865bab5
PB
6380 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6381 0, 0, 0, 0));
31e31b8a 6382 break;
048f6b4d 6383#endif
ebc05488 6384#ifdef TARGET_NR_ugetrlimit
31e31b8a 6385 case TARGET_NR_ugetrlimit:
728584be
FB
6386 {
6387 struct rlimit rlim;
6388 ret = get_errno(getrlimit(arg1, &rlim));
6389 if (!is_error(ret)) {
53a5960a 6390 struct target_rlimit *target_rlim;
579a97f7
FB
6391 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6392 goto efault;
81bbe906
TY
6393 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6394 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 6395 unlock_user_struct(target_rlim, arg2, 1);
728584be
FB
6396 }
6397 break;
6398 }
ebc05488 6399#endif
a315a145 6400#ifdef TARGET_NR_truncate64
31e31b8a 6401 case TARGET_NR_truncate64:
579a97f7
FB
6402 if (!(p = lock_user_string(arg1)))
6403 goto efault;
53a5960a
PB
6404 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6405 unlock_user(p, arg1, 0);
667f38b1 6406 break;
a315a145
FB
6407#endif
6408#ifdef TARGET_NR_ftruncate64
31e31b8a 6409 case TARGET_NR_ftruncate64:
ce4defa0 6410 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
667f38b1 6411 break;
a315a145
FB
6412#endif
6413#ifdef TARGET_NR_stat64
31e31b8a 6414 case TARGET_NR_stat64:
579a97f7
FB
6415 if (!(p = lock_user_string(arg1)))
6416 goto efault;
53a5960a
PB
6417 ret = get_errno(stat(path(p), &st));
6418 unlock_user(p, arg1, 0);
6a24a778
AZ
6419 if (!is_error(ret))
6420 ret = host_to_target_stat64(cpu_env, arg2, &st);
6421 break;
a315a145
FB
6422#endif
6423#ifdef TARGET_NR_lstat64
31e31b8a 6424 case TARGET_NR_lstat64:
579a97f7
FB
6425 if (!(p = lock_user_string(arg1)))
6426 goto efault;
53a5960a
PB
6427 ret = get_errno(lstat(path(p), &st));
6428 unlock_user(p, arg1, 0);
6a24a778
AZ
6429 if (!is_error(ret))
6430 ret = host_to_target_stat64(cpu_env, arg2, &st);
6431 break;
a315a145
FB
6432#endif
6433#ifdef TARGET_NR_fstat64
31e31b8a 6434 case TARGET_NR_fstat64:
6a24a778
AZ
6435 ret = get_errno(fstat(arg1, &st));
6436 if (!is_error(ret))
6437 ret = host_to_target_stat64(cpu_env, arg2, &st);
6438 break;
ce4defa0 6439#endif
9d33b76b
AJ
6440#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6441 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6442#ifdef TARGET_NR_fstatat64
6a24a778 6443 case TARGET_NR_fstatat64:
9d33b76b
AJ
6444#endif
6445#ifdef TARGET_NR_newfstatat
6446 case TARGET_NR_newfstatat:
6447#endif
6a24a778
AZ
6448 if (!(p = lock_user_string(arg2)))
6449 goto efault;
9d33b76b 6450#ifdef __NR_fstatat64
6a24a778 6451 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
9d33b76b
AJ
6452#else
6453 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6454#endif
6a24a778
AZ
6455 if (!is_error(ret))
6456 ret = host_to_target_stat64(cpu_env, arg3, &st);
60cd49d5 6457 break;
a315a145 6458#endif
67867308
FB
6459#ifdef USE_UID16
6460 case TARGET_NR_lchown:
579a97f7
FB
6461 if (!(p = lock_user_string(arg1)))
6462 goto efault;
53a5960a
PB
6463 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6464 unlock_user(p, arg1, 0);
67867308
FB
6465 break;
6466 case TARGET_NR_getuid:
6467 ret = get_errno(high2lowuid(getuid()));
6468 break;
6469 case TARGET_NR_getgid:
6470 ret = get_errno(high2lowgid(getgid()));
6471 break;
6472 case TARGET_NR_geteuid:
6473 ret = get_errno(high2lowuid(geteuid()));
6474 break;
6475 case TARGET_NR_getegid:
6476 ret = get_errno(high2lowgid(getegid()));
6477 break;
6478 case TARGET_NR_setreuid:
6479 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6480 break;
6481 case TARGET_NR_setregid:
6482 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6483 break;
6484 case TARGET_NR_getgroups:
6485 {
6486 int gidsetsize = arg1;
53a5960a 6487 uint16_t *target_grouplist;
67867308
FB
6488 gid_t *grouplist;
6489 int i;
6490
6491 grouplist = alloca(gidsetsize * sizeof(gid_t));
6492 ret = get_errno(getgroups(gidsetsize, grouplist));
cb3bc233
AZ
6493 if (gidsetsize == 0)
6494 break;
67867308 6495 if (!is_error(ret)) {
579a97f7
FB
6496 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6497 if (!target_grouplist)
6498 goto efault;
a2155fcc 6499 for(i = 0;i < ret; i++)
67867308 6500 target_grouplist[i] = tswap16(grouplist[i]);
53a5960a 6501 unlock_user(target_grouplist, arg2, gidsetsize * 2);
67867308
FB
6502 }
6503 }
6504 break;
6505 case TARGET_NR_setgroups:
6506 {
6507 int gidsetsize = arg1;
53a5960a 6508 uint16_t *target_grouplist;
67867308
FB
6509 gid_t *grouplist;
6510 int i;
6511
6512 grouplist = alloca(gidsetsize * sizeof(gid_t));
579a97f7
FB
6513 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6514 if (!target_grouplist) {
6515 ret = -TARGET_EFAULT;
6516 goto fail;
6517 }
67867308
FB
6518 for(i = 0;i < gidsetsize; i++)
6519 grouplist[i] = tswap16(target_grouplist[i]);
53a5960a 6520 unlock_user(target_grouplist, arg2, 0);
67867308
FB
6521 ret = get_errno(setgroups(gidsetsize, grouplist));
6522 }
6523 break;
6524 case TARGET_NR_fchown:
6525 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6526 break;
ccfa72b7
TS
6527#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6528 case TARGET_NR_fchownat:
579a97f7
FB
6529 if (!(p = lock_user_string(arg2)))
6530 goto efault;
6531 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6532 unlock_user(p, arg2, 0);
ccfa72b7
TS
6533 break;
6534#endif
67867308
FB
6535#ifdef TARGET_NR_setresuid
6536 case TARGET_NR_setresuid:
5fafdf24
TS
6537 ret = get_errno(setresuid(low2highuid(arg1),
6538 low2highuid(arg2),
67867308
FB
6539 low2highuid(arg3)));
6540 break;
6541#endif
6542#ifdef TARGET_NR_getresuid
6543 case TARGET_NR_getresuid:
6544 {
53a5960a 6545 uid_t ruid, euid, suid;
67867308
FB
6546 ret = get_errno(getresuid(&ruid, &euid, &suid));
6547 if (!is_error(ret)) {
2f619698
FB
6548 if (put_user_u16(high2lowuid(ruid), arg1)
6549 || put_user_u16(high2lowuid(euid), arg2)
6550 || put_user_u16(high2lowuid(suid), arg3))
6551 goto efault;
67867308
FB
6552 }
6553 }
6554 break;
6555#endif
6556#ifdef TARGET_NR_getresgid
6557 case TARGET_NR_setresgid:
5fafdf24
TS
6558 ret = get_errno(setresgid(low2highgid(arg1),
6559 low2highgid(arg2),
67867308
FB
6560 low2highgid(arg3)));
6561 break;
6562#endif
6563#ifdef TARGET_NR_getresgid
6564 case TARGET_NR_getresgid:
6565 {
53a5960a 6566 gid_t rgid, egid, sgid;
67867308
FB
6567 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6568 if (!is_error(ret)) {
2f619698
FB
6569 if (put_user_u16(high2lowgid(rgid), arg1)
6570 || put_user_u16(high2lowgid(egid), arg2)
6571 || put_user_u16(high2lowgid(sgid), arg3))
6572 goto efault;
67867308
FB
6573 }
6574 }
6575 break;
6576#endif
6577 case TARGET_NR_chown:
579a97f7
FB
6578 if (!(p = lock_user_string(arg1)))
6579 goto efault;
53a5960a
PB
6580 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6581 unlock_user(p, arg1, 0);
67867308
FB
6582 break;
6583 case TARGET_NR_setuid:
6584 ret = get_errno(setuid(low2highuid(arg1)));
6585 break;
6586 case TARGET_NR_setgid:
6587 ret = get_errno(setgid(low2highgid(arg1)));
6588 break;
6589 case TARGET_NR_setfsuid:
6590 ret = get_errno(setfsuid(arg1));
6591 break;
6592 case TARGET_NR_setfsgid:
6593 ret = get_errno(setfsgid(arg1));
6594 break;
6595#endif /* USE_UID16 */
6596
a315a145 6597#ifdef TARGET_NR_lchown32
31e31b8a 6598 case TARGET_NR_lchown32:
579a97f7
FB
6599 if (!(p = lock_user_string(arg1)))
6600 goto efault;
53a5960a
PB
6601 ret = get_errno(lchown(p, arg2, arg3));
6602 unlock_user(p, arg1, 0);
b03c60f3 6603 break;
a315a145
FB
6604#endif
6605#ifdef TARGET_NR_getuid32
31e31b8a 6606 case TARGET_NR_getuid32:
b03c60f3
FB
6607 ret = get_errno(getuid());
6608 break;
a315a145 6609#endif
64b4d28c
AJ
6610
6611#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6612 /* Alpha specific */
6613 case TARGET_NR_getxuid:
ba0e276d
RH
6614 {
6615 uid_t euid;
6616 euid=geteuid();
6617 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6618 }
64b4d28c
AJ
6619 ret = get_errno(getuid());
6620 break;
6621#endif
6622#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6623 /* Alpha specific */
6624 case TARGET_NR_getxgid:
ba0e276d
RH
6625 {
6626 uid_t egid;
6627 egid=getegid();
6628 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6629 }
64b4d28c
AJ
6630 ret = get_errno(getgid());
6631 break;
6632#endif
ba0e276d
RH
6633#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
6634 /* Alpha specific */
6635 case TARGET_NR_osf_getsysinfo:
6636 ret = -TARGET_EOPNOTSUPP;
6637 switch (arg1) {
6638 case TARGET_GSI_IEEE_FP_CONTROL:
6639 {
6640 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
6641
6642 /* Copied from linux ieee_fpcr_to_swcr. */
6643 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
6644 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
6645 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
6646 | SWCR_TRAP_ENABLE_DZE
6647 | SWCR_TRAP_ENABLE_OVF);
6648 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
6649 | SWCR_TRAP_ENABLE_INE);
6650 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
6651 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
6652
6653 if (put_user_u64 (swcr, arg2))
6654 goto efault;
6655 ret = 0;
6656 }
6657 break;
6658
6659 /* case GSI_IEEE_STATE_AT_SIGNAL:
6660 -- Not implemented in linux kernel.
6661 case GSI_UACPROC:
6662 -- Retrieves current unaligned access state; not much used.
6663 case GSI_PROC_TYPE:
6664 -- Retrieves implver information; surely not used.
6665 case GSI_GET_HWRPB:
6666 -- Grabs a copy of the HWRPB; surely not used.
6667 */
6668 }
6669 break;
6670#endif
6671#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
6672 /* Alpha specific */
6673 case TARGET_NR_osf_setsysinfo:
6674 ret = -TARGET_EOPNOTSUPP;
6675 switch (arg1) {
6676 case TARGET_SSI_IEEE_FP_CONTROL:
6677 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
6678 {
6679 uint64_t swcr, fpcr, orig_fpcr;
6680
6681 if (get_user_u64 (swcr, arg2))
6682 goto efault;
6683 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
6684 fpcr = orig_fpcr & FPCR_DYN_MASK;
6685
6686 /* Copied from linux ieee_swcr_to_fpcr. */
6687 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
6688 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
6689 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
6690 | SWCR_TRAP_ENABLE_DZE
6691 | SWCR_TRAP_ENABLE_OVF)) << 48;
6692 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
6693 | SWCR_TRAP_ENABLE_INE)) << 57;
6694 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
6695 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
6696
6697 cpu_alpha_store_fpcr (cpu_env, fpcr);
6698 ret = 0;
6699
6700 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
6701 /* Old exceptions are not signaled. */
6702 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
6703
6704 /* If any exceptions set by this call, and are unmasked,
6705 send a signal. */
6706 /* ??? FIXME */
6707 }
6708 }
6709 break;
6710
6711 /* case SSI_NVPAIRS:
6712 -- Used with SSIN_UACPROC to enable unaligned accesses.
6713 case SSI_IEEE_STATE_AT_SIGNAL:
6714 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
6715 -- Not implemented in linux kernel
6716 */
6717 }
6718 break;
6719#endif
6720#ifdef TARGET_NR_osf_sigprocmask
6721 /* Alpha specific. */
6722 case TARGET_NR_osf_sigprocmask:
6723 {
6724 abi_ulong mask;
6725 int how = arg1;
6726 sigset_t set, oldset;
6727
6728 switch(arg1) {
6729 case TARGET_SIG_BLOCK:
6730 how = SIG_BLOCK;
6731 break;
6732 case TARGET_SIG_UNBLOCK:
6733 how = SIG_UNBLOCK;
6734 break;
6735 case TARGET_SIG_SETMASK:
6736 how = SIG_SETMASK;
6737 break;
6738 default:
6739 ret = -TARGET_EINVAL;
6740 goto fail;
6741 }
6742 mask = arg2;
6743 target_to_host_old_sigset(&set, &mask);
6744 sigprocmask(arg1, &set, &oldset);
6745 host_to_target_old_sigset(&mask, &oldset);
6746 ret = mask;
6747 }
6748 break;
6749#endif
64b4d28c 6750
a315a145 6751#ifdef TARGET_NR_getgid32
31e31b8a 6752 case TARGET_NR_getgid32:
b03c60f3
FB
6753 ret = get_errno(getgid());
6754 break;
a315a145
FB
6755#endif
6756#ifdef TARGET_NR_geteuid32
31e31b8a 6757 case TARGET_NR_geteuid32:
b03c60f3
FB
6758 ret = get_errno(geteuid());
6759 break;
a315a145
FB
6760#endif
6761#ifdef TARGET_NR_getegid32
31e31b8a 6762 case TARGET_NR_getegid32:
b03c60f3
FB
6763 ret = get_errno(getegid());
6764 break;
a315a145
FB
6765#endif
6766#ifdef TARGET_NR_setreuid32
31e31b8a 6767 case TARGET_NR_setreuid32:
b03c60f3
FB
6768 ret = get_errno(setreuid(arg1, arg2));
6769 break;
a315a145
FB
6770#endif
6771#ifdef TARGET_NR_setregid32
31e31b8a 6772 case TARGET_NR_setregid32:
b03c60f3
FB
6773 ret = get_errno(setregid(arg1, arg2));
6774 break;
a315a145
FB
6775#endif
6776#ifdef TARGET_NR_getgroups32
31e31b8a 6777 case TARGET_NR_getgroups32:
99c475ab
FB
6778 {
6779 int gidsetsize = arg1;
53a5960a 6780 uint32_t *target_grouplist;
99c475ab
FB
6781 gid_t *grouplist;
6782 int i;
6783
6784 grouplist = alloca(gidsetsize * sizeof(gid_t));
6785 ret = get_errno(getgroups(gidsetsize, grouplist));
cb3bc233
AZ
6786 if (gidsetsize == 0)
6787 break;
99c475ab 6788 if (!is_error(ret)) {
579a97f7
FB
6789 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6790 if (!target_grouplist) {
6791 ret = -TARGET_EFAULT;
6792 goto fail;
6793 }
a2155fcc 6794 for(i = 0;i < ret; i++)
53a5960a
PB
6795 target_grouplist[i] = tswap32(grouplist[i]);
6796 unlock_user(target_grouplist, arg2, gidsetsize * 4);
99c475ab
FB
6797 }
6798 }
6799 break;
a315a145
FB
6800#endif
6801#ifdef TARGET_NR_setgroups32
31e31b8a 6802 case TARGET_NR_setgroups32:
99c475ab
FB
6803 {
6804 int gidsetsize = arg1;
53a5960a 6805 uint32_t *target_grouplist;
99c475ab
FB
6806 gid_t *grouplist;
6807 int i;
3b46e624 6808
99c475ab 6809 grouplist = alloca(gidsetsize * sizeof(gid_t));
579a97f7
FB
6810 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6811 if (!target_grouplist) {
6812 ret = -TARGET_EFAULT;
6813 goto fail;
6814 }
99c475ab 6815 for(i = 0;i < gidsetsize; i++)
53a5960a
PB
6816 grouplist[i] = tswap32(target_grouplist[i]);
6817 unlock_user(target_grouplist, arg2, 0);
99c475ab
FB
6818 ret = get_errno(setgroups(gidsetsize, grouplist));
6819 }
6820 break;
a315a145
FB
6821#endif
6822#ifdef TARGET_NR_fchown32
31e31b8a 6823 case TARGET_NR_fchown32:
b03c60f3
FB
6824 ret = get_errno(fchown(arg1, arg2, arg3));
6825 break;
a315a145
FB
6826#endif
6827#ifdef TARGET_NR_setresuid32
31e31b8a 6828 case TARGET_NR_setresuid32:
b03c60f3
FB
6829 ret = get_errno(setresuid(arg1, arg2, arg3));
6830 break;
a315a145
FB
6831#endif
6832#ifdef TARGET_NR_getresuid32
31e31b8a 6833 case TARGET_NR_getresuid32:
b03c60f3 6834 {
53a5960a 6835 uid_t ruid, euid, suid;
b03c60f3
FB
6836 ret = get_errno(getresuid(&ruid, &euid, &suid));
6837 if (!is_error(ret)) {
2f619698
FB
6838 if (put_user_u32(ruid, arg1)
6839 || put_user_u32(euid, arg2)
6840 || put_user_u32(suid, arg3))
6841 goto efault;
b03c60f3
FB
6842 }
6843 }
6844 break;
a315a145
FB
6845#endif
6846#ifdef TARGET_NR_setresgid32
31e31b8a 6847 case TARGET_NR_setresgid32:
b03c60f3
FB
6848 ret = get_errno(setresgid(arg1, arg2, arg3));
6849 break;
a315a145
FB
6850#endif
6851#ifdef TARGET_NR_getresgid32
31e31b8a 6852 case TARGET_NR_getresgid32:
b03c60f3 6853 {
53a5960a 6854 gid_t rgid, egid, sgid;
b03c60f3
FB
6855 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6856 if (!is_error(ret)) {
2f619698
FB
6857 if (put_user_u32(rgid, arg1)
6858 || put_user_u32(egid, arg2)
6859 || put_user_u32(sgid, arg3))
6860 goto efault;
b03c60f3
FB
6861 }
6862 }
6863 break;
a315a145
FB
6864#endif
6865#ifdef TARGET_NR_chown32
31e31b8a 6866 case TARGET_NR_chown32:
579a97f7
FB
6867 if (!(p = lock_user_string(arg1)))
6868 goto efault;
53a5960a
PB
6869 ret = get_errno(chown(p, arg2, arg3));
6870 unlock_user(p, arg1, 0);
b03c60f3 6871 break;
a315a145
FB
6872#endif
6873#ifdef TARGET_NR_setuid32
31e31b8a 6874 case TARGET_NR_setuid32:
b03c60f3
FB
6875 ret = get_errno(setuid(arg1));
6876 break;
a315a145
FB
6877#endif
6878#ifdef TARGET_NR_setgid32
31e31b8a 6879 case TARGET_NR_setgid32:
b03c60f3
FB
6880 ret = get_errno(setgid(arg1));
6881 break;
a315a145
FB
6882#endif
6883#ifdef TARGET_NR_setfsuid32
31e31b8a 6884 case TARGET_NR_setfsuid32:
b03c60f3
FB
6885 ret = get_errno(setfsuid(arg1));
6886 break;
a315a145
FB
6887#endif
6888#ifdef TARGET_NR_setfsgid32
31e31b8a 6889 case TARGET_NR_setfsgid32:
b03c60f3
FB
6890 ret = get_errno(setfsgid(arg1));
6891 break;
a315a145 6892#endif
67867308 6893
31e31b8a 6894 case TARGET_NR_pivot_root:
b03c60f3 6895 goto unimplemented;
ffa65c3b 6896#ifdef TARGET_NR_mincore
31e31b8a 6897 case TARGET_NR_mincore:
04bb9ace
AJ
6898 {
6899 void *a;
6900 ret = -TARGET_EFAULT;
6901 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6902 goto efault;
6903 if (!(p = lock_user_string(arg3)))
6904 goto mincore_fail;
6905 ret = get_errno(mincore(a, arg2, p));
6906 unlock_user(p, arg3, ret);
6907 mincore_fail:
6908 unlock_user(a, arg1, 0);
6909 }
6910 break;
ffa65c3b 6911#endif
408321b6
AJ
6912#ifdef TARGET_NR_arm_fadvise64_64
6913 case TARGET_NR_arm_fadvise64_64:
6914 {
6915 /*
6916 * arm_fadvise64_64 looks like fadvise64_64 but
6917 * with different argument order
6918 */
6919 abi_long temp;
6920 temp = arg3;
6921 arg3 = arg4;
6922 arg4 = temp;
6923 }
6924#endif
e72d2cc7 6925#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
408321b6
AJ
6926#ifdef TARGET_NR_fadvise64_64
6927 case TARGET_NR_fadvise64_64:
6928#endif
e72d2cc7
UH
6929#ifdef TARGET_NR_fadvise64
6930 case TARGET_NR_fadvise64:
6931#endif
6932#ifdef TARGET_S390X
6933 switch (arg4) {
6934 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
6935 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
6936 case 6: arg4 = POSIX_FADV_DONTNEED; break;
6937 case 7: arg4 = POSIX_FADV_NOREUSE; break;
6938 default: break;
6939 }
6940#endif
6941 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
408321b6
AJ
6942 break;
6943#endif
ffa65c3b 6944#ifdef TARGET_NR_madvise
31e31b8a 6945 case TARGET_NR_madvise:
24836689
PB
6946 /* A straight passthrough may not be safe because qemu sometimes
6947 turns private flie-backed mappings into anonymous mappings.
6948 This will break MADV_DONTNEED.
6949 This is a hint, so ignoring and returning success is ok. */
6950 ret = get_errno(0);
6951 break;
ffa65c3b 6952#endif
992f48a0 6953#if TARGET_ABI_BITS == 32
31e31b8a 6954 case TARGET_NR_fcntl64:
77e4672d 6955 {
b1e341eb 6956 int cmd;
77e4672d 6957 struct flock64 fl;
53a5960a 6958 struct target_flock64 *target_fl;
ce4defa0 6959#ifdef TARGET_ARM
53a5960a 6960 struct target_eabi_flock64 *target_efl;
ce4defa0 6961#endif
77e4672d 6962
5f106811
APR
6963 cmd = target_to_host_fcntl_cmd(arg2);
6964 if (cmd == -TARGET_EINVAL)
6965 return cmd;
b1e341eb 6966
60cd49d5 6967 switch(arg2) {
b1e341eb 6968 case TARGET_F_GETLK64:
5813427b
TS
6969#ifdef TARGET_ARM
6970 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
6971 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6972 goto efault;
5813427b
TS
6973 fl.l_type = tswap16(target_efl->l_type);
6974 fl.l_whence = tswap16(target_efl->l_whence);
6975 fl.l_start = tswap64(target_efl->l_start);
6976 fl.l_len = tswap64(target_efl->l_len);
7e22e546 6977 fl.l_pid = tswap32(target_efl->l_pid);
5813427b
TS
6978 unlock_user_struct(target_efl, arg3, 0);
6979 } else
6980#endif
6981 {
9ee1fa2c
FB
6982 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6983 goto efault;
5813427b
TS
6984 fl.l_type = tswap16(target_fl->l_type);
6985 fl.l_whence = tswap16(target_fl->l_whence);
6986 fl.l_start = tswap64(target_fl->l_start);
6987 fl.l_len = tswap64(target_fl->l_len);
7e22e546 6988 fl.l_pid = tswap32(target_fl->l_pid);
5813427b
TS
6989 unlock_user_struct(target_fl, arg3, 0);
6990 }
b1e341eb 6991 ret = get_errno(fcntl(arg1, cmd, &fl));
77e4672d 6992 if (ret == 0) {
ce4defa0
PB
6993#ifdef TARGET_ARM
6994 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
6995 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6996 goto efault;
ce4defa0
PB
6997 target_efl->l_type = tswap16(fl.l_type);
6998 target_efl->l_whence = tswap16(fl.l_whence);
6999 target_efl->l_start = tswap64(fl.l_start);
7000 target_efl->l_len = tswap64(fl.l_len);
7e22e546 7001 target_efl->l_pid = tswap32(fl.l_pid);
53a5960a 7002 unlock_user_struct(target_efl, arg3, 1);
ce4defa0
PB
7003 } else
7004#endif
7005 {
9ee1fa2c
FB
7006 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
7007 goto efault;
ce4defa0
PB
7008 target_fl->l_type = tswap16(fl.l_type);
7009 target_fl->l_whence = tswap16(fl.l_whence);
7010 target_fl->l_start = tswap64(fl.l_start);
7011 target_fl->l_len = tswap64(fl.l_len);
7e22e546 7012 target_fl->l_pid = tswap32(fl.l_pid);
53a5960a 7013 unlock_user_struct(target_fl, arg3, 1);
ce4defa0 7014 }
77e4672d
FB
7015 }
7016 break;
7017
b1e341eb
TS
7018 case TARGET_F_SETLK64:
7019 case TARGET_F_SETLKW64:
ce4defa0
PB
7020#ifdef TARGET_ARM
7021 if (((CPUARMState *)cpu_env)->eabi) {
9ee1fa2c
FB
7022 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7023 goto efault;
ce4defa0
PB
7024 fl.l_type = tswap16(target_efl->l_type);
7025 fl.l_whence = tswap16(target_efl->l_whence);
7026 fl.l_start = tswap64(target_efl->l_start);
7027 fl.l_len = tswap64(target_efl->l_len);
7e22e546 7028 fl.l_pid = tswap32(target_efl->l_pid);
53a5960a 7029 unlock_user_struct(target_efl, arg3, 0);
ce4defa0
PB
7030 } else
7031#endif
7032 {
9ee1fa2c
FB
7033 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7034 goto efault;
ce4defa0
PB
7035 fl.l_type = tswap16(target_fl->l_type);
7036 fl.l_whence = tswap16(target_fl->l_whence);
7037 fl.l_start = tswap64(target_fl->l_start);
7038 fl.l_len = tswap64(target_fl->l_len);
7e22e546 7039 fl.l_pid = tswap32(target_fl->l_pid);
53a5960a 7040 unlock_user_struct(target_fl, arg3, 0);
ce4defa0 7041 }
b1e341eb 7042 ret = get_errno(fcntl(arg1, cmd, &fl));
77e4672d 7043 break;
60cd49d5 7044 default:
5f106811 7045 ret = do_fcntl(arg1, arg2, arg3);
60cd49d5
FB
7046 break;
7047 }
77e4672d
FB
7048 break;
7049 }
60cd49d5 7050#endif
7d600c80
TS
7051#ifdef TARGET_NR_cacheflush
7052 case TARGET_NR_cacheflush:
7053 /* self-modifying code is handled automatically, so nothing needed */
7054 ret = 0;
7055 break;
7056#endif
ebc05488 7057#ifdef TARGET_NR_security
31e31b8a
FB
7058 case TARGET_NR_security:
7059 goto unimplemented;
c573ff67
FB
7060#endif
7061#ifdef TARGET_NR_getpagesize
7062 case TARGET_NR_getpagesize:
7063 ret = TARGET_PAGE_SIZE;
7064 break;
ebc05488 7065#endif
31e31b8a
FB
7066 case TARGET_NR_gettid:
7067 ret = get_errno(gettid());
7068 break;
e5febef5 7069#ifdef TARGET_NR_readahead
31e31b8a 7070 case TARGET_NR_readahead:
2054ac9b
AJ
7071#if TARGET_ABI_BITS == 32
7072#ifdef TARGET_ARM
7073 if (((CPUARMState *)cpu_env)->eabi)
7074 {
7075 arg2 = arg3;
7076 arg3 = arg4;
7077 arg4 = arg5;
7078 }
7079#endif
7080 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
7081#else
7082 ret = get_errno(readahead(arg1, arg2, arg3));
7083#endif
7084 break;
e5febef5 7085#endif
ebc05488 7086#ifdef TARGET_NR_setxattr
31e31b8a
FB
7087 case TARGET_NR_setxattr:
7088 case TARGET_NR_lsetxattr:
7089 case TARGET_NR_fsetxattr:
7090 case TARGET_NR_getxattr:
7091 case TARGET_NR_lgetxattr:
7092 case TARGET_NR_fgetxattr:
7093 case TARGET_NR_listxattr:
7094 case TARGET_NR_llistxattr:
7095 case TARGET_NR_flistxattr:
7096 case TARGET_NR_removexattr:
7097 case TARGET_NR_lremovexattr:
7098 case TARGET_NR_fremovexattr:
6f932f91
AP
7099 ret = -TARGET_EOPNOTSUPP;
7100 break;
ebc05488
FB
7101#endif
7102#ifdef TARGET_NR_set_thread_area
5cd4393b 7103 case TARGET_NR_set_thread_area:
8d18e893 7104#if defined(TARGET_MIPS)
6f5b89a0
TS
7105 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
7106 ret = 0;
7107 break;
ef96779b
EI
7108#elif defined(TARGET_CRIS)
7109 if (arg1 & 0xff)
7110 ret = -TARGET_EINVAL;
7111 else {
7112 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
7113 ret = 0;
7114 }
7115 break;
8d18e893
FB
7116#elif defined(TARGET_I386) && defined(TARGET_ABI32)
7117 ret = do_set_thread_area(cpu_env, arg1);
7118 break;
6f5b89a0
TS
7119#else
7120 goto unimplemented_nowarn;
7121#endif
7122#endif
7123#ifdef TARGET_NR_get_thread_area
5cd4393b 7124 case TARGET_NR_get_thread_area:
8d18e893
FB
7125#if defined(TARGET_I386) && defined(TARGET_ABI32)
7126 ret = do_get_thread_area(cpu_env, arg1);
7127#else
5cd4393b 7128 goto unimplemented_nowarn;
48dc41eb 7129#endif
8d18e893 7130#endif
48dc41eb
FB
7131#ifdef TARGET_NR_getdomainname
7132 case TARGET_NR_getdomainname:
7133 goto unimplemented_nowarn;
ebc05488 7134#endif
6f5b89a0 7135
b5906f95
TS
7136#ifdef TARGET_NR_clock_gettime
7137 case TARGET_NR_clock_gettime:
7138 {
7139 struct timespec ts;
7140 ret = get_errno(clock_gettime(arg1, &ts));
7141 if (!is_error(ret)) {
7142 host_to_target_timespec(arg2, &ts);
7143 }
7144 break;
7145 }
7146#endif
7147#ifdef TARGET_NR_clock_getres
7148 case TARGET_NR_clock_getres:
7149 {
7150 struct timespec ts;
7151 ret = get_errno(clock_getres(arg1, &ts));
7152 if (!is_error(ret)) {
7153 host_to_target_timespec(arg2, &ts);
7154 }
7155 break;
7156 }
7157#endif
63d7651b
PB
7158#ifdef TARGET_NR_clock_nanosleep
7159 case TARGET_NR_clock_nanosleep:
7160 {
7161 struct timespec ts;
7162 target_to_host_timespec(&ts, arg3);
7163 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7164 if (arg4)
7165 host_to_target_timespec(arg4, &ts);
7166 break;
7167 }
7168#endif
b5906f95 7169
6f5b89a0
TS
7170#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7171 case TARGET_NR_set_tid_address:
579a97f7
FB
7172 ret = get_errno(set_tid_address((int *)g2h(arg1)));
7173 break;
6f5b89a0
TS
7174#endif
7175
3ae43202 7176#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
4cae1d16 7177 case TARGET_NR_tkill:
4cb05961 7178 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
4cae1d16
TS
7179 break;
7180#endif
7181
3ae43202 7182#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
71455574 7183 case TARGET_NR_tgkill:
4cb05961
PB
7184 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7185 target_to_host_signal(arg3)));
71455574
TS
7186 break;
7187#endif
7188
4f2b1fe8
TS
7189#ifdef TARGET_NR_set_robust_list
7190 case TARGET_NR_set_robust_list:
7191 goto unimplemented_nowarn;
7192#endif
7193
9007f0ef
TS
7194#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7195 case TARGET_NR_utimensat:
7196 {
ebc996f3
RV
7197 struct timespec *tsp, ts[2];
7198 if (!arg3) {
7199 tsp = NULL;
7200 } else {
7201 target_to_host_timespec(ts, arg3);
7202 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7203 tsp = ts;
7204 }
9007f0ef 7205 if (!arg2)
ebc996f3 7206 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
9007f0ef 7207 else {
579a97f7 7208 if (!(p = lock_user_string(arg2))) {
0da46a6e 7209 ret = -TARGET_EFAULT;
579a97f7
FB
7210 goto fail;
7211 }
ebc996f3 7212 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
579a97f7 7213 unlock_user(p, arg2, 0);
9007f0ef
TS
7214 }
7215 }
7216 break;
7217#endif
2f7bb878 7218#if defined(CONFIG_USE_NPTL)
bd0c5661
PB
7219 case TARGET_NR_futex:
7220 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7221 break;
7222#endif
dbfe4c36 7223#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
39b59763
AJ
7224 case TARGET_NR_inotify_init:
7225 ret = get_errno(sys_inotify_init());
7226 break;
7227#endif
a1606b0b 7228#ifdef CONFIG_INOTIFY1
c05c7a73
RV
7229#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7230 case TARGET_NR_inotify_init1:
7231 ret = get_errno(sys_inotify_init1(arg1));
7232 break;
7233#endif
a1606b0b 7234#endif
dbfe4c36 7235#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
39b59763
AJ
7236 case TARGET_NR_inotify_add_watch:
7237 p = lock_user_string(arg2);
7238 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7239 unlock_user(p, arg2, 0);
7240 break;
7241#endif
dbfe4c36 7242#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
39b59763
AJ
7243 case TARGET_NR_inotify_rm_watch:
7244 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7245 break;
7246#endif
9007f0ef 7247
8ec9cf89 7248#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
24e1003a
AJ
7249 case TARGET_NR_mq_open:
7250 {
7251 struct mq_attr posix_mq_attr;
7252
7253 p = lock_user_string(arg1 - 1);
7254 if (arg4 != 0)
7255 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7256 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7257 unlock_user (p, arg1, 0);
7258 }
7259 break;
7260
7261 case TARGET_NR_mq_unlink:
7262 p = lock_user_string(arg1 - 1);
7263 ret = get_errno(mq_unlink(p));
7264 unlock_user (p, arg1, 0);
7265 break;
7266
7267 case TARGET_NR_mq_timedsend:
7268 {
7269 struct timespec ts;
7270
7271 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7272 if (arg5 != 0) {
7273 target_to_host_timespec(&ts, arg5);
7274 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7275 host_to_target_timespec(arg5, &ts);
7276 }
7277 else
7278 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7279 unlock_user (p, arg2, arg3);
7280 }
7281 break;
7282
7283 case TARGET_NR_mq_timedreceive:
7284 {
7285 struct timespec ts;
7286 unsigned int prio;
7287
7288 p = lock_user (VERIFY_READ, arg2, arg3, 1);
7289 if (arg5 != 0) {
7290 target_to_host_timespec(&ts, arg5);
7291 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7292 host_to_target_timespec(arg5, &ts);
7293 }
7294 else
7295 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7296 unlock_user (p, arg2, arg3);
7297 if (arg4 != 0)
7298 put_user_u32(prio, arg4);
7299 }
7300 break;
7301
7302 /* Not implemented for now... */
7303/* case TARGET_NR_mq_notify: */
7304/* break; */
7305
7306 case TARGET_NR_mq_getsetattr:
7307 {
7308 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7309 ret = 0;
7310 if (arg3 != 0) {
7311 ret = mq_getattr(arg1, &posix_mq_attr_out);
7312 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7313 }
7314 if (arg2 != 0) {
7315 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7316 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7317 }
7318
7319 }
7320 break;
7321#endif
7322
3ce34dfb
VS
7323#ifdef CONFIG_SPLICE
7324#ifdef TARGET_NR_tee
7325 case TARGET_NR_tee:
7326 {
7327 ret = get_errno(tee(arg1,arg2,arg3,arg4));
7328 }
7329 break;
7330#endif
7331#ifdef TARGET_NR_splice
7332 case TARGET_NR_splice:
7333 {
7334 loff_t loff_in, loff_out;
7335 loff_t *ploff_in = NULL, *ploff_out = NULL;
7336 if(arg2) {
7337 get_user_u64(loff_in, arg2);
7338 ploff_in = &loff_in;
7339 }
7340 if(arg4) {
7341 get_user_u64(loff_out, arg2);
7342 ploff_out = &loff_out;
7343 }
7344 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7345 }
7346 break;
7347#endif
7348#ifdef TARGET_NR_vmsplice
7349 case TARGET_NR_vmsplice:
7350 {
7351 int count = arg3;
7352 struct iovec *vec;
7353
7354 vec = alloca(count * sizeof(struct iovec));
7355 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7356 goto efault;
7357 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7358 unlock_iovec(vec, arg2, count, 0);
7359 }
7360 break;
7361#endif
7362#endif /* CONFIG_SPLICE */
c2882b96
RV
7363#ifdef CONFIG_EVENTFD
7364#if defined(TARGET_NR_eventfd)
7365 case TARGET_NR_eventfd:
7366 ret = get_errno(eventfd(arg1, 0));
7367 break;
7368#endif
7369#if defined(TARGET_NR_eventfd2)
7370 case TARGET_NR_eventfd2:
7371 ret = get_errno(eventfd(arg1, arg2));
7372 break;
7373#endif
7374#endif /* CONFIG_EVENTFD */
d0927938
UH
7375#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7376 case TARGET_NR_fallocate:
7377 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7378 break;
c727f47d
PM
7379#endif
7380#if defined(CONFIG_SYNC_FILE_RANGE)
7381#if defined(TARGET_NR_sync_file_range)
7382 case TARGET_NR_sync_file_range:
7383#if TARGET_ABI_BITS == 32
7384 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
7385 target_offset64(arg4, arg5), arg6));
7386#else
7387 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
7388#endif
7389 break;
7390#endif
7391#if defined(TARGET_NR_sync_file_range2)
7392 case TARGET_NR_sync_file_range2:
7393 /* This is like sync_file_range but the arguments are reordered */
7394#if TARGET_ABI_BITS == 32
7395 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
7396 target_offset64(arg5, arg6), arg2));
7397#else
7398 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
7399#endif
7400 break;
7401#endif
d0927938 7402#endif
31e31b8a
FB
7403 default:
7404 unimplemented:
5cd4393b 7405 gemu_log("qemu: Unsupported syscall: %d\n", num);
4f2b1fe8 7406#if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5cd4393b 7407 unimplemented_nowarn:
80a9d035 7408#endif
0da46a6e 7409 ret = -TARGET_ENOSYS;
31e31b8a
FB
7410 break;
7411 }
579a97f7 7412fail:
c573ff67 7413#ifdef DEBUG
0bf9e31a 7414 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
c573ff67 7415#endif
b92c47c1
TS
7416 if(do_strace)
7417 print_syscall_ret(num, ret);
31e31b8a 7418 return ret;
579a97f7
FB
7419efault:
7420 ret = -TARGET_EFAULT;
7421 goto fail;
31e31b8a 7422}