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