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