4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _ATFILE_SOURCE
20 #include "qemu/osdep.h"
21 #include "qemu/cutils.h"
22 #include "qemu/path.h"
23 #include "qemu/memfd.h"
24 #include "qemu/queue.h"
31 #include <sys/mount.h>
33 #include <sys/fsuid.h>
34 #include <sys/personality.h>
35 #include <sys/prctl.h>
36 #include <sys/resource.h>
38 #include <linux/capability.h>
40 #include <sys/timex.h>
41 #include <sys/socket.h>
42 #include <linux/sockios.h>
46 #include <sys/times.h>
49 #include <sys/statfs.h>
51 #include <sys/sysinfo.h>
52 #include <sys/signalfd.h>
53 //#include <sys/user.h>
54 #include <netinet/ip.h>
55 #include <netinet/tcp.h>
56 #include <linux/wireless.h>
57 #include <linux/icmp.h>
58 #include <linux/icmpv6.h>
59 #include <linux/errqueue.h>
60 #include <linux/random.h>
62 #include <sys/timerfd.h>
65 #include <sys/eventfd.h>
68 #include <sys/epoll.h>
71 #include "qemu/xattr.h"
73 #ifdef CONFIG_SENDFILE
74 #include <sys/sendfile.h>
80 #define termios host_termios
81 #define winsize host_winsize
82 #define termio host_termio
83 #define sgttyb host_sgttyb /* same as target */
84 #define tchars host_tchars /* same as target */
85 #define ltchars host_ltchars /* same as target */
87 #include <linux/termios.h>
88 #include <linux/unistd.h>
89 #include <linux/cdrom.h>
90 #include <linux/hdreg.h>
91 #include <linux/soundcard.h>
93 #include <linux/mtio.h>
96 #if defined(CONFIG_FIEMAP)
97 #include <linux/fiemap.h>
100 #if defined(CONFIG_USBFS)
101 #include <linux/usbdevice_fs.h>
102 #include <linux/usb/ch9.h>
104 #include <linux/vt.h>
105 #include <linux/dm-ioctl.h>
106 #include <linux/reboot.h>
107 #include <linux/route.h>
108 #include <linux/filter.h>
109 #include <linux/blkpg.h>
110 #include <netpacket/packet.h>
111 #include <linux/netlink.h>
112 #include <linux/if_alg.h>
113 #include <linux/rtc.h>
114 #include <sound/asound.h>
115 #include "linux_loop.h"
119 #include "qemu/guest-random.h"
120 #include "user/syscall-trace.h"
121 #include "qapi/error.h"
122 #include "fd-trans.h"
126 #define CLONE_IO 0x80000000 /* Clone io context */
129 /* We can't directly call the host clone syscall, because this will
130 * badly confuse libc (breaking mutexes, for example). So we must
131 * divide clone flags into:
132 * * flag combinations that look like pthread_create()
133 * * flag combinations that look like fork()
134 * * flags we can implement within QEMU itself
135 * * flags we can't support and will return an error for
137 /* For thread creation, all these flags must be present; for
138 * fork, none must be present.
140 #define CLONE_THREAD_FLAGS \
141 (CLONE_VM | CLONE_FS | CLONE_FILES | \
142 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
144 /* These flags are ignored:
145 * CLONE_DETACHED is now ignored by the kernel;
146 * CLONE_IO is just an optimisation hint to the I/O scheduler
148 #define CLONE_IGNORED_FLAGS \
149 (CLONE_DETACHED | CLONE_IO)
151 /* Flags for fork which we can implement within QEMU itself */
152 #define CLONE_OPTIONAL_FORK_FLAGS \
153 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
154 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
156 /* Flags for thread creation which we can implement within QEMU itself */
157 #define CLONE_OPTIONAL_THREAD_FLAGS \
158 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
159 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
161 #define CLONE_INVALID_FORK_FLAGS \
162 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
164 #define CLONE_INVALID_THREAD_FLAGS \
165 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
166 CLONE_IGNORED_FLAGS))
168 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
169 * have almost all been allocated. We cannot support any of
170 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
171 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
172 * The checks against the invalid thread masks above will catch these.
173 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
176 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
177 * once. This exercises the codepaths for restart.
179 //#define DEBUG_ERESTARTSYS
181 //#include <linux/msdos_fs.h>
182 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
183 #define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
193 #define _syscall0(type,name) \
194 static type name (void) \
196 return syscall(__NR_##name); \
199 #define _syscall1(type,name,type1,arg1) \
200 static type name (type1 arg1) \
202 return syscall(__NR_##name, arg1); \
205 #define _syscall2(type,name,type1,arg1,type2,arg2) \
206 static type name (type1 arg1,type2 arg2) \
208 return syscall(__NR_##name, arg1, arg2); \
211 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
212 static type name (type1 arg1,type2 arg2,type3 arg3) \
214 return syscall(__NR_##name, arg1, arg2, arg3); \
217 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
218 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
220 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
223 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
225 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
227 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
231 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
232 type5,arg5,type6,arg6) \
233 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
236 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
240 #define __NR_sys_uname __NR_uname
241 #define __NR_sys_getcwd1 __NR_getcwd
242 #define __NR_sys_getdents __NR_getdents
243 #define __NR_sys_getdents64 __NR_getdents64
244 #define __NR_sys_getpriority __NR_getpriority
245 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
246 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
247 #define __NR_sys_syslog __NR_syslog
248 #define __NR_sys_futex __NR_futex
249 #define __NR_sys_inotify_init __NR_inotify_init
250 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
251 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
252 #define __NR_sys_statx __NR_statx
254 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
255 #define __NR__llseek __NR_lseek
258 /* Newer kernel ports have llseek() instead of _llseek() */
259 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
260 #define TARGET_NR__llseek TARGET_NR_llseek
263 #define __NR_sys_gettid __NR_gettid
264 _syscall0(int, sys_gettid
)
266 /* For the 64-bit guest on 32-bit host case we must emulate
267 * getdents using getdents64, because otherwise the host
268 * might hand us back more dirent records than we can fit
269 * into the guest buffer after structure format conversion.
270 * Otherwise we emulate getdents with getdents if the host has it.
272 #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
273 #define EMULATE_GETDENTS_WITH_GETDENTS
276 #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
277 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
279 #if (defined(TARGET_NR_getdents) && \
280 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
281 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
282 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
284 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
285 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
286 loff_t
*, res
, uint
, wh
);
288 _syscall3(int, sys_rt_sigqueueinfo
, pid_t
, pid
, int, sig
, siginfo_t
*, uinfo
)
289 _syscall4(int, sys_rt_tgsigqueueinfo
, pid_t
, pid
, pid_t
, tid
, int, sig
,
291 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
292 #ifdef __NR_exit_group
293 _syscall1(int,exit_group
,int,error_code
)
295 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
296 _syscall1(int,set_tid_address
,int *,tidptr
)
298 #if defined(TARGET_NR_futex) && defined(__NR_futex)
299 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
300 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
302 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
303 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
304 unsigned long *, user_mask_ptr
);
305 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
306 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
307 unsigned long *, user_mask_ptr
);
308 #define __NR_sys_getcpu __NR_getcpu
309 _syscall3(int, sys_getcpu
, unsigned *, cpu
, unsigned *, node
, void *, tcache
);
310 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
312 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
313 struct __user_cap_data_struct
*, data
);
314 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
315 struct __user_cap_data_struct
*, data
);
316 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
317 _syscall2(int, ioprio_get
, int, which
, int, who
)
319 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
320 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
322 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
323 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
326 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
327 _syscall5(int, kcmp
, pid_t
, pid1
, pid_t
, pid2
, int, type
,
328 unsigned long, idx1
, unsigned long, idx2
)
332 * It is assumed that struct statx is architecture independent.
334 #if defined(TARGET_NR_statx) && defined(__NR_statx)
335 _syscall5(int, sys_statx
, int, dirfd
, const char *, pathname
, int, flags
,
336 unsigned int, mask
, struct target_statx
*, statxbuf
)
338 #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
339 _syscall2(int, membarrier
, int, cmd
, int, flags
)
342 static bitmask_transtbl fcntl_flags_tbl
[] = {
343 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
344 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
345 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
346 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
347 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
348 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
349 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
350 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
351 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
352 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
353 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
354 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
355 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
356 #if defined(O_DIRECT)
357 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
359 #if defined(O_NOATIME)
360 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
362 #if defined(O_CLOEXEC)
363 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
366 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
368 #if defined(O_TMPFILE)
369 { TARGET_O_TMPFILE
, TARGET_O_TMPFILE
, O_TMPFILE
, O_TMPFILE
},
371 /* Don't terminate the list prematurely on 64-bit host+guest. */
372 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
373 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
378 static int sys_getcwd1(char *buf
, size_t size
)
380 if (getcwd(buf
, size
) == NULL
) {
381 /* getcwd() sets errno */
384 return strlen(buf
)+1;
387 #ifdef TARGET_NR_utimensat
388 #if defined(__NR_utimensat)
389 #define __NR_sys_utimensat __NR_utimensat
390 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
391 const struct timespec
*,tsp
,int,flags
)
393 static int sys_utimensat(int dirfd
, const char *pathname
,
394 const struct timespec times
[2], int flags
)
400 #endif /* TARGET_NR_utimensat */
402 #ifdef TARGET_NR_renameat2
403 #if defined(__NR_renameat2)
404 #define __NR_sys_renameat2 __NR_renameat2
405 _syscall5(int, sys_renameat2
, int, oldfd
, const char *, old
, int, newfd
,
406 const char *, new, unsigned int, flags
)
408 static int sys_renameat2(int oldfd
, const char *old
,
409 int newfd
, const char *new, int flags
)
412 return renameat(oldfd
, old
, newfd
, new);
418 #endif /* TARGET_NR_renameat2 */
420 #ifdef CONFIG_INOTIFY
421 #include <sys/inotify.h>
423 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
424 static int sys_inotify_init(void)
426 return (inotify_init());
429 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
430 static int sys_inotify_add_watch(int fd
,const char *pathname
, int32_t mask
)
432 return (inotify_add_watch(fd
, pathname
, mask
));
435 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
436 static int sys_inotify_rm_watch(int fd
, int32_t wd
)
438 return (inotify_rm_watch(fd
, wd
));
441 #ifdef CONFIG_INOTIFY1
442 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
443 static int sys_inotify_init1(int flags
)
445 return (inotify_init1(flags
));
450 /* Userspace can usually survive runtime without inotify */
451 #undef TARGET_NR_inotify_init
452 #undef TARGET_NR_inotify_init1
453 #undef TARGET_NR_inotify_add_watch
454 #undef TARGET_NR_inotify_rm_watch
455 #endif /* CONFIG_INOTIFY */
457 #if defined(TARGET_NR_prlimit64)
458 #ifndef __NR_prlimit64
459 # define __NR_prlimit64 -1
461 #define __NR_sys_prlimit64 __NR_prlimit64
462 /* The glibc rlimit structure may not be that used by the underlying syscall */
463 struct host_rlimit64
{
467 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
468 const struct host_rlimit64
*, new_limit
,
469 struct host_rlimit64
*, old_limit
)
473 #if defined(TARGET_NR_timer_create)
474 /* Maxiumum of 32 active POSIX timers allowed at any one time. */
475 static timer_t g_posix_timers
[32] = { 0, } ;
477 static inline int next_free_host_timer(void)
480 /* FIXME: Does finding the next free slot require a lock? */
481 for (k
= 0; k
< ARRAY_SIZE(g_posix_timers
); k
++) {
482 if (g_posix_timers
[k
] == 0) {
483 g_posix_timers
[k
] = (timer_t
) 1;
491 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
493 static inline int regpairs_aligned(void *cpu_env
, int num
)
495 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
497 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
498 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
499 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
500 /* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
501 * of registers which translates to the same as ARM/MIPS, because we start with
503 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
504 #elif defined(TARGET_SH4)
505 /* SH4 doesn't align register pairs, except for p{read,write}64 */
506 static inline int regpairs_aligned(void *cpu_env
, int num
)
509 case TARGET_NR_pread64
:
510 case TARGET_NR_pwrite64
:
517 #elif defined(TARGET_XTENSA)
518 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 1; }
520 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 0; }
523 #define ERRNO_TABLE_SIZE 1200
525 /* target_to_host_errno_table[] is initialized from
526 * host_to_target_errno_table[] in syscall_init(). */
527 static uint16_t target_to_host_errno_table
[ERRNO_TABLE_SIZE
] = {
531 * This list is the union of errno values overridden in asm-<arch>/errno.h
532 * minus the errnos that are not actually generic to all archs.
534 static uint16_t host_to_target_errno_table
[ERRNO_TABLE_SIZE
] = {
535 [EAGAIN
] = TARGET_EAGAIN
,
536 [EIDRM
] = TARGET_EIDRM
,
537 [ECHRNG
] = TARGET_ECHRNG
,
538 [EL2NSYNC
] = TARGET_EL2NSYNC
,
539 [EL3HLT
] = TARGET_EL3HLT
,
540 [EL3RST
] = TARGET_EL3RST
,
541 [ELNRNG
] = TARGET_ELNRNG
,
542 [EUNATCH
] = TARGET_EUNATCH
,
543 [ENOCSI
] = TARGET_ENOCSI
,
544 [EL2HLT
] = TARGET_EL2HLT
,
545 [EDEADLK
] = TARGET_EDEADLK
,
546 [ENOLCK
] = TARGET_ENOLCK
,
547 [EBADE
] = TARGET_EBADE
,
548 [EBADR
] = TARGET_EBADR
,
549 [EXFULL
] = TARGET_EXFULL
,
550 [ENOANO
] = TARGET_ENOANO
,
551 [EBADRQC
] = TARGET_EBADRQC
,
552 [EBADSLT
] = TARGET_EBADSLT
,
553 [EBFONT
] = TARGET_EBFONT
,
554 [ENOSTR
] = TARGET_ENOSTR
,
555 [ENODATA
] = TARGET_ENODATA
,
556 [ETIME
] = TARGET_ETIME
,
557 [ENOSR
] = TARGET_ENOSR
,
558 [ENONET
] = TARGET_ENONET
,
559 [ENOPKG
] = TARGET_ENOPKG
,
560 [EREMOTE
] = TARGET_EREMOTE
,
561 [ENOLINK
] = TARGET_ENOLINK
,
562 [EADV
] = TARGET_EADV
,
563 [ESRMNT
] = TARGET_ESRMNT
,
564 [ECOMM
] = TARGET_ECOMM
,
565 [EPROTO
] = TARGET_EPROTO
,
566 [EDOTDOT
] = TARGET_EDOTDOT
,
567 [EMULTIHOP
] = TARGET_EMULTIHOP
,
568 [EBADMSG
] = TARGET_EBADMSG
,
569 [ENAMETOOLONG
] = TARGET_ENAMETOOLONG
,
570 [EOVERFLOW
] = TARGET_EOVERFLOW
,
571 [ENOTUNIQ
] = TARGET_ENOTUNIQ
,
572 [EBADFD
] = TARGET_EBADFD
,
573 [EREMCHG
] = TARGET_EREMCHG
,
574 [ELIBACC
] = TARGET_ELIBACC
,
575 [ELIBBAD
] = TARGET_ELIBBAD
,
576 [ELIBSCN
] = TARGET_ELIBSCN
,
577 [ELIBMAX
] = TARGET_ELIBMAX
,
578 [ELIBEXEC
] = TARGET_ELIBEXEC
,
579 [EILSEQ
] = TARGET_EILSEQ
,
580 [ENOSYS
] = TARGET_ENOSYS
,
581 [ELOOP
] = TARGET_ELOOP
,
582 [ERESTART
] = TARGET_ERESTART
,
583 [ESTRPIPE
] = TARGET_ESTRPIPE
,
584 [ENOTEMPTY
] = TARGET_ENOTEMPTY
,
585 [EUSERS
] = TARGET_EUSERS
,
586 [ENOTSOCK
] = TARGET_ENOTSOCK
,
587 [EDESTADDRREQ
] = TARGET_EDESTADDRREQ
,
588 [EMSGSIZE
] = TARGET_EMSGSIZE
,
589 [EPROTOTYPE
] = TARGET_EPROTOTYPE
,
590 [ENOPROTOOPT
] = TARGET_ENOPROTOOPT
,
591 [EPROTONOSUPPORT
] = TARGET_EPROTONOSUPPORT
,
592 [ESOCKTNOSUPPORT
] = TARGET_ESOCKTNOSUPPORT
,
593 [EOPNOTSUPP
] = TARGET_EOPNOTSUPP
,
594 [EPFNOSUPPORT
] = TARGET_EPFNOSUPPORT
,
595 [EAFNOSUPPORT
] = TARGET_EAFNOSUPPORT
,
596 [EADDRINUSE
] = TARGET_EADDRINUSE
,
597 [EADDRNOTAVAIL
] = TARGET_EADDRNOTAVAIL
,
598 [ENETDOWN
] = TARGET_ENETDOWN
,
599 [ENETUNREACH
] = TARGET_ENETUNREACH
,
600 [ENETRESET
] = TARGET_ENETRESET
,
601 [ECONNABORTED
] = TARGET_ECONNABORTED
,
602 [ECONNRESET
] = TARGET_ECONNRESET
,
603 [ENOBUFS
] = TARGET_ENOBUFS
,
604 [EISCONN
] = TARGET_EISCONN
,
605 [ENOTCONN
] = TARGET_ENOTCONN
,
606 [EUCLEAN
] = TARGET_EUCLEAN
,
607 [ENOTNAM
] = TARGET_ENOTNAM
,
608 [ENAVAIL
] = TARGET_ENAVAIL
,
609 [EISNAM
] = TARGET_EISNAM
,
610 [EREMOTEIO
] = TARGET_EREMOTEIO
,
611 [EDQUOT
] = TARGET_EDQUOT
,
612 [ESHUTDOWN
] = TARGET_ESHUTDOWN
,
613 [ETOOMANYREFS
] = TARGET_ETOOMANYREFS
,
614 [ETIMEDOUT
] = TARGET_ETIMEDOUT
,
615 [ECONNREFUSED
] = TARGET_ECONNREFUSED
,
616 [EHOSTDOWN
] = TARGET_EHOSTDOWN
,
617 [EHOSTUNREACH
] = TARGET_EHOSTUNREACH
,
618 [EALREADY
] = TARGET_EALREADY
,
619 [EINPROGRESS
] = TARGET_EINPROGRESS
,
620 [ESTALE
] = TARGET_ESTALE
,
621 [ECANCELED
] = TARGET_ECANCELED
,
622 [ENOMEDIUM
] = TARGET_ENOMEDIUM
,
623 [EMEDIUMTYPE
] = TARGET_EMEDIUMTYPE
,
625 [ENOKEY
] = TARGET_ENOKEY
,
628 [EKEYEXPIRED
] = TARGET_EKEYEXPIRED
,
631 [EKEYREVOKED
] = TARGET_EKEYREVOKED
,
634 [EKEYREJECTED
] = TARGET_EKEYREJECTED
,
637 [EOWNERDEAD
] = TARGET_EOWNERDEAD
,
639 #ifdef ENOTRECOVERABLE
640 [ENOTRECOVERABLE
] = TARGET_ENOTRECOVERABLE
,
643 [ENOMSG
] = TARGET_ENOMSG
,
646 [ERFKILL
] = TARGET_ERFKILL
,
649 [EHWPOISON
] = TARGET_EHWPOISON
,
653 static inline int host_to_target_errno(int err
)
655 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
656 host_to_target_errno_table
[err
]) {
657 return host_to_target_errno_table
[err
];
662 static inline int target_to_host_errno(int err
)
664 if (err
>= 0 && err
< ERRNO_TABLE_SIZE
&&
665 target_to_host_errno_table
[err
]) {
666 return target_to_host_errno_table
[err
];
671 static inline abi_long
get_errno(abi_long ret
)
674 return -host_to_target_errno(errno
);
679 const char *target_strerror(int err
)
681 if (err
== TARGET_ERESTARTSYS
) {
682 return "To be restarted";
684 if (err
== TARGET_QEMU_ESIGRETURN
) {
685 return "Successful exit from sigreturn";
688 if ((err
>= ERRNO_TABLE_SIZE
) || (err
< 0)) {
691 return strerror(target_to_host_errno(err
));
694 #define safe_syscall0(type, name) \
695 static type safe_##name(void) \
697 return safe_syscall(__NR_##name); \
700 #define safe_syscall1(type, name, type1, arg1) \
701 static type safe_##name(type1 arg1) \
703 return safe_syscall(__NR_##name, arg1); \
706 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
707 static type safe_##name(type1 arg1, type2 arg2) \
709 return safe_syscall(__NR_##name, arg1, arg2); \
712 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
713 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
715 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
718 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
720 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
722 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
725 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
726 type4, arg4, type5, arg5) \
727 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
730 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
733 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
734 type4, arg4, type5, arg5, type6, arg6) \
735 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
736 type5 arg5, type6 arg6) \
738 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
741 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
742 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
743 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
744 int, flags
, mode_t
, mode
)
745 #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
746 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
747 struct rusage
*, rusage
)
749 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
750 int, options
, struct rusage
*, rusage
)
751 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
752 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
753 defined(TARGET_NR_pselect6)
754 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
755 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
757 #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll)
758 safe_syscall5(int, ppoll
, struct pollfd
*, ufds
, unsigned int, nfds
,
759 struct timespec
*, tsp
, const sigset_t
*, sigmask
,
762 safe_syscall6(int, epoll_pwait
, int, epfd
, struct epoll_event
*, events
,
763 int, maxevents
, int, timeout
, const sigset_t
*, sigmask
,
765 #ifdef TARGET_NR_futex
766 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
767 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
769 safe_syscall2(int, rt_sigsuspend
, sigset_t
*, newset
, size_t, sigsetsize
)
770 safe_syscall2(int, kill
, pid_t
, pid
, int, sig
)
771 safe_syscall2(int, tkill
, int, tid
, int, sig
)
772 safe_syscall3(int, tgkill
, int, tgid
, int, pid
, int, sig
)
773 safe_syscall3(ssize_t
, readv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
774 safe_syscall3(ssize_t
, writev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
775 safe_syscall5(ssize_t
, preadv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
776 unsigned long, pos_l
, unsigned long, pos_h
)
777 safe_syscall5(ssize_t
, pwritev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
778 unsigned long, pos_l
, unsigned long, pos_h
)
779 safe_syscall3(int, connect
, int, fd
, const struct sockaddr
*, addr
,
781 safe_syscall6(ssize_t
, sendto
, int, fd
, const void *, buf
, size_t, len
,
782 int, flags
, const struct sockaddr
*, addr
, socklen_t
, addrlen
)
783 safe_syscall6(ssize_t
, recvfrom
, int, fd
, void *, buf
, size_t, len
,
784 int, flags
, struct sockaddr
*, addr
, socklen_t
*, addrlen
)
785 safe_syscall3(ssize_t
, sendmsg
, int, fd
, const struct msghdr
*, msg
, int, flags
)
786 safe_syscall3(ssize_t
, recvmsg
, int, fd
, struct msghdr
*, msg
, int, flags
)
787 safe_syscall2(int, flock
, int, fd
, int, operation
)
788 #ifdef TARGET_NR_rt_sigtimedwait
789 safe_syscall4(int, rt_sigtimedwait
, const sigset_t
*, these
, siginfo_t
*, uinfo
,
790 const struct timespec
*, uts
, size_t, sigsetsize
)
792 safe_syscall4(int, accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, len
,
794 #if defined(TARGET_NR_nanosleep)
795 safe_syscall2(int, nanosleep
, const struct timespec
*, req
,
796 struct timespec
*, rem
)
798 #ifdef TARGET_NR_clock_nanosleep
799 safe_syscall4(int, clock_nanosleep
, const clockid_t
, clock
, int, flags
,
800 const struct timespec
*, req
, struct timespec
*, rem
)
803 safe_syscall6(int, ipc
, int, call
, long, first
, long, second
, long, third
,
804 void *, ptr
, long, fifth
)
807 safe_syscall4(int, msgsnd
, int, msgid
, const void *, msgp
, size_t, sz
,
811 safe_syscall5(int, msgrcv
, int, msgid
, void *, msgp
, size_t, sz
,
812 long, msgtype
, int, flags
)
814 #ifdef __NR_semtimedop
815 safe_syscall4(int, semtimedop
, int, semid
, struct sembuf
*, tsops
,
816 unsigned, nsops
, const struct timespec
*, timeout
)
818 #ifdef TARGET_NR_mq_timedsend
819 safe_syscall5(int, mq_timedsend
, int, mqdes
, const char *, msg_ptr
,
820 size_t, len
, unsigned, prio
, const struct timespec
*, timeout
)
822 #ifdef TARGET_NR_mq_timedreceive
823 safe_syscall5(int, mq_timedreceive
, int, mqdes
, char *, msg_ptr
,
824 size_t, len
, unsigned *, prio
, const struct timespec
*, timeout
)
826 /* We do ioctl like this rather than via safe_syscall3 to preserve the
827 * "third argument might be integer or pointer or not present" behaviour of
830 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
831 /* Similarly for fcntl. Note that callers must always:
832 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
833 * use the flock64 struct rather than unsuffixed flock
834 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
837 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
839 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
842 static inline int host_to_target_sock_type(int host_type
)
846 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
848 target_type
= TARGET_SOCK_DGRAM
;
851 target_type
= TARGET_SOCK_STREAM
;
854 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
858 #if defined(SOCK_CLOEXEC)
859 if (host_type
& SOCK_CLOEXEC
) {
860 target_type
|= TARGET_SOCK_CLOEXEC
;
864 #if defined(SOCK_NONBLOCK)
865 if (host_type
& SOCK_NONBLOCK
) {
866 target_type
|= TARGET_SOCK_NONBLOCK
;
873 static abi_ulong target_brk
;
874 static abi_ulong target_original_brk
;
875 static abi_ulong brk_page
;
877 void target_set_brk(abi_ulong new_brk
)
879 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
880 brk_page
= HOST_PAGE_ALIGN(target_brk
);
883 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
884 #define DEBUGF_BRK(message, args...)
886 /* do_brk() must return target values and target errnos. */
887 abi_long
do_brk(abi_ulong new_brk
)
889 abi_long mapped_addr
;
890 abi_ulong new_alloc_size
;
892 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
895 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
898 if (new_brk
< target_original_brk
) {
899 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
904 /* If the new brk is less than the highest page reserved to the
905 * target heap allocation, set it and we're almost done... */
906 if (new_brk
<= brk_page
) {
907 /* Heap contents are initialized to zero, as for anonymous
909 if (new_brk
> target_brk
) {
910 memset(g2h(target_brk
), 0, new_brk
- target_brk
);
912 target_brk
= new_brk
;
913 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
917 /* We need to allocate more memory after the brk... Note that
918 * we don't use MAP_FIXED because that will map over the top of
919 * any existing mapping (like the one with the host libc or qemu
920 * itself); instead we treat "mapped but at wrong address" as
921 * a failure and unmap again.
923 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
924 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
925 PROT_READ
|PROT_WRITE
,
926 MAP_ANON
|MAP_PRIVATE
, 0, 0));
928 if (mapped_addr
== brk_page
) {
929 /* Heap contents are initialized to zero, as for anonymous
930 * mapped pages. Technically the new pages are already
931 * initialized to zero since they *are* anonymous mapped
932 * pages, however we have to take care with the contents that
933 * come from the remaining part of the previous page: it may
934 * contains garbage data due to a previous heap usage (grown
936 memset(g2h(target_brk
), 0, brk_page
- target_brk
);
938 target_brk
= new_brk
;
939 brk_page
= HOST_PAGE_ALIGN(target_brk
);
940 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
943 } else if (mapped_addr
!= -1) {
944 /* Mapped but at wrong address, meaning there wasn't actually
945 * enough space for this brk.
947 target_munmap(mapped_addr
, new_alloc_size
);
949 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
952 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
955 #if defined(TARGET_ALPHA)
956 /* We (partially) emulate OSF/1 on Alpha, which requires we
957 return a proper errno, not an unchanged brk value. */
958 return -TARGET_ENOMEM
;
960 /* For everything else, return the previous break. */
964 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
965 defined(TARGET_NR_pselect6)
966 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
967 abi_ulong target_fds_addr
,
971 abi_ulong b
, *target_fds
;
973 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
974 if (!(target_fds
= lock_user(VERIFY_READ
,
976 sizeof(abi_ulong
) * nw
,
978 return -TARGET_EFAULT
;
982 for (i
= 0; i
< nw
; i
++) {
983 /* grab the abi_ulong */
984 __get_user(b
, &target_fds
[i
]);
985 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
986 /* check the bit inside the abi_ulong */
993 unlock_user(target_fds
, target_fds_addr
, 0);
998 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
999 abi_ulong target_fds_addr
,
1002 if (target_fds_addr
) {
1003 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
1004 return -TARGET_EFAULT
;
1012 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
1018 abi_ulong
*target_fds
;
1020 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
1021 if (!(target_fds
= lock_user(VERIFY_WRITE
,
1023 sizeof(abi_ulong
) * nw
,
1025 return -TARGET_EFAULT
;
1028 for (i
= 0; i
< nw
; i
++) {
1030 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
1031 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
1034 __put_user(v
, &target_fds
[i
]);
1037 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
1043 #if defined(__alpha__)
1044 #define HOST_HZ 1024
1049 static inline abi_long
host_to_target_clock_t(long ticks
)
1051 #if HOST_HZ == TARGET_HZ
1054 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
1058 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
1059 const struct rusage
*rusage
)
1061 struct target_rusage
*target_rusage
;
1063 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
1064 return -TARGET_EFAULT
;
1065 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
1066 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
1067 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
1068 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
1069 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
1070 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
1071 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
1072 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
1073 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
1074 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
1075 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
1076 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
1077 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
1078 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
1079 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
1080 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
1081 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
1082 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
1083 unlock_user_struct(target_rusage
, target_addr
, 1);
1088 #ifdef TARGET_NR_setrlimit
1089 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
1091 abi_ulong target_rlim_swap
;
1094 target_rlim_swap
= tswapal(target_rlim
);
1095 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
1096 return RLIM_INFINITY
;
1098 result
= target_rlim_swap
;
1099 if (target_rlim_swap
!= (rlim_t
)result
)
1100 return RLIM_INFINITY
;
1106 #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
1107 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1109 abi_ulong target_rlim_swap
;
1112 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1113 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1115 target_rlim_swap
= rlim
;
1116 result
= tswapal(target_rlim_swap
);
1122 static inline int target_to_host_resource(int code
)
1125 case TARGET_RLIMIT_AS
:
1127 case TARGET_RLIMIT_CORE
:
1129 case TARGET_RLIMIT_CPU
:
1131 case TARGET_RLIMIT_DATA
:
1133 case TARGET_RLIMIT_FSIZE
:
1134 return RLIMIT_FSIZE
;
1135 case TARGET_RLIMIT_LOCKS
:
1136 return RLIMIT_LOCKS
;
1137 case TARGET_RLIMIT_MEMLOCK
:
1138 return RLIMIT_MEMLOCK
;
1139 case TARGET_RLIMIT_MSGQUEUE
:
1140 return RLIMIT_MSGQUEUE
;
1141 case TARGET_RLIMIT_NICE
:
1143 case TARGET_RLIMIT_NOFILE
:
1144 return RLIMIT_NOFILE
;
1145 case TARGET_RLIMIT_NPROC
:
1146 return RLIMIT_NPROC
;
1147 case TARGET_RLIMIT_RSS
:
1149 case TARGET_RLIMIT_RTPRIO
:
1150 return RLIMIT_RTPRIO
;
1151 case TARGET_RLIMIT_SIGPENDING
:
1152 return RLIMIT_SIGPENDING
;
1153 case TARGET_RLIMIT_STACK
:
1154 return RLIMIT_STACK
;
1160 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1161 abi_ulong target_tv_addr
)
1163 struct target_timeval
*target_tv
;
1165 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1166 return -TARGET_EFAULT
;
1169 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1170 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1172 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1177 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1178 const struct timeval
*tv
)
1180 struct target_timeval
*target_tv
;
1182 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1183 return -TARGET_EFAULT
;
1186 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1187 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1189 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1194 static inline abi_long
copy_to_user_timeval64(abi_ulong target_tv_addr
,
1195 const struct timeval
*tv
)
1197 struct target__kernel_sock_timeval
*target_tv
;
1199 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1200 return -TARGET_EFAULT
;
1203 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1204 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1206 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1211 #if defined(TARGET_NR_futex) || \
1212 defined(TARGET_NR_rt_sigtimedwait) || \
1213 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1214 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1215 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
1216 defined(TARGET_NR_mq_timedreceive)
1217 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
1218 abi_ulong target_addr
)
1220 struct target_timespec
*target_ts
;
1222 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1223 return -TARGET_EFAULT
;
1225 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1226 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1227 unlock_user_struct(target_ts
, target_addr
, 0);
1232 #if defined(TARGET_NR_clock_settime64)
1233 static inline abi_long
target_to_host_timespec64(struct timespec
*host_ts
,
1234 abi_ulong target_addr
)
1236 struct target__kernel_timespec
*target_ts
;
1238 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1239 return -TARGET_EFAULT
;
1241 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1242 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1243 unlock_user_struct(target_ts
, target_addr
, 0);
1248 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
1249 struct timespec
*host_ts
)
1251 struct target_timespec
*target_ts
;
1253 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1254 return -TARGET_EFAULT
;
1256 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1257 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1258 unlock_user_struct(target_ts
, target_addr
, 1);
1262 static inline abi_long
host_to_target_timespec64(abi_ulong target_addr
,
1263 struct timespec
*host_ts
)
1265 struct target__kernel_timespec
*target_ts
;
1267 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1268 return -TARGET_EFAULT
;
1270 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1271 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1272 unlock_user_struct(target_ts
, target_addr
, 1);
1276 #if defined(TARGET_NR_settimeofday)
1277 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1278 abi_ulong target_tz_addr
)
1280 struct target_timezone
*target_tz
;
1282 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1283 return -TARGET_EFAULT
;
1286 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1287 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1289 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1295 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1298 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1299 abi_ulong target_mq_attr_addr
)
1301 struct target_mq_attr
*target_mq_attr
;
1303 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1304 target_mq_attr_addr
, 1))
1305 return -TARGET_EFAULT
;
1307 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1308 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1309 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1310 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1312 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1317 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1318 const struct mq_attr
*attr
)
1320 struct target_mq_attr
*target_mq_attr
;
1322 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1323 target_mq_attr_addr
, 0))
1324 return -TARGET_EFAULT
;
1326 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1327 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1328 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1329 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1331 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1337 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1338 /* do_select() must return target values and target errnos. */
1339 static abi_long
do_select(int n
,
1340 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1341 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1343 fd_set rfds
, wfds
, efds
;
1344 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1346 struct timespec ts
, *ts_ptr
;
1349 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1353 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1357 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1362 if (target_tv_addr
) {
1363 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1364 return -TARGET_EFAULT
;
1365 ts
.tv_sec
= tv
.tv_sec
;
1366 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1372 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1375 if (!is_error(ret
)) {
1376 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1377 return -TARGET_EFAULT
;
1378 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1379 return -TARGET_EFAULT
;
1380 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1381 return -TARGET_EFAULT
;
1383 if (target_tv_addr
) {
1384 tv
.tv_sec
= ts
.tv_sec
;
1385 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1386 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1387 return -TARGET_EFAULT
;
1395 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1396 static abi_long
do_old_select(abi_ulong arg1
)
1398 struct target_sel_arg_struct
*sel
;
1399 abi_ulong inp
, outp
, exp
, tvp
;
1402 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1)) {
1403 return -TARGET_EFAULT
;
1406 nsel
= tswapal(sel
->n
);
1407 inp
= tswapal(sel
->inp
);
1408 outp
= tswapal(sel
->outp
);
1409 exp
= tswapal(sel
->exp
);
1410 tvp
= tswapal(sel
->tvp
);
1412 unlock_user_struct(sel
, arg1
, 0);
1414 return do_select(nsel
, inp
, outp
, exp
, tvp
);
1419 static abi_long
do_pipe2(int host_pipe
[], int flags
)
1422 return pipe2(host_pipe
, flags
);
1428 static abi_long
do_pipe(void *cpu_env
, abi_ulong pipedes
,
1429 int flags
, int is_pipe2
)
1433 ret
= flags
? do_pipe2(host_pipe
, flags
) : pipe(host_pipe
);
1436 return get_errno(ret
);
1438 /* Several targets have special calling conventions for the original
1439 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1441 #if defined(TARGET_ALPHA)
1442 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = host_pipe
[1];
1443 return host_pipe
[0];
1444 #elif defined(TARGET_MIPS)
1445 ((CPUMIPSState
*)cpu_env
)->active_tc
.gpr
[3] = host_pipe
[1];
1446 return host_pipe
[0];
1447 #elif defined(TARGET_SH4)
1448 ((CPUSH4State
*)cpu_env
)->gregs
[1] = host_pipe
[1];
1449 return host_pipe
[0];
1450 #elif defined(TARGET_SPARC)
1451 ((CPUSPARCState
*)cpu_env
)->regwptr
[1] = host_pipe
[1];
1452 return host_pipe
[0];
1456 if (put_user_s32(host_pipe
[0], pipedes
)
1457 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(host_pipe
[0])))
1458 return -TARGET_EFAULT
;
1459 return get_errno(ret
);
1462 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1463 abi_ulong target_addr
,
1466 struct target_ip_mreqn
*target_smreqn
;
1468 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1470 return -TARGET_EFAULT
;
1471 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1472 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1473 if (len
== sizeof(struct target_ip_mreqn
))
1474 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1475 unlock_user(target_smreqn
, target_addr
, 0);
1480 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1481 abi_ulong target_addr
,
1484 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1485 sa_family_t sa_family
;
1486 struct target_sockaddr
*target_saddr
;
1488 if (fd_trans_target_to_host_addr(fd
)) {
1489 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1492 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1494 return -TARGET_EFAULT
;
1496 sa_family
= tswap16(target_saddr
->sa_family
);
1498 /* Oops. The caller might send a incomplete sun_path; sun_path
1499 * must be terminated by \0 (see the manual page), but
1500 * unfortunately it is quite common to specify sockaddr_un
1501 * length as "strlen(x->sun_path)" while it should be
1502 * "strlen(...) + 1". We'll fix that here if needed.
1503 * Linux kernel has a similar feature.
1506 if (sa_family
== AF_UNIX
) {
1507 if (len
< unix_maxlen
&& len
> 0) {
1508 char *cp
= (char*)target_saddr
;
1510 if ( cp
[len
-1] && !cp
[len
] )
1513 if (len
> unix_maxlen
)
1517 memcpy(addr
, target_saddr
, len
);
1518 addr
->sa_family
= sa_family
;
1519 if (sa_family
== AF_NETLINK
) {
1520 struct sockaddr_nl
*nladdr
;
1522 nladdr
= (struct sockaddr_nl
*)addr
;
1523 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1524 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1525 } else if (sa_family
== AF_PACKET
) {
1526 struct target_sockaddr_ll
*lladdr
;
1528 lladdr
= (struct target_sockaddr_ll
*)addr
;
1529 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1530 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1532 unlock_user(target_saddr
, target_addr
, 0);
1537 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1538 struct sockaddr
*addr
,
1541 struct target_sockaddr
*target_saddr
;
1548 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1550 return -TARGET_EFAULT
;
1551 memcpy(target_saddr
, addr
, len
);
1552 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1553 sizeof(target_saddr
->sa_family
)) {
1554 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1556 if (addr
->sa_family
== AF_NETLINK
&&
1557 len
>= sizeof(struct target_sockaddr_nl
)) {
1558 struct target_sockaddr_nl
*target_nl
=
1559 (struct target_sockaddr_nl
*)target_saddr
;
1560 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1561 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1562 } else if (addr
->sa_family
== AF_PACKET
) {
1563 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1564 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1565 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1566 } else if (addr
->sa_family
== AF_INET6
&&
1567 len
>= sizeof(struct target_sockaddr_in6
)) {
1568 struct target_sockaddr_in6
*target_in6
=
1569 (struct target_sockaddr_in6
*)target_saddr
;
1570 target_in6
->sin6_scope_id
= tswap16(target_in6
->sin6_scope_id
);
1572 unlock_user(target_saddr
, target_addr
, len
);
1577 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1578 struct target_msghdr
*target_msgh
)
1580 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1581 abi_long msg_controllen
;
1582 abi_ulong target_cmsg_addr
;
1583 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1584 socklen_t space
= 0;
1586 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1587 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1589 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1590 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1591 target_cmsg_start
= target_cmsg
;
1593 return -TARGET_EFAULT
;
1595 while (cmsg
&& target_cmsg
) {
1596 void *data
= CMSG_DATA(cmsg
);
1597 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1599 int len
= tswapal(target_cmsg
->cmsg_len
)
1600 - sizeof(struct target_cmsghdr
);
1602 space
+= CMSG_SPACE(len
);
1603 if (space
> msgh
->msg_controllen
) {
1604 space
-= CMSG_SPACE(len
);
1605 /* This is a QEMU bug, since we allocated the payload
1606 * area ourselves (unlike overflow in host-to-target
1607 * conversion, which is just the guest giving us a buffer
1608 * that's too small). It can't happen for the payload types
1609 * we currently support; if it becomes an issue in future
1610 * we would need to improve our allocation strategy to
1611 * something more intelligent than "twice the size of the
1612 * target buffer we're reading from".
1614 qemu_log_mask(LOG_UNIMP
,
1615 ("Unsupported ancillary data %d/%d: "
1616 "unhandled msg size\n"),
1617 tswap32(target_cmsg
->cmsg_level
),
1618 tswap32(target_cmsg
->cmsg_type
));
1622 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1623 cmsg
->cmsg_level
= SOL_SOCKET
;
1625 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1627 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1628 cmsg
->cmsg_len
= CMSG_LEN(len
);
1630 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1631 int *fd
= (int *)data
;
1632 int *target_fd
= (int *)target_data
;
1633 int i
, numfds
= len
/ sizeof(int);
1635 for (i
= 0; i
< numfds
; i
++) {
1636 __get_user(fd
[i
], target_fd
+ i
);
1638 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1639 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1640 struct ucred
*cred
= (struct ucred
*)data
;
1641 struct target_ucred
*target_cred
=
1642 (struct target_ucred
*)target_data
;
1644 __get_user(cred
->pid
, &target_cred
->pid
);
1645 __get_user(cred
->uid
, &target_cred
->uid
);
1646 __get_user(cred
->gid
, &target_cred
->gid
);
1648 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1649 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1650 memcpy(data
, target_data
, len
);
1653 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1654 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1657 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1659 msgh
->msg_controllen
= space
;
1663 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1664 struct msghdr
*msgh
)
1666 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1667 abi_long msg_controllen
;
1668 abi_ulong target_cmsg_addr
;
1669 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1670 socklen_t space
= 0;
1672 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1673 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1675 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1676 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1677 target_cmsg_start
= target_cmsg
;
1679 return -TARGET_EFAULT
;
1681 while (cmsg
&& target_cmsg
) {
1682 void *data
= CMSG_DATA(cmsg
);
1683 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1685 int len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
1686 int tgt_len
, tgt_space
;
1688 /* We never copy a half-header but may copy half-data;
1689 * this is Linux's behaviour in put_cmsg(). Note that
1690 * truncation here is a guest problem (which we report
1691 * to the guest via the CTRUNC bit), unlike truncation
1692 * in target_to_host_cmsg, which is a QEMU bug.
1694 if (msg_controllen
< sizeof(struct target_cmsghdr
)) {
1695 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1699 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1700 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1702 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1704 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1706 /* Payload types which need a different size of payload on
1707 * the target must adjust tgt_len here.
1710 switch (cmsg
->cmsg_level
) {
1712 switch (cmsg
->cmsg_type
) {
1714 tgt_len
= sizeof(struct target_timeval
);
1724 if (msg_controllen
< TARGET_CMSG_LEN(tgt_len
)) {
1725 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1726 tgt_len
= msg_controllen
- sizeof(struct target_cmsghdr
);
1729 /* We must now copy-and-convert len bytes of payload
1730 * into tgt_len bytes of destination space. Bear in mind
1731 * that in both source and destination we may be dealing
1732 * with a truncated value!
1734 switch (cmsg
->cmsg_level
) {
1736 switch (cmsg
->cmsg_type
) {
1739 int *fd
= (int *)data
;
1740 int *target_fd
= (int *)target_data
;
1741 int i
, numfds
= tgt_len
/ sizeof(int);
1743 for (i
= 0; i
< numfds
; i
++) {
1744 __put_user(fd
[i
], target_fd
+ i
);
1750 struct timeval
*tv
= (struct timeval
*)data
;
1751 struct target_timeval
*target_tv
=
1752 (struct target_timeval
*)target_data
;
1754 if (len
!= sizeof(struct timeval
) ||
1755 tgt_len
!= sizeof(struct target_timeval
)) {
1759 /* copy struct timeval to target */
1760 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1761 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1764 case SCM_CREDENTIALS
:
1766 struct ucred
*cred
= (struct ucred
*)data
;
1767 struct target_ucred
*target_cred
=
1768 (struct target_ucred
*)target_data
;
1770 __put_user(cred
->pid
, &target_cred
->pid
);
1771 __put_user(cred
->uid
, &target_cred
->uid
);
1772 __put_user(cred
->gid
, &target_cred
->gid
);
1781 switch (cmsg
->cmsg_type
) {
1784 uint32_t *v
= (uint32_t *)data
;
1785 uint32_t *t_int
= (uint32_t *)target_data
;
1787 if (len
!= sizeof(uint32_t) ||
1788 tgt_len
!= sizeof(uint32_t)) {
1791 __put_user(*v
, t_int
);
1797 struct sock_extended_err ee
;
1798 struct sockaddr_in offender
;
1800 struct errhdr_t
*errh
= (struct errhdr_t
*)data
;
1801 struct errhdr_t
*target_errh
=
1802 (struct errhdr_t
*)target_data
;
1804 if (len
!= sizeof(struct errhdr_t
) ||
1805 tgt_len
!= sizeof(struct errhdr_t
)) {
1808 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1809 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1810 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1811 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1812 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1813 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1814 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1815 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1816 (void *) &errh
->offender
, sizeof(errh
->offender
));
1825 switch (cmsg
->cmsg_type
) {
1828 uint32_t *v
= (uint32_t *)data
;
1829 uint32_t *t_int
= (uint32_t *)target_data
;
1831 if (len
!= sizeof(uint32_t) ||
1832 tgt_len
!= sizeof(uint32_t)) {
1835 __put_user(*v
, t_int
);
1841 struct sock_extended_err ee
;
1842 struct sockaddr_in6 offender
;
1844 struct errhdr6_t
*errh
= (struct errhdr6_t
*)data
;
1845 struct errhdr6_t
*target_errh
=
1846 (struct errhdr6_t
*)target_data
;
1848 if (len
!= sizeof(struct errhdr6_t
) ||
1849 tgt_len
!= sizeof(struct errhdr6_t
)) {
1852 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
1853 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
1854 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
1855 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
1856 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
1857 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
1858 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
1859 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
1860 (void *) &errh
->offender
, sizeof(errh
->offender
));
1870 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1871 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1872 memcpy(target_data
, data
, MIN(len
, tgt_len
));
1873 if (tgt_len
> len
) {
1874 memset(target_data
+ len
, 0, tgt_len
- len
);
1878 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(tgt_len
));
1879 tgt_space
= TARGET_CMSG_SPACE(tgt_len
);
1880 if (msg_controllen
< tgt_space
) {
1881 tgt_space
= msg_controllen
;
1883 msg_controllen
-= tgt_space
;
1885 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1886 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1889 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
1891 target_msgh
->msg_controllen
= tswapal(space
);
1895 /* do_setsockopt() Must return target values and target errnos. */
1896 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
1897 abi_ulong optval_addr
, socklen_t optlen
)
1901 struct ip_mreqn
*ip_mreq
;
1902 struct ip_mreq_source
*ip_mreq_source
;
1906 /* TCP options all take an 'int' value. */
1907 if (optlen
< sizeof(uint32_t))
1908 return -TARGET_EINVAL
;
1910 if (get_user_u32(val
, optval_addr
))
1911 return -TARGET_EFAULT
;
1912 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1919 case IP_ROUTER_ALERT
:
1923 case IP_MTU_DISCOVER
:
1930 case IP_MULTICAST_TTL
:
1931 case IP_MULTICAST_LOOP
:
1933 if (optlen
>= sizeof(uint32_t)) {
1934 if (get_user_u32(val
, optval_addr
))
1935 return -TARGET_EFAULT
;
1936 } else if (optlen
>= 1) {
1937 if (get_user_u8(val
, optval_addr
))
1938 return -TARGET_EFAULT
;
1940 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
1942 case IP_ADD_MEMBERSHIP
:
1943 case IP_DROP_MEMBERSHIP
:
1944 if (optlen
< sizeof (struct target_ip_mreq
) ||
1945 optlen
> sizeof (struct target_ip_mreqn
))
1946 return -TARGET_EINVAL
;
1948 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
1949 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
1950 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
1953 case IP_BLOCK_SOURCE
:
1954 case IP_UNBLOCK_SOURCE
:
1955 case IP_ADD_SOURCE_MEMBERSHIP
:
1956 case IP_DROP_SOURCE_MEMBERSHIP
:
1957 if (optlen
!= sizeof (struct target_ip_mreq_source
))
1958 return -TARGET_EINVAL
;
1960 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
1961 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
1962 unlock_user (ip_mreq_source
, optval_addr
, 0);
1971 case IPV6_MTU_DISCOVER
:
1974 case IPV6_RECVPKTINFO
:
1975 case IPV6_UNICAST_HOPS
:
1976 case IPV6_MULTICAST_HOPS
:
1977 case IPV6_MULTICAST_LOOP
:
1979 case IPV6_RECVHOPLIMIT
:
1980 case IPV6_2292HOPLIMIT
:
1983 case IPV6_2292PKTINFO
:
1984 case IPV6_RECVTCLASS
:
1985 case IPV6_RECVRTHDR
:
1986 case IPV6_2292RTHDR
:
1987 case IPV6_RECVHOPOPTS
:
1988 case IPV6_2292HOPOPTS
:
1989 case IPV6_RECVDSTOPTS
:
1990 case IPV6_2292DSTOPTS
:
1992 #ifdef IPV6_RECVPATHMTU
1993 case IPV6_RECVPATHMTU
:
1995 #ifdef IPV6_TRANSPARENT
1996 case IPV6_TRANSPARENT
:
1998 #ifdef IPV6_FREEBIND
2001 #ifdef IPV6_RECVORIGDSTADDR
2002 case IPV6_RECVORIGDSTADDR
:
2005 if (optlen
< sizeof(uint32_t)) {
2006 return -TARGET_EINVAL
;
2008 if (get_user_u32(val
, optval_addr
)) {
2009 return -TARGET_EFAULT
;
2011 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2012 &val
, sizeof(val
)));
2016 struct in6_pktinfo pki
;
2018 if (optlen
< sizeof(pki
)) {
2019 return -TARGET_EINVAL
;
2022 if (copy_from_user(&pki
, optval_addr
, sizeof(pki
))) {
2023 return -TARGET_EFAULT
;
2026 pki
.ipi6_ifindex
= tswap32(pki
.ipi6_ifindex
);
2028 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2029 &pki
, sizeof(pki
)));
2032 case IPV6_ADD_MEMBERSHIP
:
2033 case IPV6_DROP_MEMBERSHIP
:
2035 struct ipv6_mreq ipv6mreq
;
2037 if (optlen
< sizeof(ipv6mreq
)) {
2038 return -TARGET_EINVAL
;
2041 if (copy_from_user(&ipv6mreq
, optval_addr
, sizeof(ipv6mreq
))) {
2042 return -TARGET_EFAULT
;
2045 ipv6mreq
.ipv6mr_interface
= tswap32(ipv6mreq
.ipv6mr_interface
);
2047 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2048 &ipv6mreq
, sizeof(ipv6mreq
)));
2059 struct icmp6_filter icmp6f
;
2061 if (optlen
> sizeof(icmp6f
)) {
2062 optlen
= sizeof(icmp6f
);
2065 if (copy_from_user(&icmp6f
, optval_addr
, optlen
)) {
2066 return -TARGET_EFAULT
;
2069 for (val
= 0; val
< 8; val
++) {
2070 icmp6f
.data
[val
] = tswap32(icmp6f
.data
[val
]);
2073 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2085 /* those take an u32 value */
2086 if (optlen
< sizeof(uint32_t)) {
2087 return -TARGET_EINVAL
;
2090 if (get_user_u32(val
, optval_addr
)) {
2091 return -TARGET_EFAULT
;
2093 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2094 &val
, sizeof(val
)));
2101 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2106 char *alg_key
= g_malloc(optlen
);
2109 return -TARGET_ENOMEM
;
2111 if (copy_from_user(alg_key
, optval_addr
, optlen
)) {
2113 return -TARGET_EFAULT
;
2115 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2120 case ALG_SET_AEAD_AUTHSIZE
:
2122 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2131 case TARGET_SOL_SOCKET
:
2133 case TARGET_SO_RCVTIMEO
:
2137 optname
= SO_RCVTIMEO
;
2140 if (optlen
!= sizeof(struct target_timeval
)) {
2141 return -TARGET_EINVAL
;
2144 if (copy_from_user_timeval(&tv
, optval_addr
)) {
2145 return -TARGET_EFAULT
;
2148 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2152 case TARGET_SO_SNDTIMEO
:
2153 optname
= SO_SNDTIMEO
;
2155 case TARGET_SO_ATTACH_FILTER
:
2157 struct target_sock_fprog
*tfprog
;
2158 struct target_sock_filter
*tfilter
;
2159 struct sock_fprog fprog
;
2160 struct sock_filter
*filter
;
2163 if (optlen
!= sizeof(*tfprog
)) {
2164 return -TARGET_EINVAL
;
2166 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
2167 return -TARGET_EFAULT
;
2169 if (!lock_user_struct(VERIFY_READ
, tfilter
,
2170 tswapal(tfprog
->filter
), 0)) {
2171 unlock_user_struct(tfprog
, optval_addr
, 1);
2172 return -TARGET_EFAULT
;
2175 fprog
.len
= tswap16(tfprog
->len
);
2176 filter
= g_try_new(struct sock_filter
, fprog
.len
);
2177 if (filter
== NULL
) {
2178 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2179 unlock_user_struct(tfprog
, optval_addr
, 1);
2180 return -TARGET_ENOMEM
;
2182 for (i
= 0; i
< fprog
.len
; i
++) {
2183 filter
[i
].code
= tswap16(tfilter
[i
].code
);
2184 filter
[i
].jt
= tfilter
[i
].jt
;
2185 filter
[i
].jf
= tfilter
[i
].jf
;
2186 filter
[i
].k
= tswap32(tfilter
[i
].k
);
2188 fprog
.filter
= filter
;
2190 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
2191 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
2194 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2195 unlock_user_struct(tfprog
, optval_addr
, 1);
2198 case TARGET_SO_BINDTODEVICE
:
2200 char *dev_ifname
, *addr_ifname
;
2202 if (optlen
> IFNAMSIZ
- 1) {
2203 optlen
= IFNAMSIZ
- 1;
2205 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2207 return -TARGET_EFAULT
;
2209 optname
= SO_BINDTODEVICE
;
2210 addr_ifname
= alloca(IFNAMSIZ
);
2211 memcpy(addr_ifname
, dev_ifname
, optlen
);
2212 addr_ifname
[optlen
] = 0;
2213 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2214 addr_ifname
, optlen
));
2215 unlock_user (dev_ifname
, optval_addr
, 0);
2218 case TARGET_SO_LINGER
:
2221 struct target_linger
*tlg
;
2223 if (optlen
!= sizeof(struct target_linger
)) {
2224 return -TARGET_EINVAL
;
2226 if (!lock_user_struct(VERIFY_READ
, tlg
, optval_addr
, 1)) {
2227 return -TARGET_EFAULT
;
2229 __get_user(lg
.l_onoff
, &tlg
->l_onoff
);
2230 __get_user(lg
.l_linger
, &tlg
->l_linger
);
2231 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, SO_LINGER
,
2233 unlock_user_struct(tlg
, optval_addr
, 0);
2236 /* Options with 'int' argument. */
2237 case TARGET_SO_DEBUG
:
2240 case TARGET_SO_REUSEADDR
:
2241 optname
= SO_REUSEADDR
;
2244 case TARGET_SO_REUSEPORT
:
2245 optname
= SO_REUSEPORT
;
2248 case TARGET_SO_TYPE
:
2251 case TARGET_SO_ERROR
:
2254 case TARGET_SO_DONTROUTE
:
2255 optname
= SO_DONTROUTE
;
2257 case TARGET_SO_BROADCAST
:
2258 optname
= SO_BROADCAST
;
2260 case TARGET_SO_SNDBUF
:
2261 optname
= SO_SNDBUF
;
2263 case TARGET_SO_SNDBUFFORCE
:
2264 optname
= SO_SNDBUFFORCE
;
2266 case TARGET_SO_RCVBUF
:
2267 optname
= SO_RCVBUF
;
2269 case TARGET_SO_RCVBUFFORCE
:
2270 optname
= SO_RCVBUFFORCE
;
2272 case TARGET_SO_KEEPALIVE
:
2273 optname
= SO_KEEPALIVE
;
2275 case TARGET_SO_OOBINLINE
:
2276 optname
= SO_OOBINLINE
;
2278 case TARGET_SO_NO_CHECK
:
2279 optname
= SO_NO_CHECK
;
2281 case TARGET_SO_PRIORITY
:
2282 optname
= SO_PRIORITY
;
2285 case TARGET_SO_BSDCOMPAT
:
2286 optname
= SO_BSDCOMPAT
;
2289 case TARGET_SO_PASSCRED
:
2290 optname
= SO_PASSCRED
;
2292 case TARGET_SO_PASSSEC
:
2293 optname
= SO_PASSSEC
;
2295 case TARGET_SO_TIMESTAMP
:
2296 optname
= SO_TIMESTAMP
;
2298 case TARGET_SO_RCVLOWAT
:
2299 optname
= SO_RCVLOWAT
;
2304 if (optlen
< sizeof(uint32_t))
2305 return -TARGET_EINVAL
;
2307 if (get_user_u32(val
, optval_addr
))
2308 return -TARGET_EFAULT
;
2309 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
2314 case NETLINK_PKTINFO
:
2315 case NETLINK_ADD_MEMBERSHIP
:
2316 case NETLINK_DROP_MEMBERSHIP
:
2317 case NETLINK_BROADCAST_ERROR
:
2318 case NETLINK_NO_ENOBUFS
:
2319 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2320 case NETLINK_LISTEN_ALL_NSID
:
2321 case NETLINK_CAP_ACK
:
2322 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2323 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2324 case NETLINK_EXT_ACK
:
2325 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2326 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2327 case NETLINK_GET_STRICT_CHK
:
2328 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2334 if (optlen
< sizeof(uint32_t)) {
2335 return -TARGET_EINVAL
;
2337 if (get_user_u32(val
, optval_addr
)) {
2338 return -TARGET_EFAULT
;
2340 ret
= get_errno(setsockopt(sockfd
, SOL_NETLINK
, optname
, &val
,
2343 #endif /* SOL_NETLINK */
2346 qemu_log_mask(LOG_UNIMP
, "Unsupported setsockopt level=%d optname=%d\n",
2348 ret
= -TARGET_ENOPROTOOPT
;
2353 /* do_getsockopt() Must return target values and target errnos. */
2354 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
2355 abi_ulong optval_addr
, abi_ulong optlen
)
2362 case TARGET_SOL_SOCKET
:
2365 /* These don't just return a single integer */
2366 case TARGET_SO_PEERNAME
:
2368 case TARGET_SO_RCVTIMEO
: {
2372 optname
= SO_RCVTIMEO
;
2375 if (get_user_u32(len
, optlen
)) {
2376 return -TARGET_EFAULT
;
2379 return -TARGET_EINVAL
;
2383 ret
= get_errno(getsockopt(sockfd
, level
, optname
,
2388 if (len
> sizeof(struct target_timeval
)) {
2389 len
= sizeof(struct target_timeval
);
2391 if (copy_to_user_timeval(optval_addr
, &tv
)) {
2392 return -TARGET_EFAULT
;
2394 if (put_user_u32(len
, optlen
)) {
2395 return -TARGET_EFAULT
;
2399 case TARGET_SO_SNDTIMEO
:
2400 optname
= SO_SNDTIMEO
;
2402 case TARGET_SO_PEERCRED
: {
2405 struct target_ucred
*tcr
;
2407 if (get_user_u32(len
, optlen
)) {
2408 return -TARGET_EFAULT
;
2411 return -TARGET_EINVAL
;
2415 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
2423 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
2424 return -TARGET_EFAULT
;
2426 __put_user(cr
.pid
, &tcr
->pid
);
2427 __put_user(cr
.uid
, &tcr
->uid
);
2428 __put_user(cr
.gid
, &tcr
->gid
);
2429 unlock_user_struct(tcr
, optval_addr
, 1);
2430 if (put_user_u32(len
, optlen
)) {
2431 return -TARGET_EFAULT
;
2435 case TARGET_SO_PEERSEC
: {
2438 if (get_user_u32(len
, optlen
)) {
2439 return -TARGET_EFAULT
;
2442 return -TARGET_EINVAL
;
2444 name
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 0);
2446 return -TARGET_EFAULT
;
2449 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERSEC
,
2451 if (put_user_u32(lv
, optlen
)) {
2452 ret
= -TARGET_EFAULT
;
2454 unlock_user(name
, optval_addr
, lv
);
2457 case TARGET_SO_LINGER
:
2461 struct target_linger
*tlg
;
2463 if (get_user_u32(len
, optlen
)) {
2464 return -TARGET_EFAULT
;
2467 return -TARGET_EINVAL
;
2471 ret
= get_errno(getsockopt(sockfd
, level
, SO_LINGER
,
2479 if (!lock_user_struct(VERIFY_WRITE
, tlg
, optval_addr
, 0)) {
2480 return -TARGET_EFAULT
;
2482 __put_user(lg
.l_onoff
, &tlg
->l_onoff
);
2483 __put_user(lg
.l_linger
, &tlg
->l_linger
);
2484 unlock_user_struct(tlg
, optval_addr
, 1);
2485 if (put_user_u32(len
, optlen
)) {
2486 return -TARGET_EFAULT
;
2490 /* Options with 'int' argument. */
2491 case TARGET_SO_DEBUG
:
2494 case TARGET_SO_REUSEADDR
:
2495 optname
= SO_REUSEADDR
;
2498 case TARGET_SO_REUSEPORT
:
2499 optname
= SO_REUSEPORT
;
2502 case TARGET_SO_TYPE
:
2505 case TARGET_SO_ERROR
:
2508 case TARGET_SO_DONTROUTE
:
2509 optname
= SO_DONTROUTE
;
2511 case TARGET_SO_BROADCAST
:
2512 optname
= SO_BROADCAST
;
2514 case TARGET_SO_SNDBUF
:
2515 optname
= SO_SNDBUF
;
2517 case TARGET_SO_RCVBUF
:
2518 optname
= SO_RCVBUF
;
2520 case TARGET_SO_KEEPALIVE
:
2521 optname
= SO_KEEPALIVE
;
2523 case TARGET_SO_OOBINLINE
:
2524 optname
= SO_OOBINLINE
;
2526 case TARGET_SO_NO_CHECK
:
2527 optname
= SO_NO_CHECK
;
2529 case TARGET_SO_PRIORITY
:
2530 optname
= SO_PRIORITY
;
2533 case TARGET_SO_BSDCOMPAT
:
2534 optname
= SO_BSDCOMPAT
;
2537 case TARGET_SO_PASSCRED
:
2538 optname
= SO_PASSCRED
;
2540 case TARGET_SO_TIMESTAMP
:
2541 optname
= SO_TIMESTAMP
;
2543 case TARGET_SO_RCVLOWAT
:
2544 optname
= SO_RCVLOWAT
;
2546 case TARGET_SO_ACCEPTCONN
:
2547 optname
= SO_ACCEPTCONN
;
2554 /* TCP options all take an 'int' value. */
2556 if (get_user_u32(len
, optlen
))
2557 return -TARGET_EFAULT
;
2559 return -TARGET_EINVAL
;
2561 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2564 if (optname
== SO_TYPE
) {
2565 val
= host_to_target_sock_type(val
);
2570 if (put_user_u32(val
, optval_addr
))
2571 return -TARGET_EFAULT
;
2573 if (put_user_u8(val
, optval_addr
))
2574 return -TARGET_EFAULT
;
2576 if (put_user_u32(len
, optlen
))
2577 return -TARGET_EFAULT
;
2584 case IP_ROUTER_ALERT
:
2588 case IP_MTU_DISCOVER
:
2594 case IP_MULTICAST_TTL
:
2595 case IP_MULTICAST_LOOP
:
2596 if (get_user_u32(len
, optlen
))
2597 return -TARGET_EFAULT
;
2599 return -TARGET_EINVAL
;
2601 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2604 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2606 if (put_user_u32(len
, optlen
)
2607 || put_user_u8(val
, optval_addr
))
2608 return -TARGET_EFAULT
;
2610 if (len
> sizeof(int))
2612 if (put_user_u32(len
, optlen
)
2613 || put_user_u32(val
, optval_addr
))
2614 return -TARGET_EFAULT
;
2618 ret
= -TARGET_ENOPROTOOPT
;
2624 case IPV6_MTU_DISCOVER
:
2627 case IPV6_RECVPKTINFO
:
2628 case IPV6_UNICAST_HOPS
:
2629 case IPV6_MULTICAST_HOPS
:
2630 case IPV6_MULTICAST_LOOP
:
2632 case IPV6_RECVHOPLIMIT
:
2633 case IPV6_2292HOPLIMIT
:
2636 case IPV6_2292PKTINFO
:
2637 case IPV6_RECVTCLASS
:
2638 case IPV6_RECVRTHDR
:
2639 case IPV6_2292RTHDR
:
2640 case IPV6_RECVHOPOPTS
:
2641 case IPV6_2292HOPOPTS
:
2642 case IPV6_RECVDSTOPTS
:
2643 case IPV6_2292DSTOPTS
:
2645 #ifdef IPV6_RECVPATHMTU
2646 case IPV6_RECVPATHMTU
:
2648 #ifdef IPV6_TRANSPARENT
2649 case IPV6_TRANSPARENT
:
2651 #ifdef IPV6_FREEBIND
2654 #ifdef IPV6_RECVORIGDSTADDR
2655 case IPV6_RECVORIGDSTADDR
:
2657 if (get_user_u32(len
, optlen
))
2658 return -TARGET_EFAULT
;
2660 return -TARGET_EINVAL
;
2662 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2665 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2667 if (put_user_u32(len
, optlen
)
2668 || put_user_u8(val
, optval_addr
))
2669 return -TARGET_EFAULT
;
2671 if (len
> sizeof(int))
2673 if (put_user_u32(len
, optlen
)
2674 || put_user_u32(val
, optval_addr
))
2675 return -TARGET_EFAULT
;
2679 ret
= -TARGET_ENOPROTOOPT
;
2686 case NETLINK_PKTINFO
:
2687 case NETLINK_BROADCAST_ERROR
:
2688 case NETLINK_NO_ENOBUFS
:
2689 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2690 case NETLINK_LISTEN_ALL_NSID
:
2691 case NETLINK_CAP_ACK
:
2692 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2693 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2694 case NETLINK_EXT_ACK
:
2695 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2696 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2697 case NETLINK_GET_STRICT_CHK
:
2698 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2699 if (get_user_u32(len
, optlen
)) {
2700 return -TARGET_EFAULT
;
2702 if (len
!= sizeof(val
)) {
2703 return -TARGET_EINVAL
;
2706 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2710 if (put_user_u32(lv
, optlen
)
2711 || put_user_u32(val
, optval_addr
)) {
2712 return -TARGET_EFAULT
;
2715 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2716 case NETLINK_LIST_MEMBERSHIPS
:
2720 if (get_user_u32(len
, optlen
)) {
2721 return -TARGET_EFAULT
;
2724 return -TARGET_EINVAL
;
2726 results
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 1);
2728 return -TARGET_EFAULT
;
2731 ret
= get_errno(getsockopt(sockfd
, level
, optname
, results
, &lv
));
2733 unlock_user(results
, optval_addr
, 0);
2736 /* swap host endianess to target endianess. */
2737 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
2738 results
[i
] = tswap32(results
[i
]);
2740 if (put_user_u32(lv
, optlen
)) {
2741 return -TARGET_EFAULT
;
2743 unlock_user(results
, optval_addr
, 0);
2746 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2751 #endif /* SOL_NETLINK */
2754 qemu_log_mask(LOG_UNIMP
,
2755 "getsockopt level=%d optname=%d not yet supported\n",
2757 ret
= -TARGET_EOPNOTSUPP
;
2763 /* Convert target low/high pair representing file offset into the host
2764 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2765 * as the kernel doesn't handle them either.
2767 static void target_to_host_low_high(abi_ulong tlow
,
2769 unsigned long *hlow
,
2770 unsigned long *hhigh
)
2772 uint64_t off
= tlow
|
2773 ((unsigned long long)thigh
<< TARGET_LONG_BITS
/ 2) <<
2774 TARGET_LONG_BITS
/ 2;
2777 *hhigh
= (off
>> HOST_LONG_BITS
/ 2) >> HOST_LONG_BITS
/ 2;
2780 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
2781 abi_ulong count
, int copy
)
2783 struct target_iovec
*target_vec
;
2785 abi_ulong total_len
, max_len
;
2788 bool bad_address
= false;
2794 if (count
> IOV_MAX
) {
2799 vec
= g_try_new0(struct iovec
, count
);
2805 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2806 count
* sizeof(struct target_iovec
), 1);
2807 if (target_vec
== NULL
) {
2812 /* ??? If host page size > target page size, this will result in a
2813 value larger than what we can actually support. */
2814 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
2817 for (i
= 0; i
< count
; i
++) {
2818 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2819 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2824 } else if (len
== 0) {
2825 /* Zero length pointer is ignored. */
2826 vec
[i
].iov_base
= 0;
2828 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
2829 /* If the first buffer pointer is bad, this is a fault. But
2830 * subsequent bad buffers will result in a partial write; this
2831 * is realized by filling the vector with null pointers and
2833 if (!vec
[i
].iov_base
) {
2844 if (len
> max_len
- total_len
) {
2845 len
= max_len
- total_len
;
2848 vec
[i
].iov_len
= len
;
2852 unlock_user(target_vec
, target_addr
, 0);
2857 if (tswapal(target_vec
[i
].iov_len
) > 0) {
2858 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
2861 unlock_user(target_vec
, target_addr
, 0);
2868 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
2869 abi_ulong count
, int copy
)
2871 struct target_iovec
*target_vec
;
2874 target_vec
= lock_user(VERIFY_READ
, target_addr
,
2875 count
* sizeof(struct target_iovec
), 1);
2877 for (i
= 0; i
< count
; i
++) {
2878 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
2879 abi_long len
= tswapal(target_vec
[i
].iov_len
);
2883 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
2885 unlock_user(target_vec
, target_addr
, 0);
2891 static inline int target_to_host_sock_type(int *type
)
2894 int target_type
= *type
;
2896 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
2897 case TARGET_SOCK_DGRAM
:
2898 host_type
= SOCK_DGRAM
;
2900 case TARGET_SOCK_STREAM
:
2901 host_type
= SOCK_STREAM
;
2904 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
2907 if (target_type
& TARGET_SOCK_CLOEXEC
) {
2908 #if defined(SOCK_CLOEXEC)
2909 host_type
|= SOCK_CLOEXEC
;
2911 return -TARGET_EINVAL
;
2914 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2915 #if defined(SOCK_NONBLOCK)
2916 host_type
|= SOCK_NONBLOCK
;
2917 #elif !defined(O_NONBLOCK)
2918 return -TARGET_EINVAL
;
2925 /* Try to emulate socket type flags after socket creation. */
2926 static int sock_flags_fixup(int fd
, int target_type
)
2928 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
2929 if (target_type
& TARGET_SOCK_NONBLOCK
) {
2930 int flags
= fcntl(fd
, F_GETFL
);
2931 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
2933 return -TARGET_EINVAL
;
2940 /* do_socket() Must return target values and target errnos. */
2941 static abi_long
do_socket(int domain
, int type
, int protocol
)
2943 int target_type
= type
;
2946 ret
= target_to_host_sock_type(&type
);
2951 if (domain
== PF_NETLINK
&& !(
2952 #ifdef CONFIG_RTNETLINK
2953 protocol
== NETLINK_ROUTE
||
2955 protocol
== NETLINK_KOBJECT_UEVENT
||
2956 protocol
== NETLINK_AUDIT
)) {
2957 return -EPFNOSUPPORT
;
2960 if (domain
== AF_PACKET
||
2961 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
2962 protocol
= tswap16(protocol
);
2965 ret
= get_errno(socket(domain
, type
, protocol
));
2967 ret
= sock_flags_fixup(ret
, target_type
);
2968 if (type
== SOCK_PACKET
) {
2969 /* Manage an obsolete case :
2970 * if socket type is SOCK_PACKET, bind by name
2972 fd_trans_register(ret
, &target_packet_trans
);
2973 } else if (domain
== PF_NETLINK
) {
2975 #ifdef CONFIG_RTNETLINK
2977 fd_trans_register(ret
, &target_netlink_route_trans
);
2980 case NETLINK_KOBJECT_UEVENT
:
2981 /* nothing to do: messages are strings */
2984 fd_trans_register(ret
, &target_netlink_audit_trans
);
2987 g_assert_not_reached();
2994 /* do_bind() Must return target values and target errnos. */
2995 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
3001 if ((int)addrlen
< 0) {
3002 return -TARGET_EINVAL
;
3005 addr
= alloca(addrlen
+1);
3007 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3011 return get_errno(bind(sockfd
, addr
, addrlen
));
3014 /* do_connect() Must return target values and target errnos. */
3015 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
3021 if ((int)addrlen
< 0) {
3022 return -TARGET_EINVAL
;
3025 addr
= alloca(addrlen
+1);
3027 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3031 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
3034 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3035 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
3036 int flags
, int send
)
3042 abi_ulong target_vec
;
3044 if (msgp
->msg_name
) {
3045 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
3046 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3047 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3048 tswapal(msgp
->msg_name
),
3050 if (ret
== -TARGET_EFAULT
) {
3051 /* For connected sockets msg_name and msg_namelen must
3052 * be ignored, so returning EFAULT immediately is wrong.
3053 * Instead, pass a bad msg_name to the host kernel, and
3054 * let it decide whether to return EFAULT or not.
3056 msg
.msg_name
= (void *)-1;
3061 msg
.msg_name
= NULL
;
3062 msg
.msg_namelen
= 0;
3064 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3065 msg
.msg_control
= alloca(msg
.msg_controllen
);
3066 memset(msg
.msg_control
, 0, msg
.msg_controllen
);
3068 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3070 count
= tswapal(msgp
->msg_iovlen
);
3071 target_vec
= tswapal(msgp
->msg_iov
);
3073 if (count
> IOV_MAX
) {
3074 /* sendrcvmsg returns a different errno for this condition than
3075 * readv/writev, so we must catch it here before lock_iovec() does.
3077 ret
= -TARGET_EMSGSIZE
;
3081 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
3082 target_vec
, count
, send
);
3084 ret
= -host_to_target_errno(errno
);
3087 msg
.msg_iovlen
= count
;
3091 if (fd_trans_target_to_host_data(fd
)) {
3094 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3095 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3096 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3097 msg
.msg_iov
->iov_len
);
3099 msg
.msg_iov
->iov_base
= host_msg
;
3100 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3104 ret
= target_to_host_cmsg(&msg
, msgp
);
3106 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3110 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3111 if (!is_error(ret
)) {
3113 if (fd_trans_host_to_target_data(fd
)) {
3114 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3115 MIN(msg
.msg_iov
->iov_len
, len
));
3117 ret
= host_to_target_cmsg(msgp
, &msg
);
3119 if (!is_error(ret
)) {
3120 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3121 msgp
->msg_flags
= tswap32(msg
.msg_flags
);
3122 if (msg
.msg_name
!= NULL
&& msg
.msg_name
!= (void *)-1) {
3123 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3124 msg
.msg_name
, msg
.msg_namelen
);
3136 unlock_iovec(vec
, target_vec
, count
, !send
);
3141 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3142 int flags
, int send
)
3145 struct target_msghdr
*msgp
;
3147 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
3151 return -TARGET_EFAULT
;
3153 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3154 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
3158 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3159 * so it might not have this *mmsg-specific flag either.
3161 #ifndef MSG_WAITFORONE
3162 #define MSG_WAITFORONE 0x10000
3165 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3166 unsigned int vlen
, unsigned int flags
,
3169 struct target_mmsghdr
*mmsgp
;
3173 if (vlen
> UIO_MAXIOV
) {
3177 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3179 return -TARGET_EFAULT
;
3182 for (i
= 0; i
< vlen
; i
++) {
3183 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3184 if (is_error(ret
)) {
3187 mmsgp
[i
].msg_len
= tswap32(ret
);
3188 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3189 if (flags
& MSG_WAITFORONE
) {
3190 flags
|= MSG_DONTWAIT
;
3194 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3196 /* Return number of datagrams sent if we sent any at all;
3197 * otherwise return the error.
3205 /* do_accept4() Must return target values and target errnos. */
3206 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3207 abi_ulong target_addrlen_addr
, int flags
)
3209 socklen_t addrlen
, ret_addrlen
;
3214 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
3216 if (target_addr
== 0) {
3217 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3220 /* linux returns EINVAL if addrlen pointer is invalid */
3221 if (get_user_u32(addrlen
, target_addrlen_addr
))
3222 return -TARGET_EINVAL
;
3224 if ((int)addrlen
< 0) {
3225 return -TARGET_EINVAL
;
3228 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3229 return -TARGET_EINVAL
;
3231 addr
= alloca(addrlen
);
3233 ret_addrlen
= addrlen
;
3234 ret
= get_errno(safe_accept4(fd
, addr
, &ret_addrlen
, host_flags
));
3235 if (!is_error(ret
)) {
3236 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3237 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3238 ret
= -TARGET_EFAULT
;
3244 /* do_getpeername() Must return target values and target errnos. */
3245 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3246 abi_ulong target_addrlen_addr
)
3248 socklen_t addrlen
, ret_addrlen
;
3252 if (get_user_u32(addrlen
, target_addrlen_addr
))
3253 return -TARGET_EFAULT
;
3255 if ((int)addrlen
< 0) {
3256 return -TARGET_EINVAL
;
3259 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3260 return -TARGET_EFAULT
;
3262 addr
= alloca(addrlen
);
3264 ret_addrlen
= addrlen
;
3265 ret
= get_errno(getpeername(fd
, addr
, &ret_addrlen
));
3266 if (!is_error(ret
)) {
3267 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3268 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3269 ret
= -TARGET_EFAULT
;
3275 /* do_getsockname() Must return target values and target errnos. */
3276 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3277 abi_ulong target_addrlen_addr
)
3279 socklen_t addrlen
, ret_addrlen
;
3283 if (get_user_u32(addrlen
, target_addrlen_addr
))
3284 return -TARGET_EFAULT
;
3286 if ((int)addrlen
< 0) {
3287 return -TARGET_EINVAL
;
3290 if (!access_ok(VERIFY_WRITE
, target_addr
, addrlen
))
3291 return -TARGET_EFAULT
;
3293 addr
= alloca(addrlen
);
3295 ret_addrlen
= addrlen
;
3296 ret
= get_errno(getsockname(fd
, addr
, &ret_addrlen
));
3297 if (!is_error(ret
)) {
3298 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3299 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3300 ret
= -TARGET_EFAULT
;
3306 /* do_socketpair() Must return target values and target errnos. */
3307 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3308 abi_ulong target_tab_addr
)
3313 target_to_host_sock_type(&type
);
3315 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
3316 if (!is_error(ret
)) {
3317 if (put_user_s32(tab
[0], target_tab_addr
)
3318 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
3319 ret
= -TARGET_EFAULT
;
3324 /* do_sendto() Must return target values and target errnos. */
3325 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
3326 abi_ulong target_addr
, socklen_t addrlen
)
3330 void *copy_msg
= NULL
;
3333 if ((int)addrlen
< 0) {
3334 return -TARGET_EINVAL
;
3337 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
3339 return -TARGET_EFAULT
;
3340 if (fd_trans_target_to_host_data(fd
)) {
3341 copy_msg
= host_msg
;
3342 host_msg
= g_malloc(len
);
3343 memcpy(host_msg
, copy_msg
, len
);
3344 ret
= fd_trans_target_to_host_data(fd
)(host_msg
, len
);
3350 addr
= alloca(addrlen
+1);
3351 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
3355 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
3357 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, NULL
, 0));
3362 host_msg
= copy_msg
;
3364 unlock_user(host_msg
, msg
, 0);
3368 /* do_recvfrom() Must return target values and target errnos. */
3369 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
3370 abi_ulong target_addr
,
3371 abi_ulong target_addrlen
)
3373 socklen_t addrlen
, ret_addrlen
;
3378 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
3380 return -TARGET_EFAULT
;
3382 if (get_user_u32(addrlen
, target_addrlen
)) {
3383 ret
= -TARGET_EFAULT
;
3386 if ((int)addrlen
< 0) {
3387 ret
= -TARGET_EINVAL
;
3390 addr
= alloca(addrlen
);
3391 ret_addrlen
= addrlen
;
3392 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
,
3393 addr
, &ret_addrlen
));
3395 addr
= NULL
; /* To keep compiler quiet. */
3396 addrlen
= 0; /* To keep compiler quiet. */
3397 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
, NULL
, 0));
3399 if (!is_error(ret
)) {
3400 if (fd_trans_host_to_target_data(fd
)) {
3402 trans
= fd_trans_host_to_target_data(fd
)(host_msg
, MIN(ret
, len
));
3403 if (is_error(trans
)) {
3409 host_to_target_sockaddr(target_addr
, addr
,
3410 MIN(addrlen
, ret_addrlen
));
3411 if (put_user_u32(ret_addrlen
, target_addrlen
)) {
3412 ret
= -TARGET_EFAULT
;
3416 unlock_user(host_msg
, msg
, len
);
3419 unlock_user(host_msg
, msg
, 0);
3424 #ifdef TARGET_NR_socketcall
3425 /* do_socketcall() must return target values and target errnos. */
3426 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
3428 static const unsigned nargs
[] = { /* number of arguments per operation */
3429 [TARGET_SYS_SOCKET
] = 3, /* domain, type, protocol */
3430 [TARGET_SYS_BIND
] = 3, /* fd, addr, addrlen */
3431 [TARGET_SYS_CONNECT
] = 3, /* fd, addr, addrlen */
3432 [TARGET_SYS_LISTEN
] = 2, /* fd, backlog */
3433 [TARGET_SYS_ACCEPT
] = 3, /* fd, addr, addrlen */
3434 [TARGET_SYS_GETSOCKNAME
] = 3, /* fd, addr, addrlen */
3435 [TARGET_SYS_GETPEERNAME
] = 3, /* fd, addr, addrlen */
3436 [TARGET_SYS_SOCKETPAIR
] = 4, /* domain, type, protocol, tab */
3437 [TARGET_SYS_SEND
] = 4, /* fd, msg, len, flags */
3438 [TARGET_SYS_RECV
] = 4, /* fd, msg, len, flags */
3439 [TARGET_SYS_SENDTO
] = 6, /* fd, msg, len, flags, addr, addrlen */
3440 [TARGET_SYS_RECVFROM
] = 6, /* fd, msg, len, flags, addr, addrlen */
3441 [TARGET_SYS_SHUTDOWN
] = 2, /* fd, how */
3442 [TARGET_SYS_SETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
3443 [TARGET_SYS_GETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
3444 [TARGET_SYS_SENDMSG
] = 3, /* fd, msg, flags */
3445 [TARGET_SYS_RECVMSG
] = 3, /* fd, msg, flags */
3446 [TARGET_SYS_ACCEPT4
] = 4, /* fd, addr, addrlen, flags */
3447 [TARGET_SYS_RECVMMSG
] = 4, /* fd, msgvec, vlen, flags */
3448 [TARGET_SYS_SENDMMSG
] = 4, /* fd, msgvec, vlen, flags */
3450 abi_long a
[6]; /* max 6 args */
3453 /* check the range of the first argument num */
3454 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3455 if (num
< 1 || num
> TARGET_SYS_SENDMMSG
) {
3456 return -TARGET_EINVAL
;
3458 /* ensure we have space for args */
3459 if (nargs
[num
] > ARRAY_SIZE(a
)) {
3460 return -TARGET_EINVAL
;
3462 /* collect the arguments in a[] according to nargs[] */
3463 for (i
= 0; i
< nargs
[num
]; ++i
) {
3464 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
3465 return -TARGET_EFAULT
;
3468 /* now when we have the args, invoke the appropriate underlying function */
3470 case TARGET_SYS_SOCKET
: /* domain, type, protocol */
3471 return do_socket(a
[0], a
[1], a
[2]);
3472 case TARGET_SYS_BIND
: /* sockfd, addr, addrlen */
3473 return do_bind(a
[0], a
[1], a
[2]);
3474 case TARGET_SYS_CONNECT
: /* sockfd, addr, addrlen */
3475 return do_connect(a
[0], a
[1], a
[2]);
3476 case TARGET_SYS_LISTEN
: /* sockfd, backlog */
3477 return get_errno(listen(a
[0], a
[1]));
3478 case TARGET_SYS_ACCEPT
: /* sockfd, addr, addrlen */
3479 return do_accept4(a
[0], a
[1], a
[2], 0);
3480 case TARGET_SYS_GETSOCKNAME
: /* sockfd, addr, addrlen */
3481 return do_getsockname(a
[0], a
[1], a
[2]);
3482 case TARGET_SYS_GETPEERNAME
: /* sockfd, addr, addrlen */
3483 return do_getpeername(a
[0], a
[1], a
[2]);
3484 case TARGET_SYS_SOCKETPAIR
: /* domain, type, protocol, tab */
3485 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
3486 case TARGET_SYS_SEND
: /* sockfd, msg, len, flags */
3487 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
3488 case TARGET_SYS_RECV
: /* sockfd, msg, len, flags */
3489 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
3490 case TARGET_SYS_SENDTO
: /* sockfd, msg, len, flags, addr, addrlen */
3491 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3492 case TARGET_SYS_RECVFROM
: /* sockfd, msg, len, flags, addr, addrlen */
3493 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3494 case TARGET_SYS_SHUTDOWN
: /* sockfd, how */
3495 return get_errno(shutdown(a
[0], a
[1]));
3496 case TARGET_SYS_SETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
3497 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3498 case TARGET_SYS_GETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
3499 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3500 case TARGET_SYS_SENDMSG
: /* sockfd, msg, flags */
3501 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
3502 case TARGET_SYS_RECVMSG
: /* sockfd, msg, flags */
3503 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
3504 case TARGET_SYS_ACCEPT4
: /* sockfd, addr, addrlen, flags */
3505 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
3506 case TARGET_SYS_RECVMMSG
: /* sockfd, msgvec, vlen, flags */
3507 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
3508 case TARGET_SYS_SENDMMSG
: /* sockfd, msgvec, vlen, flags */
3509 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
3511 qemu_log_mask(LOG_UNIMP
, "Unsupported socketcall: %d\n", num
);
3512 return -TARGET_EINVAL
;
3517 #define N_SHM_REGIONS 32
3519 static struct shm_region
{
3523 } shm_regions
[N_SHM_REGIONS
];
3525 #ifndef TARGET_SEMID64_DS
3526 /* asm-generic version of this struct */
3527 struct target_semid64_ds
3529 struct target_ipc_perm sem_perm
;
3530 abi_ulong sem_otime
;
3531 #if TARGET_ABI_BITS == 32
3532 abi_ulong __unused1
;
3534 abi_ulong sem_ctime
;
3535 #if TARGET_ABI_BITS == 32
3536 abi_ulong __unused2
;
3538 abi_ulong sem_nsems
;
3539 abi_ulong __unused3
;
3540 abi_ulong __unused4
;
3544 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
3545 abi_ulong target_addr
)
3547 struct target_ipc_perm
*target_ip
;
3548 struct target_semid64_ds
*target_sd
;
3550 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3551 return -TARGET_EFAULT
;
3552 target_ip
= &(target_sd
->sem_perm
);
3553 host_ip
->__key
= tswap32(target_ip
->__key
);
3554 host_ip
->uid
= tswap32(target_ip
->uid
);
3555 host_ip
->gid
= tswap32(target_ip
->gid
);
3556 host_ip
->cuid
= tswap32(target_ip
->cuid
);
3557 host_ip
->cgid
= tswap32(target_ip
->cgid
);
3558 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3559 host_ip
->mode
= tswap32(target_ip
->mode
);
3561 host_ip
->mode
= tswap16(target_ip
->mode
);
3563 #if defined(TARGET_PPC)
3564 host_ip
->__seq
= tswap32(target_ip
->__seq
);
3566 host_ip
->__seq
= tswap16(target_ip
->__seq
);
3568 unlock_user_struct(target_sd
, target_addr
, 0);
3572 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
3573 struct ipc_perm
*host_ip
)
3575 struct target_ipc_perm
*target_ip
;
3576 struct target_semid64_ds
*target_sd
;
3578 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3579 return -TARGET_EFAULT
;
3580 target_ip
= &(target_sd
->sem_perm
);
3581 target_ip
->__key
= tswap32(host_ip
->__key
);
3582 target_ip
->uid
= tswap32(host_ip
->uid
);
3583 target_ip
->gid
= tswap32(host_ip
->gid
);
3584 target_ip
->cuid
= tswap32(host_ip
->cuid
);
3585 target_ip
->cgid
= tswap32(host_ip
->cgid
);
3586 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3587 target_ip
->mode
= tswap32(host_ip
->mode
);
3589 target_ip
->mode
= tswap16(host_ip
->mode
);
3591 #if defined(TARGET_PPC)
3592 target_ip
->__seq
= tswap32(host_ip
->__seq
);
3594 target_ip
->__seq
= tswap16(host_ip
->__seq
);
3596 unlock_user_struct(target_sd
, target_addr
, 1);
3600 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
3601 abi_ulong target_addr
)
3603 struct target_semid64_ds
*target_sd
;
3605 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3606 return -TARGET_EFAULT
;
3607 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
3608 return -TARGET_EFAULT
;
3609 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
3610 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
3611 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
3612 unlock_user_struct(target_sd
, target_addr
, 0);
3616 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
3617 struct semid_ds
*host_sd
)
3619 struct target_semid64_ds
*target_sd
;
3621 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3622 return -TARGET_EFAULT
;
3623 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
3624 return -TARGET_EFAULT
;
3625 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
3626 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
3627 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
3628 unlock_user_struct(target_sd
, target_addr
, 1);
3632 struct target_seminfo
{
3645 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
3646 struct seminfo
*host_seminfo
)
3648 struct target_seminfo
*target_seminfo
;
3649 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
3650 return -TARGET_EFAULT
;
3651 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
3652 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
3653 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
3654 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
3655 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
3656 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
3657 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
3658 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
3659 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
3660 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
3661 unlock_user_struct(target_seminfo
, target_addr
, 1);
3667 struct semid_ds
*buf
;
3668 unsigned short *array
;
3669 struct seminfo
*__buf
;
3672 union target_semun
{
3679 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
3680 abi_ulong target_addr
)
3683 unsigned short *array
;
3685 struct semid_ds semid_ds
;
3688 semun
.buf
= &semid_ds
;
3690 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3692 return get_errno(ret
);
3694 nsems
= semid_ds
.sem_nsems
;
3696 *host_array
= g_try_new(unsigned short, nsems
);
3698 return -TARGET_ENOMEM
;
3700 array
= lock_user(VERIFY_READ
, target_addr
,
3701 nsems
*sizeof(unsigned short), 1);
3703 g_free(*host_array
);
3704 return -TARGET_EFAULT
;
3707 for(i
=0; i
<nsems
; i
++) {
3708 __get_user((*host_array
)[i
], &array
[i
]);
3710 unlock_user(array
, target_addr
, 0);
3715 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
3716 unsigned short **host_array
)
3719 unsigned short *array
;
3721 struct semid_ds semid_ds
;
3724 semun
.buf
= &semid_ds
;
3726 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3728 return get_errno(ret
);
3730 nsems
= semid_ds
.sem_nsems
;
3732 array
= lock_user(VERIFY_WRITE
, target_addr
,
3733 nsems
*sizeof(unsigned short), 0);
3735 return -TARGET_EFAULT
;
3737 for(i
=0; i
<nsems
; i
++) {
3738 __put_user((*host_array
)[i
], &array
[i
]);
3740 g_free(*host_array
);
3741 unlock_user(array
, target_addr
, 1);
3746 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
3747 abi_ulong target_arg
)
3749 union target_semun target_su
= { .buf
= target_arg
};
3751 struct semid_ds dsarg
;
3752 unsigned short *array
= NULL
;
3753 struct seminfo seminfo
;
3754 abi_long ret
= -TARGET_EINVAL
;
3761 /* In 64 bit cross-endian situations, we will erroneously pick up
3762 * the wrong half of the union for the "val" element. To rectify
3763 * this, the entire 8-byte structure is byteswapped, followed by
3764 * a swap of the 4 byte val field. In other cases, the data is
3765 * already in proper host byte order. */
3766 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
3767 target_su
.buf
= tswapal(target_su
.buf
);
3768 arg
.val
= tswap32(target_su
.val
);
3770 arg
.val
= target_su
.val
;
3772 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3776 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
3780 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3781 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
3788 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
3792 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3793 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
3799 arg
.__buf
= &seminfo
;
3800 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3801 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
3809 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
3816 struct target_sembuf
{
3817 unsigned short sem_num
;
3822 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
3823 abi_ulong target_addr
,
3826 struct target_sembuf
*target_sembuf
;
3829 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
3830 nsops
*sizeof(struct target_sembuf
), 1);
3832 return -TARGET_EFAULT
;
3834 for(i
=0; i
<nsops
; i
++) {
3835 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
3836 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
3837 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
3840 unlock_user(target_sembuf
, target_addr
, 0);
3845 static inline abi_long
do_semop(int semid
, abi_long ptr
, unsigned nsops
)
3847 struct sembuf sops
[nsops
];
3850 if (target_to_host_sembuf(sops
, ptr
, nsops
))
3851 return -TARGET_EFAULT
;
3853 ret
= -TARGET_ENOSYS
;
3854 #ifdef __NR_semtimedop
3855 ret
= get_errno(safe_semtimedop(semid
, sops
, nsops
, NULL
));
3858 if (ret
== -TARGET_ENOSYS
) {
3859 ret
= get_errno(safe_ipc(IPCOP_semtimedop
, semid
, nsops
, 0, sops
, 0));
3865 struct target_msqid_ds
3867 struct target_ipc_perm msg_perm
;
3868 abi_ulong msg_stime
;
3869 #if TARGET_ABI_BITS == 32
3870 abi_ulong __unused1
;
3872 abi_ulong msg_rtime
;
3873 #if TARGET_ABI_BITS == 32
3874 abi_ulong __unused2
;
3876 abi_ulong msg_ctime
;
3877 #if TARGET_ABI_BITS == 32
3878 abi_ulong __unused3
;
3880 abi_ulong __msg_cbytes
;
3882 abi_ulong msg_qbytes
;
3883 abi_ulong msg_lspid
;
3884 abi_ulong msg_lrpid
;
3885 abi_ulong __unused4
;
3886 abi_ulong __unused5
;
3889 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
3890 abi_ulong target_addr
)
3892 struct target_msqid_ds
*target_md
;
3894 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
3895 return -TARGET_EFAULT
;
3896 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
3897 return -TARGET_EFAULT
;
3898 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
3899 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
3900 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
3901 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
3902 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
3903 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
3904 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
3905 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
3906 unlock_user_struct(target_md
, target_addr
, 0);
3910 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
3911 struct msqid_ds
*host_md
)
3913 struct target_msqid_ds
*target_md
;
3915 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
3916 return -TARGET_EFAULT
;
3917 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
3918 return -TARGET_EFAULT
;
3919 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
3920 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
3921 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
3922 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
3923 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
3924 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
3925 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
3926 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
3927 unlock_user_struct(target_md
, target_addr
, 1);
3931 struct target_msginfo
{
3939 unsigned short int msgseg
;
3942 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
3943 struct msginfo
*host_msginfo
)
3945 struct target_msginfo
*target_msginfo
;
3946 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
3947 return -TARGET_EFAULT
;
3948 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
3949 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
3950 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
3951 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
3952 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
3953 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
3954 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
3955 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
3956 unlock_user_struct(target_msginfo
, target_addr
, 1);
3960 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
3962 struct msqid_ds dsarg
;
3963 struct msginfo msginfo
;
3964 abi_long ret
= -TARGET_EINVAL
;
3972 if (target_to_host_msqid_ds(&dsarg
,ptr
))
3973 return -TARGET_EFAULT
;
3974 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
3975 if (host_to_target_msqid_ds(ptr
,&dsarg
))
3976 return -TARGET_EFAULT
;
3979 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
3983 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
3984 if (host_to_target_msginfo(ptr
, &msginfo
))
3985 return -TARGET_EFAULT
;
3992 struct target_msgbuf
{
3997 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
3998 ssize_t msgsz
, int msgflg
)
4000 struct target_msgbuf
*target_mb
;
4001 struct msgbuf
*host_mb
;
4005 return -TARGET_EINVAL
;
4008 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
4009 return -TARGET_EFAULT
;
4010 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4012 unlock_user_struct(target_mb
, msgp
, 0);
4013 return -TARGET_ENOMEM
;
4015 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
4016 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
4017 ret
= -TARGET_ENOSYS
;
4019 ret
= get_errno(safe_msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
4022 if (ret
== -TARGET_ENOSYS
) {
4023 ret
= get_errno(safe_ipc(IPCOP_msgsnd
, msqid
, msgsz
, msgflg
,
4028 unlock_user_struct(target_mb
, msgp
, 0);
4033 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
4034 ssize_t msgsz
, abi_long msgtyp
,
4037 struct target_msgbuf
*target_mb
;
4039 struct msgbuf
*host_mb
;
4043 return -TARGET_EINVAL
;
4046 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
4047 return -TARGET_EFAULT
;
4049 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4051 ret
= -TARGET_ENOMEM
;
4054 ret
= -TARGET_ENOSYS
;
4056 ret
= get_errno(safe_msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
4059 if (ret
== -TARGET_ENOSYS
) {
4060 ret
= get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv
), msqid
, msgsz
,
4061 msgflg
, host_mb
, msgtyp
));
4066 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
4067 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
4068 if (!target_mtext
) {
4069 ret
= -TARGET_EFAULT
;
4072 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
4073 unlock_user(target_mtext
, target_mtext_addr
, ret
);
4076 target_mb
->mtype
= tswapal(host_mb
->mtype
);
4080 unlock_user_struct(target_mb
, msgp
, 1);
4085 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
4086 abi_ulong target_addr
)
4088 struct target_shmid_ds
*target_sd
;
4090 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4091 return -TARGET_EFAULT
;
4092 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
4093 return -TARGET_EFAULT
;
4094 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4095 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4096 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4097 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4098 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4099 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4100 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4101 unlock_user_struct(target_sd
, target_addr
, 0);
4105 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
4106 struct shmid_ds
*host_sd
)
4108 struct target_shmid_ds
*target_sd
;
4110 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4111 return -TARGET_EFAULT
;
4112 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
4113 return -TARGET_EFAULT
;
4114 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4115 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4116 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4117 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4118 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4119 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4120 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4121 unlock_user_struct(target_sd
, target_addr
, 1);
4125 struct target_shminfo
{
4133 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
4134 struct shminfo
*host_shminfo
)
4136 struct target_shminfo
*target_shminfo
;
4137 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
4138 return -TARGET_EFAULT
;
4139 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
4140 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
4141 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
4142 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
4143 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
4144 unlock_user_struct(target_shminfo
, target_addr
, 1);
4148 struct target_shm_info
{
4153 abi_ulong swap_attempts
;
4154 abi_ulong swap_successes
;
4157 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
4158 struct shm_info
*host_shm_info
)
4160 struct target_shm_info
*target_shm_info
;
4161 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
4162 return -TARGET_EFAULT
;
4163 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
4164 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
4165 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
4166 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
4167 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
4168 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
4169 unlock_user_struct(target_shm_info
, target_addr
, 1);
4173 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
4175 struct shmid_ds dsarg
;
4176 struct shminfo shminfo
;
4177 struct shm_info shm_info
;
4178 abi_long ret
= -TARGET_EINVAL
;
4186 if (target_to_host_shmid_ds(&dsarg
, buf
))
4187 return -TARGET_EFAULT
;
4188 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
4189 if (host_to_target_shmid_ds(buf
, &dsarg
))
4190 return -TARGET_EFAULT
;
4193 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
4194 if (host_to_target_shminfo(buf
, &shminfo
))
4195 return -TARGET_EFAULT
;
4198 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
4199 if (host_to_target_shm_info(buf
, &shm_info
))
4200 return -TARGET_EFAULT
;
4205 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
4212 #ifndef TARGET_FORCE_SHMLBA
4213 /* For most architectures, SHMLBA is the same as the page size;
4214 * some architectures have larger values, in which case they should
4215 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
4216 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
4217 * and defining its own value for SHMLBA.
4219 * The kernel also permits SHMLBA to be set by the architecture to a
4220 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
4221 * this means that addresses are rounded to the large size if
4222 * SHM_RND is set but addresses not aligned to that size are not rejected
4223 * as long as they are at least page-aligned. Since the only architecture
4224 * which uses this is ia64 this code doesn't provide for that oddity.
4226 static inline abi_ulong
target_shmlba(CPUArchState
*cpu_env
)
4228 return TARGET_PAGE_SIZE
;
4232 static inline abi_ulong
do_shmat(CPUArchState
*cpu_env
,
4233 int shmid
, abi_ulong shmaddr
, int shmflg
)
4237 struct shmid_ds shm_info
;
4241 /* find out the length of the shared memory segment */
4242 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
4243 if (is_error(ret
)) {
4244 /* can't get length, bail out */
4248 shmlba
= target_shmlba(cpu_env
);
4250 if (shmaddr
& (shmlba
- 1)) {
4251 if (shmflg
& SHM_RND
) {
4252 shmaddr
&= ~(shmlba
- 1);
4254 return -TARGET_EINVAL
;
4257 if (!guest_range_valid(shmaddr
, shm_info
.shm_segsz
)) {
4258 return -TARGET_EINVAL
;
4264 host_raddr
= shmat(shmid
, (void *)g2h(shmaddr
), shmflg
);
4266 abi_ulong mmap_start
;
4268 /* In order to use the host shmat, we need to honor host SHMLBA. */
4269 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
, MAX(SHMLBA
, shmlba
));
4271 if (mmap_start
== -1) {
4273 host_raddr
= (void *)-1;
4275 host_raddr
= shmat(shmid
, g2h(mmap_start
), shmflg
| SHM_REMAP
);
4278 if (host_raddr
== (void *)-1) {
4280 return get_errno((long)host_raddr
);
4282 raddr
=h2g((unsigned long)host_raddr
);
4284 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
4285 PAGE_VALID
| PAGE_READ
|
4286 ((shmflg
& SHM_RDONLY
)? 0 : PAGE_WRITE
));
4288 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
4289 if (!shm_regions
[i
].in_use
) {
4290 shm_regions
[i
].in_use
= true;
4291 shm_regions
[i
].start
= raddr
;
4292 shm_regions
[i
].size
= shm_info
.shm_segsz
;
4302 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
4309 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
4310 if (shm_regions
[i
].in_use
&& shm_regions
[i
].start
== shmaddr
) {
4311 shm_regions
[i
].in_use
= false;
4312 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
4316 rv
= get_errno(shmdt(g2h(shmaddr
)));
4323 #ifdef TARGET_NR_ipc
4324 /* ??? This only works with linear mappings. */
4325 /* do_ipc() must return target values and target errnos. */
4326 static abi_long
do_ipc(CPUArchState
*cpu_env
,
4327 unsigned int call
, abi_long first
,
4328 abi_long second
, abi_long third
,
4329 abi_long ptr
, abi_long fifth
)
4334 version
= call
>> 16;
4339 ret
= do_semop(first
, ptr
, second
);
4343 ret
= get_errno(semget(first
, second
, third
));
4346 case IPCOP_semctl
: {
4347 /* The semun argument to semctl is passed by value, so dereference the
4350 get_user_ual(atptr
, ptr
);
4351 ret
= do_semctl(first
, second
, third
, atptr
);
4356 ret
= get_errno(msgget(first
, second
));
4360 ret
= do_msgsnd(first
, ptr
, second
, third
);
4364 ret
= do_msgctl(first
, second
, ptr
);
4371 struct target_ipc_kludge
{
4376 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
4377 ret
= -TARGET_EFAULT
;
4381 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
4383 unlock_user_struct(tmp
, ptr
, 0);
4387 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
4396 raddr
= do_shmat(cpu_env
, first
, ptr
, second
);
4397 if (is_error(raddr
))
4398 return get_errno(raddr
);
4399 if (put_user_ual(raddr
, third
))
4400 return -TARGET_EFAULT
;
4404 ret
= -TARGET_EINVAL
;
4409 ret
= do_shmdt(ptr
);
4413 /* IPC_* flag values are the same on all linux platforms */
4414 ret
= get_errno(shmget(first
, second
, third
));
4417 /* IPC_* and SHM_* command values are the same on all linux platforms */
4419 ret
= do_shmctl(first
, second
, ptr
);
4422 qemu_log_mask(LOG_UNIMP
, "Unsupported ipc call: %d (version %d)\n",
4424 ret
= -TARGET_ENOSYS
;
4431 /* kernel structure types definitions */
4433 #define STRUCT(name, ...) STRUCT_ ## name,
4434 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
4436 #include "syscall_types.h"
4440 #undef STRUCT_SPECIAL
4442 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
4443 #define STRUCT_SPECIAL(name)
4444 #include "syscall_types.h"
4446 #undef STRUCT_SPECIAL
4448 typedef struct IOCTLEntry IOCTLEntry
;
4450 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4451 int fd
, int cmd
, abi_long arg
);
4455 unsigned int host_cmd
;
4458 do_ioctl_fn
*do_ioctl
;
4459 const argtype arg_type
[5];
4462 #define IOC_R 0x0001
4463 #define IOC_W 0x0002
4464 #define IOC_RW (IOC_R | IOC_W)
4466 #define MAX_STRUCT_SIZE 4096
4468 #ifdef CONFIG_FIEMAP
4469 /* So fiemap access checks don't overflow on 32 bit systems.
4470 * This is very slightly smaller than the limit imposed by
4471 * the underlying kernel.
4473 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4474 / sizeof(struct fiemap_extent))
4476 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4477 int fd
, int cmd
, abi_long arg
)
4479 /* The parameter for this ioctl is a struct fiemap followed
4480 * by an array of struct fiemap_extent whose size is set
4481 * in fiemap->fm_extent_count. The array is filled in by the
4484 int target_size_in
, target_size_out
;
4486 const argtype
*arg_type
= ie
->arg_type
;
4487 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
4490 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
4494 assert(arg_type
[0] == TYPE_PTR
);
4495 assert(ie
->access
== IOC_RW
);
4497 target_size_in
= thunk_type_size(arg_type
, 0);
4498 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
4500 return -TARGET_EFAULT
;
4502 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4503 unlock_user(argptr
, arg
, 0);
4504 fm
= (struct fiemap
*)buf_temp
;
4505 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
4506 return -TARGET_EINVAL
;
4509 outbufsz
= sizeof (*fm
) +
4510 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
4512 if (outbufsz
> MAX_STRUCT_SIZE
) {
4513 /* We can't fit all the extents into the fixed size buffer.
4514 * Allocate one that is large enough and use it instead.
4516 fm
= g_try_malloc(outbufsz
);
4518 return -TARGET_ENOMEM
;
4520 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
4523 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, fm
));
4524 if (!is_error(ret
)) {
4525 target_size_out
= target_size_in
;
4526 /* An extent_count of 0 means we were only counting the extents
4527 * so there are no structs to copy
4529 if (fm
->fm_extent_count
!= 0) {
4530 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
4532 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
4534 ret
= -TARGET_EFAULT
;
4536 /* Convert the struct fiemap */
4537 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
4538 if (fm
->fm_extent_count
!= 0) {
4539 p
= argptr
+ target_size_in
;
4540 /* ...and then all the struct fiemap_extents */
4541 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
4542 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
4547 unlock_user(argptr
, arg
, target_size_out
);
4557 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4558 int fd
, int cmd
, abi_long arg
)
4560 const argtype
*arg_type
= ie
->arg_type
;
4564 struct ifconf
*host_ifconf
;
4566 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
4567 int target_ifreq_size
;
4572 abi_long target_ifc_buf
;
4576 assert(arg_type
[0] == TYPE_PTR
);
4577 assert(ie
->access
== IOC_RW
);
4580 target_size
= thunk_type_size(arg_type
, 0);
4582 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4584 return -TARGET_EFAULT
;
4585 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4586 unlock_user(argptr
, arg
, 0);
4588 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
4589 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
4590 target_ifreq_size
= thunk_type_size(ifreq_arg_type
, 0);
4592 if (target_ifc_buf
!= 0) {
4593 target_ifc_len
= host_ifconf
->ifc_len
;
4594 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
4595 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
4597 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
4598 if (outbufsz
> MAX_STRUCT_SIZE
) {
4600 * We can't fit all the extents into the fixed size buffer.
4601 * Allocate one that is large enough and use it instead.
4603 host_ifconf
= malloc(outbufsz
);
4605 return -TARGET_ENOMEM
;
4607 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
4610 host_ifc_buf
= (char *)host_ifconf
+ sizeof(*host_ifconf
);
4612 host_ifconf
->ifc_len
= host_ifc_len
;
4614 host_ifc_buf
= NULL
;
4616 host_ifconf
->ifc_buf
= host_ifc_buf
;
4618 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_ifconf
));
4619 if (!is_error(ret
)) {
4620 /* convert host ifc_len to target ifc_len */
4622 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
4623 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
4624 host_ifconf
->ifc_len
= target_ifc_len
;
4626 /* restore target ifc_buf */
4628 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
4630 /* copy struct ifconf to target user */
4632 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4634 return -TARGET_EFAULT
;
4635 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
4636 unlock_user(argptr
, arg
, target_size
);
4638 if (target_ifc_buf
!= 0) {
4639 /* copy ifreq[] to target user */
4640 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
4641 for (i
= 0; i
< nb_ifreq
; i
++) {
4642 thunk_convert(argptr
+ i
* target_ifreq_size
,
4643 host_ifc_buf
+ i
* sizeof(struct ifreq
),
4644 ifreq_arg_type
, THUNK_TARGET
);
4646 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
4657 #if defined(CONFIG_USBFS)
4658 #if HOST_LONG_BITS > 64
4659 #error USBDEVFS thunks do not support >64 bit hosts yet.
4662 uint64_t target_urb_adr
;
4663 uint64_t target_buf_adr
;
4664 char *target_buf_ptr
;
4665 struct usbdevfs_urb host_urb
;
4668 static GHashTable
*usbdevfs_urb_hashtable(void)
4670 static GHashTable
*urb_hashtable
;
4672 if (!urb_hashtable
) {
4673 urb_hashtable
= g_hash_table_new(g_int64_hash
, g_int64_equal
);
4675 return urb_hashtable
;
4678 static void urb_hashtable_insert(struct live_urb
*urb
)
4680 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4681 g_hash_table_insert(urb_hashtable
, urb
, urb
);
4684 static struct live_urb
*urb_hashtable_lookup(uint64_t target_urb_adr
)
4686 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4687 return g_hash_table_lookup(urb_hashtable
, &target_urb_adr
);
4690 static void urb_hashtable_remove(struct live_urb
*urb
)
4692 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4693 g_hash_table_remove(urb_hashtable
, urb
);
4697 do_ioctl_usbdevfs_reapurb(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4698 int fd
, int cmd
, abi_long arg
)
4700 const argtype usbfsurb_arg_type
[] = { MK_STRUCT(STRUCT_usbdevfs_urb
) };
4701 const argtype ptrvoid_arg_type
[] = { TYPE_PTRVOID
, 0, 0 };
4702 struct live_urb
*lurb
;
4706 uintptr_t target_urb_adr
;
4709 target_size
= thunk_type_size(usbfsurb_arg_type
, THUNK_TARGET
);
4711 memset(buf_temp
, 0, sizeof(uint64_t));
4712 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
4713 if (is_error(ret
)) {
4717 memcpy(&hurb
, buf_temp
, sizeof(uint64_t));
4718 lurb
= (void *)((uintptr_t)hurb
- offsetof(struct live_urb
, host_urb
));
4719 if (!lurb
->target_urb_adr
) {
4720 return -TARGET_EFAULT
;
4722 urb_hashtable_remove(lurb
);
4723 unlock_user(lurb
->target_buf_ptr
, lurb
->target_buf_adr
,
4724 lurb
->host_urb
.buffer_length
);
4725 lurb
->target_buf_ptr
= NULL
;
4727 /* restore the guest buffer pointer */
4728 lurb
->host_urb
.buffer
= (void *)(uintptr_t)lurb
->target_buf_adr
;
4730 /* update the guest urb struct */
4731 argptr
= lock_user(VERIFY_WRITE
, lurb
->target_urb_adr
, target_size
, 0);
4734 return -TARGET_EFAULT
;
4736 thunk_convert(argptr
, &lurb
->host_urb
, usbfsurb_arg_type
, THUNK_TARGET
);
4737 unlock_user(argptr
, lurb
->target_urb_adr
, target_size
);
4739 target_size
= thunk_type_size(ptrvoid_arg_type
, THUNK_TARGET
);
4740 /* write back the urb handle */
4741 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4744 return -TARGET_EFAULT
;
4747 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
4748 target_urb_adr
= lurb
->target_urb_adr
;
4749 thunk_convert(argptr
, &target_urb_adr
, ptrvoid_arg_type
, THUNK_TARGET
);
4750 unlock_user(argptr
, arg
, target_size
);
4757 do_ioctl_usbdevfs_discardurb(const IOCTLEntry
*ie
,
4758 uint8_t *buf_temp
__attribute__((unused
)),
4759 int fd
, int cmd
, abi_long arg
)
4761 struct live_urb
*lurb
;
4763 /* map target address back to host URB with metadata. */
4764 lurb
= urb_hashtable_lookup(arg
);
4766 return -TARGET_EFAULT
;
4768 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, &lurb
->host_urb
));
4772 do_ioctl_usbdevfs_submiturb(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4773 int fd
, int cmd
, abi_long arg
)
4775 const argtype
*arg_type
= ie
->arg_type
;
4780 struct live_urb
*lurb
;
4783 * each submitted URB needs to map to a unique ID for the
4784 * kernel, and that unique ID needs to be a pointer to
4785 * host memory. hence, we need to malloc for each URB.
4786 * isochronous transfers have a variable length struct.
4789 target_size
= thunk_type_size(arg_type
, THUNK_TARGET
);
4791 /* construct host copy of urb and metadata */
4792 lurb
= g_try_malloc0(sizeof(struct live_urb
));
4794 return -TARGET_ENOMEM
;
4797 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4800 return -TARGET_EFAULT
;
4802 thunk_convert(&lurb
->host_urb
, argptr
, arg_type
, THUNK_HOST
);
4803 unlock_user(argptr
, arg
, 0);
4805 lurb
->target_urb_adr
= arg
;
4806 lurb
->target_buf_adr
= (uintptr_t)lurb
->host_urb
.buffer
;
4808 /* buffer space used depends on endpoint type so lock the entire buffer */
4809 /* control type urbs should check the buffer contents for true direction */
4810 rw_dir
= lurb
->host_urb
.endpoint
& USB_DIR_IN
? VERIFY_WRITE
: VERIFY_READ
;
4811 lurb
->target_buf_ptr
= lock_user(rw_dir
, lurb
->target_buf_adr
,
4812 lurb
->host_urb
.buffer_length
, 1);
4813 if (lurb
->target_buf_ptr
== NULL
) {
4815 return -TARGET_EFAULT
;
4818 /* update buffer pointer in host copy */
4819 lurb
->host_urb
.buffer
= lurb
->target_buf_ptr
;
4821 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, &lurb
->host_urb
));
4822 if (is_error(ret
)) {
4823 unlock_user(lurb
->target_buf_ptr
, lurb
->target_buf_adr
, 0);
4826 urb_hashtable_insert(lurb
);
4831 #endif /* CONFIG_USBFS */
4833 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
4834 int cmd
, abi_long arg
)
4837 struct dm_ioctl
*host_dm
;
4838 abi_long guest_data
;
4839 uint32_t guest_data_size
;
4841 const argtype
*arg_type
= ie
->arg_type
;
4843 void *big_buf
= NULL
;
4847 target_size
= thunk_type_size(arg_type
, 0);
4848 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4850 ret
= -TARGET_EFAULT
;
4853 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4854 unlock_user(argptr
, arg
, 0);
4856 /* buf_temp is too small, so fetch things into a bigger buffer */
4857 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
4858 memcpy(big_buf
, buf_temp
, target_size
);
4862 guest_data
= arg
+ host_dm
->data_start
;
4863 if ((guest_data
- arg
) < 0) {
4864 ret
= -TARGET_EINVAL
;
4867 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
4868 host_data
= (char*)host_dm
+ host_dm
->data_start
;
4870 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
4872 ret
= -TARGET_EFAULT
;
4876 switch (ie
->host_cmd
) {
4878 case DM_LIST_DEVICES
:
4881 case DM_DEV_SUSPEND
:
4884 case DM_TABLE_STATUS
:
4885 case DM_TABLE_CLEAR
:
4887 case DM_LIST_VERSIONS
:
4891 case DM_DEV_SET_GEOMETRY
:
4892 /* data contains only strings */
4893 memcpy(host_data
, argptr
, guest_data_size
);
4896 memcpy(host_data
, argptr
, guest_data_size
);
4897 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
4901 void *gspec
= argptr
;
4902 void *cur_data
= host_data
;
4903 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
4904 int spec_size
= thunk_type_size(arg_type
, 0);
4907 for (i
= 0; i
< host_dm
->target_count
; i
++) {
4908 struct dm_target_spec
*spec
= cur_data
;
4912 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
4913 slen
= strlen((char*)gspec
+ spec_size
) + 1;
4915 spec
->next
= sizeof(*spec
) + slen
;
4916 strcpy((char*)&spec
[1], gspec
+ spec_size
);
4918 cur_data
+= spec
->next
;
4923 ret
= -TARGET_EINVAL
;
4924 unlock_user(argptr
, guest_data
, 0);
4927 unlock_user(argptr
, guest_data
, 0);
4929 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
4930 if (!is_error(ret
)) {
4931 guest_data
= arg
+ host_dm
->data_start
;
4932 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
4933 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
4934 switch (ie
->host_cmd
) {
4939 case DM_DEV_SUSPEND
:
4942 case DM_TABLE_CLEAR
:
4944 case DM_DEV_SET_GEOMETRY
:
4945 /* no return data */
4947 case DM_LIST_DEVICES
:
4949 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
4950 uint32_t remaining_data
= guest_data_size
;
4951 void *cur_data
= argptr
;
4952 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
4953 int nl_size
= 12; /* can't use thunk_size due to alignment */
4956 uint32_t next
= nl
->next
;
4958 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
4960 if (remaining_data
< nl
->next
) {
4961 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
4964 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
4965 strcpy(cur_data
+ nl_size
, nl
->name
);
4966 cur_data
+= nl
->next
;
4967 remaining_data
-= nl
->next
;
4971 nl
= (void*)nl
+ next
;
4976 case DM_TABLE_STATUS
:
4978 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
4979 void *cur_data
= argptr
;
4980 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
4981 int spec_size
= thunk_type_size(arg_type
, 0);
4984 for (i
= 0; i
< host_dm
->target_count
; i
++) {
4985 uint32_t next
= spec
->next
;
4986 int slen
= strlen((char*)&spec
[1]) + 1;
4987 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
4988 if (guest_data_size
< spec
->next
) {
4989 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
4992 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
4993 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
4994 cur_data
= argptr
+ spec
->next
;
4995 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
5001 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
5002 int count
= *(uint32_t*)hdata
;
5003 uint64_t *hdev
= hdata
+ 8;
5004 uint64_t *gdev
= argptr
+ 8;
5007 *(uint32_t*)argptr
= tswap32(count
);
5008 for (i
= 0; i
< count
; i
++) {
5009 *gdev
= tswap64(*hdev
);
5015 case DM_LIST_VERSIONS
:
5017 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
5018 uint32_t remaining_data
= guest_data_size
;
5019 void *cur_data
= argptr
;
5020 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
5021 int vers_size
= thunk_type_size(arg_type
, 0);
5024 uint32_t next
= vers
->next
;
5026 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
5028 if (remaining_data
< vers
->next
) {
5029 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5032 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
5033 strcpy(cur_data
+ vers_size
, vers
->name
);
5034 cur_data
+= vers
->next
;
5035 remaining_data
-= vers
->next
;
5039 vers
= (void*)vers
+ next
;
5044 unlock_user(argptr
, guest_data
, 0);
5045 ret
= -TARGET_EINVAL
;
5048 unlock_user(argptr
, guest_data
, guest_data_size
);
5050 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5052 ret
= -TARGET_EFAULT
;
5055 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5056 unlock_user(argptr
, arg
, target_size
);
5063 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5064 int cmd
, abi_long arg
)
5068 const argtype
*arg_type
= ie
->arg_type
;
5069 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
5072 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
5073 struct blkpg_partition host_part
;
5075 /* Read and convert blkpg */
5077 target_size
= thunk_type_size(arg_type
, 0);
5078 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5080 ret
= -TARGET_EFAULT
;
5083 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5084 unlock_user(argptr
, arg
, 0);
5086 switch (host_blkpg
->op
) {
5087 case BLKPG_ADD_PARTITION
:
5088 case BLKPG_DEL_PARTITION
:
5089 /* payload is struct blkpg_partition */
5092 /* Unknown opcode */
5093 ret
= -TARGET_EINVAL
;
5097 /* Read and convert blkpg->data */
5098 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
5099 target_size
= thunk_type_size(part_arg_type
, 0);
5100 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5102 ret
= -TARGET_EFAULT
;
5105 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
5106 unlock_user(argptr
, arg
, 0);
5108 /* Swizzle the data pointer to our local copy and call! */
5109 host_blkpg
->data
= &host_part
;
5110 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_blkpg
));
5116 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5117 int fd
, int cmd
, abi_long arg
)
5119 const argtype
*arg_type
= ie
->arg_type
;
5120 const StructEntry
*se
;
5121 const argtype
*field_types
;
5122 const int *dst_offsets
, *src_offsets
;
5125 abi_ulong
*target_rt_dev_ptr
= NULL
;
5126 unsigned long *host_rt_dev_ptr
= NULL
;
5130 assert(ie
->access
== IOC_W
);
5131 assert(*arg_type
== TYPE_PTR
);
5133 assert(*arg_type
== TYPE_STRUCT
);
5134 target_size
= thunk_type_size(arg_type
, 0);
5135 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5137 return -TARGET_EFAULT
;
5140 assert(*arg_type
== (int)STRUCT_rtentry
);
5141 se
= struct_entries
+ *arg_type
++;
5142 assert(se
->convert
[0] == NULL
);
5143 /* convert struct here to be able to catch rt_dev string */
5144 field_types
= se
->field_types
;
5145 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
5146 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
5147 for (i
= 0; i
< se
->nb_fields
; i
++) {
5148 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
5149 assert(*field_types
== TYPE_PTRVOID
);
5150 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
5151 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
5152 if (*target_rt_dev_ptr
!= 0) {
5153 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
5154 tswapal(*target_rt_dev_ptr
));
5155 if (!*host_rt_dev_ptr
) {
5156 unlock_user(argptr
, arg
, 0);
5157 return -TARGET_EFAULT
;
5160 *host_rt_dev_ptr
= 0;
5165 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
5166 argptr
+ src_offsets
[i
],
5167 field_types
, THUNK_HOST
);
5169 unlock_user(argptr
, arg
, 0);
5171 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5173 assert(host_rt_dev_ptr
!= NULL
);
5174 assert(target_rt_dev_ptr
!= NULL
);
5175 if (*host_rt_dev_ptr
!= 0) {
5176 unlock_user((void *)*host_rt_dev_ptr
,
5177 *target_rt_dev_ptr
, 0);
5182 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5183 int fd
, int cmd
, abi_long arg
)
5185 int sig
= target_to_host_signal(arg
);
5186 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, sig
));
5189 static abi_long
do_ioctl_SIOCGSTAMP(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5190 int fd
, int cmd
, abi_long arg
)
5195 ret
= get_errno(safe_ioctl(fd
, SIOCGSTAMP
, &tv
));
5196 if (is_error(ret
)) {
5200 if (cmd
== (int)TARGET_SIOCGSTAMP_OLD
) {
5201 if (copy_to_user_timeval(arg
, &tv
)) {
5202 return -TARGET_EFAULT
;
5205 if (copy_to_user_timeval64(arg
, &tv
)) {
5206 return -TARGET_EFAULT
;
5213 static abi_long
do_ioctl_SIOCGSTAMPNS(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5214 int fd
, int cmd
, abi_long arg
)
5219 ret
= get_errno(safe_ioctl(fd
, SIOCGSTAMPNS
, &ts
));
5220 if (is_error(ret
)) {
5224 if (cmd
== (int)TARGET_SIOCGSTAMPNS_OLD
) {
5225 if (host_to_target_timespec(arg
, &ts
)) {
5226 return -TARGET_EFAULT
;
5229 if (host_to_target_timespec64(arg
, &ts
)) {
5230 return -TARGET_EFAULT
;
5238 static abi_long
do_ioctl_tiocgptpeer(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5239 int fd
, int cmd
, abi_long arg
)
5241 int flags
= target_to_host_bitmask(arg
, fcntl_flags_tbl
);
5242 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, flags
));
5246 static IOCTLEntry ioctl_entries
[] = {
5247 #define IOCTL(cmd, access, ...) \
5248 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5249 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5250 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
5251 #define IOCTL_IGNORE(cmd) \
5252 { TARGET_ ## cmd, 0, #cmd },
5257 /* ??? Implement proper locking for ioctls. */
5258 /* do_ioctl() Must return target values and target errnos. */
5259 static abi_long
do_ioctl(int fd
, int cmd
, abi_long arg
)
5261 const IOCTLEntry
*ie
;
5262 const argtype
*arg_type
;
5264 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
5270 if (ie
->target_cmd
== 0) {
5272 LOG_UNIMP
, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
5273 return -TARGET_ENOSYS
;
5275 if (ie
->target_cmd
== cmd
)
5279 arg_type
= ie
->arg_type
;
5281 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
5282 } else if (!ie
->host_cmd
) {
5283 /* Some architectures define BSD ioctls in their headers
5284 that are not implemented in Linux. */
5285 return -TARGET_ENOSYS
;
5288 switch(arg_type
[0]) {
5291 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
));
5297 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, arg
));
5301 target_size
= thunk_type_size(arg_type
, 0);
5302 switch(ie
->access
) {
5304 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5305 if (!is_error(ret
)) {
5306 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5308 return -TARGET_EFAULT
;
5309 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5310 unlock_user(argptr
, arg
, target_size
);
5314 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5316 return -TARGET_EFAULT
;
5317 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5318 unlock_user(argptr
, arg
, 0);
5319 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5323 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5325 return -TARGET_EFAULT
;
5326 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5327 unlock_user(argptr
, arg
, 0);
5328 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5329 if (!is_error(ret
)) {
5330 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5332 return -TARGET_EFAULT
;
5333 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5334 unlock_user(argptr
, arg
, target_size
);
5340 qemu_log_mask(LOG_UNIMP
,
5341 "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5342 (long)cmd
, arg_type
[0]);
5343 ret
= -TARGET_ENOSYS
;
5349 static const bitmask_transtbl iflag_tbl
[] = {
5350 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
5351 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
5352 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
5353 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
5354 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
5355 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
5356 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
5357 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
5358 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
5359 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
5360 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
5361 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
5362 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
5363 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
5367 static const bitmask_transtbl oflag_tbl
[] = {
5368 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
5369 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
5370 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
5371 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
5372 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
5373 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
5374 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
5375 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
5376 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
5377 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
5378 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
5379 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
5380 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
5381 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
5382 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
5383 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
5384 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
5385 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
5386 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
5387 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
5388 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
5389 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
5390 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
5391 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
5395 static const bitmask_transtbl cflag_tbl
[] = {
5396 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
5397 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
5398 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
5399 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
5400 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
5401 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
5402 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
5403 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
5404 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
5405 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
5406 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
5407 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
5408 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
5409 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
5410 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
5411 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
5412 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
5413 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
5414 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
5415 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
5416 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
5417 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
5418 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
5419 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
5420 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
5421 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
5422 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
5423 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
5424 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
5425 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
5426 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
5430 static const bitmask_transtbl lflag_tbl
[] = {
5431 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
5432 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
5433 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
5434 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
5435 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
5436 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
5437 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
5438 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
5439 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
5440 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
5441 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
5442 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
5443 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
5444 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
5445 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
5449 static void target_to_host_termios (void *dst
, const void *src
)
5451 struct host_termios
*host
= dst
;
5452 const struct target_termios
*target
= src
;
5455 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
5457 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
5459 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
5461 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
5462 host
->c_line
= target
->c_line
;
5464 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
5465 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
5466 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
5467 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
5468 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
5469 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
5470 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
5471 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
5472 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
5473 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
5474 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
5475 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
5476 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
5477 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
5478 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
5479 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
5480 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
5481 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
5484 static void host_to_target_termios (void *dst
, const void *src
)
5486 struct target_termios
*target
= dst
;
5487 const struct host_termios
*host
= src
;
5490 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
5492 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
5494 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
5496 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
5497 target
->c_line
= host
->c_line
;
5499 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
5500 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
5501 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
5502 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
5503 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
5504 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
5505 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
5506 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
5507 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
5508 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
5509 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
5510 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
5511 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
5512 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
5513 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
5514 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
5515 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
5516 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
5519 static const StructEntry struct_termios_def
= {
5520 .convert
= { host_to_target_termios
, target_to_host_termios
},
5521 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
5522 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
5525 static bitmask_transtbl mmap_flags_tbl
[] = {
5526 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
5527 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
5528 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
5529 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
,
5530 MAP_ANONYMOUS
, MAP_ANONYMOUS
},
5531 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
,
5532 MAP_GROWSDOWN
, MAP_GROWSDOWN
},
5533 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
,
5534 MAP_DENYWRITE
, MAP_DENYWRITE
},
5535 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
,
5536 MAP_EXECUTABLE
, MAP_EXECUTABLE
},
5537 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
5538 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
,
5539 MAP_NORESERVE
, MAP_NORESERVE
},
5540 { TARGET_MAP_HUGETLB
, TARGET_MAP_HUGETLB
, MAP_HUGETLB
, MAP_HUGETLB
},
5541 /* MAP_STACK had been ignored by the kernel for quite some time.
5542 Recognize it for the target insofar as we do not want to pass
5543 it through to the host. */
5544 { TARGET_MAP_STACK
, TARGET_MAP_STACK
, 0, 0 },
5548 #if defined(TARGET_I386)
5550 /* NOTE: there is really one LDT for all the threads */
5551 static uint8_t *ldt_table
;
5553 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
5560 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
5561 if (size
> bytecount
)
5563 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
5565 return -TARGET_EFAULT
;
5566 /* ??? Should this by byteswapped? */
5567 memcpy(p
, ldt_table
, size
);
5568 unlock_user(p
, ptr
, size
);
5572 /* XXX: add locking support */
5573 static abi_long
write_ldt(CPUX86State
*env
,
5574 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
5576 struct target_modify_ldt_ldt_s ldt_info
;
5577 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5578 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
5579 int seg_not_present
, useable
, lm
;
5580 uint32_t *lp
, entry_1
, entry_2
;
5582 if (bytecount
!= sizeof(ldt_info
))
5583 return -TARGET_EINVAL
;
5584 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
5585 return -TARGET_EFAULT
;
5586 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
5587 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
5588 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
5589 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
5590 unlock_user_struct(target_ldt_info
, ptr
, 0);
5592 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
5593 return -TARGET_EINVAL
;
5594 seg_32bit
= ldt_info
.flags
& 1;
5595 contents
= (ldt_info
.flags
>> 1) & 3;
5596 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
5597 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
5598 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
5599 useable
= (ldt_info
.flags
>> 6) & 1;
5603 lm
= (ldt_info
.flags
>> 7) & 1;
5605 if (contents
== 3) {
5607 return -TARGET_EINVAL
;
5608 if (seg_not_present
== 0)
5609 return -TARGET_EINVAL
;
5611 /* allocate the LDT */
5613 env
->ldt
.base
= target_mmap(0,
5614 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
5615 PROT_READ
|PROT_WRITE
,
5616 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
5617 if (env
->ldt
.base
== -1)
5618 return -TARGET_ENOMEM
;
5619 memset(g2h(env
->ldt
.base
), 0,
5620 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
5621 env
->ldt
.limit
= 0xffff;
5622 ldt_table
= g2h(env
->ldt
.base
);
5625 /* NOTE: same code as Linux kernel */
5626 /* Allow LDTs to be cleared by the user. */
5627 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
5630 read_exec_only
== 1 &&
5632 limit_in_pages
== 0 &&
5633 seg_not_present
== 1 &&
5641 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
5642 (ldt_info
.limit
& 0x0ffff);
5643 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
5644 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
5645 (ldt_info
.limit
& 0xf0000) |
5646 ((read_exec_only
^ 1) << 9) |
5648 ((seg_not_present
^ 1) << 15) |
5650 (limit_in_pages
<< 23) |
5654 entry_2
|= (useable
<< 20);
5656 /* Install the new entry ... */
5658 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
5659 lp
[0] = tswap32(entry_1
);
5660 lp
[1] = tswap32(entry_2
);
5664 /* specific and weird i386 syscalls */
5665 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
5666 unsigned long bytecount
)
5672 ret
= read_ldt(ptr
, bytecount
);
5675 ret
= write_ldt(env
, ptr
, bytecount
, 1);
5678 ret
= write_ldt(env
, ptr
, bytecount
, 0);
5681 ret
= -TARGET_ENOSYS
;
5687 #if defined(TARGET_I386) && defined(TARGET_ABI32)
5688 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
5690 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
5691 struct target_modify_ldt_ldt_s ldt_info
;
5692 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5693 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
5694 int seg_not_present
, useable
, lm
;
5695 uint32_t *lp
, entry_1
, entry_2
;
5698 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
5699 if (!target_ldt_info
)
5700 return -TARGET_EFAULT
;
5701 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
5702 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
5703 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
5704 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
5705 if (ldt_info
.entry_number
== -1) {
5706 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
5707 if (gdt_table
[i
] == 0) {
5708 ldt_info
.entry_number
= i
;
5709 target_ldt_info
->entry_number
= tswap32(i
);
5714 unlock_user_struct(target_ldt_info
, ptr
, 1);
5716 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
5717 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
5718 return -TARGET_EINVAL
;
5719 seg_32bit
= ldt_info
.flags
& 1;
5720 contents
= (ldt_info
.flags
>> 1) & 3;
5721 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
5722 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
5723 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
5724 useable
= (ldt_info
.flags
>> 6) & 1;
5728 lm
= (ldt_info
.flags
>> 7) & 1;
5731 if (contents
== 3) {
5732 if (seg_not_present
== 0)
5733 return -TARGET_EINVAL
;
5736 /* NOTE: same code as Linux kernel */
5737 /* Allow LDTs to be cleared by the user. */
5738 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
5739 if ((contents
== 0 &&
5740 read_exec_only
== 1 &&
5742 limit_in_pages
== 0 &&
5743 seg_not_present
== 1 &&
5751 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
5752 (ldt_info
.limit
& 0x0ffff);
5753 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
5754 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
5755 (ldt_info
.limit
& 0xf0000) |
5756 ((read_exec_only
^ 1) << 9) |
5758 ((seg_not_present
^ 1) << 15) |
5760 (limit_in_pages
<< 23) |
5765 /* Install the new entry ... */
5767 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
5768 lp
[0] = tswap32(entry_1
);
5769 lp
[1] = tswap32(entry_2
);
5773 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
5775 struct target_modify_ldt_ldt_s
*target_ldt_info
;
5776 uint64_t *gdt_table
= g2h(env
->gdt
.base
);
5777 uint32_t base_addr
, limit
, flags
;
5778 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
5779 int seg_not_present
, useable
, lm
;
5780 uint32_t *lp
, entry_1
, entry_2
;
5782 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
5783 if (!target_ldt_info
)
5784 return -TARGET_EFAULT
;
5785 idx
= tswap32(target_ldt_info
->entry_number
);
5786 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
5787 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
5788 unlock_user_struct(target_ldt_info
, ptr
, 1);
5789 return -TARGET_EINVAL
;
5791 lp
= (uint32_t *)(gdt_table
+ idx
);
5792 entry_1
= tswap32(lp
[0]);
5793 entry_2
= tswap32(lp
[1]);
5795 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
5796 contents
= (entry_2
>> 10) & 3;
5797 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
5798 seg_32bit
= (entry_2
>> 22) & 1;
5799 limit_in_pages
= (entry_2
>> 23) & 1;
5800 useable
= (entry_2
>> 20) & 1;
5804 lm
= (entry_2
>> 21) & 1;
5806 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
5807 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
5808 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
5809 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
5810 base_addr
= (entry_1
>> 16) |
5811 (entry_2
& 0xff000000) |
5812 ((entry_2
& 0xff) << 16);
5813 target_ldt_info
->base_addr
= tswapal(base_addr
);
5814 target_ldt_info
->limit
= tswap32(limit
);
5815 target_ldt_info
->flags
= tswap32(flags
);
5816 unlock_user_struct(target_ldt_info
, ptr
, 1);
5819 #endif /* TARGET_I386 && TARGET_ABI32 */
5821 #ifndef TARGET_ABI32
5822 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
5829 case TARGET_ARCH_SET_GS
:
5830 case TARGET_ARCH_SET_FS
:
5831 if (code
== TARGET_ARCH_SET_GS
)
5835 cpu_x86_load_seg(env
, idx
, 0);
5836 env
->segs
[idx
].base
= addr
;
5838 case TARGET_ARCH_GET_GS
:
5839 case TARGET_ARCH_GET_FS
:
5840 if (code
== TARGET_ARCH_GET_GS
)
5844 val
= env
->segs
[idx
].base
;
5845 if (put_user(val
, addr
, abi_ulong
))
5846 ret
= -TARGET_EFAULT
;
5849 ret
= -TARGET_EINVAL
;
5856 #endif /* defined(TARGET_I386) */
5858 #define NEW_STACK_SIZE 0x40000
5861 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
5864 pthread_mutex_t mutex
;
5865 pthread_cond_t cond
;
5868 abi_ulong child_tidptr
;
5869 abi_ulong parent_tidptr
;
5873 static void *clone_func(void *arg
)
5875 new_thread_info
*info
= arg
;
5880 rcu_register_thread();
5881 tcg_register_thread();
5885 ts
= (TaskState
*)cpu
->opaque
;
5886 info
->tid
= sys_gettid();
5888 if (info
->child_tidptr
)
5889 put_user_u32(info
->tid
, info
->child_tidptr
);
5890 if (info
->parent_tidptr
)
5891 put_user_u32(info
->tid
, info
->parent_tidptr
);
5892 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
5893 /* Enable signals. */
5894 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
5895 /* Signal to the parent that we're ready. */
5896 pthread_mutex_lock(&info
->mutex
);
5897 pthread_cond_broadcast(&info
->cond
);
5898 pthread_mutex_unlock(&info
->mutex
);
5899 /* Wait until the parent has finished initializing the tls state. */
5900 pthread_mutex_lock(&clone_lock
);
5901 pthread_mutex_unlock(&clone_lock
);
5907 /* do_fork() Must return host values and target errnos (unlike most
5908 do_*() functions). */
5909 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
5910 abi_ulong parent_tidptr
, target_ulong newtls
,
5911 abi_ulong child_tidptr
)
5913 CPUState
*cpu
= env_cpu(env
);
5917 CPUArchState
*new_env
;
5920 flags
&= ~CLONE_IGNORED_FLAGS
;
5922 /* Emulate vfork() with fork() */
5923 if (flags
& CLONE_VFORK
)
5924 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
5926 if (flags
& CLONE_VM
) {
5927 TaskState
*parent_ts
= (TaskState
*)cpu
->opaque
;
5928 new_thread_info info
;
5929 pthread_attr_t attr
;
5931 if (((flags
& CLONE_THREAD_FLAGS
) != CLONE_THREAD_FLAGS
) ||
5932 (flags
& CLONE_INVALID_THREAD_FLAGS
)) {
5933 return -TARGET_EINVAL
;
5936 ts
= g_new0(TaskState
, 1);
5937 init_task_state(ts
);
5939 /* Grab a mutex so that thread setup appears atomic. */
5940 pthread_mutex_lock(&clone_lock
);
5942 /* we create a new CPU instance. */
5943 new_env
= cpu_copy(env
);
5944 /* Init regs that differ from the parent. */
5945 cpu_clone_regs_child(new_env
, newsp
, flags
);
5946 cpu_clone_regs_parent(env
, flags
);
5947 new_cpu
= env_cpu(new_env
);
5948 new_cpu
->opaque
= ts
;
5949 ts
->bprm
= parent_ts
->bprm
;
5950 ts
->info
= parent_ts
->info
;
5951 ts
->signal_mask
= parent_ts
->signal_mask
;
5953 if (flags
& CLONE_CHILD_CLEARTID
) {
5954 ts
->child_tidptr
= child_tidptr
;
5957 if (flags
& CLONE_SETTLS
) {
5958 cpu_set_tls (new_env
, newtls
);
5961 memset(&info
, 0, sizeof(info
));
5962 pthread_mutex_init(&info
.mutex
, NULL
);
5963 pthread_mutex_lock(&info
.mutex
);
5964 pthread_cond_init(&info
.cond
, NULL
);
5966 if (flags
& CLONE_CHILD_SETTID
) {
5967 info
.child_tidptr
= child_tidptr
;
5969 if (flags
& CLONE_PARENT_SETTID
) {
5970 info
.parent_tidptr
= parent_tidptr
;
5973 ret
= pthread_attr_init(&attr
);
5974 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
5975 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
5976 /* It is not safe to deliver signals until the child has finished
5977 initializing, so temporarily block all signals. */
5978 sigfillset(&sigmask
);
5979 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
5980 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
5982 /* If this is our first additional thread, we need to ensure we
5983 * generate code for parallel execution and flush old translations.
5985 if (!parallel_cpus
) {
5986 parallel_cpus
= true;
5990 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
5991 /* TODO: Free new CPU state if thread creation failed. */
5993 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
5994 pthread_attr_destroy(&attr
);
5996 /* Wait for the child to initialize. */
5997 pthread_cond_wait(&info
.cond
, &info
.mutex
);
6002 pthread_mutex_unlock(&info
.mutex
);
6003 pthread_cond_destroy(&info
.cond
);
6004 pthread_mutex_destroy(&info
.mutex
);
6005 pthread_mutex_unlock(&clone_lock
);
6007 /* if no CLONE_VM, we consider it is a fork */
6008 if (flags
& CLONE_INVALID_FORK_FLAGS
) {
6009 return -TARGET_EINVAL
;
6012 /* We can't support custom termination signals */
6013 if ((flags
& CSIGNAL
) != TARGET_SIGCHLD
) {
6014 return -TARGET_EINVAL
;
6017 if (block_signals()) {
6018 return -TARGET_ERESTARTSYS
;
6024 /* Child Process. */
6025 cpu_clone_regs_child(env
, newsp
, flags
);
6027 /* There is a race condition here. The parent process could
6028 theoretically read the TID in the child process before the child
6029 tid is set. This would require using either ptrace
6030 (not implemented) or having *_tidptr to point at a shared memory
6031 mapping. We can't repeat the spinlock hack used above because
6032 the child process gets its own copy of the lock. */
6033 if (flags
& CLONE_CHILD_SETTID
)
6034 put_user_u32(sys_gettid(), child_tidptr
);
6035 if (flags
& CLONE_PARENT_SETTID
)
6036 put_user_u32(sys_gettid(), parent_tidptr
);
6037 ts
= (TaskState
*)cpu
->opaque
;
6038 if (flags
& CLONE_SETTLS
)
6039 cpu_set_tls (env
, newtls
);
6040 if (flags
& CLONE_CHILD_CLEARTID
)
6041 ts
->child_tidptr
= child_tidptr
;
6043 cpu_clone_regs_parent(env
, flags
);
6050 /* warning : doesn't handle linux specific flags... */
6051 static int target_to_host_fcntl_cmd(int cmd
)
6056 case TARGET_F_DUPFD
:
6057 case TARGET_F_GETFD
:
6058 case TARGET_F_SETFD
:
6059 case TARGET_F_GETFL
:
6060 case TARGET_F_SETFL
:
6063 case TARGET_F_GETLK
:
6066 case TARGET_F_SETLK
:
6069 case TARGET_F_SETLKW
:
6072 case TARGET_F_GETOWN
:
6075 case TARGET_F_SETOWN
:
6078 case TARGET_F_GETSIG
:
6081 case TARGET_F_SETSIG
:
6084 #if TARGET_ABI_BITS == 32
6085 case TARGET_F_GETLK64
:
6088 case TARGET_F_SETLK64
:
6091 case TARGET_F_SETLKW64
:
6095 case TARGET_F_SETLEASE
:
6098 case TARGET_F_GETLEASE
:
6101 #ifdef F_DUPFD_CLOEXEC
6102 case TARGET_F_DUPFD_CLOEXEC
:
6103 ret
= F_DUPFD_CLOEXEC
;
6106 case TARGET_F_NOTIFY
:
6110 case TARGET_F_GETOWN_EX
:
6115 case TARGET_F_SETOWN_EX
:
6120 case TARGET_F_SETPIPE_SZ
:
6123 case TARGET_F_GETPIPE_SZ
:
6128 ret
= -TARGET_EINVAL
;
6132 #if defined(__powerpc64__)
6133 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6134 * is not supported by kernel. The glibc fcntl call actually adjusts
6135 * them to 5, 6 and 7 before making the syscall(). Since we make the
6136 * syscall directly, adjust to what is supported by the kernel.
6138 if (ret
>= F_GETLK64
&& ret
<= F_SETLKW64
) {
6139 ret
-= F_GETLK64
- 5;
6146 #define FLOCK_TRANSTBL \
6148 TRANSTBL_CONVERT(F_RDLCK); \
6149 TRANSTBL_CONVERT(F_WRLCK); \
6150 TRANSTBL_CONVERT(F_UNLCK); \
6151 TRANSTBL_CONVERT(F_EXLCK); \
6152 TRANSTBL_CONVERT(F_SHLCK); \
6155 static int target_to_host_flock(int type
)
6157 #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6159 #undef TRANSTBL_CONVERT
6160 return -TARGET_EINVAL
;
6163 static int host_to_target_flock(int type
)
6165 #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6167 #undef TRANSTBL_CONVERT
6168 /* if we don't know how to convert the value coming
6169 * from the host we copy to the target field as-is
6174 static inline abi_long
copy_from_user_flock(struct flock64
*fl
,
6175 abi_ulong target_flock_addr
)
6177 struct target_flock
*target_fl
;
6180 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6181 return -TARGET_EFAULT
;
6184 __get_user(l_type
, &target_fl
->l_type
);
6185 l_type
= target_to_host_flock(l_type
);
6189 fl
->l_type
= l_type
;
6190 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6191 __get_user(fl
->l_start
, &target_fl
->l_start
);
6192 __get_user(fl
->l_len
, &target_fl
->l_len
);
6193 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6194 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6198 static inline abi_long
copy_to_user_flock(abi_ulong target_flock_addr
,
6199 const struct flock64
*fl
)
6201 struct target_flock
*target_fl
;
6204 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6205 return -TARGET_EFAULT
;
6208 l_type
= host_to_target_flock(fl
->l_type
);
6209 __put_user(l_type
, &target_fl
->l_type
);
6210 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6211 __put_user(fl
->l_start
, &target_fl
->l_start
);
6212 __put_user(fl
->l_len
, &target_fl
->l_len
);
6213 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6214 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6218 typedef abi_long
from_flock64_fn(struct flock64
*fl
, abi_ulong target_addr
);
6219 typedef abi_long
to_flock64_fn(abi_ulong target_addr
, const struct flock64
*fl
);
6221 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6222 static inline abi_long
copy_from_user_oabi_flock64(struct flock64
*fl
,
6223 abi_ulong target_flock_addr
)
6225 struct target_oabi_flock64
*target_fl
;
6228 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6229 return -TARGET_EFAULT
;
6232 __get_user(l_type
, &target_fl
->l_type
);
6233 l_type
= target_to_host_flock(l_type
);
6237 fl
->l_type
= l_type
;
6238 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6239 __get_user(fl
->l_start
, &target_fl
->l_start
);
6240 __get_user(fl
->l_len
, &target_fl
->l_len
);
6241 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6242 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6246 static inline abi_long
copy_to_user_oabi_flock64(abi_ulong target_flock_addr
,
6247 const struct flock64
*fl
)
6249 struct target_oabi_flock64
*target_fl
;
6252 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6253 return -TARGET_EFAULT
;
6256 l_type
= host_to_target_flock(fl
->l_type
);
6257 __put_user(l_type
, &target_fl
->l_type
);
6258 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6259 __put_user(fl
->l_start
, &target_fl
->l_start
);
6260 __put_user(fl
->l_len
, &target_fl
->l_len
);
6261 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6262 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6267 static inline abi_long
copy_from_user_flock64(struct flock64
*fl
,
6268 abi_ulong target_flock_addr
)
6270 struct target_flock64
*target_fl
;
6273 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6274 return -TARGET_EFAULT
;
6277 __get_user(l_type
, &target_fl
->l_type
);
6278 l_type
= target_to_host_flock(l_type
);
6282 fl
->l_type
= l_type
;
6283 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6284 __get_user(fl
->l_start
, &target_fl
->l_start
);
6285 __get_user(fl
->l_len
, &target_fl
->l_len
);
6286 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6287 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6291 static inline abi_long
copy_to_user_flock64(abi_ulong target_flock_addr
,
6292 const struct flock64
*fl
)
6294 struct target_flock64
*target_fl
;
6297 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6298 return -TARGET_EFAULT
;
6301 l_type
= host_to_target_flock(fl
->l_type
);
6302 __put_user(l_type
, &target_fl
->l_type
);
6303 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6304 __put_user(fl
->l_start
, &target_fl
->l_start
);
6305 __put_user(fl
->l_len
, &target_fl
->l_len
);
6306 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6307 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6311 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
6313 struct flock64 fl64
;
6315 struct f_owner_ex fox
;
6316 struct target_f_owner_ex
*target_fox
;
6319 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
6321 if (host_cmd
== -TARGET_EINVAL
)
6325 case TARGET_F_GETLK
:
6326 ret
= copy_from_user_flock(&fl64
, arg
);
6330 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6332 ret
= copy_to_user_flock(arg
, &fl64
);
6336 case TARGET_F_SETLK
:
6337 case TARGET_F_SETLKW
:
6338 ret
= copy_from_user_flock(&fl64
, arg
);
6342 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6345 case TARGET_F_GETLK64
:
6346 ret
= copy_from_user_flock64(&fl64
, arg
);
6350 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6352 ret
= copy_to_user_flock64(arg
, &fl64
);
6355 case TARGET_F_SETLK64
:
6356 case TARGET_F_SETLKW64
:
6357 ret
= copy_from_user_flock64(&fl64
, arg
);
6361 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
6364 case TARGET_F_GETFL
:
6365 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
6367 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
6371 case TARGET_F_SETFL
:
6372 ret
= get_errno(safe_fcntl(fd
, host_cmd
,
6373 target_to_host_bitmask(arg
,
6378 case TARGET_F_GETOWN_EX
:
6379 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
6381 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
6382 return -TARGET_EFAULT
;
6383 target_fox
->type
= tswap32(fox
.type
);
6384 target_fox
->pid
= tswap32(fox
.pid
);
6385 unlock_user_struct(target_fox
, arg
, 1);
6391 case TARGET_F_SETOWN_EX
:
6392 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
6393 return -TARGET_EFAULT
;
6394 fox
.type
= tswap32(target_fox
->type
);
6395 fox
.pid
= tswap32(target_fox
->pid
);
6396 unlock_user_struct(target_fox
, arg
, 0);
6397 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
6401 case TARGET_F_SETOWN
:
6402 case TARGET_F_GETOWN
:
6403 case TARGET_F_SETSIG
:
6404 case TARGET_F_GETSIG
:
6405 case TARGET_F_SETLEASE
:
6406 case TARGET_F_GETLEASE
:
6407 case TARGET_F_SETPIPE_SZ
:
6408 case TARGET_F_GETPIPE_SZ
:
6409 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
6413 ret
= get_errno(safe_fcntl(fd
, cmd
, arg
));
6421 static inline int high2lowuid(int uid
)
6429 static inline int high2lowgid(int gid
)
6437 static inline int low2highuid(int uid
)
6439 if ((int16_t)uid
== -1)
6445 static inline int low2highgid(int gid
)
6447 if ((int16_t)gid
== -1)
6452 static inline int tswapid(int id
)
6457 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
6459 #else /* !USE_UID16 */
6460 static inline int high2lowuid(int uid
)
6464 static inline int high2lowgid(int gid
)
6468 static inline int low2highuid(int uid
)
6472 static inline int low2highgid(int gid
)
6476 static inline int tswapid(int id
)
6481 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
6483 #endif /* USE_UID16 */
6485 /* We must do direct syscalls for setting UID/GID, because we want to
6486 * implement the Linux system call semantics of "change only for this thread",
6487 * not the libc/POSIX semantics of "change for all threads in process".
6488 * (See http://ewontfix.com/17/ for more details.)
6489 * We use the 32-bit version of the syscalls if present; if it is not
6490 * then either the host architecture supports 32-bit UIDs natively with
6491 * the standard syscall, or the 16-bit UID is the best we can do.
6493 #ifdef __NR_setuid32
6494 #define __NR_sys_setuid __NR_setuid32
6496 #define __NR_sys_setuid __NR_setuid
6498 #ifdef __NR_setgid32
6499 #define __NR_sys_setgid __NR_setgid32
6501 #define __NR_sys_setgid __NR_setgid
6503 #ifdef __NR_setresuid32
6504 #define __NR_sys_setresuid __NR_setresuid32
6506 #define __NR_sys_setresuid __NR_setresuid
6508 #ifdef __NR_setresgid32
6509 #define __NR_sys_setresgid __NR_setresgid32
6511 #define __NR_sys_setresgid __NR_setresgid
6514 _syscall1(int, sys_setuid
, uid_t
, uid
)
6515 _syscall1(int, sys_setgid
, gid_t
, gid
)
6516 _syscall3(int, sys_setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
6517 _syscall3(int, sys_setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
6519 void syscall_init(void)
6522 const argtype
*arg_type
;
6526 thunk_init(STRUCT_MAX
);
6528 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
6529 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
6530 #include "syscall_types.h"
6532 #undef STRUCT_SPECIAL
6534 /* Build target_to_host_errno_table[] table from
6535 * host_to_target_errno_table[]. */
6536 for (i
= 0; i
< ERRNO_TABLE_SIZE
; i
++) {
6537 target_to_host_errno_table
[host_to_target_errno_table
[i
]] = i
;
6540 /* we patch the ioctl size if necessary. We rely on the fact that
6541 no ioctl has all the bits at '1' in the size field */
6543 while (ie
->target_cmd
!= 0) {
6544 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
6545 TARGET_IOC_SIZEMASK
) {
6546 arg_type
= ie
->arg_type
;
6547 if (arg_type
[0] != TYPE_PTR
) {
6548 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
6553 size
= thunk_type_size(arg_type
, 0);
6554 ie
->target_cmd
= (ie
->target_cmd
&
6555 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
6556 (size
<< TARGET_IOC_SIZESHIFT
);
6559 /* automatic consistency check if same arch */
6560 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6561 (defined(__x86_64__) && defined(TARGET_X86_64))
6562 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
6563 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
6564 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
6571 #if TARGET_ABI_BITS == 32
6572 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
6574 #ifdef TARGET_WORDS_BIGENDIAN
6575 return ((uint64_t)word0
<< 32) | word1
;
6577 return ((uint64_t)word1
<< 32) | word0
;
6580 #else /* TARGET_ABI_BITS == 32 */
6581 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
6585 #endif /* TARGET_ABI_BITS != 32 */
6587 #ifdef TARGET_NR_truncate64
6588 static inline abi_long
target_truncate64(void *cpu_env
, const char *arg1
,
6593 if (regpairs_aligned(cpu_env
, TARGET_NR_truncate64
)) {
6597 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
6601 #ifdef TARGET_NR_ftruncate64
6602 static inline abi_long
target_ftruncate64(void *cpu_env
, abi_long arg1
,
6607 if (regpairs_aligned(cpu_env
, TARGET_NR_ftruncate64
)) {
6611 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
6615 #if defined(TARGET_NR_timer_settime) || \
6616 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
6617 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_itspec
,
6618 abi_ulong target_addr
)
6620 struct target_itimerspec
*target_itspec
;
6622 if (!lock_user_struct(VERIFY_READ
, target_itspec
, target_addr
, 1)) {
6623 return -TARGET_EFAULT
;
6626 host_itspec
->it_interval
.tv_sec
=
6627 tswapal(target_itspec
->it_interval
.tv_sec
);
6628 host_itspec
->it_interval
.tv_nsec
=
6629 tswapal(target_itspec
->it_interval
.tv_nsec
);
6630 host_itspec
->it_value
.tv_sec
= tswapal(target_itspec
->it_value
.tv_sec
);
6631 host_itspec
->it_value
.tv_nsec
= tswapal(target_itspec
->it_value
.tv_nsec
);
6633 unlock_user_struct(target_itspec
, target_addr
, 1);
6638 #if ((defined(TARGET_NR_timerfd_gettime) || \
6639 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
6640 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
6641 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
6642 struct itimerspec
*host_its
)
6644 struct target_itimerspec
*target_itspec
;
6646 if (!lock_user_struct(VERIFY_WRITE
, target_itspec
, target_addr
, 0)) {
6647 return -TARGET_EFAULT
;
6650 target_itspec
->it_interval
.tv_sec
= tswapal(host_its
->it_interval
.tv_sec
);
6651 target_itspec
->it_interval
.tv_nsec
= tswapal(host_its
->it_interval
.tv_nsec
);
6653 target_itspec
->it_value
.tv_sec
= tswapal(host_its
->it_value
.tv_sec
);
6654 target_itspec
->it_value
.tv_nsec
= tswapal(host_its
->it_value
.tv_nsec
);
6656 unlock_user_struct(target_itspec
, target_addr
, 0);
6661 #if defined(TARGET_NR_adjtimex) || \
6662 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
6663 static inline abi_long
target_to_host_timex(struct timex
*host_tx
,
6664 abi_long target_addr
)
6666 struct target_timex
*target_tx
;
6668 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
6669 return -TARGET_EFAULT
;
6672 __get_user(host_tx
->modes
, &target_tx
->modes
);
6673 __get_user(host_tx
->offset
, &target_tx
->offset
);
6674 __get_user(host_tx
->freq
, &target_tx
->freq
);
6675 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
6676 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
6677 __get_user(host_tx
->status
, &target_tx
->status
);
6678 __get_user(host_tx
->constant
, &target_tx
->constant
);
6679 __get_user(host_tx
->precision
, &target_tx
->precision
);
6680 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
6681 __get_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
6682 __get_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
6683 __get_user(host_tx
->tick
, &target_tx
->tick
);
6684 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
6685 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
6686 __get_user(host_tx
->shift
, &target_tx
->shift
);
6687 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
6688 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
6689 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
6690 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
6691 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
6692 __get_user(host_tx
->tai
, &target_tx
->tai
);
6694 unlock_user_struct(target_tx
, target_addr
, 0);
6698 static inline abi_long
host_to_target_timex(abi_long target_addr
,
6699 struct timex
*host_tx
)
6701 struct target_timex
*target_tx
;
6703 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
6704 return -TARGET_EFAULT
;
6707 __put_user(host_tx
->modes
, &target_tx
->modes
);
6708 __put_user(host_tx
->offset
, &target_tx
->offset
);
6709 __put_user(host_tx
->freq
, &target_tx
->freq
);
6710 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
6711 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
6712 __put_user(host_tx
->status
, &target_tx
->status
);
6713 __put_user(host_tx
->constant
, &target_tx
->constant
);
6714 __put_user(host_tx
->precision
, &target_tx
->precision
);
6715 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
6716 __put_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
6717 __put_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
6718 __put_user(host_tx
->tick
, &target_tx
->tick
);
6719 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
6720 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
6721 __put_user(host_tx
->shift
, &target_tx
->shift
);
6722 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
6723 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
6724 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
6725 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
6726 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
6727 __put_user(host_tx
->tai
, &target_tx
->tai
);
6729 unlock_user_struct(target_tx
, target_addr
, 1);
6734 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
6735 abi_ulong target_addr
)
6737 struct target_sigevent
*target_sevp
;
6739 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
6740 return -TARGET_EFAULT
;
6743 /* This union is awkward on 64 bit systems because it has a 32 bit
6744 * integer and a pointer in it; we follow the conversion approach
6745 * used for handling sigval types in signal.c so the guest should get
6746 * the correct value back even if we did a 64 bit byteswap and it's
6747 * using the 32 bit integer.
6749 host_sevp
->sigev_value
.sival_ptr
=
6750 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
6751 host_sevp
->sigev_signo
=
6752 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
6753 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
6754 host_sevp
->_sigev_un
._tid
= tswap32(target_sevp
->_sigev_un
._tid
);
6756 unlock_user_struct(target_sevp
, target_addr
, 1);
6760 #if defined(TARGET_NR_mlockall)
6761 static inline int target_to_host_mlockall_arg(int arg
)
6765 if (arg
& TARGET_MLOCKALL_MCL_CURRENT
) {
6766 result
|= MCL_CURRENT
;
6768 if (arg
& TARGET_MLOCKALL_MCL_FUTURE
) {
6769 result
|= MCL_FUTURE
;
6775 #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
6776 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
6777 defined(TARGET_NR_newfstatat))
6778 static inline abi_long
host_to_target_stat64(void *cpu_env
,
6779 abi_ulong target_addr
,
6780 struct stat
*host_st
)
6782 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
6783 if (((CPUARMState
*)cpu_env
)->eabi
) {
6784 struct target_eabi_stat64
*target_st
;
6786 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
6787 return -TARGET_EFAULT
;
6788 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
6789 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
6790 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
6791 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6792 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
6794 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
6795 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
6796 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
6797 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
6798 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
6799 __put_user(host_st
->st_size
, &target_st
->st_size
);
6800 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
6801 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
6802 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
6803 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
6804 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
6805 #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
6806 __put_user(host_st
->st_atim
.tv_nsec
, &target_st
->target_st_atime_nsec
);
6807 __put_user(host_st
->st_mtim
.tv_nsec
, &target_st
->target_st_mtime_nsec
);
6808 __put_user(host_st
->st_ctim
.tv_nsec
, &target_st
->target_st_ctime_nsec
);
6810 unlock_user_struct(target_st
, target_addr
, 1);
6814 #if defined(TARGET_HAS_STRUCT_STAT64)
6815 struct target_stat64
*target_st
;
6817 struct target_stat
*target_st
;
6820 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
6821 return -TARGET_EFAULT
;
6822 memset(target_st
, 0, sizeof(*target_st
));
6823 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
6824 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
6825 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
6826 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
6828 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
6829 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
6830 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
6831 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
6832 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
6833 /* XXX: better use of kernel struct */
6834 __put_user(host_st
->st_size
, &target_st
->st_size
);
6835 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
6836 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
6837 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
6838 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
6839 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
6840 #if _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
6841 __put_user(host_st
->st_atim
.tv_nsec
, &target_st
->target_st_atime_nsec
);
6842 __put_user(host_st
->st_mtim
.tv_nsec
, &target_st
->target_st_mtime_nsec
);
6843 __put_user(host_st
->st_ctim
.tv_nsec
, &target_st
->target_st_ctime_nsec
);
6845 unlock_user_struct(target_st
, target_addr
, 1);
6852 #if defined(TARGET_NR_statx) && defined(__NR_statx)
6853 static inline abi_long
host_to_target_statx(struct target_statx
*host_stx
,
6854 abi_ulong target_addr
)
6856 struct target_statx
*target_stx
;
6858 if (!lock_user_struct(VERIFY_WRITE
, target_stx
, target_addr
, 0)) {
6859 return -TARGET_EFAULT
;
6861 memset(target_stx
, 0, sizeof(*target_stx
));
6863 __put_user(host_stx
->stx_mask
, &target_stx
->stx_mask
);
6864 __put_user(host_stx
->stx_blksize
, &target_stx
->stx_blksize
);
6865 __put_user(host_stx
->stx_attributes
, &target_stx
->stx_attributes
);
6866 __put_user(host_stx
->stx_nlink
, &target_stx
->stx_nlink
);
6867 __put_user(host_stx
->stx_uid
, &target_stx
->stx_uid
);
6868 __put_user(host_stx
->stx_gid
, &target_stx
->stx_gid
);
6869 __put_user(host_stx
->stx_mode
, &target_stx
->stx_mode
);
6870 __put_user(host_stx
->stx_ino
, &target_stx
->stx_ino
);
6871 __put_user(host_stx
->stx_size
, &target_stx
->stx_size
);
6872 __put_user(host_stx
->stx_blocks
, &target_stx
->stx_blocks
);
6873 __put_user(host_stx
->stx_attributes_mask
, &target_stx
->stx_attributes_mask
);
6874 __put_user(host_stx
->stx_atime
.tv_sec
, &target_stx
->stx_atime
.tv_sec
);
6875 __put_user(host_stx
->stx_atime
.tv_nsec
, &target_stx
->stx_atime
.tv_nsec
);
6876 __put_user(host_stx
->stx_btime
.tv_sec
, &target_stx
->stx_btime
.tv_sec
);
6877 __put_user(host_stx
->stx_btime
.tv_nsec
, &target_stx
->stx_btime
.tv_nsec
);
6878 __put_user(host_stx
->stx_ctime
.tv_sec
, &target_stx
->stx_ctime
.tv_sec
);
6879 __put_user(host_stx
->stx_ctime
.tv_nsec
, &target_stx
->stx_ctime
.tv_nsec
);
6880 __put_user(host_stx
->stx_mtime
.tv_sec
, &target_stx
->stx_mtime
.tv_sec
);
6881 __put_user(host_stx
->stx_mtime
.tv_nsec
, &target_stx
->stx_mtime
.tv_nsec
);
6882 __put_user(host_stx
->stx_rdev_major
, &target_stx
->stx_rdev_major
);
6883 __put_user(host_stx
->stx_rdev_minor
, &target_stx
->stx_rdev_minor
);
6884 __put_user(host_stx
->stx_dev_major
, &target_stx
->stx_dev_major
);
6885 __put_user(host_stx
->stx_dev_minor
, &target_stx
->stx_dev_minor
);
6887 unlock_user_struct(target_stx
, target_addr
, 1);
6894 /* ??? Using host futex calls even when target atomic operations
6895 are not really atomic probably breaks things. However implementing
6896 futexes locally would make futexes shared between multiple processes
6897 tricky. However they're probably useless because guest atomic
6898 operations won't work either. */
6899 #if defined(TARGET_NR_futex)
6900 static int do_futex(target_ulong uaddr
, int op
, int val
, target_ulong timeout
,
6901 target_ulong uaddr2
, int val3
)
6903 struct timespec ts
, *pts
;
6906 /* ??? We assume FUTEX_* constants are the same on both host
6908 #ifdef FUTEX_CMD_MASK
6909 base_op
= op
& FUTEX_CMD_MASK
;
6915 case FUTEX_WAIT_BITSET
:
6918 target_to_host_timespec(pts
, timeout
);
6922 return get_errno(safe_futex(g2h(uaddr
), op
, tswap32(val
),
6925 return get_errno(safe_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
6927 return get_errno(safe_futex(g2h(uaddr
), op
, val
, NULL
, NULL
, 0));
6929 case FUTEX_CMP_REQUEUE
:
6931 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
6932 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
6933 But the prototype takes a `struct timespec *'; insert casts
6934 to satisfy the compiler. We do not need to tswap TIMEOUT
6935 since it's not compared to guest memory. */
6936 pts
= (struct timespec
*)(uintptr_t) timeout
;
6937 return get_errno(safe_futex(g2h(uaddr
), op
, val
, pts
,
6939 (base_op
== FUTEX_CMP_REQUEUE
6943 return -TARGET_ENOSYS
;
6947 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
6948 static abi_long
do_name_to_handle_at(abi_long dirfd
, abi_long pathname
,
6949 abi_long handle
, abi_long mount_id
,
6952 struct file_handle
*target_fh
;
6953 struct file_handle
*fh
;
6957 unsigned int size
, total_size
;
6959 if (get_user_s32(size
, handle
)) {
6960 return -TARGET_EFAULT
;
6963 name
= lock_user_string(pathname
);
6965 return -TARGET_EFAULT
;
6968 total_size
= sizeof(struct file_handle
) + size
;
6969 target_fh
= lock_user(VERIFY_WRITE
, handle
, total_size
, 0);
6971 unlock_user(name
, pathname
, 0);
6972 return -TARGET_EFAULT
;
6975 fh
= g_malloc0(total_size
);
6976 fh
->handle_bytes
= size
;
6978 ret
= get_errno(name_to_handle_at(dirfd
, path(name
), fh
, &mid
, flags
));
6979 unlock_user(name
, pathname
, 0);
6981 /* man name_to_handle_at(2):
6982 * Other than the use of the handle_bytes field, the caller should treat
6983 * the file_handle structure as an opaque data type
6986 memcpy(target_fh
, fh
, total_size
);
6987 target_fh
->handle_bytes
= tswap32(fh
->handle_bytes
);
6988 target_fh
->handle_type
= tswap32(fh
->handle_type
);
6990 unlock_user(target_fh
, handle
, total_size
);
6992 if (put_user_s32(mid
, mount_id
)) {
6993 return -TARGET_EFAULT
;
7001 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7002 static abi_long
do_open_by_handle_at(abi_long mount_fd
, abi_long handle
,
7005 struct file_handle
*target_fh
;
7006 struct file_handle
*fh
;
7007 unsigned int size
, total_size
;
7010 if (get_user_s32(size
, handle
)) {
7011 return -TARGET_EFAULT
;
7014 total_size
= sizeof(struct file_handle
) + size
;
7015 target_fh
= lock_user(VERIFY_READ
, handle
, total_size
, 1);
7017 return -TARGET_EFAULT
;
7020 fh
= g_memdup(target_fh
, total_size
);
7021 fh
->handle_bytes
= size
;
7022 fh
->handle_type
= tswap32(target_fh
->handle_type
);
7024 ret
= get_errno(open_by_handle_at(mount_fd
, fh
,
7025 target_to_host_bitmask(flags
, fcntl_flags_tbl
)));
7029 unlock_user(target_fh
, handle
, total_size
);
7035 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7037 static abi_long
do_signalfd4(int fd
, abi_long mask
, int flags
)
7040 target_sigset_t
*target_mask
;
7044 if (flags
& ~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
)) {
7045 return -TARGET_EINVAL
;
7047 if (!lock_user_struct(VERIFY_READ
, target_mask
, mask
, 1)) {
7048 return -TARGET_EFAULT
;
7051 target_to_host_sigset(&host_mask
, target_mask
);
7053 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
7055 ret
= get_errno(signalfd(fd
, &host_mask
, host_flags
));
7057 fd_trans_register(ret
, &target_signalfd_trans
);
7060 unlock_user_struct(target_mask
, mask
, 0);
7066 /* Map host to target signal numbers for the wait family of syscalls.
7067 Assume all other status bits are the same. */
7068 int host_to_target_waitstatus(int status
)
7070 if (WIFSIGNALED(status
)) {
7071 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
7073 if (WIFSTOPPED(status
)) {
7074 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
7080 static int open_self_cmdline(void *cpu_env
, int fd
)
7082 CPUState
*cpu
= env_cpu((CPUArchState
*)cpu_env
);
7083 struct linux_binprm
*bprm
= ((TaskState
*)cpu
->opaque
)->bprm
;
7086 for (i
= 0; i
< bprm
->argc
; i
++) {
7087 size_t len
= strlen(bprm
->argv
[i
]) + 1;
7089 if (write(fd
, bprm
->argv
[i
], len
) != len
) {
7097 static int open_self_maps(void *cpu_env
, int fd
)
7099 CPUState
*cpu
= env_cpu((CPUArchState
*)cpu_env
);
7100 TaskState
*ts
= cpu
->opaque
;
7106 fp
= fopen("/proc/self/maps", "r");
7111 while ((read
= getline(&line
, &len
, fp
)) != -1) {
7112 int fields
, dev_maj
, dev_min
, inode
;
7113 uint64_t min
, max
, offset
;
7114 char flag_r
, flag_w
, flag_x
, flag_p
;
7115 char path
[512] = "";
7116 fields
= sscanf(line
, "%"PRIx64
"-%"PRIx64
" %c%c%c%c %"PRIx64
" %x:%x %d"
7117 " %512s", &min
, &max
, &flag_r
, &flag_w
, &flag_x
,
7118 &flag_p
, &offset
, &dev_maj
, &dev_min
, &inode
, path
);
7120 if ((fields
< 10) || (fields
> 11)) {
7123 if (h2g_valid(min
)) {
7124 int flags
= page_get_flags(h2g(min
));
7125 max
= h2g_valid(max
- 1) ? max
: (uintptr_t)g2h(GUEST_ADDR_MAX
) + 1;
7126 if (page_check_range(h2g(min
), max
- min
, flags
) == -1) {
7129 if (h2g(min
) == ts
->info
->stack_limit
) {
7130 pstrcpy(path
, sizeof(path
), " [stack]");
7132 dprintf(fd
, TARGET_ABI_FMT_ptr
"-" TARGET_ABI_FMT_ptr
7133 " %c%c%c%c %08" PRIx64
" %02x:%02x %d %s%s\n",
7134 h2g(min
), h2g(max
- 1) + 1, flag_r
, flag_w
,
7135 flag_x
, flag_p
, offset
, dev_maj
, dev_min
, inode
,
7136 path
[0] ? " " : "", path
);
7146 static int open_self_stat(void *cpu_env
, int fd
)
7148 CPUState
*cpu
= env_cpu((CPUArchState
*)cpu_env
);
7149 TaskState
*ts
= cpu
->opaque
;
7150 abi_ulong start_stack
= ts
->info
->start_stack
;
7153 for (i
= 0; i
< 44; i
++) {
7161 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
7162 } else if (i
== 1) {
7164 snprintf(buf
, sizeof(buf
), "(%s) ", ts
->bprm
->argv
[0]);
7165 } else if (i
== 27) {
7168 snprintf(buf
, sizeof(buf
), "%"PRId64
" ", val
);
7170 /* for the rest, there is MasterCard */
7171 snprintf(buf
, sizeof(buf
), "0%c", i
== 43 ? '\n' : ' ');
7175 if (write(fd
, buf
, len
) != len
) {
7183 static int open_self_auxv(void *cpu_env
, int fd
)
7185 CPUState
*cpu
= env_cpu((CPUArchState
*)cpu_env
);
7186 TaskState
*ts
= cpu
->opaque
;
7187 abi_ulong auxv
= ts
->info
->saved_auxv
;
7188 abi_ulong len
= ts
->info
->auxv_len
;
7192 * Auxiliary vector is stored in target process stack.
7193 * read in whole auxv vector and copy it to file
7195 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
7199 r
= write(fd
, ptr
, len
);
7206 lseek(fd
, 0, SEEK_SET
);
7207 unlock_user(ptr
, auxv
, len
);
7213 static int is_proc_myself(const char *filename
, const char *entry
)
7215 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
7216 filename
+= strlen("/proc/");
7217 if (!strncmp(filename
, "self/", strlen("self/"))) {
7218 filename
+= strlen("self/");
7219 } else if (*filename
>= '1' && *filename
<= '9') {
7221 snprintf(myself
, sizeof(myself
), "%d/", getpid());
7222 if (!strncmp(filename
, myself
, strlen(myself
))) {
7223 filename
+= strlen(myself
);
7230 if (!strcmp(filename
, entry
)) {
7237 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
7238 defined(TARGET_SPARC) || defined(TARGET_M68K)
7239 static int is_proc(const char *filename
, const char *entry
)
7241 return strcmp(filename
, entry
) == 0;
7245 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7246 static int open_net_route(void *cpu_env
, int fd
)
7253 fp
= fopen("/proc/net/route", "r");
7260 read
= getline(&line
, &len
, fp
);
7261 dprintf(fd
, "%s", line
);
7265 while ((read
= getline(&line
, &len
, fp
)) != -1) {
7267 uint32_t dest
, gw
, mask
;
7268 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
7271 fields
= sscanf(line
,
7272 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7273 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
7274 &mask
, &mtu
, &window
, &irtt
);
7278 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7279 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
7280 metric
, tswap32(mask
), mtu
, window
, irtt
);
7290 #if defined(TARGET_SPARC)
7291 static int open_cpuinfo(void *cpu_env
, int fd
)
7293 dprintf(fd
, "type\t\t: sun4u\n");
7298 #if defined(TARGET_M68K)
7299 static int open_hardware(void *cpu_env
, int fd
)
7301 dprintf(fd
, "Model:\t\tqemu-m68k\n");
7306 static int do_openat(void *cpu_env
, int dirfd
, const char *pathname
, int flags
, mode_t mode
)
7309 const char *filename
;
7310 int (*fill
)(void *cpu_env
, int fd
);
7311 int (*cmp
)(const char *s1
, const char *s2
);
7313 const struct fake_open
*fake_open
;
7314 static const struct fake_open fakes
[] = {
7315 { "maps", open_self_maps
, is_proc_myself
},
7316 { "stat", open_self_stat
, is_proc_myself
},
7317 { "auxv", open_self_auxv
, is_proc_myself
},
7318 { "cmdline", open_self_cmdline
, is_proc_myself
},
7319 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7320 { "/proc/net/route", open_net_route
, is_proc
},
7322 #if defined(TARGET_SPARC)
7323 { "/proc/cpuinfo", open_cpuinfo
, is_proc
},
7325 #if defined(TARGET_M68K)
7326 { "/proc/hardware", open_hardware
, is_proc
},
7328 { NULL
, NULL
, NULL
}
7331 if (is_proc_myself(pathname
, "exe")) {
7332 int execfd
= qemu_getauxval(AT_EXECFD
);
7333 return execfd
? execfd
: safe_openat(dirfd
, exec_path
, flags
, mode
);
7336 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
7337 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
7342 if (fake_open
->filename
) {
7344 char filename
[PATH_MAX
];
7347 /* create temporary file to map stat to */
7348 tmpdir
= getenv("TMPDIR");
7351 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
7352 fd
= mkstemp(filename
);
7358 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
7364 lseek(fd
, 0, SEEK_SET
);
7369 return safe_openat(dirfd
, path(pathname
), flags
, mode
);
7372 #define TIMER_MAGIC 0x0caf0000
7373 #define TIMER_MAGIC_MASK 0xffff0000
7375 /* Convert QEMU provided timer ID back to internal 16bit index format */
7376 static target_timer_t
get_timer_id(abi_long arg
)
7378 target_timer_t timerid
= arg
;
7380 if ((timerid
& TIMER_MAGIC_MASK
) != TIMER_MAGIC
) {
7381 return -TARGET_EINVAL
;
7386 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
7387 return -TARGET_EINVAL
;
7393 static int target_to_host_cpu_mask(unsigned long *host_mask
,
7395 abi_ulong target_addr
,
7398 unsigned target_bits
= sizeof(abi_ulong
) * 8;
7399 unsigned host_bits
= sizeof(*host_mask
) * 8;
7400 abi_ulong
*target_mask
;
7403 assert(host_size
>= target_size
);
7405 target_mask
= lock_user(VERIFY_READ
, target_addr
, target_size
, 1);
7407 return -TARGET_EFAULT
;
7409 memset(host_mask
, 0, host_size
);
7411 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
7412 unsigned bit
= i
* target_bits
;
7415 __get_user(val
, &target_mask
[i
]);
7416 for (j
= 0; j
< target_bits
; j
++, bit
++) {
7417 if (val
& (1UL << j
)) {
7418 host_mask
[bit
/ host_bits
] |= 1UL << (bit
% host_bits
);
7423 unlock_user(target_mask
, target_addr
, 0);
7427 static int host_to_target_cpu_mask(const unsigned long *host_mask
,
7429 abi_ulong target_addr
,
7432 unsigned target_bits
= sizeof(abi_ulong
) * 8;
7433 unsigned host_bits
= sizeof(*host_mask
) * 8;
7434 abi_ulong
*target_mask
;
7437 assert(host_size
>= target_size
);
7439 target_mask
= lock_user(VERIFY_WRITE
, target_addr
, target_size
, 0);
7441 return -TARGET_EFAULT
;
7444 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
7445 unsigned bit
= i
* target_bits
;
7448 for (j
= 0; j
< target_bits
; j
++, bit
++) {
7449 if (host_mask
[bit
/ host_bits
] & (1UL << (bit
% host_bits
))) {
7453 __put_user(val
, &target_mask
[i
]);
7456 unlock_user(target_mask
, target_addr
, target_size
);
7460 /* This is an internal helper for do_syscall so that it is easier
7461 * to have a single return point, so that actions, such as logging
7462 * of syscall results, can be performed.
7463 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
7465 static abi_long
do_syscall1(void *cpu_env
, int num
, abi_long arg1
,
7466 abi_long arg2
, abi_long arg3
, abi_long arg4
,
7467 abi_long arg5
, abi_long arg6
, abi_long arg7
,
7470 CPUState
*cpu
= env_cpu(cpu_env
);
7472 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
7473 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
7474 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
7475 || defined(TARGET_NR_statx)
7478 #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
7479 || defined(TARGET_NR_fstatfs)
7485 case TARGET_NR_exit
:
7486 /* In old applications this may be used to implement _exit(2).
7487 However in threaded applictions it is used for thread termination,
7488 and _exit_group is used for application termination.
7489 Do thread termination if we have more then one thread. */
7491 if (block_signals()) {
7492 return -TARGET_ERESTARTSYS
;
7497 if (CPU_NEXT(first_cpu
)) {
7500 /* Remove the CPU from the list. */
7501 QTAILQ_REMOVE_RCU(&cpus
, cpu
, node
);
7506 if (ts
->child_tidptr
) {
7507 put_user_u32(0, ts
->child_tidptr
);
7508 sys_futex(g2h(ts
->child_tidptr
), FUTEX_WAKE
, INT_MAX
,
7512 object_unref(OBJECT(cpu
));
7514 rcu_unregister_thread();
7519 preexit_cleanup(cpu_env
, arg1
);
7521 return 0; /* avoid warning */
7522 case TARGET_NR_read
:
7523 if (arg2
== 0 && arg3
== 0) {
7524 return get_errno(safe_read(arg1
, 0, 0));
7526 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
7527 return -TARGET_EFAULT
;
7528 ret
= get_errno(safe_read(arg1
, p
, arg3
));
7530 fd_trans_host_to_target_data(arg1
)) {
7531 ret
= fd_trans_host_to_target_data(arg1
)(p
, ret
);
7533 unlock_user(p
, arg2
, ret
);
7536 case TARGET_NR_write
:
7537 if (arg2
== 0 && arg3
== 0) {
7538 return get_errno(safe_write(arg1
, 0, 0));
7540 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
7541 return -TARGET_EFAULT
;
7542 if (fd_trans_target_to_host_data(arg1
)) {
7543 void *copy
= g_malloc(arg3
);
7544 memcpy(copy
, p
, arg3
);
7545 ret
= fd_trans_target_to_host_data(arg1
)(copy
, arg3
);
7547 ret
= get_errno(safe_write(arg1
, copy
, ret
));
7551 ret
= get_errno(safe_write(arg1
, p
, arg3
));
7553 unlock_user(p
, arg2
, 0);
7556 #ifdef TARGET_NR_open
7557 case TARGET_NR_open
:
7558 if (!(p
= lock_user_string(arg1
)))
7559 return -TARGET_EFAULT
;
7560 ret
= get_errno(do_openat(cpu_env
, AT_FDCWD
, p
,
7561 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
7563 fd_trans_unregister(ret
);
7564 unlock_user(p
, arg1
, 0);
7567 case TARGET_NR_openat
:
7568 if (!(p
= lock_user_string(arg2
)))
7569 return -TARGET_EFAULT
;
7570 ret
= get_errno(do_openat(cpu_env
, arg1
, p
,
7571 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
7573 fd_trans_unregister(ret
);
7574 unlock_user(p
, arg2
, 0);
7576 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7577 case TARGET_NR_name_to_handle_at
:
7578 ret
= do_name_to_handle_at(arg1
, arg2
, arg3
, arg4
, arg5
);
7581 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7582 case TARGET_NR_open_by_handle_at
:
7583 ret
= do_open_by_handle_at(arg1
, arg2
, arg3
);
7584 fd_trans_unregister(ret
);
7587 case TARGET_NR_close
:
7588 fd_trans_unregister(arg1
);
7589 return get_errno(close(arg1
));
7592 return do_brk(arg1
);
7593 #ifdef TARGET_NR_fork
7594 case TARGET_NR_fork
:
7595 return get_errno(do_fork(cpu_env
, TARGET_SIGCHLD
, 0, 0, 0, 0));
7597 #ifdef TARGET_NR_waitpid
7598 case TARGET_NR_waitpid
:
7601 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, 0));
7602 if (!is_error(ret
) && arg2
&& ret
7603 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
7604 return -TARGET_EFAULT
;
7608 #ifdef TARGET_NR_waitid
7609 case TARGET_NR_waitid
:
7613 ret
= get_errno(safe_waitid(arg1
, arg2
, &info
, arg4
, NULL
));
7614 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
7615 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
7616 return -TARGET_EFAULT
;
7617 host_to_target_siginfo(p
, &info
);
7618 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
7623 #ifdef TARGET_NR_creat /* not on alpha */
7624 case TARGET_NR_creat
:
7625 if (!(p
= lock_user_string(arg1
)))
7626 return -TARGET_EFAULT
;
7627 ret
= get_errno(creat(p
, arg2
));
7628 fd_trans_unregister(ret
);
7629 unlock_user(p
, arg1
, 0);
7632 #ifdef TARGET_NR_link
7633 case TARGET_NR_link
:
7636 p
= lock_user_string(arg1
);
7637 p2
= lock_user_string(arg2
);
7639 ret
= -TARGET_EFAULT
;
7641 ret
= get_errno(link(p
, p2
));
7642 unlock_user(p2
, arg2
, 0);
7643 unlock_user(p
, arg1
, 0);
7647 #if defined(TARGET_NR_linkat)
7648 case TARGET_NR_linkat
:
7652 return -TARGET_EFAULT
;
7653 p
= lock_user_string(arg2
);
7654 p2
= lock_user_string(arg4
);
7656 ret
= -TARGET_EFAULT
;
7658 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
7659 unlock_user(p
, arg2
, 0);
7660 unlock_user(p2
, arg4
, 0);
7664 #ifdef TARGET_NR_unlink
7665 case TARGET_NR_unlink
:
7666 if (!(p
= lock_user_string(arg1
)))
7667 return -TARGET_EFAULT
;
7668 ret
= get_errno(unlink(p
));
7669 unlock_user(p
, arg1
, 0);
7672 #if defined(TARGET_NR_unlinkat)
7673 case TARGET_NR_unlinkat
:
7674 if (!(p
= lock_user_string(arg2
)))
7675 return -TARGET_EFAULT
;
7676 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
7677 unlock_user(p
, arg2
, 0);
7680 case TARGET_NR_execve
:
7682 char **argp
, **envp
;
7685 abi_ulong guest_argp
;
7686 abi_ulong guest_envp
;
7693 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
7694 if (get_user_ual(addr
, gp
))
7695 return -TARGET_EFAULT
;
7702 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
7703 if (get_user_ual(addr
, gp
))
7704 return -TARGET_EFAULT
;
7710 argp
= g_new0(char *, argc
+ 1);
7711 envp
= g_new0(char *, envc
+ 1);
7713 for (gp
= guest_argp
, q
= argp
; gp
;
7714 gp
+= sizeof(abi_ulong
), q
++) {
7715 if (get_user_ual(addr
, gp
))
7719 if (!(*q
= lock_user_string(addr
)))
7721 total_size
+= strlen(*q
) + 1;
7725 for (gp
= guest_envp
, q
= envp
; gp
;
7726 gp
+= sizeof(abi_ulong
), q
++) {
7727 if (get_user_ual(addr
, gp
))
7731 if (!(*q
= lock_user_string(addr
)))
7733 total_size
+= strlen(*q
) + 1;
7737 if (!(p
= lock_user_string(arg1
)))
7739 /* Although execve() is not an interruptible syscall it is
7740 * a special case where we must use the safe_syscall wrapper:
7741 * if we allow a signal to happen before we make the host
7742 * syscall then we will 'lose' it, because at the point of
7743 * execve the process leaves QEMU's control. So we use the
7744 * safe syscall wrapper to ensure that we either take the
7745 * signal as a guest signal, or else it does not happen
7746 * before the execve completes and makes it the other
7747 * program's problem.
7749 ret
= get_errno(safe_execve(p
, argp
, envp
));
7750 unlock_user(p
, arg1
, 0);
7755 ret
= -TARGET_EFAULT
;
7758 for (gp
= guest_argp
, q
= argp
; *q
;
7759 gp
+= sizeof(abi_ulong
), q
++) {
7760 if (get_user_ual(addr
, gp
)
7763 unlock_user(*q
, addr
, 0);
7765 for (gp
= guest_envp
, q
= envp
; *q
;
7766 gp
+= sizeof(abi_ulong
), q
++) {
7767 if (get_user_ual(addr
, gp
)
7770 unlock_user(*q
, addr
, 0);
7777 case TARGET_NR_chdir
:
7778 if (!(p
= lock_user_string(arg1
)))
7779 return -TARGET_EFAULT
;
7780 ret
= get_errno(chdir(p
));
7781 unlock_user(p
, arg1
, 0);
7783 #ifdef TARGET_NR_time
7784 case TARGET_NR_time
:
7787 ret
= get_errno(time(&host_time
));
7790 && put_user_sal(host_time
, arg1
))
7791 return -TARGET_EFAULT
;
7795 #ifdef TARGET_NR_mknod
7796 case TARGET_NR_mknod
:
7797 if (!(p
= lock_user_string(arg1
)))
7798 return -TARGET_EFAULT
;
7799 ret
= get_errno(mknod(p
, arg2
, arg3
));
7800 unlock_user(p
, arg1
, 0);
7803 #if defined(TARGET_NR_mknodat)
7804 case TARGET_NR_mknodat
:
7805 if (!(p
= lock_user_string(arg2
)))
7806 return -TARGET_EFAULT
;
7807 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
7808 unlock_user(p
, arg2
, 0);
7811 #ifdef TARGET_NR_chmod
7812 case TARGET_NR_chmod
:
7813 if (!(p
= lock_user_string(arg1
)))
7814 return -TARGET_EFAULT
;
7815 ret
= get_errno(chmod(p
, arg2
));
7816 unlock_user(p
, arg1
, 0);
7819 #ifdef TARGET_NR_lseek
7820 case TARGET_NR_lseek
:
7821 return get_errno(lseek(arg1
, arg2
, arg3
));
7823 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
7824 /* Alpha specific */
7825 case TARGET_NR_getxpid
:
7826 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
] = getppid();
7827 return get_errno(getpid());
7829 #ifdef TARGET_NR_getpid
7830 case TARGET_NR_getpid
:
7831 return get_errno(getpid());
7833 case TARGET_NR_mount
:
7835 /* need to look at the data field */
7839 p
= lock_user_string(arg1
);
7841 return -TARGET_EFAULT
;
7847 p2
= lock_user_string(arg2
);
7850 unlock_user(p
, arg1
, 0);
7852 return -TARGET_EFAULT
;
7856 p3
= lock_user_string(arg3
);
7859 unlock_user(p
, arg1
, 0);
7861 unlock_user(p2
, arg2
, 0);
7862 return -TARGET_EFAULT
;
7868 /* FIXME - arg5 should be locked, but it isn't clear how to
7869 * do that since it's not guaranteed to be a NULL-terminated
7873 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
7875 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(arg5
));
7877 ret
= get_errno(ret
);
7880 unlock_user(p
, arg1
, 0);
7882 unlock_user(p2
, arg2
, 0);
7884 unlock_user(p3
, arg3
, 0);
7888 #ifdef TARGET_NR_umount
7889 case TARGET_NR_umount
:
7890 if (!(p
= lock_user_string(arg1
)))
7891 return -TARGET_EFAULT
;
7892 ret
= get_errno(umount(p
));
7893 unlock_user(p
, arg1
, 0);
7896 #ifdef TARGET_NR_stime /* not on alpha */
7897 case TARGET_NR_stime
:
7901 if (get_user_sal(ts
.tv_sec
, arg1
)) {
7902 return -TARGET_EFAULT
;
7904 return get_errno(clock_settime(CLOCK_REALTIME
, &ts
));
7907 #ifdef TARGET_NR_alarm /* not on alpha */
7908 case TARGET_NR_alarm
:
7911 #ifdef TARGET_NR_pause /* not on alpha */
7912 case TARGET_NR_pause
:
7913 if (!block_signals()) {
7914 sigsuspend(&((TaskState
*)cpu
->opaque
)->signal_mask
);
7916 return -TARGET_EINTR
;
7918 #ifdef TARGET_NR_utime
7919 case TARGET_NR_utime
:
7921 struct utimbuf tbuf
, *host_tbuf
;
7922 struct target_utimbuf
*target_tbuf
;
7924 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
7925 return -TARGET_EFAULT
;
7926 tbuf
.actime
= tswapal(target_tbuf
->actime
);
7927 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
7928 unlock_user_struct(target_tbuf
, arg2
, 0);
7933 if (!(p
= lock_user_string(arg1
)))
7934 return -TARGET_EFAULT
;
7935 ret
= get_errno(utime(p
, host_tbuf
));
7936 unlock_user(p
, arg1
, 0);
7940 #ifdef TARGET_NR_utimes
7941 case TARGET_NR_utimes
:
7943 struct timeval
*tvp
, tv
[2];
7945 if (copy_from_user_timeval(&tv
[0], arg2
)
7946 || copy_from_user_timeval(&tv
[1],
7947 arg2
+ sizeof(struct target_timeval
)))
7948 return -TARGET_EFAULT
;
7953 if (!(p
= lock_user_string(arg1
)))
7954 return -TARGET_EFAULT
;
7955 ret
= get_errno(utimes(p
, tvp
));
7956 unlock_user(p
, arg1
, 0);
7960 #if defined(TARGET_NR_futimesat)
7961 case TARGET_NR_futimesat
:
7963 struct timeval
*tvp
, tv
[2];
7965 if (copy_from_user_timeval(&tv
[0], arg3
)
7966 || copy_from_user_timeval(&tv
[1],
7967 arg3
+ sizeof(struct target_timeval
)))
7968 return -TARGET_EFAULT
;
7973 if (!(p
= lock_user_string(arg2
))) {
7974 return -TARGET_EFAULT
;
7976 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
7977 unlock_user(p
, arg2
, 0);
7981 #ifdef TARGET_NR_access
7982 case TARGET_NR_access
:
7983 if (!(p
= lock_user_string(arg1
))) {
7984 return -TARGET_EFAULT
;
7986 ret
= get_errno(access(path(p
), arg2
));
7987 unlock_user(p
, arg1
, 0);
7990 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
7991 case TARGET_NR_faccessat
:
7992 if (!(p
= lock_user_string(arg2
))) {
7993 return -TARGET_EFAULT
;
7995 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
7996 unlock_user(p
, arg2
, 0);
7999 #ifdef TARGET_NR_nice /* not on alpha */
8000 case TARGET_NR_nice
:
8001 return get_errno(nice(arg1
));
8003 case TARGET_NR_sync
:
8006 #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
8007 case TARGET_NR_syncfs
:
8008 return get_errno(syncfs(arg1
));
8010 case TARGET_NR_kill
:
8011 return get_errno(safe_kill(arg1
, target_to_host_signal(arg2
)));
8012 #ifdef TARGET_NR_rename
8013 case TARGET_NR_rename
:
8016 p
= lock_user_string(arg1
);
8017 p2
= lock_user_string(arg2
);
8019 ret
= -TARGET_EFAULT
;
8021 ret
= get_errno(rename(p
, p2
));
8022 unlock_user(p2
, arg2
, 0);
8023 unlock_user(p
, arg1
, 0);
8027 #if defined(TARGET_NR_renameat)
8028 case TARGET_NR_renameat
:
8031 p
= lock_user_string(arg2
);
8032 p2
= lock_user_string(arg4
);
8034 ret
= -TARGET_EFAULT
;
8036 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
8037 unlock_user(p2
, arg4
, 0);
8038 unlock_user(p
, arg2
, 0);
8042 #if defined(TARGET_NR_renameat2)
8043 case TARGET_NR_renameat2
:
8046 p
= lock_user_string(arg2
);
8047 p2
= lock_user_string(arg4
);
8049 ret
= -TARGET_EFAULT
;
8051 ret
= get_errno(sys_renameat2(arg1
, p
, arg3
, p2
, arg5
));
8053 unlock_user(p2
, arg4
, 0);
8054 unlock_user(p
, arg2
, 0);
8058 #ifdef TARGET_NR_mkdir
8059 case TARGET_NR_mkdir
:
8060 if (!(p
= lock_user_string(arg1
)))
8061 return -TARGET_EFAULT
;
8062 ret
= get_errno(mkdir(p
, arg2
));
8063 unlock_user(p
, arg1
, 0);
8066 #if defined(TARGET_NR_mkdirat)
8067 case TARGET_NR_mkdirat
:
8068 if (!(p
= lock_user_string(arg2
)))
8069 return -TARGET_EFAULT
;
8070 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
8071 unlock_user(p
, arg2
, 0);
8074 #ifdef TARGET_NR_rmdir
8075 case TARGET_NR_rmdir
:
8076 if (!(p
= lock_user_string(arg1
)))
8077 return -TARGET_EFAULT
;
8078 ret
= get_errno(rmdir(p
));
8079 unlock_user(p
, arg1
, 0);
8083 ret
= get_errno(dup(arg1
));
8085 fd_trans_dup(arg1
, ret
);
8088 #ifdef TARGET_NR_pipe
8089 case TARGET_NR_pipe
:
8090 return do_pipe(cpu_env
, arg1
, 0, 0);
8092 #ifdef TARGET_NR_pipe2
8093 case TARGET_NR_pipe2
:
8094 return do_pipe(cpu_env
, arg1
,
8095 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
8097 case TARGET_NR_times
:
8099 struct target_tms
*tmsp
;
8101 ret
= get_errno(times(&tms
));
8103 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
8105 return -TARGET_EFAULT
;
8106 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
8107 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
8108 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
8109 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
8112 ret
= host_to_target_clock_t(ret
);
8115 case TARGET_NR_acct
:
8117 ret
= get_errno(acct(NULL
));
8119 if (!(p
= lock_user_string(arg1
))) {
8120 return -TARGET_EFAULT
;
8122 ret
= get_errno(acct(path(p
)));
8123 unlock_user(p
, arg1
, 0);
8126 #ifdef TARGET_NR_umount2
8127 case TARGET_NR_umount2
:
8128 if (!(p
= lock_user_string(arg1
)))
8129 return -TARGET_EFAULT
;
8130 ret
= get_errno(umount2(p
, arg2
));
8131 unlock_user(p
, arg1
, 0);
8134 case TARGET_NR_ioctl
:
8135 return do_ioctl(arg1
, arg2
, arg3
);
8136 #ifdef TARGET_NR_fcntl
8137 case TARGET_NR_fcntl
:
8138 return do_fcntl(arg1
, arg2
, arg3
);
8140 case TARGET_NR_setpgid
:
8141 return get_errno(setpgid(arg1
, arg2
));
8142 case TARGET_NR_umask
:
8143 return get_errno(umask(arg1
));
8144 case TARGET_NR_chroot
:
8145 if (!(p
= lock_user_string(arg1
)))
8146 return -TARGET_EFAULT
;
8147 ret
= get_errno(chroot(p
));
8148 unlock_user(p
, arg1
, 0);
8150 #ifdef TARGET_NR_dup2
8151 case TARGET_NR_dup2
:
8152 ret
= get_errno(dup2(arg1
, arg2
));
8154 fd_trans_dup(arg1
, arg2
);
8158 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
8159 case TARGET_NR_dup3
:
8163 if ((arg3
& ~TARGET_O_CLOEXEC
) != 0) {
8166 host_flags
= target_to_host_bitmask(arg3
, fcntl_flags_tbl
);
8167 ret
= get_errno(dup3(arg1
, arg2
, host_flags
));
8169 fd_trans_dup(arg1
, arg2
);
8174 #ifdef TARGET_NR_getppid /* not on alpha */
8175 case TARGET_NR_getppid
:
8176 return get_errno(getppid());
8178 #ifdef TARGET_NR_getpgrp
8179 case TARGET_NR_getpgrp
:
8180 return get_errno(getpgrp());
8182 case TARGET_NR_setsid
:
8183 return get_errno(setsid());
8184 #ifdef TARGET_NR_sigaction
8185 case TARGET_NR_sigaction
:
8187 #if defined(TARGET_ALPHA)
8188 struct target_sigaction act
, oact
, *pact
= 0;
8189 struct target_old_sigaction
*old_act
;
8191 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8192 return -TARGET_EFAULT
;
8193 act
._sa_handler
= old_act
->_sa_handler
;
8194 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
8195 act
.sa_flags
= old_act
->sa_flags
;
8196 act
.sa_restorer
= 0;
8197 unlock_user_struct(old_act
, arg2
, 0);
8200 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8201 if (!is_error(ret
) && arg3
) {
8202 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8203 return -TARGET_EFAULT
;
8204 old_act
->_sa_handler
= oact
._sa_handler
;
8205 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
8206 old_act
->sa_flags
= oact
.sa_flags
;
8207 unlock_user_struct(old_act
, arg3
, 1);
8209 #elif defined(TARGET_MIPS)
8210 struct target_sigaction act
, oact
, *pact
, *old_act
;
8213 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8214 return -TARGET_EFAULT
;
8215 act
._sa_handler
= old_act
->_sa_handler
;
8216 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
8217 act
.sa_flags
= old_act
->sa_flags
;
8218 unlock_user_struct(old_act
, arg2
, 0);
8224 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8226 if (!is_error(ret
) && arg3
) {
8227 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8228 return -TARGET_EFAULT
;
8229 old_act
->_sa_handler
= oact
._sa_handler
;
8230 old_act
->sa_flags
= oact
.sa_flags
;
8231 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
8232 old_act
->sa_mask
.sig
[1] = 0;
8233 old_act
->sa_mask
.sig
[2] = 0;
8234 old_act
->sa_mask
.sig
[3] = 0;
8235 unlock_user_struct(old_act
, arg3
, 1);
8238 struct target_old_sigaction
*old_act
;
8239 struct target_sigaction act
, oact
, *pact
;
8241 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
8242 return -TARGET_EFAULT
;
8243 act
._sa_handler
= old_act
->_sa_handler
;
8244 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
8245 act
.sa_flags
= old_act
->sa_flags
;
8246 act
.sa_restorer
= old_act
->sa_restorer
;
8247 #ifdef TARGET_ARCH_HAS_KA_RESTORER
8248 act
.ka_restorer
= 0;
8250 unlock_user_struct(old_act
, arg2
, 0);
8255 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8256 if (!is_error(ret
) && arg3
) {
8257 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
8258 return -TARGET_EFAULT
;
8259 old_act
->_sa_handler
= oact
._sa_handler
;
8260 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
8261 old_act
->sa_flags
= oact
.sa_flags
;
8262 old_act
->sa_restorer
= oact
.sa_restorer
;
8263 unlock_user_struct(old_act
, arg3
, 1);
8269 case TARGET_NR_rt_sigaction
:
8271 #if defined(TARGET_ALPHA)
8272 /* For Alpha and SPARC this is a 5 argument syscall, with
8273 * a 'restorer' parameter which must be copied into the
8274 * sa_restorer field of the sigaction struct.
8275 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
8276 * and arg5 is the sigsetsize.
8277 * Alpha also has a separate rt_sigaction struct that it uses
8278 * here; SPARC uses the usual sigaction struct.
8280 struct target_rt_sigaction
*rt_act
;
8281 struct target_sigaction act
, oact
, *pact
= 0;
8283 if (arg4
!= sizeof(target_sigset_t
)) {
8284 return -TARGET_EINVAL
;
8287 if (!lock_user_struct(VERIFY_READ
, rt_act
, arg2
, 1))
8288 return -TARGET_EFAULT
;
8289 act
._sa_handler
= rt_act
->_sa_handler
;
8290 act
.sa_mask
= rt_act
->sa_mask
;
8291 act
.sa_flags
= rt_act
->sa_flags
;
8292 act
.sa_restorer
= arg5
;
8293 unlock_user_struct(rt_act
, arg2
, 0);
8296 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
));
8297 if (!is_error(ret
) && arg3
) {
8298 if (!lock_user_struct(VERIFY_WRITE
, rt_act
, arg3
, 0))
8299 return -TARGET_EFAULT
;
8300 rt_act
->_sa_handler
= oact
._sa_handler
;
8301 rt_act
->sa_mask
= oact
.sa_mask
;
8302 rt_act
->sa_flags
= oact
.sa_flags
;
8303 unlock_user_struct(rt_act
, arg3
, 1);
8307 target_ulong restorer
= arg4
;
8308 target_ulong sigsetsize
= arg5
;
8310 target_ulong sigsetsize
= arg4
;
8312 struct target_sigaction
*act
;
8313 struct target_sigaction
*oact
;
8315 if (sigsetsize
!= sizeof(target_sigset_t
)) {
8316 return -TARGET_EINVAL
;
8319 if (!lock_user_struct(VERIFY_READ
, act
, arg2
, 1)) {
8320 return -TARGET_EFAULT
;
8322 #ifdef TARGET_ARCH_HAS_KA_RESTORER
8323 act
->ka_restorer
= restorer
;
8329 if (!lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
8330 ret
= -TARGET_EFAULT
;
8331 goto rt_sigaction_fail
;
8335 ret
= get_errno(do_sigaction(arg1
, act
, oact
));
8338 unlock_user_struct(act
, arg2
, 0);
8340 unlock_user_struct(oact
, arg3
, 1);
8344 #ifdef TARGET_NR_sgetmask /* not on alpha */
8345 case TARGET_NR_sgetmask
:
8348 abi_ulong target_set
;
8349 ret
= do_sigprocmask(0, NULL
, &cur_set
);
8351 host_to_target_old_sigset(&target_set
, &cur_set
);
8357 #ifdef TARGET_NR_ssetmask /* not on alpha */
8358 case TARGET_NR_ssetmask
:
8361 abi_ulong target_set
= arg1
;
8362 target_to_host_old_sigset(&set
, &target_set
);
8363 ret
= do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
8365 host_to_target_old_sigset(&target_set
, &oset
);
8371 #ifdef TARGET_NR_sigprocmask
8372 case TARGET_NR_sigprocmask
:
8374 #if defined(TARGET_ALPHA)
8375 sigset_t set
, oldset
;
8380 case TARGET_SIG_BLOCK
:
8383 case TARGET_SIG_UNBLOCK
:
8386 case TARGET_SIG_SETMASK
:
8390 return -TARGET_EINVAL
;
8393 target_to_host_old_sigset(&set
, &mask
);
8395 ret
= do_sigprocmask(how
, &set
, &oldset
);
8396 if (!is_error(ret
)) {
8397 host_to_target_old_sigset(&mask
, &oldset
);
8399 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0; /* force no error */
8402 sigset_t set
, oldset
, *set_ptr
;
8407 case TARGET_SIG_BLOCK
:
8410 case TARGET_SIG_UNBLOCK
:
8413 case TARGET_SIG_SETMASK
:
8417 return -TARGET_EINVAL
;
8419 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
8420 return -TARGET_EFAULT
;
8421 target_to_host_old_sigset(&set
, p
);
8422 unlock_user(p
, arg2
, 0);
8428 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
8429 if (!is_error(ret
) && arg3
) {
8430 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
8431 return -TARGET_EFAULT
;
8432 host_to_target_old_sigset(p
, &oldset
);
8433 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
8439 case TARGET_NR_rt_sigprocmask
:
8442 sigset_t set
, oldset
, *set_ptr
;
8444 if (arg4
!= sizeof(target_sigset_t
)) {
8445 return -TARGET_EINVAL
;
8450 case TARGET_SIG_BLOCK
:
8453 case TARGET_SIG_UNBLOCK
:
8456 case TARGET_SIG_SETMASK
:
8460 return -TARGET_EINVAL
;
8462 if (!(p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1)))
8463 return -TARGET_EFAULT
;
8464 target_to_host_sigset(&set
, p
);
8465 unlock_user(p
, arg2
, 0);
8471 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
8472 if (!is_error(ret
) && arg3
) {
8473 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
8474 return -TARGET_EFAULT
;
8475 host_to_target_sigset(p
, &oldset
);
8476 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
8480 #ifdef TARGET_NR_sigpending
8481 case TARGET_NR_sigpending
:
8484 ret
= get_errno(sigpending(&set
));
8485 if (!is_error(ret
)) {
8486 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
8487 return -TARGET_EFAULT
;
8488 host_to_target_old_sigset(p
, &set
);
8489 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
8494 case TARGET_NR_rt_sigpending
:
8498 /* Yes, this check is >, not != like most. We follow the kernel's
8499 * logic and it does it like this because it implements
8500 * NR_sigpending through the same code path, and in that case
8501 * the old_sigset_t is smaller in size.
8503 if (arg2
> sizeof(target_sigset_t
)) {
8504 return -TARGET_EINVAL
;
8507 ret
= get_errno(sigpending(&set
));
8508 if (!is_error(ret
)) {
8509 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
8510 return -TARGET_EFAULT
;
8511 host_to_target_sigset(p
, &set
);
8512 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
8516 #ifdef TARGET_NR_sigsuspend
8517 case TARGET_NR_sigsuspend
:
8519 TaskState
*ts
= cpu
->opaque
;
8520 #if defined(TARGET_ALPHA)
8521 abi_ulong mask
= arg1
;
8522 target_to_host_old_sigset(&ts
->sigsuspend_mask
, &mask
);
8524 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
8525 return -TARGET_EFAULT
;
8526 target_to_host_old_sigset(&ts
->sigsuspend_mask
, p
);
8527 unlock_user(p
, arg1
, 0);
8529 ret
= get_errno(safe_rt_sigsuspend(&ts
->sigsuspend_mask
,
8531 if (ret
!= -TARGET_ERESTARTSYS
) {
8532 ts
->in_sigsuspend
= 1;
8537 case TARGET_NR_rt_sigsuspend
:
8539 TaskState
*ts
= cpu
->opaque
;
8541 if (arg2
!= sizeof(target_sigset_t
)) {
8542 return -TARGET_EINVAL
;
8544 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
8545 return -TARGET_EFAULT
;
8546 target_to_host_sigset(&ts
->sigsuspend_mask
, p
);
8547 unlock_user(p
, arg1
, 0);
8548 ret
= get_errno(safe_rt_sigsuspend(&ts
->sigsuspend_mask
,
8550 if (ret
!= -TARGET_ERESTARTSYS
) {
8551 ts
->in_sigsuspend
= 1;
8555 #ifdef TARGET_NR_rt_sigtimedwait
8556 case TARGET_NR_rt_sigtimedwait
:
8559 struct timespec uts
, *puts
;
8562 if (arg4
!= sizeof(target_sigset_t
)) {
8563 return -TARGET_EINVAL
;
8566 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
8567 return -TARGET_EFAULT
;
8568 target_to_host_sigset(&set
, p
);
8569 unlock_user(p
, arg1
, 0);
8572 target_to_host_timespec(puts
, arg3
);
8576 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
8578 if (!is_error(ret
)) {
8580 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
8583 return -TARGET_EFAULT
;
8585 host_to_target_siginfo(p
, &uinfo
);
8586 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
8588 ret
= host_to_target_signal(ret
);
8593 case TARGET_NR_rt_sigqueueinfo
:
8597 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
8599 return -TARGET_EFAULT
;
8601 target_to_host_siginfo(&uinfo
, p
);
8602 unlock_user(p
, arg3
, 0);
8603 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, arg2
, &uinfo
));
8606 case TARGET_NR_rt_tgsigqueueinfo
:
8610 p
= lock_user(VERIFY_READ
, arg4
, sizeof(target_siginfo_t
), 1);
8612 return -TARGET_EFAULT
;
8614 target_to_host_siginfo(&uinfo
, p
);
8615 unlock_user(p
, arg4
, 0);
8616 ret
= get_errno(sys_rt_tgsigqueueinfo(arg1
, arg2
, arg3
, &uinfo
));
8619 #ifdef TARGET_NR_sigreturn
8620 case TARGET_NR_sigreturn
:
8621 if (block_signals()) {
8622 return -TARGET_ERESTARTSYS
;
8624 return do_sigreturn(cpu_env
);
8626 case TARGET_NR_rt_sigreturn
:
8627 if (block_signals()) {
8628 return -TARGET_ERESTARTSYS
;
8630 return do_rt_sigreturn(cpu_env
);
8631 case TARGET_NR_sethostname
:
8632 if (!(p
= lock_user_string(arg1
)))
8633 return -TARGET_EFAULT
;
8634 ret
= get_errno(sethostname(p
, arg2
));
8635 unlock_user(p
, arg1
, 0);
8637 #ifdef TARGET_NR_setrlimit
8638 case TARGET_NR_setrlimit
:
8640 int resource
= target_to_host_resource(arg1
);
8641 struct target_rlimit
*target_rlim
;
8643 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
8644 return -TARGET_EFAULT
;
8645 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
8646 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
8647 unlock_user_struct(target_rlim
, arg2
, 0);
8649 * If we just passed through resource limit settings for memory then
8650 * they would also apply to QEMU's own allocations, and QEMU will
8651 * crash or hang or die if its allocations fail. Ideally we would
8652 * track the guest allocations in QEMU and apply the limits ourselves.
8653 * For now, just tell the guest the call succeeded but don't actually
8656 if (resource
!= RLIMIT_AS
&&
8657 resource
!= RLIMIT_DATA
&&
8658 resource
!= RLIMIT_STACK
) {
8659 return get_errno(setrlimit(resource
, &rlim
));
8665 #ifdef TARGET_NR_getrlimit
8666 case TARGET_NR_getrlimit
:
8668 int resource
= target_to_host_resource(arg1
);
8669 struct target_rlimit
*target_rlim
;
8672 ret
= get_errno(getrlimit(resource
, &rlim
));
8673 if (!is_error(ret
)) {
8674 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
8675 return -TARGET_EFAULT
;
8676 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
8677 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
8678 unlock_user_struct(target_rlim
, arg2
, 1);
8683 case TARGET_NR_getrusage
:
8685 struct rusage rusage
;
8686 ret
= get_errno(getrusage(arg1
, &rusage
));
8687 if (!is_error(ret
)) {
8688 ret
= host_to_target_rusage(arg2
, &rusage
);
8692 #if defined(TARGET_NR_gettimeofday)
8693 case TARGET_NR_gettimeofday
:
8696 ret
= get_errno(gettimeofday(&tv
, NULL
));
8697 if (!is_error(ret
)) {
8698 if (copy_to_user_timeval(arg1
, &tv
))
8699 return -TARGET_EFAULT
;
8704 #if defined(TARGET_NR_settimeofday)
8705 case TARGET_NR_settimeofday
:
8707 struct timeval tv
, *ptv
= NULL
;
8708 struct timezone tz
, *ptz
= NULL
;
8711 if (copy_from_user_timeval(&tv
, arg1
)) {
8712 return -TARGET_EFAULT
;
8718 if (copy_from_user_timezone(&tz
, arg2
)) {
8719 return -TARGET_EFAULT
;
8724 return get_errno(settimeofday(ptv
, ptz
));
8727 #if defined(TARGET_NR_select)
8728 case TARGET_NR_select
:
8729 #if defined(TARGET_WANT_NI_OLD_SELECT)
8730 /* some architectures used to have old_select here
8731 * but now ENOSYS it.
8733 ret
= -TARGET_ENOSYS
;
8734 #elif defined(TARGET_WANT_OLD_SYS_SELECT)
8735 ret
= do_old_select(arg1
);
8737 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
8741 #ifdef TARGET_NR_pselect6
8742 case TARGET_NR_pselect6
:
8744 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
8745 fd_set rfds
, wfds
, efds
;
8746 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
8747 struct timespec ts
, *ts_ptr
;
8750 * The 6th arg is actually two args smashed together,
8751 * so we cannot use the C library.
8759 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
8760 target_sigset_t
*target_sigset
;
8768 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
8772 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
8776 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
8782 * This takes a timespec, and not a timeval, so we cannot
8783 * use the do_select() helper ...
8786 if (target_to_host_timespec(&ts
, ts_addr
)) {
8787 return -TARGET_EFAULT
;
8794 /* Extract the two packed args for the sigset */
8797 sig
.size
= SIGSET_T_SIZE
;
8799 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
8801 return -TARGET_EFAULT
;
8803 arg_sigset
= tswapal(arg7
[0]);
8804 arg_sigsize
= tswapal(arg7
[1]);
8805 unlock_user(arg7
, arg6
, 0);
8809 if (arg_sigsize
!= sizeof(*target_sigset
)) {
8810 /* Like the kernel, we enforce correct size sigsets */
8811 return -TARGET_EINVAL
;
8813 target_sigset
= lock_user(VERIFY_READ
, arg_sigset
,
8814 sizeof(*target_sigset
), 1);
8815 if (!target_sigset
) {
8816 return -TARGET_EFAULT
;
8818 target_to_host_sigset(&set
, target_sigset
);
8819 unlock_user(target_sigset
, arg_sigset
, 0);
8827 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
8830 if (!is_error(ret
)) {
8831 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
8832 return -TARGET_EFAULT
;
8833 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
8834 return -TARGET_EFAULT
;
8835 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
8836 return -TARGET_EFAULT
;
8838 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
))
8839 return -TARGET_EFAULT
;
8844 #ifdef TARGET_NR_symlink
8845 case TARGET_NR_symlink
:
8848 p
= lock_user_string(arg1
);
8849 p2
= lock_user_string(arg2
);
8851 ret
= -TARGET_EFAULT
;
8853 ret
= get_errno(symlink(p
, p2
));
8854 unlock_user(p2
, arg2
, 0);
8855 unlock_user(p
, arg1
, 0);
8859 #if defined(TARGET_NR_symlinkat)
8860 case TARGET_NR_symlinkat
:
8863 p
= lock_user_string(arg1
);
8864 p2
= lock_user_string(arg3
);
8866 ret
= -TARGET_EFAULT
;
8868 ret
= get_errno(symlinkat(p
, arg2
, p2
));
8869 unlock_user(p2
, arg3
, 0);
8870 unlock_user(p
, arg1
, 0);
8874 #ifdef TARGET_NR_readlink
8875 case TARGET_NR_readlink
:
8878 p
= lock_user_string(arg1
);
8879 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
8881 ret
= -TARGET_EFAULT
;
8883 /* Short circuit this for the magic exe check. */
8884 ret
= -TARGET_EINVAL
;
8885 } else if (is_proc_myself((const char *)p
, "exe")) {
8886 char real
[PATH_MAX
], *temp
;
8887 temp
= realpath(exec_path
, real
);
8888 /* Return value is # of bytes that we wrote to the buffer. */
8890 ret
= get_errno(-1);
8892 /* Don't worry about sign mismatch as earlier mapping
8893 * logic would have thrown a bad address error. */
8894 ret
= MIN(strlen(real
), arg3
);
8895 /* We cannot NUL terminate the string. */
8896 memcpy(p2
, real
, ret
);
8899 ret
= get_errno(readlink(path(p
), p2
, arg3
));
8901 unlock_user(p2
, arg2
, ret
);
8902 unlock_user(p
, arg1
, 0);
8906 #if defined(TARGET_NR_readlinkat)
8907 case TARGET_NR_readlinkat
:
8910 p
= lock_user_string(arg2
);
8911 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
8913 ret
= -TARGET_EFAULT
;
8914 } else if (is_proc_myself((const char *)p
, "exe")) {
8915 char real
[PATH_MAX
], *temp
;
8916 temp
= realpath(exec_path
, real
);
8917 ret
= temp
== NULL
? get_errno(-1) : strlen(real
) ;
8918 snprintf((char *)p2
, arg4
, "%s", real
);
8920 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
8922 unlock_user(p2
, arg3
, ret
);
8923 unlock_user(p
, arg2
, 0);
8927 #ifdef TARGET_NR_swapon
8928 case TARGET_NR_swapon
:
8929 if (!(p
= lock_user_string(arg1
)))
8930 return -TARGET_EFAULT
;
8931 ret
= get_errno(swapon(p
, arg2
));
8932 unlock_user(p
, arg1
, 0);
8935 case TARGET_NR_reboot
:
8936 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
8937 /* arg4 must be ignored in all other cases */
8938 p
= lock_user_string(arg4
);
8940 return -TARGET_EFAULT
;
8942 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
8943 unlock_user(p
, arg4
, 0);
8945 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
8948 #ifdef TARGET_NR_mmap
8949 case TARGET_NR_mmap
:
8950 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
8951 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
8952 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
8953 || defined(TARGET_S390X)
8956 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
8957 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
8958 return -TARGET_EFAULT
;
8965 unlock_user(v
, arg1
, 0);
8966 ret
= get_errno(target_mmap(v1
, v2
, v3
,
8967 target_to_host_bitmask(v4
, mmap_flags_tbl
),
8971 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
8972 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
8978 #ifdef TARGET_NR_mmap2
8979 case TARGET_NR_mmap2
:
8981 #define MMAP_SHIFT 12
8983 ret
= target_mmap(arg1
, arg2
, arg3
,
8984 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
8985 arg5
, arg6
<< MMAP_SHIFT
);
8986 return get_errno(ret
);
8988 case TARGET_NR_munmap
:
8989 return get_errno(target_munmap(arg1
, arg2
));
8990 case TARGET_NR_mprotect
:
8992 TaskState
*ts
= cpu
->opaque
;
8993 /* Special hack to detect libc making the stack executable. */
8994 if ((arg3
& PROT_GROWSDOWN
)
8995 && arg1
>= ts
->info
->stack_limit
8996 && arg1
<= ts
->info
->start_stack
) {
8997 arg3
&= ~PROT_GROWSDOWN
;
8998 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
8999 arg1
= ts
->info
->stack_limit
;
9002 return get_errno(target_mprotect(arg1
, arg2
, arg3
));
9003 #ifdef TARGET_NR_mremap
9004 case TARGET_NR_mremap
:
9005 return get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
9007 /* ??? msync/mlock/munlock are broken for softmmu. */
9008 #ifdef TARGET_NR_msync
9009 case TARGET_NR_msync
:
9010 return get_errno(msync(g2h(arg1
), arg2
, arg3
));
9012 #ifdef TARGET_NR_mlock
9013 case TARGET_NR_mlock
:
9014 return get_errno(mlock(g2h(arg1
), arg2
));
9016 #ifdef TARGET_NR_munlock
9017 case TARGET_NR_munlock
:
9018 return get_errno(munlock(g2h(arg1
), arg2
));
9020 #ifdef TARGET_NR_mlockall
9021 case TARGET_NR_mlockall
:
9022 return get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
9024 #ifdef TARGET_NR_munlockall
9025 case TARGET_NR_munlockall
:
9026 return get_errno(munlockall());
9028 #ifdef TARGET_NR_truncate
9029 case TARGET_NR_truncate
:
9030 if (!(p
= lock_user_string(arg1
)))
9031 return -TARGET_EFAULT
;
9032 ret
= get_errno(truncate(p
, arg2
));
9033 unlock_user(p
, arg1
, 0);
9036 #ifdef TARGET_NR_ftruncate
9037 case TARGET_NR_ftruncate
:
9038 return get_errno(ftruncate(arg1
, arg2
));
9040 case TARGET_NR_fchmod
:
9041 return get_errno(fchmod(arg1
, arg2
));
9042 #if defined(TARGET_NR_fchmodat)
9043 case TARGET_NR_fchmodat
:
9044 if (!(p
= lock_user_string(arg2
)))
9045 return -TARGET_EFAULT
;
9046 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
9047 unlock_user(p
, arg2
, 0);
9050 case TARGET_NR_getpriority
:
9051 /* Note that negative values are valid for getpriority, so we must
9052 differentiate based on errno settings. */
9054 ret
= getpriority(arg1
, arg2
);
9055 if (ret
== -1 && errno
!= 0) {
9056 return -host_to_target_errno(errno
);
9059 /* Return value is the unbiased priority. Signal no error. */
9060 ((CPUAlphaState
*)cpu_env
)->ir
[IR_V0
] = 0;
9062 /* Return value is a biased priority to avoid negative numbers. */
9066 case TARGET_NR_setpriority
:
9067 return get_errno(setpriority(arg1
, arg2
, arg3
));
9068 #ifdef TARGET_NR_statfs
9069 case TARGET_NR_statfs
:
9070 if (!(p
= lock_user_string(arg1
))) {
9071 return -TARGET_EFAULT
;
9073 ret
= get_errno(statfs(path(p
), &stfs
));
9074 unlock_user(p
, arg1
, 0);
9076 if (!is_error(ret
)) {
9077 struct target_statfs
*target_stfs
;
9079 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
9080 return -TARGET_EFAULT
;
9081 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
9082 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
9083 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
9084 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
9085 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
9086 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
9087 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
9088 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
9089 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
9090 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
9091 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
9092 #ifdef _STATFS_F_FLAGS
9093 __put_user(stfs
.f_flags
, &target_stfs
->f_flags
);
9095 __put_user(0, &target_stfs
->f_flags
);
9097 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
9098 unlock_user_struct(target_stfs
, arg2
, 1);
9102 #ifdef TARGET_NR_fstatfs
9103 case TARGET_NR_fstatfs
:
9104 ret
= get_errno(fstatfs(arg1
, &stfs
));
9105 goto convert_statfs
;
9107 #ifdef TARGET_NR_statfs64
9108 case TARGET_NR_statfs64
:
9109 if (!(p
= lock_user_string(arg1
))) {
9110 return -TARGET_EFAULT
;
9112 ret
= get_errno(statfs(path(p
), &stfs
));
9113 unlock_user(p
, arg1
, 0);
9115 if (!is_error(ret
)) {
9116 struct target_statfs64
*target_stfs
;
9118 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
9119 return -TARGET_EFAULT
;
9120 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
9121 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
9122 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
9123 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
9124 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
9125 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
9126 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
9127 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
9128 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
9129 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
9130 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
9131 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
9132 unlock_user_struct(target_stfs
, arg3
, 1);
9135 case TARGET_NR_fstatfs64
:
9136 ret
= get_errno(fstatfs(arg1
, &stfs
));
9137 goto convert_statfs64
;
9139 #ifdef TARGET_NR_socketcall
9140 case TARGET_NR_socketcall
:
9141 return do_socketcall(arg1
, arg2
);
9143 #ifdef TARGET_NR_accept
9144 case TARGET_NR_accept
:
9145 return do_accept4(arg1
, arg2
, arg3
, 0);
9147 #ifdef TARGET_NR_accept4
9148 case TARGET_NR_accept4
:
9149 return do_accept4(arg1
, arg2
, arg3
, arg4
);
9151 #ifdef TARGET_NR_bind
9152 case TARGET_NR_bind
:
9153 return do_bind(arg1
, arg2
, arg3
);
9155 #ifdef TARGET_NR_connect
9156 case TARGET_NR_connect
:
9157 return do_connect(arg1
, arg2
, arg3
);
9159 #ifdef TARGET_NR_getpeername
9160 case TARGET_NR_getpeername
:
9161 return do_getpeername(arg1
, arg2
, arg3
);
9163 #ifdef TARGET_NR_getsockname
9164 case TARGET_NR_getsockname
:
9165 return do_getsockname(arg1
, arg2
, arg3
);
9167 #ifdef TARGET_NR_getsockopt
9168 case TARGET_NR_getsockopt
:
9169 return do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
9171 #ifdef TARGET_NR_listen
9172 case TARGET_NR_listen
:
9173 return get_errno(listen(arg1
, arg2
));
9175 #ifdef TARGET_NR_recv
9176 case TARGET_NR_recv
:
9177 return do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
9179 #ifdef TARGET_NR_recvfrom
9180 case TARGET_NR_recvfrom
:
9181 return do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9183 #ifdef TARGET_NR_recvmsg
9184 case TARGET_NR_recvmsg
:
9185 return do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
9187 #ifdef TARGET_NR_send
9188 case TARGET_NR_send
:
9189 return do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
9191 #ifdef TARGET_NR_sendmsg
9192 case TARGET_NR_sendmsg
:
9193 return do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
9195 #ifdef TARGET_NR_sendmmsg
9196 case TARGET_NR_sendmmsg
:
9197 return do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
9199 #ifdef TARGET_NR_recvmmsg
9200 case TARGET_NR_recvmmsg
:
9201 return do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
9203 #ifdef TARGET_NR_sendto
9204 case TARGET_NR_sendto
:
9205 return do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9207 #ifdef TARGET_NR_shutdown
9208 case TARGET_NR_shutdown
:
9209 return get_errno(shutdown(arg1
, arg2
));
9211 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
9212 case TARGET_NR_getrandom
:
9213 p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
9215 return -TARGET_EFAULT
;
9217 ret
= get_errno(getrandom(p
, arg2
, arg3
));
9218 unlock_user(p
, arg1
, ret
);
9221 #ifdef TARGET_NR_socket
9222 case TARGET_NR_socket
:
9223 return do_socket(arg1
, arg2
, arg3
);
9225 #ifdef TARGET_NR_socketpair
9226 case TARGET_NR_socketpair
:
9227 return do_socketpair(arg1
, arg2
, arg3
, arg4
);
9229 #ifdef TARGET_NR_setsockopt
9230 case TARGET_NR_setsockopt
:
9231 return do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
9233 #if defined(TARGET_NR_syslog)
9234 case TARGET_NR_syslog
:
9239 case TARGET_SYSLOG_ACTION_CLOSE
: /* Close log */
9240 case TARGET_SYSLOG_ACTION_OPEN
: /* Open log */
9241 case TARGET_SYSLOG_ACTION_CLEAR
: /* Clear ring buffer */
9242 case TARGET_SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging */
9243 case TARGET_SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging */
9244 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL
: /* Set messages level */
9245 case TARGET_SYSLOG_ACTION_SIZE_UNREAD
: /* Number of chars */
9246 case TARGET_SYSLOG_ACTION_SIZE_BUFFER
: /* Size of the buffer */
9247 return get_errno(sys_syslog((int)arg1
, NULL
, (int)arg3
));
9248 case TARGET_SYSLOG_ACTION_READ
: /* Read from log */
9249 case TARGET_SYSLOG_ACTION_READ_CLEAR
: /* Read/clear msgs */
9250 case TARGET_SYSLOG_ACTION_READ_ALL
: /* Read last messages */
9253 return -TARGET_EINVAL
;
9258 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
9260 return -TARGET_EFAULT
;
9262 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
9263 unlock_user(p
, arg2
, arg3
);
9267 return -TARGET_EINVAL
;
9272 case TARGET_NR_setitimer
:
9274 struct itimerval value
, ovalue
, *pvalue
;
9278 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
9279 || copy_from_user_timeval(&pvalue
->it_value
,
9280 arg2
+ sizeof(struct target_timeval
)))
9281 return -TARGET_EFAULT
;
9285 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
9286 if (!is_error(ret
) && arg3
) {
9287 if (copy_to_user_timeval(arg3
,
9288 &ovalue
.it_interval
)
9289 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
9291 return -TARGET_EFAULT
;
9295 case TARGET_NR_getitimer
:
9297 struct itimerval value
;
9299 ret
= get_errno(getitimer(arg1
, &value
));
9300 if (!is_error(ret
) && arg2
) {
9301 if (copy_to_user_timeval(arg2
,
9303 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
9305 return -TARGET_EFAULT
;
9309 #ifdef TARGET_NR_stat
9310 case TARGET_NR_stat
:
9311 if (!(p
= lock_user_string(arg1
))) {
9312 return -TARGET_EFAULT
;
9314 ret
= get_errno(stat(path(p
), &st
));
9315 unlock_user(p
, arg1
, 0);
9318 #ifdef TARGET_NR_lstat
9319 case TARGET_NR_lstat
:
9320 if (!(p
= lock_user_string(arg1
))) {
9321 return -TARGET_EFAULT
;
9323 ret
= get_errno(lstat(path(p
), &st
));
9324 unlock_user(p
, arg1
, 0);
9327 #ifdef TARGET_NR_fstat
9328 case TARGET_NR_fstat
:
9330 ret
= get_errno(fstat(arg1
, &st
));
9331 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
9334 if (!is_error(ret
)) {
9335 struct target_stat
*target_st
;
9337 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
9338 return -TARGET_EFAULT
;
9339 memset(target_st
, 0, sizeof(*target_st
));
9340 __put_user(st
.st_dev
, &target_st
->st_dev
);
9341 __put_user(st
.st_ino
, &target_st
->st_ino
);
9342 __put_user(st
.st_mode
, &target_st
->st_mode
);
9343 __put_user(st
.st_uid
, &target_st
->st_uid
);
9344 __put_user(st
.st_gid
, &target_st
->st_gid
);
9345 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
9346 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
9347 __put_user(st
.st_size
, &target_st
->st_size
);
9348 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
9349 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
9350 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
9351 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
9352 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
9353 #if (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) && \
9354 defined(TARGET_STAT_HAVE_NSEC)
9355 __put_user(st
.st_atim
.tv_nsec
,
9356 &target_st
->target_st_atime_nsec
);
9357 __put_user(st
.st_mtim
.tv_nsec
,
9358 &target_st
->target_st_mtime_nsec
);
9359 __put_user(st
.st_ctim
.tv_nsec
,
9360 &target_st
->target_st_ctime_nsec
);
9362 unlock_user_struct(target_st
, arg2
, 1);
9367 case TARGET_NR_vhangup
:
9368 return get_errno(vhangup());
9369 #ifdef TARGET_NR_syscall
9370 case TARGET_NR_syscall
:
9371 return do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
9372 arg6
, arg7
, arg8
, 0);
9374 #if defined(TARGET_NR_wait4)
9375 case TARGET_NR_wait4
:
9378 abi_long status_ptr
= arg2
;
9379 struct rusage rusage
, *rusage_ptr
;
9380 abi_ulong target_rusage
= arg4
;
9381 abi_long rusage_err
;
9383 rusage_ptr
= &rusage
;
9386 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, rusage_ptr
));
9387 if (!is_error(ret
)) {
9388 if (status_ptr
&& ret
) {
9389 status
= host_to_target_waitstatus(status
);
9390 if (put_user_s32(status
, status_ptr
))
9391 return -TARGET_EFAULT
;
9393 if (target_rusage
) {
9394 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
9403 #ifdef TARGET_NR_swapoff
9404 case TARGET_NR_swapoff
:
9405 if (!(p
= lock_user_string(arg1
)))
9406 return -TARGET_EFAULT
;
9407 ret
= get_errno(swapoff(p
));
9408 unlock_user(p
, arg1
, 0);
9411 case TARGET_NR_sysinfo
:
9413 struct target_sysinfo
*target_value
;
9414 struct sysinfo value
;
9415 ret
= get_errno(sysinfo(&value
));
9416 if (!is_error(ret
) && arg1
)
9418 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
9419 return -TARGET_EFAULT
;
9420 __put_user(value
.uptime
, &target_value
->uptime
);
9421 __put_user(value
.loads
[0], &target_value
->loads
[0]);
9422 __put_user(value
.loads
[1], &target_value
->loads
[1]);
9423 __put_user(value
.loads
[2], &target_value
->loads
[2]);
9424 __put_user(value
.totalram
, &target_value
->totalram
);
9425 __put_user(value
.freeram
, &target_value
->freeram
);
9426 __put_user(value
.sharedram
, &target_value
->sharedram
);
9427 __put_user(value
.bufferram
, &target_value
->bufferram
);
9428 __put_user(value
.totalswap
, &target_value
->totalswap
);
9429 __put_user(value
.freeswap
, &target_value
->freeswap
);
9430 __put_user(value
.procs
, &target_value
->procs
);
9431 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
9432 __put_user(value
.freehigh
, &target_value
->freehigh
);
9433 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
9434 unlock_user_struct(target_value
, arg1
, 1);
9438 #ifdef TARGET_NR_ipc
9440 return do_ipc(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
9442 #ifdef TARGET_NR_semget
9443 case TARGET_NR_semget
:
9444 return get_errno(semget(arg1
, arg2
, arg3
));
9446 #ifdef TARGET_NR_semop
9447 case TARGET_NR_semop
:
9448 return do_semop(arg1
, arg2
, arg3
);
9450 #ifdef TARGET_NR_semctl
9451 case TARGET_NR_semctl
:
9452 return do_semctl(arg1
, arg2
, arg3
, arg4
);
9454 #ifdef TARGET_NR_msgctl
9455 case TARGET_NR_msgctl
:
9456 return do_msgctl(arg1
, arg2
, arg3
);
9458 #ifdef TARGET_NR_msgget
9459 case TARGET_NR_msgget
:
9460 return get_errno(msgget(arg1
, arg2
));
9462 #ifdef TARGET_NR_msgrcv
9463 case TARGET_NR_msgrcv
:
9464 return do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
9466 #ifdef TARGET_NR_msgsnd
9467 case TARGET_NR_msgsnd
:
9468 return do_msgsnd(arg1
, arg2
, arg3
, arg4
);
9470 #ifdef TARGET_NR_shmget
9471 case TARGET_NR_shmget
:
9472 return get_errno(shmget(arg1
, arg2
, arg3
));
9474 #ifdef TARGET_NR_shmctl
9475 case TARGET_NR_shmctl
:
9476 return do_shmctl(arg1
, arg2
, arg3
);
9478 #ifdef TARGET_NR_shmat
9479 case TARGET_NR_shmat
:
9480 return do_shmat(cpu_env
, arg1
, arg2
, arg3
);
9482 #ifdef TARGET_NR_shmdt
9483 case TARGET_NR_shmdt
:
9484 return do_shmdt(arg1
);
9486 case TARGET_NR_fsync
:
9487 return get_errno(fsync(arg1
));
9488 case TARGET_NR_clone
:
9489 /* Linux manages to have three different orderings for its
9490 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
9491 * match the kernel's CONFIG_CLONE_* settings.
9492 * Microblaze is further special in that it uses a sixth
9493 * implicit argument to clone for the TLS pointer.
9495 #if defined(TARGET_MICROBLAZE)
9496 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
9497 #elif defined(TARGET_CLONE_BACKWARDS)
9498 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
9499 #elif defined(TARGET_CLONE_BACKWARDS2)
9500 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
9502 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
9505 #ifdef __NR_exit_group
9506 /* new thread calls */
9507 case TARGET_NR_exit_group
:
9508 preexit_cleanup(cpu_env
, arg1
);
9509 return get_errno(exit_group(arg1
));
9511 case TARGET_NR_setdomainname
:
9512 if (!(p
= lock_user_string(arg1
)))
9513 return -TARGET_EFAULT
;
9514 ret
= get_errno(setdomainname(p
, arg2
));
9515 unlock_user(p
, arg1
, 0);
9517 case TARGET_NR_uname
:
9518 /* no need to transcode because we use the linux syscall */
9520 struct new_utsname
* buf
;
9522 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
9523 return -TARGET_EFAULT
;
9524 ret
= get_errno(sys_uname(buf
));
9525 if (!is_error(ret
)) {
9526 /* Overwrite the native machine name with whatever is being
9528 g_strlcpy(buf
->machine
, cpu_to_uname_machine(cpu_env
),
9529 sizeof(buf
->machine
));
9530 /* Allow the user to override the reported release. */
9531 if (qemu_uname_release
&& *qemu_uname_release
) {
9532 g_strlcpy(buf
->release
, qemu_uname_release
,
9533 sizeof(buf
->release
));
9536 unlock_user_struct(buf
, arg1
, 1);
9540 case TARGET_NR_modify_ldt
:
9541 return do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
9542 #if !defined(TARGET_X86_64)
9543 case TARGET_NR_vm86
:
9544 return do_vm86(cpu_env
, arg1
, arg2
);
9547 #if defined(TARGET_NR_adjtimex)
9548 case TARGET_NR_adjtimex
:
9550 struct timex host_buf
;
9552 if (target_to_host_timex(&host_buf
, arg1
) != 0) {
9553 return -TARGET_EFAULT
;
9555 ret
= get_errno(adjtimex(&host_buf
));
9556 if (!is_error(ret
)) {
9557 if (host_to_target_timex(arg1
, &host_buf
) != 0) {
9558 return -TARGET_EFAULT
;
9564 #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
9565 case TARGET_NR_clock_adjtime
:
9567 struct timex htx
, *phtx
= &htx
;
9569 if (target_to_host_timex(phtx
, arg2
) != 0) {
9570 return -TARGET_EFAULT
;
9572 ret
= get_errno(clock_adjtime(arg1
, phtx
));
9573 if (!is_error(ret
) && phtx
) {
9574 if (host_to_target_timex(arg2
, phtx
) != 0) {
9575 return -TARGET_EFAULT
;
9581 case TARGET_NR_getpgid
:
9582 return get_errno(getpgid(arg1
));
9583 case TARGET_NR_fchdir
:
9584 return get_errno(fchdir(arg1
));
9585 case TARGET_NR_personality
:
9586 return get_errno(personality(arg1
));
9587 #ifdef TARGET_NR__llseek /* Not on alpha */
9588 case TARGET_NR__llseek
:
9591 #if !defined(__NR_llseek)
9592 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | (abi_ulong
)arg3
, arg5
);
9594 ret
= get_errno(res
);
9599 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
9601 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
9602 return -TARGET_EFAULT
;
9607 #ifdef TARGET_NR_getdents
9608 case TARGET_NR_getdents
:
9609 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
9610 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
9612 struct target_dirent
*target_dirp
;
9613 struct linux_dirent
*dirp
;
9614 abi_long count
= arg3
;
9616 dirp
= g_try_malloc(count
);
9618 return -TARGET_ENOMEM
;
9621 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
9622 if (!is_error(ret
)) {
9623 struct linux_dirent
*de
;
9624 struct target_dirent
*tde
;
9626 int reclen
, treclen
;
9627 int count1
, tnamelen
;
9631 if (!(target_dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
9632 return -TARGET_EFAULT
;
9635 reclen
= de
->d_reclen
;
9636 tnamelen
= reclen
- offsetof(struct linux_dirent
, d_name
);
9637 assert(tnamelen
>= 0);
9638 treclen
= tnamelen
+ offsetof(struct target_dirent
, d_name
);
9639 assert(count1
+ treclen
<= count
);
9640 tde
->d_reclen
= tswap16(treclen
);
9641 tde
->d_ino
= tswapal(de
->d_ino
);
9642 tde
->d_off
= tswapal(de
->d_off
);
9643 memcpy(tde
->d_name
, de
->d_name
, tnamelen
);
9644 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
9646 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
9650 unlock_user(target_dirp
, arg2
, ret
);
9656 struct linux_dirent
*dirp
;
9657 abi_long count
= arg3
;
9659 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
9660 return -TARGET_EFAULT
;
9661 ret
= get_errno(sys_getdents(arg1
, dirp
, count
));
9662 if (!is_error(ret
)) {
9663 struct linux_dirent
*de
;
9668 reclen
= de
->d_reclen
;
9671 de
->d_reclen
= tswap16(reclen
);
9672 tswapls(&de
->d_ino
);
9673 tswapls(&de
->d_off
);
9674 de
= (struct linux_dirent
*)((char *)de
+ reclen
);
9678 unlock_user(dirp
, arg2
, ret
);
9682 /* Implement getdents in terms of getdents64 */
9684 struct linux_dirent64
*dirp
;
9685 abi_long count
= arg3
;
9687 dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
9689 return -TARGET_EFAULT
;
9691 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
9692 if (!is_error(ret
)) {
9693 /* Convert the dirent64 structs to target dirent. We do this
9694 * in-place, since we can guarantee that a target_dirent is no
9695 * larger than a dirent64; however this means we have to be
9696 * careful to read everything before writing in the new format.
9698 struct linux_dirent64
*de
;
9699 struct target_dirent
*tde
;
9704 tde
= (struct target_dirent
*)dirp
;
9706 int namelen
, treclen
;
9707 int reclen
= de
->d_reclen
;
9708 uint64_t ino
= de
->d_ino
;
9709 int64_t off
= de
->d_off
;
9710 uint8_t type
= de
->d_type
;
9712 namelen
= strlen(de
->d_name
);
9713 treclen
= offsetof(struct target_dirent
, d_name
)
9715 treclen
= QEMU_ALIGN_UP(treclen
, sizeof(abi_long
));
9717 memmove(tde
->d_name
, de
->d_name
, namelen
+ 1);
9718 tde
->d_ino
= tswapal(ino
);
9719 tde
->d_off
= tswapal(off
);
9720 tde
->d_reclen
= tswap16(treclen
);
9721 /* The target_dirent type is in what was formerly a padding
9722 * byte at the end of the structure:
9724 *(((char *)tde
) + treclen
- 1) = type
;
9726 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
9727 tde
= (struct target_dirent
*)((char *)tde
+ treclen
);
9733 unlock_user(dirp
, arg2
, ret
);
9737 #endif /* TARGET_NR_getdents */
9738 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
9739 case TARGET_NR_getdents64
:
9741 struct linux_dirent64
*dirp
;
9742 abi_long count
= arg3
;
9743 if (!(dirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0)))
9744 return -TARGET_EFAULT
;
9745 ret
= get_errno(sys_getdents64(arg1
, dirp
, count
));
9746 if (!is_error(ret
)) {
9747 struct linux_dirent64
*de
;
9752 reclen
= de
->d_reclen
;
9755 de
->d_reclen
= tswap16(reclen
);
9756 tswap64s((uint64_t *)&de
->d_ino
);
9757 tswap64s((uint64_t *)&de
->d_off
);
9758 de
= (struct linux_dirent64
*)((char *)de
+ reclen
);
9762 unlock_user(dirp
, arg2
, ret
);
9765 #endif /* TARGET_NR_getdents64 */
9766 #if defined(TARGET_NR__newselect)
9767 case TARGET_NR__newselect
:
9768 return do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
9770 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
9771 # ifdef TARGET_NR_poll
9772 case TARGET_NR_poll
:
9774 # ifdef TARGET_NR_ppoll
9775 case TARGET_NR_ppoll
:
9778 struct target_pollfd
*target_pfd
;
9779 unsigned int nfds
= arg2
;
9786 if (nfds
> (INT_MAX
/ sizeof(struct target_pollfd
))) {
9787 return -TARGET_EINVAL
;
9790 target_pfd
= lock_user(VERIFY_WRITE
, arg1
,
9791 sizeof(struct target_pollfd
) * nfds
, 1);
9793 return -TARGET_EFAULT
;
9796 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
9797 for (i
= 0; i
< nfds
; i
++) {
9798 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
9799 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
9804 # ifdef TARGET_NR_ppoll
9805 case TARGET_NR_ppoll
:
9807 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
9808 target_sigset_t
*target_set
;
9809 sigset_t _set
, *set
= &_set
;
9812 if (target_to_host_timespec(timeout_ts
, arg3
)) {
9813 unlock_user(target_pfd
, arg1
, 0);
9814 return -TARGET_EFAULT
;
9821 if (arg5
!= sizeof(target_sigset_t
)) {
9822 unlock_user(target_pfd
, arg1
, 0);
9823 return -TARGET_EINVAL
;
9826 target_set
= lock_user(VERIFY_READ
, arg4
, sizeof(target_sigset_t
), 1);
9828 unlock_user(target_pfd
, arg1
, 0);
9829 return -TARGET_EFAULT
;
9831 target_to_host_sigset(set
, target_set
);
9836 ret
= get_errno(safe_ppoll(pfd
, nfds
, timeout_ts
,
9837 set
, SIGSET_T_SIZE
));
9839 if (!is_error(ret
) && arg3
) {
9840 host_to_target_timespec(arg3
, timeout_ts
);
9843 unlock_user(target_set
, arg4
, 0);
9848 # ifdef TARGET_NR_poll
9849 case TARGET_NR_poll
:
9851 struct timespec ts
, *pts
;
9854 /* Convert ms to secs, ns */
9855 ts
.tv_sec
= arg3
/ 1000;
9856 ts
.tv_nsec
= (arg3
% 1000) * 1000000LL;
9859 /* -ve poll() timeout means "infinite" */
9862 ret
= get_errno(safe_ppoll(pfd
, nfds
, pts
, NULL
, 0));
9867 g_assert_not_reached();
9870 if (!is_error(ret
)) {
9871 for(i
= 0; i
< nfds
; i
++) {
9872 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
9875 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
9879 case TARGET_NR_flock
:
9880 /* NOTE: the flock constant seems to be the same for every
9882 return get_errno(safe_flock(arg1
, arg2
));
9883 case TARGET_NR_readv
:
9885 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
9887 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
9888 unlock_iovec(vec
, arg2
, arg3
, 1);
9890 ret
= -host_to_target_errno(errno
);
9894 case TARGET_NR_writev
:
9896 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
9898 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
9899 unlock_iovec(vec
, arg2
, arg3
, 0);
9901 ret
= -host_to_target_errno(errno
);
9905 #if defined(TARGET_NR_preadv)
9906 case TARGET_NR_preadv
:
9908 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
9910 unsigned long low
, high
;
9912 target_to_host_low_high(arg4
, arg5
, &low
, &high
);
9913 ret
= get_errno(safe_preadv(arg1
, vec
, arg3
, low
, high
));
9914 unlock_iovec(vec
, arg2
, arg3
, 1);
9916 ret
= -host_to_target_errno(errno
);
9921 #if defined(TARGET_NR_pwritev)
9922 case TARGET_NR_pwritev
:
9924 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
9926 unsigned long low
, high
;
9928 target_to_host_low_high(arg4
, arg5
, &low
, &high
);
9929 ret
= get_errno(safe_pwritev(arg1
, vec
, arg3
, low
, high
));
9930 unlock_iovec(vec
, arg2
, arg3
, 0);
9932 ret
= -host_to_target_errno(errno
);
9937 case TARGET_NR_getsid
:
9938 return get_errno(getsid(arg1
));
9939 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
9940 case TARGET_NR_fdatasync
:
9941 return get_errno(fdatasync(arg1
));
9943 #ifdef TARGET_NR__sysctl
9944 case TARGET_NR__sysctl
:
9945 /* We don't implement this, but ENOTDIR is always a safe
9947 return -TARGET_ENOTDIR
;
9949 case TARGET_NR_sched_getaffinity
:
9951 unsigned int mask_size
;
9952 unsigned long *mask
;
9955 * sched_getaffinity needs multiples of ulong, so need to take
9956 * care of mismatches between target ulong and host ulong sizes.
9958 if (arg2
& (sizeof(abi_ulong
) - 1)) {
9959 return -TARGET_EINVAL
;
9961 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
9963 mask
= alloca(mask_size
);
9964 memset(mask
, 0, mask_size
);
9965 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
9967 if (!is_error(ret
)) {
9969 /* More data returned than the caller's buffer will fit.
9970 * This only happens if sizeof(abi_long) < sizeof(long)
9971 * and the caller passed us a buffer holding an odd number
9972 * of abi_longs. If the host kernel is actually using the
9973 * extra 4 bytes then fail EINVAL; otherwise we can just
9974 * ignore them and only copy the interesting part.
9976 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
9977 if (numcpus
> arg2
* 8) {
9978 return -TARGET_EINVAL
;
9983 if (host_to_target_cpu_mask(mask
, mask_size
, arg3
, ret
)) {
9984 return -TARGET_EFAULT
;
9989 case TARGET_NR_sched_setaffinity
:
9991 unsigned int mask_size
;
9992 unsigned long *mask
;
9995 * sched_setaffinity needs multiples of ulong, so need to take
9996 * care of mismatches between target ulong and host ulong sizes.
9998 if (arg2
& (sizeof(abi_ulong
) - 1)) {
9999 return -TARGET_EINVAL
;
10001 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
10002 mask
= alloca(mask_size
);
10004 ret
= target_to_host_cpu_mask(mask
, mask_size
, arg3
, arg2
);
10009 return get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
10011 case TARGET_NR_getcpu
:
10013 unsigned cpu
, node
;
10014 ret
= get_errno(sys_getcpu(arg1
? &cpu
: NULL
,
10015 arg2
? &node
: NULL
,
10017 if (is_error(ret
)) {
10020 if (arg1
&& put_user_u32(cpu
, arg1
)) {
10021 return -TARGET_EFAULT
;
10023 if (arg2
&& put_user_u32(node
, arg2
)) {
10024 return -TARGET_EFAULT
;
10028 case TARGET_NR_sched_setparam
:
10030 struct sched_param
*target_schp
;
10031 struct sched_param schp
;
10034 return -TARGET_EINVAL
;
10036 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1))
10037 return -TARGET_EFAULT
;
10038 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
10039 unlock_user_struct(target_schp
, arg2
, 0);
10040 return get_errno(sched_setparam(arg1
, &schp
));
10042 case TARGET_NR_sched_getparam
:
10044 struct sched_param
*target_schp
;
10045 struct sched_param schp
;
10048 return -TARGET_EINVAL
;
10050 ret
= get_errno(sched_getparam(arg1
, &schp
));
10051 if (!is_error(ret
)) {
10052 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0))
10053 return -TARGET_EFAULT
;
10054 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
10055 unlock_user_struct(target_schp
, arg2
, 1);
10059 case TARGET_NR_sched_setscheduler
:
10061 struct sched_param
*target_schp
;
10062 struct sched_param schp
;
10064 return -TARGET_EINVAL
;
10066 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1))
10067 return -TARGET_EFAULT
;
10068 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
10069 unlock_user_struct(target_schp
, arg3
, 0);
10070 return get_errno(sched_setscheduler(arg1
, arg2
, &schp
));
10072 case TARGET_NR_sched_getscheduler
:
10073 return get_errno(sched_getscheduler(arg1
));
10074 case TARGET_NR_sched_yield
:
10075 return get_errno(sched_yield());
10076 case TARGET_NR_sched_get_priority_max
:
10077 return get_errno(sched_get_priority_max(arg1
));
10078 case TARGET_NR_sched_get_priority_min
:
10079 return get_errno(sched_get_priority_min(arg1
));
10080 #ifdef TARGET_NR_sched_rr_get_interval
10081 case TARGET_NR_sched_rr_get_interval
:
10083 struct timespec ts
;
10084 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
10085 if (!is_error(ret
)) {
10086 ret
= host_to_target_timespec(arg2
, &ts
);
10091 #if defined(TARGET_NR_nanosleep)
10092 case TARGET_NR_nanosleep
:
10094 struct timespec req
, rem
;
10095 target_to_host_timespec(&req
, arg1
);
10096 ret
= get_errno(safe_nanosleep(&req
, &rem
));
10097 if (is_error(ret
) && arg2
) {
10098 host_to_target_timespec(arg2
, &rem
);
10103 case TARGET_NR_prctl
:
10105 case PR_GET_PDEATHSIG
:
10108 ret
= get_errno(prctl(arg1
, &deathsig
, arg3
, arg4
, arg5
));
10109 if (!is_error(ret
) && arg2
10110 && put_user_ual(deathsig
, arg2
)) {
10111 return -TARGET_EFAULT
;
10118 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
10120 return -TARGET_EFAULT
;
10122 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
10123 arg3
, arg4
, arg5
));
10124 unlock_user(name
, arg2
, 16);
10129 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
10131 return -TARGET_EFAULT
;
10133 ret
= get_errno(prctl(arg1
, (unsigned long)name
,
10134 arg3
, arg4
, arg5
));
10135 unlock_user(name
, arg2
, 0);
10140 case TARGET_PR_GET_FP_MODE
:
10142 CPUMIPSState
*env
= ((CPUMIPSState
*)cpu_env
);
10144 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
10145 ret
|= TARGET_PR_FP_MODE_FR
;
10147 if (env
->CP0_Config5
& (1 << CP0C5_FRE
)) {
10148 ret
|= TARGET_PR_FP_MODE_FRE
;
10152 case TARGET_PR_SET_FP_MODE
:
10154 CPUMIPSState
*env
= ((CPUMIPSState
*)cpu_env
);
10155 bool old_fr
= env
->CP0_Status
& (1 << CP0St_FR
);
10156 bool old_fre
= env
->CP0_Config5
& (1 << CP0C5_FRE
);
10157 bool new_fr
= arg2
& TARGET_PR_FP_MODE_FR
;
10158 bool new_fre
= arg2
& TARGET_PR_FP_MODE_FRE
;
10160 const unsigned int known_bits
= TARGET_PR_FP_MODE_FR
|
10161 TARGET_PR_FP_MODE_FRE
;
10163 /* If nothing to change, return right away, successfully. */
10164 if (old_fr
== new_fr
&& old_fre
== new_fre
) {
10167 /* Check the value is valid */
10168 if (arg2
& ~known_bits
) {
10169 return -TARGET_EOPNOTSUPP
;
10171 /* Setting FRE without FR is not supported. */
10172 if (new_fre
&& !new_fr
) {
10173 return -TARGET_EOPNOTSUPP
;
10175 if (new_fr
&& !(env
->active_fpu
.fcr0
& (1 << FCR0_F64
))) {
10176 /* FR1 is not supported */
10177 return -TARGET_EOPNOTSUPP
;
10179 if (!new_fr
&& (env
->active_fpu
.fcr0
& (1 << FCR0_F64
))
10180 && !(env
->CP0_Status_rw_bitmask
& (1 << CP0St_FR
))) {
10181 /* cannot set FR=0 */
10182 return -TARGET_EOPNOTSUPP
;
10184 if (new_fre
&& !(env
->active_fpu
.fcr0
& (1 << FCR0_FREP
))) {
10185 /* Cannot set FRE=1 */
10186 return -TARGET_EOPNOTSUPP
;
10190 fpr_t
*fpr
= env
->active_fpu
.fpr
;
10191 for (i
= 0; i
< 32 ; i
+= 2) {
10192 if (!old_fr
&& new_fr
) {
10193 fpr
[i
].w
[!FP_ENDIAN_IDX
] = fpr
[i
+ 1].w
[FP_ENDIAN_IDX
];
10194 } else if (old_fr
&& !new_fr
) {
10195 fpr
[i
+ 1].w
[FP_ENDIAN_IDX
] = fpr
[i
].w
[!FP_ENDIAN_IDX
];
10200 env
->CP0_Status
|= (1 << CP0St_FR
);
10201 env
->hflags
|= MIPS_HFLAG_F64
;
10203 env
->CP0_Status
&= ~(1 << CP0St_FR
);
10204 env
->hflags
&= ~MIPS_HFLAG_F64
;
10207 env
->CP0_Config5
|= (1 << CP0C5_FRE
);
10208 if (env
->active_fpu
.fcr0
& (1 << FCR0_FREP
)) {
10209 env
->hflags
|= MIPS_HFLAG_FRE
;
10212 env
->CP0_Config5
&= ~(1 << CP0C5_FRE
);
10213 env
->hflags
&= ~MIPS_HFLAG_FRE
;
10219 #ifdef TARGET_AARCH64
10220 case TARGET_PR_SVE_SET_VL
:
10222 * We cannot support either PR_SVE_SET_VL_ONEXEC or
10223 * PR_SVE_VL_INHERIT. Note the kernel definition
10224 * of sve_vl_valid allows for VQ=512, i.e. VL=8192,
10225 * even though the current architectural maximum is VQ=16.
10227 ret
= -TARGET_EINVAL
;
10228 if (cpu_isar_feature(aa64_sve
, env_archcpu(cpu_env
))
10229 && arg2
>= 0 && arg2
<= 512 * 16 && !(arg2
& 15)) {
10230 CPUARMState
*env
= cpu_env
;
10231 ARMCPU
*cpu
= env_archcpu(env
);
10232 uint32_t vq
, old_vq
;
10234 old_vq
= (env
->vfp
.zcr_el
[1] & 0xf) + 1;
10235 vq
= MAX(arg2
/ 16, 1);
10236 vq
= MIN(vq
, cpu
->sve_max_vq
);
10239 aarch64_sve_narrow_vq(env
, vq
);
10241 env
->vfp
.zcr_el
[1] = vq
- 1;
10242 arm_rebuild_hflags(env
);
10246 case TARGET_PR_SVE_GET_VL
:
10247 ret
= -TARGET_EINVAL
;
10249 ARMCPU
*cpu
= env_archcpu(cpu_env
);
10250 if (cpu_isar_feature(aa64_sve
, cpu
)) {
10251 ret
= ((cpu
->env
.vfp
.zcr_el
[1] & 0xf) + 1) * 16;
10255 case TARGET_PR_PAC_RESET_KEYS
:
10257 CPUARMState
*env
= cpu_env
;
10258 ARMCPU
*cpu
= env_archcpu(env
);
10260 if (arg3
|| arg4
|| arg5
) {
10261 return -TARGET_EINVAL
;
10263 if (cpu_isar_feature(aa64_pauth
, cpu
)) {
10264 int all
= (TARGET_PR_PAC_APIAKEY
| TARGET_PR_PAC_APIBKEY
|
10265 TARGET_PR_PAC_APDAKEY
| TARGET_PR_PAC_APDBKEY
|
10266 TARGET_PR_PAC_APGAKEY
);
10272 } else if (arg2
& ~all
) {
10273 return -TARGET_EINVAL
;
10275 if (arg2
& TARGET_PR_PAC_APIAKEY
) {
10276 ret
|= qemu_guest_getrandom(&env
->keys
.apia
,
10277 sizeof(ARMPACKey
), &err
);
10279 if (arg2
& TARGET_PR_PAC_APIBKEY
) {
10280 ret
|= qemu_guest_getrandom(&env
->keys
.apib
,
10281 sizeof(ARMPACKey
), &err
);
10283 if (arg2
& TARGET_PR_PAC_APDAKEY
) {
10284 ret
|= qemu_guest_getrandom(&env
->keys
.apda
,
10285 sizeof(ARMPACKey
), &err
);
10287 if (arg2
& TARGET_PR_PAC_APDBKEY
) {
10288 ret
|= qemu_guest_getrandom(&env
->keys
.apdb
,
10289 sizeof(ARMPACKey
), &err
);
10291 if (arg2
& TARGET_PR_PAC_APGAKEY
) {
10292 ret
|= qemu_guest_getrandom(&env
->keys
.apga
,
10293 sizeof(ARMPACKey
), &err
);
10297 * Some unknown failure in the crypto. The best
10298 * we can do is log it and fail the syscall.
10299 * The real syscall cannot fail this way.
10301 qemu_log_mask(LOG_UNIMP
,
10302 "PR_PAC_RESET_KEYS: Crypto failure: %s",
10303 error_get_pretty(err
));
10305 return -TARGET_EIO
;
10310 return -TARGET_EINVAL
;
10311 #endif /* AARCH64 */
10312 case PR_GET_SECCOMP
:
10313 case PR_SET_SECCOMP
:
10314 /* Disable seccomp to prevent the target disabling syscalls we
10316 return -TARGET_EINVAL
;
10318 /* Most prctl options have no pointer arguments */
10319 return get_errno(prctl(arg1
, arg2
, arg3
, arg4
, arg5
));
10322 #ifdef TARGET_NR_arch_prctl
10323 case TARGET_NR_arch_prctl
:
10324 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
10325 return do_arch_prctl(cpu_env
, arg1
, arg2
);
10330 #ifdef TARGET_NR_pread64
10331 case TARGET_NR_pread64
:
10332 if (regpairs_aligned(cpu_env
, num
)) {
10336 if (arg2
== 0 && arg3
== 0) {
10337 /* Special-case NULL buffer and zero length, which should succeed */
10340 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10342 return -TARGET_EFAULT
;
10345 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
10346 unlock_user(p
, arg2
, ret
);
10348 case TARGET_NR_pwrite64
:
10349 if (regpairs_aligned(cpu_env
, num
)) {
10353 if (arg2
== 0 && arg3
== 0) {
10354 /* Special-case NULL buffer and zero length, which should succeed */
10357 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
10359 return -TARGET_EFAULT
;
10362 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
10363 unlock_user(p
, arg2
, 0);
10366 case TARGET_NR_getcwd
:
10367 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
10368 return -TARGET_EFAULT
;
10369 ret
= get_errno(sys_getcwd1(p
, arg2
));
10370 unlock_user(p
, arg1
, ret
);
10372 case TARGET_NR_capget
:
10373 case TARGET_NR_capset
:
10375 struct target_user_cap_header
*target_header
;
10376 struct target_user_cap_data
*target_data
= NULL
;
10377 struct __user_cap_header_struct header
;
10378 struct __user_cap_data_struct data
[2];
10379 struct __user_cap_data_struct
*dataptr
= NULL
;
10380 int i
, target_datalen
;
10381 int data_items
= 1;
10383 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
10384 return -TARGET_EFAULT
;
10386 header
.version
= tswap32(target_header
->version
);
10387 header
.pid
= tswap32(target_header
->pid
);
10389 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
10390 /* Version 2 and up takes pointer to two user_data structs */
10394 target_datalen
= sizeof(*target_data
) * data_items
;
10397 if (num
== TARGET_NR_capget
) {
10398 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
10400 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
10402 if (!target_data
) {
10403 unlock_user_struct(target_header
, arg1
, 0);
10404 return -TARGET_EFAULT
;
10407 if (num
== TARGET_NR_capset
) {
10408 for (i
= 0; i
< data_items
; i
++) {
10409 data
[i
].effective
= tswap32(target_data
[i
].effective
);
10410 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
10411 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
10418 if (num
== TARGET_NR_capget
) {
10419 ret
= get_errno(capget(&header
, dataptr
));
10421 ret
= get_errno(capset(&header
, dataptr
));
10424 /* The kernel always updates version for both capget and capset */
10425 target_header
->version
= tswap32(header
.version
);
10426 unlock_user_struct(target_header
, arg1
, 1);
10429 if (num
== TARGET_NR_capget
) {
10430 for (i
= 0; i
< data_items
; i
++) {
10431 target_data
[i
].effective
= tswap32(data
[i
].effective
);
10432 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
10433 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
10435 unlock_user(target_data
, arg2
, target_datalen
);
10437 unlock_user(target_data
, arg2
, 0);
10442 case TARGET_NR_sigaltstack
:
10443 return do_sigaltstack(arg1
, arg2
,
10444 get_sp_from_cpustate((CPUArchState
*)cpu_env
));
10446 #ifdef CONFIG_SENDFILE
10447 #ifdef TARGET_NR_sendfile
10448 case TARGET_NR_sendfile
:
10450 off_t
*offp
= NULL
;
10453 ret
= get_user_sal(off
, arg3
);
10454 if (is_error(ret
)) {
10459 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
10460 if (!is_error(ret
) && arg3
) {
10461 abi_long ret2
= put_user_sal(off
, arg3
);
10462 if (is_error(ret2
)) {
10469 #ifdef TARGET_NR_sendfile64
10470 case TARGET_NR_sendfile64
:
10472 off_t
*offp
= NULL
;
10475 ret
= get_user_s64(off
, arg3
);
10476 if (is_error(ret
)) {
10481 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
10482 if (!is_error(ret
) && arg3
) {
10483 abi_long ret2
= put_user_s64(off
, arg3
);
10484 if (is_error(ret2
)) {
10492 #ifdef TARGET_NR_vfork
10493 case TARGET_NR_vfork
:
10494 return get_errno(do_fork(cpu_env
,
10495 CLONE_VFORK
| CLONE_VM
| TARGET_SIGCHLD
,
10498 #ifdef TARGET_NR_ugetrlimit
10499 case TARGET_NR_ugetrlimit
:
10501 struct rlimit rlim
;
10502 int resource
= target_to_host_resource(arg1
);
10503 ret
= get_errno(getrlimit(resource
, &rlim
));
10504 if (!is_error(ret
)) {
10505 struct target_rlimit
*target_rlim
;
10506 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
10507 return -TARGET_EFAULT
;
10508 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
10509 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
10510 unlock_user_struct(target_rlim
, arg2
, 1);
10515 #ifdef TARGET_NR_truncate64
10516 case TARGET_NR_truncate64
:
10517 if (!(p
= lock_user_string(arg1
)))
10518 return -TARGET_EFAULT
;
10519 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
10520 unlock_user(p
, arg1
, 0);
10523 #ifdef TARGET_NR_ftruncate64
10524 case TARGET_NR_ftruncate64
:
10525 return target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
10527 #ifdef TARGET_NR_stat64
10528 case TARGET_NR_stat64
:
10529 if (!(p
= lock_user_string(arg1
))) {
10530 return -TARGET_EFAULT
;
10532 ret
= get_errno(stat(path(p
), &st
));
10533 unlock_user(p
, arg1
, 0);
10534 if (!is_error(ret
))
10535 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10538 #ifdef TARGET_NR_lstat64
10539 case TARGET_NR_lstat64
:
10540 if (!(p
= lock_user_string(arg1
))) {
10541 return -TARGET_EFAULT
;
10543 ret
= get_errno(lstat(path(p
), &st
));
10544 unlock_user(p
, arg1
, 0);
10545 if (!is_error(ret
))
10546 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10549 #ifdef TARGET_NR_fstat64
10550 case TARGET_NR_fstat64
:
10551 ret
= get_errno(fstat(arg1
, &st
));
10552 if (!is_error(ret
))
10553 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
10556 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
10557 #ifdef TARGET_NR_fstatat64
10558 case TARGET_NR_fstatat64
:
10560 #ifdef TARGET_NR_newfstatat
10561 case TARGET_NR_newfstatat
:
10563 if (!(p
= lock_user_string(arg2
))) {
10564 return -TARGET_EFAULT
;
10566 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
10567 unlock_user(p
, arg2
, 0);
10568 if (!is_error(ret
))
10569 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
10572 #if defined(TARGET_NR_statx)
10573 case TARGET_NR_statx
:
10575 struct target_statx
*target_stx
;
10579 p
= lock_user_string(arg2
);
10581 return -TARGET_EFAULT
;
10583 #if defined(__NR_statx)
10586 * It is assumed that struct statx is architecture independent.
10588 struct target_statx host_stx
;
10591 ret
= get_errno(sys_statx(dirfd
, p
, flags
, mask
, &host_stx
));
10592 if (!is_error(ret
)) {
10593 if (host_to_target_statx(&host_stx
, arg5
) != 0) {
10594 unlock_user(p
, arg2
, 0);
10595 return -TARGET_EFAULT
;
10599 if (ret
!= -TARGET_ENOSYS
) {
10600 unlock_user(p
, arg2
, 0);
10605 ret
= get_errno(fstatat(dirfd
, path(p
), &st
, flags
));
10606 unlock_user(p
, arg2
, 0);
10608 if (!is_error(ret
)) {
10609 if (!lock_user_struct(VERIFY_WRITE
, target_stx
, arg5
, 0)) {
10610 return -TARGET_EFAULT
;
10612 memset(target_stx
, 0, sizeof(*target_stx
));
10613 __put_user(major(st
.st_dev
), &target_stx
->stx_dev_major
);
10614 __put_user(minor(st
.st_dev
), &target_stx
->stx_dev_minor
);
10615 __put_user(st
.st_ino
, &target_stx
->stx_ino
);
10616 __put_user(st
.st_mode
, &target_stx
->stx_mode
);
10617 __put_user(st
.st_uid
, &target_stx
->stx_uid
);
10618 __put_user(st
.st_gid
, &target_stx
->stx_gid
);
10619 __put_user(st
.st_nlink
, &target_stx
->stx_nlink
);
10620 __put_user(major(st
.st_rdev
), &target_stx
->stx_rdev_major
);
10621 __put_user(minor(st
.st_rdev
), &target_stx
->stx_rdev_minor
);
10622 __put_user(st
.st_size
, &target_stx
->stx_size
);
10623 __put_user(st
.st_blksize
, &target_stx
->stx_blksize
);
10624 __put_user(st
.st_blocks
, &target_stx
->stx_blocks
);
10625 __put_user(st
.st_atime
, &target_stx
->stx_atime
.tv_sec
);
10626 __put_user(st
.st_mtime
, &target_stx
->stx_mtime
.tv_sec
);
10627 __put_user(st
.st_ctime
, &target_stx
->stx_ctime
.tv_sec
);
10628 unlock_user_struct(target_stx
, arg5
, 1);
10633 #ifdef TARGET_NR_lchown
10634 case TARGET_NR_lchown
:
10635 if (!(p
= lock_user_string(arg1
)))
10636 return -TARGET_EFAULT
;
10637 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
10638 unlock_user(p
, arg1
, 0);
10641 #ifdef TARGET_NR_getuid
10642 case TARGET_NR_getuid
:
10643 return get_errno(high2lowuid(getuid()));
10645 #ifdef TARGET_NR_getgid
10646 case TARGET_NR_getgid
:
10647 return get_errno(high2lowgid(getgid()));
10649 #ifdef TARGET_NR_geteuid
10650 case TARGET_NR_geteuid
:
10651 return get_errno(high2lowuid(geteuid()));
10653 #ifdef TARGET_NR_getegid
10654 case TARGET_NR_getegid
:
10655 return get_errno(high2lowgid(getegid()));
10657 case TARGET_NR_setreuid
:
10658 return get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
10659 case TARGET_NR_setregid
:
10660 return get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
10661 case TARGET_NR_getgroups
:
10663 int gidsetsize
= arg1
;
10664 target_id
*target_grouplist
;
10668 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
10669 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
10670 if (gidsetsize
== 0)
10672 if (!is_error(ret
)) {
10673 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
10674 if (!target_grouplist
)
10675 return -TARGET_EFAULT
;
10676 for(i
= 0;i
< ret
; i
++)
10677 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
10678 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
10682 case TARGET_NR_setgroups
:
10684 int gidsetsize
= arg1
;
10685 target_id
*target_grouplist
;
10686 gid_t
*grouplist
= NULL
;
10689 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
10690 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
10691 if (!target_grouplist
) {
10692 return -TARGET_EFAULT
;
10694 for (i
= 0; i
< gidsetsize
; i
++) {
10695 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
10697 unlock_user(target_grouplist
, arg2
, 0);
10699 return get_errno(setgroups(gidsetsize
, grouplist
));
10701 case TARGET_NR_fchown
:
10702 return get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
10703 #if defined(TARGET_NR_fchownat)
10704 case TARGET_NR_fchownat
:
10705 if (!(p
= lock_user_string(arg2
)))
10706 return -TARGET_EFAULT
;
10707 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
10708 low2highgid(arg4
), arg5
));
10709 unlock_user(p
, arg2
, 0);
10712 #ifdef TARGET_NR_setresuid
10713 case TARGET_NR_setresuid
:
10714 return get_errno(sys_setresuid(low2highuid(arg1
),
10716 low2highuid(arg3
)));
10718 #ifdef TARGET_NR_getresuid
10719 case TARGET_NR_getresuid
:
10721 uid_t ruid
, euid
, suid
;
10722 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
10723 if (!is_error(ret
)) {
10724 if (put_user_id(high2lowuid(ruid
), arg1
)
10725 || put_user_id(high2lowuid(euid
), arg2
)
10726 || put_user_id(high2lowuid(suid
), arg3
))
10727 return -TARGET_EFAULT
;
10732 #ifdef TARGET_NR_getresgid
10733 case TARGET_NR_setresgid
:
10734 return get_errno(sys_setresgid(low2highgid(arg1
),
10736 low2highgid(arg3
)));
10738 #ifdef TARGET_NR_getresgid
10739 case TARGET_NR_getresgid
:
10741 gid_t rgid
, egid
, sgid
;
10742 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
10743 if (!is_error(ret
)) {
10744 if (put_user_id(high2lowgid(rgid
), arg1
)
10745 || put_user_id(high2lowgid(egid
), arg2
)
10746 || put_user_id(high2lowgid(sgid
), arg3
))
10747 return -TARGET_EFAULT
;
10752 #ifdef TARGET_NR_chown
10753 case TARGET_NR_chown
:
10754 if (!(p
= lock_user_string(arg1
)))
10755 return -TARGET_EFAULT
;
10756 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
10757 unlock_user(p
, arg1
, 0);
10760 case TARGET_NR_setuid
:
10761 return get_errno(sys_setuid(low2highuid(arg1
)));
10762 case TARGET_NR_setgid
:
10763 return get_errno(sys_setgid(low2highgid(arg1
)));
10764 case TARGET_NR_setfsuid
:
10765 return get_errno(setfsuid(arg1
));
10766 case TARGET_NR_setfsgid
:
10767 return get_errno(setfsgid(arg1
));
10769 #ifdef TARGET_NR_lchown32
10770 case TARGET_NR_lchown32
:
10771 if (!(p
= lock_user_string(arg1
)))
10772 return -TARGET_EFAULT
;
10773 ret
= get_errno(lchown(p
, arg2
, arg3
));
10774 unlock_user(p
, arg1
, 0);
10777 #ifdef TARGET_NR_getuid32
10778 case TARGET_NR_getuid32
:
10779 return get_errno(getuid());
10782 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
10783 /* Alpha specific */
10784 case TARGET_NR_getxuid
:
10788 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=euid
;
10790 return get_errno(getuid());
10792 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
10793 /* Alpha specific */
10794 case TARGET_NR_getxgid
:
10798 ((CPUAlphaState
*)cpu_env
)->ir
[IR_A4
]=egid
;
10800 return get_errno(getgid());
10802 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
10803 /* Alpha specific */
10804 case TARGET_NR_osf_getsysinfo
:
10805 ret
= -TARGET_EOPNOTSUPP
;
10807 case TARGET_GSI_IEEE_FP_CONTROL
:
10809 uint64_t fpcr
= cpu_alpha_load_fpcr(cpu_env
);
10810 uint64_t swcr
= ((CPUAlphaState
*)cpu_env
)->swcr
;
10812 swcr
&= ~SWCR_STATUS_MASK
;
10813 swcr
|= (fpcr
>> 35) & SWCR_STATUS_MASK
;
10815 if (put_user_u64 (swcr
, arg2
))
10816 return -TARGET_EFAULT
;
10821 /* case GSI_IEEE_STATE_AT_SIGNAL:
10822 -- Not implemented in linux kernel.
10824 -- Retrieves current unaligned access state; not much used.
10825 case GSI_PROC_TYPE:
10826 -- Retrieves implver information; surely not used.
10827 case GSI_GET_HWRPB:
10828 -- Grabs a copy of the HWRPB; surely not used.
10833 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
10834 /* Alpha specific */
10835 case TARGET_NR_osf_setsysinfo
:
10836 ret
= -TARGET_EOPNOTSUPP
;
10838 case TARGET_SSI_IEEE_FP_CONTROL
:
10840 uint64_t swcr
, fpcr
;
10842 if (get_user_u64 (swcr
, arg2
)) {
10843 return -TARGET_EFAULT
;
10847 * The kernel calls swcr_update_status to update the
10848 * status bits from the fpcr at every point that it
10849 * could be queried. Therefore, we store the status
10850 * bits only in FPCR.
10852 ((CPUAlphaState
*)cpu_env
)->swcr
10853 = swcr
& (SWCR_TRAP_ENABLE_MASK
| SWCR_MAP_MASK
);
10855 fpcr
= cpu_alpha_load_fpcr(cpu_env
);
10856 fpcr
&= ((uint64_t)FPCR_DYN_MASK
<< 32);
10857 fpcr
|= alpha_ieee_swcr_to_fpcr(swcr
);
10858 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
10863 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
10865 uint64_t exc
, fpcr
, fex
;
10867 if (get_user_u64(exc
, arg2
)) {
10868 return -TARGET_EFAULT
;
10870 exc
&= SWCR_STATUS_MASK
;
10871 fpcr
= cpu_alpha_load_fpcr(cpu_env
);
10873 /* Old exceptions are not signaled. */
10874 fex
= alpha_ieee_fpcr_to_swcr(fpcr
);
10876 fex
>>= SWCR_STATUS_TO_EXCSUM_SHIFT
;
10877 fex
&= ((CPUArchState
*)cpu_env
)->swcr
;
10879 /* Update the hardware fpcr. */
10880 fpcr
|= alpha_ieee_swcr_to_fpcr(exc
);
10881 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
10884 int si_code
= TARGET_FPE_FLTUNK
;
10885 target_siginfo_t info
;
10887 if (fex
& SWCR_TRAP_ENABLE_DNO
) {
10888 si_code
= TARGET_FPE_FLTUND
;
10890 if (fex
& SWCR_TRAP_ENABLE_INE
) {
10891 si_code
= TARGET_FPE_FLTRES
;
10893 if (fex
& SWCR_TRAP_ENABLE_UNF
) {
10894 si_code
= TARGET_FPE_FLTUND
;
10896 if (fex
& SWCR_TRAP_ENABLE_OVF
) {
10897 si_code
= TARGET_FPE_FLTOVF
;
10899 if (fex
& SWCR_TRAP_ENABLE_DZE
) {
10900 si_code
= TARGET_FPE_FLTDIV
;
10902 if (fex
& SWCR_TRAP_ENABLE_INV
) {
10903 si_code
= TARGET_FPE_FLTINV
;
10906 info
.si_signo
= SIGFPE
;
10908 info
.si_code
= si_code
;
10909 info
._sifields
._sigfault
._addr
10910 = ((CPUArchState
*)cpu_env
)->pc
;
10911 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
,
10912 QEMU_SI_FAULT
, &info
);
10918 /* case SSI_NVPAIRS:
10919 -- Used with SSIN_UACPROC to enable unaligned accesses.
10920 case SSI_IEEE_STATE_AT_SIGNAL:
10921 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
10922 -- Not implemented in linux kernel
10927 #ifdef TARGET_NR_osf_sigprocmask
10928 /* Alpha specific. */
10929 case TARGET_NR_osf_sigprocmask
:
10933 sigset_t set
, oldset
;
10936 case TARGET_SIG_BLOCK
:
10939 case TARGET_SIG_UNBLOCK
:
10942 case TARGET_SIG_SETMASK
:
10946 return -TARGET_EINVAL
;
10949 target_to_host_old_sigset(&set
, &mask
);
10950 ret
= do_sigprocmask(how
, &set
, &oldset
);
10952 host_to_target_old_sigset(&mask
, &oldset
);
10959 #ifdef TARGET_NR_getgid32
10960 case TARGET_NR_getgid32
:
10961 return get_errno(getgid());
10963 #ifdef TARGET_NR_geteuid32
10964 case TARGET_NR_geteuid32
:
10965 return get_errno(geteuid());
10967 #ifdef TARGET_NR_getegid32
10968 case TARGET_NR_getegid32
:
10969 return get_errno(getegid());
10971 #ifdef TARGET_NR_setreuid32
10972 case TARGET_NR_setreuid32
:
10973 return get_errno(setreuid(arg1
, arg2
));
10975 #ifdef TARGET_NR_setregid32
10976 case TARGET_NR_setregid32
:
10977 return get_errno(setregid(arg1
, arg2
));
10979 #ifdef TARGET_NR_getgroups32
10980 case TARGET_NR_getgroups32
:
10982 int gidsetsize
= arg1
;
10983 uint32_t *target_grouplist
;
10987 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
10988 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
10989 if (gidsetsize
== 0)
10991 if (!is_error(ret
)) {
10992 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
10993 if (!target_grouplist
) {
10994 return -TARGET_EFAULT
;
10996 for(i
= 0;i
< ret
; i
++)
10997 target_grouplist
[i
] = tswap32(grouplist
[i
]);
10998 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
11003 #ifdef TARGET_NR_setgroups32
11004 case TARGET_NR_setgroups32
:
11006 int gidsetsize
= arg1
;
11007 uint32_t *target_grouplist
;
11011 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11012 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
11013 if (!target_grouplist
) {
11014 return -TARGET_EFAULT
;
11016 for(i
= 0;i
< gidsetsize
; i
++)
11017 grouplist
[i
] = tswap32(target_grouplist
[i
]);
11018 unlock_user(target_grouplist
, arg2
, 0);
11019 return get_errno(setgroups(gidsetsize
, grouplist
));
11022 #ifdef TARGET_NR_fchown32
11023 case TARGET_NR_fchown32
:
11024 return get_errno(fchown(arg1
, arg2
, arg3
));
11026 #ifdef TARGET_NR_setresuid32
11027 case TARGET_NR_setresuid32
:
11028 return get_errno(sys_setresuid(arg1
, arg2
, arg3
));
11030 #ifdef TARGET_NR_getresuid32
11031 case TARGET_NR_getresuid32
:
11033 uid_t ruid
, euid
, suid
;
11034 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11035 if (!is_error(ret
)) {
11036 if (put_user_u32(ruid
, arg1
)
11037 || put_user_u32(euid
, arg2
)
11038 || put_user_u32(suid
, arg3
))
11039 return -TARGET_EFAULT
;
11044 #ifdef TARGET_NR_setresgid32
11045 case TARGET_NR_setresgid32
:
11046 return get_errno(sys_setresgid(arg1
, arg2
, arg3
));
11048 #ifdef TARGET_NR_getresgid32
11049 case TARGET_NR_getresgid32
:
11051 gid_t rgid
, egid
, sgid
;
11052 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11053 if (!is_error(ret
)) {
11054 if (put_user_u32(rgid
, arg1
)
11055 || put_user_u32(egid
, arg2
)
11056 || put_user_u32(sgid
, arg3
))
11057 return -TARGET_EFAULT
;
11062 #ifdef TARGET_NR_chown32
11063 case TARGET_NR_chown32
:
11064 if (!(p
= lock_user_string(arg1
)))
11065 return -TARGET_EFAULT
;
11066 ret
= get_errno(chown(p
, arg2
, arg3
));
11067 unlock_user(p
, arg1
, 0);
11070 #ifdef TARGET_NR_setuid32
11071 case TARGET_NR_setuid32
:
11072 return get_errno(sys_setuid(arg1
));
11074 #ifdef TARGET_NR_setgid32
11075 case TARGET_NR_setgid32
:
11076 return get_errno(sys_setgid(arg1
));
11078 #ifdef TARGET_NR_setfsuid32
11079 case TARGET_NR_setfsuid32
:
11080 return get_errno(setfsuid(arg1
));
11082 #ifdef TARGET_NR_setfsgid32
11083 case TARGET_NR_setfsgid32
:
11084 return get_errno(setfsgid(arg1
));
11086 #ifdef TARGET_NR_mincore
11087 case TARGET_NR_mincore
:
11089 void *a
= lock_user(VERIFY_READ
, arg1
, arg2
, 0);
11091 return -TARGET_ENOMEM
;
11093 p
= lock_user_string(arg3
);
11095 ret
= -TARGET_EFAULT
;
11097 ret
= get_errno(mincore(a
, arg2
, p
));
11098 unlock_user(p
, arg3
, ret
);
11100 unlock_user(a
, arg1
, 0);
11104 #ifdef TARGET_NR_arm_fadvise64_64
11105 case TARGET_NR_arm_fadvise64_64
:
11106 /* arm_fadvise64_64 looks like fadvise64_64 but
11107 * with different argument order: fd, advice, offset, len
11108 * rather than the usual fd, offset, len, advice.
11109 * Note that offset and len are both 64-bit so appear as
11110 * pairs of 32-bit registers.
11112 ret
= posix_fadvise(arg1
, target_offset64(arg3
, arg4
),
11113 target_offset64(arg5
, arg6
), arg2
);
11114 return -host_to_target_errno(ret
);
11117 #if TARGET_ABI_BITS == 32
11119 #ifdef TARGET_NR_fadvise64_64
11120 case TARGET_NR_fadvise64_64
:
11121 #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
11122 /* 6 args: fd, advice, offset (high, low), len (high, low) */
11130 /* 6 args: fd, offset (high, low), len (high, low), advice */
11131 if (regpairs_aligned(cpu_env
, num
)) {
11132 /* offset is in (3,4), len in (5,6) and advice in 7 */
11140 ret
= posix_fadvise(arg1
, target_offset64(arg2
, arg3
),
11141 target_offset64(arg4
, arg5
), arg6
);
11142 return -host_to_target_errno(ret
);
11145 #ifdef TARGET_NR_fadvise64
11146 case TARGET_NR_fadvise64
:
11147 /* 5 args: fd, offset (high, low), len, advice */
11148 if (regpairs_aligned(cpu_env
, num
)) {
11149 /* offset is in (3,4), len in 5 and advice in 6 */
11155 ret
= posix_fadvise(arg1
, target_offset64(arg2
, arg3
), arg4
, arg5
);
11156 return -host_to_target_errno(ret
);
11159 #else /* not a 32-bit ABI */
11160 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
11161 #ifdef TARGET_NR_fadvise64_64
11162 case TARGET_NR_fadvise64_64
:
11164 #ifdef TARGET_NR_fadvise64
11165 case TARGET_NR_fadvise64
:
11167 #ifdef TARGET_S390X
11169 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
11170 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
11171 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
11172 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
11176 return -host_to_target_errno(posix_fadvise(arg1
, arg2
, arg3
, arg4
));
11178 #endif /* end of 64-bit ABI fadvise handling */
11180 #ifdef TARGET_NR_madvise
11181 case TARGET_NR_madvise
:
11182 /* A straight passthrough may not be safe because qemu sometimes
11183 turns private file-backed mappings into anonymous mappings.
11184 This will break MADV_DONTNEED.
11185 This is a hint, so ignoring and returning success is ok. */
11188 #if TARGET_ABI_BITS == 32
11189 case TARGET_NR_fcntl64
:
11193 from_flock64_fn
*copyfrom
= copy_from_user_flock64
;
11194 to_flock64_fn
*copyto
= copy_to_user_flock64
;
11197 if (!((CPUARMState
*)cpu_env
)->eabi
) {
11198 copyfrom
= copy_from_user_oabi_flock64
;
11199 copyto
= copy_to_user_oabi_flock64
;
11203 cmd
= target_to_host_fcntl_cmd(arg2
);
11204 if (cmd
== -TARGET_EINVAL
) {
11209 case TARGET_F_GETLK64
:
11210 ret
= copyfrom(&fl
, arg3
);
11214 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
11216 ret
= copyto(arg3
, &fl
);
11220 case TARGET_F_SETLK64
:
11221 case TARGET_F_SETLKW64
:
11222 ret
= copyfrom(&fl
, arg3
);
11226 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
11229 ret
= do_fcntl(arg1
, arg2
, arg3
);
11235 #ifdef TARGET_NR_cacheflush
11236 case TARGET_NR_cacheflush
:
11237 /* self-modifying code is handled automatically, so nothing needed */
11240 #ifdef TARGET_NR_getpagesize
11241 case TARGET_NR_getpagesize
:
11242 return TARGET_PAGE_SIZE
;
11244 case TARGET_NR_gettid
:
11245 return get_errno(sys_gettid());
11246 #ifdef TARGET_NR_readahead
11247 case TARGET_NR_readahead
:
11248 #if TARGET_ABI_BITS == 32
11249 if (regpairs_aligned(cpu_env
, num
)) {
11254 ret
= get_errno(readahead(arg1
, target_offset64(arg2
, arg3
) , arg4
));
11256 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
11261 #ifdef TARGET_NR_setxattr
11262 case TARGET_NR_listxattr
:
11263 case TARGET_NR_llistxattr
:
11267 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11269 return -TARGET_EFAULT
;
11272 p
= lock_user_string(arg1
);
11274 if (num
== TARGET_NR_listxattr
) {
11275 ret
= get_errno(listxattr(p
, b
, arg3
));
11277 ret
= get_errno(llistxattr(p
, b
, arg3
));
11280 ret
= -TARGET_EFAULT
;
11282 unlock_user(p
, arg1
, 0);
11283 unlock_user(b
, arg2
, arg3
);
11286 case TARGET_NR_flistxattr
:
11290 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11292 return -TARGET_EFAULT
;
11295 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
11296 unlock_user(b
, arg2
, arg3
);
11299 case TARGET_NR_setxattr
:
11300 case TARGET_NR_lsetxattr
:
11302 void *p
, *n
, *v
= 0;
11304 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
11306 return -TARGET_EFAULT
;
11309 p
= lock_user_string(arg1
);
11310 n
= lock_user_string(arg2
);
11312 if (num
== TARGET_NR_setxattr
) {
11313 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
11315 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
11318 ret
= -TARGET_EFAULT
;
11320 unlock_user(p
, arg1
, 0);
11321 unlock_user(n
, arg2
, 0);
11322 unlock_user(v
, arg3
, 0);
11325 case TARGET_NR_fsetxattr
:
11329 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
11331 return -TARGET_EFAULT
;
11334 n
= lock_user_string(arg2
);
11336 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
11338 ret
= -TARGET_EFAULT
;
11340 unlock_user(n
, arg2
, 0);
11341 unlock_user(v
, arg3
, 0);
11344 case TARGET_NR_getxattr
:
11345 case TARGET_NR_lgetxattr
:
11347 void *p
, *n
, *v
= 0;
11349 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
11351 return -TARGET_EFAULT
;
11354 p
= lock_user_string(arg1
);
11355 n
= lock_user_string(arg2
);
11357 if (num
== TARGET_NR_getxattr
) {
11358 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
11360 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
11363 ret
= -TARGET_EFAULT
;
11365 unlock_user(p
, arg1
, 0);
11366 unlock_user(n
, arg2
, 0);
11367 unlock_user(v
, arg3
, arg4
);
11370 case TARGET_NR_fgetxattr
:
11374 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
11376 return -TARGET_EFAULT
;
11379 n
= lock_user_string(arg2
);
11381 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
11383 ret
= -TARGET_EFAULT
;
11385 unlock_user(n
, arg2
, 0);
11386 unlock_user(v
, arg3
, arg4
);
11389 case TARGET_NR_removexattr
:
11390 case TARGET_NR_lremovexattr
:
11393 p
= lock_user_string(arg1
);
11394 n
= lock_user_string(arg2
);
11396 if (num
== TARGET_NR_removexattr
) {
11397 ret
= get_errno(removexattr(p
, n
));
11399 ret
= get_errno(lremovexattr(p
, n
));
11402 ret
= -TARGET_EFAULT
;
11404 unlock_user(p
, arg1
, 0);
11405 unlock_user(n
, arg2
, 0);
11408 case TARGET_NR_fremovexattr
:
11411 n
= lock_user_string(arg2
);
11413 ret
= get_errno(fremovexattr(arg1
, n
));
11415 ret
= -TARGET_EFAULT
;
11417 unlock_user(n
, arg2
, 0);
11421 #endif /* CONFIG_ATTR */
11422 #ifdef TARGET_NR_set_thread_area
11423 case TARGET_NR_set_thread_area
:
11424 #if defined(TARGET_MIPS)
11425 ((CPUMIPSState
*) cpu_env
)->active_tc
.CP0_UserLocal
= arg1
;
11427 #elif defined(TARGET_CRIS)
11429 ret
= -TARGET_EINVAL
;
11431 ((CPUCRISState
*) cpu_env
)->pregs
[PR_PID
] = arg1
;
11435 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
11436 return do_set_thread_area(cpu_env
, arg1
);
11437 #elif defined(TARGET_M68K)
11439 TaskState
*ts
= cpu
->opaque
;
11440 ts
->tp_value
= arg1
;
11444 return -TARGET_ENOSYS
;
11447 #ifdef TARGET_NR_get_thread_area
11448 case TARGET_NR_get_thread_area
:
11449 #if defined(TARGET_I386) && defined(TARGET_ABI32)
11450 return do_get_thread_area(cpu_env
, arg1
);
11451 #elif defined(TARGET_M68K)
11453 TaskState
*ts
= cpu
->opaque
;
11454 return ts
->tp_value
;
11457 return -TARGET_ENOSYS
;
11460 #ifdef TARGET_NR_getdomainname
11461 case TARGET_NR_getdomainname
:
11462 return -TARGET_ENOSYS
;
11465 #ifdef TARGET_NR_clock_settime
11466 case TARGET_NR_clock_settime
:
11468 struct timespec ts
;
11470 ret
= target_to_host_timespec(&ts
, arg2
);
11471 if (!is_error(ret
)) {
11472 ret
= get_errno(clock_settime(arg1
, &ts
));
11477 #ifdef TARGET_NR_clock_settime64
11478 case TARGET_NR_clock_settime64
:
11480 struct timespec ts
;
11482 ret
= target_to_host_timespec64(&ts
, arg2
);
11483 if (!is_error(ret
)) {
11484 ret
= get_errno(clock_settime(arg1
, &ts
));
11489 #ifdef TARGET_NR_clock_gettime
11490 case TARGET_NR_clock_gettime
:
11492 struct timespec ts
;
11493 ret
= get_errno(clock_gettime(arg1
, &ts
));
11494 if (!is_error(ret
)) {
11495 ret
= host_to_target_timespec(arg2
, &ts
);
11500 #ifdef TARGET_NR_clock_gettime64
11501 case TARGET_NR_clock_gettime64
:
11503 struct timespec ts
;
11504 ret
= get_errno(clock_gettime(arg1
, &ts
));
11505 if (!is_error(ret
)) {
11506 ret
= host_to_target_timespec64(arg2
, &ts
);
11511 #ifdef TARGET_NR_clock_getres
11512 case TARGET_NR_clock_getres
:
11514 struct timespec ts
;
11515 ret
= get_errno(clock_getres(arg1
, &ts
));
11516 if (!is_error(ret
)) {
11517 host_to_target_timespec(arg2
, &ts
);
11522 #ifdef TARGET_NR_clock_nanosleep
11523 case TARGET_NR_clock_nanosleep
:
11525 struct timespec ts
;
11526 target_to_host_timespec(&ts
, arg3
);
11527 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
11528 &ts
, arg4
? &ts
: NULL
));
11530 host_to_target_timespec(arg4
, &ts
);
11532 #if defined(TARGET_PPC)
11533 /* clock_nanosleep is odd in that it returns positive errno values.
11534 * On PPC, CR0 bit 3 should be set in such a situation. */
11535 if (ret
&& ret
!= -TARGET_ERESTARTSYS
) {
11536 ((CPUPPCState
*)cpu_env
)->crf
[0] |= 1;
11543 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
11544 case TARGET_NR_set_tid_address
:
11545 return get_errno(set_tid_address((int *)g2h(arg1
)));
11548 case TARGET_NR_tkill
:
11549 return get_errno(safe_tkill((int)arg1
, target_to_host_signal(arg2
)));
11551 case TARGET_NR_tgkill
:
11552 return get_errno(safe_tgkill((int)arg1
, (int)arg2
,
11553 target_to_host_signal(arg3
)));
11555 #ifdef TARGET_NR_set_robust_list
11556 case TARGET_NR_set_robust_list
:
11557 case TARGET_NR_get_robust_list
:
11558 /* The ABI for supporting robust futexes has userspace pass
11559 * the kernel a pointer to a linked list which is updated by
11560 * userspace after the syscall; the list is walked by the kernel
11561 * when the thread exits. Since the linked list in QEMU guest
11562 * memory isn't a valid linked list for the host and we have
11563 * no way to reliably intercept the thread-death event, we can't
11564 * support these. Silently return ENOSYS so that guest userspace
11565 * falls back to a non-robust futex implementation (which should
11566 * be OK except in the corner case of the guest crashing while
11567 * holding a mutex that is shared with another process via
11570 return -TARGET_ENOSYS
;
11573 #if defined(TARGET_NR_utimensat)
11574 case TARGET_NR_utimensat
:
11576 struct timespec
*tsp
, ts
[2];
11580 target_to_host_timespec(ts
, arg3
);
11581 target_to_host_timespec(ts
+1, arg3
+sizeof(struct target_timespec
));
11585 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
11587 if (!(p
= lock_user_string(arg2
))) {
11588 return -TARGET_EFAULT
;
11590 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
11591 unlock_user(p
, arg2
, 0);
11596 #ifdef TARGET_NR_futex
11597 case TARGET_NR_futex
:
11598 return do_futex(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
11600 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
11601 case TARGET_NR_inotify_init
:
11602 ret
= get_errno(sys_inotify_init());
11604 fd_trans_register(ret
, &target_inotify_trans
);
11608 #ifdef CONFIG_INOTIFY1
11609 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
11610 case TARGET_NR_inotify_init1
:
11611 ret
= get_errno(sys_inotify_init1(target_to_host_bitmask(arg1
,
11612 fcntl_flags_tbl
)));
11614 fd_trans_register(ret
, &target_inotify_trans
);
11619 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
11620 case TARGET_NR_inotify_add_watch
:
11621 p
= lock_user_string(arg2
);
11622 ret
= get_errno(sys_inotify_add_watch(arg1
, path(p
), arg3
));
11623 unlock_user(p
, arg2
, 0);
11626 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
11627 case TARGET_NR_inotify_rm_watch
:
11628 return get_errno(sys_inotify_rm_watch(arg1
, arg2
));
11631 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
11632 case TARGET_NR_mq_open
:
11634 struct mq_attr posix_mq_attr
;
11635 struct mq_attr
*pposix_mq_attr
;
11638 host_flags
= target_to_host_bitmask(arg2
, fcntl_flags_tbl
);
11639 pposix_mq_attr
= NULL
;
11641 if (copy_from_user_mq_attr(&posix_mq_attr
, arg4
) != 0) {
11642 return -TARGET_EFAULT
;
11644 pposix_mq_attr
= &posix_mq_attr
;
11646 p
= lock_user_string(arg1
- 1);
11648 return -TARGET_EFAULT
;
11650 ret
= get_errno(mq_open(p
, host_flags
, arg3
, pposix_mq_attr
));
11651 unlock_user (p
, arg1
, 0);
11655 case TARGET_NR_mq_unlink
:
11656 p
= lock_user_string(arg1
- 1);
11658 return -TARGET_EFAULT
;
11660 ret
= get_errno(mq_unlink(p
));
11661 unlock_user (p
, arg1
, 0);
11664 #ifdef TARGET_NR_mq_timedsend
11665 case TARGET_NR_mq_timedsend
:
11667 struct timespec ts
;
11669 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
11671 target_to_host_timespec(&ts
, arg5
);
11672 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
11673 host_to_target_timespec(arg5
, &ts
);
11675 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
11677 unlock_user (p
, arg2
, arg3
);
11682 #ifdef TARGET_NR_mq_timedreceive
11683 case TARGET_NR_mq_timedreceive
:
11685 struct timespec ts
;
11688 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
11690 target_to_host_timespec(&ts
, arg5
);
11691 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
11693 host_to_target_timespec(arg5
, &ts
);
11695 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
11698 unlock_user (p
, arg2
, arg3
);
11700 put_user_u32(prio
, arg4
);
11705 /* Not implemented for now... */
11706 /* case TARGET_NR_mq_notify: */
11709 case TARGET_NR_mq_getsetattr
:
11711 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
11714 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
11715 ret
= get_errno(mq_setattr(arg1
, &posix_mq_attr_in
,
11716 &posix_mq_attr_out
));
11717 } else if (arg3
!= 0) {
11718 ret
= get_errno(mq_getattr(arg1
, &posix_mq_attr_out
));
11720 if (ret
== 0 && arg3
!= 0) {
11721 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
11727 #ifdef CONFIG_SPLICE
11728 #ifdef TARGET_NR_tee
11729 case TARGET_NR_tee
:
11731 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
11735 #ifdef TARGET_NR_splice
11736 case TARGET_NR_splice
:
11738 loff_t loff_in
, loff_out
;
11739 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
11741 if (get_user_u64(loff_in
, arg2
)) {
11742 return -TARGET_EFAULT
;
11744 ploff_in
= &loff_in
;
11747 if (get_user_u64(loff_out
, arg4
)) {
11748 return -TARGET_EFAULT
;
11750 ploff_out
= &loff_out
;
11752 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
11754 if (put_user_u64(loff_in
, arg2
)) {
11755 return -TARGET_EFAULT
;
11759 if (put_user_u64(loff_out
, arg4
)) {
11760 return -TARGET_EFAULT
;
11766 #ifdef TARGET_NR_vmsplice
11767 case TARGET_NR_vmsplice
:
11769 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
11771 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
11772 unlock_iovec(vec
, arg2
, arg3
, 0);
11774 ret
= -host_to_target_errno(errno
);
11779 #endif /* CONFIG_SPLICE */
11780 #ifdef CONFIG_EVENTFD
11781 #if defined(TARGET_NR_eventfd)
11782 case TARGET_NR_eventfd
:
11783 ret
= get_errno(eventfd(arg1
, 0));
11785 fd_trans_register(ret
, &target_eventfd_trans
);
11789 #if defined(TARGET_NR_eventfd2)
11790 case TARGET_NR_eventfd2
:
11792 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK
| TARGET_O_CLOEXEC
));
11793 if (arg2
& TARGET_O_NONBLOCK
) {
11794 host_flags
|= O_NONBLOCK
;
11796 if (arg2
& TARGET_O_CLOEXEC
) {
11797 host_flags
|= O_CLOEXEC
;
11799 ret
= get_errno(eventfd(arg1
, host_flags
));
11801 fd_trans_register(ret
, &target_eventfd_trans
);
11806 #endif /* CONFIG_EVENTFD */
11807 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
11808 case TARGET_NR_fallocate
:
11809 #if TARGET_ABI_BITS == 32
11810 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
11811 target_offset64(arg5
, arg6
)));
11813 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
11817 #if defined(CONFIG_SYNC_FILE_RANGE)
11818 #if defined(TARGET_NR_sync_file_range)
11819 case TARGET_NR_sync_file_range
:
11820 #if TARGET_ABI_BITS == 32
11821 #if defined(TARGET_MIPS)
11822 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
11823 target_offset64(arg5
, arg6
), arg7
));
11825 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
11826 target_offset64(arg4
, arg5
), arg6
));
11827 #endif /* !TARGET_MIPS */
11829 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
11833 #if defined(TARGET_NR_sync_file_range2)
11834 case TARGET_NR_sync_file_range2
:
11835 /* This is like sync_file_range but the arguments are reordered */
11836 #if TARGET_ABI_BITS == 32
11837 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
11838 target_offset64(arg5
, arg6
), arg2
));
11840 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
11845 #if defined(TARGET_NR_signalfd4)
11846 case TARGET_NR_signalfd4
:
11847 return do_signalfd4(arg1
, arg2
, arg4
);
11849 #if defined(TARGET_NR_signalfd)
11850 case TARGET_NR_signalfd
:
11851 return do_signalfd4(arg1
, arg2
, 0);
11853 #if defined(CONFIG_EPOLL)
11854 #if defined(TARGET_NR_epoll_create)
11855 case TARGET_NR_epoll_create
:
11856 return get_errno(epoll_create(arg1
));
11858 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
11859 case TARGET_NR_epoll_create1
:
11860 return get_errno(epoll_create1(arg1
));
11862 #if defined(TARGET_NR_epoll_ctl)
11863 case TARGET_NR_epoll_ctl
:
11865 struct epoll_event ep
;
11866 struct epoll_event
*epp
= 0;
11868 struct target_epoll_event
*target_ep
;
11869 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
11870 return -TARGET_EFAULT
;
11872 ep
.events
= tswap32(target_ep
->events
);
11873 /* The epoll_data_t union is just opaque data to the kernel,
11874 * so we transfer all 64 bits across and need not worry what
11875 * actual data type it is.
11877 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
11878 unlock_user_struct(target_ep
, arg4
, 0);
11881 return get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
11885 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
11886 #if defined(TARGET_NR_epoll_wait)
11887 case TARGET_NR_epoll_wait
:
11889 #if defined(TARGET_NR_epoll_pwait)
11890 case TARGET_NR_epoll_pwait
:
11893 struct target_epoll_event
*target_ep
;
11894 struct epoll_event
*ep
;
11896 int maxevents
= arg3
;
11897 int timeout
= arg4
;
11899 if (maxevents
<= 0 || maxevents
> TARGET_EP_MAX_EVENTS
) {
11900 return -TARGET_EINVAL
;
11903 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
11904 maxevents
* sizeof(struct target_epoll_event
), 1);
11906 return -TARGET_EFAULT
;
11909 ep
= g_try_new(struct epoll_event
, maxevents
);
11911 unlock_user(target_ep
, arg2
, 0);
11912 return -TARGET_ENOMEM
;
11916 #if defined(TARGET_NR_epoll_pwait)
11917 case TARGET_NR_epoll_pwait
:
11919 target_sigset_t
*target_set
;
11920 sigset_t _set
, *set
= &_set
;
11923 if (arg6
!= sizeof(target_sigset_t
)) {
11924 ret
= -TARGET_EINVAL
;
11928 target_set
= lock_user(VERIFY_READ
, arg5
,
11929 sizeof(target_sigset_t
), 1);
11931 ret
= -TARGET_EFAULT
;
11934 target_to_host_sigset(set
, target_set
);
11935 unlock_user(target_set
, arg5
, 0);
11940 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
11941 set
, SIGSET_T_SIZE
));
11945 #if defined(TARGET_NR_epoll_wait)
11946 case TARGET_NR_epoll_wait
:
11947 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
11952 ret
= -TARGET_ENOSYS
;
11954 if (!is_error(ret
)) {
11956 for (i
= 0; i
< ret
; i
++) {
11957 target_ep
[i
].events
= tswap32(ep
[i
].events
);
11958 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
11960 unlock_user(target_ep
, arg2
,
11961 ret
* sizeof(struct target_epoll_event
));
11963 unlock_user(target_ep
, arg2
, 0);
11970 #ifdef TARGET_NR_prlimit64
11971 case TARGET_NR_prlimit64
:
11973 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
11974 struct target_rlimit64
*target_rnew
, *target_rold
;
11975 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
11976 int resource
= target_to_host_resource(arg2
);
11978 if (arg3
&& (resource
!= RLIMIT_AS
&&
11979 resource
!= RLIMIT_DATA
&&
11980 resource
!= RLIMIT_STACK
)) {
11981 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
11982 return -TARGET_EFAULT
;
11984 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
11985 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
11986 unlock_user_struct(target_rnew
, arg3
, 0);
11990 ret
= get_errno(sys_prlimit64(arg1
, resource
, rnewp
, arg4
? &rold
: 0));
11991 if (!is_error(ret
) && arg4
) {
11992 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
11993 return -TARGET_EFAULT
;
11995 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
11996 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
11997 unlock_user_struct(target_rold
, arg4
, 1);
12002 #ifdef TARGET_NR_gethostname
12003 case TARGET_NR_gethostname
:
12005 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
12007 ret
= get_errno(gethostname(name
, arg2
));
12008 unlock_user(name
, arg1
, arg2
);
12010 ret
= -TARGET_EFAULT
;
12015 #ifdef TARGET_NR_atomic_cmpxchg_32
12016 case TARGET_NR_atomic_cmpxchg_32
:
12018 /* should use start_exclusive from main.c */
12019 abi_ulong mem_value
;
12020 if (get_user_u32(mem_value
, arg6
)) {
12021 target_siginfo_t info
;
12022 info
.si_signo
= SIGSEGV
;
12024 info
.si_code
= TARGET_SEGV_MAPERR
;
12025 info
._sifields
._sigfault
._addr
= arg6
;
12026 queue_signal((CPUArchState
*)cpu_env
, info
.si_signo
,
12027 QEMU_SI_FAULT
, &info
);
12031 if (mem_value
== arg2
)
12032 put_user_u32(arg1
, arg6
);
12036 #ifdef TARGET_NR_atomic_barrier
12037 case TARGET_NR_atomic_barrier
:
12038 /* Like the kernel implementation and the
12039 qemu arm barrier, no-op this? */
12043 #ifdef TARGET_NR_timer_create
12044 case TARGET_NR_timer_create
:
12046 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
12048 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
12051 int timer_index
= next_free_host_timer();
12053 if (timer_index
< 0) {
12054 ret
= -TARGET_EAGAIN
;
12056 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
12059 phost_sevp
= &host_sevp
;
12060 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
12066 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
12070 if (put_user(TIMER_MAGIC
| timer_index
, arg3
, target_timer_t
)) {
12071 return -TARGET_EFAULT
;
12079 #ifdef TARGET_NR_timer_settime
12080 case TARGET_NR_timer_settime
:
12082 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
12083 * struct itimerspec * old_value */
12084 target_timer_t timerid
= get_timer_id(arg1
);
12088 } else if (arg3
== 0) {
12089 ret
= -TARGET_EINVAL
;
12091 timer_t htimer
= g_posix_timers
[timerid
];
12092 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
12094 if (target_to_host_itimerspec(&hspec_new
, arg3
)) {
12095 return -TARGET_EFAULT
;
12098 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
12099 if (arg4
&& host_to_target_itimerspec(arg4
, &hspec_old
)) {
12100 return -TARGET_EFAULT
;
12107 #ifdef TARGET_NR_timer_gettime
12108 case TARGET_NR_timer_gettime
:
12110 /* args: timer_t timerid, struct itimerspec *curr_value */
12111 target_timer_t timerid
= get_timer_id(arg1
);
12115 } else if (!arg2
) {
12116 ret
= -TARGET_EFAULT
;
12118 timer_t htimer
= g_posix_timers
[timerid
];
12119 struct itimerspec hspec
;
12120 ret
= get_errno(timer_gettime(htimer
, &hspec
));
12122 if (host_to_target_itimerspec(arg2
, &hspec
)) {
12123 ret
= -TARGET_EFAULT
;
12130 #ifdef TARGET_NR_timer_getoverrun
12131 case TARGET_NR_timer_getoverrun
:
12133 /* args: timer_t timerid */
12134 target_timer_t timerid
= get_timer_id(arg1
);
12139 timer_t htimer
= g_posix_timers
[timerid
];
12140 ret
= get_errno(timer_getoverrun(htimer
));
12146 #ifdef TARGET_NR_timer_delete
12147 case TARGET_NR_timer_delete
:
12149 /* args: timer_t timerid */
12150 target_timer_t timerid
= get_timer_id(arg1
);
12155 timer_t htimer
= g_posix_timers
[timerid
];
12156 ret
= get_errno(timer_delete(htimer
));
12157 g_posix_timers
[timerid
] = 0;
12163 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
12164 case TARGET_NR_timerfd_create
:
12165 return get_errno(timerfd_create(arg1
,
12166 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
12169 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
12170 case TARGET_NR_timerfd_gettime
:
12172 struct itimerspec its_curr
;
12174 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
12176 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
12177 return -TARGET_EFAULT
;
12183 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
12184 case TARGET_NR_timerfd_settime
:
12186 struct itimerspec its_new
, its_old
, *p_new
;
12189 if (target_to_host_itimerspec(&its_new
, arg3
)) {
12190 return -TARGET_EFAULT
;
12197 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
12199 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
12200 return -TARGET_EFAULT
;
12206 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
12207 case TARGET_NR_ioprio_get
:
12208 return get_errno(ioprio_get(arg1
, arg2
));
12211 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
12212 case TARGET_NR_ioprio_set
:
12213 return get_errno(ioprio_set(arg1
, arg2
, arg3
));
12216 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
12217 case TARGET_NR_setns
:
12218 return get_errno(setns(arg1
, arg2
));
12220 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
12221 case TARGET_NR_unshare
:
12222 return get_errno(unshare(arg1
));
12224 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
12225 case TARGET_NR_kcmp
:
12226 return get_errno(kcmp(arg1
, arg2
, arg3
, arg4
, arg5
));
12228 #ifdef TARGET_NR_swapcontext
12229 case TARGET_NR_swapcontext
:
12230 /* PowerPC specific. */
12231 return do_swapcontext(cpu_env
, arg1
, arg2
, arg3
);
12233 #ifdef TARGET_NR_memfd_create
12234 case TARGET_NR_memfd_create
:
12235 p
= lock_user_string(arg1
);
12237 return -TARGET_EFAULT
;
12239 ret
= get_errno(memfd_create(p
, arg2
));
12240 fd_trans_unregister(ret
);
12241 unlock_user(p
, arg1
, 0);
12244 #if defined TARGET_NR_membarrier && defined __NR_membarrier
12245 case TARGET_NR_membarrier
:
12246 return get_errno(membarrier(arg1
, arg2
));
12250 qemu_log_mask(LOG_UNIMP
, "Unsupported syscall: %d\n", num
);
12251 return -TARGET_ENOSYS
;
12256 abi_long
do_syscall(void *cpu_env
, int num
, abi_long arg1
,
12257 abi_long arg2
, abi_long arg3
, abi_long arg4
,
12258 abi_long arg5
, abi_long arg6
, abi_long arg7
,
12261 CPUState
*cpu
= env_cpu(cpu_env
);
12264 #ifdef DEBUG_ERESTARTSYS
12265 /* Debug-only code for exercising the syscall-restart code paths
12266 * in the per-architecture cpu main loops: restart every syscall
12267 * the guest makes once before letting it through.
12273 return -TARGET_ERESTARTSYS
;
12278 record_syscall_start(cpu
, num
, arg1
,
12279 arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
);
12281 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
12282 print_syscall(num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
12285 ret
= do_syscall1(cpu_env
, num
, arg1
, arg2
, arg3
, arg4
,
12286 arg5
, arg6
, arg7
, arg8
);
12288 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
12289 print_syscall_ret(num
, ret
);
12292 record_syscall_return(cpu
, num
, ret
);