4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
23 #include "qemu/memfd.h"
24 #include "qemu/queue.h"
31 #include <sys/mount.h>
33 #include <sys/fsuid.h>
34 #include <sys/personality.h>
35 #include <sys/prctl.h>
36 #include <sys/resource.h>
38 #include <linux/capability.h>
40 #include <sys/timex.h>
41 #include <sys/socket.h>
42 #include <linux/sockios.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 <linux/icmpv6.h>
59 #include <linux/errqueue.h>
60 #include <linux/random.h>
62 #include <sys/timerfd.h>
65 #include <sys/eventfd.h>
68 #include <sys/epoll.h>
71 #include "qemu/xattr.h"
73 #ifdef CONFIG_SENDFILE
74 #include <sys/sendfile.h>
80 #define termios host_termios
81 #define winsize host_winsize
82 #define termio host_termio
83 #define sgttyb host_sgttyb /* same as target */
84 #define tchars host_tchars /* same as target */
85 #define ltchars host_ltchars /* same as target */
87 #include <linux/termios.h>
88 #include <linux/unistd.h>
89 #include <linux/cdrom.h>
90 #include <linux/hdreg.h>
91 #include <linux/soundcard.h>
93 #include <linux/mtio.h>
96 #if defined(CONFIG_FIEMAP)
97 #include <linux/fiemap.h>
100 #if defined(CONFIG_USBFS)
101 #include <linux/usbdevice_fs.h>
102 #include <linux/usb/ch9.h>
104 #include <linux/vt.h>
105 #include <linux/dm-ioctl.h>
106 #include <linux/reboot.h>
107 #include <linux/route.h>
108 #include <linux/filter.h>
109 #include <linux/blkpg.h>
110 #include <netpacket/packet.h>
111 #include <linux/netlink.h>
112 #include <linux/if_alg.h>
113 #include <linux/rtc.h>
114 #include <sound/asound.h>
115 #include "linux_loop.h"
119 #include "qemu/guest-random.h"
120 #include "user/syscall-trace.h"
121 #include "qapi/error.h"
122 #include "fd-trans.h"
126 #define CLONE_IO 0x80000000 /* Clone io context */
129 /* We can't directly call the host clone syscall, because this will
130 * badly confuse libc (breaking mutexes, for example). So we must
131 * divide clone flags into:
132 * * flag combinations that look like pthread_create()
133 * * flag combinations that look like fork()
134 * * flags we can implement within QEMU itself
135 * * flags we can't support and will return an error for
137 /* For thread creation, all these flags must be present; for
138 * fork, none must be present.
140 #define CLONE_THREAD_FLAGS \
141 (CLONE_VM | CLONE_FS | CLONE_FILES | \
142 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
144 /* These flags are ignored:
145 * CLONE_DETACHED is now ignored by the kernel;
146 * CLONE_IO is just an optimisation hint to the I/O scheduler
148 #define CLONE_IGNORED_FLAGS \
149 (CLONE_DETACHED | CLONE_IO)
151 /* Flags for fork which we can implement within QEMU itself */
152 #define CLONE_OPTIONAL_FORK_FLAGS \
153 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
154 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
156 /* Flags for thread creation which we can implement within QEMU itself */
157 #define CLONE_OPTIONAL_THREAD_FLAGS \
158 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
159 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
161 #define CLONE_INVALID_FORK_FLAGS \
162 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
164 #define CLONE_INVALID_THREAD_FLAGS \
165 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
166 CLONE_IGNORED_FLAGS))
168 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
169 * have almost all been allocated. We cannot support any of
170 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
171 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
172 * The checks against the invalid thread masks above will catch these.
173 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
176 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
177 * once. This exercises the codepaths for restart.
179 //#define DEBUG_ERESTARTSYS
181 //#include <linux/msdos_fs.h>
182 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
183 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
193 #define _syscall0(type,name) \
194 static type name (void) \
196 return syscall(__NR_##name); \
199 #define _syscall1(type,name,type1,arg1) \
200 static type name (type1 arg1) \
202 return syscall(__NR_##name, arg1); \
205 #define _syscall2(type,name,type1,arg1,type2,arg2) \
206 static type name (type1 arg1,type2 arg2) \
208 return syscall(__NR_##name, arg1, arg2); \
211 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
212 static type name (type1 arg1,type2 arg2,type3 arg3) \
214 return syscall(__NR_##name, arg1, arg2, arg3); \
217 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
218 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
220 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
223 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
225 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
227 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
231 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
232 type5,arg5,type6,arg6) \
233 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
236 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
240 #define __NR_sys_uname __NR_uname
241 #define __NR_sys_getcwd1 __NR_getcwd
242 #define __NR_sys_getdents __NR_getdents
243 #define __NR_sys_getdents64 __NR_getdents64
244 #define __NR_sys_getpriority __NR_getpriority
245 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
246 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
247 #define __NR_sys_syslog __NR_syslog
248 #define __NR_sys_futex __NR_futex
249 #define __NR_sys_inotify_init __NR_inotify_init
250 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
251 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
252 #define __NR_sys_statx __NR_statx
254 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
255 #define __NR__llseek __NR_lseek
258 /* Newer kernel ports have llseek() instead of _llseek() */
259 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
260 #define TARGET_NR__llseek TARGET_NR_llseek
263 #define __NR_sys_gettid __NR_gettid
264 _syscall0(int, sys_gettid
)
266 /* For the 64-bit guest on 32-bit host case we must emulate
267 * getdents using getdents64, because otherwise the host
268 * might hand us back more dirent records than we can fit
269 * into the guest buffer after structure format conversion.
270 * Otherwise we emulate getdents with getdents if the host has it.
272 #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
273 #define EMULATE_GETDENTS_WITH_GETDENTS
276 #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
277 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
279 #if (defined(TARGET_NR_getdents) && \
280 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
281 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
282 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
284 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
285 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
286 loff_t
*, res
, uint
, wh
);
288 _syscall3(int, sys_rt_sigqueueinfo
, pid_t
, pid
, int, sig
, siginfo_t
*, uinfo
)
289 _syscall4(int, sys_rt_tgsigqueueinfo
, pid_t
, pid
, pid_t
, tid
, int, sig
,
291 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
292 #ifdef __NR_exit_group
293 _syscall1(int,exit_group
,int,error_code
)
295 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
296 _syscall1(int,set_tid_address
,int *,tidptr
)
298 #if defined(TARGET_NR_futex) && defined(__NR_futex)
299 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
300 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
302 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
303 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
304 unsigned long *, user_mask_ptr
);
305 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
306 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
307 unsigned long *, user_mask_ptr
);
308 #define __NR_sys_getcpu __NR_getcpu
309 _syscall3(int, sys_getcpu
, unsigned *, cpu
, unsigned *, node
, void *, tcache
);
310 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
312 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
313 struct __user_cap_data_struct
*, data
);
314 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
315 struct __user_cap_data_struct
*, data
);
316 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
317 _syscall2(int, ioprio_get
, int, which
, int, who
)
319 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
320 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
322 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
323 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
326 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
327 _syscall5(int, kcmp
, pid_t
, pid1
, pid_t
, pid2
, int, type
,
328 unsigned long, idx1
, unsigned long, idx2
)
332 * It is assumed that struct statx is architecture independent.
334 #if defined(TARGET_NR_statx) && defined(__NR_statx)
335 _syscall5(int, sys_statx
, int, dirfd
, const char *, pathname
, int, flags
,
336 unsigned int, mask
, struct target_statx
*, statxbuf
)
338 #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
339 _syscall2(int, membarrier
, int, cmd
, int, flags
)
342 static bitmask_transtbl fcntl_flags_tbl
[] = {
343 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
344 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
345 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
346 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
347 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
348 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
349 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
350 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
351 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
352 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
353 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
354 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
355 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
356 #if defined(O_DIRECT)
357 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
359 #if defined(O_NOATIME)
360 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
362 #if defined(O_CLOEXEC)
363 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
366 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
368 #if defined(O_TMPFILE)
369 { TARGET_O_TMPFILE
, TARGET_O_TMPFILE
, O_TMPFILE
, O_TMPFILE
},
371 /* Don't terminate the list prematurely on 64-bit host+guest. */
372 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
373 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
378 static int sys_getcwd1(char *buf
, size_t size
)
380 if (getcwd(buf
, size
) == NULL
) {
381 /* getcwd() sets errno */
384 return strlen(buf
)+1;
387 #ifdef TARGET_NR_utimensat
388 #if defined(__NR_utimensat)
389 #define __NR_sys_utimensat __NR_utimensat
390 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
391 const struct timespec
*,tsp
,int,flags
)
393 static int sys_utimensat(int dirfd
, const char *pathname
,
394 const struct timespec times
[2], int flags
)
400 #endif /* TARGET_NR_utimensat */
402 #ifdef TARGET_NR_renameat2
403 #if defined(__NR_renameat2)
404 #define __NR_sys_renameat2 __NR_renameat2
405 _syscall5(int, sys_renameat2
, int, oldfd
, const char *, old
, int, newfd
,
406 const char *, new, unsigned int, flags
)
408 static int sys_renameat2(int oldfd
, const char *old
,
409 int newfd
, const char *new, int flags
)
412 return renameat(oldfd
, old
, newfd
, new);
418 #endif /* TARGET_NR_renameat2 */
420 #ifdef CONFIG_INOTIFY
421 #include <sys/inotify.h>
423 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
424 static int sys_inotify_init(void)
426 return (inotify_init());
429 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
430 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
432 return (inotify_add_watch(fd
, pathname
, mask
));
435 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
436 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
438 return (inotify_rm_watch(fd
, wd
));
441 #ifdef CONFIG_INOTIFY1
442 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
443 static int sys_inotify_init1(int flags
)
445 return (inotify_init1(flags
));
450 /* Userspace can usually survive runtime without inotify */
451 #undef TARGET_NR_inotify_init
452 #undef TARGET_NR_inotify_init1
453 #undef TARGET_NR_inotify_add_watch
454 #undef TARGET_NR_inotify_rm_watch
455 #endif /* CONFIG_INOTIFY */
457 #if defined(TARGET_NR_prlimit64)
458 #ifndef __NR_prlimit64
459 # define __NR_prlimit64 -1
461 #define __NR_sys_prlimit64 __NR_prlimit64
462 /* The glibc rlimit structure may not be that used by the underlying syscall */
463 struct host_rlimit64
{
467 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
468 const struct host_rlimit64
*, new_limit
,
469 struct host_rlimit64
*, old_limit
)
473 #if defined(TARGET_NR_timer_create)
474 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
475 static timer_t g_posix_timers
[32] = { 0, } ;
477 static inline int next_free_host_timer(void)
480 /* FIXME: Does finding the next free slot require a lock? */
481 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
482 if (g_posix_timers
[k
] == 0) {
483 g_posix_timers
[k
] = (timer_t
) 1;
491 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
493 static inline int regpairs_aligned(void *cpu_env
, int num
)
495 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
497 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
498 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
499 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
500 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
501 * of registers which translates to the same as ARM/MIPS, because we start with
503 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
504 #elif defined(TARGET_SH4)
505 /* SH4 doesn't align register pairs, except for p{read,write}64 */
506 static inline int regpairs_aligned(void *cpu_env
, int num
)
509 case TARGET_NR_pread64
:
510 case TARGET_NR_pwrite64
:
517 #elif defined(TARGET_XTENSA)
518 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
520 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 0; }
523 #define ERRNO_TABLE_SIZE 1200
525 /* target_to_host_errno_table[] is initialized from
526 * host_to_target_errno_table[] in syscall_init(). */
527 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
531 * This list is the union of errno values overridden in asm-<arch>/errno.h
532 * minus the errnos that are not actually generic to all archs.
534 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
535 [EAGAIN
] = TARGET_EAGAIN
,
536 [EIDRM
] = TARGET_EIDRM
,
537 [ECHRNG
] = TARGET_ECHRNG
,
538 [EL2NSYNC
] = TARGET_EL2NSYNC
,
539 [EL3HLT
] = TARGET_EL3HLT
,
540 [EL3RST
] = TARGET_EL3RST
,
541 [ELNRNG
] = TARGET_ELNRNG
,
542 [EUNATCH
] = TARGET_EUNATCH
,
543 [ENOCSI
] = TARGET_ENOCSI
,
544 [EL2HLT
] = TARGET_EL2HLT
,
545 [EDEADLK
] = TARGET_EDEADLK
,
546 [ENOLCK
] = TARGET_ENOLCK
,
547 [EBADE
] = TARGET_EBADE
,
548 [EBADR
] = TARGET_EBADR
,
549 [EXFULL
] = TARGET_EXFULL
,
550 [ENOANO
] = TARGET_ENOANO
,
551 [EBADRQC
] = TARGET_EBADRQC
,
552 [EBADSLT
] = TARGET_EBADSLT
,
553 [EBFONT
] = TARGET_EBFONT
,
554 [ENOSTR
] = TARGET_ENOSTR
,
555 [ENODATA
] = TARGET_ENODATA
,
556 [ETIME
] = TARGET_ETIME
,
557 [ENOSR
] = TARGET_ENOSR
,
558 [ENONET
] = TARGET_ENONET
,
559 [ENOPKG
] = TARGET_ENOPKG
,
560 [EREMOTE
] = TARGET_EREMOTE
,
561 [ENOLINK
] = TARGET_ENOLINK
,
562 [EADV
] = TARGET_EADV
,
563 [ESRMNT
] = TARGET_ESRMNT
,
564 [ECOMM
] = TARGET_ECOMM
,
565 [EPROTO
] = TARGET_EPROTO
,
566 [EDOTDOT
] = TARGET_EDOTDOT
,
567 [EMULTIHOP
] = TARGET_EMULTIHOP
,
568 [EBADMSG
] = TARGET_EBADMSG
,
569 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
570 [EOVERFLOW
] = TARGET_EOVERFLOW
,
571 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
572 [EBADFD
] = TARGET_EBADFD
,
573 [EREMCHG
] = TARGET_EREMCHG
,
574 [ELIBACC
] = TARGET_ELIBACC
,
575 [ELIBBAD
] = TARGET_ELIBBAD
,
576 [ELIBSCN
] = TARGET_ELIBSCN
,
577 [ELIBMAX
] = TARGET_ELIBMAX
,
578 [ELIBEXEC
] = TARGET_ELIBEXEC
,
579 [EILSEQ
] = TARGET_EILSEQ
,
580 [ENOSYS
] = TARGET_ENOSYS
,
581 [ELOOP
] = TARGET_ELOOP
,
582 [ERESTART
] = TARGET_ERESTART
,
583 [ESTRPIPE
] = TARGET_ESTRPIPE
,
584 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
585 [EUSERS
] = TARGET_EUSERS
,
586 [ENOTSOCK
] = TARGET_ENOTSOCK
,
587 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
588 [EMSGSIZE
] = TARGET_EMSGSIZE
,
589 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
590 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
591 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
592 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
593 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
594 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
595 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
596 [EADDRINUSE
] = TARGET_EADDRINUSE
,
597 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
598 [ENETDOWN
] = TARGET_ENETDOWN
,
599 [ENETUNREACH
] = TARGET_ENETUNREACH
,
600 [ENETRESET
] = TARGET_ENETRESET
,
601 [ECONNABORTED
] = TARGET_ECONNABORTED
,
602 [ECONNRESET
] = TARGET_ECONNRESET
,
603 [ENOBUFS
] = TARGET_ENOBUFS
,
604 [EISCONN
] = TARGET_EISCONN
,
605 [ENOTCONN
] = TARGET_ENOTCONN
,
606 [EUCLEAN
] = TARGET_EUCLEAN
,
607 [ENOTNAM
] = TARGET_ENOTNAM
,
608 [ENAVAIL
] = TARGET_ENAVAIL
,
609 [EISNAM
] = TARGET_EISNAM
,
610 [EREMOTEIO
] = TARGET_EREMOTEIO
,
611 [EDQUOT
] = TARGET_EDQUOT
,
612 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
613 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
614 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
615 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
616 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
617 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
618 [EALREADY
] = TARGET_EALREADY
,
619 [EINPROGRESS
] = TARGET_EINPROGRESS
,
620 [ESTALE
] = TARGET_ESTALE
,
621 [ECANCELED
] = TARGET_ECANCELED
,
622 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
623 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
625 [ENOKEY
] = TARGET_ENOKEY
,
628 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
631 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
634 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
637 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
639 #ifdef ENOTRECOVERABLE
640 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
643 [ENOMSG
] = TARGET_ENOMSG
,
646 [ERFKILL
] = TARGET_ERFKILL
,
649 [EHWPOISON
] = TARGET_EHWPOISON
,
653 static inline int host_to_target_errno(int err
)
655 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
656 host_to_target_errno_table
[err
]) {
657 return host_to_target_errno_table
[err
];
662 static inline int target_to_host_errno(int err
)
664 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
665 target_to_host_errno_table
[err
]) {
666 return target_to_host_errno_table
[err
];
671 static inline abi_long
get_errno(abi_long ret
)
674 return -host_to_target_errno(errno
);
679 const char *target_strerror(int err
)
681 if (err
== TARGET_ERESTARTSYS
) {
682 return "To be restarted";
684 if (err
== TARGET_QEMU_ESIGRETURN
) {
685 return "Successful exit from sigreturn";
688 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
691 return strerror(target_to_host_errno(err
));
694 #define safe_syscall0(type, name) \
695 static type safe_##name(void) \
697 return safe_syscall(__NR_##name); \
700 #define safe_syscall1(type, name, type1, arg1) \
701 static type safe_##name(type1 arg1) \
703 return safe_syscall(__NR_##name, arg1); \
706 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
707 static type safe_##name(type1 arg1, type2 arg2) \
709 return safe_syscall(__NR_##name, arg1, arg2); \
712 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
713 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
715 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
718 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
720 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
722 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
725 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
726 type4, arg4, type5, arg5) \
727 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
730 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
733 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
734 type4, arg4, type5, arg5, type6, arg6) \
735 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
736 type5 arg5, type6 arg6) \
738 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
741 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
742 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
743 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
744 int, flags
, mode_t
, mode
)
745 #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
746 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
747 struct rusage
*, rusage
)
749 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
750 int, options
, struct rusage
*, rusage
)
751 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
752 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
753 defined(TARGET_NR_pselect6)
754 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
755 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
757 #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll)
758 safe_syscall5(int, ppoll
, struct pollfd
*, ufds
, unsigned int, nfds
,
759 struct timespec
*, tsp
, const sigset_t
*, sigmask
,
762 safe_syscall6(int, epoll_pwait
, int, epfd
, struct epoll_event
*, events
,
763 int, maxevents
, int, timeout
, const sigset_t
*, sigmask
,
765 #ifdef TARGET_NR_futex
766 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
767 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
769 safe_syscall2(int, rt_sigsuspend
, sigset_t
*, newset
, size_t, sigsetsize
)
770 safe_syscall2(int, kill
, pid_t
, pid
, int, sig
)
771 safe_syscall2(int, tkill
, int, tid
, int, sig
)
772 safe_syscall3(int, tgkill
, int, tgid
, int, pid
, int, sig
)
773 safe_syscall3(ssize_t
, readv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
774 safe_syscall3(ssize_t
, writev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
775 safe_syscall5(ssize_t
, preadv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
776 unsigned long, pos_l
, unsigned long, pos_h
)
777 safe_syscall5(ssize_t
, pwritev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
778 unsigned long, pos_l
, unsigned long, pos_h
)
779 safe_syscall3(int, connect
, int, fd
, const struct sockaddr
*, addr
,
781 safe_syscall6(ssize_t
, sendto
, int, fd
, const void *, buf
, size_t, len
,
782 int, flags
, const struct sockaddr
*, addr
, socklen_t
, addrlen
)
783 safe_syscall6(ssize_t
, recvfrom
, int, fd
, void *, buf
, size_t, len
,
784 int, flags
, struct sockaddr
*, addr
, socklen_t
*, addrlen
)
785 safe_syscall3(ssize_t
, sendmsg
, int, fd
, const struct msghdr
*, msg
, int, flags
)
786 safe_syscall3(ssize_t
, recvmsg
, int, fd
, struct msghdr
*, msg
, int, flags
)
787 safe_syscall2(int, flock
, int, fd
, int, operation
)
788 #ifdef TARGET_NR_rt_sigtimedwait
789 safe_syscall4(int, rt_sigtimedwait
, const sigset_t
*, these
, siginfo_t
*, uinfo
,
790 const struct timespec
*, uts
, size_t, sigsetsize
)
792 safe_syscall4(int, accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, len
,
794 #if defined(TARGET_NR_nanosleep)
795 safe_syscall2(int, nanosleep
, const struct timespec
*, req
,
796 struct timespec
*, rem
)
798 #ifdef TARGET_NR_clock_nanosleep
799 safe_syscall4(int, clock_nanosleep
, const clockid_t
, clock
, int, flags
,
800 const struct timespec
*, req
, struct timespec
*, rem
)
803 safe_syscall6(int, ipc
, int, call
, long, first
, long, second
, long, third
,
804 void *, ptr
, long, fifth
)
807 safe_syscall4(int, msgsnd
, int, msgid
, const void *, msgp
, size_t, sz
,
811 safe_syscall5(int, msgrcv
, int, msgid
, void *, msgp
, size_t, sz
,
812 long, msgtype
, int, flags
)
814 #ifdef __NR_semtimedop
815 safe_syscall4(int, semtimedop
, int, semid
, struct sembuf
*, tsops
,
816 unsigned, nsops
, const struct timespec
*, timeout
)
818 #ifdef TARGET_NR_mq_timedsend
819 safe_syscall5(int, mq_timedsend
, int, mqdes
, const char *, msg_ptr
,
820 size_t, len
, unsigned, prio
, const struct timespec
*, timeout
)
822 #ifdef TARGET_NR_mq_timedreceive
823 safe_syscall5(int, mq_timedreceive
, int, mqdes
, char *, msg_ptr
,
824 size_t, len
, unsigned *, prio
, const struct timespec
*, timeout
)
826 /* We do ioctl like this rather than via safe_syscall3 to preserve the
827 * "third argument might be integer or pointer or not present" behaviour of
830 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
831 /* Similarly for fcntl. Note that callers must always:
832 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
833 * use the flock64 struct rather than unsuffixed flock
834 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
837 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
839 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
842 static inline int host_to_target_sock_type(int host_type
)
846 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
848 target_type
= TARGET_SOCK_DGRAM
;
851 target_type
= TARGET_SOCK_STREAM
;
854 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
858 #if defined(SOCK_CLOEXEC)
859 if (host_type
& SOCK_CLOEXEC
) {
860 target_type
|= TARGET_SOCK_CLOEXEC
;
864 #if defined(SOCK_NONBLOCK)
865 if (host_type
& SOCK_NONBLOCK
) {
866 target_type
|= TARGET_SOCK_NONBLOCK
;
873 static abi_ulong target_brk
;
874 static abi_ulong target_original_brk
;
875 static abi_ulong brk_page
;
877 void target_set_brk(abi_ulong new_brk
)
879 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
880 brk_page
= HOST_PAGE_ALIGN(target_brk
);
883 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
884 #define DEBUGF_BRK(message, args...)
886 /* do_brk() must return target values and target errnos. */
887 abi_long
do_brk(abi_ulong new_brk
)
889 abi_long mapped_addr
;
890 abi_ulong new_alloc_size
;
892 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
895 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
898 if (new_brk
< target_original_brk
) {
899 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
904 /* If the new brk is less than the highest page reserved to the
905 * target heap allocation, set it and we're almost done... */
906 if (new_brk
<= brk_page
) {
907 /* Heap contents are initialized to zero, as for anonymous
909 if (new_brk
> target_brk
) {
910 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
912 target_brk
= new_brk
;
913 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
917 /* We need to allocate more memory after the brk... Note that
918 * we don't use MAP_FIXED because that will map over the top of
919 * any existing mapping (like the one with the host libc or qemu
920 * itself); instead we treat "mapped but at wrong address" as
921 * a failure and unmap again.
923 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
924 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
925 PROT_READ
|PROT_WRITE
,
926 MAP_ANON
|MAP_PRIVATE
, 0, 0));
928 if (mapped_addr
== brk_page
) {
929 /* Heap contents are initialized to zero, as for anonymous
930 * mapped pages. Technically the new pages are already
931 * initialized to zero since they *are* anonymous mapped
932 * pages, however we have to take care with the contents that
933 * come from the remaining part of the previous page: it may
934 * contains garbage data due to a previous heap usage (grown
936 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
938 target_brk
= new_brk
;
939 brk_page
= HOST_PAGE_ALIGN(target_brk
);
940 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
943 } else if (mapped_addr
!= -1) {
944 /* Mapped but at wrong address, meaning there wasn't actually
945 * enough space for this brk.
947 target_munmap(mapped_addr
, new_alloc_size
);
949 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
952 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
955 #if defined(TARGET_ALPHA)
956 /* We (partially) emulate OSF/1 on Alpha, which requires we
957 return a proper errno, not an unchanged brk value. */
958 return -TARGET_ENOMEM
;
960 /* For everything else, return the previous break. */
964 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
965 defined(TARGET_NR_pselect6)
966 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
967 abi_ulong target_fds_addr
,
971 abi_ulong b
, *target_fds
;
973 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
974 if (!(target_fds
= lock_user(VERIFY_READ
,
976 sizeof(abi_ulong
) * nw
,
978 return -TARGET_EFAULT
;
982 for (i
= 0; i
< nw
; i
++) {
983 /* grab the abi_ulong */
984 __get_user(b
, &target_fds
[i
]);
985 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
986 /* check the bit inside the abi_ulong */
993 unlock_user(target_fds
, target_fds_addr
, 0);
998 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
999 abi_ulong target_fds_addr
,
1002 if (target_fds_addr
) {
1003 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
1004 return -TARGET_EFAULT
;
1012 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
1018 abi_ulong
*target_fds
;
1020 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
1021 if (!(target_fds
= lock_user(VERIFY_WRITE
,
1023 sizeof(abi_ulong
) * nw
,
1025 return -TARGET_EFAULT
;
1028 for (i
= 0; i
< nw
; i
++) {
1030 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
1031 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
1034 __put_user(v
, &target_fds
[i
]);
1037 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
1043 #if defined(__alpha__)
1044 #define HOST_HZ 1024
1049 static inline abi_long
host_to_target_clock_t(long ticks
)
1051 #if HOST_HZ == TARGET_HZ
1054 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
1058 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
1059 const struct rusage
*rusage
)
1061 struct target_rusage
*target_rusage
;
1063 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
1064 return -TARGET_EFAULT
;
1065 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
1066 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
1067 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
1068 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
1069 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
1070 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
1071 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
1072 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
1073 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
1074 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
1075 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
1076 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
1077 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
1078 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
1079 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
1080 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
1081 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
1082 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
1083 unlock_user_struct(target_rusage
, target_addr
, 1);
1088 #ifdef TARGET_NR_setrlimit
1089 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
1091 abi_ulong target_rlim_swap
;
1094 target_rlim_swap
= tswapal(target_rlim
);
1095 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
1096 return RLIM_INFINITY
;
1098 result
= target_rlim_swap
;
1099 if (target_rlim_swap
!= (rlim_t
)result
)
1100 return RLIM_INFINITY
;
1106 #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
1107 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1109 abi_ulong target_rlim_swap
;
1112 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1113 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1115 target_rlim_swap
= rlim
;
1116 result
= tswapal(target_rlim_swap
);
1122 static inline int target_to_host_resource(int code
)
1125 case TARGET_RLIMIT_AS
:
1127 case TARGET_RLIMIT_CORE
:
1129 case TARGET_RLIMIT_CPU
:
1131 case TARGET_RLIMIT_DATA
:
1133 case TARGET_RLIMIT_FSIZE
:
1134 return RLIMIT_FSIZE
;
1135 case TARGET_RLIMIT_LOCKS
:
1136 return RLIMIT_LOCKS
;
1137 case TARGET_RLIMIT_MEMLOCK
:
1138 return RLIMIT_MEMLOCK
;
1139 case TARGET_RLIMIT_MSGQUEUE
:
1140 return RLIMIT_MSGQUEUE
;
1141 case TARGET_RLIMIT_NICE
:
1143 case TARGET_RLIMIT_NOFILE
:
1144 return RLIMIT_NOFILE
;
1145 case TARGET_RLIMIT_NPROC
:
1146 return RLIMIT_NPROC
;
1147 case TARGET_RLIMIT_RSS
:
1149 case TARGET_RLIMIT_RTPRIO
:
1150 return RLIMIT_RTPRIO
;
1151 case TARGET_RLIMIT_SIGPENDING
:
1152 return RLIMIT_SIGPENDING
;
1153 case TARGET_RLIMIT_STACK
:
1154 return RLIMIT_STACK
;
1160 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1161 abi_ulong target_tv_addr
)
1163 struct target_timeval
*target_tv
;
1165 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1166 return -TARGET_EFAULT
;
1169 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1170 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1172 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1177 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1178 const struct timeval
*tv
)
1180 struct target_timeval
*target_tv
;
1182 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1183 return -TARGET_EFAULT
;
1186 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1187 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1189 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1194 static inline abi_long
copy_to_user_timeval64(abi_ulong target_tv_addr
,
1195 const struct timeval
*tv
)
1197 struct target__kernel_sock_timeval
*target_tv
;
1199 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1200 return -TARGET_EFAULT
;
1203 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1204 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1206 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1211 #if defined(TARGET_NR_futex) || \
1212 defined(TARGET_NR_rt_sigtimedwait) || \
1213 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1214 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1215 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
1216 defined(TARGET_NR_mq_timedreceive)
1217 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
1218 abi_ulong target_addr
)
1220 struct target_timespec
*target_ts
;
1222 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1223 return -TARGET_EFAULT
;
1225 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1226 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1227 unlock_user_struct(target_ts
, target_addr
, 0);
1232 #if defined(TARGET_NR_clock_settime64)
1233 static inline abi_long
target_to_host_timespec64(struct timespec
*host_ts
,
1234 abi_ulong target_addr
)
1236 struct target__kernel_timespec
*target_ts
;
1238 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1239 return -TARGET_EFAULT
;
1241 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1242 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1243 unlock_user_struct(target_ts
, target_addr
, 0);
1248 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
1249 struct timespec
*host_ts
)
1251 struct target_timespec
*target_ts
;
1253 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1254 return -TARGET_EFAULT
;
1256 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1257 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1258 unlock_user_struct(target_ts
, target_addr
, 1);
1262 static inline abi_long
host_to_target_timespec64(abi_ulong target_addr
,
1263 struct timespec
*host_ts
)
1265 struct target__kernel_timespec
*target_ts
;
1267 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1268 return -TARGET_EFAULT
;
1270 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1271 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1272 unlock_user_struct(target_ts
, target_addr
, 1);
1276 #if defined(TARGET_NR_settimeofday)
1277 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1278 abi_ulong target_tz_addr
)
1280 struct target_timezone
*target_tz
;
1282 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1283 return -TARGET_EFAULT
;
1286 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1287 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1289 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1295 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1298 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1299 abi_ulong target_mq_attr_addr
)
1301 struct target_mq_attr
*target_mq_attr
;
1303 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1304 target_mq_attr_addr
, 1))
1305 return -TARGET_EFAULT
;
1307 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1308 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1309 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1310 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1312 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1317 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1318 const struct mq_attr
*attr
)
1320 struct target_mq_attr
*target_mq_attr
;
1322 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1323 target_mq_attr_addr
, 0))
1324 return -TARGET_EFAULT
;
1326 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1327 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1328 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1329 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1331 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1337 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1338 /* do_select() must return target values and target errnos. */
1339 static abi_long
do_select(int n
,
1340 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1341 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1343 fd_set rfds
, wfds
, efds
;
1344 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1346 struct timespec ts
, *ts_ptr
;
1349 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1353 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1357 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1362 if (target_tv_addr
) {
1363 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1364 return -TARGET_EFAULT
;
1365 ts
.tv_sec
= tv
.tv_sec
;
1366 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1372 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1375 if (!is_error(ret
)) {
1376 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1377 return -TARGET_EFAULT
;
1378 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1379 return -TARGET_EFAULT
;
1380 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1381 return -TARGET_EFAULT
;
1383 if (target_tv_addr
) {
1384 tv
.tv_sec
= ts
.tv_sec
;
1385 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1386 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1387 return -TARGET_EFAULT
;
1395 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1396 static abi_long
do_old_select(abi_ulong arg1
)
1398 struct target_sel_arg_struct
*sel
;
1399 abi_ulong inp
, outp
, exp
, tvp
;
1402 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1)) {
1403 return -TARGET_EFAULT
;
1406 nsel
= tswapal(sel
->n
);
1407 inp
= tswapal(sel
->inp
);
1408 outp
= tswapal(sel
->outp
);
1409 exp
= tswapal(sel
->exp
);
1410 tvp
= tswapal(sel
->tvp
);
1412 unlock_user_struct(sel
, arg1
, 0);
1414 return do_select(nsel
, inp
, outp
, exp
, tvp
);
1419 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1422 return pipe2(host_pipe
, flags
);
1428 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1429 int flags
, int is_pipe2
)
1433 ret
= flags ?
do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1436 return get_errno(ret
);
1438 /* Several targets have special calling conventions for the original
1439 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1441 #if defined(TARGET_ALPHA)
1442 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1443 return host_pipe
[0];
1444 #elif defined(TARGET_MIPS)
1445 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1446 return host_pipe
[0];
1447 #elif defined(TARGET_SH4)
1448 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1449 return host_pipe
[0];
1450 #elif defined(TARGET_SPARC)
1451 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1452 return host_pipe
[0];
1456 if (put_user_s32(host_pipe
[0], pipedes
)
1457 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1458 return -TARGET_EFAULT
;
1459 return get_errno(ret
);
1462 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1463 abi_ulong target_addr
,
1466 struct target_ip_mreqn
*target_smreqn
;
1468 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1470 return -TARGET_EFAULT
;
1471 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1472 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1473 if (len
== sizeof(struct target_ip_mreqn
))
1474 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1475 unlock_user(target_smreqn
, target_addr
, 0);
1480 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1481 abi_ulong target_addr
,
1484 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1485 sa_family_t sa_family
;
1486 struct target_sockaddr
*target_saddr
;
1488 if (fd_trans_target_to_host_addr(fd
)) {
1489 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1492 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1494 return -TARGET_EFAULT
;
1496 sa_family
= tswap16(target_saddr
->sa_family
);
1498 /* Oops. The caller might send a incomplete sun_path; sun_path
1499 * must be terminated by \0 (see the manual page), but
1500 * unfortunately it is quite common to specify sockaddr_un
1501 * length as "strlen(x->sun_path)" while it should be
1502 * "strlen(...) + 1". We'll fix that here if needed.
1503 * Linux kernel has a similar feature.
1506 if (sa_family
== AF_UNIX
) {
1507 if (len
< unix_maxlen
&& len
> 0) {
1508 char *cp
= (char*)target_saddr
;
1510 if ( cp
[len
-1] && !cp
[len
] )
1513 if (len
> unix_maxlen
)
1517 memcpy(addr
, target_saddr
, len
);
1518 addr
->sa_family
= sa_family
;
1519 if (sa_family
== AF_NETLINK
) {
1520 struct sockaddr_nl
*nladdr
;
1522 nladdr
= (struct sockaddr_nl
*)addr
;
1523 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1524 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1525 } else if (sa_family
== AF_PACKET
) {
1526 struct target_sockaddr_ll
*lladdr
;
1528 lladdr
= (struct target_sockaddr_ll
*)addr
;
1529 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1530 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1532 unlock_user(target_saddr
, target_addr
, 0);
1537 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1538 struct sockaddr
*addr
,
1541 struct target_sockaddr
*target_saddr
;
1548 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1550 return -TARGET_EFAULT
;
1551 memcpy(target_saddr
, addr
, len
);
1552 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1553 sizeof(target_saddr
->sa_family
)) {
1554 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1556 if (addr
->sa_family
== AF_NETLINK
&&
1557 len
>= sizeof(struct target_sockaddr_nl
)) {
1558 struct target_sockaddr_nl
*target_nl
=
1559 (struct target_sockaddr_nl
*)target_saddr
;
1560 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1561 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1562 } else if (addr
->sa_family
== AF_PACKET
) {
1563 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1564 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1565 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1566 } else if (addr
->sa_family
== AF_INET6
&&
1567 len
>= sizeof(struct target_sockaddr_in6
)) {
1568 struct target_sockaddr_in6
*target_in6
=
1569 (struct target_sockaddr_in6
*)target_saddr
;
1570 target_in6
->sin6_scope_id
= tswap16(target_in6
->sin6_scope_id
);
1572 unlock_user(target_saddr
, target_addr
, len
);
1577 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1578 struct target_msghdr
*target_msgh
)
1580 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1581 abi_long msg_controllen
;
1582 abi_ulong target_cmsg_addr
;
1583 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1584 socklen_t space
= 0;
1586 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1587 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1589 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1590 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1591 target_cmsg_start
= target_cmsg
;
1593 return -TARGET_EFAULT
;
1595 while (cmsg
&& target_cmsg
) {
1596 void *data
= CMSG_DATA(cmsg
);
1597 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1599 int len
= tswapal(target_cmsg
->cmsg_len
)
1600 - sizeof(struct target_cmsghdr
);
1602 space
+= CMSG_SPACE(len
);
1603 if (space
> msgh
->msg_controllen
) {
1604 space
-= CMSG_SPACE(len
);
1605 /* This is a QEMU bug, since we allocated the payload
1606 * area ourselves (unlike overflow in host-to-target
1607 * conversion, which is just the guest giving us a buffer
1608 * that's too small). It can't happen for the payload types
1609 * we currently support; if it becomes an issue in future
1610 * we would need to improve our allocation strategy to
1611 * something more intelligent than "twice the size of the
1612 * target buffer we're reading from".
1614 qemu_log_mask(LOG_UNIMP
,
1615 ("Unsupported ancillary data %d/%d: "
1616 "unhandled msg size\n"),
1617 tswap32(target_cmsg
->cmsg_level
),
1618 tswap32(target_cmsg
->cmsg_type
));
1622 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1623 cmsg
->cmsg_level
= SOL_SOCKET
;
1625 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1627 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1628 cmsg
->cmsg_len
= CMSG_LEN(len
);
1630 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1631 int *fd
= (int *)data
;
1632 int *target_fd
= (int *)target_data
;
1633 int i
, numfds
= len
/ sizeof(int);
1635 for (i
= 0; i
< numfds
; i
++) {
1636 __get_user(fd
[i
], target_fd
+ i
);
1638 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1639 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1640 struct ucred
*cred
= (struct ucred
*)data
;
1641 struct target_ucred
*target_cred
=
1642 (struct target_ucred
*)target_data
;
1644 __get_user(cred
->pid
, &target_cred
->pid
);
1645 __get_user(cred
->uid
, &target_cred
->uid
);
1646 __get_user(cred
->gid
, &target_cred
->gid
);
1648 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1649 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1650 memcpy(data
, target_data
, len
);
1653 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1654 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1657 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1659 msgh
->msg_controllen
= space
;
1663 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1664 struct msghdr
*msgh
)
1666 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1667 abi_long msg_controllen
;
1668 abi_ulong target_cmsg_addr
;
1669 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1670 socklen_t space
= 0;
1672 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1673 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1675 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1676 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1677 target_cmsg_start
= target_cmsg
;
1679 return -TARGET_EFAULT
;
1681 while (cmsg
&& target_cmsg
) {
1682 void *data
= CMSG_DATA(cmsg
);
1683 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1685 int len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
1686 int tgt_len
, tgt_space
;
1688 /* We never copy a half-header but may copy half-data;
1689 * this is Linux's behaviour in put_cmsg(). Note that
1690 * truncation here is a guest problem (which we report
1691 * to the guest via the CTRUNC bit), unlike truncation
1692 * in target_to_host_cmsg, which is a QEMU bug.
1694 if (msg_controllen
< sizeof(struct target_cmsghdr
)) {
1695 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1699 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1700 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1702 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1704 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1706 /* Payload types which need a different size of payload on
1707 * the target must adjust tgt_len here.
1710 switch (cmsg
->cmsg_level
) {
1712 switch (cmsg
->cmsg_type
) {
1714 tgt_len
= sizeof(struct target_timeval
);
1724 if (msg_controllen
< TARGET_CMSG_LEN(tgt_len
)) {
1725 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1726 tgt_len
= msg_controllen
- sizeof(struct target_cmsghdr
);
1729 /* We must now copy-and-convert len bytes of payload
1730 * into tgt_len bytes of destination space. Bear in mind
1731 * that in both source and destination we may be dealing
1732 * with a truncated value!
1734 switch (cmsg
->cmsg_level
) {
1736 switch (cmsg
->cmsg_type
) {
1739 int *fd
= (int *)data
;
1740 int *target_fd
= (int *)target_data
;
1741 int i
, numfds
= tgt_len
/ sizeof(int);
1743 for (i
= 0; i
< numfds
; i
++) {
1744 __put_user(fd
[i
], target_fd
+ i
);
1750 struct timeval
*tv
= (struct timeval
*)data
;
1751 struct target_timeval
*target_tv
=
1752 (struct target_timeval
*)target_data
;
1754 if (len
!= sizeof(struct timeval
) ||
1755 tgt_len
!= sizeof(struct target_timeval
)) {
1759 /* copy struct timeval to target */
1760 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1761 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1764 case SCM_CREDENTIALS
:
1766 struct ucred
*cred
= (struct ucred
*)data
;
1767 struct target_ucred
*target_cred
=
1768 (struct target_ucred
*)target_data
;
1770 __put_user(cred
->pid
, &target_cred
->pid
);
1771 __put_user(cred
->uid
, &target_cred
->uid
);
1772 __put_user(cred
->gid
, &target_cred
->gid
);
1781 switch (cmsg
->cmsg_type
) {
1784 uint32_t *v
= (uint32_t *)data
;
1785 uint32_t *t_int
= (uint32_t *)target_data
;
1787 if (len
!= sizeof(uint32_t) ||
1788 tgt_len
!= sizeof(uint32_t)) {
1791 __put_user(*v
, t_int
);
1797 struct sock_extended_err ee
;
1798 struct sockaddr_in offender
;
1800 struct errhdr_t
*errh
= (struct errhdr_t
*)data
;
1801 struct errhdr_t
*target_errh
=
1802 (struct errhdr_t
*)target_data
;
1804 if (len
!= sizeof(struct errhdr_t
) ||
1805 tgt_len
!= sizeof(struct errhdr_t
)) {
1808 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1809 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1810 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1811 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1812 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1813 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1814 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1815 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1816 (void *) &errh
->offender
, sizeof(errh
->offender
));
1825 switch (cmsg
->cmsg_type
) {
1828 uint32_t *v
= (uint32_t *)data
;
1829 uint32_t *t_int
= (uint32_t *)target_data
;
1831 if (len
!= sizeof(uint32_t) ||
1832 tgt_len
!= sizeof(uint32_t)) {
1835 __put_user(*v
, t_int
);
1841 struct sock_extended_err ee
;
1842 struct sockaddr_in6 offender
;
1844 struct errhdr6_t
*errh
= (struct errhdr6_t
*)data
;
1845 struct errhdr6_t
*target_errh
=
1846 (struct errhdr6_t
*)target_data
;
1848 if (len
!= sizeof(struct errhdr6_t
) ||
1849 tgt_len
!= sizeof(struct errhdr6_t
)) {
1852 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1853 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1854 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1855 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1856 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1857 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1858 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1859 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1860 (void *) &errh
->offender
, sizeof(errh
->offender
));
1870 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1871 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1872 memcpy(target_data
, data
, MIN(len
, tgt_len
));
1873 if (tgt_len
> len
) {
1874 memset(target_data
+ len
, 0, tgt_len
- len
);
1878 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(tgt_len
));
1879 tgt_space
= TARGET_CMSG_SPACE(tgt_len
);
1880 if (msg_controllen
< tgt_space
) {
1881 tgt_space
= msg_controllen
;
1883 msg_controllen
-= tgt_space
;
1885 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1886 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1889 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1891 target_msgh
->msg_controllen
= tswapal(space
);
1895 /* do_setsockopt() Must return target values and target errnos. */
1896 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1897 abi_ulong optval_addr
, socklen_t optlen
)
1901 struct ip_mreqn
*ip_mreq
;
1902 struct ip_mreq_source
*ip_mreq_source
;
1906 /* TCP options all take an 'int' value. */
1907 if (optlen
< sizeof(uint32_t))
1908 return -TARGET_EINVAL
;
1910 if (get_user_u32(val
, optval_addr
))
1911 return -TARGET_EFAULT
;
1912 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1919 case IP_ROUTER_ALERT
:
1923 case IP_MTU_DISCOVER
:
1930 case IP_MULTICAST_TTL
:
1931 case IP_MULTICAST_LOOP
:
1933 if (optlen
>= sizeof(uint32_t)) {
1934 if (get_user_u32(val
, optval_addr
))
1935 return -TARGET_EFAULT
;
1936 } else if (optlen
>= 1) {
1937 if (get_user_u8(val
, optval_addr
))
1938 return -TARGET_EFAULT
;
1940 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1942 case IP_ADD_MEMBERSHIP
:
1943 case IP_DROP_MEMBERSHIP
:
1944 if (optlen
< sizeof (struct target_ip_mreq
) ||
1945 optlen
> sizeof (struct target_ip_mreqn
))
1946 return -TARGET_EINVAL
;
1948 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1949 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1950 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1953 case IP_BLOCK_SOURCE
:
1954 case IP_UNBLOCK_SOURCE
:
1955 case IP_ADD_SOURCE_MEMBERSHIP
:
1956 case IP_DROP_SOURCE_MEMBERSHIP
:
1957 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1958 return -TARGET_EINVAL
;
1960 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1961 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1962 unlock_user (ip_mreq_source
, optval_addr
, 0);
1971 case IPV6_MTU_DISCOVER
:
1974 case IPV6_RECVPKTINFO
:
1975 case IPV6_UNICAST_HOPS
:
1976 case IPV6_MULTICAST_HOPS
:
1977 case IPV6_MULTICAST_LOOP
:
1979 case IPV6_RECVHOPLIMIT
:
1980 case IPV6_2292HOPLIMIT
:
1983 case IPV6_2292PKTINFO
:
1984 case IPV6_RECVTCLASS
:
1985 case IPV6_RECVRTHDR
:
1986 case IPV6_2292RTHDR
:
1987 case IPV6_RECVHOPOPTS
:
1988 case IPV6_2292HOPOPTS
:
1989 case IPV6_RECVDSTOPTS
:
1990 case IPV6_2292DSTOPTS
:
1992 #ifdef IPV6_RECVPATHMTU
1993 case IPV6_RECVPATHMTU
:
1995 #ifdef IPV6_TRANSPARENT
1996 case IPV6_TRANSPARENT
:
1998 #ifdef IPV6_FREEBIND
2001 #ifdef IPV6_RECVORIGDSTADDR
2002 case IPV6_RECVORIGDSTADDR
:
2005 if (optlen
< sizeof(uint32_t)) {
2006 return -TARGET_EINVAL
;
2008 if (get_user_u32(val
, optval_addr
)) {
2009 return -TARGET_EFAULT
;
2011 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2012 &val
, sizeof(val
)));
2016 struct in6_pktinfo pki
;
2018 if (optlen
< sizeof(pki
)) {
2019 return -TARGET_EINVAL
;
2022 if (copy_from_user(&pki
, optval_addr
, sizeof(pki
))) {
2023 return -TARGET_EFAULT
;
2026 pki
.ipi6_ifindex
= tswap32(pki
.ipi6_ifindex
);
2028 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2029 &pki
, sizeof(pki
)));
2032 case IPV6_ADD_MEMBERSHIP
:
2033 case IPV6_DROP_MEMBERSHIP
:
2035 struct ipv6_mreq ipv6mreq
;
2037 if (optlen
< sizeof(ipv6mreq
)) {
2038 return -TARGET_EINVAL
;
2041 if (copy_from_user(&ipv6mreq
, optval_addr
, sizeof(ipv6mreq
))) {
2042 return -TARGET_EFAULT
;
2045 ipv6mreq
.ipv6mr_interface
= tswap32(ipv6mreq
.ipv6mr_interface
);
2047 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2048 &ipv6mreq
, sizeof(ipv6mreq
)));
2059 struct icmp6_filter icmp6f
;
2061 if (optlen
> sizeof(icmp6f
)) {
2062 optlen
= sizeof(icmp6f
);
2065 if (copy_from_user(&icmp6f
, optval_addr
, optlen
)) {
2066 return -TARGET_EFAULT
;
2069 for (val
= 0; val
< 8; val
++) {
2070 icmp6f
.data
[val
] = tswap32(icmp6f
.data
[val
]);
2073 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2085 /* those take an u32 value */
2086 if (optlen
< sizeof(uint32_t)) {
2087 return -TARGET_EINVAL
;
2090 if (get_user_u32(val
, optval_addr
)) {
2091 return -TARGET_EFAULT
;
2093 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2094 &val
, sizeof(val
)));
2101 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2106 char *alg_key
= g_malloc(optlen
);
2109 return -TARGET_ENOMEM
;
2111 if (copy_from_user(alg_key
, optval_addr
, optlen
)) {
2113 return -TARGET_EFAULT
;
2115 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2120 case ALG_SET_AEAD_AUTHSIZE
:
2122 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2131 case TARGET_SOL_SOCKET
:
2133 case TARGET_SO_RCVTIMEO
:
2137 optname
= SO_RCVTIMEO
;
2140 if (optlen
!= sizeof(struct target_timeval
)) {
2141 return -TARGET_EINVAL
;
2144 if (copy_from_user_timeval(&tv
, optval_addr
)) {
2145 return -TARGET_EFAULT
;
2148 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2152 case TARGET_SO_SNDTIMEO
:
2153 optname
= SO_SNDTIMEO
;
2155 case TARGET_SO_ATTACH_FILTER
:
2157 struct target_sock_fprog
*tfprog
;
2158 struct target_sock_filter
*tfilter
;
2159 struct sock_fprog fprog
;
2160 struct sock_filter
*filter
;
2163 if (optlen
!= sizeof(*tfprog
)) {
2164 return -TARGET_EINVAL
;
2166 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
2167 return -TARGET_EFAULT
;
2169 if (!lock_user_struct(VERIFY_READ
, tfilter
,
2170 tswapal(tfprog
->filter
), 0)) {
2171 unlock_user_struct(tfprog
, optval_addr
, 1);
2172 return -TARGET_EFAULT
;
2175 fprog
.len
= tswap16(tfprog
->len
);
2176 filter
= g_try_new(struct sock_filter
, fprog
.len
);
2177 if (filter
== NULL
) {
2178 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2179 unlock_user_struct(tfprog
, optval_addr
, 1);
2180 return -TARGET_ENOMEM
;
2182 for (i
= 0; i
< fprog
.len
; i
++) {
2183 filter
[i
].code
= tswap16(tfilter
[i
].code
);
2184 filter
[i
].jt
= tfilter
[i
].jt
;
2185 filter
[i
].jf
= tfilter
[i
].jf
;
2186 filter
[i
].k
= tswap32(tfilter
[i
].k
);
2188 fprog
.filter
= filter
;
2190 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
2191 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
2194 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2195 unlock_user_struct(tfprog
, optval_addr
, 1);
2198 case TARGET_SO_BINDTODEVICE
:
2200 char *dev_ifname
, *addr_ifname
;
2202 if (optlen
> IFNAMSIZ
- 1) {
2203 optlen
= IFNAMSIZ
- 1;
2205 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2207 return -TARGET_EFAULT
;
2209 optname
= SO_BINDTODEVICE
;
2210 addr_ifname
= alloca(IFNAMSIZ
);
2211 memcpy(addr_ifname
, dev_ifname
, optlen
);
2212 addr_ifname
[optlen
] = 0;
2213 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2214 addr_ifname
, optlen
));
2215 unlock_user (dev_ifname
, optval_addr
, 0);
2218 case TARGET_SO_LINGER
:
2221 struct target_linger
*tlg
;
2223 if (optlen
!= sizeof(struct target_linger
)) {
2224 return -TARGET_EINVAL
;
2226 if (!lock_user_struct(VERIFY_READ
, tlg
, optval_addr
, 1)) {
2227 return -TARGET_EFAULT
;
2229 __get_user(lg
.l_onoff
, &tlg
->l_onoff
);
2230 __get_user(lg
.l_linger
, &tlg
->l_linger
);
2231 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, SO_LINGER
,
2233 unlock_user_struct(tlg
, optval_addr
, 0);
2236 /* Options with 'int' argument. */
2237 case TARGET_SO_DEBUG
:
2240 case TARGET_SO_REUSEADDR
:
2241 optname
= SO_REUSEADDR
;
2244 case TARGET_SO_REUSEPORT
:
2245 optname
= SO_REUSEPORT
;
2248 case TARGET_SO_TYPE
:
2251 case TARGET_SO_ERROR
:
2254 case TARGET_SO_DONTROUTE
:
2255 optname
= SO_DONTROUTE
;
2257 case TARGET_SO_BROADCAST
:
2258 optname
= SO_BROADCAST
;
2260 case TARGET_SO_SNDBUF
:
2261 optname
= SO_SNDBUF
;
2263 case TARGET_SO_SNDBUFFORCE
:
2264 optname
= SO_SNDBUFFORCE
;
2266 case TARGET_SO_RCVBUF
:
2267 optname
= SO_RCVBUF
;
2269 case TARGET_SO_RCVBUFFORCE
:
2270 optname
= SO_RCVBUFFORCE
;
2272 case TARGET_SO_KEEPALIVE
:
2273 optname
= SO_KEEPALIVE
;
2275 case TARGET_SO_OOBINLINE
:
2276 optname
= SO_OOBINLINE
;
2278 case TARGET_SO_NO_CHECK
:
2279 optname
= SO_NO_CHECK
;
2281 case TARGET_SO_PRIORITY
:
2282 optname
= SO_PRIORITY
;
2285 case TARGET_SO_BSDCOMPAT
:
2286 optname
= SO_BSDCOMPAT
;
2289 case TARGET_SO_PASSCRED
:
2290 optname
= SO_PASSCRED
;
2292 case TARGET_SO_PASSSEC
:
2293 optname
= SO_PASSSEC
;
2295 case TARGET_SO_TIMESTAMP
:
2296 optname
= SO_TIMESTAMP
;
2298 case TARGET_SO_RCVLOWAT
:
2299 optname
= SO_RCVLOWAT
;
2304 if (optlen
< sizeof(uint32_t))
2305 return -TARGET_EINVAL
;
2307 if (get_user_u32(val
, optval_addr
))
2308 return -TARGET_EFAULT
;
2309 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
2314 case NETLINK_PKTINFO
:
2315 case NETLINK_ADD_MEMBERSHIP
:
2316 case NETLINK_DROP_MEMBERSHIP
:
2317 case NETLINK_BROADCAST_ERROR
:
2318 case NETLINK_NO_ENOBUFS
:
2319 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2320 case NETLINK_LISTEN_ALL_NSID
:
2321 case NETLINK_CAP_ACK
:
2322 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2323 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2324 case NETLINK_EXT_ACK
:
2325 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2326 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2327 case NETLINK_GET_STRICT_CHK
:
2328 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2334 if (optlen
< sizeof(uint32_t)) {
2335 return -TARGET_EINVAL
;
2337 if (get_user_u32(val
, optval_addr
)) {
2338 return -TARGET_EFAULT
;
2340 ret
= get_errno(setsockopt(sockfd
, SOL_NETLINK
, optname
, &val
,
2343 #endif /* SOL_NETLINK */
2346 qemu_log_mask(LOG_UNIMP
, "Unsupported setsockopt level=%d optname=%d\n",
2348 ret
= -TARGET_ENOPROTOOPT
;
2353 /* do_getsockopt() Must return target values and target errnos. */
2354 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
2355 abi_ulong optval_addr
, abi_ulong optlen
)
2362 case TARGET_SOL_SOCKET
:
2365 /* These don't just return a single integer */
2366 case TARGET_SO_PEERNAME
:
2368 case TARGET_SO_RCVTIMEO
: {
2372 optname
= SO_RCVTIMEO
;
2375 if (get_user_u32(len
, optlen
)) {
2376 return -TARGET_EFAULT
;
2379 return -TARGET_EINVAL
;
2383 ret
= get_errno(getsockopt(sockfd
, level
, optname
,
2388 if (len
> sizeof(struct target_timeval
)) {
2389 len
= sizeof(struct target_timeval
);
2391 if (copy_to_user_timeval(optval_addr
, &tv
)) {
2392 return -TARGET_EFAULT
;
2394 if (put_user_u32(len
, optlen
)) {
2395 return -TARGET_EFAULT
;
2399 case TARGET_SO_SNDTIMEO
:
2400 optname
= SO_SNDTIMEO
;
2402 case TARGET_SO_PEERCRED
: {
2405 struct target_ucred
*tcr
;
2407 if (get_user_u32(len
, optlen
)) {
2408 return -TARGET_EFAULT
;
2411 return -TARGET_EINVAL
;
2415 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
2423 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
2424 return -TARGET_EFAULT
;
2426 __put_user(cr
.pid
, &tcr
->pid
);
2427 __put_user(cr
.uid
, &tcr
->uid
);
2428 __put_user(cr
.gid
, &tcr
->gid
);
2429 unlock_user_struct(tcr
, optval_addr
, 1);
2430 if (put_user_u32(len
, optlen
)) {
2431 return -TARGET_EFAULT
;
2435 case TARGET_SO_PEERSEC
: {
2438 if (get_user_u32(len
, optlen
)) {
2439 return -TARGET_EFAULT
;
2442 return -TARGET_EINVAL
;
2444 name
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 0);
2446 return -TARGET_EFAULT
;
2449 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERSEC
,
2451 if (put_user_u32(lv
, optlen
)) {
2452 ret
= -TARGET_EFAULT
;
2454 unlock_user(name
, optval_addr
, lv
);
2457 case TARGET_SO_LINGER
:
2461 struct target_linger
*tlg
;
2463 if (get_user_u32(len
, optlen
)) {
2464 return -TARGET_EFAULT
;
2467 return -TARGET_EINVAL
;
2471 ret
= get_errno(getsockopt(sockfd
, level
, SO_LINGER
,
2479 if (!lock_user_struct(VERIFY_WRITE
, tlg
, optval_addr
, 0)) {
2480 return -TARGET_EFAULT
;
2482 __put_user(lg
.l_onoff
, &tlg
->l_onoff
);
2483 __put_user(lg
.l_linger
, &tlg
->l_linger
);
2484 unlock_user_struct(tlg
, optval_addr
, 1);
2485 if (put_user_u32(len
, optlen
)) {
2486 return -TARGET_EFAULT
;
2490 /* Options with 'int' argument. */
2491 case TARGET_SO_DEBUG
:
2494 case TARGET_SO_REUSEADDR
:
2495 optname
= SO_REUSEADDR
;
2498 case TARGET_SO_REUSEPORT
:
2499 optname
= SO_REUSEPORT
;
2502 case TARGET_SO_TYPE
:
2505 case TARGET_SO_ERROR
:
2508 case TARGET_SO_DONTROUTE
:
2509 optname
= SO_DONTROUTE
;
2511 case TARGET_SO_BROADCAST
:
2512 optname
= SO_BROADCAST
;
2514 case TARGET_SO_SNDBUF
:
2515 optname
= SO_SNDBUF
;
2517 case TARGET_SO_RCVBUF
:
2518 optname
= SO_RCVBUF
;
2520 case TARGET_SO_KEEPALIVE
:
2521 optname
= SO_KEEPALIVE
;
2523 case TARGET_SO_OOBINLINE
:
2524 optname
= SO_OOBINLINE
;
2526 case TARGET_SO_NO_CHECK
:
2527 optname
= SO_NO_CHECK
;
2529 case TARGET_SO_PRIORITY
:
2530 optname
= SO_PRIORITY
;
2533 case TARGET_SO_BSDCOMPAT
:
2534 optname
= SO_BSDCOMPAT
;
2537 case TARGET_SO_PASSCRED
:
2538 optname
= SO_PASSCRED
;
2540 case TARGET_SO_TIMESTAMP
:
2541 optname
= SO_TIMESTAMP
;
2543 case TARGET_SO_RCVLOWAT
:
2544 optname
= SO_RCVLOWAT
;
2546 case TARGET_SO_ACCEPTCONN
:
2547 optname
= SO_ACCEPTCONN
;
2554 /* TCP options all take an 'int' value. */
2556 if (get_user_u32(len
, optlen
))
2557 return -TARGET_EFAULT
;
2559 return -TARGET_EINVAL
;
2561 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2564 if (optname
== SO_TYPE
) {
2565 val
= host_to_target_sock_type(val
);
2570 if (put_user_u32(val
, optval_addr
))
2571 return -TARGET_EFAULT
;
2573 if (put_user_u8(val
, optval_addr
))
2574 return -TARGET_EFAULT
;
2576 if (put_user_u32(len
, optlen
))
2577 return -TARGET_EFAULT
;
2584 case IP_ROUTER_ALERT
:
2588 case IP_MTU_DISCOVER
:
2594 case IP_MULTICAST_TTL
:
2595 case IP_MULTICAST_LOOP
:
2596 if (get_user_u32(len
, optlen
))
2597 return -TARGET_EFAULT
;
2599 return -TARGET_EINVAL
;
2601 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2604 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2606 if (put_user_u32(len
, optlen
)
2607 || put_user_u8(val
, optval_addr
))
2608 return -TARGET_EFAULT
;
2610 if (len
> sizeof(int))
2612 if (put_user_u32(len
, optlen
)
2613 || put_user_u32(val
, optval_addr
))
2614 return -TARGET_EFAULT
;
2618 ret
= -TARGET_ENOPROTOOPT
;
2624 case IPV6_MTU_DISCOVER
:
2627 case IPV6_RECVPKTINFO
:
2628 case IPV6_UNICAST_HOPS
:
2629 case IPV6_MULTICAST_HOPS
:
2630 case IPV6_MULTICAST_LOOP
:
2632 case IPV6_RECVHOPLIMIT
:
2633 case IPV6_2292HOPLIMIT
:
2636 case IPV6_2292PKTINFO
:
2637 case IPV6_RECVTCLASS
:
2638 case IPV6_RECVRTHDR
:
2639 case IPV6_2292RTHDR
:
2640 case IPV6_RECVHOPOPTS
:
2641 case IPV6_2292HOPOPTS
:
2642 case IPV6_RECVDSTOPTS
:
2643 case IPV6_2292DSTOPTS
:
2645 #ifdef IPV6_RECVPATHMTU
2646 case IPV6_RECVPATHMTU
:
2648 #ifdef IPV6_TRANSPARENT
2649 case IPV6_TRANSPARENT
:
2651 #ifdef IPV6_FREEBIND
2654 #ifdef IPV6_RECVORIGDSTADDR
2655 case IPV6_RECVORIGDSTADDR
:
2657 if (get_user_u32(len
, optlen
))
2658 return -TARGET_EFAULT
;
2660 return -TARGET_EINVAL
;
2662 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2665 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2667 if (put_user_u32(len
, optlen
)
2668 || put_user_u8(val
, optval_addr
))
2669 return -TARGET_EFAULT
;
2671 if (len
> sizeof(int))
2673 if (put_user_u32(len
, optlen
)
2674 || put_user_u32(val
, optval_addr
))
2675 return -TARGET_EFAULT
;
2679 ret
= -TARGET_ENOPROTOOPT
;
2686 case NETLINK_PKTINFO
:
2687 case NETLINK_BROADCAST_ERROR
:
2688 case NETLINK_NO_ENOBUFS
:
2689 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2690 case NETLINK_LISTEN_ALL_NSID
:
2691 case NETLINK_CAP_ACK
:
2692 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2693 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2694 case NETLINK_EXT_ACK
:
2695 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2696 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2697 case NETLINK_GET_STRICT_CHK
:
2698 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2699 if (get_user_u32(len
, optlen
)) {
2700 return -TARGET_EFAULT
;
2702 if (len
!= sizeof(val
)) {
2703 return -TARGET_EINVAL
;
2706 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2710 if (put_user_u32(lv
, optlen
)
2711 || put_user_u32(val
, optval_addr
)) {
2712 return -TARGET_EFAULT
;
2715 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2716 case NETLINK_LIST_MEMBERSHIPS
:
2720 if (get_user_u32(len
, optlen
)) {
2721 return -TARGET_EFAULT
;
2724 return -TARGET_EINVAL
;
2726 results
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 1);
2728 return -TARGET_EFAULT
;
2731 ret
= get_errno(getsockopt(sockfd
, level
, optname
, results
, &lv
));
2733 unlock_user(results
, optval_addr
, 0);
2736 /* swap host endianess to target endianess. */
2737 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
2738 results
[i
] = tswap32(results
[i
]);
2740 if (put_user_u32(lv
, optlen
)) {
2741 return -TARGET_EFAULT
;
2743 unlock_user(results
, optval_addr
, 0);
2746 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2751 #endif /* SOL_NETLINK */
2754 qemu_log_mask(LOG_UNIMP
,
2755 "getsockopt level=%d optname=%d not yet supported\n",
2757 ret
= -TARGET_EOPNOTSUPP
;
2763 /* Convert target low/high pair representing file offset into the host
2764 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2765 * as the kernel doesn't handle them either.
2767 static void target_to_host_low_high(abi_ulong tlow
,
2769 unsigned long *hlow
,
2770 unsigned long *hhigh
)
2772 uint64_t off
= tlow
|
2773 ((unsigned long long)thigh
<< TARGET_LONG_BITS
/ 2) <<
2774 TARGET_LONG_BITS
/ 2;
2777 *hhigh
= (off
>> HOST_LONG_BITS
/ 2) >> HOST_LONG_BITS
/ 2;
2780 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
2781 abi_ulong count
, int copy
)
2783 struct target_iovec
*target_vec
;
2785 abi_ulong total_len
, max_len
;
2788 bool bad_address
= false;
2794 if (count
> IOV_MAX
) {
2799 vec
= g_try_new0(struct iovec
, count
);
2805 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2806 count
* sizeof(struct target_iovec
), 1);
2807 if (target_vec
== NULL
) {
2812 /* ??? If host page size > target page size, this will result in a
2813 value larger than what we can actually support. */
2814 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
2817 for (i
= 0; i
< count
; i
++) {
2818 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2819 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2824 } else if (len
== 0) {
2825 /* Zero length pointer is ignored. */
2826 vec
[i
].iov_base
= 0;
2828 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
2829 /* If the first buffer pointer is bad, this is a fault. But
2830 * subsequent bad buffers will result in a partial write; this
2831 * is realized by filling the vector with null pointers and
2833 if (!vec
[i
].iov_base
) {
2844 if (len
> max_len
- total_len
) {
2845 len
= max_len
- total_len
;
2848 vec
[i
].iov_len
= len
;
2852 unlock_user(target_vec
, target_addr
, 0);
2857 if (tswapal(target_vec
[i
].iov_len
) > 0) {
2858 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
2861 unlock_user(target_vec
, target_addr
, 0);
2868 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
2869 abi_ulong count
, int copy
)
2871 struct target_iovec
*target_vec
;
2874 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2875 count
* sizeof(struct target_iovec
), 1);
2877 for (i
= 0; i
< count
; i
++) {
2878 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2879 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2883 unlock_user(vec
[i
].iov_base
, base
, copy ? vec
[i
].iov_len
: 0);
2885 unlock_user(target_vec
, target_addr
, 0);
2891 static inline int target_to_host_sock_type(int *type
)
2894 int target_type
= *type
;
2896 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
2897 case TARGET_SOCK_DGRAM
:
2898 host_type
= SOCK_DGRAM
;
2900 case TARGET_SOCK_STREAM
:
2901 host_type
= SOCK_STREAM
;
2904 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
2907 if (target_type
& TARGET_SOCK_CLOEXEC
) {
2908 #if defined(SOCK_CLOEXEC)
2909 host_type
|= SOCK_CLOEXEC
;
2911 return -TARGET_EINVAL
;
2914 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2915 #if defined(SOCK_NONBLOCK)
2916 host_type
|= SOCK_NONBLOCK
;
2917 #elif !defined(O_NONBLOCK)
2918 return -TARGET_EINVAL
;
2925 /* Try to emulate socket type flags after socket creation. */
2926 static int sock_flags_fixup(int fd
, int target_type
)
2928 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2929 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2930 int flags
= fcntl(fd
, F_GETFL
);
2931 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
2933 return -TARGET_EINVAL
;
2940 /* do_socket() Must return target values and target errnos. */
2941 static abi_long
do_socket(int domain
, int type
, int protocol
)
2943 int target_type
= type
;
2946 ret
= target_to_host_sock_type(&type
);
2951 if (domain
== PF_NETLINK
&& !(
2952 #ifdef CONFIG_RTNETLINK
2953 protocol
== NETLINK_ROUTE
||
2955 protocol
== NETLINK_KOBJECT_UEVENT
||
2956 protocol
== NETLINK_AUDIT
)) {
2957 return -EPFNOSUPPORT
;
2960 if (domain
== AF_PACKET
||
2961 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
2962 protocol
= tswap16(protocol
);
2965 ret
= get_errno(socket(domain
, type
, protocol
));
2967 ret
= sock_flags_fixup(ret
, target_type
);
2968 if (type
== SOCK_PACKET
) {
2969 /* Manage an obsolete case :
2970 * if socket type is SOCK_PACKET, bind by name
2972 fd_trans_register(ret
, &target_packet_trans
);
2973 } else if (domain
== PF_NETLINK
) {
2975 #ifdef CONFIG_RTNETLINK
2977 fd_trans_register(ret
, &target_netlink_route_trans
);
2980 case NETLINK_KOBJECT_UEVENT
:
2981 /* nothing to do: messages are strings */
2984 fd_trans_register(ret
, &target_netlink_audit_trans
);
2987 g_assert_not_reached();
2994 /* do_bind() Must return target values and target errnos. */
2995 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
3001 if ((int)addrlen
< 0) {
3002 return -TARGET_EINVAL
;
3005 addr
= alloca(addrlen
+1);
3007 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3011 return get_errno(bind(sockfd
, addr
, addrlen
));
3014 /* do_connect() Must return target values and target errnos. */
3015 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
3021 if ((int)addrlen
< 0) {
3022 return -TARGET_EINVAL
;
3025 addr
= alloca(addrlen
+1);
3027 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3031 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
3034 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3035 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
3036 int flags
, int send
)
3042 abi_ulong target_vec
;
3044 if (msgp
->msg_name
) {
3045 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
3046 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3047 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3048 tswapal(msgp
->msg_name
),
3050 if (ret
== -TARGET_EFAULT
) {
3051 /* For connected sockets msg_name and msg_namelen must
3052 * be ignored, so returning EFAULT immediately is wrong.
3053 * Instead, pass a bad msg_name to the host kernel, and
3054 * let it decide whether to return EFAULT or not.
3056 msg
.msg_name
= (void *)-1;
3061 msg
.msg_name
= NULL
;
3062 msg
.msg_namelen
= 0;
3064 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3065 msg
.msg_control
= alloca(msg
.msg_controllen
);
3066 memset(msg
.msg_control
, 0, msg
.msg_controllen
);
3068 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3070 count
= tswapal(msgp
->msg_iovlen
);
3071 target_vec
= tswapal(msgp
->msg_iov
);
3073 if (count
> IOV_MAX
) {
3074 /* sendrcvmsg returns a different errno for this condition than
3075 * readv/writev, so we must catch it here before lock_iovec() does.
3077 ret
= -TARGET_EMSGSIZE
;
3081 vec
= lock_iovec(send ? VERIFY_READ
: VERIFY_WRITE
,
3082 target_vec
, count
, send
);
3084 ret
= -host_to_target_errno(errno
);
3087 msg
.msg_iovlen
= count
;
3091 if (fd_trans_target_to_host_data(fd
)) {
3094 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3095 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3096 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3097 msg
.msg_iov
->iov_len
);
3099 msg
.msg_iov
->iov_base
= host_msg
;
3100 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3104 ret
= target_to_host_cmsg(&msg
, msgp
);
3106 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3110 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3111 if (!is_error(ret
)) {
3113 if (fd_trans_host_to_target_data(fd
)) {
3114 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3115 MIN(msg
.msg_iov
->iov_len
, len
));
3117 ret
= host_to_target_cmsg(msgp
, &msg
);
3119 if (!is_error(ret
)) {
3120 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3121 msgp
->msg_flags
= tswap32(msg
.msg_flags
);
3122 if (msg
.msg_name
!= NULL
&& msg
.msg_name
!= (void *)-1) {
3123 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3124 msg
.msg_name
, msg
.msg_namelen
);
3136 unlock_iovec(vec
, target_vec
, count
, !send
);
3141 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3142 int flags
, int send
)
3145 struct target_msghdr
*msgp
;
3147 if (!lock_user_struct(send ? VERIFY_READ
: VERIFY_WRITE
,
3151 return -TARGET_EFAULT
;
3153 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3154 unlock_user_struct(msgp
, target_msg
, send ?
0 : 1);
3158 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3159 * so it might not have this *mmsg-specific flag either.
3161 #ifndef MSG_WAITFORONE
3162 #define MSG_WAITFORONE 0x10000
3165 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3166 unsigned int vlen
, unsigned int flags
,
3169 struct target_mmsghdr
*mmsgp
;
3173 if (vlen
> UIO_MAXIOV
) {
3177 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3179 return -TARGET_EFAULT
;
3182 for (i
= 0; i
< vlen
; i
++) {
3183 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3184 if (is_error(ret
)) {
3187 mmsgp
[i
].msg_len
= tswap32(ret
);
3188 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3189 if (flags
& MSG_WAITFORONE
) {
3190 flags
|= MSG_DONTWAIT
;
3194 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3196 /* Return number of datagrams sent if we sent any at all;
3197 * otherwise return the error.
3205 /* do_accept4() Must return target values and target errnos. */
3206 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3207 abi_ulong target_addrlen_addr
, int flags
)
3209 socklen_t addrlen
, ret_addrlen
;
3214 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
3216 if (target_addr
== 0) {
3217 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3220 /* linux returns EINVAL if addrlen pointer is invalid */
3221 if (get_user_u32(addrlen
, target_addrlen_addr
))
3222 return -TARGET_EINVAL
;
3224 if ((int)addrlen
< 0) {
3225 return -TARGET_EINVAL
;
3228 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3229 return -TARGET_EINVAL
;
3231 addr
= alloca(addrlen
);
3233 ret_addrlen
= addrlen
;
3234 ret
= get_errno(safe_accept4(fd
, addr
, &ret_addrlen
, host_flags
));
3235 if (!is_error(ret
)) {
3236 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3237 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3238 ret
= -TARGET_EFAULT
;
3244 /* do_getpeername() Must return target values and target errnos. */
3245 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3246 abi_ulong target_addrlen_addr
)
3248 socklen_t addrlen
, ret_addrlen
;
3252 if (get_user_u32(addrlen
, target_addrlen_addr
))
3253 return -TARGET_EFAULT
;
3255 if ((int)addrlen
< 0) {
3256 return -TARGET_EINVAL
;
3259 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3260 return -TARGET_EFAULT
;
3262 addr
= alloca(addrlen
);
3264 ret_addrlen
= addrlen
;
3265 ret
= get_errno(getpeername(fd
, addr
, &ret_addrlen
));
3266 if (!is_error(ret
)) {
3267 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3268 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3269 ret
= -TARGET_EFAULT
;
3275 /* do_getsockname() Must return target values and target errnos. */
3276 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3277 abi_ulong target_addrlen_addr
)
3279 socklen_t addrlen
, ret_addrlen
;
3283 if (get_user_u32(addrlen
, target_addrlen_addr
))
3284 return -TARGET_EFAULT
;
3286 if ((int)addrlen
< 0) {
3287 return -TARGET_EINVAL
;
3290 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3291 return -TARGET_EFAULT
;
3293 addr
= alloca(addrlen
);
3295 ret_addrlen
= addrlen
;
3296 ret
= get_errno(getsockname(fd
, addr
, &ret_addrlen
));
3297 if (!is_error(ret
)) {
3298 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3299 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3300 ret
= -TARGET_EFAULT
;
3306 /* do_socketpair() Must return target values and target errnos. */
3307 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3308 abi_ulong target_tab_addr
)
3313 target_to_host_sock_type(&