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