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