qdev/isa: convert real time clock
[qemu.git] / linux-user / syscall.c
blob25b95ea05a9aba3840fb15203424cc30ca290a47
1 /*
2 * Linux syscalls
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
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 #include <sys/socket.h>
45 #include <sys/un.h>
46 #include <sys/uio.h>
47 #include <sys/poll.h>
48 #include <sys/times.h>
49 #include <sys/shm.h>
50 #include <sys/sem.h>
51 #include <sys/statfs.h>
52 #include <utime.h>
53 #include <sys/sysinfo.h>
54 #include <sys/utsname.h>
55 //#include <sys/user.h>
56 #include <netinet/ip.h>
57 #include <netinet/tcp.h>
58 #include <qemu-common.h>
59 #ifdef TARGET_GPROF
60 #include <sys/gmon.h>
61 #endif
62 #ifdef CONFIG_EVENTFD
63 #include <sys/eventfd.h>
64 #endif
66 #define termios host_termios
67 #define winsize host_winsize
68 #define termio host_termio
69 #define sgttyb host_sgttyb /* same as target */
70 #define tchars host_tchars /* same as target */
71 #define ltchars host_ltchars /* same as target */
73 #include <linux/termios.h>
74 #include <linux/unistd.h>
75 #include <linux/utsname.h>
76 #include <linux/cdrom.h>
77 #include <linux/hdreg.h>
78 #include <linux/soundcard.h>
79 #include <linux/kd.h>
80 #include <linux/mtio.h>
81 #include <linux/fs.h>
82 #include "linux_loop.h"
84 #include "qemu.h"
85 #include "qemu-common.h"
87 #if defined(CONFIG_USE_NPTL)
88 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
89 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
90 #else
91 /* XXX: Hardcode the above values. */
92 #define CLONE_NPTL_FLAGS2 0
93 #endif
95 //#define DEBUG
97 //#include <linux/msdos_fs.h>
98 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
99 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
102 #undef _syscall0
103 #undef _syscall1
104 #undef _syscall2
105 #undef _syscall3
106 #undef _syscall4
107 #undef _syscall5
108 #undef _syscall6
110 #define _syscall0(type,name) \
111 static type name (void) \
113 return syscall(__NR_##name); \
116 #define _syscall1(type,name,type1,arg1) \
117 static type name (type1 arg1) \
119 return syscall(__NR_##name, arg1); \
122 #define _syscall2(type,name,type1,arg1,type2,arg2) \
123 static type name (type1 arg1,type2 arg2) \
125 return syscall(__NR_##name, arg1, arg2); \
128 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
129 static type name (type1 arg1,type2 arg2,type3 arg3) \
131 return syscall(__NR_##name, arg1, arg2, arg3); \
134 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
135 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
137 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
140 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
141 type5,arg5) \
142 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
144 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
148 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
149 type5,arg5,type6,arg6) \
150 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
151 type6 arg6) \
153 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
157 #define __NR_sys_uname __NR_uname
158 #define __NR_sys_faccessat __NR_faccessat
159 #define __NR_sys_fchmodat __NR_fchmodat
160 #define __NR_sys_fchownat __NR_fchownat
161 #define __NR_sys_fstatat64 __NR_fstatat64
162 #define __NR_sys_futimesat __NR_futimesat
163 #define __NR_sys_getcwd1 __NR_getcwd
164 #define __NR_sys_getdents __NR_getdents
165 #define __NR_sys_getdents64 __NR_getdents64
166 #define __NR_sys_getpriority __NR_getpriority
167 #define __NR_sys_linkat __NR_linkat
168 #define __NR_sys_mkdirat __NR_mkdirat
169 #define __NR_sys_mknodat __NR_mknodat
170 #define __NR_sys_newfstatat __NR_newfstatat
171 #define __NR_sys_openat __NR_openat
172 #define __NR_sys_readlinkat __NR_readlinkat
173 #define __NR_sys_renameat __NR_renameat
174 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
175 #define __NR_sys_symlinkat __NR_symlinkat
176 #define __NR_sys_syslog __NR_syslog
177 #define __NR_sys_tgkill __NR_tgkill
178 #define __NR_sys_tkill __NR_tkill
179 #define __NR_sys_unlinkat __NR_unlinkat
180 #define __NR_sys_utimensat __NR_utimensat
181 #define __NR_sys_futex __NR_futex
182 #define __NR_sys_inotify_init __NR_inotify_init
183 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
184 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
186 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
187 #define __NR__llseek __NR_lseek
188 #endif
190 #ifdef __NR_gettid
191 _syscall0(int, gettid)
192 #else
193 /* This is a replacement for the host gettid() and must return a host
194 errno. */
195 static int gettid(void) {
196 return -ENOSYS;
198 #endif
199 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
200 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
201 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
202 #endif
203 _syscall2(int, sys_getpriority, int, which, int, who);
204 #if defined(TARGET_NR__llseek) && !defined (__x86_64__)
205 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
206 loff_t *, res, uint, wh);
207 #endif
208 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
209 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
210 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
211 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
212 #endif
213 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
214 _syscall2(int,sys_tkill,int,tid,int,sig)
215 #endif
216 #ifdef __NR_exit_group
217 _syscall1(int,exit_group,int,error_code)
218 #endif
219 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
220 _syscall1(int,set_tid_address,int *,tidptr)
221 #endif
222 #if defined(CONFIG_USE_NPTL)
223 #if defined(TARGET_NR_futex) && defined(__NR_futex)
224 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
225 const struct timespec *,timeout,int *,uaddr2,int,val3)
226 #endif
227 #endif
229 static bitmask_transtbl fcntl_flags_tbl[] = {
230 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
231 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
232 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
233 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
234 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
235 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
236 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
237 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
238 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
239 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
240 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
241 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
242 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
243 #if defined(O_DIRECT)
244 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
245 #endif
246 { 0, 0, 0, 0 }
249 #define COPY_UTSNAME_FIELD(dest, src) \
250 do { \
251 /* __NEW_UTS_LEN doesn't include terminating null */ \
252 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
253 (dest)[__NEW_UTS_LEN] = '\0'; \
254 } while (0)
256 static int sys_uname(struct new_utsname *buf)
258 struct utsname uts_buf;
260 if (uname(&uts_buf) < 0)
261 return (-1);
264 * Just in case these have some differences, we
265 * translate utsname to new_utsname (which is the
266 * struct linux kernel uses).
269 bzero(buf, sizeof (*buf));
270 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
271 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
272 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
273 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
274 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
275 #ifdef _GNU_SOURCE
276 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
277 #endif
278 return (0);
280 #undef COPY_UTSNAME_FIELD
283 static int sys_getcwd1(char *buf, size_t size)
285 if (getcwd(buf, size) == NULL) {
286 /* getcwd() sets errno */
287 return (-1);
289 return strlen(buf)+1;
292 #ifdef CONFIG_ATFILE
294 * Host system seems to have atfile syscall stubs available. We
295 * now enable them one by one as specified by target syscall_nr.h.
298 #ifdef TARGET_NR_faccessat
299 static int sys_faccessat(int dirfd, const char *pathname, int mode)
301 return (faccessat(dirfd, pathname, mode, 0));
303 #endif
304 #ifdef TARGET_NR_fchmodat
305 static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
307 return (fchmodat(dirfd, pathname, mode, 0));
309 #endif
310 #if defined(TARGET_NR_fchownat) && defined(USE_UID16)
311 static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
312 gid_t group, int flags)
314 return (fchownat(dirfd, pathname, owner, group, flags));
316 #endif
317 #ifdef __NR_fstatat64
318 static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
319 int flags)
321 return (fstatat(dirfd, pathname, buf, flags));
323 #endif
324 #ifdef __NR_newfstatat
325 static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
326 int flags)
328 return (fstatat(dirfd, pathname, buf, flags));
330 #endif
331 #ifdef TARGET_NR_futimesat
332 static int sys_futimesat(int dirfd, const char *pathname,
333 const struct timeval times[2])
335 return (futimesat(dirfd, pathname, times));
337 #endif
338 #ifdef TARGET_NR_linkat
339 static int sys_linkat(int olddirfd, const char *oldpath,
340 int newdirfd, const char *newpath, int flags)
342 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
344 #endif
345 #ifdef TARGET_NR_mkdirat
346 static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
348 return (mkdirat(dirfd, pathname, mode));
350 #endif
351 #ifdef TARGET_NR_mknodat
352 static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
353 dev_t dev)
355 return (mknodat(dirfd, pathname, mode, dev));
357 #endif
358 #ifdef TARGET_NR_openat
359 static int sys_openat(int dirfd, const char *pathname, int flags, ...)
362 * open(2) has extra parameter 'mode' when called with
363 * flag O_CREAT.
365 if ((flags & O_CREAT) != 0) {
366 va_list ap;
367 mode_t mode;
370 * Get the 'mode' parameter and translate it to
371 * host bits.
373 va_start(ap, flags);
374 mode = va_arg(ap, mode_t);
375 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
376 va_end(ap);
378 return (openat(dirfd, pathname, flags, mode));
380 return (openat(dirfd, pathname, flags));
382 #endif
383 #ifdef TARGET_NR_readlinkat
384 static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
386 return (readlinkat(dirfd, pathname, buf, bufsiz));
388 #endif
389 #ifdef TARGET_NR_renameat
390 static int sys_renameat(int olddirfd, const char *oldpath,
391 int newdirfd, const char *newpath)
393 return (renameat(olddirfd, oldpath, newdirfd, newpath));
395 #endif
396 #ifdef TARGET_NR_symlinkat
397 static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
399 return (symlinkat(oldpath, newdirfd, newpath));
401 #endif
402 #ifdef TARGET_NR_unlinkat
403 static int sys_unlinkat(int dirfd, const char *pathname, int flags)
405 return (unlinkat(dirfd, pathname, flags));
407 #endif
408 #else /* !CONFIG_ATFILE */
411 * Try direct syscalls instead
413 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
414 _syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
415 #endif
416 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
417 _syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
418 #endif
419 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
420 _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
421 uid_t,owner,gid_t,group,int,flags)
422 #endif
423 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
424 defined(__NR_fstatat64)
425 _syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
426 struct stat *,buf,int,flags)
427 #endif
428 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
429 _syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
430 const struct timeval *,times)
431 #endif
432 #if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
433 defined(__NR_newfstatat)
434 _syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
435 struct stat *,buf,int,flags)
436 #endif
437 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
438 _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
439 int,newdirfd,const char *,newpath,int,flags)
440 #endif
441 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
442 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
443 #endif
444 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
445 _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
446 mode_t,mode,dev_t,dev)
447 #endif
448 #if defined(TARGET_NR_openat) && defined(__NR_openat)
449 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
450 #endif
451 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
452 _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
453 char *,buf,size_t,bufsize)
454 #endif
455 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
456 _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
457 int,newdirfd,const char *,newpath)
458 #endif
459 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
460 _syscall3(int,sys_symlinkat,const char *,oldpath,
461 int,newdirfd,const char *,newpath)
462 #endif
463 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
464 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
465 #endif
467 #endif /* CONFIG_ATFILE */
469 #ifdef CONFIG_UTIMENSAT
470 static int sys_utimensat(int dirfd, const char *pathname,
471 const struct timespec times[2], int flags)
473 if (pathname == NULL)
474 return futimens(dirfd, times);
475 else
476 return utimensat(dirfd, pathname, times, flags);
478 #else
479 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
480 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
481 const struct timespec *,tsp,int,flags)
482 #endif
483 #endif /* CONFIG_UTIMENSAT */
485 #ifdef CONFIG_INOTIFY
486 #include <sys/inotify.h>
488 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
489 static int sys_inotify_init(void)
491 return (inotify_init());
493 #endif
494 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
495 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
497 return (inotify_add_watch(fd, pathname, mask));
499 #endif
500 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
501 static int sys_inotify_rm_watch(int fd, int32_t wd)
503 return (inotify_rm_watch(fd, wd));
505 #endif
506 #else
507 /* Userspace can usually survive runtime without inotify */
508 #undef TARGET_NR_inotify_init
509 #undef TARGET_NR_inotify_add_watch
510 #undef TARGET_NR_inotify_rm_watch
511 #endif /* CONFIG_INOTIFY */
514 extern int personality(int);
515 extern int flock(int, int);
516 extern int setfsuid(int);
517 extern int setfsgid(int);
518 extern int setgroups(int, gid_t *);
520 #define ERRNO_TABLE_SIZE 1200
522 /* target_to_host_errno_table[] is initialized from
523 * host_to_target_errno_table[] in syscall_init(). */
524 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
528 * This list is the union of errno values overridden in asm-<arch>/errno.h
529 * minus the errnos that are not actually generic to all archs.
531 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
532 [EIDRM] = TARGET_EIDRM,
533 [ECHRNG] = TARGET_ECHRNG,
534 [EL2NSYNC] = TARGET_EL2NSYNC,
535 [EL3HLT] = TARGET_EL3HLT,
536 [EL3RST] = TARGET_EL3RST,
537 [ELNRNG] = TARGET_ELNRNG,
538 [EUNATCH] = TARGET_EUNATCH,
539 [ENOCSI] = TARGET_ENOCSI,
540 [EL2HLT] = TARGET_EL2HLT,
541 [EDEADLK] = TARGET_EDEADLK,
542 [ENOLCK] = TARGET_ENOLCK,
543 [EBADE] = TARGET_EBADE,
544 [EBADR] = TARGET_EBADR,
545 [EXFULL] = TARGET_EXFULL,
546 [ENOANO] = TARGET_ENOANO,
547 [EBADRQC] = TARGET_EBADRQC,
548 [EBADSLT] = TARGET_EBADSLT,
549 [EBFONT] = TARGET_EBFONT,
550 [ENOSTR] = TARGET_ENOSTR,
551 [ENODATA] = TARGET_ENODATA,
552 [ETIME] = TARGET_ETIME,
553 [ENOSR] = TARGET_ENOSR,
554 [ENONET] = TARGET_ENONET,
555 [ENOPKG] = TARGET_ENOPKG,
556 [EREMOTE] = TARGET_EREMOTE,
557 [ENOLINK] = TARGET_ENOLINK,
558 [EADV] = TARGET_EADV,
559 [ESRMNT] = TARGET_ESRMNT,
560 [ECOMM] = TARGET_ECOMM,
561 [EPROTO] = TARGET_EPROTO,
562 [EDOTDOT] = TARGET_EDOTDOT,
563 [EMULTIHOP] = TARGET_EMULTIHOP,
564 [EBADMSG] = TARGET_EBADMSG,
565 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
566 [EOVERFLOW] = TARGET_EOVERFLOW,
567 [ENOTUNIQ] = TARGET_ENOTUNIQ,
568 [EBADFD] = TARGET_EBADFD,
569 [EREMCHG] = TARGET_EREMCHG,
570 [ELIBACC] = TARGET_ELIBACC,
571 [ELIBBAD] = TARGET_ELIBBAD,
572 [ELIBSCN] = TARGET_ELIBSCN,
573 [ELIBMAX] = TARGET_ELIBMAX,
574 [ELIBEXEC] = TARGET_ELIBEXEC,
575 [EILSEQ] = TARGET_EILSEQ,
576 [ENOSYS] = TARGET_ENOSYS,
577 [ELOOP] = TARGET_ELOOP,
578 [ERESTART] = TARGET_ERESTART,
579 [ESTRPIPE] = TARGET_ESTRPIPE,
580 [ENOTEMPTY] = TARGET_ENOTEMPTY,
581 [EUSERS] = TARGET_EUSERS,
582 [ENOTSOCK] = TARGET_ENOTSOCK,
583 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
584 [EMSGSIZE] = TARGET_EMSGSIZE,
585 [EPROTOTYPE] = TARGET_EPROTOTYPE,
586 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
587 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
588 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
589 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
590 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
591 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
592 [EADDRINUSE] = TARGET_EADDRINUSE,
593 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
594 [ENETDOWN] = TARGET_ENETDOWN,
595 [ENETUNREACH] = TARGET_ENETUNREACH,
596 [ENETRESET] = TARGET_ENETRESET,
597 [ECONNABORTED] = TARGET_ECONNABORTED,
598 [ECONNRESET] = TARGET_ECONNRESET,
599 [ENOBUFS] = TARGET_ENOBUFS,
600 [EISCONN] = TARGET_EISCONN,
601 [ENOTCONN] = TARGET_ENOTCONN,
602 [EUCLEAN] = TARGET_EUCLEAN,
603 [ENOTNAM] = TARGET_ENOTNAM,
604 [ENAVAIL] = TARGET_ENAVAIL,
605 [EISNAM] = TARGET_EISNAM,
606 [EREMOTEIO] = TARGET_EREMOTEIO,
607 [ESHUTDOWN] = TARGET_ESHUTDOWN,
608 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
609 [ETIMEDOUT] = TARGET_ETIMEDOUT,
610 [ECONNREFUSED] = TARGET_ECONNREFUSED,
611 [EHOSTDOWN] = TARGET_EHOSTDOWN,
612 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
613 [EALREADY] = TARGET_EALREADY,
614 [EINPROGRESS] = TARGET_EINPROGRESS,
615 [ESTALE] = TARGET_ESTALE,
616 [ECANCELED] = TARGET_ECANCELED,
617 [ENOMEDIUM] = TARGET_ENOMEDIUM,
618 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
619 #ifdef ENOKEY
620 [ENOKEY] = TARGET_ENOKEY,
621 #endif
622 #ifdef EKEYEXPIRED
623 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
624 #endif
625 #ifdef EKEYREVOKED
626 [EKEYREVOKED] = TARGET_EKEYREVOKED,
627 #endif
628 #ifdef EKEYREJECTED
629 [EKEYREJECTED] = TARGET_EKEYREJECTED,
630 #endif
631 #ifdef EOWNERDEAD
632 [EOWNERDEAD] = TARGET_EOWNERDEAD,
633 #endif
634 #ifdef ENOTRECOVERABLE
635 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
636 #endif
639 static inline int host_to_target_errno(int err)
641 if(host_to_target_errno_table[err])
642 return host_to_target_errno_table[err];
643 return err;
646 static inline int target_to_host_errno(int err)
648 if (target_to_host_errno_table[err])
649 return target_to_host_errno_table[err];
650 return err;
653 static inline abi_long get_errno(abi_long ret)
655 if (ret == -1)
656 return -host_to_target_errno(errno);
657 else
658 return ret;
661 static inline int is_error(abi_long ret)
663 return (abi_ulong)ret >= (abi_ulong)(-4096);
666 char *target_strerror(int err)
668 return strerror(target_to_host_errno(err));
671 static abi_ulong target_brk;
672 static abi_ulong target_original_brk;
674 void target_set_brk(abi_ulong new_brk)
676 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
679 /* do_brk() must return target values and target errnos. */
680 abi_long do_brk(abi_ulong new_brk)
682 abi_ulong brk_page;
683 abi_long mapped_addr;
684 int new_alloc_size;
686 if (!new_brk)
687 return target_brk;
688 if (new_brk < target_original_brk)
689 return target_brk;
691 brk_page = HOST_PAGE_ALIGN(target_brk);
693 /* If the new brk is less than this, set it and we're done... */
694 if (new_brk < brk_page) {
695 target_brk = new_brk;
696 return target_brk;
699 /* We need to allocate more memory after the brk... */
700 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
701 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
702 PROT_READ|PROT_WRITE,
703 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
705 if (!is_error(mapped_addr))
706 target_brk = new_brk;
708 return target_brk;
711 static inline abi_long copy_from_user_fdset(fd_set *fds,
712 abi_ulong target_fds_addr,
713 int n)
715 int i, nw, j, k;
716 abi_ulong b, *target_fds;
718 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
719 if (!(target_fds = lock_user(VERIFY_READ,
720 target_fds_addr,
721 sizeof(abi_ulong) * nw,
722 1)))
723 return -TARGET_EFAULT;
725 FD_ZERO(fds);
726 k = 0;
727 for (i = 0; i < nw; i++) {
728 /* grab the abi_ulong */
729 __get_user(b, &target_fds[i]);
730 for (j = 0; j < TARGET_ABI_BITS; j++) {
731 /* check the bit inside the abi_ulong */
732 if ((b >> j) & 1)
733 FD_SET(k, fds);
734 k++;
738 unlock_user(target_fds, target_fds_addr, 0);
740 return 0;
743 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
744 const fd_set *fds,
745 int n)
747 int i, nw, j, k;
748 abi_long v;
749 abi_ulong *target_fds;
751 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
752 if (!(target_fds = lock_user(VERIFY_WRITE,
753 target_fds_addr,
754 sizeof(abi_ulong) * nw,
755 0)))
756 return -TARGET_EFAULT;
758 k = 0;
759 for (i = 0; i < nw; i++) {
760 v = 0;
761 for (j = 0; j < TARGET_ABI_BITS; j++) {
762 v |= ((FD_ISSET(k, fds) != 0) << j);
763 k++;
765 __put_user(v, &target_fds[i]);
768 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
770 return 0;
773 #if defined(__alpha__)
774 #define HOST_HZ 1024
775 #else
776 #define HOST_HZ 100
777 #endif
779 static inline abi_long host_to_target_clock_t(long ticks)
781 #if HOST_HZ == TARGET_HZ
782 return ticks;
783 #else
784 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
785 #endif
788 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
789 const struct rusage *rusage)
791 struct target_rusage *target_rusage;
793 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
794 return -TARGET_EFAULT;
795 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
796 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
797 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
798 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
799 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
800 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
801 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
802 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
803 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
804 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
805 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
806 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
807 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
808 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
809 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
810 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
811 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
812 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
813 unlock_user_struct(target_rusage, target_addr, 1);
815 return 0;
818 static inline abi_long copy_from_user_timeval(struct timeval *tv,
819 abi_ulong target_tv_addr)
821 struct target_timeval *target_tv;
823 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
824 return -TARGET_EFAULT;
826 __get_user(tv->tv_sec, &target_tv->tv_sec);
827 __get_user(tv->tv_usec, &target_tv->tv_usec);
829 unlock_user_struct(target_tv, target_tv_addr, 0);
831 return 0;
834 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
835 const struct timeval *tv)
837 struct target_timeval *target_tv;
839 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
840 return -TARGET_EFAULT;
842 __put_user(tv->tv_sec, &target_tv->tv_sec);
843 __put_user(tv->tv_usec, &target_tv->tv_usec);
845 unlock_user_struct(target_tv, target_tv_addr, 1);
847 return 0;
850 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
851 #include <mqueue.h>
853 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
854 abi_ulong target_mq_attr_addr)
856 struct target_mq_attr *target_mq_attr;
858 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
859 target_mq_attr_addr, 1))
860 return -TARGET_EFAULT;
862 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
863 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
864 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
865 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
867 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
869 return 0;
872 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
873 const struct mq_attr *attr)
875 struct target_mq_attr *target_mq_attr;
877 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
878 target_mq_attr_addr, 0))
879 return -TARGET_EFAULT;
881 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
882 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
883 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
884 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
886 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
888 return 0;
890 #endif
892 /* do_select() must return target values and target errnos. */
893 static abi_long do_select(int n,
894 abi_ulong rfd_addr, abi_ulong wfd_addr,
895 abi_ulong efd_addr, abi_ulong target_tv_addr)
897 fd_set rfds, wfds, efds;
898 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
899 struct timeval tv, *tv_ptr;
900 abi_long ret;
902 if (rfd_addr) {
903 if (copy_from_user_fdset(&rfds, rfd_addr, n))
904 return -TARGET_EFAULT;
905 rfds_ptr = &rfds;
906 } else {
907 rfds_ptr = NULL;
909 if (wfd_addr) {
910 if (copy_from_user_fdset(&wfds, wfd_addr, n))
911 return -TARGET_EFAULT;
912 wfds_ptr = &wfds;
913 } else {
914 wfds_ptr = NULL;
916 if (efd_addr) {
917 if (copy_from_user_fdset(&efds, efd_addr, n))
918 return -TARGET_EFAULT;
919 efds_ptr = &efds;
920 } else {
921 efds_ptr = NULL;
924 if (target_tv_addr) {
925 if (copy_from_user_timeval(&tv, target_tv_addr))
926 return -TARGET_EFAULT;
927 tv_ptr = &tv;
928 } else {
929 tv_ptr = NULL;
932 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
934 if (!is_error(ret)) {
935 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
936 return -TARGET_EFAULT;
937 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
938 return -TARGET_EFAULT;
939 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
940 return -TARGET_EFAULT;
942 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
943 return -TARGET_EFAULT;
946 return ret;
949 static abi_long do_pipe2(int host_pipe[], int flags)
951 #ifdef CONFIG_PIPE2
952 return pipe2(host_pipe, flags);
953 #else
954 return -ENOSYS;
955 #endif
958 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes, int flags)
960 int host_pipe[2];
961 abi_long ret;
962 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
964 if (is_error(ret))
965 return get_errno(ret);
966 #if defined(TARGET_MIPS)
967 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
968 ret = host_pipe[0];
969 #elif defined(TARGET_SH4)
970 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
971 ret = host_pipe[0];
972 #else
973 if (put_user_s32(host_pipe[0], pipedes)
974 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
975 return -TARGET_EFAULT;
976 #endif
977 return get_errno(ret);
980 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
981 abi_ulong target_addr,
982 socklen_t len)
984 struct target_ip_mreqn *target_smreqn;
986 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
987 if (!target_smreqn)
988 return -TARGET_EFAULT;
989 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
990 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
991 if (len == sizeof(struct target_ip_mreqn))
992 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
993 unlock_user(target_smreqn, target_addr, 0);
995 return 0;
998 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
999 abi_ulong target_addr,
1000 socklen_t len)
1002 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1003 sa_family_t sa_family;
1004 struct target_sockaddr *target_saddr;
1006 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1007 if (!target_saddr)
1008 return -TARGET_EFAULT;
1010 sa_family = tswap16(target_saddr->sa_family);
1012 /* Oops. The caller might send a incomplete sun_path; sun_path
1013 * must be terminated by \0 (see the manual page), but
1014 * unfortunately it is quite common to specify sockaddr_un
1015 * length as "strlen(x->sun_path)" while it should be
1016 * "strlen(...) + 1". We'll fix that here if needed.
1017 * Linux kernel has a similar feature.
1020 if (sa_family == AF_UNIX) {
1021 if (len < unix_maxlen && len > 0) {
1022 char *cp = (char*)target_saddr;
1024 if ( cp[len-1] && !cp[len] )
1025 len++;
1027 if (len > unix_maxlen)
1028 len = unix_maxlen;
1031 memcpy(addr, target_saddr, len);
1032 addr->sa_family = sa_family;
1033 unlock_user(target_saddr, target_addr, 0);
1035 return 0;
1038 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1039 struct sockaddr *addr,
1040 socklen_t len)
1042 struct target_sockaddr *target_saddr;
1044 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1045 if (!target_saddr)
1046 return -TARGET_EFAULT;
1047 memcpy(target_saddr, addr, len);
1048 target_saddr->sa_family = tswap16(addr->sa_family);
1049 unlock_user(target_saddr, target_addr, len);
1051 return 0;
1054 /* ??? Should this also swap msgh->name? */
1055 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1056 struct target_msghdr *target_msgh)
1058 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1059 abi_long msg_controllen;
1060 abi_ulong target_cmsg_addr;
1061 struct target_cmsghdr *target_cmsg;
1062 socklen_t space = 0;
1064 msg_controllen = tswapl(target_msgh->msg_controllen);
1065 if (msg_controllen < sizeof (struct target_cmsghdr))
1066 goto the_end;
1067 target_cmsg_addr = tswapl(target_msgh->msg_control);
1068 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1069 if (!target_cmsg)
1070 return -TARGET_EFAULT;
1072 while (cmsg && target_cmsg) {
1073 void *data = CMSG_DATA(cmsg);
1074 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1076 int len = tswapl(target_cmsg->cmsg_len)
1077 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1079 space += CMSG_SPACE(len);
1080 if (space > msgh->msg_controllen) {
1081 space -= CMSG_SPACE(len);
1082 gemu_log("Host cmsg overflow\n");
1083 break;
1086 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1087 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1088 cmsg->cmsg_len = CMSG_LEN(len);
1090 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1091 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1092 memcpy(data, target_data, len);
1093 } else {
1094 int *fd = (int *)data;
1095 int *target_fd = (int *)target_data;
1096 int i, numfds = len / sizeof(int);
1098 for (i = 0; i < numfds; i++)
1099 fd[i] = tswap32(target_fd[i]);
1102 cmsg = CMSG_NXTHDR(msgh, cmsg);
1103 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1105 unlock_user(target_cmsg, target_cmsg_addr, 0);
1106 the_end:
1107 msgh->msg_controllen = space;
1108 return 0;
1111 /* ??? Should this also swap msgh->name? */
1112 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1113 struct msghdr *msgh)
1115 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1116 abi_long msg_controllen;
1117 abi_ulong target_cmsg_addr;
1118 struct target_cmsghdr *target_cmsg;
1119 socklen_t space = 0;
1121 msg_controllen = tswapl(target_msgh->msg_controllen);
1122 if (msg_controllen < sizeof (struct target_cmsghdr))
1123 goto the_end;
1124 target_cmsg_addr = tswapl(target_msgh->msg_control);
1125 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1126 if (!target_cmsg)
1127 return -TARGET_EFAULT;
1129 while (cmsg && target_cmsg) {
1130 void *data = CMSG_DATA(cmsg);
1131 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1133 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1135 space += TARGET_CMSG_SPACE(len);
1136 if (space > msg_controllen) {
1137 space -= TARGET_CMSG_SPACE(len);
1138 gemu_log("Target cmsg overflow\n");
1139 break;
1142 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1143 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1144 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1146 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1147 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1148 memcpy(target_data, data, len);
1149 } else {
1150 int *fd = (int *)data;
1151 int *target_fd = (int *)target_data;
1152 int i, numfds = len / sizeof(int);
1154 for (i = 0; i < numfds; i++)
1155 target_fd[i] = tswap32(fd[i]);
1158 cmsg = CMSG_NXTHDR(msgh, cmsg);
1159 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1161 unlock_user(target_cmsg, target_cmsg_addr, space);
1162 the_end:
1163 target_msgh->msg_controllen = tswapl(space);
1164 return 0;
1167 /* do_setsockopt() Must return target values and target errnos. */
1168 static abi_long do_setsockopt(int sockfd, int level, int optname,
1169 abi_ulong optval_addr, socklen_t optlen)
1171 abi_long ret;
1172 int val;
1173 struct ip_mreqn *ip_mreq;
1174 struct ip_mreq_source *ip_mreq_source;
1176 switch(level) {
1177 case SOL_TCP:
1178 /* TCP options all take an 'int' value. */
1179 if (optlen < sizeof(uint32_t))
1180 return -TARGET_EINVAL;
1182 if (get_user_u32(val, optval_addr))
1183 return -TARGET_EFAULT;
1184 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1185 break;
1186 case SOL_IP:
1187 switch(optname) {
1188 case IP_TOS:
1189 case IP_TTL:
1190 case IP_HDRINCL:
1191 case IP_ROUTER_ALERT:
1192 case IP_RECVOPTS:
1193 case IP_RETOPTS:
1194 case IP_PKTINFO:
1195 case IP_MTU_DISCOVER:
1196 case IP_RECVERR:
1197 case IP_RECVTOS:
1198 #ifdef IP_FREEBIND
1199 case IP_FREEBIND:
1200 #endif
1201 case IP_MULTICAST_TTL:
1202 case IP_MULTICAST_LOOP:
1203 val = 0;
1204 if (optlen >= sizeof(uint32_t)) {
1205 if (get_user_u32(val, optval_addr))
1206 return -TARGET_EFAULT;
1207 } else if (optlen >= 1) {
1208 if (get_user_u8(val, optval_addr))
1209 return -TARGET_EFAULT;
1211 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1212 break;
1213 case IP_ADD_MEMBERSHIP:
1214 case IP_DROP_MEMBERSHIP:
1215 if (optlen < sizeof (struct target_ip_mreq) ||
1216 optlen > sizeof (struct target_ip_mreqn))
1217 return -TARGET_EINVAL;
1219 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1220 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1221 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1222 break;
1224 case IP_BLOCK_SOURCE:
1225 case IP_UNBLOCK_SOURCE:
1226 case IP_ADD_SOURCE_MEMBERSHIP:
1227 case IP_DROP_SOURCE_MEMBERSHIP:
1228 if (optlen != sizeof (struct target_ip_mreq_source))
1229 return -TARGET_EINVAL;
1231 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1232 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1233 unlock_user (ip_mreq_source, optval_addr, 0);
1234 break;
1236 default:
1237 goto unimplemented;
1239 break;
1240 case TARGET_SOL_SOCKET:
1241 switch (optname) {
1242 /* Options with 'int' argument. */
1243 case TARGET_SO_DEBUG:
1244 optname = SO_DEBUG;
1245 break;
1246 case TARGET_SO_REUSEADDR:
1247 optname = SO_REUSEADDR;
1248 break;
1249 case TARGET_SO_TYPE:
1250 optname = SO_TYPE;
1251 break;
1252 case TARGET_SO_ERROR:
1253 optname = SO_ERROR;
1254 break;
1255 case TARGET_SO_DONTROUTE:
1256 optname = SO_DONTROUTE;
1257 break;
1258 case TARGET_SO_BROADCAST:
1259 optname = SO_BROADCAST;
1260 break;
1261 case TARGET_SO_SNDBUF:
1262 optname = SO_SNDBUF;
1263 break;
1264 case TARGET_SO_RCVBUF:
1265 optname = SO_RCVBUF;
1266 break;
1267 case TARGET_SO_KEEPALIVE:
1268 optname = SO_KEEPALIVE;
1269 break;
1270 case TARGET_SO_OOBINLINE:
1271 optname = SO_OOBINLINE;
1272 break;
1273 case TARGET_SO_NO_CHECK:
1274 optname = SO_NO_CHECK;
1275 break;
1276 case TARGET_SO_PRIORITY:
1277 optname = SO_PRIORITY;
1278 break;
1279 #ifdef SO_BSDCOMPAT
1280 case TARGET_SO_BSDCOMPAT:
1281 optname = SO_BSDCOMPAT;
1282 break;
1283 #endif
1284 case TARGET_SO_PASSCRED:
1285 optname = SO_PASSCRED;
1286 break;
1287 case TARGET_SO_TIMESTAMP:
1288 optname = SO_TIMESTAMP;
1289 break;
1290 case TARGET_SO_RCVLOWAT:
1291 optname = SO_RCVLOWAT;
1292 break;
1293 case TARGET_SO_RCVTIMEO:
1294 optname = SO_RCVTIMEO;
1295 break;
1296 case TARGET_SO_SNDTIMEO:
1297 optname = SO_SNDTIMEO;
1298 break;
1299 break;
1300 default:
1301 goto unimplemented;
1303 if (optlen < sizeof(uint32_t))
1304 return -TARGET_EINVAL;
1306 if (get_user_u32(val, optval_addr))
1307 return -TARGET_EFAULT;
1308 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1309 break;
1310 default:
1311 unimplemented:
1312 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
1313 ret = -TARGET_ENOPROTOOPT;
1315 return ret;
1318 /* do_getsockopt() Must return target values and target errnos. */
1319 static abi_long do_getsockopt(int sockfd, int level, int optname,
1320 abi_ulong optval_addr, abi_ulong optlen)
1322 abi_long ret;
1323 int len, val;
1324 socklen_t lv;
1326 switch(level) {
1327 case TARGET_SOL_SOCKET:
1328 level = SOL_SOCKET;
1329 switch (optname) {
1330 case TARGET_SO_LINGER:
1331 case TARGET_SO_RCVTIMEO:
1332 case TARGET_SO_SNDTIMEO:
1333 case TARGET_SO_PEERCRED:
1334 case TARGET_SO_PEERNAME:
1335 /* These don't just return a single integer */
1336 goto unimplemented;
1337 default:
1338 goto int_case;
1340 break;
1341 case SOL_TCP:
1342 /* TCP options all take an 'int' value. */
1343 int_case:
1344 if (get_user_u32(len, optlen))
1345 return -TARGET_EFAULT;
1346 if (len < 0)
1347 return -TARGET_EINVAL;
1348 lv = sizeof(int);
1349 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1350 if (ret < 0)
1351 return ret;
1352 if (len > lv)
1353 len = lv;
1354 if (len == 4) {
1355 if (put_user_u32(val, optval_addr))
1356 return -TARGET_EFAULT;
1357 } else {
1358 if (put_user_u8(val, optval_addr))
1359 return -TARGET_EFAULT;
1361 if (put_user_u32(len, optlen))
1362 return -TARGET_EFAULT;
1363 break;
1364 case SOL_IP:
1365 switch(optname) {
1366 case IP_TOS:
1367 case IP_TTL:
1368 case IP_HDRINCL:
1369 case IP_ROUTER_ALERT:
1370 case IP_RECVOPTS:
1371 case IP_RETOPTS:
1372 case IP_PKTINFO:
1373 case IP_MTU_DISCOVER:
1374 case IP_RECVERR:
1375 case IP_RECVTOS:
1376 #ifdef IP_FREEBIND
1377 case IP_FREEBIND:
1378 #endif
1379 case IP_MULTICAST_TTL:
1380 case IP_MULTICAST_LOOP:
1381 if (get_user_u32(len, optlen))
1382 return -TARGET_EFAULT;
1383 if (len < 0)
1384 return -TARGET_EINVAL;
1385 lv = sizeof(int);
1386 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1387 if (ret < 0)
1388 return ret;
1389 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1390 len = 1;
1391 if (put_user_u32(len, optlen)
1392 || put_user_u8(val, optval_addr))
1393 return -TARGET_EFAULT;
1394 } else {
1395 if (len > sizeof(int))
1396 len = sizeof(int);
1397 if (put_user_u32(len, optlen)
1398 || put_user_u32(val, optval_addr))
1399 return -TARGET_EFAULT;
1401 break;
1402 default:
1403 ret = -TARGET_ENOPROTOOPT;
1404 break;
1406 break;
1407 default:
1408 unimplemented:
1409 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1410 level, optname);
1411 ret = -TARGET_EOPNOTSUPP;
1412 break;
1414 return ret;
1417 /* FIXME
1418 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1419 * other lock functions have a return code of 0 for failure.
1421 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1422 int count, int copy)
1424 struct target_iovec *target_vec;
1425 abi_ulong base;
1426 int i;
1428 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1429 if (!target_vec)
1430 return -TARGET_EFAULT;
1431 for(i = 0;i < count; i++) {
1432 base = tswapl(target_vec[i].iov_base);
1433 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1434 if (vec[i].iov_len != 0) {
1435 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1436 /* Don't check lock_user return value. We must call writev even
1437 if a element has invalid base address. */
1438 } else {
1439 /* zero length pointer is ignored */
1440 vec[i].iov_base = NULL;
1443 unlock_user (target_vec, target_addr, 0);
1444 return 0;
1447 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1448 int count, int copy)
1450 struct target_iovec *target_vec;
1451 abi_ulong base;
1452 int i;
1454 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1455 if (!target_vec)
1456 return -TARGET_EFAULT;
1457 for(i = 0;i < count; i++) {
1458 if (target_vec[i].iov_base) {
1459 base = tswapl(target_vec[i].iov_base);
1460 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1463 unlock_user (target_vec, target_addr, 0);
1465 return 0;
1468 /* do_socket() Must return target values and target errnos. */
1469 static abi_long do_socket(int domain, int type, int protocol)
1471 #if defined(TARGET_MIPS)
1472 switch(type) {
1473 case TARGET_SOCK_DGRAM:
1474 type = SOCK_DGRAM;
1475 break;
1476 case TARGET_SOCK_STREAM:
1477 type = SOCK_STREAM;
1478 break;
1479 case TARGET_SOCK_RAW:
1480 type = SOCK_RAW;
1481 break;
1482 case TARGET_SOCK_RDM:
1483 type = SOCK_RDM;
1484 break;
1485 case TARGET_SOCK_SEQPACKET:
1486 type = SOCK_SEQPACKET;
1487 break;
1488 case TARGET_SOCK_PACKET:
1489 type = SOCK_PACKET;
1490 break;
1492 #endif
1493 if (domain == PF_NETLINK)
1494 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1495 return get_errno(socket(domain, type, protocol));
1498 /* do_bind() Must return target values and target errnos. */
1499 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1500 socklen_t addrlen)
1502 void *addr;
1503 abi_long ret;
1505 if (addrlen < 0)
1506 return -TARGET_EINVAL;
1508 addr = alloca(addrlen+1);
1510 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1511 if (ret)
1512 return ret;
1514 return get_errno(bind(sockfd, addr, addrlen));
1517 /* do_connect() Must return target values and target errnos. */
1518 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1519 socklen_t addrlen)
1521 void *addr;
1522 abi_long ret;
1524 if (addrlen < 0)
1525 return -TARGET_EINVAL;
1527 addr = alloca(addrlen);
1529 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1530 if (ret)
1531 return ret;
1533 return get_errno(connect(sockfd, addr, addrlen));
1536 /* do_sendrecvmsg() Must return target values and target errnos. */
1537 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1538 int flags, int send)
1540 abi_long ret, len;
1541 struct target_msghdr *msgp;
1542 struct msghdr msg;
1543 int count;
1544 struct iovec *vec;
1545 abi_ulong target_vec;
1547 /* FIXME */
1548 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1549 msgp,
1550 target_msg,
1551 send ? 1 : 0))
1552 return -TARGET_EFAULT;
1553 if (msgp->msg_name) {
1554 msg.msg_namelen = tswap32(msgp->msg_namelen);
1555 msg.msg_name = alloca(msg.msg_namelen);
1556 ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1557 msg.msg_namelen);
1558 if (ret) {
1559 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1560 return ret;
1562 } else {
1563 msg.msg_name = NULL;
1564 msg.msg_namelen = 0;
1566 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1567 msg.msg_control = alloca(msg.msg_controllen);
1568 msg.msg_flags = tswap32(msgp->msg_flags);
1570 count = tswapl(msgp->msg_iovlen);
1571 vec = alloca(count * sizeof(struct iovec));
1572 target_vec = tswapl(msgp->msg_iov);
1573 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1574 msg.msg_iovlen = count;
1575 msg.msg_iov = vec;
1577 if (send) {
1578 ret = target_to_host_cmsg(&msg, msgp);
1579 if (ret == 0)
1580 ret = get_errno(sendmsg(fd, &msg, flags));
1581 } else {
1582 ret = get_errno(recvmsg(fd, &msg, flags));
1583 if (!is_error(ret)) {
1584 len = ret;
1585 ret = host_to_target_cmsg(msgp, &msg);
1586 if (!is_error(ret))
1587 ret = len;
1590 unlock_iovec(vec, target_vec, count, !send);
1591 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1592 return ret;
1595 /* do_accept() Must return target values and target errnos. */
1596 static abi_long do_accept(int fd, abi_ulong target_addr,
1597 abi_ulong target_addrlen_addr)
1599 socklen_t addrlen;
1600 void *addr;
1601 abi_long ret;
1603 if (target_addr == 0)
1604 return get_errno(accept(fd, NULL, NULL));
1606 /* linux returns EINVAL if addrlen pointer is invalid */
1607 if (get_user_u32(addrlen, target_addrlen_addr))
1608 return -TARGET_EINVAL;
1610 if (addrlen < 0)
1611 return -TARGET_EINVAL;
1613 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1614 return -TARGET_EINVAL;
1616 addr = alloca(addrlen);
1618 ret = get_errno(accept(fd, addr, &addrlen));
1619 if (!is_error(ret)) {
1620 host_to_target_sockaddr(target_addr, addr, addrlen);
1621 if (put_user_u32(addrlen, target_addrlen_addr))
1622 ret = -TARGET_EFAULT;
1624 return ret;
1627 /* do_getpeername() Must return target values and target errnos. */
1628 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1629 abi_ulong target_addrlen_addr)
1631 socklen_t addrlen;
1632 void *addr;
1633 abi_long ret;
1635 if (get_user_u32(addrlen, target_addrlen_addr))
1636 return -TARGET_EFAULT;
1638 if (addrlen < 0)
1639 return -TARGET_EINVAL;
1641 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1642 return -TARGET_EFAULT;
1644 addr = alloca(addrlen);
1646 ret = get_errno(getpeername(fd, addr, &addrlen));
1647 if (!is_error(ret)) {
1648 host_to_target_sockaddr(target_addr, addr, addrlen);
1649 if (put_user_u32(addrlen, target_addrlen_addr))
1650 ret = -TARGET_EFAULT;
1652 return ret;
1655 /* do_getsockname() Must return target values and target errnos. */
1656 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1657 abi_ulong target_addrlen_addr)
1659 socklen_t addrlen;
1660 void *addr;
1661 abi_long ret;
1663 if (get_user_u32(addrlen, target_addrlen_addr))
1664 return -TARGET_EFAULT;
1666 if (addrlen < 0)
1667 return -TARGET_EINVAL;
1669 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1670 return -TARGET_EFAULT;
1672 addr = alloca(addrlen);
1674 ret = get_errno(getsockname(fd, addr, &addrlen));
1675 if (!is_error(ret)) {
1676 host_to_target_sockaddr(target_addr, addr, addrlen);
1677 if (put_user_u32(addrlen, target_addrlen_addr))
1678 ret = -TARGET_EFAULT;
1680 return ret;
1683 /* do_socketpair() Must return target values and target errnos. */
1684 static abi_long do_socketpair(int domain, int type, int protocol,
1685 abi_ulong target_tab_addr)
1687 int tab[2];
1688 abi_long ret;
1690 ret = get_errno(socketpair(domain, type, protocol, tab));
1691 if (!is_error(ret)) {
1692 if (put_user_s32(tab[0], target_tab_addr)
1693 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1694 ret = -TARGET_EFAULT;
1696 return ret;
1699 /* do_sendto() Must return target values and target errnos. */
1700 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1701 abi_ulong target_addr, socklen_t addrlen)
1703 void *addr;
1704 void *host_msg;
1705 abi_long ret;
1707 if (addrlen < 0)
1708 return -TARGET_EINVAL;
1710 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1711 if (!host_msg)
1712 return -TARGET_EFAULT;
1713 if (target_addr) {
1714 addr = alloca(addrlen);
1715 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1716 if (ret) {
1717 unlock_user(host_msg, msg, 0);
1718 return ret;
1720 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1721 } else {
1722 ret = get_errno(send(fd, host_msg, len, flags));
1724 unlock_user(host_msg, msg, 0);
1725 return ret;
1728 /* do_recvfrom() Must return target values and target errnos. */
1729 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1730 abi_ulong target_addr,
1731 abi_ulong target_addrlen)
1733 socklen_t addrlen;
1734 void *addr;
1735 void *host_msg;
1736 abi_long ret;
1738 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1739 if (!host_msg)
1740 return -TARGET_EFAULT;
1741 if (target_addr) {
1742 if (get_user_u32(addrlen, target_addrlen)) {
1743 ret = -TARGET_EFAULT;
1744 goto fail;
1746 if (addrlen < 0) {
1747 ret = -TARGET_EINVAL;
1748 goto fail;
1750 addr = alloca(addrlen);
1751 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1752 } else {
1753 addr = NULL; /* To keep compiler quiet. */
1754 ret = get_errno(recv(fd, host_msg, len, flags));
1756 if (!is_error(ret)) {
1757 if (target_addr) {
1758 host_to_target_sockaddr(target_addr, addr, addrlen);
1759 if (put_user_u32(addrlen, target_addrlen)) {
1760 ret = -TARGET_EFAULT;
1761 goto fail;
1764 unlock_user(host_msg, msg, len);
1765 } else {
1766 fail:
1767 unlock_user(host_msg, msg, 0);
1769 return ret;
1772 #ifdef TARGET_NR_socketcall
1773 /* do_socketcall() Must return target values and target errnos. */
1774 static abi_long do_socketcall(int num, abi_ulong vptr)
1776 abi_long ret;
1777 const int n = sizeof(abi_ulong);
1779 switch(num) {
1780 case SOCKOP_socket:
1782 abi_ulong domain, type, protocol;
1784 if (get_user_ual(domain, vptr)
1785 || get_user_ual(type, vptr + n)
1786 || get_user_ual(protocol, vptr + 2 * n))
1787 return -TARGET_EFAULT;
1789 ret = do_socket(domain, type, protocol);
1791 break;
1792 case SOCKOP_bind:
1794 abi_ulong sockfd;
1795 abi_ulong target_addr;
1796 socklen_t addrlen;
1798 if (get_user_ual(sockfd, vptr)
1799 || get_user_ual(target_addr, vptr + n)
1800 || get_user_ual(addrlen, vptr + 2 * n))
1801 return -TARGET_EFAULT;
1803 ret = do_bind(sockfd, target_addr, addrlen);
1805 break;
1806 case SOCKOP_connect:
1808 abi_ulong sockfd;
1809 abi_ulong target_addr;
1810 socklen_t addrlen;
1812 if (get_user_ual(sockfd, vptr)
1813 || get_user_ual(target_addr, vptr + n)
1814 || get_user_ual(addrlen, vptr + 2 * n))
1815 return -TARGET_EFAULT;
1817 ret = do_connect(sockfd, target_addr, addrlen);
1819 break;
1820 case SOCKOP_listen:
1822 abi_ulong sockfd, backlog;
1824 if (get_user_ual(sockfd, vptr)
1825 || get_user_ual(backlog, vptr + n))
1826 return -TARGET_EFAULT;
1828 ret = get_errno(listen(sockfd, backlog));
1830 break;
1831 case SOCKOP_accept:
1833 abi_ulong sockfd;
1834 abi_ulong target_addr, target_addrlen;
1836 if (get_user_ual(sockfd, vptr)
1837 || get_user_ual(target_addr, vptr + n)
1838 || get_user_ual(target_addrlen, vptr + 2 * n))
1839 return -TARGET_EFAULT;
1841 ret = do_accept(sockfd, target_addr, target_addrlen);
1843 break;
1844 case SOCKOP_getsockname:
1846 abi_ulong sockfd;
1847 abi_ulong target_addr, target_addrlen;
1849 if (get_user_ual(sockfd, vptr)
1850 || get_user_ual(target_addr, vptr + n)
1851 || get_user_ual(target_addrlen, vptr + 2 * n))
1852 return -TARGET_EFAULT;
1854 ret = do_getsockname(sockfd, target_addr, target_addrlen);
1856 break;
1857 case SOCKOP_getpeername:
1859 abi_ulong sockfd;
1860 abi_ulong target_addr, target_addrlen;
1862 if (get_user_ual(sockfd, vptr)
1863 || get_user_ual(target_addr, vptr + n)
1864 || get_user_ual(target_addrlen, vptr + 2 * n))
1865 return -TARGET_EFAULT;
1867 ret = do_getpeername(sockfd, target_addr, target_addrlen);
1869 break;
1870 case SOCKOP_socketpair:
1872 abi_ulong domain, type, protocol;
1873 abi_ulong tab;
1875 if (get_user_ual(domain, vptr)
1876 || get_user_ual(type, vptr + n)
1877 || get_user_ual(protocol, vptr + 2 * n)
1878 || get_user_ual(tab, vptr + 3 * n))
1879 return -TARGET_EFAULT;
1881 ret = do_socketpair(domain, type, protocol, tab);
1883 break;
1884 case SOCKOP_send:
1886 abi_ulong sockfd;
1887 abi_ulong msg;
1888 size_t len;
1889 abi_ulong flags;
1891 if (get_user_ual(sockfd, vptr)
1892 || get_user_ual(msg, vptr + n)
1893 || get_user_ual(len, vptr + 2 * n)
1894 || get_user_ual(flags, vptr + 3 * n))
1895 return -TARGET_EFAULT;
1897 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1899 break;
1900 case SOCKOP_recv:
1902 abi_ulong sockfd;
1903 abi_ulong msg;
1904 size_t len;
1905 abi_ulong flags;
1907 if (get_user_ual(sockfd, vptr)
1908 || get_user_ual(msg, vptr + n)
1909 || get_user_ual(len, vptr + 2 * n)
1910 || get_user_ual(flags, vptr + 3 * n))
1911 return -TARGET_EFAULT;
1913 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1915 break;
1916 case SOCKOP_sendto:
1918 abi_ulong sockfd;
1919 abi_ulong msg;
1920 size_t len;
1921 abi_ulong flags;
1922 abi_ulong addr;
1923 socklen_t addrlen;
1925 if (get_user_ual(sockfd, vptr)
1926 || get_user_ual(msg, vptr + n)
1927 || get_user_ual(len, vptr + 2 * n)
1928 || get_user_ual(flags, vptr + 3 * n)
1929 || get_user_ual(addr, vptr + 4 * n)
1930 || get_user_ual(addrlen, vptr + 5 * n))
1931 return -TARGET_EFAULT;
1933 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1935 break;
1936 case SOCKOP_recvfrom:
1938 abi_ulong sockfd;
1939 abi_ulong msg;
1940 size_t len;
1941 abi_ulong flags;
1942 abi_ulong addr;
1943 socklen_t addrlen;
1945 if (get_user_ual(sockfd, vptr)
1946 || get_user_ual(msg, vptr + n)
1947 || get_user_ual(len, vptr + 2 * n)
1948 || get_user_ual(flags, vptr + 3 * n)
1949 || get_user_ual(addr, vptr + 4 * n)
1950 || get_user_ual(addrlen, vptr + 5 * n))
1951 return -TARGET_EFAULT;
1953 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1955 break;
1956 case SOCKOP_shutdown:
1958 abi_ulong sockfd, how;
1960 if (get_user_ual(sockfd, vptr)
1961 || get_user_ual(how, vptr + n))
1962 return -TARGET_EFAULT;
1964 ret = get_errno(shutdown(sockfd, how));
1966 break;
1967 case SOCKOP_sendmsg:
1968 case SOCKOP_recvmsg:
1970 abi_ulong fd;
1971 abi_ulong target_msg;
1972 abi_ulong flags;
1974 if (get_user_ual(fd, vptr)
1975 || get_user_ual(target_msg, vptr + n)
1976 || get_user_ual(flags, vptr + 2 * n))
1977 return -TARGET_EFAULT;
1979 ret = do_sendrecvmsg(fd, target_msg, flags,
1980 (num == SOCKOP_sendmsg));
1982 break;
1983 case SOCKOP_setsockopt:
1985 abi_ulong sockfd;
1986 abi_ulong level;
1987 abi_ulong optname;
1988 abi_ulong optval;
1989 socklen_t optlen;
1991 if (get_user_ual(sockfd, vptr)
1992 || get_user_ual(level, vptr + n)
1993 || get_user_ual(optname, vptr + 2 * n)
1994 || get_user_ual(optval, vptr + 3 * n)
1995 || get_user_ual(optlen, vptr + 4 * n))
1996 return -TARGET_EFAULT;
1998 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2000 break;
2001 case SOCKOP_getsockopt:
2003 abi_ulong sockfd;
2004 abi_ulong level;
2005 abi_ulong optname;
2006 abi_ulong optval;
2007 socklen_t optlen;
2009 if (get_user_ual(sockfd, vptr)
2010 || get_user_ual(level, vptr + n)
2011 || get_user_ual(optname, vptr + 2 * n)
2012 || get_user_ual(optval, vptr + 3 * n)
2013 || get_user_ual(optlen, vptr + 4 * n))
2014 return -TARGET_EFAULT;
2016 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
2018 break;
2019 default:
2020 gemu_log("Unsupported socketcall: %d\n", num);
2021 ret = -TARGET_ENOSYS;
2022 break;
2024 return ret;
2026 #endif
2028 #define N_SHM_REGIONS 32
2030 static struct shm_region {
2031 abi_ulong start;
2032 abi_ulong size;
2033 } shm_regions[N_SHM_REGIONS];
2035 struct target_ipc_perm
2037 abi_long __key;
2038 abi_ulong uid;
2039 abi_ulong gid;
2040 abi_ulong cuid;
2041 abi_ulong cgid;
2042 unsigned short int mode;
2043 unsigned short int __pad1;
2044 unsigned short int __seq;
2045 unsigned short int __pad2;
2046 abi_ulong __unused1;
2047 abi_ulong __unused2;
2050 struct target_semid_ds
2052 struct target_ipc_perm sem_perm;
2053 abi_ulong sem_otime;
2054 abi_ulong __unused1;
2055 abi_ulong sem_ctime;
2056 abi_ulong __unused2;
2057 abi_ulong sem_nsems;
2058 abi_ulong __unused3;
2059 abi_ulong __unused4;
2062 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2063 abi_ulong target_addr)
2065 struct target_ipc_perm *target_ip;
2066 struct target_semid_ds *target_sd;
2068 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2069 return -TARGET_EFAULT;
2070 target_ip=&(target_sd->sem_perm);
2071 host_ip->__key = tswapl(target_ip->__key);
2072 host_ip->uid = tswapl(target_ip->uid);
2073 host_ip->gid = tswapl(target_ip->gid);
2074 host_ip->cuid = tswapl(target_ip->cuid);
2075 host_ip->cgid = tswapl(target_ip->cgid);
2076 host_ip->mode = tswapl(target_ip->mode);
2077 unlock_user_struct(target_sd, target_addr, 0);
2078 return 0;
2081 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2082 struct ipc_perm *host_ip)
2084 struct target_ipc_perm *target_ip;
2085 struct target_semid_ds *target_sd;
2087 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2088 return -TARGET_EFAULT;
2089 target_ip = &(target_sd->sem_perm);
2090 target_ip->__key = tswapl(host_ip->__key);
2091 target_ip->uid = tswapl(host_ip->uid);
2092 target_ip->gid = tswapl(host_ip->gid);
2093 target_ip->cuid = tswapl(host_ip->cuid);
2094 target_ip->cgid = tswapl(host_ip->cgid);
2095 target_ip->mode = tswapl(host_ip->mode);
2096 unlock_user_struct(target_sd, target_addr, 1);
2097 return 0;
2100 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2101 abi_ulong target_addr)
2103 struct target_semid_ds *target_sd;
2105 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2106 return -TARGET_EFAULT;
2107 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2108 return -TARGET_EFAULT;
2109 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2110 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2111 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2112 unlock_user_struct(target_sd, target_addr, 0);
2113 return 0;
2116 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2117 struct semid_ds *host_sd)
2119 struct target_semid_ds *target_sd;
2121 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2122 return -TARGET_EFAULT;
2123 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2124 return -TARGET_EFAULT;;
2125 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2126 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2127 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2128 unlock_user_struct(target_sd, target_addr, 1);
2129 return 0;
2132 struct target_seminfo {
2133 int semmap;
2134 int semmni;
2135 int semmns;
2136 int semmnu;
2137 int semmsl;
2138 int semopm;
2139 int semume;
2140 int semusz;
2141 int semvmx;
2142 int semaem;
2145 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2146 struct seminfo *host_seminfo)
2148 struct target_seminfo *target_seminfo;
2149 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2150 return -TARGET_EFAULT;
2151 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2152 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2153 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2154 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2155 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2156 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2157 __put_user(host_seminfo->semume, &target_seminfo->semume);
2158 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2159 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2160 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2161 unlock_user_struct(target_seminfo, target_addr, 1);
2162 return 0;
2165 union semun {
2166 int val;
2167 struct semid_ds *buf;
2168 unsigned short *array;
2169 struct seminfo *__buf;
2172 union target_semun {
2173 int val;
2174 abi_ulong buf;
2175 abi_ulong array;
2176 abi_ulong __buf;
2179 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2180 abi_ulong target_addr)
2182 int nsems;
2183 unsigned short *array;
2184 union semun semun;
2185 struct semid_ds semid_ds;
2186 int i, ret;
2188 semun.buf = &semid_ds;
2190 ret = semctl(semid, 0, IPC_STAT, semun);
2191 if (ret == -1)
2192 return get_errno(ret);
2194 nsems = semid_ds.sem_nsems;
2196 *host_array = malloc(nsems*sizeof(unsigned short));
2197 array = lock_user(VERIFY_READ, target_addr,
2198 nsems*sizeof(unsigned short), 1);
2199 if (!array)
2200 return -TARGET_EFAULT;
2202 for(i=0; i<nsems; i++) {
2203 __get_user((*host_array)[i], &array[i]);
2205 unlock_user(array, target_addr, 0);
2207 return 0;
2210 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2211 unsigned short **host_array)
2213 int nsems;
2214 unsigned short *array;
2215 union semun semun;
2216 struct semid_ds semid_ds;
2217 int i, ret;
2219 semun.buf = &semid_ds;
2221 ret = semctl(semid, 0, IPC_STAT, semun);
2222 if (ret == -1)
2223 return get_errno(ret);
2225 nsems = semid_ds.sem_nsems;
2227 array = lock_user(VERIFY_WRITE, target_addr,
2228 nsems*sizeof(unsigned short), 0);
2229 if (!array)
2230 return -TARGET_EFAULT;
2232 for(i=0; i<nsems; i++) {
2233 __put_user((*host_array)[i], &array[i]);
2235 free(*host_array);
2236 unlock_user(array, target_addr, 1);
2238 return 0;
2241 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2242 union target_semun target_su)
2244 union semun arg;
2245 struct semid_ds dsarg;
2246 unsigned short *array = NULL;
2247 struct seminfo seminfo;
2248 abi_long ret = -TARGET_EINVAL;
2249 abi_long err;
2250 cmd &= 0xff;
2252 switch( cmd ) {
2253 case GETVAL:
2254 case SETVAL:
2255 arg.val = tswapl(target_su.val);
2256 ret = get_errno(semctl(semid, semnum, cmd, arg));
2257 target_su.val = tswapl(arg.val);
2258 break;
2259 case GETALL:
2260 case SETALL:
2261 err = target_to_host_semarray(semid, &array, target_su.array);
2262 if (err)
2263 return err;
2264 arg.array = array;
2265 ret = get_errno(semctl(semid, semnum, cmd, arg));
2266 err = host_to_target_semarray(semid, target_su.array, &array);
2267 if (err)
2268 return err;
2269 break;
2270 case IPC_STAT:
2271 case IPC_SET:
2272 case SEM_STAT:
2273 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2274 if (err)
2275 return err;
2276 arg.buf = &dsarg;
2277 ret = get_errno(semctl(semid, semnum, cmd, arg));
2278 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2279 if (err)
2280 return err;
2281 break;
2282 case IPC_INFO:
2283 case SEM_INFO:
2284 arg.__buf = &seminfo;
2285 ret = get_errno(semctl(semid, semnum, cmd, arg));
2286 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2287 if (err)
2288 return err;
2289 break;
2290 case IPC_RMID:
2291 case GETPID:
2292 case GETNCNT:
2293 case GETZCNT:
2294 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2295 break;
2298 return ret;
2301 struct target_sembuf {
2302 unsigned short sem_num;
2303 short sem_op;
2304 short sem_flg;
2307 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2308 abi_ulong target_addr,
2309 unsigned nsops)
2311 struct target_sembuf *target_sembuf;
2312 int i;
2314 target_sembuf = lock_user(VERIFY_READ, target_addr,
2315 nsops*sizeof(struct target_sembuf), 1);
2316 if (!target_sembuf)
2317 return -TARGET_EFAULT;
2319 for(i=0; i<nsops; i++) {
2320 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2321 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2322 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2325 unlock_user(target_sembuf, target_addr, 0);
2327 return 0;
2330 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2332 struct sembuf sops[nsops];
2334 if (target_to_host_sembuf(sops, ptr, nsops))
2335 return -TARGET_EFAULT;
2337 return semop(semid, sops, nsops);
2340 struct target_msqid_ds
2342 struct target_ipc_perm msg_perm;
2343 abi_ulong msg_stime;
2344 #if TARGET_ABI_BITS == 32
2345 abi_ulong __unused1;
2346 #endif
2347 abi_ulong msg_rtime;
2348 #if TARGET_ABI_BITS == 32
2349 abi_ulong __unused2;
2350 #endif
2351 abi_ulong msg_ctime;
2352 #if TARGET_ABI_BITS == 32
2353 abi_ulong __unused3;
2354 #endif
2355 abi_ulong __msg_cbytes;
2356 abi_ulong msg_qnum;
2357 abi_ulong msg_qbytes;
2358 abi_ulong msg_lspid;
2359 abi_ulong msg_lrpid;
2360 abi_ulong __unused4;
2361 abi_ulong __unused5;
2364 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2365 abi_ulong target_addr)
2367 struct target_msqid_ds *target_md;
2369 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2370 return -TARGET_EFAULT;
2371 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2372 return -TARGET_EFAULT;
2373 host_md->msg_stime = tswapl(target_md->msg_stime);
2374 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2375 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2376 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2377 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2378 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2379 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2380 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2381 unlock_user_struct(target_md, target_addr, 0);
2382 return 0;
2385 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2386 struct msqid_ds *host_md)
2388 struct target_msqid_ds *target_md;
2390 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2391 return -TARGET_EFAULT;
2392 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2393 return -TARGET_EFAULT;
2394 target_md->msg_stime = tswapl(host_md->msg_stime);
2395 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2396 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2397 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2398 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2399 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2400 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2401 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2402 unlock_user_struct(target_md, target_addr, 1);
2403 return 0;
2406 struct target_msginfo {
2407 int msgpool;
2408 int msgmap;
2409 int msgmax;
2410 int msgmnb;
2411 int msgmni;
2412 int msgssz;
2413 int msgtql;
2414 unsigned short int msgseg;
2417 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2418 struct msginfo *host_msginfo)
2420 struct target_msginfo *target_msginfo;
2421 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2422 return -TARGET_EFAULT;
2423 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2424 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2425 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2426 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2427 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2428 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2429 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2430 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2431 unlock_user_struct(target_msginfo, target_addr, 1);
2432 return 0;
2435 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
2437 struct msqid_ds dsarg;
2438 struct msginfo msginfo;
2439 abi_long ret = -TARGET_EINVAL;
2441 cmd &= 0xff;
2443 switch (cmd) {
2444 case IPC_STAT:
2445 case IPC_SET:
2446 case MSG_STAT:
2447 if (target_to_host_msqid_ds(&dsarg,ptr))
2448 return -TARGET_EFAULT;
2449 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2450 if (host_to_target_msqid_ds(ptr,&dsarg))
2451 return -TARGET_EFAULT;
2452 break;
2453 case IPC_RMID:
2454 ret = get_errno(msgctl(msgid, cmd, NULL));
2455 break;
2456 case IPC_INFO:
2457 case MSG_INFO:
2458 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2459 if (host_to_target_msginfo(ptr, &msginfo))
2460 return -TARGET_EFAULT;
2461 break;
2464 return ret;
2467 struct target_msgbuf {
2468 abi_long mtype;
2469 char mtext[1];
2472 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2473 unsigned int msgsz, int msgflg)
2475 struct target_msgbuf *target_mb;
2476 struct msgbuf *host_mb;
2477 abi_long ret = 0;
2479 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2480 return -TARGET_EFAULT;
2481 host_mb = malloc(msgsz+sizeof(long));
2482 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2483 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
2484 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2485 free(host_mb);
2486 unlock_user_struct(target_mb, msgp, 0);
2488 return ret;
2491 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2492 unsigned int msgsz, abi_long msgtyp,
2493 int msgflg)
2495 struct target_msgbuf *target_mb;
2496 char *target_mtext;
2497 struct msgbuf *host_mb;
2498 abi_long ret = 0;
2500 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2501 return -TARGET_EFAULT;
2503 host_mb = malloc(msgsz+sizeof(long));
2504 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2506 if (ret > 0) {
2507 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2508 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2509 if (!target_mtext) {
2510 ret = -TARGET_EFAULT;
2511 goto end;
2513 memcpy(target_mb->mtext, host_mb->mtext, ret);
2514 unlock_user(target_mtext, target_mtext_addr, ret);
2517 target_mb->mtype = tswapl(host_mb->mtype);
2518 free(host_mb);
2520 end:
2521 if (target_mb)
2522 unlock_user_struct(target_mb, msgp, 1);
2523 return ret;
2526 struct target_shmid_ds
2528 struct target_ipc_perm shm_perm;
2529 abi_ulong shm_segsz;
2530 abi_ulong shm_atime;
2531 #if TARGET_ABI_BITS == 32
2532 abi_ulong __unused1;
2533 #endif
2534 abi_ulong shm_dtime;
2535 #if TARGET_ABI_BITS == 32
2536 abi_ulong __unused2;
2537 #endif
2538 abi_ulong shm_ctime;
2539 #if TARGET_ABI_BITS == 32
2540 abi_ulong __unused3;
2541 #endif
2542 int shm_cpid;
2543 int shm_lpid;
2544 abi_ulong shm_nattch;
2545 unsigned long int __unused4;
2546 unsigned long int __unused5;
2549 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2550 abi_ulong target_addr)
2552 struct target_shmid_ds *target_sd;
2554 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2555 return -TARGET_EFAULT;
2556 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2557 return -TARGET_EFAULT;
2558 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2559 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2560 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2561 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2562 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2563 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2564 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2565 unlock_user_struct(target_sd, target_addr, 0);
2566 return 0;
2569 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2570 struct shmid_ds *host_sd)
2572 struct target_shmid_ds *target_sd;
2574 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2575 return -TARGET_EFAULT;
2576 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2577 return -TARGET_EFAULT;
2578 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2579 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2580 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2581 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2582 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2583 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2584 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2585 unlock_user_struct(target_sd, target_addr, 1);
2586 return 0;
2589 struct target_shminfo {
2590 abi_ulong shmmax;
2591 abi_ulong shmmin;
2592 abi_ulong shmmni;
2593 abi_ulong shmseg;
2594 abi_ulong shmall;
2597 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2598 struct shminfo *host_shminfo)
2600 struct target_shminfo *target_shminfo;
2601 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2602 return -TARGET_EFAULT;
2603 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2604 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2605 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2606 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2607 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2608 unlock_user_struct(target_shminfo, target_addr, 1);
2609 return 0;
2612 struct target_shm_info {
2613 int used_ids;
2614 abi_ulong shm_tot;
2615 abi_ulong shm_rss;
2616 abi_ulong shm_swp;
2617 abi_ulong swap_attempts;
2618 abi_ulong swap_successes;
2621 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2622 struct shm_info *host_shm_info)
2624 struct target_shm_info *target_shm_info;
2625 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2626 return -TARGET_EFAULT;
2627 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2628 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2629 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2630 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2631 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2632 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2633 unlock_user_struct(target_shm_info, target_addr, 1);
2634 return 0;
2637 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2639 struct shmid_ds dsarg;
2640 struct shminfo shminfo;
2641 struct shm_info shm_info;
2642 abi_long ret = -TARGET_EINVAL;
2644 cmd &= 0xff;
2646 switch(cmd) {
2647 case IPC_STAT:
2648 case IPC_SET:
2649 case SHM_STAT:
2650 if (target_to_host_shmid_ds(&dsarg, buf))
2651 return -TARGET_EFAULT;
2652 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2653 if (host_to_target_shmid_ds(buf, &dsarg))
2654 return -TARGET_EFAULT;
2655 break;
2656 case IPC_INFO:
2657 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2658 if (host_to_target_shminfo(buf, &shminfo))
2659 return -TARGET_EFAULT;
2660 break;
2661 case SHM_INFO:
2662 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2663 if (host_to_target_shm_info(buf, &shm_info))
2664 return -TARGET_EFAULT;
2665 break;
2666 case IPC_RMID:
2667 case SHM_LOCK:
2668 case SHM_UNLOCK:
2669 ret = get_errno(shmctl(shmid, cmd, NULL));
2670 break;
2673 return ret;
2676 static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2678 abi_long raddr;
2679 void *host_raddr;
2680 struct shmid_ds shm_info;
2681 int i,ret;
2683 /* find out the length of the shared memory segment */
2684 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2685 if (is_error(ret)) {
2686 /* can't get length, bail out */
2687 return ret;
2690 mmap_lock();
2692 if (shmaddr)
2693 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2694 else {
2695 abi_ulong mmap_start;
2697 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2699 if (mmap_start == -1) {
2700 errno = ENOMEM;
2701 host_raddr = (void *)-1;
2702 } else
2703 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2706 if (host_raddr == (void *)-1) {
2707 mmap_unlock();
2708 return get_errno((long)host_raddr);
2710 raddr=h2g((unsigned long)host_raddr);
2712 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2713 PAGE_VALID | PAGE_READ |
2714 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2716 for (i = 0; i < N_SHM_REGIONS; i++) {
2717 if (shm_regions[i].start == 0) {
2718 shm_regions[i].start = raddr;
2719 shm_regions[i].size = shm_info.shm_segsz;
2720 break;
2724 mmap_unlock();
2725 return raddr;
2729 static inline abi_long do_shmdt(abi_ulong shmaddr)
2731 int i;
2733 for (i = 0; i < N_SHM_REGIONS; ++i) {
2734 if (shm_regions[i].start == shmaddr) {
2735 shm_regions[i].start = 0;
2736 page_set_flags(shmaddr, shm_regions[i].size, 0);
2737 break;
2741 return get_errno(shmdt(g2h(shmaddr)));
2744 #ifdef TARGET_NR_ipc
2745 /* ??? This only works with linear mappings. */
2746 /* do_ipc() must return target values and target errnos. */
2747 static abi_long do_ipc(unsigned int call, int first,
2748 int second, int third,
2749 abi_long ptr, abi_long fifth)
2751 int version;
2752 abi_long ret = 0;
2754 version = call >> 16;
2755 call &= 0xffff;
2757 switch (call) {
2758 case IPCOP_semop:
2759 ret = do_semop(first, ptr, second);
2760 break;
2762 case IPCOP_semget:
2763 ret = get_errno(semget(first, second, third));
2764 break;
2766 case IPCOP_semctl:
2767 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
2768 break;
2770 case IPCOP_msgget:
2771 ret = get_errno(msgget(first, second));
2772 break;
2774 case IPCOP_msgsnd:
2775 ret = do_msgsnd(first, ptr, second, third);
2776 break;
2778 case IPCOP_msgctl:
2779 ret = do_msgctl(first, second, ptr);
2780 break;
2782 case IPCOP_msgrcv:
2783 switch (version) {
2784 case 0:
2786 struct target_ipc_kludge {
2787 abi_long msgp;
2788 abi_long msgtyp;
2789 } *tmp;
2791 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2792 ret = -TARGET_EFAULT;
2793 break;
2796 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2798 unlock_user_struct(tmp, ptr, 0);
2799 break;
2801 default:
2802 ret = do_msgrcv(first, ptr, second, fifth, third);
2804 break;
2806 case IPCOP_shmat:
2807 switch (version) {
2808 default:
2810 abi_ulong raddr;
2811 raddr = do_shmat(first, ptr, second);
2812 if (is_error(raddr))
2813 return get_errno(raddr);
2814 if (put_user_ual(raddr, third))
2815 return -TARGET_EFAULT;
2816 break;
2818 case 1:
2819 ret = -TARGET_EINVAL;
2820 break;
2822 break;
2823 case IPCOP_shmdt:
2824 ret = do_shmdt(ptr);
2825 break;
2827 case IPCOP_shmget:
2828 /* IPC_* flag values are the same on all linux platforms */
2829 ret = get_errno(shmget(first, second, third));
2830 break;
2832 /* IPC_* and SHM_* command values are the same on all linux platforms */
2833 case IPCOP_shmctl:
2834 ret = do_shmctl(first, second, third);
2835 break;
2836 default:
2837 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2838 ret = -TARGET_ENOSYS;
2839 break;
2841 return ret;
2843 #endif
2845 /* kernel structure types definitions */
2846 #define IFNAMSIZ 16
2848 #define STRUCT(name, ...) STRUCT_ ## name,
2849 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2850 enum {
2851 #include "syscall_types.h"
2853 #undef STRUCT
2854 #undef STRUCT_SPECIAL
2856 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
2857 #define STRUCT_SPECIAL(name)
2858 #include "syscall_types.h"
2859 #undef STRUCT
2860 #undef STRUCT_SPECIAL
2862 typedef struct IOCTLEntry {
2863 unsigned int target_cmd;
2864 unsigned int host_cmd;
2865 const char *name;
2866 int access;
2867 const argtype arg_type[5];
2868 } IOCTLEntry;
2870 #define IOC_R 0x0001
2871 #define IOC_W 0x0002
2872 #define IOC_RW (IOC_R | IOC_W)
2874 #define MAX_STRUCT_SIZE 4096
2876 static IOCTLEntry ioctl_entries[] = {
2877 #define IOCTL(cmd, access, ...) \
2878 { TARGET_ ## cmd, cmd, #cmd, access, { __VA_ARGS__ } },
2879 #include "ioctls.h"
2880 { 0, 0, },
2883 /* ??? Implement proper locking for ioctls. */
2884 /* do_ioctl() Must return target values and target errnos. */
2885 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
2887 const IOCTLEntry *ie;
2888 const argtype *arg_type;
2889 abi_long ret;
2890 uint8_t buf_temp[MAX_STRUCT_SIZE];
2891 int target_size;
2892 void *argptr;
2894 ie = ioctl_entries;
2895 for(;;) {
2896 if (ie->target_cmd == 0) {
2897 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
2898 return -TARGET_ENOSYS;
2900 if (ie->target_cmd == cmd)
2901 break;
2902 ie++;
2904 arg_type = ie->arg_type;
2905 #if defined(DEBUG)
2906 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2907 #endif
2908 switch(arg_type[0]) {
2909 case TYPE_NULL:
2910 /* no argument */
2911 ret = get_errno(ioctl(fd, ie->host_cmd));
2912 break;
2913 case TYPE_PTRVOID:
2914 case TYPE_INT:
2915 /* int argment */
2916 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2917 break;
2918 case TYPE_PTR:
2919 arg_type++;
2920 target_size = thunk_type_size(arg_type, 0);
2921 switch(ie->access) {
2922 case IOC_R:
2923 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2924 if (!is_error(ret)) {
2925 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2926 if (!argptr)
2927 return -TARGET_EFAULT;
2928 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2929 unlock_user(argptr, arg, target_size);
2931 break;
2932 case IOC_W:
2933 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2934 if (!argptr)
2935 return -TARGET_EFAULT;
2936 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2937 unlock_user(argptr, arg, 0);
2938 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2939 break;
2940 default:
2941 case IOC_RW:
2942 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2943 if (!argptr)
2944 return -TARGET_EFAULT;
2945 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2946 unlock_user(argptr, arg, 0);
2947 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2948 if (!is_error(ret)) {
2949 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2950 if (!argptr)
2951 return -TARGET_EFAULT;
2952 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2953 unlock_user(argptr, arg, target_size);
2955 break;
2957 break;
2958 default:
2959 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2960 (long)cmd, arg_type[0]);
2961 ret = -TARGET_ENOSYS;
2962 break;
2964 return ret;
2967 static const bitmask_transtbl iflag_tbl[] = {
2968 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2969 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2970 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2971 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2972 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2973 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2974 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2975 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2976 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2977 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2978 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2979 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2980 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2981 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2982 { 0, 0, 0, 0 }
2985 static const bitmask_transtbl oflag_tbl[] = {
2986 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2987 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2988 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2989 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2990 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2991 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2992 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2993 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2994 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2995 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2996 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2997 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2998 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2999 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3000 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3001 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3002 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3003 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3004 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3005 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3006 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3007 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3008 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3009 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3010 { 0, 0, 0, 0 }
3013 static const bitmask_transtbl cflag_tbl[] = {
3014 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3015 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3016 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3017 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3018 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3019 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3020 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3021 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3022 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3023 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3024 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3025 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3026 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3027 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3028 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3029 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3030 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3031 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3032 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3033 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3034 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3035 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3036 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3037 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3038 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3039 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3040 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3041 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3042 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3043 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3044 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3045 { 0, 0, 0, 0 }
3048 static const bitmask_transtbl lflag_tbl[] = {
3049 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3050 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3051 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3052 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3053 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3054 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3055 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3056 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3057 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3058 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3059 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3060 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3061 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3062 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3063 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3064 { 0, 0, 0, 0 }
3067 static void target_to_host_termios (void *dst, const void *src)
3069 struct host_termios *host = dst;
3070 const struct target_termios *target = src;
3072 host->c_iflag =
3073 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
3074 host->c_oflag =
3075 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
3076 host->c_cflag =
3077 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
3078 host->c_lflag =
3079 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3080 host->c_line = target->c_line;
3082 memset(host->c_cc, 0, sizeof(host->c_cc));
3083 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3084 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3085 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
3086 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3087 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
3088 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3089 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
3090 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3091 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
3092 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3093 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3094 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3095 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3096 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3097 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3098 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
3099 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
3102 static void host_to_target_termios (void *dst, const void *src)
3104 struct target_termios *target = dst;
3105 const struct host_termios *host = src;
3107 target->c_iflag =
3108 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
3109 target->c_oflag =
3110 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
3111 target->c_cflag =
3112 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
3113 target->c_lflag =
3114 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3115 target->c_line = host->c_line;
3117 memset(target->c_cc, 0, sizeof(target->c_cc));
3118 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3119 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3120 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3121 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3122 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3123 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3124 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3125 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3126 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3127 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3128 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3129 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3130 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3131 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3132 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3133 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3134 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3137 static const StructEntry struct_termios_def = {
3138 .convert = { host_to_target_termios, target_to_host_termios },
3139 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3140 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3143 static bitmask_transtbl mmap_flags_tbl[] = {
3144 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3145 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3146 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3147 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3148 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3149 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3150 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3151 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3152 { 0, 0, 0, 0 }
3155 #if defined(TARGET_I386)
3157 /* NOTE: there is really one LDT for all the threads */
3158 static uint8_t *ldt_table;
3160 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
3162 int size;
3163 void *p;
3165 if (!ldt_table)
3166 return 0;
3167 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3168 if (size > bytecount)
3169 size = bytecount;
3170 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3171 if (!p)
3172 return -TARGET_EFAULT;
3173 /* ??? Should this by byteswapped? */
3174 memcpy(p, ldt_table, size);
3175 unlock_user(p, ptr, size);
3176 return size;
3179 /* XXX: add locking support */
3180 static abi_long write_ldt(CPUX86State *env,
3181 abi_ulong ptr, unsigned long bytecount, int oldmode)
3183 struct target_modify_ldt_ldt_s ldt_info;
3184 struct target_modify_ldt_ldt_s *target_ldt_info;
3185 int seg_32bit, contents, read_exec_only, limit_in_pages;
3186 int seg_not_present, useable, lm;
3187 uint32_t *lp, entry_1, entry_2;
3189 if (bytecount != sizeof(ldt_info))
3190 return -TARGET_EINVAL;
3191 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
3192 return -TARGET_EFAULT;
3193 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3194 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3195 ldt_info.limit = tswap32(target_ldt_info->limit);
3196 ldt_info.flags = tswap32(target_ldt_info->flags);
3197 unlock_user_struct(target_ldt_info, ptr, 0);
3199 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
3200 return -TARGET_EINVAL;
3201 seg_32bit = ldt_info.flags & 1;
3202 contents = (ldt_info.flags >> 1) & 3;
3203 read_exec_only = (ldt_info.flags >> 3) & 1;
3204 limit_in_pages = (ldt_info.flags >> 4) & 1;
3205 seg_not_present = (ldt_info.flags >> 5) & 1;
3206 useable = (ldt_info.flags >> 6) & 1;
3207 #ifdef TARGET_ABI32
3208 lm = 0;
3209 #else
3210 lm = (ldt_info.flags >> 7) & 1;
3211 #endif
3212 if (contents == 3) {
3213 if (oldmode)
3214 return -TARGET_EINVAL;
3215 if (seg_not_present == 0)
3216 return -TARGET_EINVAL;
3218 /* allocate the LDT */
3219 if (!ldt_table) {
3220 env->ldt.base = target_mmap(0,
3221 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3222 PROT_READ|PROT_WRITE,
3223 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3224 if (env->ldt.base == -1)
3225 return -TARGET_ENOMEM;
3226 memset(g2h(env->ldt.base), 0,
3227 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
3228 env->ldt.limit = 0xffff;
3229 ldt_table = g2h(env->ldt.base);
3232 /* NOTE: same code as Linux kernel */
3233 /* Allow LDTs to be cleared by the user. */
3234 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3235 if (oldmode ||
3236 (contents == 0 &&
3237 read_exec_only == 1 &&
3238 seg_32bit == 0 &&
3239 limit_in_pages == 0 &&
3240 seg_not_present == 1 &&
3241 useable == 0 )) {
3242 entry_1 = 0;
3243 entry_2 = 0;
3244 goto install;
3248 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3249 (ldt_info.limit & 0x0ffff);
3250 entry_2 = (ldt_info.base_addr & 0xff000000) |
3251 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3252 (ldt_info.limit & 0xf0000) |
3253 ((read_exec_only ^ 1) << 9) |
3254 (contents << 10) |
3255 ((seg_not_present ^ 1) << 15) |
3256 (seg_32bit << 22) |
3257 (limit_in_pages << 23) |
3258 (lm << 21) |
3259 0x7000;
3260 if (!oldmode)
3261 entry_2 |= (useable << 20);
3263 /* Install the new entry ... */
3264 install:
3265 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3266 lp[0] = tswap32(entry_1);
3267 lp[1] = tswap32(entry_2);
3268 return 0;
3271 /* specific and weird i386 syscalls */
3272 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3273 unsigned long bytecount)
3275 abi_long ret;
3277 switch (func) {
3278 case 0:
3279 ret = read_ldt(ptr, bytecount);
3280 break;
3281 case 1:
3282 ret = write_ldt(env, ptr, bytecount, 1);
3283 break;
3284 case 0x11:
3285 ret = write_ldt(env, ptr, bytecount, 0);
3286 break;
3287 default:
3288 ret = -TARGET_ENOSYS;
3289 break;
3291 return ret;
3294 #if defined(TARGET_I386) && defined(TARGET_ABI32)
3295 static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
3297 uint64_t *gdt_table = g2h(env->gdt.base);
3298 struct target_modify_ldt_ldt_s ldt_info;
3299 struct target_modify_ldt_ldt_s *target_ldt_info;
3300 int seg_32bit, contents, read_exec_only, limit_in_pages;
3301 int seg_not_present, useable, lm;
3302 uint32_t *lp, entry_1, entry_2;
3303 int i;
3305 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3306 if (!target_ldt_info)
3307 return -TARGET_EFAULT;
3308 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3309 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3310 ldt_info.limit = tswap32(target_ldt_info->limit);
3311 ldt_info.flags = tswap32(target_ldt_info->flags);
3312 if (ldt_info.entry_number == -1) {
3313 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3314 if (gdt_table[i] == 0) {
3315 ldt_info.entry_number = i;
3316 target_ldt_info->entry_number = tswap32(i);
3317 break;
3321 unlock_user_struct(target_ldt_info, ptr, 1);
3323 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3324 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3325 return -TARGET_EINVAL;
3326 seg_32bit = ldt_info.flags & 1;
3327 contents = (ldt_info.flags >> 1) & 3;
3328 read_exec_only = (ldt_info.flags >> 3) & 1;
3329 limit_in_pages = (ldt_info.flags >> 4) & 1;
3330 seg_not_present = (ldt_info.flags >> 5) & 1;
3331 useable = (ldt_info.flags >> 6) & 1;
3332 #ifdef TARGET_ABI32
3333 lm = 0;
3334 #else
3335 lm = (ldt_info.flags >> 7) & 1;
3336 #endif
3338 if (contents == 3) {
3339 if (seg_not_present == 0)
3340 return -TARGET_EINVAL;
3343 /* NOTE: same code as Linux kernel */
3344 /* Allow LDTs to be cleared by the user. */
3345 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3346 if ((contents == 0 &&
3347 read_exec_only == 1 &&
3348 seg_32bit == 0 &&
3349 limit_in_pages == 0 &&
3350 seg_not_present == 1 &&
3351 useable == 0 )) {
3352 entry_1 = 0;
3353 entry_2 = 0;
3354 goto install;
3358 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3359 (ldt_info.limit & 0x0ffff);
3360 entry_2 = (ldt_info.base_addr & 0xff000000) |
3361 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3362 (ldt_info.limit & 0xf0000) |
3363 ((read_exec_only ^ 1) << 9) |
3364 (contents << 10) |
3365 ((seg_not_present ^ 1) << 15) |
3366 (seg_32bit << 22) |
3367 (limit_in_pages << 23) |
3368 (useable << 20) |
3369 (lm << 21) |
3370 0x7000;
3372 /* Install the new entry ... */
3373 install:
3374 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3375 lp[0] = tswap32(entry_1);
3376 lp[1] = tswap32(entry_2);
3377 return 0;
3380 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
3382 struct target_modify_ldt_ldt_s *target_ldt_info;
3383 uint64_t *gdt_table = g2h(env->gdt.base);
3384 uint32_t base_addr, limit, flags;
3385 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3386 int seg_not_present, useable, lm;
3387 uint32_t *lp, entry_1, entry_2;
3389 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3390 if (!target_ldt_info)
3391 return -TARGET_EFAULT;
3392 idx = tswap32(target_ldt_info->entry_number);
3393 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3394 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3395 unlock_user_struct(target_ldt_info, ptr, 1);
3396 return -TARGET_EINVAL;
3398 lp = (uint32_t *)(gdt_table + idx);
3399 entry_1 = tswap32(lp[0]);
3400 entry_2 = tswap32(lp[1]);
3402 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3403 contents = (entry_2 >> 10) & 3;
3404 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3405 seg_32bit = (entry_2 >> 22) & 1;
3406 limit_in_pages = (entry_2 >> 23) & 1;
3407 useable = (entry_2 >> 20) & 1;
3408 #ifdef TARGET_ABI32
3409 lm = 0;
3410 #else
3411 lm = (entry_2 >> 21) & 1;
3412 #endif
3413 flags = (seg_32bit << 0) | (contents << 1) |
3414 (read_exec_only << 3) | (limit_in_pages << 4) |
3415 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3416 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3417 base_addr = (entry_1 >> 16) |
3418 (entry_2 & 0xff000000) |
3419 ((entry_2 & 0xff) << 16);
3420 target_ldt_info->base_addr = tswapl(base_addr);
3421 target_ldt_info->limit = tswap32(limit);
3422 target_ldt_info->flags = tswap32(flags);
3423 unlock_user_struct(target_ldt_info, ptr, 1);
3424 return 0;
3426 #endif /* TARGET_I386 && TARGET_ABI32 */
3428 #ifndef TARGET_ABI32
3429 static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
3431 abi_long ret;
3432 abi_ulong val;
3433 int idx;
3435 switch(code) {
3436 case TARGET_ARCH_SET_GS:
3437 case TARGET_ARCH_SET_FS:
3438 if (code == TARGET_ARCH_SET_GS)
3439 idx = R_GS;
3440 else
3441 idx = R_FS;
3442 cpu_x86_load_seg(env, idx, 0);
3443 env->segs[idx].base = addr;
3444 break;
3445 case TARGET_ARCH_GET_GS:
3446 case TARGET_ARCH_GET_FS:
3447 if (code == TARGET_ARCH_GET_GS)
3448 idx = R_GS;
3449 else
3450 idx = R_FS;
3451 val = env->segs[idx].base;
3452 if (put_user(val, addr, abi_ulong))
3453 return -TARGET_EFAULT;
3454 break;
3455 default:
3456 ret = -TARGET_EINVAL;
3457 break;
3459 return 0;
3461 #endif
3463 #endif /* defined(TARGET_I386) */
3465 #if defined(CONFIG_USE_NPTL)
3467 #define NEW_STACK_SIZE PTHREAD_STACK_MIN
3469 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3470 typedef struct {
3471 CPUState *env;
3472 pthread_mutex_t mutex;
3473 pthread_cond_t cond;
3474 pthread_t thread;
3475 uint32_t tid;
3476 abi_ulong child_tidptr;
3477 abi_ulong parent_tidptr;
3478 sigset_t sigmask;
3479 } new_thread_info;
3481 static void *clone_func(void *arg)
3483 new_thread_info *info = arg;
3484 CPUState *env;
3485 TaskState *ts;
3487 env = info->env;
3488 thread_env = env;
3489 ts = (TaskState *)thread_env->opaque;
3490 info->tid = gettid();
3491 env->host_tid = info->tid;
3492 task_settid(ts);
3493 if (info->child_tidptr)
3494 put_user_u32(info->tid, info->child_tidptr);
3495 if (info->parent_tidptr)
3496 put_user_u32(info->tid, info->parent_tidptr);
3497 /* Enable signals. */
3498 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3499 /* Signal to the parent that we're ready. */
3500 pthread_mutex_lock(&info->mutex);
3501 pthread_cond_broadcast(&info->cond);
3502 pthread_mutex_unlock(&info->mutex);
3503 /* Wait until the parent has finshed initializing the tls state. */
3504 pthread_mutex_lock(&clone_lock);
3505 pthread_mutex_unlock(&clone_lock);
3506 cpu_loop(env);
3507 /* never exits */
3508 return NULL;
3510 #else
3511 /* this stack is the equivalent of the kernel stack associated with a
3512 thread/process */
3513 #define NEW_STACK_SIZE 8192
3515 static int clone_func(void *arg)
3517 CPUState *env = arg;
3518 cpu_loop(env);
3519 /* never exits */
3520 return 0;
3522 #endif
3524 /* do_fork() Must return host values and target errnos (unlike most
3525 do_*() functions). */
3526 static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3527 abi_ulong parent_tidptr, target_ulong newtls,
3528 abi_ulong child_tidptr)
3530 int ret;
3531 TaskState *ts;
3532 uint8_t *new_stack;
3533 CPUState *new_env;
3534 #if defined(CONFIG_USE_NPTL)
3535 unsigned int nptl_flags;
3536 sigset_t sigmask;
3537 #endif
3539 /* Emulate vfork() with fork() */
3540 if (flags & CLONE_VFORK)
3541 flags &= ~(CLONE_VFORK | CLONE_VM);
3543 if (flags & CLONE_VM) {
3544 TaskState *parent_ts = (TaskState *)env->opaque;
3545 #if defined(CONFIG_USE_NPTL)
3546 new_thread_info info;
3547 pthread_attr_t attr;
3548 #endif
3549 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
3550 init_task_state(ts);
3551 new_stack = ts->stack;
3552 /* we create a new CPU instance. */
3553 new_env = cpu_copy(env);
3554 /* Init regs that differ from the parent. */
3555 cpu_clone_regs(new_env, newsp);
3556 new_env->opaque = ts;
3557 ts->bprm = parent_ts->bprm;
3558 ts->info = parent_ts->info;
3559 #if defined(CONFIG_USE_NPTL)
3560 nptl_flags = flags;
3561 flags &= ~CLONE_NPTL_FLAGS2;
3563 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3564 ts->child_tidptr = child_tidptr;
3567 if (nptl_flags & CLONE_SETTLS)
3568 cpu_set_tls (new_env, newtls);
3570 /* Grab a mutex so that thread setup appears atomic. */
3571 pthread_mutex_lock(&clone_lock);
3573 memset(&info, 0, sizeof(info));
3574 pthread_mutex_init(&info.mutex, NULL);
3575 pthread_mutex_lock(&info.mutex);
3576 pthread_cond_init(&info.cond, NULL);
3577 info.env = new_env;
3578 if (nptl_flags & CLONE_CHILD_SETTID)
3579 info.child_tidptr = child_tidptr;
3580 if (nptl_flags & CLONE_PARENT_SETTID)
3581 info.parent_tidptr = parent_tidptr;
3583 ret = pthread_attr_init(&attr);
3584 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
3585 /* It is not safe to deliver signals until the child has finished
3586 initializing, so temporarily block all signals. */
3587 sigfillset(&sigmask);
3588 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3590 ret = pthread_create(&info.thread, &attr, clone_func, &info);
3591 /* TODO: Free new CPU state if thread creation failed. */
3593 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3594 pthread_attr_destroy(&attr);
3595 if (ret == 0) {
3596 /* Wait for the child to initialize. */
3597 pthread_cond_wait(&info.cond, &info.mutex);
3598 ret = info.tid;
3599 if (flags & CLONE_PARENT_SETTID)
3600 put_user_u32(ret, parent_tidptr);
3601 } else {
3602 ret = -1;
3604 pthread_mutex_unlock(&info.mutex);
3605 pthread_cond_destroy(&info.cond);
3606 pthread_mutex_destroy(&info.mutex);
3607 pthread_mutex_unlock(&clone_lock);
3608 #else
3609 if (flags & CLONE_NPTL_FLAGS2)
3610 return -EINVAL;
3611 /* This is probably going to die very quickly, but do it anyway. */
3612 #ifdef __ia64__
3613 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3614 #else
3615 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3616 #endif
3617 #endif
3618 } else {
3619 /* if no CLONE_VM, we consider it is a fork */
3620 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
3621 return -EINVAL;
3622 fork_start();
3623 ret = fork();
3624 if (ret == 0) {
3625 /* Child Process. */
3626 cpu_clone_regs(env, newsp);
3627 fork_end(1);
3628 #if defined(CONFIG_USE_NPTL)
3629 /* There is a race condition here. The parent process could
3630 theoretically read the TID in the child process before the child
3631 tid is set. This would require using either ptrace
3632 (not implemented) or having *_tidptr to point at a shared memory
3633 mapping. We can't repeat the spinlock hack used above because
3634 the child process gets its own copy of the lock. */
3635 if (flags & CLONE_CHILD_SETTID)
3636 put_user_u32(gettid(), child_tidptr);
3637 if (flags & CLONE_PARENT_SETTID)
3638 put_user_u32(gettid(), parent_tidptr);
3639 ts = (TaskState *)env->opaque;
3640 if (flags & CLONE_SETTLS)
3641 cpu_set_tls (env, newtls);
3642 if (flags & CLONE_CHILD_CLEARTID)
3643 ts->child_tidptr = child_tidptr;
3644 #endif
3645 } else {
3646 fork_end(0);
3649 return ret;
3652 /* warning : doesn't handle linux specific flags... */
3653 static int target_to_host_fcntl_cmd(int cmd)
3655 switch(cmd) {
3656 case TARGET_F_DUPFD:
3657 case TARGET_F_GETFD:
3658 case TARGET_F_SETFD:
3659 case TARGET_F_GETFL:
3660 case TARGET_F_SETFL:
3661 return cmd;
3662 case TARGET_F_GETLK:
3663 return F_GETLK;
3664 case TARGET_F_SETLK:
3665 return F_SETLK;
3666 case TARGET_F_SETLKW:
3667 return F_SETLKW;
3668 case TARGET_F_GETOWN:
3669 return F_GETOWN;
3670 case TARGET_F_SETOWN:
3671 return F_SETOWN;
3672 case TARGET_F_GETSIG:
3673 return F_GETSIG;
3674 case TARGET_F_SETSIG:
3675 return F_SETSIG;
3676 #if TARGET_ABI_BITS == 32
3677 case TARGET_F_GETLK64:
3678 return F_GETLK64;
3679 case TARGET_F_SETLK64:
3680 return F_SETLK64;
3681 case TARGET_F_SETLKW64:
3682 return F_SETLKW64;
3683 #endif
3684 case TARGET_F_SETLEASE:
3685 return F_SETLEASE;
3686 case TARGET_F_GETLEASE:
3687 return F_GETLEASE;
3688 case TARGET_F_DUPFD_CLOEXEC:
3689 return F_DUPFD_CLOEXEC;
3690 case TARGET_F_NOTIFY:
3691 return F_NOTIFY;
3692 default:
3693 return -TARGET_EINVAL;
3695 return -TARGET_EINVAL;
3698 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
3700 struct flock fl;
3701 struct target_flock *target_fl;
3702 struct flock64 fl64;
3703 struct target_flock64 *target_fl64;
3704 abi_long ret;
3705 int host_cmd = target_to_host_fcntl_cmd(cmd);
3707 if (host_cmd == -TARGET_EINVAL)
3708 return host_cmd;
3710 switch(cmd) {
3711 case TARGET_F_GETLK:
3712 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3713 return -TARGET_EFAULT;
3714 fl.l_type = tswap16(target_fl->l_type);
3715 fl.l_whence = tswap16(target_fl->l_whence);
3716 fl.l_start = tswapl(target_fl->l_start);
3717 fl.l_len = tswapl(target_fl->l_len);
3718 fl.l_pid = tswap32(target_fl->l_pid);
3719 unlock_user_struct(target_fl, arg, 0);
3720 ret = get_errno(fcntl(fd, host_cmd, &fl));
3721 if (ret == 0) {
3722 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3723 return -TARGET_EFAULT;
3724 target_fl->l_type = tswap16(fl.l_type);
3725 target_fl->l_whence = tswap16(fl.l_whence);
3726 target_fl->l_start = tswapl(fl.l_start);
3727 target_fl->l_len = tswapl(fl.l_len);
3728 target_fl->l_pid = tswap32(fl.l_pid);
3729 unlock_user_struct(target_fl, arg, 1);
3731 break;
3733 case TARGET_F_SETLK:
3734 case TARGET_F_SETLKW:
3735 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3736 return -TARGET_EFAULT;
3737 fl.l_type = tswap16(target_fl->l_type);
3738 fl.l_whence = tswap16(target_fl->l_whence);
3739 fl.l_start = tswapl(target_fl->l_start);
3740 fl.l_len = tswapl(target_fl->l_len);
3741 fl.l_pid = tswap32(target_fl->l_pid);
3742 unlock_user_struct(target_fl, arg, 0);
3743 ret = get_errno(fcntl(fd, host_cmd, &fl));
3744 break;
3746 case TARGET_F_GETLK64:
3747 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3748 return -TARGET_EFAULT;
3749 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3750 fl64.l_whence = tswap16(target_fl64->l_whence);
3751 fl64.l_start = tswapl(target_fl64->l_start);
3752 fl64.l_len = tswapl(target_fl64->l_len);
3753 fl64.l_pid = tswap32(target_fl64->l_pid);
3754 unlock_user_struct(target_fl64, arg, 0);
3755 ret = get_errno(fcntl(fd, host_cmd, &fl64));
3756 if (ret == 0) {
3757 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3758 return -TARGET_EFAULT;
3759 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3760 target_fl64->l_whence = tswap16(fl64.l_whence);
3761 target_fl64->l_start = tswapl(fl64.l_start);
3762 target_fl64->l_len = tswapl(fl64.l_len);
3763 target_fl64->l_pid = tswap32(fl64.l_pid);
3764 unlock_user_struct(target_fl64, arg, 1);
3766 break;
3767 case TARGET_F_SETLK64:
3768 case TARGET_F_SETLKW64:
3769 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3770 return -TARGET_EFAULT;
3771 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3772 fl64.l_whence = tswap16(target_fl64->l_whence);
3773 fl64.l_start = tswapl(target_fl64->l_start);
3774 fl64.l_len = tswapl(target_fl64->l_len);
3775 fl64.l_pid = tswap32(target_fl64->l_pid);
3776 unlock_user_struct(target_fl64, arg, 0);
3777 ret = get_errno(fcntl(fd, host_cmd, &fl64));
3778 break;
3780 case TARGET_F_GETFL:
3781 ret = get_errno(fcntl(fd, host_cmd, arg));
3782 if (ret >= 0) {
3783 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3785 break;
3787 case TARGET_F_SETFL:
3788 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3789 break;
3791 case TARGET_F_SETOWN:
3792 case TARGET_F_GETOWN:
3793 case TARGET_F_SETSIG:
3794 case TARGET_F_GETSIG:
3795 case TARGET_F_SETLEASE:
3796 case TARGET_F_GETLEASE:
3797 ret = get_errno(fcntl(fd, host_cmd, arg));
3798 break;
3800 default:
3801 ret = get_errno(fcntl(fd, cmd, arg));
3802 break;
3804 return ret;
3807 #ifdef USE_UID16
3809 static inline int high2lowuid(int uid)
3811 if (uid > 65535)
3812 return 65534;
3813 else
3814 return uid;
3817 static inline int high2lowgid(int gid)
3819 if (gid > 65535)
3820 return 65534;
3821 else
3822 return gid;
3825 static inline int low2highuid(int uid)
3827 if ((int16_t)uid == -1)
3828 return -1;
3829 else
3830 return uid;
3833 static inline int low2highgid(int gid)
3835 if ((int16_t)gid == -1)
3836 return -1;
3837 else
3838 return gid;
3841 #endif /* USE_UID16 */
3843 void syscall_init(void)
3845 IOCTLEntry *ie;
3846 const argtype *arg_type;
3847 int size;
3848 int i;
3850 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
3851 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
3852 #include "syscall_types.h"
3853 #undef STRUCT
3854 #undef STRUCT_SPECIAL
3856 /* we patch the ioctl size if necessary. We rely on the fact that
3857 no ioctl has all the bits at '1' in the size field */
3858 ie = ioctl_entries;
3859 while (ie->target_cmd != 0) {
3860 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3861 TARGET_IOC_SIZEMASK) {
3862 arg_type = ie->arg_type;
3863 if (arg_type[0] != TYPE_PTR) {
3864 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
3865 ie->target_cmd);
3866 exit(1);
3868 arg_type++;
3869 size = thunk_type_size(arg_type, 0);
3870 ie->target_cmd = (ie->target_cmd &
3871 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3872 (size << TARGET_IOC_SIZESHIFT);
3875 /* Build target_to_host_errno_table[] table from
3876 * host_to_target_errno_table[]. */
3877 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3878 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3880 /* automatic consistency check if same arch */
3881 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3882 (defined(__x86_64__) && defined(TARGET_X86_64))
3883 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3884 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3885 ie->name, ie->target_cmd, ie->host_cmd);
3887 #endif
3888 ie++;
3892 #if TARGET_ABI_BITS == 32
3893 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3895 #ifdef TARGET_WORDS_BIGENDIAN
3896 return ((uint64_t)word0 << 32) | word1;
3897 #else
3898 return ((uint64_t)word1 << 32) | word0;
3899 #endif
3901 #else /* TARGET_ABI_BITS == 32 */
3902 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3904 return word0;
3906 #endif /* TARGET_ABI_BITS != 32 */
3908 #ifdef TARGET_NR_truncate64
3909 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3910 abi_long arg2,
3911 abi_long arg3,
3912 abi_long arg4)
3914 #ifdef TARGET_ARM
3915 if (((CPUARMState *)cpu_env)->eabi)
3917 arg2 = arg3;
3918 arg3 = arg4;
3920 #endif
3921 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3923 #endif
3925 #ifdef TARGET_NR_ftruncate64
3926 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3927 abi_long arg2,
3928 abi_long arg3,
3929 abi_long arg4)
3931 #ifdef TARGET_ARM
3932 if (((CPUARMState *)cpu_env)->eabi)
3934 arg2 = arg3;
3935 arg3 = arg4;
3937 #endif
3938 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3940 #endif
3942 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3943 abi_ulong target_addr)
3945 struct target_timespec *target_ts;
3947 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3948 return -TARGET_EFAULT;
3949 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3950 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3951 unlock_user_struct(target_ts, target_addr, 0);
3952 return 0;
3955 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3956 struct timespec *host_ts)
3958 struct target_timespec *target_ts;
3960 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3961 return -TARGET_EFAULT;
3962 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3963 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3964 unlock_user_struct(target_ts, target_addr, 1);
3965 return 0;
3968 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
3969 static inline abi_long host_to_target_stat64(void *cpu_env,
3970 abi_ulong target_addr,
3971 struct stat *host_st)
3973 #ifdef TARGET_ARM
3974 if (((CPUARMState *)cpu_env)->eabi) {
3975 struct target_eabi_stat64 *target_st;
3977 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3978 return -TARGET_EFAULT;
3979 memset(target_st, 0, sizeof(struct target_eabi_stat64));
3980 __put_user(host_st->st_dev, &target_st->st_dev);
3981 __put_user(host_st->st_ino, &target_st->st_ino);
3982 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3983 __put_user(host_st->st_ino, &target_st->__st_ino);
3984 #endif
3985 __put_user(host_st->st_mode, &target_st->st_mode);
3986 __put_user(host_st->st_nlink, &target_st->st_nlink);
3987 __put_user(host_st->st_uid, &target_st->st_uid);
3988 __put_user(host_st->st_gid, &target_st->st_gid);
3989 __put_user(host_st->st_rdev, &target_st->st_rdev);
3990 __put_user(host_st->st_size, &target_st->st_size);
3991 __put_user(host_st->st_blksize, &target_st->st_blksize);
3992 __put_user(host_st->st_blocks, &target_st->st_blocks);
3993 __put_user(host_st->st_atime, &target_st->target_st_atime);
3994 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3995 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3996 unlock_user_struct(target_st, target_addr, 1);
3997 } else
3998 #endif
4000 #if TARGET_LONG_BITS == 64
4001 struct target_stat *target_st;
4002 #else
4003 struct target_stat64 *target_st;
4004 #endif
4006 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4007 return -TARGET_EFAULT;
4008 memset(target_st, 0, sizeof(*target_st));
4009 __put_user(host_st->st_dev, &target_st->st_dev);
4010 __put_user(host_st->st_ino, &target_st->st_ino);
4011 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4012 __put_user(host_st->st_ino, &target_st->__st_ino);
4013 #endif
4014 __put_user(host_st->st_mode, &target_st->st_mode);
4015 __put_user(host_st->st_nlink, &target_st->st_nlink);
4016 __put_user(host_st->st_uid, &target_st->st_uid);
4017 __put_user(host_st->st_gid, &target_st->st_gid);
4018 __put_user(host_st->st_rdev, &target_st->st_rdev);
4019 /* XXX: better use of kernel struct */
4020 __put_user(host_st->st_size, &target_st->st_size);
4021 __put_user(host_st->st_blksize, &target_st->st_blksize);
4022 __put_user(host_st->st_blocks, &target_st->st_blocks);
4023 __put_user(host_st->st_atime, &target_st->target_st_atime);
4024 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4025 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4026 unlock_user_struct(target_st, target_addr, 1);
4029 return 0;
4031 #endif
4033 #if defined(CONFIG_USE_NPTL)
4034 /* ??? Using host futex calls even when target atomic operations
4035 are not really atomic probably breaks things. However implementing
4036 futexes locally would make futexes shared between multiple processes
4037 tricky. However they're probably useless because guest atomic
4038 operations won't work either. */
4039 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4040 target_ulong uaddr2, int val3)
4042 struct timespec ts, *pts;
4043 int base_op;
4045 /* ??? We assume FUTEX_* constants are the same on both host
4046 and target. */
4047 #ifdef FUTEX_CMD_MASK
4048 base_op = op & FUTEX_CMD_MASK;
4049 #else
4050 base_op = op;
4051 #endif
4052 switch (base_op) {
4053 case FUTEX_WAIT:
4054 if (timeout) {
4055 pts = &ts;
4056 target_to_host_timespec(pts, timeout);
4057 } else {
4058 pts = NULL;
4060 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
4061 pts, NULL, 0));
4062 case FUTEX_WAKE:
4063 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4064 case FUTEX_FD:
4065 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4066 case FUTEX_REQUEUE:
4067 case FUTEX_CMP_REQUEUE:
4068 case FUTEX_WAKE_OP:
4069 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4070 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4071 But the prototype takes a `struct timespec *'; insert casts
4072 to satisfy the compiler. We do not need to tswap TIMEOUT
4073 since it's not compared to guest memory. */
4074 pts = (struct timespec *)(uintptr_t) timeout;
4075 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4076 g2h(uaddr2),
4077 (base_op == FUTEX_CMP_REQUEUE
4078 ? tswap32(val3)
4079 : val3)));
4080 default:
4081 return -TARGET_ENOSYS;
4084 #endif
4086 /* Map host to target signal numbers for the wait family of syscalls.
4087 Assume all other status bits are the same. */
4088 static int host_to_target_waitstatus(int status)
4090 if (WIFSIGNALED(status)) {
4091 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4093 if (WIFSTOPPED(status)) {
4094 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4095 | (status & 0xff);
4097 return status;
4100 int get_osversion(void)
4102 static int osversion;
4103 struct new_utsname buf;
4104 const char *s;
4105 int i, n, tmp;
4106 if (osversion)
4107 return osversion;
4108 if (qemu_uname_release && *qemu_uname_release) {
4109 s = qemu_uname_release;
4110 } else {
4111 if (sys_uname(&buf))
4112 return 0;
4113 s = buf.release;
4115 tmp = 0;
4116 for (i = 0; i < 3; i++) {
4117 n = 0;
4118 while (*s >= '0' && *s <= '9') {
4119 n *= 10;
4120 n += *s - '0';
4121 s++;
4123 tmp = (tmp << 8) + n;
4124 if (*s == '.')
4125 s++;
4127 osversion = tmp;
4128 return osversion;
4131 /* do_syscall() should always have a single exit point at the end so
4132 that actions, such as logging of syscall results, can be performed.
4133 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
4134 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4135 abi_long arg2, abi_long arg3, abi_long arg4,
4136 abi_long arg5, abi_long arg6)
4138 abi_long ret;
4139 struct stat st;
4140 struct statfs stfs;
4141 void *p;
4143 #ifdef DEBUG
4144 gemu_log("syscall %d", num);
4145 #endif
4146 if(do_strace)
4147 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4149 switch(num) {
4150 case TARGET_NR_exit:
4151 #ifdef CONFIG_USE_NPTL
4152 /* In old applications this may be used to implement _exit(2).
4153 However in threaded applictions it is used for thread termination,
4154 and _exit_group is used for application termination.
4155 Do thread termination if we have more then one thread. */
4156 /* FIXME: This probably breaks if a signal arrives. We should probably
4157 be disabling signals. */
4158 if (first_cpu->next_cpu) {
4159 TaskState *ts;
4160 CPUState **lastp;
4161 CPUState *p;
4163 cpu_list_lock();
4164 lastp = &first_cpu;
4165 p = first_cpu;
4166 while (p && p != (CPUState *)cpu_env) {
4167 lastp = &p->next_cpu;
4168 p = p->next_cpu;
4170 /* If we didn't find the CPU for this thread then something is
4171 horribly wrong. */
4172 if (!p)
4173 abort();
4174 /* Remove the CPU from the list. */
4175 *lastp = p->next_cpu;
4176 cpu_list_unlock();
4177 ts = ((CPUState *)cpu_env)->opaque;
4178 if (ts->child_tidptr) {
4179 put_user_u32(0, ts->child_tidptr);
4180 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4181 NULL, NULL, 0);
4183 /* TODO: Free CPU state. */
4184 pthread_exit(NULL);
4186 #endif
4187 #ifdef TARGET_GPROF
4188 _mcleanup();
4189 #endif
4190 gdb_exit(cpu_env, arg1);
4191 _exit(arg1);
4192 ret = 0; /* avoid warning */
4193 break;
4194 case TARGET_NR_read:
4195 if (arg3 == 0)
4196 ret = 0;
4197 else {
4198 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4199 goto efault;
4200 ret = get_errno(read(arg1, p, arg3));
4201 unlock_user(p, arg2, ret);
4203 break;
4204 case TARGET_NR_write:
4205 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4206 goto efault;
4207 ret = get_errno(write(arg1, p, arg3));
4208 unlock_user(p, arg2, 0);
4209 break;
4210 case TARGET_NR_open:
4211 if (!(p = lock_user_string(arg1)))
4212 goto efault;
4213 ret = get_errno(open(path(p),
4214 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4215 arg3));
4216 unlock_user(p, arg1, 0);
4217 break;
4218 #if defined(TARGET_NR_openat) && defined(__NR_openat)
4219 case TARGET_NR_openat:
4220 if (!(p = lock_user_string(arg2)))
4221 goto efault;
4222 ret = get_errno(sys_openat(arg1,
4223 path(p),
4224 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4225 arg4));
4226 unlock_user(p, arg2, 0);
4227 break;
4228 #endif
4229 case TARGET_NR_close:
4230 ret = get_errno(close(arg1));
4231 break;
4232 case TARGET_NR_brk:
4233 ret = do_brk(arg1);
4234 break;
4235 case TARGET_NR_fork:
4236 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
4237 break;
4238 #ifdef TARGET_NR_waitpid
4239 case TARGET_NR_waitpid:
4241 int status;
4242 ret = get_errno(waitpid(arg1, &status, arg3));
4243 if (!is_error(ret) && arg2
4244 && put_user_s32(host_to_target_waitstatus(status), arg2))
4245 goto efault;
4247 break;
4248 #endif
4249 #ifdef TARGET_NR_waitid
4250 case TARGET_NR_waitid:
4252 siginfo_t info;
4253 info.si_pid = 0;
4254 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4255 if (!is_error(ret) && arg3 && info.si_pid != 0) {
4256 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
4257 goto efault;
4258 host_to_target_siginfo(p, &info);
4259 unlock_user(p, arg3, sizeof(target_siginfo_t));
4262 break;
4263 #endif
4264 #ifdef TARGET_NR_creat /* not on alpha */
4265 case TARGET_NR_creat:
4266 if (!(p = lock_user_string(arg1)))
4267 goto efault;
4268 ret = get_errno(creat(p, arg2));
4269 unlock_user(p, arg1, 0);
4270 break;
4271 #endif
4272 case TARGET_NR_link:
4274 void * p2;
4275 p = lock_user_string(arg1);
4276 p2 = lock_user_string(arg2);
4277 if (!p || !p2)
4278 ret = -TARGET_EFAULT;
4279 else
4280 ret = get_errno(link(p, p2));
4281 unlock_user(p2, arg2, 0);
4282 unlock_user(p, arg1, 0);
4284 break;
4285 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4286 case TARGET_NR_linkat:
4288 void * p2 = NULL;
4289 if (!arg2 || !arg4)
4290 goto efault;
4291 p = lock_user_string(arg2);
4292 p2 = lock_user_string(arg4);
4293 if (!p || !p2)
4294 ret = -TARGET_EFAULT;
4295 else
4296 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
4297 unlock_user(p, arg2, 0);
4298 unlock_user(p2, arg4, 0);
4300 break;
4301 #endif
4302 case TARGET_NR_unlink:
4303 if (!(p = lock_user_string(arg1)))
4304 goto efault;
4305 ret = get_errno(unlink(p));
4306 unlock_user(p, arg1, 0);
4307 break;
4308 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4309 case TARGET_NR_unlinkat:
4310 if (!(p = lock_user_string(arg2)))
4311 goto efault;
4312 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4313 unlock_user(p, arg2, 0);
4314 break;
4315 #endif
4316 case TARGET_NR_execve:
4318 char **argp, **envp;
4319 int argc, envc;
4320 abi_ulong gp;
4321 abi_ulong guest_argp;
4322 abi_ulong guest_envp;
4323 abi_ulong addr;
4324 char **q;
4326 argc = 0;
4327 guest_argp = arg2;
4328 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
4329 if (get_user_ual(addr, gp))
4330 goto efault;
4331 if (!addr)
4332 break;
4333 argc++;
4335 envc = 0;
4336 guest_envp = arg3;
4337 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
4338 if (get_user_ual(addr, gp))
4339 goto efault;
4340 if (!addr)
4341 break;
4342 envc++;
4345 argp = alloca((argc + 1) * sizeof(void *));
4346 envp = alloca((envc + 1) * sizeof(void *));
4348 for (gp = guest_argp, q = argp; gp;
4349 gp += sizeof(abi_ulong), q++) {
4350 if (get_user_ual(addr, gp))
4351 goto execve_efault;
4352 if (!addr)
4353 break;
4354 if (!(*q = lock_user_string(addr)))
4355 goto execve_efault;
4357 *q = NULL;
4359 for (gp = guest_envp, q = envp; gp;
4360 gp += sizeof(abi_ulong), q++) {
4361 if (get_user_ual(addr, gp))
4362 goto execve_efault;
4363 if (!addr)
4364 break;
4365 if (!(*q = lock_user_string(addr)))
4366 goto execve_efault;
4368 *q = NULL;
4370 if (!(p = lock_user_string(arg1)))
4371 goto execve_efault;
4372 ret = get_errno(execve(p, argp, envp));
4373 unlock_user(p, arg1, 0);
4375 goto execve_end;
4377 execve_efault:
4378 ret = -TARGET_EFAULT;
4380 execve_end:
4381 for (gp = guest_argp, q = argp; *q;
4382 gp += sizeof(abi_ulong), q++) {
4383 if (get_user_ual(addr, gp)
4384 || !addr)
4385 break;
4386 unlock_user(*q, addr, 0);
4388 for (gp = guest_envp, q = envp; *q;
4389 gp += sizeof(abi_ulong), q++) {
4390 if (get_user_ual(addr, gp)
4391 || !addr)
4392 break;
4393 unlock_user(*q, addr, 0);
4396 break;
4397 case TARGET_NR_chdir:
4398 if (!(p = lock_user_string(arg1)))
4399 goto efault;
4400 ret = get_errno(chdir(p));
4401 unlock_user(p, arg1, 0);
4402 break;
4403 #ifdef TARGET_NR_time
4404 case TARGET_NR_time:
4406 time_t host_time;
4407 ret = get_errno(time(&host_time));
4408 if (!is_error(ret)
4409 && arg1
4410 && put_user_sal(host_time, arg1))
4411 goto efault;
4413 break;
4414 #endif
4415 case TARGET_NR_mknod:
4416 if (!(p = lock_user_string(arg1)))
4417 goto efault;
4418 ret = get_errno(mknod(p, arg2, arg3));
4419 unlock_user(p, arg1, 0);
4420 break;
4421 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4422 case TARGET_NR_mknodat:
4423 if (!(p = lock_user_string(arg2)))
4424 goto efault;
4425 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4426 unlock_user(p, arg2, 0);
4427 break;
4428 #endif
4429 case TARGET_NR_chmod:
4430 if (!(p = lock_user_string(arg1)))
4431 goto efault;
4432 ret = get_errno(chmod(p, arg2));
4433 unlock_user(p, arg1, 0);
4434 break;
4435 #ifdef TARGET_NR_break
4436 case TARGET_NR_break:
4437 goto unimplemented;
4438 #endif
4439 #ifdef TARGET_NR_oldstat
4440 case TARGET_NR_oldstat:
4441 goto unimplemented;
4442 #endif
4443 case TARGET_NR_lseek:
4444 ret = get_errno(lseek(arg1, arg2, arg3));
4445 break;
4446 #ifdef TARGET_NR_getxpid
4447 case TARGET_NR_getxpid:
4448 #else
4449 case TARGET_NR_getpid:
4450 #endif
4451 ret = get_errno(getpid());
4452 break;
4453 case TARGET_NR_mount:
4455 /* need to look at the data field */
4456 void *p2, *p3;
4457 p = lock_user_string(arg1);
4458 p2 = lock_user_string(arg2);
4459 p3 = lock_user_string(arg3);
4460 if (!p || !p2 || !p3)
4461 ret = -TARGET_EFAULT;
4462 else
4463 /* FIXME - arg5 should be locked, but it isn't clear how to
4464 * do that since it's not guaranteed to be a NULL-terminated
4465 * string.
4467 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4468 unlock_user(p, arg1, 0);
4469 unlock_user(p2, arg2, 0);
4470 unlock_user(p3, arg3, 0);
4471 break;
4473 #ifdef TARGET_NR_umount
4474 case TARGET_NR_umount:
4475 if (!(p = lock_user_string(arg1)))
4476 goto efault;
4477 ret = get_errno(umount(p));
4478 unlock_user(p, arg1, 0);
4479 break;
4480 #endif
4481 #ifdef TARGET_NR_stime /* not on alpha */
4482 case TARGET_NR_stime:
4484 time_t host_time;
4485 if (get_user_sal(host_time, arg1))
4486 goto efault;
4487 ret = get_errno(stime(&host_time));
4489 break;
4490 #endif
4491 case TARGET_NR_ptrace:
4492 goto unimplemented;
4493 #ifdef TARGET_NR_alarm /* not on alpha */
4494 case TARGET_NR_alarm:
4495 ret = alarm(arg1);
4496 break;
4497 #endif
4498 #ifdef TARGET_NR_oldfstat
4499 case TARGET_NR_oldfstat:
4500 goto unimplemented;
4501 #endif
4502 #ifdef TARGET_NR_pause /* not on alpha */
4503 case TARGET_NR_pause:
4504 ret = get_errno(pause());
4505 break;
4506 #endif
4507 #ifdef TARGET_NR_utime
4508 case TARGET_NR_utime:
4510 struct utimbuf tbuf, *host_tbuf;
4511 struct target_utimbuf *target_tbuf;
4512 if (arg2) {
4513 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4514 goto efault;
4515 tbuf.actime = tswapl(target_tbuf->actime);
4516 tbuf.modtime = tswapl(target_tbuf->modtime);
4517 unlock_user_struct(target_tbuf, arg2, 0);
4518 host_tbuf = &tbuf;
4519 } else {
4520 host_tbuf = NULL;
4522 if (!(p = lock_user_string(arg1)))
4523 goto efault;
4524 ret = get_errno(utime(p, host_tbuf));
4525 unlock_user(p, arg1, 0);
4527 break;
4528 #endif
4529 case TARGET_NR_utimes:
4531 struct timeval *tvp, tv[2];
4532 if (arg2) {
4533 if (copy_from_user_timeval(&tv[0], arg2)
4534 || copy_from_user_timeval(&tv[1],
4535 arg2 + sizeof(struct target_timeval)))
4536 goto efault;
4537 tvp = tv;
4538 } else {
4539 tvp = NULL;
4541 if (!(p = lock_user_string(arg1)))
4542 goto efault;
4543 ret = get_errno(utimes(p, tvp));
4544 unlock_user(p, arg1, 0);
4546 break;
4547 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4548 case TARGET_NR_futimesat:
4550 struct timeval *tvp, tv[2];
4551 if (arg3) {
4552 if (copy_from_user_timeval(&tv[0], arg3)
4553 || copy_from_user_timeval(&tv[1],
4554 arg3 + sizeof(struct target_timeval)))
4555 goto efault;
4556 tvp = tv;
4557 } else {
4558 tvp = NULL;
4560 if (!(p = lock_user_string(arg2)))
4561 goto efault;
4562 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4563 unlock_user(p, arg2, 0);
4565 break;
4566 #endif
4567 #ifdef TARGET_NR_stty
4568 case TARGET_NR_stty:
4569 goto unimplemented;
4570 #endif
4571 #ifdef TARGET_NR_gtty
4572 case TARGET_NR_gtty:
4573 goto unimplemented;
4574 #endif
4575 case TARGET_NR_access:
4576 if (!(p = lock_user_string(arg1)))
4577 goto efault;
4578 ret = get_errno(access(path(p), arg2));
4579 unlock_user(p, arg1, 0);
4580 break;
4581 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4582 case TARGET_NR_faccessat:
4583 if (!(p = lock_user_string(arg2)))
4584 goto efault;
4585 ret = get_errno(sys_faccessat(arg1, p, arg3));
4586 unlock_user(p, arg2, 0);
4587 break;
4588 #endif
4589 #ifdef TARGET_NR_nice /* not on alpha */
4590 case TARGET_NR_nice:
4591 ret = get_errno(nice(arg1));
4592 break;
4593 #endif
4594 #ifdef TARGET_NR_ftime
4595 case TARGET_NR_ftime:
4596 goto unimplemented;
4597 #endif
4598 case TARGET_NR_sync:
4599 sync();
4600 ret = 0;
4601 break;
4602 case TARGET_NR_kill:
4603 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
4604 break;
4605 case TARGET_NR_rename:
4607 void *p2;
4608 p = lock_user_string(arg1);
4609 p2 = lock_user_string(arg2);
4610 if (!p || !p2)
4611 ret = -TARGET_EFAULT;
4612 else
4613 ret = get_errno(rename(p, p2));
4614 unlock_user(p2, arg2, 0);
4615 unlock_user(p, arg1, 0);
4617 break;
4618 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4619 case TARGET_NR_renameat:
4621 void *p2;
4622 p = lock_user_string(arg2);
4623 p2 = lock_user_string(arg4);
4624 if (!p || !p2)
4625 ret = -TARGET_EFAULT;
4626 else
4627 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
4628 unlock_user(p2, arg4, 0);
4629 unlock_user(p, arg2, 0);
4631 break;
4632 #endif
4633 case TARGET_NR_mkdir:
4634 if (!(p = lock_user_string(arg1)))
4635 goto efault;
4636 ret = get_errno(mkdir(p, arg2));
4637 unlock_user(p, arg1, 0);
4638 break;
4639 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4640 case TARGET_NR_mkdirat:
4641 if (!(p = lock_user_string(arg2)))
4642 goto efault;
4643 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4644 unlock_user(p, arg2, 0);
4645 break;
4646 #endif
4647 case TARGET_NR_rmdir:
4648 if (!(p = lock_user_string(arg1)))
4649 goto efault;
4650 ret = get_errno(rmdir(p));
4651 unlock_user(p, arg1, 0);
4652 break;
4653 case TARGET_NR_dup:
4654 ret = get_errno(dup(arg1));
4655 break;
4656 case TARGET_NR_pipe:
4657 ret = do_pipe(cpu_env, arg1, 0);
4658 break;
4659 #ifdef TARGET_NR_pipe2
4660 case TARGET_NR_pipe2:
4661 ret = do_pipe(cpu_env, arg1, arg2);
4662 break;
4663 #endif
4664 case TARGET_NR_times:
4666 struct target_tms *tmsp;
4667 struct tms tms;
4668 ret = get_errno(times(&tms));
4669 if (arg1) {
4670 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4671 if (!tmsp)
4672 goto efault;
4673 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4674 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4675 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4676 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
4678 if (!is_error(ret))
4679 ret = host_to_target_clock_t(ret);
4681 break;
4682 #ifdef TARGET_NR_prof
4683 case TARGET_NR_prof:
4684 goto unimplemented;
4685 #endif
4686 #ifdef TARGET_NR_signal
4687 case TARGET_NR_signal:
4688 goto unimplemented;
4689 #endif
4690 case TARGET_NR_acct:
4691 if (arg1 == 0) {
4692 ret = get_errno(acct(NULL));
4693 } else {
4694 if (!(p = lock_user_string(arg1)))
4695 goto efault;
4696 ret = get_errno(acct(path(p)));
4697 unlock_user(p, arg1, 0);
4699 break;
4700 #ifdef TARGET_NR_umount2 /* not on alpha */
4701 case TARGET_NR_umount2:
4702 if (!(p = lock_user_string(arg1)))
4703 goto efault;
4704 ret = get_errno(umount2(p, arg2));
4705 unlock_user(p, arg1, 0);
4706 break;
4707 #endif
4708 #ifdef TARGET_NR_lock
4709 case TARGET_NR_lock:
4710 goto unimplemented;
4711 #endif
4712 case TARGET_NR_ioctl:
4713 ret = do_ioctl(arg1, arg2, arg3);
4714 break;
4715 case TARGET_NR_fcntl:
4716 ret = do_fcntl(arg1, arg2, arg3);
4717 break;
4718 #ifdef TARGET_NR_mpx
4719 case TARGET_NR_mpx:
4720 goto unimplemented;
4721 #endif
4722 case TARGET_NR_setpgid:
4723 ret = get_errno(setpgid(arg1, arg2));
4724 break;
4725 #ifdef TARGET_NR_ulimit
4726 case TARGET_NR_ulimit:
4727 goto unimplemented;
4728 #endif
4729 #ifdef TARGET_NR_oldolduname
4730 case TARGET_NR_oldolduname:
4731 goto unimplemented;
4732 #endif
4733 case TARGET_NR_umask:
4734 ret = get_errno(umask(arg1));
4735 break;
4736 case TARGET_NR_chroot:
4737 if (!(p = lock_user_string(arg1)))
4738 goto efault;
4739 ret = get_errno(chroot(p));
4740 unlock_user(p, arg1, 0);
4741 break;
4742 case TARGET_NR_ustat:
4743 goto unimplemented;
4744 case TARGET_NR_dup2:
4745 ret = get_errno(dup2(arg1, arg2));
4746 break;
4747 #ifdef TARGET_NR_getppid /* not on alpha */
4748 case TARGET_NR_getppid:
4749 ret = get_errno(getppid());
4750 break;
4751 #endif
4752 case TARGET_NR_getpgrp:
4753 ret = get_errno(getpgrp());
4754 break;
4755 case TARGET_NR_setsid:
4756 ret = get_errno(setsid());
4757 break;
4758 #ifdef TARGET_NR_sigaction
4759 case TARGET_NR_sigaction:
4761 #if !defined(TARGET_MIPS)
4762 struct target_old_sigaction *old_act;
4763 struct target_sigaction act, oact, *pact;
4764 if (arg2) {
4765 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4766 goto efault;
4767 act._sa_handler = old_act->_sa_handler;
4768 target_siginitset(&act.sa_mask, old_act->sa_mask);
4769 act.sa_flags = old_act->sa_flags;
4770 act.sa_restorer = old_act->sa_restorer;
4771 unlock_user_struct(old_act, arg2, 0);
4772 pact = &act;
4773 } else {
4774 pact = NULL;
4776 ret = get_errno(do_sigaction(arg1, pact, &oact));
4777 if (!is_error(ret) && arg3) {
4778 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4779 goto efault;
4780 old_act->_sa_handler = oact._sa_handler;
4781 old_act->sa_mask = oact.sa_mask.sig[0];
4782 old_act->sa_flags = oact.sa_flags;
4783 old_act->sa_restorer = oact.sa_restorer;
4784 unlock_user_struct(old_act, arg3, 1);
4786 #else
4787 struct target_sigaction act, oact, *pact, *old_act;
4789 if (arg2) {
4790 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4791 goto efault;
4792 act._sa_handler = old_act->_sa_handler;
4793 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4794 act.sa_flags = old_act->sa_flags;
4795 unlock_user_struct(old_act, arg2, 0);
4796 pact = &act;
4797 } else {
4798 pact = NULL;
4801 ret = get_errno(do_sigaction(arg1, pact, &oact));
4803 if (!is_error(ret) && arg3) {
4804 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4805 goto efault;
4806 old_act->_sa_handler = oact._sa_handler;
4807 old_act->sa_flags = oact.sa_flags;
4808 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4809 old_act->sa_mask.sig[1] = 0;
4810 old_act->sa_mask.sig[2] = 0;
4811 old_act->sa_mask.sig[3] = 0;
4812 unlock_user_struct(old_act, arg3, 1);
4814 #endif
4816 break;
4817 #endif
4818 case TARGET_NR_rt_sigaction:
4820 struct target_sigaction *act;
4821 struct target_sigaction *oact;
4823 if (arg2) {
4824 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4825 goto efault;
4826 } else
4827 act = NULL;
4828 if (arg3) {
4829 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4830 ret = -TARGET_EFAULT;
4831 goto rt_sigaction_fail;
4833 } else
4834 oact = NULL;
4835 ret = get_errno(do_sigaction(arg1, act, oact));
4836 rt_sigaction_fail:
4837 if (act)
4838 unlock_user_struct(act, arg2, 0);
4839 if (oact)
4840 unlock_user_struct(oact, arg3, 1);
4842 break;
4843 #ifdef TARGET_NR_sgetmask /* not on alpha */
4844 case TARGET_NR_sgetmask:
4846 sigset_t cur_set;
4847 abi_ulong target_set;
4848 sigprocmask(0, NULL, &cur_set);
4849 host_to_target_old_sigset(&target_set, &cur_set);
4850 ret = target_set;
4852 break;
4853 #endif
4854 #ifdef TARGET_NR_ssetmask /* not on alpha */
4855 case TARGET_NR_ssetmask:
4857 sigset_t set, oset, cur_set;
4858 abi_ulong target_set = arg1;
4859 sigprocmask(0, NULL, &cur_set);
4860 target_to_host_old_sigset(&set, &target_set);
4861 sigorset(&set, &set, &cur_set);
4862 sigprocmask(SIG_SETMASK, &set, &oset);
4863 host_to_target_old_sigset(&target_set, &oset);
4864 ret = target_set;
4866 break;
4867 #endif
4868 #ifdef TARGET_NR_sigprocmask
4869 case TARGET_NR_sigprocmask:
4871 int how = arg1;
4872 sigset_t set, oldset, *set_ptr;
4874 if (arg2) {
4875 switch(how) {
4876 case TARGET_SIG_BLOCK:
4877 how = SIG_BLOCK;
4878 break;
4879 case TARGET_SIG_UNBLOCK:
4880 how = SIG_UNBLOCK;
4881 break;
4882 case TARGET_SIG_SETMASK:
4883 how = SIG_SETMASK;
4884 break;
4885 default:
4886 ret = -TARGET_EINVAL;
4887 goto fail;
4889 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4890 goto efault;
4891 target_to_host_old_sigset(&set, p);
4892 unlock_user(p, arg2, 0);
4893 set_ptr = &set;
4894 } else {
4895 how = 0;
4896 set_ptr = NULL;
4898 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
4899 if (!is_error(ret) && arg3) {
4900 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4901 goto efault;
4902 host_to_target_old_sigset(p, &oldset);
4903 unlock_user(p, arg3, sizeof(target_sigset_t));
4906 break;
4907 #endif
4908 case TARGET_NR_rt_sigprocmask:
4910 int how = arg1;
4911 sigset_t set, oldset, *set_ptr;
4913 if (arg2) {
4914 switch(how) {
4915 case TARGET_SIG_BLOCK:
4916 how = SIG_BLOCK;
4917 break;
4918 case TARGET_SIG_UNBLOCK:
4919 how = SIG_UNBLOCK;
4920 break;
4921 case TARGET_SIG_SETMASK:
4922 how = SIG_SETMASK;
4923 break;
4924 default:
4925 ret = -TARGET_EINVAL;
4926 goto fail;
4928 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4929 goto efault;
4930 target_to_host_sigset(&set, p);
4931 unlock_user(p, arg2, 0);
4932 set_ptr = &set;
4933 } else {
4934 how = 0;
4935 set_ptr = NULL;
4937 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
4938 if (!is_error(ret) && arg3) {
4939 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4940 goto efault;
4941 host_to_target_sigset(p, &oldset);
4942 unlock_user(p, arg3, sizeof(target_sigset_t));
4945 break;
4946 #ifdef TARGET_NR_sigpending
4947 case TARGET_NR_sigpending:
4949 sigset_t set;
4950 ret = get_errno(sigpending(&set));
4951 if (!is_error(ret)) {
4952 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4953 goto efault;
4954 host_to_target_old_sigset(p, &set);
4955 unlock_user(p, arg1, sizeof(target_sigset_t));
4958 break;
4959 #endif
4960 case TARGET_NR_rt_sigpending:
4962 sigset_t set;
4963 ret = get_errno(sigpending(&set));
4964 if (!is_error(ret)) {
4965 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4966 goto efault;
4967 host_to_target_sigset(p, &set);
4968 unlock_user(p, arg1, sizeof(target_sigset_t));
4971 break;
4972 #ifdef TARGET_NR_sigsuspend
4973 case TARGET_NR_sigsuspend:
4975 sigset_t set;
4976 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4977 goto efault;
4978 target_to_host_old_sigset(&set, p);
4979 unlock_user(p, arg1, 0);
4980 ret = get_errno(sigsuspend(&set));
4982 break;
4983 #endif
4984 case TARGET_NR_rt_sigsuspend:
4986 sigset_t set;
4987 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4988 goto efault;
4989 target_to_host_sigset(&set, p);
4990 unlock_user(p, arg1, 0);
4991 ret = get_errno(sigsuspend(&set));
4993 break;
4994 case TARGET_NR_rt_sigtimedwait:
4996 sigset_t set;
4997 struct timespec uts, *puts;
4998 siginfo_t uinfo;
5000 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
5001 goto efault;
5002 target_to_host_sigset(&set, p);
5003 unlock_user(p, arg1, 0);
5004 if (arg3) {
5005 puts = &uts;
5006 target_to_host_timespec(puts, arg3);
5007 } else {
5008 puts = NULL;
5010 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
5011 if (!is_error(ret) && arg2) {
5012 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
5013 goto efault;
5014 host_to_target_siginfo(p, &uinfo);
5015 unlock_user(p, arg2, sizeof(target_siginfo_t));
5018 break;
5019 case TARGET_NR_rt_sigqueueinfo:
5021 siginfo_t uinfo;
5022 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
5023 goto efault;
5024 target_to_host_siginfo(&uinfo, p);
5025 unlock_user(p, arg1, 0);
5026 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5028 break;
5029 #ifdef TARGET_NR_sigreturn
5030 case TARGET_NR_sigreturn:
5031 /* NOTE: ret is eax, so not transcoding must be done */
5032 ret = do_sigreturn(cpu_env);
5033 break;
5034 #endif
5035 case TARGET_NR_rt_sigreturn:
5036 /* NOTE: ret is eax, so not transcoding must be done */
5037 ret = do_rt_sigreturn(cpu_env);
5038 break;
5039 case TARGET_NR_sethostname:
5040 if (!(p = lock_user_string(arg1)))
5041 goto efault;
5042 ret = get_errno(sethostname(p, arg2));
5043 unlock_user(p, arg1, 0);
5044 break;
5045 case TARGET_NR_setrlimit:
5047 /* XXX: convert resource ? */
5048 int resource = arg1;
5049 struct target_rlimit *target_rlim;
5050 struct rlimit rlim;
5051 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5052 goto efault;
5053 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
5054 rlim.rlim_max = tswapl(target_rlim->rlim_max);
5055 unlock_user_struct(target_rlim, arg2, 0);
5056 ret = get_errno(setrlimit(resource, &rlim));
5058 break;
5059 case TARGET_NR_getrlimit:
5061 /* XXX: convert resource ? */
5062 int resource = arg1;
5063 struct target_rlimit *target_rlim;
5064 struct rlimit rlim;
5066 ret = get_errno(getrlimit(resource, &rlim));
5067 if (!is_error(ret)) {
5068 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5069 goto efault;
5070 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
5071 target_rlim->rlim_max = tswapl(rlim.rlim_max);
5072 unlock_user_struct(target_rlim, arg2, 1);
5075 break;
5076 case TARGET_NR_getrusage:
5078 struct rusage rusage;
5079 ret = get_errno(getrusage(arg1, &rusage));
5080 if (!is_error(ret)) {
5081 host_to_target_rusage(arg2, &rusage);
5084 break;
5085 case TARGET_NR_gettimeofday:
5087 struct timeval tv;
5088 ret = get_errno(gettimeofday(&tv, NULL));
5089 if (!is_error(ret)) {
5090 if (copy_to_user_timeval(arg1, &tv))
5091 goto efault;
5094 break;
5095 case TARGET_NR_settimeofday:
5097 struct timeval tv;
5098 if (copy_from_user_timeval(&tv, arg1))
5099 goto efault;
5100 ret = get_errno(settimeofday(&tv, NULL));
5102 break;
5103 #ifdef TARGET_NR_select
5104 case TARGET_NR_select:
5106 struct target_sel_arg_struct *sel;
5107 abi_ulong inp, outp, exp, tvp;
5108 long nsel;
5110 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5111 goto efault;
5112 nsel = tswapl(sel->n);
5113 inp = tswapl(sel->inp);
5114 outp = tswapl(sel->outp);
5115 exp = tswapl(sel->exp);
5116 tvp = tswapl(sel->tvp);
5117 unlock_user_struct(sel, arg1, 0);
5118 ret = do_select(nsel, inp, outp, exp, tvp);
5120 break;
5121 #endif
5122 case TARGET_NR_symlink:
5124 void *p2;
5125 p = lock_user_string(arg1);
5126 p2 = lock_user_string(arg2);
5127 if (!p || !p2)
5128 ret = -TARGET_EFAULT;
5129 else
5130 ret = get_errno(symlink(p, p2));
5131 unlock_user(p2, arg2, 0);
5132 unlock_user(p, arg1, 0);
5134 break;
5135 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5136 case TARGET_NR_symlinkat:
5138 void *p2;
5139 p = lock_user_string(arg1);
5140 p2 = lock_user_string(arg3);
5141 if (!p || !p2)
5142 ret = -TARGET_EFAULT;
5143 else
5144 ret = get_errno(sys_symlinkat(p, arg2, p2));
5145 unlock_user(p2, arg3, 0);
5146 unlock_user(p, arg1, 0);
5148 break;
5149 #endif
5150 #ifdef TARGET_NR_oldlstat
5151 case TARGET_NR_oldlstat:
5152 goto unimplemented;
5153 #endif
5154 case TARGET_NR_readlink:
5156 void *p2, *temp;
5157 p = lock_user_string(arg1);
5158 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5159 if (!p || !p2)
5160 ret = -TARGET_EFAULT;
5161 else {
5162 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5163 char real[PATH_MAX];
5164 temp = realpath(exec_path,real);
5165 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5166 snprintf((char *)p2, arg3, "%s", real);
5168 else
5169 ret = get_errno(readlink(path(p), p2, arg3));
5171 unlock_user(p2, arg2, ret);
5172 unlock_user(p, arg1, 0);
5174 break;
5175 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5176 case TARGET_NR_readlinkat:
5178 void *p2;
5179 p = lock_user_string(arg2);
5180 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5181 if (!p || !p2)
5182 ret = -TARGET_EFAULT;
5183 else
5184 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
5185 unlock_user(p2, arg3, ret);
5186 unlock_user(p, arg2, 0);
5188 break;
5189 #endif
5190 #ifdef TARGET_NR_uselib
5191 case TARGET_NR_uselib:
5192 goto unimplemented;
5193 #endif
5194 #ifdef TARGET_NR_swapon
5195 case TARGET_NR_swapon:
5196 if (!(p = lock_user_string(arg1)))
5197 goto efault;
5198 ret = get_errno(swapon(p, arg2));
5199 unlock_user(p, arg1, 0);
5200 break;
5201 #endif
5202 case TARGET_NR_reboot:
5203 goto unimplemented;
5204 #ifdef TARGET_NR_readdir
5205 case TARGET_NR_readdir:
5206 goto unimplemented;
5207 #endif
5208 #ifdef TARGET_NR_mmap
5209 case TARGET_NR_mmap:
5210 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE)
5212 abi_ulong *v;
5213 abi_ulong v1, v2, v3, v4, v5, v6;
5214 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5215 goto efault;
5216 v1 = tswapl(v[0]);
5217 v2 = tswapl(v[1]);
5218 v3 = tswapl(v[2]);
5219 v4 = tswapl(v[3]);
5220 v5 = tswapl(v[4]);
5221 v6 = tswapl(v[5]);
5222 unlock_user(v, arg1, 0);
5223 ret = get_errno(target_mmap(v1, v2, v3,
5224 target_to_host_bitmask(v4, mmap_flags_tbl),
5225 v5, v6));
5227 #else
5228 ret = get_errno(target_mmap(arg1, arg2, arg3,
5229 target_to_host_bitmask(arg4, mmap_flags_tbl),
5230 arg5,
5231 arg6));
5232 #endif
5233 break;
5234 #endif
5235 #ifdef TARGET_NR_mmap2
5236 case TARGET_NR_mmap2:
5237 #ifndef MMAP_SHIFT
5238 #define MMAP_SHIFT 12
5239 #endif
5240 ret = get_errno(target_mmap(arg1, arg2, arg3,
5241 target_to_host_bitmask(arg4, mmap_flags_tbl),
5242 arg5,
5243 arg6 << MMAP_SHIFT));
5244 break;
5245 #endif
5246 case TARGET_NR_munmap:
5247 ret = get_errno(target_munmap(arg1, arg2));
5248 break;
5249 case TARGET_NR_mprotect:
5250 ret = get_errno(target_mprotect(arg1, arg2, arg3));
5251 break;
5252 #ifdef TARGET_NR_mremap
5253 case TARGET_NR_mremap:
5254 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
5255 break;
5256 #endif
5257 /* ??? msync/mlock/munlock are broken for softmmu. */
5258 #ifdef TARGET_NR_msync
5259 case TARGET_NR_msync:
5260 ret = get_errno(msync(g2h(arg1), arg2, arg3));
5261 break;
5262 #endif
5263 #ifdef TARGET_NR_mlock
5264 case TARGET_NR_mlock:
5265 ret = get_errno(mlock(g2h(arg1), arg2));
5266 break;
5267 #endif
5268 #ifdef TARGET_NR_munlock
5269 case TARGET_NR_munlock:
5270 ret = get_errno(munlock(g2h(arg1), arg2));
5271 break;
5272 #endif
5273 #ifdef TARGET_NR_mlockall
5274 case TARGET_NR_mlockall:
5275 ret = get_errno(mlockall(arg1));
5276 break;
5277 #endif
5278 #ifdef TARGET_NR_munlockall
5279 case TARGET_NR_munlockall:
5280 ret = get_errno(munlockall());
5281 break;
5282 #endif
5283 case TARGET_NR_truncate:
5284 if (!(p = lock_user_string(arg1)))
5285 goto efault;
5286 ret = get_errno(truncate(p, arg2));
5287 unlock_user(p, arg1, 0);
5288 break;
5289 case TARGET_NR_ftruncate:
5290 ret = get_errno(ftruncate(arg1, arg2));
5291 break;
5292 case TARGET_NR_fchmod:
5293 ret = get_errno(fchmod(arg1, arg2));
5294 break;
5295 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5296 case TARGET_NR_fchmodat:
5297 if (!(p = lock_user_string(arg2)))
5298 goto efault;
5299 ret = get_errno(sys_fchmodat(arg1, p, arg3));
5300 unlock_user(p, arg2, 0);
5301 break;
5302 #endif
5303 case TARGET_NR_getpriority:
5304 /* libc does special remapping of the return value of
5305 * sys_getpriority() so it's just easiest to call
5306 * sys_getpriority() directly rather than through libc. */
5307 ret = sys_getpriority(arg1, arg2);
5308 break;
5309 case TARGET_NR_setpriority:
5310 ret = get_errno(setpriority(arg1, arg2, arg3));
5311 break;
5312 #ifdef TARGET_NR_profil
5313 case TARGET_NR_profil:
5314 goto unimplemented;
5315 #endif
5316 case TARGET_NR_statfs:
5317 if (!(p = lock_user_string(arg1)))
5318 goto efault;
5319 ret = get_errno(statfs(path(p), &stfs));
5320 unlock_user(p, arg1, 0);
5321 convert_statfs:
5322 if (!is_error(ret)) {
5323 struct target_statfs *target_stfs;
5325 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5326 goto efault;
5327 __put_user(stfs.f_type, &target_stfs->f_type);
5328 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5329 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5330 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5331 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5332 __put_user(stfs.f_files, &target_stfs->f_files);
5333 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5334 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5335 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5336 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5337 unlock_user_struct(target_stfs, arg2, 1);
5339 break;
5340 case TARGET_NR_fstatfs:
5341 ret = get_errno(fstatfs(arg1, &stfs));
5342 goto convert_statfs;
5343 #ifdef TARGET_NR_statfs64
5344 case TARGET_NR_statfs64:
5345 if (!(p = lock_user_string(arg1)))
5346 goto efault;
5347 ret = get_errno(statfs(path(p), &stfs));
5348 unlock_user(p, arg1, 0);
5349 convert_statfs64:
5350 if (!is_error(ret)) {
5351 struct target_statfs64 *target_stfs;
5353 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5354 goto efault;
5355 __put_user(stfs.f_type, &target_stfs->f_type);
5356 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5357 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5358 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5359 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5360 __put_user(stfs.f_files, &target_stfs->f_files);
5361 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5362 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5363 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5364 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5365 unlock_user_struct(target_stfs, arg3, 1);
5367 break;
5368 case TARGET_NR_fstatfs64:
5369 ret = get_errno(fstatfs(arg1, &stfs));
5370 goto convert_statfs64;
5371 #endif
5372 #ifdef TARGET_NR_ioperm
5373 case TARGET_NR_ioperm:
5374 goto unimplemented;
5375 #endif
5376 #ifdef TARGET_NR_socketcall
5377 case TARGET_NR_socketcall:
5378 ret = do_socketcall(arg1, arg2);
5379 break;
5380 #endif
5381 #ifdef TARGET_NR_accept
5382 case TARGET_NR_accept:
5383 ret = do_accept(arg1, arg2, arg3);
5384 break;
5385 #endif
5386 #ifdef TARGET_NR_bind
5387 case TARGET_NR_bind:
5388 ret = do_bind(arg1, arg2, arg3);
5389 break;
5390 #endif
5391 #ifdef TARGET_NR_connect
5392 case TARGET_NR_connect:
5393 ret = do_connect(arg1, arg2, arg3);
5394 break;
5395 #endif
5396 #ifdef TARGET_NR_getpeername
5397 case TARGET_NR_getpeername:
5398 ret = do_getpeername(arg1, arg2, arg3);
5399 break;
5400 #endif
5401 #ifdef TARGET_NR_getsockname
5402 case TARGET_NR_getsockname:
5403 ret = do_getsockname(arg1, arg2, arg3);
5404 break;
5405 #endif
5406 #ifdef TARGET_NR_getsockopt
5407 case TARGET_NR_getsockopt:
5408 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5409 break;
5410 #endif
5411 #ifdef TARGET_NR_listen
5412 case TARGET_NR_listen:
5413 ret = get_errno(listen(arg1, arg2));
5414 break;
5415 #endif
5416 #ifdef TARGET_NR_recv
5417 case TARGET_NR_recv:
5418 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
5419 break;
5420 #endif
5421 #ifdef TARGET_NR_recvfrom
5422 case TARGET_NR_recvfrom:
5423 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
5424 break;
5425 #endif
5426 #ifdef TARGET_NR_recvmsg
5427 case TARGET_NR_recvmsg:
5428 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5429 break;
5430 #endif
5431 #ifdef TARGET_NR_send
5432 case TARGET_NR_send:
5433 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
5434 break;
5435 #endif
5436 #ifdef TARGET_NR_sendmsg
5437 case TARGET_NR_sendmsg:
5438 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5439 break;
5440 #endif
5441 #ifdef TARGET_NR_sendto
5442 case TARGET_NR_sendto:
5443 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
5444 break;
5445 #endif
5446 #ifdef TARGET_NR_shutdown
5447 case TARGET_NR_shutdown:
5448 ret = get_errno(shutdown(arg1, arg2));
5449 break;
5450 #endif
5451 #ifdef TARGET_NR_socket
5452 case TARGET_NR_socket:
5453 ret = do_socket(arg1, arg2, arg3);
5454 break;
5455 #endif
5456 #ifdef TARGET_NR_socketpair
5457 case TARGET_NR_socketpair:
5458 ret = do_socketpair(arg1, arg2, arg3, arg4);
5459 break;
5460 #endif
5461 #ifdef TARGET_NR_setsockopt
5462 case TARGET_NR_setsockopt:
5463 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5464 break;
5465 #endif
5467 case TARGET_NR_syslog:
5468 if (!(p = lock_user_string(arg2)))
5469 goto efault;
5470 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5471 unlock_user(p, arg2, 0);
5472 break;
5474 case TARGET_NR_setitimer:
5476 struct itimerval value, ovalue, *pvalue;
5478 if (arg2) {
5479 pvalue = &value;
5480 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5481 || copy_from_user_timeval(&pvalue->it_value,
5482 arg2 + sizeof(struct target_timeval)))
5483 goto efault;
5484 } else {
5485 pvalue = NULL;
5487 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
5488 if (!is_error(ret) && arg3) {
5489 if (copy_to_user_timeval(arg3,
5490 &ovalue.it_interval)
5491 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5492 &ovalue.it_value))
5493 goto efault;
5496 break;
5497 case TARGET_NR_getitimer:
5499 struct itimerval value;
5501 ret = get_errno(getitimer(arg1, &value));
5502 if (!is_error(ret) && arg2) {
5503 if (copy_to_user_timeval(arg2,
5504 &value.it_interval)
5505 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5506 &value.it_value))
5507 goto efault;
5510 break;
5511 case TARGET_NR_stat:
5512 if (!(p = lock_user_string(arg1)))
5513 goto efault;
5514 ret = get_errno(stat(path(p), &st));
5515 unlock_user(p, arg1, 0);
5516 goto do_stat;
5517 case TARGET_NR_lstat:
5518 if (!(p = lock_user_string(arg1)))
5519 goto efault;
5520 ret = get_errno(lstat(path(p), &st));
5521 unlock_user(p, arg1, 0);
5522 goto do_stat;
5523 case TARGET_NR_fstat:
5525 ret = get_errno(fstat(arg1, &st));
5526 do_stat:
5527 if (!is_error(ret)) {
5528 struct target_stat *target_st;
5530 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5531 goto efault;
5532 memset(target_st, 0, sizeof(*target_st));
5533 __put_user(st.st_dev, &target_st->st_dev);
5534 __put_user(st.st_ino, &target_st->st_ino);
5535 __put_user(st.st_mode, &target_st->st_mode);
5536 __put_user(st.st_uid, &target_st->st_uid);
5537 __put_user(st.st_gid, &target_st->st_gid);
5538 __put_user(st.st_nlink, &target_st->st_nlink);
5539 __put_user(st.st_rdev, &target_st->st_rdev);
5540 __put_user(st.st_size, &target_st->st_size);
5541 __put_user(st.st_blksize, &target_st->st_blksize);
5542 __put_user(st.st_blocks, &target_st->st_blocks);
5543 __put_user(st.st_atime, &target_st->target_st_atime);
5544 __put_user(st.st_mtime, &target_st->target_st_mtime);
5545 __put_user(st.st_ctime, &target_st->target_st_ctime);
5546 unlock_user_struct(target_st, arg2, 1);
5549 break;
5550 #ifdef TARGET_NR_olduname
5551 case TARGET_NR_olduname:
5552 goto unimplemented;
5553 #endif
5554 #ifdef TARGET_NR_iopl
5555 case TARGET_NR_iopl:
5556 goto unimplemented;
5557 #endif
5558 case TARGET_NR_vhangup:
5559 ret = get_errno(vhangup());
5560 break;
5561 #ifdef TARGET_NR_idle
5562 case TARGET_NR_idle:
5563 goto unimplemented;
5564 #endif
5565 #ifdef TARGET_NR_syscall
5566 case TARGET_NR_syscall:
5567 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5568 break;
5569 #endif
5570 case TARGET_NR_wait4:
5572 int status;
5573 abi_long status_ptr = arg2;
5574 struct rusage rusage, *rusage_ptr;
5575 abi_ulong target_rusage = arg4;
5576 if (target_rusage)
5577 rusage_ptr = &rusage;
5578 else
5579 rusage_ptr = NULL;
5580 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5581 if (!is_error(ret)) {
5582 if (status_ptr) {
5583 status = host_to_target_waitstatus(status);
5584 if (put_user_s32(status, status_ptr))
5585 goto efault;
5587 if (target_rusage)
5588 host_to_target_rusage(target_rusage, &rusage);
5591 break;
5592 #ifdef TARGET_NR_swapoff
5593 case TARGET_NR_swapoff:
5594 if (!(p = lock_user_string(arg1)))
5595 goto efault;
5596 ret = get_errno(swapoff(p));
5597 unlock_user(p, arg1, 0);
5598 break;
5599 #endif
5600 case TARGET_NR_sysinfo:
5602 struct target_sysinfo *target_value;
5603 struct sysinfo value;
5604 ret = get_errno(sysinfo(&value));
5605 if (!is_error(ret) && arg1)
5607 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5608 goto efault;
5609 __put_user(value.uptime, &target_value->uptime);
5610 __put_user(value.loads[0], &target_value->loads[0]);
5611 __put_user(value.loads[1], &target_value->loads[1]);
5612 __put_user(value.loads[2], &target_value->loads[2]);
5613 __put_user(value.totalram, &target_value->totalram);
5614 __put_user(value.freeram, &target_value->freeram);
5615 __put_user(value.sharedram, &target_value->sharedram);
5616 __put_user(value.bufferram, &target_value->bufferram);
5617 __put_user(value.totalswap, &target_value->totalswap);
5618 __put_user(value.freeswap, &target_value->freeswap);
5619 __put_user(value.procs, &target_value->procs);
5620 __put_user(value.totalhigh, &target_value->totalhigh);
5621 __put_user(value.freehigh, &target_value->freehigh);
5622 __put_user(value.mem_unit, &target_value->mem_unit);
5623 unlock_user_struct(target_value, arg1, 1);
5626 break;
5627 #ifdef TARGET_NR_ipc
5628 case TARGET_NR_ipc:
5629 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5630 break;
5631 #endif
5632 #ifdef TARGET_NR_semget
5633 case TARGET_NR_semget:
5634 ret = get_errno(semget(arg1, arg2, arg3));
5635 break;
5636 #endif
5637 #ifdef TARGET_NR_semop
5638 case TARGET_NR_semop:
5639 ret = get_errno(do_semop(arg1, arg2, arg3));
5640 break;
5641 #endif
5642 #ifdef TARGET_NR_semctl
5643 case TARGET_NR_semctl:
5644 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5645 break;
5646 #endif
5647 #ifdef TARGET_NR_msgctl
5648 case TARGET_NR_msgctl:
5649 ret = do_msgctl(arg1, arg2, arg3);
5650 break;
5651 #endif
5652 #ifdef TARGET_NR_msgget
5653 case TARGET_NR_msgget:
5654 ret = get_errno(msgget(arg1, arg2));
5655 break;
5656 #endif
5657 #ifdef TARGET_NR_msgrcv
5658 case TARGET_NR_msgrcv:
5659 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5660 break;
5661 #endif
5662 #ifdef TARGET_NR_msgsnd
5663 case TARGET_NR_msgsnd:
5664 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5665 break;
5666 #endif
5667 #ifdef TARGET_NR_shmget
5668 case TARGET_NR_shmget:
5669 ret = get_errno(shmget(arg1, arg2, arg3));
5670 break;
5671 #endif
5672 #ifdef TARGET_NR_shmctl
5673 case TARGET_NR_shmctl:
5674 ret = do_shmctl(arg1, arg2, arg3);
5675 break;
5676 #endif
5677 #ifdef TARGET_NR_shmat
5678 case TARGET_NR_shmat:
5679 ret = do_shmat(arg1, arg2, arg3);
5680 break;
5681 #endif
5682 #ifdef TARGET_NR_shmdt
5683 case TARGET_NR_shmdt:
5684 ret = do_shmdt(arg1);
5685 break;
5686 #endif
5687 case TARGET_NR_fsync:
5688 ret = get_errno(fsync(arg1));
5689 break;
5690 case TARGET_NR_clone:
5691 #if defined(TARGET_SH4)
5692 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
5693 #elif defined(TARGET_CRIS)
5694 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
5695 #else
5696 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
5697 #endif
5698 break;
5699 #ifdef __NR_exit_group
5700 /* new thread calls */
5701 case TARGET_NR_exit_group:
5702 #ifdef TARGET_GPROF
5703 _mcleanup();
5704 #endif
5705 gdb_exit(cpu_env, arg1);
5706 ret = get_errno(exit_group(arg1));
5707 break;
5708 #endif
5709 case TARGET_NR_setdomainname:
5710 if (!(p = lock_user_string(arg1)))
5711 goto efault;
5712 ret = get_errno(setdomainname(p, arg2));
5713 unlock_user(p, arg1, 0);
5714 break;
5715 case TARGET_NR_uname:
5716 /* no need to transcode because we use the linux syscall */
5718 struct new_utsname * buf;
5720 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5721 goto efault;
5722 ret = get_errno(sys_uname(buf));
5723 if (!is_error(ret)) {
5724 /* Overrite the native machine name with whatever is being
5725 emulated. */
5726 strcpy (buf->machine, UNAME_MACHINE);
5727 /* Allow the user to override the reported release. */
5728 if (qemu_uname_release && *qemu_uname_release)
5729 strcpy (buf->release, qemu_uname_release);
5731 unlock_user_struct(buf, arg1, 1);
5733 break;
5734 #ifdef TARGET_I386
5735 case TARGET_NR_modify_ldt:
5736 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5737 break;
5738 #if !defined(TARGET_X86_64)
5739 case TARGET_NR_vm86old:
5740 goto unimplemented;
5741 case TARGET_NR_vm86:
5742 ret = do_vm86(cpu_env, arg1, arg2);
5743 break;
5744 #endif
5745 #endif
5746 case TARGET_NR_adjtimex:
5747 goto unimplemented;
5748 #ifdef TARGET_NR_create_module
5749 case TARGET_NR_create_module:
5750 #endif
5751 case TARGET_NR_init_module:
5752 case TARGET_NR_delete_module:
5753 #ifdef TARGET_NR_get_kernel_syms
5754 case TARGET_NR_get_kernel_syms:
5755 #endif
5756 goto unimplemented;
5757 case TARGET_NR_quotactl:
5758 goto unimplemented;
5759 case TARGET_NR_getpgid:
5760 ret = get_errno(getpgid(arg1));
5761 break;
5762 case TARGET_NR_fchdir:
5763 ret = get_errno(fchdir(arg1));
5764 break;
5765 #ifdef TARGET_NR_bdflush /* not on x86_64 */
5766 case TARGET_NR_bdflush:
5767 goto unimplemented;
5768 #endif
5769 #ifdef TARGET_NR_sysfs
5770 case TARGET_NR_sysfs:
5771 goto unimplemented;
5772 #endif
5773 case TARGET_NR_personality:
5774 ret = get_errno(personality(arg1));
5775 break;
5776 #ifdef TARGET_NR_afs_syscall
5777 case TARGET_NR_afs_syscall:
5778 goto unimplemented;
5779 #endif
5780 #ifdef TARGET_NR__llseek /* Not on alpha */
5781 case TARGET_NR__llseek:
5783 #if defined (__x86_64__)
5784 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
5785 if (put_user_s64(ret, arg4))
5786 goto efault;
5787 #else
5788 int64_t res;
5789 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
5790 if (put_user_s64(res, arg4))
5791 goto efault;
5792 #endif
5794 break;
5795 #endif
5796 case TARGET_NR_getdents:
5797 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
5799 struct target_dirent *target_dirp;
5800 struct linux_dirent *dirp;
5801 abi_long count = arg3;
5803 dirp = malloc(count);
5804 if (!dirp) {
5805 ret = -TARGET_ENOMEM;
5806 goto fail;
5809 ret = get_errno(sys_getdents(arg1, dirp, count));
5810 if (!is_error(ret)) {
5811 struct linux_dirent *de;
5812 struct target_dirent *tde;
5813 int len = ret;
5814 int reclen, treclen;
5815 int count1, tnamelen;
5817 count1 = 0;
5818 de = dirp;
5819 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5820 goto efault;
5821 tde = target_dirp;
5822 while (len > 0) {
5823 reclen = de->d_reclen;
5824 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
5825 tde->d_reclen = tswap16(treclen);
5826 tde->d_ino = tswapl(de->d_ino);
5827 tde->d_off = tswapl(de->d_off);
5828 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
5829 if (tnamelen > 256)
5830 tnamelen = 256;
5831 /* XXX: may not be correct */
5832 pstrcpy(tde->d_name, tnamelen, de->d_name);
5833 de = (struct linux_dirent *)((char *)de + reclen);
5834 len -= reclen;
5835 tde = (struct target_dirent *)((char *)tde + treclen);
5836 count1 += treclen;
5838 ret = count1;
5839 unlock_user(target_dirp, arg2, ret);
5841 free(dirp);
5843 #else
5845 struct linux_dirent *dirp;
5846 abi_long count = arg3;
5848 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5849 goto efault;
5850 ret = get_errno(sys_getdents(arg1, dirp, count));
5851 if (!is_error(ret)) {
5852 struct linux_dirent *de;
5853 int len = ret;
5854 int reclen;
5855 de = dirp;
5856 while (len > 0) {
5857 reclen = de->d_reclen;
5858 if (reclen > len)
5859 break;
5860 de->d_reclen = tswap16(reclen);
5861 tswapls(&de->d_ino);
5862 tswapls(&de->d_off);
5863 de = (struct linux_dirent *)((char *)de + reclen);
5864 len -= reclen;
5867 unlock_user(dirp, arg2, ret);
5869 #endif
5870 break;
5871 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
5872 case TARGET_NR_getdents64:
5874 struct linux_dirent64 *dirp;
5875 abi_long count = arg3;
5876 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5877 goto efault;
5878 ret = get_errno(sys_getdents64(arg1, dirp, count));
5879 if (!is_error(ret)) {
5880 struct linux_dirent64 *de;
5881 int len = ret;
5882 int reclen;
5883 de = dirp;
5884 while (len > 0) {
5885 reclen = de->d_reclen;
5886 if (reclen > len)
5887 break;
5888 de->d_reclen = tswap16(reclen);
5889 tswap64s((uint64_t *)&de->d_ino);
5890 tswap64s((uint64_t *)&de->d_off);
5891 de = (struct linux_dirent64 *)((char *)de + reclen);
5892 len -= reclen;
5895 unlock_user(dirp, arg2, ret);
5897 break;
5898 #endif /* TARGET_NR_getdents64 */
5899 #ifdef TARGET_NR__newselect
5900 case TARGET_NR__newselect:
5901 ret = do_select(arg1, arg2, arg3, arg4, arg5);
5902 break;
5903 #endif
5904 #ifdef TARGET_NR_poll
5905 case TARGET_NR_poll:
5907 struct target_pollfd *target_pfd;
5908 unsigned int nfds = arg2;
5909 int timeout = arg3;
5910 struct pollfd *pfd;
5911 unsigned int i;
5913 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
5914 if (!target_pfd)
5915 goto efault;
5916 pfd = alloca(sizeof(struct pollfd) * nfds);
5917 for(i = 0; i < nfds; i++) {
5918 pfd[i].fd = tswap32(target_pfd[i].fd);
5919 pfd[i].events = tswap16(target_pfd[i].events);
5921 ret = get_errno(poll(pfd, nfds, timeout));
5922 if (!is_error(ret)) {
5923 for(i = 0; i < nfds; i++) {
5924 target_pfd[i].revents = tswap16(pfd[i].revents);
5926 ret += nfds * (sizeof(struct target_pollfd)
5927 - sizeof(struct pollfd));
5929 unlock_user(target_pfd, arg1, ret);
5931 break;
5932 #endif
5933 case TARGET_NR_flock:
5934 /* NOTE: the flock constant seems to be the same for every
5935 Linux platform */
5936 ret = get_errno(flock(arg1, arg2));
5937 break;
5938 case TARGET_NR_readv:
5940 int count = arg3;
5941 struct iovec *vec;
5943 vec = alloca(count * sizeof(struct iovec));
5944 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
5945 goto efault;
5946 ret = get_errno(readv(arg1, vec, count));
5947 unlock_iovec(vec, arg2, count, 1);
5949 break;
5950 case TARGET_NR_writev:
5952 int count = arg3;
5953 struct iovec *vec;
5955 vec = alloca(count * sizeof(struct iovec));
5956 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
5957 goto efault;
5958 ret = get_errno(writev(arg1, vec, count));
5959 unlock_iovec(vec, arg2, count, 0);
5961 break;
5962 case TARGET_NR_getsid:
5963 ret = get_errno(getsid(arg1));
5964 break;
5965 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
5966 case TARGET_NR_fdatasync:
5967 ret = get_errno(fdatasync(arg1));
5968 break;
5969 #endif
5970 case TARGET_NR__sysctl:
5971 /* We don't implement this, but ENOTDIR is always a safe
5972 return value. */
5973 ret = -TARGET_ENOTDIR;
5974 break;
5975 case TARGET_NR_sched_setparam:
5977 struct sched_param *target_schp;
5978 struct sched_param schp;
5980 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
5981 goto efault;
5982 schp.sched_priority = tswap32(target_schp->sched_priority);
5983 unlock_user_struct(target_schp, arg2, 0);
5984 ret = get_errno(sched_setparam(arg1, &schp));
5986 break;
5987 case TARGET_NR_sched_getparam:
5989 struct sched_param *target_schp;
5990 struct sched_param schp;
5991 ret = get_errno(sched_getparam(arg1, &schp));
5992 if (!is_error(ret)) {
5993 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
5994 goto efault;
5995 target_schp->sched_priority = tswap32(schp.sched_priority);
5996 unlock_user_struct(target_schp, arg2, 1);
5999 break;
6000 case TARGET_NR_sched_setscheduler:
6002 struct sched_param *target_schp;
6003 struct sched_param schp;
6004 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6005 goto efault;
6006 schp.sched_priority = tswap32(target_schp->sched_priority);
6007 unlock_user_struct(target_schp, arg3, 0);
6008 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6010 break;
6011 case TARGET_NR_sched_getscheduler:
6012 ret = get_errno(sched_getscheduler(arg1));
6013 break;
6014 case TARGET_NR_sched_yield:
6015 ret = get_errno(sched_yield());
6016 break;
6017 case TARGET_NR_sched_get_priority_max:
6018 ret = get_errno(sched_get_priority_max(arg1));
6019 break;
6020 case TARGET_NR_sched_get_priority_min:
6021 ret = get_errno(sched_get_priority_min(arg1));
6022 break;
6023 case TARGET_NR_sched_rr_get_interval:
6025 struct timespec ts;
6026 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6027 if (!is_error(ret)) {
6028 host_to_target_timespec(arg2, &ts);
6031 break;
6032 case TARGET_NR_nanosleep:
6034 struct timespec req, rem;
6035 target_to_host_timespec(&req, arg1);
6036 ret = get_errno(nanosleep(&req, &rem));
6037 if (is_error(ret) && arg2) {
6038 host_to_target_timespec(arg2, &rem);
6041 break;
6042 #ifdef TARGET_NR_query_module
6043 case TARGET_NR_query_module:
6044 goto unimplemented;
6045 #endif
6046 #ifdef TARGET_NR_nfsservctl
6047 case TARGET_NR_nfsservctl:
6048 goto unimplemented;
6049 #endif
6050 case TARGET_NR_prctl:
6051 switch (arg1)
6053 case PR_GET_PDEATHSIG:
6055 int deathsig;
6056 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
6057 if (!is_error(ret) && arg2
6058 && put_user_ual(deathsig, arg2))
6059 goto efault;
6061 break;
6062 default:
6063 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6064 break;
6066 break;
6067 #ifdef TARGET_NR_arch_prctl
6068 case TARGET_NR_arch_prctl:
6069 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
6070 ret = do_arch_prctl(cpu_env, arg1, arg2);
6071 break;
6072 #else
6073 goto unimplemented;
6074 #endif
6075 #endif
6076 #ifdef TARGET_NR_pread
6077 case TARGET_NR_pread:
6078 #ifdef TARGET_ARM
6079 if (((CPUARMState *)cpu_env)->eabi)
6080 arg4 = arg5;
6081 #endif
6082 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6083 goto efault;
6084 ret = get_errno(pread(arg1, p, arg3, arg4));
6085 unlock_user(p, arg2, ret);
6086 break;
6087 case TARGET_NR_pwrite:
6088 #ifdef TARGET_ARM
6089 if (((CPUARMState *)cpu_env)->eabi)
6090 arg4 = arg5;
6091 #endif
6092 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6093 goto efault;
6094 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6095 unlock_user(p, arg2, 0);
6096 break;
6097 #endif
6098 #ifdef TARGET_NR_pread64
6099 case TARGET_NR_pread64:
6100 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6101 goto efault;
6102 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6103 unlock_user(p, arg2, ret);
6104 break;
6105 case TARGET_NR_pwrite64:
6106 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6107 goto efault;
6108 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6109 unlock_user(p, arg2, 0);
6110 break;
6111 #endif
6112 case TARGET_NR_getcwd:
6113 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6114 goto efault;
6115 ret = get_errno(sys_getcwd1(p, arg2));
6116 unlock_user(p, arg1, ret);
6117 break;
6118 case TARGET_NR_capget:
6119 goto unimplemented;
6120 case TARGET_NR_capset:
6121 goto unimplemented;
6122 case TARGET_NR_sigaltstack:
6123 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
6124 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6125 defined(TARGET_M68K)
6126 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
6127 break;
6128 #else
6129 goto unimplemented;
6130 #endif
6131 case TARGET_NR_sendfile:
6132 goto unimplemented;
6133 #ifdef TARGET_NR_getpmsg
6134 case TARGET_NR_getpmsg:
6135 goto unimplemented;
6136 #endif
6137 #ifdef TARGET_NR_putpmsg
6138 case TARGET_NR_putpmsg:
6139 goto unimplemented;
6140 #endif
6141 #ifdef TARGET_NR_vfork
6142 case TARGET_NR_vfork:
6143 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6144 0, 0, 0, 0));
6145 break;
6146 #endif
6147 #ifdef TARGET_NR_ugetrlimit
6148 case TARGET_NR_ugetrlimit:
6150 struct rlimit rlim;
6151 ret = get_errno(getrlimit(arg1, &rlim));
6152 if (!is_error(ret)) {
6153 struct target_rlimit *target_rlim;
6154 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6155 goto efault;
6156 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
6157 target_rlim->rlim_max = tswapl(rlim.rlim_max);
6158 unlock_user_struct(target_rlim, arg2, 1);
6160 break;
6162 #endif
6163 #ifdef TARGET_NR_truncate64
6164 case TARGET_NR_truncate64:
6165 if (!(p = lock_user_string(arg1)))
6166 goto efault;
6167 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6168 unlock_user(p, arg1, 0);
6169 break;
6170 #endif
6171 #ifdef TARGET_NR_ftruncate64
6172 case TARGET_NR_ftruncate64:
6173 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
6174 break;
6175 #endif
6176 #ifdef TARGET_NR_stat64
6177 case TARGET_NR_stat64:
6178 if (!(p = lock_user_string(arg1)))
6179 goto efault;
6180 ret = get_errno(stat(path(p), &st));
6181 unlock_user(p, arg1, 0);
6182 if (!is_error(ret))
6183 ret = host_to_target_stat64(cpu_env, arg2, &st);
6184 break;
6185 #endif
6186 #ifdef TARGET_NR_lstat64
6187 case TARGET_NR_lstat64:
6188 if (!(p = lock_user_string(arg1)))
6189 goto efault;
6190 ret = get_errno(lstat(path(p), &st));
6191 unlock_user(p, arg1, 0);
6192 if (!is_error(ret))
6193 ret = host_to_target_stat64(cpu_env, arg2, &st);
6194 break;
6195 #endif
6196 #ifdef TARGET_NR_fstat64
6197 case TARGET_NR_fstat64:
6198 ret = get_errno(fstat(arg1, &st));
6199 if (!is_error(ret))
6200 ret = host_to_target_stat64(cpu_env, arg2, &st);
6201 break;
6202 #endif
6203 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6204 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6205 #ifdef TARGET_NR_fstatat64
6206 case TARGET_NR_fstatat64:
6207 #endif
6208 #ifdef TARGET_NR_newfstatat
6209 case TARGET_NR_newfstatat:
6210 #endif
6211 if (!(p = lock_user_string(arg2)))
6212 goto efault;
6213 #ifdef __NR_fstatat64
6214 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
6215 #else
6216 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6217 #endif
6218 if (!is_error(ret))
6219 ret = host_to_target_stat64(cpu_env, arg3, &st);
6220 break;
6221 #endif
6222 #ifdef USE_UID16
6223 case TARGET_NR_lchown:
6224 if (!(p = lock_user_string(arg1)))
6225 goto efault;
6226 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6227 unlock_user(p, arg1, 0);
6228 break;
6229 case TARGET_NR_getuid:
6230 ret = get_errno(high2lowuid(getuid()));
6231 break;
6232 case TARGET_NR_getgid:
6233 ret = get_errno(high2lowgid(getgid()));
6234 break;
6235 case TARGET_NR_geteuid:
6236 ret = get_errno(high2lowuid(geteuid()));
6237 break;
6238 case TARGET_NR_getegid:
6239 ret = get_errno(high2lowgid(getegid()));
6240 break;
6241 case TARGET_NR_setreuid:
6242 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6243 break;
6244 case TARGET_NR_setregid:
6245 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6246 break;
6247 case TARGET_NR_getgroups:
6249 int gidsetsize = arg1;
6250 uint16_t *target_grouplist;
6251 gid_t *grouplist;
6252 int i;
6254 grouplist = alloca(gidsetsize * sizeof(gid_t));
6255 ret = get_errno(getgroups(gidsetsize, grouplist));
6256 if (gidsetsize == 0)
6257 break;
6258 if (!is_error(ret)) {
6259 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6260 if (!target_grouplist)
6261 goto efault;
6262 for(i = 0;i < ret; i++)
6263 target_grouplist[i] = tswap16(grouplist[i]);
6264 unlock_user(target_grouplist, arg2, gidsetsize * 2);
6267 break;
6268 case TARGET_NR_setgroups:
6270 int gidsetsize = arg1;
6271 uint16_t *target_grouplist;
6272 gid_t *grouplist;
6273 int i;
6275 grouplist = alloca(gidsetsize * sizeof(gid_t));
6276 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6277 if (!target_grouplist) {
6278 ret = -TARGET_EFAULT;
6279 goto fail;
6281 for(i = 0;i < gidsetsize; i++)
6282 grouplist[i] = tswap16(target_grouplist[i]);
6283 unlock_user(target_grouplist, arg2, 0);
6284 ret = get_errno(setgroups(gidsetsize, grouplist));
6286 break;
6287 case TARGET_NR_fchown:
6288 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6289 break;
6290 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6291 case TARGET_NR_fchownat:
6292 if (!(p = lock_user_string(arg2)))
6293 goto efault;
6294 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6295 unlock_user(p, arg2, 0);
6296 break;
6297 #endif
6298 #ifdef TARGET_NR_setresuid
6299 case TARGET_NR_setresuid:
6300 ret = get_errno(setresuid(low2highuid(arg1),
6301 low2highuid(arg2),
6302 low2highuid(arg3)));
6303 break;
6304 #endif
6305 #ifdef TARGET_NR_getresuid
6306 case TARGET_NR_getresuid:
6308 uid_t ruid, euid, suid;
6309 ret = get_errno(getresuid(&ruid, &euid, &suid));
6310 if (!is_error(ret)) {
6311 if (put_user_u16(high2lowuid(ruid), arg1)
6312 || put_user_u16(high2lowuid(euid), arg2)
6313 || put_user_u16(high2lowuid(suid), arg3))
6314 goto efault;
6317 break;
6318 #endif
6319 #ifdef TARGET_NR_getresgid
6320 case TARGET_NR_setresgid:
6321 ret = get_errno(setresgid(low2highgid(arg1),
6322 low2highgid(arg2),
6323 low2highgid(arg3)));
6324 break;
6325 #endif
6326 #ifdef TARGET_NR_getresgid
6327 case TARGET_NR_getresgid:
6329 gid_t rgid, egid, sgid;
6330 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6331 if (!is_error(ret)) {
6332 if (put_user_u16(high2lowgid(rgid), arg1)
6333 || put_user_u16(high2lowgid(egid), arg2)
6334 || put_user_u16(high2lowgid(sgid), arg3))
6335 goto efault;
6338 break;
6339 #endif
6340 case TARGET_NR_chown:
6341 if (!(p = lock_user_string(arg1)))
6342 goto efault;
6343 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6344 unlock_user(p, arg1, 0);
6345 break;
6346 case TARGET_NR_setuid:
6347 ret = get_errno(setuid(low2highuid(arg1)));
6348 break;
6349 case TARGET_NR_setgid:
6350 ret = get_errno(setgid(low2highgid(arg1)));
6351 break;
6352 case TARGET_NR_setfsuid:
6353 ret = get_errno(setfsuid(arg1));
6354 break;
6355 case TARGET_NR_setfsgid:
6356 ret = get_errno(setfsgid(arg1));
6357 break;
6358 #endif /* USE_UID16 */
6360 #ifdef TARGET_NR_lchown32
6361 case TARGET_NR_lchown32:
6362 if (!(p = lock_user_string(arg1)))
6363 goto efault;
6364 ret = get_errno(lchown(p, arg2, arg3));
6365 unlock_user(p, arg1, 0);
6366 break;
6367 #endif
6368 #ifdef TARGET_NR_getuid32
6369 case TARGET_NR_getuid32:
6370 ret = get_errno(getuid());
6371 break;
6372 #endif
6374 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6375 /* Alpha specific */
6376 case TARGET_NR_getxuid:
6378 uid_t euid;
6379 euid=geteuid();
6380 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6382 ret = get_errno(getuid());
6383 break;
6384 #endif
6385 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6386 /* Alpha specific */
6387 case TARGET_NR_getxgid:
6389 uid_t egid;
6390 egid=getegid();
6391 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6393 ret = get_errno(getgid());
6394 break;
6395 #endif
6397 #ifdef TARGET_NR_getgid32
6398 case TARGET_NR_getgid32:
6399 ret = get_errno(getgid());
6400 break;
6401 #endif
6402 #ifdef TARGET_NR_geteuid32
6403 case TARGET_NR_geteuid32:
6404 ret = get_errno(geteuid());
6405 break;
6406 #endif
6407 #ifdef TARGET_NR_getegid32
6408 case TARGET_NR_getegid32:
6409 ret = get_errno(getegid());
6410 break;
6411 #endif
6412 #ifdef TARGET_NR_setreuid32
6413 case TARGET_NR_setreuid32:
6414 ret = get_errno(setreuid(arg1, arg2));
6415 break;
6416 #endif
6417 #ifdef TARGET_NR_setregid32
6418 case TARGET_NR_setregid32:
6419 ret = get_errno(setregid(arg1, arg2));
6420 break;
6421 #endif
6422 #ifdef TARGET_NR_getgroups32
6423 case TARGET_NR_getgroups32:
6425 int gidsetsize = arg1;
6426 uint32_t *target_grouplist;
6427 gid_t *grouplist;
6428 int i;
6430 grouplist = alloca(gidsetsize * sizeof(gid_t));
6431 ret = get_errno(getgroups(gidsetsize, grouplist));
6432 if (gidsetsize == 0)
6433 break;
6434 if (!is_error(ret)) {
6435 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6436 if (!target_grouplist) {
6437 ret = -TARGET_EFAULT;
6438 goto fail;
6440 for(i = 0;i < ret; i++)
6441 target_grouplist[i] = tswap32(grouplist[i]);
6442 unlock_user(target_grouplist, arg2, gidsetsize * 4);
6445 break;
6446 #endif
6447 #ifdef TARGET_NR_setgroups32
6448 case TARGET_NR_setgroups32:
6450 int gidsetsize = arg1;
6451 uint32_t *target_grouplist;
6452 gid_t *grouplist;
6453 int i;
6455 grouplist = alloca(gidsetsize * sizeof(gid_t));
6456 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6457 if (!target_grouplist) {
6458 ret = -TARGET_EFAULT;
6459 goto fail;
6461 for(i = 0;i < gidsetsize; i++)
6462 grouplist[i] = tswap32(target_grouplist[i]);
6463 unlock_user(target_grouplist, arg2, 0);
6464 ret = get_errno(setgroups(gidsetsize, grouplist));
6466 break;
6467 #endif
6468 #ifdef TARGET_NR_fchown32
6469 case TARGET_NR_fchown32:
6470 ret = get_errno(fchown(arg1, arg2, arg3));
6471 break;
6472 #endif
6473 #ifdef TARGET_NR_setresuid32
6474 case TARGET_NR_setresuid32:
6475 ret = get_errno(setresuid(arg1, arg2, arg3));
6476 break;
6477 #endif
6478 #ifdef TARGET_NR_getresuid32
6479 case TARGET_NR_getresuid32:
6481 uid_t ruid, euid, suid;
6482 ret = get_errno(getresuid(&ruid, &euid, &suid));
6483 if (!is_error(ret)) {
6484 if (put_user_u32(ruid, arg1)
6485 || put_user_u32(euid, arg2)
6486 || put_user_u32(suid, arg3))
6487 goto efault;
6490 break;
6491 #endif
6492 #ifdef TARGET_NR_setresgid32
6493 case TARGET_NR_setresgid32:
6494 ret = get_errno(setresgid(arg1, arg2, arg3));
6495 break;
6496 #endif
6497 #ifdef TARGET_NR_getresgid32
6498 case TARGET_NR_getresgid32:
6500 gid_t rgid, egid, sgid;
6501 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6502 if (!is_error(ret)) {
6503 if (put_user_u32(rgid, arg1)
6504 || put_user_u32(egid, arg2)
6505 || put_user_u32(sgid, arg3))
6506 goto efault;
6509 break;
6510 #endif
6511 #ifdef TARGET_NR_chown32
6512 case TARGET_NR_chown32:
6513 if (!(p = lock_user_string(arg1)))
6514 goto efault;
6515 ret = get_errno(chown(p, arg2, arg3));
6516 unlock_user(p, arg1, 0);
6517 break;
6518 #endif
6519 #ifdef TARGET_NR_setuid32
6520 case TARGET_NR_setuid32:
6521 ret = get_errno(setuid(arg1));
6522 break;
6523 #endif
6524 #ifdef TARGET_NR_setgid32
6525 case TARGET_NR_setgid32:
6526 ret = get_errno(setgid(arg1));
6527 break;
6528 #endif
6529 #ifdef TARGET_NR_setfsuid32
6530 case TARGET_NR_setfsuid32:
6531 ret = get_errno(setfsuid(arg1));
6532 break;
6533 #endif
6534 #ifdef TARGET_NR_setfsgid32
6535 case TARGET_NR_setfsgid32:
6536 ret = get_errno(setfsgid(arg1));
6537 break;
6538 #endif
6540 case TARGET_NR_pivot_root:
6541 goto unimplemented;
6542 #ifdef TARGET_NR_mincore
6543 case TARGET_NR_mincore:
6545 void *a;
6546 ret = -TARGET_EFAULT;
6547 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6548 goto efault;
6549 if (!(p = lock_user_string(arg3)))
6550 goto mincore_fail;
6551 ret = get_errno(mincore(a, arg2, p));
6552 unlock_user(p, arg3, ret);
6553 mincore_fail:
6554 unlock_user(a, arg1, 0);
6556 break;
6557 #endif
6558 #ifdef TARGET_NR_arm_fadvise64_64
6559 case TARGET_NR_arm_fadvise64_64:
6562 * arm_fadvise64_64 looks like fadvise64_64 but
6563 * with different argument order
6565 abi_long temp;
6566 temp = arg3;
6567 arg3 = arg4;
6568 arg4 = temp;
6570 #endif
6571 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
6572 #ifdef TARGET_NR_fadvise64_64
6573 case TARGET_NR_fadvise64_64:
6574 #endif
6575 #ifdef TARGET_NR_fadvise64
6576 case TARGET_NR_fadvise64:
6577 #endif
6578 #ifdef TARGET_S390X
6579 switch (arg4) {
6580 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
6581 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
6582 case 6: arg4 = POSIX_FADV_DONTNEED; break;
6583 case 7: arg4 = POSIX_FADV_NOREUSE; break;
6584 default: break;
6586 #endif
6587 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
6588 break;
6589 #endif
6590 #ifdef TARGET_NR_madvise
6591 case TARGET_NR_madvise:
6592 /* A straight passthrough may not be safe because qemu sometimes
6593 turns private flie-backed mappings into anonymous mappings.
6594 This will break MADV_DONTNEED.
6595 This is a hint, so ignoring and returning success is ok. */
6596 ret = get_errno(0);
6597 break;
6598 #endif
6599 #if TARGET_ABI_BITS == 32
6600 case TARGET_NR_fcntl64:
6602 int cmd;
6603 struct flock64 fl;
6604 struct target_flock64 *target_fl;
6605 #ifdef TARGET_ARM
6606 struct target_eabi_flock64 *target_efl;
6607 #endif
6609 cmd = target_to_host_fcntl_cmd(arg2);
6610 if (cmd == -TARGET_EINVAL)
6611 return cmd;
6613 switch(arg2) {
6614 case TARGET_F_GETLK64:
6615 #ifdef TARGET_ARM
6616 if (((CPUARMState *)cpu_env)->eabi) {
6617 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6618 goto efault;
6619 fl.l_type = tswap16(target_efl->l_type);
6620 fl.l_whence = tswap16(target_efl->l_whence);
6621 fl.l_start = tswap64(target_efl->l_start);
6622 fl.l_len = tswap64(target_efl->l_len);
6623 fl.l_pid = tswap32(target_efl->l_pid);
6624 unlock_user_struct(target_efl, arg3, 0);
6625 } else
6626 #endif
6628 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6629 goto efault;
6630 fl.l_type = tswap16(target_fl->l_type);
6631 fl.l_whence = tswap16(target_fl->l_whence);
6632 fl.l_start = tswap64(target_fl->l_start);
6633 fl.l_len = tswap64(target_fl->l_len);
6634 fl.l_pid = tswap32(target_fl->l_pid);
6635 unlock_user_struct(target_fl, arg3, 0);
6637 ret = get_errno(fcntl(arg1, cmd, &fl));
6638 if (ret == 0) {
6639 #ifdef TARGET_ARM
6640 if (((CPUARMState *)cpu_env)->eabi) {
6641 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6642 goto efault;
6643 target_efl->l_type = tswap16(fl.l_type);
6644 target_efl->l_whence = tswap16(fl.l_whence);
6645 target_efl->l_start = tswap64(fl.l_start);
6646 target_efl->l_len = tswap64(fl.l_len);
6647 target_efl->l_pid = tswap32(fl.l_pid);
6648 unlock_user_struct(target_efl, arg3, 1);
6649 } else
6650 #endif
6652 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6653 goto efault;
6654 target_fl->l_type = tswap16(fl.l_type);
6655 target_fl->l_whence = tswap16(fl.l_whence);
6656 target_fl->l_start = tswap64(fl.l_start);
6657 target_fl->l_len = tswap64(fl.l_len);
6658 target_fl->l_pid = tswap32(fl.l_pid);
6659 unlock_user_struct(target_fl, arg3, 1);
6662 break;
6664 case TARGET_F_SETLK64:
6665 case TARGET_F_SETLKW64:
6666 #ifdef TARGET_ARM
6667 if (((CPUARMState *)cpu_env)->eabi) {
6668 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6669 goto efault;
6670 fl.l_type = tswap16(target_efl->l_type);
6671 fl.l_whence = tswap16(target_efl->l_whence);
6672 fl.l_start = tswap64(target_efl->l_start);
6673 fl.l_len = tswap64(target_efl->l_len);
6674 fl.l_pid = tswap32(target_efl->l_pid);
6675 unlock_user_struct(target_efl, arg3, 0);
6676 } else
6677 #endif
6679 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6680 goto efault;
6681 fl.l_type = tswap16(target_fl->l_type);
6682 fl.l_whence = tswap16(target_fl->l_whence);
6683 fl.l_start = tswap64(target_fl->l_start);
6684 fl.l_len = tswap64(target_fl->l_len);
6685 fl.l_pid = tswap32(target_fl->l_pid);
6686 unlock_user_struct(target_fl, arg3, 0);
6688 ret = get_errno(fcntl(arg1, cmd, &fl));
6689 break;
6690 default:
6691 ret = do_fcntl(arg1, arg2, arg3);
6692 break;
6694 break;
6696 #endif
6697 #ifdef TARGET_NR_cacheflush
6698 case TARGET_NR_cacheflush:
6699 /* self-modifying code is handled automatically, so nothing needed */
6700 ret = 0;
6701 break;
6702 #endif
6703 #ifdef TARGET_NR_security
6704 case TARGET_NR_security:
6705 goto unimplemented;
6706 #endif
6707 #ifdef TARGET_NR_getpagesize
6708 case TARGET_NR_getpagesize:
6709 ret = TARGET_PAGE_SIZE;
6710 break;
6711 #endif
6712 case TARGET_NR_gettid:
6713 ret = get_errno(gettid());
6714 break;
6715 #ifdef TARGET_NR_readahead
6716 case TARGET_NR_readahead:
6717 #if TARGET_ABI_BITS == 32
6718 #ifdef TARGET_ARM
6719 if (((CPUARMState *)cpu_env)->eabi)
6721 arg2 = arg3;
6722 arg3 = arg4;
6723 arg4 = arg5;
6725 #endif
6726 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
6727 #else
6728 ret = get_errno(readahead(arg1, arg2, arg3));
6729 #endif
6730 break;
6731 #endif
6732 #ifdef TARGET_NR_setxattr
6733 case TARGET_NR_setxattr:
6734 case TARGET_NR_lsetxattr:
6735 case TARGET_NR_fsetxattr:
6736 case TARGET_NR_getxattr:
6737 case TARGET_NR_lgetxattr:
6738 case TARGET_NR_fgetxattr:
6739 case TARGET_NR_listxattr:
6740 case TARGET_NR_llistxattr:
6741 case TARGET_NR_flistxattr:
6742 case TARGET_NR_removexattr:
6743 case TARGET_NR_lremovexattr:
6744 case TARGET_NR_fremovexattr:
6745 ret = -TARGET_EOPNOTSUPP;
6746 break;
6747 #endif
6748 #ifdef TARGET_NR_set_thread_area
6749 case TARGET_NR_set_thread_area:
6750 #if defined(TARGET_MIPS)
6751 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
6752 ret = 0;
6753 break;
6754 #elif defined(TARGET_CRIS)
6755 if (arg1 & 0xff)
6756 ret = -TARGET_EINVAL;
6757 else {
6758 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
6759 ret = 0;
6761 break;
6762 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
6763 ret = do_set_thread_area(cpu_env, arg1);
6764 break;
6765 #else
6766 goto unimplemented_nowarn;
6767 #endif
6768 #endif
6769 #ifdef TARGET_NR_get_thread_area
6770 case TARGET_NR_get_thread_area:
6771 #if defined(TARGET_I386) && defined(TARGET_ABI32)
6772 ret = do_get_thread_area(cpu_env, arg1);
6773 #else
6774 goto unimplemented_nowarn;
6775 #endif
6776 #endif
6777 #ifdef TARGET_NR_getdomainname
6778 case TARGET_NR_getdomainname:
6779 goto unimplemented_nowarn;
6780 #endif
6782 #ifdef TARGET_NR_clock_gettime
6783 case TARGET_NR_clock_gettime:
6785 struct timespec ts;
6786 ret = get_errno(clock_gettime(arg1, &ts));
6787 if (!is_error(ret)) {
6788 host_to_target_timespec(arg2, &ts);
6790 break;
6792 #endif
6793 #ifdef TARGET_NR_clock_getres
6794 case TARGET_NR_clock_getres:
6796 struct timespec ts;
6797 ret = get_errno(clock_getres(arg1, &ts));
6798 if (!is_error(ret)) {
6799 host_to_target_timespec(arg2, &ts);
6801 break;
6803 #endif
6804 #ifdef TARGET_NR_clock_nanosleep
6805 case TARGET_NR_clock_nanosleep:
6807 struct timespec ts;
6808 target_to_host_timespec(&ts, arg3);
6809 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
6810 if (arg4)
6811 host_to_target_timespec(arg4, &ts);
6812 break;
6814 #endif
6816 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
6817 case TARGET_NR_set_tid_address:
6818 ret = get_errno(set_tid_address((int *)g2h(arg1)));
6819 break;
6820 #endif
6822 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
6823 case TARGET_NR_tkill:
6824 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
6825 break;
6826 #endif
6828 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
6829 case TARGET_NR_tgkill:
6830 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
6831 target_to_host_signal(arg3)));
6832 break;
6833 #endif
6835 #ifdef TARGET_NR_set_robust_list
6836 case TARGET_NR_set_robust_list:
6837 goto unimplemented_nowarn;
6838 #endif
6840 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
6841 case TARGET_NR_utimensat:
6843 struct timespec *tsp, ts[2];
6844 if (!arg3) {
6845 tsp = NULL;
6846 } else {
6847 target_to_host_timespec(ts, arg3);
6848 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
6849 tsp = ts;
6851 if (!arg2)
6852 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
6853 else {
6854 if (!(p = lock_user_string(arg2))) {
6855 ret = -TARGET_EFAULT;
6856 goto fail;
6858 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
6859 unlock_user(p, arg2, 0);
6862 break;
6863 #endif
6864 #if defined(CONFIG_USE_NPTL)
6865 case TARGET_NR_futex:
6866 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
6867 break;
6868 #endif
6869 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
6870 case TARGET_NR_inotify_init:
6871 ret = get_errno(sys_inotify_init());
6872 break;
6873 #endif
6874 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
6875 case TARGET_NR_inotify_add_watch:
6876 p = lock_user_string(arg2);
6877 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
6878 unlock_user(p, arg2, 0);
6879 break;
6880 #endif
6881 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
6882 case TARGET_NR_inotify_rm_watch:
6883 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
6884 break;
6885 #endif
6887 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
6888 case TARGET_NR_mq_open:
6890 struct mq_attr posix_mq_attr;
6892 p = lock_user_string(arg1 - 1);
6893 if (arg4 != 0)
6894 copy_from_user_mq_attr (&posix_mq_attr, arg4);
6895 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
6896 unlock_user (p, arg1, 0);
6898 break;
6900 case TARGET_NR_mq_unlink:
6901 p = lock_user_string(arg1 - 1);
6902 ret = get_errno(mq_unlink(p));
6903 unlock_user (p, arg1, 0);
6904 break;
6906 case TARGET_NR_mq_timedsend:
6908 struct timespec ts;
6910 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6911 if (arg5 != 0) {
6912 target_to_host_timespec(&ts, arg5);
6913 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
6914 host_to_target_timespec(arg5, &ts);
6916 else
6917 ret = get_errno(mq_send(arg1, p, arg3, arg4));
6918 unlock_user (p, arg2, arg3);
6920 break;
6922 case TARGET_NR_mq_timedreceive:
6924 struct timespec ts;
6925 unsigned int prio;
6927 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6928 if (arg5 != 0) {
6929 target_to_host_timespec(&ts, arg5);
6930 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
6931 host_to_target_timespec(arg5, &ts);
6933 else
6934 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
6935 unlock_user (p, arg2, arg3);
6936 if (arg4 != 0)
6937 put_user_u32(prio, arg4);
6939 break;
6941 /* Not implemented for now... */
6942 /* case TARGET_NR_mq_notify: */
6943 /* break; */
6945 case TARGET_NR_mq_getsetattr:
6947 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
6948 ret = 0;
6949 if (arg3 != 0) {
6950 ret = mq_getattr(arg1, &posix_mq_attr_out);
6951 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
6953 if (arg2 != 0) {
6954 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
6955 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
6959 break;
6960 #endif
6962 #ifdef CONFIG_SPLICE
6963 #ifdef TARGET_NR_tee
6964 case TARGET_NR_tee:
6966 ret = get_errno(tee(arg1,arg2,arg3,arg4));
6968 break;
6969 #endif
6970 #ifdef TARGET_NR_splice
6971 case TARGET_NR_splice:
6973 loff_t loff_in, loff_out;
6974 loff_t *ploff_in = NULL, *ploff_out = NULL;
6975 if(arg2) {
6976 get_user_u64(loff_in, arg2);
6977 ploff_in = &loff_in;
6979 if(arg4) {
6980 get_user_u64(loff_out, arg2);
6981 ploff_out = &loff_out;
6983 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
6985 break;
6986 #endif
6987 #ifdef TARGET_NR_vmsplice
6988 case TARGET_NR_vmsplice:
6990 int count = arg3;
6991 struct iovec *vec;
6993 vec = alloca(count * sizeof(struct iovec));
6994 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6995 goto efault;
6996 ret = get_errno(vmsplice(arg1, vec, count, arg4));
6997 unlock_iovec(vec, arg2, count, 0);
6999 break;
7000 #endif
7001 #endif /* CONFIG_SPLICE */
7002 #ifdef CONFIG_EVENTFD
7003 #if defined(TARGET_NR_eventfd)
7004 case TARGET_NR_eventfd:
7005 ret = get_errno(eventfd(arg1, 0));
7006 break;
7007 #endif
7008 #if defined(TARGET_NR_eventfd2)
7009 case TARGET_NR_eventfd2:
7010 ret = get_errno(eventfd(arg1, arg2));
7011 break;
7012 #endif
7013 #endif /* CONFIG_EVENTFD */
7014 default:
7015 unimplemented:
7016 gemu_log("qemu: Unsupported syscall: %d\n", num);
7017 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
7018 unimplemented_nowarn:
7019 #endif
7020 ret = -TARGET_ENOSYS;
7021 break;
7023 fail:
7024 #ifdef DEBUG
7025 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
7026 #endif
7027 if(do_strace)
7028 print_syscall_ret(num, ret);
7029 return ret;
7030 efault:
7031 ret = -TARGET_EFAULT;
7032 goto fail;