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/in.h>
55 #include <netinet/ip.h>
56 #include <netinet/tcp.h>
57 #include <netinet/udp.h>
58 #include <linux/wireless.h>
59 #include <linux/icmp.h>
60 #include <linux/icmpv6.h>
61 #include <linux/if_tun.h>
62 #include <linux/in6.h>
63 #include <linux/errqueue.h>
64 #include <linux/random.h>
66 #include <sys/timerfd.h>
69 #include <sys/eventfd.h>
72 #include <sys/epoll.h>
75 #include "qemu/xattr.h"
77 #ifdef CONFIG_SENDFILE
78 #include <sys/sendfile.h>
80 #ifdef HAVE_SYS_KCOV_H
84 #define termios host_termios
85 #define winsize host_winsize
86 #define termio host_termio
87 #define sgttyb host_sgttyb /* same as target */
88 #define tchars host_tchars /* same as target */
89 #define ltchars host_ltchars /* same as target */
91 #include <linux/termios.h>
92 #include <linux/unistd.h>
93 #include <linux/cdrom.h>
94 #include <linux/hdreg.h>
95 #include <linux/soundcard.h>
97 #include <linux/mtio.h>
99 #ifdef HAVE_SYS_MOUNT_FSCONFIG
101 * glibc >= 2.36 linux/mount.h conflicts with sys/mount.h,
102 * which in turn prevents use of linux/fs.h. So we have to
103 * define the constants ourselves for now.
105 #define FS_IOC_GETFLAGS _IOR('f', 1, long)
106 #define FS_IOC_SETFLAGS _IOW('f', 2, long)
107 #define FS_IOC_GETVERSION _IOR('v', 1, long)
108 #define FS_IOC_SETVERSION _IOW('v', 2, long)
109 #define FS_IOC_FIEMAP _IOWR('f', 11, struct fiemap)
110 #define FS_IOC32_GETFLAGS _IOR('f', 1, int)
111 #define FS_IOC32_SETFLAGS _IOW('f', 2, int)
112 #define FS_IOC32_GETVERSION _IOR('v', 1, int)
113 #define FS_IOC32_SETVERSION _IOW('v', 2, int)
115 #include <linux/fs.h>
117 #include <linux/fd.h>
118 #if defined(CONFIG_FIEMAP)
119 #include <linux/fiemap.h>
121 #include <linux/fb.h>
122 #if defined(CONFIG_USBFS)
123 #include <linux/usbdevice_fs.h>
124 #include <linux/usb/ch9.h>
126 #include <linux/vt.h>
127 #include <linux/dm-ioctl.h>
128 #include <linux/reboot.h>
129 #include <linux/route.h>
130 #include <linux/filter.h>
131 #include <linux/blkpg.h>
132 #include <netpacket/packet.h>
133 #include <linux/netlink.h>
134 #include <linux/if_alg.h>
135 #include <linux/rtc.h>
136 #include <sound/asound.h>
138 #include <linux/btrfs.h>
141 #include <libdrm/drm.h>
142 #include <libdrm/i915_drm.h>
144 #include "linux_loop.h"
148 #include "user-internals.h"
150 #include "signal-common.h"
152 #include "user-mmap.h"
153 #include "user/safe-syscall.h"
154 #include "qemu/guest-random.h"
155 #include "qemu/selfmap.h"
156 #include "user/syscall-trace.h"
157 #include "special-errno.h"
158 #include "qapi/error.h"
159 #include "fd-trans.h"
163 #define CLONE_IO 0x80000000 /* Clone io context */
166 /* We can't directly call the host clone syscall, because this will
167 * badly confuse libc (breaking mutexes, for example). So we must
168 * divide clone flags into:
169 * * flag combinations that look like pthread_create()
170 * * flag combinations that look like fork()
171 * * flags we can implement within QEMU itself
172 * * flags we can't support and will return an error for
174 /* For thread creation, all these flags must be present; for
175 * fork, none must be present.
177 #define CLONE_THREAD_FLAGS \
178 (CLONE_VM | CLONE_FS | CLONE_FILES | \
179 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
181 /* These flags are ignored:
182 * CLONE_DETACHED is now ignored by the kernel;
183 * CLONE_IO is just an optimisation hint to the I/O scheduler
185 #define CLONE_IGNORED_FLAGS \
186 (CLONE_DETACHED | CLONE_IO)
188 /* Flags for fork which we can implement within QEMU itself */
189 #define CLONE_OPTIONAL_FORK_FLAGS \
190 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
191 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
193 /* Flags for thread creation which we can implement within QEMU itself */
194 #define CLONE_OPTIONAL_THREAD_FLAGS \
195 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
196 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
198 #define CLONE_INVALID_FORK_FLAGS \
199 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
201 #define CLONE_INVALID_THREAD_FLAGS \
202 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
203 CLONE_IGNORED_FLAGS))
205 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
206 * have almost all been allocated. We cannot support any of
207 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
208 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
209 * The checks against the invalid thread masks above will catch these.
210 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
213 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
214 * once. This exercises the codepaths for restart.
216 //#define DEBUG_ERESTARTSYS
218 //#include <linux/msdos_fs.h>
219 #define VFAT_IOCTL_READDIR_BOTH \
220 _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
221 #define VFAT_IOCTL_READDIR_SHORT \
222 _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
232 #define _syscall0(type,name) \
233 static type name (void) \
235 return syscall(__NR_##name); \
238 #define _syscall1(type,name,type1,arg1) \
239 static type name (type1 arg1) \
241 return syscall(__NR_##name, arg1); \
244 #define _syscall2(type,name,type1,arg1,type2,arg2) \
245 static type name (type1 arg1,type2 arg2) \
247 return syscall(__NR_##name, arg1, arg2); \
250 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
251 static type name (type1 arg1,type2 arg2,type3 arg3) \
253 return syscall(__NR_##name, arg1, arg2, arg3); \
256 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
257 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
259 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
262 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
264 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
266 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
270 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
271 type5,arg5,type6,arg6) \
272 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
275 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
279 #define __NR_sys_uname __NR_uname
280 #define __NR_sys_getcwd1 __NR_getcwd
281 #define __NR_sys_getdents __NR_getdents
282 #define __NR_sys_getdents64 __NR_getdents64
283 #define __NR_sys_getpriority __NR_getpriority
284 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
285 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
286 #define __NR_sys_syslog __NR_syslog
287 #if defined(__NR_futex)
288 # define __NR_sys_futex __NR_futex
290 #if defined(__NR_futex_time64)
291 # define __NR_sys_futex_time64 __NR_futex_time64
293 #define __NR_sys_statx __NR_statx
295 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
296 #define __NR__llseek __NR_lseek
299 /* Newer kernel ports have llseek() instead of _llseek() */
300 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
301 #define TARGET_NR__llseek TARGET_NR_llseek
304 /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
305 #ifndef TARGET_O_NONBLOCK_MASK
306 #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
309 #define __NR_sys_gettid __NR_gettid
310 _syscall0(int, sys_gettid
)
312 /* For the 64-bit guest on 32-bit host case we must emulate
313 * getdents using getdents64, because otherwise the host
314 * might hand us back more dirent records than we can fit
315 * into the guest buffer after structure format conversion.
316 * Otherwise we emulate getdents with getdents if the host has it.
318 #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
319 #define EMULATE_GETDENTS_WITH_GETDENTS
322 #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
323 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
325 #if (defined(TARGET_NR_getdents) && \
326 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
327 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
328 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
330 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
331 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
332 loff_t
*, res
, uint
, wh
);
334 _syscall3(int, sys_rt_sigqueueinfo
, pid_t
, pid
, int, sig
, siginfo_t
*, uinfo
)
335 _syscall4(int, sys_rt_tgsigqueueinfo
, pid_t
, pid
, pid_t
, tid
, int, sig
,
337 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
338 #ifdef __NR_exit_group
339 _syscall1(int,exit_group
,int,error_code
)
341 #if defined(__NR_futex)
342 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
343 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
345 #if defined(__NR_futex_time64)
346 _syscall6(int,sys_futex_time64
,int *,uaddr
,int,op
,int,val
,
347 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
349 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
350 _syscall2(int, pidfd_open
, pid_t
, pid
, unsigned int, flags
);
352 #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
353 _syscall4(int, pidfd_send_signal
, int, pidfd
, int, sig
, siginfo_t
*, info
,
354 unsigned int, flags
);
356 #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
357 _syscall3(int, pidfd_getfd
, int, pidfd
, int, targetfd
, unsigned int, flags
);
359 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
360 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
361 unsigned long *, user_mask_ptr
);
362 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
363 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
364 unsigned long *, user_mask_ptr
);
365 /* sched_attr is not defined in glibc */
368 uint32_t sched_policy
;
369 uint64_t sched_flags
;
371 uint32_t sched_priority
;
372 uint64_t sched_runtime
;
373 uint64_t sched_deadline
;
374 uint64_t sched_period
;
375 uint32_t sched_util_min
;
376 uint32_t sched_util_max
;
378 #define __NR_sys_sched_getattr __NR_sched_getattr
379 _syscall4(int, sys_sched_getattr
, pid_t
, pid
, struct sched_attr
*, attr
,
380 unsigned int, size
, unsigned int, flags
);
381 #define __NR_sys_sched_setattr __NR_sched_setattr
382 _syscall3(int, sys_sched_setattr
, pid_t
, pid
, struct sched_attr
*, attr
,
383 unsigned int, flags
);
384 #define __NR_sys_sched_getscheduler __NR_sched_getscheduler
385 _syscall1(int, sys_sched_getscheduler
, pid_t
, pid
);
386 #define __NR_sys_sched_setscheduler __NR_sched_setscheduler
387 _syscall3(int, sys_sched_setscheduler
, pid_t
, pid
, int, policy
,
388 const struct sched_param
*, param
);
389 #define __NR_sys_sched_getparam __NR_sched_getparam
390 _syscall2(int, sys_sched_getparam
, pid_t
, pid
,
391 struct sched_param
*, param
);
392 #define __NR_sys_sched_setparam __NR_sched_setparam
393 _syscall2(int, sys_sched_setparam
, pid_t
, pid
,
394 const struct sched_param
*, param
);
395 #define __NR_sys_getcpu __NR_getcpu
396 _syscall3(int, sys_getcpu
, unsigned *, cpu
, unsigned *, node
, void *, tcache
);
397 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
399 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
400 struct __user_cap_data_struct
*, data
);
401 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
402 struct __user_cap_data_struct
*, data
);
403 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
404 _syscall2(int, ioprio_get
, int, which
, int, who
)
406 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
407 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
409 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
410 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
413 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
414 _syscall5(int, kcmp
, pid_t
, pid1
, pid_t
, pid2
, int, type
,
415 unsigned long, idx1
, unsigned long, idx2
)
419 * It is assumed that struct statx is architecture independent.
421 #if defined(TARGET_NR_statx) && defined(__NR_statx)
422 _syscall5(int, sys_statx
, int, dirfd
, const char *, pathname
, int, flags
,
423 unsigned int, mask
, struct target_statx
*, statxbuf
)
425 #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
426 _syscall2(int, membarrier
, int, cmd
, int, flags
)
429 static const bitmask_transtbl fcntl_flags_tbl
[] = {
430 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
431 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
432 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
433 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
434 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
435 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
436 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
437 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
438 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
439 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
440 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
441 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
442 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
443 #if defined(O_DIRECT)
444 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
446 #if defined(O_NOATIME)
447 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
449 #if defined(O_CLOEXEC)
450 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
453 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
455 #if defined(O_TMPFILE)
456 { TARGET_O_TMPFILE
, TARGET_O_TMPFILE
, O_TMPFILE
, O_TMPFILE
},
458 /* Don't terminate the list prematurely on 64-bit host+guest. */
459 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
460 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
465 _syscall2(int, sys_getcwd1
, char *, buf
, size_t, size
)
467 #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
468 #if defined(__NR_utimensat)
469 #define __NR_sys_utimensat __NR_utimensat
470 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
471 const struct timespec
*,tsp
,int,flags
)
473 static int sys_utimensat(int dirfd
, const char *pathname
,
474 const struct timespec times
[2], int flags
)
480 #endif /* TARGET_NR_utimensat */
482 #ifdef TARGET_NR_renameat2
483 #if defined(__NR_renameat2)
484 #define __NR_sys_renameat2 __NR_renameat2
485 _syscall5(int, sys_renameat2
, int, oldfd
, const char *, old
, int, newfd
,
486 const char *, new, unsigned int, flags
)
488 static int sys_renameat2(int oldfd
, const char *old
,
489 int newfd
, const char *new, int flags
)
492 return renameat(oldfd
, old
, newfd
, new);
498 #endif /* TARGET_NR_renameat2 */
500 #ifdef CONFIG_INOTIFY
501 #include <sys/inotify.h>
503 /* Userspace can usually survive runtime without inotify */
504 #undef TARGET_NR_inotify_init
505 #undef TARGET_NR_inotify_init1
506 #undef TARGET_NR_inotify_add_watch
507 #undef TARGET_NR_inotify_rm_watch
508 #endif /* CONFIG_INOTIFY */
510 #if defined(TARGET_NR_prlimit64)
511 #ifndef __NR_prlimit64
512 # define __NR_prlimit64 -1
514 #define __NR_sys_prlimit64 __NR_prlimit64
515 /* The glibc rlimit structure may not be that used by the underlying syscall */
516 struct host_rlimit64
{
520 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
521 const struct host_rlimit64
*, new_limit
,
522 struct host_rlimit64
*, old_limit
)
526 #if defined(TARGET_NR_timer_create)
527 /* Maximum of 32 active POSIX timers allowed at any one time. */
528 #define GUEST_TIMER_MAX 32
529 static timer_t g_posix_timers
[GUEST_TIMER_MAX
];
530 static int g_posix_timer_allocated
[GUEST_TIMER_MAX
];
532 static inline int next_free_host_timer(void)
535 for (k
= 0; k
< ARRAY_SIZE(g_posix_timer_allocated
); k
++) {
536 if (qatomic_xchg(g_posix_timer_allocated
+ k
, 1) == 0) {
543 static inline void free_host_timer_slot(int id
)
545 qatomic_store_release(g_posix_timer_allocated
+ id
, 0);
549 static inline int host_to_target_errno(int host_errno
)
551 switch (host_errno
) {
552 #define E(X) case X: return TARGET_##X;
553 #include "errnos.c.inc"
560 static inline int target_to_host_errno(int target_errno
)
562 switch (target_errno
) {
563 #define E(X) case TARGET_##X: return X;
564 #include "errnos.c.inc"
571 abi_long
get_errno(abi_long ret
)
574 return -host_to_target_errno(errno
);
579 const char *target_strerror(int err
)
581 if (err
== QEMU_ERESTARTSYS
) {
582 return "To be restarted";
584 if (err
== QEMU_ESIGRETURN
) {
585 return "Successful exit from sigreturn";
588 return strerror(target_to_host_errno(err
));
591 static int check_zeroed_user(abi_long addr
, size_t ksize
, size_t usize
)
595 if (usize
<= ksize
) {
598 for (i
= ksize
; i
< usize
; i
++) {
599 if (get_user_u8(b
, addr
+ i
)) {
600 return -TARGET_EFAULT
;
609 #define safe_syscall0(type, name) \
610 static type safe_##name(void) \
612 return safe_syscall(__NR_##name); \
615 #define safe_syscall1(type, name, type1, arg1) \
616 static type safe_##name(type1 arg1) \
618 return safe_syscall(__NR_##name, arg1); \
621 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
622 static type safe_##name(type1 arg1, type2 arg2) \
624 return safe_syscall(__NR_##name, arg1, arg2); \
627 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
628 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
630 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
633 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
635 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
637 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
640 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
641 type4, arg4, type5, arg5) \
642 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
645 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
648 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
649 type4, arg4, type5, arg5, type6, arg6) \
650 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
651 type5 arg5, type6 arg6) \
653 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
656 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
657 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
658 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
659 int, flags
, mode_t
, mode
)
660 #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
661 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
662 struct rusage
*, rusage
)
664 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
665 int, options
, struct rusage
*, rusage
)
666 safe_syscall3(int, execve
, const char *, filename
, char **, argv
, char **, envp
)
667 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
668 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
669 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
670 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
672 #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
673 safe_syscall5(int, ppoll
, struct pollfd
*, ufds
, unsigned int, nfds
,
674 struct timespec
*, tsp
, const sigset_t
*, sigmask
,
677 safe_syscall6(int, epoll_pwait
, int, epfd
, struct epoll_event
*, events
,
678 int, maxevents
, int, timeout
, const sigset_t
*, sigmask
,
680 #if defined(__NR_futex)
681 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
682 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
684 #if defined(__NR_futex_time64)
685 safe_syscall6(int,futex_time64
,int *,uaddr
,int,op
,int,val
, \
686 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
688 safe_syscall2(int, rt_sigsuspend
, sigset_t
*, newset
, size_t, sigsetsize
)
689 safe_syscall2(int, kill
, pid_t
, pid
, int, sig
)
690 safe_syscall2(int, tkill
, int, tid
, int, sig
)
691 safe_syscall3(int, tgkill
, int, tgid
, int, pid
, int, sig
)
692 safe_syscall3(ssize_t
, readv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
693 safe_syscall3(ssize_t
, writev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
694 safe_syscall5(ssize_t
, preadv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
695 unsigned long, pos_l
, unsigned long, pos_h
)
696 safe_syscall5(ssize_t
, pwritev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
697 unsigned long, pos_l
, unsigned long, pos_h
)
698 safe_syscall3(int, connect
, int, fd
, const struct sockaddr
*, addr
,
700 safe_syscall6(ssize_t
, sendto
, int, fd
, const void *, buf
, size_t, len
,
701 int, flags
, const struct sockaddr
*, addr
, socklen_t
, addrlen
)
702 safe_syscall6(ssize_t
, recvfrom
, int, fd
, void *, buf
, size_t, len
,
703 int, flags
, struct sockaddr
*, addr
, socklen_t
*, addrlen
)
704 safe_syscall3(ssize_t
, sendmsg
, int, fd
, const struct msghdr
*, msg
, int, flags
)
705 safe_syscall3(ssize_t
, recvmsg
, int, fd
, struct msghdr
*, msg
, int, flags
)
706 safe_syscall2(int, flock
, int, fd
, int, operation
)
707 #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
708 safe_syscall4(int, rt_sigtimedwait
, const sigset_t
*, these
, siginfo_t
*, uinfo
,
709 const struct timespec
*, uts
, size_t, sigsetsize
)
711 safe_syscall4(int, accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, len
,
713 #if defined(TARGET_NR_nanosleep)
714 safe_syscall2(int, nanosleep
, const struct timespec
*, req
,
715 struct timespec
*, rem
)
717 #if defined(TARGET_NR_clock_nanosleep) || \
718 defined(TARGET_NR_clock_nanosleep_time64)
719 safe_syscall4(int, clock_nanosleep
, const clockid_t
, clock
, int, flags
,
720 const struct timespec
*, req
, struct timespec
*, rem
)
724 safe_syscall5(int, ipc
, int, call
, long, first
, long, second
, long, third
,
727 safe_syscall6(int, ipc
, int, call
, long, first
, long, second
, long, third
,
728 void *, ptr
, long, fifth
)
732 safe_syscall4(int, msgsnd
, int, msgid
, const void *, msgp
, size_t, sz
,
736 safe_syscall5(int, msgrcv
, int, msgid
, void *, msgp
, size_t, sz
,
737 long, msgtype
, int, flags
)
739 #ifdef __NR_semtimedop
740 safe_syscall4(int, semtimedop
, int, semid
, struct sembuf
*, tsops
,
741 unsigned, nsops
, const struct timespec
*, timeout
)
743 #if defined(TARGET_NR_mq_timedsend) || \
744 defined(TARGET_NR_mq_timedsend_time64)
745 safe_syscall5(int, mq_timedsend
, int, mqdes
, const char *, msg_ptr
,
746 size_t, len
, unsigned, prio
, const struct timespec
*, timeout
)
748 #if defined(TARGET_NR_mq_timedreceive) || \
749 defined(TARGET_NR_mq_timedreceive_time64)
750 safe_syscall5(int, mq_timedreceive
, int, mqdes
, char *, msg_ptr
,
751 size_t, len
, unsigned *, prio
, const struct timespec
*, timeout
)
753 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
754 safe_syscall6(ssize_t
, copy_file_range
, int, infd
, loff_t
*, pinoff
,
755 int, outfd
, loff_t
*, poutoff
, size_t, length
,
759 /* We do ioctl like this rather than via safe_syscall3 to preserve the
760 * "third argument might be integer or pointer or not present" behaviour of
763 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
764 /* Similarly for fcntl. Note that callers must always:
765 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
766 * use the flock64 struct rather than unsuffixed flock
767 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
770 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
772 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
775 static inline int host_to_target_sock_type(int host_type
)
779 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
781 target_type
= TARGET_SOCK_DGRAM
;
784 target_type
= TARGET_SOCK_STREAM
;
787 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
791 #if defined(SOCK_CLOEXEC)
792 if (host_type
& SOCK_CLOEXEC
) {
793 target_type
|= TARGET_SOCK_CLOEXEC
;
797 #if defined(SOCK_NONBLOCK)
798 if (host_type
& SOCK_NONBLOCK
) {
799 target_type
|= TARGET_SOCK_NONBLOCK
;
806 static abi_ulong target_brk
;
807 static abi_ulong target_original_brk
;
808 static abi_ulong brk_page
;
810 void target_set_brk(abi_ulong new_brk
)
812 target_original_brk
= target_brk
= HOST_PAGE_ALIGN(new_brk
);
813 brk_page
= HOST_PAGE_ALIGN(target_brk
);
816 //#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
817 #define DEBUGF_BRK(message, args...)
819 /* do_brk() must return target values and target errnos. */
820 abi_long
do_brk(abi_ulong new_brk
)
822 abi_long mapped_addr
;
823 abi_ulong new_alloc_size
;
825 /* brk pointers are always untagged */
827 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx
") -> ", new_brk
);
830 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (!new_brk)\n", target_brk
);
833 if (new_brk
< target_original_brk
) {
834 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk < target_original_brk)\n",
839 /* If the new brk is less than the highest page reserved to the
840 * target heap allocation, set it and we're almost done... */
841 if (new_brk
<= brk_page
) {
842 /* Heap contents are initialized to zero, as for anonymous
844 if (new_brk
> target_brk
) {
845 memset(g2h_untagged(target_brk
), 0, new_brk
- target_brk
);
847 target_brk
= new_brk
;
848 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (new_brk <= brk_page)\n", target_brk
);
852 /* We need to allocate more memory after the brk... Note that
853 * we don't use MAP_FIXED because that will map over the top of
854 * any existing mapping (like the one with the host libc or qemu
855 * itself); instead we treat "mapped but at wrong address" as
856 * a failure and unmap again.
858 new_alloc_size
= HOST_PAGE_ALIGN(new_brk
- brk_page
);
859 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
860 PROT_READ
|PROT_WRITE
,
861 MAP_ANON
|MAP_PRIVATE
, 0, 0));
863 if (mapped_addr
== brk_page
) {
864 /* Heap contents are initialized to zero, as for anonymous
865 * mapped pages. Technically the new pages are already
866 * initialized to zero since they *are* anonymous mapped
867 * pages, however we have to take care with the contents that
868 * come from the remaining part of the previous page: it may
869 * contains garbage data due to a previous heap usage (grown
871 memset(g2h_untagged(target_brk
), 0, brk_page
- target_brk
);
873 target_brk
= new_brk
;
874 brk_page
= HOST_PAGE_ALIGN(target_brk
);
875 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr == brk_page)\n",
878 } else if (mapped_addr
!= -1) {
879 /* Mapped but at wrong address, meaning there wasn't actually
880 * enough space for this brk.
882 target_munmap(mapped_addr
, new_alloc_size
);
884 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (mapped_addr != -1)\n", target_brk
);
887 DEBUGF_BRK(TARGET_ABI_FMT_lx
" (otherwise)\n", target_brk
);
890 #if defined(TARGET_ALPHA)
891 /* We (partially) emulate OSF/1 on Alpha, which requires we
892 return a proper errno, not an unchanged brk value. */
893 return -TARGET_ENOMEM
;
895 /* For everything else, return the previous break. */
899 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
900 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
901 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
902 abi_ulong target_fds_addr
,
906 abi_ulong b
, *target_fds
;
908 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
909 if (!(target_fds
= lock_user(VERIFY_READ
,
911 sizeof(abi_ulong
) * nw
,
913 return -TARGET_EFAULT
;
917 for (i
= 0; i
< nw
; i
++) {
918 /* grab the abi_ulong */
919 __get_user(b
, &target_fds
[i
]);
920 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
921 /* check the bit inside the abi_ulong */
928 unlock_user(target_fds
, target_fds_addr
, 0);
933 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
934 abi_ulong target_fds_addr
,
937 if (target_fds_addr
) {
938 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
939 return -TARGET_EFAULT
;
947 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
953 abi_ulong
*target_fds
;
955 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
956 if (!(target_fds
= lock_user(VERIFY_WRITE
,
958 sizeof(abi_ulong
) * nw
,
960 return -TARGET_EFAULT
;
963 for (i
= 0; i
< nw
; i
++) {
965 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
966 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
969 __put_user(v
, &target_fds
[i
]);
972 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
978 #if defined(__alpha__)
984 static inline abi_long
host_to_target_clock_t(long ticks
)
986 #if HOST_HZ == TARGET_HZ
989 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
993 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
994 const struct rusage
*rusage
)
996 struct target_rusage
*target_rusage
;
998 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
999 return -TARGET_EFAULT
;
1000 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
1001 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
1002 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
1003 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
1004 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
1005 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
1006 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
1007 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
1008 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
1009 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
1010 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
1011 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
1012 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
1013 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
1014 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
1015 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
1016 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
1017 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
1018 unlock_user_struct(target_rusage
, target_addr
, 1);
1023 #ifdef TARGET_NR_setrlimit
1024 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
1026 abi_ulong target_rlim_swap
;
1029 target_rlim_swap
= tswapal(target_rlim
);
1030 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
1031 return RLIM_INFINITY
;
1033 result
= target_rlim_swap
;
1034 if (target_rlim_swap
!= (rlim_t
)result
)
1035 return RLIM_INFINITY
;
1041 #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
1042 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1044 abi_ulong target_rlim_swap
;
1047 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1048 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1050 target_rlim_swap
= rlim
;
1051 result
= tswapal(target_rlim_swap
);
1057 static inline int target_to_host_resource(int code
)
1060 case TARGET_RLIMIT_AS
:
1062 case TARGET_RLIMIT_CORE
:
1064 case TARGET_RLIMIT_CPU
:
1066 case TARGET_RLIMIT_DATA
:
1068 case TARGET_RLIMIT_FSIZE
:
1069 return RLIMIT_FSIZE
;
1070 case TARGET_RLIMIT_LOCKS
:
1071 return RLIMIT_LOCKS
;
1072 case TARGET_RLIMIT_MEMLOCK
:
1073 return RLIMIT_MEMLOCK
;
1074 case TARGET_RLIMIT_MSGQUEUE
:
1075 return RLIMIT_MSGQUEUE
;
1076 case TARGET_RLIMIT_NICE
:
1078 case TARGET_RLIMIT_NOFILE
:
1079 return RLIMIT_NOFILE
;
1080 case TARGET_RLIMIT_NPROC
:
1081 return RLIMIT_NPROC
;
1082 case TARGET_RLIMIT_RSS
:
1084 case TARGET_RLIMIT_RTPRIO
:
1085 return RLIMIT_RTPRIO
;
1086 #ifdef RLIMIT_RTTIME
1087 case TARGET_RLIMIT_RTTIME
:
1088 return RLIMIT_RTTIME
;
1090 case TARGET_RLIMIT_SIGPENDING
:
1091 return RLIMIT_SIGPENDING
;
1092 case TARGET_RLIMIT_STACK
:
1093 return RLIMIT_STACK
;
1099 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1100 abi_ulong target_tv_addr
)
1102 struct target_timeval
*target_tv
;
1104 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1105 return -TARGET_EFAULT
;
1108 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1109 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1111 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1116 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1117 const struct timeval
*tv
)
1119 struct target_timeval
*target_tv
;
1121 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1122 return -TARGET_EFAULT
;
1125 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1126 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1128 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1133 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
1134 static inline abi_long
copy_from_user_timeval64(struct timeval
*tv
,
1135 abi_ulong target_tv_addr
)
1137 struct target__kernel_sock_timeval
*target_tv
;
1139 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1140 return -TARGET_EFAULT
;
1143 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1144 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1146 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1152 static inline abi_long
copy_to_user_timeval64(abi_ulong target_tv_addr
,
1153 const struct timeval
*tv
)
1155 struct target__kernel_sock_timeval
*target_tv
;
1157 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1158 return -TARGET_EFAULT
;
1161 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1162 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1164 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1169 #if defined(TARGET_NR_futex) || \
1170 defined(TARGET_NR_rt_sigtimedwait) || \
1171 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1172 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1173 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
1174 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
1175 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
1176 defined(TARGET_NR_timer_settime) || \
1177 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
1178 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
1179 abi_ulong target_addr
)
1181 struct target_timespec
*target_ts
;
1183 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1184 return -TARGET_EFAULT
;
1186 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1187 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1188 unlock_user_struct(target_ts
, target_addr
, 0);
1193 #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
1194 defined(TARGET_NR_timer_settime64) || \
1195 defined(TARGET_NR_mq_timedsend_time64) || \
1196 defined(TARGET_NR_mq_timedreceive_time64) || \
1197 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
1198 defined(TARGET_NR_clock_nanosleep_time64) || \
1199 defined(TARGET_NR_rt_sigtimedwait_time64) || \
1200 defined(TARGET_NR_utimensat) || \
1201 defined(TARGET_NR_utimensat_time64) || \
1202 defined(TARGET_NR_semtimedop_time64) || \
1203 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
1204 static inline abi_long
target_to_host_timespec64(struct timespec
*host_ts
,
1205 abi_ulong target_addr
)
1207 struct target__kernel_timespec
*target_ts
;
1209 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1210 return -TARGET_EFAULT
;
1212 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1213 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1214 /* in 32bit mode, this drops the padding */
1215 host_ts
->tv_nsec
= (long)(abi_long
)host_ts
->tv_nsec
;
1216 unlock_user_struct(target_ts
, target_addr
, 0);
1221 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
1222 struct timespec
*host_ts
)
1224 struct target_timespec
*target_ts
;
1226 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1227 return -TARGET_EFAULT
;
1229 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1230 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1231 unlock_user_struct(target_ts
, target_addr
, 1);
1235 static inline abi_long
host_to_target_timespec64(abi_ulong target_addr
,
1236 struct timespec
*host_ts
)
1238 struct target__kernel_timespec
*target_ts
;
1240 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1241 return -TARGET_EFAULT
;
1243 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1244 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1245 unlock_user_struct(target_ts
, target_addr
, 1);
1249 #if defined(TARGET_NR_gettimeofday)
1250 static inline abi_long
copy_to_user_timezone(abi_ulong target_tz_addr
,
1251 struct timezone
*tz
)
1253 struct target_timezone
*target_tz
;
1255 if (!lock_user_struct(VERIFY_WRITE
, target_tz
, target_tz_addr
, 1)) {
1256 return -TARGET_EFAULT
;
1259 __put_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1260 __put_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1262 unlock_user_struct(target_tz
, target_tz_addr
, 1);
1268 #if defined(TARGET_NR_settimeofday)
1269 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1270 abi_ulong target_tz_addr
)
1272 struct target_timezone
*target_tz
;
1274 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1275 return -TARGET_EFAULT
;
1278 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1279 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1281 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1287 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1290 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1291 abi_ulong target_mq_attr_addr
)
1293 struct target_mq_attr
*target_mq_attr
;
1295 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1296 target_mq_attr_addr
, 1))
1297 return -TARGET_EFAULT
;
1299 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1300 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1301 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1302 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1304 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1309 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1310 const struct mq_attr
*attr
)
1312 struct target_mq_attr
*target_mq_attr
;
1314 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1315 target_mq_attr_addr
, 0))
1316 return -TARGET_EFAULT
;
1318 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1319 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1320 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1321 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1323 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1329 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1330 /* do_select() must return target values and target errnos. */
1331 static abi_long
do_select(int n
,
1332 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1333 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1335 fd_set rfds
, wfds
, efds
;
1336 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1338 struct timespec ts
, *ts_ptr
;
1341 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1345 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1349 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1354 if (target_tv_addr
) {
1355 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1356 return -TARGET_EFAULT
;
1357 ts
.tv_sec
= tv
.tv_sec
;
1358 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1364 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1367 if (!is_error(ret
)) {
1368 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1369 return -TARGET_EFAULT
;
1370 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1371 return -TARGET_EFAULT
;
1372 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1373 return -TARGET_EFAULT
;
1375 if (target_tv_addr
) {
1376 tv
.tv_sec
= ts
.tv_sec
;
1377 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1378 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1379 return -TARGET_EFAULT
;
1387 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1388 static abi_long
do_old_select(abi_ulong arg1
)
1390 struct target_sel_arg_struct
*sel
;
1391 abi_ulong inp
, outp
, exp
, tvp
;
1394 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1)) {
1395 return -TARGET_EFAULT
;
1398 nsel
= tswapal(sel
->n
);
1399 inp
= tswapal(sel
->inp
);
1400 outp
= tswapal(sel
->outp
);
1401 exp
= tswapal(sel
->exp
);
1402 tvp
= tswapal(sel
->tvp
);
1404 unlock_user_struct(sel
, arg1
, 0);
1406 return do_select(nsel
, inp
, outp
, exp
, tvp
);
1411 #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
1412 static abi_long
do_pselect6(abi_long arg1
, abi_long arg2
, abi_long arg3
,
1413 abi_long arg4
, abi_long arg5
, abi_long arg6
,
1416 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
1417 fd_set rfds
, wfds
, efds
;
1418 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1419 struct timespec ts
, *ts_ptr
;
1423 * The 6th arg is actually two args smashed together,
1424 * so we cannot use the C library.
1431 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
1439 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1443 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1447 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1453 * This takes a timespec, and not a timeval, so we cannot
1454 * use the do_select() helper ...
1458 if (target_to_host_timespec64(&ts
, ts_addr
)) {
1459 return -TARGET_EFAULT
;
1462 if (target_to_host_timespec(&ts
, ts_addr
)) {
1463 return -TARGET_EFAULT
;
1471 /* Extract the two packed args for the sigset */
1474 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
1476 return -TARGET_EFAULT
;
1478 arg_sigset
= tswapal(arg7
[0]);
1479 arg_sigsize
= tswapal(arg7
[1]);
1480 unlock_user(arg7
, arg6
, 0);
1483 ret
= process_sigsuspend_mask(&sig
.set
, arg_sigset
, arg_sigsize
);
1488 sig
.size
= SIGSET_T_SIZE
;
1492 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1496 finish_sigsuspend_mask(ret
);
1499 if (!is_error(ret
)) {
1500 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
)) {
1501 return -TARGET_EFAULT
;
1503 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
)) {
1504 return -TARGET_EFAULT
;
1506 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
)) {
1507 return -TARGET_EFAULT
;
1510 if (ts_addr
&& host_to_target_timespec64(ts_addr
, &ts
)) {
1511 return -TARGET_EFAULT
;
1514 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
)) {
1515 return -TARGET_EFAULT
;
1523 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
1524 defined(TARGET_NR_ppoll_time64)
1525 static abi_long
do_ppoll(abi_long arg1
, abi_long arg2
, abi_long arg3
,
1526 abi_long arg4
, abi_long arg5
, bool ppoll
, bool time64
)
1528 struct target_pollfd
*target_pfd
;
1529 unsigned int nfds
= arg2
;
1537 if (nfds
> (INT_MAX
/ sizeof(struct target_pollfd
))) {
1538 return -TARGET_EINVAL
;
1540 target_pfd
= lock_user(VERIFY_WRITE
, arg1
,
1541 sizeof(struct target_pollfd
) * nfds
, 1);
1543 return -TARGET_EFAULT
;
1546 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
1547 for (i
= 0; i
< nfds
; i
++) {
1548 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
1549 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
1553 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
1554 sigset_t
*set
= NULL
;
1558 if (target_to_host_timespec64(timeout_ts
, arg3
)) {
1559 unlock_user(target_pfd
, arg1
, 0);
1560 return -TARGET_EFAULT
;
1563 if (target_to_host_timespec(timeout_ts
, arg3
)) {
1564 unlock_user(target_pfd
, arg1
, 0);
1565 return -TARGET_EFAULT
;
1573 ret
= process_sigsuspend_mask(&set
, arg4
, arg5
);
1575 unlock_user(target_pfd
, arg1
, 0);
1580 ret
= get_errno(safe_ppoll(pfd
, nfds
, timeout_ts
,
1581 set
, SIGSET_T_SIZE
));
1584 finish_sigsuspend_mask(ret
);
1586 if (!is_error(ret
) && arg3
) {
1588 if (host_to_target_timespec64(arg3
, timeout_ts
)) {
1589 return -TARGET_EFAULT
;
1592 if (host_to_target_timespec(arg3
, timeout_ts
)) {
1593 return -TARGET_EFAULT
;
1598 struct timespec ts
, *pts
;
1601 /* Convert ms to secs, ns */
1602 ts
.tv_sec
= arg3
/ 1000;
1603 ts
.tv_nsec
= (arg3
% 1000) * 1000000LL;
1606 /* -ve poll() timeout means "infinite" */
1609 ret
= get_errno(safe_ppoll(pfd
, nfds
, pts
, NULL
, 0));
1612 if (!is_error(ret
)) {
1613 for (i
= 0; i
< nfds
; i
++) {
1614 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
1617 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
1622 static abi_long
do_pipe(CPUArchState
*cpu_env
, abi_ulong pipedes
,
1623 int flags
, int is_pipe2
)
1627 ret
= pipe2(host_pipe
, flags
);
1630 return get_errno(ret
);
1632 /* Several targets have special calling conventions for the original
1633 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1635 #if defined(TARGET_ALPHA)
1636 cpu_env
->ir
[IR_A4
] = host_pipe
[1];
1637 return host_pipe
[0];
1638 #elif defined(TARGET_MIPS)
1639 cpu_env
->active_tc
.gpr
[3] = host_pipe
[1];
1640 return host_pipe
[0];
1641 #elif defined(TARGET_SH4)
1642 cpu_env
->gregs
[1] = host_pipe
[1];
1643 return host_pipe
[0];
1644 #elif defined(TARGET_SPARC)
1645 cpu_env
->regwptr
[1] = host_pipe
[1];
1646 return host_pipe
[0];
1650 if (put_user_s32(host_pipe
[0], pipedes
)
1651 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(abi_int
)))
1652 return -TARGET_EFAULT
;
1653 return get_errno(ret
);
1656 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1657 abi_ulong target_addr
,
1660 struct target_ip_mreqn
*target_smreqn
;
1662 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1664 return -TARGET_EFAULT
;
1665 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1666 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1667 if (len
== sizeof(struct target_ip_mreqn
))
1668 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1669 unlock_user(target_smreqn
, target_addr
, 0);
1674 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1675 abi_ulong target_addr
,
1678 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1679 sa_family_t sa_family
;
1680 struct target_sockaddr
*target_saddr
;
1682 if (fd_trans_target_to_host_addr(fd
)) {
1683 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1686 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1688 return -TARGET_EFAULT
;
1690 sa_family
= tswap16(target_saddr
->sa_family
);
1692 /* Oops. The caller might send a incomplete sun_path; sun_path
1693 * must be terminated by \0 (see the manual page), but
1694 * unfortunately it is quite common to specify sockaddr_un
1695 * length as "strlen(x->sun_path)" while it should be
1696 * "strlen(...) + 1". We'll fix that here if needed.
1697 * Linux kernel has a similar feature.
1700 if (sa_family
== AF_UNIX
) {
1701 if (len
< unix_maxlen
&& len
> 0) {
1702 char *cp
= (char*)target_saddr
;
1704 if ( cp
[len
-1] && !cp
[len
] )
1707 if (len
> unix_maxlen
)
1711 memcpy(addr
, target_saddr
, len
);
1712 addr
->sa_family
= sa_family
;
1713 if (sa_family
== AF_NETLINK
) {
1714 struct sockaddr_nl
*nladdr
;
1716 nladdr
= (struct sockaddr_nl
*)addr
;
1717 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1718 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1719 } else if (sa_family
== AF_PACKET
) {
1720 struct target_sockaddr_ll
*lladdr
;
1722 lladdr
= (struct target_sockaddr_ll
*)addr
;
1723 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1724 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1726 unlock_user(target_saddr
, target_addr
, 0);
1731 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1732 struct sockaddr
*addr
,
1735 struct target_sockaddr
*target_saddr
;
1742 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1744 return -TARGET_EFAULT
;
1745 memcpy(target_saddr
, addr
, len
);
1746 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1747 sizeof(target_saddr
->sa_family
)) {
1748 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1750 if (addr
->sa_family
== AF_NETLINK
&&
1751 len
>= sizeof(struct target_sockaddr_nl
)) {
1752 struct target_sockaddr_nl
*target_nl
=
1753 (struct target_sockaddr_nl
*)target_saddr
;
1754 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1755 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1756 } else if (addr
->sa_family
== AF_PACKET
) {
1757 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1758 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1759 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1760 } else if (addr
->sa_family
== AF_INET6
&&
1761 len
>= sizeof(struct target_sockaddr_in6
)) {
1762 struct target_sockaddr_in6
*target_in6
=
1763 (struct target_sockaddr_in6
*)target_saddr
;
1764 target_in6
->sin6_scope_id
= tswap16(target_in6
->sin6_scope_id
);
1766 unlock_user(target_saddr
, target_addr
, len
);
1771 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1772 struct target_msghdr
*target_msgh
)
1774 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1775 abi_long msg_controllen
;
1776 abi_ulong target_cmsg_addr
;
1777 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1778 socklen_t space
= 0;
1780 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1781 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1783 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1784 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1785 target_cmsg_start
= target_cmsg
;
1787 return -TARGET_EFAULT
;
1789 while (cmsg
&& target_cmsg
) {
1790 void *data
= CMSG_DATA(cmsg
);
1791 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1793 int len
= tswapal(target_cmsg
->cmsg_len
)
1794 - sizeof(struct target_cmsghdr
);
1796 space
+= CMSG_SPACE(len
);
1797 if (space
> msgh
->msg_controllen
) {
1798 space
-= CMSG_SPACE(len
);
1799 /* This is a QEMU bug, since we allocated the payload
1800 * area ourselves (unlike overflow in host-to-target
1801 * conversion, which is just the guest giving us a buffer
1802 * that's too small). It can't happen for the payload types
1803 * we currently support; if it becomes an issue in future
1804 * we would need to improve our allocation strategy to
1805 * something more intelligent than "twice the size of the
1806 * target buffer we're reading from".
1808 qemu_log_mask(LOG_UNIMP
,
1809 ("Unsupported ancillary data %d/%d: "
1810 "unhandled msg size\n"),
1811 tswap32(target_cmsg
->cmsg_level
),
1812 tswap32(target_cmsg
->cmsg_type
));
1816 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1817 cmsg
->cmsg_level
= SOL_SOCKET
;
1819 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1821 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1822 cmsg
->cmsg_len
= CMSG_LEN(len
);
1824 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1825 int *fd
= (int *)data
;
1826 int *target_fd
= (int *)target_data
;
1827 int i
, numfds
= len
/ sizeof(int);
1829 for (i
= 0; i
< numfds
; i
++) {
1830 __get_user(fd
[i
], target_fd
+ i
);
1832 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1833 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1834 struct ucred
*cred
= (struct ucred
*)data
;
1835 struct target_ucred
*target_cred
=
1836 (struct target_ucred
*)target_data
;
1838 __get_user(cred
->pid
, &target_cred
->pid
);
1839 __get_user(cred
->uid
, &target_cred
->uid
);
1840 __get_user(cred
->gid
, &target_cred
->gid
);
1842 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1843 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1844 memcpy(data
, target_data
, len
);
1847 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1848 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1851 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1853 msgh
->msg_controllen
= space
;
1857 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1858 struct msghdr
*msgh
)
1860 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1861 abi_long msg_controllen
;
1862 abi_ulong target_cmsg_addr
;
1863 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1864 socklen_t space
= 0;
1866 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1867 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1869 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1870 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1871 target_cmsg_start
= target_cmsg
;
1873 return -TARGET_EFAULT
;
1875 while (cmsg
&& target_cmsg
) {
1876 void *data
= CMSG_DATA(cmsg
);
1877 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1879 int len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
1880 int tgt_len
, tgt_space
;
1882 /* We never copy a half-header but may copy half-data;
1883 * this is Linux's behaviour in put_cmsg(). Note that
1884 * truncation here is a guest problem (which we report
1885 * to the guest via the CTRUNC bit), unlike truncation
1886 * in target_to_host_cmsg, which is a QEMU bug.
1888 if (msg_controllen
< sizeof(struct target_cmsghdr
)) {
1889 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1893 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1894 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1896 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1898 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1900 /* Payload types which need a different size of payload on
1901 * the target must adjust tgt_len here.
1904 switch (cmsg
->cmsg_level
) {
1906 switch (cmsg
->cmsg_type
) {
1908 tgt_len
= sizeof(struct target_timeval
);
1918 if (msg_controllen
< TARGET_CMSG_LEN(tgt_len
)) {
1919 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1920 tgt_len
= msg_controllen
- sizeof(struct target_cmsghdr
);
1923 /* We must now copy-and-convert len bytes of payload
1924 * into tgt_len bytes of destination space. Bear in mind
1925 * that in both source and destination we may be dealing
1926 * with a truncated value!
1928 switch (cmsg
->cmsg_level
) {
1930 switch (cmsg
->cmsg_type
) {
1933 int *fd
= (int *)data
;
1934 int *target_fd
= (int *)target_data
;
1935 int i
, numfds
= tgt_len
/ sizeof(int);
1937 for (i
= 0; i
< numfds
; i
++) {
1938 __put_user(fd
[i
], target_fd
+ i
);
1944 struct timeval
*tv
= (struct timeval
*)data
;
1945 struct target_timeval
*target_tv
=
1946 (struct target_timeval
*)target_data
;
1948 if (len
!= sizeof(struct timeval
) ||
1949 tgt_len
!= sizeof(struct target_timeval
)) {
1953 /* copy struct timeval to target */
1954 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1955 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1958 case SCM_CREDENTIALS
:
1960 struct ucred
*cred
= (struct ucred
*)data
;
1961 struct target_ucred
*target_cred
=
1962 (struct target_ucred
*)target_data
;
1964 __put_user(cred
->pid
, &target_cred
->pid
);
1965 __put_user(cred
->uid
, &target_cred
->uid
);
1966 __put_user(cred
->gid
, &target_cred
->gid
);
1975 switch (cmsg
->cmsg_type
) {
1978 uint32_t *v
= (uint32_t *)data
;
1979 uint32_t *t_int
= (uint32_t *)target_data
;
1981 if (len
!= sizeof(uint32_t) ||
1982 tgt_len
!= sizeof(uint32_t)) {
1985 __put_user(*v
, t_int
);
1991 struct sock_extended_err ee
;
1992 struct sockaddr_in offender
;
1994 struct errhdr_t
*errh
= (struct errhdr_t
*)data
;
1995 struct errhdr_t
*target_errh
=
1996 (struct errhdr_t
*)target_data
;
1998 if (len
!= sizeof(struct errhdr_t
) ||
1999 tgt_len
!= sizeof(struct errhdr_t
)) {
2002 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
2003 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
2004 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
2005 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
2006 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
2007 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
2008 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
2009 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
2010 (void *) &errh
->offender
, sizeof(errh
->offender
));
2019 switch (cmsg
->cmsg_type
) {
2022 uint32_t *v
= (uint32_t *)data
;
2023 uint32_t *t_int
= (uint32_t *)target_data
;
2025 if (len
!= sizeof(uint32_t) ||
2026 tgt_len
!= sizeof(uint32_t)) {
2029 __put_user(*v
, t_int
);
2035 struct sock_extended_err ee
;
2036 struct sockaddr_in6 offender
;
2038 struct errhdr6_t
*errh
= (struct errhdr6_t
*)data
;
2039 struct errhdr6_t
*target_errh
=
2040 (struct errhdr6_t
*)target_data
;
2042 if (len
!= sizeof(struct errhdr6_t
) ||
2043 tgt_len
!= sizeof(struct errhdr6_t
)) {
2046 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
2047 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
2048 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
2049 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
2050 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
2051 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
2052 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
2053 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
2054 (void *) &errh
->offender
, sizeof(errh
->offender
));
2064 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
2065 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
2066 memcpy(target_data
, data
, MIN(len
, tgt_len
));
2067 if (tgt_len
> len
) {
2068 memset(target_data
+ len
, 0, tgt_len
- len
);
2072 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(tgt_len
));
2073 tgt_space
= TARGET_CMSG_SPACE(tgt_len
);
2074 if (msg_controllen
< tgt_space
) {
2075 tgt_space
= msg_controllen
;
2077 msg_controllen
-= tgt_space
;
2079 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
2080 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
2083 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
2085 target_msgh
->msg_controllen
= tswapal(space
);
2089 /* do_setsockopt() Must return target values and target errnos. */
2090 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
2091 abi_ulong optval_addr
, socklen_t optlen
)
2095 struct ip_mreqn
*ip_mreq
;
2096 struct ip_mreq_source
*ip_mreq_source
;
2101 /* TCP and UDP options all take an 'int' value. */
2102 if (optlen
< sizeof(uint32_t))
2103 return -TARGET_EINVAL
;
2105 if (get_user_u32(val
, optval_addr
))
2106 return -TARGET_EFAULT
;
2107 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2114 case IP_ROUTER_ALERT
:
2118 case IP_MTU_DISCOVER
:
2125 case IP_MULTICAST_TTL
:
2126 case IP_MULTICAST_LOOP
:
2128 if (optlen
>= sizeof(uint32_t)) {
2129 if (get_user_u32(val
, optval_addr
))
2130 return -TARGET_EFAULT
;
2131 } else if (optlen
>= 1) {
2132 if (get_user_u8(val
, optval_addr
))
2133 return -TARGET_EFAULT
;
2135 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2137 case IP_ADD_MEMBERSHIP
:
2138 case IP_DROP_MEMBERSHIP
:
2139 if (optlen
< sizeof (struct target_ip_mreq
) ||
2140 optlen
> sizeof (struct target_ip_mreqn
))
2141 return -TARGET_EINVAL
;
2143 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
2144 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
2145 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
2148 case IP_BLOCK_SOURCE
:
2149 case IP_UNBLOCK_SOURCE
:
2150 case IP_ADD_SOURCE_MEMBERSHIP
:
2151 case IP_DROP_SOURCE_MEMBERSHIP
:
2152 if (optlen
!= sizeof (struct target_ip_mreq_source
))
2153 return -TARGET_EINVAL
;
2155 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2156 if (!ip_mreq_source
) {
2157 return -TARGET_EFAULT
;
2159 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
2160 unlock_user (ip_mreq_source
, optval_addr
, 0);
2169 case IPV6_MTU_DISCOVER
:
2172 case IPV6_RECVPKTINFO
:
2173 case IPV6_UNICAST_HOPS
:
2174 case IPV6_MULTICAST_HOPS
:
2175 case IPV6_MULTICAST_LOOP
:
2177 case IPV6_RECVHOPLIMIT
:
2178 case IPV6_2292HOPLIMIT
:
2181 case IPV6_2292PKTINFO
:
2182 case IPV6_RECVTCLASS
:
2183 case IPV6_RECVRTHDR
:
2184 case IPV6_2292RTHDR
:
2185 case IPV6_RECVHOPOPTS
:
2186 case IPV6_2292HOPOPTS
:
2187 case IPV6_RECVDSTOPTS
:
2188 case IPV6_2292DSTOPTS
:
2190 case IPV6_ADDR_PREFERENCES
:
2191 #ifdef IPV6_RECVPATHMTU
2192 case IPV6_RECVPATHMTU
:
2194 #ifdef IPV6_TRANSPARENT
2195 case IPV6_TRANSPARENT
:
2197 #ifdef IPV6_FREEBIND
2200 #ifdef IPV6_RECVORIGDSTADDR
2201 case IPV6_RECVORIGDSTADDR
:
2204 if (optlen
< sizeof(uint32_t)) {
2205 return -TARGET_EINVAL
;
2207 if (get_user_u32(val
, optval_addr
)) {
2208 return -TARGET_EFAULT
;
2210 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2211 &val
, sizeof(val
)));
2215 struct in6_pktinfo pki
;
2217 if (optlen
< sizeof(pki
)) {
2218 return -TARGET_EINVAL
;
2221 if (copy_from_user(&pki
, optval_addr
, sizeof(pki
))) {
2222 return -TARGET_EFAULT
;
2225 pki
.ipi6_ifindex
= tswap32(pki
.ipi6_ifindex
);
2227 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2228 &pki
, sizeof(pki
)));
2231 case IPV6_ADD_MEMBERSHIP
:
2232 case IPV6_DROP_MEMBERSHIP
:
2234 struct ipv6_mreq ipv6mreq
;
2236 if (optlen
< sizeof(ipv6mreq
)) {
2237 return -TARGET_EINVAL
;
2240 if (copy_from_user(&ipv6mreq
, optval_addr
, sizeof(ipv6mreq
))) {
2241 return -TARGET_EFAULT
;
2244 ipv6mreq
.ipv6mr_interface
= tswap32(ipv6mreq
.ipv6mr_interface
);
2246 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2247 &ipv6mreq
, sizeof(ipv6mreq
)));
2258 struct icmp6_filter icmp6f
;
2260 if (optlen
> sizeof(icmp6f
)) {
2261 optlen
= sizeof(icmp6f
);
2264 if (copy_from_user(&icmp6f
, optval_addr
, optlen
)) {
2265 return -TARGET_EFAULT
;
2268 for (val
= 0; val
< 8; val
++) {
2269 icmp6f
.data
[val
] = tswap32(icmp6f
.data
[val
]);
2272 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2284 /* those take an u32 value */
2285 if (optlen
< sizeof(uint32_t)) {
2286 return -TARGET_EINVAL
;
2289 if (get_user_u32(val
, optval_addr
)) {
2290 return -TARGET_EFAULT
;
2292 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2293 &val
, sizeof(val
)));
2300 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2305 char *alg_key
= g_malloc(optlen
);
2308 return -TARGET_ENOMEM
;
2310 if (copy_from_user(alg_key
, optval_addr
, optlen
)) {
2312 return -TARGET_EFAULT
;
2314 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2319 case ALG_SET_AEAD_AUTHSIZE
:
2321 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2330 case TARGET_SOL_SOCKET
:
2332 case TARGET_SO_RCVTIMEO
:
2336 optname
= SO_RCVTIMEO
;
2339 if (optlen
!= sizeof(struct target_timeval
)) {
2340 return -TARGET_EINVAL
;
2343 if (copy_from_user_timeval(&tv
, optval_addr
)) {
2344 return -TARGET_EFAULT
;
2347 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2351 case TARGET_SO_SNDTIMEO
:
2352 optname
= SO_SNDTIMEO
;
2354 case TARGET_SO_ATTACH_FILTER
:
2356 struct target_sock_fprog
*tfprog
;
2357 struct target_sock_filter
*tfilter
;
2358 struct sock_fprog fprog
;
2359 struct sock_filter
*filter
;
2362 if (optlen
!= sizeof(*tfprog
)) {
2363 return -TARGET_EINVAL
;
2365 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
2366 return -TARGET_EFAULT
;
2368 if (!lock_user_struct(VERIFY_READ
, tfilter
,
2369 tswapal(tfprog
->filter
), 0)) {
2370 unlock_user_struct(tfprog
, optval_addr
, 1);
2371 return -TARGET_EFAULT
;
2374 fprog
.len
= tswap16(tfprog
->len
);
2375 filter
= g_try_new(struct sock_filter
, fprog
.len
);
2376 if (filter
== NULL
) {
2377 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2378 unlock_user_struct(tfprog
, optval_addr
, 1);
2379 return -TARGET_ENOMEM
;
2381 for (i
= 0; i
< fprog
.len
; i
++) {
2382 filter
[i
].code
= tswap16(tfilter
[i
].code
);
2383 filter
[i
].jt
= tfilter
[i
].jt
;
2384 filter
[i
].jf
= tfilter
[i
].jf
;
2385 filter
[i
].k
= tswap32(tfilter
[i
].k
);
2387 fprog
.filter
= filter
;
2389 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
2390 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
2393 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2394 unlock_user_struct(tfprog
, optval_addr
, 1);
2397 case TARGET_SO_BINDTODEVICE
:
2399 char *dev_ifname
, *addr_ifname
;
2401 if (optlen
> IFNAMSIZ
- 1) {
2402 optlen
= IFNAMSIZ
- 1;
2404 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2406 return -TARGET_EFAULT
;
2408 optname
= SO_BINDTODEVICE
;
2409 addr_ifname
= alloca(IFNAMSIZ
);
2410 memcpy(addr_ifname
, dev_ifname
, optlen
);
2411 addr_ifname
[optlen
] = 0;
2412 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2413 addr_ifname
, optlen
));
2414 unlock_user (dev_ifname
, optval_addr
, 0);
2417 case TARGET_SO_LINGER
:
2420 struct target_linger
*tlg
;
2422 if (optlen
!= sizeof(struct target_linger
)) {
2423 return -TARGET_EINVAL
;
2425 if (!lock_user_struct(VERIFY_READ
, tlg
, optval_addr
, 1)) {
2426 return -TARGET_EFAULT
;
2428 __get_user(lg
.l_onoff
, &tlg
->l_onoff
);
2429 __get_user(lg
.l_linger
, &tlg
->l_linger
);
2430 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, SO_LINGER
,
2432 unlock_user_struct(tlg
, optval_addr
, 0);
2435 /* Options with 'int' argument. */
2436 case TARGET_SO_DEBUG
:
2439 case TARGET_SO_REUSEADDR
:
2440 optname
= SO_REUSEADDR
;
2443 case TARGET_SO_REUSEPORT
:
2444 optname
= SO_REUSEPORT
;
2447 case TARGET_SO_TYPE
:
2450 case TARGET_SO_ERROR
:
2453 case TARGET_SO_DONTROUTE
:
2454 optname
= SO_DONTROUTE
;
2456 case TARGET_SO_BROADCAST
:
2457 optname
= SO_BROADCAST
;
2459 case TARGET_SO_SNDBUF
:
2460 optname
= SO_SNDBUF
;
2462 case TARGET_SO_SNDBUFFORCE
:
2463 optname
= SO_SNDBUFFORCE
;
2465 case TARGET_SO_RCVBUF
:
2466 optname
= SO_RCVBUF
;
2468 case TARGET_SO_RCVBUFFORCE
:
2469 optname
= SO_RCVBUFFORCE
;
2471 case TARGET_SO_KEEPALIVE
:
2472 optname
= SO_KEEPALIVE
;
2474 case TARGET_SO_OOBINLINE
:
2475 optname
= SO_OOBINLINE
;
2477 case TARGET_SO_NO_CHECK
:
2478 optname
= SO_NO_CHECK
;
2480 case TARGET_SO_PRIORITY
:
2481 optname
= SO_PRIORITY
;
2484 case TARGET_SO_BSDCOMPAT
:
2485 optname
= SO_BSDCOMPAT
;
2488 case TARGET_SO_PASSCRED
:
2489 optname
= SO_PASSCRED
;
2491 case TARGET_SO_PASSSEC
:
2492 optname
= SO_PASSSEC
;
2494 case TARGET_SO_TIMESTAMP
:
2495 optname
= SO_TIMESTAMP
;
2497 case TARGET_SO_RCVLOWAT
:
2498 optname
= SO_RCVLOWAT
;
2503 if (optlen
< sizeof(uint32_t))
2504 return -TARGET_EINVAL
;
2506 if (get_user_u32(val
, optval_addr
))
2507 return -TARGET_EFAULT
;
2508 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
2513 case NETLINK_PKTINFO
:
2514 case NETLINK_ADD_MEMBERSHIP
:
2515 case NETLINK_DROP_MEMBERSHIP
:
2516 case NETLINK_BROADCAST_ERROR
:
2517 case NETLINK_NO_ENOBUFS
:
2518 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2519 case NETLINK_LISTEN_ALL_NSID
:
2520 case NETLINK_CAP_ACK
:
2521 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2522 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2523 case NETLINK_EXT_ACK
:
2524 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2525 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2526 case NETLINK_GET_STRICT_CHK
:
2527 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2533 if (optlen
< sizeof(uint32_t)) {
2534 return -TARGET_EINVAL
;
2536 if (get_user_u32(val
, optval_addr
)) {
2537 return -TARGET_EFAULT
;
2539 ret
= get_errno(setsockopt(sockfd
, SOL_NETLINK
, optname
, &val
,
2542 #endif /* SOL_NETLINK */
2545 qemu_log_mask(LOG_UNIMP
, "Unsupported setsockopt level=%d optname=%d\n",
2547 ret
= -TARGET_ENOPROTOOPT
;
2552 /* do_getsockopt() Must return target values and target errnos. */
2553 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
2554 abi_ulong optval_addr
, abi_ulong optlen
)
2561 case TARGET_SOL_SOCKET
:
2564 /* These don't just return a single integer */
2565 case TARGET_SO_PEERNAME
:
2567 case TARGET_SO_RCVTIMEO
: {
2571 optname
= SO_RCVTIMEO
;
2574 if (get_user_u32(len
, optlen
)) {
2575 return -TARGET_EFAULT
;
2578 return -TARGET_EINVAL
;
2582 ret
= get_errno(getsockopt(sockfd
, level
, optname
,
2587 if (len
> sizeof(struct target_timeval
)) {
2588 len
= sizeof(struct target_timeval
);
2590 if (copy_to_user_timeval(optval_addr
, &tv
)) {
2591 return -TARGET_EFAULT
;
2593 if (put_user_u32(len
, optlen
)) {
2594 return -TARGET_EFAULT
;
2598 case TARGET_SO_SNDTIMEO
:
2599 optname
= SO_SNDTIMEO
;
2601 case TARGET_SO_PEERCRED
: {
2604 struct target_ucred
*tcr
;
2606 if (get_user_u32(len
, optlen
)) {
2607 return -TARGET_EFAULT
;
2610 return -TARGET_EINVAL
;
2614 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
2622 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
2623 return -TARGET_EFAULT
;
2625 __put_user(cr
.pid
, &tcr
->pid
);
2626 __put_user(cr
.uid
, &tcr
->uid
);
2627 __put_user(cr
.gid
, &tcr
->gid
);
2628 unlock_user_struct(tcr
, optval_addr
, 1);
2629 if (put_user_u32(len
, optlen
)) {
2630 return -TARGET_EFAULT
;
2634 case TARGET_SO_PEERSEC
: {
2637 if (get_user_u32(len
, optlen
)) {
2638 return -TARGET_EFAULT
;
2641 return -TARGET_EINVAL
;
2643 name
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 0);
2645 return -TARGET_EFAULT
;
2648 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERSEC
,
2650 if (put_user_u32(lv
, optlen
)) {
2651 ret
= -TARGET_EFAULT
;
2653 unlock_user(name
, optval_addr
, lv
);
2656 case TARGET_SO_LINGER
:
2660 struct target_linger
*tlg
;
2662 if (get_user_u32(len
, optlen
)) {
2663 return -TARGET_EFAULT
;
2666 return -TARGET_EINVAL
;
2670 ret
= get_errno(getsockopt(sockfd
, level
, SO_LINGER
,
2678 if (!lock_user_struct(VERIFY_WRITE
, tlg
, optval_addr
, 0)) {
2679 return -TARGET_EFAULT
;
2681 __put_user(lg
.l_onoff
, &tlg
->l_onoff
);
2682 __put_user(lg
.l_linger
, &tlg
->l_linger
);
2683 unlock_user_struct(tlg
, optval_addr
, 1);
2684 if (put_user_u32(len
, optlen
)) {
2685 return -TARGET_EFAULT
;
2689 /* Options with 'int' argument. */
2690 case TARGET_SO_DEBUG
:
2693 case TARGET_SO_REUSEADDR
:
2694 optname
= SO_REUSEADDR
;
2697 case TARGET_SO_REUSEPORT
:
2698 optname
= SO_REUSEPORT
;
2701 case TARGET_SO_TYPE
:
2704 case TARGET_SO_ERROR
:
2707 case TARGET_SO_DONTROUTE
:
2708 optname
= SO_DONTROUTE
;
2710 case TARGET_SO_BROADCAST
:
2711 optname
= SO_BROADCAST
;
2713 case TARGET_SO_SNDBUF
:
2714 optname
= SO_SNDBUF
;
2716 case TARGET_SO_RCVBUF
:
2717 optname
= SO_RCVBUF
;
2719 case TARGET_SO_KEEPALIVE
:
2720 optname
= SO_KEEPALIVE
;
2722 case TARGET_SO_OOBINLINE
:
2723 optname
= SO_OOBINLINE
;
2725 case TARGET_SO_NO_CHECK
:
2726 optname
= SO_NO_CHECK
;
2728 case TARGET_SO_PRIORITY
:
2729 optname
= SO_PRIORITY
;
2732 case TARGET_SO_BSDCOMPAT
:
2733 optname
= SO_BSDCOMPAT
;
2736 case TARGET_SO_PASSCRED
:
2737 optname
= SO_PASSCRED
;
2739 case TARGET_SO_TIMESTAMP
:
2740 optname
= SO_TIMESTAMP
;
2742 case TARGET_SO_RCVLOWAT
:
2743 optname
= SO_RCVLOWAT
;
2745 case TARGET_SO_ACCEPTCONN
:
2746 optname
= SO_ACCEPTCONN
;
2748 case TARGET_SO_PROTOCOL
:
2749 optname
= SO_PROTOCOL
;
2751 case TARGET_SO_DOMAIN
:
2752 optname
= SO_DOMAIN
;
2760 /* TCP and UDP options all take an 'int' value. */
2762 if (get_user_u32(len
, optlen
))
2763 return -TARGET_EFAULT
;
2765 return -TARGET_EINVAL
;
2767 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2770 if (optname
== SO_TYPE
) {
2771 val
= host_to_target_sock_type(val
);
2776 if (put_user_u32(val
, optval_addr
))
2777 return -TARGET_EFAULT
;
2779 if (put_user_u8(val
, optval_addr
))
2780 return -TARGET_EFAULT
;
2782 if (put_user_u32(len
, optlen
))
2783 return -TARGET_EFAULT
;
2790 case IP_ROUTER_ALERT
:
2794 case IP_MTU_DISCOVER
:
2800 case IP_MULTICAST_TTL
:
2801 case IP_MULTICAST_LOOP
:
2802 if (get_user_u32(len
, optlen
))
2803 return -TARGET_EFAULT
;
2805 return -TARGET_EINVAL
;
2807 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2810 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2812 if (put_user_u32(len
, optlen
)
2813 || put_user_u8(val
, optval_addr
))
2814 return -TARGET_EFAULT
;
2816 if (len
> sizeof(int))
2818 if (put_user_u32(len
, optlen
)
2819 || put_user_u32(val
, optval_addr
))
2820 return -TARGET_EFAULT
;
2824 ret
= -TARGET_ENOPROTOOPT
;
2830 case IPV6_MTU_DISCOVER
:
2833 case IPV6_RECVPKTINFO
:
2834 case IPV6_UNICAST_HOPS
:
2835 case IPV6_MULTICAST_HOPS
:
2836 case IPV6_MULTICAST_LOOP
:
2838 case IPV6_RECVHOPLIMIT
:
2839 case IPV6_2292HOPLIMIT
:
2842 case IPV6_2292PKTINFO
:
2843 case IPV6_RECVTCLASS
:
2844 case IPV6_RECVRTHDR
:
2845 case IPV6_2292RTHDR
:
2846 case IPV6_RECVHOPOPTS
:
2847 case IPV6_2292HOPOPTS
:
2848 case IPV6_RECVDSTOPTS
:
2849 case IPV6_2292DSTOPTS
:
2851 case IPV6_ADDR_PREFERENCES
:
2852 #ifdef IPV6_RECVPATHMTU
2853 case IPV6_RECVPATHMTU
:
2855 #ifdef IPV6_TRANSPARENT
2856 case IPV6_TRANSPARENT
:
2858 #ifdef IPV6_FREEBIND
2861 #ifdef IPV6_RECVORIGDSTADDR
2862 case IPV6_RECVORIGDSTADDR
:
2864 if (get_user_u32(len
, optlen
))
2865 return -TARGET_EFAULT
;
2867 return -TARGET_EINVAL
;
2869 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2872 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2874 if (put_user_u32(len
, optlen
)
2875 || put_user_u8(val
, optval_addr
))
2876 return -TARGET_EFAULT
;
2878 if (len
> sizeof(int))
2880 if (put_user_u32(len
, optlen
)
2881 || put_user_u32(val
, optval_addr
))
2882 return -TARGET_EFAULT
;
2886 ret
= -TARGET_ENOPROTOOPT
;
2893 case NETLINK_PKTINFO
:
2894 case NETLINK_BROADCAST_ERROR
:
2895 case NETLINK_NO_ENOBUFS
:
2896 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2897 case NETLINK_LISTEN_ALL_NSID
:
2898 case NETLINK_CAP_ACK
:
2899 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2900 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2901 case NETLINK_EXT_ACK
:
2902 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2903 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2904 case NETLINK_GET_STRICT_CHK
:
2905 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2906 if (get_user_u32(len
, optlen
)) {
2907 return -TARGET_EFAULT
;
2909 if (len
!= sizeof(val
)) {
2910 return -TARGET_EINVAL
;
2913 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2917 if (put_user_u32(lv
, optlen
)
2918 || put_user_u32(val
, optval_addr
)) {
2919 return -TARGET_EFAULT
;
2922 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2923 case NETLINK_LIST_MEMBERSHIPS
:
2927 if (get_user_u32(len
, optlen
)) {
2928 return -TARGET_EFAULT
;
2931 return -TARGET_EINVAL
;
2933 results
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 1);
2934 if (!results
&& len
> 0) {
2935 return -TARGET_EFAULT
;
2938 ret
= get_errno(getsockopt(sockfd
, level
, optname
, results
, &lv
));
2940 unlock_user(results
, optval_addr
, 0);
2943 /* swap host endianess to target endianess. */
2944 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
2945 results
[i
] = tswap32(results
[i
]);
2947 if (put_user_u32(lv
, optlen
)) {
2948 return -TARGET_EFAULT
;
2950 unlock_user(results
, optval_addr
, 0);
2953 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2958 #endif /* SOL_NETLINK */
2961 qemu_log_mask(LOG_UNIMP
,
2962 "getsockopt level=%d optname=%d not yet supported\n",
2964 ret
= -TARGET_EOPNOTSUPP
;
2970 /* Convert target low/high pair representing file offset into the host
2971 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2972 * as the kernel doesn't handle them either.
2974 static void target_to_host_low_high(abi_ulong tlow
,
2976 unsigned long *hlow
,
2977 unsigned long *hhigh
)
2979 uint64_t off
= tlow
|
2980 ((unsigned long long)thigh
<< TARGET_LONG_BITS
/ 2) <<
2981 TARGET_LONG_BITS
/ 2;
2984 *hhigh
= (off
>> HOST_LONG_BITS
/ 2) >> HOST_LONG_BITS
/ 2;
2987 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
2988 abi_ulong count
, int copy
)
2990 struct target_iovec
*target_vec
;
2992 abi_ulong total_len
, max_len
;
2995 bool bad_address
= false;
3001 if (count
> IOV_MAX
) {
3006 vec
= g_try_new0(struct iovec
, count
);
3012 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3013 count
* sizeof(struct target_iovec
), 1);
3014 if (target_vec
== NULL
) {
3019 /* ??? If host page size > target page size, this will result in a
3020 value larger than what we can actually support. */
3021 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
3024 for (i
= 0; i
< count
; i
++) {
3025 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3026 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3031 } else if (len
== 0) {
3032 /* Zero length pointer is ignored. */
3033 vec
[i
].iov_base
= 0;
3035 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
3036 /* If the first buffer pointer is bad, this is a fault. But
3037 * subsequent bad buffers will result in a partial write; this
3038 * is realized by filling the vector with null pointers and
3040 if (!vec
[i
].iov_base
) {
3051 if (len
> max_len
- total_len
) {
3052 len
= max_len
- total_len
;
3055 vec
[i
].iov_len
= len
;
3059 unlock_user(target_vec
, target_addr
, 0);
3064 if (tswapal(target_vec
[i
].iov_len
) > 0) {
3065 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
3068 unlock_user(target_vec
, target_addr
, 0);
3075 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
3076 abi_ulong count
, int copy
)
3078 struct target_iovec
*target_vec
;
3081 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3082 count
* sizeof(struct target_iovec
), 1);
3084 for (i
= 0; i
< count
; i
++) {
3085 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3086 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3090 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
3092 unlock_user(target_vec
, target_addr
, 0);
3098 static inline int target_to_host_sock_type(int *type
)
3101 int target_type
= *type
;
3103 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
3104 case TARGET_SOCK_DGRAM
:
3105 host_type
= SOCK_DGRAM
;
3107 case TARGET_SOCK_STREAM
:
3108 host_type
= SOCK_STREAM
;
3111 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
3114 if (target_type
& TARGET_SOCK_CLOEXEC
) {
3115 #if defined(SOCK_CLOEXEC)
3116 host_type
|= SOCK_CLOEXEC
;
3118 return -TARGET_EINVAL
;
3121 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3122 #if defined(SOCK_NONBLOCK)
3123 host_type
|= SOCK_NONBLOCK
;
3124 #elif !defined(O_NONBLOCK)
3125 return -TARGET_EINVAL
;
3132 /* Try to emulate socket type flags after socket creation. */
3133 static int sock_flags_fixup(int fd
, int target_type
)
3135 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3136 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3137 int flags
= fcntl(fd
, F_GETFL
);
3138 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
3140 return -TARGET_EINVAL
;
3147 /* do_socket() Must return target values and target errnos. */
3148 static abi_long
do_socket(int domain
, int type
, int protocol
)
3150 int target_type
= type
;
3153 ret
= target_to_host_sock_type(&type
);
3158 if (domain
== PF_NETLINK
&& !(
3159 #ifdef CONFIG_RTNETLINK
3160 protocol
== NETLINK_ROUTE
||
3162 protocol
== NETLINK_KOBJECT_UEVENT
||
3163 protocol
== NETLINK_AUDIT
)) {
3164 return -TARGET_EPROTONOSUPPORT
;
3167 if (domain
== AF_PACKET
||
3168 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
3169 protocol
= tswap16(protocol
);
3172 ret
= get_errno(socket(domain
, type
, protocol
));
3174 ret
= sock_flags_fixup(ret
, target_type
);
3175 if (type
== SOCK_PACKET
) {
3176 /* Manage an obsolete case :
3177 * if socket type is SOCK_PACKET, bind by name
3179 fd_trans_register(ret
, &target_packet_trans
);
3180 } else if (domain
== PF_NETLINK
) {
3182 #ifdef CONFIG_RTNETLINK
3184 fd_trans_register(ret
, &target_netlink_route_trans
);
3187 case NETLINK_KOBJECT_UEVENT
:
3188 /* nothing to do: messages are strings */
3191 fd_trans_register(ret
, &target_netlink_audit_trans
);
3194 g_assert_not_reached();
3201 /* do_bind() Must return target values and target errnos. */
3202 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
3208 if ((int)addrlen
< 0) {
3209 return -TARGET_EINVAL
;
3212 addr
= alloca(addrlen
+1);
3214 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3218 return get_errno(bind(sockfd
, addr
, addrlen
));
3221 /* do_connect() Must return target values and target errnos. */
3222 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
3228 if ((int)addrlen
< 0) {
3229 return -TARGET_EINVAL
;
3232 addr
= alloca(addrlen
+1);
3234 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3238 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
3241 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3242 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
3243 int flags
, int send
)
3249 abi_ulong target_vec
;
3251 if (msgp
->msg_name
) {
3252 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
3253 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3254 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3255 tswapal(msgp
->msg_name
),
3257 if (ret
== -TARGET_EFAULT
) {
3258 /* For connected sockets msg_name and msg_namelen must
3259 * be ignored, so returning EFAULT immediately is wrong.
3260 * Instead, pass a bad msg_name to the host kernel, and
3261 * let it decide whether to return EFAULT or not.
3263 msg
.msg_name
= (void *)-1;
3268 msg
.msg_name
= NULL
;
3269 msg
.msg_namelen
= 0;
3271 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3272 msg
.msg_control
= alloca(msg
.msg_controllen
);
3273 memset(msg
.msg_control
, 0, msg
.msg_controllen
);
3275 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3277 count
= tswapal(msgp
->msg_iovlen
);
3278 target_vec
= tswapal(msgp
->msg_iov
);
3280 if (count
> IOV_MAX
) {
3281 /* sendrcvmsg returns a different errno for this condition than
3282 * readv/writev, so we must catch it here before lock_iovec() does.
3284 ret
= -TARGET_EMSGSIZE
;
3288 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
3289 target_vec
, count
, send
);
3291 ret
= -host_to_target_errno(errno
);
3294 msg
.msg_iovlen
= count
;
3298 if (fd_trans_target_to_host_data(fd
)) {
3301 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3302 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3303 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3304 msg
.msg_iov
->iov_len
);
3306 msg
.msg_iov
->iov_base
= host_msg
;
3307 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3311 ret
= target_to_host_cmsg(&msg
, msgp
);
3313 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3317 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3318 if (!is_error(ret
)) {
3320 if (fd_trans_host_to_target_data(fd
)) {
3321 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3322 MIN(msg
.msg_iov
->iov_len
, len
));
3324 ret
= host_to_target_cmsg(msgp
, &msg
);
3326 if (!is_error(ret
)) {
3327 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3328 msgp
->msg_flags
= tswap32(msg
.msg_flags
);
3329 if (msg
.msg_name
!= NULL
&& msg
.msg_name
!= (void *)-1) {
3330 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3331 msg
.msg_name
, msg
.msg_namelen
);
3343 unlock_iovec(vec
, target_vec
, count
, !send
);
3348 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3349 int flags
, int send
)
3352 struct target_msghdr
*msgp
;
3354 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
3358 return -TARGET_EFAULT
;
3360 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3361 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
3365 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3366 * so it might not have this *mmsg-specific flag either.
3368 #ifndef MSG_WAITFORONE
3369 #define MSG_WAITFORONE 0x10000
3372 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3373 unsigned int vlen
, unsigned int flags
,
3376 struct target_mmsghdr
*mmsgp
;
3380 if (vlen
> UIO_MAXIOV
) {
3384 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3386 return -TARGET_EFAULT
;
3389 for (i
= 0; i
< vlen
; i
++) {
3390 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3391 if (is_error(ret
)) {
3394 mmsgp
[i
].msg_len
= tswap32(ret
);
3395 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3396 if (flags
& MSG_WAITFORONE
) {
3397 flags
|= MSG_DONTWAIT
;
3401 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3403 /* Return number of datagrams sent if we sent any at all;
3404 * otherwise return the error.
3412 /* do_accept4() Must return target values and target errnos. */
3413 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3414 abi_ulong target_addrlen_addr
, int flags
)
3416 socklen_t addrlen
, ret_addrlen
;
3421 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
3423 if (target_addr
== 0) {
3424 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3427 /* linux returns EFAULT if addrlen pointer is invalid */
3428 if (get_user_u32(addrlen
, target_addrlen_addr
))
3429 return -TARGET_EFAULT
;
3431 if ((int)addrlen
< 0) {
3432 return -TARGET_EINVAL
;
3435 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3436 return -TARGET_EFAULT
;
3439 addr
= alloca(addrlen
);
3441 ret_addrlen
= addrlen
;
3442 ret
= get_errno(safe_accept4(fd
, addr
, &ret_addrlen
, host_flags
));
3443 if (!is_error(ret
)) {
3444 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3445 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3446 ret
= -TARGET_EFAULT
;
3452 /* do_getpeername() Must return target values and target errnos. */
3453 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3454 abi_ulong target_addrlen_addr
)
3456 socklen_t addrlen
, ret_addrlen
;
3460 if (get_user_u32(addrlen
, target_addrlen_addr
))
3461 return -TARGET_EFAULT
;
3463 if ((int)addrlen
< 0) {
3464 return -TARGET_EINVAL
;
3467 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3468 return -TARGET_EFAULT
;
3471 addr
= alloca(addrlen
);
3473 ret_addrlen
= addrlen
;
3474 ret
= get_errno(getpeername(fd
, addr
, &ret_addrlen
));
3475 if (!is_error(ret
)) {
3476 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3477 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3478 ret
= -TARGET_EFAULT
;
3484 /* do_getsockname() Must return target values and target errnos. */
3485 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3486 abi_ulong target_addrlen_addr
)
3488 socklen_t addrlen
, ret_addrlen
;
3492 if (get_user_u32(addrlen
, target_addrlen_addr
))
3493 return -TARGET_EFAULT
;
3495 if ((int)addrlen
< 0) {
3496 return -TARGET_EINVAL
;
3499 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3500 return -TARGET_EFAULT
;
3503 addr
= alloca(addrlen
);
3505 ret_addrlen
= addrlen
;
3506 ret
= get_errno(getsockname(fd
, addr
, &ret_addrlen
));
3507 if (!is_error(ret
)) {
3508 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3509 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3510 ret
= -TARGET_EFAULT
;
3516 /* do_socketpair() Must return target values and target errnos. */
3517 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3518 abi_ulong target_tab_addr
)
3523 target_to_host_sock_type(&type
);
3525 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
3526 if (!is_error(ret
)) {
3527 if (put_user_s32(tab
[0], target_tab_addr
)
3528 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
3529 ret
= -TARGET_EFAULT
;
3534 /* do_sendto() Must return target values and target errnos. */
3535 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
3536 abi_ulong target_addr
, socklen_t addrlen
)
3540 void *copy_msg
= NULL
;
3543 if ((int)addrlen
< 0) {
3544 return -TARGET_EINVAL
;
3547 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
3549 return -TARGET_EFAULT
;
3550 if (fd_trans_target_to_host_data(fd
)) {
3551 copy_msg
= host_msg
;
3552 host_msg
= g_malloc(len
);
3553 memcpy(host_msg
, copy_msg
, len
);
3554 ret
= fd_trans_target_to_host_data(fd
)(host_msg
, len
);
3560 addr
= alloca(addrlen
+1);
3561 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
3565 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
3567 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, NULL
, 0));
3572 host_msg
= copy_msg
;
3574 unlock_user(host_msg
, msg
, 0);
3578 /* do_recvfrom() Must return target values and target errnos. */
3579 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
3580 abi_ulong target_addr
,
3581 abi_ulong target_addrlen
)
3583 socklen_t addrlen
, ret_addrlen
;
3591 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
3593 return -TARGET_EFAULT
;
3597 if (get_user_u32(addrlen
, target_addrlen
)) {
3598 ret
= -TARGET_EFAULT
;
3601 if ((int)addrlen
< 0) {
3602 ret
= -TARGET_EINVAL
;
3605 addr
= alloca(addrlen
);
3606 ret_addrlen
= addrlen
;
3607 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
,
3608 addr
, &ret_addrlen
));
3610 addr
= NULL
; /* To keep compiler quiet. */
3611 addrlen
= 0; /* To keep compiler quiet. */
3612 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
, NULL
, 0));
3614 if (!is_error(ret
)) {
3615 if (fd_trans_host_to_target_data(fd
)) {
3617 trans
= fd_trans_host_to_target_data(fd
)(host_msg
, MIN(ret
, len
));
3618 if (is_error(trans
)) {
3624 host_to_target_sockaddr(target_addr
, addr
,
3625 MIN(addrlen
, ret_addrlen
));
3626 if (put_user_u32(ret_addrlen
, target_addrlen
)) {
3627 ret
= -TARGET_EFAULT
;
3631 unlock_user(host_msg
, msg
, len
);
3634 unlock_user(host_msg
, msg
, 0);
3639 #ifdef TARGET_NR_socketcall
3640 /* do_socketcall() must return target values and target errnos. */
3641 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
3643 static const unsigned nargs
[] = { /* number of arguments per operation */
3644 [TARGET_SYS_SOCKET
] = 3, /* domain, type, protocol */
3645 [TARGET_SYS_BIND
] = 3, /* fd, addr, addrlen */
3646 [TARGET_SYS_CONNECT
] = 3, /* fd, addr, addrlen */
3647 [TARGET_SYS_LISTEN
] = 2, /* fd, backlog */
3648 [TARGET_SYS_ACCEPT
] = 3, /* fd, addr, addrlen */
3649 [TARGET_SYS_GETSOCKNAME
] = 3, /* fd, addr, addrlen */
3650 [TARGET_SYS_GETPEERNAME
] = 3, /* fd, addr, addrlen */
3651 [TARGET_SYS_SOCKETPAIR
] = 4, /* domain, type, protocol, tab */
3652 [TARGET_SYS_SEND
] = 4, /* fd, msg, len, flags */
3653 [TARGET_SYS_RECV
] = 4, /* fd, msg, len, flags */
3654 [TARGET_SYS_SENDTO
] = 6, /* fd, msg, len, flags, addr, addrlen */
3655 [TARGET_SYS_RECVFROM
] = 6, /* fd, msg, len, flags, addr, addrlen */
3656 [TARGET_SYS_SHUTDOWN
] = 2, /* fd, how */
3657 [TARGET_SYS_SETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
3658 [TARGET_SYS_GETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
3659 [TARGET_SYS_SENDMSG
] = 3, /* fd, msg, flags */
3660 [TARGET_SYS_RECVMSG
] = 3, /* fd, msg, flags */
3661 [TARGET_SYS_ACCEPT4
] = 4, /* fd, addr, addrlen, flags */
3662 [TARGET_SYS_RECVMMSG
] = 4, /* fd, msgvec, vlen, flags */
3663 [TARGET_SYS_SENDMMSG
] = 4, /* fd, msgvec, vlen, flags */
3665 abi_long a
[6]; /* max 6 args */
3668 /* check the range of the first argument num */
3669 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3670 if (num
< 1 || num
> TARGET_SYS_SENDMMSG
) {
3671 return -TARGET_EINVAL
;
3673 /* ensure we have space for args */
3674 if (nargs
[num
] > ARRAY_SIZE(a
)) {
3675 return -TARGET_EINVAL
;
3677 /* collect the arguments in a[] according to nargs[] */
3678 for (i
= 0; i
< nargs
[num
]; ++i
) {
3679 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
3680 return -TARGET_EFAULT
;
3683 /* now when we have the args, invoke the appropriate underlying function */
3685 case TARGET_SYS_SOCKET
: /* domain, type, protocol */
3686 return do_socket(a
[0], a
[1], a
[2]);
3687 case TARGET_SYS_BIND
: /* sockfd, addr, addrlen */
3688 return do_bind(a
[0], a
[1], a
[2]);
3689 case TARGET_SYS_CONNECT
: /* sockfd, addr, addrlen */
3690 return do_connect(a
[0], a
[1], a
[2]);
3691 case TARGET_SYS_LISTEN
: /* sockfd, backlog */
3692 return get_errno(listen(a
[0], a
[1]));
3693 case TARGET_SYS_ACCEPT
: /* sockfd, addr, addrlen */
3694 return do_accept4(a
[0], a
[1], a
[2], 0);
3695 case TARGET_SYS_GETSOCKNAME
: /* sockfd, addr, addrlen */
3696 return do_getsockname(a
[0], a
[1], a
[2]);
3697 case TARGET_SYS_GETPEERNAME
: /* sockfd, addr, addrlen */
3698 return do_getpeername(a
[0], a
[1], a
[2]);
3699 case TARGET_SYS_SOCKETPAIR
: /* domain, type, protocol, tab */
3700 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
3701 case TARGET_SYS_SEND
: /* sockfd, msg, len, flags */
3702 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
3703 case TARGET_SYS_RECV
: /* sockfd, msg, len, flags */
3704 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
3705 case TARGET_SYS_SENDTO
: /* sockfd, msg, len, flags, addr, addrlen */
3706 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3707 case TARGET_SYS_RECVFROM
: /* sockfd, msg, len, flags, addr, addrlen */
3708 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3709 case TARGET_SYS_SHUTDOWN
: /* sockfd, how */
3710 return get_errno(shutdown(a
[0], a
[1]));
3711 case TARGET_SYS_SETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
3712 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3713 case TARGET_SYS_GETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
3714 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3715 case TARGET_SYS_SENDMSG
: /* sockfd, msg, flags */
3716 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
3717 case TARGET_SYS_RECVMSG
: /* sockfd, msg, flags */
3718 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
3719 case TARGET_SYS_ACCEPT4
: /* sockfd, addr, addrlen, flags */
3720 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
3721 case TARGET_SYS_RECVMMSG
: /* sockfd, msgvec, vlen, flags */
3722 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
3723 case TARGET_SYS_SENDMMSG
: /* sockfd, msgvec, vlen, flags */
3724 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
3726 qemu_log_mask(LOG_UNIMP
, "Unsupported socketcall: %d\n", num
);
3727 return -TARGET_EINVAL
;
3732 #define N_SHM_REGIONS 32
3734 static struct shm_region
{
3738 } shm_regions
[N_SHM_REGIONS
];
3740 #ifndef TARGET_SEMID64_DS
3741 /* asm-generic version of this struct */
3742 struct target_semid64_ds
3744 struct target_ipc_perm sem_perm
;
3745 abi_ulong sem_otime
;
3746 #if TARGET_ABI_BITS == 32
3747 abi_ulong __unused1
;
3749 abi_ulong sem_ctime
;
3750 #if TARGET_ABI_BITS == 32
3751 abi_ulong __unused2
;
3753 abi_ulong sem_nsems
;
3754 abi_ulong __unused3
;
3755 abi_ulong __unused4
;
3759 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
3760 abi_ulong target_addr
)
3762 struct target_ipc_perm
*target_ip
;
3763 struct target_semid64_ds
*target_sd
;
3765 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3766 return -TARGET_EFAULT
;
3767 target_ip
= &(target_sd
->sem_perm
);
3768 host_ip
->__key
= tswap32(target_ip
->__key
);
3769 host_ip
->uid
= tswap32(target_ip
->uid
);
3770 host_ip
->gid
= tswap32(target_ip
->gid
);
3771 host_ip
->cuid
= tswap32(target_ip
->cuid
);
3772 host_ip
->cgid
= tswap32(target_ip
->cgid
);
3773 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3774 host_ip
->mode
= tswap32(target_ip
->mode
);
3776 host_ip
->mode
= tswap16(target_ip
->mode
);
3778 #if defined(TARGET_PPC)
3779 host_ip
->__seq
= tswap32(target_ip
->__seq
);
3781 host_ip
->__seq
= tswap16(target_ip
->__seq
);
3783 unlock_user_struct(target_sd
, target_addr
, 0);
3787 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
3788 struct ipc_perm
*host_ip
)
3790 struct target_ipc_perm
*target_ip
;
3791 struct target_semid64_ds
*target_sd
;
3793 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3794 return -TARGET_EFAULT
;
3795 target_ip
= &(target_sd
->sem_perm
);
3796 target_ip
->__key
= tswap32(host_ip
->__key
);
3797 target_ip
->uid
= tswap32(host_ip
->uid
);
3798 target_ip
->gid
= tswap32(host_ip
->gid
);
3799 target_ip
->cuid
= tswap32(host_ip
->cuid
);
3800 target_ip
->cgid
= tswap32(host_ip
->cgid
);
3801 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3802 target_ip
->mode
= tswap32(host_ip
->mode
);
3804 target_ip
->mode
= tswap16(host_ip
->mode
);
3806 #if defined(TARGET_PPC)
3807 target_ip
->__seq
= tswap32(host_ip
->__seq
);
3809 target_ip
->__seq
= tswap16(host_ip
->__seq
);
3811 unlock_user_struct(target_sd
, target_addr
, 1);
3815 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
3816 abi_ulong target_addr
)
3818 struct target_semid64_ds
*target_sd
;
3820 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3821 return -TARGET_EFAULT
;
3822 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
3823 return -TARGET_EFAULT
;
3824 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
3825 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
3826 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
3827 unlock_user_struct(target_sd
, target_addr
, 0);
3831 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
3832 struct semid_ds
*host_sd
)
3834 struct target_semid64_ds
*target_sd
;
3836 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3837 return -TARGET_EFAULT
;
3838 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
3839 return -TARGET_EFAULT
;
3840 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
3841 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
3842 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
3843 unlock_user_struct(target_sd
, target_addr
, 1);
3847 struct target_seminfo
{
3860 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
3861 struct seminfo
*host_seminfo
)
3863 struct target_seminfo
*target_seminfo
;
3864 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
3865 return -TARGET_EFAULT
;
3866 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
3867 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
3868 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
3869 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
3870 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
3871 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
3872 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
3873 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
3874 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
3875 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
3876 unlock_user_struct(target_seminfo
, target_addr
, 1);
3882 struct semid_ds
*buf
;
3883 unsigned short *array
;
3884 struct seminfo
*__buf
;
3887 union target_semun
{
3894 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
3895 abi_ulong target_addr
)
3898 unsigned short *array
;
3900 struct semid_ds semid_ds
;
3903 semun
.buf
= &semid_ds
;
3905 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3907 return get_errno(ret
);
3909 nsems
= semid_ds
.sem_nsems
;
3911 *host_array
= g_try_new(unsigned short, nsems
);
3913 return -TARGET_ENOMEM
;
3915 array
= lock_user(VERIFY_READ
, target_addr
,
3916 nsems
*sizeof(unsigned short), 1);
3918 g_free(*host_array
);
3919 return -TARGET_EFAULT
;
3922 for(i
=0; i
<nsems
; i
++) {
3923 __get_user((*host_array
)[i
], &array
[i
]);
3925 unlock_user(array
, target_addr
, 0);
3930 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
3931 unsigned short **host_array
)
3934 unsigned short *array
;
3936 struct semid_ds semid_ds
;
3939 semun
.buf
= &semid_ds
;
3941 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3943 return get_errno(ret
);
3945 nsems
= semid_ds
.sem_nsems
;
3947 array
= lock_user(VERIFY_WRITE
, target_addr
,
3948 nsems
*sizeof(unsigned short), 0);
3950 return -TARGET_EFAULT
;
3952 for(i
=0; i
<nsems
; i
++) {
3953 __put_user((*host_array
)[i
], &array
[i
]);
3955 g_free(*host_array
);
3956 unlock_user(array
, target_addr
, 1);
3961 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
3962 abi_ulong target_arg
)
3964 union target_semun target_su
= { .buf
= target_arg
};
3966 struct semid_ds dsarg
;
3967 unsigned short *array
= NULL
;
3968 struct seminfo seminfo
;
3969 abi_long ret
= -TARGET_EINVAL
;
3976 /* In 64 bit cross-endian situations, we will erroneously pick up
3977 * the wrong half of the union for the "val" element. To rectify
3978 * this, the entire 8-byte structure is byteswapped, followed by
3979 * a swap of the 4 byte val field. In other cases, the data is
3980 * already in proper host byte order. */
3981 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
3982 target_su
.buf
= tswapal(target_su
.buf
);
3983 arg
.val
= tswap32(target_su
.val
);
3985 arg
.val
= target_su
.val
;
3987 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3991 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
3995 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
3996 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
4003 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
4007 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4008 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
4014 arg
.__buf
= &seminfo
;
4015 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4016 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
4024 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
4031 struct target_sembuf
{
4032 unsigned short sem_num
;
4037 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
4038 abi_ulong target_addr
,
4041 struct target_sembuf
*target_sembuf
;
4044 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
4045 nsops
*sizeof(struct target_sembuf
), 1);
4047 return -TARGET_EFAULT
;
4049 for(i
=0; i
<nsops
; i
++) {
4050 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
4051 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
4052 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
4055 unlock_user(target_sembuf
, target_addr
, 0);
4060 #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
4061 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
4064 * This macro is required to handle the s390 variants, which passes the
4065 * arguments in a different order than default.
4068 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4069 (__nsops), (__timeout), (__sops)
4071 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4072 (__nsops), 0, (__sops), (__timeout)
4075 static inline abi_long
do_semtimedop(int semid
,
4078 abi_long timeout
, bool time64
)
4080 struct sembuf
*sops
;
4081 struct timespec ts
, *pts
= NULL
;
4087 if (target_to_host_timespec64(pts
, timeout
)) {
4088 return -TARGET_EFAULT
;
4091 if (target_to_host_timespec(pts
, timeout
)) {
4092 return -TARGET_EFAULT
;
4097 if (nsops
> TARGET_SEMOPM
) {
4098 return -TARGET_E2BIG
;
4101 sops
= g_new(struct sembuf
, nsops
);
4103 if (target_to_host_sembuf(sops
, ptr
, nsops
)) {
4105 return -TARGET_EFAULT
;
4108 ret
= -TARGET_ENOSYS
;
4109 #ifdef __NR_semtimedop
4110 ret
= get_errno(safe_semtimedop(semid
, sops
, nsops
, pts
));
4113 if (ret
== -TARGET_ENOSYS
) {
4114 ret
= get_errno(safe_ipc(IPCOP_semtimedop
, semid
,
4115 SEMTIMEDOP_IPC_ARGS(nsops
, sops
, (long)pts
)));
4123 struct target_msqid_ds
4125 struct target_ipc_perm msg_perm
;
4126 abi_ulong msg_stime
;
4127 #if TARGET_ABI_BITS == 32
4128 abi_ulong __unused1
;
4130 abi_ulong msg_rtime
;
4131 #if TARGET_ABI_BITS == 32
4132 abi_ulong __unused2
;
4134 abi_ulong msg_ctime
;
4135 #if TARGET_ABI_BITS == 32
4136 abi_ulong __unused3
;
4138 abi_ulong __msg_cbytes
;
4140 abi_ulong msg_qbytes
;
4141 abi_ulong msg_lspid
;
4142 abi_ulong msg_lrpid
;
4143 abi_ulong __unused4
;
4144 abi_ulong __unused5
;
4147 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
4148 abi_ulong target_addr
)
4150 struct target_msqid_ds
*target_md
;
4152 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
4153 return -TARGET_EFAULT
;
4154 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
4155 return -TARGET_EFAULT
;
4156 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
4157 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
4158 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
4159 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
4160 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
4161 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
4162 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
4163 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
4164 unlock_user_struct(target_md
, target_addr
, 0);
4168 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
4169 struct msqid_ds
*host_md
)
4171 struct target_msqid_ds
*target_md
;
4173 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
4174 return -TARGET_EFAULT
;
4175 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
4176 return -TARGET_EFAULT
;
4177 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
4178 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
4179 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
4180 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
4181 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
4182 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
4183 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
4184 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
4185 unlock_user_struct(target_md
, target_addr
, 1);
4189 struct target_msginfo
{
4197 unsigned short int msgseg
;
4200 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
4201 struct msginfo
*host_msginfo
)
4203 struct target_msginfo
*target_msginfo
;
4204 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
4205 return -TARGET_EFAULT
;
4206 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
4207 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
4208 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
4209 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
4210 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
4211 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
4212 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
4213 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
4214 unlock_user_struct(target_msginfo
, target_addr
, 1);
4218 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
4220 struct msqid_ds dsarg
;
4221 struct msginfo msginfo
;
4222 abi_long ret
= -TARGET_EINVAL
;
4230 if (target_to_host_msqid_ds(&dsarg
,ptr
))
4231 return -TARGET_EFAULT
;
4232 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
4233 if (host_to_target_msqid_ds(ptr
,&dsarg
))
4234 return -TARGET_EFAULT
;
4237 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
4241 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
4242 if (host_to_target_msginfo(ptr
, &msginfo
))
4243 return -TARGET_EFAULT
;
4250 struct target_msgbuf
{
4255 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
4256 ssize_t msgsz
, int msgflg
)
4258 struct target_msgbuf
*target_mb
;
4259 struct msgbuf
*host_mb
;
4263 return -TARGET_EINVAL
;
4266 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
4267 return -TARGET_EFAULT
;
4268 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4270 unlock_user_struct(target_mb
, msgp
, 0);
4271 return -TARGET_ENOMEM
;
4273 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
4274 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
4275 ret
= -TARGET_ENOSYS
;
4277 ret
= get_errno(safe_msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
4280 if (ret
== -TARGET_ENOSYS
) {
4282 ret
= get_errno(safe_ipc(IPCOP_msgsnd
, msqid
, msgsz
, msgflg
,
4285 ret
= get_errno(safe_ipc(IPCOP_msgsnd
, msqid
, msgsz
, msgflg
,
4291 unlock_user_struct(target_mb
, msgp
, 0);
4297 #if defined(__sparc__)
4298 /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
4299 #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
4300 #elif defined(__s390x__)
4301 /* The s390 sys_ipc variant has only five parameters. */
4302 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4303 ((long int[]){(long int)__msgp, __msgtyp})
4305 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4306 ((long int[]){(long int)__msgp, __msgtyp}), 0
4310 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
4311 ssize_t msgsz
, abi_long msgtyp
,
4314 struct target_msgbuf
*target_mb
;
4316 struct msgbuf
*host_mb
;
4320 return -TARGET_EINVAL
;
4323 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
4324 return -TARGET_EFAULT
;
4326 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4328 ret
= -TARGET_ENOMEM
;
4331 ret
= -TARGET_ENOSYS
;
4333 ret
= get_errno(safe_msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
4336 if (ret
== -TARGET_ENOSYS
) {
4337 ret
= get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv
), msqid
, msgsz
,
4338 msgflg
, MSGRCV_ARGS(host_mb
, msgtyp
)));
4343 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
4344 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
4345 if (!target_mtext
) {
4346 ret
= -TARGET_EFAULT
;
4349 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
4350 unlock_user(target_mtext
, target_mtext_addr
, ret
);
4353 target_mb
->mtype
= tswapal(host_mb
->mtype
);
4357 unlock_user_struct(target_mb
, msgp
, 1);
4362 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
4363 abi_ulong target_addr
)
4365 struct target_shmid_ds
*target_sd
;
4367 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4368 return -TARGET_EFAULT
;
4369 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
4370 return -TARGET_EFAULT
;
4371 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4372 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4373 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4374 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4375 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4376 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4377 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4378 unlock_user_struct(target_sd
, target_addr
, 0);
4382 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
4383 struct shmid_ds
*host_sd
)
4385 struct target_shmid_ds
*target_sd
;
4387 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4388 return -TARGET_EFAULT
;
4389 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
4390 return -TARGET_EFAULT
;
4391 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4392 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4393 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4394 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4395 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4396 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4397 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4398 unlock_user_struct(target_sd
, target_addr
, 1);
4402 struct target_shminfo
{
4410 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
4411 struct shminfo
*host_shminfo
)
4413 struct target_shminfo
*target_shminfo
;
4414 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
4415 return -TARGET_EFAULT
;
4416 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
4417 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
4418 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
4419 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
4420 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
4421 unlock_user_struct(target_shminfo
, target_addr
, 1);
4425 struct target_shm_info
{
4430 abi_ulong swap_attempts
;
4431 abi_ulong swap_successes
;
4434 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
4435 struct shm_info
*host_shm_info
)
4437 struct target_shm_info
*target_shm_info
;
4438 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
4439 return -TARGET_EFAULT
;
4440 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
4441 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
4442 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
4443 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
4444 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
4445 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
4446 unlock_user_struct(target_shm_info
, target_addr
, 1);
4450 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
4452 struct shmid_ds dsarg
;
4453 struct shminfo shminfo
;
4454 struct shm_info shm_info
;
4455 abi_long ret
= -TARGET_EINVAL
;
4463 if (target_to_host_shmid_ds(&dsarg
, buf
))
4464 return -TARGET_EFAULT
;
4465 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
4466 if (host_to_target_shmid_ds(buf
, &dsarg
))
4467 return -TARGET_EFAULT
;
4470 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
4471 if (host_to_target_shminfo(buf
, &shminfo
))
4472 return -TARGET_EFAULT
;
4475 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
4476 if (host_to_target_shm_info(buf
, &shm_info
))
4477 return -TARGET_EFAULT
;
4482 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
4489 #ifndef TARGET_FORCE_SHMLBA
4490 /* For most architectures, SHMLBA is the same as the page size;
4491 * some architectures have larger values, in which case they should
4492 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
4493 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
4494 * and defining its own value for SHMLBA.
4496 * The kernel also permits SHMLBA to be set by the architecture to a
4497 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
4498 * this means that addresses are rounded to the large size if
4499 * SHM_RND is set but addresses not aligned to that size are not rejected
4500 * as long as they are at least page-aligned. Since the only architecture
4501 * which uses this is ia64 this code doesn't provide for that oddity.
4503 static inline abi_ulong
target_shmlba(CPUArchState
*cpu_env
)
4505 return TARGET_PAGE_SIZE
;
4509 static inline abi_ulong
do_shmat(CPUArchState
*cpu_env
,
4510 int shmid
, abi_ulong shmaddr
, int shmflg
)
4512 CPUState
*cpu
= env_cpu(cpu_env
);
4515 struct shmid_ds shm_info
;
4519 /* shmat pointers are always untagged */
4521 /* find out the length of the shared memory segment */
4522 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
4523 if (is_error(ret
)) {
4524 /* can't get length, bail out */
4528 shmlba
= target_shmlba(cpu_env
);
4530 if (shmaddr
& (shmlba
- 1)) {
4531 if (shmflg
& SHM_RND
) {
4532 shmaddr
&= ~(shmlba
- 1);
4534 return -TARGET_EINVAL
;
4537 if (!guest_range_valid_untagged(shmaddr
, shm_info
.shm_segsz
)) {
4538 return -TARGET_EINVAL
;
4544 * We're mapping shared memory, so ensure we generate code for parallel
4545 * execution and flush old translations. This will work up to the level
4546 * supported by the host -- anything that requires EXCP_ATOMIC will not
4547 * be atomic with respect to an external process.
4549 if (!(cpu
->tcg_cflags
& CF_PARALLEL
)) {
4550 cpu
->tcg_cflags
|= CF_PARALLEL
;
4555 host_raddr
= shmat(shmid
, (void *)g2h_untagged(shmaddr
), shmflg
);
4557 abi_ulong mmap_start
;
4559 /* In order to use the host shmat, we need to honor host SHMLBA. */
4560 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
, MAX(SHMLBA
, shmlba
));
4562 if (mmap_start
== -1) {
4564 host_raddr
= (void *)-1;
4566 host_raddr
= shmat(shmid
, g2h_untagged(mmap_start
),
4567 shmflg
| SHM_REMAP
);
4570 if (host_raddr
== (void *)-1) {
4572 return get_errno((long)host_raddr
);
4574 raddr
=h2g((unsigned long)host_raddr
);
4576 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
,
4577 PAGE_VALID
| PAGE_RESET
| PAGE_READ
|
4578 (shmflg
& SHM_RDONLY
? 0 : PAGE_WRITE
));
4580 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
4581 if (!shm_regions
[i
].in_use
) {
4582 shm_regions
[i
].in_use
= true;
4583 shm_regions
[i
].start
= raddr
;
4584 shm_regions
[i
].size
= shm_info
.shm_segsz
;
4594 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
4599 /* shmdt pointers are always untagged */
4603 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
4604 if (shm_regions
[i
].in_use
&& shm_regions
[i
].start
== shmaddr
) {
4605 shm_regions
[i
].in_use
= false;
4606 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
, 0);
4610 rv
= get_errno(shmdt(g2h_untagged(shmaddr
)));
4617 #ifdef TARGET_NR_ipc
4618 /* ??? This only works with linear mappings. */
4619 /* do_ipc() must return target values and target errnos. */
4620 static abi_long
do_ipc(CPUArchState
*cpu_env
,
4621 unsigned int call
, abi_long first
,
4622 abi_long second
, abi_long third
,
4623 abi_long ptr
, abi_long fifth
)
4628 version
= call
>> 16;
4633 ret
= do_semtimedop(first
, ptr
, second
, 0, false);
4635 case IPCOP_semtimedop
:
4637 * The s390 sys_ipc variant has only five parameters instead of six
4638 * (as for default variant) and the only difference is the handling of
4639 * SEMTIMEDOP where on s390 the third parameter is used as a pointer
4640 * to a struct timespec where the generic variant uses fifth parameter.
4642 #if defined(TARGET_S390X)
4643 ret
= do_semtimedop(first
, ptr
, second
, third
, TARGET_ABI_BITS
== 64);
4645 ret
= do_semtimedop(first
, ptr
, second
, fifth
, TARGET_ABI_BITS
== 64);
4650 ret
= get_errno(semget(first
, second
, third
));
4653 case IPCOP_semctl
: {
4654 /* The semun argument to semctl is passed by value, so dereference the
4657 get_user_ual(atptr
, ptr
);
4658 ret
= do_semctl(first
, second
, third
, atptr
);
4663 ret
= get_errno(msgget(first
, second
));
4667 ret
= do_msgsnd(first
, ptr
, second
, third
);
4671 ret
= do_msgctl(first
, second
, ptr
);
4678 struct target_ipc_kludge
{
4683 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
4684 ret
= -TARGET_EFAULT
;
4688 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
4690 unlock_user_struct(tmp
, ptr
, 0);
4694 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
4703 raddr
= do_shmat(cpu_env
, first
, ptr
, second
);
4704 if (is_error(raddr
))
4705 return get_errno(raddr
);
4706 if (put_user_ual(raddr
, third
))
4707 return -TARGET_EFAULT
;
4711 ret
= -TARGET_EINVAL
;
4716 ret
= do_shmdt(ptr
);
4720 /* IPC_* flag values are the same on all linux platforms */
4721 ret
= get_errno(shmget(first
, second
, third
));
4724 /* IPC_* and SHM_* command values are the same on all linux platforms */
4726 ret
= do_shmctl(first
, second
, ptr
);
4729 qemu_log_mask(LOG_UNIMP
, "Unsupported ipc call: %d (version %d)\n",
4731 ret
= -TARGET_ENOSYS
;
4738 /* kernel structure types definitions */
4740 #define STRUCT(name, ...) STRUCT_ ## name,
4741 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
4743 #include "syscall_types.h"
4747 #undef STRUCT_SPECIAL
4749 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
4750 #define STRUCT_SPECIAL(name)
4751 #include "syscall_types.h"
4753 #undef STRUCT_SPECIAL
4755 #define MAX_STRUCT_SIZE 4096
4757 #ifdef CONFIG_FIEMAP
4758 /* So fiemap access checks don't overflow on 32 bit systems.
4759 * This is very slightly smaller than the limit imposed by
4760 * the underlying kernel.
4762 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4763 / sizeof(struct fiemap_extent))
4765 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4766 int fd
, int cmd
, abi_long arg
)
4768 /* The parameter for this ioctl is a struct fiemap followed
4769 * by an array of struct fiemap_extent whose size is set
4770 * in fiemap->fm_extent_count. The array is filled in by the
4773 int target_size_in
, target_size_out
;
4775 const argtype
*arg_type
= ie
->arg_type
;
4776 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
4779 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
4783 assert(arg_type
[0] == TYPE_PTR
);
4784 assert(ie
->access
== IOC_RW
);
4786 target_size_in
= thunk_type_size(arg_type
, 0);
4787 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
4789 return -TARGET_EFAULT
;
4791 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4792 unlock_user(argptr
, arg
, 0);
4793 fm
= (struct fiemap
*)buf_temp
;
4794 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
4795 return -TARGET_EINVAL
;
4798 outbufsz
= sizeof (*fm
) +
4799 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
4801 if (outbufsz
> MAX_STRUCT_SIZE
) {
4802 /* We can't fit all the extents into the fixed size buffer.
4803 * Allocate one that is large enough and use it instead.
4805 fm
= g_try_malloc(outbufsz
);
4807 return -TARGET_ENOMEM
;
4809 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
4812 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, fm
));
4813 if (!is_error(ret
)) {
4814 target_size_out
= target_size_in
;
4815 /* An extent_count of 0 means we were only counting the extents
4816 * so there are no structs to copy
4818 if (fm
->fm_extent_count
!= 0) {
4819 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
4821 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
4823 ret
= -TARGET_EFAULT
;
4825 /* Convert the struct fiemap */
4826 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
4827 if (fm
->fm_extent_count
!= 0) {
4828 p
= argptr
+ target_size_in
;
4829 /* ...and then all the struct fiemap_extents */
4830 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
4831 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
4836 unlock_user(argptr
, arg
, target_size_out
);
4846 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4847 int fd
, int cmd
, abi_long arg
)
4849 const argtype
*arg_type
= ie
->arg_type
;
4853 struct ifconf
*host_ifconf
;
4855 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
4856 const argtype ifreq_max_type
[] = { MK_STRUCT(STRUCT_ifmap_ifreq
) };
4857 int target_ifreq_size
;
4862 abi_long target_ifc_buf
;
4866 assert(arg_type
[0] == TYPE_PTR
);
4867 assert(ie
->access
== IOC_RW
);
4870 target_size
= thunk_type_size(arg_type
, 0);
4872 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4874 return -TARGET_EFAULT
;
4875 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4876 unlock_user(argptr
, arg
, 0);
4878 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
4879 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
4880 target_ifreq_size
= thunk_type_size(ifreq_max_type
, 0);
4882 if (target_ifc_buf
!= 0) {
4883 target_ifc_len
= host_ifconf
->ifc_len
;
4884 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
4885 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
4887 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
4888 if (outbufsz
> MAX_STRUCT_SIZE
) {
4890 * We can't fit all the extents into the fixed size buffer.
4891 * Allocate one that is large enough and use it instead.
4893 host_ifconf
= g_try_malloc(outbufsz
);
4895 return -TARGET_ENOMEM
;
4897 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
4900 host_ifc_buf
= (char *)host_ifconf
+ sizeof(*host_ifconf
);
4902 host_ifconf
->ifc_len
= host_ifc_len
;
4904 host_ifc_buf
= NULL
;
4906 host_ifconf
->ifc_buf
= host_ifc_buf
;
4908 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_ifconf
));
4909 if (!is_error(ret
)) {
4910 /* convert host ifc_len to target ifc_len */
4912 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
4913 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
4914 host_ifconf
->ifc_len
= target_ifc_len
;
4916 /* restore target ifc_buf */
4918 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
4920 /* copy struct ifconf to target user */
4922 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4924 return -TARGET_EFAULT
;
4925 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
4926 unlock_user(argptr
, arg
, target_size
);
4928 if (target_ifc_buf
!= 0) {
4929 /* copy ifreq[] to target user */
4930 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
4931 for (i
= 0; i
< nb_ifreq
; i
++) {
4932 thunk_convert(argptr
+ i
* target_ifreq_size
,
4933 host_ifc_buf
+ i
* sizeof(struct ifreq
),
4934 ifreq_arg_type
, THUNK_TARGET
);
4936 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
4941 g_free(host_ifconf
);
4947 #if defined(CONFIG_USBFS)
4948 #if HOST_LONG_BITS > 64
4949 #error USBDEVFS thunks do not support >64 bit hosts yet.
4952 uint64_t target_urb_adr
;
4953 uint64_t target_buf_adr
;
4954 char *target_buf_ptr
;
4955 struct usbdevfs_urb host_urb
;
4958 static GHashTable
*usbdevfs_urb_hashtable(void)
4960 static GHashTable
*urb_hashtable
;
4962 if (!urb_hashtable
) {
4963 urb_hashtable
= g_hash_table_new(g_int64_hash
, g_int64_equal
);
4965 return urb_hashtable
;
4968 static void urb_hashtable_insert(struct live_urb
*urb
)
4970 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4971 g_hash_table_insert(urb_hashtable
, urb
, urb
);
4974 static struct live_urb
*urb_hashtable_lookup(uint64_t target_urb_adr
)
4976 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4977 return g_hash_table_lookup(urb_hashtable
, &target_urb_adr
);
4980 static void urb_hashtable_remove(struct live_urb
*urb
)
4982 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4983 g_hash_table_remove(urb_hashtable
, urb
);
4987 do_ioctl_usbdevfs_reapurb(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4988 int fd
, int cmd
, abi_long arg
)
4990 const argtype usbfsurb_arg_type
[] = { MK_STRUCT(STRUCT_usbdevfs_urb
) };
4991 const argtype ptrvoid_arg_type
[] = { TYPE_PTRVOID
, 0, 0 };
4992 struct live_urb
*lurb
;
4996 uintptr_t target_urb_adr
;
4999 target_size
= thunk_type_size(usbfsurb_arg_type
, THUNK_TARGET
);
5001 memset(buf_temp
, 0, sizeof(uint64_t));
5002 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5003 if (is_error(ret
)) {
5007 memcpy(&hurb
, buf_temp
, sizeof(uint64_t));
5008 lurb
= (void *)((uintptr_t)hurb
- offsetof(struct live_urb
, host_urb
));
5009 if (!lurb
->target_urb_adr
) {
5010 return -TARGET_EFAULT
;
5012 urb_hashtable_remove(lurb
);
5013 unlock_user(lurb
->target_buf_ptr
, lurb
->target_buf_adr
,
5014 lurb
->host_urb
.buffer_length
);
5015 lurb
->target_buf_ptr
= NULL
;
5017 /* restore the guest buffer pointer */
5018 lurb
->host_urb
.buffer
= (void *)(uintptr_t)lurb
->target_buf_adr
;
5020 /* update the guest urb struct */
5021 argptr
= lock_user(VERIFY_WRITE
, lurb
->target_urb_adr
, target_size
, 0);
5024 return -TARGET_EFAULT
;
5026 thunk_convert(argptr
, &lurb
->host_urb
, usbfsurb_arg_type
, THUNK_TARGET
);
5027 unlock_user(argptr
, lurb
->target_urb_adr
, target_size
);
5029 target_size
= thunk_type_size(ptrvoid_arg_type
, THUNK_TARGET
);
5030 /* write back the urb handle */
5031 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5034 return -TARGET_EFAULT
;
5037 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
5038 target_urb_adr
= lurb
->target_urb_adr
;
5039 thunk_convert(argptr
, &target_urb_adr
, ptrvoid_arg_type
, THUNK_TARGET
);
5040 unlock_user(argptr
, arg
, target_size
);
5047 do_ioctl_usbdevfs_discardurb(const IOCTLEntry
*ie
,
5048 uint8_t *buf_temp
__attribute__((unused
)),
5049 int fd
, int cmd
, abi_long arg
)
5051 struct live_urb
*lurb
;
5053 /* map target address back to host URB with metadata. */
5054 lurb
= urb_hashtable_lookup(arg
);
5056 return -TARGET_EFAULT
;
5058 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, &lurb
->host_urb
));
5062 do_ioctl_usbdevfs_submiturb(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5063 int fd
, int cmd
, abi_long arg
)
5065 const argtype
*arg_type
= ie
->arg_type
;
5070 struct live_urb
*lurb
;
5073 * each submitted URB needs to map to a unique ID for the
5074 * kernel, and that unique ID needs to be a pointer to
5075 * host memory. hence, we need to malloc for each URB.
5076 * isochronous transfers have a variable length struct.
5079 target_size
= thunk_type_size(arg_type
, THUNK_TARGET
);
5081 /* construct host copy of urb and metadata */
5082 lurb
= g_try_new0(struct live_urb
, 1);
5084 return -TARGET_ENOMEM
;
5087 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5090 return -TARGET_EFAULT
;
5092 thunk_convert(&lurb
->host_urb
, argptr
, arg_type
, THUNK_HOST
);
5093 unlock_user(argptr
, arg
, 0);
5095 lurb
->target_urb_adr
= arg
;
5096 lurb
->target_buf_adr
= (uintptr_t)lurb
->host_urb
.buffer
;
5098 /* buffer space used depends on endpoint type so lock the entire buffer */
5099 /* control type urbs should check the buffer contents for true direction */
5100 rw_dir
= lurb
->host_urb
.endpoint
& USB_DIR_IN
? VERIFY_WRITE
: VERIFY_READ
;
5101 lurb
->target_buf_ptr
= lock_user(rw_dir
, lurb
->target_buf_adr
,
5102 lurb
->host_urb
.buffer_length
, 1);
5103 if (lurb
->target_buf_ptr
== NULL
) {
5105 return -TARGET_EFAULT
;
5108 /* update buffer pointer in host copy */
5109 lurb
->host_urb
.buffer
= lurb
->target_buf_ptr
;
5111 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, &lurb
->host_urb
));
5112 if (is_error(ret
)) {
5113 unlock_user(lurb
->target_buf_ptr
, lurb
->target_buf_adr
, 0);
5116 urb_hashtable_insert(lurb
);
5121 #endif /* CONFIG_USBFS */
5123 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5124 int cmd
, abi_long arg
)
5127 struct dm_ioctl
*host_dm
;
5128 abi_long guest_data
;
5129 uint32_t guest_data_size
;
5131 const argtype
*arg_type
= ie
->arg_type
;
5133 void *big_buf
= NULL
;
5137 target_size
= thunk_type_size(arg_type
, 0);
5138 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5140 ret
= -TARGET_EFAULT
;
5143 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5144 unlock_user(argptr
, arg
, 0);
5146 /* buf_temp is too small, so fetch things into a bigger buffer */
5147 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
5148 memcpy(big_buf
, buf_temp
, target_size
);
5152 guest_data
= arg
+ host_dm
->data_start
;
5153 if ((guest_data
- arg
) < 0) {
5154 ret
= -TARGET_EINVAL
;
5157 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5158 host_data
= (char*)host_dm
+ host_dm
->data_start
;
5160 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
5162 ret
= -TARGET_EFAULT
;
5166 switch (ie
->host_cmd
) {
5168 case DM_LIST_DEVICES
:
5171 case DM_DEV_SUSPEND
:
5174 case DM_TABLE_STATUS
:
5175 case DM_TABLE_CLEAR
:
5177 case DM_LIST_VERSIONS
:
5181 case DM_DEV_SET_GEOMETRY
:
5182 /* data contains only strings */
5183 memcpy(host_data
, argptr
, guest_data_size
);
5186 memcpy(host_data
, argptr
, guest_data_size
);
5187 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
5191 void *gspec
= argptr
;
5192 void *cur_data
= host_data
;
5193 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5194 int spec_size
= thunk_type_size(arg_type
, 0);
5197 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5198 struct dm_target_spec
*spec
= cur_data
;
5202 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
5203 slen
= strlen((char*)gspec
+ spec_size
) + 1;
5205 spec
->next
= sizeof(*spec
) + slen
;
5206 strcpy((char*)&spec
[1], gspec
+ spec_size
);
5208 cur_data
+= spec
->next
;
5213 ret
= -TARGET_EINVAL
;
5214 unlock_user(argptr
, guest_data
, 0);
5217 unlock_user(argptr
, guest_data
, 0);
5219 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5220 if (!is_error(ret
)) {
5221 guest_data
= arg
+ host_dm
->data_start
;
5222 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5223 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
5224 switch (ie
->host_cmd
) {
5229 case DM_DEV_SUSPEND
:
5232 case DM_TABLE_CLEAR
:
5234 case DM_DEV_SET_GEOMETRY
:
5235 /* no return data */
5237 case DM_LIST_DEVICES
:
5239 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
5240 uint32_t remaining_data
= guest_data_size
;
5241 void *cur_data
= argptr
;
5242 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
5243 int nl_size
= 12; /* can't use thunk_size due to alignment */
5246 uint32_t next
= nl
->next
;
5248 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
5250 if (remaining_data
< nl
->next
) {
5251 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5254 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
5255 strcpy(cur_data
+ nl_size
, nl
->name
);
5256 cur_data
+= nl
->next
;
5257 remaining_data
-= nl
->next
;
5261 nl
= (void*)nl
+ next
;
5266 case DM_TABLE_STATUS
:
5268 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
5269 void *cur_data
= argptr
;
5270 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5271 int spec_size
= thunk_type_size(arg_type
, 0);
5274 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5275 uint32_t next
= spec
->next
;
5276 int slen
= strlen((char*)&spec
[1]) + 1;
5277 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
5278 if (guest_data_size
< spec
->next
) {
5279 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5282 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
5283 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
5284 cur_data
= argptr
+ spec
->next
;
5285 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
5291 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
5292 int count
= *(uint32_t*)hdata
;
5293 uint64_t *hdev
= hdata
+ 8;
5294 uint64_t *gdev
= argptr
+ 8;
5297 *(uint32_t*)argptr
= tswap32(count
);
5298 for (i
= 0; i
< count
; i
++) {
5299 *gdev
= tswap64(*hdev
);
5305 case DM_LIST_VERSIONS
:
5307 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
5308 uint32_t remaining_data
= guest_data_size
;
5309 void *cur_data
= argptr
;
5310 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
5311 int vers_size
= thunk_type_size(arg_type
, 0);
5314 uint32_t next
= vers
->next
;
5316 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
5318 if (remaining_data
< vers
->next
) {
5319 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5322 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
5323 strcpy(cur_data
+ vers_size
, vers
->name
);
5324 cur_data
+= vers
->next
;
5325 remaining_data
-= vers
->next
;
5329 vers
= (void*)vers
+ next
;
5334 unlock_user(argptr
, guest_data
, 0);
5335 ret
= -TARGET_EINVAL
;
5338 unlock_user(argptr
, guest_data
, guest_data_size
);
5340 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5342 ret
= -TARGET_EFAULT
;
5345 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5346 unlock_user(argptr
, arg
, target_size
);
5353 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5354 int cmd
, abi_long arg
)
5358 const argtype
*arg_type
= ie
->arg_type
;
5359 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
5362 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
5363 struct blkpg_partition host_part
;
5365 /* Read and convert blkpg */
5367 target_size
= thunk_type_size(arg_type
, 0);
5368 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5370 ret
= -TARGET_EFAULT
;
5373 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5374 unlock_user(argptr
, arg
, 0);
5376 switch (host_blkpg
->op
) {
5377 case BLKPG_ADD_PARTITION
:
5378 case BLKPG_DEL_PARTITION
:
5379 /* payload is struct blkpg_partition */
5382 /* Unknown opcode */
5383 ret
= -TARGET_EINVAL
;
5387 /* Read and convert blkpg->data */
5388 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
5389 target_size
= thunk_type_size(part_arg_type
, 0);
5390 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5392 ret
= -TARGET_EFAULT
;
5395 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
5396 unlock_user(argptr
, arg
, 0);
5398 /* Swizzle the data pointer to our local copy and call! */
5399 host_blkpg
->data
= &host_part
;
5400 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_blkpg
));
5406 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5407 int fd
, int cmd
, abi_long arg
)
5409 const argtype
*arg_type
= ie
->arg_type
;
5410 const StructEntry
*se
;
5411 const argtype
*field_types
;
5412 const int *dst_offsets
, *src_offsets
;
5415 abi_ulong
*target_rt_dev_ptr
= NULL
;
5416 unsigned long *host_rt_dev_ptr
= NULL
;
5420 assert(ie
->access
== IOC_W
);
5421 assert(*arg_type
== TYPE_PTR
);
5423 assert(*arg_type
== TYPE_STRUCT
);
5424 target_size
= thunk_type_size(arg_type
, 0);
5425 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5427 return -TARGET_EFAULT
;
5430 assert(*arg_type
== (int)STRUCT_rtentry
);
5431 se
= struct_entries
+ *arg_type
++;
5432 assert(se
->convert
[0] == NULL
);
5433 /* convert struct here to be able to catch rt_dev string */
5434 field_types
= se
->field_types
;
5435 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
5436 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
5437 for (i
= 0; i
< se
->nb_fields
; i
++) {
5438 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
5439 assert(*field_types
== TYPE_PTRVOID
);
5440 target_rt_dev_ptr
= (abi_ulong
*)(argptr
+ src_offsets
[i
]);
5441 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
5442 if (*target_rt_dev_ptr
!= 0) {
5443 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
5444 tswapal(*target_rt_dev_ptr
));
5445 if (!*host_rt_dev_ptr
) {
5446 unlock_user(argptr
, arg
, 0);
5447 return -TARGET_EFAULT
;
5450 *host_rt_dev_ptr
= 0;
5455 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
5456 argptr
+ src_offsets
[i
],
5457 field_types
, THUNK_HOST
);
5459 unlock_user(argptr
, arg
, 0);
5461 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5463 assert(host_rt_dev_ptr
!= NULL
);
5464 assert(target_rt_dev_ptr
!= NULL
);
5465 if (*host_rt_dev_ptr
!= 0) {
5466 unlock_user((void *)*host_rt_dev_ptr
,
5467 *target_rt_dev_ptr
, 0);
5472 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5473 int fd
, int cmd
, abi_long arg
)
5475 int sig
= target_to_host_signal(arg
);
5476 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, sig
));
5479 static abi_long
do_ioctl_SIOCGSTAMP(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5480 int fd
, int cmd
, abi_long arg
)
5485 ret
= get_errno(safe_ioctl(fd
, SIOCGSTAMP
, &tv
));
5486 if (is_error(ret
)) {
5490 if (cmd
== (int)TARGET_SIOCGSTAMP_OLD
) {
5491 if (copy_to_user_timeval(arg
, &tv
)) {
5492 return -TARGET_EFAULT
;
5495 if (copy_to_user_timeval64(arg
, &tv
)) {
5496 return -TARGET_EFAULT
;
5503 static abi_long
do_ioctl_SIOCGSTAMPNS(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5504 int fd
, int cmd
, abi_long arg
)
5509 ret
= get_errno(safe_ioctl(fd
, SIOCGSTAMPNS
, &ts
));
5510 if (is_error(ret
)) {
5514 if (cmd
== (int)TARGET_SIOCGSTAMPNS_OLD
) {
5515 if (host_to_target_timespec(arg
, &ts
)) {
5516 return -TARGET_EFAULT
;
5519 if (host_to_target_timespec64(arg
, &ts
)) {
5520 return -TARGET_EFAULT
;
5528 static abi_long
do_ioctl_tiocgptpeer(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5529 int fd
, int cmd
, abi_long arg
)
5531 int flags
= target_to_host_bitmask(arg
, fcntl_flags_tbl
);
5532 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, flags
));
5538 static void unlock_drm_version(struct drm_version
*host_ver
,
5539 struct target_drm_version
*target_ver
,
5542 unlock_user(host_ver
->name
, target_ver
->name
,
5543 copy
? host_ver
->name_len
: 0);
5544 unlock_user(host_ver
->date
, target_ver
->date
,
5545 copy
? host_ver
->date_len
: 0);
5546 unlock_user(host_ver
->desc
, target_ver
->desc
,
5547 copy
? host_ver
->desc_len
: 0);
5550 static inline abi_long
target_to_host_drmversion(struct drm_version
*host_ver
,
5551 struct target_drm_version
*target_ver
)
5553 memset(host_ver
, 0, sizeof(*host_ver
));
5555 __get_user(host_ver
->name_len
, &target_ver
->name_len
);
5556 if (host_ver
->name_len
) {
5557 host_ver
->name
= lock_user(VERIFY_WRITE
, target_ver
->name
,
5558 target_ver
->name_len
, 0);
5559 if (!host_ver
->name
) {
5564 __get_user(host_ver
->date_len
, &target_ver
->date_len
);
5565 if (host_ver
->date_len
) {
5566 host_ver
->date
= lock_user(VERIFY_WRITE
, target_ver
->date
,
5567 target_ver
->date_len
, 0);
5568 if (!host_ver
->date
) {
5573 __get_user(host_ver
->desc_len
, &target_ver
->desc_len
);
5574 if (host_ver
->desc_len
) {
5575 host_ver
->desc
= lock_user(VERIFY_WRITE
, target_ver
->desc
,
5576 target_ver
->desc_len
, 0);
5577 if (!host_ver
->desc
) {
5584 unlock_drm_version(host_ver
, target_ver
, false);
5588 static inline void host_to_target_drmversion(
5589 struct target_drm_version
*target_ver
,
5590 struct drm_version
*host_ver
)
5592 __put_user(host_ver
->version_major
, &target_ver
->version_major
);
5593 __put_user(host_ver
->version_minor
, &target_ver
->version_minor
);
5594 __put_user(host_ver
->version_patchlevel
, &target_ver
->version_patchlevel
);
5595 __put_user(host_ver
->name_len
, &target_ver
->name_len
);
5596 __put_user(host_ver
->date_len
, &target_ver
->date_len
);
5597 __put_user(host_ver
->desc_len
, &target_ver
->desc_len
);
5598 unlock_drm_version(host_ver
, target_ver
, true);
5601 static abi_long
do_ioctl_drm(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5602 int fd
, int cmd
, abi_long arg
)
5604 struct drm_version
*ver
;
5605 struct target_drm_version
*target_ver
;
5608 switch (ie
->host_cmd
) {
5609 case DRM_IOCTL_VERSION
:
5610 if (!lock_user_struct(VERIFY_WRITE
, target_ver
, arg
, 0)) {
5611 return -TARGET_EFAULT
;
5613 ver
= (struct drm_version
*)buf_temp
;
5614 ret
= target_to_host_drmversion(ver
, target_ver
);
5615 if (!is_error(ret
)) {
5616 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, ver
));
5617 if (is_error(ret
)) {
5618 unlock_drm_version(ver
, target_ver
, false);
5620 host_to_target_drmversion(target_ver
, ver
);
5623 unlock_user_struct(target_ver
, arg
, 0);
5626 return -TARGET_ENOSYS
;
5629 static abi_long
do_ioctl_drm_i915_getparam(const IOCTLEntry
*ie
,
5630 struct drm_i915_getparam
*gparam
,
5631 int fd
, abi_long arg
)
5635 struct target_drm_i915_getparam
*target_gparam
;
5637 if (!lock_user_struct(VERIFY_READ
, target_gparam
, arg
, 0)) {
5638 return -TARGET_EFAULT
;
5641 __get_user(gparam
->param
, &target_gparam
->param
);
5642 gparam
->value
= &value
;
5643 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, gparam
));
5644 put_user_s32(value
, target_gparam
->value
);
5646 unlock_user_struct(target_gparam
, arg
, 0);
5650 static abi_long
do_ioctl_drm_i915(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5651 int fd
, int cmd
, abi_long arg
)
5653 switch (ie
->host_cmd
) {
5654 case DRM_IOCTL_I915_GETPARAM
:
5655 return do_ioctl_drm_i915_getparam(ie
,
5656 (struct drm_i915_getparam
*)buf_temp
,
5659 return -TARGET_ENOSYS
;
5665 static abi_long
do_ioctl_TUNSETTXFILTER(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5666 int fd
, int cmd
, abi_long arg
)
5668 struct tun_filter
*filter
= (struct tun_filter
*)buf_temp
;
5669 struct tun_filter
*target_filter
;
5672 assert(ie
->access
== IOC_W
);
5674 target_filter
= lock_user(VERIFY_READ
, arg
, sizeof(*target_filter
), 1);
5675 if (!target_filter
) {
5676 return -TARGET_EFAULT
;
5678 filter
->flags
= tswap16(target_filter
->flags
);
5679 filter
->count
= tswap16(target_filter
->count
);
5680 unlock_user(target_filter
, arg
, 0);
5682 if (filter
->count
) {
5683 if (offsetof(struct tun_filter
, addr
) + filter
->count
* ETH_ALEN
>
5685 return -TARGET_EFAULT
;
5688 target_addr
= lock_user(VERIFY_READ
,
5689 arg
+ offsetof(struct tun_filter
, addr
),
5690 filter
->count
* ETH_ALEN
, 1);
5692 return -TARGET_EFAULT
;
5694 memcpy(filter
->addr
, target_addr
, filter
->count
* ETH_ALEN
);
5695 unlock_user(target_addr
, arg
+ offsetof(struct tun_filter
, addr
), 0);
5698 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, filter
));
5701 IOCTLEntry ioctl_entries
[] = {
5702 #define IOCTL(cmd, access, ...) \
5703 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5704 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5705 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
5706 #define IOCTL_IGNORE(cmd) \
5707 { TARGET_ ## cmd, 0, #cmd },
5712 /* ??? Implement proper locking for ioctls. */
5713 /* do_ioctl() Must return target values and target errnos. */
5714 static abi_long
do_ioctl(int fd
, int cmd
, abi_long arg
)
5716 const IOCTLEntry
*ie
;
5717 const argtype
*arg_type
;
5719 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
5725 if (ie
->target_cmd
== 0) {
5727 LOG_UNIMP
, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
5728 return -TARGET_ENOSYS
;
5730 if (ie
->target_cmd
== cmd
)
5734 arg_type
= ie
->arg_type
;
5736 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
5737 } else if (!ie
->host_cmd
) {
5738 /* Some architectures define BSD ioctls in their headers
5739 that are not implemented in Linux. */
5740 return -TARGET_ENOSYS
;
5743 switch(arg_type
[0]) {
5746 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
));
5752 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, arg
));
5756 target_size
= thunk_type_size(arg_type
, 0);
5757 switch(ie
->access
) {
5759 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5760 if (!is_error(ret
)) {
5761 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5763 return -TARGET_EFAULT
;
5764 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5765 unlock_user(argptr
, arg
, target_size
);
5769 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5771 return -TARGET_EFAULT
;
5772 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5773 unlock_user(argptr
, arg
, 0);
5774 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5778 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5780 return -TARGET_EFAULT
;
5781 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5782 unlock_user(argptr
, arg
, 0);
5783 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5784 if (!is_error(ret
)) {
5785 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5787 return -TARGET_EFAULT
;
5788 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5789 unlock_user(argptr
, arg
, target_size
);
5795 qemu_log_mask(LOG_UNIMP
,
5796 "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5797 (long)cmd
, arg_type
[0]);
5798 ret
= -TARGET_ENOSYS
;
5804 static const bitmask_transtbl iflag_tbl
[] = {
5805 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
5806 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
5807 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
5808 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
5809 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
5810 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
5811 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
5812 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
5813 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
5814 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
5815 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
5816 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
5817 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
5818 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
5819 { TARGET_IUTF8
, TARGET_IUTF8
, IUTF8
, IUTF8
},
5823 static const bitmask_transtbl oflag_tbl
[] = {
5824 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
5825 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
5826 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
5827 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
5828 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
5829 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
5830 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
5831 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
5832 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
5833 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
5834 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
5835 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
5836 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
5837 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
5838 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
5839 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
5840 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
5841 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
5842 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
5843 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
5844 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
5845 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
5846 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
5847 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
5851 static const bitmask_transtbl cflag_tbl
[] = {
5852 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
5853 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
5854 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
5855 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
5856 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
5857 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
5858 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
5859 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
5860 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
5861 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
5862 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
5863 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
5864 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
5865 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
5866 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
5867 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
5868 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
5869 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
5870 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
5871 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
5872 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
5873 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
5874 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
5875 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
5876 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
5877 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
5878 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
5879 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
5880 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
5881 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
5882 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
5886 static const bitmask_transtbl lflag_tbl
[] = {
5887 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
5888 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
5889 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
5890 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
5891 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
5892 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
5893 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
5894 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
5895 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
5896 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
5897 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
5898 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
5899 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
5900 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
5901 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
5902 { TARGET_EXTPROC
, TARGET_EXTPROC
, EXTPROC
, EXTPROC
},
5906 static void target_to_host_termios (void *dst
, const void *src
)
5908 struct host_termios
*host
= dst
;
5909 const struct target_termios
*target
= src
;
5912 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
5914 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
5916 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
5918 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
5919 host
->c_line
= target
->c_line
;
5921 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
5922 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
5923 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
5924 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
5925 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
5926 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
5927 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
5928 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
5929 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
5930 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
5931 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
5932 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
5933 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
5934 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
5935 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
5936 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
5937 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
5938 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
5941 static void host_to_target_termios (void *dst
, const void *src
)
5943 struct target_termios
*target
= dst
;
5944 const struct host_termios
*host
= src
;
5947 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
5949 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
5951 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
5953 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
5954 target
->c_line
= host
->c_line
;
5956 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
5957 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
5958 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
5959 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
5960 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
5961 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
5962 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
5963 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
5964 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
5965 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
5966 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
5967 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
5968 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
5969 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
5970 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
5971 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
5972 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
5973 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
5976 static const StructEntry struct_termios_def
= {
5977 .convert
= { host_to_target_termios
, target_to_host_termios
},
5978 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
5979 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
5980 .print
= print_termios
,
5983 static const bitmask_transtbl mmap_flags_tbl
[] = {
5984 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
5985 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
5986 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
5987 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
,
5988 MAP_ANONYMOUS
, MAP_ANONYMOUS
},
5989 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
,
5990 MAP_GROWSDOWN
, MAP_GROWSDOWN
},
5991 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
,
5992 MAP_DENYWRITE
, MAP_DENYWRITE
},
5993 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
,
5994 MAP_EXECUTABLE
, MAP_EXECUTABLE
},
5995 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
5996 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
,
5997 MAP_NORESERVE
, MAP_NORESERVE
},
5998 { TARGET_MAP_HUGETLB
, TARGET_MAP_HUGETLB
, MAP_HUGETLB
, MAP_HUGETLB
},
5999 /* MAP_STACK had been ignored by the kernel for quite some time.
6000 Recognize it for the target insofar as we do not want to pass
6001 it through to the host. */
6002 { TARGET_MAP_STACK
, TARGET_MAP_STACK
, 0, 0 },
6007 * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
6008 * TARGET_I386 is defined if TARGET_X86_64 is defined
6010 #if defined(TARGET_I386)
6012 /* NOTE: there is really one LDT for all the threads */
6013 static uint8_t *ldt_table
;
6015 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
6022 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
6023 if (size
> bytecount
)
6025 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
6027 return -TARGET_EFAULT
;
6028 /* ??? Should this by byteswapped? */
6029 memcpy(p
, ldt_table
, size
);
6030 unlock_user(p
, ptr
, size
);
6034 /* XXX: add locking support */
6035 static abi_long
write_ldt(CPUX86State
*env
,
6036 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
6038 struct target_modify_ldt_ldt_s ldt_info
;
6039 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6040 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
6041 int seg_not_present
, useable
, lm
;
6042 uint32_t *lp
, entry_1
, entry_2
;
6044 if (bytecount
!= sizeof(ldt_info
))
6045 return -TARGET_EINVAL
;
6046 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
6047 return -TARGET_EFAULT
;
6048 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
6049 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6050 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6051 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6052 unlock_user_struct(target_ldt_info
, ptr
, 0);
6054 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
6055 return -TARGET_EINVAL
;
6056 seg_32bit
= ldt_info
.flags
& 1;
6057 contents
= (ldt_info
.flags
>> 1) & 3;
6058 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6059 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6060 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6061 useable
= (ldt_info
.flags
>> 6) & 1;
6065 lm
= (ldt_info
.flags
>> 7) & 1;
6067 if (contents
== 3) {
6069 return -TARGET_EINVAL
;
6070 if (seg_not_present
== 0)
6071 return -TARGET_EINVAL
;
6073 /* allocate the LDT */
6075 env
->ldt
.base
= target_mmap(0,
6076 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
6077 PROT_READ
|PROT_WRITE
,
6078 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
6079 if (env
->ldt
.base
== -1)
6080 return -TARGET_ENOMEM
;
6081 memset(g2h_untagged(env
->ldt
.base
), 0,
6082 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
6083 env
->ldt
.limit
= 0xffff;
6084 ldt_table
= g2h_untagged(env
->ldt
.base
);
6087 /* NOTE: same code as Linux kernel */
6088 /* Allow LDTs to be cleared by the user. */
6089 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6092 read_exec_only
== 1 &&
6094 limit_in_pages
== 0 &&
6095 seg_not_present
== 1 &&
6103 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6104 (ldt_info
.limit
& 0x0ffff);
6105 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6106 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6107 (ldt_info
.limit
& 0xf0000) |
6108 ((read_exec_only
^ 1) << 9) |
6110 ((seg_not_present
^ 1) << 15) |
6112 (limit_in_pages
<< 23) |
6116 entry_2
|= (useable
<< 20);
6118 /* Install the new entry ... */
6120 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
6121 lp
[0] = tswap32(entry_1
);
6122 lp
[1] = tswap32(entry_2
);
6126 /* specific and weird i386 syscalls */
6127 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
6128 unsigned long bytecount
)
6134 ret
= read_ldt(ptr
, bytecount
);
6137 ret
= write_ldt(env
, ptr
, bytecount
, 1);
6140 ret
= write_ldt(env
, ptr
, bytecount
, 0);
6143 ret
= -TARGET_ENOSYS
;
6149 #if defined(TARGET_ABI32)
6150 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6152 uint64_t *gdt_table
= g2h_untagged(env
->gdt
.base
);
6153 struct target_modify_ldt_ldt_s ldt_info
;
6154 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6155 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
6156 int seg_not_present
, useable
, lm
;
6157 uint32_t *lp
, entry_1
, entry_2
;
6160 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6161 if (!target_ldt_info
)
6162 return -TARGET_EFAULT
;
6163 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
6164 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6165 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6166 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6167 if (ldt_info
.entry_number
== -1) {
6168 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
6169 if (gdt_table
[i
] == 0) {
6170 ldt_info
.entry_number
= i
;
6171 target_ldt_info
->entry_number
= tswap32(i
);
6176 unlock_user_struct(target_ldt_info
, ptr
, 1);
6178 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
6179 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
6180 return -TARGET_EINVAL
;
6181 seg_32bit
= ldt_info
.flags
& 1;
6182 contents
= (ldt_info
.flags
>> 1) & 3;
6183 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6184 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6185 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6186 useable
= (ldt_info
.flags
>> 6) & 1;
6190 lm
= (ldt_info
.flags
>> 7) & 1;
6193 if (contents
== 3) {
6194 if (seg_not_present
== 0)
6195 return -TARGET_EINVAL
;
6198 /* NOTE: same code as Linux kernel */
6199 /* Allow LDTs to be cleared by the user. */
6200 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6201 if ((contents
== 0 &&
6202 read_exec_only
== 1 &&
6204 limit_in_pages
== 0 &&
6205 seg_not_present
== 1 &&
6213 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6214 (ldt_info
.limit
& 0x0ffff);
6215 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6216 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6217 (ldt_info
.limit
& 0xf0000) |
6218 ((read_exec_only
^ 1) << 9) |
6220 ((seg_not_present
^ 1) << 15) |
6222 (limit_in_pages
<< 23) |
6227 /* Install the new entry ... */
6229 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
6230 lp
[0] = tswap32(entry_1
);
6231 lp
[1] = tswap32(entry_2
);
6235 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6237 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6238 uint64_t *gdt_table
= g2h_untagged(env
->gdt
.base
);
6239 uint32_t base_addr
, limit
, flags
;
6240 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
6241 int seg_not_present
, useable
, lm
;
6242 uint32_t *lp
, entry_1
, entry_2
;
6244 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6245 if (!target_ldt_info
)
6246 return -TARGET_EFAULT
;
6247 idx
= tswap32(target_ldt_info
->entry_number
);
6248 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
6249 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
6250 unlock_user_struct(target_ldt_info
, ptr
, 1);
6251 return -TARGET_EINVAL
;
6253 lp
= (uint32_t *)(gdt_table
+ idx
);
6254 entry_1
= tswap32(lp
[0]);
6255 entry_2
= tswap32(lp
[1]);
6257 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
6258 contents
= (entry_2
>> 10) & 3;
6259 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
6260 seg_32bit
= (entry_2
>> 22) & 1;
6261 limit_in_pages
= (entry_2
>> 23) & 1;
6262 useable
= (entry_2
>> 20) & 1;
6266 lm
= (entry_2
>> 21) & 1;
6268 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
6269 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
6270 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
6271 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
6272 base_addr
= (entry_1
>> 16) |
6273 (entry_2
& 0xff000000) |
6274 ((entry_2
& 0xff) << 16);
6275 target_ldt_info
->base_addr
= tswapal(base_addr
);
6276 target_ldt_info
->limit
= tswap32(limit
);
6277 target_ldt_info
->flags
= tswap32(flags
);
6278 unlock_user_struct(target_ldt_info
, ptr
, 1);
6282 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
6284 return -TARGET_ENOSYS
;
6287 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
6294 case TARGET_ARCH_SET_GS
:
6295 case TARGET_ARCH_SET_FS
:
6296 if (code
== TARGET_ARCH_SET_GS
)
6300 cpu_x86_load_seg(env
, idx
, 0);
6301 env
->segs
[idx
].base
= addr
;
6303 case TARGET_ARCH_GET_GS
:
6304 case TARGET_ARCH_GET_FS
:
6305 if (code
== TARGET_ARCH_GET_GS
)
6309 val
= env
->segs
[idx
].base
;
6310 if (put_user(val
, addr
, abi_ulong
))
6311 ret
= -TARGET_EFAULT
;
6314 ret
= -TARGET_EINVAL
;
6319 #endif /* defined(TARGET_ABI32 */
6320 #endif /* defined(TARGET_I386) */
6323 * These constants are generic. Supply any that are missing from the host.
6326 # define PR_SET_NAME 15
6327 # define PR_GET_NAME 16
6329 #ifndef PR_SET_FP_MODE
6330 # define PR_SET_FP_MODE 45
6331 # define PR_GET_FP_MODE 46
6332 # define PR_FP_MODE_FR (1 << 0)
6333 # define PR_FP_MODE_FRE (1 << 1)
6335 #ifndef PR_SVE_SET_VL
6336 # define PR_SVE_SET_VL 50
6337 # define PR_SVE_GET_VL 51
6338 # define PR_SVE_VL_LEN_MASK 0xffff
6339 # define PR_SVE_VL_INHERIT (1 << 17)
6341 #ifndef PR_PAC_RESET_KEYS
6342 # define PR_PAC_RESET_KEYS 54
6343 # define PR_PAC_APIAKEY (1 << 0)
6344 # define PR_PAC_APIBKEY (1 << 1)
6345 # define PR_PAC_APDAKEY (1 << 2)
6346 # define PR_PAC_APDBKEY (1 << 3)
6347 # define PR_PAC_APGAKEY (1 << 4)
6349 #ifndef PR_SET_TAGGED_ADDR_CTRL
6350 # define PR_SET_TAGGED_ADDR_CTRL 55
6351 # define PR_GET_TAGGED_ADDR_CTRL 56
6352 # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
6354 #ifndef PR_MTE_TCF_SHIFT
6355 # define PR_MTE_TCF_SHIFT 1
6356 # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
6357 # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
6358 # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
6359 # define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
6360 # define PR_MTE_TAG_SHIFT 3
6361 # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
6363 #ifndef PR_SET_IO_FLUSHER
6364 # define PR_SET_IO_FLUSHER 57
6365 # define PR_GET_IO_FLUSHER 58
6367 #ifndef PR_SET_SYSCALL_USER_DISPATCH
6368 # define PR_SET_SYSCALL_USER_DISPATCH 59
6370 #ifndef PR_SME_SET_VL
6371 # define PR_SME_SET_VL 63
6372 # define PR_SME_GET_VL 64
6373 # define PR_SME_VL_LEN_MASK 0xffff
6374 # define PR_SME_VL_INHERIT (1 << 17)
6377 #include "target_prctl.h"
6379 static abi_long
do_prctl_inval0(CPUArchState
*env
)
6381 return -TARGET_EINVAL
;
6384 static abi_long
do_prctl_inval1(CPUArchState
*env
, abi_long arg2
)
6386 return -TARGET_EINVAL
;
6389 #ifndef do_prctl_get_fp_mode
6390 #define do_prctl_get_fp_mode do_prctl_inval0
6392 #ifndef do_prctl_set_fp_mode
6393 #define do_prctl_set_fp_mode do_prctl_inval1
6395 #ifndef do_prctl_sve_get_vl
6396 #define do_prctl_sve_get_vl do_prctl_inval0
6398 #ifndef do_prctl_sve_set_vl
6399 #define do_prctl_sve_set_vl do_prctl_inval1
6401 #ifndef do_prctl_reset_keys
6402 #define do_prctl_reset_keys do_prctl_inval1
6404 #ifndef do_prctl_set_tagged_addr_ctrl
6405 #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
6407 #ifndef do_prctl_get_tagged_addr_ctrl
6408 #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
6410 #ifndef do_prctl_get_unalign
6411 #define do_prctl_get_unalign do_prctl_inval1
6413 #ifndef do_prctl_set_unalign
6414 #define do_prctl_set_unalign do_prctl_inval1
6416 #ifndef do_prctl_sme_get_vl
6417 #define do_prctl_sme_get_vl do_prctl_inval0
6419 #ifndef do_prctl_sme_set_vl
6420 #define do_prctl_sme_set_vl do_prctl_inval1
6423 static abi_long
do_prctl(CPUArchState
*env
, abi_long option
, abi_long arg2
,
6424 abi_long arg3
, abi_long arg4
, abi_long arg5
)
6429 case PR_GET_PDEATHSIG
:
6432 ret
= get_errno(prctl(PR_GET_PDEATHSIG
, &deathsig
,
6434 if (!is_error(ret
) &&
6435 put_user_s32(host_to_target_signal(deathsig
), arg2
)) {
6436 return -TARGET_EFAULT
;
6440 case PR_SET_PDEATHSIG
:
6441 return get_errno(prctl(PR_SET_PDEATHSIG
, target_to_host_signal(arg2
),
6445 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
6447 return -TARGET_EFAULT
;
6449 ret
= get_errno(prctl(PR_GET_NAME
, (uintptr_t)name
,
6451 unlock_user(name
, arg2
, 16);
6456 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
6458 return -TARGET_EFAULT
;
6460 ret
= get_errno(prctl(PR_SET_NAME
, (uintptr_t)name
,
6462 unlock_user(name
, arg2
, 0);
6465 case PR_GET_FP_MODE
:
6466 return do_prctl_get_fp_mode(env
);
6467 case PR_SET_FP_MODE
:
6468 return do_prctl_set_fp_mode(env
, arg2
);
6470 return do_prctl_sve_get_vl(env
);
6472 return do_prctl_sve_set_vl(env
, arg2
);
6474 return do_prctl_sme_get_vl(env
);
6476 return do_prctl_sme_set_vl(env
, arg2
);
6477 case PR_PAC_RESET_KEYS
:
6478 if (arg3
|| arg4
|| arg5
) {
6479 return -TARGET_EINVAL
;
6481 return do_prctl_reset_keys(env
, arg2
);
6482 case PR_SET_TAGGED_ADDR_CTRL
:
6483 if (arg3
|| arg4
|| arg5
) {
6484 return -TARGET_EINVAL
;
6486 return do_prctl_set_tagged_addr_ctrl(env
, arg2
);
6487 case PR_GET_TAGGED_ADDR_CTRL
:
6488 if (arg2
|| arg3
|| arg4
|| arg5
) {
6489 return -TARGET_EINVAL
;
6491 return do_prctl_get_tagged_addr_ctrl(env
);
6493 case PR_GET_UNALIGN
:
6494 return do_prctl_get_unalign(env
, arg2
);
6495 case PR_SET_UNALIGN
:
6496 return do_prctl_set_unalign(env
, arg2
);
6498 case PR_CAP_AMBIENT
:
6499 case PR_CAPBSET_READ
:
6500 case PR_CAPBSET_DROP
:
6501 case PR_GET_DUMPABLE
:
6502 case PR_SET_DUMPABLE
:
6503 case PR_GET_KEEPCAPS
:
6504 case PR_SET_KEEPCAPS
:
6505 case PR_GET_SECUREBITS
:
6506 case PR_SET_SECUREBITS
:
6509 case PR_GET_TIMERSLACK
:
6510 case PR_SET_TIMERSLACK
:
6512 case PR_MCE_KILL_GET
:
6513 case PR_GET_NO_NEW_PRIVS
:
6514 case PR_SET_NO_NEW_PRIVS
:
6515 case PR_GET_IO_FLUSHER
:
6516 case PR_SET_IO_FLUSHER
:
6517 /* Some prctl options have no pointer arguments and we can pass on. */
6518 return get_errno(prctl(option
, arg2
, arg3
, arg4
, arg5
));
6520 case PR_GET_CHILD_SUBREAPER
:
6521 case PR_SET_CHILD_SUBREAPER
:
6522 case PR_GET_SPECULATION_CTRL
:
6523 case PR_SET_SPECULATION_CTRL
:
6524 case PR_GET_TID_ADDRESS
:
6526 return -TARGET_EINVAL
;
6530 /* Was used for SPE on PowerPC. */
6531 return -TARGET_EINVAL
;
6538 case PR_GET_SECCOMP
:
6539 case PR_SET_SECCOMP
:
6540 case PR_SET_SYSCALL_USER_DISPATCH
:
6541 case PR_GET_THP_DISABLE
:
6542 case PR_SET_THP_DISABLE
:
6545 /* Disable to prevent the target disabling stuff we need. */
6546 return -TARGET_EINVAL
;
6549 qemu_log_mask(LOG_UNIMP
, "Unsupported prctl: " TARGET_ABI_FMT_ld
"\n",
6551 return -TARGET_EINVAL
;
6555 #define NEW_STACK_SIZE 0x40000
6558 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
6561 pthread_mutex_t mutex
;
6562 pthread_cond_t cond
;
6565 abi_ulong child_tidptr
;
6566 abi_ulong parent_tidptr
;
6570 static void *clone_func(void *arg
)
6572 new_thread_info
*info
= arg
;
6577 rcu_register_thread();
6578 tcg_register_thread();
6582 ts
= (TaskState
*)cpu
->opaque
;
6583 info
->tid
= sys_gettid();
6585 if (info
->child_tidptr
)
6586 put_user_u32(info
->tid
, info
->child_tidptr
);
6587 if (info
->parent_tidptr
)
6588 put_user_u32(info
->tid
, info
->parent_tidptr
);
6589 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
6590 /* Enable signals. */
6591 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
6592 /* Signal to the parent that we're ready. */
6593 pthread_mutex_lock(&info
->mutex
);
6594 pthread_cond_broadcast(&info
->cond
);
6595 pthread_mutex_unlock(&info
->mutex
);
6596 /* Wait until the parent has finished initializing the tls state. */
6597 pthread_mutex_lock(&clone_lock
);
6598 pthread_mutex_unlock(&clone_lock
);
6604 /* do_fork() Must return host values and target errnos (unlike most
6605 do_*() functions). */
6606 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
6607 abi_ulong parent_tidptr
, target_ulong newtls
,
6608 abi_ulong child_tidptr
)
6610 CPUState
*cpu
= env_cpu(env
);
6614 CPUArchState
*new_env
;
6617 flags
&= ~CLONE_IGNORED_FLAGS
;
6619 /* Emulate vfork() with fork() */
6620 if (flags
& CLONE_VFORK
)
6621 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
6623 if (flags
& CLONE_VM
) {
6624 TaskState
*parent_ts
= (TaskState
*)cpu
->opaque
;
6625 new_thread_info info
;
6626 pthread_attr_t attr
;
6628 if (((flags
& CLONE_THREAD_FLAGS
) != CLONE_THREAD_FLAGS
) ||
6629 (flags
& CLONE_INVALID_THREAD_FLAGS
)) {
6630 return -TARGET_EINVAL
;
6633 ts
= g_new0(TaskState
, 1);
6634 init_task_state(ts
);
6636 /* Grab a mutex so that thread setup appears atomic. */
6637 pthread_mutex_lock(&clone_lock
);
6640 * If this is our first additional thread, we need to ensure we
6641 * generate code for parallel execution and flush old translations.
6642 * Do this now so that the copy gets CF_PARALLEL too.
6644 if (!(cpu
->tcg_cflags
& CF_PARALLEL
)) {
6645 cpu
->tcg_cflags
|= CF_PARALLEL
;
6649 /* we create a new CPU instance. */
6650 new_env
= cpu_copy(env
);
6651 /* Init regs that differ from the parent. */
6652 cpu_clone_regs_child(new_env
, newsp
, flags
);
6653 cpu_clone_regs_parent(env
, flags
);
6654 new_cpu
= env_cpu(new_env
);
6655 new_cpu
->opaque
= ts
;
6656 ts
->bprm
= parent_ts
->bprm
;
6657 ts
->info
= parent_ts
->info
;
6658 ts
->signal_mask
= parent_ts
->signal_mask
;
6660 if (flags
& CLONE_CHILD_CLEARTID
) {
6661 ts
->child_tidptr
= child_tidptr
;
6664 if (flags
& CLONE_SETTLS
) {
6665 cpu_set_tls (new_env
, newtls
);
6668 memset(&info
, 0, sizeof(info
));
6669 pthread_mutex_init(&info
.mutex
, NULL
);
6670 pthread_mutex_lock(&info
.mutex
);
6671 pthread_cond_init(&info
.cond
, NULL
);
6673 if (flags
& CLONE_CHILD_SETTID
) {
6674 info
.child_tidptr
= child_tidptr
;
6676 if (flags
& CLONE_PARENT_SETTID
) {
6677 info
.parent_tidptr
= parent_tidptr
;
6680 ret
= pthread_attr_init(&attr
);
6681 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
6682 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
6683 /* It is not safe to deliver signals until the child has finished
6684 initializing, so temporarily block all signals. */
6685 sigfillset(&sigmask
);
6686 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
6687 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
6689 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
6690 /* TODO: Free new CPU state if thread creation failed. */
6692 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
6693 pthread_attr_destroy(&attr
);
6695 /* Wait for the child to initialize. */
6696 pthread_cond_wait(&info
.cond
, &info
.mutex
);
6701 pthread_mutex_unlock(&info
.mutex
);
6702 pthread_cond_destroy(&info
.cond
);
6703 pthread_mutex_destroy(&info
.mutex
);
6704 pthread_mutex_unlock(&clone_lock
);
6706 /* if no CLONE_VM, we consider it is a fork */
6707 if (flags
& CLONE_INVALID_FORK_FLAGS
) {
6708 return -TARGET_EINVAL
;
6711 /* We can't support custom termination signals */
6712 if ((flags
& CSIGNAL
) != TARGET_SIGCHLD
) {
6713 return -TARGET_EINVAL
;
6716 if (block_signals()) {
6717 return -QEMU_ERESTARTSYS
;
6723 /* Child Process. */
6724 cpu_clone_regs_child(env
, newsp
, flags
);
6726 /* There is a race condition here. The parent process could
6727 theoretically read the TID in the child process before the child
6728 tid is set. This would require using either ptrace
6729 (not implemented) or having *_tidptr to point at a shared memory
6730 mapping. We can't repeat the spinlock hack used above because
6731 the child process gets its own copy of the lock. */
6732 if (flags
& CLONE_CHILD_SETTID
)
6733 put_user_u32(sys_gettid(), child_tidptr
);
6734 if (flags
& CLONE_PARENT_SETTID
)
6735 put_user_u32(sys_gettid(), parent_tidptr
);
6736 ts
= (TaskState
*)cpu
->opaque
;
6737 if (flags
& CLONE_SETTLS
)
6738 cpu_set_tls (env
, newtls
);
6739 if (flags
& CLONE_CHILD_CLEARTID
)
6740 ts
->child_tidptr
= child_tidptr
;
6742 cpu_clone_regs_parent(env
, flags
);
6749 /* warning : doesn't handle linux specific flags... */
6750 static int target_to_host_fcntl_cmd(int cmd
)
6755 case TARGET_F_DUPFD
:
6756 case TARGET_F_GETFD
:
6757 case TARGET_F_SETFD
:
6758 case TARGET_F_GETFL
:
6759 case TARGET_F_SETFL
:
6760 case TARGET_F_OFD_GETLK
:
6761 case TARGET_F_OFD_SETLK
:
6762 case TARGET_F_OFD_SETLKW
:
6765 case TARGET_F_GETLK
:
6768 case TARGET_F_SETLK
:
6771 case TARGET_F_SETLKW
:
6774 case TARGET_F_GETOWN
:
6777 case TARGET_F_SETOWN
:
6780 case TARGET_F_GETSIG
:
6783 case TARGET_F_SETSIG
:
6786 #if TARGET_ABI_BITS == 32
6787 case TARGET_F_GETLK64
:
6790 case TARGET_F_SETLK64
:
6793 case TARGET_F_SETLKW64
:
6797 case TARGET_F_SETLEASE
:
6800 case TARGET_F_GETLEASE
:
6803 #ifdef F_DUPFD_CLOEXEC
6804 case TARGET_F_DUPFD_CLOEXEC
:
6805 ret
= F_DUPFD_CLOEXEC
;
6808 case TARGET_F_NOTIFY
:
6812 case TARGET_F_GETOWN_EX
:
6817 case TARGET_F_SETOWN_EX
:
6822 case TARGET_F_SETPIPE_SZ
:
6825 case TARGET_F_GETPIPE_SZ
:
6830 case TARGET_F_ADD_SEALS
:
6833 case TARGET_F_GET_SEALS
:
6838 ret
= -TARGET_EINVAL
;
6842 #if defined(__powerpc64__)
6843 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6844 * is not supported by kernel. The glibc fcntl call actually adjusts
6845 * them to 5, 6 and 7 before making the syscall(). Since we make the
6846 * syscall directly, adjust to what is supported by the kernel.
6848 if (ret
>= F_GETLK64
&& ret
<= F_SETLKW64
) {
6849 ret
-= F_GETLK64
- 5;
6856 #define FLOCK_TRANSTBL \
6858 TRANSTBL_CONVERT(F_RDLCK); \
6859 TRANSTBL_CONVERT(F_WRLCK); \
6860 TRANSTBL_CONVERT(F_UNLCK); \
6863 static int target_to_host_flock(int type
)
6865 #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6867 #undef TRANSTBL_CONVERT
6868 return -TARGET_EINVAL
;
6871 static int host_to_target_flock(int type
)
6873 #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6875 #undef TRANSTBL_CONVERT
6876 /* if we don't know how to convert the value coming
6877 * from the host we copy to the target field as-is
6882 static inline abi_long
copy_from_user_flock(struct flock64
*fl
,
6883 abi_ulong target_flock_addr
)
6885 struct target_flock
*target_fl
;
6888 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6889 return -TARGET_EFAULT
;
6892 __get_user(l_type
, &target_fl
->l_type
);
6893 l_type
= target_to_host_flock(l_type
);
6897 fl
->l_type
= l_type
;
6898 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6899 __get_user(fl
->l_start
, &target_fl
->l_start
);
6900 __get_user(fl
->l_len
, &target_fl
->l_len
);
6901 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6902 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6906 static inline abi_long
copy_to_user_flock(abi_ulong target_flock_addr
,
6907 const struct flock64
*fl
)
6909 struct target_flock
*target_fl
;
6912 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6913 return -TARGET_EFAULT
;
6916 l_type
= host_to_target_flock(fl
->l_type
);
6917 __put_user(l_type
, &target_fl
->l_type
);
6918 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6919 __put_user(fl
->l_start
, &target_fl
->l_start
);
6920 __put_user(fl
->l_len
, &target_fl
->l_len
);
6921 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6922 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6926 typedef abi_long
from_flock64_fn(struct flock64
*fl
, abi_ulong target_addr
);
6927 typedef abi_long
to_flock64_fn(abi_ulong target_addr
, const struct flock64
*fl
);
6929 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6930 struct target_oabi_flock64
{
6938 static inline abi_long
copy_from_user_oabi_flock64(struct flock64
*fl
,
6939 abi_ulong target_flock_addr
)
6941 struct target_oabi_flock64
*target_fl
;
6944 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6945 return -TARGET_EFAULT
;
6948 __get_user(l_type
, &target_fl
->l_type
);
6949 l_type
= target_to_host_flock(l_type
);
6953 fl
->l_type
= l_type
;
6954 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6955 __get_user(fl
->l_start
, &target_fl
->l_start
);
6956 __get_user(fl
->l_len
, &target_fl
->l_len
);
6957 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6958 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6962 static inline abi_long
copy_to_user_oabi_flock64(abi_ulong target_flock_addr
,
6963 const struct flock64
*fl
)
6965 struct target_oabi_flock64
*target_fl
;
6968 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6969 return -TARGET_EFAULT
;
6972 l_type
= host_to_target_flock(fl
->l_type
);
6973 __put_user(l_type
, &target_fl
->l_type
);
6974 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6975 __put_user(fl
->l_start
, &target_fl
->l_start
);
6976 __put_user(fl
->l_len
, &target_fl
->l_len
);
6977 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6978 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6983 static inline abi_long
copy_from_user_flock64(struct flock64
*fl
,
6984 abi_ulong target_flock_addr
)
6986 struct target_flock64
*target_fl
;
6989 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6990 return -TARGET_EFAULT
;
6993 __get_user(l_type
, &target_fl
->l_type
);
6994 l_type
= target_to_host_flock(l_type
);
6998 fl
->l_type
= l_type
;
6999 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
7000 __get_user(fl
->l_start
, &target_fl
->l_start
);
7001 __get_user(fl
->l_len
, &target_fl
->l_len
);
7002 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
7003 unlock_user_struct(target_fl
, target_flock_addr
, 0);
7007 static inline abi_long
copy_to_user_flock64(abi_ulong target_flock_addr
,
7008 const struct flock64
*fl
)
7010 struct target_flock64
*target_fl
;
7013 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
7014 return -TARGET_EFAULT
;
7017 l_type
= host_to_target_flock(fl
->l_type
);
7018 __put_user(l_type
, &target_fl
->l_type
);
7019 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
7020 __put_user(fl
->l_start
, &target_fl
->l_start
);
7021 __put_user(fl
->l_len
, &target_fl
->l_len
);
7022 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
7023 unlock_user_struct(target_fl
, target_flock_addr
, 1);
7027 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
7029 struct flock64 fl64
;
7031 struct f_owner_ex fox
;
7032 struct target_f_owner_ex
*target_fox
;
7035 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
7037 if (host_cmd
== -TARGET_EINVAL
)
7041 case TARGET_F_GETLK
:
7042 ret
= copy_from_user_flock(&fl64
, arg
);
7046 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7048 ret
= copy_to_user_flock(arg
, &fl64
);
7052 case TARGET_F_SETLK
:
7053 case TARGET_F_SETLKW
:
7054 ret
= copy_from_user_flock(&fl64
, arg
);
7058 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7061 case TARGET_F_GETLK64
:
7062 case TARGET_F_OFD_GETLK
:
7063 ret
= copy_from_user_flock64(&fl64
, arg
);
7067 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7069 ret
= copy_to_user_flock64(arg
, &fl64
);
7072 case TARGET_F_SETLK64
:
7073 case TARGET_F_SETLKW64
:
7074 case TARGET_F_OFD_SETLK
:
7075 case TARGET_F_OFD_SETLKW
:
7076 ret
= copy_from_user_flock64(&fl64
, arg
);
7080 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7083 case TARGET_F_GETFL
:
7084 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
7086 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
7090 case TARGET_F_SETFL
:
7091 ret
= get_errno(safe_fcntl(fd
, host_cmd
,
7092 target_to_host_bitmask(arg
,
7097 case TARGET_F_GETOWN_EX
:
7098 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
7100 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
7101 return -TARGET_EFAULT
;
7102 target_fox
->type
= tswap32(fox
.type
);
7103 target_fox
->pid
= tswap32(fox
.pid
);
7104 unlock_user_struct(target_fox
, arg
, 1);
7110 case TARGET_F_SETOWN_EX
:
7111 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
7112 return -TARGET_EFAULT
;
7113 fox
.type
= tswap32(target_fox
->type
);
7114 fox
.pid
= tswap32(target_fox
->pid
);
7115 unlock_user_struct(target_fox
, arg
, 0);
7116 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
7120 case TARGET_F_SETSIG
:
7121 ret
= get_errno(safe_fcntl(fd
, host_cmd
, target_to_host_signal(arg
)));
7124 case TARGET_F_GETSIG
:
7125 ret
= host_to_target_signal(get_errno(safe_fcntl(fd
, host_cmd
, arg
)));
7128 case TARGET_F_SETOWN
:
7129 case TARGET_F_GETOWN
:
7130 case TARGET_F_SETLEASE
:
7131 case TARGET_F_GETLEASE
:
7132 case TARGET_F_SETPIPE_SZ
:
7133 case TARGET_F_GETPIPE_SZ
:
7134 case TARGET_F_ADD_SEALS
:
7135 case TARGET_F_GET_SEALS
:
7136 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
7140 ret
= get_errno(safe_fcntl(fd
, cmd
, arg
));
7148 static inline int high2lowuid(int uid
)
7156 static inline int high2lowgid(int gid
)
7164 static inline int low2highuid(int uid
)
7166 if ((int16_t)uid
== -1)
7172 static inline int low2highgid(int gid
)
7174 if ((int16_t)gid
== -1)
7179 static inline int tswapid(int id
)
7184 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
7186 #else /* !USE_UID16 */
7187 static inline int high2lowuid(int uid
)
7191 static inline int high2lowgid(int gid
)
7195 static inline int low2highuid(int uid
)
7199 static inline int low2highgid(int gid
)
7203 static inline int tswapid(int id
)
7208 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
7210 #endif /* USE_UID16 */
7212 /* We must do direct syscalls for setting UID/GID, because we want to
7213 * implement the Linux system call semantics of "change only for this thread",
7214 * not the libc/POSIX semantics of "change for all threads in process".
7215 * (See http://ewontfix.com/17/ for more details.)
7216 * We use the 32-bit version of the syscalls if present; if it is not
7217 * then either the host architecture supports 32-bit UIDs natively with
7218 * the standard syscall, or the 16-bit UID is the best we can do.
7220 #ifdef __NR_setuid32
7221 #define __NR_sys_setuid __NR_setuid32
7223 #define __NR_sys_setuid __NR_setuid
7225 #ifdef __NR_setgid32
7226 #define __NR_sys_setgid __NR_setgid32
7228 #define __NR_sys_setgid __NR_setgid
7230 #ifdef __NR_setresuid32
7231 #define __NR_sys_setresuid __NR_setresuid32
7233 #define __NR_sys_setresuid __NR_setresuid
7235 #ifdef __NR_setresgid32
7236 #define __NR_sys_setresgid __NR_setresgid32
7238 #define __NR_sys_setresgid __NR_setresgid
7241 _syscall1(int, sys_setuid
, uid_t
, uid
)
7242 _syscall1(int, sys_setgid
, gid_t
, gid
)
7243 _syscall3(int, sys_setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
7244 _syscall3(int, sys_setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
7246 void syscall_init(void)
7249 const argtype
*arg_type
;
7252 thunk_init(STRUCT_MAX
);
7254 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
7255 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
7256 #include "syscall_types.h"
7258 #undef STRUCT_SPECIAL
7260 /* we patch the ioctl size if necessary. We rely on the fact that
7261 no ioctl has all the bits at '1' in the size field */
7263 while (ie
->target_cmd
!= 0) {
7264 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
7265 TARGET_IOC_SIZEMASK
) {
7266 arg_type
= ie
->arg_type
;
7267 if (arg_type
[0] != TYPE_PTR
) {
7268 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
7273 size
= thunk_type_size(arg_type
, 0);
7274 ie
->target_cmd
= (ie
->target_cmd
&
7275 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
7276 (size
<< TARGET_IOC_SIZESHIFT
);
7279 /* automatic consistency check if same arch */
7280 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7281 (defined(__x86_64__) && defined(TARGET_X86_64))
7282 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
7283 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
7284 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
7291 #ifdef TARGET_NR_truncate64
7292 static inline abi_long
target_truncate64(CPUArchState
*cpu_env
, const char *arg1
,
7297 if (regpairs_aligned(cpu_env
, TARGET_NR_truncate64
)) {
7301 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
7305 #ifdef TARGET_NR_ftruncate64
7306 static inline abi_long
target_ftruncate64(CPUArchState
*cpu_env
, abi_long arg1
,
7311 if (regpairs_aligned(cpu_env
, TARGET_NR_ftruncate64
)) {
7315 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
7319 #if defined(TARGET_NR_timer_settime) || \
7320 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
7321 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_its
,
7322 abi_ulong target_addr
)
7324 if (target_to_host_timespec(&host_its
->it_interval
, target_addr
+
7325 offsetof(struct target_itimerspec
,
7327 target_to_host_timespec(&host_its
->it_value
, target_addr
+
7328 offsetof(struct target_itimerspec
,
7330 return -TARGET_EFAULT
;
7337 #if defined(TARGET_NR_timer_settime64) || \
7338 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
7339 static inline abi_long
target_to_host_itimerspec64(struct itimerspec
*host_its
,
7340 abi_ulong target_addr
)
7342 if (target_to_host_timespec64(&host_its
->it_interval
, target_addr
+
7343 offsetof(struct target__kernel_itimerspec
,
7345 target_to_host_timespec64(&host_its
->it_value
, target_addr
+
7346 offsetof(struct target__kernel_itimerspec
,
7348 return -TARGET_EFAULT
;
7355 #if ((defined(TARGET_NR_timerfd_gettime) || \
7356 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
7357 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
7358 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
7359 struct itimerspec
*host_its
)
7361 if (host_to_target_timespec(target_addr
+ offsetof(struct target_itimerspec
,
7363 &host_its
->it_interval
) ||
7364 host_to_target_timespec(target_addr
+ offsetof(struct target_itimerspec
,
7366 &host_its
->it_value
)) {
7367 return -TARGET_EFAULT
;
7373 #if ((defined(TARGET_NR_timerfd_gettime64) || \
7374 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
7375 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
7376 static inline abi_long
host_to_target_itimerspec64(abi_ulong target_addr
,
7377 struct itimerspec
*host_its
)
7379 if (host_to_target_timespec64(target_addr
+
7380 offsetof(struct target__kernel_itimerspec
,
7382 &host_its
->it_interval
) ||
7383 host_to_target_timespec64(target_addr
+
7384 offsetof(struct target__kernel_itimerspec
,
7386 &host_its
->it_value
)) {
7387 return -TARGET_EFAULT
;
7393 #if defined(TARGET_NR_adjtimex) || \
7394 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
7395 static inline abi_long
target_to_host_timex(struct timex
*host_tx
,
7396 abi_long target_addr
)
7398 struct target_timex
*target_tx
;
7400 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
7401 return -TARGET_EFAULT
;
7404 __get_user(host_tx
->modes
, &target_tx
->modes
);
7405 __get_user(host_tx
->offset
, &target_tx
->offset
);
7406 __get_user(host_tx
->freq
, &target_tx
->freq
);
7407 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7408 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
7409 __get_user(host_tx
->status
, &target_tx
->status
);
7410 __get_user(host_tx
->constant
, &target_tx
->constant
);
7411 __get_user(host_tx
->precision
, &target_tx
->precision
);
7412 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7413 __get_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7414 __get_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7415 __get_user(host_tx
->tick
, &target_tx
->tick
);
7416 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7417 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
7418 __get_user(host_tx
->shift
, &target_tx
->shift
);
7419 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
7420 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7421 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7422 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7423 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7424 __get_user(host_tx
->tai
, &target_tx
->tai
);
7426 unlock_user_struct(target_tx
, target_addr
, 0);
7430 static inline abi_long
host_to_target_timex(abi_long target_addr
,
7431 struct timex
*host_tx
)
7433 struct target_timex
*target_tx
;
7435 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
7436 return -TARGET_EFAULT
;
7439 __put_user(host_tx
->modes
, &target_tx
->modes
);
7440 __put_user(host_tx
->offset
, &target_tx
->offset
);
7441 __put_user(host_tx
->freq
, &target_tx
->freq
);
7442 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7443 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
7444 __put_user(host_tx
->status
, &target_tx
->status
);
7445 __put_user(host_tx
->constant
, &target_tx
->constant
);
7446 __put_user(host_tx
->precision
, &target_tx
->precision
);
7447 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7448 __put_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7449 __put_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7450 __put_user(host_tx
->tick
, &target_tx
->tick
);
7451 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7452 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
7453 __put_user(host_tx
->shift
, &target_tx
->shift
);
7454 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
7455 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7456 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7457 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7458 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7459 __put_user(host_tx
->tai
, &target_tx
->tai
);
7461 unlock_user_struct(target_tx
, target_addr
, 1);
7467 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
7468 static inline abi_long
target_to_host_timex64(struct timex
*host_tx
,
7469 abi_long target_addr
)
7471 struct target__kernel_timex
*target_tx
;
7473 if (copy_from_user_timeval64(&host_tx
->time
, target_addr
+
7474 offsetof(struct target__kernel_timex
,
7476 return -TARGET_EFAULT
;
7479 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
7480 return -TARGET_EFAULT
;
7483 __get_user(host_tx
->modes
, &target_tx
->modes
);
7484 __get_user(host_tx
->offset
, &target_tx
->offset
);
7485 __get_user(host_tx
->freq
, &target_tx
->freq
);
7486 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7487 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
7488 __get_user(host_tx
->status
, &target_tx
->status
);
7489 __get_user(host_tx
->constant
, &target_tx
->constant
);
7490 __get_user(host_tx
->precision
, &target_tx
->precision
);
7491 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7492 __get_user(host_tx
->tick
, &target_tx
->tick
);
7493 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7494 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
7495 __get_user(host_tx
->shift
, &target_tx
->shift
);
7496 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
7497 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7498 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7499 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7500 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7501 __get_user(host_tx
->tai
, &target_tx
->tai
);
7503 unlock_user_struct(target_tx
, target_addr
, 0);
7507 static inline abi_long
host_to_target_timex64(abi_long target_addr
,
7508 struct timex
*host_tx
)
7510 struct target__kernel_timex
*target_tx
;
7512 if (copy_to_user_timeval64(target_addr
+
7513 offsetof(struct target__kernel_timex
, time
),
7515 return -TARGET_EFAULT
;
7518 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
7519 return -TARGET_EFAULT
;
7522 __put_user(host_tx
->modes
, &target_tx
->modes
);
7523 __put_user(host_tx
->offset
, &target_tx
->offset
);
7524 __put_user(host_tx
->freq
, &target_tx
->freq
);
7525 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7526 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
7527 __put_user(host_tx
->status
, &target_tx
->status
);
7528 __put_user(host_tx
->constant
, &target_tx
->constant
);
7529 __put_user(host_tx
->precision
, &target_tx
->precision
);
7530 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7531 __put_user(host_tx
->tick
, &target_tx
->tick
);
7532 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7533 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
7534 __put_user(host_tx
->shift
, &target_tx
->shift
);
7535 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
7536 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7537 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7538 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7539 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7540 __put_user(host_tx
->tai
, &target_tx
->tai
);
7542 unlock_user_struct(target_tx
, target_addr
, 1);
7547 #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
7548 #define sigev_notify_thread_id _sigev_un._tid
7551 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
7552 abi_ulong target_addr
)
7554 struct target_sigevent
*target_sevp
;
7556 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
7557 return -TARGET_EFAULT
;
7560 /* This union is awkward on 64 bit systems because it has a 32 bit
7561 * integer and a pointer in it; we follow the conversion approach
7562 * used for handling sigval types in signal.c so the guest should get
7563 * the correct value back even if we did a 64 bit byteswap and it's
7564 * using the 32 bit integer.
7566 host_sevp
->sigev_value
.sival_ptr
=
7567 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
7568 host_sevp
->sigev_signo
=
7569 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
7570 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
7571 host_sevp
->sigev_notify_thread_id
= tswap32(target_sevp
->_sigev_un
._tid
);
7573 unlock_user_struct(target_sevp
, target_addr
, 1);
7577 #if defined(TARGET_NR_mlockall)
7578 static inline int target_to_host_mlockall_arg(int arg
)
7582 if (arg
& TARGET_MCL_CURRENT
) {
7583 result
|= MCL_CURRENT
;
7585 if (arg
& TARGET_MCL_FUTURE
) {
7586 result
|= MCL_FUTURE
;
7589 if (arg
& TARGET_MCL_ONFAULT
) {
7590 result
|= MCL_ONFAULT
;
7598 #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
7599 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
7600 defined(TARGET_NR_newfstatat))
7601 static inline abi_long
host_to_target_stat64(CPUArchState
*cpu_env
,
7602 abi_ulong target_addr
,
7603 struct stat
*host_st
)
7605 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
7606 if (cpu_env
->eabi
) {
7607 struct target_eabi_stat64
*target_st
;
7609 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7610 return -TARGET_EFAULT
;
7611 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
7612 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7613 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7614 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7615 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7617 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7618 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7619 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7620 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7621 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7622 __put_user(host_st
->st_size
, &target_st
->st_size
);
7623 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7624 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7625 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7626 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7627 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7628 #ifdef HAVE_STRUCT_STAT_ST_ATIM
7629 __put_user(host_st
->st_atim
.tv_nsec
, &target_st
->target_st_atime_nsec
);
7630 __put_user(host_st
->st_mtim
.tv_nsec
, &target_st
->target_st_mtime_nsec
);
7631 __put_user(host_st
->st_ctim
.tv_nsec
, &target_st
->target_st_ctime_nsec
);
7633 unlock_user_struct(target_st
, target_addr
, 1);
7637 #if defined(TARGET_HAS_STRUCT_STAT64)
7638 struct target_stat64
*target_st
;
7640 struct target_stat
*target_st
;
7643 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7644 return -TARGET_EFAULT
;
7645 memset(target_st
, 0, sizeof(*target_st
));
7646 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7647 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7648 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7649 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7651 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7652 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7653 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7654 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7655 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7656 /* XXX: better use of kernel struct */
7657 __put_user(host_st
->st_size
, &target_st
->st_size
);
7658 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7659 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7660 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7661 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7662 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7663 #ifdef HAVE_STRUCT_STAT_ST_ATIM
7664 __put_user(host_st
->st_atim
.tv_nsec
, &target_st
->target_st_atime_nsec
);
7665 __put_user(host_st
->st_mtim
.tv_nsec
, &target_st
->target_st_mtime_nsec
);
7666 __put_user(host_st
->st_ctim
.tv_nsec
, &target_st
->target_st_ctime_nsec
);
7668 unlock_user_struct(target_st
, target_addr
, 1);
7675 #if defined(TARGET_NR_statx) && defined(__NR_statx)
7676 static inline abi_long
host_to_target_statx(struct target_statx
*host_stx
,
7677 abi_ulong target_addr
)
7679 struct target_statx
*target_stx
;
7681 if (!lock_user_struct(VERIFY_WRITE
, target_stx
, target_addr
, 0)) {
7682 return -TARGET_EFAULT
;
7684 memset(target_stx
, 0, sizeof(*target_stx
));
7686 __put_user(host_stx
->stx_mask
, &target_stx
->stx_mask
);
7687 __put_user(host_stx
->stx_blksize
, &target_stx
->stx_blksize
);
7688 __put_user(host_stx
->stx_attributes
, &target_stx
->stx_attributes
);
7689 __put_user(host_stx
->stx_nlink
, &target_stx
->stx_nlink
);
7690 __put_user(host_stx
->stx_uid
, &target_stx
->stx_uid
);
7691 __put_user(host_stx
->stx_gid
, &target_stx
->stx_gid
);
7692 __put_user(host_stx
->stx_mode
, &target_stx
->stx_mode
);
7693 __put_user(host_stx
->stx_ino
, &target_stx
->stx_ino
);
7694 __put_user(host_stx
->stx_size
, &target_stx
->stx_size
);
7695 __put_user(host_stx
->stx_blocks
, &target_stx
->stx_blocks
);
7696 __put_user(host_stx
->stx_attributes_mask
, &target_stx
->stx_attributes_mask
);
7697 __put_user(host_stx
->stx_atime
.tv_sec
, &target_stx
->stx_atime
.tv_sec
);
7698 __put_user(host_stx
->stx_atime
.tv_nsec
, &target_stx
->stx_atime
.tv_nsec
);
7699 __put_user(host_stx
->stx_btime
.tv_sec
, &target_stx
->stx_btime
.tv_sec
);
7700 __put_user(host_stx
->stx_btime
.tv_nsec
, &target_stx
->stx_btime
.tv_nsec
);
7701 __put_user(host_stx
->stx_ctime
.tv_sec
, &target_stx
->stx_ctime
.tv_sec
);
7702 __put_user(host_stx
->stx_ctime
.tv_nsec
, &target_stx
->stx_ctime
.tv_nsec
);
7703 __put_user(host_stx
->stx_mtime
.tv_sec
, &target_stx
->stx_mtime
.tv_sec
);
7704 __put_user(host_stx
->stx_mtime
.tv_nsec
, &target_stx
->stx_mtime
.tv_nsec
);
7705 __put_user(host_stx
->stx_rdev_major
, &target_stx
->stx_rdev_major
);
7706 __put_user(host_stx
->stx_rdev_minor
, &target_stx
->stx_rdev_minor
);
7707 __put_user(host_stx
->stx_dev_major
, &target_stx
->stx_dev_major
);
7708 __put_user(host_stx
->stx_dev_minor
, &target_stx
->stx_dev_minor
);
7710 unlock_user_struct(target_stx
, target_addr
, 1);
7716 static int do_sys_futex(int *uaddr
, int op
, int val
,
7717 const struct timespec
*timeout
, int *uaddr2
,
7720 #if HOST_LONG_BITS == 64
7721 #if defined(__NR_futex)
7722 /* always a 64-bit time_t, it doesn't define _time64 version */
7723 return sys_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7726 #else /* HOST_LONG_BITS == 64 */
7727 #if defined(__NR_futex_time64)
7728 if (sizeof(timeout
->tv_sec
) == 8) {
7729 /* _time64 function on 32bit arch */
7730 return sys_futex_time64(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7733 #if defined(__NR_futex)
7734 /* old function on 32bit arch */
7735 return sys_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7737 #endif /* HOST_LONG_BITS == 64 */
7738 g_assert_not_reached();
7741 static int do_safe_futex(int *uaddr
, int op
, int val
,
7742 const struct timespec
*timeout
, int *uaddr2
,
7745 #if HOST_LONG_BITS == 64
7746 #if defined(__NR_futex)
7747 /* always a 64-bit time_t, it doesn't define _time64 version */
7748 return get_errno(safe_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
));
7750 #else /* HOST_LONG_BITS == 64 */
7751 #if defined(__NR_futex_time64)
7752 if (sizeof(timeout
->tv_sec
) == 8) {
7753 /* _time64 function on 32bit arch */
7754 return get_errno(safe_futex_time64(uaddr
, op
, val
, timeout
, uaddr2
,
7758 #if defined(__NR_futex)
7759 /* old function on 32bit arch */
7760 return get_errno(safe_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
));
7762 #endif /* HOST_LONG_BITS == 64 */
7763 return -TARGET_ENOSYS
;
7766 /* ??? Using host futex calls even when target atomic operations
7767 are not really atomic probably breaks things. However implementing
7768 futexes locally would make futexes shared between multiple processes
7769 tricky. However they're probably useless because guest atomic
7770 operations won't work either. */
7771 #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
7772 static int do_futex(CPUState
*cpu
, bool time64
, target_ulong uaddr
,
7773 int op
, int val
, target_ulong timeout
,
7774 target_ulong uaddr2
, int val3
)
7776 struct timespec ts
, *pts
= NULL
;
7777 void *haddr2
= NULL
;
7780 /* We assume FUTEX_* constants are the same on both host and target. */
7781 #ifdef FUTEX_CMD_MASK
7782 base_op
= op
& FUTEX_CMD_MASK
;
7788 case FUTEX_WAIT_BITSET
:
7792 case FUTEX_WAKE_BITSET
:
7798 case FUTEX_CMP_REQUEUE
:
7799 val3
= tswap32(val3
);
7804 * For these, the 4th argument is not TIMEOUT, but VAL2.
7805 * But the prototype of do_safe_futex takes a pointer, so
7806 * insert casts to satisfy the compiler. We do not need
7807 * to tswap VAL2 since it's not compared to guest memory.
7809 pts
= (struct timespec
*)(uintptr_t)timeout
;
7811 haddr2
= g2h(cpu
, uaddr2
);
7814 return -TARGET_ENOSYS
;
7819 ? target_to_host_timespec64(pts
, timeout
)
7820 : target_to_host_timespec(pts
, timeout
)) {
7821 return -TARGET_EFAULT
;
7824 return do_safe_futex(g2h(cpu
, uaddr
), op
, val
, pts
, haddr2
, val3
);
7828 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7829 static abi_long
do_name_to_handle_at(abi_long dirfd
, abi_long pathname
,
7830 abi_long handle
, abi_long mount_id
,
7833 struct file_handle
*target_fh
;
7834 struct file_handle
*fh
;
7838 unsigned int size
, total_size
;
7840 if (get_user_s32(size
, handle
)) {
7841 return -TARGET_EFAULT
;
7844 name
= lock_user_string(pathname
);
7846 return -TARGET_EFAULT
;
7849 total_size
= sizeof(struct file_handle
) + size
;
7850 target_fh
= lock_user(VERIFY_WRITE
, handle
, total_size
, 0);
7852 unlock_user(name
, pathname
, 0);
7853 return -TARGET_EFAULT
;
7856 fh
= g_malloc0(total_size
);
7857 fh
->handle_bytes
= size
;
7859 ret
= get_errno(name_to_handle_at(dirfd
, path(name
), fh
, &mid
, flags
));
7860 unlock_user(name
, pathname
, 0);
7862 /* man name_to_handle_at(2):
7863 * Other than the use of the handle_bytes field, the caller should treat
7864 * the file_handle structure as an opaque data type
7867 memcpy(target_fh
, fh
, total_size
);
7868 target_fh
->handle_bytes
= tswap32(fh
->handle_bytes
);
7869 target_fh
->handle_type
= tswap32(fh
->handle_type
);
7871 unlock_user(target_fh
, handle
, total_size
);
7873 if (put_user_s32(mid
, mount_id
)) {
7874 return -TARGET_EFAULT
;
7882 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7883 static abi_long
do_open_by_handle_at(abi_long mount_fd
, abi_long handle
,
7886 struct file_handle
*target_fh
;
7887 struct file_handle
*fh
;
7888 unsigned int size
, total_size
;
7891 if (get_user_s32(size
, handle
)) {
7892 return -TARGET_EFAULT
;
7895 total_size
= sizeof(struct file_handle
) + size
;
7896 target_fh
= lock_user(VERIFY_READ
, handle
, total_size
, 1);
7898 return -TARGET_EFAULT
;
7901 fh
= g_memdup(target_fh
, total_size
);
7902 fh
->handle_bytes
= size
;
7903 fh
->handle_type
= tswap32(target_fh
->handle_type
);
7905 ret
= get_errno(open_by_handle_at(mount_fd
, fh
,
7906 target_to_host_bitmask(flags
, fcntl_flags_tbl
)));
7910 unlock_user(target_fh
, handle
, total_size
);
7916 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7918 static abi_long
do_signalfd4(int fd
, abi_long mask
, int flags
)
7921 target_sigset_t
*target_mask
;
7925 if (flags
& ~(TARGET_O_NONBLOCK_MASK
| TARGET_O_CLOEXEC
)) {
7926 return -TARGET_EINVAL
;
7928 if (!lock_user_struct(VERIFY_READ
, target_mask
, mask
, 1)) {
7929 return -TARGET_EFAULT
;
7932 target_to_host_sigset(&host_mask
, target_mask
);
7934 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
7936 ret
= get_errno(signalfd(fd
, &host_mask
, host_flags
));
7938 fd_trans_register(ret
, &target_signalfd_trans
);
7941 unlock_user_struct(target_mask
, mask
, 0);
7947 /* Map host to target signal numbers for the wait family of syscalls.
7948 Assume all other status bits are the same. */
7949 int host_to_target_waitstatus(int status
)
7951 if (WIFSIGNALED(status
)) {
7952 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
7954 if (WIFSTOPPED(status
)) {
7955 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
7961 static int open_self_cmdline(CPUArchState
*cpu_env
, int fd
)
7963 CPUState
*cpu
= env_cpu(cpu_env
);
7964 struct linux_binprm
*bprm
= ((TaskState
*)cpu
->opaque
)->bprm
;
7967 for (i
= 0; i
< bprm
->argc
; i
++) {
7968 size_t len
= strlen(bprm
->argv
[i
]) + 1;
7970 if (write(fd
, bprm
->argv
[i
], len
) != len
) {
7978 static int open_self_maps(CPUArchState
*cpu_env
, int fd
)
7980 CPUState
*cpu
= env_cpu(cpu_env
);
7981 TaskState
*ts
= cpu
->opaque
;
7982 GSList
*map_info
= read_self_maps();
7986 for (s
= map_info
; s
; s
= g_slist_next(s
)) {
7987 MapInfo
*e
= (MapInfo
*) s
->data
;
7989 if (h2g_valid(e
->start
)) {
7990 unsigned long min
= e
->start
;
7991 unsigned long max
= e
->end
;
7992 int flags
= page_get_flags(h2g(min
));
7995 max
= h2g_valid(max
- 1) ?
7996 max
: (uintptr_t) g2h_untagged(GUEST_ADDR_MAX
) + 1;
7998 if (page_check_range(h2g(min
), max
- min
, flags
) == -1) {
8003 if (h2g(max
) == ts
->info
->stack_limit
) {
8005 if (h2g(min
) == ts
->info
->stack_limit
) {
8012 count
= dprintf(fd
, TARGET_ABI_FMT_ptr
"-" TARGET_ABI_FMT_ptr
8013 " %c%c%c%c %08" PRIx64
" %s %"PRId64
,
8014 h2g(min
), h2g(max
- 1) + 1,
8015 (flags
& PAGE_READ
) ? 'r' : '-',
8016 (flags
& PAGE_WRITE_ORG
) ? 'w' : '-',
8017 (flags
& PAGE_EXEC
) ? 'x' : '-',
8018 e
->is_priv
? 'p' : 's',
8019 (uint64_t) e
->offset
, e
->dev
, e
->inode
);
8021 dprintf(fd
, "%*s%s\n", 73 - count
, "", path
);
8028 free_self_maps(map_info
);
8030 #ifdef TARGET_VSYSCALL_PAGE
8032 * We only support execution from the vsyscall page.
8033 * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
8035 count
= dprintf(fd
, TARGET_FMT_lx
"-" TARGET_FMT_lx
8036 " --xp 00000000 00:00 0",
8037 TARGET_VSYSCALL_PAGE
, TARGET_VSYSCALL_PAGE
+ TARGET_PAGE_SIZE
);
8038 dprintf(fd
, "%*s%s\n", 73 - count
, "", "[vsyscall]");
8044 static int open_self_stat(CPUArchState
*cpu_env
, int fd
)
8046 CPUState
*cpu
= env_cpu(cpu_env
);
8047 TaskState
*ts
= cpu
->opaque
;
8048 g_autoptr(GString
) buf
= g_string_new(NULL
);
8051 for (i
= 0; i
< 44; i
++) {
8054 g_string_printf(buf
, FMT_pid
" ", getpid());
8055 } else if (i
== 1) {
8057 gchar
*bin
= g_strrstr(ts
->bprm
->argv
[0], "/");
8058 bin
= bin
? bin
+ 1 : ts
->bprm
->argv
[0];
8059 g_string_printf(buf
, "(%.15s) ", bin
);
8060 } else if (i
== 3) {
8062 g_string_printf(buf
, FMT_pid
" ", getppid());
8063 } else if (i
== 21) {
8065 g_string_printf(buf
, "%" PRIu64
" ", ts
->start_boottime
);
8066 } else if (i
== 27) {
8068 g_string_printf(buf
, TARGET_ABI_FMT_ld
" ", ts
->info
->start_stack
);
8070 /* for the rest, there is MasterCard */
8071 g_string_printf(buf
, "0%c", i
== 43 ? '\n' : ' ');
8074 if (write(fd
, buf
->str
, buf
->len
) != buf
->len
) {
8082 static int open_self_auxv(CPUArchState
*cpu_env
, int fd
)
8084 CPUState
*cpu
= env_cpu(cpu_env
);
8085 TaskState
*ts
= cpu
->opaque
;
8086 abi_ulong auxv
= ts
->info
->saved_auxv
;
8087 abi_ulong len
= ts
->info
->auxv_len
;
8091 * Auxiliary vector is stored in target process stack.
8092 * read in whole auxv vector and copy it to file
8094 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
8098 r
= write(fd
, ptr
, len
);
8105 lseek(fd
, 0, SEEK_SET
);
8106 unlock_user(ptr
, auxv
, len
);
8112 static int is_proc_myself(const char *filename
, const char *entry
)
8114 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
8115 filename
+= strlen("/proc/");
8116 if (!strncmp(filename
, "self/", strlen("self/"))) {
8117 filename
+= strlen("self/");
8118 } else if (*filename
>= '1' && *filename
<= '9') {
8120 snprintf(myself
, sizeof(myself
), "%d/", getpid());
8121 if (!strncmp(filename
, myself
, strlen(myself
))) {
8122 filename
+= strlen(myself
);
8129 if (!strcmp(filename
, entry
)) {
8136 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
8137 defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
8138 static int is_proc(const char *filename
, const char *entry
)
8140 return strcmp(filename
, entry
) == 0;
8144 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
8145 static int open_net_route(CPUArchState
*cpu_env
, int fd
)
8152 fp
= fopen("/proc/net/route", "r");
8159 read
= getline(&line
, &len
, fp
);
8160 dprintf(fd
, "%s", line
);
8164 while ((read
= getline(&line
, &len
, fp
)) != -1) {
8166 uint32_t dest
, gw
, mask
;
8167 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
8170 fields
= sscanf(line
,
8171 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8172 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
8173 &mask
, &mtu
, &window
, &irtt
);
8177 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8178 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
8179 metric
, tswap32(mask
), mtu
, window
, irtt
);
8189 #if defined(TARGET_SPARC)
8190 static int open_cpuinfo(CPUArchState
*cpu_env
, int fd
)
8192 dprintf(fd
, "type\t\t: sun4u\n");
8197 #if defined(TARGET_HPPA)
8198 static int open_cpuinfo(CPUArchState
*cpu_env
, int fd
)
8200 dprintf(fd
, "cpu family\t: PA-RISC 1.1e\n");
8201 dprintf(fd
, "cpu\t\t: PA7300LC (PCX-L2)\n");
8202 dprintf(fd
, "capabilities\t: os32\n");
8203 dprintf(fd
, "model\t\t: 9000/778/B160L\n");
8204 dprintf(fd
, "model name\t: Merlin L2 160 QEMU (9000/778/B160L)\n");
8209 #if defined(TARGET_M68K)
8210 static int open_hardware(CPUArchState
*cpu_env
, int fd
)
8212 dprintf(fd
, "Model:\t\tqemu-m68k\n");
8217 static int do_openat(CPUArchState
*cpu_env
, int dirfd
, const char *pathname
, int flags
, mode_t mode
)
8220 const char *filename
;
8221 int (*fill
)(CPUArchState
*cpu_env
, int fd
);
8222 int (*cmp
)(const char *s1
, const char *s2
);
8224 const struct fake_open
*fake_open
;
8225 static const struct fake_open fakes
[] = {
8226 { "maps", open_self_maps
, is_proc_myself
},
8227 { "stat", open_self_stat
, is_proc_myself
},
8228 { "auxv", open_self_auxv
, is_proc_myself
},
8229 { "cmdline", open_self_cmdline
, is_proc_myself
},
8230 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
8231 { "/proc/net/route", open_net_route
, is_proc
},
8233 #if defined(TARGET_SPARC) || defined(TARGET_HPPA)
8234 { "/proc/cpuinfo", open_cpuinfo
, is_proc
},
8236 #if defined(TARGET_M68K)
8237 { "/proc/hardware", open_hardware
, is_proc
},
8239 { NULL
, NULL
, NULL
}
8242 if (is_proc_myself(pathname
, "exe")) {
8243 int execfd
= qemu_getauxval(AT_EXECFD
);
8244 return execfd
? execfd
: safe_openat(dirfd
, exec_path
, flags
, mode
);
8247 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
8248 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
8253 if (fake_open
->filename
) {
8255 char filename
[PATH_MAX
];
8258 fd
= memfd_create("qemu-open", 0);
8260 if (errno
!= ENOSYS
) {
8263 /* create temporary file to map stat to */
8264 tmpdir
= getenv("TMPDIR");
8267 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
8268 fd
= mkstemp(filename
);
8275 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
8281 lseek(fd
, 0, SEEK_SET
);
8286 return safe_openat(dirfd
, path(pathname
), flags
, mode
);
8289 #define TIMER_MAGIC 0x0caf0000
8290 #define TIMER_MAGIC_MASK 0xffff0000
8292 /* Convert QEMU provided timer ID back to internal 16bit index format */
8293 static target_timer_t
get_timer_id(abi_long arg
)
8295 target_timer_t timerid
= arg
;
8297 if ((timerid
& TIMER_MAGIC_MASK
) != TIMER_MAGIC
) {
8298 return -TARGET_EINVAL
;
8303 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
8304 return -TARGET_EINVAL
;
8310 static int target_to_host_cpu_mask(unsigned long *host_mask
,
8312 abi_ulong target_addr
,
8315 unsigned target_bits
= sizeof(abi_ulong
) * 8;
8316 unsigned host_bits
= sizeof(*host_mask
) * 8;
8317 abi_ulong
*target_mask
;
8320 assert(host_size
>= target_size
);
8322 target_mask
= lock_user(VERIFY_READ
, target_addr
, target_size
, 1);
8324 return -TARGET_EFAULT
;
8326 memset(host_mask
, 0, host_size
);
8328 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
8329 unsigned bit
= i
* target_bits
;
8332 __get_user(val
, &target_mask
[i
]);
8333 for (j
= 0; j
< target_bits
; j
++, bit
++) {
8334 if (val
& (1UL << j
)) {
8335 host_mask
[bit
/ host_bits
] |= 1UL << (bit
% host_bits
);
8340 unlock_user(target_mask
, target_addr
, 0);
8344 static int host_to_target_cpu_mask(const unsigned long *host_mask
,
8346 abi_ulong target_addr
,
8349 unsigned target_bits
= sizeof(abi_ulong
) * 8;
8350 unsigned host_bits
= sizeof(*host_mask
) * 8;
8351 abi_ulong
*target_mask
;
8354 assert(host_size
>= target_size
);
8356 target_mask
= lock_user(VERIFY_WRITE
, target_addr
, target_size
, 0);
8358 return -TARGET_EFAULT
;
8361 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
8362 unsigned bit
= i
* target_bits
;
8365 for (j
= 0; j
< target_bits
; j
++, bit
++) {
8366 if (host_mask
[bit
/ host_bits
] & (1UL << (bit
% host_bits
))) {
8370 __put_user(val
, &target_mask
[i
]);
8373 unlock_user(target_mask
, target_addr
, target_size
);
8377 #ifdef TARGET_NR_getdents
8378 static int do_getdents(abi_long dirfd
, abi_long arg2
, abi_long count
)
8380 g_autofree
void *hdirp
= NULL
;
8382 int hlen
, hoff
, toff
;
8383 int hreclen
, treclen
;
8384 off64_t prev_diroff
= 0;
8386 hdirp
= g_try_malloc(count
);
8388 return -TARGET_ENOMEM
;
8391 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8392 hlen
= sys_getdents(dirfd
, hdirp
, count
);
8394 hlen
= sys_getdents64(dirfd
, hdirp
, count
);
8397 hlen
= get_errno(hlen
);
8398 if (is_error(hlen
)) {
8402 tdirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
8404 return -TARGET_EFAULT
;
8407 for (hoff
= toff
= 0; hoff
< hlen
; hoff
+= hreclen
, toff
+= treclen
) {
8408 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8409 struct linux_dirent
*hde
= hdirp
+ hoff
;
8411 struct linux_dirent64
*hde
= hdirp
+ hoff
;
8413 struct target_dirent
*tde
= tdirp
+ toff
;
8417 namelen
= strlen(hde
->d_name
);
8418 hreclen
= hde
->d_reclen
;
8419 treclen
= offsetof(struct target_dirent
, d_name
) + namelen
+ 2;
8420 treclen
= QEMU_ALIGN_UP(treclen
, __alignof(struct target_dirent
));
8422 if (toff
+ treclen
> count
) {
8424 * If the host struct is smaller than the target struct, or
8425 * requires less alignment and thus packs into less space,
8426 * then the host can return more entries than we can pass
8430 toff
= -TARGET_EINVAL
; /* result buffer is too small */
8434 * Return what we have, resetting the file pointer to the
8435 * location of the first record not returned.
8437 lseek64(dirfd
, prev_diroff
, SEEK_SET
);
8441 prev_diroff
= hde
->d_off
;
8442 tde
->d_ino
= tswapal(hde
->d_ino
);
8443 tde
->d_off
= tswapal(hde
->d_off
);
8444 tde
->d_reclen
= tswap16(treclen
);
8445 memcpy(tde
->d_name
, hde
->d_name
, namelen
+ 1);
8448 * The getdents type is in what was formerly a padding byte at the
8449 * end of the structure.
8451 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8452 type
= *((uint8_t *)hde
+ hreclen
- 1);
8456 *((uint8_t *)tde
+ treclen
- 1) = type
;
8459 unlock_user(tdirp
, arg2
, toff
);
8462 #endif /* TARGET_NR_getdents */
8464 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
8465 static int do_getdents64(abi_long dirfd
, abi_long arg2
, abi_long count
)
8467 g_autofree
void *hdirp
= NULL
;
8469 int hlen
, hoff
, toff
;
8470 int hreclen
, treclen
;
8471 off64_t prev_diroff
= 0;
8473 hdirp
= g_try_malloc(count
);
8475 return -TARGET_ENOMEM
;
8478 hlen
= get_errno(sys_getdents64(dirfd
, hdirp
, count
));
8479 if (is_error(hlen
)) {
8483 tdirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
8485 return -TARGET_EFAULT
;
8488 for (hoff
= toff
= 0; hoff
< hlen
; hoff
+= hreclen
, toff
+= treclen
) {
8489 struct linux_dirent64
*hde
= hdirp
+ hoff
;
8490 struct target_dirent64
*tde
= tdirp
+ toff
;
8493 namelen
= strlen(hde
->d_name
) + 1;
8494 hreclen
= hde
->d_reclen
;
8495 treclen
= offsetof(struct target_dirent64
, d_name
) + namelen
;
8496 treclen
= QEMU_ALIGN_UP(treclen
, __alignof(struct target_dirent64
));
8498 if (toff
+ treclen
> count
) {
8500 * If the host struct is smaller than the target struct, or
8501 * requires less alignment and thus packs into less space,
8502 * then the host can return more entries than we can pass
8506 toff
= -TARGET_EINVAL
; /* result buffer is too small */
8510 * Return what we have, resetting the file pointer to the
8511 * location of the first record not returned.
8513 lseek64(dirfd
, prev_diroff
, SEEK_SET
);
8517 prev_diroff
= hde
->d_off
;
8518 tde
->d_ino
= tswap64(hde
->d_ino
);
8519 tde
->d_off
= tswap64(hde
->d_off
);
8520 tde
->d_reclen
= tswap16(treclen
);
8521 tde
->d_type
= hde
->d_type
;
8522 memcpy(tde
->d_name
, hde
->d_name
, namelen
);
8525 unlock_user(tdirp
, arg2
, toff
);
8528 #endif /* TARGET_NR_getdents64 */
8530 #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
8531 _syscall2(int, pivot_root
, const char *, new_root
, const char *, put_old
)
8534 /* This is an internal helper for do_syscall so that it is easier
8535 * to have a single return point, so that actions, such as logging
8536 * of syscall results, can be performed.
8537 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
8539 static abi_long
do_syscall1(CPUArchState
*cpu_env
, int num
, abi_long arg1
,
8540 abi_long arg2
, abi_long arg3
, abi_long arg4
,
8541 abi_long arg5
, abi_long arg6
, abi_long arg7
,
8544 CPUState
*cpu
= env_cpu(cpu_env
);
8546 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
8547 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
8548 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
8549 || defined(TARGET_NR_statx)
8552 #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
8553 || defined(TARGET_NR_fstatfs)
8559 case TARGET_NR_exit
:
8560 /* In old applications this may be used to implement _exit(2).
8561 However in threaded applications it is used for thread termination,
8562 and _exit_group is used for application termination.
8563 Do thread termination if we have more then one thread. */
8565 if (block_signals()) {
8566 return -QEMU_ERESTARTSYS
;
8569 pthread_mutex_lock(&clone_lock
);
8571 if (CPU_NEXT(first_cpu
)) {
8572 TaskState
*ts
= cpu
->opaque
;
8574 object_property_set_bool(OBJECT(cpu
), "realized", false, NULL
);
8575 object_unref(OBJECT(cpu
));
8577 * At this point the CPU should be unrealized and removed
8578 * from cpu lists. We can clean-up the rest of the thread
8579 * data without the lock held.
8582 pthread_mutex_unlock(&clone_lock
);
8584 if (ts
->child_tidptr
) {
8585 put_user_u32(0, ts
->child_tidptr
);
8586 do_sys_futex(g2h(cpu
, ts
->child_tidptr
),
8587 FUTEX_WAKE
, INT_MAX
, NULL
, NULL
, 0);
8591 rcu_unregister_thread();
8595 pthread_mutex_unlock(&clone_lock
);
8596 preexit_cleanup(cpu_env
, arg1
);
8598 return 0; /* avoid warning */
8599 case TARGET_NR_read
:
8600 if (arg2
== 0 && arg3
== 0) {
8601 return get_errno(safe_read(arg1
, 0, 0));
8603 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
8604 return -TARGET_EFAULT
;
8605 ret
= get_errno(safe_read(arg1
, p
, arg3
));
8607 fd_trans_host_to_target_data(arg1
)) {
8608 ret
= fd_trans_host_to_target_data(arg1
)(p
, ret
);
8610 unlock_user(p
, arg2
, ret
);
8613 case TARGET_NR_write
:
8614 if (arg2
== 0 && arg3
== 0) {
8615 return get_errno(safe_write(arg1
, 0, 0));
8617 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
8618 return -TARGET_EFAULT
;
8619 if (fd_trans_target_to_host_data(arg1
)) {
8620 void *copy
= g_malloc(arg3
);
8621 memcpy(copy
, p
, arg3
);
8622 ret
= fd_trans_target_to_host_data(arg1
)(copy
, arg3
);
8624 ret
= get_errno(safe_write(arg1
, copy
, ret
));
8628 ret
= get_errno(safe_write(arg1
, p
, arg3
));
8630 unlock_user(p
, arg2
, 0);
8633 #ifdef TARGET_NR_open
8634 case TARGET_NR_open
:
8635 if (!(p
= lock_user_string(arg1
)))
8636 return -TARGET_EFAULT
;
8637 ret
= get_errno(do_openat(cpu_env
, AT_FDCWD
, p
,
8638 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
8640 fd_trans_unregister(ret
);
8641 unlock_user(p
, arg1
, 0);
8644 case TARGET_NR_openat
:
8645 if (!(p
= lock_user_string(arg2
)))
8646 return -TARGET_EFAULT
;
8647 ret
= get_errno(do_openat(cpu_env
, arg1
, p
,
8648 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
8650 fd_trans_unregister(ret
);
8651 unlock_user(p
, arg2
, 0);
8653 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
8654 case TARGET_NR_name_to_handle_at
:
8655 ret
= do_name_to_handle_at(arg1
, arg2
, arg3
, arg4
, arg5
);
8658 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
8659 case TARGET_NR_open_by_handle_at
:
8660 ret
= do_open_by_handle_at(arg1
, arg2
, arg3
);
8661 fd_trans_unregister(ret
);
8664 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
8665 case TARGET_NR_pidfd_open
:
8666 return get_errno(pidfd_open(arg1
, arg2
));
8668 #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
8669 case TARGET_NR_pidfd_send_signal
:
8673 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
8675 return -TARGET_EFAULT
;
8677 target_to_host_siginfo(&uinfo
, p
);
8678 unlock_user(p
, arg3
, 0);
8679 ret
= get_errno(pidfd_send_signal(arg1
, target_to_host_signal(arg2
),
8684 #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
8685 case TARGET_NR_pidfd_getfd
:
8686 return get_errno(pidfd_getfd(arg1
, arg2
, arg3
));
8688 case TARGET_NR_close
:
8689 fd_trans_unregister(arg1
);
8690 return get_errno(close(arg1
));
8693 return do_brk(arg1
);
8694 #ifdef TARGET_NR_fork
8695 case TARGET_NR_fork
:
8696 return get_errno(do_fork(cpu_env
, TARGET_SIGCHLD
, 0, 0, 0, 0));
8698 #ifdef TARGET_NR_waitpid
8699 case TARGET_NR_waitpid
:
8702 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, 0));
8703 if (!is_error(ret
) && arg2
&& ret
8704 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
8705 return -TARGET_EFAULT
;
8709 #ifdef TARGET_NR_waitid
8710 case TARGET_NR_waitid
:
8714 ret
= get_errno(safe_waitid(arg1
, arg2
, &info
, arg4
, NULL
));
8715 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
8716 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
8717 return -TARGET_EFAULT
;
8718 host_to_target_siginfo(p
, &info
);
8719 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
8724 #ifdef TARGET_NR_creat /* not on alpha */
8725 case TARGET_NR_creat
:
8726 if (!(p
= lock_user_string(arg1
)))
8727 return -TARGET_EFAULT
;
8728 ret
= get_errno(creat(p
, arg2
));
8729 fd_trans_unregister(ret
);
8730 unlock_user(p
, arg1
, 0);
8733 #ifdef TARGET_NR_link
8734 case TARGET_NR_link
:
8737 p
= lock_user_string(arg1
);
8738 p2
= lock_user_string(arg2
);
8740 ret
= -TARGET_EFAULT
;
8742 ret
= get_errno(link(p
, p2
));
8743 unlock_user(p2
, arg2
, 0);
8744 unlock_user(p
, arg1
, 0);
8748 #if defined(TARGET_NR_linkat)
8749 case TARGET_NR_linkat
:
8753 return -TARGET_EFAULT
;
8754 p
= lock_user_string(arg2
);
8755 p2
= lock_user_string(arg4
);
8757 ret
= -TARGET_EFAULT
;
8759 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
8760 unlock_user(p
, arg2
, 0);
8761 unlock_user(p2
, arg4
, 0);
8765 #ifdef TARGET_NR_unlink
8766 case TARGET_NR_unlink
:
8767 if (!(p
= lock_user_string(arg1
)))
8768 return -TARGET_EFAULT
;
8769 ret
= get_errno(unlink(p
));
8770 unlock_user(p
, arg1
, 0);
8773 #if defined(TARGET_NR_unlinkat)
8774 case TARGET_NR_unlinkat
:
8775 if (!(p
= lock_user_string(arg2
)))
8776 return -TARGET_EFAULT
;
8777 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
8778 unlock_user(p
, arg2
, 0);
8781 case TARGET_NR_execve
:
8783 char **argp
, **envp
;
8786 abi_ulong guest_argp
;
8787 abi_ulong guest_envp
;
8793 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
8794 if (get_user_ual(addr
, gp
))
8795 return -TARGET_EFAULT
;
8802 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
8803 if (get_user_ual(addr
, gp
))
8804 return -TARGET_EFAULT
;
8810 argp
= g_new0(char *, argc
+ 1);
8811 envp
= g_new0(char *, envc
+ 1);
8813 for (gp
= guest_argp
, q
= argp
; gp
;
8814 gp
+= sizeof(abi_ulong
), q
++) {
8815 if (get_user_ual(addr
, gp
))
8819 if (!(*q
= lock_user_string(addr
)))
8824 for (gp
= guest_envp
, q
= envp
; gp
;
8825 gp
+= sizeof(abi_ulong
), q
++) {
8826 if (get_user_ual(addr
, gp
))
8830 if (!(*q
= lock_user_string(addr
)))
8835 if (!(p
= lock_user_string(arg1
)))
8837 /* Although execve() is not an interruptible syscall it is
8838 * a special case where we must use the safe_syscall wrapper:
8839 * if we allow a signal to happen before we make the host
8840 * syscall then we will 'lose' it, because at the point of
8841 * execve the process leaves QEMU's control. So we use the
8842 * safe syscall wrapper to ensure that we either take the
8843 * signal as a guest signal, or else it does not happen
8844 * before the execve completes and makes it the other
8845 * program's problem.
8847 ret
= get_errno(safe_execve(p
, argp
, envp
));
8848 unlock_user(p
, arg1
, 0);
8853 ret
= -TARGET_EFAULT
;
8856 for (gp
= guest_argp
, q
= argp
; *q
;
8857 gp
+= sizeof(abi_ulong
), q
++) {
8858 if (get_user_ual(addr
, gp
)
8861 unlock_user(*q
, addr
, 0);
8863 for (gp
= guest_envp
, q
= envp
; *q
;
8864 gp
+= sizeof(abi_ulong
), q
++) {
8865 if (get_user_ual(addr
, gp
)
8868 unlock_user(*q
, addr
, 0);
8875 case TARGET_NR_chdir
:
8876 if (!(p
= lock_user_string(arg1
)))
8877 return -TARGET_EFAULT
;
8878 ret
= get_errno(chdir(p
));
8879 unlock_user(p
, arg1
, 0);
8881 #ifdef TARGET_NR_time
8882 case TARGET_NR_time
:
8885 ret
= get_errno(time(&host_time
));
8888 && put_user_sal(host_time
, arg1
))
8889 return -TARGET_EFAULT
;
8893 #ifdef TARGET_NR_mknod
8894 case TARGET_NR_mknod
:
8895 if (!(p
= lock_user_string(arg1
)))
8896 return -TARGET_EFAULT
;
8897 ret
= get_errno(mknod(p
, arg2
, arg3
));
8898 unlock_user(p
, arg1
, 0);
8901 #if defined(TARGET_NR_mknodat)
8902 case TARGET_NR_mknodat
:
8903 if (!(p
= lock_user_string(arg2
)))
8904 return -TARGET_EFAULT
;
8905 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
8906 unlock_user(p
, arg2
, 0);
8909 #ifdef TARGET_NR_chmod
8910 case TARGET_NR_chmod
:
8911 if (!(p
= lock_user_string(arg1
)))
8912 return -TARGET_EFAULT
;
8913 ret
= get_errno(chmod(p
, arg2
));
8914 unlock_user(p
, arg1
, 0);
8917 #ifdef TARGET_NR_lseek
8918 case TARGET_NR_lseek
:
8919 return get_errno(lseek(arg1
, arg2
, arg3
));
8921 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
8922 /* Alpha specific */
8923 case TARGET_NR_getxpid
:
8924 cpu_env
->ir
[IR_A4
] = getppid();
8925 return get_errno(getpid());
8927 #ifdef TARGET_NR_getpid
8928 case TARGET_NR_getpid
:
8929 return get_errno(getpid());
8931 case TARGET_NR_mount
:
8933 /* need to look at the data field */
8937 p
= lock_user_string(arg1
);
8939 return -TARGET_EFAULT
;
8945 p2
= lock_user_string(arg2
);
8948 unlock_user(p
, arg1
, 0);
8950 return -TARGET_EFAULT
;
8954 p3
= lock_user_string(arg3
);
8957 unlock_user(p
, arg1
, 0);
8959 unlock_user(p2
, arg2
, 0);
8960 return -TARGET_EFAULT
;
8966 /* FIXME - arg5 should be locked, but it isn't clear how to
8967 * do that since it's not guaranteed to be a NULL-terminated
8971 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
8973 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(cpu
, arg5
));
8975 ret
= get_errno(ret
);
8978 unlock_user(p
, arg1
, 0);
8980 unlock_user(p2
, arg2
, 0);
8982 unlock_user(p3
, arg3
, 0);
8986 #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
8987 #if defined(TARGET_NR_umount)
8988 case TARGET_NR_umount
:
8990 #if defined(TARGET_NR_oldumount)
8991 case TARGET_NR_oldumount
:
8993 if (!(p
= lock_user_string(arg1
)))
8994 return -TARGET_EFAULT
;
8995 ret
= get_errno(umount(p
));
8996 unlock_user(p
, arg1
, 0);
8999 #ifdef TARGET_NR_stime /* not on alpha */
9000 case TARGET_NR_stime
:
9004 if (get_user_sal(ts
.tv_sec
, arg1
)) {
9005 return -TARGET_EFAULT
;
9007 return get_errno(clock_settime(CLOCK_REALTIME
, &ts
));
9010 #ifdef TARGET_NR_alarm /* not on alpha */
9011 case TARGET_NR_alarm
:
9014 #ifdef TARGET_NR_pause /* not on alpha */
9015 case TARGET_NR_pause
:
9016 if (!block_signals()) {
9017 sigsuspend(&((TaskState
*)cpu
->opaque
)->signal_mask
);
9019 return -TARGET_EINTR
;
9021 #ifdef TARGET_NR_utime
9022 case TARGET_NR_utime
:
9024 struct utimbuf tbuf
, *host_tbuf
;
9025 struct target_utimbuf
*target_tbuf
;
9027 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
9028 return -TARGET_EFAULT
;
9029 tbuf
.actime
= tswapal(target_tbuf
->actime
);
9030 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
9031 unlock_user_struct(target_tbuf
, arg2
, 0);
9036 if (!(p
= lock_user_string(arg1
)))
9037 return -TARGET_EFAULT
;
9038 ret
= get_errno(utime(p
, host_tbuf
));
9039 unlock_user(p
, arg1
, 0);
9043 #ifdef TARGET_NR_utimes
9044 case TARGET_NR_utimes
:
9046 struct timeval
*tvp
, tv
[2];
9048 if (copy_from_user_timeval(&tv
[0], arg2
)
9049 || copy_from_user_timeval(&tv
[1],
9050 arg2
+ sizeof(struct target_timeval
)))
9051 return -TARGET_EFAULT
;
9056 if (!(p
= lock_user_string(arg1
)))
9057 return -TARGET_EFAULT
;
9058 ret
= get_errno(utimes(p
, tvp
));
9059 unlock_user(p
, arg1
, 0);
9063 #if defined(TARGET_NR_futimesat)
9064 case TARGET_NR_futimesat
:
9066 struct timeval
*tvp
, tv
[2];
9068 if (copy_from_user_timeval(&tv
[0], arg3
)
9069 || copy_from_user_timeval(&tv
[1],
9070 arg3
+ sizeof(struct target_timeval
)))
9071 return -TARGET_EFAULT
;
9076 if (!(p
= lock_user_string(arg2
))) {
9077 return -TARGET_EFAULT
;
9079 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
9080 unlock_user(p
, arg2
, 0);
9084 #ifdef TARGET_NR_access
9085 case TARGET_NR_access
:
9086 if (!(p
= lock_user_string(arg1
))) {
9087 return -TARGET_EFAULT
;
9089 ret
= get_errno(access(path(p
), arg2
));
9090 unlock_user(p
, arg1
, 0);
9093 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
9094 case TARGET_NR_faccessat
:
9095 if (!(p
= lock_user_string(arg2
))) {
9096 return -TARGET_EFAULT
;
9098 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
9099 unlock_user(p
, arg2
, 0);
9102 #ifdef TARGET_NR_nice /* not on alpha */
9103 case TARGET_NR_nice
:
9104 return get_errno(nice(arg1
));
9106 case TARGET_NR_sync
:
9109 #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
9110 case TARGET_NR_syncfs
:
9111 return get_errno(syncfs(arg1
));
9113 case TARGET_NR_kill
:
9114 return get_errno(safe_kill(arg1
, target_to_host_signal(arg2
)));
9115 #ifdef TARGET_NR_rename
9116 case TARGET_NR_rename
:
9119 p
= lock_user_string(arg1
);
9120 p2
= lock_user_string(arg2
);
9122 ret
= -TARGET_EFAULT
;
9124 ret
= get_errno(rename(p
, p2
));
9125 unlock_user(p2
, arg2
, 0);
9126 unlock_user(p
, arg1
, 0);
9130 #if defined(TARGET_NR_renameat)
9131 case TARGET_NR_renameat
:
9134 p
= lock_user_string(arg2
);
9135 p2
= lock_user_string(arg4
);
9137 ret
= -TARGET_EFAULT
;
9139 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
9140 unlock_user(p2
, arg4
, 0);
9141 unlock_user(p
, arg2
, 0);
9145 #if defined(TARGET_NR_renameat2)
9146 case TARGET_NR_renameat2
:
9149 p
= lock_user_string(arg2
);
9150 p2
= lock_user_string(arg4
);
9152 ret
= -TARGET_EFAULT
;
9154 ret
= get_errno(sys_renameat2(arg1
, p
, arg3
, p2
, arg5
));
9156 unlock_user(p2
, arg4
, 0);
9157 unlock_user(p
, arg2
, 0);
9161 #ifdef TARGET_NR_mkdir
9162 case TARGET_NR_mkdir
:
9163 if (!(p
= lock_user_string(arg1
)))
9164 return -TARGET_EFAULT
;
9165 ret
= get_errno(mkdir(p
, arg2
));
9166 unlock_user(p
, arg1
, 0);
9169 #if defined(TARGET_NR_mkdirat)
9170 case TARGET_NR_mkdirat
:
9171 if (!(p
= lock_user_string(arg2
)))
9172 return -TARGET_EFAULT
;
9173 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
9174 unlock_user(p
, arg2
, 0);
9177 #ifdef TARGET_NR_rmdir
9178 case TARGET_NR_rmdir
:
9179 if (!(p
= lock_user_string(arg1
)))
9180 return -TARGET_EFAULT
;
9181 ret
= get_errno(rmdir(p
));
9182 unlock_user(p
, arg1
, 0);
9186 ret
= get_errno(dup(arg1
));
9188 fd_trans_dup(arg1
, ret
);
9191 #ifdef TARGET_NR_pipe
9192 case TARGET_NR_pipe
:
9193 return do_pipe(cpu_env
, arg1
, 0, 0);
9195 #ifdef TARGET_NR_pipe2
9196 case TARGET_NR_pipe2
:
9197 return do_pipe(cpu_env
, arg1
,
9198 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
9200 case TARGET_NR_times
:
9202 struct target_tms
*tmsp
;
9204 ret
= get_errno(times(&tms
));
9206 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
9208 return -TARGET_EFAULT
;
9209 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
9210 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
9211 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
9212 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
9215 ret
= host_to_target_clock_t(ret
);
9218 case TARGET_NR_acct
:
9220 ret
= get_errno(acct(NULL
));
9222 if (!(p
= lock_user_string(arg1
))) {
9223 return -TARGET_EFAULT
;
9225 ret
= get_errno(acct(path(p
)));
9226 unlock_user(p
, arg1
, 0);
9229 #ifdef TARGET_NR_umount2
9230 case TARGET_NR_umount2
:
9231 if (!(p
= lock_user_string(arg1
)))
9232 return -TARGET_EFAULT
;
9233 ret
= get_errno(umount2(p
, arg2
));
9234 unlock_user(p
, arg1
, 0);
9237 case TARGET_NR_ioctl
:
9238 return do_ioctl(arg1
, arg2
, arg3
);
9239 #ifdef TARGET_NR_fcntl
9240 case TARGET_NR_fcntl
:
9241 return do_fcntl(arg1
, arg2
, arg3
);
9243 case TARGET_NR_setpgid
:
9244 return get_errno(setpgid(arg1
, arg2
));
9245 case TARGET_NR_umask
:
9246 return get_errno(umask(arg1
));
9247 case TARGET_NR_chroot
:
9248 if (!(p
= lock_user_string(arg1
)))
9249 return -TARGET_EFAULT
;
9250 ret
= get_errno(chroot(p
));
9251 unlock_user(p
, arg1
, 0);
9253 #ifdef TARGET_NR_dup2
9254 case TARGET_NR_dup2
:
9255 ret
= get_errno(dup2(arg1
, arg2
));
9257 fd_trans_dup(arg1
, arg2
);
9261 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
9262 case TARGET_NR_dup3
:
9266 if ((arg3
& ~TARGET_O_CLOEXEC
) != 0) {
9269 host_flags
= target_to_host_bitmask(arg3
, fcntl_flags_tbl
);
9270 ret
= get_errno(dup3(arg1
, arg2
, host_flags
));
9272 fd_trans_dup(arg1
, arg2
);
9277 #ifdef TARGET_NR_getppid /* not on alpha */
9278 case TARGET_NR_getppid
:
9279 return get_errno(getppid());
9281 #ifdef TARGET_NR_getpgrp
9282 case TARGET_NR_getpgrp
:
9283 return get_errno(getpgrp());
9285 case TARGET_NR_setsid
:
9286 return get_errno(setsid());
9287 #ifdef TARGET_NR_sigaction
9288 case TARGET_NR_sigaction
:
9290 #if defined(TARGET_MIPS)
9291 struct target_sigaction act
, oact
, *pact
, *old_act
;
9294 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
9295 return -TARGET_EFAULT
;
9296 act
._sa_handler
= old_act
->_sa_handler
;
9297 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
9298 act
.sa_flags
= old_act
->sa_flags
;
9299 unlock_user_struct(old_act
, arg2
, 0);
9305 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
, 0));
9307 if (!is_error(ret
) && arg3
) {
9308 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
9309 return -TARGET_EFAULT
;
9310 old_act
->_sa_handler
= oact
._sa_handler
;
9311 old_act
->sa_flags
= oact
.sa_flags
;
9312 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
9313 old_act
->sa_mask
.sig
[1] = 0;
9314 old_act
->sa_mask
.sig
[2] = 0;
9315 old_act
->sa_mask
.sig
[3] = 0;
9316 unlock_user_struct(old_act
, arg3
, 1);
9319 struct target_old_sigaction
*old_act
;
9320 struct target_sigaction act
, oact
, *pact
;
9322 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
9323 return -TARGET_EFAULT
;
9324 act
._sa_handler
= old_act
->_sa_handler
;
9325 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
9326 act
.sa_flags
= old_act
->sa_flags
;
9327 #ifdef TARGET_ARCH_HAS_SA_RESTORER
9328 act
.sa_restorer
= old_act
->sa_restorer
;
9330 unlock_user_struct(old_act
, arg2
, 0);
9335 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
, 0));
9336 if (!is_error(ret
) && arg3
) {
9337 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
9338 return -TARGET_EFAULT
;
9339 old_act
->_sa_handler
= oact
._sa_handler
;
9340 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
9341 old_act
->sa_flags
= oact
.sa_flags
;
9342 #ifdef TARGET_ARCH_HAS_SA_RESTORER
9343 old_act
->sa_restorer
= oact
.sa_restorer
;
9345 unlock_user_struct(old_act
, arg3
, 1);
9351 case TARGET_NR_rt_sigaction
:
9354 * For Alpha and SPARC this is a 5 argument syscall, with
9355 * a 'restorer' parameter which must be copied into the
9356 * sa_restorer field of the sigaction struct.
9357 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
9358 * and arg5 is the sigsetsize.
9360 #if defined(TARGET_ALPHA)
9361 target_ulong sigsetsize
= arg4
;
9362 target_ulong restorer
= arg5
;
9363 #elif defined(TARGET_SPARC)
9364 target_ulong restorer
= arg4
;
9365 target_ulong sigsetsize
= arg5
;
9367 target_ulong sigsetsize
= arg4
;
9368 target_ulong restorer
= 0;
9370 struct target_sigaction
*act
= NULL
;
9371 struct target_sigaction
*oact
= NULL
;
9373 if (sigsetsize
!= sizeof(target_sigset_t
)) {
9374 return -TARGET_EINVAL
;
9376 if (arg2
&& !lock_user_struct(VERIFY_READ
, act
, arg2
, 1)) {
9377 return -TARGET_EFAULT
;
9379 if (arg3
&& !lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
9380 ret
= -TARGET_EFAULT
;
9382 ret
= get_errno(do_sigaction(arg1
, act
, oact
, restorer
));
9384 unlock_user_struct(oact
, arg3
, 1);
9388 unlock_user_struct(act
, arg2
, 0);
9392 #ifdef TARGET_NR_sgetmask /* not on alpha */
9393 case TARGET_NR_sgetmask
:
9396 abi_ulong target_set
;
9397 ret
= do_sigprocmask(0, NULL
, &cur_set
);
9399 host_to_target_old_sigset(&target_set
, &cur_set
);
9405 #ifdef TARGET_NR_ssetmask /* not on alpha */
9406 case TARGET_NR_ssetmask
:
9409 abi_ulong target_set
= arg1
;
9410 target_to_host_old_sigset(&set
, &target_set
);
9411 ret
= do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
9413 host_to_target_old_sigset(&target_set
, &oset
);
9419 #ifdef TARGET_NR_sigprocmask
9420 case TARGET_NR_sigprocmask
:
9422 #if defined(TARGET_ALPHA)
9423 sigset_t set
, oldset
;
9428 case TARGET_SIG_BLOCK
:
9431 case TARGET_SIG_UNBLOCK
:
9434 case TARGET_SIG_SETMASK
:
9438 return -TARGET_EINVAL
;
9441 target_to_host_old_sigset(&set
, &mask
);
9443 ret
= do_sigprocmask(how
, &set
, &oldset
);
9444 if (!is_error(ret
)) {
9445 host_to_target_old_sigset(&mask
, &oldset
);
9447 cpu_env
->ir
[IR_V0
] = 0; /* force no error */
9450 sigset_t set
, oldset
, *set_ptr
;
9454 p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1);
9456 return -TARGET_EFAULT
;
9458 target_to_host_old_sigset(&set
, p
);
9459 unlock_user(p
, arg2
, 0);
9462 case TARGET_SIG_BLOCK
:
9465 case TARGET_SIG_UNBLOCK
:
9468 case TARGET_SIG_SETMASK
:
9472 return -TARGET_EINVAL
;
9478 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
9479 if (!is_error(ret
) && arg3
) {
9480 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
9481 return -TARGET_EFAULT
;
9482 host_to_target_old_sigset(p
, &oldset
);
9483 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
9489 case TARGET_NR_rt_sigprocmask
:
9492 sigset_t set
, oldset
, *set_ptr
;
9494 if (arg4
!= sizeof(target_sigset_t
)) {
9495 return -TARGET_EINVAL
;
9499 p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1);
9501 return -TARGET_EFAULT
;
9503 target_to_host_sigset(&set
, p
);
9504 unlock_user(p
, arg2
, 0);
9507 case TARGET_SIG_BLOCK
:
9510 case TARGET_SIG_UNBLOCK
:
9513 case TARGET_SIG_SETMASK
:
9517 return -TARGET_EINVAL
;
9523 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
9524 if (!is_error(ret
) && arg3
) {
9525 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
9526 return -TARGET_EFAULT
;
9527 host_to_target_sigset(p
, &oldset
);
9528 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
9532 #ifdef TARGET_NR_sigpending
9533 case TARGET_NR_sigpending
:
9536 ret
= get_errno(sigpending(&set
));
9537 if (!is_error(ret
)) {
9538 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
9539 return -TARGET_EFAULT
;
9540 host_to_target_old_sigset(p
, &set
);
9541 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
9546 case TARGET_NR_rt_sigpending
:
9550 /* Yes, this check is >, not != like most. We follow the kernel's
9551 * logic and it does it like this because it implements
9552 * NR_sigpending through the same code path, and in that case
9553 * the old_sigset_t is smaller in size.
9555 if (arg2
> sizeof(target_sigset_t
)) {
9556 return -TARGET_EINVAL
;
9559 ret
= get_errno(sigpending(&set
));
9560 if (!is_error(ret
)) {
9561 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
9562 return -TARGET_EFAULT
;
9563 host_to_target_sigset(p
, &set
);
9564 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
9568 #ifdef TARGET_NR_sigsuspend
9569 case TARGET_NR_sigsuspend
:
9573 #if defined(TARGET_ALPHA)
9574 TaskState
*ts
= cpu
->opaque
;
9575 /* target_to_host_old_sigset will bswap back */
9576 abi_ulong mask
= tswapal(arg1
);
9577 set
= &ts
->sigsuspend_mask
;
9578 target_to_host_old_sigset(set
, &mask
);
9580 ret
= process_sigsuspend_mask(&set
, arg1
, sizeof(target_sigset_t
));
9585 ret
= get_errno(safe_rt_sigsuspend(set
, SIGSET_T_SIZE
));
9586 finish_sigsuspend_mask(ret
);
9590 case TARGET_NR_rt_sigsuspend
:
9594 ret
= process_sigsuspend_mask(&set
, arg1
, arg2
);
9598 ret
= get_errno(safe_rt_sigsuspend(set
, SIGSET_T_SIZE
));
9599 finish_sigsuspend_mask(ret
);
9602 #ifdef TARGET_NR_rt_sigtimedwait
9603 case TARGET_NR_rt_sigtimedwait
:
9606 struct timespec uts
, *puts
;
9609 if (arg4
!= sizeof(target_sigset_t
)) {
9610 return -TARGET_EINVAL
;
9613 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
9614 return -TARGET_EFAULT
;
9615 target_to_host_sigset(&set
, p
);
9616 unlock_user(p
, arg1
, 0);
9619 if (target_to_host_timespec(puts
, arg3
)) {
9620 return -TARGET_EFAULT
;
9625 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
9627 if (!is_error(ret
)) {
9629 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
9632 return -TARGET_EFAULT
;
9634 host_to_target_siginfo(p
, &uinfo
);
9635 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
9637 ret
= host_to_target_signal(ret
);
9642 #ifdef TARGET_NR_rt_sigtimedwait_time64
9643 case TARGET_NR_rt_sigtimedwait_time64
:
9646 struct timespec uts
, *puts
;
9649 if (arg4
!= sizeof(target_sigset_t
)) {
9650 return -TARGET_EINVAL
;
9653 p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1);
9655 return -TARGET_EFAULT
;
9657 target_to_host_sigset(&set
, p
);
9658 unlock_user(p
, arg1
, 0);
9661 if (target_to_host_timespec64(puts
, arg3
)) {
9662 return -TARGET_EFAULT
;
9667 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
9669 if (!is_error(ret
)) {
9671 p
= lock_user(VERIFY_WRITE
, arg2
,
9672 sizeof(target_siginfo_t
), 0);
9674 return -TARGET_EFAULT
;
9676 host_to_target_siginfo(p
, &uinfo
);
9677 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
9679 ret
= host_to_target_signal(ret
);
9684 case TARGET_NR_rt_sigqueueinfo
:
9688 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
9690 return -TARGET_EFAULT
;
9692 target_to_host_siginfo(&uinfo
, p
);
9693 unlock_user(p
, arg3
, 0);
9694 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, target_to_host_signal(arg2
), &uinfo
));
9697 case TARGET_NR_rt_tgsigqueueinfo
:
9701 p
= lock_user(VERIFY_READ
, arg4
, sizeof(target_siginfo_t
), 1);
9703 return -TARGET_EFAULT
;
9705 target_to_host_siginfo(&uinfo
, p
);
9706 unlock_user(p
, arg4
, 0);
9707 ret
= get_errno(sys_rt_tgsigqueueinfo(arg1
, arg2
, target_to_host_signal(arg3
), &uinfo
));
9710 #ifdef TARGET_NR_sigreturn
9711 case TARGET_NR_sigreturn
:
9712 if (block_signals()) {
9713 return -QEMU_ERESTARTSYS
;
9715 return do_sigreturn(cpu_env
);
9717 case TARGET_NR_rt_sigreturn
:
9718 if (block_signals()) {
9719 return -QEMU_ERESTARTSYS
;
9721 return do_rt_sigreturn(cpu_env
);
9722 case TARGET_NR_sethostname
:
9723 if (!(p
= lock_user_string(arg1
)))
9724 return -TARGET_EFAULT
;
9725 ret
= get_errno(sethostname(p
, arg2
));
9726 unlock_user(p
, arg1
, 0);
9728 #ifdef TARGET_NR_setrlimit
9729 case TARGET_NR_setrlimit
:
9731 int resource
= target_to_host_resource(arg1
);
9732 struct target_rlimit
*target_rlim
;
9734 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
9735 return -TARGET_EFAULT
;
9736 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
9737 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
9738 unlock_user_struct(target_rlim
, arg2
, 0);
9740 * If we just passed through resource limit settings for memory then
9741 * they would also apply to QEMU's own allocations, and QEMU will
9742 * crash or hang or die if its allocations fail. Ideally we would
9743 * track the guest allocations in QEMU and apply the limits ourselves.
9744 * For now, just tell the guest the call succeeded but don't actually
9747 if (resource
!= RLIMIT_AS
&&
9748 resource
!= RLIMIT_DATA
&&
9749 resource
!= RLIMIT_STACK
) {
9750 return get_errno(setrlimit(resource
, &rlim
));
9756 #ifdef TARGET_NR_getrlimit
9757 case TARGET_NR_getrlimit
:
9759 int resource
= target_to_host_resource(arg1
);
9760 struct target_rlimit
*target_rlim
;
9763 ret
= get_errno(getrlimit(resource
, &rlim
));
9764 if (!is_error(ret
)) {
9765 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
9766 return -TARGET_EFAULT
;
9767 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
9768 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
9769 unlock_user_struct(target_rlim
, arg2
, 1);
9774 case TARGET_NR_getrusage
:
9776 struct rusage rusage
;
9777 ret
= get_errno(getrusage(arg1
, &rusage
));
9778 if (!is_error(ret
)) {
9779 ret
= host_to_target_rusage(arg2
, &rusage
);
9783 #if defined(TARGET_NR_gettimeofday)
9784 case TARGET_NR_gettimeofday
:
9789 ret
= get_errno(gettimeofday(&tv
, &tz
));
9790 if (!is_error(ret
)) {
9791 if (arg1
&& copy_to_user_timeval(arg1
, &tv
)) {
9792 return -TARGET_EFAULT
;
9794 if (arg2
&& copy_to_user_timezone(arg2
, &tz
)) {
9795 return -TARGET_EFAULT
;
9801 #if defined(TARGET_NR_settimeofday)
9802 case TARGET_NR_settimeofday
:
9804 struct timeval tv
, *ptv
= NULL
;
9805 struct timezone tz
, *ptz
= NULL
;
9808 if (copy_from_user_timeval(&tv
, arg1
)) {
9809 return -TARGET_EFAULT
;
9815 if (copy_from_user_timezone(&tz
, arg2
)) {
9816 return -TARGET_EFAULT
;
9821 return get_errno(settimeofday(ptv
, ptz
));
9824 #if defined(TARGET_NR_select)
9825 case TARGET_NR_select
:
9826 #if defined(TARGET_WANT_NI_OLD_SELECT)
9827 /* some architectures used to have old_select here
9828 * but now ENOSYS it.
9830 ret
= -TARGET_ENOSYS
;
9831 #elif defined(TARGET_WANT_OLD_SYS_SELECT)
9832 ret
= do_old_select(arg1
);
9834 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
9838 #ifdef TARGET_NR_pselect6
9839 case TARGET_NR_pselect6
:
9840 return do_pselect6(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, false);
9842 #ifdef TARGET_NR_pselect6_time64
9843 case TARGET_NR_pselect6_time64
:
9844 return do_pselect6(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, true);
9846 #ifdef TARGET_NR_symlink
9847 case TARGET_NR_symlink
:
9850 p
= lock_user_string(arg1
);
9851 p2
= lock_user_string(arg2
);
9853 ret
= -TARGET_EFAULT
;
9855 ret
= get_errno(symlink(p
, p2
));
9856 unlock_user(p2
, arg2
, 0);
9857 unlock_user(p
, arg1
, 0);
9861 #if defined(TARGET_NR_symlinkat)
9862 case TARGET_NR_symlinkat
:
9865 p
= lock_user_string(arg1
);
9866 p2
= lock_user_string(arg3
);
9868 ret
= -TARGET_EFAULT
;
9870 ret
= get_errno(symlinkat(p
, arg2
, p2
));
9871 unlock_user(p2
, arg3
, 0);
9872 unlock_user(p
, arg1
, 0);
9876 #ifdef TARGET_NR_readlink
9877 case TARGET_NR_readlink
:
9880 p
= lock_user_string(arg1
);
9881 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
9883 ret
= -TARGET_EFAULT
;
9885 /* Short circuit this for the magic exe check. */
9886 ret
= -TARGET_EINVAL
;
9887 } else if (is_proc_myself((const char *)p
, "exe")) {
9888 char real
[PATH_MAX
], *temp
;
9889 temp
= realpath(exec_path
, real
);
9890 /* Return value is # of bytes that we wrote to the buffer. */
9892 ret
= get_errno(-1);
9894 /* Don't worry about sign mismatch as earlier mapping
9895 * logic would have thrown a bad address error. */
9896 ret
= MIN(strlen(real
), arg3
);
9897 /* We cannot NUL terminate the string. */
9898 memcpy(p2
, real
, ret
);
9901 ret
= get_errno(readlink(path(p
), p2
, arg3
));
9903 unlock_user(p2
, arg2
, ret
);
9904 unlock_user(p
, arg1
, 0);
9908 #if defined(TARGET_NR_readlinkat)
9909 case TARGET_NR_readlinkat
:
9912 p
= lock_user_string(arg2
);
9913 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
9915 ret
= -TARGET_EFAULT
;
9917 /* Short circuit this for the magic exe check. */
9918 ret
= -TARGET_EINVAL
;
9919 } else if (is_proc_myself((const char *)p
, "exe")) {
9920 char real
[PATH_MAX
], *temp
;
9921 temp
= realpath(exec_path
, real
);
9922 /* Return value is # of bytes that we wrote to the buffer. */
9924 ret
= get_errno(-1);
9926 /* Don't worry about sign mismatch as earlier mapping
9927 * logic would have thrown a bad address error. */
9928 ret
= MIN(strlen(real
), arg4
);
9929 /* We cannot NUL terminate the string. */
9930 memcpy(p2
, real
, ret
);
9933 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
9935 unlock_user(p2
, arg3
, ret
);
9936 unlock_user(p
, arg2
, 0);
9940 #ifdef TARGET_NR_swapon
9941 case TARGET_NR_swapon
:
9942 if (!(p
= lock_user_string(arg1
)))
9943 return -TARGET_EFAULT
;
9944 ret
= get_errno(swapon(p
, arg2
));
9945 unlock_user(p
, arg1
, 0);
9948 case TARGET_NR_reboot
:
9949 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
9950 /* arg4 must be ignored in all other cases */
9951 p
= lock_user_string(arg4
);
9953 return -TARGET_EFAULT
;
9955 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
9956 unlock_user(p
, arg4
, 0);
9958 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
9961 #ifdef TARGET_NR_mmap
9962 case TARGET_NR_mmap
:
9963 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
9964 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
9965 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
9966 || defined(TARGET_S390X)
9969 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
9970 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
9971 return -TARGET_EFAULT
;
9978 unlock_user(v
, arg1
, 0);
9979 ret
= get_errno(target_mmap(v1
, v2
, v3
,
9980 target_to_host_bitmask(v4
, mmap_flags_tbl
),
9984 /* mmap pointers are always untagged */
9985 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
9986 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
9992 #ifdef TARGET_NR_mmap2
9993 case TARGET_NR_mmap2
:
9995 #define MMAP_SHIFT 12
9997 ret
= target_mmap(arg1
, arg2
, arg3
,
9998 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
9999 arg5
, arg6
<< MMAP_SHIFT
);
10000 return get_errno(ret
);
10002 case TARGET_NR_munmap
:
10003 arg1
= cpu_untagged_addr(cpu
, arg1
);
10004 return get_errno(target_munmap(arg1
, arg2
));
10005 case TARGET_NR_mprotect
:
10006 arg1
= cpu_untagged_addr(cpu
, arg1
);
10008 TaskState
*ts
= cpu
->opaque
;
10009 /* Special hack to detect libc making the stack executable. */
10010 if ((arg3
& PROT_GROWSDOWN
)
10011 && arg1
>= ts
->info
->stack_limit
10012 && arg1
<= ts
->info
->start_stack
) {
10013 arg3
&= ~PROT_GROWSDOWN
;
10014 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
10015 arg1
= ts
->info
->stack_limit
;
10018 return get_errno(target_mprotect(arg1
, arg2
, arg3
));
10019 #ifdef TARGET_NR_mremap
10020 case TARGET_NR_mremap
:
10021 arg1
= cpu_untagged_addr(cpu
, arg1
);
10022 /* mremap new_addr (arg5) is always untagged */
10023 return get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
10025 /* ??? msync/mlock/munlock are broken for softmmu. */
10026 #ifdef TARGET_NR_msync
10027 case TARGET_NR_msync
:
10028 return get_errno(msync(g2h(cpu
, arg1
), arg2
, arg3
));
10030 #ifdef TARGET_NR_mlock
10031 case TARGET_NR_mlock
:
10032 return get_errno(mlock(g2h(cpu
, arg1
), arg2
));
10034 #ifdef TARGET_NR_munlock
10035 case TARGET_NR_munlock
:
10036 return get_errno(munlock(g2h(cpu
, arg1
), arg2
));
10038 #ifdef TARGET_NR_mlockall
10039 case TARGET_NR_mlockall
:
10040 return get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
10042 #ifdef TARGET_NR_munlockall
10043 case TARGET_NR_munlockall
:
10044 return get_errno(munlockall());
10046 #ifdef TARGET_NR_truncate
10047 case TARGET_NR_truncate
:
10048 if (!(p
= lock_user_string(arg1
)))
10049 return -TARGET_EFAULT
;
10050 ret
= get_errno(truncate(p
, arg2
));
10051 unlock_user(p
, arg1
, 0);
10054 #ifdef TARGET_NR_ftruncate
10055 case TARGET_NR_ftruncate
:
10056 return get_errno(ftruncate(arg1
, arg2
));
10058 case TARGET_NR_fchmod
:
10059 return get_errno(fchmod(arg1
, arg2
));
10060 #if defined(TARGET_NR_fchmodat)
10061 case TARGET_NR_fchmodat
:
10062 if (!(p
= lock_user_string(arg2
)))
10063 return -TARGET_EFAULT
;
10064 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
10065 unlock_user(p
, arg2
, 0);
10068 case TARGET_NR_getpriority
:
10069 /* Note that negative values are valid for getpriority, so we must
10070 differentiate based on errno settings. */
10072 ret
= getpriority(arg1
, arg2
);
10073 if (ret
== -1 && errno
!= 0) {
10074 return -host_to_target_errno(errno
);
10076 #ifdef TARGET_ALPHA
10077 /* Return value is the unbiased priority. Signal no error. */
10078 cpu_env
->ir
[IR_V0
] = 0;
10080 /* Return value is a biased priority to avoid negative numbers. */
10084 case TARGET_NR_setpriority
:
10085 return get_errno(setpriority(arg1
, arg2
, arg3
));
10086 #ifdef TARGET_NR_statfs
10087 case TARGET_NR_statfs
:
10088 if (!(p
= lock_user_string(arg1
))) {
10089 return -TARGET_EFAULT
;
10091 ret
= get_errno(statfs(path(p
), &stfs
));
10092 unlock_user(p
, arg1
, 0);
10094 if (!is_error(ret
)) {
10095 struct target_statfs
*target_stfs
;
10097 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
10098 return -TARGET_EFAULT
;
10099 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
10100 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
10101 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
10102 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
10103 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
10104 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
10105 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
10106 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
10107 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
10108 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
10109 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
10110 #ifdef _STATFS_F_FLAGS
10111 __put_user(stfs
.f_flags
, &target_stfs
->f_flags
);
10113 __put_user(0, &target_stfs
->f_flags
);
10115 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
10116 unlock_user_struct(target_stfs
, arg2
, 1);
10120 #ifdef TARGET_NR_fstatfs
10121 case TARGET_NR_fstatfs
:
10122 ret
= get_errno(fstatfs(arg1
, &stfs
));
10123 goto convert_statfs
;
10125 #ifdef TARGET_NR_statfs64
10126 case TARGET_NR_statfs64
:
10127 if (!(p
= lock_user_string(arg1
))) {
10128 return -TARGET_EFAULT
;
10130 ret
= get_errno(statfs(path(p
), &stfs
));
10131 unlock_user(p
, arg1
, 0);
10133 if (!is_error(ret
)) {
10134 struct target_statfs64
*target_stfs
;
10136 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
10137 return -TARGET_EFAULT
;
10138 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
10139 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
10140 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
10141 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
10142 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
10143 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
10144 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
10145 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
10146 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
10147 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
10148 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
10149 #ifdef _STATFS_F_FLAGS
10150 __put_user(stfs
.f_flags
, &target_stfs
->f_flags
);
10152 __put_user(0, &target_stfs
->f_flags
);
10154 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
10155 unlock_user_struct(target_stfs
, arg3
, 1);
10158 case TARGET_NR_fstatfs64
:
10159 ret
= get_errno(fstatfs(arg1
, &stfs
));
10160 goto convert_statfs64
;
10162 #ifdef TARGET_NR_socketcall
10163 case TARGET_NR_socketcall
:
10164 return do_socketcall(arg1
, arg2
);
10166 #ifdef TARGET_NR_accept
10167 case TARGET_NR_accept
:
10168 return do_accept4(arg1
, arg2
, arg3
, 0);
10170 #ifdef TARGET_NR_accept4
10171 case TARGET_NR_accept4
:
10172 return do_accept4(arg1
, arg2
, arg3
, arg4
);
10174 #ifdef TARGET_NR_bind
10175 case TARGET_NR_bind
:
10176 return do_bind(arg1
, arg2
, arg3
);
10178 #ifdef TARGET_NR_connect
10179 case TARGET_NR_connect
:
10180 return do_connect(arg1
, arg2
, arg3
);
10182 #ifdef TARGET_NR_getpeername
10183 case TARGET_NR_getpeername
:
10184 return do_getpeername(arg1
, arg2
, arg3
);
10186 #ifdef TARGET_NR_getsockname
10187 case TARGET_NR_getsockname
:
10188 return do_getsockname(arg1
, arg2
, arg3
);
10190 #ifdef TARGET_NR_getsockopt
10191 case TARGET_NR_getsockopt
:
10192 return do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
10194 #ifdef TARGET_NR_listen
10195 case TARGET_NR_listen
:
10196 return get_errno(listen(arg1
, arg2
));
10198 #ifdef TARGET_NR_recv
10199 case TARGET_NR_recv
:
10200 return do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
10202 #ifdef TARGET_NR_recvfrom
10203 case TARGET_NR_recvfrom
:
10204 return do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10206 #ifdef TARGET_NR_recvmsg
10207 case TARGET_NR_recvmsg
:
10208 return do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
10210 #ifdef TARGET_NR_send
10211 case TARGET_NR_send
:
10212 return do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
10214 #ifdef TARGET_NR_sendmsg
10215 case TARGET_NR_sendmsg
:
10216 return do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
10218 #ifdef TARGET_NR_sendmmsg
10219 case TARGET_NR_sendmmsg
:
10220 return do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
10222 #ifdef TARGET_NR_recvmmsg
10223 case TARGET_NR_recvmmsg
:
10224 return do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
10226 #ifdef TARGET_NR_sendto
10227 case TARGET_NR_sendto
:
10228 return do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10230 #ifdef TARGET_NR_shutdown
10231 case TARGET_NR_shutdown
:
10232 return get_errno(shutdown(arg1
, arg2
));
10234 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
10235 case TARGET_NR_getrandom
:
10236 p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
10238 return -TARGET_EFAULT
;
10240 ret
= get_errno(getrandom(p
, arg2
, arg3
));
10241 unlock_user(p
, arg1
, ret
);
10244 #ifdef TARGET_NR_socket
10245 case TARGET_NR_socket
:
10246 return do_socket(arg1
, arg2
, arg3
);
10248 #ifdef TARGET_NR_socketpair
10249 case TARGET_NR_socketpair
:
10250 return do_socketpair(arg1
, arg2
, arg3
, arg4
);
10252 #ifdef TARGET_NR_setsockopt
10253 case TARGET_NR_setsockopt
:
10254 return do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
10256 #if defined(TARGET_NR_syslog)
10257 case TARGET_NR_syslog
:
10262 case TARGET_SYSLOG_ACTION_CLOSE
: /* Close log */
10263 case TARGET_SYSLOG_ACTION_OPEN
: /* Open log */
10264 case TARGET_SYSLOG_ACTION_CLEAR
: /* Clear ring buffer */
10265 case TARGET_SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging */
10266 case TARGET_SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging */
10267 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL
: /* Set messages level */
10268 case TARGET_SYSLOG_ACTION_SIZE_UNREAD
: /* Number of chars */
10269 case TARGET_SYSLOG_ACTION_SIZE_BUFFER
: /* Size of the buffer */
10270 return get_errno(sys_syslog((int)arg1
, NULL
, (int)arg3
));
10271 case TARGET_SYSLOG_ACTION_READ
: /* Read from log */
10272 case TARGET_SYSLOG_ACTION_READ_CLEAR
: /* Read/clear msgs */
10273 case TARGET_SYSLOG_ACTION_READ_ALL
: /* Read last messages */
10276 return -TARGET_EINVAL
;
10281 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10283 return -TARGET_EFAULT
;
10285 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
10286 unlock_user(p
, arg2
, arg3
);
10290 return -TARGET_EINVAL
;
10295 case TARGET_NR_setitimer
:
10297 struct itimerval value
, ovalue
, *pvalue
;
10301 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
10302 || copy_from_user_timeval(&pvalue
->it_value
,
10303 arg2
+ sizeof(struct target_timeval
)))
10304 return -TARGET_EFAULT
;
10308 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
10309 if (!is_error(ret
) && arg3
) {
10310 if (copy_to_user_timeval(arg3
,
10311 &ovalue
.it_interval
)
10312 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
10314 return -TARGET_EFAULT
;
10318 case TARGET_NR_getitimer
:
10320 struct itimerval value
;
10322 ret
= get_errno(getitimer(arg1
, &value
));
10323 if (!is_error(ret
) && arg2
) {
10324 if (copy_to_user_timeval(arg2
,
10325 &value
.it_interval
)
10326 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
10328 return -TARGET_EFAULT
;
10332 #ifdef TARGET_NR_stat
10333 case TARGET_NR_stat
:
10334 if (!(p
= lock_user_string(arg1
))) {
10335 return -TARGET_EFAULT
;
10337 ret
= get_errno(stat(path(p
), &st
));
10338 unlock_user(p
, arg1
, 0);
10341 #ifdef TARGET_NR_lstat
10342 case TARGET_NR_lstat
:
10343 if (!(p
= lock_user_string(arg1
))) {
10344 return -TARGET_EFAULT
;
10346 ret
= get_errno(lstat(path(p
), &st
));
10347 unlock_user(p
, arg1
, 0);
10350 #ifdef TARGET_NR_fstat
10351 case TARGET_NR_fstat
:
10353 ret
= get_errno(fstat(arg1
, &st
));
10354 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
10357 if (!is_error(ret
)) {
10358 struct target_stat
*target_st
;
10360 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
10361 return -TARGET_EFAULT
;
10362 memset(target_st
, 0, sizeof(*target_st
));
10363 __put_user(st
.st_dev
, &target_st
->st_dev
);
10364 __put_user(st
.st_ino
, &target_st
->st_ino
);
10365 __put_user(st
.st_mode
, &target_st
->st_mode
);
10366 __put_user(st
.st_uid
, &target_st
->st_uid
);
10367 __put_user(st
.st_gid
, &target_st
->st_gid
);
10368 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
10369 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
10370 __put_user(st
.st_size
, &target_st
->st_size
);
10371 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
10372 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
10373 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
10374 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
10375 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
10376 #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
10377 __put_user(st
.st_atim
.tv_nsec
,
10378 &target_st
->target_st_atime_nsec
);
10379 __put_user(st
.st_mtim
.tv_nsec
,
10380 &target_st
->target_st_mtime_nsec
);
10381 __put_user(st
.st_ctim
.tv_nsec
,
10382 &target_st
->target_st_ctime_nsec
);
10384 unlock_user_struct(target_st
, arg2
, 1);
10389 case TARGET_NR_vhangup
:
10390 return get_errno(vhangup());
10391 #ifdef TARGET_NR_syscall
10392 case TARGET_NR_syscall
:
10393 return do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
10394 arg6
, arg7
, arg8
, 0);
10396 #if defined(TARGET_NR_wait4)
10397 case TARGET_NR_wait4
:
10400 abi_long status_ptr
= arg2
;
10401 struct rusage rusage
, *rusage_ptr
;
10402 abi_ulong target_rusage
= arg4
;
10403 abi_long rusage_err
;
10405 rusage_ptr
= &rusage
;
10408 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, rusage_ptr
));
10409 if (!is_error(ret
)) {
10410 if (status_ptr
&& ret
) {
10411 status
= host_to_target_waitstatus(status
);
10412 if (put_user_s32(status
, status_ptr
))
10413 return -TARGET_EFAULT
;
10415 if (target_rusage
) {
10416 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
10425 #ifdef TARGET_NR_swapoff
10426 case TARGET_NR_swapoff
:
10427 if (!(p
= lock_user_string(arg1
)))
10428 return -TARGET_EFAULT
;
10429 ret
= get_errno(swapoff(p
));
10430 unlock_user(p
, arg1
, 0);
10433 case TARGET_NR_sysinfo
:
10435 struct target_sysinfo
*target_value
;
10436 struct sysinfo value
;
10437 ret
= get_errno(sysinfo(&value
));
10438 if (!is_error(ret
) && arg1
)
10440 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
10441 return -TARGET_EFAULT
;
10442 __put_user(value
.uptime
, &target_value
->uptime
);
10443 __put_user(value
.loads
[0], &target_value
->loads
[0]);
10444 __put_user(value
.loads
[1], &target_value
->loads
[1]);
10445 __put_user(value
.loads
[2], &target_value
->loads
[2]);
10446 __put_user(value
.totalram
, &target_value
->totalram
);
10447 __put_user(value
.freeram
, &target_value
->freeram
);
10448 __put_user(value
.sharedram
, &target_value
->sharedram
);
10449 __put_user(value
.bufferram
, &target_value
->bufferram
);
10450 __put_user(value
.totalswap
, &target_value
->totalswap
);
10451 __put_user(value
.freeswap
, &target_value
->freeswap
);
10452 __put_user(value
.procs
, &target_value
->procs
);
10453 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
10454 __put_user(value
.freehigh
, &target_value
->freehigh
);
10455 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
10456 unlock_user_struct(target_value
, arg1
, 1);
10460 #ifdef TARGET_NR_ipc
10461 case TARGET_NR_ipc
:
10462 return do_ipc(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10464 #ifdef TARGET_NR_semget
10465 case TARGET_NR_semget
:
10466 return get_errno(semget(arg1
, arg2
, arg3
));
10468 #ifdef TARGET_NR_semop
10469 case TARGET_NR_semop
:
10470 return do_semtimedop(arg1
, arg2
, arg3
, 0, false);
10472 #ifdef TARGET_NR_semtimedop
10473 case TARGET_NR_semtimedop
:
10474 return do_semtimedop(arg1
, arg2
, arg3
, arg4
, false);
10476 #ifdef TARGET_NR_semtimedop_time64
10477 case TARGET_NR_semtimedop_time64
:
10478 return do_semtimedop(arg1
, arg2
, arg3
, arg4
, true);
10480 #ifdef TARGET_NR_semctl
10481 case TARGET_NR_semctl
:
10482 return do_semctl(arg1
, arg2
, arg3
, arg4
);
10484 #ifdef TARGET_NR_msgctl
10485 case TARGET_NR_msgctl
:
10486 return do_msgctl(arg1
, arg2
, arg3
);
10488 #ifdef TARGET_NR_msgget
10489 case TARGET_NR_msgget
:
10490 return get_errno(msgget(arg1
, arg2
));
10492 #ifdef TARGET_NR_msgrcv
10493 case TARGET_NR_msgrcv
:
10494 return do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
10496 #ifdef TARGET_NR_msgsnd
10497 case TARGET_NR_msgsnd
:
10498 return do_msgsnd(arg1
, arg2
, arg3
, arg4
);
10500 #ifdef TARGET_NR_shmget
10501 case TARGET_NR_shmget
:
10502 return get_errno(shmget(arg1
, arg2
, arg3
));
10504 #ifdef TARGET_NR_shmctl
10505 case TARGET_NR_shmctl
:
10506 return do_shmctl(arg1
, arg2
, arg3
);
10508 #ifdef TARGET_NR_shmat
10509 case TARGET_NR_shmat
:
10510 return do_shmat(cpu_env
, arg1
, arg2
, arg3
);
10512 #ifdef TARGET_NR_shmdt
10513 case TARGET_NR_shmdt
:
10514 return do_shmdt(arg1
);
10516 case TARGET_NR_fsync
:
10517 return get_errno(fsync(arg1
));
10518 case TARGET_NR_clone
:
10519 /* Linux manages to have three different orderings for its
10520 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
10521 * match the kernel's CONFIG_CLONE_* settings.
10522 * Microblaze is further special in that it uses a sixth
10523 * implicit argument to clone for the TLS pointer.
10525 #if defined(TARGET_MICROBLAZE)
10526 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
10527 #elif defined(TARGET_CLONE_BACKWARDS)
10528 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
10529 #elif defined(TARGET_CLONE_BACKWARDS2)
10530 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
10532 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
10535 #ifdef __NR_exit_group
10536 /* new thread calls */
10537 case TARGET_NR_exit_group
:
10538 preexit_cleanup(cpu_env
, arg1
);
10539 return get_errno(exit_group(arg1
));
10541 case TARGET_NR_setdomainname
:
10542 if (!(p
= lock_user_string(arg1
)))
10543 return -TARGET_EFAULT
;
10544 ret
= get_errno(setdomainname(p
, arg2
));
10545 unlock_user(p
, arg1
, 0);
10547 case TARGET_NR_uname
:
10548 /* no need to transcode because we use the linux syscall */
10550 struct new_utsname
* buf
;
10552 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
10553 return -TARGET_EFAULT
;
10554 ret
= get_errno(sys_uname(buf
));
10555 if (!is_error(ret
)) {
10556 /* Overwrite the native machine name with whatever is being
10558 g_strlcpy(buf
->machine
, cpu_to_uname_machine(cpu_env
),
10559 sizeof(buf
->machine
));
10560 /* Allow the user to override the reported release. */
10561 if (qemu_uname_release
&& *qemu_uname_release
) {
10562 g_strlcpy(buf
->release
, qemu_uname_release
,
10563 sizeof(buf
->release
));
10566 unlock_user_struct(buf
, arg1
, 1);
10570 case TARGET_NR_modify_ldt
:
10571 return do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
10572 #if !defined(TARGET_X86_64)
10573 case TARGET_NR_vm86
:
10574 return do_vm86(cpu_env
, arg1
, arg2
);
10577 #if defined(TARGET_NR_adjtimex)
10578 case TARGET_NR_adjtimex
:
10580 struct timex host_buf
;
10582 if (target_to_host_timex(&host_buf
, arg1
) != 0) {
10583 return -TARGET_EFAULT
;
10585 ret
= get_errno(adjtimex(&host_buf
));
10586 if (!is_error(ret
)) {
10587 if (host_to_target_timex(arg1
, &host_buf
) != 0) {
10588 return -TARGET_EFAULT
;
10594 #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10595 case TARGET_NR_clock_adjtime
:
10597 struct timex htx
, *phtx
= &htx
;
10599 if (target_to_host_timex(phtx
, arg2
) != 0) {
10600 return -TARGET_EFAULT
;
10602 ret
= get_errno(clock_adjtime(arg1
, phtx
));
10603 if (!is_error(ret
) && phtx
) {
10604 if (host_to_target_timex(arg2
, phtx
) != 0) {
10605 return -TARGET_EFAULT
;
10611 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
10612 case TARGET_NR_clock_adjtime64
:
10616 if (target_to_host_timex64(&htx
, arg2
) != 0) {
10617 return -TARGET_EFAULT
;
10619 ret
= get_errno(clock_adjtime(arg1
, &htx
));
10620 if (!is_error(ret
) && host_to_target_timex64(arg2
, &htx
)) {
10621 return -TARGET_EFAULT
;
10626 case TARGET_NR_getpgid
:
10627 return get_errno(getpgid(arg1
));
10628 case TARGET_NR_fchdir
:
10629 return get_errno(fchdir(arg1
));
10630 case TARGET_NR_personality
:
10631 return get_errno(personality(arg1
));
10632 #ifdef TARGET_NR__llseek /* Not on alpha */
10633 case TARGET_NR__llseek
:
10636 #if !defined(__NR_llseek)
10637 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | (abi_ulong
)arg3
, arg5
);
10639 ret
= get_errno(res
);
10644 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
10646 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
10647 return -TARGET_EFAULT
;
10652 #ifdef TARGET_NR_getdents
10653 case TARGET_NR_getdents
:
10654 return do_getdents(arg1
, arg2
, arg3
);
10655 #endif /* TARGET_NR_getdents */
10656 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
10657 case TARGET_NR_getdents64
:
10658 return do_getdents64(arg1
, arg2
, arg3
);
10659 #endif /* TARGET_NR_getdents64 */
10660 #if defined(TARGET_NR__newselect)
10661 case TARGET_NR__newselect
:
10662 return do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
10664 #ifdef TARGET_NR_poll
10665 case TARGET_NR_poll
:
10666 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, false, false);
10668 #ifdef TARGET_NR_ppoll
10669 case TARGET_NR_ppoll
:
10670 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, true, false);
10672 #ifdef TARGET_NR_ppoll_time64
10673 case TARGET_NR_ppoll_time64
:
10674 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, true, true);
10676 case TARGET_NR_flock
:
10677 /* NOTE: the flock constant seems to be the same for every
10679 return get_errno(safe_flock(arg1
, arg2
));
10680 case TARGET_NR_readv
:
10682 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
10684 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
10685 unlock_iovec(vec
, arg2
, arg3
, 1);
10687 ret
= -host_to_target_errno(errno
);
10691 case TARGET_NR_writev
:
10693 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
10695 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
10696 unlock_iovec(vec
, arg2
, arg3
, 0);
10698 ret
= -host_to_target_errno(errno
);
10702 #if defined(TARGET_NR_preadv)
10703 case TARGET_NR_preadv
:
10705 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
10707 unsigned long low
, high
;
10709 target_to_host_low_high(arg4
, arg5
, &low
, &high
);
10710 ret
= get_errno(safe_preadv(arg1
, vec
, arg3
, low
, high
));
10711 unlock_iovec(vec
, arg2
, arg3
, 1);
10713 ret
= -host_to_target_errno(errno
);
10718 #if defined(TARGET_NR_pwritev)
10719 case TARGET_NR_pwritev
:
10721 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
10723 unsigned long low
, high
;
10725 target_to_host_low_high(arg4
, arg5
, &low
, &high
);
10726 ret
= get_errno(safe_pwritev(arg1
, vec
, arg3
, low
, high
));
10727 unlock_iovec(vec
, arg2
, arg3
, 0);
10729 ret
= -host_to_target_errno(errno
);
10734 case TARGET_NR_getsid
:
10735 return get_errno(getsid(arg1
));
10736 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
10737 case TARGET_NR_fdatasync
:
10738 return get_errno(fdatasync(arg1
));
10740 case TARGET_NR_sched_getaffinity
:
10742 unsigned int mask_size
;
10743 unsigned long *mask
;
10746 * sched_getaffinity needs multiples of ulong, so need to take
10747 * care of mismatches between target ulong and host ulong sizes.
10749 if (arg2
& (sizeof(abi_ulong
) - 1)) {
10750 return -TARGET_EINVAL
;
10752 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
10754 mask
= alloca(mask_size
);
10755 memset(mask
, 0, mask_size
);
10756 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
10758 if (!is_error(ret
)) {
10760 /* More data returned than the caller's buffer will fit.
10761 * This only happens if sizeof(abi_long) < sizeof(long)
10762 * and the caller passed us a buffer holding an odd number
10763 * of abi_longs. If the host kernel is actually using the
10764 * extra 4 bytes then fail EINVAL; otherwise we can just
10765 * ignore them and only copy the interesting part.
10767 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
10768 if (numcpus
> arg2
* 8) {
10769 return -TARGET_EINVAL
;
10774 if (host_to_target_cpu_mask(mask
, mask_size
, arg3
, ret
)) {
10775 return -TARGET_EFAULT
;
10780 case TARGET_NR_sched_setaffinity
:
10782 unsigned int mask_size
;
10783 unsigned long *mask
;
10786 * sched_setaffinity needs multiples of ulong, so need to take
10787 * care of mismatches between target ulong and host ulong sizes.
10789 if (arg2
& (sizeof(abi_ulong
) - 1)) {
10790 return -TARGET_EINVAL
;
10792 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
10793 mask
= alloca(mask_size
);
10795 ret
= target_to_host_cpu_mask(mask
, mask_size
, arg3
, arg2
);
10800 return get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
10802 case TARGET_NR_getcpu
:
10804 unsigned cpu
, node
;
10805 ret
= get_errno(sys_getcpu(arg1
? &cpu
: NULL
,
10806 arg2
? &node
: NULL
,
10808 if (is_error(ret
)) {
10811 if (arg1
&& put_user_u32(cpu
, arg1
)) {
10812 return -TARGET_EFAULT
;
10814 if (arg2
&& put_user_u32(node
, arg2
)) {
10815 return -TARGET_EFAULT
;
10819 case TARGET_NR_sched_setparam
:
10821 struct target_sched_param
*target_schp
;
10822 struct sched_param schp
;
10825 return -TARGET_EINVAL
;
10827 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1)) {
10828 return -TARGET_EFAULT
;
10830 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
10831 unlock_user_struct(target_schp
, arg2
, 0);
10832 return get_errno(sys_sched_setparam(arg1
, &schp
));
10834 case TARGET_NR_sched_getparam
:
10836 struct target_sched_param
*target_schp
;
10837 struct sched_param schp
;
10840 return -TARGET_EINVAL
;
10842 ret
= get_errno(sys_sched_getparam(arg1
, &schp
));
10843 if (!is_error(ret
)) {
10844 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0)) {
10845 return -TARGET_EFAULT
;
10847 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
10848 unlock_user_struct(target_schp
, arg2
, 1);
10852 case TARGET_NR_sched_setscheduler
:
10854 struct target_sched_param
*target_schp
;
10855 struct sched_param schp
;
10857 return -TARGET_EINVAL
;
10859 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1)) {
10860 return -TARGET_EFAULT
;
10862 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
10863 unlock_user_struct(target_schp
, arg3
, 0);
10864 return get_errno(sys_sched_setscheduler(arg1
, arg2
, &schp
));
10866 case TARGET_NR_sched_getscheduler
:
10867 return get_errno(sys_sched_getscheduler(arg1
));
10868 case TARGET_NR_sched_getattr
:
10870 struct target_sched_attr
*target_scha
;
10871 struct sched_attr scha
;
10873 return -TARGET_EINVAL
;
10875 if (arg3
> sizeof(scha
)) {
10876 arg3
= sizeof(scha
);
10878 ret
= get_errno(sys_sched_getattr(arg1
, &scha
, arg3
, arg4
));
10879 if (!is_error(ret
)) {
10880 target_scha
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10881 if (!target_scha
) {
10882 return -TARGET_EFAULT
;
10884 target_scha
->size
= tswap32(scha
.size
);
10885 target_scha
->sched_policy
= tswap32(scha
.sched_policy
);
10886 target_scha
->sched_flags
= tswap64(scha
.sched_flags
);
10887 target_scha
->sched_nice
= tswap32(scha
.sched_nice
);
10888 target_scha
->sched_priority
= tswap32(scha
.sched_priority
);
10889 target_scha
->sched_runtime
= tswap64(scha
.sched_runtime
);
10890 target_scha
->sched_deadline
= tswap64(scha
.sched_deadline
);
10891 target_scha
->sched_period
= tswap64(scha
.sched_period
);
10892 if (scha
.size
> offsetof(struct sched_attr
, sched_util_min
)) {
10893 target_scha
->sched_util_min
= tswap32(scha
.sched_util_min
);
10894 target_scha
->sched_util_max
= tswap32(scha
.sched_util_max
);
10896 unlock_user(target_scha
, arg2
, arg3
);
10900 case TARGET_NR_sched_setattr
:
10902 struct target_sched_attr
*target_scha
;
10903 struct sched_attr scha
;
10907 return -TARGET_EINVAL
;
10909 if (get_user_u32(size
, arg2
)) {
10910 return -TARGET_EFAULT
;
10913 size
= offsetof(struct target_sched_attr
, sched_util_min
);
10915 if (size
< offsetof(struct target_sched_attr
, sched_util_min
)) {
10916 if (put_user_u32(sizeof(struct target_sched_attr
), arg2
)) {
10917 return -TARGET_EFAULT
;
10919 return -TARGET_E2BIG
;
10922 zeroed
= check_zeroed_user(arg2
, sizeof(struct target_sched_attr
), size
);
10925 } else if (zeroed
== 0) {
10926 if (put_user_u32(sizeof(struct target_sched_attr
), arg2
)) {
10927 return -TARGET_EFAULT
;
10929 return -TARGET_E2BIG
;
10931 if (size
> sizeof(struct target_sched_attr
)) {
10932 size
= sizeof(struct target_sched_attr
);
10935 target_scha
= lock_user(VERIFY_READ
, arg2
, size
, 1);
10936 if (!target_scha
) {
10937 return -TARGET_EFAULT
;
10940 scha
.sched_policy
= tswap32(target_scha
->sched_policy
);
10941 scha
.sched_flags
= tswap64(target_scha
->sched_flags
);
10942 scha
.sched_nice
= tswap32(target_scha
->sched_nice
);
10943 scha
.sched_priority
= tswap32(target_scha
->sched_priority
);
10944 scha
.sched_runtime
= tswap64(target_scha
->sched_runtime
);
10945 scha
.sched_deadline
= tswap64(target_scha
->sched_deadline
);
10946 scha
.sched_period
= tswap64(target_scha
->sched_period
);
10947 if (size
> offsetof(struct target_sched_attr
, sched_util_min
)) {
10948 scha
.sched_util_min
= tswap32(target_scha
->sched_util_min
);
10949 scha
.sched_util_max
= tswap32(target_scha
->sched_util_max
);
10951 unlock_user(target_scha
, arg2
, 0);
10952 return get_errno(sys_sched_setattr(arg1
, &scha
, arg3
));
10954 case TARGET_NR_sched_yield
:
10955 return get_errno(sched_yield());
10956 case TARGET_NR_sched_get_priority_max
:
10957 return get_errno(sched_get_priority_max(arg1
));
10958 case TARGET_NR_sched_get_priority_min
:
10959 return get_errno(sched_get_priority_min(arg1
));
10960 #ifdef TARGET_NR_sched_rr_get_interval
10961 case TARGET_NR_sched_rr_get_interval
:
10963 struct timespec ts
;
10964 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
10965 if (!is_error(ret
)) {
10966 ret
= host_to_target_timespec(arg2
, &ts
);
10971 #ifdef TARGET_NR_sched_rr_get_interval_time64
10972 case TARGET_NR_sched_rr_get_interval_time64
:
10974 struct timespec ts
;
10975 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
10976 if (!is_error(ret
)) {
10977 ret
= host_to_target_timespec64(arg2
, &ts
);
10982 #if defined(TARGET_NR_nanosleep)
10983 case TARGET_NR_nanosleep
:
10985 struct timespec req
, rem
;
10986 target_to_host_timespec(&req
, arg1
);
10987 ret
= get_errno(safe_nanosleep(&req
, &rem
));
10988 if (is_error(ret
) && arg2
) {
10989 host_to_target_timespec(arg2
, &rem
);
10994 case TARGET_NR_prctl
:
10995 return do_prctl(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
);
10997 #ifdef TARGET_NR_arch_prctl
10998 case TARGET_NR_arch_prctl
:
10999 return do_arch_prctl(cpu_env
, arg1
, arg2
);
11001 #ifdef TARGET_NR_pread64
11002 case TARGET_NR_pread64
:
11003 if (regpairs_aligned(cpu_env
, num
)) {
11007 if (arg2
== 0 && arg3
== 0) {
11008 /* Special-case NULL buffer and zero length, which should succeed */
11011 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11013 return -TARGET_EFAULT
;
11016 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
11017 unlock_user(p
, arg2
, ret
);
11019 case TARGET_NR_pwrite64
:
11020 if (regpairs_aligned(cpu_env
, num
)) {
11024 if (arg2
== 0 && arg3
== 0) {
11025 /* Special-case NULL buffer and zero length, which should succeed */
11028 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
11030 return -TARGET_EFAULT
;
11033 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
11034 unlock_user(p
, arg2
, 0);
11037 case TARGET_NR_getcwd
:
11038 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
11039 return -TARGET_EFAULT
;
11040 ret
= get_errno(sys_getcwd1(p
, arg2
));
11041 unlock_user(p
, arg1
, ret
);
11043 case TARGET_NR_capget
:
11044 case TARGET_NR_capset
:
11046 struct target_user_cap_header
*target_header
;
11047 struct target_user_cap_data
*target_data
= NULL
;
11048 struct __user_cap_header_struct header
;
11049 struct __user_cap_data_struct data
[2];
11050 struct __user_cap_data_struct
*dataptr
= NULL
;
11051 int i
, target_datalen
;
11052 int data_items
= 1;
11054 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
11055 return -TARGET_EFAULT
;
11057 header
.version
= tswap32(target_header
->version
);
11058 header
.pid
= tswap32(target_header
->pid
);
11060 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
11061 /* Version 2 and up takes pointer to two user_data structs */
11065 target_datalen
= sizeof(*target_data
) * data_items
;
11068 if (num
== TARGET_NR_capget
) {
11069 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
11071 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
11073 if (!target_data
) {
11074 unlock_user_struct(target_header
, arg1
, 0);
11075 return -TARGET_EFAULT
;
11078 if (num
== TARGET_NR_capset
) {
11079 for (i
= 0; i
< data_items
; i
++) {
11080 data
[i
].effective
= tswap32(target_data
[i
].effective
);
11081 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
11082 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
11089 if (num
== TARGET_NR_capget
) {
11090 ret
= get_errno(capget(&header
, dataptr
));
11092 ret
= get_errno(capset(&header
, dataptr
));
11095 /* The kernel always updates version for both capget and capset */
11096 target_header
->version
= tswap32(header
.version
);
11097 unlock_user_struct(target_header
, arg1
, 1);
11100 if (num
== TARGET_NR_capget
) {
11101 for (i
= 0; i
< data_items
; i
++) {
11102 target_data
[i
].effective
= tswap32(data
[i
].effective
);
11103 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
11104 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
11106 unlock_user(target_data
, arg2
, target_datalen
);
11108 unlock_user(target_data
, arg2
, 0);
11113 case TARGET_NR_sigaltstack
:
11114 return do_sigaltstack(arg1
, arg2
, cpu_env
);
11116 #ifdef CONFIG_SENDFILE
11117 #ifdef TARGET_NR_sendfile
11118 case TARGET_NR_sendfile
:
11120 off_t
*offp
= NULL
;
11123 ret
= get_user_sal(off
, arg3
);
11124 if (is_error(ret
)) {
11129 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
11130 if (!is_error(ret
) && arg3
) {
11131 abi_long ret2
= put_user_sal(off
, arg3
);
11132 if (is_error(ret2
)) {
11139 #ifdef TARGET_NR_sendfile64
11140 case TARGET_NR_sendfile64
:
11142 off_t
*offp
= NULL
;
11145 ret
= get_user_s64(off
, arg3
);
11146 if (is_error(ret
)) {
11151 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
11152 if (!is_error(ret
) && arg3
) {
11153 abi_long ret2
= put_user_s64(off
, arg3
);
11154 if (is_error(ret2
)) {
11162 #ifdef TARGET_NR_vfork
11163 case TARGET_NR_vfork
:
11164 return get_errno(do_fork(cpu_env
,
11165 CLONE_VFORK
| CLONE_VM
| TARGET_SIGCHLD
,
11168 #ifdef TARGET_NR_ugetrlimit
11169 case TARGET_NR_ugetrlimit
:
11171 struct rlimit rlim
;
11172 int resource
= target_to_host_resource(arg1
);
11173 ret
= get_errno(getrlimit(resource
, &rlim
));
11174 if (!is_error(ret
)) {
11175 struct target_rlimit
*target_rlim
;
11176 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
11177 return -TARGET_EFAULT
;
11178 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
11179 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
11180 unlock_user_struct(target_rlim
, arg2
, 1);
11185 #ifdef TARGET_NR_truncate64
11186 case TARGET_NR_truncate64
:
11187 if (!(p
= lock_user_string(arg1
)))
11188 return -TARGET_EFAULT
;
11189 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
11190 unlock_user(p
, arg1
, 0);
11193 #ifdef TARGET_NR_ftruncate64
11194 case TARGET_NR_ftruncate64
:
11195 return target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
11197 #ifdef TARGET_NR_stat64
11198 case TARGET_NR_stat64
:
11199 if (!(p
= lock_user_string(arg1
))) {
11200 return -TARGET_EFAULT
;
11202 ret
= get_errno(stat(path(p
), &st
));
11203 unlock_user(p
, arg1
, 0);
11204 if (!is_error(ret
))
11205 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11208 #ifdef TARGET_NR_lstat64
11209 case TARGET_NR_lstat64
:
11210 if (!(p
= lock_user_string(arg1
))) {
11211 return -TARGET_EFAULT
;
11213 ret
= get_errno(lstat(path(p
), &st
));
11214 unlock_user(p
, arg1
, 0);
11215 if (!is_error(ret
))
11216 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11219 #ifdef TARGET_NR_fstat64
11220 case TARGET_NR_fstat64
:
11221 ret
= get_errno(fstat(arg1
, &st
));
11222 if (!is_error(ret
))
11223 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11226 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
11227 #ifdef TARGET_NR_fstatat64
11228 case TARGET_NR_fstatat64
:
11230 #ifdef TARGET_NR_newfstatat
11231 case TARGET_NR_newfstatat
:
11233 if (!(p
= lock_user_string(arg2
))) {
11234 return -TARGET_EFAULT
;
11236 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
11237 unlock_user(p
, arg2
, 0);
11238 if (!is_error(ret
))
11239 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
11242 #if defined(TARGET_NR_statx)
11243 case TARGET_NR_statx
:
11245 struct target_statx
*target_stx
;
11249 p
= lock_user_string(arg2
);
11251 return -TARGET_EFAULT
;
11253 #if defined(__NR_statx)
11256 * It is assumed that struct statx is architecture independent.
11258 struct target_statx host_stx
;
11261 ret
= get_errno(sys_statx(dirfd
, p
, flags
, mask
, &host_stx
));
11262 if (!is_error(ret
)) {
11263 if (host_to_target_statx(&host_stx
, arg5
) != 0) {
11264 unlock_user(p
, arg2
, 0);
11265 return -TARGET_EFAULT
;
11269 if (ret
!= -TARGET_ENOSYS
) {
11270 unlock_user(p
, arg2
, 0);
11275 ret
= get_errno(fstatat(dirfd
, path(p
), &st
, flags
));
11276 unlock_user(p
, arg2
, 0);
11278 if (!is_error(ret
)) {
11279 if (!lock_user_struct(VERIFY_WRITE
, target_stx
, arg5
, 0)) {
11280 return -TARGET_EFAULT
;
11282 memset(target_stx
, 0, sizeof(*target_stx
));
11283 __put_user(major(st
.st_dev
), &target_stx
->stx_dev_major
);
11284 __put_user(minor(st
.st_dev
), &target_stx
->stx_dev_minor
);
11285 __put_user(st
.st_ino
, &target_stx
->stx_ino
);
11286 __put_user(st
.st_mode
, &target_stx
->stx_mode
);
11287 __put_user(st
.st_uid
, &target_stx
->stx_uid
);
11288 __put_user(st
.st_gid
, &target_stx
->stx_gid
);
11289 __put_user(st
.st_nlink
, &target_stx
->stx_nlink
);
11290 __put_user(major(st
.st_rdev
), &target_stx
->stx_rdev_major
);
11291 __put_user(minor(st
.st_rdev
), &target_stx
->stx_rdev_minor
);
11292 __put_user(st
.st_size
, &target_stx
->stx_size
);
11293 __put_user(st
.st_blksize
, &target_stx
->stx_blksize
);
11294 __put_user(st
.st_blocks
, &target_stx
->stx_blocks
);
11295 __put_user(st
.st_atime
, &target_stx
->stx_atime
.tv_sec
);
11296 __put_user(st
.st_mtime
, &target_stx
->stx_mtime
.tv_sec
);
11297 __put_user(st
.st_ctime
, &target_stx
->stx_ctime
.tv_sec
);
11298 unlock_user_struct(target_stx
, arg5
, 1);
11303 #ifdef TARGET_NR_lchown
11304 case TARGET_NR_lchown
:
11305 if (!(p
= lock_user_string(arg1
)))
11306 return -TARGET_EFAULT
;
11307 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
11308 unlock_user(p
, arg1
, 0);
11311 #ifdef TARGET_NR_getuid
11312 case TARGET_NR_getuid
:
11313 return get_errno(high2lowuid(getuid()));
11315 #ifdef TARGET_NR_getgid
11316 case TARGET_NR_getgid
:
11317 return get_errno(high2lowgid(getgid()));
11319 #ifdef TARGET_NR_geteuid
11320 case TARGET_NR_geteuid
:
11321 return get_errno(high2lowuid(geteuid()));
11323 #ifdef TARGET_NR_getegid
11324 case TARGET_NR_getegid
:
11325 return get_errno(high2lowgid(getegid()));
11327 case TARGET_NR_setreuid
:
11328 return get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
11329 case TARGET_NR_setregid
:
11330 return get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
11331 case TARGET_NR_getgroups
:
11333 int gidsetsize
= arg1
;
11334 target_id
*target_grouplist
;
11338 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11339 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
11340 if (gidsetsize
== 0)
11342 if (!is_error(ret
)) {
11343 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* sizeof(target_id
), 0);
11344 if (!target_grouplist
)
11345 return -TARGET_EFAULT
;
11346 for(i
= 0;i
< ret
; i
++)
11347 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
11348 unlock_user(target_grouplist
, arg2
, gidsetsize
* sizeof(target_id
));
11352 case TARGET_NR_setgroups
:
11354 int gidsetsize
= arg1
;
11355 target_id
*target_grouplist
;
11356 gid_t
*grouplist
= NULL
;
11359 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11360 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* sizeof(target_id
), 1);
11361 if (!target_grouplist
) {
11362 return -TARGET_EFAULT
;
11364 for (i
= 0; i
< gidsetsize
; i
++) {
11365 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
11367 unlock_user(target_grouplist
, arg2
, 0);
11369 return get_errno(setgroups(gidsetsize
, grouplist
));
11371 case TARGET_NR_fchown
:
11372 return get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
11373 #if defined(TARGET_NR_fchownat)
11374 case TARGET_NR_fchownat
:
11375 if (!(p
= lock_user_string(arg2
)))
11376 return -TARGET_EFAULT
;
11377 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
11378 low2highgid(arg4
), arg5
));
11379 unlock_user(p
, arg2
, 0);
11382 #ifdef TARGET_NR_setresuid
11383 case TARGET_NR_setresuid
:
11384 return get_errno(sys_setresuid(low2highuid(arg1
),
11386 low2highuid(arg3
)));
11388 #ifdef TARGET_NR_getresuid
11389 case TARGET_NR_getresuid
:
11391 uid_t ruid
, euid
, suid
;
11392 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11393 if (!is_error(ret
)) {
11394 if (put_user_id(high2lowuid(ruid
), arg1
)
11395 || put_user_id(high2lowuid(euid
), arg2
)
11396 || put_user_id(high2lowuid(suid
), arg3
))
11397 return -TARGET_EFAULT
;
11402 #ifdef TARGET_NR_getresgid
11403 case TARGET_NR_setresgid
:
11404 return get_errno(sys_setresgid(low2highgid(arg1
),
11406 low2highgid(arg3
)));
11408 #ifdef TARGET_NR_getresgid
11409 case TARGET_NR_getresgid
:
11411 gid_t rgid
, egid
, sgid
;
11412 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11413 if (!is_error(ret
)) {
11414 if (put_user_id(high2lowgid(rgid
), arg1
)
11415 || put_user_id(high2lowgid(egid
), arg2
)
11416 || put_user_id(high2lowgid(sgid
), arg3
))
11417 return -TARGET_EFAULT
;
11422 #ifdef TARGET_NR_chown
11423 case TARGET_NR_chown
:
11424 if (!(p
= lock_user_string(arg1
)))
11425 return -TARGET_EFAULT
;
11426 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
11427 unlock_user(p
, arg1
, 0);
11430 case TARGET_NR_setuid
:
11431 return get_errno(sys_setuid(low2highuid(arg1
)));
11432 case TARGET_NR_setgid
:
11433 return get_errno(sys_setgid(low2highgid(arg1
)));
11434 case TARGET_NR_setfsuid
:
11435 return get_errno(setfsuid(arg1
));
11436 case TARGET_NR_setfsgid
:
11437 return get_errno(setfsgid(arg1
));
11439 #ifdef TARGET_NR_lchown32
11440 case TARGET_NR_lchown32
:
11441 if (!(p
= lock_user_string(arg1
)))
11442 return -TARGET_EFAULT
;
11443 ret
= get_errno(lchown(p
, arg2
, arg3
));
11444 unlock_user(p
, arg1
, 0);
11447 #ifdef TARGET_NR_getuid32
11448 case TARGET_NR_getuid32
:
11449 return get_errno(getuid());
11452 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11453 /* Alpha specific */
11454 case TARGET_NR_getxuid
:
11458 cpu_env
->ir
[IR_A4
]=euid
;
11460 return get_errno(getuid());
11462 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11463 /* Alpha specific */
11464 case TARGET_NR_getxgid
:
11468 cpu_env
->ir
[IR_A4
]=egid
;
11470 return get_errno(getgid());
11472 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11473 /* Alpha specific */
11474 case TARGET_NR_osf_getsysinfo
:
11475 ret
= -TARGET_EOPNOTSUPP
;
11477 case TARGET_GSI_IEEE_FP_CONTROL
:
11479 uint64_t fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11480 uint64_t swcr
= cpu_env
->swcr
;
11482 swcr
&= ~SWCR_STATUS_MASK
;
11483 swcr
|= (fpcr
>> 35) & SWCR_STATUS_MASK
;
11485 if (put_user_u64 (swcr
, arg2
))
11486 return -TARGET_EFAULT
;
11491 /* case GSI_IEEE_STATE_AT_SIGNAL:
11492 -- Not implemented in linux kernel.
11494 -- Retrieves current unaligned access state; not much used.
11495 case GSI_PROC_TYPE:
11496 -- Retrieves implver information; surely not used.
11497 case GSI_GET_HWRPB:
11498 -- Grabs a copy of the HWRPB; surely not used.
11503 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11504 /* Alpha specific */
11505 case TARGET_NR_osf_setsysinfo
:
11506 ret
= -TARGET_EOPNOTSUPP
;
11508 case TARGET_SSI_IEEE_FP_CONTROL
:
11510 uint64_t swcr
, fpcr
;
11512 if (get_user_u64 (swcr
, arg2
)) {
11513 return -TARGET_EFAULT
;
11517 * The kernel calls swcr_update_status to update the
11518 * status bits from the fpcr at every point that it
11519 * could be queried. Therefore, we store the status
11520 * bits only in FPCR.
11522 cpu_env
->swcr
= swcr
& (SWCR_TRAP_ENABLE_MASK
| SWCR_MAP_MASK
);
11524 fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11525 fpcr
&= ((uint64_t)FPCR_DYN_MASK
<< 32);
11526 fpcr
|= alpha_ieee_swcr_to_fpcr(swcr
);
11527 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11532 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
11534 uint64_t exc
, fpcr
, fex
;
11536 if (get_user_u64(exc
, arg2
)) {
11537 return -TARGET_EFAULT
;
11539 exc
&= SWCR_STATUS_MASK
;
11540 fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11542 /* Old exceptions are not signaled. */
11543 fex
= alpha_ieee_fpcr_to_swcr(fpcr
);
11545 fex
>>= SWCR_STATUS_TO_EXCSUM_SHIFT
;
11546 fex
&= (cpu_env
)->swcr
;
11548 /* Update the hardware fpcr. */
11549 fpcr
|= alpha_ieee_swcr_to_fpcr(exc
);
11550 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11553 int si_code
= TARGET_FPE_FLTUNK
;
11554 target_siginfo_t info
;
11556 if (fex
& SWCR_TRAP_ENABLE_DNO
) {
11557 si_code
= TARGET_FPE_FLTUND
;
11559 if (fex
& SWCR_TRAP_ENABLE_INE
) {
11560 si_code
= TARGET_FPE_FLTRES
;
11562 if (fex
& SWCR_TRAP_ENABLE_UNF
) {
11563 si_code
= TARGET_FPE_FLTUND
;
11565 if (fex
& SWCR_TRAP_ENABLE_OVF
) {
11566 si_code
= TARGET_FPE_FLTOVF
;
11568 if (fex
& SWCR_TRAP_ENABLE_DZE
) {
11569 si_code
= TARGET_FPE_FLTDIV
;
11571 if (fex
& SWCR_TRAP_ENABLE_INV
) {
11572 si_code
= TARGET_FPE_FLTINV
;
11575 info
.si_signo
= SIGFPE
;
11577 info
.si_code
= si_code
;
11578 info
._sifields
._sigfault
._addr
= (cpu_env
)->pc
;
11579 queue_signal(cpu_env
, info
.si_signo
,
11580 QEMU_SI_FAULT
, &info
);
11586 /* case SSI_NVPAIRS:
11587 -- Used with SSIN_UACPROC to enable unaligned accesses.
11588 case SSI_IEEE_STATE_AT_SIGNAL:
11589 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
11590 -- Not implemented in linux kernel
11595 #ifdef TARGET_NR_osf_sigprocmask
11596 /* Alpha specific. */
11597 case TARGET_NR_osf_sigprocmask
:
11601 sigset_t set
, oldset
;
11604 case TARGET_SIG_BLOCK
:
11607 case TARGET_SIG_UNBLOCK
:
11610 case TARGET_SIG_SETMASK
:
11614 return -TARGET_EINVAL
;
11617 target_to_host_old_sigset(&set
, &mask
);
11618 ret
= do_sigprocmask(how
, &set
, &oldset
);
11620 host_to_target_old_sigset(&mask
, &oldset
);
11627 #ifdef TARGET_NR_getgid32
11628 case TARGET_NR_getgid32
:
11629 return get_errno(getgid());
11631 #ifdef TARGET_NR_geteuid32
11632 case TARGET_NR_geteuid32
:
11633 return get_errno(geteuid());
11635 #ifdef TARGET_NR_getegid32
11636 case TARGET_NR_getegid32
:
11637 return get_errno(getegid());
11639 #ifdef TARGET_NR_setreuid32
11640 case TARGET_NR_setreuid32
:
11641 return get_errno(setreuid(arg1
, arg2
));
11643 #ifdef TARGET_NR_setregid32
11644 case TARGET_NR_setregid32
:
11645 return get_errno(setregid(arg1
, arg2
));
11647 #ifdef TARGET_NR_getgroups32
11648 case TARGET_NR_getgroups32
:
11650 int gidsetsize
= arg1
;
11651 uint32_t *target_grouplist
;
11655 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11656 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
11657 if (gidsetsize
== 0)
11659 if (!is_error(ret
)) {
11660 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
, gidsetsize
* 4, 0);
11661 if (!target_grouplist
) {
11662 return -TARGET_EFAULT
;
11664 for(i
= 0;i
< ret
; i
++)
11665 target_grouplist
[i
] = tswap32(grouplist
[i
]);
11666 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
11671 #ifdef TARGET_NR_setgroups32
11672 case TARGET_NR_setgroups32
:
11674 int gidsetsize
= arg1
;
11675 uint32_t *target_grouplist
;
11679 grouplist
= alloca(gidsetsize
* sizeof(gid_t
));
11680 target_grouplist
= lock_user(VERIFY_READ
, arg2
, gidsetsize
* 4, 1);
11681 if (!target_grouplist
) {
11682 return -TARGET_EFAULT
;
11684 for(i
= 0;i
< gidsetsize
; i
++)
11685 grouplist
[i
] = tswap32(target_grouplist
[i
]);
11686 unlock_user(target_grouplist
, arg2
, 0);
11687 return get_errno(setgroups(gidsetsize
, grouplist
));
11690 #ifdef TARGET_NR_fchown32
11691 case TARGET_NR_fchown32
:
11692 return get_errno(fchown(arg1
, arg2
, arg3
));
11694 #ifdef TARGET_NR_setresuid32
11695 case TARGET_NR_setresuid32
:
11696 return get_errno(sys_setresuid(arg1
, arg2
, arg3
));
11698 #ifdef TARGET_NR_getresuid32
11699 case TARGET_NR_getresuid32
:
11701 uid_t ruid
, euid
, suid
;
11702 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11703 if (!is_error(ret
)) {
11704 if (put_user_u32(ruid
, arg1
)
11705 || put_user_u32(euid
, arg2
)
11706 || put_user_u32(suid
, arg3
))
11707 return -TARGET_EFAULT
;
11712 #ifdef TARGET_NR_setresgid32
11713 case TARGET_NR_setresgid32
:
11714 return get_errno(sys_setresgid(arg1
, arg2
, arg3
));
11716 #ifdef TARGET_NR_getresgid32
11717 case TARGET_NR_getresgid32
:
11719 gid_t rgid
, egid
, sgid
;
11720 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11721 if (!is_error(ret
)) {
11722 if (put_user_u32(rgid
, arg1
)
11723 || put_user_u32(egid
, arg2
)
11724 || put_user_u32(sgid
, arg3
))
11725 return -TARGET_EFAULT
;
11730 #ifdef TARGET_NR_chown32
11731 case TARGET_NR_chown32
:
11732 if (!(p
= lock_user_string(arg1
)))
11733 return -TARGET_EFAULT
;
11734 ret
= get_errno(chown(p
, arg2
, arg3
));
11735 unlock_user(p
, arg1
, 0);
11738 #ifdef TARGET_NR_setuid32
11739 case TARGET_NR_setuid32
:
11740 return get_errno(sys_setuid(arg1
));
11742 #ifdef TARGET_NR_setgid32
11743 case TARGET_NR_setgid32
:
11744 return get_errno(sys_setgid(arg1
));
11746 #ifdef TARGET_NR_setfsuid32
11747 case TARGET_NR_setfsuid32
:
11748 return get_errno(setfsuid(arg1
));
11750 #ifdef TARGET_NR_setfsgid32
11751 case TARGET_NR_setfsgid32
:
11752 return get_errno(setfsgid(arg1
));
11754 #ifdef TARGET_NR_mincore
11755 case TARGET_NR_mincore
:
11757 void *a
= lock_user(VERIFY_READ
, arg1
, arg2
, 0);
11759 return -TARGET_ENOMEM
;
11761 p
= lock_user_string(arg3
);
11763 ret
= -TARGET_EFAULT
;
11765 ret
= get_errno(mincore(a
, arg2
, p
));
11766 unlock_user(p
, arg3
, ret
);
11768 unlock_user(a
, arg1
, 0);
11772 #ifdef TARGET_NR_arm_fadvise64_64
11773 case TARGET_NR_arm_fadvise64_64
:
11774 /* arm_fadvise64_64 looks like fadvise64_64 but
11775 * with different argument order: fd, advice, offset, len
11776 * rather than the usual fd, offset, len, advice.
11777 * Note that offset and len are both 64-bit so appear as
11778 * pairs of 32-bit registers.
11780 ret
= posix_fadvise(arg1
, target_offset64(arg3
, arg4
),
11781 target_offset64(arg5
, arg6
), arg2
);
11782 return -host_to_target_errno(ret
);
11785 #if TARGET_ABI_BITS == 32
11787 #ifdef TARGET_NR_fadvise64_64
11788 case TARGET_NR_fadvise64_64
:
11789 #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
11790 /* 6 args: fd, advice, offset (high, low), len (high, low) */
11798 /* 6 args: fd, offset (high, low), len (high, low), advice */
11799 if (regpairs_aligned(cpu_env
, num
)) {
11800 /* offset is in (3,4), len in (5,6) and advice in 7 */
11808 ret
= posix_fadvise(arg1
, target_offset64(arg2
, arg3
),
11809 target_offset64(arg4
, arg5
), arg6
);
11810 return -host_to_target_errno(ret
);
11813 #ifdef TARGET_NR_fadvise64
11814 case TARGET_NR_fadvise64
:
11815 /* 5 args: fd, offset (high, low), len, advice */
11816 if (regpairs_aligned(cpu_env
, num
)) {
11817 /* offset is in (3,4), len in 5 and advice in 6 */
11823 ret
= posix_fadvise(arg1
, target_offset64(arg2
, arg3
), arg4
, arg5
);
11824 return -host_to_target_errno(ret
);
11827 #else /* not a 32-bit ABI */
11828 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
11829 #ifdef TARGET_NR_fadvise64_64
11830 case TARGET_NR_fadvise64_64
:
11832 #ifdef TARGET_NR_fadvise64
11833 case TARGET_NR_fadvise64
:
11835 #ifdef TARGET_S390X
11837 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
11838 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
11839 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
11840 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
11844 return -host_to_target_errno(posix_fadvise(arg1
, arg2
, arg3
, arg4
));
11846 #endif /* end of 64-bit ABI fadvise handling */
11848 #ifdef TARGET_NR_madvise
11849 case TARGET_NR_madvise
:
11850 return target_madvise(arg1
, arg2
, arg3
);
11852 #ifdef TARGET_NR_fcntl64
11853 case TARGET_NR_fcntl64
:
11857 from_flock64_fn
*copyfrom
= copy_from_user_flock64
;
11858 to_flock64_fn
*copyto
= copy_to_user_flock64
;
11861 if (!cpu_env
->eabi
) {
11862 copyfrom
= copy_from_user_oabi_flock64
;
11863 copyto
= copy_to_user_oabi_flock64
;
11867 cmd
= target_to_host_fcntl_cmd(arg2
);
11868 if (cmd
== -TARGET_EINVAL
) {
11873 case TARGET_F_GETLK64
:
11874 ret
= copyfrom(&fl
, arg3
);
11878 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
11880 ret
= copyto(arg3
, &fl
);
11884 case TARGET_F_SETLK64
:
11885 case TARGET_F_SETLKW64
:
11886 ret
= copyfrom(&fl
, arg3
);
11890 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
11893 ret
= do_fcntl(arg1
, arg2
, arg3
);
11899 #ifdef TARGET_NR_cacheflush
11900 case TARGET_NR_cacheflush
:
11901 /* self-modifying code is handled automatically, so nothing needed */
11904 #ifdef TARGET_NR_getpagesize
11905 case TARGET_NR_getpagesize
:
11906 return TARGET_PAGE_SIZE
;
11908 case TARGET_NR_gettid
:
11909 return get_errno(sys_gettid());
11910 #ifdef TARGET_NR_readahead
11911 case TARGET_NR_readahead
:
11912 #if TARGET_ABI_BITS == 32
11913 if (regpairs_aligned(cpu_env
, num
)) {
11918 ret
= get_errno(readahead(arg1
, target_offset64(arg2
, arg3
) , arg4
));
11920 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
11925 #ifdef TARGET_NR_setxattr
11926 case TARGET_NR_listxattr
:
11927 case TARGET_NR_llistxattr
:
11931 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11933 return -TARGET_EFAULT
;
11936 p
= lock_user_string(arg1
);
11938 if (num
== TARGET_NR_listxattr
) {
11939 ret
= get_errno(listxattr(p
, b
, arg3
));
11941 ret
= get_errno(llistxattr(p
, b
, arg3
));
11944 ret
= -TARGET_EFAULT
;
11946 unlock_user(p
, arg1
, 0);
11947 unlock_user(b
, arg2
, arg3
);
11950 case TARGET_NR_flistxattr
:
11954 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11956 return -TARGET_EFAULT
;
11959 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
11960 unlock_user(b
, arg2
, arg3
);
11963 case TARGET_NR_setxattr
:
11964 case TARGET_NR_lsetxattr
:
11966 void *p
, *n
, *v
= 0;
11968 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
11970 return -TARGET_EFAULT
;
11973 p
= lock_user_string(arg1
);
11974 n
= lock_user_string(arg2
);
11976 if (num
== TARGET_NR_setxattr
) {
11977 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
11979 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
11982 ret
= -TARGET_EFAULT
;
11984 unlock_user(p
, arg1
, 0);
11985 unlock_user(n
, arg2
, 0);
11986 unlock_user(v
, arg3
, 0);
11989 case TARGET_NR_fsetxattr
:
11993 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
11995 return -TARGET_EFAULT
;
11998 n
= lock_user_string(arg2
);
12000 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
12002 ret
= -TARGET_EFAULT
;
12004 unlock_user(n
, arg2
, 0);
12005 unlock_user(v
, arg3
, 0);
12008 case TARGET_NR_getxattr
:
12009 case TARGET_NR_lgetxattr
:
12011 void *p
, *n
, *v
= 0;
12013 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
12015 return -TARGET_EFAULT
;
12018 p
= lock_user_string(arg1
);
12019 n
= lock_user_string(arg2
);
12021 if (num
== TARGET_NR_getxattr
) {
12022 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
12024 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
12027 ret
= -TARGET_EFAULT
;
12029 unlock_user(p
, arg1
, 0);
12030 unlock_user(n
, arg2
, 0);
12031 unlock_user(v
, arg3
, arg4
);
12034 case TARGET_NR_fgetxattr
:
12038 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
12040 return -TARGET_EFAULT
;
12043 n
= lock_user_string(arg2
);
12045 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
12047 ret
= -TARGET_EFAULT
;
12049 unlock_user(n
, arg2
, 0);
12050 unlock_user(v
, arg3
, arg4
);
12053 case TARGET_NR_removexattr
:
12054 case TARGET_NR_lremovexattr
:
12057 p
= lock_user_string(arg1
);
12058 n
= lock_user_string(arg2
);
12060 if (num
== TARGET_NR_removexattr
) {
12061 ret
= get_errno(removexattr(p
, n
));
12063 ret
= get_errno(lremovexattr(p
, n
));
12066 ret
= -TARGET_EFAULT
;
12068 unlock_user(p
, arg1
, 0);
12069 unlock_user(n
, arg2
, 0);
12072 case TARGET_NR_fremovexattr
:
12075 n
= lock_user_string(arg2
);
12077 ret
= get_errno(fremovexattr(arg1
, n
));
12079 ret
= -TARGET_EFAULT
;
12081 unlock_user(n
, arg2
, 0);
12085 #endif /* CONFIG_ATTR */
12086 #ifdef TARGET_NR_set_thread_area
12087 case TARGET_NR_set_thread_area
:
12088 #if defined(TARGET_MIPS)
12089 cpu_env
->active_tc
.CP0_UserLocal
= arg1
;
12091 #elif defined(TARGET_CRIS)
12093 ret
= -TARGET_EINVAL
;
12095 cpu_env
->pregs
[PR_PID
] = arg1
;
12099 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
12100 return do_set_thread_area(cpu_env
, arg1
);
12101 #elif defined(TARGET_M68K)
12103 TaskState
*ts
= cpu
->opaque
;
12104 ts
->tp_value
= arg1
;
12108 return -TARGET_ENOSYS
;
12111 #ifdef TARGET_NR_get_thread_area
12112 case TARGET_NR_get_thread_area
:
12113 #if defined(TARGET_I386) && defined(TARGET_ABI32)
12114 return do_get_thread_area(cpu_env
, arg1
);
12115 #elif defined(TARGET_M68K)
12117 TaskState
*ts
= cpu
->opaque
;
12118 return ts
->tp_value
;
12121 return -TARGET_ENOSYS
;
12124 #ifdef TARGET_NR_getdomainname
12125 case TARGET_NR_getdomainname
:
12126 return -TARGET_ENOSYS
;
12129 #ifdef TARGET_NR_clock_settime
12130 case TARGET_NR_clock_settime
:
12132 struct timespec ts
;
12134 ret
= target_to_host_timespec(&ts
, arg2
);
12135 if (!is_error(ret
)) {
12136 ret
= get_errno(clock_settime(arg1
, &ts
));
12141 #ifdef TARGET_NR_clock_settime64
12142 case TARGET_NR_clock_settime64
:
12144 struct timespec ts
;
12146 ret
= target_to_host_timespec64(&ts
, arg2
);
12147 if (!is_error(ret
)) {
12148 ret
= get_errno(clock_settime(arg1
, &ts
));
12153 #ifdef TARGET_NR_clock_gettime
12154 case TARGET_NR_clock_gettime
:
12156 struct timespec ts
;
12157 ret
= get_errno(clock_gettime(arg1
, &ts
));
12158 if (!is_error(ret
)) {
12159 ret
= host_to_target_timespec(arg2
, &ts
);
12164 #ifdef TARGET_NR_clock_gettime64
12165 case TARGET_NR_clock_gettime64
:
12167 struct timespec ts
;
12168 ret
= get_errno(clock_gettime(arg1
, &ts
));
12169 if (!is_error(ret
)) {
12170 ret
= host_to_target_timespec64(arg2
, &ts
);
12175 #ifdef TARGET_NR_clock_getres
12176 case TARGET_NR_clock_getres
:
12178 struct timespec ts
;
12179 ret
= get_errno(clock_getres(arg1
, &ts
));
12180 if (!is_error(ret
)) {
12181 host_to_target_timespec(arg2
, &ts
);
12186 #ifdef TARGET_NR_clock_getres_time64
12187 case TARGET_NR_clock_getres_time64
:
12189 struct timespec ts
;
12190 ret
= get_errno(clock_getres(arg1
, &ts
));
12191 if (!is_error(ret
)) {
12192 host_to_target_timespec64(arg2
, &ts
);
12197 #ifdef TARGET_NR_clock_nanosleep
12198 case TARGET_NR_clock_nanosleep
:
12200 struct timespec ts
;
12201 if (target_to_host_timespec(&ts
, arg3
)) {
12202 return -TARGET_EFAULT
;
12204 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
12205 &ts
, arg4
? &ts
: NULL
));
12207 * if the call is interrupted by a signal handler, it fails
12208 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
12209 * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
12211 if (ret
== -TARGET_EINTR
&& arg4
&& arg2
!= TIMER_ABSTIME
&&
12212 host_to_target_timespec(arg4
, &ts
)) {
12213 return -TARGET_EFAULT
;
12219 #ifdef TARGET_NR_clock_nanosleep_time64
12220 case TARGET_NR_clock_nanosleep_time64
:
12222 struct timespec ts
;
12224 if (target_to_host_timespec64(&ts
, arg3
)) {
12225 return -TARGET_EFAULT
;
12228 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
12229 &ts
, arg4
? &ts
: NULL
));
12231 if (ret
== -TARGET_EINTR
&& arg4
&& arg2
!= TIMER_ABSTIME
&&
12232 host_to_target_timespec64(arg4
, &ts
)) {
12233 return -TARGET_EFAULT
;
12239 #if defined(TARGET_NR_set_tid_address)
12240 case TARGET_NR_set_tid_address
:
12242 TaskState
*ts
= cpu
->opaque
;
12243 ts
->child_tidptr
= arg1
;
12244 /* do not call host set_tid_address() syscall, instead return tid() */
12245 return get_errno(sys_gettid());
12249 case TARGET_NR_tkill
:
12250 return get_errno(safe_tkill((int)arg1
, target_to_host_signal(arg2
)));
12252 case TARGET_NR_tgkill
:
12253 return get_errno(safe_tgkill((int)arg1
, (int)arg2
,
12254 target_to_host_signal(arg3
)));
12256 #ifdef TARGET_NR_set_robust_list
12257 case TARGET_NR_set_robust_list
:
12258 case TARGET_NR_get_robust_list
:
12259 /* The ABI for supporting robust futexes has userspace pass
12260 * the kernel a pointer to a linked list which is updated by
12261 * userspace after the syscall; the list is walked by the kernel
12262 * when the thread exits. Since the linked list in QEMU guest
12263 * memory isn't a valid linked list for the host and we have
12264 * no way to reliably intercept the thread-death event, we can't
12265 * support these. Silently return ENOSYS so that guest userspace
12266 * falls back to a non-robust futex implementation (which should
12267 * be OK except in the corner case of the guest crashing while
12268 * holding a mutex that is shared with another process via
12271 return -TARGET_ENOSYS
;
12274 #if defined(TARGET_NR_utimensat)
12275 case TARGET_NR_utimensat
:
12277 struct timespec
*tsp
, ts
[2];
12281 if (target_to_host_timespec(ts
, arg3
)) {
12282 return -TARGET_EFAULT
;
12284 if (target_to_host_timespec(ts
+ 1, arg3
+
12285 sizeof(struct target_timespec
))) {
12286 return -TARGET_EFAULT
;
12291 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
12293 if (!(p
= lock_user_string(arg2
))) {
12294 return -TARGET_EFAULT
;
12296 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
12297 unlock_user(p
, arg2
, 0);
12302 #ifdef TARGET_NR_utimensat_time64
12303 case TARGET_NR_utimensat_time64
:
12305 struct timespec
*tsp
, ts
[2];
12309 if (target_to_host_timespec64(ts
, arg3
)) {
12310 return -TARGET_EFAULT
;
12312 if (target_to_host_timespec64(ts
+ 1, arg3
+
12313 sizeof(struct target__kernel_timespec
))) {
12314 return -TARGET_EFAULT
;
12319 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
12321 p
= lock_user_string(arg2
);
12323 return -TARGET_EFAULT
;
12325 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
12326 unlock_user(p
, arg2
, 0);
12331 #ifdef TARGET_NR_futex
12332 case TARGET_NR_futex
:
12333 return do_futex(cpu
, false, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
12335 #ifdef TARGET_NR_futex_time64
12336 case TARGET_NR_futex_time64
:
12337 return do_futex(cpu
, true, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
12339 #ifdef CONFIG_INOTIFY
12340 #if defined(TARGET_NR_inotify_init)
12341 case TARGET_NR_inotify_init
:
12342 ret
= get_errno(inotify_init());
12344 fd_trans_register(ret
, &target_inotify_trans
);
12348 #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
12349 case TARGET_NR_inotify_init1
:
12350 ret
= get_errno(inotify_init1(target_to_host_bitmask(arg1
,
12351 fcntl_flags_tbl
)));
12353 fd_trans_register(ret
, &target_inotify_trans
);
12357 #if defined(TARGET_NR_inotify_add_watch)
12358 case TARGET_NR_inotify_add_watch
:
12359 p
= lock_user_string(arg2
);
12360 ret
= get_errno(inotify_add_watch(arg1
, path(p
), arg3
));
12361 unlock_user(p
, arg2
, 0);
12364 #if defined(TARGET_NR_inotify_rm_watch)
12365 case TARGET_NR_inotify_rm_watch
:
12366 return get_errno(inotify_rm_watch(arg1
, arg2
));
12370 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
12371 case TARGET_NR_mq_open
:
12373 struct mq_attr posix_mq_attr
;
12374 struct mq_attr
*pposix_mq_attr
;
12377 host_flags
= target_to_host_bitmask(arg2
, fcntl_flags_tbl
);
12378 pposix_mq_attr
= NULL
;
12380 if (copy_from_user_mq_attr(&posix_mq_attr
, arg4
) != 0) {
12381 return -TARGET_EFAULT
;
12383 pposix_mq_attr
= &posix_mq_attr
;
12385 p
= lock_user_string(arg1
- 1);
12387 return -TARGET_EFAULT
;
12389 ret
= get_errno(mq_open(p
, host_flags
, arg3
, pposix_mq_attr
));
12390 unlock_user (p
, arg1
, 0);
12394 case TARGET_NR_mq_unlink
:
12395 p
= lock_user_string(arg1
- 1);
12397 return -TARGET_EFAULT
;
12399 ret
= get_errno(mq_unlink(p
));
12400 unlock_user (p
, arg1
, 0);
12403 #ifdef TARGET_NR_mq_timedsend
12404 case TARGET_NR_mq_timedsend
:
12406 struct timespec ts
;
12408 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12410 if (target_to_host_timespec(&ts
, arg5
)) {
12411 return -TARGET_EFAULT
;
12413 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
12414 if (!is_error(ret
) && host_to_target_timespec(arg5
, &ts
)) {
12415 return -TARGET_EFAULT
;
12418 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
12420 unlock_user (p
, arg2
, arg3
);
12424 #ifdef TARGET_NR_mq_timedsend_time64
12425 case TARGET_NR_mq_timedsend_time64
:
12427 struct timespec ts
;
12429 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
12431 if (target_to_host_timespec64(&ts
, arg5
)) {
12432 return -TARGET_EFAULT
;
12434 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
12435 if (!is_error(ret
) && host_to_target_timespec64(arg5
, &ts
)) {
12436 return -TARGET_EFAULT
;
12439 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
12441 unlock_user(p
, arg2
, arg3
);
12446 #ifdef TARGET_NR_mq_timedreceive
12447 case TARGET_NR_mq_timedreceive
:
12449 struct timespec ts
;
12452 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12454 if (target_to_host_timespec(&ts
, arg5
)) {
12455 return -TARGET_EFAULT
;
12457 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12459 if (!is_error(ret
) && host_to_target_timespec(arg5
, &ts
)) {
12460 return -TARGET_EFAULT
;
12463 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12466 unlock_user (p
, arg2
, arg3
);
12468 put_user_u32(prio
, arg4
);
12472 #ifdef TARGET_NR_mq_timedreceive_time64
12473 case TARGET_NR_mq_timedreceive_time64
:
12475 struct timespec ts
;
12478 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
12480 if (target_to_host_timespec64(&ts
, arg5
)) {
12481 return -TARGET_EFAULT
;
12483 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12485 if (!is_error(ret
) && host_to_target_timespec64(arg5
, &ts
)) {
12486 return -TARGET_EFAULT
;
12489 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12492 unlock_user(p
, arg2
, arg3
);
12494 put_user_u32(prio
, arg4
);
12500 /* Not implemented for now... */
12501 /* case TARGET_NR_mq_notify: */
12504 case TARGET_NR_mq_getsetattr
:
12506 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
12509 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
12510 ret
= get_errno(mq_setattr(arg1
, &posix_mq_attr_in
,
12511 &posix_mq_attr_out
));
12512 } else if (arg3
!= 0) {
12513 ret
= get_errno(mq_getattr(arg1
, &posix_mq_attr_out
));
12515 if (ret
== 0 && arg3
!= 0) {
12516 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
12522 #ifdef CONFIG_SPLICE
12523 #ifdef TARGET_NR_tee
12524 case TARGET_NR_tee
:
12526 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
12530 #ifdef TARGET_NR_splice
12531 case TARGET_NR_splice
:
12533 loff_t loff_in
, loff_out
;
12534 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
12536 if (get_user_u64(loff_in
, arg2
)) {
12537 return -TARGET_EFAULT
;
12539 ploff_in
= &loff_in
;
12542 if (get_user_u64(loff_out
, arg4
)) {
12543 return -TARGET_EFAULT
;
12545 ploff_out
= &loff_out
;
12547 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
12549 if (put_user_u64(loff_in
, arg2
)) {
12550 return -TARGET_EFAULT
;
12554 if (put_user_u64(loff_out
, arg4
)) {
12555 return -TARGET_EFAULT
;
12561 #ifdef TARGET_NR_vmsplice
12562 case TARGET_NR_vmsplice
:
12564 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
12566 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
12567 unlock_iovec(vec
, arg2
, arg3
, 0);
12569 ret
= -host_to_target_errno(errno
);
12574 #endif /* CONFIG_SPLICE */
12575 #ifdef CONFIG_EVENTFD
12576 #if defined(TARGET_NR_eventfd)
12577 case TARGET_NR_eventfd
:
12578 ret
= get_errno(eventfd(arg1
, 0));
12580 fd_trans_register(ret
, &target_eventfd_trans
);
12584 #if defined(TARGET_NR_eventfd2)
12585 case TARGET_NR_eventfd2
:
12587 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK_MASK
| TARGET_O_CLOEXEC
));
12588 if (arg2
& TARGET_O_NONBLOCK
) {
12589 host_flags
|= O_NONBLOCK
;
12591 if (arg2
& TARGET_O_CLOEXEC
) {
12592 host_flags
|= O_CLOEXEC
;
12594 ret
= get_errno(eventfd(arg1
, host_flags
));
12596 fd_trans_register(ret
, &target_eventfd_trans
);
12601 #endif /* CONFIG_EVENTFD */
12602 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
12603 case TARGET_NR_fallocate
:
12604 #if TARGET_ABI_BITS == 32
12605 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
12606 target_offset64(arg5
, arg6
)));
12608 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
12612 #if defined(CONFIG_SYNC_FILE_RANGE)
12613 #if defined(TARGET_NR_sync_file_range)
12614 case TARGET_NR_sync_file_range
:
12615 #if TARGET_ABI_BITS == 32
12616 #if defined(TARGET_MIPS)
12617 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
12618 target_offset64(arg5
, arg6
), arg7
));
12620 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
12621 target_offset64(arg4
, arg5
), arg6
));
12622 #endif /* !TARGET_MIPS */
12624 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
12628 #if defined(TARGET_NR_sync_file_range2) || \
12629 defined(TARGET_NR_arm_sync_file_range)
12630 #if defined(TARGET_NR_sync_file_range2)
12631 case TARGET_NR_sync_file_range2
:
12633 #if defined(TARGET_NR_arm_sync_file_range)
12634 case TARGET_NR_arm_sync_file_range
:
12636 /* This is like sync_file_range but the arguments are reordered */
12637 #if TARGET_ABI_BITS == 32
12638 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
12639 target_offset64(arg5
, arg6
), arg2
));
12641 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
12646 #if defined(TARGET_NR_signalfd4)
12647 case TARGET_NR_signalfd4
:
12648 return do_signalfd4(arg1
, arg2
, arg4
);
12650 #if defined(TARGET_NR_signalfd)
12651 case TARGET_NR_signalfd
:
12652 return do_signalfd4(arg1
, arg2
, 0);
12654 #if defined(CONFIG_EPOLL)
12655 #if defined(TARGET_NR_epoll_create)
12656 case TARGET_NR_epoll_create
:
12657 return get_errno(epoll_create(arg1
));
12659 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
12660 case TARGET_NR_epoll_create1
:
12661 return get_errno(epoll_create1(target_to_host_bitmask(arg1
, fcntl_flags_tbl
)));
12663 #if defined(TARGET_NR_epoll_ctl)
12664 case TARGET_NR_epoll_ctl
:
12666 struct epoll_event ep
;
12667 struct epoll_event
*epp
= 0;
12669 if (arg2
!= EPOLL_CTL_DEL
) {
12670 struct target_epoll_event
*target_ep
;
12671 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
12672 return -TARGET_EFAULT
;
12674 ep
.events
= tswap32(target_ep
->events
);
12676 * The epoll_data_t union is just opaque data to the kernel,
12677 * so we transfer all 64 bits across and need not worry what
12678 * actual data type it is.
12680 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
12681 unlock_user_struct(target_ep
, arg4
, 0);
12684 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
12685 * non-null pointer, even though this argument is ignored.
12690 return get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
12694 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
12695 #if defined(TARGET_NR_epoll_wait)
12696 case TARGET_NR_epoll_wait
:
12698 #if defined(TARGET_NR_epoll_pwait)
12699 case TARGET_NR_epoll_pwait
:
12702 struct target_epoll_event
*target_ep
;
12703 struct epoll_event
*ep
;
12705 int maxevents
= arg3
;
12706 int timeout
= arg4
;
12708 if (maxevents
<= 0 || maxevents
> TARGET_EP_MAX_EVENTS
) {
12709 return -TARGET_EINVAL
;
12712 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
12713 maxevents
* sizeof(struct target_epoll_event
), 1);
12715 return -TARGET_EFAULT
;
12718 ep
= g_try_new(struct epoll_event
, maxevents
);
12720 unlock_user(target_ep
, arg2
, 0);
12721 return -TARGET_ENOMEM
;
12725 #if defined(TARGET_NR_epoll_pwait)
12726 case TARGET_NR_epoll_pwait
:
12728 sigset_t
*set
= NULL
;
12731 ret
= process_sigsuspend_mask(&set
, arg5
, arg6
);
12737 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
12738 set
, SIGSET_T_SIZE
));
12741 finish_sigsuspend_mask(ret
);
12746 #if defined(TARGET_NR_epoll_wait)
12747 case TARGET_NR_epoll_wait
:
12748 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
12753 ret
= -TARGET_ENOSYS
;
12755 if (!is_error(ret
)) {
12757 for (i
= 0; i
< ret
; i
++) {
12758 target_ep
[i
].events
= tswap32(ep
[i
].events
);
12759 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
12761 unlock_user(target_ep
, arg2
,
12762 ret
* sizeof(struct target_epoll_event
));
12764 unlock_user(target_ep
, arg2
, 0);
12771 #ifdef TARGET_NR_prlimit64
12772 case TARGET_NR_prlimit64
:
12774 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
12775 struct target_rlimit64
*target_rnew
, *target_rold
;
12776 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
12777 int resource
= target_to_host_resource(arg2
);
12779 if (arg3
&& (resource
!= RLIMIT_AS
&&
12780 resource
!= RLIMIT_DATA
&&
12781 resource
!= RLIMIT_STACK
)) {
12782 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
12783 return -TARGET_EFAULT
;
12785 rnew
.rlim_cur
= tswap64(target_rnew
->rlim_cur
);
12786 rnew
.rlim_max
= tswap64(target_rnew
->rlim_max
);
12787 unlock_user_struct(target_rnew
, arg3
, 0);
12791 ret
= get_errno(sys_prlimit64(arg1
, resource
, rnewp
, arg4
? &rold
: 0));
12792 if (!is_error(ret
) && arg4
) {
12793 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
12794 return -TARGET_EFAULT
;
12796 target_rold
->rlim_cur
= tswap64(rold
.rlim_cur
);
12797 target_rold
->rlim_max
= tswap64(rold
.rlim_max
);
12798 unlock_user_struct(target_rold
, arg4
, 1);
12803 #ifdef TARGET_NR_gethostname
12804 case TARGET_NR_gethostname
:
12806 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
12808 ret
= get_errno(gethostname(name
, arg2
));
12809 unlock_user(name
, arg1
, arg2
);
12811 ret
= -TARGET_EFAULT
;
12816 #ifdef TARGET_NR_atomic_cmpxchg_32
12817 case TARGET_NR_atomic_cmpxchg_32
:
12819 /* should use start_exclusive from main.c */
12820 abi_ulong mem_value
;
12821 if (get_user_u32(mem_value
, arg6
)) {
12822 target_siginfo_t info
;
12823 info
.si_signo
= SIGSEGV
;
12825 info
.si_code
= TARGET_SEGV_MAPERR
;
12826 info
._sifields
._sigfault
._addr
= arg6
;
12827 queue_signal(cpu_env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
12831 if (mem_value
== arg2
)
12832 put_user_u32(arg1
, arg6
);
12836 #ifdef TARGET_NR_atomic_barrier
12837 case TARGET_NR_atomic_barrier
:
12838 /* Like the kernel implementation and the
12839 qemu arm barrier, no-op this? */
12843 #ifdef TARGET_NR_timer_create
12844 case TARGET_NR_timer_create
:
12846 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
12848 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
12851 int timer_index
= next_free_host_timer();
12853 if (timer_index
< 0) {
12854 ret
= -TARGET_EAGAIN
;
12856 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
12859 phost_sevp
= &host_sevp
;
12860 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
12862 free_host_timer_slot(timer_index
);
12867 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
12869 free_host_timer_slot(timer_index
);
12871 if (put_user(TIMER_MAGIC
| timer_index
, arg3
, target_timer_t
)) {
12872 timer_delete(*phtimer
);
12873 free_host_timer_slot(timer_index
);
12874 return -TARGET_EFAULT
;
12882 #ifdef TARGET_NR_timer_settime
12883 case TARGET_NR_timer_settime
:
12885 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
12886 * struct itimerspec * old_value */
12887 target_timer_t timerid
= get_timer_id(arg1
);
12891 } else if (arg3
== 0) {
12892 ret
= -TARGET_EINVAL
;
12894 timer_t htimer
= g_posix_timers
[timerid
];
12895 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
12897 if (target_to_host_itimerspec(&hspec_new
, arg3
)) {
12898 return -TARGET_EFAULT
;
12901 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
12902 if (arg4
&& host_to_target_itimerspec(arg4
, &hspec_old
)) {
12903 return -TARGET_EFAULT
;
12910 #ifdef TARGET_NR_timer_settime64
12911 case TARGET_NR_timer_settime64
:
12913 target_timer_t timerid
= get_timer_id(arg1
);
12917 } else if (arg3
== 0) {
12918 ret
= -TARGET_EINVAL
;
12920 timer_t htimer
= g_posix_timers
[timerid
];
12921 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
12923 if (target_to_host_itimerspec64(&hspec_new
, arg3
)) {
12924 return -TARGET_EFAULT
;
12927 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
12928 if (arg4
&& host_to_target_itimerspec64(arg4
, &hspec_old
)) {
12929 return -TARGET_EFAULT
;
12936 #ifdef TARGET_NR_timer_gettime
12937 case TARGET_NR_timer_gettime
:
12939 /* args: timer_t timerid, struct itimerspec *curr_value */
12940 target_timer_t timerid
= get_timer_id(arg1
);
12944 } else if (!arg2
) {
12945 ret
= -TARGET_EFAULT
;
12947 timer_t htimer
= g_posix_timers
[timerid
];
12948 struct itimerspec hspec
;
12949 ret
= get_errno(timer_gettime(htimer
, &hspec
));
12951 if (host_to_target_itimerspec(arg2
, &hspec
)) {
12952 ret
= -TARGET_EFAULT
;
12959 #ifdef TARGET_NR_timer_gettime64
12960 case TARGET_NR_timer_gettime64
:
12962 /* args: timer_t timerid, struct itimerspec64 *curr_value */
12963 target_timer_t timerid
= get_timer_id(arg1
);
12967 } else if (!arg2
) {
12968 ret
= -TARGET_EFAULT
;
12970 timer_t htimer
= g_posix_timers
[timerid
];
12971 struct itimerspec hspec
;
12972 ret
= get_errno(timer_gettime(htimer
, &hspec
));
12974 if (host_to_target_itimerspec64(arg2
, &hspec
)) {
12975 ret
= -TARGET_EFAULT
;
12982 #ifdef TARGET_NR_timer_getoverrun
12983 case TARGET_NR_timer_getoverrun
:
12985 /* args: timer_t timerid */
12986 target_timer_t timerid
= get_timer_id(arg1
);
12991 timer_t htimer
= g_posix_timers
[timerid
];
12992 ret
= get_errno(timer_getoverrun(htimer
));
12998 #ifdef TARGET_NR_timer_delete
12999 case TARGET_NR_timer_delete
:
13001 /* args: timer_t timerid */
13002 target_timer_t timerid
= get_timer_id(arg1
);
13007 timer_t htimer
= g_posix_timers
[timerid
];
13008 ret
= get_errno(timer_delete(htimer
));
13009 free_host_timer_slot(timerid
);
13015 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
13016 case TARGET_NR_timerfd_create
:
13017 return get_errno(timerfd_create(arg1
,
13018 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
13021 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
13022 case TARGET_NR_timerfd_gettime
:
13024 struct itimerspec its_curr
;
13026 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
13028 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
13029 return -TARGET_EFAULT
;
13035 #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
13036 case TARGET_NR_timerfd_gettime64
:
13038 struct itimerspec its_curr
;
13040 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
13042 if (arg2
&& host_to_target_itimerspec64(arg2
, &its_curr
)) {
13043 return -TARGET_EFAULT
;
13049 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
13050 case TARGET_NR_timerfd_settime
:
13052 struct itimerspec its_new
, its_old
, *p_new
;
13055 if (target_to_host_itimerspec(&its_new
, arg3
)) {
13056 return -TARGET_EFAULT
;
13063 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
13065 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
13066 return -TARGET_EFAULT
;
13072 #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
13073 case TARGET_NR_timerfd_settime64
:
13075 struct itimerspec its_new
, its_old
, *p_new
;
13078 if (target_to_host_itimerspec64(&its_new
, arg3
)) {
13079 return -TARGET_EFAULT
;
13086 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
13088 if (arg4
&& host_to_target_itimerspec64(arg4
, &its_old
)) {
13089 return -TARGET_EFAULT
;
13095 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
13096 case TARGET_NR_ioprio_get
:
13097 return get_errno(ioprio_get(arg1
, arg2
));
13100 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
13101 case TARGET_NR_ioprio_set
:
13102 return get_errno(ioprio_set(arg1
, arg2
, arg3
));
13105 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
13106 case TARGET_NR_setns
:
13107 return get_errno(setns(arg1
, arg2
));
13109 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
13110 case TARGET_NR_unshare
:
13111 return get_errno(unshare(arg1
));
13113 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
13114 case TARGET_NR_kcmp
:
13115 return get_errno(kcmp(arg1
, arg2
, arg3
, arg4
, arg5
));
13117 #ifdef TARGET_NR_swapcontext
13118 case TARGET_NR_swapcontext
:
13119 /* PowerPC specific. */
13120 return do_swapcontext(cpu_env
, arg1
, arg2
, arg3
);
13122 #ifdef TARGET_NR_memfd_create
13123 case TARGET_NR_memfd_create
:
13124 p
= lock_user_string(arg1
);
13126 return -TARGET_EFAULT
;
13128 ret
= get_errno(memfd_create(p
, arg2
));
13129 fd_trans_unregister(ret
);
13130 unlock_user(p
, arg1
, 0);
13133 #if defined TARGET_NR_membarrier && defined __NR_membarrier
13134 case TARGET_NR_membarrier
:
13135 return get_errno(membarrier(arg1
, arg2
));
13138 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
13139 case TARGET_NR_copy_file_range
:
13141 loff_t inoff
, outoff
;
13142 loff_t
*pinoff
= NULL
, *poutoff
= NULL
;
13145 if (get_user_u64(inoff
, arg2
)) {
13146 return -TARGET_EFAULT
;
13151 if (get_user_u64(outoff
, arg4
)) {
13152 return -TARGET_EFAULT
;
13156 /* Do not sign-extend the count parameter. */
13157 ret
= get_errno(safe_copy_file_range(arg1
, pinoff
, arg3
, poutoff
,
13158 (abi_ulong
)arg5
, arg6
));
13159 if (!is_error(ret
) && ret
> 0) {
13161 if (put_user_u64(inoff
, arg2
)) {
13162 return -TARGET_EFAULT
;
13166 if (put_user_u64(outoff
, arg4
)) {
13167 return -TARGET_EFAULT
;
13175 #if defined(TARGET_NR_pivot_root)
13176 case TARGET_NR_pivot_root
:
13179 p
= lock_user_string(arg1
); /* new_root */
13180 p2
= lock_user_string(arg2
); /* put_old */
13182 ret
= -TARGET_EFAULT
;
13184 ret
= get_errno(pivot_root(p
, p2
));
13186 unlock_user(p2
, arg2
, 0);
13187 unlock_user(p
, arg1
, 0);
13193 qemu_log_mask(LOG_UNIMP
, "Unsupported syscall: %d\n", num
);
13194 return -TARGET_ENOSYS
;
13199 abi_long
do_syscall(CPUArchState
*cpu_env
, int num
, abi_long arg1
,
13200 abi_long arg2
, abi_long arg3
, abi_long arg4
,
13201 abi_long arg5
, abi_long arg6
, abi_long arg7
,
13204 CPUState
*cpu
= env_cpu(cpu_env
);
13207 #ifdef DEBUG_ERESTARTSYS
13208 /* Debug-only code for exercising the syscall-restart code paths
13209 * in the per-architecture cpu main loops: restart every syscall
13210 * the guest makes once before letting it through.
13216 return -QEMU_ERESTARTSYS
;
13221 record_syscall_start(cpu
, num
, arg1
,
13222 arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
);
13224 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
13225 print_syscall(cpu_env
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
13228 ret
= do_syscall1(cpu_env
, num
, arg1
, arg2
, arg3
, arg4
,
13229 arg5
, arg6
, arg7
, arg8
);
13231 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
13232 print_syscall_ret(cpu_env
, num
, ret
, arg1
, arg2
,
13233 arg3
, arg4
, arg5
, arg6
);
13236 record_syscall_return(cpu
, num
, ret
);