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