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