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>
37 #include <linux/capability.h>
40 int __clone2(int (*fn
)(void *), void *child_stack_base
,
41 size_t stack_size
, int flags
, void *arg
, ...);
43 #include <sys/socket.h>
47 #include <sys/times.h>
50 #include <sys/statfs.h>
52 #include <sys/sysinfo.h>
53 #include <sys/signalfd.h>
54 //#include <sys/user.h>
55 #include <netinet/ip.h>
56 #include <netinet/tcp.h>
57 #include <linux/wireless.h>
58 #include <linux/icmp.h>
59 #include "qemu-common.h"
61 #include <sys/timerfd.h>
67 #include <sys/eventfd.h>
70 #include <sys/epoll.h>
73 #include "qemu/xattr.h"
75 #ifdef CONFIG_SENDFILE
76 #include <sys/sendfile.h>
79 #define termios host_termios
80 #define winsize host_winsize
81 #define termio host_termio
82 #define sgttyb host_sgttyb /* same as target */
83 #define tchars host_tchars /* same as target */
84 #define ltchars host_ltchars /* same as target */
86 #include <linux/termios.h>
87 #include <linux/unistd.h>
88 #include <linux/cdrom.h>
89 #include <linux/hdreg.h>
90 #include <linux/soundcard.h>
92 #include <linux/mtio.h>
94 #if defined(CONFIG_FIEMAP)
95 #include <linux/fiemap.h>
99 #include <linux/dm-ioctl.h>
100 #include <linux/reboot.h>
101 #include <linux/route.h>
102 #include <linux/filter.h>
103 #include <linux/blkpg.h>
104 #include "linux_loop.h"
109 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
110 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
113 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
114 * once. This exercises the codepaths for restart.
116 //#define DEBUG_ERESTARTSYS
118 //#include <linux/msdos_fs.h>
119 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
120 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
131 #define _syscall0(type,name) \
132 static type name (void) \
134 return syscall(__NR_##name); \
137 #define _syscall1(type,name,type1,arg1) \
138 static type name (type1 arg1) \
140 return syscall(__NR_##name, arg1); \
143 #define _syscall2(type,name,type1,arg1,type2,arg2) \
144 static type name (type1 arg1,type2 arg2) \
146 return syscall(__NR_##name, arg1, arg2); \
149 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
150 static type name (type1 arg1,type2 arg2,type3 arg3) \
152 return syscall(__NR_##name, arg1, arg2, arg3); \
155 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
156 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
158 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
161 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
163 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
165 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
169 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
170 type5,arg5,type6,arg6) \
171 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
174 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
178 #define __NR_sys_uname __NR_uname
179 #define __NR_sys_getcwd1 __NR_getcwd
180 #define __NR_sys_getdents __NR_getdents
181 #define __NR_sys_getdents64 __NR_getdents64
182 #define __NR_sys_getpriority __NR_getpriority
183 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
184 #define __NR_sys_syslog __NR_syslog
185 #define __NR_sys_tgkill __NR_tgkill
186 #define __NR_sys_tkill __NR_tkill
187 #define __NR_sys_futex __NR_futex
188 #define __NR_sys_inotify_init __NR_inotify_init
189 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
190 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
192 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
194 #define __NR__llseek __NR_lseek
197 /* Newer kernel ports have llseek() instead of _llseek() */
198 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
199 #define TARGET_NR__llseek TARGET_NR_llseek
203 _syscall0(int, gettid
)
205 /* This is a replacement for the host gettid() and must return a host
207 static int gettid(void) {
211 #if defined(TARGET_NR_getdents) && defined(__NR_getdents)
212 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
214 #if !defined(__NR_getdents) || \
215 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
216 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
218 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
219 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
220 loff_t
*, res
, uint
, wh
);
222 _syscall3(int,sys_rt_sigqueueinfo
,int,pid
,int,sig
,siginfo_t
*,uinfo
)
223 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
224 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
225 _syscall3(int,sys_tgkill
,int,tgid
,int,pid
,int,sig
)
227 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
228 _syscall2(int,sys_tkill
,int,tid
,int,sig
)
230 #ifdef __NR_exit_group
231 _syscall1(int,exit_group
,int,error_code
)
233 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234 _syscall1(int,set_tid_address
,int *,tidptr
)
236 #if defined(TARGET_NR_futex) && defined(__NR_futex)
237 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
238 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
240 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
241 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
242 unsigned long *, user_mask_ptr
);
243 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
244 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
245 unsigned long *, user_mask_ptr
);
246 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
248 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
249 struct __user_cap_data_struct
*, data
);
250 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
251 struct __user_cap_data_struct
*, data
);
252 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
253 _syscall2(int, ioprio_get
, int, which
, int, who
)
255 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
256 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
258 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
259 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
262 static bitmask_transtbl fcntl_flags_tbl
[] = {
263 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
264 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
265 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
266 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
267 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
268 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
269 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
270 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
271 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
272 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
273 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
274 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
275 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
276 #if defined(O_DIRECT)
277 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
279 #if defined(O_NOATIME)
280 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
282 #if defined(O_CLOEXEC)
283 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
286 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
288 /* Don't terminate the list prematurely on 64-bit host+guest. */
289 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
290 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
295 typedef abi_long (*TargetFdDataFunc
)(void *, size_t);
296 typedef abi_long (*TargetFdAddrFunc
)(void *, abi_ulong
, socklen_t
);
297 typedef struct TargetFdTrans
{
298 TargetFdDataFunc host_to_target_data
;
299 TargetFdDataFunc target_to_host_data
;
300 TargetFdAddrFunc target_to_host_addr
;
303 static TargetFdTrans
**target_fd_trans
;
305 static unsigned int target_fd_max
;
307 static TargetFdDataFunc
fd_trans_host_to_target_data(int fd
)
309 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
310 return target_fd_trans
[fd
]->host_to_target_data
;
315 static TargetFdAddrFunc
fd_trans_target_to_host_addr(int fd
)
317 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
318 return target_fd_trans
[fd
]->target_to_host_addr
;
323 static void fd_trans_register(int fd
, TargetFdTrans
*trans
)
327 if (fd
>= target_fd_max
) {
328 oldmax
= target_fd_max
;
329 target_fd_max
= ((fd
>> 6) + 1) << 6; /* by slice of 64 entries */
330 target_fd_trans
= g_renew(TargetFdTrans
*,
331 target_fd_trans
, target_fd_max
);
332 memset((void *)(target_fd_trans
+ oldmax
), 0,
333 (target_fd_max
- oldmax
) * sizeof(TargetFdTrans
*));
335 target_fd_trans
[fd
] = trans
;
338 static void fd_trans_unregister(int fd
)
340 if (fd
>= 0 && fd
< target_fd_max
) {
341 target_fd_trans
[fd
] = NULL
;
345 static void fd_trans_dup(int oldfd
, int newfd
)
347 fd_trans_unregister(newfd
);
348 if (oldfd
< target_fd_max
&& target_fd_trans
[oldfd
]) {
349 fd_trans_register(newfd
, target_fd_trans
[oldfd
]);
353 static int sys_getcwd1(char *buf
, size_t size
)
355 if (getcwd(buf
, size
) == NULL
) {
356 /* getcwd() sets errno */
359 return strlen(buf
)+1;
362 #ifdef TARGET_NR_utimensat
363 #ifdef CONFIG_UTIMENSAT
364 static int sys_utimensat(int dirfd
, const char *pathname
,
365 const struct timespec times
[2], int flags
)
367 if (pathname
== NULL
)
368 return futimens(dirfd
, times
);
370 return utimensat(dirfd
, pathname
, times
, flags
);
372 #elif defined(__NR_utimensat)
373 #define __NR_sys_utimensat __NR_utimensat
374 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
375 const struct timespec
*,tsp
,int,flags
)
377 static int sys_utimensat(int dirfd
, const char *pathname
,
378 const struct timespec times
[2], int flags
)
384 #endif /* TARGET_NR_utimensat */
386 #ifdef CONFIG_INOTIFY
387 #include <sys/inotify.h>
389 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
390 static int sys_inotify_init(void)
392 return (inotify_init());
395 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
396 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
398 return (inotify_add_watch(fd
, pathname
, mask
));
401 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
402 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
404 return (inotify_rm_watch(fd
, wd
));
407 #ifdef CONFIG_INOTIFY1
408 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
409 static int sys_inotify_init1(int flags
)
411 return (inotify_init1(flags
));
416 /* Userspace can usually survive runtime without inotify */
417 #undef TARGET_NR_inotify_init
418 #undef TARGET_NR_inotify_init1
419 #undef TARGET_NR_inotify_add_watch
420 #undef TARGET_NR_inotify_rm_watch
421 #endif /* CONFIG_INOTIFY */
423 #if defined(TARGET_NR_ppoll)
425 # define __NR_ppoll -1
427 #define __NR_sys_ppoll __NR_ppoll
428 _syscall5(int, sys_ppoll
, struct pollfd
*, fds
, nfds_t
, nfds
,
429 struct timespec
*, timeout
, const sigset_t
*, sigmask
,
433 #if defined(TARGET_NR_prlimit64)
434 #ifndef __NR_prlimit64
435 # define __NR_prlimit64 -1
437 #define __NR_sys_prlimit64 __NR_prlimit64
438 /* The glibc rlimit structure may not be that used by the underlying syscall */
439 struct host_rlimit64
{
443 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
444 const struct host_rlimit64
*, new_limit
,
445 struct host_rlimit64
*, old_limit
)
449 #if defined(TARGET_NR_timer_create)
450 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
451 static timer_t g_posix_timers
[32] = { 0, } ;
453 static inline int next_free_host_timer(void)
456 /* FIXME: Does finding the next free slot require a lock? */
457 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
458 if (g_posix_timers
[k
] == 0) {
459 g_posix_timers
[k
] = (timer_t
) 1;
467 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
469 static inline int regpairs_aligned(void *cpu_env
) {
470 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
472 #elif defined(TARGET_MIPS)
473 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
474 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
475 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
476 * of registers which translates to the same as ARM/MIPS, because we start with
478 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
480 static inline int regpairs_aligned(void *cpu_env
) { return 0; }
483 #define ERRNO_TABLE_SIZE 1200
485 /* target_to_host_errno_table[] is initialized from
486 * host_to_target_errno_table[] in syscall_init(). */
487 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
491 * This list is the union of errno values overridden in asm-<arch>/errno.h
492 * minus the errnos that are not actually generic to all archs.
494 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
495 [EAGAIN
] = TARGET_EAGAIN
,
496 [EIDRM
] = TARGET_EIDRM
,
497 [ECHRNG
] = TARGET_ECHRNG
,
498 [EL2NSYNC
] = TARGET_EL2NSYNC
,
499 [EL3HLT
] = TARGET_EL3HLT
,
500 [EL3RST
] = TARGET_EL3RST
,
501 [ELNRNG
] = TARGET_ELNRNG
,
502 [EUNATCH
] = TARGET_EUNATCH
,
503 [ENOCSI
] = TARGET_ENOCSI
,
504 [EL2HLT
] = TARGET_EL2HLT
,
505 [EDEADLK
] = TARGET_EDEADLK
,
506 [ENOLCK
] = TARGET_ENOLCK
,
507 [EBADE
] = TARGET_EBADE
,
508 [EBADR
] = TARGET_EBADR
,
509 [EXFULL
] = TARGET_EXFULL
,
510 [ENOANO
] = TARGET_ENOANO
,
511 [EBADRQC
] = TARGET_EBADRQC
,
512 [EBADSLT
] = TARGET_EBADSLT
,
513 [EBFONT
] = TARGET_EBFONT
,
514 [ENOSTR
] = TARGET_ENOSTR
,
515 [ENODATA
] = TARGET_ENODATA
,
516 [ETIME
] = TARGET_ETIME
,
517 [ENOSR
] = TARGET_ENOSR
,
518 [ENONET
] = TARGET_ENONET
,
519 [ENOPKG
] = TARGET_ENOPKG
,
520 [EREMOTE
] = TARGET_EREMOTE
,
521 [ENOLINK
] = TARGET_ENOLINK
,
522 [EADV
] = TARGET_EADV
,
523 [ESRMNT
] = TARGET_ESRMNT
,
524 [ECOMM
] = TARGET_ECOMM
,
525 [EPROTO
] = TARGET_EPROTO
,
526 [EDOTDOT
] = TARGET_EDOTDOT
,
527 [EMULTIHOP
] = TARGET_EMULTIHOP
,
528 [EBADMSG
] = TARGET_EBADMSG
,
529 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
530 [EOVERFLOW
] = TARGET_EOVERFLOW
,
531 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
532 [EBADFD
] = TARGET_EBADFD
,
533 [EREMCHG
] = TARGET_EREMCHG
,
534 [ELIBACC
] = TARGET_ELIBACC
,
535 [ELIBBAD
] = TARGET_ELIBBAD
,
536 [ELIBSCN
] = TARGET_ELIBSCN
,
537 [ELIBMAX
] = TARGET_ELIBMAX
,
538 [ELIBEXEC
] = TARGET_ELIBEXEC
,
539 [EILSEQ
] = TARGET_EILSEQ
,
540 [ENOSYS
] = TARGET_ENOSYS
,
541 [ELOOP
] = TARGET_ELOOP
,
542 [ERESTART
] = TARGET_ERESTART
,
543 [ESTRPIPE
] = TARGET_ESTRPIPE
,
544 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
545 [EUSERS
] = TARGET_EUSERS
,
546 [ENOTSOCK
] = TARGET_ENOTSOCK
,
547 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
548 [EMSGSIZE
] = TARGET_EMSGSIZE
,
549 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
550 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
551 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
552 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
553 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
554 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
555 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
556 [EADDRINUSE
] = TARGET_EADDRINUSE
,
557 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
558 [ENETDOWN
] = TARGET_ENETDOWN
,
559 [ENETUNREACH
] = TARGET_ENETUNREACH
,
560 [ENETRESET
] = TARGET_ENETRESET
,
561 [ECONNABORTED
] = TARGET_ECONNABORTED
,
562 [ECONNRESET
] = TARGET_ECONNRESET
,
563 [ENOBUFS
] = TARGET_ENOBUFS
,
564 [EISCONN
] = TARGET_EISCONN
,
565 [ENOTCONN
] = TARGET_ENOTCONN
,
566 [EUCLEAN
] = TARGET_EUCLEAN
,
567 [ENOTNAM
] = TARGET_ENOTNAM
,
568 [ENAVAIL
] = TARGET_ENAVAIL
,
569 [EISNAM
] = TARGET_EISNAM
,
570 [EREMOTEIO
] = TARGET_EREMOTEIO
,
571 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
572 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
573 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
574 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
575 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
576 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
577 [EALREADY
] = TARGET_EALREADY
,
578 [EINPROGRESS
] = TARGET_EINPROGRESS
,
579 [ESTALE
] = TARGET_ESTALE
,
580 [ECANCELED
] = TARGET_ECANCELED
,
581 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
582 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
584 [ENOKEY
] = TARGET_ENOKEY
,
587 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
590 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
593 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
596 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
598 #ifdef ENOTRECOVERABLE
599 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
603 static inline int host_to_target_errno(int err
)
605 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
606 host_to_target_errno_table
[err
]) {
607 return host_to_target_errno_table
[err
];
612 static inline int target_to_host_errno(int err
)
614 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
615 target_to_host_errno_table
[err
]) {
616 return target_to_host_errno_table
[err
];
621 static inline abi_long
get_errno(abi_long ret
)
624 return -host_to_target_errno(errno
);
629 static inline int is_error(abi_long ret
)
631 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
634 char *target_strerror(int err
)
636 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
639 return strerror(target_to_host_errno(err
));
642 #define safe_syscall0(type, name) \
643 static type safe_##name(void) \
645 return safe_syscall(__NR_##name); \
648 #define safe_syscall1(type, name, type1, arg1) \
649 static type safe_##name(type1 arg1) \
651 return safe_syscall(__NR_##name, arg1); \
654 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
655 static type safe_##name(type1 arg1, type2 arg2) \
657 return safe_syscall(__NR_##name, arg1, arg2); \
660 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
661 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
663 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
666 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
668 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
670 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
673 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
674 type4, arg4, type5, arg5) \
675 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
678 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
681 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
682 type4, arg4, type5, arg5, type6, arg6) \
683 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
684 type5 arg5, type6 arg6) \
686 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
689 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
690 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
691 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
692 int, flags
, mode_t
, mode
)
693 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
694 struct rusage
*, rusage
)
695 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
696 int, options
, struct rusage
*, rusage
)
697 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
698 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
699 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
700 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
701 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
703 static inline int host_to_target_sock_type(int host_type
)
707 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
709 target_type
= TARGET_SOCK_DGRAM
;
712 target_type
= TARGET_SOCK_STREAM
;
715 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
719 #if defined(SOCK_CLOEXEC)
720 if (host_type
& SOCK_CLOEXEC
) {
721 target_type
|= TARGET_SOCK_CLOEXEC
;
725 #if defined(SOCK_NONBLOCK)
726 if (host_type
& SOCK_NONBLOCK
) {
727 target_type
|= TARGET_SOCK_NONBLOCK
;
734 static abi_ulong target_brk
;
735 static abi_ulong target_original_brk
;
736 static abi_ulong brk_page
;
738 void target_set_brk(abi_ulong new_brk
)
740 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
741 brk_page
= HOST_PAGE_ALIGN(target_brk
);
744 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
745 #define DEBUGF_BRK(message, args...)
747 /* do_brk() must return target values and target errnos. */
748 abi_long
do_brk(abi_ulong new_brk
)
750 abi_long mapped_addr
;
753 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
756 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
759 if (new_brk
< target_original_brk
) {
760 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
765 /* If the new brk is less than the highest page reserved to the
766 * target heap allocation, set it and we're almost done... */
767 if (new_brk
<= brk_page
) {
768 /* Heap contents are initialized to zero, as for anonymous
770 if (new_brk
> target_brk
) {
771 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
773 target_brk
= new_brk
;
774 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
778 /* We need to allocate more memory after the brk... Note that
779 * we don't use MAP_FIXED because that will map over the top of
780 * any existing mapping (like the one with the host libc or qemu
781 * itself); instead we treat "mapped but at wrong address" as
782 * a failure and unmap again.
784 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
785 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
786 PROT_READ
|PROT_WRITE
,
787 MAP_ANON
|MAP_PRIVATE
, 0, 0));
789 if (mapped_addr
== brk_page
) {
790 /* Heap contents are initialized to zero, as for anonymous
791 * mapped pages. Technically the new pages are already
792 * initialized to zero since they *are* anonymous mapped
793 * pages, however we have to take care with the contents that
794 * come from the remaining part of the previous page: it may
795 * contains garbage data due to a previous heap usage (grown
797 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
799 target_brk
= new_brk
;
800 brk_page
= HOST_PAGE_ALIGN(target_brk
);
801 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
804 } else if (mapped_addr
!= -1) {
805 /* Mapped but at wrong address, meaning there wasn't actually
806 * enough space for this brk.
808 target_munmap(mapped_addr
, new_alloc_size
);
810 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
813 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
816 #if defined(TARGET_ALPHA)
817 /* We (partially) emulate OSF/1 on Alpha, which requires we
818 return a proper errno, not an unchanged brk value. */
819 return -TARGET_ENOMEM
;
821 /* For everything else, return the previous break. */
825 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
826 abi_ulong target_fds_addr
,
830 abi_ulong b
, *target_fds
;
832 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
833 if (!(target_fds
= lock_user(VERIFY_READ
,
835 sizeof(abi_ulong
) * nw
,
837 return -TARGET_EFAULT
;
841 for (i
= 0; i
< nw
; i
++) {
842 /* grab the abi_ulong */
843 __get_user(b
, &target_fds
[i
]);
844 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
845 /* check the bit inside the abi_ulong */
852 unlock_user(target_fds
, target_fds_addr
, 0);
857 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
858 abi_ulong target_fds_addr
,
861 if (target_fds_addr
) {
862 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
863 return -TARGET_EFAULT
;
871 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
877 abi_ulong
*target_fds
;
879 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
880 if (!(target_fds
= lock_user(VERIFY_WRITE
,
882 sizeof(abi_ulong
) * nw
,
884 return -TARGET_EFAULT
;
887 for (i
= 0; i
< nw
; i
++) {
889 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
890 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
893 __put_user(v
, &target_fds
[i
]);
896 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
901 #if defined(__alpha__)
907 static inline abi_long
host_to_target_clock_t(long ticks
)
909 #if HOST_HZ == TARGET_HZ
912 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
916 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
917 const struct rusage
*rusage
)
919 struct target_rusage
*target_rusage
;
921 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
922 return -TARGET_EFAULT
;
923 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
924 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
925 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
926 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
927 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
928 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
929 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
930 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
931 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
932 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
933 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
934 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
935 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
936 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
937 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
938 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
939 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
940 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
941 unlock_user_struct(target_rusage
, target_addr
, 1);
946 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
948 abi_ulong target_rlim_swap
;
951 target_rlim_swap
= tswapal(target_rlim
);
952 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
953 return RLIM_INFINITY
;
955 result
= target_rlim_swap
;
956 if (target_rlim_swap
!= (rlim_t
)result
)
957 return RLIM_INFINITY
;
962 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
964 abi_ulong target_rlim_swap
;
967 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
968 target_rlim_swap
= TARGET_RLIM_INFINITY
;
970 target_rlim_swap
= rlim
;
971 result
= tswapal(target_rlim_swap
);
976 static inline int target_to_host_resource(int code
)
979 case TARGET_RLIMIT_AS
:
981 case TARGET_RLIMIT_CORE
:
983 case TARGET_RLIMIT_CPU
:
985 case TARGET_RLIMIT_DATA
:
987 case TARGET_RLIMIT_FSIZE
:
989 case TARGET_RLIMIT_LOCKS
:
991 case TARGET_RLIMIT_MEMLOCK
:
992 return RLIMIT_MEMLOCK
;
993 case TARGET_RLIMIT_MSGQUEUE
:
994 return RLIMIT_MSGQUEUE
;
995 case TARGET_RLIMIT_NICE
:
997 case TARGET_RLIMIT_NOFILE
:
998 return RLIMIT_NOFILE
;
999 case TARGET_RLIMIT_NPROC
:
1000 return RLIMIT_NPROC
;
1001 case TARGET_RLIMIT_RSS
:
1003 case TARGET_RLIMIT_RTPRIO
:
1004 return RLIMIT_RTPRIO
;
1005 case TARGET_RLIMIT_SIGPENDING
:
1006 return RLIMIT_SIGPENDING
;
1007 case TARGET_RLIMIT_STACK
:
1008 return RLIMIT_STACK
;
1014 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1015 abi_ulong target_tv_addr
)
1017 struct target_timeval
*target_tv
;
1019 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
1020 return -TARGET_EFAULT
;
1022 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1023 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1025 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1030 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1031 const struct timeval
*tv
)
1033 struct target_timeval
*target_tv
;
1035 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
1036 return -TARGET_EFAULT
;
1038 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1039 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1041 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1046 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1047 abi_ulong target_tz_addr
)
1049 struct target_timezone
*target_tz
;
1051 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1052 return -TARGET_EFAULT
;
1055 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1056 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1058 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1063 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1066 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1067 abi_ulong target_mq_attr_addr
)
1069 struct target_mq_attr
*target_mq_attr
;
1071 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1072 target_mq_attr_addr
, 1))
1073 return -TARGET_EFAULT
;
1075 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1076 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1077 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1078 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1080 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1085 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1086 const struct mq_attr
*attr
)
1088 struct target_mq_attr
*target_mq_attr
;
1090 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1091 target_mq_attr_addr
, 0))
1092 return -TARGET_EFAULT
;
1094 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1095 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1096 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1097 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1099 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1105 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1106 /* do_select() must return target values and target errnos. */
1107 static abi_long
do_select(int n
,
1108 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1109 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1111 fd_set rfds
, wfds
, efds
;
1112 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1114 struct timespec ts
, *ts_ptr
;
1117 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1121 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1125 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1130 if (target_tv_addr
) {
1131 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1132 return -TARGET_EFAULT
;
1133 ts
.tv_sec
= tv
.tv_sec
;
1134 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1140 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1143 if (!is_error(ret
)) {
1144 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1145 return -TARGET_EFAULT
;
1146 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1147 return -TARGET_EFAULT
;
1148 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1149 return -TARGET_EFAULT
;
1151 if (target_tv_addr
) {
1152 tv
.tv_sec
= ts
.tv_sec
;
1153 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1154 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1155 return -TARGET_EFAULT
;
1164 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1167 return pipe2(host_pipe
, flags
);
1173 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1174 int flags
, int is_pipe2
)
1178 ret
= flags ?
do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1181 return get_errno(ret
);
1183 /* Several targets have special calling conventions for the original
1184 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1186 #if defined(TARGET_ALPHA)
1187 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1188 return host_pipe
[0];
1189 #elif defined(TARGET_MIPS)
1190 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1191 return host_pipe
[0];
1192 #elif defined(TARGET_SH4)
1193 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1194 return host_pipe
[0];
1195 #elif defined(TARGET_SPARC)
1196 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1197 return host_pipe
[0];
1201 if (put_user_s32(host_pipe
[0], pipedes
)
1202 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1203 return -TARGET_EFAULT
;
1204 return get_errno(ret
);
1207 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1208 abi_ulong target_addr
,
1211 struct target_ip_mreqn
*target_smreqn
;
1213 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1215 return -TARGET_EFAULT
;
1216 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1217 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1218 if (len
== sizeof(struct target_ip_mreqn
))
1219 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1220 unlock_user(target_smreqn
, target_addr
, 0);
1225 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1226 abi_ulong target_addr
,
1229 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1230 sa_family_t sa_family
;
1231 struct target_sockaddr
*target_saddr
;
1233 if (fd_trans_target_to_host_addr(fd
)) {
1234 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1237 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1239 return -TARGET_EFAULT
;
1241 sa_family
= tswap16(target_saddr
->sa_family
);
1243 /* Oops. The caller might send a incomplete sun_path; sun_path
1244 * must be terminated by \0 (see the manual page), but
1245 * unfortunately it is quite common to specify sockaddr_un
1246 * length as "strlen(x->sun_path)" while it should be
1247 * "strlen(...) + 1". We'll fix that here if needed.
1248 * Linux kernel has a similar feature.
1251 if (sa_family
== AF_UNIX
) {
1252 if (len
< unix_maxlen
&& len
> 0) {
1253 char *cp
= (char*)target_saddr
;
1255 if ( cp
[len
-1] && !cp
[len
] )
1258 if (len
> unix_maxlen
)
1262 memcpy(addr
, target_saddr
, len
);
1263 addr
->sa_family
= sa_family
;
1264 if (sa_family
== AF_PACKET
) {
1265 struct target_sockaddr_ll
*lladdr
;
1267 lladdr
= (struct target_sockaddr_ll
*)addr
;
1268 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1269 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1271 unlock_user(target_saddr
, target_addr
, 0);
1276 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1277 struct sockaddr
*addr
,
1280 struct target_sockaddr
*target_saddr
;
1282 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1284 return -TARGET_EFAULT
;
1285 memcpy(target_saddr
, addr
, len
);
1286 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1287 unlock_user(target_saddr
, target_addr
, len
);
1292 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1293 struct target_msghdr
*target_msgh
)
1295 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1296 abi_long msg_controllen
;
1297 abi_ulong target_cmsg_addr
;
1298 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1299 socklen_t space
= 0;
1301 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1302 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1304 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1305 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1306 target_cmsg_start
= target_cmsg
;
1308 return -TARGET_EFAULT
;
1310 while (cmsg
&& target_cmsg
) {
1311 void *data
= CMSG_DATA(cmsg
);
1312 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1314 int len
= tswapal(target_cmsg
->cmsg_len
)
1315 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr
));
1317 space
+= CMSG_SPACE(len
);
1318 if (space
> msgh
->msg_controllen
) {
1319 space
-= CMSG_SPACE(len
);
1320 /* This is a QEMU bug, since we allocated the payload
1321 * area ourselves (unlike overflow in host-to-target
1322 * conversion, which is just the guest giving us a buffer
1323 * that's too small). It can't happen for the payload types
1324 * we currently support; if it becomes an issue in future
1325 * we would need to improve our allocation strategy to
1326 * something more intelligent than "twice the size of the
1327 * target buffer we're reading from".
1329 gemu_log("Host cmsg overflow\n");
1333 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1334 cmsg
->cmsg_level
= SOL_SOCKET
;
1336 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1338 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1339 cmsg
->cmsg_len
= CMSG_LEN(len
);
1341 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1342 int *fd
= (int *)data
;
1343 int *target_fd
= (int *)target_data
;
1344 int i
, numfds
= len
/ sizeof(int);
1346 for (i
= 0; i
< numfds
; i
++) {
1347 __get_user(fd
[i
], target_fd
+ i
);
1349 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1350 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1351 struct ucred
*cred
= (struct ucred
*)data
;
1352 struct target_ucred
*target_cred
=
1353 (struct target_ucred
*)target_data
;
1355 __get_user(cred
->pid
, &target_cred
->pid
);
1356 __get_user(cred
->uid
, &target_cred
->uid
);
1357 __get_user(cred
->gid
, &target_cred
->gid
);
1359 gemu_log("Unsupported ancillary data: %d/%d\n",
1360 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1361 memcpy(data
, target_data
, len
);
1364 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1365 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1368 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1370 msgh
->msg_controllen
= space
;
1374 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1375 struct msghdr
*msgh
)
1377 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1378 abi_long msg_controllen
;
1379 abi_ulong target_cmsg_addr
;
1380 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1381 socklen_t space
= 0;
1383 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1384 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1386 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1387 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1388 target_cmsg_start
= target_cmsg
;
1390 return -TARGET_EFAULT
;
1392 while (cmsg
&& target_cmsg
) {
1393 void *data
= CMSG_DATA(cmsg
);
1394 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1396 int len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof (struct cmsghdr
));
1397 int tgt_len
, tgt_space
;
1399 /* We never copy a half-header but may copy half-data;
1400 * this is Linux's behaviour in put_cmsg(). Note that
1401 * truncation here is a guest problem (which we report
1402 * to the guest via the CTRUNC bit), unlike truncation
1403 * in target_to_host_cmsg, which is a QEMU bug.
1405 if (msg_controllen
< sizeof(struct cmsghdr
)) {
1406 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1410 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1411 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1413 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1415 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1417 tgt_len
= TARGET_CMSG_LEN(len
);
1419 /* Payload types which need a different size of payload on
1420 * the target must adjust tgt_len here.
1422 switch (cmsg
->cmsg_level
) {
1424 switch (cmsg
->cmsg_type
) {
1426 tgt_len
= sizeof(struct target_timeval
);
1435 if (msg_controllen
< tgt_len
) {
1436 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1437 tgt_len
= msg_controllen
;
1440 /* We must now copy-and-convert len bytes of payload
1441 * into tgt_len bytes of destination space. Bear in mind
1442 * that in both source and destination we may be dealing
1443 * with a truncated value!
1445 switch (cmsg
->cmsg_level
) {
1447 switch (cmsg
->cmsg_type
) {
1450 int *fd
= (int *)data
;
1451 int *target_fd
= (int *)target_data
;
1452 int i
, numfds
= tgt_len
/ sizeof(int);
1454 for (i
= 0; i
< numfds
; i
++) {
1455 __put_user(fd
[i
], target_fd
+ i
);
1461 struct timeval
*tv
= (struct timeval
*)data
;
1462 struct target_timeval
*target_tv
=
1463 (struct target_timeval
*)target_data
;
1465 if (len
!= sizeof(struct timeval
) ||
1466 tgt_len
!= sizeof(struct target_timeval
)) {
1470 /* copy struct timeval to target */
1471 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1472 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1475 case SCM_CREDENTIALS
:
1477 struct ucred
*cred
= (struct ucred
*)data
;
1478 struct target_ucred
*target_cred
=
1479 (struct target_ucred
*)target_data
;
1481 __put_user(cred
->pid
, &target_cred
->pid
);
1482 __put_user(cred
->uid
, &target_cred
->uid
);
1483 __put_user(cred
->gid
, &target_cred
->gid
);
1493 gemu_log("Unsupported ancillary data: %d/%d\n",
1494 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1495 memcpy(target_data
, data
, MIN(len
, tgt_len
));
1496 if (tgt_len
> len
) {
1497 memset(target_data
+ len
, 0, tgt_len
- len
);
1501 target_cmsg
->cmsg_len
= tswapal(tgt_len
);
1502 tgt_space
= TARGET_CMSG_SPACE(len
);
1503 if (msg_controllen
< tgt_space
) {
1504 tgt_space
= msg_controllen
;
1506 msg_controllen
-= tgt_space
;
1508 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1509 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1512 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1514 target_msgh
->msg_controllen
= tswapal(space
);
1518 /* do_setsockopt() Must return target values and target errnos. */
1519 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1520 abi_ulong optval_addr
, socklen_t optlen
)
1524 struct ip_mreqn
*ip_mreq
;
1525 struct ip_mreq_source
*ip_mreq_source
;
1529 /* TCP options all take an 'int' value. */
1530 if (optlen
< sizeof(uint32_t))
1531 return -TARGET_EINVAL
;
1533 if (get_user_u32(val
, optval_addr
))
1534 return -TARGET_EFAULT
;
1535 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1542 case IP_ROUTER_ALERT
:
1546 case IP_MTU_DISCOVER
:
1552 case IP_MULTICAST_TTL
:
1553 case IP_MULTICAST_LOOP
:
1555 if (optlen
>= sizeof(uint32_t)) {
1556 if (get_user_u32(val
, optval_addr
))
1557 return -TARGET_EFAULT
;
1558 } else if (optlen
>= 1) {
1559 if (get_user_u8(val
, optval_addr
))
1560 return -TARGET_EFAULT
;
1562 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1564 case IP_ADD_MEMBERSHIP
:
1565 case IP_DROP_MEMBERSHIP
:
1566 if (optlen
< sizeof (struct target_ip_mreq
) ||
1567 optlen
> sizeof (struct target_ip_mreqn
))
1568 return -TARGET_EINVAL
;
1570 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1571 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1572 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1575 case IP_BLOCK_SOURCE
:
1576 case IP_UNBLOCK_SOURCE
:
1577 case IP_ADD_SOURCE_MEMBERSHIP
:
1578 case IP_DROP_SOURCE_MEMBERSHIP
:
1579 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1580 return -TARGET_EINVAL
;
1582 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1583 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1584 unlock_user (ip_mreq_source
, optval_addr
, 0);
1593 case IPV6_MTU_DISCOVER
:
1596 case IPV6_RECVPKTINFO
:
1598 if (optlen
< sizeof(uint32_t)) {
1599 return -TARGET_EINVAL
;
1601 if (get_user_u32(val
, optval_addr
)) {
1602 return -TARGET_EFAULT
;
1604 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1605 &val
, sizeof(val
)));
1614 /* struct icmp_filter takes an u32 value */
1615 if (optlen
< sizeof(uint32_t)) {
1616 return -TARGET_EINVAL
;
1619 if (get_user_u32(val
, optval_addr
)) {
1620 return -TARGET_EFAULT
;
1622 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1623 &val
, sizeof(val
)));
1630 case TARGET_SOL_SOCKET
:
1632 case TARGET_SO_RCVTIMEO
:
1636 optname
= SO_RCVTIMEO
;
1639 if (optlen
!= sizeof(struct target_timeval
)) {
1640 return -TARGET_EINVAL
;
1643 if (copy_from_user_timeval(&tv
, optval_addr
)) {
1644 return -TARGET_EFAULT
;
1647 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
1651 case TARGET_SO_SNDTIMEO
:
1652 optname
= SO_SNDTIMEO
;
1654 case TARGET_SO_ATTACH_FILTER
:
1656 struct target_sock_fprog
*tfprog
;
1657 struct target_sock_filter
*tfilter
;
1658 struct sock_fprog fprog
;
1659 struct sock_filter
*filter
;
1662 if (optlen
!= sizeof(*tfprog
)) {
1663 return -TARGET_EINVAL
;
1665 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
1666 return -TARGET_EFAULT
;
1668 if (!lock_user_struct(VERIFY_READ
, tfilter
,
1669 tswapal(tfprog
->filter
), 0)) {
1670 unlock_user_struct(tfprog
, optval_addr
, 1);
1671 return -TARGET_EFAULT
;
1674 fprog
.len
= tswap16(tfprog
->len
);
1675 filter
= g_try_new(struct sock_filter
, fprog
.len
);
1676 if (filter
== NULL
) {
1677 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1678 unlock_user_struct(tfprog
, optval_addr
, 1);
1679 return -TARGET_ENOMEM
;
1681 for (i
= 0; i
< fprog
.len
; i
++) {
1682 filter
[i
].code
= tswap16(tfilter
[i
].code
);
1683 filter
[i
].jt
= tfilter
[i
].jt
;
1684 filter
[i
].jf
= tfilter
[i
].jf
;
1685 filter
[i
].k
= tswap32(tfilter
[i
].k
);
1687 fprog
.filter
= filter
;
1689 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
1690 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
1693 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1694 unlock_user_struct(tfprog
, optval_addr
, 1);
1697 case TARGET_SO_BINDTODEVICE
:
1699 char *dev_ifname
, *addr_ifname
;
1701 if (optlen
> IFNAMSIZ
- 1) {
1702 optlen
= IFNAMSIZ
- 1;
1704 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1706 return -TARGET_EFAULT
;
1708 optname
= SO_BINDTODEVICE
;
1709 addr_ifname
= alloca(IFNAMSIZ
);
1710 memcpy(addr_ifname
, dev_ifname
, optlen
);
1711 addr_ifname
[optlen
] = 0;
1712 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
1713 addr_ifname
, optlen
));
1714 unlock_user (dev_ifname
, optval_addr
, 0);
1717 /* Options with 'int' argument. */
1718 case TARGET_SO_DEBUG
:
1721 case TARGET_SO_REUSEADDR
:
1722 optname
= SO_REUSEADDR
;
1724 case TARGET_SO_TYPE
:
1727 case TARGET_SO_ERROR
:
1730 case TARGET_SO_DONTROUTE
:
1731 optname
= SO_DONTROUTE
;
1733 case TARGET_SO_BROADCAST
:
1734 optname
= SO_BROADCAST
;
1736 case TARGET_SO_SNDBUF
:
1737 optname
= SO_SNDBUF
;
1739 case TARGET_SO_SNDBUFFORCE
:
1740 optname
= SO_SNDBUFFORCE
;
1742 case TARGET_SO_RCVBUF
:
1743 optname
= SO_RCVBUF
;
1745 case TARGET_SO_RCVBUFFORCE
:
1746 optname
= SO_RCVBUFFORCE
;
1748 case TARGET_SO_KEEPALIVE
:
1749 optname
= SO_KEEPALIVE
;
1751 case TARGET_SO_OOBINLINE
:
1752 optname
= SO_OOBINLINE
;
1754 case TARGET_SO_NO_CHECK
:
1755 optname
= SO_NO_CHECK
;
1757 case TARGET_SO_PRIORITY
:
1758 optname
= SO_PRIORITY
;
1761 case TARGET_SO_BSDCOMPAT
:
1762 optname
= SO_BSDCOMPAT
;
1765 case TARGET_SO_PASSCRED
:
1766 optname
= SO_PASSCRED
;
1768 case TARGET_SO_PASSSEC
:
1769 optname
= SO_PASSSEC
;
1771 case TARGET_SO_TIMESTAMP
:
1772 optname
= SO_TIMESTAMP
;
1774 case TARGET_SO_RCVLOWAT
:
1775 optname
= SO_RCVLOWAT
;
1781 if (optlen
< sizeof(uint32_t))
1782 return -TARGET_EINVAL
;
1784 if (get_user_u32(val
, optval_addr
))
1785 return -TARGET_EFAULT
;
1786 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
1790 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level
, optname
);
1791 ret
= -TARGET_ENOPROTOOPT
;
1796 /* do_getsockopt() Must return target values and target errnos. */
1797 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
1798 abi_ulong optval_addr
, abi_ulong optlen
)
1805 case TARGET_SOL_SOCKET
:
1808 /* These don't just return a single integer */
1809 case TARGET_SO_LINGER
:
1810 case TARGET_SO_RCVTIMEO
:
1811 case TARGET_SO_SNDTIMEO
:
1812 case TARGET_SO_PEERNAME
:
1814 case TARGET_SO_PEERCRED
: {
1817 struct target_ucred
*tcr
;
1819 if (get_user_u32(len
, optlen
)) {
1820 return -TARGET_EFAULT
;
1823 return -TARGET_EINVAL
;
1827 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
1835 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
1836 return -TARGET_EFAULT
;
1838 __put_user(cr
.pid
, &tcr
->pid
);
1839 __put_user(cr
.uid
, &tcr
->uid
);
1840 __put_user(cr
.gid
, &tcr
->gid
);
1841 unlock_user_struct(tcr
, optval_addr
, 1);
1842 if (put_user_u32(len
, optlen
)) {
1843 return -TARGET_EFAULT
;
1847 /* Options with 'int' argument. */
1848 case TARGET_SO_DEBUG
:
1851 case TARGET_SO_REUSEADDR
:
1852 optname
= SO_REUSEADDR
;
1854 case TARGET_SO_TYPE
:
1857 case TARGET_SO_ERROR
:
1860 case TARGET_SO_DONTROUTE
:
1861 optname
= SO_DONTROUTE
;
1863 case TARGET_SO_BROADCAST
:
1864 optname
= SO_BROADCAST
;
1866 case TARGET_SO_SNDBUF
:
1867 optname
= SO_SNDBUF
;
1869 case TARGET_SO_RCVBUF
:
1870 optname
= SO_RCVBUF
;
1872 case TARGET_SO_KEEPALIVE
:
1873 optname
= SO_KEEPALIVE
;
1875 case TARGET_SO_OOBINLINE
:
1876 optname
= SO_OOBINLINE
;
1878 case TARGET_SO_NO_CHECK
:
1879 optname
= SO_NO_CHECK
;
1881 case TARGET_SO_PRIORITY
:
1882 optname
= SO_PRIORITY
;
1885 case TARGET_SO_BSDCOMPAT
:
1886 optname
= SO_BSDCOMPAT
;
1889 case TARGET_SO_PASSCRED
:
1890 optname
= SO_PASSCRED
;
1892 case TARGET_SO_TIMESTAMP
:
1893 optname
= SO_TIMESTAMP
;
1895 case TARGET_SO_RCVLOWAT
:
1896 optname
= SO_RCVLOWAT
;
1898 case TARGET_SO_ACCEPTCONN
:
1899 optname
= SO_ACCEPTCONN
;
1906 /* TCP options all take an 'int' value. */
1908 if (get_user_u32(len
, optlen
))
1909 return -TARGET_EFAULT
;
1911 return -TARGET_EINVAL
;
1913 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1916 if (optname
== SO_TYPE
) {
1917 val
= host_to_target_sock_type(val
);
1922 if (put_user_u32(val
, optval_addr
))
1923 return -TARGET_EFAULT
;
1925 if (put_user_u8(val
, optval_addr
))
1926 return -TARGET_EFAULT
;
1928 if (put_user_u32(len
, optlen
))
1929 return -TARGET_EFAULT
;
1936 case IP_ROUTER_ALERT
:
1940 case IP_MTU_DISCOVER
:
1946 case IP_MULTICAST_TTL
:
1947 case IP_MULTICAST_LOOP
:
1948 if (get_user_u32(len
, optlen
))
1949 return -TARGET_EFAULT
;
1951 return -TARGET_EINVAL
;
1953 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1956 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
1958 if (put_user_u32(len
, optlen
)
1959 || put_user_u8(val
, optval_addr
))
1960 return -TARGET_EFAULT
;
1962 if (len
> sizeof(int))
1964 if (put_user_u32(len
, optlen
)
1965 || put_user_u32(val
, optval_addr
))
1966 return -TARGET_EFAULT
;
1970 ret
= -TARGET_ENOPROTOOPT
;
1976 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1978 ret
= -TARGET_EOPNOTSUPP
;
1984 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
1985 int count
, int copy
)
1987 struct target_iovec
*target_vec
;
1989 abi_ulong total_len
, max_len
;
1992 bool bad_address
= false;
1998 if (count
< 0 || count
> IOV_MAX
) {
2003 vec
= g_try_new0(struct iovec
, count
);
2009 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2010 count
* sizeof(struct target_iovec
), 1);
2011 if (target_vec
== NULL
) {
2016 /* ??? If host page size > target page size, this will result in a
2017 value larger than what we can actually support. */
2018 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
2021 for (i
= 0; i
< count
; i
++) {
2022 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2023 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2028 } else if (len
== 0) {
2029 /* Zero length pointer is ignored. */
2030 vec
[i
].iov_base
= 0;
2032 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
2033 /* If the first buffer pointer is bad, this is a fault. But
2034 * subsequent bad buffers will result in a partial write; this
2035 * is realized by filling the vector with null pointers and
2037 if (!vec
[i
].iov_base
) {
2048 if (len
> max_len
- total_len
) {
2049 len
= max_len
- total_len
;
2052 vec
[i
].iov_len
= len
;
2056 unlock_user(target_vec
, target_addr
, 0);
2061 if (tswapal(target_vec
[i
].iov_len
) > 0) {
2062 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
2065 unlock_user(target_vec
, target_addr
, 0);
2072 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
2073 int count
, int copy
)
2075 struct target_iovec
*target_vec
;
2078 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2079 count
* sizeof(struct target_iovec
), 1);
2081 for (i
= 0; i
< count
; i
++) {
2082 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2083 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2087 unlock_user(vec
[i
].iov_base
, base
, copy ? vec
[i
].iov_len
: 0);
2089 unlock_user(target_vec
, target_addr
, 0);
2095 static inline int target_to_host_sock_type(int *type
)
2098 int target_type
= *type
;
2100 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
2101 case TARGET_SOCK_DGRAM
:
2102 host_type
= SOCK_DGRAM
;
2104 case TARGET_SOCK_STREAM
:
2105 host_type
= SOCK_STREAM
;
2108 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
2111 if (target_type
& TARGET_SOCK_CLOEXEC
) {
2112 #if defined(SOCK_CLOEXEC)
2113 host_type
|= SOCK_CLOEXEC
;
2115 return -TARGET_EINVAL
;
2118 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2119 #if defined(SOCK_NONBLOCK)
2120 host_type
|= SOCK_NONBLOCK
;
2121 #elif !defined(O_NONBLOCK)
2122 return -TARGET_EINVAL
;
2129 /* Try to emulate socket type flags after socket creation. */
2130 static int sock_flags_fixup(int fd
, int target_type
)
2132 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2133 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2134 int flags
= fcntl(fd
, F_GETFL
);
2135 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
2137 return -TARGET_EINVAL
;
2144 static abi_long
packet_target_to_host_sockaddr(void *host_addr
,
2145 abi_ulong target_addr
,
2148 struct sockaddr
*addr
= host_addr
;
2149 struct target_sockaddr
*target_saddr
;
2151 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
2152 if (!target_saddr
) {
2153 return -TARGET_EFAULT
;
2156 memcpy(addr
, target_saddr
, len
);
2157 addr
->sa_family
= tswap16(target_saddr
->sa_family
);
2158 /* spkt_protocol is big-endian */
2160 unlock_user(target_saddr
, target_addr
, 0);
2164 static TargetFdTrans target_packet_trans
= {
2165 .target_to_host_addr
= packet_target_to_host_sockaddr
,
2168 /* do_socket() Must return target values and target errnos. */
2169 static abi_long
do_socket(int domain
, int type
, int protocol
)
2171 int target_type
= type
;
2174 ret
= target_to_host_sock_type(&type
);
2179 if (domain
== PF_NETLINK
)
2180 return -TARGET_EAFNOSUPPORT
;
2182 if (domain
== AF_PACKET
||
2183 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
2184 protocol
= tswap16(protocol
);
2187 ret
= get_errno(socket(domain
, type
, protocol
));
2189 ret
= sock_flags_fixup(ret
, target_type
);
2190 if (type
== SOCK_PACKET
) {
2191 /* Manage an obsolete case :
2192 * if socket type is SOCK_PACKET, bind by name
2194 fd_trans_register(ret
, &target_packet_trans
);
2200 /* do_bind() Must return target values and target errnos. */
2201 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
2207 if ((int)addrlen
< 0) {
2208 return -TARGET_EINVAL
;
2211 addr
= alloca(addrlen
+1);
2213 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
2217 return get_errno(bind(sockfd
, addr
, addrlen
));
2220 /* do_connect() Must return target values and target errnos. */
2221 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
2227 if ((int)addrlen
< 0) {
2228 return -TARGET_EINVAL
;
2231 addr
= alloca(addrlen
+1);
2233 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
2237 return get_errno(connect(sockfd
, addr
, addrlen
));
2240 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
2241 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
2242 int flags
, int send
)
2248 abi_ulong target_vec
;
2250 if (msgp
->msg_name
) {
2251 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
2252 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
2253 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
2254 tswapal(msgp
->msg_name
),
2260 msg
.msg_name
= NULL
;
2261 msg
.msg_namelen
= 0;
2263 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
2264 msg
.msg_control
= alloca(msg
.msg_controllen
);
2265 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
2267 count
= tswapal(msgp
->msg_iovlen
);
2268 target_vec
= tswapal(msgp
->msg_iov
);
2269 vec
= lock_iovec(send ? VERIFY_READ
: VERIFY_WRITE
,
2270 target_vec
, count
, send
);
2272 ret
= -host_to_target_errno(errno
);
2275 msg
.msg_iovlen
= count
;
2279 ret
= target_to_host_cmsg(&msg
, msgp
);
2281 ret
= get_errno(sendmsg(fd
, &msg
, flags
));
2283 ret
= get_errno(recvmsg(fd
, &msg
, flags
));
2284 if (!is_error(ret
)) {
2286 ret
= host_to_target_cmsg(msgp
, &msg
);
2287 if (!is_error(ret
)) {
2288 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
2289 if (msg
.msg_name
!= NULL
) {
2290 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
2291 msg
.msg_name
, msg
.msg_namelen
);
2303 unlock_iovec(vec
, target_vec
, count
, !send
);
2308 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
2309 int flags
, int send
)
2312 struct target_msghdr
*msgp
;
2314 if (!lock_user_struct(send ? VERIFY_READ
: VERIFY_WRITE
,
2318 return -TARGET_EFAULT
;
2320 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
2321 unlock_user_struct(msgp
, target_msg
, send ?
0 : 1);
2325 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
2326 * so it might not have this *mmsg-specific flag either.
2328 #ifndef MSG_WAITFORONE
2329 #define MSG_WAITFORONE 0x10000
2332 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
2333 unsigned int vlen
, unsigned int flags
,
2336 struct target_mmsghdr
*mmsgp
;
2340 if (vlen
> UIO_MAXIOV
) {
2344 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
2346 return -TARGET_EFAULT
;
2349 for (i
= 0; i
< vlen
; i
++) {
2350 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
2351 if (is_error(ret
)) {
2354 mmsgp
[i
].msg_len
= tswap32(ret
);
2355 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2356 if (flags
& MSG_WAITFORONE
) {
2357 flags
|= MSG_DONTWAIT
;
2361 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
2363 /* Return number of datagrams sent if we sent any at all;
2364 * otherwise return the error.
2372 /* If we don't have a system accept4() then just call accept.
2373 * The callsites to do_accept4() will ensure that they don't
2374 * pass a non-zero flags argument in this config.
2376 #ifndef CONFIG_ACCEPT4
2377 static inline int accept4(int sockfd
, struct sockaddr
*addr
,
2378 socklen_t
*addrlen
, int flags
)
2381 return accept(sockfd
, addr
, addrlen
);
2385 /* do_accept4() Must return target values and target errnos. */
2386 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
2387 abi_ulong target_addrlen_addr
, int flags
)
2394 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
2396 if (target_addr
== 0) {
2397 return get_errno(accept4(fd
, NULL
, NULL
, host_flags
));
2400 /* linux returns EINVAL if addrlen pointer is invalid */
2401 if (get_user_u32(addrlen
, target_addrlen_addr
))
2402 return -TARGET_EINVAL
;
2404 if ((int)addrlen
< 0) {
2405 return -TARGET_EINVAL
;
2408 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2409 return -TARGET_EINVAL
;
2411 addr
= alloca(addrlen
);
2413 ret
= get_errno(accept4(fd
, addr
, &addrlen
, host_flags
));
2414 if (!is_error(ret
)) {
2415 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2416 if (put_user_u32(addrlen
, target_addrlen_addr
))
2417 ret
= -TARGET_EFAULT
;
2422 /* do_getpeername() Must return target values and target errnos. */
2423 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
2424 abi_ulong target_addrlen_addr
)
2430 if (get_user_u32(addrlen
, target_addrlen_addr
))
2431 return -TARGET_EFAULT
;
2433 if ((int)addrlen
< 0) {
2434 return -TARGET_EINVAL
;
2437 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2438 return -TARGET_EFAULT
;
2440 addr
= alloca(addrlen
);
2442 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
2443 if (!is_error(ret
)) {
2444 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2445 if (put_user_u32(addrlen
, target_addrlen_addr
))
2446 ret
= -TARGET_EFAULT
;
2451 /* do_getsockname() Must return target values and target errnos. */
2452 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
2453 abi_ulong target_addrlen_addr
)
2459 if (get_user_u32(addrlen
, target_addrlen_addr
))
2460 return -TARGET_EFAULT
;
2462 if ((int)addrlen
< 0) {
2463 return -TARGET_EINVAL
;
2466 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2467 return -TARGET_EFAULT
;
2469 addr
= alloca(addrlen
);
2471 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
2472 if (!is_error(ret
)) {
2473 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2474 if (put_user_u32(addrlen
, target_addrlen_addr
))
2475 ret
= -TARGET_EFAULT
;
2480 /* do_socketpair() Must return target values and target errnos. */
2481 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
2482 abi_ulong target_tab_addr
)
2487 target_to_host_sock_type(&type
);
2489 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
2490 if (!is_error(ret
)) {
2491 if (put_user_s32(tab
[0], target_tab_addr
)
2492 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
2493 ret
= -TARGET_EFAULT
;
2498 /* do_sendto() Must return target values and target errnos. */
2499 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
2500 abi_ulong target_addr
, socklen_t addrlen
)
2506 if ((int)addrlen
< 0) {
2507 return -TARGET_EINVAL
;
2510 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
2512 return -TARGET_EFAULT
;
2514 addr
= alloca(addrlen
+1);
2515 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
2517 unlock_user(host_msg
, msg
, 0);
2520 ret
= get_errno(sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
2522 ret
= get_errno(send(fd
, host_msg
, len
, flags
));
2524 unlock_user(host_msg
, msg
, 0);
2528 /* do_recvfrom() Must return target values and target errnos. */
2529 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
2530 abi_ulong target_addr
,
2531 abi_ulong target_addrlen
)
2538 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
2540 return -TARGET_EFAULT
;
2542 if (get_user_u32(addrlen
, target_addrlen
)) {
2543 ret
= -TARGET_EFAULT
;
2546 if ((int)addrlen
< 0) {
2547 ret
= -TARGET_EINVAL
;
2550 addr
= alloca(addrlen
);
2551 ret
= get_errno(recvfrom(fd
, host_msg
, len
, flags
, addr
, &addrlen
));
2553 addr
= NULL
; /* To keep compiler quiet. */
2554 ret
= get_errno(qemu_recv(fd
, host_msg
, len
, flags
));
2556 if (!is_error(ret
)) {
2558 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2559 if (put_user_u32(addrlen
, target_addrlen
)) {
2560 ret
= -TARGET_EFAULT
;
2564 unlock_user(host_msg
, msg
, len
);
2567 unlock_user(host_msg
, msg
, 0);
2572 #ifdef TARGET_NR_socketcall
2573 /* do_socketcall() Must return target values and target errnos. */
2574 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
2576 static const unsigned ac
[] = { /* number of arguments per call */
2577 [SOCKOP_socket
] = 3, /* domain, type, protocol */
2578 [SOCKOP_bind
] = 3, /* sockfd, addr, addrlen */
2579 [SOCKOP_connect
] = 3, /* sockfd, addr, addrlen */
2580 [SOCKOP_listen
] = 2, /* sockfd, backlog */
2581 [SOCKOP_accept
] = 3, /* sockfd, addr, addrlen */
2582 [SOCKOP_accept4
] = 4, /* sockfd, addr, addrlen, flags */
2583 [SOCKOP_getsockname
] = 3, /* sockfd, addr, addrlen */
2584 [SOCKOP_getpeername
] = 3, /* sockfd, addr, addrlen */
2585 [SOCKOP_socketpair
] = 4, /* domain, type, protocol, tab */
2586 [SOCKOP_send
] = 4, /* sockfd, msg, len, flags */
2587 [SOCKOP_recv
] = 4, /* sockfd, msg, len, flags */
2588 [SOCKOP_sendto
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2589 [SOCKOP_recvfrom
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2590 [SOCKOP_shutdown
] = 2, /* sockfd, how */
2591 [SOCKOP_sendmsg
] = 3, /* sockfd, msg, flags */
2592 [SOCKOP_recvmsg
] = 3, /* sockfd, msg, flags */
2593 [SOCKOP_sendmmsg
] = 4, /* sockfd, msgvec, vlen, flags */
2594 [SOCKOP_recvmmsg
] = 4, /* sockfd, msgvec, vlen, flags */
2595 [SOCKOP_setsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
2596 [SOCKOP_getsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
2598 abi_long a
[6]; /* max 6 args */
2600 /* first, collect the arguments in a[] according to ac[] */
2601 if (num
>= 0 && num
< ARRAY_SIZE(ac
)) {
2603 assert(ARRAY_SIZE(a
) >= ac
[num
]); /* ensure we have space for args */
2604 for (i
= 0; i
< ac
[num
]; ++i
) {
2605 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
2606 return -TARGET_EFAULT
;
2611 /* now when we have the args, actually handle the call */
2613 case SOCKOP_socket
: /* domain, type, protocol */
2614 return do_socket(a
[0], a
[1], a
[2]);
2615 case SOCKOP_bind
: /* sockfd, addr, addrlen */
2616 return do_bind(a
[0], a
[1], a
[2]);
2617 case SOCKOP_connect
: /* sockfd, addr, addrlen */
2618 return do_connect(a
[0], a
[1], a
[2]);
2619 case SOCKOP_listen
: /* sockfd, backlog */
2620 return get_errno(listen(a
[0], a
[1]));
2621 case SOCKOP_accept
: /* sockfd, addr, addrlen */
2622 return do_accept4(a
[0], a
[1], a
[2], 0);
2623 case SOCKOP_accept4
: /* sockfd, addr, addrlen, flags */
2624 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
2625 case SOCKOP_getsockname
: /* sockfd, addr, addrlen */
2626 return do_getsockname(a
[0], a
[1], a
[2]);
2627 case SOCKOP_getpeername
: /* sockfd, addr, addrlen */
2628 return do_getpeername(a
[0], a
[1], a
[2]);
2629 case SOCKOP_socketpair
: /* domain, type, protocol, tab */
2630 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
2631 case SOCKOP_send
: /* sockfd, msg, len, flags */
2632 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
2633 case SOCKOP_recv
: /* sockfd, msg, len, flags */
2634 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
2635 case SOCKOP_sendto
: /* sockfd, msg, len, flags, addr, addrlen */
2636 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
2637 case SOCKOP_recvfrom
: /* sockfd, msg, len, flags, addr, addrlen */
2638 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
2639 case SOCKOP_shutdown
: /* sockfd, how */
2640 return get_errno(shutdown(a
[0], a
[1]));
2641 case SOCKOP_sendmsg
: /* sockfd, msg, flags */
2642 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
2643 case SOCKOP_recvmsg
: /* sockfd, msg, flags */
2644 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
2645 case SOCKOP_sendmmsg
: /* sockfd, msgvec, vlen, flags */
2646 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
2647 case SOCKOP_recvmmsg
: /* sockfd, msgvec, vlen, flags */
2648 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
2649 case SOCKOP_setsockopt
: /* sockfd, level, optname, optval, optlen */
2650 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
2651 case SOCKOP_getsockopt
: /* sockfd, level, optname, optval, optlen */
2652 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
2654 gemu_log("Unsupported socketcall: %d\n", num
);
2655 return -TARGET_ENOSYS
;
2660 #define N_SHM_REGIONS 32
2662 static struct shm_region
{
2666 } shm_regions
[N_SHM_REGIONS
];
2668 struct target_semid_ds
2670 struct target_ipc_perm sem_perm
;
2671 abi_ulong sem_otime
;
2672 #if !defined(TARGET_PPC64)
2673 abi_ulong __unused1
;
2675 abi_ulong sem_ctime
;
2676 #if !defined(TARGET_PPC64)
2677 abi_ulong __unused2
;
2679 abi_ulong sem_nsems
;
2680 abi_ulong __unused3
;
2681 abi_ulong __unused4
;
2684 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
2685 abi_ulong target_addr
)
2687 struct target_ipc_perm
*target_ip
;
2688 struct target_semid_ds
*target_sd
;
2690 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2691 return -TARGET_EFAULT
;
2692 target_ip
= &(target_sd
->sem_perm
);
2693 host_ip
->__key
= tswap32(target_ip
->__key
);
2694 host_ip
->uid
= tswap32(target_ip
->uid
);
2695 host_ip
->gid
= tswap32(target_ip
->gid
);
2696 host_ip
->cuid
= tswap32(target_ip
->cuid
);
2697 host_ip
->cgid
= tswap32(target_ip
->cgid
);
2698 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2699 host_ip
->mode
= tswap32(target_ip
->mode
);
2701 host_ip
->mode
= tswap16(target_ip
->mode
);
2703 #if defined(TARGET_PPC)
2704 host_ip
->__seq
= tswap32(target_ip
->__seq
);
2706 host_ip
->__seq
= tswap16(target_ip
->__seq
);
2708 unlock_user_struct(target_sd
, target_addr
, 0);
2712 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
2713 struct ipc_perm
*host_ip
)
2715 struct target_ipc_perm
*target_ip
;
2716 struct target_semid_ds
*target_sd
;
2718 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2719 return -TARGET_EFAULT
;
2720 target_ip
= &(target_sd
->sem_perm
);
2721 target_ip
->__key
= tswap32(host_ip
->__key
);
2722 target_ip
->uid
= tswap32(host_ip
->uid
);
2723 target_ip
->gid
= tswap32(host_ip
->gid
);
2724 target_ip
->cuid
= tswap32(host_ip
->cuid
);
2725 target_ip
->cgid
= tswap32(host_ip
->cgid
);
2726 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2727 target_ip
->mode
= tswap32(host_ip
->mode
);
2729 target_ip
->mode
= tswap16(host_ip
->mode
);
2731 #if defined(TARGET_PPC)
2732 target_ip
->__seq
= tswap32(host_ip
->__seq
);
2734 target_ip
->__seq
= tswap16(host_ip
->__seq
);
2736 unlock_user_struct(target_sd
, target_addr
, 1);
2740 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
2741 abi_ulong target_addr
)
2743 struct target_semid_ds
*target_sd
;
2745 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2746 return -TARGET_EFAULT
;
2747 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
2748 return -TARGET_EFAULT
;
2749 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
2750 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
2751 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
2752 unlock_user_struct(target_sd
, target_addr
, 0);
2756 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
2757 struct semid_ds
*host_sd
)
2759 struct target_semid_ds
*target_sd
;
2761 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2762 return -TARGET_EFAULT
;
2763 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
2764 return -TARGET_EFAULT
;
2765 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
2766 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
2767 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
2768 unlock_user_struct(target_sd
, target_addr
, 1);
2772 struct target_seminfo
{
2785 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
2786 struct seminfo
*host_seminfo
)
2788 struct target_seminfo
*target_seminfo
;
2789 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
2790 return -TARGET_EFAULT
;
2791 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
2792 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
2793 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
2794 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
2795 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
2796 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
2797 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
2798 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
2799 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
2800 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
2801 unlock_user_struct(target_seminfo
, target_addr
, 1);
2807 struct semid_ds
*buf
;
2808 unsigned short *array
;
2809 struct seminfo
*__buf
;
2812 union target_semun
{
2819 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
2820 abi_ulong target_addr
)
2823 unsigned short *array
;
2825 struct semid_ds semid_ds
;
2828 semun
.buf
= &semid_ds
;
2830 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2832 return get_errno(ret
);
2834 nsems
= semid_ds
.sem_nsems
;
2836 *host_array
= g_try_new(unsigned short, nsems
);
2838 return -TARGET_ENOMEM
;
2840 array
= lock_user(VERIFY_READ
, target_addr
,
2841 nsems
*sizeof(unsigned short), 1);
2843 g_free(*host_array
);
2844 return -TARGET_EFAULT
;
2847 for(i
=0; i
<nsems
; i
++) {
2848 __get_user((*host_array
)[i
], &array
[i
]);
2850 unlock_user(array
, target_addr
, 0);
2855 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
2856 unsigned short **host_array
)
2859 unsigned short *array
;
2861 struct semid_ds semid_ds
;
2864 semun
.buf
= &semid_ds
;
2866 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2868 return get_errno(ret
);
2870 nsems
= semid_ds
.sem_nsems
;
2872 array
= lock_user(VERIFY_WRITE
, target_addr
,
2873 nsems
*sizeof(unsigned short), 0);
2875 return -TARGET_EFAULT
;
2877 for(i
=0; i
<nsems
; i
++) {
2878 __put_user((*host_array
)[i
], &array
[i
]);
2880 g_free(*host_array
);
2881 unlock_user(array
, target_addr
, 1);
2886 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
2887 abi_ulong target_arg
)
2889 union target_semun target_su
= { .buf
= target_arg
};
2891 struct semid_ds dsarg
;
2892 unsigned short *array
= NULL
;
2893 struct seminfo seminfo
;
2894 abi_long ret
= -TARGET_EINVAL
;
2901 /* In 64 bit cross-endian situations, we will erroneously pick up
2902 * the wrong half of the union for the "val" element. To rectify
2903 * this, the entire 8-byte structure is byteswapped, followed by
2904 * a swap of the 4 byte val field. In other cases, the data is
2905 * already in proper host byte order. */
2906 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
2907 target_su
.buf
= tswapal(target_su
.buf
);
2908 arg
.val
= tswap32(target_su
.val
);
2910 arg
.val
= target_su
.val
;
2912 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2916 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
2920 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2921 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
2928 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
2932 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2933 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
2939 arg
.__buf
= &seminfo
;
2940 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2941 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
2949 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
2956 struct target_sembuf
{
2957 unsigned short sem_num
;
2962 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
2963 abi_ulong target_addr
,
2966 struct target_sembuf
*target_sembuf
;
2969 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
2970 nsops
*sizeof(struct target_sembuf
), 1);
2972 return -TARGET_EFAULT
;
2974 for(i
=0; i
<nsops
; i
++) {
2975 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
2976 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
2977 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
2980 unlock_user(target_sembuf
, target_addr
, 0);
2985 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
2987 struct sembuf sops
[nsops
];
2989 if (target_to_host_sembuf(sops
, ptr
, nsops
))
2990 return -TARGET_EFAULT
;
2992 return get_errno(semop(semid
, sops
, nsops
));
2995 struct target_msqid_ds
2997 struct target_ipc_perm msg_perm
;
2998 abi_ulong msg_stime
;
2999 #if TARGET_ABI_BITS == 32
3000 abi_ulong __unused1
;
3002 abi_ulong msg_rtime
;
3003 #if TARGET_ABI_BITS == 32
3004 abi_ulong __unused2
;
3006 abi_ulong msg_ctime
;
3007 #if TARGET_ABI_BITS == 32
3008 abi_ulong __unused3
;
3010 abi_ulong __msg_cbytes
;
3012 abi_ulong msg_qbytes
;
3013 abi_ulong msg_lspid
;
3014 abi_ulong msg_lrpid
;
3015 abi_ulong __unused4
;
3016 abi_ulong __unused5
;
3019 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
3020 abi_ulong target_addr
)
3022 struct target_msqid_ds
*target_md
;
3024 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
3025 return -TARGET_EFAULT
;
3026 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
3027 return -TARGET_EFAULT
;
3028 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
3029 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
3030 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
3031 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
3032 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
3033 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
3034 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
3035 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
3036 unlock_user_struct(target_md
, target_addr
, 0);
3040 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
3041 struct msqid_ds
*host_md
)
3043 struct target_msqid_ds
*target_md
;
3045 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
3046 return -TARGET_EFAULT
;
3047 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
3048 return -TARGET_EFAULT
;
3049 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
3050 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
3051 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
3052 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
3053 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
3054 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
3055 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
3056 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
3057 unlock_user_struct(target_md
, target_addr
, 1);
3061 struct target_msginfo
{
3069 unsigned short int msgseg
;
3072 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
3073 struct msginfo
*host_msginfo
)
3075 struct target_msginfo
*target_msginfo
;
3076 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
3077 return -TARGET_EFAULT
;
3078 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
3079 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
3080 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
3081 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
3082 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
3083 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
3084 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
3085 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
3086 unlock_user_struct(target_msginfo
, target_addr
, 1);
3090 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
3092 struct msqid_ds dsarg
;
3093 struct msginfo msginfo
;
3094 abi_long ret
= -TARGET_EINVAL
;
3102 if (target_to_host_msqid_ds(&dsarg
,ptr
))
3103 return -TARGET_EFAULT
;
3104 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
3105 if (host_to_target_msqid_ds(ptr
,&dsarg
))
3106 return -TARGET_EFAULT
;
3109 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
3113 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
3114 if (host_to_target_msginfo(ptr
, &msginfo
))
3115 return -TARGET_EFAULT
;
3122 struct target_msgbuf
{
3127 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
3128 ssize_t msgsz
, int msgflg
)
3130 struct target_msgbuf
*target_mb
;
3131 struct msgbuf
*host_mb
;
3135 return -TARGET_EINVAL
;
3138 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
3139 return -TARGET_EFAULT
;
3140 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
3142 unlock_user_struct(target_mb
, msgp
, 0);
3143 return -TARGET_ENOMEM
;
3145 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
3146 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
3147 ret
= get_errno(msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
3149 unlock_user_struct(target_mb
, msgp
, 0);
3154 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
3155 ssize_t msgsz
, abi_long msgtyp
,
3158 struct target_msgbuf
*target_mb
;
3160 struct msgbuf
*host_mb
;
3164 return -TARGET_EINVAL
;
3167 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
3168 return -TARGET_EFAULT
;
3170 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
3172 ret
= -TARGET_ENOMEM
;
3175 ret
= get_errno(msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
3178 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
3179 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
3180 if (!target_mtext
) {
3181 ret
= -TARGET_EFAULT
;
3184 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
3185 unlock_user(target_mtext
, target_mtext_addr
, ret
);
3188 target_mb
->mtype
= tswapal(host_mb
->mtype
);
3192 unlock_user_struct(target_mb
, msgp
, 1);
3197 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
3198 abi_ulong target_addr
)
3200 struct target_shmid_ds
*target_sd
;
3202 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3203 return -TARGET_EFAULT
;
3204 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
3205 return -TARGET_EFAULT
;
3206 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
3207 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
3208 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
3209 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
3210 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
3211 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
3212 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
3213 unlock_user_struct(target_sd
, target_addr
, 0);
3217 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
3218 struct shmid_ds
*host_sd
)
3220 struct target_shmid_ds
*target_sd
;
3222 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3223 return -TARGET_EFAULT
;
3224 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
3225 return -TARGET_EFAULT
;
3226 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
3227 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
3228 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
3229 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
3230 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
3231 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
3232 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
3233 unlock_user_struct(target_sd
, target_addr
, 1);
3237 struct target_shminfo
{
3245 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
3246 struct shminfo
*host_shminfo
)
3248 struct target_shminfo
*target_shminfo
;
3249 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
3250 return -TARGET_EFAULT
;
3251 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
3252 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
3253 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
3254 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
3255 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
3256 unlock_user_struct(target_shminfo
, target_addr
, 1);
3260 struct target_shm_info
{
3265 abi_ulong swap_attempts
;
3266 abi_ulong swap_successes
;
3269 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
3270 struct shm_info
*host_shm_info
)
3272 struct target_shm_info
*target_shm_info
;
3273 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
3274 return -TARGET_EFAULT
;
3275 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
3276 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
3277 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
3278 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
3279 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
3280 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
3281 unlock_user_struct(target_shm_info
, target_addr
, 1);
3285 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)