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