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