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