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