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