linux-user: Use safe_syscall for read and write 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 #define safe_syscall0(type, name) \
664 static type safe_##name(void) \
665 { \
666 return safe_syscall(__NR_##name); \
667 }
668
669 #define safe_syscall1(type, name, type1, arg1) \
670 static type safe_##name(type1 arg1) \
671 { \
672 return safe_syscall(__NR_##name, arg1); \
673 }
674
675 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
676 static type safe_##name(type1 arg1, type2 arg2) \
677 { \
678 return safe_syscall(__NR_##name, arg1, arg2); \
679 }
680
681 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
682 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
683 { \
684 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
685 }
686
687 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
688 type4, arg4) \
689 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
690 { \
691 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
692 }
693
694 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
695 type4, arg4, type5, arg5) \
696 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
697 type5 arg5) \
698 { \
699 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
700 }
701
702 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
703 type4, arg4, type5, arg5, type6, arg6) \
704 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
705 type5 arg5, type6 arg6) \
706 { \
707 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
708 }
709
710 safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
711 safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
712
713 static inline int host_to_target_sock_type(int host_type)
714 {
715 int target_type;
716
717 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
718 case SOCK_DGRAM:
719 target_type = TARGET_SOCK_DGRAM;
720 break;
721 case SOCK_STREAM:
722 target_type = TARGET_SOCK_STREAM;
723 break;
724 default:
725 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
726 break;
727 }
728
729 #if defined(SOCK_CLOEXEC)
730 if (host_type & SOCK_CLOEXEC) {
731 target_type |= TARGET_SOCK_CLOEXEC;
732 }
733 #endif
734
735 #if defined(SOCK_NONBLOCK)
736 if (host_type & SOCK_NONBLOCK) {
737 target_type |= TARGET_SOCK_NONBLOCK;
738 }
739 #endif
740
741 return target_type;
742 }
743
744 static abi_ulong target_brk;
745 static abi_ulong target_original_brk;
746 static abi_ulong brk_page;
747
748 void target_set_brk(abi_ulong new_brk)
749 {
750 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
751 brk_page = HOST_PAGE_ALIGN(target_brk);
752 }
753
754 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
755 #define DEBUGF_BRK(message, args...)
756
757 /* do_brk() must return target values and target errnos. */
758 abi_long do_brk(abi_ulong new_brk)
759 {
760 abi_long mapped_addr;
761 int new_alloc_size;
762
763 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
764
765 if (!new_brk) {
766 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
767 return target_brk;
768 }
769 if (new_brk < target_original_brk) {
770 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
771 target_brk);
772 return target_brk;
773 }
774
775 /* If the new brk is less than the highest page reserved to the
776 * target heap allocation, set it and we're almost done... */
777 if (new_brk <= brk_page) {
778 /* Heap contents are initialized to zero, as for anonymous
779 * mapped pages. */
780 if (new_brk > target_brk) {
781 memset(g2h(target_brk), 0, new_brk - target_brk);
782 }
783 target_brk = new_brk;
784 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
785 return target_brk;
786 }
787
788 /* We need to allocate more memory after the brk... Note that
789 * we don't use MAP_FIXED because that will map over the top of
790 * any existing mapping (like the one with the host libc or qemu
791 * itself); instead we treat "mapped but at wrong address" as
792 * a failure and unmap again.
793 */
794 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
795 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
796 PROT_READ|PROT_WRITE,
797 MAP_ANON|MAP_PRIVATE, 0, 0));
798
799 if (mapped_addr == brk_page) {
800 /* Heap contents are initialized to zero, as for anonymous
801 * mapped pages. Technically the new pages are already
802 * initialized to zero since they *are* anonymous mapped
803 * pages, however we have to take care with the contents that
804 * come from the remaining part of the previous page: it may
805 * contains garbage data due to a previous heap usage (grown
806 * then shrunken). */
807 memset(g2h(target_brk), 0, brk_page - target_brk);
808
809 target_brk = new_brk;
810 brk_page = HOST_PAGE_ALIGN(target_brk);
811 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
812 target_brk);
813 return target_brk;
814 } else if (mapped_addr != -1) {
815 /* Mapped but at wrong address, meaning there wasn't actually
816 * enough space for this brk.
817 */
818 target_munmap(mapped_addr, new_alloc_size);
819 mapped_addr = -1;
820 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
821 }
822 else {
823 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
824 }
825
826 #if defined(TARGET_ALPHA)
827 /* We (partially) emulate OSF/1 on Alpha, which requires we
828 return a proper errno, not an unchanged brk value. */
829 return -TARGET_ENOMEM;
830 #endif
831 /* For everything else, return the previous break. */
832 return target_brk;
833 }
834
835 static inline abi_long copy_from_user_fdset(fd_set *fds,
836 abi_ulong target_fds_addr,
837 int n)
838 {
839 int i, nw, j, k;
840 abi_ulong b, *target_fds;
841
842 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
843 if (!(target_fds = lock_user(VERIFY_READ,
844 target_fds_addr,
845 sizeof(abi_ulong) * nw,
846 1)))
847 return -TARGET_EFAULT;
848
849 FD_ZERO(fds);
850 k = 0;
851 for (i = 0; i < nw; i++) {
852 /* grab the abi_ulong */
853 __get_user(b, &target_fds[i]);
854 for (j = 0; j < TARGET_ABI_BITS; j++) {
855 /* check the bit inside the abi_ulong */
856 if ((b >> j) & 1)
857 FD_SET(k, fds);
858 k++;
859 }
860 }
861
862 unlock_user(target_fds, target_fds_addr, 0);
863
864 return 0;
865 }
866
867 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
868 abi_ulong target_fds_addr,
869 int n)
870 {
871 if (target_fds_addr) {
872 if (copy_from_user_fdset(fds, target_fds_addr, n))
873 return -TARGET_EFAULT;
874 *fds_ptr = fds;
875 } else {
876 *fds_ptr = NULL;
877 }
878 return 0;
879 }
880
881 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
882 const fd_set *fds,
883 int n)
884 {
885 int i, nw, j, k;
886 abi_long v;
887 abi_ulong *target_fds;
888
889 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
890 if (!(target_fds = lock_user(VERIFY_WRITE,
891 target_fds_addr,
892 sizeof(abi_ulong) * nw,
893 0)))
894 return -TARGET_EFAULT;
895
896 k = 0;
897 for (i = 0; i < nw; i++) {
898 v = 0;
899 for (j = 0; j < TARGET_ABI_BITS; j++) {
900 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
901 k++;
902 }
903 __put_user(v, &target_fds[i]);
904 }
905
906 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
907
908 return 0;
909 }
910
911 #if defined(__alpha__)
912 #define HOST_HZ 1024
913 #else
914 #define HOST_HZ 100
915 #endif
916
917 static inline abi_long host_to_target_clock_t(long ticks)
918 {
919 #if HOST_HZ == TARGET_HZ
920 return ticks;
921 #else
922 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
923 #endif
924 }
925
926 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
927 const struct rusage *rusage)
928 {
929 struct target_rusage *target_rusage;
930
931 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
932 return -TARGET_EFAULT;
933 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
934 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
935 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
936 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
937 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
938 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
939 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
940 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
941 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
942 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
943 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
944 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
945 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
946 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
947 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
948 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
949 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
950 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
951 unlock_user_struct(target_rusage, target_addr, 1);
952
953 return 0;
954 }
955
956 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
957 {
958 abi_ulong target_rlim_swap;
959 rlim_t result;
960
961 target_rlim_swap = tswapal(target_rlim);
962 if (target_rlim_swap == TARGET_RLIM_INFINITY)
963 return RLIM_INFINITY;
964
965 result = target_rlim_swap;
966 if (target_rlim_swap != (rlim_t)result)
967 return RLIM_INFINITY;
968
969 return result;
970 }
971
972 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
973 {
974 abi_ulong target_rlim_swap;
975 abi_ulong result;
976
977 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
978 target_rlim_swap = TARGET_RLIM_INFINITY;
979 else
980 target_rlim_swap = rlim;
981 result = tswapal(target_rlim_swap);
982
983 return result;
984 }
985
986 static inline int target_to_host_resource(int code)
987 {
988 switch (code) {
989 case TARGET_RLIMIT_AS:
990 return RLIMIT_AS;
991 case TARGET_RLIMIT_CORE:
992 return RLIMIT_CORE;
993 case TARGET_RLIMIT_CPU:
994 return RLIMIT_CPU;
995 case TARGET_RLIMIT_DATA:
996 return RLIMIT_DATA;
997 case TARGET_RLIMIT_FSIZE:
998 return RLIMIT_FSIZE;
999 case TARGET_RLIMIT_LOCKS:
1000 return RLIMIT_LOCKS;
1001 case TARGET_RLIMIT_MEMLOCK:
1002 return RLIMIT_MEMLOCK;
1003 case TARGET_RLIMIT_MSGQUEUE:
1004 return RLIMIT_MSGQUEUE;
1005 case TARGET_RLIMIT_NICE:
1006 return RLIMIT_NICE;
1007 case TARGET_RLIMIT_NOFILE:
1008 return RLIMIT_NOFILE;
1009 case TARGET_RLIMIT_NPROC:
1010 return RLIMIT_NPROC;
1011 case TARGET_RLIMIT_RSS:
1012 return RLIMIT_RSS;
1013 case TARGET_RLIMIT_RTPRIO:
1014 return RLIMIT_RTPRIO;
1015 case TARGET_RLIMIT_SIGPENDING:
1016 return RLIMIT_SIGPENDING;
1017 case TARGET_RLIMIT_STACK:
1018 return RLIMIT_STACK;
1019 default:
1020 return code;
1021 }
1022 }
1023
1024 static inline abi_long copy_from_user_timeval(struct timeval *tv,
1025 abi_ulong target_tv_addr)
1026 {
1027 struct target_timeval *target_tv;
1028
1029 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
1030 return -TARGET_EFAULT;
1031
1032 __get_user(tv->tv_sec, &target_tv->tv_sec);
1033 __get_user(tv->tv_usec, &target_tv->tv_usec);
1034
1035 unlock_user_struct(target_tv, target_tv_addr, 0);
1036
1037 return 0;
1038 }
1039
1040 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1041 const struct timeval *tv)
1042 {
1043 struct target_timeval *target_tv;
1044
1045 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
1046 return -TARGET_EFAULT;
1047
1048 __put_user(tv->tv_sec, &target_tv->tv_sec);
1049 __put_user(tv->tv_usec, &target_tv->tv_usec);
1050
1051 unlock_user_struct(target_tv, target_tv_addr, 1);
1052
1053 return 0;
1054 }
1055
1056 static inline abi_long copy_from_user_timezone(struct timezone *tz,
1057 abi_ulong target_tz_addr)
1058 {
1059 struct target_timezone *target_tz;
1060
1061 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1062 return -TARGET_EFAULT;
1063 }
1064
1065 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1066 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1067
1068 unlock_user_struct(target_tz, target_tz_addr, 0);
1069
1070 return 0;
1071 }
1072
1073 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1074 #include <mqueue.h>
1075
1076 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1077 abi_ulong target_mq_attr_addr)
1078 {
1079 struct target_mq_attr *target_mq_attr;
1080
1081 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1082 target_mq_attr_addr, 1))
1083 return -TARGET_EFAULT;
1084
1085 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1086 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1087 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1088 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1089
1090 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1091
1092 return 0;
1093 }
1094
1095 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1096 const struct mq_attr *attr)
1097 {
1098 struct target_mq_attr *target_mq_attr;
1099
1100 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1101 target_mq_attr_addr, 0))
1102 return -TARGET_EFAULT;
1103
1104 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1105 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1106 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1107 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1108
1109 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1110
1111 return 0;
1112 }
1113 #endif
1114
1115 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1116 /* do_select() must return target values and target errnos. */
1117 static abi_long do_select(int n,
1118 abi_ulong rfd_addr, abi_ulong wfd_addr,
1119 abi_ulong efd_addr, abi_ulong target_tv_addr)
1120 {
1121 fd_set rfds, wfds, efds;
1122 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1123 struct timeval tv, *tv_ptr;
1124 abi_long ret;
1125
1126 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1127 if (ret) {
1128 return ret;
1129 }
1130 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1131 if (ret) {
1132 return ret;
1133 }
1134 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1135 if (ret) {
1136 return ret;
1137 }
1138
1139 if (target_tv_addr) {
1140 if (copy_from_user_timeval(&tv, target_tv_addr))
1141 return -TARGET_EFAULT;
1142 tv_ptr = &tv;
1143 } else {
1144 tv_ptr = NULL;
1145 }
1146
1147 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
1148
1149 if (!is_error(ret)) {
1150 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1151 return -TARGET_EFAULT;
1152 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1153 return -TARGET_EFAULT;
1154 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1155 return -TARGET_EFAULT;
1156
1157 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
1158 return -TARGET_EFAULT;
1159 }
1160
1161 return ret;
1162 }
1163 #endif
1164
1165 static abi_long do_pipe2(int host_pipe[], int flags)
1166 {
1167 #ifdef CONFIG_PIPE2
1168 return pipe2(host_pipe, flags);
1169 #else
1170 return -ENOSYS;
1171 #endif
1172 }
1173
1174 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1175 int flags, int is_pipe2)
1176 {
1177 int host_pipe[2];
1178 abi_long ret;
1179 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1180
1181 if (is_error(ret))
1182 return get_errno(ret);
1183
1184 /* Several targets have special calling conventions for the original
1185 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1186 if (!is_pipe2) {
1187 #if defined(TARGET_ALPHA)
1188 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1189 return host_pipe[0];
1190 #elif defined(TARGET_MIPS)
1191 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1192 return host_pipe[0];
1193 #elif defined(TARGET_SH4)
1194 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1195 return host_pipe[0];
1196 #elif defined(TARGET_SPARC)
1197 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1198 return host_pipe[0];
1199 #endif
1200 }
1201
1202 if (put_user_s32(host_pipe[0], pipedes)
1203 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1204 return -TARGET_EFAULT;
1205 return get_errno(ret);
1206 }
1207
1208 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1209 abi_ulong target_addr,
1210 socklen_t len)
1211 {
1212 struct target_ip_mreqn *target_smreqn;
1213
1214 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1215 if (!target_smreqn)
1216 return -TARGET_EFAULT;
1217 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1218 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1219 if (len == sizeof(struct target_ip_mreqn))
1220 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1221 unlock_user(target_smreqn, target_addr, 0);
1222
1223 return 0;
1224 }
1225
1226 static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
1227 abi_ulong target_addr,
1228 socklen_t len)
1229 {
1230 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1231 sa_family_t sa_family;
1232 struct target_sockaddr *target_saddr;
1233
1234 if (fd_trans_target_to_host_addr(fd)) {
1235 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1236 }
1237
1238 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1239 if (!target_saddr)
1240 return -TARGET_EFAULT;
1241
1242 sa_family = tswap16(target_saddr->sa_family);
1243
1244 /* Oops. The caller might send a incomplete sun_path; sun_path
1245 * must be terminated by \0 (see the manual page), but
1246 * unfortunately it is quite common to specify sockaddr_un
1247 * length as "strlen(x->sun_path)" while it should be
1248 * "strlen(...) + 1". We'll fix that here if needed.
1249 * Linux kernel has a similar feature.
1250 */
1251
1252 if (sa_family == AF_UNIX) {
1253 if (len < unix_maxlen && len > 0) {
1254 char *cp = (char*)target_saddr;
1255
1256 if ( cp[len-1] && !cp[len] )
1257 len++;
1258 }
1259 if (len > unix_maxlen)
1260 len = unix_maxlen;
1261 }
1262
1263 memcpy(addr, target_saddr, len);
1264 addr->sa_family = sa_family;
1265 if (sa_family == AF_PACKET) {
1266 struct target_sockaddr_ll *lladdr;
1267
1268 lladdr = (struct target_sockaddr_ll *)addr;
1269 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1270 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1271 }
1272 unlock_user(target_saddr, target_addr, 0);
1273
1274 return 0;
1275 }
1276
1277 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1278 struct sockaddr *addr,
1279 socklen_t len)
1280 {
1281 struct target_sockaddr *target_saddr;
1282
1283 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1284 if (!target_saddr)
1285 return -TARGET_EFAULT;
1286 memcpy(target_saddr, addr, len);
1287 target_saddr->sa_family = tswap16(addr->sa_family);
1288 unlock_user(target_saddr, target_addr, len);
1289
1290 return 0;
1291 }
1292
1293 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1294 struct target_msghdr *target_msgh)
1295 {
1296 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1297 abi_long msg_controllen;
1298 abi_ulong target_cmsg_addr;
1299 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1300 socklen_t space = 0;
1301
1302 msg_controllen = tswapal(target_msgh->msg_controllen);
1303 if (msg_controllen < sizeof (struct target_cmsghdr))
1304 goto the_end;
1305 target_cmsg_addr = tswapal(target_msgh->msg_control);
1306 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1307 target_cmsg_start = target_cmsg;
1308 if (!target_cmsg)
1309 return -TARGET_EFAULT;
1310
1311 while (cmsg && target_cmsg) {
1312 void *data = CMSG_DATA(cmsg);
1313 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1314
1315 int len = tswapal(target_cmsg->cmsg_len)
1316 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1317
1318 space += CMSG_SPACE(len);
1319 if (space > msgh->msg_controllen) {
1320 space -= CMSG_SPACE(len);
1321 /* This is a QEMU bug, since we allocated the payload
1322 * area ourselves (unlike overflow in host-to-target
1323 * conversion, which is just the guest giving us a buffer
1324 * that's too small). It can't happen for the payload types
1325 * we currently support; if it becomes an issue in future
1326 * we would need to improve our allocation strategy to
1327 * something more intelligent than "twice the size of the
1328 * target buffer we're reading from".
1329 */
1330 gemu_log("Host cmsg overflow\n");
1331 break;
1332 }
1333
1334 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1335 cmsg->cmsg_level = SOL_SOCKET;
1336 } else {
1337 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1338 }
1339 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1340 cmsg->cmsg_len = CMSG_LEN(len);
1341
1342 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1343 int *fd = (int *)data;
1344 int *target_fd = (int *)target_data;
1345 int i, numfds = len / sizeof(int);
1346
1347 for (i = 0; i < numfds; i++) {
1348 __get_user(fd[i], target_fd + i);
1349 }
1350 } else if (cmsg->cmsg_level == SOL_SOCKET
1351 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1352 struct ucred *cred = (struct ucred *)data;
1353 struct target_ucred *target_cred =
1354 (struct target_ucred *)target_data;
1355
1356 __get_user(cred->pid, &target_cred->pid);
1357 __get_user(cred->uid, &target_cred->uid);
1358 __get_user(cred->gid, &target_cred->gid);
1359 } else {
1360 gemu_log("Unsupported ancillary data: %d/%d\n",
1361 cmsg->cmsg_level, cmsg->cmsg_type);
1362 memcpy(data, target_data, len);
1363 }
1364
1365 cmsg = CMSG_NXTHDR(msgh, cmsg);
1366 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1367 target_cmsg_start);
1368 }
1369 unlock_user(target_cmsg, target_cmsg_addr, 0);
1370 the_end:
1371 msgh->msg_controllen = space;
1372 return 0;
1373 }
1374
1375 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1376 struct msghdr *msgh)
1377 {
1378 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1379 abi_long msg_controllen;
1380 abi_ulong target_cmsg_addr;
1381 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
1382 socklen_t space = 0;
1383
1384 msg_controllen = tswapal(target_msgh->msg_controllen);
1385 if (msg_controllen < sizeof (struct target_cmsghdr))
1386 goto the_end;
1387 target_cmsg_addr = tswapal(target_msgh->msg_control);
1388 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1389 target_cmsg_start = target_cmsg;
1390 if (!target_cmsg)
1391 return -TARGET_EFAULT;
1392
1393 while (cmsg && target_cmsg) {
1394 void *data = CMSG_DATA(cmsg);
1395 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1396
1397 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1398 int tgt_len, tgt_space;
1399
1400 /* We never copy a half-header but may copy half-data;
1401 * this is Linux's behaviour in put_cmsg(). Note that
1402 * truncation here is a guest problem (which we report
1403 * to the guest via the CTRUNC bit), unlike truncation
1404 * in target_to_host_cmsg, which is a QEMU bug.
1405 */
1406 if (msg_controllen < sizeof(struct cmsghdr)) {
1407 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1408 break;
1409 }
1410
1411 if (cmsg->cmsg_level == SOL_SOCKET) {
1412 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1413 } else {
1414 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1415 }
1416 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1417
1418 tgt_len = TARGET_CMSG_LEN(len);
1419
1420 /* Payload types which need a different size of payload on
1421 * the target must adjust tgt_len here.
1422 */
1423 switch (cmsg->cmsg_level) {
1424 case SOL_SOCKET:
1425 switch (cmsg->cmsg_type) {
1426 case SO_TIMESTAMP:
1427 tgt_len = sizeof(struct target_timeval);
1428 break;
1429 default:
1430 break;
1431 }
1432 default:
1433 break;
1434 }
1435
1436 if (msg_controllen < tgt_len) {
1437 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
1438 tgt_len = msg_controllen;
1439 }
1440
1441 /* We must now copy-and-convert len bytes of payload
1442 * into tgt_len bytes of destination space. Bear in mind
1443 * that in both source and destination we may be dealing
1444 * with a truncated value!
1445 */
1446 switch (cmsg->cmsg_level) {
1447 case SOL_SOCKET:
1448 switch (cmsg->cmsg_type) {
1449 case SCM_RIGHTS:
1450 {
1451 int *fd = (int *)data;
1452 int *target_fd = (int *)target_data;
1453 int i, numfds = tgt_len / sizeof(int);
1454
1455 for (i = 0; i < numfds; i++) {
1456 __put_user(fd[i], target_fd + i);
1457 }
1458 break;
1459 }
1460 case SO_TIMESTAMP:
1461 {
1462 struct timeval *tv = (struct timeval *)data;
1463 struct target_timeval *target_tv =
1464 (struct target_timeval *)target_data;
1465
1466 if (len != sizeof(struct timeval) ||
1467 tgt_len != sizeof(struct target_timeval)) {
1468 goto unimplemented;
1469 }
1470
1471 /* copy struct timeval to target */
1472 __put_user(tv->tv_sec, &target_tv->tv_sec);
1473 __put_user(tv->tv_usec, &target_tv->tv_usec);
1474 break;
1475 }
1476 case SCM_CREDENTIALS:
1477 {
1478 struct ucred *cred = (struct ucred *)data;
1479 struct target_ucred *target_cred =
1480 (struct target_ucred *)target_data;
1481
1482 __put_user(cred->pid, &target_cred->pid);
1483 __put_user(cred->uid, &target_cred->uid);
1484 __put_user(cred->gid, &target_cred->gid);
1485 break;
1486 }
1487 default:
1488 goto unimplemented;
1489 }
1490 break;
1491
1492 default:
1493 unimplemented:
1494 gemu_log("Unsupported ancillary data: %d/%d\n",
1495 cmsg->cmsg_level, cmsg->cmsg_type);
1496 memcpy(target_data, data, MIN(len, tgt_len));
1497 if (tgt_len > len) {
1498 memset(target_data + len, 0, tgt_len - len);
1499 }
1500 }
1501
1502 target_cmsg->cmsg_len = tswapal(tgt_len);
1503 tgt_space = TARGET_CMSG_SPACE(len);
1504 if (msg_controllen < tgt_space) {
1505 tgt_space = msg_controllen;
1506 }
1507 msg_controllen -= tgt_space;
1508 space += tgt_space;
1509 cmsg = CMSG_NXTHDR(msgh, cmsg);
1510 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1511 target_cmsg_start);
1512 }
1513 unlock_user(target_cmsg, target_cmsg_addr, space);
1514 the_end:
1515 target_msgh->msg_controllen = tswapal(space);
1516 return 0;
1517 }
1518
1519 /* do_setsockopt() Must return target values and target errnos. */
1520 static abi_long do_setsockopt(int sockfd, int level, int optname,
1521 abi_ulong optval_addr, socklen_t optlen)
1522 {
1523 abi_long ret;
1524 int val;
1525 struct ip_mreqn *ip_mreq;
1526 struct ip_mreq_source *ip_mreq_source;
1527
1528 switch(level) {
1529 case SOL_TCP:
1530 /* TCP options all take an 'int' value. */
1531 if (optlen < sizeof(uint32_t))
1532 return -TARGET_EINVAL;
1533
1534 if (get_user_u32(val, optval_addr))
1535 return -TARGET_EFAULT;
1536 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1537 break;
1538 case SOL_IP:
1539 switch(optname) {
1540 case IP_TOS:
1541 case IP_TTL:
1542 case IP_HDRINCL:
1543 case IP_ROUTER_ALERT:
1544 case IP_RECVOPTS:
1545 case IP_RETOPTS:
1546 case IP_PKTINFO:
1547 case IP_MTU_DISCOVER:
1548 case IP_RECVERR:
1549 case IP_RECVTOS:
1550 #ifdef IP_FREEBIND
1551 case IP_FREEBIND:
1552 #endif
1553 case IP_MULTICAST_TTL:
1554 case IP_MULTICAST_LOOP:
1555 val = 0;
1556 if (optlen >= sizeof(uint32_t)) {
1557 if (get_user_u32(val, optval_addr))
1558 return -TARGET_EFAULT;
1559 } else if (optlen >= 1) {
1560 if (get_user_u8(val, optval_addr))
1561 return -TARGET_EFAULT;
1562 }
1563 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1564 break;
1565 case IP_ADD_MEMBERSHIP:
1566 case IP_DROP_MEMBERSHIP:
1567 if (optlen < sizeof (struct target_ip_mreq) ||
1568 optlen > sizeof (struct target_ip_mreqn))
1569 return -TARGET_EINVAL;
1570
1571 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1572 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1573 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1574 break;
1575
1576 case IP_BLOCK_SOURCE:
1577 case IP_UNBLOCK_SOURCE:
1578 case IP_ADD_SOURCE_MEMBERSHIP:
1579 case IP_DROP_SOURCE_MEMBERSHIP:
1580 if (optlen != sizeof (struct target_ip_mreq_source))
1581 return -TARGET_EINVAL;
1582
1583 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1584 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1585 unlock_user (ip_mreq_source, optval_addr, 0);
1586 break;
1587
1588 default:
1589 goto unimplemented;
1590 }
1591 break;
1592 case SOL_IPV6:
1593 switch (optname) {
1594 case IPV6_MTU_DISCOVER:
1595 case IPV6_MTU:
1596 case IPV6_V6ONLY:
1597 case IPV6_RECVPKTINFO:
1598 val = 0;
1599 if (optlen < sizeof(uint32_t)) {
1600 return -TARGET_EINVAL;
1601 }
1602 if (get_user_u32(val, optval_addr)) {
1603 return -TARGET_EFAULT;
1604 }
1605 ret = get_errno(setsockopt(sockfd, level, optname,
1606 &val, sizeof(val)));
1607 break;
1608 default:
1609 goto unimplemented;
1610 }
1611 break;
1612 case SOL_RAW:
1613 switch (optname) {
1614 case ICMP_FILTER:
1615 /* struct icmp_filter takes an u32 value */
1616 if (optlen < sizeof(uint32_t)) {
1617 return -TARGET_EINVAL;
1618 }
1619
1620 if (get_user_u32(val, optval_addr)) {
1621 return -TARGET_EFAULT;
1622 }
1623 ret = get_errno(setsockopt(sockfd, level, optname,
1624 &val, sizeof(val)));
1625 break;
1626
1627 default:
1628 goto unimplemented;
1629 }
1630 break;
1631 case TARGET_SOL_SOCKET:
1632 switch (optname) {
1633 case TARGET_SO_RCVTIMEO:
1634 {
1635 struct timeval tv;
1636
1637 optname = SO_RCVTIMEO;
1638
1639 set_timeout:
1640 if (optlen != sizeof(struct target_timeval)) {
1641 return -TARGET_EINVAL;
1642 }
1643
1644 if (copy_from_user_timeval(&tv, optval_addr)) {
1645 return -TARGET_EFAULT;
1646 }
1647
1648 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1649 &tv, sizeof(tv)));
1650 return ret;
1651 }
1652 case TARGET_SO_SNDTIMEO:
1653 optname = SO_SNDTIMEO;
1654 goto set_timeout;
1655 case TARGET_SO_ATTACH_FILTER:
1656 {
1657 struct target_sock_fprog *tfprog;
1658 struct target_sock_filter *tfilter;
1659 struct sock_fprog fprog;
1660 struct sock_filter *filter;
1661 int i;
1662
1663 if (optlen != sizeof(*tfprog)) {
1664 return -TARGET_EINVAL;
1665 }
1666 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
1667 return -TARGET_EFAULT;
1668 }
1669 if (!lock_user_struct(VERIFY_READ, tfilter,
1670 tswapal(tfprog->filter), 0)) {
1671 unlock_user_struct(tfprog, optval_addr, 1);
1672 return -TARGET_EFAULT;
1673 }
1674
1675 fprog.len = tswap16(tfprog->len);
1676 filter = g_try_new(struct sock_filter, fprog.len);
1677 if (filter == NULL) {
1678 unlock_user_struct(tfilter, tfprog->filter, 1);
1679 unlock_user_struct(tfprog, optval_addr, 1);
1680 return -TARGET_ENOMEM;
1681 }
1682 for (i = 0; i < fprog.len; i++) {
1683 filter[i].code = tswap16(tfilter[i].code);
1684 filter[i].jt = tfilter[i].jt;
1685 filter[i].jf = tfilter[i].jf;
1686 filter[i].k = tswap32(tfilter[i].k);
1687 }
1688 fprog.filter = filter;
1689
1690 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
1691 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
1692 g_free(filter);
1693
1694 unlock_user_struct(tfilter, tfprog->filter, 1);
1695 unlock_user_struct(tfprog, optval_addr, 1);
1696 return ret;
1697 }
1698 case TARGET_SO_BINDTODEVICE:
1699 {
1700 char *dev_ifname, *addr_ifname;
1701
1702 if (optlen > IFNAMSIZ - 1) {
1703 optlen = IFNAMSIZ - 1;
1704 }
1705 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1706 if (!dev_ifname) {
1707 return -TARGET_EFAULT;
1708 }
1709 optname = SO_BINDTODEVICE;
1710 addr_ifname = alloca(IFNAMSIZ);
1711 memcpy(addr_ifname, dev_ifname, optlen);
1712 addr_ifname[optlen] = 0;
1713 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1714 addr_ifname, optlen));
1715 unlock_user (dev_ifname, optval_addr, 0);
1716 return ret;
1717 }
1718 /* Options with 'int' argument. */
1719 case TARGET_SO_DEBUG:
1720 optname = SO_DEBUG;
1721 break;
1722 case TARGET_SO_REUSEADDR:
1723 optname = SO_REUSEADDR;
1724 break;
1725 case TARGET_SO_TYPE:
1726 optname = SO_TYPE;
1727 break;
1728 case TARGET_SO_ERROR:
1729 optname = SO_ERROR;
1730 break;
1731 case TARGET_SO_DONTROUTE:
1732 optname = SO_DONTROUTE;
1733 break;
1734 case TARGET_SO_BROADCAST:
1735 optname = SO_BROADCAST;
1736 break;
1737 case TARGET_SO_SNDBUF:
1738 optname = SO_SNDBUF;
1739 break;
1740 case TARGET_SO_SNDBUFFORCE:
1741 optname = SO_SNDBUFFORCE;
1742 break;
1743 case TARGET_SO_RCVBUF:
1744 optname = SO_RCVBUF;
1745 break;
1746 case TARGET_SO_RCVBUFFORCE:
1747 optname = SO_RCVBUFFORCE;
1748 break;
1749 case TARGET_SO_KEEPALIVE:
1750 optname = SO_KEEPALIVE;
1751 break;
1752 case TARGET_SO_OOBINLINE:
1753 optname = SO_OOBINLINE;
1754 break;
1755 case TARGET_SO_NO_CHECK:
1756 optname = SO_NO_CHECK;
1757 break;
1758 case TARGET_SO_PRIORITY:
1759 optname = SO_PRIORITY;
1760 break;
1761 #ifdef SO_BSDCOMPAT
1762 case TARGET_SO_BSDCOMPAT:
1763 optname = SO_BSDCOMPAT;
1764 break;
1765 #endif
1766 case TARGET_SO_PASSCRED:
1767 optname = SO_PASSCRED;
1768 break;
1769 case TARGET_SO_PASSSEC:
1770 optname = SO_PASSSEC;
1771 break;
1772 case TARGET_SO_TIMESTAMP:
1773 optname = SO_TIMESTAMP;
1774 break;
1775 case TARGET_SO_RCVLOWAT:
1776 optname = SO_RCVLOWAT;
1777 break;
1778 break;
1779 default:
1780 goto unimplemented;
1781 }
1782 if (optlen < sizeof(uint32_t))
1783 return -TARGET_EINVAL;
1784
1785 if (get_user_u32(val, optval_addr))
1786 return -TARGET_EFAULT;
1787 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1788 break;
1789 default:
1790 unimplemented:
1791 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
1792 ret = -TARGET_ENOPROTOOPT;
1793 }
1794 return ret;
1795 }
1796
1797 /* do_getsockopt() Must return target values and target errnos. */
1798 static abi_long do_getsockopt(int sockfd, int level, int optname,
1799 abi_ulong optval_addr, abi_ulong optlen)
1800 {
1801 abi_long ret;
1802 int len, val;
1803 socklen_t lv;
1804
1805 switch(level) {
1806 case TARGET_SOL_SOCKET:
1807 level = SOL_SOCKET;
1808 switch (optname) {
1809 /* These don't just return a single integer */
1810 case TARGET_SO_LINGER:
1811 case TARGET_SO_RCVTIMEO:
1812 case TARGET_SO_SNDTIMEO:
1813 case TARGET_SO_PEERNAME:
1814 goto unimplemented;
1815 case TARGET_SO_PEERCRED: {
1816 struct ucred cr;
1817 socklen_t crlen;
1818 struct target_ucred *tcr;
1819
1820 if (get_user_u32(len, optlen)) {
1821 return -TARGET_EFAULT;
1822 }
1823 if (len < 0) {
1824 return -TARGET_EINVAL;
1825 }
1826
1827 crlen = sizeof(cr);
1828 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1829 &cr, &crlen));
1830 if (ret < 0) {
1831 return ret;
1832 }
1833 if (len > crlen) {
1834 len = crlen;
1835 }
1836 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1837 return -TARGET_EFAULT;
1838 }
1839 __put_user(cr.pid, &tcr->pid);
1840 __put_user(cr.uid, &tcr->uid);
1841 __put_user(cr.gid, &tcr->gid);
1842 unlock_user_struct(tcr, optval_addr, 1);
1843 if (put_user_u32(len, optlen)) {
1844 return -TARGET_EFAULT;
1845 }
1846 break;
1847 }
1848 /* Options with 'int' argument. */
1849 case TARGET_SO_DEBUG:
1850 optname = SO_DEBUG;
1851 goto int_case;
1852 case TARGET_SO_REUSEADDR:
1853 optname = SO_REUSEADDR;
1854 goto int_case;
1855 case TARGET_SO_TYPE:
1856 optname = SO_TYPE;
1857 goto int_case;
1858 case TARGET_SO_ERROR:
1859 optname = SO_ERROR;
1860 goto int_case;
1861 case TARGET_SO_DONTROUTE:
1862 optname = SO_DONTROUTE;
1863 goto int_case;
1864 case TARGET_SO_BROADCAST:
1865 optname = SO_BROADCAST;
1866 goto int_case;
1867 case TARGET_SO_SNDBUF:
1868 optname = SO_SNDBUF;
1869 goto int_case;
1870 case TARGET_SO_RCVBUF:
1871 optname = SO_RCVBUF;
1872 goto int_case;
1873 case TARGET_SO_KEEPALIVE:
1874 optname = SO_KEEPALIVE;
1875 goto int_case;
1876 case TARGET_SO_OOBINLINE:
1877 optname = SO_OOBINLINE;
1878 goto int_case;
1879 case TARGET_SO_NO_CHECK:
1880 optname = SO_NO_CHECK;
1881 goto int_case;
1882 case TARGET_SO_PRIORITY:
1883 optname = SO_PRIORITY;
1884 goto int_case;
1885 #ifdef SO_BSDCOMPAT
1886 case TARGET_SO_BSDCOMPAT:
1887 optname = SO_BSDCOMPAT;
1888 goto int_case;
1889 #endif
1890 case TARGET_SO_PASSCRED:
1891 optname = SO_PASSCRED;
1892 goto int_case;
1893 case TARGET_SO_TIMESTAMP:
1894 optname = SO_TIMESTAMP;
1895 goto int_case;
1896 case TARGET_SO_RCVLOWAT:
1897 optname = SO_RCVLOWAT;
1898 goto int_case;
1899 case TARGET_SO_ACCEPTCONN:
1900 optname = SO_ACCEPTCONN;
1901 goto int_case;
1902 default:
1903 goto int_case;
1904 }
1905 break;
1906 case SOL_TCP:
1907 /* TCP options all take an 'int' value. */
1908 int_case:
1909 if (get_user_u32(len, optlen))
1910 return -TARGET_EFAULT;
1911 if (len < 0)
1912 return -TARGET_EINVAL;
1913 lv = sizeof(lv);
1914 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1915 if (ret < 0)
1916 return ret;
1917 if (optname == SO_TYPE) {
1918 val = host_to_target_sock_type(val);
1919 }
1920 if (len > lv)
1921 len = lv;
1922 if (len == 4) {
1923 if (put_user_u32(val, optval_addr))
1924 return -TARGET_EFAULT;
1925 } else {
1926 if (put_user_u8(val, optval_addr))
1927 return -TARGET_EFAULT;
1928 }
1929 if (put_user_u32(len, optlen))
1930 return -TARGET_EFAULT;
1931 break;
1932 case SOL_IP:
1933 switch(optname) {
1934 case IP_TOS:
1935 case IP_TTL:
1936 case IP_HDRINCL:
1937 case IP_ROUTER_ALERT:
1938 case IP_RECVOPTS:
1939 case IP_RETOPTS:
1940 case IP_PKTINFO:
1941 case IP_MTU_DISCOVER:
1942 case IP_RECVERR:
1943 case IP_RECVTOS:
1944 #ifdef IP_FREEBIND
1945 case IP_FREEBIND:
1946 #endif
1947 case IP_MULTICAST_TTL:
1948 case IP_MULTICAST_LOOP:
1949 if (get_user_u32(len, optlen))
1950 return -TARGET_EFAULT;
1951 if (len < 0)
1952 return -TARGET_EINVAL;
1953 lv = sizeof(lv);
1954 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1955 if (ret < 0)
1956 return ret;
1957 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1958 len = 1;
1959 if (put_user_u32(len, optlen)
1960 || put_user_u8(val, optval_addr))
1961 return -TARGET_EFAULT;
1962 } else {
1963 if (len > sizeof(int))
1964 len = sizeof(int);
1965 if (put_user_u32(len, optlen)
1966 || put_user_u32(val, optval_addr))
1967 return -TARGET_EFAULT;
1968 }
1969 break;
1970 default:
1971 ret = -TARGET_ENOPROTOOPT;
1972 break;
1973 }
1974 break;
1975 default:
1976 unimplemented:
1977 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1978 level, optname);
1979 ret = -TARGET_EOPNOTSUPP;
1980 break;
1981 }
1982 return ret;
1983 }
1984
1985 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1986 int count, int copy)
1987 {
1988 struct target_iovec *target_vec;
1989 struct iovec *vec;
1990 abi_ulong total_len, max_len;
1991 int i;
1992 int err = 0;
1993 bool bad_address = false;
1994
1995 if (count == 0) {
1996 errno = 0;
1997 return NULL;
1998 }
1999 if (count < 0 || count > IOV_MAX) {
2000 errno = EINVAL;
2001 return NULL;
2002 }
2003
2004 vec = g_try_new0(struct iovec, count);
2005 if (vec == NULL) {
2006 errno = ENOMEM;
2007 return NULL;
2008 }
2009
2010 target_vec = lock_user(VERIFY_READ, target_addr,
2011 count * sizeof(struct target_iovec), 1);
2012 if (target_vec == NULL) {
2013 err = EFAULT;
2014 goto fail2;
2015 }
2016
2017 /* ??? If host page size > target page size, this will result in a
2018 value larger than what we can actually support. */
2019 max_len = 0x7fffffff & TARGET_PAGE_MASK;
2020 total_len = 0;
2021
2022 for (i = 0; i < count; i++) {
2023 abi_ulong base = tswapal(target_vec[i].iov_base);
2024 abi_long len = tswapal(target_vec[i].iov_len);
2025
2026 if (len < 0) {
2027 err = EINVAL;
2028 goto fail;
2029 } else if (len == 0) {
2030 /* Zero length pointer is ignored. */
2031 vec[i].iov_base = 0;
2032 } else {
2033 vec[i].iov_base = lock_user(type, base, len, copy);
2034 /* If the first buffer pointer is bad, this is a fault. But
2035 * subsequent bad buffers will result in a partial write; this
2036 * is realized by filling the vector with null pointers and
2037 * zero lengths. */
2038 if (!vec[i].iov_base) {
2039 if (i == 0) {
2040 err = EFAULT;
2041 goto fail;
2042 } else {
2043 bad_address = true;
2044 }
2045 }
2046 if (bad_address) {
2047 len = 0;
2048 }
2049 if (len > max_len - total_len) {
2050 len = max_len - total_len;
2051 }
2052 }
2053 vec[i].iov_len = len;
2054 total_len += len;
2055 }
2056
2057 unlock_user(target_vec, target_addr, 0);
2058 return vec;
2059
2060 fail:
2061 while (--i >= 0) {
2062 if (tswapal(target_vec[i].iov_len) > 0) {
2063 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
2064 }
2065 }
2066 unlock_user(target_vec, target_addr, 0);
2067 fail2:
2068 g_free(vec);
2069 errno = err;
2070 return NULL;
2071 }
2072
2073 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
2074 int count, int copy)
2075 {
2076 struct target_iovec *target_vec;
2077 int i;
2078
2079 target_vec = lock_user(VERIFY_READ, target_addr,
2080 count * sizeof(struct target_iovec), 1);
2081 if (target_vec) {
2082 for (i = 0; i < count; i++) {
2083 abi_ulong base = tswapal(target_vec[i].iov_base);
2084 abi_long len = tswapal(target_vec[i].iov_len);
2085 if (len < 0) {
2086 break;
2087 }
2088 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
2089 }
2090 unlock_user(target_vec, target_addr, 0);
2091 }
2092
2093 g_free(vec);
2094 }
2095
2096 static inline int target_to_host_sock_type(int *type)
2097 {
2098 int host_type = 0;
2099 int target_type = *type;
2100
2101 switch (target_type & TARGET_SOCK_TYPE_MASK) {
2102 case TARGET_SOCK_DGRAM:
2103 host_type = SOCK_DGRAM;
2104 break;
2105 case TARGET_SOCK_STREAM:
2106 host_type = SOCK_STREAM;
2107 break;
2108 default:
2109 host_type = target_type & TARGET_SOCK_TYPE_MASK;
2110 break;
2111 }
2112 if (target_type & TARGET_SOCK_CLOEXEC) {
2113 #if defined(SOCK_CLOEXEC)
2114 host_type |= SOCK_CLOEXEC;
2115 #else
2116 return -TARGET_EINVAL;
2117 #endif
2118 }
2119 if (target_type & TARGET_SOCK_NONBLOCK) {
2120 #if defined(SOCK_NONBLOCK)
2121 host_type |= SOCK_NONBLOCK;
2122 #elif !defined(O_NONBLOCK)
2123 return -TARGET_EINVAL;
2124 #endif
2125 }
2126 *type = host_type;
2127 return 0;
2128 }
2129
2130 /* Try to emulate socket type flags after socket creation. */
2131 static int sock_flags_fixup(int fd, int target_type)
2132 {
2133 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2134 if (target_type & TARGET_SOCK_NONBLOCK) {
2135 int flags = fcntl(fd, F_GETFL);
2136 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
2137 close(fd);
2138 return -TARGET_EINVAL;
2139 }
2140 }
2141 #endif
2142 return fd;
2143 }
2144
2145 static abi_long packet_target_to_host_sockaddr(void *host_addr,
2146 abi_ulong target_addr,
2147 socklen_t len)
2148 {
2149 struct sockaddr *addr = host_addr;
2150 struct target_sockaddr *target_saddr;
2151
2152 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
2153 if (!target_saddr) {
2154 return -TARGET_EFAULT;
2155 }
2156
2157 memcpy(addr, target_saddr, len);
2158 addr->sa_family = tswap16(target_saddr->sa_family);
2159 /* spkt_protocol is big-endian */
2160
2161 unlock_user(target_saddr, target_addr, 0);
2162 return 0;
2163 }
2164
2165 static TargetFdTrans target_packet_trans = {
2166 .target_to_host_addr = packet_target_to_host_sockaddr,
2167 };
2168
2169 /* do_socket() Must return target values and target errnos. */
2170 static abi_long do_socket(int domain, int type, int protocol)
2171 {
2172 int target_type = type;
2173 int ret;
2174
2175 ret = target_to_host_sock_type(&type);
2176 if (ret) {
2177 return ret;
2178 }
2179
2180 if (domain == PF_NETLINK)
2181 return -TARGET_EAFNOSUPPORT;
2182
2183 if (domain == AF_PACKET ||
2184 (domain == AF_INET && type == SOCK_PACKET)) {
2185 protocol = tswap16(protocol);
2186 }
2187
2188 ret = get_errno(socket(domain, type, protocol));
2189 if (ret >= 0) {
2190 ret = sock_flags_fixup(ret, target_type);
2191 if (type == SOCK_PACKET) {
2192 /* Manage an obsolete case :
2193 * if socket type is SOCK_PACKET, bind by name
2194 */
2195 fd_trans_register(ret, &target_packet_trans);
2196 }
2197 }
2198 return ret;
2199 }
2200
2201 /* do_bind() Must return target values and target errnos. */
2202 static abi_long do_bind(int sockfd, abi_ulong target_addr,
2203 socklen_t addrlen)
2204 {
2205 void *addr;
2206 abi_long ret;
2207
2208 if ((int)addrlen < 0) {
2209 return -TARGET_EINVAL;
2210 }
2211
2212 addr = alloca(addrlen+1);
2213
2214 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
2215 if (ret)
2216 return ret;
2217
2218 return get_errno(bind(sockfd, addr, addrlen));
2219 }
2220
2221 /* do_connect() Must return target values and target errnos. */
2222 static abi_long do_connect(int sockfd, abi_ulong target_addr,
2223 socklen_t addrlen)
2224 {
2225 void *addr;
2226 abi_long ret;
2227
2228 if ((int)addrlen < 0) {
2229 return -TARGET_EINVAL;
2230 }
2231
2232 addr = alloca(addrlen+1);
2233
2234 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
2235 if (ret)
2236 return ret;
2237
2238 return get_errno(connect(sockfd, addr, addrlen));
2239 }
2240
2241 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
2242 static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
2243 int flags, int send)
2244 {
2245 abi_long ret, len;
2246 struct msghdr msg;
2247 int count;
2248 struct iovec *vec;
2249 abi_ulong target_vec;
2250
2251 if (msgp->msg_name) {
2252 msg.msg_namelen = tswap32(msgp->msg_namelen);
2253 msg.msg_name = alloca(msg.msg_namelen+1);
2254 ret = target_to_host_sockaddr(fd, msg.msg_name,
2255 tswapal(msgp->msg_name),
2256 msg.msg_namelen);
2257 if (ret) {
2258 goto out2;
2259 }
2260 } else {
2261 msg.msg_name = NULL;
2262 msg.msg_namelen = 0;
2263 }
2264 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
2265 msg.msg_control = alloca(msg.msg_controllen);
2266 msg.msg_flags = tswap32(msgp->msg_flags);
2267
2268 count = tswapal(msgp->msg_iovlen);
2269 target_vec = tswapal(msgp->msg_iov);
2270 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
2271 target_vec, count, send);
2272 if (vec == NULL) {
2273 ret = -host_to_target_errno(errno);
2274 goto out2;
2275 }
2276 msg.msg_iovlen = count;
2277 msg.msg_iov = vec;
2278
2279 if (send) {
2280 ret = target_to_host_cmsg(&msg, msgp);
2281 if (ret == 0)
2282 ret = get_errno(sendmsg(fd, &msg, flags));
2283 } else {
2284 ret = get_errno(recvmsg(fd, &msg, flags));
2285 if (!is_error(ret)) {
2286 len = ret;
2287 ret = host_to_target_cmsg(msgp, &msg);
2288 if (!is_error(ret)) {
2289 msgp->msg_namelen = tswap32(msg.msg_namelen);
2290 if (msg.msg_name != NULL) {
2291 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
2292 msg.msg_name, msg.msg_namelen);
2293 if (ret) {
2294 goto out;
2295 }
2296 }
2297
2298 ret = len;
2299 }
2300 }
2301 }
2302
2303 out:
2304 unlock_iovec(vec, target_vec, count, !send);
2305 out2:
2306 return ret;
2307 }
2308
2309 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
2310 int flags, int send)
2311 {
2312 abi_long ret;
2313 struct target_msghdr *msgp;
2314
2315 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
2316 msgp,
2317 target_msg,
2318 send ? 1 : 0)) {
2319 return -TARGET_EFAULT;
2320 }
2321 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
2322 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
2323 return ret;
2324 }
2325
2326 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
2327 * so it might not have this *mmsg-specific flag either.
2328 */
2329 #ifndef MSG_WAITFORONE
2330 #define MSG_WAITFORONE 0x10000
2331 #endif
2332
2333 static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
2334 unsigned int vlen, unsigned int flags,
2335 int send)
2336 {
2337 struct target_mmsghdr *mmsgp;
2338 abi_long ret = 0;
2339 int i;
2340
2341 if (vlen > UIO_MAXIOV) {
2342 vlen = UIO_MAXIOV;
2343 }
2344
2345 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
2346 if (!mmsgp) {
2347 return -TARGET_EFAULT;
2348 }
2349
2350 for (i = 0; i < vlen; i++) {
2351 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
2352 if (is_error(ret)) {
2353 break;
2354 }
2355 mmsgp[i].msg_len = tswap32(ret);
2356 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2357 if (flags & MSG_WAITFORONE) {
2358 flags |= MSG_DONTWAIT;
2359 }
2360 }
2361
2362 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
2363
2364 /* Return number of datagrams sent if we sent any at all;
2365 * otherwise return the error.
2366 */
2367 if (i) {
2368 return i;
2369 }
2370 return ret;
2371 }
2372
2373 /* If we don't have a system accept4() then just call accept.
2374 * The callsites to do_accept4() will ensure that they don't
2375 * pass a non-zero flags argument in this config.
2376 */
2377 #ifndef CONFIG_ACCEPT4
2378 static inline int accept4(int sockfd, struct sockaddr *addr,
2379 socklen_t *addrlen, int flags)
2380 {
2381 assert(flags == 0);
2382 return accept(sockfd, addr, addrlen);
2383 }
2384 #endif
2385
2386 /* do_accept4() Must return target values and target errnos. */
2387 static abi_long do_accept4(int fd, abi_ulong target_addr,
2388 abi_ulong target_addrlen_addr, int flags)
2389 {
2390 socklen_t addrlen;
2391 void *addr;
2392 abi_long ret;
2393 int host_flags;
2394
2395 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
2396
2397 if (target_addr == 0) {
2398 return get_errno(accept4(fd, NULL, NULL, host_flags));
2399 }
2400
2401 /* linux returns EINVAL if addrlen pointer is invalid */
2402 if (get_user_u32(addrlen, target_addrlen_addr))
2403 return -TARGET_EINVAL;
2404
2405 if ((int)addrlen < 0) {
2406 return -TARGET_EINVAL;
2407 }
2408
2409 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2410 return -TARGET_EINVAL;
2411
2412 addr = alloca(addrlen);
2413
2414 ret = get_errno(accept4(fd, addr, &addrlen, host_flags));
2415 if (!is_error(ret)) {
2416 host_to_target_sockaddr(target_addr, addr, addrlen);
2417 if (put_user_u32(addrlen, target_addrlen_addr))
2418 ret = -TARGET_EFAULT;
2419 }
2420 return ret;
2421 }
2422
2423 /* do_getpeername() Must return target values and target errnos. */
2424 static abi_long do_getpeername(int fd, abi_ulong target_addr,
2425 abi_ulong target_addrlen_addr)
2426 {
2427 socklen_t addrlen;
2428 void *addr;
2429 abi_long ret;
2430
2431 if (get_user_u32(addrlen, target_addrlen_addr))
2432 return -TARGET_EFAULT;
2433
2434 if ((int)addrlen < 0) {
2435 return -TARGET_EINVAL;
2436 }
2437
2438 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2439 return -TARGET_EFAULT;
2440
2441 addr = alloca(addrlen);
2442
2443 ret = get_errno(getpeername(fd, addr, &addrlen));
2444 if (!is_error(ret)) {
2445 host_to_target_sockaddr(target_addr, addr, addrlen);
2446 if (put_user_u32(addrlen, target_addrlen_addr))
2447 ret = -TARGET_EFAULT;
2448 }
2449 return ret;
2450 }
2451
2452 /* do_getsockname() Must return target values and target errnos. */
2453 static abi_long do_getsockname(int fd, abi_ulong target_addr,
2454 abi_ulong target_addrlen_addr)
2455 {
2456 socklen_t addrlen;
2457 void *addr;
2458 abi_long ret;
2459
2460 if (get_user_u32(addrlen, target_addrlen_addr))
2461 return -TARGET_EFAULT;
2462
2463 if ((int)addrlen < 0) {
2464 return -TARGET_EINVAL;
2465 }
2466
2467 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2468 return -TARGET_EFAULT;
2469
2470 addr = alloca(addrlen);
2471
2472 ret = get_errno(getsockname(fd, addr, &addrlen));
2473 if (!is_error(ret)) {
2474 host_to_target_sockaddr(target_addr, addr, addrlen);
2475 if (put_user_u32(addrlen, target_addrlen_addr))
2476 ret = -TARGET_EFAULT;
2477 }
2478 return ret;
2479 }
2480
2481 /* do_socketpair() Must return target values and target errnos. */
2482 static abi_long do_socketpair(int domain, int type, int protocol,
2483 abi_ulong target_tab_addr)
2484 {
2485 int tab[2];
2486 abi_long ret;
2487
2488 target_to_host_sock_type(&type);
2489
2490 ret = get_errno(socketpair(domain, type, protocol, tab));
2491 if (!is_error(ret)) {
2492 if (put_user_s32(tab[0], target_tab_addr)
2493 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2494 ret = -TARGET_EFAULT;
2495 }
2496 return ret;
2497 }
2498
2499 /* do_sendto() Must return target values and target errnos. */
2500 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2501 abi_ulong target_addr, socklen_t addrlen)
2502 {
2503 void *addr;
2504 void *host_msg;
2505 abi_long ret;
2506
2507 if ((int)addrlen < 0) {
2508 return -TARGET_EINVAL;
2509 }
2510
2511 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2512 if (!host_msg)
2513 return -TARGET_EFAULT;
2514 if (target_addr) {
2515 addr = alloca(addrlen+1);
2516 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
2517 if (ret) {
2518 unlock_user(host_msg, msg, 0);
2519 return ret;
2520 }
2521 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2522 } else {
2523 ret = get_errno(send(fd, host_msg, len, flags));
2524 }
2525 unlock_user(host_msg, msg, 0);
2526 return ret;
2527 }
2528
2529 /* do_recvfrom() Must return target values and target errnos. */
2530 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2531 abi_ulong target_addr,
2532 abi_ulong target_addrlen)
2533 {
2534 socklen_t addrlen;
2535 void *addr;
2536 void *host_msg;
2537 abi_long ret;
2538
2539 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2540 if (!host_msg)
2541 return -TARGET_EFAULT;
2542 if (target_addr) {
2543 if (get_user_u32(addrlen, target_addrlen)) {
2544 ret = -TARGET_EFAULT;
2545 goto fail;
2546 }
2547 if ((int)addrlen < 0) {
2548 ret = -TARGET_EINVAL;
2549 goto fail;
2550 }
2551 addr = alloca(addrlen);
2552 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2553 } else {
2554 addr = NULL; /* To keep compiler quiet. */
2555 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
2556 }
2557 if (!is_error(ret)) {
2558 if (target_addr) {
2559 host_to_target_sockaddr(target_addr, addr, addrlen);
2560 if (put_user_u32(addrlen, target_addrlen)) {
2561 ret = -TARGET_EFAULT;
2562 goto fail;
2563 }
2564 }
2565 unlock_user(host_msg, msg, len);
2566 } else {
2567 fail:
2568 unlock_user(host_msg, msg, 0);
2569 }
2570 return ret;
2571 }
2572
2573 #ifdef TARGET_NR_socketcall
2574 /* do_socketcall() Must return target values and target errnos. */
2575 static abi_long do_socketcall(int num, abi_ulong vptr)
2576 {
2577 static const unsigned ac[] = { /* number of arguments per call */
2578 [SOCKOP_socket] = 3, /* domain, type, protocol */
2579 [SOCKOP_bind] = 3, /* sockfd, addr, addrlen */
2580 [SOCKOP_connect] = 3, /* sockfd, addr, addrlen */
2581 [SOCKOP_listen] = 2, /* sockfd, backlog */
2582 [SOCKOP_accept] = 3, /* sockfd, addr, addrlen */
2583 [SOCKOP_accept4] = 4, /* sockfd, addr, addrlen, flags */
2584 [SOCKOP_getsockname] = 3, /* sockfd, addr, addrlen */
2585 [SOCKOP_getpeername] = 3, /* sockfd, addr, addrlen */
2586 [SOCKOP_socketpair] = 4, /* domain, type, protocol, tab */
2587 [SOCKOP_send] = 4, /* sockfd, msg, len, flags */
2588 [SOCKOP_recv] = 4, /* sockfd, msg, len, flags */
2589 [SOCKOP_sendto] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2590 [SOCKOP_recvfrom] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2591 [SOCKOP_shutdown] = 2, /* sockfd, how */
2592 [SOCKOP_sendmsg] = 3, /* sockfd, msg, flags */
2593 [SOCKOP_recvmsg] = 3, /* sockfd, msg, flags */
2594 [SOCKOP_sendmmsg] = 4, /* sockfd, msgvec, vlen, flags */
2595 [SOCKOP_recvmmsg] = 4, /* sockfd, msgvec, vlen, flags */
2596 [SOCKOP_setsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2597 [SOCKOP_getsockopt] = 5, /* sockfd, level, optname, optval, optlen */
2598 };
2599 abi_long a[6]; /* max 6 args */
2600
2601 /* first, collect the arguments in a[] according to ac[] */
2602 if (num >= 0 && num < ARRAY_SIZE(ac)) {
2603 unsigned i;
2604 assert(ARRAY_SIZE(a) >= ac[num]); /* ensure we have space for args */
2605 for (i = 0; i < ac[num]; ++i) {
2606 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
2607 return -TARGET_EFAULT;
2608 }
2609 }
2610 }
2611
2612 /* now when we have the args, actually handle the call */
2613 switch (num) {
2614 case SOCKOP_socket: /* domain, type, protocol */
2615 return do_socket(a[0], a[1], a[2]);
2616 case SOCKOP_bind: /* sockfd, addr, addrlen */
2617 return do_bind(a[0], a[1], a[2]);
2618 case SOCKOP_connect: /* sockfd, addr, addrlen */
2619 return do_connect(a[0], a[1], a[2]);
2620 case SOCKOP_listen: /* sockfd, backlog */
2621 return get_errno(listen(a[0], a[1]));
2622 case SOCKOP_accept: /* sockfd, addr, addrlen */
2623 return do_accept4(a[0], a[1], a[2], 0);
2624 case SOCKOP_accept4: /* sockfd, addr, addrlen, flags */
2625 return do_accept4(a[0], a[1], a[2], a[3]);
2626 case SOCKOP_getsockname: /* sockfd, addr, addrlen */
2627 return do_getsockname(a[0], a[1], a[2]);
2628 case SOCKOP_getpeername: /* sockfd, addr, addrlen */
2629 return do_getpeername(a[0], a[1], a[2]);
2630 case SOCKOP_socketpair: /* domain, type, protocol, tab */
2631 return do_socketpair(a[0], a[1], a[2], a[3]);
2632 case SOCKOP_send: /* sockfd, msg, len, flags */
2633 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
2634 case SOCKOP_recv: /* sockfd, msg, len, flags */
2635 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
2636 case SOCKOP_sendto: /* sockfd, msg, len, flags, addr, addrlen */
2637 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
2638 case SOCKOP_recvfrom: /* sockfd, msg, len, flags, addr, addrlen */
2639 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
2640 case SOCKOP_shutdown: /* sockfd, how */
2641 return get_errno(shutdown(a[0], a[1]));
2642 case SOCKOP_sendmsg: /* sockfd, msg, flags */
2643 return do_sendrecvmsg(a[0], a[1], a[2], 1);
2644 case SOCKOP_recvmsg: /* sockfd, msg, flags */
2645 return do_sendrecvmsg(a[0], a[1], a[2], 0);
2646 case SOCKOP_sendmmsg: /* sockfd, msgvec, vlen, flags */
2647 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
2648 case SOCKOP_recvmmsg: /* sockfd, msgvec, vlen, flags */
2649 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
2650 case SOCKOP_setsockopt: /* sockfd, level, optname, optval, optlen */
2651 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
2652 case SOCKOP_getsockopt: /* sockfd, level, optname, optval, optlen */
2653 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
2654 default:
2655 gemu_log("Unsupported socketcall: %d\n", num);
2656 return -TARGET_ENOSYS;
2657 }
2658 }
2659 #endif
2660
2661 #define N_SHM_REGIONS 32
2662
2663 static struct shm_region {
2664 abi_ulong start;
2665 abi_ulong size;
2666 bool in_use;
2667 } shm_regions[N_SHM_REGIONS];
2668
2669 struct target_semid_ds
2670 {
2671 struct target_ipc_perm sem_perm;
2672 abi_ulong sem_otime;
2673 #if !defined(TARGET_PPC64)
2674 abi_ulong __unused1;
2675 #endif
2676 abi_ulong sem_ctime;
2677 #if !defined(TARGET_PPC64)
2678 abi_ulong __unused2;
2679 #endif
2680 abi_ulong sem_nsems;
2681 abi_ulong __unused3;
2682 abi_ulong __unused4;
2683 };
2684
2685 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2686 abi_ulong target_addr)
2687 {
2688 struct target_ipc_perm *target_ip;
2689 struct target_semid_ds *target_sd;
2690
2691 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2692 return -TARGET_EFAULT;
2693 target_ip = &(target_sd->sem_perm);
2694 host_ip->__key = tswap32(target_ip->__key);
2695 host_ip->uid = tswap32(target_ip->uid);
2696 host_ip->gid = tswap32(target_ip->gid);
2697 host_ip->cuid = tswap32(target_ip->cuid);
2698 host_ip->cgid = tswap32(target_ip->cgid);
2699 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2700 host_ip->mode = tswap32(target_ip->mode);
2701 #else
2702 host_ip->mode = tswap16(target_ip->mode);
2703 #endif
2704 #if defined(TARGET_PPC)
2705 host_ip->__seq = tswap32(target_ip->__seq);
2706 #else
2707 host_ip->__seq = tswap16(target_ip->__seq);
2708 #endif
2709 unlock_user_struct(target_sd, target_addr, 0);
2710 return 0;
2711 }
2712
2713 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2714 struct ipc_perm *host_ip)
2715 {
2716 struct target_ipc_perm *target_ip;
2717 struct target_semid_ds *target_sd;
2718
2719 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2720 return -TARGET_EFAULT;
2721 target_ip = &(target_sd->sem_perm);
2722 target_ip->__key = tswap32(host_ip->__key);
2723 target_ip->uid = tswap32(host_ip->uid);
2724 target_ip->gid = tswap32(host_ip->gid);
2725 target_ip->cuid = tswap32(host_ip->cuid);
2726 target_ip->cgid = tswap32(host_ip->cgid);
2727 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2728 target_ip->mode = tswap32(host_ip->mode);
2729 #else
2730 target_ip->mode = tswap16(host_ip->mode);
2731 #endif
2732 #if defined(TARGET_PPC)
2733 target_ip->__seq = tswap32(host_ip->__seq);
2734 #else
2735 target_ip->__seq = tswap16(host_ip->__seq);
2736 #endif
2737 unlock_user_struct(target_sd, target_addr, 1);
2738 return 0;
2739 }
2740
2741 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2742 abi_ulong target_addr)
2743 {
2744 struct target_semid_ds *target_sd;
2745
2746 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2747 return -TARGET_EFAULT;
2748 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2749 return -TARGET_EFAULT;
2750 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2751 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2752 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
2753 unlock_user_struct(target_sd, target_addr, 0);
2754 return 0;
2755 }
2756
2757 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2758 struct semid_ds *host_sd)
2759 {
2760 struct target_semid_ds *target_sd;
2761
2762 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2763 return -TARGET_EFAULT;
2764 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2765 return -TARGET_EFAULT;
2766 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2767 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2768 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
2769 unlock_user_struct(target_sd, target_addr, 1);
2770 return 0;
2771 }
2772
2773 struct target_seminfo {
2774 int semmap;
2775 int semmni;
2776 int semmns;
2777 int semmnu;
2778 int semmsl;
2779 int semopm;
2780 int semume;
2781 int semusz;
2782 int semvmx;
2783 int semaem;
2784 };
2785
2786 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2787 struct seminfo *host_seminfo)
2788 {
2789 struct target_seminfo *target_seminfo;
2790 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2791 return -TARGET_EFAULT;
2792 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2793 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2794 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2795 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2796 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2797 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2798 __put_user(host_seminfo->semume, &target_seminfo->semume);
2799 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2800 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2801 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2802 unlock_user_struct(target_seminfo, target_addr, 1);
2803 return 0;
2804 }
2805
2806 union semun {
2807 int val;
2808 struct semid_ds *buf;
2809 unsigned short *array;
2810 struct seminfo *__buf;
2811 };
2812
2813 union target_semun {
2814 int val;
2815 abi_ulong buf;
2816 abi_ulong array;
2817 abi_ulong __buf;
2818 };
2819
2820 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2821 abi_ulong target_addr)
2822 {
2823 int nsems;
2824 unsigned short *array;
2825 union semun semun;
2826 struct semid_ds semid_ds;
2827 int i, ret;
2828
2829 semun.buf = &semid_ds;
2830
2831 ret = semctl(semid, 0, IPC_STAT, semun);
2832 if (ret == -1)
2833 return get_errno(ret);
2834
2835 nsems = semid_ds.sem_nsems;
2836
2837 *host_array = g_try_new(unsigned short, nsems);
2838 if (!*host_array) {
2839 return -TARGET_ENOMEM;
2840 }
2841 array = lock_user(VERIFY_READ, target_addr,
2842 nsems*sizeof(unsigned short), 1);
2843 if (!array) {
2844 g_free(*host_array);
2845 return -TARGET_EFAULT;
2846 }
2847
2848 for(i=0; i<nsems; i++) {
2849 __get_user((*host_array)[i], &array[i]);
2850 }
2851 unlock_user(array, target_addr, 0);
2852
2853 return 0;
2854 }
2855
2856 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2857 unsigned short **host_array)
2858 {
2859 int nsems;
2860 unsigned short *array;
2861 union semun semun;
2862 struct semid_ds semid_ds;
2863 int i, ret;
2864
2865 semun.buf = &semid_ds;
2866
2867 ret = semctl(semid, 0, IPC_STAT, semun);
2868 if (ret == -1)
2869 return get_errno(ret);
2870
2871 nsems = semid_ds.sem_nsems;
2872
2873 array = lock_user(VERIFY_WRITE, target_addr,
2874 nsems*sizeof(unsigned short), 0);
2875 if (!array)
2876 return -TARGET_EFAULT;
2877
2878 for(i=0; i<nsems; i++) {
2879 __put_user((*host_array)[i], &array[i]);
2880 }
2881 g_free(*host_array);
2882 unlock_user(array, target_addr, 1);
2883
2884 return 0;
2885 }
2886
2887 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2888 abi_ulong target_arg)
2889 {
2890 union target_semun target_su = { .buf = target_arg };
2891 union semun arg;
2892 struct semid_ds dsarg;
2893 unsigned short *array = NULL;
2894 struct seminfo seminfo;
2895 abi_long ret = -TARGET_EINVAL;
2896 abi_long err;
2897 cmd &= 0xff;
2898
2899 switch( cmd ) {
2900 case GETVAL:
2901 case SETVAL:
2902 /* In 64 bit cross-endian situations, we will erroneously pick up
2903 * the wrong half of the union for the "val" element. To rectify
2904 * this, the entire 8-byte structure is byteswapped, followed by
2905 * a swap of the 4 byte val field. In other cases, the data is
2906 * already in proper host byte order. */
2907 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
2908 target_su.buf = tswapal(target_su.buf);
2909 arg.val = tswap32(target_su.val);
2910 } else {
2911 arg.val = target_su.val;
2912 }
2913 ret = get_errno(semctl(semid, semnum, cmd, arg));
2914 break;
2915 case GETALL:
2916 case SETALL:
2917 err = target_to_host_semarray(semid, &array, target_su.array);
2918 if (err)
2919 return err;
2920 arg.array = array;
2921 ret = get_errno(semctl(semid, semnum, cmd, arg));
2922 err = host_to_target_semarray(semid, target_su.array, &array);
2923 if (err)
2924 return err;
2925 break;
2926 case IPC_STAT:
2927 case IPC_SET:
2928 case SEM_STAT:
2929 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2930 if (err)
2931 return err;
2932 arg.buf = &dsarg;
2933 ret = get_errno(semctl(semid, semnum, cmd, arg));
2934 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2935 if (err)
2936 return err;
2937 break;
2938 case IPC_INFO:
2939 case SEM_INFO:
2940 arg.__buf = &seminfo;
2941 ret = get_errno(semctl(semid, semnum, cmd, arg));
2942 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2943 if (err)
2944 return err;
2945 break;
2946 case IPC_RMID:
2947 case GETPID:
2948 case GETNCNT:
2949 case GETZCNT:
2950 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2951 break;
2952 }
2953
2954 return ret;
2955 }
2956
2957 struct target_sembuf {
2958 unsigned short sem_num;
2959 short sem_op;
2960 short sem_flg;
2961 };
2962
2963 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2964 abi_ulong target_addr,
2965 unsigned nsops)
2966 {
2967 struct target_sembuf *target_sembuf;
2968 int i;
2969
2970 target_sembuf = lock_user(VERIFY_READ, target_addr,
2971 nsops*sizeof(struct target_sembuf), 1);
2972 if (!target_sembuf)
2973 return -TARGET_EFAULT;
2974
2975 for(i=0; i<nsops; i++) {
2976 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2977 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2978 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2979 }
2980
2981 unlock_user(target_sembuf, target_addr, 0);
2982
2983 return 0;
2984 }
2985
2986 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2987 {
2988 struct sembuf sops[nsops];
2989
2990 if (target_to_host_sembuf(sops, ptr, nsops))
2991 return -TARGET_EFAULT;
2992
2993 return get_errno(semop(semid, sops, nsops));
2994 }
2995
2996 struct target_msqid_ds
2997 {
2998 struct target_ipc_perm msg_perm;
2999 abi_ulong msg_stime;
3000 #if TARGET_ABI_BITS == 32
3001 abi_ulong __unused1;
3002 #endif
3003 abi_ulong msg_rtime;
3004 #if TARGET_ABI_BITS == 32
3005 abi_ulong __unused2;
3006 #endif
3007 abi_ulong msg_ctime;
3008 #if TARGET_ABI_BITS == 32
3009 abi_ulong __unused3;
3010 #endif
3011 abi_ulong __msg_cbytes;
3012 abi_ulong msg_qnum;
3013 abi_ulong msg_qbytes;
3014 abi_ulong msg_lspid;
3015 abi_ulong msg_lrpid;
3016 abi_ulong __unused4;
3017 abi_ulong __unused5;
3018 };
3019
3020 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
3021 abi_ulong target_addr)
3022 {
3023 struct target_msqid_ds *target_md;
3024
3025 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
3026 return -TARGET_EFAULT;
3027 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
3028 return -TARGET_EFAULT;
3029 host_md->msg_stime = tswapal(target_md->msg_stime);
3030 host_md->msg_rtime = tswapal(target_md->msg_rtime);
3031 host_md->msg_ctime = tswapal(target_md->msg_ctime);
3032 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
3033 host_md->msg_qnum = tswapal(target_md->msg_qnum);
3034 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
3035 host_md->msg_lspid = tswapal(target_md->msg_lspid);
3036 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
3037 unlock_user_struct(target_md, target_addr, 0);
3038 return 0;
3039 }
3040
3041 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
3042 struct msqid_ds *host_md)
3043 {
3044 struct target_msqid_ds *target_md;
3045
3046 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
3047 return -TARGET_EFAULT;
3048 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
3049 return -TARGET_EFAULT;
3050 target_md->msg_stime = tswapal(host_md->msg_stime);
3051 target_md->msg_rtime = tswapal(host_md->msg_rtime);
3052 target_md->msg_ctime = tswapal(host_md->msg_ctime);
3053 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
3054 target_md->msg_qnum = tswapal(host_md->msg_qnum);
3055 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
3056 target_md->msg_lspid = tswapal(host_md->msg_lspid);
3057 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
3058 unlock_user_struct(target_md, target_addr, 1);
3059 return 0;
3060 }
3061
3062 struct target_msginfo {
3063 int msgpool;
3064 int msgmap;
3065 int msgmax;
3066 int msgmnb;
3067 int msgmni;
3068 int msgssz;
3069 int msgtql;
3070 unsigned short int msgseg;
3071 };
3072
3073 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
3074 struct msginfo *host_msginfo)
3075 {
3076 struct target_msginfo *target_msginfo;
3077 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
3078 return -TARGET_EFAULT;
3079 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
3080 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
3081 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
3082 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
3083 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
3084 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
3085 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
3086 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
3087 unlock_user_struct(target_msginfo, target_addr, 1);
3088 return 0;
3089 }
3090
3091 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
3092 {
3093 struct msqid_ds dsarg;
3094 struct msginfo msginfo;
3095 abi_long ret = -TARGET_EINVAL;
3096
3097 cmd &= 0xff;
3098
3099 switch (cmd) {
3100 case IPC_STAT:
3101 case IPC_SET:
3102 case MSG_STAT:
3103 if (target_to_host_msqid_ds(&dsarg,ptr))
3104 return -TARGET_EFAULT;
3105 ret = get_errno(msgctl(msgid, cmd, &dsarg));
3106 if (host_to_target_msqid_ds(ptr,&dsarg))
3107 return -TARGET_EFAULT;
3108 break;
3109 case IPC_RMID:
3110 ret = get_errno(msgctl(msgid, cmd, NULL));
3111 break;
3112 case IPC_INFO:
3113 case MSG_INFO:
3114 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
3115 if (host_to_target_msginfo(ptr, &msginfo))
3116 return -TARGET_EFAULT;
3117 break;
3118 }
3119
3120 return ret;
3121 }
3122
3123 struct target_msgbuf {
3124 abi_long mtype;
3125 char mtext[1];
3126 };
3127
3128 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
3129 ssize_t msgsz, int msgflg)
3130 {
3131 struct target_msgbuf *target_mb;
3132 struct msgbuf *host_mb;
3133 abi_long ret = 0;
3134
3135 if (msgsz < 0) {
3136 return -TARGET_EINVAL;
3137 }
3138
3139 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
3140 return -TARGET_EFAULT;
3141 host_mb = g_try_malloc(msgsz + sizeof(long));
3142 if (!host_mb) {
3143 unlock_user_struct(target_mb, msgp, 0);
3144 return -TARGET_ENOMEM;
3145 }
3146 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
3147 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
3148 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
3149 g_free(host_mb);
3150 unlock_user_struct(target_mb, msgp, 0);
3151
3152 return ret;
3153 }
3154
3155 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
3156 unsigned int msgsz, abi_long msgtyp,
3157 int msgflg)
3158 {
3159 struct target_msgbuf *target_mb;
3160 char *target_mtext;
3161 struct msgbuf *host_mb;
3162 abi_long ret = 0;
3163
3164 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
3165 return -TARGET_EFAULT;
3166
3167 host_mb = g_malloc(msgsz+sizeof(long));
3168 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
3169
3170 if (ret > 0) {
3171 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
3172 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
3173 if (!target_mtext) {
3174 ret = -TARGET_EFAULT;
3175 goto end;
3176 }
3177 memcpy(target_mb->mtext, host_mb->mtext, ret);
3178 unlock_user(target_mtext, target_mtext_addr, ret);
3179 }
3180
3181 target_mb->mtype = tswapal(host_mb->mtype);
3182
3183 end:
3184 if (target_mb)
3185 unlock_user_struct(target_mb, msgp, 1);
3186 g_free(host_mb);
3187 return ret;
3188 }
3189
3190 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
3191 abi_ulong target_addr)
3192 {
3193 struct target_shmid_ds *target_sd;
3194
3195 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3196 return -TARGET_EFAULT;
3197 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
3198 return -TARGET_EFAULT;
3199 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3200 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
3201 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3202 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3203 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3204 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3205 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3206 unlock_user_struct(target_sd, target_addr, 0);
3207 return 0;
3208 }
3209
3210 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
3211 struct shmid_ds *host_sd)
3212 {
3213 struct target_shmid_ds *target_sd;
3214
3215 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3216 return -TARGET_EFAULT;
3217 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
3218 return -TARGET_EFAULT;
3219 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
3220 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
3221 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
3222 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
3223 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
3224 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
3225 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
3226 unlock_user_struct(target_sd, target_addr, 1);
3227 return 0;
3228 }
3229
3230 struct target_shminfo {
3231 abi_ulong shmmax;
3232 abi_ulong shmmin;
3233 abi_ulong shmmni;
3234 abi_ulong shmseg;
3235 abi_ulong shmall;
3236 };
3237
3238 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
3239 struct shminfo *host_shminfo)
3240 {
3241 struct target_shminfo *target_shminfo;
3242 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
3243 return -TARGET_EFAULT;
3244 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
3245 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
3246 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
3247 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
3248 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
3249 unlock_user_struct(target_shminfo, target_addr, 1);
3250 return 0;
3251 }
3252
3253 struct target_shm_info {
3254 int used_ids;
3255 abi_ulong shm_tot;
3256 abi_ulong shm_rss;
3257 abi_ulong shm_swp;
3258 abi_ulong swap_attempts;
3259 abi_ulong swap_successes;
3260 };
3261
3262 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
3263 struct shm_info *host_shm_info)
3264 {
3265 struct target_shm_info *target_shm_info;
3266 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
3267 return -TARGET_EFAULT;
3268 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
3269 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
3270 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
3271 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
3272 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
3273 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
3274 unlock_user_struct(target_shm_info, target_addr, 1);
3275 return 0;
3276 }
3277
3278 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
3279 {
3280 struct shmid_ds dsarg;
3281 struct shminfo shminfo;
3282 struct shm_info shm_info;
3283 abi_long ret = -TARGET_EINVAL;
3284
3285 cmd &= 0xff;
3286
3287 switch(cmd) {
3288 case IPC_STAT:
3289 case IPC_SET:
3290 case SHM_STAT:
3291 if (target_to_host_shmid_ds(&dsarg, buf))
3292 return -TARGET_EFAULT;
3293 ret =