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 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
97 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
98 /* 16 bit uid wrappers emulation */
102 //#include <linux/msdos_fs.h>
103 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
104 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
115 #define _syscall0(type,name) \
116 static type name (void) \
118 return syscall(__NR_##name); \
121 #define _syscall1(type,name,type1,arg1) \
122 static type name (type1 arg1) \
124 return syscall(__NR_##name, arg1); \
127 #define _syscall2(type,name,type1,arg1,type2,arg2) \
128 static type name (type1 arg1,type2 arg2) \
130 return syscall(__NR_##name, arg1, arg2); \
133 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
134 static type name (type1 arg1,type2 arg2,type3 arg3) \
136 return syscall(__NR_##name, arg1, arg2, arg3); \
139 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
140 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
142 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
145 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
147 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
149 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
153 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
154 type5,arg5,type6,arg6) \
155 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
158 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
162 #define __NR_sys_uname __NR_uname
163 #define __NR_sys_faccessat __NR_faccessat
164 #define __NR_sys_fchmodat __NR_fchmodat
165 #define __NR_sys_fchownat __NR_fchownat
166 #define __NR_sys_fstatat64 __NR_fstatat64
167 #define __NR_sys_futimesat __NR_futimesat
168 #define __NR_sys_getcwd1 __NR_getcwd
169 #define __NR_sys_getdents __NR_getdents
170 #define __NR_sys_getdents64 __NR_getdents64
171 #define __NR_sys_getpriority __NR_getpriority
172 #define __NR_sys_linkat __NR_linkat
173 #define __NR_sys_mkdirat __NR_mkdirat
174 #define __NR_sys_mknodat __NR_mknodat
175 #define __NR_sys_newfstatat __NR_newfstatat
176 #define __NR_sys_openat __NR_openat
177 #define __NR_sys_readlinkat __NR_readlinkat
178 #define __NR_sys_renameat __NR_renameat
179 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
180 #define __NR_sys_symlinkat __NR_symlinkat
181 #define __NR_sys_syslog __NR_syslog
182 #define __NR_sys_tgkill __NR_tgkill
183 #define __NR_sys_tkill __NR_tkill
184 #define __NR_sys_unlinkat __NR_unlinkat
185 #define __NR_sys_utimensat __NR_utimensat
186 #define __NR_sys_futex __NR_futex
187 #define __NR_sys_inotify_init __NR_inotify_init
188 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
189 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
191 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
192 #define __NR__llseek __NR_lseek
196 _syscall0(int, gettid)
198 /* This is a replacement for the host gettid() and must return a host
200 static int gettid(void) {
204 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
205 defined(__NR_fstatat64)
206 _syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
207 struct stat *,buf,int,flags)
209 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
210 _syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
211 const struct timeval *,times)
213 #if TARGET_ABI_BITS == 32
214 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
216 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
217 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
219 _syscall2(int, sys_getpriority, int, which, int, who);
220 #if !defined (__x86_64__)
221 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
222 loff_t *, res, uint, wh);
224 #if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
225 defined(__NR_newfstatat)
226 _syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
227 struct stat *,buf,int,flags)
229 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
230 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
231 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
232 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
234 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
235 _syscall2(int,sys_tkill,int,tid,int,sig)
237 #ifdef __NR_exit_group
238 _syscall1(int,exit_group,int,error_code)
240 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
241 _syscall1(int,set_tid_address,int *,tidptr)
243 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
244 _syscall0(int,sys_inotify_init)
246 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
247 _syscall3(int,sys_inotify_add_watch,int,fd,const char *,pathname,uint32_t,mask)
249 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
250 _syscall2(int,sys_inotify_rm_watch,int,fd,uint32_t,wd)
252 #if defined(USE_NPTL)
253 #if defined(TARGET_NR_futex) && defined(__NR_futex)
254 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
255 const struct timespec *,timeout,int *,uaddr2,int,val3)
259 static bitmask_transtbl fcntl_flags_tbl[] = {
260 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
261 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
262 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
263 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
264 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
265 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
266 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
267 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
268 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
269 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
270 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
271 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
272 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
273 #if defined(O_DIRECT)
274 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
280 sys_uname(struct new_utsname *buf)
282 struct utsname uts_buf;
284 if (uname(&uts_buf) < 0)
288 * Just in case these have some differences, we
289 * translate utsname to new_utsname (which is the
290 * struct linux kernel uses).
293 #define COPY_UTSNAME_FIELD(dest, src) \
295 /* __NEW_UTS_LEN doesn't include terminating null */ \
296 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
297 (dest)[__NEW_UTS_LEN] = '\0'; \
300 bzero(buf, sizeof (*buf));
301 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
302 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
303 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
304 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
305 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
307 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
311 #undef COPY_UTSNAME_FIELD
315 sys_getcwd1(char *buf, size_t size)
317 if (getcwd(buf, size) == NULL) {
318 /* getcwd() sets errno */
327 * Host system seems to have atfile syscall stubs available. We
328 * now enable them one by one as specified by target syscall_nr.h.
331 #ifdef TARGET_NR_openat
333 sys_openat(int dirfd, const char *pathname, int flags, ...)
336 * open(2) has extra parameter 'mode' when called with
339 if ((flags & O_CREAT) != 0) {
344 * Get the 'mode' parameter and translate it to
348 mode = va_arg(ap, mode_t);
349 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
352 return (openat(dirfd, pathname, flags, mode));
354 return (openat(dirfd, pathname, flags));
358 #ifdef TARGET_NR_mkdirat
360 sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
362 return (mkdirat(dirfd, pathname, mode));
366 #ifdef TARGET_NR_mknodat
368 sys_mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev)
370 return (mknodat(dirfd, pathname, mode, dev));
374 #ifdef TARGET_NR_fchownat
376 sys_fchownat(int dirfd, const char *pathname, uid_t owner,
377 gid_t group, int flags)
379 return (fchownat(dirfd, pathname, owner, group, flags));
383 #ifdef TARGET_NR_fstatat
385 sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
388 return (fstatat64(dirfd, pathname, buf, flags));
392 #ifdef TARGET_NR_unlinkat
394 sys_unlinkat(int dirfd, const char *pathname, int flags)
396 return (unlinkat(dirfd, pathname, flags));
400 #ifdef TARGET_NR_renameat
402 sys_renameat(int olddirfd, const char *oldpath,
403 int newdirfd, const char *newpath)
405 return (renameat(olddirfd, oldpath, newdirfd, newpath));
409 #ifdef TARGET_NR_linkat
411 sys_linkat(int olddirfd, const char *oldpath,
412 int newdirfd, const char *newpath, int flags)
414 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
418 #ifdef TARGET_NR_symlinkat
420 sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
422 return (symlinkat(oldpath, newdirfd, newpath));
426 #ifdef TARGET_NR_readlinkat
428 sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
430 return (readlinkat(dirfd, pathname, buf, bufsiz));
434 #ifdef TARGET_NR_fchmodat
436 sys_fchmodat(int dirfd, const char *pathname, mode_t mode, int flags)
438 return (fchmodat(dirfd, pathname, mode, flags));
442 #ifdef TARGET_NR_faccessat
444 sys_faccessat(int dirfd, const char *pathname, int mode, int flags)
446 return (faccessat(dirfd, pathname, mode, flags));
450 #ifdef TARGET_NR_utimensat
452 sys_utimensat(int dirfd, const char *pathname,
453 const struct timespec times[2], int flags)
455 return (utimensat(dirfd, pathname, times, flags));
459 #else /* !CONFIG_ATFILE */
462 * Host system doesn't have these available so we don't try
466 #undef TARGET_NR_openat
467 #undef TARGET_NR_mkdirat
468 #undef TARGET_NR_mknodat
469 #undef TARGET_NR_fchownat
470 #undef TARGET_NR_fstatat
471 #undef TARGET_NR_unlinkat
472 #undef TARGET_NR_renameat
473 #undef TARGET_NR_linkat
474 #undef TARGET_NR_symlinkat
475 #undef TARGET_NR_readlinkat
476 #undef TARGET_NR_fchmodat
477 #undef TARGET_NR_faccessat
478 #undef TARGET_NR_utimensat
480 #endif /* CONFIG_ATFILE */
483 extern int personality(int);
484 extern int flock(int, int);
485 extern int setfsuid(int);
486 extern int setfsgid(int);
487 extern int setgroups(int, gid_t *);
489 #define ERRNO_TABLE_SIZE 1200
491 /* target_to_host_errno_table[] is initialized from
492 * host_to_target_errno_table[] in syscall_init(). */
493 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
497 * This list is the union of errno values overridden in asm-<arch>/errno.h
498 * minus the errnos that are not actually generic to all archs.
500 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
501 [EIDRM] = TARGET_EIDRM,
502 [ECHRNG] = TARGET_ECHRNG,
503 [EL2NSYNC] = TARGET_EL2NSYNC,
504 [EL3HLT] = TARGET_EL3HLT,
505 [EL3RST] = TARGET_EL3RST,
506 [ELNRNG] = TARGET_ELNRNG,
507 [EUNATCH] = TARGET_EUNATCH,
508 [ENOCSI] = TARGET_ENOCSI,
509 [EL2HLT] = TARGET_EL2HLT,
510 [EDEADLK] = TARGET_EDEADLK,
511 [ENOLCK] = TARGET_ENOLCK,
512 [EBADE] = TARGET_EBADE,
513 [EBADR] = TARGET_EBADR,
514 [EXFULL] = TARGET_EXFULL,
515 [ENOANO] = TARGET_ENOANO,
516 [EBADRQC] = TARGET_EBADRQC,
517 [EBADSLT] = TARGET_EBADSLT,
518 [EBFONT] = TARGET_EBFONT,
519 [ENOSTR] = TARGET_ENOSTR,
520 [ENODATA] = TARGET_ENODATA,
521 [ETIME] = TARGET_ETIME,
522 [ENOSR] = TARGET_ENOSR,
523 [ENONET] = TARGET_ENONET,
524 [ENOPKG] = TARGET_ENOPKG,
525 [EREMOTE] = TARGET_EREMOTE,
526 [ENOLINK] = TARGET_ENOLINK,
527 [EADV] = TARGET_EADV,
528 [ESRMNT] = TARGET_ESRMNT,
529 [ECOMM] = TARGET_ECOMM,
530 [EPROTO] = TARGET_EPROTO,
531 [EDOTDOT] = TARGET_EDOTDOT,
532 [EMULTIHOP] = TARGET_EMULTIHOP,
533 [EBADMSG] = TARGET_EBADMSG,
534 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
535 [EOVERFLOW] = TARGET_EOVERFLOW,
536 [ENOTUNIQ] = TARGET_ENOTUNIQ,
537 [EBADFD] = TARGET_EBADFD,
538 [EREMCHG] = TARGET_EREMCHG,
539 [ELIBACC] = TARGET_ELIBACC,
540 [ELIBBAD] = TARGET_ELIBBAD,
541 [ELIBSCN] = TARGET_ELIBSCN,
542 [ELIBMAX] = TARGET_ELIBMAX,
543 [ELIBEXEC] = TARGET_ELIBEXEC,
544 [EILSEQ] = TARGET_EILSEQ,
545 [ENOSYS] = TARGET_ENOSYS,
546 [ELOOP] = TARGET_ELOOP,
547 [ERESTART] = TARGET_ERESTART,
548 [ESTRPIPE] = TARGET_ESTRPIPE,
549 [ENOTEMPTY] = TARGET_ENOTEMPTY,
550 [EUSERS] = TARGET_EUSERS,
551 [ENOTSOCK] = TARGET_ENOTSOCK,
552 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
553 [EMSGSIZE] = TARGET_EMSGSIZE,
554 [EPROTOTYPE] = TARGET_EPROTOTYPE,
555 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
556 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
557 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
558 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
559 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
560 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
561 [EADDRINUSE] = TARGET_EADDRINUSE,
562 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
563 [ENETDOWN] = TARGET_ENETDOWN,
564 [ENETUNREACH] = TARGET_ENETUNREACH,
565 [ENETRESET] = TARGET_ENETRESET,
566 [ECONNABORTED] = TARGET_ECONNABORTED,
567 [ECONNRESET] = TARGET_ECONNRESET,
568 [ENOBUFS] = TARGET_ENOBUFS,
569 [EISCONN] = TARGET_EISCONN,
570 [ENOTCONN] = TARGET_ENOTCONN,
571 [EUCLEAN] = TARGET_EUCLEAN,
572 [ENOTNAM] = TARGET_ENOTNAM,
573 [ENAVAIL] = TARGET_ENAVAIL,
574 [EISNAM] = TARGET_EISNAM,
575 [EREMOTEIO] = TARGET_EREMOTEIO,
576 [ESHUTDOWN] = TARGET_ESHUTDOWN,
577 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
578 [ETIMEDOUT] = TARGET_ETIMEDOUT,
579 [ECONNREFUSED] = TARGET_ECONNREFUSED,
580 [EHOSTDOWN] = TARGET_EHOSTDOWN,
581 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
582 [EALREADY] = TARGET_EALREADY,
583 [EINPROGRESS] = TARGET_EINPROGRESS,
584 [ESTALE] = TARGET_ESTALE,
585 [ECANCELED] = TARGET_ECANCELED,
586 [ENOMEDIUM] = TARGET_ENOMEDIUM,
587 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
589 [ENOKEY] = TARGET_ENOKEY,
592 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
595 [EKEYREVOKED] = TARGET_EKEYREVOKED,
598 [EKEYREJECTED] = TARGET_EKEYREJECTED,
601 [EOWNERDEAD] = TARGET_EOWNERDEAD,
603 #ifdef ENOTRECOVERABLE
604 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
608 static inline int host_to_target_errno(int err)
610 if(host_to_target_errno_table[err])
611 return host_to_target_errno_table[err];
615 static inline int target_to_host_errno(int err)
617 if (target_to_host_errno_table[err])
618 return target_to_host_errno_table[err];
622 static inline abi_long get_errno(abi_long ret)
625 return -host_to_target_errno(errno);
630 static inline int is_error(abi_long ret)
632 return (abi_ulong)ret >= (abi_ulong)(-4096);
635 char *target_strerror(int err)
637 return strerror(target_to_host_errno(err));
640 static abi_ulong target_brk;
641 static abi_ulong target_original_brk;
643 void target_set_brk(abi_ulong new_brk)
645 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
648 /* do_brk() must return target values and target errnos. */
649 abi_long do_brk(abi_ulong new_brk)
652 abi_long mapped_addr;
657 if (new_brk < target_original_brk)
660 brk_page = HOST_PAGE_ALIGN(target_brk);
662 /* If the new brk is less than this, set it and we're done... */
663 if (new_brk < brk_page) {
664 target_brk = new_brk;
668 /* We need to allocate more memory after the brk... */
669 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
670 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
671 PROT_READ|PROT_WRITE,
672 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
674 if (!is_error(mapped_addr))
675 target_brk = new_brk;
680 static inline abi_long copy_from_user_fdset(fd_set *fds,
681 abi_ulong target_fds_addr,
685 abi_ulong b, *target_fds;
687 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
688 if (!(target_fds = lock_user(VERIFY_READ,
690 sizeof(abi_ulong) * nw,
692 return -TARGET_EFAULT;
696 for (i = 0; i < nw; i++) {
697 /* grab the abi_ulong */
698 __get_user(b, &target_fds[i]);
699 for (j = 0; j < TARGET_ABI_BITS; j++) {
700 /* check the bit inside the abi_ulong */
707 unlock_user(target_fds, target_fds_addr, 0);
712 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
718 abi_ulong *target_fds;
720 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
721 if (!(target_fds = lock_user(VERIFY_WRITE,
723 sizeof(abi_ulong) * nw,
725 return -TARGET_EFAULT;
728 for (i = 0; i < nw; i++) {
730 for (j = 0; j < TARGET_ABI_BITS; j++) {
731 v |= ((FD_ISSET(k, fds) != 0) << j);
734 __put_user(v, &target_fds[i]);
737 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
742 #if defined(__alpha__)
748 static inline abi_long host_to_target_clock_t(long ticks)
750 #if HOST_HZ == TARGET_HZ
753 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
757 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
758 const struct rusage *rusage)
760 struct target_rusage *target_rusage;
762 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
763 return -TARGET_EFAULT;
764 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
765 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
766 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
767 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
768 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
769 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
770 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
771 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
772 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
773 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
774 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
775 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
776 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
777 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
778 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
779 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
780 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
781 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
782 unlock_user_struct(target_rusage, target_addr, 1);
787 static inline abi_long copy_from_user_timeval(struct timeval *tv,
788 abi_ulong target_tv_addr)
790 struct target_timeval *target_tv;
792 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
793 return -TARGET_EFAULT;
795 __get_user(tv->tv_sec, &target_tv->tv_sec);
796 __get_user(tv->tv_usec, &target_tv->tv_usec);
798 unlock_user_struct(target_tv, target_tv_addr, 0);
803 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
804 const struct timeval *tv)
806 struct target_timeval *target_tv;
808 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
809 return -TARGET_EFAULT;
811 __put_user(tv->tv_sec, &target_tv->tv_sec);
812 __put_user(tv->tv_usec, &target_tv->tv_usec);
814 unlock_user_struct(target_tv, target_tv_addr, 1);
819 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
820 abi_ulong target_mq_attr_addr)
822 struct target_mq_attr *target_mq_attr;
824 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
825 target_mq_attr_addr, 1))
826 return -TARGET_EFAULT;
828 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
829 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
830 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
831 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
833 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
838 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
839 const struct mq_attr *attr)
841 struct target_mq_attr *target_mq_attr;
843 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
844 target_mq_attr_addr, 0))
845 return -TARGET_EFAULT;
847 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
848 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
849 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
850 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
852 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
857 /* do_select() must return target values and target errnos. */
858 static abi_long do_select(int n,
859 abi_ulong rfd_addr, abi_ulong wfd_addr,
860 abi_ulong efd_addr, abi_ulong target_tv_addr)
862 fd_set rfds, wfds, efds;
863 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
864 struct timeval tv, *tv_ptr;
868 if (copy_from_user_fdset(&rfds, rfd_addr, n))
869 return -TARGET_EFAULT;
875 if (copy_from_user_fdset(&wfds, wfd_addr, n))
876 return -TARGET_EFAULT;
882 if (copy_from_user_fdset(&efds, efd_addr, n))
883 return -TARGET_EFAULT;
889 if (target_tv_addr) {
890 if (copy_from_user_timeval(&tv, target_tv_addr))
891 return -TARGET_EFAULT;
897 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
899 if (!is_error(ret)) {
900 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
901 return -TARGET_EFAULT;
902 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
903 return -TARGET_EFAULT;
904 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
905 return -TARGET_EFAULT;
907 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
908 return -TARGET_EFAULT;
914 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
915 abi_ulong target_addr,
918 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
919 sa_family_t sa_family;
920 struct target_sockaddr *target_saddr;
922 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
924 return -TARGET_EFAULT;
926 sa_family = tswap16(target_saddr->sa_family);
928 /* Oops. The caller might send a incomplete sun_path; sun_path
929 * must be terminated by \0 (see the manual page), but
930 * unfortunately it is quite common to specify sockaddr_un
931 * length as "strlen(x->sun_path)" while it should be
932 * "strlen(...) + 1". We'll fix that here if needed.
933 * Linux kernel has a similar feature.
936 if (sa_family == AF_UNIX) {
937 if (len < unix_maxlen) {
938 char *cp = (char*)target_saddr;
940 if ( cp[len-1] && !cp[len] )
943 if (len > unix_maxlen)
947 memcpy(addr, target_saddr, len);
948 addr->sa_family = sa_family;
949 unlock_user(target_saddr, target_addr, 0);
954 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
955 struct sockaddr *addr,
958 struct target_sockaddr *target_saddr;
960 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
962 return -TARGET_EFAULT;
963 memcpy(target_saddr, addr, len);
964 target_saddr->sa_family = tswap16(addr->sa_family);
965 unlock_user(target_saddr, target_addr, len);
970 /* ??? Should this also swap msgh->name? */
971 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
972 struct target_msghdr *target_msgh)
974 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
975 abi_long msg_controllen;
976 abi_ulong target_cmsg_addr;
977 struct target_cmsghdr *target_cmsg;
980 msg_controllen = tswapl(target_msgh->msg_controllen);
981 if (msg_controllen < sizeof (struct target_cmsghdr))
983 target_cmsg_addr = tswapl(target_msgh->msg_control);
984 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
986 return -TARGET_EFAULT;
988 while (cmsg && target_cmsg) {
989 void *data = CMSG_DATA(cmsg);
990 void *target_data = TARGET_CMSG_DATA(target_cmsg);
992 int len = tswapl(target_cmsg->cmsg_len)
993 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
995 space += CMSG_SPACE(len);
996 if (space > msgh->msg_controllen) {
997 space -= CMSG_SPACE(len);
998 gemu_log("Host cmsg overflow\n");
1002 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1003 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1004 cmsg->cmsg_len = CMSG_LEN(len);
1006 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1007 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1008 memcpy(data, target_data, len);
1010 int *fd = (int *)data;
1011 int *target_fd = (int *)target_data;
1012 int i, numfds = len / sizeof(int);
1014 for (i = 0; i < numfds; i++)
1015 fd[i] = tswap32(target_fd[i]);
1018 cmsg = CMSG_NXTHDR(msgh, cmsg);
1019 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1021 unlock_user(target_cmsg, target_cmsg_addr, 0);
1023 msgh->msg_controllen = space;
1027 /* ??? Should this also swap msgh->name? */
1028 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1029 struct msghdr *msgh)
1031 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1032 abi_long msg_controllen;
1033 abi_ulong target_cmsg_addr;
1034 struct target_cmsghdr *target_cmsg;
1035 socklen_t space = 0;
1037 msg_controllen = tswapl(target_msgh->msg_controllen);
1038 if (msg_controllen < sizeof (struct target_cmsghdr))
1040 target_cmsg_addr = tswapl(target_msgh->msg_control);
1041 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1043 return -TARGET_EFAULT;
1045 while (cmsg && target_cmsg) {
1046 void *data = CMSG_DATA(cmsg);
1047 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1049 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1051 space += TARGET_CMSG_SPACE(len);
1052 if (space > msg_controllen) {
1053 space -= TARGET_CMSG_SPACE(len);
1054 gemu_log("Target cmsg overflow\n");
1058 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1059 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1060 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1062 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1063 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1064 memcpy(target_data, data, len);
1066 int *fd = (int *)data;
1067 int *target_fd = (int *)target_data;
1068 int i, numfds = len / sizeof(int);
1070 for (i = 0; i < numfds; i++)
1071 target_fd[i] = tswap32(fd[i]);
1074 cmsg = CMSG_NXTHDR(msgh, cmsg);
1075 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1077 unlock_user(target_cmsg, target_cmsg_addr, space);
1079 target_msgh->msg_controllen = tswapl(space);
1083 /* do_setsockopt() Must return target values and target errnos. */
1084 static abi_long do_setsockopt(int sockfd, int level, int optname,
1085 abi_ulong optval_addr, socklen_t optlen)
1092 /* TCP options all take an 'int' value. */
1093 if (optlen < sizeof(uint32_t))
1094 return -TARGET_EINVAL;
1096 if (get_user_u32(val, optval_addr))
1097 return -TARGET_EFAULT;
1098 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1105 case IP_ROUTER_ALERT:
1109 case IP_MTU_DISCOVER:
1115 case IP_MULTICAST_TTL:
1116 case IP_MULTICAST_LOOP:
1118 if (optlen >= sizeof(uint32_t)) {
1119 if (get_user_u32(val, optval_addr))
1120 return -TARGET_EFAULT;
1121 } else if (optlen >= 1) {
1122 if (get_user_u8(val, optval_addr))
1123 return -TARGET_EFAULT;
1125 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1131 case TARGET_SOL_SOCKET:
1133 /* Options with 'int' argument. */
1134 case TARGET_SO_DEBUG:
1137 case TARGET_SO_REUSEADDR:
1138 optname = SO_REUSEADDR;
1140 case TARGET_SO_TYPE:
1143 case TARGET_SO_ERROR:
1146 case TARGET_SO_DONTROUTE:
1147 optname = SO_DONTROUTE;
1149 case TARGET_SO_BROADCAST:
1150 optname = SO_BROADCAST;
1152 case TARGET_SO_SNDBUF:
1153 optname = SO_SNDBUF;
1155 case TARGET_SO_RCVBUF:
1156 optname = SO_RCVBUF;
1158 case TARGET_SO_KEEPALIVE:
1159 optname = SO_KEEPALIVE;
1161 case TARGET_SO_OOBINLINE:
1162 optname = SO_OOBINLINE;
1164 case TARGET_SO_NO_CHECK:
1165 optname = SO_NO_CHECK;
1167 case TARGET_SO_PRIORITY:
1168 optname = SO_PRIORITY;
1171 case TARGET_SO_BSDCOMPAT:
1172 optname = SO_BSDCOMPAT;
1175 case TARGET_SO_PASSCRED:
1176 optname = SO_PASSCRED;
1178 case TARGET_SO_TIMESTAMP:
1179 optname = SO_TIMESTAMP;
1181 case TARGET_SO_RCVLOWAT:
1182 optname = SO_RCVLOWAT;
1184 case TARGET_SO_RCVTIMEO:
1185 optname = SO_RCVTIMEO;
1187 case TARGET_SO_SNDTIMEO:
1188 optname = SO_SNDTIMEO;
1194 if (optlen < sizeof(uint32_t))
1195 return -TARGET_EINVAL;
1197 if (get_user_u32(val, optval_addr))
1198 return -TARGET_EFAULT;
1199 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1203 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
1204 ret = -TARGET_ENOPROTOOPT;
1209 /* do_getsockopt() Must return target values and target errnos. */
1210 static abi_long do_getsockopt(int sockfd, int level, int optname,
1211 abi_ulong optval_addr, abi_ulong optlen)
1218 case TARGET_SOL_SOCKET:
1221 case TARGET_SO_LINGER:
1222 case TARGET_SO_RCVTIMEO:
1223 case TARGET_SO_SNDTIMEO:
1224 case TARGET_SO_PEERCRED:
1225 case TARGET_SO_PEERNAME:
1226 /* These don't just return a single integer */
1233 /* TCP options all take an 'int' value. */
1235 if (get_user_u32(len, optlen))
1236 return -TARGET_EFAULT;
1238 return -TARGET_EINVAL;
1240 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1247 if (put_user_u32(val, optval_addr))
1248 return -TARGET_EFAULT;
1250 if (put_user_u8(val, optval_addr))
1251 return -TARGET_EFAULT;
1253 if (put_user_u32(len, optlen))
1254 return -TARGET_EFAULT;
1261 case IP_ROUTER_ALERT:
1265 case IP_MTU_DISCOVER:
1271 case IP_MULTICAST_TTL:
1272 case IP_MULTICAST_LOOP:
1273 if (get_user_u32(len, optlen))
1274 return -TARGET_EFAULT;
1276 return -TARGET_EINVAL;
1278 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1281 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1283 if (put_user_u32(len, optlen)
1284 || put_user_u8(val, optval_addr))
1285 return -TARGET_EFAULT;
1287 if (len > sizeof(int))
1289 if (put_user_u32(len, optlen)
1290 || put_user_u32(val, optval_addr))
1291 return -TARGET_EFAULT;
1295 ret = -TARGET_ENOPROTOOPT;
1301 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1303 ret = -TARGET_EOPNOTSUPP;
1310 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1311 * other lock functions have a return code of 0 for failure.
1313 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1314 int count, int copy)
1316 struct target_iovec *target_vec;
1320 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1322 return -TARGET_EFAULT;
1323 for(i = 0;i < count; i++) {
1324 base = tswapl(target_vec[i].iov_base);
1325 vec[i].iov_len = tswapl(target_vec[i].iov_len);
1326 if (vec[i].iov_len != 0) {
1327 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1328 /* Don't check lock_user return value. We must call writev even
1329 if a element has invalid base address. */
1331 /* zero length pointer is ignored */
1332 vec[i].iov_base = NULL;
1335 unlock_user (target_vec, target_addr, 0);
1339 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1340 int count, int copy)
1342 struct target_iovec *target_vec;
1346 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1348 return -TARGET_EFAULT;
1349 for(i = 0;i < count; i++) {
1350 if (target_vec[i].iov_base) {
1351 base = tswapl(target_vec[i].iov_base);
1352 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1355 unlock_user (target_vec, target_addr, 0);
1360 /* do_socket() Must return target values and target errnos. */
1361 static abi_long do_socket(int domain, int type, int protocol)
1363 #if defined(TARGET_MIPS)
1365 case TARGET_SOCK_DGRAM:
1368 case TARGET_SOCK_STREAM:
1371 case TARGET_SOCK_RAW:
1374 case TARGET_SOCK_RDM:
1377 case TARGET_SOCK_SEQPACKET:
1378 type = SOCK_SEQPACKET;
1380 case TARGET_SOCK_PACKET:
1385 if (domain == PF_NETLINK)
1386 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1387 return get_errno(socket(domain, type, protocol));
1390 /* do_bind() Must return target values and target errnos. */
1391 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1397 return -TARGET_EINVAL;
1399 addr = alloca(addrlen+1);
1401 target_to_host_sockaddr(addr, target_addr, addrlen);
1402 return get_errno(bind(sockfd, addr, addrlen));
1405 /* do_connect() Must return target values and target errnos. */
1406 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1412 return -TARGET_EINVAL;
1414 addr = alloca(addrlen);
1416 target_to_host_sockaddr(addr, target_addr, addrlen);
1417 return get_errno(connect(sockfd, addr, addrlen));
1420 /* do_sendrecvmsg() Must return target values and target errnos. */
1421 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1422 int flags, int send)
1425 struct target_msghdr *msgp;
1429 abi_ulong target_vec;
1432 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1436 return -TARGET_EFAULT;
1437 if (msgp->msg_name) {
1438 msg.msg_namelen = tswap32(msgp->msg_namelen);
1439 msg.msg_name = alloca(msg.msg_namelen);
1440 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1443 msg.msg_name = NULL;
1444 msg.msg_namelen = 0;
1446 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1447 msg.msg_control = alloca(msg.msg_controllen);
1448 msg.msg_flags = tswap32(msgp->msg_flags);
1450 count = tswapl(msgp->msg_iovlen);
1451 vec = alloca(count * sizeof(struct iovec));
1452 target_vec = tswapl(msgp->msg_iov);
1453 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1454 msg.msg_iovlen = count;
1458 ret = target_to_host_cmsg(&msg, msgp);
1460 ret = get_errno(sendmsg(fd, &msg, flags));
1462 ret = get_errno(recvmsg(fd, &msg, flags));
1463 if (!is_error(ret)) {
1465 ret = host_to_target_cmsg(msgp, &msg);
1470 unlock_iovec(vec, target_vec, count, !send);
1471 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1475 /* do_accept() Must return target values and target errnos. */
1476 static abi_long do_accept(int fd, abi_ulong target_addr,
1477 abi_ulong target_addrlen_addr)
1483 if (target_addr == 0)
1484 return get_errno(accept(fd, NULL, NULL));
1486 if (get_user_u32(addrlen, target_addrlen_addr))
1487 return -TARGET_EFAULT;
1490 return -TARGET_EINVAL;
1492 addr = alloca(addrlen);
1494 ret = get_errno(accept(fd, addr, &addrlen));
1495 if (!is_error(ret)) {
1496 host_to_target_sockaddr(target_addr, addr, addrlen);
1497 if (put_user_u32(addrlen, target_addrlen_addr))
1498 ret = -TARGET_EFAULT;
1503 /* do_getpeername() Must return target values and target errnos. */
1504 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1505 abi_ulong target_addrlen_addr)
1511 if (get_user_u32(addrlen, target_addrlen_addr))
1512 return -TARGET_EFAULT;
1515 return -TARGET_EINVAL;
1517 addr = alloca(addrlen);
1519 ret = get_errno(getpeername(fd, addr, &addrlen));
1520 if (!is_error(ret)) {
1521 host_to_target_sockaddr(target_addr, addr, addrlen);
1522 if (put_user_u32(addrlen, target_addrlen_addr))
1523 ret = -TARGET_EFAULT;
1528 /* do_getsockname() Must return target values and target errnos. */
1529 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1530 abi_ulong target_addrlen_addr)
1536 if (target_addr == 0)
1537 return get_errno(accept(fd, NULL, NULL));
1539 if (get_user_u32(addrlen, target_addrlen_addr))
1540 return -TARGET_EFAULT;
1543 return -TARGET_EINVAL;
1545 addr = alloca(addrlen);
1547 ret = get_errno(getsockname(fd, addr, &addrlen));
1548 if (!is_error(ret)) {
1549 host_to_target_sockaddr(target_addr, addr, addrlen);
1550 if (put_user_u32(addrlen, target_addrlen_addr))
1551 ret = -TARGET_EFAULT;
1556 /* do_socketpair() Must return target values and target errnos. */
1557 static abi_long do_socketpair(int domain, int type, int protocol,
1558 abi_ulong target_tab_addr)
1563 ret = get_errno(socketpair(domain, type, protocol, tab));
1564 if (!is_error(ret)) {
1565 if (put_user_s32(tab[0], target_tab_addr)
1566 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1567 ret = -TARGET_EFAULT;
1572 /* do_sendto() Must return target values and target errnos. */
1573 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1574 abi_ulong target_addr, socklen_t addrlen)
1581 return -TARGET_EINVAL;
1583 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1585 return -TARGET_EFAULT;
1587 addr = alloca(addrlen);
1588 target_to_host_sockaddr(addr, target_addr, addrlen);
1589 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1591 ret = get_errno(send(fd, host_msg, len, flags));
1593 unlock_user(host_msg, msg, 0);
1597 /* do_recvfrom() Must return target values and target errnos. */
1598 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1599 abi_ulong target_addr,
1600 abi_ulong target_addrlen)
1607 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1609 return -TARGET_EFAULT;
1611 if (get_user_u32(addrlen, target_addrlen)) {
1612 ret = -TARGET_EFAULT;
1616 ret = -TARGET_EINVAL;
1619 addr = alloca(addrlen);
1620 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1622 addr = NULL; /* To keep compiler quiet. */
1623 ret = get_errno(recv(fd, host_msg, len, flags));
1625 if (!is_error(ret)) {
1627 host_to_target_sockaddr(target_addr, addr, addrlen);
1628 if (put_user_u32(addrlen, target_addrlen)) {
1629 ret = -TARGET_EFAULT;
1633 unlock_user(host_msg, msg, len);
1636 unlock_user(host_msg, msg, 0);
1641 #ifdef TARGET_NR_socketcall
1642 /* do_socketcall() Must return target values and target errnos. */
1643 static abi_long do_socketcall(int num, abi_ulong vptr)
1646 const int n = sizeof(abi_ulong);
1651 int domain, type, protocol;
1653 if (get_user_s32(domain, vptr)
1654 || get_user_s32(type, vptr + n)
1655 || get_user_s32(protocol, vptr + 2 * n))
1656 return -TARGET_EFAULT;
1658 ret = do_socket(domain, type, protocol);
1664 abi_ulong target_addr;
1667 if (get_user_s32(sockfd, vptr)
1668 || get_user_ual(target_addr, vptr + n)
1669 || get_user_u32(addrlen, vptr + 2 * n))
1670 return -TARGET_EFAULT;
1672 ret = do_bind(sockfd, target_addr, addrlen);
1675 case SOCKOP_connect:
1678 abi_ulong target_addr;
1681 if (get_user_s32(sockfd, vptr)
1682 || get_user_ual(target_addr, vptr + n)
1683 || get_user_u32(addrlen, vptr + 2 * n))
1684 return -TARGET_EFAULT;
1686 ret = do_connect(sockfd, target_addr, addrlen);
1691 int sockfd, backlog;
1693 if (get_user_s32(sockfd, vptr)
1694 || get_user_s32(backlog, vptr + n))
1695 return -TARGET_EFAULT;
1697 ret = get_errno(listen(sockfd, backlog));
1703 abi_ulong target_addr, target_addrlen;
1705 if (get_user_s32(sockfd, vptr)
1706 || get_user_ual(target_addr, vptr + n)
1707 || get_user_u32(target_addrlen, vptr + 2 * n))
1708 return -TARGET_EFAULT;
1710 ret = do_accept(sockfd, target_addr, target_addrlen);
1713 case SOCKOP_getsockname:
1716 abi_ulong target_addr, target_addrlen;
1718 if (get_user_s32(sockfd, vptr)
1719 || get_user_ual(target_addr, vptr + n)
1720 || get_user_u32(target_addrlen, vptr + 2 * n))
1721 return -TARGET_EFAULT;
1723 ret = do_getsockname(sockfd, target_addr, target_addrlen);
1726 case SOCKOP_getpeername:
1729 abi_ulong target_addr, target_addrlen;
1731 if (get_user_s32(sockfd, vptr)
1732 || get_user_ual(target_addr, vptr + n)
1733 || get_user_u32(target_addrlen, vptr + 2 * n))
1734 return -TARGET_EFAULT;
1736 ret = do_getpeername(sockfd, target_addr, target_addrlen);
1739 case SOCKOP_socketpair:
1741 int domain, type, protocol;
1744 if (get_user_s32(domain, vptr)
1745 || get_user_s32(type, vptr + n)
1746 || get_user_s32(protocol, vptr + 2 * n)
1747 || get_user_ual(tab, vptr + 3 * n))
1748 return -TARGET_EFAULT;
1750 ret = do_socketpair(domain, type, protocol, tab);
1760 if (get_user_s32(sockfd, vptr)
1761 || get_user_ual(msg, vptr + n)
1762 || get_user_ual(len, vptr + 2 * n)
1763 || get_user_s32(flags, vptr + 3 * n))
1764 return -TARGET_EFAULT;
1766 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1776 if (get_user_s32(sockfd, vptr)
1777 || get_user_ual(msg, vptr + n)
1778 || get_user_ual(len, vptr + 2 * n)
1779 || get_user_s32(flags, vptr + 3 * n))
1780 return -TARGET_EFAULT;
1782 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1794 if (get_user_s32(sockfd, vptr)
1795 || get_user_ual(msg, vptr + n)
1796 || get_user_ual(len, vptr + 2 * n)
1797 || get_user_s32(flags, vptr + 3 * n)
1798 || get_user_ual(addr, vptr + 4 * n)
1799 || get_user_u32(addrlen, vptr + 5 * n))
1800 return -TARGET_EFAULT;
1802 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1805 case SOCKOP_recvfrom:
1814 if (get_user_s32(sockfd, vptr)
1815 || get_user_ual(msg, vptr + n)
1816 || get_user_ual(len, vptr + 2 * n)
1817 || get_user_s32(flags, vptr + 3 * n)
1818 || get_user_ual(addr, vptr + 4 * n)
1819 || get_user_u32(addrlen, vptr + 5 * n))
1820 return -TARGET_EFAULT;
1822 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1825 case SOCKOP_shutdown:
1829 if (get_user_s32(sockfd, vptr)
1830 || get_user_s32(how, vptr + n))
1831 return -TARGET_EFAULT;
1833 ret = get_errno(shutdown(sockfd, how));
1836 case SOCKOP_sendmsg:
1837 case SOCKOP_recvmsg:
1840 abi_ulong target_msg;
1843 if (get_user_s32(fd, vptr)
1844 || get_user_ual(target_msg, vptr + n)
1845 || get_user_s32(flags, vptr + 2 * n))
1846 return -TARGET_EFAULT;
1848 ret = do_sendrecvmsg(fd, target_msg, flags,
1849 (num == SOCKOP_sendmsg));
1852 case SOCKOP_setsockopt:
1860 if (get_user_s32(sockfd, vptr)
1861 || get_user_s32(level, vptr + n)
1862 || get_user_s32(optname, vptr + 2 * n)
1863 || get_user_ual(optval, vptr + 3 * n)
1864 || get_user_u32(optlen, vptr + 4 * n))
1865 return -TARGET_EFAULT;
1867 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1870 case SOCKOP_getsockopt:
1878 if (get_user_s32(sockfd, vptr)
1879 || get_user_s32(level, vptr + n)
1880 || get_user_s32(optname, vptr + 2 * n)
1881 || get_user_ual(optval, vptr + 3 * n)
1882 || get_user_u32(optlen, vptr + 4 * n))
1883 return -TARGET_EFAULT;
1885 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
1889 gemu_log("Unsupported socketcall: %d\n", num);
1890 ret = -TARGET_ENOSYS;
1897 #define N_SHM_REGIONS 32
1899 static struct shm_region {
1902 } shm_regions[N_SHM_REGIONS];
1904 struct target_ipc_perm
1911 unsigned short int mode;
1912 unsigned short int __pad1;
1913 unsigned short int __seq;
1914 unsigned short int __pad2;
1915 abi_ulong __unused1;
1916 abi_ulong __unused2;
1919 struct target_semid_ds
1921 struct target_ipc_perm sem_perm;
1922 abi_ulong sem_otime;
1923 abi_ulong __unused1;
1924 abi_ulong sem_ctime;
1925 abi_ulong __unused2;
1926 abi_ulong sem_nsems;
1927 abi_ulong __unused3;
1928 abi_ulong __unused4;
1931 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1932 abi_ulong target_addr)
1934 struct target_ipc_perm *target_ip;
1935 struct target_semid_ds *target_sd;
1937 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1938 return -TARGET_EFAULT;
1939 target_ip=&(target_sd->sem_perm);
1940 host_ip->__key = tswapl(target_ip->__key);
1941 host_ip->uid = tswapl(target_ip->uid);
1942 host_ip->gid = tswapl(target_ip->gid);
1943 host_ip->cuid = tswapl(target_ip->cuid);
1944 host_ip->cgid = tswapl(target_ip->cgid);
1945 host_ip->mode = tswapl(target_ip->mode);
1946 unlock_user_struct(target_sd, target_addr, 0);
1950 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1951 struct ipc_perm *host_ip)
1953 struct target_ipc_perm *target_ip;
1954 struct target_semid_ds *target_sd;
1956 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1957 return -TARGET_EFAULT;
1958 target_ip = &(target_sd->sem_perm);
1959 target_ip->__key = tswapl(host_ip->__key);
1960 target_ip->uid = tswapl(host_ip->uid);
1961 target_ip->gid = tswapl(host_ip->gid);
1962 target_ip->cuid = tswapl(host_ip->cuid);
1963 target_ip->cgid = tswapl(host_ip->cgid);
1964 target_ip->mode = tswapl(host_ip->mode);
1965 unlock_user_struct(target_sd, target_addr, 1);
1969 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1970 abi_ulong target_addr)
1972 struct target_semid_ds *target_sd;
1974 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1975 return -TARGET_EFAULT;
1976 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
1977 return -TARGET_EFAULT;
1978 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1979 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1980 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1981 unlock_user_struct(target_sd, target_addr, 0);
1985 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1986 struct semid_ds *host_sd)
1988 struct target_semid_ds *target_sd;
1990 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1991 return -TARGET_EFAULT;
1992 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
1993 return -TARGET_EFAULT;;
1994 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1995 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1996 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1997 unlock_user_struct(target_sd, target_addr, 1);
2001 struct target_seminfo {
2014 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2015 struct seminfo *host_seminfo)
2017 struct target_seminfo *target_seminfo;
2018 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2019 return -TARGET_EFAULT;
2020 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2021 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2022 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2023 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2024 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2025 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2026 __put_user(host_seminfo->semume, &target_seminfo->semume);
2027 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2028 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2029 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2030 unlock_user_struct(target_seminfo, target_addr, 1);
2036 struct semid_ds *buf;
2037 unsigned short *array;
2038 struct seminfo *__buf;
2041 union target_semun {
2048 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2049 abi_ulong target_addr)
2052 unsigned short *array;
2054 struct semid_ds semid_ds;
2057 semun.buf = &semid_ds;
2059 ret = semctl(semid, 0, IPC_STAT, semun);
2061 return get_errno(ret);
2063 nsems = semid_ds.sem_nsems;
2065 *host_array = malloc(nsems*sizeof(unsigned short));
2066 array = lock_user(VERIFY_READ, target_addr,
2067 nsems*sizeof(unsigned short), 1);
2069 return -TARGET_EFAULT;
2071 for(i=0; i<nsems; i++) {
2072 __get_user((*host_array)[i], &array[i]);
2074 unlock_user(array, target_addr, 0);
2079 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2080 unsigned short **host_array)
2083 unsigned short *array;
2085 struct semid_ds semid_ds;
2088 semun.buf = &semid_ds;
2090 ret = semctl(semid, 0, IPC_STAT, semun);
2092 return get_errno(ret);
2094 nsems = semid_ds.sem_nsems;
2096 array = lock_user(VERIFY_WRITE, target_addr,
2097 nsems*sizeof(unsigned short), 0);
2099 return -TARGET_EFAULT;
2101 for(i=0; i<nsems; i++) {
2102 __put_user((*host_array)[i], &array[i]);
2105 unlock_user(array, target_addr, 1);
2110 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2111 union target_semun target_su)
2114 struct semid_ds dsarg;
2115 unsigned short *array;
2116 struct seminfo seminfo;
2117 abi_long ret = -TARGET_EINVAL;
2126 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2130 ret = get_errno(semctl(semid, semnum, cmd, arg));
2131 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2137 arg.val = tswapl(target_su.val);
2138 ret = get_errno(semctl(semid, semnum, cmd, arg));
2139 target_su.val = tswapl(arg.val);
2143 err = target_to_host_semarray(semid, &array, target_su.array);
2147 ret = get_errno(semctl(semid, semnum, cmd, arg));
2148 err = host_to_target_semarray(semid, target_su.array, &array);
2154 arg.__buf = &seminfo;
2155 ret = get_errno(semctl(semid, semnum, cmd, arg));
2156 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2164 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2171 struct target_sembuf {
2172 unsigned short sem_num;
2177 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2178 abi_ulong target_addr,
2181 struct target_sembuf *target_sembuf;
2184 target_sembuf = lock_user(VERIFY_READ, target_addr,
2185 nsops*sizeof(struct target_sembuf), 1);
2187 return -TARGET_EFAULT;
2189 for(i=0; i<nsops; i++) {
2190 __put_user(target_sembuf[i].sem_num, &host_sembuf[i].sem_num);
2191 __put_user(target_sembuf[i].sem_op, &host_sembuf[i].sem_op);
2192 __put_user(target_sembuf[i].sem_flg, &host_sembuf[i].sem_flg);
2195 unlock_user(target_sembuf, target_addr, 0);
2200 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2202 struct sembuf sops[nsops];
2204 if (target_to_host_sembuf(sops, ptr, nsops))
2205 return -TARGET_EFAULT;
2207 return semop(semid, sops, nsops);
2210 struct target_msqid_ds
2212 struct target_ipc_perm msg_perm;
2213 abi_ulong msg_stime;
2214 #if TARGET_ABI_BITS == 32
2215 abi_ulong __unused1;
2217 abi_ulong msg_rtime;
2218 #if TARGET_ABI_BITS == 32
2219 abi_ulong __unused2;
2221 abi_ulong msg_ctime;
2222 #if TARGET_ABI_BITS == 32
2223 abi_ulong __unused3;
2225 abi_ulong __msg_cbytes;
2227 abi_ulong msg_qbytes;
2228 abi_ulong msg_lspid;
2229 abi_ulong msg_lrpid;
2230 abi_ulong __unused4;
2231 abi_ulong __unused5;
2234 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2235 abi_ulong target_addr)
2237 struct target_msqid_ds *target_md;
2239 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2240 return -TARGET_EFAULT;
2241 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2242 return -TARGET_EFAULT;
2243 host_md->msg_stime = tswapl(target_md->msg_stime);
2244 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2245 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2246 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2247 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2248 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2249 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2250 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2251 unlock_user_struct(target_md, target_addr, 0);
2255 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2256 struct msqid_ds *host_md)
2258 struct target_msqid_ds *target_md;
2260 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2261 return -TARGET_EFAULT;
2262 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2263 return -TARGET_EFAULT;
2264 target_md->msg_stime = tswapl(host_md->msg_stime);
2265 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2266 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2267 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2268 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2269 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2270 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2271 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2272 unlock_user_struct(target_md, target_addr, 1);
2276 struct target_msginfo {
2284 unsigned short int msgseg;
2287 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2288 struct msginfo *host_msginfo)
2290 struct target_msginfo *target_msginfo;
2291 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2292 return -TARGET_EFAULT;
2293 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2294 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2295 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2296 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2297 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2298 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2299 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2300 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2301 unlock_user_struct(target_msginfo, target_addr, 1);
2305 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
2307 struct msqid_ds dsarg;
2308 struct msginfo msginfo;
2309 abi_long ret = -TARGET_EINVAL;
2317 if (target_to_host_msqid_ds(&dsarg,ptr))
2318 return -TARGET_EFAULT;
2319 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2320 if (host_to_target_msqid_ds(ptr,&dsarg))
2321 return -TARGET_EFAULT;
2324 ret = get_errno(msgctl(msgid, cmd, NULL));
2328 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2329 if (host_to_target_msginfo(ptr, &msginfo))
2330 return -TARGET_EFAULT;
2337 struct target_msgbuf {
2342 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2343 unsigned int msgsz, int msgflg)
2345 struct target_msgbuf *target_mb;
2346 struct msgbuf *host_mb;
2349 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2350 return -TARGET_EFAULT;
2351 host_mb = malloc(msgsz+sizeof(long));
2352 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2353 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
2354 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2356 unlock_user_struct(target_mb, msgp, 0);
2361 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2362 unsigned int msgsz, abi_long msgtyp,
2365 struct target_msgbuf *target_mb;
2367 struct msgbuf *host_mb;
2370 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2371 return -TARGET_EFAULT;
2373 host_mb = malloc(msgsz+sizeof(long));
2374 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2377 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2378 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2379 if (!target_mtext) {
2380 ret = -TARGET_EFAULT;
2383 memcpy(target_mb->mtext, host_mb->mtext, ret);
2384 unlock_user(target_mtext, target_mtext_addr, ret);
2387 target_mb->mtype = tswapl(host_mb->mtype);
2392 unlock_user_struct(target_mb, msgp, 1);
2396 struct target_shmid_ds
2398 struct target_ipc_perm shm_perm;
2399 abi_ulong shm_segsz;
2400 abi_ulong shm_atime;
2401 #if TARGET_ABI_BITS == 32
2402 abi_ulong __unused1;
2404 abi_ulong shm_dtime;
2405 #if TARGET_ABI_BITS == 32
2406 abi_ulong __unused2;
2408 abi_ulong shm_ctime;
2409 #if TARGET_ABI_BITS == 32
2410 abi_ulong __unused3;
2414 abi_ulong shm_nattch;
2415 unsigned long int __unused4;
2416 unsigned long int __unused5;
2419 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2420 abi_ulong target_addr)
2422 struct target_shmid_ds *target_sd;
2424 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2425 return -TARGET_EFAULT;
2426 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2427 return -TARGET_EFAULT;
2428 __put_user(target_sd->shm_segsz, &host_sd->shm_segsz);
2429 __put_user(target_sd->shm_atime, &host_sd->shm_atime);
2430 __put_user(target_sd->shm_dtime, &host_sd->shm_dtime);
2431 __put_user(target_sd->shm_ctime, &host_sd->shm_ctime);
2432 __put_user(target_sd->shm_cpid, &host_sd->shm_cpid);
2433 __put_user(target_sd->shm_lpid, &host_sd->shm_lpid);
2434 __put_user(target_sd->shm_nattch, &host_sd->shm_nattch);
2435 unlock_user_struct(target_sd, target_addr, 0);
2439 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2440 struct shmid_ds *host_sd)
2442 struct target_shmid_ds *target_sd;
2444 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2445 return -TARGET_EFAULT;
2446 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2447 return -TARGET_EFAULT;
2448 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2449 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2450 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2451 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2452 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2453 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2454 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2455 unlock_user_struct(target_sd, target_addr, 1);
2459 struct target_shminfo {
2467 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2468 struct shminfo *host_shminfo)
2470 struct target_shminfo *target_shminfo;
2471 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2472 return -TARGET_EFAULT;
2473 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2474 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2475 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2476 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2477 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2478 unlock_user_struct(target_shminfo, target_addr, 1);
2482 struct target_shm_info {
2487 abi_ulong swap_attempts;
2488 abi_ulong swap_successes;
2491 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2492 struct shm_info *host_shm_info)
2494 struct target_shm_info *target_shm_info;
2495 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2496 return -TARGET_EFAULT;
2497 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2498 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2499 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2500 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2501 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2502 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2503 unlock_user_struct(target_shm_info, target_addr, 1);
2507 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2509 struct shmid_ds dsarg;
2510 struct shminfo shminfo;
2511 struct shm_info shm_info;
2512 abi_long ret = -TARGET_EINVAL;
2520 if (target_to_host_shmid_ds(&dsarg, buf))
2521 return -TARGET_EFAULT;
2522 ret = get_errno(shmctl(shmid, cmd, &dsarg));
2523 if (host_to_target_shmid_ds(buf, &dsarg))
2524 return -TARGET_EFAULT;
2527 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2528 if (host_to_target_shminfo(buf, &shminfo))
2529 return -TARGET_EFAULT;
2532 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2533 if (host_to_target_shm_info(buf, &shm_info))
2534 return -TARGET_EFAULT;
2539 ret = get_errno(shmctl(shmid, cmd, NULL));
2546 static inline abi_long do_shmat(int shmid, abi_ulong shmaddr, int shmflg,
2547 unsigned long *raddr)
2549 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
2551 struct shmid_ds shm_info;
2554 /* find out the length of the shared memory segment */
2555 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2556 if (is_error(ret)) {
2557 /* can't get length, bail out */
2558 return get_errno(ret);
2564 *raddr = (unsigned long) shmat(shmid, g2h(shmaddr), shmflg);
2566 abi_ulong mmap_start;
2568 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2570 if (mmap_start == -1) {
2574 *raddr = (unsigned long) shmat(shmid, g2h(mmap_start),
2575 shmflg | SHM_REMAP);
2580 return get_errno(*raddr);
2583 page_set_flags(h2g(*raddr), h2g(*raddr) + shm_info.shm_segsz,
2584 PAGE_VALID | PAGE_READ |
2585 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2587 for (i = 0; i < N_SHM_REGIONS; i++) {
2588 if (shm_regions[i].start == 0) {
2589 shm_regions[i].start = h2g(*raddr);
2590 shm_regions[i].size = shm_info.shm_segsz;
2595 *raddr = h2g(*raddr);
2601 static inline abi_long do_shmdt(abi_ulong shmaddr)
2605 for (i = 0; i < N_SHM_REGIONS; ++i) {
2606 if (shm_regions[i].start == shmaddr) {
2607 shm_regions[i].start = 0;
2608 page_set_flags(shmaddr, shm_regions[i].size, 0);
2613 return get_errno(shmdt(g2h(shmaddr)));
2616 #ifdef TARGET_NR_ipc
2617 /* ??? This only works with linear mappings. */
2618 /* do_ipc() must return target values and target errnos. */
2619 static abi_long do_ipc(unsigned int call, int first,
2620 int second, int third,
2621 abi_long ptr, abi_long fifth)
2626 version = call >> 16;
2631 ret = do_semop(first, ptr, second);
2635 ret = get_errno(semget(first, second, third));
2639 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
2643 ret = get_errno(msgget(first, second));
2647 ret = do_msgsnd(first, ptr, second, third);
2651 ret = do_msgctl(first, second, ptr);
2658 struct target_ipc_kludge {
2663 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2664 ret = -TARGET_EFAULT;
2668 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2670 unlock_user_struct(tmp, ptr, 0);
2674 ret = do_msgrcv(first, ptr, second, fifth, third);
2682 unsigned long raddr;
2684 ret = do_shmat(first, ptr, second, &raddr);
2688 ret = put_user_ual(raddr, third);
2692 ret = -TARGET_EINVAL;
2698 ret = do_shmdt(ptr);
2702 ret = get_errno(shmget(first, second, third));
2706 ret = do_shmctl(first, second, third);
2710 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2711 ret = -TARGET_ENOSYS;
2718 /* kernel structure types definitions */
2721 #define STRUCT(name, list...) STRUCT_ ## name,
2722 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2724 #include "syscall_types.h"
2727 #undef STRUCT_SPECIAL
2729 #define STRUCT(name, list...) static const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2730 #define STRUCT_SPECIAL(name)
2731 #include "syscall_types.h"
2733 #undef STRUCT_SPECIAL
2735 typedef struct IOCTLEntry {
2736 unsigned int target_cmd;
2737 unsigned int host_cmd;
2740 const argtype arg_type[5];
2743 #define IOC_R 0x0001
2744 #define IOC_W 0x0002
2745 #define IOC_RW (IOC_R | IOC_W)
2747 #define MAX_STRUCT_SIZE 4096
2749 static IOCTLEntry ioctl_entries[] = {
2750 #define IOCTL(cmd, access, types...) \
2751 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2756 /* ??? Implement proper locking for ioctls. */
2757 /* do_ioctl() Must return target values and target errnos. */
2758 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
2760 const IOCTLEntry *ie;
2761 const argtype *arg_type;
2763 uint8_t buf_temp[MAX_STRUCT_SIZE];
2769 if (ie->target_cmd == 0) {
2770 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
2771 return -TARGET_ENOSYS;
2773 if (ie->target_cmd == cmd)
2777 arg_type = ie->arg_type;
2779 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2781 switch(arg_type[0]) {
2784 ret = get_errno(ioctl(fd, ie->host_cmd));
2789 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2793 target_size = thunk_type_size(arg_type, 0);
2794 switch(ie->access) {
2796 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2797 if (!is_error(ret)) {
2798 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2800 return -TARGET_EFAULT;
2801 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2802 unlock_user(argptr, arg, target_size);
2806 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2808 return -TARGET_EFAULT;
2809 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2810 unlock_user(argptr, arg, 0);
2811 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2815 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2817 return -TARGET_EFAULT;
2818 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2819 unlock_user(argptr, arg, 0);
2820 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2821 if (!is_error(ret)) {
2822 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2824 return -TARGET_EFAULT;
2825 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2826 unlock_user(argptr, arg, target_size);
2832 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2833 (long)cmd, arg_type[0]);
2834 ret = -TARGET_ENOSYS;
2840 static const bitmask_transtbl iflag_tbl[] = {
2841 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2842 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2843 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2844 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2845 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2846 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2847 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2848 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2849 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2850 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2851 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2852 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2853 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2854 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2858 static const bitmask_transtbl oflag_tbl[] = {
2859 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2860 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2861 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2862 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2863 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2864 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2865 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2866 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2867 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2868 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2869 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2870 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2871 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2872 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2873 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2874 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2875 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2876 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2877 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2878 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2879 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2880 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2881 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2882 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2886 static const bitmask_transtbl cflag_tbl[] = {
2887 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2888 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2889 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2890 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2891 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2892 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2893 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2894 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2895 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2896 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2897 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2898 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2899 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2900 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2901 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2902 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2903 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2904 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2905 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2906 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2907 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2908 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2909 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2910 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2911 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2912 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2913 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2914 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2915 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2916 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2917 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2921 static const bitmask_transtbl lflag_tbl[] = {
2922 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2923 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2924 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2925 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2926 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2927 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2928 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2929 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2930 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2931 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2932 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2933 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2934 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2935 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2936 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2940 static void target_to_host_termios (void *dst, const void *src)
2942 struct host_termios *host = dst;
2943 const struct target_termios *target = src;
2946 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2948 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2950 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2952 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2953 host->c_line = target->c_line;
2955 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2956 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2957 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2958 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2959 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2960 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2961 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2962 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2963 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2964 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2965 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2966 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2967 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2968 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2969 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2970 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2971 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2974 static void host_to_target_termios (void *dst, const void *src)
2976 struct target_termios *target = dst;
2977 const struct host_termios *host = src;
2980 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2982 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2984 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2986 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2987 target->c_line = host->c_line;
2989 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2990 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2991 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2992 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2993 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2994 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2995 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2996 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2997 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2998 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2999 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3000 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3001 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3002 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3003 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3004 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3005 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3008 static const StructEntry struct_termios_def = {
3009 .convert = { host_to_target_termios, target_to_host_termios },
3010 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3011 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3014 static bitmask_transtbl mmap_flags_tbl[] = {
3015 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3016 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3017 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3018 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3019 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3020 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3021 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3022 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3026 #if defined(TARGET_I386)
3028 /* NOTE: there is really one LDT for all the threads */
3029 static uint8_t *ldt_table;
3031 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
3038 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3039 if (size > bytecount)
3041 p = lock_user(VERIFY_WRITE, ptr, size, 0);
3043 return -TARGET_EFAULT;
3044 /* ??? Should this by byteswapped? */
3045 memcpy(p, ldt_table, size);
3046 unlock_user(p, ptr, size);
3050 /* XXX: add locking support */
3051 static abi_long write_ldt(CPUX86State *env,
3052 abi_ulong ptr, unsigned long bytecount, int oldmode)
3054 struct target_modify_ldt_ldt_s ldt_info;
3055 struct target_modify_ldt_ldt_s *target_ldt_info;
3056 int seg_32bit, contents, read_exec_only, limit_in_pages;
3057 int seg_not_present, useable, lm;
3058 uint32_t *lp, entry_1, entry_2;
3060 if (bytecount != sizeof(ldt_info))
3061 return -TARGET_EINVAL;
3062 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
3063 return -TARGET_EFAULT;
3064 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3065 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3066 ldt_info.limit = tswap32(target_ldt_info->limit);
3067 ldt_info.flags = tswap32(target_ldt_info->flags);
3068 unlock_user_struct(target_ldt_info, ptr, 0);
3070 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
3071 return -TARGET_EINVAL;
3072 seg_32bit = ldt_info.flags & 1;
3073 contents = (ldt_info.flags >> 1) & 3;
3074 read_exec_only = (ldt_info.flags >> 3) & 1;
3075 limit_in_pages = (ldt_info.flags >> 4) & 1;
3076 seg_not_present = (ldt_info.flags >> 5) & 1;
3077 useable = (ldt_info.flags >> 6) & 1;
3081 lm = (ldt_info.flags >> 7) & 1;
3083 if (contents == 3) {
3085 return -TARGET_EINVAL;
3086 if (seg_not_present == 0)
3087 return -TARGET_EINVAL;
3089 /* allocate the LDT */
3091 env->ldt.base = target_mmap(0,
3092 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3093 PROT_READ|PROT_WRITE,
3094 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3095 if (env->ldt.base == -1)
3096 return -TARGET_ENOMEM;
3097 memset(g2h(env->ldt.base), 0,
3098 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
3099 env->ldt.limit = 0xffff;
3100 ldt_table = g2h(env->ldt.base);
3103 /* NOTE: same code as Linux kernel */
3104 /* Allow LDTs to be cleared by the user. */
3105 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3108 read_exec_only == 1 &&
3110 limit_in_pages == 0 &&
3111 seg_not_present == 1 &&
3119 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3120 (ldt_info.limit & 0x0ffff);
3121 entry_2 = (ldt_info.base_addr & 0xff000000) |
3122 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3123 (ldt_info.limit & 0xf0000) |
3124 ((read_exec_only ^ 1) << 9) |
3126 ((seg_not_present ^ 1) << 15) |
3128 (limit_in_pages << 23) |
3132 entry_2 |= (useable << 20);
3134 /* Install the new entry ... */
3136 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3137 lp[0] = tswap32(entry_1);
3138 lp[1] = tswap32(entry_2);
3142 /* specific and weird i386 syscalls */
3143 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3144 unsigned long bytecount)
3150 ret = read_ldt(ptr, bytecount);
3153 ret = write_ldt(env, ptr, bytecount, 1);
3156 ret = write_ldt(env, ptr, bytecount, 0);
3159 ret = -TARGET_ENOSYS;
3165 #if defined(TARGET_I386) && defined(TARGET_ABI32)
3166 static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
3168 uint64_t *gdt_table = g2h(env->gdt.base);
3169 struct target_modify_ldt_ldt_s ldt_info;
3170 struct target_modify_ldt_ldt_s *target_ldt_info;
3171 int seg_32bit, contents, read_exec_only, limit_in_pages;
3172 int seg_not_present, useable, lm;
3173 uint32_t *lp, entry_1, entry_2;
3176 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3177 if (!target_ldt_info)
3178 return -TARGET_EFAULT;
3179 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3180 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3181 ldt_info.limit = tswap32(target_ldt_info->limit);
3182 ldt_info.flags = tswap32(target_ldt_info->flags);
3183 if (ldt_info.entry_number == -1) {
3184 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3185 if (gdt_table[i] == 0) {
3186 ldt_info.entry_number = i;
3187 target_ldt_info->entry_number = tswap32(i);
3192 unlock_user_struct(target_ldt_info, ptr, 1);
3194 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3195 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3196 return -TARGET_EINVAL;
3197 seg_32bit = ldt_info.flags & 1;
3198 contents = (ldt_info.flags >> 1) & 3;
3199 read_exec_only = (ldt_info.flags >> 3) & 1;
3200 limit_in_pages = (ldt_info.flags >> 4) & 1;
3201 seg_not_present = (ldt_info.flags >> 5) & 1;
3202 useable = (ldt_info.flags >> 6) & 1;
3206 lm = (ldt_info.flags >> 7) & 1;
3209 if (contents == 3) {
3210 if (seg_not_present == 0)
3211 return -TARGET_EINVAL;
3214 /* NOTE: same code as Linux kernel */
3215 /* Allow LDTs to be cleared by the user. */
3216 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3217 if ((contents == 0 &&
3218 read_exec_only == 1 &&
3220 limit_in_pages == 0 &&
3221 seg_not_present == 1 &&
3229 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3230 (ldt_info.limit & 0x0ffff);
3231 entry_2 = (ldt_info.base_addr & 0xff000000) |
3232 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3233 (ldt_info.limit & 0xf0000) |
3234 ((read_exec_only ^ 1) << 9) |
3236 ((seg_not_present ^ 1) << 15) |
3238 (limit_in_pages << 23) |
3243 /* Install the new entry ... */
3245 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3246 lp[0] = tswap32(entry_1);
3247 lp[1] = tswap32(entry_2);
3251 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
3253 struct target_modify_ldt_ldt_s *target_ldt_info;
3254 uint64_t *gdt_table = g2h(env->gdt.base);
3255 uint32_t base_addr, limit, flags;
3256 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3257 int seg_not_present, useable, lm;
3258 uint32_t *lp, entry_1, entry_2;
3260 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3261 if (!target_ldt_info)
3262 return -TARGET_EFAULT;
3263 idx = tswap32(target_ldt_info->entry_number);
3264 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3265 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3266 unlock_user_struct(target_ldt_info, ptr, 1);
3267 return -TARGET_EINVAL;
3269 lp = (uint32_t *)(gdt_table + idx);
3270 entry_1 = tswap32(lp[0]);
3271 entry_2 = tswap32(lp[1]);
3273 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3274 contents = (entry_2 >> 10) & 3;
3275 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3276 seg_32bit = (entry_2 >> 22) & 1;
3277 limit_in_pages = (entry_2 >> 23) & 1;
3278 useable = (entry_2 >> 20) & 1;
3282 lm = (entry_2 >> 21) & 1;
3284 flags = (seg_32bit << 0) | (contents << 1) |
3285 (read_exec_only << 3) | (limit_in_pages << 4) |
3286 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3287 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3288 base_addr = (entry_1 >> 16) |
3289 (entry_2 & 0xff000000) |
3290 ((entry_2 & 0xff) << 16);
3291 target_ldt_info->base_addr = tswapl(base_addr);
3292 target_ldt_info->limit = tswap32(limit);
3293 target_ldt_info->flags = tswap32(flags);
3294 unlock_user_struct(target_ldt_info, ptr, 1);
3297 #endif /* TARGET_I386 && TARGET_ABI32 */
3299 #ifndef TARGET_ABI32
3300 static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
3307 case TARGET_ARCH_SET_GS:
3308 case TARGET_ARCH_SET_FS:
3309 if (code == TARGET_ARCH_SET_GS)
3313 cpu_x86_load_seg(env, idx, 0);
3314 env->segs[idx].base = addr;
3316 case TARGET_ARCH_GET_GS:
3317 case TARGET_ARCH_GET_FS:
3318 if (code == TARGET_ARCH_GET_GS)
3322 val = env->segs[idx].base;
3323 if (put_user(val, addr, abi_ulong))
3324 return -TARGET_EFAULT;
3327 ret = -TARGET_EINVAL;
3334 #endif /* defined(TARGET_I386) */
3336 #if defined(USE_NPTL)
3338 #define NEW_STACK_SIZE PTHREAD_STACK_MIN
3340 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3343 pthread_mutex_t mutex;
3344 pthread_cond_t cond;
3348 abi_ulong child_tidptr;
3349 abi_ulong parent_tidptr;
3353 static void *clone_func(void *arg)
3355 new_thread_info *info = arg;
3360 info->tid = gettid();
3361 if (info->child_tidptr)
3362 put_user_u32(info->tid, info->child_tidptr);
3363 if (info->parent_tidptr)
3364 put_user_u32(info->tid, info->parent_tidptr);
3365 /* Enable signals. */
3366 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3367 /* Signal to the parent that we're ready. */
3368 pthread_mutex_lock(&info->mutex);
3369 pthread_cond_broadcast(&info->cond);
3370 pthread_mutex_unlock(&info->mutex);
3371 /* Wait until the parent has finshed initializing the tls state. */
3372 pthread_mutex_lock(&clone_lock);
3373 pthread_mutex_unlock(&clone_lock);
3379 /* this stack is the equivalent of the kernel stack associated with a
3381 #define NEW_STACK_SIZE 8192
3383 static int clone_func(void *arg)
3385 CPUState *env = arg;
3392 /* do_fork() Must return host values and target errnos (unlike most
3393 do_*() functions). */
3394 static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3395 abi_ulong parent_tidptr, target_ulong newtls,
3396 abi_ulong child_tidptr)
3402 #if defined(USE_NPTL)
3403 unsigned int nptl_flags;
3407 /* Emulate vfork() with fork() */
3408 if (flags & CLONE_VFORK)
3409 flags &= ~(CLONE_VFORK | CLONE_VM);
3411 if (flags & CLONE_VM) {
3412 #if defined(USE_NPTL)
3413 new_thread_info info;
3414 pthread_attr_t attr;
3416 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
3417 init_task_state(ts);
3418 new_stack = ts->stack;
3419 /* we create a new CPU instance. */
3420 new_env = cpu_copy(env);
3421 /* Init regs that differ from the parent. */
3422 cpu_clone_regs(new_env, newsp);
3423 new_env->opaque = ts;
3424 #if defined(USE_NPTL)
3426 flags &= ~CLONE_NPTL_FLAGS2;
3428 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3429 ts->child_tidptr = child_tidptr;
3432 if (nptl_flags & CLONE_SETTLS)
3433 cpu_set_tls (new_env, newtls);
3435 /* Grab a mutex so that thread setup appears atomic. */
3436 pthread_mutex_lock(&clone_lock);
3438 memset(&info, 0, sizeof(info));
3439 pthread_mutex_init(&info.mutex, NULL);
3440 pthread_mutex_lock(&info.mutex);
3441 pthread_cond_init(&info.cond, NULL);
3443 if (nptl_flags & CLONE_CHILD_SETTID)
3444 info.child_tidptr = child_tidptr;
3445 if (nptl_flags & CLONE_PARENT_SETTID)
3446 info.parent_tidptr = parent_tidptr;
3448 ret = pthread_attr_init(&attr);
3449 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
3450 /* It is not safe to deliver signals until the child has finished
3451 initializing, so temporarily block all signals. */
3452 sigfillset(&sigmask);
3453 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3455 ret = pthread_create(&info.thread, &attr, clone_func, &info);
3456 /* TODO: Free new CPU state if thread creation failed. */
3458 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3459 pthread_attr_destroy(&attr);
3461 /* Wait for the child to initialize. */
3462 pthread_cond_wait(&info.cond, &info.mutex);
3464 if (flags & CLONE_PARENT_SETTID)
3465 put_user_u32(ret, parent_tidptr);
3469 pthread_mutex_unlock(&info.mutex);
3470 pthread_cond_destroy(&info.cond);
3471 pthread_mutex_destroy(&info.mutex);
3472 pthread_mutex_unlock(&clone_lock);
3474 if (flags & CLONE_NPTL_FLAGS2)
3476 /* This is probably going to die very quickly, but do it anyway. */
3478 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3480 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3484 /* if no CLONE_VM, we consider it is a fork */
3485 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
3490 /* Child Process. */
3491 cpu_clone_regs(env, newsp);
3493 #if defined(USE_NPTL)
3494 /* There is a race condition here. The parent process could
3495 theoretically read the TID in the child process before the child
3496 tid is set. This would require using either ptrace
3497 (not implemented) or having *_tidptr to point at a shared memory
3498 mapping. We can't repeat the spinlock hack used above because
3499 the child process gets its own copy of the lock. */
3500 if (flags & CLONE_CHILD_SETTID)
3501 put_user_u32(gettid(), child_tidptr);
3502 if (flags & CLONE_PARENT_SETTID)
3503 put_user_u32(gettid(), parent_tidptr);
3504 ts = (TaskState *)env->opaque;
3505 if (flags & CLONE_SETTLS)
3506 cpu_set_tls (env, newtls);
3507 if (flags & CLONE_CHILD_CLEARTID)
3508 ts->child_tidptr = child_tidptr;
3517 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
3520 struct target_flock *target_fl;
3521 struct flock64 fl64;
3522 struct target_flock64 *target_fl64;
3526 case TARGET_F_GETLK:
3527 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3528 return -TARGET_EFAULT;
3529 fl.l_type = tswap16(target_fl->l_type);
3530 fl.l_whence = tswap16(target_fl->l_whence);
3531 fl.l_start = tswapl(target_fl->l_start);
3532 fl.l_len = tswapl(target_fl->l_len);
3533 fl.l_pid = tswapl(target_fl->l_pid);
3534 unlock_user_struct(target_fl, arg, 0);
3535 ret = get_errno(fcntl(fd, cmd, &fl));
3537 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3538 return -TARGET_EFAULT;
3539 target_fl->l_type = tswap16(fl.l_type);
3540 target_fl->l_whence = tswap16(fl.l_whence);
3541 target_fl->l_start = tswapl(fl.l_start);
3542 target_fl->l_len = tswapl(fl.l_len);
3543 target_fl->l_pid = tswapl(fl.l_pid);
3544 unlock_user_struct(target_fl, arg, 1);
3548 case TARGET_F_SETLK:
3549 case TARGET_F_SETLKW:
3550 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3551 return -TARGET_EFAULT;
3552 fl.l_type = tswap16(target_fl->l_type);
3553 fl.l_whence = tswap16(target_fl->l_whence);
3554 fl.l_start = tswapl(target_fl->l_start);
3555 fl.l_len = tswapl(target_fl->l_len);
3556 fl.l_pid = tswapl(target_fl->l_pid);
3557 unlock_user_struct(target_fl, arg, 0);
3558 ret = get_errno(fcntl(fd, cmd, &fl));
3561 case TARGET_F_GETLK64:
3562 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3563 return -TARGET_EFAULT;
3564 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3565 fl64.l_whence = tswap16(target_fl64->l_whence);
3566 fl64.l_start = tswapl(target_fl64->l_start);
3567 fl64.l_len = tswapl(target_fl64->l_len);
3568 fl64.l_pid = tswap16(target_fl64->l_pid);
3569 unlock_user_struct(target_fl64, arg, 0);
3570 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
3572 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3573 return -TARGET_EFAULT;
3574 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3575 target_fl64->l_whence = tswap16(fl64.l_whence);
3576 target_fl64->l_start = tswapl(fl64.l_start);
3577 target_fl64->l_len = tswapl(fl64.l_len);
3578 target_fl64->l_pid = tswapl(fl64.l_pid);
3579 unlock_user_struct(target_fl64, arg, 1);
3582 case TARGET_F_SETLK64:
3583 case TARGET_F_SETLKW64:
3584 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3585 return -TARGET_EFAULT;
3586 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3587 fl64.l_whence = tswap16(target_fl64->l_whence);
3588 fl64.l_start = tswapl(target_fl64->l_start);
3589 fl64.l_len = tswapl(target_fl64->l_len);
3590 fl64.l_pid = tswap16(target_fl64->l_pid);
3591 unlock_user_struct(target_fl64, arg, 0);
3592 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
3596 ret = get_errno(fcntl(fd, cmd, arg));
3598 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3603 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
3607 ret = get_errno(fcntl(fd, cmd, arg));
3615 static inline int high2lowuid(int uid)
3623 static inline int high2lowgid(int gid)
3631 static inline int low2highuid(int uid)
3633 if ((int16_t)uid == -1)
3639 static inline int low2highgid(int gid)
3641 if ((int16_t)gid == -1)
3647 #endif /* USE_UID16 */
3649 void syscall_init(void)
3652 const argtype *arg_type;
3656 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
3657 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
3658 #include "syscall_types.h"
3660 #undef STRUCT_SPECIAL
3662 /* we patch the ioctl size if necessary. We rely on the fact that
3663 no ioctl has all the bits at '1' in the size field */
3665 while (ie->target_cmd != 0) {
3666 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3667 TARGET_IOC_SIZEMASK) {
3668 arg_type = ie->arg_type;
3669 if (arg_type[0] != TYPE_PTR) {
3670 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
3675 size = thunk_type_size(arg_type, 0);
3676 ie->target_cmd = (ie->target_cmd &
3677 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3678 (size << TARGET_IOC_SIZESHIFT);
3681 /* Build target_to_host_errno_table[] table from
3682 * host_to_target_errno_table[]. */
3683 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3684 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3686 /* automatic consistency check if same arch */
3687 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3688 (defined(__x86_64__) && defined(TARGET_X86_64))
3689 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3690 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3691 ie->name, ie->target_cmd, ie->host_cmd);
3698 #if TARGET_ABI_BITS == 32
3699 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3701 #ifdef TARGET_WORDS_BIGENDIAN
3702 return ((uint64_t)word0 << 32) | word1;
3704 return ((uint64_t)word1 << 32) | word0;
3707 #else /* TARGET_ABI_BITS == 32 */
3708 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3712 #endif /* TARGET_ABI_BITS != 32 */
3714 #ifdef TARGET_NR_truncate64
3715 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3721 if (((CPUARMState *)cpu_env)->eabi)
3727 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3731 #ifdef TARGET_NR_ftruncate64
3732 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3738 if (((CPUARMState *)cpu_env)->eabi)
3744 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3748 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3749 abi_ulong target_addr)
3751 struct target_timespec *target_ts;
3753 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3754 return -TARGET_EFAULT;
3755 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3756 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3757 unlock_user_struct(target_ts, target_addr, 0);
3761 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3762 struct timespec *host_ts)
3764 struct target_timespec *target_ts;
3766 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3767 return -TARGET_EFAULT;
3768 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3769 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3770 unlock_user_struct(target_ts, target_addr, 1);
3774 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
3775 static inline abi_long host_to_target_stat64(void *cpu_env,
3776 abi_ulong target_addr,
3777 struct stat *host_st)
3780 if (((CPUARMState *)cpu_env)->eabi) {
3781 struct target_eabi_stat64 *target_st;
3783 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3784 return -TARGET_EFAULT;
3785 memset(target_st, 0, sizeof(struct target_eabi_stat64));
3786 __put_user(host_st->st_dev, &target_st->st_dev);
3787 __put_user(host_st->st_ino, &target_st->st_ino);
3788 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3789 __put_user(host_st->st_ino, &target_st->__st_ino);
3791 __put_user(host_st->st_mode, &target_st->st_mode);
3792 __put_user(host_st->st_nlink, &target_st->st_nlink);
3793 __put_user(host_st->st_uid, &target_st->st_uid);
3794 __put_user(host_st->st_gid, &target_st->st_gid);
3795 __put_user(host_st->st_rdev, &target_st->st_rdev);
3796 __put_user(host_st->st_size, &target_st->st_size);
3797 __put_user(host_st->st_blksize, &target_st->st_blksize);
3798 __put_user(host_st->st_blocks, &target_st->st_blocks);
3799 __put_user(host_st->st_atime, &target_st->target_st_atime);
3800 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3801 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3802 unlock_user_struct(target_st, target_addr, 1);
3806 #if TARGET_LONG_BITS == 64
3807 struct target_stat *target_st;
3809 struct target_stat64 *target_st;
3812 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3813 return -TARGET_EFAULT;
3814 memset(target_st, 0, sizeof(*target_st));
3815 __put_user(host_st->st_dev, &target_st->st_dev);
3816 __put_user(host_st->st_ino, &target_st->st_ino);
3817 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3818 __put_user(host_st->st_ino, &target_st->__st_ino);
3820 __put_user(host_st->st_mode, &target_st->st_mode);
3821 __put_user(host_st->st_nlink, &target_st->st_nlink);
3822 __put_user(host_st->st_uid, &target_st->st_uid);
3823 __put_user(host_st->st_gid, &target_st->st_gid);
3824 __put_user(host_st->st_rdev, &target_st->st_rdev);
3825 /* XXX: better use of kernel struct */
3826 __put_user(host_st->st_size, &target_st->st_size);
3827 __put_user(host_st->st_blksize, &target_st->st_blksize);
3828 __put_user(host_st->st_blocks, &target_st->st_blocks);
3829 __put_user(host_st->st_atime, &target_st->target_st_atime);
3830 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3831 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3832 unlock_user_struct(target_st, target_addr, 1);
3839 #if defined(USE_NPTL)
3840 /* ??? Using host futex calls even when target atomic operations
3841 are not really atomic probably breaks things. However implementing
3842 futexes locally would make futexes shared between multiple processes
3843 tricky. However they're probably useless because guest atomic
3844 operations won't work either. */
3845 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
3846 target_ulong uaddr2, int val3)
3848 struct timespec ts, *pts;
3850 /* ??? We assume FUTEX_* constants are the same on both host
3856 target_to_host_timespec(pts, timeout);
3860 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAIT, tswap32(val),
3863 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAKE, val, NULL, NULL, 0));
3865 return get_errno(sys_futex(g2h(uaddr), FUTEX_FD, val, NULL, NULL, 0));
3867 return get_errno(sys_futex(g2h(uaddr), FUTEX_REQUEUE, val,
3868 NULL, g2h(uaddr2), 0));
3869 case FUTEX_CMP_REQUEUE:
3870 return get_errno(sys_futex(g2h(uaddr), FUTEX_CMP_REQUEUE, val,
3871 NULL, g2h(uaddr2), tswap32(val3)));
3873 return -TARGET_ENOSYS;
3878 int get_osversion(void)
3880 static int osversion;
3881 struct new_utsname buf;
3886 if (qemu_uname_release && *qemu_uname_release) {
3887 s = qemu_uname_release;
3889 if (sys_uname(&buf))
3894 for (i = 0; i < 3; i++) {
3896 while (*s >= '0' && *s <= '9') {
3901 tmp = (tmp << 8) + n;
3909 /* do_syscall() should always have a single exit point at the end so
3910 that actions, such as logging of syscall results, can be performed.
3911 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
3912 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3913 abi_long arg2, abi_long arg3, abi_long arg4,
3914 abi_long arg5, abi_long arg6)
3922 gemu_log("syscall %d", num);
3925 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3928 case TARGET_NR_exit:
3930 /* In old applications this may be used to implement _exit(2).
3931 However in threaded applictions it is used for thread termination,
3932 and _exit_group is used for application termination.
3933 Do thread termination if we have more then one thread. */
3934 /* FIXME: This probably breaks if a signal arrives. We should probably
3935 be disabling signals. */
3936 if (first_cpu->next_cpu) {
3943 while (p && p != (CPUState *)cpu_env) {
3944 lastp = &p->next_cpu;
3947 /* If we didn't find the CPU for this thread then something is
3951 /* Remove the CPU from the list. */
3952 *lastp = p->next_cpu;
3954 TaskState *ts = ((CPUState *)cpu_env)->opaque;
3955 if (ts->child_tidptr) {
3956 put_user_u32(0, ts->child_tidptr);
3957 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
3960 /* TODO: Free CPU state. */
3967 gdb_exit(cpu_env, arg1);
3969 ret = 0; /* avoid warning */
3971 case TARGET_NR_read:
3975 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3977 ret = get_errno(read(arg1, p, arg3));
3978 unlock_user(p, arg2, ret);
3981 case TARGET_NR_write:
3982 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3984 ret = get_errno(write(arg1, p, arg3));
3985 unlock_user(p, arg2, 0);
3987 case TARGET_NR_open:
3988 if (!(p = lock_user_string(arg1)))
3990 ret = get_errno(open(path(p),
3991 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3993 unlock_user(p, arg1, 0);
3995 #if defined(TARGET_NR_openat) && defined(__NR_openat)
3996 case TARGET_NR_openat:
3997 if (!(p = lock_user_string(arg2)))
3999 ret = get_errno(sys_openat(arg1,
4001 target_to_host_bitmask(arg3, fcntl_flags_tbl),
4003 unlock_user(p, arg2, 0);
4006 case TARGET_NR_close:
4007 ret = get_errno(close(arg1));
4012 case TARGET_NR_fork:
4013 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
4015 #ifdef TARGET_NR_waitpid
4016 case TARGET_NR_waitpid:
4019 ret = get_errno(waitpid(arg1, &status, arg3));
4020 if (!is_error(ret) && arg2
4021 && put_user_s32(status, arg2))
4026 #ifdef TARGET_NR_waitid
4027 case TARGET_NR_waitid:
4031 ret = get_errno(waitid(arg1, arg2, &info, arg4));
4032 if (!is_error(ret) && arg3 && info.si_pid != 0) {
4033 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
4035 host_to_target_siginfo(p, &info);
4036 unlock_user(p, arg3, sizeof(target_siginfo_t));
4041 #ifdef TARGET_NR_creat /* not on alpha */
4042 case TARGET_NR_creat:
4043 if (!(p = lock_user_string(arg1)))
4045 ret = get_errno(creat(p, arg2));
4046 unlock_user(p, arg1, 0);
4049 case TARGET_NR_link:
4052 p = lock_user_string(arg1);
4053 p2 = lock_user_string(arg2);
4055 ret = -TARGET_EFAULT;
4057 ret = get_errno(link(p, p2));
4058 unlock_user(p2, arg2, 0);
4059 unlock_user(p, arg1, 0);
4062 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4063 case TARGET_NR_linkat:
4068 p = lock_user_string(arg2);
4069 p2 = lock_user_string(arg4);
4071 ret = -TARGET_EFAULT;
4073 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
4074 unlock_user(p, arg2, 0);
4075 unlock_user(p2, arg4, 0);
4079 case TARGET_NR_unlink:
4080 if (!(p = lock_user_string(arg1)))
4082 ret = get_errno(unlink(p));
4083 unlock_user(p, arg1, 0);
4085 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4086 case TARGET_NR_unlinkat:
4087 if (!(p = lock_user_string(arg2)))
4089 ret = get_errno(sys_unlinkat(arg1, p, arg3));
4090 unlock_user(p, arg2, 0);
4093 case TARGET_NR_execve:
4095 char **argp, **envp;
4098 abi_ulong guest_argp;
4099 abi_ulong guest_envp;
4105 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
4106 if (get_user_ual(addr, gp))
4114 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
4115 if (get_user_ual(addr, gp))
4122 argp = alloca((argc + 1) * sizeof(void *));
4123 envp = alloca((envc + 1) * sizeof(void *));
4125 for (gp = guest_argp, q = argp; gp;
4126 gp += sizeof(abi_ulong), q++) {
4127 if (get_user_ual(addr, gp))
4131 if (!(*q = lock_user_string(addr)))
4136 for (gp = guest_envp, q = envp; gp;
4137 gp += sizeof(abi_ulong), q++) {
4138 if (get_user_ual(addr, gp))
4142 if (!(*q = lock_user_string(addr)))
4147 if (!(p = lock_user_string(arg1)))
4149 ret = get_errno(execve(p, argp, envp));
4150 unlock_user(p, arg1, 0);
4155 ret = -TARGET_EFAULT;
4158 for (gp = guest_argp, q = argp; *q;
4159 gp += sizeof(abi_ulong), q++) {
4160 if (get_user_ual(addr, gp)
4163 unlock_user(*q, addr, 0);
4165 for (gp = guest_envp, q = envp; *q;
4166 gp += sizeof(abi_ulong), q++) {
4167 if (get_user_ual(addr, gp)
4170 unlock_user(*q, addr, 0);
4174 case TARGET_NR_chdir:
4175 if (!(p = lock_user_string(arg1)))
4177 ret = get_errno(chdir(p));
4178 unlock_user(p, arg1, 0);
4180 #ifdef TARGET_NR_time
4181 case TARGET_NR_time:
4184 ret = get_errno(time(&host_time));
4187 && put_user_sal(host_time, arg1))
4192 case TARGET_NR_mknod:
4193 if (!(p = lock_user_string(arg1)))
4195 ret = get_errno(mknod(p, arg2, arg3));
4196 unlock_user(p, arg1, 0);
4198 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4199 case TARGET_NR_mknodat:
4200 if (!(p = lock_user_string(arg2)))
4202 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4203 unlock_user(p, arg2, 0);
4206 case TARGET_NR_chmod:
4207 if (!(p = lock_user_string(arg1)))
4209 ret = get_errno(chmod(p, arg2));
4210 unlock_user(p, arg1, 0);
4212 #ifdef TARGET_NR_break
4213 case TARGET_NR_break:
4216 #ifdef TARGET_NR_oldstat
4217 case TARGET_NR_oldstat:
4220 case TARGET_NR_lseek:
4221 ret = get_errno(lseek(arg1, arg2, arg3));
4223 #ifdef TARGET_NR_getxpid
4224 case TARGET_NR_getxpid:
4226 case TARGET_NR_getpid:
4228 ret = get_errno(getpid());
4230 case TARGET_NR_mount:
4232 /* need to look at the data field */
4234 p = lock_user_string(arg1);
4235 p2 = lock_user_string(arg2);
4236 p3 = lock_user_string(arg3);
4237 if (!p || !p2 || !p3)
4238 ret = -TARGET_EFAULT;
4240 /* FIXME - arg5 should be locked, but it isn't clear how to
4241 * do that since it's not guaranteed to be a NULL-terminated
4244 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4245 unlock_user(p, arg1, 0);
4246 unlock_user(p2, arg2, 0);
4247 unlock_user(p3, arg3, 0);
4250 #ifdef TARGET_NR_umount
4251 case TARGET_NR_umount:
4252 if (!(p = lock_user_string(arg1)))
4254 ret = get_errno(umount(p));
4255 unlock_user(p, arg1, 0);
4258 #ifdef TARGET_NR_stime /* not on alpha */
4259 case TARGET_NR_stime:
4262 if (get_user_sal(host_time, arg1))
4264 ret = get_errno(stime(&host_time));
4268 case TARGET_NR_ptrace:
4270 #ifdef TARGET_NR_alarm /* not on alpha */
4271 case TARGET_NR_alarm:
4275 #ifdef TARGET_NR_oldfstat
4276 case TARGET_NR_oldfstat:
4279 #ifdef TARGET_NR_pause /* not on alpha */
4280 case TARGET_NR_pause:
4281 ret = get_errno(pause());
4284 #ifdef TARGET_NR_utime
4285 case TARGET_NR_utime:
4287 struct utimbuf tbuf, *host_tbuf;
4288 struct target_utimbuf *target_tbuf;
4290 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4292 tbuf.actime = tswapl(target_tbuf->actime);
4293 tbuf.modtime = tswapl(target_tbuf->modtime);
4294 unlock_user_struct(target_tbuf, arg2, 0);
4299 if (!(p = lock_user_string(arg1)))
4301 ret = get_errno(utime(p, host_tbuf));
4302 unlock_user(p, arg1, 0);
4306 case TARGET_NR_utimes:
4308 struct timeval *tvp, tv[2];
4310 if (copy_from_user_timeval(&tv[0], arg2)
4311 || copy_from_user_timeval(&tv[1],
4312 arg2 + sizeof(struct target_timeval)))
4318 if (!(p = lock_user_string(arg1)))
4320 ret = get_errno(utimes(p, tvp));
4321 unlock_user(p, arg1, 0);
4324 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4325 case TARGET_NR_futimesat:
4327 struct timeval *tvp, tv[2];
4329 if (copy_from_user_timeval(&tv[0], arg3)
4330 || copy_from_user_timeval(&tv[1],
4331 arg3 + sizeof(struct target_timeval)))
4337 if (!(p = lock_user_string(arg2)))
4339 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4340 unlock_user(p, arg2, 0);
4344 #ifdef TARGET_NR_stty
4345 case TARGET_NR_stty:
4348 #ifdef TARGET_NR_gtty
4349 case TARGET_NR_gtty:
4352 case TARGET_NR_access:
4353 if (!(p = lock_user_string(arg1)))
4355 ret = get_errno(access(p, arg2));
4356 unlock_user(p, arg1, 0);
4358 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4359 case TARGET_NR_faccessat:
4360 if (!(p = lock_user_string(arg2)))
4362 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
4363 unlock_user(p, arg2, 0);
4366 #ifdef TARGET_NR_nice /* not on alpha */
4367 case TARGET_NR_nice:
4368 ret = get_errno(nice(arg1));
4371 #ifdef TARGET_NR_ftime
4372 case TARGET_NR_ftime:
4375 case TARGET_NR_sync:
4379 case TARGET_NR_kill:
4380 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
4382 case TARGET_NR_rename:
4385 p = lock_user_string(arg1);
4386 p2 = lock_user_string(arg2);
4388 ret = -TARGET_EFAULT;
4390 ret = get_errno(rename(p, p2));
4391 unlock_user(p2, arg2, 0);
4392 unlock_user(p, arg1, 0);
4395 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4396 case TARGET_NR_renameat:
4399 p = lock_user_string(arg2);
4400 p2 = lock_user_string(arg4);
4402 ret = -TARGET_EFAULT;
4404 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
4405 unlock_user(p2, arg4, 0);
4406 unlock_user(p, arg2, 0);
4410 case TARGET_NR_mkdir:
4411 if (!(p = lock_user_string(arg1)))
4413 ret = get_errno(mkdir(p, arg2));
4414 unlock_user(p, arg1, 0);
4416 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4417 case TARGET_NR_mkdirat:
4418 if (!(p = lock_user_string(arg2)))
4420 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4421 unlock_user(p, arg2, 0);
4424 case TARGET_NR_rmdir:
4425 if (!(p = lock_user_string(arg1)))
4427 ret = get_errno(rmdir(p));
4428 unlock_user(p, arg1, 0);
4431 ret = get_errno(dup(arg1));
4433 case TARGET_NR_pipe:
4436 ret = get_errno(pipe(host_pipe));
4437 if (!is_error(ret)) {
4438 #if defined(TARGET_MIPS)
4439 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
4440 env->active_tc.gpr[3] = host_pipe[1];
4442 #elif defined(TARGET_SH4)
4443 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
4446 if (put_user_s32(host_pipe[0], arg1)
4447 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
4453 case TARGET_NR_times:
4455 struct target_tms *tmsp;
4457 ret = get_errno(times(&tms));
4459 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4462 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4463 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4464 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4465 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
4468 ret = host_to_target_clock_t(ret);
4471 #ifdef TARGET_NR_prof
4472 case TARGET_NR_prof:
4475 #ifdef TARGET_NR_signal
4476 case TARGET_NR_signal:
4479 case TARGET_NR_acct:
4481 ret = get_errno(acct(NULL));
4483 if (!(p = lock_user_string(arg1)))
4485 ret = get_errno(acct(path(p)));
4486 unlock_user(p, arg1, 0);
4489 #ifdef TARGET_NR_umount2 /* not on alpha */
4490 case TARGET_NR_umount2:
4491 if (!(p = lock_user_string(arg1)))
4493 ret = get_errno(umount2(p, arg2));
4494 unlock_user(p, arg1, 0);
4497 #ifdef TARGET_NR_lock
4498 case TARGET_NR_lock:
4501 case TARGET_NR_ioctl:
4502 ret = do_ioctl(arg1, arg2, arg3);
4504 case TARGET_NR_fcntl:
4505 ret = do_fcntl(arg1, arg2, arg3);
4507 #ifdef TARGET_NR_mpx
4511 case TARGET_NR_setpgid:
4512 ret = get_errno(setpgid(arg1, arg2));
4514 #ifdef TARGET_NR_ulimit
4515 case TARGET_NR_ulimit:
4518 #ifdef TARGET_NR_oldolduname
4519 case TARGET_NR_oldolduname:
4522 case TARGET_NR_umask:
4523 ret = get_errno(umask(arg1));
4525 case TARGET_NR_chroot:
4526 if (!(p = lock_user_string(arg1)))
4528 ret = get_errno(chroot(p));
4529 unlock_user(p, arg1, 0);
4531 case TARGET_NR_ustat:
4533 case TARGET_NR_dup2:
4534 ret = get_errno(dup2(arg1, arg2));
4536 #ifdef TARGET_NR_getppid /* not on alpha */
4537 case TARGET_NR_getppid:
4538 ret = get_errno(getppid());
4541 case TARGET_NR_getpgrp:
4542 ret = get_errno(getpgrp());
4544 case TARGET_NR_setsid:
4545 ret = get_errno(setsid());
4547 #ifdef TARGET_NR_sigaction
4548 case TARGET_NR_sigaction:
4550 #if !defined(TARGET_MIPS)
4551 struct target_old_sigaction *old_act;
4552 struct target_sigaction act, oact, *pact;
4554 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4556 act._sa_handler = old_act->_sa_handler;
4557 target_siginitset(&act.sa_mask, old_act->sa_mask);
4558 act.sa_flags = old_act->sa_flags;
4559 act.sa_restorer = old_act->sa_restorer;
4560 unlock_user_struct(old_act, arg2, 0);
4565 ret = get_errno(do_sigaction(arg1, pact, &oact));
4566 if (!is_error(ret) && arg3) {
4567 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4569 old_act->_sa_handler = oact._sa_handler;
4570 old_act->sa_mask = oact.sa_mask.sig[0];
4571 old_act->sa_flags = oact.sa_flags;
4572 old_act->sa_restorer = oact.sa_restorer;
4573 unlock_user_struct(old_act, arg3, 1);
4576 struct target_sigaction act, oact, *pact, *old_act;
4579 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4581 act._sa_handler = old_act->_sa_handler;
4582 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4583 act.sa_flags = old_act->sa_flags;
4584 unlock_user_struct(old_act, arg2, 0);
4590 ret = get_errno(do_sigaction(arg1, pact, &oact));
4592 if (!is_error(ret) && arg3) {
4593 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4595 old_act->_sa_handler = oact._sa_handler;
4596 old_act->sa_flags = oact.sa_flags;
4597 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4598 old_act->sa_mask.sig[1] = 0;
4599 old_act->sa_mask.sig[2] = 0;
4600 old_act->sa_mask.sig[3] = 0;
4601 unlock_user_struct(old_act, arg3, 1);
4607 case TARGET_NR_rt_sigaction:
4609 struct target_sigaction *act;
4610 struct target_sigaction *oact;
4613 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4618 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4619 ret = -TARGET_EFAULT;
4620 goto rt_sigaction_fail;
4624 ret = get_errno(do_sigaction(arg1, act, oact));
4627 unlock_user_struct(act, arg2, 0);
4629 unlock_user_struct(oact, arg3, 1);
4632 #ifdef TARGET_NR_sgetmask /* not on alpha */
4633 case TARGET_NR_sgetmask:
4636 abi_ulong target_set;
4637 sigprocmask(0, NULL, &cur_set);
4638 host_to_target_old_sigset(&target_set, &cur_set);
4643 #ifdef TARGET_NR_ssetmask /* not on alpha */
4644 case TARGET_NR_ssetmask:
4646 sigset_t set, oset, cur_set;
4647 abi_ulong target_set = arg1;
4648 sigprocmask(0, NULL, &cur_set);
4649 target_to_host_old_sigset(&set, &target_set);
4650 sigorset(&set, &set, &cur_set);
4651 sigprocmask(SIG_SETMASK, &set, &oset);
4652 host_to_target_old_sigset(&target_set, &oset);
4657 #ifdef TARGET_NR_sigprocmask
4658 case TARGET_NR_sigprocmask:
4661 sigset_t set, oldset, *set_ptr;
4665 case TARGET_SIG_BLOCK:
4668 case TARGET_SIG_UNBLOCK:
4671 case TARGET_SIG_SETMASK:
4675 ret = -TARGET_EINVAL;
4678 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4680 target_to_host_old_sigset(&set, p);
4681 unlock_user(p, arg2, 0);
4687 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
4688 if (!is_error(ret) && arg3) {
4689 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4691 host_to_target_old_sigset(p, &oldset);
4692 unlock_user(p, arg3, sizeof(target_sigset_t));
4697 case TARGET_NR_rt_sigprocmask:
4700 sigset_t set, oldset, *set_ptr;
4704 case TARGET_SIG_BLOCK:
4707 case TARGET_SIG_UNBLOCK:
4710 case TARGET_SIG_SETMASK:
4714 ret = -TARGET_EINVAL;
4717 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4719 target_to_host_sigset(&set, p);
4720 unlock_user(p, arg2, 0);
4726 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
4727 if (!is_error(ret) && arg3) {
4728 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4730 host_to_target_sigset(p, &oldset);
4731 unlock_user(p, arg3, sizeof(target_sigset_t));
4735 #ifdef TARGET_NR_sigpending
4736 case TARGET_NR_sigpending:
4739 ret = get_errno(sigpending(&set));
4740 if (!is_error(ret)) {
4741 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4743 host_to_target_old_sigset(p, &set);
4744 unlock_user(p, arg1, sizeof(target_sigset_t));
4749 case TARGET_NR_rt_sigpending:
4752 ret = get_errno(sigpending(&set));
4753 if (!is_error(ret)) {
4754 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4756 host_to_target_sigset(p, &set);
4757 unlock_user(p, arg1, sizeof(target_sigset_t));
4761 #ifdef TARGET_NR_sigsuspend
4762 case TARGET_NR_sigsuspend:
4765 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4767 target_to_host_old_sigset(&set, p);
4768 unlock_user(p, arg1, 0);
4769 ret = get_errno(sigsuspend(&set));
4773 case TARGET_NR_rt_sigsuspend:
4776 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4778 target_to_host_sigset(&set, p);
4779 unlock_user(p, arg1, 0);
4780 ret = get_errno(sigsuspend(&set));
4783 case TARGET_NR_rt_sigtimedwait:
4786 struct timespec uts, *puts;
4789 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4791 target_to_host_sigset(&set, p);
4792 unlock_user(p, arg1, 0);
4795 target_to_host_timespec(puts, arg3);
4799 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
4800 if (!is_error(ret) && arg2) {
4801 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
4803 host_to_target_siginfo(p, &uinfo);
4804 unlock_user(p, arg2, sizeof(target_siginfo_t));
4808 case TARGET_NR_rt_sigqueueinfo:
4811 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
4813 target_to_host_siginfo(&uinfo, p);
4814 unlock_user(p, arg1, 0);
4815 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
4818 #ifdef TARGET_NR_sigreturn
4819 case TARGET_NR_sigreturn:
4820 /* NOTE: ret is eax, so not transcoding must be done */
4821 ret = do_sigreturn(cpu_env);
4824 case TARGET_NR_rt_sigreturn:
4825 /* NOTE: ret is eax, so not transcoding must be done */
4826 ret = do_rt_sigreturn(cpu_env);
4828 case TARGET_NR_sethostname:
4829 if (!(p = lock_user_string(arg1)))
4831 ret = get_errno(sethostname(p, arg2));
4832 unlock_user(p, arg1, 0);
4834 case TARGET_NR_setrlimit:
4836 /* XXX: convert resource ? */
4837 int resource = arg1;
4838 struct target_rlimit *target_rlim;
4840 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
4842 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4843 rlim.rlim_max = tswapl(target_rlim->rlim_max);
4844 unlock_user_struct(target_rlim, arg2, 0);
4845 ret = get_errno(setrlimit(resource, &rlim));
4848 case TARGET_NR_getrlimit:
4850 /* XXX: convert resource ? */
4851 int resource = arg1;
4852 struct target_rlimit *target_rlim;
4855 ret = get_errno(getrlimit(resource, &rlim));
4856 if (!is_error(ret)) {
4857 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4859 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4860 rlim.rlim_max = tswapl(target_rlim->rlim_max);
4861 unlock_user_struct(target_rlim, arg2, 1);
4865 case TARGET_NR_getrusage:
4867 struct rusage rusage;
4868 ret = get_errno(getrusage(arg1, &rusage));
4869 if (!is_error(ret)) {
4870 host_to_target_rusage(arg2, &rusage);
4874 case TARGET_NR_gettimeofday:
4877 ret = get_errno(gettimeofday(&tv, NULL));
4878 if (!is_error(ret)) {
4879 if (copy_to_user_timeval(arg1, &tv))
4884 case TARGET_NR_settimeofday:
4887 if (copy_from_user_timeval(&tv, arg1))
4889 ret = get_errno(settimeofday(&tv, NULL));
4892 #ifdef TARGET_NR_select
4893 case TARGET_NR_select:
4895 struct target_sel_arg_struct *sel;
4896 abi_ulong inp, outp, exp, tvp;
4899 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
4901 nsel = tswapl(sel->n);
4902 inp = tswapl(sel->inp);
4903 outp = tswapl(sel->outp);
4904 exp = tswapl(sel->exp);
4905 tvp = tswapl(sel->tvp);
4906 unlock_user_struct(sel, arg1, 0);
4907 ret = do_select(nsel, inp, outp, exp, tvp);
4911 case TARGET_NR_symlink:
4914 p = lock_user_string(arg1);
4915 p2 = lock_user_string(arg2);
4917 ret = -TARGET_EFAULT;
4919 ret = get_errno(symlink(p, p2));
4920 unlock_user(p2, arg2, 0);
4921 unlock_user(p, arg1, 0);
4924 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
4925 case TARGET_NR_symlinkat:
4928 p = lock_user_string(arg1);
4929 p2 = lock_user_string(arg3);
4931 ret = -TARGET_EFAULT;
4933 ret = get_errno(sys_symlinkat(p, arg2, p2));
4934 unlock_user(p2, arg3, 0);
4935 unlock_user(p, arg1, 0);
4939 #ifdef TARGET_NR_oldlstat
4940 case TARGET_NR_oldlstat:
4943 case TARGET_NR_readlink:
4946 p = lock_user_string(arg1);
4947 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4949 ret = -TARGET_EFAULT;
4951 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
4952 char real[PATH_MAX];
4953 temp = realpath(exec_path,real);
4954 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
4955 snprintf((char *)p2, arg3, "%s", real);
4958 ret = get_errno(readlink(path(p), p2, arg3));
4960 unlock_user(p2, arg2, ret);
4961 unlock_user(p, arg1, 0);
4964 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4965 case TARGET_NR_readlinkat:
4968 p = lock_user_string(arg2);
4969 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4971 ret = -TARGET_EFAULT;
4973 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
4974 unlock_user(p2, arg3, ret);
4975 unlock_user(p, arg2, 0);
4979 #ifdef TARGET_NR_uselib
4980 case TARGET_NR_uselib:
4983 #ifdef TARGET_NR_swapon
4984 case TARGET_NR_swapon:
4985 if (!(p = lock_user_string(arg1)))
4987 ret = get_errno(swapon(p, arg2));
4988 unlock_user(p, arg1, 0);
4991 case TARGET_NR_reboot:
4993 #ifdef TARGET_NR_readdir
4994 case TARGET_NR_readdir:
4997 #ifdef TARGET_NR_mmap
4998 case TARGET_NR_mmap:
4999 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
5002 abi_ulong v1, v2, v3, v4, v5, v6;
5003 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5011 unlock_user(v, arg1, 0);
5012 ret = get_errno(target_mmap(v1, v2, v3,
5013 target_to_host_bitmask(v4, mmap_flags_tbl),
5017 ret = get_errno(target_mmap(arg1, arg2, arg3,
5018 target_to_host_bitmask(arg4, mmap_flags_tbl),
5024 #ifdef TARGET_NR_mmap2
5025 case TARGET_NR_mmap2:
5027 #define MMAP_SHIFT 12
5029 ret = get_errno(target_mmap(arg1, arg2, arg3,
5030 target_to_host_bitmask(arg4, mmap_flags_tbl),
5032 arg6 << MMAP_SHIFT));
5035 case TARGET_NR_munmap:
5036 ret = get_errno(target_munmap(arg1, arg2));
5038 case TARGET_NR_mprotect:
5039 ret = get_errno(target_mprotect(arg1, arg2, arg3));
5041 #ifdef TARGET_NR_mremap
5042 case TARGET_NR_mremap:
5043 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
5046 /* ??? msync/mlock/munlock are broken for softmmu. */
5047 #ifdef TARGET_NR_msync
5048 case TARGET_NR_msync:
5049 ret = get_errno(msync(g2h(arg1), arg2, arg3));
5052 #ifdef TARGET_NR_mlock
5053 case TARGET_NR_mlock:
5054 ret = get_errno(mlock(g2h(arg1), arg2));
5057 #ifdef TARGET_NR_munlock
5058 case TARGET_NR_munlock:
5059 ret = get_errno(munlock(g2h(arg1), arg2));
5062 #ifdef TARGET_NR_mlockall
5063 case TARGET_NR_mlockall:
5064 ret = get_errno(mlockall(arg1));
5067 #ifdef TARGET_NR_munlockall
5068 case TARGET_NR_munlockall:
5069 ret = get_errno(munlockall());
5072 case TARGET_NR_truncate:
5073 if (!(p = lock_user_string(arg1)))
5075 ret = get_errno(truncate(p, arg2));
5076 unlock_user(p, arg1, 0);
5078 case TARGET_NR_ftruncate:
5079 ret = get_errno(ftruncate(arg1, arg2));
5081 case TARGET_NR_fchmod:
5082 ret = get_errno(fchmod(arg1, arg2));
5084 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5085 case TARGET_NR_fchmodat:
5086 if (!(p = lock_user_string(arg2)))
5088 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
5089 unlock_user(p, arg2, 0);
5092 case TARGET_NR_getpriority:
5093 /* libc does special remapping of the return value of
5094 * sys_getpriority() so it's just easiest to call
5095 * sys_getpriority() directly rather than through libc. */
5096 ret = sys_getpriority(arg1, arg2);
5098 case TARGET_NR_setpriority:
5099 ret = get_errno(setpriority(arg1, arg2, arg3));
5101 #ifdef TARGET_NR_profil
5102 case TARGET_NR_profil:
5105 case TARGET_NR_statfs:
5106 if (!(p = lock_user_string(arg1)))
5108 ret = get_errno(statfs(path(p), &stfs));
5109 unlock_user(p, arg1, 0);
5111 if (!is_error(ret)) {
5112 struct target_statfs *target_stfs;
5114 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5116 __put_user(stfs.f_type, &target_stfs->f_type);
5117 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5118 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5119 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5120 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5121 __put_user(stfs.f_files, &target_stfs->f_files);
5122 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5123 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5124 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5125 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5126 unlock_user_struct(target_stfs, arg2, 1);
5129 case TARGET_NR_fstatfs:
5130 ret = get_errno(fstatfs(arg1, &stfs));
5131 goto convert_statfs;
5132 #ifdef TARGET_NR_statfs64
5133 case TARGET_NR_statfs64:
5134 if (!(p = lock_user_string(arg1)))
5136 ret = get_errno(statfs(path(p), &stfs));
5137 unlock_user(p, arg1, 0);
5139 if (!is_error(ret)) {
5140 struct target_statfs64 *target_stfs;
5142 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5144 __put_user(stfs.f_type, &target_stfs->f_type);
5145 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5146 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5147 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5148 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5149 __put_user(stfs.f_files, &target_stfs->f_files);
5150 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5151 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5152 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5153 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5154 unlock_user_struct(target_stfs, arg3, 1);
5157 case TARGET_NR_fstatfs64:
5158 ret = get_errno(fstatfs(arg1, &stfs));
5159 goto convert_statfs64;
5161 #ifdef TARGET_NR_ioperm
5162 case TARGET_NR_ioperm:
5165 #ifdef TARGET_NR_socketcall
5166 case TARGET_NR_socketcall:
5167 ret = do_socketcall(arg1, arg2);
5170 #ifdef TARGET_NR_accept
5171 case TARGET_NR_accept:
5172 ret = do_accept(arg1, arg2, arg3);
5175 #ifdef TARGET_NR_bind
5176 case TARGET_NR_bind:
5177 ret = do_bind(arg1, arg2, arg3);
5180 #ifdef TARGET_NR_connect
5181 case TARGET_NR_connect:
5182 ret = do_connect(arg1, arg2, arg3);
5185 #ifdef TARGET_NR_getpeername
5186 case TARGET_NR_getpeername:
5187 ret = do_getpeername(arg1, arg2, arg3);
5190 #ifdef TARGET_NR_getsockname
5191 case TARGET_NR_getsockname:
5192 ret = do_getsockname(arg1, arg2, arg3);
5195 #ifdef TARGET_NR_getsockopt
5196 case TARGET_NR_getsockopt:
5197 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5200 #ifdef TARGET_NR_listen
5201 case TARGET_NR_listen:
5202 ret = get_errno(listen(arg1, arg2));
5205 #ifdef TARGET_NR_recv
5206 case TARGET_NR_recv:
5207 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
5210 #ifdef TARGET_NR_recvfrom
5211 case TARGET_NR_recvfrom:
5212 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
5215 #ifdef TARGET_NR_recvmsg
5216 case TARGET_NR_recvmsg:
5217 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5220 #ifdef TARGET_NR_send
5221 case TARGET_NR_send:
5222 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
5225 #ifdef TARGET_NR_sendmsg
5226 case TARGET_NR_sendmsg:
5227 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5230 #ifdef TARGET_NR_sendto
5231 case TARGET_NR_sendto:
5232 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
5235 #ifdef TARGET_NR_shutdown
5236 case TARGET_NR_shutdown:
5237 ret = get_errno(shutdown(arg1, arg2));
5240 #ifdef TARGET_NR_socket
5241 case TARGET_NR_socket:
5242 ret = do_socket(arg1, arg2, arg3);
5245 #ifdef TARGET_NR_socketpair
5246 case TARGET_NR_socketpair:
5247 ret = do_socketpair(arg1, arg2, arg3, arg4);
5250 #ifdef TARGET_NR_setsockopt
5251 case TARGET_NR_setsockopt:
5252 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5256 case TARGET_NR_syslog:
5257 if (!(p = lock_user_string(arg2)))
5259 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5260 unlock_user(p, arg2, 0);
5263 case TARGET_NR_setitimer:
5265 struct itimerval value, ovalue, *pvalue;
5269 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5270 || copy_from_user_timeval(&pvalue->it_value,
5271 arg2 + sizeof(struct target_timeval)))
5276 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
5277 if (!is_error(ret) && arg3) {
5278 if (copy_to_user_timeval(arg3,
5279 &ovalue.it_interval)
5280 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5286 case TARGET_NR_getitimer:
5288 struct itimerval value;
5290 ret = get_errno(getitimer(arg1, &value));
5291 if (!is_error(ret) && arg2) {
5292 if (copy_to_user_timeval(arg2,
5294 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5300 case TARGET_NR_stat:
5301 if (!(p = lock_user_string(arg1)))
5303 ret = get_errno(stat(path(p), &st));
5304 unlock_user(p, arg1, 0);
5306 case TARGET_NR_lstat:
5307 if (!(p = lock_user_string(arg1)))
5309 ret = get_errno(lstat(path(p), &st));
5310 unlock_user(p, arg1, 0);
5312 case TARGET_NR_fstat:
5314 ret = get_errno(fstat(arg1, &st));
5316 if (!is_error(ret)) {
5317 struct target_stat *target_st;
5319 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5321 __put_user(st.st_dev, &target_st->st_dev);
5322 __put_user(st.st_ino, &target_st->st_ino);
5323 __put_user(st.st_mode, &target_st->st_mode);
5324 __put_user(st.st_uid, &target_st->st_uid);
5325 __put_user(st.st_gid, &target_st->st_gid);
5326 __put_user(st.st_nlink, &target_st->st_nlink);
5327 __put_user(st.st_rdev, &target_st->st_rdev);
5328 __put_user(st.st_size, &target_st->st_size);
5329 __put_user(st.st_blksize, &target_st->st_blksize);
5330 __put_user(st.st_blocks, &target_st->st_blocks);
5331 __put_user(st.st_atime, &target_st->target_st_atime);
5332 __put_user(st.st_mtime, &target_st->target_st_mtime);
5333 __put_user(st.st_ctime, &target_st->target_st_ctime);
5334 unlock_user_struct(target_st, arg2, 1);
5338 #ifdef TARGET_NR_olduname
5339 case TARGET_NR_olduname:
5342 #ifdef TARGET_NR_iopl
5343 case TARGET_NR_iopl:
5346 case TARGET_NR_vhangup:
5347 ret = get_errno(vhangup());
5349 #ifdef TARGET_NR_idle
5350 case TARGET_NR_idle:
5353 #ifdef TARGET_NR_syscall
5354 case TARGET_NR_syscall:
5355 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5358 case TARGET_NR_wait4:
5361 abi_long status_ptr = arg2;
5362 struct rusage rusage, *rusage_ptr;
5363 abi_ulong target_rusage = arg4;
5365 rusage_ptr = &rusage;
5368 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5369 if (!is_error(ret)) {
5371 if (put_user_s32(status, status_ptr))
5375 host_to_target_rusage(target_rusage, &rusage);
5379 #ifdef TARGET_NR_swapoff
5380 case TARGET_NR_swapoff:
5381 if (!(p = lock_user_string(arg1)))
5383 ret = get_errno(swapoff(p));
5384 unlock_user(p, arg1, 0);
5387 case TARGET_NR_sysinfo:
5389 struct target_sysinfo *target_value;
5390 struct sysinfo value;
5391 ret = get_errno(sysinfo(&value));
5392 if (!is_error(ret) && arg1)
5394 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5396 __put_user(value.uptime, &target_value->uptime);
5397 __put_user(value.loads[0], &target_value->loads[0]);
5398 __put_user(value.loads[1], &target_value->loads[1]);
5399 __put_user(value.loads[2], &target_value->loads[2]);
5400 __put_user(value.totalram, &target_value->totalram);
5401 __put_user(value.freeram, &target_value->freeram);
5402 __put_user(value.sharedram, &target_value->sharedram);
5403 __put_user(value.bufferram, &target_value->bufferram);
5404 __put_user(value.totalswap, &target_value->totalswap);
5405 __put_user(value.freeswap, &target_value->freeswap);
5406 __put_user(value.procs, &target_value->procs);
5407 __put_user(value.totalhigh, &target_value->totalhigh);
5408 __put_user(value.freehigh, &target_value->freehigh);
5409 __put_user(value.mem_unit, &target_value->mem_unit);
5410 unlock_user_struct(target_value, arg1, 1);
5414 #ifdef TARGET_NR_ipc
5416 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5419 #ifdef TARGET_NR_semget
5420 case TARGET_NR_semget:
5421 ret = get_errno(semget(arg1, arg2, arg3));
5424 #ifdef TARGET_NR_semop
5425 case TARGET_NR_semop:
5426 ret = get_errno(do_semop(arg1, arg2, arg3));
5429 #ifdef TARGET_NR_semctl
5430 case TARGET_NR_semctl:
5431 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5434 #ifdef TARGET_NR_msgctl
5435 case TARGET_NR_msgctl:
5436 ret = do_msgctl(arg1, arg2, arg3);
5439 #ifdef TARGET_NR_msgget
5440 case TARGET_NR_msgget:
5441 ret = get_errno(msgget(arg1, arg2));
5444 #ifdef TARGET_NR_msgrcv
5445 case TARGET_NR_msgrcv:
5446 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5449 #ifdef TARGET_NR_msgsnd
5450 case TARGET_NR_msgsnd:
5451 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5454 #ifdef TARGET_NR_shmget
5455 case TARGET_NR_shmget:
5456 ret = get_errno(shmget(arg1, arg2, arg3));
5459 #ifdef TARGET_NR_shmctl
5460 case TARGET_NR_shmctl:
5461 ret = do_shmctl(arg1, arg2, arg3);
5464 #ifdef TARGET_NR_shmat
5465 case TARGET_NR_shmat:
5470 err = do_shmat(arg1, arg2, arg3, &_ret);
5471 ret = err ? err : _ret;
5475 #ifdef TARGET_NR_shmdt
5476 case TARGET_NR_shmdt:
5477 ret = do_shmdt(arg1);
5480 case TARGET_NR_fsync:
5481 ret = get_errno(fsync(arg1));
5483 case TARGET_NR_clone:
5484 #if defined(TARGET_SH4)
5485 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
5486 #elif defined(TARGET_CRIS)
5487 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
5489 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
5492 #ifdef __NR_exit_group
5493 /* new thread calls */
5494 case TARGET_NR_exit_group:
5498 gdb_exit(cpu_env, arg1);
5499 ret = get_errno(exit_group(arg1));
5502 case TARGET_NR_setdomainname:
5503 if (!(p = lock_user_string(arg1)))
5505 ret = get_errno(setdomainname(p, arg2));
5506 unlock_user(p, arg1, 0);
5508 case TARGET_NR_uname:
5509 /* no need to transcode because we use the linux syscall */
5511 struct new_utsname * buf;
5513 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5515 ret = get_errno(sys_uname(buf));
5516 if (!is_error(ret)) {
5517 /* Overrite the native machine name with whatever is being
5519 strcpy (buf->machine, UNAME_MACHINE);
5520 /* Allow the user to override the reported release. */
5521 if (qemu_uname_release && *qemu_uname_release)
5522 strcpy (buf->release, qemu_uname_release);
5524 unlock_user_struct(buf, arg1, 1);
5528 case TARGET_NR_modify_ldt:
5529 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5531 #if !defined(TARGET_X86_64)
5532 case TARGET_NR_vm86old:
5534 case TARGET_NR_vm86:
5535 ret = do_vm86(cpu_env, arg1, arg2);
5539 case TARGET_NR_adjtimex:
5541 #ifdef TARGET_NR_create_module
5542 case TARGET_NR_create_module:
5544 case TARGET_NR_init_module:
5545 case TARGET_NR_delete_module:
5546 #ifdef TARGET_NR_get_kernel_syms
5547 case TARGET_NR_get_kernel_syms:
5550 case TARGET_NR_quotactl:
5552 case TARGET_NR_getpgid:
5553 ret = get_errno(getpgid(arg1));
5555 case TARGET_NR_fchdir:
5556 ret = get_errno(fchdir(arg1));
5558 #ifdef TARGET_NR_bdflush /* not on x86_64 */
5559 case TARGET_NR_bdflush:
5562 #ifdef TARGET_NR_sysfs
5563 case TARGET_NR_sysfs:
5566 case TARGET_NR_personality:
5567 ret = get_errno(personality(arg1));
5569 #ifdef TARGET_NR_afs_syscall
5570 case TARGET_NR_afs_syscall:
5573 #ifdef TARGET_NR__llseek /* Not on alpha */
5574 case TARGET_NR__llseek:
5576 #if defined (__x86_64__)
5577 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
5578 if (put_user_s64(ret, arg4))
5582 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
5583 if (put_user_s64(res, arg4))
5589 case TARGET_NR_getdents:
5590 #if TARGET_ABI_BITS != 32
5592 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
5594 struct target_dirent *target_dirp;
5595 struct linux_dirent *dirp;
5596 abi_long count = arg3;
5598 dirp = malloc(count);
5600 ret = -TARGET_ENOMEM;
5604 ret = get_errno(sys_getdents(arg1, dirp, count));
5605 if (!is_error(ret)) {
5606 struct linux_dirent *de;
5607 struct target_dirent *tde;
5609 int reclen, treclen;
5610 int count1, tnamelen;
5614 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5618 reclen = de->d_reclen;
5619 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
5620 tde->d_reclen = tswap16(treclen);
5621 tde->d_ino = tswapl(de->d_ino);
5622 tde->d_off = tswapl(de->d_off);
5623 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
5626 /* XXX: may not be correct */
5627 pstrcpy(tde->d_name, tnamelen, de->d_name);
5628 de = (struct linux_dirent *)((char *)de + reclen);
5630 tde = (struct target_dirent *)((char *)tde + treclen);
5634 unlock_user(target_dirp, arg2, ret);
5640 struct linux_dirent *dirp;
5641 abi_long count = arg3;
5643 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5645 ret = get_errno(sys_getdents(arg1, dirp, count));
5646 if (!is_error(ret)) {
5647 struct linux_dirent *de;
5652 reclen = de->d_reclen;
5655 de->d_reclen = tswap16(reclen);
5656 tswapls(&de->d_ino);
5657 tswapls(&de->d_off);
5658 de = (struct linux_dirent *)((char *)de + reclen);
5662 unlock_user(dirp, arg2, ret);
5666 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
5667 case TARGET_NR_getdents64:
5669 struct linux_dirent64 *dirp;
5670 abi_long count = arg3;
5671 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5673 ret = get_errno(sys_getdents64(arg1, dirp, count));
5674 if (!is_error(ret)) {
5675 struct linux_dirent64 *de;
5680 reclen = de->d_reclen;
5683 de->d_reclen = tswap16(reclen);
5684 tswap64s((uint64_t *)&de->d_ino);
5685 tswap64s((uint64_t *)&de->d_off);
5686 de = (struct linux_dirent64 *)((char *)de + reclen);
5690 unlock_user(dirp, arg2, ret);
5693 #endif /* TARGET_NR_getdents64 */
5694 #ifdef TARGET_NR__newselect
5695 case TARGET_NR__newselect:
5696 ret = do_select(arg1, arg2, arg3, arg4, arg5);
5699 #ifdef TARGET_NR_poll
5700 case TARGET_NR_poll:
5702 struct target_pollfd *target_pfd;
5703 unsigned int nfds = arg2;
5708 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
5711 pfd = alloca(sizeof(struct pollfd) * nfds);
5712 for(i = 0; i < nfds; i++) {
5713 pfd[i].fd = tswap32(target_pfd[i].fd);
5714 pfd[i].events = tswap16(target_pfd[i].events);
5716 ret = get_errno(poll(pfd, nfds, timeout));
5717 if (!is_error(ret)) {
5718 for(i = 0; i < nfds; i++) {
5719 target_pfd[i].revents = tswap16(pfd[i].revents);
5721 ret += nfds * (sizeof(struct target_pollfd)
5722 - sizeof(struct pollfd));
5724 unlock_user(target_pfd, arg1, ret);
5728 case TARGET_NR_flock:
5729 /* NOTE: the flock constant seems to be the same for every
5731 ret = get_errno(flock(arg1, arg2));
5733 case TARGET_NR_readv:
5738 vec = alloca(count * sizeof(struct iovec));
5739 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
5741 ret = get_errno(readv(arg1, vec, count));
5742 unlock_iovec(vec, arg2, count, 1);
5745 case TARGET_NR_writev:
5750 vec = alloca(count * sizeof(struct iovec));
5751 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
5753 ret = get_errno(writev(arg1, vec, count));
5754 unlock_iovec(vec, arg2, count, 0);
5757 case TARGET_NR_getsid:
5758 ret = get_errno(getsid(arg1));
5760 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
5761 case TARGET_NR_fdatasync:
5762 ret = get_errno(fdatasync(arg1));
5765 case TARGET_NR__sysctl:
5766 /* We don't implement this, but ENOTDIR is always a safe
5768 ret = -TARGET_ENOTDIR;
5770 case TARGET_NR_sched_setparam:
5772 struct sched_param *target_schp;
5773 struct sched_param schp;
5775 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
5777 schp.sched_priority = tswap32(target_schp->sched_priority);
5778 unlock_user_struct(target_schp, arg2, 0);
5779 ret = get_errno(sched_setparam(arg1, &schp));
5782 case TARGET_NR_sched_getparam:
5784 struct sched_param *target_schp;
5785 struct sched_param schp;
5786 ret = get_errno(sched_getparam(arg1, &schp));
5787 if (!is_error(ret)) {
5788 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
5790 target_schp->sched_priority = tswap32(schp.sched_priority);
5791 unlock_user_struct(target_schp, arg2, 1);
5795 case TARGET_NR_sched_setscheduler:
5797 struct sched_param *target_schp;
5798 struct sched_param schp;
5799 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
5801 schp.sched_priority = tswap32(target_schp->sched_priority);
5802 unlock_user_struct(target_schp, arg3, 0);
5803 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
5806 case TARGET_NR_sched_getscheduler:
5807 ret = get_errno(sched_getscheduler(arg1));
5809 case TARGET_NR_sched_yield:
5810 ret = get_errno(sched_yield());
5812 case TARGET_NR_sched_get_priority_max:
5813 ret = get_errno(sched_get_priority_max(arg1));
5815 case TARGET_NR_sched_get_priority_min:
5816 ret = get_errno(sched_get_priority_min(arg1));
5818 case TARGET_NR_sched_rr_get_interval:
5821 ret = get_errno(sched_rr_get_interval(arg1, &ts));
5822 if (!is_error(ret)) {
5823 host_to_target_timespec(arg2, &ts);
5827 case TARGET_NR_nanosleep:
5829 struct timespec req, rem;
5830 target_to_host_timespec(&req, arg1);
5831 ret = get_errno(nanosleep(&req, &rem));
5832 if (is_error(ret) && arg2) {
5833 host_to_target_timespec(arg2, &rem);
5837 #ifdef TARGET_NR_query_module
5838 case TARGET_NR_query_module:
5841 #ifdef TARGET_NR_nfsservctl
5842 case TARGET_NR_nfsservctl:
5845 case TARGET_NR_prctl:
5848 case PR_GET_PDEATHSIG:
5851 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
5852 if (!is_error(ret) && arg2
5853 && put_user_ual(deathsig, arg2))
5858 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
5862 #ifdef TARGET_NR_arch_prctl
5863 case TARGET_NR_arch_prctl:
5864 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
5865 ret = do_arch_prctl(cpu_env, arg1, arg2);
5871 #ifdef TARGET_NR_pread
5872 case TARGET_NR_pread:
5874 if (((CPUARMState *)cpu_env)->eabi)
5877 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5879 ret = get_errno(pread(arg1, p, arg3, arg4));
5880 unlock_user(p, arg2, ret);
5882 case TARGET_NR_pwrite:
5884 if (((CPUARMState *)cpu_env)->eabi)
5887 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5889 ret = get_errno(pwrite(arg1, p, arg3, arg4));
5890 unlock_user(p, arg2, 0);
5893 #ifdef TARGET_NR_pread64
5894 case TARGET_NR_pread64:
5895 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5897 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
5898 unlock_user(p, arg2, ret);
5900 case TARGET_NR_pwrite64:
5901 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5903 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
5904 unlock_user(p, arg2, 0);
5907 case TARGET_NR_getcwd:
5908 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
5910 ret = get_errno(sys_getcwd1(p, arg2));
5911 unlock_user(p, arg1, ret);
5913 case TARGET_NR_capget:
5915 case TARGET_NR_capset:
5917 case TARGET_NR_sigaltstack:
5918 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
5919 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
5920 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
5925 case TARGET_NR_sendfile:
5927 #ifdef TARGET_NR_getpmsg
5928 case TARGET_NR_getpmsg:
5931 #ifdef TARGET_NR_putpmsg
5932 case TARGET_NR_putpmsg:
5935 #ifdef TARGET_NR_vfork
5936 case TARGET_NR_vfork:
5937 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
5941 #ifdef TARGET_NR_ugetrlimit
5942 case TARGET_NR_ugetrlimit:
5945 ret = get_errno(getrlimit(arg1, &rlim));
5946 if (!is_error(ret)) {
5947 struct target_rlimit *target_rlim;
5948 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5950 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
5951 target_rlim->rlim_max = tswapl(rlim.rlim_max);
5952 unlock_user_struct(target_rlim, arg2, 1);
5957 #ifdef TARGET_NR_truncate64
5958 case TARGET_NR_truncate64:
5959 if (!(p = lock_user_string(arg1)))
5961 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
5962 unlock_user(p, arg1, 0);
5965 #ifdef TARGET_NR_ftruncate64
5966 case TARGET_NR_ftruncate64:
5967 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
5970 #ifdef TARGET_NR_stat64
5971 case TARGET_NR_stat64:
5972 if (!(p = lock_user_string(arg1)))
5974 ret = get_errno(stat(path(p), &st));
5975 unlock_user(p, arg1, 0);
5977 ret = host_to_target_stat64(cpu_env, arg2, &st);
5980 #ifdef TARGET_NR_lstat64
5981 case TARGET_NR_lstat64:
5982 if (!(p = lock_user_string(arg1)))
5984 ret = get_errno(lstat(path(p), &st));
5985 unlock_user(p, arg1, 0);
5987 ret = host_to_target_stat64(cpu_env, arg2, &st);
5990 #ifdef TARGET_NR_fstat64
5991 case TARGET_NR_fstat64:
5992 ret = get_errno(fstat(arg1, &st));
5994 ret = host_to_target_stat64(cpu_env, arg2, &st);
5997 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
5998 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
5999 #ifdef TARGET_NR_fstatat64
6000 case TARGET_NR_fstatat64:
6002 #ifdef TARGET_NR_newfstatat
6003 case TARGET_NR_newfstatat:
6005 if (!(p = lock_user_string(arg2)))
6007 #ifdef __NR_fstatat64
6008 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
6010 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6013 ret = host_to_target_stat64(cpu_env, arg3, &st);
6017 case TARGET_NR_lchown:
6018 if (!(p = lock_user_string(arg1)))
6020 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6021 unlock_user(p, arg1, 0);
6023 case TARGET_NR_getuid:
6024 ret = get_errno(high2lowuid(getuid()));
6026 case TARGET_NR_getgid:
6027 ret = get_errno(high2lowgid(getgid()));
6029 case TARGET_NR_geteuid:
6030 ret = get_errno(high2lowuid(geteuid()));
6032 case TARGET_NR_getegid:
6033 ret = get_errno(high2lowgid(getegid()));
6035 case TARGET_NR_setreuid:
6036 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6038 case TARGET_NR_setregid:
6039 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6041 case TARGET_NR_getgroups:
6043 int gidsetsize = arg1;
6044 uint16_t *target_grouplist;
6048 grouplist = alloca(gidsetsize * sizeof(gid_t));
6049 ret = get_errno(getgroups(gidsetsize, grouplist));
6050 if (gidsetsize == 0)
6052 if (!is_error(ret)) {
6053 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6054 if (!target_grouplist)
6056 for(i = 0;i < ret; i++)
6057 target_grouplist[i] = tswap16(grouplist[i]);
6058 unlock_user(target_grouplist, arg2, gidsetsize * 2);
6062 case TARGET_NR_setgroups:
6064 int gidsetsize = arg1;
6065 uint16_t *target_grouplist;
6069 grouplist = alloca(gidsetsize * sizeof(gid_t));
6070 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6071 if (!target_grouplist) {
6072 ret = -TARGET_EFAULT;
6075 for(i = 0;i < gidsetsize; i++)
6076 grouplist[i] = tswap16(target_grouplist[i]);
6077 unlock_user(target_grouplist, arg2, 0);
6078 ret = get_errno(setgroups(gidsetsize, grouplist));
6081 case TARGET_NR_fchown:
6082 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6084 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6085 case TARGET_NR_fchownat:
6086 if (!(p = lock_user_string(arg2)))
6088 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6089 unlock_user(p, arg2, 0);
6092 #ifdef TARGET_NR_setresuid
6093 case TARGET_NR_setresuid:
6094 ret = get_errno(setresuid(low2highuid(arg1),
6096 low2highuid(arg3)));
6099 #ifdef TARGET_NR_getresuid
6100 case TARGET_NR_getresuid:
6102 uid_t ruid, euid, suid;
6103 ret = get_errno(getresuid(&ruid, &euid, &suid));
6104 if (!is_error(ret)) {
6105 if (put_user_u16(high2lowuid(ruid), arg1)
6106 || put_user_u16(high2lowuid(euid), arg2)
6107 || put_user_u16(high2lowuid(suid), arg3))
6113 #ifdef TARGET_NR_getresgid
6114 case TARGET_NR_setresgid:
6115 ret = get_errno(setresgid(low2highgid(arg1),
6117 low2highgid(arg3)));
6120 #ifdef TARGET_NR_getresgid
6121 case TARGET_NR_getresgid:
6123 gid_t rgid, egid, sgid;
6124 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6125 if (!is_error(ret)) {
6126 if (put_user_u16(high2lowgid(rgid), arg1)
6127 || put_user_u16(high2lowgid(egid), arg2)
6128 || put_user_u16(high2lowgid(sgid), arg3))
6134 case TARGET_NR_chown:
6135 if (!(p = lock_user_string(arg1)))
6137 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6138 unlock_user(p, arg1, 0);
6140 case TARGET_NR_setuid:
6141 ret = get_errno(setuid(low2highuid(arg1)));
6143 case TARGET_NR_setgid:
6144 ret = get_errno(setgid(low2highgid(arg1)));
6146 case TARGET_NR_setfsuid:
6147 ret = get_errno(setfsuid(arg1));
6149 case TARGET_NR_setfsgid:
6150 ret = get_errno(setfsgid(arg1));
6152 #endif /* USE_UID16 */
6154 #ifdef TARGET_NR_lchown32
6155 case TARGET_NR_lchown32:
6156 if (!(p = lock_user_string(arg1)))
6158 ret = get_errno(lchown(p, arg2, arg3));
6159 unlock_user(p, arg1, 0);
6162 #ifdef TARGET_NR_getuid32
6163 case TARGET_NR_getuid32:
6164 ret = get_errno(getuid());
6168 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6169 /* Alpha specific */
6170 case TARGET_NR_getxuid:
6174 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6176 ret = get_errno(getuid());
6179 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6180 /* Alpha specific */
6181 case TARGET_NR_getxgid:
6185 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6187 ret = get_errno(getgid());
6191 #ifdef TARGET_NR_getgid32
6192 case TARGET_NR_getgid32:
6193 ret = get_errno(getgid());
6196 #ifdef TARGET_NR_geteuid32
6197 case TARGET_NR_geteuid32:
6198 ret = get_errno(geteuid());
6201 #ifdef TARGET_NR_getegid32
6202 case TARGET_NR_getegid32:
6203 ret = get_errno(getegid());
6206 #ifdef TARGET_NR_setreuid32
6207 case TARGET_NR_setreuid32:
6208 ret = get_errno(setreuid(arg1, arg2));
6211 #ifdef TARGET_NR_setregid32
6212 case TARGET_NR_setregid32:
6213 ret = get_errno(setregid(arg1, arg2));
6216 #ifdef TARGET_NR_getgroups32
6217 case TARGET_NR_getgroups32:
6219 int gidsetsize = arg1;
6220 uint32_t *target_grouplist;
6224 grouplist = alloca(gidsetsize * sizeof(gid_t));
6225 ret = get_errno(getgroups(gidsetsize, grouplist));
6226 if (gidsetsize == 0)
6228 if (!is_error(ret)) {
6229 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6230 if (!target_grouplist) {
6231 ret = -TARGET_EFAULT;
6234 for(i = 0;i < ret; i++)
6235 target_grouplist[i] = tswap32(grouplist[i]);
6236 unlock_user(target_grouplist, arg2, gidsetsize * 4);
6241 #ifdef TARGET_NR_setgroups32
6242 case TARGET_NR_setgroups32:
6244 int gidsetsize = arg1;
6245 uint32_t *target_grouplist;
6249 grouplist = alloca(gidsetsize * sizeof(gid_t));
6250 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6251 if (!target_grouplist) {
6252 ret = -TARGET_EFAULT;
6255 for(i = 0;i < gidsetsize; i++)
6256 grouplist[i] = tswap32(target_grouplist[i]);
6257 unlock_user(target_grouplist, arg2, 0);
6258 ret = get_errno(setgroups(gidsetsize, grouplist));
6262 #ifdef TARGET_NR_fchown32
6263 case TARGET_NR_fchown32:
6264 ret = get_errno(fchown(arg1, arg2, arg3));
6267 #ifdef TARGET_NR_setresuid32
6268 case TARGET_NR_setresuid32:
6269 ret = get_errno(setresuid(arg1, arg2, arg3));
6272 #ifdef TARGET_NR_getresuid32
6273 case TARGET_NR_getresuid32:
6275 uid_t ruid, euid, suid;
6276 ret = get_errno(getresuid(&ruid, &euid, &suid));
6277 if (!is_error(ret)) {
6278 if (put_user_u32(ruid, arg1)
6279 || put_user_u32(euid, arg2)
6280 || put_user_u32(suid, arg3))
6286 #ifdef TARGET_NR_setresgid32
6287 case TARGET_NR_setresgid32:
6288 ret = get_errno(setresgid(arg1, arg2, arg3));
6291 #ifdef TARGET_NR_getresgid32
6292 case TARGET_NR_getresgid32:
6294 gid_t rgid, egid, sgid;
6295 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6296 if (!is_error(ret)) {
6297 if (put_user_u32(rgid, arg1)
6298 || put_user_u32(egid, arg2)
6299 || put_user_u32(sgid, arg3))
6305 #ifdef TARGET_NR_chown32
6306 case TARGET_NR_chown32:
6307 if (!(p = lock_user_string(arg1)))
6309 ret = get_errno(chown(p, arg2, arg3));
6310 unlock_user(p, arg1, 0);
6313 #ifdef TARGET_NR_setuid32
6314 case TARGET_NR_setuid32:
6315 ret = get_errno(setuid(arg1));
6318 #ifdef TARGET_NR_setgid32
6319 case TARGET_NR_setgid32:
6320 ret = get_errno(setgid(arg1));
6323 #ifdef TARGET_NR_setfsuid32
6324 case TARGET_NR_setfsuid32:
6325 ret = get_errno(setfsuid(arg1));
6328 #ifdef TARGET_NR_setfsgid32
6329 case TARGET_NR_setfsgid32:
6330 ret = get_errno(setfsgid(arg1));
6334 case TARGET_NR_pivot_root:
6336 #ifdef TARGET_NR_mincore
6337 case TARGET_NR_mincore:
6340 ret = -TARGET_EFAULT;
6341 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6343 if (!(p = lock_user_string(arg3)))
6345 ret = get_errno(mincore(a, arg2, p));
6346 unlock_user(p, arg3, ret);
6348 unlock_user(a, arg1, 0);
6352 #ifdef TARGET_NR_arm_fadvise64_64
6353 case TARGET_NR_arm_fadvise64_64:
6356 * arm_fadvise64_64 looks like fadvise64_64 but
6357 * with different argument order
6365 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64)
6366 #ifdef TARGET_NR_fadvise64_64
6367 case TARGET_NR_fadvise64_64:
6369 /* This is a hint, so ignoring and returning success is ok. */
6373 #ifdef TARGET_NR_madvise
6374 case TARGET_NR_madvise:
6375 /* A straight passthrough may not be safe because qemu sometimes
6376 turns private flie-backed mappings into anonymous mappings.
6377 This will break MADV_DONTNEED.
6378 This is a hint, so ignoring and returning success is ok. */
6382 #if TARGET_ABI_BITS == 32
6383 case TARGET_NR_fcntl64:
6387 struct target_flock64 *target_fl;
6389 struct target_eabi_flock64 *target_efl;
6393 case TARGET_F_GETLK64:
6396 case TARGET_F_SETLK64:
6399 case TARGET_F_SETLKW64:
6408 case TARGET_F_GETLK64:
6410 if (((CPUARMState *)cpu_env)->eabi) {
6411 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6413 fl.l_type = tswap16(target_efl->l_type);
6414 fl.l_whence = tswap16(target_efl->l_whence);
6415 fl.l_start = tswap64(target_efl->l_start);
6416 fl.l_len = tswap64(target_efl->l_len);
6417 fl.l_pid = tswapl(target_efl->l_pid);
6418 unlock_user_struct(target_efl, arg3, 0);
6422 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6424 fl.l_type = tswap16(target_fl->l_type);
6425 fl.l_whence = tswap16(target_fl->l_whence);
6426 fl.l_start = tswap64(target_fl->l_start);
6427 fl.l_len = tswap64(target_fl->l_len);
6428 fl.l_pid = tswapl(target_fl->l_pid);
6429 unlock_user_struct(target_fl, arg3, 0);
6431 ret = get_errno(fcntl(arg1, cmd, &fl));
6434 if (((CPUARMState *)cpu_env)->eabi) {
6435 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6437 target_efl->l_type = tswap16(fl.l_type);
6438 target_efl->l_whence = tswap16(fl.l_whence);
6439 target_efl->l_start = tswap64(fl.l_start);
6440 target_efl->l_len = tswap64(fl.l_len);
6441 target_efl->l_pid = tswapl(fl.l_pid);
6442 unlock_user_struct(target_efl, arg3, 1);
6446 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6448 target_fl->l_type = tswap16(fl.l_type);
6449 target_fl->l_whence = tswap16(fl.l_whence);
6450 target_fl->l_start = tswap64(fl.l_start);
6451 target_fl->l_len = tswap64(fl.l_len);
6452 target_fl->l_pid = tswapl(fl.l_pid);
6453 unlock_user_struct(target_fl, arg3, 1);
6458 case TARGET_F_SETLK64:
6459 case TARGET_F_SETLKW64:
6461 if (((CPUARMState *)cpu_env)->eabi) {
6462 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6464 fl.l_type = tswap16(target_efl->l_type);
6465 fl.l_whence = tswap16(target_efl->l_whence);
6466 fl.l_start = tswap64(target_efl->l_start);
6467 fl.l_len = tswap64(target_efl->l_len);
6468 fl.l_pid = tswapl(target_efl->l_pid);
6469 unlock_user_struct(target_efl, arg3, 0);
6473 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6475 fl.l_type = tswap16(target_fl->l_type);
6476 fl.l_whence = tswap16(target_fl->l_whence);
6477 fl.l_start = tswap64(target_fl->l_start);
6478 fl.l_len = tswap64(target_fl->l_len);
6479 fl.l_pid = tswapl(target_fl->l_pid);
6480 unlock_user_struct(target_fl, arg3, 0);
6482 ret = get_errno(fcntl(arg1, cmd, &fl));
6485 ret = do_fcntl(arg1, cmd, arg3);
6491 #ifdef TARGET_NR_cacheflush
6492 case TARGET_NR_cacheflush:
6493 /* self-modifying code is handled automatically, so nothing needed */
6497 #ifdef TARGET_NR_security
6498 case TARGET_NR_security:
6501 #ifdef TARGET_NR_getpagesize
6502 case TARGET_NR_getpagesize:
6503 ret = TARGET_PAGE_SIZE;
6506 case TARGET_NR_gettid:
6507 ret = get_errno(gettid());
6509 #ifdef TARGET_NR_readahead
6510 case TARGET_NR_readahead:
6511 #if TARGET_ABI_BITS == 32
6513 if (((CPUARMState *)cpu_env)->eabi)
6520 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
6522 ret = get_errno(readahead(arg1, arg2, arg3));
6526 #ifdef TARGET_NR_setxattr
6527 case TARGET_NR_setxattr:
6528 case TARGET_NR_lsetxattr:
6529 case TARGET_NR_fsetxattr:
6530 case TARGET_NR_getxattr:
6531 case TARGET_NR_lgetxattr:
6532 case TARGET_NR_fgetxattr:
6533 case TARGET_NR_listxattr:
6534 case TARGET_NR_llistxattr:
6535 case TARGET_NR_flistxattr:
6536 case TARGET_NR_removexattr:
6537 case TARGET_NR_lremovexattr:
6538 case TARGET_NR_fremovexattr:
6539 goto unimplemented_nowarn;
6541 #ifdef TARGET_NR_set_thread_area
6542 case TARGET_NR_set_thread_area:
6543 #if defined(TARGET_MIPS)
6544 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
6547 #elif defined(TARGET_CRIS)
6549 ret = -TARGET_EINVAL;
6551 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
6555 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
6556 ret = do_set_thread_area(cpu_env, arg1);
6559 goto unimplemented_nowarn;
6562 #ifdef TARGET_NR_get_thread_area
6563 case TARGET_NR_get_thread_area:
6564 #if defined(TARGET_I386) && defined(TARGET_ABI32)
6565 ret = do_get_thread_area(cpu_env, arg1);
6567 goto unimplemented_nowarn;
6570 #ifdef TARGET_NR_getdomainname
6571 case TARGET_NR_getdomainname:
6572 goto unimplemented_nowarn;
6575 #ifdef TARGET_NR_clock_gettime
6576 case TARGET_NR_clock_gettime:
6579 ret = get_errno(clock_gettime(arg1, &ts));
6580 if (!is_error(ret)) {
6581 host_to_target_timespec(arg2, &ts);
6586 #ifdef TARGET_NR_clock_getres
6587 case TARGET_NR_clock_getres:
6590 ret = get_errno(clock_getres(arg1, &ts));
6591 if (!is_error(ret)) {
6592 host_to_target_timespec(arg2, &ts);
6597 #ifdef TARGET_NR_clock_nanosleep
6598 case TARGET_NR_clock_nanosleep:
6601 target_to_host_timespec(&ts, arg3);
6602 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
6604 host_to_target_timespec(arg4, &ts);
6609 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
6610 case TARGET_NR_set_tid_address:
6611 ret = get_errno(set_tid_address((int *)g2h(arg1)));
6615 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
6616 case TARGET_NR_tkill:
6617 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
6621 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
6622 case TARGET_NR_tgkill:
6623 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
6624 target_to_host_signal(arg3)));
6628 #ifdef TARGET_NR_set_robust_list
6629 case TARGET_NR_set_robust_list:
6630 goto unimplemented_nowarn;
6633 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
6634 case TARGET_NR_utimensat:
6636 struct timespec ts[2];
6637 target_to_host_timespec(ts, arg3);
6638 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
6640 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
6642 if (!(p = lock_user_string(arg2))) {
6643 ret = -TARGET_EFAULT;
6646 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
6647 unlock_user(p, arg2, 0);
6652 #if defined(USE_NPTL)
6653 case TARGET_NR_futex:
6654 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
6657 #ifdef TARGET_NR_inotify_init
6658 case TARGET_NR_inotify_init:
6659 ret = get_errno(sys_inotify_init());
6662 #ifdef TARGET_NR_inotify_add_watch
6663 case TARGET_NR_inotify_add_watch:
6664 p = lock_user_string(arg2);
6665 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
6666 unlock_user(p, arg2, 0);
6669 #ifdef TARGET_NR_inotify_rm_watch
6670 case TARGET_NR_inotify_rm_watch:
6671 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
6675 #ifdef TARGET_NR_mq_open
6676 case TARGET_NR_mq_open:
6678 struct mq_attr posix_mq_attr;
6680 p = lock_user_string(arg1 - 1);
6682 copy_from_user_mq_attr (&posix_mq_attr, arg4);
6683 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
6684 unlock_user (p, arg1, 0);
6688 case TARGET_NR_mq_unlink:
6689 p = lock_user_string(arg1 - 1);
6690 ret = get_errno(mq_unlink(p));
6691 unlock_user (p, arg1, 0);
6694 case TARGET_NR_mq_timedsend:
6698 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6700 target_to_host_timespec(&ts, arg5);
6701 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
6702 host_to_target_timespec(arg5, &ts);
6705 ret = get_errno(mq_send(arg1, p, arg3, arg4));
6706 unlock_user (p, arg2, arg3);
6710 case TARGET_NR_mq_timedreceive:
6715 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6717 target_to_host_timespec(&ts, arg5);
6718 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
6719 host_to_target_timespec(arg5, &ts);
6722 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
6723 unlock_user (p, arg2, arg3);
6725 put_user_u32(prio, arg4);
6729 /* Not implemented for now... */
6730 /* case TARGET_NR_mq_notify: */
6733 case TARGET_NR_mq_getsetattr:
6735 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
6738 ret = mq_getattr(arg1, &posix_mq_attr_out);
6739 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
6742 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
6743 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
6752 gemu_log("qemu: Unsupported syscall: %d\n", num);
6753 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
6754 unimplemented_nowarn:
6756 ret = -TARGET_ENOSYS;
6761 gemu_log(" = %ld\n", ret);
6764 print_syscall_ret(num, ret);
6767 ret = -TARGET_EFAULT;