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