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>
46 #include <linux/capability.h>
50 int __clone2(int (*fn
)(void *), void *child_stack_base
,
51 size_t stack_size
, int flags
, void *arg
, ...);
53 #include <sys/socket.h>
57 #include <sys/times.h>
60 #include <sys/statfs.h>
62 #include <sys/sysinfo.h>
63 #include <sys/utsname.h>
64 //#include <sys/user.h>
65 #include <netinet/ip.h>
66 #include <netinet/tcp.h>
67 #include <linux/wireless.h>
68 #include <linux/icmp.h>
69 #include "qemu-common.h"
74 #include <sys/eventfd.h>
77 #include <sys/epoll.h>
80 #include "qemu/xattr.h"
82 #ifdef CONFIG_SENDFILE
83 #include <sys/sendfile.h>
86 #define termios host_termios
87 #define winsize host_winsize
88 #define termio host_termio
89 #define sgttyb host_sgttyb /* same as target */
90 #define tchars host_tchars /* same as target */
91 #define ltchars host_ltchars /* same as target */
93 #include <linux/termios.h>
94 #include <linux/unistd.h>
95 #include <linux/utsname.h>
96 #include <linux/cdrom.h>
97 #include <linux/hdreg.h>
98 #include <linux/soundcard.h>
100 #include <linux/mtio.h>
101 #include <linux/fs.h>
102 #if defined(CONFIG_FIEMAP)
103 #include <linux/fiemap.h>
105 #include <linux/fb.h>
106 #include <linux/vt.h>
107 #include <linux/dm-ioctl.h>
108 #include <linux/reboot.h>
109 #include <linux/route.h>
110 #include <linux/filter.h>
111 #include <linux/blkpg.h>
112 #include "linux_loop.h"
113 #include "cpu-uname.h"
117 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
118 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
122 //#include <linux/msdos_fs.h>
123 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
124 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
135 #define _syscall0(type,name) \
136 static type name (void) \
138 return syscall(__NR_##name); \
141 #define _syscall1(type,name,type1,arg1) \
142 static type name (type1 arg1) \
144 return syscall(__NR_##name, arg1); \
147 #define _syscall2(type,name,type1,arg1,type2,arg2) \
148 static type name (type1 arg1,type2 arg2) \
150 return syscall(__NR_##name, arg1, arg2); \
153 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
154 static type name (type1 arg1,type2 arg2,type3 arg3) \
156 return syscall(__NR_##name, arg1, arg2, arg3); \
159 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
160 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
162 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
165 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
167 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
169 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
173 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
174 type5,arg5,type6,arg6) \
175 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
178 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
182 #define __NR_sys_uname __NR_uname
183 #define __NR_sys_getcwd1 __NR_getcwd
184 #define __NR_sys_getdents __NR_getdents
185 #define __NR_sys_getdents64 __NR_getdents64
186 #define __NR_sys_getpriority __NR_getpriority
187 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
188 #define __NR_sys_syslog __NR_syslog
189 #define __NR_sys_tgkill __NR_tgkill
190 #define __NR_sys_tkill __NR_tkill
191 #define __NR_sys_futex __NR_futex
192 #define __NR_sys_inotify_init __NR_inotify_init
193 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
194 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
196 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
198 #define __NR__llseek __NR_lseek
202 _syscall0(int, gettid
)
204 /* This is a replacement for the host gettid() and must return a host
206 static int gettid(void) {
211 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
213 #if !defined(__NR_getdents) || \
214 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
215 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
217 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
218 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
219 loff_t
*, res
, uint
, wh
);
221 _syscall3(int,sys_rt_sigqueueinfo
,int,pid
,int,sig
,siginfo_t
*,uinfo
)
222 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
223 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
224 _syscall3(int,sys_tgkill
,int,tgid
,int,pid
,int,sig
)
226 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
227 _syscall2(int,sys_tkill
,int,tid
,int,sig
)
229 #ifdef __NR_exit_group
230 _syscall1(int,exit_group
,int,error_code
)
232 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
233 _syscall1(int,set_tid_address
,int *,tidptr
)
235 #if defined(TARGET_NR_futex) && defined(__NR_futex)
236 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
237 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
239 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
240 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
241 unsigned long *, user_mask_ptr
);
242 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
243 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
244 unsigned long *, user_mask_ptr
);
245 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
247 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
248 struct __user_cap_data_struct
*, data
);
249 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
250 struct __user_cap_data_struct
*, data
);
252 static bitmask_transtbl fcntl_flags_tbl
[] = {
253 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
254 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
255 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
256 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
257 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
258 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
259 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
260 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
261 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
262 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
263 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
264 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
265 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
266 #if defined(O_DIRECT)
267 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
269 #if defined(O_NOATIME)
270 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
272 #if defined(O_CLOEXEC)
273 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
276 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
278 /* Don't terminate the list prematurely on 64-bit host+guest. */
279 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
280 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
285 #define COPY_UTSNAME_FIELD(dest, src) \
287 /* __NEW_UTS_LEN doesn't include terminating null */ \
288 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
289 (dest)[__NEW_UTS_LEN] = '\0'; \
292 static int sys_uname(struct new_utsname
*buf
)
294 struct utsname uts_buf
;
296 if (uname(&uts_buf
) < 0)
300 * Just in case these have some differences, we
301 * translate utsname to new_utsname (which is the
302 * struct linux kernel uses).
305 memset(buf
, 0, sizeof(*buf
));
306 COPY_UTSNAME_FIELD(buf
->sysname
, uts_buf
.sysname
);
307 COPY_UTSNAME_FIELD(buf
->nodename
, uts_buf
.nodename
);
308 COPY_UTSNAME_FIELD(buf
->release
, uts_buf
.release
);
309 COPY_UTSNAME_FIELD(buf
->version
, uts_buf
.version
);
310 COPY_UTSNAME_FIELD(buf
->machine
, uts_buf
.machine
);
312 COPY_UTSNAME_FIELD(buf
->domainname
, uts_buf
.domainname
);
316 #undef COPY_UTSNAME_FIELD
319 static int sys_getcwd1(char *buf
, size_t size
)
321 if (getcwd(buf
, size
) == NULL
) {
322 /* getcwd() sets errno */
325 return strlen(buf
)+1;
328 #ifdef TARGET_NR_openat
329 static int sys_openat(int dirfd
, const char *pathname
, int flags
, mode_t mode
)
332 * open(2) has extra parameter 'mode' when called with
335 if ((flags
& O_CREAT
) != 0) {
336 return (openat(dirfd
, pathname
, flags
, mode
));
338 return (openat(dirfd
, pathname
, flags
));
342 #ifdef TARGET_NR_utimensat
343 #ifdef CONFIG_UTIMENSAT
344 static int sys_utimensat(int dirfd
, const char *pathname
,
345 const struct timespec times
[2], int flags
)
347 if (pathname
== NULL
)
348 return futimens(dirfd
, times
);
350 return utimensat(dirfd
, pathname
, times
, flags
);
352 #elif defined(__NR_utimensat)
353 #define __NR_sys_utimensat __NR_utimensat
354 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
355 const struct timespec
*,tsp
,int,flags
)
357 static int sys_utimensat(int dirfd
, const char *pathname
,
358 const struct timespec times
[2], int flags
)
364 #endif /* TARGET_NR_utimensat */
366 #ifdef CONFIG_INOTIFY
367 #include <sys/inotify.h>
369 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
370 static int sys_inotify_init(void)
372 return (inotify_init());
375 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
376 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
378 return (inotify_add_watch(fd
, pathname
, mask
));
381 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
382 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
384 return (inotify_rm_watch(fd
, wd
));
387 #ifdef CONFIG_INOTIFY1
388 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
389 static int sys_inotify_init1(int flags
)
391 return (inotify_init1(flags
));
396 /* Userspace can usually survive runtime without inotify */
397 #undef TARGET_NR_inotify_init
398 #undef TARGET_NR_inotify_init1
399 #undef TARGET_NR_inotify_add_watch
400 #undef TARGET_NR_inotify_rm_watch
401 #endif /* CONFIG_INOTIFY */
403 #if defined(TARGET_NR_ppoll)
405 # define __NR_ppoll -1
407 #define __NR_sys_ppoll __NR_ppoll
408 _syscall5(int, sys_ppoll
, struct pollfd
*, fds
, nfds_t
, nfds
,
409 struct timespec
*, timeout
, const __sigset_t
*, sigmask
,
413 #if defined(TARGET_NR_pselect6)
414 #ifndef __NR_pselect6
415 # define __NR_pselect6 -1
417 #define __NR_sys_pselect6 __NR_pselect6
418 _syscall6(int, sys_pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
,
419 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
);
422 #if defined(TARGET_NR_prlimit64)
423 #ifndef __NR_prlimit64
424 # define __NR_prlimit64 -1
426 #define __NR_sys_prlimit64 __NR_prlimit64
427 /* The glibc rlimit structure may not be that used by the underlying syscall */
428 struct host_rlimit64
{
432 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
433 const struct host_rlimit64
*, new_limit
,
434 struct host_rlimit64
*, old_limit
)
438 #if defined(TARGET_NR_timer_create)
439 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
440 static timer_t g_posix_timers
[32] = { 0, } ;
442 static inline int next_free_host_timer(void)
445 /* FIXME: Does finding the next free slot require a lock? */
446 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
447 if (g_posix_timers
[k
] == 0) {
448 g_posix_timers
[k
] = (timer_t
) 1;
456 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
458 static inline int regpairs_aligned(void *cpu_env
) {
459 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
461 #elif defined(TARGET_MIPS)
462 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
463 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
464 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
465 * of registers which translates to the same as ARM/MIPS, because we start with
467 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
469 static inline int regpairs_aligned(void *cpu_env
) { return 0; }
472 #define ERRNO_TABLE_SIZE 1200
474 /* target_to_host_errno_table[] is initialized from
475 * host_to_target_errno_table[] in syscall_init(). */
476 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
480 * This list is the union of errno values overridden in asm-<arch>/errno.h
481 * minus the errnos that are not actually generic to all archs.
483 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
484 [EIDRM
] = TARGET_EIDRM
,
485 [ECHRNG
] = TARGET_ECHRNG
,
486 [EL2NSYNC
] = TARGET_EL2NSYNC
,
487 [EL3HLT
] = TARGET_EL3HLT
,
488 [EL3RST
] = TARGET_EL3RST
,
489 [ELNRNG
] = TARGET_ELNRNG
,
490 [EUNATCH
] = TARGET_EUNATCH
,
491 [ENOCSI
] = TARGET_ENOCSI
,
492 [EL2HLT
] = TARGET_EL2HLT
,
493 [EDEADLK
] = TARGET_EDEADLK
,
494 [ENOLCK
] = TARGET_ENOLCK
,
495 [EBADE
] = TARGET_EBADE
,
496 [EBADR
] = TARGET_EBADR
,
497 [EXFULL
] = TARGET_EXFULL
,
498 [ENOANO
] = TARGET_ENOANO
,
499 [EBADRQC
] = TARGET_EBADRQC
,
500 [EBADSLT
] = TARGET_EBADSLT
,
501 [EBFONT
] = TARGET_EBFONT
,
502 [ENOSTR
] = TARGET_ENOSTR
,
503 [ENODATA
] = TARGET_ENODATA
,
504 [ETIME
] = TARGET_ETIME
,
505 [ENOSR
] = TARGET_ENOSR
,
506 [ENONET
] = TARGET_ENONET
,
507 [ENOPKG
] = TARGET_ENOPKG
,
508 [EREMOTE
] = TARGET_EREMOTE
,
509 [ENOLINK
] = TARGET_ENOLINK
,
510 [EADV
] = TARGET_EADV
,
511 [ESRMNT
] = TARGET_ESRMNT
,
512 [ECOMM
] = TARGET_ECOMM
,
513 [EPROTO
] = TARGET_EPROTO
,
514 [EDOTDOT
] = TARGET_EDOTDOT
,
515 [EMULTIHOP
] = TARGET_EMULTIHOP
,
516 [EBADMSG
] = TARGET_EBADMSG
,
517 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
518 [EOVERFLOW
] = TARGET_EOVERFLOW
,
519 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
520 [EBADFD
] = TARGET_EBADFD
,
521 [EREMCHG
] = TARGET_EREMCHG
,
522 [ELIBACC
] = TARGET_ELIBACC
,
523 [ELIBBAD
] = TARGET_ELIBBAD
,
524 [ELIBSCN
] = TARGET_ELIBSCN
,
525 [ELIBMAX
] = TARGET_ELIBMAX
,
526 [ELIBEXEC
] = TARGET_ELIBEXEC
,
527 [EILSEQ
] = TARGET_EILSEQ
,
528 [ENOSYS
] = TARGET_ENOSYS
,
529 [ELOOP
] = TARGET_ELOOP
,
530 [ERESTART
] = TARGET_ERESTART
,
531 [ESTRPIPE
] = TARGET_ESTRPIPE
,
532 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
533 [EUSERS
] = TARGET_EUSERS
,
534 [ENOTSOCK
] = TARGET_ENOTSOCK
,
535 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
536 [EMSGSIZE
] = TARGET_EMSGSIZE
,
537 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
538 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
539 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
540 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
541 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
542 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
543 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
544 [EADDRINUSE
] = TARGET_EADDRINUSE
,
545 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
546 [ENETDOWN
] = TARGET_ENETDOWN
,
547 [ENETUNREACH
] = TARGET_ENETUNREACH
,
548 [ENETRESET
] = TARGET_ENETRESET
,
549 [ECONNABORTED
] = TARGET_ECONNABORTED
,
550 [ECONNRESET
] = TARGET_ECONNRESET
,
551 [ENOBUFS
] = TARGET_ENOBUFS
,
552 [EISCONN
] = TARGET_EISCONN
,
553 [ENOTCONN
] = TARGET_ENOTCONN
,
554 [EUCLEAN
] = TARGET_EUCLEAN
,
555 [ENOTNAM
] = TARGET_ENOTNAM
,
556 [ENAVAIL
] = TARGET_ENAVAIL
,
557 [EISNAM
] = TARGET_EISNAM
,
558 [EREMOTEIO
] = TARGET_EREMOTEIO
,
559 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
560 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
561 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
562 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
563 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
564 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
565 [EALREADY
] = TARGET_EALREADY
,
566 [EINPROGRESS
] = TARGET_EINPROGRESS
,
567 [ESTALE
] = TARGET_ESTALE
,
568 [ECANCELED
] = TARGET_ECANCELED
,
569 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
570 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
572 [ENOKEY
] = TARGET_ENOKEY
,
575 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
578 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
581 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
584 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
586 #ifdef ENOTRECOVERABLE
587 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
591 static inline int host_to_target_errno(int err
)
593 if(host_to_target_errno_table
[err
])
594 return host_to_target_errno_table
[err
];
598 static inline int target_to_host_errno(int err
)
600 if (target_to_host_errno_table
[err
])
601 return target_to_host_errno_table
[err
];
605 static inline abi_long
get_errno(abi_long ret
)
608 return -host_to_target_errno(errno
);
613 static inline int is_error(abi_long ret
)
615 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
618 char *target_strerror(int err
)
620 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
623 return strerror(target_to_host_errno(err
));
626 static abi_ulong target_brk
;
627 static abi_ulong target_original_brk
;
628 static abi_ulong brk_page
;
630 void target_set_brk(abi_ulong new_brk
)
632 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
633 brk_page
= HOST_PAGE_ALIGN(target_brk
);
636 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
637 #define DEBUGF_BRK(message, args...)
639 /* do_brk() must return target values and target errnos. */
640 abi_long
do_brk(abi_ulong new_brk
)
642 abi_long mapped_addr
;
645 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
648 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
651 if (new_brk
< target_original_brk
) {
652 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
657 /* If the new brk is less than the highest page reserved to the
658 * target heap allocation, set it and we're almost done... */
659 if (new_brk
<= brk_page
) {
660 /* Heap contents are initialized to zero, as for anonymous
662 if (new_brk
> target_brk
) {
663 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
665 target_brk
= new_brk
;
666 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
670 /* We need to allocate more memory after the brk... Note that
671 * we don't use MAP_FIXED because that will map over the top of
672 * any existing mapping (like the one with the host libc or qemu
673 * itself); instead we treat "mapped but at wrong address" as
674 * a failure and unmap again.
676 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
677 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
678 PROT_READ
|PROT_WRITE
,
679 MAP_ANON
|MAP_PRIVATE
, 0, 0));
681 if (mapped_addr
== brk_page
) {
682 /* Heap contents are initialized to zero, as for anonymous
683 * mapped pages. Technically the new pages are already
684 * initialized to zero since they *are* anonymous mapped
685 * pages, however we have to take care with the contents that
686 * come from the remaining part of the previous page: it may
687 * contains garbage data due to a previous heap usage (grown
689 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
691 target_brk
= new_brk
;
692 brk_page
= HOST_PAGE_ALIGN(target_brk
);
693 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
696 } else if (mapped_addr
!= -1) {
697 /* Mapped but at wrong address, meaning there wasn't actually
698 * enough space for this brk.
700 target_munmap(mapped_addr
, new_alloc_size
);
702 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
705 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
708 #if defined(TARGET_ALPHA)
709 /* We (partially) emulate OSF/1 on Alpha, which requires we
710 return a proper errno, not an unchanged brk value. */
711 return -TARGET_ENOMEM
;
713 /* For everything else, return the previous break. */
717 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
718 abi_ulong target_fds_addr
,
722 abi_ulong b
, *target_fds
;
724 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
725 if (!(target_fds
= lock_user(VERIFY_READ
,
727 sizeof(abi_ulong
) * nw
,
729 return -TARGET_EFAULT
;
733 for (i
= 0; i
< nw
; i
++) {
734 /* grab the abi_ulong */
735 __get_user(b
, &target_fds
[i
]);
736 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
737 /* check the bit inside the abi_ulong */
744 unlock_user(target_fds
, target_fds_addr
, 0);
749 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
750 abi_ulong target_fds_addr
,
753 if (target_fds_addr
) {
754 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
755 return -TARGET_EFAULT
;
763 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
769 abi_ulong
*target_fds
;
771 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
772 if (!(target_fds
= lock_user(VERIFY_WRITE
,
774 sizeof(abi_ulong
) * nw
,
776 return -TARGET_EFAULT
;
779 for (i
= 0; i
< nw
; i
++) {
781 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
782 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
785 __put_user(v
, &target_fds
[i
]);
788 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
793 #if defined(__alpha__)
799 static inline abi_long
host_to_target_clock_t(long ticks
)
801 #if HOST_HZ == TARGET_HZ
804 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
808 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
809 const struct rusage
*rusage
)
811 struct target_rusage
*target_rusage
;
813 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
814 return -TARGET_EFAULT
;
815 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
816 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
817 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
818 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
819 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
820 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
821 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
822 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
823 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
824 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
825 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
826 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
827 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
828 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
829 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
830 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
831 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
832 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
833 unlock_user_struct(target_rusage
, target_addr
, 1);
838 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
840 abi_ulong target_rlim_swap
;
843 target_rlim_swap
= tswapal(target_rlim
);
844 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
845 return RLIM_INFINITY
;
847 result
= target_rlim_swap
;
848 if (target_rlim_swap
!= (rlim_t
)result
)
849 return RLIM_INFINITY
;
854 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
856 abi_ulong target_rlim_swap
;
859 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
860 target_rlim_swap
= TARGET_RLIM_INFINITY
;
862 target_rlim_swap
= rlim
;
863 result
= tswapal(target_rlim_swap
);
868 static inline int target_to_host_resource(int code
)
871 case TARGET_RLIMIT_AS
:
873 case TARGET_RLIMIT_CORE
:
875 case TARGET_RLIMIT_CPU
:
877 case TARGET_RLIMIT_DATA
:
879 case TARGET_RLIMIT_FSIZE
:
881 case TARGET_RLIMIT_LOCKS
:
883 case TARGET_RLIMIT_MEMLOCK
:
884 return RLIMIT_MEMLOCK
;
885 case TARGET_RLIMIT_MSGQUEUE
:
886 return RLIMIT_MSGQUEUE
;
887 case TARGET_RLIMIT_NICE
:
889 case TARGET_RLIMIT_NOFILE
:
890 return RLIMIT_NOFILE
;
891 case TARGET_RLIMIT_NPROC
:
893 case TARGET_RLIMIT_RSS
:
895 case TARGET_RLIMIT_RTPRIO
:
896 return RLIMIT_RTPRIO
;
897 case TARGET_RLIMIT_SIGPENDING
:
898 return RLIMIT_SIGPENDING
;
899 case TARGET_RLIMIT_STACK
:
906 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
907 abi_ulong target_tv_addr
)
909 struct target_timeval
*target_tv
;
911 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
912 return -TARGET_EFAULT
;
914 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
915 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
917 unlock_user_struct(target_tv
, target_tv_addr
, 0);
922 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
923 const struct timeval
*tv
)
925 struct target_timeval
*target_tv
;
927 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
928 return -TARGET_EFAULT
;
930 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
931 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
933 unlock_user_struct(target_tv
, target_tv_addr
, 1);
938 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
941 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
942 abi_ulong target_mq_attr_addr
)
944 struct target_mq_attr
*target_mq_attr
;
946 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
947 target_mq_attr_addr
, 1))
948 return -TARGET_EFAULT
;
950 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
951 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
952 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
953 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
955 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
960 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
961 const struct mq_attr
*attr
)
963 struct target_mq_attr
*target_mq_attr
;
965 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
966 target_mq_attr_addr
, 0))
967 return -TARGET_EFAULT
;
969 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
970 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
971 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
972 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
974 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
980 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
981 /* do_select() must return target values and target errnos. */
982 static abi_long
do_select(int n
,
983 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
984 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
986 fd_set rfds
, wfds
, efds
;
987 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
988 struct timeval tv
, *tv_ptr
;
991 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
995 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
999 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1004 if (target_tv_addr
) {
1005 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1006 return -TARGET_EFAULT
;
1012 ret
= get_errno(select(n
, rfds_ptr
, wfds_ptr
, efds_ptr
, tv_ptr
));
1014 if (!is_error(ret
)) {
1015 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1016 return -TARGET_EFAULT
;
1017 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1018 return -TARGET_EFAULT
;
1019 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1020 return -TARGET_EFAULT
;
1022 if (target_tv_addr
&& copy_to_user_timeval(target_tv_addr
, &tv
))
1023 return -TARGET_EFAULT
;
1030 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1033 return pipe2(host_pipe
, flags
);
1039 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1040 int flags
, int is_pipe2
)
1044 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1047 return get_errno(ret
);
1049 /* Several targets have special calling conventions for the original
1050 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1052 #if defined(TARGET_ALPHA)
1053 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1054 return host_pipe
[0];
1055 #elif defined(TARGET_MIPS)
1056 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1057 return host_pipe
[0];
1058 #elif defined(TARGET_SH4)
1059 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1060 return host_pipe
[0];
1061 #elif defined(TARGET_SPARC)
1062 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1063 return host_pipe
[0];
1067 if (put_user_s32(host_pipe
[0], pipedes
)
1068 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1069 return -TARGET_EFAULT
;
1070 return get_errno(ret
);
1073 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1074 abi_ulong target_addr
,
1077 struct target_ip_mreqn
*target_smreqn
;
1079 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1081 return -TARGET_EFAULT
;
1082 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1083 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1084 if (len
== sizeof(struct target_ip_mreqn
))
1085 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1086 unlock_user(target_smreqn
, target_addr
, 0);
1091 static inline abi_long
target_to_host_sockaddr(struct sockaddr
*addr
,
1092 abi_ulong target_addr
,
1095 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1096 sa_family_t sa_family
;
1097 struct target_sockaddr
*target_saddr
;
1099 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1101 return -TARGET_EFAULT
;
1103 sa_family
= tswap16(target_saddr
->sa_family
);
1105 /* Oops. The caller might send a incomplete sun_path; sun_path
1106 * must be terminated by \0 (see the manual page), but
1107 * unfortunately it is quite common to specify sockaddr_un
1108 * length as "strlen(x->sun_path)" while it should be
1109 * "strlen(...) + 1". We'll fix that here if needed.
1110 * Linux kernel has a similar feature.
1113 if (sa_family
== AF_UNIX
) {
1114 if (len
< unix_maxlen
&& len
> 0) {
1115 char *cp
= (char*)target_saddr
;
1117 if ( cp
[len
-1] && !cp
[len
] )
1120 if (len
> unix_maxlen
)
1124 memcpy(addr
, target_saddr
, len
);
1125 addr
->sa_family
= sa_family
;
1126 unlock_user(target_saddr
, target_addr
, 0);
1131 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1132 struct sockaddr
*addr
,
1135 struct target_sockaddr
*target_saddr
;
1137 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1139 return -TARGET_EFAULT
;
1140 memcpy(target_saddr
, addr
, len
);
1141 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1142 unlock_user(target_saddr
, target_addr
, len
);
1147 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1148 struct target_msghdr
*target_msgh
)
1150 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1151 abi_long msg_controllen
;
1152 abi_ulong target_cmsg_addr
;
1153 struct target_cmsghdr
*target_cmsg
;
1154 socklen_t space
= 0;
1156 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1157 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1159 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1160 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1162 return -TARGET_EFAULT
;
1164 while (cmsg
&& target_cmsg
) {
1165 void *data
= CMSG_DATA(cmsg
);
1166 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1168 int len
= tswapal(target_cmsg
->cmsg_len
)
1169 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr
));
1171 space
+= CMSG_SPACE(len
);
1172 if (space
> msgh
->msg_controllen
) {
1173 space
-= CMSG_SPACE(len
);
1174 gemu_log("Host cmsg overflow\n");
1178 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1179 cmsg
->cmsg_level
= SOL_SOCKET
;
1181 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1183 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1184 cmsg
->cmsg_len
= CMSG_LEN(len
);
1186 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
1187 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1188 memcpy(data
, target_data
, len
);
1190 int *fd
= (int *)data
;
1191 int *target_fd
= (int *)target_data
;
1192 int i
, numfds
= len
/ sizeof(int);
1194 for (i
= 0; i
< numfds
; i
++)
1195 fd
[i
] = tswap32(target_fd
[i
]);
1198 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1199 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
);
1201 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1203 msgh
->msg_controllen
= space
;
1207 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1208 struct msghdr
*msgh
)
1210 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1211 abi_long msg_controllen
;
1212 abi_ulong target_cmsg_addr
;
1213 struct target_cmsghdr
*target_cmsg
;
1214 socklen_t space
= 0;
1216 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1217 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1219 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1220 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1222 return -TARGET_EFAULT
;
1224 while (cmsg
&& target_cmsg
) {
1225 void *data
= CMSG_DATA(cmsg
);
1226 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1228 int len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof (struct cmsghdr
));
1230 space
+= TARGET_CMSG_SPACE(len
);
1231 if (space
> msg_controllen
) {
1232 space
-= TARGET_CMSG_SPACE(len
);
1233 gemu_log("Target cmsg overflow\n");
1237 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1238 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1240 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1242 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1243 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(len
));
1245 if ((cmsg
->cmsg_level
== SOL_SOCKET
) &&
1246 (cmsg
->cmsg_type
== SCM_RIGHTS
)) {
1247 int *fd
= (int *)data
;
1248 int *target_fd
= (int *)target_data
;
1249 int i
, numfds
= len
/ sizeof(int);
1251 for (i
= 0; i
< numfds
; i
++)
1252 target_fd
[i
] = tswap32(fd
[i
]);
1253 } else if ((cmsg
->cmsg_level
== SOL_SOCKET
) &&
1254 (cmsg
->cmsg_type
== SO_TIMESTAMP
) &&
1255 (len
== sizeof(struct timeval
))) {
1256 /* copy struct timeval to target */
1257 struct timeval
*tv
= (struct timeval
*)data
;
1258 struct target_timeval
*target_tv
=
1259 (struct target_timeval
*)target_data
;
1261 target_tv
->tv_sec
= tswapal(tv
->tv_sec
);
1262 target_tv
->tv_usec
= tswapal(tv
->tv_usec
);
1264 gemu_log("Unsupported ancillary data: %d/%d\n",
1265 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1266 memcpy(target_data
, data
, len
);
1269 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1270 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
);
1272 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1274 target_msgh
->msg_controllen
= tswapal(space
);
1278 /* do_setsockopt() Must return target values and target errnos. */
1279 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1280 abi_ulong optval_addr
, socklen_t optlen
)
1284 struct ip_mreqn
*ip_mreq
;
1285 struct ip_mreq_source
*ip_mreq_source
;
1289 /* TCP options all take an 'int' value. */
1290 if (optlen
< sizeof(uint32_t))
1291 return -TARGET_EINVAL
;
1293 if (get_user_u32(val
, optval_addr
))
1294 return -TARGET_EFAULT
;
1295 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1302 case IP_ROUTER_ALERT
:
1306 case IP_MTU_DISCOVER
:
1312 case IP_MULTICAST_TTL
:
1313 case IP_MULTICAST_LOOP
:
1315 if (optlen
>= sizeof(uint32_t)) {
1316 if (get_user_u32(val
, optval_addr
))
1317 return -TARGET_EFAULT
;
1318 } else if (optlen
>= 1) {
1319 if (get_user_u8(val
, optval_addr
))
1320 return -TARGET_EFAULT
;
1322 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1324 case IP_ADD_MEMBERSHIP
:
1325 case IP_DROP_MEMBERSHIP
:
1326 if (optlen
< sizeof (struct target_ip_mreq
) ||
1327 optlen
> sizeof (struct target_ip_mreqn
))
1328 return -TARGET_EINVAL
;
1330 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1331 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1332 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1335 case IP_BLOCK_SOURCE
:
1336 case IP_UNBLOCK_SOURCE
:
1337 case IP_ADD_SOURCE_MEMBERSHIP
:
1338 case IP_DROP_SOURCE_MEMBERSHIP
:
1339 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1340 return -TARGET_EINVAL
;
1342 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1343 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1344 unlock_user (ip_mreq_source
, optval_addr
, 0);
1353 case IPV6_MTU_DISCOVER
:
1356 case IPV6_RECVPKTINFO
:
1358 if (optlen
< sizeof(uint32_t)) {
1359 return -TARGET_EINVAL
;
1361 if (get_user_u32(val
, optval_addr
)) {
1362 return -TARGET_EFAULT
;
1364 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1365 &val
, sizeof(val
)));
1374 /* struct icmp_filter takes an u32 value */
1375 if (optlen
< sizeof(uint32_t)) {
1376 return -TARGET_EINVAL
;
1379 if (get_user_u32(val
, optval_addr
)) {
1380 return -TARGET_EFAULT
;
1382 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1383 &val
, sizeof(val
)));
1390 case TARGET_SOL_SOCKET
:
1392 case TARGET_SO_RCVTIMEO
:
1396 optname
= SO_RCVTIMEO
;
1399 if (optlen
!= sizeof(struct target_timeval
)) {
1400 return -TARGET_EINVAL
;
1403 if (copy_from_user_timeval(&tv
, optval_addr
)) {
1404 return -TARGET_EFAULT
;
1407 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
1411 case TARGET_SO_SNDTIMEO
:
1412 optname
= SO_SNDTIMEO
;
1414 case TARGET_SO_ATTACH_FILTER
:
1416 struct target_sock_fprog
*tfprog
;
1417 struct target_sock_filter
*tfilter
;
1418 struct sock_fprog fprog
;
1419 struct sock_filter
*filter
;
1422 if (optlen
!= sizeof(*tfprog
)) {
1423 return -TARGET_EINVAL
;
1425 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
1426 return -TARGET_EFAULT
;
1428 if (!lock_user_struct(VERIFY_READ
, tfilter
,
1429 tswapal(tfprog
->filter
), 0)) {
1430 unlock_user_struct(tfprog
, optval_addr
, 1);
1431 return -TARGET_EFAULT
;
1434 fprog
.len
= tswap16(tfprog
->len
);
1435 filter
= malloc(fprog
.len
* sizeof(*filter
));
1436 if (filter
== NULL
) {
1437 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1438 unlock_user_struct(tfprog
, optval_addr
, 1);
1439 return -TARGET_ENOMEM
;
1441 for (i
= 0; i
< fprog
.len
; i
++) {
1442 filter
[i
].code
= tswap16(tfilter
[i
].code
);
1443 filter
[i
].jt
= tfilter
[i
].jt
;
1444 filter
[i
].jf
= tfilter
[i
].jf
;
1445 filter
[i
].k
= tswap32(tfilter
[i
].k
);
1447 fprog
.filter
= filter
;
1449 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
1450 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
1453 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1454 unlock_user_struct(tfprog
, optval_addr
, 1);
1457 /* Options with 'int' argument. */
1458 case TARGET_SO_DEBUG
:
1461 case TARGET_SO_REUSEADDR
:
1462 optname
= SO_REUSEADDR
;
1464 case TARGET_SO_TYPE
:
1467 case TARGET_SO_ERROR
:
1470 case TARGET_SO_DONTROUTE
:
1471 optname
= SO_DONTROUTE
;
1473 case TARGET_SO_BROADCAST
:
1474 optname
= SO_BROADCAST
;
1476 case TARGET_SO_SNDBUF
:
1477 optname
= SO_SNDBUF
;
1479 case TARGET_SO_RCVBUF
:
1480 optname
= SO_RCVBUF
;
1482 case TARGET_SO_KEEPALIVE
:
1483 optname
= SO_KEEPALIVE
;
1485 case TARGET_SO_OOBINLINE
:
1486 optname
= SO_OOBINLINE
;
1488 case TARGET_SO_NO_CHECK
:
1489 optname
= SO_NO_CHECK
;
1491 case TARGET_SO_PRIORITY
:
1492 optname
= SO_PRIORITY
;
1495 case TARGET_SO_BSDCOMPAT
:
1496 optname
= SO_BSDCOMPAT
;
1499 case TARGET_SO_PASSCRED
:
1500 optname
= SO_PASSCRED
;
1502 case TARGET_SO_TIMESTAMP
:
1503 optname
= SO_TIMESTAMP
;
1505 case TARGET_SO_RCVLOWAT
:
1506 optname
= SO_RCVLOWAT
;
1512 if (optlen
< sizeof(uint32_t))
1513 return -TARGET_EINVAL
;
1515 if (get_user_u32(val
, optval_addr
))
1516 return -TARGET_EFAULT
;
1517 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
1521 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level
, optname
);
1522 ret
= -TARGET_ENOPROTOOPT
;
1527 /* do_getsockopt() Must return target values and target errnos. */
1528 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
1529 abi_ulong optval_addr
, abi_ulong optlen
)
1536 case TARGET_SOL_SOCKET
:
1539 /* These don't just return a single integer */
1540 case TARGET_SO_LINGER
:
1541 case TARGET_SO_RCVTIMEO
:
1542 case TARGET_SO_SNDTIMEO
:
1543 case TARGET_SO_PEERNAME
:
1545 case TARGET_SO_PEERCRED
: {
1548 struct target_ucred
*tcr
;
1550 if (get_user_u32(len
, optlen
)) {
1551 return -TARGET_EFAULT
;
1554 return -TARGET_EINVAL
;
1558 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
1566 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
1567 return -TARGET_EFAULT
;
1569 __put_user(cr
.pid
, &tcr
->pid
);
1570 __put_user(cr
.uid
, &tcr
->uid
);
1571 __put_user(cr
.gid
, &tcr
->gid
);
1572 unlock_user_struct(tcr
, optval_addr
, 1);
1573 if (put_user_u32(len
, optlen
)) {
1574 return -TARGET_EFAULT
;
1578 /* Options with 'int' argument. */
1579 case TARGET_SO_DEBUG
:
1582 case TARGET_SO_REUSEADDR
:
1583 optname
= SO_REUSEADDR
;
1585 case TARGET_SO_TYPE
:
1588 case TARGET_SO_ERROR
:
1591 case TARGET_SO_DONTROUTE
:
1592 optname
= SO_DONTROUTE
;
1594 case TARGET_SO_BROADCAST
:
1595 optname
= SO_BROADCAST
;
1597 case TARGET_SO_SNDBUF
:
1598 optname
= SO_SNDBUF
;
1600 case TARGET_SO_RCVBUF
:
1601 optname
= SO_RCVBUF
;
1603 case TARGET_SO_KEEPALIVE
:
1604 optname
= SO_KEEPALIVE
;
1606 case TARGET_SO_OOBINLINE
:
1607 optname
= SO_OOBINLINE
;
1609 case TARGET_SO_NO_CHECK
:
1610 optname
= SO_NO_CHECK
;
1612 case TARGET_SO_PRIORITY
:
1613 optname
= SO_PRIORITY
;
1616 case TARGET_SO_BSDCOMPAT
:
1617 optname
= SO_BSDCOMPAT
;
1620 case TARGET_SO_PASSCRED
:
1621 optname
= SO_PASSCRED
;
1623 case TARGET_SO_TIMESTAMP
:
1624 optname
= SO_TIMESTAMP
;
1626 case TARGET_SO_RCVLOWAT
:
1627 optname
= SO_RCVLOWAT
;
1634 /* TCP options all take an 'int' value. */
1636 if (get_user_u32(len
, optlen
))
1637 return -TARGET_EFAULT
;
1639 return -TARGET_EINVAL
;
1641 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1647 if (put_user_u32(val
, optval_addr
))
1648 return -TARGET_EFAULT
;
1650 if (put_user_u8(val
, optval_addr
))
1651 return -TARGET_EFAULT
;
1653 if (put_user_u32(len
, optlen
))
1654 return -TARGET_EFAULT
;
1661 case IP_ROUTER_ALERT
:
1665 case IP_MTU_DISCOVER
:
1671 case IP_MULTICAST_TTL
:
1672 case IP_MULTICAST_LOOP
:
1673 if (get_user_u32(len
, optlen
))
1674 return -TARGET_EFAULT
;
1676 return -TARGET_EINVAL
;
1678 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1681 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
1683 if (put_user_u32(len
, optlen
)
1684 || put_user_u8(val
, optval_addr
))
1685 return -TARGET_EFAULT
;
1687 if (len
> sizeof(int))
1689 if (put_user_u32(len
, optlen
)
1690 || put_user_u32(val
, optval_addr
))
1691 return -TARGET_EFAULT
;
1695 ret
= -TARGET_ENOPROTOOPT
;
1701 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1703 ret
= -TARGET_EOPNOTSUPP
;
1709 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
1710 int count
, int copy
)
1712 struct target_iovec
*target_vec
;
1714 abi_ulong total_len
, max_len
;
1722 if (count
< 0 || count
> IOV_MAX
) {
1727 vec
= calloc(count
, sizeof(struct iovec
));
1733 target_vec
= lock_user(VERIFY_READ
, target_addr
,
1734 count
* sizeof(struct target_iovec
), 1);
1735 if (target_vec
== NULL
) {
1740 /* ??? If host page size > target page size, this will result in a
1741 value larger than what we can actually support. */
1742 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
1745 for (i
= 0; i
< count
; i
++) {
1746 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
1747 abi_long len
= tswapal(target_vec
[i
].iov_len
);
1752 } else if (len
== 0) {
1753 /* Zero length pointer is ignored. */
1754 vec
[i
].iov_base
= 0;
1756 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
1757 if (!vec
[i
].iov_base
) {
1761 if (len
> max_len
- total_len
) {
1762 len
= max_len
- total_len
;
1765 vec
[i
].iov_len
= len
;
1769 unlock_user(target_vec
, target_addr
, 0);
1773 unlock_user(target_vec
, target_addr
, 0);
1780 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
1781 int count
, int copy
)
1783 struct target_iovec
*target_vec
;
1786 target_vec
= lock_user(VERIFY_READ
, target_addr
,
1787 count
* sizeof(struct target_iovec
), 1);
1789 for (i
= 0; i
< count
; i
++) {
1790 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
1791 abi_long len
= tswapal(target_vec
[i
].iov_base
);
1795 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
1797 unlock_user(target_vec
, target_addr
, 0);
1803 static inline int target_to_host_sock_type(int *type
)
1806 int target_type
= *type
;
1808 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
1809 case TARGET_SOCK_DGRAM
:
1810 host_type
= SOCK_DGRAM
;
1812 case TARGET_SOCK_STREAM
:
1813 host_type
= SOCK_STREAM
;
1816 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
1819 if (target_type
& TARGET_SOCK_CLOEXEC
) {
1820 #if defined(SOCK_CLOEXEC)
1821 host_type
|= SOCK_CLOEXEC
;
1823 return -TARGET_EINVAL
;
1826 if (target_type
& TARGET_SOCK_NONBLOCK
) {
1827 #if defined(SOCK_NONBLOCK)
1828 host_type
|= SOCK_NONBLOCK
;
1829 #elif !defined(O_NONBLOCK)
1830 return -TARGET_EINVAL
;
1837 /* Try to emulate socket type flags after socket creation. */
1838 static int sock_flags_fixup(int fd
, int target_type
)
1840 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
1841 if (target_type
& TARGET_SOCK_NONBLOCK
) {
1842 int flags
= fcntl(fd
, F_GETFL
);
1843 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
1845 return -TARGET_EINVAL
;
1852 /* do_socket() Must return target values and target errnos. */
1853 static abi_long
do_socket(int domain
, int type
, int protocol
)
1855 int target_type
= type
;
1858 ret
= target_to_host_sock_type(&type
);
1863 if (domain
== PF_NETLINK
)
1864 return -EAFNOSUPPORT
; /* do not NETLINK socket connections possible */
1865 ret
= get_errno(socket(domain
, type
, protocol
));
1867 ret
= sock_flags_fixup(ret
, target_type
);
1872 /* do_bind() Must return target values and target errnos. */
1873 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
1879 if ((int)addrlen
< 0) {
1880 return -TARGET_EINVAL
;
1883 addr
= alloca(addrlen
+1);
1885 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
1889 return get_errno(bind(sockfd
, addr
, addrlen
));
1892 /* do_connect() Must return target values and target errnos. */
1893 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
1899 if ((int)addrlen
< 0) {
1900 return -TARGET_EINVAL
;
1903 addr
= alloca(addrlen
);
1905 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
1909 return get_errno(connect(sockfd
, addr
, addrlen
));
1912 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
1913 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
1914 int flags
, int send
)
1920 abi_ulong target_vec
;
1922 if (msgp
->msg_name
) {
1923 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
1924 msg
.msg_name
= alloca(msg
.msg_namelen
);
1925 ret
= target_to_host_sockaddr(msg
.msg_name
, tswapal(msgp
->msg_name
),
1931 msg
.msg_name
= NULL
;
1932 msg
.msg_namelen
= 0;
1934 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
1935 msg
.msg_control
= alloca(msg
.msg_controllen
);
1936 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
1938 count
= tswapal(msgp
->msg_iovlen
);
1939 target_vec
= tswapal(msgp
->msg_iov
);
1940 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
1941 target_vec
, count
, send
);
1943 ret
= -host_to_target_errno(errno
);
1946 msg
.msg_iovlen
= count
;
1950 ret
= target_to_host_cmsg(&msg
, msgp
);
1952 ret
= get_errno(sendmsg(fd
, &msg
, flags
));
1954 ret
= get_errno(recvmsg(fd
, &msg
, flags
));
1955 if (!is_error(ret
)) {
1957 ret
= host_to_target_cmsg(msgp
, &msg
);
1958 if (!is_error(ret
)) {
1959 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
1960 if (msg
.msg_name
!= NULL
) {
1961 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
1962 msg
.msg_name
, msg
.msg_namelen
);
1974 unlock_iovec(vec
, target_vec
, count
, !send
);
1979 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
1980 int flags
, int send
)
1983 struct target_msghdr
*msgp
;
1985 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
1989 return -TARGET_EFAULT
;
1991 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
1992 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
1996 #ifdef TARGET_NR_sendmmsg
1997 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
1998 * so it might not have this *mmsg-specific flag either.
2000 #ifndef MSG_WAITFORONE
2001 #define MSG_WAITFORONE 0x10000
2004 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
2005 unsigned int vlen
, unsigned int flags
,
2008 struct target_mmsghdr
*mmsgp
;
2012 if (vlen
> UIO_MAXIOV
) {
2016 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
2018 return -TARGET_EFAULT
;
2021 for (i
= 0; i
< vlen
; i
++) {
2022 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
2023 if (is_error(ret
)) {
2026 mmsgp
[i
].msg_len
= tswap32(ret
);
2027 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2028 if (flags
& MSG_WAITFORONE
) {
2029 flags
|= MSG_DONTWAIT
;
2033 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
2035 /* Return number of datagrams sent if we sent any at all;
2036 * otherwise return the error.
2045 /* If we don't have a system accept4() then just call accept.
2046 * The callsites to do_accept4() will ensure that they don't
2047 * pass a non-zero flags argument in this config.
2049 #ifndef CONFIG_ACCEPT4
2050 static inline int accept4(int sockfd
, struct sockaddr
*addr
,
2051 socklen_t
*addrlen
, int flags
)
2054 return accept(sockfd
, addr
, addrlen
);
2058 /* do_accept4() Must return target values and target errnos. */
2059 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
2060 abi_ulong target_addrlen_addr
, int flags
)
2067 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
2069 if (target_addr
== 0) {
2070 return get_errno(accept4(fd
, NULL
, NULL
, host_flags
));
2073 /* linux returns EINVAL if addrlen pointer is invalid */
2074 if (get_user_u32(addrlen
, target_addrlen_addr
))
2075 return -TARGET_EINVAL
;
2077 if ((int)addrlen
< 0) {
2078 return -TARGET_EINVAL
;
2081 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2082 return -TARGET_EINVAL
;
2084 addr
= alloca(addrlen
);
2086 ret
= get_errno(accept4(fd
, addr
, &addrlen
, host_flags
));
2087 if (!is_error(ret
)) {
2088 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2089 if (put_user_u32(addrlen
, target_addrlen_addr
))
2090 ret
= -TARGET_EFAULT
;
2095 /* do_getpeername() Must return target values and target errnos. */
2096 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
2097 abi_ulong target_addrlen_addr
)
2103 if (get_user_u32(addrlen
, target_addrlen_addr
))
2104 return -TARGET_EFAULT
;
2106 if ((int)addrlen
< 0) {
2107 return -TARGET_EINVAL
;
2110 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2111 return -TARGET_EFAULT
;
2113 addr
= alloca(addrlen
);
2115 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
2116 if (!is_error(ret
)) {
2117 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2118 if (put_user_u32(addrlen
, target_addrlen_addr
))
2119 ret
= -TARGET_EFAULT
;
2124 /* do_getsockname() Must return target values and target errnos. */
2125 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
2126 abi_ulong target_addrlen_addr
)
2132 if (get_user_u32(addrlen
, target_addrlen_addr
))
2133 return -TARGET_EFAULT
;
2135 if ((int)addrlen
< 0) {
2136 return -TARGET_EINVAL
;
2139 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2140 return -TARGET_EFAULT
;
2142 addr
= alloca(addrlen
);
2144 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
2145 if (!is_error(ret
)) {
2146 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2147 if (put_user_u32(addrlen
, target_addrlen_addr
))
2148 ret
= -TARGET_EFAULT
;
2153 /* do_socketpair() Must return target values and target errnos. */
2154 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
2155 abi_ulong target_tab_addr
)
2160 target_to_host_sock_type(&type
);
2162 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
2163 if (!is_error(ret
)) {
2164 if (put_user_s32(tab
[0], target_tab_addr
)
2165 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
2166 ret
= -TARGET_EFAULT
;
2171 /* do_sendto() Must return target values and target errnos. */
2172 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
2173 abi_ulong target_addr
, socklen_t addrlen
)
2179 if ((int)addrlen
< 0) {
2180 return -TARGET_EINVAL
;
2183 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
2185 return -TARGET_EFAULT
;
2187 addr
= alloca(addrlen
);
2188 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
2190 unlock_user(host_msg
, msg
, 0);
2193 ret
= get_errno(sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
2195 ret
= get_errno(send(fd
, host_msg
, len
, flags
));
2197 unlock_user(host_msg
, msg
, 0);
2201 /* do_recvfrom() Must return target values and target errnos. */
2202 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
2203 abi_ulong target_addr
,
2204 abi_ulong target_addrlen
)
2211 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
2213 return -TARGET_EFAULT
;
2215 if (get_user_u32(addrlen
, target_addrlen
)) {
2216 ret
= -TARGET_EFAULT
;
2219 if ((int)addrlen
< 0) {
2220 ret
= -TARGET_EINVAL
;
2223 addr
= alloca(addrlen
);
2224 ret
= get_errno(recvfrom(fd
, host_msg
, len
, flags
, addr
, &addrlen
));
2226 addr
= NULL
; /* To keep compiler quiet. */
2227 ret
= get_errno(qemu_recv(fd
, host_msg
, len
, flags
));
2229 if (!is_error(ret
)) {
2231 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2232 if (put_user_u32(addrlen
, target_addrlen
)) {
2233 ret
= -TARGET_EFAULT
;
2237 unlock_user(host_msg
, msg
, len
);
2240 unlock_user(host_msg
, msg
, 0);
2245 #ifdef TARGET_NR_socketcall
2246 /* do_socketcall() Must return target values and target errnos. */
2247 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
2249 static const unsigned ac
[] = { /* number of arguments per call */
2250 [SOCKOP_socket
] = 3, /* domain, type, protocol */
2251 [SOCKOP_bind
] = 3, /* sockfd, addr, addrlen */
2252 [SOCKOP_connect
] = 3, /* sockfd, addr, addrlen */
2253 [SOCKOP_listen
] = 2, /* sockfd, backlog */
2254 [SOCKOP_accept
] = 3, /* sockfd, addr, addrlen */
2255 [SOCKOP_accept4
] = 4, /* sockfd, addr, addrlen, flags */
2256 [SOCKOP_getsockname
] = 3, /* sockfd, addr, addrlen */
2257 [SOCKOP_getpeername
] = 3, /* sockfd, addr, addrlen */
2258 [SOCKOP_socketpair
] = 4, /* domain, type, protocol, tab */
2259 [SOCKOP_send
] = 4, /* sockfd, msg, len, flags */
2260 [SOCKOP_recv
] = 4, /* sockfd, msg, len, flags */
2261 [SOCKOP_sendto
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2262 [SOCKOP_recvfrom
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2263 [SOCKOP_shutdown
] = 2, /* sockfd, how */
2264 [SOCKOP_sendmsg
] = 3, /* sockfd, msg, flags */
2265 [SOCKOP_recvmsg
] = 3, /* sockfd, msg, flags */
2266 [SOCKOP_setsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
2267 [SOCKOP_getsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
2269 abi_long a
[6]; /* max 6 args */
2271 /* first, collect the arguments in a[] according to ac[] */
2272 if (num
>= 0 && num
< ARRAY_SIZE(ac
)) {
2274 assert(ARRAY_SIZE(a
) >= ac
[num
]); /* ensure we have space for args */
2275 for (i
= 0; i
< ac
[num
]; ++i
) {
2276 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
2277 return -TARGET_EFAULT
;
2282 /* now when we have the args, actually handle the call */
2284 case SOCKOP_socket
: /* domain, type, protocol */
2285 return do_socket(a
[0], a
[1], a
[2]);
2286 case SOCKOP_bind
: /* sockfd, addr, addrlen */
2287 return do_bind(a
[0], a
[1], a
[2]);
2288 case SOCKOP_connect
: /* sockfd, addr, addrlen */
2289 return do_connect(a
[0], a
[1], a
[2]);
2290 case SOCKOP_listen
: /* sockfd, backlog */
2291 return get_errno(listen(a
[0], a
[1]));
2292 case SOCKOP_accept
: /* sockfd, addr, addrlen */
2293 return do_accept4(a
[0], a
[1], a
[2], 0);
2294 case SOCKOP_accept4
: /* sockfd, addr, addrlen, flags */
2295 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
2296 case SOCKOP_getsockname
: /* sockfd, addr, addrlen */
2297 return do_getsockname(a
[0], a
[1], a
[2]);
2298 case SOCKOP_getpeername
: /* sockfd, addr, addrlen */
2299 return do_getpeername(a
[0], a
[1], a
[2]);
2300 case SOCKOP_socketpair
: /* domain, type, protocol, tab */
2301 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
2302 case SOCKOP_send
: /* sockfd, msg, len, flags */
2303 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
2304 case SOCKOP_recv
: /* sockfd, msg, len, flags */
2305 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
2306 case SOCKOP_sendto
: /* sockfd, msg, len, flags, addr, addrlen */
2307 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
2308 case SOCKOP_recvfrom
: /* sockfd, msg, len, flags, addr, addrlen */
2309 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
2310 case SOCKOP_shutdown
: /* sockfd, how */
2311 return get_errno(shutdown(a
[0], a
[1]));
2312 case SOCKOP_sendmsg
: /* sockfd, msg, flags */
2313 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
2314 case SOCKOP_recvmsg
: /* sockfd, msg, flags */
2315 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
2316 case SOCKOP_setsockopt
: /* sockfd, level, optname, optval, optlen */
2317 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
2318 case SOCKOP_getsockopt
: /* sockfd, level, optname, optval, optlen */
2319 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
2321 gemu_log("Unsupported socketcall: %d\n", num
);
2322 return -TARGET_ENOSYS
;
2327 #define N_SHM_REGIONS 32
2329 static struct shm_region
{
2332 } shm_regions
[N_SHM_REGIONS
];
2334 struct target_semid_ds
2336 struct target_ipc_perm sem_perm
;
2337 abi_ulong sem_otime
;
2338 abi_ulong __unused1
;
2339 abi_ulong sem_ctime
;
2340 abi_ulong __unused2
;
2341 abi_ulong sem_nsems
;
2342 abi_ulong __unused3
;
2343 abi_ulong __unused4
;
2346 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
2347 abi_ulong target_addr
)
2349 struct target_ipc_perm
*target_ip
;
2350 struct target_semid_ds
*target_sd
;
2352 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2353 return -TARGET_EFAULT
;
2354 target_ip
= &(target_sd
->sem_perm
);
2355 host_ip
->__key
= tswap32(target_ip
->__key
);
2356 host_ip
->uid
= tswap32(target_ip
->uid
);
2357 host_ip
->gid
= tswap32(target_ip
->gid
);
2358 host_ip
->cuid
= tswap32(target_ip
->cuid
);
2359 host_ip
->cgid
= tswap32(target_ip
->cgid
);
2360 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2361 host_ip
->mode
= tswap32(target_ip
->mode
);
2363 host_ip
->mode
= tswap16(target_ip
->mode
);
2365 #if defined(TARGET_PPC)
2366 host_ip
->__seq
= tswap32(target_ip
->__seq
);
2368 host_ip
->__seq
= tswap16(target_ip
->__seq
);
2370 unlock_user_struct(target_sd
, target_addr
, 0);
2374 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
2375 struct ipc_perm
*host_ip
)
2377 struct target_ipc_perm
*target_ip
;
2378 struct target_semid_ds
*target_sd
;
2380 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2381 return -TARGET_EFAULT
;
2382 target_ip
= &(target_sd
->sem_perm
);
2383 target_ip
->__key
= tswap32(host_ip
->__key
);
2384 target_ip
->uid
= tswap32(host_ip
->uid
);
2385 target_ip
->gid
= tswap32(host_ip
->gid
);
2386 target_ip
->cuid
= tswap32(host_ip
->cuid
);
2387 target_ip
->cgid
= tswap32(host_ip
->cgid
);
2388 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2389 target_ip
->mode
= tswap32(host_ip
->mode
);
2391 target_ip
->mode
= tswap16(host_ip
->mode
);
2393 #if defined(TARGET_PPC)
2394 target_ip
->__seq
= tswap32(host_ip
->__seq
);
2396 target_ip
->__seq
= tswap16(host_ip
->__seq
);
2398 unlock_user_struct(target_sd
, target_addr
, 1);
2402 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
2403 abi_ulong target_addr
)
2405 struct target_semid_ds
*target_sd
;
2407 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2408 return -TARGET_EFAULT
;
2409 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
2410 return -TARGET_EFAULT
;
2411 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
2412 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
2413 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
2414 unlock_user_struct(target_sd
, target_addr
, 0);
2418 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
2419 struct semid_ds
*host_sd
)
2421 struct target_semid_ds
*target_sd
;
2423 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2424 return -TARGET_EFAULT
;
2425 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
2426 return -TARGET_EFAULT
;
2427 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
2428 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
2429 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
2430 unlock_user_struct(target_sd
, target_addr
, 1);
2434 struct target_seminfo
{
2447 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
2448 struct seminfo
*host_seminfo
)
2450 struct target_seminfo
*target_seminfo
;
2451 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
2452 return -TARGET_EFAULT
;
2453 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
2454 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
2455 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
2456 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
2457 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
2458 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
2459 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
2460 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
2461 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
2462 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
2463 unlock_user_struct(target_seminfo
, target_addr
, 1);
2469 struct semid_ds
*buf
;
2470 unsigned short *array
;
2471 struct seminfo
*__buf
;
2474 union target_semun
{
2481 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
2482 abi_ulong target_addr
)
2485 unsigned short *array
;
2487 struct semid_ds semid_ds
;
2490 semun
.buf
= &semid_ds
;
2492 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2494 return get_errno(ret
);
2496 nsems
= semid_ds
.sem_nsems
;
2498 *host_array
= malloc(nsems
*sizeof(unsigned short));
2500 return -TARGET_ENOMEM
;
2502 array
= lock_user(VERIFY_READ
, target_addr
,
2503 nsems
*sizeof(unsigned short), 1);
2506 return -TARGET_EFAULT
;
2509 for(i
=0; i
<nsems
; i
++) {
2510 __get_user((*host_array
)[i
], &array
[i
]);
2512 unlock_user(array
, target_addr
, 0);
2517 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
2518 unsigned short **host_array
)
2521 unsigned short *array
;
2523 struct semid_ds semid_ds
;
2526 semun
.buf
= &semid_ds
;
2528 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2530 return get_errno(ret
);
2532 nsems
= semid_ds
.sem_nsems
;
2534 array
= lock_user(VERIFY_WRITE
, target_addr
,
2535 nsems
*sizeof(unsigned short), 0);
2537 return -TARGET_EFAULT
;
2539 for(i
=0; i
<nsems
; i
++) {
2540 __put_user((*host_array
)[i
], &array
[i
]);
2543 unlock_user(array
, target_addr
, 1);
2548 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
2549 union target_semun target_su
)
2552 struct semid_ds dsarg
;
2553 unsigned short *array
= NULL
;
2554 struct seminfo seminfo
;
2555 abi_long ret
= -TARGET_EINVAL
;
2562 arg
.val
= tswap32(target_su
.val
);
2563 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2564 target_su
.val
= tswap32(arg
.val
);
2568 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
2572 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2573 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
2580 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
2584 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2585 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
2591 arg
.__buf
= &seminfo
;
2592 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2593 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
2601 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
2608 struct target_sembuf
{
2609 unsigned short sem_num
;
2614 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
2615 abi_ulong target_addr
,
2618 struct target_sembuf
*target_sembuf
;
2621 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
2622 nsops
*sizeof(struct target_sembuf
), 1);
2624 return -TARGET_EFAULT
;
2626 for(i
=0; i
<nsops
; i
++) {
2627 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
2628 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
2629 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
2632 unlock_user(target_sembuf
, target_addr
, 0);
2637 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
2639 struct sembuf sops
[nsops
];
2641 if (target_to_host_sembuf(sops
, ptr
, nsops
))
2642 return -TARGET_EFAULT
;
2644 return get_errno(semop(semid
, sops
, nsops
));
2647 struct target_msqid_ds
2649 struct target_ipc_perm msg_perm
;
2650 abi_ulong msg_stime
;
2651 #if TARGET_ABI_BITS == 32
2652 abi_ulong __unused1
;
2654 abi_ulong msg_rtime
;
2655 #if TARGET_ABI_BITS == 32
2656 abi_ulong __unused2
;
2658 abi_ulong msg_ctime
;
2659 #if TARGET_ABI_BITS == 32
2660 abi_ulong __unused3
;
2662 abi_ulong __msg_cbytes
;
2664 abi_ulong msg_qbytes
;
2665 abi_ulong msg_lspid
;
2666 abi_ulong msg_lrpid
;
2667 abi_ulong __unused4
;
2668 abi_ulong __unused5
;
2671 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
2672 abi_ulong target_addr
)
2674 struct target_msqid_ds
*target_md
;
2676 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
2677 return -TARGET_EFAULT
;
2678 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
2679 return -TARGET_EFAULT
;
2680 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
2681 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
2682 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
2683 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
2684 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
2685 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
2686 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
2687 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
2688 unlock_user_struct(target_md
, target_addr
, 0);
2692 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
2693 struct msqid_ds
*host_md
)
2695 struct target_msqid_ds
*target_md
;
2697 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
2698 return -TARGET_EFAULT
;
2699 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
2700 return -TARGET_EFAULT
;
2701 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
2702 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
2703 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
2704 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
2705 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
2706 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
2707 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
2708 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
2709 unlock_user_struct(target_md
, target_addr
, 1);
2713 struct target_msginfo
{
2721 unsigned short int msgseg
;
2724 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
2725 struct msginfo
*host_msginfo
)
2727 struct target_msginfo
*target_msginfo
;
2728 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
2729 return -TARGET_EFAULT
;
2730 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
2731 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
2732 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
2733 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
2734 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
2735 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
2736 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
2737 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
2738 unlock_user_struct(target_msginfo
, target_addr
, 1);
2742 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
2744 struct msqid_ds dsarg
;
2745 struct msginfo msginfo
;
2746 abi_long ret
= -TARGET_EINVAL
;
2754 if (target_to_host_msqid_ds(&dsarg
,ptr
))
2755 return -TARGET_EFAULT
;
2756 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
2757 if (host_to_target_msqid_ds(ptr
,&dsarg
))
2758 return -TARGET_EFAULT
;
2761 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
2765 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
2766 if (host_to_target_msginfo(ptr
, &msginfo
))
2767 return -TARGET_EFAULT
;
2774 struct target_msgbuf
{
2779 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
2780 unsigned int msgsz
, int msgflg
)
2782 struct target_msgbuf
*target_mb
;
2783 struct msgbuf
*host_mb
;
2786 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
2787 return -TARGET_EFAULT
;
2788 host_mb
= malloc(msgsz
+sizeof(long));
2789 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
2790 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
2791 ret
= get_errno(msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
2793 unlock_user_struct(target_mb
, msgp
, 0);
2798 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
2799 unsigned int msgsz
, abi_long msgtyp
,
2802 struct target_msgbuf
*target_mb
;
2804 struct msgbuf
*host_mb
;
2807 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
2808 return -TARGET_EFAULT
;
2810 host_mb
= g_malloc(msgsz
+sizeof(long));
2811 ret
= get_errno(msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
2814 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
2815 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
2816 if (!target_mtext
) {
2817 ret
= -TARGET_EFAULT
;
2820 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
2821 unlock_user(target_mtext
, target_mtext_addr
, ret
);
2824 target_mb
->mtype
= tswapal(host_mb
->mtype
);
2828 unlock_user_struct(target_mb
, msgp
, 1);
2833 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
2834 abi_ulong target_addr
)
2836 struct target_shmid_ds
*target_sd
;
2838 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2839 return -TARGET_EFAULT
;
2840 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
2841 return -TARGET_EFAULT
;
2842 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
2843 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
2844 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
2845 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
2846 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
2847 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
2848 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
2849 unlock_user_struct(target_sd
, target_addr
, 0);
2853 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
2854 struct shmid_ds
*host_sd
)
2856 struct target_shmid_ds
*target_sd
;
2858 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2859 return -TARGET_EFAULT
;
2860 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
2861 return -TARGET_EFAULT
;
2862 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
2863 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
2864 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
2865 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
2866 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
2867 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
2868 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
2869 unlock_user_struct(target_sd
, target_addr
, 1);
2873 struct target_shminfo
{
2881 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
2882 struct shminfo
*host_shminfo
)
2884 struct target_shminfo
*target_shminfo
;
2885 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
2886 return -TARGET_EFAULT
;
2887 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
2888 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
2889 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
2890 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
2891 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
2892 unlock_user_struct(target_shminfo
, target_addr
, 1);
2896 struct target_shm_info
{
2901 abi_ulong swap_attempts
;
2902 abi_ulong swap_successes
;
2905 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
2906 struct shm_info
*host_shm_info
)
2908 struct target_shm_info
*target_shm_info
;
2909 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
2910 return -TARGET_EFAULT
;
2911 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
2912 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
2913 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
2914 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
2915 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
2916 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
2917 unlock_user_struct(target_shm_info
, target_addr
, 1);
2921 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
2923 struct shmid_ds dsarg
;
2924 struct shminfo shminfo
;
2925 struct shm_info shm_info
;
2926 abi_long ret
= -TARGET_EINVAL
;
2934 if (target_to_host_shmid_ds(&dsarg
, buf
))
2935 return -TARGET_EFAULT
;
2936 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
2937 if (host_to_target_shmid_ds(buf
, &dsarg
))
2938 return -TARGET_EFAULT
;
2941 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
2942 if (host_to_target_shminfo(buf
, &shminfo
))
2943 return -TARGET_EFAULT
;
2946 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
2947 if (host_to_target_shm_info(buf
, &shm_info
))
2948 return -TARGET_EFAULT
;
2953 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
2960 static inline abi_ulong
do_shmat(int shmid
, abi_ulong shmaddr
, int shmflg
)
2964 struct shmid_ds shm_info
;
2967 /* find out the length of the shared memory segment */
2968 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
2969 if (is_error(ret
)) {
2970 /* can't get length, bail out */
2977 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
2979 abi_ulong mmap_start
;
2981 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
2983 if (mmap_start
== -1) {
2985 host_raddr
= (void *)-1;
2987 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
2990 if (host_raddr
== (void *)-1) {
2992 return get_errno((long)host_raddr
);
2994 raddr
=h2g((unsigned long)host_raddr
);
2996 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
2997 PAGE_VALID
| PAGE_READ
|
2998 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
3000 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
3001 if (shm_regions
[i
].start
== 0) {
3002 shm_regions
[i
].start
= raddr
;
3003 shm_regions
[i
].size
= shm_info
.shm_segsz
;
3013 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
3017 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
3018 if (shm_regions
[i
].start
== shmaddr
) {
3019 shm_regions
[i
].start
= 0;
3020 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
3025 return get_errno(shmdt(g2h(shmaddr
)));
3028 #ifdef TARGET_NR_ipc
3029 /* ??? This only works with linear mappings. */
3030 /* do_ipc() must return target values and target errnos. */
3031 static abi_long
do_ipc(unsigned int call
, int first
,
3032 int second
, int third
,
3033 abi_long ptr
, abi_long fifth
)
3038 version
= call
>> 16;
3043 ret
= do_semop(first
, ptr
, second
);
3047 ret
= get_errno(semget(first
, second
, third
));
3051 ret
= do_semctl(first
, second
, third
, (union target_semun
)(abi_ulong
) ptr
);
3055 ret
= get_errno(msgget(first
, second
));
3059 ret
= do_msgsnd(first
, ptr
, second
, third
);
3063 ret
= do_msgctl(first
, second
, ptr
);
3070 struct target_ipc_kludge
{
3075 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
3076 ret
= -TARGET_EFAULT
;
3080 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
3082 unlock_user_struct(tmp
, ptr
, 0);
3086 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
3095 raddr
= do_shmat(first
, ptr
, second
);
3096 if (is_error(raddr
))
3097 return get_errno(raddr
);
3098 if (put_user_ual(raddr
, third
))
3099 return -TARGET_EFAULT
;
3103 ret
= -TARGET_EINVAL
;
3108 ret
= do_shmdt(ptr
);
3112 /* IPC_* flag values are the same on all linux platforms */
3113 ret
= get_errno(shmget(first
, second
, third
));
3116 /* IPC_* and SHM_* command values are the same on all linux platforms */
3118 ret
= do_shmctl(first
, second
, ptr
);
3121 gemu_log("Unsupported ipc call: %d (version %d)\n", call
, version
);
3122 ret
= -TARGET_ENOSYS
;
3129 /* kernel structure types definitions */
3131 #define STRUCT(name, ...) STRUCT_ ## name,
3132 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
3134 #include "syscall_types.h"
3137 #undef STRUCT_SPECIAL
3139 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
3140 #define STRUCT_SPECIAL(name)
3141 #include "syscall_types.h"
3143 #undef STRUCT_SPECIAL
3145 typedef struct IOCTLEntry IOCTLEntry
;
3147 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3148 int fd
, abi_long cmd
, abi_long arg
);
3151 unsigned int target_cmd
;
3152 unsigned int host_cmd
;
3155 do_ioctl_fn
*do_ioctl
;
3156 const argtype arg_type
[5];
3159 #define IOC_R 0x0001
3160 #define IOC_W 0x0002
3161 #define IOC_RW (IOC_R | IOC_W)
3163 #define MAX_STRUCT_SIZE 4096
3165 #ifdef CONFIG_FIEMAP
3166 /* So fiemap access checks don't overflow on 32 bit systems.
3167 * This is very slightly smaller than the limit imposed by
3168 * the underlying kernel.
3170 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3171 / sizeof(struct fiemap_extent))
3173 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3174 int fd
, abi_long cmd
, abi_long arg
)
3176 /* The parameter for this ioctl is a struct fiemap followed
3177 * by an array of struct fiemap_extent whose size is set
3178 * in fiemap->fm_extent_count. The array is filled in by the
3181 int target_size_in
, target_size_out
;
3183 const argtype
*arg_type
= ie
->arg_type
;
3184 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
3187 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
3191 assert(arg_type
[0] == TYPE_PTR
);
3192 assert(ie
->access
== IOC_RW
);
3194 target_size_in
= thunk_type_size(arg_type
, 0);
3195 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
3197 return -TARGET_EFAULT
;
3199 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3200 unlock_user(argptr
, arg
, 0);
3201 fm
= (struct fiemap
*)buf_temp
;
3202 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
3203 return -TARGET_EINVAL
;
3206 outbufsz
= sizeof (*fm
) +
3207 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
3209 if (outbufsz
> MAX_STRUCT_SIZE
) {
3210 /* We can't fit all the extents into the fixed size buffer.
3211 * Allocate one that is large enough and use it instead.
3213 fm
= malloc(outbufsz
);
3215 return -TARGET_ENOMEM
;
3217 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
3220 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, fm
));
3221 if (!is_error(ret
)) {
3222 target_size_out
= target_size_in
;
3223 /* An extent_count of 0 means we were only counting the extents
3224 * so there are no structs to copy
3226 if (fm
->fm_extent_count
!= 0) {
3227 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
3229 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
3231 ret
= -TARGET_EFAULT
;
3233 /* Convert the struct fiemap */
3234 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
3235 if (fm
->fm_extent_count
!= 0) {
3236 p
= argptr
+ target_size_in
;
3237 /* ...and then all the struct fiemap_extents */
3238 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
3239 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
3244 unlock_user(argptr
, arg
, target_size_out
);
3254 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3255 int fd
, abi_long cmd
, abi_long arg
)
3257 const argtype
*arg_type
= ie
->arg_type
;
3261 struct ifconf
*host_ifconf
;
3263 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
3264 int target_ifreq_size
;
3269 abi_long target_ifc_buf
;
3273 assert(arg_type
[0] == TYPE_PTR
);
3274 assert(ie
->access
== IOC_RW
);
3277 target_size
= thunk_type_size(arg_type
, 0);
3279 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3281 return -TARGET_EFAULT
;
3282 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3283 unlock_user(argptr
, arg
, 0);
3285 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
3286 target_ifc_len
= host_ifconf
->ifc_len
;
3287 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
3289 target_ifreq_size
= thunk_type_size(ifreq_arg_type
, 0);
3290 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
3291 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
3293 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
3294 if (outbufsz
> MAX_STRUCT_SIZE
) {
3295 /* We can't fit all the extents into the fixed size buffer.
3296 * Allocate one that is large enough and use it instead.
3298 host_ifconf
= malloc(outbufsz
);
3300 return -TARGET_ENOMEM
;
3302 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
3305 host_ifc_buf
= (char*)host_ifconf
+ sizeof(*host_ifconf
);
3307 host_ifconf
->ifc_len
= host_ifc_len
;
3308 host_ifconf
->ifc_buf
= host_ifc_buf
;
3310 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, host_ifconf
));
3311 if (!is_error(ret
)) {
3312 /* convert host ifc_len to target ifc_len */
3314 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
3315 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
3316 host_ifconf
->ifc_len
= target_ifc_len
;
3318 /* restore target ifc_buf */
3320 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
3322 /* copy struct ifconf to target user */
3324 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3326 return -TARGET_EFAULT
;
3327 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
3328 unlock_user(argptr
, arg
, target_size
);
3330 /* copy ifreq[] to target user */
3332 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
3333 for (i
= 0; i
< nb_ifreq
; i
++) {
3334 thunk_convert(argptr
+ i
* target_ifreq_size
,
3335 host_ifc_buf
+ i
* sizeof(struct ifreq
),
3336 ifreq_arg_type
, THUNK_TARGET
);
3338 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
3348 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
3349 abi_long cmd
, abi_long arg
)
3352 struct dm_ioctl
*host_dm
;
3353 abi_long guest_data
;
3354 uint32_t guest_data_size
;
3356 const argtype
*arg_type
= ie
->arg_type
;
3358 void *big_buf
= NULL
;
3362 target_size
= thunk_type_size(arg_type
, 0);
3363 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3365 ret
= -TARGET_EFAULT
;
3368 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3369 unlock_user(argptr
, arg
, 0);
3371 /* buf_temp is too small, so fetch things into a bigger buffer */
3372 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
3373 memcpy(big_buf
, buf_temp
, target_size
);
3377 guest_data
= arg
+ host_dm
->data_start
;
3378 if ((guest_data
- arg
) < 0) {
3382 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
3383 host_data
= (char*)host_dm
+ host_dm
->data_start
;
3385 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
3386 switch (ie
->host_cmd
) {
3388 case DM_LIST_DEVICES
:
3391 case DM_DEV_SUSPEND
:
3394 case DM_TABLE_STATUS
:
3395 case DM_TABLE_CLEAR
:
3397 case DM_LIST_VERSIONS
:
3401 case DM_DEV_SET_GEOMETRY
:
3402 /* data contains only strings */
3403 memcpy(host_data
, argptr
, guest_data_size
);
3406 memcpy(host_data
, argptr
, guest_data_size
);
3407 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
3411 void *gspec
= argptr
;
3412 void *cur_data
= host_data
;
3413 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
3414 int spec_size
= thunk_type_size(arg_type
, 0);
3417 for (i
= 0; i
< host_dm
->target_count
; i
++) {
3418 struct dm_target_spec
*spec
= cur_data
;
3422 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
3423 slen
= strlen((char*)gspec
+ spec_size
) + 1;
3425 spec
->next
= sizeof(*spec
) + slen
;
3426 strcpy((char*)&spec
[1], gspec
+ spec_size
);
3428 cur_data
+= spec
->next
;
3433 ret
= -TARGET_EINVAL
;
3436 unlock_user(argptr
, guest_data
, 0);
3438 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3439 if (!is_error(ret
)) {
3440 guest_data
= arg
+ host_dm
->data_start
;
3441 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
3442 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
3443 switch (ie
->host_cmd
) {
3448 case DM_DEV_SUSPEND
:
3451 case DM_TABLE_CLEAR
:
3453 case DM_DEV_SET_GEOMETRY
:
3454 /* no return data */
3456 case DM_LIST_DEVICES
:
3458 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
3459 uint32_t remaining_data
= guest_data_size
;
3460 void *cur_data
= argptr
;
3461 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
3462 int nl_size
= 12; /* can't use thunk_size due to alignment */
3465 uint32_t next
= nl
->next
;
3467 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
3469 if (remaining_data
< nl
->next
) {
3470 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3473 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
3474 strcpy(cur_data
+ nl_size
, nl
->name
);
3475 cur_data
+= nl
->next
;
3476 remaining_data
-= nl
->next
;
3480 nl
= (void*)nl
+ next
;
3485 case DM_TABLE_STATUS
:
3487 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
3488 void *cur_data
= argptr
;
3489 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
3490 int spec_size
= thunk_type_size(arg_type
, 0);
3493 for (i
= 0; i
< host_dm
->target_count
; i
++) {
3494 uint32_t next
= spec
->next
;
3495 int slen
= strlen((char*)&spec
[1]) + 1;
3496 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
3497 if (guest_data_size
< spec
->next
) {
3498 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3501 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
3502 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
3503 cur_data
= argptr
+ spec
->next
;
3504 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
3510 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
3511 int count
= *(uint32_t*)hdata
;
3512 uint64_t *hdev
= hdata
+ 8;
3513 uint64_t *gdev
= argptr
+ 8;
3516 *(uint32_t*)argptr
= tswap32(count
);
3517 for (i
= 0; i
< count
; i
++) {
3518 *gdev
= tswap64(*hdev
);
3524 case DM_LIST_VERSIONS
:
3526 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
3527 uint32_t remaining_data
= guest_data_size
;
3528 void *cur_data
= argptr
;
3529 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
3530 int vers_size
= thunk_type_size(arg_type
, 0);
3533 uint32_t next
= vers
->next
;
3535 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
3537 if (remaining_data
< vers
->next
) {
3538 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3541 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
3542 strcpy(cur_data
+ vers_size
, vers
->name
);
3543 cur_data
+= vers
->next
;
3544 remaining_data
-= vers
->next
;
3548 vers
= (void*)vers
+ next
;
3553 ret
= -TARGET_EINVAL
;
3556 unlock_user(argptr
, guest_data
, guest_data_size
);
3558 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3560 ret
= -TARGET_EFAULT
;
3563 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3564 unlock_user(argptr
, arg
, target_size
);
3571 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3572 int fd
, abi_long cmd
, abi_long arg
)
3574 const argtype
*arg_type
= ie
->arg_type
;
3575 const StructEntry
*se
;
3576 const argtype
*field_types
;
3577 const int *dst_offsets
, *src_offsets
;
3580 abi_ulong
*target_rt_dev_ptr
;
3581 unsigned long *host_rt_dev_ptr
;
3585 assert(ie
->access
== IOC_W
);
3586 assert(*arg_type
== TYPE_PTR
);
3588 assert(*arg_type
== TYPE_STRUCT
);
3589 target_size
= thunk_type_size(arg_type
, 0);
3590 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3592 return -TARGET_EFAULT
;
3595 assert(*arg_type
== (int)STRUCT_rtentry
);
3596 se
= struct_entries
+ *arg_type
++;
3597 assert(se
->convert
[0] == NULL
);
3598 /* convert struct here to be able to catch rt_dev string */
3599 field_types
= se
->field_types
;
3600 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
3601 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
3602 for (i
= 0; i
< se
->nb_fields
; i
++) {
3603 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
3604 assert(*field_types
== TYPE_PTRVOID
);
3605 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
3606 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
3607 if (*target_rt_dev_ptr
!= 0) {
3608 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
3609 tswapal(*target_rt_dev_ptr
));
3610 if (!*host_rt_dev_ptr
) {
3611 unlock_user(argptr
, arg
, 0);
3612 return -TARGET_EFAULT
;
3615 *host_rt_dev_ptr
= 0;
3620 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
3621 argptr
+ src_offsets
[i
],
3622 field_types
, THUNK_HOST
);
3624 unlock_user(argptr
, arg
, 0);
3626 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3627 if (*host_rt_dev_ptr
!= 0) {
3628 unlock_user((void *)*host_rt_dev_ptr
,
3629 *target_rt_dev_ptr
, 0);
3634 static IOCTLEntry ioctl_entries
[] = {
3635 #define IOCTL(cmd, access, ...) \
3636 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3637 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3638 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
3643 /* ??? Implement proper locking for ioctls. */
3644 /* do_ioctl() Must return target values and target errnos. */
3645 static abi_long
do_ioctl(int fd
, abi_long cmd
, abi_long arg
)
3647 const IOCTLEntry
*ie
;
3648 const argtype
*arg_type
;
3650 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
3656 if (ie
->target_cmd
== 0) {
3657 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
3658 return -TARGET_ENOSYS
;
3660 if (ie
->target_cmd
== cmd
)
3664 arg_type
= ie
->arg_type
;
3666 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd
, ie
->name
);
3669 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
3672 switch(arg_type
[0]) {
3675 ret
= get_errno(ioctl(fd
, ie
->host_cmd
));
3680 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, arg
));
3684 target_size
= thunk_type_size(arg_type
, 0);
3685 switch(ie
->access
) {
3687 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3688 if (!is_error(ret
)) {
3689 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3691 return -TARGET_EFAULT
;
3692 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3693 unlock_user(argptr
, arg
, target_size
);
3697 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3699 return -TARGET_EFAULT
;
3700 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3701 unlock_user(argptr
, arg
, 0);
3702 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3706 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3708 return -TARGET_EFAULT
;
3709 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3710 unlock_user(argptr
, arg
, 0);
3711 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3712 if (!is_error(ret
)) {
3713 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3715 return -TARGET_EFAULT
;
3716 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3717 unlock_user(argptr
, arg
, target_size
);
3723 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3724 (long)cmd
, arg_type
[0]);
3725 ret
= -TARGET_ENOSYS
;
3731 static const bitmask_transtbl iflag_tbl
[] = {
3732 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
3733 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
3734 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
3735 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
3736 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
3737 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
3738 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
3739 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
3740 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
3741 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
3742 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
3743 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
3744 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
3745 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
3749 static const bitmask_transtbl oflag_tbl
[] = {
3750 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
3751 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
3752 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
3753 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
3754 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
3755 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
3756 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
3757 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
3758 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
3759 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
3760 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
3761 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
3762 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
3763 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
3764 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
3765 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
3766 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
3767 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
3768 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
3769 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
3770 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
3771 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
3772 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
3773 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
3777 static const bitmask_transtbl cflag_tbl
[] = {
3778 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
3779 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
3780 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
3781 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
3782 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
3783 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
3784 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
3785 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
3786 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
3787 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
3788 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
3789 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
3790 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
3791 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
3792 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
3793 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
3794 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
3795 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
3796 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
3797 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
3798 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
3799 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
3800 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
3801 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
3802 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
3803 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
3804 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
3805 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
3806 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
3807 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
3808 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
3812 static const bitmask_transtbl lflag_tbl
[] = {
3813 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
3814 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
3815 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
3816 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
3817 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
3818 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
3819 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
3820 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
3821 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
3822 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
3823 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
3824 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
3825 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
3826 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
3827 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
3831 static void target_to_host_termios (void *dst
, const void *src
)
3833 struct host_termios
*host
= dst
;
3834 const struct target_termios
*target
= src
;
3837 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
3839 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
3841 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
3843 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
3844 host
->c_line
= target
->c_line
;
3846 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
3847 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
3848 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
3849 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
3850 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
3851 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
3852 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
3853 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
3854 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
3855 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
3856 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
3857 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
3858 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
3859 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
3860 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
3861 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
3862 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
3863 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
3866 static void host_to_target_termios (void *dst
, const void *src
)
3868 struct target_termios
*target
= dst
;
3869 const struct host_termios
*host
= src
;
3872 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
3874 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
3876 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
3878 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
3879 target
->c_line
= host
->c_line
;
3881 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
3882 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
3883 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
3884 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
3885 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
3886 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
3887 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
3888 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
3889 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
3890 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
3891 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
3892 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
3893 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
3894 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
3895 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
3896 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
3897 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
3898 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
3901 static const StructEntry struct_termios_def
= {
3902 .convert
= { host_to_target_termios
, target_to_host_termios
},
3903 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
3904 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
3907 static bitmask_transtbl mmap_flags_tbl
[] = {
3908 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
3909 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
3910 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
3911 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
, MAP_ANONYMOUS
, MAP_ANONYMOUS
},
3912 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
, MAP_GROWSDOWN
, MAP_GROWSDOWN
},
3913 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
, MAP_DENYWRITE
, MAP_DENYWRITE
},
3914 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
, MAP_EXECUTABLE
, MAP_EXECUTABLE
},
3915 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
3919 #if defined(TARGET_I386)
3921 /* NOTE: there is really one LDT for all the threads */
3922 static uint8_t *ldt_table
;
3924 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
3931 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
3932 if (size
> bytecount
)
3934 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
3936 return -TARGET_EFAULT
;
3937 /* ??? Should this by byteswapped? */
3938 memcpy(p
, ldt_table
, size
);
3939 unlock_user(p
, ptr
, size
);
3943 /* XXX: add locking support */
3944 static abi_long
write_ldt(CPUX86State
*env
,
3945 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
3947 struct target_modify_ldt_ldt_s ldt_info
;
3948 struct target_modify_ldt_ldt_s
*target_ldt_info
;
3949 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
3950 int seg_not_present
, useable
, lm
;
3951 uint32_t *lp
, entry_1
, entry_2
;
3953 if (bytecount
!= sizeof(ldt_info
))
3954 return -TARGET_EINVAL
;
3955 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
3956 return -TARGET_EFAULT
;
3957 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
3958 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
3959 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
3960 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
3961 unlock_user_struct(target_ldt_info
, ptr
, 0);
3963 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
3964 return -TARGET_EINVAL
;
3965 seg_32bit
= ldt_info
.flags
& 1;
3966 contents
= (ldt_info
.flags
>> 1) & 3;
3967 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
3968 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
3969 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
3970 useable
= (ldt_info
.flags
>> 6) & 1;
3974 lm
= (ldt_info
.flags
>> 7) & 1;
3976 if (contents
== 3) {
3978 return -TARGET_EINVAL
;
3979 if (seg_not_present
== 0)
3980 return -TARGET_EINVAL
;
3982 /* allocate the LDT */
3984 env
->ldt
.base
= target_mmap(0,
3985 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
3986 PROT_READ
|PROT_WRITE
,
3987 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
3988 if (env
->ldt
.base
== -1)
3989 return -TARGET_ENOMEM
;
3990 memset(g2h(env
->ldt
.base
), 0,
3991 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
3992 env
->ldt
.limit
= 0xffff;
3993 ldt_table
= g2h(env
->ldt
.base
);
3996 /* NOTE: same code as Linux kernel */
3997 /* Allow LDTs to be cleared by the user. */
3998 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
4001 read_exec_only
== 1 &&
4003 limit_in_pages
== 0 &&
4004 seg_not_present
== 1 &&
4012 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
4013 (ldt_info
.limit
& 0x0ffff);
4014 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
4015 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
4016 (ldt_info
.limit
& 0xf0000) |
4017 ((read_exec_only
^ 1) << 9) |
4019 ((seg_not_present
^ 1) << 15) |
4021 (limit_in_pages
<< 23) |
4025 entry_2
|= (useable
<< 20);
4027 /* Install the new entry ... */
4029 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
4030 lp
[0] = tswap32(entry_1
);
4031 lp
[1] = tswap32(entry_2
);
4035 /* specific and weird i386 syscalls */
4036 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
4037 unsigned long bytecount
)
4043 ret
= read_ldt(ptr
, bytecount
);
4046 ret
= write_ldt(env
, ptr
, bytecount
, 1);
4049 ret
= write_ldt(env
, ptr
, bytecount
, 0);
4052 ret
= -TARGET_ENOSYS
;
4058 #if defined(TARGET_I386) && defined(TARGET_ABI32)
4059 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
4061 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
4062 struct target_modify_ldt_ldt_s ldt_info
;
4063 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4064 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
4065 int seg_not_present
, useable
, lm
;
4066 uint32_t *lp
, entry_1
, entry_2
;
4069 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
4070 if (!target_ldt_info
)
4071 return -TARGET_EFAULT
;
4072 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
4073 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
4074 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
4075 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
4076 if (ldt_info
.entry_number
== -1) {
4077 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
4078 if (gdt_table
[i
] == 0) {
4079 ldt_info
.entry_number
= i
;
4080 target_ldt_info
->entry_number
= tswap32(i
);
4085 unlock_user_struct(target_ldt_info
, ptr
, 1);
4087 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
4088 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
4089 return -TARGET_EINVAL
;
4090 seg_32bit
= ldt_info
.flags
& 1;
4091 contents
= (ldt_info
.flags
>> 1) & 3;
4092 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
4093 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
4094 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
4095 useable
= (ldt_info
.flags
>> 6) & 1;
4099 lm
= (ldt_info
.flags
>> 7) & 1;
4102 if (contents
== 3) {
4103 if (seg_not_present
== 0)
4104 return -TARGET_EINVAL
;
4107 /* NOTE: same code as Linux kernel */
4108 /* Allow LDTs to be cleared by the user. */
4109 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
4110 if ((contents
== 0 &&
4111 read_exec_only
== 1 &&
4113 limit_in_pages
== 0 &&
4114 seg_not_present
== 1 &&
4122 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
4123 (ldt_info
.limit
& 0x0ffff);
4124 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
4125 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
4126 (ldt_info
.limit
& 0xf0000) |
4127 ((read_exec_only
^ 1) << 9) |
4129 ((seg_not_present
^ 1) << 15) |
4131 (limit_in_pages
<< 23) |
4136 /* Install the new entry ... */
4138 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
4139 lp
[0] = tswap32(entry_1
);
4140 lp
[1] = tswap32(entry_2
);
4144 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
4146 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4147 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
4148 uint32_t base_addr
, limit
, flags
;
4149 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
4150 int seg_not_present
, useable
, lm
;
4151 uint32_t *lp
, entry_1
, entry_2
;
4153 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
4154 if (!target_ldt_info
)
4155 return -TARGET_EFAULT
;
4156 idx
= tswap32(target_ldt_info
->entry_number
);
4157 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
4158 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
4159 unlock_user_struct(target_ldt_info
, ptr
, 1);
4160 return -TARGET_EINVAL
;
4162 lp
= (uint32_t *)(gdt_table
+ idx
);
4163 entry_1
= tswap32(lp
[0]);
4164 entry_2
= tswap32(lp
[1]);
4166 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
4167 contents
= (entry_2
>> 10) & 3;
4168 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
4169 seg_32bit
= (entry_2
>> 22) & 1;
4170 limit_in_pages
= (entry_2
>> 23) & 1;
4171 useable
= (entry_2
>> 20) & 1;
4175 lm
= (entry_2
>> 21) & 1;
4177 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
4178 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
4179 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
4180 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
4181 base_addr
= (entry_1
>> 16) |
4182 (entry_2
& 0xff000000) |
4183 ((entry_2
& 0xff) << 16);
4184 target_ldt_info
->base_addr
= tswapal(base_addr
);
4185 target_ldt_info
->limit
= tswap32(limit
);
4186 target_ldt_info
->flags
= tswap32(flags
);
4187 unlock_user_struct(target_ldt_info
, ptr
, 1);
4190 #endif /* TARGET_I386 && TARGET_ABI32 */
4192 #ifndef TARGET_ABI32
4193 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
4200 case TARGET_ARCH_SET_GS
:
4201 case TARGET_ARCH_SET_FS
:
4202 if (code
== TARGET_ARCH_SET_GS
)
4206 cpu_x86_load_seg(env
, idx
, 0);
4207 env
->segs
[idx
].base
= addr
;
4209 case TARGET_ARCH_GET_GS
:
4210 case TARGET_ARCH_GET_FS
:
4211 if (code
== TARGET_ARCH_GET_GS
)
4215 val
= env
->segs
[idx
].base
;
4216 if (put_user(val
, addr
, abi_ulong
))
4217 ret
= -TARGET_EFAULT
;
4220 ret
= -TARGET_EINVAL
;
4227 #endif /* defined(TARGET_I386) */
4229 #define NEW_STACK_SIZE 0x40000
4232 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
4235 pthread_mutex_t mutex
;
4236 pthread_cond_t cond
;
4239 abi_ulong child_tidptr
;
4240 abi_ulong parent_tidptr
;
4244 static void *clone_func(void *arg
)
4246 new_thread_info
*info
= arg
;
4252 cpu
= ENV_GET_CPU(env
);
4254 ts
= (TaskState
*)cpu
->opaque
;
4255 info
->tid
= gettid();
4256 cpu
->host_tid
= info
->tid
;
4258 if (info
->child_tidptr
)
4259 put_user_u32(info
->tid
, info
->child_tidptr
);
4260 if (info
->parent_tidptr
)
4261 put_user_u32(info
->tid
, info
->parent_tidptr
);
4262 /* Enable signals. */
4263 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
4264 /* Signal to the parent that we're ready. */
4265 pthread_mutex_lock(&info
->mutex
);
4266 pthread_cond_broadcast(&info
->cond
);
4267 pthread_mutex_unlock(&info
->mutex
);
4268 /* Wait until the parent has finshed initializing the tls state. */
4269 pthread_mutex_lock(&clone_lock
);
4270 pthread_mutex_unlock(&clone_lock
);
4276 /* do_fork() Must return host values and target errnos (unlike most
4277 do_*() functions). */
4278 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
4279 abi_ulong parent_tidptr
, target_ulong newtls
,
4280 abi_ulong child_tidptr
)
4282 CPUState
*cpu
= ENV_GET_CPU(env
);
4286 CPUArchState
*new_env
;
4287 unsigned int nptl_flags
;
4290 /* Emulate vfork() with fork() */
4291 if (flags
& CLONE_VFORK
)
4292 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
4294 if (flags
& CLONE_VM
) {
4295 TaskState
*parent_ts
= (TaskState
*)cpu
->opaque
;
4296 new_thread_info info
;
4297 pthread_attr_t attr
;
4299 ts
= g_malloc0(sizeof(TaskState
));
4300 init_task_state(ts
);
4301 /* we create a new CPU instance. */
4302 new_env
= cpu_copy(env
);
4303 /* Init regs that differ from the parent. */
4304 cpu_clone_regs(new_env
, newsp
);
4305 new_cpu
= ENV_GET_CPU(new_env
);
4306 new_cpu
->opaque
= ts
;
4307 ts
->bprm
= parent_ts
->bprm
;
4308 ts
->info
= parent_ts
->info
;
4310 flags
&= ~CLONE_NPTL_FLAGS2
;
4312 if (nptl_flags
& CLONE_CHILD_CLEARTID
) {
4313 ts
->child_tidptr
= child_tidptr
;
4316 if (nptl_flags
& CLONE_SETTLS
)
4317 cpu_set_tls (new_env
, newtls
);
4319 /* Grab a mutex so that thread setup appears atomic. */
4320 pthread_mutex_lock(&clone_lock
);
4322 memset(&info
, 0, sizeof(info
));
4323 pthread_mutex_init(&info
.mutex
, NULL
);
4324 pthread_mutex_lock(&info
.mutex
);
4325 pthread_cond_init(&info
.cond
, NULL
);
4327 if (nptl_flags
& CLONE_CHILD_SETTID
)
4328 info
.child_tidptr
= child_tidptr
;
4329 if (nptl_flags
& CLONE_PARENT_SETTID
)
4330 info
.parent_tidptr
= parent_tidptr
;
4332 ret
= pthread_attr_init(&attr
);
4333 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
4334 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
4335 /* It is not safe to deliver signals until the child has finished
4336 initializing, so temporarily block all signals. */
4337 sigfillset(&sigmask
);
4338 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
4340 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
4341 /* TODO: Free new CPU state if thread creation failed. */
4343 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
4344 pthread_attr_destroy(&attr
);
4346 /* Wait for the child to initialize. */
4347 pthread_cond_wait(&info
.cond
, &info
.mutex
);
4349 if (flags
& CLONE_PARENT_SETTID
)
4350 put_user_u32(ret
, parent_tidptr
);
4354 pthread_mutex_unlock(&info
.mutex
);
4355 pthread_cond_destroy(&info
.cond
);
4356 pthread_mutex_destroy(&info
.mutex
);
4357 pthread_mutex_unlock(&clone_lock
);
4359 /* if no CLONE_VM, we consider it is a fork */
4360 if ((flags
& ~(CSIGNAL
| CLONE_NPTL_FLAGS2
)) != 0)
4365 /* Child Process. */
4366 cpu_clone_regs(env
, newsp
);
4368 /* There is a race condition here. The parent process could
4369 theoretically read the TID in the child process before the child
4370 tid is set. This would require using either ptrace
4371 (not implemented) or having *_tidptr to point at a shared memory
4372 mapping. We can't repeat the spinlock hack used above because
4373 the child process gets its own copy of the lock. */
4374 if (flags
& CLONE_CHILD_SETTID
)
4375 put_user_u32(gettid(), child_tidptr
);
4376 if (flags
& CLONE_PARENT_SETTID
)
4377 put_user_u32(gettid(), parent_tidptr
);
4378 ts
= (TaskState
*)cpu
->opaque
;
4379 if (flags
& CLONE_SETTLS
)
4380 cpu_set_tls (env
, newtls
);
4381 if (flags
& CLONE_CHILD_CLEARTID
)
4382 ts
->child_tidptr
= child_tidptr
;
4390 /* warning : doesn't handle linux specific flags... */
4391 static int target_to_host_fcntl_cmd(int cmd
)
4394 case TARGET_F_DUPFD
:
4395 case TARGET_F_GETFD
:
4396 case TARGET_F_SETFD
:
4397 case TARGET_F_GETFL
:
4398 case TARGET_F_SETFL
:
4400 case TARGET_F_GETLK
:
4402 case TARGET_F_SETLK
:
4404 case TARGET_F_SETLKW
:
4406 case TARGET_F_GETOWN
:
4408 case TARGET_F_SETOWN
:
4410 case TARGET_F_GETSIG
:
4412 case TARGET_F_SETSIG
:
4414 #if TARGET_ABI_BITS == 32
4415 case TARGET_F_GETLK64
:
4417 case TARGET_F_SETLK64
:
4419 case TARGET_F_SETLKW64
:
4422 case TARGET_F_SETLEASE
:
4424 case TARGET_F_GETLEASE
:
4426 #ifdef F_DUPFD_CLOEXEC
4427 case TARGET_F_DUPFD_CLOEXEC
:
4428 return F_DUPFD_CLOEXEC
;
4430 case TARGET_F_NOTIFY
:
4433 case TARGET_F_GETOWN_EX
:
4437 case TARGET_F_SETOWN_EX
:
4441 return -TARGET_EINVAL
;
4443 return -TARGET_EINVAL
;
4446 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4447 static const bitmask_transtbl flock_tbl
[] = {
4448 TRANSTBL_CONVERT(F_RDLCK
),
4449 TRANSTBL_CONVERT(F_WRLCK
),
4450 TRANSTBL_CONVERT(F_UNLCK
),
4451 TRANSTBL_CONVERT(F_EXLCK
),
4452 TRANSTBL_CONVERT(F_SHLCK
),
4456 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
4459 struct target_flock
*target_fl
;
4460 struct flock64 fl64
;
4461 struct target_flock64
*target_fl64
;
4463 struct f_owner_ex fox
;
4464 struct target_f_owner_ex
*target_fox
;
4467 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
4469 if (host_cmd
== -TARGET_EINVAL
)
4473 case TARGET_F_GETLK
:
4474 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
4475 return -TARGET_EFAULT
;
4477 target_to_host_bitmask(tswap16(target_fl
->l_type
), flock_tbl
);
4478 fl
.l_whence
= tswap16(target_fl
->l_whence
);
4479 fl
.l_start
= tswapal(target_fl
->l_start
);
4480 fl
.l_len
= tswapal(target_fl
->l_len
);
4481 fl
.l_pid
= tswap32(target_fl
->l_pid
);
4482 unlock_user_struct(target_fl
, arg
, 0);
4483 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
4485 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg
, 0))
4486 return -TARGET_EFAULT
;
4488 host_to_target_bitmask(tswap16(fl
.l_type
), flock_tbl
);
4489 target_fl
->l_whence
= tswap16(fl
.l_whence
);
4490 target_fl
->l_start
= tswapal(fl
.l_start
);
4491 target_fl
->l_len
= tswapal(fl
.l_len
);
4492 target_fl
->l_pid
= tswap32(fl
.l_pid
);
4493 unlock_user_struct(target_fl
, arg
, 1);
4497 case TARGET_F_SETLK
:
4498 case TARGET_F_SETLKW
:
4499 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
4500 return -TARGET_EFAULT
;
4502 target_to_host_bitmask(tswap16(target_fl
->l_type
), flock_tbl
);
4503 fl
.l_whence
= tswap16(target_fl
->l_whence
);
4504 fl
.l_start
= tswapal(target_fl
->l_start
);
4505 fl
.l_len
= tswapal(target_fl
->l_len
);
4506 fl
.l_pid
= tswap32(target_fl
->l_pid
);
4507 unlock_user_struct(target_fl
, arg
, 0);
4508 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
4511 case TARGET_F_GETLK64
:
4512 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
4513 return -TARGET_EFAULT
;
4515 target_to_host_bitmask(tswap16(target_fl64
->l_type
), flock_tbl
) >> 1;
4516 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
4517 fl64
.l_start
= tswap64(target_fl64
->l_start
);
4518 fl64
.l_len
= tswap64(target_fl64
->l_len
);
4519 fl64
.l_pid
= tswap32(target_fl64
->l_pid
);
4520 unlock_user_struct(target_fl64
, arg
, 0);
4521 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
4523 if (!lock_user_struct(VERIFY_WRITE
, target_fl64
, arg
, 0))
4524 return -TARGET_EFAULT
;
4525 target_fl64
->l_type
=
4526 host_to_target_bitmask(tswap16(fl64
.l_type
), flock_tbl
) >> 1;
4527 target_fl64
->l_whence
= tswap16(fl64
.l_whence
);
4528 target_fl64
->l_start
= tswap64(fl64
.l_start
);
4529 target_fl64
->l_len
= tswap64(fl64
.l_len
);
4530 target_fl64
->l_pid
= tswap32(fl64
.l_pid
);
4531 unlock_user_struct(target_fl64
, arg
, 1);
4534 case TARGET_F_SETLK64
:
4535 case TARGET_F_SETLKW64
:
4536 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
4537 return -TARGET_EFAULT
;
4539 target_to_host_bitmask(tswap16(target_fl64
->l_type
), flock_tbl
) >> 1;
4540 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
4541 fl64
.l_start
= tswap64(target_fl64
->l_start
);
4542 fl64
.l_len
= tswap64(target_fl64
->l_len
);
4543 fl64
.l_pid
= tswap32(target_fl64
->l_pid
);
4544 unlock_user_struct(target_fl64
, arg
, 0);
4545 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
4548 case TARGET_F_GETFL
:
4549 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
4551 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
4555 case TARGET_F_SETFL
:
4556 ret
= get_errno(fcntl(fd
, host_cmd
, target_to_host_bitmask(arg
, fcntl_flags_tbl
)));
4560 case TARGET_F_GETOWN_EX
:
4561 ret
= get_errno(fcntl(fd
, host_cmd
, &fox
));
4563 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
4564 return -TARGET_EFAULT
;
4565 target_fox
->type
= tswap32(fox
.type
);
4566 target_fox
->pid
= tswap32(fox
.pid
);
4567 unlock_user_struct(target_fox
, arg
, 1);
4573 case TARGET_F_SETOWN_EX
:
4574 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
4575 return -TARGET_EFAULT
;
4576 fox
.type
= tswap32(target_fox
->type
);
4577 fox
.pid
= tswap32(target_fox
->pid
);
4578 unlock_user_struct(target_fox
, arg
, 0);
4579 ret
= get_errno(fcntl(fd
, host_cmd
, &fox
));
4583 case TARGET_F_SETOWN
:
4584 case TARGET_F_GETOWN
:
4585 case TARGET_F_SETSIG
:
4586 case TARGET_F_GETSIG
:
4587 case TARGET_F_SETLEASE
:
4588 case TARGET_F_GETLEASE
:
4589 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
4593 ret
= get_errno(fcntl(fd
, cmd
, arg
));
4601 static inline int high2lowuid(int uid
)
4609 static inline int high2lowgid(int gid
)
4617 static inline int low2highuid(int uid
)
4619 if ((int16_t)uid
== -1)
4625 static inline int low2highgid(int gid
)
4627 if ((int16_t)gid
== -1)
4632 static inline int tswapid(int id
)
4637 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
4639 #else /* !USE_UID16 */
4640 static inline int high2lowuid(int uid
)
4644 static inline int high2lowgid(int gid
)
4648 static inline int low2highuid(int uid
)
4652 static inline int low2highgid(int gid
)
4656 static inline int tswapid(int id
)
4661 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
4663 #endif /* USE_UID16 */
4665 void syscall_init(void)
4668 const argtype
*arg_type
;
4672 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
4673 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
4674 #include "syscall_types.h"
4676 #undef STRUCT_SPECIAL
4678 /* Build target_to_host_errno_table[] table from
4679 * host_to_target_errno_table[]. */
4680 for (i
= 0; i
< ERRNO_TABLE_SIZE
; i
++) {
4681 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
4684 /* we patch the ioctl size if necessary. We rely on the fact that
4685 no ioctl has all the bits at '1' in the size field */
4687 while (ie
->target_cmd
!= 0) {
4688 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
4689 TARGET_IOC_SIZEMASK
) {
4690 arg_type
= ie
->arg_type
;
4691 if (arg_type
[0] != TYPE_PTR
) {
4692 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
4697 size
= thunk_type_size(arg_type
, 0);
4698 ie
->target_cmd
= (ie
->target_cmd
&
4699 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
4700 (size
<< TARGET_IOC_SIZESHIFT
);
4703 /* automatic consistency check if same arch */
4704 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4705 (defined(__x86_64__) && defined(TARGET_X86_64))
4706 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
4707 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4708 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
4715 #if TARGET_ABI_BITS == 32
4716 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
4718 #ifdef TARGET_WORDS_BIGENDIAN
4719 return ((uint64_t)word0
<< 32) | word1
;
4721 return ((uint64_t)word1
<< 32) | word0
;
4724 #else /* TARGET_ABI_BITS == 32 */
4725 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
4729 #endif /* TARGET_ABI_BITS != 32 */
4731 #ifdef TARGET_NR_truncate64
4732 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
4737 if (regpairs_aligned(cpu_env
)) {
4741 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
4745 #ifdef TARGET_NR_ftruncate64
4746 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
4751 if (regpairs_aligned(cpu_env
)) {
4755 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
4759 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
4760 abi_ulong target_addr
)
4762 struct target_timespec
*target_ts
;
4764 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1))
4765 return -TARGET_EFAULT
;
4766 host_ts
->tv_sec
= tswapal(target_ts
->tv_sec
);
4767 host_ts
->tv_nsec
= tswapal(target_ts
->tv_nsec
);
4768 unlock_user_struct(target_ts
, target_addr
, 0);
4772 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
4773 struct timespec
*host_ts
)
4775 struct target_timespec
*target_ts
;
4777 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0))
4778 return -TARGET_EFAULT
;
4779 target_ts
->tv_sec
= tswapal(host_ts
->tv_sec
);
4780 target_ts
->tv_nsec
= tswapal(host_ts
->tv_nsec
);
4781 unlock_user_struct(target_ts
, target_addr
, 1);
4785 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_itspec
,
4786 abi_ulong target_addr
)
4788 struct target_itimerspec
*target_itspec
;
4790 if (!lock_user_struct(VERIFY_READ
, target_itspec
, target_addr
, 1)) {
4791 return -TARGET_EFAULT
;
4794 host_itspec
->it_interval
.tv_sec
=
4795 tswapal(target_itspec
->it_interval
.tv_sec
);
4796 host_itspec
->it_interval
.tv_nsec
=
4797 tswapal(target_itspec
->it_interval
.tv_nsec
);
4798 host_itspec
->it_value
.tv_sec
= tswapal(target_itspec
->it_value
.tv_sec
);
4799 host_itspec
->it_value
.tv_nsec
= tswapal(target_itspec
->it_value
.tv_nsec
);
4801 unlock_user_struct(target_itspec
, target_addr
, 1);
4805 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
4806 struct itimerspec
*host_its
)
4808 struct target_itimerspec
*target_itspec
;
4810 if (!lock_user_struct(VERIFY_WRITE
, target_itspec
, target_addr
, 0)) {
4811 return -TARGET_EFAULT
;
4814 target_itspec
->it_interval
.tv_sec
= tswapal(host_its
->it_interval
.tv_sec
);
4815 target_itspec
->it_interval
.tv_nsec
= tswapal(host_its
->it_interval
.tv_nsec
);
4817 target_itspec
->it_value
.tv_sec
= tswapal(host_its
->it_value
.tv_sec
);
4818 target_itspec
->it_value
.tv_nsec
= tswapal(host_its
->it_value
.tv_nsec
);
4820 unlock_user_struct(target_itspec
, target_addr
, 0);
4824 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
4825 static inline abi_long
host_to_target_stat64(void *cpu_env
,
4826 abi_ulong target_addr
,
4827 struct stat
*host_st
)
4829 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
4830 if (((CPUARMState
*)cpu_env
)->eabi
) {
4831 struct target_eabi_stat64
*target_st
;
4833 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
4834 return -TARGET_EFAULT
;
4835 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
4836 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
4837 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
4838 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4839 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
4841 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
4842 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
4843 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
4844 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
4845 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
4846 __put_user(host_st
->st_size
, &target_st
->st_size
);
4847 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
4848 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
4849 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
4850 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
4851 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
4852 unlock_user_struct(target_st
, target_addr
, 1);
4856 #if defined(TARGET_HAS_STRUCT_STAT64)
4857 struct target_stat64
*target_st
;
4859 struct target_stat
*target_st
;
4862 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
4863 return -TARGET_EFAULT
;
4864 memset(target_st
, 0, sizeof(*target_st
));
4865 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
4866 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
4867 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4868 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
4870 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
4871 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
4872 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
4873 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
4874 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
4875 /* XXX: better use of kernel struct */
4876 __put_user(host_st
->st_size
, &target_st
->st_size
);
4877 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
4878 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
4879 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
4880 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
4881 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
4882 unlock_user_struct(target_st
, target_addr
, 1);
4889 /* ??? Using host futex calls even when target atomic operations
4890 are not really atomic probably breaks things. However implementing
4891 futexes locally would make futexes shared between multiple processes
4892 tricky. However they're probably useless because guest atomic
4893 operations won't work either. */
4894 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
4895 target_ulong uaddr2
, int val3
)
4897 struct timespec ts
, *pts
;
4900 /* ??? We assume FUTEX_* constants are the same on both host
4902 #ifdef FUTEX_CMD_MASK
4903 base_op
= op
& FUTEX_CMD_MASK
;
4909 case FUTEX_WAIT_BITSET
:
4912 target_to_host_timespec(pts
, timeout
);
4916 return get_errno(sys_futex(g2h(uaddr
), op
, tswap32(val
),
4919 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
4921 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
4923 case FUTEX_CMP_REQUEUE
:
4925 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4926 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4927 But the prototype takes a `struct timespec *'; insert casts
4928 to satisfy the compiler. We do not need to tswap TIMEOUT
4929 since it's not compared to guest memory. */
4930 pts
= (struct timespec
*)(uintptr_t) timeout
;
4931 return get_errno(sys_futex(g2h(uaddr
), op
, val
, pts
,
4933 (base_op
== FUTEX_CMP_REQUEUE
4937 return -TARGET_ENOSYS
;
4941 /* Map host to target signal numbers for the wait family of syscalls.
4942 Assume all other status bits are the same. */
4943 int host_to_target_waitstatus(int status
)
4945 if (WIFSIGNALED(status
)) {
4946 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
4948 if (WIFSTOPPED(status
)) {
4949 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
4955 static int relstr_to_int(const char *s
)
4957 /* Convert a uname release string like "2.6.18" to an integer
4958 * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
4963 for (i
= 0; i
< 3; i
++) {
4965 while (*s
>= '0' && *s
<= '9') {
4970 tmp
= (tmp
<< 8) + n
;
4978 int get_osversion(void)
4980 static int osversion
;
4981 struct new_utsname buf
;
4986 if (qemu_uname_release
&& *qemu_uname_release
) {
4987 s
= qemu_uname_release
;
4989 if (sys_uname(&buf
))
4993 osversion
= relstr_to_int(s
);
4997 void init_qemu_uname_release(void)
4999 /* Initialize qemu_uname_release for later use.
5000 * If the host kernel is too old and the user hasn't asked for
5001 * a specific fake version number, we might want to fake a minimum
5002 * target kernel version.
5004 #ifdef UNAME_MINIMUM_RELEASE
5005 struct new_utsname buf
;
5007 if (qemu_uname_release
&& *qemu_uname_release
) {
5011 if (sys_uname(&buf
)) {
5015 if (relstr_to_int(buf
.release
) < relstr_to_int(UNAME_MINIMUM_RELEASE
)) {
5016 qemu_uname_release
= UNAME_MINIMUM_RELEASE
;
5021 static int open_self_maps(void *cpu_env
, int fd
)
5023 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
5024 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5025 TaskState
*ts
= cpu
->opaque
;
5032 fp
= fopen("/proc/self/maps", "r");
5037 while ((read
= getline(&line
, &len
, fp
)) != -1) {
5038 int fields
, dev_maj
, dev_min
, inode
;
5039 uint64_t min
, max
, offset
;
5040 char flag_r
, flag_w
, flag_x
, flag_p
;
5041 char path
[512] = "";
5042 fields
= sscanf(line
, "%"PRIx64
"-%"PRIx64
" %c%c%c%c %"PRIx64
" %x:%x %d"
5043 " %512s", &min
, &max
, &flag_r
, &flag_w
, &flag_x
,
5044 &flag_p
, &offset
, &dev_maj
, &dev_min
, &inode
, path
);
5046 if ((fields
< 10) || (fields
> 11)) {
5049 if (!strncmp(path
, "[stack]", 7)) {
5052 if (h2g_valid(min
) && h2g_valid(max
)) {
5053 dprintf(fd
, TARGET_ABI_FMT_lx
"-" TARGET_ABI_FMT_lx
5054 " %c%c%c%c %08" PRIx64
" %02x:%02x %d %s%s\n",
5055 h2g(min
), h2g(max
), flag_r
, flag_w
,
5056 flag_x
, flag_p
, offset
, dev_maj
, dev_min
, inode
,
5057 path
[0] ? " " : "", path
);
5064 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
5065 dprintf(fd
, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
5066 (unsigned long long)ts
->info
->stack_limit
,
5067 (unsigned long long)(ts
->info
->start_stack
+
5068 (TARGET_PAGE_SIZE
- 1)) & TARGET_PAGE_MASK
,
5069 (unsigned long long)0);
5075 static int open_self_stat(void *cpu_env
, int fd
)
5077 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5078 TaskState
*ts
= cpu
->opaque
;
5079 abi_ulong start_stack
= ts
->info
->start_stack
;
5082 for (i
= 0; i
< 44; i
++) {
5090 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
5091 } else if (i
== 1) {
5093 snprintf(buf
, sizeof(buf
), "(%s) ", ts
->bprm
->argv
[0]);
5094 } else if (i
== 27) {
5097 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
5099 /* for the rest, there is MasterCard */
5100 snprintf(buf
, sizeof(buf
), "0%c", i
== 43 ? '\n' : ' ');
5104 if (write(fd
, buf
, len
) != len
) {
5112 static int open_self_auxv(void *cpu_env
, int fd
)
5114 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5115 TaskState
*ts
= cpu
->opaque
;
5116 abi_ulong auxv
= ts
->info
->saved_auxv
;
5117 abi_ulong len
= ts
->info
->auxv_len
;
5121 * Auxiliary vector is stored in target process stack.
5122 * read in whole auxv vector and copy it to file
5124 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
5128 r
= write(fd
, ptr
, len
);
5135 lseek(fd
, 0, SEEK_SET
);
5136 unlock_user(ptr
, auxv
, len
);
5142 static int is_proc_myself(const char *filename
, const char *entry
)
5144 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
5145 filename
+= strlen("/proc/");
5146 if (!strncmp(filename
, "self/", strlen("self/"))) {
5147 filename
+= strlen("self/");
5148 } else if (*filename
>= '1' && *filename
<= '9') {
5150 snprintf(myself
, sizeof(myself
), "%d/", getpid());
5151 if (!strncmp(filename
, myself
, strlen(myself
))) {
5152 filename
+= strlen(myself
);
5159 if (!strcmp(filename
, entry
)) {
5166 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5167 static int is_proc(const char *filename
, const char *entry
)
5169 return strcmp(filename
, entry
) == 0;
5172 static int open_net_route(void *cpu_env
, int fd
)
5179 fp
= fopen("/proc/net/route", "r");
5186 read
= getline(&line
, &len
, fp
);
5187 dprintf(fd
, "%s", line
);
5191 while ((read
= getline(&line
, &len
, fp
)) != -1) {
5193 uint32_t dest
, gw
, mask
;
5194 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
5195 sscanf(line
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5196 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
5197 &mask
, &mtu
, &window
, &irtt
);
5198 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5199 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
5200 metric
, tswap32(mask
), mtu
, window
, irtt
);
5210 static int do_open(void *cpu_env
, const char *pathname
, int flags
, mode_t mode
)
5213 const char *filename
;
5214 int (*fill
)(void *cpu_env
, int fd
);
5215 int (*cmp
)(const char *s1
, const char *s2
);
5217 const struct fake_open
*fake_open
;
5218 static const struct fake_open fakes
[] = {
5219 { "maps", open_self_maps
, is_proc_myself
},
5220 { "stat", open_self_stat
, is_proc_myself
},
5221 { "auxv", open_self_auxv
, is_proc_myself
},
5222 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5223 { "/proc/net/route", open_net_route
, is_proc
},
5225 { NULL
, NULL
, NULL
}
5228 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
5229 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
5234 if (fake_open
->filename
) {
5236 char filename
[PATH_MAX
];
5239 /* create temporary file to map stat to */
5240 tmpdir
= getenv("TMPDIR");
5243 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
5244 fd
= mkstemp(filename
);
5250 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
5254 lseek(fd
, 0, SEEK_SET
);
5259 return get_errno(open(path(pathname
), flags
, mode
));
5262 /* do_syscall() should always have a single exit point at the end so
5263 that actions, such as logging of syscall results, can be performed.
5264 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
5265 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
5266 abi_long arg2
, abi_long arg3
, abi_long arg4
,
5267 abi_long arg5
, abi_long arg6
, abi_long arg7
,
5270 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
5277 gemu_log("syscall %d", num
);
5280 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
5283 case TARGET_NR_exit
:
5284 /* In old applications this may be used to implement _exit(2).
5285 However in threaded applictions it is used for thread termination,
5286 and _exit_group is used for application termination.
5287 Do thread termination if we have more then one thread. */
5288 /* FIXME: This probably breaks if a signal arrives. We should probably
5289 be disabling signals. */
5290 if (CPU_NEXT(first_cpu
)) {
5294 /* Remove the CPU from the list. */
5295 QTAILQ_REMOVE(&cpus
, cpu
, node
);
5298 if (ts
->child_tidptr
) {
5299 put_user_u32(0, ts
->child_tidptr
);
5300 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
5304 object_unref(OBJECT(cpu
));
5311 gdb_exit(cpu_env
, arg1
);
5313 ret
= 0; /* avoid warning */
5315 case TARGET_NR_read
:
5319 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
5321 ret
= get_errno(read(arg1
, p
, arg3
));
5322 unlock_user(p
, arg2
, ret
);
5325 case TARGET_NR_write
:
5326 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
5328 ret
= get_errno(write(arg1
, p
, arg3
));
5329 unlock_user(p
, arg2
, 0);
5331 case TARGET_NR_open
:
5332 if (!(p
= lock_user_string(arg1
)))
5334 ret
= get_errno(do_open(cpu_env
, p
,
5335 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
5337 unlock_user(p
, arg1
, 0);
5339 #if defined(TARGET_NR_openat) && defined(__NR_openat)
5340 case TARGET_NR_openat
:
5341 if (!(p
= lock_user_string(arg2
)))
5343 ret
= get_errno(sys_openat(arg1
,
5345 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
5347 unlock_user(p
, arg2
, 0);
5350 case TARGET_NR_close
:
5351 ret
= get_errno(close(arg1
));
5356 case TARGET_NR_fork
:
5357 ret
= get_errno(do_fork(cpu_env
, SIGCHLD
, 0, 0, 0, 0));
5359 #ifdef TARGET_NR_waitpid
5360 case TARGET_NR_waitpid
:
5363 ret
= get_errno(waitpid(arg1
, &status
, arg3
));
5364 if (!is_error(ret
) && arg2
&& ret
5365 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
5370 #ifdef TARGET_NR_waitid
5371 case TARGET_NR_waitid
:
5375 ret
= get_errno(waitid(arg1
, arg2
, &info
, arg4
));
5376 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
5377 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
5379 host_to_target_siginfo(p
, &info
);
5380 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
5385 #ifdef TARGET_NR_creat /* not on alpha */
5386 case TARGET_NR_creat
:
5387 if (!(p
= lock_user_string(arg1
)))
5389 ret
= get_errno(creat(p
, arg2
));
5390 unlock_user(p
, arg1
, 0);
5393 case TARGET_NR_link
:
5396 p
= lock_user_string(arg1
);
5397 p2
= lock_user_string(arg2
);
5399 ret
= -TARGET_EFAULT
;
5401 ret
= get_errno(link(p
, p2
));
5402 unlock_user(p2
, arg2
, 0);
5403 unlock_user(p
, arg1
, 0);
5406 #if defined(TARGET_NR_linkat)
5407 case TARGET_NR_linkat
:
5412 p
= lock_user_string(arg2
);
5413 p2
= lock_user_string(arg4
);
5415 ret
= -TARGET_EFAULT
;
5417 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
5418 unlock_user(p
, arg2
, 0);
5419 unlock_user(p2
, arg4
, 0);
5423 case TARGET_NR_unlink
:
5424 if (!(p
= lock_user_string(arg1
)))
5426 ret
= get_errno(unlink(p
));
5427 unlock_user(p
, arg1
, 0);
5429 #if defined(TARGET_NR_unlinkat)
5430 case TARGET_NR_unlinkat
:
5431 if (!(p
= lock_user_string(arg2
)))
5433 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
5434 unlock_user(p
, arg2
, 0);
5437 case TARGET_NR_execve
:
5439 char **argp
, **envp
;
5442 abi_ulong guest_argp
;
5443 abi_ulong guest_envp
;
5450 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
5451 if (get_user_ual(addr
, gp
))
5459 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
5460 if (get_user_ual(addr
, gp
))
5467 argp
= alloca((argc
+ 1) * sizeof(void *));
5468 envp
= alloca((envc
+ 1) * sizeof(void *));
5470 for (gp
= guest_argp
, q
= argp
; gp
;
5471 gp
+= sizeof(abi_ulong
), q
++) {
5472 if (get_user_ual(addr
, gp
))
5476 if (!(*q
= lock_user_string(addr
)))
5478 total_size
+= strlen(*q
) + 1;
5482 for (gp
= guest_envp
, q
= envp
; gp
;
5483 gp
+= sizeof(abi_ulong
), q
++) {
5484 if (get_user_ual(addr
, gp
))
5488 if (!(*q
= lock_user_string(addr
)))
5490 total_size
+= strlen(*q
) + 1;
5494 /* This case will not be caught by the host's execve() if its
5495 page size is bigger than the target's. */
5496 if (total_size
> MAX_ARG_PAGES
* TARGET_PAGE_SIZE
) {
5497 ret
= -TARGET_E2BIG
;
5500 if (!(p
= lock_user_string(arg1
)))
5502 ret
= get_errno(execve(p
, argp
, envp
));
5503 unlock_user(p
, arg1
, 0);
5508 ret
= -TARGET_EFAULT
;
5511 for (gp
= guest_argp
, q
= argp
; *q
;
5512 gp
+= sizeof(abi_ulong
), q
++) {
5513 if (get_user_ual(addr
, gp
)
5516 unlock_user(*q
, addr
, 0);
5518 for (gp
= guest_envp
, q
= envp
; *q
;
5519 gp
+= sizeof(abi_ulong
), q
++) {
5520 if (get_user_ual(addr
, gp
)
5523 unlock_user(*q
, addr
, 0);
5527 case TARGET_NR_chdir
:
5528 if (!(p
= lock_user_string(arg1
)))
5530 ret
= get_errno(chdir(p
));
5531 unlock_user(p
, arg1
, 0);
5533 #ifdef TARGET_NR_time
5534 case TARGET_NR_time
:
5537 ret
= get_errno(time(&host_time
));
5540 && put_user_sal(host_time
, arg1
))
5545 case TARGET_NR_mknod
:
5546 if (!(p
= lock_user_string(arg1
)))
5548 ret
= get_errno(mknod(p
, arg2
, arg3
));
5549 unlock_user(p
, arg1
, 0);
5551 #if defined(TARGET_NR_mknodat)
5552 case TARGET_NR_mknodat
:
5553 if (!(p
= lock_user_string(arg2
)))
5555 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
5556 unlock_user(p
, arg2
, 0);
5559 case TARGET_NR_chmod
:
5560 if (!(p
= lock_user_string(arg1
)))
5562 ret
= get_errno(chmod(p
, arg2
));
5563 unlock_user(p
, arg1
, 0);
5565 #ifdef TARGET_NR_break
5566 case TARGET_NR_break
:
5569 #ifdef TARGET_NR_oldstat
5570 case TARGET_NR_oldstat
:
5573 case TARGET_NR_lseek
:
5574 ret
= get_errno(lseek(arg1
, arg2
, arg3
));
5576 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5577 /* Alpha specific */
5578 case TARGET_NR_getxpid
:
5579 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = getppid();
5580 ret
= get_errno(getpid());
5583 #ifdef TARGET_NR_getpid
5584 case TARGET_NR_getpid
:
5585 ret
= get_errno(getpid());
5588 case TARGET_NR_mount
:
5590 /* need to look at the data field */
5592 p
= lock_user_string(arg1
);
5593 p2
= lock_user_string(arg2
);
5594 p3
= lock_user_string(arg3
);
5595 if (!p
|| !p2
|| !p3
)
5596 ret
= -TARGET_EFAULT
;
5598 /* FIXME - arg5 should be locked, but it isn't clear how to
5599 * do that since it's not guaranteed to be a NULL-terminated
5603 ret
= get_errno(mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
));
5605 ret
= get_errno(mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
)));
5607 unlock_user(p
, arg1
, 0);
5608 unlock_user(p2
, arg2
, 0);
5609 unlock_user(p3
, arg3
, 0);
5612 #ifdef TARGET_NR_umount
5613 case TARGET_NR_umount
:
5614 if (!(p
= lock_user_string(arg1
)))
5616 ret
= get_errno(umount(p
));
5617 unlock_user(p
, arg1
, 0);
5620 #ifdef TARGET_NR_stime /* not on alpha */
5621 case TARGET_NR_stime
:
5624 if (get_user_sal(host_time
, arg1
))
5626 ret
= get_errno(stime(&host_time
));
5630 case TARGET_NR_ptrace
:
5632 #ifdef TARGET_NR_alarm /* not on alpha */
5633 case TARGET_NR_alarm
:
5637 #ifdef TARGET_NR_oldfstat
5638 case TARGET_NR_oldfstat
:
5641 #ifdef TARGET_NR_pause /* not on alpha */
5642 case TARGET_NR_pause
:
5643 ret
= get_errno(pause());
5646 #ifdef TARGET_NR_utime
5647 case TARGET_NR_utime
:
5649 struct utimbuf tbuf
, *host_tbuf
;
5650 struct target_utimbuf
*target_tbuf
;
5652 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
5654 tbuf
.actime
= tswapal(target_tbuf
->actime
);
5655 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
5656 unlock_user_struct(target_tbuf
, arg2
, 0);
5661 if (!(p
= lock_user_string(arg1
)))
5663 ret
= get_errno(utime(p
, host_tbuf
));
5664 unlock_user(p
, arg1
, 0);
5668 case TARGET_NR_utimes
:
5670 struct timeval
*tvp
, tv
[2];
5672 if (copy_from_user_timeval(&tv
[0], arg2
)
5673 || copy_from_user_timeval(&tv
[1],
5674 arg2
+ sizeof(struct target_timeval
)))
5680 if (!(p
= lock_user_string(arg1
)))
5682 ret
= get_errno(utimes(p
, tvp
));
5683 unlock_user(p
, arg1
, 0);
5686 #if defined(TARGET_NR_futimesat)
5687 case TARGET_NR_futimesat
:
5689 struct timeval
*tvp
, tv
[2];
5691 if (copy_from_user_timeval(&tv
[0], arg3
)
5692 || copy_from_user_timeval(&tv
[1],
5693 arg3
+ sizeof(struct target_timeval
)))
5699 if (!(p
= lock_user_string(arg2
)))
5701 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
5702 unlock_user(p
, arg2
, 0);
5706 #ifdef TARGET_NR_stty
5707 case TARGET_NR_stty
:
5710 #ifdef TARGET_NR_gtty
5711 case TARGET_NR_gtty
:
5714 case TARGET_NR_access
:
5715 if (!(p
= lock_user_string(arg1
)))
5717 ret
= get_errno(access(path(p
), arg2
));
5718 unlock_user(p
, arg1
, 0);
5720 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5721 case TARGET_NR_faccessat
:
5722 if (!(p
= lock_user_string(arg2
)))
5724 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
5725 unlock_user(p
, arg2
, 0);
5728 #ifdef TARGET_NR_nice /* not on alpha */
5729 case TARGET_NR_nice
:
5730 ret
= get_errno(nice(arg1
));
5733 #ifdef TARGET_NR_ftime
5734 case TARGET_NR_ftime
:
5737 case TARGET_NR_sync
:
5741 case TARGET_NR_kill
:
5742 ret
= get_errno(kill(arg1
, target_to_host_signal(arg2
)));
5744 case TARGET_NR_rename
:
5747 p
= lock_user_string(arg1
);
5748 p2
= lock_user_string(arg2
);
5750 ret
= -TARGET_EFAULT
;
5752 ret
= get_errno(rename(p
, p2
));
5753 unlock_user(p2
, arg2
, 0);
5754 unlock_user(p
, arg1
, 0);
5757 #if defined(TARGET_NR_renameat)
5758 case TARGET_NR_renameat
:
5761 p
= lock_user_string(arg2
);
5762 p2
= lock_user_string(arg4
);
5764 ret
= -TARGET_EFAULT
;
5766 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
5767 unlock_user(p2
, arg4
, 0);
5768 unlock_user(p
, arg2
, 0);
5772 case TARGET_NR_mkdir
:
5773 if (!(p
= lock_user_string(arg1
)))
5775 ret
= get_errno(mkdir(p
, arg2
));
5776 unlock_user(p
, arg1
, 0);
5778 #if defined(TARGET_NR_mkdirat)
5779 case TARGET_NR_mkdirat
:
5780 if (!(p
= lock_user_string(arg2
)))
5782 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
5783 unlock_user(p
, arg2
, 0);
5786 case TARGET_NR_rmdir
:
5787 if (!(p
= lock_user_string(arg1
)))
5789 ret
= get_errno(rmdir(p
));
5790 unlock_user(p
, arg1
, 0);
5793 ret
= get_errno(dup(arg1
));
5795 case TARGET_NR_pipe
:
5796 ret
= do_pipe(cpu_env
, arg1
, 0, 0);
5798 #ifdef TARGET_NR_pipe2
5799 case TARGET_NR_pipe2
:
5800 ret
= do_pipe(cpu_env
, arg1
,
5801 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
5804 case TARGET_NR_times
:
5806 struct target_tms
*tmsp
;
5808 ret
= get_errno(times(&tms
));
5810 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
5813 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
5814 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
5815 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
5816 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
5819 ret
= host_to_target_clock_t(ret
);
5822 #ifdef TARGET_NR_prof
5823 case TARGET_NR_prof
:
5826 #ifdef TARGET_NR_signal
5827 case TARGET_NR_signal
:
5830 case TARGET_NR_acct
:
5832 ret
= get_errno(acct(NULL
));
5834 if (!(p
= lock_user_string(arg1
)))
5836 ret
= get_errno(acct(path(p
)));
5837 unlock_user(p
, arg1
, 0);
5840 #ifdef TARGET_NR_umount2
5841 case TARGET_NR_umount2
:
5842 if (!(p
= lock_user_string(arg1
)))
5844 ret
= get_errno(umount2(p
, arg2
));
5845 unlock_user(p
, arg1
, 0);
5848 #ifdef TARGET_NR_lock
5849 case TARGET_NR_lock
:
5852 case TARGET_NR_ioctl
:
5853 ret
= do_ioctl(arg1
, arg2
, arg3
);
5855 case TARGET_NR_fcntl
:
5856 ret
= do_fcntl(arg1
, arg2
, arg3
);
5858 #ifdef TARGET_NR_mpx
5862 case TARGET_NR_setpgid
:
5863 ret
= get_errno(setpgid(arg1
, arg2
));
5865 #ifdef TARGET_NR_ulimit
5866 case TARGET_NR_ulimit
:
5869 #ifdef TARGET_NR_oldolduname
5870 case TARGET_NR_oldolduname
:
5873 case TARGET_NR_umask
:
5874 ret
= get_errno(umask(arg1
));
5876 case TARGET_NR_chroot
:
5877 if (!(p
= lock_user_string(arg1
)))
5879 ret
= get_errno(chroot(p
));
5880 unlock_user(p
, arg1
, 0);
5882 case TARGET_NR_ustat
:
5884 case TARGET_NR_dup2
:
5885 ret
= get_errno(dup2(arg1
, arg2
));
5887 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5888 case TARGET_NR_dup3
:
5889 ret
= get_errno(dup3(arg1
, arg2
, arg3
));
5892 #ifdef TARGET_NR_getppid /* not on alpha */
5893 case TARGET_NR_getppid
:
5894 ret
= get_errno(getppid());
5897 case TARGET_NR_getpgrp
:
5898 ret
= get_errno(getpgrp());
5900 case TARGET_NR_setsid
:
5901 ret
= get_errno(setsid());
5903 #ifdef TARGET_NR_sigaction
5904 case TARGET_NR_sigaction
:
5906 #if defined(TARGET_ALPHA)
5907 struct target_sigaction act
, oact
, *pact
= 0;
5908 struct target_old_sigaction
*old_act
;
5910 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
5912 act
._sa_handler
= old_act
->_sa_handler
;
5913 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
5914 act
.sa_flags
= old_act
->sa_flags
;
5915 act
.sa_restorer
= 0;
5916 unlock_user_struct(old_act
, arg2
, 0);
5919 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
5920 if (!is_error(ret
) && arg3
) {
5921 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
5923 old_act
->_sa_handler
= oact
._sa_handler
;
5924 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
5925 old_act
->sa_flags
= oact
.sa_flags
;
5926 unlock_user_struct(old_act
, arg3
, 1);
5928 #elif defined(TARGET_MIPS)
5929 struct target_sigaction act
, oact
, *pact
, *old_act
;
5932 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
5934 act
._sa_handler
= old_act
->_sa_handler
;
5935 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
5936 act
.sa_flags
= old_act
->sa_flags
;
5937 unlock_user_struct(old_act
, arg2
, 0);
5943 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
5945 if (!is_error(ret
) && arg3
) {
5946 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
5948 old_act
->_sa_handler
= oact
._sa_handler
;
5949 old_act
->sa_flags
= oact
.sa_flags
;
5950 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
5951 old_act
->sa_mask
.sig
[1] = 0;
5952 old_act
->sa_mask
.sig
[2] = 0;
5953 old_act
->sa_mask
.sig
[3] = 0;
5954 unlock_user_struct(old_act
, arg3
, 1);
5957 struct target_old_sigaction
*old_act
;
5958 struct target_sigaction act
, oact
, *pact
;
5960 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
5962 act
._sa_handler
= old_act
->_sa_handler
;
5963 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
5964 act
.sa_flags
= old_act
->sa_flags
;
5965 act
.sa_restorer
= old_act
->sa_restorer
;
5966 unlock_user_struct(old_act
, arg2
, 0);
5971 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
5972 if (!is_error(ret
) && arg3
) {
5973 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
5975 old_act
->_sa_handler
= oact
._sa_handler
;
5976 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
5977 old_act
->sa_flags
= oact
.sa_flags
;
5978 old_act
->sa_restorer
= oact
.sa_restorer
;
5979 unlock_user_struct(old_act
, arg3
, 1);
5985 case TARGET_NR_rt_sigaction
:
5987 #if defined(TARGET_ALPHA)
5988 struct target_sigaction act
, oact
, *pact
= 0;
5989 struct target_rt_sigaction
*rt_act
;
5990 /* ??? arg4 == sizeof(sigset_t). */
5992 if (!lock_user_struct(VERIFY_READ
, rt_act
, arg2
, 1))
5994 act
._sa_handler
= rt_act
->_sa_handler
;
5995 act
.sa_mask
= rt_act
->sa_mask
;
5996 act
.sa_flags
= rt_act
->sa_flags
;
5997 act
.sa_restorer
= arg5
;
5998 unlock_user_struct(rt_act
, arg2
, 0);
6001 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6002 if (!is_error(ret
) && arg3
) {
6003 if (!lock_user_struct(VERIFY_WRITE
, rt_act
, arg3
, 0))
6005 rt_act
->_sa_handler
= oact
._sa_handler
;
6006 rt_act
->sa_mask
= oact
.sa_mask
;
6007 rt_act
->sa_flags
= oact
.sa_flags
;
6008 unlock_user_struct(rt_act
, arg3
, 1);
6011 struct target_sigaction
*act
;
6012 struct target_sigaction
*oact
;
6015 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1))
6020 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
6021 ret
= -TARGET_EFAULT
;
6022 goto rt_sigaction_fail
;
6026 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
6029 unlock_user_struct(act
, arg2
, 0);
6031 unlock_user_struct(oact
, arg3
, 1);
6035 #ifdef TARGET_NR_sgetmask /* not on alpha */
6036 case TARGET_NR_sgetmask
:
6039 abi_ulong target_set
;
6040 do_sigprocmask(0, NULL
, &cur_set
);
6041 host_to_target_old_sigset(&target_set
, &cur_set
);
6046 #ifdef TARGET_NR_ssetmask /* not on alpha */
6047 case TARGET_NR_ssetmask
:
6049 sigset_t set
, oset
, cur_set
;
6050 abi_ulong target_set
= arg1
;
6051 do_sigprocmask(0, NULL
, &cur_set
);
6052 target_to_host_old_sigset(&set
, &target_set
);
6053 sigorset(&set
, &set
, &cur_set
);
6054 do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
6055 host_to_target_old_sigset(&target_set
, &oset
);
6060 #ifdef TARGET_NR_sigprocmask
6061 case TARGET_NR_sigprocmask
:
6063 #if defined(TARGET_ALPHA)
6064 sigset_t set
, oldset
;
6069 case TARGET_SIG_BLOCK
:
6072 case TARGET_SIG_UNBLOCK
:
6075 case TARGET_SIG_SETMASK
:
6079 ret
= -TARGET_EINVAL
;
6083 target_to_host_old_sigset(&set
, &mask
);
6085 ret
= get_errno(do_sigprocmask(how
, &set
, &oldset
));
6086 if (!is_error(ret
)) {
6087 host_to_target_old_sigset(&mask
, &oldset
);
6089 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0; /* force no error */
6092 sigset_t set
, oldset
, *set_ptr
;
6097 case TARGET_SIG_BLOCK
:
6100 case TARGET_SIG_UNBLOCK
:
6103 case TARGET_SIG_SETMASK
:
6107 ret
= -TARGET_EINVAL
;
6110 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
6112 target_to_host_old_sigset(&set
, p
);
6113 unlock_user(p
, arg2
, 0);
6119 ret
= get_errno(do_sigprocmask(how
, set_ptr
, &oldset
));
6120 if (!is_error(ret
) && arg3
) {
6121 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
6123 host_to_target_old_sigset(p
, &oldset
);
6124 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
6130 case TARGET_NR_rt_sigprocmask
:
6133 sigset_t set
, oldset
, *set_ptr
;
6137 case TARGET_SIG_BLOCK
:
6140 case TARGET_SIG_UNBLOCK
:
6143 case TARGET_SIG_SETMASK
:
6147 ret
= -TARGET_EINVAL
;
6150 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
6152 target_to_host_sigset(&set
, p
);
6153 unlock_user(p
, arg2
, 0);
6159 ret
= get_errno(do_sigprocmask(how
, set_ptr
, &oldset
));
6160 if (!is_error(ret
) && arg3
) {
6161 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
6163 host_to_target_sigset(p
, &oldset
);
6164 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
6168 #ifdef TARGET_NR_sigpending
6169 case TARGET_NR_sigpending
:
6172 ret
= get_errno(sigpending(&set
));
6173 if (!is_error(ret
)) {
6174 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
6176 host_to_target_old_sigset(p
, &set
);
6177 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
6182 case TARGET_NR_rt_sigpending
:
6185 ret
= get_errno(sigpending(&set
));
6186 if (!is_error(ret
)) {
6187 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
6189 host_to_target_sigset(p
, &set
);
6190 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
6194 #ifdef TARGET_NR_sigsuspend
6195 case TARGET_NR_sigsuspend
:
6198 #if defined(TARGET_ALPHA)
6199 abi_ulong mask
= arg1
;
6200 target_to_host_old_sigset(&set
, &mask
);
6202 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6204 target_to_host_old_sigset(&set
, p
);
6205 unlock_user(p
, arg1
, 0);
6207 ret
= get_errno(sigsuspend(&set
));
6211 case TARGET_NR_rt_sigsuspend
:
6214 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6216 target_to_host_sigset(&set
, p
);
6217 unlock_user(p
, arg1
, 0);
6218 ret
= get_errno(sigsuspend(&set
));
6221 case TARGET_NR_rt_sigtimedwait
:
6224 struct timespec uts
, *puts
;
6227 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6229 target_to_host_sigset(&set
, p
);
6230 unlock_user(p
, arg1
, 0);
6233 target_to_host_timespec(puts
, arg3
);
6237 ret
= get_errno(sigtimedwait(&set
, &uinfo
, puts
));
6238 if (!is_error(ret
)) {
6240 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
6245 host_to_target_siginfo(p
, &uinfo
);
6246 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
6248 ret
= host_to_target_signal(ret
);
6252 case TARGET_NR_rt_sigqueueinfo
:
6255 if (!(p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_sigset_t
), 1)))
6257 target_to_host_siginfo(&uinfo
, p
);
6258 unlock_user(p
, arg1
, 0);
6259 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
6262 #ifdef TARGET_NR_sigreturn
6263 case TARGET_NR_sigreturn
:
6264 /* NOTE: ret is eax, so not transcoding must be done */
6265 ret
= do_sigreturn(cpu_env
);
6268 case TARGET_NR_rt_sigreturn
:
6269 /* NOTE: ret is eax, so not transcoding must be done */
6270 ret
= do_rt_sigreturn(cpu_env
);
6272 case TARGET_NR_sethostname
:
6273 if (!(p
= lock_user_string(arg1
)))
6275 ret
= get_errno(sethostname(p
, arg2
));
6276 unlock_user(p
, arg1
, 0);
6278 case TARGET_NR_setrlimit
:
6280 int resource
= target_to_host_resource(arg1
);
6281 struct target_rlimit
*target_rlim
;
6283 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
6285 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
6286 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
6287 unlock_user_struct(target_rlim
, arg2
, 0);
6288 ret
= get_errno(setrlimit(resource
, &rlim
));
6291 case TARGET_NR_getrlimit
:
6293 int resource
= target_to_host_resource(arg1
);
6294 struct target_rlimit
*target_rlim
;
6297 ret
= get_errno(getrlimit(resource
, &rlim
));
6298 if (!is_error(ret
)) {
6299 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
6301 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
6302 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
6303 unlock_user_struct(target_rlim
, arg2
, 1);
6307 case TARGET_NR_getrusage
:
6309 struct rusage rusage
;
6310 ret
= get_errno(getrusage(arg1
, &rusage
));
6311 if (!is_error(ret
)) {
6312 host_to_target_rusage(arg2
, &rusage
);
6316 case TARGET_NR_gettimeofday
:
6319 ret
= get_errno(gettimeofday(&tv
, NULL
));
6320 if (!is_error(ret
)) {
6321 if (copy_to_user_timeval(arg1
, &tv
))
6326 case TARGET_NR_settimeofday
:
6329 if (copy_from_user_timeval(&tv
, arg1
))
6331 ret
= get_errno(settimeofday(&tv
, NULL
));
6334 #if defined(TARGET_NR_select)
6335 case TARGET_NR_select
:
6336 #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6337 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
6340 struct target_sel_arg_struct
*sel
;
6341 abi_ulong inp
, outp
, exp
, tvp
;
6344 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1))
6346 nsel
= tswapal(sel
->n
);
6347 inp
= tswapal(sel
->inp
);
6348 outp
= tswapal(sel
->outp
);
6349 exp
= tswapal(sel
->exp
);
6350 tvp
= tswapal(sel
->tvp
);
6351 unlock_user_struct(sel
, arg1
, 0);
6352 ret
= do_select(nsel
, inp
, outp
, exp
, tvp
);
6357 #ifdef TARGET_NR_pselect6
6358 case TARGET_NR_pselect6
:
6360 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
6361 fd_set rfds
, wfds
, efds
;
6362 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
6363 struct timespec ts
, *ts_ptr
;
6366 * The 6th arg is actually two args smashed together,
6367 * so we cannot use the C library.
6375 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
6376 target_sigset_t
*target_sigset
;
6384 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
6388 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
6392 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
6398 * This takes a timespec, and not a timeval, so we cannot
6399 * use the do_select() helper ...
6402 if (target_to_host_timespec(&ts
, ts_addr
)) {
6410 /* Extract the two packed args for the sigset */
6413 sig
.size
= _NSIG
/ 8;
6415 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
6419 arg_sigset
= tswapal(arg7
[0]);
6420 arg_sigsize
= tswapal(arg7
[1]);
6421 unlock_user(arg7
, arg6
, 0);
6425 if (arg_sigsize
!= sizeof(*target_sigset
)) {
6426 /* Like the kernel, we enforce correct size sigsets */
6427 ret
= -TARGET_EINVAL
;
6430 target_sigset
= lock_user(VERIFY_READ
, arg_sigset
,
6431 sizeof(*target_sigset
), 1);
6432 if (!target_sigset
) {
6435 target_to_host_sigset(&set
, target_sigset
);
6436 unlock_user(target_sigset
, arg_sigset
, 0);
6444 ret
= get_errno(sys_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
6447 if (!is_error(ret
)) {
6448 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
6450 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
6452 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
6455 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
))
6461 case TARGET_NR_symlink
:
6464 p
= lock_user_string(arg1
);
6465 p2
= lock_user_string(arg2
);
6467 ret
= -TARGET_EFAULT
;
6469 ret
= get_errno(symlink(p
, p2
));
6470 unlock_user(p2
, arg2
, 0);
6471 unlock_user(p
, arg1
, 0);
6474 #if defined(TARGET_NR_symlinkat)
6475 case TARGET_NR_symlinkat
:
6478 p
= lock_user_string(arg1
);
6479 p2
= lock_user_string(arg3
);
6481 ret
= -TARGET_EFAULT
;
6483 ret
= get_errno(symlinkat(p
, arg2
, p2
));
6484 unlock_user(p2
, arg3
, 0);
6485 unlock_user(p
, arg1
, 0);
6489 #ifdef TARGET_NR_oldlstat
6490 case TARGET_NR_oldlstat
:
6493 case TARGET_NR_readlink
:
6496 p
= lock_user_string(arg1
);
6497 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
6499 ret
= -TARGET_EFAULT
;
6500 } else if (is_proc_myself((const char *)p
, "exe")) {
6501 char real
[PATH_MAX
], *temp
;
6502 temp
= realpath(exec_path
, real
);
6503 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
6504 snprintf((char *)p2
, arg3
, "%s", real
);
6506 ret
= get_errno(readlink(path(p
), p2
, arg3
));
6508 unlock_user(p2
, arg2
, ret
);
6509 unlock_user(p
, arg1
, 0);
6512 #if defined(TARGET_NR_readlinkat)
6513 case TARGET_NR_readlinkat
:
6516 p
= lock_user_string(arg2
);
6517 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
6519 ret
= -TARGET_EFAULT
;
6520 } else if (is_proc_myself((const char *)p
, "exe")) {
6521 char real
[PATH_MAX
], *temp
;
6522 temp
= realpath(exec_path
, real
);
6523 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
6524 snprintf((char *)p2
, arg4
, "%s", real
);
6526 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
6528 unlock_user(p2
, arg3
, ret
);
6529 unlock_user(p
, arg2
, 0);
6533 #ifdef TARGET_NR_uselib
6534 case TARGET_NR_uselib
:
6537 #ifdef TARGET_NR_swapon
6538 case TARGET_NR_swapon
:
6539 if (!(p
= lock_user_string(arg1
)))
6541 ret
= get_errno(swapon(p
, arg2
));
6542 unlock_user(p
, arg1
, 0);
6545 case TARGET_NR_reboot
:
6546 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
6547 /* arg4 must be ignored in all other cases */
6548 p
= lock_user_string(arg4
);
6552 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
6553 unlock_user(p
, arg4
, 0);
6555 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
6558 #ifdef TARGET_NR_readdir
6559 case TARGET_NR_readdir
:
6562 #ifdef TARGET_NR_mmap
6563 case TARGET_NR_mmap
:
6564 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6565 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
6566 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6567 || defined(TARGET_S390X)
6570 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
6571 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
6579 unlock_user(v
, arg1
, 0);
6580 ret
= get_errno(target_mmap(v1
, v2
, v3
,
6581 target_to_host_bitmask(v4
, mmap_flags_tbl
),
6585 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
6586 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
6592 #ifdef TARGET_NR_mmap2
6593 case TARGET_NR_mmap2
:
6595 #define MMAP_SHIFT 12
6597 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
6598 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
6600 arg6
<< MMAP_SHIFT
));
6603 case TARGET_NR_munmap
:
6604 ret
= get_errno(target_munmap(arg1
, arg2
));
6606 case TARGET_NR_mprotect
:
6608 TaskState
*ts
= cpu
->opaque
;
6609 /* Special hack to detect libc making the stack executable. */
6610 if ((arg3
& PROT_GROWSDOWN
)
6611 && arg1
>= ts
->info
->stack_limit
6612 && arg1
<= ts
->info
->start_stack
) {
6613 arg3
&= ~PROT_GROWSDOWN
;
6614 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
6615 arg1
= ts
->info
->stack_limit
;
6618 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
6620 #ifdef TARGET_NR_mremap
6621 case TARGET_NR_mremap
:
6622 ret
= get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
6625 /* ??? msync/mlock/munlock are broken for softmmu. */
6626 #ifdef TARGET_NR_msync
6627 case TARGET_NR_msync
:
6628 ret
= get_errno(msync(g2h(arg1
), arg2
, arg3
));
6631 #ifdef TARGET_NR_mlock
6632 case TARGET_NR_mlock
:
6633 ret
= get_errno(mlock(g2h(arg1
), arg2
));
6636 #ifdef TARGET_NR_munlock
6637 case TARGET_NR_munlock
:
6638 ret
= get_errno(munlock(g2h(arg1
), arg2
));
6641 #ifdef TARGET_NR_mlockall
6642 case TARGET_NR_mlockall
:
6643 ret
= get_errno(mlockall(arg1
));
6646 #ifdef TARGET_NR_munlockall
6647 case TARGET_NR_munlockall
:
6648 ret
= get_errno(munlockall());
6651 case TARGET_NR_truncate
:
6652 if (!(p
= lock_user_string(arg1
)))
6654 ret
= get_errno(truncate(p
, arg2
));
6655 unlock_user(p
, arg1
, 0);
6657 case TARGET_NR_ftruncate
:
6658 ret
= get_errno(ftruncate(arg1
, arg2
));
6660 case TARGET_NR_fchmod
:
6661 ret
= get_errno(fchmod(arg1
, arg2
));
6663 #if defined(TARGET_NR_fchmodat)
6664 case TARGET_NR_fchmodat
:
6665 if (!(p
= lock_user_string(arg2
)))
6667 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
6668 unlock_user(p
, arg2
, 0);
6671 case TARGET_NR_getpriority
:
6672 /* Note that negative values are valid for getpriority, so we must
6673 differentiate based on errno settings. */
6675 ret
= getpriority(arg1
, arg2
);
6676 if (ret
== -1 && errno
!= 0) {
6677 ret
= -host_to_target_errno(errno
);
6681 /* Return value is the unbiased priority. Signal no error. */
6682 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0;
6684 /* Return value is a biased priority to avoid negative numbers. */
6688 case TARGET_NR_setpriority
:
6689 ret
= get_errno(setpriority(arg1
, arg2
, arg3
));
6691 #ifdef TARGET_NR_profil
6692 case TARGET_NR_profil
:
6695 case TARGET_NR_statfs
:
6696 if (!(p
= lock_user_string(arg1
)))
6698 ret
= get_errno(statfs(path(p
), &stfs
));
6699 unlock_user(p
, arg1
, 0);
6701 if (!is_error(ret
)) {
6702 struct target_statfs
*target_stfs
;
6704 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
6706 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
6707 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
6708 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
6709 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
6710 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
6711 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
6712 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
6713 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
6714 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
6715 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
6716 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
6717 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
6718 unlock_user_struct(target_stfs
, arg2
, 1);
6721 case TARGET_NR_fstatfs
:
6722 ret
= get_errno(fstatfs(arg1
, &stfs
));
6723 goto convert_statfs
;
6724 #ifdef TARGET_NR_statfs64
6725 case TARGET_NR_statfs64
:
6726 if (!(p
= lock_user_string(arg1
)))
6728 ret
= get_errno(statfs(path(p
), &stfs
));
6729 unlock_user(p
, arg1
, 0);
6731 if (!is_error(ret
)) {
6732 struct target_statfs64
*target_stfs
;
6734 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
6736 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
6737 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
6738 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
6739 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
6740 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
6741 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
6742 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
6743 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
6744 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
6745 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
6746 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
6747 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
6748 unlock_user_struct(target_stfs
, arg3
, 1);
6751 case TARGET_NR_fstatfs64
:
6752 ret
= get_errno(fstatfs(arg1
, &stfs
));
6753 goto convert_statfs64
;
6755 #ifdef TARGET_NR_ioperm
6756 case TARGET_NR_ioperm
:
6759 #ifdef TARGET_NR_socketcall
6760 case TARGET_NR_socketcall
:
6761 ret
= do_socketcall(arg1
, arg2
);
6764 #ifdef TARGET_NR_accept
6765 case TARGET_NR_accept
:
6766 ret
= do_accept4(arg1
, arg2
, arg3
, 0);
6769 #ifdef TARGET_NR_accept4
6770 case TARGET_NR_accept4
:
6771 #ifdef CONFIG_ACCEPT4
6772 ret
= do_accept4(arg1
, arg2
, arg3
, arg4
);
6778 #ifdef TARGET_NR_bind
6779 case TARGET_NR_bind
:
6780 ret
= do_bind(arg1
, arg2
, arg3
);
6783 #ifdef TARGET_NR_connect
6784 case TARGET_NR_connect
:
6785 ret
= do_connect(arg1
, arg2
, arg3
);
6788 #ifdef TARGET_NR_getpeername
6789 case TARGET_NR_getpeername
:
6790 ret
= do_getpeername(arg1
, arg2
, arg3
);
6793 #ifdef TARGET_NR_getsockname
6794 case TARGET_NR_getsockname
:
6795 ret
= do_getsockname(arg1
, arg2
, arg3
);
6798 #ifdef TARGET_NR_getsockopt
6799 case TARGET_NR_getsockopt
:
6800 ret
= do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
6803 #ifdef TARGET_NR_listen
6804 case TARGET_NR_listen
:
6805 ret
= get_errno(listen(arg1
, arg2
));
6808 #ifdef TARGET_NR_recv
6809 case TARGET_NR_recv
:
6810 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
6813 #ifdef TARGET_NR_recvfrom
6814 case TARGET_NR_recvfrom
:
6815 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
6818 #ifdef TARGET_NR_recvmsg
6819 case TARGET_NR_recvmsg
:
6820 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
6823 #ifdef TARGET_NR_send
6824 case TARGET_NR_send
:
6825 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
6828 #ifdef TARGET_NR_sendmsg
6829 case TARGET_NR_sendmsg
:
6830 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
6833 #ifdef TARGET_NR_sendmmsg
6834 case TARGET_NR_sendmmsg
:
6835 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
6837 case TARGET_NR_recvmmsg
:
6838 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
6841 #ifdef TARGET_NR_sendto
6842 case TARGET_NR_sendto
:
6843 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
6846 #ifdef TARGET_NR_shutdown
6847 case TARGET_NR_shutdown
:
6848 ret
= get_errno(shutdown(arg1
, arg2
));
6851 #ifdef TARGET_NR_socket
6852 case TARGET_NR_socket
:
6853 ret
= do_socket(arg1
, arg2
, arg3
);
6856 #ifdef TARGET_NR_socketpair
6857 case TARGET_NR_socketpair
:
6858 ret
= do_socketpair(arg1
, arg2
, arg3
, arg4
);
6861 #ifdef TARGET_NR_setsockopt
6862 case TARGET_NR_setsockopt
:
6863 ret
= do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
6867 case TARGET_NR_syslog
:
6868 if (!(p
= lock_user_string(arg2
)))
6870 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
6871 unlock_user(p
, arg2
, 0);
6874 case TARGET_NR_setitimer
:
6876 struct itimerval value
, ovalue
, *pvalue
;
6880 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
6881 || copy_from_user_timeval(&pvalue
->it_value
,
6882 arg2
+ sizeof(struct target_timeval
)))
6887 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
6888 if (!is_error(ret
) && arg3
) {
6889 if (copy_to_user_timeval(arg3
,
6890 &ovalue
.it_interval
)
6891 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
6897 case TARGET_NR_getitimer
:
6899 struct itimerval value
;
6901 ret
= get_errno(getitimer(arg1
, &value
));
6902 if (!is_error(ret
) && arg2
) {
6903 if (copy_to_user_timeval(arg2
,
6905 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
6911 case TARGET_NR_stat
:
6912 if (!(p
= lock_user_string(arg1
)))
6914 ret
= get_errno(stat(path(p
), &st
));
6915 unlock_user(p
, arg1
, 0);
6917 case TARGET_NR_lstat
:
6918 if (!(p
= lock_user_string(arg1
)))
6920 ret
= get_errno(lstat(path(p
), &st
));
6921 unlock_user(p
, arg1
, 0);
6923 case TARGET_NR_fstat
:
6925 ret
= get_errno(fstat(arg1
, &st
));
6927 if (!is_error(ret
)) {
6928 struct target_stat
*target_st
;
6930 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
6932 memset(target_st
, 0, sizeof(*target_st
));
6933 __put_user(st
.st_dev
, &target_st
->st_dev
);
6934 __put_user(st
.st_ino
, &target_st
->st_ino
);
6935 __put_user(st
.st_mode
, &target_st
->st_mode
);
6936 __put_user(st
.st_uid
, &target_st
->st_uid
);
6937 __put_user(st
.st_gid
, &target_st
->st_gid
);
6938 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
6939 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
6940 __put_user(st
.st_size
, &target_st
->st_size
);
6941 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
6942 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
6943 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
6944 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
6945 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
6946 unlock_user_struct(target_st
, arg2
, 1);
6950 #ifdef TARGET_NR_olduname
6951 case TARGET_NR_olduname
:
6954 #ifdef TARGET_NR_iopl
6955 case TARGET_NR_iopl
:
6958 case TARGET_NR_vhangup
:
6959 ret
= get_errno(vhangup());
6961 #ifdef TARGET_NR_idle
6962 case TARGET_NR_idle
:
6965 #ifdef TARGET_NR_syscall
6966 case TARGET_NR_syscall
:
6967 ret
= do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
6968 arg6
, arg7
, arg8
, 0);
6971 case TARGET_NR_wait4
:
6974 abi_long status_ptr
= arg2
;
6975 struct rusage rusage
, *rusage_ptr
;
6976 abi_ulong target_rusage
= arg4
;
6978 rusage_ptr
= &rusage
;
6981 ret
= get_errno(wait4(arg1
, &status
, arg3
, rusage_ptr
));
6982 if (!is_error(ret
)) {
6983 if (status_ptr
&& ret
) {
6984 status
= host_to_target_waitstatus(status
);
6985 if (put_user_s32(status
, status_ptr
))
6989 host_to_target_rusage(target_rusage
, &rusage
);
6993 #ifdef TARGET_NR_swapoff
6994 case TARGET_NR_swapoff
:
6995 if (!(p
= lock_user_string(arg1
)))
6997 ret
= get_errno(swapoff(p
));
6998 unlock_user(p
, arg1
, 0);
7001 case TARGET_NR_sysinfo
:
7003 struct target_sysinfo
*target_value
;
7004 struct sysinfo value
;
7005 ret
= get_errno(sysinfo(&value
));
7006 if (!is_error(ret
) && arg1
)
7008 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
7010 __put_user(value
.uptime
, &target_value
->uptime
);
7011 __put_user(value
.loads
[0], &target_value
->loads
[0]);
7012 __put_user(value
.loads
[1], &target_value
->loads
[1]);
7013 __put_user(value
.loads
[2], &target_value
->loads
[2]);
7014 __put_user(value
.totalram
, &target_value
->totalram
);
7015 __put_user(value
.freeram
, &target_value
->freeram
);
7016 __put_user(value
.sharedram
, &target_value
->sharedram
);
7017 __put_user(value
.bufferram
, &target_value
->bufferram
);
7018 __put_user(value
.totalswap
, &target_value
->totalswap
);
7019 __put_user(value
.freeswap
, &target_value
->freeswap
);
7020 __put_user(value
.procs
, &target_value
->procs
);
7021 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
7022 __put_user(value
.freehigh
, &target_value
->freehigh
);
7023 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
7024 unlock_user_struct(target_value
, arg1
, 1);
7028 #ifdef TARGET_NR_ipc
7030 ret
= do_ipc(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7033 #ifdef TARGET_NR_semget
7034 case TARGET_NR_semget
:
7035 ret
= get_errno(semget(arg1
, arg2
, arg3
));
7038 #ifdef TARGET_NR_semop
7039 case TARGET_NR_semop
:
7040 ret
= do_semop(arg1
, arg2
, arg3
);
7043 #ifdef TARGET_NR_semctl
7044 case TARGET_NR_semctl
:
7045 ret
= do_semctl(arg1
, arg2
, arg3
, (union target_semun
)(abi_ulong
)arg4
);
7048 #ifdef TARGET_NR_msgctl
7049 case TARGET_NR_msgctl
:
7050 ret
= do_msgctl(arg1
, arg2
, arg3
);
7053 #ifdef TARGET_NR_msgget
7054 case TARGET_NR_msgget
:
7055 ret
= get_errno(msgget(arg1
, arg2
));
7058 #ifdef TARGET_NR_msgrcv
7059 case TARGET_NR_msgrcv
:
7060 ret
= do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
7063 #ifdef TARGET_NR_msgsnd
7064 case TARGET_NR_msgsnd
:
7065 ret
= do_msgsnd(arg1
, arg2
, arg3
, arg4
);
7068 #ifdef TARGET_NR_shmget
7069 case TARGET_NR_shmget
:
7070 ret
= get_errno(shmget(arg1
, arg2
, arg3
));
7073 #ifdef TARGET_NR_shmctl
7074 case TARGET_NR_shmctl
:
7075 ret
= do_shmctl(arg1
, arg2
, arg3
);
7078 #ifdef TARGET_NR_shmat
7079 case TARGET_NR_shmat
:
7080 ret
= do_shmat(arg1
, arg2
, arg3
);
7083 #ifdef TARGET_NR_shmdt
7084 case TARGET_NR_shmdt
:
7085 ret
= do_shmdt(arg1
);
7088 case TARGET_NR_fsync
:
7089 ret
= get_errno(fsync(arg1
));
7091 case TARGET_NR_clone
:
7092 /* Linux manages to have three different orderings for its
7093 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7094 * match the kernel's CONFIG_CLONE_* settings.
7095 * Microblaze is further special in that it uses a sixth
7096 * implicit argument to clone for the TLS pointer.
7098 #if defined(TARGET_MICROBLAZE)
7099 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
7100 #elif defined(TARGET_CLONE_BACKWARDS)
7101 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
7102 #elif defined(TARGET_CLONE_BACKWARDS2)
7103 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
7105 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
7108 #ifdef __NR_exit_group
7109 /* new thread calls */
7110 case TARGET_NR_exit_group
:
7114 gdb_exit(cpu_env
, arg1
);
7115 ret
= get_errno(exit_group(arg1
));
7118 case TARGET_NR_setdomainname
:
7119 if (!(p
= lock_user_string(arg1
)))
7121 ret
= get_errno(setdomainname(p
, arg2
));
7122 unlock_user(p
, arg1
, 0);
7124 case TARGET_NR_uname
:
7125 /* no need to transcode because we use the linux syscall */
7127 struct new_utsname
* buf
;
7129 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
7131 ret
= get_errno(sys_uname(buf
));
7132 if (!is_error(ret
)) {
7133 /* Overrite the native machine name with whatever is being
7135 strcpy (buf
->machine
, cpu_to_uname_machine(cpu_env
));
7136 /* Allow the user to override the reported release. */
7137 if (qemu_uname_release
&& *qemu_uname_release
)
7138 strcpy (buf
->release
, qemu_uname_release
);
7140 unlock_user_struct(buf
, arg1
, 1);
7144 case TARGET_NR_modify_ldt
:
7145 ret
= do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
7147 #if !defined(TARGET_X86_64)
7148 case TARGET_NR_vm86old
:
7150 case TARGET_NR_vm86
:
7151 ret
= do_vm86(cpu_env
, arg1
, arg2
);
7155 case TARGET_NR_adjtimex
:
7157 #ifdef TARGET_NR_create_module
7158 case TARGET_NR_create_module
:
7160 case TARGET_NR_init_module
:
7161 case TARGET_NR_delete_module
:
7162 #ifdef TARGET_NR_get_kernel_syms
7163 case TARGET_NR_get_kernel_syms
:
7166 case TARGET_NR_quotactl
:
7168 case TARGET_NR_getpgid
:
7169 ret
= get_errno(getpgid(arg1
));
7171 case TARGET_NR_fchdir
:
7172 ret
= get_errno(fchdir(arg1
));
7174 #ifdef TARGET_NR_bdflush /* not on x86_64 */
7175 case TARGET_NR_bdflush
:
7178 #ifdef TARGET_NR_sysfs
7179 case TARGET_NR_sysfs
:
7182 case TARGET_NR_personality
:
7183 ret
= get_errno(personality(arg1
));
7185 #ifdef TARGET_NR_afs_syscall
7186 case TARGET_NR_afs_syscall
:
7189 #ifdef TARGET_NR__llseek /* Not on alpha */
7190 case TARGET_NR__llseek
:
7193 #if !defined(__NR_llseek)
7194 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | arg3
, arg5
);
7196 ret
= get_errno(res
);
7201 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
7203 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
7209 case TARGET_NR_getdents
:
7210 #ifdef __NR_getdents
7211 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
7213 struct target_dirent
*target_dirp
;
7214 struct linux_dirent
*dirp
;
7215 abi_long count
= arg3
;
7217 dirp
= malloc(count
);
7219 ret
= -TARGET_ENOMEM
;
7223 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
7224 if (!is_error(ret
)) {
7225 struct linux_dirent
*de
;
7226 struct target_dirent
*tde
;
7228 int reclen
, treclen
;
7229 int count1
, tnamelen
;
7233 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7237 reclen
= de
->d_reclen
;
7238 tnamelen
= reclen
- offsetof(struct linux_dirent
, d_name
);
7239 assert(tnamelen
>= 0);
7240 treclen
= tnamelen
+ offsetof(struct target_dirent
, d_name
);
7241 assert(count1
+ treclen
<= count
);
7242 tde
->d_reclen
= tswap16(treclen
);
7243 tde
->d_ino
= tswapal(de
->d_ino
);
7244 tde
->d_off
= tswapal(de
->d_off
);
7245 memcpy(tde
->d_name
, de
->d_name
, tnamelen
);
7246 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
7248 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
7252 unlock_user(target_dirp
, arg2
, ret
);
7258 struct linux_dirent
*dirp
;
7259 abi_long count
= arg3
;
7261 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7263 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
7264 if (!is_error(ret
)) {
7265 struct linux_dirent
*de
;
7270 reclen
= de
->d_reclen
;
7273 de
->d_reclen
= tswap16(reclen
);
7274 tswapls(&de
->d_ino
);
7275 tswapls(&de
->d_off
);
7276 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
7280 unlock_user(dirp
, arg2
, ret
);
7284 /* Implement getdents in terms of getdents64 */
7286 struct linux_dirent64
*dirp
;
7287 abi_long count
= arg3
;
7289 dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
7293 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
7294 if (!is_error(ret
)) {
7295 /* Convert the dirent64 structs to target dirent. We do this
7296 * in-place, since we can guarantee that a target_dirent is no
7297 * larger than a dirent64; however this means we have to be
7298 * careful to read everything before writing in the new format.
7300 struct linux_dirent64
*de
;
7301 struct target_dirent
*tde
;
7306 tde
= (struct target_dirent
*)dirp
;
7308 int namelen
, treclen
;
7309 int reclen
= de
->d_reclen
;
7310 uint64_t ino
= de
->d_ino
;
7311 int64_t off
= de
->d_off
;
7312 uint8_t type
= de
->d_type
;
7314 namelen
= strlen(de
->d_name
);
7315 treclen
= offsetof(struct target_dirent
, d_name
)
7317 treclen
= QEMU_ALIGN_UP(treclen
, sizeof(abi_long
));
7319 memmove(tde
->d_name
, de
->d_name
, namelen
+ 1);
7320 tde
->d_ino
= tswapal(ino
);
7321 tde
->d_off
= tswapal(off
);
7322 tde
->d_reclen
= tswap16(treclen
);
7323 /* The target_dirent type is in what was formerly a padding
7324 * byte at the end of the structure:
7326 *(((char *)tde
) + treclen
- 1) = type
;
7328 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
7329 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
7335 unlock_user(dirp
, arg2
, ret
);
7339 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
7340 case TARGET_NR_getdents64
:
7342 struct linux_dirent64
*dirp
;
7343 abi_long count
= arg3
;
7344 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7346 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
7347 if (!is_error(ret
)) {
7348 struct linux_dirent64
*de
;
7353 reclen
= de
->d_reclen
;
7356 de
->d_reclen
= tswap16(reclen
);
7357 tswap64s((uint64_t *)&de
->d_ino
);
7358 tswap64s((uint64_t *)&de
->d_off
);
7359 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
7363 unlock_user(dirp
, arg2
, ret
);
7366 #endif /* TARGET_NR_getdents64 */
7367 #if defined(TARGET_NR__newselect)
7368 case TARGET_NR__newselect
:
7369 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
7372 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7373 # ifdef TARGET_NR_poll
7374 case TARGET_NR_poll
:
7376 # ifdef TARGET_NR_ppoll
7377 case TARGET_NR_ppoll
:
7380 struct target_pollfd
*target_pfd
;
7381 unsigned int nfds
= arg2
;
7386 target_pfd
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_pollfd
) * nfds
, 1);
7390 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
7391 for(i
= 0; i
< nfds
; i
++) {
7392 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
7393 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
7396 # ifdef TARGET_NR_ppoll
7397 if (num
== TARGET_NR_ppoll
) {
7398 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
7399 target_sigset_t
*target_set
;
7400 sigset_t _set
, *set
= &_set
;
7403 if (target_to_host_timespec(timeout_ts
, arg3
)) {
7404 unlock_user(target_pfd
, arg1
, 0);
7412 target_set
= lock_user(VERIFY_READ
, arg4
, sizeof(target_sigset_t
), 1);
7414 unlock_user(target_pfd
, arg1
, 0);
7417 target_to_host_sigset(set
, target_set
);
7422 ret
= get_errno(sys_ppoll(pfd
, nfds
, timeout_ts
, set
, _NSIG
/8));
7424 if (!is_error(ret
) && arg3
) {
7425 host_to_target_timespec(arg3
, timeout_ts
);
7428 unlock_user(target_set
, arg4
, 0);
7432 ret
= get_errno(poll(pfd
, nfds
, timeout
));
7434 if (!is_error(ret
)) {
7435 for(i
= 0; i
< nfds
; i
++) {
7436 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
7439 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
7443 case TARGET_NR_flock
:
7444 /* NOTE: the flock constant seems to be the same for every
7446 ret
= get_errno(flock(arg1
, arg2
));
7448 case TARGET_NR_readv
:
7450 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
7452 ret
= get_errno(readv(arg1
, vec
, arg3
));
7453 unlock_iovec(vec
, arg2
, arg3
, 1);
7455 ret
= -host_to_target_errno(errno
);
7459 case TARGET_NR_writev
:
7461 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
7463 ret
= get_errno(writev(arg1
, vec
, arg3
));
7464 unlock_iovec(vec
, arg2
, arg3
, 0);
7466 ret
= -host_to_target_errno(errno
);
7470 case TARGET_NR_getsid
:
7471 ret
= get_errno(getsid(arg1
));
7473 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
7474 case TARGET_NR_fdatasync
:
7475 ret
= get_errno(fdatasync(arg1
));
7478 case TARGET_NR__sysctl
:
7479 /* We don't implement this, but ENOTDIR is always a safe
7481 ret
= -TARGET_ENOTDIR
;
7483 case TARGET_NR_sched_getaffinity
:
7485 unsigned int mask_size
;
7486 unsigned long *mask
;
7489 * sched_getaffinity needs multiples of ulong, so need to take
7490 * care of mismatches between target ulong and host ulong sizes.
7492 if (arg2
& (sizeof(abi_ulong
) - 1)) {
7493 ret
= -TARGET_EINVAL
;
7496 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
7498 mask
= alloca(mask_size
);
7499 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
7501 if (!is_error(ret
)) {
7502 if (copy_to_user(arg3
, mask
, ret
)) {
7508 case TARGET_NR_sched_setaffinity
:
7510 unsigned int mask_size
;
7511 unsigned long *mask
;
7514 * sched_setaffinity needs multiples of ulong, so need to take
7515 * care of mismatches between target ulong and host ulong sizes.
7517 if (arg2
& (sizeof(abi_ulong
) - 1)) {
7518 ret
= -TARGET_EINVAL
;
7521 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
7523 mask
= alloca(mask_size
);
7524 if (!lock_user_struct(VERIFY_READ
, p
, arg3
, 1)) {
7527 memcpy(mask
, p
, arg2
);
7528 unlock_user_struct(p
, arg2
, 0);
7530 ret
= get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
7533 case TARGET_NR_sched_setparam
:
7535 struct sched_param
*target_schp
;
7536 struct sched_param schp
;
7538 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
7540 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
7541 unlock_user_struct(target_schp
, arg2
, 0);
7542 ret
= get_errno(sched_setparam(arg1
, &schp
));
7545 case TARGET_NR_sched_getparam
:
7547 struct sched_param
*target_schp
;
7548 struct sched_param schp
;
7549 ret
= get_errno(sched_getparam(arg1
, &schp
));
7550 if (!is_error(ret
)) {
7551 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
7553 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
7554 unlock_user_struct(target_schp
, arg2
, 1);
7558 case TARGET_NR_sched_setscheduler
:
7560 struct sched_param
*target_schp
;
7561 struct sched_param schp
;
7562 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
7564 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
7565 unlock_user_struct(target_schp
, arg3
, 0);
7566 ret
= get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
7569 case TARGET_NR_sched_getscheduler
:
7570 ret
= get_errno(sched_getscheduler(arg1
));
7572 case TARGET_NR_sched_yield
:
7573 ret
= get_errno(sched_yield());
7575 case TARGET_NR_sched_get_priority_max
:
7576 ret
= get_errno(sched_get_priority_max(arg1
));
7578 case TARGET_NR_sched_get_priority_min
:
7579 ret
= get_errno(sched_get_priority_min(arg1
));
7581 case TARGET_NR_sched_rr_get_interval
:
7584 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
7585 if (!is_error(ret
)) {
7586 host_to_target_timespec(arg2
, &ts
);
7590 case TARGET_NR_nanosleep
:
7592 struct timespec req
, rem
;
7593 target_to_host_timespec(&req
, arg1
);
7594 ret
= get_errno(nanosleep(&req
, &rem
));
7595 if (is_error(ret
) && arg2
) {
7596 host_to_target_timespec(arg2
, &rem
);
7600 #ifdef TARGET_NR_query_module
7601 case TARGET_NR_query_module
:
7604 #ifdef TARGET_NR_nfsservctl
7605 case TARGET_NR_nfsservctl
:
7608 case TARGET_NR_prctl
:
7610 case PR_GET_PDEATHSIG
:
7613 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
7614 if (!is_error(ret
) && arg2
7615 && put_user_ual(deathsig
, arg2
)) {
7623 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
7627 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
7629 unlock_user(name
, arg2
, 16);
7634 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
7638 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
7640 unlock_user(name
, arg2
, 0);
7645 /* Most prctl options have no pointer arguments */
7646 ret
= get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
7650 #ifdef TARGET_NR_arch_prctl
7651 case TARGET_NR_arch_prctl
:
7652 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
7653 ret
= do_arch_prctl(cpu_env
, arg1
, arg2
);
7659 #ifdef TARGET_NR_pread64
7660 case TARGET_NR_pread64
:
7661 if (regpairs_aligned(cpu_env
)) {
7665 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
7667 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
7668 unlock_user(p
, arg2
, ret
);
7670 case TARGET_NR_pwrite64
:
7671 if (regpairs_aligned(cpu_env
)) {
7675 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
7677 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
7678 unlock_user(p
, arg2
, 0);
7681 case TARGET_NR_getcwd
:
7682 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
7684 ret
= get_errno(sys_getcwd1(p
, arg2
));
7685 unlock_user(p
, arg1
, ret
);
7687 case TARGET_NR_capget
:
7688 case TARGET_NR_capset
:
7690 struct target_user_cap_header
*target_header
;
7691 struct target_user_cap_data
*target_data
= NULL
;
7692 struct __user_cap_header_struct header
;
7693 struct __user_cap_data_struct data
[2];
7694 struct __user_cap_data_struct
*dataptr
= NULL
;
7695 int i
, target_datalen
;
7698 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
7701 header
.version
= tswap32(target_header
->version
);
7702 header
.pid
= tswap32(target_header
->pid
);
7704 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
7705 /* Version 2 and up takes pointer to two user_data structs */
7709 target_datalen
= sizeof(*target_data
) * data_items
;
7712 if (num
== TARGET_NR_capget
) {
7713 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
7715 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
7718 unlock_user_struct(target_header
, arg1
, 0);
7722 if (num
== TARGET_NR_capset
) {
7723 for (i
= 0; i
< data_items
; i
++) {
7724 data
[i
].effective
= tswap32(target_data
[i
].effective
);
7725 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
7726 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
7733 if (num
== TARGET_NR_capget
) {
7734 ret
= get_errno(capget(&header
, dataptr
));
7736 ret
= get_errno(capset(&header
, dataptr
));
7739 /* The kernel always updates version for both capget and capset */
7740 target_header
->version
= tswap32(header
.version
);
7741 unlock_user_struct(target_header
, arg1
, 1);
7744 if (num
== TARGET_NR_capget
) {
7745 for (i
= 0; i
< data_items
; i
++) {
7746 target_data
[i
].effective
= tswap32(data
[i
].effective
);
7747 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
7748 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
7750 unlock_user(target_data
, arg2
, target_datalen
);
7752 unlock_user(target_data
, arg2
, 0);
7757 case TARGET_NR_sigaltstack
:
7758 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
7759 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
7760 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
7761 ret
= do_sigaltstack(arg1
, arg2
, get_sp_from_cpustate((CPUArchState
*)cpu_env
));
7767 #ifdef CONFIG_SENDFILE
7768 case TARGET_NR_sendfile
:
7773 ret
= get_user_sal(off
, arg3
);
7774 if (is_error(ret
)) {
7779 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
7780 if (!is_error(ret
) && arg3
) {
7781 abi_long ret2
= put_user_sal(off
, arg3
);
7782 if (is_error(ret2
)) {
7788 #ifdef TARGET_NR_sendfile64
7789 case TARGET_NR_sendfile64
:
7794 ret
= get_user_s64(off
, arg3
);
7795 if (is_error(ret
)) {
7800 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
7801 if (!is_error(ret
) && arg3
) {
7802 abi_long ret2
= put_user_s64(off
, arg3
);
7803 if (is_error(ret2
)) {
7811 case TARGET_NR_sendfile
:
7812 #ifdef TARGET_NR_sendfile64
7813 case TARGET_NR_sendfile64
:
7818 #ifdef TARGET_NR_getpmsg
7819 case TARGET_NR_getpmsg
:
7822 #ifdef TARGET_NR_putpmsg
7823 case TARGET_NR_putpmsg
:
7826 #ifdef TARGET_NR_vfork
7827 case TARGET_NR_vfork
:
7828 ret
= get_errno(do_fork(cpu_env
, CLONE_VFORK
| CLONE_VM
| SIGCHLD
,
7832 #ifdef TARGET_NR_ugetrlimit
7833 case TARGET_NR_ugetrlimit
:
7836 int resource
= target_to_host_resource(arg1
);
7837 ret
= get_errno(getrlimit(resource
, &rlim
));
7838 if (!is_error(ret
)) {
7839 struct target_rlimit
*target_rlim
;
7840 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
7842 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
7843 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
7844 unlock_user_struct(target_rlim
, arg2
, 1);
7849 #ifdef TARGET_NR_truncate64
7850 case TARGET_NR_truncate64
:
7851 if (!(p
= lock_user_string(arg1
)))
7853 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
7854 unlock_user(p
, arg1
, 0);
7857 #ifdef TARGET_NR_ftruncate64
7858 case TARGET_NR_ftruncate64
:
7859 ret
= target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
7862 #ifdef TARGET_NR_stat64
7863 case TARGET_NR_stat64
:
7864 if (!(p
= lock_user_string(arg1
)))
7866 ret
= get_errno(stat(path(p
), &st
));
7867 unlock_user(p
, arg1
, 0);
7869 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
7872 #ifdef TARGET_NR_lstat64
7873 case TARGET_NR_lstat64
:
7874 if (!(p
= lock_user_string(arg1
)))
7876 ret
= get_errno(lstat(path(p
), &st
));
7877 unlock_user(p
, arg1
, 0);
7879 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
7882 #ifdef TARGET_NR_fstat64
7883 case TARGET_NR_fstat64
:
7884 ret
= get_errno(fstat(arg1
, &st
));
7886 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
7889 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
7890 #ifdef TARGET_NR_fstatat64
7891 case TARGET_NR_fstatat64
:
7893 #ifdef TARGET_NR_newfstatat
7894 case TARGET_NR_newfstatat
:
7896 if (!(p
= lock_user_string(arg2
)))
7898 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
7900 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
7903 case TARGET_NR_lchown
:
7904 if (!(p
= lock_user_string(arg1
)))
7906 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
7907 unlock_user(p
, arg1
, 0);
7909 #ifdef TARGET_NR_getuid
7910 case TARGET_NR_getuid
:
7911 ret
= get_errno(high2lowuid(getuid()));
7914 #ifdef TARGET_NR_getgid
7915 case TARGET_NR_getgid
:
7916 ret
= get_errno(high2lowgid(getgid()));
7919 #ifdef TARGET_NR_geteuid
7920 case TARGET_NR_geteuid
:
7921 ret
= get_errno(high2lowuid(geteuid()));
7924 #ifdef TARGET_NR_getegid
7925 case TARGET_NR_getegid
:
7926 ret
= get_errno(high2lowgid(getegid()));
7929 case TARGET_NR_setreuid
:
7930 ret
= get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
7932 case TARGET_NR_setregid
:
7933 ret
= get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
7935 case TARGET_NR_getgroups
:
7937 int gidsetsize
= arg1
;
7938 target_id
*target_grouplist
;
7942 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
7943 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
7944 if (gidsetsize
== 0)
7946 if (!is_error(ret
)) {
7947 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
7948 if (!target_grouplist
)
7950 for(i
= 0;i
< ret
; i
++)
7951 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
7952 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
7956 case TARGET_NR_setgroups
:
7958 int gidsetsize
= arg1
;
7959 target_id
*target_grouplist
;
7960 gid_t
*grouplist
= NULL
;
7963 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
7964 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
7965 if (!target_grouplist
) {
7966 ret
= -TARGET_EFAULT
;
7969 for (i
= 0; i
< gidsetsize
; i
++) {
7970 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
7972 unlock_user(target_grouplist
, arg2
, 0);
7974 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
7977 case TARGET_NR_fchown
:
7978 ret
= get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
7980 #if defined(TARGET_NR_fchownat)
7981 case TARGET_NR_fchownat
:
7982 if (!(p
= lock_user_string(arg2
)))
7984 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
7985 low2highgid(arg4
), arg5
));
7986 unlock_user(p
, arg2
, 0);
7989 #ifdef TARGET_NR_setresuid
7990 case TARGET_NR_setresuid
:
7991 ret
= get_errno(setresuid(low2highuid(arg1
),
7993 low2highuid(arg3
)));
7996 #ifdef TARGET_NR_getresuid
7997 case TARGET_NR_getresuid
:
7999 uid_t ruid
, euid
, suid
;
8000 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
8001 if (!is_error(ret
)) {
8002 if (put_user_id(high2lowuid(ruid
), arg1
)
8003 || put_user_id(high2lowuid(euid
), arg2
)
8004 || put_user_id(high2lowuid(suid
), arg3
))
8010 #ifdef TARGET_NR_getresgid
8011 case TARGET_NR_setresgid
:
8012 ret
= get_errno(setresgid(low2highgid(arg1
),
8014 low2highgid(arg3
)));
8017 #ifdef TARGET_NR_getresgid
8018 case TARGET_NR_getresgid
:
8020 gid_t rgid
, egid
, sgid
;
8021 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
8022 if (!is_error(ret
)) {
8023 if (put_user_id(high2lowgid(rgid
), arg1
)
8024 || put_user_id(high2lowgid(egid
), arg2
)
8025 || put_user_id(high2lowgid(sgid
), arg3
))
8031 case TARGET_NR_chown
:
8032 if (!(p
= lock_user_string(arg1
)))
8034 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
8035 unlock_user(p
, arg1
, 0);
8037 case TARGET_NR_setuid
:
8038 ret
= get_errno(setuid(low2highuid(arg1
)));
8040 case TARGET_NR_setgid
:
8041 ret
= get_errno(setgid(low2highgid(arg1
)));
8043 case TARGET_NR_setfsuid
:
8044 ret
= get_errno(setfsuid(arg1
));
8046 case TARGET_NR_setfsgid
:
8047 ret
= get_errno(setfsgid(arg1
));
8050 #ifdef TARGET_NR_lchown32
8051 case TARGET_NR_lchown32
:
8052 if (!(p
= lock_user_string(arg1
)))
8054 ret
= get_errno(lchown(p
, arg2
, arg3
));
8055 unlock_user(p
, arg1
, 0);
8058 #ifdef TARGET_NR_getuid32
8059 case TARGET_NR_getuid32
:
8060 ret
= get_errno(getuid());
8064 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
8065 /* Alpha specific */
8066 case TARGET_NR_getxuid
:
8070 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
8072 ret
= get_errno(getuid());
8075 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
8076 /* Alpha specific */
8077 case TARGET_NR_getxgid
:
8081 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
8083 ret
= get_errno(getgid());
8086 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
8087 /* Alpha specific */
8088 case TARGET_NR_osf_getsysinfo
:
8089 ret
= -TARGET_EOPNOTSUPP
;
8091 case TARGET_GSI_IEEE_FP_CONTROL
:
8093 uint64_t swcr
, fpcr
= cpu_alpha_load_fpcr (cpu_env
);
8095 /* Copied from linux ieee_fpcr_to_swcr. */
8096 swcr
= (fpcr
>> 35) & SWCR_STATUS_MASK
;
8097 swcr
|= (fpcr
>> 36) & SWCR_MAP_DMZ
;
8098 swcr
|= (~fpcr
>> 48) & (SWCR_TRAP_ENABLE_INV
8099 | SWCR_TRAP_ENABLE_DZE
8100 | SWCR_TRAP_ENABLE_OVF
);
8101 swcr
|= (~fpcr
>> 57) & (SWCR_TRAP_ENABLE_UNF
8102 | SWCR_TRAP_ENABLE_INE
);
8103 swcr
|= (fpcr
>> 47) & SWCR_MAP_UMZ
;
8104 swcr
|= (~fpcr
>> 41) & SWCR_TRAP_ENABLE_DNO
;
8106 if (put_user_u64 (swcr
, arg2
))
8112 /* case GSI_IEEE_STATE_AT_SIGNAL:
8113 -- Not implemented in linux kernel.
8115 -- Retrieves current unaligned access state; not much used.
8117 -- Retrieves implver information; surely not used.
8119 -- Grabs a copy of the HWRPB; surely not used.
8124 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8125 /* Alpha specific */
8126 case TARGET_NR_osf_setsysinfo
:
8127 ret
= -TARGET_EOPNOTSUPP
;
8129 case TARGET_SSI_IEEE_FP_CONTROL
:
8131 uint64_t swcr
, fpcr
, orig_fpcr
;
8133 if (get_user_u64 (swcr
, arg2
)) {
8136 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
8137 fpcr
= orig_fpcr
& FPCR_DYN_MASK
;
8139 /* Copied from linux ieee_swcr_to_fpcr. */
8140 fpcr
|= (swcr
& SWCR_STATUS_MASK
) << 35;
8141 fpcr
|= (swcr
& SWCR_MAP_DMZ
) << 36;
8142 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_INV
8143 | SWCR_TRAP_ENABLE_DZE
8144 | SWCR_TRAP_ENABLE_OVF
)) << 48;
8145 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_UNF
8146 | SWCR_TRAP_ENABLE_INE
)) << 57;
8147 fpcr
|= (swcr
& SWCR_MAP_UMZ
? FPCR_UNDZ
| FPCR_UNFD
: 0);
8148 fpcr
|= (~swcr
& SWCR_TRAP_ENABLE_DNO
) << 41;
8150 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
8155 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
8157 uint64_t exc
, fpcr
, orig_fpcr
;
8160 if (get_user_u64(exc
, arg2
)) {
8164 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
8166 /* We only add to the exception status here. */
8167 fpcr
= orig_fpcr
| ((exc
& SWCR_STATUS_MASK
) << 35);
8169 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
8172 /* Old exceptions are not signaled. */
8173 fpcr
&= ~(orig_fpcr
& FPCR_STATUS_MASK
);
8175 /* If any exceptions set by this call,
8176 and are unmasked, send a signal. */
8178 if ((fpcr
& (FPCR_INE
| FPCR_INED
)) == FPCR_INE
) {
8179 si_code
= TARGET_FPE_FLTRES
;
8181 if ((fpcr
& (FPCR_UNF
| FPCR_UNFD
)) == FPCR_UNF
) {
8182 si_code
= TARGET_FPE_FLTUND
;
8184 if ((fpcr
& (FPCR_OVF
| FPCR_OVFD
)) == FPCR_OVF
) {
8185 si_code
= TARGET_FPE_FLTOVF
;
8187 if ((fpcr
& (FPCR_DZE
| FPCR_DZED
)) == FPCR_DZE
) {
8188 si_code
= TARGET_FPE_FLTDIV
;
8190 if ((fpcr
& (FPCR_INV
| FPCR_INVD
)) == FPCR_INV
) {
8191 si_code
= TARGET_FPE_FLTINV
;
8194 target_siginfo_t info
;
8195 info
.si_signo
= SIGFPE
;
8197 info
.si_code
= si_code
;
8198 info
._sifields
._sigfault
._addr
8199 = ((CPUArchState
*)cpu_env
)->pc
;
8200 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
8205 /* case SSI_NVPAIRS:
8206 -- Used with SSIN_UACPROC to enable unaligned accesses.
8207 case SSI_IEEE_STATE_AT_SIGNAL:
8208 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8209 -- Not implemented in linux kernel
8214 #ifdef TARGET_NR_osf_sigprocmask
8215 /* Alpha specific. */
8216 case TARGET_NR_osf_sigprocmask
:
8220 sigset_t set
, oldset
;
8223 case TARGET_SIG_BLOCK
:
8226 case TARGET_SIG_UNBLOCK
:
8229 case TARGET_SIG_SETMASK
:
8233 ret
= -TARGET_EINVAL
;
8237 target_to_host_old_sigset(&set
, &mask
);
8238 do_sigprocmask(how
, &set
, &oldset
);
8239 host_to_target_old_sigset(&mask
, &oldset
);
8245 #ifdef TARGET_NR_getgid32
8246 case TARGET_NR_getgid32
:
8247 ret
= get_errno(getgid());
8250 #ifdef TARGET_NR_geteuid32
8251 case TARGET_NR_geteuid32
:
8252 ret
= get_errno(geteuid());
8255 #ifdef TARGET_NR_getegid32
8256 case TARGET_NR_getegid32
:
8257 ret
= get_errno(getegid());
8260 #ifdef TARGET_NR_setreuid32
8261 case TARGET_NR_setreuid32
:
8262 ret
= get_errno(setreuid(arg1
, arg2
));
8265 #ifdef TARGET_NR_setregid32
8266 case TARGET_NR_setregid32
:
8267 ret
= get_errno(setregid(arg1
, arg2
));
8270 #ifdef TARGET_NR_getgroups32
8271 case TARGET_NR_getgroups32
:
8273 int gidsetsize
= arg1
;
8274 uint32_t *target_grouplist
;
8278 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8279 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
8280 if (gidsetsize
== 0)
8282 if (!is_error(ret
)) {
8283 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
8284 if (!target_grouplist
) {
8285 ret
= -TARGET_EFAULT
;
8288 for(i
= 0;i
< ret
; i
++)
8289 target_grouplist
[i
] = tswap32(grouplist
[i
]);
8290 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
8295 #ifdef TARGET_NR_setgroups32
8296 case TARGET_NR_setgroups32
:
8298 int gidsetsize
= arg1
;
8299 uint32_t *target_grouplist
;
8303 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8304 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
8305 if (!target_grouplist
) {
8306 ret
= -TARGET_EFAULT
;
8309 for(i
= 0;i
< gidsetsize
; i
++)
8310 grouplist
[i
] = tswap32(target_grouplist
[i
]);
8311 unlock_user(target_grouplist
, arg2
, 0);
8312 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
8316 #ifdef TARGET_NR_fchown32
8317 case TARGET_NR_fchown32
:
8318 ret
= get_errno(fchown(arg1
, arg2
, arg3
));
8321 #ifdef TARGET_NR_setresuid32
8322 case TARGET_NR_setresuid32
:
8323 ret
= get_errno(setresuid(arg1
, arg2
, arg3
));
8326 #ifdef TARGET_NR_getresuid32
8327 case TARGET_NR_getresuid32
:
8329 uid_t ruid
, euid
, suid
;
8330 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
8331 if (!is_error(ret
)) {
8332 if (put_user_u32(ruid
, arg1
)
8333 || put_user_u32(euid
, arg2
)
8334 || put_user_u32(suid
, arg3
))
8340 #ifdef TARGET_NR_setresgid32
8341 case TARGET_NR_setresgid32
:
8342 ret
= get_errno(setresgid(arg1
, arg2
, arg3
));
8345 #ifdef TARGET_NR_getresgid32
8346 case TARGET_NR_getresgid32
:
8348 gid_t rgid
, egid
, sgid
;
8349 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
8350 if (!is_error(ret
)) {
8351 if (put_user_u32(rgid
, arg1
)
8352 || put_user_u32(egid
, arg2
)
8353 || put_user_u32(sgid
, arg3
))
8359 #ifdef TARGET_NR_chown32
8360 case TARGET_NR_chown32
:
8361 if (!(p
= lock_user_string(arg1
)))
8363 ret
= get_errno(chown(p
, arg2
, arg3
));
8364 unlock_user(p
, arg1
, 0);
8367 #ifdef TARGET_NR_setuid32
8368 case TARGET_NR_setuid32
:
8369 ret
= get_errno(setuid(arg1
));
8372 #ifdef TARGET_NR_setgid32
8373 case TARGET_NR_setgid32
:
8374 ret
= get_errno(setgid(arg1
));
8377 #ifdef TARGET_NR_setfsuid32
8378 case TARGET_NR_setfsuid32
:
8379 ret
= get_errno(setfsuid(arg1
));
8382 #ifdef TARGET_NR_setfsgid32
8383 case TARGET_NR_setfsgid32
:
8384 ret
= get_errno(setfsgid(arg1
));
8388 case TARGET_NR_pivot_root
:
8390 #ifdef TARGET_NR_mincore
8391 case TARGET_NR_mincore
:
8394 ret
= -TARGET_EFAULT
;
8395 if (!(a
= lock_user(VERIFY_READ
, arg1
,arg2
, 0)))
8397 if (!(p
= lock_user_string(arg3
)))
8399 ret
= get_errno(mincore(a
, arg2
, p
));
8400 unlock_user(p
, arg3
, ret
);
8402 unlock_user(a
, arg1
, 0);
8406 #ifdef TARGET_NR_arm_fadvise64_64
8407 case TARGET_NR_arm_fadvise64_64
:
8410 * arm_fadvise64_64 looks like fadvise64_64 but
8411 * with different argument order
8419 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
8420 #ifdef TARGET_NR_fadvise64_64
8421 case TARGET_NR_fadvise64_64
:
8423 #ifdef TARGET_NR_fadvise64
8424 case TARGET_NR_fadvise64
:
8428 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
8429 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
8430 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
8431 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
8435 ret
= -posix_fadvise(arg1
, arg2
, arg3
, arg4
);
8438 #ifdef TARGET_NR_madvise
8439 case TARGET_NR_madvise
:
8440 /* A straight passthrough may not be safe because qemu sometimes
8441 turns private file-backed mappings into anonymous mappings.
8442 This will break MADV_DONTNEED.
8443 This is a hint, so ignoring and returning success is ok. */
8447 #if TARGET_ABI_BITS == 32
8448 case TARGET_NR_fcntl64
:
8452 struct target_flock64
*target_fl
;
8454 struct target_eabi_flock64
*target_efl
;
8457 cmd
= target_to_host_fcntl_cmd(arg2
);
8458 if (cmd
== -TARGET_EINVAL
) {
8464 case TARGET_F_GETLK64
:
8466 if (((CPUARMState
*)cpu_env
)->eabi
) {
8467 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
8469 fl
.l_type
= tswap16(target_efl
->l_type
);
8470 fl
.l_whence
= tswap16(target_efl
->l_whence
);
8471 fl
.l_start
= tswap64(target_efl
->l_start
);
8472 fl
.l_len
= tswap64(target_efl
->l_len
);
8473 fl
.l_pid
= tswap32(target_efl
->l_pid
);
8474 unlock_user_struct(target_efl
, arg3
, 0);
8478 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
8480 fl
.l_type
= tswap16(target_fl
->l_type
);
8481 fl
.l_whence
= tswap16(target_fl
->l_whence
);
8482 fl
.l_start
= tswap64(target_fl
->l_start
);
8483 fl
.l_len
= tswap64(target_fl
->l_len
);
8484 fl
.l_pid
= tswap32(target_fl
->l_pid
);
8485 unlock_user_struct(target_fl
, arg3
, 0);
8487 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
8490 if (((CPUARMState
*)cpu_env
)->eabi
) {
8491 if (!lock_user_struct(VERIFY_WRITE
, target_efl
, arg3
, 0))
8493 target_efl
->l_type
= tswap16(fl
.l_type
);
8494 target_efl
->l_whence
= tswap16(fl
.l_whence
);
8495 target_efl
->l_start
= tswap64(fl
.l_start
);
8496 target_efl
->l_len
= tswap64(fl
.l_len
);
8497 target_efl
->l_pid
= tswap32(fl
.l_pid
);
8498 unlock_user_struct(target_efl
, arg3
, 1);
8502 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg3
, 0))
8504 target_fl
->l_type
= tswap16(fl
.l_type
);
8505 target_fl
->l_whence
= tswap16(fl
.l_whence
);
8506 target_fl
->l_start
= tswap64(fl
.l_start
);
8507 target_fl
->l_len
= tswap64(fl
.l_len
);
8508 target_fl
->l_pid
= tswap32(fl
.l_pid
);
8509 unlock_user_struct(target_fl
, arg3
, 1);
8514 case TARGET_F_SETLK64
:
8515 case TARGET_F_SETLKW64
:
8517 if (((CPUARMState
*)cpu_env
)->eabi
) {
8518 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
8520 fl
.l_type
= tswap16(target_efl
->l_type
);
8521 fl
.l_whence
= tswap16(target_efl
->l_whence
);
8522 fl
.l_start
= tswap64(target_efl
->l_start
);
8523 fl
.l_len
= tswap64(target_efl
->l_len
);
8524 fl
.l_pid
= tswap32(target_efl
->l_pid
);
8525 unlock_user_struct(target_efl
, arg3
, 0);
8529 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
8531 fl
.l_type
= tswap16(target_fl
->l_type
);
8532 fl
.l_whence
= tswap16(target_fl
->l_whence
);
8533 fl
.l_start
= tswap64(target_fl
->l_start
);
8534 fl
.l_len
= tswap64(target_fl
->l_len
);
8535 fl
.l_pid
= tswap32(target_fl
->l_pid
);
8536 unlock_user_struct(target_fl
, arg3
, 0);
8538 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
8541 ret
= do_fcntl(arg1
, arg2
, arg3
);
8547 #ifdef TARGET_NR_cacheflush
8548 case TARGET_NR_cacheflush
:
8549 /* self-modifying code is handled automatically, so nothing needed */
8553 #ifdef TARGET_NR_security
8554 case TARGET_NR_security
:
8557 #ifdef TARGET_NR_getpagesize
8558 case TARGET_NR_getpagesize
:
8559 ret
= TARGET_PAGE_SIZE
;
8562 case TARGET_NR_gettid
:
8563 ret
= get_errno(gettid());
8565 #ifdef TARGET_NR_readahead
8566 case TARGET_NR_readahead
:
8567 #if TARGET_ABI_BITS == 32
8568 if (regpairs_aligned(cpu_env
)) {
8573 ret
= get_errno(readahead(arg1
, ((off64_t
)arg3
<< 32) | arg2
, arg4
));
8575 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
8580 #ifdef TARGET_NR_setxattr
8581 case TARGET_NR_listxattr
:
8582 case TARGET_NR_llistxattr
:
8586 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
8588 ret
= -TARGET_EFAULT
;
8592 p
= lock_user_string(arg1
);
8594 if (num
== TARGET_NR_listxattr
) {
8595 ret
= get_errno(listxattr(p
, b
, arg3
));
8597 ret
= get_errno(llistxattr(p
, b
, arg3
));
8600 ret
= -TARGET_EFAULT
;
8602 unlock_user(p
, arg1
, 0);
8603 unlock_user(b
, arg2
, arg3
);
8606 case TARGET_NR_flistxattr
:
8610 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
8612 ret
= -TARGET_EFAULT
;
8616 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
8617 unlock_user(b
, arg2
, arg3
);
8620 case TARGET_NR_setxattr
:
8621 case TARGET_NR_lsetxattr
:
8623 void *p
, *n
, *v
= 0;
8625 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
8627 ret
= -TARGET_EFAULT
;
8631 p
= lock_user_string(arg1
);
8632 n
= lock_user_string(arg2
);
8634 if (num
== TARGET_NR_setxattr
) {
8635 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
8637 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
8640 ret
= -TARGET_EFAULT
;
8642 unlock_user(p
, arg1
, 0);
8643 unlock_user(n
, arg2
, 0);
8644 unlock_user(v
, arg3
, 0);
8647 case TARGET_NR_fsetxattr
:
8651 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
8653 ret
= -TARGET_EFAULT
;
8657 n
= lock_user_string(arg2
);
8659 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
8661 ret
= -TARGET_EFAULT
;
8663 unlock_user(n
, arg2
, 0);
8664 unlock_user(v
, arg3
, 0);
8667 case TARGET_NR_getxattr
:
8668 case TARGET_NR_lgetxattr
:
8670 void *p
, *n
, *v
= 0;
8672 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
8674 ret
= -TARGET_EFAULT
;
8678 p
= lock_user_string(arg1
);
8679 n
= lock_user_string(arg2
);
8681 if (num
== TARGET_NR_getxattr
) {
8682 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
8684 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
8687 ret
= -TARGET_EFAULT
;
8689 unlock_user(p
, arg1
, 0);
8690 unlock_user(n
, arg2
, 0);
8691 unlock_user(v
, arg3
, arg4
);
8694 case TARGET_NR_fgetxattr
:
8698 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
8700 ret
= -TARGET_EFAULT
;
8704 n
= lock_user_string(arg2
);
8706 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
8708 ret
= -TARGET_EFAULT
;
8710 unlock_user(n
, arg2
, 0);
8711 unlock_user(v
, arg3
, arg4
);
8714 case TARGET_NR_removexattr
:
8715 case TARGET_NR_lremovexattr
:
8718 p
= lock_user_string(arg1
);
8719 n
= lock_user_string(arg2
);
8721 if (num
== TARGET_NR_removexattr
) {
8722 ret
= get_errno(removexattr(p
, n
));
8724 ret
= get_errno(lremovexattr(p
, n
));
8727 ret
= -TARGET_EFAULT
;
8729 unlock_user(p
, arg1
, 0);
8730 unlock_user(n
, arg2
, 0);
8733 case TARGET_NR_fremovexattr
:
8736 n
= lock_user_string(arg2
);
8738 ret
= get_errno(fremovexattr(arg1
, n
));
8740 ret
= -TARGET_EFAULT
;
8742 unlock_user(n
, arg2
, 0);
8746 #endif /* CONFIG_ATTR */
8747 #ifdef TARGET_NR_set_thread_area
8748 case TARGET_NR_set_thread_area
:
8749 #if defined(TARGET_MIPS)
8750 ((CPUMIPSState
*) cpu_env
)->tls_value
= arg1
;
8753 #elif defined(TARGET_CRIS)
8755 ret
= -TARGET_EINVAL
;
8757 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
8761 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
8762 ret
= do_set_thread_area(cpu_env
, arg1
);
8764 #elif defined(TARGET_M68K)
8766 TaskState
*ts
= cpu
->opaque
;
8767 ts
->tp_value
= arg1
;
8772 goto unimplemented_nowarn
;
8775 #ifdef TARGET_NR_get_thread_area
8776 case TARGET_NR_get_thread_area
:
8777 #if defined(TARGET_I386) && defined(TARGET_ABI32)
8778 ret
= do_get_thread_area(cpu_env
, arg1
);
8780 #elif defined(TARGET_M68K)
8782 TaskState
*ts
= cpu
->opaque
;
8787 goto unimplemented_nowarn
;
8790 #ifdef TARGET_NR_getdomainname
8791 case TARGET_NR_getdomainname
:
8792 goto unimplemented_nowarn
;
8795 #ifdef TARGET_NR_clock_gettime
8796 case TARGET_NR_clock_gettime
:
8799 ret
= get_errno(clock_gettime(arg1
, &ts
));
8800 if (!is_error(ret
)) {
8801 host_to_target_timespec(arg2
, &ts
);
8806 #ifdef TARGET_NR_clock_getres
8807 case TARGET_NR_clock_getres
:
8810 ret
= get_errno(clock_getres(arg1
, &ts
));
8811 if (!is_error(ret
)) {
8812 host_to_target_timespec(arg2
, &ts
);
8817 #ifdef TARGET_NR_clock_nanosleep
8818 case TARGET_NR_clock_nanosleep
:
8821 target_to_host_timespec(&ts
, arg3
);
8822 ret
= get_errno(clock_nanosleep(arg1
, arg2
, &ts
, arg4
? &ts
: NULL
));
8824 host_to_target_timespec(arg4
, &ts
);
8829 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8830 case TARGET_NR_set_tid_address
:
8831 ret
= get_errno(set_tid_address((int *)g2h(arg1
)));
8835 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
8836 case TARGET_NR_tkill
:
8837 ret
= get_errno(sys_tkill((int)arg1
, target_to_host_signal(arg2
)));
8841 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
8842 case TARGET_NR_tgkill
:
8843 ret
= get_errno(sys_tgkill((int)arg1
, (int)arg2
,
8844 target_to_host_signal(arg3
)));
8848 #ifdef TARGET_NR_set_robust_list
8849 case TARGET_NR_set_robust_list
:
8850 case TARGET_NR_get_robust_list
:
8851 /* The ABI for supporting robust futexes has userspace pass
8852 * the kernel a pointer to a linked list which is updated by
8853 * userspace after the syscall; the list is walked by the kernel
8854 * when the thread exits. Since the linked list in QEMU guest
8855 * memory isn't a valid linked list for the host and we have
8856 * no way to reliably intercept the thread-death event, we can't
8857 * support these. Silently return ENOSYS so that guest userspace
8858 * falls back to a non-robust futex implementation (which should
8859 * be OK except in the corner case of the guest crashing while
8860 * holding a mutex that is shared with another process via
8863 goto unimplemented_nowarn
;
8866 #if defined(TARGET_NR_utimensat)
8867 case TARGET_NR_utimensat
:
8869 struct timespec
*tsp
, ts
[2];
8873 target_to_host_timespec(ts
, arg3
);
8874 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
8878 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
8880 if (!(p
= lock_user_string(arg2
))) {
8881 ret
= -TARGET_EFAULT
;
8884 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
8885 unlock_user(p
, arg2
, 0);
8890 case TARGET_NR_futex
:
8891 ret
= do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
8893 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
8894 case TARGET_NR_inotify_init
:
8895 ret
= get_errno(sys_inotify_init());
8898 #ifdef CONFIG_INOTIFY1
8899 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8900 case TARGET_NR_inotify_init1
:
8901 ret
= get_errno(sys_inotify_init1(arg1
));
8905 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
8906 case TARGET_NR_inotify_add_watch
:
8907 p
= lock_user_string(arg2
);
8908 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
8909 unlock_user(p
, arg2
, 0);
8912 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
8913 case TARGET_NR_inotify_rm_watch
:
8914 ret
= get_errno(sys_inotify_rm_watch(arg1
, arg2
));
8918 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
8919 case TARGET_NR_mq_open
:
8921 struct mq_attr posix_mq_attr
;
8923 p
= lock_user_string(arg1
- 1);
8925 copy_from_user_mq_attr (&posix_mq_attr
, arg4
);
8926 ret
= get_errno(mq_open(p
, arg2
, arg3
, &posix_mq_attr
));
8927 unlock_user (p
, arg1
, 0);
8931 case TARGET_NR_mq_unlink
:
8932 p
= lock_user_string(arg1
- 1);
8933 ret
= get_errno(mq_unlink(p
));
8934 unlock_user (p
, arg1
, 0);
8937 case TARGET_NR_mq_timedsend
:
8941 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
8943 target_to_host_timespec(&ts
, arg5
);
8944 ret
= get_errno(mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
8945 host_to_target_timespec(arg5
, &ts
);
8948 ret
= get_errno(mq_send(arg1
, p
, arg3
, arg4
));
8949 unlock_user (p
, arg2
, arg3
);
8953 case TARGET_NR_mq_timedreceive
:
8958 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
8960 target_to_host_timespec(&ts
, arg5
);
8961 ret
= get_errno(mq_timedreceive(arg1
, p
, arg3
, &prio
, &ts
));
8962 host_to_target_timespec(arg5
, &ts
);
8965 ret
= get_errno(mq_receive(arg1
, p
, arg3
, &prio
));
8966 unlock_user (p
, arg2
, arg3
);
8968 put_user_u32(prio
, arg4
);
8972 /* Not implemented for now... */
8973 /* case TARGET_NR_mq_notify: */
8976 case TARGET_NR_mq_getsetattr
:
8978 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
8981 ret
= mq_getattr(arg1
, &posix_mq_attr_out
);
8982 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
8985 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
8986 ret
|= mq_setattr(arg1
, &posix_mq_attr_in
, &posix_mq_attr_out
);
8993 #ifdef CONFIG_SPLICE
8994 #ifdef TARGET_NR_tee
8997 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
9001 #ifdef TARGET_NR_splice
9002 case TARGET_NR_splice
:
9004 loff_t loff_in
, loff_out
;
9005 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
9007 get_user_u64(loff_in
, arg2
);
9008 ploff_in
= &loff_in
;
9011 get_user_u64(loff_out
, arg2
);
9012 ploff_out
= &loff_out
;
9014 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
9018 #ifdef TARGET_NR_vmsplice
9019 case TARGET_NR_vmsplice
:
9021 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
9023 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
9024 unlock_iovec(vec
, arg2
, arg3
, 0);
9026 ret
= -host_to_target_errno(errno
);
9031 #endif /* CONFIG_SPLICE */
9032 #ifdef CONFIG_EVENTFD
9033 #if defined(TARGET_NR_eventfd)
9034 case TARGET_NR_eventfd
:
9035 ret
= get_errno(eventfd(arg1
, 0));
9038 #if defined(TARGET_NR_eventfd2)
9039 case TARGET_NR_eventfd2
:
9041 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
));
9042 if (arg2
& TARGET_O_NONBLOCK
) {
9043 host_flags
|= O_NONBLOCK
;
9045 if (arg2
& TARGET_O_CLOEXEC
) {
9046 host_flags
|= O_CLOEXEC
;
9048 ret
= get_errno(eventfd(arg1
, host_flags
));
9052 #endif /* CONFIG_EVENTFD */
9053 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
9054 case TARGET_NR_fallocate
:
9055 #if TARGET_ABI_BITS == 32
9056 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
9057 target_offset64(arg5
, arg6
)));
9059 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
9063 #if defined(CONFIG_SYNC_FILE_RANGE)
9064 #if defined(TARGET_NR_sync_file_range)
9065 case TARGET_NR_sync_file_range
:
9066 #if TARGET_ABI_BITS == 32
9067 #if defined(TARGET_MIPS)
9068 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
9069 target_offset64(arg5
, arg6
), arg7
));
9071 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
9072 target_offset64(arg4
, arg5
), arg6
));
9073 #endif /* !TARGET_MIPS */
9075 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
9079 #if defined(TARGET_NR_sync_file_range2)
9080 case TARGET_NR_sync_file_range2
:
9081 /* This is like sync_file_range but the arguments are reordered */
9082 #if TARGET_ABI_BITS == 32
9083 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
9084 target_offset64(arg5
, arg6
), arg2
));
9086 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
9091 #if defined(CONFIG_EPOLL)
9092 #if defined(TARGET_NR_epoll_create)
9093 case TARGET_NR_epoll_create
:
9094 ret
= get_errno(epoll_create(arg1
));
9097 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
9098 case TARGET_NR_epoll_create1
:
9099 ret
= get_errno(epoll_create1(arg1
));
9102 #if defined(TARGET_NR_epoll_ctl)
9103 case TARGET_NR_epoll_ctl
:
9105 struct epoll_event ep
;
9106 struct epoll_event
*epp
= 0;
9108 struct target_epoll_event
*target_ep
;
9109 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
9112 ep
.events
= tswap32(target_ep
->events
);
9113 /* The epoll_data_t union is just opaque data to the kernel,
9114 * so we transfer all 64 bits across and need not worry what
9115 * actual data type it is.
9117 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
9118 unlock_user_struct(target_ep
, arg4
, 0);
9121 ret
= get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
9126 #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9127 #define IMPLEMENT_EPOLL_PWAIT
9129 #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9130 #if defined(TARGET_NR_epoll_wait)
9131 case TARGET_NR_epoll_wait
:
9133 #if defined(IMPLEMENT_EPOLL_PWAIT)
9134 case TARGET_NR_epoll_pwait
:
9137 struct target_epoll_event
*target_ep
;
9138 struct epoll_event
*ep
;
9140 int maxevents
= arg3
;
9143 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
9144 maxevents
* sizeof(struct target_epoll_event
), 1);
9149 ep
= alloca(maxevents
* sizeof(struct epoll_event
));
9152 #if defined(IMPLEMENT_EPOLL_PWAIT)
9153 case TARGET_NR_epoll_pwait
:
9155 target_sigset_t
*target_set
;
9156 sigset_t _set
, *set
= &_set
;
9159 target_set
= lock_user(VERIFY_READ
, arg5
,
9160 sizeof(target_sigset_t
), 1);
9162 unlock_user(target_ep
, arg2
, 0);
9165 target_to_host_sigset(set
, target_set
);
9166 unlock_user(target_set
, arg5
, 0);
9171 ret
= get_errno(epoll_pwait(epfd
, ep
, maxevents
, timeout
, set
));
9175 #if defined(TARGET_NR_epoll_wait)
9176 case TARGET_NR_epoll_wait
:
9177 ret
= get_errno(epoll_wait(epfd
, ep
, maxevents
, timeout
));
9181 ret
= -TARGET_ENOSYS
;
9183 if (!is_error(ret
)) {
9185 for (i
= 0; i
< ret
; i
++) {
9186 target_ep
[i
].events
= tswap32(ep
[i
].events
);
9187 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
9190 unlock_user(target_ep
, arg2
, ret
* sizeof(struct target_epoll_event
));
9195 #ifdef TARGET_NR_prlimit64
9196 case TARGET_NR_prlimit64
:
9198 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9199 struct target_rlimit64
*target_rnew
, *target_rold
;
9200 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
9202 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
9205 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
9206 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
9207 unlock_user_struct(target_rnew
, arg3
, 0);
9211 ret
= get_errno(sys_prlimit64(arg1
, arg2
, rnewp
, arg4
? &rold
: 0));
9212 if (!is_error(ret
) && arg4
) {
9213 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
9216 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
9217 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
9218 unlock_user_struct(target_rold
, arg4
, 1);
9223 #ifdef TARGET_NR_gethostname
9224 case TARGET_NR_gethostname
:
9226 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
9228 ret
= get_errno(gethostname(name
, arg2
));
9229 unlock_user(name
, arg1
, arg2
);
9231 ret
= -TARGET_EFAULT
;
9236 #ifdef TARGET_NR_atomic_cmpxchg_32
9237 case TARGET_NR_atomic_cmpxchg_32
:
9239 /* should use start_exclusive from main.c */
9240 abi_ulong mem_value
;
9241 if (get_user_u32(mem_value
, arg6
)) {
9242 target_siginfo_t info
;
9243 info
.si_signo
= SIGSEGV
;
9245 info
.si_code
= TARGET_SEGV_MAPERR
;
9246 info
._sifields
._sigfault
._addr
= arg6
;
9247 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
9251 if (mem_value
== arg2
)
9252 put_user_u32(arg1
, arg6
);
9257 #ifdef TARGET_NR_atomic_barrier
9258 case TARGET_NR_atomic_barrier
:
9260 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
9266 #ifdef TARGET_NR_timer_create
9267 case TARGET_NR_timer_create
:
9269 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
9271 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
9272 struct target_sigevent
*ptarget_sevp
;
9273 struct target_timer_t
*ptarget_timer
;
9276 int timer_index
= next_free_host_timer();
9278 if (timer_index
< 0) {
9279 ret
= -TARGET_EAGAIN
;
9281 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
9284 if (!lock_user_struct(VERIFY_READ
, ptarget_sevp
, arg2
, 1)) {
9288 host_sevp
.sigev_signo
= tswap32(ptarget_sevp
->sigev_signo
);
9289 host_sevp
.sigev_notify
= tswap32(ptarget_sevp
->sigev_notify
);
9291 phost_sevp
= &host_sevp
;
9294 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
9298 if (!lock_user_struct(VERIFY_WRITE
, ptarget_timer
, arg3
, 1)) {
9301 ptarget_timer
->ptr
= tswap32(0xcafe0000 | timer_index
);
9302 unlock_user_struct(ptarget_timer
, arg3
, 1);
9309 #ifdef TARGET_NR_timer_settime
9310 case TARGET_NR_timer_settime
:
9312 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
9313 * struct itimerspec * old_value */
9315 if (arg3
== 0 || arg1
< 0 || arg1
>= ARRAY_SIZE(g_posix_timers
)) {
9316 ret
= -TARGET_EINVAL
;
9318 timer_t htimer
= g_posix_timers
[arg1
];
9319 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
9321 target_to_host_itimerspec(&hspec_new
, arg3
);
9323 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
9324 host_to_target_itimerspec(arg2
, &hspec_old
);
9330 #ifdef TARGET_NR_timer_gettime
9331 case TARGET_NR_timer_gettime
:
9333 /* args: timer_t timerid, struct itimerspec *curr_value */
9336 return -TARGET_EFAULT
;
9337 } else if (arg1
< 0 || arg1
>= ARRAY_SIZE(g_posix_timers
)) {
9338 ret
= -TARGET_EINVAL
;
9340 timer_t htimer
= g_posix_timers
[arg1
];
9341 struct itimerspec hspec
;
9342 ret
= get_errno(timer_gettime(htimer
, &hspec
));
9344 if (host_to_target_itimerspec(arg2
, &hspec
)) {
9345 ret
= -TARGET_EFAULT
;
9352 #ifdef TARGET_NR_timer_getoverrun
9353 case TARGET_NR_timer_getoverrun
:
9355 /* args: timer_t timerid */
9357 if (arg1
< 0 || arg1
>= ARRAY_SIZE(g_posix_timers
)) {
9358 ret
= -TARGET_EINVAL
;
9360 timer_t htimer
= g_posix_timers
[arg1
];
9361 ret
= get_errno(timer_getoverrun(htimer
));
9367 #ifdef TARGET_NR_timer_delete
9368 case TARGET_NR_timer_delete
:
9370 /* args: timer_t timerid */
9372 if (arg1
< 0 || arg1
>= ARRAY_SIZE(g_posix_timers
)) {
9373 ret
= -TARGET_EINVAL
;
9375 timer_t htimer
= g_posix_timers
[arg1
];
9376 ret
= get_errno(timer_delete(htimer
));
9377 g_posix_timers
[arg1
] = 0;
9385 gemu_log("qemu: Unsupported syscall: %d\n", num
);
9386 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
9387 unimplemented_nowarn
:
9389 ret
= -TARGET_ENOSYS
;
9394 gemu_log(" = " TARGET_ABI_FMT_ld
"\n", ret
);
9397 print_syscall_ret(num
, ret
);
9400 ret
= -TARGET_EFAULT
;