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"
29 #include <sys/mount.h>
31 #include <sys/fsuid.h>
32 #include <sys/personality.h>
33 #include <sys/prctl.h>
34 #include <sys/resource.h>
36 #include <linux/capability.h>
39 int __clone2(int (*fn
)(void *), void *child_stack_base
,
40 size_t stack_size
, int flags
, void *arg
, ...);
42 #include <sys/socket.h>
46 #include <sys/times.h>
49 #include <sys/statfs.h>
51 #include <sys/sysinfo.h>
52 #include <sys/signalfd.h>
53 //#include <sys/user.h>
54 #include <netinet/ip.h>
55 #include <netinet/tcp.h>
56 #include <linux/wireless.h>
57 #include <linux/icmp.h>
58 #include "qemu-common.h"
60 #include <sys/timerfd.h>
66 #include <sys/eventfd.h>
69 #include <sys/epoll.h>
72 #include "qemu/xattr.h"
74 #ifdef CONFIG_SENDFILE
75 #include <sys/sendfile.h>
78 #define termios host_termios
79 #define winsize host_winsize
80 #define termio host_termio
81 #define sgttyb host_sgttyb /* same as target */
82 #define tchars host_tchars /* same as target */
83 #define ltchars host_ltchars /* same as target */
85 #include <linux/termios.h>
86 #include <linux/unistd.h>
87 #include <linux/cdrom.h>
88 #include <linux/hdreg.h>
89 #include <linux/soundcard.h>
91 #include <linux/mtio.h>
93 #if defined(CONFIG_FIEMAP)
94 #include <linux/fiemap.h>
98 #include <linux/dm-ioctl.h>
99 #include <linux/reboot.h>
100 #include <linux/route.h>
101 #include <linux/filter.h>
102 #include <linux/blkpg.h>
103 #include <netpacket/packet.h>
104 #include <linux/netlink.h>
105 #ifdef CONFIG_RTNETLINK
106 #include <linux/rtnetlink.h>
107 #include <linux/if_bridge.h>
109 #include <linux/audit.h>
110 #include "linux_loop.h"
115 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
116 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
119 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
120 * once. This exercises the codepaths for restart.
122 //#define DEBUG_ERESTARTSYS
124 //#include <linux/msdos_fs.h>
125 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
126 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
136 #define _syscall0(type,name) \
137 static type name (void) \
139 return syscall(__NR_##name); \
142 #define _syscall1(type,name,type1,arg1) \
143 static type name (type1 arg1) \
145 return syscall(__NR_##name, arg1); \
148 #define _syscall2(type,name,type1,arg1,type2,arg2) \
149 static type name (type1 arg1,type2 arg2) \
151 return syscall(__NR_##name, arg1, arg2); \
154 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
155 static type name (type1 arg1,type2 arg2,type3 arg3) \
157 return syscall(__NR_##name, arg1, arg2, arg3); \
160 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
161 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
163 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
166 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
168 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
170 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
174 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
175 type5,arg5,type6,arg6) \
176 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
179 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
183 #define __NR_sys_uname __NR_uname
184 #define __NR_sys_getcwd1 __NR_getcwd
185 #define __NR_sys_getdents __NR_getdents
186 #define __NR_sys_getdents64 __NR_getdents64
187 #define __NR_sys_getpriority __NR_getpriority
188 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
189 #define __NR_sys_syslog __NR_syslog
190 #define __NR_sys_futex __NR_futex
191 #define __NR_sys_inotify_init __NR_inotify_init
192 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
193 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
195 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
197 #define __NR__llseek __NR_lseek
200 /* Newer kernel ports have llseek() instead of _llseek() */
201 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
202 #define TARGET_NR__llseek TARGET_NR_llseek
206 _syscall0(int, gettid
)
208 /* This is a replacement for the host gettid() and must return a host
210 static int gettid(void) {
214 #if defined(TARGET_NR_getdents) && defined(__NR_getdents)
215 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
217 #if !defined(__NR_getdents) || \
218 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
219 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
221 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
222 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
223 loff_t
*, res
, uint
, wh
);
225 _syscall3(int,sys_rt_sigqueueinfo
,int,pid
,int,sig
,siginfo_t
*,uinfo
)
226 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
227 #ifdef __NR_exit_group
228 _syscall1(int,exit_group
,int,error_code
)
230 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
231 _syscall1(int,set_tid_address
,int *,tidptr
)
233 #if defined(TARGET_NR_futex) && defined(__NR_futex)
234 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
235 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
237 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
238 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
239 unsigned long *, user_mask_ptr
);
240 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
241 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
242 unsigned long *, user_mask_ptr
);
243 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
245 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
246 struct __user_cap_data_struct
*, data
);
247 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
248 struct __user_cap_data_struct
*, data
);
249 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
250 _syscall2(int, ioprio_get
, int, which
, int, who
)
252 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
253 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
255 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
256 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
259 static bitmask_transtbl fcntl_flags_tbl
[] = {
260 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
261 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
262 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
263 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
264 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
265 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
266 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
267 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
268 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
269 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
270 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
271 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
272 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
273 #if defined(O_DIRECT)
274 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
276 #if defined(O_NOATIME)
277 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
279 #if defined(O_CLOEXEC)
280 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
283 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
285 /* Don't terminate the list prematurely on 64-bit host+guest. */
286 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
287 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
292 typedef abi_long (*TargetFdDataFunc
)(void *, size_t);
293 typedef abi_long (*TargetFdAddrFunc
)(void *, abi_ulong
, socklen_t
);
294 typedef struct TargetFdTrans
{
295 TargetFdDataFunc host_to_target_data
;
296 TargetFdDataFunc target_to_host_data
;
297 TargetFdAddrFunc target_to_host_addr
;
300 static TargetFdTrans
**target_fd_trans
;
302 static unsigned int target_fd_max
;
304 static TargetFdDataFunc
fd_trans_target_to_host_data(int fd
)
306 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
307 return target_fd_trans
[fd
]->target_to_host_data
;
312 static TargetFdDataFunc
fd_trans_host_to_target_data(int fd
)
314 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
315 return target_fd_trans
[fd
]->host_to_target_data
;
320 static TargetFdAddrFunc
fd_trans_target_to_host_addr(int fd
)
322 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
323 return target_fd_trans
[fd
]->target_to_host_addr
;
328 static void fd_trans_register(int fd
, TargetFdTrans
*trans
)
332 if (fd
>= target_fd_max
) {
333 oldmax
= target_fd_max
;
334 target_fd_max
= ((fd
>> 6) + 1) << 6; /* by slice of 64 entries */
335 target_fd_trans
= g_renew(TargetFdTrans
*,
336 target_fd_trans
, target_fd_max
);
337 memset((void *)(target_fd_trans
+ oldmax
), 0,
338 (target_fd_max
- oldmax
) * sizeof(TargetFdTrans
*));
340 target_fd_trans
[fd
] = trans
;
343 static void fd_trans_unregister(int fd
)
345 if (fd
>= 0 && fd
< target_fd_max
) {
346 target_fd_trans
[fd
] = NULL
;
350 static void fd_trans_dup(int oldfd
, int newfd
)
352 fd_trans_unregister(newfd
);
353 if (oldfd
< target_fd_max
&& target_fd_trans
[oldfd
]) {
354 fd_trans_register(newfd
, target_fd_trans
[oldfd
]);
358 static int sys_getcwd1(char *buf
, size_t size
)
360 if (getcwd(buf
, size
) == NULL
) {
361 /* getcwd() sets errno */
364 return strlen(buf
)+1;
367 #ifdef TARGET_NR_utimensat
368 #ifdef CONFIG_UTIMENSAT
369 static int sys_utimensat(int dirfd
, const char *pathname
,
370 const struct timespec times
[2], int flags
)
372 if (pathname
== NULL
)
373 return futimens(dirfd
, times
);
375 return utimensat(dirfd
, pathname
, times
, flags
);
377 #elif defined(__NR_utimensat)
378 #define __NR_sys_utimensat __NR_utimensat
379 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
380 const struct timespec
*,tsp
,int,flags
)
382 static int sys_utimensat(int dirfd
, const char *pathname
,
383 const struct timespec times
[2], int flags
)
389 #endif /* TARGET_NR_utimensat */
391 #ifdef CONFIG_INOTIFY
392 #include <sys/inotify.h>
394 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
395 static int sys_inotify_init(void)
397 return (inotify_init());
400 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
401 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
403 return (inotify_add_watch(fd
, pathname
, mask
));
406 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
407 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
409 return (inotify_rm_watch(fd
, wd
));
412 #ifdef CONFIG_INOTIFY1
413 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
414 static int sys_inotify_init1(int flags
)
416 return (inotify_init1(flags
));
421 /* Userspace can usually survive runtime without inotify */
422 #undef TARGET_NR_inotify_init
423 #undef TARGET_NR_inotify_init1
424 #undef TARGET_NR_inotify_add_watch
425 #undef TARGET_NR_inotify_rm_watch
426 #endif /* CONFIG_INOTIFY */
428 #if defined(TARGET_NR_prlimit64)
429 #ifndef __NR_prlimit64
430 # define __NR_prlimit64 -1
432 #define __NR_sys_prlimit64 __NR_prlimit64
433 /* The glibc rlimit structure may not be that used by the underlying syscall */
434 struct host_rlimit64
{
438 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
439 const struct host_rlimit64
*, new_limit
,
440 struct host_rlimit64
*, old_limit
)
444 #if defined(TARGET_NR_timer_create)
445 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
446 static timer_t g_posix_timers
[32] = { 0, } ;
448 static inline int next_free_host_timer(void)
451 /* FIXME: Does finding the next free slot require a lock? */
452 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
453 if (g_posix_timers
[k
] == 0) {
454 g_posix_timers
[k
] = (timer_t
) 1;
462 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
464 static inline int regpairs_aligned(void *cpu_env
) {
465 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
467 #elif defined(TARGET_MIPS)
468 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
469 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
470 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
471 * of registers which translates to the same as ARM/MIPS, because we start with
473 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
475 static inline int regpairs_aligned(void *cpu_env
) { return 0; }
478 #define ERRNO_TABLE_SIZE 1200
480 /* target_to_host_errno_table[] is initialized from
481 * host_to_target_errno_table[] in syscall_init(). */
482 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
486 * This list is the union of errno values overridden in asm-<arch>/errno.h
487 * minus the errnos that are not actually generic to all archs.
489 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
490 [EAGAIN
] = TARGET_EAGAIN
,
491 [EIDRM
] = TARGET_EIDRM
,
492 [ECHRNG
] = TARGET_ECHRNG
,
493 [EL2NSYNC
] = TARGET_EL2NSYNC
,
494 [EL3HLT
] = TARGET_EL3HLT
,
495 [EL3RST
] = TARGET_EL3RST
,
496 [ELNRNG
] = TARGET_ELNRNG
,
497 [EUNATCH
] = TARGET_EUNATCH
,
498 [ENOCSI
] = TARGET_ENOCSI
,
499 [EL2HLT
] = TARGET_EL2HLT
,
500 [EDEADLK
] = TARGET_EDEADLK
,
501 [ENOLCK
] = TARGET_ENOLCK
,
502 [EBADE
] = TARGET_EBADE
,
503 [EBADR
] = TARGET_EBADR
,
504 [EXFULL
] = TARGET_EXFULL
,
505 [ENOANO
] = TARGET_ENOANO
,
506 [EBADRQC
] = TARGET_EBADRQC
,
507 [EBADSLT
] = TARGET_EBADSLT
,
508 [EBFONT
] = TARGET_EBFONT
,
509 [ENOSTR
] = TARGET_ENOSTR
,
510 [ENODATA
] = TARGET_ENODATA
,
511 [ETIME
] = TARGET_ETIME
,
512 [ENOSR
] = TARGET_ENOSR
,
513 [ENONET
] = TARGET_ENONET
,
514 [ENOPKG
] = TARGET_ENOPKG
,
515 [EREMOTE
] = TARGET_EREMOTE
,
516 [ENOLINK
] = TARGET_ENOLINK
,
517 [EADV
] = TARGET_EADV
,
518 [ESRMNT
] = TARGET_ESRMNT
,
519 [ECOMM
] = TARGET_ECOMM
,
520 [EPROTO
] = TARGET_EPROTO
,
521 [EDOTDOT
] = TARGET_EDOTDOT
,
522 [EMULTIHOP
] = TARGET_EMULTIHOP
,
523 [EBADMSG
] = TARGET_EBADMSG
,
524 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
525 [EOVERFLOW
] = TARGET_EOVERFLOW
,
526 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
527 [EBADFD
] = TARGET_EBADFD
,
528 [EREMCHG
] = TARGET_EREMCHG
,
529 [ELIBACC
] = TARGET_ELIBACC
,
530 [ELIBBAD
] = TARGET_ELIBBAD
,
531 [ELIBSCN
] = TARGET_ELIBSCN
,
532 [ELIBMAX
] = TARGET_ELIBMAX
,
533 [ELIBEXEC
] = TARGET_ELIBEXEC
,
534 [EILSEQ
] = TARGET_EILSEQ
,
535 [ENOSYS
] = TARGET_ENOSYS
,
536 [ELOOP
] = TARGET_ELOOP
,
537 [ERESTART
] = TARGET_ERESTART
,
538 [ESTRPIPE
] = TARGET_ESTRPIPE
,
539 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
540 [EUSERS
] = TARGET_EUSERS
,
541 [ENOTSOCK
] = TARGET_ENOTSOCK
,
542 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
543 [EMSGSIZE
] = TARGET_EMSGSIZE
,
544 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
545 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
546 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
547 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
548 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
549 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
550 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
551 [EADDRINUSE
] = TARGET_EADDRINUSE
,
552 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
553 [ENETDOWN
] = TARGET_ENETDOWN
,
554 [ENETUNREACH
] = TARGET_ENETUNREACH
,
555 [ENETRESET
] = TARGET_ENETRESET
,
556 [ECONNABORTED
] = TARGET_ECONNABORTED
,
557 [ECONNRESET
] = TARGET_ECONNRESET
,
558 [ENOBUFS
] = TARGET_ENOBUFS
,
559 [EISCONN
] = TARGET_EISCONN
,
560 [ENOTCONN
] = TARGET_ENOTCONN
,
561 [EUCLEAN
] = TARGET_EUCLEAN
,
562 [ENOTNAM
] = TARGET_ENOTNAM
,
563 [ENAVAIL
] = TARGET_ENAVAIL
,
564 [EISNAM
] = TARGET_EISNAM
,
565 [EREMOTEIO
] = TARGET_EREMOTEIO
,
566 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
567 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
568 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
569 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
570 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
571 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
572 [EALREADY
] = TARGET_EALREADY
,
573 [EINPROGRESS
] = TARGET_EINPROGRESS
,
574 [ESTALE
] = TARGET_ESTALE
,
575 [ECANCELED
] = TARGET_ECANCELED
,
576 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
577 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
579 [ENOKEY
] = TARGET_ENOKEY
,
582 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
585 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
588 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
591 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
593 #ifdef ENOTRECOVERABLE
594 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
598 static inline int host_to_target_errno(int err
)
600 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
601 host_to_target_errno_table
[err
]) {
602 return host_to_target_errno_table
[err
];
607 static inline int target_to_host_errno(int err
)
609 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
610 target_to_host_errno_table
[err
]) {
611 return target_to_host_errno_table
[err
];
616 static inline abi_long
get_errno(abi_long ret
)
619 return -host_to_target_errno(errno
);
624 static inline int is_error(abi_long ret
)
626 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
629 const char *target_strerror(int err
)
631 if (err
== TARGET_ERESTARTSYS
) {
632 return "To be restarted";
634 if (err
== TARGET_QEMU_ESIGRETURN
) {
635 return "Successful exit from sigreturn";
638 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
641 return strerror(target_to_host_errno(err
));
644 #define safe_syscall0(type, name) \
645 static type safe_##name(void) \
647 return safe_syscall(__NR_##name); \
650 #define safe_syscall1(type, name, type1, arg1) \
651 static type safe_##name(type1 arg1) \
653 return safe_syscall(__NR_##name, arg1); \
656 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
657 static type safe_##name(type1 arg1, type2 arg2) \
659 return safe_syscall(__NR_##name, arg1, arg2); \
662 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
663 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
665 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
668 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
670 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
672 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
675 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
676 type4, arg4, type5, arg5) \
677 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
680 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
683 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
684 type4, arg4, type5, arg5, type6, arg6) \
685 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
686 type5 arg5, type6 arg6) \
688 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
691 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
692 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
693 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
694 int, flags
, mode_t
, mode
)
695 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
696 struct rusage
*, rusage
)
697 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
698 int, options
, struct rusage
*, rusage
)
699 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
700 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
701 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
702 safe_syscall5(int, ppoll
, struct pollfd
*, ufds
, unsigned int, nfds
,
703 struct timespec
*, tsp
, const sigset_t
*, sigmask
,
705 safe_syscall6(int, epoll_pwait
, int, epfd
, struct epoll_event
*, events
,
706 int, maxevents
, int, timeout
, const sigset_t
*, sigmask
,
708 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
709 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
710 safe_syscall2(int, rt_sigsuspend
, sigset_t
*, newset
, size_t, sigsetsize
)
711 safe_syscall2(int, kill
, pid_t
, pid
, int, sig
)
712 safe_syscall2(int, tkill
, int, tid
, int, sig
)
713 safe_syscall3(int, tgkill
, int, tgid
, int, pid
, int, sig
)
714 safe_syscall3(ssize_t
, readv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
715 safe_syscall3(ssize_t
, writev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
716 safe_syscall3(int, connect
, int, fd
, const struct sockaddr
*, addr
,
718 safe_syscall6(ssize_t
, sendto
, int, fd
, const void *, buf
, size_t, len
,
719 int, flags
, const struct sockaddr
*, addr
, socklen_t
, addrlen
)
720 safe_syscall6(ssize_t
, recvfrom
, int, fd
, void *, buf
, size_t, len
,
721 int, flags
, struct sockaddr
*, addr
, socklen_t
*, addrlen
)
722 safe_syscall3(ssize_t
, sendmsg
, int, fd
, const struct msghdr
*, msg
, int, flags
)
723 safe_syscall3(ssize_t
, recvmsg
, int, fd
, struct msghdr
*, msg
, int, flags
)
724 safe_syscall2(int, flock
, int, fd
, int, operation
)
725 safe_syscall4(int, rt_sigtimedwait
, const sigset_t
*, these
, siginfo_t
*, uinfo
,
726 const struct timespec
*, uts
, size_t, sigsetsize
)
727 safe_syscall4(int, accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, len
,
729 safe_syscall2(int, nanosleep
, const struct timespec
*, req
,
730 struct timespec
*, rem
)
731 #ifdef TARGET_NR_clock_nanosleep
732 safe_syscall4(int, clock_nanosleep
, const clockid_t
, clock
, int, flags
,
733 const struct timespec
*, req
, struct timespec
*, rem
)
736 safe_syscall4(int, msgsnd
, int, msgid
, const void *, msgp
, size_t, sz
,
738 safe_syscall5(int, msgrcv
, int, msgid
, void *, msgp
, size_t, sz
,
739 long, msgtype
, int, flags
)
740 safe_syscall4(int, semtimedop
, int, semid
, struct sembuf
*, tsops
,
741 unsigned, nsops
, const struct timespec
*, timeout
)
743 /* This host kernel architecture uses a single ipc syscall; fake up
744 * wrappers for the sub-operations to hide this implementation detail.
745 * Annoyingly we can't include linux/ipc.h to get the constant definitions
746 * for the call parameter because some structs in there conflict with the
747 * sys/ipc.h ones. So we just define them here, and rely on them being
748 * the same for all host architectures.
750 #define Q_SEMTIMEDOP 4
753 #define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP))
755 safe_syscall6(int, ipc
, int, call
, long, first
, long, second
, long, third
,
756 void *, ptr
, long, fifth
)
757 static int safe_msgsnd(int msgid
, const void *msgp
, size_t sz
, int flags
)
759 return safe_ipc(Q_IPCCALL(0, Q_MSGSND
), msgid
, sz
, flags
, (void *)msgp
, 0);
761 static int safe_msgrcv(int msgid
, void *msgp
, size_t sz
, long type
, int flags
)
763 return safe_ipc(Q_IPCCALL(1, Q_MSGRCV
), msgid
, sz
, flags
, msgp
, type
);
765 static int safe_semtimedop(int semid
, struct sembuf
*tsops
, unsigned nsops
,
766 const struct timespec
*timeout
)
768 return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP
), semid
, nsops
, 0, tsops
,
772 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
773 safe_syscall5(int, mq_timedsend
, int, mqdes
, const char *, msg_ptr
,
774 size_t, len
, unsigned, prio
, const struct timespec
*, timeout
)
775 safe_syscall5(int, mq_timedreceive
, int, mqdes
, char *, msg_ptr
,
776 size_t, len
, unsigned *, prio
, const struct timespec
*, timeout
)
778 /* We do ioctl like this rather than via safe_syscall3 to preserve the
779 * "third argument might be integer or pointer or not present" behaviour of
782 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
783 /* Similarly for fcntl. Note that callers must always:
784 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
785 * use the flock64 struct rather than unsuffixed flock
786 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
789 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
791 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
794 static inline int host_to_target_sock_type(int host_type
)
798 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
800 target_type
= TARGET_SOCK_DGRAM
;
803 target_type
= TARGET_SOCK_STREAM
;
806 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
810 #if defined(SOCK_CLOEXEC)
811 if (host_type
& SOCK_CLOEXEC
) {
812 target_type
|= TARGET_SOCK_CLOEXEC
;
816 #if defined(SOCK_NONBLOCK)
817 if (host_type
& SOCK_NONBLOCK
) {
818 target_type
|= TARGET_SOCK_NONBLOCK
;
825 static abi_ulong target_brk
;
826 static abi_ulong target_original_brk
;
827 static abi_ulong brk_page
;
829 void target_set_brk(abi_ulong new_brk
)
831 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
832 brk_page
= HOST_PAGE_ALIGN(target_brk
);
835 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
836 #define DEBUGF_BRK(message, args...)
838 /* do_brk() must return target values and target errnos. */
839 abi_long
do_brk(abi_ulong new_brk
)
841 abi_long mapped_addr
;
844 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
847 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
850 if (new_brk
< target_original_brk
) {
851 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
856 /* If the new brk is less than the highest page reserved to the
857 * target heap allocation, set it and we're almost done... */
858 if (new_brk
<= brk_page
) {
859 /* Heap contents are initialized to zero, as for anonymous
861 if (new_brk
> target_brk
) {
862 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
864 target_brk
= new_brk
;
865 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
869 /* We need to allocate more memory after the brk... Note that
870 * we don't use MAP_FIXED because that will map over the top of
871 * any existing mapping (like the one with the host libc or qemu
872 * itself); instead we treat "mapped but at wrong address" as
873 * a failure and unmap again.
875 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
876 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
877 PROT_READ
|PROT_WRITE
,
878 MAP_ANON
|MAP_PRIVATE
, 0, 0));
880 if (mapped_addr
== brk_page
) {
881 /* Heap contents are initialized to zero, as for anonymous
882 * mapped pages. Technically the new pages are already
883 * initialized to zero since they *are* anonymous mapped
884 * pages, however we have to take care with the contents that
885 * come from the remaining part of the previous page: it may
886 * contains garbage data due to a previous heap usage (grown
888 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
890 target_brk
= new_brk
;
891 brk_page
= HOST_PAGE_ALIGN(target_brk
);
892 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
895 } else if (mapped_addr
!= -1) {
896 /* Mapped but at wrong address, meaning there wasn't actually
897 * enough space for this brk.
899 target_munmap(mapped_addr
, new_alloc_size
);
901 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
904 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
907 #if defined(TARGET_ALPHA)
908 /* We (partially) emulate OSF/1 on Alpha, which requires we
909 return a proper errno, not an unchanged brk value. */
910 return -TARGET_ENOMEM
;
912 /* For everything else, return the previous break. */
916 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
917 abi_ulong target_fds_addr
,
921 abi_ulong b
, *target_fds
;
923 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
924 if (!(target_fds
= lock_user(VERIFY_READ
,
926 sizeof(abi_ulong
) * nw
,
928 return -TARGET_EFAULT
;
932 for (i
= 0; i
< nw
; i
++) {
933 /* grab the abi_ulong */
934 __get_user(b
, &target_fds
[i
]);
935 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
936 /* check the bit inside the abi_ulong */
943 unlock_user(target_fds
, target_fds_addr
, 0);
948 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
949 abi_ulong target_fds_addr
,
952 if (target_fds_addr
) {
953 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
954 return -TARGET_EFAULT
;
962 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
968 abi_ulong
*target_fds
;
970 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
971 if (!(target_fds
= lock_user(VERIFY_WRITE
,
973 sizeof(abi_ulong
) * nw
,
975 return -TARGET_EFAULT
;
978 for (i
= 0; i
< nw
; i
++) {
980 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
981 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
984 __put_user(v
, &target_fds
[i
]);
987 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
992 #if defined(__alpha__)
998 static inline abi_long
host_to_target_clock_t(long ticks
)
1000 #if HOST_HZ == TARGET_HZ
1003 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
1007 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
1008 const struct rusage
*rusage
)
1010 struct target_rusage
*target_rusage
;
1012 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
1013 return -TARGET_EFAULT
;
1014 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
1015 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
1016 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
1017 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
1018 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
1019 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
1020 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
1021 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
1022 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
1023 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
1024 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
1025 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
1026 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
1027 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
1028 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
1029 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
1030 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
1031 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
1032 unlock_user_struct(target_rusage
, target_addr
, 1);
1037 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
1039 abi_ulong target_rlim_swap
;
1042 target_rlim_swap
= tswapal(target_rlim
);
1043 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
1044 return RLIM_INFINITY
;
1046 result
= target_rlim_swap
;
1047 if (target_rlim_swap
!= (rlim_t
)result
)
1048 return RLIM_INFINITY
;
1053 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1055 abi_ulong target_rlim_swap
;
1058 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1059 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1061 target_rlim_swap
= rlim
;
1062 result
= tswapal(target_rlim_swap
);
1067 static inline int target_to_host_resource(int code
)
1070 case TARGET_RLIMIT_AS
:
1072 case TARGET_RLIMIT_CORE
:
1074 case TARGET_RLIMIT_CPU
:
1076 case TARGET_RLIMIT_DATA
:
1078 case TARGET_RLIMIT_FSIZE
:
1079 return RLIMIT_FSIZE
;
1080 case TARGET_RLIMIT_LOCKS
:
1081 return RLIMIT_LOCKS
;
1082 case TARGET_RLIMIT_MEMLOCK
:
1083 return RLIMIT_MEMLOCK
;
1084 case TARGET_RLIMIT_MSGQUEUE
:
1085 return RLIMIT_MSGQUEUE
;
1086 case TARGET_RLIMIT_NICE
:
1088 case TARGET_RLIMIT_NOFILE
:
1089 return RLIMIT_NOFILE
;
1090 case TARGET_RLIMIT_NPROC
:
1091 return RLIMIT_NPROC
;
1092 case TARGET_RLIMIT_RSS
:
1094 case TARGET_RLIMIT_RTPRIO
:
1095 return RLIMIT_RTPRIO
;
1096 case TARGET_RLIMIT_SIGPENDING
:
1097 return RLIMIT_SIGPENDING
;
1098 case TARGET_RLIMIT_STACK
:
1099 return RLIMIT_STACK
;
1105 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1106 abi_ulong target_tv_addr
)
1108 struct target_timeval
*target_tv
;
1110 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
1111 return -TARGET_EFAULT
;
1113 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1114 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1116 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1121 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1122 const struct timeval
*tv
)
1124 struct target_timeval
*target_tv
;
1126 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
1127 return -TARGET_EFAULT
;
1129 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1130 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1132 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1137 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1138 abi_ulong target_tz_addr
)
1140 struct target_timezone
*target_tz
;
1142 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1143 return -TARGET_EFAULT
;
1146 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1147 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1149 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1154 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1157 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1158 abi_ulong target_mq_attr_addr
)
1160 struct target_mq_attr
*target_mq_attr
;
1162 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1163 target_mq_attr_addr
, 1))
1164 return -TARGET_EFAULT
;
1166 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1167 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1168 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1169 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1171 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1176 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1177 const struct mq_attr
*attr
)
1179 struct target_mq_attr
*target_mq_attr
;
1181 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1182 target_mq_attr_addr
, 0))
1183 return -TARGET_EFAULT
;
1185 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1186 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1187 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1188 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1190 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1196 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1197 /* do_select() must return target values and target errnos. */
1198 static abi_long
do_select(int n
,
1199 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1200 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1202 fd_set rfds
, wfds
, efds
;
1203 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1205 struct timespec ts
, *ts_ptr
;
1208 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1212 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1216 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1221 if (target_tv_addr
) {
1222 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1223 return -TARGET_EFAULT
;
1224 ts
.tv_sec
= tv
.tv_sec
;
1225 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1231 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1234 if (!is_error(ret
)) {
1235 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1236 return -TARGET_EFAULT
;
1237 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1238 return -TARGET_EFAULT
;
1239 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1240 return -TARGET_EFAULT
;
1242 if (target_tv_addr
) {
1243 tv
.tv_sec
= ts
.tv_sec
;
1244 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1245 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1246 return -TARGET_EFAULT
;
1255 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1258 return pipe2(host_pipe
, flags
);
1264 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1265 int flags
, int is_pipe2
)
1269 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1272 return get_errno(ret
);
1274 /* Several targets have special calling conventions for the original
1275 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1277 #if defined(TARGET_ALPHA)
1278 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1279 return host_pipe
[0];
1280 #elif defined(TARGET_MIPS)
1281 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1282 return host_pipe
[0];
1283 #elif defined(TARGET_SH4)
1284 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1285 return host_pipe
[0];
1286 #elif defined(TARGET_SPARC)
1287 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1288 return host_pipe
[0];
1292 if (put_user_s32(host_pipe
[0], pipedes
)
1293 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1294 return -TARGET_EFAULT
;
1295 return get_errno(ret
);
1298 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1299 abi_ulong target_addr
,
1302 struct target_ip_mreqn
*target_smreqn
;
1304 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1306 return -TARGET_EFAULT
;
1307 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1308 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1309 if (len
== sizeof(struct target_ip_mreqn
))
1310 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1311 unlock_user(target_smreqn
, target_addr
, 0);
1316 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1317 abi_ulong target_addr
,
1320 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1321 sa_family_t sa_family
;
1322 struct target_sockaddr
*target_saddr
;
1324 if (fd_trans_target_to_host_addr(fd
)) {
1325 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1328 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1330 return -TARGET_EFAULT
;
1332 sa_family
= tswap16(target_saddr
->sa_family
);
1334 /* Oops. The caller might send a incomplete sun_path; sun_path
1335 * must be terminated by \0 (see the manual page), but
1336 * unfortunately it is quite common to specify sockaddr_un
1337 * length as "strlen(x->sun_path)" while it should be
1338 * "strlen(...) + 1". We'll fix that here if needed.
1339 * Linux kernel has a similar feature.
1342 if (sa_family
== AF_UNIX
) {
1343 if (len
< unix_maxlen
&& len
> 0) {
1344 char *cp
= (char*)target_saddr
;
1346 if ( cp
[len
-1] && !cp
[len
] )
1349 if (len
> unix_maxlen
)
1353 memcpy(addr
, target_saddr
, len
);
1354 addr
->sa_family
= sa_family
;
1355 if (sa_family
== AF_NETLINK
) {
1356 struct sockaddr_nl
*nladdr
;
1358 nladdr
= (struct sockaddr_nl
*)addr
;
1359 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1360 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1361 } else if (sa_family
== AF_PACKET
) {
1362 struct target_sockaddr_ll
*lladdr
;
1364 lladdr
= (struct target_sockaddr_ll
*)addr
;
1365 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1366 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1368 unlock_user(target_saddr
, target_addr
, 0);
1373 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1374 struct sockaddr
*addr
,
1377 struct target_sockaddr
*target_saddr
;
1383 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1385 return -TARGET_EFAULT
;
1386 memcpy(target_saddr
, addr
, len
);
1387 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1388 sizeof(target_saddr
->sa_family
)) {
1389 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1391 if (addr
->sa_family
== AF_NETLINK
&& len
>= sizeof(struct sockaddr_nl
)) {
1392 struct sockaddr_nl
*target_nl
= (struct sockaddr_nl
*)target_saddr
;
1393 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1394 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1395 } else if (addr
->sa_family
== AF_PACKET
) {
1396 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1397 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1398 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1400 unlock_user(target_saddr
, target_addr
, len
);
1405 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1406 struct target_msghdr
*target_msgh
)
1408 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1409 abi_long msg_controllen
;
1410 abi_ulong target_cmsg_addr
;
1411 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1412 socklen_t space
= 0;
1414 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1415 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1417 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1418 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1419 target_cmsg_start
= target_cmsg
;
1421 return -TARGET_EFAULT
;
1423 while (cmsg
&& target_cmsg
) {
1424 void *data
= CMSG_DATA(cmsg
);
1425 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1427 int len
= tswapal(target_cmsg
->cmsg_len
)
1428 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr
));
1430 space
+= CMSG_SPACE(len
);
1431 if (space
> msgh
->msg_controllen
) {
1432 space
-= CMSG_SPACE(len
);
1433 /* This is a QEMU bug, since we allocated the payload
1434 * area ourselves (unlike overflow in host-to-target
1435 * conversion, which is just the guest giving us a buffer
1436 * that's too small). It can't happen for the payload types
1437 * we currently support; if it becomes an issue in future
1438 * we would need to improve our allocation strategy to
1439 * something more intelligent than "twice the size of the
1440 * target buffer we're reading from".
1442 gemu_log("Host cmsg overflow\n");
1446 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1447 cmsg
->cmsg_level
= SOL_SOCKET
;
1449 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1451 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1452 cmsg
->cmsg_len
= CMSG_LEN(len
);
1454 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1455 int *fd
= (int *)data
;
1456 int *target_fd
= (int *)target_data
;
1457 int i
, numfds
= len
/ sizeof(int);
1459 for (i
= 0; i
< numfds
; i
++) {
1460 __get_user(fd
[i
], target_fd
+ i
);
1462 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1463 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1464 struct ucred
*cred
= (struct ucred
*)data
;
1465 struct target_ucred
*target_cred
=
1466 (struct target_ucred
*)target_data
;
1468 __get_user(cred
->pid
, &target_cred
->pid
);
1469 __get_user(cred
->uid
, &target_cred
->uid
);
1470 __get_user(cred
->gid
, &target_cred
->gid
);
1472 gemu_log("Unsupported ancillary data: %d/%d\n",
1473 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1474 memcpy(data
, target_data
, len
);
1477 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1478 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1481 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1483 msgh
->msg_controllen
= space
;
1487 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1488 struct msghdr
*msgh
)
1490 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1491 abi_long msg_controllen
;
1492 abi_ulong target_cmsg_addr
;
1493 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1494 socklen_t space
= 0;
1496 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1497 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1499 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1500 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1501 target_cmsg_start
= target_cmsg
;
1503 return -TARGET_EFAULT
;
1505 while (cmsg
&& target_cmsg
) {
1506 void *data
= CMSG_DATA(cmsg
);
1507 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1509 int len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof (struct cmsghdr
));
1510 int tgt_len
, tgt_space
;
1512 /* We never copy a half-header but may copy half-data;
1513 * this is Linux's behaviour in put_cmsg(). Note that
1514 * truncation here is a guest problem (which we report
1515 * to the guest via the CTRUNC bit), unlike truncation
1516 * in target_to_host_cmsg, which is a QEMU bug.
1518 if (msg_controllen
< sizeof(struct cmsghdr
)) {
1519 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1523 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1524 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1526 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1528 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1530 tgt_len
= TARGET_CMSG_LEN(len
);
1532 /* Payload types which need a different size of payload on
1533 * the target must adjust tgt_len here.
1535 switch (cmsg
->cmsg_level
) {
1537 switch (cmsg
->cmsg_type
) {
1539 tgt_len
= sizeof(struct target_timeval
);
1548 if (msg_controllen
< tgt_len
) {
1549 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1550 tgt_len
= msg_controllen
;
1553 /* We must now copy-and-convert len bytes of payload
1554 * into tgt_len bytes of destination space. Bear in mind
1555 * that in both source and destination we may be dealing
1556 * with a truncated value!
1558 switch (cmsg
->cmsg_level
) {
1560 switch (cmsg
->cmsg_type
) {
1563 int *fd
= (int *)data
;
1564 int *target_fd
= (int *)target_data
;
1565 int i
, numfds
= tgt_len
/ sizeof(int);
1567 for (i
= 0; i
< numfds
; i
++) {
1568 __put_user(fd
[i
], target_fd
+ i
);
1574 struct timeval
*tv
= (struct timeval
*)data
;
1575 struct target_timeval
*target_tv
=
1576 (struct target_timeval
*)target_data
;
1578 if (len
!= sizeof(struct timeval
) ||
1579 tgt_len
!= sizeof(struct target_timeval
)) {
1583 /* copy struct timeval to target */
1584 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1585 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1588 case SCM_CREDENTIALS
:
1590 struct ucred
*cred
= (struct ucred
*)data
;
1591 struct target_ucred
*target_cred
=
1592 (struct target_ucred
*)target_data
;
1594 __put_user(cred
->pid
, &target_cred
->pid
);
1595 __put_user(cred
->uid
, &target_cred
->uid
);
1596 __put_user(cred
->gid
, &target_cred
->gid
);
1606 gemu_log("Unsupported ancillary data: %d/%d\n",
1607 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1608 memcpy(target_data
, data
, MIN(len
, tgt_len
));
1609 if (tgt_len
> len
) {
1610 memset(target_data
+ len
, 0, tgt_len
- len
);
1614 target_cmsg
->cmsg_len
= tswapal(tgt_len
);
1615 tgt_space
= TARGET_CMSG_SPACE(len
);
1616 if (msg_controllen
< tgt_space
) {
1617 tgt_space
= msg_controllen
;
1619 msg_controllen
-= tgt_space
;
1621 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1622 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1625 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1627 target_msgh
->msg_controllen
= tswapal(space
);
1631 static void tswap_nlmsghdr(struct nlmsghdr
*nlh
)
1633 nlh
->nlmsg_len
= tswap32(nlh
->nlmsg_len
);
1634 nlh
->nlmsg_type
= tswap16(nlh
->nlmsg_type
);
1635 nlh
->nlmsg_flags
= tswap16(nlh
->nlmsg_flags
);
1636 nlh
->nlmsg_seq
= tswap32(nlh
->nlmsg_seq
);
1637 nlh
->nlmsg_pid
= tswap32(nlh
->nlmsg_pid
);
1640 static abi_long
host_to_target_for_each_nlmsg(struct nlmsghdr
*nlh
,
1642 abi_long (*host_to_target_nlmsg
)
1643 (struct nlmsghdr
*))
1648 while (len
> sizeof(struct nlmsghdr
)) {
1650 nlmsg_len
= nlh
->nlmsg_len
;
1651 if (nlmsg_len
< sizeof(struct nlmsghdr
) ||
1656 switch (nlh
->nlmsg_type
) {
1658 tswap_nlmsghdr(nlh
);
1664 struct nlmsgerr
*e
= NLMSG_DATA(nlh
);
1665 e
->error
= tswap32(e
->error
);
1666 tswap_nlmsghdr(&e
->msg
);
1667 tswap_nlmsghdr(nlh
);
1671 ret
= host_to_target_nlmsg(nlh
);
1673 tswap_nlmsghdr(nlh
);
1678 tswap_nlmsghdr(nlh
);
1679 len
-= NLMSG_ALIGN(nlmsg_len
);
1680 nlh
= (struct nlmsghdr
*)(((char*)nlh
) + NLMSG_ALIGN(nlmsg_len
));
1685 static abi_long
target_to_host_for_each_nlmsg(struct nlmsghdr
*nlh
,
1687 abi_long (*target_to_host_nlmsg
)
1688 (struct nlmsghdr
*))
1692 while (len
> sizeof(struct nlmsghdr
)) {
1693 if (tswap32(nlh
->nlmsg_len
) < sizeof(struct nlmsghdr
) ||
1694 tswap32(nlh
->nlmsg_len
) > len
) {
1697 tswap_nlmsghdr(nlh
);
1698 switch (nlh
->nlmsg_type
) {
1705 struct nlmsgerr
*e
= NLMSG_DATA(nlh
);
1706 e
->error
= tswap32(e
->error
);
1707 tswap_nlmsghdr(&e
->msg
);
1711 ret
= target_to_host_nlmsg(nlh
);
1716 len
-= NLMSG_ALIGN(nlh
->nlmsg_len
);
1717 nlh
= (struct nlmsghdr
*)(((char *)nlh
) + NLMSG_ALIGN(nlh
->nlmsg_len
));
1722 #ifdef CONFIG_RTNETLINK
1723 static abi_long
host_to_target_for_each_nlattr(struct nlattr
*nlattr
,
1724 size_t len
, void *context
,
1725 abi_long (*host_to_target_nlattr
)
1729 unsigned short nla_len
;
1732 while (len
> sizeof(struct nlattr
)) {
1733 nla_len
= nlattr
->nla_len
;
1734 if (nla_len
< sizeof(struct nlattr
) ||
1738 ret
= host_to_target_nlattr(nlattr
, context
);
1739 nlattr
->nla_len
= tswap16(nlattr
->nla_len
);
1740 nlattr
->nla_type
= tswap16(nlattr
->nla_type
);
1744 len
-= NLA_ALIGN(nla_len
);
1745 nlattr
= (struct nlattr
*)(((char *)nlattr
) + NLA_ALIGN(nla_len
));
1750 static abi_long
host_to_target_for_each_rtattr(struct rtattr
*rtattr
,
1752 abi_long (*host_to_target_rtattr
)
1755 unsigned short rta_len
;
1758 while (len
> sizeof(struct rtattr
)) {
1759 rta_len
= rtattr
->rta_len
;
1760 if (rta_len
< sizeof(struct rtattr
) ||
1764 ret
= host_to_target_rtattr(rtattr
);
1765 rtattr
->rta_len
= tswap16(rtattr
->rta_len
);
1766 rtattr
->rta_type
= tswap16(rtattr
->rta_type
);
1770 len
-= RTA_ALIGN(rta_len
);
1771 rtattr
= (struct rtattr
*)(((char *)rtattr
) + RTA_ALIGN(rta_len
));
1776 #define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
1778 static abi_long
host_to_target_data_bridge_nlattr(struct nlattr
*nlattr
,
1785 switch (nlattr
->nla_type
) {
1787 case IFLA_BR_FDB_FLUSH
:
1790 case IFLA_BR_GROUP_ADDR
:
1793 case IFLA_BR_VLAN_FILTERING
:
1794 case IFLA_BR_TOPOLOGY_CHANGE
:
1795 case IFLA_BR_TOPOLOGY_CHANGE_DETECTED
:
1796 case IFLA_BR_MCAST_ROUTER
:
1797 case IFLA_BR_MCAST_SNOOPING
:
1798 case IFLA_BR_MCAST_QUERY_USE_IFADDR
:
1799 case IFLA_BR_MCAST_QUERIER
:
1800 case IFLA_BR_NF_CALL_IPTABLES
:
1801 case IFLA_BR_NF_CALL_IP6TABLES
:
1802 case IFLA_BR_NF_CALL_ARPTABLES
:
1805 case IFLA_BR_PRIORITY
:
1806 case IFLA_BR_VLAN_PROTOCOL
:
1807 case IFLA_BR_GROUP_FWD_MASK
:
1808 case IFLA_BR_ROOT_PORT
:
1809 case IFLA_BR_VLAN_DEFAULT_PVID
:
1810 u16
= NLA_DATA(nlattr
);
1811 *u16
= tswap16(*u16
);
1814 case IFLA_BR_FORWARD_DELAY
:
1815 case IFLA_BR_HELLO_TIME
:
1816 case IFLA_BR_MAX_AGE
:
1817 case IFLA_BR_AGEING_TIME
:
1818 case IFLA_BR_STP_STATE
:
1819 case IFLA_BR_ROOT_PATH_COST
:
1820 case IFLA_BR_MCAST_HASH_ELASTICITY
:
1821 case IFLA_BR_MCAST_HASH_MAX
:
1822 case IFLA_BR_MCAST_LAST_MEMBER_CNT
:
1823 case IFLA_BR_MCAST_STARTUP_QUERY_CNT
:
1824 u32
= NLA_DATA(nlattr
);
1825 *u32
= tswap32(*u32
);
1828 case IFLA_BR_HELLO_TIMER
:
1829 case IFLA_BR_TCN_TIMER
:
1830 case IFLA_BR_GC_TIMER
:
1831 case IFLA_BR_TOPOLOGY_CHANGE_TIMER
:
1832 case IFLA_BR_MCAST_LAST_MEMBER_INTVL
:
1833 case IFLA_BR_MCAST_MEMBERSHIP_INTVL
:
1834 case IFLA_BR_MCAST_QUERIER_INTVL
:
1835 case IFLA_BR_MCAST_QUERY_INTVL
:
1836 case IFLA_BR_MCAST_QUERY_RESPONSE_INTVL
:
1837 case IFLA_BR_MCAST_STARTUP_QUERY_INTVL
:
1838 u64
= NLA_DATA(nlattr
);
1839 *u64
= tswap64(*u64
);
1841 /* ifla_bridge_id: uin8_t[] */
1842 case IFLA_BR_ROOT_ID
:
1843 case IFLA_BR_BRIDGE_ID
:
1846 gemu_log("Unknown IFLA_BR type %d\n", nlattr
->nla_type
);
1852 static abi_long
host_to_target_slave_data_bridge_nlattr(struct nlattr
*nlattr
,
1859 switch (nlattr
->nla_type
) {
1861 case IFLA_BRPORT_STATE
:
1862 case IFLA_BRPORT_MODE
:
1863 case IFLA_BRPORT_GUARD
:
1864 case IFLA_BRPORT_PROTECT
:
1865 case IFLA_BRPORT_FAST_LEAVE
:
1866 case IFLA_BRPORT_LEARNING
:
1867 case IFLA_BRPORT_UNICAST_FLOOD
:
1868 case IFLA_BRPORT_PROXYARP
:
1869 case IFLA_BRPORT_LEARNING_SYNC
:
1870 case IFLA_BRPORT_PROXYARP_WIFI
:
1871 case IFLA_BRPORT_TOPOLOGY_CHANGE_ACK
:
1872 case IFLA_BRPORT_CONFIG_PENDING
:
1873 case IFLA_BRPORT_MULTICAST_ROUTER
:
1876 case IFLA_BRPORT_PRIORITY
:
1877 case IFLA_BRPORT_DESIGNATED_PORT
:
1878 case IFLA_BRPORT_DESIGNATED_COST
:
1879 case IFLA_BRPORT_ID
:
1880 case IFLA_BRPORT_NO
:
1881 u16
= NLA_DATA(nlattr
);
1882 *u16
= tswap16(*u16
);
1885 case IFLA_BRPORT_COST
:
1886 u32
= NLA_DATA(nlattr
);
1887 *u32
= tswap32(*u32
);
1890 case IFLA_BRPORT_MESSAGE_AGE_TIMER
:
1891 case IFLA_BRPORT_FORWARD_DELAY_TIMER
:
1892 case IFLA_BRPORT_HOLD_TIMER
:
1893 u64
= NLA_DATA(nlattr
);
1894 *u64
= tswap64(*u64
);
1896 /* ifla_bridge_id: uint8_t[] */
1897 case IFLA_BRPORT_ROOT_ID
:
1898 case IFLA_BRPORT_BRIDGE_ID
:
1901 gemu_log("Unknown IFLA_BRPORT type %d\n", nlattr
->nla_type
);
1907 struct linkinfo_context
{
1914 static abi_long
host_to_target_data_linkinfo_nlattr(struct nlattr
*nlattr
,
1917 struct linkinfo_context
*li_context
= context
;
1919 switch (nlattr
->nla_type
) {
1921 case IFLA_INFO_KIND
:
1922 li_context
->name
= NLA_DATA(nlattr
);
1923 li_context
->len
= nlattr
->nla_len
- NLA_HDRLEN
;
1925 case IFLA_INFO_SLAVE_KIND
:
1926 li_context
->slave_name
= NLA_DATA(nlattr
);
1927 li_context
->slave_len
= nlattr
->nla_len
- NLA_HDRLEN
;
1930 case IFLA_INFO_XSTATS
:
1931 /* FIXME: only used by CAN */
1934 case IFLA_INFO_DATA
:
1935 if (strncmp(li_context
->name
, "bridge",
1936 li_context
->len
) == 0) {
1937 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
1940 host_to_target_data_bridge_nlattr
);
1942 gemu_log("Unknown IFLA_INFO_KIND %s\n", li_context
->name
);
1945 case IFLA_INFO_SLAVE_DATA
:
1946 if (strncmp(li_context
->slave_name
, "bridge",
1947 li_context
->slave_len
) == 0) {
1948 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
),
1951 host_to_target_slave_data_bridge_nlattr
);
1953 gemu_log("Unknown IFLA_INFO_SLAVE_KIND %s\n",
1954 li_context
->slave_name
);
1958 gemu_log("Unknown host IFLA_INFO type: %d\n", nlattr
->nla_type
);
1965 static abi_long
host_to_target_data_inet_nlattr(struct nlattr
*nlattr
,
1971 switch (nlattr
->nla_type
) {
1972 case IFLA_INET_CONF
:
1973 u32
= NLA_DATA(nlattr
);
1974 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u32
);
1976 u32
[i
] = tswap32(u32
[i
]);
1980 gemu_log("Unknown host AF_INET type: %d\n", nlattr
->nla_type
);
1985 static abi_long
host_to_target_data_inet6_nlattr(struct nlattr
*nlattr
,
1990 struct ifla_cacheinfo
*ci
;
1993 switch (nlattr
->nla_type
) {
1995 case IFLA_INET6_TOKEN
:
1998 case IFLA_INET6_ADDR_GEN_MODE
:
2001 case IFLA_INET6_FLAGS
:
2002 u32
= NLA_DATA(nlattr
);
2003 *u32
= tswap32(*u32
);
2006 case IFLA_INET6_CONF
:
2007 u32
= NLA_DATA(nlattr
);
2008 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u32
);
2010 u32
[i
] = tswap32(u32
[i
]);
2013 /* ifla_cacheinfo */
2014 case IFLA_INET6_CACHEINFO
:
2015 ci
= NLA_DATA(nlattr
);
2016 ci
->max_reasm_len
= tswap32(ci
->max_reasm_len
);
2017 ci
->tstamp
= tswap32(ci
->tstamp
);
2018 ci
->reachable_time
= tswap32(ci
->reachable_time
);
2019 ci
->retrans_time
= tswap32(ci
->retrans_time
);
2022 case IFLA_INET6_STATS
:
2023 case IFLA_INET6_ICMP6STATS
:
2024 u64
= NLA_DATA(nlattr
);
2025 for (i
= 0; i
< (nlattr
->nla_len
- NLA_HDRLEN
) / sizeof(*u64
);
2027 u64
[i
] = tswap64(u64
[i
]);
2031 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr
->nla_type
);
2036 static abi_long
host_to_target_data_spec_nlattr(struct nlattr
*nlattr
,
2039 switch (nlattr
->nla_type
) {
2041 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
), nlattr
->nla_len
,
2043 host_to_target_data_inet_nlattr
);
2045 return host_to_target_for_each_nlattr(NLA_DATA(nlattr
), nlattr
->nla_len
,
2047 host_to_target_data_inet6_nlattr
);
2049 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr
->nla_type
);
2055 static abi_long
host_to_target_data_link_rtattr(struct rtattr
*rtattr
)
2058 struct rtnl_link_stats
*st
;
2059 struct rtnl_link_stats64
*st64
;
2060 struct rtnl_link_ifmap
*map
;
2061 struct linkinfo_context li_context
;
2063 switch (rtattr
->rta_type
) {
2066 case IFLA_BROADCAST
:
2072 case IFLA_OPERSTATE
:
2075 case IFLA_PROTO_DOWN
:
2082 case IFLA_CARRIER_CHANGES
:
2083 case IFLA_NUM_RX_QUEUES
:
2084 case IFLA_NUM_TX_QUEUES
:
2085 case IFLA_PROMISCUITY
:
2087 case IFLA_LINK_NETNSID
:
2091 u32
= RTA_DATA(rtattr
);
2092 *u32
= tswap32(*u32
);
2094 /* struct rtnl_link_stats */
2096 st
= RTA_DATA(rtattr
);
2097 st
->rx_packets
= tswap32(st
->rx_packets
);
2098 st
->tx_packets
= tswap32(st
->tx_packets
);
2099 st
->rx_bytes
= tswap32(st
->rx_bytes
);
2100 st
->tx_bytes
= tswap32(st
->tx_bytes
);
2101 st
->rx_errors
= tswap32(st
->rx_errors
);
2102 st
->tx_errors
= tswap32(st
->tx_errors
);
2103 st
->rx_dropped
= tswap32(st
->rx_dropped
);
2104 st
->tx_dropped
= tswap32(st
->tx_dropped
);
2105 st
->multicast
= tswap32(st
->multicast
);
2106 st
->collisions
= tswap32(st
->collisions
);
2108 /* detailed rx_errors: */
2109 st
->rx_length_errors
= tswap32(st
->rx_length_errors
);
2110 st
->rx_over_errors
= tswap32(st
->rx_over_errors
);
2111 st
->rx_crc_errors
= tswap32(st
->rx_crc_errors
);
2112 st
->rx_frame_errors
= tswap32(st
->rx_frame_errors
);
2113 st
->rx_fifo_errors
= tswap32(st
->rx_fifo_errors
);
2114 st
->rx_missed_errors
= tswap32(st
->rx_missed_errors
);
2116 /* detailed tx_errors */
2117 st
->tx_aborted_errors
= tswap32(st
->tx_aborted_errors
);
2118 st
->tx_carrier_errors
= tswap32(st
->tx_carrier_errors
);
2119 st
->tx_fifo_errors
= tswap32(st
->tx_fifo_errors
);
2120 st
->tx_heartbeat_errors
= tswap32(st
->tx_heartbeat_errors
);
2121 st
->tx_window_errors
= tswap32(st
->tx_window_errors
);
2124 st
->rx_compressed
= tswap32(st
->rx_compressed
);
2125 st
->tx_compressed
= tswap32(st
->tx_compressed
);
2127 /* struct rtnl_link_stats64 */
2129 st64
= RTA_DATA(rtattr
);
2130 st64
->rx_packets
= tswap64(st64
->rx_packets
);
2131 st64
->tx_packets
= tswap64(st64
->tx_packets
);
2132 st64
->rx_bytes
= tswap64(st64
->rx_bytes
);
2133 st64
->tx_bytes
= tswap64(st64
->tx_bytes
);
2134 st64
->rx_errors
= tswap64(st64
->rx_errors
);
2135 st64
->tx_errors
= tswap64(st64
->tx_errors
);
2136 st64
->rx_dropped
= tswap64(st64
->rx_dropped
);
2137 st64
->tx_dropped
= tswap64(st64
->tx_dropped
);
2138 st64
->multicast
= tswap64(st64
->multicast
);
2139 st64
->collisions
= tswap64(st64
->collisions
);
2141 /* detailed rx_errors: */
2142 st64
->rx_length_errors
= tswap64(st64
->rx_length_errors
);
2143 st64
->rx_over_errors
= tswap64(st64
->rx_over_errors
);
2144 st64
->rx_crc_errors
= tswap64(st64
->rx_crc_errors
);
2145 st64
->rx_frame_errors
= tswap64(st64
->rx_frame_errors
);
2146 st64
->rx_fifo_errors
= tswap64(st64
->rx_fifo_errors
);
2147 st64
->rx_missed_errors
= tswap64(st64
->rx_missed_errors
);
2149 /* detailed tx_errors */
2150 st64
->tx_aborted_errors
= tswap64(st64
->tx_aborted_errors
);
2151 st64
->tx_carrier_errors
= tswap64(st64
->tx_carrier_errors
);
2152 st64
->tx_fifo_errors
= tswap64(st64
->tx_fifo_errors
);
2153 st64
->tx_heartbeat_errors
= tswap64(st64
->tx_heartbeat_errors
);
2154 st64
->tx_window_errors
= tswap64(st64
->tx_window_errors
);
2157 st64
->rx_compressed
= tswap64(st64
->rx_compressed
);
2158 st64
->tx_compressed
= tswap64(st64
->tx_compressed
);
2160 /* struct rtnl_link_ifmap */
2162 map
= RTA_DATA(rtattr
);
2163 map
->mem_start
= tswap64(map
->mem_start
);
2164 map
->mem_end
= tswap64(map
->mem_end
);
2165 map
->base_addr
= tswap64(map
->base_addr
);
2166 map
->irq
= tswap16(map
->irq
);
2170 memset(&li_context
, 0, sizeof(li_context
));
2171 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
2173 host_to_target_data_linkinfo_nlattr
);
2175 return host_to_target_for_each_nlattr(RTA_DATA(rtattr
), rtattr
->rta_len
,
2177 host_to_target_data_spec_nlattr
);
2179 gemu_log("Unknown host IFLA type: %d\n", rtattr
->rta_type
);
2185 static abi_long
host_to_target_data_addr_rtattr(struct rtattr
*rtattr
)
2188 struct ifa_cacheinfo
*ci
;
2190 switch (rtattr
->rta_type
) {
2191 /* binary: depends on family type */
2201 u32
= RTA_DATA(rtattr
);
2202 *u32
= tswap32(*u32
);
2204 /* struct ifa_cacheinfo */
2206 ci
= RTA_DATA(rtattr
);
2207 ci
->ifa_prefered
= tswap32(ci
->ifa_prefered
);
2208 ci
->ifa_valid
= tswap32(ci
->ifa_valid
);
2209 ci
->cstamp
= tswap32(ci
->cstamp
);
2210 ci
->tstamp
= tswap32(ci
->tstamp
);
2213 gemu_log("Unknown host IFA type: %d\n", rtattr
->rta_type
);
2219 static abi_long
host_to_target_data_route_rtattr(struct rtattr
*rtattr
)
2222 switch (rtattr
->rta_type
) {
2223 /* binary: depends on family type */
2232 u32
= RTA_DATA(rtattr
);
2233 *u32
= tswap32(*u32
);
2236 gemu_log("Unknown host RTA type: %d\n", rtattr
->rta_type
);
2242 static abi_long
host_to_target_link_rtattr(struct rtattr
*rtattr
,
2243 uint32_t rtattr_len
)
2245 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2246 host_to_target_data_link_rtattr
);
2249 static abi_long
host_to_target_addr_rtattr(struct rtattr
*rtattr
,
2250 uint32_t rtattr_len
)
2252 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2253 host_to_target_data_addr_rtattr
);
2256 static abi_long
host_to_target_route_rtattr(struct rtattr
*rtattr
,
2257 uint32_t rtattr_len
)
2259 return host_to_target_for_each_rtattr(rtattr
, rtattr_len
,
2260 host_to_target_data_route_rtattr
);
2263 static abi_long
host_to_target_data_route(struct nlmsghdr
*nlh
)
2266 struct ifinfomsg
*ifi
;
2267 struct ifaddrmsg
*ifa
;
2270 nlmsg_len
= nlh
->nlmsg_len
;
2271 switch (nlh
->nlmsg_type
) {
2275 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifi
))) {
2276 ifi
= NLMSG_DATA(nlh
);
2277 ifi
->ifi_type
= tswap16(ifi
->ifi_type
);
2278 ifi
->ifi_index
= tswap32(ifi
->ifi_index
);
2279 ifi
->ifi_flags
= tswap32(ifi
->ifi_flags
);
2280 ifi
->ifi_change
= tswap32(ifi
->ifi_change
);
2281 host_to_target_link_rtattr(IFLA_RTA(ifi
),
2282 nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
)));
2288 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifa
))) {
2289 ifa
= NLMSG_DATA(nlh
);
2290 ifa
->ifa_index
= tswap32(ifa
->ifa_index
);
2291 host_to_target_addr_rtattr(IFA_RTA(ifa
),
2292 nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa
)));
2298 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*rtm
))) {
2299 rtm
= NLMSG_DATA(nlh
);
2300 rtm
->rtm_flags
= tswap32(rtm
->rtm_flags
);
2301 host_to_target_route_rtattr(RTM_RTA(rtm
),
2302 nlmsg_len
- NLMSG_LENGTH(sizeof(*rtm
)));
2306 return -TARGET_EINVAL
;
2311 static inline abi_long
host_to_target_nlmsg_route(struct nlmsghdr
*nlh
,
2314 return host_to_target_for_each_nlmsg(nlh
, len
, host_to_target_data_route
);
2317 static abi_long
target_to_host_for_each_rtattr(struct rtattr
*rtattr
,
2319 abi_long (*target_to_host_rtattr
)
2324 while (len
>= sizeof(struct rtattr
)) {
2325 if (tswap16(rtattr
->rta_len
) < sizeof(struct rtattr
) ||
2326 tswap16(rtattr
->rta_len
) > len
) {
2329 rtattr
->rta_len
= tswap16(rtattr
->rta_len
);
2330 rtattr
->rta_type
= tswap16(rtattr
->rta_type
);
2331 ret
= target_to_host_rtattr(rtattr
);
2335 len
-= RTA_ALIGN(rtattr
->rta_len
);
2336 rtattr
= (struct rtattr
*)(((char *)rtattr
) +
2337 RTA_ALIGN(rtattr
->rta_len
));
2342 static abi_long
target_to_host_data_link_rtattr(struct rtattr
*rtattr
)
2344 switch (rtattr
->rta_type
) {
2346 gemu_log("Unknown target IFLA type: %d\n", rtattr
->rta_type
);
2352 static abi_long
target_to_host_data_addr_rtattr(struct rtattr
*rtattr
)
2354 switch (rtattr
->rta_type
) {
2355 /* binary: depends on family type */
2360 gemu_log("Unknown target IFA type: %d\n", rtattr
->rta_type
);
2366 static abi_long
target_to_host_data_route_rtattr(struct rtattr
*rtattr
)
2369 switch (rtattr
->rta_type
) {
2370 /* binary: depends on family type */
2377 u32
= RTA_DATA(rtattr
);
2378 *u32
= tswap32(*u32
);
2381 gemu_log("Unknown target RTA type: %d\n", rtattr
->rta_type
);
2387 static void target_to_host_link_rtattr(struct rtattr
*rtattr
,
2388 uint32_t rtattr_len
)
2390 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2391 target_to_host_data_link_rtattr
);
2394 static void target_to_host_addr_rtattr(struct rtattr
*rtattr
,
2395 uint32_t rtattr_len
)
2397 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2398 target_to_host_data_addr_rtattr
);
2401 static void target_to_host_route_rtattr(struct rtattr
*rtattr
,
2402 uint32_t rtattr_len
)
2404 target_to_host_for_each_rtattr(rtattr
, rtattr_len
,
2405 target_to_host_data_route_rtattr
);
2408 static abi_long
target_to_host_data_route(struct nlmsghdr
*nlh
)
2410 struct ifinfomsg
*ifi
;
2411 struct ifaddrmsg
*ifa
;
2414 switch (nlh
->nlmsg_type
) {
2419 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifi
))) {
2420 ifi
= NLMSG_DATA(nlh
);
2421 ifi
->ifi_type
= tswap16(ifi
->ifi_type
);
2422 ifi
->ifi_index
= tswap32(ifi
->ifi_index
);
2423 ifi
->ifi_flags
= tswap32(ifi
->ifi_flags
);
2424 ifi
->ifi_change
= tswap32(ifi
->ifi_change
);
2425 target_to_host_link_rtattr(IFLA_RTA(ifi
), nlh
->nlmsg_len
-
2426 NLMSG_LENGTH(sizeof(*ifi
)));
2432 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*ifa
))) {
2433 ifa
= NLMSG_DATA(nlh
);
2434 ifa
->ifa_index
= tswap32(ifa
->ifa_index
);
2435 target_to_host_addr_rtattr(IFA_RTA(ifa
), nlh
->nlmsg_len
-
2436 NLMSG_LENGTH(sizeof(*ifa
)));
2443 if (nlh
->nlmsg_len
>= NLMSG_LENGTH(sizeof(*rtm
))) {
2444 rtm
= NLMSG_DATA(nlh
);
2445 rtm
->rtm_flags
= tswap32(rtm
->rtm_flags
);
2446 target_to_host_route_rtattr(RTM_RTA(rtm
), nlh
->nlmsg_len
-
2447 NLMSG_LENGTH(sizeof(*rtm
)));
2451 return -TARGET_EOPNOTSUPP
;
2456 static abi_long
target_to_host_nlmsg_route(struct nlmsghdr
*nlh
, size_t len
)
2458 return target_to_host_for_each_nlmsg(nlh
, len
, target_to_host_data_route
);
2460 #endif /* CONFIG_RTNETLINK */
2462 static abi_long
host_to_target_data_audit(struct nlmsghdr
*nlh
)
2464 switch (nlh
->nlmsg_type
) {
2466 gemu_log("Unknown host audit message type %d\n",
2468 return -TARGET_EINVAL
;
2473 static inline abi_long
host_to_target_nlmsg_audit(struct nlmsghdr
*nlh
,
2476 return host_to_target_for_each_nlmsg(nlh
, len
, host_to_target_data_audit
);
2479 static abi_long
target_to_host_data_audit(struct nlmsghdr
*nlh
)
2481 switch (nlh
->nlmsg_type
) {
2483 case AUDIT_FIRST_USER_MSG
... AUDIT_LAST_USER_MSG
:
2484 case AUDIT_FIRST_USER_MSG2
... AUDIT_LAST_USER_MSG2
:
2487 gemu_log("Unknown target audit message type %d\n",
2489 return -TARGET_EINVAL
;
2495 static abi_long
target_to_host_nlmsg_audit(struct nlmsghdr
*nlh
, size_t len
)
2497 return target_to_host_for_each_nlmsg(nlh
, len
, target_to_host_data_audit
);
2500 /* do_setsockopt() Must return target values and target errnos. */
2501 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
2502 abi_ulong optval_addr
, socklen_t optlen
)
2506 struct ip_mreqn
*ip_mreq
;
2507 struct ip_mreq_source
*ip_mreq_source
;
2511 /* TCP options all take an 'int' value. */
2512 if (optlen
< sizeof(uint32_t))
2513 return -TARGET_EINVAL
;
2515 if (get_user_u32(val
, optval_addr
))
2516 return -TARGET_EFAULT
;
2517 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2524 case IP_ROUTER_ALERT
:
2528 case IP_MTU_DISCOVER
:
2534 case IP_MULTICAST_TTL
:
2535 case IP_MULTICAST_LOOP
:
2537 if (optlen
>= sizeof(uint32_t)) {
2538 if (get_user_u32(val
, optval_addr
))
2539 return -TARGET_EFAULT
;
2540 } else if (optlen
>= 1) {
2541 if (get_user_u8(val
, optval_addr
))
2542 return -TARGET_EFAULT
;
2544 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2546 case IP_ADD_MEMBERSHIP
:
2547 case IP_DROP_MEMBERSHIP
:
2548 if (optlen
< sizeof (struct target_ip_mreq
) ||
2549 optlen
> sizeof (struct target_ip_mreqn
))
2550 return -TARGET_EINVAL
;
2552 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
2553 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
2554 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
2557 case IP_BLOCK_SOURCE
:
2558 case IP_UNBLOCK_SOURCE
:
2559 case IP_ADD_SOURCE_MEMBERSHIP
:
2560 case IP_DROP_SOURCE_MEMBERSHIP
:
2561 if (optlen
!= sizeof (struct target_ip_mreq_source
))
2562 return -TARGET_EINVAL
;
2564 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2565 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
2566 unlock_user (ip_mreq_source
, optval_addr
, 0);
2575 case IPV6_MTU_DISCOVER
:
2578 case IPV6_RECVPKTINFO
:
2580 if (optlen
< sizeof(uint32_t)) {
2581 return -TARGET_EINVAL
;
2583 if (get_user_u32(val
, optval_addr
)) {
2584 return -TARGET_EFAULT
;
2586 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2587 &val
, sizeof(val
)));
2596 /* struct icmp_filter takes an u32 value */
2597 if (optlen
< sizeof(uint32_t)) {
2598 return -TARGET_EINVAL
;
2601 if (get_user_u32(val
, optval_addr
)) {
2602 return -TARGET_EFAULT
;
2604 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2605 &val
, sizeof(val
)));
2612 case TARGET_SOL_SOCKET
:
2614 case TARGET_SO_RCVTIMEO
:
2618 optname
= SO_RCVTIMEO
;
2621 if (optlen
!= sizeof(struct target_timeval
)) {
2622 return -TARGET_EINVAL
;
2625 if (copy_from_user_timeval(&tv
, optval_addr
)) {
2626 return -TARGET_EFAULT
;
2629 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2633 case TARGET_SO_SNDTIMEO
:
2634 optname
= SO_SNDTIMEO
;
2636 case TARGET_SO_ATTACH_FILTER
:
2638 struct target_sock_fprog
*tfprog
;
2639 struct target_sock_filter
*tfilter
;
2640 struct sock_fprog fprog
;
2641 struct sock_filter
*filter
;
2644 if (optlen
!= sizeof(*tfprog
)) {
2645 return -TARGET_EINVAL
;
2647 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
2648 return -TARGET_EFAULT
;
2650 if (!lock_user_struct(VERIFY_READ
, tfilter
,
2651 tswapal(tfprog
->filter
), 0)) {
2652 unlock_user_struct(tfprog
, optval_addr
, 1);
2653 return -TARGET_EFAULT
;
2656 fprog
.len
= tswap16(tfprog
->len
);
2657 filter
= g_try_new(struct sock_filter
, fprog
.len
);
2658 if (filter
== NULL
) {
2659 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2660 unlock_user_struct(tfprog
, optval_addr
, 1);
2661 return -TARGET_ENOMEM
;
2663 for (i
= 0; i
< fprog
.len
; i
++) {
2664 filter
[i
].code
= tswap16(tfilter
[i
].code
);
2665 filter
[i
].jt
= tfilter
[i
].jt
;
2666 filter
[i
].jf
= tfilter
[i
].jf
;
2667 filter
[i
].k
= tswap32(tfilter
[i
].k
);
2669 fprog
.filter
= filter
;
2671 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
2672 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
2675 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2676 unlock_user_struct(tfprog
, optval_addr
, 1);
2679 case TARGET_SO_BINDTODEVICE
:
2681 char *dev_ifname
, *addr_ifname
;
2683 if (optlen
> IFNAMSIZ
- 1) {
2684 optlen
= IFNAMSIZ
- 1;
2686 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2688 return -TARGET_EFAULT
;
2690 optname
= SO_BINDTODEVICE
;
2691 addr_ifname
= alloca(IFNAMSIZ
);
2692 memcpy(addr_ifname
, dev_ifname
, optlen
);
2693 addr_ifname
[optlen
] = 0;
2694 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2695 addr_ifname
, optlen
));
2696 unlock_user (dev_ifname
, optval_addr
, 0);
2699 /* Options with 'int' argument. */
2700 case TARGET_SO_DEBUG
:
2703 case TARGET_SO_REUSEADDR
:
2704 optname
= SO_REUSEADDR
;
2706 case TARGET_SO_TYPE
:
2709 case TARGET_SO_ERROR
:
2712 case TARGET_SO_DONTROUTE
:
2713 optname
= SO_DONTROUTE
;
2715 case TARGET_SO_BROADCAST
:
2716 optname
= SO_BROADCAST
;
2718 case TARGET_SO_SNDBUF
:
2719 optname
= SO_SNDBUF
;
2721 case TARGET_SO_SNDBUFFORCE
:
2722 optname
= SO_SNDBUFFORCE
;
2724 case TARGET_SO_RCVBUF
:
2725 optname
= SO_RCVBUF
;
2727 case TARGET_SO_RCVBUFFORCE
:
2728 optname
= SO_RCVBUFFORCE
;
2730 case TARGET_SO_KEEPALIVE
:
2731 optname
= SO_KEEPALIVE
;
2733 case TARGET_SO_OOBINLINE
:
2734 optname
= SO_OOBINLINE
;
2736 case TARGET_SO_NO_CHECK
:
2737 optname
= SO_NO_CHECK
;
2739 case TARGET_SO_PRIORITY
:
2740 optname
= SO_PRIORITY
;
2743 case TARGET_SO_BSDCOMPAT
:
2744 optname
= SO_BSDCOMPAT
;
2747 case TARGET_SO_PASSCRED
:
2748 optname
= SO_PASSCRED
;
2750 case TARGET_SO_PASSSEC
:
2751 optname
= SO_PASSSEC
;
2753 case TARGET_SO_TIMESTAMP
:
2754 optname
= SO_TIMESTAMP
;
2756 case TARGET_SO_RCVLOWAT
:
2757 optname
= SO_RCVLOWAT
;
2763 if (optlen
< sizeof(uint32_t))
2764 return -TARGET_EINVAL
;
2766 if (get_user_u32(val
, optval_addr
))
2767 return -TARGET_EFAULT
;
2768 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
2772 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level
, optname
);
2773 ret
= -TARGET_ENOPROTOOPT
;
2778 /* do_getsockopt() Must return target values and target errnos. */
2779 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
2780 abi_ulong optval_addr
, abi_ulong optlen
)
2787 case TARGET_SOL_SOCKET
:
2790 /* These don't just return a single integer */
2791 case TARGET_SO_LINGER
:
2792 case TARGET_SO_RCVTIMEO
:
2793 case TARGET_SO_SNDTIMEO
:
2794 case TARGET_SO_PEERNAME
:
2796 case TARGET_SO_PEERCRED
: {
2799 struct target_ucred
*tcr
;
2801 if (get_user_u32(len
, optlen
)) {
2802 return -TARGET_EFAULT
;
2805 return -TARGET_EINVAL
;
2809 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
2817 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
2818 return -TARGET_EFAULT
;
2820 __put_user(cr
.pid
, &tcr
->pid
);
2821 __put_user(cr
.uid
, &tcr
->uid
);
2822 __put_user(cr
.gid
, &tcr
->gid
);
2823 unlock_user_struct(tcr
, optval_addr
, 1);
2824 if (put_user_u32(len
, optlen
)) {
2825 return -TARGET_EFAULT
;
2829 /* Options with 'int' argument. */
2830 case TARGET_SO_DEBUG
:
2833 case TARGET_SO_REUSEADDR
:
2834 optname
= SO_REUSEADDR
;
2836 case TARGET_SO_TYPE
:
2839 case TARGET_SO_ERROR
:
2842 case TARGET_SO_DONTROUTE
:
2843 optname
= SO_DONTROUTE
;
2845 case TARGET_SO_BROADCAST
:
2846 optname
= SO_BROADCAST
;
2848 case TARGET_SO_SNDBUF
:
2849 optname
= SO_SNDBUF
;
2851 case TARGET_SO_RCVBUF
:
2852 optname
= SO_RCVBUF
;
2854 case TARGET_SO_KEEPALIVE
:
2855 optname
= SO_KEEPALIVE
;
2857 case TARGET_SO_OOBINLINE
:
2858 optname
= SO_OOBINLINE
;
2860 case TARGET_SO_NO_CHECK
:
2861 optname
= SO_NO_CHECK
;
2863 case TARGET_SO_PRIORITY
:
2864 optname
= SO_PRIORITY
;
2867 case TARGET_SO_BSDCOMPAT
:
2868 optname
= SO_BSDCOMPAT
;
2871 case TARGET_SO_PASSCRED
:
2872 optname
= SO_PASSCRED
;
2874 case TARGET_SO_TIMESTAMP
:
2875 optname
= SO_TIMESTAMP
;
2877 case TARGET_SO_RCVLOWAT
:
2878 optname
= SO_RCVLOWAT
;
2880 case TARGET_SO_ACCEPTCONN
:
2881 optname
= SO_ACCEPTCONN
;
2888 /* TCP options all take an 'int' value. */
2890 if (get_user_u32(len
, optlen
))
2891 return -TARGET_EFAULT
;
2893 return -TARGET_EINVAL
;
2895 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2898 if (optname
== SO_TYPE
) {
2899 val
= host_to_target_sock_type(val
);
2904 if (put_user_u32(val
, optval_addr
))
2905 return -TARGET_EFAULT
;
2907 if (put_user_u8(val
, optval_addr
))
2908 return -TARGET_EFAULT
;
2910 if (put_user_u32(len
, optlen
))
2911 return -TARGET_EFAULT
;
2918 case IP_ROUTER_ALERT
:
2922 case IP_MTU_DISCOVER
:
2928 case IP_MULTICAST_TTL
:
2929 case IP_MULTICAST_LOOP
:
2930 if (get_user_u32(len
, optlen
))
2931 return -TARGET_EFAULT
;
2933 return -TARGET_EINVAL
;
2935 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2938 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2940 if (put_user_u32(len
, optlen
)
2941 || put_user_u8(val
, optval_addr
))
2942 return -TARGET_EFAULT
;
2944 if (len
> sizeof(int))
2946 if (put_user_u32(len
, optlen
)
2947 || put_user_u32(val
, optval_addr
))
2948 return -TARGET_EFAULT
;
2952 ret
= -TARGET_ENOPROTOOPT
;
2958 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
2960 ret
= -TARGET_EOPNOTSUPP
;
2966 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
2967 int count
, int copy
)
2969 struct target_iovec
*target_vec
;
2971 abi_ulong total_len
, max_len
;
2974 bool bad_address
= false;
2980 if (count
< 0 || count
> IOV_MAX
) {
2985 vec
= g_try_new0(struct iovec
, count
);
2991 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2992 count
* sizeof(struct target_iovec
), 1);
2993 if (target_vec
== NULL
) {
2998 /* ??? If host page size > target page size, this will result in a
2999 value larger than what we can actually support. */
3000 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
3003 for (i
= 0; i
< count
; i
++) {
3004 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3005 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3010 } else if (len
== 0) {
3011 /* Zero length pointer is ignored. */
3012 vec
[i
].iov_base
= 0;
3014 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
3015 /* If the first buffer pointer is bad, this is a fault. But
3016 * subsequent bad buffers will result in a partial write; this
3017 * is realized by filling the vector with null pointers and
3019 if (!vec
[i
].iov_base
) {
3030 if (len
> max_len
- total_len
) {
3031 len
= max_len
- total_len
;
3034 vec
[i
].iov_len
= len
;
3038 unlock_user(target_vec
, target_addr
, 0);
3043 if (tswapal(target_vec
[i
].iov_len
) > 0) {
3044 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
3047 unlock_user(target_vec
, target_addr
, 0);
3054 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
3055 int count
, int copy
)
3057 struct target_iovec
*target_vec
;
3060 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3061 count
* sizeof(struct target_iovec
), 1);
3063 for (i
= 0; i
< count
; i
++) {
3064 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3065 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3069 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
3071 unlock_user(target_vec
, target_addr
, 0);
3077 static inline int target_to_host_sock_type(int *type
)
3080 int target_type
= *type
;
3082 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
3083 case TARGET_SOCK_DGRAM
:
3084 host_type
= SOCK_DGRAM
;
3086 case TARGET_SOCK_STREAM
:
3087 host_type
= SOCK_STREAM
;
3090 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
3093 if (target_type
& TARGET_SOCK_CLOEXEC
) {
3094 #if defined(SOCK_CLOEXEC)
3095 host_type
|= SOCK_CLOEXEC
;
3097 return -TARGET_EINVAL
;
3100 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3101 #if defined(SOCK_NONBLOCK)
3102 host_type
|= SOCK_NONBLOCK
;
3103 #elif !defined(O_NONBLOCK)
3104 return -TARGET_EINVAL
;
3111 /* Try to emulate socket type flags after socket creation. */
3112 static int sock_flags_fixup(int fd
, int target_type
)
3114 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3115 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3116 int flags
= fcntl(fd
, F_GETFL
);
3117 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
3119 return -TARGET_EINVAL
;
3126 static abi_long
packet_target_to_host_sockaddr(void *host_addr
,
3127 abi_ulong target_addr
,
3130 struct sockaddr
*addr
= host_addr
;
3131 struct target_sockaddr
*target_saddr
;
3133 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
3134 if (!target_saddr
) {
3135 return -TARGET_EFAULT
;
3138 memcpy(addr
, target_saddr
, len
);
3139 addr
->sa_family
= tswap16(target_saddr
->sa_family
);
3140 /* spkt_protocol is big-endian */
3142 unlock_user(target_saddr
, target_addr
, 0);
3146 static TargetFdTrans target_packet_trans
= {
3147 .target_to_host_addr
= packet_target_to_host_sockaddr
,
3150 #ifdef CONFIG_RTNETLINK
3151 static abi_long
netlink_route_target_to_host(void *buf
, size_t len
)
3155 ret
= target_to_host_nlmsg_route(buf
, len
);
3163 static abi_long
netlink_route_host_to_target(void *buf
, size_t len
)
3167 ret
= host_to_target_nlmsg_route(buf
, len
);
3175 static TargetFdTrans target_netlink_route_trans
= {
3176 .target_to_host_data
= netlink_route_target_to_host
,
3177 .host_to_target_data
= netlink_route_host_to_target
,
3179 #endif /* CONFIG_RTNETLINK */
3181 static abi_long
netlink_audit_target_to_host(void *buf
, size_t len
)
3185 ret
= target_to_host_nlmsg_audit(buf
, len
);
3193 static abi_long
netlink_audit_host_to_target(void *buf
, size_t len
)
3197 ret
= host_to_target_nlmsg_audit(buf
, len
);
3205 static TargetFdTrans target_netlink_audit_trans
= {
3206 .target_to_host_data
= netlink_audit_target_to_host
,
3207 .host_to_target_data
= netlink_audit_host_to_target
,
3210 /* do_socket() Must return target values and target errnos. */
3211 static abi_long
do_socket(int domain
, int type
, int protocol
)
3213 int target_type
= type
;
3216 ret
= target_to_host_sock_type(&type
);
3221 if (domain
== PF_NETLINK
&& !(
3222 #ifdef CONFIG_RTNETLINK
3223 protocol
== NETLINK_ROUTE
||
3225 protocol
== NETLINK_KOBJECT_UEVENT
||
3226 protocol
== NETLINK_AUDIT
)) {
3227 return -EPFNOSUPPORT
;
3230 if (domain
== AF_PACKET
||
3231 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
3232 protocol
= tswap16(protocol
);
3235 ret
= get_errno(socket(domain
, type
, protocol
));
3237 ret
= sock_flags_fixup(ret
, target_type
);
3238 if (type
== SOCK_PACKET
) {
3239 /* Manage an obsolete case :
3240 * if socket type is SOCK_PACKET, bind by name
3242 fd_trans_register(ret
, &target_packet_trans
);
3243 } else if (domain
== PF_NETLINK
) {
3245 #ifdef CONFIG_RTNETLINK
3247 fd_trans_register(ret
, &target_netlink_route_trans
);
3250 case NETLINK_KOBJECT_UEVENT
:
3251 /* nothing to do: messages are strings */
3254 fd_trans_register(ret
, &target_netlink_audit_trans
);
3257 g_assert_not_reached();
3264 /* do_bind() Must return target values and target errnos. */
3265 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
3271 if ((int)addrlen
< 0) {
3272 return -TARGET_EINVAL
;
3275 addr
= alloca(addrlen
+1);
3277 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3281 return get_errno(bind(sockfd
, addr
, addrlen
));
3284 /* do_connect() Must return target values and target errnos. */
3285 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
3291 if ((int)addrlen
< 0) {
3292 return -TARGET_EINVAL
;
3295 addr
= alloca(addrlen
+1);
3297 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3301 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
3304 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3305 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
3306 int flags
, int send
)
3312 abi_ulong target_vec
;
3314 if (msgp
->msg_name
) {
3315 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
3316 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3317 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3318 tswapal(msgp
->msg_name
),
3324 msg
.msg_name
= NULL
;
3325 msg
.msg_namelen
= 0;
3327 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3328 msg
.msg_control
= alloca(msg
.msg_controllen
);
3329 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3331 count
= tswapal(msgp
->msg_iovlen
);
3332 target_vec
= tswapal(msgp
->msg_iov
);
3333 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
3334 target_vec
, count
, send
);
3336 ret
= -host_to_target_errno(errno
);
3339 msg
.msg_iovlen
= count
;
3343 if (fd_trans_target_to_host_data(fd
)) {
3346 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3347 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3348 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3349 msg
.msg_iov
->iov_len
);
3351 msg
.msg_iov
->iov_base
= host_msg
;
3352 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3356 ret
= target_to_host_cmsg(&msg
, msgp
);
3358 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3362 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3363 if (!is_error(ret
)) {
3365 if (fd_trans_host_to_target_data(fd
)) {
3366 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3369 ret
= host_to_target_cmsg(msgp
, &msg
);
3371 if (!is_error(ret
)) {
3372 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3373 if (msg
.msg_name
!= NULL
) {
3374 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3375 msg
.msg_name
, msg
.msg_namelen
);
3387 unlock_iovec(vec
, target_vec
, count
, !send
);
3392 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3393 int flags
, int send
)
3396 struct target_msghdr
*msgp
;
3398 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
3402 return -TARGET_EFAULT
;
3404 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3405 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
3409 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3410 * so it might not have this *mmsg-specific flag either.
3412 #ifndef MSG_WAITFORONE
3413 #define MSG_WAITFORONE 0x10000
3416 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3417 unsigned int vlen
, unsigned int flags
,
3420 struct target_mmsghdr
*mmsgp
;
3424 if (vlen
> UIO_MAXIOV
) {
3428 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3430 return -TARGET_EFAULT
;
3433 for (i
= 0; i
< vlen
; i
++) {
3434 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3435 if (is_error(ret
)) {
3438 mmsgp
[i
].msg_len
= tswap32(ret
);
3439 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3440 if (flags
& MSG_WAITFORONE
) {
3441 flags
|= MSG_DONTWAIT
;
3445 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3447 /* Return number of datagrams sent if we sent any at all;
3448 * otherwise return the error.
3456 /* do_accept4() Must return target values and target errnos. */
3457 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3458 abi_ulong target_addrlen_addr
, int flags
)
3465 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
3467 if (target_addr
== 0) {
3468 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3471 /* linux returns EINVAL if addrlen pointer is invalid */
3472 if (get_user_u32(addrlen
, target_addrlen_addr
))
3473 return -TARGET_EINVAL
;
3475 if ((int)addrlen
< 0) {
3476 return -TARGET_EINVAL
;
3479 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3480 return -TARGET_EINVAL
;
3482 addr
= alloca(addrlen
);
3484 ret
= get_errno(safe_accept4(fd
, addr
, &addrlen
, host_flags
));
3485 if (!is_error(ret
)) {
3486 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3487 if (put_user_u32(addrlen
, target_addrlen_addr
))
3488 ret
= -TARGET_EFAULT
;
3493 /* do_getpeername() Must return target values and target errnos. */
3494 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3495 abi_ulong target_addrlen_addr
)
3501 if (get_user_u32(addrlen
, target_addrlen_addr
))
3502 return -TARGET_EFAULT
;
3504 if ((int)addrlen
< 0) {
3505 return -TARGET_EINVAL
;
3508 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3509 return -TARGET_EFAULT
;
3511 addr
= alloca(addrlen
);
3513 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
3514 if (!is_error(ret
)) {
3515 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3516 if (put_user_u32(addrlen
, target_addrlen_addr
))
3517 ret
= -TARGET_EFAULT
;
3522 /* do_getsockname() Must return target values and target errnos. */
3523 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3524 abi_ulong target_addrlen_addr
)
3530 if (get_user_u32(addrlen
, target_addrlen_addr
))
3531 return -TARGET_EFAULT
;
3533 if ((int)addrlen
< 0) {
3534 return -TARGET_EINVAL
;
3537 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3538 return -TARGET_EFAULT
;
3540 addr
= alloca(addrlen
);
3542 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
3543 if (!is_error(ret
)) {
3544 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3545 if (put_user_u32(addrlen
, target_addrlen_addr
))
3546 ret
= -TARGET_EFAULT
;
3551 /* do_socketpair() Must return target values and target errnos. */
3552 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3553 abi_ulong target_tab_addr
)
3558 target_to_host_sock_type(&type
);
3560 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
3561 if (!is_error(ret
)) {
3562 if (put_user_s32(tab
[0], target_tab_addr
)
3563 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
3564 ret
= -TARGET_EFAULT
;
3569 /* do_sendto() Must return target values and target errnos. */
3570 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
3571 abi_ulong target_addr
, socklen_t addrlen
)
3575 void *copy_msg
= NULL
;
3578 if ((int)addrlen
< 0) {
3579 return -TARGET_EINVAL
;
3582 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
3584 return -TARGET_EFAULT
;
3585 if (fd_trans_target_to_host_data(fd
)) {
3586 copy_msg
= host_msg
;
3587 host_msg
= g_malloc(len
);
3588 memcpy(host_msg
, copy_msg
, len
);
3589 ret
= fd_trans_target_to_host_data(fd
)(host_msg
, len
);
3595 addr
= alloca(addrlen
+1);
3596 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
3600 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
3602 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, NULL
, 0));
3607 host_msg
= copy_msg
;
3609 unlock_user(host_msg
, msg
, 0);
3613 /* do_recvfrom() Must return target values and target errnos. */
3614 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
3615 abi_ulong target_addr
,
3616 abi_ulong target_addrlen
)
3623 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
3625 return -TARGET_EFAULT
;
3627 if (get_user_u32(addrlen
, target_addrlen
)) {
3628 ret
= -TARGET_EFAULT
;
3631 if ((int)addrlen
< 0) {
3632 ret
= -TARGET_EINVAL
;
3635 addr
= alloca(addrlen
);
3636 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
,
3639 addr
= NULL
; /* To keep compiler quiet. */
3640 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
, NULL
, 0));
3642 if (!is_error(ret
)) {
3643 if (fd_trans_host_to_target_data(fd
)) {
3644 ret
= fd_trans_host_to_target_data(fd
)(host_msg
, ret
);
3647 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
3648 if (put_user_u32(addrlen
, target_addrlen
)) {
3649 ret
= -TARGET_EFAULT
;
3653 unlock_user(host_msg
, msg
, len
);
3656 unlock_user(host_msg
, msg
, 0);
3661 #ifdef TARGET_NR_socketcall
3662 /* do_socketcall() Must return target values and target errnos. */
3663 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
3665 static const unsigned ac
[] = { /* number of arguments per call */
3666 [SOCKOP_socket
] = 3, /* domain, type, protocol */
3667 [SOCKOP_bind
] = 3, /* sockfd, addr, addrlen */
3668 [SOCKOP_connect
] = 3, /* sockfd, addr, addrlen */
3669 [SOCKOP_listen
] = 2, /* sockfd, backlog */
3670 [SOCKOP_accept
] = 3, /* sockfd, addr, addrlen */
3671 [SOCKOP_accept4
] = 4, /* sockfd, addr, addrlen, flags */
3672 [SOCKOP_getsockname
] = 3, /* sockfd, addr, addrlen */
3673 [SOCKOP_getpeername
] = 3, /* sockfd, addr, addrlen */
3674 [SOCKOP_socketpair
] = 4, /* domain, type, protocol, tab */
3675 [SOCKOP_send
] = 4, /* sockfd, msg, len, flags */
3676 [SOCKOP_recv
] = 4, /* sockfd, msg, len, flags */
3677 [SOCKOP_sendto
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
3678 [SOCKOP_recvfrom
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
3679 [SOCKOP_shutdown
] = 2, /* sockfd, how */
3680 [SOCKOP_sendmsg
] = 3, /* sockfd, msg, flags */
3681 [SOCKOP_recvmsg
] = 3, /* sockfd, msg, flags */
3682 [SOCKOP_sendmmsg
] = 4, /* sockfd, msgvec, vlen, flags */
3683 [SOCKOP_recvmmsg
] = 4, /* sockfd, msgvec, vlen, flags */
3684 [SOCKOP_setsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
3685 [SOCKOP_getsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
3687 abi_long a
[6]; /* max 6 args */
3689 /* first, collect the arguments in a[] according to ac[] */
3690 if (num
>= 0 && num
< ARRAY_SIZE(ac
)) {
3692 assert(ARRAY_SIZE(a
) >= ac
[num
]); /* ensure we have space for args */
3693 for (i
= 0; i
< ac
[num
]; ++i
) {
3694 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
3695 return -TARGET_EFAULT
;
3700 /* now when we have the args, actually handle the call */
3702 case SOCKOP_socket
: /* domain, type, protocol */
3703 return do_socket(a
[0], a
[1], a
[2]);
3704 case SOCKOP_bind
: /* sockfd, addr, addrlen */
3705 return do_bind(a
[0], a
[1], a
[2]);
3706 case SOCKOP_connect
: /* sockfd, addr, addrlen */
3707 return do_connect(a
[0], a
[1], a
[2]);
3708 case SOCKOP_listen
: /* sockfd, backlog */
3709 return get_errno(listen(a
[0], a
[1]));
3710 case SOCKOP_accept
: /* sockfd, addr, addrlen */
3711 return do_accept4(a
[0], a
[1], a
[2], 0);
3712 case SOCKOP_accept4
: /* sockfd, addr, addrlen, flags */
3713 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
3714 case SOCKOP_getsockname
: /* sockfd, addr, addrlen */
3715 return do_getsockname(a
[0], a
[1], a
[2]);
3716 case SOCKOP_getpeername
: /* sockfd, addr, addrlen */
3717 return do_getpeername(a
[0], a
[1], a
[2]);
3718 case SOCKOP_socketpair
: /* domain, type, protocol, tab */
3719 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
3720 case SOCKOP_send
: /* sockfd, msg, len, flags */
3721 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
3722 case SOCKOP_recv
: /* sockfd, msg, len, flags */
3723 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
3724 case SOCKOP_sendto
: /* sockfd, msg, len, flags, addr, addrlen */
3725 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3726 case SOCKOP_recvfrom
: /* sockfd, msg, len, flags, addr, addrlen */
3727 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3728 case SOCKOP_shutdown
: /* sockfd, how */
3729 return get_errno(shutdown(a
[0], a
[1]));
3730 case SOCKOP_sendmsg
: /* sockfd, msg, flags */
3731 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
3732 case SOCKOP_recvmsg
: /* sockfd, msg, flags */
3733 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
3734 case SOCKOP_sendmmsg
: /* sockfd, msgvec, vlen, flags */
3735 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
3736 case SOCKOP_recvmmsg
: /* sockfd, msgvec, vlen, flags */
3737 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
3738 case SOCKOP_setsockopt
: /* sockfd, level, optname, optval, optlen */
3739 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3740 case SOCKOP_getsockopt
: /* sockfd, level, optname, optval, optlen */
3741 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3743 gemu_log("Unsupported socketcall: %d\n", num
);
3744 return -TARGET_ENOSYS
;
3749 #define N_SHM_REGIONS 32
3751 static struct shm_region
{
3755 } shm_regions
[N_SHM_REGIONS
];
3757 struct target_semid_ds
3759 struct target_ipc_perm sem_perm
;
3760 abi_ulong sem_otime
;
3761 #if !defined(TARGET_PPC64)
3762 abi_ulong __unused1
;
3764 abi_ulong sem_ctime
;
3765 #if !defined(TARGET_PPC64)
3766 abi_ulong __unused2
;
3768 abi_ulong sem_nsems
;
3769 abi_ulong __unused3
;
3770 abi_ulong __unused4
;
3773 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
3774 abi_ulong target_addr
)
3776 struct target_ipc_perm
*target_ip
;
3777 struct target_semid_ds
*target_sd
;
3779 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3780 return -TARGET_EFAULT
;
3781 target_ip
= &(target_sd
->sem_perm
);
3782 host_ip
->__key
= tswap32(target_ip
->__key
);
3783 host_ip
->uid
= tswap32(target_ip
->uid
);
3784 host_ip
->gid
= tswap32(target_ip
->gid
);
3785 host_ip
->cuid
= tswap32(target_ip
->cuid
);
3786 host_ip
->cgid
= tswap32(target_ip
->cgid
);
3787 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3788 host_ip
->mode
= tswap32(target_ip
->mode
);
3790 host_ip
->mode
= tswap16(target_ip
->mode
);
3792 #if defined(TARGET_PPC)
3793 host_ip
->__seq
= tswap32(target_ip
->__seq
);
3795 host_ip
->__seq
= tswap16(target_ip
->__seq
);
3797 unlock_user_struct(target_sd
, target_addr
, 0);
3801 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
3802 struct ipc_perm
*host_ip
)
3804 struct target_ipc_perm
*target_ip
;
3805 struct target_semid_ds
*target_sd
;
3807 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3808 return -TARGET_EFAULT
;
3809 target_ip
= &(target_sd
->sem_perm
);
3810 target_ip
->__key
= tswap32(host_ip
->__key
);
3811 target_ip
->uid
= tswap32(host_ip
->uid
);
3812 target_ip
->gid
= tswap32(host_ip
->gid
);
3813 target_ip
->cuid
= tswap32(host_ip
->cuid
);
3814 target_ip
->cgid
= tswap32(host_ip
->cgid
);
3815 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3816 target_ip
->mode
= tswap32(host_ip
->mode
);
3818 target_ip
->mode
= tswap16(host_ip
->mode
);
3820 #if defined(TARGET_PPC)
3821 target_ip
->__seq
= tswap32(host_ip
->__seq
);
3823 target_ip
->__seq
= tswap16(host_ip
->__seq
);
3825 unlock_user_struct(target_sd
, target_addr
, 1);
3829 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
3830 abi_ulong target_addr
)
3832 struct target_semid_ds
*target_sd
;
3834 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3835 return -TARGET_EFAULT
;
3836 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
3837 return -TARGET_EFAULT
;
3838 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
3839 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
3840 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
3841 unlock_user_struct(target_sd
, target_addr
, 0);
3845 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
3846 struct semid_ds
*host_sd
)
3848 struct target_semid_ds
*target_sd
;
3850 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3851 return -TARGET_EFAULT
;
3852 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
3853 return -TARGET_EFAULT
;
3854 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
3855 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
3856 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
3857 unlock_user_struct(target_sd
, target_addr
, 1);
3861 struct target_seminfo
{
3874 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
3875 struct seminfo
*host_seminfo
)
3877 struct target_seminfo
*target_seminfo
;
3878 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
3879 return -TARGET_EFAULT
;
3880 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
3881 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
3882 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
3883 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
3884 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
3885 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
3886 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
3887 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
3888 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
3889 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
3890 unlock_user_struct(target_seminfo
, target_addr
, 1);
3896 struct semid_ds
*buf
;
3897 unsigned short *array
;
3898 struct seminfo
*__buf
;
3901 union target_semun
{
3908 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
3909 abi_ulong target_addr
)
3912 unsigned short *array
;
3914 struct semid_ds semid_ds
;
3917 semun
.buf
= &semid_ds
;
3919 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3921 return get_errno(ret
);
3923 nsems
= semid_ds
.sem_nsems
;
3925 *host_array
= g_try_new(unsigned short, nsems
);
3927 return -TARGET_ENOMEM
;
3929 array
= lock_user(VERIFY_READ
, target_addr
,
3930 nsems
*sizeof(unsigned short), 1);
3932 g_free(*host_array
);
3933 return -TARGET_EFAULT
;
3936 for(i
=0; i
<nsems
; i
++) {
3937 __get_user((*host_array
)[i
], &array
[i
]);
3939 unlock_user(array
, target_addr
, 0);
3944 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
3945 unsigned short **host_array
)
3948 unsigned short *array
;
3950 struct semid_ds semid_ds
;
3953 semun
.buf
= &semid_ds
;
3955 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3957 return get_errno(ret
);
3959 nsems
= semid_ds
.sem_nsems
;
3961 array
= lock_user(VERIFY_WRITE
, target_addr
,
3962 nsems
*sizeof(unsigned short), 0);
3964 return -TARGET_EFAULT
;
3966 for(i
=0; i
<nsems
; i
++) {
3967 __put_user((*host_array
)[i
], &array
[i
]);
3969 g_free(*host_array
);
3970 unlock_user(array
, target_addr
, 1);
3975 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
3976 abi_ulong target_arg
)
3978 union target_semun target_su
= { .buf
= target_arg
};
3980 struct semid_ds dsarg
;
3981 unsigned short *array
= NULL
;
3982 struct seminfo seminfo
;
3983 abi_long ret
= -TARGET_EINVAL
;
3990 /* In 64 bit cross-endian situations, we will erroneously pick up
3991 * the wrong half of the union for the "val" element. To rectify
3992 * this, the entire 8-byte structure is byteswapped, followed by
3993 * a swap of the 4 byte val field. In other cases, the data is
3994 * already in proper host byte order. */
3995 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
3996 target_su
.buf
= tswapal(target_su
.buf
);
3997 arg
.val
= tswap32(target_su
.val
);
3999 arg
.val
= target_su
.val
;
4001 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4005 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
4009 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4010 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
4017 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
4021 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4022 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
4028 arg
.__buf
= &seminfo
;
4029 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4030 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
4038 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
4045 struct target_sembuf
{
4046 unsigned short sem_num
;
4051 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
4052 abi_ulong target_addr
,
4055 struct target_sembuf
*target_sembuf
;
4058 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
4059 nsops
*sizeof(struct target_sembuf
), 1);
4061 return -TARGET_EFAULT
;
4063 for(i
=0; i
<nsops
; i
++) {
4064 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
4065 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
4066 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
4069 unlock_user(target_sembuf
, target_addr
, 0);
4074 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
4076 struct sembuf sops
[nsops
];
4078 if (target_to_host_sembuf(sops
, ptr
, nsops
))
4079 return -TARGET_EFAULT
;
4081 return get_errno(safe_semtimedop(semid
, sops
, nsops
, NULL
));
4084 struct target_msqid_ds
4086 struct target_ipc_perm msg_perm
;
4087 abi_ulong msg_stime
;
4088 #if TARGET_ABI_BITS == 32
4089 abi_ulong __unused1
;
4091 abi_ulong msg_rtime
;
4092 #if TARGET_ABI_BITS == 32
4093 abi_ulong __unused2
;
4095 abi_ulong msg_ctime
;
4096 #if TARGET_ABI_BITS == 32
4097 abi_ulong __unused3
;
4099 abi_ulong __msg_cbytes
;
4101 abi_ulong msg_qbytes
;
4102 abi_ulong msg_lspid
;
4103 abi_ulong msg_lrpid
;
4104 abi_ulong __unused4
;
4105 abi_ulong __unused5
;
4108 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
4109 abi_ulong target_addr
)
4111 struct target_msqid_ds
*target_md
;
4113 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
4114 return -TARGET_EFAULT
;
4115 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
4116 return -TARGET_EFAULT
;
4117 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
4118 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
4119 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
4120 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
4121 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
4122 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
4123 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
4124 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
4125 unlock_user_struct(target_md
, target_addr
, 0);
4129 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
4130 struct msqid_ds
*host_md
)
4132 struct target_msqid_ds
*target_md
;
4134 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
4135 return -TARGET_EFAULT
;
4136 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
4137 return -TARGET_EFAULT
;
4138 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
4139 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
4140 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
4141 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
4142 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
4143 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
4144 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
4145 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
4146 unlock_user_struct(target_md
, target_addr
, 1);
4150 struct target_msginfo
{
4158 unsigned short int msgseg
;
4161 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
4162 struct msginfo
*host_msginfo
)
4164 struct target_msginfo
*target_msginfo
;
4165 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
4166 return -TARGET_EFAULT
;
4167 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
4168 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
4169 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
4170 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
4171 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
4172 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
4173 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
4174 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
4175 unlock_user_struct(target_msginfo
, target_addr
, 1);
4179 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
4181 struct msqid_ds dsarg
;
4182 struct msginfo msginfo
;
4183 abi_long ret
= -TARGET_EINVAL
;
4191 if (target_to_host_msqid_ds(&dsarg
,ptr
))
4192 return -TARGET_EFAULT
;
4193 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
4194 if (host_to_target_msqid_ds(ptr
,&dsarg
))
4195 return -TARGET_EFAULT
;
4198 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
4202 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
4203 if (host_to_target_msginfo(ptr
, &msginfo
))
4204 return -TARGET_EFAULT
;
4211 struct target_msgbuf
{
4216 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
4217 ssize_t msgsz
, int msgflg
)
4219 struct target_msgbuf
*target_mb
;
4220 struct msgbuf
*host_mb
;
4224 return -TARGET_EINVAL
;
4227 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
4228 return -TARGET_EFAULT
;
4229 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4231 unlock_user_struct(target_mb
, msgp
, 0);
4232 return -TARGET_ENOMEM
;
4234 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
4235 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
4236 ret
= get_errno(safe_msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
4238 unlock_user_struct(target_mb
, msgp
, 0);
4243 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
4244 ssize_t msgsz
, abi_long msgtyp
,
4247 struct target_msgbuf
*target_mb
;
4249 struct msgbuf
*host_mb
;
4253 return -TARGET_EINVAL
;
4256 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
4257 return -TARGET_EFAULT
;
4259 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4261 ret
= -TARGET_ENOMEM
;
4264 ret
= get_errno(safe_msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
4267 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
4268 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
4269 if (!target_mtext
) {
4270 ret
= -TARGET_EFAULT
;
4273 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
4274 unlock_user(target_mtext
, target_mtext_addr
, ret
);
4277 target_mb
->mtype
= tswapal(host_mb
->mtype
);
4281 unlock_user_struct(target_mb
, msgp
, 1);
4286 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
4287 abi_ulong target_addr
)
4289 struct target_shmid_ds
*target_sd
;
4291 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4292 return -TARGET_EFAULT
;
4293 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
4294 return -TARGET_EFAULT
;
4295 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4296 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4297 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4298 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4299 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4300 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4301 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4302 unlock_user_struct(target_sd
, target_addr
, 0);
4306 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
4307 struct shmid_ds
*host_sd
)
4309 struct target_shmid_ds
*target_sd
;
4311 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4312 return -TARGET_EFAULT
;
4313 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
4314 return -TARGET_EFAULT
;
4315 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4316 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4317 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4318 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4319 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4320 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4321 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4322 unlock_user_struct(target_sd
, target_addr
, 1);
4326 struct target_shminfo
{
4334 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
4335 struct shminfo
*host_shminfo
)
4337 struct target_shminfo
*target_shminfo
;
4338 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
4339 return -TARGET_EFAULT
;
4340 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
4341 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
4342 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
4343 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
4344 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
4345 unlock_user_struct(target_shminfo
, target_addr
, 1);
4349 struct target_shm_info
{
4354 abi_ulong swap_attempts
;
4355 abi_ulong swap_successes
;
4358 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
4359 struct shm_info
*host_shm_info
)
4361 struct target_shm_info
*target_shm_info
;
4362 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
4363 return -TARGET_EFAULT
;
4364 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
4365 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
4366 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
4367 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
4368 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
4369 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
4370 unlock_user_struct(target_shm_info
, target_addr
, 1);
4374 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
4376 struct shmid_ds dsarg
;
4377 struct shminfo shminfo
;
4378 struct shm_info shm_info
;
4379 abi_long ret
= -TARGET_EINVAL
;
4387 if (target_to_host_shmid_ds(&dsarg
, buf
))
4388 return -TARGET_EFAULT
;
4389 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
4390 if (host_to_target_shmid_ds(buf
, &dsarg
))
4391 return -TARGET_EFAULT
;
4394 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
4395 if (host_to_target_shminfo(buf
, &shminfo
))
4396 return -TARGET_EFAULT
;
4399 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
4400 if (host_to_target_shm_info(buf
, &shm_info
))
4401 return -TARGET_EFAULT
;
4406 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
4413 static inline abi_ulong
do_shmat(int shmid
, abi_ulong shmaddr
, int shmflg
)
4417 struct shmid_ds shm_info
;
4420 /* find out the length of the shared memory segment */
4421 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
4422 if (is_error(ret
)) {
4423 /* can't get length, bail out */
4430 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
4432 abi_ulong mmap_start
;
4434 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
4436 if (mmap_start
== -1) {
4438 host_raddr
= (void *)-1;
4440 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
4443 if (host_raddr
== (void *)-1) {
4445 return get_errno((long)host_raddr
);
4447 raddr
=h2g((unsigned long)host_raddr
);
4449 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
4450 PAGE_VALID
| PAGE_READ
|
4451 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
4453 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
4454 if (!shm_regions
[i
].in_use
) {
4455 shm_regions
[i
].in_use
= true;
4456 shm_regions
[i
].start
= raddr
;
4457 shm_regions
[i
].size
= shm_info
.shm_segsz
;
4467 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
4471 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
4472 if (shm_regions
[i
].in_use
&& shm_regions
[i
].start
== shmaddr
) {
4473 shm_regions
[i
].in_use
= false;
4474 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
4479 return get_errno(shmdt(g2h(shmaddr
)));
4482 #ifdef TARGET_NR_ipc
4483 /* ??? This only works with linear mappings. */
4484 /* do_ipc() must return target values and target errnos. */
4485 static abi_long
do_ipc(unsigned int call
, abi_long first
,
4486 abi_long second
, abi_long third
,
4487 abi_long ptr
, abi_long fifth
)
4492 version
= call
>> 16;
4497 ret
= do_semop(first
, ptr
, second
);
4501 ret
= get_errno(semget(first
, second
, third
));
4504 case IPCOP_semctl
: {
4505 /* The semun argument to semctl is passed by value, so dereference the
4508 get_user_ual(atptr
, ptr
);
4509 ret
= do_semctl(first
, second
, third
, atptr
);
4514 ret
= get_errno(msgget(first
, second
));
4518 ret
= do_msgsnd(first
, ptr
, second
, third
);
4522 ret
= do_msgctl(first
, second
, ptr
);
4529 struct target_ipc_kludge
{
4534 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
4535 ret
= -TARGET_EFAULT
;
4539 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
4541 unlock_user_struct(tmp
, ptr
, 0);
4545 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
4554 raddr
= do_shmat(first
, ptr
, second
);
4555 if (is_error(raddr
))
4556 return get_errno(raddr
);
4557 if (put_user_ual(raddr
, third
))
4558 return -TARGET_EFAULT
;
4562 ret
= -TARGET_EINVAL
;
4567 ret
= do_shmdt(ptr
);
4571 /* IPC_* flag values are the same on all linux platforms */
4572 ret
= get_errno(shmget(first
, second
, third
));
4575 /* IPC_* and SHM_* command values are the same on all linux platforms */
4577 ret
= do_shmctl(first
, second
, ptr
);
4580 gemu_log("Unsupported ipc call: %d (version %d)\n", call
, version
);
4581 ret
= -TARGET_ENOSYS
;
4588 /* kernel structure types definitions */
4590 #define STRUCT(name, ...) STRUCT_ ## name,
4591 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
4593 #include "syscall_types.h"
4597 #undef STRUCT_SPECIAL
4599 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
4600 #define STRUCT_SPECIAL(name)
4601 #include "syscall_types.h"
4603 #undef STRUCT_SPECIAL
4605 typedef struct IOCTLEntry IOCTLEntry
;
4607 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4608 int fd
, int cmd
, abi_long arg
);
4612 unsigned int host_cmd
;
4615 do_ioctl_fn
*do_ioctl
;
4616 const argtype arg_type
[5];
4619 #define IOC_R 0x0001
4620 #define IOC_W 0x0002
4621 #define IOC_RW (IOC_R | IOC_W)
4623 #define MAX_STRUCT_SIZE 4096
4625 #ifdef CONFIG_FIEMAP
4626 /* So fiemap access checks don't overflow on 32 bit systems.
4627 * This is very slightly smaller than the limit imposed by
4628 * the underlying kernel.
4630 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4631 / sizeof(struct fiemap_extent))
4633 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4634 int fd
, int cmd
, abi_long arg
)
4636 /* The parameter for this ioctl is a struct fiemap followed
4637 * by an array of struct fiemap_extent whose size is set
4638 * in fiemap->fm_extent_count. The array is filled in by the
4641 int target_size_in
, target_size_out
;
4643 const argtype
*arg_type
= ie
->arg_type
;
4644 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
4647 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
4651 assert(arg_type
[0] == TYPE_PTR
);
4652 assert(ie
->access
== IOC_RW
);
4654 target_size_in
= thunk_type_size(arg_type
, 0);
4655 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
4657 return -TARGET_EFAULT
;
4659 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4660 unlock_user(argptr
, arg
, 0);
4661 fm
= (struct fiemap
*)buf_temp
;
4662 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
4663 return -TARGET_EINVAL
;
4666 outbufsz
= sizeof (*fm
) +
4667 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
4669 if (outbufsz
> MAX_STRUCT_SIZE
) {
4670 /* We can't fit all the extents into the fixed size buffer.
4671 * Allocate one that is large enough and use it instead.
4673 fm
= g_try_malloc(outbufsz
);
4675 return -TARGET_ENOMEM
;
4677 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
4680 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, fm
));
4681 if (!is_error(ret
)) {
4682 target_size_out
= target_size_in
;
4683 /* An extent_count of 0 means we were only counting the extents
4684 * so there are no structs to copy
4686 if (fm
->fm_extent_count
!= 0) {
4687 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
4689 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
4691 ret
= -TARGET_EFAULT
;
4693 /* Convert the struct fiemap */
4694 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
4695 if (fm
->fm_extent_count
!= 0) {
4696 p
= argptr
+ target_size_in
;
4697 /* ...and then all the struct fiemap_extents */
4698 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
4699 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
4704 unlock_user(argptr
, arg
, target_size_out
);
4714 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4715 int fd
, int cmd
, abi_long arg
)
4717 const argtype
*arg_type
= ie
->arg_type
;
4721 struct ifconf
*host_ifconf
;
4723 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
4724 int target_ifreq_size
;
4729 abi_long target_ifc_buf
;
4733 assert(arg_type
[0] == TYPE_PTR
);
4734 assert(ie
->access
== IOC_RW
);
4737 target_size
= thunk_type_size(arg_type
, 0);
4739 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4741 return -TARGET_EFAULT
;
4742 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4743 unlock_user(argptr
, arg
, 0);
4745 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
4746 target_ifc_len
= host_ifconf
->ifc_len
;
4747 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
4749 target_ifreq_size
= thunk_type_size(ifreq_arg_type
, 0);
4750 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
4751 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
4753 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
4754 if (outbufsz
> MAX_STRUCT_SIZE
) {
4755 /* We can't fit all the extents into the fixed size buffer.
4756 * Allocate one that is large enough and use it instead.
4758 host_ifconf
= malloc(outbufsz
);
4760 return -TARGET_ENOMEM
;
4762 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
4765 host_ifc_buf
= (char*)host_ifconf
+ sizeof(*host_ifconf
);
4767 host_ifconf
->ifc_len
= host_ifc_len
;
4768 host_ifconf
->ifc_buf
= host_ifc_buf
;
4770 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_ifconf
));
4771 if (!is_error(ret
)) {
4772 /* convert host ifc_len to target ifc_len */
4774 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
4775 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
4776 host_ifconf
->ifc_len
= target_ifc_len
;
4778 /* restore target ifc_buf */
4780 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
4782 /* copy struct ifconf to target user */
4784 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4786 return -TARGET_EFAULT
;
4787 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
4788 unlock_user(argptr
, arg
, target_size
);
4790 /* copy ifreq[] to target user */
4792 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
4793 for (i
= 0; i
< nb_ifreq
; i
++) {
4794 thunk_convert(argptr
+ i
* target_ifreq_size
,
4795 host_ifc_buf
+ i
* sizeof(struct ifreq
),
4796 ifreq_arg_type
, THUNK_TARGET
);
4798 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
4808 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
4809 int cmd
, abi_long arg
)
4812 struct dm_ioctl
*host_dm
;
4813 abi_long guest_data
;
4814 uint32_t guest_data_size
;
4816 const argtype
*arg_type
= ie
->arg_type
;
4818 void *big_buf
= NULL
;
4822 target_size
= thunk_type_size(arg_type
, 0);
4823 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4825 ret
= -TARGET_EFAULT
;
4828 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4829 unlock_user(argptr
, arg
, 0);
4831 /* buf_temp is too small, so fetch things into a bigger buffer */
4832 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
4833 memcpy(big_buf
, buf_temp
, target_size
);
4837 guest_data
= arg
+ host_dm
->data_start
;
4838 if ((guest_data
- arg
) < 0) {
4842 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
4843 host_data
= (char*)host_dm
+ host_dm
->data_start
;
4845 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
4846 switch (ie
->host_cmd
) {
4848 case DM_LIST_DEVICES
:
4851 case DM_DEV_SUSPEND
:
4854 case DM_TABLE_STATUS
:
4855 case DM_TABLE_CLEAR
:
4857 case DM_LIST_VERSIONS
:
4861 case DM_DEV_SET_GEOMETRY
:
4862 /* data contains only strings */
4863 memcpy(host_data
, argptr
, guest_data_size
);
4866 memcpy(host_data
, argptr
, guest_data_size
);
4867 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
4871 void *gspec
= argptr
;
4872 void *cur_data
= host_data
;
4873 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
4874 int spec_size
= thunk_type_size(arg_type
, 0);
4877 for (i
= 0; i
< host_dm
->target_count
; i
++) {
4878 struct dm_target_spec
*spec
= cur_data
;
4882 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
4883 slen
= strlen((char*)gspec
+ spec_size
) + 1;
4885 spec
->next
= sizeof(*spec
) + slen
;
4886 strcpy((char*)&spec
[1], gspec
+ spec_size
);
4888 cur_data
+= spec
->next
;
4893 ret
= -TARGET_EINVAL
;
4894 unlock_user(argptr
, guest_data
, 0);
4897 unlock_user(argptr
, guest_data
, 0);
4899 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
4900 if (!is_error(ret
)) {
4901 guest_data
= arg
+ host_dm
->data_start
;
4902 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
4903 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
4904 switch (ie
->host_cmd
) {
4909 case DM_DEV_SUSPEND
:
4912 case DM_TABLE_CLEAR
:
4914 case DM_DEV_SET_GEOMETRY
:
4915 /* no return data */
4917 case DM_LIST_DEVICES
:
4919 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
4920 uint32_t remaining_data
= guest_data_size
;
4921 void *cur_data
= argptr
;
4922 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
4923 int nl_size
= 12; /* can't use thunk_size due to alignment */
4926 uint32_t next
= nl
->next
;
4928 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
4930 if (remaining_data
< nl
->next
) {
4931 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
4934 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
4935 strcpy(cur_data
+ nl_size
, nl
->name
);
4936 cur_data
+= nl
->next
;
4937 remaining_data
-= nl
->next
;
4941 nl
= (void*)nl
+ next
;
4946 case DM_TABLE_STATUS
:
4948 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
4949 void *cur_data
= argptr
;
4950 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
4951 int spec_size
= thunk_type_size(arg_type
, 0);
4954 for (i
= 0; i
< host_dm
->target_count
; i
++) {
4955 uint32_t next
= spec
->next
;
4956 int slen
= strlen((char*)&spec
[1]) + 1;
4957 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
4958 if (guest_data_size
< spec
->next
) {
4959 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
4962 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
4963 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
4964 cur_data
= argptr
+ spec
->next
;
4965 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
4971 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
4972 int count
= *(uint32_t*)hdata
;
4973 uint64_t *hdev
= hdata
+ 8;
4974 uint64_t *gdev
= argptr
+ 8;
4977 *(uint32_t*)argptr
= tswap32(count
);
4978 for (i
= 0; i
< count
; i
++) {
4979 *gdev
= tswap64(*hdev
);
4985 case DM_LIST_VERSIONS
:
4987 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
4988 uint32_t remaining_data
= guest_data_size
;
4989 void *cur_data
= argptr
;
4990 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
4991 int vers_size
= thunk_type_size(arg_type
, 0);
4994 uint32_t next
= vers
->next
;
4996 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
4998 if (remaining_data
< vers
->next
) {
4999 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5002 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
5003 strcpy(cur_data
+ vers_size
, vers
->name
);
5004 cur_data
+= vers
->next
;
5005 remaining_data
-= vers
->next
;
5009 vers
= (void*)vers
+ next
;
5014 unlock_user(argptr
, guest_data
, 0);
5015 ret
= -TARGET_EINVAL
;
5018 unlock_user(argptr
, guest_data
, guest_data_size
);
5020 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5022 ret
= -TARGET_EFAULT
;
5025 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5026 unlock_user(argptr
, arg
, target_size
);
5033 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5034 int cmd
, abi_long arg
)
5038 const argtype
*arg_type
= ie
->arg_type
;
5039 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
5042 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
5043 struct blkpg_partition host_part
;
5045 /* Read and convert blkpg */
5047 target_size
= thunk_type_size(arg_type
, 0);
5048 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5050 ret
= -TARGET_EFAULT
;
5053 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5054 unlock_user(argptr
, arg
, 0);
5056 switch (host_blkpg
->op
) {
5057 case BLKPG_ADD_PARTITION
:
5058 case BLKPG_DEL_PARTITION
:
5059 /* payload is struct blkpg_partition */
5062 /* Unknown opcode */
5063 ret
= -TARGET_EINVAL
;
5067 /* Read and convert blkpg->data */
5068 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
5069 target_size
= thunk_type_size(part_arg_type
, 0);
5070 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5072 ret
= -TARGET_EFAULT
;
5075 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
5076 unlock_user(argptr
, arg
, 0);
5078 /* Swizzle the data pointer to our local copy and call! */
5079 host_blkpg
->data
= &host_part
;
5080 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_blkpg
));
5086 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5087 int fd
, int cmd
, abi_long arg
)
5089 const argtype
*arg_type
= ie
->arg_type
;
5090 const StructEntry
*se
;
5091 const argtype
*field_types
;
5092 const int *dst_offsets
, *src_offsets
;
5095 abi_ulong
*target_rt_dev_ptr
;
5096 unsigned long *host_rt_dev_ptr
;
5100 assert(ie
->access
== IOC_W
);
5101 assert(*arg_type
== TYPE_PTR
);
5103 assert(*arg_type
== TYPE_STRUCT
);
5104 target_size
= thunk_type_size(arg_type
, 0);
5105 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5107 return -TARGET_EFAULT
;
5110 assert(*arg_type
== (int)STRUCT_rtentry
);
5111 se
= struct_entries
+ *arg_type
++;
5112 assert(se
->convert
[0] == NULL
);
5113 /* convert struct here to be able to catch rt_dev string */
5114 field_types
= se
->field_types
;
5115 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
5116 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
5117 for (i
= 0; i
< se
->nb_fields
; i
++) {
5118 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
5119 assert(*field_types
== TYPE_PTRVOID
);
5120 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
5121 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
5122 if (*target_rt_dev_ptr
!= 0) {
5123 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
5124 tswapal(*target_rt_dev_ptr
));
5125 if (!*host_rt_dev_ptr
) {
5126 unlock_user(argptr
, arg
, 0);
5127 return -TARGET_EFAULT
;
5130 *host_rt_dev_ptr
= 0;
5135 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
5136 argptr
+ src_offsets
[i
],
5137 field_types
, THUNK_HOST
);
5139 unlock_user(argptr
, arg
, 0);
5141 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5142 if (*host_rt_dev_ptr
!= 0) {
5143 unlock_user((void *)*host_rt_dev_ptr
,
5144 *target_rt_dev_ptr
, 0);
5149 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5150 int fd
, int cmd
, abi_long arg
)
5152 int sig
= target_to_host_signal(arg
);
5153 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, sig
));
5156 static IOCTLEntry ioctl_entries
[] = {
5157 #define IOCTL(cmd, access, ...) \
5158 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5159 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5160 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
5165 /* ??? Implement proper locking for ioctls. */
5166 /* do_ioctl() Must return target values and target errnos. */
5167 static abi_long
do_ioctl(int fd
, int cmd
, abi_long arg
)
5169 const IOCTLEntry
*ie
;
5170 const argtype
*arg_type
;
5172 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
5178 if (ie
->target_cmd
== 0) {
5179 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
5180 return -TARGET_ENOSYS
;
5182 if (ie
->target_cmd
== cmd
)
5186 arg_type
= ie
->arg_type
;
5188 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd
, ie
->name
);
5191 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
5194 switch(arg_type
[0]) {
5197 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
));
5201 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, arg
));
5205 target_size
= thunk_type_size(arg_type
, 0);
5206 switch(ie
->access
) {
5208 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5209 if (!is_error(ret
)) {
5210 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5212 return -TARGET_EFAULT
;
5213 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5214 unlock_user(argptr
, arg
, target_size
);
5218 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5220 return -TARGET_EFAULT
;
5221 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5222 unlock_user(argptr
, arg
, 0);
5223 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5227 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5229 return -TARGET_EFAULT
;
5230 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5231 unlock_user(argptr
, arg
, 0);
5232 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5233 if (!is_error(ret
)) {
5234 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5236 return -TARGET_EFAULT
;
5237 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5238 unlock_user(argptr
, arg
, target_size
);
5244 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5245 (long)cmd
, arg_type
[0]);
5246 ret
= -TARGET_ENOSYS
;
5252 static const bitmask_transtbl iflag_tbl
[] = {
5253 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
5254 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
5255 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
5256 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
5257 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
5258 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
5259 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
5260 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
5261 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
5262 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
5263 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
5264 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
5265 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
5266 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
5270 static const bitmask_transtbl oflag_tbl
[] = {
5271 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
5272 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
5273 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
5274 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
5275 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
5276 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
5277 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
5278 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
5279 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
5280 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
5281 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
5282 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
5283 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
5284 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
5285 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
5286 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
5287 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
5288 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
5289 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
5290 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
5291 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
5292 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
5293 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
5294 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
5298 static const bitmask_transtbl cflag_tbl
[] = {
5299 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
5300 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
5301 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
5302 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
5303 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
5304 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
5305 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
5306 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
5307 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
5308 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
5309 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
5310 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
5311 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
5312 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
5313 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
5314 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
5315 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
5316 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
5317 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
5318 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
5319 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
5320 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
5321 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
5322 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
5323 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
5324 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
5325 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
5326 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
5327 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
5328 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
5329 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
5333 static const bitmask_transtbl lflag_tbl
[] = {
5334 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
5335 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
5336 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
5337 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
5338 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
5339 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
5340 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
5341 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
5342 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
5343 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
5344 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
5345 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
5346 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
5347 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
5348 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
5352 static void target_to_host_termios (void *dst
, const void *src
)
5354 struct host_termios
*host
= dst
;
5355 const struct target_termios
*target
= src
;
5358 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
5360 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
5362 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
5364 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
5365 host
->c_line
= target
->c_line
;
5367 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
5368 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
5369 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
5370 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
5371 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
5372 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
5373 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
5374 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
5375 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
5376 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
5377 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
5378 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
5379 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
5380 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
5381 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
5382 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
5383 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
5384 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
5387 static void host_to_target_termios (void *dst
, const void *src
)
5389 struct target_termios
*target
= dst
;
5390 const struct host_termios
*host
= src
;
5393 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
5395 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
5397 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
5399 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
5400 target
->c_line
= host
->c_line
;
5402 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
5403 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
5404 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
5405 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
5406 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
5407 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
5408 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
5409 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
5410 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
5411 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
5412 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
5413 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
5414 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
5415 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
5416 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
5417 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
5418 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
5419 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
5422 static const StructEntry struct_termios_def
= {
5423 .convert
= { host_to_target_termios
, target_to_host_termios
},
5424 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
5425 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
5428 static bitmask_transtbl mmap_flags_tbl
[] = {
5429 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
5430 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
5431 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
5432 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
, MAP_ANONYMOUS
, MAP_ANONYMOUS
},
5433 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
, MAP_GROWSDOWN
, MAP_GROWSDOWN
},
5434 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
, MAP_DENYWRITE
, MAP_DENYWRITE
},
5435 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
, MAP_EXECUTABLE
, MAP_EXECUTABLE
},
5436 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
5437 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
, MAP_NORESERVE
,
5442 #if defined(TARGET_I386)
5444 /* NOTE: there is really one LDT for all the threads */
5445 static uint8_t *ldt_table
;
5447 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
5454 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
5455 if (size
> bytecount
)
5457 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
5459 return -TARGET_EFAULT
;
5460 /* ??? Should this by byteswapped? */
5461 memcpy(p
, ldt_table
, size
);
5462 unlock_user(p
, ptr
, size
);
5466 /* XXX: add locking support */
5467 static abi_long
write_ldt(CPUX86State
*env
,
5468 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
5470 struct target_modify_ldt_ldt_s ldt_info
;
5471 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5472 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
5473 int seg_not_present
, useable
, lm
;
5474 uint32_t *lp
, entry_1
, entry_2
;
5476 if (bytecount
!= sizeof(ldt_info
))
5477 return -TARGET_EINVAL
;
5478 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
5479 return -TARGET_EFAULT
;
5480 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
5481 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
5482 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
5483 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
5484 unlock_user_struct(target_ldt_info
, ptr
, 0);
5486 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
5487 return -TARGET_EINVAL
;
5488 seg_32bit
= ldt_info
.flags
& 1;
5489 contents
= (ldt_info
.flags
>> 1) & 3;
5490 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
5491 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
5492 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
5493 useable
= (ldt_info
.flags
>> 6) & 1;
5497 lm
= (ldt_info
.flags
>> 7) & 1;
5499 if (contents
== 3) {
5501 return -TARGET_EINVAL
;
5502 if (seg_not_present
== 0)
5503 return -TARGET_EINVAL
;
5505 /* allocate the LDT */
5507 env
->ldt
.base
= target_mmap(0,
5508 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
5509 PROT_READ
|PROT_WRITE
,
5510 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
5511 if (env
->ldt
.base
== -1)
5512 return -TARGET_ENOMEM
;
5513 memset(g2h(env
->ldt
.base
), 0,
5514 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
5515 env
->ldt
.limit
= 0xffff;
5516 ldt_table
= g2h(env
->ldt
.base
);
5519 /* NOTE: same code as Linux kernel */
5520 /* Allow LDTs to be cleared by the user. */
5521 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
5524 read_exec_only
== 1 &&
5526 limit_in_pages
== 0 &&
5527 seg_not_present
== 1 &&
5535 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
5536 (ldt_info
.limit
& 0x0ffff);
5537 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
5538 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
5539 (ldt_info
.limit
& 0xf0000) |
5540 ((read_exec_only
^ 1) << 9) |
5542 ((seg_not_present
^ 1) << 15) |
5544 (limit_in_pages
<< 23) |
5548 entry_2
|= (useable
<< 20);
5550 /* Install the new entry ... */
5552 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
5553 lp
[0] = tswap32(entry_1
);
5554 lp
[1] = tswap32(entry_2
);
5558 /* specific and weird i386 syscalls */
5559 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
5560 unsigned long bytecount
)
5566 ret
= read_ldt(ptr
, bytecount
);
5569 ret
= write_ldt(env
, ptr
, bytecount
, 1);
5572 ret
= write_ldt(env
, ptr
, bytecount
, 0);
5575 ret
= -TARGET_ENOSYS
;
5581 #if defined(TARGET_I386) && defined(TARGET_ABI32)
5582 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
5584 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
5585 struct target_modify_ldt_ldt_s ldt_info
;
5586 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5587 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
5588 int seg_not_present
, useable
, lm
;
5589 uint32_t *lp
, entry_1
, entry_2
;
5592 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
5593 if (!target_ldt_info
)
5594 return -TARGET_EFAULT
;
5595 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
5596 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
5597 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
5598 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
5599 if (ldt_info
.entry_number
== -1) {
5600 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
5601 if (gdt_table
[i
] == 0) {
5602 ldt_info
.entry_number
= i
;
5603 target_ldt_info
->entry_number
= tswap32(i
);
5608 unlock_user_struct(target_ldt_info
, ptr
, 1);
5610 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
5611 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
5612 return -TARGET_EINVAL
;
5613 seg_32bit
= ldt_info
.flags
& 1;
5614 contents
= (ldt_info
.flags
>> 1) & 3;
5615 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
5616 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
5617 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
5618 useable
= (ldt_info
.flags
>> 6) & 1;
5622 lm
= (ldt_info
.flags
>> 7) & 1;
5625 if (contents
== 3) {
5626 if (seg_not_present
== 0)
5627 return -TARGET_EINVAL
;
5630 /* NOTE: same code as Linux kernel */
5631 /* Allow LDTs to be cleared by the user. */
5632 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
5633 if ((contents
== 0 &&
5634 read_exec_only
== 1 &&
5636 limit_in_pages
== 0 &&
5637 seg_not_present
== 1 &&
5645 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
5646 (ldt_info
.limit
& 0x0ffff);
5647 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
5648 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
5649 (ldt_info
.limit
& 0xf0000) |
5650 ((read_exec_only
^ 1) << 9) |
5652 ((seg_not_present
^ 1) << 15) |
5654 (limit_in_pages
<< 23) |
5659 /* Install the new entry ... */
5661 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
5662 lp
[0] = tswap32(entry_1
);
5663 lp
[1] = tswap32(entry_2
);
5667 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
5669 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5670 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
5671 uint32_t base_addr
, limit
, flags
;
5672 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
5673 int seg_not_present
, useable
, lm
;
5674 uint32_t *lp
, entry_1
, entry_2
;
5676 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
5677 if (!target_ldt_info
)
5678 return -TARGET_EFAULT
;
5679 idx
= tswap32(target_ldt_info
->entry_number
);
5680 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
5681 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
5682 unlock_user_struct(target_ldt_info
, ptr
, 1);
5683 return -TARGET_EINVAL
;
5685 lp
= (uint32_t *)(gdt_table
+ idx
);
5686 entry_1
= tswap32(lp
[0]);
5687 entry_2
= tswap32(lp
[1]);
5689 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
5690 contents
= (entry_2
>> 10) & 3;
5691 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
5692 seg_32bit
= (entry_2
>> 22) & 1;
5693 limit_in_pages
= (entry_2
>> 23) & 1;
5694 useable
= (entry_2
>> 20) & 1;
5698 lm
= (entry_2
>> 21) & 1;
5700 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
5701 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
5702 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
5703 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
5704 base_addr
= (entry_1
>> 16) |
5705 (entry_2
& 0xff000000) |
5706 ((entry_2
& 0xff) << 16);
5707 target_ldt_info
->base_addr
= tswapal(base_addr
);
5708 target_ldt_info
->limit
= tswap32(limit
);
5709 target_ldt_info
->flags
= tswap32(flags
);
5710 unlock_user_struct(target_ldt_info
, ptr
, 1);
5713 #endif /* TARGET_I386 && TARGET_ABI32 */
5715 #ifndef TARGET_ABI32
5716 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
5723 case TARGET_ARCH_SET_GS
:
5724 case TARGET_ARCH_SET_FS
:
5725 if (code
== TARGET_ARCH_SET_GS
)
5729 cpu_x86_load_seg(env
, idx
, 0);
5730 env
->segs
[idx
].base
= addr
;
5732 case TARGET_ARCH_GET_GS
:
5733 case TARGET_ARCH_GET_FS
:
5734 if (code
== TARGET_ARCH_GET_GS
)
5738 val
= env
->segs
[idx
].base
;
5739 if (put_user(val
, addr
, abi_ulong
))
5740 ret
= -TARGET_EFAULT
;
5743 ret
= -TARGET_EINVAL
;
5750 #endif /* defined(TARGET_I386) */
5752 #define NEW_STACK_SIZE 0x40000
5755 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
5758 pthread_mutex_t mutex
;
5759 pthread_cond_t cond
;
5762 abi_ulong child_tidptr
;
5763 abi_ulong parent_tidptr
;
5767 static void *clone_func(void *arg
)
5769 new_thread_info
*info
= arg
;
5774 rcu_register_thread();
5776 cpu
= ENV_GET_CPU(env
);
5778 ts
= (TaskState
*)cpu
->opaque
;
5779 info
->tid
= gettid();
5780 cpu
->host_tid
= info
->tid
;
5782 if (info
->child_tidptr
)
5783 put_user_u32(info
->tid
, info
->child_tidptr
);
5784 if (info
->parent_tidptr
)
5785 put_user_u32(info
->tid
, info
->parent_tidptr
);
5786 /* Enable signals. */
5787 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
5788 /* Signal to the parent that we're ready. */
5789 pthread_mutex_lock(&info
->mutex
);
5790 pthread_cond_broadcast(&info
->cond
);
5791 pthread_mutex_unlock(&info
->mutex
);
5792 /* Wait until the parent has finshed initializing the tls state. */
5793 pthread_mutex_lock(&clone_lock
);
5794 pthread_mutex_unlock(&clone_lock
);
5800 /* do_fork() Must return host values and target errnos (unlike most
5801 do_*() functions). */
5802 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
5803 abi_ulong parent_tidptr
, target_ulong newtls
,
5804 abi_ulong child_tidptr
)
5806 CPUState
*cpu
= ENV_GET_CPU(env
);
5810 CPUArchState
*new_env
;
5811 unsigned int nptl_flags
;
5814 /* Emulate vfork() with fork() */
5815 if (flags
& CLONE_VFORK
)
5816 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
5818 if (flags
& CLONE_VM
) {
5819 TaskState
*parent_ts
= (TaskState
*)cpu
->opaque
;
5820 new_thread_info info
;
5821 pthread_attr_t attr
;
5823 ts
= g_new0(TaskState
, 1);
5824 init_task_state(ts
);
5825 /* we create a new CPU instance. */
5826 new_env
= cpu_copy(env
);
5827 /* Init regs that differ from the parent. */
5828 cpu_clone_regs(new_env
, newsp
);
5829 new_cpu
= ENV_GET_CPU(new_env
);
5830 new_cpu
->opaque
= ts
;
5831 ts
->bprm
= parent_ts
->bprm
;
5832 ts
->info
= parent_ts
->info
;
5833 ts
->signal_mask
= parent_ts
->signal_mask
;
5835 flags
&= ~CLONE_NPTL_FLAGS2
;
5837 if (nptl_flags
& CLONE_CHILD_CLEARTID
) {
5838 ts
->child_tidptr
= child_tidptr
;
5841 if (nptl_flags
& CLONE_SETTLS
)
5842 cpu_set_tls (new_env
, newtls
);
5844 /* Grab a mutex so that thread setup appears atomic. */
5845 pthread_mutex_lock(&clone_lock
);
5847 memset(&info
, 0, sizeof(info
));
5848 pthread_mutex_init(&info
.mutex
, NULL
);
5849 pthread_mutex_lock(&info
.mutex
);
5850 pthread_cond_init(&info
.cond
, NULL
);
5852 if (nptl_flags
& CLONE_CHILD_SETTID
)
5853 info
.child_tidptr
= child_tidptr
;
5854 if (nptl_flags
& CLONE_PARENT_SETTID
)
5855 info
.parent_tidptr
= parent_tidptr
;
5857 ret
= pthread_attr_init(&attr
);
5858 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
5859 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
5860 /* It is not safe to deliver signals until the child has finished
5861 initializing, so temporarily block all signals. */
5862 sigfillset(&sigmask
);
5863 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
5865 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
5866 /* TODO: Free new CPU state if thread creation failed. */
5868 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
5869 pthread_attr_destroy(&attr
);
5871 /* Wait for the child to initialize. */
5872 pthread_cond_wait(&info
.cond
, &info
.mutex
);
5874 if (flags
& CLONE_PARENT_SETTID
)
5875 put_user_u32(ret
, parent_tidptr
);
5879 pthread_mutex_unlock(&info
.mutex
);
5880 pthread_cond_destroy(&info
.cond
);
5881 pthread_mutex_destroy(&info
.mutex
);
5882 pthread_mutex_unlock(&clone_lock
);
5884 /* if no CLONE_VM, we consider it is a fork */
5885 if ((flags
& ~(CSIGNAL
| CLONE_NPTL_FLAGS2
)) != 0) {
5886 return -TARGET_EINVAL
;
5889 if (block_signals()) {
5890 return -TARGET_ERESTARTSYS
;
5896 /* Child Process. */
5898 cpu_clone_regs(env
, newsp
);
5900 /* There is a race condition here. The parent process could
5901 theoretically read the TID in the child process before the child
5902 tid is set. This would require using either ptrace
5903 (not implemented) or having *_tidptr to point at a shared memory
5904 mapping. We can't repeat the spinlock hack used above because
5905 the child process gets its own copy of the lock. */
5906 if (flags
& CLONE_CHILD_SETTID
)
5907 put_user_u32(gettid(), child_tidptr
);
5908 if (flags
& CLONE_PARENT_SETTID
)
5909 put_user_u32(gettid(), parent_tidptr
);
5910 ts
= (TaskState
*)cpu
->opaque
;
5911 if (flags
& CLONE_SETTLS
)
5912 cpu_set_tls (env
, newtls
);
5913 if (flags
& CLONE_CHILD_CLEARTID
)
5914 ts
->child_tidptr
= child_tidptr
;
5922 /* warning : doesn't handle linux specific flags... */
5923 static int target_to_host_fcntl_cmd(int cmd
)
5926 case TARGET_F_DUPFD
:
5927 case TARGET_F_GETFD
:
5928 case TARGET_F_SETFD
:
5929 case TARGET_F_GETFL
:
5930 case TARGET_F_SETFL
:
5932 case TARGET_F_GETLK
:
5934 case TARGET_F_SETLK
:
5936 case TARGET_F_SETLKW
:
5938 case TARGET_F_GETOWN
:
5940 case TARGET_F_SETOWN
:
5942 case TARGET_F_GETSIG
:
5944 case TARGET_F_SETSIG
:
5946 #if TARGET_ABI_BITS == 32
5947 case TARGET_F_GETLK64
:
5949 case TARGET_F_SETLK64
:
5951 case TARGET_F_SETLKW64
:
5954 case TARGET_F_SETLEASE
:
5956 case TARGET_F_GETLEASE
:
5958 #ifdef F_DUPFD_CLOEXEC
5959 case TARGET_F_DUPFD_CLOEXEC
:
5960 return F_DUPFD_CLOEXEC
;
5962 case TARGET_F_NOTIFY
:
5965 case TARGET_F_GETOWN_EX
:
5969 case TARGET_F_SETOWN_EX
:
5973 case TARGET_F_SETPIPE_SZ
:
5974 return F_SETPIPE_SZ
;
5975 case TARGET_F_GETPIPE_SZ
:
5976 return F_GETPIPE_SZ
;
5979 return -TARGET_EINVAL
;
5981 return -TARGET_EINVAL
;
5984 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
5985 static const bitmask_transtbl flock_tbl
[] = {
5986 TRANSTBL_CONVERT(F_RDLCK
),
5987 TRANSTBL_CONVERT(F_WRLCK
),
5988 TRANSTBL_CONVERT(F_UNLCK
),
5989 TRANSTBL_CONVERT(F_EXLCK
),
5990 TRANSTBL_CONVERT(F_SHLCK
),
5994 static inline abi_long
copy_from_user_flock(struct flock64
*fl
,
5995 abi_ulong target_flock_addr
)
5997 struct target_flock
*target_fl
;
6000 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6001 return -TARGET_EFAULT
;
6004 __get_user(l_type
, &target_fl
->l_type
);
6005 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6006 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6007 __get_user(fl
->l_start
, &target_fl
->l_start
);
6008 __get_user(fl
->l_len
, &target_fl
->l_len
);
6009 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6010 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6014 static inline abi_long
copy_to_user_flock(abi_ulong target_flock_addr
,
6015 const struct flock64
*fl
)
6017 struct target_flock
*target_fl
;
6020 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6021 return -TARGET_EFAULT
;
6024 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6025 __put_user(l_type
, &target_fl
->l_type
);
6026 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6027 __put_user(fl
->l_start
, &target_fl
->l_start
);
6028 __put_user(fl
->l_len
, &target_fl
->l_len
);
6029 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6030 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6034 typedef abi_long
from_flock64_fn(struct flock64
*fl
, abi_ulong target_addr
);
6035 typedef abi_long
to_flock64_fn(abi_ulong target_addr
, const struct flock64
*fl
);
6037 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6038 static inline abi_long
copy_from_user_eabi_flock64(struct flock64
*fl
,
6039 abi_ulong target_flock_addr
)
6041 struct target_eabi_flock64
*target_fl
;
6044 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6045 return -TARGET_EFAULT
;
6048 __get_user(l_type
, &target_fl
->l_type
);
6049 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6050 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6051 __get_user(fl
->l_start
, &target_fl
->l_start
);
6052 __get_user(fl
->l_len
, &target_fl
->l_len
);
6053 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6054 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6058 static inline abi_long
copy_to_user_eabi_flock64(abi_ulong target_flock_addr
,
6059 const struct flock64
*fl
)
6061 struct target_eabi_flock64
*target_fl
;
6064 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6065 return -TARGET_EFAULT
;
6068 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6069 __put_user(l_type
, &target_fl
->l_type
);
6070 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6071 __put_user(fl
->l_start
, &target_fl
->l_start
);
6072 __put_user(fl
->l_len
, &target_fl
->l_len
);
6073 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6074 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6079 static inline abi_long
copy_from_user_flock64(struct flock64
*fl
,
6080 abi_ulong target_flock_addr
)
6082 struct target_flock64
*target_fl
;
6085 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6086 return -TARGET_EFAULT
;
6089 __get_user(l_type
, &target_fl
->l_type
);
6090 fl
->l_type
= target_to_host_bitmask(l_type
, flock_tbl
);
6091 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6092 __get_user(fl
->l_start
, &target_fl
->l_start
);
6093 __get_user(fl
->l_len
, &target_fl
->l_len
);
6094 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6095 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6099 static inline abi_long
copy_to_user_flock64(abi_ulong target_flock_addr
,
6100 const struct flock64
*fl
)
6102 struct target_flock64
*target_fl
;
6105 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6106 return -TARGET_EFAULT
;
6109 l_type
= host_to_target_bitmask(fl
->l_type
, flock_tbl
);
6110 __put_user(l_type
, &target_fl
->l_type
);
6111 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6112 __put_user(fl
->l_start
, &target_fl
->l_start
);
6113 __put_user(fl
->l_len
, &target_fl
->l_len
);
6114 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6115 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6119 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
6121 struct flock64 fl64
;
6123 struct f_owner_ex fox
;
6124 struct target_f_owner_ex
*target_fox
;
6127 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
6129 if (host_cmd
== -TARGET_EINVAL
)
6133 case TARGET_F_GETLK
:
6134 ret
= copy_from_user_flock(&fl64
, arg
);
6138 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6140 ret
= copy_to_user_flock(arg
, &fl64
);
6144 case TARGET_F_SETLK
:
6145 case TARGET_F_SETLKW
:
6146 ret
= copy_from_user_flock(&fl64
, arg
);
6150 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6153 case TARGET_F_GETLK64
:
6154 ret
= copy_from_user_flock64(&fl64
, arg
);
6158 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6160 ret
= copy_to_user_flock64(arg
, &fl64
);
6163 case TARGET_F_SETLK64
:
6164 case TARGET_F_SETLKW64
:
6165 ret
= copy_from_user_flock64(&fl64
, arg
);
6169 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6172 case TARGET_F_GETFL
:
6173 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
6175 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
6179 case TARGET_F_SETFL
:
6180 ret
= get_errno(safe_fcntl(fd
, host_cmd
,
6181 target_to_host_bitmask(arg
,
6186 case TARGET_F_GETOWN_EX
:
6187 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
6189 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
6190 return -TARGET_EFAULT
;
6191 target_fox
->type
= tswap32(fox
.type
);
6192 target_fox
->pid
= tswap32(fox
.pid
);
6193 unlock_user_struct(target_fox
, arg
, 1);
6199 case TARGET_F_SETOWN_EX
:
6200 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
6201 return -TARGET_EFAULT
;
6202 fox
.type
= tswap32(target_fox
->type
);
6203 fox
.pid
= tswap32(target_fox
->pid
);
6204 unlock_user_struct(target_fox
, arg
, 0);
6205 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
6209 case TARGET_F_SETOWN
:
6210 case TARGET_F_GETOWN
:
6211 case TARGET_F_SETSIG
:
6212 case TARGET_F_GETSIG
:
6213 case TARGET_F_SETLEASE
:
6214 case TARGET_F_GETLEASE
:
6215 case TARGET_F_SETPIPE_SZ
:
6216 case TARGET_F_GETPIPE_SZ
:
6217 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
6221 ret
= get_errno(safe_fcntl(fd
, cmd
, arg
));
6229 static inline int high2lowuid(int uid
)
6237 static inline int high2lowgid(int gid
)
6245 static inline int low2highuid(int uid
)
6247 if ((int16_t)uid
== -1)
6253 static inline int low2highgid(int gid
)
6255 if ((int16_t)gid
== -1)
6260 static inline int tswapid(int id
)
6265 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
6267 #else /* !USE_UID16 */
6268 static inline int high2lowuid(int uid
)
6272 static inline int high2lowgid(int gid
)
6276 static inline int low2highuid(int uid
)
6280 static inline int low2highgid(int gid
)
6284 static inline int tswapid(int id
)
6289 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
6291 #endif /* USE_UID16 */
6293 /* We must do direct syscalls for setting UID/GID, because we want to
6294 * implement the Linux system call semantics of "change only for this thread",
6295 * not the libc/POSIX semantics of "change for all threads in process".
6296 * (See http://ewontfix.com/17/ for more details.)
6297 * We use the 32-bit version of the syscalls if present; if it is not
6298 * then either the host architecture supports 32-bit UIDs natively with
6299 * the standard syscall, or the 16-bit UID is the best we can do.
6301 #ifdef __NR_setuid32
6302 #define __NR_sys_setuid __NR_setuid32
6304 #define __NR_sys_setuid __NR_setuid
6306 #ifdef __NR_setgid32
6307 #define __NR_sys_setgid __NR_setgid32
6309 #define __NR_sys_setgid __NR_setgid
6311 #ifdef __NR_setresuid32
6312 #define __NR_sys_setresuid __NR_setresuid32
6314 #define __NR_sys_setresuid __NR_setresuid
6316 #ifdef __NR_setresgid32
6317 #define __NR_sys_setresgid __NR_setresgid32
6319 #define __NR_sys_setresgid __NR_setresgid
6322 _syscall1(int, sys_setuid
, uid_t
, uid
)
6323 _syscall1(int, sys_setgid
, gid_t
, gid
)
6324 _syscall3(int, sys_setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
6325 _syscall3(int, sys_setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
6327 void syscall_init(void)
6330 const argtype
*arg_type
;
6334 thunk_init(STRUCT_MAX
);
6336 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
6337 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
6338 #include "syscall_types.h"
6340 #undef STRUCT_SPECIAL
6342 /* Build target_to_host_errno_table[] table from
6343 * host_to_target_errno_table[]. */
6344 for (i
= 0; i
< ERRNO_TABLE_SIZE
; i
++) {
6345 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
6348 /* we patch the ioctl size if necessary. We rely on the fact that
6349 no ioctl has all the bits at '1' in the size field */
6351 while (ie
->target_cmd
!= 0) {
6352 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
6353 TARGET_IOC_SIZEMASK
) {
6354 arg_type
= ie
->arg_type
;
6355 if (arg_type
[0] != TYPE_PTR
) {
6356 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
6361 size
= thunk_type_size(arg_type
, 0);
6362 ie
->target_cmd
= (ie
->target_cmd
&
6363 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
6364 (size
<< TARGET_IOC_SIZESHIFT
);
6367 /* automatic consistency check if same arch */
6368 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6369 (defined(__x86_64__) && defined(TARGET_X86_64))
6370 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
6371 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
6372 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
6379 #if TARGET_ABI_BITS == 32
6380 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
6382 #ifdef TARGET_WORDS_BIGENDIAN
6383 return ((uint64_t)word0
<< 32) | word1
;
6385 return ((uint64_t)word1
<< 32) | word0
;
6388 #else /* TARGET_ABI_BITS == 32 */
6389 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
6393 #endif /* TARGET_ABI_BITS != 32 */
6395 #ifdef TARGET_NR_truncate64
6396 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
6401 if (regpairs_aligned(cpu_env
)) {
6405 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
6409 #ifdef TARGET_NR_ftruncate64
6410 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
6415 if (regpairs_aligned(cpu_env
)) {
6419 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
6423 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
6424 abi_ulong target_addr
)
6426 struct target_timespec
*target_ts
;
6428 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1))
6429 return -TARGET_EFAULT
;
6430 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
6431 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
6432 unlock_user_struct(target_ts
, target_addr
, 0);
6436 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
6437 struct timespec
*host_ts
)
6439 struct target_timespec
*target_ts
;
6441 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0))
6442 return -TARGET_EFAULT
;
6443 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
6444 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
6445 unlock_user_struct(target_ts
, target_addr
, 1);
6449 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_itspec
,
6450 abi_ulong target_addr
)
6452 struct target_itimerspec
*target_itspec
;
6454 if (!lock_user_struct(VERIFY_READ
, target_itspec
, target_addr
, 1)) {
6455 return -TARGET_EFAULT
;
6458 host_itspec
->it_interval
.tv_sec
=
6459 tswapal(target_itspec
->it_interval
.tv_sec
);
6460 host_itspec
->it_interval
.tv_nsec
=
6461 tswapal(target_itspec
->it_interval
.tv_nsec
);
6462 host_itspec
->it_value
.tv_sec
= tswapal(target_itspec
->it_value
.tv_sec
);
6463 host_itspec
->it_value
.tv_nsec
= tswapal(target_itspec
->it_value
.tv_nsec
);
6465 unlock_user_struct(target_itspec
, target_addr
, 1);
6469 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
6470 struct itimerspec
*host_its
)
6472 struct target_itimerspec
*target_itspec
;
6474 if (!lock_user_struct(VERIFY_WRITE
, target_itspec
, target_addr
, 0)) {
6475 return -TARGET_EFAULT
;
6478 target_itspec
->it_interval
.tv_sec
= tswapal(host_its
->it_interval
.tv_sec
);
6479 target_itspec
->it_interval
.tv_nsec
= tswapal(host_its
->it_interval
.tv_nsec
);
6481 target_itspec
->it_value
.tv_sec
= tswapal(host_its
->it_value
.tv_sec
);
6482 target_itspec
->it_value
.tv_nsec
= tswapal(host_its
->it_value
.tv_nsec
);
6484 unlock_user_struct(target_itspec
, target_addr
, 0);
6488 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
6489 abi_ulong target_addr
)
6491 struct target_sigevent
*target_sevp
;
6493 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
6494 return -TARGET_EFAULT
;
6497 /* This union is awkward on 64 bit systems because it has a 32 bit
6498 * integer and a pointer in it; we follow the conversion approach
6499 * used for handling sigval types in signal.c so the guest should get
6500 * the correct value back even if we did a 64 bit byteswap and it's
6501 * using the 32 bit integer.
6503 host_sevp
->sigev_value
.sival_ptr
=
6504 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
6505 host_sevp
->sigev_signo
=
6506 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
6507 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
6508 host_sevp
->_sigev_un
._tid
= tswap32(target_sevp
->_sigev_un
._tid
);
6510 unlock_user_struct(target_sevp
, target_addr
, 1);
6514 #if defined(TARGET_NR_mlockall)
6515 static inline int target_to_host_mlockall_arg(int arg
)
6519 if (arg
& TARGET_MLOCKALL_MCL_CURRENT
) {
6520 result
|= MCL_CURRENT
;
6522 if (arg
& TARGET_MLOCKALL_MCL_FUTURE
) {
6523 result
|= MCL_FUTURE
;
6529 static inline abi_long
host_to_target_stat64(void *cpu_env
,
6530 abi_ulong target_addr
,
6531 struct stat
*host_st
)
6533 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
6534 if (((CPUARMState
*)cpu_env
)->eabi
) {
6535 struct target_eabi_stat64
*target_st
;
6537 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
6538 return -TARGET_EFAULT
;
6539 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
6540 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
6541 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
6542 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6543 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
6545 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
6546 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
6547 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
6548 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
6549 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
6550 __put_user(host_st
->st_size
, &target_st
->st_size
);
6551 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
6552 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
6553 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
6554 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
6555 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
6556 unlock_user_struct(target_st
, target_addr
, 1);
6560 #if defined(TARGET_HAS_STRUCT_STAT64)
6561 struct target_stat64
*target_st
;
6563 struct target_stat
*target_st
;
6566 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
6567 return -TARGET_EFAULT
;
6568 memset(target_st
, 0, sizeof(*target_st
));
6569 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
6570 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
6571 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6572 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
6574 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
6575 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
6576 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
6577 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
6578 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
6579 /* XXX: better use of kernel struct */
6580 __put_user(host_st
->st_size
, &target_st
->st_size
);
6581 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
6582 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
6583 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
6584 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
6585 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
6586 unlock_user_struct(target_st
, target_addr
, 1);
6592 /* ??? Using host futex calls even when target atomic operations
6593 are not really atomic probably breaks things. However implementing
6594 futexes locally would make futexes shared between multiple processes
6595 tricky. However they're probably useless because guest atomic
6596 operations won't work either. */
6597 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
6598 target_ulong uaddr2
, int val3
)
6600 struct timespec ts
, *pts
;
6603 /* ??? We assume FUTEX_* constants are the same on both host
6605 #ifdef FUTEX_CMD_MASK
6606 base_op
= op
& FUTEX_CMD_MASK
;
6612 case FUTEX_WAIT_BITSET
:
6615 target_to_host_timespec(pts
, timeout
);
6619 return get_errno(safe_futex(g2h(uaddr
), op
, tswap32(val
),
6622 return get_errno(safe_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
6624 return get_errno(safe_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
6626 case FUTEX_CMP_REQUEUE
:
6628 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
6629 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
6630 But the prototype takes a `struct timespec *'; insert casts
6631 to satisfy the compiler. We do not need to tswap TIMEOUT
6632 since it's not compared to guest memory. */
6633 pts
= (struct timespec
*)(uintptr_t) timeout
;
6634 return get_errno(safe_futex(g2h(uaddr
), op
, val
, pts
,
6636 (base_op
== FUTEX_CMP_REQUEUE
6640 return -TARGET_ENOSYS
;
6643 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6644 static abi_long
do_name_to_handle_at(abi_long dirfd
, abi_long pathname
,
6645 abi_long handle
, abi_long mount_id
,
6648 struct file_handle
*target_fh
;
6649 struct file_handle
*fh
;
6653 unsigned int size
, total_size
;
6655 if (get_user_s32(size
, handle
)) {
6656 return -TARGET_EFAULT
;
6659 name
= lock_user_string(pathname
);
6661 return -TARGET_EFAULT
;
6664 total_size
= sizeof(struct file_handle
) + size
;
6665 target_fh
= lock_user(VERIFY_WRITE
, handle
, total_size
, 0);
6667 unlock_user(name
, pathname
, 0);
6668 return -TARGET_EFAULT
;
6671 fh
= g_malloc0(total_size
);
6672 fh
->handle_bytes
= size
;
6674 ret
= get_errno(name_to_handle_at(dirfd
, path(name
), fh
, &mid
, flags
));
6675 unlock_user(name
, pathname
, 0);
6677 /* man name_to_handle_at(2):
6678 * Other than the use of the handle_bytes field, the caller should treat
6679 * the file_handle structure as an opaque data type
6682 memcpy(target_fh
, fh
, total_size
);
6683 target_fh
->handle_bytes
= tswap32(fh
->handle_bytes
);
6684 target_fh
->handle_type
= tswap32(fh
->handle_type
);
6686 unlock_user(target_fh
, handle
, total_size
);
6688 if (put_user_s32(mid
, mount_id
)) {
6689 return -TARGET_EFAULT
;
6697 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6698 static abi_long
do_open_by_handle_at(abi_long mount_fd
, abi_long handle
,
6701 struct file_handle
*target_fh
;
6702 struct file_handle
*fh
;
6703 unsigned int size
, total_size
;
6706 if (get_user_s32(size
, handle
)) {
6707 return -TARGET_EFAULT
;
6710 total_size
= sizeof(struct file_handle
) + size
;
6711 target_fh
= lock_user(VERIFY_READ
, handle
, total_size
, 1);
6713 return -TARGET_EFAULT
;
6716 fh
= g_memdup(target_fh
, total_size
);
6717 fh
->handle_bytes
= size
;
6718 fh
->handle_type
= tswap32(target_fh
->handle_type
);
6720 ret
= get_errno(open_by_handle_at(mount_fd
, fh
,
6721 target_to_host_bitmask(flags
, fcntl_flags_tbl
)));
6725 unlock_user(target_fh
, handle
, total_size
);
6731 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
6733 /* signalfd siginfo conversion */
6736 host_to_target_signalfd_siginfo(struct signalfd_siginfo
*tinfo
,
6737 const struct signalfd_siginfo
*info
)
6739 int sig
= host_to_target_signal(info
->ssi_signo
);
6741 /* linux/signalfd.h defines a ssi_addr_lsb
6742 * not defined in sys/signalfd.h but used by some kernels
6745 #ifdef BUS_MCEERR_AO
6746 if (tinfo
->ssi_signo
== SIGBUS
&&
6747 (tinfo
->ssi_code
== BUS_MCEERR_AR
||
6748 tinfo
->ssi_code
== BUS_MCEERR_AO
)) {
6749 uint16_t *ssi_addr_lsb
= (uint16_t *)(&info
->ssi_addr
+ 1);
6750 uint16_t *tssi_addr_lsb
= (uint16_t *)(&tinfo
->ssi_addr
+ 1);
6751 *tssi_addr_lsb
= tswap16(*ssi_addr_lsb
);
6755 tinfo
->ssi_signo
= tswap32(sig
);
6756 tinfo
->ssi_errno
= tswap32(tinfo
->ssi_errno
);
6757 tinfo
->ssi_code
= tswap32(info
->ssi_code
);
6758 tinfo
->ssi_pid
= tswap32(info
->ssi_pid
);
6759 tinfo
->ssi_uid
= tswap32(info
->ssi_uid
);
6760 tinfo
->ssi_fd
= tswap32(info
->ssi_fd
);
6761 tinfo
->ssi_tid
= tswap32(info
->ssi_tid
);
6762 tinfo
->ssi_band
= tswap32(info
->ssi_band
);
6763 tinfo
->ssi_overrun
= tswap32(info
->ssi_overrun
);
6764 tinfo
->ssi_trapno
= tswap32(info
->ssi_trapno
);
6765 tinfo
->ssi_status
= tswap32(info
->ssi_status
);
6766 tinfo
->ssi_int
= tswap32(info
->ssi_int
);
6767 tinfo
->ssi_ptr
= tswap64(info
->ssi_ptr
);
6768 tinfo
->ssi_utime
= tswap64(info
->ssi_utime
);
6769 tinfo
->ssi_stime
= tswap64(info
->ssi_stime
);
6770 tinfo
->ssi_addr
= tswap64(info
->ssi_addr
);
6773 static abi_long
host_to_target_data_signalfd(void *buf
, size_t len
)
6777 for (i
= 0; i
< len
; i
+= sizeof(struct signalfd_siginfo
)) {
6778 host_to_target_signalfd_siginfo(buf
+ i
, buf
+ i
);
6784 static TargetFdTrans target_signalfd_trans
= {
6785 .host_to_target_data
= host_to_target_data_signalfd
,
6788 static abi_long
do_signalfd4(int fd
, abi_long mask
, int flags
)
6791 target_sigset_t
*target_mask
;
6795 if (flags
& ~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
)) {
6796 return -TARGET_EINVAL
;
6798 if (!lock_user_struct(VERIFY_READ
, target_mask
, mask
, 1)) {
6799 return -TARGET_EFAULT
;
6802 target_to_host_sigset(&host_mask
, target_mask
);
6804 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
6806 ret
= get_errno(signalfd(fd
, &host_mask
, host_flags
));
6808 fd_trans_register(ret
, &target_signalfd_trans
);
6811 unlock_user_struct(target_mask
, mask
, 0);
6817 /* Map host to target signal numbers for the wait family of syscalls.
6818 Assume all other status bits are the same. */
6819 int host_to_target_waitstatus(int status
)
6821 if (WIFSIGNALED(status
)) {
6822 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
6824 if (WIFSTOPPED(status
)) {
6825 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
6831 static int open_self_cmdline(void *cpu_env
, int fd
)
6834 bool word_skipped
= false;
6836 fd_orig
= open("/proc/self/cmdline", O_RDONLY
);
6846 nb_read
= read(fd_orig
, buf
, sizeof(buf
));
6849 fd_orig
= close(fd_orig
);
6852 } else if (nb_read
== 0) {
6856 if (!word_skipped
) {
6857 /* Skip the first string, which is the path to qemu-*-static
6858 instead of the actual command. */
6859 cp_buf
= memchr(buf
, 0, sizeof(buf
));
6861 /* Null byte found, skip one string */
6863 nb_read
-= cp_buf
- buf
;
6864 word_skipped
= true;
6869 if (write(fd
, cp_buf
, nb_read
) != nb_read
) {
6878 return close(fd_orig
);
6881 static int open_self_maps(void *cpu_env
, int fd
)
6883 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
6884 TaskState
*ts
= cpu
->opaque
;
6890 fp
= fopen("/proc/self/maps", "r");
6895 while ((read
= getline(&line
, &len
, fp
)) != -1) {
6896 int fields
, dev_maj
, dev_min
, inode
;
6897 uint64_t min
, max
, offset
;
6898 char flag_r
, flag_w
, flag_x
, flag_p
;
6899 char path
[512] = "";
6900 fields
= sscanf(line
, "%"PRIx64
"-%"PRIx64
" %c%c%c%c %"PRIx64
" %x:%x %d"
6901 " %512s", &min
, &max
, &flag_r
, &flag_w
, &flag_x
,
6902 &flag_p
, &offset
, &dev_maj
, &dev_min
, &inode
, path
);
6904 if ((fields
< 10) || (fields
> 11)) {
6907 if (h2g_valid(min
)) {
6908 int flags
= page_get_flags(h2g(min
));
6909 max
= h2g_valid(max
- 1) ? max
: (uintptr_t)g2h(GUEST_ADDR_MAX
);
6910 if (page_check_range(h2g(min
), max
- min
, flags
) == -1) {
6913 if (h2g(min
) == ts
->info
->stack_limit
) {
6914 pstrcpy(path
, sizeof(path
), " [stack]");
6916 dprintf(fd
, TARGET_ABI_FMT_lx
"-" TARGET_ABI_FMT_lx
6917 " %c%c%c%c %08" PRIx64
" %02x:%02x %d %s%s\n",
6918 h2g(min
), h2g(max
- 1) + 1, flag_r
, flag_w
,
6919 flag_x
, flag_p
, offset
, dev_maj
, dev_min
, inode
,
6920 path
[0] ? " " : "", path
);
6930 static int open_self_stat(void *cpu_env
, int fd
)
6932 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
6933 TaskState
*ts
= cpu
->opaque
;
6934 abi_ulong start_stack
= ts
->info
->start_stack
;
6937 for (i
= 0; i
< 44; i
++) {
6945 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
6946 } else if (i
== 1) {
6948 snprintf(buf
, sizeof(buf
), "(%s) ", ts
->bprm
->argv
[0]);
6949 } else if (i
== 27) {
6952 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
6954 /* for the rest, there is MasterCard */
6955 snprintf(buf
, sizeof(buf
), "0%c", i
== 43 ? '\n' : ' ');
6959 if (write(fd
, buf
, len
) != len
) {
6967 static int open_self_auxv(void *cpu_env
, int fd
)
6969 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
6970 TaskState
*ts
= cpu
->opaque
;
6971 abi_ulong auxv
= ts
->info
->saved_auxv
;
6972 abi_ulong len
= ts
->info
->auxv_len
;
6976 * Auxiliary vector is stored in target process stack.
6977 * read in whole auxv vector and copy it to file
6979 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
6983 r
= write(fd
, ptr
, len
);
6990 lseek(fd
, 0, SEEK_SET
);
6991 unlock_user(ptr
, auxv
, len
);
6997 static int is_proc_myself(const char *filename
, const char *entry
)
6999 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
7000 filename
+= strlen("/proc/");
7001 if (!strncmp(filename
, "self/", strlen("self/"))) {
7002 filename
+= strlen("self/");
7003 } else if (*filename
>= '1' && *filename
<= '9') {
7005 snprintf(myself
, sizeof(myself
), "%d/", getpid());
7006 if (!strncmp(filename
, myself
, strlen(myself
))) {
7007 filename
+= strlen(myself
);
7014 if (!strcmp(filename
, entry
)) {
7021 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7022 static int is_proc(const char *filename
, const char *entry
)
7024 return strcmp(filename
, entry
) == 0;
7027 static int open_net_route(void *cpu_env
, int fd
)
7034 fp
= fopen("/proc/net/route", "r");
7041 read
= getline(&line
, &len
, fp
);
7042 dprintf(fd
, "%s", line
);
7046 while ((read
= getline(&line
, &len
, fp
)) != -1) {
7048 uint32_t dest
, gw
, mask
;
7049 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
7050 sscanf(line
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7051 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
7052 &mask
, &mtu
, &window
, &irtt
);
7053 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7054 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
7055 metric
, tswap32(mask
), mtu
, window
, irtt
);
7065 static int do_openat(void *cpu_env
, int dirfd
, const char *pathname
, int flags
, mode_t mode
)
7068 const char *filename
;
7069 int (*fill
)(void *cpu_env
, int fd
);
7070 int (*cmp
)(const char *s1
, const char *s2
);
7072 const struct fake_open
*fake_open
;
7073 static const struct fake_open fakes
[] = {
7074 { "maps", open_self_maps
, is_proc_myself
},
7075 { "stat", open_self_stat
, is_proc_myself
},
7076 { "auxv", open_self_auxv
, is_proc_myself
},
7077 { "cmdline", open_self_cmdline
, is_proc_myself
},
7078 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7079 { "/proc/net/route", open_net_route
, is_proc
},
7081 { NULL
, NULL
, NULL
}
7084 if (is_proc_myself(pathname
, "exe")) {
7085 int execfd
= qemu_getauxval(AT_EXECFD
);
7086 return execfd
? execfd
: safe_openat(dirfd
, exec_path
, flags
, mode
);
7089 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
7090 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
7095 if (fake_open
->filename
) {
7097 char filename
[PATH_MAX
];
7100 /* create temporary file to map stat to */
7101 tmpdir
= getenv("TMPDIR");
7104 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
7105 fd
= mkstemp(filename
);
7111 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
7117 lseek(fd
, 0, SEEK_SET
);
7122 return safe_openat(dirfd
, path(pathname
), flags
, mode
);
7125 #define TIMER_MAGIC 0x0caf0000
7126 #define TIMER_MAGIC_MASK 0xffff0000
7128 /* Convert QEMU provided timer ID back to internal 16bit index format */
7129 static target_timer_t
get_timer_id(abi_long arg
)
7131 target_timer_t timerid
= arg
;
7133 if ((timerid
& TIMER_MAGIC_MASK
) != TIMER_MAGIC
) {
7134 return -TARGET_EINVAL
;
7139 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
7140 return -TARGET_EINVAL
;
7146 /* do_syscall() should always have a single exit point at the end so
7147 that actions, such as logging of syscall results, can be performed.
7148 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
7149 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
7150 abi_long arg2
, abi_long arg3
, abi_long arg4
,
7151 abi_long arg5
, abi_long arg6
, abi_long arg7
,
7154 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
7160 #if defined(DEBUG_ERESTARTSYS)
7161 /* Debug-only code for exercising the syscall-restart code paths
7162 * in the per-architecture cpu main loops: restart every syscall
7163 * the guest makes once before letting it through.
7170 return -TARGET_ERESTARTSYS
;
7176 gemu_log("syscall %d", num
);
7178 trace_guest_user_syscall(cpu
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
);
7180 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7183 case TARGET_NR_exit
:
7184 /* In old applications this may be used to implement _exit(2).
7185 However in threaded applictions it is used for thread termination,
7186 and _exit_group is used for application termination.
7187 Do thread termination if we have more then one thread. */
7189 if (block_signals()) {
7190 ret
= -TARGET_ERESTARTSYS
;
7194 if (CPU_NEXT(first_cpu
)) {
7198 /* Remove the CPU from the list. */
7199 QTAILQ_REMOVE(&cpus
, cpu
, node
);
7202 if (ts
->child_tidptr
) {
7203 put_user_u32(0, ts
->child_tidptr
);
7204 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
7208 object_unref(OBJECT(cpu
));
7210 rcu_unregister_thread();
7216 gdb_exit(cpu_env
, arg1
);
7218 ret
= 0; /* avoid warning */
7220 case TARGET_NR_read
:
7224 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
7226 ret
= get_errno(safe_read(arg1
, p
, arg3
));
7228 fd_trans_host_to_target_data(arg1
)) {
7229 ret
= fd_trans_host_to_target_data(arg1
)(p
, ret
);
7231 unlock_user(p
, arg2
, ret
);
7234 case TARGET_NR_write
:
7235 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
7237 ret
= get_errno(safe_write(arg1
, p
, arg3
));
7238 unlock_user(p
, arg2
, 0);
7240 #ifdef TARGET_NR_open
7241 case TARGET_NR_open
:
7242 if (!(p
= lock_user_string(arg1
)))
7244 ret
= get_errno(do_openat(cpu_env
, AT_FDCWD
, p
,
7245 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
7247 fd_trans_unregister(ret
);
7248 unlock_user(p
, arg1
, 0);
7251 case TARGET_NR_openat
:
7252 if (!(p
= lock_user_string(arg2
)))
7254 ret
= get_errno(do_openat(cpu_env
, arg1
, p
,
7255 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
7257 fd_trans_unregister(ret
);
7258 unlock_user(p
, arg2
, 0);
7260 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7261 case TARGET_NR_name_to_handle_at
:
7262 ret
= do_name_to_handle_at(arg1
, arg2
, arg3
, arg4
, arg5
);
7265 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7266 case TARGET_NR_open_by_handle_at
:
7267 ret
= do_open_by_handle_at(arg1
, arg2
, arg3
);
7268 fd_trans_unregister(ret
);
7271 case TARGET_NR_close
:
7272 fd_trans_unregister(arg1
);
7273 ret
= get_errno(close(arg1
));
7278 #ifdef TARGET_NR_fork
7279 case TARGET_NR_fork
:
7280 ret
= get_errno(do_fork(cpu_env
, SIGCHLD
, 0, 0, 0, 0));
7283 #ifdef TARGET_NR_waitpid
7284 case TARGET_NR_waitpid
:
7287 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, 0));
7288 if (!is_error(ret
) && arg2
&& ret
7289 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
7294 #ifdef TARGET_NR_waitid
7295 case TARGET_NR_waitid
:
7299 ret
= get_errno(safe_waitid(arg1
, arg2
, &info
, arg4
, NULL
));
7300 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
7301 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
7303 host_to_target_siginfo(p
, &info
);
7304 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
7309 #ifdef TARGET_NR_creat /* not on alpha */
7310 case TARGET_NR_creat
:
7311 if (!(p
= lock_user_string(arg1
)))
7313 ret
= get_errno(creat(p
, arg2
));
7314 fd_trans_unregister(ret
);
7315 unlock_user(p
, arg1
, 0);
7318 #ifdef TARGET_NR_link
7319 case TARGET_NR_link
:
7322 p
= lock_user_string(arg1
);
7323 p2
= lock_user_string(arg2
);
7325 ret
= -TARGET_EFAULT
;
7327 ret
= get_errno(link(p
, p2
));
7328 unlock_user(p2
, arg2
, 0);
7329 unlock_user(p
, arg1
, 0);
7333 #if defined(TARGET_NR_linkat)
7334 case TARGET_NR_linkat
:
7339 p
= lock_user_string(arg2
);
7340 p2
= lock_user_string(arg4
);
7342 ret
= -TARGET_EFAULT
;
7344 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
7345 unlock_user(p
, arg2
, 0);
7346 unlock_user(p2
, arg4
, 0);
7350 #ifdef TARGET_NR_unlink
7351 case TARGET_NR_unlink
:
7352 if (!(p
= lock_user_string(arg1
)))
7354 ret
= get_errno(unlink(p
));
7355 unlock_user(p
, arg1
, 0);
7358 #if defined(TARGET_NR_unlinkat)
7359 case TARGET_NR_unlinkat
:
7360 if (!(p
= lock_user_string(arg2
)))
7362 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
7363 unlock_user(p
, arg2
, 0);
7366 case TARGET_NR_execve
:
7368 char **argp
, **envp
;
7371 abi_ulong guest_argp
;
7372 abi_ulong guest_envp
;
7379 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
7380 if (get_user_ual(addr
, gp
))
7388 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
7389 if (get_user_ual(addr
, gp
))
7396 argp
= alloca((argc
+ 1) * sizeof(void *));
7397 envp
= alloca((envc
+ 1) * sizeof(void *));
7399 for (gp
= guest_argp
, q
= argp
; gp
;
7400 gp
+= sizeof(abi_ulong
), q
++) {
7401 if (get_user_ual(addr
, gp
))
7405 if (!(*q
= lock_user_string(addr
)))
7407 total_size
+= strlen(*q
) + 1;
7411 for (gp
= guest_envp
, q
= envp
; gp
;
7412 gp
+= sizeof(abi_ulong
), q
++) {
7413 if (get_user_ual(addr
, gp
))
7417 if (!(*q
= lock_user_string(addr
)))
7419 total_size
+= strlen(*q
) + 1;
7423 if (!(p
= lock_user_string(arg1
)))
7425 /* Although execve() is not an interruptible syscall it is
7426 * a special case where we must use the safe_syscall wrapper:
7427 * if we allow a signal to happen before we make the host
7428 * syscall then we will 'lose' it, because at the point of
7429 * execve the process leaves QEMU's control. So we use the
7430 * safe syscall wrapper to ensure that we either take the
7431 * signal as a guest signal, or else it does not happen
7432 * before the execve completes and makes it the other
7433 * program's problem.
7435 ret
= get_errno(safe_execve(p
, argp
, envp
));
7436 unlock_user(p
, arg1
, 0);
7441 ret
= -TARGET_EFAULT
;
7444 for (gp
= guest_argp
, q
= argp
; *q
;
7445 gp
+= sizeof(abi_ulong
), q
++) {
7446 if (get_user_ual(addr
, gp
)
7449 unlock_user(*q
, addr
, 0);
7451 for (gp
= guest_envp
, q
= envp
; *q
;
7452 gp
+= sizeof(abi_ulong
), q
++) {
7453 if (get_user_ual(addr
, gp
)
7456 unlock_user(*q
, addr
, 0);
7460 case TARGET_NR_chdir
:
7461 if (!(p
= lock_user_string(arg1
)))
7463 ret
= get_errno(chdir(p
));
7464 unlock_user(p
, arg1
, 0);
7466 #ifdef TARGET_NR_time
7467 case TARGET_NR_time
:
7470 ret
= get_errno(time(&host_time
));
7473 && put_user_sal(host_time
, arg1
))
7478 #ifdef TARGET_NR_mknod
7479 case TARGET_NR_mknod
:
7480 if (!(p
= lock_user_string(arg1
)))
7482 ret
= get_errno(mknod(p
, arg2
, arg3
));
7483 unlock_user(p
, arg1
, 0);
7486 #if defined(TARGET_NR_mknodat)
7487 case TARGET_NR_mknodat
:
7488 if (!(p
= lock_user_string(arg2
)))
7490 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
7491 unlock_user(p
, arg2
, 0);
7494 #ifdef TARGET_NR_chmod
7495 case TARGET_NR_chmod
:
7496 if (!(p
= lock_user_string(arg1
)))
7498 ret
= get_errno(chmod(p
, arg2
));
7499 unlock_user(p
, arg1
, 0);
7502 #ifdef TARGET_NR_break
7503 case TARGET_NR_break
:
7506 #ifdef TARGET_NR_oldstat
7507 case TARGET_NR_oldstat
:
7510 case TARGET_NR_lseek
:
7511 ret
= get_errno(lseek(arg1
, arg2
, arg3
));
7513 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
7514 /* Alpha specific */
7515 case TARGET_NR_getxpid
:
7516 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = getppid();
7517 ret
= get_errno(getpid());
7520 #ifdef TARGET_NR_getpid
7521 case TARGET_NR_getpid
:
7522 ret
= get_errno(getpid());
7525 case TARGET_NR_mount
:
7527 /* need to look at the data field */
7531 p
= lock_user_string(arg1
);
7539 p2
= lock_user_string(arg2
);
7542 unlock_user(p
, arg1
, 0);
7548 p3
= lock_user_string(arg3
);
7551 unlock_user(p
, arg1
, 0);
7553 unlock_user(p2
, arg2
, 0);
7560 /* FIXME - arg5 should be locked, but it isn't clear how to
7561 * do that since it's not guaranteed to be a NULL-terminated
7565 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
7567 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
));
7569 ret
= get_errno(ret
);
7572 unlock_user(p
, arg1
, 0);
7574 unlock_user(p2
, arg2
, 0);
7576 unlock_user(p3
, arg3
, 0);
7580 #ifdef TARGET_NR_umount
7581 case TARGET_NR_umount
:
7582 if (!(p
= lock_user_string(arg1
)))
7584 ret
= get_errno(umount(p
));
7585 unlock_user(p
, arg1
, 0);
7588 #ifdef TARGET_NR_stime /* not on alpha */
7589 case TARGET_NR_stime
:
7592 if (get_user_sal(host_time
, arg1
))
7594 ret
= get_errno(stime(&host_time
));
7598 case TARGET_NR_ptrace
:
7600 #ifdef TARGET_NR_alarm /* not on alpha */
7601 case TARGET_NR_alarm
:
7605 #ifdef TARGET_NR_oldfstat
7606 case TARGET_NR_oldfstat
:
7609 #ifdef TARGET_NR_pause /* not on alpha */
7610 case TARGET_NR_pause
:
7611 if (!block_signals()) {
7612 sigsuspend(&((TaskState
*)cpu
->opaque
)->signal_mask
);
7614 ret
= -TARGET_EINTR
;
7617 #ifdef TARGET_NR_utime
7618 case TARGET_NR_utime
:
7620 struct utimbuf tbuf
, *host_tbuf
;
7621 struct target_utimbuf
*target_tbuf
;
7623 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
7625 tbuf
.actime
= tswapal(target_tbuf
->actime
);
7626 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
7627 unlock_user_struct(target_tbuf
, arg2
, 0);
7632 if (!(p
= lock_user_string(arg1
)))
7634 ret
= get_errno(utime(p
, host_tbuf
));
7635 unlock_user(p
, arg1
, 0);
7639 #ifdef TARGET_NR_utimes
7640 case TARGET_NR_utimes
:
7642 struct timeval
*tvp
, tv
[2];
7644 if (copy_from_user_timeval(&tv
[0], arg2
)
7645 || copy_from_user_timeval(&tv
[1],
7646 arg2
+ sizeof(struct target_timeval
)))
7652 if (!(p
= lock_user_string(arg1
)))
7654 ret
= get_errno(utimes(p
, tvp
));
7655 unlock_user(p
, arg1
, 0);
7659 #if defined(TARGET_NR_futimesat)
7660 case TARGET_NR_futimesat
:
7662 struct timeval
*tvp
, tv
[2];
7664 if (copy_from_user_timeval(&tv
[0], arg3
)
7665 || copy_from_user_timeval(&tv
[1],
7666 arg3
+ sizeof(struct target_timeval
)))
7672 if (!(p
= lock_user_string(arg2
)))
7674 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
7675 unlock_user(p
, arg2
, 0);
7679 #ifdef TARGET_NR_stty
7680 case TARGET_NR_stty
:
7683 #ifdef TARGET_NR_gtty
7684 case TARGET_NR_gtty
:
7687 #ifdef TARGET_NR_access
7688 case TARGET_NR_access
:
7689 if (!(p
= lock_user_string(arg1
)))
7691 ret
= get_errno(access(path(p
), arg2
));
7692 unlock_user(p
, arg1
, 0);
7695 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
7696 case TARGET_NR_faccessat
:
7697 if (!(p
= lock_user_string(arg2
)))
7699 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
7700 unlock_user(p
, arg2
, 0);
7703 #ifdef TARGET_NR_nice /* not on alpha */
7704 case TARGET_NR_nice
:
7705 ret
= get_errno(nice(arg1
));
7708 #ifdef TARGET_NR_ftime
7709 case TARGET_NR_ftime
:
7712 case TARGET_NR_sync
:
7716 case TARGET_NR_kill
:
7717 ret
= get_errno(safe_kill(arg1
, target_to_host_signal(arg2
)));
7719 #ifdef TARGET_NR_rename
7720 case TARGET_NR_rename
:
7723 p
= lock_user_string(arg1
);
7724 p2
= lock_user_string(arg2
);
7726 ret
= -TARGET_EFAULT
;
7728 ret
= get_errno(rename(p
, p2
));
7729 unlock_user(p2
, arg2
, 0);
7730 unlock_user(p
, arg1
, 0);
7734 #if defined(TARGET_NR_renameat)
7735 case TARGET_NR_renameat
:
7738 p
= lock_user_string(arg2
);
7739 p2
= lock_user_string(arg4
);
7741 ret
= -TARGET_EFAULT
;
7743 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
7744 unlock_user(p2
, arg4
, 0);
7745 unlock_user(p
, arg2
, 0);
7749 #ifdef TARGET_NR_mkdir
7750 case TARGET_NR_mkdir
:
7751 if (!(p
= lock_user_string(arg1
)))
7753 ret
= get_errno(mkdir(p
, arg2
));
7754 unlock_user(p
, arg1
, 0);
7757 #if defined(TARGET_NR_mkdirat)
7758 case TARGET_NR_mkdirat
:
7759 if (!(p
= lock_user_string(arg2
)))
7761 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
7762 unlock_user(p
, arg2
, 0);
7765 #ifdef TARGET_NR_rmdir
7766 case TARGET_NR_rmdir
:
7767 if (!(p
= lock_user_string(arg1
)))
7769 ret
= get_errno(rmdir(p
));
7770 unlock_user(p
, arg1
, 0);
7774 ret
= get_errno(dup(arg1
));
7776 fd_trans_dup(arg1
, ret
);
7779 #ifdef TARGET_NR_pipe
7780 case TARGET_NR_pipe
:
7781 ret
= do_pipe(cpu_env
, arg1
, 0, 0);
7784 #ifdef TARGET_NR_pipe2
7785 case TARGET_NR_pipe2
:
7786 ret
= do_pipe(cpu_env
, arg1
,
7787 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
7790 case TARGET_NR_times
:
7792 struct target_tms
*tmsp
;
7794 ret
= get_errno(times(&tms
));
7796 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
7799 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
7800 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
7801 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
7802 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
7805 ret
= host_to_target_clock_t(ret
);
7808 #ifdef TARGET_NR_prof
7809 case TARGET_NR_prof
:
7812 #ifdef TARGET_NR_signal
7813 case TARGET_NR_signal
:
7816 case TARGET_NR_acct
:
7818 ret
= get_errno(acct(NULL
));
7820 if (!(p
= lock_user_string(arg1
)))
7822 ret
= get_errno(acct(path(p
)));
7823 unlock_user(p
, arg1
, 0);
7826 #ifdef TARGET_NR_umount2
7827 case TARGET_NR_umount2
:
7828 if (!(p
= lock_user_string(arg1
)))
7830 ret
= get_errno(umount2(p
, arg2
));
7831 unlock_user(p
, arg1
, 0);
7834 #ifdef TARGET_NR_lock
7835 case TARGET_NR_lock
:
7838 case TARGET_NR_ioctl
:
7839 ret
= do_ioctl(arg1
, arg2
, arg3
);
7841 case TARGET_NR_fcntl
:
7842 ret
= do_fcntl(arg1
, arg2
, arg3
);
7844 #ifdef TARGET_NR_mpx
7848 case TARGET_NR_setpgid
:
7849 ret
= get_errno(setpgid(arg1
, arg2
));
7851 #ifdef TARGET_NR_ulimit
7852 case TARGET_NR_ulimit
:
7855 #ifdef TARGET_NR_oldolduname
7856 case TARGET_NR_oldolduname
:
7859 case TARGET_NR_umask
:
7860 ret
= get_errno(umask(arg1
));
7862 case TARGET_NR_chroot
:
7863 if (!(p
= lock_user_string(arg1
)))
7865 ret
= get_errno(chroot(p
));
7866 unlock_user(p
, arg1
, 0);
7868 #ifdef TARGET_NR_ustat
7869 case TARGET_NR_ustat
:
7872 #ifdef TARGET_NR_dup2
7873 case TARGET_NR_dup2
:
7874 ret
= get_errno(dup2(arg1
, arg2
));
7876 fd_trans_dup(arg1
, arg2
);
7880 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
7881 case TARGET_NR_dup3
:
7882 ret
= get_errno(dup3(arg1
, arg2
, arg3
));
7884 fd_trans_dup(arg1
, arg2
);
7888 #ifdef TARGET_NR_getppid /* not on alpha */
7889 case TARGET_NR_getppid
:
7890 ret
= get_errno(getppid());
7893 #ifdef TARGET_NR_getpgrp
7894 case TARGET_NR_getpgrp
:
7895 ret
= get_errno(getpgrp());
7898 case TARGET_NR_setsid
:
7899 ret
= get_errno(setsid());
7901 #ifdef TARGET_NR_sigaction
7902 case TARGET_NR_sigaction
:
7904 #if defined(TARGET_ALPHA)
7905 struct target_sigaction act
, oact
, *pact
= 0;
7906 struct target_old_sigaction
*old_act
;
7908 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
7910 act
._sa_handler
= old_act
->_sa_handler
;
7911 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
7912 act
.sa_flags
= old_act
->sa_flags
;
7913 act
.sa_restorer
= 0;
7914 unlock_user_struct(old_act
, arg2
, 0);
7917 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
7918 if (!is_error(ret
) && arg3
) {
7919 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
7921 old_act
->_sa_handler
= oact
._sa_handler
;
7922 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
7923 old_act
->sa_flags
= oact
.sa_flags
;
7924 unlock_user_struct(old_act
, arg3
, 1);
7926 #elif defined(TARGET_MIPS)
7927 struct target_sigaction act
, oact
, *pact
, *old_act
;
7930 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
7932 act
._sa_handler
= old_act
->_sa_handler
;
7933 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
7934 act
.sa_flags
= old_act
->sa_flags
;
7935 unlock_user_struct(old_act
, arg2
, 0);
7941 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
7943 if (!is_error(ret
) && arg3
) {
7944 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
7946 old_act
->_sa_handler
= oact
._sa_handler
;
7947 old_act
->sa_flags
= oact
.sa_flags
;
7948 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
7949 old_act
->sa_mask
.sig
[1] = 0;
7950 old_act
->sa_mask
.sig
[2] = 0;
7951 old_act
->sa_mask
.sig
[3] = 0;
7952 unlock_user_struct(old_act
, arg3
, 1);
7955 struct target_old_sigaction
*old_act
;
7956 struct target_sigaction act
, oact
, *pact
;
7958 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
7960 act
._sa_handler
= old_act
->_sa_handler
;
7961 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
7962 act
.sa_flags
= old_act
->sa_flags
;
7963 act
.sa_restorer
= old_act
->sa_restorer
;
7964 unlock_user_struct(old_act
, arg2
, 0);
7969 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
7970 if (!is_error(ret
) && arg3
) {
7971 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
7973 old_act
->_sa_handler
= oact
._sa_handler
;
7974 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
7975 old_act
->sa_flags
= oact
.sa_flags
;
7976 old_act
->sa_restorer
= oact
.sa_restorer
;
7977 unlock_user_struct(old_act
, arg3
, 1);
7983 case TARGET_NR_rt_sigaction
:
7985 #if defined(TARGET_ALPHA)
7986 struct target_sigaction act
, oact
, *pact
= 0;
7987 struct target_rt_sigaction
*rt_act
;
7989 if (arg4
!= sizeof(target_sigset_t
)) {
7990 ret
= -TARGET_EINVAL
;
7994 if (!lock_user_struct(VERIFY_READ
, rt_act
, arg2
, 1))
7996 act
._sa_handler
= rt_act
->_sa_handler
;
7997 act
.sa_mask
= rt_act
->sa_mask
;
7998 act
.sa_flags
= rt_act
->sa_flags
;
7999 act
.sa_restorer
= arg5
;
8000 unlock_user_struct(rt_act
, arg2
, 0);
8003 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8004 if (!is_error(ret
) && arg3
) {
8005 if (!lock_user_struct(VERIFY_WRITE
, rt_act
, arg3
, 0))
8007 rt_act
->_sa_handler
= oact
._sa_handler
;
8008 rt_act
->sa_mask
= oact
.sa_mask
;
8009 rt_act
->sa_flags
= oact
.sa_flags
;
8010 unlock_user_struct(rt_act
, arg3
, 1);
8013 struct target_sigaction
*act
;
8014 struct target_sigaction
*oact
;
8016 if (arg4
!= sizeof(target_sigset_t
)) {
8017 ret
= -TARGET_EINVAL
;
8021 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1))
8026 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
8027 ret
= -TARGET_EFAULT
;
8028 goto rt_sigaction_fail
;
8032 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
8035 unlock_user_struct(act
, arg2
, 0);
8037 unlock_user_struct(oact
, arg3
, 1);
8041 #ifdef TARGET_NR_sgetmask /* not on alpha */
8042 case TARGET_NR_sgetmask
:
8045 abi_ulong target_set
;
8046 ret
= do_sigprocmask(0, NULL
, &cur_set
);
8048 host_to_target_old_sigset(&target_set
, &cur_set
);
8054 #ifdef TARGET_NR_ssetmask /* not on alpha */
8055 case TARGET_NR_ssetmask
:
8057 sigset_t set
, oset
, cur_set
;
8058 abi_ulong target_set
= arg1
;
8059 /* We only have one word of the new mask so we must read
8060 * the rest of it with do_sigprocmask() and OR in this word.
8061 * We are guaranteed that a do_sigprocmask() that only queries
8062 * the signal mask will not fail.
8064 ret
= do_sigprocmask(0, NULL
, &cur_set
);
8066 target_to_host_old_sigset(&set
, &target_set
);
8067 sigorset(&set
, &set
, &cur_set
);
8068 ret
= do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
8070 host_to_target_old_sigset(&target_set
, &oset
);
8076 #ifdef TARGET_NR_sigprocmask
8077 case TARGET_NR_sigprocmask
:
8079 #if defined(TARGET_ALPHA)
8080 sigset_t set
, oldset
;
8085 case TARGET_SIG_BLOCK
:
8088 case TARGET_SIG_UNBLOCK
:
8091 case TARGET_SIG_SETMASK
:
8095 ret
= -TARGET_EINVAL
;
8099 target_to_host_old_sigset(&set
, &mask
);
8101 ret
= do_sigprocmask(how
, &set
, &oldset
);
8102 if (!is_error(ret
)) {
8103 host_to_target_old_sigset(&mask
, &oldset
);
8105 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0; /* force no error */
8108 sigset_t set
, oldset
, *set_ptr
;
8113 case TARGET_SIG_BLOCK
:
8116 case TARGET_SIG_UNBLOCK
:
8119 case TARGET_SIG_SETMASK
:
8123 ret
= -TARGET_EINVAL
;
8126 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
8128 target_to_host_old_sigset(&set
, p
);
8129 unlock_user(p
, arg2
, 0);
8135 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
8136 if (!is_error(ret
) && arg3
) {
8137 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
8139 host_to_target_old_sigset(p
, &oldset
);
8140 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
8146 case TARGET_NR_rt_sigprocmask
:
8149 sigset_t set
, oldset
, *set_ptr
;
8151 if (arg4
!= sizeof(target_sigset_t
)) {
8152 ret
= -TARGET_EINVAL
;
8158 case TARGET_SIG_BLOCK
:
8161 case TARGET_SIG_UNBLOCK
:
8164 case TARGET_SIG_SETMASK
:
8168 ret
= -TARGET_EINVAL
;
8171 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
8173 target_to_host_sigset(&set
, p
);
8174 unlock_user(p
, arg2
, 0);
8180 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
8181 if (!is_error(ret
) && arg3
) {
8182 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
8184 host_to_target_sigset(p
, &oldset
);
8185 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
8189 #ifdef TARGET_NR_sigpending
8190 case TARGET_NR_sigpending
:
8193 ret
= get_errno(sigpending(&set
));
8194 if (!is_error(ret
)) {
8195 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
8197 host_to_target_old_sigset(p
, &set
);
8198 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
8203 case TARGET_NR_rt_sigpending
:
8207 /* Yes, this check is >, not != like most. We follow the kernel's
8208 * logic and it does it like this because it implements
8209 * NR_sigpending through the same code path, and in that case
8210 * the old_sigset_t is smaller in size.
8212 if (arg2
> sizeof(target_sigset_t
)) {
8213 ret
= -TARGET_EINVAL
;
8217 ret
= get_errno(sigpending(&set
));
8218 if (!is_error(ret
)) {
8219 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
8221 host_to_target_sigset(p
, &set
);
8222 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
8226 #ifdef TARGET_NR_sigsuspend
8227 case TARGET_NR_sigsuspend
:
8229 TaskState
*ts
= cpu
->opaque
;
8230 #if defined(TARGET_ALPHA)
8231 abi_ulong mask
= arg1
;
8232 target_to_host_old_sigset(&ts
->sigsuspend_mask
, &mask
);
8234 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
8236 target_to_host_old_sigset(&ts
->sigsuspend_mask
, p
);
8237 unlock_user(p
, arg1
, 0);
8239 ret
= get_errno(safe_rt_sigsuspend(&ts
->sigsuspend_mask
,
8241 if (ret
!= -TARGET_ERESTARTSYS
) {
8242 ts
->in_sigsuspend
= 1;
8247 case TARGET_NR_rt_sigsuspend
:
8249 TaskState
*ts
= cpu
->opaque
;
8251 if (arg2
!= sizeof(target_sigset_t
)) {
8252 ret
= -TARGET_EINVAL
;
8255 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
8257 target_to_host_sigset(&ts
->sigsuspend_mask
, p
);
8258 unlock_user(p
, arg1
, 0);
8259 ret
= get_errno(safe_rt_sigsuspend(&ts
->sigsuspend_mask
,
8261 if (ret
!= -TARGET_ERESTARTSYS
) {
8262 ts
->in_sigsuspend
= 1;
8266 case TARGET_NR_rt_sigtimedwait
:
8269 struct timespec uts
, *puts
;
8272 if (arg4
!= sizeof(target_sigset_t
)) {
8273 ret
= -TARGET_EINVAL
;
8277 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
8279 target_to_host_sigset(&set
, p
);
8280 unlock_user(p
, arg1
, 0);
8283 target_to_host_timespec(puts
, arg3
);
8287 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
8289 if (!is_error(ret
)) {
8291 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
8296 host_to_target_siginfo(p
, &uinfo
);
8297 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
8299 ret
= host_to_target_signal(ret
);
8303 case TARGET_NR_rt_sigqueueinfo
:
8307 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
8311 target_to_host_siginfo(&uinfo
, p
);
8312 unlock_user(p
, arg1
, 0);
8313 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
8316 #ifdef TARGET_NR_sigreturn
8317 case TARGET_NR_sigreturn
:
8318 if (block_signals()) {
8319 ret
= -TARGET_ERESTARTSYS
;
8321 ret
= do_sigreturn(cpu_env
);
8325 case TARGET_NR_rt_sigreturn
:
8326 if (block_signals()) {
8327 ret
= -TARGET_ERESTARTSYS
;
8329 ret
= do_rt_sigreturn(cpu_env
);
8332 case TARGET_NR_sethostname
:
8333 if (!(p
= lock_user_string(arg1
)))
8335 ret
= get_errno(sethostname(p
, arg2
));
8336 unlock_user(p
, arg1
, 0);
8338 case TARGET_NR_setrlimit
:
8340 int resource
= target_to_host_resource(arg1
);
8341 struct target_rlimit
*target_rlim
;
8343 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
8345 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
8346 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
8347 unlock_user_struct(target_rlim
, arg2
, 0);
8348 ret
= get_errno(setrlimit(resource
, &rlim
));
8351 case TARGET_NR_getrlimit
:
8353 int resource
= target_to_host_resource(arg1
);
8354 struct target_rlimit
*target_rlim
;
8357 ret
= get_errno(getrlimit(resource
, &rlim
));
8358 if (!is_error(ret
)) {
8359 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
8361 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
8362 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
8363 unlock_user_struct(target_rlim
, arg2
, 1);
8367 case TARGET_NR_getrusage
:
8369 struct rusage rusage
;
8370 ret
= get_errno(getrusage(arg1
, &rusage
));
8371 if (!is_error(ret
)) {
8372 ret
= host_to_target_rusage(arg2
, &rusage
);
8376 case TARGET_NR_gettimeofday
:
8379 ret
= get_errno(gettimeofday(&tv
, NULL
));
8380 if (!is_error(ret
)) {
8381 if (copy_to_user_timeval(arg1
, &tv
))
8386 case TARGET_NR_settimeofday
:
8388 struct timeval tv
, *ptv
= NULL
;
8389 struct timezone tz
, *ptz
= NULL
;
8392 if (copy_from_user_timeval(&tv
, arg1
)) {
8399 if (copy_from_user_timezone(&tz
, arg2
)) {
8405 ret
= get_errno(settimeofday(ptv
, ptz
));
8408 #if defined(TARGET_NR_select)
8409 case TARGET_NR_select
:
8410 #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
8411 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
8414 struct target_sel_arg_struct
*sel
;
8415 abi_ulong inp
, outp
, exp
, tvp
;
8418 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1))
8420 nsel
= tswapal(sel
->n
);
8421 inp
= tswapal(sel
->inp
);
8422 outp
= tswapal(sel
->outp
);
8423 exp
= tswapal(sel
->exp
);
8424 tvp
= tswapal(sel
->tvp
);
8425 unlock_user_struct(sel
, arg1
, 0);
8426 ret
= do_select(nsel
, inp
, outp
, exp
, tvp
);
8431 #ifdef TARGET_NR_pselect6
8432 case TARGET_NR_pselect6
:
8434 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
8435 fd_set rfds
, wfds
, efds
;
8436 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
8437 struct timespec ts
, *ts_ptr
;
8440 * The 6th arg is actually two args smashed together,
8441 * so we cannot use the C library.
8449 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
8450 target_sigset_t
*target_sigset
;
8458 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
8462 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
8466 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
8472 * This takes a timespec, and not a timeval, so we cannot
8473 * use the do_select() helper ...
8476 if (target_to_host_timespec(&ts
, ts_addr
)) {
8484 /* Extract the two packed args for the sigset */
8487 sig
.size
= SIGSET_T_SIZE
;
8489 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
8493 arg_sigset
= tswapal(arg7
[0]);
8494 arg_sigsize
= tswapal(arg7
[1]);
8495 unlock_user(arg7
, arg6
, 0);
8499 if (arg_sigsize
!= sizeof(*target_sigset
)) {
8500 /* Like the kernel, we enforce correct size sigsets */
8501 ret
= -TARGET_EINVAL
;
8504 target_sigset
= lock_user(VERIFY_READ
, arg_sigset
,
8505 sizeof(*target_sigset
), 1);
8506 if (!target_sigset
) {
8509 target_to_host_sigset(&set
, target_sigset
);
8510 unlock_user(target_sigset
, arg_sigset
, 0);
8518 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
8521 if (!is_error(ret
)) {
8522 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
8524 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
8526 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
8529 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
))
8535 #ifdef TARGET_NR_symlink
8536 case TARGET_NR_symlink
:
8539 p
= lock_user_string(arg1
);
8540 p2
= lock_user_string(arg2
);
8542 ret
= -TARGET_EFAULT
;
8544 ret
= get_errno(symlink(p
, p2
));
8545 unlock_user(p2
, arg2
, 0);
8546 unlock_user(p
, arg1
, 0);
8550 #if defined(TARGET_NR_symlinkat)
8551 case TARGET_NR_symlinkat
:
8554 p
= lock_user_string(arg1
);
8555 p2
= lock_user_string(arg3
);
8557 ret
= -TARGET_EFAULT
;
8559 ret
= get_errno(symlinkat(p
, arg2
, p2
));
8560 unlock_user(p2
, arg3
, 0);
8561 unlock_user(p
, arg1
, 0);
8565 #ifdef TARGET_NR_oldlstat
8566 case TARGET_NR_oldlstat
:
8569 #ifdef TARGET_NR_readlink
8570 case TARGET_NR_readlink
:
8573 p
= lock_user_string(arg1
);
8574 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
8576 ret
= -TARGET_EFAULT
;
8578 /* Short circuit this for the magic exe check. */
8579 ret
= -TARGET_EINVAL
;
8580 } else if (is_proc_myself((const char *)p
, "exe")) {
8581 char real
[PATH_MAX
], *temp
;
8582 temp
= realpath(exec_path
, real
);
8583 /* Return value is # of bytes that we wrote to the buffer. */
8585 ret
= get_errno(-1);
8587 /* Don't worry about sign mismatch as earlier mapping
8588 * logic would have thrown a bad address error. */
8589 ret
= MIN(strlen(real
), arg3
);
8590 /* We cannot NUL terminate the string. */
8591 memcpy(p2
, real
, ret
);
8594 ret
= get_errno(readlink(path(p
), p2
, arg3
));
8596 unlock_user(p2
, arg2
, ret
);
8597 unlock_user(p
, arg1
, 0);
8601 #if defined(TARGET_NR_readlinkat)
8602 case TARGET_NR_readlinkat
:
8605 p
= lock_user_string(arg2
);
8606 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
8608 ret
= -TARGET_EFAULT
;
8609 } else if (is_proc_myself((const char *)p
, "exe")) {
8610 char real
[PATH_MAX
], *temp
;
8611 temp
= realpath(exec_path
, real
);
8612 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
8613 snprintf((char *)p2
, arg4
, "%s", real
);
8615 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
8617 unlock_user(p2
, arg3
, ret
);
8618 unlock_user(p
, arg2
, 0);
8622 #ifdef TARGET_NR_uselib
8623 case TARGET_NR_uselib
:
8626 #ifdef TARGET_NR_swapon
8627 case TARGET_NR_swapon
:
8628 if (!(p
= lock_user_string(arg1
)))
8630 ret
= get_errno(swapon(p
, arg2
));
8631 unlock_user(p
, arg1
, 0);
8634 case TARGET_NR_reboot
:
8635 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
8636 /* arg4 must be ignored in all other cases */
8637 p
= lock_user_string(arg4
);
8641 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
8642 unlock_user(p
, arg4
, 0);
8644 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
8647 #ifdef TARGET_NR_readdir
8648 case TARGET_NR_readdir
:
8651 #ifdef TARGET_NR_mmap
8652 case TARGET_NR_mmap
:
8653 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
8654 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
8655 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
8656 || defined(TARGET_S390X)
8659 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
8660 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
8668 unlock_user(v
, arg1
, 0);
8669 ret
= get_errno(target_mmap(v1
, v2
, v3
,
8670 target_to_host_bitmask(v4
, mmap_flags_tbl
),
8674 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
8675 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
8681 #ifdef TARGET_NR_mmap2
8682 case TARGET_NR_mmap2
:
8684 #define MMAP_SHIFT 12
8686 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
8687 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
8689 arg6
<< MMAP_SHIFT
));
8692 case TARGET_NR_munmap
:
8693 ret
= get_errno(target_munmap(arg1
, arg2
));
8695 case TARGET_NR_mprotect
:
8697 TaskState
*ts
= cpu
->opaque
;
8698 /* Special hack to detect libc making the stack executable. */
8699 if ((arg3
& PROT_GROWSDOWN
)
8700 && arg1
>= ts
->info
->stack_limit
8701 && arg1
<= ts
->info
->start_stack
) {
8702 arg3
&= ~PROT_GROWSDOWN
;
8703 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
8704 arg1
= ts
->info
->stack_limit
;
8707 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
8709 #ifdef TARGET_NR_mremap
8710 case TARGET_NR_mremap
:
8711 ret
= get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
8714 /* ??? msync/mlock/munlock are broken for softmmu. */
8715 #ifdef TARGET_NR_msync
8716 case TARGET_NR_msync
:
8717 ret
= get_errno(msync(g2h(arg1
), arg2
, arg3
));
8720 #ifdef TARGET_NR_mlock
8721 case TARGET_NR_mlock
:
8722 ret
= get_errno(mlock(g2h(arg1
), arg2
));
8725 #ifdef TARGET_NR_munlock
8726 case TARGET_NR_munlock
:
8727 ret
= get_errno(munlock(g2h(arg1
), arg2
));
8730 #ifdef TARGET_NR_mlockall
8731 case TARGET_NR_mlockall
:
8732 ret
= get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
8735 #ifdef TARGET_NR_munlockall
8736 case TARGET_NR_munlockall
:
8737 ret
= get_errno(munlockall());
8740 case TARGET_NR_truncate
:
8741 if (!(p
= lock_user_string(arg1
)))
8743 ret
= get_errno(truncate(p
, arg2
));
8744 unlock_user(p
, arg1
, 0);
8746 case TARGET_NR_ftruncate
:
8747 ret
= get_errno(ftruncate(arg1
, arg2
));
8749 case TARGET_NR_fchmod
:
8750 ret
= get_errno(fchmod(arg1
, arg2
));
8752 #if defined(TARGET_NR_fchmodat)
8753 case TARGET_NR_fchmodat
:
8754 if (!(p
= lock_user_string(arg2
)))
8756 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
8757 unlock_user(p
, arg2
, 0);
8760 case TARGET_NR_getpriority
:
8761 /* Note that negative values are valid for getpriority, so we must
8762 differentiate based on errno settings. */
8764 ret
= getpriority(arg1
, arg2
);
8765 if (ret
== -1 && errno
!= 0) {
8766 ret
= -host_to_target_errno(errno
);
8770 /* Return value is the unbiased priority. Signal no error. */
8771 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0;
8773 /* Return value is a biased priority to avoid negative numbers. */
8777 case TARGET_NR_setpriority
:
8778 ret
= get_errno(setpriority(arg1
, arg2
, arg3
));
8780 #ifdef TARGET_NR_profil
8781 case TARGET_NR_profil
:
8784 case TARGET_NR_statfs
:
8785 if (!(p
= lock_user_string(arg1
)))
8787 ret
= get_errno(statfs(path(p
), &stfs
));
8788 unlock_user(p
, arg1
, 0);
8790 if (!is_error(ret
)) {
8791 struct target_statfs
*target_stfs
;
8793 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
8795 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
8796 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
8797 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
8798 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
8799 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
8800 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
8801 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
8802 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
8803 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
8804 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
8805 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
8806 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
8807 unlock_user_struct(target_stfs
, arg2
, 1);
8810 case TARGET_NR_fstatfs
:
8811 ret
= get_errno(fstatfs(arg1
, &stfs
));
8812 goto convert_statfs
;
8813 #ifdef TARGET_NR_statfs64
8814 case TARGET_NR_statfs64
:
8815 if (!(p
= lock_user_string(arg1
)))
8817 ret
= get_errno(statfs(path(p
), &stfs
));
8818 unlock_user(p
, arg1
, 0);
8820 if (!is_error(ret
)) {
8821 struct target_statfs64
*target_stfs
;
8823 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
8825 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
8826 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
8827 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
8828 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
8829 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
8830 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
8831 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
8832 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
8833 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
8834 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
8835 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
8836 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
8837 unlock_user_struct(target_stfs
, arg3
, 1);
8840 case TARGET_NR_fstatfs64
:
8841 ret
= get_errno(fstatfs(arg1
, &stfs
));
8842 goto convert_statfs64
;
8844 #ifdef TARGET_NR_ioperm
8845 case TARGET_NR_ioperm
:
8848 #ifdef TARGET_NR_socketcall
8849 case TARGET_NR_socketcall
:
8850 ret
= do_socketcall(arg1
, arg2
);
8853 #ifdef TARGET_NR_accept
8854 case TARGET_NR_accept
:
8855 ret
= do_accept4(arg1
, arg2
, arg3
, 0);
8858 #ifdef TARGET_NR_accept4
8859 case TARGET_NR_accept4
:
8860 ret
= do_accept4(arg1
, arg2
, arg3
, arg4
);
8863 #ifdef TARGET_NR_bind
8864 case TARGET_NR_bind
:
8865 ret
= do_bind(arg1
, arg2
, arg3
);
8868 #ifdef TARGET_NR_connect
8869 case TARGET_NR_connect
:
8870 ret
= do_connect(arg1
, arg2
, arg3
);
8873 #ifdef TARGET_NR_getpeername
8874 case TARGET_NR_getpeername
:
8875 ret
= do_getpeername(arg1
, arg2
, arg3
);
8878 #ifdef TARGET_NR_getsockname
8879 case TARGET_NR_getsockname
:
8880 ret
= do_getsockname(arg1
, arg2
, arg3
);
8883 #ifdef TARGET_NR_getsockopt
8884 case TARGET_NR_getsockopt
:
8885 ret
= do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
8888 #ifdef TARGET_NR_listen
8889 case TARGET_NR_listen
:
8890 ret
= get_errno(listen(arg1
, arg2
));
8893 #ifdef TARGET_NR_recv
8894 case TARGET_NR_recv
:
8895 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
8898 #ifdef TARGET_NR_recvfrom
8899 case TARGET_NR_recvfrom
:
8900 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
8903 #ifdef TARGET_NR_recvmsg
8904 case TARGET_NR_recvmsg
:
8905 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
8908 #ifdef TARGET_NR_send
8909 case TARGET_NR_send
:
8910 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
8913 #ifdef TARGET_NR_sendmsg
8914 case TARGET_NR_sendmsg
:
8915 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
8918 #ifdef TARGET_NR_sendmmsg
8919 case TARGET_NR_sendmmsg
:
8920 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
8922 case TARGET_NR_recvmmsg
:
8923 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
8926 #ifdef TARGET_NR_sendto
8927 case TARGET_NR_sendto
:
8928 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
8931 #ifdef TARGET_NR_shutdown
8932 case TARGET_NR_shutdown
:
8933 ret
= get_errno(shutdown(arg1
, arg2
));
8936 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
8937 case TARGET_NR_getrandom
:
8938 p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
8942 ret
= get_errno(getrandom(p
, arg2
, arg3
));
8943 unlock_user(p
, arg1
, ret
);
8946 #ifdef TARGET_NR_socket
8947 case TARGET_NR_socket
:
8948 ret
= do_socket(arg1
, arg2
, arg3
);
8949 fd_trans_unregister(ret
);
8952 #ifdef TARGET_NR_socketpair
8953 case TARGET_NR_socketpair
:
8954 ret
= do_socketpair(arg1
, arg2
, arg3
, arg4
);
8957 #ifdef TARGET_NR_setsockopt
8958 case TARGET_NR_setsockopt
:
8959 ret
= do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
8963 case TARGET_NR_syslog
:
8964 if (!(p
= lock_user_string(arg2
)))
8966 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
8967 unlock_user(p
, arg2
, 0);
8970 case TARGET_NR_setitimer
:
8972 struct itimerval value
, ovalue
, *pvalue
;
8976 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
8977 || copy_from_user_timeval(&pvalue
->it_value
,
8978 arg2
+ sizeof(struct target_timeval
)))
8983 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
8984 if (!is_error(ret
) && arg3
) {
8985 if (copy_to_user_timeval(arg3
,
8986 &ovalue
.it_interval
)
8987 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
8993 case TARGET_NR_getitimer
:
8995 struct itimerval value
;
8997 ret
= get_errno(getitimer(arg1
, &value
));
8998 if (!is_error(ret
) && arg2
) {
8999 if (copy_to_user_timeval(arg2
,
9001 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
9007 #ifdef TARGET_NR_stat
9008 case TARGET_NR_stat
:
9009 if (!(p
= lock_user_string(arg1
)))
9011 ret
= get_errno(stat(path(p
), &st
));
9012 unlock_user(p
, arg1
, 0);
9015 #ifdef TARGET_NR_lstat
9016 case TARGET_NR_lstat
:
9017 if (!(p
= lock_user_string(arg1
)))
9019 ret
= get_errno(lstat(path(p
), &st
));
9020 unlock_user(p
, arg1
, 0);
9023 case TARGET_NR_fstat
:
9025 ret
= get_errno(fstat(arg1
, &st
));
9026 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
9029 if (!is_error(ret
)) {
9030 struct target_stat
*target_st
;
9032 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
9034 memset(target_st
, 0, sizeof(*target_st
));
9035 __put_user(st
.st_dev
, &target_st
->st_dev
);
9036 __put_user(st
.st_ino
, &target_st
->st_ino
);
9037 __put_user(st
.st_mode
, &target_st
->st_mode
);
9038 __put_user(st
.st_uid
, &target_st
->st_uid
);
9039 __put_user(st
.st_gid
, &target_st
->st_gid
);
9040 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
9041 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
9042 __put_user(st
.st_size
, &target_st
->st_size
);
9043 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
9044 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
9045 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
9046 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
9047 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
9048 unlock_user_struct(target_st
, arg2
, 1);
9052 #ifdef TARGET_NR_olduname
9053 case TARGET_NR_olduname
:
9056 #ifdef TARGET_NR_iopl
9057 case TARGET_NR_iopl
:
9060 case TARGET_NR_vhangup
:
9061 ret
= get_errno(vhangup());
9063 #ifdef TARGET_NR_idle
9064 case TARGET_NR_idle
:
9067 #ifdef TARGET_NR_syscall
9068 case TARGET_NR_syscall
:
9069 ret
= do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
9070 arg6
, arg7
, arg8
, 0);
9073 case TARGET_NR_wait4
:
9076 abi_long status_ptr
= arg2
;
9077 struct rusage rusage
, *rusage_ptr
;
9078 abi_ulong target_rusage
= arg4
;
9079 abi_long rusage_err
;
9081 rusage_ptr
= &rusage
;
9084 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, rusage_ptr
));
9085 if (!is_error(ret
)) {
9086 if (status_ptr
&& ret
) {
9087 status
= host_to_target_waitstatus(status
);
9088 if (put_user_s32(status
, status_ptr
))
9091 if (target_rusage
) {
9092 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
9100 #ifdef TARGET_NR_swapoff
9101 case TARGET_NR_swapoff
:
9102 if (!(p
= lock_user_string(arg1
)))
9104 ret
= get_errno(swapoff(p
));
9105 unlock_user(p
, arg1
, 0);
9108 case TARGET_NR_sysinfo
:
9110 struct target_sysinfo
*target_value
;
9111 struct sysinfo value
;
9112 ret
= get_errno(sysinfo(&value
));
9113 if (!is_error(ret
) && arg1
)
9115 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
9117 __put_user(value
.uptime
, &target_value
->uptime
);
9118 __put_user(value
.loads
[0], &target_value
->loads
[0]);
9119 __put_user(value
.loads
[1], &target_value
->loads
[1]);
9120 __put_user(value
.loads
[2], &target_value
->loads
[2]);
9121 __put_user(value
.totalram
, &target_value
->totalram
);
9122 __put_user(value
.freeram
, &target_value
->freeram
);
9123 __put_user(value
.sharedram
, &target_value
->sharedram
);
9124 __put_user(value
.bufferram
, &target_value
->bufferram
);
9125 __put_user(value
.totalswap
, &target_value
->totalswap
);
9126 __put_user(value
.freeswap
, &target_value
->freeswap
);
9127 __put_user(value
.procs
, &target_value
->procs
);
9128 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
9129 __put_user(value
.freehigh
, &target_value
->freehigh
);
9130 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
9131 unlock_user_struct(target_value
, arg1
, 1);
9135 #ifdef TARGET_NR_ipc
9137 ret
= do_ipc(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9140 #ifdef TARGET_NR_semget
9141 case TARGET_NR_semget
:
9142 ret
= get_errno(semget(arg1
, arg2
, arg3
));
9145 #ifdef TARGET_NR_semop
9146 case TARGET_NR_semop
:
9147 ret
= do_semop(arg1
, arg2
, arg3
);
9150 #ifdef TARGET_NR_semctl
9151 case TARGET_NR_semctl
:
9152 ret
= do_semctl(arg1
, arg2
, arg3
, arg4
);
9155 #ifdef TARGET_NR_msgctl
9156 case TARGET_NR_msgctl
:
9157 ret
= do_msgctl(arg1
, arg2
, arg3
);
9160 #ifdef TARGET_NR_msgget
9161 case TARGET_NR_msgget
:
9162 ret
= get_errno(msgget(arg1
, arg2
));
9165 #ifdef TARGET_NR_msgrcv
9166 case TARGET_NR_msgrcv
:
9167 ret
= do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
9170 #ifdef TARGET_NR_msgsnd
9171 case TARGET_NR_msgsnd
:
9172 ret
= do_msgsnd(arg1
, arg2
, arg3
, arg4
);
9175 #ifdef TARGET_NR_shmget
9176 case TARGET_NR_shmget
:
9177 ret
= get_errno(shmget(arg1
, arg2
, arg3
));
9180 #ifdef TARGET_NR_shmctl
9181 case TARGET_NR_shmctl
:
9182 ret
= do_shmctl(arg1
, arg2
, arg3
);
9185 #ifdef TARGET_NR_shmat
9186 case TARGET_NR_shmat
:
9187 ret
= do_shmat(arg1
, arg2
, arg3
);
9190 #ifdef TARGET_NR_shmdt
9191 case TARGET_NR_shmdt
:
9192 ret
= do_shmdt(arg1
);
9195 case TARGET_NR_fsync
:
9196 ret
= get_errno(fsync(arg1
));
9198 case TARGET_NR_clone
:
9199 /* Linux manages to have three different orderings for its
9200 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
9201 * match the kernel's CONFIG_CLONE_* settings.
9202 * Microblaze is further special in that it uses a sixth
9203 * implicit argument to clone for the TLS pointer.
9205 #if defined(TARGET_MICROBLAZE)
9206 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
9207 #elif defined(TARGET_CLONE_BACKWARDS)
9208 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
9209 #elif defined(TARGET_CLONE_BACKWARDS2)
9210 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
9212 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
9215 #ifdef __NR_exit_group
9216 /* new thread calls */
9217 case TARGET_NR_exit_group
:
9221 gdb_exit(cpu_env
, arg1
);
9222 ret
= get_errno(exit_group(arg1
));
9225 case TARGET_NR_setdomainname
:
9226 if (!(p
= lock_user_string(arg1
)))
9228 ret
= get_errno(setdomainname(p
, arg2
));
9229 unlock_user(p
, arg1
, 0);
9231 case TARGET_NR_uname
:
9232 /* no need to transcode because we use the linux syscall */
9234 struct new_utsname
* buf
;
9236 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
9238 ret
= get_errno(sys_uname(buf
));
9239 if (!is_error(ret
)) {
9240 /* Overrite the native machine name with whatever is being
9242 strcpy (buf
->machine
, cpu_to_uname_machine(cpu_env
));
9243 /* Allow the user to override the reported release. */
9244 if (qemu_uname_release
&& *qemu_uname_release
)
9245 strcpy (buf
->release
, qemu_uname_release
);
9247 unlock_user_struct(buf
, arg1
, 1);
9251 case TARGET_NR_modify_ldt
:
9252 ret
= do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
9254 #if !defined(TARGET_X86_64)
9255 case TARGET_NR_vm86old
:
9257 case TARGET_NR_vm86
:
9258 ret
= do_vm86(cpu_env
, arg1
, arg2
);
9262 case TARGET_NR_adjtimex
:
9264 #ifdef TARGET_NR_create_module
9265 case TARGET_NR_create_module
:
9267 case TARGET_NR_init_module
:
9268 case TARGET_NR_delete_module
:
9269 #ifdef TARGET_NR_get_kernel_syms
9270 case TARGET_NR_get_kernel_syms
:
9273 case TARGET_NR_quotactl
:
9275 case TARGET_NR_getpgid
:
9276 ret
= get_errno(getpgid(arg1
));
9278 case TARGET_NR_fchdir
:
9279 ret
= get_errno(fchdir(arg1
));
9281 #ifdef TARGET_NR_bdflush /* not on x86_64 */
9282 case TARGET_NR_bdflush
:
9285 #ifdef TARGET_NR_sysfs
9286 case TARGET_NR_sysfs
:
9289 case TARGET_NR_personality
:
9290 ret
= get_errno(personality(arg1
));
9292 #ifdef TARGET_NR_afs_syscall
9293 case TARGET_NR_afs_syscall
:
9296 #ifdef TARGET_NR__llseek /* Not on alpha */
9297 case TARGET_NR__llseek
:
9300 #if !defined(__NR_llseek)
9301 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | arg3
, arg5
);
9303 ret
= get_errno(res
);
9308 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
9310 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
9316 #ifdef TARGET_NR_getdents
9317 case TARGET_NR_getdents
:
9318 #ifdef __NR_getdents
9319 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
9321 struct target_dirent
*target_dirp
;
9322 struct linux_dirent
*dirp
;
9323 abi_long count
= arg3
;
9325 dirp
= g_try_malloc(count
);
9327 ret
= -TARGET_ENOMEM
;
9331 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
9332 if (!is_error(ret
)) {
9333 struct linux_dirent
*de
;
9334 struct target_dirent
*tde
;
9336 int reclen
, treclen
;
9337 int count1
, tnamelen
;
9341 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
9345 reclen
= de
->d_reclen
;
9346 tnamelen
= reclen
- offsetof(struct linux_dirent
, d_name
);
9347 assert(tnamelen
>= 0);
9348 treclen
= tnamelen
+ offsetof(struct target_dirent
, d_name
);
9349 assert(count1
+ treclen
<= count
);
9350 tde
->d_reclen
= tswap16(treclen
);
9351 tde
->d_ino
= tswapal(de
->d_ino
);
9352 tde
->d_off
= tswapal(de
->d_off
);
9353 memcpy(tde
->d_name
, de
->d_name
, tnamelen
);
9354 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
9356 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
9360 unlock_user(target_dirp
, arg2
, ret
);
9366 struct linux_dirent
*dirp
;
9367 abi_long count
= arg3
;
9369 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
9371 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
9372 if (!is_error(ret
)) {
9373 struct linux_dirent
*de
;
9378 reclen
= de
->d_reclen
;
9381 de
->d_reclen
= tswap16(reclen
);
9382 tswapls(&de
->d_ino
);
9383 tswapls(&de
->d_off
);
9384 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
9388 unlock_user(dirp
, arg2
, ret
);
9392 /* Implement getdents in terms of getdents64 */
9394 struct linux_dirent64
*dirp
;
9395 abi_long count
= arg3
;
9397 dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
9401 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
9402 if (!is_error(ret
)) {
9403 /* Convert the dirent64 structs to target dirent. We do this
9404 * in-place, since we can guarantee that a target_dirent is no
9405 * larger than a dirent64; however this means we have to be
9406 * careful to read everything before writing in the new format.
9408 struct linux_dirent64
*de
;
9409 struct target_dirent
*tde
;
9414 tde
= (struct target_dirent
*)dirp
;
9416 int namelen
, treclen
;
9417 int reclen
= de
->d_reclen
;
9418 uint64_t ino
= de
->d_ino
;
9419 int64_t off
= de
->d_off
;
9420 uint8_t type
= de
->d_type
;
9422 namelen
= strlen(de
->d_name
);
9423 treclen
= offsetof(struct target_dirent
, d_name
)
9425 treclen
= QEMU_ALIGN_UP(treclen
, sizeof(abi_long
));
9427 memmove(tde
->d_name
, de
->d_name
, namelen
+ 1);
9428 tde
->d_ino
= tswapal(ino
);
9429 tde
->d_off
= tswapal(off
);
9430 tde
->d_reclen
= tswap16(treclen
);
9431 /* The target_dirent type is in what was formerly a padding
9432 * byte at the end of the structure:
9434 *(((char *)tde
) + treclen
- 1) = type
;
9436 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
9437 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
9443 unlock_user(dirp
, arg2
, ret
);
9447 #endif /* TARGET_NR_getdents */
9448 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
9449 case TARGET_NR_getdents64
:
9451 struct linux_dirent64
*dirp
;
9452 abi_long count
= arg3
;
9453 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
9455 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
9456 if (!is_error(ret
)) {
9457 struct linux_dirent64
*de
;
9462 reclen
= de
->d_reclen
;
9465 de
->d_reclen
= tswap16(reclen
);
9466 tswap64s((uint64_t *)&de
->d_ino
);
9467 tswap64s((uint64_t *)&de
->d_off
);
9468 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
9472 unlock_user(dirp
, arg2
, ret
);
9475 #endif /* TARGET_NR_getdents64 */
9476 #if defined(TARGET_NR__newselect)
9477 case TARGET_NR__newselect
:
9478 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
9481 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
9482 # ifdef TARGET_NR_poll
9483 case TARGET_NR_poll
:
9485 # ifdef TARGET_NR_ppoll
9486 case TARGET_NR_ppoll
:
9489 struct target_pollfd
*target_pfd
;
9490 unsigned int nfds
= arg2
;
9497 target_pfd
= lock_user(VERIFY_WRITE
, arg1
,
9498 sizeof(struct target_pollfd
) * nfds
, 1);
9503 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
9504 for (i
= 0; i
< nfds
; i
++) {
9505 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
9506 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
9511 # ifdef TARGET_NR_ppoll
9512 case TARGET_NR_ppoll
:
9514 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
9515 target_sigset_t
*target_set
;
9516 sigset_t _set
, *set
= &_set
;
9519 if (target_to_host_timespec(timeout_ts
, arg3
)) {
9520 unlock_user(target_pfd
, arg1
, 0);
9528 if (arg5
!= sizeof(target_sigset_t
)) {
9529 unlock_user(target_pfd
, arg1
, 0);
9530 ret
= -TARGET_EINVAL
;
9534 target_set
= lock_user(VERIFY_READ
, arg4
, sizeof(target_sigset_t
), 1);
9536 unlock_user(target_pfd
, arg1
, 0);
9539 target_to_host_sigset(set
, target_set
);
9544 ret
= get_errno(safe_ppoll(pfd
, nfds
, timeout_ts
,
9545 set
, SIGSET_T_SIZE
));
9547 if (!is_error(ret
) && arg3
) {
9548 host_to_target_timespec(arg3
, timeout_ts
);
9551 unlock_user(target_set
, arg4
, 0);
9556 # ifdef TARGET_NR_poll
9557 case TARGET_NR_poll
:
9559 struct timespec ts
, *pts
;
9562 /* Convert ms to secs, ns */
9563 ts
.tv_sec
= arg3
/ 1000;
9564 ts
.tv_nsec
= (arg3
% 1000) * 1000000LL;
9567 /* -ve poll() timeout means "infinite" */
9570 ret
= get_errno(safe_ppoll(pfd
, nfds
, pts
, NULL
, 0));
9575 g_assert_not_reached();
9578 if (!is_error(ret
)) {
9579 for(i
= 0; i
< nfds
; i
++) {
9580 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
9583 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
9587 case TARGET_NR_flock
:
9588 /* NOTE: the flock constant seems to be the same for every
9590 ret
= get_errno(safe_flock(arg1
, arg2
));
9592 case TARGET_NR_readv
:
9594 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
9596 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
9597 unlock_iovec(vec
, arg2
, arg3
, 1);
9599 ret
= -host_to_target_errno(errno
);
9603 case TARGET_NR_writev
:
9605 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
9607 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
9608 unlock_iovec(vec
, arg2
, arg3
, 0);
9610 ret
= -host_to_target_errno(errno
);
9614 case TARGET_NR_getsid
:
9615 ret
= get_errno(getsid(arg1
));
9617 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
9618 case TARGET_NR_fdatasync
:
9619 ret
= get_errno(fdatasync(arg1
));
9622 #ifdef TARGET_NR__sysctl
9623 case TARGET_NR__sysctl
:
9624 /* We don't implement this, but ENOTDIR is always a safe
9626 ret
= -TARGET_ENOTDIR
;
9629 case TARGET_NR_sched_getaffinity
:
9631 unsigned int mask_size
;
9632 unsigned long *mask
;
9635 * sched_getaffinity needs multiples of ulong, so need to take
9636 * care of mismatches between target ulong and host ulong sizes.
9638 if (arg2
& (sizeof(abi_ulong
) - 1)) {
9639 ret
= -TARGET_EINVAL
;
9642 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
9644 mask
= alloca(mask_size
);
9645 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
9647 if (!is_error(ret
)) {
9649 /* More data returned than the caller's buffer will fit.
9650 * This only happens if sizeof(abi_long) < sizeof(long)
9651 * and the caller passed us a buffer holding an odd number
9652 * of abi_longs. If the host kernel is actually using the
9653 * extra 4 bytes then fail EINVAL; otherwise we can just
9654 * ignore them and only copy the interesting part.
9656 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
9657 if (numcpus
> arg2
* 8) {
9658 ret
= -TARGET_EINVAL
;
9664 if (copy_to_user(arg3
, mask
, ret
)) {
9670 case TARGET_NR_sched_setaffinity
:
9672 unsigned int mask_size
;
9673 unsigned long *mask
;
9676 * sched_setaffinity needs multiples of ulong, so need to take
9677 * care of mismatches between target ulong and host ulong sizes.
9679 if (arg2
& (sizeof(abi_ulong
) - 1)) {
9680 ret
= -TARGET_EINVAL
;
9683 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
9685 mask
= alloca(mask_size
);
9686 if (!lock_user_struct(VERIFY_READ
, p
, arg3
, 1)) {
9689 memcpy(mask
, p
, arg2
);
9690 unlock_user_struct(p
, arg2
, 0);
9692 ret
= get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
9695 case TARGET_NR_sched_setparam
:
9697 struct sched_param
*target_schp
;
9698 struct sched_param schp
;
9701 return -TARGET_EINVAL
;
9703 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
9705 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
9706 unlock_user_struct(target_schp
, arg2
, 0);
9707 ret
= get_errno(sched_setparam(arg1
, &schp
));
9710 case TARGET_NR_sched_getparam
:
9712 struct sched_param
*target_schp
;
9713 struct sched_param schp
;
9716 return -TARGET_EINVAL
;
9718 ret
= get_errno(sched_getparam(arg1
, &schp
));
9719 if (!is_error(ret
)) {
9720 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
9722 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
9723 unlock_user_struct(target_schp
, arg2
, 1);
9727 case TARGET_NR_sched_setscheduler
:
9729 struct sched_param
*target_schp
;
9730 struct sched_param schp
;
9732 return -TARGET_EINVAL
;
9734 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
9736 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
9737 unlock_user_struct(target_schp
, arg3
, 0);
9738 ret
= get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
9741 case TARGET_NR_sched_getscheduler
:
9742 ret
= get_errno(sched_getscheduler(arg1
));
9744 case TARGET_NR_sched_yield
:
9745 ret
= get_errno(sched_yield());
9747 case TARGET_NR_sched_get_priority_max
:
9748 ret
= get_errno(sched_get_priority_max(arg1
));
9750 case TARGET_NR_sched_get_priority_min
:
9751 ret
= get_errno(sched_get_priority_min(arg1
));
9753 case TARGET_NR_sched_rr_get_interval
:
9756 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
9757 if (!is_error(ret
)) {
9758 ret
= host_to_target_timespec(arg2
, &ts
);
9762 case TARGET_NR_nanosleep
:
9764 struct timespec req
, rem
;
9765 target_to_host_timespec(&req
, arg1
);
9766 ret
= get_errno(safe_nanosleep(&req
, &rem
));
9767 if (is_error(ret
) && arg2
) {
9768 host_to_target_timespec(arg2
, &rem
);
9772 #ifdef TARGET_NR_query_module
9773 case TARGET_NR_query_module
:
9776 #ifdef TARGET_NR_nfsservctl
9777 case TARGET_NR_nfsservctl
:
9780 case TARGET_NR_prctl
:
9782 case PR_GET_PDEATHSIG
:
9785 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
9786 if (!is_error(ret
) && arg2
9787 && put_user_ual(deathsig
, arg2
)) {
9795 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
9799 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
9801 unlock_user(name
, arg2
, 16);
9806 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
9810 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
9812 unlock_user(name
, arg2
, 0);
9817 /* Most prctl options have no pointer arguments */
9818 ret
= get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
9822 #ifdef TARGET_NR_arch_prctl
9823 case TARGET_NR_arch_prctl
:
9824 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
9825 ret
= do_arch_prctl(cpu_env
, arg1
, arg2
);
9831 #ifdef TARGET_NR_pread64
9832 case TARGET_NR_pread64
:
9833 if (regpairs_aligned(cpu_env
)) {
9837 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
9839 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
9840 unlock_user(p
, arg2
, ret
);
9842 case TARGET_NR_pwrite64
:
9843 if (regpairs_aligned(cpu_env
)) {
9847 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
9849 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
9850 unlock_user(p
, arg2
, 0);
9853 case TARGET_NR_getcwd
:
9854 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
9856 ret
= get_errno(sys_getcwd1(p
, arg2
));
9857 unlock_user(p
, arg1
, ret
);
9859 case TARGET_NR_capget
:
9860 case TARGET_NR_capset
:
9862 struct target_user_cap_header
*target_header
;
9863 struct target_user_cap_data
*target_data
= NULL
;
9864 struct __user_cap_header_struct header
;
9865 struct __user_cap_data_struct data
[2];
9866 struct __user_cap_data_struct
*dataptr
= NULL
;
9867 int i
, target_datalen
;
9870 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
9873 header
.version
= tswap32(target_header
->version
);
9874 header
.pid
= tswap32(target_header
->pid
);
9876 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
9877 /* Version 2 and up takes pointer to two user_data structs */
9881 target_datalen
= sizeof(*target_data
) * data_items
;
9884 if (num
== TARGET_NR_capget
) {
9885 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
9887 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
9890 unlock_user_struct(target_header
, arg1
, 0);
9894 if (num
== TARGET_NR_capset
) {
9895 for (i
= 0; i
< data_items
; i
++) {
9896 data
[i
].effective
= tswap32(target_data
[i
].effective
);
9897 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
9898 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
9905 if (num
== TARGET_NR_capget
) {
9906 ret
= get_errno(capget(&header
, dataptr
));
9908 ret
= get_errno(capset(&header
, dataptr
));
9911 /* The kernel always updates version for both capget and capset */
9912 target_header
->version
= tswap32(header
.version
);
9913 unlock_user_struct(target_header
, arg1
, 1);
9916 if (num
== TARGET_NR_capget
) {
9917 for (i
= 0; i
< data_items
; i
++) {
9918 target_data
[i
].effective
= tswap32(data
[i
].effective
);
9919 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
9920 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
9922 unlock_user(target_data
, arg2
, target_datalen
);
9924 unlock_user(target_data
, arg2
, 0);
9929 case TARGET_NR_sigaltstack
:
9930 ret
= do_sigaltstack(arg1
, arg2
, get_sp_from_cpustate((CPUArchState
*)cpu_env
));
9933 #ifdef CONFIG_SENDFILE
9934 case TARGET_NR_sendfile
:
9939 ret
= get_user_sal(off
, arg3
);
9940 if (is_error(ret
)) {
9945 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
9946 if (!is_error(ret
) && arg3
) {
9947 abi_long ret2
= put_user_sal(off
, arg3
);
9948 if (is_error(ret2
)) {
9954 #ifdef TARGET_NR_sendfile64
9955 case TARGET_NR_sendfile64
:
9960 ret
= get_user_s64(off
, arg3
);
9961 if (is_error(ret
)) {
9966 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
9967 if (!is_error(ret
) && arg3
) {
9968 abi_long ret2
= put_user_s64(off
, arg3
);
9969 if (is_error(ret2
)) {
9977 case TARGET_NR_sendfile
:
9978 #ifdef TARGET_NR_sendfile64
9979 case TARGET_NR_sendfile64
:
9984 #ifdef TARGET_NR_getpmsg
9985 case TARGET_NR_getpmsg
:
9988 #ifdef TARGET_NR_putpmsg
9989 case TARGET_NR_putpmsg
:
9992 #ifdef TARGET_NR_vfork
9993 case TARGET_NR_vfork
:
9994 ret
= get_errno(do_fork(cpu_env
, CLONE_VFORK
| CLONE_VM
| SIGCHLD
,
9998 #ifdef TARGET_NR_ugetrlimit
9999 case TARGET_NR_ugetrlimit
:
10001 struct rlimit rlim
;
10002 int resource
= target_to_host_resource(arg1
);
10003 ret
= get_errno(getrlimit(resource
, &rlim
));
10004 if (!is_error(ret
)) {
10005 struct target_rlimit
*target_rlim
;
10006 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
10008 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
10009 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
10010 unlock_user_struct(target_rlim
, arg2
, 1);
10015 #ifdef TARGET_NR_truncate64
10016 case TARGET_NR_truncate64
:
10017 if (!(p
= lock_user_string(arg1
)))
10019 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
10020 unlock_user(p
, arg1
, 0);
10023 #ifdef TARGET_NR_ftruncate64
10024 case TARGET_NR_ftruncate64
:
10025 ret
= target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
10028 #ifdef TARGET_NR_stat64
10029 case TARGET_NR_stat64
:
10030 if (!(p
= lock_user_string(arg1
)))
10032 ret
= get_errno(stat(path(p
), &st
));
10033 unlock_user(p
, arg1
, 0);
10034 if (!is_error(ret
))
10035 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10038 #ifdef TARGET_NR_lstat64
10039 case TARGET_NR_lstat64
:
10040 if (!(p
= lock_user_string(arg1
)))
10042 ret
= get_errno(lstat(path(p
), &st
));
10043 unlock_user(p
, arg1
, 0);
10044 if (!is_error(ret
))
10045 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10048 #ifdef TARGET_NR_fstat64
10049 case TARGET_NR_fstat64
:
10050 ret
= get_errno(fstat(arg1
, &st
));
10051 if (!is_error(ret
))
10052 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10055 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
10056 #ifdef TARGET_NR_fstatat64
10057 case TARGET_NR_fstatat64
:
10059 #ifdef TARGET_NR_newfstatat
10060 case TARGET_NR_newfstatat
:
10062 if (!(p
= lock_user_string(arg2
)))
10064 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
10065 if (!is_error(ret
))
10066 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
10069 #ifdef TARGET_NR_lchown
10070 case TARGET_NR_lchown
:
10071 if (!(p
= lock_user_string(arg1
)))
10073 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
10074 unlock_user(p
, arg1
, 0);
10077 #ifdef TARGET_NR_getuid
10078 case TARGET_NR_getuid
:
10079 ret
= get_errno(high2lowuid(getuid()));
10082 #ifdef TARGET_NR_getgid
10083 case TARGET_NR_getgid
:
10084 ret
= get_errno(high2lowgid(getgid()));
10087 #ifdef TARGET_NR_geteuid
10088 case TARGET_NR_geteuid
:
10089 ret
= get_errno(high2lowuid(geteuid()));
10092 #ifdef TARGET_NR_getegid
10093 case TARGET_NR_getegid
:
10094 ret
= get_errno(high2lowgid(getegid()));
10097 case TARGET_NR_setreuid
:
10098 ret
= get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
10100 case TARGET_NR_setregid
:
10101 ret
= get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
10103 case TARGET_NR_getgroups
:
10105 int gidsetsize
= arg1
;
10106 target_id
*target_grouplist
;
10110 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
10111 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
10112 if (gidsetsize
== 0)
10114 if (!is_error(ret
)) {
10115 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
10116 if (!target_grouplist
)
10118 for(i
= 0;i
< ret
; i
++)
10119 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
10120 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
10124 case TARGET_NR_setgroups
:
10126 int gidsetsize
= arg1
;
10127 target_id
*target_grouplist
;
10128 gid_t
*grouplist
= NULL
;
10131 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
10132 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
10133 if (!target_grouplist
) {
10134 ret
= -TARGET_EFAULT
;
10137 for (i
= 0; i
< gidsetsize
; i
++) {
10138 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
10140 unlock_user(target_grouplist
, arg2
, 0);
10142 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
10145 case TARGET_NR_fchown
:
10146 ret
= get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
10148 #if defined(TARGET_NR_fchownat)
10149 case TARGET_NR_fchownat
:
10150 if (!(p
= lock_user_string(arg2
)))
10152 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
10153 low2highgid(arg4
), arg5
));
10154 unlock_user(p
, arg2
, 0);
10157 #ifdef TARGET_NR_setresuid
10158 case TARGET_NR_setresuid
:
10159 ret
= get_errno(sys_setresuid(low2highuid(arg1
),
10161 low2highuid(arg3
)));
10164 #ifdef TARGET_NR_getresuid
10165 case TARGET_NR_getresuid
:
10167 uid_t ruid
, euid
, suid
;
10168 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
10169 if (!is_error(ret
)) {
10170 if (put_user_id(high2lowuid(ruid
), arg1
)
10171 || put_user_id(high2lowuid(euid
), arg2
)
10172 || put_user_id(high2lowuid(suid
), arg3
))
10178 #ifdef TARGET_NR_getresgid
10179 case TARGET_NR_setresgid
:
10180 ret
= get_errno(sys_setresgid(low2highgid(arg1
),
10182 low2highgid(arg3
)));
10185 #ifdef TARGET_NR_getresgid
10186 case TARGET_NR_getresgid
:
10188 gid_t rgid
, egid
, sgid
;
10189 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
10190 if (!is_error(ret
)) {
10191 if (put_user_id(high2lowgid(rgid
), arg1
)
10192 || put_user_id(high2lowgid(egid
), arg2
)
10193 || put_user_id(high2lowgid(sgid
), arg3
))
10199 #ifdef TARGET_NR_chown
10200 case TARGET_NR_chown
:
10201 if (!(p
= lock_user_string(arg1
)))
10203 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
10204 unlock_user(p
, arg1
, 0);
10207 case TARGET_NR_setuid
:
10208 ret
= get_errno(sys_setuid(low2highuid(arg1
)));
10210 case TARGET_NR_setgid
:
10211 ret
= get_errno(sys_setgid(low2highgid(arg1
)));
10213 case TARGET_NR_setfsuid
:
10214 ret
= get_errno(setfsuid(arg1
));
10216 case TARGET_NR_setfsgid
:
10217 ret
= get_errno(setfsgid(arg1
));
10220 #ifdef TARGET_NR_lchown32
10221 case TARGET_NR_lchown32
:
10222 if (!(p
= lock_user_string(arg1
)))
10224 ret
= get_errno(lchown(p
, arg2
, arg3
));
10225 unlock_user(p
, arg1
, 0);
10228 #ifdef TARGET_NR_getuid32
10229 case TARGET_NR_getuid32
:
10230 ret
= get_errno(getuid());
10234 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
10235 /* Alpha specific */
10236 case TARGET_NR_getxuid
:
10240 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
10242 ret
= get_errno(getuid());
10245 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
10246 /* Alpha specific */
10247 case TARGET_NR_getxgid
:
10251 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
10253 ret
= get_errno(getgid());
10256 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
10257 /* Alpha specific */
10258 case TARGET_NR_osf_getsysinfo
:
10259 ret
= -TARGET_EOPNOTSUPP
;
10261 case TARGET_GSI_IEEE_FP_CONTROL
:
10263 uint64_t swcr
, fpcr
= cpu_alpha_load_fpcr (cpu_env
);
10265 /* Copied from linux ieee_fpcr_to_swcr. */
10266 swcr
= (fpcr
>> 35) & SWCR_STATUS_MASK
;
10267 swcr
|= (fpcr
>> 36) & SWCR_MAP_DMZ
;
10268 swcr
|= (~fpcr
>> 48) & (SWCR_TRAP_ENABLE_INV
10269 | SWCR_TRAP_ENABLE_DZE
10270 | SWCR_TRAP_ENABLE_OVF
);
10271 swcr
|= (~fpcr
>> 57) & (SWCR_TRAP_ENABLE_UNF
10272 | SWCR_TRAP_ENABLE_INE
);
10273 swcr
|= (fpcr
>> 47) & SWCR_MAP_UMZ
;
10274 swcr
|= (~fpcr
>> 41) & SWCR_TRAP_ENABLE_DNO
;
10276 if (put_user_u64 (swcr
, arg2
))
10282 /* case GSI_IEEE_STATE_AT_SIGNAL:
10283 -- Not implemented in linux kernel.
10285 -- Retrieves current unaligned access state; not much used.
10286 case GSI_PROC_TYPE:
10287 -- Retrieves implver information; surely not used.
10288 case GSI_GET_HWRPB:
10289 -- Grabs a copy of the HWRPB; surely not used.
10294 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
10295 /* Alpha specific */
10296 case TARGET_NR_osf_setsysinfo
:
10297 ret
= -TARGET_EOPNOTSUPP
;
10299 case TARGET_SSI_IEEE_FP_CONTROL
:
10301 uint64_t swcr
, fpcr
, orig_fpcr
;
10303 if (get_user_u64 (swcr
, arg2
)) {
10306 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
10307 fpcr
= orig_fpcr
& FPCR_DYN_MASK
;
10309 /* Copied from linux ieee_swcr_to_fpcr. */
10310 fpcr
|= (swcr
& SWCR_STATUS_MASK
) << 35;
10311 fpcr
|= (swcr
& SWCR_MAP_DMZ
) << 36;
10312 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_INV
10313 | SWCR_TRAP_ENABLE_DZE
10314 | SWCR_TRAP_ENABLE_OVF
)) << 48;
10315 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_UNF
10316 | SWCR_TRAP_ENABLE_INE
)) << 57;
10317 fpcr
|= (swcr
& SWCR_MAP_UMZ
? FPCR_UNDZ
| FPCR_UNFD
: 0);
10318 fpcr
|= (~swcr
& SWCR_TRAP_ENABLE_DNO
) << 41;
10320 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
10325 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
10327 uint64_t exc
, fpcr
, orig_fpcr
;
10330 if (get_user_u64(exc
, arg2
)) {
10334 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
10336 /* We only add to the exception status here. */
10337 fpcr
= orig_fpcr
| ((exc
& SWCR_STATUS_MASK
) << 35);
10339 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
10342 /* Old exceptions are not signaled. */
10343 fpcr
&= ~(orig_fpcr
& FPCR_STATUS_MASK
);
10345 /* If any exceptions set by this call,
10346 and are unmasked, send a signal. */
10348 if ((fpcr
& (FPCR_INE
| FPCR_INED
)) == FPCR_INE
) {
10349 si_code
= TARGET_FPE_FLTRES
;
10351 if ((fpcr
& (FPCR_UNF
| FPCR_UNFD
)) == FPCR_UNF
) {
10352 si_code
= TARGET_FPE_FLTUND
;
10354 if ((fpcr
& (FPCR_OVF
| FPCR_OVFD
)) == FPCR_OVF
) {
10355 si_code
= TARGET_FPE_FLTOVF
;
10357 if ((fpcr
& (FPCR_DZE
| FPCR_DZED
)) == FPCR_DZE
) {
10358 si_code
= TARGET_FPE_FLTDIV
;
10360 if ((fpcr
& (FPCR_INV
| FPCR_INVD
)) == FPCR_INV
) {
10361 si_code
= TARGET_FPE_FLTINV
;
10363 if (si_code
!= 0) {
10364 target_siginfo_t info
;
10365 info
.si_signo
= SIGFPE
;
10367 info
.si_code
= si_code
;
10368 info
._sifields
._sigfault
._addr
10369 = ((CPUArchState
*)cpu_env
)->pc
;
10370 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
10375 /* case SSI_NVPAIRS:
10376 -- Used with SSIN_UACPROC to enable unaligned accesses.
10377 case SSI_IEEE_STATE_AT_SIGNAL:
10378 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
10379 -- Not implemented in linux kernel
10384 #ifdef TARGET_NR_osf_sigprocmask
10385 /* Alpha specific. */
10386 case TARGET_NR_osf_sigprocmask
:
10390 sigset_t set
, oldset
;
10393 case TARGET_SIG_BLOCK
:
10396 case TARGET_SIG_UNBLOCK
:
10399 case TARGET_SIG_SETMASK
:
10403 ret
= -TARGET_EINVAL
;
10407 target_to_host_old_sigset(&set
, &mask
);
10408 ret
= do_sigprocmask(how
, &set
, &oldset
);
10410 host_to_target_old_sigset(&mask
, &oldset
);
10417 #ifdef TARGET_NR_getgid32
10418 case TARGET_NR_getgid32
:
10419 ret
= get_errno(getgid());
10422 #ifdef TARGET_NR_geteuid32
10423 case TARGET_NR_geteuid32
:
10424 ret
= get_errno(geteuid());
10427 #ifdef TARGET_NR_getegid32
10428 case TARGET_NR_getegid32
:
10429 ret
= get_errno(getegid());
10432 #ifdef TARGET_NR_setreuid32
10433 case TARGET_NR_setreuid32
:
10434 ret
= get_errno(setreuid(arg1
, arg2
));
10437 #ifdef TARGET_NR_setregid32
10438 case TARGET_NR_setregid32
:
10439 ret
= get_errno(setregid(arg1
, arg2
));
10442 #ifdef TARGET_NR_getgroups32
10443 case TARGET_NR_getgroups32
:
10445 int gidsetsize
= arg1
;
10446 uint32_t *target_grouplist
;
10450 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
10451 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
10452 if (gidsetsize
== 0)
10454 if (!is_error(ret
)) {
10455 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
10456 if (!target_grouplist
) {
10457 ret
= -TARGET_EFAULT
;
10460 for(i
= 0;i
< ret
; i
++)
10461 target_grouplist
[i
] = tswap32(grouplist
[i
]);
10462 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
10467 #ifdef TARGET_NR_setgroups32
10468 case TARGET_NR_setgroups32
:
10470 int gidsetsize
= arg1
;
10471 uint32_t *target_grouplist
;
10475 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
10476 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
10477 if (!target_grouplist
) {
10478 ret
= -TARGET_EFAULT
;
10481 for(i
= 0;i
< gidsetsize
; i
++)
10482 grouplist
[i
] = tswap32(target_grouplist
[i
]);
10483 unlock_user(target_grouplist
, arg2
, 0);
10484 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
10488 #ifdef TARGET_NR_fchown32
10489 case TARGET_NR_fchown32
:
10490 ret
= get_errno(fchown(arg1
, arg2
, arg3
));
10493 #ifdef TARGET_NR_setresuid32
10494 case TARGET_NR_setresuid32
:
10495 ret
= get_errno(sys_setresuid(arg1
, arg2
, arg3
));
10498 #ifdef TARGET_NR_getresuid32
10499 case TARGET_NR_getresuid32
:
10501 uid_t ruid
, euid
, suid
;
10502 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
10503 if (!is_error(ret
)) {
10504 if (put_user_u32(ruid
, arg1
)
10505 || put_user_u32(euid
, arg2
)
10506 || put_user_u32(suid
, arg3
))
10512 #ifdef TARGET_NR_setresgid32
10513 case TARGET_NR_setresgid32
:
10514 ret
= get_errno(sys_setresgid(arg1
, arg2
, arg3
));
10517 #ifdef TARGET_NR_getresgid32
10518 case TARGET_NR_getresgid32
:
10520 gid_t rgid
, egid
, sgid
;
10521 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
10522 if (!is_error(ret
)) {
10523 if (put_user_u32(rgid
, arg1
)
10524 || put_user_u32(egid
, arg2
)
10525 || put_user_u32(sgid
, arg3
))
10531 #ifdef TARGET_NR_chown32
10532 case TARGET_NR_chown32
:
10533 if (!(p
= lock_user_string(arg1
)))
10535 ret
= get_errno(chown(p
, arg2
, arg3
));
10536 unlock_user(p
, arg1
, 0);
10539 #ifdef TARGET_NR_setuid32
10540 case TARGET_NR_setuid32
:
10541 ret
= get_errno(sys_setuid(arg1
));
10544 #ifdef TARGET_NR_setgid32
10545 case TARGET_NR_setgid32
:
10546 ret
= get_errno(sys_setgid(arg1
));
10549 #ifdef TARGET_NR_setfsuid32
10550 case TARGET_NR_setfsuid32
:
10551 ret
= get_errno(setfsuid(arg1
));
10554 #ifdef TARGET_NR_setfsgid32
10555 case TARGET_NR_setfsgid32
:
10556 ret
= get_errno(setfsgid(arg1
));
10560 case TARGET_NR_pivot_root
:
10561 goto unimplemented
;
10562 #ifdef TARGET_NR_mincore
10563 case TARGET_NR_mincore
:
10566 ret
= -TARGET_EFAULT
;
10567 if (!(a
= lock_user(VERIFY_READ
, arg1
,arg2
, 0)))
10569 if (!(p
= lock_user_string(arg3
)))
10571 ret
= get_errno(mincore(a
, arg2
, p
));
10572 unlock_user(p
, arg3
, ret
);
10574 unlock_user(a
, arg1
, 0);
10578 #ifdef TARGET_NR_arm_fadvise64_64
10579 case TARGET_NR_arm_fadvise64_64
:
10580 /* arm_fadvise64_64 looks like fadvise64_64 but
10581 * with different argument order: fd, advice, offset, len
10582 * rather than the usual fd, offset, len, advice.
10583 * Note that offset and len are both 64-bit so appear as
10584 * pairs of 32-bit registers.
10586 ret
= posix_fadvise(arg1
, target_offset64(arg3
, arg4
),
10587 target_offset64(arg5
, arg6
), arg2
);
10588 ret
= -host_to_target_errno(ret
);
10592 #if TARGET_ABI_BITS == 32
10594 #ifdef TARGET_NR_fadvise64_64
10595 case TARGET_NR_fadvise64_64
:
10596 /* 6 args: fd, offset (high, low), len (high, low), advice */
10597 if (regpairs_aligned(cpu_env
)) {
10598 /* offset is in (3,4), len in (5,6) and advice in 7 */
10605 ret
= -host_to_target_errno(posix_fadvise(arg1
,
10606 target_offset64(arg2
, arg3
),
10607 target_offset64(arg4
, arg5
),
10612 #ifdef TARGET_NR_fadvise64
10613 case TARGET_NR_fadvise64
:
10614 /* 5 args: fd, offset (high, low), len, advice */
10615 if (regpairs_aligned(cpu_env
)) {
10616 /* offset is in (3,4), len in 5 and advice in 6 */
10622 ret
= -host_to_target_errno(posix_fadvise(arg1
,
10623 target_offset64(arg2
, arg3
),
10628 #else /* not a 32-bit ABI */
10629 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
10630 #ifdef TARGET_NR_fadvise64_64
10631 case TARGET_NR_fadvise64_64
:
10633 #ifdef TARGET_NR_fadvise64
10634 case TARGET_NR_fadvise64
:
10636 #ifdef TARGET_S390X
10638 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
10639 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
10640 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
10641 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
10645 ret
= -host_to_target_errno(posix_fadvise(arg1
, arg2
, arg3
, arg4
));
10648 #endif /* end of 64-bit ABI fadvise handling */
10650 #ifdef TARGET_NR_madvise
10651 case TARGET_NR_madvise
:
10652 /* A straight passthrough may not be safe because qemu sometimes
10653 turns private file-backed mappings into anonymous mappings.
10654 This will break MADV_DONTNEED.
10655 This is a hint, so ignoring and returning success is ok. */
10656 ret
= get_errno(0);
10659 #if TARGET_ABI_BITS == 32
10660 case TARGET_NR_fcntl64
:
10664 from_flock64_fn
*copyfrom
= copy_from_user_flock64
;
10665 to_flock64_fn
*copyto
= copy_to_user_flock64
;
10668 if (((CPUARMState
*)cpu_env
)->eabi
) {
10669 copyfrom
= copy_from_user_eabi_flock64
;
10670 copyto
= copy_to_user_eabi_flock64
;
10674 cmd
= target_to_host_fcntl_cmd(arg2
);
10675 if (cmd
== -TARGET_EINVAL
) {
10681 case TARGET_F_GETLK64
:
10682 ret
= copyfrom(&fl
, arg3
);
10686 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
10688 ret
= copyto(arg3
, &fl
);
10692 case TARGET_F_SETLK64
:
10693 case TARGET_F_SETLKW64
:
10694 ret
= copyfrom(&fl
, arg3
);
10698 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
10701 ret
= do_fcntl(arg1
, arg2
, arg3
);
10707 #ifdef TARGET_NR_cacheflush
10708 case TARGET_NR_cacheflush
:
10709 /* self-modifying code is handled automatically, so nothing needed */
10713 #ifdef TARGET_NR_security
10714 case TARGET_NR_security
:
10715 goto unimplemented
;
10717 #ifdef TARGET_NR_getpagesize
10718 case TARGET_NR_getpagesize
:
10719 ret
= TARGET_PAGE_SIZE
;
10722 case TARGET_NR_gettid
:
10723 ret
= get_errno(gettid());
10725 #ifdef TARGET_NR_readahead
10726 case TARGET_NR_readahead
:
10727 #if TARGET_ABI_BITS == 32
10728 if (regpairs_aligned(cpu_env
)) {
10733 ret
= get_errno(readahead(arg1
, ((off64_t
)arg3
<< 32) | arg2
, arg4
));
10735 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
10740 #ifdef TARGET_NR_setxattr
10741 case TARGET_NR_listxattr
:
10742 case TARGET_NR_llistxattr
:
10746 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10748 ret
= -TARGET_EFAULT
;
10752 p
= lock_user_string(arg1
);
10754 if (num
== TARGET_NR_listxattr
) {
10755 ret
= get_errno(listxattr(p
, b
, arg3
));
10757 ret
= get_errno(llistxattr(p
, b
, arg3
));
10760 ret
= -TARGET_EFAULT
;
10762 unlock_user(p
, arg1
, 0);
10763 unlock_user(b
, arg2
, arg3
);
10766 case TARGET_NR_flistxattr
:
10770 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10772 ret
= -TARGET_EFAULT
;
10776 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
10777 unlock_user(b
, arg2
, arg3
);
10780 case TARGET_NR_setxattr
:
10781 case TARGET_NR_lsetxattr
:
10783 void *p
, *n
, *v
= 0;
10785 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
10787 ret
= -TARGET_EFAULT
;
10791 p
= lock_user_string(arg1
);
10792 n
= lock_user_string(arg2
);
10794 if (num
== TARGET_NR_setxattr
) {
10795 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
10797 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
10800 ret
= -TARGET_EFAULT
;
10802 unlock_user(p
, arg1
, 0);
10803 unlock_user(n
, arg2
, 0);
10804 unlock_user(v
, arg3
, 0);
10807 case TARGET_NR_fsetxattr
:
10811 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
10813 ret
= -TARGET_EFAULT
;
10817 n
= lock_user_string(arg2
);
10819 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
10821 ret
= -TARGET_EFAULT
;
10823 unlock_user(n
, arg2
, 0);
10824 unlock_user(v
, arg3
, 0);
10827 case TARGET_NR_getxattr
:
10828 case TARGET_NR_lgetxattr
:
10830 void *p
, *n
, *v
= 0;
10832 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
10834 ret
= -TARGET_EFAULT
;
10838 p
= lock_user_string(arg1
);
10839 n
= lock_user_string(arg2
);
10841 if (num
== TARGET_NR_getxattr
) {
10842 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
10844 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
10847 ret
= -TARGET_EFAULT
;
10849 unlock_user(p
, arg1
, 0);
10850 unlock_user(n
, arg2
, 0);
10851 unlock_user(v
, arg3
, arg4
);
10854 case TARGET_NR_fgetxattr
:
10858 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
10860 ret
= -TARGET_EFAULT
;
10864 n
= lock_user_string(arg2
);
10866 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
10868 ret
= -TARGET_EFAULT
;
10870 unlock_user(n
, arg2
, 0);
10871 unlock_user(v
, arg3
, arg4
);
10874 case TARGET_NR_removexattr
:
10875 case TARGET_NR_lremovexattr
:
10878 p
= lock_user_string(arg1
);
10879 n
= lock_user_string(arg2
);
10881 if (num
== TARGET_NR_removexattr
) {
10882 ret
= get_errno(removexattr(p
, n
));
10884 ret
= get_errno(lremovexattr(p
, n
));
10887 ret
= -TARGET_EFAULT
;
10889 unlock_user(p
, arg1
, 0);
10890 unlock_user(n
, arg2
, 0);
10893 case TARGET_NR_fremovexattr
:
10896 n
= lock_user_string(arg2
);
10898 ret
= get_errno(fremovexattr(arg1
, n
));
10900 ret
= -TARGET_EFAULT
;
10902 unlock_user(n
, arg2
, 0);
10906 #endif /* CONFIG_ATTR */
10907 #ifdef TARGET_NR_set_thread_area
10908 case TARGET_NR_set_thread_area
:
10909 #if defined(TARGET_MIPS)
10910 ((CPUMIPSState
*) cpu_env
)->active_tc
.CP0_UserLocal
= arg1
;
10913 #elif defined(TARGET_CRIS)
10915 ret
= -TARGET_EINVAL
;
10917 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
10921 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
10922 ret
= do_set_thread_area(cpu_env
, arg1
);
10924 #elif defined(TARGET_M68K)
10926 TaskState
*ts
= cpu
->opaque
;
10927 ts
->tp_value
= arg1
;
10932 goto unimplemented_nowarn
;
10935 #ifdef TARGET_NR_get_thread_area
10936 case TARGET_NR_get_thread_area
:
10937 #if defined(TARGET_I386) && defined(TARGET_ABI32)
10938 ret
= do_get_thread_area(cpu_env
, arg1
);
10940 #elif defined(TARGET_M68K)
10942 TaskState
*ts
= cpu
->opaque
;
10943 ret
= ts
->tp_value
;
10947 goto unimplemented_nowarn
;
10950 #ifdef TARGET_NR_getdomainname
10951 case TARGET_NR_getdomainname
:
10952 goto unimplemented_nowarn
;
10955 #ifdef TARGET_NR_clock_gettime
10956 case TARGET_NR_clock_gettime
:
10958 struct timespec ts
;
10959 ret
= get_errno(clock_gettime(arg1
, &ts
));
10960 if (!is_error(ret
)) {
10961 host_to_target_timespec(arg2
, &ts
);
10966 #ifdef TARGET_NR_clock_getres
10967 case TARGET_NR_clock_getres
:
10969 struct timespec ts
;
10970 ret
= get_errno(clock_getres(arg1
, &ts
));
10971 if (!is_error(ret
)) {
10972 host_to_target_timespec(arg2
, &ts
);
10977 #ifdef TARGET_NR_clock_nanosleep
10978 case TARGET_NR_clock_nanosleep
:
10980 struct timespec ts
;
10981 target_to_host_timespec(&ts
, arg3
);
10982 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
10983 &ts
, arg4
? &ts
: NULL
));
10985 host_to_target_timespec(arg4
, &ts
);
10987 #if defined(TARGET_PPC)
10988 /* clock_nanosleep is odd in that it returns positive errno values.
10989 * On PPC, CR0 bit 3 should be set in such a situation. */
10990 if (ret
&& ret
!= -TARGET_ERESTARTSYS
) {
10991 ((CPUPPCState
*)cpu_env
)->crf
[0] |= 1;
10998 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
10999 case TARGET_NR_set_tid_address
:
11000 ret
= get_errno(set_tid_address((int *)g2h(arg1
)));
11004 case TARGET_NR_tkill
:
11005 ret
= get_errno(safe_tkill((int)arg1
, target_to_host_signal(arg2
)));
11008 case TARGET_NR_tgkill
:
11009 ret
= get_errno(safe_tgkill((int)arg1
, (int)arg2
,
11010 target_to_host_signal(arg3
)));
11013 #ifdef TARGET_NR_set_robust_list
11014 case TARGET_NR_set_robust_list
:
11015 case TARGET_NR_get_robust_list
:
11016 /* The ABI for supporting robust futexes has userspace pass
11017 * the kernel a pointer to a linked list which is updated by
11018 * userspace after the syscall; the list is walked by the kernel
11019 * when the thread exits. Since the linked list in QEMU guest
11020 * memory isn't a valid linked list for the host and we have
11021 * no way to reliably intercept the thread-death event, we can't
11022 * support these. Silently return ENOSYS so that guest userspace
11023 * falls back to a non-robust futex implementation (which should
11024 * be OK except in the corner case of the guest crashing while
11025 * holding a mutex that is shared with another process via
11028 goto unimplemented_nowarn
;
11031 #if defined(TARGET_NR_utimensat)
11032 case TARGET_NR_utimensat
:
11034 struct timespec
*tsp
, ts
[2];
11038 target_to_host_timespec(ts
, arg3
);
11039 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
11043 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
11045 if (!(p
= lock_user_string(arg2
))) {
11046 ret
= -TARGET_EFAULT
;
11049 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
11050 unlock_user(p
, arg2
, 0);
11055 case TARGET_NR_futex
:
11056 ret
= do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
11058 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
11059 case TARGET_NR_inotify_init
:
11060 ret
= get_errno(sys_inotify_init());
11063 #ifdef CONFIG_INOTIFY1
11064 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
11065 case TARGET_NR_inotify_init1
:
11066 ret
= get_errno(sys_inotify_init1(arg1
));
11070 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
11071 case TARGET_NR_inotify_add_watch
:
11072 p
= lock_user_string(arg2
);
11073 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
11074 unlock_user(p
, arg2
, 0);
11077 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
11078 case TARGET_NR_inotify_rm_watch
:
11079 ret
= get_errno(sys_inotify_rm_watch(arg1
, arg2
));
11083 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
11084 case TARGET_NR_mq_open
:
11086 struct mq_attr posix_mq_attr
, *attrp
;
11088 p
= lock_user_string(arg1
- 1);
11090 copy_from_user_mq_attr (&posix_mq_attr
, arg4
);
11091 attrp
= &posix_mq_attr
;
11095 ret
= get_errno(mq_open(p
, arg2
, arg3
, attrp
));
11096 unlock_user (p
, arg1
, 0);
11100 case TARGET_NR_mq_unlink
:
11101 p
= lock_user_string(arg1
- 1);
11102 ret
= get_errno(mq_unlink(p
));
11103 unlock_user (p
, arg1
, 0);
11106 case TARGET_NR_mq_timedsend
:
11108 struct timespec ts
;
11110 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
11112 target_to_host_timespec(&ts
, arg5
);
11113 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
11114 host_to_target_timespec(arg5
, &ts
);
11116 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
11118 unlock_user (p
, arg2
, arg3
);
11122 case TARGET_NR_mq_timedreceive
:
11124 struct timespec ts
;
11127 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
11129 target_to_host_timespec(&ts
, arg5
);
11130 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
11132 host_to_target_timespec(arg5
, &ts
);
11134 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
11137 unlock_user (p
, arg2
, arg3
);
11139 put_user_u32(prio
, arg4
);
11143 /* Not implemented for now... */
11144 /* case TARGET_NR_mq_notify: */
11147 case TARGET_NR_mq_getsetattr
:
11149 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
11152 ret
= mq_getattr(arg1
, &posix_mq_attr_out
);
11153 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
11156 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
11157 ret
|= mq_setattr(arg1
, &posix_mq_attr_in
, &posix_mq_attr_out
);
11164 #ifdef CONFIG_SPLICE
11165 #ifdef TARGET_NR_tee
11166 case TARGET_NR_tee
:
11168 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
11172 #ifdef TARGET_NR_splice
11173 case TARGET_NR_splice
:
11175 loff_t loff_in
, loff_out
;
11176 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
11178 if (get_user_u64(loff_in
, arg2
)) {
11181 ploff_in
= &loff_in
;
11184 if (get_user_u64(loff_out
, arg4
)) {
11187 ploff_out
= &loff_out
;
11189 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
11191 if (put_user_u64(loff_in
, arg2
)) {
11196 if (put_user_u64(loff_out
, arg4
)) {
11203 #ifdef TARGET_NR_vmsplice
11204 case TARGET_NR_vmsplice
:
11206 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
11208 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
11209 unlock_iovec(vec
, arg2
, arg3
, 0);
11211 ret
= -host_to_target_errno(errno
);
11216 #endif /* CONFIG_SPLICE */
11217 #ifdef CONFIG_EVENTFD
11218 #if defined(TARGET_NR_eventfd)
11219 case TARGET_NR_eventfd
:
11220 ret
= get_errno(eventfd(arg1
, 0));
11221 fd_trans_unregister(ret
);
11224 #if defined(TARGET_NR_eventfd2)
11225 case TARGET_NR_eventfd2
:
11227 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
));
11228 if (arg2
& TARGET_O_NONBLOCK
) {
11229 host_flags
|= O_NONBLOCK
;
11231 if (arg2
& TARGET_O_CLOEXEC
) {
11232 host_flags
|= O_CLOEXEC
;
11234 ret
= get_errno(eventfd(arg1
, host_flags
));
11235 fd_trans_unregister(ret
);
11239 #endif /* CONFIG_EVENTFD */
11240 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
11241 case TARGET_NR_fallocate
:
11242 #if TARGET_ABI_BITS == 32
11243 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
11244 target_offset64(arg5
, arg6
)));
11246 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
11250 #if defined(CONFIG_SYNC_FILE_RANGE)
11251 #if defined(TARGET_NR_sync_file_range)
11252 case TARGET_NR_sync_file_range
:
11253 #if TARGET_ABI_BITS == 32
11254 #if defined(TARGET_MIPS)
11255 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
11256 target_offset64(arg5
, arg6
), arg7
));
11258 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
11259 target_offset64(arg4
, arg5
), arg6
));
11260 #endif /* !TARGET_MIPS */
11262 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
11266 #if defined(TARGET_NR_sync_file_range2)
11267 case TARGET_NR_sync_file_range2
:
11268 /* This is like sync_file_range but the arguments are reordered */
11269 #if TARGET_ABI_BITS == 32
11270 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
11271 target_offset64(arg5
, arg6
), arg2
));
11273 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
11278 #if defined(TARGET_NR_signalfd4)
11279 case TARGET_NR_signalfd4
:
11280 ret
= do_signalfd4(arg1
, arg2
, arg4
);
11283 #if defined(TARGET_NR_signalfd)
11284 case TARGET_NR_signalfd
:
11285 ret
= do_signalfd4(arg1
, arg2
, 0);
11288 #if defined(CONFIG_EPOLL)
11289 #if defined(TARGET_NR_epoll_create)
11290 case TARGET_NR_epoll_create
:
11291 ret
= get_errno(epoll_create(arg1
));
11294 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
11295 case TARGET_NR_epoll_create1
:
11296 ret
= get_errno(epoll_create1(arg1
));
11299 #if defined(TARGET_NR_epoll_ctl)
11300 case TARGET_NR_epoll_ctl
:
11302 struct epoll_event ep
;
11303 struct epoll_event
*epp
= 0;
11305 struct target_epoll_event
*target_ep
;
11306 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
11309 ep
.events
= tswap32(target_ep
->events
);
11310 /* The epoll_data_t union is just opaque data to the kernel,
11311 * so we transfer all 64 bits across and need not worry what
11312 * actual data type it is.
11314 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
11315 unlock_user_struct(target_ep
, arg4
, 0);
11318 ret
= get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
11323 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
11324 #if defined(TARGET_NR_epoll_wait)
11325 case TARGET_NR_epoll_wait
:
11327 #if defined(TARGET_NR_epoll_pwait)
11328 case TARGET_NR_epoll_pwait
:
11331 struct target_epoll_event
*target_ep
;
11332 struct epoll_event
*ep
;
11334 int maxevents
= arg3
;
11335 int timeout
= arg4
;
11337 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
11338 maxevents
* sizeof(struct target_epoll_event
), 1);
11343 ep
= alloca(maxevents
* sizeof(struct epoll_event
));
11346 #if defined(TARGET_NR_epoll_pwait)
11347 case TARGET_NR_epoll_pwait
:
11349 target_sigset_t
*target_set
;
11350 sigset_t _set
, *set
= &_set
;
11353 if (arg6
!= sizeof(target_sigset_t
)) {
11354 ret
= -TARGET_EINVAL
;
11358 target_set
= lock_user(VERIFY_READ
, arg5
,
11359 sizeof(target_sigset_t
), 1);
11361 unlock_user(target_ep
, arg2
, 0);
11364 target_to_host_sigset(set
, target_set
);
11365 unlock_user(target_set
, arg5
, 0);
11370 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
11371 set
, SIGSET_T_SIZE
));
11375 #if defined(TARGET_NR_epoll_wait)
11376 case TARGET_NR_epoll_wait
:
11377 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
11382 ret
= -TARGET_ENOSYS
;
11384 if (!is_error(ret
)) {
11386 for (i
= 0; i
< ret
; i
++) {
11387 target_ep
[i
].events
= tswap32(ep
[i
].events
);
11388 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
11391 unlock_user(target_ep
, arg2
, ret
* sizeof(struct target_epoll_event
));
11396 #ifdef TARGET_NR_prlimit64
11397 case TARGET_NR_prlimit64
:
11399 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
11400 struct target_rlimit64
*target_rnew
, *target_rold
;
11401 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
11402 int resource
= target_to_host_resource(arg2
);
11404 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
11407 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
11408 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
11409 unlock_user_struct(target_rnew
, arg3
, 0);
11413 ret
= get_errno(sys_prlimit64(arg1
, resource
, rnewp
, arg4
? &rold
: 0));
11414 if (!is_error(ret
) && arg4
) {
11415 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
11418 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
11419 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
11420 unlock_user_struct(target_rold
, arg4
, 1);
11425 #ifdef TARGET_NR_gethostname
11426 case TARGET_NR_gethostname
:
11428 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
11430 ret
= get_errno(gethostname(name
, arg2
));
11431 unlock_user(name
, arg1
, arg2
);
11433 ret
= -TARGET_EFAULT
;
11438 #ifdef TARGET_NR_atomic_cmpxchg_32
11439 case TARGET_NR_atomic_cmpxchg_32
:
11441 /* should use start_exclusive from main.c */
11442 abi_ulong mem_value
;
11443 if (get_user_u32(mem_value
, arg6
)) {
11444 target_siginfo_t info
;
11445 info
.si_signo
= SIGSEGV
;
11447 info
.si_code
= TARGET_SEGV_MAPERR
;
11448 info
._sifields
._sigfault
._addr
= arg6
;
11449 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
11453 if (mem_value
== arg2
)
11454 put_user_u32(arg1
, arg6
);
11459 #ifdef TARGET_NR_atomic_barrier
11460 case TARGET_NR_atomic_barrier
:
11462 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
11468 #ifdef TARGET_NR_timer_create
11469 case TARGET_NR_timer_create
:
11471 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
11473 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
11476 int timer_index
= next_free_host_timer();
11478 if (timer_index
< 0) {
11479 ret
= -TARGET_EAGAIN
;
11481 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
11484 phost_sevp
= &host_sevp
;
11485 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
11491 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
11495 if (put_user(TIMER_MAGIC
| timer_index
, arg3
, target_timer_t
)) {
11504 #ifdef TARGET_NR_timer_settime
11505 case TARGET_NR_timer_settime
:
11507 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
11508 * struct itimerspec * old_value */
11509 target_timer_t timerid
= get_timer_id(arg1
);
11513 } else if (arg3
== 0) {
11514 ret
= -TARGET_EINVAL
;
11516 timer_t htimer
= g_posix_timers
[timerid
];
11517 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
11519 target_to_host_itimerspec(&hspec_new
, arg3
);
11521 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
11522 host_to_target_itimerspec(arg2
, &hspec_old
);
11528 #ifdef TARGET_NR_timer_gettime
11529 case TARGET_NR_timer_gettime
:
11531 /* args: timer_t timerid, struct itimerspec *curr_value */
11532 target_timer_t timerid
= get_timer_id(arg1
);
11536 } else if (!arg2
) {
11537 ret
= -TARGET_EFAULT
;
11539 timer_t htimer
= g_posix_timers
[timerid
];
11540 struct itimerspec hspec
;
11541 ret
= get_errno(timer_gettime(htimer
, &hspec
));
11543 if (host_to_target_itimerspec(arg2
, &hspec
)) {
11544 ret
= -TARGET_EFAULT
;
11551 #ifdef TARGET_NR_timer_getoverrun
11552 case TARGET_NR_timer_getoverrun
:
11554 /* args: timer_t timerid */
11555 target_timer_t timerid
= get_timer_id(arg1
);
11560 timer_t htimer
= g_posix_timers
[timerid
];
11561 ret
= get_errno(timer_getoverrun(htimer
));
11563 fd_trans_unregister(ret
);
11568 #ifdef TARGET_NR_timer_delete
11569 case TARGET_NR_timer_delete
:
11571 /* args: timer_t timerid */
11572 target_timer_t timerid
= get_timer_id(arg1
);
11577 timer_t htimer
= g_posix_timers
[timerid
];
11578 ret
= get_errno(timer_delete(htimer
));
11579 g_posix_timers
[timerid
] = 0;
11585 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
11586 case TARGET_NR_timerfd_create
:
11587 ret
= get_errno(timerfd_create(arg1
,
11588 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
11592 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
11593 case TARGET_NR_timerfd_gettime
:
11595 struct itimerspec its_curr
;
11597 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
11599 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
11606 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
11607 case TARGET_NR_timerfd_settime
:
11609 struct itimerspec its_new
, its_old
, *p_new
;
11612 if (target_to_host_itimerspec(&its_new
, arg3
)) {
11620 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
11622 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
11629 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
11630 case TARGET_NR_ioprio_get
:
11631 ret
= get_errno(ioprio_get(arg1
, arg2
));
11635 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
11636 case TARGET_NR_ioprio_set
:
11637 ret
= get_errno(ioprio_set(arg1
, arg2
, arg3
));
11641 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
11642 case TARGET_NR_setns
:
11643 ret
= get_errno(setns(arg1
, arg2
));
11646 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
11647 case TARGET_NR_unshare
:
11648 ret
= get_errno(unshare(arg1
));
11654 gemu_log("qemu: Unsupported syscall: %d\n", num
);
11655 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
11656 unimplemented_nowarn
:
11658 ret
= -TARGET_ENOSYS
;
11663 gemu_log(" = " TARGET_ABI_FMT_ld
"\n", ret
);
11666 print_syscall_ret(num
, ret
);
11667 trace_guest_user_syscall_ret(cpu
, num
, ret
);
11670 ret
= -TARGET_EFAULT
;