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