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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
33 #include <sys/types.h>
39 #include <sys/mount.h>
40 #include <sys/prctl.h>
41 #include <sys/resource.h>
46 #include <sys/socket.h>
50 #include <sys/times.h>
53 #include <sys/statfs.h>
55 #include <sys/sysinfo.h>
56 #include <sys/utsname.h>
57 //#include <sys/user.h>
58 #include <netinet/ip.h>
59 #include <netinet/tcp.h>
60 #include <qemu-common.h>
65 #define termios host_termios
66 #define winsize host_winsize
67 #define termio host_termio
68 #define sgttyb host_sgttyb /* same as target */
69 #define tchars host_tchars /* same as target */
70 #define ltchars host_ltchars /* same as target */
72 #include <linux/termios.h>
73 #include <linux/unistd.h>
74 #include <linux/utsname.h>
75 #include <linux/cdrom.h>
76 #include <linux/hdreg.h>
77 #include <linux/soundcard.h>
79 #include <linux/mtio.h>
80 #include "linux_loop.h"
83 #include "qemu-common.h"
86 #include <linux/futex.h>
87 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
88 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
90 /* XXX: Hardcode the above values. */
91 #define CLONE_NPTL_FLAGS2 0
96 //#include <linux/msdos_fs.h>
97 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
98 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
109 #define _syscall0(type,name) \
110 static type name (void) \
112 return syscall(__NR_##name); \
115 #define _syscall1(type,name,type1,arg1) \
116 static type name (type1 arg1) \
118 return syscall(__NR_##name, arg1); \
121 #define _syscall2(type,name,type1,arg1,type2,arg2) \
122 static type name (type1 arg1,type2 arg2) \
124 return syscall(__NR_##name, arg1, arg2); \
127 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
128 static type name (type1 arg1,type2 arg2,type3 arg3) \
130 return syscall(__NR_##name, arg1, arg2, arg3); \
133 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
134 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
136 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
139 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
141 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
143 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
147 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
148 type5,arg5,type6,arg6) \
149 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
152 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
156 #define __NR_sys_uname __NR_uname
157 #define __NR_sys_faccessat __NR_faccessat
158 #define __NR_sys_fchmodat __NR_fchmodat
159 #define __NR_sys_fchownat __NR_fchownat
160 #define __NR_sys_fstatat64 __NR_fstatat64
161 #define __NR_sys_futimesat __NR_futimesat
162 #define __NR_sys_getcwd1 __NR_getcwd
163 #define __NR_sys_getdents __NR_getdents
164 #define __NR_sys_getdents64 __NR_getdents64
165 #define __NR_sys_getpriority __NR_getpriority
166 #define __NR_sys_linkat __NR_linkat
167 #define __NR_sys_mkdirat __NR_mkdirat
168 #define __NR_sys_mknodat __NR_mknodat
169 #define __NR_sys_newfstatat __NR_newfstatat
170 #define __NR_sys_openat __NR_openat
171 #define __NR_sys_readlinkat __NR_readlinkat
172 #define __NR_sys_renameat __NR_renameat
173 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
174 #define __NR_sys_symlinkat __NR_symlinkat
175 #define __NR_sys_syslog __NR_syslog
176 #define __NR_sys_tgkill __NR_tgkill
177 #define __NR_sys_tkill __NR_tkill
178 #define __NR_sys_unlinkat __NR_unlinkat
179 #define __NR_sys_utimensat __NR_utimensat
180 #define __NR_sys_futex __NR_futex
181 #define __NR_sys_inotify_init __NR_inotify_init
182 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
183 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
185 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
186 #define __NR__llseek __NR_lseek
190 _syscall0(int, gettid)
192 /* This is a replacement for the host gettid() and must return a host
194 static int gettid(void) {
198 #if TARGET_ABI_BITS == 32
199 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
201 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
202 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
204 _syscall2(int, sys_getpriority, int, which, int, who);
205 #if !defined (__x86_64__)
206 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
207 loff_t *, res, uint, wh);
209 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
210 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
211 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
212 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
214 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
215 _syscall2(int,sys_tkill,int,tid,int,sig)
217 #ifdef __NR_exit_group
218 _syscall1(int,exit_group,int,error_code)
220 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
221 _syscall1(int,set_tid_address,int *,tidptr)
223 #if defined(USE_NPTL)
224 #if defined(TARGET_NR_futex) && defined(__NR_futex)
225 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
226 const struct timespec *,timeout,int *,uaddr2,int,val3)
230 static bitmask_transtbl fcntl_flags_tbl[] = {
231 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
232 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
233 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
234 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
235 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
236 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
237 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
238 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
239 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
240 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
241 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
242 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
243 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
244 #if defined(O_DIRECT)
245 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
250 #define COPY_UTSNAME_FIELD(dest, src) \
252 /* __NEW_UTS_LEN doesn't include terminating null */ \
253 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
254 (dest)[__NEW_UTS_LEN] = '\0'; \
257 static int sys_uname(struct new_utsname *buf)
259 struct utsname uts_buf;
261 if (uname(&uts_buf) < 0)
265 * Just in case these have some differences, we
266 * translate utsname to new_utsname (which is the
267 * struct linux kernel uses).
270 bzero(buf, sizeof (*buf));
271 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
272 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
273 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
274 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
275 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
277 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
281 #undef COPY_UTSNAME_FIELD
284 static int sys_getcwd1(char *buf, size_t size)
286 if (getcwd(buf, size) == NULL) {
287 /* getcwd() sets errno */
290 return strlen(buf)+1;
295 * Host system seems to have atfile syscall stubs available. We
296 * now enable them one by one as specified by target syscall_nr.h.
299 #ifdef TARGET_NR_faccessat
300 static int sys_faccessat(int dirfd, const char *pathname, int mode)
302 return (faccessat(dirfd, pathname, mode, 0));
305 #ifdef TARGET_NR_fchmodat
306 static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
308 return (fchmodat(dirfd, pathname, mode, 0));
311 #if defined(TARGET_NR_fchownat) && defined(USE_UID16)
312 static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
313 gid_t group, int flags)
315 return (fchownat(dirfd, pathname, owner, group, flags));
318 #ifdef __NR_fstatat64
319 static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
322 return (fstatat(dirfd, pathname, buf, flags));
325 #ifdef __NR_newfstatat
326 static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
329 return (fstatat(dirfd, pathname, buf, flags));
332 #ifdef TARGET_NR_futimesat
333 static int sys_futimesat(int dirfd, const char *pathname,
334 const struct timeval times[2])
336 return (futimesat(dirfd, pathname, times));
339 #ifdef TARGET_NR_linkat
340 static int sys_linkat(int olddirfd, const char *oldpath,
341 int newdirfd, const char *newpath, int flags)
343 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
346 #ifdef TARGET_NR_mkdirat
347 static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
349 return (mkdirat(dirfd, pathname, mode));
352 #ifdef TARGET_NR_mknodat
353 static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
356 return (mknodat(dirfd, pathname, mode, dev));
359 #ifdef TARGET_NR_openat
360 static int sys_openat(int dirfd, const char *pathname, int flags, ...)
363 * open(2) has extra parameter 'mode' when called with
366 if ((flags & O_CREAT) != 0) {
371 * Get the 'mode' parameter and translate it to
375 mode = va_arg(ap, mode_t);
376 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
379 return (openat(dirfd, pathname, flags, mode));
381 return (openat(dirfd, pathname, flags));
384 #ifdef TARGET_NR_readlinkat
385 static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
387 return (readlinkat(dirfd, pathname, buf, bufsiz));
390 #ifdef TARGET_NR_renameat
391 static int sys_renameat(int olddirfd, const char *oldpath,
392 int newdirfd, const char *newpath)
394 return (renameat(olddirfd, oldpath, newdirfd, newpath));
397 #ifdef TARGET_NR_symlinkat
398 static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
400 return (symlinkat(oldpath, newdirfd, newpath));
403 #ifdef TARGET_NR_unlinkat
404 static int sys_unlinkat(int dirfd, const char *pathname, int flags)
406 return (unlinkat(dirfd, pathname, flags));
409 #else /* !CONFIG_ATFILE */
412 * Try direct syscalls instead
414 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
415 _syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
417 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
418 _syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
420 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
421 _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
422 uid_t,owner,gid_t,group,int,flags)
424 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
425 defined(__NR_fstatat64)
426 _syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
427 struct stat *,buf,int,flags)
429 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
430 _syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
431 const struct timeval *,times)
433 #if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
434 defined(__NR_newfstatat)
435 _syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
436 struct stat *,buf,int,flags)
438 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
439 _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
440 int,newdirfd,const char *,newpath,int,flags)
442 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
443 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
445 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
446 _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
447 mode_t,mode,dev_t,dev)
449 #if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
450 defined(__NR_newfstatat)
451 _syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
452 struct stat *,buf,int,flags)
454 #if defined(TARGET_NR_openat) && defined(__NR_openat)
455 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
457 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
458 _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
459 char *,buf,size_t,bufsize)
461 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
462 _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
463 int,newdirfd,const char *,newpath)
465 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
466 _syscall3(int,sys_symlinkat,const char *,oldpath,
467 int,newdirfd,const char *,newpath)
469 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
470 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
473 #endif /* CONFIG_ATFILE */
475 #ifdef CONFIG_UTIMENSAT
476 static int sys_utimensat(int dirfd, const char *pathname,
477 const struct timespec times[2], int flags)
479 if (pathname == NULL)
480 return futimens(dirfd, times);
482 return utimensat(dirfd, pathname, times, flags);
485 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
486 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
487 const struct timespec *,tsp,int,flags)
489 #endif /* CONFIG_UTIMENSAT */
491 #ifdef CONFIG_INOTIFY
492 #include <sys/inotify.h>
494 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
495 static int sys_inotify_init(void)
497 return (inotify_init());
500 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
501 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
503 return (inotify_add_watch(fd, pathname, mask));
506 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
507 static int sys_inotify_rm_watch(int fd, int32_t wd)
509 return (inotify_rm_watch(fd, wd));
513 /* Userspace can usually survive runtime without inotify */
514 #undef TARGET_NR_inotify_init
515 #undef TARGET_NR_inotify_add_watch
516 #undef TARGET_NR_inotify_rm_watch
517 #endif /* CONFIG_INOTIFY */
520 extern int personality(int);
521 extern int flock(int, int);
522 extern int setfsuid(int);
523 extern int setfsgid(int);
524 extern int setgroups(int, gid_t *);
526 #define ERRNO_TABLE_SIZE 1200
528 /* target_to_host_errno_table[] is initialized from
529 * host_to_target_errno_table[] in syscall_init(). */
530 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
534 * This list is the union of errno values overridden in asm-<arch>/errno.h
535 * minus the errnos that are not actually generic to all archs.
537 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
538 [EIDRM] = TARGET_EIDRM,
539 [ECHRNG] = TARGET_ECHRNG,
540 [EL2NSYNC] = TARGET_EL2NSYNC,
541 [EL3HLT] = TARGET_EL3HLT,
542 [EL3RST] = TARGET_EL3RST,
543 [ELNRNG] = TARGET_ELNRNG,
544 [EUNATCH] = TARGET_EUNATCH,
545 [ENOCSI] = TARGET_ENOCSI,
546 [EL2HLT] = TARGET_EL2HLT,
547 [EDEADLK] = TARGET_EDEADLK,
548 [ENOLCK] = TARGET_ENOLCK,
549 [EBADE] = TARGET_EBADE,
550 [EBADR] = TARGET_EBADR,
551 [EXFULL] = TARGET_EXFULL,
552 [ENOANO] = TARGET_ENOANO,
553 [EBADRQC] = TARGET_EBADRQC,
554 [EBADSLT] = TARGET_EBADSLT,
555 [EBFONT] = TARGET_EBFONT,
556 [ENOSTR] = TARGET_ENOSTR,
557 [ENODATA] = TARGET_ENODATA,
558 [ETIME] = TARGET_ETIME,
559 [ENOSR] = TARGET_ENOSR,
560 [ENONET] = TARGET_ENONET,
561 [ENOPKG] = TARGET_ENOPKG,
562 [EREMOTE] = TARGET_EREMOTE,
563 [ENOLINK] = TARGET_ENOLINK,
564 [EADV] = TARGET_EADV,
565 [ESRMNT] = TARGET_ESRMNT,
566 [ECOMM] = TARGET_ECOMM,
567 [EPROTO] = TARGET_EPROTO,
568 [EDOTDOT] = TARGET_EDOTDOT,
569 [EMULTIHOP] = TARGET_EMULTIHOP,
570 [EBADMSG] = TARGET_EBADMSG,
571 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
572 [EOVERFLOW] = TARGET_EOVERFLOW,
573 [ENOTUNIQ] = TARGET_ENOTUNIQ,
574 [EBADFD] = TARGET_EBADFD,
575 [EREMCHG] = TARGET_EREMCHG,
576 [ELIBACC] = TARGET_ELIBACC,
577 [ELIBBAD] = TARGET_ELIBBAD,
578 [ELIBSCN] = TARGET_ELIBSCN,
579 [ELIBMAX] = TARGET_ELIBMAX,
580 [ELIBEXEC] = TARGET_ELIBEXEC,
581 [EILSEQ] = TARGET_EILSEQ,
582 [ENOSYS] = TARGET_ENOSYS,
583 [ELOOP] = TARGET_ELOOP,
584 [ERESTART] = TARGET_ERESTART,
585 [ESTRPIPE] = TARGET_ESTRPIPE,
586 [ENOTEMPTY] = TARGET_ENOTEMPTY,
587 [EUSERS] = TARGET_EUSERS,
588 [ENOTSOCK] = TARGET_ENOTSOCK,
589 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
590 [EMSGSIZE] = TARGET_EMSGSIZE,
591 [EPROTOTYPE] = TARGET_EPROTOTYPE,
592 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
593 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
594 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
595 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
596 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
597 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
598 [EADDRINUSE] = TARGET_EADDRINUSE,
599 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
600 [ENETDOWN] = TARGET_ENETDOWN,
601 [ENETUNREACH] = TARGET_ENETUNREACH,
602 [ENETRESET] = TARGET_ENETRESET,
603 [ECONNABORTED] = TARGET_ECONNABORTED,
604 [ECONNRESET] = TARGET_ECONNRESET,
605 [ENOBUFS] = TARGET_ENOBUFS,
606 [EISCONN] = TARGET_EISCONN,
607 [ENOTCONN] = TARGET_ENOTCONN,
608 [EUCLEAN] = TARGET_EUCLEAN,
609 [ENOTNAM] = TARGET_ENOTNAM,
610 [ENAVAIL] = TARGET_ENAVAIL,
611 [EISNAM] = TARGET_EISNAM,
612 [EREMOTEIO] = TARGET_EREMOTEIO,
613 [ESHUTDOWN] = TARGET_ESHUTDOWN,
614 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
615 [ETIMEDOUT] = TARGET_ETIMEDOUT,
616 [ECONNREFUSED] = TARGET_ECONNREFUSED,
617 [EHOSTDOWN] = TARGET_EHOSTDOWN,
618 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
619 [EALREADY] = TARGET_EALREADY,
620 [EINPROGRESS] = TARGET_EINPROGRESS,
621 [ESTALE] = TARGET_ESTALE,
622 [ECANCELED] = TARGET_ECANCELED,
623 [ENOMEDIUM] = TARGET_ENOMEDIUM,
624 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
626 [ENOKEY] = TARGET_ENOKEY,
629 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
632 [EKEYREVOKED] = TARGET_EKEYREVOKED,
635 [EKEYREJECTED] = TARGET_EKEYREJECTED,
638 [EOWNERDEAD] = TARGET_EOWNERDEAD,
640 #ifdef ENOTRECOVERABLE
641 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
645 static inline int host_to_target_errno(int err)
647 if(host_to_target_errno_table[err])
648 return host_to_target_errno_table[err];
652 static inline int target_to_host_errno(int err)
654 if (target_to_host_errno_table[err])
655 return target_to_host_errno_table[err];
659 static inline abi_long get_errno(abi_long ret)
662 return -host_to_target_errno(errno);
667 static inline int is_error(abi_long ret)
669 return (abi_ulong)ret >= (abi_ulong)(-4096);
672 char *target_strerror(int err)
674 return strerror(target_to_host_errno(err));
677 static abi_ulong target_brk;
678 static abi_ulong target_original_brk;
680 void target_set_brk(abi_ulong new_brk)
682 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
685 /* do_brk() must return target values and target errnos. */
686 abi_long do_brk(abi_ulong new_brk)
689 abi_long mapped_addr;
694 if (new_brk < target_original_brk)
697 brk_page = HOST_PAGE_ALIGN(target_brk);
699 /* If the new brk is less than this, set it and we're done... */
700 if (new_brk < brk_page) {
701 target_brk = new_brk;
705 /* We need to allocate more memory after the brk... */
706 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
707 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
708 PROT_READ|PROT_WRITE,
709 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
711 if (!is_error(mapped_addr))
712 target_brk = new_brk;
717 static inline abi_long copy_from_user_fdset(fd_set *fds,
718 abi_ulong target_fds_addr,
722 abi_ulong b, *target_fds;
724 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
725 if (!(target_fds = lock_user(VERIFY_READ,
727 sizeof(abi_ulong) * nw,
729 return -TARGET_EFAULT;
733 for (i = 0; i < nw; i++) {
734 /* grab the abi_ulong */
735 __get_user(b, &target_fds[i]);
736 for (j = 0; j < TARGET_ABI_BITS; j++) {
737 /* check the bit inside the abi_ulong */
744 unlock_user(target_fds, target_fds_addr, 0);
749 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
755 abi_ulong *target_fds;
757 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
758 if (!(target_fds = lock_user(VERIFY_WRITE,
760 sizeof(abi_ulong) * nw,
762 return -TARGET_EFAULT;
765 for (i = 0; i < nw; i++) {
767 for (j = 0; j < TARGET_ABI_BITS; j++) {
768 v |= ((FD_ISSET(k, fds) != 0) << j);
771 __put_user(v, &target_fds[i]);
774 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
779 #if defined(__alpha__)
785 static inline abi_long host_to_target_clock_t(long ticks)
787 #if HOST_HZ == TARGET_HZ
790 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
794 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
795 const struct rusage *rusage)
797 struct target_rusage *target_rusage;
799 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
800 return -TARGET_EFAULT;
801 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
802 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
803 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
804 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
805 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
806 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
807 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
808 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
809 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
810 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
811 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
812 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
813 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
814 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
815 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
816 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
817 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
818 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
819 unlock_user_struct(target_rusage, target_addr, 1);
824 static inline abi_long copy_from_user_timeval(struct timeval *tv,
825 abi_ulong target_tv_addr)
827 struct target_timeval *target_tv;
829 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
830 return -TARGET_EFAULT;
832 __get_user(tv->tv_sec, &target_tv->tv_sec);
833 __get_user(tv->tv_usec, &target_tv->tv_usec);
835 unlock_user_struct(target_tv, target_tv_addr, 0);
840 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
841 const struct timeval *tv)
843 struct target_timeval *target_tv;
845 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
846 return -TARGET_EFAULT;
848 __put_user(tv->tv_sec, &target_tv->tv_sec);
849 __put_user(tv->tv_usec, &target_tv->tv_usec);
851 unlock_user_struct(target_tv, target_tv_addr, 1);
856 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
857 abi_ulong target_mq_attr_addr)
859 struct target_mq_attr *target_mq_attr;
861 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
862 target_mq_attr_addr, 1))
863 return -TARGET_EFAULT;
865 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
866 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
867 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
868 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
870 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
875 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
876 const struct mq_attr *attr)
878 struct target_mq_attr *target_mq_attr;
880 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
881 target_mq_attr_addr, 0))
882 return -TARGET_EFAULT;
884 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
885 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
886 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
887 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
889 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
894 /* do_select() must return target values and target errnos. */
895 static abi_long do_select(int n,
896 abi_ulong rfd_addr, abi_ulong wfd_addr,
897 abi_ulong efd_addr, abi_ulong target_tv_addr)
899 fd_set rfds, wfds, efds;
900 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
901 struct timeval tv, *tv_ptr;
905 if (copy_from_user_fdset(&rfds, rfd_addr, n))
906 return -TARGET_EFAULT;
912 if (copy_from_user_fdset(&wfds, wfd_addr, n))
913 return -TARGET_EFAULT;
919 if (copy_from_user_fdset(&efds, efd_addr, n))
920 return -TARGET_EFAULT;
926 if (target_tv_addr) {
927 if (copy_from_user_timeval(&tv, target_tv_addr))
928 return -TARGET_EFAULT;
934 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
936 if (!is_error(ret)) {
937 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
938 return -TARGET_EFAULT;
939 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
940 return -TARGET_EFAULT;
941 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
942 return -TARGET_EFAULT;
944 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
945 return -TARGET_EFAULT;
951 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
952 abi_ulong target_addr,
955 struct target_ip_mreqn *target_smreqn;
957 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
959 return -TARGET_EFAULT;
960 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
961 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
962 if (len == sizeof(struct target_ip_mreqn))
963 mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
964 unlock_user(target_smreqn, target_addr, 0);
969 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
970 abi_ulong target_addr,
973 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
974 sa_family_t sa_family;
975 struct target_sockaddr *target_saddr;
977 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
979 return -TARGET_EFAULT;
981 sa_family = tswap16(target_saddr->sa_family);
983 /* Oops. The caller might send a incomplete sun_path; sun_path
984 * must be terminated by \0 (see the manual page), but
985 * unfortunately it is quite common to specify sockaddr_un
986 * length as "strlen(x->sun_path)" while it should be
987 * "strlen(...) + 1". We'll fix that here if needed.
988 * Linux kernel has a similar feature.
991 if (sa_family == AF_UNIX) {
992 if (len < unix_maxlen && len > 0) {
993 char *cp = (char*)target_saddr;
995 if ( cp[len-1] && !cp[len] )
998 if (len > unix_maxlen)
1002 memcpy(addr, target_saddr, len);
1003 addr->sa_family = sa_family;
1004 unlock_user(target_saddr, target_addr, 0);
1009 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1010 struct sockaddr *addr,
1013 struct target_sockaddr *target_saddr;
1015 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1017 return -TARGET_EFAULT;
1018 memcpy(target_saddr, addr, len);
1019 target_saddr->sa_family = tswap16(addr->sa_family);
1020 unlock_user(target_saddr, target_addr, len);
1025 /* ??? Should this also swap msgh->name? */
1026 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1027 struct target_msghdr *target_msgh)
1029 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1030 abi_long msg_controllen;
1031 abi_ulong target_cmsg_addr;
1032 struct target_cmsghdr *target_cmsg;
1033 socklen_t space = 0;
1035 msg_controllen = tswapl(target_msgh->msg_controllen);
1036 if (msg_controllen < sizeof (struct target_cmsghdr))
1038 target_cmsg_addr = tswapl(target_msgh->msg_control);
1039 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1041 return -TARGET_EFAULT;
1043 while (cmsg && target_cmsg) {
1044 void *data = CMSG_DATA(cmsg);
1045 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1047 int len = tswapl(target_cmsg->cmsg_len)
1048 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1050 space += CMSG_SPACE(len);
1051 if (space > msgh->msg_controllen) {
1052 space -= CMSG_SPACE(len);
1053 gemu_log("Host cmsg overflow\n");
1057 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1058 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1059 cmsg->cmsg_len = CMSG_LEN(len);
1061 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1062 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1063 memcpy(data, target_data, len);
1065 int *fd = (int *)data;
1066 int *target_fd = (int *)target_data;
1067 int i, numfds = len / sizeof(int);
1069 for (i = 0; i < numfds; i++)
1070 fd[i] = tswap32(target_fd[i]);
1073 cmsg = CMSG_NXTHDR(msgh, cmsg);
1074 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1076 unlock_user(target_cmsg, target_cmsg_addr, 0);
1078 msgh->msg_controllen = space;
1082 /* ??? Should this also swap msgh->name? */
1083 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1084 struct msghdr *msgh)
1086 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1087 abi_long msg_controllen;
1088 abi_ulong target_cmsg_addr;
1089 struct target_cmsghdr *target_cmsg;
1090 socklen_t space = 0;
1092 msg_controllen = tswapl(target_msgh->msg_controllen);
1093 if (msg_controllen < sizeof (struct target_cmsghdr))
1095 target_cmsg_addr = tswapl(target_msgh->msg_control);
1096 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1098 return -TARGET_EFAULT;
1100 while (cmsg && target_cmsg) {
1101 void *data = CMSG_DATA(cmsg);
1102 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1104 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1106 space += TARGET_CMSG_SPACE(len);
1107 if (space > msg_controllen) {
1108 space -= TARGET_CMSG_SPACE(len);
1109 gemu_log("Target cmsg overflow\n");
1113 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1114 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1115 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1117 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1118 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1119 memcpy(target_data, data, len);
1121 int *fd = (int *)data;
1122 int *target_fd = (int *)target_data;
1123 int i, numfds = len / sizeof(int);
1125 for (i = 0; i < numfds; i++)
1126 target_fd[i] = tswap32(fd[i]);
1129 cmsg = CMSG_NXTHDR(msgh, cmsg);
1130 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1132 unlock_user(target_cmsg, target_cmsg_addr, space);
1134 target_msgh->msg_controllen = tswapl(space);
1138 /* do_setsockopt() Must return target values and target errnos. */
1139 static abi_long do_setsockopt(int sockfd, int level, int optname,
1140 abi_ulong optval_addr, socklen_t optlen)
1144 struct ip_mreqn *ip_mreq;
1148 /* TCP options all take an 'int' value. */
1149 if (optlen < sizeof(uint32_t))
1150 return -TARGET_EINVAL;
1152 if (get_user_u32(val, optval_addr))
1153 return -TARGET_EFAULT;
1154 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1161 case IP_ROUTER_ALERT:
1165 case IP_MTU_DISCOVER:
1171 case IP_MULTICAST_TTL:
1172 case IP_MULTICAST_LOOP:
1174 if (optlen >= sizeof(uint32_t)) {
1175 if (get_user_u32(val, optval_addr))
1176 return -TARGET_EFAULT;
1177 } else if (optlen >= 1) {
1178 if (get_user_u8(val, optval_addr))
1179 return -TARGET_EFAULT;
1181 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1183 case IP_ADD_MEMBERSHIP:
1184 case IP_DROP_MEMBERSHIP:
1185 if (optlen < sizeof (struct target_ip_mreq) ||
1186 optlen > sizeof (struct target_ip_mreqn))
1187 return -TARGET_EINVAL;
1189 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1190 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1191 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1198 case TARGET_SOL_SOCKET:
1200 /* Options with 'int' argument. */
1201 case TARGET_SO_DEBUG:
1204 case TARGET_SO_REUSEADDR:
1205 optname = SO_REUSEADDR;
1207 case TARGET_SO_TYPE:
1210 case TARGET_SO_ERROR:
1213 case TARGET_SO_DONTROUTE:
1214 optname = SO_DONTROUTE;
1216 case TARGET_SO_BROADCAST:
1217 optname = SO_BROADCAST;
1219 case TARGET_SO_SNDBUF:
1220 optname = SO_SNDBUF;
1222 case TARGET_SO_RCVBUF:
1223 optname = SO_RCVBUF;
1225 case TARGET_SO_KEEPALIVE:
1226 optname = SO_KEEPALIVE;
1228 case TARGET_SO_OOBINLINE:
1229 optname = SO_OOBINLINE;
1231 case TARGET_SO_NO_CHECK:
1232 optname = SO_NO_CHECK;
1234 case TARGET_SO_PRIORITY:
1235 optname = SO_PRIORITY;
1238 case TARGET_SO_BSDCOMPAT:
1239 optname = SO_BSDCOMPAT;
1242 case TARGET_SO_PASSCRED:
1243 optname = SO_PASSCRED;
1245 case TARGET_SO_TIMESTAMP:
1246 optname = SO_TIMESTAMP;
1248 case TARGET_SO_RCVLOWAT:
1249 optname = SO_RCVLOWAT;
1251 case TARGET_SO_RCVTIMEO:
1252 optname = SO_RCVTIMEO;
1254 case TARGET_SO_SNDTIMEO:
1255 optname = SO_SNDTIMEO;
1261 if (optlen < sizeof(uint32_t))
1262 return -TARGET_EINVAL;
1264 if (get_user_u32(val, optval_addr))
1265 return -TARGET_EFAULT;
1266 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1270 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
1271 ret = -TARGET_ENOPROTOOPT;
1276 /* do_getsockopt() Must return target values and target errnos. */
1277 static abi_long do_getsockopt(int sockfd, int level, int optname,
1278 abi_ulong optval_addr, abi_ulong optlen)
1285 case TARGET_SOL_SOCKET:
1288 case TARGET_SO_LINGER:
1289 case TARGET_SO_RCVTIMEO:
1290 case TARGET_SO_SNDTIMEO:
1291 case TARGET_SO_PEERCRED:
1292 case TARGET_SO_PEERNAME:
1293 /* These don't just return a single integer */
1300 /* TCP options all take an 'int' value. */
1302 if (get_user_u32(len, optlen))
1303 return -TARGET_EFAULT;
1305 return -TARGET_EINVAL;
1307 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1314 if (put_user_u32(val, optval_addr))
1315 return -TARGET_EFAULT;
1317 if (put_user_u8(val, optval_addr))
1318 return -TARGET_EFAULT;
1320 if (put_user_u32(len, optlen))
1321 return -TARGET_EFAULT;
1328 case IP_ROUTER_ALERT:
1332 case IP_MTU_DISCOVER:
1338 case IP_MULTICAST_TTL:
1339 case IP_MULTICAST_LOOP:
1340 if (get_user_u32(len, optlen))
1341 return -TARGET_EFAULT;
1343 return -TARGET_EINVAL;
1345 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1348 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1350 if (put_user_u32(len, optlen)
1351 || put_user_u8(val, optval_addr))
1352 return -TARGET_EFAULT;
1354 if (len > sizeof(int))
1356 if (put_user_u32(len, optlen)
1357 || put_user_u32(val, optval_addr))
1358 return -TARGET_EFAULT;
1362 ret = -TARGET_ENOPROTOOPT;
1368 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1370 ret = -TARGET_EOPNOTSUPP;
1377 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1378 * other lock functions have a return code of 0 for failure.
1380 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1381 int count, int copy)
1383 struct target_iovec *target_vec;
1387 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1389 return -TARGET_EFAULT;
1390 for(i = 0;i < count; i++) {
1391 base = tswapl(target_vec[i].iov_base);
1392 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1393 if (vec[i].iov_len != 0) {
1394 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1395 /* Don't check lock_user return value. We must call writev even
1396 if a element has invalid base address. */
1398 /* zero length pointer is ignored */
1399 vec[i].iov_base = NULL;
1402 unlock_user (target_vec, target_addr, 0);
1406 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1407 int count, int copy)
1409 struct target_iovec *target_vec;
1413 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1415 return -TARGET_EFAULT;
1416 for(i = 0;i < count; i++) {
1417 if (target_vec[i].iov_base) {
1418 base = tswapl(target_vec[i].iov_base);
1419 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1422 unlock_user (target_vec, target_addr, 0);
1427 /* do_socket() Must return target values and target errnos. */
1428 static abi_long do_socket(int domain, int type, int protocol)
1430 #if defined(TARGET_MIPS)
1432 case TARGET_SOCK_DGRAM:
1435 case TARGET_SOCK_STREAM:
1438 case TARGET_SOCK_RAW:
1441 case TARGET_SOCK_RDM:
1444 case TARGET_SOCK_SEQPACKET:
1445 type = SOCK_SEQPACKET;
1447 case TARGET_SOCK_PACKET:
1452 if (domain == PF_NETLINK)
1453 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1454 return get_errno(socket(domain, type, protocol));
1457 /* do_bind() Must return target values and target errnos. */
1458 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1464 return -TARGET_EINVAL;
1466 addr = alloca(addrlen+1);
1468 target_to_host_sockaddr(addr, target_addr, addrlen);
1469 return get_errno(bind(sockfd, addr, addrlen));
1472 /* do_connect() Must return target values and target errnos. */
1473 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1479 return -TARGET_EINVAL;
1481 addr = alloca(addrlen);
1483 target_to_host_sockaddr(addr, target_addr, addrlen);
1484 return get_errno(connect(sockfd, addr, addrlen));
1487 /* do_sendrecvmsg() Must return target values and target errnos. */
1488 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1489 int flags, int send)
1492 struct target_msghdr *msgp;
1496 abi_ulong target_vec;
1499 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1503 return -TARGET_EFAULT;
1504 if (msgp->msg_name) {
1505 msg.msg_namelen = tswap32(msgp->msg_namelen);
1506 msg.msg_name = alloca(msg.msg_namelen);
1507 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1510 msg.msg_name = NULL;
1511 msg.msg_namelen = 0;
1513 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1514 msg.msg_control = alloca(msg.msg_controllen);
1515 msg.msg_flags = tswap32(msgp->msg_flags);
1517 count = tswapl(msgp->msg_iovlen);
1518 vec = alloca(count * sizeof(struct iovec));
1519 target_vec = tswapl(msgp->msg_iov);
1520 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1521 msg.msg_iovlen = count;
1525 ret = target_to_host_cmsg(&msg, msgp);
1527 ret = get_errno(sendmsg(fd, &msg, flags));
1529 ret = get_errno(recvmsg(fd, &msg, flags));
1530 if (!is_error(ret)) {
1532 ret = host_to_target_cmsg(msgp, &msg);
1537 unlock_iovec(vec, target_vec, count, !send);
1538 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1542 /* do_accept() Must return target values and target errnos. */
1543 static abi_long do_accept(int fd, abi_ulong target_addr,
1544 abi_ulong target_addrlen_addr)
1550 if (target_addr == 0)
1551 return get_errno(accept(fd, NULL, NULL));
1553 if (get_user_u32(addrlen, target_addrlen_addr))
1554 return -TARGET_EFAULT;
1557 return -TARGET_EINVAL;
1559 addr = alloca(addrlen);
1561 ret = get_errno(accept(fd, addr, &addrlen));
1562 if (!is_error(ret)) {
1563 host_to_target_sockaddr(target_addr, addr, addrlen);
1564 if (put_user_u32(addrlen, target_addrlen_addr))
1565 ret = -TARGET_EFAULT;
1570 /* do_getpeername() Must return target values and target errnos. */
1571 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1572 abi_ulong target_addrlen_addr)
1578 if (get_user_u32(addrlen, target_addrlen_addr))
1579 return -TARGET_EFAULT;
1582 return -TARGET_EINVAL;
1584 addr = alloca(addrlen);
1586 ret = get_errno(getpeername(fd, addr, &addrlen));
1587 if (!is_error(ret)) {
1588 host_to_target_sockaddr(target_addr, addr, addrlen);
1589 if (put_user_u32(addrlen, target_addrlen_addr))
1590 ret = -TARGET_EFAULT;
1595 /* do_getsockname() Must return target values and target errnos. */
1596 static abi_long do_getsockname(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 if (get_user_u32(addrlen, target_addrlen_addr))
1607 return -TARGET_EFAULT;
1610 return -TARGET_EINVAL;
1612 addr = alloca(addrlen);
1614 ret = get_errno(getsockname(fd, addr, &addrlen));
1615 if (!is_error(ret)) {
1616 host_to_target_sockaddr(target_addr, addr, addrlen);
1617 if (put_user_u32(addrlen, target_addrlen_addr))
1618 ret = -TARGET_EFAULT;
1623 /* do_socketpair() Must return target values and target errnos. */
1624 static abi_long do_socketpair(int domain, int type, int protocol,
1625 abi_ulong target_tab_addr)
1630 ret = get_errno(socketpair(domain, type, protocol, tab));
1631 if (!is_error(ret)) {
1632 if (put_user_s32(tab[0], target_tab_addr)
1633 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1634 ret = -TARGET_EFAULT;
1639 /* do_sendto() Must return target values and target errnos. */
1640 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1641 abi_ulong target_addr, socklen_t addrlen)
1648 return -TARGET_EINVAL;
1650 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1652 return -TARGET_EFAULT;
1654 addr = alloca(addrlen);
1655 target_to_host_sockaddr(addr, target_addr, addrlen);
1656 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1658 ret = get_errno(send(fd, host_msg, len, flags));
1660 unlock_user(host_msg, msg, 0);
1664 /* do_recvfrom() Must return target values and target errnos. */
1665 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1666 abi_ulong target_addr,
1667 abi_ulong target_addrlen)
1674 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1676 return -TARGET_EFAULT;
1678 if (get_user_u32(addrlen, target_addrlen)) {
1679 ret = -TARGET_EFAULT;
1683 ret = -TARGET_EINVAL;
1686 addr = alloca(addrlen);
1687 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1689 addr = NULL; /* To keep compiler quiet. */
1690 ret = get_errno(recv(fd, host_msg, len, flags));
1692 if (!is_error(ret)) {
1694 host_to_target_sockaddr(target_addr, addr, addrlen);
1695 if (put_user_u32(addrlen, target_addrlen)) {
1696 ret = -TARGET_EFAULT;
1700 unlock_user(host_msg, msg, len);
1703 unlock_user(host_msg, msg, 0);
1708 #ifdef TARGET_NR_socketcall
1709 /* do_socketcall() Must return target values and target errnos. */
1710 static abi_long do_socketcall(int num, abi_ulong vptr)
1713 const int n = sizeof(abi_ulong);
1718 int domain, type, protocol;
1720 if (get_user_s32(domain, vptr)
1721 || get_user_s32(type, vptr + n)
1722 || get_user_s32(protocol, vptr + 2 * n))
1723 return -TARGET_EFAULT;
1725 ret = do_socket(domain, type, protocol);
1731 abi_ulong target_addr;
1734 if (get_user_s32(sockfd, vptr)
1735 || get_user_ual(target_addr, vptr + n)
1736 || get_user_u32(addrlen, vptr + 2 * n))
1737 return -TARGET_EFAULT;
1739 ret = do_bind(sockfd, target_addr, addrlen);
1742 case SOCKOP_connect:
1745 abi_ulong target_addr;
1748 if (get_user_s32(sockfd, vptr)
1749 || get_user_ual(target_addr, vptr + n)
1750 || get_user_u32(addrlen, vptr + 2 * n))
1751 return -TARGET_EFAULT;
1753 ret = do_connect(sockfd, target_addr, addrlen);
1758 int sockfd, backlog;
1760 if (get_user_s32(sockfd, vptr)
1761 || get_user_s32(backlog, vptr + n))
1762 return -TARGET_EFAULT;
1764 ret = get_errno(listen(sockfd, backlog));
1770 abi_ulong target_addr, target_addrlen;
1772 if (get_user_s32(sockfd, vptr)
1773 || get_user_ual(target_addr, vptr + n)
1774 || get_user_u32(target_addrlen, vptr + 2 * n))
1775 return -TARGET_EFAULT;
1777 ret = do_accept(sockfd, target_addr, target_addrlen);
1780 case SOCKOP_getsockname:
1783 abi_ulong target_addr, target_addrlen;
1785 if (get_user_s32(sockfd, vptr)
1786 || get_user_ual(target_addr, vptr + n)
1787 || get_user_u32(target_addrlen, vptr + 2 * n))
1788 return -TARGET_EFAULT;
1790 ret = do_getsockname(sockfd, target_addr, target_addrlen);
1793 case SOCKOP_getpeername:
1796 abi_ulong target_addr, target_addrlen;
1798 if (get_user_s32(sockfd, vptr)
1799 || get_user_ual(target_addr, vptr + n)
1800 || get_user_u32(target_addrlen, vptr + 2 * n))
1801 return -TARGET_EFAULT;
1803 ret = do_getpeername(sockfd, target_addr, target_addrlen);
1806 case SOCKOP_socketpair:
1808 int domain, type, protocol;
1811 if (get_user_s32(domain, vptr)
1812 || get_user_s32(type, vptr + n)
1813 || get_user_s32(protocol, vptr + 2 * n)
1814 || get_user_ual(tab, vptr + 3 * n))
1815 return -TARGET_EFAULT;
1817 ret = do_socketpair(domain, type, protocol, tab);
1827 if (get_user_s32(sockfd, vptr)
1828 || get_user_ual(msg, vptr + n)
1829 || get_user_ual(len, vptr + 2 * n)
1830 || get_user_s32(flags, vptr + 3 * n))
1831 return -TARGET_EFAULT;
1833 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1843 if (get_user_s32(sockfd, vptr)
1844 || get_user_ual(msg, vptr + n)
1845 || get_user_ual(len, vptr + 2 * n)
1846 || get_user_s32(flags, vptr + 3 * n))
1847 return -TARGET_EFAULT;
1849 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1861 if (get_user_s32(sockfd, vptr)
1862 || get_user_ual(msg, vptr + n)
1863 || get_user_ual(len, vptr + 2 * n)
1864 || get_user_s32(flags, vptr + 3 * n)
1865 || get_user_ual(addr, vptr + 4 * n)
1866 || get_user_u32(addrlen, vptr + 5 * n))
1867 return -TARGET_EFAULT;
1869 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1872 case SOCKOP_recvfrom:
1881 if (get_user_s32(sockfd, vptr)
1882 || get_user_ual(msg, vptr + n)
1883 || get_user_ual(len, vptr + 2 * n)
1884 || get_user_s32(flags, vptr + 3 * n)
1885 || get_user_ual(addr, vptr + 4 * n)
1886 || get_user_u32(addrlen, vptr + 5 * n))
1887 return -TARGET_EFAULT;
1889 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1892 case SOCKOP_shutdown:
1896 if (get_user_s32(sockfd, vptr)
1897 || get_user_s32(how, vptr + n))
1898 return -TARGET_EFAULT;
1900 ret = get_errno(shutdown(sockfd, how));
1903 case SOCKOP_sendmsg:
1904 case SOCKOP_recvmsg:
1907 abi_ulong target_msg;
1910 if (get_user_s32(fd, vptr)
1911 || get_user_ual(target_msg, vptr + n)
1912 || get_user_s32(flags, vptr + 2 * n))
1913 return -TARGET_EFAULT;
1915 ret = do_sendrecvmsg(fd, target_msg, flags,
1916 (num == SOCKOP_sendmsg));
1919 case SOCKOP_setsockopt:
1927 if (get_user_s32(sockfd, vptr)
1928 || get_user_s32(level, vptr + n)
1929 || get_user_s32(optname, vptr + 2 * n)
1930 || get_user_ual(optval, vptr + 3 * n)
1931 || get_user_u32(optlen, vptr + 4 * n))
1932 return -TARGET_EFAULT;
1934 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1937 case SOCKOP_getsockopt:
1945 if (get_user_s32(sockfd, vptr)
1946 || get_user_s32(level, vptr + n)
1947 || get_user_s32(optname, vptr + 2 * n)
1948 || get_user_ual(optval, vptr + 3 * n)
1949 || get_user_u32(optlen, vptr + 4 * n))
1950 return -TARGET_EFAULT;
1952 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
1956 gemu_log("Unsupported socketcall: %d\n", num);
1957 ret = -TARGET_ENOSYS;
1964 #define N_SHM_REGIONS 32
1966 static struct shm_region {
1969 } shm_regions[N_SHM_REGIONS];
1971 struct target_ipc_perm
1978 unsigned short int mode;
1979 unsigned short int __pad1;
1980 unsigned short int __seq;
1981 unsigned short int __pad2;
1982 abi_ulong __unused1;
1983 abi_ulong __unused2;
1986 struct target_semid_ds
1988 struct target_ipc_perm sem_perm;
1989 abi_ulong sem_otime;
1990 abi_ulong __unused1;
1991 abi_ulong sem_ctime;
1992 abi_ulong __unused2;
1993 abi_ulong sem_nsems;
1994 abi_ulong __unused3;
1995 abi_ulong __unused4;
1998 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1999 abi_ulong target_addr)
2001 struct target_ipc_perm *target_ip;
2002 struct target_semid_ds *target_sd;
2004 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2005 return -TARGET_EFAULT;
2006 target_ip=&(target_sd->sem_perm);
2007 host_ip->__key = tswapl(target_ip->__key);
2008 host_ip->uid = tswapl(target_ip->uid);
2009 host_ip->gid = tswapl(target_ip->gid);
2010 host_ip->cuid = tswapl(target_ip->cuid);
2011 host_ip->cgid = tswapl(target_ip->cgid);
2012 host_ip->mode = tswapl(target_ip->mode);
2013 unlock_user_struct(target_sd, target_addr, 0);
2017 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2018 struct ipc_perm *host_ip)
2020 struct target_ipc_perm *target_ip;
2021 struct target_semid_ds *target_sd;
2023 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2024 return -TARGET_EFAULT;
2025 target_ip = &(target_sd->sem_perm);
2026 target_ip->__key = tswapl(host_ip->__key);
2027 target_ip->uid = tswapl(host_ip->uid);
2028 target_ip->gid = tswapl(host_ip->gid);
2029 target_ip->cuid = tswapl(host_ip->cuid);
2030 target_ip->cgid = tswapl(host_ip->cgid);
2031 target_ip->mode = tswapl(host_ip->mode);
2032 unlock_user_struct(target_sd, target_addr, 1);
2036 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2037 abi_ulong target_addr)
2039 struct target_semid_ds *target_sd;
2041 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2042 return -TARGET_EFAULT;
2043 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2044 return -TARGET_EFAULT;
2045 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2046 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2047 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2048 unlock_user_struct(target_sd, target_addr, 0);
2052 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2053 struct semid_ds *host_sd)
2055 struct target_semid_ds *target_sd;
2057 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2058 return -TARGET_EFAULT;
2059 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2060 return -TARGET_EFAULT;;
2061 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2062 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2063 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2064 unlock_user_struct(target_sd, target_addr, 1);
2068 struct target_seminfo {
2081 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2082 struct seminfo *host_seminfo)
2084 struct target_seminfo *target_seminfo;
2085 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2086 return -TARGET_EFAULT;
2087 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2088 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2089 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2090 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2091 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2092 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2093 __put_user(host_seminfo->semume, &target_seminfo->semume);
2094 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2095 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2096 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2097 unlock_user_struct(target_seminfo, target_addr, 1);
2103 struct semid_ds *buf;
2104 unsigned short *array;
2105 struct seminfo *__buf;
2108 union target_semun {
2115 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2116 abi_ulong target_addr)
2119 unsigned short *array;
2121 struct semid_ds semid_ds;
2124 semun.buf = &semid_ds;
2126 ret = semctl(semid, 0, IPC_STAT, semun);
2128 return get_errno(ret);
2130 nsems = semid_ds.sem_nsems;
2132 *host_array = malloc(nsems*sizeof(unsigned short));
2133 array = lock_user(VERIFY_READ, target_addr,
2134 nsems*sizeof(unsigned short), 1);
2136 return -TARGET_EFAULT;
2138 for(i=0; i<nsems; i++) {
2139 __get_user((*host_array)[i], &array[i]);
2141 unlock_user(array, target_addr, 0);
2146 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2147 unsigned short **host_array)
2150 unsigned short *array;
2152 struct semid_ds semid_ds;
2155 semun.buf = &semid_ds;
2157 ret = semctl(semid, 0, IPC_STAT, semun);
2159 return get_errno(ret);
2161 nsems = semid_ds.sem_nsems;
2163 array = lock_user(VERIFY_WRITE, target_addr,
2164 nsems*sizeof(unsigned short), 0);
2166 return -TARGET_EFAULT;
2168 for(i=0; i<nsems; i++) {
2169 __put_user((*host_array)[i], &array[i]);
2172 unlock_user(array, target_addr, 1);
2177 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2178 union target_semun target_su)
2181 struct semid_ds dsarg;
2182 unsigned short *array;
2183 struct seminfo seminfo;
2184 abi_long ret = -TARGET_EINVAL;
2191 arg.val = tswapl(target_su.val);
2192 ret = get_errno(semctl(semid, semnum, cmd, arg));
2193 target_su.val = tswapl(arg.val);
2197 err = target_to_host_semarray(semid, &array, target_su.array);
2201 ret = get_errno(semctl(semid, semnum, cmd, arg));
2202 err = host_to_target_semarray(semid, target_su.array, &array);
2209 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2213 ret = get_errno(semctl(semid, semnum, cmd, arg));
2214 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2220 arg.__buf = &seminfo;
2221 ret = get_errno(semctl(semid, semnum, cmd, arg));
2222 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2230 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2237 struct target_sembuf {
2238 unsigned short sem_num;
2243 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2244 abi_ulong target_addr,
2247 struct target_sembuf *target_sembuf;
2250 target_sembuf = lock_user(VERIFY_READ, target_addr,
2251 nsops*sizeof(struct target_sembuf), 1);
2253 return -TARGET_EFAULT;
2255 for(i=0; i<nsops; i++) {
2256 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2257 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2258 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2261 unlock_user(target_sembuf, target_addr, 0);
2266 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2268 struct sembuf sops[nsops];
2270 if (target_to_host_sembuf(sops, ptr, nsops))
2271 return -TARGET_EFAULT;
2273 return semop(semid, sops, nsops);
2276 struct target_msqid_ds
2278 struct target_ipc_perm msg_perm;
2279 abi_ulong msg_stime;
2280 #if TARGET_ABI_BITS == 32
2281 abi_ulong __unused1;
2283 abi_ulong msg_rtime;
2284 #if TARGET_ABI_BITS == 32
2285 abi_ulong __unused2;
2287 abi_ulong msg_ctime;
2288 #if TARGET_ABI_BITS == 32
2289 abi_ulong __unused3;
2291 abi_ulong __msg_cbytes;
2293 abi_ulong msg_qbytes;
2294 abi_ulong msg_lspid;
2295 abi_ulong msg_lrpid;
2296 abi_ulong __unused4;
2297 abi_ulong __unused5;
2300 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2301 abi_ulong target_addr)
2303 struct target_msqid_ds *target_md;
2305 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2306 return -TARGET_EFAULT;
2307 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2308 return -TARGET_EFAULT;
2309 host_md->msg_stime = tswapl(target_md->msg_stime);
2310 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2311 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2312 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2313 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2314 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2315 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2316 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2317 unlock_user_struct(target_md, target_addr, 0);
2321 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2322 struct msqid_ds *host_md)
2324 struct target_msqid_ds *target_md;
2326 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2327 return -TARGET_EFAULT;
2328 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2329 return -TARGET_EFAULT;
2330 target_md->msg_stime = tswapl(host_md->msg_stime);
2331 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2332 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2333 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2334 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2335 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2336 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2337 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2338 unlock_user_struct(target_md, target_addr, 1);
2342 struct target_msginfo {
2350 unsigned short int msgseg;
2353 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2354 struct msginfo *host_msginfo)
2356 struct target_msginfo *target_msginfo;
2357 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2358 return -TARGET_EFAULT;
2359 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2360 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2361 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2362 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2363 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2364 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2365 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2366 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2367 unlock_user_struct(target_msginfo, target_addr, 1);
2371 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
2373 struct msqid_ds dsarg;
2374 struct msginfo msginfo;
2375 abi_long ret = -TARGET_EINVAL;
2383 if (target_to_host_msqid_ds(&dsarg,ptr))
2384 return -TARGET_EFAULT;
2385 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2386 if (host_to_target_msqid_ds(ptr,&dsarg))
2387 return -TARGET_EFAULT;
2390 ret = get_errno(msgctl(msgid, cmd, NULL));
2394 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2395 if (host_to_target_msginfo(ptr, &msginfo))
2396 return -TARGET_EFAULT;
2403 struct target_msgbuf {
2408 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2409 unsigned int msgsz, int msgflg)
2411 struct target_msgbuf *target_mb;
2412 struct msgbuf *host_mb;
2415 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2416 return -TARGET_EFAULT;
2417 host_mb = malloc(msgsz+sizeof(long));
2418 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2419 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
2420 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2422 unlock_user_struct(target_mb, msgp, 0);
2427 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2428 unsigned int msgsz, abi_long msgtyp,
2431 struct target_msgbuf *target_mb;
2433 struct msgbuf *host_mb;
2436 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2437 return -TARGET_EFAULT;
2439 host_mb = malloc(msgsz+sizeof(long));
2440 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2443 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2444 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2445 if (!target_mtext) {
2446 ret = -TARGET_EFAULT;
2449 memcpy(target_mb->mtext, host_mb->mtext, ret);
2450 unlock_user(target_mtext, target_mtext_addr, ret);
2453 target_mb->mtype = tswapl(host_mb->mtype);
2458 unlock_user_struct(target_mb, msgp, 1);
2462 struct target_shmid_ds
2464 struct target_ipc_perm shm_perm;
2465 abi_ulong shm_segsz;
2466 abi_ulong shm_atime;
2467 #if TARGET_ABI_BITS == 32
2468 abi_ulong __unused1;
2470 abi_ulong shm_dtime;
2471 #if TARGET_ABI_BITS == 32
2472 abi_ulong __unused2;
2474 abi_ulong shm_ctime;
2475 #if TARGET_ABI_BITS == 32
2476 abi_ulong __unused3;
2480 abi_ulong shm_nattch;
2481 unsigned long int __unused4;
2482 unsigned long int __unused5;
2485 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2486 abi_ulong target_addr)
2488 struct target_shmid_ds *target_sd;
2490 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2491 return -TARGET_EFAULT;
2492 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2493 return -TARGET_EFAULT;
2494 __put_user(target_sd->shm_segsz, &host_sd->shm_segsz);
2495 __put_user(target_sd->shm_atime, &host_sd->shm_atime);
2496 __put_user(target_sd->shm_dtime, &host_sd->shm_dtime);
2497 __put_user(target_sd->shm_ctime, &host_sd->shm_ctime);
2498 __put_user(target_sd->shm_cpid, &host_sd->shm_cpid);
2499 __put_user(target_sd->shm_lpid, &host_sd->shm_lpid);
2500 __put_user(target_sd->shm_nattch, &host_sd->shm_nattch);
2501 unlock_user_struct(target_sd, target_addr, 0);
2505 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2506 struct shmid_ds *host_sd)
2508 struct target_shmid_ds *target_sd;
2510 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2511 return -TARGET_EFAULT;
2512 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2513 return -TARGET_EFAULT;
2514 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2515 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2516 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2517 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2518 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2519 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2520 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2521 unlock_user_struct(target_sd, target_addr, 1);
2525 struct target_shminfo {
2533 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2534 struct shminfo *host_shminfo)
2536 struct target_shminfo *target_shminfo;
2537 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2538 return -TARGET_EFAULT;
2539 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2540 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2541 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2542 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2543 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2544 unlock_user_struct(target_shminfo, target_addr, 1);
2548 struct target_shm_info {
2553 abi_ulong swap_attempts;
2554 abi_ulong swap_successes;
2557 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2558 struct shm_info *host_shm_info)
2560 struct target_shm_info *target_shm_info;
2561 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2562 return -TARGET_EFAULT;
2563 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2564 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2565 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2566 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2567 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2568 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2569 unlock_user_struct(target_shm_info, target_addr, 1);
2573 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2575 struct shmid_ds dsarg;
2576 struct shminfo shminfo;
2577 struct shm_info shm_info;
2578 abi_long ret = -TARGET_EINVAL;
2586 if (target_to_host_shmid_ds(&dsarg, buf))
2587 return -TARGET_EFAULT;
2588 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2589 if (host_to_target_shmid_ds(buf, &dsarg))
2590 return -TARGET_EFAULT;
2593 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2594 if (host_to_target_shminfo(buf, &shminfo))
2595 return -TARGET_EFAULT;
2598 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2599 if (host_to_target_shm_info(buf, &shm_info))
2600 return -TARGET_EFAULT;
2605 ret = get_errno(shmctl(shmid, cmd, NULL));
2612 static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2616 struct shmid_ds shm_info;
2619 /* find out the length of the shared memory segment */
2620 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2621 if (is_error(ret)) {
2622 /* can't get length, bail out */
2629 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2631 abi_ulong mmap_start;
2633 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2635 if (mmap_start == -1) {
2637 host_raddr = (void *)-1;
2639 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2642 if (host_raddr == (void *)-1) {
2644 return get_errno((long)host_raddr);
2646 raddr=h2g((unsigned long)host_raddr);
2648 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2649 PAGE_VALID | PAGE_READ |
2650 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2652 for (i = 0; i < N_SHM_REGIONS; i++) {
2653 if (shm_regions[i].start == 0) {
2654 shm_regions[i].start = raddr;
2655 shm_regions[i].size = shm_info.shm_segsz;
2665 static inline abi_long do_shmdt(abi_ulong shmaddr)
2669 for (i = 0; i < N_SHM_REGIONS; ++i) {
2670 if (shm_regions[i].start == shmaddr) {
2671 shm_regions[i].start = 0;
2672 page_set_flags(shmaddr, shm_regions[i].size, 0);
2677 return get_errno(shmdt(g2h(shmaddr)));
2680 #ifdef TARGET_NR_ipc
2681 /* ??? This only works with linear mappings. */
2682 /* do_ipc() must return target values and target errnos. */
2683 static abi_long do_ipc(unsigned int call, int first,
2684 int second, int third,
2685 abi_long ptr, abi_long fifth)
2690 version = call >> 16;
2695 ret = do_semop(first, ptr, second);
2699 ret = get_errno(semget(first, second, third));
2703 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
2707 ret = get_errno(msgget(first, second));
2711 ret = do_msgsnd(first, ptr, second, third);
2715 ret = do_msgctl(first, second, ptr);
2722 struct target_ipc_kludge {
2727 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2728 ret = -TARGET_EFAULT;
2732 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2734 unlock_user_struct(tmp, ptr, 0);
2738 ret = do_msgrcv(first, ptr, second, fifth, third);
2747 raddr = do_shmat(first, ptr, second);
2748 if (is_error(raddr))
2749 return get_errno(raddr);
2750 if (put_user_ual(raddr, third))
2751 return -TARGET_EFAULT;
2755 ret = -TARGET_EINVAL;
2760 ret = do_shmdt(ptr);
2764 /* IPC_* flag values are the same on all linux platforms */
2765 ret = get_errno(shmget(first, second, third));
2768 /* IPC_* and SHM_* command values are the same on all linux platforms */
2770 ret = do_shmctl(first, second, third);
2773 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2774 ret = -TARGET_ENOSYS;
2781 /* kernel structure types definitions */
2784 #define STRUCT(name, list...) STRUCT_ ## name,
2785 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2787 #include "syscall_types.h"
2790 #undef STRUCT_SPECIAL
2792 #define STRUCT(name, list...) static const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2793 #define STRUCT_SPECIAL(name)
2794 #include "syscall_types.h"
2796 #undef STRUCT_SPECIAL
2798 typedef struct IOCTLEntry {
2799 unsigned int target_cmd;
2800 unsigned int host_cmd;
2803 const argtype arg_type[5];
2806 #define IOC_R 0x0001
2807 #define IOC_W 0x0002
2808 #define IOC_RW (IOC_R | IOC_W)
2810 #define MAX_STRUCT_SIZE 4096
2812 static IOCTLEntry ioctl_entries[] = {
2813 #define IOCTL(cmd, access, types...) \
2814 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2819 /* ??? Implement proper locking for ioctls. */
2820 /* do_ioctl() Must return target values and target errnos. */
2821 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
2823 const IOCTLEntry *ie;
2824 const argtype *arg_type;
2826 uint8_t buf_temp[MAX_STRUCT_SIZE];
2832 if (ie->target_cmd == 0) {
2833 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
2834 return -TARGET_ENOSYS;
2836 if (ie->target_cmd == cmd)
2840 arg_type = ie->arg_type;
2842 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2844 switch(arg_type[0]) {
2847 ret = get_errno(ioctl(fd, ie->host_cmd));
2852 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2856 target_size = thunk_type_size(arg_type, 0);
2857 switch(ie->access) {
2859 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2860 if (!is_error(ret)) {
2861 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2863 return -TARGET_EFAULT;
2864 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2865 unlock_user(argptr, arg, target_size);
2869 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2871 return -TARGET_EFAULT;
2872 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2873 unlock_user(argptr, arg, 0);
2874 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2878 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2880 return -TARGET_EFAULT;
2881 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2882 unlock_user(argptr, arg, 0);
2883 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2884 if (!is_error(ret)) {
2885 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2887 return -TARGET_EFAULT;
2888 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2889 unlock_user(argptr, arg, target_size);
2895 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2896 (long)cmd, arg_type[0]);
2897 ret = -TARGET_ENOSYS;
2903 static const bitmask_transtbl iflag_tbl[] = {
2904 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2905 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2906 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2907 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2908 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2909 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2910 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2911 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2912 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2913 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2914 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2915 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2916 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2917 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2921 static const bitmask_transtbl oflag_tbl[] = {
2922 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2923 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2924 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2925 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2926 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2927 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2928 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2929 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2930 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2931 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2932 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2933 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2934 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2935 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2936 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2937 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2938 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2939 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2940 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2941 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2942 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2943 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2944 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2945 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2949 static const bitmask_transtbl cflag_tbl[] = {
2950 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2951 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2952 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2953 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2954 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2955 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2956 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2957 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2958 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2959 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2960 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2961 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2962 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2963 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2964 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2965 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2966 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2967 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2968 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2969 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2970 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2971 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2972 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2973 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2974 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2975 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2976 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2977 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2978 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2979 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2980 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2984 static const bitmask_transtbl lflag_tbl[] = {
2985 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2986 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2987 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2988 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2989 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2990 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2991 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2992 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2993 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2994 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2995 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2996 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2997 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2998 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2999 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3003 static void target_to_host_termios (void *dst, const void *src)
3005 struct host_termios *host = dst;
3006 const struct target_termios *target = src;
3009 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
3011 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
3013 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
3015 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3016 host->c_line = target->c_line;
3018 memset(host->c_cc, 0, sizeof(host->c_cc));
3019 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3020 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3021 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
3022 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3023 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
3024 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3025 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
3026 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3027 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
3028 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3029 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3030 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3031 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3032 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3033 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3034 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
3035 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
3038 static void host_to_target_termios (void *dst, const void *src)
3040 struct target_termios *target = dst;
3041 const struct host_termios *host = src;
3044 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
3046 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
3048 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
3050 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3051 target->c_line = host->c_line;
3053 memset(target->c_cc, 0, sizeof(target->c_cc));
3054 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3055 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3056 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3057 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3058 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3059 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3060 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3061 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3062 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3063 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3064 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3065 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3066 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3067 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3068 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3069 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3070 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3073 static const StructEntry struct_termios_def = {
3074 .convert = { host_to_target_termios, target_to_host_termios },
3075 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3076 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3079 static bitmask_transtbl mmap_flags_tbl[] = {
3080 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3081 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3082 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3083 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3084 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3085 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3086 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3087 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3091 #if defined(TARGET_I386)
3093 /* NOTE: there is really one LDT for all the threads */
3094 static uint8_t *ldt_table;
3096 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
3103 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3104 if (size > bytecount)
3106 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3108 return -TARGET_EFAULT;
3109 /* ??? Should this by byteswapped? */
3110 memcpy(p, ldt_table, size);
3111 unlock_user(p, ptr, size);
3115 /* XXX: add locking support */
3116 static abi_long write_ldt(CPUX86State *env,
3117 abi_ulong ptr, unsigned long bytecount, int oldmode)
3119 struct target_modify_ldt_ldt_s ldt_info;
3120 struct target_modify_ldt_ldt_s *target_ldt_info;
3121 int seg_32bit, contents, read_exec_only, limit_in_pages;
3122 int seg_not_present, useable, lm;
3123 uint32_t *lp, entry_1, entry_2;
3125 if (bytecount != sizeof(ldt_info))
3126 return -TARGET_EINVAL;
3127 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
3128 return -TARGET_EFAULT;
3129 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3130 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3131 ldt_info.limit = tswap32(target_ldt_info->limit);
3132 ldt_info.flags = tswap32(target_ldt_info->flags);
3133 unlock_user_struct(target_ldt_info, ptr, 0);
3135 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
3136 return -TARGET_EINVAL;
3137 seg_32bit = ldt_info.flags & 1;
3138 contents = (ldt_info.flags >> 1) & 3;
3139 read_exec_only = (ldt_info.flags >> 3) & 1;
3140 limit_in_pages = (ldt_info.flags >> 4) & 1;
3141 seg_not_present = (ldt_info.flags >> 5) & 1;
3142 useable = (ldt_info.flags >> 6) & 1;
3146 lm = (ldt_info.flags >> 7) & 1;
3148 if (contents == 3) {
3150 return -TARGET_EINVAL;
3151 if (seg_not_present == 0)
3152 return -TARGET_EINVAL;
3154 /* allocate the LDT */
3156 env->ldt.base = target_mmap(0,
3157 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3158 PROT_READ|PROT_WRITE,
3159 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3160 if (env->ldt.base == -1)
3161 return -TARGET_ENOMEM;
3162 memset(g2h(env->ldt.base), 0,
3163 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
3164 env->ldt.limit = 0xffff;
3165 ldt_table = g2h(env->ldt.base);
3168 /* NOTE: same code as Linux kernel */
3169 /* Allow LDTs to be cleared by the user. */
3170 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3173 read_exec_only == 1 &&
3175 limit_in_pages == 0 &&
3176 seg_not_present == 1 &&
3184 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3185 (ldt_info.limit & 0x0ffff);
3186 entry_2 = (ldt_info.base_addr & 0xff000000) |
3187 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3188 (ldt_info.limit & 0xf0000) |
3189 ((read_exec_only ^ 1) << 9) |
3191 ((seg_not_present ^ 1) << 15) |
3193 (limit_in_pages << 23) |
3197 entry_2 |= (useable << 20);
3199 /* Install the new entry ... */
3201 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3202 lp[0] = tswap32(entry_1);
3203 lp[1] = tswap32(entry_2);
3207 /* specific and weird i386 syscalls */
3208 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3209 unsigned long bytecount)
3215 ret = read_ldt(ptr, bytecount);
3218 ret = write_ldt(env, ptr, bytecount, 1);
3221 ret = write_ldt(env, ptr, bytecount, 0);
3224 ret = -TARGET_ENOSYS;
3230 #if defined(TARGET_I386) && defined(TARGET_ABI32)
3231 static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
3233 uint64_t *gdt_table = g2h(env->gdt.base);
3234 struct target_modify_ldt_ldt_s ldt_info;
3235 struct target_modify_ldt_ldt_s *target_ldt_info;
3236 int seg_32bit, contents, read_exec_only, limit_in_pages;
3237 int seg_not_present, useable, lm;
3238 uint32_t *lp, entry_1, entry_2;
3241 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3242 if (!target_ldt_info)
3243 return -TARGET_EFAULT;
3244 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3245 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3246 ldt_info.limit = tswap32(target_ldt_info->limit);
3247 ldt_info.flags = tswap32(target_ldt_info->flags);
3248 if (ldt_info.entry_number == -1) {
3249 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3250 if (gdt_table[i] == 0) {
3251 ldt_info.entry_number = i;
3252 target_ldt_info->entry_number = tswap32(i);
3257 unlock_user_struct(target_ldt_info, ptr, 1);
3259 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3260 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3261 return -TARGET_EINVAL;
3262 seg_32bit = ldt_info.flags & 1;
3263 contents = (ldt_info.flags >> 1) & 3;
3264 read_exec_only = (ldt_info.flags >> 3) & 1;
3265 limit_in_pages = (ldt_info.flags >> 4) & 1;
3266 seg_not_present = (ldt_info.flags >> 5) & 1;
3267 useable = (ldt_info.flags >> 6) & 1;
3271 lm = (ldt_info.flags >> 7) & 1;
3274 if (contents == 3) {
3275 if (seg_not_present == 0)
3276 return -TARGET_EINVAL;
3279 /* NOTE: same code as Linux kernel */
3280 /* Allow LDTs to be cleared by the user. */
3281 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3282 if ((contents == 0 &&
3283 read_exec_only == 1 &&
3285 limit_in_pages == 0 &&
3286 seg_not_present == 1 &&
3294 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3295 (ldt_info.limit & 0x0ffff);
3296 entry_2 = (ldt_info.base_addr & 0xff000000) |
3297 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3298 (ldt_info.limit & 0xf0000) |
3299 ((read_exec_only ^ 1) << 9) |
3301 ((seg_not_present ^ 1) << 15) |
3303 (limit_in_pages << 23) |
3308 /* Install the new entry ... */
3310 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3311 lp[0] = tswap32(entry_1);
3312 lp[1] = tswap32(entry_2);
3316 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
3318 struct target_modify_ldt_ldt_s *target_ldt_info;
3319 uint64_t *gdt_table = g2h(env->gdt.base);
3320 uint32_t base_addr, limit, flags;
3321 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3322 int seg_not_present, useable, lm;
3323 uint32_t *lp, entry_1, entry_2;
3325 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3326 if (!target_ldt_info)
3327 return -TARGET_EFAULT;
3328 idx = tswap32(target_ldt_info->entry_number);
3329 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3330 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3331 unlock_user_struct(target_ldt_info, ptr, 1);
3332 return -TARGET_EINVAL;
3334 lp = (uint32_t *)(gdt_table + idx);
3335 entry_1 = tswap32(lp[0]);
3336 entry_2 = tswap32(lp[1]);
3338 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3339 contents = (entry_2 >> 10) & 3;
3340 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3341 seg_32bit = (entry_2 >> 22) & 1;
3342 limit_in_pages = (entry_2 >> 23) & 1;
3343 useable = (entry_2 >> 20) & 1;
3347 lm = (entry_2 >> 21) & 1;
3349 flags = (seg_32bit << 0) | (contents << 1) |
3350 (read_exec_only << 3) | (limit_in_pages << 4) |
3351 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3352 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3353 base_addr = (entry_1 >> 16) |
3354 (entry_2 & 0xff000000) |
3355 ((entry_2 & 0xff) << 16);
3356 target_ldt_info->base_addr = tswapl(base_addr);
3357 target_ldt_info->limit = tswap32(limit);
3358 target_ldt_info->flags = tswap32(flags);
3359 unlock_user_struct(target_ldt_info, ptr, 1);
3362 #endif /* TARGET_I386 && TARGET_ABI32 */
3364 #ifndef TARGET_ABI32
3365 static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
3372 case TARGET_ARCH_SET_GS:
3373 case TARGET_ARCH_SET_FS:
3374 if (code == TARGET_ARCH_SET_GS)
3378 cpu_x86_load_seg(env, idx, 0);
3379 env->segs[idx].base = addr;
3381 case TARGET_ARCH_GET_GS:
3382 case TARGET_ARCH_GET_FS:
3383 if (code == TARGET_ARCH_GET_GS)
3387 val = env->segs[idx].base;
3388 if (put_user(val, addr, abi_ulong))
3389 return -TARGET_EFAULT;
3392 ret = -TARGET_EINVAL;
3399 #endif /* defined(TARGET_I386) */
3401 #if defined(USE_NPTL)
3403 #define NEW_STACK_SIZE PTHREAD_STACK_MIN
3405 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3408 pthread_mutex_t mutex;
3409 pthread_cond_t cond;
3413 abi_ulong child_tidptr;
3414 abi_ulong parent_tidptr;
3418 static void *clone_func(void *arg)
3420 new_thread_info *info = arg;
3426 ts = (TaskState *)thread_env->opaque;
3427 info->tid = gettid();
3429 if (info->child_tidptr)
3430 put_user_u32(info->tid, info->child_tidptr);
3431 if (info->parent_tidptr)
3432 put_user_u32(info->tid, info->parent_tidptr);
3433 /* Enable signals. */
3434 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3435 /* Signal to the parent that we're ready. */
3436 pthread_mutex_lock(&info->mutex);
3437 pthread_cond_broadcast(&info->cond);
3438 pthread_mutex_unlock(&info->mutex);
3439 /* Wait until the parent has finshed initializing the tls state. */
3440 pthread_mutex_lock(&clone_lock);
3441 pthread_mutex_unlock(&clone_lock);
3447 /* this stack is the equivalent of the kernel stack associated with a
3449 #define NEW_STACK_SIZE 8192
3451 static int clone_func(void *arg)
3453 CPUState *env = arg;
3460 /* do_fork() Must return host values and target errnos (unlike most
3461 do_*() functions). */
3462 static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3463 abi_ulong parent_tidptr, target_ulong newtls,
3464 abi_ulong child_tidptr)
3470 #if defined(USE_NPTL)
3471 unsigned int nptl_flags;
3475 /* Emulate vfork() with fork() */
3476 if (flags & CLONE_VFORK)
3477 flags &= ~(CLONE_VFORK | CLONE_VM);
3479 if (flags & CLONE_VM) {
3480 TaskState *parent_ts = (TaskState *)env->opaque;
3481 #if defined(USE_NPTL)
3482 new_thread_info info;
3483 pthread_attr_t attr;
3485 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
3486 init_task_state(ts);
3487 new_stack = ts->stack;
3488 /* we create a new CPU instance. */
3489 new_env = cpu_copy(env);
3490 /* Init regs that differ from the parent. */
3491 cpu_clone_regs(new_env, newsp);
3492 new_env->opaque = ts;
3493 ts->bprm = parent_ts->bprm;
3494 ts->info = parent_ts->info;
3495 #if defined(USE_NPTL)
3497 flags &= ~CLONE_NPTL_FLAGS2;
3499 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3500 ts->child_tidptr = child_tidptr;
3503 if (nptl_flags & CLONE_SETTLS)
3504 cpu_set_tls (new_env, newtls);
3506 /* Grab a mutex so that thread setup appears atomic. */
3507 pthread_mutex_lock(&clone_lock);
3509 memset(&info, 0, sizeof(info));
3510 pthread_mutex_init(&info.mutex, NULL);
3511 pthread_mutex_lock(&info.mutex);
3512 pthread_cond_init(&info.cond, NULL);
3514 if (nptl_flags & CLONE_CHILD_SETTID)
3515 info.child_tidptr = child_tidptr;
3516 if (nptl_flags & CLONE_PARENT_SETTID)
3517 info.parent_tidptr = parent_tidptr;
3519 ret = pthread_attr_init(&attr);
3520 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
3521 /* It is not safe to deliver signals until the child has finished
3522 initializing, so temporarily block all signals. */
3523 sigfillset(&sigmask);
3524 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3526 ret = pthread_create(&info.thread, &attr, clone_func, &info);
3527 /* TODO: Free new CPU state if thread creation failed. */
3529 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3530 pthread_attr_destroy(&attr);
3532 /* Wait for the child to initialize. */
3533 pthread_cond_wait(&info.cond, &info.mutex);
3535 if (flags & CLONE_PARENT_SETTID)
3536 put_user_u32(ret, parent_tidptr);
3540 pthread_mutex_unlock(&info.mutex);
3541 pthread_cond_destroy(&info.cond);
3542 pthread_mutex_destroy(&info.mutex);
3543 pthread_mutex_unlock(&clone_lock);
3545 if (flags & CLONE_NPTL_FLAGS2)
3547 /* This is probably going to die very quickly, but do it anyway. */
3549 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3551 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3555 /* if no CLONE_VM, we consider it is a fork */
3556 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
3561 /* Child Process. */
3562 cpu_clone_regs(env, newsp);
3564 #if defined(USE_NPTL)
3565 /* There is a race condition here. The parent process could
3566 theoretically read the TID in the child process before the child
3567 tid is set. This would require using either ptrace
3568 (not implemented) or having *_tidptr to point at a shared memory
3569 mapping. We can't repeat the spinlock hack used above because
3570 the child process gets its own copy of the lock. */
3571 if (flags & CLONE_CHILD_SETTID)
3572 put_user_u32(gettid(), child_tidptr);
3573 if (flags & CLONE_PARENT_SETTID)
3574 put_user_u32(gettid(), parent_tidptr);
3575 ts = (TaskState *)env->opaque;
3576 if (flags & CLONE_SETTLS)
3577 cpu_set_tls (env, newtls);
3578 if (flags & CLONE_CHILD_CLEARTID)
3579 ts->child_tidptr = child_tidptr;
3588 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
3591 struct target_flock *target_fl;
3592 struct flock64 fl64;
3593 struct target_flock64 *target_fl64;
3597 case TARGET_F_GETLK:
3598 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3599 return -TARGET_EFAULT;
3600 fl.l_type = tswap16(target_fl->l_type);
3601 fl.l_whence = tswap16(target_fl->l_whence);
3602 fl.l_start = tswapl(target_fl->l_start);
3603 fl.l_len = tswapl(target_fl->l_len);
3604 fl.l_pid = tswapl(target_fl->l_pid);
3605 unlock_user_struct(target_fl, arg, 0);
3606 ret = get_errno(fcntl(fd, F_GETLK, &fl));
3608 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3609 return -TARGET_EFAULT;
3610 target_fl->l_type = tswap16(fl.l_type);
3611 target_fl->l_whence = tswap16(fl.l_whence);
3612 target_fl->l_start = tswapl(fl.l_start);
3613 target_fl->l_len = tswapl(fl.l_len);
3614 target_fl->l_pid = tswapl(fl.l_pid);
3615 unlock_user_struct(target_fl, arg, 1);
3619 case TARGET_F_SETLK:
3620 case TARGET_F_SETLKW:
3621 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3622 return -TARGET_EFAULT;
3623 fl.l_type = tswap16(target_fl->l_type);
3624 fl.l_whence = tswap16(target_fl->l_whence);
3625 fl.l_start = tswapl(target_fl->l_start);
3626 fl.l_len = tswapl(target_fl->l_len);
3627 fl.l_pid = tswapl(target_fl->l_pid);
3628 unlock_user_struct(target_fl, arg, 0);
3629 ret = get_errno(fcntl(fd, F_SETLK+(cmd-TARGET_F_SETLK), &fl));
3632 case TARGET_F_GETLK64:
3633 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3634 return -TARGET_EFAULT;
3635 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3636 fl64.l_whence = tswap16(target_fl64->l_whence);
3637 fl64.l_start = tswapl(target_fl64->l_start);
3638 fl64.l_len = tswapl(target_fl64->l_len);
3639 fl64.l_pid = tswap16(target_fl64->l_pid);
3640 unlock_user_struct(target_fl64, arg, 0);
3641 ret = get_errno(fcntl(fd, F_GETLK64, &fl64));
3643 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3644 return -TARGET_EFAULT;
3645 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3646 target_fl64->l_whence = tswap16(fl64.l_whence);
3647 target_fl64->l_start = tswapl(fl64.l_start);
3648 target_fl64->l_len = tswapl(fl64.l_len);
3649 target_fl64->l_pid = tswapl(fl64.l_pid);
3650 unlock_user_struct(target_fl64, arg, 1);
3653 case TARGET_F_SETLK64:
3654 case TARGET_F_SETLKW64:
3655 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3656 return -TARGET_EFAULT;
3657 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3658 fl64.l_whence = tswap16(target_fl64->l_whence);
3659 fl64.l_start = tswapl(target_fl64->l_start);
3660 fl64.l_len = tswapl(target_fl64->l_len);
3661 fl64.l_pid = tswap16(target_fl64->l_pid);
3662 unlock_user_struct(target_fl64, arg, 0);
3663 ret = get_errno(fcntl(fd, F_SETLK64+(cmd-TARGET_F_SETLK64), &fl64));
3667 ret = get_errno(fcntl(fd, cmd, arg));
3669 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3674 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3678 ret = get_errno(fcntl(fd, cmd, arg));
3686 static inline int high2lowuid(int uid)
3694 static inline int high2lowgid(int gid)
3702 static inline int low2highuid(int uid)
3704 if ((int16_t)uid == -1)
3710 static inline int low2highgid(int gid)
3712 if ((int16_t)gid == -1)
3718 #endif /* USE_UID16 */
3720 void syscall_init(void)
3723 const argtype *arg_type;
3727 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
3728 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
3729 #include "syscall_types.h"
3731 #undef STRUCT_SPECIAL
3733 /* we patch the ioctl size if necessary. We rely on the fact that
3734 no ioctl has all the bits at '1' in the size field */
3736 while (ie->target_cmd != 0) {
3737 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3738 TARGET_IOC_SIZEMASK) {
3739 arg_type = ie->arg_type;
3740 if (arg_type[0] != TYPE_PTR) {
3741 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
3746 size = thunk_type_size(arg_type, 0);
3747 ie->target_cmd = (ie->target_cmd &
3748 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3749 (size << TARGET_IOC_SIZESHIFT);
3752 /* Build target_to_host_errno_table[] table from
3753 * host_to_target_errno_table[]. */
3754 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3755 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3757 /* automatic consistency check if same arch */
3758 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3759 (defined(__x86_64__) && defined(TARGET_X86_64))
3760 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3761 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3762 ie->name, ie->target_cmd, ie->host_cmd);
3769 #if TARGET_ABI_BITS == 32
3770 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3772 #ifdef TARGET_WORDS_BIGENDIAN
3773 return ((uint64_t)word0 << 32) | word1;
3775 return ((uint64_t)word1 << 32) | word0;
3778 #else /* TARGET_ABI_BITS == 32 */
3779 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3783 #endif /* TARGET_ABI_BITS != 32 */
3785 #ifdef TARGET_NR_truncate64
3786 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3792 if (((CPUARMState *)cpu_env)->eabi)
3798 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3802 #ifdef TARGET_NR_ftruncate64
3803 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3809 if (((CPUARMState *)cpu_env)->eabi)
3815 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3819 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3820 abi_ulong target_addr)
3822 struct target_timespec *target_ts;
3824 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3825 return -TARGET_EFAULT;
3826 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3827 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3828 unlock_user_struct(target_ts, target_addr, 0);
3832 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3833 struct timespec *host_ts)
3835 struct target_timespec *target_ts;
3837 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3838 return -TARGET_EFAULT;
3839 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3840 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3841 unlock_user_struct(target_ts, target_addr, 1);
3845 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
3846 static inline abi_long host_to_target_stat64(void *cpu_env,
3847 abi_ulong target_addr,
3848 struct stat *host_st)
3851 if (((CPUARMState *)cpu_env)->eabi) {
3852 struct target_eabi_stat64 *target_st;
3854 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3855 return -TARGET_EFAULT;
3856 memset(target_st, 0, sizeof(struct target_eabi_stat64));
3857 __put_user(host_st->st_dev, &target_st->st_dev);
3858 __put_user(host_st->st_ino, &target_st->st_ino);
3859 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3860 __put_user(host_st->st_ino, &target_st->__st_ino);
3862 __put_user(host_st->st_mode, &target_st->st_mode);
3863 __put_user(host_st->st_nlink, &target_st->st_nlink);
3864 __put_user(host_st->st_uid, &target_st->st_uid);
3865 __put_user(host_st->st_gid, &target_st->st_gid);
3866 __put_user(host_st->st_rdev, &target_st->st_rdev);
3867 __put_user(host_st->st_size, &target_st->st_size);
3868 __put_user(host_st->st_blksize, &target_st->st_blksize);
3869 __put_user(host_st->st_blocks, &target_st->st_blocks);
3870 __put_user(host_st->st_atime, &target_st->target_st_atime);
3871 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3872 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3873 unlock_user_struct(target_st, target_addr, 1);
3877 #if TARGET_LONG_BITS == 64
3878 struct target_stat *target_st;
3880 struct target_stat64 *target_st;
3883 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3884 return -TARGET_EFAULT;
3885 memset(target_st, 0, sizeof(*target_st));
3886 __put_user(host_st->st_dev, &target_st->st_dev);
3887 __put_user(host_st->st_ino, &target_st->st_ino);
3888 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3889 __put_user(host_st->st_ino, &target_st->__st_ino);
3891 __put_user(host_st->st_mode, &target_st->st_mode);
3892 __put_user(host_st->st_nlink, &target_st->st_nlink);
3893 __put_user(host_st->st_uid, &target_st->st_uid);
3894 __put_user(host_st->st_gid, &target_st->st_gid);
3895 __put_user(host_st->st_rdev, &target_st->st_rdev);
3896 /* XXX: better use of kernel struct */
3897 __put_user(host_st->st_size, &target_st->st_size);
3898 __put_user(host_st->st_blksize, &target_st->st_blksize);
3899 __put_user(host_st->st_blocks, &target_st->st_blocks);
3900 __put_user(host_st->st_atime, &target_st->target_st_atime);
3901 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3902 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3903 unlock_user_struct(target_st, target_addr, 1);
3910 #if defined(USE_NPTL)
3911 /* ??? Using host futex calls even when target atomic operations
3912 are not really atomic probably breaks things. However implementing
3913 futexes locally would make futexes shared between multiple processes
3914 tricky. However they're probably useless because guest atomic
3915 operations won't work either. */
3916 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
3917 target_ulong uaddr2, int val3)
3919 struct timespec ts, *pts;
3921 /* ??? We assume FUTEX_* constants are the same on both host
3927 target_to_host_timespec(pts, timeout);
3931 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAIT, tswap32(val),
3934 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAKE, val, NULL, NULL, 0));
3936 return get_errno(sys_futex(g2h(uaddr), FUTEX_FD, val, NULL, NULL, 0));
3938 return get_errno(sys_futex(g2h(uaddr), FUTEX_REQUEUE, val,
3939 NULL, g2h(uaddr2), 0));
3940 case FUTEX_CMP_REQUEUE:
3941 return get_errno(sys_futex(g2h(uaddr), FUTEX_CMP_REQUEUE, val,
3942 NULL, g2h(uaddr2), tswap32(val3)));
3944 return -TARGET_ENOSYS;
3949 /* Map host to target signal numbers for the wait family of syscalls.
3950 Assume all other status bits are the same. */
3951 static int host_to_target_waitstatus(int status)
3953 if (WIFSIGNALED(status)) {
3954 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
3956 if (WIFSTOPPED(status)) {
3957 return (host_to_target_signal(WSTOPSIG(status)) << 8)
3963 int get_osversion(void)
3965 static int osversion;
3966 struct new_utsname buf;
3971 if (qemu_uname_release && *qemu_uname_release) {
3972 s = qemu_uname_release;
3974 if (sys_uname(&buf))
3979 for (i = 0; i < 3; i++) {
3981 while (*s >= '0' && *s <= '9') {
3986 tmp = (tmp << 8) + n;
3994 /* do_syscall() should always have a single exit point at the end so
3995 that actions, such as logging of syscall results, can be performed.
3996 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
3997 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3998 abi_long arg2, abi_long arg3, abi_long arg4,
3999 abi_long arg5, abi_long arg6)
4007 gemu_log("syscall %d", num);
4010 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4013 case TARGET_NR_exit:
4015 /* In old applications this may be used to implement _exit(2).
4016 However in threaded applictions it is used for thread termination,
4017 and _exit_group is used for application termination.
4018 Do thread termination if we have more then one thread. */
4019 /* FIXME: This probably breaks if a signal arrives. We should probably
4020 be disabling signals. */
4021 if (first_cpu->next_cpu) {
4028 while (p && p != (CPUState *)cpu_env) {
4029 lastp = &p->next_cpu;
4032 /* If we didn't find the CPU for this thread then something is
4036 /* Remove the CPU from the list. */
4037 *lastp = p->next_cpu;
4039 TaskState *ts = ((CPUState *)cpu_env)->opaque;
4040 if (ts->child_tidptr) {
4041 put_user_u32(0, ts->child_tidptr);
4042 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4045 /* TODO: Free CPU state. */
4052 gdb_exit(cpu_env, arg1);
4054 ret = 0; /* avoid warning */
4056 case TARGET_NR_read:
4060 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4062 ret = get_errno(read(arg1, p, arg3));
4063 unlock_user(p, arg2, ret);
4066 case TARGET_NR_write:
4067 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4069 ret = get_errno(write(arg1, p, arg3));
4070 unlock_user(p, arg2, 0);
4072 case TARGET_NR_open:
4073 if (!(p = lock_user_string(arg1)))
4075 ret = get_errno(open(path(p),
4076 target_to_host_bitmask(arg2, fcntl_flags_tbl),
4078 unlock_user(p, arg1, 0);
4080 #if defined(TARGET_NR_openat) && defined(__NR_openat)
4081 case TARGET_NR_openat:
4082 if (!(p = lock_user_string(arg2)))
4084 ret = get_errno(sys_openat(arg1,
4086 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4088 unlock_user(p, arg2, 0);
4091 case TARGET_NR_close:
4092 ret = get_errno(close(arg1));
4097 case TARGET_NR_fork:
4098 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
4100 #ifdef TARGET_NR_waitpid
4101 case TARGET_NR_waitpid:
4104 ret = get_errno(waitpid(arg1, &status, arg3));
4105 if (!is_error(ret) && arg2
4106 && put_user_s32(host_to_target_waitstatus(status), arg2))
4111 #ifdef TARGET_NR_waitid
4112 case TARGET_NR_waitid:
4116 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4117 if (!is_error(ret) && arg3 && info.si_pid != 0) {
4118 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
4120 host_to_target_siginfo(p, &info);
4121 unlock_user(p, arg3, sizeof(target_siginfo_t));
4126 #ifdef TARGET_NR_creat /* not on alpha */
4127 case TARGET_NR_creat:
4128 if (!(p = lock_user_string(arg1)))
4130 ret = get_errno(creat(p, arg2));
4131 unlock_user(p, arg1, 0);
4134 case TARGET_NR_link:
4137 p = lock_user_string(arg1);
4138 p2 = lock_user_string(arg2);
4140 ret = -TARGET_EFAULT;
4142 ret = get_errno(link(p, p2));
4143 unlock_user(p2, arg2, 0);
4144 unlock_user(p, arg1, 0);
4147 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4148 case TARGET_NR_linkat:
4153 p = lock_user_string(arg2);
4154 p2 = lock_user_string(arg4);
4156 ret = -TARGET_EFAULT;
4158 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
4159 unlock_user(p, arg2, 0);
4160 unlock_user(p2, arg4, 0);
4164 case TARGET_NR_unlink:
4165 if (!(p = lock_user_string(arg1)))
4167 ret = get_errno(unlink(p));
4168 unlock_user(p, arg1, 0);
4170 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4171 case TARGET_NR_unlinkat:
4172 if (!(p = lock_user_string(arg2)))
4174 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4175 unlock_user(p, arg2, 0);
4178 case TARGET_NR_execve:
4180 char **argp, **envp;
4183 abi_ulong guest_argp;
4184 abi_ulong guest_envp;
4190 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
4191 if (get_user_ual(addr, gp))
4199 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
4200 if (get_user_ual(addr, gp))
4207 argp = alloca((argc + 1) * sizeof(void *));
4208 envp = alloca((envc + 1) * sizeof(void *));
4210 for (gp = guest_argp, q = argp; gp;
4211 gp += sizeof(abi_ulong), q++) {
4212 if (get_user_ual(addr, gp))
4216 if (!(*q = lock_user_string(addr)))
4221 for (gp = guest_envp, q = envp; gp;
4222 gp += sizeof(abi_ulong), q++) {
4223 if (get_user_ual(addr, gp))
4227 if (!(*q = lock_user_string(addr)))
4232 if (!(p = lock_user_string(arg1)))
4234 ret = get_errno(execve(p, argp, envp));
4235 unlock_user(p, arg1, 0);
4240 ret = -TARGET_EFAULT;
4243 for (gp = guest_argp, q = argp; *q;
4244 gp += sizeof(abi_ulong), q++) {
4245 if (get_user_ual(addr, gp)
4248 unlock_user(*q, addr, 0);
4250 for (gp = guest_envp, q = envp; *q;
4251 gp += sizeof(abi_ulong), q++) {
4252 if (get_user_ual(addr, gp)
4255 unlock_user(*q, addr, 0);
4259 case TARGET_NR_chdir:
4260 if (!(p = lock_user_string(arg1)))
4262 ret = get_errno(chdir(p));
4263 unlock_user(p, arg1, 0);
4265 #ifdef TARGET_NR_time
4266 case TARGET_NR_time:
4269 ret = get_errno(time(&host_time));
4272 && put_user_sal(host_time, arg1))
4277 case TARGET_NR_mknod:
4278 if (!(p = lock_user_string(arg1)))
4280 ret = get_errno(mknod(p, arg2, arg3));
4281 unlock_user(p, arg1, 0);
4283 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4284 case TARGET_NR_mknodat:
4285 if (!(p = lock_user_string(arg2)))
4287 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4288 unlock_user(p, arg2, 0);
4291 case TARGET_NR_chmod:
4292 if (!(p = lock_user_string(arg1)))
4294 ret = get_errno(chmod(p, arg2));
4295 unlock_user(p, arg1, 0);
4297 #ifdef TARGET_NR_break
4298 case TARGET_NR_break:
4301 #ifdef TARGET_NR_oldstat
4302 case TARGET_NR_oldstat:
4305 case TARGET_NR_lseek:
4306 ret = get_errno(lseek(arg1, arg2, arg3));
4308 #ifdef TARGET_NR_getxpid
4309 case TARGET_NR_getxpid:
4311 case TARGET_NR_getpid:
4313 ret = get_errno(getpid());
4315 case TARGET_NR_mount:
4317 /* need to look at the data field */
4319 p = lock_user_string(arg1);
4320 p2 = lock_user_string(arg2);
4321 p3 = lock_user_string(arg3);
4322 if (!p || !p2 || !p3)
4323 ret = -TARGET_EFAULT;
4325 /* FIXME - arg5 should be locked, but it isn't clear how to
4326 * do that since it's not guaranteed to be a NULL-terminated
4329 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4330 unlock_user(p, arg1, 0);
4331 unlock_user(p2, arg2, 0);
4332 unlock_user(p3, arg3, 0);
4335 #ifdef TARGET_NR_umount
4336 case TARGET_NR_umount:
4337 if (!(p = lock_user_string(arg1)))
4339 ret = get_errno(umount(p));
4340 unlock_user(p, arg1, 0);
4343 #ifdef TARGET_NR_stime /* not on alpha */
4344 case TARGET_NR_stime:
4347 if (get_user_sal(host_time, arg1))
4349 ret = get_errno(stime(&host_time));
4353 case TARGET_NR_ptrace:
4355 #ifdef TARGET_NR_alarm /* not on alpha */
4356 case TARGET_NR_alarm:
4360 #ifdef TARGET_NR_oldfstat
4361 case TARGET_NR_oldfstat:
4364 #ifdef TARGET_NR_pause /* not on alpha */
4365 case TARGET_NR_pause:
4366 ret = get_errno(pause());
4369 #ifdef TARGET_NR_utime
4370 case TARGET_NR_utime:
4372 struct utimbuf tbuf, *host_tbuf;
4373 struct target_utimbuf *target_tbuf;
4375 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4377 tbuf.actime = tswapl(target_tbuf->actime);
4378 tbuf.modtime = tswapl(target_tbuf->modtime);
4379 unlock_user_struct(target_tbuf, arg2, 0);
4384 if (!(p = lock_user_string(arg1)))
4386 ret = get_errno(utime(p, host_tbuf));
4387 unlock_user(p, arg1, 0);
4391 case TARGET_NR_utimes:
4393 struct timeval *tvp, tv[2];
4395 if (copy_from_user_timeval(&tv[0], arg2)
4396 || copy_from_user_timeval(&tv[1],
4397 arg2 + sizeof(struct target_timeval)))
4403 if (!(p = lock_user_string(arg1)))
4405 ret = get_errno(utimes(p, tvp));
4406 unlock_user(p, arg1, 0);
4409 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4410 case TARGET_NR_futimesat:
4412 struct timeval *tvp, tv[2];
4414 if (copy_from_user_timeval(&tv[0], arg3)
4415 || copy_from_user_timeval(&tv[1],
4416 arg3 + sizeof(struct target_timeval)))
4422 if (!(p = lock_user_string(arg2)))
4424 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4425 unlock_user(p, arg2, 0);
4429 #ifdef TARGET_NR_stty
4430 case TARGET_NR_stty:
4433 #ifdef TARGET_NR_gtty
4434 case TARGET_NR_gtty:
4437 case TARGET_NR_access:
4438 if (!(p = lock_user_string(arg1)))
4440 ret = get_errno(access(p, arg2));
4441 unlock_user(p, arg1, 0);
4443 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4444 case TARGET_NR_faccessat:
4445 if (!(p = lock_user_string(arg2)))
4447 ret = get_errno(sys_faccessat(arg1, p, arg3));
4448 unlock_user(p, arg2, 0);
4451 #ifdef TARGET_NR_nice /* not on alpha */
4452 case TARGET_NR_nice:
4453 ret = get_errno(nice(arg1));
4456 #ifdef TARGET_NR_ftime
4457 case TARGET_NR_ftime:
4460 case TARGET_NR_sync:
4464 case TARGET_NR_kill:
4465 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
4467 case TARGET_NR_rename:
4470 p = lock_user_string(arg1);
4471 p2 = lock_user_string(arg2);
4473 ret = -TARGET_EFAULT;
4475 ret = get_errno(rename(p, p2));
4476 unlock_user(p2, arg2, 0);
4477 unlock_user(p, arg1, 0);
4480 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4481 case TARGET_NR_renameat:
4484 p = lock_user_string(arg2);
4485 p2 = lock_user_string(arg4);
4487 ret = -TARGET_EFAULT;
4489 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
4490 unlock_user(p2, arg4, 0);
4491 unlock_user(p, arg2, 0);
4495 case TARGET_NR_mkdir:
4496 if (!(p = lock_user_string(arg1)))
4498 ret = get_errno(mkdir(p, arg2));
4499 unlock_user(p, arg1, 0);
4501 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4502 case TARGET_NR_mkdirat:
4503 if (!(p = lock_user_string(arg2)))
4505 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4506 unlock_user(p, arg2, 0);
4509 case TARGET_NR_rmdir:
4510 if (!(p = lock_user_string(arg1)))
4512 ret = get_errno(rmdir(p));
4513 unlock_user(p, arg1, 0);
4516 ret = get_errno(dup(arg1));
4518 case TARGET_NR_pipe:
4521 ret = get_errno(pipe(host_pipe));
4522 if (!is_error(ret)) {
4523 #if defined(TARGET_MIPS)
4524 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
4525 env->active_tc.gpr[3] = host_pipe[1];
4527 #elif defined(TARGET_SH4)
4528 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
4531 if (put_user_s32(host_pipe[0], arg1)
4532 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
4538 case TARGET_NR_times:
4540 struct target_tms *tmsp;
4542 ret = get_errno(times(&tms));
4544 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4547 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4548 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4549 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4550 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
4553 ret = host_to_target_clock_t(ret);
4556 #ifdef TARGET_NR_prof
4557 case TARGET_NR_prof:
4560 #ifdef TARGET_NR_signal
4561 case TARGET_NR_signal:
4564 case TARGET_NR_acct:
4566 ret = get_errno(acct(NULL));
4568 if (!(p = lock_user_string(arg1)))
4570 ret = get_errno(acct(path(p)));
4571 unlock_user(p, arg1, 0);
4574 #ifdef TARGET_NR_umount2 /* not on alpha */
4575 case TARGET_NR_umount2:
4576 if (!(p = lock_user_string(arg1)))
4578 ret = get_errno(umount2(p, arg2));
4579 unlock_user(p, arg1, 0);
4582 #ifdef TARGET_NR_lock
4583 case TARGET_NR_lock:
4586 case TARGET_NR_ioctl:
4587 ret = do_ioctl(arg1, arg2, arg3);
4589 case TARGET_NR_fcntl:
4590 ret = do_fcntl(arg1, arg2, arg3);
4592 #ifdef TARGET_NR_mpx
4596 case TARGET_NR_setpgid:
4597 ret = get_errno(setpgid(arg1, arg2));
4599 #ifdef TARGET_NR_ulimit
4600 case TARGET_NR_ulimit:
4603 #ifdef TARGET_NR_oldolduname
4604 case TARGET_NR_oldolduname:
4607 case TARGET_NR_umask:
4608 ret = get_errno(umask(arg1));
4610 case TARGET_NR_chroot:
4611 if (!(p = lock_user_string(arg1)))
4613 ret = get_errno(chroot(p));
4614 unlock_user(p, arg1, 0);
4616 case TARGET_NR_ustat:
4618 case TARGET_NR_dup2:
4619 ret = get_errno(dup2(arg1, arg2));
4621 #ifdef TARGET_NR_getppid /* not on alpha */
4622 case TARGET_NR_getppid:
4623 ret = get_errno(getppid());
4626 case TARGET_NR_getpgrp:
4627 ret = get_errno(getpgrp());
4629 case TARGET_NR_setsid:
4630 ret = get_errno(setsid());
4632 #ifdef TARGET_NR_sigaction
4633 case TARGET_NR_sigaction:
4635 #if !defined(TARGET_MIPS)
4636 struct target_old_sigaction *old_act;
4637 struct target_sigaction act, oact, *pact;
4639 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4641 act._sa_handler = old_act->_sa_handler;
4642 target_siginitset(&act.sa_mask, old_act->sa_mask);
4643 act.sa_flags = old_act->sa_flags;
4644 act.sa_restorer = old_act->sa_restorer;
4645 unlock_user_struct(old_act, arg2, 0);
4650 ret = get_errno(do_sigaction(arg1, pact, &oact));
4651 if (!is_error(ret) && arg3) {
4652 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4654 old_act->_sa_handler = oact._sa_handler;
4655 old_act->sa_mask = oact.sa_mask.sig[0];
4656 old_act->sa_flags = oact.sa_flags;
4657 old_act->sa_restorer = oact.sa_restorer;
4658 unlock_user_struct(old_act, arg3, 1);
4661 struct target_sigaction act, oact, *pact, *old_act;
4664 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4666 act._sa_handler = old_act->_sa_handler;
4667 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4668 act.sa_flags = old_act->sa_flags;
4669 unlock_user_struct(old_act, arg2, 0);
4675 ret = get_errno(do_sigaction(arg1, pact, &oact));
4677 if (!is_error(ret) && arg3) {
4678 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4680 old_act->_sa_handler = oact._sa_handler;
4681 old_act->sa_flags = oact.sa_flags;
4682 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4683 old_act->sa_mask.sig[1] = 0;
4684 old_act->sa_mask.sig[2] = 0;
4685 old_act->sa_mask.sig[3] = 0;
4686 unlock_user_struct(old_act, arg3, 1);
4692 case TARGET_NR_rt_sigaction:
4694 struct target_sigaction *act;
4695 struct target_sigaction *oact;
4698 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4703 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4704 ret = -TARGET_EFAULT;
4705 goto rt_sigaction_fail;
4709 ret = get_errno(do_sigaction(arg1, act, oact));
4712 unlock_user_struct(act, arg2, 0);
4714 unlock_user_struct(oact, arg3, 1);
4717 #ifdef TARGET_NR_sgetmask /* not on alpha */
4718 case TARGET_NR_sgetmask:
4721 abi_ulong target_set;
4722 sigprocmask(0, NULL, &cur_set);
4723 host_to_target_old_sigset(&target_set, &cur_set);
4728 #ifdef TARGET_NR_ssetmask /* not on alpha */
4729 case TARGET_NR_ssetmask:
4731 sigset_t set, oset, cur_set;
4732 abi_ulong target_set = arg1;
4733 sigprocmask(0, NULL, &cur_set);
4734 target_to_host_old_sigset(&set, &target_set);
4735 sigorset(&set, &set, &cur_set);
4736 sigprocmask(SIG_SETMASK, &set, &oset);
4737 host_to_target_old_sigset(&target_set, &oset);
4742 #ifdef TARGET_NR_sigprocmask
4743 case TARGET_NR_sigprocmask:
4746 sigset_t set, oldset, *set_ptr;
4750 case TARGET_SIG_BLOCK:
4753 case TARGET_SIG_UNBLOCK:
4756 case TARGET_SIG_SETMASK:
4760 ret = -TARGET_EINVAL;
4763 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4765 target_to_host_old_sigset(&set, p);
4766 unlock_user(p, arg2, 0);
4772 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
4773 if (!is_error(ret) && arg3) {
4774 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4776 host_to_target_old_sigset(p, &oldset);
4777 unlock_user(p, arg3, sizeof(target_sigset_t));
4782 case TARGET_NR_rt_sigprocmask:
4785 sigset_t set, oldset, *set_ptr;
4789 case TARGET_SIG_BLOCK:
4792 case TARGET_SIG_UNBLOCK:
4795 case TARGET_SIG_SETMASK:
4799 ret = -TARGET_EINVAL;
4802 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4804 target_to_host_sigset(&set, p);
4805 unlock_user(p, arg2, 0);
4811 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
4812 if (!is_error(ret) && arg3) {
4813 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4815 host_to_target_sigset(p, &oldset);
4816 unlock_user(p, arg3, sizeof(target_sigset_t));
4820 #ifdef TARGET_NR_sigpending
4821 case TARGET_NR_sigpending:
4824 ret = get_errno(sigpending(&set));
4825 if (!is_error(ret)) {
4826 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4828 host_to_target_old_sigset(p, &set);
4829 unlock_user(p, arg1, sizeof(target_sigset_t));
4834 case TARGET_NR_rt_sigpending:
4837 ret = get_errno(sigpending(&set));
4838 if (!is_error(ret)) {
4839 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4841 host_to_target_sigset(p, &set);
4842 unlock_user(p, arg1, sizeof(target_sigset_t));
4846 #ifdef TARGET_NR_sigsuspend
4847 case TARGET_NR_sigsuspend:
4850 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4852 target_to_host_old_sigset(&set, p);
4853 unlock_user(p, arg1, 0);
4854 ret = get_errno(sigsuspend(&set));
4858 case TARGET_NR_rt_sigsuspend:
4861 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4863 target_to_host_sigset(&set, p);
4864 unlock_user(p, arg1, 0);
4865 ret = get_errno(sigsuspend(&set));
4868 case TARGET_NR_rt_sigtimedwait:
4871 struct timespec uts, *puts;
4874 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4876 target_to_host_sigset(&set, p);
4877 unlock_user(p, arg1, 0);
4880 target_to_host_timespec(puts, arg3);
4884 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
4885 if (!is_error(ret) && arg2) {
4886 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
4888 host_to_target_siginfo(p, &uinfo);
4889 unlock_user(p, arg2, sizeof(target_siginfo_t));
4893 case TARGET_NR_rt_sigqueueinfo:
4896 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
4898 target_to_host_siginfo(&uinfo, p);
4899 unlock_user(p, arg1, 0);
4900 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
4903 #ifdef TARGET_NR_sigreturn
4904 case TARGET_NR_sigreturn:
4905 /* NOTE: ret is eax, so not transcoding must be done */
4906 ret = do_sigreturn(cpu_env);
4909 case TARGET_NR_rt_sigreturn:
4910 /* NOTE: ret is eax, so not transcoding must be done */
4911 ret = do_rt_sigreturn(cpu_env);
4913 case TARGET_NR_sethostname:
4914 if (!(p = lock_user_string(arg1)))
4916 ret = get_errno(sethostname(p, arg2));
4917 unlock_user(p, arg1, 0);
4919 case TARGET_NR_setrlimit:
4921 /* XXX: convert resource ? */
4922 int resource = arg1;
4923 struct target_rlimit *target_rlim;
4925 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
4927 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4928 rlim.rlim_max = tswapl(target_rlim->rlim_max);
4929 unlock_user_struct(target_rlim, arg2, 0);
4930 ret = get_errno(setrlimit(resource, &rlim));
4933 case TARGET_NR_getrlimit:
4935 /* XXX: convert resource ? */
4936 int resource = arg1;
4937 struct target_rlimit *target_rlim;
4940 ret = get_errno(getrlimit(resource, &rlim));
4941 if (!is_error(ret)) {
4942 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4944 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4945 rlim.rlim_max = tswapl(target_rlim->rlim_max);
4946 unlock_user_struct(target_rlim, arg2, 1);
4950 case TARGET_NR_getrusage:
4952 struct rusage rusage;
4953 ret = get_errno(getrusage(arg1, &rusage));
4954 if (!is_error(ret)) {
4955 host_to_target_rusage(arg2, &rusage);
4959 case TARGET_NR_gettimeofday:
4962 ret = get_errno(gettimeofday(&tv, NULL));
4963 if (!is_error(ret)) {
4964 if (copy_to_user_timeval(arg1, &tv))
4969 case TARGET_NR_settimeofday:
4972 if (copy_from_user_timeval(&tv, arg1))
4974 ret = get_errno(settimeofday(&tv, NULL));
4977 #ifdef TARGET_NR_select
4978 case TARGET_NR_select:
4980 struct target_sel_arg_struct *sel;
4981 abi_ulong inp, outp, exp, tvp;
4984 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
4986 nsel = tswapl(sel->n);
4987 inp = tswapl(sel->inp);
4988 outp = tswapl(sel->outp);
4989 exp = tswapl(sel->exp);
4990 tvp = tswapl(sel->tvp);
4991 unlock_user_struct(sel, arg1, 0);
4992 ret = do_select(nsel, inp, outp, exp, tvp);
4996 case TARGET_NR_symlink:
4999 p = lock_user_string(arg1);
5000 p2 = lock_user_string(arg2);
5002 ret = -TARGET_EFAULT;
5004 ret = get_errno(symlink(p, p2));
5005 unlock_user(p2, arg2, 0);
5006 unlock_user(p, arg1, 0);
5009 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5010 case TARGET_NR_symlinkat:
5013 p = lock_user_string(arg1);
5014 p2 = lock_user_string(arg3);
5016 ret = -TARGET_EFAULT;
5018 ret = get_errno(sys_symlinkat(p, arg2, p2));
5019 unlock_user(p2, arg3, 0);
5020 unlock_user(p, arg1, 0);
5024 #ifdef TARGET_NR_oldlstat
5025 case TARGET_NR_oldlstat:
5028 case TARGET_NR_readlink:
5031 p = lock_user_string(arg1);
5032 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5034 ret = -TARGET_EFAULT;
5036 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5037 char real[PATH_MAX];
5038 temp = realpath(exec_path,real);
5039 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5040 snprintf((char *)p2, arg3, "%s", real);
5043 ret = get_errno(readlink(path(p), p2, arg3));
5045 unlock_user(p2, arg2, ret);
5046 unlock_user(p, arg1, 0);
5049 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5050 case TARGET_NR_readlinkat:
5053 p = lock_user_string(arg2);
5054 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5056 ret = -TARGET_EFAULT;
5058 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
5059 unlock_user(p2, arg3, ret);
5060 unlock_user(p, arg2, 0);
5064 #ifdef TARGET_NR_uselib
5065 case TARGET_NR_uselib:
5068 #ifdef TARGET_NR_swapon
5069 case TARGET_NR_swapon:
5070 if (!(p = lock_user_string(arg1)))
5072 ret = get_errno(swapon(p, arg2));
5073 unlock_user(p, arg1, 0);
5076 case TARGET_NR_reboot:
5078 #ifdef TARGET_NR_readdir
5079 case TARGET_NR_readdir:
5082 #ifdef TARGET_NR_mmap
5083 case TARGET_NR_mmap:
5084 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
5087 abi_ulong v1, v2, v3, v4, v5, v6;
5088 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5096 unlock_user(v, arg1, 0);
5097 ret = get_errno(target_mmap(v1, v2, v3,
5098 target_to_host_bitmask(v4, mmap_flags_tbl),
5102 ret = get_errno(target_mmap(arg1, arg2, arg3,
5103 target_to_host_bitmask(arg4, mmap_flags_tbl),
5109 #ifdef TARGET_NR_mmap2
5110 case TARGET_NR_mmap2:
5112 #define MMAP_SHIFT 12
5114 ret = get_errno(target_mmap(arg1, arg2, arg3,
5115 target_to_host_bitmask(arg4, mmap_flags_tbl),
5117 arg6 << MMAP_SHIFT));
5120 case TARGET_NR_munmap:
5121 ret = get_errno(target_munmap(arg1, arg2));
5123 case TARGET_NR_mprotect:
5124 ret = get_errno(target_mprotect(arg1, arg2, arg3));
5126 #ifdef TARGET_NR_mremap
5127 case TARGET_NR_mremap:
5128 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
5131 /* ??? msync/mlock/munlock are broken for softmmu. */
5132 #ifdef TARGET_NR_msync
5133 case TARGET_NR_msync:
5134 ret = get_errno(msync(g2h(arg1), arg2, arg3));
5137 #ifdef TARGET_NR_mlock
5138 case TARGET_NR_mlock:
5139 ret = get_errno(mlock(g2h(arg1), arg2));
5142 #ifdef TARGET_NR_munlock
5143 case TARGET_NR_munlock:
5144 ret = get_errno(munlock(g2h(arg1), arg2));
5147 #ifdef TARGET_NR_mlockall
5148 case TARGET_NR_mlockall:
5149 ret = get_errno(mlockall(arg1));
5152 #ifdef TARGET_NR_munlockall
5153 case TARGET_NR_munlockall:
5154 ret = get_errno(munlockall());
5157 case TARGET_NR_truncate:
5158 if (!(p = lock_user_string(arg1)))
5160 ret = get_errno(truncate(p, arg2));
5161 unlock_user(p, arg1, 0);
5163 case TARGET_NR_ftruncate:
5164 ret = get_errno(ftruncate(arg1, arg2));
5166 case TARGET_NR_fchmod:
5167 ret = get_errno(fchmod(arg1, arg2));
5169 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5170 case TARGET_NR_fchmodat:
5171 if (!(p = lock_user_string(arg2)))
5173 ret = get_errno(sys_fchmodat(arg1, p, arg3));
5174 unlock_user(p, arg2, 0);
5177 case TARGET_NR_getpriority:
5178 /* libc does special remapping of the return value of
5179 * sys_getpriority() so it's just easiest to call
5180 * sys_getpriority() directly rather than through libc. */
5181 ret = sys_getpriority(arg1, arg2);
5183 case TARGET_NR_setpriority:
5184 ret = get_errno(setpriority(arg1, arg2, arg3));
5186 #ifdef TARGET_NR_profil
5187 case TARGET_NR_profil:
5190 case TARGET_NR_statfs:
5191 if (!(p = lock_user_string(arg1)))
5193 ret = get_errno(statfs(path(p), &stfs));
5194 unlock_user(p, arg1, 0);
5196 if (!is_error(ret)) {
5197 struct target_statfs *target_stfs;
5199 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5201 __put_user(stfs.f_type, &target_stfs->f_type);
5202 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5203 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5204 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5205 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5206 __put_user(stfs.f_files, &target_stfs->f_files);
5207 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5208 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5209 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5210 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5211 unlock_user_struct(target_stfs, arg2, 1);
5214 case TARGET_NR_fstatfs:
5215 ret = get_errno(fstatfs(arg1, &stfs));
5216 goto convert_statfs;
5217 #ifdef TARGET_NR_statfs64
5218 case TARGET_NR_statfs64:
5219 if (!(p = lock_user_string(arg1)))
5221 ret = get_errno(statfs(path(p), &stfs));
5222 unlock_user(p, arg1, 0);
5224 if (!is_error(ret)) {
5225 struct target_statfs64 *target_stfs;
5227 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5229 __put_user(stfs.f_type, &target_stfs->f_type);
5230 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5231 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5232 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5233 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5234 __put_user(stfs.f_files, &target_stfs->f_files);
5235 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5236 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5237 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5238 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5239 unlock_user_struct(target_stfs, arg3, 1);
5242 case TARGET_NR_fstatfs64:
5243 ret = get_errno(fstatfs(arg1, &stfs));
5244 goto convert_statfs64;
5246 #ifdef TARGET_NR_ioperm
5247 case TARGET_NR_ioperm:
5250 #ifdef TARGET_NR_socketcall
5251 case TARGET_NR_socketcall:
5252 ret = do_socketcall(arg1, arg2);
5255 #ifdef TARGET_NR_accept
5256 case TARGET_NR_accept:
5257 ret = do_accept(arg1, arg2, arg3);
5260 #ifdef TARGET_NR_bind
5261 case TARGET_NR_bind:
5262 ret = do_bind(arg1, arg2, arg3);
5265 #ifdef TARGET_NR_connect
5266 case TARGET_NR_connect:
5267 ret = do_connect(arg1, arg2, arg3);
5270 #ifdef TARGET_NR_getpeername
5271 case TARGET_NR_getpeername:
5272 ret = do_getpeername(arg1, arg2, arg3);
5275 #ifdef TARGET_NR_getsockname
5276 case TARGET_NR_getsockname:
5277 ret = do_getsockname(arg1, arg2, arg3);
5280 #ifdef TARGET_NR_getsockopt
5281 case TARGET_NR_getsockopt:
5282 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5285 #ifdef TARGET_NR_listen
5286 case TARGET_NR_listen:
5287 ret = get_errno(listen(arg1, arg2));
5290 #ifdef TARGET_NR_recv
5291 case TARGET_NR_recv:
5292 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
5295 #ifdef TARGET_NR_recvfrom
5296 case TARGET_NR_recvfrom:
5297 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
5300 #ifdef TARGET_NR_recvmsg
5301 case TARGET_NR_recvmsg:
5302 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5305 #ifdef TARGET_NR_send
5306 case TARGET_NR_send:
5307 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
5310 #ifdef TARGET_NR_sendmsg
5311 case TARGET_NR_sendmsg:
5312 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5315 #ifdef TARGET_NR_sendto
5316 case TARGET_NR_sendto:
5317 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
5320 #ifdef TARGET_NR_shutdown
5321 case TARGET_NR_shutdown:
5322 ret = get_errno(shutdown(arg1, arg2));
5325 #ifdef TARGET_NR_socket
5326 case TARGET_NR_socket:
5327 ret = do_socket(arg1, arg2, arg3);
5330 #ifdef TARGET_NR_socketpair
5331 case TARGET_NR_socketpair:
5332 ret = do_socketpair(arg1, arg2, arg3, arg4);
5335 #ifdef TARGET_NR_setsockopt
5336 case TARGET_NR_setsockopt:
5337 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5341 case TARGET_NR_syslog:
5342 if (!(p = lock_user_string(arg2)))
5344 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5345 unlock_user(p, arg2, 0);
5348 case TARGET_NR_setitimer:
5350 struct itimerval value, ovalue, *pvalue;
5354 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5355 || copy_from_user_timeval(&pvalue->it_value,
5356 arg2 + sizeof(struct target_timeval)))
5361 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
5362 if (!is_error(ret) && arg3) {
5363 if (copy_to_user_timeval(arg3,
5364 &ovalue.it_interval)
5365 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5371 case TARGET_NR_getitimer:
5373 struct itimerval value;
5375 ret = get_errno(getitimer(arg1, &value));
5376 if (!is_error(ret) && arg2) {
5377 if (copy_to_user_timeval(arg2,
5379 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5385 case TARGET_NR_stat:
5386 if (!(p = lock_user_string(arg1)))
5388 ret = get_errno(stat(path(p), &st));
5389 unlock_user(p, arg1, 0);
5391 case TARGET_NR_lstat:
5392 if (!(p = lock_user_string(arg1)))
5394 ret = get_errno(lstat(path(p), &st));
5395 unlock_user(p, arg1, 0);
5397 case TARGET_NR_fstat:
5399 ret = get_errno(fstat(arg1, &st));
5401 if (!is_error(ret)) {
5402 struct target_stat *target_st;
5404 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5406 __put_user(st.st_dev, &target_st->st_dev);
5407 __put_user(st.st_ino, &target_st->st_ino);
5408 __put_user(st.st_mode, &target_st->st_mode);
5409 __put_user(st.st_uid, &target_st->st_uid);
5410 __put_user(st.st_gid, &target_st->st_gid);
5411 __put_user(st.st_nlink, &target_st->st_nlink);
5412 __put_user(st.st_rdev, &target_st->st_rdev);
5413 __put_user(st.st_size, &target_st->st_size);
5414 __put_user(st.st_blksize, &target_st->st_blksize);
5415 __put_user(st.st_blocks, &target_st->st_blocks);
5416 __put_user(st.st_atime, &target_st->target_st_atime);
5417 __put_user(st.st_mtime, &target_st->target_st_mtime);
5418 __put_user(st.st_ctime, &target_st->target_st_ctime);
5419 unlock_user_struct(target_st, arg2, 1);
5423 #ifdef TARGET_NR_olduname
5424 case TARGET_NR_olduname:
5427 #ifdef TARGET_NR_iopl
5428 case TARGET_NR_iopl:
5431 case TARGET_NR_vhangup:
5432 ret = get_errno(vhangup());
5434 #ifdef TARGET_NR_idle
5435 case TARGET_NR_idle:
5438 #ifdef TARGET_NR_syscall
5439 case TARGET_NR_syscall:
5440 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5443 case TARGET_NR_wait4:
5446 abi_long status_ptr = arg2;
5447 struct rusage rusage, *rusage_ptr;
5448 abi_ulong target_rusage = arg4;
5450 rusage_ptr = &rusage;
5453 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5454 if (!is_error(ret)) {
5456 status = host_to_target_waitstatus(status);
5457 if (put_user_s32(status, status_ptr))
5461 host_to_target_rusage(target_rusage, &rusage);
5465 #ifdef TARGET_NR_swapoff
5466 case TARGET_NR_swapoff:
5467 if (!(p = lock_user_string(arg1)))
5469 ret = get_errno(swapoff(p));
5470 unlock_user(p, arg1, 0);
5473 case TARGET_NR_sysinfo:
5475 struct target_sysinfo *target_value;
5476 struct sysinfo value;
5477 ret = get_errno(sysinfo(&value));
5478 if (!is_error(ret) && arg1)
5480 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5482 __put_user(value.uptime, &target_value->uptime);
5483 __put_user(value.loads[0], &target_value->loads[0]);
5484 __put_user(value.loads[1], &target_value->loads[1]);
5485 __put_user(value.loads[2], &target_value->loads[2]);
5486 __put_user(value.totalram, &target_value->totalram);
5487 __put_user(value.freeram, &target_value->freeram);
5488 __put_user(value.sharedram, &target_value->sharedram);
5489 __put_user(value.bufferram, &target_value->bufferram);
5490 __put_user(value.totalswap, &target_value->totalswap);
5491 __put_user(value.freeswap, &target_value->freeswap);
5492 __put_user(value.procs, &target_value->procs);
5493 __put_user(value.totalhigh, &target_value->totalhigh);
5494 __put_user(value.freehigh, &target_value->freehigh);
5495 __put_user(value.mem_unit, &target_value->mem_unit);
5496 unlock_user_struct(target_value, arg1, 1);
5500 #ifdef TARGET_NR_ipc
5502 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5505 #ifdef TARGET_NR_semget
5506 case TARGET_NR_semget:
5507 ret = get_errno(semget(arg1, arg2, arg3));
5510 #ifdef TARGET_NR_semop
5511 case TARGET_NR_semop:
5512 ret = get_errno(do_semop(arg1, arg2, arg3));
5515 #ifdef TARGET_NR_semctl
5516 case TARGET_NR_semctl:
5517 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5520 #ifdef TARGET_NR_msgctl
5521 case TARGET_NR_msgctl:
5522 ret = do_msgctl(arg1, arg2, arg3);
5525 #ifdef TARGET_NR_msgget
5526 case TARGET_NR_msgget:
5527 ret = get_errno(msgget(arg1, arg2));
5530 #ifdef TARGET_NR_msgrcv
5531 case TARGET_NR_msgrcv:
5532 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5535 #ifdef TARGET_NR_msgsnd
5536 case TARGET_NR_msgsnd:
5537 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5540 #ifdef TARGET_NR_shmget
5541 case TARGET_NR_shmget:
5542 ret = get_errno(shmget(arg1, arg2, arg3));
5545 #ifdef TARGET_NR_shmctl
5546 case TARGET_NR_shmctl:
5547 ret = do_shmctl(arg1, arg2, arg3);
5550 #ifdef TARGET_NR_shmat
5551 case TARGET_NR_shmat:
5552 ret = do_shmat(arg1, arg2, arg3);
5555 #ifdef TARGET_NR_shmdt
5556 case TARGET_NR_shmdt:
5557 ret = do_shmdt(arg1);
5560 case TARGET_NR_fsync:
5561 ret = get_errno(fsync(arg1));
5563 case TARGET_NR_clone:
5564 #if defined(TARGET_SH4)
5565 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
5566 #elif defined(TARGET_CRIS)
5567 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
5569 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
5572 #ifdef __NR_exit_group
5573 /* new thread calls */
5574 case TARGET_NR_exit_group:
5578 gdb_exit(cpu_env, arg1);
5579 ret = get_errno(exit_group(arg1));
5582 case TARGET_NR_setdomainname:
5583 if (!(p = lock_user_string(arg1)))
5585 ret = get_errno(setdomainname(p, arg2));
5586 unlock_user(p, arg1, 0);
5588 case TARGET_NR_uname:
5589 /* no need to transcode because we use the linux syscall */
5591 struct new_utsname * buf;
5593 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5595 ret = get_errno(sys_uname(buf));
5596 if (!is_error(ret)) {
5597 /* Overrite the native machine name with whatever is being
5599 strcpy (buf->machine, UNAME_MACHINE);
5600 /* Allow the user to override the reported release. */
5601 if (qemu_uname_release && *qemu_uname_release)
5602 strcpy (buf->release, qemu_uname_release);
5604 unlock_user_struct(buf, arg1, 1);
5608 case TARGET_NR_modify_ldt:
5609 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5611 #if !defined(TARGET_X86_64)
5612 case TARGET_NR_vm86old:
5614 case TARGET_NR_vm86:
5615 ret = do_vm86(cpu_env, arg1, arg2);
5619 case TARGET_NR_adjtimex:
5621 #ifdef TARGET_NR_create_module
5622 case TARGET_NR_create_module:
5624 case TARGET_NR_init_module:
5625 case TARGET_NR_delete_module:
5626 #ifdef TARGET_NR_get_kernel_syms
5627 case TARGET_NR_get_kernel_syms:
5630 case TARGET_NR_quotactl:
5632 case TARGET_NR_getpgid:
5633 ret = get_errno(getpgid(arg1));
5635 case TARGET_NR_fchdir:
5636 ret = get_errno(fchdir(arg1));
5638 #ifdef TARGET_NR_bdflush /* not on x86_64 */
5639 case TARGET_NR_bdflush:
5642 #ifdef TARGET_NR_sysfs
5643 case TARGET_NR_sysfs:
5646 case TARGET_NR_personality:
5647 ret = get_errno(personality(arg1));
5649 #ifdef TARGET_NR_afs_syscall
5650 case TARGET_NR_afs_syscall:
5653 #ifdef TARGET_NR__llseek /* Not on alpha */
5654 case TARGET_NR__llseek:
5656 #if defined (__x86_64__)
5657 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
5658 if (put_user_s64(ret, arg4))
5662 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
5663 if (put_user_s64(res, arg4))
5669 case TARGET_NR_getdents:
5670 #if TARGET_ABI_BITS != 32
5672 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
5674 struct target_dirent *target_dirp;
5675 struct linux_dirent *dirp;
5676 abi_long count = arg3;
5678 dirp = malloc(count);
5680 ret = -TARGET_ENOMEM;
5684 ret = get_errno(sys_getdents(arg1, dirp, count));
5685 if (!is_error(ret)) {
5686 struct linux_dirent *de;
5687 struct target_dirent *tde;
5689 int reclen, treclen;
5690 int count1, tnamelen;
5694 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5698 reclen = de->d_reclen;
5699 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
5700 tde->d_reclen = tswap16(treclen);
5701 tde->d_ino = tswapl(de->d_ino);
5702 tde->d_off = tswapl(de->d_off);
5703 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
5706 /* XXX: may not be correct */
5707 pstrcpy(tde->d_name, tnamelen, de->d_name);
5708 de = (struct linux_dirent *)((char *)de + reclen);
5710 tde = (struct target_dirent *)((char *)tde + treclen);
5714 unlock_user(target_dirp, arg2, ret);
5720 struct linux_dirent *dirp;
5721 abi_long count = arg3;
5723 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5725 ret = get_errno(sys_getdents(arg1, dirp, count));
5726 if (!is_error(ret)) {
5727 struct linux_dirent *de;
5732 reclen = de->d_reclen;
5735 de->d_reclen = tswap16(reclen);
5736 tswapls(&de->d_ino);
5737 tswapls(&de->d_off);
5738 de = (struct linux_dirent *)((char *)de + reclen);
5742 unlock_user(dirp, arg2, ret);
5746 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
5747 case TARGET_NR_getdents64:
5749 struct linux_dirent64 *dirp;
5750 abi_long count = arg3;
5751 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5753 ret = get_errno(sys_getdents64(arg1, dirp, count));
5754 if (!is_error(ret)) {
5755 struct linux_dirent64 *de;
5760 reclen = de->d_reclen;
5763 de->d_reclen = tswap16(reclen);
5764 tswap64s((uint64_t *)&de->d_ino);
5765 tswap64s((uint64_t *)&de->d_off);
5766 de = (struct linux_dirent64 *)((char *)de + reclen);
5770 unlock_user(dirp, arg2, ret);
5773 #endif /* TARGET_NR_getdents64 */
5774 #ifdef TARGET_NR__newselect
5775 case TARGET_NR__newselect:
5776 ret = do_select(arg1, arg2, arg3, arg4, arg5);
5779 #ifdef TARGET_NR_poll
5780 case TARGET_NR_poll:
5782 struct target_pollfd *target_pfd;
5783 unsigned int nfds = arg2;
5788 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
5791 pfd = alloca(sizeof(struct pollfd) * nfds);
5792 for(i = 0; i < nfds; i++) {
5793 pfd[i].fd = tswap32(target_pfd[i].fd);
5794 pfd[i].events = tswap16(target_pfd[i].events);
5796 ret = get_errno(poll(pfd, nfds, timeout));
5797 if (!is_error(ret)) {
5798 for(i = 0; i < nfds; i++) {
5799 target_pfd[i].revents = tswap16(pfd[i].revents);
5801 ret += nfds * (sizeof(struct target_pollfd)
5802 - sizeof(struct pollfd));
5804 unlock_user(target_pfd, arg1, ret);
5808 case TARGET_NR_flock:
5809 /* NOTE: the flock constant seems to be the same for every
5811 ret = get_errno(flock(arg1, arg2));
5813 case TARGET_NR_readv:
5818 vec = alloca(count * sizeof(struct iovec));
5819 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
5821 ret = get_errno(readv(arg1, vec, count));
5822 unlock_iovec(vec, arg2, count, 1);
5825 case TARGET_NR_writev:
5830 vec = alloca(count * sizeof(struct iovec));
5831 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
5833 ret = get_errno(writev(arg1, vec, count));
5834 unlock_iovec(vec, arg2, count, 0);
5837 case TARGET_NR_getsid:
5838 ret = get_errno(getsid(arg1));
5840 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
5841 case TARGET_NR_fdatasync:
5842 ret = get_errno(fdatasync(arg1));
5845 case TARGET_NR__sysctl:
5846 /* We don't implement this, but ENOTDIR is always a safe
5848 ret = -TARGET_ENOTDIR;
5850 case TARGET_NR_sched_setparam:
5852 struct sched_param *target_schp;
5853 struct sched_param schp;
5855 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
5857 schp.sched_priority = tswap32(target_schp->sched_priority);
5858 unlock_user_struct(target_schp, arg2, 0);
5859 ret = get_errno(sched_setparam(arg1, &schp));
5862 case TARGET_NR_sched_getparam:
5864 struct sched_param *target_schp;
5865 struct sched_param schp;
5866 ret = get_errno(sched_getparam(arg1, &schp));
5867 if (!is_error(ret)) {
5868 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
5870 target_schp->sched_priority = tswap32(schp.sched_priority);
5871 unlock_user_struct(target_schp, arg2, 1);
5875 case TARGET_NR_sched_setscheduler:
5877 struct sched_param *target_schp;
5878 struct sched_param schp;
5879 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
5881 schp.sched_priority = tswap32(target_schp->sched_priority);
5882 unlock_user_struct(target_schp, arg3, 0);
5883 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
5886 case TARGET_NR_sched_getscheduler:
5887 ret = get_errno(sched_getscheduler(arg1));
5889 case TARGET_NR_sched_yield:
5890 ret = get_errno(sched_yield());
5892 case TARGET_NR_sched_get_priority_max:
5893 ret = get_errno(sched_get_priority_max(arg1));
5895 case TARGET_NR_sched_get_priority_min:
5896 ret = get_errno(sched_get_priority_min(arg1));
5898 case TARGET_NR_sched_rr_get_interval:
5901 ret = get_errno(sched_rr_get_interval(arg1, &ts));
5902 if (!is_error(ret)) {
5903 host_to_target_timespec(arg2, &ts);
5907 case TARGET_NR_nanosleep:
5909 struct timespec req, rem;
5910 target_to_host_timespec(&req, arg1);
5911 ret = get_errno(nanosleep(&req, &rem));
5912 if (is_error(ret) && arg2) {
5913 host_to_target_timespec(arg2, &rem);
5917 #ifdef TARGET_NR_query_module
5918 case TARGET_NR_query_module:
5921 #ifdef TARGET_NR_nfsservctl
5922 case TARGET_NR_nfsservctl:
5925 case TARGET_NR_prctl:
5928 case PR_GET_PDEATHSIG:
5931 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
5932 if (!is_error(ret) && arg2
5933 && put_user_ual(deathsig, arg2))
5938 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
5942 #ifdef TARGET_NR_arch_prctl
5943 case TARGET_NR_arch_prctl:
5944 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
5945 ret = do_arch_prctl(cpu_env, arg1, arg2);
5951 #ifdef TARGET_NR_pread
5952 case TARGET_NR_pread:
5954 if (((CPUARMState *)cpu_env)->eabi)
5957 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5959 ret = get_errno(pread(arg1, p, arg3, arg4));
5960 unlock_user(p, arg2, ret);
5962 case TARGET_NR_pwrite:
5964 if (((CPUARMState *)cpu_env)->eabi)
5967 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5969 ret = get_errno(pwrite(arg1, p, arg3, arg4));
5970 unlock_user(p, arg2, 0);
5973 #ifdef TARGET_NR_pread64
5974 case TARGET_NR_pread64:
5975 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5977 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
5978 unlock_user(p, arg2, ret);
5980 case TARGET_NR_pwrite64:
5981 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5983 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
5984 unlock_user(p, arg2, 0);
5987 case TARGET_NR_getcwd:
5988 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
5990 ret = get_errno(sys_getcwd1(p, arg2));
5991 unlock_user(p, arg1, ret);
5993 case TARGET_NR_capget:
5995 case TARGET_NR_capset:
5997 case TARGET_NR_sigaltstack:
5998 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
5999 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
6000 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
6005 case TARGET_NR_sendfile:
6007 #ifdef TARGET_NR_getpmsg
6008 case TARGET_NR_getpmsg:
6011 #ifdef TARGET_NR_putpmsg
6012 case TARGET_NR_putpmsg:
6015 #ifdef TARGET_NR_vfork
6016 case TARGET_NR_vfork:
6017 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6021 #ifdef TARGET_NR_ugetrlimit
6022 case TARGET_NR_ugetrlimit:
6025 ret = get_errno(getrlimit(arg1, &rlim));
6026 if (!is_error(ret)) {
6027 struct target_rlimit *target_rlim;
6028 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6030 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
6031 target_rlim->rlim_max = tswapl(rlim.rlim_max);
6032 unlock_user_struct(target_rlim, arg2, 1);
6037 #ifdef TARGET_NR_truncate64
6038 case TARGET_NR_truncate64:
6039 if (!(p = lock_user_string(arg1)))
6041 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6042 unlock_user(p, arg1, 0);
6045 #ifdef TARGET_NR_ftruncate64
6046 case TARGET_NR_ftruncate64:
6047 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
6050 #ifdef TARGET_NR_stat64
6051 case TARGET_NR_stat64:
6052 if (!(p = lock_user_string(arg1)))
6054 ret = get_errno(stat(path(p), &st));
6055 unlock_user(p, arg1, 0);
6057 ret = host_to_target_stat64(cpu_env, arg2, &st);
6060 #ifdef TARGET_NR_lstat64
6061 case TARGET_NR_lstat64:
6062 if (!(p = lock_user_string(arg1)))
6064 ret = get_errno(lstat(path(p), &st));
6065 unlock_user(p, arg1, 0);
6067 ret = host_to_target_stat64(cpu_env, arg2, &st);
6070 #ifdef TARGET_NR_fstat64
6071 case TARGET_NR_fstat64:
6072 ret = get_errno(fstat(arg1, &st));
6074 ret = host_to_target_stat64(cpu_env, arg2, &st);
6077 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6078 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6079 #ifdef TARGET_NR_fstatat64
6080 case TARGET_NR_fstatat64:
6082 #ifdef TARGET_NR_newfstatat
6083 case TARGET_NR_newfstatat:
6085 if (!(p = lock_user_string(arg2)))
6087 #ifdef __NR_fstatat64
6088 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
6090 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6093 ret = host_to_target_stat64(cpu_env, arg3, &st);
6097 case TARGET_NR_lchown:
6098 if (!(p = lock_user_string(arg1)))
6100 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6101 unlock_user(p, arg1, 0);
6103 case TARGET_NR_getuid:
6104 ret = get_errno(high2lowuid(getuid()));
6106 case TARGET_NR_getgid:
6107 ret = get_errno(high2lowgid(getgid()));
6109 case TARGET_NR_geteuid:
6110 ret = get_errno(high2lowuid(geteuid()));
6112 case TARGET_NR_getegid:
6113 ret = get_errno(high2lowgid(getegid()));
6115 case TARGET_NR_setreuid:
6116 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6118 case TARGET_NR_setregid:
6119 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6121 case TARGET_NR_getgroups:
6123 int gidsetsize = arg1;
6124 uint16_t *target_grouplist;
6128 grouplist = alloca(gidsetsize * sizeof(gid_t));
6129 ret = get_errno(getgroups(gidsetsize, grouplist));
6130 if (gidsetsize == 0)
6132 if (!is_error(ret)) {
6133 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6134 if (!target_grouplist)
6136 for(i = 0;i < ret; i++)
6137 target_grouplist[i] = tswap16(grouplist[i]);
6138 unlock_user(target_grouplist, arg2, gidsetsize * 2);
6142 case TARGET_NR_setgroups:
6144 int gidsetsize = arg1;
6145 uint16_t *target_grouplist;
6149 grouplist = alloca(gidsetsize * sizeof(gid_t));
6150 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6151 if (!target_grouplist) {
6152 ret = -TARGET_EFAULT;
6155 for(i = 0;i < gidsetsize; i++)
6156 grouplist[i] = tswap16(target_grouplist[i]);
6157 unlock_user(target_grouplist, arg2, 0);
6158 ret = get_errno(setgroups(gidsetsize, grouplist));
6161 case TARGET_NR_fchown:
6162 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6164 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6165 case TARGET_NR_fchownat:
6166 if (!(p = lock_user_string(arg2)))
6168 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6169 unlock_user(p, arg2, 0);
6172 #ifdef TARGET_NR_setresuid
6173 case TARGET_NR_setresuid:
6174 ret = get_errno(setresuid(low2highuid(arg1),
6176 low2highuid(arg3)));
6179 #ifdef TARGET_NR_getresuid
6180 case TARGET_NR_getresuid:
6182 uid_t ruid, euid, suid;
6183 ret = get_errno(getresuid(&ruid, &euid, &suid));
6184 if (!is_error(ret)) {
6185 if (put_user_u16(high2lowuid(ruid), arg1)
6186 || put_user_u16(high2lowuid(euid), arg2)
6187 || put_user_u16(high2lowuid(suid), arg3))
6193 #ifdef TARGET_NR_getresgid
6194 case TARGET_NR_setresgid:
6195 ret = get_errno(setresgid(low2highgid(arg1),
6197 low2highgid(arg3)));
6200 #ifdef TARGET_NR_getresgid
6201 case TARGET_NR_getresgid:
6203 gid_t rgid, egid, sgid;
6204 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6205 if (!is_error(ret)) {
6206 if (put_user_u16(high2lowgid(rgid), arg1)
6207 || put_user_u16(high2lowgid(egid), arg2)
6208 || put_user_u16(high2lowgid(sgid), arg3))
6214 case TARGET_NR_chown:
6215 if (!(p = lock_user_string(arg1)))
6217 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6218 unlock_user(p, arg1, 0);
6220 case TARGET_NR_setuid:
6221 ret = get_errno(setuid(low2highuid(arg1)));
6223 case TARGET_NR_setgid:
6224 ret = get_errno(setgid(low2highgid(arg1)));
6226 case TARGET_NR_setfsuid:
6227 ret = get_errno(setfsuid(arg1));
6229 case TARGET_NR_setfsgid:
6230 ret = get_errno(setfsgid(arg1));
6232 #endif /* USE_UID16 */
6234 #ifdef TARGET_NR_lchown32
6235 case TARGET_NR_lchown32:
6236 if (!(p = lock_user_string(arg1)))
6238 ret = get_errno(lchown(p, arg2, arg3));
6239 unlock_user(p, arg1, 0);
6242 #ifdef TARGET_NR_getuid32
6243 case TARGET_NR_getuid32:
6244 ret = get_errno(getuid());
6248 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6249 /* Alpha specific */
6250 case TARGET_NR_getxuid:
6254 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6256 ret = get_errno(getuid());
6259 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6260 /* Alpha specific */
6261 case TARGET_NR_getxgid:
6265 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6267 ret = get_errno(getgid());
6271 #ifdef TARGET_NR_getgid32
6272 case TARGET_NR_getgid32:
6273 ret = get_errno(getgid());
6276 #ifdef TARGET_NR_geteuid32
6277 case TARGET_NR_geteuid32:
6278 ret = get_errno(geteuid());
6281 #ifdef TARGET_NR_getegid32
6282 case TARGET_NR_getegid32:
6283 ret = get_errno(getegid());
6286 #ifdef TARGET_NR_setreuid32
6287 case TARGET_NR_setreuid32:
6288 ret = get_errno(setreuid(arg1, arg2));
6291 #ifdef TARGET_NR_setregid32
6292 case TARGET_NR_setregid32:
6293 ret = get_errno(setregid(arg1, arg2));
6296 #ifdef TARGET_NR_getgroups32
6297 case TARGET_NR_getgroups32:
6299 int gidsetsize = arg1;
6300 uint32_t *target_grouplist;
6304 grouplist = alloca(gidsetsize * sizeof(gid_t));
6305 ret = get_errno(getgroups(gidsetsize, grouplist));
6306 if (gidsetsize == 0)
6308 if (!is_error(ret)) {
6309 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6310 if (!target_grouplist) {
6311 ret = -TARGET_EFAULT;
6314 for(i = 0;i < ret; i++)
6315 target_grouplist[i] = tswap32(grouplist[i]);
6316 unlock_user(target_grouplist, arg2, gidsetsize * 4);
6321 #ifdef TARGET_NR_setgroups32
6322 case TARGET_NR_setgroups32:
6324 int gidsetsize = arg1;
6325 uint32_t *target_grouplist;
6329 grouplist = alloca(gidsetsize * sizeof(gid_t));
6330 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6331 if (!target_grouplist) {
6332 ret = -TARGET_EFAULT;
6335 for(i = 0;i < gidsetsize; i++)
6336 grouplist[i] = tswap32(target_grouplist[i]);
6337 unlock_user(target_grouplist, arg2, 0);
6338 ret = get_errno(setgroups(gidsetsize, grouplist));
6342 #ifdef TARGET_NR_fchown32
6343 case TARGET_NR_fchown32:
6344 ret = get_errno(fchown(arg1, arg2, arg3));
6347 #ifdef TARGET_NR_setresuid32
6348 case TARGET_NR_setresuid32:
6349 ret = get_errno(setresuid(arg1, arg2, arg3));
6352 #ifdef TARGET_NR_getresuid32
6353 case TARGET_NR_getresuid32:
6355 uid_t ruid, euid, suid;
6356 ret = get_errno(getresuid(&ruid, &euid, &suid));
6357 if (!is_error(ret)) {
6358 if (put_user_u32(ruid, arg1)
6359 || put_user_u32(euid, arg2)
6360 || put_user_u32(suid, arg3))
6366 #ifdef TARGET_NR_setresgid32
6367 case TARGET_NR_setresgid32:
6368 ret = get_errno(setresgid(arg1, arg2, arg3));
6371 #ifdef TARGET_NR_getresgid32
6372 case TARGET_NR_getresgid32:
6374 gid_t rgid, egid, sgid;
6375 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6376 if (!is_error(ret)) {
6377 if (put_user_u32(rgid, arg1)
6378 || put_user_u32(egid, arg2)
6379 || put_user_u32(sgid, arg3))
6385 #ifdef TARGET_NR_chown32
6386 case TARGET_NR_chown32:
6387 if (!(p = lock_user_string(arg1)))
6389 ret = get_errno(chown(p, arg2, arg3));
6390 unlock_user(p, arg1, 0);
6393 #ifdef TARGET_NR_setuid32
6394 case TARGET_NR_setuid32:
6395 ret = get_errno(setuid(arg1));
6398 #ifdef TARGET_NR_setgid32
6399 case TARGET_NR_setgid32:
6400 ret = get_errno(setgid(arg1));
6403 #ifdef TARGET_NR_setfsuid32
6404 case TARGET_NR_setfsuid32:
6405 ret = get_errno(setfsuid(arg1));
6408 #ifdef TARGET_NR_setfsgid32
6409 case TARGET_NR_setfsgid32:
6410 ret = get_errno(setfsgid(arg1));
6414 case TARGET_NR_pivot_root:
6416 #ifdef TARGET_NR_mincore
6417 case TARGET_NR_mincore:
6420 ret = -TARGET_EFAULT;
6421 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6423 if (!(p = lock_user_string(arg3)))
6425 ret = get_errno(mincore(a, arg2, p));
6426 unlock_user(p, arg3, ret);
6428 unlock_user(a, arg1, 0);
6432 #ifdef TARGET_NR_arm_fadvise64_64
6433 case TARGET_NR_arm_fadvise64_64:
6436 * arm_fadvise64_64 looks like fadvise64_64 but
6437 * with different argument order
6445 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64)
6446 #ifdef TARGET_NR_fadvise64_64
6447 case TARGET_NR_fadvise64_64:
6449 /* This is a hint, so ignoring and returning success is ok. */
6453 #ifdef TARGET_NR_madvise
6454 case TARGET_NR_madvise:
6455 /* A straight passthrough may not be safe because qemu sometimes
6456 turns private flie-backed mappings into anonymous mappings.
6457 This will break MADV_DONTNEED.
6458 This is a hint, so ignoring and returning success is ok. */
6462 #if TARGET_ABI_BITS == 32
6463 case TARGET_NR_fcntl64:
6467 struct target_flock64 *target_fl;
6469 struct target_eabi_flock64 *target_efl;
6473 case TARGET_F_GETLK64:
6476 case TARGET_F_SETLK64:
6479 case TARGET_F_SETLKW64:
6488 case TARGET_F_GETLK64:
6490 if (((CPUARMState *)cpu_env)->eabi) {
6491 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6493 fl.l_type = tswap16(target_efl->l_type);
6494 fl.l_whence = tswap16(target_efl->l_whence);
6495 fl.l_start = tswap64(target_efl->l_start);
6496 fl.l_len = tswap64(target_efl->l_len);
6497 fl.l_pid = tswapl(target_efl->l_pid);
6498 unlock_user_struct(target_efl, arg3, 0);
6502 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6504 fl.l_type = tswap16(target_fl->l_type);
6505 fl.l_whence = tswap16(target_fl->l_whence);
6506 fl.l_start = tswap64(target_fl->l_start);
6507 fl.l_len = tswap64(target_fl->l_len);
6508 fl.l_pid = tswapl(target_fl->l_pid);
6509 unlock_user_struct(target_fl, arg3, 0);
6511 ret = get_errno(fcntl(arg1, cmd, &fl));
6514 if (((CPUARMState *)cpu_env)->eabi) {
6515 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6517 target_efl->l_type = tswap16(fl.l_type);
6518 target_efl->l_whence = tswap16(fl.l_whence);
6519 target_efl->l_start = tswap64(fl.l_start);
6520 target_efl->l_len = tswap64(fl.l_len);
6521 target_efl->l_pid = tswapl(fl.l_pid);
6522 unlock_user_struct(target_efl, arg3, 1);
6526 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6528 target_fl->l_type = tswap16(fl.l_type);
6529 target_fl->l_whence = tswap16(fl.l_whence);
6530 target_fl->l_start = tswap64(fl.l_start);
6531 target_fl->l_len = tswap64(fl.l_len);
6532 target_fl->l_pid = tswapl(fl.l_pid);
6533 unlock_user_struct(target_fl, arg3, 1);
6538 case TARGET_F_SETLK64:
6539 case TARGET_F_SETLKW64:
6541 if (((CPUARMState *)cpu_env)->eabi) {
6542 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6544 fl.l_type = tswap16(target_efl->l_type);
6545 fl.l_whence = tswap16(target_efl->l_whence);
6546 fl.l_start = tswap64(target_efl->l_start);
6547 fl.l_len = tswap64(target_efl->l_len);
6548 fl.l_pid = tswapl(target_efl->l_pid);
6549 unlock_user_struct(target_efl, arg3, 0);
6553 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6555 fl.l_type = tswap16(target_fl->l_type);
6556 fl.l_whence = tswap16(target_fl->l_whence);
6557 fl.l_start = tswap64(target_fl->l_start);
6558 fl.l_len = tswap64(target_fl->l_len);
6559 fl.l_pid = tswapl(target_fl->l_pid);
6560 unlock_user_struct(target_fl, arg3, 0);
6562 ret = get_errno(fcntl(arg1, cmd, &fl));
6565 ret = do_fcntl(arg1, arg2, arg3);
6571 #ifdef TARGET_NR_cacheflush
6572 case TARGET_NR_cacheflush:
6573 /* self-modifying code is handled automatically, so nothing needed */
6577 #ifdef TARGET_NR_security
6578 case TARGET_NR_security:
6581 #ifdef TARGET_NR_getpagesize
6582 case TARGET_NR_getpagesize:
6583 ret = TARGET_PAGE_SIZE;
6586 case TARGET_NR_gettid:
6587 ret = get_errno(gettid());
6589 #ifdef TARGET_NR_readahead
6590 case TARGET_NR_readahead:
6591 #if TARGET_ABI_BITS == 32
6593 if (((CPUARMState *)cpu_env)->eabi)
6600 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
6602 ret = get_errno(readahead(arg1, arg2, arg3));
6606 #ifdef TARGET_NR_setxattr
6607 case TARGET_NR_setxattr:
6608 case TARGET_NR_lsetxattr:
6609 case TARGET_NR_fsetxattr:
6610 case TARGET_NR_getxattr:
6611 case TARGET_NR_lgetxattr:
6612 case TARGET_NR_fgetxattr:
6613 case TARGET_NR_listxattr:
6614 case TARGET_NR_llistxattr:
6615 case TARGET_NR_flistxattr:
6616 case TARGET_NR_removexattr:
6617 case TARGET_NR_lremovexattr:
6618 case TARGET_NR_fremovexattr:
6619 ret = -TARGET_EOPNOTSUPP;
6622 #ifdef TARGET_NR_set_thread_area
6623 case TARGET_NR_set_thread_area:
6624 #if defined(TARGET_MIPS)
6625 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
6628 #elif defined(TARGET_CRIS)
6630 ret = -TARGET_EINVAL;
6632 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
6636 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
6637 ret = do_set_thread_area(cpu_env, arg1);
6640 goto unimplemented_nowarn;
6643 #ifdef TARGET_NR_get_thread_area
6644 case TARGET_NR_get_thread_area:
6645 #if defined(TARGET_I386) && defined(TARGET_ABI32)
6646 ret = do_get_thread_area(cpu_env, arg1);
6648 goto unimplemented_nowarn;
6651 #ifdef TARGET_NR_getdomainname
6652 case TARGET_NR_getdomainname:
6653 goto unimplemented_nowarn;
6656 #ifdef TARGET_NR_clock_gettime
6657 case TARGET_NR_clock_gettime:
6660 ret = get_errno(clock_gettime(arg1, &ts));
6661 if (!is_error(ret)) {
6662 host_to_target_timespec(arg2, &ts);
6667 #ifdef TARGET_NR_clock_getres
6668 case TARGET_NR_clock_getres:
6671 ret = get_errno(clock_getres(arg1, &ts));
6672 if (!is_error(ret)) {
6673 host_to_target_timespec(arg2, &ts);
6678 #ifdef TARGET_NR_clock_nanosleep
6679 case TARGET_NR_clock_nanosleep:
6682 target_to_host_timespec(&ts, arg3);
6683 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
6685 host_to_target_timespec(arg4, &ts);
6690 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
6691 case TARGET_NR_set_tid_address:
6692 ret = get_errno(set_tid_address((int *)g2h(arg1)));
6696 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
6697 case TARGET_NR_tkill:
6698 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
6702 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
6703 case TARGET_NR_tgkill:
6704 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
6705 target_to_host_signal(arg3)));
6709 #ifdef TARGET_NR_set_robust_list
6710 case TARGET_NR_set_robust_list:
6711 goto unimplemented_nowarn;
6714 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
6715 case TARGET_NR_utimensat:
6717 struct timespec *tsp, ts[2];
6721 target_to_host_timespec(ts, arg3);
6722 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
6726 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
6728 if (!(p = lock_user_string(arg2))) {
6729 ret = -TARGET_EFAULT;
6732 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
6733 unlock_user(p, arg2, 0);
6738 #if defined(USE_NPTL)
6739 case TARGET_NR_futex:
6740 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
6743 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
6744 case TARGET_NR_inotify_init:
6745 ret = get_errno(sys_inotify_init());
6748 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
6749 case TARGET_NR_inotify_add_watch:
6750 p = lock_user_string(arg2);
6751 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
6752 unlock_user(p, arg2, 0);
6755 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
6756 case TARGET_NR_inotify_rm_watch:
6757 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
6761 #ifdef TARGET_NR_mq_open
6762 case TARGET_NR_mq_open:
6764 struct mq_attr posix_mq_attr;
6766 p = lock_user_string(arg1 - 1);
6768 copy_from_user_mq_attr (&posix_mq_attr, arg4);
6769 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
6770 unlock_user (p, arg1, 0);
6774 case TARGET_NR_mq_unlink:
6775 p = lock_user_string(arg1 - 1);
6776 ret = get_errno(mq_unlink(p));
6777 unlock_user (p, arg1, 0);
6780 case TARGET_NR_mq_timedsend:
6784 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6786 target_to_host_timespec(&ts, arg5);
6787 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
6788 host_to_target_timespec(arg5, &ts);
6791 ret = get_errno(mq_send(arg1, p, arg3, arg4));
6792 unlock_user (p, arg2, arg3);
6796 case TARGET_NR_mq_timedreceive:
6801 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6803 target_to_host_timespec(&ts, arg5);
6804 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
6805 host_to_target_timespec(arg5, &ts);
6808 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
6809 unlock_user (p, arg2, arg3);
6811 put_user_u32(prio, arg4);
6815 /* Not implemented for now... */
6816 /* case TARGET_NR_mq_notify: */
6819 case TARGET_NR_mq_getsetattr:
6821 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
6824 ret = mq_getattr(arg1, &posix_mq_attr_out);
6825 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
6828 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
6829 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
6838 gemu_log("qemu: Unsupported syscall: %d\n", num);
6839 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
6840 unimplemented_nowarn:
6842 ret = -TARGET_ENOSYS;
6847 gemu_log(" = %ld\n", ret);
6850 print_syscall_ret(num, ret);
6853 ret = -TARGET_EFAULT;