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