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