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