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