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