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