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