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 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
746 struct rusage
*, rusage
)
747 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
748 int, options
, struct rusage
*, rusage
)
749 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
750 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
751 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
752 safe_syscall5(int, ppoll
, struct pollfd
*, ufds
, unsigned int, nfds
,
753 struct timespec
*, tsp
, const sigset_t
*, sigmask
,
755 safe_syscall6(int, epoll_pwait
, int, epfd
, struct epoll_event
*, events
,
756 int, maxevents
, int, timeout
, const sigset_t
*, sigmask
,
758 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
759 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
760 safe_syscall2(int, rt_sigsuspend
, sigset_t
*, newset
, size_t, sigsetsize
)
761 safe_syscall2(int, kill
, pid_t
, pid
, int, sig
)
762 safe_syscall2(int, tkill
, int, tid
, int, sig
)
763 safe_syscall3(int, tgkill
, int, tgid
, int, pid
, int, sig
)
764 safe_syscall3(ssize_t
, readv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
765 safe_syscall3(ssize_t
, writev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
766 safe_syscall5(ssize_t
, preadv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
767 unsigned long, pos_l
, unsigned long, pos_h
)
768 safe_syscall5(ssize_t
, pwritev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
769 unsigned long, pos_l
, unsigned long, pos_h
)
770 safe_syscall3(int, connect
, int, fd
, const struct sockaddr
*, addr
,
772 safe_syscall6(ssize_t
, sendto
, int, fd
, const void *, buf
, size_t, len
,
773 int, flags
, const struct sockaddr
*, addr
, socklen_t
, addrlen
)
774 safe_syscall6(ssize_t
, recvfrom
, int, fd
, void *, buf
, size_t, len
,
775 int, flags
, struct sockaddr
*, addr
, socklen_t
*, addrlen
)
776 safe_syscall3(ssize_t
, sendmsg
, int, fd
, const struct msghdr
*, msg
, int, flags
)
777 safe_syscall3(ssize_t
, recvmsg
, int, fd
, struct msghdr
*, msg
, int, flags
)
778 safe_syscall2(int, flock
, int, fd
, int, operation
)
779 safe_syscall4(int, rt_sigtimedwait
, const sigset_t
*, these
, siginfo_t
*, uinfo
,
780 const struct timespec
*, uts
, size_t, sigsetsize
)
781 safe_syscall4(int, accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, len
,
783 safe_syscall2(int, nanosleep
, const struct timespec
*, req
,
784 struct timespec
*, rem
)
785 #ifdef TARGET_NR_clock_nanosleep
786 safe_syscall4(int, clock_nanosleep
, const clockid_t
, clock
, int, flags
,
787 const struct timespec
*, req
, struct timespec
*, rem
)
790 safe_syscall6(int, ipc
, int, call
, long, first
, long, second
, long, third
,
791 void *, ptr
, long, fifth
)
794 safe_syscall4(int, msgsnd
, int, msgid
, const void *, msgp
, size_t, sz
,
798 safe_syscall5(int, msgrcv
, int, msgid
, void *, msgp
, size_t, sz
,
799 long, msgtype
, int, flags
)
801 #ifdef __NR_semtimedop
802 safe_syscall4(int, semtimedop
, int, semid
, struct sembuf
*, tsops
,
803 unsigned, nsops
, const struct timespec
*, timeout
)
805 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
806 safe_syscall5(int, mq_timedsend
, int, mqdes
, const char *, msg_ptr
,
807 size_t, len
, unsigned, prio
, const struct timespec
*, timeout
)
808 safe_syscall5(int, mq_timedreceive
, int, mqdes
, char *, msg_ptr
,
809 size_t, len
, unsigned *, prio
, const struct timespec
*, timeout
)
811 /* We do ioctl like this rather than via safe_syscall3 to preserve the
812 * "third argument might be integer or pointer or not present" behaviour of
815 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
816 /* Similarly for fcntl. Note that callers must always:
817 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
818 * use the flock64 struct rather than unsuffixed flock
819 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
822 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
824 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
827 static inline int host_to_target_sock_type(int host_type
)
831 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
833 target_type
= TARGET_SOCK_DGRAM
;
836 target_type
= TARGET_SOCK_STREAM
;
839 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
843 #if defined(SOCK_CLOEXEC)
844 if (host_type
& SOCK_CLOEXEC
) {
845 target_type
|= TARGET_SOCK_CLOEXEC
;
849 #if defined(SOCK_NONBLOCK)
850 if (host_type
& SOCK_NONBLOCK
) {
851 target_type
|= TARGET_SOCK_NONBLOCK
;
858 static abi_ulong target_brk
;
859 static abi_ulong target_original_brk
;
860 static abi_ulong brk_page
;
862 void target_set_brk(abi_ulong new_brk
)
864 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
865 brk_page
= HOST_PAGE_ALIGN(target_brk
);
868 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
869 #define DEBUGF_BRK(message, args...)
871 /* do_brk() must return target values and target errnos. */
872 abi_long
do_brk(abi_ulong new_brk
)
874 abi_long mapped_addr
;
875 abi_ulong new_alloc_size
;
877 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
880 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
883 if (new_brk
< target_original_brk
) {
884 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
889 /* If the new brk is less than the highest page reserved to the
890 * target heap allocation, set it and we're almost done... */
891 if (new_brk
<= brk_page
) {
892 /* Heap contents are initialized to zero, as for anonymous
894 if (new_brk
> target_brk
) {
895 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
897 target_brk
= new_brk
;
898 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
902 /* We need to allocate more memory after the brk... Note that
903 * we don't use MAP_FIXED because that will map over the top of
904 * any existing mapping (like the one with the host libc or qemu
905 * itself); instead we treat "mapped but at wrong address" as
906 * a failure and unmap again.
908 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
909 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
910 PROT_READ
|PROT_WRITE
,
911 MAP_ANON
|MAP_PRIVATE
, 0, 0));
913 if (mapped_addr
== brk_page
) {
914 /* Heap contents are initialized to zero, as for anonymous
915 * mapped pages. Technically the new pages are already
916 * initialized to zero since they *are* anonymous mapped
917 * pages, however we have to take care with the contents that
918 * come from the remaining part of the previous page: it may
919 * contains garbage data due to a previous heap usage (grown
921 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
923 target_brk
= new_brk
;
924 brk_page
= HOST_PAGE_ALIGN(target_brk
);
925 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
928 } else if (mapped_addr
!= -1) {
929 /* Mapped but at wrong address, meaning there wasn't actually
930 * enough space for this brk.
932 target_munmap(mapped_addr
, new_alloc_size
);
934 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
937 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
940 #if defined(TARGET_ALPHA)
941 /* We (partially) emulate OSF/1 on Alpha, which requires we
942 return a proper errno, not an unchanged brk value. */
943 return -TARGET_ENOMEM
;
945 /* For everything else, return the previous break. */
949 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
950 abi_ulong target_fds_addr
,
954 abi_ulong b
, *target_fds
;
956 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
957 if (!(target_fds
= lock_user(VERIFY_READ
,
959 sizeof(abi_ulong
) * nw
,
961 return -TARGET_EFAULT
;
965 for (i
= 0; i
< nw
; i
++) {
966 /* grab the abi_ulong */
967 __get_user(b
, &target_fds
[i
]);
968 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
969 /* check the bit inside the abi_ulong */
976 unlock_user(target_fds
, target_fds_addr
, 0);
981 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
982 abi_ulong target_fds_addr
,
985 if (target_fds_addr
) {
986 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
987 return -TARGET_EFAULT
;
995 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
1001 abi_ulong
*target_fds
;
1003 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
1004 if (!(target_fds
= lock_user(VERIFY_WRITE
,
1006 sizeof(abi_ulong
) * nw
,
1008 return -TARGET_EFAULT
;
1011 for (i
= 0; i
< nw
; i
++) {
1013 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
1014 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
1017 __put_user(v
, &target_fds
[i
]);
1020 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
1025 #if defined(__alpha__)
1026 #define HOST_HZ 1024
1031 static inline abi_long
host_to_target_clock_t(long ticks
)
1033 #if HOST_HZ == TARGET_HZ
1036 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
1040 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
1041 const struct rusage
*rusage
)
1043 struct target_rusage
*target_rusage
;
1045 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
1046 return -TARGET_EFAULT
;
1047 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
1048 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
1049 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
1050 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
1051 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
1052 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
1053 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
1054 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
1055 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
1056 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
1057 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
1058 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
1059 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
1060 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
1061 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
1062 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
1063 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
1064 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
1065 unlock_user_struct(target_rusage
, target_addr
, 1);
1070 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
1072 abi_ulong target_rlim_swap
;
1075 target_rlim_swap
= tswapal(target_rlim
);
1076 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
1077 return RLIM_INFINITY
;
1079 result
= target_rlim_swap
;
1080 if (target_rlim_swap
!= (rlim_t
)result
)
1081 return RLIM_INFINITY
;
1086 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1088 abi_ulong target_rlim_swap
;
1091 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1092 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1094 target_rlim_swap
= rlim
;
1095 result
= tswapal(target_rlim_swap
);
1100 static inline int target_to_host_resource(int code
)
1103 case TARGET_RLIMIT_AS
:
1105 case TARGET_RLIMIT_CORE
:
1107 case TARGET_RLIMIT_CPU
:
1109 case TARGET_RLIMIT_DATA
:
1111 case TARGET_RLIMIT_FSIZE
:
1112 return RLIMIT_FSIZE
;
1113 case TARGET_RLIMIT_LOCKS
:
1114 return RLIMIT_LOCKS
;
1115 case TARGET_RLIMIT_MEMLOCK
:
1116 return RLIMIT_MEMLOCK
;
1117 case TARGET_RLIMIT_MSGQUEUE
:
1118 return RLIMIT_MSGQUEUE
;
1119 case TARGET_RLIMIT_NICE
:
1121 case TARGET_RLIMIT_NOFILE
:
1122 return RLIMIT_NOFILE
;
1123 case TARGET_RLIMIT_NPROC
:
1124 return RLIMIT_NPROC
;
1125 case TARGET_RLIMIT_RSS
:
1127 case TARGET_RLIMIT_RTPRIO
:
1128 return RLIMIT_RTPRIO
;
1129 case TARGET_RLIMIT_SIGPENDING
:
1130 return RLIMIT_SIGPENDING
;
1131 case TARGET_RLIMIT_STACK
:
1132 return RLIMIT_STACK
;
1138 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1139 abi_ulong target_tv_addr
)
1141 struct target_timeval
*target_tv
;
1143 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1144 return -TARGET_EFAULT
;
1147 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1148 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1150 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1155 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1156 const struct timeval
*tv
)
1158 struct target_timeval
*target_tv
;
1160 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1161 return -TARGET_EFAULT
;
1164 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1165 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1167 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1172 static inline abi_long
copy_to_user_timeval64(abi_ulong target_tv_addr
,
1173 const struct timeval
*tv
)
1175 struct target__kernel_sock_timeval
*target_tv
;
1177 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1178 return -TARGET_EFAULT
;
1181 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1182 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1184 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1189 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
1190 abi_ulong target_addr
)
1192 struct target_timespec
*target_ts
;
1194 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1195 return -TARGET_EFAULT
;
1197 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1198 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1199 unlock_user_struct(target_ts
, target_addr
, 0);
1203 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
1204 struct timespec
*host_ts
)
1206 struct target_timespec
*target_ts
;
1208 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1209 return -TARGET_EFAULT
;
1211 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1212 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1213 unlock_user_struct(target_ts
, target_addr
, 1);
1217 static inline abi_long
host_to_target_timespec64(abi_ulong target_addr
,
1218 struct timespec
*host_ts
)
1220 struct target__kernel_timespec
*target_ts
;
1222 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1223 return -TARGET_EFAULT
;
1225 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1226 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1227 unlock_user_struct(target_ts
, target_addr
, 1);
1231 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1232 abi_ulong target_tz_addr
)
1234 struct target_timezone
*target_tz
;
1236 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1237 return -TARGET_EFAULT
;
1240 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1241 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1243 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1248 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1251 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1252 abi_ulong target_mq_attr_addr
)
1254 struct target_mq_attr
*target_mq_attr
;
1256 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1257 target_mq_attr_addr
, 1))
1258 return -TARGET_EFAULT
;
1260 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1261 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1262 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1263 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1265 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1270 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1271 const struct mq_attr
*attr
)
1273 struct target_mq_attr
*target_mq_attr
;
1275 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1276 target_mq_attr_addr
, 0))
1277 return -TARGET_EFAULT
;
1279 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1280 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1281 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1282 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1284 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1290 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1291 /* do_select() must return target values and target errnos. */
1292 static abi_long
do_select(int n
,
1293 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1294 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1296 fd_set rfds
, wfds
, efds
;
1297 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1299 struct timespec ts
, *ts_ptr
;
1302 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1306 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1310 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1315 if (target_tv_addr
) {
1316 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1317 return -TARGET_EFAULT
;
1318 ts
.tv_sec
= tv
.tv_sec
;
1319 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1325 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1328 if (!is_error(ret
)) {
1329 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1330 return -TARGET_EFAULT
;
1331 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1332 return -TARGET_EFAULT
;
1333 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1334 return -TARGET_EFAULT
;
1336 if (target_tv_addr
) {
1337 tv
.tv_sec
= ts
.tv_sec
;
1338 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1339 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1340 return -TARGET_EFAULT
;
1348 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1349 static abi_long
do_old_select(abi_ulong arg1
)
1351 struct target_sel_arg_struct
*sel
;
1352 abi_ulong inp
, outp
, exp
, tvp
;
1355 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1)) {
1356 return -TARGET_EFAULT
;
1359 nsel
= tswapal(sel
->n
);
1360 inp
= tswapal(sel
->inp
);
1361 outp
= tswapal(sel
->outp
);
1362 exp
= tswapal(sel
->exp
);
1363 tvp
= tswapal(sel
->tvp
);
1365 unlock_user_struct(sel
, arg1
, 0);
1367 return do_select(nsel
, inp
, outp
, exp
, tvp
);
1372 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1375 return pipe2(host_pipe
, flags
);
1381 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1382 int flags
, int is_pipe2
)
1386 ret
= flags ?
do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1389 return get_errno(ret
);
1391 /* Several targets have special calling conventions for the original
1392 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1394 #if defined(TARGET_ALPHA)
1395 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1396 return host_pipe
[0];
1397 #elif defined(TARGET_MIPS)
1398 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1399 return host_pipe
[0];
1400 #elif defined(TARGET_SH4)
1401 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1402 return host_pipe
[0];
1403 #elif defined(TARGET_SPARC)
1404 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1405 return host_pipe
[0];
1409 if (put_user_s32(host_pipe
[0], pipedes
)
1410 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1411 return -TARGET_EFAULT
;
1412 return get_errno(ret
);
1415 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1416 abi_ulong target_addr
,
1419 struct target_ip_mreqn
*target_smreqn
;
1421 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1423 return -TARGET_EFAULT
;
1424 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1425 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1426 if (len
== sizeof(struct target_ip_mreqn
))
1427 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1428 unlock_user(target_smreqn
, target_addr
, 0);
1433 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1434 abi_ulong target_addr
,
1437 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1438 sa_family_t sa_family
;
1439 struct target_sockaddr
*target_saddr
;
1441 if (fd_trans_target_to_host_addr(fd
)) {
1442 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1445 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1447 return -TARGET_EFAULT
;
1449 sa_family
= tswap16(target_saddr
->sa_family
);
1451 /* Oops. The caller might send a incomplete sun_path; sun_path
1452 * must be terminated by \0 (see the manual page), but
1453 * unfortunately it is quite common to specify sockaddr_un
1454 * length as "strlen(x->sun_path)" while it should be
1455 * "strlen(...) + 1". We'll fix that here if needed.
1456 * Linux kernel has a similar feature.
1459 if (sa_family
== AF_UNIX
) {
1460 if (len
< unix_maxlen
&& len
> 0) {
1461 char *cp
= (char*)target_saddr
;
1463 if ( cp
[len
-1] && !cp
[len
] )
1466 if (len
> unix_maxlen
)
1470 memcpy(addr
, target_saddr
, len
);
1471 addr
->sa_family
= sa_family
;
1472 if (sa_family
== AF_NETLINK
) {
1473 struct sockaddr_nl
*nladdr
;
1475 nladdr
= (struct sockaddr_nl
*)addr
;
1476 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1477 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1478 } else if (sa_family
== AF_PACKET
) {
1479 struct target_sockaddr_ll
*lladdr
;
1481 lladdr
= (struct target_sockaddr_ll
*)addr
;
1482 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1483 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1485 unlock_user(target_saddr
, target_addr
, 0);
1490 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1491 struct sockaddr
*addr
,
1494 struct target_sockaddr
*target_saddr
;
1501 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1503 return -TARGET_EFAULT
;
1504 memcpy(target_saddr
, addr
, len
);
1505 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1506 sizeof(target_saddr
->sa_family
)) {
1507 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1509 if (addr
->sa_family
== AF_NETLINK
&&
1510 len
>= sizeof(struct target_sockaddr_nl
)) {
1511 struct target_sockaddr_nl
*target_nl
=
1512 (struct target_sockaddr_nl
*)target_saddr
;
1513 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1514 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1515 } else if (addr
->sa_family
== AF_PACKET
) {
1516 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1517 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1518 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1519 } else if (addr
->sa_family
== AF_INET6
&&
1520 len
>= sizeof(struct target_sockaddr_in6
)) {
1521 struct target_sockaddr_in6
*target_in6
=
1522 (struct target_sockaddr_in6
*)target_saddr
;
1523 target_in6
->sin6_scope_id
= tswap16(target_in6
->sin6_scope_id
);
1525 unlock_user(target_saddr
, target_addr
, len
);
1530 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1531 struct target_msghdr
*target_msgh
)
1533 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1534 abi_long msg_controllen
;
1535 abi_ulong target_cmsg_addr
;
1536 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1537 socklen_t space
= 0;
1539 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1540 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1542 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1543 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1544 target_cmsg_start
= target_cmsg
;
1546 return -TARGET_EFAULT
;
1548 while (cmsg
&& target_cmsg
) {
1549 void *data
= CMSG_DATA(cmsg
);
1550 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1552 int len
= tswapal(target_cmsg
->cmsg_len
)
1553 - sizeof(struct target_cmsghdr
);
1555 space
+= CMSG_SPACE(len
);
1556 if (space
> msgh
->msg_controllen
) {
1557 space
-= CMSG_SPACE(len
);
1558 /* This is a QEMU bug, since we allocated the payload
1559 * area ourselves (unlike overflow in host-to-target
1560 * conversion, which is just the guest giving us a buffer
1561 * that's too small). It can't happen for the payload types
1562 * we currently support; if it becomes an issue in future
1563 * we would need to improve our allocation strategy to
1564 * something more intelligent than "twice the size of the
1565 * target buffer we're reading from".
1567 qemu_log_mask(LOG_UNIMP
,
1568 ("Unsupported ancillary data %d/%d: "
1569 "unhandled msg size\n"),
1570 tswap32(target_cmsg
->cmsg_level
),
1571 tswap32(target_cmsg
->cmsg_type
));
1575 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1576 cmsg
->cmsg_level
= SOL_SOCKET
;
1578 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1580 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1581 cmsg
->cmsg_len
= CMSG_LEN(len
);
1583 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1584 int *fd
= (int *)data
;
1585 int *target_fd
= (int *)target_data
;
1586 int i
, numfds
= len
/ sizeof(int);
1588 for (i
= 0; i
< numfds
; i
++) {
1589 __get_user(fd
[i
], target_fd
+ i
);
1591 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1592 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1593 struct ucred
*cred
= (struct ucred
*)data
;
1594 struct target_ucred
*target_cred
=
1595 (struct target_ucred
*)target_data
;
1597 __get_user(cred
->pid
, &target_cred
->pid
);
1598 __get_user(cred
->uid
, &target_cred
->uid
);
1599 __get_user(cred
->gid
, &target_cred
->gid
);
1601 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1602 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1603 memcpy(data
, target_data
, len
);
1606 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1607 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1610 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1612 msgh
->msg_controllen
= space
;
1616 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1617 struct msghdr
*msgh
)
1619 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1620 abi_long msg_controllen
;
1621 abi_ulong target_cmsg_addr
;
1622 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1623 socklen_t space
= 0;
1625 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1626 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1628 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1629 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1630 target_cmsg_start
= target_cmsg
;
1632 return -TARGET_EFAULT
;
1634 while (cmsg
&& target_cmsg
) {
1635 void *data
= CMSG_DATA(cmsg
);
1636 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1638 int len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
1639 int tgt_len
, tgt_space
;
1641 /* We never copy a half-header but may copy half-data;
1642 * this is Linux's behaviour in put_cmsg(). Note that
1643 * truncation here is a guest problem (which we report
1644 * to the guest via the CTRUNC bit), unlike truncation
1645 * in target_to_host_cmsg, which is a QEMU bug.
1647 if (msg_controllen
< sizeof(struct target_cmsghdr
)) {
1648 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1652 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1653 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1655 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1657 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1659 /* Payload types which need a different size of payload on
1660 * the target must adjust tgt_len here.
1663 switch (cmsg
->cmsg_level
) {
1665 switch (cmsg
->cmsg_type
) {
1667 tgt_len
= sizeof(struct target_timeval
);
1677 if (msg_controllen
< TARGET_CMSG_LEN(tgt_len
)) {
1678 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1679 tgt_len
= msg_controllen
- sizeof(struct target_cmsghdr
);
1682 /* We must now copy-and-convert len bytes of payload
1683 * into tgt_len bytes of destination space. Bear in mind
1684 * that in both source and destination we may be dealing
1685 * with a truncated value!
1687 switch (cmsg
->cmsg_level
) {
1689 switch (cmsg
->cmsg_type
) {
1692 int *fd
= (int *)data
;
1693 int *target_fd
= (int *)target_data
;
1694 int i
, numfds
= tgt_len
/ sizeof(int);
1696 for (i
= 0; i
< numfds
; i
++) {
1697 __put_user(fd
[i
], target_fd
+ i
);
1703 struct timeval
*tv
= (struct timeval
*)data
;
1704 struct target_timeval
*target_tv
=
1705 (struct target_timeval
*)target_data
;
1707 if (len
!= sizeof(struct timeval
) ||
1708 tgt_len
!= sizeof(struct target_timeval
)) {
1712 /* copy struct timeval to target */
1713 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1714 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1717 case SCM_CREDENTIALS
:
1719 struct ucred
*cred
= (struct ucred
*)data
;
1720 struct target_ucred
*target_cred
=
1721 (struct target_ucred
*)target_data
;
1723 __put_user(cred
->pid
, &target_cred
->pid
);
1724 __put_user(cred
->uid
, &target_cred
->uid
);
1725 __put_user(cred
->gid
, &target_cred
->gid
);
1734 switch (cmsg
->cmsg_type
) {
1737 uint32_t *v
= (uint32_t *)data
;
1738 uint32_t *t_int
= (uint32_t *)target_data
;
1740 if (len
!= sizeof(uint32_t) ||
1741 tgt_len
!= sizeof(uint32_t)) {
1744 __put_user(*v
, t_int
);
1750 struct sock_extended_err ee
;
1751 struct sockaddr_in offender
;
1753 struct errhdr_t
*errh
= (struct errhdr_t
*)data
;
1754 struct errhdr_t
*target_errh
=
1755 (struct errhdr_t
*)target_data
;
1757 if (len
!= sizeof(struct errhdr_t
) ||
1758 tgt_len
!= sizeof(struct errhdr_t
)) {
1761 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1762 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1763 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1764 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1765 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1766 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1767 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1768 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1769 (void *) &errh
->offender
, sizeof(errh
->offender
));
1778 switch (cmsg
->cmsg_type
) {
1781 uint32_t *v
= (uint32_t *)data
;
1782 uint32_t *t_int
= (uint32_t *)target_data
;
1784 if (len
!= sizeof(uint32_t) ||
1785 tgt_len
!= sizeof(uint32_t)) {
1788 __put_user(*v
, t_int
);
1794 struct sock_extended_err ee
;
1795 struct sockaddr_in6 offender
;
1797 struct errhdr6_t
*errh
= (struct errhdr6_t
*)data
;
1798 struct errhdr6_t
*target_errh
=
1799 (struct errhdr6_t
*)target_data
;
1801 if (len
!= sizeof(struct errhdr6_t
) ||
1802 tgt_len
!= sizeof(struct errhdr6_t
)) {
1805 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1806 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1807 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1808 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1809 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1810 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1811 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1812 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1813 (void *) &errh
->offender
, sizeof(errh
->offender
));
1823 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1824 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1825 memcpy(target_data
, data
, MIN(len
, tgt_len
));
1826 if (tgt_len
> len
) {
1827 memset(target_data
+ len
, 0, tgt_len
- len
);
1831 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(tgt_len
));
1832 tgt_space
= TARGET_CMSG_SPACE(tgt_len
);
1833 if (msg_controllen
< tgt_space
) {
1834 tgt_space
= msg_controllen
;
1836 msg_controllen
-= tgt_space
;
1838 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1839 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1842 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1844 target_msgh
->msg_controllen
= tswapal(space
);
1848 /* do_setsockopt() Must return target values and target errnos. */
1849 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1850 abi_ulong optval_addr
, socklen_t optlen
)
1854 struct ip_mreqn
*ip_mreq
;
1855 struct ip_mreq_source
*ip_mreq_source
;
1859 /* TCP options all take an 'int' value. */
1860 if (optlen
< sizeof(uint32_t))
1861 return -TARGET_EINVAL
;
1863 if (get_user_u32(val
, optval_addr
))
1864 return -TARGET_EFAULT
;
1865 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1872 case IP_ROUTER_ALERT
:
1876 case IP_MTU_DISCOVER
:
1883 case IP_MULTICAST_TTL
:
1884 case IP_MULTICAST_LOOP
:
1886 if (optlen
>= sizeof(uint32_t)) {
1887 if (get_user_u32(val
, optval_addr
))
1888 return -TARGET_EFAULT
;
1889 } else if (optlen
>= 1) {
1890 if (get_user_u8(val
, optval_addr
))
1891 return -TARGET_EFAULT
;
1893 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1895 case IP_ADD_MEMBERSHIP
:
1896 case IP_DROP_MEMBERSHIP
:
1897 if (optlen
< sizeof (struct target_ip_mreq
) ||
1898 optlen
> sizeof (struct target_ip_mreqn
))
1899 return -TARGET_EINVAL
;
1901 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1902 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1903 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1906 case IP_BLOCK_SOURCE
:
1907 case IP_UNBLOCK_SOURCE
:
1908 case IP_ADD_SOURCE_MEMBERSHIP
:
1909 case IP_DROP_SOURCE_MEMBERSHIP
:
1910 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1911 return -TARGET_EINVAL
;
1913 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1914 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1915 unlock_user (ip_mreq_source
, optval_addr
, 0);
1924 case IPV6_MTU_DISCOVER
:
1927 case IPV6_RECVPKTINFO
:
1928 case IPV6_UNICAST_HOPS
:
1929 case IPV6_MULTICAST_HOPS
:
1930 case IPV6_MULTICAST_LOOP
:
1932 case IPV6_RECVHOPLIMIT
:
1933 case IPV6_2292HOPLIMIT
:
1936 case IPV6_2292PKTINFO
:
1937 case IPV6_RECVTCLASS
:
1938 case IPV6_RECVRTHDR
:
1939 case IPV6_2292RTHDR
:
1940 case IPV6_RECVHOPOPTS
:
1941 case IPV6_2292HOPOPTS
:
1942 case IPV6_RECVDSTOPTS
:
1943 case IPV6_2292DSTOPTS
:
1945 #ifdef IPV6_RECVPATHMTU
1946 case IPV6_RECVPATHMTU
:
1948 #ifdef IPV6_TRANSPARENT
1949 case IPV6_TRANSPARENT
:
1951 #ifdef IPV6_FREEBIND
1954 #ifdef IPV6_RECVORIGDSTADDR
1955 case IPV6_RECVORIGDSTADDR
:
1958 if (optlen
< sizeof(uint32_t)) {
1959 return -TARGET_EINVAL
;
1961 if (get_user_u32(val
, optval_addr
)) {
1962 return -TARGET_EFAULT
;
1964 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1965 &val
, sizeof(val
)));
1969 struct in6_pktinfo pki
;
1971 if (optlen
< sizeof(pki
)) {
1972 return -TARGET_EINVAL
;
1975 if (copy_from_user(&pki
, optval_addr
, sizeof(pki
))) {
1976 return -TARGET_EFAULT
;
1979 pki
.ipi6_ifindex
= tswap32(pki
.ipi6_ifindex
);
1981 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1982 &pki
, sizeof(pki
)));
1985 case IPV6_ADD_MEMBERSHIP
:
1986 case IPV6_DROP_MEMBERSHIP
:
1988 struct ipv6_mreq ipv6mreq
;
1990 if (optlen
< sizeof(ipv6mreq
)) {
1991 return -TARGET_EINVAL
;
1994 if (copy_from_user(&ipv6mreq
, optval_addr
, sizeof(ipv6mreq
))) {
1995 return -TARGET_EFAULT
;
1998 ipv6mreq
.ipv6mr_interface
= tswap32(ipv6mreq
.ipv6mr_interface
);
2000 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2001 &ipv6mreq
, sizeof(ipv6mreq
)));
2012 struct icmp6_filter icmp6f
;
2014 if (optlen
> sizeof(icmp6f
)) {
2015 optlen
= sizeof(icmp6f
);
2018 if (copy_from_user(&icmp6f
, optval_addr
, optlen
)) {
2019 return -TARGET_EFAULT
;
2022 for (val
= 0; val
< 8; val
++) {
2023 icmp6f
.data
[val
] = tswap32(icmp6f
.data
[val
]);
2026 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2038 /* those take an u32 value */
2039 if (optlen
< sizeof(uint32_t)) {
2040 return -TARGET_EINVAL
;
2043 if (get_user_u32(val
, optval_addr
)) {
2044 return -TARGET_EFAULT
;
2046 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2047 &val
, sizeof(val
)));
2054 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2059 char *alg_key
= g_malloc(optlen
);
2062 return -TARGET_ENOMEM
;
2064 if (copy_from_user(alg_key
, optval_addr
, optlen
)) {
2066 return -TARGET_EFAULT
;
2068 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2073 case ALG_SET_AEAD_AUTHSIZE
:
2075 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2084 case TARGET_SOL_SOCKET
:
2086 case TARGET_SO_RCVTIMEO
:
2090 optname
= SO_RCVTIMEO
;
2093 if (optlen
!= sizeof(struct target_timeval
)) {
2094 return -TARGET_EINVAL
;
2097 if (copy_from_user_timeval(&tv
, optval_addr
)) {
2098 return -TARGET_EFAULT
;
2101 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2105 case TARGET_SO_SNDTIMEO
:
2106 optname
= SO_SNDTIMEO
;
2108 case TARGET_SO_ATTACH_FILTER
:
2110 struct target_sock_fprog
*tfprog
;
2111 struct target_sock_filter
*tfilter
;
2112 struct sock_fprog fprog
;
2113 struct sock_filter
*filter
;
2116 if (optlen
!= sizeof(*tfprog
)) {
2117 return -TARGET_EINVAL
;
2119 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
2120 return -TARGET_EFAULT
;
2122 if (!lock_user_struct(VERIFY_READ
, tfilter
,
2123 tswapal(tfprog
->filter
), 0)) {
2124 unlock_user_struct(tfprog
, optval_addr
, 1);
2125 return -TARGET_EFAULT
;
2128 fprog
.len
= tswap16(tfprog
->len
);
2129 filter
= g_try_new(struct sock_filter
, fprog
.len
);
2130 if (filter
== NULL
) {
2131 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2132 unlock_user_struct(tfprog
, optval_addr
, 1);
2133 return -TARGET_ENOMEM
;
2135 for (i
= 0; i
< fprog
.len
; i
++) {
2136 filter
[i
].code
= tswap16(tfilter
[i
].code
);
2137 filter
[i
].jt
= tfilter
[i
].jt
;
2138 filter
[i
].jf
= tfilter
[i
].jf
;
2139 filter
[i
].k
= tswap32(tfilter
[i
].k
);
2141 fprog
.filter
= filter
;
2143 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
2144 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
2147 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2148 unlock_user_struct(tfprog
, optval_addr
, 1);
2151 case TARGET_SO_BINDTODEVICE
:
2153 char *dev_ifname
, *addr_ifname
;
2155 if (optlen
> IFNAMSIZ
- 1) {
2156 optlen
= IFNAMSIZ
- 1;
2158 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2160 return -TARGET_EFAULT
;
2162 optname
= SO_BINDTODEVICE
;
2163 addr_ifname
= alloca(IFNAMSIZ
);
2164 memcpy(addr_ifname
, dev_ifname
, optlen
);
2165 addr_ifname
[optlen
] = 0;
2166 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2167 addr_ifname
, optlen
));
2168 unlock_user (dev_ifname
, optval_addr
, 0);
2171 case TARGET_SO_LINGER
:
2174 struct target_linger
*tlg
;
2176 if (optlen
!= sizeof(struct target_linger
)) {
2177 return -TARGET_EINVAL
;
2179 if (!lock_user_struct(VERIFY_READ
, tlg
, optval_addr
, 1)) {
2180 return -TARGET_EFAULT
;
2182 __get_user(lg
.l_onoff
, &tlg
->l_onoff
);
2183 __get_user(lg
.l_linger
, &tlg
->l_linger
);
2184 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, SO_LINGER
,
2186 unlock_user_struct(tlg
, optval_addr
, 0);
2189 /* Options with 'int' argument. */
2190 case TARGET_SO_DEBUG
:
2193 case TARGET_SO_REUSEADDR
:
2194 optname
= SO_REUSEADDR
;
2197 case TARGET_SO_REUSEPORT
:
2198 optname
= SO_REUSEPORT
;
2201 case TARGET_SO_TYPE
:
2204 case TARGET_SO_ERROR
:
2207 case TARGET_SO_DONTROUTE
:
2208 optname
= SO_DONTROUTE
;
2210 case TARGET_SO_BROADCAST
:
2211 optname
= SO_BROADCAST
;
2213 case TARGET_SO_SNDBUF
:
2214 optname
= SO_SNDBUF
;
2216 case TARGET_SO_SNDBUFFORCE
:
2217 optname
= SO_SNDBUFFORCE
;
2219 case TARGET_SO_RCVBUF
:
2220 optname
= SO_RCVBUF
;
2222 case TARGET_SO_RCVBUFFORCE
:
2223 optname
= SO_RCVBUFFORCE
;
2225 case TARGET_SO_KEEPALIVE
:
2226 optname
= SO_KEEPALIVE
;
2228 case TARGET_SO_OOBINLINE
:
2229 optname
= SO_OOBINLINE
;
2231 case TARGET_SO_NO_CHECK
:
2232 optname
= SO_NO_CHECK
;
2234 case TARGET_SO_PRIORITY
:
2235 optname
= SO_PRIORITY
;
2238 case TARGET_SO_BSDCOMPAT
:
2239 optname
= SO_BSDCOMPAT
;
2242 case TARGET_SO_PASSCRED
:
2243 optname
= SO_PASSCRED
;
2245 case TARGET_SO_PASSSEC
:
2246 optname
= SO_PASSSEC
;
2248 case TARGET_SO_TIMESTAMP
:
2249 optname
= SO_TIMESTAMP
;
2251 case TARGET_SO_RCVLOWAT
:
2252 optname
= SO_RCVLOWAT
;
2257 if (optlen
< sizeof(uint32_t))
2258 return -TARGET_EINVAL
;
2260 if (get_user_u32(val
, optval_addr
))
2261 return -TARGET_EFAULT
;
2262 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
2267 case NETLINK_PKTINFO
:
2268 case NETLINK_ADD_MEMBERSHIP
:
2269 case NETLINK_DROP_MEMBERSHIP
:
2270 case NETLINK_BROADCAST_ERROR
:
2271 case NETLINK_NO_ENOBUFS
:
2272 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2273 case NETLINK_LISTEN_ALL_NSID
:
2274 case NETLINK_CAP_ACK
:
2275 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2276 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2277 case NETLINK_EXT_ACK
:
2278 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2279 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2280 case NETLINK_GET_STRICT_CHK
:
2281 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2287 if (optlen
< sizeof(uint32_t)) {
2288 return -TARGET_EINVAL
;
2290 if (get_user_u32(val
, optval_addr
)) {
2291 return -TARGET_EFAULT
;
2293 ret
= get_errno(setsockopt(sockfd
, SOL_NETLINK
, optname
, &val
,
2296 #endif /* SOL_NETLINK */
2299 qemu_log_mask(LOG_UNIMP
, "Unsupported setsockopt level=%d optname=%d\n",
2301 ret
= -TARGET_ENOPROTOOPT
;
2306 /* do_getsockopt() Must return target values and target errnos. */
2307 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
2308 abi_ulong optval_addr
, abi_ulong optlen
)
2315 case TARGET_SOL_SOCKET
:
2318 /* These don't just return a single integer */
2319 case TARGET_SO_PEERNAME
:
2321 case TARGET_SO_RCVTIMEO
: {
2325 optname
= SO_RCVTIMEO
;
2328 if (get_user_u32(len
, optlen
)) {
2329 return -TARGET_EFAULT
;
2332 return -TARGET_EINVAL
;
2336 ret
= get_errno(getsockopt(sockfd
, level
, optname
,
2341 if (len
> sizeof(struct target_timeval
)) {
2342 len
= sizeof(struct target_timeval
);
2344 if (copy_to_user_timeval(optval_addr
, &tv
)) {
2345 return -TARGET_EFAULT
;
2347 if (put_user_u32(len
, optlen
)) {
2348 return -TARGET_EFAULT
;
2352 case TARGET_SO_SNDTIMEO
:
2353 optname
= SO_SNDTIMEO
;
2355 case TARGET_SO_PEERCRED
: {
2358 struct target_ucred
*tcr
;
2360 if (get_user_u32(len
, optlen
)) {
2361 return -TARGET_EFAULT
;
2364 return -TARGET_EINVAL
;
2368 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
2376 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
2377 return -TARGET_EFAULT
;
2379 __put_user(cr
.pid
, &tcr
->pid
);
2380 __put_user(cr
.uid
, &tcr
->uid
);
2381 __put_user(cr
.gid
, &tcr
->gid
);
2382 unlock_user_struct(tcr
, optval_addr
, 1);
2383 if (put_user_u32(len
, optlen
)) {
2384 return -TARGET_EFAULT
;
2388 case TARGET_SO_PEERSEC
: {
2391 if (get_user_u32(len
, optlen
)) {
2392 return -TARGET_EFAULT
;
2395 return -TARGET_EINVAL
;
2397 name
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 0);
2399 return -TARGET_EFAULT
;
2402 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERSEC
,
2404 if (put_user_u32(lv
, optlen
)) {
2405 ret
= -TARGET_EFAULT
;
2407 unlock_user(name
, optval_addr
, lv
);
2410 case TARGET_SO_LINGER
:
2414 struct target_linger
*tlg
;
2416 if (get_user_u32(len
, optlen
)) {
2417 return -TARGET_EFAULT
;
2420 return -TARGET_EINVAL
;
2424 ret
= get_errno(getsockopt(sockfd
, level
, SO_LINGER
,
2432 if (!lock_user_struct(VERIFY_WRITE
, tlg
, optval_addr
, 0)) {
2433 return -TARGET_EFAULT
;
2435 __put_user(lg
.l_onoff
, &tlg
->l_onoff
);
2436 __put_user(lg
.l_linger
, &tlg
->l_linger
);
2437 unlock_user_struct(tlg
, optval_addr
, 1);
2438 if (put_user_u32(len
, optlen
)) {
2439 return -TARGET_EFAULT
;
2443 /* Options with 'int' argument. */
2444 case TARGET_SO_DEBUG
:
2447 case TARGET_SO_REUSEADDR
:
2448 optname
= SO_REUSEADDR
;
2451 case TARGET_SO_REUSEPORT
:
2452 optname
= SO_REUSEPORT
;
2455 case TARGET_SO_TYPE
:
2458 case TARGET_SO_ERROR
:
2461 case TARGET_SO_DONTROUTE
:
2462 optname
= SO_DONTROUTE
;
2464 case TARGET_SO_BROADCAST
:
2465 optname
= SO_BROADCAST
;
2467 case TARGET_SO_SNDBUF
:
2468 optname
= SO_SNDBUF
;
2470 case TARGET_SO_RCVBUF
:
2471 optname
= SO_RCVBUF
;
2473 case TARGET_SO_KEEPALIVE
:
2474 optname
= SO_KEEPALIVE
;
2476 case TARGET_SO_OOBINLINE
:
2477 optname
= SO_OOBINLINE
;
2479 case TARGET_SO_NO_CHECK
:
2480 optname
= SO_NO_CHECK
;
2482 case TARGET_SO_PRIORITY
:
2483 optname
= SO_PRIORITY
;
2486 case TARGET_SO_BSDCOMPAT
:
2487 optname
= SO_BSDCOMPAT
;
2490 case TARGET_SO_PASSCRED
:
2491 optname
= SO_PASSCRED
;
2493 case TARGET_SO_TIMESTAMP
:
2494 optname
= SO_TIMESTAMP
;
2496 case TARGET_SO_RCVLOWAT
:
2497 optname
= SO_RCVLOWAT
;
2499 case TARGET_SO_ACCEPTCONN
:
2500 optname
= SO_ACCEPTCONN
;
2507 /* TCP options all take an 'int' value. */
2509 if (get_user_u32(len
, optlen
))
2510 return -TARGET_EFAULT
;
2512 return -TARGET_EINVAL
;
2514 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2517 if (optname
== SO_TYPE
) {
2518 val
= host_to_target_sock_type(val
);
2523 if (put_user_u32(val
, optval_addr
))
2524 return -TARGET_EFAULT
;
2526 if (put_user_u8(val
, optval_addr
))
2527 return -TARGET_EFAULT
;
2529 if (put_user_u32(len
, optlen
))
2530 return -TARGET_EFAULT
;
2537 case IP_ROUTER_ALERT
:
2541 case IP_MTU_DISCOVER
:
2547 case IP_MULTICAST_TTL
:
2548 case IP_MULTICAST_LOOP
:
2549 if (get_user_u32(len
, optlen
))
2550 return -TARGET_EFAULT
;
2552 return -TARGET_EINVAL
;
2554 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2557 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2559 if (put_user_u32(len
, optlen
)
2560 || put_user_u8(val
, optval_addr
))
2561 return -TARGET_EFAULT
;
2563 if (len
> sizeof(int))
2565 if (put_user_u32(len
, optlen
)
2566 || put_user_u32(val
, optval_addr
))
2567 return -TARGET_EFAULT
;
2571 ret
= -TARGET_ENOPROTOOPT
;
2577 case IPV6_MTU_DISCOVER
:
2580 case IPV6_RECVPKTINFO
:
2581 case IPV6_UNICAST_HOPS
:
2582 case IPV6_MULTICAST_HOPS
:
2583 case IPV6_MULTICAST_LOOP
:
2585 case IPV6_RECVHOPLIMIT
:
2586 case IPV6_2292HOPLIMIT
:
2589 case IPV6_2292PKTINFO
:
2590 case IPV6_RECVTCLASS
:
2591 case IPV6_RECVRTHDR
:
2592 case IPV6_2292RTHDR
:
2593 case IPV6_RECVHOPOPTS
:
2594 case IPV6_2292HOPOPTS
:
2595 case IPV6_RECVDSTOPTS
:
2596 case IPV6_2292DSTOPTS
:
2598 #ifdef IPV6_RECVPATHMTU
2599 case IPV6_RECVPATHMTU
:
2601 #ifdef IPV6_TRANSPARENT
2602 case IPV6_TRANSPARENT
:
2604 #ifdef IPV6_FREEBIND
2607 #ifdef IPV6_RECVORIGDSTADDR
2608 case IPV6_RECVORIGDSTADDR
:
2610 if (get_user_u32(len
, optlen
))
2611 return -TARGET_EFAULT
;
2613 return -TARGET_EINVAL
;
2615 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2618 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2620 if (put_user_u32(len
, optlen
)
2621 || put_user_u8(val
, optval_addr
))
2622 return -TARGET_EFAULT
;
2624 if (len
> sizeof(int))
2626 if (put_user_u32(len
, optlen
)
2627 || put_user_u32(val
, optval_addr
))
2628 return -TARGET_EFAULT
;
2632 ret
= -TARGET_ENOPROTOOPT
;
2639 case NETLINK_PKTINFO
:
2640 case NETLINK_BROADCAST_ERROR
:
2641 case NETLINK_NO_ENOBUFS
:
2642 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2643 case NETLINK_LISTEN_ALL_NSID
:
2644 case NETLINK_CAP_ACK
:
2645 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2646 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2647 case NETLINK_EXT_ACK
:
2648 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2649 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2650 case NETLINK_GET_STRICT_CHK
:
2651 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2652 if (get_user_u32(len
, optlen
)) {
2653 return -TARGET_EFAULT
;
2655 if (len
!= sizeof(val
)) {
2656 return -TARGET_EINVAL
;
2659 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2663 if (put_user_u32(lv
, optlen
)
2664 || put_user_u32(val
, optval_addr
)) {
2665 return -TARGET_EFAULT
;
2668 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2669 case NETLINK_LIST_MEMBERSHIPS
:
2673 if (get_user_u32(len
, optlen
)) {
2674 return -TARGET_EFAULT
;
2677 return -TARGET_EINVAL
;
2679 results
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 1);
2681 return -TARGET_EFAULT
;
2684 ret
= get_errno(getsockopt(sockfd
, level
, optname
, results
, &lv
));
2686 unlock_user(results
, optval_addr
, 0);
2689 /* swap host endianess to target endianess. */
2690 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
2691 results
[i
] = tswap32(results
[i
]);
2693 if (put_user_u32(lv
, optlen
)) {
2694 return -TARGET_EFAULT
;
2696 unlock_user(results
, optval_addr
, 0);
2699 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2704 #endif /* SOL_NETLINK */
2707 qemu_log_mask(LOG_UNIMP
,
2708 "getsockopt level=%d optname=%d not yet supported\n",
2710 ret
= -TARGET_EOPNOTSUPP
;
2716 /* Convert target low/high pair representing file offset into the host
2717 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2718 * as the kernel doesn't handle them either.
2720 static void target_to_host_low_high(abi_ulong tlow
,
2722 unsigned long *hlow
,
2723 unsigned long *hhigh
)
2725 uint64_t off
= tlow
|
2726 ((unsigned long long)thigh
<< TARGET_LONG_BITS
/ 2) <<
2727 TARGET_LONG_BITS
/ 2;
2730 *hhigh
= (off
>> HOST_LONG_BITS
/ 2) >> HOST_LONG_BITS
/ 2;
2733 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
2734 abi_ulong count
, int copy
)
2736 struct target_iovec
*target_vec
;
2738 abi_ulong total_len
, max_len
;
2741 bool bad_address
= false;
2747 if (count
> IOV_MAX
) {
2752 vec
= g_try_new0(struct iovec
, count
);
2758 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2759 count
* sizeof(struct target_iovec
), 1);
2760 if (target_vec
== NULL
) {
2765 /* ??? If host page size > target page size, this will result in a
2766 value larger than what we can actually support. */
2767 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
2770 for (i
= 0; i
< count
; i
++) {
2771 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2772 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2777 } else if (len
== 0) {
2778 /* Zero length pointer is ignored. */
2779 vec
[i
].iov_base
= 0;
2781 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
2782 /* If the first buffer pointer is bad, this is a fault. But
2783 * subsequent bad buffers will result in a partial write; this
2784 * is realized by filling the vector with null pointers and
2786 if (!vec
[i
].iov_base
) {
2797 if (len
> max_len
- total_len
) {
2798 len
= max_len
- total_len
;
2801 vec
[i
].iov_len
= len
;
2805 unlock_user(target_vec
, target_addr
, 0);
2810 if (tswapal(target_vec
[i
].iov_len
) > 0) {
2811 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
2814 unlock_user(target_vec
, target_addr
, 0);
2821 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
2822 abi_ulong count
, int copy
)
2824 struct target_iovec
*target_vec
;
2827 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2828 count
* sizeof(struct target_iovec
), 1);
2830 for (i
= 0; i
< count
; i
++) {
2831 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2832 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2836 unlock_user(vec
[i
].iov_base
, base
, copy ? vec
[i
].iov_len
: 0);
2838 unlock_user(target_vec
, target_addr
, 0);
2844 static inline int target_to_host_sock_type(int *type
)
2847 int target_type
= *type
;
2849 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
2850 case TARGET_SOCK_DGRAM
:
2851 host_type
= SOCK_DGRAM
;
2853 case TARGET_SOCK_STREAM
:
2854 host_type
= SOCK_STREAM
;
2857 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
2860 if (target_type
& TARGET_SOCK_CLOEXEC
) {
2861 #if defined(SOCK_CLOEXEC)
2862 host_type
|= SOCK_CLOEXEC
;
2864 return -TARGET_EINVAL
;
2867 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2868 #if defined(SOCK_NONBLOCK)
2869 host_type
|= SOCK_NONBLOCK
;
2870 #elif !defined(O_NONBLOCK)
2871 return -TARGET_EINVAL
;
2878 /* Try to emulate socket type flags after socket creation. */
2879 static int sock_flags_fixup(int fd
, int target_type
)
2881 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2882 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2883 int flags
= fcntl(fd
, F_GETFL
);
2884 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
2886 return -TARGET_EINVAL
;
2893 /* do_socket() Must return target values and target errnos. */
2894 static abi_long
do_socket(int domain
, int type
, int protocol
)
2896 int target_type
= type
;
2899 ret
= target_to_host_sock_type(&type
);
2904 if (domain
== PF_NETLINK
&& !(
2905 #ifdef CONFIG_RTNETLINK
2906 protocol
== NETLINK_ROUTE
||
2908 protocol
== NETLINK_KOBJECT_UEVENT
||
2909 protocol
== NETLINK_AUDIT
)) {
2910 return -EPFNOSUPPORT
;
2913 if (domain
== AF_PACKET
||
2914 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
2915 protocol
= tswap16(protocol
);
2918 ret
= get_errno(socket(domain
, type
, protocol
));
2920 ret
= sock_flags_fixup(ret
, target_type
);
2921 if (type
== SOCK_PACKET
) {
2922 /* Manage an obsolete case :
2923 * if socket type is SOCK_PACKET, bind by name
2925 fd_trans_register(ret
, &target_packet_trans
);
2926 } else if (domain
== PF_NETLINK
) {
2928 #ifdef CONFIG_RTNETLINK
2930 fd_trans_register(ret
, &target_netlink_route_trans
);
2933 case NETLINK_KOBJECT_UEVENT
:
2934 /* nothing to do: messages are strings */
2937 fd_trans_register(ret
, &target_netlink_audit_trans
);
2940 g_assert_not_reached();
2947 /* do_bind() Must return target values and target errnos. */
2948 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
2954 if ((int)addrlen
< 0) {
2955 return -TARGET_EINVAL
;
2958 addr
= alloca(addrlen
+1);
2960 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
2964 return get_errno(bind(sockfd
, addr
, addrlen
));
2967 /* do_connect() Must return target values and target errnos. */
2968 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
2974 if ((int)addrlen
< 0) {
2975 return -TARGET_EINVAL
;
2978 addr
= alloca(addrlen
+1);
2980 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
2984 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
2987 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
2988 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
2989 int flags
, int send
)
2995 abi_ulong target_vec
;
2997 if (msgp
->msg_name
) {
2998 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
2999 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3000 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3001 tswapal(msgp
->msg_name
),
3003 if (ret
== -TARGET_EFAULT
) {
3004 /* For connected sockets msg_name and msg_namelen must
3005 * be ignored, so returning EFAULT immediately is wrong.
3006 * Instead, pass a bad msg_name to the host kernel, and
3007 * let it decide whether to return EFAULT or not.
3009 msg
.msg_name
= (void *)-1;
3014 msg
.msg_name
= NULL
;
3015 msg
.msg_namelen
= 0;
3017 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3018 msg
.msg_control
= alloca(msg
.msg_controllen
);
3019 memset(msg
.msg_control
, 0, msg
.msg_controllen
);
3021 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3023 count
= tswapal(msgp
->msg_iovlen
);
3024 target_vec
= tswapal(msgp
->msg_iov
);
3026 if (count
> IOV_MAX
) {
3027 /* sendrcvmsg returns a different errno for this condition than
3028 * readv/writev, so we must catch it here before lock_iovec() does.
3030 ret
= -TARGET_EMSGSIZE
;
3034 vec
= lock_iovec(send ? VERIFY_READ
: VERIFY_WRITE
,
3035 target_vec
, count
, send
);
3037 ret
= -host_to_target_errno(errno
);
3040 msg
.msg_iovlen
= count
;
3044 if (fd_trans_target_to_host_data(fd
)) {
3047 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3048 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3049 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3050 msg
.msg_iov
->iov_len
);
3052 msg
.msg_iov
->iov_base
= host_msg
;
3053 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3057 ret
= target_to_host_cmsg(&msg
, msgp
);
3059 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3063 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3064 if (!is_error(ret
)) {
3066 if (fd_trans_host_to_target_data(fd
)) {
3067 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3068 MIN(msg
.msg_iov
->iov_len
, len
));
3070 ret
= host_to_target_cmsg(msgp
, &msg
);
3072 if (!is_error(ret
)) {
3073 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3074 msgp
->msg_flags
= tswap32(msg
.msg_flags
);
3075 if (msg
.msg_name
!= NULL
&& msg
.msg_name
!= (void *)-1) {
3076 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3077 msg
.msg_name
, msg
.msg_namelen
);
3089 unlock_iovec(vec
, target_vec
, count
, !send
);
3094 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3095 int flags
, int send
)
3098 struct target_msghdr
*msgp
;
3100 if (!lock_user_struct(send ? VERIFY_READ
: VERIFY_WRITE
,
3104 return -TARGET_EFAULT
;
3106 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3107 unlock_user_struct(msgp
, target_msg
, send ?
0 : 1);
3111 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3112 * so it might not have this *mmsg-specific flag either.
3114 #ifndef MSG_WAITFORONE
3115 #define MSG_WAITFORONE 0x10000
3118 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3119 unsigned int vlen
, unsigned int flags
,
3122 struct target_mmsghdr
*mmsgp
;
3126 if (vlen
> UIO_MAXIOV
) {
3130 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3132 return -TARGET_EFAULT
;
3135 for (i
= 0; i
< vlen
; i
++) {
3136 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3137 if (is_error(ret
)) {
3140 mmsgp
[i
].msg_len
= tswap32(ret
);
3141 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3142 if (flags
& MSG_WAITFORONE
) {
3143 flags
|= MSG_DONTWAIT
;
3147 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3149 /* Return number of datagrams sent if we sent any at all;
3150 * otherwise return the error.
3158 /* do_accept4() Must return target values and target errnos. */
3159 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3160 abi_ulong target_addrlen_addr
, int flags
)
3162 socklen_t addrlen
, ret_addrlen
;
3167 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
3169 if (target_addr
== 0) {
3170 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3173 /* linux returns EINVAL if addrlen pointer is invalid */
3174 if (get_user_u32(addrlen
, target_addrlen_addr
))
3175 return -TARGET_EINVAL
;
3177 if ((int)addrlen
< 0) {
3178 return -TARGET_EINVAL
;
3181 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3182 return -TARGET_EINVAL
;
3184 addr
= alloca(addrlen
);
3186 ret_addrlen
= addrlen
;
3187 ret
= get_errno(safe_accept4(fd
, addr
, &ret_addrlen
, host_flags
));
3188 if (!is_error(ret
)) {
3189 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3190 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3191 ret
= -TARGET_EFAULT
;
3197 /* do_getpeername() Must return target values and target errnos. */
3198 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3199 abi_ulong target_addrlen_addr
)
3201 socklen_t addrlen
, ret_addrlen
;
3205 if (get_user_u32(addrlen
, target_addrlen_addr
))
3206 return -TARGET_EFAULT
;
3208 if ((int)addrlen
< 0) {
3209 return -TARGET_EINVAL
;
3212 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3213 return -TARGET_EFAULT
;
3215 addr
= alloca(addrlen
);
3217 ret_addrlen
= addrlen
;
3218 ret
= get_errno(getpeername(fd
, addr
, &ret_addrlen
));
3219 if (!is_error(ret
)) {
3220 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3221 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3222 ret
= -TARGET_EFAULT
;
3228 /* do_getsockname() Must return target values and target errnos. */
3229 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3230 abi_ulong target_addrlen_addr
)
3232 socklen_t addrlen
, ret_addrlen
;
3236 if (get_user_u32(addrlen
, target_addrlen_addr
))
3237 return -TARGET_EFAULT
;
3239 if ((int)addrlen
< 0) {
3240 return -TARGET_EINVAL
;
3243 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3244 return -TARGET_EFAULT
;
3246 addr
= alloca(addrlen
);
3248 ret_addrlen
= addrlen
;
3249 ret
= get_errno(getsockname(fd
, addr
, &ret_addrlen
));
3250 if (!is_error(ret
)) {
3251 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3252 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3253 ret
= -TARGET_EFAULT
;
3259 /* do_socketpair() Must return target values and target errnos. */
3260 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3261 abi_ulong target_tab_addr
)
3266 target_to_host_sock_type(&type
);
3268 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
3269 if (!is_error(ret
)) {
3270 if (put_user_s32(tab
[0], target_tab_addr
)
3271 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
3272 ret
= -TARGET_EFAULT
;
3277 /* do_sendto() Must return target values and target errnos. */
3278 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
3279 abi_ulong target_addr
, socklen_t addrlen
)
3283 void *copy_msg
= NULL
;
3286 if ((int)addrlen
< 0) {
3287 return -TARGET_EINVAL
;
3290 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
3292 return -TARGET_EFAULT
;
3293 if (fd_trans_target_to_host_data(fd
)) {
3294 copy_msg
= host_msg
;
3295 host_msg
= g_malloc(len
);
3296 memcpy(host_msg
, copy_msg
, len
);
3297 ret
= fd_trans_target_to_host_data(fd
)(host_msg
, len
);
3303 addr
= alloca(addrlen
+1);
3304 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);