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