4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
29 #include <sys/mount.h>
31 #include <sys/fsuid.h>
32 #include <sys/personality.h>
33 #include <sys/prctl.h>
34 #include <sys/resource.h>
36 #include <linux/capability.h>
38 #include <sys/timex.h>
39 #include <sys/socket.h>
43 #include <sys/times.h>
46 #include <sys/statfs.h>
48 #include <sys/sysinfo.h>
49 #include <sys/signalfd.h>
50 //#include <sys/user.h>
51 #include <netinet/ip.h>
52 #include <netinet/tcp.h>
53 #include <linux/wireless.h>
54 #include <linux/icmp.h>
55 #include <linux/icmpv6.h>
56 #include <linux/errqueue.h>
57 #include <linux/random.h>
59 #include <sys/timerfd.h>
62 #include <sys/eventfd.h>
65 #include <sys/epoll.h>
68 #include "qemu/xattr.h"
70 #ifdef CONFIG_SENDFILE
71 #include <sys/sendfile.h>
74 #define termios host_termios
75 #define winsize host_winsize
76 #define termio host_termio
77 #define sgttyb host_sgttyb /* same as target */
78 #define tchars host_tchars /* same as target */
79 #define ltchars host_ltchars /* same as target */
81 #include <linux/termios.h>
82 #include <linux/unistd.h>
83 #include <linux/cdrom.h>
84 #include <linux/hdreg.h>
85 #include <linux/soundcard.h>
87 #include <linux/mtio.h>
89 #if defined(CONFIG_FIEMAP)
90 #include <linux/fiemap.h>
93 #if defined(CONFIG_USBFS)
94 #include <linux/usbdevice_fs.h>
95 #include <linux/usb/ch9.h>
98 #include <linux/dm-ioctl.h>
99 #include <linux/reboot.h>
100 #include <linux/route.h>
101 #include <linux/filter.h>
102 #include <linux/blkpg.h>
103 #include <netpacket/packet.h>
104 #include <linux/netlink.h>
105 #include <linux/if_alg.h>
106 #include "linux_loop.h"
110 #include "qemu/guest-random.h"
111 #include "qapi/error.h"
112 #include "fd-trans.h"
115 #define CLONE_IO 0x80000000 /* Clone io context */
118 /* We can't directly call the host clone syscall, because this will
119 * badly confuse libc (breaking mutexes, for example). So we must
120 * divide clone flags into:
121 * * flag combinations that look like pthread_create()
122 * * flag combinations that look like fork()
123 * * flags we can implement within QEMU itself
124 * * flags we can't support and will return an error for
126 /* For thread creation, all these flags must be present; for
127 * fork, none must be present.
129 #define CLONE_THREAD_FLAGS \
130 (CLONE_VM | CLONE_FS | CLONE_FILES | \
131 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
133 /* These flags are ignored:
134 * CLONE_DETACHED is now ignored by the kernel;
135 * CLONE_IO is just an optimisation hint to the I/O scheduler
137 #define CLONE_IGNORED_FLAGS \
138 (CLONE_DETACHED | CLONE_IO)
140 /* Flags for fork which we can implement within QEMU itself */
141 #define CLONE_OPTIONAL_FORK_FLAGS \
142 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
143 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
145 /* Flags for thread creation which we can implement within QEMU itself */
146 #define CLONE_OPTIONAL_THREAD_FLAGS \
147 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
148 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
150 #define CLONE_INVALID_FORK_FLAGS \
151 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
153 #define CLONE_INVALID_THREAD_FLAGS \
154 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
155 CLONE_IGNORED_FLAGS))
157 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
158 * have almost all been allocated. We cannot support any of
159 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
160 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
161 * The checks against the invalid thread masks above will catch these.
162 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
165 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
166 * once. This exercises the codepaths for restart.
168 //#define DEBUG_ERESTARTSYS
170 //#include <linux/msdos_fs.h>
171 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
172 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
182 #define _syscall0(type,name) \
183 static type name (void) \
185 return syscall(__NR_##name); \
188 #define _syscall1(type,name,type1,arg1) \
189 static type name (type1 arg1) \
191 return syscall(__NR_##name, arg1); \
194 #define _syscall2(type,name,type1,arg1,type2,arg2) \
195 static type name (type1 arg1,type2 arg2) \
197 return syscall(__NR_##name, arg1, arg2); \
200 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
201 static type name (type1 arg1,type2 arg2,type3 arg3) \
203 return syscall(__NR_##name, arg1, arg2, arg3); \
206 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
207 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
209 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
212 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
214 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
216 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
220 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
221 type5,arg5,type6,arg6) \
222 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
225 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
229 #define __NR_sys_uname __NR_uname
230 #define __NR_sys_getcwd1 __NR_getcwd
231 #define __NR_sys_getdents __NR_getdents
232 #define __NR_sys_getdents64 __NR_getdents64
233 #define __NR_sys_getpriority __NR_getpriority
234 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
235 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
236 #define __NR_sys_syslog __NR_syslog
237 #define __NR_sys_futex __NR_futex
238 #define __NR_sys_inotify_init __NR_inotify_init
239 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
240 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
241 #define __NR_sys_statx __NR_statx
243 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
244 #define __NR__llseek __NR_lseek
247 /* Newer kernel ports have llseek() instead of _llseek() */
248 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
249 #define TARGET_NR__llseek TARGET_NR_llseek
252 #define __NR_sys_gettid __NR_gettid
253 _syscall0(int, sys_gettid)
255 /* For the 64-bit guest on 32-bit host case we must emulate
256 * getdents using getdents64, because otherwise the host
257 * might hand us back more dirent records than we can fit
258 * into the guest buffer after structure format conversion.
259 * Otherwise we emulate getdents with getdents if the host has it.
261 #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
262 #define EMULATE_GETDENTS_WITH_GETDENTS
265 #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
266 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
268 #if (defined(TARGET_NR_getdents) && \
269 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
270 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
271 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
273 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
274 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
275 loff_t *, res, uint, wh);
277 _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
278 _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
280 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
281 #ifdef __NR_exit_group
282 _syscall1(int,exit_group,int,error_code)
284 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
285 _syscall1(int,set_tid_address,int *,tidptr)
287 #if defined(TARGET_NR_futex) && defined(__NR_futex)
288 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
289 const struct timespec *,timeout,int *,uaddr2,int,val3)
291 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
292 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
293 unsigned long *, user_mask_ptr);
294 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
295 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
296 unsigned long *, user_mask_ptr);
297 #define __NR_sys_getcpu __NR_getcpu
298 _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
299 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
301 _syscall2(int, capget, struct __user_cap_header_struct *, header,
302 struct __user_cap_data_struct *, data);
303 _syscall2(int, capset, struct __user_cap_header_struct *, header,
304 struct __user_cap_data_struct *, data);
305 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
306 _syscall2(int, ioprio_get, int, which, int, who)
308 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
309 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
311 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
312 _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
315 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
316 _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
317 unsigned long, idx1, unsigned long, idx2)
321 * It is assumed that struct statx is architecture independent.
323 #if defined(TARGET_NR_statx) && defined(__NR_statx)
324 _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
325 unsigned int, mask, struct target_statx *, statxbuf)
328 static bitmask_transtbl fcntl_flags_tbl[] = {
329 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
330 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
331 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
332 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
333 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
334 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
335 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
336 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
337 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
338 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
339 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
340 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
341 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
342 #if defined(O_DIRECT)
343 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
345 #if defined(O_NOATIME)
346 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
348 #if defined(O_CLOEXEC)
349 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
352 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
354 #if defined(O_TMPFILE)
355 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
357 /* Don't terminate the list prematurely on 64-bit host+guest. */
358 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
359 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
364 static int sys_getcwd1(char *buf, size_t size)
366 if (getcwd(buf, size) == NULL) {
367 /* getcwd() sets errno */
370 return strlen(buf)+1;
373 #ifdef TARGET_NR_utimensat
374 #if defined(__NR_utimensat)
375 #define __NR_sys_utimensat __NR_utimensat
376 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
377 const struct timespec *,tsp,int,flags)
379 static int sys_utimensat(int dirfd, const char *pathname,
380 const struct timespec times[2], int flags)
386 #endif /* TARGET_NR_utimensat */
388 #ifdef TARGET_NR_renameat2
389 #if defined(__NR_renameat2)
390 #define __NR_sys_renameat2 __NR_renameat2
391 _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
392 const char *, new, unsigned int, flags)
394 static int sys_renameat2(int oldfd, const char *old,
395 int newfd, const char *new, int flags)
398 return renameat(oldfd, old, newfd, new);
404 #endif /* TARGET_NR_renameat2 */
406 #ifdef CONFIG_INOTIFY
407 #include <sys/inotify.h>
409 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
410 static int sys_inotify_init(void)
412 return (inotify_init());
415 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
416 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
418 return (inotify_add_watch(fd, pathname, mask));
421 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
422 static int sys_inotify_rm_watch(int fd, int32_t wd)
424 return (inotify_rm_watch(fd, wd));
427 #ifdef CONFIG_INOTIFY1
428 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
429 static int sys_inotify_init1(int flags)
431 return (inotify_init1(flags));
436 /* Userspace can usually survive runtime without inotify */
437 #undef TARGET_NR_inotify_init
438 #undef TARGET_NR_inotify_init1
439 #undef TARGET_NR_inotify_add_watch
440 #undef TARGET_NR_inotify_rm_watch
441 #endif /* CONFIG_INOTIFY */
443 #if defined(TARGET_NR_prlimit64)
444 #ifndef __NR_prlimit64
445 # define __NR_prlimit64 -1
447 #define __NR_sys_prlimit64 __NR_prlimit64
448 /* The glibc rlimit structure may not be that used by the underlying syscall */
449 struct host_rlimit64 {
453 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
454 const struct host_rlimit64 *, new_limit,
455 struct host_rlimit64 *, old_limit)
459 #if defined(TARGET_NR_timer_create)
460 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
461 static timer_t g_posix_timers[32] = { 0, } ;
463 static inline int next_free_host_timer(void)
466 /* FIXME: Does finding the next free slot require a lock? */
467 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
468 if (g_posix_timers[k] == 0) {
469 g_posix_timers[k] = (timer_t) 1;
477 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
479 static inline int regpairs_aligned(void *cpu_env, int num)
481 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
483 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
484 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
485 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
486 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
487 * of registers which translates to the same as ARM/MIPS, because we start with
489 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
490 #elif defined(TARGET_SH4)
491 /* SH4 doesn't align register pairs, except for p{read,write}64 */
492 static inline int regpairs_aligned(void *cpu_env, int num)
495 case TARGET_NR_pread64:
496 case TARGET_NR_pwrite64:
503 #elif defined(TARGET_XTENSA)
504 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
506 static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
509 #define ERRNO_TABLE_SIZE 1200
511 /* target_to_host_errno_table[] is initialized from
512 * host_to_target_errno_table[] in syscall_init(). */
513 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
517 * This list is the union of errno values overridden in asm-<arch>/errno.h
518 * minus the errnos that are not actually generic to all archs.
520 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
521 [EAGAIN] = TARGET_EAGAIN,
522 [EIDRM] = TARGET_EIDRM,
523 [ECHRNG] = TARGET_ECHRNG,
524 [EL2NSYNC] = TARGET_EL2NSYNC,
525 [EL3HLT] = TARGET_EL3HLT,
526 [EL3RST] = TARGET_EL3RST,
527 [ELNRNG] = TARGET_ELNRNG,
528 [EUNATCH] = TARGET_EUNATCH,
529 [ENOCSI] = TARGET_ENOCSI,
530 [EL2HLT] = TARGET_EL2HLT,
531 [EDEADLK] = TARGET_EDEADLK,
532 [ENOLCK] = TARGET_ENOLCK,
533 [EBADE] = TARGET_EBADE,
534 [EBADR] = TARGET_EBADR,
535 [EXFULL] = TARGET_EXFULL,
536 [ENOANO] = TARGET_ENOANO,
537 [EBADRQC] = TARGET_EBADRQC,
538 [EBADSLT] = TARGET_EBADSLT,
539 [EBFONT] = TARGET_EBFONT,
540 [ENOSTR] = TARGET_ENOSTR,
541 [ENODATA] = TARGET_ENODATA,
542 [ETIME] = TARGET_ETIME,
543 [ENOSR] = TARGET_ENOSR,
544 [ENONET] = TARGET_ENONET,
545 [ENOPKG] = TARGET_ENOPKG,
546 [EREMOTE] = TARGET_EREMOTE,
547 [ENOLINK] = TARGET_ENOLINK,
548 [EADV] = TARGET_EADV,
549 [ESRMNT] = TARGET_ESRMNT,
550 [ECOMM] = TARGET_ECOMM,
551 [EPROTO] = TARGET_EPROTO,
552 [EDOTDOT] = TARGET_EDOTDOT,
553 [EMULTIHOP] = TARGET_EMULTIHOP,
554 [EBADMSG] = TARGET_EBADMSG,
555 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
556 [EOVERFLOW] = TARGET_EOVERFLOW,
557 [ENOTUNIQ] = TARGET_ENOTUNIQ,
558 [EBADFD] = TARGET_EBADFD,
559 [EREMCHG] = TARGET_EREMCHG,
560 [ELIBACC] = TARGET_ELIBACC,
561 [ELIBBAD] = TARGET_ELIBBAD,
562 [ELIBSCN] = TARGET_ELIBSCN,
563 [ELIBMAX] = TARGET_ELIBMAX,
564 [ELIBEXEC] = TARGET_ELIBEXEC,
565 [EILSEQ] = TARGET_EILSEQ,
566 [ENOSYS] = TARGET_ENOSYS,
567 [ELOOP] = TARGET_ELOOP,
568 [ERESTART] = TARGET_ERESTART,
569 [ESTRPIPE] = TARGET_ESTRPIPE,
570 [ENOTEMPTY] = TARGET_ENOTEMPTY,
571 [EUSERS] = TARGET_EUSERS,
572 [ENOTSOCK] = TARGET_ENOTSOCK,
573 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
574 [EMSGSIZE] = TARGET_EMSGSIZE,
575 [EPROTOTYPE] = TARGET_EPROTOTYPE,
576 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
577 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
578 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
579 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
580 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
581 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
582 [EADDRINUSE] = TARGET_EADDRINUSE,
583 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
584 [ENETDOWN] = TARGET_ENETDOWN,
585 [ENETUNREACH] = TARGET_ENETUNREACH,
586 [ENETRESET] = TARGET_ENETRESET,
587 [ECONNABORTED] = TARGET_ECONNABORTED,
588 [ECONNRESET] = TARGET_ECONNRESET,
589 [ENOBUFS] = TARGET_ENOBUFS,
590 [EISCONN] = TARGET_EISCONN,
591 [ENOTCONN] = TARGET_ENOTCONN,
592 [EUCLEAN] = TARGET_EUCLEAN,
593 [ENOTNAM] = TARGET_ENOTNAM,
594 [ENAVAIL] = TARGET_ENAVAIL,
595 [EISNAM] = TARGET_EISNAM,
596 [EREMOTEIO] = TARGET_EREMOTEIO,
597 [EDQUOT] = TARGET_EDQUOT,
598 [ESHUTDOWN] = TARGET_ESHUTDOWN,
599 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
600 [ETIMEDOUT] = TARGET_ETIMEDOUT,
601 [ECONNREFUSED] = TARGET_ECONNREFUSED,
602 [EHOSTDOWN] = TARGET_EHOSTDOWN,
603 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
604 [EALREADY] = TARGET_EALREADY,
605 [EINPROGRESS] = TARGET_EINPROGRESS,
606 [ESTALE] = TARGET_ESTALE,
607 [ECANCELED] = TARGET_ECANCELED,
608 [ENOMEDIUM] = TARGET_ENOMEDIUM,
609 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
611 [ENOKEY] = TARGET_ENOKEY,
614 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
617 [EKEYREVOKED] = TARGET_EKEYREVOKED,
620 [EKEYREJECTED] = TARGET_EKEYREJECTED,
623 [EOWNERDEAD] = TARGET_EOWNERDEAD,
625 #ifdef ENOTRECOVERABLE
626 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
629 [ENOMSG] = TARGET_ENOMSG,
632 [ERFKILL] = TARGET_ERFKILL,
635 [EHWPOISON] = TARGET_EHWPOISON,
639 static inline int host_to_target_errno(int err)
641 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
642 host_to_target_errno_table[err]) {
643 return host_to_target_errno_table[err];
648 static inline int target_to_host_errno(int err)
650 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
651 target_to_host_errno_table[err]) {
652 return target_to_host_errno_table[err];
657 static inline abi_long get_errno(abi_long ret)
660 return -host_to_target_errno(errno);
665 const char *target_strerror(int err)
667 if (err == TARGET_ERESTARTSYS) {
668 return "To be restarted";
670 if (err == TARGET_QEMU_ESIGRETURN) {
671 return "Successful exit from sigreturn";
674 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
677 return strerror(target_to_host_errno(err));
680 #define safe_syscall0(type, name) \
681 static type safe_##name(void) \
683 return safe_syscall(__NR_##name); \
686 #define safe_syscall1(type, name, type1, arg1) \
687 static type safe_##name(type1 arg1) \
689 return safe_syscall(__NR_##name, arg1); \
692 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
693 static type safe_##name(type1 arg1, type2 arg2) \
695 return safe_syscall(__NR_##name, arg1, arg2); \
698 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
699 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
701 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
704 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
706 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
708 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
711 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
712 type4, arg4, type5, arg5) \
713 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
716 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
719 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
720 type4, arg4, type5, arg5, type6, arg6) \
721 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
722 type5 arg5, type6 arg6) \
724 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
727 safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
728 safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
729 safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
730 int, flags, mode_t, mode)
731 safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
732 struct rusage *, rusage)
733 safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
734 int, options, struct rusage *, rusage)
735 safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
736 safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
737 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
738 safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
739 struct timespec *, tsp, const sigset_t *, sigmask,
741 safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
742 int, maxevents, int, timeout, const sigset_t *, sigmask,
744 safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
745 const struct timespec *,timeout,int *,uaddr2,int,val3)
746 safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
747 safe_syscall2(int, kill, pid_t, pid, int, sig)
748 safe_syscall2(int, tkill, int, tid, int, sig)
749 safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
750 safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
751 safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
752 safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
753 unsigned long, pos_l, unsigned long, pos_h)
754 safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
755 unsigned long, pos_l, unsigned long, pos_h)
756 safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
758 safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
759 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
760 safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
761 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
762 safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
763 safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
764 safe_syscall2(int, flock, int, fd, int, operation)
765 safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
766 const struct timespec *, uts, size_t, sigsetsize)
767 safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
769 safe_syscall2(int, nanosleep, const struct timespec *, req,
770 struct timespec *, rem)
771 #ifdef TARGET_NR_clock_nanosleep
772 safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
773 const struct timespec *, req, struct timespec *, rem)
776 safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
777 void *, ptr, long, fifth)
780 safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
784 safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
785 long, msgtype, int, flags)
787 #ifdef __NR_semtimedop
788 safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
789 unsigned, nsops, const struct timespec *, timeout)
791 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
792 safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
793 size_t, len, unsigned, prio, const struct timespec *, timeout)
794 safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
795 size_t, len, unsigned *, prio, const struct timespec *, timeout)
797 /* We do ioctl like this rather than via safe_syscall3 to preserve the
798 * "third argument might be integer or pointer or not present" behaviour of
801 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
802 /* Similarly for fcntl. Note that callers must always:
803 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
804 * use the flock64 struct rather than unsuffixed flock
805 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
808 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
810 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
813 static inline int host_to_target_sock_type(int host_type)
817 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
819 target_type = TARGET_SOCK_DGRAM;
822 target_type = TARGET_SOCK_STREAM;
825 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
829 #if defined(SOCK_CLOEXEC)
830 if (host_type & SOCK_CLOEXEC) {
831 target_type |= TARGET_SOCK_CLOEXEC;
835 #if defined(SOCK_NONBLOCK)
836 if (host_type & SOCK_NONBLOCK) {
837 target_type |= TARGET_SOCK_NONBLOCK;
844 static abi_ulong target_brk;
845 static abi_ulong target_original_brk;
846 static abi_ulong brk_page;
848 void target_set_brk(abi_ulong new_brk)
850 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
851 brk_page = HOST_PAGE_ALIGN(target_brk);
854 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
855 #define DEBUGF_BRK(message, args...)
857 /* do_brk() must return target values and target errnos. */
858 abi_long do_brk(abi_ulong new_brk)
860 abi_long mapped_addr;
861 abi_ulong new_alloc_size;
863 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
866 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
869 if (new_brk < target_original_brk) {
870 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
875 /* If the new brk is less than the highest page reserved to the
876 * target heap allocation, set it and we're almost done... */
877 if (new_brk <= brk_page) {
878 /* Heap contents are initialized to zero, as for anonymous
880 if (new_brk > target_brk) {
881 memset(g2h(target_brk), 0, new_brk - target_brk);
883 target_brk = new_brk;
884 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
888 /* We need to allocate more memory after the brk... Note that
889 * we don't use MAP_FIXED because that will map over the top of
890 * any existing mapping (like the one with the host libc or qemu
891 * itself); instead we treat "mapped but at wrong address" as
892 * a failure and unmap again.
894 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
895 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
896 PROT_READ|PROT_WRITE,
897 MAP_ANON|MAP_PRIVATE, 0, 0));
899 if (mapped_addr == brk_page) {
900 /* Heap contents are initialized to zero, as for anonymous
901 * mapped pages. Technically the new pages are already
902 * initialized to zero since they *are* anonymous mapped
903 * pages, however we have to take care with the contents that
904 * come from the remaining part of the previous page: it may
905 * contains garbage data due to a previous heap usage (grown
907 memset(g2h(target_brk), 0, brk_page - target_brk);
909 target_brk = new_brk;
910 brk_page = HOST_PAGE_ALIGN(target_brk);
911 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
914 } else if (mapped_addr != -1) {
915 /* Mapped but at wrong address, meaning there wasn't actually
916 * enough space for this brk.
918 target_munmap(mapped_addr, new_alloc_size);
920 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
923 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
926 #if defined(TARGET_ALPHA)
927 /* We (partially) emulate OSF/1 on Alpha, which requires we
928 return a proper errno, not an unchanged brk value. */
929 return -TARGET_ENOMEM;
931 /* For everything else, return the previous break. */
935 static inline abi_long copy_from_user_fdset(fd_set *fds,
936 abi_ulong target_fds_addr,
940 abi_ulong b, *target_fds;
942 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
943 if (!(target_fds = lock_user(VERIFY_READ,
945 sizeof(abi_ulong) * nw,
947 return -TARGET_EFAULT;
951 for (i = 0; i < nw; i++) {
952 /* grab the abi_ulong */
953 __get_user(b, &target_fds[i]);
954 for (j = 0; j < TARGET_ABI_BITS; j++) {
955 /* check the bit inside the abi_ulong */
962 unlock_user(target_fds, target_fds_addr, 0);
967 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
968 abi_ulong target_fds_addr,
971 if (target_fds_addr) {
972 if (copy_from_user_fdset(fds, target_fds_addr, n))
973 return -TARGET_EFAULT;
981 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
987 abi_ulong *target_fds;
989 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
990 if (!(target_fds = lock_user(VERIFY_WRITE,
992 sizeof(abi_ulong) * nw,
994 return -TARGET_EFAULT;
997 for (i = 0; i < nw; i++) {
999 for (j = 0; j < TARGET_ABI_BITS; j++) {
1000 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
1003 __put_user(v, &target_fds[i]);
1006 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
1011 #if defined(__alpha__)
1012 #define HOST_HZ 1024
1017 static inline abi_long host_to_target_clock_t(long ticks)
1019 #if HOST_HZ == TARGET_HZ
1022 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
1026 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
1027 const struct rusage *rusage)
1029 struct target_rusage *target_rusage;
1031 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
1032 return -TARGET_EFAULT;
1033 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
1034 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
1035 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
1036 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
1037 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
1038 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
1039 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
1040 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
1041 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
1042 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
1043 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
1044 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
1045 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
1046 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
1047 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
1048 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
1049 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
1050 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
1051 unlock_user_struct(target_rusage, target_addr, 1);
1056 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
1058 abi_ulong target_rlim_swap;
1061 target_rlim_swap = tswapal(target_rlim);
1062 if (target_rlim_swap == TARGET_RLIM_INFINITY)
1063 return RLIM_INFINITY;
1065 result = target_rlim_swap;
1066 if (target_rlim_swap != (rlim_t)result)
1067 return RLIM_INFINITY;
1072 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
1074 abi_ulong target_rlim_swap;
1077 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
1078 target_rlim_swap = TARGET_RLIM_INFINITY;
1080 target_rlim_swap = rlim;
1081 result = tswapal(target_rlim_swap);
1086 static inline int target_to_host_resource(int code)
1089 case TARGET_RLIMIT_AS:
1091 case TARGET_RLIMIT_CORE:
1093 case TARGET_RLIMIT_CPU:
1095 case TARGET_RLIMIT_DATA:
1097 case TARGET_RLIMIT_FSIZE:
1098 return RLIMIT_FSIZE;
1099 case TARGET_RLIMIT_LOCKS:
1100 return RLIMIT_LOCKS;
1101 case TARGET_RLIMIT_MEMLOCK:
1102 return RLIMIT_MEMLOCK;
1103 case TARGET_RLIMIT_MSGQUEUE:
1104 return RLIMIT_MSGQUEUE;
1105 case TARGET_RLIMIT_NICE:
1107 case TARGET_RLIMIT_NOFILE:
1108 return RLIMIT_NOFILE;
1109 case TARGET_RLIMIT_NPROC:
1110 return RLIMIT_NPROC;
1111 case TARGET_RLIMIT_RSS:
1113 case TARGET_RLIMIT_RTPRIO:
1114 return RLIMIT_RTPRIO;
1115 case TARGET_RLIMIT_SIGPENDING:
1116 return RLIMIT_SIGPENDING;
1117 case TARGET_RLIMIT_STACK:
1118 return RLIMIT_STACK;
1124 static inline abi_long copy_from_user_timeval(struct timeval *tv,
1125 abi_ulong target_tv_addr)
1127 struct target_timeval *target_tv;
1129 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
1130 return -TARGET_EFAULT;
1132 __get_user(tv->tv_sec, &target_tv->tv_sec);
1133 __get_user(tv->tv_usec, &target_tv->tv_usec);
1135 unlock_user_struct(target_tv, target_tv_addr, 0);
1140 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1141 const struct timeval *tv)
1143 struct target_timeval *target_tv;
1145 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
1146 return -TARGET_EFAULT;
1148 __put_user(tv->tv_sec, &target_tv->tv_sec);
1149 __put_user(tv->tv_usec, &target_tv->tv_usec);
1151 unlock_user_struct(target_tv, target_tv_addr, 1);
1156 static inline abi_long copy_from_user_timezone(struct timezone *tz,
1157 abi_ulong target_tz_addr)
1159 struct target_timezone *target_tz;
1161 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1162 return -TARGET_EFAULT;
1165 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1166 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1168 unlock_user_struct(target_tz, target_tz_addr, 0);
1173 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1176 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1177 abi_ulong target_mq_attr_addr)
1179 struct target_mq_attr *target_mq_attr;
1181 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1182 target_mq_attr_addr, 1))
1183 return -TARGET_EFAULT;
1185 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1186 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1187 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1188 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1190 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1195 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1196 const struct mq_attr *attr)
1198 struct target_mq_attr *target_mq_attr;
1200 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1201 target_mq_attr_addr, 0))
1202 return -TARGET_EFAULT;
1204 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1205 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1206 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1207 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1209 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1215 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1216 /* do_select() must return target values and target errnos. */
1217 static abi_long do_select(int n,
1218 abi_ulong rfd_addr, abi_ulong wfd_addr,
1219 abi_ulong efd_addr, abi_ulong target_tv_addr)
1221 fd_set rfds, wfds, efds;
1222 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1224 struct timespec ts, *ts_ptr;
1227 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1231 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1235 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1240 if (target_tv_addr) {
1241 if (copy_from_user_timeval(&tv, target_tv_addr))
1242 return -TARGET_EFAULT;
1243 ts.tv_sec = tv.tv_sec;
1244 ts.tv_nsec = tv.tv_usec * 1000;
1250 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1253 if (!is_error(ret)) {
1254 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1255 return -TARGET_EFAULT;
1256 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1257 return -TARGET_EFAULT;
1258 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1259 return -TARGET_EFAULT;
1261 if (target_tv_addr) {
1262 tv.tv_sec = ts.tv_sec;
1263 tv.tv_usec = ts.tv_nsec / 1000;
1264 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1265 return -TARGET_EFAULT;
1273 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1274 static abi_long do_old_select(abi_ulong arg1)
1276 struct target_sel_arg_struct *sel;
1277 abi_ulong inp, outp, exp, tvp;
1280 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1281 return -TARGET_EFAULT;
1284 nsel = tswapal(sel->n);
1285 inp = tswapal(sel->inp);
1286 outp = tswapal(sel->outp);
1287 exp = tswapal(sel->exp);
1288 tvp = tswapal(sel->tvp);
1290 unlock_user_struct(sel, arg1, 0);
1292 return do_select(nsel, inp, outp, exp, tvp);
1297 static abi_long do_pipe2(int host_pipe[], int flags)
1300 return pipe2(host_pipe, flags);
1306 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1307 int flags, int is_pipe2)
1311 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1314 return get_errno(ret);
1316 /* Several targets have special calling conventions for the original
1317 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1319 #if defined(TARGET_ALPHA)
1320 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1321 return host_pipe[0];
1322 #elif defined(TARGET_MIPS)
1323 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1324 return host_pipe[0];
1325 #elif defined(TARGET_SH4)
1326 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1327 return host_pipe[0];
1328 #elif defined(TARGET_SPARC)
1329 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1330 return host_pipe[0];
1334 if (put_user_s32(host_pipe[0], pipedes)
1335 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1336 return -TARGET_EFAULT;
1337 return get_errno(ret);
1340 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1341 abi_ulong target_addr,
1344 struct target_ip_mreqn *target_smreqn;
1346 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1348 return -TARGET_EFAULT;
1349 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1350 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1351 if (len == sizeof(struct target_ip_mreqn))
1352 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1353 unlock_user(target_smreqn, target_addr, 0);
1358 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
1359 abi_ulong target_addr,
1362 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1363 sa_family_t sa_family;
1364 struct target_sockaddr *target_saddr;
1366 if (fd_trans_target_to_host_addr(fd)) {
1367 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1370 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1372 return -TARGET_EFAULT;
1374 sa_family = tswap16(target_saddr->sa_family);
1376 /* Oops. The caller might send a incomplete sun_path; sun_path
1377 * must be terminated by \0 (see the manual page), but
1378 * unfortunately it is quite common to specify sockaddr_un
1379 * length as "strlen(x->sun_path)" while it should be
1380 * "strlen(...) + 1". We'll fix that here if needed.
1381 * Linux kernel has a similar feature.
1384 if (sa_family == AF_UNIX) {
1385 if (len < unix_maxlen && len > 0) {
1386 char *cp = (char*)target_saddr;
1388 if ( cp[len-1] && !cp[len] )
1391 if (len > unix_maxlen)
1395 memcpy(addr, target_saddr, len);
1396 addr->sa_family = sa_family;
1397 if (sa_family == AF_NETLINK) {
1398 struct sockaddr_nl *nladdr;
1400 nladdr = (struct sockaddr_nl *)addr;
1401 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1402 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1403 } else if (sa_family == AF_PACKET) {
1404 struct target_sockaddr_ll *lladdr;
1406 lladdr = (struct target_sockaddr_ll *)addr;
1407 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1408 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1410 unlock_user(target_saddr, target_addr, 0);
1415 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1416 struct sockaddr *addr,
1419 struct target_sockaddr *target_saddr;
1426 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1428 return -TARGET_EFAULT;
1429 memcpy(target_saddr, addr, len);
1430 if (len >= offsetof(struct target_sockaddr, sa_family) +
1431 sizeof(target_saddr->sa_family)) {
1432 target_saddr->sa_family = tswap16(addr->sa_family);
1434 if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) {
1435 struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
1436 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1437 target_nl->nl_groups = tswap32(target_nl->nl_groups);
1438 } else if (addr->sa_family == AF_PACKET) {
1439 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1440 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1441 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
1442 } else if (addr->sa_family == AF_INET6 &&
1443 len >= sizeof(struct target_sockaddr_in6)) {
1444 struct target_sockaddr_in6 *target_in6 =
1445 (struct target_sockaddr_in6 *)target_saddr;
1446 target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
1448 unlock_user(target_saddr, target_addr, len);
1453 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1454 struct target_msghdr *target_msgh)
1456 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1457 abi_long msg_controllen;
1458 abi_ulong target_cmsg_addr;
1459 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1460 socklen_t space = 0;
1462 msg_controllen = tswapal(target_msgh->msg_controllen);
1463 if (msg_controllen < sizeof (struct target_cmsghdr))
1465 target_cmsg_addr = tswapal(target_msgh->msg_control);
1466 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1467 target_cmsg_start = target_cmsg;
1469 return -TARGET_EFAULT;
1471 while (cmsg && target_cmsg) {
1472 void *data = CMSG_DATA(cmsg);
1473 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1475 int len = tswapal(target_cmsg->cmsg_len)
1476 - sizeof(struct target_cmsghdr);
1478 space += CMSG_SPACE(len);
1479 if (space > msgh->msg_controllen) {
1480 space -= CMSG_SPACE(len);
1481 /* This is a QEMU bug, since we allocated the payload
1482 * area ourselves (unlike overflow in host-to-target
1483 * conversion, which is just the guest giving us a buffer
1484 * that's too small). It can't happen for the payload types
1485 * we currently support; if it becomes an issue in future
1486 * we would need to improve our allocation strategy to
1487 * something more intelligent than "twice the size of the
1488 * target buffer we're reading from".
1490 gemu_log("Host cmsg overflow\n");
1494 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1495 cmsg->cmsg_level = SOL_SOCKET;
1497 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1499 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1500 cmsg->cmsg_len = CMSG_LEN(len);
1502 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1503 int *fd = (int *)data;
1504 int *target_fd = (int *)target_data;
1505 int i, numfds = len / sizeof(int);
1507 for (i = 0; i < numfds; i++) {
1508 __get_user(fd[i], target_fd + i);
1510 } else if (cmsg->cmsg_level == SOL_SOCKET
1511 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1512 struct ucred *cred = (struct ucred *)data;
1513 struct target_ucred *target_cred =
1514 (struct target_ucred *)target_data;
1516 __get_user(cred->pid, &target_cred->pid);
1517 __get_user(cred->uid, &target_cred->uid);
1518 __get_user(cred->gid, &target_cred->gid);
1520 gemu_log("Unsupported ancillary data: %d/%d\n",
1521 cmsg->cmsg_level, cmsg->cmsg_type);
1522 memcpy(data, target_data, len);
1525 cmsg = CMSG_NXTHDR(msgh, cmsg);
1526 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1529 unlock_user(target_cmsg, target_cmsg_addr, 0);
1531 msgh->msg_controllen = space;
1535 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1536 struct msghdr *msgh)
1538 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1539 abi_long msg_controllen;
1540 abi_ulong target_cmsg_addr;
1541 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1542 socklen_t space = 0;
1544 msg_controllen = tswapal(target_msgh->msg_controllen);
1545 if (msg_controllen < sizeof (struct target_cmsghdr))
1547 target_cmsg_addr = tswapal(target_msgh->msg_control);
1548 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1549 target_cmsg_start = target_cmsg;
1551 return -TARGET_EFAULT;
1553 while (cmsg && target_cmsg) {
1554 void *data = CMSG_DATA(cmsg);
1555 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1557 int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
1558 int tgt_len, tgt_space;
1560 /* We never copy a half-header but may copy half-data;
1561 * this is Linux's behaviour in put_cmsg(). Note that
1562 * truncation here is a guest problem (which we report
1563 * to the guest via the CTRUNC bit), unlike truncation
1564 * in target_to_host_cmsg, which is a QEMU bug.
1566 if (msg_controllen < sizeof(struct target_cmsghdr)) {
1567 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1571 if (cmsg->cmsg_level == SOL_SOCKET) {
1572 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1574 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1576 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1578 /* Payload types which need a different size of payload on
1579 * the target must adjust tgt_len here.
1582 switch (cmsg->cmsg_level) {
1584 switch (cmsg->cmsg_type) {
1586 tgt_len = sizeof(struct target_timeval);
1596 if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
1597 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1598 tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
1601 /* We must now copy-and-convert len bytes of payload
1602 * into tgt_len bytes of destination space. Bear in mind
1603 * that in both source and destination we may be dealing
1604 * with a truncated value!
1606 switch (cmsg->cmsg_level) {
1608 switch (cmsg->cmsg_type) {
1611 int *fd = (int *)data;
1612 int *target_fd = (int *)target_data;
1613 int i, numfds = tgt_len / sizeof(int);
1615 for (i = 0; i < numfds; i++) {
1616 __put_user(fd[i], target_fd + i);
1622 struct timeval *tv = (struct timeval *)data;
1623 struct target_timeval *target_tv =
1624 (struct target_timeval *)target_data;
1626 if (len != sizeof(struct timeval) ||
1627 tgt_len != sizeof(struct target_timeval)) {
1631 /* copy struct timeval to target */
1632 __put_user(tv->tv_sec, &target_tv->tv_sec);
1633 __put_user(tv->tv_usec, &target_tv->tv_usec);
1636 case SCM_CREDENTIALS:
1638 struct ucred *cred = (struct ucred *)data;
1639 struct target_ucred *target_cred =
1640 (struct target_ucred *)target_data;
1642 __put_user(cred->pid, &target_cred->pid);
1643 __put_user(cred->uid, &target_cred->uid);
1644 __put_user(cred->gid, &target_cred->gid);
1653 switch (cmsg->cmsg_type) {
1656 uint32_t *v = (uint32_t *)data;
1657 uint32_t *t_int = (uint32_t *)target_data;
1659 if (len != sizeof(uint32_t) ||
1660 tgt_len != sizeof(uint32_t)) {
1663 __put_user(*v, t_int);
1669 struct sock_extended_err ee;
1670 struct sockaddr_in offender;
1672 struct errhdr_t *errh = (struct errhdr_t *)data;
1673 struct errhdr_t *target_errh =
1674 (struct errhdr_t *)target_data;
1676 if (len != sizeof(struct errhdr_t) ||
1677 tgt_len != sizeof(struct errhdr_t)) {
1680 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
1681 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
1682 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
1683 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
1684 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
1685 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
1686 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
1687 host_to_target_sockaddr((unsigned long) &target_errh->offender,
1688 (void *) &errh->offender, sizeof(errh->offender));
1697 switch (cmsg->cmsg_type) {
1700 uint32_t *v = (uint32_t *)data;
1701 uint32_t *t_int = (uint32_t *)target_data;
1703 if (len != sizeof(uint32_t) ||
1704 tgt_len != sizeof(uint32_t)) {
1707 __put_user(*v, t_int);
1713 struct sock_extended_err ee;
1714 struct sockaddr_in6 offender;
1716 struct errhdr6_t *errh = (struct errhdr6_t *)data;
1717 struct errhdr6_t *target_errh =
1718 (struct errhdr6_t *)target_data;
1720 if (len != sizeof(struct errhdr6_t) ||
1721 tgt_len != sizeof(struct errhdr6_t)) {
1724 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
1725 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
1726 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
1727 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
1728 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
1729 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
1730 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
1731 host_to_target_sockaddr((unsigned long) &target_errh->offender,
1732 (void *) &errh->offender, sizeof(errh->offender));
1742 gemu_log("Unsupported ancillary data: %d/%d\n",
1743 cmsg->cmsg_level, cmsg->cmsg_type);
1744 memcpy(target_data, data, MIN(len, tgt_len));
1745 if (tgt_len > len) {
1746 memset(target_data + len, 0, tgt_len - len);
1750 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
1751 tgt_space = TARGET_CMSG_SPACE(tgt_len);
1752 if (msg_controllen < tgt_space) {
1753 tgt_space = msg_controllen;
1755 msg_controllen -= tgt_space;
1757 cmsg = CMSG_NXTHDR(msgh, cmsg);
1758 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1761 unlock_user(target_cmsg, target_cmsg_addr, space);
1763 target_msgh->msg_controllen = tswapal(space);
1767 /* do_setsockopt() Must return target values and target errnos. */
1768 static abi_long do_setsockopt(int sockfd, int level, int optname,
1769 abi_ulong optval_addr, socklen_t optlen)
1773 struct ip_mreqn *ip_mreq;
1774 struct ip_mreq_source *ip_mreq_source;
1778 /* TCP options all take an 'int' value. */
1779 if (optlen < sizeof(uint32_t))
1780 return -TARGET_EINVAL;
1782 if (get_user_u32(val, optval_addr))
1783 return -TARGET_EFAULT;
1784 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1791 case IP_ROUTER_ALERT:
1795 case IP_MTU_DISCOVER:
1802 case IP_MULTICAST_TTL:
1803 case IP_MULTICAST_LOOP:
1805 if (optlen >= sizeof(uint32_t)) {
1806 if (get_user_u32(val, optval_addr))
1807 return -TARGET_EFAULT;
1808 } else if (optlen >= 1) {
1809 if (get_user_u8(val, optval_addr))
1810 return -TARGET_EFAULT;
1812 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1814 case IP_ADD_MEMBERSHIP:
1815 case IP_DROP_MEMBERSHIP:
1816 if (optlen < sizeof (struct target_ip_mreq) ||
1817 optlen > sizeof (struct target_ip_mreqn))
1818 return -TARGET_EINVAL;
1820 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1821 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1822 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1825 case IP_BLOCK_SOURCE:
1826 case IP_UNBLOCK_SOURCE:
1827 case IP_ADD_SOURCE_MEMBERSHIP:
1828 case IP_DROP_SOURCE_MEMBERSHIP:
1829 if (optlen != sizeof (struct target_ip_mreq_source))
1830 return -TARGET_EINVAL;
1832 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1833 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1834 unlock_user (ip_mreq_source, optval_addr, 0);
1843 case IPV6_MTU_DISCOVER:
1846 case IPV6_RECVPKTINFO:
1847 case IPV6_UNICAST_HOPS:
1848 case IPV6_MULTICAST_HOPS:
1849 case IPV6_MULTICAST_LOOP:
1851 case IPV6_RECVHOPLIMIT:
1852 case IPV6_2292HOPLIMIT:
1855 case IPV6_2292PKTINFO:
1856 case IPV6_RECVTCLASS:
1857 case IPV6_RECVRTHDR:
1858 case IPV6_2292RTHDR:
1859 case IPV6_RECVHOPOPTS:
1860 case IPV6_2292HOPOPTS:
1861 case IPV6_RECVDSTOPTS:
1862 case IPV6_2292DSTOPTS:
1864 #ifdef IPV6_RECVPATHMTU
1865 case IPV6_RECVPATHMTU:
1867 #ifdef IPV6_TRANSPARENT
1868 case IPV6_TRANSPARENT:
1870 #ifdef IPV6_FREEBIND
1873 #ifdef IPV6_RECVORIGDSTADDR
1874 case IPV6_RECVORIGDSTADDR:
1877 if (optlen < sizeof(uint32_t)) {
1878 return -TARGET_EINVAL;
1880 if (get_user_u32(val, optval_addr)) {
1881 return -TARGET_EFAULT;
1883 ret = get_errno(setsockopt(sockfd, level, optname,
1884 &val, sizeof(val)));
1888 struct in6_pktinfo pki;
1890 if (optlen < sizeof(pki)) {
1891 return -TARGET_EINVAL;
1894 if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
1895 return -TARGET_EFAULT;
1898 pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
1900 ret = get_errno(setsockopt(sockfd, level, optname,
1901 &pki, sizeof(pki)));
1904 case IPV6_ADD_MEMBERSHIP:
1905 case IPV6_DROP_MEMBERSHIP:
1907 struct ipv6_mreq ipv6mreq;
1909 if (optlen < sizeof(ipv6mreq)) {
1910 return -TARGET_EINVAL;
1913 if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
1914 return -TARGET_EFAULT;
1917 ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
1919 ret = get_errno(setsockopt(sockfd, level, optname,
1920 &ipv6mreq, sizeof(ipv6mreq)));
1931 struct icmp6_filter icmp6f;
1933 if (optlen > sizeof(icmp6f)) {
1934 optlen = sizeof(icmp6f);
1937 if (copy_from_user(&icmp6f, optval_addr, optlen)) {
1938 return -TARGET_EFAULT;
1941 for (val = 0; val < 8; val++) {
1942 icmp6f.data[val] = tswap32(icmp6f.data[val]);
1945 ret = get_errno(setsockopt(sockfd, level, optname,
1957 /* those take an u32 value */
1958 if (optlen < sizeof(uint32_t)) {
1959 return -TARGET_EINVAL;
1962 if (get_user_u32(val, optval_addr)) {
1963 return -TARGET_EFAULT;
1965 ret = get_errno(setsockopt(sockfd, level, optname,
1966 &val, sizeof(val)));
1973 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
1978 char *alg_key = g_malloc(optlen);
1981 return -TARGET_ENOMEM;
1983 if (copy_from_user(alg_key, optval_addr, optlen)) {
1985 return -TARGET_EFAULT;
1987 ret = get_errno(setsockopt(sockfd, level, optname,
1992 case ALG_SET_AEAD_AUTHSIZE:
1994 ret = get_errno(setsockopt(sockfd, level, optname,
2003 case TARGET_SOL_SOCKET:
2005 case TARGET_SO_RCVTIMEO:
2009 optname = SO_RCVTIMEO;
2012 if (optlen != sizeof(struct target_timeval)) {
2013 return -TARGET_EINVAL;
2016 if (copy_from_user_timeval(&tv, optval_addr)) {
2017 return -TARGET_EFAULT;
2020 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2024 case TARGET_SO_SNDTIMEO:
2025 optname = SO_SNDTIMEO;
2027 case TARGET_SO_ATTACH_FILTER:
2029 struct target_sock_fprog *tfprog;
2030 struct target_sock_filter *tfilter;
2031 struct sock_fprog fprog;
2032 struct sock_filter *filter;
2035 if (optlen != sizeof(*tfprog)) {
2036 return -TARGET_EINVAL;
2038 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2039 return -TARGET_EFAULT;
2041 if (!lock_user_struct(VERIFY_READ, tfilter,
2042 tswapal(tfprog->filter), 0)) {
2043 unlock_user_struct(tfprog, optval_addr, 1);
2044 return -TARGET_EFAULT;
2047 fprog.len = tswap16(tfprog->len);
2048 filter = g_try_new(struct sock_filter, fprog.len);
2049 if (filter == NULL) {
2050 unlock_user_struct(tfilter, tfprog->filter, 1);
2051 unlock_user_struct(tfprog, optval_addr, 1);
2052 return -TARGET_ENOMEM;
2054 for (i = 0; i < fprog.len; i++) {
2055 filter[i].code = tswap16(tfilter[i].code);
2056 filter[i].jt = tfilter[i].jt;
2057 filter[i].jf = tfilter[i].jf;
2058 filter[i].k = tswap32(tfilter[i].k);
2060 fprog.filter = filter;
2062 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2063 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
2066 unlock_user_struct(tfilter, tfprog->filter, 1);
2067 unlock_user_struct(tfprog, optval_addr, 1);
2070 case TARGET_SO_BINDTODEVICE:
2072 char *dev_ifname, *addr_ifname;
2074 if (optlen > IFNAMSIZ - 1) {
2075 optlen = IFNAMSIZ - 1;
2077 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2079 return -TARGET_EFAULT;
2081 optname = SO_BINDTODEVICE;
2082 addr_ifname = alloca(IFNAMSIZ);
2083 memcpy(addr_ifname, dev_ifname, optlen);
2084 addr_ifname[optlen] = 0;
2085 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2086 addr_ifname, optlen));
2087 unlock_user (dev_ifname, optval_addr, 0);
2090 case TARGET_SO_LINGER:
2093 struct target_linger *tlg;
2095 if (optlen != sizeof(struct target_linger)) {
2096 return -TARGET_EINVAL;
2098 if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
2099 return -TARGET_EFAULT;
2101 __get_user(lg.l_onoff, &tlg->l_onoff);
2102 __get_user(lg.l_linger, &tlg->l_linger);
2103 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
2105 unlock_user_struct(tlg, optval_addr, 0);
2108 /* Options with 'int' argument. */
2109 case TARGET_SO_DEBUG:
2112 case TARGET_SO_REUSEADDR:
2113 optname = SO_REUSEADDR;
2116 case TARGET_SO_REUSEPORT:
2117 optname = SO_REUSEPORT;
2120 case TARGET_SO_TYPE:
2123 case TARGET_SO_ERROR:
2126 case TARGET_SO_DONTROUTE:
2127 optname = SO_DONTROUTE;
2129 case TARGET_SO_BROADCAST:
2130 optname = SO_BROADCAST;
2132 case TARGET_SO_SNDBUF:
2133 optname = SO_SNDBUF;
2135 case TARGET_SO_SNDBUFFORCE:
2136 optname = SO_SNDBUFFORCE;
2138 case TARGET_SO_RCVBUF:
2139 optname = SO_RCVBUF;
2141 case TARGET_SO_RCVBUFFORCE:
2142 optname = SO_RCVBUFFORCE;
2144 case TARGET_SO_KEEPALIVE:
2145 optname = SO_KEEPALIVE;
2147 case TARGET_SO_OOBINLINE:
2148 optname = SO_OOBINLINE;
2150 case TARGET_SO_NO_CHECK:
2151 optname = SO_NO_CHECK;
2153 case TARGET_SO_PRIORITY:
2154 optname = SO_PRIORITY;
2157 case TARGET_SO_BSDCOMPAT:
2158 optname = SO_BSDCOMPAT;
2161 case TARGET_SO_PASSCRED:
2162 optname = SO_PASSCRED;
2164 case TARGET_SO_PASSSEC:
2165 optname = SO_PASSSEC;
2167 case TARGET_SO_TIMESTAMP:
2168 optname = SO_TIMESTAMP;
2170 case TARGET_SO_RCVLOWAT:
2171 optname = SO_RCVLOWAT;
2176 if (optlen < sizeof(uint32_t))
2177 return -TARGET_EINVAL;
2179 if (get_user_u32(val, optval_addr))
2180 return -TARGET_EFAULT;
2181 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
2185 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
2186 ret = -TARGET_ENOPROTOOPT;
2191 /* do_getsockopt() Must return target values and target errnos. */
2192 static abi_long do_getsockopt(int sockfd, int level, int optname,
2193 abi_ulong optval_addr, abi_ulong optlen)
2200 case TARGET_SOL_SOCKET:
2203 /* These don't just return a single integer */
2204 case TARGET_SO_RCVTIMEO:
2205 case TARGET_SO_SNDTIMEO:
2206 case TARGET_SO_PEERNAME:
2208 case TARGET_SO_PEERCRED: {
2211 struct target_ucred *tcr;
2213 if (get_user_u32(len, optlen)) {
2214 return -TARGET_EFAULT;
2217 return -TARGET_EINVAL;
2221 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2229 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2230 return -TARGET_EFAULT;
2232 __put_user(cr.pid, &tcr->pid);
2233 __put_user(cr.uid, &tcr->uid);
2234 __put_user(cr.gid, &tcr->gid);
2235 unlock_user_struct(tcr, optval_addr, 1);
2236 if (put_user_u32(len, optlen)) {
2237 return -TARGET_EFAULT;
2241 case TARGET_SO_LINGER:
2245 struct target_linger *tlg;
2247 if (get_user_u32(len, optlen)) {
2248 return -TARGET_EFAULT;
2251 return -TARGET_EINVAL;
2255 ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
2263 if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
2264 return -TARGET_EFAULT;
2266 __put_user(lg.l_onoff, &tlg->l_onoff);
2267 __put_user(lg.l_linger, &tlg->l_linger);
2268 unlock_user_struct(tlg, optval_addr, 1);
2269 if (put_user_u32(len, optlen)) {
2270 return -TARGET_EFAULT;
2274 /* Options with 'int' argument. */
2275 case TARGET_SO_DEBUG:
2278 case TARGET_SO_REUSEADDR:
2279 optname = SO_REUSEADDR;
2282 case TARGET_SO_REUSEPORT:
2283 optname = SO_REUSEPORT;
2286 case TARGET_SO_TYPE:
2289 case TARGET_SO_ERROR:
2292 case TARGET_SO_DONTROUTE:
2293 optname = SO_DONTROUTE;
2295 case TARGET_SO_BROADCAST:
2296 optname = SO_BROADCAST;
2298 case TARGET_SO_SNDBUF:
2299 optname = SO_SNDBUF;
2301 case TARGET_SO_RCVBUF:
2302 optname = SO_RCVBUF;
2304 case TARGET_SO_KEEPALIVE:
2305 optname = SO_KEEPALIVE;
2307 case TARGET_SO_OOBINLINE:
2308 optname = SO_OOBINLINE;
2310 case TARGET_SO_NO_CHECK:
2311 optname = SO_NO_CHECK;
2313 case TARGET_SO_PRIORITY:
2314 optname = SO_PRIORITY;
2317 case TARGET_SO_BSDCOMPAT:
2318 optname = SO_BSDCOMPAT;
2321 case TARGET_SO_PASSCRED:
2322 optname = SO_PASSCRED;
2324 case TARGET_SO_TIMESTAMP:
2325 optname = SO_TIMESTAMP;
2327 case TARGET_SO_RCVLOWAT:
2328 optname = SO_RCVLOWAT;
2330 case TARGET_SO_ACCEPTCONN:
2331 optname = SO_ACCEPTCONN;
2338 /* TCP options all take an 'int' value. */
2340 if (get_user_u32(len, optlen))
2341 return -TARGET_EFAULT;
2343 return -TARGET_EINVAL;
2345 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2348 if (optname == SO_TYPE) {
2349 val = host_to_target_sock_type(val);
2354 if (put_user_u32(val, optval_addr))
2355 return -TARGET_EFAULT;
2357 if (put_user_u8(val, optval_addr))
2358 return -TARGET_EFAULT;
2360 if (put_user_u32(len, optlen))
2361 return -TARGET_EFAULT;
2368 case IP_ROUTER_ALERT:
2372 case IP_MTU_DISCOVER:
2378 case IP_MULTICAST_TTL:
2379 case IP_MULTICAST_LOOP:
2380 if (get_user_u32(len, optlen))
2381 return -TARGET_EFAULT;
2383 return -TARGET_EINVAL;
2385 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2388 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2390 if (put_user_u32(len, optlen)
2391 || put_user_u8(val, optval_addr))
2392 return -TARGET_EFAULT;
2394 if (len > sizeof(int))
2396 if (put_user_u32(len, optlen)
2397 || put_user_u32(val, optval_addr))
2398 return -TARGET_EFAULT;
2402 ret = -TARGET_ENOPROTOOPT;
2408 case IPV6_MTU_DISCOVER:
2411 case IPV6_RECVPKTINFO:
2412 case IPV6_UNICAST_HOPS:
2413 case IPV6_MULTICAST_HOPS:
2414 case IPV6_MULTICAST_LOOP:
2416 case IPV6_RECVHOPLIMIT:
2417 case IPV6_2292HOPLIMIT:
2420 case IPV6_2292PKTINFO:
2421 case IPV6_RECVTCLASS:
2422 case IPV6_RECVRTHDR:
2423 case IPV6_2292RTHDR:
2424 case IPV6_RECVHOPOPTS:
2425 case IPV6_2292HOPOPTS:
2426 case IPV6_RECVDSTOPTS:
2427 case IPV6_2292DSTOPTS:
2429 #ifdef IPV6_RECVPATHMTU
2430 case IPV6_RECVPATHMTU:
2432 #ifdef IPV6_TRANSPARENT
2433 case IPV6_TRANSPARENT:
2435 #ifdef IPV6_FREEBIND
2438 #ifdef IPV6_RECVORIGDSTADDR
2439 case IPV6_RECVORIGDSTADDR:
2441 if (get_user_u32(len, optlen))
2442 return -TARGET_EFAULT;
2444 return -TARGET_EINVAL;
2446 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2449 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2451 if (put_user_u32(len, optlen)
2452 || put_user_u8(val, optval_addr))
2453 return -TARGET_EFAULT;
2455 if (len > sizeof(int))
2457 if (put_user_u32(len, optlen)
2458 || put_user_u32(val, optval_addr))
2459 return -TARGET_EFAULT;
2463 ret = -TARGET_ENOPROTOOPT;
2469 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
2471 ret = -TARGET_EOPNOTSUPP;
2477 /* Convert target low/high pair representing file offset into the host
2478 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2479 * as the kernel doesn't handle them either.
2481 static void target_to_host_low_high(abi_ulong tlow,
2483 unsigned long *hlow,
2484 unsigned long *hhigh)
2486 uint64_t off = tlow |
2487 ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
2488 TARGET_LONG_BITS / 2;
2491 *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
2494 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
2495 abi_ulong count, int copy)
2497 struct target_iovec *target_vec;
2499 abi_ulong total_len, max_len;
2502 bool bad_address = false;
2508 if (count > IOV_MAX) {
2513 vec = g_try_new0(struct iovec, count);
2519 target_vec = lock_user(VERIFY_READ, target_addr,
2520 count * sizeof(struct target_iovec), 1);
2521 if (target_vec == NULL) {
2526 /* ??? If host page size > target page size, this will result in a
2527 value larger than what we can actually support. */
2528 max_len = 0x7fffffff & TARGET_PAGE_MASK;
2531 for (i = 0; i < count; i++) {
2532 abi_ulong base = tswapal(target_vec[i].iov_base);
2533 abi_long len = tswapal(target_vec[i].iov_len);
2538 } else if (len == 0) {
2539 /* Zero length pointer is ignored. */
2540 vec[i].iov_base = 0;
2542 vec[i].iov_base = lock_user(type, base, len, copy);
2543 /* If the first buffer pointer is bad, this is a fault. But
2544 * subsequent bad buffers will result in a partial write; this
2545 * is realized by filling the vector with null pointers and
2547 if (!vec[i].iov_base) {
2558 if (len > max_len - total_len) {
2559 len = max_len - total_len;
2562 vec[i].iov_len = len;
2566 unlock_user(target_vec, target_addr, 0);
2571 if (tswapal(target_vec[i].iov_len) > 0) {
2572 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
2575 unlock_user(target_vec, target_addr, 0);
2582 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
2583 abi_ulong count, int copy)
2585 struct target_iovec *target_vec;
2588 target_vec = lock_user(VERIFY_READ, target_addr,
2589 count * sizeof(struct target_iovec), 1);
2591 for (i = 0; i < count; i++) {
2592 abi_ulong base = tswapal(target_vec[i].iov_base);
2593 abi_long len = tswapal(target_vec[i].iov_len);
2597 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
2599 unlock_user(target_vec, target_addr, 0);
2605 static inline int target_to_host_sock_type(int *type)
2608 int target_type = *type;
2610 switch (target_type & TARGET_SOCK_TYPE_MASK) {
2611 case TARGET_SOCK_DGRAM:
2612 host_type = SOCK_DGRAM;
2614 case TARGET_SOCK_STREAM:
2615 host_type = SOCK_STREAM;
2618 host_type = target_type & TARGET_SOCK_TYPE_MASK;
2621 if (target_type & TARGET_SOCK_CLOEXEC) {
2622 #if defined(SOCK_CLOEXEC)
2623 host_type |= SOCK_CLOEXEC;
2625 return -TARGET_EINVAL;
2628 if (target_type & TARGET_SOCK_NONBLOCK) {
2629 #if defined(SOCK_NONBLOCK)
2630 host_type |= SOCK_NONBLOCK;
2631 #elif !defined(O_NONBLOCK)
2632 return -TARGET_EINVAL;
2639 /* Try to emulate socket type flags after socket creation. */
2640 static int sock_flags_fixup(int fd, int target_type)
2642 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2643 if (target_type & TARGET_SOCK_NONBLOCK) {
2644 int flags = fcntl(fd, F_GETFL);
2645 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
2647 return -TARGET_EINVAL;
2654 /* do_socket() Must return target values and target errnos. */
2655 static abi_long do_socket(int domain, int type, int protocol)
2657 int target_type = type;
2660 ret = target_to_host_sock_type(&type);
2665 if (domain == PF_NETLINK && !(
2666 #ifdef CONFIG_RTNETLINK
2667 protocol == NETLINK_ROUTE ||
2669 protocol == NETLINK_KOBJECT_UEVENT ||
2670 protocol == NETLINK_AUDIT)) {
2671 return -EPFNOSUPPORT;
2674 if (domain == AF_PACKET ||
2675 (domain == AF_INET && type == SOCK_PACKET)) {
2676 protocol = tswap16(protocol);
2679 ret = get_errno(socket(domain, type, protocol));
2681 ret = sock_flags_fixup(ret, target_type);
2682 if (type == SOCK_PACKET) {
2683 /* Manage an obsolete case :
2684 * if socket type is SOCK_PACKET, bind by name
2686 fd_trans_register(ret, &target_packet_trans);
2687 } else if (domain == PF_NETLINK) {
2689 #ifdef CONFIG_RTNETLINK
2691 fd_trans_register(ret, &target_netlink_route_trans);
2694 case NETLINK_KOBJECT_UEVENT:
2695 /* nothing to do: messages are strings */
2698 fd_trans_register(ret, &target_netlink_audit_trans);
2701 g_assert_not_reached();
2708 /* do_bind() Must return target values and target errnos. */
2709 static abi_long do_bind(int sockfd, abi_ulong target_addr,
2715 if ((int)addrlen < 0) {
2716 return -TARGET_EINVAL;
2719 addr = alloca(addrlen+1);
2721 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
2725 return get_errno(bind(sockfd, addr, addrlen));
2728 /* do_connect() Must return target values and target errnos. */
2729 static abi_long do_connect(int sockfd, abi_ulong target_addr,
2735 if ((int)addrlen < 0) {
2736 return -TARGET_EINVAL;
2739 addr = alloca(addrlen+1);
2741 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
2745 return get_errno(safe_connect(sockfd, addr, addrlen));
2748 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
2749 static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
2750 int flags, int send)
2756 abi_ulong target_vec;
2758 if (msgp->msg_name) {
2759 msg.msg_namelen = tswap32(msgp->msg_namelen);
2760 msg.msg_name = alloca(msg.msg_namelen+1);
2761 ret = target_to_host_sockaddr(fd, msg.msg_name,
2762 tswapal(msgp->msg_name),
2764 if (ret == -TARGET_EFAULT) {
2765 /* For connected sockets msg_name and msg_namelen must
2766 * be ignored, so returning EFAULT immediately is wrong.
2767 * Instead, pass a bad msg_name to the host kernel, and
2768 * let it decide whether to return EFAULT or not.
2770 msg.msg_name = (void *)-1;
2775 msg.msg_name = NULL;
2776 msg.msg_namelen = 0;
2778 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
2779 msg.msg_control = alloca(msg.msg_controllen);
2780 memset(msg.msg_control, 0, msg.msg_controllen);
2782 msg.msg_flags = tswap32(msgp->msg_flags);
2784 count = tswapal(msgp->msg_iovlen);
2785 target_vec = tswapal(msgp->msg_iov);
2787 if (count > IOV_MAX) {
2788 /* sendrcvmsg returns a different errno for this condition than
2789 * readv/writev, so we must catch it here before lock_iovec() does.
2791 ret = -TARGET_EMSGSIZE;
2795 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2796 target_vec, count, send);
2798 ret = -host_to_target_errno(errno);
2801 msg.msg_iovlen = count;
2805 if (fd_trans_target_to_host_data(fd)) {
2808 host_msg = g_malloc(msg.msg_iov->iov_len);
2809 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
2810 ret = fd_trans_target_to_host_data(fd)(host_msg,
2811 msg.msg_iov->iov_len);
2813 msg.msg_iov->iov_base = host_msg;
2814 ret = get_errno(safe_sendmsg(fd, &msg, flags));
2818 ret = target_to_host_cmsg(&msg, msgp);
2820 ret = get_errno(safe_sendmsg(fd, &msg, flags));
2824 ret = get_errno(safe_recvmsg(fd, &msg, flags));
2825 if (!is_error(ret)) {
2827 if (fd_trans_host_to_target_data(fd)) {
2828 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
2829 MIN(msg.msg_iov->iov_len, len));
2831 ret = host_to_target_cmsg(msgp, &msg);
2833 if (!is_error(ret)) {
2834 msgp->msg_namelen = tswap32(msg.msg_namelen);
2835 msgp->msg_flags = tswap32(msg.msg_flags);
2836 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
2837 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
2838 msg.msg_name, msg.msg_namelen);
2850 unlock_iovec(vec, target_vec, count, !send);
2855 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
2856 int flags, int send)
2859 struct target_msghdr *msgp;
2861 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
2865 return -TARGET_EFAULT;
2867 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
2868 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
2872 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
2873 * so it might not have this *mmsg-specific flag either.
2875 #ifndef MSG_WAITFORONE
2876 #define MSG_WAITFORONE 0x10000
2879 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
2880 unsigned int vlen, unsigned int flags,
2883 struct target_mmsghdr *mmsgp;
2887 if (vlen > UIO_MAXIOV) {
2891 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
2893 return -TARGET_EFAULT;
2896 for (i = 0; i < vlen; i++) {
2897 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
2898 if (is_error(ret)) {
2901 mmsgp[i].msg_len = tswap32(ret);
2902 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2903 if (flags & MSG_WAITFORONE) {
2904 flags |= MSG_DONTWAIT;
2908 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
2910 /* Return number of datagrams sent if we sent any at all;
2911 * otherwise return the error.
2919 /* do_accept4() Must return target values and target errnos. */
2920 static abi_long do_accept4(int fd, abi_ulong target_addr,
2921 abi_ulong target_addrlen_addr, int flags)
2923 socklen_t addrlen, ret_addrlen;
2928 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
2930 if (target_addr == 0) {
2931 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
2934 /* linux returns EINVAL if addrlen pointer is invalid */
2935 if (get_user_u32(addrlen, target_addrlen_addr))
2936 return -TARGET_EINVAL;
2938 if ((int)addrlen < 0) {
2939 return -TARGET_EINVAL;
2942 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2943 return -TARGET_EINVAL;
2945 addr = alloca(addrlen);
2947 ret_addrlen = addrlen;
2948 ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
2949 if (!is_error(ret)) {
2950 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
2951 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
2952 ret = -TARGET_EFAULT;
2958 /* do_getpeername() Must return target values and target errnos. */
2959 static abi_long do_getpeername(int fd, abi_ulong target_addr,
2960 abi_ulong target_addrlen_addr)
2962 socklen_t addrlen, ret_addrlen;
2966 if (get_user_u32(addrlen, target_addrlen_addr))
2967 return -TARGET_EFAULT;
2969 if ((int)addrlen < 0) {
2970 return -TARGET_EINVAL;
2973 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2974 return -TARGET_EFAULT;
2976 addr = alloca(addrlen);
2978 ret_addrlen = addrlen;
2979 ret = get_errno(getpeername(fd, addr, &ret_addrlen));
2980 if (!is_error(ret)) {
2981 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
2982 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
2983 ret = -TARGET_EFAULT;
2989 /* do_getsockname() Must return target values and target errnos. */
2990 static abi_long do_getsockname(int fd, abi_ulong target_addr,
2991 abi_ulong target_addrlen_addr)
2993 socklen_t addrlen, ret_addrlen;
2997 if (get_user_u32(addrlen, target_addrlen_addr))
2998 return -TARGET_EFAULT;
3000 if ((int)addrlen < 0) {
3001 return -TARGET_EINVAL;
3004 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3005 return -TARGET_EFAULT;
3007 addr = alloca(addrlen);
3009 ret_addrlen = addrlen;
3010 ret = get_errno(getsockname(fd, addr, &ret_addrlen));
3011 if (!is_error(ret)) {
3012 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3013 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
3014 ret = -TARGET_EFAULT;
3020 /* do_socketpair() Must return target values and target errnos. */
3021 static abi_long do_socketpair(int domain, int type, int protocol,
3022 abi_ulong target_tab_addr)
3027 target_to_host_sock_type(&type);
3029 ret = get_errno(socketpair(domain, type, protocol, tab));
3030 if (!is_error(ret)) {
3031 if (put_user_s32(tab[0], target_tab_addr)
3032 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3033 ret = -TARGET_EFAULT;
3038 /* do_sendto() Must return target values and target errnos. */
3039 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3040 abi_ulong target_addr, socklen_t addrlen)
3044 void *copy_msg = NULL;
3047 if ((int)addrlen < 0) {
3048 return -TARGET_EINVAL;
3051 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3053 return -TARGET_EFAULT;
3054 if (fd_trans_target_to_host_data(fd)) {
3055 copy_msg = host_msg;
3056 host_msg = g_malloc(len);
3057 memcpy(host_msg, copy_msg, len);
3058 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3064 addr = alloca(addrlen+1);
3065 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
3069 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
3071 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
3076 host_msg = copy_msg;
3078 unlock_user(host_msg, msg, 0);
3082 /* do_recvfrom() Must return target values and target errnos. */
3083 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3084 abi_ulong target_addr,
3085 abi_ulong target_addrlen)
3087 socklen_t addrlen, ret_addrlen;
3092 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3094 return -TARGET_EFAULT;
3096 if (get_user_u32(addrlen, target_addrlen)) {
3097 ret = -TARGET_EFAULT;
3100 if ((int)addrlen < 0) {
3101 ret = -TARGET_EINVAL;
3104 addr = alloca(addrlen);
3105 ret_addrlen = addrlen;
3106 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
3107 addr, &ret_addrlen));
3109 addr = NULL; /* To keep compiler quiet. */
3110 addrlen = 0; /* To keep compiler quiet. */
3111 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
3113 if (!is_error(ret)) {
3114 if (fd_trans_host_to_target_data(fd)) {
3116 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
3117 if (is_error(trans)) {
3123 host_to_target_sockaddr(target_addr, addr,
3124 MIN(addrlen, ret_addrlen));
3125 if (put_user_u32(ret_addrlen, target_addrlen)) {
3126 ret = -TARGET_EFAULT;
3130 unlock_user(host_msg, msg, len);
3133 unlock_user(host_msg, msg, 0);
3138 #ifdef TARGET_NR_socketcall
3139 /* do_socketcall() must return target values and target errnos. */
3140 static abi_long do_socketcall(int num, abi_ulong vptr)
3142 static const unsigned nargs[] = { /* number of arguments per operation */
3143 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
3144 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
3145 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
3146 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
3147 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
3148 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
3149 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
3150 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
3151 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
3152 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
3153 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
3154 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
3155 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
3156 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3157 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3158 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
3159 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
3160 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
3161 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
3162 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
3164 abi_long a[6]; /* max 6 args */
3167 /* check the range of the first argument num */
3168 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3169 if (num < 1 || num > TARGET_SYS_SENDMMSG) {
3170 return -TARGET_EINVAL;
3172 /* ensure we have space for args */
3173 if (nargs[num] > ARRAY_SIZE(a)) {
3174 return -TARGET_EINVAL;
3176 /* collect the arguments in a[] according to nargs[] */
3177 for (i = 0; i < nargs[num]; ++i) {
3178 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
3179 return -TARGET_EFAULT;
3182 /* now when we have the args, invoke the appropriate underlying function */
3184 case TARGET_SYS_SOCKET: /* domain, type, protocol */
3185 return do_socket(a[0], a[1], a[2]);
3186 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
3187 return do_bind(a[0], a[1], a[2]);
3188 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
3189 return do_connect(a[0], a[1], a[2]);
3190 case TARGET_SYS_LISTEN: /* sockfd, backlog */
3191 return get_errno(listen(a[0], a[1]));
3192 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
3193 return do_accept4(a[0], a[1], a[2], 0);
3194 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
3195 return do_getsockname(a[0], a[1], a[2]);
3196 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
3197 return do_getpeername(a[0], a[1], a[2]);
3198 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
3199 return do_socketpair(a[0], a[1], a[2], a[3]);
3200 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
3201 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
3202 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
3203 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
3204 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
3205 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
3206 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
3207 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
3208 case TARGET_SYS_SHUTDOWN: /* sockfd, how */
3209 return get_errno(shutdown(a[0], a[1]));
3210 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3211 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
3212 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3213 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
3214 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
3215 return do_sendrecvmsg(a[0], a[1], a[2], 1);
3216 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
3217 return do_sendrecvmsg(a[0], a[1], a[2], 0);
3218 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
3219 return do_accept4(a[0], a[1], a[2], a[3]);
3220 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
3221 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
3222 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
3223 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
3225 gemu_log("Unsupported socketcall: %d\n", num);
3226 return -TARGET_EINVAL;
3231 #define N_SHM_REGIONS 32
3233 static struct shm_region {
3237 } shm_regions[N_SHM_REGIONS];
3239 #ifndef TARGET_SEMID64_DS
3240 /* asm-generic version of this struct */
3241 struct target_semid64_ds
3243 struct target_ipc_perm sem_perm;
3244 abi_ulong sem_otime;
3245 #if TARGET_ABI_BITS == 32
3246 abi_ulong __unused1;
3248 abi_ulong sem_ctime;
3249 #if TARGET_ABI_BITS == 32
3250 abi_ulong __unused2;
3252 abi_ulong sem_nsems;
3253 abi_ulong __unused3;
3254 abi_ulong __unused4;
3258 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
3259 abi_ulong target_addr)
3261 struct target_ipc_perm *target_ip;
3262 struct target_semid64_ds *target_sd;
3264 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3265 return -TARGET_EFAULT;
3266 target_ip = &(target_sd->sem_perm);
3267 host_ip->__key = tswap32(target_ip->__key);
3268 host_ip->uid = tswap32(target_ip->uid);
3269 host_ip->gid = tswap32(target_ip->gid);
3270 host_ip->cuid = tswap32(target_ip->cuid);
3271 host_ip->cgid = tswap32(target_ip->cgid);
3272 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3273 host_ip->mode = tswap32(target_ip->mode);
3275 host_ip->mode = tswap16(target_ip->mode);
3277 #if defined(TARGET_PPC)
3278 host_ip->__seq = tswap32(target_ip->__seq);
3280 host_ip->__seq = tswap16(target_ip->__seq);
3282 unlock_user_struct(target_sd, target_addr, 0);
3286 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
3287 struct ipc_perm *host_ip)
3289 struct target_ipc_perm *target_ip;
3290 struct target_semid64_ds *target_sd;
3292 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3293 return -TARGET_EFAULT;
3294 target_ip = &(target_sd->sem_perm);
3295 target_ip->__key = tswap32(host_ip->__key);
3296 target_ip->uid = tswap32(host_ip->uid);
3297 target_ip->gid = tswap32(host_ip->gid);
3298 target_ip->cuid = tswap32(host_ip->cuid);
3299 target_ip->cgid = tswap32(host_ip->cgid);
3300 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3301 target_ip->mode = tswap32(host_ip->mode);
3303 target_ip->mode = tswap16(host_ip->mode);
3305 #if defined(TARGET_PPC)
3306 target_ip->__seq = tswap32(host_ip->__seq);
3308 target_ip->__seq = tswap16(host_ip->__seq);
3310 unlock_user_struct(target_sd, target_addr, 1);
3314 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
3315 abi_ulong target_addr)
3317 struct target_semid64_ds *target_sd;
3319 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3320 return -TARGET_EFAULT;
3321 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
3322 return -TARGET_EFAULT;
3323 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
3324 host_sd->sem_otime = tswapal(target_sd->sem_otime);
3325 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
3326 unlock_user_struct(target_sd, target_addr, 0);
3330 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
3331 struct semid_ds *host_sd)
3333 struct target_semid64_ds *target_sd;
3335 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3336 return -TARGET_EFAULT;
3337 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
3338 return -TARGET_EFAULT;
3339 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
3340 target_sd->sem_otime = tswapal(host_sd->sem_otime);
3341 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
3342 unlock_user_struct(target_sd, target_addr, 1);
3346 struct target_seminfo {
3359 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
3360 struct seminfo *host_seminfo)
3362 struct target_seminfo *target_seminfo;
3363 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
3364 return -TARGET_EFAULT;
3365 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
3366 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
3367 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
3368 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
3369 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
3370 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
3371 __put_user(host_seminfo->semume, &target_seminfo->semume);
3372 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
3373 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
3374 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
3375 unlock_user_struct(target_seminfo, target_addr, 1);
3381 struct semid_ds *buf;
3382 unsigned short *array;
3383 struct seminfo *__buf;
3386 union target_semun {
3393 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3394 abi_ulong target_addr)
3397 unsigned short *array;
3399 struct semid_ds semid_ds;
3402 semun.buf = &semid_ds;
3404 ret = semctl(semid, 0, IPC_STAT, semun);
3406 return get_errno(ret);
3408 nsems = semid_ds.sem_nsems;
3410 *host_array = g_try_new(unsigned short, nsems);
3412 return -TARGET_ENOMEM;
3414 array = lock_user(VERIFY_READ, target_addr,
3415 nsems*sizeof(unsigned short), 1);
3417 g_free(*host_array);
3418 return -TARGET_EFAULT;
3421 for(i=0; i<nsems; i++) {
3422 __get_user((*host_array)[i], &array[i]);
3424 unlock_user(array, target_addr, 0);
3429 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3430 unsigned short **host_array)
3433 unsigned short *array;
3435 struct semid_ds semid_ds;
3438 semun.buf = &semid_ds;
3440 ret = semctl(semid, 0, IPC_STAT, semun);
3442 return get_errno(ret);
3444 nsems = semid_ds.sem_nsems;
3446 array = lock_user(VERIFY_WRITE, target_addr,
3447 nsems*sizeof(unsigned short), 0);
3449 return -TARGET_EFAULT;
3451 for(i=0; i<nsems; i++) {
3452 __put_user((*host_array)[i], &array[i]);
3454 g_free(*host_array);
3455 unlock_user(array, target_addr, 1);
3460 static inline abi_long do_semctl(int semid, int semnum, int cmd,
3461 abi_ulong target_arg)
3463 union target_semun target_su = { .buf = target_arg };
3465 struct semid_ds dsarg;
3466 unsigned short *array = NULL;
3467 struct seminfo seminfo;
3468 abi_long ret = -TARGET_EINVAL;
3475 /* In 64 bit cross-endian situations, we will erroneously pick up
3476 * the wrong half of the union for the "val" element. To rectify
3477 * this, the entire 8-byte structure is byteswapped, followed by
3478 * a swap of the 4 byte val field. In other cases, the data is
3479 * already in proper host byte order. */
3480 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
3481 target_su.buf = tswapal(target_su.buf);
3482 arg.val = tswap32(target_su.val);
3484 arg.val = target_su.val;
3486 ret = get_errno(semctl(semid, semnum, cmd, arg));
3490 err = target_to_host_semarray(semid, &array, target_su.array);
3494 ret = get_errno(semctl(semid, semnum, cmd, arg));
3495 err = host_to_target_semarray(semid, target_su.array, &array);
3502 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3506 ret = get_errno(semctl(semid, semnum, cmd, arg));
3507 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3513 arg.__buf = &seminfo;
3514 ret = get_errno(semctl(semid, semnum, cmd, arg));
3515 err = host_to_target_seminfo(target_su.__buf, &seminfo);
3523 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3530 struct target_sembuf {
3531 unsigned short sem_num;
3536 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
3537 abi_ulong target_addr,
3540 struct target_sembuf *target_sembuf;
3543 target_sembuf = lock_user(VERIFY_READ, target_addr,
3544 nsops*sizeof(struct target_sembuf), 1);
3546 return -TARGET_EFAULT;
3548 for(i=0; i<nsops; i++) {
3549 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
3550 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
3551 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
3554 unlock_user(target_sembuf, target_addr, 0);
3559 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
3561 struct sembuf sops[nsops];
3564 if (target_to_host_sembuf(sops, ptr, nsops))
3565 return -TARGET_EFAULT;
3567 ret = -TARGET_ENOSYS;
3568 #ifdef __NR_semtimedop
3569 ret = get_errno(safe_semtimedop(semid, sops, nsops, NULL));
3572 if (ret == -TARGET_ENOSYS) {
3573 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid, nsops, 0, sops, 0));
3579 struct target_msqid_ds
3581 struct target_ipc_perm msg_perm;
3582 abi_ulong msg_stime;
3583 #if TARGET_ABI_BITS == 32
3584 abi_ulong __unused1;
3586 abi_ulong msg_rtime;
3587 #if TARGET_ABI_BITS == 32
3588 abi_ulong __unused2;
3590 abi_ulong msg_ctime;
3591 #if TARGET_ABI_BITS == 32
3592 abi_ulong __unused3;
3594 abi_ulong __msg_cbytes;
3596 abi_ulong msg_qbytes;
3597 abi_ulong msg_lspid;
3598 abi_ulong msg_lrpid;
3599 abi_ulong __unused4;
3600 abi_ulong __unused5;
3603 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
3604 abi_ulong target_addr)
3606 struct target_msqid_ds *target_md;
3608 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
3609 return -TARGET_EFAULT;
3610 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
3611 return -TARGET_EFAULT;
3612 host_md->msg_stime = tswapal(target_md->msg_stime);
3613 host_md->msg_rtime = tswapal(target_md->msg_rtime);
3614 host_md->msg_ctime = tswapal(target_md->msg_ctime);
3615 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
3616 host_md->msg_qnum = tswapal(target_md->msg_qnum);
3617 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
3618 host_md->msg_lspid = tswapal(target_md->msg_lspid);
3619 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
3620 unlock_user_struct(target_md, target_addr, 0);
3624 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
3625 struct msqid_ds *host_md)
3627 struct target_msqid_ds *target_md;
3629 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
3630 return -TARGET_EFAULT;
3631 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
3632 return -TARGET_EFAULT;
3633 target_md->msg_stime = tswapal(host_md->msg_stime);
3634 target_md->msg_rtime = tswapal(host_md->msg_rtime);
3635 target_md->msg_ctime = tswapal(host_md->msg_ctime);
3636 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
3637 target_md->msg_qnum = tswapal(host_md->msg_qnum);
3638 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
3639 target_md->msg_lspid = tswapal(host_md->msg_lspid);
3640 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
3641 unlock_user_struct(target_md, target_addr, 1);
3645 struct target_msginfo {
3653 unsigned short int msgseg;
3656 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
3657 struct msginfo *host_msginfo)
3659 struct target_msginfo *target_msginfo;
3660 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
3661 return -TARGET_EFAULT;
3662 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
3663 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
3664 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
3665 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
3666 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
3667 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
3668 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
3669 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
3670 unlock_user_struct(target_msginfo, target_addr, 1);
3674 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
3676 struct msqid_ds dsarg;
3677 struct msginfo msginfo;
3678 abi_long ret = -TARGET_EINVAL;
3686 if (target_to_host_msqid_ds(&dsarg,ptr))
3687 return -TARGET_EFAULT;
3688 ret = get_errno(msgctl(msgid, cmd, &dsarg));
3689 if (host_to_target_msqid_ds(ptr,&dsarg))
3690 return -TARGET_EFAULT;
3693 ret = get_errno(msgctl(msgid, cmd, NULL));
3697 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
3698 if (host_to_target_msginfo(ptr, &msginfo))
3699 return -TARGET_EFAULT;
3706 struct target_msgbuf {
3711 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
3712 ssize_t msgsz, int msgflg)
3714 struct target_msgbuf *target_mb;
3715 struct msgbuf *host_mb;
3719 return -TARGET_EINVAL;
3722 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
3723 return -TARGET_EFAULT;
3724 host_mb = g_try_malloc(msgsz + sizeof(long));
3726 unlock_user_struct(target_mb, msgp, 0);
3727 return -TARGET_ENOMEM;
3729 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
3730 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
3731 ret = -TARGET_ENOSYS;
3733 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
3736 if (ret == -TARGET_ENOSYS) {
3737 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
3742 unlock_user_struct(target_mb, msgp, 0);
3747 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
3748 ssize_t msgsz, abi_long msgtyp,
3751 struct target_msgbuf *target_mb;
3753 struct msgbuf *host_mb;
3757 return -TARGET_EINVAL;
3760 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
3761 return -TARGET_EFAULT;
3763 host_mb = g_try_malloc(msgsz + sizeof(long));
3765 ret = -TARGET_ENOMEM;
3768 ret = -TARGET_ENOSYS;
3770 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
3773 if (ret == -TARGET_ENOSYS) {
3774 ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
3775 msgflg, host_mb, msgtyp));
3780 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
3781 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
3782 if (!target_mtext) {
3783 ret = -TARGET_EFAULT;
3786 memcpy(target_mb->mtext, host_mb->mtext, ret);
3787 unlock_user(target_mtext, target_mtext_addr, ret);
3790 target_mb->mtype = tswapal(host_mb->mtype);
3794 unlock_user_struct(target_mb, msgp, 1);
3799 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
3800 abi_ulong target_addr)
3802 struct target_shmid_ds *target_sd;
3804 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3805 return -TARGET_EFAULT;
3806 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
3807 return -TARGET_EFAULT;
3808 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3809 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
3810 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3811 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3812 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3813 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3814 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3815 unlock_user_struct(target_sd, target_addr, 0);
3819 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
3820 struct shmid_ds *host_sd)
3822 struct target_shmid_ds *target_sd;
3824 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3825 return -TARGET_EFAULT;
3826 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
3827 return -TARGET_EFAULT;
3828 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3829 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
3830 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3831 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3832 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3833 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3834 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3835 unlock_user_struct(target_sd, target_addr, 1);
3839 struct target_shminfo {
3847 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
3848 struct shminfo *host_shminfo)
3850 struct target_shminfo *target_shminfo;
3851 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3852 return -TARGET_EFAULT;
3853 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3854 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3855 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3856 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3857 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3858 unlock_user_struct(target_shminfo, target_addr, 1);
3862 struct target_shm_info {
3867 abi_ulong swap_attempts;
3868 abi_ulong swap_successes;
3871 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3872 struct shm_info *host_shm_info)
3874 struct target_shm_info *target_shm_info;
3875 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3876 return -TARGET_EFAULT;
3877 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3878 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3879 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3880 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3881 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3882 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3883 unlock_user_struct(target_shm_info, target_addr, 1);
3887 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3889 struct shmid_ds dsarg;
3890 struct shminfo shminfo;
3891 struct shm_info shm_info;
3892 abi_long ret = -TARGET_EINVAL;
3900 if (target_to_host_shmid_ds(&dsarg, buf))
3901 return -TARGET_EFAULT;
3902 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3903 if (host_to_target_shmid_ds(buf, &dsarg))
3904 return -TARGET_EFAULT;
3907 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3908 if (host_to_target_shminfo(buf, &shminfo))
3909 return -TARGET_EFAULT;
3912 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3913 if (host_to_target_shm_info(buf, &shm_info))
3914 return -TARGET_EFAULT;
3919 ret = get_errno(shmctl(shmid, cmd, NULL));
3926 #ifndef TARGET_FORCE_SHMLBA
3927 /* For most architectures, SHMLBA is the same as the page size;
3928 * some architectures have larger values, in which case they should
3929 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
3930 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
3931 * and defining its own value for SHMLBA.
3933 * The kernel also permits SHMLBA to be set by the architecture to a
3934 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
3935 * this means that addresses are rounded to the large size if
3936 * SHM_RND is set but addresses not aligned to that size are not rejected
3937 * as long as they are at least page-aligned. Since the only architecture
3938 * which uses this is ia64 this code doesn't provide for that oddity.
3940 static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
3942 return TARGET_PAGE_SIZE;
3946 static inline abi_ulong do_shmat(CPUArchState *cpu_env,
3947 int shmid, abi_ulong shmaddr, int shmflg)
3951 struct shmid_ds shm_info;
3955 /* find out the length of the shared memory segment */
3956 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3957 if (is_error(ret)) {
3958 /* can't get length, bail out */
3962 shmlba = target_shmlba(cpu_env);
3964 if (shmaddr & (shmlba - 1)) {
3965 if (shmflg & SHM_RND) {
3966 shmaddr &= ~(shmlba - 1);
3968 return -TARGET_EINVAL;
3971 if (!guest_range_valid(shmaddr, shm_info.shm_segsz)) {
3972 return -TARGET_EINVAL;
3978 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3980 abi_ulong mmap_start;
3982 /* In order to use the host shmat, we need to honor host SHMLBA. */
3983 mmap_start = mmap_find_vma(0, shm_info.shm_segsz, MAX(SHMLBA, shmlba));
3985 if (mmap_start == -1) {
3987 host_raddr = (void *)-1;
3989 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3992 if (host_raddr == (void *)-1) {
3994 return get_errno((long)host_raddr);
3996 raddr=h2g((unsigned long)host_raddr);
3998 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3999 PAGE_VALID | PAGE_READ |
4000 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
4002 for (i = 0; i < N_SHM_REGIONS; i++) {
4003 if (!shm_regions[i].in_use) {
4004 shm_regions[i].in_use = true;
4005 shm_regions[i].start = raddr;
4006 shm_regions[i].size = shm_info.shm_segsz;
4016 static inline abi_long do_shmdt(abi_ulong shmaddr)
4023 for (i = 0; i < N_SHM_REGIONS; ++i) {
4024 if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
4025 shm_regions[i].in_use = false;
4026 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
4030 rv = get_errno(shmdt(g2h(shmaddr)));
4037 #ifdef TARGET_NR_ipc
4038 /* ??? This only works with linear mappings. */
4039 /* do_ipc() must return target values and target errnos. */
4040 static abi_long do_ipc(CPUArchState *cpu_env,
4041 unsigned int call, abi_long first,
4042 abi_long second, abi_long third,
4043 abi_long ptr, abi_long fifth)
4048 version = call >> 16;
4053 ret = do_semop(first, ptr, second);
4057 ret = get_errno(semget(first, second, third));
4060 case IPCOP_semctl: {
4061 /* The semun argument to semctl is passed by value, so dereference the
4064 get_user_ual(atptr, ptr);
4065 ret = do_semctl(first, second, third, atptr);
4070 ret = get_errno(msgget(first, second));
4074 ret = do_msgsnd(first, ptr, second, third);
4078 ret = do_msgctl(first, second, ptr);
4085 struct target_ipc_kludge {
4090 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4091 ret = -TARGET_EFAULT;
4095 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
4097 unlock_user_struct(tmp, ptr, 0);
4101 ret = do_msgrcv(first, ptr, second, fifth, third);
4110 raddr = do_shmat(cpu_env, first, ptr, second);
4111 if (is_error(raddr))
4112 return get_errno(raddr);
4113 if (put_user_ual(raddr, third))
4114 return -TARGET_EFAULT;
4118 ret = -TARGET_EINVAL;
4123 ret = do_shmdt(ptr);
4127 /* IPC_* flag values are the same on all linux platforms */
4128 ret = get_errno(shmget(first, second, third));
4131 /* IPC_* and SHM_* command values are the same on all linux platforms */
4133 ret = do_shmctl(first, second, ptr);
4136 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
4137 ret = -TARGET_ENOSYS;
4144 /* kernel structure types definitions */
4146 #define STRUCT(name, ...) STRUCT_ ## name,
4147 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
4149 #include "syscall_types.h"
4153 #undef STRUCT_SPECIAL
4155 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
4156 #define STRUCT_SPECIAL(name)
4157 #include "syscall_types.h"
4159 #undef STRUCT_SPECIAL
4161 typedef struct IOCTLEntry IOCTLEntry;
4163 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
4164 int fd, int cmd, abi_long arg);
4168 unsigned int host_cmd;
4171 do_ioctl_fn *do_ioctl;
4172 const argtype arg_type[5];
4175 #define IOC_R 0x0001
4176 #define IOC_W 0x0002
4177 #define IOC_RW (IOC_R | IOC_W)
4179 #define MAX_STRUCT_SIZE 4096
4181 #ifdef CONFIG_FIEMAP
4182 /* So fiemap access checks don't overflow on 32 bit systems.
4183 * This is very slightly smaller than the limit imposed by
4184 * the underlying kernel.
4186 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4187 / sizeof(struct fiemap_extent))
4189 static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
4190 int fd, int cmd, abi_long arg)
4192 /* The parameter for this ioctl is a struct fiemap followed
4193 * by an array of struct fiemap_extent whose size is set
4194 * in fiemap->fm_extent_count. The array is filled in by the
4197 int target_size_in, target_size_out;
4199 const argtype *arg_type = ie->arg_type;
4200 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4203 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4207 assert(arg_type[0] == TYPE_PTR);
4208 assert(ie->access == IOC_RW);
4210 target_size_in = thunk_type_size(arg_type, 0);
4211 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4213 return -TARGET_EFAULT;
4215 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4216 unlock_user(argptr, arg, 0);
4217 fm = (struct fiemap *)buf_temp;
4218 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
4219 return -TARGET_EINVAL;
4222 outbufsz = sizeof (*fm) +
4223 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
4225 if (outbufsz > MAX_STRUCT_SIZE) {
4226 /* We can't fit all the extents into the fixed size buffer.
4227 * Allocate one that is large enough and use it instead.
4229 fm = g_try_malloc(outbufsz);
4231 return -TARGET_ENOMEM;
4233 memcpy(fm, buf_temp, sizeof(struct fiemap));
4236 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
4237 if (!is_error(ret)) {
4238 target_size_out = target_size_in;
4239 /* An extent_count of 0 means we were only counting the extents
4240 * so there are no structs to copy
4242 if (fm->fm_extent_count != 0) {
4243 target_size_out += fm->fm_mapped_extents * extent_size;
4245 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
4247 ret = -TARGET_EFAULT;
4249 /* Convert the struct fiemap */
4250 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
4251 if (fm->fm_extent_count != 0) {
4252 p = argptr + target_size_in;
4253 /* ...and then all the struct fiemap_extents */
4254 for (i = 0; i < fm->fm_mapped_extents; i++) {
4255 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
4260 unlock_user(argptr, arg, target_size_out);
4270 static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
4271 int fd, int cmd, abi_long arg)
4273 const argtype *arg_type = ie->arg_type;
4277 struct ifconf *host_ifconf;
4279 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
4280 int target_ifreq_size;
4285 abi_long target_ifc_buf;
4289 assert(arg_type[0] == TYPE_PTR);
4290 assert(ie->access == IOC_RW);
4293 target_size = thunk_type_size(arg_type, 0);
4295 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4297 return -TARGET_EFAULT;
4298 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4299 unlock_user(argptr, arg, 0);
4301 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
4302 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
4303 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
4305 if (target_ifc_buf != 0) {
4306 target_ifc_len = host_ifconf->ifc_len;
4307 nb_ifreq = target_ifc_len / target_ifreq_size;
4308 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
4310 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
4311 if (outbufsz > MAX_STRUCT_SIZE) {
4313 * We can't fit all the extents into the fixed size buffer.
4314 * Allocate one that is large enough and use it instead.
4316 host_ifconf = malloc(outbufsz);
4318 return -TARGET_ENOMEM;
4320 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
4323 host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
4325 host_ifconf->ifc_len = host_ifc_len;
4327 host_ifc_buf = NULL;
4329 host_ifconf->ifc_buf = host_ifc_buf;
4331 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
4332 if (!is_error(ret)) {
4333 /* convert host ifc_len to target ifc_len */
4335 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
4336 target_ifc_len = nb_ifreq * target_ifreq_size;
4337 host_ifconf->ifc_len = target_ifc_len;
4339 /* restore target ifc_buf */
4341 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
4343 /* copy struct ifconf to target user */
4345 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4347 return -TARGET_EFAULT;
4348 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
4349 unlock_user(argptr, arg, target_size);
4351 if (target_ifc_buf != 0) {
4352 /* copy ifreq[] to target user */
4353 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
4354 for (i = 0; i < nb_ifreq ; i++) {
4355 thunk_convert(argptr + i * target_ifreq_size,
4356 host_ifc_buf + i * sizeof(struct ifreq),
4357 ifreq_arg_type, THUNK_TARGET);
4359 unlock_user(argptr, target_ifc_buf, target_ifc_len);
4370 #if defined(CONFIG_USBFS)
4371 #if HOST_LONG_BITS > 64
4372 #error USBDEVFS thunks do not support >64 bit hosts yet.
4375 uint64_t target_urb_adr;
4376 uint64_t target_buf_adr;
4377 char *target_buf_ptr;
4378 struct usbdevfs_urb host_urb;
4381 static GHashTable *usbdevfs_urb_hashtable(void)
4383 static GHashTable *urb_hashtable;
4385 if (!urb_hashtable) {
4386 urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
4388 return urb_hashtable;
4391 static void urb_hashtable_insert(struct live_urb *urb)
4393 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4394 g_hash_table_insert(urb_hashtable, urb, urb);
4397 static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
4399 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4400 return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
4403 static void urb_hashtable_remove(struct live_urb *urb)
4405 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4406 g_hash_table_remove(urb_hashtable, urb);
4410 do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
4411 int fd, int cmd, abi_long arg)
4413 const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
4414 const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
4415 struct live_urb *lurb;
4419 uintptr_t target_urb_adr;
4422 target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
4424 memset(buf_temp, 0, sizeof(uint64_t));
4425 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
4426 if (is_error(ret)) {
4430 memcpy(&hurb, buf_temp, sizeof(uint64_t));
4431 lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
4432 if (!lurb->target_urb_adr) {
4433 return -TARGET_EFAULT;
4435 urb_hashtable_remove(lurb);
4436 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
4437 lurb->host_urb.buffer_length);
4438 lurb->target_buf_ptr = NULL;
4440 /* restore the guest buffer pointer */
4441 lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
4443 /* update the guest urb struct */
4444 argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
4447 return -TARGET_EFAULT;
4449 thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
4450 unlock_user(argptr, lurb->target_urb_adr, target_size);
4452 target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
4453 /* write back the urb handle */
4454 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4457 return -TARGET_EFAULT;
4460 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
4461 target_urb_adr = lurb->target_urb_adr;
4462 thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
4463 unlock_user(argptr, arg, target_size);
4470 do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
4471 uint8_t *buf_temp __attribute__((unused)),
4472 int fd, int cmd, abi_long arg)
4474 struct live_urb *lurb;