4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
32 #include <sys/types.h>
38 #include <sys/mount.h>
40 #include <sys/fsuid.h>
41 #include <sys/personality.h>
42 #include <sys/prctl.h>
43 #include <sys/resource.h>
49 int __clone2(int (*fn
)(void *), void *child_stack_base
,
50 size_t stack_size
, int flags
, void *arg
, ...);
52 #include <sys/socket.h>
56 #include <sys/times.h>
59 #include <sys/statfs.h>
61 #include <sys/sysinfo.h>
62 #include <sys/utsname.h>
63 //#include <sys/user.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
66 #include <linux/wireless.h>
67 #include <linux/icmp.h>
68 #include "qemu-common.h"
73 #include <sys/eventfd.h>
76 #include <sys/epoll.h>
79 #include "qemu/xattr.h"
81 #ifdef CONFIG_SENDFILE
82 #include <sys/sendfile.h>
85 #define termios host_termios
86 #define winsize host_winsize
87 #define termio host_termio
88 #define sgttyb host_sgttyb /* same as target */
89 #define tchars host_tchars /* same as target */
90 #define ltchars host_ltchars /* same as target */
92 #include <linux/termios.h>
93 #include <linux/unistd.h>
94 #include <linux/utsname.h>
95 #include <linux/cdrom.h>
96 #include <linux/hdreg.h>
97 #include <linux/soundcard.h>
99 #include <linux/mtio.h>
100 #include <linux/fs.h>
101 #if defined(CONFIG_FIEMAP)
102 #include <linux/fiemap.h>
104 #include <linux/fb.h>
105 #include <linux/vt.h>
106 #include <linux/dm-ioctl.h>
107 #include <linux/reboot.h>
108 #include <linux/route.h>
109 #include <linux/filter.h>
110 #include "linux_loop.h"
111 #include "cpu-uname.h"
115 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
116 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
120 //#include <linux/msdos_fs.h>
121 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
122 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
133 #define _syscall0(type,name) \
134 static type name (void) \
136 return syscall(__NR_##name); \
139 #define _syscall1(type,name,type1,arg1) \
140 static type name (type1 arg1) \
142 return syscall(__NR_##name, arg1); \
145 #define _syscall2(type,name,type1,arg1,type2,arg2) \
146 static type name (type1 arg1,type2 arg2) \
148 return syscall(__NR_##name, arg1, arg2); \
151 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
152 static type name (type1 arg1,type2 arg2,type3 arg3) \
154 return syscall(__NR_##name, arg1, arg2, arg3); \
157 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
158 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
160 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
163 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
165 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
167 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
171 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
172 type5,arg5,type6,arg6) \
173 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
176 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
180 #define __NR_sys_uname __NR_uname
181 #define __NR_sys_getcwd1 __NR_getcwd
182 #define __NR_sys_getdents __NR_getdents
183 #define __NR_sys_getdents64 __NR_getdents64
184 #define __NR_sys_getpriority __NR_getpriority
185 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
186 #define __NR_sys_syslog __NR_syslog
187 #define __NR_sys_tgkill __NR_tgkill
188 #define __NR_sys_tkill __NR_tkill
189 #define __NR_sys_futex __NR_futex
190 #define __NR_sys_inotify_init __NR_inotify_init
191 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
192 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
194 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__) || \
196 #define __NR__llseek __NR_lseek
200 _syscall0(int, gettid
)
202 /* This is a replacement for the host gettid() and must return a host
204 static int gettid(void) {
209 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
211 #if !defined(__NR_getdents) || \
212 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
213 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
215 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
216 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
217 loff_t
*, res
, uint
, wh
);
219 _syscall3(int,sys_rt_sigqueueinfo
,int,pid
,int,sig
,siginfo_t
*,uinfo
)
220 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
221 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
222 _syscall3(int,sys_tgkill
,int,tgid
,int,pid
,int,sig
)
224 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
225 _syscall2(int,sys_tkill
,int,tid
,int,sig
)
227 #ifdef __NR_exit_group
228 _syscall1(int,exit_group
,int,error_code
)
230 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
231 _syscall1(int,set_tid_address
,int *,tidptr
)
233 #if defined(TARGET_NR_futex) && defined(__NR_futex)
234 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
235 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
237 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
238 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
239 unsigned long *, user_mask_ptr
);
240 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
241 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
242 unsigned long *, user_mask_ptr
);
243 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
246 static bitmask_transtbl fcntl_flags_tbl
[] = {
247 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
248 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
249 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
250 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
251 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
252 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
253 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
254 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
255 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
256 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
257 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
258 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
259 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
260 #if defined(O_DIRECT)
261 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
263 #if defined(O_NOATIME)
264 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
266 #if defined(O_CLOEXEC)
267 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
270 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
272 /* Don't terminate the list prematurely on 64-bit host+guest. */
273 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
274 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
279 #define COPY_UTSNAME_FIELD(dest, src) \
281 /* __NEW_UTS_LEN doesn't include terminating null */ \
282 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
283 (dest)[__NEW_UTS_LEN] = '\0'; \
286 static int sys_uname(struct new_utsname
*buf
)
288 struct utsname uts_buf
;
290 if (uname(&uts_buf
) < 0)
294 * Just in case these have some differences, we
295 * translate utsname to new_utsname (which is the
296 * struct linux kernel uses).
299 memset(buf
, 0, sizeof(*buf
));
300 COPY_UTSNAME_FIELD(buf
->sysname
, uts_buf
.sysname
);
301 COPY_UTSNAME_FIELD(buf
->nodename
, uts_buf
.nodename
);
302 COPY_UTSNAME_FIELD(buf
->release
, uts_buf
.release
);
303 COPY_UTSNAME_FIELD(buf
->version
, uts_buf
.version
);
304 COPY_UTSNAME_FIELD(buf
->machine
, uts_buf
.machine
);
306 COPY_UTSNAME_FIELD(buf
->domainname
, uts_buf
.domainname
);
310 #undef COPY_UTSNAME_FIELD
313 static int sys_getcwd1(char *buf
, size_t size
)
315 if (getcwd(buf
, size
) == NULL
) {
316 /* getcwd() sets errno */
319 return strlen(buf
)+1;
322 #ifdef TARGET_NR_openat
323 static int sys_openat(int dirfd
, const char *pathname
, int flags
, mode_t mode
)
326 * open(2) has extra parameter 'mode' when called with
329 if ((flags
& O_CREAT
) != 0) {
330 return (openat(dirfd
, pathname
, flags
, mode
));
332 return (openat(dirfd
, pathname
, flags
));
336 #ifdef TARGET_NR_utimensat
337 #ifdef CONFIG_UTIMENSAT
338 static int sys_utimensat(int dirfd
, const char *pathname
,
339 const struct timespec times
[2], int flags
)
341 if (pathname
== NULL
)
342 return futimens(dirfd
, times
);
344 return utimensat(dirfd
, pathname
, times
, flags
);
346 #elif defined(__NR_utimensat)
347 #define __NR_sys_utimensat __NR_utimensat
348 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
349 const struct timespec
*,tsp
,int,flags
)
351 static int sys_utimensat(int dirfd
, const char *pathname
,
352 const struct timespec times
[2], int flags
)
358 #endif /* TARGET_NR_utimensat */
360 #ifdef CONFIG_INOTIFY
361 #include <sys/inotify.h>
363 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
364 static int sys_inotify_init(void)
366 return (inotify_init());
369 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
370 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
372 return (inotify_add_watch(fd
, pathname
, mask
));
375 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
376 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
378 return (inotify_rm_watch(fd
, wd
));
381 #ifdef CONFIG_INOTIFY1
382 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
383 static int sys_inotify_init1(int flags
)
385 return (inotify_init1(flags
));
390 /* Userspace can usually survive runtime without inotify */
391 #undef TARGET_NR_inotify_init
392 #undef TARGET_NR_inotify_init1
393 #undef TARGET_NR_inotify_add_watch
394 #undef TARGET_NR_inotify_rm_watch
395 #endif /* CONFIG_INOTIFY */
397 #if defined(TARGET_NR_ppoll)
399 # define __NR_ppoll -1
401 #define __NR_sys_ppoll __NR_ppoll
402 _syscall5(int, sys_ppoll
, struct pollfd
*, fds
, nfds_t
, nfds
,
403 struct timespec
*, timeout
, const __sigset_t
*, sigmask
,
407 #if defined(TARGET_NR_pselect6)
408 #ifndef __NR_pselect6
409 # define __NR_pselect6 -1
411 #define __NR_sys_pselect6 __NR_pselect6
412 _syscall6(int, sys_pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
,
413 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
);
416 #if defined(TARGET_NR_prlimit64)
417 #ifndef __NR_prlimit64
418 # define __NR_prlimit64 -1
420 #define __NR_sys_prlimit64 __NR_prlimit64
421 /* The glibc rlimit structure may not be that used by the underlying syscall */
422 struct host_rlimit64
{
426 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
427 const struct host_rlimit64
*, new_limit
,
428 struct host_rlimit64
*, old_limit
)
431 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
433 static inline int regpairs_aligned(void *cpu_env
) {
434 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
436 #elif defined(TARGET_MIPS)
437 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
438 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
439 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
440 * of registers which translates to the same as ARM/MIPS, because we start with
442 static inline int regpairs_aligned(void *cpu_env
) { return 1; }
444 static inline int regpairs_aligned(void *cpu_env
) { return 0; }
447 #define ERRNO_TABLE_SIZE 1200
449 /* target_to_host_errno_table[] is initialized from
450 * host_to_target_errno_table[] in syscall_init(). */
451 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
455 * This list is the union of errno values overridden in asm-<arch>/errno.h
456 * minus the errnos that are not actually generic to all archs.
458 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
459 [EIDRM
] = TARGET_EIDRM
,
460 [ECHRNG
] = TARGET_ECHRNG
,
461 [EL2NSYNC
] = TARGET_EL2NSYNC
,
462 [EL3HLT
] = TARGET_EL3HLT
,
463 [EL3RST
] = TARGET_EL3RST
,
464 [ELNRNG
] = TARGET_ELNRNG
,
465 [EUNATCH
] = TARGET_EUNATCH
,
466 [ENOCSI
] = TARGET_ENOCSI
,
467 [EL2HLT
] = TARGET_EL2HLT
,
468 [EDEADLK
] = TARGET_EDEADLK
,
469 [ENOLCK
] = TARGET_ENOLCK
,
470 [EBADE
] = TARGET_EBADE
,
471 [EBADR
] = TARGET_EBADR
,
472 [EXFULL
] = TARGET_EXFULL
,
473 [ENOANO
] = TARGET_ENOANO
,
474 [EBADRQC
] = TARGET_EBADRQC
,
475 [EBADSLT
] = TARGET_EBADSLT
,
476 [EBFONT
] = TARGET_EBFONT
,
477 [ENOSTR
] = TARGET_ENOSTR
,
478 [ENODATA
] = TARGET_ENODATA
,
479 [ETIME
] = TARGET_ETIME
,
480 [ENOSR
] = TARGET_ENOSR
,
481 [ENONET
] = TARGET_ENONET
,
482 [ENOPKG
] = TARGET_ENOPKG
,
483 [EREMOTE
] = TARGET_EREMOTE
,
484 [ENOLINK
] = TARGET_ENOLINK
,
485 [EADV
] = TARGET_EADV
,
486 [ESRMNT
] = TARGET_ESRMNT
,
487 [ECOMM
] = TARGET_ECOMM
,
488 [EPROTO
] = TARGET_EPROTO
,
489 [EDOTDOT
] = TARGET_EDOTDOT
,
490 [EMULTIHOP
] = TARGET_EMULTIHOP
,
491 [EBADMSG
] = TARGET_EBADMSG
,
492 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
493 [EOVERFLOW
] = TARGET_EOVERFLOW
,
494 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
495 [EBADFD
] = TARGET_EBADFD
,
496 [EREMCHG
] = TARGET_EREMCHG
,
497 [ELIBACC
] = TARGET_ELIBACC
,
498 [ELIBBAD
] = TARGET_ELIBBAD
,
499 [ELIBSCN
] = TARGET_ELIBSCN
,
500 [ELIBMAX
] = TARGET_ELIBMAX
,
501 [ELIBEXEC
] = TARGET_ELIBEXEC
,
502 [EILSEQ
] = TARGET_EILSEQ
,
503 [ENOSYS
] = TARGET_ENOSYS
,
504 [ELOOP
] = TARGET_ELOOP
,
505 [ERESTART
] = TARGET_ERESTART
,
506 [ESTRPIPE
] = TARGET_ESTRPIPE
,
507 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
508 [EUSERS
] = TARGET_EUSERS
,
509 [ENOTSOCK
] = TARGET_ENOTSOCK
,
510 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
511 [EMSGSIZE
] = TARGET_EMSGSIZE
,
512 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
513 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
514 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
515 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
516 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
517 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
518 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
519 [EADDRINUSE
] = TARGET_EADDRINUSE
,
520 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
521 [ENETDOWN
] = TARGET_ENETDOWN
,
522 [ENETUNREACH
] = TARGET_ENETUNREACH
,
523 [ENETRESET
] = TARGET_ENETRESET
,
524 [ECONNABORTED
] = TARGET_ECONNABORTED
,
525 [ECONNRESET
] = TARGET_ECONNRESET
,
526 [ENOBUFS
] = TARGET_ENOBUFS
,
527 [EISCONN
] = TARGET_EISCONN
,
528 [ENOTCONN
] = TARGET_ENOTCONN
,
529 [EUCLEAN
] = TARGET_EUCLEAN
,
530 [ENOTNAM
] = TARGET_ENOTNAM
,
531 [ENAVAIL
] = TARGET_ENAVAIL
,
532 [EISNAM
] = TARGET_EISNAM
,
533 [EREMOTEIO
] = TARGET_EREMOTEIO
,
534 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
535 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
536 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
537 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
538 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
539 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
540 [EALREADY
] = TARGET_EALREADY
,
541 [EINPROGRESS
] = TARGET_EINPROGRESS
,
542 [ESTALE
] = TARGET_ESTALE
,
543 [ECANCELED
] = TARGET_ECANCELED
,
544 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
545 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
547 [ENOKEY
] = TARGET_ENOKEY
,
550 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
553 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
556 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
559 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
561 #ifdef ENOTRECOVERABLE
562 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
566 static inline int host_to_target_errno(int err
)
568 if(host_to_target_errno_table
[err
])
569 return host_to_target_errno_table
[err
];
573 static inline int target_to_host_errno(int err
)
575 if (target_to_host_errno_table
[err
])
576 return target_to_host_errno_table
[err
];
580 static inline abi_long
get_errno(abi_long ret
)
583 return -host_to_target_errno(errno
);
588 static inline int is_error(abi_long ret
)
590 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
593 char *target_strerror(int err
)
595 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
598 return strerror(target_to_host_errno(err
));
601 static abi_ulong target_brk
;
602 static abi_ulong target_original_brk
;
603 static abi_ulong brk_page
;
605 void target_set_brk(abi_ulong new_brk
)
607 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
608 brk_page
= HOST_PAGE_ALIGN(target_brk
);
611 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
612 #define DEBUGF_BRK(message, args...)
614 /* do_brk() must return target values and target errnos. */
615 abi_long
do_brk(abi_ulong new_brk
)
617 abi_long mapped_addr
;
620 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
623 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
626 if (new_brk
< target_original_brk
) {
627 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
632 /* If the new brk is less than the highest page reserved to the
633 * target heap allocation, set it and we're almost done... */
634 if (new_brk
<= brk_page
) {
635 /* Heap contents are initialized to zero, as for anonymous
637 if (new_brk
> target_brk
) {
638 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
640 target_brk
= new_brk
;
641 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
645 /* We need to allocate more memory after the brk... Note that
646 * we don't use MAP_FIXED because that will map over the top of
647 * any existing mapping (like the one with the host libc or qemu
648 * itself); instead we treat "mapped but at wrong address" as
649 * a failure and unmap again.
651 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
652 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
653 PROT_READ
|PROT_WRITE
,
654 MAP_ANON
|MAP_PRIVATE
, 0, 0));
656 if (mapped_addr
== brk_page
) {
657 /* Heap contents are initialized to zero, as for anonymous
658 * mapped pages. Technically the new pages are already
659 * initialized to zero since they *are* anonymous mapped
660 * pages, however we have to take care with the contents that
661 * come from the remaining part of the previous page: it may
662 * contains garbage data due to a previous heap usage (grown
664 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
666 target_brk
= new_brk
;
667 brk_page
= HOST_PAGE_ALIGN(target_brk
);
668 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
671 } else if (mapped_addr
!= -1) {
672 /* Mapped but at wrong address, meaning there wasn't actually
673 * enough space for this brk.
675 target_munmap(mapped_addr
, new_alloc_size
);
677 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
680 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
683 #if defined(TARGET_ALPHA)
684 /* We (partially) emulate OSF/1 on Alpha, which requires we
685 return a proper errno, not an unchanged brk value. */
686 return -TARGET_ENOMEM
;
688 /* For everything else, return the previous break. */
692 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
693 abi_ulong target_fds_addr
,
697 abi_ulong b
, *target_fds
;
699 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
700 if (!(target_fds
= lock_user(VERIFY_READ
,
702 sizeof(abi_ulong
) * nw
,
704 return -TARGET_EFAULT
;
708 for (i
= 0; i
< nw
; i
++) {
709 /* grab the abi_ulong */
710 __get_user(b
, &target_fds
[i
]);
711 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
712 /* check the bit inside the abi_ulong */
719 unlock_user(target_fds
, target_fds_addr
, 0);
724 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
725 abi_ulong target_fds_addr
,
728 if (target_fds_addr
) {
729 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
730 return -TARGET_EFAULT
;
738 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
744 abi_ulong
*target_fds
;
746 nw
= (n
+ TARGET_ABI_BITS
- 1) / TARGET_ABI_BITS
;
747 if (!(target_fds
= lock_user(VERIFY_WRITE
,
749 sizeof(abi_ulong
) * nw
,
751 return -TARGET_EFAULT
;
754 for (i
= 0; i
< nw
; i
++) {
756 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
757 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
760 __put_user(v
, &target_fds
[i
]);
763 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
768 #if defined(__alpha__)
774 static inline abi_long
host_to_target_clock_t(long ticks
)
776 #if HOST_HZ == TARGET_HZ
779 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
783 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
784 const struct rusage
*rusage
)
786 struct target_rusage
*target_rusage
;
788 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
789 return -TARGET_EFAULT
;
790 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
791 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
792 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
793 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
794 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
795 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
796 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
797 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
798 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
799 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
800 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
801 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
802 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
803 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
804 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
805 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
806 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
807 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
808 unlock_user_struct(target_rusage
, target_addr
, 1);
813 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
815 abi_ulong target_rlim_swap
;
818 target_rlim_swap
= tswapal(target_rlim
);
819 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
820 return RLIM_INFINITY
;
822 result
= target_rlim_swap
;
823 if (target_rlim_swap
!= (rlim_t
)result
)
824 return RLIM_INFINITY
;
829 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
831 abi_ulong target_rlim_swap
;
834 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
835 target_rlim_swap
= TARGET_RLIM_INFINITY
;
837 target_rlim_swap
= rlim
;
838 result
= tswapal(target_rlim_swap
);
843 static inline int target_to_host_resource(int code
)
846 case TARGET_RLIMIT_AS
:
848 case TARGET_RLIMIT_CORE
:
850 case TARGET_RLIMIT_CPU
:
852 case TARGET_RLIMIT_DATA
:
854 case TARGET_RLIMIT_FSIZE
:
856 case TARGET_RLIMIT_LOCKS
:
858 case TARGET_RLIMIT_MEMLOCK
:
859 return RLIMIT_MEMLOCK
;
860 case TARGET_RLIMIT_MSGQUEUE
:
861 return RLIMIT_MSGQUEUE
;
862 case TARGET_RLIMIT_NICE
:
864 case TARGET_RLIMIT_NOFILE
:
865 return RLIMIT_NOFILE
;
866 case TARGET_RLIMIT_NPROC
:
868 case TARGET_RLIMIT_RSS
:
870 case TARGET_RLIMIT_RTPRIO
:
871 return RLIMIT_RTPRIO
;
872 case TARGET_RLIMIT_SIGPENDING
:
873 return RLIMIT_SIGPENDING
;
874 case TARGET_RLIMIT_STACK
:
881 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
882 abi_ulong target_tv_addr
)
884 struct target_timeval
*target_tv
;
886 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1))
887 return -TARGET_EFAULT
;
889 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
890 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
892 unlock_user_struct(target_tv
, target_tv_addr
, 0);
897 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
898 const struct timeval
*tv
)
900 struct target_timeval
*target_tv
;
902 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0))
903 return -TARGET_EFAULT
;
905 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
906 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
908 unlock_user_struct(target_tv
, target_tv_addr
, 1);
913 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
916 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
917 abi_ulong target_mq_attr_addr
)
919 struct target_mq_attr
*target_mq_attr
;
921 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
922 target_mq_attr_addr
, 1))
923 return -TARGET_EFAULT
;
925 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
926 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
927 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
928 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
930 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
935 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
936 const struct mq_attr
*attr
)
938 struct target_mq_attr
*target_mq_attr
;
940 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
941 target_mq_attr_addr
, 0))
942 return -TARGET_EFAULT
;
944 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
945 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
946 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
947 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
949 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
955 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
956 /* do_select() must return target values and target errnos. */
957 static abi_long
do_select(int n
,
958 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
959 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
961 fd_set rfds
, wfds
, efds
;
962 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
963 struct timeval tv
, *tv_ptr
;
966 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
970 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
974 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
979 if (target_tv_addr
) {
980 if (copy_from_user_timeval(&tv
, target_tv_addr
))
981 return -TARGET_EFAULT
;
987 ret
= get_errno(select(n
, rfds_ptr
, wfds_ptr
, efds_ptr
, tv_ptr
));
989 if (!is_error(ret
)) {
990 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
991 return -TARGET_EFAULT
;
992 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
993 return -TARGET_EFAULT
;
994 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
995 return -TARGET_EFAULT
;
997 if (target_tv_addr
&& copy_to_user_timeval(target_tv_addr
, &tv
))
998 return -TARGET_EFAULT
;
1005 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1008 return pipe2(host_pipe
, flags
);
1014 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1015 int flags
, int is_pipe2
)
1019 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1022 return get_errno(ret
);
1024 /* Several targets have special calling conventions for the original
1025 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1027 #if defined(TARGET_ALPHA)
1028 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1029 return host_pipe
[0];
1030 #elif defined(TARGET_MIPS)
1031 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1032 return host_pipe
[0];
1033 #elif defined(TARGET_SH4)
1034 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1035 return host_pipe
[0];
1036 #elif defined(TARGET_SPARC)
1037 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1038 return host_pipe
[0];
1042 if (put_user_s32(host_pipe
[0], pipedes
)
1043 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1044 return -TARGET_EFAULT
;
1045 return get_errno(ret
);
1048 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1049 abi_ulong target_addr
,
1052 struct target_ip_mreqn
*target_smreqn
;
1054 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1056 return -TARGET_EFAULT
;
1057 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1058 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1059 if (len
== sizeof(struct target_ip_mreqn
))
1060 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1061 unlock_user(target_smreqn
, target_addr
, 0);
1066 static inline abi_long
target_to_host_sockaddr(struct sockaddr
*addr
,
1067 abi_ulong target_addr
,
1070 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1071 sa_family_t sa_family
;
1072 struct target_sockaddr
*target_saddr
;
1074 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1076 return -TARGET_EFAULT
;
1078 sa_family
= tswap16(target_saddr
->sa_family
);
1080 /* Oops. The caller might send a incomplete sun_path; sun_path
1081 * must be terminated by \0 (see the manual page), but
1082 * unfortunately it is quite common to specify sockaddr_un
1083 * length as "strlen(x->sun_path)" while it should be
1084 * "strlen(...) + 1". We'll fix that here if needed.
1085 * Linux kernel has a similar feature.
1088 if (sa_family
== AF_UNIX
) {
1089 if (len
< unix_maxlen
&& len
> 0) {
1090 char *cp
= (char*)target_saddr
;
1092 if ( cp
[len
-1] && !cp
[len
] )
1095 if (len
> unix_maxlen
)
1099 memcpy(addr
, target_saddr
, len
);
1100 addr
->sa_family
= sa_family
;
1101 unlock_user(target_saddr
, target_addr
, 0);
1106 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1107 struct sockaddr
*addr
,
1110 struct target_sockaddr
*target_saddr
;
1112 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1114 return -TARGET_EFAULT
;
1115 memcpy(target_saddr
, addr
, len
);
1116 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1117 unlock_user(target_saddr
, target_addr
, len
);
1122 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1123 struct target_msghdr
*target_msgh
)
1125 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1126 abi_long msg_controllen
;
1127 abi_ulong target_cmsg_addr
;
1128 struct target_cmsghdr
*target_cmsg
;
1129 socklen_t space
= 0;
1131 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1132 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1134 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1135 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1137 return -TARGET_EFAULT
;
1139 while (cmsg
&& target_cmsg
) {
1140 void *data
= CMSG_DATA(cmsg
);
1141 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1143 int len
= tswapal(target_cmsg
->cmsg_len
)
1144 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr
));
1146 space
+= CMSG_SPACE(len
);
1147 if (space
> msgh
->msg_controllen
) {
1148 space
-= CMSG_SPACE(len
);
1149 gemu_log("Host cmsg overflow\n");
1153 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1154 cmsg
->cmsg_level
= SOL_SOCKET
;
1156 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1158 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1159 cmsg
->cmsg_len
= CMSG_LEN(len
);
1161 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
1162 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1163 memcpy(data
, target_data
, len
);
1165 int *fd
= (int *)data
;
1166 int *target_fd
= (int *)target_data
;
1167 int i
, numfds
= len
/ sizeof(int);
1169 for (i
= 0; i
< numfds
; i
++)
1170 fd
[i
] = tswap32(target_fd
[i
]);
1173 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1174 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
);
1176 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1178 msgh
->msg_controllen
= space
;
1182 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1183 struct msghdr
*msgh
)
1185 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1186 abi_long msg_controllen
;
1187 abi_ulong target_cmsg_addr
;
1188 struct target_cmsghdr
*target_cmsg
;
1189 socklen_t space
= 0;
1191 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1192 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1194 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1195 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1197 return -TARGET_EFAULT
;
1199 while (cmsg
&& target_cmsg
) {
1200 void *data
= CMSG_DATA(cmsg
);
1201 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1203 int len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof (struct cmsghdr
));
1205 space
+= TARGET_CMSG_SPACE(len
);
1206 if (space
> msg_controllen
) {
1207 space
-= TARGET_CMSG_SPACE(len
);
1208 gemu_log("Target cmsg overflow\n");
1212 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1213 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1215 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1217 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1218 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(len
));
1220 if ((cmsg
->cmsg_level
== SOL_SOCKET
) &&
1221 (cmsg
->cmsg_type
== SCM_RIGHTS
)) {
1222 int *fd
= (int *)data
;
1223 int *target_fd
= (int *)target_data
;
1224 int i
, numfds
= len
/ sizeof(int);
1226 for (i
= 0; i
< numfds
; i
++)
1227 target_fd
[i
] = tswap32(fd
[i
]);
1228 } else if ((cmsg
->cmsg_level
== SOL_SOCKET
) &&
1229 (cmsg
->cmsg_type
== SO_TIMESTAMP
) &&
1230 (len
== sizeof(struct timeval
))) {
1231 /* copy struct timeval to target */
1232 struct timeval
*tv
= (struct timeval
*)data
;
1233 struct target_timeval
*target_tv
=
1234 (struct target_timeval
*)target_data
;
1236 target_tv
->tv_sec
= tswapal(tv
->tv_sec
);
1237 target_tv
->tv_usec
= tswapal(tv
->tv_usec
);
1239 gemu_log("Unsupported ancillary data: %d/%d\n",
1240 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1241 memcpy(target_data
, data
, len
);
1244 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1245 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
);
1247 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1249 target_msgh
->msg_controllen
= tswapal(space
);
1253 /* do_setsockopt() Must return target values and target errnos. */
1254 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1255 abi_ulong optval_addr
, socklen_t optlen
)
1259 struct ip_mreqn
*ip_mreq
;
1260 struct ip_mreq_source
*ip_mreq_source
;
1264 /* TCP options all take an 'int' value. */
1265 if (optlen
< sizeof(uint32_t))
1266 return -TARGET_EINVAL
;
1268 if (get_user_u32(val
, optval_addr
))
1269 return -TARGET_EFAULT
;
1270 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1277 case IP_ROUTER_ALERT
:
1281 case IP_MTU_DISCOVER
:
1287 case IP_MULTICAST_TTL
:
1288 case IP_MULTICAST_LOOP
:
1290 if (optlen
>= sizeof(uint32_t)) {
1291 if (get_user_u32(val
, optval_addr
))
1292 return -TARGET_EFAULT
;
1293 } else if (optlen
>= 1) {
1294 if (get_user_u8(val
, optval_addr
))
1295 return -TARGET_EFAULT
;
1297 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1299 case IP_ADD_MEMBERSHIP
:
1300 case IP_DROP_MEMBERSHIP
:
1301 if (optlen
< sizeof (struct target_ip_mreq
) ||
1302 optlen
> sizeof (struct target_ip_mreqn
))
1303 return -TARGET_EINVAL
;
1305 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1306 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1307 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1310 case IP_BLOCK_SOURCE
:
1311 case IP_UNBLOCK_SOURCE
:
1312 case IP_ADD_SOURCE_MEMBERSHIP
:
1313 case IP_DROP_SOURCE_MEMBERSHIP
:
1314 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1315 return -TARGET_EINVAL
;
1317 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1318 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1319 unlock_user (ip_mreq_source
, optval_addr
, 0);
1328 case IPV6_MTU_DISCOVER
:
1331 case IPV6_RECVPKTINFO
:
1333 if (optlen
< sizeof(uint32_t)) {
1334 return -TARGET_EINVAL
;
1336 if (get_user_u32(val
, optval_addr
)) {
1337 return -TARGET_EFAULT
;
1339 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1340 &val
, sizeof(val
)));
1349 /* struct icmp_filter takes an u32 value */
1350 if (optlen
< sizeof(uint32_t)) {
1351 return -TARGET_EINVAL
;
1354 if (get_user_u32(val
, optval_addr
)) {
1355 return -TARGET_EFAULT
;
1357 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
1358 &val
, sizeof(val
)));
1365 case TARGET_SOL_SOCKET
:
1367 case TARGET_SO_RCVTIMEO
:
1371 optname
= SO_RCVTIMEO
;
1374 if (optlen
!= sizeof(struct target_timeval
)) {
1375 return -TARGET_EINVAL
;
1378 if (copy_from_user_timeval(&tv
, optval_addr
)) {
1379 return -TARGET_EFAULT
;
1382 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
1386 case TARGET_SO_SNDTIMEO
:
1387 optname
= SO_SNDTIMEO
;
1389 case TARGET_SO_ATTACH_FILTER
:
1391 struct target_sock_fprog
*tfprog
;
1392 struct target_sock_filter
*tfilter
;
1393 struct sock_fprog fprog
;
1394 struct sock_filter
*filter
;
1397 if (optlen
!= sizeof(*tfprog
)) {
1398 return -TARGET_EINVAL
;
1400 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
1401 return -TARGET_EFAULT
;
1403 if (!lock_user_struct(VERIFY_READ
, tfilter
,
1404 tswapal(tfprog
->filter
), 0)) {
1405 unlock_user_struct(tfprog
, optval_addr
, 1);
1406 return -TARGET_EFAULT
;
1409 fprog
.len
= tswap16(tfprog
->len
);
1410 filter
= malloc(fprog
.len
* sizeof(*filter
));
1411 if (filter
== NULL
) {
1412 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1413 unlock_user_struct(tfprog
, optval_addr
, 1);
1414 return -TARGET_ENOMEM
;
1416 for (i
= 0; i
< fprog
.len
; i
++) {
1417 filter
[i
].code
= tswap16(tfilter
[i
].code
);
1418 filter
[i
].jt
= tfilter
[i
].jt
;
1419 filter
[i
].jf
= tfilter
[i
].jf
;
1420 filter
[i
].k
= tswap32(tfilter
[i
].k
);
1422 fprog
.filter
= filter
;
1424 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
1425 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
1428 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
1429 unlock_user_struct(tfprog
, optval_addr
, 1);
1432 /* Options with 'int' argument. */
1433 case TARGET_SO_DEBUG
:
1436 case TARGET_SO_REUSEADDR
:
1437 optname
= SO_REUSEADDR
;
1439 case TARGET_SO_TYPE
:
1442 case TARGET_SO_ERROR
:
1445 case TARGET_SO_DONTROUTE
:
1446 optname
= SO_DONTROUTE
;
1448 case TARGET_SO_BROADCAST
:
1449 optname
= SO_BROADCAST
;
1451 case TARGET_SO_SNDBUF
:
1452 optname
= SO_SNDBUF
;
1454 case TARGET_SO_RCVBUF
:
1455 optname
= SO_RCVBUF
;
1457 case TARGET_SO_KEEPALIVE
:
1458 optname
= SO_KEEPALIVE
;
1460 case TARGET_SO_OOBINLINE
:
1461 optname
= SO_OOBINLINE
;
1463 case TARGET_SO_NO_CHECK
:
1464 optname
= SO_NO_CHECK
;
1466 case TARGET_SO_PRIORITY
:
1467 optname
= SO_PRIORITY
;
1470 case TARGET_SO_BSDCOMPAT
:
1471 optname
= SO_BSDCOMPAT
;
1474 case TARGET_SO_PASSCRED
:
1475 optname
= SO_PASSCRED
;
1477 case TARGET_SO_TIMESTAMP
:
1478 optname
= SO_TIMESTAMP
;
1480 case TARGET_SO_RCVLOWAT
:
1481 optname
= SO_RCVLOWAT
;
1487 if (optlen
< sizeof(uint32_t))
1488 return -TARGET_EINVAL
;
1490 if (get_user_u32(val
, optval_addr
))
1491 return -TARGET_EFAULT
;
1492 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
1496 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level
, optname
);
1497 ret
= -TARGET_ENOPROTOOPT
;
1502 /* do_getsockopt() Must return target values and target errnos. */
1503 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
1504 abi_ulong optval_addr
, abi_ulong optlen
)
1511 case TARGET_SOL_SOCKET
:
1514 /* These don't just return a single integer */
1515 case TARGET_SO_LINGER
:
1516 case TARGET_SO_RCVTIMEO
:
1517 case TARGET_SO_SNDTIMEO
:
1518 case TARGET_SO_PEERNAME
:
1520 case TARGET_SO_PEERCRED
: {
1523 struct target_ucred
*tcr
;
1525 if (get_user_u32(len
, optlen
)) {
1526 return -TARGET_EFAULT
;
1529 return -TARGET_EINVAL
;
1533 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
1541 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
1542 return -TARGET_EFAULT
;
1544 __put_user(cr
.pid
, &tcr
->pid
);
1545 __put_user(cr
.uid
, &tcr
->uid
);
1546 __put_user(cr
.gid
, &tcr
->gid
);
1547 unlock_user_struct(tcr
, optval_addr
, 1);
1548 if (put_user_u32(len
, optlen
)) {
1549 return -TARGET_EFAULT
;
1553 /* Options with 'int' argument. */
1554 case TARGET_SO_DEBUG
:
1557 case TARGET_SO_REUSEADDR
:
1558 optname
= SO_REUSEADDR
;
1560 case TARGET_SO_TYPE
:
1563 case TARGET_SO_ERROR
:
1566 case TARGET_SO_DONTROUTE
:
1567 optname
= SO_DONTROUTE
;
1569 case TARGET_SO_BROADCAST
:
1570 optname
= SO_BROADCAST
;
1572 case TARGET_SO_SNDBUF
:
1573 optname
= SO_SNDBUF
;
1575 case TARGET_SO_RCVBUF
:
1576 optname
= SO_RCVBUF
;
1578 case TARGET_SO_KEEPALIVE
:
1579 optname
= SO_KEEPALIVE
;
1581 case TARGET_SO_OOBINLINE
:
1582 optname
= SO_OOBINLINE
;
1584 case TARGET_SO_NO_CHECK
:
1585 optname
= SO_NO_CHECK
;
1587 case TARGET_SO_PRIORITY
:
1588 optname
= SO_PRIORITY
;
1591 case TARGET_SO_BSDCOMPAT
:
1592 optname
= SO_BSDCOMPAT
;
1595 case TARGET_SO_PASSCRED
:
1596 optname
= SO_PASSCRED
;
1598 case TARGET_SO_TIMESTAMP
:
1599 optname
= SO_TIMESTAMP
;
1601 case TARGET_SO_RCVLOWAT
:
1602 optname
= SO_RCVLOWAT
;
1609 /* TCP options all take an 'int' value. */
1611 if (get_user_u32(len
, optlen
))
1612 return -TARGET_EFAULT
;
1614 return -TARGET_EINVAL
;
1616 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1622 if (put_user_u32(val
, optval_addr
))
1623 return -TARGET_EFAULT
;
1625 if (put_user_u8(val
, optval_addr
))
1626 return -TARGET_EFAULT
;
1628 if (put_user_u32(len
, optlen
))
1629 return -TARGET_EFAULT
;
1636 case IP_ROUTER_ALERT
:
1640 case IP_MTU_DISCOVER
:
1646 case IP_MULTICAST_TTL
:
1647 case IP_MULTICAST_LOOP
:
1648 if (get_user_u32(len
, optlen
))
1649 return -TARGET_EFAULT
;
1651 return -TARGET_EINVAL
;
1653 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
1656 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
1658 if (put_user_u32(len
, optlen
)
1659 || put_user_u8(val
, optval_addr
))
1660 return -TARGET_EFAULT
;
1662 if (len
> sizeof(int))
1664 if (put_user_u32(len
, optlen
)
1665 || put_user_u32(val
, optval_addr
))
1666 return -TARGET_EFAULT
;
1670 ret
= -TARGET_ENOPROTOOPT
;
1676 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1678 ret
= -TARGET_EOPNOTSUPP
;
1684 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
1685 int count
, int copy
)
1687 struct target_iovec
*target_vec
;
1689 abi_ulong total_len
, max_len
;
1696 if (count
< 0 || count
> IOV_MAX
) {
1701 vec
= calloc(count
, sizeof(struct iovec
));
1707 target_vec
= lock_user(VERIFY_READ
, target_addr
,
1708 count
* sizeof(struct target_iovec
), 1);
1709 if (target_vec
== NULL
) {
1714 /* ??? If host page size > target page size, this will result in a
1715 value larger than what we can actually support. */
1716 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
1719 for (i
= 0; i
< count
; i
++) {
1720 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
1721 abi_long len
= tswapal(target_vec
[i
].iov_len
);
1726 } else if (len
== 0) {
1727 /* Zero length pointer is ignored. */
1728 vec
[i
].iov_base
= 0;
1730 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
1731 if (!vec
[i
].iov_base
) {
1735 if (len
> max_len
- total_len
) {
1736 len
= max_len
- total_len
;
1739 vec
[i
].iov_len
= len
;
1743 unlock_user(target_vec
, target_addr
, 0);
1749 unlock_user(target_vec
, target_addr
, 0);
1753 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
1754 int count
, int copy
)
1756 struct target_iovec
*target_vec
;
1759 target_vec
= lock_user(VERIFY_READ
, target_addr
,
1760 count
* sizeof(struct target_iovec
), 1);
1762 for (i
= 0; i
< count
; i
++) {
1763 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
1764 abi_long len
= tswapal(target_vec
[i
].iov_base
);
1768 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
1770 unlock_user(target_vec
, target_addr
, 0);
1776 static inline int target_to_host_sock_type(int *type
)
1779 int target_type
= *type
;
1781 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
1782 case TARGET_SOCK_DGRAM
:
1783 host_type
= SOCK_DGRAM
;
1785 case TARGET_SOCK_STREAM
:
1786 host_type
= SOCK_STREAM
;
1789 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
1792 if (target_type
& TARGET_SOCK_CLOEXEC
) {
1793 #if defined(SOCK_CLOEXEC)
1794 host_type
|= SOCK_CLOEXEC
;
1796 return -TARGET_EINVAL
;
1799 if (target_type
& TARGET_SOCK_NONBLOCK
) {
1800 #if defined(SOCK_NONBLOCK)
1801 host_type
|= SOCK_NONBLOCK
;
1802 #elif !defined(O_NONBLOCK)
1803 return -TARGET_EINVAL
;
1810 /* Try to emulate socket type flags after socket creation. */
1811 static int sock_flags_fixup(int fd
, int target_type
)
1813 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
1814 if (target_type
& TARGET_SOCK_NONBLOCK
) {
1815 int flags
= fcntl(fd
, F_GETFL
);
1816 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
1818 return -TARGET_EINVAL
;
1825 /* do_socket() Must return target values and target errnos. */
1826 static abi_long
do_socket(int domain
, int type
, int protocol
)
1828 int target_type
= type
;
1831 ret
= target_to_host_sock_type(&type
);
1836 if (domain
== PF_NETLINK
)
1837 return -EAFNOSUPPORT
; /* do not NETLINK socket connections possible */
1838 ret
= get_errno(socket(domain
, type
, protocol
));
1840 ret
= sock_flags_fixup(ret
, target_type
);
1845 /* do_bind() Must return target values and target errnos. */
1846 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
1852 if ((int)addrlen
< 0) {
1853 return -TARGET_EINVAL
;
1856 addr
= alloca(addrlen
+1);
1858 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
1862 return get_errno(bind(sockfd
, addr
, addrlen
));
1865 /* do_connect() Must return target values and target errnos. */
1866 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
1872 if ((int)addrlen
< 0) {
1873 return -TARGET_EINVAL
;
1876 addr
= alloca(addrlen
);
1878 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
1882 return get_errno(connect(sockfd
, addr
, addrlen
));
1885 /* do_sendrecvmsg() Must return target values and target errnos. */
1886 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
1887 int flags
, int send
)
1890 struct target_msghdr
*msgp
;
1894 abi_ulong target_vec
;
1897 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
1901 return -TARGET_EFAULT
;
1902 if (msgp
->msg_name
) {
1903 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
1904 msg
.msg_name
= alloca(msg
.msg_namelen
);
1905 ret
= target_to_host_sockaddr(msg
.msg_name
, tswapal(msgp
->msg_name
),
1911 msg
.msg_name
= NULL
;
1912 msg
.msg_namelen
= 0;
1914 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
1915 msg
.msg_control
= alloca(msg
.msg_controllen
);
1916 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
1918 count
= tswapal(msgp
->msg_iovlen
);
1919 target_vec
= tswapal(msgp
->msg_iov
);
1920 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
1921 target_vec
, count
, send
);
1923 ret
= -host_to_target_errno(errno
);
1926 msg
.msg_iovlen
= count
;
1930 ret
= target_to_host_cmsg(&msg
, msgp
);
1932 ret
= get_errno(sendmsg(fd
, &msg
, flags
));
1934 ret
= get_errno(recvmsg(fd
, &msg
, flags
));
1935 if (!is_error(ret
)) {
1937 ret
= host_to_target_cmsg(msgp
, &msg
);
1938 if (!is_error(ret
)) {
1939 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
1940 if (msg
.msg_name
!= NULL
) {
1941 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
1942 msg
.msg_name
, msg
.msg_namelen
);
1954 unlock_iovec(vec
, target_vec
, count
, !send
);
1956 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
1960 /* If we don't have a system accept4() then just call accept.
1961 * The callsites to do_accept4() will ensure that they don't
1962 * pass a non-zero flags argument in this config.
1964 #ifndef CONFIG_ACCEPT4
1965 static inline int accept4(int sockfd
, struct sockaddr
*addr
,
1966 socklen_t
*addrlen
, int flags
)
1969 return accept(sockfd
, addr
, addrlen
);
1973 /* do_accept4() Must return target values and target errnos. */
1974 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
1975 abi_ulong target_addrlen_addr
, int flags
)
1981 if (target_addr
== 0) {
1982 return get_errno(accept4(fd
, NULL
, NULL
, flags
));
1985 /* linux returns EINVAL if addrlen pointer is invalid */
1986 if (get_user_u32(addrlen
, target_addrlen_addr
))
1987 return -TARGET_EINVAL
;
1989 if ((int)addrlen
< 0) {
1990 return -TARGET_EINVAL
;
1993 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
1994 return -TARGET_EINVAL
;
1996 addr
= alloca(addrlen
);
1998 ret
= get_errno(accept4(fd
, addr
, &addrlen
, flags
));
1999 if (!is_error(ret
)) {
2000 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2001 if (put_user_u32(addrlen
, target_addrlen_addr
))
2002 ret
= -TARGET_EFAULT
;
2007 /* do_getpeername() Must return target values and target errnos. */
2008 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
2009 abi_ulong target_addrlen_addr
)
2015 if (get_user_u32(addrlen
, target_addrlen_addr
))
2016 return -TARGET_EFAULT
;
2018 if ((int)addrlen
< 0) {
2019 return -TARGET_EINVAL
;
2022 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2023 return -TARGET_EFAULT
;
2025 addr
= alloca(addrlen
);
2027 ret
= get_errno(getpeername(fd
, addr
, &addrlen
));
2028 if (!is_error(ret
)) {
2029 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2030 if (put_user_u32(addrlen
, target_addrlen_addr
))
2031 ret
= -TARGET_EFAULT
;
2036 /* do_getsockname() Must return target values and target errnos. */
2037 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
2038 abi_ulong target_addrlen_addr
)
2044 if (get_user_u32(addrlen
, target_addrlen_addr
))
2045 return -TARGET_EFAULT
;
2047 if ((int)addrlen
< 0) {
2048 return -TARGET_EINVAL
;
2051 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
2052 return -TARGET_EFAULT
;
2054 addr
= alloca(addrlen
);
2056 ret
= get_errno(getsockname(fd
, addr
, &addrlen
));
2057 if (!is_error(ret
)) {
2058 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2059 if (put_user_u32(addrlen
, target_addrlen_addr
))
2060 ret
= -TARGET_EFAULT
;
2065 /* do_socketpair() Must return target values and target errnos. */
2066 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
2067 abi_ulong target_tab_addr
)
2072 target_to_host_sock_type(&type
);
2074 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
2075 if (!is_error(ret
)) {
2076 if (put_user_s32(tab
[0], target_tab_addr
)
2077 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
2078 ret
= -TARGET_EFAULT
;
2083 /* do_sendto() Must return target values and target errnos. */
2084 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
2085 abi_ulong target_addr
, socklen_t addrlen
)
2091 if ((int)addrlen
< 0) {
2092 return -TARGET_EINVAL
;
2095 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
2097 return -TARGET_EFAULT
;
2099 addr
= alloca(addrlen
);
2100 ret
= target_to_host_sockaddr(addr
, target_addr
, addrlen
);
2102 unlock_user(host_msg
, msg
, 0);
2105 ret
= get_errno(sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
2107 ret
= get_errno(send(fd
, host_msg
, len
, flags
));
2109 unlock_user(host_msg
, msg
, 0);
2113 /* do_recvfrom() Must return target values and target errnos. */
2114 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
2115 abi_ulong target_addr
,
2116 abi_ulong target_addrlen
)
2123 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
2125 return -TARGET_EFAULT
;
2127 if (get_user_u32(addrlen
, target_addrlen
)) {
2128 ret
= -TARGET_EFAULT
;
2131 if ((int)addrlen
< 0) {
2132 ret
= -TARGET_EINVAL
;
2135 addr
= alloca(addrlen
);
2136 ret
= get_errno(recvfrom(fd
, host_msg
, len
, flags
, addr
, &addrlen
));
2138 addr
= NULL
; /* To keep compiler quiet. */
2139 ret
= get_errno(qemu_recv(fd
, host_msg
, len
, flags
));
2141 if (!is_error(ret
)) {
2143 host_to_target_sockaddr(target_addr
, addr
, addrlen
);
2144 if (put_user_u32(addrlen
, target_addrlen
)) {
2145 ret
= -TARGET_EFAULT
;
2149 unlock_user(host_msg
, msg
, len
);
2152 unlock_user(host_msg
, msg
, 0);
2157 #ifdef TARGET_NR_socketcall
2158 /* do_socketcall() Must return target values and target errnos. */
2159 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
2162 const int n
= sizeof(abi_ulong
);
2167 abi_ulong domain
, type
, protocol
;
2169 if (get_user_ual(domain
, vptr
)
2170 || get_user_ual(type
, vptr
+ n
)
2171 || get_user_ual(protocol
, vptr
+ 2 * n
))
2172 return -TARGET_EFAULT
;
2174 ret
= do_socket(domain
, type
, protocol
);
2180 abi_ulong target_addr
;
2183 if (get_user_ual(sockfd
, vptr
)
2184 || get_user_ual(target_addr
, vptr
+ n
)
2185 || get_user_ual(addrlen
, vptr
+ 2 * n
))
2186 return -TARGET_EFAULT
;
2188 ret
= do_bind(sockfd
, target_addr
, addrlen
);
2191 case SOCKOP_connect
:
2194 abi_ulong target_addr
;
2197 if (get_user_ual(sockfd
, vptr
)
2198 || get_user_ual(target_addr
, vptr
+ n
)
2199 || get_user_ual(addrlen
, vptr
+ 2 * n
))
2200 return -TARGET_EFAULT
;
2202 ret
= do_connect(sockfd
, target_addr
, addrlen
);
2207 abi_ulong sockfd
, backlog
;
2209 if (get_user_ual(sockfd
, vptr
)
2210 || get_user_ual(backlog
, vptr
+ n
))
2211 return -TARGET_EFAULT
;
2213 ret
= get_errno(listen(sockfd
, backlog
));
2219 abi_ulong target_addr
, target_addrlen
;
2221 if (get_user_ual(sockfd
, vptr
)
2222 || get_user_ual(target_addr
, vptr
+ n
)
2223 || get_user_ual(target_addrlen
, vptr
+ 2 * n
))
2224 return -TARGET_EFAULT
;
2226 ret
= do_accept4(sockfd
, target_addr
, target_addrlen
, 0);
2229 case SOCKOP_getsockname
:
2232 abi_ulong target_addr
, target_addrlen
;
2234 if (get_user_ual(sockfd
, vptr
)
2235 || get_user_ual(target_addr
, vptr
+ n
)
2236 || get_user_ual(target_addrlen
, vptr
+ 2 * n
))
2237 return -TARGET_EFAULT
;
2239 ret
= do_getsockname(sockfd
, target_addr
, target_addrlen
);
2242 case SOCKOP_getpeername
:
2245 abi_ulong target_addr
, target_addrlen
;
2247 if (get_user_ual(sockfd
, vptr
)
2248 || get_user_ual(target_addr
, vptr
+ n
)
2249 || get_user_ual(target_addrlen
, vptr
+ 2 * n
))
2250 return -TARGET_EFAULT
;
2252 ret
= do_getpeername(sockfd
, target_addr
, target_addrlen
);
2255 case SOCKOP_socketpair
:
2257 abi_ulong domain
, type
, protocol
;
2260 if (get_user_ual(domain
, vptr
)
2261 || get_user_ual(type
, vptr
+ n
)
2262 || get_user_ual(protocol
, vptr
+ 2 * n
)
2263 || get_user_ual(tab
, vptr
+ 3 * n
))
2264 return -TARGET_EFAULT
;
2266 ret
= do_socketpair(domain
, type
, protocol
, tab
);
2276 if (get_user_ual(sockfd
, vptr
)
2277 || get_user_ual(msg
, vptr
+ n
)
2278 || get_user_ual(len
, vptr
+ 2 * n
)
2279 || get_user_ual(flags
, vptr
+ 3 * n
))
2280 return -TARGET_EFAULT
;
2282 ret
= do_sendto(sockfd
, msg
, len
, flags
, 0, 0);
2292 if (get_user_ual(sockfd
, vptr
)
2293 || get_user_ual(msg
, vptr
+ n
)
2294 || get_user_ual(len
, vptr
+ 2 * n
)
2295 || get_user_ual(flags
, vptr
+ 3 * n
))
2296 return -TARGET_EFAULT
;
2298 ret
= do_recvfrom(sockfd
, msg
, len
, flags
, 0, 0);
2310 if (get_user_ual(sockfd
, vptr
)
2311 || get_user_ual(msg
, vptr
+ n
)
2312 || get_user_ual(len
, vptr
+ 2 * n
)
2313 || get_user_ual(flags
, vptr
+ 3 * n
)
2314 || get_user_ual(addr
, vptr
+ 4 * n
)
2315 || get_user_ual(addrlen
, vptr
+ 5 * n
))
2316 return -TARGET_EFAULT
;
2318 ret
= do_sendto(sockfd
, msg
, len
, flags
, addr
, addrlen
);
2321 case SOCKOP_recvfrom
:
2330 if (get_user_ual(sockfd
, vptr
)
2331 || get_user_ual(msg
, vptr
+ n
)
2332 || get_user_ual(len
, vptr
+ 2 * n
)
2333 || get_user_ual(flags
, vptr
+ 3 * n
)
2334 || get_user_ual(addr
, vptr
+ 4 * n
)
2335 || get_user_ual(addrlen
, vptr
+ 5 * n
))
2336 return -TARGET_EFAULT
;
2338 ret
= do_recvfrom(sockfd
, msg
, len
, flags
, addr
, addrlen
);
2341 case SOCKOP_shutdown
:
2343 abi_ulong sockfd
, how
;
2345 if (get_user_ual(sockfd
, vptr
)
2346 || get_user_ual(how
, vptr
+ n
))
2347 return -TARGET_EFAULT
;
2349 ret
= get_errno(shutdown(sockfd
, how
));
2352 case SOCKOP_sendmsg
:
2353 case SOCKOP_recvmsg
:
2356 abi_ulong target_msg
;
2359 if (get_user_ual(fd
, vptr
)
2360 || get_user_ual(target_msg
, vptr
+ n
)
2361 || get_user_ual(flags
, vptr
+ 2 * n
))
2362 return -TARGET_EFAULT
;
2364 ret
= do_sendrecvmsg(fd
, target_msg
, flags
,
2365 (num
== SOCKOP_sendmsg
));
2368 case SOCKOP_setsockopt
:
2376 if (get_user_ual(sockfd
, vptr
)
2377 || get_user_ual(level
, vptr
+ n
)
2378 || get_user_ual(optname
, vptr
+ 2 * n
)
2379 || get_user_ual(optval
, vptr
+ 3 * n
)
2380 || get_user_ual(optlen
, vptr
+ 4 * n
))
2381 return -TARGET_EFAULT
;
2383 ret
= do_setsockopt(sockfd
, level
, optname
, optval
, optlen
);
2386 case SOCKOP_getsockopt
:
2394 if (get_user_ual(sockfd
, vptr
)
2395 || get_user_ual(level
, vptr
+ n
)
2396 || get_user_ual(optname
, vptr
+ 2 * n
)
2397 || get_user_ual(optval
, vptr
+ 3 * n
)
2398 || get_user_ual(optlen
, vptr
+ 4 * n
))
2399 return -TARGET_EFAULT
;
2401 ret
= do_getsockopt(sockfd
, level
, optname
, optval
, optlen
);
2405 gemu_log("Unsupported socketcall: %d\n", num
);
2406 ret
= -TARGET_ENOSYS
;
2413 #define N_SHM_REGIONS 32
2415 static struct shm_region
{
2418 } shm_regions
[N_SHM_REGIONS
];
2420 struct target_ipc_perm
2427 unsigned short int mode
;
2428 unsigned short int __pad1
;
2429 unsigned short int __seq
;
2430 unsigned short int __pad2
;
2431 abi_ulong __unused1
;
2432 abi_ulong __unused2
;
2435 struct target_semid_ds
2437 struct target_ipc_perm sem_perm
;
2438 abi_ulong sem_otime
;
2439 abi_ulong __unused1
;
2440 abi_ulong sem_ctime
;
2441 abi_ulong __unused2
;
2442 abi_ulong sem_nsems
;
2443 abi_ulong __unused3
;
2444 abi_ulong __unused4
;
2447 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
2448 abi_ulong target_addr
)
2450 struct target_ipc_perm
*target_ip
;
2451 struct target_semid_ds
*target_sd
;
2453 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2454 return -TARGET_EFAULT
;
2455 target_ip
= &(target_sd
->sem_perm
);
2456 host_ip
->__key
= tswapal(target_ip
->__key
);
2457 host_ip
->uid
= tswapal(target_ip
->uid
);
2458 host_ip
->gid
= tswapal(target_ip
->gid
);
2459 host_ip
->cuid
= tswapal(target_ip
->cuid
);
2460 host_ip
->cgid
= tswapal(target_ip
->cgid
);
2461 host_ip
->mode
= tswap16(target_ip
->mode
);
2462 unlock_user_struct(target_sd
, target_addr
, 0);
2466 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
2467 struct ipc_perm
*host_ip
)
2469 struct target_ipc_perm
*target_ip
;
2470 struct target_semid_ds
*target_sd
;
2472 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2473 return -TARGET_EFAULT
;
2474 target_ip
= &(target_sd
->sem_perm
);
2475 target_ip
->__key
= tswapal(host_ip
->__key
);
2476 target_ip
->uid
= tswapal(host_ip
->uid
);
2477 target_ip
->gid
= tswapal(host_ip
->gid
);
2478 target_ip
->cuid
= tswapal(host_ip
->cuid
);
2479 target_ip
->cgid
= tswapal(host_ip
->cgid
);
2480 target_ip
->mode
= tswap16(host_ip
->mode
);
2481 unlock_user_struct(target_sd
, target_addr
, 1);
2485 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
2486 abi_ulong target_addr
)
2488 struct target_semid_ds
*target_sd
;
2490 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2491 return -TARGET_EFAULT
;
2492 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
2493 return -TARGET_EFAULT
;
2494 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
2495 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
2496 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
2497 unlock_user_struct(target_sd
, target_addr
, 0);
2501 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
2502 struct semid_ds
*host_sd
)
2504 struct target_semid_ds
*target_sd
;
2506 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2507 return -TARGET_EFAULT
;
2508 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
2509 return -TARGET_EFAULT
;
2510 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
2511 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
2512 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
2513 unlock_user_struct(target_sd
, target_addr
, 1);
2517 struct target_seminfo
{
2530 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
2531 struct seminfo
*host_seminfo
)
2533 struct target_seminfo
*target_seminfo
;
2534 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
2535 return -TARGET_EFAULT
;
2536 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
2537 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
2538 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
2539 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
2540 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
2541 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
2542 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
2543 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
2544 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
2545 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
2546 unlock_user_struct(target_seminfo
, target_addr
, 1);
2552 struct semid_ds
*buf
;
2553 unsigned short *array
;
2554 struct seminfo
*__buf
;
2557 union target_semun
{
2564 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
2565 abi_ulong target_addr
)
2568 unsigned short *array
;
2570 struct semid_ds semid_ds
;
2573 semun
.buf
= &semid_ds
;
2575 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2577 return get_errno(ret
);
2579 nsems
= semid_ds
.sem_nsems
;
2581 *host_array
= malloc(nsems
*sizeof(unsigned short));
2582 array
= lock_user(VERIFY_READ
, target_addr
,
2583 nsems
*sizeof(unsigned short), 1);
2585 return -TARGET_EFAULT
;
2587 for(i
=0; i
<nsems
; i
++) {
2588 __get_user((*host_array
)[i
], &array
[i
]);
2590 unlock_user(array
, target_addr
, 0);
2595 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
2596 unsigned short **host_array
)
2599 unsigned short *array
;
2601 struct semid_ds semid_ds
;
2604 semun
.buf
= &semid_ds
;
2606 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
2608 return get_errno(ret
);
2610 nsems
= semid_ds
.sem_nsems
;
2612 array
= lock_user(VERIFY_WRITE
, target_addr
,
2613 nsems
*sizeof(unsigned short), 0);
2615 return -TARGET_EFAULT
;
2617 for(i
=0; i
<nsems
; i
++) {
2618 __put_user((*host_array
)[i
], &array
[i
]);
2621 unlock_user(array
, target_addr
, 1);
2626 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
2627 union target_semun target_su
)
2630 struct semid_ds dsarg
;
2631 unsigned short *array
= NULL
;
2632 struct seminfo seminfo
;
2633 abi_long ret
= -TARGET_EINVAL
;
2640 arg
.val
= tswap32(target_su
.val
);
2641 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2642 target_su
.val
= tswap32(arg
.val
);
2646 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
2650 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2651 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
2658 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
2662 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2663 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
2669 arg
.__buf
= &seminfo
;
2670 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
2671 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
2679 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
2686 struct target_sembuf
{
2687 unsigned short sem_num
;
2692 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
2693 abi_ulong target_addr
,
2696 struct target_sembuf
*target_sembuf
;
2699 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
2700 nsops
*sizeof(struct target_sembuf
), 1);
2702 return -TARGET_EFAULT
;
2704 for(i
=0; i
<nsops
; i
++) {
2705 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
2706 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
2707 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
2710 unlock_user(target_sembuf
, target_addr
, 0);
2715 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
2717 struct sembuf sops
[nsops
];
2719 if (target_to_host_sembuf(sops
, ptr
, nsops
))
2720 return -TARGET_EFAULT
;
2722 return get_errno(semop(semid
, sops
, nsops
));
2725 struct target_msqid_ds
2727 struct target_ipc_perm msg_perm
;
2728 abi_ulong msg_stime
;
2729 #if TARGET_ABI_BITS == 32
2730 abi_ulong __unused1
;
2732 abi_ulong msg_rtime
;
2733 #if TARGET_ABI_BITS == 32
2734 abi_ulong __unused2
;
2736 abi_ulong msg_ctime
;
2737 #if TARGET_ABI_BITS == 32
2738 abi_ulong __unused3
;
2740 abi_ulong __msg_cbytes
;
2742 abi_ulong msg_qbytes
;
2743 abi_ulong msg_lspid
;
2744 abi_ulong msg_lrpid
;
2745 abi_ulong __unused4
;
2746 abi_ulong __unused5
;
2749 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
2750 abi_ulong target_addr
)
2752 struct target_msqid_ds
*target_md
;
2754 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
2755 return -TARGET_EFAULT
;
2756 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
2757 return -TARGET_EFAULT
;
2758 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
2759 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
2760 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
2761 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
2762 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
2763 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
2764 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
2765 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
2766 unlock_user_struct(target_md
, target_addr
, 0);
2770 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
2771 struct msqid_ds
*host_md
)
2773 struct target_msqid_ds
*target_md
;
2775 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
2776 return -TARGET_EFAULT
;
2777 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
2778 return -TARGET_EFAULT
;
2779 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
2780 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
2781 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
2782 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
2783 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
2784 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
2785 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
2786 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
2787 unlock_user_struct(target_md
, target_addr
, 1);
2791 struct target_msginfo
{
2799 unsigned short int msgseg
;
2802 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
2803 struct msginfo
*host_msginfo
)
2805 struct target_msginfo
*target_msginfo
;
2806 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
2807 return -TARGET_EFAULT
;
2808 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
2809 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
2810 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
2811 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
2812 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
2813 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
2814 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
2815 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
2816 unlock_user_struct(target_msginfo
, target_addr
, 1);
2820 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
2822 struct msqid_ds dsarg
;
2823 struct msginfo msginfo
;
2824 abi_long ret
= -TARGET_EINVAL
;
2832 if (target_to_host_msqid_ds(&dsarg
,ptr
))
2833 return -TARGET_EFAULT
;
2834 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
2835 if (host_to_target_msqid_ds(ptr
,&dsarg
))
2836 return -TARGET_EFAULT
;
2839 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
2843 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
2844 if (host_to_target_msginfo(ptr
, &msginfo
))
2845 return -TARGET_EFAULT
;
2852 struct target_msgbuf
{
2857 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
2858 unsigned int msgsz
, int msgflg
)
2860 struct target_msgbuf
*target_mb
;
2861 struct msgbuf
*host_mb
;
2864 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
2865 return -TARGET_EFAULT
;
2866 host_mb
= malloc(msgsz
+sizeof(long));
2867 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
2868 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
2869 ret
= get_errno(msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
2871 unlock_user_struct(target_mb
, msgp
, 0);
2876 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
2877 unsigned int msgsz
, abi_long msgtyp
,
2880 struct target_msgbuf
*target_mb
;
2882 struct msgbuf
*host_mb
;
2885 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
2886 return -TARGET_EFAULT
;
2888 host_mb
= g_malloc(msgsz
+sizeof(long));
2889 ret
= get_errno(msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
2892 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
2893 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
2894 if (!target_mtext
) {
2895 ret
= -TARGET_EFAULT
;
2898 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
2899 unlock_user(target_mtext
, target_mtext_addr
, ret
);
2902 target_mb
->mtype
= tswapal(host_mb
->mtype
);
2906 unlock_user_struct(target_mb
, msgp
, 1);
2911 struct target_shmid_ds
2913 struct target_ipc_perm shm_perm
;
2914 abi_ulong shm_segsz
;
2915 abi_ulong shm_atime
;
2916 #if TARGET_ABI_BITS == 32
2917 abi_ulong __unused1
;
2919 abi_ulong shm_dtime
;
2920 #if TARGET_ABI_BITS == 32
2921 abi_ulong __unused2
;
2923 abi_ulong shm_ctime
;
2924 #if TARGET_ABI_BITS == 32
2925 abi_ulong __unused3
;
2929 abi_ulong shm_nattch
;
2930 unsigned long int __unused4
;
2931 unsigned long int __unused5
;
2934 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
2935 abi_ulong target_addr
)
2937 struct target_shmid_ds
*target_sd
;
2939 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
2940 return -TARGET_EFAULT
;
2941 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
2942 return -TARGET_EFAULT
;
2943 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
2944 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
2945 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
2946 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
2947 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
2948 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
2949 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
2950 unlock_user_struct(target_sd
, target_addr
, 0);
2954 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
2955 struct shmid_ds
*host_sd
)
2957 struct target_shmid_ds
*target_sd
;
2959 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
2960 return -TARGET_EFAULT
;
2961 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
2962 return -TARGET_EFAULT
;
2963 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
2964 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
2965 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
2966 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
2967 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
2968 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
2969 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
2970 unlock_user_struct(target_sd
, target_addr
, 1);
2974 struct target_shminfo
{
2982 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
2983 struct shminfo
*host_shminfo
)
2985 struct target_shminfo
*target_shminfo
;
2986 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
2987 return -TARGET_EFAULT
;
2988 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
2989 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
2990 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
2991 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
2992 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
2993 unlock_user_struct(target_shminfo
, target_addr
, 1);
2997 struct target_shm_info
{
3002 abi_ulong swap_attempts
;
3003 abi_ulong swap_successes
;
3006 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
3007 struct shm_info
*host_shm_info
)
3009 struct target_shm_info
*target_shm_info
;
3010 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
3011 return -TARGET_EFAULT
;
3012 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
3013 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
3014 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
3015 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
3016 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
3017 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
3018 unlock_user_struct(target_shm_info
, target_addr
, 1);
3022 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
3024 struct shmid_ds dsarg
;
3025 struct shminfo shminfo
;
3026 struct shm_info shm_info
;
3027 abi_long ret
= -TARGET_EINVAL
;
3035 if (target_to_host_shmid_ds(&dsarg
, buf
))
3036 return -TARGET_EFAULT
;
3037 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
3038 if (host_to_target_shmid_ds(buf
, &dsarg
))
3039 return -TARGET_EFAULT
;
3042 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
3043 if (host_to_target_shminfo(buf
, &shminfo
))
3044 return -TARGET_EFAULT
;
3047 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
3048 if (host_to_target_shm_info(buf
, &shm_info
))
3049 return -TARGET_EFAULT
;
3054 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
3061 static inline abi_ulong
do_shmat(int shmid
, abi_ulong shmaddr
, int shmflg
)
3065 struct shmid_ds shm_info
;
3068 /* find out the length of the shared memory segment */
3069 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
3070 if (is_error(ret
)) {
3071 /* can't get length, bail out */
3078 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
3080 abi_ulong mmap_start
;
3082 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
);
3084 if (mmap_start
== -1) {
3086 host_raddr
= (void *)-1;
3088 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
3091 if (host_raddr
== (void *)-1) {
3093 return get_errno((long)host_raddr
);
3095 raddr
=h2g((unsigned long)host_raddr
);
3097 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
3098 PAGE_VALID
| PAGE_READ
|
3099 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
3101 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
3102 if (shm_regions
[i
].start
== 0) {
3103 shm_regions
[i
].start
= raddr
;
3104 shm_regions
[i
].size
= shm_info
.shm_segsz
;
3114 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
3118 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
3119 if (shm_regions
[i
].start
== shmaddr
) {
3120 shm_regions
[i
].start
= 0;
3121 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
3126 return get_errno(shmdt(g2h(shmaddr
)));
3129 #ifdef TARGET_NR_ipc
3130 /* ??? This only works with linear mappings. */
3131 /* do_ipc() must return target values and target errnos. */
3132 static abi_long
do_ipc(unsigned int call
, int first
,
3133 int second
, int third
,
3134 abi_long ptr
, abi_long fifth
)
3139 version
= call
>> 16;
3144 ret
= do_semop(first
, ptr
, second
);
3148 ret
= get_errno(semget(first
, second
, third
));
3152 ret
= do_semctl(first
, second
, third
, (union target_semun
)(abi_ulong
) ptr
);
3156 ret
= get_errno(msgget(first
, second
));
3160 ret
= do_msgsnd(first
, ptr
, second
, third
);
3164 ret
= do_msgctl(first
, second
, ptr
);
3171 struct target_ipc_kludge
{
3176 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
3177 ret
= -TARGET_EFAULT
;
3181 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
3183 unlock_user_struct(tmp
, ptr
, 0);
3187 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
3196 raddr
= do_shmat(first
, ptr
, second
);
3197 if (is_error(raddr
))
3198 return get_errno(raddr
);
3199 if (put_user_ual(raddr
, third
))
3200 return -TARGET_EFAULT
;
3204 ret
= -TARGET_EINVAL
;
3209 ret
= do_shmdt(ptr
);
3213 /* IPC_* flag values are the same on all linux platforms */
3214 ret
= get_errno(shmget(first
, second
, third
));
3217 /* IPC_* and SHM_* command values are the same on all linux platforms */
3219 ret
= do_shmctl(first
, second
, third
);
3222 gemu_log("Unsupported ipc call: %d (version %d)\n", call
, version
);
3223 ret
= -TARGET_ENOSYS
;
3230 /* kernel structure types definitions */
3232 #define STRUCT(name, ...) STRUCT_ ## name,
3233 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
3235 #include "syscall_types.h"
3238 #undef STRUCT_SPECIAL
3240 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
3241 #define STRUCT_SPECIAL(name)
3242 #include "syscall_types.h"
3244 #undef STRUCT_SPECIAL
3246 typedef struct IOCTLEntry IOCTLEntry
;
3248 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3249 int fd
, abi_long cmd
, abi_long arg
);
3252 unsigned int target_cmd
;
3253 unsigned int host_cmd
;
3256 do_ioctl_fn
*do_ioctl
;
3257 const argtype arg_type
[5];
3260 #define IOC_R 0x0001
3261 #define IOC_W 0x0002
3262 #define IOC_RW (IOC_R | IOC_W)
3264 #define MAX_STRUCT_SIZE 4096
3266 #ifdef CONFIG_FIEMAP
3267 /* So fiemap access checks don't overflow on 32 bit systems.
3268 * This is very slightly smaller than the limit imposed by
3269 * the underlying kernel.
3271 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
3272 / sizeof(struct fiemap_extent))
3274 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3275 int fd
, abi_long cmd
, abi_long arg
)
3277 /* The parameter for this ioctl is a struct fiemap followed
3278 * by an array of struct fiemap_extent whose size is set
3279 * in fiemap->fm_extent_count. The array is filled in by the
3282 int target_size_in
, target_size_out
;
3284 const argtype
*arg_type
= ie
->arg_type
;
3285 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
3288 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
3292 assert(arg_type
[0] == TYPE_PTR
);
3293 assert(ie
->access
== IOC_RW
);
3295 target_size_in
= thunk_type_size(arg_type
, 0);
3296 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
3298 return -TARGET_EFAULT
;
3300 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3301 unlock_user(argptr
, arg
, 0);
3302 fm
= (struct fiemap
*)buf_temp
;
3303 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
3304 return -TARGET_EINVAL
;
3307 outbufsz
= sizeof (*fm
) +
3308 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
3310 if (outbufsz
> MAX_STRUCT_SIZE
) {
3311 /* We can't fit all the extents into the fixed size buffer.
3312 * Allocate one that is large enough and use it instead.
3314 fm
= malloc(outbufsz
);
3316 return -TARGET_ENOMEM
;
3318 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
3321 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, fm
));
3322 if (!is_error(ret
)) {
3323 target_size_out
= target_size_in
;
3324 /* An extent_count of 0 means we were only counting the extents
3325 * so there are no structs to copy
3327 if (fm
->fm_extent_count
!= 0) {
3328 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
3330 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
3332 ret
= -TARGET_EFAULT
;
3334 /* Convert the struct fiemap */
3335 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
3336 if (fm
->fm_extent_count
!= 0) {
3337 p
= argptr
+ target_size_in
;
3338 /* ...and then all the struct fiemap_extents */
3339 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
3340 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
3345 unlock_user(argptr
, arg
, target_size_out
);
3355 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3356 int fd
, abi_long cmd
, abi_long arg
)
3358 const argtype
*arg_type
= ie
->arg_type
;
3362 struct ifconf
*host_ifconf
;
3364 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
3365 int target_ifreq_size
;
3370 abi_long target_ifc_buf
;
3374 assert(arg_type
[0] == TYPE_PTR
);
3375 assert(ie
->access
== IOC_RW
);
3378 target_size
= thunk_type_size(arg_type
, 0);
3380 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3382 return -TARGET_EFAULT
;
3383 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3384 unlock_user(argptr
, arg
, 0);
3386 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
3387 target_ifc_len
= host_ifconf
->ifc_len
;
3388 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
3390 target_ifreq_size
= thunk_type_size(ifreq_arg_type
, 0);
3391 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
3392 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
3394 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
3395 if (outbufsz
> MAX_STRUCT_SIZE
) {
3396 /* We can't fit all the extents into the fixed size buffer.
3397 * Allocate one that is large enough and use it instead.
3399 host_ifconf
= malloc(outbufsz
);
3401 return -TARGET_ENOMEM
;
3403 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
3406 host_ifc_buf
= (char*)host_ifconf
+ sizeof(*host_ifconf
);
3408 host_ifconf
->ifc_len
= host_ifc_len
;
3409 host_ifconf
->ifc_buf
= host_ifc_buf
;
3411 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, host_ifconf
));
3412 if (!is_error(ret
)) {
3413 /* convert host ifc_len to target ifc_len */
3415 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
3416 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
3417 host_ifconf
->ifc_len
= target_ifc_len
;
3419 /* restore target ifc_buf */
3421 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
3423 /* copy struct ifconf to target user */
3425 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3427 return -TARGET_EFAULT
;
3428 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
3429 unlock_user(argptr
, arg
, target_size
);
3431 /* copy ifreq[] to target user */
3433 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
3434 for (i
= 0; i
< nb_ifreq
; i
++) {
3435 thunk_convert(argptr
+ i
* target_ifreq_size
,
3436 host_ifc_buf
+ i
* sizeof(struct ifreq
),
3437 ifreq_arg_type
, THUNK_TARGET
);
3439 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
3449 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
3450 abi_long cmd
, abi_long arg
)
3453 struct dm_ioctl
*host_dm
;
3454 abi_long guest_data
;
3455 uint32_t guest_data_size
;
3457 const argtype
*arg_type
= ie
->arg_type
;
3459 void *big_buf
= NULL
;
3463 target_size
= thunk_type_size(arg_type
, 0);
3464 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3466 ret
= -TARGET_EFAULT
;
3469 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3470 unlock_user(argptr
, arg
, 0);
3472 /* buf_temp is too small, so fetch things into a bigger buffer */
3473 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
3474 memcpy(big_buf
, buf_temp
, target_size
);
3478 guest_data
= arg
+ host_dm
->data_start
;
3479 if ((guest_data
- arg
) < 0) {
3483 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
3484 host_data
= (char*)host_dm
+ host_dm
->data_start
;
3486 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
3487 switch (ie
->host_cmd
) {
3489 case DM_LIST_DEVICES
:
3492 case DM_DEV_SUSPEND
:
3495 case DM_TABLE_STATUS
:
3496 case DM_TABLE_CLEAR
:
3498 case DM_LIST_VERSIONS
:
3502 case DM_DEV_SET_GEOMETRY
:
3503 /* data contains only strings */
3504 memcpy(host_data
, argptr
, guest_data_size
);
3507 memcpy(host_data
, argptr
, guest_data_size
);
3508 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
3512 void *gspec
= argptr
;
3513 void *cur_data
= host_data
;
3514 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
3515 int spec_size
= thunk_type_size(arg_type
, 0);
3518 for (i
= 0; i
< host_dm
->target_count
; i
++) {
3519 struct dm_target_spec
*spec
= cur_data
;
3523 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
3524 slen
= strlen((char*)gspec
+ spec_size
) + 1;
3526 spec
->next
= sizeof(*spec
) + slen
;
3527 strcpy((char*)&spec
[1], gspec
+ spec_size
);
3529 cur_data
+= spec
->next
;
3534 ret
= -TARGET_EINVAL
;
3537 unlock_user(argptr
, guest_data
, 0);
3539 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3540 if (!is_error(ret
)) {
3541 guest_data
= arg
+ host_dm
->data_start
;
3542 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
3543 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
3544 switch (ie
->host_cmd
) {
3549 case DM_DEV_SUSPEND
:
3552 case DM_TABLE_CLEAR
:
3554 case DM_DEV_SET_GEOMETRY
:
3555 /* no return data */
3557 case DM_LIST_DEVICES
:
3559 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
3560 uint32_t remaining_data
= guest_data_size
;
3561 void *cur_data
= argptr
;
3562 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
3563 int nl_size
= 12; /* can't use thunk_size due to alignment */
3566 uint32_t next
= nl
->next
;
3568 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
3570 if (remaining_data
< nl
->next
) {
3571 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3574 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
3575 strcpy(cur_data
+ nl_size
, nl
->name
);
3576 cur_data
+= nl
->next
;
3577 remaining_data
-= nl
->next
;
3581 nl
= (void*)nl
+ next
;
3586 case DM_TABLE_STATUS
:
3588 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
3589 void *cur_data
= argptr
;
3590 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
3591 int spec_size
= thunk_type_size(arg_type
, 0);
3594 for (i
= 0; i
< host_dm
->target_count
; i
++) {
3595 uint32_t next
= spec
->next
;
3596 int slen
= strlen((char*)&spec
[1]) + 1;
3597 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
3598 if (guest_data_size
< spec
->next
) {
3599 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3602 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
3603 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
3604 cur_data
= argptr
+ spec
->next
;
3605 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
3611 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
3612 int count
= *(uint32_t*)hdata
;
3613 uint64_t *hdev
= hdata
+ 8;
3614 uint64_t *gdev
= argptr
+ 8;
3617 *(uint32_t*)argptr
= tswap32(count
);
3618 for (i
= 0; i
< count
; i
++) {
3619 *gdev
= tswap64(*hdev
);
3625 case DM_LIST_VERSIONS
:
3627 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
3628 uint32_t remaining_data
= guest_data_size
;
3629 void *cur_data
= argptr
;
3630 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
3631 int vers_size
= thunk_type_size(arg_type
, 0);
3634 uint32_t next
= vers
->next
;
3636 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
3638 if (remaining_data
< vers
->next
) {
3639 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
3642 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
3643 strcpy(cur_data
+ vers_size
, vers
->name
);
3644 cur_data
+= vers
->next
;
3645 remaining_data
-= vers
->next
;
3649 vers
= (void*)vers
+ next
;
3654 ret
= -TARGET_EINVAL
;
3657 unlock_user(argptr
, guest_data
, guest_data_size
);
3659 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3661 ret
= -TARGET_EFAULT
;
3664 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3665 unlock_user(argptr
, arg
, target_size
);
3672 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
3673 int fd
, abi_long cmd
, abi_long arg
)
3675 const argtype
*arg_type
= ie
->arg_type
;
3676 const StructEntry
*se
;
3677 const argtype
*field_types
;
3678 const int *dst_offsets
, *src_offsets
;
3681 abi_ulong
*target_rt_dev_ptr
;
3682 unsigned long *host_rt_dev_ptr
;
3686 assert(ie
->access
== IOC_W
);
3687 assert(*arg_type
== TYPE_PTR
);
3689 assert(*arg_type
== TYPE_STRUCT
);
3690 target_size
= thunk_type_size(arg_type
, 0);
3691 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3693 return -TARGET_EFAULT
;
3696 assert(*arg_type
== (int)STRUCT_rtentry
);
3697 se
= struct_entries
+ *arg_type
++;
3698 assert(se
->convert
[0] == NULL
);
3699 /* convert struct here to be able to catch rt_dev string */
3700 field_types
= se
->field_types
;
3701 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
3702 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
3703 for (i
= 0; i
< se
->nb_fields
; i
++) {
3704 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
3705 assert(*field_types
== TYPE_PTRVOID
);
3706 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
3707 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
3708 if (*target_rt_dev_ptr
!= 0) {
3709 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
3710 tswapal(*target_rt_dev_ptr
));
3711 if (!*host_rt_dev_ptr
) {
3712 unlock_user(argptr
, arg
, 0);
3713 return -TARGET_EFAULT
;
3716 *host_rt_dev_ptr
= 0;
3721 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
3722 argptr
+ src_offsets
[i
],
3723 field_types
, THUNK_HOST
);
3725 unlock_user(argptr
, arg
, 0);
3727 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3728 if (*host_rt_dev_ptr
!= 0) {
3729 unlock_user((void *)*host_rt_dev_ptr
,
3730 *target_rt_dev_ptr
, 0);
3735 static IOCTLEntry ioctl_entries
[] = {
3736 #define IOCTL(cmd, access, ...) \
3737 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
3738 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
3739 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
3744 /* ??? Implement proper locking for ioctls. */
3745 /* do_ioctl() Must return target values and target errnos. */
3746 static abi_long
do_ioctl(int fd
, abi_long cmd
, abi_long arg
)
3748 const IOCTLEntry
*ie
;
3749 const argtype
*arg_type
;
3751 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
3757 if (ie
->target_cmd
== 0) {
3758 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
3759 return -TARGET_ENOSYS
;
3761 if (ie
->target_cmd
== cmd
)
3765 arg_type
= ie
->arg_type
;
3767 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd
, ie
->name
);
3770 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
3773 switch(arg_type
[0]) {
3776 ret
= get_errno(ioctl(fd
, ie
->host_cmd
));
3781 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, arg
));
3785 target_size
= thunk_type_size(arg_type
, 0);
3786 switch(ie
->access
) {
3788 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3789 if (!is_error(ret
)) {
3790 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3792 return -TARGET_EFAULT
;
3793 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3794 unlock_user(argptr
, arg
, target_size
);
3798 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3800 return -TARGET_EFAULT
;
3801 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3802 unlock_user(argptr
, arg
, 0);
3803 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3807 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
3809 return -TARGET_EFAULT
;
3810 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
3811 unlock_user(argptr
, arg
, 0);
3812 ret
= get_errno(ioctl(fd
, ie
->host_cmd
, buf_temp
));
3813 if (!is_error(ret
)) {
3814 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
3816 return -TARGET_EFAULT
;
3817 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
3818 unlock_user(argptr
, arg
, target_size
);
3824 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3825 (long)cmd
, arg_type
[0]);
3826 ret
= -TARGET_ENOSYS
;
3832 static const bitmask_transtbl iflag_tbl
[] = {
3833 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
3834 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
3835 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
3836 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
3837 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
3838 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
3839 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
3840 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
3841 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
3842 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
3843 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
3844 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
3845 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
3846 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
3850 static const bitmask_transtbl oflag_tbl
[] = {
3851 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
3852 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
3853 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
3854 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
3855 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
3856 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
3857 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
3858 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
3859 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
3860 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
3861 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
3862 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
3863 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
3864 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
3865 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
3866 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
3867 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
3868 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
3869 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
3870 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
3871 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
3872 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
3873 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
3874 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
3878 static const bitmask_transtbl cflag_tbl
[] = {
3879 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
3880 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
3881 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
3882 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
3883 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
3884 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
3885 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
3886 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
3887 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
3888 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
3889 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
3890 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
3891 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
3892 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
3893 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
3894 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
3895 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
3896 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
3897 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
3898 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
3899 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
3900 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
3901 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
3902 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
3903 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
3904 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
3905 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
3906 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
3907 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
3908 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
3909 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
3913 static const bitmask_transtbl lflag_tbl
[] = {
3914 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
3915 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
3916 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
3917 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
3918 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
3919 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
3920 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
3921 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
3922 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
3923 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
3924 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
3925 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
3926 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
3927 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
3928 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
3932 static void target_to_host_termios (void *dst
, const void *src
)
3934 struct host_termios
*host
= dst
;
3935 const struct target_termios
*target
= src
;
3938 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
3940 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
3942 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
3944 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
3945 host
->c_line
= target
->c_line
;
3947 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
3948 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
3949 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
3950 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
3951 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
3952 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
3953 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
3954 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
3955 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
3956 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
3957 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
3958 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
3959 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
3960 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
3961 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
3962 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
3963 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
3964 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
3967 static void host_to_target_termios (void *dst
, const void *src
)
3969 struct target_termios
*target
= dst
;
3970 const struct host_termios
*host
= src
;
3973 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
3975 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
3977 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
3979 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
3980 target
->c_line
= host
->c_line
;
3982 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
3983 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
3984 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
3985 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
3986 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
3987 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
3988 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
3989 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
3990 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
3991 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
3992 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
3993 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
3994 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
3995 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
3996 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
3997 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
3998 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
3999 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
4002 static const StructEntry struct_termios_def
= {
4003 .convert
= { host_to_target_termios
, target_to_host_termios
},
4004 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
4005 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
4008 static bitmask_transtbl mmap_flags_tbl
[] = {
4009 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
4010 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
4011 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
4012 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
, MAP_ANONYMOUS
, MAP_ANONYMOUS
},
4013 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
, MAP_GROWSDOWN
, MAP_GROWSDOWN
},
4014 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
, MAP_DENYWRITE
, MAP_DENYWRITE
},
4015 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
, MAP_EXECUTABLE
, MAP_EXECUTABLE
},
4016 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
4020 #if defined(TARGET_I386)
4022 /* NOTE: there is really one LDT for all the threads */
4023 static uint8_t *ldt_table
;
4025 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
4032 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
4033 if (size
> bytecount
)
4035 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
4037 return -TARGET_EFAULT
;
4038 /* ??? Should this by byteswapped? */
4039 memcpy(p
, ldt_table
, size
);
4040 unlock_user(p
, ptr
, size
);
4044 /* XXX: add locking support */
4045 static abi_long
write_ldt(CPUX86State
*env
,
4046 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
4048 struct target_modify_ldt_ldt_s ldt_info
;
4049 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4050 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
4051 int seg_not_present
, useable
, lm
;
4052 uint32_t *lp
, entry_1
, entry_2
;
4054 if (bytecount
!= sizeof(ldt_info
))
4055 return -TARGET_EINVAL
;
4056 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
4057 return -TARGET_EFAULT
;
4058 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
4059 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
4060 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
4061 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
4062 unlock_user_struct(target_ldt_info
, ptr
, 0);
4064 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
4065 return -TARGET_EINVAL
;
4066 seg_32bit
= ldt_info
.flags
& 1;
4067 contents
= (ldt_info
.flags
>> 1) & 3;
4068 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
4069 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
4070 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
4071 useable
= (ldt_info
.flags
>> 6) & 1;
4075 lm
= (ldt_info
.flags
>> 7) & 1;
4077 if (contents
== 3) {
4079 return -TARGET_EINVAL
;
4080 if (seg_not_present
== 0)
4081 return -TARGET_EINVAL
;
4083 /* allocate the LDT */
4085 env
->ldt
.base
= target_mmap(0,
4086 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
4087 PROT_READ
|PROT_WRITE
,
4088 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
4089 if (env
->ldt
.base
== -1)
4090 return -TARGET_ENOMEM
;
4091 memset(g2h(env
->ldt
.base
), 0,
4092 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
4093 env
->ldt
.limit
= 0xffff;
4094 ldt_table
= g2h(env
->ldt
.base
);
4097 /* NOTE: same code as Linux kernel */
4098 /* Allow LDTs to be cleared by the user. */
4099 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
4102 read_exec_only
== 1 &&
4104 limit_in_pages
== 0 &&
4105 seg_not_present
== 1 &&
4113 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
4114 (ldt_info
.limit
& 0x0ffff);
4115 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
4116 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
4117 (ldt_info
.limit
& 0xf0000) |
4118 ((read_exec_only
^ 1) << 9) |
4120 ((seg_not_present
^ 1) << 15) |
4122 (limit_in_pages
<< 23) |
4126 entry_2
|= (useable
<< 20);
4128 /* Install the new entry ... */
4130 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
4131 lp
[0] = tswap32(entry_1
);
4132 lp
[1] = tswap32(entry_2
);
4136 /* specific and weird i386 syscalls */
4137 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
4138 unsigned long bytecount
)
4144 ret
= read_ldt(ptr
, bytecount
);
4147 ret
= write_ldt(env
, ptr
, bytecount
, 1);
4150 ret
= write_ldt(env
, ptr
, bytecount
, 0);
4153 ret
= -TARGET_ENOSYS
;
4159 #if defined(TARGET_I386) && defined(TARGET_ABI32)
4160 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
4162 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
4163 struct target_modify_ldt_ldt_s ldt_info
;
4164 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4165 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
4166 int seg_not_present
, useable
, lm
;
4167 uint32_t *lp
, entry_1
, entry_2
;
4170 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
4171 if (!target_ldt_info
)
4172 return -TARGET_EFAULT
;
4173 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
4174 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
4175 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
4176 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
4177 if (ldt_info
.entry_number
== -1) {
4178 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
4179 if (gdt_table
[i
] == 0) {
4180 ldt_info
.entry_number
= i
;
4181 target_ldt_info
->entry_number
= tswap32(i
);
4186 unlock_user_struct(target_ldt_info
, ptr
, 1);
4188 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
4189 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
4190 return -TARGET_EINVAL
;
4191 seg_32bit
= ldt_info
.flags
& 1;
4192 contents
= (ldt_info
.flags
>> 1) & 3;
4193 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
4194 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
4195 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
4196 useable
= (ldt_info
.flags
>> 6) & 1;
4200 lm
= (ldt_info
.flags
>> 7) & 1;
4203 if (contents
== 3) {
4204 if (seg_not_present
== 0)
4205 return -TARGET_EINVAL
;
4208 /* NOTE: same code as Linux kernel */
4209 /* Allow LDTs to be cleared by the user. */
4210 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
4211 if ((contents
== 0 &&
4212 read_exec_only
== 1 &&
4214 limit_in_pages
== 0 &&
4215 seg_not_present
== 1 &&
4223 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
4224 (ldt_info
.limit
& 0x0ffff);
4225 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
4226 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
4227 (ldt_info
.limit
& 0xf0000) |
4228 ((read_exec_only
^ 1) << 9) |
4230 ((seg_not_present
^ 1) << 15) |
4232 (limit_in_pages
<< 23) |
4237 /* Install the new entry ... */
4239 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
4240 lp
[0] = tswap32(entry_1
);
4241 lp
[1] = tswap32(entry_2
);
4245 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
4247 struct target_modify_ldt_ldt_s
*target_ldt_info
;
4248 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
4249 uint32_t base_addr
, limit
, flags
;
4250 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
4251 int seg_not_present
, useable
, lm
;
4252 uint32_t *lp
, entry_1
, entry_2
;
4254 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
4255 if (!target_ldt_info
)
4256 return -TARGET_EFAULT
;
4257 idx
= tswap32(target_ldt_info
->entry_number
);
4258 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
4259 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
4260 unlock_user_struct(target_ldt_info
, ptr
, 1);
4261 return -TARGET_EINVAL
;
4263 lp
= (uint32_t *)(gdt_table
+ idx
);
4264 entry_1
= tswap32(lp
[0]);
4265 entry_2
= tswap32(lp
[1]);
4267 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
4268 contents
= (entry_2
>> 10) & 3;
4269 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
4270 seg_32bit
= (entry_2
>> 22) & 1;
4271 limit_in_pages
= (entry_2
>> 23) & 1;
4272 useable
= (entry_2
>> 20) & 1;
4276 lm
= (entry_2
>> 21) & 1;
4278 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
4279 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
4280 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
4281 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
4282 base_addr
= (entry_1
>> 16) |
4283 (entry_2
& 0xff000000) |
4284 ((entry_2
& 0xff) << 16);
4285 target_ldt_info
->base_addr
= tswapal(base_addr
);
4286 target_ldt_info
->limit
= tswap32(limit
);
4287 target_ldt_info
->flags
= tswap32(flags
);
4288 unlock_user_struct(target_ldt_info
, ptr
, 1);
4291 #endif /* TARGET_I386 && TARGET_ABI32 */
4293 #ifndef TARGET_ABI32
4294 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
4301 case TARGET_ARCH_SET_GS
:
4302 case TARGET_ARCH_SET_FS
:
4303 if (code
== TARGET_ARCH_SET_GS
)
4307 cpu_x86_load_seg(env
, idx
, 0);
4308 env
->segs
[idx
].base
= addr
;
4310 case TARGET_ARCH_GET_GS
:
4311 case TARGET_ARCH_GET_FS
:
4312 if (code
== TARGET_ARCH_GET_GS
)
4316 val
= env
->segs
[idx
].base
;
4317 if (put_user(val
, addr
, abi_ulong
))
4318 ret
= -TARGET_EFAULT
;
4321 ret
= -TARGET_EINVAL
;
4328 #endif /* defined(TARGET_I386) */
4330 #define NEW_STACK_SIZE 0x40000
4333 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
4336 pthread_mutex_t mutex
;
4337 pthread_cond_t cond
;
4340 abi_ulong child_tidptr
;
4341 abi_ulong parent_tidptr
;
4345 static void *clone_func(void *arg
)
4347 new_thread_info
*info
= arg
;
4353 cpu
= ENV_GET_CPU(env
);
4355 ts
= (TaskState
*)env
->opaque
;
4356 info
->tid
= gettid();
4357 cpu
->host_tid
= info
->tid
;
4359 if (info
->child_tidptr
)
4360 put_user_u32(info
->tid
, info
->child_tidptr
);
4361 if (info
->parent_tidptr
)
4362 put_user_u32(info
->tid
, info
->parent_tidptr
);
4363 /* Enable signals. */
4364 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
4365 /* Signal to the parent that we're ready. */
4366 pthread_mutex_lock(&info
->mutex
);
4367 pthread_cond_broadcast(&info
->cond
);
4368 pthread_mutex_unlock(&info
->mutex
);
4369 /* Wait until the parent has finshed initializing the tls state. */
4370 pthread_mutex_lock(&clone_lock
);
4371 pthread_mutex_unlock(&clone_lock
);
4377 /* do_fork() Must return host values and target errnos (unlike most
4378 do_*() functions). */
4379 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
4380 abi_ulong parent_tidptr
, target_ulong newtls
,
4381 abi_ulong child_tidptr
)
4385 CPUArchState
*new_env
;
4386 unsigned int nptl_flags
;
4389 /* Emulate vfork() with fork() */
4390 if (flags
& CLONE_VFORK
)
4391 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
4393 if (flags
& CLONE_VM
) {
4394 TaskState
*parent_ts
= (TaskState
*)env
->opaque
;
4395 new_thread_info info
;
4396 pthread_attr_t attr
;
4398 ts
= g_malloc0(sizeof(TaskState
));
4399 init_task_state(ts
);
4400 /* we create a new CPU instance. */
4401 new_env
= cpu_copy(env
);
4402 /* Init regs that differ from the parent. */
4403 cpu_clone_regs(new_env
, newsp
);
4404 new_env
->opaque
= ts
;
4405 ts
->bprm
= parent_ts
->bprm
;
4406 ts
->info
= parent_ts
->info
;
4408 flags
&= ~CLONE_NPTL_FLAGS2
;
4410 if (nptl_flags
& CLONE_CHILD_CLEARTID
) {
4411 ts
->child_tidptr
= child_tidptr
;
4414 if (nptl_flags
& CLONE_SETTLS
)
4415 cpu_set_tls (new_env
, newtls
);
4417 /* Grab a mutex so that thread setup appears atomic. */
4418 pthread_mutex_lock(&clone_lock
);
4420 memset(&info
, 0, sizeof(info
));
4421 pthread_mutex_init(&info
.mutex
, NULL
);
4422 pthread_mutex_lock(&info
.mutex
);
4423 pthread_cond_init(&info
.cond
, NULL
);
4425 if (nptl_flags
& CLONE_CHILD_SETTID
)
4426 info
.child_tidptr
= child_tidptr
;
4427 if (nptl_flags
& CLONE_PARENT_SETTID
)
4428 info
.parent_tidptr
= parent_tidptr
;
4430 ret
= pthread_attr_init(&attr
);
4431 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
4432 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
4433 /* It is not safe to deliver signals until the child has finished
4434 initializing, so temporarily block all signals. */
4435 sigfillset(&sigmask
);
4436 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
4438 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
4439 /* TODO: Free new CPU state if thread creation failed. */
4441 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
4442 pthread_attr_destroy(&attr
);
4444 /* Wait for the child to initialize. */
4445 pthread_cond_wait(&info
.cond
, &info
.mutex
);
4447 if (flags
& CLONE_PARENT_SETTID
)
4448 put_user_u32(ret
, parent_tidptr
);
4452 pthread_mutex_unlock(&info
.mutex
);
4453 pthread_cond_destroy(&info
.cond
);
4454 pthread_mutex_destroy(&info
.mutex
);
4455 pthread_mutex_unlock(&clone_lock
);
4457 /* if no CLONE_VM, we consider it is a fork */
4458 if ((flags
& ~(CSIGNAL
| CLONE_NPTL_FLAGS2
)) != 0)
4463 /* Child Process. */
4464 cpu_clone_regs(env
, newsp
);
4466 /* There is a race condition here. The parent process could
4467 theoretically read the TID in the child process before the child
4468 tid is set. This would require using either ptrace
4469 (not implemented) or having *_tidptr to point at a shared memory
4470 mapping. We can't repeat the spinlock hack used above because
4471 the child process gets its own copy of the lock. */
4472 if (flags
& CLONE_CHILD_SETTID
)
4473 put_user_u32(gettid(), child_tidptr
);
4474 if (flags
& CLONE_PARENT_SETTID
)
4475 put_user_u32(gettid(), parent_tidptr
);
4476 ts
= (TaskState
*)env
->opaque
;
4477 if (flags
& CLONE_SETTLS
)
4478 cpu_set_tls (env
, newtls
);
4479 if (flags
& CLONE_CHILD_CLEARTID
)
4480 ts
->child_tidptr
= child_tidptr
;
4488 /* warning : doesn't handle linux specific flags... */
4489 static int target_to_host_fcntl_cmd(int cmd
)
4492 case TARGET_F_DUPFD
:
4493 case TARGET_F_GETFD
:
4494 case TARGET_F_SETFD
:
4495 case TARGET_F_GETFL
:
4496 case TARGET_F_SETFL
:
4498 case TARGET_F_GETLK
:
4500 case TARGET_F_SETLK
:
4502 case TARGET_F_SETLKW
:
4504 case TARGET_F_GETOWN
:
4506 case TARGET_F_SETOWN
:
4508 case TARGET_F_GETSIG
:
4510 case TARGET_F_SETSIG
:
4512 #if TARGET_ABI_BITS == 32
4513 case TARGET_F_GETLK64
:
4515 case TARGET_F_SETLK64
:
4517 case TARGET_F_SETLKW64
:
4520 case TARGET_F_SETLEASE
:
4522 case TARGET_F_GETLEASE
:
4524 #ifdef F_DUPFD_CLOEXEC
4525 case TARGET_F_DUPFD_CLOEXEC
:
4526 return F_DUPFD_CLOEXEC
;
4528 case TARGET_F_NOTIFY
:
4531 return -TARGET_EINVAL
;
4533 return -TARGET_EINVAL
;
4536 #define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
4537 static const bitmask_transtbl flock_tbl
[] = {
4538 TRANSTBL_CONVERT(F_RDLCK
),
4539 TRANSTBL_CONVERT(F_WRLCK
),
4540 TRANSTBL_CONVERT(F_UNLCK
),
4541 TRANSTBL_CONVERT(F_EXLCK
),
4542 TRANSTBL_CONVERT(F_SHLCK
),
4546 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
4549 struct target_flock
*target_fl
;
4550 struct flock64 fl64
;
4551 struct target_flock64
*target_fl64
;
4553 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
4555 if (host_cmd
== -TARGET_EINVAL
)
4559 case TARGET_F_GETLK
:
4560 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
4561 return -TARGET_EFAULT
;
4563 target_to_host_bitmask(tswap16(target_fl
->l_type
), flock_tbl
);
4564 fl
.l_whence
= tswap16(target_fl
->l_whence
);
4565 fl
.l_start
= tswapal(target_fl
->l_start
);
4566 fl
.l_len
= tswapal(target_fl
->l_len
);
4567 fl
.l_pid
= tswap32(target_fl
->l_pid
);
4568 unlock_user_struct(target_fl
, arg
, 0);
4569 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
4571 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg
, 0))
4572 return -TARGET_EFAULT
;
4574 host_to_target_bitmask(tswap16(fl
.l_type
), flock_tbl
);
4575 target_fl
->l_whence
= tswap16(fl
.l_whence
);
4576 target_fl
->l_start
= tswapal(fl
.l_start
);
4577 target_fl
->l_len
= tswapal(fl
.l_len
);
4578 target_fl
->l_pid
= tswap32(fl
.l_pid
);
4579 unlock_user_struct(target_fl
, arg
, 1);
4583 case TARGET_F_SETLK
:
4584 case TARGET_F_SETLKW
:
4585 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg
, 1))
4586 return -TARGET_EFAULT
;
4588 target_to_host_bitmask(tswap16(target_fl
->l_type
), flock_tbl
);
4589 fl
.l_whence
= tswap16(target_fl
->l_whence
);
4590 fl
.l_start
= tswapal(target_fl
->l_start
);
4591 fl
.l_len
= tswapal(target_fl
->l_len
);
4592 fl
.l_pid
= tswap32(target_fl
->l_pid
);
4593 unlock_user_struct(target_fl
, arg
, 0);
4594 ret
= get_errno(fcntl(fd
, host_cmd
, &fl
));
4597 case TARGET_F_GETLK64
:
4598 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
4599 return -TARGET_EFAULT
;
4601 target_to_host_bitmask(tswap16(target_fl64
->l_type
), flock_tbl
) >> 1;
4602 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
4603 fl64
.l_start
= tswap64(target_fl64
->l_start
);
4604 fl64
.l_len
= tswap64(target_fl64
->l_len
);
4605 fl64
.l_pid
= tswap32(target_fl64
->l_pid
);
4606 unlock_user_struct(target_fl64
, arg
, 0);
4607 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
4609 if (!lock_user_struct(VERIFY_WRITE
, target_fl64
, arg
, 0))
4610 return -TARGET_EFAULT
;
4611 target_fl64
->l_type
=
4612 host_to_target_bitmask(tswap16(fl64
.l_type
), flock_tbl
) >> 1;
4613 target_fl64
->l_whence
= tswap16(fl64
.l_whence
);
4614 target_fl64
->l_start
= tswap64(fl64
.l_start
);
4615 target_fl64
->l_len
= tswap64(fl64
.l_len
);
4616 target_fl64
->l_pid
= tswap32(fl64
.l_pid
);
4617 unlock_user_struct(target_fl64
, arg
, 1);
4620 case TARGET_F_SETLK64
:
4621 case TARGET_F_SETLKW64
:
4622 if (!lock_user_struct(VERIFY_READ
, target_fl64
, arg
, 1))
4623 return -TARGET_EFAULT
;
4625 target_to_host_bitmask(tswap16(target_fl64
->l_type
), flock_tbl
) >> 1;
4626 fl64
.l_whence
= tswap16(target_fl64
->l_whence
);
4627 fl64
.l_start
= tswap64(target_fl64
->l_start
);
4628 fl64
.l_len
= tswap64(target_fl64
->l_len
);
4629 fl64
.l_pid
= tswap32(target_fl64
->l_pid
);
4630 unlock_user_struct(target_fl64
, arg
, 0);
4631 ret
= get_errno(fcntl(fd
, host_cmd
, &fl64
));
4634 case TARGET_F_GETFL
:
4635 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
4637 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
4641 case TARGET_F_SETFL
:
4642 ret
= get_errno(fcntl(fd
, host_cmd
, target_to_host_bitmask(arg
, fcntl_flags_tbl
)));
4645 case TARGET_F_SETOWN
:
4646 case TARGET_F_GETOWN
:
4647 case TARGET_F_SETSIG
:
4648 case TARGET_F_GETSIG
:
4649 case TARGET_F_SETLEASE
:
4650 case TARGET_F_GETLEASE
:
4651 ret
= get_errno(fcntl(fd
, host_cmd
, arg
));
4655 ret
= get_errno(fcntl(fd
, cmd
, arg
));
4663 static inline int high2lowuid(int uid
)
4671 static inline int high2lowgid(int gid
)
4679 static inline int low2highuid(int uid
)
4681 if ((int16_t)uid
== -1)
4687 static inline int low2highgid(int gid
)
4689 if ((int16_t)gid
== -1)
4694 static inline int tswapid(int id
)
4698 #else /* !USE_UID16 */
4699 static inline int high2lowuid(int uid
)
4703 static inline int high2lowgid(int gid
)
4707 static inline int low2highuid(int uid
)
4711 static inline int low2highgid(int gid
)
4715 static inline int tswapid(int id
)
4719 #endif /* USE_UID16 */
4721 void syscall_init(void)
4724 const argtype
*arg_type
;
4728 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
4729 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
4730 #include "syscall_types.h"
4732 #undef STRUCT_SPECIAL
4734 /* Build target_to_host_errno_table[] table from
4735 * host_to_target_errno_table[]. */
4736 for (i
= 0; i
< ERRNO_TABLE_SIZE
; i
++) {
4737 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
4740 /* we patch the ioctl size if necessary. We rely on the fact that
4741 no ioctl has all the bits at '1' in the size field */
4743 while (ie
->target_cmd
!= 0) {
4744 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
4745 TARGET_IOC_SIZEMASK
) {
4746 arg_type
= ie
->arg_type
;
4747 if (arg_type
[0] != TYPE_PTR
) {
4748 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
4753 size
= thunk_type_size(arg_type
, 0);
4754 ie
->target_cmd
= (ie
->target_cmd
&
4755 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
4756 (size
<< TARGET_IOC_SIZESHIFT
);
4759 /* automatic consistency check if same arch */
4760 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4761 (defined(__x86_64__) && defined(TARGET_X86_64))
4762 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
4763 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4764 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
4771 #if TARGET_ABI_BITS == 32
4772 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
4774 #ifdef TARGET_WORDS_BIGENDIAN
4775 return ((uint64_t)word0
<< 32) | word1
;
4777 return ((uint64_t)word1
<< 32) | word0
;
4780 #else /* TARGET_ABI_BITS == 32 */
4781 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
4785 #endif /* TARGET_ABI_BITS != 32 */
4787 #ifdef TARGET_NR_truncate64
4788 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
4793 if (regpairs_aligned(cpu_env
)) {
4797 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
4801 #ifdef TARGET_NR_ftruncate64
4802 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
4807 if (regpairs_aligned(cpu_env
)) {
4811 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
4815 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
4816 abi_ulong target_addr
)
4818 struct target_timespec
*target_ts
;
4820 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1))
4821 return -TARGET_EFAULT
;
4822 host_ts
->tv_sec
= tswapal(target_ts
->tv_sec
);
4823 host_ts
->tv_nsec
= tswapal(target_ts
->tv_nsec
);
4824 unlock_user_struct(target_ts
, target_addr
, 0);
4828 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
4829 struct timespec
*host_ts
)
4831 struct target_timespec
*target_ts
;
4833 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0))
4834 return -TARGET_EFAULT
;
4835 target_ts
->tv_sec
= tswapal(host_ts
->tv_sec
);
4836 target_ts
->tv_nsec
= tswapal(host_ts
->tv_nsec
);
4837 unlock_user_struct(target_ts
, target_addr
, 1);
4841 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
4842 static inline abi_long
host_to_target_stat64(void *cpu_env
,
4843 abi_ulong target_addr
,
4844 struct stat
*host_st
)
4846 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
4847 if (((CPUARMState
*)cpu_env
)->eabi
) {
4848 struct target_eabi_stat64
*target_st
;
4850 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
4851 return -TARGET_EFAULT
;
4852 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
4853 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
4854 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
4855 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4856 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
4858 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
4859 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
4860 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
4861 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
4862 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
4863 __put_user(host_st
->st_size
, &target_st
->st_size
);
4864 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
4865 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
4866 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
4867 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
4868 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
4869 unlock_user_struct(target_st
, target_addr
, 1);
4873 #if defined(TARGET_HAS_STRUCT_STAT64)
4874 struct target_stat64
*target_st
;
4876 struct target_stat
*target_st
;
4879 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
4880 return -TARGET_EFAULT
;
4881 memset(target_st
, 0, sizeof(*target_st
));
4882 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
4883 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
4884 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4885 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
4887 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
4888 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
4889 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
4890 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
4891 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
4892 /* XXX: better use of kernel struct */
4893 __put_user(host_st
->st_size
, &target_st
->st_size
);
4894 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
4895 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
4896 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
4897 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
4898 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
4899 unlock_user_struct(target_st
, target_addr
, 1);
4906 /* ??? Using host futex calls even when target atomic operations
4907 are not really atomic probably breaks things. However implementing
4908 futexes locally would make futexes shared between multiple processes
4909 tricky. However they're probably useless because guest atomic
4910 operations won't work either. */
4911 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
4912 target_ulong uaddr2
, int val3
)
4914 struct timespec ts
, *pts
;
4917 /* ??? We assume FUTEX_* constants are the same on both host
4919 #ifdef FUTEX_CMD_MASK
4920 base_op
= op
& FUTEX_CMD_MASK
;
4926 case FUTEX_WAIT_BITSET
:
4929 target_to_host_timespec(pts
, timeout
);
4933 return get_errno(sys_futex(g2h(uaddr
), op
, tswap32(val
),
4936 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
4938 return get_errno(sys_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
4940 case FUTEX_CMP_REQUEUE
:
4942 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4943 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4944 But the prototype takes a `struct timespec *'; insert casts
4945 to satisfy the compiler. We do not need to tswap TIMEOUT
4946 since it's not compared to guest memory. */
4947 pts
= (struct timespec
*)(uintptr_t) timeout
;
4948 return get_errno(sys_futex(g2h(uaddr
), op
, val
, pts
,
4950 (base_op
== FUTEX_CMP_REQUEUE
4954 return -TARGET_ENOSYS
;
4958 /* Map host to target signal numbers for the wait family of syscalls.
4959 Assume all other status bits are the same. */
4960 int host_to_target_waitstatus(int status
)
4962 if (WIFSIGNALED(status
)) {
4963 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
4965 if (WIFSTOPPED(status
)) {
4966 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
4972 static int relstr_to_int(const char *s
)
4974 /* Convert a uname release string like "2.6.18" to an integer
4975 * of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
4980 for (i
= 0; i
< 3; i
++) {
4982 while (*s
>= '0' && *s
<= '9') {
4987 tmp
= (tmp
<< 8) + n
;
4995 int get_osversion(void)
4997 static int osversion
;
4998 struct new_utsname buf
;
5003 if (qemu_uname_release
&& *qemu_uname_release
) {
5004 s
= qemu_uname_release
;
5006 if (sys_uname(&buf
))
5010 osversion
= relstr_to_int(s
);
5014 void init_qemu_uname_release(void)
5016 /* Initialize qemu_uname_release for later use.
5017 * If the host kernel is too old and the user hasn't asked for
5018 * a specific fake version number, we might want to fake a minimum
5019 * target kernel version.
5021 #ifdef UNAME_MINIMUM_RELEASE
5022 struct new_utsname buf
;
5024 if (qemu_uname_release
&& *qemu_uname_release
) {
5028 if (sys_uname(&buf
)) {
5032 if (relstr_to_int(buf
.release
) < relstr_to_int(UNAME_MINIMUM_RELEASE
)) {
5033 qemu_uname_release
= UNAME_MINIMUM_RELEASE
;
5038 static int open_self_maps(void *cpu_env
, int fd
)
5040 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
5041 TaskState
*ts
= ((CPUArchState
*)cpu_env
)->opaque
;
5048 fp
= fopen("/proc/self/maps", "r");
5053 while ((read
= getline(&line
, &len
, fp
)) != -1) {
5054 int fields
, dev_maj
, dev_min
, inode
;
5055 uint64_t min
, max
, offset
;
5056 char flag_r
, flag_w
, flag_x
, flag_p
;
5057 char path
[512] = "";
5058 fields
= sscanf(line
, "%"PRIx64
"-%"PRIx64
" %c%c%c%c %"PRIx64
" %x:%x %d"
5059 " %512s", &min
, &max
, &flag_r
, &flag_w
, &flag_x
,
5060 &flag_p
, &offset
, &dev_maj
, &dev_min
, &inode
, path
);
5062 if ((fields
< 10) || (fields
> 11)) {
5065 if (!strncmp(path
, "[stack]", 7)) {
5068 if (h2g_valid(min
) && h2g_valid(max
)) {
5069 dprintf(fd
, TARGET_ABI_FMT_lx
"-" TARGET_ABI_FMT_lx
5070 " %c%c%c%c %08" PRIx64
" %02x:%02x %d %s%s\n",
5071 h2g(min
), h2g(max
), flag_r
, flag_w
,
5072 flag_x
, flag_p
, offset
, dev_maj
, dev_min
, inode
,
5073 path
[0] ? " " : "", path
);
5080 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
5081 dprintf(fd
, "%08llx-%08llx rw-p %08llx 00:00 0 [stack]\n",
5082 (unsigned long long)ts
->info
->stack_limit
,
5083 (unsigned long long)(ts
->info
->start_stack
+
5084 (TARGET_PAGE_SIZE
- 1)) & TARGET_PAGE_MASK
,
5085 (unsigned long long)0);
5091 static int open_self_stat(void *cpu_env
, int fd
)
5093 TaskState
*ts
= ((CPUArchState
*)cpu_env
)->opaque
;
5094 abi_ulong start_stack
= ts
->info
->start_stack
;
5097 for (i
= 0; i
< 44; i
++) {
5105 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
5106 } else if (i
== 1) {
5108 snprintf(buf
, sizeof(buf
), "(%s) ", ts
->bprm
->argv
[0]);
5109 } else if (i
== 27) {
5112 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
5114 /* for the rest, there is MasterCard */
5115 snprintf(buf
, sizeof(buf
), "0%c", i
== 43 ? '\n' : ' ');
5119 if (write(fd
, buf
, len
) != len
) {
5127 static int open_self_auxv(void *cpu_env
, int fd
)
5129 TaskState
*ts
= ((CPUArchState
*)cpu_env
)->opaque
;
5130 abi_ulong auxv
= ts
->info
->saved_auxv
;
5131 abi_ulong len
= ts
->info
->auxv_len
;
5135 * Auxiliary vector is stored in target process stack.
5136 * read in whole auxv vector and copy it to file
5138 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
5142 r
= write(fd
, ptr
, len
);
5149 lseek(fd
, 0, SEEK_SET
);
5150 unlock_user(ptr
, auxv
, len
);
5156 static int is_proc_myself(const char *filename
, const char *entry
)
5158 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
5159 filename
+= strlen("/proc/");
5160 if (!strncmp(filename
, "self/", strlen("self/"))) {
5161 filename
+= strlen("self/");
5162 } else if (*filename
>= '1' && *filename
<= '9') {
5164 snprintf(myself
, sizeof(myself
), "%d/", getpid());
5165 if (!strncmp(filename
, myself
, strlen(myself
))) {
5166 filename
+= strlen(myself
);
5173 if (!strcmp(filename
, entry
)) {
5180 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5181 static int is_proc(const char *filename
, const char *entry
)
5183 return strcmp(filename
, entry
) == 0;
5186 static int open_net_route(void *cpu_env
, int fd
)
5193 fp
= fopen("/proc/net/route", "r");
5200 read
= getline(&line
, &len
, fp
);
5201 dprintf(fd
, "%s", line
);
5205 while ((read
= getline(&line
, &len
, fp
)) != -1) {
5207 uint32_t dest
, gw
, mask
;
5208 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
5209 sscanf(line
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5210 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
5211 &mask
, &mtu
, &window
, &irtt
);
5212 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
5213 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
5214 metric
, tswap32(mask
), mtu
, window
, irtt
);
5224 static int do_open(void *cpu_env
, const char *pathname
, int flags
, mode_t mode
)
5227 const char *filename
;
5228 int (*fill
)(void *cpu_env
, int fd
);
5229 int (*cmp
)(const char *s1
, const char *s2
);
5231 const struct fake_open
*fake_open
;
5232 static const struct fake_open fakes
[] = {
5233 { "maps", open_self_maps
, is_proc_myself
},
5234 { "stat", open_self_stat
, is_proc_myself
},
5235 { "auxv", open_self_auxv
, is_proc_myself
},
5236 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
5237 { "/proc/net/route", open_net_route
, is_proc
},
5239 { NULL
, NULL
, NULL
}
5242 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
5243 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
5248 if (fake_open
->filename
) {
5250 char filename
[PATH_MAX
];
5253 /* create temporary file to map stat to */
5254 tmpdir
= getenv("TMPDIR");
5257 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
5258 fd
= mkstemp(filename
);
5264 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
5268 lseek(fd
, 0, SEEK_SET
);
5273 return get_errno(open(path(pathname
), flags
, mode
));
5276 /* do_syscall() should always have a single exit point at the end so
5277 that actions, such as logging of syscall results, can be performed.
5278 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
5279 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
5280 abi_long arg2
, abi_long arg3
, abi_long arg4
,
5281 abi_long arg5
, abi_long arg6
, abi_long arg7
,
5284 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
5291 gemu_log("syscall %d", num
);
5294 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
5297 case TARGET_NR_exit
:
5298 /* In old applications this may be used to implement _exit(2).
5299 However in threaded applictions it is used for thread termination,
5300 and _exit_group is used for application termination.
5301 Do thread termination if we have more then one thread. */
5302 /* FIXME: This probably breaks if a signal arrives. We should probably
5303 be disabling signals. */
5304 if (CPU_NEXT(first_cpu
)) {
5308 /* Remove the CPU from the list. */
5309 QTAILQ_REMOVE(&cpus
, cpu
, node
);
5311 ts
= ((CPUArchState
*)cpu_env
)->opaque
;
5312 if (ts
->child_tidptr
) {
5313 put_user_u32(0, ts
->child_tidptr
);
5314 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
5318 object_unref(OBJECT(ENV_GET_CPU(cpu_env
)));
5325 gdb_exit(cpu_env
, arg1
);
5327 ret
= 0; /* avoid warning */
5329 case TARGET_NR_read
:
5333 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
5335 ret
= get_errno(read(arg1
, p
, arg3
));
5336 unlock_user(p
, arg2
, ret
);
5339 case TARGET_NR_write
:
5340 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
5342 ret
= get_errno(write(arg1
, p
, arg3
));
5343 unlock_user(p
, arg2
, 0);
5345 case TARGET_NR_open
:
5346 if (!(p
= lock_user_string(arg1
)))
5348 ret
= get_errno(do_open(cpu_env
, p
,
5349 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
5351 unlock_user(p
, arg1
, 0);
5353 #if defined(TARGET_NR_openat) && defined(__NR_openat)
5354 case TARGET_NR_openat
:
5355 if (!(p
= lock_user_string(arg2
)))
5357 ret
= get_errno(sys_openat(arg1
,
5359 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
5361 unlock_user(p
, arg2
, 0);
5364 case TARGET_NR_close
:
5365 ret
= get_errno(close(arg1
));
5370 case TARGET_NR_fork
:
5371 ret
= get_errno(do_fork(cpu_env
, SIGCHLD
, 0, 0, 0, 0));
5373 #ifdef TARGET_NR_waitpid
5374 case TARGET_NR_waitpid
:
5377 ret
= get_errno(waitpid(arg1
, &status
, arg3
));
5378 if (!is_error(ret
) && arg2
&& ret
5379 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
5384 #ifdef TARGET_NR_waitid
5385 case TARGET_NR_waitid
:
5389 ret
= get_errno(waitid(arg1
, arg2
, &info
, arg4
));
5390 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
5391 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
5393 host_to_target_siginfo(p
, &info
);
5394 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
5399 #ifdef TARGET_NR_creat /* not on alpha */
5400 case TARGET_NR_creat
:
5401 if (!(p
= lock_user_string(arg1
)))
5403 ret
= get_errno(creat(p
, arg2
));
5404 unlock_user(p
, arg1
, 0);
5407 case TARGET_NR_link
:
5410 p
= lock_user_string(arg1
);
5411 p2
= lock_user_string(arg2
);
5413 ret
= -TARGET_EFAULT
;
5415 ret
= get_errno(link(p
, p2
));
5416 unlock_user(p2
, arg2
, 0);
5417 unlock_user(p
, arg1
, 0);
5420 #if defined(TARGET_NR_linkat)
5421 case TARGET_NR_linkat
:
5426 p
= lock_user_string(arg2
);
5427 p2
= lock_user_string(arg4
);
5429 ret
= -TARGET_EFAULT
;
5431 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
5432 unlock_user(p
, arg2
, 0);
5433 unlock_user(p2
, arg4
, 0);
5437 case TARGET_NR_unlink
:
5438 if (!(p
= lock_user_string(arg1
)))
5440 ret
= get_errno(unlink(p
));
5441 unlock_user(p
, arg1
, 0);
5443 #if defined(TARGET_NR_unlinkat)
5444 case TARGET_NR_unlinkat
:
5445 if (!(p
= lock_user_string(arg2
)))
5447 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
5448 unlock_user(p
, arg2
, 0);
5451 case TARGET_NR_execve
:
5453 char **argp
, **envp
;
5456 abi_ulong guest_argp
;
5457 abi_ulong guest_envp
;
5464 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
5465 if (get_user_ual(addr
, gp
))
5473 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
5474 if (get_user_ual(addr
, gp
))
5481 argp
= alloca((argc
+ 1) * sizeof(void *));
5482 envp
= alloca((envc
+ 1) * sizeof(void *));
5484 for (gp
= guest_argp
, q
= argp
; gp
;
5485 gp
+= sizeof(abi_ulong
), q
++) {
5486 if (get_user_ual(addr
, gp
))
5490 if (!(*q
= lock_user_string(addr
)))
5492 total_size
+= strlen(*q
) + 1;
5496 for (gp
= guest_envp
, q
= envp
; gp
;
5497 gp
+= sizeof(abi_ulong
), q
++) {
5498 if (get_user_ual(addr
, gp
))
5502 if (!(*q
= lock_user_string(addr
)))
5504 total_size
+= strlen(*q
) + 1;
5508 /* This case will not be caught by the host's execve() if its
5509 page size is bigger than the target's. */
5510 if (total_size
> MAX_ARG_PAGES
* TARGET_PAGE_SIZE
) {
5511 ret
= -TARGET_E2BIG
;
5514 if (!(p
= lock_user_string(arg1
)))
5516 ret
= get_errno(execve(p
, argp
, envp
));
5517 unlock_user(p
, arg1
, 0);
5522 ret
= -TARGET_EFAULT
;
5525 for (gp
= guest_argp
, q
= argp
; *q
;
5526 gp
+= sizeof(abi_ulong
), q
++) {
5527 if (get_user_ual(addr
, gp
)
5530 unlock_user(*q
, addr
, 0);
5532 for (gp
= guest_envp
, q
= envp
; *q
;
5533 gp
+= sizeof(abi_ulong
), q
++) {
5534 if (get_user_ual(addr
, gp
)
5537 unlock_user(*q
, addr
, 0);
5541 case TARGET_NR_chdir
:
5542 if (!(p
= lock_user_string(arg1
)))
5544 ret
= get_errno(chdir(p
));
5545 unlock_user(p
, arg1
, 0);
5547 #ifdef TARGET_NR_time
5548 case TARGET_NR_time
:
5551 ret
= get_errno(time(&host_time
));
5554 && put_user_sal(host_time
, arg1
))
5559 case TARGET_NR_mknod
:
5560 if (!(p
= lock_user_string(arg1
)))
5562 ret
= get_errno(mknod(p
, arg2
, arg3
));
5563 unlock_user(p
, arg1
, 0);
5565 #if defined(TARGET_NR_mknodat)
5566 case TARGET_NR_mknodat
:
5567 if (!(p
= lock_user_string(arg2
)))
5569 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
5570 unlock_user(p
, arg2
, 0);
5573 case TARGET_NR_chmod
:
5574 if (!(p
= lock_user_string(arg1
)))
5576 ret
= get_errno(chmod(p
, arg2
));
5577 unlock_user(p
, arg1
, 0);
5579 #ifdef TARGET_NR_break
5580 case TARGET_NR_break
:
5583 #ifdef TARGET_NR_oldstat
5584 case TARGET_NR_oldstat
:
5587 case TARGET_NR_lseek
:
5588 ret
= get_errno(lseek(arg1
, arg2
, arg3
));
5590 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
5591 /* Alpha specific */
5592 case TARGET_NR_getxpid
:
5593 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = getppid();
5594 ret
= get_errno(getpid());
5597 #ifdef TARGET_NR_getpid
5598 case TARGET_NR_getpid
:
5599 ret
= get_errno(getpid());
5602 case TARGET_NR_mount
:
5604 /* need to look at the data field */
5606 p
= lock_user_string(arg1
);
5607 p2
= lock_user_string(arg2
);
5608 p3
= lock_user_string(arg3
);
5609 if (!p
|| !p2
|| !p3
)
5610 ret
= -TARGET_EFAULT
;
5612 /* FIXME - arg5 should be locked, but it isn't clear how to
5613 * do that since it's not guaranteed to be a NULL-terminated
5617 ret
= get_errno(mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
));
5619 ret
= get_errno(mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
)));
5621 unlock_user(p
, arg1
, 0);
5622 unlock_user(p2
, arg2
, 0);
5623 unlock_user(p3
, arg3
, 0);
5626 #ifdef TARGET_NR_umount
5627 case TARGET_NR_umount
:
5628 if (!(p
= lock_user_string(arg1
)))
5630 ret
= get_errno(umount(p
));
5631 unlock_user(p
, arg1
, 0);
5634 #ifdef TARGET_NR_stime /* not on alpha */
5635 case TARGET_NR_stime
:
5638 if (get_user_sal(host_time
, arg1
))
5640 ret
= get_errno(stime(&host_time
));
5644 case TARGET_NR_ptrace
:
5646 #ifdef TARGET_NR_alarm /* not on alpha */
5647 case TARGET_NR_alarm
:
5651 #ifdef TARGET_NR_oldfstat
5652 case TARGET_NR_oldfstat
:
5655 #ifdef TARGET_NR_pause /* not on alpha */
5656 case TARGET_NR_pause
:
5657 ret
= get_errno(pause());
5660 #ifdef TARGET_NR_utime
5661 case TARGET_NR_utime
:
5663 struct utimbuf tbuf
, *host_tbuf
;
5664 struct target_utimbuf
*target_tbuf
;
5666 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
5668 tbuf
.actime
= tswapal(target_tbuf
->actime
);
5669 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
5670 unlock_user_struct(target_tbuf
, arg2
, 0);
5675 if (!(p
= lock_user_string(arg1
)))
5677 ret
= get_errno(utime(p
, host_tbuf
));
5678 unlock_user(p
, arg1
, 0);
5682 case TARGET_NR_utimes
:
5684 struct timeval
*tvp
, tv
[2];
5686 if (copy_from_user_timeval(&tv
[0], arg2
)
5687 || copy_from_user_timeval(&tv
[1],
5688 arg2
+ sizeof(struct target_timeval
)))
5694 if (!(p
= lock_user_string(arg1
)))
5696 ret
= get_errno(utimes(p
, tvp
));
5697 unlock_user(p
, arg1
, 0);
5700 #if defined(TARGET_NR_futimesat)
5701 case TARGET_NR_futimesat
:
5703 struct timeval
*tvp
, tv
[2];
5705 if (copy_from_user_timeval(&tv
[0], arg3
)
5706 || copy_from_user_timeval(&tv
[1],
5707 arg3
+ sizeof(struct target_timeval
)))
5713 if (!(p
= lock_user_string(arg2
)))
5715 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
5716 unlock_user(p
, arg2
, 0);
5720 #ifdef TARGET_NR_stty
5721 case TARGET_NR_stty
:
5724 #ifdef TARGET_NR_gtty
5725 case TARGET_NR_gtty
:
5728 case TARGET_NR_access
:
5729 if (!(p
= lock_user_string(arg1
)))
5731 ret
= get_errno(access(path(p
), arg2
));
5732 unlock_user(p
, arg1
, 0);
5734 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
5735 case TARGET_NR_faccessat
:
5736 if (!(p
= lock_user_string(arg2
)))
5738 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
5739 unlock_user(p
, arg2
, 0);
5742 #ifdef TARGET_NR_nice /* not on alpha */
5743 case TARGET_NR_nice
:
5744 ret
= get_errno(nice(arg1
));
5747 #ifdef TARGET_NR_ftime
5748 case TARGET_NR_ftime
:
5751 case TARGET_NR_sync
:
5755 case TARGET_NR_kill
:
5756 ret
= get_errno(kill(arg1
, target_to_host_signal(arg2
)));
5758 case TARGET_NR_rename
:
5761 p
= lock_user_string(arg1
);
5762 p2
= lock_user_string(arg2
);
5764 ret
= -TARGET_EFAULT
;
5766 ret
= get_errno(rename(p
, p2
));
5767 unlock_user(p2
, arg2
, 0);
5768 unlock_user(p
, arg1
, 0);
5771 #if defined(TARGET_NR_renameat)
5772 case TARGET_NR_renameat
:
5775 p
= lock_user_string(arg2
);
5776 p2
= lock_user_string(arg4
);
5778 ret
= -TARGET_EFAULT
;
5780 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
5781 unlock_user(p2
, arg4
, 0);
5782 unlock_user(p
, arg2
, 0);
5786 case TARGET_NR_mkdir
:
5787 if (!(p
= lock_user_string(arg1
)))
5789 ret
= get_errno(mkdir(p
, arg2
));
5790 unlock_user(p
, arg1
, 0);
5792 #if defined(TARGET_NR_mkdirat)
5793 case TARGET_NR_mkdirat
:
5794 if (!(p
= lock_user_string(arg2
)))
5796 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
5797 unlock_user(p
, arg2
, 0);
5800 case TARGET_NR_rmdir
:
5801 if (!(p
= lock_user_string(arg1
)))
5803 ret
= get_errno(rmdir(p
));
5804 unlock_user(p
, arg1
, 0);
5807 ret
= get_errno(dup(arg1
));
5809 case TARGET_NR_pipe
:
5810 ret
= do_pipe(cpu_env
, arg1
, 0, 0);
5812 #ifdef TARGET_NR_pipe2
5813 case TARGET_NR_pipe2
:
5814 ret
= do_pipe(cpu_env
, arg1
,
5815 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
5818 case TARGET_NR_times
:
5820 struct target_tms
*tmsp
;
5822 ret
= get_errno(times(&tms
));
5824 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
5827 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
5828 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
5829 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
5830 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
5833 ret
= host_to_target_clock_t(ret
);
5836 #ifdef TARGET_NR_prof
5837 case TARGET_NR_prof
:
5840 #ifdef TARGET_NR_signal
5841 case TARGET_NR_signal
:
5844 case TARGET_NR_acct
:
5846 ret
= get_errno(acct(NULL
));
5848 if (!(p
= lock_user_string(arg1
)))
5850 ret
= get_errno(acct(path(p
)));
5851 unlock_user(p
, arg1
, 0);
5854 #ifdef TARGET_NR_umount2
5855 case TARGET_NR_umount2
:
5856 if (!(p
= lock_user_string(arg1
)))
5858 ret
= get_errno(umount2(p
, arg2
));
5859 unlock_user(p
, arg1
, 0);
5862 #ifdef TARGET_NR_lock
5863 case TARGET_NR_lock
:
5866 case TARGET_NR_ioctl
:
5867 ret
= do_ioctl(arg1
, arg2
, arg3
);
5869 case TARGET_NR_fcntl
:
5870 ret
= do_fcntl(arg1
, arg2
, arg3
);
5872 #ifdef TARGET_NR_mpx
5876 case TARGET_NR_setpgid
:
5877 ret
= get_errno(setpgid(arg1
, arg2
));
5879 #ifdef TARGET_NR_ulimit
5880 case TARGET_NR_ulimit
:
5883 #ifdef TARGET_NR_oldolduname
5884 case TARGET_NR_oldolduname
:
5887 case TARGET_NR_umask
:
5888 ret
= get_errno(umask(arg1
));
5890 case TARGET_NR_chroot
:
5891 if (!(p
= lock_user_string(arg1
)))
5893 ret
= get_errno(chroot(p
));
5894 unlock_user(p
, arg1
, 0);
5896 case TARGET_NR_ustat
:
5898 case TARGET_NR_dup2
:
5899 ret
= get_errno(dup2(arg1
, arg2
));
5901 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
5902 case TARGET_NR_dup3
:
5903 ret
= get_errno(dup3(arg1
, arg2
, arg3
));
5906 #ifdef TARGET_NR_getppid /* not on alpha */
5907 case TARGET_NR_getppid
:
5908 ret
= get_errno(getppid());
5911 case TARGET_NR_getpgrp
:
5912 ret
= get_errno(getpgrp());
5914 case TARGET_NR_setsid
:
5915 ret
= get_errno(setsid());
5917 #ifdef TARGET_NR_sigaction
5918 case TARGET_NR_sigaction
:
5920 #if defined(TARGET_ALPHA)
5921 struct target_sigaction act
, oact
, *pact
= 0;
5922 struct target_old_sigaction
*old_act
;
5924 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
5926 act
._sa_handler
= old_act
->_sa_handler
;
5927 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
5928 act
.sa_flags
= old_act
->sa_flags
;
5929 act
.sa_restorer
= 0;
5930 unlock_user_struct(old_act
, arg2
, 0);
5933 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
5934 if (!is_error(ret
) && arg3
) {
5935 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
5937 old_act
->_sa_handler
= oact
._sa_handler
;
5938 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
5939 old_act
->sa_flags
= oact
.sa_flags
;
5940 unlock_user_struct(old_act
, arg3
, 1);
5942 #elif defined(TARGET_MIPS)
5943 struct target_sigaction act
, oact
, *pact
, *old_act
;
5946 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
5948 act
._sa_handler
= old_act
->_sa_handler
;
5949 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
5950 act
.sa_flags
= old_act
->sa_flags
;
5951 unlock_user_struct(old_act
, arg2
, 0);
5957 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
5959 if (!is_error(ret
) && arg3
) {
5960 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
5962 old_act
->_sa_handler
= oact
._sa_handler
;
5963 old_act
->sa_flags
= oact
.sa_flags
;
5964 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
5965 old_act
->sa_mask
.sig
[1] = 0;
5966 old_act
->sa_mask
.sig
[2] = 0;
5967 old_act
->sa_mask
.sig
[3] = 0;
5968 unlock_user_struct(old_act
, arg3
, 1);
5971 struct target_old_sigaction
*old_act
;
5972 struct target_sigaction act
, oact
, *pact
;
5974 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
5976 act
._sa_handler
= old_act
->_sa_handler
;
5977 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
5978 act
.sa_flags
= old_act
->sa_flags
;
5979 act
.sa_restorer
= old_act
->sa_restorer
;
5980 unlock_user_struct(old_act
, arg2
, 0);
5985 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
5986 if (!is_error(ret
) && arg3
) {
5987 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
5989 old_act
->_sa_handler
= oact
._sa_handler
;
5990 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
5991 old_act
->sa_flags
= oact
.sa_flags
;
5992 old_act
->sa_restorer
= oact
.sa_restorer
;
5993 unlock_user_struct(old_act
, arg3
, 1);
5999 case TARGET_NR_rt_sigaction
:
6001 #if defined(TARGET_ALPHA)
6002 struct target_sigaction act
, oact
, *pact
= 0;
6003 struct target_rt_sigaction
*rt_act
;
6004 /* ??? arg4 == sizeof(sigset_t). */
6006 if (!lock_user_struct(VERIFY_READ
, rt_act
, arg2
, 1))
6008 act
._sa_handler
= rt_act
->_sa_handler
;
6009 act
.sa_mask
= rt_act
->sa_mask
;
6010 act
.sa_flags
= rt_act
->sa_flags
;
6011 act
.sa_restorer
= arg5
;
6012 unlock_user_struct(rt_act
, arg2
, 0);
6015 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
6016 if (!is_error(ret
) && arg3
) {
6017 if (!lock_user_struct(VERIFY_WRITE
, rt_act
, arg3
, 0))
6019 rt_act
->_sa_handler
= oact
._sa_handler
;
6020 rt_act
->sa_mask
= oact
.sa_mask
;
6021 rt_act
->sa_flags
= oact
.sa_flags
;
6022 unlock_user_struct(rt_act
, arg3
, 1);
6025 struct target_sigaction
*act
;
6026 struct target_sigaction
*oact
;
6029 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1))
6034 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
6035 ret
= -TARGET_EFAULT
;
6036 goto rt_sigaction_fail
;
6040 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
6043 unlock_user_struct(act
, arg2
, 0);
6045 unlock_user_struct(oact
, arg3
, 1);
6049 #ifdef TARGET_NR_sgetmask /* not on alpha */
6050 case TARGET_NR_sgetmask
:
6053 abi_ulong target_set
;
6054 sigprocmask(0, NULL
, &cur_set
);
6055 host_to_target_old_sigset(&target_set
, &cur_set
);
6060 #ifdef TARGET_NR_ssetmask /* not on alpha */
6061 case TARGET_NR_ssetmask
:
6063 sigset_t set
, oset
, cur_set
;
6064 abi_ulong target_set
= arg1
;
6065 sigprocmask(0, NULL
, &cur_set
);
6066 target_to_host_old_sigset(&set
, &target_set
);
6067 sigorset(&set
, &set
, &cur_set
);
6068 sigprocmask(SIG_SETMASK
, &set
, &oset
);
6069 host_to_target_old_sigset(&target_set
, &oset
);
6074 #ifdef TARGET_NR_sigprocmask
6075 case TARGET_NR_sigprocmask
:
6077 #if defined(TARGET_ALPHA)
6078 sigset_t set
, oldset
;
6083 case TARGET_SIG_BLOCK
:
6086 case TARGET_SIG_UNBLOCK
:
6089 case TARGET_SIG_SETMASK
:
6093 ret
= -TARGET_EINVAL
;
6097 target_to_host_old_sigset(&set
, &mask
);
6099 ret
= get_errno(sigprocmask(how
, &set
, &oldset
));
6100 if (!is_error(ret
)) {
6101 host_to_target_old_sigset(&mask
, &oldset
);
6103 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0; /* force no error */
6106 sigset_t set
, oldset
, *set_ptr
;
6111 case TARGET_SIG_BLOCK
:
6114 case TARGET_SIG_UNBLOCK
:
6117 case TARGET_SIG_SETMASK
:
6121 ret
= -TARGET_EINVAL
;
6124 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
6126 target_to_host_old_sigset(&set
, p
);
6127 unlock_user(p
, arg2
, 0);
6133 ret
= get_errno(sigprocmask(how
, set_ptr
, &oldset
));
6134 if (!is_error(ret
) && arg3
) {
6135 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
6137 host_to_target_old_sigset(p
, &oldset
);
6138 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
6144 case TARGET_NR_rt_sigprocmask
:
6147 sigset_t set
, oldset
, *set_ptr
;
6151 case TARGET_SIG_BLOCK
:
6154 case TARGET_SIG_UNBLOCK
:
6157 case TARGET_SIG_SETMASK
:
6161 ret
= -TARGET_EINVAL
;
6164 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
6166 target_to_host_sigset(&set
, p
);
6167 unlock_user(p
, arg2
, 0);
6173 ret
= get_errno(sigprocmask(how
, set_ptr
, &oldset
));
6174 if (!is_error(ret
) && arg3
) {
6175 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
6177 host_to_target_sigset(p
, &oldset
);
6178 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
6182 #ifdef TARGET_NR_sigpending
6183 case TARGET_NR_sigpending
:
6186 ret
= get_errno(sigpending(&set
));
6187 if (!is_error(ret
)) {
6188 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
6190 host_to_target_old_sigset(p
, &set
);
6191 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
6196 case TARGET_NR_rt_sigpending
:
6199 ret
= get_errno(sigpending(&set
));
6200 if (!is_error(ret
)) {
6201 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
6203 host_to_target_sigset(p
, &set
);
6204 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
6208 #ifdef TARGET_NR_sigsuspend
6209 case TARGET_NR_sigsuspend
:
6212 #if defined(TARGET_ALPHA)
6213 abi_ulong mask
= arg1
;
6214 target_to_host_old_sigset(&set
, &mask
);
6216 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6218 target_to_host_old_sigset(&set
, p
);
6219 unlock_user(p
, arg1
, 0);
6221 ret
= get_errno(sigsuspend(&set
));
6225 case TARGET_NR_rt_sigsuspend
:
6228 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6230 target_to_host_sigset(&set
, p
);
6231 unlock_user(p
, arg1
, 0);
6232 ret
= get_errno(sigsuspend(&set
));
6235 case TARGET_NR_rt_sigtimedwait
:
6238 struct timespec uts
, *puts
;
6241 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
6243 target_to_host_sigset(&set
, p
);
6244 unlock_user(p
, arg1
, 0);
6247 target_to_host_timespec(puts
, arg3
);
6251 ret
= get_errno(sigtimedwait(&set
, &uinfo
, puts
));
6252 if (!is_error(ret
) && arg2
) {
6253 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
), 0)))
6255 host_to_target_siginfo(p
, &uinfo
);
6256 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
6260 case TARGET_NR_rt_sigqueueinfo
:
6263 if (!(p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_sigset_t
), 1)))
6265 target_to_host_siginfo(&uinfo
, p
);
6266 unlock_user(p
, arg1
, 0);
6267 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
6270 #ifdef TARGET_NR_sigreturn
6271 case TARGET_NR_sigreturn
:
6272 /* NOTE: ret is eax, so not transcoding must be done */
6273 ret
= do_sigreturn(cpu_env
);
6276 case TARGET_NR_rt_sigreturn
:
6277 /* NOTE: ret is eax, so not transcoding must be done */
6278 ret
= do_rt_sigreturn(cpu_env
);
6280 case TARGET_NR_sethostname
:
6281 if (!(p
= lock_user_string(arg1
)))
6283 ret
= get_errno(sethostname(p
, arg2
));
6284 unlock_user(p
, arg1
, 0);
6286 case TARGET_NR_setrlimit
:
6288 int resource
= target_to_host_resource(arg1
);
6289 struct target_rlimit
*target_rlim
;
6291 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
6293 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
6294 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
6295 unlock_user_struct(target_rlim
, arg2
, 0);
6296 ret
= get_errno(setrlimit(resource
, &rlim
));
6299 case TARGET_NR_getrlimit
:
6301 int resource
= target_to_host_resource(arg1
);
6302 struct target_rlimit
*target_rlim
;
6305 ret
= get_errno(getrlimit(resource
, &rlim
));
6306 if (!is_error(ret
)) {
6307 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
6309 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
6310 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
6311 unlock_user_struct(target_rlim
, arg2
, 1);
6315 case TARGET_NR_getrusage
:
6317 struct rusage rusage
;
6318 ret
= get_errno(getrusage(arg1
, &rusage
));
6319 if (!is_error(ret
)) {
6320 host_to_target_rusage(arg2
, &rusage
);
6324 case TARGET_NR_gettimeofday
:
6327 ret
= get_errno(gettimeofday(&tv
, NULL
));
6328 if (!is_error(ret
)) {
6329 if (copy_to_user_timeval(arg1
, &tv
))
6334 case TARGET_NR_settimeofday
:
6337 if (copy_from_user_timeval(&tv
, arg1
))
6339 ret
= get_errno(settimeofday(&tv
, NULL
));
6342 #if defined(TARGET_NR_select)
6343 case TARGET_NR_select
:
6344 #if defined(TARGET_S390X) || defined(TARGET_ALPHA)
6345 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
6348 struct target_sel_arg_struct
*sel
;
6349 abi_ulong inp
, outp
, exp
, tvp
;
6352 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1))
6354 nsel
= tswapal(sel
->n
);
6355 inp
= tswapal(sel
->inp
);
6356 outp
= tswapal(sel
->outp
);
6357 exp
= tswapal(sel
->exp
);
6358 tvp
= tswapal(sel
->tvp
);
6359 unlock_user_struct(sel
, arg1
, 0);
6360 ret
= do_select(nsel
, inp
, outp
, exp
, tvp
);
6365 #ifdef TARGET_NR_pselect6
6366 case TARGET_NR_pselect6
:
6368 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
6369 fd_set rfds
, wfds
, efds
;
6370 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
6371 struct timespec ts
, *ts_ptr
;
6374 * The 6th arg is actually two args smashed together,
6375 * so we cannot use the C library.
6383 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
6384 target_sigset_t
*target_sigset
;
6392 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
6396 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
6400 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
6406 * This takes a timespec, and not a timeval, so we cannot
6407 * use the do_select() helper ...
6410 if (target_to_host_timespec(&ts
, ts_addr
)) {
6418 /* Extract the two packed args for the sigset */
6421 sig
.size
= _NSIG
/ 8;
6423 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
6427 arg_sigset
= tswapal(arg7
[0]);
6428 arg_sigsize
= tswapal(arg7
[1]);
6429 unlock_user(arg7
, arg6
, 0);
6433 if (arg_sigsize
!= sizeof(*target_sigset
)) {
6434 /* Like the kernel, we enforce correct size sigsets */
6435 ret
= -TARGET_EINVAL
;
6438 target_sigset
= lock_user(VERIFY_READ
, arg_sigset
,
6439 sizeof(*target_sigset
), 1);
6440 if (!target_sigset
) {
6443 target_to_host_sigset(&set
, target_sigset
);
6444 unlock_user(target_sigset
, arg_sigset
, 0);
6452 ret
= get_errno(sys_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
6455 if (!is_error(ret
)) {
6456 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
6458 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
6460 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
6463 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
))
6469 case TARGET_NR_symlink
:
6472 p
= lock_user_string(arg1
);
6473 p2
= lock_user_string(arg2
);
6475 ret
= -TARGET_EFAULT
;
6477 ret
= get_errno(symlink(p
, p2
));
6478 unlock_user(p2
, arg2
, 0);
6479 unlock_user(p
, arg1
, 0);
6482 #if defined(TARGET_NR_symlinkat)
6483 case TARGET_NR_symlinkat
:
6486 p
= lock_user_string(arg1
);
6487 p2
= lock_user_string(arg3
);
6489 ret
= -TARGET_EFAULT
;
6491 ret
= get_errno(symlinkat(p
, arg2
, p2
));
6492 unlock_user(p2
, arg3
, 0);
6493 unlock_user(p
, arg1
, 0);
6497 #ifdef TARGET_NR_oldlstat
6498 case TARGET_NR_oldlstat
:
6501 case TARGET_NR_readlink
:
6504 p
= lock_user_string(arg1
);
6505 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
6507 ret
= -TARGET_EFAULT
;
6508 } else if (is_proc_myself((const char *)p
, "exe")) {
6509 char real
[PATH_MAX
], *temp
;
6510 temp
= realpath(exec_path
, real
);
6511 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
6512 snprintf((char *)p2
, arg3
, "%s", real
);
6514 ret
= get_errno(readlink(path(p
), p2
, arg3
));
6516 unlock_user(p2
, arg2
, ret
);
6517 unlock_user(p
, arg1
, 0);
6520 #if defined(TARGET_NR_readlinkat)
6521 case TARGET_NR_readlinkat
:
6524 p
= lock_user_string(arg2
);
6525 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
6527 ret
= -TARGET_EFAULT
;
6528 } else if (is_proc_myself((const char *)p
, "exe")) {
6529 char real
[PATH_MAX
], *temp
;
6530 temp
= realpath(exec_path
, real
);
6531 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
6532 snprintf((char *)p2
, arg4
, "%s", real
);
6534 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
6536 unlock_user(p2
, arg3
, ret
);
6537 unlock_user(p
, arg2
, 0);
6541 #ifdef TARGET_NR_uselib
6542 case TARGET_NR_uselib
:
6545 #ifdef TARGET_NR_swapon
6546 case TARGET_NR_swapon
:
6547 if (!(p
= lock_user_string(arg1
)))
6549 ret
= get_errno(swapon(p
, arg2
));
6550 unlock_user(p
, arg1
, 0);
6553 case TARGET_NR_reboot
:
6554 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
6555 /* arg4 must be ignored in all other cases */
6556 p
= lock_user_string(arg4
);
6560 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
6561 unlock_user(p
, arg4
, 0);
6563 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
6566 #ifdef TARGET_NR_readdir
6567 case TARGET_NR_readdir
:
6570 #ifdef TARGET_NR_mmap
6571 case TARGET_NR_mmap
:
6572 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6573 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
6574 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
6575 || defined(TARGET_S390X)
6578 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
6579 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
6587 unlock_user(v
, arg1
, 0);
6588 ret
= get_errno(target_mmap(v1
, v2
, v3
,
6589 target_to_host_bitmask(v4
, mmap_flags_tbl
),
6593 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
6594 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
6600 #ifdef TARGET_NR_mmap2
6601 case TARGET_NR_mmap2
:
6603 #define MMAP_SHIFT 12
6605 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
6606 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
6608 arg6
<< MMAP_SHIFT
));
6611 case TARGET_NR_munmap
:
6612 ret
= get_errno(target_munmap(arg1
, arg2
));
6614 case TARGET_NR_mprotect
:
6616 TaskState
*ts
= ((CPUArchState
*)cpu_env
)->opaque
;
6617 /* Special hack to detect libc making the stack executable. */
6618 if ((arg3
& PROT_GROWSDOWN
)
6619 && arg1
>= ts
->info
->stack_limit
6620 && arg1
<= ts
->info
->start_stack
) {
6621 arg3
&= ~PROT_GROWSDOWN
;
6622 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
6623 arg1
= ts
->info
->stack_limit
;
6626 ret
= get_errno(target_mprotect(arg1
, arg2
, arg3
));
6628 #ifdef TARGET_NR_mremap
6629 case TARGET_NR_mremap
:
6630 ret
= get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
6633 /* ??? msync/mlock/munlock are broken for softmmu. */
6634 #ifdef TARGET_NR_msync
6635 case TARGET_NR_msync
:
6636 ret
= get_errno(msync(g2h(arg1
), arg2
, arg3
));
6639 #ifdef TARGET_NR_mlock
6640 case TARGET_NR_mlock
:
6641 ret
= get_errno(mlock(g2h(arg1
), arg2
));
6644 #ifdef TARGET_NR_munlock
6645 case TARGET_NR_munlock
:
6646 ret
= get_errno(munlock(g2h(arg1
), arg2
));
6649 #ifdef TARGET_NR_mlockall
6650 case TARGET_NR_mlockall
:
6651 ret
= get_errno(mlockall(arg1
));
6654 #ifdef TARGET_NR_munlockall
6655 case TARGET_NR_munlockall
:
6656 ret
= get_errno(munlockall());
6659 case TARGET_NR_truncate
:
6660 if (!(p
= lock_user_string(arg1
)))
6662 ret
= get_errno(truncate(p
, arg2
));
6663 unlock_user(p
, arg1
, 0);
6665 case TARGET_NR_ftruncate
:
6666 ret
= get_errno(ftruncate(arg1
, arg2
));
6668 case TARGET_NR_fchmod
:
6669 ret
= get_errno(fchmod(arg1
, arg2
));
6671 #if defined(TARGET_NR_fchmodat)
6672 case TARGET_NR_fchmodat
:
6673 if (!(p
= lock_user_string(arg2
)))
6675 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
6676 unlock_user(p
, arg2
, 0);
6679 case TARGET_NR_getpriority
:
6680 /* Note that negative values are valid for getpriority, so we must
6681 differentiate based on errno settings. */
6683 ret
= getpriority(arg1
, arg2
);
6684 if (ret
== -1 && errno
!= 0) {
6685 ret
= -host_to_target_errno(errno
);
6689 /* Return value is the unbiased priority. Signal no error. */
6690 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0;
6692 /* Return value is a biased priority to avoid negative numbers. */
6696 case TARGET_NR_setpriority
:
6697 ret
= get_errno(setpriority(arg1
, arg2
, arg3
));
6699 #ifdef TARGET_NR_profil
6700 case TARGET_NR_profil
:
6703 case TARGET_NR_statfs
:
6704 if (!(p
= lock_user_string(arg1
)))
6706 ret
= get_errno(statfs(path(p
), &stfs
));
6707 unlock_user(p
, arg1
, 0);
6709 if (!is_error(ret
)) {
6710 struct target_statfs
*target_stfs
;
6712 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
6714 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
6715 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
6716 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
6717 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
6718 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
6719 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
6720 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
6721 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
6722 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
6723 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
6724 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
6725 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
6726 unlock_user_struct(target_stfs
, arg2
, 1);
6729 case TARGET_NR_fstatfs
:
6730 ret
= get_errno(fstatfs(arg1
, &stfs
));
6731 goto convert_statfs
;
6732 #ifdef TARGET_NR_statfs64
6733 case TARGET_NR_statfs64
:
6734 if (!(p
= lock_user_string(arg1
)))
6736 ret
= get_errno(statfs(path(p
), &stfs
));
6737 unlock_user(p
, arg1
, 0);
6739 if (!is_error(ret
)) {
6740 struct target_statfs64
*target_stfs
;
6742 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
6744 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
6745 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
6746 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
6747 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
6748 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
6749 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
6750 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
6751 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
6752 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
6753 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
6754 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
6755 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
6756 unlock_user_struct(target_stfs
, arg3
, 1);
6759 case TARGET_NR_fstatfs64
:
6760 ret
= get_errno(fstatfs(arg1
, &stfs
));
6761 goto convert_statfs64
;
6763 #ifdef TARGET_NR_ioperm
6764 case TARGET_NR_ioperm
:
6767 #ifdef TARGET_NR_socketcall
6768 case TARGET_NR_socketcall
:
6769 ret
= do_socketcall(arg1
, arg2
);
6772 #ifdef TARGET_NR_accept
6773 case TARGET_NR_accept
:
6774 ret
= do_accept4(arg1
, arg2
, arg3
, 0);
6777 #ifdef TARGET_NR_accept4
6778 case TARGET_NR_accept4
:
6779 #ifdef CONFIG_ACCEPT4
6780 ret
= do_accept4(arg1
, arg2
, arg3
, arg4
);
6786 #ifdef TARGET_NR_bind
6787 case TARGET_NR_bind
:
6788 ret
= do_bind(arg1
, arg2
, arg3
);
6791 #ifdef TARGET_NR_connect
6792 case TARGET_NR_connect
:
6793 ret
= do_connect(arg1
, arg2
, arg3
);
6796 #ifdef TARGET_NR_getpeername
6797 case TARGET_NR_getpeername
:
6798 ret
= do_getpeername(arg1
, arg2
, arg3
);
6801 #ifdef TARGET_NR_getsockname
6802 case TARGET_NR_getsockname
:
6803 ret
= do_getsockname(arg1
, arg2
, arg3
);
6806 #ifdef TARGET_NR_getsockopt
6807 case TARGET_NR_getsockopt
:
6808 ret
= do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
6811 #ifdef TARGET_NR_listen
6812 case TARGET_NR_listen
:
6813 ret
= get_errno(listen(arg1
, arg2
));
6816 #ifdef TARGET_NR_recv
6817 case TARGET_NR_recv
:
6818 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
6821 #ifdef TARGET_NR_recvfrom
6822 case TARGET_NR_recvfrom
:
6823 ret
= do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
6826 #ifdef TARGET_NR_recvmsg
6827 case TARGET_NR_recvmsg
:
6828 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
6831 #ifdef TARGET_NR_send
6832 case TARGET_NR_send
:
6833 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
6836 #ifdef TARGET_NR_sendmsg
6837 case TARGET_NR_sendmsg
:
6838 ret
= do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
6841 #ifdef TARGET_NR_sendto
6842 case TARGET_NR_sendto
:
6843 ret
= do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
6846 #ifdef TARGET_NR_shutdown
6847 case TARGET_NR_shutdown
:
6848 ret
= get_errno(shutdown(arg1
, arg2
));
6851 #ifdef TARGET_NR_socket
6852 case TARGET_NR_socket
:
6853 ret
= do_socket(arg1
, arg2
, arg3
);
6856 #ifdef TARGET_NR_socketpair
6857 case TARGET_NR_socketpair
:
6858 ret
= do_socketpair(arg1
, arg2
, arg3
, arg4
);
6861 #ifdef TARGET_NR_setsockopt
6862 case TARGET_NR_setsockopt
:
6863 ret
= do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
6867 case TARGET_NR_syslog
:
6868 if (!(p
= lock_user_string(arg2
)))
6870 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
6871 unlock_user(p
, arg2
, 0);
6874 case TARGET_NR_setitimer
:
6876 struct itimerval value
, ovalue
, *pvalue
;
6880 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
6881 || copy_from_user_timeval(&pvalue
->it_value
,
6882 arg2
+ sizeof(struct target_timeval
)))
6887 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
6888 if (!is_error(ret
) && arg3
) {
6889 if (copy_to_user_timeval(arg3
,
6890 &ovalue
.it_interval
)
6891 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
6897 case TARGET_NR_getitimer
:
6899 struct itimerval value
;
6901 ret
= get_errno(getitimer(arg1
, &value
));
6902 if (!is_error(ret
) && arg2
) {
6903 if (copy_to_user_timeval(arg2
,
6905 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
6911 case TARGET_NR_stat
:
6912 if (!(p
= lock_user_string(arg1
)))
6914 ret
= get_errno(stat(path(p
), &st
));
6915 unlock_user(p
, arg1
, 0);
6917 case TARGET_NR_lstat
:
6918 if (!(p
= lock_user_string(arg1
)))
6920 ret
= get_errno(lstat(path(p
), &st
));
6921 unlock_user(p
, arg1
, 0);
6923 case TARGET_NR_fstat
:
6925 ret
= get_errno(fstat(arg1
, &st
));
6927 if (!is_error(ret
)) {
6928 struct target_stat
*target_st
;
6930 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
6932 memset(target_st
, 0, sizeof(*target_st
));
6933 __put_user(st
.st_dev
, &target_st
->st_dev
);
6934 __put_user(st
.st_ino
, &target_st
->st_ino
);
6935 __put_user(st
.st_mode
, &target_st
->st_mode
);
6936 __put_user(st
.st_uid
, &target_st
->st_uid
);
6937 __put_user(st
.st_gid
, &target_st
->st_gid
);
6938 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
6939 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
6940 __put_user(st
.st_size
, &target_st
->st_size
);
6941 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
6942 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
6943 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
6944 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
6945 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
6946 unlock_user_struct(target_st
, arg2
, 1);
6950 #ifdef TARGET_NR_olduname
6951 case TARGET_NR_olduname
:
6954 #ifdef TARGET_NR_iopl
6955 case TARGET_NR_iopl
:
6958 case TARGET_NR_vhangup
:
6959 ret
= get_errno(vhangup());
6961 #ifdef TARGET_NR_idle
6962 case TARGET_NR_idle
:
6965 #ifdef TARGET_NR_syscall
6966 case TARGET_NR_syscall
:
6967 ret
= do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
6968 arg6
, arg7
, arg8
, 0);
6971 case TARGET_NR_wait4
:
6974 abi_long status_ptr
= arg2
;
6975 struct rusage rusage
, *rusage_ptr
;
6976 abi_ulong target_rusage
= arg4
;
6978 rusage_ptr
= &rusage
;
6981 ret
= get_errno(wait4(arg1
, &status
, arg3
, rusage_ptr
));
6982 if (!is_error(ret
)) {
6983 if (status_ptr
&& ret
) {
6984 status
= host_to_target_waitstatus(status
);
6985 if (put_user_s32(status
, status_ptr
))
6989 host_to_target_rusage(target_rusage
, &rusage
);
6993 #ifdef TARGET_NR_swapoff
6994 case TARGET_NR_swapoff
:
6995 if (!(p
= lock_user_string(arg1
)))
6997 ret
= get_errno(swapoff(p
));
6998 unlock_user(p
, arg1
, 0);
7001 case TARGET_NR_sysinfo
:
7003 struct target_sysinfo
*target_value
;
7004 struct sysinfo value
;
7005 ret
= get_errno(sysinfo(&value
));
7006 if (!is_error(ret
) && arg1
)
7008 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
7010 __put_user(value
.uptime
, &target_value
->uptime
);
7011 __put_user(value
.loads
[0], &target_value
->loads
[0]);
7012 __put_user(value
.loads
[1], &target_value
->loads
[1]);
7013 __put_user(value
.loads
[2], &target_value
->loads
[2]);
7014 __put_user(value
.totalram
, &target_value
->totalram
);
7015 __put_user(value
.freeram
, &target_value
->freeram
);
7016 __put_user(value
.sharedram
, &target_value
->sharedram
);
7017 __put_user(value
.bufferram
, &target_value
->bufferram
);
7018 __put_user(value
.totalswap
, &target_value
->totalswap
);
7019 __put_user(value
.freeswap
, &target_value
->freeswap
);
7020 __put_user(value
.procs
, &target_value
->procs
);
7021 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
7022 __put_user(value
.freehigh
, &target_value
->freehigh
);
7023 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
7024 unlock_user_struct(target_value
, arg1
, 1);
7028 #ifdef TARGET_NR_ipc
7030 ret
= do_ipc(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
7033 #ifdef TARGET_NR_semget
7034 case TARGET_NR_semget
:
7035 ret
= get_errno(semget(arg1
, arg2
, arg3
));
7038 #ifdef TARGET_NR_semop
7039 case TARGET_NR_semop
:
7040 ret
= do_semop(arg1
, arg2
, arg3
);
7043 #ifdef TARGET_NR_semctl
7044 case TARGET_NR_semctl
:
7045 ret
= do_semctl(arg1
, arg2
, arg3
, (union target_semun
)(abi_ulong
)arg4
);
7048 #ifdef TARGET_NR_msgctl
7049 case TARGET_NR_msgctl
:
7050 ret
= do_msgctl(arg1
, arg2
, arg3
);
7053 #ifdef TARGET_NR_msgget
7054 case TARGET_NR_msgget
:
7055 ret
= get_errno(msgget(arg1
, arg2
));
7058 #ifdef TARGET_NR_msgrcv
7059 case TARGET_NR_msgrcv
:
7060 ret
= do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
7063 #ifdef TARGET_NR_msgsnd
7064 case TARGET_NR_msgsnd
:
7065 ret
= do_msgsnd(arg1
, arg2
, arg3
, arg4
);
7068 #ifdef TARGET_NR_shmget
7069 case TARGET_NR_shmget
:
7070 ret
= get_errno(shmget(arg1
, arg2
, arg3
));
7073 #ifdef TARGET_NR_shmctl
7074 case TARGET_NR_shmctl
:
7075 ret
= do_shmctl(arg1
, arg2
, arg3
);
7078 #ifdef TARGET_NR_shmat
7079 case TARGET_NR_shmat
:
7080 ret
= do_shmat(arg1
, arg2
, arg3
);
7083 #ifdef TARGET_NR_shmdt
7084 case TARGET_NR_shmdt
:
7085 ret
= do_shmdt(arg1
);
7088 case TARGET_NR_fsync
:
7089 ret
= get_errno(fsync(arg1
));
7091 case TARGET_NR_clone
:
7092 /* Linux manages to have three different orderings for its
7093 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
7094 * match the kernel's CONFIG_CLONE_* settings.
7095 * Microblaze is further special in that it uses a sixth
7096 * implicit argument to clone for the TLS pointer.
7098 #if defined(TARGET_MICROBLAZE)
7099 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
7100 #elif defined(TARGET_CLONE_BACKWARDS)
7101 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
7102 #elif defined(TARGET_CLONE_BACKWARDS2)
7103 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
7105 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
7108 #ifdef __NR_exit_group
7109 /* new thread calls */
7110 case TARGET_NR_exit_group
:
7114 gdb_exit(cpu_env
, arg1
);
7115 ret
= get_errno(exit_group(arg1
));
7118 case TARGET_NR_setdomainname
:
7119 if (!(p
= lock_user_string(arg1
)))
7121 ret
= get_errno(setdomainname(p
, arg2
));
7122 unlock_user(p
, arg1
, 0);
7124 case TARGET_NR_uname
:
7125 /* no need to transcode because we use the linux syscall */
7127 struct new_utsname
* buf
;
7129 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
7131 ret
= get_errno(sys_uname(buf
));
7132 if (!is_error(ret
)) {
7133 /* Overrite the native machine name with whatever is being
7135 strcpy (buf
->machine
, cpu_to_uname_machine(cpu_env
));
7136 /* Allow the user to override the reported release. */
7137 if (qemu_uname_release
&& *qemu_uname_release
)
7138 strcpy (buf
->release
, qemu_uname_release
);
7140 unlock_user_struct(buf
, arg1
, 1);
7144 case TARGET_NR_modify_ldt
:
7145 ret
= do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
7147 #if !defined(TARGET_X86_64)
7148 case TARGET_NR_vm86old
:
7150 case TARGET_NR_vm86
:
7151 ret
= do_vm86(cpu_env
, arg1
, arg2
);
7155 case TARGET_NR_adjtimex
:
7157 #ifdef TARGET_NR_create_module
7158 case TARGET_NR_create_module
:
7160 case TARGET_NR_init_module
:
7161 case TARGET_NR_delete_module
:
7162 #ifdef TARGET_NR_get_kernel_syms
7163 case TARGET_NR_get_kernel_syms
:
7166 case TARGET_NR_quotactl
:
7168 case TARGET_NR_getpgid
:
7169 ret
= get_errno(getpgid(arg1
));
7171 case TARGET_NR_fchdir
:
7172 ret
= get_errno(fchdir(arg1
));
7174 #ifdef TARGET_NR_bdflush /* not on x86_64 */
7175 case TARGET_NR_bdflush
:
7178 #ifdef TARGET_NR_sysfs
7179 case TARGET_NR_sysfs
:
7182 case TARGET_NR_personality
:
7183 ret
= get_errno(personality(arg1
));
7185 #ifdef TARGET_NR_afs_syscall
7186 case TARGET_NR_afs_syscall
:
7189 #ifdef TARGET_NR__llseek /* Not on alpha */
7190 case TARGET_NR__llseek
:
7193 #if !defined(__NR_llseek)
7194 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | arg3
, arg5
);
7196 ret
= get_errno(res
);
7201 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
7203 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
7209 case TARGET_NR_getdents
:
7210 #ifdef __NR_getdents
7211 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
7213 struct target_dirent
*target_dirp
;
7214 struct linux_dirent
*dirp
;
7215 abi_long count
= arg3
;
7217 dirp
= malloc(count
);
7219 ret
= -TARGET_ENOMEM
;
7223 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
7224 if (!is_error(ret
)) {
7225 struct linux_dirent
*de
;
7226 struct target_dirent
*tde
;
7228 int reclen
, treclen
;
7229 int count1
, tnamelen
;
7233 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7237 reclen
= de
->d_reclen
;
7238 tnamelen
= reclen
- offsetof(struct linux_dirent
, d_name
);
7239 assert(tnamelen
>= 0);
7240 treclen
= tnamelen
+ offsetof(struct target_dirent
, d_name
);
7241 assert(count1
+ treclen
<= count
);
7242 tde
->d_reclen
= tswap16(treclen
);
7243 tde
->d_ino
= tswapal(de
->d_ino
);
7244 tde
->d_off
= tswapal(de
->d_off
);
7245 memcpy(tde
->d_name
, de
->d_name
, tnamelen
);
7246 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
7248 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
7252 unlock_user(target_dirp
, arg2
, ret
);
7258 struct linux_dirent
*dirp
;
7259 abi_long count
= arg3
;
7261 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7263 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
7264 if (!is_error(ret
)) {
7265 struct linux_dirent
*de
;
7270 reclen
= de
->d_reclen
;
7273 de
->d_reclen
= tswap16(reclen
);
7274 tswapls(&de
->d_ino
);
7275 tswapls(&de
->d_off
);
7276 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
7280 unlock_user(dirp
, arg2
, ret
);
7284 /* Implement getdents in terms of getdents64 */
7286 struct linux_dirent64
*dirp
;
7287 abi_long count
= arg3
;
7289 dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
7293 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
7294 if (!is_error(ret
)) {
7295 /* Convert the dirent64 structs to target dirent. We do this
7296 * in-place, since we can guarantee that a target_dirent is no
7297 * larger than a dirent64; however this means we have to be
7298 * careful to read everything before writing in the new format.
7300 struct linux_dirent64
*de
;
7301 struct target_dirent
*tde
;
7306 tde
= (struct target_dirent
*)dirp
;
7308 int namelen
, treclen
;
7309 int reclen
= de
->d_reclen
;
7310 uint64_t ino
= de
->d_ino
;
7311 int64_t off
= de
->d_off
;
7312 uint8_t type
= de
->d_type
;
7314 namelen
= strlen(de
->d_name
);
7315 treclen
= offsetof(struct target_dirent
, d_name
)
7317 treclen
= QEMU_ALIGN_UP(treclen
, sizeof(abi_long
));
7319 memmove(tde
->d_name
, de
->d_name
, namelen
+ 1);
7320 tde
->d_ino
= tswapal(ino
);
7321 tde
->d_off
= tswapal(off
);
7322 tde
->d_reclen
= tswap16(treclen
);
7323 /* The target_dirent type is in what was formerly a padding
7324 * byte at the end of the structure:
7326 *(((char *)tde
) + treclen
- 1) = type
;
7328 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
7329 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
7335 unlock_user(dirp
, arg2
, ret
);
7339 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
7340 case TARGET_NR_getdents64
:
7342 struct linux_dirent64
*dirp
;
7343 abi_long count
= arg3
;
7344 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
7346 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
7347 if (!is_error(ret
)) {
7348 struct linux_dirent64
*de
;
7353 reclen
= de
->d_reclen
;
7356 de
->d_reclen
= tswap16(reclen
);
7357 tswap64s((uint64_t *)&de
->d_ino
);
7358 tswap64s((uint64_t *)&de
->d_off
);
7359 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
7363 unlock_user(dirp
, arg2
, ret
);
7366 #endif /* TARGET_NR_getdents64 */
7367 #if defined(TARGET_NR__newselect)
7368 case TARGET_NR__newselect
:
7369 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
7372 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
7373 # ifdef TARGET_NR_poll
7374 case TARGET_NR_poll
:
7376 # ifdef TARGET_NR_ppoll
7377 case TARGET_NR_ppoll
:
7380 struct target_pollfd
*target_pfd
;
7381 unsigned int nfds
= arg2
;
7386 target_pfd
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_pollfd
) * nfds
, 1);
7390 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
7391 for(i
= 0; i
< nfds
; i
++) {
7392 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
7393 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
7396 # ifdef TARGET_NR_ppoll
7397 if (num
== TARGET_NR_ppoll
) {
7398 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
7399 target_sigset_t
*target_set
;
7400 sigset_t _set
, *set
= &_set
;
7403 if (target_to_host_timespec(timeout_ts
, arg3
)) {
7404 unlock_user(target_pfd
, arg1
, 0);
7412 target_set
= lock_user(VERIFY_READ
, arg4
, sizeof(target_sigset_t
), 1);
7414 unlock_user(target_pfd
, arg1
, 0);
7417 target_to_host_sigset(set
, target_set
);
7422 ret
= get_errno(sys_ppoll(pfd
, nfds
, timeout_ts
, set
, _NSIG
/8));
7424 if (!is_error(ret
) && arg3
) {
7425 host_to_target_timespec(arg3
, timeout_ts
);
7428 unlock_user(target_set
, arg4
, 0);
7432 ret
= get_errno(poll(pfd
, nfds
, timeout
));
7434 if (!is_error(ret
)) {
7435 for(i
= 0; i
< nfds
; i
++) {
7436 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
7439 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
7443 case TARGET_NR_flock
:
7444 /* NOTE: the flock constant seems to be the same for every
7446 ret
= get_errno(flock(arg1
, arg2
));
7448 case TARGET_NR_readv
:
7450 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
7452 ret
= get_errno(readv(arg1
, vec
, arg3
));
7453 unlock_iovec(vec
, arg2
, arg3
, 1);
7455 ret
= -host_to_target_errno(errno
);
7459 case TARGET_NR_writev
:
7461 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
7463 ret
= get_errno(writev(arg1
, vec
, arg3
));
7464 unlock_iovec(vec
, arg2
, arg3
, 0);
7466 ret
= -host_to_target_errno(errno
);
7470 case TARGET_NR_getsid
:
7471 ret
= get_errno(getsid(arg1
));
7473 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
7474 case TARGET_NR_fdatasync
:
7475 ret
= get_errno(fdatasync(arg1
));
7478 case TARGET_NR__sysctl
:
7479 /* We don't implement this, but ENOTDIR is always a safe
7481 ret
= -TARGET_ENOTDIR
;
7483 case TARGET_NR_sched_getaffinity
:
7485 unsigned int mask_size
;
7486 unsigned long *mask
;
7489 * sched_getaffinity needs multiples of ulong, so need to take
7490 * care of mismatches between target ulong and host ulong sizes.
7492 if (arg2
& (sizeof(abi_ulong
) - 1)) {
7493 ret
= -TARGET_EINVAL
;
7496 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
7498 mask
= alloca(mask_size
);
7499 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
7501 if (!is_error(ret
)) {
7502 if (copy_to_user(arg3
, mask
, ret
)) {
7508 case TARGET_NR_sched_setaffinity
:
7510 unsigned int mask_size
;
7511 unsigned long *mask
;
7514 * sched_setaffinity needs multiples of ulong, so need to take
7515 * care of mismatches between target ulong and host ulong sizes.
7517 if (arg2
& (sizeof(abi_ulong
) - 1)) {
7518 ret
= -TARGET_EINVAL
;
7521 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
7523 mask
= alloca(mask_size
);
7524 if (!lock_user_struct(VERIFY_READ
, p
, arg3
, 1)) {
7527 memcpy(mask
, p
, arg2
);
7528 unlock_user_struct(p
, arg2
, 0);
7530 ret
= get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
7533 case TARGET_NR_sched_setparam
:
7535 struct sched_param
*target_schp
;
7536 struct sched_param schp
;
7538 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
7540 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
7541 unlock_user_struct(target_schp
, arg2
, 0);
7542 ret
= get_errno(sched_setparam(arg1
, &schp
));
7545 case TARGET_NR_sched_getparam
:
7547 struct sched_param
*target_schp
;
7548 struct sched_param schp
;
7549 ret
= get_errno(sched_getparam(arg1
, &schp
));
7550 if (!is_error(ret
)) {
7551 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
7553 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
7554 unlock_user_struct(target_schp
, arg2
, 1);
7558 case TARGET_NR_sched_setscheduler
:
7560 struct sched_param
*target_schp
;
7561 struct sched_param schp
;
7562 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
7564 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
7565 unlock_user_struct(target_schp
, arg3
, 0);
7566 ret
= get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
7569 case TARGET_NR_sched_getscheduler
:
7570 ret
= get_errno(sched_getscheduler(arg1
));
7572 case TARGET_NR_sched_yield
:
7573 ret
= get_errno(sched_yield());
7575 case TARGET_NR_sched_get_priority_max
:
7576 ret
= get_errno(sched_get_priority_max(arg1
));
7578 case TARGET_NR_sched_get_priority_min
:
7579 ret
= get_errno(sched_get_priority_min(arg1
));
7581 case TARGET_NR_sched_rr_get_interval
:
7584 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
7585 if (!is_error(ret
)) {
7586 host_to_target_timespec(arg2
, &ts
);
7590 case TARGET_NR_nanosleep
:
7592 struct timespec req
, rem
;
7593 target_to_host_timespec(&req
, arg1
);
7594 ret
= get_errno(nanosleep(&req
, &rem
));
7595 if (is_error(ret
) && arg2
) {
7596 host_to_target_timespec(arg2
, &rem
);
7600 #ifdef TARGET_NR_query_module
7601 case TARGET_NR_query_module
:
7604 #ifdef TARGET_NR_nfsservctl
7605 case TARGET_NR_nfsservctl
:
7608 case TARGET_NR_prctl
:
7610 case PR_GET_PDEATHSIG
:
7613 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
7614 if (!is_error(ret
) && arg2
7615 && put_user_ual(deathsig
, arg2
)) {
7623 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
7627 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
7629 unlock_user(name
, arg2
, 16);
7634 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
7638 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
7640 unlock_user(name
, arg2
, 0);
7645 /* Most prctl options have no pointer arguments */
7646 ret
= get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
7650 #ifdef TARGET_NR_arch_prctl
7651 case TARGET_NR_arch_prctl
:
7652 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
7653 ret
= do_arch_prctl(cpu_env
, arg1
, arg2
);
7659 #ifdef TARGET_NR_pread64
7660 case TARGET_NR_pread64
:
7661 if (regpairs_aligned(cpu_env
)) {
7665 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
7667 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
7668 unlock_user(p
, arg2
, ret
);
7670 case TARGET_NR_pwrite64
:
7671 if (regpairs_aligned(cpu_env
)) {
7675 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
7677 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
7678 unlock_user(p
, arg2
, 0);
7681 case TARGET_NR_getcwd
:
7682 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
7684 ret
= get_errno(sys_getcwd1(p
, arg2
));
7685 unlock_user(p
, arg1
, ret
);
7687 case TARGET_NR_capget
:
7689 case TARGET_NR_capset
:
7691 case TARGET_NR_sigaltstack
:
7692 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
7693 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
7694 defined(TARGET_M68K) || defined(TARGET_S390X) || defined(TARGET_OPENRISC)
7695 ret
= do_sigaltstack(arg1
, arg2
, get_sp_from_cpustate((CPUArchState
*)cpu_env
));
7701 #ifdef CONFIG_SENDFILE
7702 case TARGET_NR_sendfile
:
7707 ret
= get_user_sal(off
, arg3
);
7708 if (is_error(ret
)) {
7713 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
7714 if (!is_error(ret
) && arg3
) {
7715 abi_long ret2
= put_user_sal(off
, arg3
);
7716 if (is_error(ret2
)) {
7722 #ifdef TARGET_NR_sendfile64
7723 case TARGET_NR_sendfile64
:
7728 ret
= get_user_s64(off
, arg3
);
7729 if (is_error(ret
)) {
7734 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
7735 if (!is_error(ret
) && arg3
) {
7736 abi_long ret2
= put_user_s64(off
, arg3
);
7737 if (is_error(ret2
)) {
7745 case TARGET_NR_sendfile
:
7746 #ifdef TARGET_NR_sendfile64
7747 case TARGET_NR_sendfile64
:
7752 #ifdef TARGET_NR_getpmsg
7753 case TARGET_NR_getpmsg
:
7756 #ifdef TARGET_NR_putpmsg
7757 case TARGET_NR_putpmsg
:
7760 #ifdef TARGET_NR_vfork
7761 case TARGET_NR_vfork
:
7762 ret
= get_errno(do_fork(cpu_env
, CLONE_VFORK
| CLONE_VM
| SIGCHLD
,
7766 #ifdef TARGET_NR_ugetrlimit
7767 case TARGET_NR_ugetrlimit
:
7770 int resource
= target_to_host_resource(arg1
);
7771 ret
= get_errno(getrlimit(resource
, &rlim
));
7772 if (!is_error(ret
)) {
7773 struct target_rlimit
*target_rlim
;
7774 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
7776 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
7777 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
7778 unlock_user_struct(target_rlim
, arg2
, 1);
7783 #ifdef TARGET_NR_truncate64
7784 case TARGET_NR_truncate64
:
7785 if (!(p
= lock_user_string(arg1
)))
7787 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
7788 unlock_user(p
, arg1
, 0);
7791 #ifdef TARGET_NR_ftruncate64
7792 case TARGET_NR_ftruncate64
:
7793 ret
= target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
7796 #ifdef TARGET_NR_stat64
7797 case TARGET_NR_stat64
:
7798 if (!(p
= lock_user_string(arg1
)))
7800 ret
= get_errno(stat(path(p
), &st
));
7801 unlock_user(p
, arg1
, 0);
7803 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
7806 #ifdef TARGET_NR_lstat64
7807 case TARGET_NR_lstat64
:
7808 if (!(p
= lock_user_string(arg1
)))
7810 ret
= get_errno(lstat(path(p
), &st
));
7811 unlock_user(p
, arg1
, 0);
7813 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
7816 #ifdef TARGET_NR_fstat64
7817 case TARGET_NR_fstat64
:
7818 ret
= get_errno(fstat(arg1
, &st
));
7820 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
7823 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
7824 #ifdef TARGET_NR_fstatat64
7825 case TARGET_NR_fstatat64
:
7827 #ifdef TARGET_NR_newfstatat
7828 case TARGET_NR_newfstatat
:
7830 if (!(p
= lock_user_string(arg2
)))
7832 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
7834 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
7837 case TARGET_NR_lchown
:
7838 if (!(p
= lock_user_string(arg1
)))
7840 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
7841 unlock_user(p
, arg1
, 0);
7843 #ifdef TARGET_NR_getuid
7844 case TARGET_NR_getuid
:
7845 ret
= get_errno(high2lowuid(getuid()));
7848 #ifdef TARGET_NR_getgid
7849 case TARGET_NR_getgid
:
7850 ret
= get_errno(high2lowgid(getgid()));
7853 #ifdef TARGET_NR_geteuid
7854 case TARGET_NR_geteuid
:
7855 ret
= get_errno(high2lowuid(geteuid()));
7858 #ifdef TARGET_NR_getegid
7859 case TARGET_NR_getegid
:
7860 ret
= get_errno(high2lowgid(getegid()));
7863 case TARGET_NR_setreuid
:
7864 ret
= get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
7866 case TARGET_NR_setregid
:
7867 ret
= get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
7869 case TARGET_NR_getgroups
:
7871 int gidsetsize
= arg1
;
7872 target_id
*target_grouplist
;
7876 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
7877 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
7878 if (gidsetsize
== 0)
7880 if (!is_error(ret
)) {
7881 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
7882 if (!target_grouplist
)
7884 for(i
= 0;i
< ret
; i
++)
7885 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
7886 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
7890 case TARGET_NR_setgroups
:
7892 int gidsetsize
= arg1
;
7893 target_id
*target_grouplist
;
7894 gid_t
*grouplist
= NULL
;
7897 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
7898 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
7899 if (!target_grouplist
) {
7900 ret
= -TARGET_EFAULT
;
7903 for (i
= 0; i
< gidsetsize
; i
++) {
7904 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
7906 unlock_user(target_grouplist
, arg2
, 0);
7908 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
7911 case TARGET_NR_fchown
:
7912 ret
= get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
7914 #if defined(TARGET_NR_fchownat)
7915 case TARGET_NR_fchownat
:
7916 if (!(p
= lock_user_string(arg2
)))
7918 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
7919 low2highgid(arg4
), arg5
));
7920 unlock_user(p
, arg2
, 0);
7923 #ifdef TARGET_NR_setresuid
7924 case TARGET_NR_setresuid
:
7925 ret
= get_errno(setresuid(low2highuid(arg1
),
7927 low2highuid(arg3
)));
7930 #ifdef TARGET_NR_getresuid
7931 case TARGET_NR_getresuid
:
7933 uid_t ruid
, euid
, suid
;
7934 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
7935 if (!is_error(ret
)) {
7936 if (put_user_u16(high2lowuid(ruid
), arg1
)
7937 || put_user_u16(high2lowuid(euid
), arg2
)
7938 || put_user_u16(high2lowuid(suid
), arg3
))
7944 #ifdef TARGET_NR_getresgid
7945 case TARGET_NR_setresgid
:
7946 ret
= get_errno(setresgid(low2highgid(arg1
),
7948 low2highgid(arg3
)));
7951 #ifdef TARGET_NR_getresgid
7952 case TARGET_NR_getresgid
:
7954 gid_t rgid
, egid
, sgid
;
7955 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
7956 if (!is_error(ret
)) {
7957 if (put_user_u16(high2lowgid(rgid
), arg1
)
7958 || put_user_u16(high2lowgid(egid
), arg2
)
7959 || put_user_u16(high2lowgid(sgid
), arg3
))
7965 case TARGET_NR_chown
:
7966 if (!(p
= lock_user_string(arg1
)))
7968 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
7969 unlock_user(p
, arg1
, 0);
7971 case TARGET_NR_setuid
:
7972 ret
= get_errno(setuid(low2highuid(arg1
)));
7974 case TARGET_NR_setgid
:
7975 ret
= get_errno(setgid(low2highgid(arg1
)));
7977 case TARGET_NR_setfsuid
:
7978 ret
= get_errno(setfsuid(arg1
));
7980 case TARGET_NR_setfsgid
:
7981 ret
= get_errno(setfsgid(arg1
));
7984 #ifdef TARGET_NR_lchown32
7985 case TARGET_NR_lchown32
:
7986 if (!(p
= lock_user_string(arg1
)))
7988 ret
= get_errno(lchown(p
, arg2
, arg3
));
7989 unlock_user(p
, arg1
, 0);
7992 #ifdef TARGET_NR_getuid32
7993 case TARGET_NR_getuid32
:
7994 ret
= get_errno(getuid());
7998 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
7999 /* Alpha specific */
8000 case TARGET_NR_getxuid
:
8004 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
8006 ret
= get_errno(getuid());
8009 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
8010 /* Alpha specific */
8011 case TARGET_NR_getxgid
:
8015 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
8017 ret
= get_errno(getgid());
8020 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
8021 /* Alpha specific */
8022 case TARGET_NR_osf_getsysinfo
:
8023 ret
= -TARGET_EOPNOTSUPP
;
8025 case TARGET_GSI_IEEE_FP_CONTROL
:
8027 uint64_t swcr
, fpcr
= cpu_alpha_load_fpcr (cpu_env
);
8029 /* Copied from linux ieee_fpcr_to_swcr. */
8030 swcr
= (fpcr
>> 35) & SWCR_STATUS_MASK
;
8031 swcr
|= (fpcr
>> 36) & SWCR_MAP_DMZ
;
8032 swcr
|= (~fpcr
>> 48) & (SWCR_TRAP_ENABLE_INV
8033 | SWCR_TRAP_ENABLE_DZE
8034 | SWCR_TRAP_ENABLE_OVF
);
8035 swcr
|= (~fpcr
>> 57) & (SWCR_TRAP_ENABLE_UNF
8036 | SWCR_TRAP_ENABLE_INE
);
8037 swcr
|= (fpcr
>> 47) & SWCR_MAP_UMZ
;
8038 swcr
|= (~fpcr
>> 41) & SWCR_TRAP_ENABLE_DNO
;
8040 if (put_user_u64 (swcr
, arg2
))
8046 /* case GSI_IEEE_STATE_AT_SIGNAL:
8047 -- Not implemented in linux kernel.
8049 -- Retrieves current unaligned access state; not much used.
8051 -- Retrieves implver information; surely not used.
8053 -- Grabs a copy of the HWRPB; surely not used.
8058 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
8059 /* Alpha specific */
8060 case TARGET_NR_osf_setsysinfo
:
8061 ret
= -TARGET_EOPNOTSUPP
;
8063 case TARGET_SSI_IEEE_FP_CONTROL
:
8065 uint64_t swcr
, fpcr
, orig_fpcr
;
8067 if (get_user_u64 (swcr
, arg2
)) {
8070 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
8071 fpcr
= orig_fpcr
& FPCR_DYN_MASK
;
8073 /* Copied from linux ieee_swcr_to_fpcr. */
8074 fpcr
|= (swcr
& SWCR_STATUS_MASK
) << 35;
8075 fpcr
|= (swcr
& SWCR_MAP_DMZ
) << 36;
8076 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_INV
8077 | SWCR_TRAP_ENABLE_DZE
8078 | SWCR_TRAP_ENABLE_OVF
)) << 48;
8079 fpcr
|= (~swcr
& (SWCR_TRAP_ENABLE_UNF
8080 | SWCR_TRAP_ENABLE_INE
)) << 57;
8081 fpcr
|= (swcr
& SWCR_MAP_UMZ
? FPCR_UNDZ
| FPCR_UNFD
: 0);
8082 fpcr
|= (~swcr
& SWCR_TRAP_ENABLE_DNO
) << 41;
8084 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
8089 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
8091 uint64_t exc
, fpcr
, orig_fpcr
;
8094 if (get_user_u64(exc
, arg2
)) {
8098 orig_fpcr
= cpu_alpha_load_fpcr(cpu_env
);
8100 /* We only add to the exception status here. */
8101 fpcr
= orig_fpcr
| ((exc
& SWCR_STATUS_MASK
) << 35);
8103 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
8106 /* Old exceptions are not signaled. */
8107 fpcr
&= ~(orig_fpcr
& FPCR_STATUS_MASK
);
8109 /* If any exceptions set by this call,
8110 and are unmasked, send a signal. */
8112 if ((fpcr
& (FPCR_INE
| FPCR_INED
)) == FPCR_INE
) {
8113 si_code
= TARGET_FPE_FLTRES
;
8115 if ((fpcr
& (FPCR_UNF
| FPCR_UNFD
)) == FPCR_UNF
) {
8116 si_code
= TARGET_FPE_FLTUND
;
8118 if ((fpcr
& (FPCR_OVF
| FPCR_OVFD
)) == FPCR_OVF
) {
8119 si_code
= TARGET_FPE_FLTOVF
;
8121 if ((fpcr
& (FPCR_DZE
| FPCR_DZED
)) == FPCR_DZE
) {
8122 si_code
= TARGET_FPE_FLTDIV
;
8124 if ((fpcr
& (FPCR_INV
| FPCR_INVD
)) == FPCR_INV
) {
8125 si_code
= TARGET_FPE_FLTINV
;
8128 target_siginfo_t info
;
8129 info
.si_signo
= SIGFPE
;
8131 info
.si_code
= si_code
;
8132 info
._sifields
._sigfault
._addr
8133 = ((CPUArchState
*)cpu_env
)->pc
;
8134 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
8139 /* case SSI_NVPAIRS:
8140 -- Used with SSIN_UACPROC to enable unaligned accesses.
8141 case SSI_IEEE_STATE_AT_SIGNAL:
8142 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
8143 -- Not implemented in linux kernel
8148 #ifdef TARGET_NR_osf_sigprocmask
8149 /* Alpha specific. */
8150 case TARGET_NR_osf_sigprocmask
:
8154 sigset_t set
, oldset
;
8157 case TARGET_SIG_BLOCK
:
8160 case TARGET_SIG_UNBLOCK
:
8163 case TARGET_SIG_SETMASK
:
8167 ret
= -TARGET_EINVAL
;
8171 target_to_host_old_sigset(&set
, &mask
);
8172 sigprocmask(how
, &set
, &oldset
);
8173 host_to_target_old_sigset(&mask
, &oldset
);
8179 #ifdef TARGET_NR_getgid32
8180 case TARGET_NR_getgid32
:
8181 ret
= get_errno(getgid());
8184 #ifdef TARGET_NR_geteuid32
8185 case TARGET_NR_geteuid32
:
8186 ret
= get_errno(geteuid());
8189 #ifdef TARGET_NR_getegid32
8190 case TARGET_NR_getegid32
:
8191 ret
= get_errno(getegid());
8194 #ifdef TARGET_NR_setreuid32
8195 case TARGET_NR_setreuid32
:
8196 ret
= get_errno(setreuid(arg1
, arg2
));
8199 #ifdef TARGET_NR_setregid32
8200 case TARGET_NR_setregid32
:
8201 ret
= get_errno(setregid(arg1
, arg2
));
8204 #ifdef TARGET_NR_getgroups32
8205 case TARGET_NR_getgroups32
:
8207 int gidsetsize
= arg1
;
8208 uint32_t *target_grouplist
;
8212 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8213 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
8214 if (gidsetsize
== 0)
8216 if (!is_error(ret
)) {
8217 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
8218 if (!target_grouplist
) {
8219 ret
= -TARGET_EFAULT
;
8222 for(i
= 0;i
< ret
; i
++)
8223 target_grouplist
[i
] = tswap32(grouplist
[i
]);
8224 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
8229 #ifdef TARGET_NR_setgroups32
8230 case TARGET_NR_setgroups32
:
8232 int gidsetsize
= arg1
;
8233 uint32_t *target_grouplist
;
8237 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
8238 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
8239 if (!target_grouplist
) {
8240 ret
= -TARGET_EFAULT
;
8243 for(i
= 0;i
< gidsetsize
; i
++)
8244 grouplist
[i
] = tswap32(target_grouplist
[i
]);
8245 unlock_user(target_grouplist
, arg2
, 0);
8246 ret
= get_errno(setgroups(gidsetsize
, grouplist
));
8250 #ifdef TARGET_NR_fchown32
8251 case TARGET_NR_fchown32
:
8252 ret
= get_errno(fchown(arg1
, arg2
, arg3
));
8255 #ifdef TARGET_NR_setresuid32
8256 case TARGET_NR_setresuid32
:
8257 ret
= get_errno(setresuid(arg1
, arg2
, arg3
));
8260 #ifdef TARGET_NR_getresuid32
8261 case TARGET_NR_getresuid32
:
8263 uid_t ruid
, euid
, suid
;
8264 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
8265 if (!is_error(ret
)) {
8266 if (put_user_u32(ruid
, arg1
)
8267 || put_user_u32(euid
, arg2
)
8268 || put_user_u32(suid
, arg3
))
8274 #ifdef TARGET_NR_setresgid32
8275 case TARGET_NR_setresgid32
:
8276 ret
= get_errno(setresgid(arg1
, arg2
, arg3
));
8279 #ifdef TARGET_NR_getresgid32
8280 case TARGET_NR_getresgid32
:
8282 gid_t rgid
, egid
, sgid
;
8283 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
8284 if (!is_error(ret
)) {
8285 if (put_user_u32(rgid
, arg1
)
8286 || put_user_u32(egid
, arg2
)
8287 || put_user_u32(sgid
, arg3
))
8293 #ifdef TARGET_NR_chown32
8294 case TARGET_NR_chown32
:
8295 if (!(p
= lock_user_string(arg1
)))
8297 ret
= get_errno(chown(p
, arg2
, arg3
));
8298 unlock_user(p
, arg1
, 0);
8301 #ifdef TARGET_NR_setuid32
8302 case TARGET_NR_setuid32
:
8303 ret
= get_errno(setuid(arg1
));
8306 #ifdef TARGET_NR_setgid32
8307 case TARGET_NR_setgid32
:
8308 ret
= get_errno(setgid(arg1
));
8311 #ifdef TARGET_NR_setfsuid32
8312 case TARGET_NR_setfsuid32
:
8313 ret
= get_errno(setfsuid(arg1
));
8316 #ifdef TARGET_NR_setfsgid32
8317 case TARGET_NR_setfsgid32
:
8318 ret
= get_errno(setfsgid(arg1
));
8322 case TARGET_NR_pivot_root
:
8324 #ifdef TARGET_NR_mincore
8325 case TARGET_NR_mincore
:
8328 ret
= -TARGET_EFAULT
;
8329 if (!(a
= lock_user(VERIFY_READ
, arg1
,arg2
, 0)))
8331 if (!(p
= lock_user_string(arg3
)))
8333 ret
= get_errno(mincore(a
, arg2
, p
));
8334 unlock_user(p
, arg3
, ret
);
8336 unlock_user(a
, arg1
, 0);
8340 #ifdef TARGET_NR_arm_fadvise64_64
8341 case TARGET_NR_arm_fadvise64_64
:
8344 * arm_fadvise64_64 looks like fadvise64_64 but
8345 * with different argument order
8353 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
8354 #ifdef TARGET_NR_fadvise64_64
8355 case TARGET_NR_fadvise64_64
:
8357 #ifdef TARGET_NR_fadvise64
8358 case TARGET_NR_fadvise64
:
8362 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
8363 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
8364 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
8365 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
8369 ret
= -posix_fadvise(arg1
, arg2
, arg3
, arg4
);
8372 #ifdef TARGET_NR_madvise
8373 case TARGET_NR_madvise
:
8374 /* A straight passthrough may not be safe because qemu sometimes
8375 turns private file-backed mappings into anonymous mappings.
8376 This will break MADV_DONTNEED.
8377 This is a hint, so ignoring and returning success is ok. */
8381 #if TARGET_ABI_BITS == 32
8382 case TARGET_NR_fcntl64
:
8386 struct target_flock64
*target_fl
;
8388 struct target_eabi_flock64
*target_efl
;
8391 cmd
= target_to_host_fcntl_cmd(arg2
);
8392 if (cmd
== -TARGET_EINVAL
) {
8398 case TARGET_F_GETLK64
:
8400 if (((CPUARMState
*)cpu_env
)->eabi
) {
8401 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
8403 fl
.l_type
= tswap16(target_efl
->l_type
);
8404 fl
.l_whence
= tswap16(target_efl
->l_whence
);
8405 fl
.l_start
= tswap64(target_efl
->l_start
);
8406 fl
.l_len
= tswap64(target_efl
->l_len
);
8407 fl
.l_pid
= tswap32(target_efl
->l_pid
);
8408 unlock_user_struct(target_efl
, arg3
, 0);
8412 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
8414 fl
.l_type
= tswap16(target_fl
->l_type
);
8415 fl
.l_whence
= tswap16(target_fl
->l_whence
);
8416 fl
.l_start
= tswap64(target_fl
->l_start
);
8417 fl
.l_len
= tswap64(target_fl
->l_len
);
8418 fl
.l_pid
= tswap32(target_fl
->l_pid
);
8419 unlock_user_struct(target_fl
, arg3
, 0);
8421 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
8424 if (((CPUARMState
*)cpu_env
)->eabi
) {
8425 if (!lock_user_struct(VERIFY_WRITE
, target_efl
, arg3
, 0))
8427 target_efl
->l_type
= tswap16(fl
.l_type
);
8428 target_efl
->l_whence
= tswap16(fl
.l_whence
);
8429 target_efl
->l_start
= tswap64(fl
.l_start
);
8430 target_efl
->l_len
= tswap64(fl
.l_len
);
8431 target_efl
->l_pid
= tswap32(fl
.l_pid
);
8432 unlock_user_struct(target_efl
, arg3
, 1);
8436 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, arg3
, 0))
8438 target_fl
->l_type
= tswap16(fl
.l_type
);
8439 target_fl
->l_whence
= tswap16(fl
.l_whence
);
8440 target_fl
->l_start
= tswap64(fl
.l_start
);
8441 target_fl
->l_len
= tswap64(fl
.l_len
);
8442 target_fl
->l_pid
= tswap32(fl
.l_pid
);
8443 unlock_user_struct(target_fl
, arg3
, 1);
8448 case TARGET_F_SETLK64
:
8449 case TARGET_F_SETLKW64
:
8451 if (((CPUARMState
*)cpu_env
)->eabi
) {
8452 if (!lock_user_struct(VERIFY_READ
, target_efl
, arg3
, 1))
8454 fl
.l_type
= tswap16(target_efl
->l_type
);
8455 fl
.l_whence
= tswap16(target_efl
->l_whence
);
8456 fl
.l_start
= tswap64(target_efl
->l_start
);
8457 fl
.l_len
= tswap64(target_efl
->l_len
);
8458 fl
.l_pid
= tswap32(target_efl
->l_pid
);
8459 unlock_user_struct(target_efl
, arg3
, 0);
8463 if (!lock_user_struct(VERIFY_READ
, target_fl
, arg3
, 1))
8465 fl
.l_type
= tswap16(target_fl
->l_type
);
8466 fl
.l_whence
= tswap16(target_fl
->l_whence
);
8467 fl
.l_start
= tswap64(target_fl
->l_start
);
8468 fl
.l_len
= tswap64(target_fl
->l_len
);
8469 fl
.l_pid
= tswap32(target_fl
->l_pid
);
8470 unlock_user_struct(target_fl
, arg3
, 0);
8472 ret
= get_errno(fcntl(arg1
, cmd
, &fl
));
8475 ret
= do_fcntl(arg1
, arg2
, arg3
);
8481 #ifdef TARGET_NR_cacheflush
8482 case TARGET_NR_cacheflush
:
8483 /* self-modifying code is handled automatically, so nothing needed */
8487 #ifdef TARGET_NR_security
8488 case TARGET_NR_security
:
8491 #ifdef TARGET_NR_getpagesize
8492 case TARGET_NR_getpagesize
:
8493 ret
= TARGET_PAGE_SIZE
;
8496 case TARGET_NR_gettid
:
8497 ret
= get_errno(gettid());
8499 #ifdef TARGET_NR_readahead
8500 case TARGET_NR_readahead
:
8501 #if TARGET_ABI_BITS == 32
8502 if (regpairs_aligned(cpu_env
)) {
8507 ret
= get_errno(readahead(arg1
, ((off64_t
)arg3
<< 32) | arg2
, arg4
));
8509 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
8514 #ifdef TARGET_NR_setxattr
8515 case TARGET_NR_listxattr
:
8516 case TARGET_NR_llistxattr
:
8520 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
8522 ret
= -TARGET_EFAULT
;
8526 p
= lock_user_string(arg1
);
8528 if (num
== TARGET_NR_listxattr
) {
8529 ret
= get_errno(listxattr(p
, b
, arg3
));
8531 ret
= get_errno(llistxattr(p
, b
, arg3
));
8534 ret
= -TARGET_EFAULT
;
8536 unlock_user(p
, arg1
, 0);
8537 unlock_user(b
, arg2
, arg3
);
8540 case TARGET_NR_flistxattr
:
8544 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
8546 ret
= -TARGET_EFAULT
;
8550 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
8551 unlock_user(b
, arg2
, arg3
);
8554 case TARGET_NR_setxattr
:
8555 case TARGET_NR_lsetxattr
:
8557 void *p
, *n
, *v
= 0;
8559 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
8561 ret
= -TARGET_EFAULT
;
8565 p
= lock_user_string(arg1
);
8566 n
= lock_user_string(arg2
);
8568 if (num
== TARGET_NR_setxattr
) {
8569 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
8571 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
8574 ret
= -TARGET_EFAULT
;
8576 unlock_user(p
, arg1
, 0);
8577 unlock_user(n
, arg2
, 0);
8578 unlock_user(v
, arg3
, 0);
8581 case TARGET_NR_fsetxattr
:
8585 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
8587 ret
= -TARGET_EFAULT
;
8591 n
= lock_user_string(arg2
);
8593 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
8595 ret
= -TARGET_EFAULT
;
8597 unlock_user(n
, arg2
, 0);
8598 unlock_user(v
, arg3
, 0);
8601 case TARGET_NR_getxattr
:
8602 case TARGET_NR_lgetxattr
:
8604 void *p
, *n
, *v
= 0;
8606 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
8608 ret
= -TARGET_EFAULT
;
8612 p
= lock_user_string(arg1
);
8613 n
= lock_user_string(arg2
);
8615 if (num
== TARGET_NR_getxattr
) {
8616 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
8618 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
8621 ret
= -TARGET_EFAULT
;
8623 unlock_user(p
, arg1
, 0);
8624 unlock_user(n
, arg2
, 0);
8625 unlock_user(v
, arg3
, arg4
);
8628 case TARGET_NR_fgetxattr
:
8632 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
8634 ret
= -TARGET_EFAULT
;
8638 n
= lock_user_string(arg2
);
8640 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
8642 ret
= -TARGET_EFAULT
;
8644 unlock_user(n
, arg2
, 0);
8645 unlock_user(v
, arg3
, arg4
);
8648 case TARGET_NR_removexattr
:
8649 case TARGET_NR_lremovexattr
:
8652 p
= lock_user_string(arg1
);
8653 n
= lock_user_string(arg2
);
8655 if (num
== TARGET_NR_removexattr
) {
8656 ret
= get_errno(removexattr(p
, n
));
8658 ret
= get_errno(lremovexattr(p
, n
));
8661 ret
= -TARGET_EFAULT
;
8663 unlock_user(p
, arg1
, 0);
8664 unlock_user(n
, arg2
, 0);
8667 case TARGET_NR_fremovexattr
:
8670 n
= lock_user_string(arg2
);
8672 ret
= get_errno(fremovexattr(arg1
, n
));
8674 ret
= -TARGET_EFAULT
;
8676 unlock_user(n
, arg2
, 0);
8680 #endif /* CONFIG_ATTR */
8681 #ifdef TARGET_NR_set_thread_area
8682 case TARGET_NR_set_thread_area
:
8683 #if defined(TARGET_MIPS)
8684 ((CPUMIPSState
*) cpu_env
)->tls_value
= arg1
;
8687 #elif defined(TARGET_CRIS)
8689 ret
= -TARGET_EINVAL
;
8691 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
8695 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
8696 ret
= do_set_thread_area(cpu_env
, arg1
);
8698 #elif defined(TARGET_M68K)
8700 TaskState
*ts
= ((CPUArchState
*)cpu_env
)->opaque
;
8701 ts
->tp_value
= arg1
;
8706 goto unimplemented_nowarn
;
8709 #ifdef TARGET_NR_get_thread_area
8710 case TARGET_NR_get_thread_area
:
8711 #if defined(TARGET_I386) && defined(TARGET_ABI32)
8712 ret
= do_get_thread_area(cpu_env
, arg1
);
8714 #elif defined(TARGET_M68K)
8716 TaskState
*ts
= ((CPUArchState
*)cpu_env
)->opaque
;
8721 goto unimplemented_nowarn
;
8724 #ifdef TARGET_NR_getdomainname
8725 case TARGET_NR_getdomainname
:
8726 goto unimplemented_nowarn
;
8729 #ifdef TARGET_NR_clock_gettime
8730 case TARGET_NR_clock_gettime
:
8733 ret
= get_errno(clock_gettime(arg1
, &ts
));
8734 if (!is_error(ret
)) {
8735 host_to_target_timespec(arg2
, &ts
);
8740 #ifdef TARGET_NR_clock_getres
8741 case TARGET_NR_clock_getres
:
8744 ret
= get_errno(clock_getres(arg1
, &ts
));
8745 if (!is_error(ret
)) {
8746 host_to_target_timespec(arg2
, &ts
);
8751 #ifdef TARGET_NR_clock_nanosleep
8752 case TARGET_NR_clock_nanosleep
:
8755 target_to_host_timespec(&ts
, arg3
);
8756 ret
= get_errno(clock_nanosleep(arg1
, arg2
, &ts
, arg4
? &ts
: NULL
));
8758 host_to_target_timespec(arg4
, &ts
);
8763 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
8764 case TARGET_NR_set_tid_address
:
8765 ret
= get_errno(set_tid_address((int *)g2h(arg1
)));
8769 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
8770 case TARGET_NR_tkill
:
8771 ret
= get_errno(sys_tkill((int)arg1
, target_to_host_signal(arg2
)));
8775 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
8776 case TARGET_NR_tgkill
:
8777 ret
= get_errno(sys_tgkill((int)arg1
, (int)arg2
,
8778 target_to_host_signal(arg3
)));
8782 #ifdef TARGET_NR_set_robust_list
8783 case TARGET_NR_set_robust_list
:
8784 case TARGET_NR_get_robust_list
:
8785 /* The ABI for supporting robust futexes has userspace pass
8786 * the kernel a pointer to a linked list which is updated by
8787 * userspace after the syscall; the list is walked by the kernel
8788 * when the thread exits. Since the linked list in QEMU guest
8789 * memory isn't a valid linked list for the host and we have
8790 * no way to reliably intercept the thread-death event, we can't
8791 * support these. Silently return ENOSYS so that guest userspace
8792 * falls back to a non-robust futex implementation (which should
8793 * be OK except in the corner case of the guest crashing while
8794 * holding a mutex that is shared with another process via
8797 goto unimplemented_nowarn
;
8800 #if defined(TARGET_NR_utimensat)
8801 case TARGET_NR_utimensat
:
8803 struct timespec
*tsp
, ts
[2];
8807 target_to_host_timespec(ts
, arg3
);
8808 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
8812 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
8814 if (!(p
= lock_user_string(arg2
))) {
8815 ret
= -TARGET_EFAULT
;
8818 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
8819 unlock_user(p
, arg2
, 0);
8824 case TARGET_NR_futex
:
8825 ret
= do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
8827 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
8828 case TARGET_NR_inotify_init
:
8829 ret
= get_errno(sys_inotify_init());
8832 #ifdef CONFIG_INOTIFY1
8833 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
8834 case TARGET_NR_inotify_init1
:
8835 ret
= get_errno(sys_inotify_init1(arg1
));
8839 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
8840 case TARGET_NR_inotify_add_watch
:
8841 p
= lock_user_string(arg2
);
8842 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
8843 unlock_user(p
, arg2
, 0);
8846 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
8847 case TARGET_NR_inotify_rm_watch
:
8848 ret
= get_errno(sys_inotify_rm_watch(arg1
, arg2
));
8852 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
8853 case TARGET_NR_mq_open
:
8855 struct mq_attr posix_mq_attr
;
8857 p
= lock_user_string(arg1
- 1);
8859 copy_from_user_mq_attr (&posix_mq_attr
, arg4
);
8860 ret
= get_errno(mq_open(p
, arg2
, arg3
, &posix_mq_attr
));
8861 unlock_user (p
, arg1
, 0);
8865 case TARGET_NR_mq_unlink
:
8866 p
= lock_user_string(arg1
- 1);
8867 ret
= get_errno(mq_unlink(p
));
8868 unlock_user (p
, arg1
, 0);
8871 case TARGET_NR_mq_timedsend
:
8875 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
8877 target_to_host_timespec(&ts
, arg5
);
8878 ret
= get_errno(mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
8879 host_to_target_timespec(arg5
, &ts
);
8882 ret
= get_errno(mq_send(arg1
, p
, arg3
, arg4
));
8883 unlock_user (p
, arg2
, arg3
);
8887 case TARGET_NR_mq_timedreceive
:
8892 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
8894 target_to_host_timespec(&ts
, arg5
);
8895 ret
= get_errno(mq_timedreceive(arg1
, p
, arg3
, &prio
, &ts
));
8896 host_to_target_timespec(arg5
, &ts
);
8899 ret
= get_errno(mq_receive(arg1
, p
, arg3
, &prio
));
8900 unlock_user (p
, arg2
, arg3
);
8902 put_user_u32(prio
, arg4
);
8906 /* Not implemented for now... */
8907 /* case TARGET_NR_mq_notify: */
8910 case TARGET_NR_mq_getsetattr
:
8912 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
8915 ret
= mq_getattr(arg1
, &posix_mq_attr_out
);
8916 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
8919 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
8920 ret
|= mq_setattr(arg1
, &posix_mq_attr_in
, &posix_mq_attr_out
);
8927 #ifdef CONFIG_SPLICE
8928 #ifdef TARGET_NR_tee
8931 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
8935 #ifdef TARGET_NR_splice
8936 case TARGET_NR_splice
:
8938 loff_t loff_in
, loff_out
;
8939 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
8941 get_user_u64(loff_in
, arg2
);
8942 ploff_in
= &loff_in
;
8945 get_user_u64(loff_out
, arg2
);
8946 ploff_out
= &loff_out
;
8948 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
8952 #ifdef TARGET_NR_vmsplice
8953 case TARGET_NR_vmsplice
:
8955 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
8957 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
8958 unlock_iovec(vec
, arg2
, arg3
, 0);
8960 ret
= -host_to_target_errno(errno
);
8965 #endif /* CONFIG_SPLICE */
8966 #ifdef CONFIG_EVENTFD
8967 #if defined(TARGET_NR_eventfd)
8968 case TARGET_NR_eventfd
:
8969 ret
= get_errno(eventfd(arg1
, 0));
8972 #if defined(TARGET_NR_eventfd2)
8973 case TARGET_NR_eventfd2
:
8975 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
));
8976 if (arg2
& TARGET_O_NONBLOCK
) {
8977 host_flags
|= O_NONBLOCK
;
8979 if (arg2
& TARGET_O_CLOEXEC
) {
8980 host_flags
|= O_CLOEXEC
;
8982 ret
= get_errno(eventfd(arg1
, host_flags
));
8986 #endif /* CONFIG_EVENTFD */
8987 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
8988 case TARGET_NR_fallocate
:
8989 #if TARGET_ABI_BITS == 32
8990 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
8991 target_offset64(arg5
, arg6
)));
8993 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
8997 #if defined(CONFIG_SYNC_FILE_RANGE)
8998 #if defined(TARGET_NR_sync_file_range)
8999 case TARGET_NR_sync_file_range
:
9000 #if TARGET_ABI_BITS == 32
9001 #if defined(TARGET_MIPS)
9002 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
9003 target_offset64(arg5
, arg6
), arg7
));
9005 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
9006 target_offset64(arg4
, arg5
), arg6
));
9007 #endif /* !TARGET_MIPS */
9009 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
9013 #if defined(TARGET_NR_sync_file_range2)
9014 case TARGET_NR_sync_file_range2
:
9015 /* This is like sync_file_range but the arguments are reordered */
9016 #if TARGET_ABI_BITS == 32
9017 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
9018 target_offset64(arg5
, arg6
), arg2
));
9020 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
9025 #if defined(CONFIG_EPOLL)
9026 #if defined(TARGET_NR_epoll_create)
9027 case TARGET_NR_epoll_create
:
9028 ret
= get_errno(epoll_create(arg1
));
9031 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
9032 case TARGET_NR_epoll_create1
:
9033 ret
= get_errno(epoll_create1(arg1
));
9036 #if defined(TARGET_NR_epoll_ctl)
9037 case TARGET_NR_epoll_ctl
:
9039 struct epoll_event ep
;
9040 struct epoll_event
*epp
= 0;
9042 struct target_epoll_event
*target_ep
;
9043 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
9046 ep
.events
= tswap32(target_ep
->events
);
9047 /* The epoll_data_t union is just opaque data to the kernel,
9048 * so we transfer all 64 bits across and need not worry what
9049 * actual data type it is.
9051 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
9052 unlock_user_struct(target_ep
, arg4
, 0);
9055 ret
= get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
9060 #if defined(TARGET_NR_epoll_pwait) && defined(CONFIG_EPOLL_PWAIT)
9061 #define IMPLEMENT_EPOLL_PWAIT
9063 #if defined(TARGET_NR_epoll_wait) || defined(IMPLEMENT_EPOLL_PWAIT)
9064 #if defined(TARGET_NR_epoll_wait)
9065 case TARGET_NR_epoll_wait
:
9067 #if defined(IMPLEMENT_EPOLL_PWAIT)
9068 case TARGET_NR_epoll_pwait
:
9071 struct target_epoll_event
*target_ep
;
9072 struct epoll_event
*ep
;
9074 int maxevents
= arg3
;
9077 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
9078 maxevents
* sizeof(struct target_epoll_event
), 1);
9083 ep
= alloca(maxevents
* sizeof(struct epoll_event
));
9086 #if defined(IMPLEMENT_EPOLL_PWAIT)
9087 case TARGET_NR_epoll_pwait
:
9089 target_sigset_t
*target_set
;
9090 sigset_t _set
, *set
= &_set
;
9093 target_set
= lock_user(VERIFY_READ
, arg5
,
9094 sizeof(target_sigset_t
), 1);
9096 unlock_user(target_ep
, arg2
, 0);
9099 target_to_host_sigset(set
, target_set
);
9100 unlock_user(target_set
, arg5
, 0);
9105 ret
= get_errno(epoll_pwait(epfd
, ep
, maxevents
, timeout
, set
));
9109 #if defined(TARGET_NR_epoll_wait)
9110 case TARGET_NR_epoll_wait
:
9111 ret
= get_errno(epoll_wait(epfd
, ep
, maxevents
, timeout
));
9115 ret
= -TARGET_ENOSYS
;
9117 if (!is_error(ret
)) {
9119 for (i
= 0; i
< ret
; i
++) {
9120 target_ep
[i
].events
= tswap32(ep
[i
].events
);
9121 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
9124 unlock_user(target_ep
, arg2
, ret
* sizeof(struct target_epoll_event
));
9129 #ifdef TARGET_NR_prlimit64
9130 case TARGET_NR_prlimit64
:
9132 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
9133 struct target_rlimit64
*target_rnew
, *target_rold
;
9134 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
9136 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
9139 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
9140 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
9141 unlock_user_struct(target_rnew
, arg3
, 0);
9145 ret
= get_errno(sys_prlimit64(arg1
, arg2
, rnewp
, arg4
? &rold
: 0));
9146 if (!is_error(ret
) && arg4
) {
9147 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
9150 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
9151 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
9152 unlock_user_struct(target_rold
, arg4
, 1);
9157 #ifdef TARGET_NR_gethostname
9158 case TARGET_NR_gethostname
:
9160 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
9162 ret
= get_errno(gethostname(name
, arg2
));
9163 unlock_user(name
, arg1
, arg2
);
9165 ret
= -TARGET_EFAULT
;
9170 #ifdef TARGET_NR_atomic_cmpxchg_32
9171 case TARGET_NR_atomic_cmpxchg_32
:
9173 /* should use start_exclusive from main.c */
9174 abi_ulong mem_value
;
9175 if (get_user_u32(mem_value
, arg6
)) {
9176 target_siginfo_t info
;
9177 info
.si_signo
= SIGSEGV
;
9179 info
.si_code
= TARGET_SEGV_MAPERR
;
9180 info
._sifields
._sigfault
._addr
= arg6
;
9181 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
, &info
);
9185 if (mem_value
== arg2
)
9186 put_user_u32(arg1
, arg6
);
9191 #ifdef TARGET_NR_atomic_barrier
9192 case TARGET_NR_atomic_barrier
:
9194 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
9200 gemu_log("qemu: Unsupported syscall: %d\n", num
);
9201 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
9202 unimplemented_nowarn
:
9204 ret
= -TARGET_ENOSYS
;
9209 gemu_log(" = " TARGET_ABI_FMT_ld
"\n", ret
);
9212 print_syscall_ret(num
, ret
);
9215 ret
= -TARGET_EFAULT
;