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