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
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
29 #include <sys/mount.h>
31 #include <sys/fsuid.h>
32 #include <sys/personality.h>
33 #include <sys/prctl.h>
34 #include <sys/resource.h>
37 #include <linux/capability.h>
40 int __clone2(int (*fn
)(void *), void *child_stack_base
,
41 size_t stack_size
, int flags
, void *arg
, ...);
43 #include <sys/socket.h>
47 #include <sys/times.h>
50 #include <sys/statfs.h>
52 #include <sys/sysinfo.h>
53 #include <sys/signalfd.h>
54 //#include <sys/user.h>
55 #include <netinet/ip.h>
56 #include <netinet/tcp.h>
57 #include <linux/wireless.h>
58 #include <linux/icmp.h>
59 #include "qemu-common.h"
61 #include <sys/timerfd.h>
67 #include <sys/eventfd.h>
70 #include <sys/epoll.h>
73 #include "qemu/xattr.h"
75 #ifdef CONFIG_SENDFILE
76 #include <sys/sendfile.h>
79 #define termios host_termios
80 #define winsize host_winsize
81 #define termio host_termio
82 #define sgttyb host_sgttyb /* same as target */
83 #define tchars host_tchars /* same as target */
84 #define ltchars host_ltchars /* same as target */
86 #include <linux/termios.h>
87 #include <linux/unistd.h>
88 #include <linux/cdrom.h>
89 #include <linux/hdreg.h>
90 #include <linux/soundcard.h>
92 #include <linux/mtio.h>
94 #if defined(CONFIG_FIEMAP)
95 #include <linux/fiemap.h>
99 #include <linux/dm-ioctl.h>
100 #include <linux/reboot.h>
101 #include <linux/route.h>
102 #include <linux/filter.h>
103 #include <linux/blkpg.h>
104 #include "linux_loop.h"
109 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
110 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
114 //#include <linux/msdos_fs.h>
115 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
116 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
127 #define _syscall0(type,name) \
128 static type name (void) \
130 return syscall(__NR_##name); \
133 #define _syscall1(type,name,type1,arg1) \
134 static type name (type1 arg1) \
136 return syscall(__NR_##name, arg1); \
139 #define _syscall2(type,name,type1,arg1,type2,arg2) \
140 static type name (type1 arg1,type2 arg2) \
142 return syscall(__NR_##name, arg1, arg2); \
145 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
146 static type name (type1 arg1,type2 arg2,type3 arg3) \
148 return syscall(__NR_##name, arg1, arg2, arg3); \
151 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
152 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
154 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
157 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
159 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
161 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
165 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
166 type5,arg5,type6,arg6) \
167 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
170 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
174 #define __NR_sys_uname __NR_uname
175 #define __NR_sys_getcwd1 __NR_getcwd
176 #define __NR_sys_getdents __NR_getdents
177 #define __NR_sys_getdents64 __NR_getdents64
178 #define __NR_sys_getpriority __NR_getpriority
179 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
180 #define __NR_sys_syslog __NR_syslog
181 #define __NR_sys_tgkill __NR_tgkill
182 #define __NR_sys_tkill __NR_tkill
183 #define __NR_sys_futex __NR_futex
184 #define __NR_sys_inotify_init __NR_inotify_init
185 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
186 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
188 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
190 #define __NR__llseek __NR_lseek
193 /* Newer kernel ports have llseek() instead of _llseek() */
194 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
195 #define TARGET_NR__llseek TARGET_NR_llseek
199 _syscall0(int, gettid
)
201 /* This is a replacement for the host gettid() and must return a host
203 static int gettid(void) {
207 #if defined(TARGET_NR_getdents) && defined(__NR_getdents)
208 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
210 #if !defined(__NR_getdents) || \
211 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
212 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
214 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
215 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
216 loff_t
*, res
, uint
, wh
);
218 _syscall3(int,sys_rt_sigqueueinfo
,int,pid
,int,sig
,siginfo_t
*,uinfo
)
219 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
220 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
221 _syscall3(int,sys_tgkill
,int,tgid
,int,pid
,int,sig
)
223 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
224 _syscall2(int,sys_tkill
,int,tid
,int,sig
)
226 #ifdef __NR_exit_group
227 _syscall1(int,exit_group
,int,error_code
)
229 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
230 _syscall1(int,set_tid_address
,int *,tidptr
)
232 #if defined(TARGET_NR_futex) && defined(__NR_futex)
233 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
234 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
236 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
237 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
238 unsigned long *, user_mask_ptr
);
239 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
240 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
241 unsigned long *, user_mask_ptr
);
242 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
244 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
245 struct __user_cap_data_struct
*, data
);
246 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
247 struct __user_cap_data_struct
*, data
);
248 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
249 _syscall2(int, ioprio_get
, int, which
, int, who
)
251 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
252 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
254 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
255 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
258 static bitmask_transtbl fcntl_flags_tbl
[] = {
259 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
260 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
261 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
262 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
263 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
264 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
265 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
266 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
267 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
268 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
269 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
270 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
271 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
272 #if defined(O_DIRECT)
273 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
275 #if defined(O_NOATIME)
276 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
278 #if defined(O_CLOEXEC)
279 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
282 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
284 /* Don't terminate the list prematurely on 64-bit host+guest. */
285 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
286 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
291 typedef abi_long (*TargetFdDataFunc
)(void *, size_t);
292 typedef abi_long (*TargetFdAddrFunc
)(void *, abi_ulong
, socklen_t
);
293 typedef struct TargetFdTrans
{
294 TargetFdDataFunc host_to_target_data
;
295 TargetFdDataFunc target_to_host_data
;
296 TargetFdAddrFunc target_to_host_addr
;
299 static TargetFdTrans
**target_fd_trans
;
301 static unsigned int target_fd_max
;
303 static TargetFdDataFunc
fd_trans_host_to_target_data(int fd
)
305 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
306 return target_fd_trans
[fd
]->host_to_target_data
;
311 static TargetFdAddrFunc
fd_trans_target_to_host_addr(int fd
)
313 if (fd
>= 0 && fd
< target_fd_max
&& target_fd_trans
[fd
]) {
314 return target_fd_trans
[fd
]->target_to_host_addr
;
319 static void fd_trans_register(int fd
, TargetFdTrans
*trans
)
323 if (fd
>= target_fd_max
) {
324 oldmax
= target_fd_max
;
325 target_fd_max
= ((fd
>> 6) + 1) << 6; /* by slice of 64 entries */
326 target_fd_trans
= g_renew(TargetFdTrans
*,
327 target_fd_trans
, target_fd_max
);
328 memset((void *)(target_fd_trans
+ oldmax
), 0,
329 (target_fd_max
- oldmax
) * sizeof(TargetFdTrans
*));
331 target_fd_trans
[fd
] = trans
;
334 static void fd_trans_unregister(int fd
)
336 if (fd
>= 0 && fd
< target_fd_max
) {
337 target_fd_trans
[fd
] = NULL
;
341 static void fd_trans_dup(int oldfd
, int newfd
)
343 fd_trans_unregister(newfd
);
344 if (oldfd
< target_fd_max
&& target_fd_trans
[oldfd
]) {
345 fd_trans_register(newfd
, target_fd_trans
[oldfd
]);
349 static int sys_getcwd1(char *buf
, size_t size
)
351 if (getcwd(buf
, size
) == NULL
) {
352 /* getcwd() sets errno */
355 return strlen(buf
)+1;
358 static int sys_openat(int dirfd
, const char *pathname
, int flags
, mode_t mode
)
361 * open(2) has extra parameter 'mode' when called with
364 if ((flags
& O_CREAT
) != 0) {
365 return (openat(dirfd
, pathname
, flags
, mode
));
367 return (openat(dirfd
, pathname
, flags
));
370 #ifdef TARGET_NR_utimensat
371 #ifdef CONFIG_UTIMENSAT
372 static int sys_utimensat(int dirfd
, const char *pathname
,
373 const struct timespec times
[2], int flags
)
375 if (pathname
== NULL
)
376 return futimens(dirfd
, times
);
378 return utimensat(dirfd
, pathname
, times
, flags
);
380 #elif defined(__NR_utimensat)
381 #define __NR_sys_utimensat __NR_utimensat
382 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
383 const struct timespec
*,tsp
,int,flags
)
385 static int sys_utimensat(int dirfd
, const char *pathname
,
386 const struct timespec times
[2], int flags
)
392 #endif /* TARGET_NR_utimensat */
394 #ifdef CONFIG_INOTIFY
395 #include <sys/inotify.h>
397 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
398 static int sys_inotify_init(void)
400 return (inotify_init());
403 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
404 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
406 return (inotify_add_watch(fd
, pathname
, mask
));
409 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
410 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
412 return (inotify_rm_watch(fd
, wd
));
415 #ifdef CONFIG_INOTIFY1
416 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
417 static int sys_inotify_init1(int flags
)
419 return (inotify_init1(flags
));
424 /* Userspace can usually survive runtime without inotify */
425 #undef TARGET_NR_inotify_init
426 #undef TARGET_NR_inotify_init1
427 #undef TARGET_NR_inotify_add_watch
428 #undef TARGET_NR_inotify_rm_watch
429 #endif /* CONFIG_INOTIFY */
431 #if defined(TARGET_NR_ppoll)
433 # define __NR_ppoll -1
435 #define __NR_sys_ppoll __NR_ppoll
436 _syscall5(int, sys_ppoll
, struct pollfd
*, fds
, nfds_t
, nfds
,
437 struct timespec
*, timeout
, const sigset_t
*, sigmask
,
441 #if defined(TARGET_NR_pselect6)
442 #ifndef __NR_pselect6
443 # define __NR_pselect6 -1
445 #define __NR_sys_pselect6 __NR_pselect6
446 _syscall6(int, sys_pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
,
447 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
);
450 #if defined(TARGET_NR_prlimit64)
451 #ifndef __NR_prlimit64
452 # define __NR_prlimit64 -1
454 #define __NR_sys_prlimit64 __NR_prlimit64
455 /* The glibc rlimit structure may not be that used by the underlying syscall */
456 struct host_rlimit64
{
460 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
461 const struct host_rlimit64
*, new_limit
,
462 struct host_rlimit64
*, old_limit
)
466 #if defined(TARGET_NR_timer_create)
467 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
468 static timer_t g_posix_timers
[32] = { 0, } ;
470 static inline int next_free_host_timer(void)
473 /* FIXME: Does finding the next free slot require a lock? */
474 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
475 if (g_posix_timers
[k
] == 0) {
476 g_posix_timers
[k
] = (timer_t
) 1;
484 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
486 static inline int regpairs_aligned(void *cpu_env
) {
487 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
489 #elif defined(TARGET_MIPS)
490 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
491 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
492 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
493 * of registers which translates to the same as ARM/MIPS, because we start with
495 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
497 static inline int regpairs_aligned(void *cpu_env
) { return 0; }
500 #define ERRNO_TABLE_SIZE 1200
502 /* target_to_host_errno_table[] is initialized from
503 * host_to_target_errno_table[] in syscall_init(). */
504 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
508 * This list is the union of errno values overridden in asm-<arch>/errno.h
509 * minus the errnos that are not actually generic to all archs.
511 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
512 [EAGAIN
] = TARGET_EAGAIN
,
513 [EIDRM
] = TARGET_EIDRM
,
514 [ECHRNG
] = TARGET_ECHRNG
,
515 [EL2NSYNC
] = TARGET_EL2NSYNC
,
516 [EL3HLT
] = TARGET_EL3HLT
,
517 [EL3RST
] = TARGET_EL3RST
,
518 [ELNRNG
] = TARGET_ELNRNG
,
519 [EUNATCH
] = TARGET_EUNATCH
,
520 [ENOCSI
] = TARGET_ENOCSI
,
521 [EL2HLT
] = TARGET_EL2HLT
,
522 [EDEADLK
] = TARGET_EDEADLK
,
523 [ENOLCK
] = TARGET_ENOLCK
,
524 [EBADE
] = TARGET_EBADE
,
525 [EBADR
] = TARGET_EBADR
,
526 [EXFULL
] = TARGET_EXFULL
,
527 [ENOANO
] = TARGET_ENOANO
,
528 [EBADRQC
] = TARGET_EBADRQC
,
529 [EBADSLT
] = TARGET_EBADSLT
,
530 [EBFONT
] = TARGET_EBFONT
,
531 [ENOSTR
] = TARGET_ENOSTR
,
532 [ENODATA
] = TARGET_ENODATA
,
533 [ETIME
] = TARGET_ETIME
,
534 [ENOSR
] = TARGET_ENOSR
,
535 [ENONET
] = TARGET_ENONET
,
536 [ENOPKG
] = TARGET_ENOPKG
,
537 [EREMOTE
] = TARGET_EREMOTE
,
538 [ENOLINK
] = TARGET_ENOLINK
,
539 [EADV
] = TARGET_EADV
,
540 [ESRMNT
] = TARGET_ESRMNT
,
541 [ECOMM
] = TARGET_ECOMM
,
542 [EPROTO
] = TARGET_EPROTO
,
543 [EDOTDOT
] = TARGET_EDOTDOT
,
544 [EMULTIHOP
] = TARGET_EMULTIHOP
,
545 [EBADMSG
] = TARGET_EBADMSG
,
546 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
547 [EOVERFLOW
] = TARGET_EOVERFLOW
,
548 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
549 [EBADFD
] = TARGET_EBADFD
,
550 [EREMCHG
] = TARGET_EREMCHG
,
551 [ELIBACC
] = TARGET_ELIBACC
,
552 [ELIBBAD
] = TARGET_ELIBBAD
,
553 [ELIBSCN
] = TARGET_ELIBSCN
,
554 [ELIBMAX
] = TARGET_ELIBMAX
,
555 [ELIBEXEC
] = TARGET_ELIBEXEC
,
556 [EILSEQ
] = TARGET_EILSEQ
,
557 [ENOSYS
] = TARGET_ENOSYS
,
558 [ELOOP
] = TARGET_ELOOP
,
559 [ERESTART
] = TARGET_ERESTART
,
560 [ESTRPIPE
] = TARGET_ESTRPIPE
,
561 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
562 [EUSERS
] = TARGET_EUSERS
,
563 [ENOTSOCK
] = TARGET_ENOTSOCK
,
564 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
565 [EMSGSIZE
] = TARGET_EMSGSIZE
,
566 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
567 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
568 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
569 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
570 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
571 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
572 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
573 [EADDRINUSE
] = TARGET_EADDRINUSE
,
574 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
575 [ENETDOWN
] = TARGET_ENETDOWN
,
576 [ENETUNREACH
] = TARGET_ENETUNREACH
,
577 [ENETRESET
] = TARGET_ENETRESET
,
578 [ECONNABORTED
] = TARGET_ECONNABORTED
,
579 [ECONNRESET
] = TARGET_ECONNRESET
,
580 [ENOBUFS
] = TARGET_ENOBUFS
,
581 [EISCONN
] = TARGET_EISCONN
,
582 [ENOTCONN
] = TARGET_ENOTCONN
,
583 [EUCLEAN
] = TARGET_EUCLEAN
,
584 [ENOTNAM
] = TARGET_ENOTNAM
,
585 [ENAVAIL
] = TARGET_ENAVAIL
,
586 [EISNAM
] = TARGET_EISNAM
,
587 [EREMOTEIO
] = TARGET_EREMOTEIO
,
588 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
589 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
590 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
591 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
592 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
593 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
594 [EALREADY
] = TARGET_EALREADY
,
595 [EINPROGRESS
] = TARGET_EINPROGRESS
,
596 [ESTALE
] = TARGET_ESTALE
,
597 [ECANCELED
] = TARGET_ECANCELED
,
598 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
599 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
601 [ENOKEY
] = TARGET_ENOKEY
,
604 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
607 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
610 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
613 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
615 #ifdef ENOTRECOVERABLE
616 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
620 static inline int host_to_target_errno(int err
)
622 if(host_to_target_errno_table
[err
])
623 return host_to_target_errno_table
[err
];
627 static inline int target_to_host_errno(int err
)
629 if (target_to_host_errno_table
[err
])
630 return target_to_host_errno_table
[err
];
634 static inline abi_long
get_errno(abi_long ret
)
637 return -host_to_target_errno(errno
);
642 static inline int is_error(abi_long ret
)
644 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
647 char *target_strerror(int err
)
649 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
652 return strerror(target_to_host_errno(err
));
655 static inline int host_to_target_sock_type(int host_type
)
659 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
661 target_type
= TARGET_SOCK_DGRAM
;
664 target_type
= TARGET_SOCK_STREAM
;
667 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
671 #if defined(SOCK_CLOEXEC)
672 if (host_type
& SOCK_CLOEXEC
) {
673 target_type
|= TARGET_SOCK_CLOEXEC
;
677 #if defined(SOCK_NONBLOCK)
678 if (host_type
& SOCK_NONBLOCK
) {
679 target_type
|= TARGET_SOCK_NONBLOCK
;
686 static abi_ulong target_brk
;
687 static abi_ulong target_original_brk
;
688 static abi_ulong brk_page
;
690 void target_set_brk(abi_ulong new_brk
)
692 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
693 brk_page
= HOST_PAGE_ALIGN(target_brk
);
696 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
697 #define DEBUGF_BRK(message, args...)
699 /* do_brk() must return target values and target errnos. */
700 abi_long
do_brk(abi_ulong new_brk
)
702 abi_long mapped_addr
;
705 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
708 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
711 if (new_brk
< target_original_brk
) {
712 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
717 /* If the new brk is less than the highest page reserved to the
718 * target heap allocation, set it and we're almost done... */
719 if (new_brk
<= brk_page
) {
720 /* Heap contents are initialized to zero, as for anonymous
722 if (new_brk
> target_brk
) {
723 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
725 target_brk
= new_brk
;
726 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
730 /* We need to allocate more memory after the brk... Note that
731 * we don't use MAP_FIXED because that will map over the top of
732 * any existing mapping (like the one with the host libc or qemu
733 * itself); instead we treat "mapped but at wrong address" as
734 * a failure and unmap again.
736 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
737 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
738 PROT_READ
|PROT_WRITE
,
739 MAP_ANON
|MAP_PRIVATE
, 0, 0));
741 if (mapped_addr
== brk_page
) {
742 /* Heap contents are initialized to zero, as for anonymous
743 * mapped pages. Technically the new pages are already
744 * initialized to zero since they *are* anonymous mapped
745 * pages, however we have to take care with the contents that
746 * come from the remaining part of the previous page: it may
747 * contains garbage data due to a previous heap usage (grown
749 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
751 target_brk
= new_brk
;
752 brk_page
= HOST_PAGE_ALIGN(target_brk
);
753 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
756 } else if (mapped_addr
!= -1) {
757 /* Mapped but at wrong address, meaning there wasn't actually
758 * enough space for this brk.
760 target_munmap(mapped_addr
, new_alloc_size
);
762 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
765 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
768 #if defined(TARGET_ALPHA)
769 /* We (partially) emulate OSF/1 on Alpha, which requires we
770 return a proper errno, not an unchanged brk value. */
771 return -TARGET_ENOMEM
;
773 /* For everything else, return the previous break. */
777 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
778 abi_ulong target_fds_addr
,
782 abi_ulong b
, *target_fds
;
784 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
785 if (!(target_fds
= lock_user(VERIFY_READ
,
787 sizeof(abi_ulong
) * nw
,
789 return -TARGET_EFAULT
;
793 for (i
= 0; i
< nw
; i
++) {
794 /* grab the abi_ulong */
795 __get_user(b
, &target_fds
[i
]);
796 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
797 /* check the bit inside the abi_ulong */
804 unlock_user(target_fds
, target_fds_addr
, 0);
809 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
810 abi_ulong target_fds_addr
,
813 if (target_fds_addr
) {
814 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
815 return -TARGET_EFAULT
;
823 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
829 abi_ulong
*target_fds
;
831 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
832 if (!(target_fds
= lock_user(VERIFY_WRITE
,
834 sizeof(abi_ulong
) * nw
,
836 return -TARGET_EFAULT
;
839 for (i
= 0; i
< nw
; i
++) {
841 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
842 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
845 __put_user(v
, &target_fds
[i
]);
848 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
853 #if defined(__alpha__)
859 static inline abi_long
host_to_target_clock_t(long ticks
)
861 #if HOST_HZ == TARGET_HZ
864 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
868 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
869 const struct rusage
*rusage
)
871 struct target_rusage
*target_rusage
;
873 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
874 return -TARGET_EFAULT
;
875 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
876 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
877 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
878 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
879 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
880 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
881 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
882 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
883 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
884 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
885 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
886 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
887 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
888 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
889 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
890 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
891 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
892 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
893 unlock_user_struct(target_rusage
, target_addr
, 1);
898 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
900 abi_ulong target_rlim_swap
;
903 target_rlim_swap
= tswapal(target_rlim
);
904 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
905 return RLIM_INFINITY
;
907 result
= target_rlim_swap
;
908 if (target_rlim_swap
!= (rlim_t
)result
)
909 return RLIM_INFINITY
;
914 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
916 abi_ulong target_rlim_swap
;
919 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
920 target_rlim_swap
= TARGET_RLIM_INFINITY
;
922 target_rlim_swap
= rlim
;
923 result
= tswapal(target_rlim_swap
);
928 static inline int target_to_host_resource(int code
)
931 case TARGET_RLIMIT_AS
:
933 case TARGET_RLIMIT_CORE
:
935 case TARGET_RLIMIT_CPU
:
937 case TARGET_RLIMIT_DATA
:
939 case TARGET_RLIMIT_FSIZE
:
941 case TARGET_RLIMIT_LOCKS
:
943 case TARGET_RLIMIT_MEMLOCK
:
944 return RLIMIT_MEMLOCK
;
945 case TARGET_RLIMIT_MSGQUEUE
:
946 return RLIMIT_MSGQUEUE
;
947 case TARGET_RLIMIT_NICE
:
949 case TARGET_RLIMIT_NOFILE
:
950 return RLIMIT_NOFILE
;
951 case TARGET_RLIMIT_NPROC
:
953 case TARGET_RLIMIT_RSS
:
955 case TARGET_RLIMIT_RTPRIO
:
956 return RLIMIT_RTPRIO
;
957 case TARGET_RLIMIT_SIGPENDING
:
958 return RLIMIT_SIGPENDING
;
959 case TARGET_RLIMIT_STACK
:
966 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
967 abi_ulong target_tv_addr
)
969 struct target_timeval
*target_tv
;
971 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
972 return -TARGET_EFAULT
;
974 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
975 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
977 unlock_user_struct(target_tv
, target_tv_addr
, 0);
982 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
983 const struct timeval
*tv
)
985 struct target_timeval
*target_tv
;
987 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
988 return -TARGET_EFAULT
;
990 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
991 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
993 unlock_user_struct(target_tv
, target_tv_addr
, 1);
998 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
999 abi_ulong target_tz_addr
)
1001 struct target_timezone
*target_tz
;
1003 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1004 return -TARGET_EFAULT
;
1007 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1008 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1010 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1015 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1018 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1019 abi_ulong target_mq_attr_addr
)
1021 struct target_mq_attr
*target_mq_attr
;
1023 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1024 target_mq_attr_addr
, 1))
1025 return -TARGET_EFAULT
;
1027 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1028 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1029 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1030 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1032 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1037 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1038 const struct mq_attr
*attr
)
1040 struct target_mq_attr
*target_mq_attr
;
1042 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1043 target_mq_attr_addr
, 0))
1044 return -TARGET_EFAULT
;
1046 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1047 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1048 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1049 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1051 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1057 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1058 /* do_select() must return target values and target errnos. */
1059 static abi_long
do_select(int n
,
1060 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1061 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1063 fd_set rfds
, wfds
, efds
;
1064 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1065 struct timeval tv
, *tv_ptr
;
1068 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1072 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1076 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1081 if (target_tv_addr
) {
1082 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1083 return -TARGET_EFAULT
;
1089 ret
= get_errno(select(n
, rfds_ptr
, wfds_ptr
, efds_ptr
, tv_ptr
));
1091 if (!is_error(ret
)) {
1092 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1093 return -TARGET_EFAULT
;
1094 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1095 return -TARGET_EFAULT
;
1096 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1097 return -TARGET_EFAULT
;
1099 if (target_tv_addr
&& copy_to_user_timeval(target_tv_addr
, &tv
))
1100 return -TARGET_EFAULT
;
1107 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1110 return pipe2(host_pipe
, flags
);
1116 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1117 int flags
, int is_pipe2
)
1121 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1124 return get_errno(ret
);
1126 /* Several targets have special calling conventions for the original
1127 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1129 #if defined(TARGET_ALPHA)
1130 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1131 return host_pipe
[0];
1132 #elif defined(TARGET_MIPS)
1133 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1134 return host_pipe
[0];
1135 #elif defined(TARGET_SH4)
1136 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1137 return host_pipe
[0];
1138 #elif defined(TARGET_SPARC)
1139 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1140 return host_pipe
[0];
1144 if (put_user_s32(host_pipe
[0], pipedes
)
1145 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1146 return -TARGET_EFAULT
;
1147 return get_errno(ret
);
1150 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1151 abi_ulong target_addr
,
1154 struct target_ip_mreqn
*target_smreqn
;
1156 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1158 return -TARGET_EFAULT
;
1159 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1160 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1161 if (len
== sizeof(struct target_ip_mreqn
))
1162 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1163 unlock_user(target_smreqn
, target_addr
, 0);
1168 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1169 abi_ulong target_addr
,
1172 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1173 sa_family_t sa_family
;
1174 struct target_sockaddr
*target_saddr
;
1176 if (fd_trans_target_to_host_addr(fd
)) {
1177 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1180 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1182 return -TARGET_EFAULT
;
1184 sa_family
= tswap16(target_saddr
->sa_family
);
1186 /* Oops. The caller might send a incomplete sun_path; sun_path
1187 * must be terminated by \0 (see the manual page), but
1188 * unfortunately it is quite common to specify sockaddr_un
1189 * length as "strlen(x->sun_path)" while it should be
1190 * "strlen(...) + 1". We'll fix that here if needed.
1191 * Linux kernel has a similar feature.
1194 if (sa_family
== AF_UNIX
) {
1195 if (len
< unix_maxlen
&& len
> 0) {
1196 char *cp
= (char*)target_saddr
;
1198 if ( cp
[len
-1] && !cp
[len
] )
1201 if (len
> unix_maxlen
)
1205 memcpy(addr
, target_saddr
, len
);
1206 addr
->sa_family
= sa_family
;
1207 if (sa_family
== AF_PACKET
) {
1208 struct target_sockaddr_ll
*lladdr
;
1210 lladdr
= (struct target_sockaddr_ll
*)addr
;
1211 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1212 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1214 unlock_user(target_saddr
, target_addr
, 0);
1219 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1220 struct sockaddr
*addr
,
1223 struct target_sockaddr
*target_saddr
;
1225 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1227 return -TARGET_EFAULT
;
1228 memcpy(target_saddr
, addr
, len
);
1229 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1230 unlock_user(target_saddr
, target_addr
, len
);
1235 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1236 struct target_msghdr
*target_msgh
)
1238 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1239 abi_long msg_controllen
;
1240 abi_ulong target_cmsg_addr
;
1241 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1242 socklen_t space
= 0;
1244 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1245 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1247 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1248 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1249 target_cmsg_start
= target_cmsg
;
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
= tswapal(target_cmsg
->cmsg_len
)
1258 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr
));
1260 space
+= CMSG_SPACE(len
);
1261 if (space
> msgh
->msg_controllen
) {
1262 space
-= CMSG_SPACE(len
);
1263 /* This is a QEMU bug, since we allocated the payload
1264 * area ourselves (unlike overflow in host-to-target
1265 * conversion, which is just the guest giving us a buffer
1266 * that's too small). It can't happen for the payload types
1267 * we currently support; if it becomes an issue in future
1268 * we would need to improve our allocation strategy to
1269 * something more intelligent than "twice the size of the
1270 * target buffer we're reading from".
1272 gemu_log("Host cmsg overflow\n");
1276 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1277 cmsg
->cmsg_level
= SOL_SOCKET
;
1279 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1281 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1282 cmsg
->cmsg_len
= CMSG_LEN(len
);
1284 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1285 int *fd
= (int *)data
;
1286 int *target_fd
= (int *)target_data
;
1287 int i
, numfds
= len
/ sizeof(int);
1289 for (i
= 0; i
< numfds
; i
++) {
1290 __get_user(fd
[i
], target_fd
+ i
);
1292 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1293 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1294 struct ucred
*cred
= (struct ucred
*)data
;
1295 struct target_ucred
*target_cred
=
1296 (struct target_ucred
*)target_data
;
1298 __get_user(cred
->pid
, &target_cred
->pid
);
1299 __get_user(cred
->uid
, &target_cred
->uid
);
1300 __get_user(cred
->gid
, &target_cred
->gid
);
1302 gemu_log("Unsupported ancillary data: %d/%d\n",
1303 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1304 memcpy(data
, target_data
, len
);
1307 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1308 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1311 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1313 msgh
->msg_controllen
= space
;
1317 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1318 struct msghdr
*msgh
)
1320 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1321 abi_long msg_controllen
;
1322 abi_ulong target_cmsg_addr
;
1323 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1324 socklen_t space
= 0;
1326 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1327 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1329 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1330 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1331 target_cmsg_start
= target_cmsg
;
1333 return -TARGET_EFAULT
;
1335 while (cmsg
&& target_cmsg
) {
1336 void *data
= CMSG_DATA(cmsg
);
1337 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1339 int len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof (struct cmsghdr
));
1340 int tgt_len
, tgt_space
;
1342 /* We never copy a half-header but may copy half-data;
1343 * this is Linux's behaviour in put_cmsg(). Note that
1344 * truncation here is a guest problem (which we report
1345 * to the guest via the CTRUNC bit), unlike truncation
1346 * in target_to_host_cmsg, which is a QEMU bug.
1348 if (msg_controllen
< sizeof(struct cmsghdr
)) {
1349 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1353 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1354 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1356 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1358 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1360 tgt_len
= TARGET_CMSG_LEN(len
);
1362 /* Payload types which need a different size of payload on
1363 * the target must adjust tgt_len here.
1365 switch (cmsg
->cmsg_level
) {
1367 switch (cmsg
->cmsg_type
) {
1369 tgt_len
= sizeof(struct target_timeval
);
1378 if (msg_controllen
< tgt_len
) {
1379 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1380 tgt_len
= msg_controllen
;
1383 /* We must now copy-and-convert len bytes of payload
1384 * into tgt_len bytes of destination space. Bear in mind
1385 * that in both source and destination we may be dealing
1386 * with a truncated value!
1388 switch (cmsg
->cmsg_level
) {
1390 switch (cmsg
->cmsg_type
) {
1393 int *fd
= (int *)data
;
1394 int *target_fd
= (int *)target_data
;
1395 int i
, numfds
= tgt_len
/ sizeof(int);
1397 for (i
= 0; i
< numfds
; i
++) {
1398 __put_user(fd
[i
], target_fd
+ i
);
1404 struct timeval
*tv
= (struct timeval
*)data
;
1405 struct target_timeval
*target_tv
=
1406 (struct target_timeval
*)target_data
;
1408 if (len
!= sizeof(struct timeval
) ||
1409 tgt_len
!= sizeof(struct target_timeval
)) {
1413 /* copy struct timeval to target */
1414 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1415 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1418 case SCM_CREDENTIALS
:
1420 struct ucred
*cred
= (struct ucred
*)data
;
1421 struct target_ucred
*target_cred
=
1422 (struct target_ucred
*)target_data
;
1424 __put_user(cred
->pid
, &target_cred
->pid
);
1425 __put_user(cred
->uid
, &target_cred
->uid
);
1426 __put_user(cred
->gid
, &target_cred
->gid
);
1436 gemu_log("Unsupported ancillary data: %d/%d\n",
1437 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1438 memcpy(target_data
, data
, MIN(len
, tgt_len
));
1439 if (tgt_len
> len
) {
1440 memset(target_data
+ len
, 0, tgt_len
- len
);
1444 target_cmsg
->cmsg_len
= tswapal(tgt_len
);
1445 tgt_space
= TARGET_CMSG_SPACE(len
);
1446 if (msg_controllen
< tgt_space
) {
1447 tgt_space
= msg_controllen
;
1449 msg_controllen
-= tgt_space
;
1451 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1452 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1455 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1457 target_msgh
->msg_controllen
= tswapal(space
);
1461 /* do_setsockopt() Must return target values and target errnos. */
1462 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1463 abi_ulong optval_addr
, socklen_t optlen
)
1467 struct ip_mreqn
*ip_mreq
;
1468 struct ip_mreq_source
*ip_mreq_source
;
1472 /* TCP options all take an 'int' value. */
1473 if (optlen
< sizeof(uint32_t))
1474 return -TARGET_EINVAL
;
1476 if (get_user_u32(val
, optval_addr
))
1477 return -TARGET_EFAULT
;
1478 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1485 case IP_ROUTER_ALERT
:
1489 case IP_MTU_DISCOVER
:
1495 case IP_MULTICAST_TTL
:
1496 case IP_MULTICAST_LOOP
:
1498 if (optlen
>= sizeof(uint32_t)) {
1499 if (get_user_u32(val
, optval_addr
))
1500 return -TARGET_EFAULT
;
1501 } else if (optlen
>= 1) {
1502 if (get_user_u8(val
, optval_addr
))
1503 return -TARGET_EFAULT
;
1505 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1507 case IP_ADD_MEMBERSHIP
:
1508 case IP_DROP_MEMBERSHIP
:
1509 if (optlen
< sizeof (struct target_ip_mreq
) ||
1510 optlen
> sizeof (struct target_ip_mreqn
))
1511 return -TARGET_EINVAL
;
1513 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1514 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1515 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1518 case IP_BLOCK_SOURCE
:
1519 case IP_UNBLOCK_SOURCE
:
1520 case IP_ADD_SOURCE_MEMBERSHIP
:
1521 case IP_DROP_SOURCE_MEMBERSHIP
:
1522 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1523 return -TARGET_EINVAL
;
1525 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1526 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1527 unlock_user (ip_mreq_source
, optval_addr
, 0);
1536 case IPV6_MTU_DISCOVER
:
1539 case IPV6_RECVPKTINFO
:
1541 if (optlen
< sizeof(uint32_t)) {
1542 return -TARGET_EINVAL
;
1544 if (get_user_u32(val
, optval_addr
)) {
1545 return -TARGET_EFAULT
;
1547 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1548 &val
, sizeof(val
)));
1557 /* struct icmp_filter takes an u32 value */
1558 if (optlen
< sizeof(uint32_t)) {
1559 return -TARGET_EINVAL
;
1562 if (get_user_u32(val
, optval_addr
)) {
1563 return -TARGET_EFAULT
;
1565 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1566 &val
, sizeof(val
)));
1573 case TARGET_SOL_SOCKET
:
1575 case TARGET_SO_RCVTIMEO
:
1579 optname
= SO_RCVTIMEO
;
1582 if (optlen
!= sizeof(struct target_timeval
)) {
1583 return -TARGET_EINVAL
;
1586 if (copy_from_user_timeval(&tv
, optval_addr
)) {
1587 return -TARGET_EFAULT
;
1590 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
1594 case TARGET_SO_SNDTIMEO
:
1595 optname
= SO_SNDTIMEO
;
1597 case TARGET_SO_ATTACH_FILTER
:
1599 struct target_sock_fprog
*tfprog
;
1600 struct target_sock_filter
*tfilter
;
1601 struct sock_fprog fprog
;
1602 struct sock_filter
*filter
;
1605 if (optlen
!= sizeof(*tfprog
)) {
1606 return -TARGET_EINVAL
;
1608 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
1609 return -TARGET_EFAULT
;
1611 if (!lock_user_struct(VERIFY_READ
, tfilter
,
1612 tswapal(tfprog
->filter
), 0)) {
1613 unlock_user_struct(tfprog
, optval_addr
, 1);
1614 return -TARGET_EFAULT
;
1617 fprog
.len
= tswap16(tfprog
->len
);
1618 filter
= g_try_new(struct sock_filter
, fprog
.len
);
1619 if (filter
== NULL
) {
1620 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1621 unlock_user_struct(tfprog
, optval_addr
, 1);
1622 return -TARGET_ENOMEM
;
1624 for (i
= 0; i
< fprog
.len
; i
++) {
1625 filter
[i
].code
= tswap16(tfilter
[i
].code
);
1626 filter
[i
].jt
= tfilter
[i
].jt
;
1627 filter
[i
].jf
= tfilter
[i
].jf
;
1628 filter
[i
].k
= tswap32(tfilter
[i
].k
);
1630 fprog
.filter
= filter
;
1632 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
1633 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
1636 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1637 unlock_user_struct(tfprog
, optval_addr
, 1);
1640 case TARGET_SO_BINDTODEVICE
:
1642 char *dev_ifname
, *addr_ifname
;
1644 if (optlen
> IFNAMSIZ
- 1) {
1645 optlen
= IFNAMSIZ
- 1;
1647 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1649 return -TARGET_EFAULT
;
1651 optname
= SO_BINDTODEVICE
;
1652 addr_ifname
= alloca(IFNAMSIZ
);
1653 memcpy(addr_ifname
, dev_ifname
, optlen
);
1654 addr_ifname
[optlen
] = 0;
1655 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
1656 addr_ifname
, optlen
));
1657 unlock_user (dev_ifname
, optval_addr
, 0);
1660 /* Options with 'int' argument. */
1661 case TARGET_SO_DEBUG
:
1664 case TARGET_SO_REUSEADDR
:
1665 optname
= SO_REUSEADDR
;
1667 case TARGET_SO_TYPE
:
1670 case TARGET_SO_ERROR
:
1673 case TARGET_SO_DONTROUTE
:
1674 optname
= SO_DONTROUTE
;
1676 case TARGET_SO_BROADCAST
:
1677 optname
= SO_BROADCAST
;
1679 case TARGET_SO_SNDBUF
:
1680 optname
= SO_SNDBUF
;
1682 case TARGET_SO_SNDBUFFORCE
:
1683 optname
= SO_SNDBUFFORCE
;
1685 case TARGET_SO_RCVBUF
:
1686 optname
= SO_RCVBUF
;
1688 case TARGET_SO_RCVBUFFORCE
:
1689 optname
= SO_RCVBUFFORCE
;
1691 case TARGET_SO_KEEPALIVE
:
1692 optname
= SO_KEEPALIVE
;
1694 case TARGET_SO_OOBINLINE
:
1695 optname
= SO_OOBINLINE
;
1697 case TARGET_SO_NO_CHECK
:
1698 optname
= SO_NO_CHECK
;
1700 case TARGET_SO_PRIORITY
:
1701 optname
= SO_PRIORITY
;
1704 case TARGET_SO_BSDCOMPAT
:
1705 optname
= SO_BSDCOMPAT
;
1708 case TARGET_SO_PASSCRED
:
1709 optname
= SO_PASSCRED
;
1711 case TARGET_SO_PASSSEC
:
1712 optname
= SO_PASSSEC
;
1714 case TARGET_SO_TIMESTAMP
:
1715 optname
= SO_TIMESTAMP
;
1717 case TARGET_SO_RCVLOWAT
:
1718 optname
= SO_RCVLOWAT
;
1724 if (optlen
< sizeof(uint32_t))
1725 return -TARGET_EINVAL
;
1727 if (get_user_u32(val
, optval_addr
))
1728 return -TARGET_EFAULT
;
1729 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
1733 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level
, optname
);
1734 ret
= -TARGET_ENOPROTOOPT
;
1739 /* do_getsockopt() Must return target values and target errnos. */
1740 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
1741 abi_ulong optval_addr
, abi_ulong optlen
)
1748 case TARGET_SOL_SOCKET
:
1751 /* These don't just return a single integer */
1752 case TARGET_SO_LINGER
:
1753 case TARGET_SO_RCVTIMEO
:
1754 case TARGET_SO_SNDTIMEO
:
1755 case TARGET_SO_PEERNAME
:
1757 case TARGET_SO_PEERCRED
: {
1760 struct target_ucred
*tcr
;
1762 if (get_user_u32(len
, optlen
)) {
1763 return -TARGET_EFAULT
;
1766 return -TARGET_EINVAL
;
1770 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
1778 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
1779 return -TARGET_EFAULT
;
1781 __put_user(cr
.pid
, &tcr
->pid
);
1782 __put_user(cr
.uid
, &tcr
->uid
);
1783 __put_user(cr
.gid
, &tcr
->gid
);
1784 unlock_user_struct(tcr
, optval_addr
, 1);
1785 if (put_user_u32(len
, optlen
)) {
1786 return -TARGET_EFAULT
;
1790 /* Options with 'int' argument. */
1791 case TARGET_SO_DEBUG
:
1794 case TARGET_SO_REUSEADDR
:
1795 optname
= SO_REUSEADDR
;
1797 case TARGET_SO_TYPE
:
1800 case TARGET_SO_ERROR
:
1803 case TARGET_SO_DONTROUTE
:
1804 optname
= SO_DONTROUTE
;
1806 case TARGET_SO_BROADCAST
:
1807 optname
= SO_BROADCAST
;
1809 case TARGET_SO_SNDBUF
:
1810 optname
= SO_SNDBUF
;
1812 case TARGET_SO_RCVBUF
:
1813 optname
= SO_RCVBUF
;
1815 case TARGET_SO_KEEPALIVE
:
1816 optname
= SO_KEEPALIVE
;
1818 case TARGET_SO_OOBINLINE
:
1819 optname
= SO_OOBINLINE
;
1821 case TARGET_SO_NO_CHECK
:
1822 optname
= SO_NO_CHECK
;
1824 case TARGET_SO_PRIORITY
:
1825 optname
= SO_PRIORITY
;
1828 case TARGET_SO_BSDCOMPAT
:
1829 optname
= SO_BSDCOMPAT
;
1832 case TARGET_SO_PASSCRED
:
1833 optname
= SO_PASSCRED
;
1835 case TARGET_SO_TIMESTAMP
:
1836 optname
= SO_TIMESTAMP
;
1838 case TARGET_SO_RCVLOWAT
:
1839 optname
= SO_RCVLOWAT
;
1841 case TARGET_SO_ACCEPTCONN
:
1842 optname
= SO_ACCEPTCONN
;
1849 /* TCP options all take an 'int' value. */
1851 if (get_user_u32(len
, optlen
))
1852 return -TARGET_EFAULT
;
1854 return -TARGET_EINVAL
;
1856 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1859 if (optname
== SO_TYPE
) {
1860 val
= host_to_target_sock_type(val
);
1865 if (put_user_u32(val
, optval_addr
))
1866 return -TARGET_EFAULT
;
1868 if (put_user_u8(val
, optval_addr
))
1869 return -TARGET_EFAULT
;
1871 if (put_user_u32(len
, optlen
))
1872 return -TARGET_EFAULT
;
1879 case IP_ROUTER_ALERT
:
1883 case IP_MTU_DISCOVER
:
1889 case IP_MULTICAST_TTL
:
1890 case IP_MULTICAST_LOOP
:
1891 if (get_user_u32(len
, optlen
))
1892 return -TARGET_EFAULT
;
1894 return -TARGET_EINVAL
;
1896 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1899 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
1901 if (put_user_u32(len
, optlen
)
1902 || put_user_u8(val
, optval_addr
))
1903 return -TARGET_EFAULT
;
1905 if (len
> sizeof(int))
1907 if (put_user_u32(len
, optlen
)
1908 || put_user_u32(val
, optval_addr
))
1909 return -TARGET_EFAULT
;
1913 ret
= -TARGET_ENOPROTOOPT
;
1919 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1921 ret
= -TARGET_EOPNOTSUPP
;
1927 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
1928 int count
, int copy
)
1930 struct target_iovec
*target_vec
;
1932 abi_ulong total_len
, max_len
;
1935 bool bad_address
= false;
1941 if (count
< 0 || count
> IOV_MAX
) {
1946 vec
= g_try_new0(struct iovec
, count
);
1952 target_vec
= lock_user(VERIFY_READ
, target_addr
,
1953 count
* sizeof(struct target_iovec
), 1);
1954 if (target_vec
== NULL
) {
1959 /* ??? If host page size > target page size, this will result in a
1960 value larger than what we can actually support. */
1961 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
1964 for (i
= 0; i
< count
; i
++) {
1965 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
1966 abi_long len
= tswapal(target_vec
[i
].iov_len
);
1971 } else if (len
== 0) {
1972 /* Zero length pointer is ignored. */
1973 vec
[i
].iov_base
= 0;
1975 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
1976 /* If the first buffer pointer is bad, this is a fault. But
1977 * subsequent bad buffers will result in a partial write; this
1978 * is realized by filling the vector with null pointers and
1980 if (!vec
[i
].iov_base
) {
1991 if (len
> max_len
- total_len
) {
1992 len
= max_len
- total_len
;
1995 vec
[i
].iov_len
= len
;
1999 unlock_user(target_vec
, target_addr
, 0);
2004 if (tswapal(target_vec
[i
].iov_len
) > 0) {
2005 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
2008 unlock_user(target_vec
, target_addr
, 0);
2015 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
2016 int count
, int copy
)
2018 struct target_iovec
*target_vec
;
2021 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2022 count
* sizeof(struct target_iovec
), 1);
2024 for (i
= 0; i
< count
; i
++) {
2025 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2026 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2030 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
2032 unlock_user(target_vec
, target_addr
, 0);
2038 static inline int target_to_host_sock_type(int *type
)
2041 int target_type
= *type
;
2043 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
2044 case TARGET_SOCK_DGRAM
:
2045 host_type
= SOCK_DGRAM
;
2047 case TARGET_SOCK_STREAM
:
2048 host_type
= SOCK_STREAM
;
2051 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
2054 if (target_type
& TARGET_SOCK_CLOEXEC
) {
2055 #if defined(SOCK_CLOEXEC)
2056 host_type
|= SOCK_CLOEXEC
;
2058 return -TARGET_EINVAL
;
2061 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2062 #if defined(SOCK_NONBLOCK)
2063 host_type
|= SOCK_NONBLOCK
;
2064 #elif !defined(O_NONBLOCK)
2065 return -TARGET_EINVAL
;
2072 /* Try to emulate socket type flags after socket creation. */
2073 static int sock_flags_fixup(int fd
, int target_type
)
2075 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2076 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2077 int flags
= fcntl(fd
, F_GETFL
);
2078 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
2080 return -TARGET_EINVAL
;
2087 static abi_long
packet_target_to_host_sockaddr(void *host_addr
,
2088 abi_ulong target_addr
,
2091 struct sockaddr
*addr
= host_addr
;
2092 struct target_sockaddr
*target_saddr
;
2094 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
2095 if (!target_saddr
) {
2096 return -TARGET_EFAULT
;
2099 memcpy(addr
, target_saddr
, len
);
2100 addr
->sa_family
= tswap16(target_saddr
->sa_family
);
2101 /* spkt_protocol is big-endian */
2103 unlock_user(target_saddr
, target_addr
, 0);
2107 static TargetFdTrans target_packet_trans
= {
2108 .target_to_host_addr
= packet_target_to_host_sockaddr
,
2111 /* do_socket() Must return target values and target errnos. */
2112 static abi_long
do_socket(int domain
, int type
, int protocol
)
2114 int target_type
= type
;
2117 ret
= target_to_host_sock_type(&type
);
2122 if (domain
== PF_NETLINK
)
2123 return -TARGET_EAFNOSUPPORT
;
2125 if (domain
== AF_PACKET
||
2126 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
2127 protocol
= tswap16(protocol
);
2130 ret
= get_errno(socket(domain
, type
, protocol
));
2132 ret
= sock_flags_fixup(ret
, target_type
);
2133 if (type
== SOCK_PACKET
) {
2134 /* Manage an obsolete case :
2135 * if socket type is SOCK_PACKET, bind by name
2137 fd_trans_register(ret
, &target_packet_trans
);
2143 /* do_bind() Must return target values and target errnos. */
2144 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
2150 if ((int)addrlen
< 0) {
2151 return -TARGET_EINVAL
;
2154 addr
= alloca(addrlen
+1);
2156 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
2160 return get_errno(bind(sockfd
, addr
, addrlen
));
2163 /* do_connect() Must return target values and target errnos. */
2164 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
2170 if ((int)addrlen
< 0) {
2171 return -TARGET_EINVAL
;
2174 addr
= alloca(addrlen
+1);
2176 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
2180 return get_errno(connect(sockfd
, addr
, addrlen
));
2183 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
2184 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
2185 int flags
, int send
)
2191 abi_ulong target_vec
;
2193 if (msgp
->msg_name
) {
2194 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
2195 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
2196 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
2197 tswapal(msgp
->msg_name
),
2203 msg
.msg_name
= NULL
;
2204 msg
.msg_namelen
= 0;
2206 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
2207 msg
.msg_control
= alloca(msg
.msg_controllen
);
2208 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
2210 count
= tswapal(msgp
->msg_iovlen
);
2211 target_vec
= tswapal(msgp
->msg_iov
);
2212 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
2213 target_vec
, count
, send
);
2215 ret
= -host_to_target_errno(errno
);
2218 msg
.msg_iovlen
= count
;
2222 ret
= target_to_host_cmsg(&msg
, msgp
);
2224 ret
= get_errno(sendmsg(fd
, &msg
, flags
));
2226 ret
= get_errno(recvmsg(fd
, &msg
, flags
));
2227 if (!is_error(ret
)) {
2229 ret
= host_to_target_cmsg(msgp
, &msg
);
2230 if (!is_error(ret
)) {
2231 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
2232 if (msg
.msg_name
!= NULL
) {
2233 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
2234 msg
.msg_name
, msg
.msg_namelen
);
2246 unlock_iovec(vec
, target_vec
, count
, !send
);
2251 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
2252 int flags
, int send
)
2255 struct target_msghdr
*msgp
;
2257 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
2261 return -TARGET_EFAULT
;
2263 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
2264 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
2268 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
2269 * so it might not have this *mmsg-specific flag either.
2271 #ifndef MSG_WAITFORONE
2272 #define MSG_WAITFORONE 0x10000
2275 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
2276 unsigned int vlen
, unsigned int flags
,
2279 struct target_mmsghdr
*mmsgp
;
2283 if (vlen
> UIO_MAXIOV
) {
2287 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
2289 return -TARGET_EFAULT
;
2292 for (i
= 0; i
< vlen
; i
++) {
2293 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
2294 if (is_error(ret
)) {
2297 mmsgp
[i
].msg_len
= tswap32(ret
);
2298 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2299 if (flags
& MSG_WAITFORONE
) {
2300 flags
|= MSG_DONTWAIT
;
2304 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
2306 /* Return number of datagrams sent if we sent any at all;
2307 * otherwise return the error.
2315 /* If we don't have a system accept4() then just call accept.
2316 * The callsites to do_accept4() will ensure that they don't
2317 * pass a non-zero flags argument in this config.
2319 #ifndef CONFIG_ACCEPT4
2320 static inline int accept4(int sockfd
, struct sockaddr
*addr
,
2321 socklen_t
*addrlen
, int flags
)
2324 return accept(sockfd
, addr
, addrlen
);
2328 /* do_accept4() Must return target values and target errnos. */
2329 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
2330 abi_ulong target_addrlen_addr
, int flags
)
2337 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
2339 if (target_addr
== 0) {
2340 return get_errno(accept4(fd
, NULL
, NULL
, host_flags
));
2343 /* linux returns EINVAL if addrlen pointer is invalid */
2344 if (get_user_u32(addrlen
, target_addrlen_addr
))
2345 return -TARGET_EINVAL
;
2347 if ((int)addrlen
< 0) {
2348 return -TARGET_EINVAL
;
2351 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2352 return -TARGET_EINVAL
;
2354 addr
= alloca(addrlen
);
2356 ret
= get_errno(accept4(fd
, addr
, &addrlen
, host_flags
));
2357 if (!is_error(ret
)) {
2358 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2359 if (put_user_u32(addrlen
, target_addrlen_addr
))
2360 ret
= -TARGET_EFAULT
;
2365 /* do_getpeername() Must return target values and target errnos. */
2366 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
2367 abi_ulong target_addrlen_addr
)
2373 if (get_user_u32(addrlen
, target_addrlen_addr
))
2374 return -TARGET_EFAULT
;
2376 if ((int)addrlen
< 0) {
2377 return -TARGET_EINVAL
;
2380 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2381 return -TARGET_EFAULT
;
2383 addr
= alloca(addrlen
);
2385 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
2386 if (!is_error(ret
)) {
2387 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2388 if (put_user_u32(addrlen
, target_addrlen_addr
))
2389 ret
= -TARGET_EFAULT
;
2394 /* do_getsockname() Must return target values and target errnos. */
2395 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
2396 abi_ulong target_addrlen_addr
)
2402 if (get_user_u32(addrlen
, target_addrlen_addr
))
2403 return -TARGET_EFAULT
;
2405 if ((int)addrlen
< 0) {
2406 return -TARGET_EINVAL
;
2409 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2410 return -TARGET_EFAULT
;
2412 addr
= alloca(addrlen
);
2414 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
2415 if (!is_error(ret
)) {
2416 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2417 if (put_user_u32(addrlen
, target_addrlen_addr
))
2418 ret
= -TARGET_EFAULT
;
2423 /* do_socketpair() Must return target values and target errnos. */
2424 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
2425 abi_ulong target_tab_addr
)
2430 target_to_host_sock_type(&type
);
2432 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
2433 if (!is_error(ret
)) {
2434 if (put_user_s32(tab
[0], target_tab_addr
)
2435 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
2436 ret
= -TARGET_EFAULT
;
2441 /* do_sendto() Must return target values and target errnos. */
2442 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
2443 abi_ulong target_addr
, socklen_t addrlen
)
2449 if ((int)addrlen
< 0) {
2450 return -TARGET_EINVAL
;
2453 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
2455 return -TARGET_EFAULT
;
2457 addr
= alloca(addrlen
+1);
2458 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
2460 unlock_user(host_msg
, msg
, 0);
2463 ret
= get_errno(sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
2465 ret
= get_errno(send(fd
, host_msg
, len
, flags
));
2467 unlock_user(host_msg
, msg
, 0);
2471 /* do_recvfrom() Must return target values and target errnos. */
2472 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
2473 abi_ulong target_addr
,
2474 abi_ulong target_addrlen
)
2481 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
2483 return -TARGET_EFAULT
;
2485 if (get_user_u32(addrlen
, target_addrlen
)) {
2486 ret
= -TARGET_EFAULT
;
2489 if ((int)addrlen
< 0) {
2490 ret
= -TARGET_EINVAL
;
2493 addr
= alloca(addrlen
);
2494 ret
= get_errno(recvfrom(fd
, host_msg
, len
, flags
, addr
, &addrlen
));
2496 addr
= NULL
; /* To keep compiler quiet. */
2497 ret
= get_errno(qemu_recv(fd
, host_msg
, len
, flags
));
2499 if (!is_error(ret
)) {
2501 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2502 if (put_user_u32(addrlen
, target_addrlen
)) {
2503 ret
= -TARGET_EFAULT
;
2507 unlock_user(host_msg
, msg
, len
);
2510 unlock_user(host_msg
, msg
, 0);
2515 #ifdef TARGET_NR_socketcall
2516 /* do_socketcall() Must return target values and target errnos. */
2517 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
2519 static const unsigned ac
[] = { /* number of arguments per call */
2520 [SOCKOP_socket
] = 3, /* domain, type, protocol */
2521 [SOCKOP_bind
] = 3, /* sockfd, addr, addrlen */
2522 [SOCKOP_connect
] = 3, /* sockfd, addr, addrlen */
2523 [SOCKOP_listen
] = 2, /* sockfd, backlog */
2524 [SOCKOP_accept
] = 3, /* sockfd, addr, addrlen */
2525 [SOCKOP_accept4
] = 4, /* sockfd, addr, addrlen, flags */
2526 [SOCKOP_getsockname
] = 3, /* sockfd, addr, addrlen */
2527 [SOCKOP_getpeername
] = 3, /* sockfd, addr, addrlen */
2528 [SOCKOP_socketpair
] = 4, /* domain, type, protocol, tab */
2529 [SOCKOP_send
] = 4, /* sockfd, msg, len, flags */
2530 [SOCKOP_recv
] = 4, /* sockfd, msg, len, flags */
2531 [SOCKOP_sendto
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2532 [SOCKOP_recvfrom
] = 6, /* sockfd, msg, len, flags, addr, addrlen */
2533 [SOCKOP_shutdown
] = 2, /* sockfd, how */
2534 [SOCKOP_sendmsg
] = 3, /* sockfd, msg, flags */
2535 [SOCKOP_recvmsg
] = 3, /* sockfd, msg, flags */
2536 [SOCKOP_sendmmsg
] = 4, /* sockfd, msgvec, vlen, flags */
2537 [SOCKOP_recvmmsg
] = 4, /* sockfd, msgvec, vlen, flags */
2538 [SOCKOP_setsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
2539 [SOCKOP_getsockopt
] = 5, /* sockfd, level, optname, optval, optlen */
2541 abi_long a
[6]; /* max 6 args */
2543 /* first, collect the arguments in a[] according to ac[] */
2544 if (num
>= 0 && num
< ARRAY_SIZE(ac
)) {
2546 assert(ARRAY_SIZE(a
) >= ac
[num
]); /* ensure we have space for args */
2547 for (i
= 0; i
< ac
[num
]; ++i
) {
2548 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
2549 return -TARGET_EFAULT
;
2554 /* now when we have the args, actually handle the call */
2556 case SOCKOP_socket
: /* domain, type, protocol */
2557 return do_socket(a
[0], a
[1], a
[2]);
2558 case SOCKOP_bind
: /* sockfd, addr, addrlen */
2559 return do_bind(a
[0], a
[1], a
[2]);
2560 case SOCKOP_connect
: /* sockfd, addr, addrlen */
2561 return do_connect(a
[0], a
[1], a
[2]);
2562 case SOCKOP_listen
: /* sockfd, backlog */
2563 return get_errno(listen(a
[0], a
[1]));
2564 case SOCKOP_accept
: /* sockfd, addr, addrlen */
2565 return do_accept4(a
[0], a
[1], a
[2], 0);
2566 case SOCKOP_accept4
: /* sockfd, addr, addrlen, flags */
2567 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
2568 case SOCKOP_getsockname
: /* sockfd, addr, addrlen */
2569 return do_getsockname(a
[0], a
[1], a
[2]);
2570 case SOCKOP_getpeername
: /* sockfd, addr, addrlen */
2571 return do_getpeername(a
[0], a
[1], a
[2]);
2572 case SOCKOP_socketpair
: /* domain, type, protocol, tab */
2573 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
2574 case SOCKOP_send
: /* sockfd, msg, len, flags */
2575 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
2576 case SOCKOP_recv
: /* sockfd, msg, len, flags */
2577 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
2578 case SOCKOP_sendto
: /* sockfd, msg, len, flags, addr, addrlen */
2579 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
2580 case SOCKOP_recvfrom
: /* sockfd, msg, len, flags, addr, addrlen */
2581 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
2582 case SOCKOP_shutdown
: /* sockfd, how */
2583 return get_errno(shutdown(a
[0], a
[1]));
2584 case SOCKOP_sendmsg
: /* sockfd, msg, flags */
2585 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
2586 case SOCKOP_recvmsg
: /* sockfd, msg, flags */
2587 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
2588 case SOCKOP_sendmmsg
: /* sockfd, msgvec, vlen, flags */
2589 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
2590 case SOCKOP_recvmmsg
: /* sockfd, msgvec, vlen, flags */
2591 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
2592 case SOCKOP_setsockopt
: /* sockfd, level, optname, optval, optlen */
2593 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
2594 case SOCKOP_getsockopt
: /* sockfd, level, optname, optval, optlen */
2595 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
2597 gemu_log("Unsupported socketcall: %d\n", num
);
2598 return -TARGET_ENOSYS
;
2603 #define N_SHM_REGIONS 32
2605 static struct shm_region
{
2609 } shm_regions
[N_SHM_REGIONS
];
2611 struct target_semid_ds
2613 struct target_ipc_perm sem_perm
;
2614 abi_ulong sem_otime
;
2615 #if !defined(TARGET_PPC64)
2616 abi_ulong __unused1
;
2618 abi_ulong sem_ctime
;
2619 #if !defined(TARGET_PPC64)
2620 abi_ulong __unused2
;
2622 abi_ulong sem_nsems
;
2623 abi_ulong __unused3
;
2624 abi_ulong __unused4
;
2627 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
2628 abi_ulong target_addr
)
2630 struct target_ipc_perm
*target_ip
;
2631 struct target_semid_ds
*target_sd
;
2633 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2634 return -TARGET_EFAULT
;
2635 target_ip
= &(target_sd
->sem_perm
);
2636 host_ip
->__key
= tswap32(target_ip
->__key
);
2637 host_ip
->uid
= tswap32(target_ip
->uid
);
2638 host_ip
->gid
= tswap32(target_ip
->gid
);
2639 host_ip
->cuid
= tswap32(target_ip
->cuid
);
2640 host_ip
->cgid
= tswap32(target_ip
->cgid
);
2641 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2642 host_ip
->mode
= tswap32(target_ip
->mode
);
2644 host_ip
->mode
= tswap16(target_ip
->mode
);
2646 #if defined(TARGET_PPC)
2647 host_ip
->__seq
= tswap32(target_ip
->__seq
);
2649 host_ip
->__seq
= tswap16(target_ip
->__seq
);
2651 unlock_user_struct(target_sd
, target_addr
, 0);
2655 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
2656 struct ipc_perm
*host_ip
)
2658 struct target_ipc_perm
*target_ip
;
2659 struct target_semid_ds
*target_sd
;
2661 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2662 return -TARGET_EFAULT
;
2663 target_ip
= &(target_sd
->sem_perm
);
2664 target_ip
->__key
= tswap32(host_ip
->__key
);
2665 target_ip
->uid
= tswap32(host_ip
->uid
);
2666 target_ip
->gid
= tswap32(host_ip
->gid
);
2667 target_ip
->cuid
= tswap32(host_ip
->cuid
);
2668 target_ip
->cgid
= tswap32(host_ip
->cgid
);
2669 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
2670 target_ip
->mode
= tswap32(host_ip
->mode
);
2672 target_ip
->mode
= tswap16(host_ip
->mode
);
2674 #if defined(TARGET_PPC)
2675 target_ip
->__seq
= tswap32(host_ip
->__seq
);
2677 target_ip
->__seq
= tswap16(host_ip
->__seq
);
2679 unlock_user_struct(target_sd
, target_addr
, 1);
2683 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
2684 abi_ulong target_addr
)
2686 struct target_semid_ds
*target_sd
;
2688 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2689 return -TARGET_EFAULT
;
2690 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
2691 return -TARGET_EFAULT
;
2692 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
2693 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
2694 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
2695 unlock_user_struct(target_sd
, target_addr
, 0);
2699 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
2700 struct semid_ds
*host_sd
)
2702 struct target_semid_ds
*target_sd
;
2704 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2705 return -TARGET_EFAULT
;
2706 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
2707 return -TARGET_EFAULT
;
2708 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
2709 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
2710 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
2711 unlock_user_struct(target_sd
, target_addr
, 1);
2715 struct target_seminfo
{
2728 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
2729 struct seminfo
*host_seminfo
)
2731 struct target_seminfo
*target_seminfo
;
2732 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
2733 return -TARGET_EFAULT
;
2734 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
2735 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
2736 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
2737 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
2738 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
2739 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
2740 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
2741 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
2742 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
2743 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
2744 unlock_user_struct(target_seminfo
, target_addr
, 1);
2750 struct semid_ds
*buf
;
2751 unsigned short *array
;
2752 struct seminfo
*__buf
;
2755 union target_semun
{
2762 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
2763 abi_ulong target_addr
)
2766 unsigned short *array
;
2768 struct semid_ds semid_ds
;
2771 semun
.buf
= &semid_ds
;
2773 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2775 return get_errno(ret
);
2777 nsems
= semid_ds
.sem_nsems
;
2779 *host_array
= g_try_new(unsigned short, nsems
);
2781 return -TARGET_ENOMEM
;
2783 array
= lock_user(VERIFY_READ
, target_addr
,
2784 nsems
*sizeof(unsigned short), 1);
2786 g_free(*host_array
);
2787 return -TARGET_EFAULT
;
2790 for(i
=0; i
<nsems
; i
++) {
2791 __get_user((*host_array
)[i
], &array
[i
]);
2793 unlock_user(array
, target_addr
, 0);
2798 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
2799 unsigned short **host_array
)
2802 unsigned short *array
;
2804 struct semid_ds semid_ds
;
2807 semun
.buf
= &semid_ds
;
2809 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2811 return get_errno(ret
);
2813 nsems
= semid_ds
.sem_nsems
;
2815 array
= lock_user(VERIFY_WRITE
, target_addr
,
2816 nsems
*sizeof(unsigned short), 0);
2818 return -TARGET_EFAULT
;
2820 for(i
=0; i
<nsems
; i
++) {
2821 __put_user((*host_array
)[i
], &array
[i
]);
2823 g_free(*host_array
);
2824 unlock_user(array
, target_addr
, 1);
2829 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
2830 abi_ulong target_arg
)
2832 union target_semun target_su
= { .buf
= target_arg
};
2834 struct semid_ds dsarg
;
2835 unsigned short *array
= NULL
;
2836 struct seminfo seminfo
;
2837 abi_long ret
= -TARGET_EINVAL
;
2844 /* In 64 bit cross-endian situations, we will erroneously pick up
2845 * the wrong half of the union for the "val" element. To rectify
2846 * this, the entire 8-byte structure is byteswapped, followed by
2847 * a swap of the 4 byte val field. In other cases, the data is
2848 * already in proper host byte order. */
2849 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
2850 target_su
.buf
= tswapal(target_su
.buf
);
2851 arg
.val
= tswap32(target_su
.val
);
2853 arg
.val
= target_su
.val
;
2855 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2859 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
2863 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2864 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
2871 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
2875 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2876 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
2882 arg
.__buf
= &seminfo
;
2883 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2884 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
2892 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
2899 struct target_sembuf
{
2900 unsigned short sem_num
;
2905 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
2906 abi_ulong target_addr
,
2909 struct target_sembuf
*target_sembuf
;
2912 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
2913 nsops
*sizeof(struct target_sembuf
), 1);
2915 return -TARGET_EFAULT
;
2917 for(i
=0; i
<nsops
; i
++) {
2918 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
2919 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
2920 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
2923 unlock_user(target_sembuf
, target_addr
, 0);
2928 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
2930 struct sembuf sops
[nsops
];
2932 if (target_to_host_sembuf(sops
, ptr
, nsops
))
2933 return -TARGET_EFAULT
;
2935 return get_errno(semop(semid
, sops
, nsops
));
2938 struct target_msqid_ds
2940 struct target_ipc_perm msg_perm
;
2941 abi_ulong msg_stime
;
2942 #if TARGET_ABI_BITS == 32
2943 abi_ulong __unused1
;
2945 abi_ulong msg_rtime
;
2946 #if TARGET_ABI_BITS == 32
2947 abi_ulong __unused2
;
2949 abi_ulong msg_ctime
;
2950 #if TARGET_ABI_BITS == 32
2951 abi_ulong __unused3
;
2953 abi_ulong __msg_cbytes
;
2955 abi_ulong msg_qbytes
;
2956 abi_ulong msg_lspid
;
2957 abi_ulong msg_lrpid
;
2958 abi_ulong __unused4
;
2959 abi_ulong __unused5
;
2962 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
2963 abi_ulong target_addr
)
2965 struct target_msqid_ds
*target_md
;
2967 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
2968 return -TARGET_EFAULT
;
2969 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
2970 return -TARGET_EFAULT
;
2971 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
2972 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
2973 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
2974 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
2975 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
2976 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
2977 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
2978 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
2979 unlock_user_struct(target_md
, target_addr
, 0);
2983 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
2984 struct msqid_ds
*host_md
)
2986 struct target_msqid_ds
*target_md
;
2988 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
2989 return -TARGET_EFAULT
;
2990 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
2991 return -TARGET_EFAULT
;
2992 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
2993 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
2994 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
2995 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
2996 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
2997 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
2998 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
2999 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
3000 unlock_user_struct(target_md
, target_addr
, 1);
3004 struct target_msginfo
{
3012 unsigned short int msgseg
;
3015 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
3016 struct msginfo
*host_msginfo
)
3018 struct target_msginfo
*target_msginfo
;
3019 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
3020 return -TARGET_EFAULT
;
3021 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
3022 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
3023 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
3024 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
3025 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
3026 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
3027 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
3028 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
3029 unlock_user_struct(target_msginfo
, target_addr
, 1);
3033 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
3035 struct msqid_ds dsarg
;
3036 struct msginfo msginfo
;
3037 abi_long ret
= -TARGET_EINVAL
;
3045 if (target_to_host_msqid_ds(&dsarg
,ptr
))
3046 return -TARGET_EFAULT
;
3047 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
3048 if (host_to_target_msqid_ds(ptr
,&dsarg
))
3049 return -TARGET_EFAULT
;
3052 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
3056 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
3057 if (host_to_target_msginfo(ptr
, &msginfo
))
3058 return -TARGET_EFAULT
;
3065 struct target_msgbuf
{
3070 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
3071 ssize_t msgsz
, int msgflg
)
3073 struct target_msgbuf
*target_mb
;
3074 struct msgbuf
*host_mb
;
3078 return -TARGET_EINVAL
;
3081 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
3082 return -TARGET_EFAULT
;
3083 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
3085 unlock_user_struct(target_mb
, msgp
, 0);
3086 return -TARGET_ENOMEM
;
3088 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
3089 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
3090 ret
= get_errno(msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
3092 unlock_user_struct(target_mb
, msgp
, 0);
3097 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
3098 unsigned int msgsz
, abi_long msgtyp
,
3101 struct target_msgbuf
*target_mb
;
3103 struct msgbuf
*host_mb
;
3106 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
3107 return -TARGET_EFAULT
;
3109 host_mb
= g_malloc(msgsz
+sizeof(long));
3110 ret
= get_errno(msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
3113 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
3114 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
3115 if (!target_mtext
) {
3116 ret
= -TARGET_EFAULT
;
3119 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
3120 unlock_user(target_mtext
, target_mtext_addr
, ret
);
3123 target_mb
->mtype
= tswapal(host_mb
->mtype
);
3127 unlock_user_struct(target_mb
, msgp
, 1);
3132 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
3133 abi_ulong target_addr
)
3135 struct target_shmid_ds
*target_sd
;
3137 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3138 return -TARGET_EFAULT
;
3139 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
3140 return -TARGET_EFAULT
;
3141 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
3142 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
3143 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
3144 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
3145 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
3146 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
3147 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
3148 unlock_user_struct(target_sd
, target_addr
, 0);
3152 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
3153 struct shmid_ds
*host_sd
)
3155 struct target_shmid_ds
*target_sd
;
3157 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3158 return -TARGET_EFAULT
;
3159 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
3160 return -TARGET_EFAULT
;
3161 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
3162 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
3163 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
3164 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
3165 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
3166 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
3167 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
3168 unlock_user_struct(target_sd
, target_addr
, 1);
3172 struct target_shminfo
{
3180 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
3181 struct shminfo
*host_shminfo
)
3183 struct target_shminfo
*target_shminfo
;
3184 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
3185 return -TARGET_EFAULT
;
3186 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
3187 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
3188 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
3189 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
3190 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
3191 unlock_user_struct(target_shminfo
, target_addr
, 1);
3195 struct target_shm_info
{
3200 abi_ulong swap_attempts
;
3201 abi_ulong swap_successes
;
3204 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
3205 struct shm_info
*host_shm_info
)
3207 struct target_shm_info
*target_shm_info
;
3208 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
3209 return -TARGET_EFAULT
;
3210 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
3211 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
3212 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
3213 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
3214 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
3215 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
3216 unlock_user_struct(target_shm_info
, target_addr
, 1);
3220 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
3222 struct shmid_ds dsarg
;
3223 struct shminfo shminfo
;
3224 struct shm_info shm_info
;
3225 abi_long ret
= -TARGET_EINVAL
;
3233 if (target_to_host_shmid_ds(&dsarg
, buf
))
3234 return -TARGET_EFAULT
;
3235 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
3236 if (host_to_target_shmid_ds(buf
, &dsarg
))
3237 return -TARGET_EFAULT
;
3240 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
3241 if (host_to_target_shminfo(buf
, &shminfo
))
3242 return -TARGET_EFAULT
;
3245 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
3246 if (host_to_target_shm_info(buf
, &shm_info
))
3247 return -TARGET_EFAULT
;
3252 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
3259 static inline abi_ulong
do_shmat(int shmid
, abi_ulong shmaddr
, int shmflg
)
3263 struct shmid_ds shm_info
;
3266 /* find out the length of the shared memory segment */
3267 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
3268 if (is_error(ret
)) {
3269 /* can't get length, bail out */
3276 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
3278 abi_ulong mmap_start
;
3280 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
3282 if (mmap_start
== -1) {
3284 host_raddr
= (void *)-1;
3286 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
3289 if (host_raddr
== (void *)-1) {
3291 return get_errno((long)host_raddr
);
3293 raddr
=h2g((unsigned long)host_raddr
);
3295 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
3296 PAGE_VALID
| PAGE_READ
|
3297 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
3299 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
3300 if (!shm_regions
[i
].in_use
) {
3301 shm_regions
[i
].in_use
= true;
3302 shm_regions
[i
].start
= raddr
;
3303 shm_regions
[i
].size
= shm_info
.shm_segsz
;
3313 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
3317 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
3318 if (shm_regions
[i
].in_use
&& shm_regions
[i
].start
== shmaddr
) {
3319 shm_regions
[i
].in_use
= false;
3320 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
3325 return get_errno(shmdt(g2h(shmaddr
)));
3328 #ifdef TARGET_NR_ipc
3329 /* ??? This only works with linear mappings. */
3330 /* do_ipc() must return target values and target errnos. */
3331 static abi_long
do_ipc(unsigned int call
, abi_long first
,
3332 abi_long second
, abi_long third
,
3333 abi_long ptr
, abi_long fifth
)
3338 version
= call
>> 16;
3343 ret
= do_semop(first
, ptr
, second
);
3347 ret
= get_errno(semget(first
, second
, third
));
3350 case IPCOP_semctl
: {
3351 /* The semun argument to semctl is passed by value, so dereference the
3354 get_user_ual(atptr
, ptr
);
3355 ret
= do_semctl(first
, second
, third
, atptr
);
3360 ret
= get_errno(msgget(first
, second
));
3364 ret
= do_msgsnd(first
, ptr
, second
, third
);
3368 ret
= do_msgctl(first
, second
, ptr
);
3375 struct target_ipc_kludge
{
3380 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
3381 ret
= -TARGET_EFAULT
;
3385 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
3387 unlock_user_struct(tmp
, ptr
, 0);
3391 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
3400 raddr
= do_shmat(first
, ptr
, second
);
3401 if (is_error(raddr
))
3402 return get_errno(raddr
);
3403 if (put_user_ual(raddr
, third
))
3404 return -TARGET_EFAULT
;
3408 ret
= -TARGET_EINVAL
;
3413 ret
= do_shmdt(ptr
);
3417 /* IPC_* flag values are the same on all linux platforms */
3418 ret
= get_errno(shmget(first
, second
, third
));
3421 /* IPC_* and SHM_* command values are the same on all linux platforms */
3423 ret
= do_shmctl(first
, second
, ptr
);
3426 gemu_log("Unsupported ipc call: %d (version %d)\n", call
, version
);
3427 ret
= -TARGET_ENOSYS
;
3434 /* kernel structure types definitions */
3436 #define STRUCT(name, ...) STRUCT_ ## name,
3437 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
3439 #include "syscall_types.h"
3443 #undef STRUCT_SPECIAL
3445 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
3446 #define STRUCT_SPECIAL(name)
3447 #include "syscall_types.h"
3449 #undef STRUCT_SPECIAL
3451 typedef struct IOCTLEntry IOCTLEntry
;
3453 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3454 int fd
, int cmd
, abi_long arg
);
3458 unsigned int host_cmd
;
3461 do_ioctl_fn
*do_ioctl
;
3462 const argtype arg_type
[5];
3465 #define IOC_R 0x0001
3466 #define IOC_W 0x0002
3467 #define IOC_RW (IOC_R | IOC_W)
3469 #define MAX_STRUCT_SIZE 4096
3471 #ifdef CONFIG_FIEMAP
3472 /* So fiemap access checks don't overflow on 32 bit systems.
3473 * This is very slightly smaller than the limit imposed by
3474 * the underlying kernel.
3476 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3477 / sizeof(struct fiemap_extent))
3479 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3480 int fd
, int cmd
, abi_long arg
)
3482 /* The parameter for this ioctl is a struct fiemap followed
3483 * by an array of struct fiemap_extent whose size is set
3484 * in fiemap->fm_extent_count. The array is filled in by the
3487 int target_size_in
, target_size_out
;
3489 const argtype
*arg_type
= ie
->arg_type
;
3490 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
3493 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
3497 assert(arg_type
[0] == TYPE_PTR
);
3498 assert(ie
->access
== IOC_RW
);
3500 target_size_in
= thunk_type_size(arg_type
, 0);
3501 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
3503 return -TARGET_EFAULT
;
3505 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3506 unlock_user(argptr
, arg
, 0);
3507 fm
= (struct fiemap
*)buf_temp
;
3508 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
3509 return -TARGET_EINVAL
;
3512 outbufsz
= sizeof (*fm
) +
3513 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
3515 if (outbufsz
> MAX_STRUCT_SIZE
) {
3516 /* We can't fit all the extents into the fixed size buffer.
3517 * Allocate one that is large enough and use it instead.
3519 fm
= g_try_malloc(outbufsz
);
3521 return -TARGET_ENOMEM
;
3523 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
3526 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, fm
));
3527 if (!is_error(ret
)) {
3528 target_size_out
= target_size_in
;
3529 /* An extent_count of 0 means we were only counting the extents
3530 * so there are no structs to copy
3532 if (fm
->fm_extent_count
!= 0) {
3533 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
3535 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
3537 ret
= -TARGET_EFAULT
;
3539 /* Convert the struct fiemap */
3540 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
3541 if (fm
->fm_extent_count
!= 0) {
3542 p
= argptr
+ target_size_in
;
3543 /* ...and then all the struct fiemap_extents */
3544 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
3545 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
3550 unlock_user(argptr
, arg
, target_size_out
);
3560 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3561 int fd
, int cmd
, abi_long arg
)
3563 const argtype
*arg_type
= ie
->arg_type
;
3567 struct ifconf
*host_ifconf
;
3569 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
3570 int target_ifreq_size
;
3575 abi_long target_ifc_buf
;
3579 assert(arg_type
[0] == TYPE_PTR
);
3580 assert(ie
->access
== IOC_RW
);
3583 target_size
= thunk_type_size(arg_type
, 0);
3585 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3587 return -TARGET_EFAULT
;
3588 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3589 unlock_user(argptr
, arg
, 0);
3591 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
3592 target_ifc_len
= host_ifconf
->ifc_len
;
3593 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
3595 target_ifreq_size
= thunk_type_size(ifreq_arg_type
, 0);
3596 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
3597 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
3599 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
3600 if (outbufsz
> MAX_STRUCT_SIZE
) {
3601 /* We can't fit all the extents into the fixed size buffer.
3602 * Allocate one that is large enough and use it instead.
3604 host_ifconf
= malloc(outbufsz
);
3606 return -TARGET_ENOMEM
;
3608 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
3611 host_ifc_buf
= (char*)host_ifconf
+ sizeof(*host_ifconf
);
3613 host_ifconf
->ifc_len
= host_ifc_len
;
3614 host_ifconf
->ifc_buf
= host_ifc_buf
;
3616 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, host_ifconf
));
3617 if (!is_error(ret
)) {
3618 /* convert host ifc_len to target ifc_len */
3620 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
3621 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
3622 host_ifconf
->ifc_len
= target_ifc_len
;
3624 /* restore target ifc_buf */
3626 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
3628 /* copy struct ifconf to target user */
3630 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3632 return -TARGET_EFAULT
;
3633 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
3634 unlock_user(argptr
, arg
, target_size
);
3636 /* copy ifreq[] to target user */
3638 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
3639 for (i
= 0; i
< nb_ifreq
; i
++) {
3640 thunk_convert(argptr
+ i
* target_ifreq_size
,
3641 host_ifc_buf
+ i
* sizeof(struct ifreq
),
3642 ifreq_arg_type
, THUNK_TARGET
);
3644 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
3654 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
3655 int cmd
, abi_long arg
)
3658 struct dm_ioctl
*host_dm
;
3659 abi_long guest_data
;
3660 uint32_t guest_data_size
;
3662 const argtype
*arg_type
= ie
->arg_type
;
3664 void *big_buf
= NULL
;
3668 target_size
= thunk_type_size(arg_type
, 0);
3669 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3671 ret
= -TARGET_EFAULT
;
3674 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3675 unlock_user(argptr
, arg
, 0);
3677 /* buf_temp is too small, so fetch things into a bigger buffer */
3678 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
3679 memcpy(big_buf
, buf_temp
, target_size
);
3683 guest_data
= arg
+ host_dm
->data_start
;
3684 if ((guest_data
- arg
) < 0) {
3688 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
3689 host_data
= (char*)host_dm
+ host_dm
->data_start
;
3691 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
3692 switch (ie
->host_cmd
) {
3694 case DM_LIST_DEVICES
:
3697 case DM_DEV_SUSPEND
:
3700 case DM_TABLE_STATUS
:
3701 case DM_TABLE_CLEAR
:
3703 case DM_LIST_VERSIONS
:
3707 case DM_DEV_SET_GEOMETRY
:
3708 /* data contains only strings */
3709 memcpy(host_data
, argptr
, guest_data_size
);
3712 memcpy(host_data
, argptr
, guest_data_size
);
3713 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
3717 void *gspec
= argptr
;
3718 void *cur_data
= host_data
;
3719 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
3720 int spec_size
= thunk_type_size(arg_type
, 0);
3723 for (i
= 0; i
< host_dm
->target_count
; i
++) {
3724 struct dm_target_spec
*spec
= cur_data
;
3728 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
3729 slen
= strlen((char*)gspec
+ spec_size
) + 1;
3731 spec
->next
= sizeof(*spec
) + slen
;
3732 strcpy((char*)&spec
[1], gspec
+ spec_size
);
3734 cur_data
+= spec
->next
;
3739 ret
= -TARGET_EINVAL
;
3740 unlock_user(argptr
, guest_data
, 0);
3743 unlock_user(argptr
, guest_data
, 0);
3745 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3746 if (!is_error(ret
)) {
3747 guest_data
= arg
+ host_dm
->data_start
;
3748 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
3749 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
3750 switch (ie
->host_cmd
) {
3755 case DM_DEV_SUSPEND
:
3758 case DM_TABLE_CLEAR
:
3760 case DM_DEV_SET_GEOMETRY
:
3761 /* no return data */
3763 case DM_LIST_DEVICES
:
3765 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
3766 uint32_t remaining_data
= guest_data_size
;
3767 void *cur_data
= argptr
;
3768 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
3769 int nl_size
= 12; /* can't use thunk_size due to alignment */
3772 uint32_t next
= nl
->next
;
3774 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
3776 if (remaining_data
< nl
->next
) {
3777 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3780 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
3781 strcpy(cur_data
+ nl_size
, nl
->name
);
3782 cur_data
+= nl
->next
;
3783 remaining_data
-= nl
->next
;
3787 nl
= (void*)nl
+ next
;
3792 case DM_TABLE_STATUS
:
3794 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
3795 void *cur_data
= argptr
;
3796 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
3797 int spec_size
= thunk_type_size(arg_type
, 0);
3800 for (i
= 0; i
< host_dm
->target_count
; i
++) {
3801 uint32_t next
= spec
->next
;
3802 int slen
= strlen((char*)&spec
[1]) + 1;
3803 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
3804 if (guest_data_size
< spec
->next
) {
3805 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3808 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
3809 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
3810 cur_data
= argptr
+ spec
->next
;
3811 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
3817 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
3818 int count
= *(uint32_t*)hdata
;
3819 uint64_t *hdev
= hdata
+ 8;
3820 uint64_t *gdev
= argptr
+ 8;
3823 *(uint32_t*)argptr
= tswap32(count
);
3824 for (i
= 0; i
< count
; i
++) {
3825 *gdev
= tswap64(*hdev
);
3831 case DM_LIST_VERSIONS
:
3833 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
3834 uint32_t remaining_data
= guest_data_size
;
3835 void *cur_data
= argptr
;
3836 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
3837 int vers_size
= thunk_type_size(arg_type
, 0);
3840 uint32_t next
= vers
->next
;
3842 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
3844 if (remaining_data
< vers
->next
) {
3845 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3848 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
3849 strcpy(cur_data
+ vers_size
, vers
->name
);
3850 cur_data
+= vers
->next
;
3851 remaining_data
-= vers
->next
;
3855 vers
= (void*)vers
+ next
;
3860 unlock_user(argptr
, guest_data
, 0);
3861 ret
= -TARGET_EINVAL
;
3864 unlock_user(argptr
, guest_data
, guest_data_size
);
3866 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3868 ret
= -TARGET_EFAULT
;
3871 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3872 unlock_user(argptr
, arg
, target_size
);
3879 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
3880 int cmd
, abi_long arg
)
3884 const argtype
*arg_type
= ie
->arg_type
;
3885 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
3888 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
3889 struct blkpg_partition host_part
;
3891 /* Read and convert blkpg */
3893 target_size
= thunk_type_size(arg_type
, 0);
3894 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3896 ret
= -TARGET_EFAULT
;
3899 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3900 unlock_user(argptr
, arg
, 0);
3902 switch (host_blkpg
->op
) {
3903 case BLKPG_ADD_PARTITION
:
3904 case BLKPG_DEL_PARTITION
:
3905 /* payload is struct blkpg_partition */
3908 /* Unknown opcode */
3909 ret
= -TARGET_EINVAL
;
3913 /* Read and convert blkpg->data */
3914 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
3915 target_size
= thunk_type_size(part_arg_type
, 0);
3916 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3918 ret
= -TARGET_EFAULT
;
3921 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
3922 unlock_user(argptr
, arg
, 0);
3924 /* Swizzle the data pointer to our local copy and call! */
3925 host_blkpg
->data
= &host_part
;
3926 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, host_blkpg
));
3932 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3933 int fd
, int cmd
, abi_long arg
)
3935 const argtype
*arg_type
= ie
->arg_type
;
3936 const StructEntry
*se
;
3937 const argtype
*field_types
;
3938 const int *dst_offsets
, *src_offsets
;
3941 abi_ulong
*target_rt_dev_ptr
;
3942 unsigned long *host_rt_dev_ptr
;
3946 assert(ie
->access
== IOC_W
);
3947 assert(*arg_type
== TYPE_PTR
);
3949 assert(*arg_type
== TYPE_STRUCT
);
3950 target_size
= thunk_type_size(arg_type
, 0);
3951 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3953 return -TARGET_EFAULT
;
3956 assert(*arg_type
== (int)STRUCT_rtentry
);
3957 se
= struct_entries
+ *arg_type
++;
3958 assert(se
->convert
[0] == NULL
);
3959 /* convert struct here to be able to catch rt_dev string */
3960 field_types
= se
->field_types
;
3961 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
3962 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
3963 for (i
= 0; i
< se
->nb_fields
; i
++) {
3964 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
3965 assert(*field_types
== TYPE_PTRVOID
);
3966 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
3967 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
3968 if (*target_rt_dev_ptr
!= 0) {
3969 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
3970 tswapal(*target_rt_dev_ptr
));
3971 if (!*host_rt_dev_ptr
) {
3972 unlock_user(argptr
, arg
, 0);
3973 return -TARGET_EFAULT
;
3976 *host_rt_dev_ptr
= 0;
3981 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
3982 argptr
+ src_offsets
[i
],
3983 field_types
, THUNK_HOST
);
3985 unlock_user(argptr
, arg
, 0);
3987 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3988 if (*host_rt_dev_ptr
!= 0) {
3989 unlock_user((void *)*host_rt_dev_ptr
,
3990 *target_rt_dev_ptr
, 0);
3995 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3996 int fd
, int cmd
, abi_long arg
)
3998 int sig
= target_to_host_signal(arg
);
3999 return get_errno(ioctl(fd
, ie
->host_cmd
, sig
));
4002 static IOCTLEntry ioctl_entries
[] = {
4003 #define IOCTL(cmd, access, ...) \
4004 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
4005 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
4006 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
4011 /* ??? Implement proper locking for ioctls. */
4012 /* do_ioctl() Must return target values and target errnos. */
4013 static abi_long
do_ioctl(int fd
, int cmd
, abi_long arg
)
4015 const IOCTLEntry
*ie
;
4016 const argtype
*arg_type
;
4018 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
4024 if (ie
->target_cmd
== 0) {
4025 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
4026 return -TARGET_ENOSYS
;
4028 if (ie
->target_cmd
== cmd
)
4032 arg_type
= ie
->arg_type
;
4034 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd
, ie
->name
);
4037 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
4040 switch(arg_type
[0]) {
4043 ret
= get_errno(ioctl(fd
, ie
->host_cmd
));
4047 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, arg
));
4051 target_size
= thunk_type_size(arg_type
, 0);
4052 switch(ie
->access
) {
4054 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
4055 if (!is_error(ret
)) {
4056 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4058 return -TARGET_EFAULT
;
4059 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
4060 unlock_user(argptr
, arg
, target_size
);
4064 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4066 return -TARGET_EFAULT
;
4067 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4068 unlock_user(argptr
, arg
, 0);
4069 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
4073 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4075 return -TARGET_EFAULT
;
4076 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4077 unlock_user(argptr
, arg
, 0);
4078 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
4079 if (!is_error(ret
)) {
4080 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4082 return -TARGET_EFAULT
;
4083 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
4084 unlock_user(argptr
, arg
, target_size
);
4090 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
4091 (long)cmd
, arg_type
[0]);
4092 ret
= -TARGET_ENOSYS
;
4098 static const bitmask_transtbl iflag_tbl
[] = {
4099 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
4100 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
4101 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
4102 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
4103 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
4104 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
4105 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
4106 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
4107 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
4108 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
4109 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
4110 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
4111 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
4112 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
4116 static const bitmask_transtbl oflag_tbl
[] = {
4117 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
4118 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
4119 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
4120 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
4121 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
4122 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
4123 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
4124 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
4125 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
4126 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
4127 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
4128 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
4129 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
4130 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
4131 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
4132 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
4133 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
4134 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
4135 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
4136 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
4137 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
4138 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
4139 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
4140 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
4144 static const bitmask_transtbl cflag_tbl
[] = {
4145 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
4146 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
4147 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
4148 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
4149 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
4150 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
4151 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
4152 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
4153 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
4154 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
4155 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
4156 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
4157 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
4158 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
4159 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
4160 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
4161 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
4162 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
4163 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
4164 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
4165 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
4166 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
4167 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
4168 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
4169 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
4170 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
4171 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
4172 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
4173 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
4174 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
4175 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
4179 static const bitmask_transtbl lflag_tbl
[] = {
4180 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
4181 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
4182 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
4183 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
4184 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
4185 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
4186 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
4187 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
4188 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
4189 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
4190 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
4191 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
4192 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
4193 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
4194 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
4198 static void target_to_host_termios (void *dst
, const void *src
)
4200 struct host_termios
*host
= dst
;
4201 const struct target_termios
*target
= src
;
4204 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
4206 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
4208 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
4210 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
4211 host
->c_line
= target
->c_line
;
4213 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
4214 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
4215 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
4216 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
4217 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
4218 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
4219 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
4220 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
4221 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
4222 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
4223 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
4224 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
4225 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
4226 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
4227 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
4228 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
4229 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
4230 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
4233 static void host_to_target_termios (void *dst
, const void *src
)
4235 struct target_termios
*target
= dst
;
4236 const struct host_termios
*host
= src
;
4239 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
4241 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
4243 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
4245 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
4246 target
->c_line
= host
->c_line
;
4248 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
4249 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
4250 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
4251 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
4252 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
4253 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
4254 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
4255 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
4256 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
4257 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
4258 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
4259 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
4260 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
4261 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
4262 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
4263 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
4264 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
4265 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
4268 static const StructEntry struct_termios_def
= {
4269 .convert
= { host_to_target_termios
, target_to_host_termios
},
4270 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
4271 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
4274 static bitmask_transtbl mmap_flags_tbl
[] = {
4275 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
4276 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
4277 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
4278 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
, MAP_ANONYMOUS
, MAP_ANONYMOUS
},
4279 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
, MAP_GROWSDOWN
, MAP_GROWSDOWN
},
4280 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
, MAP_DENYWRITE
, MAP_DENYWRITE
},
4281 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
, MAP_EXECUTABLE
, MAP_EXECUTABLE
},
4282 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
4283 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
, MAP_NORESERVE
,
4288 #if defined(TARGET_I386)
4290 /* NOTE: there is really one LDT for all the threads */
4291 static uint8_t *ldt_table
;
4293 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
4300 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
4301 if (size
> bytecount
)
4303 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
4305 return -TARGET_EFAULT
;
4306 /* ??? Should this by byteswapped? */
4307 memcpy(p
, ldt_table
, size
);
4308 unlock_user(p
, ptr
, size
);
4312 /* XXX: add locking support */
4313 static abi_long
write_ldt(CPUX86State
*env
,
4314 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
4316 struct target_modify_ldt_ldt_s ldt_info
;
4317 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4318 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
4319 int seg_not_present
, useable
, lm
;
4320 uint32_t *lp
, entry_1
, entry_2
;
4322 if (bytecount
!= sizeof(ldt_info
))
4323 return -TARGET_EINVAL
;
4324 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
4325 return -TARGET_EFAULT
;
4326 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
4327 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
4328 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
4329 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
4330 unlock_user_struct(target_ldt_info
, ptr
, 0);
4332 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
4333 return -TARGET_EINVAL
;
4334 seg_32bit
= ldt_info
.flags
& 1;
4335 contents
= (ldt_info
.flags
>> 1) & 3;
4336 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
4337 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
4338 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
4339 useable
= (ldt_info
.flags
>> 6) & 1;
4343 lm
= (ldt_info
.flags
>> 7) & 1;
4345 if (contents
== 3) {
4347 return -TARGET_EINVAL
;
4348 if (seg_not_present
== 0)
4349 return -TARGET_EINVAL
;
4351 /* allocate the LDT */
4353 env
->ldt
.base
= target_mmap(0,
4354 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
4355 PROT_READ
|PROT_WRITE
,
4356 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
4357 if (env
->ldt
.base
== -1)
4358 return -TARGET_ENOMEM
;
4359 memset(g2h(env
->ldt
.base
), 0,
4360 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
4361 env
->ldt
.limit
= 0xffff;
4362 ldt_table
= g2h(env
->ldt
.base
);
4365 /* NOTE: same code as Linux kernel */
4366 /* Allow LDTs to be cleared by the user. */
4367 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
4370 read_exec_only
== 1 &&
4372 limit_in_pages
== 0 &&
4373 seg_not_present
== 1 &&
4381 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
4382 (ldt_info
.limit
& 0x0ffff);
4383 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
4384 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
4385 (ldt_info
.limit
& 0xf0000) |
4386 ((read_exec_only
^ 1) << 9) |
4388 ((seg_not_present
^ 1) << 15) |
4390 (limit_in_pages
<< 23) |
4394 entry_2
|= (useable
<< 20);
4396 /* Install the new entry ... */
4398 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
4399 lp
[0] = tswap32(entry_1
);
4400 lp
[1] = tswap32(entry_2
);
4404 /* specific and weird i386 syscalls */
4405 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
4406 unsigned long bytecount
)
4412 ret
= read_ldt(ptr
, bytecount
);
4415 ret
= write_ldt(env
, ptr
, bytecount
, 1);
4418 ret
= write_ldt(env
, ptr
, bytecount
, 0);
4421 ret
= -TARGET_ENOSYS
;
4427 #if defined(TARGET_I386) && defined(TARGET_ABI32)
4428 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
4430 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
4431 struct target_modify_ldt_ldt_s ldt_info
;
4432 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4433 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
4434 int seg_not_present
, useable
, lm
;
4435 uint32_t *lp
, entry_1
, entry_2
;
4438 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
4439 if (!target_ldt_info
)
4440 return -TARGET_EFAULT
;
4441 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
4442 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
4443 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
4444 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
4445 if (ldt_info
.entry_number
== -1) {
4446 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
4447 if (gdt_table
[i
] == 0) {
4448 ldt_info
.entry_number
= i
;
4449 target_ldt_info
->entry_number
= tswap32(i
);
4454 unlock_user_struct(target_ldt_info
, ptr
, 1);
4456 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
4457 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
4458 return -TARGET_EINVAL
;
4459 seg_32bit
= ldt_info
.flags
& 1;
4460 contents
= (ldt_info
.flags
>> 1) & 3;
4461 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
4462 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
4463 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
4464 useable
= (ldt_info
.flags
>> 6) & 1;
4468 lm
= (ldt_info
.flags
>> 7) & 1;
4471 if (contents
== 3) {
4472 if (seg_not_present
== 0)
4473 return -TARGET_EINVAL
;
4476 /* NOTE: same code as Linux kernel */
4477 /* Allow LDTs to be cleared by the user. */
4478 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
4479 if ((contents
== 0 &&
4480 read_exec_only
== 1 &&
4482 limit_in_pages
== 0 &&
4483 seg_not_present
== 1 &&
4491 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
4492 (ldt_info
.limit
& 0x0ffff);
4493 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
4494 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
4495 (ldt_info
.limit
& 0xf0000) |
4496 ((read_exec_only
^ 1) << 9) |
4498 ((seg_not_present
^ 1) << 15) |
4500 (limit_in_pages
<< 23) |
4505 /* Install the new entry ... */
4507 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
4508 lp
[0] = tswap32(entry_1
);
4509 lp
[1] = tswap32(entry_2
);
4513 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
4515 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4516 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
4517 uint32_t base_addr
, limit
, flags
;
4518 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
4519 int seg_not_present
, useable
, lm
;
4520 uint32_t *lp
, entry_1
, entry_2
;
4522 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
4523 if (!target_ldt_info
)
4524 return -TARGET_EFAULT
;
4525 idx
= tswap32(target_ldt_info
->entry_number
);
4526 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
4527 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
4528 unlock_user_struct(target_ldt_info
, ptr
, 1);
4529 return -TARGET_EINVAL
;
4531 lp
= (uint32_t *)(gdt_table
+ idx
);
4532 entry_1
= tswap32(lp
[0]);
4533 entry_2
= tswap32(lp
[1]);
4535 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
4536 contents
= (entry_2
>> 10) & 3;
4537 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
4538 seg_32bit
= (entry_2
>> 22) & 1;
4539 limit_in_pages
= (entry_2
>> 23) & 1;
4540 useable
= (entry_2
>> 20) & 1;
4544 lm
= (entry_2
>> 21) & 1;
4546 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
4547 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
4548 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
4549 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
4550 base_addr
= (entry_1
>> 16) |
4551 (entry_2
& 0xff000000) |
4552 ((entry_2
& 0xff) << 16);
4553 target_ldt_info
->base_addr
= tswapal(base_addr
);
4554 target_ldt_info
->limit
= tswap32(limit
);
4555 target_ldt_info
->flags
= tswap32(flags
);
4556 unlock_user_struct(target_ldt_info
, ptr
, 1);
4559 #endif /* TARGET_I386 && TARGET_ABI32 */
4561 #ifndef TARGET_ABI32
4562 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
4569 case TARGET_ARCH_SET_GS
:
4570 case TARGET_ARCH_SET_FS
:
4571 if (code
== TARGET_ARCH_SET_GS
)
4575 cpu_x86_load_seg(env
, idx
, 0);
4576 env
->segs
[idx
].base
= addr
;
4578 case TARGET_ARCH_GET_GS
:
4579 case TARGET_ARCH_GET_FS
:
4580 if (code
== TARGET_ARCH_GET_GS
)
4584 val
= env
->segs
[idx
].base
;
4585 if (put_user(val
, addr
, abi_ulong
))
4586 ret
= -TARGET_EFAULT
;
4589 ret
= -TARGET_EINVAL
;
4596 #endif /* defined(TARGET_I386) */
4598 #define NEW_STACK_SIZE 0x40000
4601 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
4604 pthread_mutex_t mutex
;
4605 pthread_cond_t cond
;
4608 abi_ulong child_tidptr
;
4609 abi_ulong parent_tidptr
;
4613 static void *clone_func(void *arg
)
4615 new_thread_info
*info
= arg
;
4620 rcu_register_thread();
4622 cpu
= ENV_GET_CPU(env
);
4624 ts
= (TaskState
*)cpu
->opaque
;
4625 info
->tid
= gettid();
4626 cpu
->host_tid
= info
->tid
;
4628 if (info
->child_tidptr
)
4629 put_user_u32(info
->tid
, info
->child_tidptr
);
4630 if (info
->parent_tidptr
)
4631 put_user_u32(info
->tid
, info
->parent_tidptr
);
4632 /* Enable signals. */
4633 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
4634 /* Signal to the parent that we're ready. */
4635 pthread_mutex_lock(&info
->mutex
);
4636 pthread_cond_broadcast(&info
->cond
);
4637 pthread_mutex_unlock(&info
->mutex
);
4638 /* Wait until the parent has finshed initializing the tls state. */
4639 pthread_mutex_lock(&clone_lock
);
4640 pthread_mutex_unlock(&clone_lock
);
4646 /* do_fork() Must return host values and target errnos (unlike most
4647 do_*() functions). */
4648 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
4649 abi_ulong parent_tidptr
, target_ulong newtls
,
4650 abi_ulong child_tidptr
)
4652 CPUState
*cpu
= ENV_GET_CPU(env
);
4656 CPUArchState
*new_env
;
4657 unsigned int nptl_flags
;
4660 /* Emulate vfork() with fork() */
4661 if (flags
& CLONE_VFORK
)
4662 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
4664 if (flags
& CLONE_VM
) {
4665 TaskState
*parent_ts
= (TaskState
*)cpu
->opaque
;
4666 new_thread_info info
;
4667 pthread_attr_t attr
;
4669 ts
= g_new0(TaskState
, 1);
4670 init_task_state(ts
);
4671 /* we create a new CPU instance. */
4672 new_env
= cpu_copy(env
);
4673 /* Init regs that differ from the parent. */
4674 cpu_clone_regs(new_env
, newsp
);
4675 new_cpu
= ENV_GET_CPU(new_env
);
4676 new_cpu
->opaque
= ts
;
4677 ts
->bprm
= parent_ts
->bprm
;
4678 ts
->info
= parent_ts
->info
;
4680 flags
&= ~CLONE_NPTL_FLAGS2
;
4682 if (nptl_flags
& CLONE_CHILD_CLEARTID
) {
4683 ts
->child_tidptr
= child_tidptr
;
4686 if (nptl_flags
& CLONE_SETTLS
)
4687 cpu_set_tls (new_env
, newtls
);
4689 /* Grab a mutex so that thread setup appears atomic. */
4690 pthread_mutex_lock(&clone_lock
);
4692 memset(&info
, 0, sizeof(info
));
4693 pthread_mutex_init(&info
.mutex
, NULL
);
4694 pthread_mutex_lock(&info
.mutex
);
4695 pthread_cond_init(&info
.cond
, NULL
);
4697 if (nptl_flags
& CLONE_CHILD_SETTID
)
4698 info
.child_tidptr
= child_tidptr
;
4699 if (nptl_flags
& CLONE_PARENT_SETTID
)
4700 info
.parent_tidptr
= parent_tidptr
;
4702 ret
= pthread_attr_init(&attr
);
4703 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
4704 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
4705 /* It is not safe to deliver signals until the child has finished
4706 initializing, so temporarily block all signals. */
4707 sigfillset(&sigmask
);
4708 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
4710 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
4711 /* TODO: Free new CPU state if thread creation failed. */
4713 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
4714 pthread_attr_destroy(&attr
);
4716 /* Wait for the child to initialize. */
4717 pthread_cond_wait(&info
.cond
, &info
.mutex
);
4719 if (flags
& CLONE_PARENT_SETTID
)
4720 put_user_u32(ret
, parent_tidptr
);
4724 pthread_mutex_unlock(&info
.mutex
);
4725 pthread_cond_destroy(&info
.cond
);
4726 pthread_mutex_destroy(&info
.mutex
);
4727 pthread_mutex_unlock(&clone_lock
);
4729 /* if no CLONE_VM, we consider it is a fork */
4730 if ((flags
& ~(CSIGNAL
| CLONE_NPTL_FLAGS2
)) != 0) {
4731 return -TARGET_EINVAL
;
4736 /* Child Process. */
4738 cpu_clone_regs(env
, newsp
);
4740 /* There is a race condition here. The parent process could
4741 theoretically read the TID in the child process before the child
4742 tid is set. This would require using either ptrace
4743 (not implemented) or having *_tidptr to point at a shared memory
4744 mapping. We can't repeat the spinlock hack used above because
4745 the child process gets its own copy of the lock. */
4746 if (flags
& CLONE_CHILD_SETTID
)
4747 put_user_u32(gettid(), child_tidptr
);
4748 if (flags
& CLONE_PARENT_SETTID
)
4749 put_user_u32(gettid(), parent_tidptr
);
4750 ts
= (TaskState
*)cpu
->opaque
;
4751 if (flags
& CLONE_SETTLS
)
4752 cpu_set_tls (env
, newtls
);
4753 if (flags
& CLONE_CHILD_CLEARTID
)
4754 ts
->child_tidptr
= child_tidptr
;
4762 /* warning : doesn't handle linux specific flags... */
4763 static int target_to_host_fcntl_cmd(int cmd
)
4766 case TARGET_F_DUPFD
:
4767 case TARGET_F_GETFD
:
4768 case TARGET_F_SETFD
:
4769 case TARGET_F_GETFL
:
4770 case TARGET_F_SETFL
:
4772 case TARGET_F_GETLK
:
4774 case TARGET_F_SETLK
:
4776 case TARGET_F_SETLKW
:
4778 case TARGET_F_GETOWN
:
4780 case TARGET_F_SETOWN
:
4782 case TARGET_F_GETSIG
:
4784 case TARGET_F_SETSIG
:
4786 #if TARGET_ABI_BITS == 32
4787 case TARGET_F_GETLK64
:
4789 case TARGET_F_SETLK64
:
4791 case TARGET_F_SETLKW64
:
4794 case TARGET_F_SETLEASE
:
4796 case TARGET_F_GETLEASE
:
4798 #ifdef F_DUPFD_CLOEXEC
4799 case TARGET_F_DUPFD_CLOEXEC
:
4800 return F_DUPFD_CLOEXEC
;
4802 case TARGET_F_NOTIFY
:
4805 case TARGET_F_GETOWN_EX
:
4809 case TARGET_F_SETOWN_EX
:
4813 return -TARGET_EINVAL
;
4815 return -TARGET_EINVAL
;
4818 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4819 static const bitmask_transtbl flock_tbl
[] = {
4820 TRANSTBL_CONVERT(F_RDLCK
),
4821 TRANSTBL_CONVERT(F_WRLCK
),
4822 TRANSTBL_CONVERT(F_UNLCK
),
4823 TRANSTBL_CONVERT(F_EXLCK
),
4824 TRANSTBL_CONVERT(F_SHLCK
),
4828 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
4831 struct target_flock
*target_fl
;
4832 struct flock64 fl64
;
4833 struct target_flock64
*target_fl64
;
4835 struct f_owner_ex fox
;
4836 struct target_f_owner_ex
*target_fox
;
4839 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
4841 if (host_cmd
== -TARGET_EINVAL
)
4845 case TARGET_F_GETLK
:
4846 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
4847 return -TARGET_EFAULT
;
4849 target_to_host_bitmask(tswap16(target_fl
->l_type
), flock_tbl
);
4850 fl
.l_whence
= tswap16(target_fl
->l_whence
);
4851 fl
.l_start
= tswapal(target_fl
->l_start
);
4852 fl
.l_len
= tswapal(target_fl
->l_len
);
4853 fl
.l_pid
= tswap32(target_fl
->l_pid
);
4854 unlock_user_struct(target_fl
, arg
, 0);
4855 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
4857 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg
, 0))
4858 return -TARGET_EFAULT
;
4860 host_to_target_bitmask(tswap16(fl
.l_type
), flock_tbl
);
4861 target_fl
->l_whence
= tswap16(fl
.l_whence
);
4862 target_fl
->l_start
= tswapal(fl
.l_start
);
4863 target_fl
->l_len
= tswapal(fl
.l_len
);
4864 target_fl
->l_pid
= tswap32(fl
.l_pid
);
4865 unlock_user_struct(target_fl
, arg
, 1);
4869 case TARGET_F_SETLK
:
4870 case TARGET_F_SETLKW
:
4871 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
4872 return -TARGET_EFAULT
;
4874 target_to_host_bitmask(tswap16(target_fl
->l_type
), flock_tbl
);
4875 fl
.l_whence
= tswap16(target_fl
->l_whence
);
4876 fl
.l_start
= tswapal(target_fl
->l_start
);
4877 fl
.l_len
= tswapal(target_fl
->l_len
);
4878 fl
.l_pid
= tswap32(target_fl
->l_pid
);
4879 unlock_user_struct(target_fl
, arg
, 0);
4880 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
4883 case TARGET_F_GETLK64
:
4884 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
4885 return -TARGET_EFAULT
;
4887 target_to_host_bitmask(tswap16(target_fl64
->l_type
), flock_tbl
) >> 1;
4888 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
4889 fl64
.l_start
= tswap64(target_fl64
->l_start
);
4890 fl64
.l_len
= tswap64(target_fl64
->l_len
);
4891 fl64
.l_pid
= tswap32(target_fl64
->l_pid
);
4892 unlock_user_struct(target_fl64
, arg
, 0);
4893 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
4895 if (!lock_user_struct(VERIFY_WRITE
, target_fl64
, arg
, 0))
4896 return -TARGET_EFAULT
;
4897 target_fl64
->l_type
=
4898 host_to_target_bitmask(tswap16(fl64
.l_type
), flock_tbl
) >> 1;
4899 target_fl64
->l_whence
= tswap16(fl64
.l_whence
);
4900 target_fl64
->l_start
= tswap64(fl64
.l_start
);
4901 target_fl64
->l_len
= tswap64(fl64
.l_len
);
4902 target_fl64
->l_pid
= tswap32(fl64
.l_pid
);
4903 unlock_user_struct(target_fl64
, arg
, 1);
4906 case TARGET_F_SETLK64
:
4907 case TARGET_F_SETLKW64
:
4908 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
4909 return -TARGET_EFAULT
;
4911 target_to_host_bitmask(tswap16(target_fl64
->l_type
), flock_tbl
) >> 1;
4912 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
4913 fl64
.l_start
= tswap64(target_fl64
->l_start
);
4914 fl64
.l_len
= tswap64(target_fl64
->l_len
);
4915 fl64
.l_pid
= tswap32(target_fl64
->l_pid
);
4916 unlock_user_struct(target_fl64
, arg
, 0);
4917 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
4920 case TARGET_F_GETFL
:
4921 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
4923 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
4927 case TARGET_F_SETFL
:
4928 ret
= get_errno(fcntl(fd
, host_cmd
, target_to_host_bitmask(arg
, fcntl_flags_tbl
)));
4932 case TARGET_F_GETOWN_EX
:
4933 ret
= get_errno(fcntl(fd
, host_cmd
, &fox
));
4935 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
4936 return -TARGET_EFAULT
;
4937 target_fox
->type
= tswap32(fox
.type
);
4938 target_fox
->pid
= tswap32(fox
.pid
);
4939 unlock_user_struct(target_fox
, arg
, 1);
4945 case TARGET_F_SETOWN_EX
:
4946 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
4947 return -TARGET_EFAULT
;
4948 fox
.type
= tswap32(target_fox
->type
);
4949 fox
.pid
= tswap32(target_fox
->pid
);
4950 unlock_user_struct(target_fox
, arg
, 0);
4951 ret
= get_errno(fcntl(fd
, host_cmd
, &fox
));
4955 case TARGET_F_SETOWN
:
4956 case TARGET_F_GETOWN
:
4957 case TARGET_F_SETSIG
:
4958 case TARGET_F_GETSIG
:
4959 case TARGET_F_SETLEASE
:
4960 case TARGET_F_GETLEASE
:
4961 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
4965 ret
= get_errno(fcntl(fd
, cmd
, arg
));
4973 static inline int high2lowuid(int uid
)
4981 static inline int high2lowgid(int gid
)
4989 static inline int low2highuid(int uid
)
4991 if ((int16_t)uid
== -1)
4997 static inline int low2highgid(int gid
)
4999 if ((int16_t)gid
== -1)
5004 static inline int tswapid(int id
)
5009 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
5011 #else /* !USE_UID16 */
5012 static inline int high2lowuid(int uid
)
5016 static inline int high2lowgid(int gid
)
5020 static inline int low2highuid(int uid
)
5024 static inline int low2highgid(int gid
)
5028 static inline int tswapid(int id
)
5033 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
5035 #endif /* USE_UID16 */
5037 void syscall_init(void)
5040 const argtype
*arg_type
;
5044 thunk_init(STRUCT_MAX
);
5046 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
5047 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
5048 #include "syscall_types.h"
5050 #undef STRUCT_SPECIAL
5052 /* Build target_to_host_errno_table[] table from
5053 * host_to_target_errno_table[]. */
5054 for (i
= 0; i
< ERRNO_TABLE_SIZE
; i
++) {
5055 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
5058 /* we patch the ioctl size if necessary. We rely on the fact that
5059 no ioctl has all the bits at '1' in the size field */
5061 while (ie
->target_cmd
!= 0) {
5062 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
5063 TARGET_IOC_SIZEMASK
) {
5064 arg_type
= ie
->arg_type
;
5065 if (arg_type
[0] != TYPE_PTR
) {
5066 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
5071 size
= thunk_type_size(arg_type
, 0);
5072 ie
->target_cmd
= (ie
->target_cmd
&
5073 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
5074 (size
<< TARGET_IOC_SIZESHIFT
);
5077 /* automatic consistency check if same arch */
5078 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
5079 (defined(__x86_64__) && defined(TARGET_X86_64))
5080 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
5081 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
5082 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
5089 #if TARGET_ABI_BITS == 32
5090 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
5092 #ifdef TARGET_WORDS_BIGENDIAN
5093 return ((uint64_t)word0
<< 32) | word1
;
5095 return ((uint64_t)word1
<< 32) | word0
;
5098 #else /* TARGET_ABI_BITS == 32 */
5099 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
5103 #endif /* TARGET_ABI_BITS != 32 */
5105 #ifdef TARGET_NR_truncate64
5106 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
5111 if (regpairs_aligned(cpu_env
)) {
5115 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
5119 #ifdef TARGET_NR_ftruncate64
5120 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
5125 if (regpairs_aligned(cpu_env
)) {
5129 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
5133 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
5134 abi_ulong target_addr
)
5136 struct target_timespec
*target_ts
;
5138 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1))
5139 return -TARGET_EFAULT
;
5140 host_ts
->tv_sec
= tswapal(target_ts
->tv_sec
);
5141 host_ts
->tv_nsec
= tswapal(target_ts
->tv_nsec
);
5142 unlock_user_struct(target_ts
, target_addr
, 0);
5146 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
5147 struct timespec
*host_ts
)
5149 struct target_timespec
*target_ts
;
5151 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0))
5152 return -TARGET_EFAULT
;
5153 target_ts
->tv_sec
= tswapal(host_ts
->tv_sec
);
5154 target_ts
->tv_nsec
= tswapal(host_ts
->tv_nsec
);
5155 unlock_user_struct(target_ts
, target_addr
, 1);
5159 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_itspec
,
5160 abi_ulong target_addr
)
5162 struct target_itimerspec
*target_itspec
;
5164 if (!lock_user_struct(VERIFY_READ
, target_itspec
, target_addr
, 1)) {
5165 return -TARGET_EFAULT
;
5168 host_itspec
->it_interval
.tv_sec
=
5169 tswapal(target_itspec
->it_interval
.tv_sec
);
5170 host_itspec
->it_interval
.tv_nsec
=
5171 tswapal(target_itspec
->it_interval
.tv_nsec
);
5172 host_itspec
->it_value
.tv_sec
= tswapal(target_itspec
->it_value
.tv_sec
);
5173 host_itspec
->it_value
.tv_nsec
= tswapal(target_itspec
->it_value
.tv_nsec
);
5175 unlock_user_struct(target_itspec
, target_addr
, 1);
5179 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
5180 struct itimerspec
*host_its
)
5182 struct target_itimerspec
*target_itspec
;
5184 if (!lock_user_struct(VERIFY_WRITE
, target_itspec
, target_addr
, 0)) {
5185 return -TARGET_EFAULT
;
5188 target_itspec
->it_interval
.tv_sec
= tswapal(host_its
->it_interval
.tv_sec
);
5189 target_itspec
->it_interval
.tv_nsec
= tswapal(host_its
->it_interval
.tv_nsec
);
5191 target_itspec
->it_value
.tv_sec
= tswapal(host_its
->it_value
.tv_sec
);
5192 target_itspec
->it_value
.tv_nsec
= tswapal(host_its
->it_value
.tv_nsec
);
5194 unlock_user_struct(target_itspec
, target_addr
, 0);
5198 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
5199 abi_ulong target_addr
)
5201 struct target_sigevent
*target_sevp
;
5203 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
5204 return -TARGET_EFAULT
;
5207 /* This union is awkward on 64 bit systems because it has a 32 bit
5208 * integer and a pointer in it; we follow the conversion approach
5209 * used for handling sigval types in signal.c so the guest should get
5210 * the correct value back even if we did a 64 bit byteswap and it's
5211 * using the 32 bit integer.
5213 host_sevp
->sigev_value
.sival_ptr
=
5214 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
5215 host_sevp
->sigev_signo
=
5216 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
5217 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
5218 host_sevp
->_sigev_un
._tid
= tswap32(target_sevp
->_sigev_un
._tid
);
5220 unlock_user_struct(target_sevp
, target_addr
, 1);
5224 #if defined(TARGET_NR_mlockall)
5225 static inline int target_to_host_mlockall_arg(int arg
)
5229 if (arg
& TARGET_MLOCKALL_MCL_CURRENT
) {
5230 result
|= MCL_CURRENT
;
5232 if (arg
& TARGET_MLOCKALL_MCL_FUTURE
) {
5233 result
|= MCL_FUTURE
;
5239 static inline abi_long
host_to_target_stat64(void *cpu_env
,
5240 abi_ulong target_addr
,
5241 struct stat
*host_st
)
5243 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
5244 if (((CPUARMState
*)cpu_env
)->eabi
) {
5245 struct target_eabi_stat64
*target_st
;
5247 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
5248 return -TARGET_EFAULT
;
5249 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
5250 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
5251 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
5252 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5253 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
5255 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
5256 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
5257 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
5258 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
5259 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
5260 __put_user(host_st
->st_size
, &target_st
->st_size
);
5261 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
5262 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
5263 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
5264 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
5265 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
5266 unlock_user_struct(target_st
, target_addr
, 1);
5270 #if defined(TARGET_HAS_STRUCT_STAT64)
5271 struct target_stat64
*target_st
;
5273 struct target_stat
*target_st
;
5276 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
5277 return -TARGET_EFAULT
;
5278 memset(target_st
, 0, sizeof(*target_st
));
5279 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
5280 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
5281 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5282 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
5284 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
5285 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
5286 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
5287 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
5288 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
5289 /* XXX: better use of kernel struct */
5290 __put_user(host_st
->st_size
, &target_st
->st_size
);
5291 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
5292 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
5293 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
5294 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
5295 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
5296 unlock_user_struct(target_st
, target_addr
, 1);
5302 /* ??? Using host futex calls even when target atomic operations
5303 are not really atomic probably breaks things. However implementing
5304 futexes locally would make futexes shared between multiple processes
5305 tricky. However they're probably useless because guest atomic
5306 operations won't work either. */
5307 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
5308 target_ulong uaddr2
, int val3
)
5310 struct timespec ts
, *pts
;
5313 /* ??? We assume FUTEX_* constants are the same on both host
5315 #ifdef FUTEX_CMD_MASK
5316 base_op
= op
& FUTEX_CMD_MASK
;
5322 case FUTEX_WAIT_BITSET
:
5325 target_to_host_timespec(pts
, timeout
);
5329 return get_errno(sys_futex(g2h(uaddr
), op
, tswap32(val
),
5332 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
5334 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
5336 case FUTEX_CMP_REQUEUE
:
5338 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
5339 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
5340 But the prototype takes a `struct timespec *'; insert casts
5341 to satisfy the compiler. We do not need to tswap TIMEOUT
5342 since it's not compared to guest memory. */
5343 pts
= (struct timespec
*)(uintptr_t) timeout
;
5344 return get_errno(sys_futex(g2h(uaddr
), op
, val
, pts
,
5346 (base_op
== FUTEX_CMP_REQUEUE
5350 return -TARGET_ENOSYS
;
5353 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5354 static abi_long
do_name_to_handle_at(abi_long dirfd
, abi_long pathname
,
5355 abi_long handle
, abi_long mount_id
,
5358 struct file_handle
*target_fh
;
5359 struct file_handle
*fh
;
5363 unsigned int size
, total_size
;
5365 if (get_user_s32(size
, handle
)) {
5366 return -TARGET_EFAULT
;
5369 name
= lock_user_string(pathname
);
5371 return -TARGET_EFAULT
;
5374 total_size
= sizeof(struct file_handle
) + size
;
5375 target_fh
= lock_user(VERIFY_WRITE
, handle
, total_size
, 0);
5377 unlock_user(name
, pathname
, 0);
5378 return -TARGET_EFAULT
;
5381 fh
= g_malloc0(total_size
);
5382 fh
->handle_bytes
= size
;
5384 ret
= get_errno(name_to_handle_at(dirfd
, path(name
), fh
, &mid
, flags
));
5385 unlock_user(name
, pathname
, 0);
5387 /* man name_to_handle_at(2):
5388 * Other than the use of the handle_bytes field, the caller should treat
5389 * the file_handle structure as an opaque data type
5392 memcpy(target_fh
, fh
, total_size
);
5393 target_fh
->handle_bytes
= tswap32(fh
->handle_bytes
);
5394 target_fh
->handle_type
= tswap32(fh
->handle_type
);
5396 unlock_user(target_fh
, handle
, total_size
);
5398 if (put_user_s32(mid
, mount_id
)) {
5399 return -TARGET_EFAULT
;
5407 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5408 static abi_long
do_open_by_handle_at(abi_long mount_fd
, abi_long handle
,
5411 struct file_handle
*target_fh
;
5412 struct file_handle
*fh
;
5413 unsigned int size
, total_size
;
5416 if (get_user_s32(size
, handle
)) {
5417 return -TARGET_EFAULT
;
5420 total_size
= sizeof(struct file_handle
) + size
;
5421 target_fh
= lock_user(VERIFY_READ
, handle
, total_size
, 1);
5423 return -TARGET_EFAULT
;
5426 fh
= g_memdup(target_fh
, total_size
);
5427 fh
->handle_bytes
= size
;
5428 fh
->handle_type
= tswap32(target_fh
->handle_type
);
5430 ret
= get_errno(open_by_handle_at(mount_fd
, fh
,
5431 target_to_host_bitmask(flags
, fcntl_flags_tbl
)));
5435 unlock_user(target_fh
, handle
, total_size
);
5441 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
5443 /* signalfd siginfo conversion */
5446 host_to_target_signalfd_siginfo(struct signalfd_siginfo
*tinfo
,
5447 const struct signalfd_siginfo
*info
)
5449 int sig
= host_to_target_signal(info
->ssi_signo
);
5451 /* linux/signalfd.h defines a ssi_addr_lsb
5452 * not defined in sys/signalfd.h but used by some kernels
5455 #ifdef BUS_MCEERR_AO
5456 if (tinfo
->ssi_signo
== SIGBUS
&&
5457 (tinfo
->ssi_code
== BUS_MCEERR_AR
||
5458 tinfo
->ssi_code
== BUS_MCEERR_AO
)) {
5459 uint16_t *ssi_addr_lsb
= (uint16_t *)(&info
->ssi_addr
+ 1);
5460 uint16_t *tssi_addr_lsb
= (uint16_t *)(&tinfo
->ssi_addr
+ 1);
5461 *tssi_addr_lsb
= tswap16(*ssi_addr_lsb
);
5465 tinfo
->ssi_signo
= tswap32(sig
);
5466 tinfo
->ssi_errno
= tswap32(tinfo
->ssi_errno
);
5467 tinfo
->ssi_code
= tswap32(info
->ssi_code
);
5468 tinfo
->ssi_pid
= tswap32(info
->ssi_pid
);
5469 tinfo
->ssi_uid
= tswap32(info
->ssi_uid
);
5470 tinfo
->ssi_fd
= tswap32(info
->ssi_fd
);
5471 tinfo
->ssi_tid
= tswap32(info
->ssi_tid
);
5472 tinfo
->ssi_band
= tswap32(info
->ssi_band
);
5473 tinfo
->ssi_overrun
= tswap32(info
->ssi_overrun
);
5474 tinfo
->ssi_trapno
= tswap32(info
->ssi_trapno
);
5475 tinfo
->ssi_status
= tswap32(info
->ssi_status
);
5476 tinfo
->ssi_int
= tswap32(info
->ssi_int
);
5477 tinfo
->ssi_ptr
= tswap64(info
->ssi_ptr
);
5478 tinfo
->ssi_utime
= tswap64(info
->ssi_utime
);
5479 tinfo
->ssi_stime
= tswap64(info
->ssi_stime
);
5480 tinfo
->ssi_addr
= tswap64(info
->ssi_addr
);
5483 static abi_long
host_to_target_data_signalfd(void *buf
, size_t len
)
5487 for (i
= 0; i
< len
; i
+= sizeof(struct signalfd_siginfo
)) {
5488 host_to_target_signalfd_siginfo(buf
+ i
, buf
+ i
);
5494 static TargetFdTrans target_signalfd_trans
= {
5495 .host_to_target_data
= host_to_target_data_signalfd
,
5498 static abi_long
do_signalfd4(int fd
, abi_long mask
, int flags
)
5501 target_sigset_t
*target_mask
;
5505 if (flags
& ~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
)) {
5506 return -TARGET_EINVAL
;
5508 if (!lock_user_struct(VERIFY_READ
, target_mask
, mask
, 1)) {
5509 return -TARGET_EFAULT
;
5512 target_to_host_sigset(&host_mask
, target_mask
);
5514 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
5516 ret
= get_errno(signalfd(fd
, &host_mask
, host_flags
));
5518 fd_trans_register(ret
, &target_signalfd_trans
);
5521 unlock_user_struct(target_mask
, mask
, 0);
5527 /* Map host to target signal numbers for the wait family of syscalls.
5528 Assume all other status bits are the same. */
5529 int host_to_target_waitstatus(int status
)
5531 if (WIFSIGNALED(status
)) {
5532 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
5534 if (WIFSTOPPED(status
)) {
5535 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
5541 static int open_self_cmdline(void *cpu_env
, int fd
)
5544 bool word_skipped
= false;
5546 fd_orig
= open("/proc/self/cmdline", O_RDONLY
);
5556 nb_read
= read(fd_orig
, buf
, sizeof(buf
));
5558 fd_orig
= close(fd_orig
);
5560 } else if (nb_read
== 0) {
5564 if (!word_skipped
) {
5565 /* Skip the first string, which is the path to qemu-*-static
5566 instead of the actual command. */
5567 cp_buf
= memchr(buf
, 0, sizeof(buf
));
5569 /* Null byte found, skip one string */
5571 nb_read
-= cp_buf
- buf
;
5572 word_skipped
= true;
5577 if (write(fd
, cp_buf
, nb_read
) != nb_read
) {
5584 return close(fd_orig
);
5587 static int open_self_maps(void *cpu_env
, int fd
)
5589 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5590 TaskState
*ts
= cpu
->opaque
;
5596 fp
= fopen("/proc/self/maps", "r");
5601 while ((read
= getline(&line
, &len
, fp
)) != -1) {
5602 int fields
, dev_maj
, dev_min
, inode
;
5603 uint64_t min
, max
, offset
;
5604 char flag_r
, flag_w
, flag_x
, flag_p
;
5605 char path
[512] = "";
5606 fields
= sscanf(line
, "%"PRIx64
"-%"PRIx64
" %c%c%c%c %"PRIx64
" %x:%x %d"
5607 " %512s", &min
, &max
, &flag_r
, &flag_w
, &flag_x
,
5608 &flag_p
, &offset
, &dev_maj
, &dev_min
, &inode
, path
);
5610 if ((fields
< 10) || (fields
> 11)) {
5613 if (h2g_valid(min
)) {
5614 int flags
= page_get_flags(h2g(min
));
5615 max
= h2g_valid(max
- 1) ? max
: (uintptr_t)g2h(GUEST_ADDR_MAX
);
5616 if (page_check_range(h2g(min
), max
- min
, flags
) == -1) {
5619 if (h2g(min
) == ts
->info
->stack_limit
) {
5620 pstrcpy(path
, sizeof(path
), " [stack]");
5622 dprintf(fd
, TARGET_ABI_FMT_lx
"-" TARGET_ABI_FMT_lx
5623 " %c%c%c%c %08" PRIx64
" %02x:%02x %d %s%s\n",
5624 h2g(min
), h2g(max
- 1) + 1, flag_r
, flag_w
,
5625 flag_x
, flag_p
, offset
, dev_maj
, dev_min
, inode
,
5626 path
[0] ? " " : "", path
);
5636 static int open_self_stat(void *cpu_env
, int fd
)
5638 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5639 TaskState
*ts
= cpu
->opaque
;
5640 abi_ulong start_stack
= ts
->info
->start_stack
;
5643 for (i
= 0; i
< 44; i
++) {
5651 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
5652 } else if (i
== 1) {
5654 snprintf(buf
, sizeof(buf
), "(%s) ", ts
->bprm
->argv
[0]);
5655 } else if (i
== 27) {
5658 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
5660 /* for the rest, there is MasterCard */
5661 snprintf(buf
, sizeof(buf
), "0%c", i
== 43 ? '\n' : ' ');
5665 if (write(fd
, buf
, len
) != len
) {
5673 static int open_self_auxv(void *cpu_env
, int fd
)
5675 CPUState
*cpu
= ENV_GET_CPU((CPUArchState
*)cpu_env
);
5676 TaskState
*ts
= cpu
->opaque
;
5677 abi_ulong auxv
= ts
->info
->saved_auxv
;
5678 abi_ulong len
= ts
->info
->auxv_len
;
5682 * Auxiliary vector is stored in target process stack.
5683 * read in whole auxv vector and copy it to file
5685 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
5689 r
= write(fd
, ptr
, len
);
5696 lseek(fd
, 0, SEEK_SET
);
5697 unlock_user(ptr
, auxv
, len
);
5703 static int is_proc_myself(const char *filename
, const char *entry
)
5705 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
5706 filename
+= strlen("/proc/");
5707 if (!strncmp(filename
, "self/", strlen("self/"))) {
5708 filename
+= strlen("self/");
5709 } else if (*filename
>= '1' && *filename
<= '9') {
5711 snprintf(myself
, sizeof(myself
), "%d/", getpid());
5712 if (!strncmp(filename
, myself
, strlen(myself
))) {
5713 filename
+= strlen(myself
);
5720 if (!strcmp(filename
, entry
)) {
5727 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5728 static int is_proc(const char *filename
, const char *entry
)
5730 return strcmp(filename
, entry
) == 0;
5733 static int open_net_route(void *cpu_env
, int fd
)
5740 fp
= fopen("/proc/net/route", "r");
5747 read
= getline(&line
, &len
, fp
);
5748 dprintf(fd
, "%s", line
);
5752 while ((read
= getline(&line
, &len
, fp
)) != -1) {
5754 uint32_t dest
, gw
, mask
;
5755 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
5756 sscanf(line
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5757 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
5758 &mask
, &mtu
, &window
, &irtt
);
5759 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5760 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
5761 metric
, tswap32(mask
), mtu
, window
, irtt
);
5771 static int do_openat(void *cpu_env
, int dirfd
, const char *pathname
, int flags
, mode_t mode
)
5774 const char *filename
;
5775 int (*fill
)(void *cpu_env
, int fd
);
5776 int (*cmp
)(const char *s1
, const char *s2
);
5778 const struct fake_open
*fake_open
;
5779 static const struct fake_open fakes
[] = {
5780 { "maps", open_self_maps
, is_proc_myself
},
5781 { "stat", open_self_stat
, is_proc_myself
},
5782 { "auxv", open_self_auxv
, is_proc_myself
},
5783 { "cmdline", open_self_cmdline
, is_proc_myself
},
5784 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5785 { "/proc/net/route", open_net_route
, is_proc
},
5787 { NULL
, NULL
, NULL
}
5790 if (is_proc_myself(pathname
, "exe")) {
5791 int execfd
= qemu_getauxval(AT_EXECFD
);
5792 return execfd
? execfd
: get_errno(sys_openat(dirfd
, exec_path
, flags
, mode
));
5795 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
5796 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
5801 if (fake_open
->filename
) {
5803 char filename
[PATH_MAX
];
5806 /* create temporary file to map stat to */
5807 tmpdir
= getenv("TMPDIR");
5810 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
5811 fd
= mkstemp(filename
);
5817 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
5821 lseek(fd
, 0, SEEK_SET
);
5826 return get_errno(sys_openat(dirfd
, path(pathname
), flags
, mode
));
5829 #define TIMER_MAGIC 0x0caf0000
5830 #define TIMER_MAGIC_MASK 0xffff0000
5832 /* Convert QEMU provided timer ID back to internal 16bit index format */
5833 static target_timer_t
get_timer_id(abi_long arg
)
5835 target_timer_t timerid
= arg
;
5837 if ((timerid
& TIMER_MAGIC_MASK
) != TIMER_MAGIC
) {
5838 return -TARGET_EINVAL
;
5843 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
5844 return -TARGET_EINVAL
;
5850 /* do_syscall() should always have a single exit point at the end so
5851 that actions, such as logging of syscall results, can be performed.
5852 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
5853 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
5854 abi_long arg2
, abi_long arg3
, abi_long arg4
,
5855 abi_long arg5
, abi_long arg6
, abi_long arg7
,
5858 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
5865 gemu_log("syscall %d", num
);
5868 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
5871 case TARGET_NR_exit
:
5872 /* In old applications this may be used to implement _exit(2).
5873 However in threaded applictions it is used for thread termination,
5874 and _exit_group is used for application termination.
5875 Do thread termination if we have more then one thread. */
5876 /* FIXME: This probably breaks if a signal arrives. We should probably
5877 be disabling signals. */
5878 if (CPU_NEXT(first_cpu
)) {
5882 /* Remove the CPU from the list. */
5883 QTAILQ_REMOVE(&cpus
, cpu
, node
);
5886 if (ts
->child_tidptr
) {
5887 put_user_u32(0, ts
->child_tidptr
);
5888 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
5892 object_unref(OBJECT(cpu
));
5894 rcu_unregister_thread();
5900 gdb_exit(cpu_env
, arg1
);
5902 ret
= 0; /* avoid warning */
5904 case TARGET_NR_read
:
5908 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
5910 ret
= get_errno(read(arg1
, p
, arg3
));
5912 fd_trans_host_to_target_data(arg1
)) {
5913 ret
= fd_trans_host_to_target_data(arg1
)(p
, ret
);
5915 unlock_user(p
, arg2
, ret
);
5918 case TARGET_NR_write
:
5919 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
5921 ret
= get_errno(write(arg1
, p
, arg3
));
5922 unlock_user(p
, arg2
, 0);
5924 #ifdef TARGET_NR_open
5925 case TARGET_NR_open
:
5926 if (!(p
= lock_user_string(arg1
)))
5928 ret
= get_errno(do_openat(cpu_env
, AT_FDCWD
, p
,
5929 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
5931 fd_trans_unregister(ret
);
5932 unlock_user(p
, arg1
, 0);
5935 case TARGET_NR_openat
:
5936 if (!(p
= lock_user_string(arg2
)))
5938 ret
= get_errno(do_openat(cpu_env
, arg1
, p
,
5939 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
5941 fd_trans_unregister(ret
);
5942 unlock_user(p
, arg2
, 0);
5944 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5945 case TARGET_NR_name_to_handle_at
:
5946 ret
= do_name_to_handle_at(arg1
, arg2
, arg3
, arg4
, arg5
);
5949 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
5950 case TARGET_NR_open_by_handle_at
:
5951 ret
= do_open_by_handle_at(arg1
, arg2
, arg3
);
5952 fd_trans_unregister(ret
);
5955 case TARGET_NR_close
:
5956 fd_trans_unregister(arg1
);
5957 ret
= get_errno(close(arg1
));
5962 #ifdef TARGET_NR_fork
5963 case TARGET_NR_fork
:
5964 ret
= get_errno(do_fork(cpu_env
, SIGCHLD
, 0, 0, 0, 0));
5967 #ifdef TARGET_NR_waitpid
5968 case TARGET_NR_waitpid
:
5971 ret
= get_errno(waitpid(arg1
, &status
, arg3
));
5972 if (!is_error(ret
) && arg2
&& ret
5973 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
5978 #ifdef TARGET_NR_waitid
5979 case TARGET_NR_waitid
:
5983 ret
= get_errno(waitid(arg1
, arg2
, &info
, arg4
));
5984 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
5985 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
5987 host_to_target_siginfo(p
, &info
);
5988 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
5993 #ifdef TARGET_NR_creat /* not on alpha */
5994 case TARGET_NR_creat
:
5995 if (!(p
= lock_user_string(arg1
)))
5997 ret
= get_errno(creat(p
, arg2
));
5998 fd_trans_unregister(ret
);
5999 unlock_user(p
, arg1
, 0);
6002 #ifdef TARGET_NR_link
6003 case TARGET_NR_link
:
6006 p
= lock_user_string(arg1
);
6007 p2
= lock_user_string(arg2
);
6009 ret
= -TARGET_EFAULT
;
6011 ret
= get_errno(link(p
, p2
));
6012 unlock_user(p2
, arg2
, 0);
6013 unlock_user(p
, arg1
, 0);
6017 #if defined(TARGET_NR_linkat)
6018 case TARGET_NR_linkat
:
6023 p
= lock_user_string(arg2
);
6024 p2
= lock_user_string(arg4
);
6026 ret
= -TARGET_EFAULT
;
6028 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
6029 unlock_user(p
, arg2
, 0);
6030 unlock_user(p2
, arg4
, 0);
6034 #ifdef TARGET_NR_unlink
6035 case TARGET_NR_unlink
:
6036 if (!(p
= lock_user_string(arg1
)))
6038 ret
= get_errno(unlink(p
));
6039 unlock_user(p
, arg1
, 0);
6042 #if defined(TARGET_NR_unlinkat)
6043 case TARGET_NR_unlinkat
:
6044 if (!(p
= lock_user_string(arg2
)))
6046 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
6047 unlock_user(p
, arg2
, 0);
6050 case TARGET_NR_execve
:
6052 char **argp
, **envp
;
6055 abi_ulong guest_argp
;
6056 abi_ulong guest_envp
;
6063 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
6064 if (get_user_ual(addr
, gp
))
6072 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
6073 if (get_user_ual(addr
, gp
))
6080 argp
= alloca((argc
+ 1) * sizeof(void *));
6081 envp
= alloca((envc
+ 1) * sizeof(void *));
6083 for (gp
= guest_argp
, q
= argp
; gp
;
6084 gp
+= sizeof(abi_ulong
), q
++) {
6085 if (get_user_ual(addr
, gp
))
6089 if (!(*q
= lock_user_string(addr
)))
6091 total_size
+= strlen(*q
) + 1;
6095 for (gp
= guest_envp
, q
= envp
; gp
;
6096 gp
+= sizeof(abi_ulong
), q
++) {
6097 if (get_user_ual(addr
, gp
))
6101 if (!(*q
= lock_user_string(addr
)))
6103 total_size
+= strlen(*q
) + 1;
6107 if (!(p
= lock_user_string(arg1
)))
6109 ret
= get_errno(execve(p
, argp
, envp
));
6110 unlock_user(p
, arg1
, 0);
6115 ret
= -TARGET_EFAULT
;
6118 for (gp
= guest_argp
, q
= argp
; *q
;
6119 gp
+= sizeof(abi_ulong
), q
++) {
6120 if (get_user_ual(addr
, gp
)
6123 unlock_user(*q
, addr
, 0);
6125 for (gp
= guest_envp
, q
= envp
; *q
;
6126 gp
+= sizeof(abi_ulong
), q
++) {
6127 if (get_user_ual(addr
, gp
)
6130 unlock_user(*q
, addr
, 0);
6134 case TARGET_NR_chdir
:
6135 if (!(p
= lock_user_string(arg1
)))
6137 ret
= get_errno(chdir(p
));
6138 unlock_user(p
, arg1
, 0);
6140 #ifdef TARGET_NR_time
6141 case TARGET_NR_time
:
6144 ret
= get_errno(time(&host_time
));
6147 && put_user_sal(host_time
, arg1
))
6152 #ifdef TARGET_NR_mknod
6153 case TARGET_NR_mknod
:
6154 if (!(p
= lock_user_string(arg1
)))
6156 ret
= get_errno(mknod(p
, arg2
, arg3
));
6157 unlock_user(p
, arg1
, 0);
6160 #if defined(TARGET_NR_mknodat)
6161 case TARGET_NR_mknodat
:
6162 if (!(p
= lock_user_string(arg2
)))
6164 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
6165 unlock_user(p
, arg2
, 0);
6168 #ifdef TARGET_NR_chmod
6169 case TARGET_NR_chmod
:
6170 if (!(p
= lock_user_string(arg1
)))
6172 ret
= get_errno(chmod(p
, arg2
));
6173 unlock_user(p
, arg1
, 0);
6176 #ifdef TARGET_NR_break
6177 case TARGET_NR_break
:
6180 #ifdef TARGET_NR_oldstat
6181 case TARGET_NR_oldstat
:
6184 case TARGET_NR_lseek
:
6185 ret
= get_errno(lseek(arg1
, arg2
, arg3
));
6187 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
6188 /* Alpha specific */
6189 case TARGET_NR_getxpid
:
6190 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = getppid();
6191 ret
= get_errno(getpid());
6194 #ifdef TARGET_NR_getpid
6195 case TARGET_NR_getpid
:
6196 ret
= get_errno(getpid());
6199 case TARGET_NR_mount
:
6201 /* need to look at the data field */
6205 p
= lock_user_string(arg1
);
6213 p2
= lock_user_string(arg2
);
6216 unlock_user(p
, arg1
, 0);
6222 p3
= lock_user_string(arg3
);
6225 unlock_user(p
, arg1
, 0);
6227 unlock_user(p2
, arg2
, 0);
6234 /* FIXME - arg5 should be locked, but it isn't clear how to
6235 * do that since it's not guaranteed to be a NULL-terminated
6239 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
6241 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
));
6243 ret
= get_errno(ret
);
6246 unlock_user(p
, arg1
, 0);
6248 unlock_user(p2
, arg2
, 0);
6250 unlock_user(p3
, arg3
, 0);
6254 #ifdef TARGET_NR_umount
6255 case TARGET_NR_umount
:
6256 if (!(p
= lock_user_string(arg1
)))
6258 ret
= get_errno(umount(p
));
6259 unlock_user(p
, arg1
, 0);
6262 #ifdef TARGET_NR_stime /* not on alpha */
6263 case TARGET_NR_stime
:
6266 if (get_user_sal(host_time
, arg1
))
6268 ret
= get_errno(stime(&host_time
));
6272 case TARGET_NR_ptrace
:
6274 #ifdef TARGET_NR_alarm /* not on alpha */
6275 case TARGET_NR_alarm
:
6279 #ifdef TARGET_NR_oldfstat
6280 case TARGET_NR_oldfstat
:
6283 #ifdef TARGET_NR_pause /* not on alpha */
6284 case TARGET_NR_pause
:
6285 ret
= get_errno(pause());
6288 #ifdef TARGET_NR_utime
6289 case TARGET_NR_utime
:
6291 struct utimbuf tbuf
, *host_tbuf
;
6292 struct target_utimbuf
*target_tbuf
;
6294 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
6296 tbuf
.actime
= tswapal(target_tbuf
->actime
);
6297 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
6298 unlock_user_struct(target_tbuf
, arg2
, 0);
6303 if (!(p
= lock_user_string(arg1
)))
6305 ret
= get_errno(utime(p
, host_tbuf
));
6306 unlock_user(p
, arg1
, 0);
6310 #ifdef TARGET_NR_utimes
6311 case TARGET_NR_utimes
:
6313 struct timeval
*tvp
, tv
[2];
6315 if (copy_from_user_timeval(&tv
[0], arg2
)
6316 || copy_from_user_timeval(&tv
[1],
6317 arg2
+ sizeof(struct target_timeval
)))
6323 if (!(p
= lock_user_string(arg1
)))
6325 ret
= get_errno(utimes(p
, tvp
));
6326 unlock_user(p
, arg1
, 0);
6330 #if defined(TARGET_NR_futimesat)
6331 case TARGET_NR_futimesat
:
6333 struct timeval
*tvp
, tv
[2];
6335 if (copy_from_user_timeval(&tv
[0], arg3
)
6336 || copy_from_user_timeval(&tv
[1],
6337 arg3
+ sizeof(struct target_timeval
)))
6343 if (!(p
= lock_user_string(arg2
)))
6345 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
6346 unlock_user(p
, arg2
, 0);
6350 #ifdef TARGET_NR_stty
6351 case TARGET_NR_stty
:
6354 #ifdef TARGET_NR_gtty
6355 case TARGET_NR_gtty
:
6358 #ifdef TARGET_NR_access
6359 case TARGET_NR_access
:
6360 if (!(p
= lock_user_string(arg1
)))
6362 ret
= get_errno(access(path(p
), arg2
));
6363 unlock_user(p
, arg1
, 0);
6366 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
6367 case TARGET_NR_faccessat
:
6368 if (!(p
= lock_user_string(arg2
)))
6370 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
6371 unlock_user(p
, arg2
, 0);
6374 #ifdef TARGET_NR_nice /* not on alpha */
6375 case TARGET_NR_nice
:
6376 ret
= get_errno(nice(arg1
));
6379 #ifdef TARGET_NR_ftime
6380 case TARGET_NR_ftime
:
6383 case TARGET_NR_sync
:
6387 case TARGET_NR_kill
:
6388 ret
= get_errno(kill(arg1
, target_to_host_signal(arg2
)));
6390 #ifdef TARGET_NR_rename
6391 case TARGET_NR_rename
:
6394 p
= lock_user_string(arg1
);
6395 p2
= lock_user_string(arg2
);
6397 ret
= -TARGET_EFAULT
;
6399 ret
= get_errno(rename(p
, p2
));
6400 unlock_user(p2
, arg2
, 0);
6401 unlock_user(p
, arg1
, 0);
6405 #if defined(TARGET_NR_renameat)
6406 case TARGET_NR_renameat
:
6409 p
= lock_user_string(arg2
);
6410 p2
= lock_user_string(arg4
);
6412 ret
= -TARGET_EFAULT
;
6414 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
6415 unlock_user(p2
, arg4
, 0);
6416 unlock_user(p
, arg2
, 0);
6420 #ifdef TARGET_NR_mkdir
6421 case TARGET_NR_mkdir
:
6422 if (!(p
= lock_user_string(arg1
)))
6424 ret
= get_errno(mkdir(p
, arg2
));
6425 unlock_user(p
, arg1
, 0);
6428 #if defined(TARGET_NR_mkdirat)
6429 case TARGET_NR_mkdirat
:
6430 if (!(p
= lock_user_string(arg2
)))
6432 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
6433 unlock_user(p
, arg2
, 0);
6436 #ifdef TARGET_NR_rmdir
6437 case TARGET_NR_rmdir
:
6438 if (!(p
= lock_user_string(arg1
)))
6440 ret
= get_errno(rmdir(p
));
6441 unlock_user(p
, arg1
, 0);
6445 ret
= get_errno(dup(arg1
));
6447 fd_trans_dup(arg1
, ret
);
6450 #ifdef TARGET_NR_pipe
6451 case TARGET_NR_pipe
:
6452 ret
= do_pipe(cpu_env
, arg1
, 0, 0);
6455 #ifdef TARGET_NR_pipe2
6456 case TARGET_NR_pipe2
:
6457 ret
= do_pipe(cpu_env
, arg1
,
6458 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
6461 case TARGET_NR_times
:
6463 struct target_tms
*tmsp
;
6465 ret
= get_errno(times(&tms
));
6467 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
6470 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
6471 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
6472 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
6473 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
6476 ret
= host_to_target_clock_t(ret
);
6479 #ifdef TARGET_NR_prof
6480 case TARGET_NR_prof
:
6483 #ifdef TARGET_NR_signal
6484 case TARGET_NR_signal
:
6487 case TARGET_NR_acct
:
6489 ret
= get_errno(acct(NULL
));
6491 if (!(p
= lock_user_string(arg1
)))
6493 ret
= get_errno(acct(path(p
)));
6494 unlock_user(p
, arg1
, 0);
6497 #ifdef TARGET_NR_umount2
6498 case TARGET_NR_umount2
:
6499 if (!(p
= lock_user_string(arg1
)))
6501 ret
= get_errno(umount2(p
, arg2
));
6502 unlock_user(p
, arg1
, 0);
6505 #ifdef TARGET_NR_lock
6506 case TARGET_NR_lock
:
6509 case TARGET_NR_ioctl
:
6510 ret
= do_ioctl(arg1
, arg2
, arg3
);
6512 case TARGET_NR_fcntl
:
6513 ret
= do_fcntl(arg1
, arg2
, arg3
);
6515 #ifdef TARGET_NR_mpx
6519 case TARGET_NR_setpgid
:
6520 ret
= get_errno(setpgid(arg1
, arg2
));
6522 #ifdef TARGET_NR_ulimit
6523 case TARGET_NR_ulimit
:
6526 #ifdef TARGET_NR_oldolduname
6527 case TARGET_NR_oldolduname
:
6530 case TARGET_NR_umask
:
6531 ret
= get_errno(umask(arg1
));
6533 case TARGET_NR_chroot
:
6534 if (!(p
= lock_user_string(arg1
)))
6536 ret
= get_errno(chroot(p
));
6537 unlock_user(p
, arg1
, 0);
6539 #ifdef TARGET_NR_ustat
6540 case TARGET_NR_ustat
:
6543 #ifdef TARGET_NR_dup2
6544 case TARGET_NR_dup2
:
6545 ret
= get_errno(dup2(arg1
, arg2
));
6547 fd_trans_dup(arg1
, arg2
);
6551 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
6552 case TARGET_NR_dup3
:
6553 ret
= get_errno(dup3(arg1
, arg2
, arg3
));
6555 fd_trans_dup(arg1
, arg2
);
6559 #ifdef TARGET_NR_getppid /* not on alpha */
6560 case TARGET_NR_getppid
:
6561 ret
= get_errno(getppid());
6564 #ifdef TARGET_NR_getpgrp
6565 case TARGET_NR_getpgrp
:
6566 ret
= get_errno(getpgrp());
6569 case TARGET_NR_setsid
:
6570 ret
= get_errno(setsid());
6572 #ifdef TARGET_NR_sigaction
6573 case TARGET_NR_sigaction
:
6575 #if defined(TARGET_ALPHA)
6576 struct target_sigaction act
, oact
, *pact
= 0;
6577 struct target_old_sigaction
*old_act
;
6579 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
6581 act
._sa_handler
= old_act
->_sa_handler
;
6582 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
6583 act
.sa_flags
= old_act
->sa_flags
;
6584 act
.sa_restorer
= 0;
6585 unlock_user_struct(old_act
, arg2
, 0);
6588 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6589 if (!is_error(ret
) && arg3
) {
6590 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
6592 old_act
->_sa_handler
= oact
._sa_handler
;
6593 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
6594 old_act
->sa_flags
= oact
.sa_flags
;
6595 unlock_user_struct(old_act
, arg3
, 1);
6597 #elif defined(TARGET_MIPS)
6598 struct target_sigaction act
, oact
, *pact
, *old_act
;
6601 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
6603 act
._sa_handler
= old_act
->_sa_handler
;
6604 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
6605 act
.sa_flags
= old_act
->sa_flags
;
6606 unlock_user_struct(old_act
, arg2
, 0);
6612 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6614 if (!is_error(ret
) && arg3
) {
6615 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
6617 old_act
->_sa_handler
= oact
._sa_handler
;
6618 old_act
->sa_flags
= oact
.sa_flags
;
6619 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
6620 old_act
->sa_mask
.sig
[1] = 0;
6621 old_act
->sa_mask
.sig
[2] = 0;
6622 old_act
->sa_mask
.sig
[3] = 0;
6623 unlock_user_struct(old_act
, arg3
, 1);
6626 struct target_old_sigaction
*old_act
;
6627 struct target_sigaction act
, oact
, *pact
;
6629 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
6631 act
._sa_handler
= old_act
->_sa_handler
;
6632 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
6633 act
.sa_flags
= old_act
->sa_flags
;
6634 act
.sa_restorer
= old_act
->sa_restorer
;
6635 unlock_user_struct(old_act
, arg2
, 0);
6640 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6641 if (!is_error(ret
) && arg3
) {
6642 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
6644 old_act
->_sa_handler
= oact
._sa_handler
;
6645 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
6646 old_act
->sa_flags
= oact
.sa_flags
;
6647 old_act
->sa_restorer
= oact
.sa_restorer
;
6648 unlock_user_struct(old_act
, arg3
, 1);
6654 case TARGET_NR_rt_sigaction
:
6656 #if defined(TARGET_ALPHA)
6657 struct target_sigaction act
, oact
, *pact
= 0;
6658 struct target_rt_sigaction
*rt_act
;
6659 /* ??? arg4 == sizeof(sigset_t). */
6661 if (!lock_user_struct(VERIFY_READ
, rt_act
, arg2
, 1))
6663 act
._sa_handler
= rt_act
->_sa_handler
;
6664 act
.sa_mask
= rt_act
->sa_mask
;
6665 act
.sa_flags
= rt_act
->sa_flags
;
6666 act
.sa_restorer
= arg5
;
6667 unlock_user_struct(rt_act
, arg2
, 0);
6670 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6671 if (!is_error(ret
) && arg3
) {
6672 if (!lock_user_struct(VERIFY_WRITE
, rt_act
, arg3
, 0))
6674 rt_act
->_sa_handler
= oact
._sa_handler
;
6675 rt_act
->sa_mask
= oact
.sa_mask
;
6676 rt_act
->sa_flags
= oact
.sa_flags
;
6677 unlock_user_struct(rt_act
, arg3
, 1);
6680 struct target_sigaction
*act
;
6681 struct target_sigaction
*oact
;
6684 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1))
6689 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
6690 ret
= -TARGET_EFAULT
;
6691 goto rt_sigaction_fail
;
6695 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
6698 unlock_user_struct(act
, arg2
, 0);
6700 unlock_user_struct(oact
, arg3
, 1);
6704 #ifdef TARGET_NR_sgetmask /* not on alpha */
6705 case TARGET_NR_sgetmask
:
6708 abi_ulong target_set
;
6709 do_sigprocmask(0, NULL
, &cur_set
);
6710 host_to_target_old_sigset(&target_set
, &cur_set
);
6715 #ifdef TARGET_NR_ssetmask /* not on alpha */
6716 case TARGET_NR_ssetmask
:
6718 sigset_t set
, oset
, cur_set
;
6719 abi_ulong target_set
= arg1
;
6720 do_sigprocmask(0, NULL
, &cur_set
);
6721 target_to_host_old_sigset(&set
, &target_set
);
6722 sigorset(&set
, &set
, &cur_set
);
6723 do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
6724 host_to_target_old_sigset(&target_set
, &oset
);
6729 #ifdef TARGET_NR_sigprocmask
6730 case TARGET_NR_sigprocmask
:
6732 #if defined(TARGET_ALPHA)
6733 sigset_t set
, oldset
;
6738 case TARGET_SIG_BLOCK
:
6741 case TARGET_SIG_UNBLOCK
:
6744 case TARGET_SIG_SETMASK
:
6748 ret
= -TARGET_EINVAL
;
6752 target_to_host_old_sigset(&set
, &mask
);
6754 ret
= get_errno(do_sigprocmask(how
, &set
, &oldset
));
6755 if (!is_error(ret
)) {
6756 host_to_target_old_sigset(&mask
, &oldset
);
6758 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0; /* force no error */
6761 sigset_t set
, oldset
, *set_ptr
;
6766 case TARGET_SIG_BLOCK
:
6769 case TARGET_SIG_UNBLOCK
:
6772 case TARGET_SIG_SETMASK
:
6776 ret
= -TARGET_EINVAL
;
6779 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
6781 target_to_host_old_sigset(&set
, p
);
6782 unlock_user(p
, arg2
, 0);
6788 ret
= get_errno(do_sigprocmask(how
, set_ptr
, &oldset
));
6789 if (!is_error(ret
) && arg3
) {
6790 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
6792 host_to_target_old_sigset(p
, &oldset
);
6793 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
6799 case TARGET_NR_rt_sigprocmask
:
6802 sigset_t set
, oldset
, *set_ptr
;
6806 case TARGET_SIG_BLOCK
:
6809 case TARGET_SIG_UNBLOCK
:
6812 case TARGET_SIG_SETMASK
:
6816 ret
= -TARGET_EINVAL
;
6819 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
6821 target_to_host_sigset(&set
, p
);
6822 unlock_user(p
, arg2
, 0);
6828 ret
= get_errno(do_sigprocmask(how
, set_ptr
, &oldset
));
6829 if (!is_error(ret
) && arg3
) {
6830 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
6832 host_to_target_sigset(p
, &oldset
);
6833 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
6837 #ifdef TARGET_NR_sigpending
6838 case TARGET_NR_sigpending
:
6841 ret
= get_errno(sigpending(&set
));
6842 if (!is_error(ret
)) {
6843 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
6845 host_to_target_old_sigset(p
, &set
);
6846 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
6851 case TARGET_NR_rt_sigpending
:
6854 ret
= get_errno(sigpending(&set
));
6855 if (!is_error(ret
)) {
6856 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
6858 host_to_target_sigset(p
, &set
);
6859 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
6863 #ifdef TARGET_NR_sigsuspend
6864 case TARGET_NR_sigsuspend
:
6867 #if defined(TARGET_ALPHA)
6868 abi_ulong mask
= arg1
;
6869 target_to_host_old_sigset(&set
, &mask
);
6871 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6873 target_to_host_old_sigset(&set
, p
);
6874 unlock_user(p
, arg1
, 0);
6876 ret
= get_errno(sigsuspend(&set
));
6880 case TARGET_NR_rt_sigsuspend
:
6883 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6885 target_to_host_sigset(&set
, p
);
6886 unlock_user(p
, arg1
, 0);
6887 ret
= get_errno(sigsuspend(&set
));
6890 case TARGET_NR_rt_sigtimedwait
:
6893 struct timespec uts
, *puts
;
6896 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6898 target_to_host_sigset(&set
, p
);
6899 unlock_user(p
, arg1
, 0);
6902 target_to_host_timespec(puts
, arg3
);
6906 ret
= get_errno(sigtimedwait(&set
, &uinfo
, puts
));
6907 if (!is_error(ret
)) {
6909 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
6914 host_to_target_siginfo(p
, &uinfo
);
6915 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
6917 ret
= host_to_target_signal(ret
);
6921 case TARGET_NR_rt_sigqueueinfo
:
6924 if (!(p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_sigset_t
), 1)))
6926 target_to_host_siginfo(&uinfo
, p
);
6927 unlock_user(p
, arg1
, 0);
6928 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
6931 #ifdef TARGET_NR_sigreturn
6932 case TARGET_NR_sigreturn
:
6933 /* NOTE: ret is eax, so not transcoding must be done */
6934 ret
= do_sigreturn(cpu_env
);
6937 case TARGET_NR_rt_sigreturn
:
6938 /* NOTE: ret is eax, so not transcoding must be done */
6939 ret
= do_rt_sigreturn(cpu_env
);
6941 case TARGET_NR_sethostname
:
6942 if (!(p
= lock_user_string(arg1
)))
6944 ret
= get_errno(sethostname(p
, arg2
));
6945 unlock_user(p
, arg1
, 0);
6947 case TARGET_NR_setrlimit
:
6949 int resource
= target_to_host_resource(arg1
);
6950 struct target_rlimit
*target_rlim
;
6952 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
6954 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
6955 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
6956 unlock_user_struct(target_rlim
, arg2
, 0);
6957 ret
= get_errno(setrlimit(resource
, &rlim
));
6960 case TARGET_NR_getrlimit
:
6962 int resource
= target_to_host_resource(arg1
);
6963 struct target_rlimit
*target_rlim
;
6966 ret
= get_errno(getrlimit(resource
, &rlim
));
6967 if (!is_error(ret
)) {
6968 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
6970 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
6971 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
6972 unlock_user_struct(target_rlim
, arg2
, 1);
6976 case TARGET_NR_getrusage
:
6978 struct rusage rusage
;
6979 ret
= get_errno(getrusage(arg1
, &rusage
));
6980 if (!is_error(ret
)) {
6981 ret
= host_to_target_rusage(arg2
, &rusage
);
6985 case TARGET_NR_gettimeofday
:
6988 ret
= get_errno(gettimeofday(&tv
, NULL
));
6989 if (!is_error(ret
)) {
6990 if (copy_to_user_timeval(arg1
, &tv
))
6995 case TARGET_NR_settimeofday
:
6997 struct timeval tv
, *ptv
= NULL
;
6998 struct timezone tz
, *ptz
= NULL
;
7001 if (copy_from_user_timeval(&tv
, arg1
)) {
7008 if (copy_from_user_timezone(&tz
, arg2
)) {
7014 ret
= get_errno(settimeofday(ptv
, ptz
));
7017 #if defined(TARGET_NR_select)
7018 case TARGET_NR_select
:
7019 #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
7020 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
7023 struct target_sel_arg_struct
*sel
;
7024 abi_ulong inp
, outp
, exp
, tvp
;
7027 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1))
7029 nsel
= tswapal(sel
->n
);
7030 inp
= tswapal(sel
->inp
);
7031 outp
= tswapal(sel
->outp
);
7032 exp
= tswapal(sel
->exp
);
7033 tvp
= tswapal(sel
->tvp
);
7034 unlock_user_struct(sel
, arg1
, 0);
7035 ret
= do_select(nsel
, inp
, outp
, exp
, tvp
);
7040 #ifdef TARGET_NR_pselect6
7041 case TARGET_NR_pselect6
:
7043 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
7044 fd_set rfds
, wfds
, efds
;
7045 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
7046 struct timespec ts
, *ts_ptr
;
7049 * The 6th arg is actually two args smashed together,
7050 * so we cannot use the C library.
7058 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
7059 target_sigset_t
*target_sigset
;
7067 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
7071 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
7075 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
7081 * This takes a timespec, and not a timeval, so we cannot
7082 * use the do_select() helper ...
7085 if (target_to_host_timespec(&ts
, ts_addr
)) {
7093 /* Extract the two packed args for the sigset */
7096 sig
.size
= _NSIG
/ 8;
7098 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
7102 arg_sigset
= tswapal(arg7
[0]);
7103 arg_sigsize
= tswapal(arg7
[1]);
7104 unlock_user(arg7
, arg6
, 0);
7108 if (arg_sigsize
!= sizeof(*target_sigset
)) {
7109 /* Like the kernel, we enforce correct size sigsets */
7110 ret
= -TARGET_EINVAL
;
7113 target_sigset
= lock_user(VERIFY_READ
, arg_sigset
,
7114 sizeof(*target_sigset
), 1);
7115 if (!target_sigset
) {
7118 target_to_host_sigset(&set
, target_sigset
);
7119 unlock_user(target_sigset
, arg_sigset
, 0);
7127 ret
= get_errno(sys_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
7130 if (!is_error(ret
)) {
7131 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
7133 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
7135 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
7138 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
))
7144 #ifdef TARGET_NR_symlink
7145 case TARGET_NR_symlink
:
7148 p
= lock_user_string(arg1
);
7149 p2
= lock_user_string(arg2
);
7151 ret
= -TARGET_EFAULT
;
7153 ret
= get_errno(symlink(p
, p2
));
7154 unlock_user(p2
, arg2
, 0);
7155 unlock_user(p
, arg1
, 0);
7159 #if defined(TARGET_NR_symlinkat)
7160 case TARGET_NR_symlinkat
:
7163 p
= lock_user_string(arg1
);
7164 p2
= lock_user_string(arg3
);
7166 ret
= -TARGET_EFAULT
;
7168 ret
= get_errno(symlinkat(p
, arg2
, p2
));
7169 unlock_user(p2
, arg3
, 0);
7170 unlock_user(p
, arg1
, 0);
7174 #ifdef TARGET_NR_oldlstat
7175 case TARGET_NR_oldlstat
:
7178 #ifdef TARGET_NR_readlink
7179 case TARGET_NR_readlink
:
7182 p
= lock_user_string(arg1
);
7183 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
7185 ret
= -TARGET_EFAULT
;
7187 /* Short circuit this for the magic exe check. */
7188 ret
= -TARGET_EINVAL
;
7189 } else if (is_proc_myself((const char *)p
, "exe")) {
7190 char real
[PATH_MAX
], *temp
;
7191 temp
= realpath(exec_path
, real
);
7192 /* Return value is # of bytes that we wrote to the buffer. */
7194 ret
= get_errno(-1);
7196 /* Don't worry about sign mismatch as earlier mapping
7197 * logic would have thrown a bad address error. */
7198 ret
= MIN(strlen(real
), arg3
);
7199 /* We cannot NUL terminate the string. */
7200 memcpy(p2
, real
, ret
);
7203 ret
= get_errno(readlink(path(p
), p2
, arg3
));
7205 unlock_user(p2
, arg2
, ret
);
7206 unlock_user(p
, arg1
, 0);
7210 #if defined(TARGET_NR_readlinkat)
7211 case TARGET_NR_readlinkat
:
7214 p
= lock_user_string(arg2
);
7215 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
7217 ret
= -TARGET_EFAULT
;
7218 } else if (is_proc_myself((const char *)p
, "exe")) {
7219 char real
[PATH_MAX
], *temp
;
7220 temp
= realpath(exec_path
, real
);
7221 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
7222 snprintf((char *)p2
, arg4
, "%s", real
);
7224 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
7226 unlock_user(p2
, arg3
, ret
);
7227 unlock_user(p
, arg2
, 0);
7231 #ifdef TARGET_NR_uselib
7232 case TARGET_NR_uselib
:
7235 #ifdef TARGET_NR_swapon
7236 case TARGET_NR_swapon
:
7237 if (!(p
= lock_user_string(arg1
)))
7239 ret
= get_errno(swapon(p
, arg2
));
7240 unlock_user(p
, arg1
, 0);
7243 case TARGET_NR_reboot
:
7244 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
7245 /* arg4 must be ignored in all other cases */
7246 p
= lock_user_string(arg4
);
7250 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
7251 unlock_user(p
, arg4
, 0);
7253 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
7256 #ifdef TARGET_NR_readdir
7257 case TARGET_NR_readdir
:
7260 #ifdef TARGET_NR_mmap
7261 case TARGET_NR_mmap
:
7262 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7263 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
7264 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
7265 || defined(TARGET_S390X)
7268 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
7269 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
7277 unlock_user(v
, arg1
, 0);
7278 ret
= get_errno(target_mmap(v1
, v2
, v3
,
7279 target_to_host_bitmask(v4
, mmap_flags_tbl
),
7283 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
7284 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
7290 #ifdef TARGET_NR_mmap2
7291 case TARGET_NR_mmap2
:
7293 #define MMAP_SHIFT 12
7295 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
7296 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
7298 arg6
<< MMAP_SHIFT
));
7301 case TARGET_NR_munmap
:
7302 ret
= get_errno(target_munmap(arg1
, arg2
));
7304 case TARGET_NR_mprotect
:
7306 TaskState
*ts
= cpu
->opaque
;
7307 /* Special hack to detect libc making the stack executable. */
7308 if ((arg3
& PROT_GROWSDOWN
)
7309 && arg1
>= ts
->info
->stack_limit
7310 && arg1
<= ts
->info
->start_stack
) {
7311 arg3
&= ~PROT_GROWSDOWN
;
7312 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
7313 arg1
= ts
->info
->stack_limit
;
7316 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
7318 #ifdef TARGET_NR_mremap
7319 case TARGET_NR_mremap
:
7320 ret
= get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
7323 /* ??? msync/mlock/munlock are broken for softmmu. */
7324 #ifdef TARGET_NR_msync
7325 case TARGET_NR_msync
:
7326 ret
= get_errno(msync(g2h(arg1
), arg2
, arg3
));
7329 #ifdef TARGET_NR_mlock
7330 case TARGET_NR_mlock
:
7331 ret
= get_errno(mlock(g2h(arg1
), arg2
));
7334 #ifdef TARGET_NR_munlock
7335 case TARGET_NR_munlock
:
7336 ret
= get_errno(munlock(g2h(arg1
), arg2
));
7339 #ifdef TARGET_NR_mlockall
7340 case TARGET_NR_mlockall
:
7341 ret
= get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
7344 #ifdef TARGET_NR_munlockall
7345 case TARGET_NR_munlockall
:
7346 ret
= get_errno(munlockall());
7349 case TARGET_NR_truncate
:
7350 if (!(p
= lock_user_string(arg1
)))
7352 ret
= get_errno(truncate(p
, arg2
));
7353 unlock_user(p
, arg1
, 0);
7355 case TARGET_NR_ftruncate
:
7356 ret
= get_errno(ftruncate(arg1
, arg2
));
7358 case TARGET_NR_fchmod
:
7359 ret
= get_errno(fchmod(arg1
, arg2
));
7361 #if defined(TARGET_NR_fchmodat)
7362 case TARGET_NR_fchmodat
:
7363 if (!(p
= lock_user_string(arg2
)))
7365 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
7366 unlock_user(p
, arg2
, 0);
7369 case TARGET_NR_getpriority
:
7370 /* Note that negative values are valid for getpriority, so we must
7371 differentiate based on errno settings. */
7373 ret
= getpriority(arg1
, arg2
);
7374 if (ret
== -1 && errno
!= 0) {
7375 ret
= -host_to_target_errno(errno
);
7379 /* Return value is the unbiased priority. Signal no error. */
7380 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0;
7382 /* Return value is a biased priority to avoid negative numbers. */
7386 case TARGET_NR_setpriority
:
7387 ret
= get_errno(setpriority(arg1
, arg2
, arg3
));
7389 #ifdef TARGET_NR_profil
7390 case TARGET_NR_profil
:
7393 case TARGET_NR_statfs
:
7394 if (!(p
= lock_user_string(arg1
)))
7396 ret
= get_errno(statfs(path(p
), &stfs
));
7397 unlock_user(p
, arg1
, 0);
7399 if (!is_error(ret
)) {
7400 struct target_statfs
*target_stfs
;
7402 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
7404 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
7405 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
7406 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
7407 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
7408 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
7409 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
7410 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
7411 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
7412 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
7413 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
7414 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
7415 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
7416 unlock_user_struct(target_stfs
, arg2
, 1);
7419 case TARGET_NR_fstatfs
:
7420 ret
= get_errno(fstatfs(arg1
, &stfs
));
7421 goto convert_statfs
;
7422 #ifdef TARGET_NR_statfs64
7423 case TARGET_NR_statfs64
:
7424 if (!(p
= lock_user_string(arg1
)))
7426 ret
= get_errno(statfs(path(p
), &stfs
));
7427 unlock_user(p
, arg1
, 0);
7429 if (!is_error(ret
)) {
7430 struct target_statfs64
*target_stfs
;
7432 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
7434 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
7435 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
7436 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
7437 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
7438 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
7439 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
7440 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
7441 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
7442 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
7443 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
7444 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
7445 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
7446 unlock_user_struct(target_stfs
, arg3
, 1);
7449 case TARGET_NR_fstatfs64
:
7450 ret
= get_errno(fstatfs(arg1
, &stfs
));
7451 goto convert_statfs64
;
7453 #ifdef TARGET_NR_ioperm
7454 case TARGET_NR_ioperm
:
7457 #ifdef TARGET_NR_socketcall
7458 case TARGET_NR_socketcall
:
7459 ret
= do_socketcall(arg1
, arg2
);
7462 #ifdef TARGET_NR_accept
7463 case TARGET_NR_accept
:
7464 ret
= do_accept4(arg1
, arg2
, arg3
, 0);
7467 #ifdef TARGET_NR_accept4
7468 case TARGET_NR_accept4
:
7469 #ifdef CONFIG_ACCEPT4
7470 ret
= do_accept4(arg1
, arg2
, arg3
, arg4
);
7476 #ifdef TARGET_NR_bind
7477 case TARGET_NR_bind
:
7478 ret
= do_bind(arg1
, arg2
, arg3
);
7481 #ifdef TARGET_NR_connect
7482 case TARGET_NR_connect
:
7483 ret
= do_connect(arg1
, arg2
, arg3
);
7486 #ifdef TARGET_NR_getpeername
7487 case TARGET_NR_getpeername
:
7488 ret
= do_getpeername(arg1
, arg2
, arg3
);
7491 #ifdef TARGET_NR_getsockname
7492 case TARGET_NR_getsockname
:
7493 ret
= do_getsockname(arg1
, arg2
, arg3
);
7496 #ifdef TARGET_NR_getsockopt
7497 case TARGET_NR_getsockopt
:
7498 ret
= do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
7501 #ifdef TARGET_NR_listen
7502 case TARGET_NR_listen
:
7503 ret
= get_errno(listen(arg1
, arg2
));
7506 #ifdef TARGET_NR_recv
7507 case TARGET_NR_recv
:
7508 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
7511 #ifdef TARGET_NR_recvfrom
7512 case TARGET_NR_recvfrom
:
7513 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7516 #ifdef TARGET_NR_recvmsg
7517 case TARGET_NR_recvmsg
:
7518 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
7521 #ifdef TARGET_NR_send
7522 case TARGET_NR_send
:
7523 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
7526 #ifdef TARGET_NR_sendmsg
7527 case TARGET_NR_sendmsg
:
7528 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
7531 #ifdef TARGET_NR_sendmmsg
7532 case TARGET_NR_sendmmsg
:
7533 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
7535 case TARGET_NR_recvmmsg
:
7536 ret
= do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
7539 #ifdef TARGET_NR_sendto
7540 case TARGET_NR_sendto
:
7541 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7544 #ifdef TARGET_NR_shutdown
7545 case TARGET_NR_shutdown
:
7546 ret
= get_errno(shutdown(arg1
, arg2
));
7549 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
7550 case TARGET_NR_getrandom
:
7551 p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
7555 ret
= get_errno(getrandom(p
, arg2
, arg3
));
7556 unlock_user(p
, arg1
, ret
);
7559 #ifdef TARGET_NR_socket
7560 case TARGET_NR_socket
:
7561 ret
= do_socket(arg1
, arg2
, arg3
);
7562 fd_trans_unregister(ret
);
7565 #ifdef TARGET_NR_socketpair
7566 case TARGET_NR_socketpair
:
7567 ret
= do_socketpair(arg1
, arg2
, arg3
, arg4
);
7570 #ifdef TARGET_NR_setsockopt
7571 case TARGET_NR_setsockopt
:
7572 ret
= do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
7576 case TARGET_NR_syslog
:
7577 if (!(p
= lock_user_string(arg2
)))
7579 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
7580 unlock_user(p
, arg2
, 0);
7583 case TARGET_NR_setitimer
:
7585 struct itimerval value
, ovalue
, *pvalue
;
7589 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
7590 || copy_from_user_timeval(&pvalue
->it_value
,
7591 arg2
+ sizeof(struct target_timeval
)))
7596 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
7597 if (!is_error(ret
) && arg3
) {
7598 if (copy_to_user_timeval(arg3
,
7599 &ovalue
.it_interval
)
7600 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
7606 case TARGET_NR_getitimer
:
7608 struct itimerval value
;
7610 ret
= get_errno(getitimer(arg1
, &value
));
7611 if (!is_error(ret
) && arg2
) {
7612 if (copy_to_user_timeval(arg2
,
7614 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
7620 #ifdef TARGET_NR_stat
7621 case TARGET_NR_stat
:
7622 if (!(p
= lock_user_string(arg1
)))
7624 ret
= get_errno(stat(path(p
), &st
));
7625 unlock_user(p
, arg1
, 0);
7628 #ifdef TARGET_NR_lstat
7629 case TARGET_NR_lstat
:
7630 if (!(p
= lock_user_string(arg1
)))
7632 ret
= get_errno(lstat(path(p
), &st
));
7633 unlock_user(p
, arg1
, 0);
7636 case TARGET_NR_fstat
:
7638 ret
= get_errno(fstat(arg1
, &st
));
7639 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
7642 if (!is_error(ret
)) {
7643 struct target_stat
*target_st
;
7645 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
7647 memset(target_st
, 0, sizeof(*target_st
));
7648 __put_user(st
.st_dev
, &target_st
->st_dev
);
7649 __put_user(st
.st_ino
, &target_st
->st_ino
);
7650 __put_user(st
.st_mode
, &target_st
->st_mode
);
7651 __put_user(st
.st_uid
, &target_st
->st_uid
);
7652 __put_user(st
.st_gid
, &target_st
->st_gid
);
7653 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
7654 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
7655 __put_user(st
.st_size
, &target_st
->st_size
);
7656 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
7657 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
7658 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
7659 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
7660 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
7661 unlock_user_struct(target_st
, arg2
, 1);
7665 #ifdef TARGET_NR_olduname
7666 case TARGET_NR_olduname
:
7669 #ifdef TARGET_NR_iopl
7670 case TARGET_NR_iopl
:
7673 case TARGET_NR_vhangup
:
7674 ret
= get_errno(vhangup());
7676 #ifdef TARGET_NR_idle
7677 case TARGET_NR_idle
:
7680 #ifdef TARGET_NR_syscall
7681 case TARGET_NR_syscall
:
7682 ret
= do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
7683 arg6
, arg7
, arg8
, 0);
7686 case TARGET_NR_wait4
:
7689 abi_long status_ptr
= arg2
;
7690 struct rusage rusage
, *rusage_ptr
;
7691 abi_ulong target_rusage
= arg4
;
7692 abi_long rusage_err
;
7694 rusage_ptr
= &rusage
;
7697 ret
= get_errno(wait4(arg1
, &status
, arg3
, rusage_ptr
));
7698 if (!is_error(ret
)) {
7699 if (status_ptr
&& ret
) {
7700 status
= host_to_target_waitstatus(status
);
7701 if (put_user_s32(status
, status_ptr
))
7704 if (target_rusage
) {
7705 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
7713 #ifdef TARGET_NR_swapoff
7714 case TARGET_NR_swapoff
:
7715 if (!(p
= lock_user_string(arg1
)))
7717 ret
= get_errno(swapoff(p
));
7718 unlock_user(p
, arg1
, 0);
7721 case TARGET_NR_sysinfo
:
7723 struct target_sysinfo
*target_value
;
7724 struct sysinfo value
;
7725 ret
= get_errno(sysinfo(&value
));
7726 if (!is_error(ret
) && arg1
)
7728 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
7730 __put_user(value
.uptime
, &target_value
->uptime
);
7731 __put_user(value
.loads
[0], &target_value
->loads
[0]);
7732 __put_user(value
.loads
[1], &target_value
->loads
[1]);
7733 __put_user(value
.loads
[2], &target_value
->loads
[2]);
7734 __put_user(value
.totalram
, &target_value
->totalram
);
7735 __put_user(value
.freeram
, &target_value
->freeram
);
7736 __put_user(value
.sharedram
, &target_value
->sharedram
);
7737 __put_user(value
.bufferram
, &target_value
->bufferram
);
7738 __put_user(value
.totalswap
, &target_value
->totalswap
);
7739 __put_user(value
.freeswap
, &target_value
->freeswap
);
7740 __put_user(value
.procs
, &target_value
->procs
);
7741 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
7742 __put_user(value
.freehigh
, &target_value
->freehigh
);
7743 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
7744 unlock_user_struct(target_value
, arg1
, 1);
7748 #ifdef TARGET_NR_ipc
7750 ret
= do_ipc(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7753 #ifdef TARGET_NR_semget
7754 case TARGET_NR_semget
:
7755 ret
= get_errno(semget(arg1
, arg2
, arg3
));
7758 #ifdef TARGET_NR_semop
7759 case TARGET_NR_semop
:
7760 ret
= do_semop(arg1
, arg2
, arg3
);
7763 #ifdef TARGET_NR_semctl
7764 case TARGET_NR_semctl
:
7765 ret
= do_semctl(arg1
, arg2
, arg3
, arg4
);
7768 #ifdef TARGET_NR_msgctl
7769 case TARGET_NR_msgctl
:
7770 ret
= do_msgctl(arg1
, arg2
, arg3
);
7773 #ifdef TARGET_NR_msgget
7774 case TARGET_NR_msgget
:
7775 ret
= get_errno(msgget(arg1
, arg2
));
7778 #ifdef TARGET_NR_msgrcv
7779 case TARGET_NR_msgrcv
:
7780 ret
= do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
7783 #ifdef TARGET_NR_msgsnd
7784 case TARGET_NR_msgsnd
:
7785 ret
= do_msgsnd(arg1
, arg2
, arg3
, arg4
);
7788 #ifdef TARGET_NR_shmget
7789 case TARGET_NR_shmget
:
7790 ret
= get_errno(shmget(arg1
, arg2
, arg3
));
7793 #ifdef TARGET_NR_shmctl
7794 case TARGET_NR_shmctl
:
7795 ret
= do_shmctl(arg1
, arg2
, arg3
);
7798 #ifdef TARGET_NR_shmat
7799 case TARGET_NR_shmat
:
7800 ret
= do_shmat(arg1
, arg2
, arg3
);
7803 #ifdef TARGET_NR_shmdt
7804 case TARGET_NR_shmdt
:
7805 ret
= do_shmdt(arg1
);
7808 case TARGET_NR_fsync
:
7809 ret
= get_errno(fsync(arg1
));
7811 case TARGET_NR_clone
:
7812 /* Linux manages to have three different orderings for its
7813 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7814 * match the kernel's CONFIG_CLONE_* settings.
7815 * Microblaze is further special in that it uses a sixth
7816 * implicit argument to clone for the TLS pointer.
7818 #if defined(TARGET_MICROBLAZE)
7819 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
7820 #elif defined(TARGET_CLONE_BACKWARDS)
7821 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
7822 #elif defined(TARGET_CLONE_BACKWARDS2)
7823 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
7825 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
7828 #ifdef __NR_exit_group
7829 /* new thread calls */
7830 case TARGET_NR_exit_group
:
7834 gdb_exit(cpu_env
, arg1
);
7835 ret
= get_errno(exit_group(arg1
));
7838 case TARGET_NR_setdomainname
:
7839 if (!(p
= lock_user_string(arg1
)))
7841 ret
= get_errno(setdomainname(p
, arg2
));
7842 unlock_user(p
, arg1
, 0);
7844 case TARGET_NR_uname
:
7845 /* no need to transcode because we use the linux syscall */
7847 struct new_utsname
* buf
;
7849 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
7851 ret
= get_errno(sys_uname(buf
));
7852 if (!is_error(ret
)) {
7853 /* Overrite the native machine name with whatever is being
7855 strcpy (buf
->machine
, cpu_to_uname_machine(cpu_env
));
7856 /* Allow the user to override the reported release. */
7857 if (qemu_uname_release
&& *qemu_uname_release
)
7858 strcpy (buf
->release
, qemu_uname_release
);
7860 unlock_user_struct(buf
, arg1
, 1);
7864 case TARGET_NR_modify_ldt
:
7865 ret
= do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
7867 #if !defined(TARGET_X86_64)
7868 case TARGET_NR_vm86old
:
7870 case TARGET_NR_vm86
:
7871 ret
= do_vm86(cpu_env
, arg1
, arg2
);
7875 case TARGET_NR_adjtimex
:
7877 #ifdef TARGET_NR_create_module
7878 case TARGET_NR_create_module
:
7880 case TARGET_NR_init_module
:
7881 case TARGET_NR_delete_module
:
7882 #ifdef TARGET_NR_get_kernel_syms
7883 case TARGET_NR_get_kernel_syms
:
7886 case TARGET_NR_quotactl
:
7888 case TARGET_NR_getpgid
:
7889 ret
= get_errno(getpgid(arg1
));
7891 case TARGET_NR_fchdir
:
7892 ret
= get_errno(fchdir(arg1
));
7894 #ifdef TARGET_NR_bdflush /* not on x86_64 */
7895 case TARGET_NR_bdflush
:
7898 #ifdef TARGET_NR_sysfs
7899 case TARGET_NR_sysfs
:
7902 case TARGET_NR_personality
:
7903 ret
= get_errno(personality(arg1
));
7905 #ifdef TARGET_NR_afs_syscall
7906 case TARGET_NR_afs_syscall
:
7909 #ifdef TARGET_NR__llseek /* Not on alpha */
7910 case TARGET_NR__llseek
:
7913 #if !defined(__NR_llseek)
7914 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | arg3
, arg5
);
7916 ret
= get_errno(res
);
7921 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
7923 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
7929 #ifdef TARGET_NR_getdents
7930 case TARGET_NR_getdents
:
7931 #ifdef __NR_getdents
7932 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
7934 struct target_dirent
*target_dirp
;
7935 struct linux_dirent
*dirp
;
7936 abi_long count
= arg3
;
7938 dirp
= g_try_malloc(count
);
7940 ret
= -TARGET_ENOMEM
;
7944 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
7945 if (!is_error(ret
)) {
7946 struct linux_dirent
*de
;
7947 struct target_dirent
*tde
;
7949 int reclen
, treclen
;
7950 int count1
, tnamelen
;
7954 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7958 reclen
= de
->d_reclen
;
7959 tnamelen
= reclen
- offsetof(struct linux_dirent
, d_name
);
7960 assert(tnamelen
>= 0);
7961 treclen
= tnamelen
+ offsetof(struct target_dirent
, d_name
);
7962 assert(count1
+ treclen
<= count
);
7963 tde
->d_reclen
= tswap16(treclen
);
7964 tde
->d_ino
= tswapal(de
->d_ino
);
7965 tde
->d_off
= tswapal(de
->d_off
);
7966 memcpy(tde
->d_name
, de
->d_name
, tnamelen
);
7967 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
7969 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
7973 unlock_user(target_dirp
, arg2
, ret
);
7979 struct linux_dirent
*dirp
;
7980 abi_long count
= arg3
;
7982 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7984 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
7985 if (!is_error(ret
)) {
7986 struct linux_dirent
*de
;
7991 reclen
= de
->d_reclen
;
7994 de
->d_reclen
= tswap16(reclen
);
7995 tswapls(&de
->d_ino
);
7996 tswapls(&de
->d_off
);
7997 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
8001 unlock_user(dirp
, arg2
, ret
);
8005 /* Implement getdents in terms of getdents64 */
8007 struct linux_dirent64
*dirp
;
8008 abi_long count
= arg3
;
8010 dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
8014 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
8015 if (!is_error(ret
)) {
8016 /* Convert the dirent64 structs to target dirent. We do this
8017 * in-place, since we can guarantee that a target_dirent is no
8018 * larger than a dirent64; however this means we have to be
8019 * careful to read everything before writing in the new format.
8021 struct linux_dirent64
*de
;
8022 struct target_dirent
*tde
;
8027 tde
= (struct target_dirent
*)dirp
;
8029 int namelen
, treclen
;
8030 int reclen
= de
->d_reclen
;
8031 uint64_t ino
= de
->d_ino
;
8032 int64_t off
= de
->d_off
;
8033 uint8_t type
= de
->d_type
;
8035 namelen
= strlen(de
->d_name
);
8036 treclen
= offsetof(struct target_dirent
, d_name
)
8038 treclen
= QEMU_ALIGN_UP(treclen
, sizeof(abi_long
));
8040 memmove(tde
->d_name
, de
->d_name
, namelen
+ 1);
8041 tde
->d_ino
= tswapal(ino
);
8042 tde
->d_off
= tswapal(off
);
8043 tde
->d_reclen
= tswap16(treclen
);
8044 /* The target_dirent type is in what was formerly a padding
8045 * byte at the end of the structure:
8047 *(((char *)tde
) + treclen
- 1) = type
;
8049 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
8050 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
8056 unlock_user(dirp
, arg2
, ret
);
8060 #endif /* TARGET_NR_getdents */
8061 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
8062 case TARGET_NR_getdents64
:
8064 struct linux_dirent64
*dirp
;
8065 abi_long count
= arg3
;
8066 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
8068 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
8069 if (!is_error(ret
)) {
8070 struct linux_dirent64
*de
;
8075 reclen
= de
->d_reclen
;
8078 de
->d_reclen
= tswap16(reclen
);
8079 tswap64s((uint64_t *)&de
->d_ino
);
8080 tswap64s((uint64_t *)&de
->d_off
);
8081 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
8085 unlock_user(dirp
, arg2
, ret
);
8088 #endif /* TARGET_NR_getdents64 */
8089 #if defined(TARGET_NR__newselect)
8090 case TARGET_NR__newselect
:
8091 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
8094 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
8095 # ifdef TARGET_NR_poll
8096 case TARGET_NR_poll
:
8098 # ifdef TARGET_NR_ppoll
8099 case TARGET_NR_ppoll
:
8102 struct target_pollfd
*target_pfd
;
8103 unsigned int nfds
= arg2
;
8111 target_pfd
= lock_user(VERIFY_WRITE
, arg1
,
8112 sizeof(struct target_pollfd
) * nfds
, 1);
8117 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
8118 for (i
= 0; i
< nfds
; i
++) {
8119 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
8120 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
8124 # ifdef TARGET_NR_ppoll
8125 if (num
== TARGET_NR_ppoll
) {
8126 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
8127 target_sigset_t
*target_set
;
8128 sigset_t _set
, *set
= &_set
;
8131 if (target_to_host_timespec(timeout_ts
, arg3
)) {
8132 unlock_user(target_pfd
, arg1
, 0);
8140 target_set
= lock_user(VERIFY_READ
, arg4
, sizeof(target_sigset_t
), 1);
8142 unlock_user(target_pfd
, arg1
, 0);
8145 target_to_host_sigset(set
, target_set
);
8150 ret
= get_errno(sys_ppoll(pfd
, nfds
, timeout_ts
, set
, _NSIG
/8));
8152 if (!is_error(ret
) && arg3
) {
8153 host_to_target_timespec(arg3
, timeout_ts
);
8156 unlock_user(target_set
, arg4
, 0);
8160 ret
= get_errno(poll(pfd
, nfds
, timeout
));
8162 if (!is_error(ret
)) {
8163 for(i
= 0; i
< nfds
; i
++) {
8164 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
8167 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
8171 case TARGET_NR_flock
:
8172 /* NOTE: the flock constant seems to be the same for every
8174 ret
= get_errno(flock(arg1
, arg2
));
8176 case TARGET_NR_readv
:
8178 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
8180 ret
= get_errno(readv(arg1
, vec
, arg3
));
8181 unlock_iovec(vec
, arg2
, arg3
, 1);
8183 ret
= -host_to_target_errno(errno
);
8187 case TARGET_NR_writev
:
8189 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
8191 ret
= get_errno(writev(arg1
, vec
, arg3
));
8192 unlock_iovec(vec
, arg2
, arg3
, 0);
8194 ret
= -host_to_target_errno(errno
);
8198 case TARGET_NR_getsid
:
8199 ret
= get_errno(getsid(arg1
));
8201 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
8202 case TARGET_NR_fdatasync
:
8203 ret
= get_errno(fdatasync(arg1
));
8206 #ifdef TARGET_NR__sysctl
8207 case TARGET_NR__sysctl
:
8208 /* We don't implement this, but ENOTDIR is always a safe
8210 ret
= -TARGET_ENOTDIR
;
8213 case TARGET_NR_sched_getaffinity
:
8215 unsigned int mask_size
;
8216 unsigned long *mask
;
8219 * sched_getaffinity needs multiples of ulong, so need to take
8220 * care of mismatches between target ulong and host ulong sizes.
8222 if (arg2
& (sizeof(abi_ulong
) - 1)) {
8223 ret
= -TARGET_EINVAL
;
8226 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
8228 mask
= alloca(mask_size
);
8229 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
8231 if (!is_error(ret
)) {
8233 /* More data returned than the caller's buffer will fit.
8234 * This only happens if sizeof(abi_long) < sizeof(long)
8235 * and the caller passed us a buffer holding an odd number
8236 * of abi_longs. If the host kernel is actually using the
8237 * extra 4 bytes then fail EINVAL; otherwise we can just
8238 * ignore them and only copy the interesting part.
8240 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
8241 if (numcpus
> arg2
* 8) {
8242 ret
= -TARGET_EINVAL
;
8248 if (copy_to_user(arg3
, mask
, ret
)) {
8254 case TARGET_NR_sched_setaffinity
:
8256 unsigned int mask_size
;
8257 unsigned long *mask
;
8260 * sched_setaffinity needs multiples of ulong, so need to take
8261 * care of mismatches between target ulong and host ulong sizes.
8263 if (arg2
& (sizeof(abi_ulong
) - 1)) {
8264 ret
= -TARGET_EINVAL
;
8267 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
8269 mask
= alloca(mask_size
);
8270 if (!lock_user_struct(VERIFY_READ
, p
, arg3
, 1)) {
8273 memcpy(mask
, p
, arg2
);
8274 unlock_user_struct(p
, arg2
, 0);
8276 ret
= get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
8279 case TARGET_NR_sched_setparam
:
8281 struct sched_param
*target_schp
;
8282 struct sched_param schp
;
8285 return -TARGET_EINVAL
;
8287 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
8289 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
8290 unlock_user_struct(target_schp
, arg2
, 0);
8291 ret
= get_errno(sched_setparam(arg1
, &schp
));
8294 case TARGET_NR_sched_getparam
:
8296 struct sched_param
*target_schp
;
8297 struct sched_param schp
;
8300 return -TARGET_EINVAL
;
8302 ret
= get_errno(sched_getparam(arg1
, &schp
));
8303 if (!is_error(ret
)) {
8304 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
8306 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
8307 unlock_user_struct(target_schp
, arg2
, 1);
8311 case TARGET_NR_sched_setscheduler
:
8313 struct sched_param
*target_schp
;
8314 struct sched_param schp
;
8316 return -TARGET_EINVAL
;
8318 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
8320 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
8321 unlock_user_struct(target_schp
, arg3
, 0);
8322 ret
= get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
8325 case TARGET_NR_sched_getscheduler
:
8326 ret
= get_errno(sched_getscheduler(arg1
));
8328 case TARGET_NR_sched_yield
:
8329 ret
= get_errno(sched_yield());
8331 case TARGET_NR_sched_get_priority_max
:
8332 ret
= get_errno(sched_get_priority_max(arg1
));
8334 case TARGET_NR_sched_get_priority_min
:
8335 ret
= get_errno(sched_get_priority_min(arg1
));
8337 case TARGET_NR_sched_rr_get_interval
:
8340 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
8341 if (!is_error(ret
)) {
8342 ret
= host_to_target_timespec(arg2
, &ts
);
8346 case TARGET_NR_nanosleep
:
8348 struct timespec req
, rem
;
8349 target_to_host_timespec(&req
, arg1
);
8350 ret
= get_errno(nanosleep(&req
, &rem
));
8351 if (is_error(ret
) && arg2
) {
8352 host_to_target_timespec(arg2
, &rem
);
8356 #ifdef TARGET_NR_query_module
8357 case TARGET_NR_query_module
:
8360 #ifdef TARGET_NR_nfsservctl
8361 case TARGET_NR_nfsservctl
:
8364 case TARGET_NR_prctl
:
8366 case PR_GET_PDEATHSIG
:
8369 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
8370 if (!is_error(ret
) && arg2
8371 && put_user_ual(deathsig
, arg2
)) {
8379 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
8383 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
8385 unlock_user(name
, arg2
, 16);
8390 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
8394 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
8396 unlock_user(name
, arg2
, 0);
8401 /* Most prctl options have no pointer arguments */
8402 ret
= get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
8406 #ifdef TARGET_NR_arch_prctl
8407 case TARGET_NR_arch_prctl
:
8408 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
8409 ret
= do_arch_prctl(cpu_env
, arg1
, arg2
);
8415 #ifdef TARGET_NR_pread64
8416 case TARGET_NR_pread64
:
8417 if (regpairs_aligned(cpu_env
)) {
8421 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
8423 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
8424 unlock_user(p
, arg2
, ret
);
8426 case TARGET_NR_pwrite64
:
8427 if (regpairs_aligned(cpu_env
)) {
8431 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
8433 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
8434 unlock_user(p
, arg2
, 0);
8437 case TARGET_NR_getcwd
:
8438 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
8440 ret
= get_errno(sys_getcwd1(p
, arg2
));
8441 unlock_user(p
, arg1
, ret
);
8443 case TARGET_NR_capget
:
8444 case TARGET_NR_capset
:
8446 struct target_user_cap_header
*target_header
;
8447 struct target_user_cap_data
*target_data
= NULL
;
8448 struct __user_cap_header_struct header
;
8449 struct __user_cap_data_struct data
[2];
8450 struct __user_cap_data_struct
*dataptr
= NULL
;
8451 int i
, target_datalen
;
8454 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
8457 header
.version
= tswap32(target_header
->version
);
8458 header
.pid
= tswap32(target_header
->pid
);
8460 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
8461 /* Version 2 and up takes pointer to two user_data structs */
8465 target_datalen
= sizeof(*target_data
) * data_items
;
8468 if (num
== TARGET_NR_capget
) {
8469 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
8471 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
8474 unlock_user_struct(target_header
, arg1
, 0);
8478 if (num
== TARGET_NR_capset
) {
8479 for (i
= 0; i
< data_items
; i
++) {
8480 data
[i
].effective
= tswap32(target_data
[i
].effective
);
8481 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
8482 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
8489 if (num
== TARGET_NR_capget
) {
8490 ret
= get_errno(capget(&header
, dataptr
));
8492 ret
= get_errno(capset(&header
, dataptr
));
8495 /* The kernel always updates version for both capget and capset */
8496 target_header
->version
= tswap32(header
.version
);
8497 unlock_user_struct(target_header
, arg1
, 1);
8500 if (num
== TARGET_NR_capget
) {
8501 for (i
= 0; i
< data_items
; i
++) {
8502 target_data
[i
].effective
= tswap32(data
[i
].effective
);
8503 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
8504 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
8506 unlock_user(target_data
, arg2
, target_datalen
);
8508 unlock_user(target_data
, arg2
, 0);
8513 case TARGET_NR_sigaltstack
:
8514 ret
= do_sigaltstack(arg1
, arg2
, get_sp_from_cpustate((CPUArchState
*)cpu_env
));
8517 #ifdef CONFIG_SENDFILE
8518 case TARGET_NR_sendfile
:
8523 ret
= get_user_sal(off
, arg3
);
8524 if (is_error(ret
)) {
8529 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
8530 if (!is_error(ret
) && arg3
) {
8531 abi_long ret2
= put_user_sal(off
, arg3
);
8532 if (is_error(ret2
)) {
8538 #ifdef TARGET_NR_sendfile64
8539 case TARGET_NR_sendfile64
:
8544 ret
= get_user_s64(off
, arg3
);
8545 if (is_error(ret
)) {
8550 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
8551 if (!is_error(ret
) && arg3
) {
8552 abi_long ret2
= put_user_s64(off
, arg3
);
8553 if (is_error(ret2
)) {
8561 case TARGET_NR_sendfile
:
8562 #ifdef TARGET_NR_sendfile64
8563 case TARGET_NR_sendfile64
:
8568 #ifdef TARGET_NR_getpmsg
8569 case TARGET_NR_getpmsg
:
8572 #ifdef TARGET_NR_putpmsg
8573 case TARGET_NR_putpmsg
:
8576 #ifdef TARGET_NR_vfork
8577 case TARGET_NR_vfork
:
8578 ret
= get_errno(do_fork(cpu_env
, CLONE_VFORK
| CLONE_VM
| SIGCHLD
,
8582 #ifdef TARGET_NR_ugetrlimit
8583 case TARGET_NR_ugetrlimit
:
8586 int resource
= target_to_host_resource(arg1
);
8587 ret
= get_errno(getrlimit(resource
, &rlim
));
8588 if (!is_error(ret
)) {
8589 struct target_rlimit
*target_rlim
;
8590 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
8592 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
8593 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
8594 unlock_user_struct(target_rlim
, arg2
, 1);
8599 #ifdef TARGET_NR_truncate64
8600 case TARGET_NR_truncate64
:
8601 if (!(p
= lock_user_string(arg1
)))
8603 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
8604 unlock_user(p
, arg1
, 0);
8607 #ifdef TARGET_NR_ftruncate64
8608 case TARGET_NR_ftruncate64
:
8609 ret
= target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
8612 #ifdef TARGET_NR_stat64
8613 case TARGET_NR_stat64
:
8614 if (!(p
= lock_user_string(arg1
)))
8616 ret
= get_errno(stat(path(p
), &st
));
8617 unlock_user(p
, arg1
, 0);
8619 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
8622 #ifdef TARGET_NR_lstat64
8623 case TARGET_NR_lstat64
:
8624 if (!(p
= lock_user_string(arg1
)))
8626 ret
= get_errno(lstat(path(p
), &st
));
8627 unlock_user(p
, arg1
, 0);
8629 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
8632 #ifdef TARGET_NR_fstat64
8633 case TARGET_NR_fstat64
:
8634 ret
= get_errno(fstat(arg1
, &st
));
8636 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
8639 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
8640 #ifdef TARGET_NR_fstatat64
8641 case TARGET_NR_fstatat64
:
8643 #ifdef TARGET_NR_newfstatat
8644 case TARGET_NR_newfstatat
:
8646 if (!(p
= lock_user_string(arg2
)))
8648 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
8650 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
8653 #ifdef TARGET_NR_lchown
8654 case TARGET_NR_lchown
:
8655 if (!(p
= lock_user_string(arg1
)))
8657 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
8658 unlock_user(p
, arg1
, 0);
8661 #ifdef TARGET_NR_getuid
8662 case TARGET_NR_getuid
:
8663 ret
= get_errno(high2lowuid(getuid()));
8666 #ifdef TARGET_NR_getgid
8667 case TARGET_NR_getgid
:
8668 ret
= get_errno(high2lowgid(getgid()));
8671 #ifdef TARGET_NR_geteuid
8672 case TARGET_NR_geteuid
:
8673 ret
= get_errno(high2lowuid(geteuid()));
8676 #ifdef TARGET_NR_getegid
8677 case TARGET_NR_getegid
:
8678 ret
= get_errno(high2lowgid(getegid()));
8681 case TARGET_NR_setreuid
:
8682 ret
= get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
8684 case TARGET_NR_setregid
:
8685 ret
= get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
8687 case TARGET_NR_getgroups
:
8689 int gidsetsize
= arg1
;
8690 target_id
*target_grouplist
;
8694 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8695 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
8696 if (gidsetsize
== 0)
8698 if (!is_error(ret
)) {
8699 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
8700 if (!target_grouplist
)
8702 for(i
= 0;i
< ret
; i
++)
8703 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
8704 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
8708 case TARGET_NR_setgroups
:
8710 int gidsetsize
= arg1
;
8711 target_id
*target_grouplist
;
8712 gid_t
*grouplist
= NULL
;
8715 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8716 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
8717 if (!target_grouplist
) {
8718 ret
= -TARGET_EFAULT
;
8721 for (i
= 0; i
< gidsetsize
; i
++) {
8722 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
8724 unlock_user(target_grouplist
, arg2
, 0);
8726 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
8729 case TARGET_NR_fchown
:
8730 ret
= get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
8732 #if defined(TARGET_NR_fchownat)
8733 case TARGET_NR_fchownat
:
8734 if (!(p
= lock_user_string(arg2
)))
8736 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
8737 low2highgid(arg4
), arg5
));
8738 unlock_user(p
, arg2
, 0);
8741 #ifdef TARGET_NR_setresuid
8742 case TARGET_NR_setresuid
:
8743 ret
= get_errno(setresuid(low2highuid(arg1
),
8745 low2highuid(arg3
)));
8748 #ifdef TARGET_NR_getresuid
8749 case TARGET_NR_getresuid
:
8751 uid_t ruid
, euid
, suid
;
8752 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
8753 if (!is_error(ret
)) {
8754 if (put_user_id(high2lowuid(ruid
), arg1
)
8755 || put_user_id(high2lowuid(euid
), arg2
)
8756 || put_user_id(high2lowuid(suid
), arg3
))
8762 #ifdef TARGET_NR_getresgid
8763 case TARGET_NR_setresgid
:
8764 ret
= get_errno(setresgid(low2highgid(arg1
),
8766 low2highgid(arg3
)));
8769 #ifdef TARGET_NR_getresgid
8770 case TARGET_NR_getresgid
:
8772 gid_t rgid
, egid
, sgid
;
8773 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
8774 if (!is_error(ret
)) {
8775 if (put_user_id(high2lowgid(rgid
), arg1
)
8776 || put_user_id(high2lowgid(egid
), arg2
)
8777 || put_user_id(high2lowgid(sgid
), arg3
))
8783 #ifdef TARGET_NR_chown
8784 case TARGET_NR_chown
:
8785 if (!(p
= lock_user_string(arg1
)))
8787 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
8788 unlock_user(p
, arg1
, 0);
8791 case TARGET_NR_setuid
:
8792 ret
= get_errno(setuid(low2highuid(arg1
)));
8794 case TARGET_NR_setgid
:
8795 ret
= get_errno(setgid(low2highgid(arg1
)));
8797 case TARGET_NR_setfsuid
:
8798 ret
= get_errno(setfsuid(arg1
));
8800 case TARGET_NR_setfsgid
:
8801 ret
= get_errno(setfsgid(arg1
));
8804 #ifdef TARGET_NR_lchown32
8805 case TARGET_NR_lchown32
:
8806 if (!(p
= lock_user_string(arg1
)))
8808 ret
= get_errno(lchown(p
, arg2
, arg3
));
8809 unlock_user(p
, arg1
, 0);
8812 #ifdef TARGET_NR_getuid32
8813 case TARGET_NR_getuid32
:
8814 ret
= get_errno(getuid());
8818 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
8819 /* Alpha specific */
8820 case TARGET_NR_getxuid
:
8824 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
8826 ret
= get_errno(getuid());
8829 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
8830 /* Alpha specific */
8831 case TARGET_NR_getxgid
:
8835 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
8837 ret
= get_errno(getgid());
8840 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
8841 /* Alpha specific */
8842 case TARGET_NR_osf_getsysinfo
:
8843 ret
= -TARGET_EOPNOTSUPP
;
8845 case TARGET_GSI_IEEE_FP_CONTROL
:
8847 uint64_t swcr
, fpcr
= cpu_alpha_load_fpcr (cpu_env
);
8849 /* Copied from linux ieee_fpcr_to_swcr. */
8850 swcr
= (fpcr
>> 35) & SWCR_STATUS_MASK
;
8851 swcr
|= (fpcr
>> 36) & SWCR_MAP_DMZ
;
8852 swcr
|= (~fpcr
>> 48) & (SWCR_TRAP_ENABLE_INV
8853 | SWCR_TRAP_ENABLE_DZE
8854 | SWCR_TRAP_ENABLE_OVF
);
8855 swcr
|= (~fpcr
>> 57) & (SWCR_TRAP_ENABLE_UNF
8856 | SWCR_TRAP_ENABLE_INE
);
8857 swcr
|= (fpcr
>> 47) & SWCR_MAP_UMZ
;
8858 swcr
|= (~fpcr
>> 41) & SWCR_TRAP_ENABLE_DNO
;
8860 if (put_user_u64 (swcr
, arg2
))
8866 /* case GSI_IEEE_STATE_AT_SIGNAL:
8867 -- Not implemented in linux kernel.
8869 -- Retrieves current unaligned access state; not much used.
8871 -- Retrieves implver information; surely not used.
8873 -- Grabs a copy of the HWRPB; surely not used.
8878 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8879 /* Alpha specific */
8880 case TARGET_NR_osf_setsysinfo
:
8881 ret
= -TARGET_EOPNOTSUPP
;
8883 case TARGET_SSI_IEEE_FP_CONTROL
:
8885 uint64_t swcr
, fpcr
, orig_fpcr
;
8887 if (get_user_u64 (swcr
, arg2
)) {
8890 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
8891 fpcr
= orig_fpcr
& FPCR_DYN_MASK
;
8893 /* Copied from linux ieee_swcr_to_fpcr. */
8894 fpcr
|= (swcr
& SWCR_STATUS_MASK
) << 35;
8895 fpcr
|= (swcr
& SWCR_MAP_DMZ
) << 36;
8896 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_INV
8897 | SWCR_TRAP_ENABLE_DZE
8898 | SWCR_TRAP_ENABLE_OVF
)) << 48;
8899 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_UNF
8900 | SWCR_TRAP_ENABLE_INE
)) << 57;
8901 fpcr
|= (swcr
& SWCR_MAP_UMZ
? FPCR_UNDZ
| FPCR_UNFD
: 0);
8902 fpcr
|= (~swcr
& SWCR_TRAP_ENABLE_DNO
) << 41;
8904 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
8909 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
8911 uint64_t exc
, fpcr
, orig_fpcr
;
8914 if (get_user_u64(exc
, arg2
)) {
8918 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
8920 /* We only add to the exception status here. */
8921 fpcr
= orig_fpcr
| ((exc
& SWCR_STATUS_MASK
) << 35);
8923 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
8926 /* Old exceptions are not signaled. */
8927 fpcr
&= ~(orig_fpcr
& FPCR_STATUS_MASK
);
8929 /* If any exceptions set by this call,
8930 and are unmasked, send a signal. */
8932 if ((fpcr
& (FPCR_INE
| FPCR_INED
)) == FPCR_INE
) {
8933 si_code
= TARGET_FPE_FLTRES
;
8935 if ((fpcr
& (FPCR_UNF
| FPCR_UNFD
)) == FPCR_UNF
) {
8936 si_code
= TARGET_FPE_FLTUND
;
8938 if ((fpcr
& (FPCR_OVF
| FPCR_OVFD
)) == FPCR_OVF
) {
8939 si_code
= TARGET_FPE_FLTOVF
;
8941 if ((fpcr
& (FPCR_DZE
| FPCR_DZED
)) == FPCR_DZE
) {
8942 si_code
= TARGET_FPE_FLTDIV
;
8944 if ((fpcr
& (FPCR_INV
| FPCR_INVD
)) == FPCR_INV
) {
8945 si_code
= TARGET_FPE_FLTINV
;
8948 target_siginfo_t info
;
8949 info
.si_signo
= SIGFPE
;
8951 info
.si_code
= si_code
;
8952 info
._sifields
._sigfault
._addr
8953 = ((CPUArchState
*)cpu_env
)->pc
;
8954 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
8959 /* case SSI_NVPAIRS:
8960 -- Used with SSIN_UACPROC to enable unaligned accesses.
8961 case SSI_IEEE_STATE_AT_SIGNAL:
8962 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8963 -- Not implemented in linux kernel
8968 #ifdef TARGET_NR_osf_sigprocmask
8969 /* Alpha specific. */
8970 case TARGET_NR_osf_sigprocmask
:
8974 sigset_t set
, oldset
;
8977 case TARGET_SIG_BLOCK
:
8980 case TARGET_SIG_UNBLOCK
:
8983 case TARGET_SIG_SETMASK
:
8987 ret
= -TARGET_EINVAL
;
8991 target_to_host_old_sigset(&set
, &mask
);
8992 do_sigprocmask(how
, &set
, &oldset
);
8993 host_to_target_old_sigset(&mask
, &oldset
);
8999 #ifdef TARGET_NR_getgid32
9000 case TARGET_NR_getgid32
:
9001 ret
= get_errno(getgid());
9004 #ifdef TARGET_NR_geteuid32
9005 case TARGET_NR_geteuid32
:
9006 ret
= get_errno(geteuid());
9009 #ifdef TARGET_NR_getegid32
9010 case TARGET_NR_getegid32
:
9011 ret
= get_errno(getegid());
9014 #ifdef TARGET_NR_setreuid32
9015 case TARGET_NR_setreuid32
:
9016 ret
= get_errno(setreuid(arg1
, arg2
));
9019 #ifdef TARGET_NR_setregid32
9020 case TARGET_NR_setregid32
:
9021 ret
= get_errno(setregid(arg1
, arg2
));
9024 #ifdef TARGET_NR_getgroups32
9025 case TARGET_NR_getgroups32
:
9027 int gidsetsize
= arg1
;
9028 uint32_t *target_grouplist
;
9032 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
9033 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
9034 if (gidsetsize
== 0)
9036 if (!is_error(ret
)) {
9037 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
9038 if (!target_grouplist
) {
9039 ret
= -TARGET_EFAULT
;
9042 for(i
= 0;i
< ret
; i
++)
9043 target_grouplist
[i
] = tswap32(grouplist
[i
]);
9044 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
9049 #ifdef TARGET_NR_setgroups32
9050 case TARGET_NR_setgroups32
:
9052 int gidsetsize
= arg1
;
9053 uint32_t *target_grouplist
;
9057 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
9058 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
9059 if (!target_grouplist
) {
9060 ret
= -TARGET_EFAULT
;
9063 for(i
= 0;i
< gidsetsize
; i
++)
9064 grouplist
[i
] = tswap32(target_grouplist
[i
]);
9065 unlock_user(target_grouplist
, arg2
, 0);
9066 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
9070 #ifdef TARGET_NR_fchown32
9071 case TARGET_NR_fchown32
:
9072 ret
= get_errno(fchown(arg1
, arg2
, arg3
));
9075 #ifdef TARGET_NR_setresuid32
9076 case TARGET_NR_setresuid32
:
9077 ret
= get_errno(setresuid(arg1
, arg2
, arg3
));
9080 #ifdef TARGET_NR_getresuid32
9081 case TARGET_NR_getresuid32
:
9083 uid_t ruid
, euid
, suid
;
9084 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
9085 if (!is_error(ret
)) {
9086 if (put_user_u32(ruid
, arg1
)
9087 || put_user_u32(euid
, arg2
)
9088 || put_user_u32(suid
, arg3
))
9094 #ifdef TARGET_NR_setresgid32
9095 case TARGET_NR_setresgid32
:
9096 ret
= get_errno(setresgid(arg1
, arg2
, arg3
));
9099 #ifdef TARGET_NR_getresgid32
9100 case TARGET_NR_getresgid32
:
9102 gid_t rgid
, egid
, sgid
;
9103 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
9104 if (!is_error(ret
)) {
9105 if (put_user_u32(rgid
, arg1
)
9106 || put_user_u32(egid
, arg2
)
9107 || put_user_u32(sgid
, arg3
))
9113 #ifdef TARGET_NR_chown32
9114 case TARGET_NR_chown32
:
9115 if (!(p
= lock_user_string(arg1
)))
9117 ret
= get_errno(chown(p
, arg2
, arg3
));
9118 unlock_user(p
, arg1
, 0);
9121 #ifdef TARGET_NR_setuid32
9122 case TARGET_NR_setuid32
:
9123 ret
= get_errno(setuid(arg1
));
9126 #ifdef TARGET_NR_setgid32
9127 case TARGET_NR_setgid32
:
9128 ret
= get_errno(setgid(arg1
));
9131 #ifdef TARGET_NR_setfsuid32
9132 case TARGET_NR_setfsuid32
:
9133 ret
= get_errno(setfsuid(arg1
));
9136 #ifdef TARGET_NR_setfsgid32
9137 case TARGET_NR_setfsgid32
:
9138 ret
= get_errno(setfsgid(arg1
));
9142 case TARGET_NR_pivot_root
:
9144 #ifdef TARGET_NR_mincore
9145 case TARGET_NR_mincore
:
9148 ret
= -TARGET_EFAULT
;
9149 if (!(a
= lock_user(VERIFY_READ
, arg1
,arg2
, 0)))
9151 if (!(p
= lock_user_string(arg3
)))
9153 ret
= get_errno(mincore(a
, arg2
, p
));
9154 unlock_user(p
, arg3
, ret
);
9156 unlock_user(a
, arg1
, 0);
9160 #ifdef TARGET_NR_arm_fadvise64_64
9161 case TARGET_NR_arm_fadvise64_64
:
9164 * arm_fadvise64_64 looks like fadvise64_64 but
9165 * with different argument order
9173 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
9174 #ifdef TARGET_NR_fadvise64_64
9175 case TARGET_NR_fadvise64_64
:
9177 #ifdef TARGET_NR_fadvise64
9178 case TARGET_NR_fadvise64
:
9182 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
9183 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
9184 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
9185 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
9189 ret
= -posix_fadvise(arg1
, arg2
, arg3
, arg4
);
9192 #ifdef TARGET_NR_madvise
9193 case TARGET_NR_madvise
:
9194 /* A straight passthrough may not be safe because qemu sometimes
9195 turns private file-backed mappings into anonymous mappings.
9196 This will break MADV_DONTNEED.
9197 This is a hint, so ignoring and returning success is ok. */
9201 #if TARGET_ABI_BITS == 32
9202 case TARGET_NR_fcntl64
:
9206 struct target_flock64
*target_fl
;
9208 struct target_eabi_flock64
*target_efl
;
9211 cmd
= target_to_host_fcntl_cmd(arg2
);
9212 if (cmd
== -TARGET_EINVAL
) {
9218 case TARGET_F_GETLK64
:
9220 if (((CPUARMState
*)cpu_env
)->eabi
) {
9221 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
9223 fl
.l_type
= tswap16(target_efl
->l_type
);
9224 fl
.l_whence
= tswap16(target_efl
->l_whence
);
9225 fl
.l_start
= tswap64(target_efl
->l_start
);
9226 fl
.l_len
= tswap64(target_efl
->l_len
);
9227 fl
.l_pid
= tswap32(target_efl
->l_pid
);
9228 unlock_user_struct(target_efl
, arg3
, 0);
9232 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
9234 fl
.l_type
= tswap16(target_fl
->l_type
);
9235 fl
.l_whence
= tswap16(target_fl
->l_whence
);
9236 fl
.l_start
= tswap64(target_fl
->l_start
);
9237 fl
.l_len
= tswap64(target_fl
->l_len
);
9238 fl
.l_pid
= tswap32(target_fl
->l_pid
);
9239 unlock_user_struct(target_fl
, arg3
, 0);
9241 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
9244 if (((CPUARMState
*)cpu_env
)->eabi
) {
9245 if (!lock_user_struct(VERIFY_WRITE
, target_efl
, arg3
, 0))
9247 target_efl
->l_type
= tswap16(fl
.l_type
);
9248 target_efl
->l_whence
= tswap16(fl
.l_whence
);
9249 target_efl
->l_start
= tswap64(fl
.l_start
);
9250 target_efl
->l_len
= tswap64(fl
.l_len
);
9251 target_efl
->l_pid
= tswap32(fl
.l_pid
);
9252 unlock_user_struct(target_efl
, arg3
, 1);
9256 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg3
, 0))
9258 target_fl
->l_type
= tswap16(fl
.l_type
);
9259 target_fl
->l_whence
= tswap16(fl
.l_whence
);
9260 target_fl
->l_start
= tswap64(fl
.l_start
);
9261 target_fl
->l_len
= tswap64(fl
.l_len
);
9262 target_fl
->l_pid
= tswap32(fl
.l_pid
);
9263 unlock_user_struct(target_fl
, arg3
, 1);
9268 case TARGET_F_SETLK64
:
9269 case TARGET_F_SETLKW64
:
9271 if (((CPUARMState
*)cpu_env
)->eabi
) {
9272 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
9274 fl
.l_type
= tswap16(target_efl
->l_type
);
9275 fl
.l_whence
= tswap16(target_efl
->l_whence
);
9276 fl
.l_start
= tswap64(target_efl
->l_start
);
9277 fl
.l_len
= tswap64(target_efl
->l_len
);
9278 fl
.l_pid
= tswap32(target_efl
->l_pid
);
9279 unlock_user_struct(target_efl
, arg3
, 0);
9283 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
9285 fl
.l_type
= tswap16(target_fl
->l_type
);
9286 fl
.l_whence
= tswap16(target_fl
->l_whence
);
9287 fl
.l_start
= tswap64(target_fl
->l_start
);
9288 fl
.l_len
= tswap64(target_fl
->l_len
);
9289 fl
.l_pid
= tswap32(target_fl
->l_pid
);
9290 unlock_user_struct(target_fl
, arg3
, 0);
9292 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
9295 ret
= do_fcntl(arg1
, arg2
, arg3
);
9301 #ifdef TARGET_NR_cacheflush
9302 case TARGET_NR_cacheflush
:
9303 /* self-modifying code is handled automatically, so nothing needed */
9307 #ifdef TARGET_NR_security
9308 case TARGET_NR_security
:
9311 #ifdef TARGET_NR_getpagesize
9312 case TARGET_NR_getpagesize
:
9313 ret
= TARGET_PAGE_SIZE
;
9316 case TARGET_NR_gettid
:
9317 ret
= get_errno(gettid());
9319 #ifdef TARGET_NR_readahead
9320 case TARGET_NR_readahead
:
9321 #if TARGET_ABI_BITS == 32
9322 if (regpairs_aligned(cpu_env
)) {
9327 ret
= get_errno(readahead(arg1
, ((off64_t
)arg3
<< 32) | arg2
, arg4
));
9329 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
9334 #ifdef TARGET_NR_setxattr
9335 case TARGET_NR_listxattr
:
9336 case TARGET_NR_llistxattr
:
9340 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
9342 ret
= -TARGET_EFAULT
;
9346 p
= lock_user_string(arg1
);
9348 if (num
== TARGET_NR_listxattr
) {
9349 ret
= get_errno(listxattr(p
, b
, arg3
));
9351 ret
= get_errno(llistxattr(p
, b
, arg3
));
9354 ret
= -TARGET_EFAULT
;
9356 unlock_user(p
, arg1
, 0);
9357 unlock_user(b
, arg2
, arg3
);
9360 case TARGET_NR_flistxattr
:
9364 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
9366 ret
= -TARGET_EFAULT
;
9370 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
9371 unlock_user(b
, arg2
, arg3
);
9374 case TARGET_NR_setxattr
:
9375 case TARGET_NR_lsetxattr
:
9377 void *p
, *n
, *v
= 0;
9379 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
9381 ret
= -TARGET_EFAULT
;
9385 p
= lock_user_string(arg1
);
9386 n
= lock_user_string(arg2
);
9388 if (num
== TARGET_NR_setxattr
) {
9389 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
9391 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
9394 ret
= -TARGET_EFAULT
;
9396 unlock_user(p
, arg1
, 0);
9397 unlock_user(n
, arg2
, 0);
9398 unlock_user(v
, arg3
, 0);
9401 case TARGET_NR_fsetxattr
:
9405 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
9407 ret
= -TARGET_EFAULT
;
9411 n
= lock_user_string(arg2
);
9413 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
9415 ret
= -TARGET_EFAULT
;
9417 unlock_user(n
, arg2
, 0);
9418 unlock_user(v
, arg3
, 0);
9421 case TARGET_NR_getxattr
:
9422 case TARGET_NR_lgetxattr
:
9424 void *p
, *n
, *v
= 0;
9426 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
9428 ret
= -TARGET_EFAULT
;
9432 p
= lock_user_string(arg1
);
9433 n
= lock_user_string(arg2
);
9435 if (num
== TARGET_NR_getxattr
) {
9436 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
9438 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
9441 ret
= -TARGET_EFAULT
;
9443 unlock_user(p
, arg1
, 0);
9444 unlock_user(n
, arg2
, 0);
9445 unlock_user(v
, arg3
, arg4
);
9448 case TARGET_NR_fgetxattr
:
9452 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
9454 ret
= -TARGET_EFAULT
;
9458 n
= lock_user_string(arg2
);
9460 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
9462 ret
= -TARGET_EFAULT
;
9464 unlock_user(n
, arg2
, 0);
9465 unlock_user(v
, arg3
, arg4
);
9468 case TARGET_NR_removexattr
:
9469 case TARGET_NR_lremovexattr
:
9472 p
= lock_user_string(arg1
);
9473 n
= lock_user_string(arg2
);
9475 if (num
== TARGET_NR_removexattr
) {
9476 ret
= get_errno(removexattr(p
, n
));
9478 ret
= get_errno(lremovexattr(p
, n
));
9481 ret
= -TARGET_EFAULT
;
9483 unlock_user(p
, arg1
, 0);
9484 unlock_user(n
, arg2
, 0);
9487 case TARGET_NR_fremovexattr
:
9490 n
= lock_user_string(arg2
);
9492 ret
= get_errno(fremovexattr(arg1
, n
));
9494 ret
= -TARGET_EFAULT
;
9496 unlock_user(n
, arg2
, 0);
9500 #endif /* CONFIG_ATTR */
9501 #ifdef TARGET_NR_set_thread_area
9502 case TARGET_NR_set_thread_area
:
9503 #if defined(TARGET_MIPS)
9504 ((CPUMIPSState
*) cpu_env
)->active_tc
.CP0_UserLocal
= arg1
;
9507 #elif defined(TARGET_CRIS)
9509 ret
= -TARGET_EINVAL
;
9511 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
9515 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
9516 ret
= do_set_thread_area(cpu_env
, arg1
);
9518 #elif defined(TARGET_M68K)
9520 TaskState
*ts
= cpu
->opaque
;
9521 ts
->tp_value
= arg1
;
9526 goto unimplemented_nowarn
;
9529 #ifdef TARGET_NR_get_thread_area
9530 case TARGET_NR_get_thread_area
:
9531 #if defined(TARGET_I386) && defined(TARGET_ABI32)
9532 ret
= do_get_thread_area(cpu_env
, arg1
);
9534 #elif defined(TARGET_M68K)
9536 TaskState
*ts
= cpu
->opaque
;
9541 goto unimplemented_nowarn
;
9544 #ifdef TARGET_NR_getdomainname
9545 case TARGET_NR_getdomainname
:
9546 goto unimplemented_nowarn
;
9549 #ifdef TARGET_NR_clock_gettime
9550 case TARGET_NR_clock_gettime
:
9553 ret
= get_errno(clock_gettime(arg1
, &ts
));
9554 if (!is_error(ret
)) {
9555 host_to_target_timespec(arg2
, &ts
);
9560 #ifdef TARGET_NR_clock_getres
9561 case TARGET_NR_clock_getres
:
9564 ret
= get_errno(clock_getres(arg1
, &ts
));
9565 if (!is_error(ret
)) {
9566 host_to_target_timespec(arg2
, &ts
);
9571 #ifdef TARGET_NR_clock_nanosleep
9572 case TARGET_NR_clock_nanosleep
:
9575 target_to_host_timespec(&ts
, arg3
);
9576 ret
= get_errno(clock_nanosleep(arg1
, arg2
, &ts
, arg4
? &ts
: NULL
));
9578 host_to_target_timespec(arg4
, &ts
);
9580 #if defined(TARGET_PPC)
9581 /* clock_nanosleep is odd in that it returns positive errno values.
9582 * On PPC, CR0 bit 3 should be set in such a situation. */
9584 ((CPUPPCState
*)cpu_env
)->crf
[0] |= 1;
9591 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
9592 case TARGET_NR_set_tid_address
:
9593 ret
= get_errno(set_tid_address((int *)g2h(arg1
)));
9597 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
9598 case TARGET_NR_tkill
:
9599 ret
= get_errno(sys_tkill((int)arg1
, target_to_host_signal(arg2
)));
9603 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
9604 case TARGET_NR_tgkill
:
9605 ret
= get_errno(sys_tgkill((int)arg1
, (int)arg2
,
9606 target_to_host_signal(arg3
)));
9610 #ifdef TARGET_NR_set_robust_list
9611 case TARGET_NR_set_robust_list
:
9612 case TARGET_NR_get_robust_list
:
9613 /* The ABI for supporting robust futexes has userspace pass
9614 * the kernel a pointer to a linked list which is updated by
9615 * userspace after the syscall; the list is walked by the kernel
9616 * when the thread exits. Since the linked list in QEMU guest
9617 * memory isn't a valid linked list for the host and we have
9618 * no way to reliably intercept the thread-death event, we can't
9619 * support these. Silently return ENOSYS so that guest userspace
9620 * falls back to a non-robust futex implementation (which should
9621 * be OK except in the corner case of the guest crashing while
9622 * holding a mutex that is shared with another process via
9625 goto unimplemented_nowarn
;
9628 #if defined(TARGET_NR_utimensat)
9629 case TARGET_NR_utimensat
:
9631 struct timespec
*tsp
, ts
[2];
9635 target_to_host_timespec(ts
, arg3
);
9636 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
9640 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
9642 if (!(p
= lock_user_string(arg2
))) {
9643 ret
= -TARGET_EFAULT
;
9646 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
9647 unlock_user(p
, arg2
, 0);
9652 case TARGET_NR_futex
:
9653 ret
= do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9655 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
9656 case TARGET_NR_inotify_init
:
9657 ret
= get_errno(sys_inotify_init());
9660 #ifdef CONFIG_INOTIFY1
9661 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
9662 case TARGET_NR_inotify_init1
:
9663 ret
= get_errno(sys_inotify_init1(arg1
));
9667 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
9668 case TARGET_NR_inotify_add_watch
:
9669 p
= lock_user_string(arg2
);
9670 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
9671 unlock_user(p
, arg2
, 0);
9674 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
9675 case TARGET_NR_inotify_rm_watch
:
9676 ret
= get_errno(sys_inotify_rm_watch(arg1
, arg2
));
9680 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
9681 case TARGET_NR_mq_open
:
9683 struct mq_attr posix_mq_attr
, *attrp
;
9685 p
= lock_user_string(arg1
- 1);
9687 copy_from_user_mq_attr (&posix_mq_attr
, arg4
);
9688 attrp
= &posix_mq_attr
;
9692 ret
= get_errno(mq_open(p
, arg2
, arg3
, attrp
));
9693 unlock_user (p
, arg1
, 0);
9697 case TARGET_NR_mq_unlink
:
9698 p
= lock_user_string(arg1
- 1);
9699 ret
= get_errno(mq_unlink(p
));
9700 unlock_user (p
, arg1
, 0);
9703 case TARGET_NR_mq_timedsend
:
9707 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
9709 target_to_host_timespec(&ts
, arg5
);
9710 ret
= get_errno(mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
9711 host_to_target_timespec(arg5
, &ts
);
9714 ret
= get_errno(mq_send(arg1
, p
, arg3
, arg4
));
9715 unlock_user (p
, arg2
, arg3
);
9719 case TARGET_NR_mq_timedreceive
:
9724 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
9726 target_to_host_timespec(&ts
, arg5
);
9727 ret
= get_errno(mq_timedreceive(arg1
, p
, arg3
, &prio
, &ts
));
9728 host_to_target_timespec(arg5
, &ts
);
9731 ret
= get_errno(mq_receive(arg1
, p
, arg3
, &prio
));
9732 unlock_user (p
, arg2
, arg3
);
9734 put_user_u32(prio
, arg4
);
9738 /* Not implemented for now... */
9739 /* case TARGET_NR_mq_notify: */
9742 case TARGET_NR_mq_getsetattr
:
9744 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
9747 ret
= mq_getattr(arg1
, &posix_mq_attr_out
);
9748 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
9751 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
9752 ret
|= mq_setattr(arg1
, &posix_mq_attr_in
, &posix_mq_attr_out
);
9759 #ifdef CONFIG_SPLICE
9760 #ifdef TARGET_NR_tee
9763 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
9767 #ifdef TARGET_NR_splice
9768 case TARGET_NR_splice
:
9770 loff_t loff_in
, loff_out
;
9771 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
9773 if (get_user_u64(loff_in
, arg2
)) {
9776 ploff_in
= &loff_in
;
9779 if (get_user_u64(loff_out
, arg4
)) {
9782 ploff_out
= &loff_out
;
9784 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
9786 if (put_user_u64(loff_in
, arg2
)) {
9791 if (put_user_u64(loff_out
, arg4
)) {
9798 #ifdef TARGET_NR_vmsplice
9799 case TARGET_NR_vmsplice
:
9801 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
9803 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
9804 unlock_iovec(vec
, arg2
, arg3
, 0);
9806 ret
= -host_to_target_errno(errno
);
9811 #endif /* CONFIG_SPLICE */
9812 #ifdef CONFIG_EVENTFD
9813 #if defined(TARGET_NR_eventfd)
9814 case TARGET_NR_eventfd
:
9815 ret
= get_errno(eventfd(arg1
, 0));
9816 fd_trans_unregister(ret
);
9819 #if defined(TARGET_NR_eventfd2)
9820 case TARGET_NR_eventfd2
:
9822 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
));
9823 if (arg2
& TARGET_O_NONBLOCK
) {
9824 host_flags
|= O_NONBLOCK
;
9826 if (arg2
& TARGET_O_CLOEXEC
) {
9827 host_flags
|= O_CLOEXEC
;
9829 ret
= get_errno(eventfd(arg1
, host_flags
));
9830 fd_trans_unregister(ret
);
9834 #endif /* CONFIG_EVENTFD */
9835 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
9836 case TARGET_NR_fallocate
:
9837 #if TARGET_ABI_BITS == 32
9838 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
9839 target_offset64(arg5
, arg6
)));
9841 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
9845 #if defined(CONFIG_SYNC_FILE_RANGE)
9846 #if defined(TARGET_NR_sync_file_range)
9847 case TARGET_NR_sync_file_range
:
9848 #if TARGET_ABI_BITS == 32
9849 #if defined(TARGET_MIPS)
9850 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
9851 target_offset64(arg5
, arg6
), arg7
));
9853 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
9854 target_offset64(arg4
, arg5
), arg6
));
9855 #endif /* !TARGET_MIPS */
9857 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
9861 #if defined(TARGET_NR_sync_file_range2)
9862 case TARGET_NR_sync_file_range2
:
9863 /* This is like sync_file_range but the arguments are reordered */
9864 #if TARGET_ABI_BITS == 32
9865 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
9866 target_offset64(arg5
, arg6
), arg2
));
9868 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
9873 #if defined(TARGET_NR_signalfd4)
9874 case TARGET_NR_signalfd4
:
9875 ret
= do_signalfd4(arg1
, arg2
, arg4
);
9878 #if defined(TARGET_NR_signalfd)
9879 case TARGET_NR_signalfd
:
9880 ret
= do_signalfd4(arg1
, arg2
, 0);
9883 #if defined(CONFIG_EPOLL)
9884 #if defined(TARGET_NR_epoll_create)
9885 case TARGET_NR_epoll_create
:
9886 ret
= get_errno(epoll_create(arg1
));
9889 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
9890 case TARGET_NR_epoll_create1
:
9891 ret
= get_errno(epoll_create1(arg1
));
9894 #if defined(TARGET_NR_epoll_ctl)
9895 case TARGET_NR_epoll_ctl
:
9897 struct epoll_event ep
;
9898 struct epoll_event
*epp
= 0;
9900 struct target_epoll_event
*target_ep
;
9901 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
9904 ep
.events
= tswap32(target_ep
->events
);
9905 /* The epoll_data_t union is just opaque data to the kernel,
9906 * so we transfer all 64 bits across and need not worry what
9907 * actual data type it is.
9909 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
9910 unlock_user_struct(target_ep
, arg4
, 0);
9913 ret
= get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
9918 #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9919 #define IMPLEMENT_EPOLL_PWAIT
9921 #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9922 #if defined(TARGET_NR_epoll_wait)
9923 case TARGET_NR_epoll_wait
:
9925 #if defined(IMPLEMENT_EPOLL_PWAIT)
9926 case TARGET_NR_epoll_pwait
:
9929 struct target_epoll_event
*target_ep
;
9930 struct epoll_event
*ep
;
9932 int maxevents
= arg3
;
9935 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
9936 maxevents
* sizeof(struct target_epoll_event
), 1);
9941 ep
= alloca(maxevents
* sizeof(struct epoll_event
));
9944 #if defined(IMPLEMENT_EPOLL_PWAIT)
9945 case TARGET_NR_epoll_pwait
:
9947 target_sigset_t
*target_set
;
9948 sigset_t _set
, *set
= &_set
;
9951 target_set
= lock_user(VERIFY_READ
, arg5
,
9952 sizeof(target_sigset_t
), 1);
9954 unlock_user(target_ep
, arg2
, 0);
9957 target_to_host_sigset(set
, target_set
);
9958 unlock_user(target_set
, arg5
, 0);
9963 ret
= get_errno(epoll_pwait(epfd
, ep
, maxevents
, timeout
, set
));
9967 #if defined(TARGET_NR_epoll_wait)
9968 case TARGET_NR_epoll_wait
:
9969 ret
= get_errno(epoll_wait(epfd
, ep
, maxevents
, timeout
));
9973 ret
= -TARGET_ENOSYS
;
9975 if (!is_error(ret
)) {
9977 for (i
= 0; i
< ret
; i
++) {
9978 target_ep
[i
].events
= tswap32(ep
[i
].events
);
9979 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
9982 unlock_user(target_ep
, arg2
, ret
* sizeof(struct target_epoll_event
));
9987 #ifdef TARGET_NR_prlimit64
9988 case TARGET_NR_prlimit64
:
9990 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9991 struct target_rlimit64
*target_rnew
, *target_rold
;
9992 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
9993 int resource
= target_to_host_resource(arg2
);
9995 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
9998 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
9999 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
10000 unlock_user_struct(target_rnew
, arg3
, 0);
10004 ret
= get_errno(sys_prlimit64(arg1
, resource
, rnewp
, arg4
? &rold
: 0));
10005 if (!is_error(ret
) && arg4
) {
10006 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
10009 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
10010 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
10011 unlock_user_struct(target_rold
, arg4
, 1);
10016 #ifdef TARGET_NR_gethostname
10017 case TARGET_NR_gethostname
:
10019 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
10021 ret
= get_errno(gethostname(name
, arg2
));
10022 unlock_user(name
, arg1
, arg2
);
10024 ret
= -TARGET_EFAULT
;
10029 #ifdef TARGET_NR_atomic_cmpxchg_32
10030 case TARGET_NR_atomic_cmpxchg_32
:
10032 /* should use start_exclusive from main.c */
10033 abi_ulong mem_value
;
10034 if (get_user_u32(mem_value
, arg6
)) {
10035 target_siginfo_t info
;
10036 info
.si_signo
= SIGSEGV
;
10038 info
.si_code
= TARGET_SEGV_MAPERR
;
10039 info
._sifields
._sigfault
._addr
= arg6
;
10040 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
10044 if (mem_value
== arg2
)
10045 put_user_u32(arg1
, arg6
);
10050 #ifdef TARGET_NR_atomic_barrier
10051 case TARGET_NR_atomic_barrier
:
10053 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
10059 #ifdef TARGET_NR_timer_create
10060 case TARGET_NR_timer_create
:
10062 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
10064 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
10067 int timer_index
= next_free_host_timer();
10069 if (timer_index
< 0) {
10070 ret
= -TARGET_EAGAIN
;
10072 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
10075 phost_sevp
= &host_sevp
;
10076 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
10082 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
10086 if (put_user(TIMER_MAGIC
| timer_index
, arg3
, target_timer_t
)) {
10095 #ifdef TARGET_NR_timer_settime
10096 case TARGET_NR_timer_settime
:
10098 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
10099 * struct itimerspec * old_value */
10100 target_timer_t timerid
= get_timer_id(arg1
);
10104 } else if (arg3
== 0) {
10105 ret
= -TARGET_EINVAL
;
10107 timer_t htimer
= g_posix_timers
[timerid
];
10108 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
10110 target_to_host_itimerspec(&hspec_new
, arg3
);
10112 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
10113 host_to_target_itimerspec(arg2
, &hspec_old
);
10119 #ifdef TARGET_NR_timer_gettime
10120 case TARGET_NR_timer_gettime
:
10122 /* args: timer_t timerid, struct itimerspec *curr_value */
10123 target_timer_t timerid
= get_timer_id(arg1
);
10127 } else if (!arg2
) {
10128 ret
= -TARGET_EFAULT
;
10130 timer_t htimer
= g_posix_timers
[timerid
];
10131 struct itimerspec hspec
;
10132 ret
= get_errno(timer_gettime(htimer
, &hspec
));
10134 if (host_to_target_itimerspec(arg2
, &hspec
)) {
10135 ret
= -TARGET_EFAULT
;
10142 #ifdef TARGET_NR_timer_getoverrun
10143 case TARGET_NR_timer_getoverrun
:
10145 /* args: timer_t timerid */
10146 target_timer_t timerid
= get_timer_id(arg1
);
10151 timer_t htimer
= g_posix_timers
[timerid
];
10152 ret
= get_errno(timer_getoverrun(htimer
));
10154 fd_trans_unregister(ret
);
10159 #ifdef TARGET_NR_timer_delete
10160 case TARGET_NR_timer_delete
:
10162 /* args: timer_t timerid */
10163 target_timer_t timerid
= get_timer_id(arg1
);
10168 timer_t htimer
= g_posix_timers
[timerid
];
10169 ret
= get_errno(timer_delete(htimer
));
10170 g_posix_timers
[timerid
] = 0;
10176 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
10177 case TARGET_NR_timerfd_create
:
10178 ret
= get_errno(timerfd_create(arg1
,
10179 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
10183 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
10184 case TARGET_NR_timerfd_gettime
:
10186 struct itimerspec its_curr
;
10188 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
10190 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
10197 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
10198 case TARGET_NR_timerfd_settime
:
10200 struct itimerspec its_new
, its_old
, *p_new
;
10203 if (target_to_host_itimerspec(&its_new
, arg3
)) {
10211 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
10213 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
10220 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
10221 case TARGET_NR_ioprio_get
:
10222 ret
= get_errno(ioprio_get(arg1
, arg2
));
10226 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
10227 case TARGET_NR_ioprio_set
:
10228 ret
= get_errno(ioprio_set(arg1
, arg2
, arg3
));
10232 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
10233 case TARGET_NR_setns
:
10234 ret
= get_errno(setns(arg1
, arg2
));
10237 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
10238 case TARGET_NR_unshare
:
10239 ret
= get_errno(unshare(arg1
));
10245 gemu_log("qemu: Unsupported syscall: %d\n", num
);
10246 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
10247 unimplemented_nowarn
:
10249 ret
= -TARGET_ENOSYS
;
10254 gemu_log(" = " TARGET_ABI_FMT_ld
"\n", ret
);
10257 print_syscall_ret(num
, ret
);
10260 ret
= -TARGET_EFAULT
;