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