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