Merge remote-tracking branch 'remotes/armbru/tags/pull-build-2019-07-02-v2' into...
[qemu/ar7.git] / linux-user / syscall.c
blob39a37496fed52bc1ef3f6a86eecf52f43688611f
1 /*
2 * Linux syscalls
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
23 #include <elf.h>
24 #include <endian.h>
25 #include <grp.h>
26 #include <sys/ipc.h>
27 #include <sys/msg.h>
28 #include <sys/wait.h>
29 #include <sys/mount.h>
30 #include <sys/file.h>
31 #include <sys/fsuid.h>
32 #include <sys/personality.h>
33 #include <sys/prctl.h>
34 #include <sys/resource.h>
35 #include <sys/swap.h>
36 #include <linux/capability.h>
37 #include <sched.h>
38 #include <sys/timex.h>
39 #include <sys/socket.h>
40 #include <sys/un.h>
41 #include <sys/uio.h>
42 #include <poll.h>
43 #include <sys/times.h>
44 #include <sys/shm.h>
45 #include <sys/sem.h>
46 #include <sys/statfs.h>
47 #include <utime.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>
58 #ifdef CONFIG_TIMERFD
59 #include <sys/timerfd.h>
60 #endif
61 #ifdef CONFIG_EVENTFD
62 #include <sys/eventfd.h>
63 #endif
64 #ifdef CONFIG_EPOLL
65 #include <sys/epoll.h>
66 #endif
67 #ifdef CONFIG_ATTR
68 #include "qemu/xattr.h"
69 #endif
70 #ifdef CONFIG_SENDFILE
71 #include <sys/sendfile.h>
72 #endif
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>
86 #include <linux/kd.h>
87 #include <linux/mtio.h>
88 #include <linux/fs.h>
89 #if defined(CONFIG_FIEMAP)
90 #include <linux/fiemap.h>
91 #endif
92 #include <linux/fb.h>
93 #if defined(CONFIG_USBFS)
94 #include <linux/usbdevice_fs.h>
95 #include <linux/usb/ch9.h>
96 #endif
97 #include <linux/vt.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"
107 #include "uname.h"
109 #include "qemu.h"
110 #include "qemu/guest-random.h"
111 #include "qapi/error.h"
112 #include "fd-trans.h"
114 #ifndef CLONE_IO
115 #define CLONE_IO 0x80000000 /* Clone io context */
116 #endif
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])
174 #undef _syscall0
175 #undef _syscall1
176 #undef _syscall2
177 #undef _syscall3
178 #undef _syscall4
179 #undef _syscall5
180 #undef _syscall6
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, \
213 type5,arg5) \
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, \
223 type6 arg6) \
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
245 #endif
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
250 #endif
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
263 #endif
265 #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
266 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
267 #endif
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);
272 #endif
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);
276 #endif
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,
279 siginfo_t *, uinfo)
280 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
281 #ifdef __NR_exit_group
282 _syscall1(int,exit_group,int,error_code)
283 #endif
284 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
285 _syscall1(int,set_tid_address,int *,tidptr)
286 #endif
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)
290 #endif
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,
300 void *, arg);
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)
307 #endif
308 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
309 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
310 #endif
311 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
312 _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
313 #endif
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)
318 #endif
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)
326 #endif
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, },
344 #endif
345 #if defined(O_NOATIME)
346 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
347 #endif
348 #if defined(O_CLOEXEC)
349 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
350 #endif
351 #if defined(O_PATH)
352 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
353 #endif
354 #if defined(O_TMPFILE)
355 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
356 #endif
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, },
360 #endif
361 { 0, 0, 0, 0 }
364 static int sys_getcwd1(char *buf, size_t size)
366 if (getcwd(buf, size) == NULL) {
367 /* getcwd() sets errno */
368 return (-1);
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)
378 #else
379 static int sys_utimensat(int dirfd, const char *pathname,
380 const struct timespec times[2], int flags)
382 errno = ENOSYS;
383 return -1;
385 #endif
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)
393 #else
394 static int sys_renameat2(int oldfd, const char *old,
395 int newfd, const char *new, int flags)
397 if (flags == 0) {
398 return renameat(oldfd, old, newfd, new);
400 errno = ENOSYS;
401 return -1;
403 #endif
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());
414 #endif
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));
420 #endif
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));
426 #endif
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));
433 #endif
434 #endif
435 #else
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
446 #endif
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 {
450 uint64_t rlim_cur;
451 uint64_t rlim_max;
453 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
454 const struct host_rlimit64 *, new_limit,
455 struct host_rlimit64 *, old_limit)
456 #endif
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)
465 int k ;
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;
470 return k;
473 return -1;
475 #endif
477 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
478 #ifdef TARGET_ARM
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
488 * r3 as arg1 */
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)
494 switch (num) {
495 case TARGET_NR_pread64:
496 case TARGET_NR_pwrite64:
497 return 1;
499 default:
500 return 0;
503 #elif defined(TARGET_XTENSA)
504 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
505 #else
506 static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
507 #endif
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,
610 #ifdef ENOKEY
611 [ENOKEY] = TARGET_ENOKEY,
612 #endif
613 #ifdef EKEYEXPIRED
614 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
615 #endif
616 #ifdef EKEYREVOKED
617 [EKEYREVOKED] = TARGET_EKEYREVOKED,
618 #endif
619 #ifdef EKEYREJECTED
620 [EKEYREJECTED] = TARGET_EKEYREJECTED,
621 #endif
622 #ifdef EOWNERDEAD
623 [EOWNERDEAD] = TARGET_EOWNERDEAD,
624 #endif
625 #ifdef ENOTRECOVERABLE
626 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
627 #endif
628 #ifdef ENOMSG
629 [ENOMSG] = TARGET_ENOMSG,
630 #endif
631 #ifdef ERKFILL
632 [ERFKILL] = TARGET_ERFKILL,
633 #endif
634 #ifdef EHWPOISON
635 [EHWPOISON] = TARGET_EHWPOISON,
636 #endif
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];
645 return 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];
654 return err;
657 static inline abi_long get_errno(abi_long ret)
659 if (ret == -1)
660 return -host_to_target_errno(errno);
661 else
662 return ret;
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)) {
675 return NULL;
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, \
705 type4, arg4) \
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, \
714 type5 arg5) \
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,
740 size_t, sigsetsize)
741 safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
742 int, maxevents, int, timeout, const sigset_t *, sigmask,
743 size_t, sigsetsize)
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,
757 socklen_t, addrlen)
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,
768 int, flags)
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)
774 #endif
775 #ifdef __NR_ipc
776 safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
777 void *, ptr, long, fifth)
778 #endif
779 #ifdef __NR_msgsnd
780 safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
781 int, flags)
782 #endif
783 #ifdef __NR_msgrcv
784 safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
785 long, msgtype, int, flags)
786 #endif
787 #ifdef __NR_semtimedop
788 safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
789 unsigned, nsops, const struct timespec *, timeout)
790 #endif
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)
796 #endif
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
799 * the libc function.
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.
807 #ifdef __NR_fcntl64
808 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
809 #else
810 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
811 #endif
813 static inline int host_to_target_sock_type(int host_type)
815 int target_type;
817 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
818 case SOCK_DGRAM:
819 target_type = TARGET_SOCK_DGRAM;
820 break;
821 case SOCK_STREAM:
822 target_type = TARGET_SOCK_STREAM;
823 break;
824 default:
825 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
826 break;
829 #if defined(SOCK_CLOEXEC)
830 if (host_type & SOCK_CLOEXEC) {
831 target_type |= TARGET_SOCK_CLOEXEC;
833 #endif
835 #if defined(SOCK_NONBLOCK)
836 if (host_type & SOCK_NONBLOCK) {
837 target_type |= TARGET_SOCK_NONBLOCK;
839 #endif
841 return target_type;
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);
865 if (!new_brk) {
866 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
867 return target_brk;
869 if (new_brk < target_original_brk) {
870 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
871 target_brk);
872 return target_brk;
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
879 * mapped pages. */
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);
885 return 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
906 * then shrunken). */
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",
912 target_brk);
913 return target_brk;
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);
919 mapped_addr = -1;
920 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
922 else {
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;
930 #endif
931 /* For everything else, return the previous break. */
932 return target_brk;
935 static inline abi_long copy_from_user_fdset(fd_set *fds,
936 abi_ulong target_fds_addr,
937 int n)
939 int i, nw, j, k;
940 abi_ulong b, *target_fds;
942 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
943 if (!(target_fds = lock_user(VERIFY_READ,
944 target_fds_addr,
945 sizeof(abi_ulong) * nw,
946 1)))
947 return -TARGET_EFAULT;
949 FD_ZERO(fds);
950 k = 0;
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 */
956 if ((b >> j) & 1)
957 FD_SET(k, fds);
958 k++;
962 unlock_user(target_fds, target_fds_addr, 0);
964 return 0;
967 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
968 abi_ulong target_fds_addr,
969 int n)
971 if (target_fds_addr) {
972 if (copy_from_user_fdset(fds, target_fds_addr, n))
973 return -TARGET_EFAULT;
974 *fds_ptr = fds;
975 } else {
976 *fds_ptr = NULL;
978 return 0;
981 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
982 const fd_set *fds,
983 int n)
985 int i, nw, j, k;
986 abi_long v;
987 abi_ulong *target_fds;
989 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
990 if (!(target_fds = lock_user(VERIFY_WRITE,
991 target_fds_addr,
992 sizeof(abi_ulong) * nw,
993 0)))
994 return -TARGET_EFAULT;
996 k = 0;
997 for (i = 0; i < nw; i++) {
998 v = 0;
999 for (j = 0; j < TARGET_ABI_BITS; j++) {
1000 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
1001 k++;
1003 __put_user(v, &target_fds[i]);
1006 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
1008 return 0;
1011 #if defined(__alpha__)
1012 #define HOST_HZ 1024
1013 #else
1014 #define HOST_HZ 100
1015 #endif
1017 static inline abi_long host_to_target_clock_t(long ticks)
1019 #if HOST_HZ == TARGET_HZ
1020 return ticks;
1021 #else
1022 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
1023 #endif
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);
1053 return 0;
1056 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
1058 abi_ulong target_rlim_swap;
1059 rlim_t result;
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;
1069 return result;
1072 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
1074 abi_ulong target_rlim_swap;
1075 abi_ulong result;
1077 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
1078 target_rlim_swap = TARGET_RLIM_INFINITY;
1079 else
1080 target_rlim_swap = rlim;
1081 result = tswapal(target_rlim_swap);
1083 return result;
1086 static inline int target_to_host_resource(int code)
1088 switch (code) {
1089 case TARGET_RLIMIT_AS:
1090 return RLIMIT_AS;
1091 case TARGET_RLIMIT_CORE:
1092 return RLIMIT_CORE;
1093 case TARGET_RLIMIT_CPU:
1094 return RLIMIT_CPU;
1095 case TARGET_RLIMIT_DATA:
1096 return 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:
1106 return 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:
1112 return 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;
1119 default:
1120 return code;
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);
1137 return 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);
1153 return 0;
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);
1170 return 0;
1173 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1174 #include <mqueue.h>
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);
1192 return 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);
1211 return 0;
1213 #endif
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;
1223 struct timeval tv;
1224 struct timespec ts, *ts_ptr;
1225 abi_long ret;
1227 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1228 if (ret) {
1229 return ret;
1231 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1232 if (ret) {
1233 return ret;
1235 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1236 if (ret) {
1237 return ret;
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;
1245 ts_ptr = &ts;
1246 } else {
1247 ts_ptr = NULL;
1250 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1251 ts_ptr, NULL));
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;
1270 return ret;
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;
1278 long nsel;
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);
1294 #endif
1295 #endif
1297 static abi_long do_pipe2(int host_pipe[], int flags)
1299 #ifdef CONFIG_PIPE2
1300 return pipe2(host_pipe, flags);
1301 #else
1302 return -ENOSYS;
1303 #endif
1306 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1307 int flags, int is_pipe2)
1309 int host_pipe[2];
1310 abi_long ret;
1311 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1313 if (is_error(ret))
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. */
1318 if (!is_pipe2) {
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];
1331 #endif
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,
1342 socklen_t len)
1344 struct target_ip_mreqn *target_smreqn;
1346 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1347 if (!target_smreqn)
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);
1355 return 0;
1358 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
1359 abi_ulong target_addr,
1360 socklen_t len)
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);
1371 if (!target_saddr)
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] )
1389 len++;
1391 if (len > unix_maxlen)
1392 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);
1412 return 0;
1415 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1416 struct sockaddr *addr,
1417 socklen_t len)
1419 struct target_sockaddr *target_saddr;
1421 if (len == 0) {
1422 return 0;
1424 assert(addr);
1426 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1427 if (!target_saddr)
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);
1450 return 0;
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))
1464 goto the_end;
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;
1468 if (!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");
1491 break;
1494 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1495 cmsg->cmsg_level = SOL_SOCKET;
1496 } else {
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);
1519 } else {
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,
1527 target_cmsg_start);
1529 unlock_user(target_cmsg, target_cmsg_addr, 0);
1530 the_end:
1531 msgh->msg_controllen = space;
1532 return 0;
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))
1546 goto the_end;
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;
1550 if (!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);
1568 break;
1571 if (cmsg->cmsg_level == SOL_SOCKET) {
1572 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1573 } else {
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.
1581 tgt_len = len;
1582 switch (cmsg->cmsg_level) {
1583 case SOL_SOCKET:
1584 switch (cmsg->cmsg_type) {
1585 case SO_TIMESTAMP:
1586 tgt_len = sizeof(struct target_timeval);
1587 break;
1588 default:
1589 break;
1591 break;
1592 default:
1593 break;
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) {
1607 case SOL_SOCKET:
1608 switch (cmsg->cmsg_type) {
1609 case SCM_RIGHTS:
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);
1618 break;
1620 case SO_TIMESTAMP:
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)) {
1628 goto unimplemented;
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);
1634 break;
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);
1645 break;
1647 default:
1648 goto unimplemented;
1650 break;
1652 case SOL_IP:
1653 switch (cmsg->cmsg_type) {
1654 case IP_TTL:
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)) {
1661 goto unimplemented;
1663 __put_user(*v, t_int);
1664 break;
1666 case IP_RECVERR:
1668 struct errhdr_t {
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)) {
1678 goto unimplemented;
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));
1689 break;
1691 default:
1692 goto unimplemented;
1694 break;
1696 case SOL_IPV6:
1697 switch (cmsg->cmsg_type) {
1698 case IPV6_HOPLIMIT:
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)) {
1705 goto unimplemented;
1707 __put_user(*v, t_int);
1708 break;
1710 case IPV6_RECVERR:
1712 struct errhdr6_t {
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)) {
1722 goto unimplemented;
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));
1733 break;
1735 default:
1736 goto unimplemented;
1738 break;
1740 default:
1741 unimplemented:
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;
1756 space += tgt_space;
1757 cmsg = CMSG_NXTHDR(msgh, cmsg);
1758 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1759 target_cmsg_start);
1761 unlock_user(target_cmsg, target_cmsg_addr, space);
1762 the_end:
1763 target_msgh->msg_controllen = tswapal(space);
1764 return 0;
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)
1771 abi_long ret;
1772 int val;
1773 struct ip_mreqn *ip_mreq;
1774 struct ip_mreq_source *ip_mreq_source;
1776 switch(level) {
1777 case SOL_TCP:
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)));
1785 break;
1786 case SOL_IP:
1787 switch(optname) {
1788 case IP_TOS:
1789 case IP_TTL:
1790 case IP_HDRINCL:
1791 case IP_ROUTER_ALERT:
1792 case IP_RECVOPTS:
1793 case IP_RETOPTS:
1794 case IP_PKTINFO:
1795 case IP_MTU_DISCOVER:
1796 case IP_RECVERR:
1797 case IP_RECVTTL:
1798 case IP_RECVTOS:
1799 #ifdef IP_FREEBIND
1800 case IP_FREEBIND:
1801 #endif
1802 case IP_MULTICAST_TTL:
1803 case IP_MULTICAST_LOOP:
1804 val = 0;
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)));
1813 break;
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));
1823 break;
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);
1835 break;
1837 default:
1838 goto unimplemented;
1840 break;
1841 case SOL_IPV6:
1842 switch (optname) {
1843 case IPV6_MTU_DISCOVER:
1844 case IPV6_MTU:
1845 case IPV6_V6ONLY:
1846 case IPV6_RECVPKTINFO:
1847 case IPV6_UNICAST_HOPS:
1848 case IPV6_MULTICAST_HOPS:
1849 case IPV6_MULTICAST_LOOP:
1850 case IPV6_RECVERR:
1851 case IPV6_RECVHOPLIMIT:
1852 case IPV6_2292HOPLIMIT:
1853 case IPV6_CHECKSUM:
1854 case IPV6_ADDRFORM:
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:
1863 case IPV6_TCLASS:
1864 #ifdef IPV6_RECVPATHMTU
1865 case IPV6_RECVPATHMTU:
1866 #endif
1867 #ifdef IPV6_TRANSPARENT
1868 case IPV6_TRANSPARENT:
1869 #endif
1870 #ifdef IPV6_FREEBIND
1871 case IPV6_FREEBIND:
1872 #endif
1873 #ifdef IPV6_RECVORIGDSTADDR
1874 case IPV6_RECVORIGDSTADDR:
1875 #endif
1876 val = 0;
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)));
1885 break;
1886 case IPV6_PKTINFO:
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)));
1902 break;
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)));
1921 break;
1923 default:
1924 goto unimplemented;
1926 break;
1927 case SOL_ICMPV6:
1928 switch (optname) {
1929 case ICMPV6_FILTER:
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,
1946 &icmp6f, optlen));
1947 break;
1949 default:
1950 goto unimplemented;
1952 break;
1953 case SOL_RAW:
1954 switch (optname) {
1955 case ICMP_FILTER:
1956 case IPV6_CHECKSUM:
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)));
1967 break;
1969 default:
1970 goto unimplemented;
1972 break;
1973 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
1974 case SOL_ALG:
1975 switch (optname) {
1976 case ALG_SET_KEY:
1978 char *alg_key = g_malloc(optlen);
1980 if (!alg_key) {
1981 return -TARGET_ENOMEM;
1983 if (copy_from_user(alg_key, optval_addr, optlen)) {
1984 g_free(alg_key);
1985 return -TARGET_EFAULT;
1987 ret = get_errno(setsockopt(sockfd, level, optname,
1988 alg_key, optlen));
1989 g_free(alg_key);
1990 break;
1992 case ALG_SET_AEAD_AUTHSIZE:
1994 ret = get_errno(setsockopt(sockfd, level, optname,
1995 NULL, optlen));
1996 break;
1998 default:
1999 goto unimplemented;
2001 break;
2002 #endif
2003 case TARGET_SOL_SOCKET:
2004 switch (optname) {
2005 case TARGET_SO_RCVTIMEO:
2007 struct timeval tv;
2009 optname = SO_RCVTIMEO;
2011 set_timeout:
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,
2021 &tv, sizeof(tv)));
2022 return ret;
2024 case TARGET_SO_SNDTIMEO:
2025 optname = SO_SNDTIMEO;
2026 goto set_timeout;
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;
2033 int i;
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)));
2064 g_free(filter);
2066 unlock_user_struct(tfilter, tfprog->filter, 1);
2067 unlock_user_struct(tfprog, optval_addr, 1);
2068 return ret;
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);
2078 if (!dev_ifname) {
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);
2088 return ret;
2090 case TARGET_SO_LINGER:
2092 struct linger lg;
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,
2104 &lg, sizeof(lg)));
2105 unlock_user_struct(tlg, optval_addr, 0);
2106 return ret;
2108 /* Options with 'int' argument. */
2109 case TARGET_SO_DEBUG:
2110 optname = SO_DEBUG;
2111 break;
2112 case TARGET_SO_REUSEADDR:
2113 optname = SO_REUSEADDR;
2114 break;
2115 #ifdef SO_REUSEPORT
2116 case TARGET_SO_REUSEPORT:
2117 optname = SO_REUSEPORT;
2118 break;
2119 #endif
2120 case TARGET_SO_TYPE:
2121 optname = SO_TYPE;
2122 break;
2123 case TARGET_SO_ERROR:
2124 optname = SO_ERROR;
2125 break;
2126 case TARGET_SO_DONTROUTE:
2127 optname = SO_DONTROUTE;
2128 break;
2129 case TARGET_SO_BROADCAST:
2130 optname = SO_BROADCAST;
2131 break;
2132 case TARGET_SO_SNDBUF:
2133 optname = SO_SNDBUF;
2134 break;
2135 case TARGET_SO_SNDBUFFORCE:
2136 optname = SO_SNDBUFFORCE;
2137 break;
2138 case TARGET_SO_RCVBUF:
2139 optname = SO_RCVBUF;
2140 break;
2141 case TARGET_SO_RCVBUFFORCE:
2142 optname = SO_RCVBUFFORCE;
2143 break;
2144 case TARGET_SO_KEEPALIVE:
2145 optname = SO_KEEPALIVE;
2146 break;
2147 case TARGET_SO_OOBINLINE:
2148 optname = SO_OOBINLINE;
2149 break;
2150 case TARGET_SO_NO_CHECK:
2151 optname = SO_NO_CHECK;
2152 break;
2153 case TARGET_SO_PRIORITY:
2154 optname = SO_PRIORITY;
2155 break;
2156 #ifdef SO_BSDCOMPAT
2157 case TARGET_SO_BSDCOMPAT:
2158 optname = SO_BSDCOMPAT;
2159 break;
2160 #endif
2161 case TARGET_SO_PASSCRED:
2162 optname = SO_PASSCRED;
2163 break;
2164 case TARGET_SO_PASSSEC:
2165 optname = SO_PASSSEC;
2166 break;
2167 case TARGET_SO_TIMESTAMP:
2168 optname = SO_TIMESTAMP;
2169 break;
2170 case TARGET_SO_RCVLOWAT:
2171 optname = SO_RCVLOWAT;
2172 break;
2173 default:
2174 goto unimplemented;
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)));
2182 break;
2183 default:
2184 unimplemented:
2185 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
2186 ret = -TARGET_ENOPROTOOPT;
2188 return ret;
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)
2195 abi_long ret;
2196 int len, val;
2197 socklen_t lv;
2199 switch(level) {
2200 case TARGET_SOL_SOCKET:
2201 level = SOL_SOCKET;
2202 switch (optname) {
2203 /* These don't just return a single integer */
2204 case TARGET_SO_RCVTIMEO:
2205 case TARGET_SO_SNDTIMEO:
2206 case TARGET_SO_PEERNAME:
2207 goto unimplemented;
2208 case TARGET_SO_PEERCRED: {
2209 struct ucred cr;
2210 socklen_t crlen;
2211 struct target_ucred *tcr;
2213 if (get_user_u32(len, optlen)) {
2214 return -TARGET_EFAULT;
2216 if (len < 0) {
2217 return -TARGET_EINVAL;
2220 crlen = sizeof(cr);
2221 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2222 &cr, &crlen));
2223 if (ret < 0) {
2224 return ret;
2226 if (len > crlen) {
2227 len = crlen;
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;
2239 break;
2241 case TARGET_SO_LINGER:
2243 struct linger lg;
2244 socklen_t lglen;
2245 struct target_linger *tlg;
2247 if (get_user_u32(len, optlen)) {
2248 return -TARGET_EFAULT;
2250 if (len < 0) {
2251 return -TARGET_EINVAL;
2254 lglen = sizeof(lg);
2255 ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
2256 &lg, &lglen));
2257 if (ret < 0) {
2258 return ret;
2260 if (len > lglen) {
2261 len = lglen;
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;
2272 break;
2274 /* Options with 'int' argument. */
2275 case TARGET_SO_DEBUG:
2276 optname = SO_DEBUG;
2277 goto int_case;
2278 case TARGET_SO_REUSEADDR:
2279 optname = SO_REUSEADDR;
2280 goto int_case;
2281 #ifdef SO_REUSEPORT
2282 case TARGET_SO_REUSEPORT:
2283 optname = SO_REUSEPORT;
2284 goto int_case;
2285 #endif
2286 case TARGET_SO_TYPE:
2287 optname = SO_TYPE;
2288 goto int_case;
2289 case TARGET_SO_ERROR:
2290 optname = SO_ERROR;
2291 goto int_case;
2292 case TARGET_SO_DONTROUTE:
2293 optname = SO_DONTROUTE;
2294 goto int_case;
2295 case TARGET_SO_BROADCAST:
2296 optname = SO_BROADCAST;
2297 goto int_case;
2298 case TARGET_SO_SNDBUF:
2299 optname = SO_SNDBUF;
2300 goto int_case;
2301 case TARGET_SO_RCVBUF:
2302 optname = SO_RCVBUF;
2303 goto int_case;
2304 case TARGET_SO_KEEPALIVE:
2305 optname = SO_KEEPALIVE;
2306 goto int_case;
2307 case TARGET_SO_OOBINLINE:
2308 optname = SO_OOBINLINE;
2309 goto int_case;
2310 case TARGET_SO_NO_CHECK:
2311 optname = SO_NO_CHECK;
2312 goto int_case;
2313 case TARGET_SO_PRIORITY:
2314 optname = SO_PRIORITY;
2315 goto int_case;
2316 #ifdef SO_BSDCOMPAT
2317 case TARGET_SO_BSDCOMPAT:
2318 optname = SO_BSDCOMPAT;
2319 goto int_case;
2320 #endif
2321 case TARGET_SO_PASSCRED:
2322 optname = SO_PASSCRED;
2323 goto int_case;
2324 case TARGET_SO_TIMESTAMP:
2325 optname = SO_TIMESTAMP;
2326 goto int_case;
2327 case TARGET_SO_RCVLOWAT:
2328 optname = SO_RCVLOWAT;
2329 goto int_case;
2330 case TARGET_SO_ACCEPTCONN:
2331 optname = SO_ACCEPTCONN;
2332 goto int_case;
2333 default:
2334 goto int_case;
2336 break;
2337 case SOL_TCP:
2338 /* TCP options all take an 'int' value. */
2339 int_case:
2340 if (get_user_u32(len, optlen))
2341 return -TARGET_EFAULT;
2342 if (len < 0)
2343 return -TARGET_EINVAL;
2344 lv = sizeof(lv);
2345 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2346 if (ret < 0)
2347 return ret;
2348 if (optname == SO_TYPE) {
2349 val = host_to_target_sock_type(val);
2351 if (len > lv)
2352 len = lv;
2353 if (len == 4) {
2354 if (put_user_u32(val, optval_addr))
2355 return -TARGET_EFAULT;
2356 } else {
2357 if (put_user_u8(val, optval_addr))
2358 return -TARGET_EFAULT;
2360 if (put_user_u32(len, optlen))
2361 return -TARGET_EFAULT;
2362 break;
2363 case SOL_IP:
2364 switch(optname) {
2365 case IP_TOS:
2366 case IP_TTL:
2367 case IP_HDRINCL:
2368 case IP_ROUTER_ALERT:
2369 case IP_RECVOPTS:
2370 case IP_RETOPTS:
2371 case IP_PKTINFO:
2372 case IP_MTU_DISCOVER:
2373 case IP_RECVERR:
2374 case IP_RECVTOS:
2375 #ifdef IP_FREEBIND
2376 case IP_FREEBIND:
2377 #endif
2378 case IP_MULTICAST_TTL:
2379 case IP_MULTICAST_LOOP:
2380 if (get_user_u32(len, optlen))
2381 return -TARGET_EFAULT;
2382 if (len < 0)
2383 return -TARGET_EINVAL;
2384 lv = sizeof(lv);
2385 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2386 if (ret < 0)
2387 return ret;
2388 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2389 len = 1;
2390 if (put_user_u32(len, optlen)
2391 || put_user_u8(val, optval_addr))
2392 return -TARGET_EFAULT;
2393 } else {
2394 if (len > sizeof(int))
2395 len = sizeof(int);
2396 if (put_user_u32(len, optlen)
2397 || put_user_u32(val, optval_addr))
2398 return -TARGET_EFAULT;
2400 break;
2401 default:
2402 ret = -TARGET_ENOPROTOOPT;
2403 break;
2405 break;
2406 case SOL_IPV6:
2407 switch (optname) {
2408 case IPV6_MTU_DISCOVER:
2409 case IPV6_MTU:
2410 case IPV6_V6ONLY:
2411 case IPV6_RECVPKTINFO:
2412 case IPV6_UNICAST_HOPS:
2413 case IPV6_MULTICAST_HOPS:
2414 case IPV6_MULTICAST_LOOP:
2415 case IPV6_RECVERR:
2416 case IPV6_RECVHOPLIMIT:
2417 case IPV6_2292HOPLIMIT:
2418 case IPV6_CHECKSUM:
2419 case IPV6_ADDRFORM:
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:
2428 case IPV6_TCLASS:
2429 #ifdef IPV6_RECVPATHMTU
2430 case IPV6_RECVPATHMTU:
2431 #endif
2432 #ifdef IPV6_TRANSPARENT
2433 case IPV6_TRANSPARENT:
2434 #endif
2435 #ifdef IPV6_FREEBIND
2436 case IPV6_FREEBIND:
2437 #endif
2438 #ifdef IPV6_RECVORIGDSTADDR
2439 case IPV6_RECVORIGDSTADDR:
2440 #endif
2441 if (get_user_u32(len, optlen))
2442 return -TARGET_EFAULT;
2443 if (len < 0)
2444 return -TARGET_EINVAL;
2445 lv = sizeof(lv);
2446 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2447 if (ret < 0)
2448 return ret;
2449 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2450 len = 1;
2451 if (put_user_u32(len, optlen)
2452 || put_user_u8(val, optval_addr))
2453 return -TARGET_EFAULT;
2454 } else {
2455 if (len > sizeof(int))
2456 len = sizeof(int);
2457 if (put_user_u32(len, optlen)
2458 || put_user_u32(val, optval_addr))
2459 return -TARGET_EFAULT;
2461 break;
2462 default:
2463 ret = -TARGET_ENOPROTOOPT;
2464 break;
2466 break;
2467 default:
2468 unimplemented:
2469 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
2470 level, optname);
2471 ret = -TARGET_EOPNOTSUPP;
2472 break;
2474 return ret;
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,
2482 abi_ulong thigh,
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;
2490 *hlow = off;
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;
2498 struct iovec *vec;
2499 abi_ulong total_len, max_len;
2500 int i;
2501 int err = 0;
2502 bool bad_address = false;
2504 if (count == 0) {
2505 errno = 0;
2506 return NULL;
2508 if (count > IOV_MAX) {
2509 errno = EINVAL;
2510 return NULL;
2513 vec = g_try_new0(struct iovec, count);
2514 if (vec == NULL) {
2515 errno = ENOMEM;
2516 return NULL;
2519 target_vec = lock_user(VERIFY_READ, target_addr,
2520 count * sizeof(struct target_iovec), 1);
2521 if (target_vec == NULL) {
2522 err = EFAULT;
2523 goto fail2;
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;
2529 total_len = 0;
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);
2535 if (len < 0) {
2536 err = EINVAL;
2537 goto fail;
2538 } else if (len == 0) {
2539 /* Zero length pointer is ignored. */
2540 vec[i].iov_base = 0;
2541 } else {
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
2546 * zero lengths. */
2547 if (!vec[i].iov_base) {
2548 if (i == 0) {
2549 err = EFAULT;
2550 goto fail;
2551 } else {
2552 bad_address = true;
2555 if (bad_address) {
2556 len = 0;
2558 if (len > max_len - total_len) {
2559 len = max_len - total_len;
2562 vec[i].iov_len = len;
2563 total_len += len;
2566 unlock_user(target_vec, target_addr, 0);
2567 return vec;
2569 fail:
2570 while (--i >= 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);
2576 fail2:
2577 g_free(vec);
2578 errno = err;
2579 return NULL;
2582 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
2583 abi_ulong count, int copy)
2585 struct target_iovec *target_vec;
2586 int i;
2588 target_vec = lock_user(VERIFY_READ, target_addr,
2589 count * sizeof(struct target_iovec), 1);
2590 if (target_vec) {
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);
2594 if (len < 0) {
2595 break;
2597 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
2599 unlock_user(target_vec, target_addr, 0);
2602 g_free(vec);
2605 static inline int target_to_host_sock_type(int *type)
2607 int host_type = 0;
2608 int target_type = *type;
2610 switch (target_type & TARGET_SOCK_TYPE_MASK) {
2611 case TARGET_SOCK_DGRAM:
2612 host_type = SOCK_DGRAM;
2613 break;
2614 case TARGET_SOCK_STREAM:
2615 host_type = SOCK_STREAM;
2616 break;
2617 default:
2618 host_type = target_type & TARGET_SOCK_TYPE_MASK;
2619 break;
2621 if (target_type & TARGET_SOCK_CLOEXEC) {
2622 #if defined(SOCK_CLOEXEC)
2623 host_type |= SOCK_CLOEXEC;
2624 #else
2625 return -TARGET_EINVAL;
2626 #endif
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;
2633 #endif
2635 *type = host_type;
2636 return 0;
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) {
2646 close(fd);
2647 return -TARGET_EINVAL;
2650 #endif
2651 return fd;
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;
2658 int ret;
2660 ret = target_to_host_sock_type(&type);
2661 if (ret) {
2662 return ret;
2665 if (domain == PF_NETLINK && !(
2666 #ifdef CONFIG_RTNETLINK
2667 protocol == NETLINK_ROUTE ||
2668 #endif
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));
2680 if (ret >= 0) {
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) {
2688 switch (protocol) {
2689 #ifdef CONFIG_RTNETLINK
2690 case NETLINK_ROUTE:
2691 fd_trans_register(ret, &target_netlink_route_trans);
2692 break;
2693 #endif
2694 case NETLINK_KOBJECT_UEVENT:
2695 /* nothing to do: messages are strings */
2696 break;
2697 case NETLINK_AUDIT:
2698 fd_trans_register(ret, &target_netlink_audit_trans);
2699 break;
2700 default:
2701 g_assert_not_reached();
2705 return ret;
2708 /* do_bind() Must return target values and target errnos. */
2709 static abi_long do_bind(int sockfd, abi_ulong target_addr,
2710 socklen_t addrlen)
2712 void *addr;
2713 abi_long ret;
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);
2722 if (ret)
2723 return ret;
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,
2730 socklen_t addrlen)
2732 void *addr;
2733 abi_long ret;
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);
2742 if (ret)
2743 return ret;
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)
2752 abi_long ret, len;
2753 struct msghdr msg;
2754 abi_ulong count;
2755 struct iovec *vec;
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),
2763 msg.msg_namelen);
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;
2771 } else if (ret) {
2772 goto out2;
2774 } else {
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;
2792 goto out2;
2795 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2796 target_vec, count, send);
2797 if (vec == NULL) {
2798 ret = -host_to_target_errno(errno);
2799 goto out2;
2801 msg.msg_iovlen = count;
2802 msg.msg_iov = vec;
2804 if (send) {
2805 if (fd_trans_target_to_host_data(fd)) {
2806 void *host_msg;
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);
2812 if (ret >= 0) {
2813 msg.msg_iov->iov_base = host_msg;
2814 ret = get_errno(safe_sendmsg(fd, &msg, flags));
2816 g_free(host_msg);
2817 } else {
2818 ret = target_to_host_cmsg(&msg, msgp);
2819 if (ret == 0) {
2820 ret = get_errno(safe_sendmsg(fd, &msg, flags));
2823 } else {
2824 ret = get_errno(safe_recvmsg(fd, &msg, flags));
2825 if (!is_error(ret)) {
2826 len = 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));
2830 } else {
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);
2839 if (ret) {
2840 goto out;
2844 ret = len;
2849 out:
2850 unlock_iovec(vec, target_vec, count, !send);
2851 out2:
2852 return ret;
2855 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
2856 int flags, int send)
2858 abi_long ret;
2859 struct target_msghdr *msgp;
2861 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
2862 msgp,
2863 target_msg,
2864 send ? 1 : 0)) {
2865 return -TARGET_EFAULT;
2867 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
2868 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
2869 return ret;
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
2877 #endif
2879 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
2880 unsigned int vlen, unsigned int flags,
2881 int send)
2883 struct target_mmsghdr *mmsgp;
2884 abi_long ret = 0;
2885 int i;
2887 if (vlen > UIO_MAXIOV) {
2888 vlen = UIO_MAXIOV;
2891 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
2892 if (!mmsgp) {
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)) {
2899 break;
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.
2913 if (i) {
2914 return i;
2916 return ret;
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;
2924 void *addr;
2925 abi_long ret;
2926 int host_flags;
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;
2955 return ret;
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;
2963 void *addr;
2964 abi_long ret;
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;
2986 return ret;
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;
2994 void *addr;
2995 abi_long ret;
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;
3017 return ret;
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)
3024 int tab[2];
3025 abi_long ret;
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;
3035 return ret;
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)
3042 void *addr;
3043 void *host_msg;
3044 void *copy_msg = NULL;
3045 abi_long ret;
3047 if ((int)addrlen < 0) {
3048 return -TARGET_EINVAL;
3051 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3052 if (!host_msg)
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);
3059 if (ret < 0) {
3060 goto fail;
3063 if (target_addr) {
3064 addr = alloca(addrlen+1);
3065 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
3066 if (ret) {
3067 goto fail;
3069 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
3070 } else {
3071 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
3073 fail:
3074 if (copy_msg) {
3075 g_free(host_msg);
3076 host_msg = copy_msg;
3078 unlock_user(host_msg, msg, 0);
3079 return ret;
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;
3088 void *addr;
3089 void *host_msg;
3090 abi_long ret;
3092 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3093 if (!host_msg)
3094 return -TARGET_EFAULT;
3095 if (target_addr) {
3096 if (get_user_u32(addrlen, target_addrlen)) {
3097 ret = -TARGET_EFAULT;
3098 goto fail;
3100 if ((int)addrlen < 0) {
3101 ret = -TARGET_EINVAL;
3102 goto fail;
3104 addr = alloca(addrlen);
3105 ret_addrlen = addrlen;
3106 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
3107 addr, &ret_addrlen));
3108 } else {
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)) {
3115 abi_long trans;
3116 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
3117 if (is_error(trans)) {
3118 ret = trans;
3119 goto fail;
3122 if (target_addr) {
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;
3127 goto fail;
3130 unlock_user(host_msg, msg, len);
3131 } else {
3132 fail:
3133 unlock_user(host_msg, msg, 0);
3135 return ret;
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 */
3165 unsigned i;
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 */
3183 switch (num) {
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);
3224 default:
3225 gemu_log("Unsupported socketcall: %d\n", num);
3226 return -TARGET_EINVAL;
3229 #endif
3231 #define N_SHM_REGIONS 32
3233 static struct shm_region {
3234 abi_ulong start;
3235 abi_ulong size;
3236 bool in_use;
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;
3247 #endif
3248 abi_ulong sem_ctime;
3249 #if TARGET_ABI_BITS == 32
3250 abi_ulong __unused2;
3251 #endif
3252 abi_ulong sem_nsems;
3253 abi_ulong __unused3;
3254 abi_ulong __unused4;
3256 #endif
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);
3274 #else
3275 host_ip->mode = tswap16(target_ip->mode);
3276 #endif
3277 #if defined(TARGET_PPC)
3278 host_ip->__seq = tswap32(target_ip->__seq);
3279 #else
3280 host_ip->__seq = tswap16(target_ip->__seq);
3281 #endif
3282 unlock_user_struct(target_sd, target_addr, 0);
3283 return 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);
3302 #else
3303 target_ip->mode = tswap16(host_ip->mode);
3304 #endif
3305 #if defined(TARGET_PPC)
3306 target_ip->__seq = tswap32(host_ip->__seq);
3307 #else
3308 target_ip->__seq = tswap16(host_ip->__seq);
3309 #endif
3310 unlock_user_struct(target_sd, target_addr, 1);
3311 return 0;
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);
3327 return 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);
3343 return 0;
3346 struct target_seminfo {
3347 int semmap;
3348 int semmni;
3349 int semmns;
3350 int semmnu;
3351 int semmsl;
3352 int semopm;
3353 int semume;
3354 int semusz;
3355 int semvmx;
3356 int semaem;
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);
3376 return 0;
3379 union semun {
3380 int val;
3381 struct semid_ds *buf;
3382 unsigned short *array;
3383 struct seminfo *__buf;
3386 union target_semun {
3387 int val;
3388 abi_ulong buf;
3389 abi_ulong array;
3390 abi_ulong __buf;
3393 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3394 abi_ulong target_addr)
3396 int nsems;
3397 unsigned short *array;
3398 union semun semun;
3399 struct semid_ds semid_ds;
3400 int i, ret;
3402 semun.buf = &semid_ds;
3404 ret = semctl(semid, 0, IPC_STAT, semun);
3405 if (ret == -1)
3406 return get_errno(ret);
3408 nsems = semid_ds.sem_nsems;
3410 *host_array = g_try_new(unsigned short, nsems);
3411 if (!*host_array) {
3412 return -TARGET_ENOMEM;
3414 array = lock_user(VERIFY_READ, target_addr,
3415 nsems*sizeof(unsigned short), 1);
3416 if (!array) {
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);
3426 return 0;
3429 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3430 unsigned short **host_array)
3432 int nsems;
3433 unsigned short *array;
3434 union semun semun;
3435 struct semid_ds semid_ds;
3436 int i, ret;
3438 semun.buf = &semid_ds;
3440 ret = semctl(semid, 0, IPC_STAT, semun);
3441 if (ret == -1)
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);
3448 if (!array)
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);
3457 return 0;
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 };
3464 union semun arg;
3465 struct semid_ds dsarg;
3466 unsigned short *array = NULL;
3467 struct seminfo seminfo;
3468 abi_long ret = -TARGET_EINVAL;
3469 abi_long err;
3470 cmd &= 0xff;
3472 switch( cmd ) {
3473 case GETVAL:
3474 case SETVAL:
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);
3483 } else {
3484 arg.val = target_su.val;
3486 ret = get_errno(semctl(semid, semnum, cmd, arg));
3487 break;
3488 case GETALL:
3489 case SETALL:
3490 err = target_to_host_semarray(semid, &array, target_su.array);
3491 if (err)
3492 return err;
3493 arg.array = array;
3494 ret = get_errno(semctl(semid, semnum, cmd, arg));
3495 err = host_to_target_semarray(semid, target_su.array, &array);
3496 if (err)
3497 return err;
3498 break;
3499 case IPC_STAT:
3500 case IPC_SET:
3501 case SEM_STAT:
3502 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3503 if (err)
3504 return err;
3505 arg.buf = &dsarg;
3506 ret = get_errno(semctl(semid, semnum, cmd, arg));
3507 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3508 if (err)
3509 return err;
3510 break;
3511 case IPC_INFO:
3512 case SEM_INFO:
3513 arg.__buf = &seminfo;
3514 ret = get_errno(semctl(semid, semnum, cmd, arg));
3515 err = host_to_target_seminfo(target_su.__buf, &seminfo);
3516 if (err)
3517 return err;
3518 break;
3519 case IPC_RMID:
3520 case GETPID:
3521 case GETNCNT:
3522 case GETZCNT:
3523 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3524 break;
3527 return ret;
3530 struct target_sembuf {
3531 unsigned short sem_num;
3532 short sem_op;
3533 short sem_flg;
3536 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
3537 abi_ulong target_addr,
3538 unsigned nsops)
3540 struct target_sembuf *target_sembuf;
3541 int i;
3543 target_sembuf = lock_user(VERIFY_READ, target_addr,
3544 nsops*sizeof(struct target_sembuf), 1);
3545 if (!target_sembuf)
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);
3556 return 0;
3559 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
3561 struct sembuf sops[nsops];
3562 abi_long ret;
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));
3570 #endif
3571 #ifdef __NR_ipc
3572 if (ret == -TARGET_ENOSYS) {
3573 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid, nsops, 0, sops, 0));
3575 #endif
3576 return ret;
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;
3585 #endif
3586 abi_ulong msg_rtime;
3587 #if TARGET_ABI_BITS == 32
3588 abi_ulong __unused2;
3589 #endif
3590 abi_ulong msg_ctime;
3591 #if TARGET_ABI_BITS == 32
3592 abi_ulong __unused3;
3593 #endif
3594 abi_ulong __msg_cbytes;
3595 abi_ulong msg_qnum;
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);
3621 return 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);
3642 return 0;
3645 struct target_msginfo {
3646 int msgpool;
3647 int msgmap;
3648 int msgmax;
3649 int msgmnb;
3650 int msgmni;
3651 int msgssz;
3652 int msgtql;
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);
3671 return 0;
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;
3680 cmd &= 0xff;
3682 switch (cmd) {
3683 case IPC_STAT:
3684 case IPC_SET:
3685 case MSG_STAT:
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;
3691 break;
3692 case IPC_RMID:
3693 ret = get_errno(msgctl(msgid, cmd, NULL));
3694 break;
3695 case IPC_INFO:
3696 case MSG_INFO:
3697 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
3698 if (host_to_target_msginfo(ptr, &msginfo))
3699 return -TARGET_EFAULT;
3700 break;
3703 return ret;
3706 struct target_msgbuf {
3707 abi_long mtype;
3708 char mtext[1];
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;
3716 abi_long ret = 0;
3718 if (msgsz < 0) {
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));
3725 if (!host_mb) {
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;
3732 #ifdef __NR_msgsnd
3733 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
3734 #endif
3735 #ifdef __NR_ipc
3736 if (ret == -TARGET_ENOSYS) {
3737 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
3738 host_mb, 0));
3740 #endif
3741 g_free(host_mb);
3742 unlock_user_struct(target_mb, msgp, 0);
3744 return ret;
3747 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
3748 ssize_t msgsz, abi_long msgtyp,
3749 int msgflg)
3751 struct target_msgbuf *target_mb;
3752 char *target_mtext;
3753 struct msgbuf *host_mb;
3754 abi_long ret = 0;
3756 if (msgsz < 0) {
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));
3764 if (!host_mb) {
3765 ret = -TARGET_ENOMEM;
3766 goto end;
3768 ret = -TARGET_ENOSYS;
3769 #ifdef __NR_msgrcv
3770 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
3771 #endif
3772 #ifdef __NR_ipc
3773 if (ret == -TARGET_ENOSYS) {
3774 ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
3775 msgflg, host_mb, msgtyp));
3777 #endif
3779 if (ret > 0) {
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;
3784 goto end;
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);
3792 end:
3793 if (target_mb)
3794 unlock_user_struct(target_mb, msgp, 1);
3795 g_free(host_mb);
3796 return ret;
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);
3816 return 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_natt