4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
32 #include <sys/types.h>
38 #include <sys/mount.h>
40 #include <sys/fsuid.h>
41 #include <sys/personality.h>
42 #include <sys/prctl.h>
43 #include <sys/resource.h>
49 int __clone2(int (*fn)(void *), void *child_stack_base,
50 size_t stack_size, int flags, void *arg, ...);
52 #include <sys/socket.h>
56 #include <sys/times.h>
59 #include <sys/statfs.h>
61 #include <sys/sysinfo.h>
62 #include <sys/utsname.h>
63 //#include <sys/user.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
66 #include <linux/wireless.h>
67 #include <linux/icmp.h>
68 #include "qemu-common.h"
73 #include <sys/eventfd.h>
76 #include <sys/epoll.h>
79 #include "qemu/xattr.h"
81 #ifdef CONFIG_SENDFILE
82 #include <sys/sendfile.h>
85 #define termios host_termios
86 #define winsize host_winsize
87 #define termio host_termio
88 #define sgttyb host_sgttyb /* same as target */
89 #define tchars host_tchars /* same as target */
90 #define ltchars host_ltchars /* same as target */
92 #include <linux/termios.h>
93 #include <linux/unistd.h>
94 #include <linux/utsname.h>
95 #include <linux/cdrom.h>
96 #include <linux/hdreg.h>
97 #include <linux/soundcard.h>
99 #include <linux/mtio.h>
100 #include <linux/fs.h>
101 #if defined(CONFIG_FIEMAP)
102 #include <linux/fiemap.h>
104 #include <linux/fb.h>
105 #include <linux/vt.h>
106 #include <linux/dm-ioctl.h>
107 #include <linux/reboot.h>
108 #include <linux/route.h>
109 #include <linux/filter.h>
110 #include "linux_loop.h"
111 #include "cpu-uname.h"
115 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
116 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
120 //#include <linux/msdos_fs.h>
121 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
122 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
133 #define _syscall0(type,name) \
134 static type name (void) \
136 return syscall(__NR_##name); \
139 #define _syscall1(type,name,type1,arg1) \
140 static type name (type1 arg1) \
142 return syscall(__NR_##name, arg1); \
145 #define _syscall2(type,name,type1,arg1,type2,arg2) \
146 static type name (type1 arg1,type2 arg2) \
148 return syscall(__NR_##name, arg1, arg2); \
151 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
152 static type name (type1 arg1,type2 arg2,type3 arg3) \
154 return syscall(__NR_##name, arg1, arg2, arg3); \
157 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
158 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
160 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
163 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
165 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
167 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
171 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
172 type5,arg5,type6,arg6) \
173 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
176 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
180 #define __NR_sys_uname __NR_uname
181 #define __NR_sys_getcwd1 __NR_getcwd
182 #define __NR_sys_getdents __NR_getdents
183 #define __NR_sys_getdents64 __NR_getdents64
184 #define __NR_sys_getpriority __NR_getpriority
185 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
186 #define __NR_sys_syslog __NR_syslog
187 #define __NR_sys_tgkill __NR_tgkill
188 #define __NR_sys_tkill __NR_tkill
189 #define __NR_sys_futex __NR_futex
190 #define __NR_sys_inotify_init __NR_inotify_init
191 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
192 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
194 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
196 #define __NR__llseek __NR_lseek
200 _syscall0(int, gettid)
202 /* This is a replacement for the host gettid() and must return a host
204 static int gettid(void) {
209 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
211 #if !defined(__NR_getdents) || \
212 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
213 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
215 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
216 _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
217 loff_t *, res, uint, wh);
219 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
220 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
221 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
222 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
224 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
225 _syscall2(int,sys_tkill,int,tid,int,sig)
227 #ifdef __NR_exit_group
228 _syscall1(int,exit_group,int,error_code)
230 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
231 _syscall1(int,set_tid_address,int *,tidptr)
233 #if defined(TARGET_NR_futex) && defined(__NR_futex)
234 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
235 const struct timespec *,timeout,int *,uaddr2,int,val3)
237 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
238 _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
239 unsigned long *, user_mask_ptr);
240 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
241 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
242 unsigned long *, user_mask_ptr);
243 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
246 static bitmask_transtbl fcntl_flags_tbl[] = {
247 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
248 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
249 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
250 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
251 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
252 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
253 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
254 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
255 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
256 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
257 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
258 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
259 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
260 #if defined(O_DIRECT)
261 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
263 #if defined(O_NOATIME)
264 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
266 #if defined(O_CLOEXEC)
267 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
270 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
272 /* Don't terminate the list prematurely on 64-bit host+guest. */
273 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
274 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
279 #define COPY_UTSNAME_FIELD(dest, src) \
281 /* __NEW_UTS_LEN doesn't include terminating null */ \
282 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
283 (dest)[__NEW_UTS_LEN] = '\0'; \
286 static int sys_uname(struct new_utsname *buf)
288 struct utsname uts_buf;
290 if (uname(&uts_buf) < 0)
294 * Just in case these have some differences, we
295 * translate utsname to new_utsname (which is the
296 * struct linux kernel uses).
299 memset(buf, 0, sizeof(*buf));
300 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
301 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
302 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
303 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
304 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
306 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
310 #undef COPY_UTSNAME_FIELD
313 static int sys_getcwd1(char *buf, size_t size)
315 if (getcwd(buf, size) == NULL) {
316 /* getcwd() sets errno */
319 return strlen(buf)+1;
322 #ifdef TARGET_NR_openat
323 static int sys_openat(int dirfd, const char *pathname, int flags, mode_t mode)
326 * open(2) has extra parameter 'mode' when called with
329 if ((flags & O_CREAT) != 0) {
330 return (openat(dirfd, pathname, flags, mode));
332 return (openat(dirfd, pathname, flags));
336 #ifdef TARGET_NR_utimensat
337 #ifdef CONFIG_UTIMENSAT
338 static int sys_utimensat(int dirfd, const char *pathname,
339 const struct timespec times[2], int flags)
341 if (pathname == NULL)
342 return futimens(dirfd, times);
344 return utimensat(dirfd, pathname, times, flags);
346 #elif defined(__NR_utimensat)
347 #define __NR_sys_utimensat __NR_utimensat
348 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
349 const struct timespec *,tsp,int,flags)
351 static int sys_utimensat(int dirfd, const char *pathname,
352 const struct timespec times[2], int flags)
358 #endif /* TARGET_NR_utimensat */
360 #ifdef CONFIG_INOTIFY
361 #include <sys/inotify.h>
363 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
364 static int sys_inotify_init(void)
366 return (inotify_init());
369 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
370 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
372 return (inotify_add_watch(fd, pathname, mask));
375 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
376 static int sys_inotify_rm_watch(int fd, int32_t wd)
378 return (inotify_rm_watch(fd, wd));
381 #ifdef CONFIG_INOTIFY1
382 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
383 static int sys_inotify_init1(int flags)
385 return (inotify_init1(flags));
390 /* Userspace can usually survive runtime without inotify */
391 #undef TARGET_NR_inotify_init
392 #undef TARGET_NR_inotify_init1
393 #undef TARGET_NR_inotify_add_watch
394 #undef TARGET_NR_inotify_rm_watch
395 #endif /* CONFIG_INOTIFY */
397 #if defined(TARGET_NR_ppoll)
399 # define __NR_ppoll -1
401 #define __NR_sys_ppoll __NR_ppoll
402 _syscall5(int, sys_ppoll, struct pollfd *, fds, nfds_t, nfds,
403 struct timespec *, timeout, const __sigset_t *, sigmask,
407 #if defined(TARGET_NR_pselect6)
408 #ifndef __NR_pselect6
409 # define __NR_pselect6 -1
411 #define __NR_sys_pselect6 __NR_pselect6
412 _syscall6(int, sys_pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds,
413 fd_set *, exceptfds, struct timespec *, timeout, void *, sig);
416 #if defined(TARGET_NR_prlimit64)
417 #ifndef __NR_prlimit64
418 # define __NR_prlimit64 -1
420 #define __NR_sys_prlimit64 __NR_prlimit64
421 /* The glibc rlimit structure may not be that used by the underlying syscall */
422 struct host_rlimit64 {
426 _syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
427 const struct host_rlimit64 *, new_limit,
428 struct host_rlimit64 *, old_limit)
431 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
433 static inline int regpairs_aligned(void *cpu_env) {
434 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
436 #elif defined(TARGET_MIPS)
437 static inline int regpairs_aligned(void *cpu_env) { return 1; }
438 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
439 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
440 * of registers which translates to the same as ARM/MIPS, because we start with
442 static inline int regpairs_aligned(void *cpu_env) { return 1; }
444 static inline int regpairs_aligned(void *cpu_env) { return 0; }
447 #define ERRNO_TABLE_SIZE 1200
449 /* target_to_host_errno_table[] is initialized from
450 * host_to_target_errno_table[] in syscall_init(). */
451 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
455 * This list is the union of errno values overridden in asm-<arch>/errno.h
456 * minus the errnos that are not actually generic to all archs.
458 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
459 [EIDRM] = TARGET_EIDRM,
460 [ECHRNG] = TARGET_ECHRNG,
461 [EL2NSYNC] = TARGET_EL2NSYNC,
462 [EL3HLT] = TARGET_EL3HLT,
463 [EL3RST] = TARGET_EL3RST,
464 [ELNRNG] = TARGET_ELNRNG,
465 [EUNATCH] = TARGET_EUNATCH,
466 [ENOCSI] = TARGET_ENOCSI,
467 [EL2HLT] = TARGET_EL2HLT,
468 [EDEADLK] = TARGET_EDEADLK,
469 [ENOLCK] = TARGET_ENOLCK,
470 [EBADE] = TARGET_EBADE,
471 [EBADR] = TARGET_EBADR,
472 [EXFULL] = TARGET_EXFULL,
473 [ENOANO] = TARGET_ENOANO,
474 [EBADRQC] = TARGET_EBADRQC,
475 [EBADSLT] = TARGET_EBADSLT,
476 [EBFONT] = TARGET_EBFONT,
477 [ENOSTR] = TARGET_ENOSTR,
478 [ENODATA] = TARGET_ENODATA,
479 [ETIME] = TARGET_ETIME,
480 [ENOSR] = TARGET_ENOSR,
481 [ENONET] = TARGET_ENONET,
482 [ENOPKG] = TARGET_ENOPKG,
483 [EREMOTE] = TARGET_EREMOTE,
484 [ENOLINK] = TARGET_ENOLINK,
485 [EADV] = TARGET_EADV,
486 [ESRMNT] = TARGET_ESRMNT,
487 [ECOMM] = TARGET_ECOMM,
488 [EPROTO] = TARGET_EPROTO,
489 [EDOTDOT] = TARGET_EDOTDOT,
490 [EMULTIHOP] = TARGET_EMULTIHOP,
491 [EBADMSG] = TARGET_EBADMSG,
492 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
493 [EOVERFLOW] = TARGET_EOVERFLOW,
494 [ENOTUNIQ] = TARGET_ENOTUNIQ,
495 [EBADFD] = TARGET_EBADFD,
496 [EREMCHG] = TARGET_EREMCHG,
497 [ELIBACC] = TARGET_ELIBACC,
498 [ELIBBAD] = TARGET_ELIBBAD,
499 [ELIBSCN] = TARGET_ELIBSCN,
500 [ELIBMAX] = TARGET_ELIBMAX,
501 [ELIBEXEC] = TARGET_ELIBEXEC,
502 [EILSEQ] = TARGET_EILSEQ,
503 [ENOSYS] = TARGET_ENOSYS,
504 [ELOOP] = TARGET_ELOOP,
505 [ERESTART] = TARGET_ERESTART,
506 [ESTRPIPE] = TARGET_ESTRPIPE,
507 [ENOTEMPTY] = TARGET_ENOTEMPTY,
508 [EUSERS] = TARGET_EUSERS,
509 [ENOTSOCK] = TARGET_ENOTSOCK,
510 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
511 [EMSGSIZE] = TARGET_EMSGSIZE,
512 [EPROTOTYPE] = TARGET_EPROTOTYPE,
513 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
514 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
515 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
516 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
517 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
518 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
519 [EADDRINUSE] = TARGET_EADDRINUSE,
520 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
521 [ENETDOWN] = TARGET_ENETDOWN,
522 [ENETUNREACH] = TARGET_ENETUNREACH,
523 [ENETRESET] = TARGET_ENETRESET,
524 [ECONNABORTED] = TARGET_ECONNABORTED,
525 [ECONNRESET] = TARGET_ECONNRESET,
526 [ENOBUFS] = TARGET_ENOBUFS,
527 [EISCONN] = TARGET_EISCONN,
528 [ENOTCONN] = TARGET_ENOTCONN,
529 [EUCLEAN] = TARGET_EUCLEAN,
530 [ENOTNAM] = TARGET_ENOTNAM,
531 [ENAVAIL] = TARGET_ENAVAIL,
532 [EISNAM] = TARGET_EISNAM,
533 [EREMOTEIO] = TARGET_EREMOTEIO,
534 [ESHUTDOWN] = TARGET_ESHUTDOWN,
535 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
536 [ETIMEDOUT] = TARGET_ETIMEDOUT,
537 [ECONNREFUSED] = TARGET_ECONNREFUSED,
538 [EHOSTDOWN] = TARGET_EHOSTDOWN,
539 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
540 [EALREADY] = TARGET_EALREADY,
541 [EINPROGRESS] = TARGET_EINPROGRESS,
542 [ESTALE] = TARGET_ESTALE,
543 [ECANCELED] = TARGET_ECANCELED,
544 [ENOMEDIUM] = TARGET_ENOMEDIUM,
545 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
547 [ENOKEY] = TARGET_ENOKEY,
550 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
553 [EKEYREVOKED] = TARGET_EKEYREVOKED,
556 [EKEYREJECTED] = TARGET_EKEYREJECTED,
559 [EOWNERDEAD] = TARGET_EOWNERDEAD,
561 #ifdef ENOTRECOVERABLE
562 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
566 static inline int host_to_target_errno(int err)
568 if(host_to_target_errno_table[err])
569 return host_to_target_errno_table[err];
573 static inline int target_to_host_errno(int err)
575 if (target_to_host_errno_table[err])
576 return target_to_host_errno_table[err];
580 static inline abi_long get_errno(abi_long ret)
583 return -host_to_target_errno(errno);
588 static inline int is_error(abi_long ret)
590 return (abi_ulong)ret >= (abi_ulong)(-4096);
593 char *target_strerror(int err)
595 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
598 return strerror(target_to_host_errno(err));
601 static abi_ulong target_brk;
602 static abi_ulong target_original_brk;
603 static abi_ulong brk_page;
605 void target_set_brk(abi_ulong new_brk)
607 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
608 brk_page = HOST_PAGE_ALIGN(target_brk);
611 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
612 #define DEBUGF_BRK(message, args...)
614 /* do_brk() must return target values and target errnos. */
615 abi_long do_brk(abi_ulong new_brk)
617 abi_long mapped_addr;
620 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
623 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
626 if (new_brk < target_original_brk) {
627 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
632 /* If the new brk is less than the highest page reserved to the
633 * target heap allocation, set it and we're almost done... */
634 if (new_brk <= brk_page) {
635 /* Heap contents are initialized to zero, as for anonymous
637 if (new_brk > target_brk) {
638 memset(g2h(target_brk), 0, new_brk - target_brk);
640 target_brk = new_brk;
641 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
645 /* We need to allocate more memory after the brk... Note that
646 * we don't use MAP_FIXED because that will map over the top of
647 * any existing mapping (like the one with the host libc or qemu
648 * itself); instead we treat "mapped but at wrong address" as
649 * a failure and unmap again.
651 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
652 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
653 PROT_READ|PROT_WRITE,
654 MAP_ANON|MAP_PRIVATE, 0, 0));
656 if (mapped_addr == brk_page) {
657 /* Heap contents are initialized to zero, as for anonymous
658 * mapped pages. Technically the new pages are already
659 * initialized to zero since they *are* anonymous mapped
660 * pages, however we have to take care with the contents that
661 * come from the remaining part of the previous page: it may
662 * contains garbage data due to a previous heap usage (grown
664 memset(g2h(target_brk), 0, brk_page - target_brk);
666 target_brk = new_brk;
667 brk_page = HOST_PAGE_ALIGN(target_brk);
668 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
671 } else if (mapped_addr != -1) {
672 /* Mapped but at wrong address, meaning there wasn't actually
673 * enough space for this brk.
675 target_munmap(mapped_addr, new_alloc_size);
677 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
680 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
683 #if defined(TARGET_ALPHA)
684 /* We (partially) emulate OSF/1 on Alpha, which requires we
685 return a proper errno, not an unchanged brk value. */
686 return -TARGET_ENOMEM;
688 /* For everything else, return the previous break. */
692 static inline abi_long copy_from_user_fdset(fd_set *fds,
693 abi_ulong target_fds_addr,
697 abi_ulong b, *target_fds;
699 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
700 if (!(target_fds = lock_user(VERIFY_READ,
702 sizeof(abi_ulong) * nw,
704 return -TARGET_EFAULT;
708 for (i = 0; i < nw; i++) {
709 /* grab the abi_ulong */
710 __get_user(b, &target_fds[i]);
711 for (j = 0; j < TARGET_ABI_BITS; j++) {
712 /* check the bit inside the abi_ulong */
719 unlock_user(target_fds, target_fds_addr, 0);
724 static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
725 abi_ulong target_fds_addr,
728 if (target_fds_addr) {
729 if (copy_from_user_fdset(fds, target_fds_addr, n))
730 return -TARGET_EFAULT;
738 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
744 abi_ulong *target_fds;
746 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
747 if (!(target_fds = lock_user(VERIFY_WRITE,
749 sizeof(abi_ulong) * nw,
751 return -TARGET_EFAULT;
754 for (i = 0; i < nw; i++) {
756 for (j = 0; j < TARGET_ABI_BITS; j++) {
757 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
760 __put_user(v, &target_fds[i]);
763 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
768 #if defined(__alpha__)
774 static inline abi_long host_to_target_clock_t(long ticks)
776 #if HOST_HZ == TARGET_HZ
779 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
783 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
784 const struct rusage *rusage)
786 struct target_rusage *target_rusage;
788 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
789 return -TARGET_EFAULT;
790 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
791 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
792 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
793 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
794 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
795 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
796 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
797 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
798 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
799 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
800 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
801 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
802 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
803 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
804 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
805 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
806 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
807 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
808 unlock_user_struct(target_rusage, target_addr, 1);
813 static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
815 abi_ulong target_rlim_swap;
818 target_rlim_swap = tswapal(target_rlim);
819 if (target_rlim_swap == TARGET_RLIM_INFINITY)
820 return RLIM_INFINITY;
822 result = target_rlim_swap;
823 if (target_rlim_swap != (rlim_t)result)
824 return RLIM_INFINITY;
829 static inline abi_ulong host_to_target_rlim(rlim_t rlim)
831 abi_ulong target_rlim_swap;
834 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
835 target_rlim_swap = TARGET_RLIM_INFINITY;
837 target_rlim_swap = rlim;
838 result = tswapal(target_rlim_swap);
843 static inline int target_to_host_resource(int code)
846 case TARGET_RLIMIT_AS:
848 case TARGET_RLIMIT_CORE:
850 case TARGET_RLIMIT_CPU:
852 case TARGET_RLIMIT_DATA:
854 case TARGET_RLIMIT_FSIZE:
856 case TARGET_RLIMIT_LOCKS:
858 case TARGET_RLIMIT_MEMLOCK:
859 return RLIMIT_MEMLOCK;
860 case TARGET_RLIMIT_MSGQUEUE:
861 return RLIMIT_MSGQUEUE;
862 case TARGET_RLIMIT_NICE:
864 case TARGET_RLIMIT_NOFILE:
865 return RLIMIT_NOFILE;
866 case TARGET_RLIMIT_NPROC:
868 case TARGET_RLIMIT_RSS:
870 case TARGET_RLIMIT_RTPRIO:
871 return RLIMIT_RTPRIO;
872 case TARGET_RLIMIT_SIGPENDING:
873 return RLIMIT_SIGPENDING;
874 case TARGET_RLIMIT_STACK:
881 static inline abi_long copy_from_user_timeval(struct timeval *tv,
882 abi_ulong target_tv_addr)
884 struct target_timeval *target_tv;
886 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
887 return -TARGET_EFAULT;
889 __get_user(tv->tv_sec, &target_tv->tv_sec);
890 __get_user(tv->tv_usec, &target_tv->tv_usec);
892 unlock_user_struct(target_tv, target_tv_addr, 0);
897 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
898 const struct timeval *tv)
900 struct target_timeval *target_tv;
902 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
903 return -TARGET_EFAULT;
905 __put_user(tv->tv_sec, &target_tv->tv_sec);
906 __put_user(tv->tv_usec, &target_tv->tv_usec);
908 unlock_user_struct(target_tv, target_tv_addr, 1);
913 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
916 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
917 abi_ulong target_mq_attr_addr)
919 struct target_mq_attr *target_mq_attr;
921 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
922 target_mq_attr_addr, 1))
923 return -TARGET_EFAULT;
925 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
926 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
927 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
928 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
930 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
935 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
936 const struct mq_attr *attr)
938 struct target_mq_attr *target_mq_attr;
940 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
941 target_mq_attr_addr, 0))
942 return -TARGET_EFAULT;
944 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
945 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
946 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
947 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
949 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
955 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
956 /* do_select() must return target values and target errnos. */
957 static abi_long do_select(int n,
958 abi_ulong rfd_addr, abi_ulong wfd_addr,
959 abi_ulong efd_addr, abi_ulong target_tv_addr)
961 fd_set rfds, wfds, efds;
962 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
963 struct timeval tv, *tv_ptr;
966 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
970 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
974 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
979 if (target_tv_addr) {
980 if (copy_from_user_timeval(&tv, target_tv_addr))
981 return -TARGET_EFAULT;
987 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
989 if (!is_error(ret)) {
990 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
991 return -TARGET_EFAULT;
992 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
993 return -TARGET_EFAULT;
994 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
995 return -TARGET_EFAULT;
997 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
998 return -TARGET_EFAULT;
1005 static abi_long do_pipe2(int host_pipe[], int flags)
1008 return pipe2(host_pipe, flags);
1014 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1015 int flags, int is_pipe2)
1019 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1022 return get_errno(ret);
1024 /* Several targets have special calling conventions for the original
1025 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1027 #if defined(TARGET_ALPHA)
1028 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1029 return host_pipe[0];
1030 #elif defined(TARGET_MIPS)
1031 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1032 return host_pipe[0];
1033 #elif defined(TARGET_SH4)
1034 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1035 return host_pipe[0];
1036 #elif defined(TARGET_SPARC)
1037 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1038 return host_pipe[0];
1042 if (put_user_s32(host_pipe[0], pipedes)
1043 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1044 return -TARGET_EFAULT;
1045 return get_errno(ret);
1048 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1049 abi_ulong target_addr,
1052 struct target_ip_mreqn *target_smreqn;
1054 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1056 return -TARGET_EFAULT;
1057 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1058 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1059 if (len == sizeof(struct target_ip_mreqn))
1060 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
1061 unlock_user(target_smreqn, target_addr, 0);
1066 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1067 abi_ulong target_addr,
1070 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1071 sa_family_t sa_family;
1072 struct target_sockaddr *target_saddr;
1074 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1076 return -TARGET_EFAULT;
1078 sa_family = tswap16(target_saddr->sa_family);
1080 /* Oops. The caller might send a incomplete sun_path; sun_path
1081 * must be terminated by \0 (see the manual page), but
1082 * unfortunately it is quite common to specify sockaddr_un
1083 * length as "strlen(x->sun_path)" while it should be
1084 * "strlen(...) + 1". We'll fix that here if needed.
1085 * Linux kernel has a similar feature.
1088 if (sa_family == AF_UNIX) {
1089 if (len < unix_maxlen && len > 0) {
1090 char *cp = (char*)target_saddr;
1092 if ( cp[len-1] && !cp[len] )
1095 if (len > unix_maxlen)
1099 memcpy(addr, target_saddr, len);
1100 addr->sa_family = sa_family;
1101 unlock_user(target_saddr, target_addr, 0);
1106 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1107 struct sockaddr *addr,
1110 struct target_sockaddr *target_saddr;
1112 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1114 return -TARGET_EFAULT;
1115 memcpy(target_saddr, addr, len);
1116 target_saddr->sa_family = tswap16(addr->sa_family);
1117 unlock_user(target_saddr, target_addr, len);
1122 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1123 struct target_msghdr *target_msgh)
1125 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1126 abi_long msg_controllen;
1127 abi_ulong target_cmsg_addr;
1128 struct target_cmsghdr *target_cmsg;
1129 socklen_t space = 0;
1131 msg_controllen = tswapal(target_msgh->msg_controllen);
1132 if (msg_controllen < sizeof (struct target_cmsghdr))
1134 target_cmsg_addr = tswapal(target_msgh->msg_control);
1135 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1137 return -TARGET_EFAULT;
1139 while (cmsg && target_cmsg) {
1140 void *data = CMSG_DATA(cmsg);
1141 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1143 int len = tswapal(target_cmsg->cmsg_len)
1144 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1146 space += CMSG_SPACE(len);
1147 if (space > msgh->msg_controllen) {
1148 space -= CMSG_SPACE(len);
1149 gemu_log("Host cmsg overflow\n");
1153 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1154 cmsg->cmsg_level = SOL_SOCKET;
1156 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1158 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1159 cmsg->cmsg_len = CMSG_LEN(len);
1161 if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1162 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1163 memcpy(data, target_data, len);
1165 int *fd = (int *)data;
1166 int *target_fd = (int *)target_data;
1167 int i, numfds = len / sizeof(int);
1169 for (i = 0; i < numfds; i++)
1170 fd[i] = tswap32(target_fd[i]);
1173 cmsg = CMSG_NXTHDR(msgh, cmsg);
1174 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1176 unlock_user(target_cmsg, target_cmsg_addr, 0);
1178 msgh->msg_controllen = space;
1182 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1183 struct msghdr *msgh)
1185 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1186 abi_long msg_controllen;
1187 abi_ulong target_cmsg_addr;
1188 struct target_cmsghdr *target_cmsg;
1189 socklen_t space = 0;
1191 msg_controllen = tswapal(target_msgh->msg_controllen);
1192 if (msg_controllen < sizeof (struct target_cmsghdr))
1194 target_cmsg_addr = tswapal(target_msgh->msg_control);
1195 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1197 return -TARGET_EFAULT;
1199 while (cmsg && target_cmsg) {
1200 void *data = CMSG_DATA(cmsg);
1201 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1203 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1205 space += TARGET_CMSG_SPACE(len);
1206 if (space > msg_controllen) {
1207 space -= TARGET_CMSG_SPACE(len);
1208 gemu_log("Target cmsg overflow\n");
1212 if (cmsg->cmsg_level == SOL_SOCKET) {
1213 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1215 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1217 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1218 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(len));
1220 if ((cmsg->cmsg_level == SOL_SOCKET) &&
1221 (cmsg->cmsg_type == SCM_RIGHTS)) {
1222 int *fd = (int *)data;
1223 int *target_fd = (int *)target_data;
1224 int i, numfds = len / sizeof(int);
1226 for (i = 0; i < numfds; i++)
1227 target_fd[i] = tswap32(fd[i]);
1228 } else if ((cmsg->cmsg_level == SOL_SOCKET) &&
1229 (cmsg->cmsg_type == SO_TIMESTAMP) &&
1230 (len == sizeof(struct timeval))) {
1231 /* copy struct timeval to target */
1232 struct timeval *tv = (struct timeval *)data;
1233 struct target_timeval *target_tv =
1234 (struct target_timeval *)target_data;
1236 target_tv->tv_sec = tswapal(tv->tv_sec);
1237 target_tv->tv_usec = tswapal(tv->tv_usec);
1239 gemu_log("Unsupported ancillary data: %d/%d\n",
1240 cmsg->cmsg_level, cmsg->cmsg_type);
1241 memcpy(target_data, data, len);
1244 cmsg = CMSG_NXTHDR(msgh, cmsg);
1245 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1247 unlock_user(target_cmsg, target_cmsg_addr, space);
1249 target_msgh->msg_controllen = tswapal(space);
1253 /* do_setsockopt() Must return target values and target errnos. */
1254 static abi_long do_setsockopt(int sockfd, int level, int optname,
1255 abi_ulong optval_addr, socklen_t optlen)
1259 struct ip_mreqn *ip_mreq;
1260 struct ip_mreq_source *ip_mreq_source;
1264 /* TCP options all take an 'int' value. */
1265 if (optlen < sizeof(uint32_t))
1266 return -TARGET_EINVAL;
1268 if (get_user_u32(val, optval_addr))
1269 return -TARGET_EFAULT;
1270 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1277 case IP_ROUTER_ALERT:
1281 case IP_MTU_DISCOVER:
1287 case IP_MULTICAST_TTL:
1288 case IP_MULTICAST_LOOP:
1290 if (optlen >= sizeof(uint32_t)) {
1291 if (get_user_u32(val, optval_addr))
1292 return -TARGET_EFAULT;
1293 } else if (optlen >= 1) {
1294 if (get_user_u8(val, optval_addr))
1295 return -TARGET_EFAULT;
1297 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1299 case IP_ADD_MEMBERSHIP:
1300 case IP_DROP_MEMBERSHIP:
1301 if (optlen < sizeof (struct target_ip_mreq) ||
1302 optlen > sizeof (struct target_ip_mreqn))
1303 return -TARGET_EINVAL;
1305 ip_mreq = (struct ip_mreqn *) alloca(optlen);
1306 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1307 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1310 case IP_BLOCK_SOURCE:
1311 case IP_UNBLOCK_SOURCE:
1312 case IP_ADD_SOURCE_MEMBERSHIP:
1313 case IP_DROP_SOURCE_MEMBERSHIP:
1314 if (optlen != sizeof (struct target_ip_mreq_source))
1315 return -TARGET_EINVAL;
1317 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1318 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1319 unlock_user (ip_mreq_source, optval_addr, 0);
1328 case IPV6_MTU_DISCOVER:
1331 case IPV6_RECVPKTINFO:
1333 if (optlen < sizeof(uint32_t)) {
1334 return -TARGET_EINVAL;
1336 if (get_user_u32(val, optval_addr)) {
1337 return -TARGET_EFAULT;
1339 ret = get_errno(setsockopt(sockfd, level, optname,
1340 &val, sizeof(val)));
1349 /* struct icmp_filter takes an u32 value */
1350 if (optlen < sizeof(uint32_t)) {
1351 return -TARGET_EINVAL;
1354 if (get_user_u32(val, optval_addr)) {
1355 return -TARGET_EFAULT;
1357 ret = get_errno(setsockopt(sockfd, level, optname,
1358 &val, sizeof(val)));
1365 case TARGET_SOL_SOCKET:
1367 case TARGET_SO_RCVTIMEO:
1371 optname = SO_RCVTIMEO;
1374 if (optlen != sizeof(struct target_timeval)) {
1375 return -TARGET_EINVAL;
1378 if (copy_from_user_timeval(&tv, optval_addr)) {
1379 return -TARGET_EFAULT;
1382 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
1386 case TARGET_SO_SNDTIMEO:
1387 optname = SO_SNDTIMEO;
1389 case TARGET_SO_ATTACH_FILTER:
1391 struct target_sock_fprog *tfprog;
1392 struct target_sock_filter *tfilter;
1393 struct sock_fprog fprog;
1394 struct sock_filter *filter;
1397 if (optlen != sizeof(*tfprog)) {
1398 return -TARGET_EINVAL;
1400 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
1401 return -TARGET_EFAULT;
1403 if (!lock_user_struct(VERIFY_READ, tfilter,
1404 tswapal(tfprog->filter), 0)) {
1405 unlock_user_struct(tfprog, optval_addr, 1);
1406 return -TARGET_EFAULT;
1409 fprog.len = tswap16(tfprog->len);
1410 filter = malloc(fprog.len * sizeof(*filter));
1411 if (filter == NULL) {
1412 unlock_user_struct(tfilter, tfprog->filter, 1);
1413 unlock_user_struct(tfprog, optval_addr, 1);
1414 return -TARGET_ENOMEM;
1416 for (i = 0; i < fprog.len; i++) {
1417 filter[i].code = tswap16(tfilter[i].code);
1418 filter[i].jt = tfilter[i].jt;
1419 filter[i].jf = tfilter[i].jf;
1420 filter[i].k = tswap32(tfilter[i].k);
1422 fprog.filter = filter;
1424 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
1425 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
1428 unlock_user_struct(tfilter, tfprog->filter, 1);
1429 unlock_user_struct(tfprog, optval_addr, 1);
1432 /* Options with 'int' argument. */
1433 case TARGET_SO_DEBUG:
1436 case TARGET_SO_REUSEADDR:
1437 optname = SO_REUSEADDR;
1439 case TARGET_SO_TYPE:
1442 case TARGET_SO_ERROR:
1445 case TARGET_SO_DONTROUTE:
1446 optname = SO_DONTROUTE;
1448 case TARGET_SO_BROADCAST:
1449 optname = SO_BROADCAST;
1451 case TARGET_SO_SNDBUF:
1452 optname = SO_SNDBUF;
1454 case TARGET_SO_RCVBUF:
1455 optname = SO_RCVBUF;
1457 case TARGET_SO_KEEPALIVE:
1458 optname = SO_KEEPALIVE;
1460 case TARGET_SO_OOBINLINE:
1461 optname = SO_OOBINLINE;
1463 case TARGET_SO_NO_CHECK:
1464 optname = SO_NO_CHECK;
1466 case TARGET_SO_PRIORITY:
1467 optname = SO_PRIORITY;
1470 case TARGET_SO_BSDCOMPAT:
1471 optname = SO_BSDCOMPAT;
1474 case TARGET_SO_PASSCRED:
1475 optname = SO_PASSCRED;
1477 case TARGET_SO_TIMESTAMP:
1478 optname = SO_TIMESTAMP;
1480 case TARGET_SO_RCVLOWAT:
1481 optname = SO_RCVLOWAT;
1487 if (optlen < sizeof(uint32_t))
1488 return -TARGET_EINVAL;
1490 if (get_user_u32(val, optval_addr))
1491 return -TARGET_EFAULT;
1492 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1496 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
1497 ret = -TARGET_ENOPROTOOPT;
1502 /* do_getsockopt() Must return target values and target errnos. */
1503 static abi_long do_getsockopt(int sockfd, int level, int optname,
1504 abi_ulong optval_addr, abi_ulong optlen)
1511 case TARGET_SOL_SOCKET:
1514 /* These don't just return a single integer */
1515 case TARGET_SO_LINGER:
1516 case TARGET_SO_RCVTIMEO:
1517 case TARGET_SO_SNDTIMEO:
1518 case TARGET_SO_PEERNAME:
1520 case TARGET_SO_PEERCRED: {
1523 struct target_ucred *tcr;
1525 if (get_user_u32(len, optlen)) {
1526 return -TARGET_EFAULT;
1529 return -TARGET_EINVAL;
1533 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
1541 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
1542 return -TARGET_EFAULT;
1544 __put_user(cr.pid, &tcr->pid);
1545 __put_user(cr.uid, &tcr->uid);
1546 __put_user(cr.gid, &tcr->gid);
1547 unlock_user_struct(tcr, optval_addr, 1);
1548 if (put_user_u32(len, optlen)) {
1549 return -TARGET_EFAULT;
1553 /* Options with 'int' argument. */
1554 case TARGET_SO_DEBUG:
1557 case TARGET_SO_REUSEADDR:
1558 optname = SO_REUSEADDR;
1560 case TARGET_SO_TYPE:
1563 case TARGET_SO_ERROR:
1566 case TARGET_SO_DONTROUTE:
1567 optname = SO_DONTROUTE;
1569 case TARGET_SO_BROADCAST:
1570 optname = SO_BROADCAST;
1572 case TARGET_SO_SNDBUF:
1573 optname = SO_SNDBUF;
1575 case TARGET_SO_RCVBUF:
1576 optname = SO_RCVBUF;
1578 case TARGET_SO_KEEPALIVE:
1579 optname = SO_KEEPALIVE;
1581 case TARGET_SO_OOBINLINE:
1582 optname = SO_OOBINLINE;
1584 case TARGET_SO_NO_CHECK:
1585 optname = SO_NO_CHECK;
1587 case TARGET_SO_PRIORITY:
1588 optname = SO_PRIORITY;
1591 case TARGET_SO_BSDCOMPAT:
1592 optname = SO_BSDCOMPAT;
1595 case TARGET_SO_PASSCRED:
1596 optname = SO_PASSCRED;
1598 case TARGET_SO_TIMESTAMP:
1599 optname = SO_TIMESTAMP;
1601 case TARGET_SO_RCVLOWAT:
1602 optname = SO_RCVLOWAT;
1609 /* TCP options all take an 'int' value. */
1611 if (get_user_u32(len, optlen))
1612 return -TARGET_EFAULT;
1614 return -TARGET_EINVAL;
1616 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1622 if (put_user_u32(val, optval_addr))
1623 return -TARGET_EFAULT;
1625 if (put_user_u8(val, optval_addr))
1626 return -TARGET_EFAULT;
1628 if (put_user_u32(len, optlen))
1629 return -TARGET_EFAULT;
1636 case IP_ROUTER_ALERT:
1640 case IP_MTU_DISCOVER:
1646 case IP_MULTICAST_TTL:
1647 case IP_MULTICAST_LOOP:
1648 if (get_user_u32(len, optlen))
1649 return -TARGET_EFAULT;
1651 return -TARGET_EINVAL;
1653 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1656 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1658 if (put_user_u32(len, optlen)
1659 || put_user_u8(val, optval_addr))
1660 return -TARGET_EFAULT;
1662 if (len > sizeof(int))
1664 if (put_user_u32(len, optlen)
1665 || put_user_u32(val, optval_addr))
1666 return -TARGET_EFAULT;
1670 ret = -TARGET_ENOPROTOOPT;
1676 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1678 ret = -TARGET_EOPNOTSUPP;
1684 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
1685 int count, int copy)
1687 struct target_iovec *target_vec;
1689 abi_ulong total_len, max_len;
1696 if (count < 0 || count > IOV_MAX) {
1701 vec = calloc(count, sizeof(struct iovec));
1707 target_vec = lock_user(VERIFY_READ, target_addr,
1708 count * sizeof(struct target_iovec), 1);
1709 if (target_vec == NULL) {
1714 /* ??? If host page size > target page size, this will result in a
1715 value larger than what we can actually support. */
1716 max_len = 0x7fffffff & TARGET_PAGE_MASK;
1719 for (i = 0; i < count; i++) {
1720 abi_ulong base = tswapal(target_vec[i].iov_base);
1721 abi_long len = tswapal(target_vec[i].iov_len);
1726 } else if (len == 0) {
1727 /* Zero length pointer is ignored. */
1728 vec[i].iov_base = 0;
1730 vec[i].iov_base = lock_user(type, base, len, copy);
1731 if (!vec[i].iov_base) {
1735 if (len > max_len - total_len) {
1736 len = max_len - total_len;
1739 vec[i].iov_len = len;
1743 unlock_user(target_vec, target_addr, 0);
1749 unlock_user(target_vec, target_addr, 0);
1753 static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1754 int count, int copy)
1756 struct target_iovec *target_vec;
1759 target_vec = lock_user(VERIFY_READ, target_addr,
1760 count * sizeof(struct target_iovec), 1);
1762 for (i = 0; i < count; i++) {
1763 abi_ulong base = tswapal(target_vec[i].iov_base);
1764 abi_long len = tswapal(target_vec[i].iov_base);
1768 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1770 unlock_user(target_vec, target_addr, 0);
1776 static inline void target_to_host_sock_type(int *type)
1779 int target_type = *type;
1781 switch (target_type & TARGET_SOCK_TYPE_MASK) {
1782 case TARGET_SOCK_DGRAM:
1783 host_type = SOCK_DGRAM;
1785 case TARGET_SOCK_STREAM:
1786 host_type = SOCK_STREAM;
1789 host_type = target_type & TARGET_SOCK_TYPE_MASK;
1792 if (target_type & TARGET_SOCK_CLOEXEC) {
1793 host_type |= SOCK_CLOEXEC;
1795 if (target_type & TARGET_SOCK_NONBLOCK) {
1796 host_type |= SOCK_NONBLOCK;
1801 /* do_socket() Must return target values and target errnos. */
1802 static abi_long do_socket(int domain, int type, int protocol)
1804 target_to_host_sock_type(&type);
1806 if (domain == PF_NETLINK)
1807 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1808 return get_errno(socket(domain, type, protocol));
1811 /* do_bind() Must return target values and target errnos. */
1812 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1818 if ((int)addrlen < 0) {
1819 return -TARGET_EINVAL;
1822 addr = alloca(addrlen+1);
1824 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1828 return get_errno(bind(sockfd, addr, addrlen));
1831 /* do_connect() Must return target values and target errnos. */
1832 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1838 if ((int)addrlen < 0) {
1839 return -TARGET_EINVAL;
1842 addr = alloca(addrlen);
1844 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1848 return get_errno(connect(sockfd, addr, addrlen));
1851 /* do_sendrecvmsg() Must return target values and target errnos. */
1852 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1853 int flags, int send)
1856 struct target_msghdr *msgp;
1860 abi_ulong target_vec;
1863 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1867 return -TARGET_EFAULT;
1868 if (msgp->msg_name) {
1869 msg.msg_namelen = tswap32(msgp->msg_namelen);
1870 msg.msg_name = alloca(msg.msg_namelen);
1871 ret = target_to_host_sockaddr(msg.msg_name, tswapal(msgp->msg_name),
1877 msg.msg_name = NULL;
1878 msg.msg_namelen = 0;
1880 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
1881 msg.msg_control = alloca(msg.msg_controllen);
1882 msg.msg_flags = tswap32(msgp->msg_flags);
1884 count = tswapal(msgp->msg_iovlen);
1885 target_vec = tswapal(msgp->msg_iov);
1886 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
1887 target_vec, count, send);
1889 ret = -host_to_target_errno(errno);
1892 msg.msg_iovlen = count;
1896 ret = target_to_host_cmsg(&msg, msgp);
1898 ret = get_errno(sendmsg(fd, &msg, flags));
1900 ret = get_errno(recvmsg(fd, &msg, flags));
1901 if (!is_error(ret)) {
1903 ret = host_to_target_cmsg(msgp, &msg);
1904 if (!is_error(ret)) {
1905 msgp->msg_namelen = tswap32(msg.msg_namelen);
1906 if (msg.msg_name != NULL) {
1907 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
1908 msg.msg_name, msg.msg_namelen);
1920 unlock_iovec(vec, target_vec, count, !send);
1922 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1926 /* If we don't have a system accept4() then just call accept.
1927 * The callsites to do_accept4() will ensure that they don't
1928 * pass a non-zero flags argument in this config.
1930 #ifndef CONFIG_ACCEPT4
1931 static inline int accept4(int sockfd, struct sockaddr *addr,
1932 socklen_t *addrlen, int flags)
1935 return accept(sockfd, addr, addrlen);
1939 /* do_accept4() Must return target values and target errnos. */
1940 static abi_long do_accept4(int fd, abi_ulong target_addr,
1941 abi_ulong target_addrlen_addr, int flags)
1947 if (target_addr == 0) {
1948 return get_errno(accept4(fd, NULL, NULL, flags));
1951 /* linux returns EINVAL if addrlen pointer is invalid */
1952 if (get_user_u32(addrlen, target_addrlen_addr))
1953 return -TARGET_EINVAL;
1955 if ((int)addrlen < 0) {
1956 return -TARGET_EINVAL;
1959 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1960 return -TARGET_EINVAL;
1962 addr = alloca(addrlen);
1964 ret = get_errno(accept4(fd, addr, &addrlen, flags));
1965 if (!is_error(ret)) {
1966 host_to_target_sockaddr(target_addr, addr, addrlen);
1967 if (put_user_u32(addrlen, target_addrlen_addr))
1968 ret = -TARGET_EFAULT;
1973 /* do_getpeername() Must return target values and target errnos. */
1974 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1975 abi_ulong target_addrlen_addr)
1981 if (get_user_u32(addrlen, target_addrlen_addr))
1982 return -TARGET_EFAULT;
1984 if ((int)addrlen < 0) {
1985 return -TARGET_EINVAL;
1988 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1989 return -TARGET_EFAULT;
1991 addr = alloca(addrlen);
1993 ret = get_errno(getpeername(fd, addr, &addrlen));
1994 if (!is_error(ret)) {
1995 host_to_target_sockaddr(target_addr, addr, addrlen);
1996 if (put_user_u32(addrlen, target_addrlen_addr))
1997 ret = -TARGET_EFAULT;
2002 /* do_getsockname() Must return target values and target errnos. */
2003 static abi_long do_getsockname(int fd, abi_ulong target_addr,
2004 abi_ulong target_addrlen_addr)
2010 if (get_user_u32(addrlen, target_addrlen_addr))
2011 return -TARGET_EFAULT;
2013 if ((int)addrlen < 0) {
2014 return -TARGET_EINVAL;
2017 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
2018 return -TARGET_EFAULT;
2020 addr = alloca(addrlen);
2022 ret = get_errno(getsockname(fd, addr, &addrlen));
2023 if (!is_error(ret)) {
2024 host_to_target_sockaddr(target_addr, addr, addrlen);
2025 if (put_user_u32(addrlen, target_addrlen_addr))
2026 ret = -TARGET_EFAULT;
2031 /* do_socketpair() Must return target values and target errnos. */
2032 static abi_long do_socketpair(int domain, int type, int protocol,
2033 abi_ulong target_tab_addr)
2038 target_to_host_sock_type(&type);
2040 ret = get_errno(socketpair(domain, type, protocol, tab));
2041 if (!is_error(ret)) {
2042 if (put_user_s32(tab[0], target_tab_addr)
2043 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
2044 ret = -TARGET_EFAULT;
2049 /* do_sendto() Must return target values and target errnos. */
2050 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
2051 abi_ulong target_addr, socklen_t addrlen)
2057 if ((int)addrlen < 0) {
2058 return -TARGET_EINVAL;
2061 host_msg = lock_user(VERIFY_READ, msg, len, 1);
2063 return -TARGET_EFAULT;
2065 addr = alloca(addrlen);
2066 ret = target_to_host_sockaddr(addr, target_addr, addrlen);
2068 unlock_user(host_msg, msg, 0);
2071 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
2073 ret = get_errno(send(fd, host_msg, len, flags));
2075 unlock_user(host_msg, msg, 0);
2079 /* do_recvfrom() Must return target values and target errnos. */
2080 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
2081 abi_ulong target_addr,
2082 abi_ulong target_addrlen)
2089 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
2091 return -TARGET_EFAULT;
2093 if (get_user_u32(addrlen, target_addrlen)) {
2094 ret = -TARGET_EFAULT;
2097 if ((int)addrlen < 0) {
2098 ret = -TARGET_EINVAL;
2101 addr = alloca(addrlen);
2102 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
2104 addr = NULL; /* To keep compiler quiet. */
2105 ret = get_errno(qemu_recv(fd, host_msg, len, flags));
2107 if (!is_error(ret)) {
2109 host_to_target_sockaddr(target_addr, addr, addrlen);
2110 if (put_user_u32(addrlen, target_addrlen)) {
2111 ret = -TARGET_EFAULT;
2115 unlock_user(host_msg, msg, len);
2118 unlock_user(host_msg, msg, 0);
2123 #ifdef TARGET_NR_socketcall
2124 /* do_socketcall() Must return target values and target errnos. */
2125 static abi_long do_socketcall(int num, abi_ulong vptr)
2128 const int n = sizeof(abi_ulong);
2133 abi_ulong domain, type, protocol;
2135 if (get_user_ual(domain, vptr)
2136 || get_user_ual(type, vptr + n)
2137 || get_user_ual(protocol, vptr + 2 * n))
2138 return -TARGET_EFAULT;
2140 ret = do_socket(domain, type, protocol);
2146 abi_ulong target_addr;
2149 if (get_user_ual(sockfd, vptr)
2150 || get_user_ual(target_addr, vptr + n)
2151 || get_user_ual(addrlen, vptr + 2 * n))
2152 return -TARGET_EFAULT;
2154 ret = do_bind(sockfd, target_addr, addrlen);
2157 case SOCKOP_connect:
2160 abi_ulong target_addr;
2163 if (get_user_ual(sockfd, vptr)
2164 || get_user_ual(target_addr, vptr + n)
2165 || get_user_ual(addrlen, vptr + 2 * n))
2166 return -TARGET_EFAULT;
2168 ret = do_connect(sockfd, target_addr, addrlen);
2173 abi_ulong sockfd, backlog;
2175 if (get_user_ual(sockfd, vptr)
2176 || get_user_ual(backlog, vptr + n))
2177 return -TARGET_EFAULT;
2179 ret = get_errno(listen(sockfd, backlog));
2185 abi_ulong target_addr, target_addrlen;
2187 if (get_user_ual(sockfd, vptr)
2188 || get_user_ual(target_addr, vptr + n)
2189 || get_user_ual(target_addrlen, vptr + 2 * n))
2190 return -TARGET_EFAULT;
2192 ret = do_accept4(sockfd, target_addr, target_addrlen, 0);
2195 case SOCKOP_getsockname:
2198 abi_ulong target_addr, target_addrlen;
2200 if (get_user_ual(sockfd, vptr)
2201 || get_user_ual(target_addr, vptr + n)
2202 || get_user_ual(target_addrlen, vptr + 2 * n))
2203 return -TARGET_EFAULT;
2205 ret = do_getsockname(sockfd, target_addr, target_addrlen);
2208 case SOCKOP_getpeername:
2211 abi_ulong target_addr, target_addrlen;
2213 if (get_user_ual(sockfd, vptr)
2214 || get_user_ual(target_addr, vptr + n)
2215 || get_user_ual(target_addrlen, vptr + 2 * n))
2216 return -TARGET_EFAULT;
2218 ret = do_getpeername(sockfd, target_addr, target_addrlen);
2221 case SOCKOP_socketpair:
2223 abi_ulong domain, type, protocol;
2226 if (get_user_ual(domain, vptr)
2227 || get_user_ual(type, vptr + n)
2228 || get_user_ual(protocol, vptr + 2 * n)
2229 || get_user_ual(tab, vptr + 3 * n))
2230 return -TARGET_EFAULT;
2232 ret = do_socketpair(domain, type, protocol, tab);
2242 if (get_user_ual(sockfd, vptr)
2243 || get_user_ual(msg, vptr + n)
2244 || get_user_ual(len, vptr + 2 * n)
2245 || get_user_ual(flags, vptr + 3 * n))
2246 return -TARGET_EFAULT;
2248 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
2258 if (get_user_ual(sockfd, vptr)
2259 || get_user_ual(msg, vptr + n)
2260 || get_user_ual(len, vptr + 2 * n)
2261 || get_user_ual(flags, vptr + 3 * n))
2262 return -TARGET_EFAULT;
2264 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
2276 if (get_user_ual(sockfd, vptr)
2277 || get_user_ual(msg, vptr + n)
2278 || get_user_ual(len, vptr + 2 * n)
2279 || get_user_ual(flags, vptr + 3 * n)
2280 || get_user_ual(addr, vptr + 4 * n)
2281 || get_user_ual(addrlen, vptr + 5 * n))
2282 return -TARGET_EFAULT;
2284 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
2287 case SOCKOP_recvfrom:
2296 if (get_user_ual(sockfd, vptr)
2297 || get_user_ual(msg, vptr + n)
2298 || get_user_ual(len, vptr + 2 * n)
2299 || get_user_ual(flags, vptr + 3 * n)
2300 || get_user_ual(addr, vptr + 4 * n)
2301 || get_user_ual(addrlen, vptr + 5 * n))
2302 return -TARGET_EFAULT;
2304 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
2307 case SOCKOP_shutdown:
2309 abi_ulong sockfd, how;
2311 if (get_user_ual(sockfd, vptr)
2312 || get_user_ual(how, vptr + n))
2313 return -TARGET_EFAULT;
2315 ret = get_errno(shutdown(sockfd, how));
2318 case SOCKOP_sendmsg:
2319 case SOCKOP_recvmsg:
2322 abi_ulong target_msg;
2325 if (get_user_ual(fd, vptr)
2326 || get_user_ual(target_msg, vptr + n)
2327 || get_user_ual(flags, vptr + 2 * n))
2328 return -TARGET_EFAULT;
2330 ret = do_sendrecvmsg(fd, target_msg, flags,
2331 (num == SOCKOP_sendmsg));
2334 case SOCKOP_setsockopt:
2342 if (get_user_ual(sockfd, vptr)
2343 || get_user_ual(level, vptr + n)
2344 || get_user_ual(optname, vptr + 2 * n)
2345 || get_user_ual(optval, vptr + 3 * n)
2346 || get_user_ual(optlen, vptr + 4 * n))
2347 return -TARGET_EFAULT;
2349 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2352 case SOCKOP_getsockopt:
2360 if (get_user_ual(sockfd, vptr)
2361 || get_user_ual(level, vptr + n)
2362 || get_user_ual(optname, vptr + 2 * n)
2363 || get_user_ual(optval, vptr + 3 * n)
2364 || get_user_ual(optlen, vptr + 4 * n))
2365 return -TARGET_EFAULT;
2367 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
2371 gemu_log("Unsupported socketcall: %d\n", num);
2372 ret = -TARGET_ENOSYS;
2379 #define N_SHM_REGIONS 32
2381 static struct shm_region {
2384 } shm_regions[N_SHM_REGIONS];
2386 struct target_ipc_perm
2393 unsigned short int mode;
2394 unsigned short int __pad1;
2395 unsigned short int __seq;
2396 unsigned short int __pad2;
2397 abi_ulong __unused1;
2398 abi_ulong __unused2;
2401 struct target_semid_ds
2403 struct target_ipc_perm sem_perm;
2404 abi_ulong sem_otime;
2405 abi_ulong __unused1;
2406 abi_ulong sem_ctime;
2407 abi_ulong __unused2;
2408 abi_ulong sem_nsems;
2409 abi_ulong __unused3;
2410 abi_ulong __unused4;
2413 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2414 abi_ulong target_addr)
2416 struct target_ipc_perm *target_ip;
2417 struct target_semid_ds *target_sd;
2419 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2420 return -TARGET_EFAULT;
2421 target_ip = &(target_sd->sem_perm);
2422 host_ip->__key = tswapal(target_ip->__key);
2423 host_ip->uid = tswapal(target_ip->uid);
2424 host_ip->gid = tswapal(target_ip->gid);
2425 host_ip->cuid = tswapal(target_ip->cuid);
2426 host_ip->cgid = tswapal(target_ip->cgid);
2427 host_ip->mode = tswap16(target_ip->mode);
2428 unlock_user_struct(target_sd, target_addr, 0);
2432 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2433 struct ipc_perm *host_ip)
2435 struct target_ipc_perm *target_ip;
2436 struct target_semid_ds *target_sd;
2438 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2439 return -TARGET_EFAULT;
2440 target_ip = &(target_sd->sem_perm);
2441 target_ip->__key = tswapal(host_ip->__key);
2442 target_ip->uid = tswapal(host_ip->uid);
2443 target_ip->gid = tswapal(host_ip->gid);
2444 target_ip->cuid = tswapal(host_ip->cuid);
2445 target_ip->cgid = tswapal(host_ip->cgid);
2446 target_ip->mode = tswap16(host_ip->mode);
2447 unlock_user_struct(target_sd, target_addr, 1);
2451 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2452 abi_ulong target_addr)
2454 struct target_semid_ds *target_sd;
2456 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2457 return -TARGET_EFAULT;
2458 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2459 return -TARGET_EFAULT;
2460 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
2461 host_sd->sem_otime = tswapal(target_sd->sem_otime);
2462 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
2463 unlock_user_struct(target_sd, target_addr, 0);
2467 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2468 struct semid_ds *host_sd)
2470 struct target_semid_ds *target_sd;
2472 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2473 return -TARGET_EFAULT;
2474 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2475 return -TARGET_EFAULT;
2476 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
2477 target_sd->sem_otime = tswapal(host_sd->sem_otime);
2478 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
2479 unlock_user_struct(target_sd, target_addr, 1);
2483 struct target_seminfo {
2496 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2497 struct seminfo *host_seminfo)
2499 struct target_seminfo *target_seminfo;
2500 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2501 return -TARGET_EFAULT;
2502 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2503 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2504 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2505 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2506 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2507 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2508 __put_user(host_seminfo->semume, &target_seminfo->semume);
2509 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2510 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2511 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2512 unlock_user_struct(target_seminfo, target_addr, 1);
2518 struct semid_ds *buf;
2519 unsigned short *array;
2520 struct seminfo *__buf;
2523 union target_semun {
2530 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2531 abi_ulong target_addr)
2534 unsigned short *array;
2536 struct semid_ds semid_ds;
2539 semun.buf = &semid_ds;
2541 ret = semctl(semid, 0, IPC_STAT, semun);
2543 return get_errno(ret);
2545 nsems = semid_ds.sem_nsems;
2547 *host_array = malloc(nsems*sizeof(unsigned short));
2548 array = lock_user(VERIFY_READ, target_addr,
2549 nsems*sizeof(unsigned short), 1);
2551 return -TARGET_EFAULT;
2553 for(i=0; i<nsems; i++) {
2554 __get_user((*host_array)[i], &array[i]);
2556 unlock_user(array, target_addr, 0);
2561 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2562 unsigned short **host_array)
2565 unsigned short *array;
2567 struct semid_ds semid_ds;
2570 semun.buf = &semid_ds;
2572 ret = semctl(semid, 0, IPC_STAT, semun);
2574 return get_errno(ret);
2576 nsems = semid_ds.sem_nsems;
2578 array = lock_user(VERIFY_WRITE, target_addr,
2579 nsems*sizeof(unsigned short), 0);
2581 return -TARGET_EFAULT;
2583 for(i=0; i<nsems; i++) {
2584 __put_user((*host_array)[i], &array[i]);
2587 unlock_user(array, target_addr, 1);
2592 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2593 union target_semun target_su)
2596 struct semid_ds dsarg;
2597 unsigned short *array = NULL;
2598 struct seminfo seminfo;
2599 abi_long ret = -TARGET_EINVAL;
2606 arg.val = tswap32(target_su.val);
2607 ret = get_errno(semctl(semid, semnum, cmd, arg));
2608 target_su.val = tswap32(arg.val);
2612 err = target_to_host_semarray(semid, &array, target_su.array);
2616 ret = get_errno(semctl(semid, semnum, cmd, arg));
2617 err = host_to_target_semarray(semid, target_su.array, &array);
2624 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2628 ret = get_errno(semctl(semid, semnum, cmd, arg));
2629 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2635 arg.__buf = &seminfo;
2636 ret = get_errno(semctl(semid, semnum, cmd, arg));
2637 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2645 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2652 struct target_sembuf {
2653 unsigned short sem_num;
2658 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2659 abi_ulong target_addr,
2662 struct target_sembuf *target_sembuf;
2665 target_sembuf = lock_user(VERIFY_READ, target_addr,
2666 nsops*sizeof(struct target_sembuf), 1);
2668 return -TARGET_EFAULT;
2670 for(i=0; i<nsops; i++) {
2671 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2672 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2673 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2676 unlock_user(target_sembuf, target_addr, 0);
2681 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2683 struct sembuf sops[nsops];
2685 if (target_to_host_sembuf(sops, ptr, nsops))
2686 return -TARGET_EFAULT;
2688 return get_errno(semop(semid, sops, nsops));
2691 struct target_msqid_ds
2693 struct target_ipc_perm msg_perm;
2694 abi_ulong msg_stime;
2695 #if TARGET_ABI_BITS == 32
2696 abi_ulong __unused1;
2698 abi_ulong msg_rtime;
2699 #if TARGET_ABI_BITS == 32
2700 abi_ulong __unused2;
2702 abi_ulong msg_ctime;
2703 #if TARGET_ABI_BITS == 32
2704 abi_ulong __unused3;
2706 abi_ulong __msg_cbytes;
2708 abi_ulong msg_qbytes;
2709 abi_ulong msg_lspid;
2710 abi_ulong msg_lrpid;
2711 abi_ulong __unused4;
2712 abi_ulong __unused5;
2715 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2716 abi_ulong target_addr)
2718 struct target_msqid_ds *target_md;
2720 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2721 return -TARGET_EFAULT;
2722 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2723 return -TARGET_EFAULT;
2724 host_md->msg_stime = tswapal(target_md->msg_stime);
2725 host_md->msg_rtime = tswapal(target_md->msg_rtime);
2726 host_md->msg_ctime = tswapal(target_md->msg_ctime);
2727 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
2728 host_md->msg_qnum = tswapal(target_md->msg_qnum);
2729 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
2730 host_md->msg_lspid = tswapal(target_md->msg_lspid);
2731 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
2732 unlock_user_struct(target_md, target_addr, 0);
2736 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2737 struct msqid_ds *host_md)
2739 struct target_msqid_ds *target_md;
2741 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2742 return -TARGET_EFAULT;
2743 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2744 return -TARGET_EFAULT;
2745 target_md->msg_stime = tswapal(host_md->msg_stime);
2746 target_md->msg_rtime = tswapal(host_md->msg_rtime);
2747 target_md->msg_ctime = tswapal(host_md->msg_ctime);
2748 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
2749 target_md->msg_qnum = tswapal(host_md->msg_qnum);
2750 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
2751 target_md->msg_lspid = tswapal(host_md->msg_lspid);
2752 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
2753 unlock_user_struct(target_md, target_addr, 1);
2757 struct target_msginfo {
2765 unsigned short int msgseg;
2768 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2769 struct msginfo *host_msginfo)
2771 struct target_msginfo *target_msginfo;
2772 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2773 return -TARGET_EFAULT;
2774 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2775 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2776 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2777 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2778 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2779 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2780 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2781 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2782 unlock_user_struct(target_msginfo, target_addr, 1);
2786 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
2788 struct msqid_ds dsarg;
2789 struct msginfo msginfo;
2790 abi_long ret = -TARGET_EINVAL;
2798 if (target_to_host_msqid_ds(&dsarg,ptr))
2799 return -TARGET_EFAULT;
2800 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2801 if (host_to_target_msqid_ds(ptr,&dsarg))
2802 return -TARGET_EFAULT;
2805 ret = get_errno(msgctl(msgid, cmd, NULL));
2809 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2810 if (host_to_target_msginfo(ptr, &msginfo))
2811 return -TARGET_EFAULT;
2818 struct target_msgbuf {
2823 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2824 unsigned int msgsz, int msgflg)
2826 struct target_msgbuf *target_mb;
2827 struct msgbuf *host_mb;
2830 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2831 return -TARGET_EFAULT;
2832 host_mb = malloc(msgsz+sizeof(long));
2833 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
2834 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
2835 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2837 unlock_user_struct(target_mb, msgp, 0);
2842 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2843 unsigned int msgsz, abi_long msgtyp,
2846 struct target_msgbuf *target_mb;
2848 struct msgbuf *host_mb;
2851 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2852 return -TARGET_EFAULT;
2854 host_mb = g_malloc(msgsz+sizeof(long));
2855 ret = get_errno(msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
2858 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2859 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2860 if (!target_mtext) {
2861 ret = -TARGET_EFAULT;
2864 memcpy(target_mb->mtext, host_mb->mtext, ret);
2865 unlock_user(target_mtext, target_mtext_addr, ret);
2868 target_mb->mtype = tswapal(host_mb->mtype);
2872 unlock_user_struct(target_mb, msgp, 1);
2877 struct target_shmid_ds
2879 struct target_ipc_perm shm_perm;
2880 abi_ulong shm_segsz;
2881 abi_ulong shm_atime;
2882 #if TARGET_ABI_BITS == 32
2883 abi_ulong __unused1;
2885 abi_ulong shm_dtime;
2886 #if TARGET_ABI_BITS == 32
2887 abi_ulong __unused2;
2889 abi_ulong shm_ctime;
2890 #if TARGET_ABI_BITS == 32
2891 abi_ulong __unused3;
2895 abi_ulong shm_nattch;
2896 unsigned long int __unused4;
2897 unsigned long int __unused5;
2900 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2901 abi_ulong target_addr)
2903 struct target_shmid_ds *target_sd;
2905 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2906 return -TARGET_EFAULT;
2907 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2908 return -TARGET_EFAULT;
2909 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2910 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2911 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2912 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2913 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2914 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2915 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2916 unlock_user_struct(target_sd, target_addr, 0);
2920 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2921 struct shmid_ds *host_sd)
2923 struct target_shmid_ds *target_sd;
2925 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2926 return -TARGET_EFAULT;
2927 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2928 return -TARGET_EFAULT;
2929 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2930 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2931 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2932 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2933 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2934 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2935 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2936 unlock_user_struct(target_sd, target_addr, 1);
2940 struct target_shminfo {
2948 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2949 struct shminfo *host_shminfo)
2951 struct target_shminfo *target_shminfo;
2952 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2953 return -TARGET_EFAULT;
2954 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2955 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2956 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2957 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2958 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2959 unlock_user_struct(target_shminfo, target_addr, 1);
2963 struct target_shm_info {
2968 abi_ulong swap_attempts;
2969 abi_ulong swap_successes;
2972 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2973 struct shm_info *host_shm_info)
2975 struct target_shm_info *target_shm_info;
2976 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2977 return -TARGET_EFAULT;
2978 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2979 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2980 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2981 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2982 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2983 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2984 unlock_user_struct(target_shm_info, target_addr, 1);
2988 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2990 struct shmid_ds dsarg;
2991 struct shminfo shminfo;
2992 struct shm_info shm_info;
2993 abi_long ret = -TARGET_EINVAL;
3001 if (target_to_host_shmid_ds(&dsarg, buf))
3002 return -TARGET_EFAULT;
3003 ret = get_errno(shmctl(shmid, cmd, &dsarg));
3004 if (host_to_target_shmid_ds(buf, &dsarg))
3005 return -TARGET_EFAULT;
3008 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
3009 if (host_to_target_shminfo(buf, &shminfo))
3010 return -TARGET_EFAULT;
3013 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
3014 if (host_to_target_shm_info(buf, &shm_info))
3015 return -TARGET_EFAULT;
3020 ret = get_errno(shmctl(shmid, cmd, NULL));
3027 static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
3031 struct shmid_ds shm_info;
3034 /* find out the length of the shared memory segment */
3035 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
3036 if (is_error(ret)) {
3037 /* can't get length, bail out */
3044 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
3046 abi_ulong mmap_start;
3048 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
3050 if (mmap_start == -1) {
3052 host_raddr = (void *)-1;
3054 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
3057 if (host_raddr == (void *)-1) {
3059 return get_errno((long)host_raddr);
3061 raddr=h2g((unsigned long)host_raddr);
3063 page_set_flags(raddr, raddr + shm_info.shm_segsz,
3064 PAGE_VALID | PAGE_READ |
3065 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
3067 for (i = 0; i < N_SHM_REGIONS; i++) {
3068 if (shm_regions[i].start == 0) {
3069 shm_regions[i].start = raddr;
3070 shm_regions[i].size = shm_info.shm_segsz;
3080 static inline abi_long do_shmdt(abi_ulong shmaddr)
3084 for (i = 0; i < N_SHM_REGIONS; ++i) {
3085 if (shm_regions[i].start == shmaddr) {
3086 shm_regions[i].start = 0;
3087 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
3092 return get_errno(shmdt(g2h(shmaddr)));
3095 #ifdef TARGET_NR_ipc
3096 /* ??? This only works with linear mappings. */
3097 /* do_ipc() must return target values and target errnos. */
3098 static abi_long do_ipc(unsigned int call, int first,
3099 int second, int third,
3100 abi_long ptr, abi_long fifth)
3105 version = call >> 16;
3110 ret = do_semop(first, ptr, second);
3114 ret = get_errno(semget(first, second, third));
3118 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
3122 ret = get_errno(msgget(first, second));
3126 ret = do_msgsnd(first, ptr, second, third);
3130 ret = do_msgctl(first, second, ptr);
3137 struct target_ipc_kludge {
3142 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
3143 ret = -TARGET_EFAULT;
3147 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
3149 unlock_user_struct(tmp, ptr, 0);
3153 ret = do_msgrcv(first, ptr, second, fifth, third);
3162 raddr = do_shmat(first, ptr, second);
3163 if (is_error(raddr))
3164 return get_errno(raddr);
3165 if (put_user_ual(raddr, third))
3166 return -TARGET_EFAULT;
3170 ret = -TARGET_EINVAL;
3175 ret = do_shmdt(ptr);
3179 /* IPC_* flag values are the same on all linux platforms */
3180 ret = get_errno(shmget(first, second, third));
3183 /* IPC_* and SHM_* command values are the same on all linux platforms */
3185 ret = do_shmctl(first, second, third);
3188 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
3189 ret = -TARGET_ENOSYS;
3196 /* kernel structure types definitions */
3198 #define STRUCT(name, ...) STRUCT_ ## name,
3199 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
3201 #include "syscall_types.h"
3204 #undef STRUCT_SPECIAL
3206 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
3207 #define STRUCT_SPECIAL(name)
3208 #include "syscall_types.h"
3210 #undef STRUCT_SPECIAL
3212 typedef struct IOCTLEntry IOCTLEntry;
3214 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
3215 int fd, abi_long cmd, abi_long arg);
3218 unsigned int target_cmd;
3219 unsigned int host_cmd;
3222 do_ioctl_fn *do_ioctl;
3223 const argtype arg_type[5];
3226 #define IOC_R 0x0001
3227 #define IOC_W 0x0002
3228 #define IOC_RW (IOC_R | IOC_W)
3230 #define MAX_STRUCT_SIZE 4096
3232 #ifdef CONFIG_FIEMAP
3233 /* So fiemap access checks don't overflow on 32 bit systems.
3234 * This is very slightly smaller than the limit imposed by
3235 * the underlying kernel.
3237 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3238 / sizeof(struct fiemap_extent))
3240 static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
3241 int fd, abi_long cmd, abi_long arg)
3243 /* The parameter for this ioctl is a struct fiemap followed
3244 * by an array of struct fiemap_extent whose size is set
3245 * in fiemap->fm_extent_count. The array is filled in by the
3248 int target_size_in, target_size_out;
3250 const argtype *arg_type = ie->arg_type;
3251 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3254 int i, extent_size = thunk_type_size(extent_arg_type, 0);
3258 assert(arg_type[0] == TYPE_PTR);
3259 assert(ie->access == IOC_RW);
3261 target_size_in = thunk_type_size(arg_type, 0);
3262 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3264 return -TARGET_EFAULT;
3266 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3267 unlock_user(argptr, arg, 0);
3268 fm = (struct fiemap *)buf_temp;
3269 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3270 return -TARGET_EINVAL;
3273 outbufsz = sizeof (*fm) +
3274 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3276 if (outbufsz > MAX_STRUCT_SIZE) {
3277 /* We can't fit all the extents into the fixed size buffer.
3278 * Allocate one that is large enough and use it instead.
3280 fm = malloc(outbufsz);
3282 return -TARGET_ENOMEM;
3284 memcpy(fm, buf_temp, sizeof(struct fiemap));
3287 ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3288 if (!is_error(ret)) {
3289 target_size_out = target_size_in;
3290 /* An extent_count of 0 means we were only counting the extents
3291 * so there are no structs to copy
3293 if (fm->fm_extent_count != 0) {
3294 target_size_out += fm->fm_mapped_extents * extent_size;
3296 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3298 ret = -TARGET_EFAULT;
3300 /* Convert the struct fiemap */
3301 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3302 if (fm->fm_extent_count != 0) {
3303 p = argptr + target_size_in;
3304 /* ...and then all the struct fiemap_extents */
3305 for (i = 0; i < fm->fm_mapped_extents; i++) {
3306 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3311 unlock_user(argptr, arg, target_size_out);
3321 static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
3322 int fd, abi_long cmd, abi_long arg)
3324 const argtype *arg_type = ie->arg_type;
3328 struct ifconf *host_ifconf;
3330 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
3331 int target_ifreq_size;
3336 abi_long target_ifc_buf;
3340 assert(arg_type[0] == TYPE_PTR);
3341 assert(ie->access == IOC_RW);
3344 target_size = thunk_type_size(arg_type, 0);
3346 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3348 return -TARGET_EFAULT;
3349 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3350 unlock_user(argptr, arg, 0);
3352 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
3353 target_ifc_len = host_ifconf->ifc_len;
3354 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
3356 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
3357 nb_ifreq = target_ifc_len / target_ifreq_size;
3358 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
3360 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
3361 if (outbufsz > MAX_STRUCT_SIZE) {
3362 /* We can't fit all the extents into the fixed size buffer.
3363 * Allocate one that is large enough and use it instead.
3365 host_ifconf = malloc(outbufsz);
3367 return -TARGET_ENOMEM;
3369 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
3372 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
3374 host_ifconf->ifc_len = host_ifc_len;
3375 host_ifconf->ifc_buf = host_ifc_buf;
3377 ret = get_errno(ioctl(fd, ie->host_cmd, host_ifconf));
3378 if (!is_error(ret)) {
3379 /* convert host ifc_len to target ifc_len */
3381 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
3382 target_ifc_len = nb_ifreq * target_ifreq_size;
3383 host_ifconf->ifc_len = target_ifc_len;
3385 /* restore target ifc_buf */
3387 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
3389 /* copy struct ifconf to target user */
3391 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3393 return -TARGET_EFAULT;
3394 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
3395 unlock_user(argptr, arg, target_size);
3397 /* copy ifreq[] to target user */
3399 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
3400 for (i = 0; i < nb_ifreq ; i++) {
3401 thunk_convert(argptr + i * target_ifreq_size,
3402 host_ifc_buf + i * sizeof(struct ifreq),
3403 ifreq_arg_type, THUNK_TARGET);
3405 unlock_user(argptr, target_ifc_buf, target_ifc_len);
3415 static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
3416 abi_long cmd, abi_long arg)
3419 struct dm_ioctl *host_dm;
3420 abi_long guest_data;
3421 uint32_t guest_data_size;
3423 const argtype *arg_type = ie->arg_type;
3425 void *big_buf = NULL;
3429 target_size = thunk_type_size(arg_type, 0);
3430 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3432 ret = -TARGET_EFAULT;
3435 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3436 unlock_user(argptr, arg, 0);
3438 /* buf_temp is too small, so fetch things into a bigger buffer */
3439 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
3440 memcpy(big_buf, buf_temp, target_size);
3444 guest_data = arg + host_dm->data_start;
3445 if ((guest_data - arg) < 0) {
3449 guest_data_size = host_dm->data_size - host_dm->data_start;
3450 host_data = (char*)host_dm + host_dm->data_start;
3452 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3453 switch (ie->host_cmd) {
3455 case DM_LIST_DEVICES:
3458 case DM_DEV_SUSPEND:
3461 case DM_TABLE_STATUS:
3462 case DM_TABLE_CLEAR:
3464 case DM_LIST_VERSIONS:
3468 case DM_DEV_SET_GEOMETRY:
3469 /* data contains only strings */
3470 memcpy(host_data, argptr, guest_data_size);
3473 memcpy(host_data, argptr, guest_data_size);
3474 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
3478 void *gspec = argptr;
3479 void *cur_data = host_data;
3480 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3481 int spec_size = thunk_type_size(arg_type, 0);
3484 for (i = 0; i < host_dm->target_count; i++) {
3485 struct dm_target_spec *spec = cur_data;
3489 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
3490 slen = strlen((char*)gspec + spec_size) + 1;
3492 spec->next = sizeof(*spec) + slen;
3493 strcpy((char*)&spec[1], gspec + spec_size);
3495 cur_data += spec->next;
3500 ret = -TARGET_EINVAL;
3503 unlock_user(argptr, guest_data, 0);
3505 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3506 if (!is_error(ret)) {
3507 guest_data = arg + host_dm->data_start;
3508 guest_data_size = host_dm->data_size - host_dm->data_start;
3509 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
3510 switch (ie->host_cmd) {
3515 case DM_DEV_SUSPEND:
3518 case DM_TABLE_CLEAR:
3520 case DM_DEV_SET_GEOMETRY:
3521 /* no return data */
3523 case DM_LIST_DEVICES:
3525 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
3526 uint32_t remaining_data = guest_data_size;
3527 void *cur_data = argptr;
3528 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
3529 int nl_size = 12; /* can't use thunk_size due to alignment */
3532 uint32_t next = nl->next;
3534 nl->next = nl_size + (strlen(nl->name) + 1);
3536 if (remaining_data < nl->next) {
3537 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3540 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
3541 strcpy(cur_data + nl_size, nl->name);
3542 cur_data += nl->next;
3543 remaining_data -= nl->next;
3547 nl = (void*)nl + next;
3552 case DM_TABLE_STATUS:
3554 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
3555 void *cur_data = argptr;
3556 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
3557 int spec_size = thunk_type_size(arg_type, 0);
3560 for (i = 0; i < host_dm->target_count; i++) {
3561 uint32_t next = spec->next;
3562 int slen = strlen((char*)&spec[1]) + 1;
3563 spec->next = (cur_data - argptr) + spec_size + slen;
3564 if (guest_data_size < spec->next) {
3565 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3568 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
3569 strcpy(cur_data + spec_size, (char*)&spec[1]);
3570 cur_data = argptr + spec->next;
3571 spec = (void*)host_dm + host_dm->data_start + next;
3577 void *hdata = (void*)host_dm + host_dm->data_start;
3578 int count = *(uint32_t*)hdata;
3579 uint64_t *hdev = hdata + 8;
3580 uint64_t *gdev = argptr + 8;
3583 *(uint32_t*)argptr = tswap32(count);
3584 for (i = 0; i < count; i++) {
3585 *gdev = tswap64(*hdev);
3591 case DM_LIST_VERSIONS:
3593 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
3594 uint32_t remaining_data = guest_data_size;
3595 void *cur_data = argptr;
3596 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
3597 int vers_size = thunk_type_size(arg_type, 0);
3600 uint32_t next = vers->next;
3602 vers->next = vers_size + (strlen(vers->name) + 1);
3604 if (remaining_data < vers->next) {
3605 host_dm->flags |= DM_BUFFER_FULL_FLAG;
3608 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
3609 strcpy(cur_data + vers_size, vers->name);
3610 cur_data += vers->next;
3611 remaining_data -= vers->next;
3615 vers = (void*)vers + next;
3620 ret = -TARGET_EINVAL;
3623 unlock_user(argptr, guest_data, guest_data_size);
3625 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3627 ret = -TARGET_EFAULT;
3630 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3631 unlock_user(argptr, arg, target_size);
3638 static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
3639 int fd, abi_long cmd, abi_long arg)
3641 const argtype *arg_type = ie->arg_type;
3642 const StructEntry *se;
3643 const argtype *field_types;
3644 const int *dst_offsets, *src_offsets;
3647 abi_ulong *target_rt_dev_ptr;
3648 unsigned long *host_rt_dev_ptr;
3652 assert(ie->access == IOC_W);
3653 assert(*arg_type == TYPE_PTR);
3655 assert(*arg_type == TYPE_STRUCT);
3656 target_size = thunk_type_size(arg_type, 0);
3657 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3659 return -TARGET_EFAULT;
3662 assert(*arg_type == (int)STRUCT_rtentry);
3663 se = struct_entries + *arg_type++;
3664 assert(se->convert[0] == NULL);
3665 /* convert struct here to be able to catch rt_dev string */
3666 field_types = se->field_types;
3667 dst_offsets = se->field_offsets[THUNK_HOST];
3668 src_offsets = se->field_offsets[THUNK_TARGET];
3669 for (i = 0; i < se->nb_fields; i++) {
3670 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
3671 assert(*field_types == TYPE_PTRVOID);
3672 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
3673 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
3674 if (*target_rt_dev_ptr != 0) {
3675 *host_rt_dev_ptr = (unsigned long)lock_user_string(
3676 tswapal(*target_rt_dev_ptr));
3677 if (!*host_rt_dev_ptr) {
3678 unlock_user(argptr, arg, 0);
3679 return -TARGET_EFAULT;
3682 *host_rt_dev_ptr = 0;
3687 field_types = thunk_convert(buf_temp + dst_offsets[i],
3688 argptr + src_offsets[i],
3689 field_types, THUNK_HOST);
3691 unlock_user(argptr, arg, 0);
3693 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3694 if (*host_rt_dev_ptr != 0) {
3695 unlock_user((void *)*host_rt_dev_ptr,
3696 *target_rt_dev_ptr, 0);
3701 static IOCTLEntry ioctl_entries[] = {
3702 #define IOCTL(cmd, access, ...) \
3703 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3704 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3705 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
3710 /* ??? Implement proper locking for ioctls. */
3711 /* do_ioctl() Must return target values and target errnos. */
3712 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
3714 const IOCTLEntry *ie;
3715 const argtype *arg_type;
3717 uint8_t buf_temp[MAX_STRUCT_SIZE];
3723 if (ie->target_cmd == 0) {
3724 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
3725 return -TARGET_ENOSYS;
3727 if (ie->target_cmd == cmd)
3731 arg_type = ie->arg_type;
3733 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
3736 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3739 switch(arg_type[0]) {
3742 ret = get_errno(ioctl(fd, ie->host_cmd));
3747 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3751 target_size = thunk_type_size(arg_type, 0);
3752 switch(ie->access) {
3754 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3755 if (!is_error(ret)) {
3756 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3758 return -TARGET_EFAULT;
3759 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3760 unlock_user(argptr, arg, target_size);
3764 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3766 return -TARGET_EFAULT;
3767 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3768 unlock_user(argptr, arg, 0);
3769 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3773 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3775 return -TARGET_EFAULT;
3776 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3777 unlock_user(argptr, arg, 0);
3778 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3779 if (!is_error(ret)) {
3780 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3782 return -TARGET_EFAULT;
3783 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3784 unlock_user(argptr, arg, target_size);
3790 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3791 (long)cmd, arg_type[0]);
3792 ret = -TARGET_ENOSYS;
3798 static const bitmask_transtbl iflag_tbl[] = {
3799 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3800 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3801 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3802 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3803 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3804 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3805 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3806 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3807 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3808 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3809 { TARGET_IXON, TARGET_IXON, IXON, IXON },
3810 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3811 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3812 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3816 static const bitmask_transtbl oflag_tbl[] = {
3817 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3818 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3819 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3820 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3821 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3822 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3823 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3824 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3825 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3826 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3827 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3828 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3829 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3830 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3831 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3832 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3833 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3834 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3835 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3836 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3837 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3838 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3839 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3840 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3844 static const bitmask_transtbl cflag_tbl[] = {
3845 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3846 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3847 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3848 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3849 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3850 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3851 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3852 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3853 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3854 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3855 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3856 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3857 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3858 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3859 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3860 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3861 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3862 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3863 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3864 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3865 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3866 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3867 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3868 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3869 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3870 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3871 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3872 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3873 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3874 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3875 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3879 static const bitmask_transtbl lflag_tbl[] = {
3880 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3881 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3882 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3883 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3884 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3885 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3886 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3887 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3888 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3889 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3890 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3891 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3892 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3893 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3894 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3898 static void target_to_host_termios (void *dst, const void *src)
3900 struct host_termios *host = dst;
3901 const struct target_termios *target = src;
3904 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
3906 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
3908 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
3910 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3911 host->c_line = target->c_line;
3913 memset(host->c_cc, 0, sizeof(host->c_cc));
3914 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3915 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3916 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
3917 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3918 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
3919 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3920 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
3921 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3922 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
3923 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3924 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3925 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3926 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3927 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3928 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3929 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
3930 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
3933 static void host_to_target_termios (void *dst, const void *src)
3935 struct target_termios *target = dst;
3936 const struct host_termios *host = src;
3939 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
3941 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
3943 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
3945 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3946 target->c_line = host->c_line;
3948 memset(target->c_cc, 0, sizeof(target->c_cc));
3949 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3950 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3951 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3952 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3953 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3954 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3955 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3956 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3957 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3958 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3959 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3960 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3961 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3962 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3963 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3964 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3965 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3968 static const StructEntry struct_termios_def = {
3969 .convert = { host_to_target_termios, target_to_host_termios },
3970 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3971 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3974 static bitmask_transtbl mmap_flags_tbl[] = {
3975 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3976 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3977 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3978 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3979 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3980 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3981 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3982 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3986 #if defined(TARGET_I386)
3988 /* NOTE: there is really one LDT for all the threads */
3989 static uint8_t *ldt_table;
3991 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
3998 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3999 if (size > bytecount)
4001 p = lock_user(VERIFY_WRITE, ptr, size, 0);
4003 return -TARGET_EFAULT;
4004 /* ??? Should this by byteswapped? */
4005 memcpy(p, ldt_table, size);
4006 unlock_user(p, ptr, size);
4010 /* XXX: add locking support */
4011 static abi_long write_ldt(CPUX86State *env,
4012 abi_ulong ptr, unsigned long bytecount, int oldmode)
4014 struct target_modify_ldt_ldt_s ldt_info;
4015 struct target_modify_ldt_ldt_s *target_ldt_info;
4016 int seg_32bit, contents, read_exec_only, limit_in_pages;
4017 int seg_not_present, useable, lm;
4018 uint32_t *lp, entry_1, entry_2;
4020 if (bytecount != sizeof(ldt_info))
4021 return -TARGET_EINVAL;
4022 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
4023 return -TARGET_EFAULT;
4024 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
4025 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
4026 ldt_info.limit = tswap32(target_ldt_info->limit);
4027 ldt_info.flags = tswap32(target_ldt_info->flags);
4028 unlock_user_struct(target_ldt_info, ptr, 0);
4030 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
4031 return -TARGET_EINVAL;
4032 seg_32bit = ldt_info.flags & 1;
4033 contents = (ldt_info.flags >> 1) & 3;
4034 read_exec_only = (ldt_info.flags >> 3) & 1;
4035 limit_in_pages = (ldt_info.flags >> 4) & 1;
4036 seg_not_present = (ldt_info.flags >> 5) & 1;
4037 useable = (ldt_info.flags >> 6) & 1;
4041 lm = (ldt_info.flags >> 7) & 1;
4043 if (contents == 3) {
4045 return -TARGET_EINVAL;
4046 if (seg_not_present == 0)
4047 return -TARGET_EINVAL;
4049 /* allocate the LDT */
4051 env->ldt.base = target_mmap(0,
4052 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
4053 PROT_READ|PROT_WRITE,
4054 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
4055 if (env->ldt.base == -1)
4056 return -TARGET_ENOMEM;
4057 memset(g2h(env->ldt.base), 0,
4058 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
4059 env->ldt.limit = 0xffff;
4060 ldt_table = g2h(env->ldt.base);
4063 /* NOTE: same code as Linux kernel */
4064 /* Allow LDTs to be cleared by the user. */
4065 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4068 read_exec_only == 1 &&
4070 limit_in_pages == 0 &&
4071 seg_not_present == 1 &&
4079 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4080 (ldt_info.limit & 0x0ffff);
4081 entry_2 = (ldt_info.base_addr & 0xff000000) |
4082 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4083 (ldt_info.limit & 0xf0000) |
4084 ((read_exec_only ^ 1) << 9) |
4086 ((seg_not_present ^ 1) << 15) |
4088 (limit_in_pages << 23) |
4092 entry_2 |= (useable << 20);
4094 /* Install the new entry ... */
4096 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
4097 lp[0] = tswap32(entry_1);
4098 lp[1] = tswap32(entry_2);
4102 /* specific and weird i386 syscalls */
4103 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
4104 unsigned long bytecount)
4110 ret = read_ldt(ptr, bytecount);
4113 ret = write_ldt(env, ptr, bytecount, 1);
4116 ret = write_ldt(env, ptr, bytecount, 0);
4119 ret = -TARGET_ENOSYS;
4125 #if defined(TARGET_I386) && defined(TARGET_ABI32)
4126 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
4128 uint64_t *gdt_table = g2h(env->gdt.base);
4129 struct target_modify_ldt_ldt_s ldt_info;
4130 struct target_modify_ldt_ldt_s *target_ldt_info;
4131 int seg_32bit, contents, read_exec_only, limit_in_pages;
4132 int seg_not_present, useable, lm;
4133 uint32_t *lp, entry_1, entry_2;
4136 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4137 if (!target_ldt_info)
4138 return -TARGET_EFAULT;
4139 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
4140 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
4141 ldt_info.limit = tswap32(target_ldt_info->limit);
4142 ldt_info.flags = tswap32(target_ldt_info->flags);
4143 if (ldt_info.entry_number == -1) {
4144 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
4145 if (gdt_table[i] == 0) {
4146 ldt_info.entry_number = i;
4147 target_ldt_info->entry_number = tswap32(i);
4152 unlock_user_struct(target_ldt_info, ptr, 1);
4154 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
4155 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
4156 return -TARGET_EINVAL;
4157 seg_32bit = ldt_info.flags & 1;
4158 contents = (ldt_info.flags >> 1) & 3;
4159 read_exec_only = (ldt_info.flags >> 3) & 1;
4160 limit_in_pages = (ldt_info.flags >> 4) & 1;
4161 seg_not_present = (ldt_info.flags >> 5) & 1;
4162 useable = (ldt_info.flags >> 6) & 1;
4166 lm = (ldt_info.flags >> 7) & 1;
4169 if (contents == 3) {
4170 if (seg_not_present == 0)
4171 return -TARGET_EINVAL;
4174 /* NOTE: same code as Linux kernel */
4175 /* Allow LDTs to be cleared by the user. */
4176 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
4177 if ((contents == 0 &&
4178 read_exec_only == 1 &&
4180 limit_in_pages == 0 &&
4181 seg_not_present == 1 &&
4189 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
4190 (ldt_info.limit & 0x0ffff);
4191 entry_2 = (ldt_info.base_addr & 0xff000000) |
4192 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
4193 (ldt_info.limit & 0xf0000) |
4194 ((read_exec_only ^ 1) << 9) |
4196 ((seg_not_present ^ 1) << 15) |
4198 (limit_in_pages << 23) |
4203 /* Install the new entry ... */
4205 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
4206 lp[0] = tswap32(entry_1);
4207 lp[1] = tswap32(entry_2);
4211 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
4213 struct target_modify_ldt_ldt_s *target_ldt_info;
4214 uint64_t *gdt_table = g2h(env->gdt.base);
4215 uint32_t base_addr, limit, flags;
4216 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
4217 int seg_not_present, useable, lm;
4218 uint32_t *lp, entry_1, entry_2;
4220 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
4221 if (!target_ldt_info)
4222 return -TARGET_EFAULT;
4223 idx = tswap32(target_ldt_info->entry_number);
4224 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
4225 idx > TARGET_GDT_ENTRY_TLS_MAX) {
4226 unlock_user_struct(target_ldt_info, ptr, 1);
4227 return -TARGET_EINVAL;
4229 lp = (uint32_t *)(gdt_table + idx);
4230 entry_1 = tswap32(lp[0]);
4231 entry_2 = tswap32(lp[1]);
4233 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
4234 contents = (entry_2 >> 10) & 3;
4235 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
4236 seg_32bit = (entry_2 >> 22) & 1;
4237 limit_in_pages = (entry_2 >> 23) & 1;
4238 useable = (entry_2 >> 20) & 1;
4242 lm = (entry_2 >> 21) & 1;
4244 flags = (seg_32bit << 0) | (contents << 1) |
4245 (read_exec_only << 3) | (limit_in_pages << 4) |
4246 (seg_not_present << 5) | (useable << 6) | (lm << 7);
4247 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
4248 base_addr = (entry_1 >> 16) |
4249 (entry_2 & 0xff000000) |
4250 ((entry_2 & 0xff) << 16);
4251 target_ldt_info->base_addr = tswapal(base_addr);
4252 target_ldt_info->limit = tswap32(limit);
4253 target_ldt_info->flags = tswap32(flags);
4254 unlock_user_struct(target_ldt_info, ptr, 1);
4257 #endif /* TARGET_I386 && TARGET_ABI32 */
4259 #ifndef TARGET_ABI32
4260 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
4267 case TARGET_ARCH_SET_GS:
4268 case TARGET_ARCH_SET_FS:
4269 if (code == TARGET_ARCH_SET_GS)
4273 cpu_x86_load_seg(env, idx, 0);
4274 env->segs[idx].base = addr;
4276 case TARGET_ARCH_GET_GS:
4277 case TARGET_ARCH_GET_FS:
4278 if (code == TARGET_ARCH_GET_GS)
4282 val = env->segs[idx].base;
4283 if (put_user(val, addr, abi_ulong))
4284 ret = -TARGET_EFAULT;
4287 ret = -TARGET_EINVAL;
4294 #endif /* defined(TARGET_I386) */
4296 #define NEW_STACK_SIZE 0x40000
4299 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
4302 pthread_mutex_t mutex;
4303 pthread_cond_t cond;
4306 abi_ulong child_tidptr;
4307 abi_ulong parent_tidptr;
4311 static void *clone_func(void *arg)
4313 new_thread_info *info = arg;
4319 cpu = ENV_GET_CPU(env);
4321 ts = (TaskState *)env->opaque;
4322 info->tid = gettid();
4323 cpu->host_tid = info->tid;
4325 if (info->child_tidptr)
4326 put_user_u32(info->tid, info->child_tidptr);
4327 if (info->parent_tidptr)
4328 put_user_u32(info->tid, info->parent_tidptr);
4329 /* Enable signals. */
4330 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
4331 /* Signal to the parent that we're ready. */
4332 pthread_mutex_lock(&info->mutex);
4333 pthread_cond_broadcast(&info->cond);
4334 pthread_mutex_unlock(&info->mutex);
4335 /* Wait until the parent has finshed initializing the tls state. */
4336 pthread_mutex_lock(&clone_lock);
4337 pthread_mutex_unlock(&clone_lock);
4343 /* do_fork() Must return host values and target errnos (unlike most
4344 do_*() functions). */
4345 static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
4346 abi_ulong parent_tidptr, target_ulong newtls,
4347 abi_ulong child_tidptr)
4351 CPUArchState *new_env;
4352 unsigned int nptl_flags;
4355 /* Emulate vfork() with fork() */
4356 if (flags & CLONE_VFORK)
4357 flags &= ~(CLONE_VFORK | CLONE_VM);
4359 if (flags & CLONE_VM) {
4360 TaskState *parent_ts = (TaskState *)env->opaque;
4361 new_thread_info info;
4362 pthread_attr_t attr;
4364 ts = g_malloc0(sizeof(TaskState));
4365 init_task_state(ts);
4366 /* we create a new CPU instance. */
4367 new_env = cpu_copy(env);
4368 /* Init regs that differ from the parent. */
4369 cpu_clone_regs(new_env, newsp);
4370 new_env->opaque = ts;
4371 ts->bprm = parent_ts->bprm;
4372 ts->info = parent_ts->info;
4374 flags &= ~CLONE_NPTL_FLAGS2;
4376 if (nptl_flags & CLONE_CHILD_CLEARTID) {
4377 ts->child_tidptr = child_tidptr;
4380 if (nptl_flags & CLONE_SETTLS)
4381 cpu_set_tls (new_env, newtls);
4383 /* Grab a mutex so that thread setup appears atomic. */
4384 pthread_mutex_lock(&clone_lock);
4386 memset(&info, 0, sizeof(info));
4387 pthread_mutex_init(&info.mutex, NULL);
4388 pthread_mutex_lock(&info.mutex);
4389 pthread_cond_init(&info.cond, NULL);
4391 if (nptl_flags & CLONE_CHILD_SETTID)
4392 info.child_tidptr = child_tidptr;
4393 if (nptl_flags & CLONE_PARENT_SETTID)
4394 info.parent_tidptr = parent_tidptr;
4396 ret = pthread_attr_init(&attr);
4397 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
4398 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
4399 /* It is not safe to deliver signals until the child has finished
4400 initializing, so temporarily block all signals. */
4401 sigfillset(&sigmask);
4402 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
4404 ret = pthread_create(&info.thread, &attr, clone_func, &info);
4405 /* TODO: Free new CPU state if thread creation failed. */
4407 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
4408 pthread_attr_destroy(&attr);
4410 /* Wait for the child to initialize. */
4411 pthread_cond_wait(&info.cond, &info.mutex);
4413 if (flags & CLONE_PARENT_SETTID)
4414 put_user_u32(ret, parent_tidptr);
4418 pthread_mutex_unlock(&info.mutex);
4419 pthread_cond_destroy(&info.cond);
4420 pthread_mutex_destroy(&info.mutex);
4421 pthread_mutex_unlock(&clone_lock);
4423 /* if no CLONE_VM, we consider it is a fork */
4424 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
4429 /* Child Process. */
4430 cpu_clone_regs(env, newsp);
4432 /* There is a race condition here. The parent process could
4433 theoretically read the TID in the child process before the child
4434 tid is set. This would require using either ptrace
4435 (not implemented) or having *_tidptr to point at a shared memory
4436 mapping. We can't repeat the spinlock hack used above because
4437 the child process gets its own copy of the lock. */
4438 if (flags & CLONE_CHILD_SETTID)
4439 put_user_u32(gettid(), child_tidptr);
4440 if (flags & CLONE_PARENT_SETTID)
4441 put_user_u32(gettid(), parent_tidptr);
4442 ts = (TaskState *)env->opaque;
4443 if (flags & CLONE_SETTLS)
4444 cpu_set_tls (env, newtls);
4445 if (flags & CLONE_CHILD_CLEARTID)
4446 ts->child_tidptr = child_tidptr;
4454 /* warning : doesn't handle linux specific flags... */
4455 static int target_to_host_fcntl_cmd(int cmd)
4458 case TARGET_F_DUPFD:
4459 case TARGET_F_GETFD:
4460 case TARGET_F_SETFD:
4461 case TARGET_F_GETFL:
4462 case TARGET_F_SETFL:
4464 case TARGET_F_GETLK:
4466 case TARGET_F_SETLK:
4468 case TARGET_F_SETLKW:
4470 case TARGET_F_GETOWN:
4472 case TARGET_F_SETOWN:
4474 case TARGET_F_GETSIG:
4476 case TARGET_F_SETSIG:
4478 #if TARGET_ABI_BITS == 32
4479 case TARGET_F_GETLK64:
4481 case TARGET_F_SETLK64:
4483 case TARGET_F_SETLKW64:
4486 case TARGET_F_SETLEASE:
4488 case TARGET_F_GETLEASE:
4490 #ifdef F_DUPFD_CLOEXEC
4491 case TARGET_F_DUPFD_CLOEXEC:
4492 return F_DUPFD_CLOEXEC;
4494 case TARGET_F_NOTIFY:
4497 return -TARGET_EINVAL;
4499 return -TARGET_EINVAL;
4502 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4503 static const bitmask_transtbl flock_tbl[] = {
4504 TRANSTBL_CONVERT(F_RDLCK),
4505 TRANSTBL_CONVERT(F_WRLCK),
4506 TRANSTBL_CONVERT(F_UNLCK),
4507 TRANSTBL_CONVERT(F_EXLCK),
4508 TRANSTBL_CONVERT(F_SHLCK),
4512 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
4515 struct target_flock *target_fl;
4516 struct flock64 fl64;
4517 struct target_flock64 *target_fl64;
4519 int host_cmd = target_to_host_fcntl_cmd(cmd);
4521 if (host_cmd == -TARGET_EINVAL)
4525 case TARGET_F_GETLK:
4526 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4527 return -TARGET_EFAULT;
4529 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
4530 fl.l_whence = tswap16(target_fl->l_whence);
4531 fl.l_start = tswapal(target_fl->l_start);
4532 fl.l_len = tswapal(target_fl->l_len);
4533 fl.l_pid = tswap32(target_fl->l_pid);
4534 unlock_user_struct(target_fl, arg, 0);
4535 ret = get_errno(fcntl(fd, host_cmd, &fl));
4537 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
4538 return -TARGET_EFAULT;
4540 host_to_target_bitmask(tswap16(fl.l_type), flock_tbl);
4541 target_fl->l_whence = tswap16(fl.l_whence);
4542 target_fl->l_start = tswapal(fl.l_start);
4543 target_fl->l_len = tswapal(fl.l_len);
4544 target_fl->l_pid = tswap32(fl.l_pid);
4545 unlock_user_struct(target_fl, arg, 1);
4549 case TARGET_F_SETLK:
4550 case TARGET_F_SETLKW:
4551 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
4552 return -TARGET_EFAULT;
4554 target_to_host_bitmask(tswap16(target_fl->l_type), flock_tbl);
4555 fl.l_whence = tswap16(target_fl->l_whence);
4556 fl.l_start = tswapal(target_fl->l_start);
4557 fl.l_len = tswapal(target_fl->l_len);
4558 fl.l_pid = tswap32(target_fl->l_pid);
4559 unlock_user_struct(target_fl, arg, 0);
4560 ret = get_errno(fcntl(fd, host_cmd, &fl));
4563 case TARGET_F_GETLK64:
4564 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4565 return -TARGET_EFAULT;
4567 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
4568 fl64.l_whence = tswap16(target_fl64->l_whence);
4569 fl64.l_start = tswap64(target_fl64->l_start);
4570 fl64.l_len = tswap64(target_fl64->l_len);
4571 fl64.l_pid = tswap32(target_fl64->l_pid);
4572 unlock_user_struct(target_fl64, arg, 0);
4573 ret = get_errno(fcntl(fd, host_cmd, &fl64));
4575 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
4576 return -TARGET_EFAULT;
4577 target_fl64->l_type =
4578 host_to_target_bitmask(tswap16(fl64.l_type), flock_tbl) >> 1;
4579 target_fl64->l_whence = tswap16(fl64.l_whence);
4580 target_fl64->l_start = tswap64(fl64.l_start);
4581 target_fl64->l_len = tswap64(fl64.l_len);
4582 target_fl64->l_pid = tswap32(fl64.l_pid);
4583 unlock_user_struct(target_fl64, arg, 1);
4586 case TARGET_F_SETLK64:
4587 case TARGET_F_SETLKW64:
4588 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
4589 return -TARGET_EFAULT;
4591 target_to_host_bitmask(tswap16(target_fl64->l_type), flock_tbl) >> 1;
4592 fl64.l_whence = tswap16(target_fl64->l_whence);
4593 fl64.l_start = tswap64(target_fl64->l_start);
4594 fl64.l_len = tswap64(target_fl64->l_len);
4595 fl64.l_pid = tswap32(target_fl64->l_pid);
4596 unlock_user_struct(target_fl64, arg, 0);
4597 ret = get_errno(fcntl(fd, host_cmd, &fl64));
4600 case TARGET_F_GETFL:
4601 ret = get_errno(fcntl(fd, host_cmd, arg));
4603 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
4607 case TARGET_F_SETFL:
4608 ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4611 case TARGET_F_SETOWN:
4612 case TARGET_F_GETOWN:
4613 case TARGET_F_SETSIG:
4614 case TARGET_F_GETSIG:
4615 case TARGET_F_SETLEASE:
4616 case TARGET_F_GETLEASE:
4617 ret = get_errno(fcntl(fd, host_cmd, arg));
4621 ret = get_errno(fcntl(fd, cmd, arg));
4629 static inline int high2lowuid(int uid)
4637 static inline int high2lowgid(int gid)
4645 static inline int low2highuid(int uid)
4647 if ((int16_t)uid == -1)
4653 static inline int low2highgid(int gid)
4655 if ((int16_t)gid == -1)
4660 static inline int tswapid(int id)
4664 #else /* !USE_UID16 */
4665 static inline int high2lowuid(int uid)
4669 static inline int high2lowgid(int gid)
4673 static inline int low2highuid(int uid)
4677 static inline int low2highgid(int gid)
4681 static inline int tswapid(int id)
4685 #endif /* USE_UID16 */
4687 void syscall_init(void)
4690 const argtype *arg_type;
4694 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
4695 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
4696 #include "syscall_types.h"
4698 #undef STRUCT_SPECIAL
4700 /* Build target_to_host_errno_table[] table from
4701 * host_to_target_errno_table[]. */
4702 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
4703 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4706 /* we patch the ioctl size if necessary. We rely on the fact that
4707 no ioctl has all the bits at '1' in the size field */
4709 while (ie->target_cmd != 0) {
4710 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4711 TARGET_IOC_SIZEMASK) {
4712 arg_type = ie->arg_type;
4713 if (arg_type[0] != TYPE_PTR) {
4714 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
4719 size = thunk_type_size(arg_type, 0);
4720 ie->target_cmd = (ie->target_cmd &
4721 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4722 (size << TARGET_IOC_SIZESHIFT);
4725 /* automatic consistency check if same arch */
4726 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4727 (defined(__x86_64__) && defined(TARGET_X86_64))
4728 if (unlikely(ie->target_cmd != ie->host_cmd)) {
4729 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4730 ie->name, ie->target_cmd, ie->host_cmd);
4737 #if TARGET_ABI_BITS == 32
4738 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4740 #ifdef TARGET_WORDS_BIGENDIAN
4741 return ((uint64_t)word0 << 32) | word1;
4743 return ((uint64_t)word1 << 32) | word0;
4746 #else /* TARGET_ABI_BITS == 32 */
4747 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4751 #endif /* TARGET_ABI_BITS != 32 */
4753 #ifdef TARGET_NR_truncate64
4754 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4759 if (regpairs_aligned(cpu_env)) {
4763 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4767 #ifdef TARGET_NR_ftruncate64
4768 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4773 if (regpairs_aligned(cpu_env)) {
4777 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4781 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4782 abi_ulong target_addr)
4784 struct target_timespec *target_ts;
4786 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4787 return -TARGET_EFAULT;
4788 host_ts->tv_sec = tswapal(target_ts->tv_sec);
4789 host_ts->tv_nsec = tswapal(target_ts->tv_nsec);
4790 unlock_user_struct(target_ts, target_addr, 0);
4794 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4795 struct timespec *host_ts)
4797 struct target_timespec *target_ts;
4799 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4800 return -TARGET_EFAULT;
4801 target_ts->tv_sec = tswapal(host_ts->tv_sec);
4802 target_ts->tv_nsec = tswapal(host_ts->tv_nsec);
4803 unlock_user_struct(target_ts, target_addr, 1);
4807 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
4808 static inline abi_long host_to_target_stat64(void *cpu_env,
4809 abi_ulong target_addr,
4810 struct stat *host_st)
4812 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
4813 if (((CPUARMState *)cpu_env)->eabi) {
4814 struct target_eabi_stat64 *target_st;
4816 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4817 return -TARGET_EFAULT;
4818 memset(target_st, 0, sizeof(struct target_eabi_stat64));
4819 __put_user(host_st->st_dev, &target_st->st_dev);
4820 __put_user(host_st->st_ino, &target_st->st_ino);
4821 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4822 __put_user(host_st->st_ino, &target_st->__st_ino);
4824 __put_user(host_st->st_mode, &target_st->st_mode);
4825 __put_user(host_st->st_nlink, &target_st->st_nlink);
4826 __put_user(host_st->st_uid, &target_st->st_uid);
4827 __put_user(host_st->st_gid, &target_st->st_gid);
4828 __put_user(host_st->st_rdev, &target_st->st_rdev);
4829 __put_user(host_st->st_size, &target_st->st_size);
4830 __put_user(host_st->st_blksize, &target_st->st_blksize);
4831 __put_user(host_st->st_blocks, &target_st->st_blocks);
4832 __put_user(host_st->st_atime, &target_st->target_st_atime);
4833 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4834 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4835 unlock_user_struct(target_st, target_addr, 1);
4839 #if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
4840 struct target_stat *target_st;
4842 struct target_stat64 *target_st;
4845 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4846 return -TARGET_EFAULT;
4847 memset(target_st, 0, sizeof(*target_st));
4848 __put_user(host_st->st_dev, &target_st->st_dev);
4849 __put_user(host_st->st_ino, &target_st->st_ino);
4850 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4851 __put_user(host_st->st_ino, &target_st->__st_ino);
4853 __put_user(host_st->st_mode, &target_st->st_mode);
4854 __put_user(host_st->st_nlink, &target_st->st_nlink);
4855 __put_user(host_st->st_uid, &target_st->st_uid);
4856 __put_user(host_st->st_gid, &target_st->st_gid);
4857 __put_user(host_st->st_rdev, &target_st->st_rdev);
4858 /* XXX: better use of kernel struct */
4859 __put_user(host_st->st_size, &target_st->st_size);
4860 __put_user(host_st->st_blksize, &target_st->st_blksize);
4861 __put_user(host_st->st_blocks, &target_st->st_blocks);
4862 __put_user(host_st->st_atime, &target_st->target_st_atime);
4863 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4864 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4865 unlock_user_struct(target_st, target_addr, 1);
4872 /* ??? Using host futex calls even when target atomic operations
4873 are not really atomic probably breaks things. However implementing
4874 futexes locally would make futexes shared between multiple processes
4875 tricky. However they're probably useless because guest atomic
4876 operations won't work either. */
4877 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4878 target_ulong uaddr2, int val3)
4880 struct timespec ts, *pts;
4883 /* ??? We assume FUTEX_* constants are the same on both host
4885 #ifdef FUTEX_CMD_MASK
4886 base_op = op & FUTEX_CMD_MASK;
4892 case FUTEX_WAIT_BITSET:
4895 target_to_host_timespec(pts, timeout);
4899 return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
4902 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4904 return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4906 case FUTEX_CMP_REQUEUE:
4908 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4909 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4910 But the prototype takes a `struct timespec *'; insert casts
4911 to satisfy the compiler. We do not need to tswap TIMEOUT
4912 since it's not compared to guest memory. */
4913 pts = (struct timespec *)(uintptr_t) timeout;
4914 return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4916 (base_op == FUTEX_CMP_REQUEUE
4920 return -TARGET_ENOSYS;
4924 /* Map host to target signal numbers for the wait family of syscalls.
4925 Assume all other status bits are the same. */
4926 int host_to_target_waitstatus(int status)
4928 if (WIFSIGNALED(status)) {
4929 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4931 if (WIFSTOPPED(status)) {
4932 return (host_to_target_signal(WSTOPSIG(status)) << 8)
4938 static int relstr_to_int(const char *s)
4940 /* Convert a uname release string like "2.6.18" to an integer
4941 * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
4946 for (i = 0; i < 3; i++) {
4948 while (*s >= '0' && *s <= '9') {
4953 tmp = (tmp << 8) + n;
4961 int get_osversion(void)
4963 static int osversion;
4964 struct new_utsname buf;
4969 if (qemu_uname_release && *qemu_uname_release) {
4970 s = qemu_uname_release;
4972 if (sys_uname(&buf))
4976 osversion = relstr_to_int(s);
4980 void init_qemu_uname_release(void)
4982 /* Initialize qemu_uname_release for later use.
4983 * If the host kernel is too old and the user hasn't asked for
4984 * a specific fake version number, we might want to fake a minimum
4985 * target kernel version.
4987 #ifdef UNAME_MINIMUM_RELEASE
4988 struct new_utsname buf;
4990 if (qemu_uname_release && *qemu_uname_release) {
4994 if (sys_uname(&buf)) {
4998 if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
4999 qemu_uname_release = UNAME_MINIMUM_RELEASE;
5004 static int open_self_maps(void *cpu_env, int fd)
5006 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
5007 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
5014 fp = fopen("/proc/self/maps", "r");
5019 while ((read = getline(&line, &len, fp)) != -1) {
5020 int fields, dev_maj, dev_min, inode;
5021 uint64_t min, max, offset;
5022 char flag_r, flag_w, flag_x, flag_p;
5023 char path[512] = "";
5024 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
5025 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
5026 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
5028 if ((fields < 10) || (fields > 11)) {
5031 if (!strncmp(path, "[stack]", 7)) {
5034 if (h2g_valid(min) && h2g_valid(max)) {
5035 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
5036 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
5037 h2g(min), h2g(max), flag_r, flag_w,
5038 flag_x, flag_p, offset, dev_maj, dev_min, inode,
5039 path[0] ? " " : "", path);
5046 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
5047 dprintf(fd, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
5048 (unsigned long long)ts->info->stack_limit,
5049 (unsigned long long)(ts->info->start_stack +
5050 (TARGET_PAGE_SIZE - 1)) & TARGET_PAGE_MASK,
5051 (unsigned long long)0);
5057 static int open_self_stat(void *cpu_env, int fd)
5059 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
5060 abi_ulong start_stack = ts->info->start_stack;
5063 for (i = 0; i < 44; i++) {
5071 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5072 } else if (i == 1) {
5074 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
5075 } else if (i == 27) {
5078 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
5080 /* for the rest, there is MasterCard */
5081 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
5085 if (write(fd, buf, len) != len) {
5093 static int open_self_auxv(void *cpu_env, int fd)
5095 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
5096 abi_ulong auxv = ts->info->saved_auxv;
5097 abi_ulong len = ts->info->auxv_len;
5101 * Auxiliary vector is stored in target process stack.
5102 * read in whole auxv vector and copy it to file
5104 ptr = lock_user(VERIFY_READ, auxv, len, 0);
5108 r = write(fd, ptr, len);
5115 lseek(fd, 0, SEEK_SET);
5116 unlock_user(ptr, auxv, len);
5122 static int is_proc_myself(const char *filename, const char *entry)
5124 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
5125 filename += strlen("/proc/");
5126 if (!strncmp(filename, "self/", strlen("self/"))) {
5127 filename += strlen("self/");
5128 } else if (*filename >= '1' && *filename <= '9') {
5130 snprintf(myself, sizeof(myself), "%d/", getpid());
5131 if (!strncmp(filename, myself, strlen(myself))) {
5132 filename += strlen(myself);
5139 if (!strcmp(filename, entry)) {
5146 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5147 static int is_proc(const char *filename, const char *entry)
5149 return strcmp(filename, entry) == 0;
5152 static int open_net_route(void *cpu_env, int fd)
5159 fp = fopen("/proc/net/route", "r");
5166 read = getline(&line, &len, fp);
5167 dprintf(fd, "%s", line);
5171 while ((read = getline(&line, &len, fp)) != -1) {
5173 uint32_t dest, gw, mask;
5174 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
5175 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5176 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
5177 &mask, &mtu, &window, &irtt);
5178 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5179 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
5180 metric, tswap32(mask), mtu, window, irtt);
5190 static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
5193 const char *filename;
5194 int (*fill)(void *cpu_env, int fd);
5195 int (*cmp)(const char *s1, const char *s2);
5197 const struct fake_open *fake_open;
5198 static const struct fake_open fakes[] = {
5199 { "maps", open_self_maps, is_proc_myself },
5200 { "stat", open_self_stat, is_proc_myself },
5201 { "auxv", open_self_auxv, is_proc_myself },
5202 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5203 { "/proc/net/route", open_net_route, is_proc },
5205 { NULL, NULL, NULL }
5208 for (fake_open = fakes; fake_open->filename; fake_open++) {
5209 if (fake_open->cmp(pathname, fake_open->filename)) {
5214 if (fake_open->filename) {
5216 char filename[PATH_MAX];
5219 /* create temporary file to map stat to */
5220 tmpdir = getenv("TMPDIR");
5223 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
5224 fd = mkstemp(filename);
5230 if ((r = fake_open->fill(cpu_env, fd))) {
5234 lseek(fd, 0, SEEK_SET);
5239 return get_errno(open(path(pathname), flags, mode));
5242 /* do_syscall() should always have a single exit point at the end so
5243 that actions, such as logging of syscall results, can be performed.
5244 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
5245 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
5246 abi_long arg2, abi_long arg3, abi_long arg4,
5247 abi_long arg5, abi_long arg6, abi_long arg7,
5250 CPUState *cpu = ENV_GET_CPU(cpu_env);
5257 gemu_log("syscall %d", num);
5260 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
5263 case TARGET_NR_exit:
5264 /* In old applications this may be used to implement _exit(2).
5265 However in threaded applictions it is used for thread termination,
5266 and _exit_group is used for application termination.
5267 Do thread termination if we have more then one thread. */
5268 /* FIXME: This probably breaks if a signal arrives. We should probably
5269 be disabling signals. */
5270 if (CPU_NEXT(first_cpu)) {
5274 /* Remove the CPU from the list. */
5275 QTAILQ_REMOVE(&cpus, cpu, node);
5277 ts = ((CPUArchState *)cpu_env)->opaque;
5278 if (ts->child_tidptr) {
5279 put_user_u32(0, ts->child_tidptr);
5280 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
5284 object_unref(OBJECT(ENV_GET_CPU(cpu_env)));
5291 gdb_exit(cpu_env, arg1);
5293 ret = 0; /* avoid warning */
5295 case TARGET_NR_read:
5299 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5301 ret = get_errno(read(arg1, p, arg3));
5302 unlock_user(p, arg2, ret);
5305 case TARGET_NR_write:
5306 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5308 ret = get_errno(write(arg1, p, arg3));
5309 unlock_user(p, arg2, 0);
5311 case TARGET_NR_open:
5312 if (!(p = lock_user_string(arg1)))
5314 ret = get_errno(do_open(cpu_env, p,
5315 target_to_host_bitmask(arg2, fcntl_flags_tbl),
5317 unlock_user(p, arg1, 0);
5319 #if defined(TARGET_NR_openat) && defined(__NR_openat)
5320 case TARGET_NR_openat:
5321 if (!(p = lock_user_string(arg2)))
5323 ret = get_errno(sys_openat(arg1,
5325 target_to_host_bitmask(arg3, fcntl_flags_tbl),
5327 unlock_user(p, arg2, 0);
5330 case TARGET_NR_close:
5331 ret = get_errno(close(arg1));
5336 case TARGET_NR_fork:
5337 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
5339 #ifdef TARGET_NR_waitpid
5340 case TARGET_NR_waitpid:
5343 ret = get_errno(waitpid(arg1, &status, arg3));
5344 if (!is_error(ret) && arg2 && ret
5345 && put_user_s32(host_to_target_waitstatus(status), arg2))
5350 #ifdef TARGET_NR_waitid
5351 case TARGET_NR_waitid:
5355 ret = get_errno(waitid(arg1, arg2, &info, arg4));
5356 if (!is_error(ret) && arg3 && info.si_pid != 0) {
5357 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
5359 host_to_target_siginfo(p, &info);
5360 unlock_user(p, arg3, sizeof(target_siginfo_t));
5365 #ifdef TARGET_NR_creat /* not on alpha */
5366 case TARGET_NR_creat:
5367 if (!(p = lock_user_string(arg1)))
5369 ret = get_errno(creat(p, arg2));
5370 unlock_user(p, arg1, 0);
5373 case TARGET_NR_link:
5376 p = lock_user_string(arg1);
5377 p2 = lock_user_string(arg2);
5379 ret = -TARGET_EFAULT;
5381 ret = get_errno(link(p, p2));
5382 unlock_user(p2, arg2, 0);
5383 unlock_user(p, arg1, 0);
5386 #if defined(TARGET_NR_linkat)
5387 case TARGET_NR_linkat:
5392 p = lock_user_string(arg2);
5393 p2 = lock_user_string(arg4);
5395 ret = -TARGET_EFAULT;
5397 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
5398 unlock_user(p, arg2, 0);
5399 unlock_user(p2, arg4, 0);
5403 case TARGET_NR_unlink:
5404 if (!(p = lock_user_string(arg1)))
5406 ret = get_errno(unlink(p));
5407 unlock_user(p, arg1, 0);
5409 #if defined(TARGET_NR_unlinkat)
5410 case TARGET_NR_unlinkat:
5411 if (!(p = lock_user_string(arg2)))
5413 ret = get_errno(unlinkat(arg1, p, arg3));
5414 unlock_user(p, arg2, 0);
5417 case TARGET_NR_execve:
5419 char **argp, **envp;
5422 abi_ulong guest_argp;
5423 abi_ulong guest_envp;
5430 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
5431 if (get_user_ual(addr, gp))
5439 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
5440 if (get_user_ual(addr, gp))
5447 argp = alloca((argc + 1) * sizeof(void *));
5448 envp = alloca((envc + 1) * sizeof(void *));
5450 for (gp = guest_argp, q = argp; gp;
5451 gp += sizeof(abi_ulong), q++) {
5452 if (get_user_ual(addr, gp))
5456 if (!(*q = lock_user_string(addr)))
5458 total_size += strlen(*q) + 1;
5462 for (gp = guest_envp, q = envp; gp;
5463 gp += sizeof(abi_ulong), q++) {
5464 if (get_user_ual(addr, gp))
5468 if (!(*q = lock_user_string(addr)))
5470 total_size += strlen(*q) + 1;
5474 /* This case will not be caught by the host's execve() if its
5475 page size is bigger than the target's. */
5476 if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
5477 ret = -TARGET_E2BIG;
5480 if (!(p = lock_user_string(arg1)))
5482 ret = get_errno(execve(p, argp, envp));
5483 unlock_user(p, arg1, 0);
5488 ret = -TARGET_EFAULT;
5491 for (gp = guest_argp, q = argp; *q;
5492 gp += sizeof(abi_ulong), q++) {
5493 if (get_user_ual(addr, gp)
5496 unlock_user(*q, addr, 0);
5498 for (gp = guest_envp, q = envp; *q;
5499 gp += sizeof(abi_ulong), q++) {
5500 if (get_user_ual(addr, gp)
5503 unlock_user(*q, addr, 0);
5507 case TARGET_NR_chdir:
5508 if (!(p = lock_user_string(arg1)))
5510 ret = get_errno(chdir(p));
5511 unlock_user(p, arg1, 0);
5513 #ifdef TARGET_NR_time
5514 case TARGET_NR_time:
5517 ret = get_errno(time(&host_time));
5520 && put_user_sal(host_time, arg1))
5525 case TARGET_NR_mknod:
5526 if (!(p = lock_user_string(arg1)))
5528 ret = get_errno(mknod(p, arg2, arg3));
5529 unlock_user(p, arg1, 0);
5531 #if defined(TARGET_NR_mknodat)
5532 case TARGET_NR_mknodat:
5533 if (!(p = lock_user_string(arg2)))
5535 ret = get_errno(mknodat(arg1, p, arg3, arg4));
5536 unlock_user(p, arg2, 0);
5539 case TARGET_NR_chmod:
5540 if (!(p = lock_user_string(arg1)))
5542 ret = get_errno(chmod(p, arg2));
5543 unlock_user(p, arg1, 0);
5545 #ifdef TARGET_NR_break
5546 case TARGET_NR_break:
5549 #ifdef TARGET_NR_oldstat
5550 case TARGET_NR_oldstat:
5553 case TARGET_NR_lseek:
5554 ret = get_errno(lseek(arg1, arg2, arg3));
5556 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5557 /* Alpha specific */
5558 case TARGET_NR_getxpid:
5559 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
5560 ret = get_errno(getpid());
5563 #ifdef TARGET_NR_getpid
5564 case TARGET_NR_getpid:
5565 ret = get_errno(getpid());
5568 case TARGET_NR_mount:
5570 /* need to look at the data field */
5572 p = lock_user_string(arg1);
5573 p2 = lock_user_string(arg2);
5574 p3 = lock_user_string(arg3);
5575 if (!p || !p2 || !p3)
5576 ret = -TARGET_EFAULT;
5578 /* FIXME - arg5 should be locked, but it isn't clear how to
5579 * do that since it's not guaranteed to be a NULL-terminated
5583 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
5585 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
5587 unlock_user(p, arg1, 0);
5588 unlock_user(p2, arg2, 0);
5589 unlock_user(p3, arg3, 0);
5592 #ifdef TARGET_NR_umount
5593 case TARGET_NR_umount:
5594 if (!(p = lock_user_string(arg1)))
5596 ret = get_errno(umount(p));
5597 unlock_user(p, arg1, 0);
5600 #ifdef TARGET_NR_stime /* not on alpha */
5601 case TARGET_NR_stime:
5604 if (get_user_sal(host_time, arg1))
5606 ret = get_errno(stime(&host_time));
5610 case TARGET_NR_ptrace:
5612 #ifdef TARGET_NR_alarm /* not on alpha */
5613 case TARGET_NR_alarm:
5617 #ifdef TARGET_NR_oldfstat
5618 case TARGET_NR_oldfstat:
5621 #ifdef TARGET_NR_pause /* not on alpha */
5622 case TARGET_NR_pause:
5623 ret = get_errno(pause());
5626 #ifdef TARGET_NR_utime
5627 case TARGET_NR_utime:
5629 struct utimbuf tbuf, *host_tbuf;
5630 struct target_utimbuf *target_tbuf;
5632 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
5634 tbuf.actime = tswapal(target_tbuf->actime);
5635 tbuf.modtime = tswapal(target_tbuf->modtime);
5636 unlock_user_struct(target_tbuf, arg2, 0);
5641 if (!(p = lock_user_string(arg1)))
5643 ret = get_errno(utime(p, host_tbuf));
5644 unlock_user(p, arg1, 0);
5648 case TARGET_NR_utimes:
5650 struct timeval *tvp, tv[2];
5652 if (copy_from_user_timeval(&tv[0], arg2)
5653 || copy_from_user_timeval(&tv[1],
5654 arg2 + sizeof(struct target_timeval)))
5660 if (!(p = lock_user_string(arg1)))
5662 ret = get_errno(utimes(p, tvp));
5663 unlock_user(p, arg1, 0);
5666 #if defined(TARGET_NR_futimesat)
5667 case TARGET_NR_futimesat:
5669 struct timeval *tvp, tv[2];
5671 if (copy_from_user_timeval(&tv[0], arg3)
5672 || copy_from_user_timeval(&tv[1],
5673 arg3 + sizeof(struct target_timeval)))
5679 if (!(p = lock_user_string(arg2)))
5681 ret = get_errno(futimesat(arg1, path(p), tvp));
5682 unlock_user(p, arg2, 0);
5686 #ifdef TARGET_NR_stty
5687 case TARGET_NR_stty:
5690 #ifdef TARGET_NR_gtty
5691 case TARGET_NR_gtty:
5694 case TARGET_NR_access:
5695 if (!(p = lock_user_string(arg1)))
5697 ret = get_errno(access(path(p), arg2));
5698 unlock_user(p, arg1, 0);
5700 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5701 case TARGET_NR_faccessat:
5702 if (!(p = lock_user_string(arg2)))
5704 ret = get_errno(faccessat(arg1, p, arg3, 0));
5705 unlock_user(p, arg2, 0);
5708 #ifdef TARGET_NR_nice /* not on alpha */
5709 case TARGET_NR_nice:
5710 ret = get_errno(nice(arg1));
5713 #ifdef TARGET_NR_ftime
5714 case TARGET_NR_ftime:
5717 case TARGET_NR_sync:
5721 case TARGET_NR_kill:
5722 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
5724 case TARGET_NR_rename:
5727 p = lock_user_string(arg1);
5728 p2 = lock_user_string(arg2);
5730 ret = -TARGET_EFAULT;
5732 ret = get_errno(rename(p, p2));
5733 unlock_user(p2, arg2, 0);
5734 unlock_user(p, arg1, 0);
5737 #if defined(TARGET_NR_renameat)
5738 case TARGET_NR_renameat:
5741 p = lock_user_string(arg2);
5742 p2 = lock_user_string(arg4);
5744 ret = -TARGET_EFAULT;
5746 ret = get_errno(renameat(arg1, p, arg3, p2));
5747 unlock_user(p2, arg4, 0);
5748 unlock_user(p, arg2, 0);
5752 case TARGET_NR_mkdir:
5753 if (!(p = lock_user_string(arg1)))
5755 ret = get_errno(mkdir(p, arg2));
5756 unlock_user(p, arg1, 0);
5758 #if defined(TARGET_NR_mkdirat)
5759 case TARGET_NR_mkdirat:
5760 if (!(p = lock_user_string(arg2)))
5762 ret = get_errno(mkdirat(arg1, p, arg3));
5763 unlock_user(p, arg2, 0);
5766 case TARGET_NR_rmdir:
5767 if (!(p = lock_user_string(arg1)))
5769 ret = get_errno(rmdir(p));
5770 unlock_user(p, arg1, 0);
5773 ret = get_errno(dup(arg1));
5775 case TARGET_NR_pipe:
5776 ret = do_pipe(cpu_env, arg1, 0, 0);
5778 #ifdef TARGET_NR_pipe2
5779 case TARGET_NR_pipe2:
5780 ret = do_pipe(cpu_env, arg1,
5781 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
5784 case TARGET_NR_times:
5786 struct target_tms *tmsp;
5788 ret = get_errno(times(&tms));
5790 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
5793 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
5794 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
5795 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
5796 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
5799 ret = host_to_target_clock_t(ret);
5802 #ifdef TARGET_NR_prof
5803 case TARGET_NR_prof:
5806 #ifdef TARGET_NR_signal
5807 case TARGET_NR_signal:
5810 case TARGET_NR_acct:
5812 ret = get_errno(acct(NULL));
5814 if (!(p = lock_user_string(arg1)))
5816 ret = get_errno(acct(path(p)));
5817 unlock_user(p, arg1, 0);
5820 #ifdef TARGET_NR_umount2
5821 case TARGET_NR_umount2:
5822 if (!(p = lock_user_string(arg1)))
5824 ret = get_errno(umount2(p, arg2));
5825 unlock_user(p, arg1, 0);
5828 #ifdef TARGET_NR_lock
5829 case TARGET_NR_lock:
5832 case TARGET_NR_ioctl:
5833 ret = do_ioctl(arg1, arg2, arg3);
5835 case TARGET_NR_fcntl:
5836 ret = do_fcntl(arg1, arg2, arg3);
5838 #ifdef TARGET_NR_mpx
5842 case TARGET_NR_setpgid:
5843 ret = get_errno(setpgid(arg1, arg2));
5845 #ifdef TARGET_NR_ulimit
5846 case TARGET_NR_ulimit:
5849 #ifdef TARGET_NR_oldolduname
5850 case TARGET_NR_oldolduname:
5853 case TARGET_NR_umask:
5854 ret = get_errno(umask(arg1));
5856 case TARGET_NR_chroot:
5857 if (!(p = lock_user_string(arg1)))
5859 ret = get_errno(chroot(p));
5860 unlock_user(p, arg1, 0);
5862 case TARGET_NR_ustat:
5864 case TARGET_NR_dup2:
5865 ret = get_errno(dup2(arg1, arg2));
5867 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5868 case TARGET_NR_dup3:
5869 ret = get_errno(dup3(arg1, arg2, arg3));
5872 #ifdef TARGET_NR_getppid /* not on alpha */
5873 case TARGET_NR_getppid:
5874 ret = get_errno(getppid());
5877 case TARGET_NR_getpgrp:
5878 ret = get_errno(getpgrp());
5880 case TARGET_NR_setsid:
5881 ret = get_errno(setsid());
5883 #ifdef TARGET_NR_sigaction
5884 case TARGET_NR_sigaction:
5886 #if defined(TARGET_ALPHA)
5887 struct target_sigaction act, oact, *pact = 0;
5888 struct target_old_sigaction *old_act;
5890 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5892 act._sa_handler = old_act->_sa_handler;
5893 target_siginitset(&act.sa_mask, old_act->sa_mask);
5894 act.sa_flags = old_act->sa_flags;
5895 act.sa_restorer = 0;
5896 unlock_user_struct(old_act, arg2, 0);
5899 ret = get_errno(do_sigaction(arg1, pact, &oact));
5900 if (!is_error(ret) && arg3) {
5901 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5903 old_act->_sa_handler = oact._sa_handler;
5904 old_act->sa_mask = oact.sa_mask.sig[0];
5905 old_act->sa_flags = oact.sa_flags;
5906 unlock_user_struct(old_act, arg3, 1);
5908 #elif defined(TARGET_MIPS)
5909 struct target_sigaction act, oact, *pact, *old_act;
5912 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5914 act._sa_handler = old_act->_sa_handler;
5915 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5916 act.sa_flags = old_act->sa_flags;
5917 unlock_user_struct(old_act, arg2, 0);
5923 ret = get_errno(do_sigaction(arg1, pact, &oact));
5925 if (!is_error(ret) && arg3) {
5926 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5928 old_act->_sa_handler = oact._sa_handler;
5929 old_act->sa_flags = oact.sa_flags;
5930 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5931 old_act->sa_mask.sig[1] = 0;
5932 old_act->sa_mask.sig[2] = 0;
5933 old_act->sa_mask.sig[3] = 0;
5934 unlock_user_struct(old_act, arg3, 1);
5937 struct target_old_sigaction *old_act;
5938 struct target_sigaction act, oact, *pact;
5940 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5942 act._sa_handler = old_act->_sa_handler;
5943 target_siginitset(&act.sa_mask, old_act->sa_mask);
5944 act.sa_flags = old_act->sa_flags;
5945 act.sa_restorer = old_act->sa_restorer;
5946 unlock_user_struct(old_act, arg2, 0);
5951 ret = get_errno(do_sigaction(arg1, pact, &oact));
5952 if (!is_error(ret) && arg3) {
5953 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5955 old_act->_sa_handler = oact._sa_handler;
5956 old_act->sa_mask = oact.sa_mask.sig[0];
5957 old_act->sa_flags = oact.sa_flags;
5958 old_act->sa_restorer = oact.sa_restorer;
5959 unlock_user_struct(old_act, arg3, 1);
5965 case TARGET_NR_rt_sigaction:
5967 #if defined(TARGET_ALPHA)
5968 struct target_sigaction act, oact, *pact = 0;
5969 struct target_rt_sigaction *rt_act;
5970 /* ??? arg4 == sizeof(sigset_t). */
5972 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5974 act._sa_handler = rt_act->_sa_handler;
5975 act.sa_mask = rt_act->sa_mask;
5976 act.sa_flags = rt_act->sa_flags;
5977 act.sa_restorer = arg5;
5978 unlock_user_struct(rt_act, arg2, 0);
5981 ret = get_errno(do_sigaction(arg1, pact, &oact));
5982 if (!is_error(ret) && arg3) {
5983 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5985 rt_act->_sa_handler = oact._sa_handler;
5986 rt_act->sa_mask = oact.sa_mask;
5987 rt_act->sa_flags = oact.sa_flags;
5988 unlock_user_struct(rt_act, arg3, 1);
5991 struct target_sigaction *act;
5992 struct target_sigaction *oact;
5995 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
6000 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
6001 ret = -TARGET_EFAULT;
6002 goto rt_sigaction_fail;
6006 ret = get_errno(do_sigaction(arg1, act, oact));
6009 unlock_user_struct(act, arg2, 0);
6011 unlock_user_struct(oact, arg3, 1);
6015 #ifdef TARGET_NR_sgetmask /* not on alpha */
6016 case TARGET_NR_sgetmask:
6019 abi_ulong target_set;
6020 sigprocmask(0, NULL, &cur_set);
6021 host_to_target_old_sigset(&target_set, &cur_set);
6026 #ifdef TARGET_NR_ssetmask /* not on alpha */
6027 case TARGET_NR_ssetmask:
6029 sigset_t set, oset, cur_set;
6030 abi_ulong target_set = arg1;
6031 sigprocmask(0, NULL, &cur_set);
6032 target_to_host_old_sigset(&set, &target_set);
6033 sigorset(&set, &set, &cur_set);
6034 sigprocmask(SIG_SETMASK, &set, &oset);
6035 host_to_target_old_sigset(&target_set, &oset);
6040 #ifdef TARGET_NR_sigprocmask
6041 case TARGET_NR_sigprocmask:
6043 #if defined(TARGET_ALPHA)
6044 sigset_t set, oldset;
6049 case TARGET_SIG_BLOCK:
6052 case TARGET_SIG_UNBLOCK:
6055 case TARGET_SIG_SETMASK:
6059 ret = -TARGET_EINVAL;
6063 target_to_host_old_sigset(&set, &mask);
6065 ret = get_errno(sigprocmask(how, &set, &oldset));
6066 if (!is_error(ret)) {
6067 host_to_target_old_sigset(&mask, &oldset);
6069 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
6072 sigset_t set, oldset, *set_ptr;
6077 case TARGET_SIG_BLOCK:
6080 case TARGET_SIG_UNBLOCK:
6083 case TARGET_SIG_SETMASK:
6087 ret = -TARGET_EINVAL;
6090 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
6092 target_to_host_old_sigset(&set, p);
6093 unlock_user(p, arg2, 0);
6099 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
6100 if (!is_error(ret) && arg3) {
6101 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
6103 host_to_target_old_sigset(p, &oldset);
6104 unlock_user(p, arg3, sizeof(target_sigset_t));
6110 case TARGET_NR_rt_sigprocmask:
6113 sigset_t set, oldset, *set_ptr;
6117 case TARGET_SIG_BLOCK:
6120 case TARGET_SIG_UNBLOCK:
6123 case TARGET_SIG_SETMASK:
6127 ret = -TARGET_EINVAL;
6130 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
6132 target_to_host_sigset(&set, p);
6133 unlock_user(p, arg2, 0);
6139 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
6140 if (!is_error(ret) && arg3) {
6141 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
6143 host_to_target_sigset(p, &oldset);
6144 unlock_user(p, arg3, sizeof(target_sigset_t));
6148 #ifdef TARGET_NR_sigpending
6149 case TARGET_NR_sigpending:
6152 ret = get_errno(sigpending(&set));
6153 if (!is_error(ret)) {
6154 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
6156 host_to_target_old_sigset(p, &set);
6157 unlock_user(p, arg1, sizeof(target_sigset_t));
6162 case TARGET_NR_rt_sigpending:
6165 ret = get_errno(sigpending(&set));
6166 if (!is_error(ret)) {
6167 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
6169 host_to_target_sigset(p, &set);
6170 unlock_user(p, arg1, sizeof(target_sigset_t));
6174 #ifdef TARGET_NR_sigsuspend
6175 case TARGET_NR_sigsuspend:
6178 #if defined(TARGET_ALPHA)
6179 abi_ulong mask = arg1;
6180 target_to_host_old_sigset(&set, &mask);
6182 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6184 target_to_host_old_sigset(&set, p);
6185 unlock_user(p, arg1, 0);
6187 ret = get_errno(sigsuspend(&set));
6191 case TARGET_NR_rt_sigsuspend:
6194 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6196 target_to_host_sigset(&set, p);
6197 unlock_user(p, arg1, 0);
6198 ret = get_errno(sigsuspend(&set));
6201 case TARGET_NR_rt_sigtimedwait:
6204 struct timespec uts, *puts;
6207 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
6209 target_to_host_sigset(&set, p);
6210 unlock_user(p, arg1, 0);
6213 target_to_host_timespec(puts, arg3);
6217 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
6218 if (!is_error(ret) && arg2) {
6219 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
6221 host_to_target_siginfo(p, &uinfo);
6222 unlock_user(p, arg2, sizeof(target_siginfo_t));
6226 case TARGET_NR_rt_sigqueueinfo:
6229 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
6231 target_to_host_siginfo(&uinfo, p);
6232 unlock_user(p, arg1, 0);
6233 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
6236 #ifdef TARGET_NR_sigreturn
6237 case TARGET_NR_sigreturn:
6238 /* NOTE: ret is eax, so not transcoding must be done */
6239 ret = do_sigreturn(cpu_env);
6242 case TARGET_NR_rt_sigreturn:
6243 /* NOTE: ret is eax, so not transcoding must be done */
6244 ret = do_rt_sigreturn(cpu_env);
6246 case TARGET_NR_sethostname:
6247 if (!(p = lock_user_string(arg1)))
6249 ret = get_errno(sethostname(p, arg2));
6250 unlock_user(p, arg1, 0);
6252 case TARGET_NR_setrlimit:
6254 int resource = target_to_host_resource(arg1);
6255 struct target_rlimit *target_rlim;
6257 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
6259 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
6260 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
6261 unlock_user_struct(target_rlim, arg2, 0);
6262 ret = get_errno(setrlimit(resource, &rlim));
6265 case TARGET_NR_getrlimit:
6267 int resource = target_to_host_resource(arg1);
6268 struct target_rlimit *target_rlim;
6271 ret = get_errno(getrlimit(resource, &rlim));
6272 if (!is_error(ret)) {
6273 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6275 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6276 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
6277 unlock_user_struct(target_rlim, arg2, 1);
6281 case TARGET_NR_getrusage:
6283 struct rusage rusage;
6284 ret = get_errno(getrusage(arg1, &rusage));
6285 if (!is_error(ret)) {
6286 host_to_target_rusage(arg2, &rusage);
6290 case TARGET_NR_gettimeofday:
6293 ret = get_errno(gettimeofday(&tv, NULL));
6294 if (!is_error(ret)) {
6295 if (copy_to_user_timeval(arg1, &tv))
6300 case TARGET_NR_settimeofday:
6303 if (copy_from_user_timeval(&tv, arg1))
6305 ret = get_errno(settimeofday(&tv, NULL));
6308 #if defined(TARGET_NR_select)
6309 case TARGET_NR_select:
6310 #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6311 ret = do_select(arg1, arg2, arg3, arg4, arg5);
6314 struct target_sel_arg_struct *sel;
6315 abi_ulong inp, outp, exp, tvp;
6318 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
6320 nsel = tswapal(sel->n);
6321 inp = tswapal(sel->inp);
6322 outp = tswapal(sel->outp);
6323 exp = tswapal(sel->exp);
6324 tvp = tswapal(sel->tvp);
6325 unlock_user_struct(sel, arg1, 0);
6326 ret = do_select(nsel, inp, outp, exp, tvp);
6331 #ifdef TARGET_NR_pselect6
6332 case TARGET_NR_pselect6:
6334 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
6335 fd_set rfds, wfds, efds;
6336 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6337 struct timespec ts, *ts_ptr;
6340 * The 6th arg is actually two args smashed together,
6341 * so we cannot use the C library.
6349 abi_ulong arg_sigset, arg_sigsize, *arg7;
6350 target_sigset_t *target_sigset;
6358 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
6362 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
6366 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
6372 * This takes a timespec, and not a timeval, so we cannot
6373 * use the do_select() helper ...
6376 if (target_to_host_timespec(&ts, ts_addr)) {
6384 /* Extract the two packed args for the sigset */
6387 sig.size = _NSIG / 8;
6389 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
6393 arg_sigset = tswapal(arg7[0]);
6394 arg_sigsize = tswapal(arg7[1]);
6395 unlock_user(arg7, arg6, 0);
6399 if (arg_sigsize != sizeof(*target_sigset)) {
6400 /* Like the kernel, we enforce correct size sigsets */
6401 ret = -TARGET_EINVAL;
6404 target_sigset = lock_user(VERIFY_READ, arg_sigset,
6405 sizeof(*target_sigset), 1);
6406 if (!target_sigset) {
6409 target_to_host_sigset(&set, target_sigset);
6410 unlock_user(target_sigset, arg_sigset, 0);
6418 ret = get_errno(sys_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
6421 if (!is_error(ret)) {
6422 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
6424 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
6426 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
6429 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
6435 case TARGET_NR_symlink:
6438 p = lock_user_string(arg1);
6439 p2 = lock_user_string(arg2);
6441 ret = -TARGET_EFAULT;
6443 ret = get_errno(symlink(p, p2));
6444 unlock_user(p2, arg2, 0);
6445 unlock_user(p, arg1, 0);
6448 #if defined(TARGET_NR_symlinkat)
6449 case TARGET_NR_symlinkat:
6452 p = lock_user_string(arg1);
6453 p2 = lock_user_string(arg3);
6455 ret = -TARGET_EFAULT;
6457 ret = get_errno(symlinkat(p, arg2, p2));
6458 unlock_user(p2, arg3, 0);
6459 unlock_user(p, arg1, 0);
6463 #ifdef TARGET_NR_oldlstat
6464 case TARGET_NR_oldlstat:
6467 case TARGET_NR_readlink:
6470 p = lock_user_string(arg1);
6471 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
6473 ret = -TARGET_EFAULT;
6474 } else if (is_proc_myself((const char *)p, "exe")) {
6475 char real[PATH_MAX], *temp;
6476 temp = realpath(exec_path, real);
6477 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
6478 snprintf((char *)p2, arg3, "%s", real);
6480 ret = get_errno(readlink(path(p), p2, arg3));
6482 unlock_user(p2, arg2, ret);
6483 unlock_user(p, arg1, 0);
6486 #if defined(TARGET_NR_readlinkat)
6487 case TARGET_NR_readlinkat:
6490 p = lock_user_string(arg2);
6491 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
6493 ret = -TARGET_EFAULT;
6494 } else if (is_proc_myself((const char *)p, "exe")) {
6495 char real[PATH_MAX], *temp;
6496 temp = realpath(exec_path, real);
6497 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
6498 snprintf((char *)p2, arg4, "%s", real);
6500 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
6502 unlock_user(p2, arg3, ret);
6503 unlock_user(p, arg2, 0);
6507 #ifdef TARGET_NR_uselib
6508 case TARGET_NR_uselib:
6511 #ifdef TARGET_NR_swapon
6512 case TARGET_NR_swapon:
6513 if (!(p = lock_user_string(arg1)))
6515 ret = get_errno(swapon(p, arg2));
6516 unlock_user(p, arg1, 0);
6519 case TARGET_NR_reboot:
6520 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
6521 /* arg4 must be ignored in all other cases */
6522 p = lock_user_string(arg4);
6526 ret = get_errno(reboot(arg1, arg2, arg3, p));
6527 unlock_user(p, arg4, 0);
6529 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
6532 #ifdef TARGET_NR_readdir
6533 case TARGET_NR_readdir:
6536 #ifdef TARGET_NR_mmap
6537 case TARGET_NR_mmap:
6538 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6539 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
6540 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6541 || defined(TARGET_S390X)
6544 abi_ulong v1, v2, v3, v4, v5, v6;
6545 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
6553 unlock_user(v, arg1, 0);
6554 ret = get_errno(target_mmap(v1, v2, v3,
6555 target_to_host_bitmask(v4, mmap_flags_tbl),
6559 ret = get_errno(target_mmap(arg1, arg2, arg3,
6560 target_to_host_bitmask(arg4, mmap_flags_tbl),
6566 #ifdef TARGET_NR_mmap2
6567 case TARGET_NR_mmap2:
6569 #define MMAP_SHIFT 12
6571 ret = get_errno(target_mmap(arg1, arg2, arg3,
6572 target_to_host_bitmask(arg4, mmap_flags_tbl),
6574 arg6 << MMAP_SHIFT));
6577 case TARGET_NR_munmap:
6578 ret = get_errno(target_munmap(arg1, arg2));
6580 case TARGET_NR_mprotect:
6582 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
6583 /* Special hack to detect libc making the stack executable. */
6584 if ((arg3 & PROT_GROWSDOWN)
6585 && arg1 >= ts->info->stack_limit
6586 && arg1 <= ts->info->start_stack) {
6587 arg3 &= ~PROT_GROWSDOWN;
6588 arg2 = arg2 + arg1 - ts->info->stack_limit;
6589 arg1 = ts->info->stack_limit;
6592 ret = get_errno(target_mprotect(arg1, arg2, arg3));
6594 #ifdef TARGET_NR_mremap
6595 case TARGET_NR_mremap:
6596 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
6599 /* ??? msync/mlock/munlock are broken for softmmu. */
6600 #ifdef TARGET_NR_msync
6601 case TARGET_NR_msync:
6602 ret = get_errno(msync(g2h(arg1), arg2, arg3));
6605 #ifdef TARGET_NR_mlock
6606 case TARGET_NR_mlock:
6607 ret = get_errno(mlock(g2h(arg1), arg2));
6610 #ifdef TARGET_NR_munlock
6611 case TARGET_NR_munlock:
6612 ret = get_errno(munlock(g2h(arg1), arg2));
6615 #ifdef TARGET_NR_mlockall
6616 case TARGET_NR_mlockall:
6617 ret = get_errno(mlockall(arg1));
6620 #ifdef TARGET_NR_munlockall
6621 case TARGET_NR_munlockall:
6622 ret = get_errno(munlockall());
6625 case TARGET_NR_truncate:
6626 if (!(p = lock_user_string(arg1)))
6628 ret = get_errno(truncate(p, arg2));
6629 unlock_user(p, arg1, 0);
6631 case TARGET_NR_ftruncate:
6632 ret = get_errno(ftruncate(arg1, arg2));
6634 case TARGET_NR_fchmod:
6635 ret = get_errno(fchmod(arg1, arg2));
6637 #if defined(TARGET_NR_fchmodat)
6638 case TARGET_NR_fchmodat:
6639 if (!(p = lock_user_string(arg2)))
6641 ret = get_errno(fchmodat(arg1, p, arg3, 0));
6642 unlock_user(p, arg2, 0);
6645 case TARGET_NR_getpriority:
6646 /* Note that negative values are valid for getpriority, so we must
6647 differentiate based on errno settings. */
6649 ret = getpriority(arg1, arg2);
6650 if (ret == -1 && errno != 0) {
6651 ret = -host_to_target_errno(errno);
6655 /* Return value is the unbiased priority. Signal no error. */
6656 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
6658 /* Return value is a biased priority to avoid negative numbers. */
6662 case TARGET_NR_setpriority:
6663 ret = get_errno(setpriority(arg1, arg2, arg3));
6665 #ifdef TARGET_NR_profil
6666 case TARGET_NR_profil:
6669 case TARGET_NR_statfs:
6670 if (!(p = lock_user_string(arg1)))
6672 ret = get_errno(statfs(path(p), &stfs));
6673 unlock_user(p, arg1, 0);
6675 if (!is_error(ret)) {
6676 struct target_statfs *target_stfs;
6678 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
6680 __put_user(stfs.f_type, &target_stfs->f_type);
6681 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6682 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6683 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6684 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6685 __put_user(stfs.f_files, &target_stfs->f_files);
6686 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6687 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6688 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6689 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
6690 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6691 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
6692 unlock_user_struct(target_stfs, arg2, 1);
6695 case TARGET_NR_fstatfs:
6696 ret = get_errno(fstatfs(arg1, &stfs));
6697 goto convert_statfs;
6698 #ifdef TARGET_NR_statfs64
6699 case TARGET_NR_statfs64:
6700 if (!(p = lock_user_string(arg1)))
6702 ret = get_errno(statfs(path(p), &stfs));
6703 unlock_user(p, arg1, 0);
6705 if (!is_error(ret)) {
6706 struct target_statfs64 *target_stfs;
6708 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
6710 __put_user(stfs.f_type, &target_stfs->f_type);
6711 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
6712 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
6713 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
6714 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
6715 __put_user(stfs.f_files, &target_stfs->f_files);
6716 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
6717 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
6718 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
6719 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
6720 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
6721 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
6722 unlock_user_struct(target_stfs, arg3, 1);
6725 case TARGET_NR_fstatfs64:
6726 ret = get_errno(fstatfs(arg1, &stfs));
6727 goto convert_statfs64;
6729 #ifdef TARGET_NR_ioperm
6730 case TARGET_NR_ioperm:
6733 #ifdef TARGET_NR_socketcall
6734 case TARGET_NR_socketcall:
6735 ret = do_socketcall(arg1, arg2);
6738 #ifdef TARGET_NR_accept
6739 case TARGET_NR_accept:
6740 ret = do_accept4(arg1, arg2, arg3, 0);
6743 #ifdef TARGET_NR_accept4
6744 case TARGET_NR_accept4:
6745 #ifdef CONFIG_ACCEPT4
6746 ret = do_accept4(arg1, arg2, arg3, arg4);
6752 #ifdef TARGET_NR_bind
6753 case TARGET_NR_bind:
6754 ret = do_bind(arg1, arg2, arg3);
6757 #ifdef TARGET_NR_connect
6758 case TARGET_NR_connect:
6759 ret = do_connect(arg1, arg2, arg3);
6762 #ifdef TARGET_NR_getpeername
6763 case TARGET_NR_getpeername:
6764 ret = do_getpeername(arg1, arg2, arg3);
6767 #ifdef TARGET_NR_getsockname
6768 case TARGET_NR_getsockname:
6769 ret = do_getsockname(arg1, arg2, arg3);
6772 #ifdef TARGET_NR_getsockopt
6773 case TARGET_NR_getsockopt:
6774 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
6777 #ifdef TARGET_NR_listen
6778 case TARGET_NR_listen:
6779 ret = get_errno(listen(arg1, arg2));
6782 #ifdef TARGET_NR_recv
6783 case TARGET_NR_recv:
6784 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
6787 #ifdef TARGET_NR_recvfrom
6788 case TARGET_NR_recvfrom:
6789 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
6792 #ifdef TARGET_NR_recvmsg
6793 case TARGET_NR_recvmsg:
6794 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
6797 #ifdef TARGET_NR_send
6798 case TARGET_NR_send:
6799 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
6802 #ifdef TARGET_NR_sendmsg
6803 case TARGET_NR_sendmsg:
6804 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
6807 #ifdef TARGET_NR_sendto
6808 case TARGET_NR_sendto:
6809 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
6812 #ifdef TARGET_NR_shutdown
6813 case TARGET_NR_shutdown:
6814 ret = get_errno(shutdown(arg1, arg2));
6817 #ifdef TARGET_NR_socket
6818 case TARGET_NR_socket:
6819 ret = do_socket(arg1, arg2, arg3);
6822 #ifdef TARGET_NR_socketpair
6823 case TARGET_NR_socketpair:
6824 ret = do_socketpair(arg1, arg2, arg3, arg4);
6827 #ifdef TARGET_NR_setsockopt
6828 case TARGET_NR_setsockopt:
6829 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
6833 case TARGET_NR_syslog:
6834 if (!(p = lock_user_string(arg2)))
6836 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
6837 unlock_user(p, arg2, 0);
6840 case TARGET_NR_setitimer:
6842 struct itimerval value, ovalue, *pvalue;
6846 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
6847 || copy_from_user_timeval(&pvalue->it_value,
6848 arg2 + sizeof(struct target_timeval)))
6853 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
6854 if (!is_error(ret) && arg3) {
6855 if (copy_to_user_timeval(arg3,
6856 &ovalue.it_interval)
6857 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
6863 case TARGET_NR_getitimer:
6865 struct itimerval value;
6867 ret = get_errno(getitimer(arg1, &value));
6868 if (!is_error(ret) && arg2) {
6869 if (copy_to_user_timeval(arg2,
6871 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
6877 case TARGET_NR_stat:
6878 if (!(p = lock_user_string(arg1)))
6880 ret = get_errno(stat(path(p), &st));
6881 unlock_user(p, arg1, 0);
6883 case TARGET_NR_lstat:
6884 if (!(p = lock_user_string(arg1)))
6886 ret = get_errno(lstat(path(p), &st));
6887 unlock_user(p, arg1, 0);
6889 case TARGET_NR_fstat:
6891 ret = get_errno(fstat(arg1, &st));
6893 if (!is_error(ret)) {
6894 struct target_stat *target_st;
6896 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
6898 memset(target_st, 0, sizeof(*target_st));
6899 __put_user(st.st_dev, &target_st->st_dev);
6900 __put_user(st.st_ino, &target_st->st_ino);
6901 __put_user(st.st_mode, &target_st->st_mode);
6902 __put_user(st.st_uid, &target_st->st_uid);
6903 __put_user(st.st_gid, &target_st->st_gid);
6904 __put_user(st.st_nlink, &target_st->st_nlink);
6905 __put_user(st.st_rdev, &target_st->st_rdev);
6906 __put_user(st.st_size, &target_st->st_size);
6907 __put_user(st.st_blksize, &target_st->st_blksize);
6908 __put_user(st.st_blocks, &target_st->st_blocks);
6909 __put_user(st.st_atime, &target_st->target_st_atime);
6910 __put_user(st.st_mtime, &target_st->target_st_mtime);
6911 __put_user(st.st_ctime, &target_st->target_st_ctime);
6912 unlock_user_struct(target_st, arg2, 1);
6916 #ifdef TARGET_NR_olduname
6917 case TARGET_NR_olduname:
6920 #ifdef TARGET_NR_iopl
6921 case TARGET_NR_iopl:
6924 case TARGET_NR_vhangup:
6925 ret = get_errno(vhangup());
6927 #ifdef TARGET_NR_idle
6928 case TARGET_NR_idle:
6931 #ifdef TARGET_NR_syscall
6932 case TARGET_NR_syscall:
6933 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
6934 arg6, arg7, arg8, 0);
6937 case TARGET_NR_wait4:
6940 abi_long status_ptr = arg2;
6941 struct rusage rusage, *rusage_ptr;
6942 abi_ulong target_rusage = arg4;
6944 rusage_ptr = &rusage;
6947 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
6948 if (!is_error(ret)) {
6949 if (status_ptr && ret) {
6950 status = host_to_target_waitstatus(status);
6951 if (put_user_s32(status, status_ptr))
6955 host_to_target_rusage(target_rusage, &rusage);
6959 #ifdef TARGET_NR_swapoff
6960 case TARGET_NR_swapoff:
6961 if (!(p = lock_user_string(arg1)))
6963 ret = get_errno(swapoff(p));
6964 unlock_user(p, arg1, 0);
6967 case TARGET_NR_sysinfo:
6969 struct target_sysinfo *target_value;
6970 struct sysinfo value;
6971 ret = get_errno(sysinfo(&value));
6972 if (!is_error(ret) && arg1)
6974 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
6976 __put_user(value.uptime, &target_value->uptime);
6977 __put_user(value.loads[0], &target_value->loads[0]);
6978 __put_user(value.loads[1], &target_value->loads[1]);
6979 __put_user(value.loads[2], &target_value->loads[2]);
6980 __put_user(value.totalram, &target_value->totalram);
6981 __put_user(value.freeram, &target_value->freeram);
6982 __put_user(value.sharedram, &target_value->sharedram);
6983 __put_user(value.bufferram, &target_value->bufferram);
6984 __put_user(value.totalswap, &target_value->totalswap);
6985 __put_user(value.freeswap, &target_value->freeswap);
6986 __put_user(value.procs, &target_value->procs);
6987 __put_user(value.totalhigh, &target_value->totalhigh);
6988 __put_user(value.freehigh, &target_value->freehigh);
6989 __put_user(value.mem_unit, &target_value->mem_unit);
6990 unlock_user_struct(target_value, arg1, 1);
6994 #ifdef TARGET_NR_ipc
6996 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
6999 #ifdef TARGET_NR_semget
7000 case TARGET_NR_semget:
7001 ret = get_errno(semget(arg1, arg2, arg3));
7004 #ifdef TARGET_NR_semop
7005 case TARGET_NR_semop:
7006 ret = do_semop(arg1, arg2, arg3);
7009 #ifdef TARGET_NR_semctl
7010 case TARGET_NR_semctl:
7011 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
7014 #ifdef TARGET_NR_msgctl
7015 case TARGET_NR_msgctl:
7016 ret = do_msgctl(arg1, arg2, arg3);
7019 #ifdef TARGET_NR_msgget
7020 case TARGET_NR_msgget:
7021 ret = get_errno(msgget(arg1, arg2));
7024 #ifdef TARGET_NR_msgrcv
7025 case TARGET_NR_msgrcv:
7026 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
7029 #ifdef TARGET_NR_msgsnd
7030 case TARGET_NR_msgsnd:
7031 ret = do_msgsnd(arg1, arg2, arg3, arg4);
7034 #ifdef TARGET_NR_shmget
7035 case TARGET_NR_shmget:
7036 ret = get_errno(shmget(arg1, arg2, arg3));
7039 #ifdef TARGET_NR_shmctl
7040 case TARGET_NR_shmctl:
7041 ret = do_shmctl(arg1, arg2, arg3);
7044 #ifdef TARGET_NR_shmat
7045 case TARGET_NR_shmat:
7046 ret = do_shmat(arg1, arg2, arg3);
7049 #ifdef TARGET_NR_shmdt
7050 case TARGET_NR_shmdt:
7051 ret = do_shmdt(arg1);
7054 case TARGET_NR_fsync:
7055 ret = get_errno(fsync(arg1));
7057 case TARGET_NR_clone:
7058 /* Linux manages to have three different orderings for its
7059 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7060 * match the kernel's CONFIG_CLONE_* settings.
7061 * Microblaze is further special in that it uses a sixth
7062 * implicit argument to clone for the TLS pointer.
7064 #if defined(TARGET_MICROBLAZE)
7065 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
7066 #elif defined(TARGET_CLONE_BACKWARDS)
7067 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
7068 #elif defined(TARGET_CLONE_BACKWARDS2)
7069 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
7071 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
7074 #ifdef __NR_exit_group
7075 /* new thread calls */
7076 case TARGET_NR_exit_group:
7080 gdb_exit(cpu_env, arg1);
7081 ret = get_errno(exit_group(arg1));
7084 case TARGET_NR_setdomainname:
7085 if (!(p = lock_user_string(arg1)))
7087 ret = get_errno(setdomainname(p, arg2));
7088 unlock_user(p, arg1, 0);
7090 case TARGET_NR_uname:
7091 /* no need to transcode because we use the linux syscall */
7093 struct new_utsname * buf;
7095 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
7097 ret = get_errno(sys_uname(buf));
7098 if (!is_error(ret)) {
7099 /* Overrite the native machine name with whatever is being
7101 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
7102 /* Allow the user to override the reported release. */
7103 if (qemu_uname_release && *qemu_uname_release)
7104 strcpy (buf->release, qemu_uname_release);
7106 unlock_user_struct(buf, arg1, 1);
7110 case TARGET_NR_modify_ldt:
7111 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
7113 #if !defined(TARGET_X86_64)
7114 case TARGET_NR_vm86old:
7116 case TARGET_NR_vm86:
7117 ret = do_vm86(cpu_env, arg1, arg2);
7121 case TARGET_NR_adjtimex:
7123 #ifdef TARGET_NR_create_module
7124 case TARGET_NR_create_module:
7126 case TARGET_NR_init_module:
7127 case TARGET_NR_delete_module:
7128 #ifdef TARGET_NR_get_kernel_syms
7129 case TARGET_NR_get_kernel_syms:
7132 case TARGET_NR_quotactl:
7134 case TARGET_NR_getpgid:
7135 ret = get_errno(getpgid(arg1));
7137 case TARGET_NR_fchdir:
7138 ret = get_errno(fchdir(arg1));
7140 #ifdef TARGET_NR_bdflush /* not on x86_64 */
7141 case TARGET_NR_bdflush:
7144 #ifdef TARGET_NR_sysfs
7145 case TARGET_NR_sysfs:
7148 case TARGET_NR_personality:
7149 ret = get_errno(personality(arg1));
7151 #ifdef TARGET_NR_afs_syscall
7152 case TARGET_NR_afs_syscall:
7155 #ifdef TARGET_NR__llseek /* Not on alpha */
7156 case TARGET_NR__llseek:
7159 #if !defined(__NR_llseek)
7160 res = lseek(arg1, ((uint64_t)arg2 << 32) | arg3, arg5);
7162 ret = get_errno(res);
7167 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
7169 if ((ret == 0) && put_user_s64(res, arg4)) {
7175 case TARGET_NR_getdents:
7176 #ifdef __NR_getdents
7177 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
7179 struct target_dirent *target_dirp;
7180 struct linux_dirent *dirp;
7181 abi_long count = arg3;
7183 dirp = malloc(count);
7185 ret = -TARGET_ENOMEM;
7189 ret = get_errno(sys_getdents(arg1, dirp, count));
7190 if (!is_error(ret)) {
7191 struct linux_dirent *de;
7192 struct target_dirent *tde;
7194 int reclen, treclen;
7195 int count1, tnamelen;
7199 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7203 reclen = de->d_reclen;
7204 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
7205 assert(tnamelen >= 0);
7206 treclen = tnamelen + offsetof(struct target_dirent, d_name);
7207 assert(count1 + treclen <= count);
7208 tde->d_reclen = tswap16(treclen);
7209 tde->d_ino = tswapal(de->d_ino);
7210 tde->d_off = tswapal(de->d_off);
7211 memcpy(tde->d_name, de->d_name, tnamelen);
7212 de = (struct linux_dirent *)((char *)de + reclen);
7214 tde = (struct target_dirent *)((char *)tde + treclen);
7218 unlock_user(target_dirp, arg2, ret);
7224 struct linux_dirent *dirp;
7225 abi_long count = arg3;
7227 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7229 ret = get_errno(sys_getdents(arg1, dirp, count));
7230 if (!is_error(ret)) {
7231 struct linux_dirent *de;
7236 reclen = de->d_reclen;
7239 de->d_reclen = tswap16(reclen);
7240 tswapls(&de->d_ino);
7241 tswapls(&de->d_off);
7242 de = (struct linux_dirent *)((char *)de + reclen);
7246 unlock_user(dirp, arg2, ret);
7250 /* Implement getdents in terms of getdents64 */
7252 struct linux_dirent64 *dirp;
7253 abi_long count = arg3;
7255 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
7259 ret = get_errno(sys_getdents64(arg1, dirp, count));
7260 if (!is_error(ret)) {
7261 /* Convert the dirent64 structs to target dirent. We do this
7262 * in-place, since we can guarantee that a target_dirent is no
7263 * larger than a dirent64; however this means we have to be
7264 * careful to read everything before writing in the new format.
7266 struct linux_dirent64 *de;
7267 struct target_dirent *tde;
7272 tde = (struct target_dirent *)dirp;
7274 int namelen, treclen;
7275 int reclen = de->d_reclen;
7276 uint64_t ino = de->d_ino;
7277 int64_t off = de->d_off;
7278 uint8_t type = de->d_type;
7280 namelen = strlen(de->d_name);
7281 treclen = offsetof(struct target_dirent, d_name)
7283 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
7285 memmove(tde->d_name, de->d_name, namelen + 1);
7286 tde->d_ino = tswapal(ino);
7287 tde->d_off = tswapal(off);
7288 tde->d_reclen = tswap16(treclen);
7289 /* The target_dirent type is in what was formerly a padding
7290 * byte at the end of the structure:
7292 *(((char *)tde) + treclen - 1) = type;
7294 de = (struct linux_dirent64 *)((char *)de + reclen);
7295 tde = (struct target_dirent *)((char *)tde + treclen);
7301 unlock_user(dirp, arg2, ret);
7305 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
7306 case TARGET_NR_getdents64:
7308 struct linux_dirent64 *dirp;
7309 abi_long count = arg3;
7310 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
7312 ret = get_errno(sys_getdents64(arg1, dirp, count));
7313 if (!is_error(ret)) {
7314 struct linux_dirent64 *de;
7319 reclen = de->d_reclen;
7322 de->d_reclen = tswap16(reclen);
7323 tswap64s((uint64_t *)&de->d_ino);
7324 tswap64s((uint64_t *)&de->d_off);
7325 de = (struct linux_dirent64 *)((char *)de + reclen);
7329 unlock_user(dirp, arg2, ret);
7332 #endif /* TARGET_NR_getdents64 */
7333 #if defined(TARGET_NR__newselect)
7334 case TARGET_NR__newselect:
7335 ret = do_select(arg1, arg2, arg3, arg4, arg5);
7338 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7339 # ifdef TARGET_NR_poll
7340 case TARGET_NR_poll:
7342 # ifdef TARGET_NR_ppoll
7343 case TARGET_NR_ppoll:
7346 struct target_pollfd *target_pfd;
7347 unsigned int nfds = arg2;
7352 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
7356 pfd = alloca(sizeof(struct pollfd) * nfds);
7357 for(i = 0; i < nfds; i++) {
7358 pfd[i].fd = tswap32(target_pfd[i].fd);
7359 pfd[i].events = tswap16(target_pfd[i].events);
7362 # ifdef TARGET_NR_ppoll
7363 if (num == TARGET_NR_ppoll) {
7364 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
7365 target_sigset_t *target_set;
7366 sigset_t _set, *set = &_set;
7369 if (target_to_host_timespec(timeout_ts, arg3)) {
7370 unlock_user(target_pfd, arg1, 0);
7378 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
7380 unlock_user(target_pfd, arg1, 0);
7383 target_to_host_sigset(set, target_set);
7388 ret = get_errno(sys_ppoll(pfd, nfds, timeout_ts, set, _NSIG/8));
7390 if (!is_error(ret) && arg3) {
7391 host_to_target_timespec(arg3, timeout_ts);
7394 unlock_user(target_set, arg4, 0);
7398 ret = get_errno(poll(pfd, nfds, timeout));
7400 if (!is_error(ret)) {
7401 for(i = 0; i < nfds; i++) {
7402 target_pfd[i].revents = tswap16(pfd[i].revents);
7405 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
7409 case TARGET_NR_flock:
7410 /* NOTE: the flock constant seems to be the same for every
7412 ret = get_errno(flock(arg1, arg2));
7414 case TARGET_NR_readv:
7416 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
7418 ret = get_errno(readv(arg1, vec, arg3));
7419 unlock_iovec(vec, arg2, arg3, 1);
7421 ret = -host_to_target_errno(errno);
7425 case TARGET_NR_writev:
7427 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
7429 ret = get_errno(writev(arg1, vec, arg3));
7430 unlock_iovec(vec, arg2, arg3, 0);
7432 ret = -host_to_target_errno(errno);
7436 case TARGET_NR_getsid:
7437 ret = get_errno(getsid(arg1));
7439 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
7440 case TARGET_NR_fdatasync:
7441 ret = get_errno(fdatasync(arg1));
7444 case TARGET_NR__sysctl:
7445 /* We don't implement this, but ENOTDIR is always a safe
7447 ret = -TARGET_ENOTDIR;
7449 case TARGET_NR_sched_getaffinity:
7451 unsigned int mask_size;
7452 unsigned long *mask;
7455 * sched_getaffinity needs multiples of ulong, so need to take
7456 * care of mismatches between target ulong and host ulong sizes.
7458 if (arg2 & (sizeof(abi_ulong) - 1)) {
7459 ret = -TARGET_EINVAL;
7462 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7464 mask = alloca(mask_size);
7465 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
7467 if (!is_error(ret)) {
7468 if (copy_to_user(arg3, mask, ret)) {
7474 case TARGET_NR_sched_setaffinity:
7476 unsigned int mask_size;
7477 unsigned long *mask;
7480 * sched_setaffinity needs multiples of ulong, so need to take
7481 * care of mismatches between target ulong and host ulong sizes.
7483 if (arg2 & (sizeof(abi_ulong) - 1)) {
7484 ret = -TARGET_EINVAL;
7487 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
7489 mask = alloca(mask_size);
7490 if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
7493 memcpy(mask, p, arg2);
7494 unlock_user_struct(p, arg2, 0);
7496 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
7499 case TARGET_NR_sched_setparam:
7501 struct sched_param *target_schp;
7502 struct sched_param schp;
7504 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
7506 schp.sched_priority = tswap32(target_schp->sched_priority);
7507 unlock_user_struct(target_schp, arg2, 0);
7508 ret = get_errno(sched_setparam(arg1, &schp));
7511 case TARGET_NR_sched_getparam:
7513 struct sched_param *target_schp;
7514 struct sched_param schp;
7515 ret = get_errno(sched_getparam(arg1, &schp));
7516 if (!is_error(ret)) {
7517 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
7519 target_schp->sched_priority = tswap32(schp.sched_priority);
7520 unlock_user_struct(target_schp, arg2, 1);
7524 case TARGET_NR_sched_setscheduler:
7526 struct sched_param *target_schp;
7527 struct sched_param schp;
7528 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
7530 schp.sched_priority = tswap32(target_schp->sched_priority);
7531 unlock_user_struct(target_schp, arg3, 0);
7532 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
7535 case TARGET_NR_sched_getscheduler:
7536 ret = get_errno(sched_getscheduler(arg1));
7538 case TARGET_NR_sched_yield:
7539 ret = get_errno(sched_yield());
7541 case TARGET_NR_sched_get_priority_max:
7542 ret = get_errno(sched_get_priority_max(arg1));
7544 case TARGET_NR_sched_get_priority_min:
7545 ret = get_errno(sched_get_priority_min(arg1));
7547 case TARGET_NR_sched_rr_get_interval:
7550 ret = get_errno(sched_rr_get_interval(arg1, &ts));
7551 if (!is_error(ret)) {
7552 host_to_target_timespec(arg2, &ts);
7556 case TARGET_NR_nanosleep:
7558 struct timespec req, rem;
7559 target_to_host_timespec(&req, arg1);
7560 ret = get_errno(nanosleep(&req, &rem));
7561 if (is_error(ret) && arg2) {
7562 host_to_target_timespec(arg2, &rem);
7566 #ifdef TARGET_NR_query_module
7567 case TARGET_NR_query_module:
7570 #ifdef TARGET_NR_nfsservctl
7571 case TARGET_NR_nfsservctl:
7574 case TARGET_NR_prctl:
7576 case PR_GET_PDEATHSIG:
7579 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
7580 if (!is_error(ret) && arg2
7581 && put_user_ual(deathsig, arg2)) {
7589 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
7593 ret = get_errno(prctl(arg1, (unsigned long)name,
7595 unlock_user(name, arg2, 16);
7600 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
7604 ret = get_errno(prctl(arg1, (unsigned long)name,
7606 unlock_user(name, arg2, 0);
7611 /* Most prctl options have no pointer arguments */
7612 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
7616 #ifdef TARGET_NR_arch_prctl
7617 case TARGET_NR_arch_prctl:
7618 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
7619 ret = do_arch_prctl(cpu_env, arg1, arg2);
7625 #ifdef TARGET_NR_pread64
7626 case TARGET_NR_pread64:
7627 if (regpairs_aligned(cpu_env)) {
7631 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7633 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
7634 unlock_user(p, arg2, ret);
7636 case TARGET_NR_pwrite64:
7637 if (regpairs_aligned(cpu_env)) {
7641 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7643 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
7644 unlock_user(p, arg2, 0);
7647 case TARGET_NR_getcwd:
7648 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
7650 ret = get_errno(sys_getcwd1(p, arg2));
7651 unlock_user(p, arg1, ret);
7653 case TARGET_NR_capget:
7655 case TARGET_NR_capset:
7657 case TARGET_NR_sigaltstack:
7658 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
7659 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
7660 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
7661 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
7667 #ifdef CONFIG_SENDFILE
7668 case TARGET_NR_sendfile:
7673 ret = get_user_sal(off, arg3);
7674 if (is_error(ret)) {
7679 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7680 if (!is_error(ret) && arg3) {
7681 abi_long ret2 = put_user_sal(off, arg3);
7682 if (is_error(ret2)) {
7688 #ifdef TARGET_NR_sendfile64
7689 case TARGET_NR_sendfile64:
7694 ret = get_user_s64(off, arg3);
7695 if (is_error(ret)) {
7700 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
7701 if (!is_error(ret) && arg3) {
7702 abi_long ret2 = put_user_s64(off, arg3);
7703 if (is_error(ret2)) {
7711 case TARGET_NR_sendfile:
7712 #ifdef TARGET_NR_sendfile64
7713 case TARGET_NR_sendfile64:
7718 #ifdef TARGET_NR_getpmsg
7719 case TARGET_NR_getpmsg:
7722 #ifdef TARGET_NR_putpmsg
7723 case TARGET_NR_putpmsg:
7726 #ifdef TARGET_NR_vfork
7727 case TARGET_NR_vfork:
7728 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
7732 #ifdef TARGET_NR_ugetrlimit
7733 case TARGET_NR_ugetrlimit:
7736 int resource = target_to_host_resource(arg1);
7737 ret = get_errno(getrlimit(resource, &rlim));
7738 if (!is_error(ret)) {
7739 struct target_rlimit *target_rlim;
7740 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
7742 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
7743 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
7744 unlock_user_struct(target_rlim, arg2, 1);
7749 #ifdef TARGET_NR_truncate64
7750 case TARGET_NR_truncate64:
7751 if (!(p = lock_user_string(arg1)))
7753 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
7754 unlock_user(p, arg1, 0);
7757 #ifdef TARGET_NR_ftruncate64
7758 case TARGET_NR_ftruncate64:
7759 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
7762 #ifdef TARGET_NR_stat64
7763 case TARGET_NR_stat64:
7764 if (!(p = lock_user_string(arg1)))
7766 ret = get_errno(stat(path(p), &st));
7767 unlock_user(p, arg1, 0);
7769 ret = host_to_target_stat64(cpu_env, arg2, &st);
7772 #ifdef TARGET_NR_lstat64
7773 case TARGET_NR_lstat64:
7774 if (!(p = lock_user_string(arg1)))
7776 ret = get_errno(lstat(path(p), &st));
7777 unlock_user(p, arg1, 0);
7779 ret = host_to_target_stat64(cpu_env, arg2, &st);
7782 #ifdef TARGET_NR_fstat64
7783 case TARGET_NR_fstat64:
7784 ret = get_errno(fstat(arg1, &st));
7786 ret = host_to_target_stat64(cpu_env, arg2, &st);
7789 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
7790 #ifdef TARGET_NR_fstatat64
7791 case TARGET_NR_fstatat64:
7793 #ifdef TARGET_NR_newfstatat
7794 case TARGET_NR_newfstatat:
7796 if (!(p = lock_user_string(arg2)))
7798 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
7800 ret = host_to_target_stat64(cpu_env, arg3, &st);
7803 case TARGET_NR_lchown:
7804 if (!(p = lock_user_string(arg1)))
7806 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
7807 unlock_user(p, arg1, 0);
7809 #ifdef TARGET_NR_getuid
7810 case TARGET_NR_getuid:
7811 ret = get_errno(high2lowuid(getuid()));
7814 #ifdef TARGET_NR_getgid
7815 case TARGET_NR_getgid:
7816 ret = get_errno(high2lowgid(getgid()));
7819 #ifdef TARGET_NR_geteuid
7820 case TARGET_NR_geteuid:
7821 ret = get_errno(high2lowuid(geteuid()));
7824 #ifdef TARGET_NR_getegid
7825 case TARGET_NR_getegid:
7826 ret = get_errno(high2lowgid(getegid()));
7829 case TARGET_NR_setreuid:
7830 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
7832 case TARGET_NR_setregid:
7833 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
7835 case TARGET_NR_getgroups:
7837 int gidsetsize = arg1;
7838 target_id *target_grouplist;
7842 grouplist = alloca(gidsetsize * sizeof(gid_t));
7843 ret = get_errno(getgroups(gidsetsize, grouplist));
7844 if (gidsetsize == 0)
7846 if (!is_error(ret)) {
7847 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
7848 if (!target_grouplist)
7850 for(i = 0;i < ret; i++)
7851 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
7852 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
7856 case TARGET_NR_setgroups:
7858 int gidsetsize = arg1;
7859 target_id *target_grouplist;
7860 gid_t *grouplist = NULL;
7863 grouplist = alloca(gidsetsize * sizeof(gid_t));
7864 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
7865 if (!target_grouplist) {
7866 ret = -TARGET_EFAULT;
7869 for (i = 0; i < gidsetsize; i++) {
7870 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
7872 unlock_user(target_grouplist, arg2, 0);
7874 ret = get_errno(setgroups(gidsetsize, grouplist));
7877 case TARGET_NR_fchown:
7878 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
7880 #if defined(TARGET_NR_fchownat)
7881 case TARGET_NR_fchownat:
7882 if (!(p = lock_user_string(arg2)))
7884 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
7885 low2highgid(arg4), arg5));
7886 unlock_user(p, arg2, 0);
7889 #ifdef TARGET_NR_setresuid
7890 case TARGET_NR_setresuid:
7891 ret = get_errno(setresuid(low2highuid(arg1),
7893 low2highuid(arg3)));
7896 #ifdef TARGET_NR_getresuid
7897 case TARGET_NR_getresuid:
7899 uid_t ruid, euid, suid;
7900 ret = get_errno(getresuid(&ruid, &euid, &suid));
7901 if (!is_error(ret)) {
7902 if (put_user_u16(high2lowuid(ruid), arg1)
7903 || put_user_u16(high2lowuid(euid), arg2)
7904 || put_user_u16(high2lowuid(suid), arg3))
7910 #ifdef TARGET_NR_getresgid
7911 case TARGET_NR_setresgid:
7912 ret = get_errno(setresgid(low2highgid(arg1),
7914 low2highgid(arg3)));
7917 #ifdef TARGET_NR_getresgid
7918 case TARGET_NR_getresgid:
7920 gid_t rgid, egid, sgid;
7921 ret = get_errno(getresgid(&rgid, &egid, &sgid));
7922 if (!is_error(ret)) {
7923 if (put_user_u16(high2lowgid(rgid), arg1)
7924 || put_user_u16(high2lowgid(egid), arg2)
7925 || put_user_u16(high2lowgid(sgid), arg3))
7931 case TARGET_NR_chown:
7932 if (!(p = lock_user_string(arg1)))
7934 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
7935 unlock_user(p, arg1, 0);
7937 case TARGET_NR_setuid:
7938 ret = get_errno(setuid(low2highuid(arg1)));
7940 case TARGET_NR_setgid:
7941 ret = get_errno(setgid(low2highgid(arg1)));
7943 case TARGET_NR_setfsuid:
7944 ret = get_errno(setfsuid(arg1));
7946 case TARGET_NR_setfsgid:
7947 ret = get_errno(setfsgid(arg1));
7950 #ifdef TARGET_NR_lchown32
7951 case TARGET_NR_lchown32:
7952 if (!(p = lock_user_string(arg1)))
7954 ret = get_errno(lchown(p, arg2, arg3));
7955 unlock_user(p, arg1, 0);
7958 #ifdef TARGET_NR_getuid32
7959 case TARGET_NR_getuid32:
7960 ret = get_errno(getuid());
7964 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7965 /* Alpha specific */
7966 case TARGET_NR_getxuid:
7970 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
7972 ret = get_errno(getuid());
7975 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
7976 /* Alpha specific */
7977 case TARGET_NR_getxgid:
7981 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
7983 ret = get_errno(getgid());
7986 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
7987 /* Alpha specific */
7988 case TARGET_NR_osf_getsysinfo:
7989 ret = -TARGET_EOPNOTSUPP;
7991 case TARGET_GSI_IEEE_FP_CONTROL:
7993 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
7995 /* Copied from linux ieee_fpcr_to_swcr. */
7996 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
7997 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
7998 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
7999 | SWCR_TRAP_ENABLE_DZE
8000 | SWCR_TRAP_ENABLE_OVF);
8001 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
8002 | SWCR_TRAP_ENABLE_INE);
8003 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
8004 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
8006 if (put_user_u64 (swcr, arg2))
8012 /* case GSI_IEEE_STATE_AT_SIGNAL:
8013 -- Not implemented in linux kernel.
8015 -- Retrieves current unaligned access state; not much used.
8017 -- Retrieves implver information; surely not used.
8019 -- Grabs a copy of the HWRPB; surely not used.
8024 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8025 /* Alpha specific */
8026 case TARGET_NR_osf_setsysinfo:
8027 ret = -TARGET_EOPNOTSUPP;
8029 case TARGET_SSI_IEEE_FP_CONTROL:
8031 uint64_t swcr, fpcr, orig_fpcr;
8033 if (get_user_u64 (swcr, arg2)) {
8036 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8037 fpcr = orig_fpcr & FPCR_DYN_MASK;
8039 /* Copied from linux ieee_swcr_to_fpcr. */
8040 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
8041 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
8042 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
8043 | SWCR_TRAP_ENABLE_DZE
8044 | SWCR_TRAP_ENABLE_OVF)) << 48;
8045 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
8046 | SWCR_TRAP_ENABLE_INE)) << 57;
8047 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
8048 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
8050 cpu_alpha_store_fpcr(cpu_env, fpcr);
8055 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
8057 uint64_t exc, fpcr, orig_fpcr;
8060 if (get_user_u64(exc, arg2)) {
8064 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
8066 /* We only add to the exception status here. */
8067 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
8069 cpu_alpha_store_fpcr(cpu_env, fpcr);
8072 /* Old exceptions are not signaled. */
8073 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
8075 /* If any exceptions set by this call,
8076 and are unmasked, send a signal. */
8078 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
8079 si_code = TARGET_FPE_FLTRES;
8081 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
8082 si_code = TARGET_FPE_FLTUND;
8084 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
8085 si_code = TARGET_FPE_FLTOVF;
8087 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
8088 si_code = TARGET_FPE_FLTDIV;
8090 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
8091 si_code = TARGET_FPE_FLTINV;
8094 target_siginfo_t info;
8095 info.si_signo = SIGFPE;
8097 info.si_code = si_code;
8098 info._sifields._sigfault._addr
8099 = ((CPUArchState *)cpu_env)->pc;
8100 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
8105 /* case SSI_NVPAIRS:
8106 -- Used with SSIN_UACPROC to enable unaligned accesses.
8107 case SSI_IEEE_STATE_AT_SIGNAL:
8108 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8109 -- Not implemented in linux kernel
8114 #ifdef TARGET_NR_osf_sigprocmask
8115 /* Alpha specific. */
8116 case TARGET_NR_osf_sigprocmask:
8120 sigset_t set, oldset;
8123 case TARGET_SIG_BLOCK:
8126 case TARGET_SIG_UNBLOCK:
8129 case TARGET_SIG_SETMASK:
8133 ret = -TARGET_EINVAL;
8137 target_to_host_old_sigset(&set, &mask);
8138 sigprocmask(how, &set, &oldset);
8139 host_to_target_old_sigset(&mask, &oldset);
8145 #ifdef TARGET_NR_getgid32
8146 case TARGET_NR_getgid32:
8147 ret = get_errno(getgid());
8150 #ifdef TARGET_NR_geteuid32
8151 case TARGET_NR_geteuid32:
8152 ret = get_errno(geteuid());
8155 #ifdef TARGET_NR_getegid32
8156 case TARGET_NR_getegid32:
8157 ret = get_errno(getegid());
8160 #ifdef TARGET_NR_setreuid32
8161 case TARGET_NR_setreuid32:
8162 ret = get_errno(setreuid(arg1, arg2));
8165 #ifdef TARGET_NR_setregid32
8166 case TARGET_NR_setregid32:
8167 ret = get_errno(setregid(arg1, arg2));
8170 #ifdef TARGET_NR_getgroups32
8171 case TARGET_NR_getgroups32:
8173 int gidsetsize = arg1;
8174 uint32_t *target_grouplist;
8178 grouplist = alloca(gidsetsize * sizeof(gid_t));
8179 ret = get_errno(getgroups(gidsetsize, grouplist));
8180 if (gidsetsize == 0)
8182 if (!is_error(ret)) {
8183 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
8184 if (!target_grouplist) {
8185 ret = -TARGET_EFAULT;
8188 for(i = 0;i < ret; i++)
8189 target_grouplist[i] = tswap32(grouplist[i]);
8190 unlock_user(target_grouplist, arg2, gidsetsize * 4);
8195 #ifdef TARGET_NR_setgroups32
8196 case TARGET_NR_setgroups32:
8198 int gidsetsize = arg1;
8199 uint32_t *target_grouplist;
8203 grouplist = alloca(gidsetsize * sizeof(gid_t));
8204 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
8205 if (!target_grouplist) {
8206 ret = -TARGET_EFAULT;
8209 for(i = 0;i < gidsetsize; i++)
8210 grouplist[i] = tswap32(target_grouplist[i]);
8211 unlock_user(target_grouplist, arg2, 0);
8212 ret = get_errno(setgroups(gidsetsize, grouplist));
8216 #ifdef TARGET_NR_fchown32
8217 case TARGET_NR_fchown32:
8218 ret = get_errno(fchown(arg1, arg2, arg3));
8221 #ifdef TARGET_NR_setresuid32
8222 case TARGET_NR_setresuid32:
8223 ret = get_errno(setresuid(arg1, arg2, arg3));
8226 #ifdef TARGET_NR_getresuid32
8227 case TARGET_NR_getresuid32:
8229 uid_t ruid, euid, suid;
8230 ret = get_errno(getresuid(&ruid, &euid, &suid));
8231 if (!is_error(ret)) {
8232 if (put_user_u32(ruid, arg1)
8233 || put_user_u32(euid, arg2)
8234 || put_user_u32(suid, arg3))
8240 #ifdef TARGET_NR_setresgid32
8241 case TARGET_NR_setresgid32:
8242 ret = get_errno(setresgid(arg1, arg2, arg3));
8245 #ifdef TARGET_NR_getresgid32
8246 case TARGET_NR_getresgid32:
8248 gid_t rgid, egid, sgid;
8249 ret = get_errno(getresgid(&rgid, &egid, &sgid));
8250 if (!is_error(ret)) {
8251 if (put_user_u32(rgid, arg1)
8252 || put_user_u32(egid, arg2)
8253 || put_user_u32(sgid, arg3))
8259 #ifdef TARGET_NR_chown32
8260 case TARGET_NR_chown32:
8261 if (!(p = lock_user_string(arg1)))
8263 ret = get_errno(chown(p, arg2, arg3));
8264 unlock_user(p, arg1, 0);
8267 #ifdef TARGET_NR_setuid32
8268 case TARGET_NR_setuid32:
8269 ret = get_errno(setuid(arg1));
8272 #ifdef TARGET_NR_setgid32
8273 case TARGET_NR_setgid32:
8274 ret = get_errno(setgid(arg1));
8277 #ifdef TARGET_NR_setfsuid32
8278 case TARGET_NR_setfsuid32:
8279 ret = get_errno(setfsuid(arg1));
8282 #ifdef TARGET_NR_setfsgid32
8283 case TARGET_NR_setfsgid32:
8284 ret = get_errno(setfsgid(arg1));
8288 case TARGET_NR_pivot_root:
8290 #ifdef TARGET_NR_mincore
8291 case TARGET_NR_mincore:
8294 ret = -TARGET_EFAULT;
8295 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
8297 if (!(p = lock_user_string(arg3)))
8299 ret = get_errno(mincore(a, arg2, p));
8300 unlock_user(p, arg3, ret);
8302 unlock_user(a, arg1, 0);
8306 #ifdef TARGET_NR_arm_fadvise64_64
8307 case TARGET_NR_arm_fadvise64_64:
8310 * arm_fadvise64_64 looks like fadvise64_64 but
8311 * with different argument order
8319 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
8320 #ifdef TARGET_NR_fadvise64_64
8321 case TARGET_NR_fadvise64_64:
8323 #ifdef TARGET_NR_fadvise64
8324 case TARGET_NR_fadvise64:
8328 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
8329 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
8330 case 6: arg4 = POSIX_FADV_DONTNEED; break;
8331 case 7: arg4 = POSIX_FADV_NOREUSE; break;
8335 ret = -posix_fadvise(arg1, arg2, arg3, arg4);
8338 #ifdef TARGET_NR_madvise
8339 case TARGET_NR_madvise:
8340 /* A straight passthrough may not be safe because qemu sometimes
8341 turns private file-backed mappings into anonymous mappings.
8342 This will break MADV_DONTNEED.
8343 This is a hint, so ignoring and returning success is ok. */
8347 #if TARGET_ABI_BITS == 32
8348 case TARGET_NR_fcntl64:
8352 struct target_flock64 *target_fl;
8354 struct target_eabi_flock64 *target_efl;
8357 cmd = target_to_host_fcntl_cmd(arg2);
8358 if (cmd == -TARGET_EINVAL) {
8364 case TARGET_F_GETLK64:
8366 if (((CPUARMState *)cpu_env)->eabi) {
8367 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8369 fl.l_type = tswap16(target_efl->l_type);
8370 fl.l_whence = tswap16(target_efl->l_whence);
8371 fl.l_start = tswap64(target_efl->l_start);
8372 fl.l_len = tswap64(target_efl->l_len);
8373 fl.l_pid = tswap32(target_efl->l_pid);
8374 unlock_user_struct(target_efl, arg3, 0);
8378 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8380 fl.l_type = tswap16(target_fl->l_type);
8381 fl.l_whence = tswap16(target_fl->l_whence);
8382 fl.l_start = tswap64(target_fl->l_start);
8383 fl.l_len = tswap64(target_fl->l_len);
8384 fl.l_pid = tswap32(target_fl->l_pid);
8385 unlock_user_struct(target_fl, arg3, 0);
8387 ret = get_errno(fcntl(arg1, cmd, &fl));
8390 if (((CPUARMState *)cpu_env)->eabi) {
8391 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
8393 target_efl->l_type = tswap16(fl.l_type);
8394 target_efl->l_whence = tswap16(fl.l_whence);
8395 target_efl->l_start = tswap64(fl.l_start);
8396 target_efl->l_len = tswap64(fl.l_len);
8397 target_efl->l_pid = tswap32(fl.l_pid);
8398 unlock_user_struct(target_efl, arg3, 1);
8402 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
8404 target_fl->l_type = tswap16(fl.l_type);
8405 target_fl->l_whence = tswap16(fl.l_whence);
8406 target_fl->l_start = tswap64(fl.l_start);
8407 target_fl->l_len = tswap64(fl.l_len);
8408 target_fl->l_pid = tswap32(fl.l_pid);
8409 unlock_user_struct(target_fl, arg3, 1);
8414 case TARGET_F_SETLK64:
8415 case TARGET_F_SETLKW64:
8417 if (((CPUARMState *)cpu_env)->eabi) {
8418 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
8420 fl.l_type = tswap16(target_efl->l_type);
8421 fl.l_whence = tswap16(target_efl->l_whence);
8422 fl.l_start = tswap64(target_efl->l_start);
8423 fl.l_len = tswap64(target_efl->l_len);
8424 fl.l_pid = tswap32(target_efl->l_pid);
8425 unlock_user_struct(target_efl, arg3, 0);
8429 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
8431 fl.l_type = tswap16(target_fl->l_type);
8432 fl.l_whence = tswap16(target_fl->l_whence);
8433 fl.l_start = tswap64(target_fl->l_start);
8434 fl.l_len = tswap64(target_fl->l_len);
8435 fl.l_pid = tswap32(target_fl->l_pid);
8436 unlock_user_struct(target_fl, arg3, 0);
8438 ret = get_errno(fcntl(arg1, cmd, &fl));
8441 ret = do_fcntl(arg1, arg2, arg3);
8447 #ifdef TARGET_NR_cacheflush
8448 case TARGET_NR_cacheflush:
8449 /* self-modifying code is handled automatically, so nothing needed */
8453 #ifdef TARGET_NR_security
8454 case TARGET_NR_security:
8457 #ifdef TARGET_NR_getpagesize
8458 case TARGET_NR_getpagesize:
8459 ret = TARGET_PAGE_SIZE;
8462 case TARGET_NR_gettid:
8463 ret = get_errno(gettid());
8465 #ifdef TARGET_NR_readahead
8466 case TARGET_NR_readahead:
8467 #if TARGET_ABI_BITS == 32
8468 if (regpairs_aligned(cpu_env)) {
8473 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
8475 ret = get_errno(readahead(arg1, arg2, arg3));
8480 #ifdef TARGET_NR_setxattr
8481 case TARGET_NR_listxattr:
8482 case TARGET_NR_llistxattr:
8486 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8488 ret = -TARGET_EFAULT;
8492 p = lock_user_string(arg1);
8494 if (num == TARGET_NR_listxattr) {
8495 ret = get_errno(listxattr(p, b, arg3));
8497 ret = get_errno(llistxattr(p, b, arg3));
8500 ret = -TARGET_EFAULT;
8502 unlock_user(p, arg1, 0);
8503 unlock_user(b, arg2, arg3);
8506 case TARGET_NR_flistxattr:
8510 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
8512 ret = -TARGET_EFAULT;
8516 ret = get_errno(flistxattr(arg1, b, arg3));
8517 unlock_user(b, arg2, arg3);
8520 case TARGET_NR_setxattr:
8521 case TARGET_NR_lsetxattr:
8523 void *p, *n, *v = 0;
8525 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8527 ret = -TARGET_EFAULT;
8531 p = lock_user_string(arg1);
8532 n = lock_user_string(arg2);
8534 if (num == TARGET_NR_setxattr) {
8535 ret = get_errno(setxattr(p, n, v, arg4, arg5));
8537 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
8540 ret = -TARGET_EFAULT;
8542 unlock_user(p, arg1, 0);
8543 unlock_user(n, arg2, 0);
8544 unlock_user(v, arg3, 0);
8547 case TARGET_NR_fsetxattr:
8551 v = lock_user(VERIFY_READ, arg3, arg4, 1);
8553 ret = -TARGET_EFAULT;
8557 n = lock_user_string(arg2);
8559 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
8561 ret = -TARGET_EFAULT;
8563 unlock_user(n, arg2, 0);
8564 unlock_user(v, arg3, 0);
8567 case TARGET_NR_getxattr:
8568 case TARGET_NR_lgetxattr:
8570 void *p, *n, *v = 0;
8572 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8574 ret = -TARGET_EFAULT;
8578 p = lock_user_string(arg1);
8579 n = lock_user_string(arg2);
8581 if (num == TARGET_NR_getxattr) {
8582 ret = get_errno(getxattr(p, n, v, arg4));
8584 ret = get_errno(lgetxattr(p, n, v, arg4));
8587 ret = -TARGET_EFAULT;
8589 unlock_user(p, arg1, 0);
8590 unlock_user(n, arg2, 0);
8591 unlock_user(v, arg3, arg4);
8594 case TARGET_NR_fgetxattr:
8598 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
8600 ret = -TARGET_EFAULT;
8604 n = lock_user_string(arg2);
8606 ret = get_errno(fgetxattr(arg1, n, v, arg4));
8608 ret = -TARGET_EFAULT;
8610 unlock_user(n, arg2, 0);
8611 unlock_user(v, arg3, arg4);
8614 case TARGET_NR_removexattr:
8615 case TARGET_NR_lremovexattr:
8618 p = lock_user_string(arg1);
8619 n = lock_user_string(arg2);
8621 if (num == TARGET_NR_removexattr) {
8622 ret = get_errno(removexattr(p, n));
8624 ret = get_errno(lremovexattr(p, n));
8627 ret = -TARGET_EFAULT;
8629 unlock_user(p, arg1, 0);
8630 unlock_user(n, arg2, 0);
8633 case TARGET_NR_fremovexattr:
8636 n = lock_user_string(arg2);
8638 ret = get_errno(fremovexattr(arg1, n));
8640 ret = -TARGET_EFAULT;
8642 unlock_user(n, arg2, 0);
8646 #endif /* CONFIG_ATTR */
8647 #ifdef TARGET_NR_set_thread_area
8648 case TARGET_NR_set_thread_area:
8649 #if defined(TARGET_MIPS)
8650 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
8653 #elif defined(TARGET_CRIS)
8655 ret = -TARGET_EINVAL;
8657 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
8661 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
8662 ret = do_set_thread_area(cpu_env, arg1);
8664 #elif defined(TARGET_M68K)
8666 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
8667 ts->tp_value = arg1;
8672 goto unimplemented_nowarn;
8675 #ifdef TARGET_NR_get_thread_area
8676 case TARGET_NR_get_thread_area:
8677 #if defined(TARGET_I386) && defined(TARGET_ABI32)
8678 ret = do_get_thread_area(cpu_env, arg1);
8680 #elif defined(TARGET_M68K)
8682 TaskState *ts = ((CPUArchState *)cpu_env)->opaque;
8687 goto unimplemented_nowarn;
8690 #ifdef TARGET_NR_getdomainname
8691 case TARGET_NR_getdomainname:
8692 goto unimplemented_nowarn;
8695 #ifdef TARGET_NR_clock_gettime
8696 case TARGET_NR_clock_gettime:
8699 ret = get_errno(clock_gettime(arg1, &ts));
8700 if (!is_error(ret)) {
8701 host_to_target_timespec(arg2, &ts);
8706 #ifdef TARGET_NR_clock_getres
8707 case TARGET_NR_clock_getres:
8710 ret = get_errno(clock_getres(arg1, &ts));
8711 if (!is_error(ret)) {
8712 host_to_target_timespec(arg2, &ts);
8717 #ifdef TARGET_NR_clock_nanosleep
8718 case TARGET_NR_clock_nanosleep:
8721 target_to_host_timespec(&ts, arg3);
8722 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
8724 host_to_target_timespec(arg4, &ts);
8729 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8730 case TARGET_NR_set_tid_address:
8731 ret = get_errno(set_tid_address((int *)g2h(arg1)));
8735 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
8736 case TARGET_NR_tkill:
8737 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
8741 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
8742 case TARGET_NR_tgkill:
8743 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
8744 target_to_host_signal(arg3)));
8748 #ifdef TARGET_NR_set_robust_list
8749 case TARGET_NR_set_robust_list:
8750 case TARGET_NR_get_robust_list:
8751 /* The ABI for supporting robust futexes has userspace pass
8752 * the kernel a pointer to a linked list which is updated by
8753 * userspace after the syscall; the list is walked by the kernel
8754 * when the thread exits. Since the linked list in QEMU guest
8755 * memory isn't a valid linked list for the host and we have
8756 * no way to reliably intercept the thread-death event, we can't
8757 * support these. Silently return ENOSYS so that guest userspace
8758 * falls back to a non-robust futex implementation (which should
8759 * be OK except in the corner case of the guest crashing while
8760 * holding a mutex that is shared with another process via
8763 goto unimplemented_nowarn;
8766 #if defined(TARGET_NR_utimensat)
8767 case TARGET_NR_utimensat:
8769 struct timespec *tsp, ts[2];
8773 target_to_host_timespec(ts, arg3);
8774 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
8778 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
8780 if (!(p = lock_user_string(arg2))) {
8781 ret = -TARGET_EFAULT;
8784 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
8785 unlock_user(p, arg2, 0);
8790 case TARGET_NR_futex:
8791 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
8793 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
8794 case TARGET_NR_inotify_init:
8795 ret = get_errno(sys_inotify_init());
8798 #ifdef CONFIG_INOTIFY1
8799 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8800 case TARGET_NR_inotify_init1:
8801 ret = get_errno(sys_inotify_init1(arg1));
8805 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
8806 case TARGET_NR_inotify_add_watch:
8807 p = lock_user_string(arg2);
8808 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
8809 unlock_user(p, arg2, 0);
8812 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
8813 case TARGET_NR_inotify_rm_watch:
8814 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
8818 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
8819 case TARGET_NR_mq_open:
8821 struct mq_attr posix_mq_attr;
8823 p = lock_user_string(arg1 - 1);
8825 copy_from_user_mq_attr (&posix_mq_attr, arg4);
8826 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
8827 unlock_user (p, arg1, 0);
8831 case TARGET_NR_mq_unlink:
8832 p = lock_user_string(arg1 - 1);
8833 ret = get_errno(mq_unlink(p));
8834 unlock_user (p, arg1, 0);
8837 case TARGET_NR_mq_timedsend:
8841 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8843 target_to_host_timespec(&ts, arg5);
8844 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
8845 host_to_target_timespec(arg5, &ts);
8848 ret = get_errno(mq_send(arg1, p, arg3, arg4));
8849 unlock_user (p, arg2, arg3);
8853 case TARGET_NR_mq_timedreceive:
8858 p = lock_user (VERIFY_READ, arg2, arg3, 1);
8860 target_to_host_timespec(&ts, arg5);
8861 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
8862 host_to_target_timespec(arg5, &ts);
8865 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
8866 unlock_user (p, arg2, arg3);
8868 put_user_u32(prio, arg4);
8872 /* Not implemented for now... */
8873 /* case TARGET_NR_mq_notify: */
8876 case TARGET_NR_mq_getsetattr:
8878 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
8881 ret = mq_getattr(arg1, &posix_mq_attr_out);
8882 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
8885 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
8886 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
8893 #ifdef CONFIG_SPLICE
8894 #ifdef TARGET_NR_tee
8897 ret = get_errno(tee(arg1,arg2,arg3,arg4));
8901 #ifdef TARGET_NR_splice
8902 case TARGET_NR_splice:
8904 loff_t loff_in, loff_out;
8905 loff_t *ploff_in = NULL, *ploff_out = NULL;
8907 get_user_u64(loff_in, arg2);
8908 ploff_in = &loff_in;
8911 get_user_u64(loff_out, arg2);
8912 ploff_out = &loff_out;
8914 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
8918 #ifdef TARGET_NR_vmsplice
8919 case TARGET_NR_vmsplice:
8921 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
8923 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
8924 unlock_iovec(vec, arg2, arg3, 0);
8926 ret = -host_to_target_errno(errno);
8931 #endif /* CONFIG_SPLICE */
8932 #ifdef CONFIG_EVENTFD
8933 #if defined(TARGET_NR_eventfd)
8934 case TARGET_NR_eventfd:
8935 ret = get_errno(eventfd(arg1, 0));
8938 #if defined(TARGET_NR_eventfd2)
8939 case TARGET_NR_eventfd2:
8941 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
8942 if (arg2 & TARGET_O_NONBLOCK) {
8943 host_flags |= O_NONBLOCK;
8945 if (arg2 & TARGET_O_CLOEXEC) {
8946 host_flags |= O_CLOEXEC;
8948 ret = get_errno(eventfd(arg1, host_flags));
8952 #endif /* CONFIG_EVENTFD */
8953 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8954 case TARGET_NR_fallocate:
8955 #if TARGET_ABI_BITS == 32
8956 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
8957 target_offset64(arg5, arg6)));
8959 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
8963 #if defined(CONFIG_SYNC_FILE_RANGE)
8964 #if defined(TARGET_NR_sync_file_range)
8965 case TARGET_NR_sync_file_range:
8966 #if TARGET_ABI_BITS == 32
8967 #if defined(TARGET_MIPS)
8968 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8969 target_offset64(arg5, arg6), arg7));
8971 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
8972 target_offset64(arg4, arg5), arg6));
8973 #endif /* !TARGET_MIPS */
8975 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
8979 #if defined(TARGET_NR_sync_file_range2)
8980 case TARGET_NR_sync_file_range2:
8981 /* This is like sync_file_range but the arguments are reordered */
8982 #if TARGET_ABI_BITS == 32
8983 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
8984 target_offset64(arg5, arg6), arg2));
8986 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
8991 #if defined(CONFIG_EPOLL)
8992 #if defined(TARGET_NR_epoll_create)
8993 case TARGET_NR_epoll_create:
8994 ret = get_errno(epoll_create(arg1));
8997 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
8998 case TARGET_NR_epoll_create1:
8999 ret = get_errno(epoll_create1(arg1));
9002 #if defined(TARGET_NR_epoll_ctl)
9003 case TARGET_NR_epoll_ctl:
9005 struct epoll_event ep;
9006 struct epoll_event *epp = 0;
9008 struct target_epoll_event *target_ep;
9009 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
9012 ep.events = tswap32(target_ep->events);
9013 /* The epoll_data_t union is just opaque data to the kernel,
9014 * so we transfer all 64 bits across and need not worry what
9015 * actual data type it is.
9017 ep.data.u64 = tswap64(target_ep->data.u64);
9018 unlock_user_struct(target_ep, arg4, 0);
9021 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
9026 #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9027 #define IMPLEMENT_EPOLL_PWAIT
9029 #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9030 #if defined(TARGET_NR_epoll_wait)
9031 case TARGET_NR_epoll_wait:
9033 #if defined(IMPLEMENT_EPOLL_PWAIT)
9034 case TARGET_NR_epoll_pwait:
9037 struct target_epoll_event *target_ep;
9038 struct epoll_event *ep;
9040 int maxevents = arg3;
9043 target_ep = lock_user(VERIFY_WRITE, arg2,
9044 maxevents * sizeof(struct target_epoll_event), 1);
9049 ep = alloca(maxevents * sizeof(struct epoll_event));
9052 #if defined(IMPLEMENT_EPOLL_PWAIT)
9053 case TARGET_NR_epoll_pwait:
9055 target_sigset_t *target_set;
9056 sigset_t _set, *set = &_set;
9059 target_set = lock_user(VERIFY_READ, arg5,
9060 sizeof(target_sigset_t), 1);
9062 unlock_user(target_ep, arg2, 0);
9065 target_to_host_sigset(set, target_set);
9066 unlock_user(target_set, arg5, 0);
9071 ret = get_errno(epoll_pwait(epfd, ep, maxevents, timeout, set));
9075 #if defined(TARGET_NR_epoll_wait)
9076 case TARGET_NR_epoll_wait:
9077 ret = get_errno(epoll_wait(epfd, ep, maxevents, timeout));
9081 ret = -TARGET_ENOSYS;
9083 if (!is_error(ret)) {
9085 for (i = 0; i < ret; i++) {
9086 target_ep[i].events = tswap32(ep[i].events);
9087 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
9090 unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
9095 #ifdef TARGET_NR_prlimit64
9096 case TARGET_NR_prlimit64:
9098 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9099 struct target_rlimit64 *target_rnew, *target_rold;
9100 struct host_rlimit64 rnew, rold, *rnewp = 0;
9102 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
9105 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
9106 rnew.rlim_max = tswap64(target_rnew->rlim_max);
9107 unlock_user_struct(target_rnew, arg3, 0);
9111 ret = get_errno(sys_prlimit64(arg1, arg2, rnewp, arg4 ? &rold : 0));
9112 if (!is_error(ret) && arg4) {
9113 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
9116 target_rold->rlim_cur = tswap64(rold.rlim_cur);
9117 target_rold->rlim_max = tswap64(rold.rlim_max);
9118 unlock_user_struct(target_rold, arg4, 1);
9123 #ifdef TARGET_NR_gethostname
9124 case TARGET_NR_gethostname:
9126 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
9128 ret = get_errno(gethostname(name, arg2));
9129 unlock_user(name, arg1, arg2);
9131 ret = -TARGET_EFAULT;
9136 #ifdef TARGET_NR_atomic_cmpxchg_32
9137 case TARGET_NR_atomic_cmpxchg_32:
9139 /* should use start_exclusive from main.c */
9140 abi_ulong mem_value;
9141 if (get_user_u32(mem_value, arg6)) {
9142 target_siginfo_t info;
9143 info.si_signo = SIGSEGV;
9145 info.si_code = TARGET_SEGV_MAPERR;
9146 info._sifields._sigfault._addr = arg6;
9147 queue_signal((CPUArchState *)cpu_env, info.si_signo, &info);
9151 if (mem_value == arg2)
9152 put_user_u32(arg1, arg6);
9157 #ifdef TARGET_NR_atomic_barrier
9158 case TARGET_NR_atomic_barrier:
9160 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
9166 gemu_log("qemu: Unsupported syscall: %d\n", num);
9167 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
9168 unimplemented_nowarn:
9170 ret = -TARGET_ENOSYS;
9175 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
9178 print_syscall_ret(num, ret);
9181 ret = -TARGET_EFAULT;