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