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