Merge tag 'pull-target-arm-20220812' of https://git.linaro.org/people/pmaydell/qemu...
[qemu.git] / linux-user / syscall.c
1 /*
2 * Linux syscalls
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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/>.
18 */
19 #define _ATFILE_SOURCE
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
23 #include "qemu/memfd.h"
24 #include "qemu/queue.h"
25 #include <elf.h>
26 #include <endian.h>
27 #include <grp.h>
28 #include <sys/ipc.h>
29 #include <sys/msg.h>
30 #include <sys/wait.h>
31 #include <sys/mount.h>
32 #include <sys/file.h>
33 #include <sys/fsuid.h>
34 #include <sys/personality.h>
35 #include <sys/prctl.h>
36 #include <sys/resource.h>
37 #include <sys/swap.h>
38 #include <linux/capability.h>
39 #include <sched.h>
40 #include <sys/timex.h>
41 #include <sys/socket.h>
42 #include <linux/sockios.h>
43 #include <sys/un.h>
44 #include <sys/uio.h>
45 #include <poll.h>
46 #include <sys/times.h>
47 #include <sys/shm.h>
48 #include <sys/sem.h>
49 #include <sys/statfs.h>
50 #include <utime.h>
51 #include <sys/sysinfo.h>
52 #include <sys/signalfd.h>
53 //#include <sys/user.h>
54 #include <netinet/in.h>
55 #include <netinet/ip.h>
56 #include <netinet/tcp.h>
57 #include <netinet/udp.h>
58 #include <linux/wireless.h>
59 #include <linux/icmp.h>
60 #include <linux/icmpv6.h>
61 #include <linux/if_tun.h>
62 #include <linux/in6.h>
63 #include <linux/errqueue.h>
64 #include <linux/random.h>
65 #ifdef CONFIG_TIMERFD
66 #include <sys/timerfd.h>
67 #endif
68 #ifdef CONFIG_EVENTFD
69 #include <sys/eventfd.h>
70 #endif
71 #ifdef CONFIG_EPOLL
72 #include <sys/epoll.h>
73 #endif
74 #ifdef CONFIG_ATTR
75 #include "qemu/xattr.h"
76 #endif
77 #ifdef CONFIG_SENDFILE
78 #include <sys/sendfile.h>
79 #endif
80 #ifdef HAVE_SYS_KCOV_H
81 #include <sys/kcov.h>
82 #endif
83
84 #define termios host_termios
85 #define winsize host_winsize
86 #define termio host_termio
87 #define sgttyb host_sgttyb /* same as target */
88 #define tchars host_tchars /* same as target */
89 #define ltchars host_ltchars /* same as target */
90
91 #include <linux/termios.h>
92 #include <linux/unistd.h>
93 #include <linux/cdrom.h>
94 #include <linux/hdreg.h>
95 #include <linux/soundcard.h>
96 #include <linux/kd.h>
97 #include <linux/mtio.h>
98
99 #ifdef HAVE_SYS_MOUNT_FSCONFIG
100 /*
101 * glibc >= 2.36 linux/mount.h conflicts with sys/mount.h,
102 * which in turn prevents use of linux/fs.h. So we have to
103 * define the constants ourselves for now.
104 */
105 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
106 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
107 #define FS_IOC_GETVERSION _IOR('v', 1, long)
108 #define FS_IOC_SETVERSION _IOW('v', 2, long)
109 #define FS_IOC_FIEMAP _IOWR('f', 11, struct fiemap)
110 #define FS_IOC32_GETFLAGS _IOR('f', 1, int)
111 #define FS_IOC32_SETFLAGS _IOW('f', 2, int)
112 #define FS_IOC32_GETVERSION _IOR('v', 1, int)
113 #define FS_IOC32_SETVERSION _IOW('v', 2, int)
114 #else
115 #include <linux/fs.h>
116 #endif
117 #include <linux/fd.h>
118 #if defined(CONFIG_FIEMAP)
119 #include <linux/fiemap.h>
120 #endif
121 #include <linux/fb.h>
122 #if defined(CONFIG_USBFS)
123 #include <linux/usbdevice_fs.h>
124 #include <linux/usb/ch9.h>
125 #endif
126 #include <linux/vt.h>
127 #include <linux/dm-ioctl.h>
128 #include <linux/reboot.h>
129 #include <linux/route.h>
130 #include <linux/filter.h>
131 #include <linux/blkpg.h>
132 #include <netpacket/packet.h>
133 #include <linux/netlink.h>
134 #include <linux/if_alg.h>
135 #include <linux/rtc.h>
136 #include <sound/asound.h>
137 #ifdef HAVE_BTRFS_H
138 #include <linux/btrfs.h>
139 #endif
140 #ifdef HAVE_DRM_H
141 #include <libdrm/drm.h>
142 #include <libdrm/i915_drm.h>
143 #endif
144 #include "linux_loop.h"
145 #include "uname.h"
146
147 #include "qemu.h"
148 #include "user-internals.h"
149 #include "strace.h"
150 #include "signal-common.h"
151 #include "loader.h"
152 #include "user-mmap.h"
153 #include "user/safe-syscall.h"
154 #include "qemu/guest-random.h"
155 #include "qemu/selfmap.h"
156 #include "user/syscall-trace.h"
157 #include "special-errno.h"
158 #include "qapi/error.h"
159 #include "fd-trans.h"
160 #include "tcg/tcg.h"
161
162 #ifndef CLONE_IO
163 #define CLONE_IO 0x80000000 /* Clone io context */
164 #endif
165
166 /* We can't directly call the host clone syscall, because this will
167 * badly confuse libc (breaking mutexes, for example). So we must
168 * divide clone flags into:
169 * * flag combinations that look like pthread_create()
170 * * flag combinations that look like fork()
171 * * flags we can implement within QEMU itself
172 * * flags we can't support and will return an error for
173 */
174 /* For thread creation, all these flags must be present; for
175 * fork, none must be present.
176 */
177 #define CLONE_THREAD_FLAGS \
178 (CLONE_VM | CLONE_FS | CLONE_FILES | \
179 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
180
181 /* These flags are ignored:
182 * CLONE_DETACHED is now ignored by the kernel;
183 * CLONE_IO is just an optimisation hint to the I/O scheduler
184 */
185 #define CLONE_IGNORED_FLAGS \
186 (CLONE_DETACHED | CLONE_IO)
187
188 /* Flags for fork which we can implement within QEMU itself */
189 #define CLONE_OPTIONAL_FORK_FLAGS \
190 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
191 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
192
193 /* Flags for thread creation which we can implement within QEMU itself */
194 #define CLONE_OPTIONAL_THREAD_FLAGS \
195 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
196 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
197
198 #define CLONE_INVALID_FORK_FLAGS \
199 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
200
201 #define CLONE_INVALID_THREAD_FLAGS \
202 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
203 CLONE_IGNORED_FLAGS))
204
205 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
206 * have almost all been allocated. We cannot support any of
207 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
208 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
209 * The checks against the invalid thread masks above will catch these.
210 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
211 */
212
213 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
214 * once. This exercises the codepaths for restart.
215 */
216 //#define DEBUG_ERESTARTSYS
217
218 //#include <linux/msdos_fs.h>
219 #define VFAT_IOCTL_READDIR_BOTH \
220 _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
221 #define VFAT_IOCTL_READDIR_SHORT \
222 _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
223
224 #undef _syscall0
225 #undef _syscall1
226 #undef _syscall2
227 #undef _syscall3
228 #undef _syscall4
229 #undef _syscall5
230 #undef _syscall6
231
232 #define _syscall0(type,name) \
233 static type name (void) \
234 { \
235 return syscall(__NR_##name); \
236 }
237
238 #define _syscall1(type,name,type1,arg1) \
239 static type name (type1 arg1) \
240 { \
241 return syscall(__NR_##name, arg1); \
242 }
243
244 #define _syscall2(type,name,type1,arg1,type2,arg2) \
245 static type name (type1 arg1,type2 arg2) \
246 { \
247 return syscall(__NR_##name, arg1, arg2); \
248 }
249
250 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
251 static type name (type1 arg1,type2 arg2,type3 arg3) \
252 { \
253 return syscall(__NR_##name, arg1, arg2, arg3); \
254 }
255
256 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
257 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
258 { \
259 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
260 }
261
262 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
263 type5,arg5) \
264 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
265 { \
266 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
267 }
268
269
270 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
271 type5,arg5,type6,arg6) \
272 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
273 type6 arg6) \
274 { \
275 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
276 }
277
278
279 #define __NR_sys_uname __NR_uname
280 #define __NR_sys_getcwd1 __NR_getcwd
281 #define __NR_sys_getdents __NR_getdents
282 #define __NR_sys_getdents64 __NR_getdents64
283 #define __NR_sys_getpriority __NR_getpriority
284 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
285 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
286 #define __NR_sys_syslog __NR_syslog
287 #if defined(__NR_futex)
288 # define __NR_sys_futex __NR_futex
289 #endif
290 #if defined(__NR_futex_time64)
291 # define __NR_sys_futex_time64 __NR_futex_time64
292 #endif
293 #define __NR_sys_statx __NR_statx
294
295 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
296 #define __NR__llseek __NR_lseek
297 #endif
298
299 /* Newer kernel ports have llseek() instead of _llseek() */
300 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
301 #define TARGET_NR__llseek TARGET_NR_llseek
302 #endif
303
304 /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
305 #ifndef TARGET_O_NONBLOCK_MASK
306 #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
307 #endif
308
309 #define __NR_sys_gettid __NR_gettid
310 _syscall0(int, sys_gettid)
311
312 /* For the 64-bit guest on 32-bit host case we must emulate
313 * getdents using getdents64, because otherwise the host
314 * might hand us back more dirent records than we can fit
315 * into the guest buffer after structure format conversion.
316 * Otherwise we emulate getdents with getdents if the host has it.
317 */
318 #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
319 #define EMULATE_GETDENTS_WITH_GETDENTS
320 #endif
321
322 #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
323 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
324 #endif
325 #if (defined(TARGET_NR_getdents) && \
326 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
327 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
328 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
329 #endif
330 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
331 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
332 loff_t *, res, uint, wh);
333 #endif
334 _syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
335 _syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
336 siginfo_t *, uinfo)
337 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
338 #ifdef __NR_exit_group
339 _syscall1(int,exit_group,int,error_code)
340 #endif
341 #if defined(__NR_futex)
342 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
343 const struct timespec *,timeout,int *,uaddr2,int,val3)
344 #endif
345 #if defined(__NR_futex_time64)
346 _syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
347 const struct timespec *,timeout,int *,uaddr2,int,val3)
348 #endif
349 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
350 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
351 unsigned long *, user_mask_ptr);
352 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
353 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
354 unsigned long *, user_mask_ptr);
355 /* sched_attr is not defined in glibc */
356 struct sched_attr {
357 uint32_t size;
358 uint32_t sched_policy;
359 uint64_t sched_flags;
360 int32_t sched_nice;
361 uint32_t sched_priority;
362 uint64_t sched_runtime;
363 uint64_t sched_deadline;
364 uint64_t sched_period;
365 uint32_t sched_util_min;
366 uint32_t sched_util_max;
367 };
368 #define __NR_sys_sched_getattr __NR_sched_getattr
369 _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
370 unsigned int, size, unsigned int, flags);
371 #define __NR_sys_sched_setattr __NR_sched_setattr
372 _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
373 unsigned int, flags);
374 #define __NR_sys_sched_getscheduler __NR_sched_getscheduler
375 _syscall1(int, sys_sched_getscheduler, pid_t, pid);
376 #define __NR_sys_sched_setscheduler __NR_sched_setscheduler
377 _syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
378 const struct sched_param *, param);
379 #define __NR_sys_sched_getparam __NR_sched_getparam
380 _syscall2(int, sys_sched_getparam, pid_t, pid,
381 struct sched_param *, param);
382 #define __NR_sys_sched_setparam __NR_sched_setparam
383 _syscall2(int, sys_sched_setparam, pid_t, pid,
384 const struct sched_param *, param);
385 #define __NR_sys_getcpu __NR_getcpu
386 _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
387 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
388 void *, arg);
389 _syscall2(int, capget, struct __user_cap_header_struct *, header,
390 struct __user_cap_data_struct *, data);
391 _syscall2(int, capset, struct __user_cap_header_struct *, header,
392 struct __user_cap_data_struct *, data);
393 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
394 _syscall2(int, ioprio_get, int, which, int, who)
395 #endif
396 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
397 _syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
398 #endif
399 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
400 _syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
401 #endif
402
403 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
404 _syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
405 unsigned long, idx1, unsigned long, idx2)
406 #endif
407
408 /*
409 * It is assumed that struct statx is architecture independent.
410 */
411 #if defined(TARGET_NR_statx) && defined(__NR_statx)
412 _syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
413 unsigned int, mask, struct target_statx *, statxbuf)
414 #endif
415 #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
416 _syscall2(int, membarrier, int, cmd, int, flags)
417 #endif
418
419 static const bitmask_transtbl fcntl_flags_tbl[] = {
420 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
421 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
422 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
423 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
424 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
425 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
426 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
427 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
428 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
429 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
430 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
431 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
432 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
433 #if defined(O_DIRECT)
434 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
435 #endif
436 #if defined(O_NOATIME)
437 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
438 #endif
439 #if defined(O_CLOEXEC)
440 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
441 #endif
442 #if defined(O_PATH)
443 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
444 #endif
445 #if defined(O_TMPFILE)
446 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
447 #endif
448 /* Don't terminate the list prematurely on 64-bit host+guest. */
449 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
450 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
451 #endif
452 { 0, 0, 0, 0 }
453 };
454
455 _syscall2(int, sys_getcwd1, char *, buf, size_t, size)
456
457 #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
458 #if defined(__NR_utimensat)
459 #define __NR_sys_utimensat __NR_utimensat
460 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
461 const struct timespec *,tsp,int,flags)
462 #else
463 static int sys_utimensat(int dirfd, const char *pathname,
464 const struct timespec times[2], int flags)
465 {
466 errno = ENOSYS;
467 return -1;
468 }
469 #endif
470 #endif /* TARGET_NR_utimensat */
471
472 #ifdef TARGET_NR_renameat2
473 #if defined(__NR_renameat2)
474 #define __NR_sys_renameat2 __NR_renameat2
475 _syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
476 const char *, new, unsigned int, flags)
477 #else
478 static int sys_renameat2(int oldfd, const char *old,
479 int newfd, const char *new, int flags)
480 {
481 if (flags == 0) {
482 return renameat(oldfd, old, newfd, new);
483 }
484 errno = ENOSYS;
485 return -1;
486 }
487 #endif
488 #endif /* TARGET_NR_renameat2 */
489
490 #ifdef CONFIG_INOTIFY
491 #include <sys/inotify.h>
492 #else
493 /* Userspace can usually survive runtime without inotify */
494 #undef TARGET_NR_inotify_init
495 #undef TARGET_NR_inotify_init1
496 #undef TARGET_NR_inotify_add_watch
497 #undef TARGET_NR_inotify_rm_watch
498 #endif /* CONFIG_INOTIFY */
499
500 #if defined(TARGET_NR_prlimit64)
501 #ifndef __NR_prlimit64
502 # define __NR_prlimit64 -1
503 #endif
504 #define __NR_sys_prlimit64 __NR_prlimit64
505 /* The glibc rlimit structure may not be that used by the underlying syscall */
506 struct host_rlimit64 {
507 uint64_t rlim_cur;
508 uint64_t rlim_max;
509 };
510 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
511 const struct host_rlimit64 *, new_limit,
512 struct host_rlimit64 *, old_limit)
513 #endif
514
515
516 #if defined(TARGET_NR_timer_create)
517 /* Maximum of 32 active POSIX timers allowed at any one time. */
518 static timer_t g_posix_timers[32] = { 0, } ;
519
520 static inline int next_free_host_timer(void)
521 {
522 int k ;
523 /* FIXME: Does finding the next free slot require a lock? */
524 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
525 if (g_posix_timers[k] == 0) {
526 g_posix_timers[k] = (timer_t) 1;
527 return k;
528 }
529 }
530 return -1;
531 }
532 #endif
533
534 static inline int host_to_target_errno(int host_errno)
535 {
536 switch (host_errno) {
537 #define E(X) case X: return TARGET_##X;
538 #include "errnos.c.inc"
539 #undef E
540 default:
541 return host_errno;
542 }
543 }
544
545 static inline int target_to_host_errno(int target_errno)
546 {
547 switch (target_errno) {
548 #define E(X) case TARGET_##X: return X;
549 #include "errnos.c.inc"
550 #undef E
551 default:
552 return target_errno;
553 }
554 }
555
556 abi_long get_errno(abi_long ret)
557 {
558 if (ret == -1)
559 return -host_to_target_errno(errno);
560 else
561 return ret;
562 }
563
564 const char *target_strerror(int err)
565 {
566 if (err == QEMU_ERESTARTSYS) {
567 return "To be restarted";
568 }
569 if (err == QEMU_ESIGRETURN) {
570 return "Successful exit from sigreturn";
571 }
572
573 return strerror(target_to_host_errno(err));
574 }
575
576 static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
577 {
578 int i;
579 uint8_t b;
580 if (usize <= ksize) {
581 return 1;
582 }
583 for (i = ksize; i < usize; i++) {
584 if (get_user_u8(b, addr + i)) {
585 return -TARGET_EFAULT;
586 }
587 if (b != 0) {
588 return 0;
589 }
590 }
591 return 1;
592 }
593
594 #define safe_syscall0(type, name) \
595 static type safe_##name(void) \
596 { \
597 return safe_syscall(__NR_##name); \
598 }
599
600 #define safe_syscall1(type, name, type1, arg1) \
601 static type safe_##name(type1 arg1) \
602 { \
603 return safe_syscall(__NR_##name, arg1); \
604 }
605
606 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
607 static type safe_##name(type1 arg1, type2 arg2) \
608 { \
609 return safe_syscall(__NR_##name, arg1, arg2); \
610 }
611
612 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
613 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
614 { \
615 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
616 }
617
618 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
619 type4, arg4) \
620 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
621 { \
622 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
623 }
624
625 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
626 type4, arg4, type5, arg5) \
627 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
628 type5 arg5) \
629 { \
630 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
631 }
632
633 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
634 type4, arg4, type5, arg5, type6, arg6) \
635 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
636 type5 arg5, type6 arg6) \
637 { \
638 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
639 }
640
641 safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
642 safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
643 safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
644 int, flags, mode_t, mode)
645 #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
646 safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
647 struct rusage *, rusage)
648 #endif
649 safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
650 int, options, struct rusage *, rusage)
651 safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
652 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
653 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
654 safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
655 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
656 #endif
657 #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
658 safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
659 struct timespec *, tsp, const sigset_t *, sigmask,
660 size_t, sigsetsize)
661 #endif
662 safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
663 int, maxevents, int, timeout, const sigset_t *, sigmask,
664 size_t, sigsetsize)
665 #if defined(__NR_futex)
666 safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
667 const struct timespec *,timeout,int *,uaddr2,int,val3)
668 #endif
669 #if defined(__NR_futex_time64)
670 safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
671 const struct timespec *,timeout,int *,uaddr2,int,val3)
672 #endif
673 safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
674 safe_syscall2(int, kill, pid_t, pid, int, sig)
675 safe_syscall2(int, tkill, int, tid, int, sig)
676 safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
677 safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
678 safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
679 safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
680 unsigned long, pos_l, unsigned long, pos_h)
681 safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
682 unsigned long, pos_l, unsigned long, pos_h)
683 safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
684 socklen_t, addrlen)
685 safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
686 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
687 safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
688 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
689 safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
690 safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
691 safe_syscall2(int, flock, int, fd, int, operation)
692 #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
693 safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
694 const struct timespec *, uts, size_t, sigsetsize)
695 #endif
696 safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
697 int, flags)
698 #if defined(TARGET_NR_nanosleep)
699 safe_syscall2(int, nanosleep, const struct timespec *, req,
700 struct timespec *, rem)
701 #endif
702 #if defined(TARGET_NR_clock_nanosleep) || \
703 defined(TARGET_NR_clock_nanosleep_time64)
704 safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
705 const struct timespec *, req, struct timespec *, rem)
706 #endif
707 #ifdef __NR_ipc
708 #ifdef __s390x__
709 safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
710 void *, ptr)
711 #else
712 safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
713 void *, ptr, long, fifth)
714 #endif
715 #endif
716 #ifdef __NR_msgsnd
717 safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
718 int, flags)
719 #endif
720 #ifdef __NR_msgrcv
721 safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
722 long, msgtype, int, flags)
723 #endif
724 #ifdef __NR_semtimedop
725 safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
726 unsigned, nsops, const struct timespec *, timeout)
727 #endif
728 #if defined(TARGET_NR_mq_timedsend) || \
729 defined(TARGET_NR_mq_timedsend_time64)
730 safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
731 size_t, len, unsigned, prio, const struct timespec *, timeout)
732 #endif
733 #if defined(TARGET_NR_mq_timedreceive) || \
734 defined(TARGET_NR_mq_timedreceive_time64)
735 safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
736 size_t, len, unsigned *, prio, const struct timespec *, timeout)
737 #endif
738 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
739 safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
740 int, outfd, loff_t *, poutoff, size_t, length,
741 unsigned int, flags)
742 #endif
743
744 /* We do ioctl like this rather than via safe_syscall3 to preserve the
745 * "third argument might be integer or pointer or not present" behaviour of
746 * the libc function.
747 */
748 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
749 /* Similarly for fcntl. Note that callers must always:
750 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
751 * use the flock64 struct rather than unsuffixed flock
752 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
753 */
754 #ifdef __NR_fcntl64
755 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
756 #else
757 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
758 #endif
759
760 static inline int host_to_target_sock_type(int host_type)
761 {
762 int target_type;
763
764 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
765 case SOCK_DGRAM:
766 target_type = TARGET_SOCK_DGRAM;
767 break;
768 case SOCK_STREAM:
769 target_type = TARGET_SOCK_STREAM;
770 break;
771 default:
772 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
773 break;
774 }
775
776 #if defined(SOCK_CLOEXEC)
777 if (host_type & SOCK_CLOEXEC) {
778 target_type |= TARGET_SOCK_CLOEXEC;
779 }
780 #endif
781
782 #if defined(SOCK_NONBLOCK)
783 if (host_type & SOCK_NONBLOCK) {
784 target_type |= TARGET_SOCK_NONBLOCK;
785 }
786 #endif
787
788 return target_type;
789 }
790
791 static abi_ulong target_brk;
792 static abi_ulong target_original_brk;
793 static abi_ulong brk_page;
794
795 void target_set_brk(abi_ulong new_brk)
796 {
797 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
798 brk_page = HOST_PAGE_ALIGN(target_brk);
799 }
800
801 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
802 #define DEBUGF_BRK(message, args...)
803
804 /* do_brk() must return target values and target errnos. */
805 abi_long do_brk(abi_ulong new_brk)
806 {
807 abi_long mapped_addr;
808 abi_ulong new_alloc_size;
809
810 /* brk pointers are always untagged */
811
812 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
813
814 if (!new_brk) {
815 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
816 return target_brk;
817 }
818 if (new_brk < target_original_brk) {
819 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
820 target_brk);
821 return target_brk;
822 }
823
824 /* If the new brk is less than the highest page reserved to the
825 * target heap allocation, set it and we're almost done... */
826 if (new_brk <= brk_page) {
827 /* Heap contents are initialized to zero, as for anonymous
828 * mapped pages. */
829 if (new_brk > target_brk) {
830 memset(g2h_untagged(target_brk), 0, new_brk - target_brk);
831 }
832 target_brk = new_brk;
833 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
834 return target_brk;
835 }
836
837 /* We need to allocate more memory after the brk... Note that
838 * we don't use MAP_FIXED because that will map over the top of
839 * any existing mapping (like the one with the host libc or qemu
840 * itself); instead we treat "mapped but at wrong address" as
841 * a failure and unmap again.
842 */
843 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
844 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
845 PROT_READ|PROT_WRITE,
846 MAP_ANON|MAP_PRIVATE, 0, 0));
847
848 if (mapped_addr == brk_page) {
849 /* Heap contents are initialized to zero, as for anonymous
850 * mapped pages. Technically the new pages are already
851 * initialized to zero since they *are* anonymous mapped
852 * pages, however we have to take care with the contents that
853 * come from the remaining part of the previous page: it may
854 * contains garbage data due to a previous heap usage (grown
855 * then shrunken). */
856 memset(g2h_untagged(target_brk), 0, brk_page - target_brk);
857
858 target_brk = new_brk;
859 brk_page = HOST_PAGE_ALIGN(target_brk);
860 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
861 target_brk);
862 return target_brk;
863 } else if (mapped_addr != -1) {
864 /* Mapped but at wrong address, meaning there wasn't actually
865 * enough space for this brk.
866 */
867 target_munmap(mapped_addr, new_alloc_size);
868 mapped_addr = -1;
869 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
870 }
871 else {
872 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
873 }
874
875 #if defined(TARGET_ALPHA)
876 /* We (partially) emulate OSF/1 on Alpha, which requires we
877 return a proper errno, not an unchanged brk value. */
878 return -TARGET_ENOMEM;
879 #endif
880 /* For everything else, return the previous break. */
881 return target_brk;
882 }
883
884 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
885 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
886 static inline abi_long copy_from_user_fdset(fd_set *fds,
887 abi_ulong target_fds_addr,
888 int n)
889 {
890 int i, nw, j, k;
891 abi_ulong b, *target_fds;
892
893 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
894 if (!(target_fds = lock_user(VERIFY_READ,
895 target_fds_addr,
896 sizeof(abi_ulong) * nw,
897 1)))
898 return -TARGET_EFAULT;
899
900 FD_ZERO(fds);
901 k = 0;
902 for (i = 0; i < nw; i++) {
903 /* grab the abi_ulong */
904 __get_user(b, &target_fds[i]);
905 for (j = 0; j < TARGET_ABI_BITS; j++) {
906 /* check the bit inside the abi_ulong */
907 if ((b >> j) & 1)
908 FD_SET(k, fds);
909 k++;
910 }
911 }
912
913 unlock_user(target_fds, target_fds_addr, 0);
914
915 return 0;
916 }
917
918 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
919 abi_ulong target_fds_addr,
920 int n)
921 {
922 if (target_fds_addr) {
923 if (copy_from_user_fdset(fds, target_fds_addr, n))
924 return -TARGET_EFAULT;
925 *fds_ptr = fds;
926 } else {
927 *fds_ptr = NULL;
928 }
929 return 0;
930 }
931
932 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
933 const fd_set *fds,
934 int n)
935 {
936 int i, nw, j, k;
937 abi_long v;
938 abi_ulong *target_fds;
939
940 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
941 if (!(target_fds = lock_user(VERIFY_WRITE,
942 target_fds_addr,
943 sizeof(abi_ulong) * nw,
944 0)))
945 return -TARGET_EFAULT;
946
947 k = 0;
948 for (i = 0; i < nw; i++) {
949 v = 0;
950 for (j = 0; j < TARGET_ABI_BITS; j++) {
951 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
952 k++;
953 }
954 __put_user(v, &target_fds[i]);
955 }
956
957 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
958
959 return 0;
960 }
961 #endif
962
963 #if defined(__alpha__)
964 #define HOST_HZ 1024
965 #else
966 #define HOST_HZ 100
967 #endif
968
969 static inline abi_long host_to_target_clock_t(long ticks)
970 {
971 #if HOST_HZ == TARGET_HZ
972 return ticks;
973 #else
974 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
975 #endif
976 }
977
978 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
979 const struct rusage *rusage)
980 {
981 struct target_rusage *target_rusage;
982
983 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
984 return -TARGET_EFAULT;
985 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
986 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
987 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
988 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
989 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
990 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
991 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
992 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
993 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
994 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
995 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
996 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
997 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
998 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
999 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
1000 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
1001 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
1002 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
1003 unlock_user_struct(target_rusage, target_addr, 1);
1004
1005 return 0;
1006 }
1007
1008 #ifdef TARGET_NR_setrlimit
1009 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
1010 {
1011 abi_ulong target_rlim_swap;
1012 rlim_t result;
1013
1014 target_rlim_swap = tswapal(target_rlim);
1015 if (target_rlim_swap == TARGET_RLIM_INFINITY)
1016 return RLIM_INFINITY;
1017
1018 result = target_rlim_swap;
1019 if (target_rlim_swap != (rlim_t)result)
1020 return RLIM_INFINITY;
1021
1022 return result;
1023 }
1024 #endif
1025
1026 #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
1027 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
1028 {
1029 abi_ulong target_rlim_swap;
1030 abi_ulong result;
1031
1032 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
1033 target_rlim_swap = TARGET_RLIM_INFINITY;
1034 else
1035 target_rlim_swap = rlim;
1036 result = tswapal(target_rlim_swap);
1037
1038 return result;
1039 }
1040 #endif
1041
1042 static inline int target_to_host_resource(int code)
1043 {
1044 switch (code) {
1045 case TARGET_RLIMIT_AS:
1046 return RLIMIT_AS;
1047 case TARGET_RLIMIT_CORE:
1048 return RLIMIT_CORE;
1049 case TARGET_RLIMIT_CPU:
1050 return RLIMIT_CPU;
1051 case TARGET_RLIMIT_DATA:
1052 return RLIMIT_DATA;
1053 case TARGET_RLIMIT_FSIZE:
1054 return RLIMIT_FSIZE;
1055 case TARGET_RLIMIT_LOCKS:
1056 return RLIMIT_LOCKS;
1057 case TARGET_RLIMIT_MEMLOCK:
1058 return RLIMIT_MEMLOCK;
1059 case TARGET_RLIMIT_MSGQUEUE:
1060 return RLIMIT_MSGQUEUE;
1061 case TARGET_RLIMIT_NICE:
1062 return RLIMIT_NICE;
1063 case TARGET_RLIMIT_NOFILE:
1064 return RLIMIT_NOFILE;
1065 case TARGET_RLIMIT_NPROC:
1066 return RLIMIT_NPROC;
1067 case TARGET_RLIMIT_RSS:
1068 return RLIMIT_RSS;
1069 case TARGET_RLIMIT_RTPRIO:
1070 return RLIMIT_RTPRIO;
1071 #ifdef RLIMIT_RTTIME
1072 case TARGET_RLIMIT_RTTIME:
1073 return RLIMIT_RTTIME;
1074 #endif
1075 case TARGET_RLIMIT_SIGPENDING:
1076 return RLIMIT_SIGPENDING;
1077 case TARGET_RLIMIT_STACK:
1078 return RLIMIT_STACK;
1079 default:
1080 return code;
1081 }
1082 }
1083
1084 static inline abi_long copy_from_user_timeval(struct timeval *tv,
1085 abi_ulong target_tv_addr)
1086 {
1087 struct target_timeval *target_tv;
1088
1089 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
1090 return -TARGET_EFAULT;
1091 }
1092
1093 __get_user(tv->tv_sec, &target_tv->tv_sec);
1094 __get_user(tv->tv_usec, &target_tv->tv_usec);
1095
1096 unlock_user_struct(target_tv, target_tv_addr, 0);
1097
1098 return 0;
1099 }
1100
1101 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1102 const struct timeval *tv)
1103 {
1104 struct target_timeval *target_tv;
1105
1106 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
1107 return -TARGET_EFAULT;
1108 }
1109
1110 __put_user(tv->tv_sec, &target_tv->tv_sec);
1111 __put_user(tv->tv_usec, &target_tv->tv_usec);
1112
1113 unlock_user_struct(target_tv, target_tv_addr, 1);
1114
1115 return 0;
1116 }
1117
1118 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
1119 static inline abi_long copy_from_user_timeval64(struct timeval *tv,
1120 abi_ulong target_tv_addr)
1121 {
1122 struct target__kernel_sock_timeval *target_tv;
1123
1124 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
1125 return -TARGET_EFAULT;
1126 }
1127
1128 __get_user(tv->tv_sec, &target_tv->tv_sec);
1129 __get_user(tv->tv_usec, &target_tv->tv_usec);
1130
1131 unlock_user_struct(target_tv, target_tv_addr, 0);
1132
1133 return 0;
1134 }
1135 #endif
1136
1137 static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
1138 const struct timeval *tv)
1139 {
1140 struct target__kernel_sock_timeval *target_tv;
1141
1142 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
1143 return -TARGET_EFAULT;
1144 }
1145
1146 __put_user(tv->tv_sec, &target_tv->tv_sec);
1147 __put_user(tv->tv_usec, &target_tv->tv_usec);
1148
1149 unlock_user_struct(target_tv, target_tv_addr, 1);
1150
1151 return 0;
1152 }
1153
1154 #if defined(TARGET_NR_futex) || \
1155 defined(TARGET_NR_rt_sigtimedwait) || \
1156 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1157 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1158 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
1159 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
1160 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
1161 defined(TARGET_NR_timer_settime) || \
1162 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
1163 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
1164 abi_ulong target_addr)
1165 {
1166 struct target_timespec *target_ts;
1167
1168 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1169 return -TARGET_EFAULT;
1170 }
1171 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1172 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1173 unlock_user_struct(target_ts, target_addr, 0);
1174 return 0;
1175 }
1176 #endif
1177
1178 #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
1179 defined(TARGET_NR_timer_settime64) || \
1180 defined(TARGET_NR_mq_timedsend_time64) || \
1181 defined(TARGET_NR_mq_timedreceive_time64) || \
1182 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
1183 defined(TARGET_NR_clock_nanosleep_time64) || \
1184 defined(TARGET_NR_rt_sigtimedwait_time64) || \
1185 defined(TARGET_NR_utimensat) || \
1186 defined(TARGET_NR_utimensat_time64) || \
1187 defined(TARGET_NR_semtimedop_time64) || \
1188 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
1189 static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
1190 abi_ulong target_addr)
1191 {
1192 struct target__kernel_timespec *target_ts;
1193
1194 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1195 return -TARGET_EFAULT;
1196 }
1197 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1198 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1199 /* in 32bit mode, this drops the padding */
1200 host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
1201 unlock_user_struct(target_ts, target_addr, 0);
1202 return 0;
1203 }
1204 #endif
1205
1206 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
1207 struct timespec *host_ts)
1208 {
1209 struct target_timespec *target_ts;
1210
1211 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1212 return -TARGET_EFAULT;
1213 }
1214 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1215 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1216 unlock_user_struct(target_ts, target_addr, 1);
1217 return 0;
1218 }
1219
1220 static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
1221 struct timespec *host_ts)
1222 {
1223 struct target__kernel_timespec *target_ts;
1224
1225 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1226 return -TARGET_EFAULT;
1227 }
1228 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1229 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1230 unlock_user_struct(target_ts, target_addr, 1);
1231 return 0;
1232 }
1233
1234 #if defined(TARGET_NR_gettimeofday)
1235 static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
1236 struct timezone *tz)
1237 {
1238 struct target_timezone *target_tz;
1239
1240 if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
1241 return -TARGET_EFAULT;
1242 }
1243
1244 __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1245 __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1246
1247 unlock_user_struct(target_tz, target_tz_addr, 1);
1248
1249 return 0;
1250 }
1251 #endif
1252
1253 #if defined(TARGET_NR_settimeofday)
1254 static inline abi_long copy_from_user_timezone(struct timezone *tz,
1255 abi_ulong target_tz_addr)
1256 {
1257 struct target_timezone *target_tz;
1258
1259 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1260 return -TARGET_EFAULT;
1261 }
1262
1263 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1264 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1265
1266 unlock_user_struct(target_tz, target_tz_addr, 0);
1267
1268 return 0;
1269 }
1270 #endif
1271
1272 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1273 #include <mqueue.h>
1274
1275 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1276 abi_ulong target_mq_attr_addr)
1277 {
1278 struct target_mq_attr *target_mq_attr;
1279
1280 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1281 target_mq_attr_addr, 1))
1282 return -TARGET_EFAULT;
1283
1284 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1285 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1286 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1287 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1288
1289 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1290
1291 return 0;
1292 }
1293
1294 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1295 const struct mq_attr *attr)
1296 {
1297 struct target_mq_attr *target_mq_attr;
1298
1299 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1300 target_mq_attr_addr, 0))
1301 return -TARGET_EFAULT;
1302
1303 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1304 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1305 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1306 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1307
1308 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1309
1310 return 0;
1311 }
1312 #endif
1313
1314 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1315 /* do_select() must return target values and target errnos. */
1316 static abi_long do_select(int n,
1317 abi_ulong rfd_addr, abi_ulong wfd_addr,
1318 abi_ulong efd_addr, abi_ulong target_tv_addr)
1319 {
1320 fd_set rfds, wfds, efds;
1321 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1322 struct timeval tv;
1323 struct timespec ts, *ts_ptr;
1324 abi_long ret;
1325
1326 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1327 if (ret) {
1328 return ret;
1329 }
1330 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1331 if (ret) {
1332 return ret;
1333 }
1334 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1335 if (ret) {
1336 return ret;
1337 }
1338
1339 if (target_tv_addr) {
1340 if (copy_from_user_timeval(&tv, target_tv_addr))
1341 return -TARGET_EFAULT;
1342 ts.tv_sec = tv.tv_sec;
1343 ts.tv_nsec = tv.tv_usec * 1000;
1344 ts_ptr = &ts;
1345 } else {
1346 ts_ptr = NULL;
1347 }
1348
1349 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1350 ts_ptr, NULL));
1351
1352 if (!is_error(ret)) {
1353 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1354 return -TARGET_EFAULT;
1355 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1356 return -TARGET_EFAULT;
1357 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1358 return -TARGET_EFAULT;
1359
1360 if (target_tv_addr) {
1361 tv.tv_sec = ts.tv_sec;
1362 tv.tv_usec = ts.tv_nsec / 1000;
1363 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1364 return -TARGET_EFAULT;
1365 }
1366 }
1367 }
1368
1369 return ret;
1370 }
1371
1372 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1373 static abi_long do_old_select(abi_ulong arg1)
1374 {
1375 struct target_sel_arg_struct *sel;
1376 abi_ulong inp, outp, exp, tvp;
1377 long nsel;
1378
1379 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1380 return -TARGET_EFAULT;
1381 }
1382
1383 nsel = tswapal(sel->n);
1384 inp = tswapal(sel->inp);
1385 outp = tswapal(sel->outp);
1386 exp = tswapal(sel->exp);
1387 tvp = tswapal(sel->tvp);
1388
1389 unlock_user_struct(sel, arg1, 0);
1390
1391 return do_select(nsel, inp, outp, exp, tvp);
1392 }
1393 #endif
1394 #endif
1395
1396 #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
1397 static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
1398 abi_long arg4, abi_long arg5, abi_long arg6,
1399 bool time64)
1400 {
1401 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
1402 fd_set rfds, wfds, efds;
1403 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1404 struct timespec ts, *ts_ptr;
1405 abi_long ret;
1406
1407 /*
1408 * The 6th arg is actually two args smashed together,
1409 * so we cannot use the C library.
1410 */
1411 struct {
1412 sigset_t *set;
1413 size_t size;
1414 } sig, *sig_ptr;
1415
1416 abi_ulong arg_sigset, arg_sigsize, *arg7;
1417
1418 n = arg1;
1419 rfd_addr = arg2;
1420 wfd_addr = arg3;
1421 efd_addr = arg4;
1422 ts_addr = arg5;
1423
1424 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1425 if (ret) {
1426 return ret;
1427 }
1428 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1429 if (ret) {
1430 return ret;
1431 }
1432 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1433 if (ret) {
1434 return ret;
1435 }
1436
1437 /*
1438 * This takes a timespec, and not a timeval, so we cannot
1439 * use the do_select() helper ...
1440 */
1441 if (ts_addr) {
1442 if (time64) {
1443 if (target_to_host_timespec64(&ts, ts_addr)) {
1444 return -TARGET_EFAULT;
1445 }
1446 } else {
1447 if (target_to_host_timespec(&ts, ts_addr)) {
1448 return -TARGET_EFAULT;
1449 }
1450 }
1451 ts_ptr = &ts;
1452 } else {
1453 ts_ptr = NULL;
1454 }
1455
1456 /* Extract the two packed args for the sigset */
1457 sig_ptr = NULL;
1458 if (arg6) {
1459 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
1460 if (!arg7) {
1461 return -TARGET_EFAULT;
1462 }
1463 arg_sigset = tswapal(arg7[0]);
1464 arg_sigsize = tswapal(arg7[1]);
1465 unlock_user(arg7, arg6, 0);
1466
1467 if (arg_sigset) {
1468 ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
1469 if (ret != 0) {
1470 return ret;
1471 }
1472 sig_ptr = &sig;
1473 sig.size = SIGSET_T_SIZE;
1474 }
1475 }
1476
1477 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1478 ts_ptr, sig_ptr));
1479
1480 if (sig_ptr) {
1481 finish_sigsuspend_mask(ret);
1482 }
1483
1484 if (!is_error(ret)) {
1485 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
1486 return -TARGET_EFAULT;
1487 }
1488 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
1489 return -TARGET_EFAULT;
1490 }
1491 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
1492 return -TARGET_EFAULT;
1493 }
1494 if (time64) {
1495 if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
1496 return -TARGET_EFAULT;
1497 }
1498 } else {
1499 if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
1500 return -TARGET_EFAULT;
1501 }
1502 }
1503 }
1504 return ret;
1505 }
1506 #endif
1507
1508 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
1509 defined(TARGET_NR_ppoll_time64)
1510 static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
1511 abi_long arg4, abi_long arg5, bool ppoll, bool time64)
1512 {
1513 struct target_pollfd *target_pfd;
1514 unsigned int nfds = arg2;
1515 struct pollfd *pfd;
1516 unsigned int i;
1517 abi_long ret;
1518
1519 pfd = NULL;
1520 target_pfd = NULL;
1521 if (nfds) {
1522 if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
1523 return -TARGET_EINVAL;
1524 }
1525 target_pfd = lock_user(VERIFY_WRITE, arg1,
1526 sizeof(struct target_pollfd) * nfds, 1);
1527 if (!target_pfd) {
1528 return -TARGET_EFAULT;
1529 }
1530
1531 pfd = alloca(sizeof(struct pollfd) * nfds);
1532 for (i = 0; i < nfds; i++) {
1533 pfd[i].fd = tswap32(target_pfd[i].fd);
1534 pfd[i].events = tswap16(target_pfd[i].events);
1535 }
1536 }
1537 if (ppoll) {
1538 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
1539 sigset_t *set = NULL;
1540
1541 if (arg3) {
1542 if (time64) {
1543 if (target_to_host_timespec64(timeout_ts, arg3)) {
1544 unlock_user(target_pfd, arg1, 0);
1545 return -TARGET_EFAULT;
1546 }
1547 } else {
1548 if (target_to_host_timespec(timeout_ts, arg3)) {
1549 unlock_user(target_pfd, arg1, 0);
1550 return -TARGET_EFAULT;
1551 }
1552 }
1553 } else {
1554 timeout_ts = NULL;
1555 }
1556
1557 if (arg4) {
1558 ret = process_sigsuspend_mask(&set, arg4, arg5);
1559 if (ret != 0) {
1560 unlock_user(target_pfd, arg1, 0);
1561 return ret;
1562 }
1563 }
1564
1565 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
1566 set, SIGSET_T_SIZE));
1567
1568 if (set) {
1569 finish_sigsuspend_mask(ret);
1570 }
1571 if (!is_error(ret) && arg3) {
1572 if (time64) {
1573 if (host_to_target_timespec64(arg3, timeout_ts)) {
1574 return -TARGET_EFAULT;
1575 }
1576 } else {
1577 if (host_to_target_timespec(arg3, timeout_ts)) {
1578 return -TARGET_EFAULT;
1579 }
1580 }
1581 }
1582 } else {
1583 struct timespec ts, *pts;
1584
1585 if (arg3 >= 0) {
1586 /* Convert ms to secs, ns */
1587 ts.tv_sec = arg3 / 1000;
1588 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
1589 pts = &ts;
1590 } else {
1591 /* -ve poll() timeout means "infinite" */
1592 pts = NULL;
1593 }
1594 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
1595 }
1596
1597 if (!is_error(ret)) {
1598 for (i = 0; i < nfds; i++) {
1599 target_pfd[i].revents = tswap16(pfd[i].revents);
1600 }
1601 }
1602 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
1603 return ret;
1604 }
1605 #endif
1606
1607 static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
1608 int flags, int is_pipe2)
1609 {
1610 int host_pipe[2];
1611 abi_long ret;
1612 ret = pipe2(host_pipe, flags);
1613
1614 if (is_error(ret))
1615 return get_errno(ret);
1616
1617 /* Several targets have special calling conventions for the original
1618 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1619 if (!is_pipe2) {
1620 #if defined(TARGET_ALPHA)
1621 cpu_env->ir[IR_A4] = host_pipe[1];
1622 return host_pipe[0];
1623 #elif defined(TARGET_MIPS)
1624 cpu_env->active_tc.gpr[3] = host_pipe[1];
1625 return host_pipe[0];
1626 #elif defined(TARGET_SH4)
1627 cpu_env->gregs[1] = host_pipe[1];
1628 return host_pipe[0];
1629 #elif defined(TARGET_SPARC)
1630 cpu_env->regwptr[1] = host_pipe[1];
1631 return host_pipe[0];
1632 #endif
1633 }
1634
1635 if (put_user_s32(host_pipe[0], pipedes)
1636 || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
1637 return -TARGET_EFAULT;
1638 return get_errno(ret);
1639 }
1640
1641 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1642 abi_ulong target_addr,
1643 socklen_t len)
1644 {
1645 struct target_ip_mreqn *target_smreqn;
1646
1647 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1648 if (!target_smreqn)
1649 return -TARGET_EFAULT;
1650 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1651 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1652 if (len == sizeof(struct target_ip_mreqn))
1653 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1654 unlock_user(target_smreqn, target_addr, 0);
1655
1656 return 0;
1657 }
1658
1659 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
1660 abi_ulong target_addr,
1661 socklen_t len)
1662 {
1663 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1664 sa_family_t sa_family;
1665 struct target_sockaddr *target_saddr;
1666
1667 if (fd_trans_target_to_host_addr(fd)) {
1668 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1669 }
1670
1671 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1672 if (!target_saddr)
1673 return -TARGET_EFAULT;
1674
1675 sa_family = tswap16(target_saddr->sa_family);
1676
1677 /* Oops. The caller might send a incomplete sun_path; sun_path
1678 * must be terminated by \0 (see the manual page), but
1679 * unfortunately it is quite common to specify sockaddr_un
1680 * length as "strlen(x->sun_path)" while it should be
1681 * "strlen(...) + 1". We'll fix that here if needed.
1682 * Linux kernel has a similar feature.
1683 */
1684
1685 if (sa_family == AF_UNIX) {
1686 if (len < unix_maxlen && len > 0) {
1687 char *cp = (char*)target_saddr;
1688
1689 if ( cp[len-1] && !cp[len] )
1690 len++;
1691 }
1692 if (len > unix_maxlen)
1693 len = unix_maxlen;
1694 }
1695
1696 memcpy(addr, target_saddr, len);
1697 addr->sa_family = sa_family;
1698 if (sa_family == AF_NETLINK) {
1699 struct sockaddr_nl *nladdr;
1700
1701 nladdr = (struct sockaddr_nl *)addr;
1702 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1703 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1704 } else if (sa_family == AF_PACKET) {
1705 struct target_sockaddr_ll *lladdr;
1706
1707 lladdr = (struct target_sockaddr_ll *)addr;
1708 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1709 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1710 }
1711 unlock_user(target_saddr, target_addr, 0);
1712
1713 return 0;
1714 }
1715
1716 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1717 struct sockaddr *addr,
1718 socklen_t len)
1719 {
1720 struct target_sockaddr *target_saddr;
1721
1722 if (len == 0) {
1723 return 0;
1724 }
1725 assert(addr);
1726
1727 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1728 if (!target_saddr)
1729 return -TARGET_EFAULT;
1730 memcpy(target_saddr, addr, len);
1731 if (len >= offsetof(struct target_sockaddr, sa_family) +
1732 sizeof(target_saddr->sa_family)) {
1733 target_saddr->sa_family = tswap16(addr->sa_family);
1734 }
1735 if (addr->sa_family == AF_NETLINK &&
1736 len >= sizeof(struct target_sockaddr_nl)) {
1737 struct target_sockaddr_nl *target_nl =
1738 (struct target_sockaddr_nl *)target_saddr;
1739 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1740 target_nl->nl_groups = tswap32(target_nl->nl_groups);
1741 } else if (addr->sa_family == AF_PACKET) {
1742 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1743 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1744 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
1745 } else if (addr->sa_family == AF_INET6 &&
1746 len >= sizeof(struct target_sockaddr_in6)) {
1747 struct target_sockaddr_in6 *target_in6 =
1748 (struct target_sockaddr_in6 *)target_saddr;
1749 target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
1750 }
1751 unlock_user(target_saddr, target_addr, len);
1752
1753 return 0;
1754 }
1755
1756 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1757 struct target_msghdr *target_msgh)
1758 {
1759 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1760 abi_long msg_controllen;
1761 abi_ulong target_cmsg_addr;
1762 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1763 socklen_t space = 0;
1764
1765 msg_controllen = tswapal(target_msgh->msg_controllen);
1766 if (msg_controllen < sizeof (struct target_cmsghdr))
1767 goto the_end;
1768 target_cmsg_addr = tswapal(target_msgh->msg_control);
1769 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1770 target_cmsg_start = target_cmsg;
1771 if (!target_cmsg)
1772 return -TARGET_EFAULT;
1773
1774 while (cmsg && target_cmsg) {
1775 void *data = CMSG_DATA(cmsg);
1776 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1777
1778 int len = tswapal(target_cmsg->cmsg_len)
1779 - sizeof(struct target_cmsghdr);
1780
1781 space += CMSG_SPACE(len);
1782 if (space > msgh->msg_controllen) {
1783 space -= CMSG_SPACE(len);
1784 /* This is a QEMU bug, since we allocated the payload
1785 * area ourselves (unlike overflow in host-to-target
1786 * conversion, which is just the guest giving us a buffer
1787 * that's too small). It can't happen for the payload types
1788 * we currently support; if it becomes an issue in future
1789 * we would need to improve our allocation strategy to
1790 * something more intelligent than "twice the size of the
1791 * target buffer we're reading from".
1792 */
1793 qemu_log_mask(LOG_UNIMP,
1794 ("Unsupported ancillary data %d/%d: "
1795 "unhandled msg size\n"),
1796 tswap32(target_cmsg->cmsg_level),
1797 tswap32(target_cmsg->cmsg_type));
1798 break;
1799 }
1800
1801 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1802 cmsg->cmsg_level = SOL_SOCKET;
1803 } else {
1804 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1805 }
1806 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1807 cmsg->cmsg_len = CMSG_LEN(len);
1808
1809 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1810 int *fd = (int *)data;
1811 int *target_fd = (int *)target_data;
1812 int i, numfds = len / sizeof(int);
1813
1814 for (i = 0; i < numfds; i++) {
1815 __get_user(fd[i], target_fd + i);
1816 }
1817 } else if (cmsg->cmsg_level == SOL_SOCKET
1818 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1819 struct ucred *cred = (struct ucred *)data;
1820 struct target_ucred *target_cred =
1821 (struct target_ucred *)target_data;
1822
1823 __get_user(cred->pid, &target_cred->pid);
1824 __get_user(cred->uid, &target_cred->uid);
1825 __get_user(cred->gid, &target_cred->gid);
1826 } else {
1827 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
1828 cmsg->cmsg_level, cmsg->cmsg_type);
1829 memcpy(data, target_data, len);
1830 }
1831
1832 cmsg = CMSG_NXTHDR(msgh, cmsg);
1833 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1834 target_cmsg_start);
1835 }
1836 unlock_user(target_cmsg, target_cmsg_addr, 0);
1837 the_end:
1838 msgh->msg_controllen = space;
1839 return 0;
1840 }
1841
1842 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1843 struct msghdr *msgh)
1844 {
1845 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1846 abi_long msg_controllen;
1847 abi_ulong target_cmsg_addr;
1848 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1849 socklen_t space = 0;
1850
1851 msg_controllen = tswapal(target_msgh->msg_controllen);
1852 if (msg_controllen < sizeof (struct target_cmsghdr))
1853 goto the_end;
1854 target_cmsg_addr = tswapal(target_msgh->msg_control);
1855 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1856 target_cmsg_start = target_cmsg;
1857 if (!target_cmsg)
1858 return -TARGET_EFAULT;
1859
1860 while (cmsg && target_cmsg) {
1861 void *data = CMSG_DATA(cmsg);
1862 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1863
1864 int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
1865 int tgt_len, tgt_space;
1866
1867 /* We never copy a half-header but may copy half-data;
1868 * this is Linux's behaviour in put_cmsg(). Note that
1869 * truncation here is a guest problem (which we report
1870 * to the guest via the CTRUNC bit), unlike truncation
1871 * in target_to_host_cmsg, which is a QEMU bug.
1872 */
1873 if (msg_controllen < sizeof(struct target_cmsghdr)) {
1874 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1875 break;
1876 }
1877
1878 if (cmsg->cmsg_level == SOL_SOCKET) {
1879 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1880 } else {
1881 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1882 }
1883 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1884
1885 /* Payload types which need a different size of payload on
1886 * the target must adjust tgt_len here.
1887 */
1888 tgt_len = len;
1889 switch (cmsg->cmsg_level) {
1890 case SOL_SOCKET:
1891 switch (cmsg->cmsg_type) {
1892 case SO_TIMESTAMP:
1893 tgt_len = sizeof(struct target_timeval);
1894 break;
1895 default:
1896 break;
1897 }
1898 break;
1899 default:
1900 break;
1901 }
1902
1903 if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
1904 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1905 tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
1906 }
1907
1908 /* We must now copy-and-convert len bytes of payload
1909 * into tgt_len bytes of destination space. Bear in mind
1910 * that in both source and destination we may be dealing
1911 * with a truncated value!
1912 */
1913 switch (cmsg->cmsg_level) {
1914 case SOL_SOCKET:
1915 switch (cmsg->cmsg_type) {
1916 case SCM_RIGHTS:
1917 {
1918 int *fd = (int *)data;
1919 int *target_fd = (int *)target_data;
1920 int i, numfds = tgt_len / sizeof(int);
1921
1922 for (i = 0; i < numfds; i++) {
1923 __put_user(fd[i], target_fd + i);
1924 }
1925 break;
1926 }
1927 case SO_TIMESTAMP:
1928 {
1929 struct timeval *tv = (struct timeval *)data;
1930 struct target_timeval *target_tv =
1931 (struct target_timeval *)target_data;
1932
1933 if (len != sizeof(struct timeval) ||
1934 tgt_len != sizeof(struct target_timeval)) {
1935 goto unimplemented;
1936 }
1937
1938 /* copy struct timeval to target */
1939 __put_user(tv->tv_sec, &target_tv->tv_sec);
1940 __put_user(tv->tv_usec, &target_tv->tv_usec);
1941 break;
1942 }
1943 case SCM_CREDENTIALS:
1944 {
1945 struct ucred *cred = (struct ucred *)data;
1946 struct target_ucred *target_cred =
1947 (struct target_ucred *)target_data;
1948
1949 __put_user(cred->pid, &target_cred->pid);
1950 __put_user(cred->uid, &target_cred->uid);
1951 __put_user(cred->gid, &target_cred->gid);
1952 break;
1953 }
1954 default:
1955 goto unimplemented;
1956 }
1957 break;
1958
1959 case SOL_IP:
1960 switch (cmsg->cmsg_type) {
1961 case IP_TTL:
1962 {
1963 uint32_t *v = (uint32_t *)data;
1964 uint32_t *t_int = (uint32_t *)target_data;
1965
1966 if (len != sizeof(uint32_t) ||
1967 tgt_len != sizeof(uint32_t)) {
1968 goto unimplemented;
1969 }
1970 __put_user(*v, t_int);
1971 break;
1972 }
1973 case IP_RECVERR:
1974 {
1975 struct errhdr_t {
1976 struct sock_extended_err ee;
1977 struct sockaddr_in offender;
1978 };
1979 struct errhdr_t *errh = (struct errhdr_t *)data;
1980 struct errhdr_t *target_errh =
1981 (struct errhdr_t *)target_data;
1982
1983 if (len != sizeof(struct errhdr_t) ||
1984 tgt_len != sizeof(struct errhdr_t)) {
1985 goto unimplemented;
1986 }
1987 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
1988 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
1989 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
1990 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
1991 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
1992 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
1993 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
1994 host_to_target_sockaddr((unsigned long) &target_errh->offender,
1995 (void *) &errh->offender, sizeof(errh->offender));
1996 break;
1997 }
1998 default:
1999 goto unimplemented;
2000 }
2001 break;
2002
2003 case SOL_IPV6:
2004 switch (cmsg->cmsg_type) {
2005 case IPV6_HOPLIMIT:
2006 {
2007 uint32_t *v = (uint32_t *)data;
2008 uint32_t *t_int = (uint32_t *)target_data;
2009
2010 if (len != sizeof(uint32_t) ||
2011 tgt_len != sizeof(uint32_t)) {
2012 goto unimplemented;
2013 }
2014 __put_user(*v, t_int);
2015 break;
2016 }
2017 case IPV6_RECVERR:
2018 {
2019 struct errhdr6_t {
2020 struct sock_extended_err ee;
2021 struct sockaddr_in6 offender;
2022 };
2023 struct errhdr6_t *errh = (struct errhdr6_t *)data;
2024 struct errhdr6_t *target_errh =
2025 (struct errhdr6_t *)target_data;
2026
2027 if (len != sizeof(struct errhdr6_t) ||
2028 tgt_len != sizeof(struct errhdr6_t)) {
2029 goto unimplemented;
2030 }
2031 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
2032 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
2033 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
2034 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
2035 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
2036 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
2037 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
2038 host_to_target_sockaddr((unsigned long) &target_errh->offender,
2039 (void *) &errh->offender, sizeof(errh->offender));
2040 break;
2041 }
2042 default:
2043 goto unimplemented;
2044 }
2045 break;
2046
2047 default:
2048 unimplemented:
2049 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
2050 cmsg->cmsg_level, cmsg->cmsg_type);
2051 memcpy(target_data, data, MIN(len, tgt_len));
2052 if (tgt_len > len) {
2053 memset(target_data + len, 0, tgt_len - len);
2054 }
2055 }
2056
2057 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
2058 tgt_space = TARGET_CMSG_SPACE(tgt_len);
2059 if (msg_controllen < tgt_space) {
2060 tgt_space = msg_controllen;
2061 }
2062 msg_controllen -= tgt_space;
2063 space += tgt_space;
2064 cmsg = CMSG_NXTHDR(msgh, cmsg);
2065 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
2066 target_cmsg_start);
2067 }
2068 unlock_user(target_cmsg, target_cmsg_addr, space);
2069 the_end:
2070 target_msgh->msg_controllen = tswapal(space);
2071 return 0;
2072 }
2073
2074 /* do_setsockopt() Must return target values and target errnos. */
2075 static abi_long do_setsockopt(int sockfd, int level, int optname,
2076 abi_ulong optval_addr, socklen_t optlen)
2077 {
2078 abi_long ret;
2079 int val;
2080 struct ip_mreqn *ip_mreq;
2081 struct ip_mreq_source *ip_mreq_source;
2082
2083 switch(level) {
2084 case SOL_TCP:
2085 case SOL_UDP:
2086 /* TCP and UDP options all take an 'int' value. */
2087 if (optlen < sizeof(uint32_t))
2088 return -TARGET_EINVAL;
2089
2090 if (get_user_u32(val, optval_addr))
2091 return -TARGET_EFAULT;
2092 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2093 break;
2094 case SOL_IP:
2095 switch(optname) {
2096 case IP_TOS:
2097 case IP_TTL:
2098 case IP_HDRINCL:
2099 case IP_ROUTER_ALERT:
2100 case IP_RECVOPTS:
2101 case IP_RETOPTS:
2102 case IP_PKTINFO:
2103 case IP_MTU_DISCOVER:
2104 case IP_RECVERR:
2105 case IP_RECVTTL:
2106 case IP_RECVTOS:
2107 #ifdef IP_FREEBIND
2108 case IP_FREEBIND:
2109 #endif
2110 case IP_MULTICAST_TTL:
2111 case IP_MULTICAST_LOOP:
2112 val = 0;
2113 if (optlen >= sizeof(uint32_t)) {
2114 if (get_user_u32(val, optval_addr))
2115 return -TARGET_EFAULT;
2116 } else if (optlen >= 1) {
2117 if (get_user_u8(val, optval_addr))
2118 return -TARGET_EFAULT;
2119 }
2120 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2121 break;
2122 case IP_ADD_MEMBERSHIP:
2123 case IP_DROP_MEMBERSHIP:
2124 if (optlen < sizeof (struct target_ip_mreq) ||
2125 optlen > sizeof (struct target_ip_mreqn))
2126 return -TARGET_EINVAL;
2127
2128 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2129 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2130 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2131 break;
2132
2133 case IP_BLOCK_SOURCE:
2134 case IP_UNBLOCK_SOURCE:
2135 case IP_ADD_SOURCE_MEMBERSHIP:
2136 case IP_DROP_SOURCE_MEMBERSHIP:
2137 if (optlen != sizeof (struct target_ip_mreq_source))
2138 return -TARGET_EINVAL;
2139
2140 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2141 if (!ip_mreq_source) {
2142 return -TARGET_EFAULT;
2143 }
2144 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2145 unlock_user (ip_mreq_source, optval_addr, 0);
2146 break;
2147
2148 default:
2149 goto unimplemented;
2150 }
2151 break;
2152 case SOL_IPV6:
2153 switch (optname) {
2154 case IPV6_MTU_DISCOVER:
2155 case IPV6_MTU:
2156 case IPV6_V6ONLY:
2157 case IPV6_RECVPKTINFO:
2158 case IPV6_UNICAST_HOPS:
2159 case IPV6_MULTICAST_HOPS:
2160 case IPV6_MULTICAST_LOOP:
2161 case IPV6_RECVERR:
2162 case IPV6_RECVHOPLIMIT:
2163 case IPV6_2292HOPLIMIT:
2164 case IPV6_CHECKSUM:
2165 case IPV6_ADDRFORM:
2166 case IPV6_2292PKTINFO:
2167 case IPV6_RECVTCLASS:
2168 case IPV6_RECVRTHDR:
2169 case IPV6_2292RTHDR:
2170 case IPV6_RECVHOPOPTS:
2171 case IPV6_2292HOPOPTS:
2172 case IPV6_RECVDSTOPTS:
2173 case IPV6_2292DSTOPTS:
2174 case IPV6_TCLASS:
2175 case IPV6_ADDR_PREFERENCES:
2176 #ifdef IPV6_RECVPATHMTU
2177 case IPV6_RECVPATHMTU:
2178 #endif
2179 #ifdef IPV6_TRANSPARENT
2180 case IPV6_TRANSPARENT:
2181 #endif
2182 #ifdef IPV6_FREEBIND
2183 case IPV6_FREEBIND:
2184 #endif
2185 #ifdef IPV6_RECVORIGDSTADDR
2186 case IPV6_RECVORIGDSTADDR:
2187 #endif
2188 val = 0;
2189 if (optlen < sizeof(uint32_t)) {
2190 return -TARGET_EINVAL;
2191 }
2192 if (get_user_u32(val, optval_addr)) {
2193 return -TARGET_EFAULT;
2194 }
2195 ret = get_errno(setsockopt(sockfd, level, optname,
2196 &val, sizeof(val)));
2197 break;
2198 case IPV6_PKTINFO:
2199 {
2200 struct in6_pktinfo pki;
2201
2202 if (optlen < sizeof(pki)) {
2203 return -TARGET_EINVAL;
2204 }
2205
2206 if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
2207 return -TARGET_EFAULT;
2208 }
2209
2210 pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
2211
2212 ret = get_errno(setsockopt(sockfd, level, optname,
2213 &pki, sizeof(pki)));
2214 break;
2215 }
2216 case IPV6_ADD_MEMBERSHIP:
2217 case IPV6_DROP_MEMBERSHIP:
2218 {
2219 struct ipv6_mreq ipv6mreq;
2220
2221 if (optlen < sizeof(ipv6mreq)) {
2222 return -TARGET_EINVAL;
2223 }
2224
2225 if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
2226 return -TARGET_EFAULT;
2227 }
2228
2229 ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
2230
2231 ret = get_errno(setsockopt(sockfd, level, optname,
2232 &ipv6mreq, sizeof(ipv6mreq)));
2233 break;
2234 }
2235 default:
2236 goto unimplemented;
2237 }
2238 break;
2239 case SOL_ICMPV6:
2240 switch (optname) {
2241 case ICMPV6_FILTER:
2242 {
2243 struct icmp6_filter icmp6f;
2244
2245 if (optlen > sizeof(icmp6f)) {
2246 optlen = sizeof(icmp6f);
2247 }
2248
2249 if (copy_from_user(&icmp6f, optval_addr, optlen)) {
2250 return -TARGET_EFAULT;
2251 }
2252
2253 for (val = 0; val < 8; val++) {
2254 icmp6f.data[val] = tswap32(icmp6f.data[val]);
2255 }
2256
2257 ret = get_errno(setsockopt(sockfd, level, optname,
2258 &icmp6f, optlen));
2259 break;
2260 }
2261 default:
2262 goto unimplemented;
2263 }
2264 break;
2265 case SOL_RAW:
2266 switch (optname) {
2267 case ICMP_FILTER:
2268 case IPV6_CHECKSUM:
2269 /* those take an u32 value */
2270 if (optlen < sizeof(uint32_t)) {
2271 return -TARGET_EINVAL;
2272 }
2273
2274 if (get_user_u32(val, optval_addr)) {
2275 return -TARGET_EFAULT;
2276 }
2277 ret = get_errno(setsockopt(sockfd, level, optname,
2278 &val, sizeof(val)));
2279 break;
2280
2281 default:
2282 goto unimplemented;
2283 }
2284 break;
2285 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2286 case SOL_ALG:
2287 switch (optname) {
2288 case ALG_SET_KEY:
2289 {
2290 char *alg_key = g_malloc(optlen);
2291
2292 if (!alg_key) {
2293 return -TARGET_ENOMEM;
2294 }
2295 if (copy_from_user(alg_key, optval_addr, optlen)) {
2296 g_free(alg_key);
2297 return -TARGET_EFAULT;
2298 }
2299 ret = get_errno(setsockopt(sockfd, level, optname,
2300 alg_key, optlen));
2301 g_free(alg_key);
2302 break;
2303 }
2304 case ALG_SET_AEAD_AUTHSIZE:
2305 {
2306 ret = get_errno(setsockopt(sockfd, level, optname,
2307 NULL, optlen));
2308 break;
2309 }
2310 default:
2311 goto unimplemented;
2312 }
2313 break;
2314 #endif
2315 case TARGET_SOL_SOCKET:
2316 switch (optname) {
2317 case TARGET_SO_RCVTIMEO:
2318 {
2319 struct timeval tv;
2320
2321 optname = SO_RCVTIMEO;
2322
2323 set_timeout:
2324 if (optlen != sizeof(struct target_timeval)) {
2325 return -TARGET_EINVAL;
2326 }
2327
2328 if (copy_from_user_timeval(&tv, optval_addr)) {
2329 return -TARGET_EFAULT;
2330 }
2331
2332 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2333 &tv, sizeof(tv)));
2334 return ret;
2335 }
2336 case TARGET_SO_SNDTIMEO:
2337 optname = SO_SNDTIMEO;
2338 goto set_timeout;
2339 case TARGET_SO_ATTACH_FILTER:
2340 {
2341 struct target_sock_fprog *tfprog;
2342 struct target_sock_filter *tfilter;
2343 struct sock_fprog fprog;
2344 struct sock_filter *filter;
2345 int i;
2346
2347 if (optlen != sizeof(*tfprog)) {
2348 return -TARGET_EINVAL;
2349 }
2350 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2351 return -TARGET_EFAULT;
2352 }
2353 if (!lock_user_struct(VERIFY_READ, tfilter,
2354 tswapal(tfprog->filter), 0)) {
2355 unlock_user_struct(tfprog, optval_addr, 1);
2356 return -TARGET_EFAULT;
2357 }
2358
2359 fprog.len = tswap16(tfprog->len);
2360 filter = g_try_new(struct sock_filter, fprog.len);
2361 if (filter == NULL) {
2362 unlock_user_struct(tfilter, tfprog->filter, 1);
2363 unlock_user_struct(tfprog, optval_addr, 1);
2364 return -TARGET_ENOMEM;
2365 }
2366 for (i = 0; i < fprog.len; i++) {
2367 filter[i].code = tswap16(tfilter[i].code);
2368 filter[i].jt = tfilter[i].jt;
2369 filter[i].jf = tfilter[i].jf;
2370 filter[i].k = tswap32(tfilter[i].k);
2371 }
2372 fprog.filter = filter;
2373
2374 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2375 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
2376 g_free(filter);
2377
2378 unlock_user_struct(tfilter, tfprog->filter, 1);
2379 unlock_user_struct(tfprog, optval_addr, 1);
2380 return ret;
2381 }
2382 case TARGET_SO_BINDTODEVICE:
2383 {
2384 char *dev_ifname, *addr_ifname;
2385
2386 if (optlen > IFNAMSIZ - 1) {
2387 optlen = IFNAMSIZ - 1;
2388 }
2389 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2390 if (!dev_ifname) {
2391 return -TARGET_EFAULT;
2392 }
2393 optname = SO_BINDTODEVICE;
2394 addr_ifname = alloca(IFNAMSIZ);
2395 memcpy(addr_ifname, dev_ifname, optlen);
2396 addr_ifname[optlen] = 0;
2397 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2398 addr_ifname, optlen));
2399 unlock_user (dev_ifname, optval_addr, 0);
2400 return ret;
2401 }
2402 case TARGET_SO_LINGER:
2403 {
2404 struct linger lg;
2405 struct target_linger *tlg;
2406
2407 if (optlen != sizeof(struct target_linger)) {
2408 return -TARGET_EINVAL;
2409 }
2410 if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
2411 return -TARGET_EFAULT;
2412 }
2413 __get_user(lg.l_onoff, &tlg->l_onoff);
2414 __get_user(lg.l_linger, &tlg->l_linger);
2415 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
2416 &lg, sizeof(lg)));
2417 unlock_user_struct(tlg, optval_addr, 0);
2418 return ret;
2419 }
2420 /* Options with 'int' argument. */
2421 case TARGET_SO_DEBUG:
2422 optname = SO_DEBUG;
2423 break;
2424 case TARGET_SO_REUSEADDR:
2425 optname = SO_REUSEADDR;
2426 break;
2427 #ifdef SO_REUSEPORT
2428 case TARGET_SO_REUSEPORT:
2429 optname = SO_REUSEPORT;
2430 break;
2431 #endif
2432 case TARGET_SO_TYPE:
2433 optname = SO_TYPE;
2434 break;
2435 case TARGET_SO_ERROR:
2436 optname = SO_ERROR;
2437 break;
2438 case TARGET_SO_DONTROUTE:
2439 optname = SO_DONTROUTE;
2440 break;
2441 case TARGET_SO_BROADCAST:
2442 optname = SO_BROADCAST;
2443 break;
2444 case TARGET_SO_SNDBUF:
2445 optname = SO_SNDBUF;
2446 break;
2447 case TARGET_SO_SNDBUFFORCE:
2448 optname = SO_SNDBUFFORCE;
2449 break;
2450 case TARGET_SO_RCVBUF:
2451 optname = SO_RCVBUF;
2452 break;
2453 case TARGET_SO_RCVBUFFORCE:
2454 optname = SO_RCVBUFFORCE;
2455 break;
2456 case TARGET_SO_KEEPALIVE:
2457 optname = SO_KEEPALIVE;
2458 break;
2459 case TARGET_SO_OOBINLINE:
2460 optname = SO_OOBINLINE;
2461 break;
2462 case TARGET_SO_NO_CHECK:
2463 optname = SO_NO_CHECK;
2464 break;
2465 case TARGET_SO_PRIORITY:
2466 optname = SO_PRIORITY;
2467 break;
2468 #ifdef SO_BSDCOMPAT
2469 case TARGET_SO_BSDCOMPAT:
2470 optname = SO_BSDCOMPAT;
2471 break;
2472 #endif
2473 case TARGET_SO_PASSCRED:
2474 optname = SO_PASSCRED;
2475 break;
2476 case TARGET_SO_PASSSEC:
2477 optname = SO_PASSSEC;
2478 break;
2479 case TARGET_SO_TIMESTAMP:
2480 optname = SO_TIMESTAMP;
2481 break;
2482 case TARGET_SO_RCVLOWAT:
2483 optname = SO_RCVLOWAT;
2484 break;
2485 default:
2486 goto unimplemented;
2487 }
2488 if (optlen < sizeof(uint32_t))
2489 return -TARGET_EINVAL;
2490
2491 if (get_user_u32(val, optval_addr))
2492 return -TARGET_EFAULT;
2493 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
2494 break;
2495 #ifdef SOL_NETLINK
2496 case SOL_NETLINK:
2497 switch (optname) {
2498 case NETLINK_PKTINFO:
2499 case NETLINK_ADD_MEMBERSHIP:
2500 case NETLINK_DROP_MEMBERSHIP:
2501 case NETLINK_BROADCAST_ERROR:
2502 case NETLINK_NO_ENOBUFS:
2503 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2504 case NETLINK_LISTEN_ALL_NSID:
2505 case NETLINK_CAP_ACK:
2506 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2507 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2508 case NETLINK_EXT_ACK:
2509 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2510 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2511 case NETLINK_GET_STRICT_CHK:
2512 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2513 break;
2514 default:
2515 goto unimplemented;
2516 }
2517 val = 0;
2518 if (optlen < sizeof(uint32_t)) {
2519 return -TARGET_EINVAL;
2520 }
2521 if (get_user_u32(val, optval_addr)) {
2522 return -TARGET_EFAULT;
2523 }
2524 ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
2525 sizeof(val)));
2526 break;
2527 #endif /* SOL_NETLINK */
2528 default:
2529 unimplemented:
2530 qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
2531 level, optname);
2532 ret = -TARGET_ENOPROTOOPT;
2533 }
2534 return ret;
2535 }
2536
2537 /* do_getsockopt() Must return target values and target errnos. */
2538 static abi_long do_getsockopt(int sockfd, int level, int optname,
2539 abi_ulong optval_addr, abi_ulong optlen)
2540 {
2541 abi_long ret;
2542 int len, val;
2543 socklen_t lv;
2544
2545 switch(level) {
2546 case TARGET_SOL_SOCKET:
2547 level = SOL_SOCKET;
2548 switch (optname) {
2549 /* These don't just return a single integer */
2550 case TARGET_SO_PEERNAME:
2551 goto unimplemented;
2552 case TARGET_SO_RCVTIMEO: {
2553 struct timeval tv;
2554 socklen_t tvlen;
2555
2556 optname = SO_RCVTIMEO;
2557
2558 get_timeout:
2559 if (get_user_u32(len, optlen)) {
2560 return -TARGET_EFAULT;
2561 }
2562 if (len < 0) {
2563 return -TARGET_EINVAL;
2564 }
2565
2566 tvlen = sizeof(tv);
2567 ret = get_errno(getsockopt(sockfd, level, optname,
2568 &tv, &tvlen));
2569 if (ret < 0) {
2570 return ret;
2571 }
2572 if (len > sizeof(struct target_timeval)) {
2573 len = sizeof(struct target_timeval);
2574 }
2575 if (copy_to_user_timeval(optval_addr, &tv)) {
2576 return -TARGET_EFAULT;
2577 }
2578 if (put_user_u32(len, optlen)) {
2579 return -TARGET_EFAULT;
2580 }
2581 break;
2582 }
2583 case TARGET_SO_SNDTIMEO:
2584 optname = SO_SNDTIMEO;
2585 goto get_timeout;
2586 case TARGET_SO_PEERCRED: {
2587 struct ucred cr;
2588 socklen_t crlen;
2589 struct target_ucred *tcr;
2590
2591 if (get_user_u32(len, optlen)) {
2592 return -TARGET_EFAULT;
2593 }
2594 if (len < 0) {
2595 return -TARGET_EINVAL;
2596 }
2597
2598 crlen = sizeof(cr);
2599 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2600 &cr, &crlen));
2601 if (ret < 0) {
2602 return ret;
2603 }
2604 if (len > crlen) {
2605 len = crlen;
2606 }
2607 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2608 return -TARGET_EFAULT;
2609 }
2610 __put_user(cr.pid, &tcr->pid);
2611 __put_user(cr.uid, &tcr->uid);
2612 __put_user(cr.gid, &tcr->gid);
2613 unlock_user_struct(tcr, optval_addr, 1);
2614 if (put_user_u32(len, optlen)) {
2615 return -TARGET_EFAULT;
2616 }
2617 break;
2618 }
2619 case TARGET_SO_PEERSEC: {
2620 char *name;
2621
2622 if (get_user_u32(len, optlen)) {
2623 return -TARGET_EFAULT;
2624 }
2625 if (len < 0) {
2626 return -TARGET_EINVAL;
2627 }
2628 name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
2629 if (!name) {
2630 return -TARGET_EFAULT;
2631 }
2632 lv = len;
2633 ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
2634 name, &lv));
2635 if (put_user_u32(lv, optlen)) {
2636 ret = -TARGET_EFAULT;
2637 }
2638 unlock_user(name, optval_addr, lv);
2639 break;
2640 }
2641 case TARGET_SO_LINGER:
2642 {
2643 struct linger lg;
2644 socklen_t lglen;
2645 struct target_linger *tlg;
2646
2647 if (get_user_u32(len, optlen)) {
2648 return -TARGET_EFAULT;
2649 }
2650 if (len < 0) {
2651 return -TARGET_EINVAL;
2652 }
2653
2654 lglen = sizeof(lg);
2655 ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
2656 &lg, &lglen));
2657 if (ret < 0) {
2658 return ret;
2659 }
2660 if (len > lglen) {
2661 len = lglen;
2662 }
2663 if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
2664 return -TARGET_EFAULT;
2665 }
2666 __put_user(lg.l_onoff, &tlg->l_onoff);
2667 __put_user(lg.l_linger, &tlg->l_linger);
2668 unlock_user_struct(tlg, optval_addr, 1);
2669 if (put_user_u32(len, optlen)) {
2670 return -TARGET_EFAULT;
2671 }
2672 break;
2673 }
2674 /* Options with 'int' argument. */
2675 case TARGET_SO_DEBUG:
2676 optname = SO_DEBUG;
2677 goto int_case;
2678 case TARGET_SO_REUSEADDR:
2679 optname = SO_REUSEADDR;
2680 goto int_case;
2681 #ifdef SO_REUSEPORT
2682 case TARGET_SO_REUSEPORT:
2683 optname = SO_REUSEPORT;
2684 goto int_case;
2685 #endif
2686 case TARGET_SO_TYPE:
2687 optname = SO_TYPE;
2688 goto int_case;
2689 case TARGET_SO_ERROR:
2690 optname = SO_ERROR;
2691 goto int_case;
2692 case TARGET_SO_DONTROUTE:
2693 optname = SO_DONTROUTE;
2694 goto int_case;
2695 case TARGET_SO_BROADCAST:
2696 optname = SO_BROADCAST;
2697 goto int_case;
2698 case TARGET_SO_SNDBUF:
2699 optname = SO_SNDBUF;
2700 goto int_case;
2701 case TARGET_SO_RCVBUF:
2702 optname = SO_RCVBUF;
2703 goto int_case;
2704 case TARGET_SO_KEEPALIVE:
2705 optname = SO_KEEPALIVE;
2706 goto int_case;
2707 case TARGET_SO_OOBINLINE:
2708 optname = SO_OOBINLINE;
2709 goto int_case;
2710 case TARGET_SO_NO_CHECK:
2711 optname = SO_NO_CHECK;
2712 goto int_case;
2713 case TARGET_SO_PRIORITY:
2714 optname = SO_PRIORITY;
2715 goto int_case;
2716 #ifdef SO_BSDCOMPAT
2717 case TARGET_SO_BSDCOMPAT:
2718 optname = SO_BSDCOMPAT;
2719 goto int_case;
2720 #endif
2721 case TARGET_SO_PASSCRED:
2722 optname = SO_PASSCRED;
2723 goto int_case;
2724 case TARGET_SO_TIMESTAMP:
2725 optname = SO_TIMESTAMP;
2726 goto int_case;
2727 case TARGET_SO_RCVLOWAT:
2728 optname = SO_RCVLOWAT;
2729 goto int_case;
2730 case TARGET_SO_ACCEPTCONN:
2731 optname = SO_ACCEPTCONN;
2732 goto int_case;
2733 case TARGET_SO_PROTOCOL:
2734 optname = SO_PROTOCOL;
2735 goto int_case;
2736 case TARGET_SO_DOMAIN:
2737 optname = SO_DOMAIN;
2738 goto int_case;
2739 default:
2740 goto int_case;
2741 }
2742 break;
2743 case SOL_TCP:
2744 case SOL_UDP:
2745 /* TCP and UDP options all take an 'int' value. */
2746 int_case:
2747 if (get_user_u32(len, optlen))
2748 return -TARGET_EFAULT;
2749 if (len < 0)
2750 return -TARGET_EINVAL;
2751 lv = sizeof(lv);
2752 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2753 if (ret < 0)
2754 return ret;
2755 if (optname == SO_TYPE) {
2756 val = host_to_target_sock_type(val);
2757 }
2758 if (len > lv)
2759 len = lv;
2760 if (len == 4) {
2761 if (put_user_u32(val, optval_addr))
2762 return -TARGET_EFAULT;
2763 } else {
2764 if (put_user_u8(val, optval_addr))
2765 return -TARGET_EFAULT;
2766 }
2767 if (put_user_u32(len, optlen))
2768 return -TARGET_EFAULT;
2769 break;
2770 case SOL_IP:
2771 switch(optname) {
2772 case IP_TOS:
2773 case IP_TTL:
2774 case IP_HDRINCL:
2775 case IP_ROUTER_ALERT:
2776 case IP_RECVOPTS:
2777 case IP_RETOPTS:
2778 case IP_PKTINFO:
2779 case IP_MTU_DISCOVER:
2780 case IP_RECVERR:
2781 case IP_RECVTOS:
2782 #ifdef IP_FREEBIND
2783 case IP_FREEBIND:
2784 #endif
2785 case IP_MULTICAST_TTL:
2786 case IP_MULTICAST_LOOP:
2787 if (get_user_u32(len, optlen))
2788 return -TARGET_EFAULT;
2789 if (len < 0)
2790 return -TARGET_EINVAL;
2791 lv = sizeof(lv);
2792 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2793 if (ret < 0)
2794 return ret;
2795 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2796 len = 1;
2797 if (put_user_u32(len, optlen)
2798 || put_user_u8(val, optval_addr))
2799 return -TARGET_EFAULT;
2800 } else {
2801 if (len > sizeof(int))
2802 len = sizeof(int);
2803 if (put_user_u32(len, optlen)
2804 || put_user_u32(val, optval_addr))
2805 return -TARGET_EFAULT;
2806 }
2807 break;
2808 default:
2809 ret = -TARGET_ENOPROTOOPT;
2810 break;
2811 }
2812 break;
2813 case SOL_IPV6:
2814 switch (optname) {
2815 case IPV6_MTU_DISCOVER:
2816 case IPV6_MTU:
2817 case IPV6_V6ONLY:
2818 case IPV6_RECVPKTINFO:
2819 case IPV6_UNICAST_HOPS:
2820 case IPV6_MULTICAST_HOPS:
2821 case IPV6_MULTICAST_LOOP:
2822 case IPV6_RECVERR:
2823 case IPV6_RECVHOPLIMIT:
2824 case IPV6_2292HOPLIMIT:
2825 case IPV6_CHECKSUM:
2826 case IPV6_ADDRFORM:
2827 case IPV6_2292PKTINFO:
2828 case IPV6_RECVTCLASS:
2829 case IPV6_RECVRTHDR:
2830 case IPV6_2292RTHDR:
2831 case IPV6_RECVHOPOPTS:
2832 case IPV6_2292HOPOPTS:
2833 case IPV6_RECVDSTOPTS:
2834 case IPV6_2292DSTOPTS:
2835 case IPV6_TCLASS:
2836 case IPV6_ADDR_PREFERENCES:
2837 #ifdef IPV6_RECVPATHMTU
2838 case IPV6_RECVPATHMTU:
2839 #endif
2840 #ifdef IPV6_TRANSPARENT
2841 case IPV6_TRANSPARENT:
2842 #endif
2843 #ifdef IPV6_FREEBIND
2844 case IPV6_FREEBIND:
2845 #endif
2846 #ifdef IPV6_RECVORIGDSTADDR
2847 case IPV6_RECVORIGDSTADDR:
2848 #endif
2849 if (get_user_u32(len, optlen))
2850 return -TARGET_EFAULT;
2851 if (len < 0)
2852 return -TARGET_EINVAL;
2853 lv = sizeof(lv);
2854 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2855 if (ret < 0)
2856 return ret;
2857 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2858 len = 1;
2859 if (put_user_u32(len, optlen)
2860 || put_user_u8(val, optval_addr))
2861 return -TARGET_EFAULT;
2862 } else {
2863 if (len > sizeof(int))
2864 len = sizeof(int);
2865 if (put_user_u32(len, optlen)
2866 || put_user_u32(val, optval_addr))
2867 return -TARGET_EFAULT;
2868 }
2869 break;
2870 default:
2871 ret = -TARGET_ENOPROTOOPT;
2872 break;
2873 }
2874 break;
2875 #ifdef SOL_NETLINK
2876 case SOL_NETLINK:
2877 switch (optname) {
2878 case NETLINK_PKTINFO:
2879 case NETLINK_BROADCAST_ERROR:
2880 case NETLINK_NO_ENOBUFS:
2881 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2882 case NETLINK_LISTEN_ALL_NSID:
2883 case NETLINK_CAP_ACK:
2884 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2885 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2886 case NETLINK_EXT_ACK:
2887 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2888 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2889 case NETLINK_GET_STRICT_CHK:
2890 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2891 if (get_user_u32(len, optlen)) {
2892 return -TARGET_EFAULT;
2893 }
2894 if (len != sizeof(val)) {
2895 return -TARGET_EINVAL;
2896 }
2897 lv = len;
2898 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2899 if (ret < 0) {
2900 return ret;
2901 }
2902 if (put_user_u32(lv, optlen)
2903 || put_user_u32(val, optval_addr)) {
2904 return -TARGET_EFAULT;
2905 }
2906 break;
2907 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2908 case NETLINK_LIST_MEMBERSHIPS:
2909 {
2910 uint32_t *results;
2911 int i;
2912 if (get_user_u32(len, optlen)) {
2913 return -TARGET_EFAULT;
2914 }
2915 if (len < 0) {
2916 return -TARGET_EINVAL;
2917 }
2918 results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
2919 if (!results && len > 0) {
2920 return -TARGET_EFAULT;
2921 }
2922 lv = len;
2923 ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
2924 if (ret < 0) {
2925 unlock_user(results, optval_addr, 0);
2926 return ret;
2927 }
2928 /* swap host endianess to target endianess. */
2929 for (i = 0; i < (len / sizeof(uint32_t)); i++) {
2930 results[i] = tswap32(results[i]);
2931 }
2932 if (put_user_u32(lv, optlen)) {
2933 return -TARGET_EFAULT;
2934 }
2935 unlock_user(results, optval_addr, 0);
2936 break;
2937 }
2938 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2939 default:
2940 goto unimplemented;
2941 }
2942 break;
2943 #endif /* SOL_NETLINK */
2944 default:
2945 unimplemented:
2946 qemu_log_mask(LOG_UNIMP,
2947 "getsockopt level=%d optname=%d not yet supported\n",
2948 level, optname);
2949 ret = -TARGET_EOPNOTSUPP;
2950 break;
2951 }
2952 return ret;
2953 }
2954
2955 /* Convert target low/high pair representing file offset into the host
2956 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2957 * as the kernel doesn't handle them either.
2958 */
2959 static void target_to_host_low_high(abi_ulong tlow,
2960 abi_ulong thigh,
2961 unsigned long *hlow,
2962 unsigned long *hhigh)
2963 {
2964 uint64_t off = tlow |
2965 ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
2966 TARGET_LONG_BITS / 2;
2967
2968 *hlow = off;
2969 *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
2970 }
2971
2972 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
2973 abi_ulong count, int copy)
2974 {
2975 struct target_iovec *target_vec;
2976 struct iovec *vec;
2977 abi_ulong total_len, max_len;
2978 int i;
2979 int err = 0;
2980 bool bad_address = false;
2981
2982 if (count == 0) {
2983 errno = 0;
2984 return NULL;
2985 }
2986 if (count > IOV_MAX) {
2987 errno = EINVAL;
2988 return NULL;
2989 }
2990
2991 vec = g_try_new0(struct iovec, count);
2992 if (vec == NULL) {
2993 errno = ENOMEM;
2994 return NULL;
2995 }
2996
2997 target_vec = lock_user(VERIFY_READ, target_addr,
2998 count * sizeof(struct target_iovec), 1);
2999 if (target_vec == NULL) {
3000 err = EFAULT;
3001 goto fail2;
3002 }
3003
3004 /* ??? If host page size > target page size, this will result in a
3005 value larger than what we can actually support. */
3006 max_len = 0x7fffffff & TARGET_PAGE_MASK;
3007 total_len = 0;
3008
3009 for (i = 0; i < count; i++) {
3010 abi_ulong base = tswapal(target_vec[i].iov_base);
3011 abi_long len = tswapal(target_vec[i].iov_len);
3012
3013 if (len < 0) {
3014 err = EINVAL;
3015 goto fail;
3016 } else if (len == 0) {
3017 /* Zero length pointer is ignored. */
3018 vec[i].iov_base = 0;
3019 } else {
3020 vec[i].iov_base = lock_user(type, base, len, copy);
3021 /* If the first buffer pointer is bad, this is a fault. But
3022 * subsequent bad buffers will result in a partial write; this
3023 * is realized by filling the vector with null pointers and
3024 * zero lengths. */
3025 if (!vec[i].iov_base) {
3026 if (i == 0) {
3027 err = EFAULT;
3028 goto fail;
3029 } else {
3030 bad_address = true;
3031 }
3032 }
3033 if (bad_address) {
3034 len = 0;
3035 }
3036 if (len > max_len - total_len) {
3037 len = max_len - total_len;
3038 }
3039 }
3040 vec[i].iov_len = len;
3041 total_len += len;
3042 }
3043
3044 unlock_user(target_vec, target_addr, 0);
3045 return vec;
3046
3047 fail:
3048 while (--i >= 0) {
3049 if (tswapal(target_vec[i].iov_len) > 0) {
3050 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
3051 }
3052 }
3053 unlock_user(target_vec, target_addr, 0);
3054 fail2:
3055 g_free(vec);
3056 errno = err;
3057 return NULL;
3058 }
3059
3060 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
3061 abi_ulong count, int copy)
3062 {
3063 struct target_iovec *target_vec;
3064 int i;
3065
3066 target_vec = lock_user(VERIFY_READ, target_addr,
3067 count * sizeof(struct target_iovec), 1);
3068 if (target_vec) {
3069 for (i = 0; i < count; i++) {
3070 abi_ulong base = tswapal(target_vec[i].iov_base);
3071 abi_long len = tswapal(target_vec[i].iov_len);
3072 if (len < 0) {
3073 break;
3074 }
3075 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3076 }
3077 unlock_user(target_vec, target_addr, 0);
3078 }
3079
3080 g_free(vec);
3081 }
3082
3083 static inline int target_to_host_sock_type(int *type)
3084 {
3085 int host_type = 0;
3086 int target_type = *type;
3087
3088 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3089 case TARGET_SOCK_DGRAM:
3090 host_type = SOCK_DGRAM;
3091 break;
3092 case TARGET_SOCK_STREAM:
3093 host_type = SOCK_STREAM;
3094 break;
3095 default:
3096 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3097 break;
3098 }
3099 if (target_type & TARGET_SOCK_CLOEXEC) {
3100 #if defined(SOCK_CLOEXEC)
3101 host_type |= SOCK_CLOEXEC;
3102 #else
3103 return -TARGET_EINVAL;
3104 #endif
3105 }
3106 if (target_type & TARGET_SOCK_NONBLOCK) {
3107 #if defined(SOCK_NONBLOCK)
3108 host_type |= SOCK_NONBLOCK;
3109 #elif !defined(O_NONBLOCK)
3110 return -TARGET_EINVAL;
3111 #endif
3112 }
3113 *type = host_type;
3114 return 0;
3115 }
3116
3117 /* Try to emulate socket type flags after socket creation. */
3118 static int sock_flags_fixup(int fd, int target_type)
3119 {
3120 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3121 if (target_type & TARGET_SOCK_NONBLOCK) {
3122 int flags = fcntl(fd, F_GETFL);
3123 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
3124 close(fd);
3125 return -TARGET_EINVAL;
3126 }
3127 }
3128 #endif
3129 return fd;
3130 }
3131
3132 /* do_socket() Must return target values and target errnos. */
3133 static abi_long do_socket(int domain, int type, int protocol)
3134 {
3135 int target_type = type;
3136 int ret;
3137
3138 ret = target_to_host_sock_type(&type);
3139 if (ret) {
3140 return ret;
3141 }
3142
3143 if (domain == PF_NETLINK && !(
3144 #ifdef CONFIG_RTNETLINK
3145 protocol == NETLINK_ROUTE ||
3146 #endif
3147 protocol == NETLINK_KOBJECT_UEVENT ||
3148 protocol == NETLINK_AUDIT)) {
3149 return -TARGET_EPROTONOSUPPORT;
3150 }
3151
3152 if (domain == AF_PACKET ||
3153 (domain == AF_INET && type == SOCK_PACKET)) {
3154 protocol = tswap16(protocol);
3155 }
3156
3157 ret = get_errno(socket(domain, type, protocol));
3158 if (ret >= 0) {
3159 ret = sock_flags_fixup(ret, target_type);
3160 if (type == SOCK_PACKET) {
3161 /* Manage an obsolete case :
3162 * if socket type is SOCK_PACKET, bind by name
3163 */
3164 fd_trans_register(ret, &target_packet_trans);
3165 } else if (domain == PF_NETLINK) {
3166 switch (protocol) {
3167 #ifdef CONFIG_RTNETLINK
3168 case NETLINK_ROUTE:
3169 fd_trans_register(ret, &target_netlink_route_trans);
3170 break;
3171 #endif
3172 case NETLINK_KOBJECT_UEVENT:
3173 /* nothing to do: messages are strings */
3174 break;
3175 case NETLINK_AUDIT:
3176 fd_trans_register(ret, &target_netlink_audit_trans);
3177 break;
3178 default:
3179 g_assert_not_reached();
3180 }
3181 }
3182 }
3183 return ret;
3184 }
3185
3186 /* do_bind() Must return target values and target errnos. */
3187 static abi_long do_bind(int sockfd, abi_ulong target_addr,
3188 socklen_t addrlen)
3189 {
3190 void *addr;
3191 abi_long ret;
3192
3193 if ((int)addrlen < 0) {
3194 return -TARGET_EINVAL;
3195 }
3196
3197 addr = alloca(addrlen+1);
3198
3199 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
3200 if (ret)
3201 return ret;
3202
3203 return get_errno(bind(sockfd, addr, addrlen));
3204 }
3205
3206 /* do_connect() Must return target values and target errnos. */
3207 static abi_long do_connect(int sockfd, abi_ulong target_addr,
3208 socklen_t addrlen)
3209 {
3210 void *addr;
3211 abi_long ret;
3212
3213 if ((int)addrlen < 0) {
3214 return -TARGET_EINVAL;
3215 }
3216
3217 addr = alloca(addrlen+1);
3218
3219 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
3220 if (ret)
3221 return ret;
3222
3223 return get_errno(safe_connect(sockfd, addr, addrlen));
3224 }
3225
3226 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3227 static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
3228 int flags, int send)
3229 {
3230 abi_long ret, len;
3231 struct msghdr msg;
3232 abi_ulong count;
3233 struct iovec *vec;
3234 abi_ulong target_vec;
3235
3236 if (msgp->msg_name) {
3237 msg.msg_namelen = tswap32(msgp->msg_namelen);
3238 msg.msg_name = alloca(msg.msg_namelen+1);
3239 ret = target_to_host_sockaddr(fd, msg.msg_name,
3240 tswapal(msgp->msg_name),
3241 msg.msg_namelen);
3242 if (ret == -TARGET_EFAULT) {
3243 /* For connected sockets msg_name and msg_namelen must
3244 * be ignored, so returning EFAULT immediately is wrong.
3245 * Instead, pass a bad msg_name to the host kernel, and
3246 * let it decide whether to return EFAULT or not.
3247 */
3248 msg.msg_name = (void *)-1;
3249 } else if (ret) {
3250 goto out2;
3251 }
3252 } else {
3253 msg.msg_name = NULL;
3254 msg.msg_namelen = 0;
3255 }
3256 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
3257 msg.msg_control = alloca(msg.msg_controllen);
3258 memset(msg.msg_control, 0, msg.msg_controllen);
3259
3260 msg.msg_flags = tswap32(msgp->msg_flags);
3261
3262 count = tswapal(msgp->msg_iovlen);
3263 target_vec = tswapal(msgp->msg_iov);
3264
3265 if (count > IOV_MAX) {
3266 /* sendrcvmsg returns a different errno for this condition than
3267 * readv/writev, so we must catch it here before lock_iovec() does.
3268 */
3269 ret = -TARGET_EMSGSIZE;
3270 goto out2;
3271 }
3272
3273 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
3274 target_vec, count, send);
3275 if (vec == NULL) {
3276 ret = -host_to_target_errno(errno);
3277 goto out2;
3278 }
3279 msg.msg_iovlen = count;
3280 msg.msg_iov = vec;
3281
3282 if (send) {
3283 if (fd_trans_target_to_host_data(fd)) {
3284 void *host_msg;
3285
3286 host_msg = g_malloc(msg.msg_iov->iov_len);
3287 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
3288 ret = fd_trans_target_to_host_data(fd)(host_msg,
3289 msg.msg_iov->iov_len);
3290 if (ret >= 0) {
3291 msg.msg_iov->iov_base = host_msg;
3292 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3293 }
3294 g_free(host_msg);
3295 } else {
3296 ret = target_to_host_cmsg(&msg, msgp);
3297 if (ret == 0) {
3298 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3299 }
3300 }
3301 } else {
3302 ret = get_errno(safe_recvmsg(fd, &msg, flags));
3303 if (!is_error(ret)) {
3304 len = ret;
3305 if (fd_trans_host_to_target_data(fd)) {
3306 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
3307 MIN(msg.msg_iov->iov_len, len));
3308 } else {
3309 ret = host_to_target_cmsg(msgp, &msg);
3310 }
3311 if (!is_error(ret)) {
3312 msgp->msg_namelen = tswap32(msg.msg_namelen);
3313 msgp->msg_flags = tswap32(msg.msg_flags);
3314 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
3315 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
3316 msg.msg_name, msg.msg_namelen);
3317 if (ret) {
3318 goto out;
3319 }
3320 }
3321
3322 ret = len;
3323 }
3324 }
3325 }
3326
3327 out:
3328 unlock_iovec(vec, target_vec, count, !send);
3329 out2:
3330 return ret;
3331 }
3332
3333 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3334 int flags, int send)
3335 {
3336 abi_long ret;
3337 struct target_msghdr *msgp;
3338
3339 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3340 msgp,
3341 target_msg,
3342 send ? 1 : 0)) {
3343 return -TARGET_EFAULT;
3344 }
3345 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
3346 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3347 return ret;
3348 }
3349
3350 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3351 * so it might not have this *mmsg-specific flag either.
3352 */
3353 #ifndef MSG_WAITFORONE
3354 #define MSG_WAITFORONE 0x10000
3355 #endif
3356
3357 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3358 unsigned int vlen, unsigned int flags,
3359 int send)
3360 {
3361 struct target_mmsghdr *mmsgp;
3362 abi_long ret = 0;
3363 int i;
3364
3365 if (vlen > UIO_MAXIOV) {
3366 vlen = UIO_MAXIOV;
3367 }
3368
3369 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3370 if (!mmsgp) {
3371 return -TARGET_EFAULT;
3372 }
3373
3374 for (i = 0; i < vlen; i++) {
3375 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3376 if (is_error(ret)) {
3377 break;
3378 }
3379 mmsgp[i].msg_len = tswap32(ret);
3380 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3381 if (flags & MSG_WAITFORONE) {
3382 flags |= MSG_DONTWAIT;
3383 }
3384 }
3385
3386 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3387
3388 /* Return number of datagrams sent if we sent any at all;
3389 * otherwise return the error.
3390 */
3391 if (i) {
3392 return i;
3393 }
3394 return ret;
3395 }
3396
3397 /* do_accept4() Must return target values and target errnos. */
3398 static abi_long do_accept4(int fd, abi_ulong target_addr,
3399 abi_ulong target_addrlen_addr, int flags)
3400 {
3401 socklen_t addrlen, ret_addrlen;
3402 void *addr;
3403 abi_long ret;
3404 int host_flags;
3405
3406 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
3407
3408 if (target_addr == 0) {
3409 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
3410 }
3411
3412 /* linux returns EFAULT if addrlen pointer is invalid */
3413 if (get_user_u32(addrlen, target_addrlen_addr))
3414 return -TARGET_EFAULT;
3415
3416 if ((int)addrlen < 0) {
3417 return -TARGET_EINVAL;
3418 }
3419
3420 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
3421 return -TARGET_EFAULT;
3422 }
3423
3424 addr = alloca(addrlen);
3425
3426 ret_addrlen = addrlen;
3427 ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
3428 if (!is_error(ret)) {
3429 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3430 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
3431 ret = -TARGET_EFAULT;
3432 }
3433 }
3434 return ret;
3435 }
3436
3437 /* do_getpeername() Must return target values and target errnos. */
3438 static abi_long do_getpeername(int fd, abi_ulong target_addr,
3439 abi_ulong target_addrlen_addr)
3440 {
3441 socklen_t addrlen, ret_addrlen;
3442 void *addr;
3443 abi_long ret;
3444
3445 if (get_user_u32(addrlen, target_addrlen_addr))
3446 return -TARGET_EFAULT;
3447
3448 if ((int)addrlen < 0) {
3449 return -TARGET_EINVAL;
3450 }
3451
3452 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
3453 return -TARGET_EFAULT;
3454 }
3455
3456 addr = alloca(addrlen);
3457
3458 ret_addrlen = addrlen;
3459 ret = get_errno(getpeername(fd, addr, &ret_addrlen));
3460 if (!is_error(ret)) {
3461 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3462 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
3463 ret = -TARGET_EFAULT;
3464 }
3465 }
3466 return ret;
3467 }
3468
3469 /* do_getsockname() Must return target values and target errnos. */
3470 static abi_long do_getsockname(int fd, abi_ulong target_addr,
3471 abi_ulong target_addrlen_addr)
3472 {
3473 socklen_t addrlen, ret_addrlen;
3474 void *addr;
3475 abi_long ret;
3476
3477 if (get_user_u32(addrlen, target_addrlen_addr))
3478 return -TARGET_EFAULT;
3479
3480 if ((int)addrlen < 0) {
3481 return -TARGET_EINVAL;
3482 }
3483
3484 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
3485 return -TARGET_EFAULT;
3486 }
3487
3488 addr = alloca(addrlen);
3489
3490 ret_addrlen = addrlen;
3491 ret = get_errno(getsockname(fd, addr, &ret_addrlen));
3492 if (!is_error(ret)) {
3493 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3494 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
3495 ret = -TARGET_EFAULT;
3496 }
3497 }
3498 return ret;
3499 }
3500
3501 /* do_socketpair() Must return target values and target errnos. */
3502 static abi_long do_socketpair(int domain, int type, int protocol,
3503 abi_ulong target_tab_addr)
3504 {
3505 int tab[2];
3506 abi_long ret;
3507
3508 target_to_host_sock_type(&type);
3509
3510 ret = get_errno(socketpair(domain, type, protocol, tab));
3511 if (!is_error(ret)) {
3512 if (put_user_s32(tab[0], target_tab_addr)
3513 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3514 ret = -TARGET_EFAULT;
3515 }
3516 return ret;
3517 }
3518
3519 /* do_sendto() Must return target values and target errnos. */
3520 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3521 abi_ulong target_addr, socklen_t addrlen)
3522 {
3523 void *addr;
3524 void *host_msg;
3525 void *copy_msg = NULL;
3526 abi_long ret;
3527
3528 if ((int)addrlen < 0) {
3529 return -TARGET_EINVAL;
3530 }
3531
3532 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3533 if (!host_msg)
3534 return -TARGET_EFAULT;
3535 if (fd_trans_target_to_host_data(fd)) {
3536 copy_msg = host_msg;
3537 host_msg = g_malloc(len);
3538 memcpy(host_msg, copy_msg, len);
3539 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3540 if (ret < 0) {
3541 goto fail;
3542 }
3543 }
3544 if (target_addr) {
3545 addr = alloca(addrlen+1);
3546 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
3547 if (ret) {
3548 goto fail;
3549 }
3550 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
3551 } else {
3552 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
3553 }
3554 fail:
3555 if (copy_msg) {
3556 g_free(host_msg);
3557 host_msg = copy_msg;
3558 }
3559 unlock_user(host_msg, msg, 0);
3560 return ret;
3561 }
3562
3563 /* do_recvfrom() Must return target values and target errnos. */
3564 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3565 abi_ulong target_addr,
3566 abi_ulong target_addrlen)
3567 {
3568 socklen_t addrlen, ret_addrlen;
3569 void *addr;
3570 void *host_msg;
3571 abi_long ret;
3572
3573 if (!msg) {
3574 host_msg = NULL;
3575 } else {
3576 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3577 if (!host_msg) {
3578 return -TARGET_EFAULT;
3579 }
3580 }
3581 if (target_addr) {
3582 if (get_user_u32(addrlen, target_addrlen)) {
3583 ret = -TARGET_EFAULT;
3584 goto fail;
3585 }
3586 if ((int)addrlen < 0) {
3587 ret = -TARGET_EINVAL;
3588 goto fail;
3589 }
3590 addr = alloca(addrlen);
3591 ret_addrlen = addrlen;
3592 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
3593 addr, &ret_addrlen));
3594 } else {
3595 addr = NULL; /* To keep compiler quiet. */
3596 addrlen = 0; /* To keep compiler quiet. */
3597 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
3598 }
3599 if (!is_error(ret)) {
3600 if (fd_trans_host_to_target_data(fd)) {
3601 abi_long trans;
3602 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
3603 if (is_error(trans)) {
3604 ret = trans;
3605 goto fail;
3606 }
3607 }
3608 if (target_addr) {
3609 host_to_target_sockaddr(target_addr, addr,
3610 MIN(addrlen, ret_addrlen));
3611 if (put_user_u32(ret_addrlen, target_addrlen)) {
3612 ret = -TARGET_EFAULT;
3613 goto fail;
3614 }
3615 }
3616 unlock_user(host_msg, msg, len);
3617 } else {
3618 fail:
3619 unlock_user(host_msg, msg, 0);
3620 }
3621 return ret;
3622 }
3623
3624 #ifdef TARGET_NR_socketcall
3625 /* do_socketcall() must return target values and target errnos. */
3626 static abi_long do_socketcall(int num, abi_ulong vptr)
3627 {
3628 static const unsigned nargs[] = { /* number of arguments per operation */
3629 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
3630 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
3631 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
3632 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
3633 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
3634 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
3635 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
3636 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
3637 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
3638 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
3639 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
3640 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
3641 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
3642 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3643 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3644 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
3645 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
3646 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
3647 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
3648 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
3649 };
3650 abi_long a[6]; /* max 6 args */
3651 unsigned i;
3652
3653 /* check the range of the first argument num */
3654 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3655 if (num < 1 || num > TARGET_SYS_SENDMMSG) {
3656 return -TARGET_EINVAL;
3657 }
3658 /* ensure we have space for args */
3659 if (nargs[num] > ARRAY_SIZE(a)) {
3660 return -TARGET_EINVAL;
3661 }
3662 /* collect the arguments in a[] according to nargs[] */
3663 for (i = 0; i < nargs[num]; ++i) {
3664 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
3665 return -TARGET_EFAULT;
3666 }
3667 }
3668 /* now when we have the args, invoke the appropriate underlying function */
3669 switch (num) {
3670 case TARGET_SYS_SOCKET: /* domain, type, protocol */
3671 return do_socket(a[0], a[1], a[2]);
3672 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
3673 return do_bind(a[0], a[1], a[2]);
3674 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
3675 return do_connect(a[0], a[1], a[2]);
3676 case TARGET_SYS_LISTEN: /* sockfd, backlog */
3677 return get_errno(listen(a[0], a[1]));
3678 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
3679 return do_accept4(a[0], a[1], a[2], 0);
3680 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
3681 return do_getsockname(a[0], a[1], a[2]);
3682 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
3683 return do_getpeername(a[0], a[1], a[2]);
3684 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
3685 return do_socketpair(a[0], a[1], a[2], a[3]);
3686 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
3687 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
3688 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
3689 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
3690 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
3691 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
3692 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
3693 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
3694 case TARGET_SYS_SHUTDOWN: /* sockfd, how */
3695 return get_errno(shutdown(a[0], a[1]));
3696 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3697 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
3698 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3699 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
3700 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
3701 return do_sendrecvmsg(a[0], a[1], a[2], 1);
3702 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
3703 return do_sendrecvmsg(a[0], a[1], a[2], 0);
3704 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
3705 return do_accept4(a[0], a[1], a[2], a[3]);
3706 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
3707 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
3708 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
3709 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
3710 default:
3711 qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
3712 return -TARGET_EINVAL;
3713 }
3714 }
3715 #endif
3716
3717 #define N_SHM_REGIONS 32
3718
3719 static struct shm_region {
3720 abi_ulong start;
3721 abi_ulong size;
3722 bool in_use;
3723 } shm_regions[N_SHM_REGIONS];
3724
3725 #ifndef TARGET_SEMID64_DS
3726 /* asm-generic version of this struct */
3727 struct target_semid64_ds
3728 {
3729 struct target_ipc_perm sem_perm;
3730 abi_ulong sem_otime;
3731 #if TARGET_ABI_BITS == 32
3732 abi_ulong __unused1;
3733 #endif
3734 abi_ulong sem_ctime;
3735 #if TARGET_ABI_BITS == 32
3736 abi_ulong __unused2;
3737 #endif
3738 abi_ulong sem_nsems;
3739 abi_ulong __unused3;
3740 abi_ulong __unused4;
3741 };
3742 #endif
3743
3744 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
3745 abi_ulong target_addr)
3746 {
3747 struct target_ipc_perm *target_ip;
3748 struct target_semid64_ds *target_sd;
3749
3750 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3751 return -TARGET_EFAULT;
3752 target_ip = &(target_sd->sem_perm);
3753 host_ip->__key = tswap32(target_ip->__key);
3754 host_ip->uid = tswap32(target_ip->uid);
3755 host_ip->gid = tswap32(target_ip->gid);
3756 host_ip->cuid = tswap32(target_ip->cuid);
3757 host_ip->cgid = tswap32(target_ip->cgid);
3758 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3759 host_ip->mode = tswap32(target_ip->mode);
3760 #else
3761 host_ip->mode = tswap16(target_ip->mode);
3762 #endif
3763 #if defined(TARGET_PPC)
3764 host_ip->__seq = tswap32(target_ip->__seq);
3765 #else
3766 host_ip->__seq = tswap16(target_ip->__seq);
3767 #endif
3768 unlock_user_struct(target_sd, target_addr, 0);
3769 return 0;
3770 }
3771
3772 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
3773 struct ipc_perm *host_ip)
3774 {
3775 struct target_ipc_perm *target_ip;
3776 struct target_semid64_ds *target_sd;
3777
3778 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3779 return -TARGET_EFAULT;
3780 target_ip = &(target_sd->sem_perm);
3781 target_ip->__key = tswap32(host_ip->__key);
3782 target_ip->uid = tswap32(host_ip->uid);
3783 target_ip->gid = tswap32(host_ip->gid);
3784 target_ip->cuid = tswap32(host_ip->cuid);
3785 target_ip->cgid = tswap32(host_ip->cgid);
3786 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3787 target_ip->mode = tswap32(host_ip->mode);
3788 #else
3789 target_ip->mode = tswap16(host_ip->mode);
3790 #endif
3791 #if defined(TARGET_PPC)
3792 target_ip->__seq = tswap32(host_ip->__seq);
3793 #else
3794 target_ip->__seq = tswap16(host_ip->__seq);
3795 #endif
3796 unlock_user_struct(target_sd, target_addr, 1);
3797 return 0;
3798 }
3799
3800 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
3801 abi_ulong target_addr)
3802 {
3803 struct target_semid64_ds *target_sd;
3804
3805 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3806 return -TARGET_EFAULT;
3807 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
3808 return -TARGET_EFAULT;
3809 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
3810 host_sd->sem_otime = tswapal(target_sd->sem_otime);
3811 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
3812 unlock_user_struct(target_sd, target_addr, 0);
3813 return 0;
3814 }
3815
3816 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
3817 struct semid_ds *host_sd)
3818 {
3819 struct target_semid64_ds *target_sd;
3820
3821 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3822 return -TARGET_EFAULT;
3823 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
3824 return -TARGET_EFAULT;
3825 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
3826 target_sd->sem_otime = tswapal(host_sd->sem_otime);
3827 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
3828 unlock_user_struct(target_sd, target_addr, 1);
3829 return 0;
3830 }
3831
3832 struct target_seminfo {
3833 int semmap;
3834 int semmni;
3835 int semmns;
3836 int semmnu;
3837 int semmsl;
3838 int semopm;
3839 int semume;
3840 int semusz;
3841 int semvmx;
3842 int semaem;
3843 };
3844
3845 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
3846 struct seminfo *host_seminfo)
3847 {
3848 struct target_seminfo *target_seminfo;
3849 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
3850 return -TARGET_EFAULT;
3851 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
3852 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
3853 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
3854 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
3855 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
3856 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
3857 __put_user(host_seminfo->semume, &target_seminfo->semume);
3858 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
3859 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
3860 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
3861 unlock_user_struct(target_seminfo, target_addr, 1);
3862 return 0;
3863 }
3864
3865 union semun {
3866 int val;
3867 struct semid_ds *buf;
3868 unsigned short *array;
3869 struct seminfo *__buf;
3870 };
3871
3872 union target_semun {
3873 int val;
3874 abi_ulong buf;
3875 abi_ulong array;
3876 abi_ulong __buf;
3877 };
3878
3879 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3880 abi_ulong target_addr)
3881 {
3882 int nsems;
3883 unsigned short *array;
3884 union semun semun;
3885 struct semid_ds semid_ds;
3886 int i, ret;
3887
3888 semun.buf = &semid_ds;
3889
3890 ret = semctl(semid, 0, IPC_STAT, semun);
3891 if (ret == -1)
3892 return get_errno(ret);
3893
3894 nsems = semid_ds.sem_nsems;
3895
3896 *host_array = g_try_new(unsigned short, nsems);
3897 if (!*host_array) {
3898 return -TARGET_ENOMEM;
3899 }
3900 array = lock_user(VERIFY_READ, target_addr,
3901 nsems*sizeof(unsigned short), 1);
3902 if (!array) {
3903 g_free(*host_array);
3904 return -TARGET_EFAULT;
3905 }
3906
3907 for(i=0; i<nsems; i++) {
3908 __get_user((*host_array)[i], &array[i]);
3909 }
3910 unlock_user(array, target_addr, 0);
3911
3912 return 0;
3913 }
3914
3915 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3916 unsigned short **host_array)
3917 {
3918 int nsems;
3919 unsigned short *array;
3920 union semun semun;
3921 struct semid_ds semid_ds;
3922 int i, ret;
3923
3924 semun.buf = &semid_ds;
3925
3926 ret = semctl(semid, 0, IPC_STAT, semun);
3927 if (ret == -1)
3928 return get_errno(ret);
3929
3930 nsems = semid_ds.sem_nsems;
3931
3932 array = lock_user(VERIFY_WRITE, target_addr,
3933 nsems*sizeof(unsigned short), 0);
3934 if (!array)
3935 return -TARGET_EFAULT;
3936
3937 for(i=0; i<nsems; i++) {
3938 __put_user((*host_array)[i], &array[i]);
3939 }
3940 g_free(*host_array);
3941 unlock_user(array, target_addr, 1);
3942
3943 return 0;
3944 }
3945
3946 static inline abi_long do_semctl(int semid, int semnum, int cmd,
3947 abi_ulong target_arg)
3948 {
3949 union target_semun target_su = { .buf = target_arg };
3950 union semun arg;
3951 struct semid_ds dsarg;
3952 unsigned short *array = NULL;
3953 struct seminfo seminfo;
3954 abi_long ret = -TARGET_EINVAL;
3955 abi_long err;
3956 cmd &= 0xff;
3957
3958 switch( cmd ) {
3959 case GETVAL:
3960 case SETVAL:
3961 /* In 64 bit cross-endian situations, we will erroneously pick up
3962 * the wrong half of the union for the "val" element. To rectify
3963 * this, the entire 8-byte structure is byteswapped, followed by
3964 * a swap of the 4 byte val field. In other cases, the data is
3965 * already in proper host byte order. */
3966 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
3967 target_su.buf = tswapal(target_su.buf);
3968 arg.val = tswap32(target_su.val);
3969 } else {
3970 arg.val = target_su.val;
3971 }
3972 ret = get_errno(semctl(semid, semnum, cmd, arg));
3973 break;
3974 case GETALL:
3975 case SETALL:
3976 err = target_to_host_semarray(semid, &array, target_su.array);
3977 if (err)
3978 return err;
3979 arg.array = array;
3980 ret = get_errno(semctl(semid, semnum, cmd, arg));
3981 err = host_to_target_semarray(semid, target_su.array, &array);
3982 if (err)
3983 return err;
3984 break;
3985 case IPC_STAT:
3986 case IPC_SET:
3987 case SEM_STAT:
3988 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3989 if (err)
3990 return err;
3991 arg.buf = &dsarg;
3992 ret = get_errno(semctl(semid, semnum, cmd, arg));
3993 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3994 if (err)
3995 return err;
3996 break;
3997 case IPC_INFO:
3998 case SEM_INFO:
3999 arg.__buf = &seminfo;
4000 ret = get_errno(semctl(semid, semnum, cmd, arg));
4001 err = host_to_target_seminfo(target_su.__buf, &seminfo);
4002 if (err)
4003 return err;
4004 break;
4005 case IPC_RMID:
4006 case GETPID:
4007 case GETNCNT:
4008 case GETZCNT:
4009 ret = get_errno(semctl(semid, semnum, cmd, NULL));
4010 break;
4011 }
4012
4013 return ret;
4014 }
4015
4016 struct target_sembuf {
4017 unsigned short sem_num;
4018 short sem_op;
4019 short sem_flg;
4020 };
4021
4022 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
4023 abi_ulong target_addr,
4024 unsigned nsops)
4025 {
4026 struct target_sembuf *target_sembuf;
4027 int i;
4028
4029 target_sembuf = lock_user(VERIFY_READ, target_addr,
4030 nsops*sizeof(struct target_sembuf), 1);
4031 if (!target_sembuf)
4032 return -TARGET_EFAULT;
4033
4034 for(i=0; i<nsops; i++) {
4035 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
4036 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
4037 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
4038 }
4039
4040 unlock_user(target_sembuf, target_addr, 0);
4041
4042 return 0;
4043 }
4044
4045 #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
4046 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
4047
4048 /*
4049 * This macro is required to handle the s390 variants, which passes the
4050 * arguments in a different order than default.
4051 */
4052 #ifdef __s390x__
4053 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4054 (__nsops), (__timeout), (__sops)
4055 #else
4056 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4057 (__nsops), 0, (__sops), (__timeout)
4058 #endif
4059
4060 static inline abi_long do_semtimedop(int semid,
4061 abi_long ptr,
4062 unsigned nsops,
4063 abi_long timeout, bool time64)
4064 {
4065 struct sembuf *sops;
4066 struct timespec ts, *pts = NULL;
4067 abi_long ret;
4068
4069 if (timeout) {
4070 pts = &ts;
4071 if (time64) {
4072 if (target_to_host_timespec64(pts, timeout)) {
4073 return -TARGET_EFAULT;
4074 }
4075 } else {
4076 if (target_to_host_timespec(pts, timeout)) {
4077 return -TARGET_EFAULT;
4078 }
4079 }
4080 }
4081
4082 if (nsops > TARGET_SEMOPM) {
4083 return -TARGET_E2BIG;
4084 }
4085
4086 sops = g_new(struct sembuf, nsops);
4087
4088 if (target_to_host_sembuf(sops, ptr, nsops)) {
4089 g_free(sops);
4090 return -TARGET_EFAULT;
4091 }
4092
4093 ret = -TARGET_ENOSYS;
4094 #ifdef __NR_semtimedop
4095 ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
4096 #endif
4097 #ifdef __NR_ipc
4098 if (ret == -TARGET_ENOSYS) {
4099 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
4100 SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
4101 }
4102 #endif
4103 g_free(sops);
4104 return ret;
4105 }
4106 #endif
4107
4108 struct target_msqid_ds
4109 {
4110 struct target_ipc_perm msg_perm;
4111 abi_ulong msg_stime;
4112 #if TARGET_ABI_BITS == 32
4113 abi_ulong __unused1;
4114 #endif
4115 abi_ulong msg_rtime;
4116 #if TARGET_ABI_BITS == 32
4117 abi_ulong __unused2;
4118 #endif
4119 abi_ulong msg_ctime;
4120 #if TARGET_ABI_BITS == 32
4121 abi_ulong __unused3;
4122 #endif
4123 abi_ulong __msg_cbytes;
4124 abi_ulong msg_qnum;
4125 abi_ulong msg_qbytes;
4126 abi_ulong msg_lspid;
4127 abi_ulong msg_lrpid;
4128 abi_ulong __unused4;
4129 abi_ulong __unused5;
4130 };
4131
4132 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
4133 abi_ulong target_addr)
4134 {
4135 struct target_msqid_ds *target_md;
4136
4137 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
4138 return -TARGET_EFAULT;
4139 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
4140 return -TARGET_EFAULT;
4141 host_md->msg_stime = tswapal(target_md->msg_stime);
4142 host_md->msg_rtime = tswapal(target_md->msg_rtime);
4143 host_md->msg_ctime = tswapal(target_md->msg_ctime);
4144 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
4145 host_md->msg_qnum = tswapal(target_md->msg_qnum);
4146 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
4147 host_md->msg_lspid = tswapal(target_md->msg_lspid);
4148 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
4149 unlock_user_struct(target_md, target_addr, 0);
4150 return 0;
4151 }
4152
4153 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
4154 struct msqid_ds *host_md)
4155 {
4156 struct target_msqid_ds *target_md;
4157
4158 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
4159 return -TARGET_EFAULT;
4160 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
4161 return -TARGET_EFAULT;
4162 target_md->msg_stime = tswapal(host_md->msg_stime);
4163 target_md->msg_rtime = tswapal(host_md->msg_rtime);
4164 target_md->msg_ctime = tswapal(host_md->msg_ctime);
4165 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
4166 target_md->msg_qnum = tswapal(host_md->msg_qnum);
4167 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
4168 target_md->msg_lspid = tswapal(host_md->msg_lspid);
4169 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
4170 unlock_user_struct(target_md, target_addr, 1);
4171 return 0;
4172 }
4173
4174 struct target_msginfo {
4175 int msgpool;
4176 int msgmap;
4177 int msgmax;
4178 int msgmnb;
4179 int msgmni;
4180 int msgssz;
4181 int msgtql;
4182 unsigned short int msgseg;
4183 };
4184
4185 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
4186 struct msginfo *host_msginfo)
4187 {
4188 struct target_msginfo *target_msginfo;
4189 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
4190 return -TARGET_EFAULT;
4191 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
4192 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
4193 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
4194 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
4195 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
4196 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
4197 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
4198 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
4199 unlock_user_struct(target_msginfo, target_addr, 1);
4200 return 0;
4201 }
4202
4203 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
4204 {
4205 struct msqid_ds dsarg;
4206 struct msginfo msginfo;
4207 abi_long ret = -TARGET_EINVAL;
4208
4209 cmd &= 0xff;
4210
4211 switch (cmd) {
4212 case IPC_STAT:
4213 case IPC_SET:
4214 case MSG_STAT:
4215 if (target_to_host_msqid_ds(&dsarg,ptr))
4216 return -TARGET_EFAULT;
4217 ret = get_errno(msgctl(msgid, cmd, &dsarg));
4218 if (host_to_target_msqid_ds(ptr,&dsarg))
4219 return -TARGET_EFAULT;
4220 break;
4221 case IPC_RMID:
4222 ret = get_errno(msgctl(msgid, cmd, NULL));
4223 break;
4224 case IPC_INFO:
4225 case MSG_INFO:
4226 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
4227 if (host_to_target_msginfo(ptr, &msginfo))
4228 return -TARGET_EFAULT;
4229 break;
4230 }
4231
4232 return ret;
4233 }
4234
4235 struct target_msgbuf {
4236 abi_long mtype;
4237 char mtext[1];
4238 };
4239
4240 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
4241 ssize_t msgsz, int msgflg)
4242 {
4243 struct target_msgbuf *target_mb;
4244 struct msgbuf *host_mb;
4245 abi_long ret = 0;
4246
4247 if (msgsz < 0) {
4248 return -TARGET_EINVAL;
4249 }
4250
4251 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
4252 return -TARGET_EFAULT;
4253 host_mb = g_try_malloc(msgsz + sizeof(long));
4254 if (!host_mb) {
4255 unlock_user_struct(target_mb, msgp, 0);
4256 return -TARGET_ENOMEM;
4257 }
4258 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
4259 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
4260 ret = -TARGET_ENOSYS;
4261 #ifdef __NR_msgsnd
4262 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
4263 #endif
4264 #ifdef __NR_ipc
4265 if (ret == -TARGET_ENOSYS) {
4266 #ifdef __s390x__
4267 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4268 host_mb));
4269 #else
4270 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4271 host_mb, 0));
4272 #endif
4273 }
4274 #endif
4275 g_free(host_mb);
4276 unlock_user_struct(target_mb, msgp, 0);
4277
4278 return ret;
4279 }
4280
4281 #ifdef __NR_ipc
4282 #if defined(__sparc__)
4283 /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
4284 #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
4285 #elif defined(__s390x__)
4286 /* The s390 sys_ipc variant has only five parameters. */
4287 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4288 ((long int[]){(long int)__msgp, __msgtyp})
4289 #else
4290 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4291 ((long int[]){(long int)__msgp, __msgtyp}), 0
4292 #endif
4293 #endif
4294
4295 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
4296 ssize_t msgsz, abi_long msgtyp,
4297 int msgflg)
4298 {
4299 struct target_msgbuf *target_mb;
4300 char *target_mtext;
4301 struct msgbuf *host_mb;
4302 abi_long ret = 0;
4303
4304 if (msgsz < 0) {
4305 return -TARGET_EINVAL;
4306 }
4307
4308 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
4309 return -TARGET_EFAULT;
4310
4311 host_mb = g_try_malloc(msgsz + sizeof(long));
4312 if (!host_mb) {
4313 ret = -TARGET_ENOMEM;
4314 goto end;
4315 }
4316 ret = -TARGET_ENOSYS;
4317 #ifdef __NR_msgrcv
4318 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
4319 #endif
4320 #ifdef __NR_ipc
4321 if (ret == -TARGET_ENOSYS) {
4322 ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
4323 msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
4324 }
4325 #endif
4326
4327 if (ret > 0) {
4328 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
4329 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
4330 if (!target_mtext) {
4331 ret = -TARGET_EFAULT;
4332 goto end;
4333 }
4334 memcpy(target_mb->mtext, host_mb->mtext, ret);
4335 unlock_user(target_mtext, target_mtext_addr, ret);
4336 }
4337
4338 target_mb->mtype = tswapal(host_mb->mtype);
4339
4340 end:
4341 if (target_mb)
4342 unlock_user_struct(target_mb, msgp, 1);
4343 g_free(host_mb);
4344 return ret;
4345 }
4346
4347 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
4348 abi_ulong target_addr)
4349 {
4350 struct target_shmid_ds *target_sd;
4351
4352 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4353 return -TARGET_EFAULT;
4354 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
4355 return -TARGET_EFAULT;
4356 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4357 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
4358 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4359 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4360 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4361 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4362 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4363 unlock_user_struct(target_sd, target_addr, 0);
4364 return 0;
4365 }
4366
4367 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
4368 struct shmid_ds *host_sd)
4369 {
4370 struct target_shmid_ds *target_sd;
4371
4372 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))