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
31 #include <sys/types.h>
37 #include <sys/mount.h>
38 #include <sys/prctl.h>
39 #include <sys/resource.h>
44 #include <sys/socket.h>
48 #include <sys/times.h>
51 #include <sys/statfs.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>
63 #include <sys/eventfd.h>
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>
80 #include <linux/mtio.h>
82 #include "linux_loop.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)
91 /* XXX: Hardcode the above values. */
92 #define CLONE_NPTL_FLAGS2 0
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])
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, \
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, \
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
191 _syscall0(int, gettid)
193 /* This is a replacement for the host gettid() and must return a host
195 static int gettid(void) {
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);
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);
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)
213 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
214 _syscall2(int,sys_tkill,int,tid,int,sig)
216 #ifdef __NR_exit_group
217 _syscall1(int,exit_group,int,error_code)
219 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
220 _syscall1(int,set_tid_address,int *,tidptr)
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)
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, },
249 #define COPY_UTSNAME_FIELD(dest, src) \
251 /* __NEW_UTS_LEN doesn't include terminating null */ \
252 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
253 (dest)[__NEW_UTS_LEN] = '\0'; \
256 static int sys_uname(struct new_utsname *buf)
258 struct utsname uts_buf;
260 if (uname(&uts_buf) < 0)
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);
276 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
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 */
289 return strlen(buf)+1;
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));
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));
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));
317 #ifdef __NR_fstatat64
318 static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
321 return (fstatat(dirfd, pathname, buf, flags));
324 #ifdef __NR_newfstatat
325 static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
328 return (fstatat(dirfd, pathname, buf, flags));
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));
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));
345 #ifdef TARGET_NR_mkdirat
346 static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
348 return (mkdirat(dirfd, pathname, mode));
351 #ifdef TARGET_NR_mknodat
352 static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
355 return (mknodat(dirfd, pathname, mode, dev));
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
365 if ((flags & O_CREAT) != 0) {
370 * Get the 'mode' parameter and translate it to
374 mode = va_arg(ap, mode_t);
375 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
378 return (openat(dirfd, pathname, flags, mode));
380 return (openat(dirfd, pathname, flags));
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));
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));
396 #ifdef TARGET_NR_symlinkat
397 static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
399 return (symlinkat(oldpath, newdirfd, newpath));
402 #ifdef TARGET_NR_unlinkat
403 static int sys_unlinkat(int dirfd, const char *pathname, int flags)
405 return (unlinkat(dirfd, pathname, flags));
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)
416 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
417 _syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
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)
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)
428 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
429 _syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
430 const struct timeval *,times)
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)
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)
441 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
442 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
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)
448 #if defined(TARGET_NR_openat) && defined(__NR_openat)
449 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
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)
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)
459 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
460 _syscall3(int,sys_symlinkat,const char *,oldpath,
461 int,newdirfd,const char *,newpath)
463 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
464 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
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);
476 return utimensat(dirfd, pathname, times, flags);
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)
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());
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));
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));
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,
620 [ENOKEY] = TARGET_ENOKEY,
623 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
626 [EKEYREVOKED] = TARGET_EKEYREVOKED,
629 [EKEYREJECTED] = TARGET_EKEYREJECTED,
632 [EOWNERDEAD] = TARGET_EOWNERDEAD,
634 #ifdef ENOTRECOVERABLE
635 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
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];
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];
653 static inline abi_long get_errno(abi_long ret)
656 return -host_to_target_errno(errno);
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)
683 abi_long mapped_addr;
688 if (new_brk < target_original_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;
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;
711 static inline abi_long copy_from_user_fdset(fd_set *fds,
712 abi_ulong target_fds_addr,
716 abi_ulong b, *target_fds;
718 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
719 if (!(target_fds = lock_user(VERIFY_READ,
721 sizeof(abi_ulong) * nw,
723 return -TARGET_EFAULT;
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 */
738 unlock_user(target_fds, target_fds_addr, 0);
743 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
749 abi_ulong *target_fds;
751 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
752 if (!(target_fds = lock_user(VERIFY_WRITE,
754 sizeof(abi_ulong) * nw,
756 return -TARGET_EFAULT;
759 for (i = 0; i < nw; i++) {
761 for (j = 0; j < TARGET_ABI_BITS; j++) {
762 v |= ((FD_ISSET(k, fds) != 0) << j);
765 __put_user(v, &target_fds[i]);
768 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
773 #if defined(__alpha__)
779 static inline abi_long host_to_target_clock_t(long ticks)
781 #if HOST_HZ == TARGET_HZ
784 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
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);
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);
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);
850 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
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);
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);
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;
903 if (copy_from_user_fdset(&rfds, rfd_addr, n))
904 return -TARGET_EFAULT;
910 if (copy_from_user_fdset(&wfds, wfd_addr, n))
911 return -TARGET_EFAULT;
917 if (copy_from_user_fdset(&efds, efd_addr, n))
918 return -TARGET_EFAULT;
924 if (target_tv_addr) {
925 if (copy_from_user_timeval(&tv, target_tv_addr))
926 return -TARGET_EFAULT;
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;
949 static abi_long do_pipe2(int host_pipe[], int flags)
952 return pipe2(host_pipe, flags);
958 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes, int flags)
962 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
965 return get_errno(ret);
966 #if defined(TARGET_MIPS)
967 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
969 #elif defined(TARGET_SH4)
970 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
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;
977 return get_errno(ret);
980 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
981 abi_ulong target_addr,
984 struct target_ip_mreqn *target_smreqn;
986 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
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);
998 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
999 abi_ulong target_addr,
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);
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] )
1027 if (len > unix_maxlen)
1031 memcpy(addr, target_saddr, len);
1032 addr->sa_family = sa_family;
1033 unlock_user(target_saddr, target_addr, 0);
1038 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1039 struct sockaddr *addr,
1042 struct target_sockaddr *target_saddr;
1044 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
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);
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))
1067 target_cmsg_addr = tswapl(target_msgh->msg_control);
1068 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
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");
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);
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);
1107 msgh->msg_controllen = space;
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))
1124 target_cmsg_addr = tswapl(target_msgh->msg_control);
1125 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
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");
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);
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);
1163 target_msgh->msg_controllen = tswapl(space);
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)
1173 struct ip_mreqn *ip_mreq;
1174 struct ip_mreq_source *ip_mreq_source;
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)));
1191 case IP_ROUTER_ALERT:
1195 case IP_MTU_DISCOVER:
1201 case IP_MULTICAST_TTL:
1202 case IP_MULTICAST_LOOP:
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)));
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));
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);
1240 case TARGET_SOL_SOCKET:
1242 /* Options with 'int' argument. */
1243 case TARGET_SO_DEBUG:
1246 case TARGET_SO_REUSEADDR:
1247 optname = SO_REUSEADDR;
1249 case TARGET_SO_TYPE:
1252 case TARGET_SO_ERROR:
1255 case TARGET_SO_DONTROUTE:
1256 optname = SO_DONTROUTE;
1258 case TARGET_SO_BROADCAST:
1259 optname = SO_BROADCAST;
1261 case TARGET_SO_SNDBUF:
1262 optname = SO_SNDBUF;
1264 case TARGET_SO_RCVBUF:
1265 optname = SO_RCVBUF;
1267 case TARGET_SO_KEEPALIVE:
1268 optname = SO_KEEPALIVE;
1270 case TARGET_SO_OOBINLINE:
1271 optname = SO_OOBINLINE;
1273 case TARGET_SO_NO_CHECK:
1274 optname = SO_NO_CHECK;
1276 case TARGET_SO_PRIORITY:
1277 optname = SO_PRIORITY;
1280 case TARGET_SO_BSDCOMPAT:
1281 optname = SO_BSDCOMPAT;
1284 case TARGET_SO_PASSCRED:
1285 optname = SO_PASSCRED;
1287 case TARGET_SO_TIMESTAMP:
1288 optname = SO_TIMESTAMP;
1290 case TARGET_SO_RCVLOWAT:
1291 optname = SO_RCVLOWAT;
1293 case TARGET_SO_RCVTIMEO:
1294 optname = SO_RCVTIMEO;
1296 case TARGET_SO_SNDTIMEO:
1297 optname = SO_SNDTIMEO;
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)));
1312 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
1313 ret = -TARGET_ENOPROTOOPT;
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)
1327 case TARGET_SOL_SOCKET:
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 */
1342 /* TCP options all take an 'int' value. */
1344 if (get_user_u32(len, optlen))
1345 return -TARGET_EFAULT;
1347 return -TARGET_EINVAL;
1349 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1355 if (put_user_u32(val, optval_addr))
1356 return -TARGET_EFAULT;
1358 if (put_user_u8(val, optval_addr))
1359 return -TARGET_EFAULT;
1361 if (put_user_u32(len, optlen))
1362 return -TARGET_EFAULT;
1369 case IP_ROUTER_ALERT:
1373 case IP_MTU_DISCOVER:
1379 case IP_MULTICAST_TTL:
1380 case IP_MULTICAST_LOOP:
1381 if (get_user_u32(len, optlen))
1382 return -TARGET_EFAULT;
1384 return -TARGET_EINVAL;
1386 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1389 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1391 if (put_user_u32(len, optlen)
1392 || put_user_u8(val, optval_addr))
1393 return -TARGET_EFAULT;
1395 if (len > sizeof(int))
1397 if (put_user_u32(len, optlen)
1398 || put_user_u32(val, optval_addr))
1399 return -TARGET_EFAULT;
1403 ret = -TARGET_ENOPROTOOPT;
1409 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1411 ret = -TARGET_EOPNOTSUPP;
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;
1428 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
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. */
1439 /* zero length pointer is ignored */
1440 vec[i].iov_base = NULL;
1443 unlock_user (target_vec, target_addr, 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;
1454 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
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);
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)
1473 case TARGET_SOCK_DGRAM:
1476 case TARGET_SOCK_STREAM:
1479 case TARGET_SOCK_RAW:
1482 case TARGET_SOCK_RDM:
1485 case TARGET_SOCK_SEQPACKET:
1486 type = SOCK_SEQPACKET;
1488 case TARGET_SOCK_PACKET:
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,
1506 return -TARGET_EINVAL;
1508 addr = alloca(addrlen+1);
1510 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
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,
1525 return -TARGET_EINVAL;
1527 addr = alloca(addrlen);
1529 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
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)
1541 struct target_msghdr *msgp;
1545 abi_ulong target_vec;
1548 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
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),
1559 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
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;
1578 ret = target_to_host_cmsg(&msg, msgp);
1580 ret = get_errno(sendmsg(fd, &msg, flags));
1582 ret = get_errno(recvmsg(fd, &msg, flags));
1583 if (!is_error(ret)) {
1585 ret = host_to_target_cmsg(msgp, &msg);
1590 unlock_iovec(vec, target_vec, count, !send);
1591 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
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)
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;
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;
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)
1635 if (get_user_u32(addrlen, target_addrlen_addr))
1636 return -TARGET_EFAULT;
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;
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)
1663 if (get_user_u32(addrlen, target_addrlen_addr))
1664 return -TARGET_EFAULT;
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;
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)
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;
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)
1708 return -TARGET_EINVAL;
1710 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1712 return -TARGET_EFAULT;
1714 addr = alloca(addrlen);
1715 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1717 unlock_user(host_msg, msg, 0);
1720 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1722 ret = get_errno(send(fd, host_msg, len, flags));
1724 unlock_user(host_msg, msg, 0);
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)
1738 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1740 return -TARGET_EFAULT;
1742 if (get_user_u32(addrlen, target_addrlen)) {
1743 ret = -TARGET_EFAULT;
1747 ret = -TARGET_EINVAL;
1750 addr = alloca(addrlen);
1751 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1753 addr = NULL; /* To keep compiler quiet. */
1754 ret = get_errno(recv(fd, host_msg, len, flags));
1756 if (!is_error(ret)) {
1758 host_to_target_sockaddr(target_addr, addr, addrlen);
1759 if (put_user_u32(addrlen, target_addrlen)) {
1760 ret = -TARGET_EFAULT;
1764 unlock_user(host_msg, msg, len);
1767 unlock_user(host_msg, msg, 0);
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)
1777 const int n = sizeof(abi_ulong);
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);
1795 abi_ulong target_addr;
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);
1806 case SOCKOP_connect:
1809 abi_ulong target_addr;
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);
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));
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);
1844 case SOCKOP_getsockname:
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);
1857 case SOCKOP_getpeername:
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);
1870 case SOCKOP_socketpair:
1872 abi_ulong domain, type, protocol;
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);
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);
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);
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);
1936 case SOCKOP_recvfrom:
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);
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));
1967 case SOCKOP_sendmsg:
1968 case SOCKOP_recvmsg:
1971 abi_ulong target_msg;
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));
1983 case SOCKOP_setsockopt:
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);
2001 case SOCKOP_getsockopt:
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);
2020 gemu_log("Unsupported socketcall: %d\n", num);
2021 ret = -TARGET_ENOSYS;
2028 #define N_SHM_REGIONS 32
2030 static struct shm_region {
2033 } shm_regions[N_SHM_REGIONS];
2035 struct target_ipc_perm
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);
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);
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);
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);
2132 struct target_seminfo {
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);
2167 struct semid_ds *buf;
2168 unsigned short *array;
2169 struct seminfo *__buf;
2172 union target_semun {
2179 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2180 abi_ulong target_addr)
2183 unsigned short *array;
2185 struct semid_ds semid_ds;
2188 semun.buf = &semid_ds;
2190 ret = semctl(semid, 0, IPC_STAT, semun);
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);
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);
2210 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2211 unsigned short **host_array)
2214 unsigned short *array;
2216 struct semid_ds semid_ds;
2219 semun.buf = &semid_ds;
2221 ret = semctl(semid, 0, IPC_STAT, semun);
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);
2230 return -TARGET_EFAULT;
2232 for(i=0; i<nsems; i++) {
2233 __put_user((*host_array)[i], &array[i]);
2236 unlock_user(array, target_addr, 1);
2241 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2242 union target_semun target_su)
2245 struct semid_ds dsarg;
2246 unsigned short *array = NULL;
2247 struct seminfo seminfo;
2248 abi_long ret = -TARGET_EINVAL;
2255 arg.val = tswapl(target_su.val);
2256 ret = get_errno(semctl(semid, semnum, cmd, arg));
2257 target_su.val = tswapl(arg.val);
2261 err = target_to_host_semarray(semid, &array, target_su.array);
2265 ret = get_errno(semctl(semid, semnum, cmd, arg));
2266 err = host_to_target_semarray(semid, target_su.array, &array);
2273 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2277 ret = get_errno(semctl(semid, semnum, cmd, arg));
2278 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2284 arg.__buf = &seminfo;
2285 ret = get_errno(semctl(semid, semnum, cmd, arg));
2286 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2294 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2301 struct target_sembuf {
2302 unsigned short sem_num;
2307 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2308 abi_ulong target_addr,
2311 struct target_sembuf *target_sembuf;
2314 target_sembuf = lock_user(VERIFY_READ, target_addr,
2315 nsops*sizeof(struct target_sembuf), 1);
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);
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;
2347 abi_ulong msg_rtime;
2348 #if TARGET_ABI_BITS == 32
2349 abi_ulong __unused2;
2351 abi_ulong msg_ctime;
2352 #if TARGET_ABI_BITS == 32
2353 abi_ulong __unused3;
2355 abi_ulong __msg_cbytes;
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);
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);
2406 struct target_msginfo {
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);
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;
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;
2454 ret = get_errno(msgctl(msgid, cmd, NULL));
2458 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2459 if (host_to_target_msginfo(ptr, &msginfo))
2460 return -TARGET_EFAULT;
2467 struct target_msgbuf {
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;
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));
2486 unlock_user_struct(target_mb, msgp, 0);
2491 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2492 unsigned int msgsz, abi_long msgtyp,
2495 struct target_msgbuf *target_mb;
2497 struct msgbuf *host_mb;
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));
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;
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);
2522 unlock_user_struct(target_mb, msgp, 1);
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;
2534 abi_ulong shm_dtime;
2535 #if TARGET_ABI_BITS == 32
2536 abi_ulong __unused2;
2538 abi_ulong shm_ctime;
2539 #if TARGET_ABI_BITS == 32
2540 abi_ulong __unused3;
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);
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);
2589 struct target_shminfo {
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);
2612 struct target_shm_info {
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);
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;
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;
2657 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2658 if (host_to_target_shminfo(buf, &shminfo))
2659 return -TARGET_EFAULT;
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;
2669 ret = get_errno(shmctl(shmid, cmd, NULL));
2676 static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2680 struct shmid_ds shm_info;
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 */
2693 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2695 abi_ulong mmap_start;
2697 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2699 if (mmap_start == -1) {
2701 host_raddr = (void *)-1;
2703 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2706 if (host_raddr == (void *)-1) {
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;
2729 static inline abi_long do_shmdt(abi_ulong shmaddr)
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);
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)
2754 version = call >> 16;
2759 ret = do_semop(first, ptr, second);
2763 ret = get_errno(semget(first, second, third));
2767 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
2771 ret = get_errno(msgget(first, second));
2775 ret = do_msgsnd(first, ptr, second, third);
2779 ret = do_msgctl(first, second, ptr);
2786 struct target_ipc_kludge {
2791 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2792 ret = -TARGET_EFAULT;
2796 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2798 unlock_user_struct(tmp, ptr, 0);
2802 ret = do_msgrcv(first, ptr, second, fifth, third);
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;
2819 ret = -TARGET_EINVAL;
2824 ret = do_shmdt(ptr);
2828 /* IPC_* flag values are the same on all linux platforms */
2829 ret = get_errno(shmget(first, second, third));
2832 /* IPC_* and SHM_* command values are the same on all linux platforms */
2834 ret = do_shmctl(first, second, third);
2837 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2838 ret = -TARGET_ENOSYS;
2845 /* kernel structure types definitions */
2848 #define STRUCT(name, ...) STRUCT_ ## name,
2849 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2851 #include "syscall_types.h"
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"
2860 #undef STRUCT_SPECIAL
2862 typedef struct IOCTLEntry {
2863 unsigned int target_cmd;
2864 unsigned int host_cmd;
2867 const argtype arg_type[5];
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__ } },
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;
2890 uint8_t buf_temp[MAX_STRUCT_SIZE];
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)
2904 arg_type = ie->arg_type;
2906 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2908 switch(arg_type[0]) {
2911 ret = get_errno(ioctl(fd, ie->host_cmd));
2916 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2920 target_size = thunk_type_size(arg_type, 0);
2921 switch(ie->access) {
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);
2927 return -TARGET_EFAULT;
2928 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2929 unlock_user(argptr, arg, target_size);
2933 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
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));
2942 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
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);
2951 return -TARGET_EFAULT;
2952 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2953 unlock_user(argptr, arg, target_size);
2959 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2960 (long)cmd, arg_type[0]);
2961 ret = -TARGET_ENOSYS;
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 },
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 },
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 },
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 },
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;
3073 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
3075 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
3077 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
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;
3108 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
3110 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
3112 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
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 },
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)
3167 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3168 if (size > bytecount)
3170 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3172 return -TARGET_EFAULT;
3173 /* ??? Should this by byteswapped? */
3174 memcpy(p, ldt_table, size);
3175 unlock_user(p, ptr, 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;
3210 lm = (ldt_info.flags >> 7) & 1;
3212 if (contents == 3) {
3214 return -TARGET_EINVAL;
3215 if (seg_not_present == 0)
3216 return -TARGET_EINVAL;
3218 /* allocate the LDT */
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) {
3237 read_exec_only == 1 &&
3239 limit_in_pages == 0 &&
3240 seg_not_present == 1 &&
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) |
3255 ((seg_not_present ^ 1) << 15) |
3257 (limit_in_pages << 23) |
3261 entry_2 |= (useable << 20);
3263 /* Install the new entry ... */
3265 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3266 lp[0] = tswap32(entry_1);
3267 lp[1] = tswap32(entry_2);
3271 /* specific and weird i386 syscalls */
3272 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3273 unsigned long bytecount)
3279 ret = read_ldt(ptr, bytecount);
3282 ret = write_ldt(env, ptr, bytecount, 1);
3285 ret = write_ldt(env, ptr, bytecount, 0);
3288 ret = -TARGET_ENOSYS;
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;
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);
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;
3335 lm = (ldt_info.flags >> 7) & 1;
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 &&
3349 limit_in_pages == 0 &&
3350 seg_not_present == 1 &&
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) |
3365 ((seg_not_present ^ 1) << 15) |
3367 (limit_in_pages << 23) |
3372 /* Install the new entry ... */
3374 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3375 lp[0] = tswap32(entry_1);
3376 lp[1] = tswap32(entry_2);
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;
3411 lm = (entry_2 >> 21) & 1;
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);
3426 #endif /* TARGET_I386 && TARGET_ABI32 */
3428 #ifndef TARGET_ABI32
3429 static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
3436 case TARGET_ARCH_SET_GS:
3437 case TARGET_ARCH_SET_FS:
3438 if (code == TARGET_ARCH_SET_GS)
3442 cpu_x86_load_seg(env, idx, 0);
3443 env->segs[idx].base = addr;
3445 case TARGET_ARCH_GET_GS:
3446 case TARGET_ARCH_GET_FS:
3447 if (code == TARGET_ARCH_GET_GS)
3451 val = env->segs[idx].base;
3452 if (put_user(val, addr, abi_ulong))
3453 return -TARGET_EFAULT;
3456 ret = -TARGET_EINVAL;
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;
3472 pthread_mutex_t mutex;
3473 pthread_cond_t cond;
3476 abi_ulong child_tidptr;
3477 abi_ulong parent_tidptr;
3481 static void *clone_func(void *arg)
3483 new_thread_info *info = arg;
3489 ts = (TaskState *)thread_env->opaque;
3490 info->tid = gettid();
3491 env->host_tid = info->tid;
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);
3511 /* this stack is the equivalent of the kernel stack associated with a
3513 #define NEW_STACK_SIZE 8192
3515 static int clone_func(void *arg)
3517 CPUState *env = arg;
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)
3534 #if defined(CONFIG_USE_NPTL)
3535 unsigned int nptl_flags;
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;
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)
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);
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);
3596 /* Wait for the child to initialize. */
3597 pthread_cond_wait(&info.cond, &info.mutex);
3599 if (flags & CLONE_PARENT_SETTID)
3600 put_user_u32(ret, parent_tidptr);
3604 pthread_mutex_unlock(&info.mutex);
3605 pthread_cond_destroy(&info.cond);
3606 pthread_mutex_destroy(&info.mutex);
3607 pthread_mutex_unlock(&clone_lock);
3609 if (flags & CLONE_NPTL_FLAGS2)
3611 /* This is probably going to die very quickly, but do it anyway. */
3613 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3615 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3619 /* if no CLONE_VM, we consider it is a fork */
3620 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
3625 /* Child Process. */
3626 cpu_clone_regs(env, newsp);
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;
3652 /* warning : doesn't handle linux specific flags... */
3653 static int target_to_host_fcntl_cmd(int 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:
3662 case TARGET_F_GETLK:
3664 case TARGET_F_SETLK:
3666 case TARGET_F_SETLKW:
3668 case TARGET_F_GETOWN:
3670 case TARGET_F_SETOWN:
3672 case TARGET_F_GETSIG:
3674 case TARGET_F_SETSIG:
3676 #if TARGET_ABI_BITS == 32
3677 case TARGET_F_GETLK64:
3679 case TARGET_F_SETLK64:
3681 case TARGET_F_SETLKW64:
3684 case TARGET_F_SETLEASE:
3686 case TARGET_F_GETLEASE:
3688 #ifdef F_DUPFD_CLOEXEC
3689 case TARGET_F_DUPFD_CLOEXEC:
3690 return F_DUPFD_CLOEXEC;
3692 case TARGET_F_NOTIFY:
3695 return -TARGET_EINVAL;
3697 return -TARGET_EINVAL;
3700 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
3703 struct target_flock *target_fl;
3704 struct flock64 fl64;
3705 struct target_flock64 *target_fl64;
3707 int host_cmd = target_to_host_fcntl_cmd(cmd);
3709 if (host_cmd == -TARGET_EINVAL)
3713 case TARGET_F_GETLK:
3714 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3715 return -TARGET_EFAULT;
3716 fl.l_type = tswap16(target_fl->l_type);
3717 fl.l_whence = tswap16(target_fl->l_whence);
3718 fl.l_start = tswapl(target_fl->l_start);
3719 fl.l_len = tswapl(target_fl->l_len);
3720 fl.l_pid = tswap32(target_fl->l_pid);
3721 unlock_user_struct(target_fl, arg, 0);
3722 ret = get_errno(fcntl(fd, host_cmd, &fl));
3724 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3725 return -TARGET_EFAULT;
3726 target_fl->l_type = tswap16(fl.l_type);
3727 target_fl->l_whence = tswap16(fl.l_whence);
3728 target_fl->l_start = tswapl(fl.l_start);
3729 target_fl->l_len = tswapl(fl.l_len);
3730 target_fl->l_pid = tswap32(fl.l_pid);
3731 unlock_user_struct(target_fl, arg, 1);
3735 case TARGET_F_SETLK:
3736 case TARGET_F_SETLKW:
3737 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3738 return -TARGET_EFAULT;
3739 fl.l_type = tswap16(target_fl->l_type);
3740 fl.l_whence = tswap16(target_fl->l_whence);
3741 fl.l_start = tswapl(target_fl->l_start);
3742 fl.l_len = tswapl(target_fl->l_len);
3743 fl.l_pid = tswap32(target_fl->l_pid);
3744 unlock_user_struct(target_fl, arg, 0);
3745 ret = get_errno(fcntl(fd, host_cmd, &fl));
3748 case TARGET_F_GETLK64:
3749 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3750 return -TARGET_EFAULT;
3751 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3752 fl64.l_whence = tswap16(target_fl64->l_whence);
3753 fl64.l_start = tswapl(target_fl64->l_start);
3754 fl64.l_len = tswapl(target_fl64->l_len);
3755 fl64.l_pid = tswap32(target_fl64->l_pid);
3756 unlock_user_struct(target_fl64, arg, 0);
3757 ret = get_errno(fcntl(fd, host_cmd, &fl64));
3759 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3760 return -TARGET_EFAULT;
3761 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3762 target_fl64->l_whence = tswap16(fl64.l_whence);
3763 target_fl64->l_start = tswapl(fl64.l_start);
3764 target_fl64->l_len = tswapl(fl64.l_len);
3765 target_fl64->l_pid = tswap32(fl64.l_pid);
3766 unlock_user_struct(target_fl64, arg, 1);
3769 case TARGET_F_SETLK64:
3770 case TARGET_F_SETLKW64:
3771 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3772 return -TARGET_EFAULT;
3773 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3774 fl64.l_whence = tswap16(target_fl64->l_whence);
3775 fl64.l_start = tswapl(target_fl64->l_start);
3776 fl64.l_len = tswapl(target_fl64->l_len);
3777 fl64.l_pid = tswap32(target_fl64->l_pid);
3778 unlock_user_struct(target_fl64, arg, 0);
3779 ret = get_errno(fcntl(fd, host_cmd, &fl64));
3782 case TARGET_F_GETFL:
3783 ret = get_errno(fcntl(fd, host_cmd, arg));
3785 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3789 case TARGET_F_SETFL:
3790 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3793 case TARGET_F_SETOWN:
3794 case TARGET_F_GETOWN:
3795 case TARGET_F_SETSIG:
3796 case TARGET_F_GETSIG:
3797 case TARGET_F_SETLEASE:
3798 case TARGET_F_GETLEASE:
3799 ret = get_errno(fcntl(fd, host_cmd, arg));
3803 ret = get_errno(fcntl(fd, cmd, arg));
3811 static inline int high2lowuid(int uid)
3819 static inline int high2lowgid(int gid)
3827 static inline int low2highuid(int uid)
3829 if ((int16_t)uid == -1)
3835 static inline int low2highgid(int gid)
3837 if ((int16_t)gid == -1)
3843 #endif /* USE_UID16 */
3845 void syscall_init(void)
3848 const argtype *arg_type;
3852 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
3853 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
3854 #include "syscall_types.h"
3856 #undef STRUCT_SPECIAL
3858 /* we patch the ioctl size if necessary. We rely on the fact that
3859 no ioctl has all the bits at '1' in the size field */
3861 while (ie->target_cmd != 0) {
3862 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3863 TARGET_IOC_SIZEMASK) {
3864 arg_type = ie->arg_type;
3865 if (arg_type[0] != TYPE_PTR) {
3866 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
3871 size = thunk_type_size(arg_type, 0);
3872 ie->target_cmd = (ie->target_cmd &
3873 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3874 (size << TARGET_IOC_SIZESHIFT);
3877 /* Build target_to_host_errno_table[] table from
3878 * host_to_target_errno_table[]. */
3879 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3880 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3882 /* automatic consistency check if same arch */
3883 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3884 (defined(__x86_64__) && defined(TARGET_X86_64))
3885 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3886 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3887 ie->name, ie->target_cmd, ie->host_cmd);
3894 #if TARGET_ABI_BITS == 32
3895 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3897 #ifdef TARGET_WORDS_BIGENDIAN
3898 return ((uint64_t)word0 << 32) | word1;
3900 return ((uint64_t)word1 << 32) | word0;
3903 #else /* TARGET_ABI_BITS == 32 */
3904 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3908 #endif /* TARGET_ABI_BITS != 32 */
3910 #ifdef TARGET_NR_truncate64
3911 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3917 if (((CPUARMState *)cpu_env)->eabi)
3923 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3927 #ifdef TARGET_NR_ftruncate64
3928 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3934 if (((CPUARMState *)cpu_env)->eabi)
3940 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3944 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3945 abi_ulong target_addr)
3947 struct target_timespec *target_ts;
3949 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3950 return -TARGET_EFAULT;
3951 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3952 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3953 unlock_user_struct(target_ts, target_addr, 0);
3957 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3958 struct timespec *host_ts)
3960 struct target_timespec *target_ts;
3962 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3963 return -TARGET_EFAULT;
3964 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3965 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3966 unlock_user_struct(target_ts, target_addr, 1);
3970 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
3971 static inline abi_long host_to_target_stat64(void *cpu_env,
3972 abi_ulong target_addr,
3973 struct stat *host_st)
3976 if (((CPUARMState *)cpu_env)->eabi) {
3977 struct target_eabi_stat64 *target_st;
3979 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3980 return -TARGET_EFAULT;
3981 memset(target_st, 0, sizeof(struct target_eabi_stat64));
3982 __put_user(host_st->st_dev, &target_st->st_dev);
3983 __put_user(host_st->st_ino, &target_st->st_ino);
3984 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3985 __put_user(host_st->st_ino, &target_st->__st_ino);
3987 __put_user(host_st->st_mode, &target_st->st_mode);
3988 __put_user(host_st->st_nlink, &target_st->st_nlink);
3989 __put_user(host_st->st_uid, &target_st->st_uid);
3990 __put_user(host_st->st_gid, &target_st->st_gid);
3991 __put_user(host_st->st_rdev, &target_st->st_rdev);
3992 __put_user(host_st->st_size, &target_st->st_size);
3993 __put_user(host_st->st_blksize, &target_st->st_blksize);
3994 __put_user(host_st->st_blocks, &target_st->st_blocks);
3995 __put_user(host_st->st_atime, &target_st->target_st_atime);
3996 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3997 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3998 unlock_user_struct(target_st, target_addr, 1);
4002 #if TARGET_LONG_BITS == 64
4003 struct target_stat *target_st;
4005 struct target_stat64 *target_st;
4008 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4009 return -TARGET_EFAULT;
4010 memset(target_st, 0, sizeof(*target_st));
4011 __put_user(host_st->st_dev, &target_st->st_dev);
4012 __put_user(host_st->st_ino, &target_st->st_ino);
4013 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4014 __put_user(host_st->st_ino, &target_st->__st_ino);
4016 __put_user(host_st->st_mode, &target_st->st_mode);
4017 __put_user(host_st->st_nlink, &target_st->st_nlink);
4018 __put_user(host_st->st_uid, &target_st->st_uid);
4019 __put_user(host_st->st_gid, &target_st->st_gid);
4020 __put_user(host_st->st_rdev, &target_st->st_rdev);
4021 /* XXX: better use of kernel struct */
4022 __put_user(host_st->st_size, &target_st->st_size);
4023 __put_user(host_st->st_blksize, &target_st->st_blksize);
4024 __put_user(host_st->st_blocks, &target_st->st_blocks);
4025 __put_user(host_st->st_atime, &target_st->target_st_atime);
4026 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4027 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4028 unlock_user_struct(target_st, target_addr, 1);
4035 #if defined(CONFIG_USE_NPTL)
4036 /* ??? Using host futex calls even when target atomic operations
4037 are not really atomic probably breaks things. However implementing
4038 futexes locally would make futexes shared between multiple processes
4039 tricky. However they're probably useless because guest atomic
4040 operations won't work either. */
4041 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4042 target_ulong uaddr2, int val3)
4044 struct timespec ts, *pts;
4047 /* ??? We assume FUTEX_* constants are the same on both host
4049 #ifdef FUTEX_CMD_MASK
4050 base_op = op & FUTEX_CMD_MASK;
4058 target_to_host_timespec(pts, timeout);
4062 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
4065 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4067 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4069 case FUTEX_CMP_REQUEUE:
4071 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4072 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4073 But the prototype takes a `struct timespec *'; insert casts
4074 to satisfy the compiler. We do not need to tswap TIMEOUT
4075 since it's not compared to guest memory. */
4076 pts = (struct timespec *)(uintptr_t) timeout;
4077 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4079 (base_op == FUTEX_CMP_REQUEUE
4083 return -TARGET_ENOSYS;
4088 /* Map host to target signal numbers for the wait family of syscalls.
4089 Assume all other status bits are the same. */
4090 static int host_to_target_waitstatus(int status)
4092 if (WIFSIGNALED(status)) {
4093 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4095 if (WIFSTOPPED(status)) {
4096 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4102 int get_osversion(void)
4104 static int osversion;
4105 struct new_utsname buf;
4110 if (qemu_uname_release && *qemu_uname_release) {
4111 s = qemu_uname_release;
4113 if (sys_uname(&buf))
4118 for (i = 0; i < 3; i++) {
4120 while (*s >= '0' && *s <= '9') {
4125 tmp = (tmp << 8) + n;
4133 /* do_syscall() should always have a single exit point at the end so
4134 that actions, such as logging of syscall results, can be performed.
4135 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
4136 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4137 abi_long arg2, abi_long arg3, abi_long arg4,
4138 abi_long arg5, abi_long arg6)
4146 gemu_log("syscall %d", num);
4149 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4152 case TARGET_NR_exit:
4153 #ifdef CONFIG_USE_NPTL
4154 /* In old applications this may be used to implement _exit(2).
4155 However in threaded applictions it is used for thread termination,
4156 and _exit_group is used for application termination.
4157 Do thread termination if we have more then one thread. */
4158 /* FIXME: This probably breaks if a signal arrives. We should probably
4159 be disabling signals. */
4160 if (first_cpu->next_cpu) {
4168 while (p && p != (CPUState *)cpu_env) {
4169 lastp = &p->next_cpu;
4172 /* If we didn't find the CPU for this thread then something is
4176 /* Remove the CPU from the list. */
4177 *lastp = p->next_cpu;
4179 ts = ((CPUState *)cpu_env)->opaque;
4180 if (ts->child_tidptr) {
4181 put_user_u32(0, ts->child_tidptr);
4182 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4185 /* TODO: Free CPU state. */
4192 gdb_exit(cpu_env, arg1);
4194 ret = 0; /* avoid warning */
4196 case TARGET_NR_read:
4200 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4202 ret = get_errno(read(arg1, p, arg3));
4203 unlock_user(p, arg2, ret);
4206 case TARGET_NR_write:
4207 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4209 ret = get_errno(write(arg1, p, arg3));
4210 unlock_user(p, arg2, 0);
4212 case TARGET_NR_open:
4213 if (!(p = lock_user_string(arg1)))
4215 ret = get_errno(open(path(p),
4216 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4218 unlock_user(p, arg1, 0);
4220 #if defined(TARGET_NR_openat) && defined(__NR_openat)
4221 case TARGET_NR_openat:
4222 if (!(p = lock_user_string(arg2)))
4224 ret = get_errno(sys_openat(arg1,
4226 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4228 unlock_user(p, arg2, 0);
4231 case TARGET_NR_close:
4232 ret = get_errno(close(arg1));
4237 case TARGET_NR_fork:
4238 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
4240 #ifdef TARGET_NR_waitpid
4241 case TARGET_NR_waitpid:
4244 ret = get_errno(waitpid(arg1, &status, arg3));
4245 if (!is_error(ret) && arg2
4246 && put_user_s32(host_to_target_waitstatus(status), arg2))
4251 #ifdef TARGET_NR_waitid
4252 case TARGET_NR_waitid:
4256 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4257 if (!is_error(ret) && arg3 && info.si_pid != 0) {
4258 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
4260 host_to_target_siginfo(p, &info);
4261 unlock_user(p, arg3, sizeof(target_siginfo_t));
4266 #ifdef TARGET_NR_creat /* not on alpha */
4267 case TARGET_NR_creat:
4268 if (!(p = lock_user_string(arg1)))
4270 ret = get_errno(creat(p, arg2));
4271 unlock_user(p, arg1, 0);
4274 case TARGET_NR_link:
4277 p = lock_user_string(arg1);
4278 p2 = lock_user_string(arg2);
4280 ret = -TARGET_EFAULT;
4282 ret = get_errno(link(p, p2));
4283 unlock_user(p2, arg2, 0);
4284 unlock_user(p, arg1, 0);
4287 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4288 case TARGET_NR_linkat:
4293 p = lock_user_string(arg2);
4294 p2 = lock_user_string(arg4);
4296 ret = -TARGET_EFAULT;
4298 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
4299 unlock_user(p, arg2, 0);
4300 unlock_user(p2, arg4, 0);
4304 case TARGET_NR_unlink:
4305 if (!(p = lock_user_string(arg1)))
4307 ret = get_errno(unlink(p));
4308 unlock_user(p, arg1, 0);
4310 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4311 case TARGET_NR_unlinkat:
4312 if (!(p = lock_user_string(arg2)))
4314 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4315 unlock_user(p, arg2, 0);
4318 case TARGET_NR_execve:
4320 char **argp, **envp;
4323 abi_ulong guest_argp;
4324 abi_ulong guest_envp;
4330 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
4331 if (get_user_ual(addr, gp))
4339 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
4340 if (get_user_ual(addr, gp))
4347 argp = alloca((argc + 1) * sizeof(void *));
4348 envp = alloca((envc + 1) * sizeof(void *));
4350 for (gp = guest_argp, q = argp; gp;
4351 gp += sizeof(abi_ulong), q++) {
4352 if (get_user_ual(addr, gp))
4356 if (!(*q = lock_user_string(addr)))
4361 for (gp = guest_envp, q = envp; gp;
4362 gp += sizeof(abi_ulong), q++) {
4363 if (get_user_ual(addr, gp))
4367 if (!(*q = lock_user_string(addr)))
4372 if (!(p = lock_user_string(arg1)))
4374 ret = get_errno(execve(p, argp, envp));
4375 unlock_user(p, arg1, 0);
4380 ret = -TARGET_EFAULT;
4383 for (gp = guest_argp, q = argp; *q;
4384 gp += sizeof(abi_ulong), q++) {
4385 if (get_user_ual(addr, gp)
4388 unlock_user(*q, addr, 0);
4390 for (gp = guest_envp, q = envp; *q;
4391 gp += sizeof(abi_ulong), q++) {
4392 if (get_user_ual(addr, gp)
4395 unlock_user(*q, addr, 0);
4399 case TARGET_NR_chdir:
4400 if (!(p = lock_user_string(arg1)))
4402 ret = get_errno(chdir(p));
4403 unlock_user(p, arg1, 0);
4405 #ifdef TARGET_NR_time
4406 case TARGET_NR_time:
4409 ret = get_errno(time(&host_time));
4412 && put_user_sal(host_time, arg1))
4417 case TARGET_NR_mknod:
4418 if (!(p = lock_user_string(arg1)))
4420 ret = get_errno(mknod(p, arg2, arg3));
4421 unlock_user(p, arg1, 0);
4423 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4424 case TARGET_NR_mknodat:
4425 if (!(p = lock_user_string(arg2)))
4427 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4428 unlock_user(p, arg2, 0);
4431 case TARGET_NR_chmod:
4432 if (!(p = lock_user_string(arg1)))
4434 ret = get_errno(chmod(p, arg2));
4435 unlock_user(p, arg1, 0);
4437 #ifdef TARGET_NR_break
4438 case TARGET_NR_break:
4441 #ifdef TARGET_NR_oldstat
4442 case TARGET_NR_oldstat:
4445 case TARGET_NR_lseek:
4446 ret = get_errno(lseek(arg1, arg2, arg3));
4448 #ifdef TARGET_NR_getxpid
4449 case TARGET_NR_getxpid:
4451 case TARGET_NR_getpid:
4453 ret = get_errno(getpid());
4455 case TARGET_NR_mount:
4457 /* need to look at the data field */
4459 p = lock_user_string(arg1);
4460 p2 = lock_user_string(arg2);
4461 p3 = lock_user_string(arg3);
4462 if (!p || !p2 || !p3)
4463 ret = -TARGET_EFAULT;
4465 /* FIXME - arg5 should be locked, but it isn't clear how to
4466 * do that since it's not guaranteed to be a NULL-terminated
4469 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4470 unlock_user(p, arg1, 0);
4471 unlock_user(p2, arg2, 0);
4472 unlock_user(p3, arg3, 0);
4475 #ifdef TARGET_NR_umount
4476 case TARGET_NR_umount:
4477 if (!(p = lock_user_string(arg1)))
4479 ret = get_errno(umount(p));
4480 unlock_user(p, arg1, 0);
4483 #ifdef TARGET_NR_stime /* not on alpha */
4484 case TARGET_NR_stime:
4487 if (get_user_sal(host_time, arg1))
4489 ret = get_errno(stime(&host_time));
4493 case TARGET_NR_ptrace:
4495 #ifdef TARGET_NR_alarm /* not on alpha */
4496 case TARGET_NR_alarm:
4500 #ifdef TARGET_NR_oldfstat
4501 case TARGET_NR_oldfstat:
4504 #ifdef TARGET_NR_pause /* not on alpha */
4505 case TARGET_NR_pause:
4506 ret = get_errno(pause());
4509 #ifdef TARGET_NR_utime
4510 case TARGET_NR_utime:
4512 struct utimbuf tbuf, *host_tbuf;
4513 struct target_utimbuf *target_tbuf;
4515 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4517 tbuf.actime = tswapl(target_tbuf->actime);
4518 tbuf.modtime = tswapl(target_tbuf->modtime);
4519 unlock_user_struct(target_tbuf, arg2, 0);
4524 if (!(p = lock_user_string(arg1)))
4526 ret = get_errno(utime(p, host_tbuf));
4527 unlock_user(p, arg1, 0);
4531 case TARGET_NR_utimes:
4533 struct timeval *tvp, tv[2];
4535 if (copy_from_user_timeval(&tv[0], arg2)
4536 || copy_from_user_timeval(&tv[1],
4537 arg2 + sizeof(struct target_timeval)))
4543 if (!(p = lock_user_string(arg1)))
4545 ret = get_errno(utimes(p, tvp));
4546 unlock_user(p, arg1, 0);
4549 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4550 case TARGET_NR_futimesat:
4552 struct timeval *tvp, tv[2];
4554 if (copy_from_user_timeval(&tv[0], arg3)
4555 || copy_from_user_timeval(&tv[1],
4556 arg3 + sizeof(struct target_timeval)))
4562 if (!(p = lock_user_string(arg2)))
4564 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4565 unlock_user(p, arg2, 0);
4569 #ifdef TARGET_NR_stty
4570 case TARGET_NR_stty:
4573 #ifdef TARGET_NR_gtty
4574 case TARGET_NR_gtty:
4577 case TARGET_NR_access:
4578 if (!(p = lock_user_string(arg1)))
4580 ret = get_errno(access(path(p), arg2));
4581 unlock_user(p, arg1, 0);
4583 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4584 case TARGET_NR_faccessat:
4585 if (!(p = lock_user_string(arg2)))
4587 ret = get_errno(sys_faccessat(arg1, p, arg3));
4588 unlock_user(p, arg2, 0);
4591 #ifdef TARGET_NR_nice /* not on alpha */
4592 case TARGET_NR_nice:
4593 ret = get_errno(nice(arg1));
4596 #ifdef TARGET_NR_ftime
4597 case TARGET_NR_ftime:
4600 case TARGET_NR_sync:
4604 case TARGET_NR_kill:
4605 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
4607 case TARGET_NR_rename:
4610 p = lock_user_string(arg1);
4611 p2 = lock_user_string(arg2);
4613 ret = -TARGET_EFAULT;
4615 ret = get_errno(rename(p, p2));
4616 unlock_user(p2, arg2, 0);
4617 unlock_user(p, arg1, 0);
4620 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4621 case TARGET_NR_renameat:
4624 p = lock_user_string(arg2);
4625 p2 = lock_user_string(arg4);
4627 ret = -TARGET_EFAULT;
4629 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
4630 unlock_user(p2, arg4, 0);
4631 unlock_user(p, arg2, 0);
4635 case TARGET_NR_mkdir:
4636 if (!(p = lock_user_string(arg1)))
4638 ret = get_errno(mkdir(p, arg2));
4639 unlock_user(p, arg1, 0);
4641 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4642 case TARGET_NR_mkdirat:
4643 if (!(p = lock_user_string(arg2)))
4645 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4646 unlock_user(p, arg2, 0);
4649 case TARGET_NR_rmdir:
4650 if (!(p = lock_user_string(arg1)))
4652 ret = get_errno(rmdir(p));
4653 unlock_user(p, arg1, 0);
4656 ret = get_errno(dup(arg1));
4658 case TARGET_NR_pipe:
4659 ret = do_pipe(cpu_env, arg1, 0);
4661 #ifdef TARGET_NR_pipe2
4662 case TARGET_NR_pipe2:
4663 ret = do_pipe(cpu_env, arg1, arg2);
4666 case TARGET_NR_times:
4668 struct target_tms *tmsp;
4670 ret = get_errno(times(&tms));
4672 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4675 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4676 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4677 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4678 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
4681 ret = host_to_target_clock_t(ret);
4684 #ifdef TARGET_NR_prof
4685 case TARGET_NR_prof:
4688 #ifdef TARGET_NR_signal
4689 case TARGET_NR_signal:
4692 case TARGET_NR_acct:
4694 ret = get_errno(acct(NULL));
4696 if (!(p = lock_user_string(arg1)))
4698 ret = get_errno(acct(path(p)));
4699 unlock_user(p, arg1, 0);
4702 #ifdef TARGET_NR_umount2 /* not on alpha */
4703 case TARGET_NR_umount2:
4704 if (!(p = lock_user_string(arg1)))
4706 ret = get_errno(umount2(p, arg2));
4707 unlock_user(p, arg1, 0);
4710 #ifdef TARGET_NR_lock
4711 case TARGET_NR_lock:
4714 case TARGET_NR_ioctl:
4715 ret = do_ioctl(arg1, arg2, arg3);
4717 case TARGET_NR_fcntl:
4718 ret = do_fcntl(arg1, arg2, arg3);
4720 #ifdef TARGET_NR_mpx
4724 case TARGET_NR_setpgid:
4725 ret = get_errno(setpgid(arg1, arg2));
4727 #ifdef TARGET_NR_ulimit
4728 case TARGET_NR_ulimit:
4731 #ifdef TARGET_NR_oldolduname
4732 case TARGET_NR_oldolduname:
4735 case TARGET_NR_umask:
4736 ret = get_errno(umask(arg1));
4738 case TARGET_NR_chroot:
4739 if (!(p = lock_user_string(arg1)))
4741 ret = get_errno(chroot(p));
4742 unlock_user(p, arg1, 0);
4744 case TARGET_NR_ustat:
4746 case TARGET_NR_dup2:
4747 ret = get_errno(dup2(arg1, arg2));
4749 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
4750 case TARGET_NR_dup3:
4751 ret = get_errno(dup3(arg1, arg2, arg3));
4754 #ifdef TARGET_NR_getppid /* not on alpha */
4755 case TARGET_NR_getppid:
4756 ret = get_errno(getppid());
4759 case TARGET_NR_getpgrp:
4760 ret = get_errno(getpgrp());
4762 case TARGET_NR_setsid:
4763 ret = get_errno(setsid());
4765 #ifdef TARGET_NR_sigaction
4766 case TARGET_NR_sigaction:
4768 #if !defined(TARGET_MIPS)
4769 struct target_old_sigaction *old_act;
4770 struct target_sigaction act, oact, *pact;
4772 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4774 act._sa_handler = old_act->_sa_handler;
4775 target_siginitset(&act.sa_mask, old_act->sa_mask);
4776 act.sa_flags = old_act->sa_flags;
4777 act.sa_restorer = old_act->sa_restorer;
4778 unlock_user_struct(old_act, arg2, 0);
4783 ret = get_errno(do_sigaction(arg1, pact, &oact));
4784 if (!is_error(ret) && arg3) {
4785 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4787 old_act->_sa_handler = oact._sa_handler;
4788 old_act->sa_mask = oact.sa_mask.sig[0];
4789 old_act->sa_flags = oact.sa_flags;
4790 old_act->sa_restorer = oact.sa_restorer;
4791 unlock_user_struct(old_act, arg3, 1);
4794 struct target_sigaction act, oact, *pact, *old_act;
4797 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4799 act._sa_handler = old_act->_sa_handler;
4800 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4801 act.sa_flags = old_act->sa_flags;
4802 unlock_user_struct(old_act, arg2, 0);
4808 ret = get_errno(do_sigaction(arg1, pact, &oact));
4810 if (!is_error(ret) && arg3) {
4811 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4813 old_act->_sa_handler = oact._sa_handler;
4814 old_act->sa_flags = oact.sa_flags;
4815 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4816 old_act->sa_mask.sig[1] = 0;
4817 old_act->sa_mask.sig[2] = 0;
4818 old_act->sa_mask.sig[3] = 0;
4819 unlock_user_struct(old_act, arg3, 1);
4825 case TARGET_NR_rt_sigaction:
4827 struct target_sigaction *act;
4828 struct target_sigaction *oact;
4831 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4836 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4837 ret = -TARGET_EFAULT;
4838 goto rt_sigaction_fail;
4842 ret = get_errno(do_sigaction(arg1, act, oact));
4845 unlock_user_struct(act, arg2, 0);
4847 unlock_user_struct(oact, arg3, 1);
4850 #ifdef TARGET_NR_sgetmask /* not on alpha */
4851 case TARGET_NR_sgetmask:
4854 abi_ulong target_set;
4855 sigprocmask(0, NULL, &cur_set);
4856 host_to_target_old_sigset(&target_set, &cur_set);
4861 #ifdef TARGET_NR_ssetmask /* not on alpha */
4862 case TARGET_NR_ssetmask:
4864 sigset_t set, oset, cur_set;
4865 abi_ulong target_set = arg1;
4866 sigprocmask(0, NULL, &cur_set);
4867 target_to_host_old_sigset(&set, &target_set);
4868 sigorset(&set, &set, &cur_set);
4869 sigprocmask(SIG_SETMASK, &set, &oset);
4870 host_to_target_old_sigset(&target_set, &oset);
4875 #ifdef TARGET_NR_sigprocmask
4876 case TARGET_NR_sigprocmask:
4879 sigset_t set, oldset, *set_ptr;
4883 case TARGET_SIG_BLOCK:
4886 case TARGET_SIG_UNBLOCK:
4889 case TARGET_SIG_SETMASK:
4893 ret = -TARGET_EINVAL;
4896 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4898 target_to_host_old_sigset(&set, p);
4899 unlock_user(p, arg2, 0);
4905 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
4906 if (!is_error(ret) && arg3) {
4907 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4909 host_to_target_old_sigset(p, &oldset);
4910 unlock_user(p, arg3, sizeof(target_sigset_t));
4915 case TARGET_NR_rt_sigprocmask:
4918 sigset_t set, oldset, *set_ptr;
4922 case TARGET_SIG_BLOCK:
4925 case TARGET_SIG_UNBLOCK:
4928 case TARGET_SIG_SETMASK:
4932 ret = -TARGET_EINVAL;
4935 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4937 target_to_host_sigset(&set, p);
4938 unlock_user(p, arg2, 0);
4944 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
4945 if (!is_error(ret) && arg3) {
4946 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4948 host_to_target_sigset(p, &oldset);
4949 unlock_user(p, arg3, sizeof(target_sigset_t));
4953 #ifdef TARGET_NR_sigpending
4954 case TARGET_NR_sigpending:
4957 ret = get_errno(sigpending(&set));
4958 if (!is_error(ret)) {
4959 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4961 host_to_target_old_sigset(p, &set);
4962 unlock_user(p, arg1, sizeof(target_sigset_t));
4967 case TARGET_NR_rt_sigpending:
4970 ret = get_errno(sigpending(&set));
4971 if (!is_error(ret)) {
4972 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4974 host_to_target_sigset(p, &set);
4975 unlock_user(p, arg1, sizeof(target_sigset_t));
4979 #ifdef TARGET_NR_sigsuspend
4980 case TARGET_NR_sigsuspend:
4983 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4985 target_to_host_old_sigset(&set, p);
4986 unlock_user(p, arg1, 0);
4987 ret = get_errno(sigsuspend(&set));
4991 case TARGET_NR_rt_sigsuspend:
4994 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4996 target_to_host_sigset(&set, p);
4997 unlock_user(p, arg1, 0);
4998 ret = get_errno(sigsuspend(&set));
5001 case TARGET_NR_rt_sigtimedwait:
5004 struct timespec uts, *puts;
5007 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
5009 target_to_host_sigset(&set, p);
5010 unlock_user(p, arg1, 0);
5013 target_to_host_timespec(puts, arg3);
5017 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
5018 if (!is_error(ret) && arg2) {
5019 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
5021 host_to_target_siginfo(p, &uinfo);
5022 unlock_user(p, arg2, sizeof(target_siginfo_t));
5026 case TARGET_NR_rt_sigqueueinfo:
5029 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
5031 target_to_host_siginfo(&uinfo, p);
5032 unlock_user(p, arg1, 0);
5033 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5036 #ifdef TARGET_NR_sigreturn
5037 case TARGET_NR_sigreturn:
5038 /* NOTE: ret is eax, so not transcoding must be done */
5039 ret = do_sigreturn(cpu_env);
5042 case TARGET_NR_rt_sigreturn:
5043 /* NOTE: ret is eax, so not transcoding must be done */
5044 ret = do_rt_sigreturn(cpu_env);
5046 case TARGET_NR_sethostname:
5047 if (!(p = lock_user_string(arg1)))
5049 ret = get_errno(sethostname(p, arg2));
5050 unlock_user(p, arg1, 0);
5052 case TARGET_NR_setrlimit:
5054 /* XXX: convert resource ? */
5055 int resource = arg1;
5056 struct target_rlimit *target_rlim;
5058 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5060 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
5061 rlim.rlim_max = tswapl(target_rlim->rlim_max);
5062 unlock_user_struct(target_rlim, arg2, 0);
5063 ret = get_errno(setrlimit(resource, &rlim));
5066 case TARGET_NR_getrlimit:
5068 /* XXX: convert resource ? */
5069 int resource = arg1;
5070 struct target_rlimit *target_rlim;
5073 ret = get_errno(getrlimit(resource, &rlim));
5074 if (!is_error(ret)) {
5075 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5077 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
5078 target_rlim->rlim_max = tswapl(rlim.rlim_max);
5079 unlock_user_struct(target_rlim, arg2, 1);
5083 case TARGET_NR_getrusage:
5085 struct rusage rusage;
5086 ret = get_errno(getrusage(arg1, &rusage));
5087 if (!is_error(ret)) {
5088 host_to_target_rusage(arg2, &rusage);
5092 case TARGET_NR_gettimeofday:
5095 ret = get_errno(gettimeofday(&tv, NULL));
5096 if (!is_error(ret)) {
5097 if (copy_to_user_timeval(arg1, &tv))
5102 case TARGET_NR_settimeofday:
5105 if (copy_from_user_timeval(&tv, arg1))
5107 ret = get_errno(settimeofday(&tv, NULL));
5110 #ifdef TARGET_NR_select
5111 case TARGET_NR_select:
5113 struct target_sel_arg_struct *sel;
5114 abi_ulong inp, outp, exp, tvp;
5117 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5119 nsel = tswapl(sel->n);
5120 inp = tswapl(sel->inp);
5121 outp = tswapl(sel->outp);
5122 exp = tswapl(sel->exp);
5123 tvp = tswapl(sel->tvp);
5124 unlock_user_struct(sel, arg1, 0);
5125 ret = do_select(nsel, inp, outp, exp, tvp);
5129 case TARGET_NR_symlink:
5132 p = lock_user_string(arg1);
5133 p2 = lock_user_string(arg2);
5135 ret = -TARGET_EFAULT;
5137 ret = get_errno(symlink(p, p2));
5138 unlock_user(p2, arg2, 0);
5139 unlock_user(p, arg1, 0);
5142 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5143 case TARGET_NR_symlinkat:
5146 p = lock_user_string(arg1);
5147 p2 = lock_user_string(arg3);
5149 ret = -TARGET_EFAULT;
5151 ret = get_errno(sys_symlinkat(p, arg2, p2));
5152 unlock_user(p2, arg3, 0);
5153 unlock_user(p, arg1, 0);
5157 #ifdef TARGET_NR_oldlstat
5158 case TARGET_NR_oldlstat:
5161 case TARGET_NR_readlink:
5164 p = lock_user_string(arg1);
5165 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5167 ret = -TARGET_EFAULT;
5169 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5170 char real[PATH_MAX];
5171 temp = realpath(exec_path,real);
5172 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5173 snprintf((char *)p2, arg3, "%s", real);
5176 ret = get_errno(readlink(path(p), p2, arg3));
5178 unlock_user(p2, arg2, ret);
5179 unlock_user(p, arg1, 0);
5182 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5183 case TARGET_NR_readlinkat:
5186 p = lock_user_string(arg2);
5187 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5189 ret = -TARGET_EFAULT;
5191 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
5192 unlock_user(p2, arg3, ret);
5193 unlock_user(p, arg2, 0);
5197 #ifdef TARGET_NR_uselib
5198 case TARGET_NR_uselib:
5201 #ifdef TARGET_NR_swapon
5202 case TARGET_NR_swapon:
5203 if (!(p = lock_user_string(arg1)))
5205 ret = get_errno(swapon(p, arg2));
5206 unlock_user(p, arg1, 0);
5209 case TARGET_NR_reboot:
5211 #ifdef TARGET_NR_readdir
5212 case TARGET_NR_readdir:
5215 #ifdef TARGET_NR_mmap
5216 case TARGET_NR_mmap:
5217 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE)
5220 abi_ulong v1, v2, v3, v4, v5, v6;
5221 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5229 unlock_user(v, arg1, 0);
5230 ret = get_errno(target_mmap(v1, v2, v3,
5231 target_to_host_bitmask(v4, mmap_flags_tbl),
5235 ret = get_errno(target_mmap(arg1, arg2, arg3,
5236 target_to_host_bitmask(arg4, mmap_flags_tbl),
5242 #ifdef TARGET_NR_mmap2
5243 case TARGET_NR_mmap2:
5245 #define MMAP_SHIFT 12
5247 ret = get_errno(target_mmap(arg1, arg2, arg3,
5248 target_to_host_bitmask(arg4, mmap_flags_tbl),
5250 arg6 << MMAP_SHIFT));
5253 case TARGET_NR_munmap:
5254 ret = get_errno(target_munmap(arg1, arg2));
5256 case TARGET_NR_mprotect:
5257 ret = get_errno(target_mprotect(arg1, arg2, arg3));
5259 #ifdef TARGET_NR_mremap
5260 case TARGET_NR_mremap:
5261 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
5264 /* ??? msync/mlock/munlock are broken for softmmu. */
5265 #ifdef TARGET_NR_msync
5266 case TARGET_NR_msync:
5267 ret = get_errno(msync(g2h(arg1), arg2, arg3));
5270 #ifdef TARGET_NR_mlock
5271 case TARGET_NR_mlock:
5272 ret = get_errno(mlock(g2h(arg1), arg2));
5275 #ifdef TARGET_NR_munlock
5276 case TARGET_NR_munlock:
5277 ret = get_errno(munlock(g2h(arg1), arg2));
5280 #ifdef TARGET_NR_mlockall
5281 case TARGET_NR_mlockall:
5282 ret = get_errno(mlockall(arg1));
5285 #ifdef TARGET_NR_munlockall
5286 case TARGET_NR_munlockall:
5287 ret = get_errno(munlockall());
5290 case TARGET_NR_truncate:
5291 if (!(p = lock_user_string(arg1)))
5293 ret = get_errno(truncate(p, arg2));
5294 unlock_user(p, arg1, 0);
5296 case TARGET_NR_ftruncate:
5297 ret = get_errno(ftruncate(arg1, arg2));
5299 case TARGET_NR_fchmod:
5300 ret = get_errno(fchmod(arg1, arg2));
5302 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5303 case TARGET_NR_fchmodat:
5304 if (!(p = lock_user_string(arg2)))
5306 ret = get_errno(sys_fchmodat(arg1, p, arg3));
5307 unlock_user(p, arg2, 0);
5310 case TARGET_NR_getpriority:
5311 /* libc does special remapping of the return value of
5312 * sys_getpriority() so it's just easiest to call
5313 * sys_getpriority() directly rather than through libc. */
5314 ret = sys_getpriority(arg1, arg2);
5316 case TARGET_NR_setpriority:
5317 ret = get_errno(setpriority(arg1, arg2, arg3));
5319 #ifdef TARGET_NR_profil
5320 case TARGET_NR_profil:
5323 case TARGET_NR_statfs:
5324 if (!(p = lock_user_string(arg1)))
5326 ret = get_errno(statfs(path(p), &stfs));
5327 unlock_user(p, arg1, 0);
5329 if (!is_error(ret)) {
5330 struct target_statfs *target_stfs;
5332 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5334 __put_user(stfs.f_type, &target_stfs->f_type);
5335 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5336 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5337 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5338 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5339 __put_user(stfs.f_files, &target_stfs->f_files);
5340 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5341 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5342 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5343 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5344 unlock_user_struct(target_stfs, arg2, 1);
5347 case TARGET_NR_fstatfs:
5348 ret = get_errno(fstatfs(arg1, &stfs));
5349 goto convert_statfs;
5350 #ifdef TARGET_NR_statfs64
5351 case TARGET_NR_statfs64:
5352 if (!(p = lock_user_string(arg1)))
5354 ret = get_errno(statfs(path(p), &stfs));
5355 unlock_user(p, arg1, 0);
5357 if (!is_error(ret)) {
5358 struct target_statfs64 *target_stfs;
5360 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5362 __put_user(stfs.f_type, &target_stfs->f_type);
5363 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5364 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5365 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5366 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5367 __put_user(stfs.f_files, &target_stfs->f_files);
5368 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5369 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5370 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5371 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5372 unlock_user_struct(target_stfs, arg3, 1);
5375 case TARGET_NR_fstatfs64:
5376 ret = get_errno(fstatfs(arg1, &stfs));
5377 goto convert_statfs64;
5379 #ifdef TARGET_NR_ioperm
5380 case TARGET_NR_ioperm:
5383 #ifdef TARGET_NR_socketcall
5384 case TARGET_NR_socketcall:
5385 ret = do_socketcall(arg1, arg2);
5388 #ifdef TARGET_NR_accept
5389 case TARGET_NR_accept:
5390 ret = do_accept(arg1, arg2, arg3);
5393 #ifdef TARGET_NR_bind
5394 case TARGET_NR_bind:
5395 ret = do_bind(arg1, arg2, arg3);
5398 #ifdef TARGET_NR_connect
5399 case TARGET_NR_connect:
5400 ret = do_connect(arg1, arg2, arg3);
5403 #ifdef TARGET_NR_getpeername
5404 case TARGET_NR_getpeername:
5405 ret = do_getpeername(arg1, arg2, arg3);
5408 #ifdef TARGET_NR_getsockname
5409 case TARGET_NR_getsockname:
5410 ret = do_getsockname(arg1, arg2, arg3);
5413 #ifdef TARGET_NR_getsockopt
5414 case TARGET_NR_getsockopt:
5415 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5418 #ifdef TARGET_NR_listen
5419 case TARGET_NR_listen:
5420 ret = get_errno(listen(arg1, arg2));
5423 #ifdef TARGET_NR_recv
5424 case TARGET_NR_recv:
5425 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
5428 #ifdef TARGET_NR_recvfrom
5429 case TARGET_NR_recvfrom:
5430 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
5433 #ifdef TARGET_NR_recvmsg
5434 case TARGET_NR_recvmsg:
5435 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5438 #ifdef TARGET_NR_send
5439 case TARGET_NR_send:
5440 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
5443 #ifdef TARGET_NR_sendmsg
5444 case TARGET_NR_sendmsg:
5445 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5448 #ifdef TARGET_NR_sendto
5449 case TARGET_NR_sendto:
5450 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
5453 #ifdef TARGET_NR_shutdown
5454 case TARGET_NR_shutdown:
5455 ret = get_errno(shutdown(arg1, arg2));
5458 #ifdef TARGET_NR_socket
5459 case TARGET_NR_socket:
5460 ret = do_socket(arg1, arg2, arg3);
5463 #ifdef TARGET_NR_socketpair
5464 case TARGET_NR_socketpair:
5465 ret = do_socketpair(arg1, arg2, arg3, arg4);
5468 #ifdef TARGET_NR_setsockopt
5469 case TARGET_NR_setsockopt:
5470 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5474 case TARGET_NR_syslog:
5475 if (!(p = lock_user_string(arg2)))
5477 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5478 unlock_user(p, arg2, 0);
5481 case TARGET_NR_setitimer:
5483 struct itimerval value, ovalue, *pvalue;
5487 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5488 || copy_from_user_timeval(&pvalue->it_value,
5489 arg2 + sizeof(struct target_timeval)))
5494 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
5495 if (!is_error(ret) && arg3) {
5496 if (copy_to_user_timeval(arg3,
5497 &ovalue.it_interval)
5498 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5504 case TARGET_NR_getitimer:
5506 struct itimerval value;
5508 ret = get_errno(getitimer(arg1, &value));
5509 if (!is_error(ret) && arg2) {
5510 if (copy_to_user_timeval(arg2,
5512 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5518 case TARGET_NR_stat:
5519 if (!(p = lock_user_string(arg1)))
5521 ret = get_errno(stat(path(p), &st));
5522 unlock_user(p, arg1, 0);
5524 case TARGET_NR_lstat:
5525 if (!(p = lock_user_string(arg1)))
5527 ret = get_errno(lstat(path(p), &st));
5528 unlock_user(p, arg1, 0);
5530 case TARGET_NR_fstat:
5532 ret = get_errno(fstat(arg1, &st));
5534 if (!is_error(ret)) {
5535 struct target_stat *target_st;
5537 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5539 memset(target_st, 0, sizeof(*target_st));
5540 __put_user(st.st_dev, &target_st->st_dev);
5541 __put_user(st.st_ino, &target_st->st_ino);
5542 __put_user(st.st_mode, &target_st->st_mode);
5543 __put_user(st.st_uid, &target_st->st_uid);
5544 __put_user(st.st_gid, &target_st->st_gid);
5545 __put_user(st.st_nlink, &target_st->st_nlink);
5546 __put_user(st.st_rdev, &target_st->st_rdev);
5547 __put_user(st.st_size, &target_st->st_size);
5548 __put_user(st.st_blksize, &target_st->st_blksize);
5549 __put_user(st.st_blocks, &target_st->st_blocks);
5550 __put_user(st.st_atime, &target_st->target_st_atime);
5551 __put_user(st.st_mtime, &target_st->target_st_mtime);
5552 __put_user(st.st_ctime, &target_st->target_st_ctime);
5553 unlock_user_struct(target_st, arg2, 1);
5557 #ifdef TARGET_NR_olduname
5558 case TARGET_NR_olduname:
5561 #ifdef TARGET_NR_iopl
5562 case TARGET_NR_iopl:
5565 case TARGET_NR_vhangup:
5566 ret = get_errno(vhangup());
5568 #ifdef TARGET_NR_idle
5569 case TARGET_NR_idle:
5572 #ifdef TARGET_NR_syscall
5573 case TARGET_NR_syscall:
5574 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5577 case TARGET_NR_wait4:
5580 abi_long status_ptr = arg2;
5581 struct rusage rusage, *rusage_ptr;
5582 abi_ulong target_rusage = arg4;
5584 rusage_ptr = &rusage;
5587 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5588 if (!is_error(ret)) {
5590 status = host_to_target_waitstatus(status);
5591 if (put_user_s32(status, status_ptr))
5595 host_to_target_rusage(target_rusage, &rusage);
5599 #ifdef TARGET_NR_swapoff
5600 case TARGET_NR_swapoff:
5601 if (!(p = lock_user_string(arg1)))
5603 ret = get_errno(swapoff(p));
5604 unlock_user(p, arg1, 0);
5607 case TARGET_NR_sysinfo:
5609 struct target_sysinfo *target_value;
5610 struct sysinfo value;
5611 ret = get_errno(sysinfo(&value));
5612 if (!is_error(ret) && arg1)
5614 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5616 __put_user(value.uptime, &target_value->uptime);
5617 __put_user(value.loads[0], &target_value->loads[0]);
5618 __put_user(value.loads[1], &target_value->loads[1]);
5619 __put_user(value.loads[2], &target_value->loads[2]);
5620 __put_user(value.totalram, &target_value->totalram);
5621 __put_user(value.freeram, &target_value->freeram);
5622 __put_user(value.sharedram, &target_value->sharedram);
5623 __put_user(value.bufferram, &target_value->bufferram);
5624 __put_user(value.totalswap, &target_value->totalswap);
5625 __put_user(value.freeswap, &target_value->freeswap);
5626 __put_user(value.procs, &target_value->procs);
5627 __put_user(value.totalhigh, &target_value->totalhigh);
5628 __put_user(value.freehigh, &target_value->freehigh);
5629 __put_user(value.mem_unit, &target_value->mem_unit);
5630 unlock_user_struct(target_value, arg1, 1);
5634 #ifdef TARGET_NR_ipc
5636 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5639 #ifdef TARGET_NR_semget
5640 case TARGET_NR_semget:
5641 ret = get_errno(semget(arg1, arg2, arg3));
5644 #ifdef TARGET_NR_semop
5645 case TARGET_NR_semop:
5646 ret = get_errno(do_semop(arg1, arg2, arg3));
5649 #ifdef TARGET_NR_semctl
5650 case TARGET_NR_semctl:
5651 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5654 #ifdef TARGET_NR_msgctl
5655 case TARGET_NR_msgctl:
5656 ret = do_msgctl(arg1, arg2, arg3);
5659 #ifdef TARGET_NR_msgget
5660 case TARGET_NR_msgget:
5661 ret = get_errno(msgget(arg1, arg2));
5664 #ifdef TARGET_NR_msgrcv
5665 case TARGET_NR_msgrcv:
5666 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5669 #ifdef TARGET_NR_msgsnd
5670 case TARGET_NR_msgsnd:
5671 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5674 #ifdef TARGET_NR_shmget
5675 case TARGET_NR_shmget:
5676 ret = get_errno(shmget(arg1, arg2, arg3));
5679 #ifdef TARGET_NR_shmctl
5680 case TARGET_NR_shmctl:
5681 ret = do_shmctl(arg1, arg2, arg3);
5684 #ifdef TARGET_NR_shmat
5685 case TARGET_NR_shmat:
5686 ret = do_shmat(arg1, arg2, arg3);
5689 #ifdef TARGET_NR_shmdt
5690 case TARGET_NR_shmdt:
5691 ret = do_shmdt(arg1);
5694 case TARGET_NR_fsync:
5695 ret = get_errno(fsync(arg1));
5697 case TARGET_NR_clone:
5698 #if defined(TARGET_SH4)
5699 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
5700 #elif defined(TARGET_CRIS)
5701 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
5703 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
5706 #ifdef __NR_exit_group
5707 /* new thread calls */
5708 case TARGET_NR_exit_group:
5712 gdb_exit(cpu_env, arg1);
5713 ret = get_errno(exit_group(arg1));
5716 case TARGET_NR_setdomainname:
5717 if (!(p = lock_user_string(arg1)))
5719 ret = get_errno(setdomainname(p, arg2));
5720 unlock_user(p, arg1, 0);
5722 case TARGET_NR_uname:
5723 /* no need to transcode because we use the linux syscall */
5725 struct new_utsname * buf;
5727 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5729 ret = get_errno(sys_uname(buf));
5730 if (!is_error(ret)) {
5731 /* Overrite the native machine name with whatever is being
5733 strcpy (buf->machine, UNAME_MACHINE);
5734 /* Allow the user to override the reported release. */
5735 if (qemu_uname_release && *qemu_uname_release)
5736 strcpy (buf->release, qemu_uname_release);
5738 unlock_user_struct(buf, arg1, 1);
5742 case TARGET_NR_modify_ldt:
5743 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5745 #if !defined(TARGET_X86_64)
5746 case TARGET_NR_vm86old:
5748 case TARGET_NR_vm86:
5749 ret = do_vm86(cpu_env, arg1, arg2);
5753 case TARGET_NR_adjtimex:
5755 #ifdef TARGET_NR_create_module
5756 case TARGET_NR_create_module:
5758 case TARGET_NR_init_module:
5759 case TARGET_NR_delete_module:
5760 #ifdef TARGET_NR_get_kernel_syms
5761 case TARGET_NR_get_kernel_syms:
5764 case TARGET_NR_quotactl:
5766 case TARGET_NR_getpgid:
5767 ret = get_errno(getpgid(arg1));
5769 case TARGET_NR_fchdir:
5770 ret = get_errno(fchdir(arg1));
5772 #ifdef TARGET_NR_bdflush /* not on x86_64 */
5773 case TARGET_NR_bdflush:
5776 #ifdef TARGET_NR_sysfs
5777 case TARGET_NR_sysfs:
5780 case TARGET_NR_personality:
5781 ret = get_errno(personality(arg1));
5783 #ifdef TARGET_NR_afs_syscall
5784 case TARGET_NR_afs_syscall:
5787 #ifdef TARGET_NR__llseek /* Not on alpha */
5788 case TARGET_NR__llseek:
5790 #if defined (__x86_64__)
5791 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
5792 if (put_user_s64(ret, arg4))
5796 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
5797 if (put_user_s64(res, arg4))
5803 case TARGET_NR_getdents:
5804 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
5806 struct target_dirent *target_dirp;
5807 struct linux_dirent *dirp;
5808 abi_long count = arg3;
5810 dirp = malloc(count);
5812 ret = -TARGET_ENOMEM;
5816 ret = get_errno(sys_getdents(arg1, dirp, count));
5817 if (!is_error(ret)) {
5818 struct linux_dirent *de;
5819 struct target_dirent *tde;
5821 int reclen, treclen;
5822 int count1, tnamelen;
5826 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5830 reclen = de->d_reclen;
5831 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
5832 tde->d_reclen = tswap16(treclen);
5833 tde->d_ino = tswapl(de->d_ino);
5834 tde->d_off = tswapl(de->d_off);
5835 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
5838 /* XXX: may not be correct */
5839 pstrcpy(tde->d_name, tnamelen, de->d_name);
5840 de = (struct linux_dirent *)((char *)de + reclen);
5842 tde = (struct target_dirent *)((char *)tde + treclen);
5846 unlock_user(target_dirp, arg2, ret);
5852 struct linux_dirent *dirp;
5853 abi_long count = arg3;
5855 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5857 ret = get_errno(sys_getdents(arg1, dirp, count));
5858 if (!is_error(ret)) {
5859 struct linux_dirent *de;
5864 reclen = de->d_reclen;
5867 de->d_reclen = tswap16(reclen);
5868 tswapls(&de->d_ino);
5869 tswapls(&de->d_off);
5870 de = (struct linux_dirent *)((char *)de + reclen);
5874 unlock_user(dirp, arg2, ret);
5878 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
5879 case TARGET_NR_getdents64:
5881 struct linux_dirent64 *dirp;
5882 abi_long count = arg3;
5883 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5885 ret = get_errno(sys_getdents64(arg1, dirp, count));
5886 if (!is_error(ret)) {
5887 struct linux_dirent64 *de;
5892 reclen = de->d_reclen;
5895 de->d_reclen = tswap16(reclen);
5896 tswap64s((uint64_t *)&de->d_ino);
5897 tswap64s((uint64_t *)&de->d_off);
5898 de = (struct linux_dirent64 *)((char *)de + reclen);
5902 unlock_user(dirp, arg2, ret);
5905 #endif /* TARGET_NR_getdents64 */
5906 #ifdef TARGET_NR__newselect
5907 case TARGET_NR__newselect:
5908 ret = do_select(arg1, arg2, arg3, arg4, arg5);
5911 #ifdef TARGET_NR_poll
5912 case TARGET_NR_poll:
5914 struct target_pollfd *target_pfd;
5915 unsigned int nfds = arg2;
5920 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
5923 pfd = alloca(sizeof(struct pollfd) * nfds);
5924 for(i = 0; i < nfds; i++) {
5925 pfd[i].fd = tswap32(target_pfd[i].fd);
5926 pfd[i].events = tswap16(target_pfd[i].events);
5928 ret = get_errno(poll(pfd, nfds, timeout));
5929 if (!is_error(ret)) {
5930 for(i = 0; i < nfds; i++) {
5931 target_pfd[i].revents = tswap16(pfd[i].revents);
5933 ret += nfds * (sizeof(struct target_pollfd)
5934 - sizeof(struct pollfd));
5936 unlock_user(target_pfd, arg1, ret);
5940 case TARGET_NR_flock:
5941 /* NOTE: the flock constant seems to be the same for every
5943 ret = get_errno(flock(arg1, arg2));
5945 case TARGET_NR_readv:
5950 vec = alloca(count * sizeof(struct iovec));
5951 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
5953 ret = get_errno(readv(arg1, vec, count));
5954 unlock_iovec(vec, arg2, count, 1);
5957 case TARGET_NR_writev:
5962 vec = alloca(count * sizeof(struct iovec));
5963 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
5965 ret = get_errno(writev(arg1, vec, count));
5966 unlock_iovec(vec, arg2, count, 0);
5969 case TARGET_NR_getsid:
5970 ret = get_errno(getsid(arg1));
5972 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
5973 case TARGET_NR_fdatasync:
5974 ret = get_errno(fdatasync(arg1));
5977 case TARGET_NR__sysctl:
5978 /* We don't implement this, but ENOTDIR is always a safe
5980 ret = -TARGET_ENOTDIR;
5982 case TARGET_NR_sched_setparam:
5984 struct sched_param *target_schp;
5985 struct sched_param schp;
5987 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
5989 schp.sched_priority = tswap32(target_schp->sched_priority);
5990 unlock_user_struct(target_schp, arg2, 0);
5991 ret = get_errno(sched_setparam(arg1, &schp));
5994 case TARGET_NR_sched_getparam:
5996 struct sched_param *target_schp;
5997 struct sched_param schp;
5998 ret = get_errno(sched_getparam(arg1, &schp));
5999 if (!is_error(ret)) {
6000 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6002 target_schp->sched_priority = tswap32(schp.sched_priority);
6003 unlock_user_struct(target_schp, arg2, 1);
6007 case TARGET_NR_sched_setscheduler:
6009 struct sched_param *target_schp;
6010 struct sched_param schp;
6011 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6013 schp.sched_priority = tswap32(target_schp->sched_priority);
6014 unlock_user_struct(target_schp, arg3, 0);
6015 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6018 case TARGET_NR_sched_getscheduler:
6019 ret = get_errno(sched_getscheduler(arg1));
6021 case TARGET_NR_sched_yield:
6022 ret = get_errno(sched_yield());
6024 case TARGET_NR_sched_get_priority_max:
6025 ret = get_errno(sched_get_priority_max(arg1));
6027 case TARGET_NR_sched_get_priority_min:
6028 ret = get_errno(sched_get_priority_min(arg1));
6030 case TARGET_NR_sched_rr_get_interval:
6033 ret = get_errno(sched_rr_get_interval(arg1, &ts));
6034 if (!is_error(ret)) {
6035 host_to_target_timespec(arg2, &ts);
6039 case TARGET_NR_nanosleep:
6041 struct timespec req, rem;
6042 target_to_host_timespec(&req, arg1);
6043 ret = get_errno(nanosleep(&req, &rem));
6044 if (is_error(ret) && arg2) {
6045 host_to_target_timespec(arg2, &rem);
6049 #ifdef TARGET_NR_query_module
6050 case TARGET_NR_query_module:
6053 #ifdef TARGET_NR_nfsservctl
6054 case TARGET_NR_nfsservctl:
6057 case TARGET_NR_prctl:
6060 case PR_GET_PDEATHSIG:
6063 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
6064 if (!is_error(ret) && arg2
6065 && put_user_ual(deathsig, arg2))
6070 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6074 #ifdef TARGET_NR_arch_prctl
6075 case TARGET_NR_arch_prctl:
6076 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
6077 ret = do_arch_prctl(cpu_env, arg1, arg2);
6083 #ifdef TARGET_NR_pread
6084 case TARGET_NR_pread:
6086 if (((CPUARMState *)cpu_env)->eabi)
6089 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6091 ret = get_errno(pread(arg1, p, arg3, arg4));
6092 unlock_user(p, arg2, ret);
6094 case TARGET_NR_pwrite:
6096 if (((CPUARMState *)cpu_env)->eabi)
6099 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6101 ret = get_errno(pwrite(arg1, p, arg3, arg4));
6102 unlock_user(p, arg2, 0);
6105 #ifdef TARGET_NR_pread64
6106 case TARGET_NR_pread64:
6107 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6109 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6110 unlock_user(p, arg2, ret);
6112 case TARGET_NR_pwrite64:
6113 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6115 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6116 unlock_user(p, arg2, 0);
6119 case TARGET_NR_getcwd:
6120 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6122 ret = get_errno(sys_getcwd1(p, arg2));
6123 unlock_user(p, arg1, ret);
6125 case TARGET_NR_capget:
6127 case TARGET_NR_capset:
6129 case TARGET_NR_sigaltstack:
6130 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
6131 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6132 defined(TARGET_M68K)
6133 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
6138 case TARGET_NR_sendfile:
6140 #ifdef TARGET_NR_getpmsg
6141 case TARGET_NR_getpmsg:
6144 #ifdef TARGET_NR_putpmsg
6145 case TARGET_NR_putpmsg:
6148 #ifdef TARGET_NR_vfork
6149 case TARGET_NR_vfork:
6150 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6154 #ifdef TARGET_NR_ugetrlimit
6155 case TARGET_NR_ugetrlimit:
6158 ret = get_errno(getrlimit(arg1, &rlim));
6159 if (!is_error(ret)) {
6160 struct target_rlimit *target_rlim;
6161 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6163 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
6164 target_rlim->rlim_max = tswapl(rlim.rlim_max);
6165 unlock_user_struct(target_rlim, arg2, 1);
6170 #ifdef TARGET_NR_truncate64
6171 case TARGET_NR_truncate64:
6172 if (!(p = lock_user_string(arg1)))
6174 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6175 unlock_user(p, arg1, 0);
6178 #ifdef TARGET_NR_ftruncate64
6179 case TARGET_NR_ftruncate64:
6180 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
6183 #ifdef TARGET_NR_stat64
6184 case TARGET_NR_stat64:
6185 if (!(p = lock_user_string(arg1)))
6187 ret = get_errno(stat(path(p), &st));
6188 unlock_user(p, arg1, 0);
6190 ret = host_to_target_stat64(cpu_env, arg2, &st);
6193 #ifdef TARGET_NR_lstat64
6194 case TARGET_NR_lstat64:
6195 if (!(p = lock_user_string(arg1)))
6197 ret = get_errno(lstat(path(p), &st));
6198 unlock_user(p, arg1, 0);
6200 ret = host_to_target_stat64(cpu_env, arg2, &st);
6203 #ifdef TARGET_NR_fstat64
6204 case TARGET_NR_fstat64:
6205 ret = get_errno(fstat(arg1, &st));
6207 ret = host_to_target_stat64(cpu_env, arg2, &st);
6210 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6211 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6212 #ifdef TARGET_NR_fstatat64
6213 case TARGET_NR_fstatat64:
6215 #ifdef TARGET_NR_newfstatat
6216 case TARGET_NR_newfstatat:
6218 if (!(p = lock_user_string(arg2)))
6220 #ifdef __NR_fstatat64
6221 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
6223 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6226 ret = host_to_target_stat64(cpu_env, arg3, &st);
6230 case TARGET_NR_lchown:
6231 if (!(p = lock_user_string(arg1)))
6233 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6234 unlock_user(p, arg1, 0);
6236 case TARGET_NR_getuid:
6237 ret = get_errno(high2lowuid(getuid()));
6239 case TARGET_NR_getgid:
6240 ret = get_errno(high2lowgid(getgid()));
6242 case TARGET_NR_geteuid:
6243 ret = get_errno(high2lowuid(geteuid()));
6245 case TARGET_NR_getegid:
6246 ret = get_errno(high2lowgid(getegid()));
6248 case TARGET_NR_setreuid:
6249 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6251 case TARGET_NR_setregid:
6252 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6254 case TARGET_NR_getgroups:
6256 int gidsetsize = arg1;
6257 uint16_t *target_grouplist;
6261 grouplist = alloca(gidsetsize * sizeof(gid_t));
6262 ret = get_errno(getgroups(gidsetsize, grouplist));
6263 if (gidsetsize == 0)
6265 if (!is_error(ret)) {
6266 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6267 if (!target_grouplist)
6269 for(i = 0;i < ret; i++)
6270 target_grouplist[i] = tswap16(grouplist[i]);
6271 unlock_user(target_grouplist, arg2, gidsetsize * 2);
6275 case TARGET_NR_setgroups:
6277 int gidsetsize = arg1;
6278 uint16_t *target_grouplist;
6282 grouplist = alloca(gidsetsize * sizeof(gid_t));
6283 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6284 if (!target_grouplist) {
6285 ret = -TARGET_EFAULT;
6288 for(i = 0;i < gidsetsize; i++)
6289 grouplist[i] = tswap16(target_grouplist[i]);
6290 unlock_user(target_grouplist, arg2, 0);
6291 ret = get_errno(setgroups(gidsetsize, grouplist));
6294 case TARGET_NR_fchown:
6295 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6297 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6298 case TARGET_NR_fchownat:
6299 if (!(p = lock_user_string(arg2)))
6301 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6302 unlock_user(p, arg2, 0);
6305 #ifdef TARGET_NR_setresuid
6306 case TARGET_NR_setresuid:
6307 ret = get_errno(setresuid(low2highuid(arg1),
6309 low2highuid(arg3)));
6312 #ifdef TARGET_NR_getresuid
6313 case TARGET_NR_getresuid:
6315 uid_t ruid, euid, suid;
6316 ret = get_errno(getresuid(&ruid, &euid, &suid));
6317 if (!is_error(ret)) {
6318 if (put_user_u16(high2lowuid(ruid), arg1)
6319 || put_user_u16(high2lowuid(euid), arg2)
6320 || put_user_u16(high2lowuid(suid), arg3))
6326 #ifdef TARGET_NR_getresgid
6327 case TARGET_NR_setresgid:
6328 ret = get_errno(setresgid(low2highgid(arg1),
6330 low2highgid(arg3)));
6333 #ifdef TARGET_NR_getresgid
6334 case TARGET_NR_getresgid:
6336 gid_t rgid, egid, sgid;
6337 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6338 if (!is_error(ret)) {
6339 if (put_user_u16(high2lowgid(rgid), arg1)
6340 || put_user_u16(high2lowgid(egid), arg2)
6341 || put_user_u16(high2lowgid(sgid), arg3))
6347 case TARGET_NR_chown:
6348 if (!(p = lock_user_string(arg1)))
6350 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6351 unlock_user(p, arg1, 0);
6353 case TARGET_NR_setuid:
6354 ret = get_errno(setuid(low2highuid(arg1)));
6356 case TARGET_NR_setgid:
6357 ret = get_errno(setgid(low2highgid(arg1)));
6359 case TARGET_NR_setfsuid:
6360 ret = get_errno(setfsuid(arg1));
6362 case TARGET_NR_setfsgid:
6363 ret = get_errno(setfsgid(arg1));
6365 #endif /* USE_UID16 */
6367 #ifdef TARGET_NR_lchown32
6368 case TARGET_NR_lchown32:
6369 if (!(p = lock_user_string(arg1)))
6371 ret = get_errno(lchown(p, arg2, arg3));
6372 unlock_user(p, arg1, 0);
6375 #ifdef TARGET_NR_getuid32
6376 case TARGET_NR_getuid32:
6377 ret = get_errno(getuid());
6381 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6382 /* Alpha specific */
6383 case TARGET_NR_getxuid:
6387 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6389 ret = get_errno(getuid());
6392 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6393 /* Alpha specific */
6394 case TARGET_NR_getxgid:
6398 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6400 ret = get_errno(getgid());
6404 #ifdef TARGET_NR_getgid32
6405 case TARGET_NR_getgid32:
6406 ret = get_errno(getgid());
6409 #ifdef TARGET_NR_geteuid32
6410 case TARGET_NR_geteuid32:
6411 ret = get_errno(geteuid());
6414 #ifdef TARGET_NR_getegid32
6415 case TARGET_NR_getegid32:
6416 ret = get_errno(getegid());
6419 #ifdef TARGET_NR_setreuid32
6420 case TARGET_NR_setreuid32:
6421 ret = get_errno(setreuid(arg1, arg2));
6424 #ifdef TARGET_NR_setregid32
6425 case TARGET_NR_setregid32:
6426 ret = get_errno(setregid(arg1, arg2));
6429 #ifdef TARGET_NR_getgroups32
6430 case TARGET_NR_getgroups32:
6432 int gidsetsize = arg1;
6433 uint32_t *target_grouplist;
6437 grouplist = alloca(gidsetsize * sizeof(gid_t));
6438 ret = get_errno(getgroups(gidsetsize, grouplist));
6439 if (gidsetsize == 0)
6441 if (!is_error(ret)) {
6442 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6443 if (!target_grouplist) {
6444 ret = -TARGET_EFAULT;
6447 for(i = 0;i < ret; i++)
6448 target_grouplist[i] = tswap32(grouplist[i]);
6449 unlock_user(target_grouplist, arg2, gidsetsize * 4);
6454 #ifdef TARGET_NR_setgroups32
6455 case TARGET_NR_setgroups32:
6457 int gidsetsize = arg1;
6458 uint32_t *target_grouplist;
6462 grouplist = alloca(gidsetsize * sizeof(gid_t));
6463 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6464 if (!target_grouplist) {
6465 ret = -TARGET_EFAULT;
6468 for(i = 0;i < gidsetsize; i++)
6469 grouplist[i] = tswap32(target_grouplist[i]);
6470 unlock_user(target_grouplist, arg2, 0);
6471 ret = get_errno(setgroups(gidsetsize, grouplist));
6475 #ifdef TARGET_NR_fchown32
6476 case TARGET_NR_fchown32:
6477 ret = get_errno(fchown(arg1, arg2, arg3));
6480 #ifdef TARGET_NR_setresuid32
6481 case TARGET_NR_setresuid32:
6482 ret = get_errno(setresuid(arg1, arg2, arg3));
6485 #ifdef TARGET_NR_getresuid32
6486 case TARGET_NR_getresuid32:
6488 uid_t ruid, euid, suid;
6489 ret = get_errno(getresuid(&ruid, &euid, &suid));
6490 if (!is_error(ret)) {
6491 if (put_user_u32(ruid, arg1)
6492 || put_user_u32(euid, arg2)
6493 || put_user_u32(suid, arg3))
6499 #ifdef TARGET_NR_setresgid32
6500 case TARGET_NR_setresgid32:
6501 ret = get_errno(setresgid(arg1, arg2, arg3));
6504 #ifdef TARGET_NR_getresgid32
6505 case TARGET_NR_getresgid32:
6507 gid_t rgid, egid, sgid;
6508 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6509 if (!is_error(ret)) {
6510 if (put_user_u32(rgid, arg1)
6511 || put_user_u32(egid, arg2)
6512 || put_user_u32(sgid, arg3))
6518 #ifdef TARGET_NR_chown32
6519 case TARGET_NR_chown32:
6520 if (!(p = lock_user_string(arg1)))
6522 ret = get_errno(chown(p, arg2, arg3));
6523 unlock_user(p, arg1, 0);
6526 #ifdef TARGET_NR_setuid32
6527 case TARGET_NR_setuid32:
6528 ret = get_errno(setuid(arg1));
6531 #ifdef TARGET_NR_setgid32
6532 case TARGET_NR_setgid32:
6533 ret = get_errno(setgid(arg1));
6536 #ifdef TARGET_NR_setfsuid32
6537 case TARGET_NR_setfsuid32:
6538 ret = get_errno(setfsuid(arg1));
6541 #ifdef TARGET_NR_setfsgid32
6542 case TARGET_NR_setfsgid32:
6543 ret = get_errno(setfsgid(arg1));
6547 case TARGET_NR_pivot_root:
6549 #ifdef TARGET_NR_mincore
6550 case TARGET_NR_mincore:
6553 ret = -TARGET_EFAULT;
6554 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6556 if (!(p = lock_user_string(arg3)))
6558 ret = get_errno(mincore(a, arg2, p));
6559 unlock_user(p, arg3, ret);
6561 unlock_user(a, arg1, 0);
6565 #ifdef TARGET_NR_arm_fadvise64_64
6566 case TARGET_NR_arm_fadvise64_64:
6569 * arm_fadvise64_64 looks like fadvise64_64 but
6570 * with different argument order
6578 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
6579 #ifdef TARGET_NR_fadvise64_64
6580 case TARGET_NR_fadvise64_64:
6582 #ifdef TARGET_NR_fadvise64
6583 case TARGET_NR_fadvise64:
6587 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
6588 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
6589 case 6: arg4 = POSIX_FADV_DONTNEED; break;
6590 case 7: arg4 = POSIX_FADV_NOREUSE; break;
6594 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
6597 #ifdef TARGET_NR_madvise
6598 case TARGET_NR_madvise:
6599 /* A straight passthrough may not be safe because qemu sometimes
6600 turns private flie-backed mappings into anonymous mappings.
6601 This will break MADV_DONTNEED.
6602 This is a hint, so ignoring and returning success is ok. */
6606 #if TARGET_ABI_BITS == 32
6607 case TARGET_NR_fcntl64:
6611 struct target_flock64 *target_fl;
6613 struct target_eabi_flock64 *target_efl;
6616 cmd = target_to_host_fcntl_cmd(arg2);
6617 if (cmd == -TARGET_EINVAL)
6621 case TARGET_F_GETLK64:
6623 if (((CPUARMState *)cpu_env)->eabi) {
6624 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6626 fl.l_type = tswap16(target_efl->l_type);
6627 fl.l_whence = tswap16(target_efl->l_whence);
6628 fl.l_start = tswap64(target_efl->l_start);
6629 fl.l_len = tswap64(target_efl->l_len);
6630 fl.l_pid = tswap32(target_efl->l_pid);
6631 unlock_user_struct(target_efl, arg3, 0);
6635 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6637 fl.l_type = tswap16(target_fl->l_type);
6638 fl.l_whence = tswap16(target_fl->l_whence);
6639 fl.l_start = tswap64(target_fl->l_start);
6640 fl.l_len = tswap64(target_fl->l_len);
6641 fl.l_pid = tswap32(target_fl->l_pid);
6642 unlock_user_struct(target_fl, arg3, 0);
6644 ret = get_errno(fcntl(arg1, cmd, &fl));
6647 if (((CPUARMState *)cpu_env)->eabi) {
6648 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6650 target_efl->l_type = tswap16(fl.l_type);
6651 target_efl->l_whence = tswap16(fl.l_whence);
6652 target_efl->l_start = tswap64(fl.l_start);
6653 target_efl->l_len = tswap64(fl.l_len);
6654 target_efl->l_pid = tswap32(fl.l_pid);
6655 unlock_user_struct(target_efl, arg3, 1);
6659 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6661 target_fl->l_type = tswap16(fl.l_type);
6662 target_fl->l_whence = tswap16(fl.l_whence);
6663 target_fl->l_start = tswap64(fl.l_start);
6664 target_fl->l_len = tswap64(fl.l_len);
6665 target_fl->l_pid = tswap32(fl.l_pid);
6666 unlock_user_struct(target_fl, arg3, 1);
6671 case TARGET_F_SETLK64:
6672 case TARGET_F_SETLKW64:
6674 if (((CPUARMState *)cpu_env)->eabi) {
6675 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6677 fl.l_type = tswap16(target_efl->l_type);
6678 fl.l_whence = tswap16(target_efl->l_whence);
6679 fl.l_start = tswap64(target_efl->l_start);
6680 fl.l_len = tswap64(target_efl->l_len);
6681 fl.l_pid = tswap32(target_efl->l_pid);
6682 unlock_user_struct(target_efl, arg3, 0);
6686 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6688 fl.l_type = tswap16(target_fl->l_type);
6689 fl.l_whence = tswap16(target_fl->l_whence);
6690 fl.l_start = tswap64(target_fl->l_start);
6691 fl.l_len = tswap64(target_fl->l_len);
6692 fl.l_pid = tswap32(target_fl->l_pid);
6693 unlock_user_struct(target_fl, arg3, 0);
6695 ret = get_errno(fcntl(arg1, cmd, &fl));
6698 ret = do_fcntl(arg1, arg2, arg3);
6704 #ifdef TARGET_NR_cacheflush
6705 case TARGET_NR_cacheflush:
6706 /* self-modifying code is handled automatically, so nothing needed */
6710 #ifdef TARGET_NR_security
6711 case TARGET_NR_security:
6714 #ifdef TARGET_NR_getpagesize
6715 case TARGET_NR_getpagesize:
6716 ret = TARGET_PAGE_SIZE;
6719 case TARGET_NR_gettid:
6720 ret = get_errno(gettid());
6722 #ifdef TARGET_NR_readahead
6723 case TARGET_NR_readahead:
6724 #if TARGET_ABI_BITS == 32
6726 if (((CPUARMState *)cpu_env)->eabi)
6733 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
6735 ret = get_errno(readahead(arg1, arg2, arg3));
6739 #ifdef TARGET_NR_setxattr
6740 case TARGET_NR_setxattr:
6741 case TARGET_NR_lsetxattr:
6742 case TARGET_NR_fsetxattr:
6743 case TARGET_NR_getxattr:
6744 case TARGET_NR_lgetxattr:
6745 case TARGET_NR_fgetxattr:
6746 case TARGET_NR_listxattr:
6747 case TARGET_NR_llistxattr:
6748 case TARGET_NR_flistxattr:
6749 case TARGET_NR_removexattr:
6750 case TARGET_NR_lremovexattr:
6751 case TARGET_NR_fremovexattr:
6752 ret = -TARGET_EOPNOTSUPP;
6755 #ifdef TARGET_NR_set_thread_area
6756 case TARGET_NR_set_thread_area:
6757 #if defined(TARGET_MIPS)
6758 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
6761 #elif defined(TARGET_CRIS)
6763 ret = -TARGET_EINVAL;
6765 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
6769 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
6770 ret = do_set_thread_area(cpu_env, arg1);
6773 goto unimplemented_nowarn;
6776 #ifdef TARGET_NR_get_thread_area
6777 case TARGET_NR_get_thread_area:
6778 #if defined(TARGET_I386) && defined(TARGET_ABI32)
6779 ret = do_get_thread_area(cpu_env, arg1);
6781 goto unimplemented_nowarn;
6784 #ifdef TARGET_NR_getdomainname
6785 case TARGET_NR_getdomainname:
6786 goto unimplemented_nowarn;
6789 #ifdef TARGET_NR_clock_gettime
6790 case TARGET_NR_clock_gettime:
6793 ret = get_errno(clock_gettime(arg1, &ts));
6794 if (!is_error(ret)) {
6795 host_to_target_timespec(arg2, &ts);
6800 #ifdef TARGET_NR_clock_getres
6801 case TARGET_NR_clock_getres:
6804 ret = get_errno(clock_getres(arg1, &ts));
6805 if (!is_error(ret)) {
6806 host_to_target_timespec(arg2, &ts);
6811 #ifdef TARGET_NR_clock_nanosleep
6812 case TARGET_NR_clock_nanosleep:
6815 target_to_host_timespec(&ts, arg3);
6816 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
6818 host_to_target_timespec(arg4, &ts);
6823 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
6824 case TARGET_NR_set_tid_address:
6825 ret = get_errno(set_tid_address((int *)g2h(arg1)));
6829 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
6830 case TARGET_NR_tkill:
6831 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
6835 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
6836 case TARGET_NR_tgkill:
6837 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
6838 target_to_host_signal(arg3)));
6842 #ifdef TARGET_NR_set_robust_list
6843 case TARGET_NR_set_robust_list:
6844 goto unimplemented_nowarn;
6847 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
6848 case TARGET_NR_utimensat:
6850 struct timespec *tsp, ts[2];
6854 target_to_host_timespec(ts, arg3);
6855 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
6859 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
6861 if (!(p = lock_user_string(arg2))) {
6862 ret = -TARGET_EFAULT;
6865 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
6866 unlock_user(p, arg2, 0);
6871 #if defined(CONFIG_USE_NPTL)
6872 case TARGET_NR_futex:
6873 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
6876 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
6877 case TARGET_NR_inotify_init:
6878 ret = get_errno(sys_inotify_init());
6881 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
6882 case TARGET_NR_inotify_add_watch:
6883 p = lock_user_string(arg2);
6884 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
6885 unlock_user(p, arg2, 0);
6888 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
6889 case TARGET_NR_inotify_rm_watch:
6890 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
6894 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
6895 case TARGET_NR_mq_open:
6897 struct mq_attr posix_mq_attr;
6899 p = lock_user_string(arg1 - 1);
6901 copy_from_user_mq_attr (&posix_mq_attr, arg4);
6902 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
6903 unlock_user (p, arg1, 0);
6907 case TARGET_NR_mq_unlink:
6908 p = lock_user_string(arg1 - 1);
6909 ret = get_errno(mq_unlink(p));
6910 unlock_user (p, arg1, 0);
6913 case TARGET_NR_mq_timedsend:
6917 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6919 target_to_host_timespec(&ts, arg5);
6920 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
6921 host_to_target_timespec(arg5, &ts);
6924 ret = get_errno(mq_send(arg1, p, arg3, arg4));
6925 unlock_user (p, arg2, arg3);
6929 case TARGET_NR_mq_timedreceive:
6934 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6936 target_to_host_timespec(&ts, arg5);
6937 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
6938 host_to_target_timespec(arg5, &ts);
6941 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
6942 unlock_user (p, arg2, arg3);
6944 put_user_u32(prio, arg4);
6948 /* Not implemented for now... */
6949 /* case TARGET_NR_mq_notify: */
6952 case TARGET_NR_mq_getsetattr:
6954 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
6957 ret = mq_getattr(arg1, &posix_mq_attr_out);
6958 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
6961 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
6962 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
6969 #ifdef CONFIG_SPLICE
6970 #ifdef TARGET_NR_tee
6973 ret = get_errno(tee(arg1,arg2,arg3,arg4));
6977 #ifdef TARGET_NR_splice
6978 case TARGET_NR_splice:
6980 loff_t loff_in, loff_out;
6981 loff_t *ploff_in = NULL, *ploff_out = NULL;
6983 get_user_u64(loff_in, arg2);
6984 ploff_in = &loff_in;
6987 get_user_u64(loff_out, arg2);
6988 ploff_out = &loff_out;
6990 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
6994 #ifdef TARGET_NR_vmsplice
6995 case TARGET_NR_vmsplice:
7000 vec = alloca(count * sizeof(struct iovec));
7001 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7003 ret = get_errno(vmsplice(arg1, vec, count, arg4));
7004 unlock_iovec(vec, arg2, count, 0);
7008 #endif /* CONFIG_SPLICE */
7009 #ifdef CONFIG_EVENTFD
7010 #if defined(TARGET_NR_eventfd)
7011 case TARGET_NR_eventfd:
7012 ret = get_errno(eventfd(arg1, 0));
7015 #if defined(TARGET_NR_eventfd2)
7016 case TARGET_NR_eventfd2:
7017 ret = get_errno(eventfd(arg1, arg2));
7020 #endif /* CONFIG_EVENTFD */
7021 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7022 case TARGET_NR_fallocate:
7023 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7028 gemu_log("qemu: Unsupported syscall: %d\n", num);
7029 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
7030 unimplemented_nowarn:
7032 ret = -TARGET_ENOSYS;
7037 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
7040 print_syscall_ret(num, ret);
7043 ret = -TARGET_EFAULT;