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