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