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