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