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