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