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