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>
61 #include <sys/timerfd.h>
63 #include <sys/sysinfo.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/cdrom.h>
96 #include <linux/hdreg.h>
97 #include <linux/soundcard.h>
99 #include <linux/mtio.h>
100 #include <linux/fs.h>
101 #if defined(CONFIG_FIEMAP)
102 #include <linux/fiemap.h>
104 #include <linux/fb.h>
105 #include <linux/vt.h>
106 #include <linux/dm-ioctl.h>
107 #include <linux/reboot.h>
108 #include <linux/route.h>
109 #include <linux/filter.h>
110 #include <linux/blkpg.h>
111 #include "linux_loop.h"
116 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
117 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
121 //#include <linux/msdos_fs.h>
122 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
123 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
134 #define _syscall0(type,name) \
135 static type name (void) \
137 return syscall(__NR_##name); \
140 #define _syscall1(type,name,type1,arg1) \
141 static type name (type1 arg1) \
143 return syscall(__NR_##name, arg1); \
146 #define _syscall2(type,name,type1,arg1,type2,arg2) \
147 static type name (type1 arg1,type2 arg2) \
149 return syscall(__NR_##name, arg1, arg2); \
152 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
153 static type name (type1 arg1,type2 arg2,type3 arg3) \
155 return syscall(__NR_##name, arg1, arg2, arg3); \
158 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
159 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
161 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
164 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
166 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
168 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
172 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
173 type5,arg5,type6,arg6) \
174 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
177 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
181 #define __NR_sys_uname __NR_uname
182 #define __NR_sys_getcwd1 __NR_getcwd
183 #define __NR_sys_getdents __NR_getdents
184 #define __NR_sys_getdents64 __NR_getdents64
185 #define __NR_sys_getpriority __NR_getpriority
186 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
187 #define __NR_sys_syslog __NR_syslog
188 #define __NR_sys_tgkill __NR_tgkill
189 #define __NR_sys_tkill __NR_tkill
190 #define __NR_sys_futex __NR_futex
191 #define __NR_sys_inotify_init __NR_inotify_init
192 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
193 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
195 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
197 #define __NR__llseek __NR_lseek
200 /* Newer kernel ports have llseek() instead of _llseek() */
201 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
202 #define TARGET_NR__llseek TARGET_NR_llseek
206 _syscall0(int, gettid
)
208 /* This is a replacement for the host gettid() and must return a host
210 static int gettid(void) {
215 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
217 #if !defined(__NR_getdents) || \
218 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
219 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
221 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
222 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
223 loff_t
*, res
, uint
, wh
);
225 _syscall3(int,sys_rt_sigqueueinfo
,int,pid
,int,sig
,siginfo_t
*,uinfo
)
226 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
227 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
228 _syscall3(int,sys_tgkill
,int,tgid
,int,pid
,int,sig
)
230 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
231 _syscall2(int,sys_tkill
,int,tid
,int,sig
)
233 #ifdef __NR_exit_group
234 _syscall1(int,exit_group
,int,error_code
)
236 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
237 _syscall1(int,set_tid_address
,int *,tidptr
)
239 #if defined(TARGET_NR_futex) && defined(__NR_futex)
240 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
241 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
243 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
244 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
245 unsigned long *, user_mask_ptr
);
246 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
247 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
248 unsigned long *, user_mask_ptr
);
249 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
251 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
252 struct __user_cap_data_struct
*, data
);
253 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
254 struct __user_cap_data_struct
*, data
);
255 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
256 _syscall2(int, ioprio_get
, int, which
, int, who
)
258 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
259 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
262 static bitmask_transtbl fcntl_flags_tbl
[] = {
263 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
264 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
265 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
266 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
267 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
268 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
269 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
270 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
271 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
272 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
273 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
274 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
275 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
276 #if defined(O_DIRECT)
277 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
279 #if defined(O_NOATIME)
280 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
282 #if defined(O_CLOEXEC)
283 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
286 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
288 /* Don't terminate the list prematurely on 64-bit host+guest. */
289 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
290 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
295 static int sys_getcwd1(char *buf
, size_t size
)
297 if (getcwd(buf
, size
) == NULL
) {
298 /* getcwd() sets errno */
301 return strlen(buf
)+1;
304 static int sys_openat(int dirfd
, const char *pathname
, int flags
, mode_t mode
)
307 * open(2) has extra parameter 'mode' when called with
310 if ((flags
& O_CREAT
) != 0) {
311 return (openat(dirfd
, pathname
, flags
, mode
));
313 return (openat(dirfd
, pathname
, flags
));
316 #ifdef TARGET_NR_utimensat
317 #ifdef CONFIG_UTIMENSAT
318 static int sys_utimensat(int dirfd
, const char *pathname
,
319 const struct timespec times
[2], int flags
)
321 if (pathname
== NULL
)
322 return futimens(dirfd
, times
);
324 return utimensat(dirfd
, pathname
, times
, flags
);
326 #elif defined(__NR_utimensat)
327 #define __NR_sys_utimensat __NR_utimensat
328 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
329 const struct timespec
*,tsp
,int,flags
)
331 static int sys_utimensat(int dirfd
, const char *pathname
,
332 const struct timespec times
[2], int flags
)
338 #endif /* TARGET_NR_utimensat */
340 #ifdef CONFIG_INOTIFY
341 #include <sys/inotify.h>
343 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
344 static int sys_inotify_init(void)
346 return (inotify_init());
349 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
350 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
352 return (inotify_add_watch(fd
, pathname
, mask
));
355 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
356 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
358 return (inotify_rm_watch(fd
, wd
));
361 #ifdef CONFIG_INOTIFY1
362 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
363 static int sys_inotify_init1(int flags
)
365 return (inotify_init1(flags
));
370 /* Userspace can usually survive runtime without inotify */
371 #undef TARGET_NR_inotify_init
372 #undef TARGET_NR_inotify_init1
373 #undef TARGET_NR_inotify_add_watch
374 #undef TARGET_NR_inotify_rm_watch
375 #endif /* CONFIG_INOTIFY */
377 #if defined(TARGET_NR_ppoll)
379 # define __NR_ppoll -1
381 #define __NR_sys_ppoll __NR_ppoll
382 _syscall5(int, sys_ppoll
, struct pollfd
*, fds
, nfds_t
, nfds
,
383 struct timespec
*, timeout
, const sigset_t
*, sigmask
,
387 #if defined(TARGET_NR_pselect6)
388 #ifndef __NR_pselect6
389 # define __NR_pselect6 -1
391 #define __NR_sys_pselect6 __NR_pselect6
392 _syscall6(int, sys_pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
,
393 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
);
396 #if defined(TARGET_NR_prlimit64)
397 #ifndef __NR_prlimit64
398 # define __NR_prlimit64 -1
400 #define __NR_sys_prlimit64 __NR_prlimit64
401 /* The glibc rlimit structure may not be that used by the underlying syscall */
402 struct host_rlimit64
{
406 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
407 const struct host_rlimit64
*, new_limit
,
408 struct host_rlimit64
*, old_limit
)
412 #if defined(TARGET_NR_timer_create)
413 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
414 static timer_t g_posix_timers
[32] = { 0, } ;
416 static inline int next_free_host_timer(void)
419 /* FIXME: Does finding the next free slot require a lock? */
420 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
421 if (g_posix_timers
[k
] == 0) {
422 g_posix_timers
[k
] = (timer_t
) 1;
430 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
432 static inline int regpairs_aligned(void *cpu_env
) {
433 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
435 #elif defined(TARGET_MIPS)
436 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
437 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
438 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
439 * of registers which translates to the same as ARM/MIPS, because we start with
441 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
443 static inline int regpairs_aligned(void *cpu_env
) { return 0; }
446 #define ERRNO_TABLE_SIZE 1200
448 /* target_to_host_errno_table[] is initialized from
449 * host_to_target_errno_table[] in syscall_init(). */
450 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
454 * This list is the union of errno values overridden in asm-<arch>/errno.h
455 * minus the errnos that are not actually generic to all archs.
457 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
458 [EIDRM
] = TARGET_EIDRM
,
459 [ECHRNG
] = TARGET_ECHRNG
,
460 [EL2NSYNC
] = TARGET_EL2NSYNC
,
461 [EL3HLT
] = TARGET_EL3HLT
,
462 [EL3RST
] = TARGET_EL3RST
,
463 [ELNRNG
] = TARGET_ELNRNG
,
464 [EUNATCH
] = TARGET_EUNATCH
,
465 [ENOCSI
] = TARGET_ENOCSI
,
466 [EL2HLT
] = TARGET_EL2HLT
,
467 [EDEADLK
] = TARGET_EDEADLK
,
468 [ENOLCK
] = TARGET_ENOLCK
,
469 [EBADE
] = TARGET_EBADE
,
470 [EBADR
] = TARGET_EBADR
,
471 [EXFULL
] = TARGET_EXFULL
,
472 [ENOANO
] = TARGET_ENOANO
,
473 [EBADRQC
] = TARGET_EBADRQC
,
474 [EBADSLT
] = TARGET_EBADSLT
,
475 [EBFONT
] = TARGET_EBFONT
,
476 [ENOSTR
] = TARGET_ENOSTR
,
477 [ENODATA
] = TARGET_ENODATA
,
478 [ETIME
] = TARGET_ETIME
,
479 [ENOSR
] = TARGET_ENOSR
,
480 [ENONET
] = TARGET_ENONET
,
481 [ENOPKG
] = TARGET_ENOPKG
,
482 [EREMOTE
] = TARGET_EREMOTE
,
483 [ENOLINK
] = TARGET_ENOLINK
,
484 [EADV
] = TARGET_EADV
,
485 [ESRMNT
] = TARGET_ESRMNT
,
486 [ECOMM
] = TARGET_ECOMM
,
487 [EPROTO
] = TARGET_EPROTO
,
488 [EDOTDOT
] = TARGET_EDOTDOT
,
489 [EMULTIHOP
] = TARGET_EMULTIHOP
,
490 [EBADMSG
] = TARGET_EBADMSG
,
491 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
492 [EOVERFLOW
] = TARGET_EOVERFLOW
,
493 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
494 [EBADFD
] = TARGET_EBADFD
,
495 [EREMCHG
] = TARGET_EREMCHG
,
496 [ELIBACC
] = TARGET_ELIBACC
,
497 [ELIBBAD
] = TARGET_ELIBBAD
,
498 [ELIBSCN
] = TARGET_ELIBSCN
,
499 [ELIBMAX
] = TARGET_ELIBMAX
,
500 [ELIBEXEC
] = TARGET_ELIBEXEC
,
501 [EILSEQ
] = TARGET_EILSEQ
,
502 [ENOSYS
] = TARGET_ENOSYS
,
503 [ELOOP
] = TARGET_ELOOP
,
504 [ERESTART
] = TARGET_ERESTART
,
505 [ESTRPIPE
] = TARGET_ESTRPIPE
,
506 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
507 [EUSERS
] = TARGET_EUSERS
,
508 [ENOTSOCK
] = TARGET_ENOTSOCK
,
509 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
510 [EMSGSIZE
] = TARGET_EMSGSIZE
,
511 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
512 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
513 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
514 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
515 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
516 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
517 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
518 [EADDRINUSE
] = TARGET_EADDRINUSE
,
519 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
520 [ENETDOWN
] = TARGET_ENETDOWN
,
521 [ENETUNREACH
] = TARGET_ENETUNREACH
,
522 [ENETRESET
] = TARGET_ENETRESET
,
523 [ECONNABORTED
] = TARGET_ECONNABORTED
,
524 [ECONNRESET
] = TARGET_ECONNRESET
,
525 [ENOBUFS
] = TARGET_ENOBUFS
,
526 [EISCONN
] = TARGET_EISCONN
,
527 [ENOTCONN
] = TARGET_ENOTCONN
,
528 [EUCLEAN
] = TARGET_EUCLEAN
,
529 [ENOTNAM
] = TARGET_ENOTNAM
,
530 [ENAVAIL
] = TARGET_ENAVAIL
,
531 [EISNAM
] = TARGET_EISNAM
,
532 [EREMOTEIO
] = TARGET_EREMOTEIO
,
533 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
534 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
535 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
536 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
537 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
538 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
539 [EALREADY
] = TARGET_EALREADY
,
540 [EINPROGRESS
] = TARGET_EINPROGRESS
,
541 [ESTALE
] = TARGET_ESTALE
,
542 [ECANCELED
] = TARGET_ECANCELED
,
543 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
544 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
546 [ENOKEY
] = TARGET_ENOKEY
,
549 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
552 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
555 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
558 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
560 #ifdef ENOTRECOVERABLE
561 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
565 static inline int host_to_target_errno(int err
)
567 if(host_to_target_errno_table
[err
])
568 return host_to_target_errno_table
[err
];
572 static inline int target_to_host_errno(int err
)
574 if (target_to_host_errno_table
[err
])
575 return target_to_host_errno_table
[err
];
579 static inline abi_long
get_errno(abi_long ret
)
582 return -host_to_target_errno(errno
);
587 static inline int is_error(abi_long ret
)
589 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
592 char *target_strerror(int err
)
594 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
597 return strerror(target_to_host_errno(err
));
600 static inline int host_to_target_sock_type(int host_type
)
604 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
606 target_type
= TARGET_SOCK_DGRAM
;
609 target_type
= TARGET_SOCK_STREAM
;
612 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
616 #if defined(SOCK_CLOEXEC)
617 if (host_type
& SOCK_CLOEXEC
) {
618 target_type
|= TARGET_SOCK_CLOEXEC
;
622 #if defined(SOCK_NONBLOCK)
623 if (host_type
& SOCK_NONBLOCK
) {
624 target_type
|= TARGET_SOCK_NONBLOCK
;
631 static abi_ulong target_brk
;
632 static abi_ulong target_original_brk
;
633 static abi_ulong brk_page
;
635 void target_set_brk(abi_ulong new_brk
)
637 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
638 brk_page
= HOST_PAGE_ALIGN(target_brk
);
641 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
642 #define DEBUGF_BRK(message, args...)
644 /* do_brk() must return target values and target errnos. */
645 abi_long
do_brk(abi_ulong new_brk
)
647 abi_long mapped_addr
;
650 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
653 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
656 if (new_brk
< target_original_brk
) {
657 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
662 /* If the new brk is less than the highest page reserved to the
663 * target heap allocation, set it and we're almost done... */
664 if (new_brk
<= brk_page
) {
665 /* Heap contents are initialized to zero, as for anonymous
667 if (new_brk
> target_brk
) {
668 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
670 target_brk
= new_brk
;
671 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
675 /* We need to allocate more memory after the brk... Note that
676 * we don't use MAP_FIXED because that will map over the top of
677 * any existing mapping (like the one with the host libc or qemu
678 * itself); instead we treat "mapped but at wrong address" as
679 * a failure and unmap again.
681 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
682 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
683 PROT_READ
|PROT_WRITE
,
684 MAP_ANON
|MAP_PRIVATE
, 0, 0));
686 if (mapped_addr
== brk_page
) {
687 /* Heap contents are initialized to zero, as for anonymous
688 * mapped pages. Technically the new pages are already
689 * initialized to zero since they *are* anonymous mapped
690 * pages, however we have to take care with the contents that
691 * come from the remaining part of the previous page: it may
692 * contains garbage data due to a previous heap usage (grown
694 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
696 target_brk
= new_brk
;
697 brk_page
= HOST_PAGE_ALIGN(target_brk
);
698 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
701 } else if (mapped_addr
!= -1) {
702 /* Mapped but at wrong address, meaning there wasn't actually
703 * enough space for this brk.
705 target_munmap(mapped_addr
, new_alloc_size
);
707 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
710 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
713 #if defined(TARGET_ALPHA)
714 /* We (partially) emulate OSF/1 on Alpha, which requires we
715 return a proper errno, not an unchanged brk value. */
716 return -TARGET_ENOMEM
;
718 /* For everything else, return the previous break. */
722 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
723 abi_ulong target_fds_addr
,
727 abi_ulong b
, *target_fds
;
729 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
730 if (!(target_fds
= lock_user(VERIFY_READ
,
732 sizeof(abi_ulong
) * nw
,
734 return -TARGET_EFAULT
;
738 for (i
= 0; i
< nw
; i
++) {
739 /* grab the abi_ulong */
740 __get_user(b
, &target_fds
[i
]);
741 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
742 /* check the bit inside the abi_ulong */
749 unlock_user(target_fds
, target_fds_addr
, 0);
754 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
755 abi_ulong target_fds_addr
,
758 if (target_fds_addr
) {
759 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
760 return -TARGET_EFAULT
;
768 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
774 abi_ulong
*target_fds
;
776 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
777 if (!(target_fds
= lock_user(VERIFY_WRITE
,
779 sizeof(abi_ulong
) * nw
,
781 return -TARGET_EFAULT
;
784 for (i
= 0; i
< nw
; i
++) {
786 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
787 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
790 __put_user(v
, &target_fds
[i
]);
793 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
798 #if defined(__alpha__)
804 static inline abi_long
host_to_target_clock_t(long ticks
)
806 #if HOST_HZ == TARGET_HZ
809 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
813 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
814 const struct rusage
*rusage
)
816 struct target_rusage
*target_rusage
;
818 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
819 return -TARGET_EFAULT
;
820 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
821 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
822 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
823 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
824 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
825 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
826 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
827 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
828 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
829 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
830 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
831 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
832 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
833 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
834 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
835 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
836 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
837 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
838 unlock_user_struct(target_rusage
, target_addr
, 1);
843 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
845 abi_ulong target_rlim_swap
;
848 target_rlim_swap
= tswapal(target_rlim
);
849 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
850 return RLIM_INFINITY
;
852 result
= target_rlim_swap
;
853 if (target_rlim_swap
!= (rlim_t
)result
)
854 return RLIM_INFINITY
;
859 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
861 abi_ulong target_rlim_swap
;
864 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
865 target_rlim_swap
= TARGET_RLIM_INFINITY
;
867 target_rlim_swap
= rlim
;
868 result
= tswapal(target_rlim_swap
);
873 static inline int target_to_host_resource(int code
)
876 case TARGET_RLIMIT_AS
:
878 case TARGET_RLIMIT_CORE
:
880 case TARGET_RLIMIT_CPU
:
882 case TARGET_RLIMIT_DATA
:
884 case TARGET_RLIMIT_FSIZE
:
886 case TARGET_RLIMIT_LOCKS
:
888 case TARGET_RLIMIT_MEMLOCK
:
889 return RLIMIT_MEMLOCK
;
890 case TARGET_RLIMIT_MSGQUEUE
:
891 return RLIMIT_MSGQUEUE
;
892 case TARGET_RLIMIT_NICE
:
894 case TARGET_RLIMIT_NOFILE
:
895 return RLIMIT_NOFILE
;
896 case TARGET_RLIMIT_NPROC
:
898 case TARGET_RLIMIT_RSS
:
900 case TARGET_RLIMIT_RTPRIO
:
901 return RLIMIT_RTPRIO
;
902 case TARGET_RLIMIT_SIGPENDING
:
903 return RLIMIT_SIGPENDING
;
904 case TARGET_RLIMIT_STACK
:
911 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
912 abi_ulong target_tv_addr
)
914 struct target_timeval
*target_tv
;
916 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
917 return -TARGET_EFAULT
;
919 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
920 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
922 unlock_user_struct(target_tv
, target_tv_addr
, 0);
927 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
928 const struct timeval
*tv
)
930 struct target_timeval
*target_tv
;
932 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
933 return -TARGET_EFAULT
;
935 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
936 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
938 unlock_user_struct(target_tv
, target_tv_addr
, 1);
943 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
944 abi_ulong target_tz_addr
)
946 struct target_timezone
*target_tz
;
948 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
949 return -TARGET_EFAULT
;
952 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
953 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
955 unlock_user_struct(target_tz
, target_tz_addr
, 0);
960 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
963 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
964 abi_ulong target_mq_attr_addr
)
966 struct target_mq_attr
*target_mq_attr
;
968 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
969 target_mq_attr_addr
, 1))
970 return -TARGET_EFAULT
;
972 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
973 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
974 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
975 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
977 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
982 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
983 const struct mq_attr
*attr
)
985 struct target_mq_attr
*target_mq_attr
;
987 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
988 target_mq_attr_addr
, 0))
989 return -TARGET_EFAULT
;
991 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
992 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
993 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
994 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
996 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1002 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1003 /* do_select() must return target values and target errnos. */
1004 static abi_long
do_select(int n
,
1005 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1006 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1008 fd_set rfds
, wfds
, efds
;
1009 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1010 struct timeval tv
, *tv_ptr
;
1013 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1017 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1021 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1026 if (target_tv_addr
) {
1027 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1028 return -TARGET_EFAULT
;
1034 ret
= get_errno(select(n
, rfds_ptr
, wfds_ptr
, efds_ptr
, tv_ptr
));
1036 if (!is_error(ret
)) {
1037 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1038 return -TARGET_EFAULT
;
1039 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1040 return -TARGET_EFAULT
;
1041 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1042 return -TARGET_EFAULT
;
1044 if (target_tv_addr
&& copy_to_user_timeval(target_tv_addr
, &tv
))
1045 return -TARGET_EFAULT
;
1052 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1055 return pipe2(host_pipe
, flags
);
1061 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1062 int flags
, int is_pipe2
)
1066 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1069 return get_errno(ret
);
1071 /* Several targets have special calling conventions for the original
1072 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1074 #if defined(TARGET_ALPHA)
1075 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1076 return host_pipe
[0];
1077 #elif defined(TARGET_MIPS)
1078 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1079 return host_pipe
[0];
1080 #elif defined(TARGET_SH4)
1081 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1082 return host_pipe
[0];
1083 #elif defined(TARGET_SPARC)
1084 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1085 return host_pipe
[0];
1089 if (put_user_s32(host_pipe
[0], pipedes
)
1090 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1091 return -TARGET_EFAULT
;
1092 return get_errno(ret
);
1095 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1096 abi_ulong target_addr
,
1099 struct target_ip_mreqn
*target_smreqn
;
1101 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1103 return -TARGET_EFAULT
;
1104 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1105 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1106 if (len
== sizeof(struct target_ip_mreqn
))
1107 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1108 unlock_user(target_smreqn
, target_addr
, 0);
1113 static inline abi_long
target_to_host_sockaddr(struct sockaddr
*addr
,
1114 abi_ulong target_addr
,
1117 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1118 sa_family_t sa_family
;
1119 struct target_sockaddr
*target_saddr
;
1121 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1123 return -TARGET_EFAULT
;
1125 sa_family
= tswap16(target_saddr
->sa_family
);
1127 /* Oops. The caller might send a incomplete sun_path; sun_path
1128 * must be terminated by \0 (see the manual page), but
1129 * unfortunately it is quite common to specify sockaddr_un
1130 * length as "strlen(x->sun_path)" while it should be
1131 * "strlen(...) + 1". We'll fix that here if needed.
1132 * Linux kernel has a similar feature.
1135 if (sa_family
== AF_UNIX
) {
1136 if (len
< unix_maxlen
&& len
> 0) {
1137 char *cp
= (char*)target_saddr
;
1139 if ( cp
[len
-1] && !cp
[len
] )
1142 if (len
> unix_maxlen
)
1146 memcpy(addr
, target_saddr
, len
);
1147 addr
->sa_family
= sa_family
;
1148 if (sa_family
== AF_PACKET
) {
1149 struct target_sockaddr_ll
*lladdr
;
1151 lladdr
= (struct target_sockaddr_ll
*)addr
;
1152 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1153 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1155 unlock_user(target_saddr
, target_addr
, 0);
1160 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1161 struct sockaddr
*addr
,
1164 struct target_sockaddr
*target_saddr
;
1166 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1168 return -TARGET_EFAULT
;
1169 memcpy(target_saddr
, addr
, len
);
1170 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1171 unlock_user(target_saddr
, target_addr
, len
);
1176 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1177 struct target_msghdr
*target_msgh
)
1179 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1180 abi_long msg_controllen
;
1181 abi_ulong target_cmsg_addr
;
1182 struct target_cmsghdr
*target_cmsg
;
1183 socklen_t space
= 0;
1185 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1186 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1188 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1189 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1191 return -TARGET_EFAULT
;
1193 while (cmsg
&& target_cmsg
) {
1194 void *data
= CMSG_DATA(cmsg
);
1195 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1197 int len
= tswapal(target_cmsg
->cmsg_len
)
1198 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr
));
1200 space
+= CMSG_SPACE(len
);
1201 if (space
> msgh
->msg_controllen
) {
1202 space
-= CMSG_SPACE(len
);
1203 gemu_log("Host cmsg overflow\n");
1207 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1208 cmsg
->cmsg_level
= SOL_SOCKET
;
1210 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1212 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1213 cmsg
->cmsg_len
= CMSG_LEN(len
);
1215 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
1216 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1217 memcpy(data
, target_data
, len
);
1219 int *fd
= (int *)data
;
1220 int *target_fd
= (int *)target_data
;
1221 int i
, numfds
= len
/ sizeof(int);
1223 for (i
= 0; i
< numfds
; i
++)
1224 fd
[i
] = tswap32(target_fd
[i
]);
1227 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1228 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
);
1230 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1232 msgh
->msg_controllen
= space
;
1236 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1237 struct msghdr
*msgh
)
1239 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1240 abi_long msg_controllen
;
1241 abi_ulong target_cmsg_addr
;
1242 struct target_cmsghdr
*target_cmsg
;
1243 socklen_t space
= 0;
1245 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1246 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1248 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1249 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1251 return -TARGET_EFAULT
;
1253 while (cmsg
&& target_cmsg
) {
1254 void *data
= CMSG_DATA(cmsg
);
1255 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1257 int len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof (struct cmsghdr
));
1259 space
+= TARGET_CMSG_SPACE(len
);
1260 if (space
> msg_controllen
) {
1261 space
-= TARGET_CMSG_SPACE(len
);
1262 gemu_log("Target cmsg overflow\n");
1266 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1267 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1269 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1271 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1272 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(len
));
1274 switch (cmsg
->cmsg_level
) {
1276 switch (cmsg
->cmsg_type
) {
1279 int *fd
= (int *)data
;
1280 int *target_fd
= (int *)target_data
;
1281 int i
, numfds
= len
/ sizeof(int);
1283 for (i
= 0; i
< numfds
; i
++)
1284 target_fd
[i
] = tswap32(fd
[i
]);
1289 struct timeval
*tv
= (struct timeval
*)data
;
1290 struct target_timeval
*target_tv
=
1291 (struct target_timeval
*)target_data
;
1293 if (len
!= sizeof(struct timeval
))
1296 /* copy struct timeval to target */
1297 target_tv
->tv_sec
= tswapal(tv
->tv_sec
);
1298 target_tv
->tv_usec
= tswapal(tv
->tv_usec
);
1301 case SCM_CREDENTIALS
:
1303 struct ucred
*cred
= (struct ucred
*)data
;
1304 struct target_ucred
*target_cred
=
1305 (struct target_ucred
*)target_data
;
1307 __put_user(cred
->pid
, &target_cred
->pid
);
1308 __put_user(cred
->uid
, &target_cred
->uid
);
1309 __put_user(cred
->gid
, &target_cred
->gid
);
1319 gemu_log("Unsupported ancillary data: %d/%d\n",
1320 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1321 memcpy(target_data
, data
, len
);
1324 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1325 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
);
1327 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1329 target_msgh
->msg_controllen
= tswapal(space
);
1333 /* do_setsockopt() Must return target values and target errnos. */
1334 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1335 abi_ulong optval_addr
, socklen_t optlen
)
1339 struct ip_mreqn
*ip_mreq
;
1340 struct ip_mreq_source
*ip_mreq_source
;
1344 /* TCP options all take an 'int' value. */
1345 if (optlen
< sizeof(uint32_t))
1346 return -TARGET_EINVAL
;
1348 if (get_user_u32(val
, optval_addr
))
1349 return -TARGET_EFAULT
;
1350 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1357 case IP_ROUTER_ALERT
:
1361 case IP_MTU_DISCOVER
:
1367 case IP_MULTICAST_TTL
:
1368 case IP_MULTICAST_LOOP
:
1370 if (optlen
>= sizeof(uint32_t)) {
1371 if (get_user_u32(val
, optval_addr
))
1372 return -TARGET_EFAULT
;
1373 } else if (optlen
>= 1) {
1374 if (get_user_u8(val
, optval_addr
))
1375 return -TARGET_EFAULT
;
1377 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1379 case IP_ADD_MEMBERSHIP
:
1380 case IP_DROP_MEMBERSHIP
:
1381 if (optlen
< sizeof (struct target_ip_mreq
) ||
1382 optlen
> sizeof (struct target_ip_mreqn
))
1383 return -TARGET_EINVAL
;
1385 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1386 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1387 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1390 case IP_BLOCK_SOURCE
:
1391 case IP_UNBLOCK_SOURCE
:
1392 case IP_ADD_SOURCE_MEMBERSHIP
:
1393 case IP_DROP_SOURCE_MEMBERSHIP
:
1394 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1395 return -TARGET_EINVAL
;
1397 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1398 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1399 unlock_user (ip_mreq_source
, optval_addr
, 0);
1408 case IPV6_MTU_DISCOVER
:
1411 case IPV6_RECVPKTINFO
:
1413 if (optlen
< sizeof(uint32_t)) {
1414 return -TARGET_EINVAL
;
1416 if (get_user_u32(val
, optval_addr
)) {
1417 return -TARGET_EFAULT
;
1419 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1420 &val
, sizeof(val
)));
1429 /* struct icmp_filter takes an u32 value */
1430 if (optlen
< sizeof(uint32_t)) {
1431 return -TARGET_EINVAL
;
1434 if (get_user_u32(val
, optval_addr
)) {
1435 return -TARGET_EFAULT
;
1437 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1438 &val
, sizeof(val
)));
1445 case TARGET_SOL_SOCKET
:
1447 case TARGET_SO_RCVTIMEO
:
1451 optname
= SO_RCVTIMEO
;
1454 if (optlen
!= sizeof(struct target_timeval
)) {
1455 return -TARGET_EINVAL
;
1458 if (copy_from_user_timeval(&tv
, optval_addr
)) {
1459 return -TARGET_EFAULT
;
1462 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
1466 case TARGET_SO_SNDTIMEO
:
1467 optname
= SO_SNDTIMEO
;
1469 case TARGET_SO_ATTACH_FILTER
:
1471 struct target_sock_fprog
*tfprog
;
1472 struct target_sock_filter
*tfilter
;
1473 struct sock_fprog fprog
;
1474 struct sock_filter
*filter
;
1477 if (optlen
!= sizeof(*tfprog
)) {
1478 return -TARGET_EINVAL
;
1480 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
1481 return -TARGET_EFAULT
;
1483 if (!lock_user_struct(VERIFY_READ
, tfilter
,
1484 tswapal(tfprog
->filter
), 0)) {
1485 unlock_user_struct(tfprog
, optval_addr
, 1);
1486 return -TARGET_EFAULT
;
1489 fprog
.len
= tswap16(tfprog
->len
);
1490 filter
= malloc(fprog
.len
* sizeof(*filter
));
1491 if (filter
== NULL
) {
1492 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1493 unlock_user_struct(tfprog
, optval_addr
, 1);
1494 return -TARGET_ENOMEM
;
1496 for (i
= 0; i
< fprog
.len
; i
++) {
1497 filter
[i
].code
= tswap16(tfilter
[i
].code
);
1498 filter
[i
].jt
= tfilter
[i
].jt
;
1499 filter
[i
].jf
= tfilter
[i
].jf
;
1500 filter
[i
].k
= tswap32(tfilter
[i
].k
);
1502 fprog
.filter
= filter
;
1504 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
1505 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
1508 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1509 unlock_user_struct(tfprog
, optval_addr
, 1);
1512 case TARGET_SO_BINDTODEVICE
:
1514 char *dev_ifname
, *addr_ifname
;
1516 if (optlen
> IFNAMSIZ
- 1) {
1517 optlen
= IFNAMSIZ
- 1;
1519 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1521 return -TARGET_EFAULT
;
1523 optname
= SO_BINDTODEVICE
;
1524 addr_ifname
= alloca(IFNAMSIZ
);
1525 memcpy(addr_ifname
, dev_ifname
, optlen
);
1526 addr_ifname
[optlen
] = 0;
1527 ret
= get_errno(setsockopt(sockfd
, level
, optname
, addr_ifname
, optlen
));
1528 unlock_user (dev_ifname
, optval_addr
, 0);
1531 /* Options with 'int' argument. */
1532 case TARGET_SO_DEBUG
:
1535 case TARGET_SO_REUSEADDR
:
1536 optname
= SO_REUSEADDR
;
1538 case TARGET_SO_TYPE
:
1541 case TARGET_SO_ERROR
:
1544 case TARGET_SO_DONTROUTE
:
1545 optname
= SO_DONTROUTE
;
1547 case TARGET_SO_BROADCAST
:
1548 optname
= SO_BROADCAST
;
1550 case TARGET_SO_SNDBUF
:
1551 optname
= SO_SNDBUF
;
1553 case TARGET_SO_SNDBUFFORCE
:
1554 optname
= SO_SNDBUFFORCE
;
1556 case TARGET_SO_RCVBUF
:
1557 optname
= SO_RCVBUF
;
1559 case TARGET_SO_RCVBUFFORCE
:
1560 optname
= SO_RCVBUFFORCE
;
1562 case TARGET_SO_KEEPALIVE
:
1563 optname
= SO_KEEPALIVE
;
1565 case TARGET_SO_OOBINLINE
:
1566 optname
= SO_OOBINLINE
;
1568 case TARGET_SO_NO_CHECK
:
1569 optname
= SO_NO_CHECK
;
1571 case TARGET_SO_PRIORITY
:
1572 optname
= SO_PRIORITY
;
1575 case TARGET_SO_BSDCOMPAT
:
1576 optname
= SO_BSDCOMPAT
;
1579 case TARGET_SO_PASSCRED
:
1580 optname
= SO_PASSCRED
;
1582 case TARGET_SO_PASSSEC
:
1583 optname
= SO_PASSSEC
;
1585 case TARGET_SO_TIMESTAMP
:
1586 optname
= SO_TIMESTAMP
;
1588 case TARGET_SO_RCVLOWAT
:
1589 optname
= SO_RCVLOWAT
;
1595 if (optlen
< sizeof(uint32_t))
1596 return -TARGET_EINVAL
;
1598 if (get_user_u32(val
, optval_addr
))
1599 return -TARGET_EFAULT
;
1600 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
1604 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level
, optname
);
1605 ret
= -TARGET_ENOPROTOOPT
;
1610 /* do_getsockopt() Must return target values and target errnos. */
1611 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
1612 abi_ulong optval_addr
, abi_ulong optlen
)
1619 case TARGET_SOL_SOCKET
:
1622 /* These don't just return a single integer */
1623 case TARGET_SO_LINGER
:
1624 case TARGET_SO_RCVTIMEO
:
1625 case TARGET_SO_SNDTIMEO
:
1626 case TARGET_SO_PEERNAME
:
1628 case TARGET_SO_PEERCRED
: {
1631 struct target_ucred
*tcr
;
1633 if (get_user_u32(len
, optlen
)) {
1634 return -TARGET_EFAULT
;
1637 return -TARGET_EINVAL
;
1641 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
1649 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
1650 return -TARGET_EFAULT
;
1652 __put_user(cr
.pid
, &tcr
->pid
);
1653 __put_user(cr
.uid
, &tcr
->uid
);
1654 __put_user(cr
.gid
, &tcr
->gid
);
1655 unlock_user_struct(tcr
, optval_addr
, 1);
1656 if (put_user_u32(len
, optlen
)) {
1657 return -TARGET_EFAULT
;
1661 /* Options with 'int' argument. */
1662 case TARGET_SO_DEBUG
:
1665 case TARGET_SO_REUSEADDR
:
1666 optname
= SO_REUSEADDR
;
1668 case TARGET_SO_TYPE
:
1671 case TARGET_SO_ERROR
:
1674 case TARGET_SO_DONTROUTE
:
1675 optname
= SO_DONTROUTE
;
1677 case TARGET_SO_BROADCAST
:
1678 optname
= SO_BROADCAST
;
1680 case TARGET_SO_SNDBUF
:
1681 optname
= SO_SNDBUF
;
1683 case TARGET_SO_RCVBUF
:
1684 optname
= SO_RCVBUF
;
1686 case TARGET_SO_KEEPALIVE
:
1687 optname
= SO_KEEPALIVE
;
1689 case TARGET_SO_OOBINLINE
:
1690 optname
= SO_OOBINLINE
;
1692 case TARGET_SO_NO_CHECK
:
1693 optname
= SO_NO_CHECK
;
1695 case TARGET_SO_PRIORITY
:
1696 optname
= SO_PRIORITY
;
1699 case TARGET_SO_BSDCOMPAT
:
1700 optname
= SO_BSDCOMPAT
;
1703 case TARGET_SO_PASSCRED
:
1704 optname
= SO_PASSCRED
;
1706 case TARGET_SO_TIMESTAMP
:
1707 optname
= SO_TIMESTAMP
;
1709 case TARGET_SO_RCVLOWAT
:
1710 optname
= SO_RCVLOWAT
;
1712 case TARGET_SO_ACCEPTCONN
:
1713 optname
= SO_ACCEPTCONN
;
1720 /* TCP options all take an 'int' value. */
1722 if (get_user_u32(len
, optlen
))
1723 return -TARGET_EFAULT
;
1725 return -TARGET_EINVAL
;
1727 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1730 if (optname
== SO_TYPE
) {
1731 val
= host_to_target_sock_type(val
);
1736 if (put_user_u32(val
, optval_addr
))
1737 return -TARGET_EFAULT
;
1739 if (put_user_u8(val
, optval_addr
))
1740 return -TARGET_EFAULT
;
1742 if (put_user_u32(len
, optlen
))
1743 return -TARGET_EFAULT
;
1750 case IP_ROUTER_ALERT
:
1754 case IP_MTU_DISCOVER
:
1760 case IP_MULTICAST_TTL
:
1761 case IP_MULTICAST_LOOP
:
1762 if (get_user_u32(len
, optlen
))
1763 return -TARGET_EFAULT
;
1765 return -TARGET_EINVAL
;
1767 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1770 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
1772 if (put_user_u32(len
, optlen
)
1773 || put_user_u8(val
, optval_addr
))
1774 return -TARGET_EFAULT
;
1776 if (len
> sizeof(int))
1778 if (put_user_u32(len
, optlen
)
1779 || put_user_u32(val
, optval_addr
))
1780 return -TARGET_EFAULT
;
1784 ret
= -TARGET_ENOPROTOOPT
;
1790 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1792 ret
= -TARGET_EOPNOTSUPP
;
1798 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
1799 int count
, int copy
)
1801 struct target_iovec
*target_vec
;
1803 abi_ulong total_len
, max_len
;
1806 bool bad_address
= false;
1812 if (count
< 0 || count
> IOV_MAX
) {
1817 vec
= calloc(count
, sizeof(struct iovec
));
1823 target_vec
= lock_user(VERIFY_READ
, target_addr
,
1824 count
* sizeof(struct target_iovec
), 1);
1825 if (target_vec
== NULL
) {
1830 /* ??? If host page size > target page size, this will result in a
1831 value larger than what we can actually support. */
1832 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
1835 for (i
= 0; i
< count
; i
++) {
1836 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
1837 abi_long len
= tswapal(target_vec
[i
].iov_len
);
1842 } else if (len
== 0) {
1843 /* Zero length pointer is ignored. */
1844 vec
[i
].iov_base
= 0;
1846 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
1847 /* If the first buffer pointer is bad, this is a fault. But
1848 * subsequent bad buffers will result in a partial write; this
1849 * is realized by filling the vector with null pointers and
1851 if (!vec
[i
].iov_base
) {
1862 if (len
> max_len
- total_len
) {
1863 len
= max_len
- total_len
;
1866 vec
[i
].iov_len
= len
;
1870 unlock_user(target_vec
, target_addr
, 0);
1874 unlock_user(target_vec
, target_addr
, 0);
1881 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
1882 int count
, int copy
)
1884 struct target_iovec
*target_vec
;
1887 target_vec
= lock_user(VERIFY_READ
, target_addr
,
1888 count
* sizeof(struct target_iovec
), 1);
1890 for (i
= 0; i
< count
; i
++) {
1891 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
1892 abi_long len
= tswapal(target_vec
[i
].iov_base
);
1896 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
1898 unlock_user(target_vec
, target_addr
, 0);
1904 static inline int target_to_host_sock_type(int *type
)
1907 int target_type
= *type
;
1909 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
1910 case TARGET_SOCK_DGRAM
:
1911 host_type
= SOCK_DGRAM
;
1913 case TARGET_SOCK_STREAM
:
1914 host_type
= SOCK_STREAM
;
1917 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
1920 if (target_type
& TARGET_SOCK_CLOEXEC
) {
1921 #if defined(SOCK_CLOEXEC)
1922 host_type
|= SOCK_CLOEXEC
;
1924 return -TARGET_EINVAL
;
1927 if (target_type
& TARGET_SOCK_NONBLOCK
) {
1928 #if defined(SOCK_NONBLOCK)
1929 host_type
|= SOCK_NONBLOCK
;
1930 #elif !defined(O_NONBLOCK)
1931 return -TARGET_EINVAL
;
1938 /* Try to emulate socket type flags after socket creation. */
1939 static int sock_flags_fixup(int fd
, int target_type
)
1941 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
1942 if (target_type
& TARGET_SOCK_NONBLOCK
) {
1943 int flags
= fcntl(fd
, F_GETFL
);
1944 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
1946 return -TARGET_EINVAL
;
1953 /* do_socket() Must return target values and target errnos. */
1954 static abi_long
do_socket(int domain
, int type
, int protocol
)
1956 int target_type
= type
;
1959 ret
= target_to_host_sock_type(&type
);
1964 if (domain
== PF_NETLINK
)
1965 return -TARGET_EAFNOSUPPORT
;
1966 ret
= get_errno(socket(domain
, type
, protocol
));
1968 ret
= sock_flags_fixup(ret
, target_type
);
1973 /* do_bind() Must return target values and target errnos. */
1974 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
1980 if ((int)addrlen
< 0) {
1981 return -TARGET_EINVAL
;
1984 addr
= alloca(addrlen
+1);
1986 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
1990 return get_errno(bind(sockfd
, addr
, addrlen
));
1993 /* do_connect() Must return target values and target errnos. */
1994 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
2000 if ((int)addrlen
< 0) {
2001 return -TARGET_EINVAL
;
2004 addr
= alloca(addrlen
+1);
2006 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
2010 return get_errno(connect(sockfd
, addr
, addrlen
));
2013 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
2014 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
2015 int flags
, int send
)
2021 abi_ulong target_vec
;
2023 if (msgp
->msg_name
) {
2024 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
2025 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
2026 ret
= target_to_host_sockaddr(msg
.msg_name
, tswapal(msgp
->msg_name
),
2032 msg
.msg_name
= NULL
;
2033 msg
.msg_namelen
= 0;
2035 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
2036 msg
.msg_control
= alloca(msg
.msg_controllen
);
2037 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
2039 count
= tswapal(msgp
->msg_iovlen
);
2040 target_vec
= tswapal(msgp
->msg_iov
);
2041 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
2042 target_vec
, count
, send
);
2044 ret
= -host_to_target_errno(errno
);
2047 msg
.msg_iovlen
= count
;
2051 ret
= target_to_host_cmsg(&msg
, msgp
);
2053 ret
= get_errno(sendmsg(fd
, &msg
, flags
));
2055 ret
= get_errno(recvmsg(fd
, &msg
, flags
));
2056 if (!is_error(ret
)) {
2058 ret
= host_to_target_cmsg(msgp
, &msg
);
2059 if (!is_error(ret
)) {
2060 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
2061 if (msg
.msg_name
!= NULL
) {
2062 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
2063 msg
.msg_name
, msg
.msg_namelen
);
2075 unlock_iovec(vec
, target_vec
, count
, !send
);
2080 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
2081 int flags
, int send
)
2084 struct target_msghdr
*msgp
;
2086 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
2090 return -TARGET_EFAULT
;
2092 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
2093 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
2097 #ifdef TARGET_NR_sendmmsg
2098 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
2099 * so it might not have this *mmsg-specific flag either.
2101 #ifndef MSG_WAITFORONE
2102 #define MSG_WAITFORONE 0x10000
2105 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
2106 unsigned int vlen
, unsigned int flags
,
2109 struct target_mmsghdr
*mmsgp
;
2113 if (vlen
> UIO_MAXIOV
) {
2117 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
2119 return -TARGET_EFAULT
;
2122 for (i
= 0; i
< vlen
; i
++) {
2123 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
2124 if (is_error(ret
)) {
2127 mmsgp
[i
].msg_len
= tswap32(ret
);
2128 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2129 if (flags
& MSG_WAITFORONE
) {
2130 flags
|= MSG_DONTWAIT
;
2134 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
2136 /* Return number of datagrams sent if we sent any at all;
2137 * otherwise return the error.
2146 /* If we don't have a system accept4() then just call accept.
2147 * The callsites to do_accept4() will ensure that they don't
2148 * pass a non-zero flags argument in this config.
2150 #ifndef CONFIG_ACCEPT4
2151 static inline int accept4(int sockfd
, struct sockaddr
*addr
,
2152 socklen_t
*addrlen
, int flags
)
2155 return accept(sockfd
, addr
, addrlen
);
2159 /* do_accept4() Must return target values and target errnos. */
2160 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
2161 abi_ulong target_addrlen_addr
, int flags
)
2168 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
2170 if (target_addr
== 0) {
2171 return get_errno(accept4(fd
, NULL
, NULL
, host_flags
));
2174 /* linux returns EINVAL if addrlen pointer is invalid */
2175 if (get_user_u32(addrlen
, target_addrlen_addr
))
2176 return -TARGET_EINVAL
;
2178 if ((int)addrlen
< 0) {
2179 return -TARGET_EINVAL
;
2182 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2183 return -TARGET_EINVAL
;
2185 addr
= alloca(addrlen
);
2187 ret
= get_errno(accept4(fd
, addr
, &addrlen
, host_flags
));
2188 if (!is_error(ret
)) {
2189 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2190 if (put_user_u32(addrlen
, target_addrlen_addr
))
2191 ret
= -TARGET_EFAULT
;
2196 /* do_getpeername() Must return target values and target errnos. */
2197 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
2198 abi_ulong target_addrlen_addr
)
2204 if (get_user_u32(addrlen
, target_addrlen_addr
))
2205 return -TARGET_EFAULT
;
2207 if ((int)addrlen
< 0) {
2208 return -TARGET_EINVAL
;
2211 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2212 return -TARGET_EFAULT
;
2214 addr
= alloca(addrlen
);
2216 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
2217 if (!is_error(ret
)) {
2218 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2219 if (put_user_u32(addrlen
, target_addrlen_addr
))
2220 ret
= -TARGET_EFAULT
;
2225 /* do_getsockname() Must return target values and target errnos. */
2226 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
2227 abi_ulong target_addrlen_addr
)
2233 if (get_user_u32(addrlen
, target_addrlen_addr
))
2234 return -TARGET_EFAULT
;
2236 if ((int)addrlen
< 0) {
2237 return -TARGET_EINVAL
;
2240 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2241 return -TARGET_EFAULT
;
2243 addr
= alloca(addrlen
);
2245 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
2246 if (!is_error(ret
)) {
2247 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2248 if (put_user_u32(addrlen
, target_addrlen_addr
))
2249 ret
= -TARGET_EFAULT
;
2254 /* do_socketpair() Must return target values and target errnos. */
2255 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
2256 abi_ulong target_tab_addr
)
2261 target_to_host_sock_type(&type
);
2263 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
2264 if (!is_error(ret
)) {
2265 if (put_user_s32(tab
[0], target_tab_addr
)
2266 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
2267 ret
= -TARGET_EFAULT
;
2272 /* do_sendto() Must return target values and target errnos. */
2273 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
2274 abi_ulong target_addr
, socklen_t addrlen
)
2280 if ((int)addrlen
< 0) {
2281 return -TARGET_EINVAL
;
2284 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
2286 return -TARGET_EFAULT
;
2288 addr
= alloca(addrlen
+1);
2289 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
2291 unlock_user(host_msg
, msg
, 0);
2294 ret
= get_errno(sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
2296 ret
= get_errno(send(fd
, host_msg
, len
, flags
));
2298 unlock_user(host_msg
, msg
, 0);
2302 /* do_recvfrom() Must return target values and target errnos. */
2303 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
2304 abi_ulong target_addr
,
2305 abi_ulong target_addrlen
)
2312 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
2314 return -TARGET_EFAULT
;
2316 if (get_user_u32(addrlen
, target_addrlen
)) {
2317 ret
= -TARGET_EFAULT
;
2320 if ((int)addrlen
< 0) {
2321 ret
= -TARGET_EINVAL
;
2324 addr
= alloca(addrlen
);
2325 ret
= get_errno(recvfrom(fd
, host_msg
, len
, flags
, addr
, &addrlen
));
2327 addr
= NULL
; /* To keep compiler quiet. */
2328 ret
= get_errno(qemu_recv(fd
, host_msg
, len
, flags
));
2330 if (!is_error(ret
)) {
2332 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2333 if (put_user_u32(addrlen
, target_addrlen
)) {
2334 ret
= -TARGET_EFAULT
;
2338 unlock_user(host_msg
, msg
, len
);
2341 unlock_user(host_msg
, msg
, 0);
2346 #ifdef TARGET_NR_socketcall
2347 /* do_socketcall() Must return target values and target errnos. */
2348 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
2350 static const unsigned ac
[] = { /* number of arguments per call */
2351 [SOCKOP_socket
] = 3, /* domain, type, protocol */
2352 [SOCKOP_bind
] = 3, /* sockfd, addr, addrlen */
2353 [SOCKOP_connect
] = 3, /* sockfd, addr, addrlen */
2354 [SOCKOP_listen
] = 2, /* sockfd, backlog */
2355 [SOCKOP_accept
] = 3, /* sockfd, addr, addrlen */
2356 [SOCKOP_accept4
] = 4, /* sockfd, addr, addrlen, flags */
2357 [SOCKOP_getsockname
] = 3, /* sockfd, addr, addrlen */
2358 [SOCKOP_getpeername
] = 3, /* sockfd, addr, addrlen */
2359 [SOCKOP_socketpair
] = 4, /* domain, type, protocol, tab */
2360 [SOCKOP_send
] = 4, /* sockfd, msg, len, flags */
2361 [SOCKOP_recv
] = 4, /* sockfd, msg, len, flags */
2362 [SOCKOP_sendto
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2363 [SOCKOP_recvfrom
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2364 [SOCKOP_shutdown
] = 2, /* sockfd, how */
2365 [SOCKOP_sendmsg
] = 3, /* sockfd, msg, flags */
2366 [SOCKOP_recvmsg
] = 3, /* sockfd, msg, flags */
2367 [SOCKOP_setsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
2368 [SOCKOP_getsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
2370 abi_long a
[6]; /* max 6 args */
2372 /* first, collect the arguments in a[] according to ac[] */
2373 if (num
>= 0 && num
< ARRAY_SIZE(ac
)) {
2375 assert(ARRAY_SIZE(a
) >= ac
[num
]); /* ensure we have space for args */
2376 for (i
= 0; i
< ac
[num
]; ++i
) {
2377 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
2378 return -TARGET_EFAULT
;
2383 /* now when we have the args, actually handle the call */
2385 case SOCKOP_socket
: /* domain, type, protocol */
2386 return do_socket(a
[0], a
[1], a
[2]);
2387 case SOCKOP_bind
: /* sockfd, addr, addrlen */
2388 return do_bind(a
[0], a
[1], a
[2]);
2389 case SOCKOP_connect
: /* sockfd, addr, addrlen */
2390 return do_connect(a
[0], a
[1], a
[2]);
2391 case SOCKOP_listen
: /* sockfd, backlog */
2392 return get_errno(listen(a
[0], a
[1]));
2393 case SOCKOP_accept
: /* sockfd, addr, addrlen */
2394 return do_accept4(a
[0], a
[1], a
[2], 0);
2395 case SOCKOP_accept4
: /* sockfd, addr, addrlen, flags */
2396 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
2397 case SOCKOP_getsockname
: /* sockfd, addr, addrlen */
2398 return do_getsockname(a
[0], a
[1], a
[2]);
2399 case SOCKOP_getpeername
: /* sockfd, addr, addrlen */
2400 return do_getpeername(a
[0], a
[1], a
[2]);
2401 case SOCKOP_socketpair
: /* domain, type, protocol, tab */
2402 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
2403 case SOCKOP_send
: /* sockfd, msg, len, flags */
2404 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
2405 case SOCKOP_recv
: /* sockfd, msg, len, flags */
2406 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
2407 case SOCKOP_sendto
: /* sockfd, msg, len, flags, addr, addrlen */
2408 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
2409 case SOCKOP_recvfrom
: /* sockfd, msg, len, flags, addr, addrlen */
2410 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
2411 case SOCKOP_shutdown
: /* sockfd, how */
2412 return get_errno(shutdown(a
[0], a
[1]));
2413 case SOCKOP_sendmsg
: /* sockfd, msg, flags */
2414 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
2415 case SOCKOP_recvmsg
: /* sockfd, msg, flags */
2416 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
2417 case SOCKOP_setsockopt
: /* sockfd, level, optname, optval, optlen */
2418 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
2419 case SOCKOP_getsockopt
: /* sockfd, level, optname, optval, optlen */
2420 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
2422 gemu_log("Unsupported socketcall: %d\n", num
);
2423 return -TARGET_ENOSYS
;
2428 #define N_SHM_REGIONS 32
2430 static struct shm_region
{
2433 } shm_regions
[N_SHM_REGIONS
];
2435 struct target_semid_ds
2437 struct target_ipc_perm sem_perm
;
2438 abi_ulong sem_otime
;
2439 #if !defined(TARGET_PPC64)
2440 abi_ulong __unused1
;
2442 abi_ulong sem_ctime
;
2443 #if !defined(TARGET_PPC64)
2444 abi_ulong __unused2
;
2446 abi_ulong sem_nsems
;
2447 abi_ulong __unused3
;
2448 abi_ulong __unused4
;
2451 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
2452 abi_ulong target_addr
)
2454 struct target_ipc_perm
*target_ip
;
2455 struct target_semid_ds
*target_sd
;
2457 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2458 return -TARGET_EFAULT
;
2459 target_ip
= &(target_sd
->sem_perm
);
2460 host_ip
->__key
= tswap32(target_ip
->__key
);
2461 host_ip
->uid
= tswap32(target_ip
->uid
);
2462 host_ip
->gid
= tswap32(target_ip
->gid
);
2463 host_ip
->cuid
= tswap32(target_ip
->cuid
);
2464 host_ip
->cgid
= tswap32(target_ip
->cgid
);
2465 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2466 host_ip
->mode
= tswap32(target_ip
->mode
);
2468 host_ip
->mode
= tswap16(target_ip
->mode
);
2470 #if defined(TARGET_PPC)
2471 host_ip
->__seq
= tswap32(target_ip
->__seq
);
2473 host_ip
->__seq
= tswap16(target_ip
->__seq
);
2475 unlock_user_struct(target_sd
, target_addr
, 0);
2479 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
2480 struct ipc_perm
*host_ip
)
2482 struct target_ipc_perm
*target_ip
;
2483 struct target_semid_ds
*target_sd
;
2485 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2486 return -TARGET_EFAULT
;
2487 target_ip
= &(target_sd
->sem_perm
);
2488 target_ip
->__key
= tswap32(host_ip
->__key
);
2489 target_ip
->uid
= tswap32(host_ip
->uid
);
2490 target_ip
->gid
= tswap32(host_ip
->gid
);
2491 target_ip
->cuid
= tswap32(host_ip
->cuid
);
2492 target_ip
->cgid
= tswap32(host_ip
->cgid
);
2493 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2494 target_ip
->mode
= tswap32(host_ip
->mode
);
2496 target_ip
->mode
= tswap16(host_ip
->mode
);
2498 #if defined(TARGET_PPC)
2499 target_ip
->__seq
= tswap32(host_ip
->__seq
);
2501 target_ip
->__seq
= tswap16(host_ip
->__seq
);
2503 unlock_user_struct(target_sd
, target_addr
, 1);
2507 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
2508 abi_ulong target_addr
)
2510 struct target_semid_ds
*target_sd
;
2512 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2513 return -TARGET_EFAULT
;
2514 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
2515 return -TARGET_EFAULT
;
2516 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
2517 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
2518 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
2519 unlock_user_struct(target_sd
, target_addr
, 0);
2523 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
2524 struct semid_ds
*host_sd
)
2526 struct target_semid_ds
*target_sd
;
2528 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2529 return -TARGET_EFAULT
;
2530 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
2531 return -TARGET_EFAULT
;
2532 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
2533 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
2534 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
2535 unlock_user_struct(target_sd
, target_addr
, 1);
2539 struct target_seminfo
{
2552 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
2553 struct seminfo
*host_seminfo
)
2555 struct target_seminfo
*target_seminfo
;
2556 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
2557 return -TARGET_EFAULT
;
2558 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
2559 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
2560 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
2561 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
2562 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
2563 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
2564 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
2565 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
2566 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
2567 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
2568 unlock_user_struct(target_seminfo
, target_addr
, 1);
2574 struct semid_ds
*buf
;
2575 unsigned short *array
;
2576 struct seminfo
*__buf
;
2579 union target_semun
{
2586 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
2587 abi_ulong target_addr
)
2590 unsigned short *array
;
2592 struct semid_ds semid_ds
;
2595 semun
.buf
= &semid_ds
;
2597 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2599 return get_errno(ret
);
2601 nsems
= semid_ds
.sem_nsems
;
2603 *host_array
= malloc(nsems
*sizeof(unsigned short));
2605 return -TARGET_ENOMEM
;
2607 array
= lock_user(VERIFY_READ
, target_addr
,
2608 nsems
*sizeof(unsigned short), 1);
2611 return -TARGET_EFAULT
;
2614 for(i
=0; i
<nsems
; i
++) {
2615 __get_user((*host_array
)[i
], &array
[i
]);
2617 unlock_user(array
, target_addr
, 0);
2622 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
2623 unsigned short **host_array
)
2626 unsigned short *array
;
2628 struct semid_ds semid_ds
;
2631 semun
.buf
= &semid_ds
;
2633 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2635 return get_errno(ret
);
2637 nsems
= semid_ds
.sem_nsems
;
2639 array
= lock_user(VERIFY_WRITE
, target_addr
,
2640 nsems
*sizeof(unsigned short), 0);
2642 return -TARGET_EFAULT
;
2644 for(i
=0; i
<nsems
; i
++) {
2645 __put_user((*host_array
)[i
], &array
[i
]);
2648 unlock_user(array
, target_addr
, 1);
2653 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
2654 union target_semun target_su
)
2657 struct semid_ds dsarg
;
2658 unsigned short *array
= NULL
;
2659 struct seminfo seminfo
;
2660 abi_long ret
= -TARGET_EINVAL
;
2667 /* In 64 bit cross-endian situations, we will erroneously pick up
2668 * the wrong half of the union for the "val" element. To rectify
2669 * this, the entire 8-byte structure is byteswapped, followed by
2670 * a swap of the 4 byte val field. In other cases, the data is
2671 * already in proper host byte order. */
2672 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
2673 target_su
.buf
= tswapal(target_su
.buf
);
2674 arg
.val
= tswap32(target_su
.val
);
2676 arg
.val
= target_su
.val
;
2678 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2682 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
2686 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2687 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
2694 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
2698 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2699 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
2705 arg
.__buf
= &seminfo
;
2706 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2707 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
2715 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
2722 struct target_sembuf
{
2723 unsigned short sem_num
;
2728 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
2729 abi_ulong target_addr
,
2732 struct target_sembuf
*target_sembuf
;
2735 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
2736 nsops
*sizeof(struct target_sembuf
), 1);
2738 return -TARGET_EFAULT
;
2740 for(i
=0; i
<nsops
; i
++) {
2741 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
2742 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
2743 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
2746 unlock_user(target_sembuf
, target_addr
, 0);
2751 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
2753 struct sembuf sops
[nsops
];
2755 if (target_to_host_sembuf(sops
, ptr
, nsops
))
2756 return -TARGET_EFAULT
;
2758 return get_errno(semop(semid
, sops
, nsops
));
2761 struct target_msqid_ds
2763 struct target_ipc_perm msg_perm
;
2764 abi_ulong msg_stime
;
2765 #if TARGET_ABI_BITS == 32
2766 abi_ulong __unused1
;
2768 abi_ulong msg_rtime
;
2769 #if TARGET_ABI_BITS == 32
2770 abi_ulong __unused2
;
2772 abi_ulong msg_ctime
;
2773 #if TARGET_ABI_BITS == 32
2774 abi_ulong __unused3
;
2776 abi_ulong __msg_cbytes
;
2778 abi_ulong msg_qbytes
;
2779 abi_ulong msg_lspid
;
2780 abi_ulong msg_lrpid
;
2781 abi_ulong __unused4
;
2782 abi_ulong __unused5
;
2785 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
2786 abi_ulong target_addr
)
2788 struct target_msqid_ds
*target_md
;
2790 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
2791 return -TARGET_EFAULT
;
2792 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
2793 return -TARGET_EFAULT
;
2794 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
2795 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
2796 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
2797 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
2798 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
2799 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
2800 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
2801 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
2802 unlock_user_struct(target_md
, target_addr
, 0);
2806 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
2807 struct msqid_ds
*host_md
)
2809 struct target_msqid_ds
*target_md
;
2811 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
2812 return -TARGET_EFAULT
;
2813 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
2814 return -TARGET_EFAULT
;
2815 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
2816 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
2817 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
2818 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
2819 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
2820 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
2821 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
2822 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
2823 unlock_user_struct(target_md
, target_addr
, 1);
2827 struct target_msginfo
{
2835 unsigned short int msgseg
;
2838 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
2839 struct msginfo
*host_msginfo
)
2841 struct target_msginfo
*target_msginfo
;
2842 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
2843 return -TARGET_EFAULT
;
2844 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
2845 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
2846 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
2847 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
2848 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
2849 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
2850 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
2851 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
2852 unlock_user_struct(target_msginfo
, target_addr
, 1);
2856 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
2858 struct msqid_ds dsarg
;
2859 struct msginfo msginfo
;
2860 abi_long ret
= -TARGET_EINVAL
;
2868 if (target_to_host_msqid_ds(&dsarg
,ptr
))
2869 return -TARGET_EFAULT
;
2870 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
2871 if (host_to_target_msqid_ds(ptr
,&dsarg
))
2872 return -TARGET_EFAULT
;
2875 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
2879 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
2880 if (host_to_target_msginfo(ptr
, &msginfo
))
2881 return -TARGET_EFAULT
;
2888 struct target_msgbuf
{
2893 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
2894 ssize_t msgsz
, int msgflg
)
2896 struct target_msgbuf
*target_mb
;
2897 struct msgbuf
*host_mb
;
2901 return -TARGET_EINVAL
;
2904 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
2905 return -TARGET_EFAULT
;
2906 host_mb
= malloc(msgsz
+sizeof(long));
2908 unlock_user_struct(target_mb
, msgp
, 0);
2909 return -TARGET_ENOMEM
;
2911 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
2912 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
2913 ret
= get_errno(msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
2915 unlock_user_struct(target_mb
, msgp
, 0);
2920 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
2921 unsigned int msgsz
, abi_long msgtyp
,
2924 struct target_msgbuf
*target_mb
;
2926 struct msgbuf
*host_mb
;
2929 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
2930 return -TARGET_EFAULT
;
2932 host_mb
= g_malloc(msgsz
+sizeof(long));
2933 ret
= get_errno(msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
2936 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
2937 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
2938 if (!target_mtext
) {
2939 ret
= -TARGET_EFAULT
;
2942 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
2943 unlock_user(target_mtext
, target_mtext_addr
, ret
);
2946 target_mb
->mtype
= tswapal(host_mb
->mtype
);
2950 unlock_user_struct(target_mb
, msgp
, 1);
2955 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
2956 abi_ulong target_addr
)
2958 struct target_shmid_ds
*target_sd
;
2960 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2961 return -TARGET_EFAULT
;
2962 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
2963 return -TARGET_EFAULT
;
2964 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
2965 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
2966 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
2967 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
2968 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
2969 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
2970 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
2971 unlock_user_struct(target_sd
, target_addr
, 0);
2975 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
2976 struct shmid_ds
*host_sd
)
2978 struct target_shmid_ds
*target_sd
;
2980 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2981 return -TARGET_EFAULT
;
2982 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
2983 return -TARGET_EFAULT
;
2984 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
2985 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
2986 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
2987 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
2988 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
2989 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
2990 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
2991 unlock_user_struct(target_sd
, target_addr
, 1);
2995 struct target_shminfo
{
3003 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
3004 struct shminfo
*host_shminfo
)
3006 struct target_shminfo
*target_shminfo
;
3007 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
3008 return -TARGET_EFAULT
;
3009 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
3010 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
3011 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
3012 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
3013 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
3014 unlock_user_struct(target_shminfo
, target_addr
, 1);
3018 struct target_shm_info
{
3023 abi_ulong swap_attempts
;
3024 abi_ulong swap_successes
;
3027 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
3028 struct shm_info
*host_shm_info
)
3030 struct target_shm_info
*target_shm_info
;
3031 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
3032 return -TARGET_EFAULT
;
3033 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
3034 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
3035 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
3036 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
3037 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
3038 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
3039 unlock_user_struct(target_shm_info
, target_addr
, 1);
3043 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
3045 struct shmid_ds dsarg
;
3046 struct shminfo shminfo
;
3047 struct shm_info shm_info
;
3048 abi_long ret
= -TARGET_EINVAL
;
3056 if (target_to_host_shmid_ds(&dsarg
, buf
))
3057 return -TARGET_EFAULT
;
3058 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
3059 if (host_to_target_shmid_ds(buf
, &dsarg
))
3060 return -TARGET_EFAULT
;
3063 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
3064 if (host_to_target_shminfo(buf
, &shminfo
))
3065 return -TARGET_EFAULT
;
3068 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
3069 if (host_to_target_shm_info(buf
, &shm_info
))
3070 return -TARGET_EFAULT
;
3075 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
3082 static inline abi_ulong
do_shmat(int shmid
, abi_ulong shmaddr
, int shmflg
)
3086 struct shmid_ds shm_info
;
3089 /* find out the length of the shared memory segment */
3090 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
3091 if (is_error(ret
)) {
3092 /* can't get length, bail out */
3099 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
3101 abi_ulong mmap_start
;
3103 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
3105 if (mmap_start
== -1) {
3107 host_raddr
= (void *)-1;
3109 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
3112 if (host_raddr
== (void *)-1) {
3114 return get_errno((long)host_raddr
);
3116 raddr
=h2g((unsigned long)host_raddr
);
3118 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
3119 PAGE_VALID
| PAGE_READ
|
3120 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
3122 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
3123 if (shm_regions
[i
].start
== 0) {
3124 shm_regions
[i
].start
= raddr
;
3125 shm_regions
[i
].size
= shm_info
.shm_segsz
;
3135 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
3139 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
3140 if (shm_regions
[i
].start
== shmaddr
) {
3141 shm_regions
[i
].start
= 0;
3142 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
3147 return get_errno(shmdt(g2h(shmaddr
)));
3150 #ifdef TARGET_NR_ipc
3151 /* ??? This only works with linear mappings. */
3152 /* do_ipc() must return target values and target errnos. */
3153 static abi_long
do_ipc(unsigned int call
, abi_long first
,
3154 abi_long second
, abi_long third
,
3155 abi_long ptr
, abi_long fifth
)
3160 version
= call
>> 16;
3165 ret
= do_semop(first
, ptr
, second
);
3169 ret
= get_errno(semget(first
, second
, third
));
3172 case IPCOP_semctl
: {
3173 /* The semun argument to semctl is passed by value, so dereference the
3176 get_user_ual(atptr
, ptr
);
3177 ret
= do_semctl(first
, second
, third
,
3178 (union target_semun
) atptr
);
3183 ret
= get_errno(msgget(first
, second
));
3187 ret
= do_msgsnd(first
, ptr
, second
, third
);
3191 ret
= do_msgctl(first
, second
, ptr
);
3198 struct target_ipc_kludge
{
3203 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
3204 ret
= -TARGET_EFAULT
;
3208 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
3210 unlock_user_struct(tmp
, ptr
, 0);
3214 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
3223 raddr
= do_shmat(first
, ptr
, second
);
3224 if (is_error(raddr
))
3225 return get_errno(raddr
);
3226 if (put_user_ual(raddr
, third
))
3227 return -TARGET_EFAULT
;
3231 ret
= -TARGET_EINVAL
;
3236 ret
= do_shmdt(ptr
);
3240 /* IPC_* flag values are the same on all linux platforms */
3241 ret
= get_errno(shmget(first
, second
, third
));
3244 /* IPC_* and SHM_* command values are the same on all linux platforms */
3246 ret
= do_shmctl(first
, second
, ptr
);
3249 gemu_log("Unsupported ipc call: %d (version %d)\n", call
, version
);
3250 ret
= -TARGET_ENOSYS
;
3257 /* kernel structure types definitions */
3259 #define STRUCT(name, ...) STRUCT_ ## name,
3260 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
3262 #include "syscall_types.h"
3265 #undef STRUCT_SPECIAL
3267 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
3268 #define STRUCT_SPECIAL(name)
3269 #include "syscall_types.h"
3271 #undef STRUCT_SPECIAL
3273 typedef struct IOCTLEntry IOCTLEntry
;
3275 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3276 int fd
, abi_long cmd
, abi_long arg
);
3279 unsigned int target_cmd
;
3280 unsigned int host_cmd
;
3283 do_ioctl_fn
*do_ioctl
;
3284 const argtype arg_type
[5];
3287 #define IOC_R 0x0001
3288 #define IOC_W 0x0002
3289 #define IOC_RW (IOC_R | IOC_W)
3291 #define MAX_STRUCT_SIZE 4096
3293 #ifdef CONFIG_FIEMAP
3294 /* So fiemap access checks don't overflow on 32 bit systems.
3295 * This is very slightly smaller than the limit imposed by
3296 * the underlying kernel.
3298 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3299 / sizeof(struct fiemap_extent))
3301 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3302 int fd
, abi_long cmd
, abi_long arg
)
3304 /* The parameter for this ioctl is a struct fiemap followed
3305 * by an array of struct fiemap_extent whose size is set
3306 * in fiemap->fm_extent_count. The array is filled in by the
3309 int target_size_in
, target_size_out
;
3311 const argtype
*arg_type
= ie
->arg_type
;
3312 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
3315 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
3319 assert(arg_type
[0] == TYPE_PTR
);
3320 assert(ie
->access
== IOC_RW
);
3322 target_size_in
= thunk_type_size(arg_type
, 0);
3323 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
3325 return -TARGET_EFAULT
;
3327 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3328 unlock_user(argptr
, arg
, 0);
3329 fm
= (struct fiemap
*)buf_temp
;
3330 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
3331 return -TARGET_EINVAL
;
3334 outbufsz
= sizeof (*fm
) +
3335 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
3337 if (outbufsz
> MAX_STRUCT_SIZE
) {
3338 /* We can't fit all the extents into the fixed size buffer.
3339 * Allocate one that is large enough and use it instead.
3341 fm
= malloc(outbufsz
);
3343 return -TARGET_ENOMEM
;
3345 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
3348 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, fm
));
3349 if (!is_error(ret
)) {
3350 target_size_out
= target_size_in
;
3351 /* An extent_count of 0 means we were only counting the extents
3352 * so there are no structs to copy
3354 if (fm
->fm_extent_count
!= 0) {
3355 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
3357 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
3359 ret
= -TARGET_EFAULT
;
3361 /* Convert the struct fiemap */
3362 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
3363 if (fm
->fm_extent_count
!= 0) {
3364 p
= argptr
+ target_size_in
;
3365 /* ...and then all the struct fiemap_extents */
3366 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
3367 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
3372 unlock_user(argptr
, arg
, target_size_out
);
3382 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3383 int fd
, abi_long cmd
, abi_long arg
)
3385 const argtype
*arg_type
= ie
->arg_type
;
3389 struct ifconf
*host_ifconf
;
3391 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
3392 int target_ifreq_size
;
3397 abi_long target_ifc_buf
;
3401 assert(arg_type
[0] == TYPE_PTR
);
3402 assert(ie
->access
== IOC_RW
);
3405 target_size
= thunk_type_size(arg_type
, 0);
3407 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3409 return -TARGET_EFAULT
;
3410 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3411 unlock_user(argptr
, arg
, 0);
3413 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
3414 target_ifc_len
= host_ifconf
->ifc_len
;
3415 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
3417 target_ifreq_size
= thunk_type_size(ifreq_arg_type
, 0);
3418 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
3419 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
3421 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
3422 if (outbufsz
> MAX_STRUCT_SIZE
) {
3423 /* We can't fit all the extents into the fixed size buffer.
3424 * Allocate one that is large enough and use it instead.
3426 host_ifconf
= malloc(outbufsz
);
3428 return -TARGET_ENOMEM
;
3430 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
3433 host_ifc_buf
= (char*)host_ifconf
+ sizeof(*host_ifconf
);
3435 host_ifconf
->ifc_len
= host_ifc_len
;
3436 host_ifconf
->ifc_buf
= host_ifc_buf
;
3438 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, host_ifconf
));
3439 if (!is_error(ret
)) {
3440 /* convert host ifc_len to target ifc_len */
3442 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
3443 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
3444 host_ifconf
->ifc_len
= target_ifc_len
;
3446 /* restore target ifc_buf */
3448 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
3450 /* copy struct ifconf to target user */
3452 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3454 return -TARGET_EFAULT
;
3455 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
3456 unlock_user(argptr
, arg
, target_size
);
3458 /* copy ifreq[] to target user */
3460 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
3461 for (i
= 0; i
< nb_ifreq
; i
++) {
3462 thunk_convert(argptr
+ i
* target_ifreq_size
,
3463 host_ifc_buf
+ i
* sizeof(struct ifreq
),
3464 ifreq_arg_type
, THUNK_TARGET
);
3466 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
3476 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
3477 abi_long cmd
, abi_long arg
)
3480 struct dm_ioctl
*host_dm
;
3481 abi_long guest_data
;
3482 uint32_t guest_data_size
;
3484 const argtype
*arg_type
= ie
->arg_type
;
3486 void *big_buf
= NULL
;
3490 target_size
= thunk_type_size(arg_type
, 0);
3491 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3493 ret
= -TARGET_EFAULT
;
3496 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3497 unlock_user(argptr
, arg
, 0);
3499 /* buf_temp is too small, so fetch things into a bigger buffer */
3500 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
3501 memcpy(big_buf
, buf_temp
, target_size
);
3505 guest_data
= arg
+ host_dm
->data_start
;
3506 if ((guest_data
- arg
) < 0) {
3510 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
3511 host_data
= (char*)host_dm
+ host_dm
->data_start
;
3513 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
3514 switch (ie
->host_cmd
) {
3516 case DM_LIST_DEVICES
:
3519 case DM_DEV_SUSPEND
:
3522 case DM_TABLE_STATUS
:
3523 case DM_TABLE_CLEAR
:
3525 case DM_LIST_VERSIONS
:
3529 case DM_DEV_SET_GEOMETRY
:
3530 /* data contains only strings */
3531 memcpy(host_data
, argptr
, guest_data_size
);
3534 memcpy(host_data
, argptr
, guest_data_size
);
3535 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
3539 void *gspec
= argptr
;
3540 void *cur_data
= host_data
;
3541 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
3542 int spec_size
= thunk_type_size(arg_type
, 0);
3545 for (i
= 0; i
< host_dm
->target_count
; i
++) {
3546 struct dm_target_spec
*spec
= cur_data
;
3550 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
3551 slen
= strlen((char*)gspec
+ spec_size
) + 1;
3553 spec
->next
= sizeof(*spec
) + slen
;
3554 strcpy((char*)&spec
[1], gspec
+ spec_size
);
3556 cur_data
+= spec
->next
;
3561 ret
= -TARGET_EINVAL
;
3564 unlock_user(argptr
, guest_data
, 0);
3566 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3567 if (!is_error(ret
)) {
3568 guest_data
= arg
+ host_dm
->data_start
;
3569 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
3570 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
3571 switch (ie
->host_cmd
) {
3576 case DM_DEV_SUSPEND
:
3579 case DM_TABLE_CLEAR
:
3581 case DM_DEV_SET_GEOMETRY
:
3582 /* no return data */
3584 case DM_LIST_DEVICES
:
3586 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
3587 uint32_t remaining_data
= guest_data_size
;
3588 void *cur_data
= argptr
;
3589 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
3590 int nl_size
= 12; /* can't use thunk_size due to alignment */
3593 uint32_t next
= nl
->next
;
3595 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
3597 if (remaining_data
< nl
->next
) {
3598 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3601 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
3602 strcpy(cur_data
+ nl_size
, nl
->name
);
3603 cur_data
+= nl
->next
;
3604 remaining_data
-= nl
->next
;
3608 nl
= (void*)nl
+ next
;
3613 case DM_TABLE_STATUS
:
3615 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
3616 void *cur_data
= argptr
;
3617 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
3618 int spec_size
= thunk_type_size(arg_type
, 0);
3621 for (i
= 0; i
< host_dm
->target_count
; i
++) {
3622 uint32_t next
= spec
->next
;
3623 int slen
= strlen((char*)&spec
[1]) + 1;
3624 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
3625 if (guest_data_size
< spec
->next
) {
3626 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3629 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
3630 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
3631 cur_data
= argptr
+ spec
->next
;
3632 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
3638 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
3639 int count
= *(uint32_t*)hdata
;
3640 uint64_t *hdev
= hdata
+ 8;
3641 uint64_t *gdev
= argptr
+ 8;
3644 *(uint32_t*)argptr
= tswap32(count
);
3645 for (i
= 0; i
< count
; i
++) {
3646 *gdev
= tswap64(*hdev
);
3652 case DM_LIST_VERSIONS
:
3654 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
3655 uint32_t remaining_data
= guest_data_size
;
3656 void *cur_data
= argptr
;
3657 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
3658 int vers_size
= thunk_type_size(arg_type
, 0);
3661 uint32_t next
= vers
->next
;
3663 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
3665 if (remaining_data
< vers
->next
) {
3666 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3669 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
3670 strcpy(cur_data
+ vers_size
, vers
->name
);
3671 cur_data
+= vers
->next
;
3672 remaining_data
-= vers
->next
;
3676 vers
= (void*)vers
+ next
;
3681 ret
= -TARGET_EINVAL
;
3684 unlock_user(argptr
, guest_data
, guest_data_size
);
3686 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3688 ret
= -TARGET_EFAULT
;
3691 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3692 unlock_user(argptr
, arg
, target_size
);
3699 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
3700 abi_long cmd
, abi_long arg
)
3704 const argtype
*arg_type
= ie
->arg_type
;
3705 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
3708 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
3709 struct blkpg_partition host_part
;
3711 /* Read and convert blkpg */
3713 target_size
= thunk_type_size(arg_type
, 0);
3714 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3716 ret
= -TARGET_EFAULT
;
3719 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3720 unlock_user(argptr
, arg
, 0);
3722 switch (host_blkpg
->op
) {
3723 case BLKPG_ADD_PARTITION
:
3724 case BLKPG_DEL_PARTITION
:
3725 /* payload is struct blkpg_partition */
3728 /* Unknown opcode */
3729 ret
= -TARGET_EINVAL
;
3733 /* Read and convert blkpg->data */
3734 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
3735 target_size
= thunk_type_size(part_arg_type
, 0);
3736 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3738 ret
= -TARGET_EFAULT
;
3741 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
3742 unlock_user(argptr
, arg
, 0);
3744 /* Swizzle the data pointer to our local copy and call! */
3745 host_blkpg
->data
= &host_part
;
3746 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, host_blkpg
));
3752 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3753 int fd
, abi_long cmd
, abi_long arg
)
3755 const argtype
*arg_type
= ie
->arg_type
;
3756 const StructEntry
*se
;
3757 const argtype
*field_types
;
3758 const int *dst_offsets
, *src_offsets
;
3761 abi_ulong
*target_rt_dev_ptr
;
3762 unsigned long *host_rt_dev_ptr
;
3766 assert(ie
->access
== IOC_W
);
3767 assert(*arg_type
== TYPE_PTR
);
3769 assert(*arg_type
== TYPE_STRUCT
);
3770 target_size
= thunk_type_size(arg_type
, 0);
3771 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3773 return -TARGET_EFAULT
;
3776 assert(*arg_type
== (int)STRUCT_rtentry
);
3777 se
= struct_entries
+ *arg_type
++;
3778 assert(se
->convert
[0] == NULL
);
3779 /* convert struct here to be able to catch rt_dev string */
3780 field_types
= se
->field_types
;
3781 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
3782 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
3783 for (i
= 0; i
< se
->nb_fields
; i
++) {
3784 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
3785 assert(*field_types
== TYPE_PTRVOID
);
3786 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
3787 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
3788 if (*target_rt_dev_ptr
!= 0) {
3789 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
3790 tswapal(*target_rt_dev_ptr
));
3791 if (!*host_rt_dev_ptr
) {
3792 unlock_user(argptr
, arg
, 0);
3793 return -TARGET_EFAULT
;
3796 *host_rt_dev_ptr
= 0;
3801 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
3802 argptr
+ src_offsets
[i
],
3803 field_types
, THUNK_HOST
);
3805 unlock_user(argptr
, arg
, 0);
3807 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3808 if (*host_rt_dev_ptr
!= 0) {
3809 unlock_user((void *)*host_rt_dev_ptr
,
3810 *target_rt_dev_ptr
, 0);
3815 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3816 int fd
, abi_long cmd
, abi_long arg
)
3818 int sig
= target_to_host_signal(arg
);
3819 return get_errno(ioctl(fd
, ie
->host_cmd
, sig
));
3822 static IOCTLEntry ioctl_entries
[] = {
3823 #define IOCTL(cmd, access, ...) \
3824 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3825 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3826 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
3831 /* ??? Implement proper locking for ioctls. */
3832 /* do_ioctl() Must return target values and target errnos. */
3833 static abi_long
do_ioctl(int fd
, abi_long cmd
, abi_long arg
)
3835 const IOCTLEntry
*ie
;
3836 const argtype
*arg_type
;
3838 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
3844 if (ie
->target_cmd
== 0) {
3845 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
3846 return -TARGET_ENOSYS
;
3848 if (ie
->target_cmd
== cmd
)
3852 arg_type
= ie
->arg_type
;
3854 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd
, ie
->name
);
3857 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
3860 switch(arg_type
[0]) {
3863 ret
= get_errno(ioctl(fd
, ie
->host_cmd
));
3868 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, arg
));
3872 target_size
= thunk_type_size(arg_type
, 0);
3873 switch(ie
->access
) {
3875 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3876 if (!is_error(ret
)) {
3877 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3879 return -TARGET_EFAULT
;
3880 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3881 unlock_user(argptr
, arg
, target_size
);
3885 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3887 return -TARGET_EFAULT
;
3888 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3889 unlock_user(argptr
, arg
, 0);
3890 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3894 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3896 return -TARGET_EFAULT
;
3897 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3898 unlock_user(argptr
, arg
, 0);
3899 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3900 if (!is_error(ret
)) {
3901 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3903 return -TARGET_EFAULT
;
3904 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3905 unlock_user(argptr
, arg
, target_size
);
3911 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3912 (long)cmd
, arg_type
[0]);
3913 ret
= -TARGET_ENOSYS
;
3919 static const bitmask_transtbl iflag_tbl
[] = {
3920 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
3921 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
3922 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
3923 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
3924 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
3925 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
3926 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
3927 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
3928 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
3929 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
3930 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
3931 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
3932 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
3933 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
3937 static const bitmask_transtbl oflag_tbl
[] = {
3938 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
3939 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
3940 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
3941 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
3942 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
3943 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
3944 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
3945 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
3946 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
3947 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
3948 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
3949 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
3950 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
3951 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
3952 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
3953 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
3954 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
3955 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
3956 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
3957 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
3958 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
3959 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
3960 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
3961 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
3965 static const bitmask_transtbl cflag_tbl
[] = {
3966 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
3967 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
3968 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
3969 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
3970 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
3971 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
3972 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
3973 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
3974 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
3975 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
3976 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
3977 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
3978 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
3979 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
3980 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
3981 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
3982 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
3983 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
3984 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
3985 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
3986 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
3987 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
3988 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
3989 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
3990 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
3991 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
3992 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
3993 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
3994 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
3995 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
3996 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
4000 static const bitmask_transtbl lflag_tbl
[] = {
4001 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
4002 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
4003 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
4004 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
4005 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
4006 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
4007 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
4008 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
4009 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
4010 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
4011 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
4012 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
4013 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
4014 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
4015 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
4019 static void target_to_host_termios (void *dst
, const void *src
)
4021 struct host_termios
*host
= dst
;
4022 const struct target_termios
*target
= src
;
4025 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
4027 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
4029 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
4031 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
4032 host
->c_line
= target
->c_line
;
4034 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
4035 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
4036 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
4037 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
4038 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
4039 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
4040 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
4041 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
4042 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
4043 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
4044 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
4045 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
4046 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
4047 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
4048 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
4049 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
4050 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
4051 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
4054 static void host_to_target_termios (void *dst
, const void *src
)
4056 struct target_termios
*target
= dst
;
4057 const struct host_termios
*host
= src
;
4060 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
4062 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
4064 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
4066 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
4067 target
->c_line
= host
->c_line
;
4069 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
4070 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
4071 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
4072 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
4073 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
4074 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
4075 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
4076 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
4077 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
4078 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
4079 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
4080 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
4081 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
4082 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
4083 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
4084 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
4085 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
4086 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
4089 static const StructEntry struct_termios_def
= {
4090 .convert
= { host_to_target_termios
, target_to_host_termios
},
4091 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
4092 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
4095 static bitmask_transtbl mmap_flags_tbl
[] = {
4096 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
4097 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
4098 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
4099 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
, MAP_ANONYMOUS
, MAP_ANONYMOUS
},
4100 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
, MAP_GROWSDOWN
, MAP_GROWSDOWN
},
4101 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
, MAP_DENYWRITE
, MAP_DENYWRITE
},
4102 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
, MAP_EXECUTABLE
, MAP_EXECUTABLE
},
4103 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
4104 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
, MAP_NORESERVE
,
4109 #if defined(TARGET_I386)
4111 /* NOTE: there is really one LDT for all the threads */
4112 static uint8_t *ldt_table
;
4114 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
4121 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
4122 if (size
> bytecount
)
4124 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
4126 return -TARGET_EFAULT
;
4127 /* ??? Should this by byteswapped? */
4128 memcpy(p
, ldt_table
, size
);
4129 unlock_user(p
, ptr
, size
);
4133 /* XXX: add locking support */
4134 static abi_long
write_ldt(CPUX86State
*env
,
4135 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
4137 struct target_modify_ldt_ldt_s ldt_info
;
4138 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4139 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
4140 int seg_not_present
, useable
, lm
;
4141 uint32_t *lp
, entry_1
, entry_2
;
4143 if (bytecount
!= sizeof(ldt_info
))
4144 return -TARGET_EINVAL
;
4145 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
4146 return -TARGET_EFAULT
;
4147 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
4148 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
4149 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
4150 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
4151 unlock_user_struct(target_ldt_info
, ptr
, 0);
4153 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
4154 return -TARGET_EINVAL
;
4155 seg_32bit
= ldt_info
.flags
& 1;
4156 contents
= (ldt_info
.flags
>> 1) & 3;
4157 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
4158 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
4159 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
4160 useable
= (ldt_info
.flags
>> 6) & 1;
4164 lm
= (ldt_info
.flags
>> 7) & 1;
4166 if (contents
== 3) {
4168 return -TARGET_EINVAL
;
4169 if (seg_not_present
== 0)
4170 return -TARGET_EINVAL
;
4172 /* allocate the LDT */
4174 env
->ldt
.base
= target_mmap(0,
4175 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
4176 PROT_READ
|PROT_WRITE
,
4177 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
4178 if (env
->ldt
.base
== -1)
4179 return -TARGET_ENOMEM
;
4180 memset(g2h(env
->ldt
.base
), 0,
4181 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
4182 env
->ldt
.limit
= 0xffff;
4183 ldt_table
= g2h(env
->ldt
.base
);
4186 /* NOTE: same code as Linux kernel */
4187 /* Allow LDTs to be cleared by the user. */
4188 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
4191 read_exec_only
== 1 &&
4193 limit_in_pages
== 0 &&
4194 seg_not_present
== 1 &&
4202 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
4203 (ldt_info
.limit
& 0x0ffff);
4204 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
4205 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
4206 (ldt_info
.limit
& 0xf0000) |
4207 ((read_exec_only
^ 1) << 9) |
4209 ((seg_not_present
^ 1) << 15) |
4211 (limit_in_pages
<< 23) |
4215 entry_2
|= (useable
<< 20);
4217 /* Install the new entry ... */
4219 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
4220 lp
[0] = tswap32(entry_1
);
4221 lp
[1] = tswap32(entry_2
);
4225 /* specific and weird i386 syscalls */
4226 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
4227 unsigned long bytecount
)
4233 ret
= read_ldt(ptr
, bytecount
);
4236 ret
= write_ldt(env
, ptr
, bytecount
, 1);
4239 ret
= write_ldt(env
, ptr
, bytecount
, 0);
4242 ret
= -TARGET_ENOSYS
;
4248 #if defined(TARGET_I386) && defined(TARGET_ABI32)
4249 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
4251 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
4252 struct target_modify_ldt_ldt_s ldt_info
;
4253 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4254 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
4255 int seg_not_present
, useable
, lm
;
4256 uint32_t *lp
, entry_1
, entry_2
;
4259 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
4260 if (!target_ldt_info
)
4261 return -TARGET_EFAULT
;
4262 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
4263 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
4264 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
4265 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
4266 if (ldt_info
.entry_number
== -1) {
4267 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
4268 if (gdt_table
[i
] == 0) {
4269 ldt_info
.entry_number
= i
;
4270 target_ldt_info
->entry_number
= tswap32(i
);
4275 unlock_user_struct(target_ldt_info
, ptr
, 1);
4277 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
4278 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
4279 return -TARGET_EINVAL
;
4280 seg_32bit
= ldt_info
.flags
& 1;
4281 contents
= (ldt_info
.flags
>> 1) & 3;
4282 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
4283 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
4284 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
4285 useable
= (ldt_info
.flags
>> 6) & 1;
4289 lm
= (ldt_info
.flags
>> 7) & 1;
4292 if (contents
== 3) {
4293 if (seg_not_present
== 0)
4294 return -TARGET_EINVAL
;
4297 /* NOTE: same code as Linux kernel */
4298 /* Allow LDTs to be cleared by the user. */
4299 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
4300 if ((contents
== 0 &&
4301 read_exec_only
== 1 &&
4303 limit_in_pages
== 0 &&
4304 seg_not_present
== 1 &&
4312 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
4313 (ldt_info
.limit
& 0x0ffff);
4314 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
4315 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
4316 (ldt_info
.limit
& 0xf0000) |
4317 ((read_exec_only
^ 1) << 9) |
4319 ((seg_not_present
^ 1) << 15) |
4321 (limit_in_pages
<< 23) |
4326 /* Install the new entry ... */
4328 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
4329 lp
[0] = tswap32(entry_1
);
4330 lp
[1] = tswap32(entry_2
);
4334 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
4336 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4337 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
4338 uint32_t base_addr
, limit
, flags
;
4339 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
4340 int seg_not_present
, useable
, lm
;
4341 uint32_t *lp
, entry_1
, entry_2
;
4343 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
4344 if (!target_ldt_info
)
4345 return -TARGET_EFAULT
;
4346 idx
= tswap32(target_ldt_info
->entry_number
);
4347 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
4348 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
4349 unlock_user_struct(target_ldt_info
, ptr
, 1);
4350 return -TARGET_EINVAL
;
4352 lp
= (uint32_t *)(gdt_table
+ idx
);
4353 entry_1
= tswap32(lp
[0]);
4354 entry_2
= tswap32(lp
[1]);
4356 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
4357 contents
= (entry_2
>> 10) & 3;
4358 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
4359 seg_32bit
= (entry_2
>> 22) & 1;
4360 limit_in_pages
= (entry_2
>> 23) & 1;
4361 useable
= (entry_2
>> 20) & 1;
4365 lm
= (entry_2
>> 21) & 1;
4367 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
4368 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
4369 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
4370 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
4371 base_addr
= (entry_1
>> 16) |
4372 (entry_2
& 0xff000000) |
4373 ((entry_2
& 0xff) << 16);
4374 target_ldt_info
->base_addr
= tswapal(base_addr
);
4375 target_ldt_info
->limit
= tswap32(limit
);
4376 target_ldt_info
->flags
= tswap32(flags
);
4377 unlock_user_struct(target_ldt_info
, ptr
, 1);
4380 #endif /* TARGET_I386 && TARGET_ABI32 */
4382 #ifndef TARGET_ABI32
4383 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
4390 case TARGET_ARCH_SET_GS
:
4391 case TARGET_ARCH_SET_FS
:
4392 if (code
== TARGET_ARCH_SET_GS
)
4396 cpu_x86_load_seg(env
, idx
, 0);
4397 env
->segs
[idx
].base
= addr
;
4399 case TARGET_ARCH_GET_GS
:
4400 case TARGET_ARCH_GET_FS
:
4401 if (code
== TARGET_ARCH_GET_GS
)
4405 val
= env
->segs
[idx
].base
;
4406 if (put_user(val
, addr
, abi_ulong
))
4407 ret
= -TARGET_EFAULT
;
4410 ret
= -TARGET_EINVAL
;
4417 #endif /* defined(TARGET_I386) */
4419 #define NEW_STACK_SIZE 0x40000
4422 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
4425 pthread_mutex_t mutex
;
4426 pthread_cond_t cond
;
4429 abi_ulong child_tidptr
;
4430 abi_ulong parent_tidptr
;
4434 static void *clone_func(void *arg
)
4436 new_thread_info
*info
= arg
;
4442 cpu
= ENV_GET_CPU(env
);
4444 ts
= (TaskState
*)cpu
->opaque
;
4445 info
->tid
= gettid();
4446 cpu
->host_tid
= info
->tid
;
4448 if (info
->child_tidptr
)
4449 put_user_u32(info
->tid
, info
->child_tidptr
);
4450 if (info
->parent_tidptr
)
4451 put_user_u32(info
->tid
, info
->parent_tidptr
);
4452 /* Enable signals. */
4453 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
4454 /* Signal to the parent that we're ready. */
4455 pthread_mutex_lock(&info
->mutex
);
4456 pthread_cond_broadcast(&info
->cond
);
4457 pthread_mutex_unlock(&info
->mutex
);
4458 /* Wait until the parent has finshed initializing the tls state. */
4459 pthread_mutex_lock(&clone_lock
);
4460 pthread_mutex_unlock(&clone_lock
);
4466 /* do_fork() Must return host values and target errnos (unlike most
4467 do_*() functions). */
4468 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
4469 abi_ulong parent_tidptr
, target_ulong newtls
,
4470 abi_ulong child_tidptr
)
4472 CPUState
*cpu
= ENV_GET_CPU(env
);
4476 CPUArchState
*new_env
;
4477 unsigned int nptl_flags
;
4480 /* Emulate vfork() with fork() */
4481 if (flags
& CLONE_VFORK
)
4482 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
4484 if (flags
& CLONE_VM
) {
4485 TaskState
*parent_ts
= (TaskState
*)cpu
->opaque
;
4486 new_thread_info info
;
4487 pthread_attr_t attr
;
4489 ts
= g_malloc0(sizeof(TaskState
));
4490 init_task_state(ts
);
4491 /* we create a new CPU instance. */
4492 new_env
= cpu_copy(env
);
4493 /* Init regs that differ from the parent. */
4494 cpu_clone_regs(new_env
, newsp
);
4495 new_cpu
= ENV_GET_CPU(new_env
);
4496 new_cpu
->opaque
= ts
;
4497 ts
->bprm
= parent_ts
->bprm
;
4498 ts
->info
= parent_ts
->info
;
4500 flags
&= ~CLONE_NPTL_FLAGS2
;
4502 if (nptl_flags
& CLONE_CHILD_CLEARTID
) {
4503 ts
->child_tidptr
= child_tidptr
;
4506 if (nptl_flags
& CLONE_SETTLS
)
4507 cpu_set_tls (new_env
, newtls
);
4509 /* Grab a mutex so that thread setup appears atomic. */
4510 pthread_mutex_lock(&clone_lock
);
4512 memset(&info
, 0, sizeof(info
));
4513 pthread_mutex_init(&info
.mutex
, NULL
);
4514 pthread_mutex_lock(&info
.mutex
);
4515 pthread_cond_init(&info
.cond
, NULL
);
4517 if (nptl_flags
& CLONE_CHILD_SETTID
)
4518 info
.child_tidptr
= child_tidptr
;
4519 if (nptl_flags
& CLONE_PARENT_SETTID
)
4520 info
.parent_tidptr
= parent_tidptr
;
4522 ret
= pthread_attr_init(&attr
);
4523 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
4524 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
4525 /* It is not safe to deliver signals until the child has finished
4526 initializing, so temporarily block all signals. */
4527 sigfillset(&sigmask
);
4528 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
4530 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
4531 /* TODO: Free new CPU state if thread creation failed. */
4533 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
4534 pthread_attr_destroy(&attr
);
4536 /* Wait for the child to initialize. */
4537 pthread_cond_wait(&info
.cond
, &info
.mutex
);
4539 if (flags
& CLONE_PARENT_SETTID
)
4540 put_user_u32(ret
, parent_tidptr
);
4544 pthread_mutex_unlock(&info
.mutex
);
4545 pthread_cond_destroy(&info
.cond
);
4546 pthread_mutex_destroy(&info
.mutex
);
4547 pthread_mutex_unlock(&clone_lock
);
4549 /* if no CLONE_VM, we consider it is a fork */
4550 if ((flags
& ~(CSIGNAL
| CLONE_NPTL_FLAGS2
)) != 0)
4555 /* Child Process. */
4556 cpu_clone_regs(env
, newsp
);
4558 /* There is a race condition here. The parent process could
4559 theoretically read the TID in the child process before the child
4560 tid is set. This would require using either ptrace
4561 (not implemented) or having *_tidptr to point at a shared memory
4562 mapping. We can't repeat the spinlock hack used above because
4563 the child process gets its own copy of the lock. */
4564 if (flags
& CLONE_CHILD_SETTID
)
4565 put_user_u32(gettid(), child_tidptr
);
4566 if (flags
& CLONE_PARENT_SETTID
)
4567 put_user_u32(gettid(), parent_tidptr
);
4568 ts
= (TaskState
*)cpu
->opaque
;
4569 if (flags
& CLONE_SETTLS
)
4570 cpu_set_tls (env
, newtls
);
4571 if (flags
& CLONE_CHILD_CLEARTID
)
4572 ts
->child_tidptr
= child_tidptr
;
4580 /* warning : doesn't handle linux specific flags... */
4581 static int target_to_host_fcntl_cmd(int cmd
)
4584 case TARGET_F_DUPFD
:
4585 case TARGET_F_GETFD
:
4586 case TARGET_F_SETFD
:
4587 case TARGET_F_GETFL
:
4588 case TARGET_F_SETFL
:
4590 case TARGET_F_GETLK
:
4592 case TARGET_F_SETLK
:
4594 case TARGET_F_SETLKW
:
4596 case TARGET_F_GETOWN
:
4598 case TARGET_F_SETOWN
:
4600 case TARGET_F_GETSIG
:
4602 case TARGET_F_SETSIG
:
4604 #if TARGET_ABI_BITS == 32
4605 case TARGET_F_GETLK64
:
4607 case TARGET_F_SETLK64
:
4609 case TARGET_F_SETLKW64
:
4612 case TARGET_F_SETLEASE
:
4614 case TARGET_F_GETLEASE
:
4616 #ifdef F_DUPFD_CLOEXEC
4617 case TARGET_F_DUPFD_CLOEXEC
:
4618 return F_DUPFD_CLOEXEC
;
4620 case TARGET_F_NOTIFY
:
4623 case TARGET_F_GETOWN_EX
:
4627 case TARGET_F_SETOWN_EX
:
4631 return -TARGET_EINVAL
;
4633 return -TARGET_EINVAL
;
4636 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4637 static const bitmask_transtbl flock_tbl
[] = {
4638 TRANSTBL_CONVERT(F_RDLCK
),
4639 TRANSTBL_CONVERT(F_WRLCK
),
4640 TRANSTBL_CONVERT(F_UNLCK
),
4641 TRANSTBL_CONVERT(F_EXLCK
),
4642 TRANSTBL_CONVERT(F_SHLCK
),
4646 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
4649 struct target_flock
*target_fl
;
4650 struct flock64 fl64
;
4651 struct target_flock64
*target_fl64
;
4653 struct f_owner_ex fox
;
4654 struct target_f_owner_ex
*target_fox
;
4657 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
4659 if (host_cmd
== -TARGET_EINVAL
)
4663 case TARGET_F_GETLK
:
4664 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
4665 return -TARGET_EFAULT
;
4667 target_to_host_bitmask(tswap16(target_fl
->l_type
), flock_tbl
);
4668 fl
.l_whence
= tswap16(target_fl
->l_whence
);
4669 fl
.l_start
= tswapal(target_fl
->l_start
);
4670 fl
.l_len
= tswapal(target_fl
->l_len
);
4671 fl
.l_pid
= tswap32(target_fl
->l_pid
);
4672 unlock_user_struct(target_fl
, arg
, 0);
4673 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
4675 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg
, 0))
4676 return -TARGET_EFAULT
;
4678 host_to_target_bitmask(tswap16(fl
.l_type
), flock_tbl
);
4679 target_fl
->l_whence
= tswap16(fl
.l_whence
);
4680 target_fl
->l_start
= tswapal(fl
.l_start
);
4681 target_fl
->l_len
= tswapal(fl
.l_len
);
4682 target_fl
->l_pid
= tswap32(fl
.l_pid
);
4683 unlock_user_struct(target_fl
, arg
, 1);
4687 case TARGET_F_SETLK
:
4688 case TARGET_F_SETLKW
:
4689 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
4690 return -TARGET_EFAULT
;
4692 target_to_host_bitmask(tswap16(target_fl
->l_type
), flock_tbl
);
4693 fl
.l_whence
= tswap16(target_fl
->l_whence
);
4694 fl
.l_start
= tswapal(target_fl
->l_start
);
4695 fl
.l_len
= tswapal(target_fl
->l_len
);
4696 fl
.l_pid
= tswap32(target_fl
->l_pid
);
4697 unlock_user_struct(target_fl
, arg
, 0);
4698 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
4701 case TARGET_F_GETLK64
:
4702 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
4703 return -TARGET_EFAULT
;
4705 target_to_host_bitmask(tswap16(target_fl64
->l_type
), flock_tbl
) >> 1;
4706 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
4707 fl64
.l_start
= tswap64(target_fl64
->l_start
);
4708 fl64
.l_len
= tswap64(target_fl64
->l_len
);
4709 fl64
.l_pid
= tswap32(target_fl64
->l_pid
);
4710 unlock_user_struct(target_fl64
, arg
, 0);
4711 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
4713 if (!lock_user_struct(VERIFY_WRITE
, target_fl64
, arg
, 0))
4714 return -TARGET_EFAULT
;
4715 target_fl64
->l_type
=
4716 host_to_target_bitmask(tswap16(fl64
.l_type
), flock_tbl
) >> 1;
4717 target_fl64
->l_whence
= tswap16(fl64
.l_whence
);
4718 target_fl64
->l_start
= tswap64(fl64
.l_start
);
4719 target_fl64
->l_len
= tswap64(fl64
.l_len
);
4720 target_fl64
->l_pid
= tswap32(fl64
.l_pid
);
4721 unlock_user_struct(target_fl64
, arg
, 1);
4724 case TARGET_F_SETLK64
:
4725 case TARGET_F_SETLKW64
:
4726 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
4727 return -TARGET_EFAULT
;
4729 target_to_host_bitmask(tswap16(target_fl64
->l_type
), flock_tbl
) >> 1;
4730 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
4731 fl64
.l_start
= tswap64(target_fl64
->l_start
);
4732 fl64
.l_len
= tswap64(target_fl64
->l_len
);
4733 fl64
.l_pid
= tswap32(target_fl64
->l_pid
);
4734 unlock_user_struct(target_fl64
, arg
, 0);
4735 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
4738 case TARGET_F_GETFL
:
4739 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
4741 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
4745 case TARGET_F_SETFL
:
4746 ret
= get_errno(fcntl(fd
, host_cmd
, target_to_host_bitmask(arg
, fcntl_flags_tbl
)));
4750 case TARGET_F_GETOWN_EX
:
4751 ret
= get_errno(fcntl(fd
, host_cmd
, &fox
));
4753 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
4754 return -TARGET_EFAULT
;
4755 target_fox
->type
= tswap32(fox
.type
);
4756 target_fox
->pid
= tswap32(fox
.pid
);
4757 unlock_user_struct(target_fox
, arg
, 1);
4763 case TARGET_F_SETOWN_EX
:
4764 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
4765 return -TARGET_EFAULT
;
4766 fox
.type
= tswap32(target_fox
->type
);
4767 fox
.pid
= tswap32(target_fox
->pid
);
4768 unlock_user_struct(target_fox
, arg
, 0);
4769 ret
= get_errno(fcntl(fd
, host_cmd
, &fox
));
4773 case TARGET_F_SETOWN
:
4774 case TARGET_F_GETOWN
:
4775 case TARGET_F_SETSIG
:
4776 case TARGET_F_GETSIG
:
4777 case TARGET_F_SETLEASE
:
4778 case TARGET_F_GETLEASE
:
4779 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
4783 ret
= get_errno(fcntl(fd
, cmd
, arg
));
4791 static inline int high2lowuid(int uid
)
4799 static inline int high2lowgid(int gid
)
4807 static inline int low2highuid(int uid
)
4809 if ((int16_t)uid
== -1)
4815 static inline int low2highgid(int gid
)
4817 if ((int16_t)gid
== -1)
4822 static inline int tswapid(int id
)
4827 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
4829 #else /* !USE_UID16 */
4830 static inline int high2lowuid(int uid
)
4834 static inline int high2lowgid(int gid
)
4838 static inline int low2highuid(int uid
)
4842 static inline int low2highgid(int gid
)
4846 static inline int tswapid(int id
)
4851 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
4853 #endif /* USE_UID16 */
4855 void syscall_init(void)
4858 const argtype
*arg_type
;
4862 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
4863 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
4864 #include "syscall_types.h"
4866 #undef STRUCT_SPECIAL
4868 /* Build target_to_host_errno_table[] table from
4869 * host_to_target_errno_table[]. */
4870 for (i
= 0; i
< ERRNO_TABLE_SIZE
; i
++) {
4871 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
4874 /* we patch the ioctl size if necessary. We rely on the fact that
4875 no ioctl has all the bits at '1' in the size field */
4877 while (ie
->target_cmd
!= 0) {
4878 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
4879 TARGET_IOC_SIZEMASK
) {
4880 arg_type
= ie
->arg_type
;
4881 if (arg_type
[0] != TYPE_PTR
) {
4882 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
4887 size
= thunk_type_size(arg_type
, 0);
4888 ie
->target_cmd
= (ie
->target_cmd
&
4889 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
4890 (size
<< TARGET_IOC_SIZESHIFT
);
4893 /* automatic consistency check if same arch */
4894 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4895 (defined(__x86_64__) && defined(TARGET_X86_64))
4896 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
4897 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4898 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
4905 #if TARGET_ABI_BITS == 32
4906 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
4908 #ifdef TARGET_WORDS_BIGENDIAN
4909 return ((uint64_t)word0
<< 32) | word1
;
4911 return ((uint64_t)word1
<< 32) | word0
;
4914 #else /* TARGET_ABI_BITS == 32 */
4915 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
4919 #endif /* TARGET_ABI_BITS != 32 */
4921 #ifdef TARGET_NR_truncate64
4922 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
4927 if (regpairs_aligned(cpu_env
)) {
4931 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
4935 #ifdef TARGET_NR_ftruncate64
4936 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
4941 if (regpairs_aligned(cpu_env
)) {
4945 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
4949 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
4950 abi_ulong target_addr
)
4952 struct target_timespec
*target_ts
;
4954 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1))
4955 return -TARGET_EFAULT
;
4956 host_ts
->tv_sec
= tswapal(target_ts
->tv_sec
);
4957 host_ts
->tv_nsec
= tswapal(target_ts
->tv_nsec
);
4958 unlock_user_struct(target_ts
, target_addr
, 0);
4962 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
4963 struct timespec
*host_ts
)
4965 struct target_timespec
*target_ts
;
4967 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0))
4968 return -TARGET_EFAULT
;
4969 target_ts
->tv_sec
= tswapal(host_ts
->tv_sec
);
4970 target_ts
->tv_nsec
= tswapal(host_ts
->tv_nsec
);
4971 unlock_user_struct(target_ts
, target_addr
, 1);
4975 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_itspec
,
4976 abi_ulong target_addr
)
4978 struct target_itimerspec
*target_itspec
;
4980 if (!lock_user_struct(VERIFY_READ
, target_itspec
, target_addr
, 1)) {
4981 return -TARGET_EFAULT
;
4984 host_itspec
->it_interval
.tv_sec
=
4985 tswapal(target_itspec
->it_interval
.tv_sec
);
4986 host_itspec
->it_interval
.tv_nsec
=
4987 tswapal(target_itspec
->it_interval
.tv_nsec
);
4988 host_itspec
->it_value
.tv_sec
= tswapal(target_itspec
->it_value
.tv_sec
);
4989 host_itspec
->it_value
.tv_nsec
= tswapal(target_itspec
->it_value
.tv_nsec
);
4991 unlock_user_struct(target_itspec
, target_addr
, 1);
4995 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
4996 struct itimerspec
*host_its
)
4998 struct target_itimerspec
*target_itspec
;
5000 if (!lock_user_struct(VERIFY_WRITE
, target_itspec
, target_addr
, 0)) {
5001 return -TARGET_EFAULT
;
5004 target_itspec
->it_interval
.tv_sec
= tswapal(host_its
->it_interval
.tv_sec
);
5005 target_itspec
->it_interval
.tv_nsec
= tswapal(host_its
->it_interval
.tv_nsec
);
5007 target_itspec
->it_value
.tv_sec
= tswapal(host_its
->it_value
.tv_sec
);
5008 target_itspec
->it_value
.tv_nsec
= tswapal(host_its
->it_value
.tv_nsec
);
5010 unlock_user_struct(target_itspec
, target_addr
, 0);
5014 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
5015 abi_ulong target_addr
)
5017 struct target_sigevent
*target_sevp
;
5019 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
5020 return -TARGET_EFAULT
;
5023 /* This union is awkward on 64 bit systems because it has a 32 bit
5024 * integer and a pointer in it; we follow the conversion approach
5025 * used for handling sigval types in signal.c so the guest should get
5026 * the correct value back even if we did a 64 bit byteswap and it's
5027 * using the 32 bit integer.
5029 host_sevp
->sigev_value
.sival_ptr
=
5030 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
5031 host_sevp
->sigev_signo
=
5032 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
5033 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
5034 host_sevp
->_sigev_un
._tid
= tswap32(target_sevp
->_sigev_un
._tid
);
5036 unlock_user_struct(target_sevp
, target_addr
, 1);
5040 #if defined(TARGET_NR_mlockall)
5041 static inline int target_to_host_mlockall_arg(int arg
)
5045 if (arg
& TARGET_MLOCKALL_MCL_CURRENT
) {
5046 result
|= MCL_CURRENT
;
5048 if (arg
& TARGET_MLOCKALL_MCL_FUTURE
) {
5049 result
|= MCL_FUTURE
;
5055 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
5056 static inline abi_long
host_to_target_stat64(void *cpu_env
,
5057 abi_ulong target_addr
,
5058 struct stat
*host_st
)
5060 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
5061 if (((CPUARMState
*)cpu_env
)->eabi
) {
5062 struct target_eabi_stat64
*target_st
;
5064 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
5065 return -TARGET_EFAULT
;
5066 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
5067 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
5068 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
5069 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5070 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
5072 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
5073 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
5074 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
5075 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
5076 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
5077 __put_user(host_st
->st_size
, &target_st
->st_size
);
5078 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
5079 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
5080 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
5081 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
5082 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
5083 unlock_user_struct(target_st
, target_addr
, 1);
5087 #if defined(TARGET_HAS_STRUCT_STAT64)
5088 struct target_stat64
*target_st
;
5090 struct target_stat
*target_st
;
5093 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
5094 return -TARGET_EFAULT
;
5095 memset(target_st
, 0, sizeof(*target_st
));
5096 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
5097 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
5098 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5099 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
5101 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
5102 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
5103 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
5104 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
5105 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
5106 /* XXX: better use of kernel struct */
5107 __put_user(host_st
->st_size
, &target_st
->st_size
);
5108 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
5109 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
5110 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
5111 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
5112 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
5113 unlock_user_struct(target_st
, target_addr
, 1);
5120 /* ??? Using host futex calls even when target atomic operations
5121 are not really atomic probably breaks things. However implementing
5122 futexes locally would make futexes shared between multiple processes
5123 tricky. However they're probably useless because guest atomic
5124 operations won't work either. */
5125 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
5126 target_ulong uaddr2
, int val3
)
5128 struct timespec ts
, *pts
;
5131 /* ??? We assume FUTEX_* constants are the same on both host
5133 #ifdef FUTEX_CMD_MASK
5134 base_op
= op
& FUTEX_CMD_MASK
;
5140 case FUTEX_WAIT_BITSET
:
5143 target_to_host_timespec(pts
, timeout
);
5147 return get_errno(sys_futex(g2h(uaddr
), op
, tswap32(val
),
5150 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
5152 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
5154 case FUTEX_CMP_REQUEUE
:
5156 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
5157 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
5158 But the prototype takes a `struct timespec *'; insert casts
5159 to satisfy the compiler. We do not need to tswap TIMEOUT
5160 since it's not compared to guest memory. */
5161 pts
= (struct timespec
*)(uintptr_t) timeout
;
5162 return get_errno(sys_futex(g2h(uaddr
), op
, val
, pts
,
5164 (base_op
== FUTEX_CMP_REQUEUE
5168 return -TARGET_ENOSYS
;
5172 /* Map host to target signal numbers for the wait family of syscalls.
5173 Assume all other status bits are the same. */
5174 int host_to_target_waitstatus(int status
)
5176 if (WIFSIGNALED(status
)) {
5177 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
5179 if (WIFSTOPPED(status
)) {
5180 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
5186 static int open_self_cmdline(void *cpu_env
, int fd
)
5189 bool word_skipped
= false;
5191 fd_orig
= open("/proc/self/cmdline", O_RDONLY
);
5201 nb_read
= read(fd_orig
, buf
, sizeof(buf
));
5203 fd_orig
= close(fd_orig
);
5205 } else if (nb_read
== 0) {
5209 if (!word_skipped
) {
5210 /* Skip the first string, which is the path to qemu-*-static
5211 instead of the actual command. */
5212 cp_buf
= memchr(buf
, 0, sizeof(buf
));
5214 /* Null byte found, skip one string */
5216 nb_read
-= cp_buf
- buf
;
5217 word_skipped
= true;
5222 if (write(fd
, cp_buf
, nb_read
) != nb_read
) {
5229 return close(fd_orig
);
5232 static int open_self_maps(void *cpu_env
, int fd
)
5234 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5235 TaskState
*ts
= cpu
->opaque
;
5241 fp
= fopen("/proc/self/maps", "r");
5246 while ((read
= getline(&line
, &len
, fp
)) != -1) {
5247 int fields
, dev_maj
, dev_min
, inode
;
5248 uint64_t min
, max
, offset
;
5249 char flag_r
, flag_w
, flag_x
, flag_p
;
5250 char path
[512] = "";
5251 fields
= sscanf(line
, "%"PRIx64
"-%"PRIx64
" %c%c%c%c %"PRIx64
" %x:%x %d"
5252 " %512s", &min
, &max
, &flag_r
, &flag_w
, &flag_x
,
5253 &flag_p
, &offset
, &dev_maj
, &dev_min
, &inode
, path
);
5255 if ((fields
< 10) || (fields
> 11)) {
5258 if (h2g_valid(min
)) {
5259 int flags
= page_get_flags(h2g(min
));
5260 max
= h2g_valid(max
- 1) ? max
: (uintptr_t)g2h(GUEST_ADDR_MAX
);
5261 if (page_check_range(h2g(min
), max
- min
, flags
) == -1) {
5264 if (h2g(min
) == ts
->info
->stack_limit
) {
5265 pstrcpy(path
, sizeof(path
), " [stack]");
5267 dprintf(fd
, TARGET_ABI_FMT_lx
"-" TARGET_ABI_FMT_lx
5268 " %c%c%c%c %08" PRIx64
" %02x:%02x %d %s%s\n",
5269 h2g(min
), h2g(max
- 1) + 1, flag_r
, flag_w
,
5270 flag_x
, flag_p
, offset
, dev_maj
, dev_min
, inode
,
5271 path
[0] ? " " : "", path
);
5281 static int open_self_stat(void *cpu_env
, int fd
)
5283 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5284 TaskState
*ts
= cpu
->opaque
;
5285 abi_ulong start_stack
= ts
->info
->start_stack
;
5288 for (i
= 0; i
< 44; i
++) {
5296 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
5297 } else if (i
== 1) {
5299 snprintf(buf
, sizeof(buf
), "(%s) ", ts
->bprm
->argv
[0]);
5300 } else if (i
== 27) {
5303 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
5305 /* for the rest, there is MasterCard */
5306 snprintf(buf
, sizeof(buf
), "0%c", i
== 43 ? '\n' : ' ');
5310 if (write(fd
, buf
, len
) != len
) {
5318 static int open_self_auxv(void *cpu_env
, int fd
)
5320 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5321 TaskState
*ts
= cpu
->opaque
;
5322 abi_ulong auxv
= ts
->info
->saved_auxv
;
5323 abi_ulong len
= ts
->info
->auxv_len
;
5327 * Auxiliary vector is stored in target process stack.
5328 * read in whole auxv vector and copy it to file
5330 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
5334 r
= write(fd
, ptr
, len
);
5341 lseek(fd
, 0, SEEK_SET
);
5342 unlock_user(ptr
, auxv
, len
);
5348 static int is_proc_myself(const char *filename
, const char *entry
)
5350 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
5351 filename
+= strlen("/proc/");
5352 if (!strncmp(filename
, "self/", strlen("self/"))) {
5353 filename
+= strlen("self/");
5354 } else if (*filename
>= '1' && *filename
<= '9') {
5356 snprintf(myself
, sizeof(myself
), "%d/", getpid());
5357 if (!strncmp(filename
, myself
, strlen(myself
))) {
5358 filename
+= strlen(myself
);
5365 if (!strcmp(filename
, entry
)) {
5372 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5373 static int is_proc(const char *filename
, const char *entry
)
5375 return strcmp(filename
, entry
) == 0;
5378 static int open_net_route(void *cpu_env
, int fd
)
5385 fp
= fopen("/proc/net/route", "r");
5392 read
= getline(&line
, &len
, fp
);
5393 dprintf(fd
, "%s", line
);
5397 while ((read
= getline(&line
, &len
, fp
)) != -1) {
5399 uint32_t dest
, gw
, mask
;
5400 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
5401 sscanf(line
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5402 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
5403 &mask
, &mtu
, &window
, &irtt
);
5404 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5405 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
5406 metric
, tswap32(mask
), mtu
, window
, irtt
);
5416 static int do_openat(void *cpu_env
, int dirfd
, const char *pathname
, int flags
, mode_t mode
)
5419 const char *filename
;
5420 int (*fill
)(void *cpu_env
, int fd
);
5421 int (*cmp
)(const char *s1
, const char *s2
);
5423 const struct fake_open
*fake_open
;
5424 static const struct fake_open fakes
[] = {
5425 { "maps", open_self_maps
, is_proc_myself
},
5426 { "stat", open_self_stat
, is_proc_myself
},
5427 { "auxv", open_self_auxv
, is_proc_myself
},
5428 { "cmdline", open_self_cmdline
, is_proc_myself
},
5429 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5430 { "/proc/net/route", open_net_route
, is_proc
},
5432 { NULL
, NULL
, NULL
}
5435 if (is_proc_myself(pathname
, "exe")) {
5436 int execfd
= qemu_getauxval(AT_EXECFD
);
5437 return execfd
? execfd
: get_errno(sys_openat(dirfd
, exec_path
, flags
, mode
));
5440 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
5441 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
5446 if (fake_open
->filename
) {
5448 char filename
[PATH_MAX
];
5451 /* create temporary file to map stat to */
5452 tmpdir
= getenv("TMPDIR");
5455 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
5456 fd
= mkstemp(filename
);
5462 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
5466 lseek(fd
, 0, SEEK_SET
);
5471 return get_errno(sys_openat(dirfd
, path(pathname
), flags
, mode
));
5474 /* do_syscall() should always have a single exit point at the end so
5475 that actions, such as logging of syscall results, can be performed.
5476 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
5477 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
5478 abi_long arg2
, abi_long arg3
, abi_long arg4
,
5479 abi_long arg5
, abi_long arg6
, abi_long arg7
,
5482 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
5489 gemu_log("syscall %d", num
);
5492 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
5495 case TARGET_NR_exit
:
5496 /* In old applications this may be used to implement _exit(2).
5497 However in threaded applictions it is used for thread termination,
5498 and _exit_group is used for application termination.
5499 Do thread termination if we have more then one thread. */
5500 /* FIXME: This probably breaks if a signal arrives. We should probably
5501 be disabling signals. */
5502 if (CPU_NEXT(first_cpu
)) {
5506 /* Remove the CPU from the list. */
5507 QTAILQ_REMOVE(&cpus
, cpu
, node
);
5510 if (ts
->child_tidptr
) {
5511 put_user_u32(0, ts
->child_tidptr
);
5512 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
5516 object_unref(OBJECT(cpu
));
5523 gdb_exit(cpu_env
, arg1
);
5525 ret
= 0; /* avoid warning */
5527 case TARGET_NR_read
:
5531 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
5533 ret
= get_errno(read(arg1
, p
, arg3
));
5534 unlock_user(p
, arg2
, ret
);
5537 case TARGET_NR_write
:
5538 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
5540 ret
= get_errno(write(arg1
, p
, arg3
));
5541 unlock_user(p
, arg2
, 0);
5543 case TARGET_NR_open
:
5544 if (!(p
= lock_user_string(arg1
)))
5546 ret
= get_errno(do_openat(cpu_env
, AT_FDCWD
, p
,
5547 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
5549 unlock_user(p
, arg1
, 0);
5551 case TARGET_NR_openat
:
5552 if (!(p
= lock_user_string(arg2
)))
5554 ret
= get_errno(do_openat(cpu_env
, arg1
, p
,
5555 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
5557 unlock_user(p
, arg2
, 0);
5559 case TARGET_NR_close
:
5560 ret
= get_errno(close(arg1
));
5565 case TARGET_NR_fork
:
5566 ret
= get_errno(do_fork(cpu_env
, SIGCHLD
, 0, 0, 0, 0));
5568 #ifdef TARGET_NR_waitpid
5569 case TARGET_NR_waitpid
:
5572 ret
= get_errno(waitpid(arg1
, &status
, arg3
));
5573 if (!is_error(ret
) && arg2
&& ret
5574 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
5579 #ifdef TARGET_NR_waitid
5580 case TARGET_NR_waitid
:
5584 ret
= get_errno(waitid(arg1
, arg2
, &info
, arg4
));
5585 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
5586 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
5588 host_to_target_siginfo(p
, &info
);
5589 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
5594 #ifdef TARGET_NR_creat /* not on alpha */
5595 case TARGET_NR_creat
:
5596 if (!(p
= lock_user_string(arg1
)))
5598 ret
= get_errno(creat(p
, arg2
));
5599 unlock_user(p
, arg1
, 0);
5602 case TARGET_NR_link
:
5605 p
= lock_user_string(arg1
);
5606 p2
= lock_user_string(arg2
);
5608 ret
= -TARGET_EFAULT
;
5610 ret
= get_errno(link(p
, p2
));
5611 unlock_user(p2
, arg2
, 0);
5612 unlock_user(p
, arg1
, 0);
5615 #if defined(TARGET_NR_linkat)
5616 case TARGET_NR_linkat
:
5621 p
= lock_user_string(arg2
);
5622 p2
= lock_user_string(arg4
);
5624 ret
= -TARGET_EFAULT
;
5626 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
5627 unlock_user(p
, arg2
, 0);
5628 unlock_user(p2
, arg4
, 0);
5632 case TARGET_NR_unlink
:
5633 if (!(p
= lock_user_string(arg1
)))
5635 ret
= get_errno(unlink(p
));
5636 unlock_user(p
, arg1
, 0);
5638 #if defined(TARGET_NR_unlinkat)
5639 case TARGET_NR_unlinkat
:
5640 if (!(p
= lock_user_string(arg2
)))
5642 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
5643 unlock_user(p
, arg2
, 0);
5646 case TARGET_NR_execve
:
5648 char **argp
, **envp
;
5651 abi_ulong guest_argp
;
5652 abi_ulong guest_envp
;
5659 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
5660 if (get_user_ual(addr
, gp
))
5668 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
5669 if (get_user_ual(addr
, gp
))
5676 argp
= alloca((argc
+ 1) * sizeof(void *));
5677 envp
= alloca((envc
+ 1) * sizeof(void *));
5679 for (gp
= guest_argp
, q
= argp
; gp
;
5680 gp
+= sizeof(abi_ulong
), q
++) {
5681 if (get_user_ual(addr
, gp
))
5685 if (!(*q
= lock_user_string(addr
)))
5687 total_size
+= strlen(*q
) + 1;
5691 for (gp
= guest_envp
, q
= envp
; gp
;
5692 gp
+= sizeof(abi_ulong
), q
++) {
5693 if (get_user_ual(addr
, gp
))
5697 if (!(*q
= lock_user_string(addr
)))
5699 total_size
+= strlen(*q
) + 1;
5703 /* This case will not be caught by the host's execve() if its
5704 page size is bigger than the target's. */
5705 if (total_size
> MAX_ARG_PAGES
* TARGET_PAGE_SIZE
) {
5706 ret
= -TARGET_E2BIG
;
5709 if (!(p
= lock_user_string(arg1
)))
5711 ret
= get_errno(execve(p
, argp
, envp
));
5712 unlock_user(p
, arg1
, 0);
5717 ret
= -TARGET_EFAULT
;
5720 for (gp
= guest_argp
, q
= argp
; *q
;
5721 gp
+= sizeof(abi_ulong
), q
++) {
5722 if (get_user_ual(addr
, gp
)
5725 unlock_user(*q
, addr
, 0);
5727 for (gp
= guest_envp
, q
= envp
; *q
;
5728 gp
+= sizeof(abi_ulong
), q
++) {
5729 if (get_user_ual(addr
, gp
)
5732 unlock_user(*q
, addr
, 0);
5736 case TARGET_NR_chdir
:
5737 if (!(p
= lock_user_string(arg1
)))
5739 ret
= get_errno(chdir(p
));
5740 unlock_user(p
, arg1
, 0);
5742 #ifdef TARGET_NR_time
5743 case TARGET_NR_time
:
5746 ret
= get_errno(time(&host_time
));
5749 && put_user_sal(host_time
, arg1
))
5754 case TARGET_NR_mknod
:
5755 if (!(p
= lock_user_string(arg1
)))
5757 ret
= get_errno(mknod(p
, arg2
, arg3
));
5758 unlock_user(p
, arg1
, 0);
5760 #if defined(TARGET_NR_mknodat)
5761 case TARGET_NR_mknodat
:
5762 if (!(p
= lock_user_string(arg2
)))
5764 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
5765 unlock_user(p
, arg2
, 0);
5768 case TARGET_NR_chmod
:
5769 if (!(p
= lock_user_string(arg1
)))
5771 ret
= get_errno(chmod(p
, arg2
));
5772 unlock_user(p
, arg1
, 0);
5774 #ifdef TARGET_NR_break
5775 case TARGET_NR_break
:
5778 #ifdef TARGET_NR_oldstat
5779 case TARGET_NR_oldstat
:
5782 case TARGET_NR_lseek
:
5783 ret
= get_errno(lseek(arg1
, arg2
, arg3
));
5785 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5786 /* Alpha specific */
5787 case TARGET_NR_getxpid
:
5788 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = getppid();
5789 ret
= get_errno(getpid());
5792 #ifdef TARGET_NR_getpid
5793 case TARGET_NR_getpid
:
5794 ret
= get_errno(getpid());
5797 case TARGET_NR_mount
:
5799 /* need to look at the data field */
5803 p
= lock_user_string(arg1
);
5811 p2
= lock_user_string(arg2
);
5814 unlock_user(p
, arg1
, 0);
5820 p3
= lock_user_string(arg3
);
5823 unlock_user(p
, arg1
, 0);
5825 unlock_user(p2
, arg2
, 0);
5832 /* FIXME - arg5 should be locked, but it isn't clear how to
5833 * do that since it's not guaranteed to be a NULL-terminated
5837 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
5839 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
));
5841 ret
= get_errno(ret
);
5844 unlock_user(p
, arg1
, 0);
5846 unlock_user(p2
, arg2
, 0);
5848 unlock_user(p3
, arg3
, 0);
5852 #ifdef TARGET_NR_umount
5853 case TARGET_NR_umount
:
5854 if (!(p
= lock_user_string(arg1
)))
5856 ret
= get_errno(umount(p
));
5857 unlock_user(p
, arg1
, 0);
5860 #ifdef TARGET_NR_stime /* not on alpha */
5861 case TARGET_NR_stime
:
5864 if (get_user_sal(host_time
, arg1
))
5866 ret
= get_errno(stime(&host_time
));
5870 case TARGET_NR_ptrace
:
5872 #ifdef TARGET_NR_alarm /* not on alpha */
5873 case TARGET_NR_alarm
:
5877 #ifdef TARGET_NR_oldfstat
5878 case TARGET_NR_oldfstat
:
5881 #ifdef TARGET_NR_pause /* not on alpha */
5882 case TARGET_NR_pause
:
5883 ret
= get_errno(pause());
5886 #ifdef TARGET_NR_utime
5887 case TARGET_NR_utime
:
5889 struct utimbuf tbuf
, *host_tbuf
;
5890 struct target_utimbuf
*target_tbuf
;
5892 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
5894 tbuf
.actime
= tswapal(target_tbuf
->actime
);
5895 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
5896 unlock_user_struct(target_tbuf
, arg2
, 0);
5901 if (!(p
= lock_user_string(arg1
)))
5903 ret
= get_errno(utime(p
, host_tbuf
));
5904 unlock_user(p
, arg1
, 0);
5908 case TARGET_NR_utimes
:
5910 struct timeval
*tvp
, tv
[2];
5912 if (copy_from_user_timeval(&tv
[0], arg2
)
5913 || copy_from_user_timeval(&tv
[1],
5914 arg2
+ sizeof(struct target_timeval
)))
5920 if (!(p
= lock_user_string(arg1
)))
5922 ret
= get_errno(utimes(p
, tvp
));
5923 unlock_user(p
, arg1
, 0);
5926 #if defined(TARGET_NR_futimesat)
5927 case TARGET_NR_futimesat
:
5929 struct timeval
*tvp
, tv
[2];
5931 if (copy_from_user_timeval(&tv
[0], arg3
)
5932 || copy_from_user_timeval(&tv
[1],
5933 arg3
+ sizeof(struct target_timeval
)))
5939 if (!(p
= lock_user_string(arg2
)))
5941 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
5942 unlock_user(p
, arg2
, 0);
5946 #ifdef TARGET_NR_stty
5947 case TARGET_NR_stty
:
5950 #ifdef TARGET_NR_gtty
5951 case TARGET_NR_gtty
:
5954 case TARGET_NR_access
:
5955 if (!(p
= lock_user_string(arg1
)))
5957 ret
= get_errno(access(path(p
), arg2
));
5958 unlock_user(p
, arg1
, 0);
5960 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5961 case TARGET_NR_faccessat
:
5962 if (!(p
= lock_user_string(arg2
)))
5964 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
5965 unlock_user(p
, arg2
, 0);
5968 #ifdef TARGET_NR_nice /* not on alpha */
5969 case TARGET_NR_nice
:
5970 ret
= get_errno(nice(arg1
));
5973 #ifdef TARGET_NR_ftime
5974 case TARGET_NR_ftime
:
5977 case TARGET_NR_sync
:
5981 case TARGET_NR_kill
:
5982 ret
= get_errno(kill(arg1
, target_to_host_signal(arg2
)));
5984 case TARGET_NR_rename
:
5987 p
= lock_user_string(arg1
);
5988 p2
= lock_user_string(arg2
);
5990 ret
= -TARGET_EFAULT
;
5992 ret
= get_errno(rename(p
, p2
));
5993 unlock_user(p2
, arg2
, 0);
5994 unlock_user(p
, arg1
, 0);
5997 #if defined(TARGET_NR_renameat)
5998 case TARGET_NR_renameat
:
6001 p
= lock_user_string(arg2
);
6002 p2
= lock_user_string(arg4
);
6004 ret
= -TARGET_EFAULT
;
6006 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
6007 unlock_user(p2
, arg4
, 0);
6008 unlock_user(p
, arg2
, 0);
6012 case TARGET_NR_mkdir
:
6013 if (!(p
= lock_user_string(arg1
)))
6015 ret
= get_errno(mkdir(p
, arg2
));
6016 unlock_user(p
, arg1
, 0);
6018 #if defined(TARGET_NR_mkdirat)
6019 case TARGET_NR_mkdirat
:
6020 if (!(p
= lock_user_string(arg2
)))
6022 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
6023 unlock_user(p
, arg2
, 0);
6026 case TARGET_NR_rmdir
:
6027 if (!(p
= lock_user_string(arg1
)))
6029 ret
= get_errno(rmdir(p
));
6030 unlock_user(p
, arg1
, 0);
6033 ret
= get_errno(dup(arg1
));
6035 case TARGET_NR_pipe
:
6036 ret
= do_pipe(cpu_env
, arg1
, 0, 0);
6038 #ifdef TARGET_NR_pipe2
6039 case TARGET_NR_pipe2
:
6040 ret
= do_pipe(cpu_env
, arg1
,
6041 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
6044 case TARGET_NR_times
:
6046 struct target_tms
*tmsp
;
6048 ret
= get_errno(times(&tms
));
6050 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
6053 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
6054 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
6055 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
6056 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
6059 ret
= host_to_target_clock_t(ret
);
6062 #ifdef TARGET_NR_prof
6063 case TARGET_NR_prof
:
6066 #ifdef TARGET_NR_signal
6067 case TARGET_NR_signal
:
6070 case TARGET_NR_acct
:
6072 ret
= get_errno(acct(NULL
));
6074 if (!(p
= lock_user_string(arg1
)))
6076 ret
= get_errno(acct(path(p
)));
6077 unlock_user(p
, arg1
, 0);
6080 #ifdef TARGET_NR_umount2
6081 case TARGET_NR_umount2
:
6082 if (!(p
= lock_user_string(arg1
)))
6084 ret
= get_errno(umount2(p
, arg2
));
6085 unlock_user(p
, arg1
, 0);
6088 #ifdef TARGET_NR_lock
6089 case TARGET_NR_lock
:
6092 case TARGET_NR_ioctl
:
6093 ret
= do_ioctl(arg1
, arg2
, arg3
);
6095 case TARGET_NR_fcntl
:
6096 ret
= do_fcntl(arg1
, arg2
, arg3
);
6098 #ifdef TARGET_NR_mpx
6102 case TARGET_NR_setpgid
:
6103 ret
= get_errno(setpgid(arg1
, arg2
));
6105 #ifdef TARGET_NR_ulimit
6106 case TARGET_NR_ulimit
:
6109 #ifdef TARGET_NR_oldolduname
6110 case TARGET_NR_oldolduname
:
6113 case TARGET_NR_umask
:
6114 ret
= get_errno(umask(arg1
));
6116 case TARGET_NR_chroot
:
6117 if (!(p
= lock_user_string(arg1
)))
6119 ret
= get_errno(chroot(p
));
6120 unlock_user(p
, arg1
, 0);
6122 case TARGET_NR_ustat
:
6124 case TARGET_NR_dup2
:
6125 ret
= get_errno(dup2(arg1
, arg2
));
6127 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
6128 case TARGET_NR_dup3
:
6129 ret
= get_errno(dup3(arg1
, arg2
, arg3
));
6132 #ifdef TARGET_NR_getppid /* not on alpha */
6133 case TARGET_NR_getppid
:
6134 ret
= get_errno(getppid());
6137 case TARGET_NR_getpgrp
:
6138 ret
= get_errno(getpgrp());
6140 case TARGET_NR_setsid
:
6141 ret
= get_errno(setsid());
6143 #ifdef TARGET_NR_sigaction
6144 case TARGET_NR_sigaction
:
6146 #if defined(TARGET_ALPHA)
6147 struct target_sigaction act
, oact
, *pact
= 0;
6148 struct target_old_sigaction
*old_act
;
6150 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
6152 act
._sa_handler
= old_act
->_sa_handler
;
6153 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
6154 act
.sa_flags
= old_act
->sa_flags
;
6155 act
.sa_restorer
= 0;
6156 unlock_user_struct(old_act
, arg2
, 0);
6159 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6160 if (!is_error(ret
) && arg3
) {
6161 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
6163 old_act
->_sa_handler
= oact
._sa_handler
;
6164 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
6165 old_act
->sa_flags
= oact
.sa_flags
;
6166 unlock_user_struct(old_act
, arg3
, 1);
6168 #elif defined(TARGET_MIPS)
6169 struct target_sigaction act
, oact
, *pact
, *old_act
;
6172 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
6174 act
._sa_handler
= old_act
->_sa_handler
;
6175 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
6176 act
.sa_flags
= old_act
->sa_flags
;
6177 unlock_user_struct(old_act
, arg2
, 0);
6183 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6185 if (!is_error(ret
) && arg3
) {
6186 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
6188 old_act
->_sa_handler
= oact
._sa_handler
;
6189 old_act
->sa_flags
= oact
.sa_flags
;
6190 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
6191 old_act
->sa_mask
.sig
[1] = 0;
6192 old_act
->sa_mask
.sig
[2] = 0;
6193 old_act
->sa_mask
.sig
[3] = 0;
6194 unlock_user_struct(old_act
, arg3
, 1);
6197 struct target_old_sigaction
*old_act
;
6198 struct target_sigaction act
, oact
, *pact
;
6200 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
6202 act
._sa_handler
= old_act
->_sa_handler
;
6203 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
6204 act
.sa_flags
= old_act
->sa_flags
;
6205 act
.sa_restorer
= old_act
->sa_restorer
;
6206 unlock_user_struct(old_act
, arg2
, 0);
6211 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6212 if (!is_error(ret
) && arg3
) {
6213 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
6215 old_act
->_sa_handler
= oact
._sa_handler
;
6216 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
6217 old_act
->sa_flags
= oact
.sa_flags
;
6218 old_act
->sa_restorer
= oact
.sa_restorer
;
6219 unlock_user_struct(old_act
, arg3
, 1);
6225 case TARGET_NR_rt_sigaction
:
6227 #if defined(TARGET_ALPHA)
6228 struct target_sigaction act
, oact
, *pact
= 0;
6229 struct target_rt_sigaction
*rt_act
;
6230 /* ??? arg4 == sizeof(sigset_t). */
6232 if (!lock_user_struct(VERIFY_READ
, rt_act
, arg2
, 1))
6234 act
._sa_handler
= rt_act
->_sa_handler
;
6235 act
.sa_mask
= rt_act
->sa_mask
;
6236 act
.sa_flags
= rt_act
->sa_flags
;
6237 act
.sa_restorer
= arg5
;
6238 unlock_user_struct(rt_act
, arg2
, 0);
6241 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6242 if (!is_error(ret
) && arg3
) {
6243 if (!lock_user_struct(VERIFY_WRITE
, rt_act
, arg3
, 0))
6245 rt_act
->_sa_handler
= oact
._sa_handler
;
6246 rt_act
->sa_mask
= oact
.sa_mask
;
6247 rt_act
->sa_flags
= oact
.sa_flags
;
6248 unlock_user_struct(rt_act
, arg3
, 1);
6251 struct target_sigaction
*act
;
6252 struct target_sigaction
*oact
;
6255 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1))
6260 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
6261 ret
= -TARGET_EFAULT
;
6262 goto rt_sigaction_fail
;
6266 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
6269 unlock_user_struct(act
, arg2
, 0);
6271 unlock_user_struct(oact
, arg3
, 1);
6275 #ifdef TARGET_NR_sgetmask /* not on alpha */
6276 case TARGET_NR_sgetmask
:
6279 abi_ulong target_set
;
6280 do_sigprocmask(0, NULL
, &cur_set
);
6281 host_to_target_old_sigset(&target_set
, &cur_set
);
6286 #ifdef TARGET_NR_ssetmask /* not on alpha */
6287 case TARGET_NR_ssetmask
:
6289 sigset_t set
, oset
, cur_set
;
6290 abi_ulong target_set
= arg1
;
6291 do_sigprocmask(0, NULL
, &cur_set
);
6292 target_to_host_old_sigset(&set
, &target_set
);
6293 sigorset(&set
, &set
, &cur_set
);
6294 do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
6295 host_to_target_old_sigset(&target_set
, &oset
);
6300 #ifdef TARGET_NR_sigprocmask
6301 case TARGET_NR_sigprocmask
:
6303 #if defined(TARGET_ALPHA)
6304 sigset_t set
, oldset
;
6309 case TARGET_SIG_BLOCK
:
6312 case TARGET_SIG_UNBLOCK
:
6315 case TARGET_SIG_SETMASK
:
6319 ret
= -TARGET_EINVAL
;
6323 target_to_host_old_sigset(&set
, &mask
);
6325 ret
= get_errno(do_sigprocmask(how
, &set
, &oldset
));
6326 if (!is_error(ret
)) {
6327 host_to_target_old_sigset(&mask
, &oldset
);
6329 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0; /* force no error */
6332 sigset_t set
, oldset
, *set_ptr
;
6337 case TARGET_SIG_BLOCK
:
6340 case TARGET_SIG_UNBLOCK
:
6343 case TARGET_SIG_SETMASK
:
6347 ret
= -TARGET_EINVAL
;
6350 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
6352 target_to_host_old_sigset(&set
, p
);
6353 unlock_user(p
, arg2
, 0);
6359 ret
= get_errno(do_sigprocmask(how
, set_ptr
, &oldset
));
6360 if (!is_error(ret
) && arg3
) {
6361 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
6363 host_to_target_old_sigset(p
, &oldset
);
6364 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
6370 case TARGET_NR_rt_sigprocmask
:
6373 sigset_t set
, oldset
, *set_ptr
;
6377 case TARGET_SIG_BLOCK
:
6380 case TARGET_SIG_UNBLOCK
:
6383 case TARGET_SIG_SETMASK
:
6387 ret
= -TARGET_EINVAL
;
6390 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
6392 target_to_host_sigset(&set
, p
);
6393 unlock_user(p
, arg2
, 0);
6399 ret
= get_errno(do_sigprocmask(how
, set_ptr
, &oldset
));
6400 if (!is_error(ret
) && arg3
) {
6401 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
6403 host_to_target_sigset(p
, &oldset
);
6404 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
6408 #ifdef TARGET_NR_sigpending
6409 case TARGET_NR_sigpending
:
6412 ret
= get_errno(sigpending(&set
));
6413 if (!is_error(ret
)) {
6414 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
6416 host_to_target_old_sigset(p
, &set
);
6417 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
6422 case TARGET_NR_rt_sigpending
:
6425 ret
= get_errno(sigpending(&set
));
6426 if (!is_error(ret
)) {
6427 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
6429 host_to_target_sigset(p
, &set
);
6430 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
6434 #ifdef TARGET_NR_sigsuspend
6435 case TARGET_NR_sigsuspend
:
6438 #if defined(TARGET_ALPHA)
6439 abi_ulong mask
= arg1
;
6440 target_to_host_old_sigset(&set
, &mask
);
6442 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6444 target_to_host_old_sigset(&set
, p
);
6445 unlock_user(p
, arg1
, 0);
6447 ret
= get_errno(sigsuspend(&set
));
6451 case TARGET_NR_rt_sigsuspend
:
6454 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6456 target_to_host_sigset(&set
, p
);
6457 unlock_user(p
, arg1
, 0);
6458 ret
= get_errno(sigsuspend(&set
));
6461 case TARGET_NR_rt_sigtimedwait
:
6464 struct timespec uts
, *puts
;
6467 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6469 target_to_host_sigset(&set
, p
);
6470 unlock_user(p
, arg1
, 0);
6473 target_to_host_timespec(puts
, arg3
);
6477 ret
= get_errno(sigtimedwait(&set
, &uinfo
, puts
));
6478 if (!is_error(ret
)) {
6480 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
6485 host_to_target_siginfo(p
, &uinfo
);
6486 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
6488 ret
= host_to_target_signal(ret
);
6492 case TARGET_NR_rt_sigqueueinfo
:
6495 if (!(p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_sigset_t
), 1)))
6497 target_to_host_siginfo(&uinfo
, p
);
6498 unlock_user(p
, arg1
, 0);
6499 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
6502 #ifdef TARGET_NR_sigreturn
6503 case TARGET_NR_sigreturn
:
6504 /* NOTE: ret is eax, so not transcoding must be done */
6505 ret
= do_sigreturn(cpu_env
);
6508 case TARGET_NR_rt_sigreturn
:
6509 /* NOTE: ret is eax, so not transcoding must be done */
6510 ret
= do_rt_sigreturn(cpu_env
);
6512 case TARGET_NR_sethostname
:
6513 if (!(p
= lock_user_string(arg1
)))
6515 ret
= get_errno(sethostname(p
, arg2
));
6516 unlock_user(p
, arg1
, 0);
6518 case TARGET_NR_setrlimit
:
6520 int resource
= target_to_host_resource(arg1
);
6521 struct target_rlimit
*target_rlim
;
6523 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
6525 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
6526 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
6527 unlock_user_struct(target_rlim
, arg2
, 0);
6528 ret
= get_errno(setrlimit(resource
, &rlim
));
6531 case TARGET_NR_getrlimit
:
6533 int resource
= target_to_host_resource(arg1
);
6534 struct target_rlimit
*target_rlim
;
6537 ret
= get_errno(getrlimit(resource
, &rlim
));
6538 if (!is_error(ret
)) {
6539 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
6541 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
6542 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
6543 unlock_user_struct(target_rlim
, arg2
, 1);
6547 case TARGET_NR_getrusage
:
6549 struct rusage rusage
;
6550 ret
= get_errno(getrusage(arg1
, &rusage
));
6551 if (!is_error(ret
)) {
6552 ret
= host_to_target_rusage(arg2
, &rusage
);
6556 case TARGET_NR_gettimeofday
:
6559 ret
= get_errno(gettimeofday(&tv
, NULL
));
6560 if (!is_error(ret
)) {
6561 if (copy_to_user_timeval(arg1
, &tv
))
6566 case TARGET_NR_settimeofday
:
6568 struct timeval tv
, *ptv
= NULL
;
6569 struct timezone tz
, *ptz
= NULL
;
6572 if (copy_from_user_timeval(&tv
, arg1
)) {
6579 if (copy_from_user_timezone(&tz
, arg2
)) {
6585 ret
= get_errno(settimeofday(ptv
, ptz
));
6588 #if defined(TARGET_NR_select)
6589 case TARGET_NR_select
:
6590 #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6591 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
6594 struct target_sel_arg_struct
*sel
;
6595 abi_ulong inp
, outp
, exp
, tvp
;
6598 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1))
6600 nsel
= tswapal(sel
->n
);
6601 inp
= tswapal(sel
->inp
);
6602 outp
= tswapal(sel
->outp
);
6603 exp
= tswapal(sel
->exp
);
6604 tvp
= tswapal(sel
->tvp
);
6605 unlock_user_struct(sel
, arg1
, 0);
6606 ret
= do_select(nsel
, inp
, outp
, exp
, tvp
);
6611 #ifdef TARGET_NR_pselect6
6612 case TARGET_NR_pselect6
:
6614 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
6615 fd_set rfds
, wfds
, efds
;
6616 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
6617 struct timespec ts
, *ts_ptr
;
6620 * The 6th arg is actually two args smashed together,
6621 * so we cannot use the C library.
6629 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
6630 target_sigset_t
*target_sigset
;
6638 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
6642 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
6646 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
6652 * This takes a timespec, and not a timeval, so we cannot
6653 * use the do_select() helper ...
6656 if (target_to_host_timespec(&ts
, ts_addr
)) {
6664 /* Extract the two packed args for the sigset */
6667 sig
.size
= _NSIG
/ 8;
6669 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
6673 arg_sigset
= tswapal(arg7
[0]);
6674 arg_sigsize
= tswapal(arg7
[1]);
6675 unlock_user(arg7
, arg6
, 0);
6679 if (arg_sigsize
!= sizeof(*target_sigset
)) {
6680 /* Like the kernel, we enforce correct size sigsets */
6681 ret
= -TARGET_EINVAL
;
6684 target_sigset
= lock_user(VERIFY_READ
, arg_sigset
,
6685 sizeof(*target_sigset
), 1);
6686 if (!target_sigset
) {
6689 target_to_host_sigset(&set
, target_sigset
);
6690 unlock_user(target_sigset
, arg_sigset
, 0);
6698 ret
= get_errno(sys_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
6701 if (!is_error(ret
)) {
6702 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
6704 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
6706 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
6709 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
))
6715 case TARGET_NR_symlink
:
6718 p
= lock_user_string(arg1
);
6719 p2
= lock_user_string(arg2
);
6721 ret
= -TARGET_EFAULT
;
6723 ret
= get_errno(symlink(p
, p2
));
6724 unlock_user(p2
, arg2
, 0);
6725 unlock_user(p
, arg1
, 0);
6728 #if defined(TARGET_NR_symlinkat)
6729 case TARGET_NR_symlinkat
:
6732 p
= lock_user_string(arg1
);
6733 p2
= lock_user_string(arg3
);
6735 ret
= -TARGET_EFAULT
;
6737 ret
= get_errno(symlinkat(p
, arg2
, p2
));
6738 unlock_user(p2
, arg3
, 0);
6739 unlock_user(p
, arg1
, 0);
6743 #ifdef TARGET_NR_oldlstat
6744 case TARGET_NR_oldlstat
:
6747 case TARGET_NR_readlink
:
6750 p
= lock_user_string(arg1
);
6751 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
6753 ret
= -TARGET_EFAULT
;
6755 /* Short circuit this for the magic exe check. */
6756 ret
= -TARGET_EINVAL
;
6757 } else if (is_proc_myself((const char *)p
, "exe")) {
6758 char real
[PATH_MAX
], *temp
;
6759 temp
= realpath(exec_path
, real
);
6760 /* Return value is # of bytes that we wrote to the buffer. */
6762 ret
= get_errno(-1);
6764 /* Don't worry about sign mismatch as earlier mapping
6765 * logic would have thrown a bad address error. */
6766 ret
= MIN(strlen(real
), arg3
);
6767 /* We cannot NUL terminate the string. */
6768 memcpy(p2
, real
, ret
);
6771 ret
= get_errno(readlink(path(p
), p2
, arg3
));
6773 unlock_user(p2
, arg2
, ret
);
6774 unlock_user(p
, arg1
, 0);
6777 #if defined(TARGET_NR_readlinkat)
6778 case TARGET_NR_readlinkat
:
6781 p
= lock_user_string(arg2
);
6782 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
6784 ret
= -TARGET_EFAULT
;
6785 } else if (is_proc_myself((const char *)p
, "exe")) {
6786 char real
[PATH_MAX
], *temp
;
6787 temp
= realpath(exec_path
, real
);
6788 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
6789 snprintf((char *)p2
, arg4
, "%s", real
);
6791 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
6793 unlock_user(p2
, arg3
, ret
);
6794 unlock_user(p
, arg2
, 0);
6798 #ifdef TARGET_NR_uselib
6799 case TARGET_NR_uselib
:
6802 #ifdef TARGET_NR_swapon
6803 case TARGET_NR_swapon
:
6804 if (!(p
= lock_user_string(arg1
)))
6806 ret
= get_errno(swapon(p
, arg2
));
6807 unlock_user(p
, arg1
, 0);
6810 case TARGET_NR_reboot
:
6811 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
6812 /* arg4 must be ignored in all other cases */
6813 p
= lock_user_string(arg4
);
6817 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
6818 unlock_user(p
, arg4
, 0);
6820 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
6823 #ifdef TARGET_NR_readdir
6824 case TARGET_NR_readdir
:
6827 #ifdef TARGET_NR_mmap
6828 case TARGET_NR_mmap
:
6829 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6830 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
6831 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6832 || defined(TARGET_S390X)
6835 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
6836 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
6844 unlock_user(v
, arg1
, 0);
6845 ret
= get_errno(target_mmap(v1
, v2
, v3
,
6846 target_to_host_bitmask(v4
, mmap_flags_tbl
),
6850 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
6851 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
6857 #ifdef TARGET_NR_mmap2
6858 case TARGET_NR_mmap2
:
6860 #define MMAP_SHIFT 12
6862 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
6863 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
6865 arg6
<< MMAP_SHIFT
));
6868 case TARGET_NR_munmap
:
6869 ret
= get_errno(target_munmap(arg1
, arg2
));
6871 case TARGET_NR_mprotect
:
6873 TaskState
*ts
= cpu
->opaque
;
6874 /* Special hack to detect libc making the stack executable. */
6875 if ((arg3
& PROT_GROWSDOWN
)
6876 && arg1
>= ts
->info
->stack_limit
6877 && arg1
<= ts
->info
->start_stack
) {
6878 arg3
&= ~PROT_GROWSDOWN
;
6879 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
6880 arg1
= ts
->info
->stack_limit
;
6883 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
6885 #ifdef TARGET_NR_mremap
6886 case TARGET_NR_mremap
:
6887 ret
= get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
6890 /* ??? msync/mlock/munlock are broken for softmmu. */
6891 #ifdef TARGET_NR_msync
6892 case TARGET_NR_msync
:
6893 ret
= get_errno(msync(g2h(arg1
), arg2
, arg3
));
6896 #ifdef TARGET_NR_mlock
6897 case TARGET_NR_mlock
:
6898 ret
= get_errno(mlock(g2h(arg1
), arg2
));
6901 #ifdef TARGET_NR_munlock
6902 case TARGET_NR_munlock
:
6903 ret
= get_errno(munlock(g2h(arg1
), arg2
));
6906 #ifdef TARGET_NR_mlockall
6907 case TARGET_NR_mlockall
:
6908 ret
= get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
6911 #ifdef TARGET_NR_munlockall
6912 case TARGET_NR_munlockall
:
6913 ret
= get_errno(munlockall());
6916 case TARGET_NR_truncate
:
6917 if (!(p
= lock_user_string(arg1
)))
6919 ret
= get_errno(truncate(p
, arg2
));
6920 unlock_user(p
, arg1
, 0);
6922 case TARGET_NR_ftruncate
:
6923 ret
= get_errno(ftruncate(arg1
, arg2
));
6925 case TARGET_NR_fchmod
:
6926 ret
= get_errno(fchmod(arg1
, arg2
));
6928 #if defined(TARGET_NR_fchmodat)
6929 case TARGET_NR_fchmodat
:
6930 if (!(p
= lock_user_string(arg2
)))
6932 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
6933 unlock_user(p
, arg2
, 0);
6936 case TARGET_NR_getpriority
:
6937 /* Note that negative values are valid for getpriority, so we must
6938 differentiate based on errno settings. */
6940 ret
= getpriority(arg1
, arg2
);
6941 if (ret
== -1 && errno
!= 0) {
6942 ret
= -host_to_target_errno(errno
);
6946 /* Return value is the unbiased priority. Signal no error. */
6947 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0;
6949 /* Return value is a biased priority to avoid negative numbers. */
6953 case TARGET_NR_setpriority
:
6954 ret
= get_errno(setpriority(arg1
, arg2
, arg3
));
6956 #ifdef TARGET_NR_profil
6957 case TARGET_NR_profil
:
6960 case TARGET_NR_statfs
:
6961 if (!(p
= lock_user_string(arg1
)))
6963 ret
= get_errno(statfs(path(p
), &stfs
));
6964 unlock_user(p
, arg1
, 0);
6966 if (!is_error(ret
)) {
6967 struct target_statfs
*target_stfs
;
6969 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
6971 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
6972 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
6973 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
6974 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
6975 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
6976 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
6977 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
6978 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
6979 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
6980 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
6981 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
6982 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
6983 unlock_user_struct(target_stfs
, arg2
, 1);
6986 case TARGET_NR_fstatfs
:
6987 ret
= get_errno(fstatfs(arg1
, &stfs
));
6988 goto convert_statfs
;
6989 #ifdef TARGET_NR_statfs64
6990 case TARGET_NR_statfs64
:
6991 if (!(p
= lock_user_string(arg1
)))
6993 ret
= get_errno(statfs(path(p
), &stfs
));
6994 unlock_user(p
, arg1
, 0);
6996 if (!is_error(ret
)) {
6997 struct target_statfs64
*target_stfs
;
6999 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
7001 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
7002 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
7003 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
7004 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
7005 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
7006 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
7007 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
7008 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
7009 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
7010 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
7011 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
7012 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
7013 unlock_user_struct(target_stfs
, arg3
, 1);
7016 case TARGET_NR_fstatfs64
:
7017 ret
= get_errno(fstatfs(arg1
, &stfs
));
7018 goto convert_statfs64
;
7020 #ifdef TARGET_NR_ioperm
7021 case TARGET_NR_ioperm
:
7024 #ifdef TARGET_NR_socketcall
7025 case TARGET_NR_socketcall
:
7026 ret
= do_socketcall(arg1
, arg2
);
7029 #ifdef TARGET_NR_accept
7030 case TARGET_NR_accept
:
7031 ret
= do_accept4(arg1
, arg2
, arg3
, 0);
7034 #ifdef TARGET_NR_accept4
7035 case TARGET_NR_accept4
:
7036 #ifdef CONFIG_ACCEPT4
7037 ret
= do_accept4(arg1
, arg2
, arg3
, arg4
);
7043 #ifdef TARGET_NR_bind
7044 case TARGET_NR_bind
:
7045 ret
= do_bind(arg1
, arg2
, arg3
);
7048 #ifdef TARGET_NR_connect
7049 case TARGET_NR_connect
:
7050 ret
= do_connect(arg1
, arg2
, arg3
);
7053 #ifdef TARGET_NR_getpeername
7054 case TARGET_NR_getpeername
:
7055 ret
= do_getpeername(arg1
, arg2
, arg3
);
7058 #ifdef TARGET_NR_getsockname
7059 case TARGET_NR_getsockname
:
7060 ret
= do_getsockname(arg1
, arg2
, arg3
);
7063 #ifdef TARGET_NR_getsockopt
7064 case TARGET_NR_getsockopt
:
7065 ret
= do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
7068 #ifdef TARGET_NR_listen
7069 case TARGET_NR_listen
:
7070 ret
= get_errno(listen(arg1
, arg2
));
7073 #ifdef TARGET_NR_recv
7074 case TARGET_NR_recv
:
7075 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
7078 #ifdef TARGET_NR_recvfrom
7079 case TARGET_NR_recvfrom
:
7080 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7083 #ifdef TARGET_NR_recvmsg
7084 case TARGET_NR_recvmsg
:
7085 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
7088 #ifdef TARGET_NR_send
7089 case TARGET_NR_send
:
7090 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
7093 #ifdef TARGET_NR_sendmsg
7094 case TARGET_NR_sendmsg
:
7095 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
7098 #ifdef TARGET_NR_sendmmsg
7099 case TARGET_NR_sendmmsg
:
7100 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
7102 case TARGET_NR_recvmmsg
:
7103 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
7106 #ifdef TARGET_NR_sendto
7107 case TARGET_NR_sendto
:
7108 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7111 #ifdef TARGET_NR_shutdown
7112 case TARGET_NR_shutdown
:
7113 ret
= get_errno(shutdown(arg1
, arg2
));
7116 #ifdef TARGET_NR_socket
7117 case TARGET_NR_socket
:
7118 ret
= do_socket(arg1
, arg2
, arg3
);
7121 #ifdef TARGET_NR_socketpair
7122 case TARGET_NR_socketpair
:
7123 ret
= do_socketpair(arg1
, arg2
, arg3
, arg4
);
7126 #ifdef TARGET_NR_setsockopt
7127 case TARGET_NR_setsockopt
:
7128 ret
= do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
7132 case TARGET_NR_syslog
:
7133 if (!(p
= lock_user_string(arg2
)))
7135 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
7136 unlock_user(p
, arg2
, 0);
7139 case TARGET_NR_setitimer
:
7141 struct itimerval value
, ovalue
, *pvalue
;
7145 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
7146 || copy_from_user_timeval(&pvalue
->it_value
,
7147 arg2
+ sizeof(struct target_timeval
)))
7152 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
7153 if (!is_error(ret
) && arg3
) {
7154 if (copy_to_user_timeval(arg3
,
7155 &ovalue
.it_interval
)
7156 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
7162 case TARGET_NR_getitimer
:
7164 struct itimerval value
;
7166 ret
= get_errno(getitimer(arg1
, &value
));
7167 if (!is_error(ret
) && arg2
) {
7168 if (copy_to_user_timeval(arg2
,
7170 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
7176 case TARGET_NR_stat
:
7177 if (!(p
= lock_user_string(arg1
)))
7179 ret
= get_errno(stat(path(p
), &st
));
7180 unlock_user(p
, arg1
, 0);
7182 case TARGET_NR_lstat
:
7183 if (!(p
= lock_user_string(arg1
)))
7185 ret
= get_errno(lstat(path(p
), &st
));
7186 unlock_user(p
, arg1
, 0);
7188 case TARGET_NR_fstat
:
7190 ret
= get_errno(fstat(arg1
, &st
));
7192 if (!is_error(ret
)) {
7193 struct target_stat
*target_st
;
7195 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
7197 memset(target_st
, 0, sizeof(*target_st
));
7198 __put_user(st
.st_dev
, &target_st
->st_dev
);
7199 __put_user(st
.st_ino
, &target_st
->st_ino
);
7200 __put_user(st
.st_mode
, &target_st
->st_mode
);
7201 __put_user(st
.st_uid
, &target_st
->st_uid
);
7202 __put_user(st
.st_gid
, &target_st
->st_gid
);
7203 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
7204 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
7205 __put_user(st
.st_size
, &target_st
->st_size
);
7206 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
7207 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
7208 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
7209 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
7210 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
7211 unlock_user_struct(target_st
, arg2
, 1);
7215 #ifdef TARGET_NR_olduname
7216 case TARGET_NR_olduname
:
7219 #ifdef TARGET_NR_iopl
7220 case TARGET_NR_iopl
:
7223 case TARGET_NR_vhangup
:
7224 ret
= get_errno(vhangup());
7226 #ifdef TARGET_NR_idle
7227 case TARGET_NR_idle
:
7230 #ifdef TARGET_NR_syscall
7231 case TARGET_NR_syscall
:
7232 ret
= do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
7233 arg6
, arg7
, arg8
, 0);
7236 case TARGET_NR_wait4
:
7239 abi_long status_ptr
= arg2
;
7240 struct rusage rusage
, *rusage_ptr
;
7241 abi_ulong target_rusage
= arg4
;
7242 abi_long rusage_err
;
7244 rusage_ptr
= &rusage
;
7247 ret
= get_errno(wait4(arg1
, &status
, arg3
, rusage_ptr
));
7248 if (!is_error(ret
)) {
7249 if (status_ptr
&& ret
) {
7250 status
= host_to_target_waitstatus(status
);
7251 if (put_user_s32(status
, status_ptr
))
7254 if (target_rusage
) {
7255 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
7263 #ifdef TARGET_NR_swapoff
7264 case TARGET_NR_swapoff
:
7265 if (!(p
= lock_user_string(arg1
)))
7267 ret
= get_errno(swapoff(p
));
7268 unlock_user(p
, arg1
, 0);
7271 case TARGET_NR_sysinfo
:
7273 struct target_sysinfo
*target_value
;
7274 struct sysinfo value
;
7275 ret
= get_errno(sysinfo(&value
));
7276 if (!is_error(ret
) && arg1
)
7278 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
7280 __put_user(value
.uptime
, &target_value
->uptime
);
7281 __put_user(value
.loads
[0], &target_value
->loads
[0]);
7282 __put_user(value
.loads
[1], &target_value
->loads
[1]);
7283 __put_user(value
.loads
[2], &target_value
->loads
[2]);
7284 __put_user(value
.totalram
, &target_value
->totalram
);
7285 __put_user(value
.freeram
, &target_value
->freeram
);
7286 __put_user(value
.sharedram
, &target_value
->sharedram
);
7287 __put_user(value
.bufferram
, &target_value
->bufferram
);
7288 __put_user(value
.totalswap
, &target_value
->totalswap
);
7289 __put_user(value
.freeswap
, &target_value
->freeswap
);
7290 __put_user(value
.procs
, &target_value
->procs
);
7291 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
7292 __put_user(value
.freehigh
, &target_value
->freehigh
);
7293 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
7294 unlock_user_struct(target_value
, arg1
, 1);
7298 #ifdef TARGET_NR_ipc
7300 ret
= do_ipc(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7303 #ifdef TARGET_NR_semget
7304 case TARGET_NR_semget
:
7305 ret
= get_errno(semget(arg1
, arg2
, arg3
));
7308 #ifdef TARGET_NR_semop
7309 case TARGET_NR_semop
:
7310 ret
= do_semop(arg1
, arg2
, arg3
);
7313 #ifdef TARGET_NR_semctl
7314 case TARGET_NR_semctl
:
7315 ret
= do_semctl(arg1
, arg2
, arg3
, (union target_semun
)(abi_ulong
)arg4
);
7318 #ifdef TARGET_NR_msgctl
7319 case TARGET_NR_msgctl
:
7320 ret
= do_msgctl(arg1
, arg2
, arg3
);
7323 #ifdef TARGET_NR_msgget
7324 case TARGET_NR_msgget
:
7325 ret
= get_errno(msgget(arg1
, arg2
));
7328 #ifdef TARGET_NR_msgrcv
7329 case TARGET_NR_msgrcv
:
7330 ret
= do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
7333 #ifdef TARGET_NR_msgsnd
7334 case TARGET_NR_msgsnd
:
7335 ret
= do_msgsnd(arg1
, arg2
, arg3
, arg4
);
7338 #ifdef TARGET_NR_shmget
7339 case TARGET_NR_shmget
:
7340 ret
= get_errno(shmget(arg1
, arg2
, arg3
));
7343 #ifdef TARGET_NR_shmctl
7344 case TARGET_NR_shmctl
:
7345 ret
= do_shmctl(arg1
, arg2
, arg3
);
7348 #ifdef TARGET_NR_shmat
7349 case TARGET_NR_shmat
:
7350 ret
= do_shmat(arg1
, arg2
, arg3
);
7353 #ifdef TARGET_NR_shmdt
7354 case TARGET_NR_shmdt
:
7355 ret
= do_shmdt(arg1
);
7358 case TARGET_NR_fsync
:
7359 ret
= get_errno(fsync(arg1
));
7361 case TARGET_NR_clone
:
7362 /* Linux manages to have three different orderings for its
7363 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7364 * match the kernel's CONFIG_CLONE_* settings.
7365 * Microblaze is further special in that it uses a sixth
7366 * implicit argument to clone for the TLS pointer.
7368 #if defined(TARGET_MICROBLAZE)
7369 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
7370 #elif defined(TARGET_CLONE_BACKWARDS)
7371 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
7372 #elif defined(TARGET_CLONE_BACKWARDS2)
7373 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
7375 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
7378 #ifdef __NR_exit_group
7379 /* new thread calls */
7380 case TARGET_NR_exit_group
:
7384 gdb_exit(cpu_env
, arg1
);
7385 ret
= get_errno(exit_group(arg1
));
7388 case TARGET_NR_setdomainname
:
7389 if (!(p
= lock_user_string(arg1
)))
7391 ret
= get_errno(setdomainname(p
, arg2
));
7392 unlock_user(p
, arg1
, 0);
7394 case TARGET_NR_uname
:
7395 /* no need to transcode because we use the linux syscall */
7397 struct new_utsname
* buf
;
7399 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
7401 ret
= get_errno(sys_uname(buf
));
7402 if (!is_error(ret
)) {
7403 /* Overrite the native machine name with whatever is being
7405 strcpy (buf
->machine
, cpu_to_uname_machine(cpu_env
));
7406 /* Allow the user to override the reported release. */
7407 if (qemu_uname_release
&& *qemu_uname_release
)
7408 strcpy (buf
->release
, qemu_uname_release
);
7410 unlock_user_struct(buf
, arg1
, 1);
7414 case TARGET_NR_modify_ldt
:
7415 ret
= do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
7417 #if !defined(TARGET_X86_64)
7418 case TARGET_NR_vm86old
:
7420 case TARGET_NR_vm86
:
7421 ret
= do_vm86(cpu_env
, arg1
, arg2
);
7425 case TARGET_NR_adjtimex
:
7427 #ifdef TARGET_NR_create_module
7428 case TARGET_NR_create_module
:
7430 case TARGET_NR_init_module
:
7431 case TARGET_NR_delete_module
:
7432 #ifdef TARGET_NR_get_kernel_syms
7433 case TARGET_NR_get_kernel_syms
:
7436 case TARGET_NR_quotactl
:
7438 case TARGET_NR_getpgid
:
7439 ret
= get_errno(getpgid(arg1
));
7441 case TARGET_NR_fchdir
:
7442 ret
= get_errno(fchdir(arg1
));
7444 #ifdef TARGET_NR_bdflush /* not on x86_64 */
7445 case TARGET_NR_bdflush
:
7448 #ifdef TARGET_NR_sysfs
7449 case TARGET_NR_sysfs
:
7452 case TARGET_NR_personality
:
7453 ret
= get_errno(personality(arg1
));
7455 #ifdef TARGET_NR_afs_syscall
7456 case TARGET_NR_afs_syscall
:
7459 #ifdef TARGET_NR__llseek /* Not on alpha */
7460 case TARGET_NR__llseek
:
7463 #if !defined(__NR_llseek)
7464 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | arg3
, arg5
);
7466 ret
= get_errno(res
);
7471 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
7473 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
7479 case TARGET_NR_getdents
:
7480 #ifdef __NR_getdents
7481 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
7483 struct target_dirent
*target_dirp
;
7484 struct linux_dirent
*dirp
;
7485 abi_long count
= arg3
;
7487 dirp
= malloc(count
);
7489 ret
= -TARGET_ENOMEM
;
7493 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
7494 if (!is_error(ret
)) {
7495 struct linux_dirent
*de
;
7496 struct target_dirent
*tde
;
7498 int reclen
, treclen
;
7499 int count1
, tnamelen
;
7503 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7507 reclen
= de
->d_reclen
;
7508 tnamelen
= reclen
- offsetof(struct linux_dirent
, d_name
);
7509 assert(tnamelen
>= 0);
7510 treclen
= tnamelen
+ offsetof(struct target_dirent
, d_name
);
7511 assert(count1
+ treclen
<= count
);
7512 tde
->d_reclen
= tswap16(treclen
);
7513 tde
->d_ino
= tswapal(de
->d_ino
);
7514 tde
->d_off
= tswapal(de
->d_off
);
7515 memcpy(tde
->d_name
, de
->d_name
, tnamelen
);
7516 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
7518 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
7522 unlock_user(target_dirp
, arg2
, ret
);
7528 struct linux_dirent
*dirp
;
7529 abi_long count
= arg3
;
7531 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7533 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
7534 if (!is_error(ret
)) {
7535 struct linux_dirent
*de
;
7540 reclen
= de
->d_reclen
;
7543 de
->d_reclen
= tswap16(reclen
);
7544 tswapls(&de
->d_ino
);
7545 tswapls(&de
->d_off
);
7546 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
7550 unlock_user(dirp
, arg2
, ret
);
7554 /* Implement getdents in terms of getdents64 */
7556 struct linux_dirent64
*dirp
;
7557 abi_long count
= arg3
;
7559 dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
7563 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
7564 if (!is_error(ret
)) {
7565 /* Convert the dirent64 structs to target dirent. We do this
7566 * in-place, since we can guarantee that a target_dirent is no
7567 * larger than a dirent64; however this means we have to be
7568 * careful to read everything before writing in the new format.
7570 struct linux_dirent64
*de
;
7571 struct target_dirent
*tde
;
7576 tde
= (struct target_dirent
*)dirp
;
7578 int namelen
, treclen
;
7579 int reclen
= de
->d_reclen
;
7580 uint64_t ino
= de
->d_ino
;
7581 int64_t off
= de
->d_off
;
7582 uint8_t type
= de
->d_type
;
7584 namelen
= strlen(de
->d_name
);
7585 treclen
= offsetof(struct target_dirent
, d_name
)
7587 treclen
= QEMU_ALIGN_UP(treclen
, sizeof(abi_long
));
7589 memmove(tde
->d_name
, de
->d_name
, namelen
+ 1);
7590 tde
->d_ino
= tswapal(ino
);
7591 tde
->d_off
= tswapal(off
);
7592 tde
->d_reclen
= tswap16(treclen
);
7593 /* The target_dirent type is in what was formerly a padding
7594 * byte at the end of the structure:
7596 *(((char *)tde
) + treclen
- 1) = type
;
7598 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
7599 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
7605 unlock_user(dirp
, arg2
, ret
);
7609 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
7610 case TARGET_NR_getdents64
:
7612 struct linux_dirent64
*dirp
;
7613 abi_long count
= arg3
;
7614 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7616 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
7617 if (!is_error(ret
)) {
7618 struct linux_dirent64
*de
;
7623 reclen
= de
->d_reclen
;
7626 de
->d_reclen
= tswap16(reclen
);
7627 tswap64s((uint64_t *)&de
->d_ino
);
7628 tswap64s((uint64_t *)&de
->d_off
);
7629 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
7633 unlock_user(dirp
, arg2
, ret
);
7636 #endif /* TARGET_NR_getdents64 */
7637 #if defined(TARGET_NR__newselect)
7638 case TARGET_NR__newselect
:
7639 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
7642 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7643 # ifdef TARGET_NR_poll
7644 case TARGET_NR_poll
:
7646 # ifdef TARGET_NR_ppoll
7647 case TARGET_NR_ppoll
:
7650 struct target_pollfd
*target_pfd
;
7651 unsigned int nfds
= arg2
;
7656 target_pfd
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_pollfd
) * nfds
, 1);
7660 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
7661 for(i
= 0; i
< nfds
; i
++) {
7662 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
7663 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
7666 # ifdef TARGET_NR_ppoll
7667 if (num
== TARGET_NR_ppoll
) {
7668 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
7669 target_sigset_t
*target_set
;
7670 sigset_t _set
, *set
= &_set
;
7673 if (target_to_host_timespec(timeout_ts
, arg3
)) {
7674 unlock_user(target_pfd
, arg1
, 0);
7682 target_set
= lock_user(VERIFY_READ
, arg4
, sizeof(target_sigset_t
), 1);
7684 unlock_user(target_pfd
, arg1
, 0);
7687 target_to_host_sigset(set
, target_set
);
7692 ret
= get_errno(sys_ppoll(pfd
, nfds
, timeout_ts
, set
, _NSIG
/8));
7694 if (!is_error(ret
) && arg3
) {
7695 host_to_target_timespec(arg3
, timeout_ts
);
7698 unlock_user(target_set
, arg4
, 0);
7702 ret
= get_errno(poll(pfd
, nfds
, timeout
));
7704 if (!is_error(ret
)) {
7705 for(i
= 0; i
< nfds
; i
++) {
7706 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
7709 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
7713 case TARGET_NR_flock
:
7714 /* NOTE: the flock constant seems to be the same for every
7716 ret
= get_errno(flock(arg1
, arg2
));
7718 case TARGET_NR_readv
:
7720 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
7722 ret
= get_errno(readv(arg1
, vec
, arg3
));
7723 unlock_iovec(vec
, arg2
, arg3
, 1);
7725 ret
= -host_to_target_errno(errno
);
7729 case TARGET_NR_writev
:
7731 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
7733 ret
= get_errno(writev(arg1
, vec
, arg3
));
7734 unlock_iovec(vec
, arg2
, arg3
, 0);
7736 ret
= -host_to_target_errno(errno
);
7740 case TARGET_NR_getsid
:
7741 ret
= get_errno(getsid(arg1
));
7743 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
7744 case TARGET_NR_fdatasync
:
7745 ret
= get_errno(fdatasync(arg1
));
7748 case TARGET_NR__sysctl
:
7749 /* We don't implement this, but ENOTDIR is always a safe
7751 ret
= -TARGET_ENOTDIR
;
7753 case TARGET_NR_sched_getaffinity
:
7755 unsigned int mask_size
;
7756 unsigned long *mask
;
7759 * sched_getaffinity needs multiples of ulong, so need to take
7760 * care of mismatches between target ulong and host ulong sizes.
7762 if (arg2
& (sizeof(abi_ulong
) - 1)) {
7763 ret
= -TARGET_EINVAL
;
7766 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
7768 mask
= alloca(mask_size
);
7769 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
7771 if (!is_error(ret
)) {
7773 /* More data returned than the caller's buffer will fit.
7774 * This only happens if sizeof(abi_long) < sizeof(long)
7775 * and the caller passed us a buffer holding an odd number
7776 * of abi_longs. If the host kernel is actually using the
7777 * extra 4 bytes then fail EINVAL; otherwise we can just
7778 * ignore them and only copy the interesting part.
7780 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
7781 if (numcpus
> arg2
* 8) {
7782 ret
= -TARGET_EINVAL
;
7788 if (copy_to_user(arg3
, mask
, ret
)) {
7794 case TARGET_NR_sched_setaffinity
:
7796 unsigned int mask_size
;
7797 unsigned long *mask
;
7800 * sched_setaffinity needs multiples of ulong, so need to take
7801 * care of mismatches between target ulong and host ulong sizes.
7803 if (arg2
& (sizeof(abi_ulong
) - 1)) {
7804 ret
= -TARGET_EINVAL
;
7807 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
7809 mask
= alloca(mask_size
);
7810 if (!lock_user_struct(VERIFY_READ
, p
, arg3
, 1)) {
7813 memcpy(mask
, p
, arg2
);
7814 unlock_user_struct(p
, arg2
, 0);
7816 ret
= get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
7819 case TARGET_NR_sched_setparam
:
7821 struct sched_param
*target_schp
;
7822 struct sched_param schp
;
7825 return -TARGET_EINVAL
;
7827 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
7829 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
7830 unlock_user_struct(target_schp
, arg2
, 0);
7831 ret
= get_errno(sched_setparam(arg1
, &schp
));
7834 case TARGET_NR_sched_getparam
:
7836 struct sched_param
*target_schp
;
7837 struct sched_param schp
;
7840 return -TARGET_EINVAL
;
7842 ret
= get_errno(sched_getparam(arg1
, &schp
));
7843 if (!is_error(ret
)) {
7844 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
7846 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
7847 unlock_user_struct(target_schp
, arg2
, 1);
7851 case TARGET_NR_sched_setscheduler
:
7853 struct sched_param
*target_schp
;
7854 struct sched_param schp
;
7856 return -TARGET_EINVAL
;
7858 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
7860 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
7861 unlock_user_struct(target_schp
, arg3
, 0);
7862 ret
= get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
7865 case TARGET_NR_sched_getscheduler
:
7866 ret
= get_errno(sched_getscheduler(arg1
));
7868 case TARGET_NR_sched_yield
:
7869 ret
= get_errno(sched_yield());
7871 case TARGET_NR_sched_get_priority_max
:
7872 ret
= get_errno(sched_get_priority_max(arg1
));
7874 case TARGET_NR_sched_get_priority_min
:
7875 ret
= get_errno(sched_get_priority_min(arg1
));
7877 case TARGET_NR_sched_rr_get_interval
:
7880 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
7881 if (!is_error(ret
)) {
7882 ret
= host_to_target_timespec(arg2
, &ts
);
7886 case TARGET_NR_nanosleep
:
7888 struct timespec req
, rem
;
7889 target_to_host_timespec(&req
, arg1
);
7890 ret
= get_errno(nanosleep(&req
, &rem
));
7891 if (is_error(ret
) && arg2
) {
7892 host_to_target_timespec(arg2
, &rem
);
7896 #ifdef TARGET_NR_query_module
7897 case TARGET_NR_query_module
:
7900 #ifdef TARGET_NR_nfsservctl
7901 case TARGET_NR_nfsservctl
:
7904 case TARGET_NR_prctl
:
7906 case PR_GET_PDEATHSIG
:
7909 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
7910 if (!is_error(ret
) && arg2
7911 && put_user_ual(deathsig
, arg2
)) {
7919 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
7923 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
7925 unlock_user(name
, arg2
, 16);
7930 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
7934 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
7936 unlock_user(name
, arg2
, 0);
7941 /* Most prctl options have no pointer arguments */
7942 ret
= get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
7946 #ifdef TARGET_NR_arch_prctl
7947 case TARGET_NR_arch_prctl
:
7948 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
7949 ret
= do_arch_prctl(cpu_env
, arg1
, arg2
);
7955 #ifdef TARGET_NR_pread64
7956 case TARGET_NR_pread64
:
7957 if (regpairs_aligned(cpu_env
)) {
7961 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
7963 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
7964 unlock_user(p
, arg2
, ret
);
7966 case TARGET_NR_pwrite64
:
7967 if (regpairs_aligned(cpu_env
)) {
7971 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
7973 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
7974 unlock_user(p
, arg2
, 0);
7977 case TARGET_NR_getcwd
:
7978 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
7980 ret
= get_errno(sys_getcwd1(p
, arg2
));
7981 unlock_user(p
, arg1
, ret
);
7983 case TARGET_NR_capget
:
7984 case TARGET_NR_capset
:
7986 struct target_user_cap_header
*target_header
;
7987 struct target_user_cap_data
*target_data
= NULL
;
7988 struct __user_cap_header_struct header
;
7989 struct __user_cap_data_struct data
[2];
7990 struct __user_cap_data_struct
*dataptr
= NULL
;
7991 int i
, target_datalen
;
7994 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
7997 header
.version
= tswap32(target_header
->version
);
7998 header
.pid
= tswap32(target_header
->pid
);
8000 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
8001 /* Version 2 and up takes pointer to two user_data structs */
8005 target_datalen
= sizeof(*target_data
) * data_items
;
8008 if (num
== TARGET_NR_capget
) {
8009 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
8011 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
8014 unlock_user_struct(target_header
, arg1
, 0);
8018 if (num
== TARGET_NR_capset
) {
8019 for (i
= 0; i
< data_items
; i
++) {
8020 data
[i
].effective
= tswap32(target_data
[i
].effective
);
8021 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
8022 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
8029 if (num
== TARGET_NR_capget
) {
8030 ret
= get_errno(capget(&header
, dataptr
));
8032 ret
= get_errno(capset(&header
, dataptr
));
8035 /* The kernel always updates version for both capget and capset */
8036 target_header
->version
= tswap32(header
.version
);
8037 unlock_user_struct(target_header
, arg1
, 1);
8040 if (num
== TARGET_NR_capget
) {
8041 for (i
= 0; i
< data_items
; i
++) {
8042 target_data
[i
].effective
= tswap32(data
[i
].effective
);
8043 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
8044 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
8046 unlock_user(target_data
, arg2
, target_datalen
);
8048 unlock_user(target_data
, arg2
, 0);
8053 case TARGET_NR_sigaltstack
:
8054 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
8055 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
8056 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
8057 ret
= do_sigaltstack(arg1
, arg2
, get_sp_from_cpustate((CPUArchState
*)cpu_env
));
8063 #ifdef CONFIG_SENDFILE
8064 case TARGET_NR_sendfile
:
8069 ret
= get_user_sal(off
, arg3
);
8070 if (is_error(ret
)) {
8075 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
8076 if (!is_error(ret
) && arg3
) {
8077 abi_long ret2
= put_user_sal(off
, arg3
);
8078 if (is_error(ret2
)) {
8084 #ifdef TARGET_NR_sendfile64
8085 case TARGET_NR_sendfile64
:
8090 ret
= get_user_s64(off
, arg3
);
8091 if (is_error(ret
)) {
8096 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
8097 if (!is_error(ret
) && arg3
) {
8098 abi_long ret2
= put_user_s64(off
, arg3
);
8099 if (is_error(ret2
)) {
8107 case TARGET_NR_sendfile
:
8108 #ifdef TARGET_NR_sendfile64
8109 case TARGET_NR_sendfile64
:
8114 #ifdef TARGET_NR_getpmsg
8115 case TARGET_NR_getpmsg
:
8118 #ifdef TARGET_NR_putpmsg
8119 case TARGET_NR_putpmsg
:
8122 #ifdef TARGET_NR_vfork
8123 case TARGET_NR_vfork
:
8124 ret
= get_errno(do_fork(cpu_env
, CLONE_VFORK
| CLONE_VM
| SIGCHLD
,
8128 #ifdef TARGET_NR_ugetrlimit
8129 case TARGET_NR_ugetrlimit
:
8132 int resource
= target_to_host_resource(arg1
);
8133 ret
= get_errno(getrlimit(resource
, &rlim
));
8134 if (!is_error(ret
)) {
8135 struct target_rlimit
*target_rlim
;
8136 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
8138 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
8139 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
8140 unlock_user_struct(target_rlim
, arg2
, 1);
8145 #ifdef TARGET_NR_truncate64
8146 case TARGET_NR_truncate64
:
8147 if (!(p
= lock_user_string(arg1
)))
8149 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
8150 unlock_user(p
, arg1
, 0);
8153 #ifdef TARGET_NR_ftruncate64
8154 case TARGET_NR_ftruncate64
:
8155 ret
= target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
8158 #ifdef TARGET_NR_stat64
8159 case TARGET_NR_stat64
:
8160 if (!(p
= lock_user_string(arg1
)))
8162 ret
= get_errno(stat(path(p
), &st
));
8163 unlock_user(p
, arg1
, 0);
8165 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
8168 #ifdef TARGET_NR_lstat64
8169 case TARGET_NR_lstat64
:
8170 if (!(p
= lock_user_string(arg1
)))
8172 ret
= get_errno(lstat(path(p
), &st
));
8173 unlock_user(p
, arg1
, 0);
8175 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
8178 #ifdef TARGET_NR_fstat64
8179 case TARGET_NR_fstat64
:
8180 ret
= get_errno(fstat(arg1
, &st
));
8182 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
8185 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
8186 #ifdef TARGET_NR_fstatat64
8187 case TARGET_NR_fstatat64
:
8189 #ifdef TARGET_NR_newfstatat
8190 case TARGET_NR_newfstatat
:
8192 if (!(p
= lock_user_string(arg2
)))
8194 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
8196 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
8199 case TARGET_NR_lchown
:
8200 if (!(p
= lock_user_string(arg1
)))
8202 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
8203 unlock_user(p
, arg1
, 0);
8205 #ifdef TARGET_NR_getuid
8206 case TARGET_NR_getuid
:
8207 ret
= get_errno(high2lowuid(getuid()));
8210 #ifdef TARGET_NR_getgid
8211 case TARGET_NR_getgid
:
8212 ret
= get_errno(high2lowgid(getgid()));
8215 #ifdef TARGET_NR_geteuid
8216 case TARGET_NR_geteuid
:
8217 ret
= get_errno(high2lowuid(geteuid()));
8220 #ifdef TARGET_NR_getegid
8221 case TARGET_NR_getegid
:
8222 ret
= get_errno(high2lowgid(getegid()));
8225 case TARGET_NR_setreuid
:
8226 ret
= get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
8228 case TARGET_NR_setregid
:
8229 ret
= get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
8231 case TARGET_NR_getgroups
:
8233 int gidsetsize
= arg1
;
8234 target_id
*target_grouplist
;
8238 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8239 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
8240 if (gidsetsize
== 0)
8242 if (!is_error(ret
)) {
8243 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
8244 if (!target_grouplist
)
8246 for(i
= 0;i
< ret
; i
++)
8247 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
8248 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
8252 case TARGET_NR_setgroups
:
8254 int gidsetsize
= arg1
;
8255 target_id
*target_grouplist
;
8256 gid_t
*grouplist
= NULL
;
8259 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8260 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
8261 if (!target_grouplist
) {
8262 ret
= -TARGET_EFAULT
;
8265 for (i
= 0; i
< gidsetsize
; i
++) {
8266 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
8268 unlock_user(target_grouplist
, arg2
, 0);
8270 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
8273 case TARGET_NR_fchown
:
8274 ret
= get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
8276 #if defined(TARGET_NR_fchownat)
8277 case TARGET_NR_fchownat
:
8278 if (!(p
= lock_user_string(arg2
)))
8280 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
8281 low2highgid(arg4
), arg5
));
8282 unlock_user(p
, arg2
, 0);
8285 #ifdef TARGET_NR_setresuid
8286 case TARGET_NR_setresuid
:
8287 ret
= get_errno(setresuid(low2highuid(arg1
),
8289 low2highuid(arg3
)));
8292 #ifdef TARGET_NR_getresuid
8293 case TARGET_NR_getresuid
:
8295 uid_t ruid
, euid
, suid
;
8296 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
8297 if (!is_error(ret
)) {
8298 if (put_user_id(high2lowuid(ruid
), arg1
)
8299 || put_user_id(high2lowuid(euid
), arg2
)
8300 || put_user_id(high2lowuid(suid
), arg3
))
8306 #ifdef TARGET_NR_getresgid
8307 case TARGET_NR_setresgid
:
8308 ret
= get_errno(setresgid(low2highgid(arg1
),
8310 low2highgid(arg3
)));
8313 #ifdef TARGET_NR_getresgid
8314 case TARGET_NR_getresgid
:
8316 gid_t rgid
, egid
, sgid
;
8317 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
8318 if (!is_error(ret
)) {
8319 if (put_user_id(high2lowgid(rgid
), arg1
)
8320 || put_user_id(high2lowgid(egid
), arg2
)
8321 || put_user_id(high2lowgid(sgid
), arg3
))
8327 case TARGET_NR_chown
:
8328 if (!(p
= lock_user_string(arg1
)))
8330 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
8331 unlock_user(p
, arg1
, 0);
8333 case TARGET_NR_setuid
:
8334 ret
= get_errno(setuid(low2highuid(arg1
)));
8336 case TARGET_NR_setgid
:
8337 ret
= get_errno(setgid(low2highgid(arg1
)));
8339 case TARGET_NR_setfsuid
:
8340 ret
= get_errno(setfsuid(arg1
));
8342 case TARGET_NR_setfsgid
:
8343 ret
= get_errno(setfsgid(arg1
));
8346 #ifdef TARGET_NR_lchown32
8347 case TARGET_NR_lchown32
:
8348 if (!(p
= lock_user_string(arg1
)))
8350 ret
= get_errno(lchown(p
, arg2
, arg3
));
8351 unlock_user(p
, arg1
, 0);
8354 #ifdef TARGET_NR_getuid32
8355 case TARGET_NR_getuid32
:
8356 ret
= get_errno(getuid());
8360 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
8361 /* Alpha specific */
8362 case TARGET_NR_getxuid
:
8366 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
8368 ret
= get_errno(getuid());
8371 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
8372 /* Alpha specific */
8373 case TARGET_NR_getxgid
:
8377 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
8379 ret
= get_errno(getgid());
8382 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
8383 /* Alpha specific */
8384 case TARGET_NR_osf_getsysinfo
:
8385 ret
= -TARGET_EOPNOTSUPP
;
8387 case TARGET_GSI_IEEE_FP_CONTROL
:
8389 uint64_t swcr
, fpcr
= cpu_alpha_load_fpcr (cpu_env
);
8391 /* Copied from linux ieee_fpcr_to_swcr. */
8392 swcr
= (fpcr
>> 35) & SWCR_STATUS_MASK
;
8393 swcr
|= (fpcr
>> 36) & SWCR_MAP_DMZ
;
8394 swcr
|= (~fpcr
>> 48) & (SWCR_TRAP_ENABLE_INV
8395 | SWCR_TRAP_ENABLE_DZE
8396 | SWCR_TRAP_ENABLE_OVF
);
8397 swcr
|= (~fpcr
>> 57) & (SWCR_TRAP_ENABLE_UNF
8398 | SWCR_TRAP_ENABLE_INE
);
8399 swcr
|= (fpcr
>> 47) & SWCR_MAP_UMZ
;
8400 swcr
|= (~fpcr
>> 41) & SWCR_TRAP_ENABLE_DNO
;
8402 if (put_user_u64 (swcr
, arg2
))
8408 /* case GSI_IEEE_STATE_AT_SIGNAL:
8409 -- Not implemented in linux kernel.
8411 -- Retrieves current unaligned access state; not much used.
8413 -- Retrieves implver information; surely not used.
8415 -- Grabs a copy of the HWRPB; surely not used.
8420 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8421 /* Alpha specific */
8422 case TARGET_NR_osf_setsysinfo
:
8423 ret
= -TARGET_EOPNOTSUPP
;
8425 case TARGET_SSI_IEEE_FP_CONTROL
:
8427 uint64_t swcr
, fpcr
, orig_fpcr
;
8429 if (get_user_u64 (swcr
, arg2
)) {
8432 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
8433 fpcr
= orig_fpcr
& FPCR_DYN_MASK
;
8435 /* Copied from linux ieee_swcr_to_fpcr. */
8436 fpcr
|= (swcr
& SWCR_STATUS_MASK
) << 35;
8437 fpcr
|= (swcr
& SWCR_MAP_DMZ
) << 36;
8438 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_INV
8439 | SWCR_TRAP_ENABLE_DZE
8440 | SWCR_TRAP_ENABLE_OVF
)) << 48;
8441 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_UNF
8442 | SWCR_TRAP_ENABLE_INE
)) << 57;
8443 fpcr
|= (swcr
& SWCR_MAP_UMZ
? FPCR_UNDZ
| FPCR_UNFD
: 0);
8444 fpcr
|= (~swcr
& SWCR_TRAP_ENABLE_DNO
) << 41;
8446 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
8451 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
8453 uint64_t exc
, fpcr
, orig_fpcr
;
8456 if (get_user_u64(exc
, arg2
)) {
8460 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
8462 /* We only add to the exception status here. */
8463 fpcr
= orig_fpcr
| ((exc
& SWCR_STATUS_MASK
) << 35);
8465 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
8468 /* Old exceptions are not signaled. */
8469 fpcr
&= ~(orig_fpcr
& FPCR_STATUS_MASK
);
8471 /* If any exceptions set by this call,
8472 and are unmasked, send a signal. */
8474 if ((fpcr
& (FPCR_INE
| FPCR_INED
)) == FPCR_INE
) {
8475 si_code
= TARGET_FPE_FLTRES
;
8477 if ((fpcr
& (FPCR_UNF
| FPCR_UNFD
)) == FPCR_UNF
) {
8478 si_code
= TARGET_FPE_FLTUND
;
8480 if ((fpcr
& (FPCR_OVF
| FPCR_OVFD
)) == FPCR_OVF
) {
8481 si_code
= TARGET_FPE_FLTOVF
;
8483 if ((fpcr
& (FPCR_DZE
| FPCR_DZED
)) == FPCR_DZE
) {
8484 si_code
= TARGET_FPE_FLTDIV
;
8486 if ((fpcr
& (FPCR_INV
| FPCR_INVD
)) == FPCR_INV
) {
8487 si_code
= TARGET_FPE_FLTINV
;
8490 target_siginfo_t info
;
8491 info
.si_signo
= SIGFPE
;
8493 info
.si_code
= si_code
;
8494 info
._sifields
._sigfault
._addr
8495 = ((CPUArchState
*)cpu_env
)->pc
;
8496 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
8501 /* case SSI_NVPAIRS:
8502 -- Used with SSIN_UACPROC to enable unaligned accesses.
8503 case SSI_IEEE_STATE_AT_SIGNAL:
8504 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8505 -- Not implemented in linux kernel
8510 #ifdef TARGET_NR_osf_sigprocmask
8511 /* Alpha specific. */
8512 case TARGET_NR_osf_sigprocmask
:
8516 sigset_t set
, oldset
;
8519 case TARGET_SIG_BLOCK
:
8522 case TARGET_SIG_UNBLOCK
:
8525 case TARGET_SIG_SETMASK
:
8529 ret
= -TARGET_EINVAL
;
8533 target_to_host_old_sigset(&set
, &mask
);
8534 do_sigprocmask(how
, &set
, &oldset
);
8535 host_to_target_old_sigset(&mask
, &oldset
);
8541 #ifdef TARGET_NR_getgid32
8542 case TARGET_NR_getgid32
:
8543 ret
= get_errno(getgid());
8546 #ifdef TARGET_NR_geteuid32
8547 case TARGET_NR_geteuid32
:
8548 ret
= get_errno(geteuid());
8551 #ifdef TARGET_NR_getegid32
8552 case TARGET_NR_getegid32
:
8553 ret
= get_errno(getegid());
8556 #ifdef TARGET_NR_setreuid32
8557 case TARGET_NR_setreuid32
:
8558 ret
= get_errno(setreuid(arg1
, arg2
));
8561 #ifdef TARGET_NR_setregid32
8562 case TARGET_NR_setregid32
:
8563 ret
= get_errno(setregid(arg1
, arg2
));
8566 #ifdef TARGET_NR_getgroups32
8567 case TARGET_NR_getgroups32
:
8569 int gidsetsize
= arg1
;
8570 uint32_t *target_grouplist
;
8574 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8575 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
8576 if (gidsetsize
== 0)
8578 if (!is_error(ret
)) {
8579 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
8580 if (!target_grouplist
) {
8581 ret
= -TARGET_EFAULT
;
8584 for(i
= 0;i
< ret
; i
++)
8585 target_grouplist
[i
] = tswap32(grouplist
[i
]);
8586 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
8591 #ifdef TARGET_NR_setgroups32
8592 case TARGET_NR_setgroups32
:
8594 int gidsetsize
= arg1
;
8595 uint32_t *target_grouplist
;
8599 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8600 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
8601 if (!target_grouplist
) {
8602 ret
= -TARGET_EFAULT
;
8605 for(i
= 0;i
< gidsetsize
; i
++)
8606 grouplist
[i
] = tswap32(target_grouplist
[i
]);
8607 unlock_user(target_grouplist
, arg2
, 0);
8608 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
8612 #ifdef TARGET_NR_fchown32
8613 case TARGET_NR_fchown32
:
8614 ret
= get_errno(fchown(arg1
, arg2
, arg3
));
8617 #ifdef TARGET_NR_setresuid32
8618 case TARGET_NR_setresuid32
:
8619 ret
= get_errno(setresuid(arg1
, arg2
, arg3
));
8622 #ifdef TARGET_NR_getresuid32
8623 case TARGET_NR_getresuid32
:
8625 uid_t ruid
, euid
, suid
;
8626 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
8627 if (!is_error(ret
)) {
8628 if (put_user_u32(ruid
, arg1
)
8629 || put_user_u32(euid
, arg2
)
8630 || put_user_u32(suid
, arg3
))
8636 #ifdef TARGET_NR_setresgid32
8637 case TARGET_NR_setresgid32
:
8638 ret
= get_errno(setresgid(arg1
, arg2
, arg3
));
8641 #ifdef TARGET_NR_getresgid32
8642 case TARGET_NR_getresgid32
:
8644 gid_t rgid
, egid
, sgid
;
8645 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
8646 if (!is_error(ret
)) {
8647 if (put_user_u32(rgid
, arg1
)
8648 || put_user_u32(egid
, arg2
)
8649 || put_user_u32(sgid
, arg3
))
8655 #ifdef TARGET_NR_chown32
8656 case TARGET_NR_chown32
:
8657 if (!(p
= lock_user_string(arg1
)))
8659 ret
= get_errno(chown(p
, arg2
, arg3
));
8660 unlock_user(p
, arg1
, 0);
8663 #ifdef TARGET_NR_setuid32
8664 case TARGET_NR_setuid32
:
8665 ret
= get_errno(setuid(arg1
));
8668 #ifdef TARGET_NR_setgid32
8669 case TARGET_NR_setgid32
:
8670 ret
= get_errno(setgid(arg1
));
8673 #ifdef TARGET_NR_setfsuid32
8674 case TARGET_NR_setfsuid32
:
8675 ret
= get_errno(setfsuid(arg1
));
8678 #ifdef TARGET_NR_setfsgid32
8679 case TARGET_NR_setfsgid32
:
8680 ret
= get_errno(setfsgid(arg1
));
8684 case TARGET_NR_pivot_root
:
8686 #ifdef TARGET_NR_mincore
8687 case TARGET_NR_mincore
:
8690 ret
= -TARGET_EFAULT
;
8691 if (!(a
= lock_user(VERIFY_READ
, arg1
,arg2
, 0)))
8693 if (!(p
= lock_user_string(arg3
)))
8695 ret
= get_errno(mincore(a
, arg2
, p
));
8696 unlock_user(p
, arg3
, ret
);
8698 unlock_user(a
, arg1
, 0);
8702 #ifdef TARGET_NR_arm_fadvise64_64
8703 case TARGET_NR_arm_fadvise64_64
:
8706 * arm_fadvise64_64 looks like fadvise64_64 but
8707 * with different argument order
8715 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
8716 #ifdef TARGET_NR_fadvise64_64
8717 case TARGET_NR_fadvise64_64
:
8719 #ifdef TARGET_NR_fadvise64
8720 case TARGET_NR_fadvise64
:
8724 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
8725 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
8726 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
8727 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
8731 ret
= -posix_fadvise(arg1
, arg2
, arg3
, arg4
);
8734 #ifdef TARGET_NR_madvise
8735 case TARGET_NR_madvise
:
8736 /* A straight passthrough may not be safe because qemu sometimes
8737 turns private file-backed mappings into anonymous mappings.
8738 This will break MADV_DONTNEED.
8739 This is a hint, so ignoring and returning success is ok. */
8743 #if TARGET_ABI_BITS == 32
8744 case TARGET_NR_fcntl64
:
8748 struct target_flock64
*target_fl
;
8750 struct target_eabi_flock64
*target_efl
;
8753 cmd
= target_to_host_fcntl_cmd(arg2
);
8754 if (cmd
== -TARGET_EINVAL
) {
8760 case TARGET_F_GETLK64
:
8762 if (((CPUARMState
*)cpu_env
)->eabi
) {
8763 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
8765 fl
.l_type
= tswap16(target_efl
->l_type
);
8766 fl
.l_whence
= tswap16(target_efl
->l_whence
);
8767 fl
.l_start
= tswap64(target_efl
->l_start
);
8768 fl
.l_len
= tswap64(target_efl
->l_len
);
8769 fl
.l_pid
= tswap32(target_efl
->l_pid
);
8770 unlock_user_struct(target_efl
, arg3
, 0);
8774 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
8776 fl
.l_type
= tswap16(target_fl
->l_type
);
8777 fl
.l_whence
= tswap16(target_fl
->l_whence
);
8778 fl
.l_start
= tswap64(target_fl
->l_start
);
8779 fl
.l_len
= tswap64(target_fl
->l_len
);
8780 fl
.l_pid
= tswap32(target_fl
->l_pid
);
8781 unlock_user_struct(target_fl
, arg3
, 0);
8783 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
8786 if (((CPUARMState
*)cpu_env
)->eabi
) {
8787 if (!lock_user_struct(VERIFY_WRITE
, target_efl
, arg3
, 0))
8789 target_efl
->l_type
= tswap16(fl
.l_type
);
8790 target_efl
->l_whence
= tswap16(fl
.l_whence
);
8791 target_efl
->l_start
= tswap64(fl
.l_start
);
8792 target_efl
->l_len
= tswap64(fl
.l_len
);
8793 target_efl
->l_pid
= tswap32(fl
.l_pid
);
8794 unlock_user_struct(target_efl
, arg3
, 1);
8798 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg3
, 0))
8800 target_fl
->l_type
= tswap16(fl
.l_type
);
8801 target_fl
->l_whence
= tswap16(fl
.l_whence
);
8802 target_fl
->l_start
= tswap64(fl
.l_start
);
8803 target_fl
->l_len
= tswap64(fl
.l_len
);
8804 target_fl
->l_pid
= tswap32(fl
.l_pid
);
8805 unlock_user_struct(target_fl
, arg3
, 1);
8810 case TARGET_F_SETLK64
:
8811 case TARGET_F_SETLKW64
:
8813 if (((CPUARMState
*)cpu_env
)->eabi
) {
8814 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
8816 fl
.l_type
= tswap16(target_efl
->l_type
);
8817 fl
.l_whence
= tswap16(target_efl
->l_whence
);
8818 fl
.l_start
= tswap64(target_efl
->l_start
);
8819 fl
.l_len
= tswap64(target_efl
->l_len
);
8820 fl
.l_pid
= tswap32(target_efl
->l_pid
);
8821 unlock_user_struct(target_efl
, arg3
, 0);
8825 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
8827 fl
.l_type
= tswap16(target_fl
->l_type
);
8828 fl
.l_whence
= tswap16(target_fl
->l_whence
);
8829 fl
.l_start
= tswap64(target_fl
->l_start
);
8830 fl
.l_len
= tswap64(target_fl
->l_len
);
8831 fl
.l_pid
= tswap32(target_fl
->l_pid
);
8832 unlock_user_struct(target_fl
, arg3
, 0);
8834 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
8837 ret
= do_fcntl(arg1
, arg2
, arg3
);
8843 #ifdef TARGET_NR_cacheflush
8844 case TARGET_NR_cacheflush
:
8845 /* self-modifying code is handled automatically, so nothing needed */
8849 #ifdef TARGET_NR_security
8850 case TARGET_NR_security
:
8853 #ifdef TARGET_NR_getpagesize
8854 case TARGET_NR_getpagesize
:
8855 ret
= TARGET_PAGE_SIZE
;
8858 case TARGET_NR_gettid
:
8859 ret
= get_errno(gettid());
8861 #ifdef TARGET_NR_readahead
8862 case TARGET_NR_readahead
:
8863 #if TARGET_ABI_BITS == 32
8864 if (regpairs_aligned(cpu_env
)) {
8869 ret
= get_errno(readahead(arg1
, ((off64_t
)arg3
<< 32) | arg2
, arg4
));
8871 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
8876 #ifdef TARGET_NR_setxattr
8877 case TARGET_NR_listxattr
:
8878 case TARGET_NR_llistxattr
:
8882 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
8884 ret
= -TARGET_EFAULT
;
8888 p
= lock_user_string(arg1
);
8890 if (num
== TARGET_NR_listxattr
) {
8891 ret
= get_errno(listxattr(p
, b
, arg3
));
8893 ret
= get_errno(llistxattr(p
, b
, arg3
));
8896 ret
= -TARGET_EFAULT
;
8898 unlock_user(p
, arg1
, 0);
8899 unlock_user(b
, arg2
, arg3
);
8902 case TARGET_NR_flistxattr
:
8906 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
8908 ret
= -TARGET_EFAULT
;
8912 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
8913 unlock_user(b
, arg2
, arg3
);
8916 case TARGET_NR_setxattr
:
8917 case TARGET_NR_lsetxattr
:
8919 void *p
, *n
, *v
= 0;
8921 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
8923 ret
= -TARGET_EFAULT
;
8927 p
= lock_user_string(arg1
);
8928 n
= lock_user_string(arg2
);
8930 if (num
== TARGET_NR_setxattr
) {
8931 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
8933 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
8936 ret
= -TARGET_EFAULT
;
8938 unlock_user(p
, arg1
, 0);
8939 unlock_user(n
, arg2
, 0);
8940 unlock_user(v
, arg3
, 0);
8943 case TARGET_NR_fsetxattr
:
8947 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
8949 ret
= -TARGET_EFAULT
;
8953 n
= lock_user_string(arg2
);
8955 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
8957 ret
= -TARGET_EFAULT
;
8959 unlock_user(n
, arg2
, 0);
8960 unlock_user(v
, arg3
, 0);
8963 case TARGET_NR_getxattr
:
8964 case TARGET_NR_lgetxattr
:
8966 void *p
, *n
, *v
= 0;
8968 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
8970 ret
= -TARGET_EFAULT
;
8974 p
= lock_user_string(arg1
);
8975 n
= lock_user_string(arg2
);
8977 if (num
== TARGET_NR_getxattr
) {
8978 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
8980 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
8983 ret
= -TARGET_EFAULT
;
8985 unlock_user(p
, arg1
, 0);
8986 unlock_user(n
, arg2
, 0);
8987 unlock_user(v
, arg3
, arg4
);
8990 case TARGET_NR_fgetxattr
:
8994 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
8996 ret
= -TARGET_EFAULT
;
9000 n
= lock_user_string(arg2
);
9002 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
9004 ret
= -TARGET_EFAULT
;
9006 unlock_user(n
, arg2
, 0);
9007 unlock_user(v
, arg3
, arg4
);
9010 case TARGET_NR_removexattr
:
9011 case TARGET_NR_lremovexattr
:
9014 p
= lock_user_string(arg1
);
9015 n
= lock_user_string(arg2
);
9017 if (num
== TARGET_NR_removexattr
) {
9018 ret
= get_errno(removexattr(p
, n
));
9020 ret
= get_errno(lremovexattr(p
, n
));
9023 ret
= -TARGET_EFAULT
;
9025 unlock_user(p
, arg1
, 0);
9026 unlock_user(n
, arg2
, 0);
9029 case TARGET_NR_fremovexattr
:
9032 n
= lock_user_string(arg2
);
9034 ret
= get_errno(fremovexattr(arg1
, n
));
9036 ret
= -TARGET_EFAULT
;
9038 unlock_user(n
, arg2
, 0);
9042 #endif /* CONFIG_ATTR */
9043 #ifdef TARGET_NR_set_thread_area
9044 case TARGET_NR_set_thread_area
:
9045 #if defined(TARGET_MIPS)
9046 ((CPUMIPSState
*) cpu_env
)->active_tc
.CP0_UserLocal
= arg1
;
9049 #elif defined(TARGET_CRIS)
9051 ret
= -TARGET_EINVAL
;
9053 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
9057 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
9058 ret
= do_set_thread_area(cpu_env
, arg1
);
9060 #elif defined(TARGET_M68K)
9062 TaskState
*ts
= cpu
->opaque
;
9063 ts
->tp_value
= arg1
;
9068 goto unimplemented_nowarn
;
9071 #ifdef TARGET_NR_get_thread_area
9072 case TARGET_NR_get_thread_area
:
9073 #if defined(TARGET_I386) && defined(TARGET_ABI32)
9074 ret
= do_get_thread_area(cpu_env
, arg1
);
9076 #elif defined(TARGET_M68K)
9078 TaskState
*ts
= cpu
->opaque
;
9083 goto unimplemented_nowarn
;
9086 #ifdef TARGET_NR_getdomainname
9087 case TARGET_NR_getdomainname
:
9088 goto unimplemented_nowarn
;
9091 #ifdef TARGET_NR_clock_gettime
9092 case TARGET_NR_clock_gettime
:
9095 ret
= get_errno(clock_gettime(arg1
, &ts
));
9096 if (!is_error(ret
)) {
9097 host_to_target_timespec(arg2
, &ts
);
9102 #ifdef TARGET_NR_clock_getres
9103 case TARGET_NR_clock_getres
:
9106 ret
= get_errno(clock_getres(arg1
, &ts
));
9107 if (!is_error(ret
)) {
9108 host_to_target_timespec(arg2
, &ts
);
9113 #ifdef TARGET_NR_clock_nanosleep
9114 case TARGET_NR_clock_nanosleep
:
9117 target_to_host_timespec(&ts
, arg3
);
9118 ret
= get_errno(clock_nanosleep(arg1
, arg2
, &ts
, arg4
? &ts
: NULL
));
9120 host_to_target_timespec(arg4
, &ts
);
9122 #if defined(TARGET_PPC)
9123 /* clock_nanosleep is odd in that it returns positive errno values.
9124 * On PPC, CR0 bit 3 should be set in such a situation. */
9126 ((CPUPPCState
*)cpu_env
)->crf
[0] |= 1;
9133 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
9134 case TARGET_NR_set_tid_address
:
9135 ret
= get_errno(set_tid_address((int *)g2h(arg1
)));
9139 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
9140 case TARGET_NR_tkill
:
9141 ret
= get_errno(sys_tkill((int)arg1
, target_to_host_signal(arg2
)));
9145 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
9146 case TARGET_NR_tgkill
:
9147 ret
= get_errno(sys_tgkill((int)arg1
, (int)arg2
,
9148 target_to_host_signal(arg3
)));
9152 #ifdef TARGET_NR_set_robust_list
9153 case TARGET_NR_set_robust_list
:
9154 case TARGET_NR_get_robust_list
:
9155 /* The ABI for supporting robust futexes has userspace pass
9156 * the kernel a pointer to a linked list which is updated by
9157 * userspace after the syscall; the list is walked by the kernel
9158 * when the thread exits. Since the linked list in QEMU guest
9159 * memory isn't a valid linked list for the host and we have
9160 * no way to reliably intercept the thread-death event, we can't
9161 * support these. Silently return ENOSYS so that guest userspace
9162 * falls back to a non-robust futex implementation (which should
9163 * be OK except in the corner case of the guest crashing while
9164 * holding a mutex that is shared with another process via
9167 goto unimplemented_nowarn
;
9170 #if defined(TARGET_NR_utimensat)
9171 case TARGET_NR_utimensat
:
9173 struct timespec
*tsp
, ts
[2];
9177 target_to_host_timespec(ts
, arg3
);
9178 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
9182 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
9184 if (!(p
= lock_user_string(arg2
))) {
9185 ret
= -TARGET_EFAULT
;
9188 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
9189 unlock_user(p
, arg2
, 0);
9194 case TARGET_NR_futex
:
9195 ret
= do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9197 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
9198 case TARGET_NR_inotify_init
:
9199 ret
= get_errno(sys_inotify_init());
9202 #ifdef CONFIG_INOTIFY1
9203 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
9204 case TARGET_NR_inotify_init1
:
9205 ret
= get_errno(sys_inotify_init1(arg1
));
9209 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
9210 case TARGET_NR_inotify_add_watch
:
9211 p
= lock_user_string(arg2
);
9212 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
9213 unlock_user(p
, arg2
, 0);
9216 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
9217 case TARGET_NR_inotify_rm_watch
:
9218 ret
= get_errno(sys_inotify_rm_watch(arg1
, arg2
));
9222 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
9223 case TARGET_NR_mq_open
:
9225 struct mq_attr posix_mq_attr
, *attrp
;
9227 p
= lock_user_string(arg1
- 1);
9229 copy_from_user_mq_attr (&posix_mq_attr
, arg4
);
9230 attrp
= &posix_mq_attr
;
9234 ret
= get_errno(mq_open(p
, arg2
, arg3
, attrp
));
9235 unlock_user (p
, arg1
, 0);
9239 case TARGET_NR_mq_unlink
:
9240 p
= lock_user_string(arg1
- 1);
9241 ret
= get_errno(mq_unlink(p
));
9242 unlock_user (p
, arg1
, 0);
9245 case TARGET_NR_mq_timedsend
:
9249 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
9251 target_to_host_timespec(&ts
, arg5
);
9252 ret
= get_errno(mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
9253 host_to_target_timespec(arg5
, &ts
);
9256 ret
= get_errno(mq_send(arg1
, p
, arg3
, arg4
));
9257 unlock_user (p
, arg2
, arg3
);
9261 case TARGET_NR_mq_timedreceive
:
9266 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
9268 target_to_host_timespec(&ts
, arg5
);
9269 ret
= get_errno(mq_timedreceive(arg1
, p
, arg3
, &prio
, &ts
));
9270 host_to_target_timespec(arg5
, &ts
);
9273 ret
= get_errno(mq_receive(arg1
, p
, arg3
, &prio
));
9274 unlock_user (p
, arg2
, arg3
);
9276 put_user_u32(prio
, arg4
);
9280 /* Not implemented for now... */
9281 /* case TARGET_NR_mq_notify: */
9284 case TARGET_NR_mq_getsetattr
:
9286 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
9289 ret
= mq_getattr(arg1
, &posix_mq_attr_out
);
9290 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
9293 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
9294 ret
|= mq_setattr(arg1
, &posix_mq_attr_in
, &posix_mq_attr_out
);
9301 #ifdef CONFIG_SPLICE
9302 #ifdef TARGET_NR_tee
9305 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
9309 #ifdef TARGET_NR_splice
9310 case TARGET_NR_splice
:
9312 loff_t loff_in
, loff_out
;
9313 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
9315 get_user_u64(loff_in
, arg2
);
9316 ploff_in
= &loff_in
;
9319 get_user_u64(loff_out
, arg2
);
9320 ploff_out
= &loff_out
;
9322 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
9326 #ifdef TARGET_NR_vmsplice
9327 case TARGET_NR_vmsplice
:
9329 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
9331 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
9332 unlock_iovec(vec
, arg2
, arg3
, 0);
9334 ret
= -host_to_target_errno(errno
);
9339 #endif /* CONFIG_SPLICE */
9340 #ifdef CONFIG_EVENTFD
9341 #if defined(TARGET_NR_eventfd)
9342 case TARGET_NR_eventfd
:
9343 ret
= get_errno(eventfd(arg1
, 0));
9346 #if defined(TARGET_NR_eventfd2)
9347 case TARGET_NR_eventfd2
:
9349 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
));
9350 if (arg2
& TARGET_O_NONBLOCK
) {
9351 host_flags
|= O_NONBLOCK
;
9353 if (arg2
& TARGET_O_CLOEXEC
) {
9354 host_flags
|= O_CLOEXEC
;
9356 ret
= get_errno(eventfd(arg1
, host_flags
));
9360 #endif /* CONFIG_EVENTFD */
9361 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
9362 case TARGET_NR_fallocate
:
9363 #if TARGET_ABI_BITS == 32
9364 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
9365 target_offset64(arg5
, arg6
)));
9367 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
9371 #if defined(CONFIG_SYNC_FILE_RANGE)
9372 #if defined(TARGET_NR_sync_file_range)
9373 case TARGET_NR_sync_file_range
:
9374 #if TARGET_ABI_BITS == 32
9375 #if defined(TARGET_MIPS)
9376 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
9377 target_offset64(arg5
, arg6
), arg7
));
9379 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
9380 target_offset64(arg4
, arg5
), arg6
));
9381 #endif /* !TARGET_MIPS */
9383 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
9387 #if defined(TARGET_NR_sync_file_range2)
9388 case TARGET_NR_sync_file_range2
:
9389 /* This is like sync_file_range but the arguments are reordered */
9390 #if TARGET_ABI_BITS == 32
9391 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
9392 target_offset64(arg5
, arg6
), arg2
));
9394 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
9399 #if defined(CONFIG_EPOLL)
9400 #if defined(TARGET_NR_epoll_create)
9401 case TARGET_NR_epoll_create
:
9402 ret
= get_errno(epoll_create(arg1
));
9405 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
9406 case TARGET_NR_epoll_create1
:
9407 ret
= get_errno(epoll_create1(arg1
));
9410 #if defined(TARGET_NR_epoll_ctl)
9411 case TARGET_NR_epoll_ctl
:
9413 struct epoll_event ep
;
9414 struct epoll_event
*epp
= 0;
9416 struct target_epoll_event
*target_ep
;
9417 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
9420 ep
.events
= tswap32(target_ep
->events
);
9421 /* The epoll_data_t union is just opaque data to the kernel,
9422 * so we transfer all 64 bits across and need not worry what
9423 * actual data type it is.
9425 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
9426 unlock_user_struct(target_ep
, arg4
, 0);
9429 ret
= get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
9434 #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9435 #define IMPLEMENT_EPOLL_PWAIT
9437 #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9438 #if defined(TARGET_NR_epoll_wait)
9439 case TARGET_NR_epoll_wait
:
9441 #if defined(IMPLEMENT_EPOLL_PWAIT)
9442 case TARGET_NR_epoll_pwait
:
9445 struct target_epoll_event
*target_ep
;
9446 struct epoll_event
*ep
;
9448 int maxevents
= arg3
;
9451 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
9452 maxevents
* sizeof(struct target_epoll_event
), 1);
9457 ep
= alloca(maxevents
* sizeof(struct epoll_event
));
9460 #if defined(IMPLEMENT_EPOLL_PWAIT)
9461 case TARGET_NR_epoll_pwait
:
9463 target_sigset_t
*target_set
;
9464 sigset_t _set
, *set
= &_set
;
9467 target_set
= lock_user(VERIFY_READ
, arg5
,
9468 sizeof(target_sigset_t
), 1);
9470 unlock_user(target_ep
, arg2
, 0);
9473 target_to_host_sigset(set
, target_set
);
9474 unlock_user(target_set
, arg5
, 0);
9479 ret
= get_errno(epoll_pwait(epfd
, ep
, maxevents
, timeout
, set
));
9483 #if defined(TARGET_NR_epoll_wait)
9484 case TARGET_NR_epoll_wait
:
9485 ret
= get_errno(epoll_wait(epfd
, ep
, maxevents
, timeout
));
9489 ret
= -TARGET_ENOSYS
;
9491 if (!is_error(ret
)) {
9493 for (i
= 0; i
< ret
; i
++) {
9494 target_ep
[i
].events
= tswap32(ep
[i
].events
);
9495 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
9498 unlock_user(target_ep
, arg2
, ret
* sizeof(struct target_epoll_event
));
9503 #ifdef TARGET_NR_prlimit64
9504 case TARGET_NR_prlimit64
:
9506 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9507 struct target_rlimit64
*target_rnew
, *target_rold
;
9508 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
9510 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
9513 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
9514 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
9515 unlock_user_struct(target_rnew
, arg3
, 0);
9519 ret
= get_errno(sys_prlimit64(arg1
, arg2
, rnewp
, arg4
? &rold
: 0));
9520 if (!is_error(ret
) && arg4
) {
9521 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
9524 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
9525 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
9526 unlock_user_struct(target_rold
, arg4
, 1);
9531 #ifdef TARGET_NR_gethostname
9532 case TARGET_NR_gethostname
:
9534 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
9536 ret
= get_errno(gethostname(name
, arg2
));
9537 unlock_user(name
, arg1
, arg2
);
9539 ret
= -TARGET_EFAULT
;
9544 #ifdef TARGET_NR_atomic_cmpxchg_32
9545 case TARGET_NR_atomic_cmpxchg_32
:
9547 /* should use start_exclusive from main.c */
9548 abi_ulong mem_value
;
9549 if (get_user_u32(mem_value
, arg6
)) {
9550 target_siginfo_t info
;
9551 info
.si_signo
= SIGSEGV
;
9553 info
.si_code
= TARGET_SEGV_MAPERR
;
9554 info
._sifields
._sigfault
._addr
= arg6
;
9555 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
9559 if (mem_value
== arg2
)
9560 put_user_u32(arg1
, arg6
);
9565 #ifdef TARGET_NR_atomic_barrier
9566 case TARGET_NR_atomic_barrier
:
9568 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
9574 #ifdef TARGET_NR_timer_create
9575 case TARGET_NR_timer_create
:
9577 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
9579 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
9580 struct target_timer_t
*ptarget_timer
;
9583 int timer_index
= next_free_host_timer();
9585 if (timer_index
< 0) {
9586 ret
= -TARGET_EAGAIN
;
9588 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
9591 phost_sevp
= &host_sevp
;
9592 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
9598 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
9602 if (!lock_user_struct(VERIFY_WRITE
, ptarget_timer
, arg3
, 1)) {
9605 ptarget_timer
->ptr
= tswap32(0xcafe0000 | timer_index
);
9606 unlock_user_struct(ptarget_timer
, arg3
, 1);
9613 #ifdef TARGET_NR_timer_settime
9614 case TARGET_NR_timer_settime
:
9616 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
9617 * struct itimerspec * old_value */
9618 target_ulong timerid
= arg1
;
9620 if (arg3
== 0 || timerid
>= ARRAY_SIZE(g_posix_timers
)) {
9621 ret
= -TARGET_EINVAL
;
9623 timer_t htimer
= g_posix_timers
[timerid
];
9624 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
9626 target_to_host_itimerspec(&hspec_new
, arg3
);
9628 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
9629 host_to_target_itimerspec(arg2
, &hspec_old
);
9635 #ifdef TARGET_NR_timer_gettime
9636 case TARGET_NR_timer_gettime
:
9638 /* args: timer_t timerid, struct itimerspec *curr_value */
9639 target_ulong timerid
= arg1
;
9642 return -TARGET_EFAULT
;
9643 } else if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
9644 ret
= -TARGET_EINVAL
;
9646 timer_t htimer
= g_posix_timers
[timerid
];
9647 struct itimerspec hspec
;
9648 ret
= get_errno(timer_gettime(htimer
, &hspec
));
9650 if (host_to_target_itimerspec(arg2
, &hspec
)) {
9651 ret
= -TARGET_EFAULT
;
9658 #ifdef TARGET_NR_timer_getoverrun
9659 case TARGET_NR_timer_getoverrun
:
9661 /* args: timer_t timerid */
9662 target_ulong timerid
= arg1
;
9664 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
9665 ret
= -TARGET_EINVAL
;
9667 timer_t htimer
= g_posix_timers
[timerid
];
9668 ret
= get_errno(timer_getoverrun(htimer
));
9674 #ifdef TARGET_NR_timer_delete
9675 case TARGET_NR_timer_delete
:
9677 /* args: timer_t timerid */
9678 target_ulong timerid
= arg1
;
9680 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
9681 ret
= -TARGET_EINVAL
;
9683 timer_t htimer
= g_posix_timers
[timerid
];
9684 ret
= get_errno(timer_delete(htimer
));
9685 g_posix_timers
[timerid
] = 0;
9691 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
9692 case TARGET_NR_timerfd_create
:
9693 ret
= get_errno(timerfd_create(arg1
,
9694 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
9698 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
9699 case TARGET_NR_timerfd_gettime
:
9701 struct itimerspec its_curr
;
9703 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
9705 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
9712 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
9713 case TARGET_NR_timerfd_settime
:
9715 struct itimerspec its_new
, its_old
, *p_new
;
9718 if (target_to_host_itimerspec(&its_new
, arg3
)) {
9726 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
9728 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
9735 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
9736 case TARGET_NR_ioprio_get
:
9737 ret
= get_errno(ioprio_get(arg1
, arg2
));
9741 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
9742 case TARGET_NR_ioprio_set
:
9743 ret
= get_errno(ioprio_set(arg1
, arg2
, arg3
));
9747 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
9748 case TARGET_NR_setns
:
9749 ret
= get_errno(setns(arg1
, arg2
));
9752 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
9753 case TARGET_NR_unshare
:
9754 ret
= get_errno(unshare(arg1
));
9760 gemu_log("qemu: Unsupported syscall: %d\n", num
);
9761 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
9762 unimplemented_nowarn
:
9764 ret
= -TARGET_ENOSYS
;
9769 gemu_log(" = " TARGET_ABI_FMT_ld
"\n", ret
);
9772 print_syscall_ret(num
, ret
);
9775 ret
= -TARGET_EFAULT
;