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