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"
25 #include "qemu/plugin.h"
26 #include "target_mman.h"
33 #include <sys/mount.h>
35 #include <sys/fsuid.h>
36 #include <sys/personality.h>
37 #include <sys/prctl.h>
38 #include <sys/resource.h>
40 #include <linux/capability.h>
42 #include <sys/timex.h>
43 #include <sys/socket.h>
44 #include <linux/sockios.h>
48 #include <sys/times.h>
51 #include <sys/statfs.h>
53 #include <sys/sysinfo.h>
54 #include <sys/signalfd.h>
55 //#include <sys/user.h>
56 #include <netinet/in.h>
57 #include <netinet/ip.h>
58 #include <netinet/tcp.h>
59 #include <netinet/udp.h>
60 #include <linux/wireless.h>
61 #include <linux/icmp.h>
62 #include <linux/icmpv6.h>
63 #include <linux/if_tun.h>
64 #include <linux/in6.h>
65 #include <linux/errqueue.h>
66 #include <linux/random.h>
68 #include <sys/timerfd.h>
71 #include <sys/eventfd.h>
74 #include <sys/epoll.h>
77 #include "qemu/xattr.h"
79 #ifdef CONFIG_SENDFILE
80 #include <sys/sendfile.h>
82 #ifdef HAVE_SYS_KCOV_H
86 #define termios host_termios
87 #define winsize host_winsize
88 #define termio host_termio
89 #define sgttyb host_sgttyb /* same as target */
90 #define tchars host_tchars /* same as target */
91 #define ltchars host_ltchars /* same as target */
93 #include <linux/termios.h>
94 #include <linux/unistd.h>
95 #include <linux/cdrom.h>
96 #include <linux/hdreg.h>
97 #include <linux/soundcard.h>
99 #include <linux/mtio.h>
100 #include <linux/fs.h>
101 #include <linux/fd.h>
102 #if defined(CONFIG_FIEMAP)
103 #include <linux/fiemap.h>
105 #include <linux/fb.h>
106 #if defined(CONFIG_USBFS)
107 #include <linux/usbdevice_fs.h>
108 #include <linux/usb/ch9.h>
110 #include <linux/vt.h>
111 #include <linux/dm-ioctl.h>
112 #include <linux/reboot.h>
113 #include <linux/route.h>
114 #include <linux/filter.h>
115 #include <linux/blkpg.h>
116 #include <netpacket/packet.h>
117 #include <linux/netlink.h>
118 #include <linux/if_alg.h>
119 #include <linux/rtc.h>
120 #include <sound/asound.h>
122 #include <linux/btrfs.h>
125 #include <libdrm/drm.h>
126 #include <libdrm/i915_drm.h>
128 #include "linux_loop.h"
132 #include "user-internals.h"
134 #include "signal-common.h"
136 #include "user-mmap.h"
137 #include "user/safe-syscall.h"
138 #include "qemu/guest-random.h"
139 #include "qemu/selfmap.h"
140 #include "user/syscall-trace.h"
141 #include "special-errno.h"
142 #include "qapi/error.h"
143 #include "fd-trans.h"
145 #include "cpu_loop-common.h"
148 #define CLONE_IO 0x80000000 /* Clone io context */
151 /* We can't directly call the host clone syscall, because this will
152 * badly confuse libc (breaking mutexes, for example). So we must
153 * divide clone flags into:
154 * * flag combinations that look like pthread_create()
155 * * flag combinations that look like fork()
156 * * flags we can implement within QEMU itself
157 * * flags we can't support and will return an error for
159 /* For thread creation, all these flags must be present; for
160 * fork, none must be present.
162 #define CLONE_THREAD_FLAGS \
163 (CLONE_VM | CLONE_FS | CLONE_FILES | \
164 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
166 /* These flags are ignored:
167 * CLONE_DETACHED is now ignored by the kernel;
168 * CLONE_IO is just an optimisation hint to the I/O scheduler
170 #define CLONE_IGNORED_FLAGS \
171 (CLONE_DETACHED | CLONE_IO)
174 # define CLONE_PIDFD 0x00001000
177 /* Flags for fork which we can implement within QEMU itself */
178 #define CLONE_OPTIONAL_FORK_FLAGS \
179 (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
180 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
182 /* Flags for thread creation which we can implement within QEMU itself */
183 #define CLONE_OPTIONAL_THREAD_FLAGS \
184 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
185 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
187 #define CLONE_INVALID_FORK_FLAGS \
188 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
190 #define CLONE_INVALID_THREAD_FLAGS \
191 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
192 CLONE_IGNORED_FLAGS))
194 /* CLONE_VFORK is special cased early in do_fork(). The other flag bits
195 * have almost all been allocated. We cannot support any of
196 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
197 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
198 * The checks against the invalid thread masks above will catch these.
199 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
202 /* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
203 * once. This exercises the codepaths for restart.
205 //#define DEBUG_ERESTARTSYS
207 //#include <linux/msdos_fs.h>
208 #define VFAT_IOCTL_READDIR_BOTH \
209 _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
210 #define VFAT_IOCTL_READDIR_SHORT \
211 _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
221 #define _syscall0(type,name) \
222 static type name (void) \
224 return syscall(__NR_##name); \
227 #define _syscall1(type,name,type1,arg1) \
228 static type name (type1 arg1) \
230 return syscall(__NR_##name, arg1); \
233 #define _syscall2(type,name,type1,arg1,type2,arg2) \
234 static type name (type1 arg1,type2 arg2) \
236 return syscall(__NR_##name, arg1, arg2); \
239 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
240 static type name (type1 arg1,type2 arg2,type3 arg3) \
242 return syscall(__NR_##name, arg1, arg2, arg3); \
245 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
246 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
248 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
251 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
253 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
255 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
259 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
260 type5,arg5,type6,arg6) \
261 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
264 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
268 #define __NR_sys_uname __NR_uname
269 #define __NR_sys_getcwd1 __NR_getcwd
270 #define __NR_sys_getdents __NR_getdents
271 #define __NR_sys_getdents64 __NR_getdents64
272 #define __NR_sys_getpriority __NR_getpriority
273 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
274 #define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
275 #define __NR_sys_syslog __NR_syslog
276 #if defined(__NR_futex)
277 # define __NR_sys_futex __NR_futex
279 #if defined(__NR_futex_time64)
280 # define __NR_sys_futex_time64 __NR_futex_time64
282 #define __NR_sys_statx __NR_statx
284 #if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
285 #define __NR__llseek __NR_lseek
288 /* Newer kernel ports have llseek() instead of _llseek() */
289 #if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
290 #define TARGET_NR__llseek TARGET_NR_llseek
293 /* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
294 #ifndef TARGET_O_NONBLOCK_MASK
295 #define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
298 #define __NR_sys_gettid __NR_gettid
299 _syscall0(int, sys_gettid
)
301 /* For the 64-bit guest on 32-bit host case we must emulate
302 * getdents using getdents64, because otherwise the host
303 * might hand us back more dirent records than we can fit
304 * into the guest buffer after structure format conversion.
305 * Otherwise we emulate getdents with getdents if the host has it.
307 #if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
308 #define EMULATE_GETDENTS_WITH_GETDENTS
311 #if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
312 _syscall3(int, sys_getdents
, uint
, fd
, struct linux_dirent
*, dirp
, uint
, count
);
314 #if (defined(TARGET_NR_getdents) && \
315 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
316 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
317 _syscall3(int, sys_getdents64
, uint
, fd
, struct linux_dirent64
*, dirp
, uint
, count
);
319 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
320 _syscall5(int, _llseek
, uint
, fd
, ulong
, hi
, ulong
, lo
,
321 loff_t
*, res
, uint
, wh
);
323 _syscall3(int, sys_rt_sigqueueinfo
, pid_t
, pid
, int, sig
, siginfo_t
*, uinfo
)
324 _syscall4(int, sys_rt_tgsigqueueinfo
, pid_t
, pid
, pid_t
, tid
, int, sig
,
326 _syscall3(int,sys_syslog
,int,type
,char*,bufp
,int,len
)
327 #ifdef __NR_exit_group
328 _syscall1(int,exit_group
,int,error_code
)
330 #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
331 #define __NR_sys_close_range __NR_close_range
332 _syscall3(int,sys_close_range
,int,first
,int,last
,int,flags
)
333 #ifndef CLOSE_RANGE_CLOEXEC
334 #define CLOSE_RANGE_CLOEXEC (1U << 2)
337 #if defined(__NR_futex)
338 _syscall6(int,sys_futex
,int *,uaddr
,int,op
,int,val
,
339 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
341 #if defined(__NR_futex_time64)
342 _syscall6(int,sys_futex_time64
,int *,uaddr
,int,op
,int,val
,
343 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
345 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
346 _syscall2(int, pidfd_open
, pid_t
, pid
, unsigned int, flags
);
348 #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
349 _syscall4(int, pidfd_send_signal
, int, pidfd
, int, sig
, siginfo_t
*, info
,
350 unsigned int, flags
);
352 #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
353 _syscall3(int, pidfd_getfd
, int, pidfd
, int, targetfd
, unsigned int, flags
);
355 #define __NR_sys_sched_getaffinity __NR_sched_getaffinity
356 _syscall3(int, sys_sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
357 unsigned long *, user_mask_ptr
);
358 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
359 _syscall3(int, sys_sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
360 unsigned long *, user_mask_ptr
);
361 /* sched_attr is not defined in glibc */
364 uint32_t sched_policy
;
365 uint64_t sched_flags
;
367 uint32_t sched_priority
;
368 uint64_t sched_runtime
;
369 uint64_t sched_deadline
;
370 uint64_t sched_period
;
371 uint32_t sched_util_min
;
372 uint32_t sched_util_max
;
374 #define __NR_sys_sched_getattr __NR_sched_getattr
375 _syscall4(int, sys_sched_getattr
, pid_t
, pid
, struct sched_attr
*, attr
,
376 unsigned int, size
, unsigned int, flags
);
377 #define __NR_sys_sched_setattr __NR_sched_setattr
378 _syscall3(int, sys_sched_setattr
, pid_t
, pid
, struct sched_attr
*, attr
,
379 unsigned int, flags
);
380 #define __NR_sys_sched_getscheduler __NR_sched_getscheduler
381 _syscall1(int, sys_sched_getscheduler
, pid_t
, pid
);
382 #define __NR_sys_sched_setscheduler __NR_sched_setscheduler
383 _syscall3(int, sys_sched_setscheduler
, pid_t
, pid
, int, policy
,
384 const struct sched_param
*, param
);
385 #define __NR_sys_sched_getparam __NR_sched_getparam
386 _syscall2(int, sys_sched_getparam
, pid_t
, pid
,
387 struct sched_param
*, param
);
388 #define __NR_sys_sched_setparam __NR_sched_setparam
389 _syscall2(int, sys_sched_setparam
, pid_t
, pid
,
390 const struct sched_param
*, param
);
391 #define __NR_sys_getcpu __NR_getcpu
392 _syscall3(int, sys_getcpu
, unsigned *, cpu
, unsigned *, node
, void *, tcache
);
393 _syscall4(int, reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
395 _syscall2(int, capget
, struct __user_cap_header_struct
*, header
,
396 struct __user_cap_data_struct
*, data
);
397 _syscall2(int, capset
, struct __user_cap_header_struct
*, header
,
398 struct __user_cap_data_struct
*, data
);
399 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
400 _syscall2(int, ioprio_get
, int, which
, int, who
)
402 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
403 _syscall3(int, ioprio_set
, int, which
, int, who
, int, ioprio
)
405 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
406 _syscall3(int, getrandom
, void *, buf
, size_t, buflen
, unsigned int, flags
)
409 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
410 _syscall5(int, kcmp
, pid_t
, pid1
, pid_t
, pid2
, int, type
,
411 unsigned long, idx1
, unsigned long, idx2
)
415 * It is assumed that struct statx is architecture independent.
417 #if defined(TARGET_NR_statx) && defined(__NR_statx)
418 _syscall5(int, sys_statx
, int, dirfd
, const char *, pathname
, int, flags
,
419 unsigned int, mask
, struct target_statx
*, statxbuf
)
421 #if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
422 _syscall2(int, membarrier
, int, cmd
, int, flags
)
425 static const bitmask_transtbl fcntl_flags_tbl
[] = {
426 { TARGET_O_ACCMODE
, TARGET_O_WRONLY
, O_ACCMODE
, O_WRONLY
, },
427 { TARGET_O_ACCMODE
, TARGET_O_RDWR
, O_ACCMODE
, O_RDWR
, },
428 { TARGET_O_CREAT
, TARGET_O_CREAT
, O_CREAT
, O_CREAT
, },
429 { TARGET_O_EXCL
, TARGET_O_EXCL
, O_EXCL
, O_EXCL
, },
430 { TARGET_O_NOCTTY
, TARGET_O_NOCTTY
, O_NOCTTY
, O_NOCTTY
, },
431 { TARGET_O_TRUNC
, TARGET_O_TRUNC
, O_TRUNC
, O_TRUNC
, },
432 { TARGET_O_APPEND
, TARGET_O_APPEND
, O_APPEND
, O_APPEND
, },
433 { TARGET_O_NONBLOCK
, TARGET_O_NONBLOCK
, O_NONBLOCK
, O_NONBLOCK
, },
434 { TARGET_O_SYNC
, TARGET_O_DSYNC
, O_SYNC
, O_DSYNC
, },
435 { TARGET_O_SYNC
, TARGET_O_SYNC
, O_SYNC
, O_SYNC
, },
436 { TARGET_FASYNC
, TARGET_FASYNC
, FASYNC
, FASYNC
, },
437 { TARGET_O_DIRECTORY
, TARGET_O_DIRECTORY
, O_DIRECTORY
, O_DIRECTORY
, },
438 { TARGET_O_NOFOLLOW
, TARGET_O_NOFOLLOW
, O_NOFOLLOW
, O_NOFOLLOW
, },
439 #if defined(O_DIRECT)
440 { TARGET_O_DIRECT
, TARGET_O_DIRECT
, O_DIRECT
, O_DIRECT
, },
442 #if defined(O_NOATIME)
443 { TARGET_O_NOATIME
, TARGET_O_NOATIME
, O_NOATIME
, O_NOATIME
},
445 #if defined(O_CLOEXEC)
446 { TARGET_O_CLOEXEC
, TARGET_O_CLOEXEC
, O_CLOEXEC
, O_CLOEXEC
},
449 { TARGET_O_PATH
, TARGET_O_PATH
, O_PATH
, O_PATH
},
451 #if defined(O_TMPFILE)
452 { TARGET_O_TMPFILE
, TARGET_O_TMPFILE
, O_TMPFILE
, O_TMPFILE
},
454 /* Don't terminate the list prematurely on 64-bit host+guest. */
455 #if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
456 { TARGET_O_LARGEFILE
, TARGET_O_LARGEFILE
, O_LARGEFILE
, O_LARGEFILE
, },
461 _syscall2(int, sys_getcwd1
, char *, buf
, size_t, size
)
463 #if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
464 #if defined(__NR_utimensat)
465 #define __NR_sys_utimensat __NR_utimensat
466 _syscall4(int,sys_utimensat
,int,dirfd
,const char *,pathname
,
467 const struct timespec
*,tsp
,int,flags
)
469 static int sys_utimensat(int dirfd
, const char *pathname
,
470 const struct timespec times
[2], int flags
)
476 #endif /* TARGET_NR_utimensat */
478 #ifdef TARGET_NR_renameat2
479 #if defined(__NR_renameat2)
480 #define __NR_sys_renameat2 __NR_renameat2
481 _syscall5(int, sys_renameat2
, int, oldfd
, const char *, old
, int, newfd
,
482 const char *, new, unsigned int, flags
)
484 static int sys_renameat2(int oldfd
, const char *old
,
485 int newfd
, const char *new, int flags
)
488 return renameat(oldfd
, old
, newfd
, new);
494 #endif /* TARGET_NR_renameat2 */
496 #ifdef CONFIG_INOTIFY
497 #include <sys/inotify.h>
499 /* Userspace can usually survive runtime without inotify */
500 #undef TARGET_NR_inotify_init
501 #undef TARGET_NR_inotify_init1
502 #undef TARGET_NR_inotify_add_watch
503 #undef TARGET_NR_inotify_rm_watch
504 #endif /* CONFIG_INOTIFY */
506 #if defined(TARGET_NR_prlimit64)
507 #ifndef __NR_prlimit64
508 # define __NR_prlimit64 -1
510 #define __NR_sys_prlimit64 __NR_prlimit64
511 /* The glibc rlimit structure may not be that used by the underlying syscall */
512 struct host_rlimit64
{
516 _syscall4(int, sys_prlimit64
, pid_t
, pid
, int, resource
,
517 const struct host_rlimit64
*, new_limit
,
518 struct host_rlimit64
*, old_limit
)
522 #if defined(TARGET_NR_timer_create)
523 /* Maximum of 32 active POSIX timers allowed at any one time. */
524 #define GUEST_TIMER_MAX 32
525 static timer_t g_posix_timers
[GUEST_TIMER_MAX
];
526 static int g_posix_timer_allocated
[GUEST_TIMER_MAX
];
528 static inline int next_free_host_timer(void)
531 for (k
= 0; k
< ARRAY_SIZE(g_posix_timer_allocated
); k
++) {
532 if (qatomic_xchg(g_posix_timer_allocated
+ k
, 1) == 0) {
539 static inline void free_host_timer_slot(int id
)
541 qatomic_store_release(g_posix_timer_allocated
+ id
, 0);
545 static inline int host_to_target_errno(int host_errno
)
547 switch (host_errno
) {
548 #define E(X) case X: return TARGET_##X;
549 #include "errnos.c.inc"
556 static inline int target_to_host_errno(int target_errno
)
558 switch (target_errno
) {
559 #define E(X) case TARGET_##X: return X;
560 #include "errnos.c.inc"
567 abi_long
get_errno(abi_long ret
)
570 return -host_to_target_errno(errno
);
575 const char *target_strerror(int err
)
577 if (err
== QEMU_ERESTARTSYS
) {
578 return "To be restarted";
580 if (err
== QEMU_ESIGRETURN
) {
581 return "Successful exit from sigreturn";
584 return strerror(target_to_host_errno(err
));
587 static int check_zeroed_user(abi_long addr
, size_t ksize
, size_t usize
)
591 if (usize
<= ksize
) {
594 for (i
= ksize
; i
< usize
; i
++) {
595 if (get_user_u8(b
, addr
+ i
)) {
596 return -TARGET_EFAULT
;
605 #define safe_syscall0(type, name) \
606 static type safe_##name(void) \
608 return safe_syscall(__NR_##name); \
611 #define safe_syscall1(type, name, type1, arg1) \
612 static type safe_##name(type1 arg1) \
614 return safe_syscall(__NR_##name, arg1); \
617 #define safe_syscall2(type, name, type1, arg1, type2, arg2) \
618 static type safe_##name(type1 arg1, type2 arg2) \
620 return safe_syscall(__NR_##name, arg1, arg2); \
623 #define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
624 static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
626 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
629 #define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
631 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
633 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
636 #define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
637 type4, arg4, type5, arg5) \
638 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
641 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
644 #define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
645 type4, arg4, type5, arg5, type6, arg6) \
646 static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
647 type5 arg5, type6 arg6) \
649 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
652 safe_syscall3(ssize_t
, read
, int, fd
, void *, buff
, size_t, count
)
653 safe_syscall3(ssize_t
, write
, int, fd
, const void *, buff
, size_t, count
)
654 safe_syscall4(int, openat
, int, dirfd
, const char *, pathname
, \
655 int, flags
, mode_t
, mode
)
656 #if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
657 safe_syscall4(pid_t
, wait4
, pid_t
, pid
, int *, status
, int, options
, \
658 struct rusage
*, rusage
)
660 safe_syscall5(int, waitid
, idtype_t
, idtype
, id_t
, id
, siginfo_t
*, infop
, \
661 int, options
, struct rusage
*, rusage
)
662 safe_syscall5(int, execveat
, int, dirfd
, const char *, filename
,
663 char **, argv
, char **, envp
, int, flags
)
664 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
665 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
666 safe_syscall6(int, pselect6
, int, nfds
, fd_set
*, readfds
, fd_set
*, writefds
, \
667 fd_set
*, exceptfds
, struct timespec
*, timeout
, void *, sig
)
669 #if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
670 safe_syscall5(int, ppoll
, struct pollfd
*, ufds
, unsigned int, nfds
,
671 struct timespec
*, tsp
, const sigset_t
*, sigmask
,
674 safe_syscall6(int, epoll_pwait
, int, epfd
, struct epoll_event
*, events
,
675 int, maxevents
, int, timeout
, const sigset_t
*, sigmask
,
677 #if defined(__NR_futex)
678 safe_syscall6(int,futex
,int *,uaddr
,int,op
,int,val
, \
679 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
681 #if defined(__NR_futex_time64)
682 safe_syscall6(int,futex_time64
,int *,uaddr
,int,op
,int,val
, \
683 const struct timespec
*,timeout
,int *,uaddr2
,int,val3
)
685 safe_syscall2(int, rt_sigsuspend
, sigset_t
*, newset
, size_t, sigsetsize
)
686 safe_syscall2(int, kill
, pid_t
, pid
, int, sig
)
687 safe_syscall2(int, tkill
, int, tid
, int, sig
)
688 safe_syscall3(int, tgkill
, int, tgid
, int, pid
, int, sig
)
689 safe_syscall3(ssize_t
, readv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
690 safe_syscall3(ssize_t
, writev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
)
691 safe_syscall5(ssize_t
, preadv
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
692 unsigned long, pos_l
, unsigned long, pos_h
)
693 safe_syscall5(ssize_t
, pwritev
, int, fd
, const struct iovec
*, iov
, int, iovcnt
,
694 unsigned long, pos_l
, unsigned long, pos_h
)
695 safe_syscall3(int, connect
, int, fd
, const struct sockaddr
*, addr
,
697 safe_syscall6(ssize_t
, sendto
, int, fd
, const void *, buf
, size_t, len
,
698 int, flags
, const struct sockaddr
*, addr
, socklen_t
, addrlen
)
699 safe_syscall6(ssize_t
, recvfrom
, int, fd
, void *, buf
, size_t, len
,
700 int, flags
, struct sockaddr
*, addr
, socklen_t
*, addrlen
)
701 safe_syscall3(ssize_t
, sendmsg
, int, fd
, const struct msghdr
*, msg
, int, flags
)
702 safe_syscall3(ssize_t
, recvmsg
, int, fd
, struct msghdr
*, msg
, int, flags
)
703 safe_syscall2(int, flock
, int, fd
, int, operation
)
704 #if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
705 safe_syscall4(int, rt_sigtimedwait
, const sigset_t
*, these
, siginfo_t
*, uinfo
,
706 const struct timespec
*, uts
, size_t, sigsetsize
)
708 safe_syscall4(int, accept4
, int, fd
, struct sockaddr
*, addr
, socklen_t
*, len
,
710 #if defined(TARGET_NR_nanosleep)
711 safe_syscall2(int, nanosleep
, const struct timespec
*, req
,
712 struct timespec
*, rem
)
714 #if defined(TARGET_NR_clock_nanosleep) || \
715 defined(TARGET_NR_clock_nanosleep_time64)
716 safe_syscall4(int, clock_nanosleep
, const clockid_t
, clock
, int, flags
,
717 const struct timespec
*, req
, struct timespec
*, rem
)
721 safe_syscall5(int, ipc
, int, call
, long, first
, long, second
, long, third
,
724 safe_syscall6(int, ipc
, int, call
, long, first
, long, second
, long, third
,
725 void *, ptr
, long, fifth
)
729 safe_syscall4(int, msgsnd
, int, msgid
, const void *, msgp
, size_t, sz
,
733 safe_syscall5(int, msgrcv
, int, msgid
, void *, msgp
, size_t, sz
,
734 long, msgtype
, int, flags
)
736 #ifdef __NR_semtimedop
737 safe_syscall4(int, semtimedop
, int, semid
, struct sembuf
*, tsops
,
738 unsigned, nsops
, const struct timespec
*, timeout
)
740 #if defined(TARGET_NR_mq_timedsend) || \
741 defined(TARGET_NR_mq_timedsend_time64)
742 safe_syscall5(int, mq_timedsend
, int, mqdes
, const char *, msg_ptr
,
743 size_t, len
, unsigned, prio
, const struct timespec
*, timeout
)
745 #if defined(TARGET_NR_mq_timedreceive) || \
746 defined(TARGET_NR_mq_timedreceive_time64)
747 safe_syscall5(int, mq_timedreceive
, int, mqdes
, char *, msg_ptr
,
748 size_t, len
, unsigned *, prio
, const struct timespec
*, timeout
)
750 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
751 safe_syscall6(ssize_t
, copy_file_range
, int, infd
, loff_t
*, pinoff
,
752 int, outfd
, loff_t
*, poutoff
, size_t, length
,
756 /* We do ioctl like this rather than via safe_syscall3 to preserve the
757 * "third argument might be integer or pointer or not present" behaviour of
760 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
761 /* Similarly for fcntl. Note that callers must always:
762 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
763 * use the flock64 struct rather than unsuffixed flock
764 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
767 #define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
769 #define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
772 static inline int host_to_target_sock_type(int host_type
)
776 switch (host_type
& 0xf /* SOCK_TYPE_MASK */) {
778 target_type
= TARGET_SOCK_DGRAM
;
781 target_type
= TARGET_SOCK_STREAM
;
784 target_type
= host_type
& 0xf /* SOCK_TYPE_MASK */;
788 #if defined(SOCK_CLOEXEC)
789 if (host_type
& SOCK_CLOEXEC
) {
790 target_type
|= TARGET_SOCK_CLOEXEC
;
794 #if defined(SOCK_NONBLOCK)
795 if (host_type
& SOCK_NONBLOCK
) {
796 target_type
|= TARGET_SOCK_NONBLOCK
;
803 static abi_ulong target_brk
;
804 static abi_ulong brk_page
;
806 void target_set_brk(abi_ulong new_brk
)
808 target_brk
= new_brk
;
809 brk_page
= HOST_PAGE_ALIGN(target_brk
);
812 /* do_brk() must return target values and target errnos. */
813 abi_long
do_brk(abi_ulong brk_val
)
815 abi_long mapped_addr
;
816 abi_ulong new_alloc_size
;
817 abi_ulong new_brk
, new_host_brk_page
;
819 /* brk pointers are always untagged */
821 /* return old brk value if brk_val unchanged or zero */
822 if (!brk_val
|| brk_val
== target_brk
) {
826 new_brk
= TARGET_PAGE_ALIGN(brk_val
);
827 new_host_brk_page
= HOST_PAGE_ALIGN(brk_val
);
829 /* brk_val and old target_brk might be on the same page */
830 if (new_brk
== TARGET_PAGE_ALIGN(target_brk
)) {
831 if (brk_val
> target_brk
) {
832 /* empty remaining bytes in (possibly larger) host page */
833 memset(g2h_untagged(target_brk
), 0, new_host_brk_page
- target_brk
);
835 target_brk
= brk_val
;
839 /* Release heap if necesary */
840 if (new_brk
< target_brk
) {
841 /* empty remaining bytes in (possibly larger) host page */
842 memset(g2h_untagged(brk_val
), 0, new_host_brk_page
- brk_val
);
844 /* free unused host pages and set new brk_page */
845 target_munmap(new_host_brk_page
, brk_page
- new_host_brk_page
);
846 brk_page
= new_host_brk_page
;
848 target_brk
= brk_val
;
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
= new_host_brk_page
- brk_page
;
859 if (new_alloc_size
) {
860 mapped_addr
= get_errno(target_mmap(brk_page
, new_alloc_size
,
861 PROT_READ
|PROT_WRITE
,
862 MAP_ANON
|MAP_PRIVATE
, 0, 0));
864 mapped_addr
= brk_page
;
867 if (mapped_addr
== brk_page
) {
868 /* Heap contents are initialized to zero, as for anonymous
869 * mapped pages. Technically the new pages are already
870 * initialized to zero since they *are* anonymous mapped
871 * pages, however we have to take care with the contents that
872 * come from the remaining part of the previous page: it may
873 * contains garbage data due to a previous heap usage (grown
875 memset(g2h_untagged(target_brk
), 0, brk_page
- target_brk
);
877 target_brk
= brk_val
;
878 brk_page
= new_host_brk_page
;
880 } else if (mapped_addr
!= -1) {
881 /* Mapped but at wrong address, meaning there wasn't actually
882 * enough space for this brk.
884 target_munmap(mapped_addr
, new_alloc_size
);
888 #if defined(TARGET_ALPHA)
889 /* We (partially) emulate OSF/1 on Alpha, which requires we
890 return a proper errno, not an unchanged brk value. */
891 return -TARGET_ENOMEM
;
893 /* For everything else, return the previous break. */
897 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
898 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
899 static inline abi_long
copy_from_user_fdset(fd_set
*fds
,
900 abi_ulong target_fds_addr
,
904 abi_ulong b
, *target_fds
;
906 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
907 if (!(target_fds
= lock_user(VERIFY_READ
,
909 sizeof(abi_ulong
) * nw
,
911 return -TARGET_EFAULT
;
915 for (i
= 0; i
< nw
; i
++) {
916 /* grab the abi_ulong */
917 __get_user(b
, &target_fds
[i
]);
918 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
919 /* check the bit inside the abi_ulong */
926 unlock_user(target_fds
, target_fds_addr
, 0);
931 static inline abi_ulong
copy_from_user_fdset_ptr(fd_set
*fds
, fd_set
**fds_ptr
,
932 abi_ulong target_fds_addr
,
935 if (target_fds_addr
) {
936 if (copy_from_user_fdset(fds
, target_fds_addr
, n
))
937 return -TARGET_EFAULT
;
945 static inline abi_long
copy_to_user_fdset(abi_ulong target_fds_addr
,
951 abi_ulong
*target_fds
;
953 nw
= DIV_ROUND_UP(n
, TARGET_ABI_BITS
);
954 if (!(target_fds
= lock_user(VERIFY_WRITE
,
956 sizeof(abi_ulong
) * nw
,
958 return -TARGET_EFAULT
;
961 for (i
= 0; i
< nw
; i
++) {
963 for (j
= 0; j
< TARGET_ABI_BITS
; j
++) {
964 v
|= ((abi_ulong
)(FD_ISSET(k
, fds
) != 0) << j
);
967 __put_user(v
, &target_fds
[i
]);
970 unlock_user(target_fds
, target_fds_addr
, sizeof(abi_ulong
) * nw
);
976 #if defined(__alpha__)
982 static inline abi_long
host_to_target_clock_t(long ticks
)
984 #if HOST_HZ == TARGET_HZ
987 return ((int64_t)ticks
* TARGET_HZ
) / HOST_HZ
;
991 static inline abi_long
host_to_target_rusage(abi_ulong target_addr
,
992 const struct rusage
*rusage
)
994 struct target_rusage
*target_rusage
;
996 if (!lock_user_struct(VERIFY_WRITE
, target_rusage
, target_addr
, 0))
997 return -TARGET_EFAULT
;
998 target_rusage
->ru_utime
.tv_sec
= tswapal(rusage
->ru_utime
.tv_sec
);
999 target_rusage
->ru_utime
.tv_usec
= tswapal(rusage
->ru_utime
.tv_usec
);
1000 target_rusage
->ru_stime
.tv_sec
= tswapal(rusage
->ru_stime
.tv_sec
);
1001 target_rusage
->ru_stime
.tv_usec
= tswapal(rusage
->ru_stime
.tv_usec
);
1002 target_rusage
->ru_maxrss
= tswapal(rusage
->ru_maxrss
);
1003 target_rusage
->ru_ixrss
= tswapal(rusage
->ru_ixrss
);
1004 target_rusage
->ru_idrss
= tswapal(rusage
->ru_idrss
);
1005 target_rusage
->ru_isrss
= tswapal(rusage
->ru_isrss
);
1006 target_rusage
->ru_minflt
= tswapal(rusage
->ru_minflt
);
1007 target_rusage
->ru_majflt
= tswapal(rusage
->ru_majflt
);
1008 target_rusage
->ru_nswap
= tswapal(rusage
->ru_nswap
);
1009 target_rusage
->ru_inblock
= tswapal(rusage
->ru_inblock
);
1010 target_rusage
->ru_oublock
= tswapal(rusage
->ru_oublock
);
1011 target_rusage
->ru_msgsnd
= tswapal(rusage
->ru_msgsnd
);
1012 target_rusage
->ru_msgrcv
= tswapal(rusage
->ru_msgrcv
);
1013 target_rusage
->ru_nsignals
= tswapal(rusage
->ru_nsignals
);
1014 target_rusage
->ru_nvcsw
= tswapal(rusage
->ru_nvcsw
);
1015 target_rusage
->ru_nivcsw
= tswapal(rusage
->ru_nivcsw
);
1016 unlock_user_struct(target_rusage
, target_addr
, 1);
1021 #ifdef TARGET_NR_setrlimit
1022 static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim
)
1024 abi_ulong target_rlim_swap
;
1027 target_rlim_swap
= tswapal(target_rlim
);
1028 if (target_rlim_swap
== TARGET_RLIM_INFINITY
)
1029 return RLIM_INFINITY
;
1031 result
= target_rlim_swap
;
1032 if (target_rlim_swap
!= (rlim_t
)result
)
1033 return RLIM_INFINITY
;
1039 #if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
1040 static inline abi_ulong
host_to_target_rlim(rlim_t rlim
)
1042 abi_ulong target_rlim_swap
;
1045 if (rlim
== RLIM_INFINITY
|| rlim
!= (abi_long
)rlim
)
1046 target_rlim_swap
= TARGET_RLIM_INFINITY
;
1048 target_rlim_swap
= rlim
;
1049 result
= tswapal(target_rlim_swap
);
1055 static inline int target_to_host_resource(int code
)
1058 case TARGET_RLIMIT_AS
:
1060 case TARGET_RLIMIT_CORE
:
1062 case TARGET_RLIMIT_CPU
:
1064 case TARGET_RLIMIT_DATA
:
1066 case TARGET_RLIMIT_FSIZE
:
1067 return RLIMIT_FSIZE
;
1068 case TARGET_RLIMIT_LOCKS
:
1069 return RLIMIT_LOCKS
;
1070 case TARGET_RLIMIT_MEMLOCK
:
1071 return RLIMIT_MEMLOCK
;
1072 case TARGET_RLIMIT_MSGQUEUE
:
1073 return RLIMIT_MSGQUEUE
;
1074 case TARGET_RLIMIT_NICE
:
1076 case TARGET_RLIMIT_NOFILE
:
1077 return RLIMIT_NOFILE
;
1078 case TARGET_RLIMIT_NPROC
:
1079 return RLIMIT_NPROC
;
1080 case TARGET_RLIMIT_RSS
:
1082 case TARGET_RLIMIT_RTPRIO
:
1083 return RLIMIT_RTPRIO
;
1084 #ifdef RLIMIT_RTTIME
1085 case TARGET_RLIMIT_RTTIME
:
1086 return RLIMIT_RTTIME
;
1088 case TARGET_RLIMIT_SIGPENDING
:
1089 return RLIMIT_SIGPENDING
;
1090 case TARGET_RLIMIT_STACK
:
1091 return RLIMIT_STACK
;
1097 static inline abi_long
copy_from_user_timeval(struct timeval
*tv
,
1098 abi_ulong target_tv_addr
)
1100 struct target_timeval
*target_tv
;
1102 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1103 return -TARGET_EFAULT
;
1106 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1107 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1109 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1114 static inline abi_long
copy_to_user_timeval(abi_ulong target_tv_addr
,
1115 const struct timeval
*tv
)
1117 struct target_timeval
*target_tv
;
1119 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1120 return -TARGET_EFAULT
;
1123 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1124 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1126 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1131 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
1132 static inline abi_long
copy_from_user_timeval64(struct timeval
*tv
,
1133 abi_ulong target_tv_addr
)
1135 struct target__kernel_sock_timeval
*target_tv
;
1137 if (!lock_user_struct(VERIFY_READ
, target_tv
, target_tv_addr
, 1)) {
1138 return -TARGET_EFAULT
;
1141 __get_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1142 __get_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1144 unlock_user_struct(target_tv
, target_tv_addr
, 0);
1150 static inline abi_long
copy_to_user_timeval64(abi_ulong target_tv_addr
,
1151 const struct timeval
*tv
)
1153 struct target__kernel_sock_timeval
*target_tv
;
1155 if (!lock_user_struct(VERIFY_WRITE
, target_tv
, target_tv_addr
, 0)) {
1156 return -TARGET_EFAULT
;
1159 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1160 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1162 unlock_user_struct(target_tv
, target_tv_addr
, 1);
1167 #if defined(TARGET_NR_futex) || \
1168 defined(TARGET_NR_rt_sigtimedwait) || \
1169 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1170 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1171 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
1172 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
1173 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
1174 defined(TARGET_NR_timer_settime) || \
1175 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
1176 static inline abi_long
target_to_host_timespec(struct timespec
*host_ts
,
1177 abi_ulong target_addr
)
1179 struct target_timespec
*target_ts
;
1181 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1182 return -TARGET_EFAULT
;
1184 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1185 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1186 unlock_user_struct(target_ts
, target_addr
, 0);
1191 #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
1192 defined(TARGET_NR_timer_settime64) || \
1193 defined(TARGET_NR_mq_timedsend_time64) || \
1194 defined(TARGET_NR_mq_timedreceive_time64) || \
1195 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
1196 defined(TARGET_NR_clock_nanosleep_time64) || \
1197 defined(TARGET_NR_rt_sigtimedwait_time64) || \
1198 defined(TARGET_NR_utimensat) || \
1199 defined(TARGET_NR_utimensat_time64) || \
1200 defined(TARGET_NR_semtimedop_time64) || \
1201 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
1202 static inline abi_long
target_to_host_timespec64(struct timespec
*host_ts
,
1203 abi_ulong target_addr
)
1205 struct target__kernel_timespec
*target_ts
;
1207 if (!lock_user_struct(VERIFY_READ
, target_ts
, target_addr
, 1)) {
1208 return -TARGET_EFAULT
;
1210 __get_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1211 __get_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1212 /* in 32bit mode, this drops the padding */
1213 host_ts
->tv_nsec
= (long)(abi_long
)host_ts
->tv_nsec
;
1214 unlock_user_struct(target_ts
, target_addr
, 0);
1219 static inline abi_long
host_to_target_timespec(abi_ulong target_addr
,
1220 struct timespec
*host_ts
)
1222 struct target_timespec
*target_ts
;
1224 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1225 return -TARGET_EFAULT
;
1227 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1228 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1229 unlock_user_struct(target_ts
, target_addr
, 1);
1233 static inline abi_long
host_to_target_timespec64(abi_ulong target_addr
,
1234 struct timespec
*host_ts
)
1236 struct target__kernel_timespec
*target_ts
;
1238 if (!lock_user_struct(VERIFY_WRITE
, target_ts
, target_addr
, 0)) {
1239 return -TARGET_EFAULT
;
1241 __put_user(host_ts
->tv_sec
, &target_ts
->tv_sec
);
1242 __put_user(host_ts
->tv_nsec
, &target_ts
->tv_nsec
);
1243 unlock_user_struct(target_ts
, target_addr
, 1);
1247 #if defined(TARGET_NR_gettimeofday)
1248 static inline abi_long
copy_to_user_timezone(abi_ulong target_tz_addr
,
1249 struct timezone
*tz
)
1251 struct target_timezone
*target_tz
;
1253 if (!lock_user_struct(VERIFY_WRITE
, target_tz
, target_tz_addr
, 1)) {
1254 return -TARGET_EFAULT
;
1257 __put_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1258 __put_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1260 unlock_user_struct(target_tz
, target_tz_addr
, 1);
1266 #if defined(TARGET_NR_settimeofday)
1267 static inline abi_long
copy_from_user_timezone(struct timezone
*tz
,
1268 abi_ulong target_tz_addr
)
1270 struct target_timezone
*target_tz
;
1272 if (!lock_user_struct(VERIFY_READ
, target_tz
, target_tz_addr
, 1)) {
1273 return -TARGET_EFAULT
;
1276 __get_user(tz
->tz_minuteswest
, &target_tz
->tz_minuteswest
);
1277 __get_user(tz
->tz_dsttime
, &target_tz
->tz_dsttime
);
1279 unlock_user_struct(target_tz
, target_tz_addr
, 0);
1285 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1288 static inline abi_long
copy_from_user_mq_attr(struct mq_attr
*attr
,
1289 abi_ulong target_mq_attr_addr
)
1291 struct target_mq_attr
*target_mq_attr
;
1293 if (!lock_user_struct(VERIFY_READ
, target_mq_attr
,
1294 target_mq_attr_addr
, 1))
1295 return -TARGET_EFAULT
;
1297 __get_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1298 __get_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1299 __get_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1300 __get_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1302 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 0);
1307 static inline abi_long
copy_to_user_mq_attr(abi_ulong target_mq_attr_addr
,
1308 const struct mq_attr
*attr
)
1310 struct target_mq_attr
*target_mq_attr
;
1312 if (!lock_user_struct(VERIFY_WRITE
, target_mq_attr
,
1313 target_mq_attr_addr
, 0))
1314 return -TARGET_EFAULT
;
1316 __put_user(attr
->mq_flags
, &target_mq_attr
->mq_flags
);
1317 __put_user(attr
->mq_maxmsg
, &target_mq_attr
->mq_maxmsg
);
1318 __put_user(attr
->mq_msgsize
, &target_mq_attr
->mq_msgsize
);
1319 __put_user(attr
->mq_curmsgs
, &target_mq_attr
->mq_curmsgs
);
1321 unlock_user_struct(target_mq_attr
, target_mq_attr_addr
, 1);
1327 #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
1328 /* do_select() must return target values and target errnos. */
1329 static abi_long
do_select(int n
,
1330 abi_ulong rfd_addr
, abi_ulong wfd_addr
,
1331 abi_ulong efd_addr
, abi_ulong target_tv_addr
)
1333 fd_set rfds
, wfds
, efds
;
1334 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1336 struct timespec ts
, *ts_ptr
;
1339 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1343 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1347 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1352 if (target_tv_addr
) {
1353 if (copy_from_user_timeval(&tv
, target_tv_addr
))
1354 return -TARGET_EFAULT
;
1355 ts
.tv_sec
= tv
.tv_sec
;
1356 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1362 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1365 if (!is_error(ret
)) {
1366 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
))
1367 return -TARGET_EFAULT
;
1368 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
))
1369 return -TARGET_EFAULT
;
1370 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
))
1371 return -TARGET_EFAULT
;
1373 if (target_tv_addr
) {
1374 tv
.tv_sec
= ts
.tv_sec
;
1375 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1376 if (copy_to_user_timeval(target_tv_addr
, &tv
)) {
1377 return -TARGET_EFAULT
;
1385 #if defined(TARGET_WANT_OLD_SYS_SELECT)
1386 static abi_long
do_old_select(abi_ulong arg1
)
1388 struct target_sel_arg_struct
*sel
;
1389 abi_ulong inp
, outp
, exp
, tvp
;
1392 if (!lock_user_struct(VERIFY_READ
, sel
, arg1
, 1)) {
1393 return -TARGET_EFAULT
;
1396 nsel
= tswapal(sel
->n
);
1397 inp
= tswapal(sel
->inp
);
1398 outp
= tswapal(sel
->outp
);
1399 exp
= tswapal(sel
->exp
);
1400 tvp
= tswapal(sel
->tvp
);
1402 unlock_user_struct(sel
, arg1
, 0);
1404 return do_select(nsel
, inp
, outp
, exp
, tvp
);
1409 #if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
1410 static abi_long
do_pselect6(abi_long arg1
, abi_long arg2
, abi_long arg3
,
1411 abi_long arg4
, abi_long arg5
, abi_long arg6
,
1414 abi_long rfd_addr
, wfd_addr
, efd_addr
, n
, ts_addr
;
1415 fd_set rfds
, wfds
, efds
;
1416 fd_set
*rfds_ptr
, *wfds_ptr
, *efds_ptr
;
1417 struct timespec ts
, *ts_ptr
;
1421 * The 6th arg is actually two args smashed together,
1422 * so we cannot use the C library.
1429 abi_ulong arg_sigset
, arg_sigsize
, *arg7
;
1437 ret
= copy_from_user_fdset_ptr(&rfds
, &rfds_ptr
, rfd_addr
, n
);
1441 ret
= copy_from_user_fdset_ptr(&wfds
, &wfds_ptr
, wfd_addr
, n
);
1445 ret
= copy_from_user_fdset_ptr(&efds
, &efds_ptr
, efd_addr
, n
);
1451 * This takes a timespec, and not a timeval, so we cannot
1452 * use the do_select() helper ...
1456 if (target_to_host_timespec64(&ts
, ts_addr
)) {
1457 return -TARGET_EFAULT
;
1460 if (target_to_host_timespec(&ts
, ts_addr
)) {
1461 return -TARGET_EFAULT
;
1469 /* Extract the two packed args for the sigset */
1472 arg7
= lock_user(VERIFY_READ
, arg6
, sizeof(*arg7
) * 2, 1);
1474 return -TARGET_EFAULT
;
1476 arg_sigset
= tswapal(arg7
[0]);
1477 arg_sigsize
= tswapal(arg7
[1]);
1478 unlock_user(arg7
, arg6
, 0);
1481 ret
= process_sigsuspend_mask(&sig
.set
, arg_sigset
, arg_sigsize
);
1486 sig
.size
= SIGSET_T_SIZE
;
1490 ret
= get_errno(safe_pselect6(n
, rfds_ptr
, wfds_ptr
, efds_ptr
,
1494 finish_sigsuspend_mask(ret
);
1497 if (!is_error(ret
)) {
1498 if (rfd_addr
&& copy_to_user_fdset(rfd_addr
, &rfds
, n
)) {
1499 return -TARGET_EFAULT
;
1501 if (wfd_addr
&& copy_to_user_fdset(wfd_addr
, &wfds
, n
)) {
1502 return -TARGET_EFAULT
;
1504 if (efd_addr
&& copy_to_user_fdset(efd_addr
, &efds
, n
)) {
1505 return -TARGET_EFAULT
;
1508 if (ts_addr
&& host_to_target_timespec64(ts_addr
, &ts
)) {
1509 return -TARGET_EFAULT
;
1512 if (ts_addr
&& host_to_target_timespec(ts_addr
, &ts
)) {
1513 return -TARGET_EFAULT
;
1521 #if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
1522 defined(TARGET_NR_ppoll_time64)
1523 static abi_long
do_ppoll(abi_long arg1
, abi_long arg2
, abi_long arg3
,
1524 abi_long arg4
, abi_long arg5
, bool ppoll
, bool time64
)
1526 struct target_pollfd
*target_pfd
;
1527 unsigned int nfds
= arg2
;
1535 if (nfds
> (INT_MAX
/ sizeof(struct target_pollfd
))) {
1536 return -TARGET_EINVAL
;
1538 target_pfd
= lock_user(VERIFY_WRITE
, arg1
,
1539 sizeof(struct target_pollfd
) * nfds
, 1);
1541 return -TARGET_EFAULT
;
1544 pfd
= alloca(sizeof(struct pollfd
) * nfds
);
1545 for (i
= 0; i
< nfds
; i
++) {
1546 pfd
[i
].fd
= tswap32(target_pfd
[i
].fd
);
1547 pfd
[i
].events
= tswap16(target_pfd
[i
].events
);
1551 struct timespec _timeout_ts
, *timeout_ts
= &_timeout_ts
;
1552 sigset_t
*set
= NULL
;
1556 if (target_to_host_timespec64(timeout_ts
, arg3
)) {
1557 unlock_user(target_pfd
, arg1
, 0);
1558 return -TARGET_EFAULT
;
1561 if (target_to_host_timespec(timeout_ts
, arg3
)) {
1562 unlock_user(target_pfd
, arg1
, 0);
1563 return -TARGET_EFAULT
;
1571 ret
= process_sigsuspend_mask(&set
, arg4
, arg5
);
1573 unlock_user(target_pfd
, arg1
, 0);
1578 ret
= get_errno(safe_ppoll(pfd
, nfds
, timeout_ts
,
1579 set
, SIGSET_T_SIZE
));
1582 finish_sigsuspend_mask(ret
);
1584 if (!is_error(ret
) && arg3
) {
1586 if (host_to_target_timespec64(arg3
, timeout_ts
)) {
1587 return -TARGET_EFAULT
;
1590 if (host_to_target_timespec(arg3
, timeout_ts
)) {
1591 return -TARGET_EFAULT
;
1596 struct timespec ts
, *pts
;
1599 /* Convert ms to secs, ns */
1600 ts
.tv_sec
= arg3
/ 1000;
1601 ts
.tv_nsec
= (arg3
% 1000) * 1000000LL;
1604 /* -ve poll() timeout means "infinite" */
1607 ret
= get_errno(safe_ppoll(pfd
, nfds
, pts
, NULL
, 0));
1610 if (!is_error(ret
)) {
1611 for (i
= 0; i
< nfds
; i
++) {
1612 target_pfd
[i
].revents
= tswap16(pfd
[i
].revents
);
1615 unlock_user(target_pfd
, arg1
, sizeof(struct target_pollfd
) * nfds
);
1620 static abi_long
do_pipe(CPUArchState
*cpu_env
, abi_ulong pipedes
,
1621 int flags
, int is_pipe2
)
1625 ret
= pipe2(host_pipe
, flags
);
1628 return get_errno(ret
);
1630 /* Several targets have special calling conventions for the original
1631 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1633 #if defined(TARGET_ALPHA)
1634 cpu_env
->ir
[IR_A4
] = host_pipe
[1];
1635 return host_pipe
[0];
1636 #elif defined(TARGET_MIPS)
1637 cpu_env
->active_tc
.gpr
[3] = host_pipe
[1];
1638 return host_pipe
[0];
1639 #elif defined(TARGET_SH4)
1640 cpu_env
->gregs
[1] = host_pipe
[1];
1641 return host_pipe
[0];
1642 #elif defined(TARGET_SPARC)
1643 cpu_env
->regwptr
[1] = host_pipe
[1];
1644 return host_pipe
[0];
1648 if (put_user_s32(host_pipe
[0], pipedes
)
1649 || put_user_s32(host_pipe
[1], pipedes
+ sizeof(abi_int
)))
1650 return -TARGET_EFAULT
;
1651 return get_errno(ret
);
1654 static inline abi_long
target_to_host_ip_mreq(struct ip_mreqn
*mreqn
,
1655 abi_ulong target_addr
,
1658 struct target_ip_mreqn
*target_smreqn
;
1660 target_smreqn
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1662 return -TARGET_EFAULT
;
1663 mreqn
->imr_multiaddr
.s_addr
= target_smreqn
->imr_multiaddr
.s_addr
;
1664 mreqn
->imr_address
.s_addr
= target_smreqn
->imr_address
.s_addr
;
1665 if (len
== sizeof(struct target_ip_mreqn
))
1666 mreqn
->imr_ifindex
= tswapal(target_smreqn
->imr_ifindex
);
1667 unlock_user(target_smreqn
, target_addr
, 0);
1672 static inline abi_long
target_to_host_sockaddr(int fd
, struct sockaddr
*addr
,
1673 abi_ulong target_addr
,
1676 const socklen_t unix_maxlen
= sizeof (struct sockaddr_un
);
1677 sa_family_t sa_family
;
1678 struct target_sockaddr
*target_saddr
;
1680 if (fd_trans_target_to_host_addr(fd
)) {
1681 return fd_trans_target_to_host_addr(fd
)(addr
, target_addr
, len
);
1684 target_saddr
= lock_user(VERIFY_READ
, target_addr
, len
, 1);
1686 return -TARGET_EFAULT
;
1688 sa_family
= tswap16(target_saddr
->sa_family
);
1690 /* Oops. The caller might send a incomplete sun_path; sun_path
1691 * must be terminated by \0 (see the manual page), but
1692 * unfortunately it is quite common to specify sockaddr_un
1693 * length as "strlen(x->sun_path)" while it should be
1694 * "strlen(...) + 1". We'll fix that here if needed.
1695 * Linux kernel has a similar feature.
1698 if (sa_family
== AF_UNIX
) {
1699 if (len
< unix_maxlen
&& len
> 0) {
1700 char *cp
= (char*)target_saddr
;
1702 if ( cp
[len
-1] && !cp
[len
] )
1705 if (len
> unix_maxlen
)
1709 memcpy(addr
, target_saddr
, len
);
1710 addr
->sa_family
= sa_family
;
1711 if (sa_family
== AF_NETLINK
) {
1712 struct sockaddr_nl
*nladdr
;
1714 nladdr
= (struct sockaddr_nl
*)addr
;
1715 nladdr
->nl_pid
= tswap32(nladdr
->nl_pid
);
1716 nladdr
->nl_groups
= tswap32(nladdr
->nl_groups
);
1717 } else if (sa_family
== AF_PACKET
) {
1718 struct target_sockaddr_ll
*lladdr
;
1720 lladdr
= (struct target_sockaddr_ll
*)addr
;
1721 lladdr
->sll_ifindex
= tswap32(lladdr
->sll_ifindex
);
1722 lladdr
->sll_hatype
= tswap16(lladdr
->sll_hatype
);
1723 } else if (sa_family
== AF_INET6
) {
1724 struct sockaddr_in6
*in6addr
;
1726 in6addr
= (struct sockaddr_in6
*)addr
;
1727 in6addr
->sin6_scope_id
= tswap32(in6addr
->sin6_scope_id
);
1729 unlock_user(target_saddr
, target_addr
, 0);
1734 static inline abi_long
host_to_target_sockaddr(abi_ulong target_addr
,
1735 struct sockaddr
*addr
,
1738 struct target_sockaddr
*target_saddr
;
1745 target_saddr
= lock_user(VERIFY_WRITE
, target_addr
, len
, 0);
1747 return -TARGET_EFAULT
;
1748 memcpy(target_saddr
, addr
, len
);
1749 if (len
>= offsetof(struct target_sockaddr
, sa_family
) +
1750 sizeof(target_saddr
->sa_family
)) {
1751 target_saddr
->sa_family
= tswap16(addr
->sa_family
);
1753 if (addr
->sa_family
== AF_NETLINK
&&
1754 len
>= sizeof(struct target_sockaddr_nl
)) {
1755 struct target_sockaddr_nl
*target_nl
=
1756 (struct target_sockaddr_nl
*)target_saddr
;
1757 target_nl
->nl_pid
= tswap32(target_nl
->nl_pid
);
1758 target_nl
->nl_groups
= tswap32(target_nl
->nl_groups
);
1759 } else if (addr
->sa_family
== AF_PACKET
) {
1760 struct sockaddr_ll
*target_ll
= (struct sockaddr_ll
*)target_saddr
;
1761 target_ll
->sll_ifindex
= tswap32(target_ll
->sll_ifindex
);
1762 target_ll
->sll_hatype
= tswap16(target_ll
->sll_hatype
);
1763 } else if (addr
->sa_family
== AF_INET6
&&
1764 len
>= sizeof(struct target_sockaddr_in6
)) {
1765 struct target_sockaddr_in6
*target_in6
=
1766 (struct target_sockaddr_in6
*)target_saddr
;
1767 target_in6
->sin6_scope_id
= tswap16(target_in6
->sin6_scope_id
);
1769 unlock_user(target_saddr
, target_addr
, len
);
1774 static inline abi_long
target_to_host_cmsg(struct msghdr
*msgh
,
1775 struct target_msghdr
*target_msgh
)
1777 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1778 abi_long msg_controllen
;
1779 abi_ulong target_cmsg_addr
;
1780 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1781 socklen_t space
= 0;
1783 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1784 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1786 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1787 target_cmsg
= lock_user(VERIFY_READ
, target_cmsg_addr
, msg_controllen
, 1);
1788 target_cmsg_start
= target_cmsg
;
1790 return -TARGET_EFAULT
;
1792 while (cmsg
&& target_cmsg
) {
1793 void *data
= CMSG_DATA(cmsg
);
1794 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1796 int len
= tswapal(target_cmsg
->cmsg_len
)
1797 - sizeof(struct target_cmsghdr
);
1799 space
+= CMSG_SPACE(len
);
1800 if (space
> msgh
->msg_controllen
) {
1801 space
-= CMSG_SPACE(len
);
1802 /* This is a QEMU bug, since we allocated the payload
1803 * area ourselves (unlike overflow in host-to-target
1804 * conversion, which is just the guest giving us a buffer
1805 * that's too small). It can't happen for the payload types
1806 * we currently support; if it becomes an issue in future
1807 * we would need to improve our allocation strategy to
1808 * something more intelligent than "twice the size of the
1809 * target buffer we're reading from".
1811 qemu_log_mask(LOG_UNIMP
,
1812 ("Unsupported ancillary data %d/%d: "
1813 "unhandled msg size\n"),
1814 tswap32(target_cmsg
->cmsg_level
),
1815 tswap32(target_cmsg
->cmsg_type
));
1819 if (tswap32(target_cmsg
->cmsg_level
) == TARGET_SOL_SOCKET
) {
1820 cmsg
->cmsg_level
= SOL_SOCKET
;
1822 cmsg
->cmsg_level
= tswap32(target_cmsg
->cmsg_level
);
1824 cmsg
->cmsg_type
= tswap32(target_cmsg
->cmsg_type
);
1825 cmsg
->cmsg_len
= CMSG_LEN(len
);
1827 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
1828 int *fd
= (int *)data
;
1829 int *target_fd
= (int *)target_data
;
1830 int i
, numfds
= len
/ sizeof(int);
1832 for (i
= 0; i
< numfds
; i
++) {
1833 __get_user(fd
[i
], target_fd
+ i
);
1835 } else if (cmsg
->cmsg_level
== SOL_SOCKET
1836 && cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
1837 struct ucred
*cred
= (struct ucred
*)data
;
1838 struct target_ucred
*target_cred
=
1839 (struct target_ucred
*)target_data
;
1841 __get_user(cred
->pid
, &target_cred
->pid
);
1842 __get_user(cred
->uid
, &target_cred
->uid
);
1843 __get_user(cred
->gid
, &target_cred
->gid
);
1844 } else if (cmsg
->cmsg_level
== SOL_ALG
) {
1845 uint32_t *dst
= (uint32_t *)data
;
1847 memcpy(dst
, target_data
, len
);
1848 /* fix endianess of first 32-bit word */
1849 if (len
>= sizeof(uint32_t)) {
1850 *dst
= tswap32(*dst
);
1853 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
1854 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1855 memcpy(data
, target_data
, len
);
1858 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
1859 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
1862 unlock_user(target_cmsg
, target_cmsg_addr
, 0);
1864 msgh
->msg_controllen
= space
;
1868 static inline abi_long
host_to_target_cmsg(struct target_msghdr
*target_msgh
,
1869 struct msghdr
*msgh
)
1871 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(msgh
);
1872 abi_long msg_controllen
;
1873 abi_ulong target_cmsg_addr
;
1874 struct target_cmsghdr
*target_cmsg
, *target_cmsg_start
;
1875 socklen_t space
= 0;
1877 msg_controllen
= tswapal(target_msgh
->msg_controllen
);
1878 if (msg_controllen
< sizeof (struct target_cmsghdr
))
1880 target_cmsg_addr
= tswapal(target_msgh
->msg_control
);
1881 target_cmsg
= lock_user(VERIFY_WRITE
, target_cmsg_addr
, msg_controllen
, 0);
1882 target_cmsg_start
= target_cmsg
;
1884 return -TARGET_EFAULT
;
1886 while (cmsg
&& target_cmsg
) {
1887 void *data
= CMSG_DATA(cmsg
);
1888 void *target_data
= TARGET_CMSG_DATA(target_cmsg
);
1890 int len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
1891 int tgt_len
, tgt_space
;
1893 /* We never copy a half-header but may copy half-data;
1894 * this is Linux's behaviour in put_cmsg(). Note that
1895 * truncation here is a guest problem (which we report
1896 * to the guest via the CTRUNC bit), unlike truncation
1897 * in target_to_host_cmsg, which is a QEMU bug.
1899 if (msg_controllen
< sizeof(struct target_cmsghdr
)) {
1900 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1904 if (cmsg
->cmsg_level
== SOL_SOCKET
) {
1905 target_cmsg
->cmsg_level
= tswap32(TARGET_SOL_SOCKET
);
1907 target_cmsg
->cmsg_level
= tswap32(cmsg
->cmsg_level
);
1909 target_cmsg
->cmsg_type
= tswap32(cmsg
->cmsg_type
);
1911 /* Payload types which need a different size of payload on
1912 * the target must adjust tgt_len here.
1915 switch (cmsg
->cmsg_level
) {
1917 switch (cmsg
->cmsg_type
) {
1919 tgt_len
= sizeof(struct target_timeval
);
1929 if (msg_controllen
< TARGET_CMSG_LEN(tgt_len
)) {
1930 target_msgh
->msg_flags
|= tswap32(MSG_CTRUNC
);
1931 tgt_len
= msg_controllen
- sizeof(struct target_cmsghdr
);
1934 /* We must now copy-and-convert len bytes of payload
1935 * into tgt_len bytes of destination space. Bear in mind
1936 * that in both source and destination we may be dealing
1937 * with a truncated value!
1939 switch (cmsg
->cmsg_level
) {
1941 switch (cmsg
->cmsg_type
) {
1944 int *fd
= (int *)data
;
1945 int *target_fd
= (int *)target_data
;
1946 int i
, numfds
= tgt_len
/ sizeof(int);
1948 for (i
= 0; i
< numfds
; i
++) {
1949 __put_user(fd
[i
], target_fd
+ i
);
1955 struct timeval
*tv
= (struct timeval
*)data
;
1956 struct target_timeval
*target_tv
=
1957 (struct target_timeval
*)target_data
;
1959 if (len
!= sizeof(struct timeval
) ||
1960 tgt_len
!= sizeof(struct target_timeval
)) {
1964 /* copy struct timeval to target */
1965 __put_user(tv
->tv_sec
, &target_tv
->tv_sec
);
1966 __put_user(tv
->tv_usec
, &target_tv
->tv_usec
);
1969 case SCM_CREDENTIALS
:
1971 struct ucred
*cred
= (struct ucred
*)data
;
1972 struct target_ucred
*target_cred
=
1973 (struct target_ucred
*)target_data
;
1975 __put_user(cred
->pid
, &target_cred
->pid
);
1976 __put_user(cred
->uid
, &target_cred
->uid
);
1977 __put_user(cred
->gid
, &target_cred
->gid
);
1986 switch (cmsg
->cmsg_type
) {
1989 uint32_t *v
= (uint32_t *)data
;
1990 uint32_t *t_int
= (uint32_t *)target_data
;
1992 if (len
!= sizeof(uint32_t) ||
1993 tgt_len
!= sizeof(uint32_t)) {
1996 __put_user(*v
, t_int
);
2002 struct sock_extended_err ee
;
2003 struct sockaddr_in offender
;
2005 struct errhdr_t
*errh
= (struct errhdr_t
*)data
;
2006 struct errhdr_t
*target_errh
=
2007 (struct errhdr_t
*)target_data
;
2009 if (len
!= sizeof(struct errhdr_t
) ||
2010 tgt_len
!= sizeof(struct errhdr_t
)) {
2013 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
2014 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
2015 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
2016 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
2017 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
2018 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
2019 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
2020 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
2021 (void *) &errh
->offender
, sizeof(errh
->offender
));
2030 switch (cmsg
->cmsg_type
) {
2033 uint32_t *v
= (uint32_t *)data
;
2034 uint32_t *t_int
= (uint32_t *)target_data
;
2036 if (len
!= sizeof(uint32_t) ||
2037 tgt_len
!= sizeof(uint32_t)) {
2040 __put_user(*v
, t_int
);
2046 struct sock_extended_err ee
;
2047 struct sockaddr_in6 offender
;
2049 struct errhdr6_t
*errh
= (struct errhdr6_t
*)data
;
2050 struct errhdr6_t
*target_errh
=
2051 (struct errhdr6_t
*)target_data
;
2053 if (len
!= sizeof(struct errhdr6_t
) ||
2054 tgt_len
!= sizeof(struct errhdr6_t
)) {
2057 __put_user(errh
->ee
.ee_errno
, &target_errh
->ee
.ee_errno
);
2058 __put_user(errh
->ee
.ee_origin
, &target_errh
->ee
.ee_origin
);
2059 __put_user(errh
->ee
.ee_type
, &target_errh
->ee
.ee_type
);
2060 __put_user(errh
->ee
.ee_code
, &target_errh
->ee
.ee_code
);
2061 __put_user(errh
->ee
.ee_pad
, &target_errh
->ee
.ee_pad
);
2062 __put_user(errh
->ee
.ee_info
, &target_errh
->ee
.ee_info
);
2063 __put_user(errh
->ee
.ee_data
, &target_errh
->ee
.ee_data
);
2064 host_to_target_sockaddr((unsigned long) &target_errh
->offender
,
2065 (void *) &errh
->offender
, sizeof(errh
->offender
));
2075 qemu_log_mask(LOG_UNIMP
, "Unsupported ancillary data: %d/%d\n",
2076 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
2077 memcpy(target_data
, data
, MIN(len
, tgt_len
));
2078 if (tgt_len
> len
) {
2079 memset(target_data
+ len
, 0, tgt_len
- len
);
2083 target_cmsg
->cmsg_len
= tswapal(TARGET_CMSG_LEN(tgt_len
));
2084 tgt_space
= TARGET_CMSG_SPACE(tgt_len
);
2085 if (msg_controllen
< tgt_space
) {
2086 tgt_space
= msg_controllen
;
2088 msg_controllen
-= tgt_space
;
2090 cmsg
= CMSG_NXTHDR(msgh
, cmsg
);
2091 target_cmsg
= TARGET_CMSG_NXTHDR(target_msgh
, target_cmsg
,
2094 unlock_user(target_cmsg
, target_cmsg_addr
, space
);
2096 target_msgh
->msg_controllen
= tswapal(space
);
2100 /* do_setsockopt() Must return target values and target errnos. */
2101 static abi_long
do_setsockopt(int sockfd
, int level
, int optname
,
2102 abi_ulong optval_addr
, socklen_t optlen
)
2106 struct ip_mreqn
*ip_mreq
;
2107 struct ip_mreq_source
*ip_mreq_source
;
2112 /* TCP and UDP options all take an 'int' value. */
2113 if (optlen
< sizeof(uint32_t))
2114 return -TARGET_EINVAL
;
2116 if (get_user_u32(val
, optval_addr
))
2117 return -TARGET_EFAULT
;
2118 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2125 case IP_ROUTER_ALERT
:
2129 case IP_MTU_DISCOVER
:
2136 case IP_MULTICAST_TTL
:
2137 case IP_MULTICAST_LOOP
:
2139 if (optlen
>= sizeof(uint32_t)) {
2140 if (get_user_u32(val
, optval_addr
))
2141 return -TARGET_EFAULT
;
2142 } else if (optlen
>= 1) {
2143 if (get_user_u8(val
, optval_addr
))
2144 return -TARGET_EFAULT
;
2146 ret
= get_errno(setsockopt(sockfd
, level
, optname
, &val
, sizeof(val
)));
2148 case IP_ADD_MEMBERSHIP
:
2149 case IP_DROP_MEMBERSHIP
:
2150 if (optlen
< sizeof (struct target_ip_mreq
) ||
2151 optlen
> sizeof (struct target_ip_mreqn
))
2152 return -TARGET_EINVAL
;
2154 ip_mreq
= (struct ip_mreqn
*) alloca(optlen
);
2155 target_to_host_ip_mreq(ip_mreq
, optval_addr
, optlen
);
2156 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq
, optlen
));
2159 case IP_BLOCK_SOURCE
:
2160 case IP_UNBLOCK_SOURCE
:
2161 case IP_ADD_SOURCE_MEMBERSHIP
:
2162 case IP_DROP_SOURCE_MEMBERSHIP
:
2163 if (optlen
!= sizeof (struct target_ip_mreq_source
))
2164 return -TARGET_EINVAL
;
2166 ip_mreq_source
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2167 if (!ip_mreq_source
) {
2168 return -TARGET_EFAULT
;
2170 ret
= get_errno(setsockopt(sockfd
, level
, optname
, ip_mreq_source
, optlen
));
2171 unlock_user (ip_mreq_source
, optval_addr
, 0);
2180 case IPV6_MTU_DISCOVER
:
2183 case IPV6_RECVPKTINFO
:
2184 case IPV6_UNICAST_HOPS
:
2185 case IPV6_MULTICAST_HOPS
:
2186 case IPV6_MULTICAST_LOOP
:
2188 case IPV6_RECVHOPLIMIT
:
2189 case IPV6_2292HOPLIMIT
:
2192 case IPV6_2292PKTINFO
:
2193 case IPV6_RECVTCLASS
:
2194 case IPV6_RECVRTHDR
:
2195 case IPV6_2292RTHDR
:
2196 case IPV6_RECVHOPOPTS
:
2197 case IPV6_2292HOPOPTS
:
2198 case IPV6_RECVDSTOPTS
:
2199 case IPV6_2292DSTOPTS
:
2201 case IPV6_ADDR_PREFERENCES
:
2202 #ifdef IPV6_RECVPATHMTU
2203 case IPV6_RECVPATHMTU
:
2205 #ifdef IPV6_TRANSPARENT
2206 case IPV6_TRANSPARENT
:
2208 #ifdef IPV6_FREEBIND
2211 #ifdef IPV6_RECVORIGDSTADDR
2212 case IPV6_RECVORIGDSTADDR
:
2215 if (optlen
< sizeof(uint32_t)) {
2216 return -TARGET_EINVAL
;
2218 if (get_user_u32(val
, optval_addr
)) {
2219 return -TARGET_EFAULT
;
2221 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2222 &val
, sizeof(val
)));
2226 struct in6_pktinfo pki
;
2228 if (optlen
< sizeof(pki
)) {
2229 return -TARGET_EINVAL
;
2232 if (copy_from_user(&pki
, optval_addr
, sizeof(pki
))) {
2233 return -TARGET_EFAULT
;
2236 pki
.ipi6_ifindex
= tswap32(pki
.ipi6_ifindex
);
2238 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2239 &pki
, sizeof(pki
)));
2242 case IPV6_ADD_MEMBERSHIP
:
2243 case IPV6_DROP_MEMBERSHIP
:
2245 struct ipv6_mreq ipv6mreq
;
2247 if (optlen
< sizeof(ipv6mreq
)) {
2248 return -TARGET_EINVAL
;
2251 if (copy_from_user(&ipv6mreq
, optval_addr
, sizeof(ipv6mreq
))) {
2252 return -TARGET_EFAULT
;
2255 ipv6mreq
.ipv6mr_interface
= tswap32(ipv6mreq
.ipv6mr_interface
);
2257 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2258 &ipv6mreq
, sizeof(ipv6mreq
)));
2269 struct icmp6_filter icmp6f
;
2271 if (optlen
> sizeof(icmp6f
)) {
2272 optlen
= sizeof(icmp6f
);
2275 if (copy_from_user(&icmp6f
, optval_addr
, optlen
)) {
2276 return -TARGET_EFAULT
;
2279 for (val
= 0; val
< 8; val
++) {
2280 icmp6f
.data
[val
] = tswap32(icmp6f
.data
[val
]);
2283 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2295 /* those take an u32 value */
2296 if (optlen
< sizeof(uint32_t)) {
2297 return -TARGET_EINVAL
;
2300 if (get_user_u32(val
, optval_addr
)) {
2301 return -TARGET_EFAULT
;
2303 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2304 &val
, sizeof(val
)));
2311 #if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2316 char *alg_key
= g_malloc(optlen
);
2319 return -TARGET_ENOMEM
;
2321 if (copy_from_user(alg_key
, optval_addr
, optlen
)) {
2323 return -TARGET_EFAULT
;
2325 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2330 case ALG_SET_AEAD_AUTHSIZE
:
2332 ret
= get_errno(setsockopt(sockfd
, level
, optname
,
2341 case TARGET_SOL_SOCKET
:
2343 case TARGET_SO_RCVTIMEO
:
2347 optname
= SO_RCVTIMEO
;
2350 if (optlen
!= sizeof(struct target_timeval
)) {
2351 return -TARGET_EINVAL
;
2354 if (copy_from_user_timeval(&tv
, optval_addr
)) {
2355 return -TARGET_EFAULT
;
2358 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2362 case TARGET_SO_SNDTIMEO
:
2363 optname
= SO_SNDTIMEO
;
2365 case TARGET_SO_ATTACH_FILTER
:
2367 struct target_sock_fprog
*tfprog
;
2368 struct target_sock_filter
*tfilter
;
2369 struct sock_fprog fprog
;
2370 struct sock_filter
*filter
;
2373 if (optlen
!= sizeof(*tfprog
)) {
2374 return -TARGET_EINVAL
;
2376 if (!lock_user_struct(VERIFY_READ
, tfprog
, optval_addr
, 0)) {
2377 return -TARGET_EFAULT
;
2379 if (!lock_user_struct(VERIFY_READ
, tfilter
,
2380 tswapal(tfprog
->filter
), 0)) {
2381 unlock_user_struct(tfprog
, optval_addr
, 1);
2382 return -TARGET_EFAULT
;
2385 fprog
.len
= tswap16(tfprog
->len
);
2386 filter
= g_try_new(struct sock_filter
, fprog
.len
);
2387 if (filter
== NULL
) {
2388 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2389 unlock_user_struct(tfprog
, optval_addr
, 1);
2390 return -TARGET_ENOMEM
;
2392 for (i
= 0; i
< fprog
.len
; i
++) {
2393 filter
[i
].code
= tswap16(tfilter
[i
].code
);
2394 filter
[i
].jt
= tfilter
[i
].jt
;
2395 filter
[i
].jf
= tfilter
[i
].jf
;
2396 filter
[i
].k
= tswap32(tfilter
[i
].k
);
2398 fprog
.filter
= filter
;
2400 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
,
2401 SO_ATTACH_FILTER
, &fprog
, sizeof(fprog
)));
2404 unlock_user_struct(tfilter
, tfprog
->filter
, 1);
2405 unlock_user_struct(tfprog
, optval_addr
, 1);
2408 case TARGET_SO_BINDTODEVICE
:
2410 char *dev_ifname
, *addr_ifname
;
2412 if (optlen
> IFNAMSIZ
- 1) {
2413 optlen
= IFNAMSIZ
- 1;
2415 dev_ifname
= lock_user(VERIFY_READ
, optval_addr
, optlen
, 1);
2417 return -TARGET_EFAULT
;
2419 optname
= SO_BINDTODEVICE
;
2420 addr_ifname
= alloca(IFNAMSIZ
);
2421 memcpy(addr_ifname
, dev_ifname
, optlen
);
2422 addr_ifname
[optlen
] = 0;
2423 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
,
2424 addr_ifname
, optlen
));
2425 unlock_user (dev_ifname
, optval_addr
, 0);
2428 case TARGET_SO_LINGER
:
2431 struct target_linger
*tlg
;
2433 if (optlen
!= sizeof(struct target_linger
)) {
2434 return -TARGET_EINVAL
;
2436 if (!lock_user_struct(VERIFY_READ
, tlg
, optval_addr
, 1)) {
2437 return -TARGET_EFAULT
;
2439 __get_user(lg
.l_onoff
, &tlg
->l_onoff
);
2440 __get_user(lg
.l_linger
, &tlg
->l_linger
);
2441 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, SO_LINGER
,
2443 unlock_user_struct(tlg
, optval_addr
, 0);
2446 /* Options with 'int' argument. */
2447 case TARGET_SO_DEBUG
:
2450 case TARGET_SO_REUSEADDR
:
2451 optname
= SO_REUSEADDR
;
2454 case TARGET_SO_REUSEPORT
:
2455 optname
= SO_REUSEPORT
;
2458 case TARGET_SO_TYPE
:
2461 case TARGET_SO_ERROR
:
2464 case TARGET_SO_DONTROUTE
:
2465 optname
= SO_DONTROUTE
;
2467 case TARGET_SO_BROADCAST
:
2468 optname
= SO_BROADCAST
;
2470 case TARGET_SO_SNDBUF
:
2471 optname
= SO_SNDBUF
;
2473 case TARGET_SO_SNDBUFFORCE
:
2474 optname
= SO_SNDBUFFORCE
;
2476 case TARGET_SO_RCVBUF
:
2477 optname
= SO_RCVBUF
;
2479 case TARGET_SO_RCVBUFFORCE
:
2480 optname
= SO_RCVBUFFORCE
;
2482 case TARGET_SO_KEEPALIVE
:
2483 optname
= SO_KEEPALIVE
;
2485 case TARGET_SO_OOBINLINE
:
2486 optname
= SO_OOBINLINE
;
2488 case TARGET_SO_NO_CHECK
:
2489 optname
= SO_NO_CHECK
;
2491 case TARGET_SO_PRIORITY
:
2492 optname
= SO_PRIORITY
;
2495 case TARGET_SO_BSDCOMPAT
:
2496 optname
= SO_BSDCOMPAT
;
2499 case TARGET_SO_PASSCRED
:
2500 optname
= SO_PASSCRED
;
2502 case TARGET_SO_PASSSEC
:
2503 optname
= SO_PASSSEC
;
2505 case TARGET_SO_TIMESTAMP
:
2506 optname
= SO_TIMESTAMP
;
2508 case TARGET_SO_RCVLOWAT
:
2509 optname
= SO_RCVLOWAT
;
2514 if (optlen
< sizeof(uint32_t))
2515 return -TARGET_EINVAL
;
2517 if (get_user_u32(val
, optval_addr
))
2518 return -TARGET_EFAULT
;
2519 ret
= get_errno(setsockopt(sockfd
, SOL_SOCKET
, optname
, &val
, sizeof(val
)));
2524 case NETLINK_PKTINFO
:
2525 case NETLINK_ADD_MEMBERSHIP
:
2526 case NETLINK_DROP_MEMBERSHIP
:
2527 case NETLINK_BROADCAST_ERROR
:
2528 case NETLINK_NO_ENOBUFS
:
2529 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2530 case NETLINK_LISTEN_ALL_NSID
:
2531 case NETLINK_CAP_ACK
:
2532 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2533 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2534 case NETLINK_EXT_ACK
:
2535 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2536 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2537 case NETLINK_GET_STRICT_CHK
:
2538 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2544 if (optlen
< sizeof(uint32_t)) {
2545 return -TARGET_EINVAL
;
2547 if (get_user_u32(val
, optval_addr
)) {
2548 return -TARGET_EFAULT
;
2550 ret
= get_errno(setsockopt(sockfd
, SOL_NETLINK
, optname
, &val
,
2553 #endif /* SOL_NETLINK */
2556 qemu_log_mask(LOG_UNIMP
, "Unsupported setsockopt level=%d optname=%d\n",
2558 ret
= -TARGET_ENOPROTOOPT
;
2563 /* do_getsockopt() Must return target values and target errnos. */
2564 static abi_long
do_getsockopt(int sockfd
, int level
, int optname
,
2565 abi_ulong optval_addr
, abi_ulong optlen
)
2572 case TARGET_SOL_SOCKET
:
2575 /* These don't just return a single integer */
2576 case TARGET_SO_PEERNAME
:
2578 case TARGET_SO_RCVTIMEO
: {
2582 optname
= SO_RCVTIMEO
;
2585 if (get_user_u32(len
, optlen
)) {
2586 return -TARGET_EFAULT
;
2589 return -TARGET_EINVAL
;
2593 ret
= get_errno(getsockopt(sockfd
, level
, optname
,
2598 if (len
> sizeof(struct target_timeval
)) {
2599 len
= sizeof(struct target_timeval
);
2601 if (copy_to_user_timeval(optval_addr
, &tv
)) {
2602 return -TARGET_EFAULT
;
2604 if (put_user_u32(len
, optlen
)) {
2605 return -TARGET_EFAULT
;
2609 case TARGET_SO_SNDTIMEO
:
2610 optname
= SO_SNDTIMEO
;
2612 case TARGET_SO_PEERCRED
: {
2615 struct target_ucred
*tcr
;
2617 if (get_user_u32(len
, optlen
)) {
2618 return -TARGET_EFAULT
;
2621 return -TARGET_EINVAL
;
2625 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERCRED
,
2633 if (!lock_user_struct(VERIFY_WRITE
, tcr
, optval_addr
, 0)) {
2634 return -TARGET_EFAULT
;
2636 __put_user(cr
.pid
, &tcr
->pid
);
2637 __put_user(cr
.uid
, &tcr
->uid
);
2638 __put_user(cr
.gid
, &tcr
->gid
);
2639 unlock_user_struct(tcr
, optval_addr
, 1);
2640 if (put_user_u32(len
, optlen
)) {
2641 return -TARGET_EFAULT
;
2645 case TARGET_SO_PEERSEC
: {
2648 if (get_user_u32(len
, optlen
)) {
2649 return -TARGET_EFAULT
;
2652 return -TARGET_EINVAL
;
2654 name
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 0);
2656 return -TARGET_EFAULT
;
2659 ret
= get_errno(getsockopt(sockfd
, level
, SO_PEERSEC
,
2661 if (put_user_u32(lv
, optlen
)) {
2662 ret
= -TARGET_EFAULT
;
2664 unlock_user(name
, optval_addr
, lv
);
2667 case TARGET_SO_LINGER
:
2671 struct target_linger
*tlg
;
2673 if (get_user_u32(len
, optlen
)) {
2674 return -TARGET_EFAULT
;
2677 return -TARGET_EINVAL
;
2681 ret
= get_errno(getsockopt(sockfd
, level
, SO_LINGER
,
2689 if (!lock_user_struct(VERIFY_WRITE
, tlg
, optval_addr
, 0)) {
2690 return -TARGET_EFAULT
;
2692 __put_user(lg
.l_onoff
, &tlg
->l_onoff
);
2693 __put_user(lg
.l_linger
, &tlg
->l_linger
);
2694 unlock_user_struct(tlg
, optval_addr
, 1);
2695 if (put_user_u32(len
, optlen
)) {
2696 return -TARGET_EFAULT
;
2700 /* Options with 'int' argument. */
2701 case TARGET_SO_DEBUG
:
2704 case TARGET_SO_REUSEADDR
:
2705 optname
= SO_REUSEADDR
;
2708 case TARGET_SO_REUSEPORT
:
2709 optname
= SO_REUSEPORT
;
2712 case TARGET_SO_TYPE
:
2715 case TARGET_SO_ERROR
:
2718 case TARGET_SO_DONTROUTE
:
2719 optname
= SO_DONTROUTE
;
2721 case TARGET_SO_BROADCAST
:
2722 optname
= SO_BROADCAST
;
2724 case TARGET_SO_SNDBUF
:
2725 optname
= SO_SNDBUF
;
2727 case TARGET_SO_RCVBUF
:
2728 optname
= SO_RCVBUF
;
2730 case TARGET_SO_KEEPALIVE
:
2731 optname
= SO_KEEPALIVE
;
2733 case TARGET_SO_OOBINLINE
:
2734 optname
= SO_OOBINLINE
;
2736 case TARGET_SO_NO_CHECK
:
2737 optname
= SO_NO_CHECK
;
2739 case TARGET_SO_PRIORITY
:
2740 optname
= SO_PRIORITY
;
2743 case TARGET_SO_BSDCOMPAT
:
2744 optname
= SO_BSDCOMPAT
;
2747 case TARGET_SO_PASSCRED
:
2748 optname
= SO_PASSCRED
;
2750 case TARGET_SO_TIMESTAMP
:
2751 optname
= SO_TIMESTAMP
;
2753 case TARGET_SO_RCVLOWAT
:
2754 optname
= SO_RCVLOWAT
;
2756 case TARGET_SO_ACCEPTCONN
:
2757 optname
= SO_ACCEPTCONN
;
2759 case TARGET_SO_PROTOCOL
:
2760 optname
= SO_PROTOCOL
;
2762 case TARGET_SO_DOMAIN
:
2763 optname
= SO_DOMAIN
;
2771 /* TCP and UDP options all take an 'int' value. */
2773 if (get_user_u32(len
, optlen
))
2774 return -TARGET_EFAULT
;
2776 return -TARGET_EINVAL
;
2778 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2783 val
= host_to_target_sock_type(val
);
2786 val
= host_to_target_errno(val
);
2792 if (put_user_u32(val
, optval_addr
))
2793 return -TARGET_EFAULT
;
2795 if (put_user_u8(val
, optval_addr
))
2796 return -TARGET_EFAULT
;
2798 if (put_user_u32(len
, optlen
))
2799 return -TARGET_EFAULT
;
2806 case IP_ROUTER_ALERT
:
2810 case IP_MTU_DISCOVER
:
2816 case IP_MULTICAST_TTL
:
2817 case IP_MULTICAST_LOOP
:
2818 if (get_user_u32(len
, optlen
))
2819 return -TARGET_EFAULT
;
2821 return -TARGET_EINVAL
;
2823 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2826 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2828 if (put_user_u32(len
, optlen
)
2829 || put_user_u8(val
, optval_addr
))
2830 return -TARGET_EFAULT
;
2832 if (len
> sizeof(int))
2834 if (put_user_u32(len
, optlen
)
2835 || put_user_u32(val
, optval_addr
))
2836 return -TARGET_EFAULT
;
2840 ret
= -TARGET_ENOPROTOOPT
;
2846 case IPV6_MTU_DISCOVER
:
2849 case IPV6_RECVPKTINFO
:
2850 case IPV6_UNICAST_HOPS
:
2851 case IPV6_MULTICAST_HOPS
:
2852 case IPV6_MULTICAST_LOOP
:
2854 case IPV6_RECVHOPLIMIT
:
2855 case IPV6_2292HOPLIMIT
:
2858 case IPV6_2292PKTINFO
:
2859 case IPV6_RECVTCLASS
:
2860 case IPV6_RECVRTHDR
:
2861 case IPV6_2292RTHDR
:
2862 case IPV6_RECVHOPOPTS
:
2863 case IPV6_2292HOPOPTS
:
2864 case IPV6_RECVDSTOPTS
:
2865 case IPV6_2292DSTOPTS
:
2867 case IPV6_ADDR_PREFERENCES
:
2868 #ifdef IPV6_RECVPATHMTU
2869 case IPV6_RECVPATHMTU
:
2871 #ifdef IPV6_TRANSPARENT
2872 case IPV6_TRANSPARENT
:
2874 #ifdef IPV6_FREEBIND
2877 #ifdef IPV6_RECVORIGDSTADDR
2878 case IPV6_RECVORIGDSTADDR
:
2880 if (get_user_u32(len
, optlen
))
2881 return -TARGET_EFAULT
;
2883 return -TARGET_EINVAL
;
2885 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2888 if (len
< sizeof(int) && len
> 0 && val
>= 0 && val
< 255) {
2890 if (put_user_u32(len
, optlen
)
2891 || put_user_u8(val
, optval_addr
))
2892 return -TARGET_EFAULT
;
2894 if (len
> sizeof(int))
2896 if (put_user_u32(len
, optlen
)
2897 || put_user_u32(val
, optval_addr
))
2898 return -TARGET_EFAULT
;
2902 ret
= -TARGET_ENOPROTOOPT
;
2909 case NETLINK_PKTINFO
:
2910 case NETLINK_BROADCAST_ERROR
:
2911 case NETLINK_NO_ENOBUFS
:
2912 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2913 case NETLINK_LISTEN_ALL_NSID
:
2914 case NETLINK_CAP_ACK
:
2915 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2916 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2917 case NETLINK_EXT_ACK
:
2918 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2919 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2920 case NETLINK_GET_STRICT_CHK
:
2921 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2922 if (get_user_u32(len
, optlen
)) {
2923 return -TARGET_EFAULT
;
2925 if (len
!= sizeof(val
)) {
2926 return -TARGET_EINVAL
;
2929 ret
= get_errno(getsockopt(sockfd
, level
, optname
, &val
, &lv
));
2933 if (put_user_u32(lv
, optlen
)
2934 || put_user_u32(val
, optval_addr
)) {
2935 return -TARGET_EFAULT
;
2938 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2939 case NETLINK_LIST_MEMBERSHIPS
:
2943 if (get_user_u32(len
, optlen
)) {
2944 return -TARGET_EFAULT
;
2947 return -TARGET_EINVAL
;
2949 results
= lock_user(VERIFY_WRITE
, optval_addr
, len
, 1);
2950 if (!results
&& len
> 0) {
2951 return -TARGET_EFAULT
;
2954 ret
= get_errno(getsockopt(sockfd
, level
, optname
, results
, &lv
));
2956 unlock_user(results
, optval_addr
, 0);
2959 /* swap host endianess to target endianess. */
2960 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
2961 results
[i
] = tswap32(results
[i
]);
2963 if (put_user_u32(lv
, optlen
)) {
2964 return -TARGET_EFAULT
;
2966 unlock_user(results
, optval_addr
, 0);
2969 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2974 #endif /* SOL_NETLINK */
2977 qemu_log_mask(LOG_UNIMP
,
2978 "getsockopt level=%d optname=%d not yet supported\n",
2980 ret
= -TARGET_EOPNOTSUPP
;
2986 /* Convert target low/high pair representing file offset into the host
2987 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2988 * as the kernel doesn't handle them either.
2990 static void target_to_host_low_high(abi_ulong tlow
,
2992 unsigned long *hlow
,
2993 unsigned long *hhigh
)
2995 uint64_t off
= tlow
|
2996 ((unsigned long long)thigh
<< TARGET_LONG_BITS
/ 2) <<
2997 TARGET_LONG_BITS
/ 2;
3000 *hhigh
= (off
>> HOST_LONG_BITS
/ 2) >> HOST_LONG_BITS
/ 2;
3003 static struct iovec
*lock_iovec(int type
, abi_ulong target_addr
,
3004 abi_ulong count
, int copy
)
3006 struct target_iovec
*target_vec
;
3008 abi_ulong total_len
, max_len
;
3011 bool bad_address
= false;
3017 if (count
> IOV_MAX
) {
3022 vec
= g_try_new0(struct iovec
, count
);
3028 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3029 count
* sizeof(struct target_iovec
), 1);
3030 if (target_vec
== NULL
) {
3035 /* ??? If host page size > target page size, this will result in a
3036 value larger than what we can actually support. */
3037 max_len
= 0x7fffffff & TARGET_PAGE_MASK
;
3040 for (i
= 0; i
< count
; i
++) {
3041 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3042 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3047 } else if (len
== 0) {
3048 /* Zero length pointer is ignored. */
3049 vec
[i
].iov_base
= 0;
3051 vec
[i
].iov_base
= lock_user(type
, base
, len
, copy
);
3052 /* If the first buffer pointer is bad, this is a fault. But
3053 * subsequent bad buffers will result in a partial write; this
3054 * is realized by filling the vector with null pointers and
3056 if (!vec
[i
].iov_base
) {
3067 if (len
> max_len
- total_len
) {
3068 len
= max_len
- total_len
;
3071 vec
[i
].iov_len
= len
;
3075 unlock_user(target_vec
, target_addr
, 0);
3080 if (tswapal(target_vec
[i
].iov_len
) > 0) {
3081 unlock_user(vec
[i
].iov_base
, tswapal(target_vec
[i
].iov_base
), 0);
3084 unlock_user(target_vec
, target_addr
, 0);
3091 static void unlock_iovec(struct iovec
*vec
, abi_ulong target_addr
,
3092 abi_ulong count
, int copy
)
3094 struct target_iovec
*target_vec
;
3097 target_vec
= lock_user(VERIFY_READ
, target_addr
,
3098 count
* sizeof(struct target_iovec
), 1);
3100 for (i
= 0; i
< count
; i
++) {
3101 abi_ulong base
= tswapal(target_vec
[i
].iov_base
);
3102 abi_long len
= tswapal(target_vec
[i
].iov_len
);
3106 unlock_user(vec
[i
].iov_base
, base
, copy
? vec
[i
].iov_len
: 0);
3108 unlock_user(target_vec
, target_addr
, 0);
3114 static inline int target_to_host_sock_type(int *type
)
3117 int target_type
= *type
;
3119 switch (target_type
& TARGET_SOCK_TYPE_MASK
) {
3120 case TARGET_SOCK_DGRAM
:
3121 host_type
= SOCK_DGRAM
;
3123 case TARGET_SOCK_STREAM
:
3124 host_type
= SOCK_STREAM
;
3127 host_type
= target_type
& TARGET_SOCK_TYPE_MASK
;
3130 if (target_type
& TARGET_SOCK_CLOEXEC
) {
3131 #if defined(SOCK_CLOEXEC)
3132 host_type
|= SOCK_CLOEXEC
;
3134 return -TARGET_EINVAL
;
3137 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3138 #if defined(SOCK_NONBLOCK)
3139 host_type
|= SOCK_NONBLOCK
;
3140 #elif !defined(O_NONBLOCK)
3141 return -TARGET_EINVAL
;
3148 /* Try to emulate socket type flags after socket creation. */
3149 static int sock_flags_fixup(int fd
, int target_type
)
3151 #if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3152 if (target_type
& TARGET_SOCK_NONBLOCK
) {
3153 int flags
= fcntl(fd
, F_GETFL
);
3154 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
| flags
) == -1) {
3156 return -TARGET_EINVAL
;
3163 /* do_socket() Must return target values and target errnos. */
3164 static abi_long
do_socket(int domain
, int type
, int protocol
)
3166 int target_type
= type
;
3169 ret
= target_to_host_sock_type(&type
);
3174 if (domain
== PF_NETLINK
&& !(
3175 #ifdef CONFIG_RTNETLINK
3176 protocol
== NETLINK_ROUTE
||
3178 protocol
== NETLINK_KOBJECT_UEVENT
||
3179 protocol
== NETLINK_AUDIT
)) {
3180 return -TARGET_EPROTONOSUPPORT
;
3183 if (domain
== AF_PACKET
||
3184 (domain
== AF_INET
&& type
== SOCK_PACKET
)) {
3185 protocol
= tswap16(protocol
);
3188 ret
= get_errno(socket(domain
, type
, protocol
));
3190 ret
= sock_flags_fixup(ret
, target_type
);
3191 if (type
== SOCK_PACKET
) {
3192 /* Manage an obsolete case :
3193 * if socket type is SOCK_PACKET, bind by name
3195 fd_trans_register(ret
, &target_packet_trans
);
3196 } else if (domain
== PF_NETLINK
) {
3198 #ifdef CONFIG_RTNETLINK
3200 fd_trans_register(ret
, &target_netlink_route_trans
);
3203 case NETLINK_KOBJECT_UEVENT
:
3204 /* nothing to do: messages are strings */
3207 fd_trans_register(ret
, &target_netlink_audit_trans
);
3210 g_assert_not_reached();
3217 /* do_bind() Must return target values and target errnos. */
3218 static abi_long
do_bind(int sockfd
, abi_ulong target_addr
,
3224 if ((int)addrlen
< 0) {
3225 return -TARGET_EINVAL
;
3228 addr
= alloca(addrlen
+1);
3230 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3234 return get_errno(bind(sockfd
, addr
, addrlen
));
3237 /* do_connect() Must return target values and target errnos. */
3238 static abi_long
do_connect(int sockfd
, abi_ulong target_addr
,
3244 if ((int)addrlen
< 0) {
3245 return -TARGET_EINVAL
;
3248 addr
= alloca(addrlen
+1);
3250 ret
= target_to_host_sockaddr(sockfd
, addr
, target_addr
, addrlen
);
3254 return get_errno(safe_connect(sockfd
, addr
, addrlen
));
3257 /* do_sendrecvmsg_locked() Must return target values and target errnos. */
3258 static abi_long
do_sendrecvmsg_locked(int fd
, struct target_msghdr
*msgp
,
3259 int flags
, int send
)
3265 abi_ulong target_vec
;
3267 if (msgp
->msg_name
) {
3268 msg
.msg_namelen
= tswap32(msgp
->msg_namelen
);
3269 msg
.msg_name
= alloca(msg
.msg_namelen
+1);
3270 ret
= target_to_host_sockaddr(fd
, msg
.msg_name
,
3271 tswapal(msgp
->msg_name
),
3273 if (ret
== -TARGET_EFAULT
) {
3274 /* For connected sockets msg_name and msg_namelen must
3275 * be ignored, so returning EFAULT immediately is wrong.
3276 * Instead, pass a bad msg_name to the host kernel, and
3277 * let it decide whether to return EFAULT or not.
3279 msg
.msg_name
= (void *)-1;
3284 msg
.msg_name
= NULL
;
3285 msg
.msg_namelen
= 0;
3287 msg
.msg_controllen
= 2 * tswapal(msgp
->msg_controllen
);
3288 msg
.msg_control
= alloca(msg
.msg_controllen
);
3289 memset(msg
.msg_control
, 0, msg
.msg_controllen
);
3291 msg
.msg_flags
= tswap32(msgp
->msg_flags
);
3293 count
= tswapal(msgp
->msg_iovlen
);
3294 target_vec
= tswapal(msgp
->msg_iov
);
3296 if (count
> IOV_MAX
) {
3297 /* sendrcvmsg returns a different errno for this condition than
3298 * readv/writev, so we must catch it here before lock_iovec() does.
3300 ret
= -TARGET_EMSGSIZE
;
3304 vec
= lock_iovec(send
? VERIFY_READ
: VERIFY_WRITE
,
3305 target_vec
, count
, send
);
3307 ret
= -host_to_target_errno(errno
);
3308 /* allow sending packet without any iov, e.g. with MSG_MORE flag */
3313 msg
.msg_iovlen
= count
;
3317 if (fd_trans_target_to_host_data(fd
)) {
3320 host_msg
= g_malloc(msg
.msg_iov
->iov_len
);
3321 memcpy(host_msg
, msg
.msg_iov
->iov_base
, msg
.msg_iov
->iov_len
);
3322 ret
= fd_trans_target_to_host_data(fd
)(host_msg
,
3323 msg
.msg_iov
->iov_len
);
3325 msg
.msg_iov
->iov_base
= host_msg
;
3326 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3330 ret
= target_to_host_cmsg(&msg
, msgp
);
3332 ret
= get_errno(safe_sendmsg(fd
, &msg
, flags
));
3336 ret
= get_errno(safe_recvmsg(fd
, &msg
, flags
));
3337 if (!is_error(ret
)) {
3339 if (fd_trans_host_to_target_data(fd
)) {
3340 ret
= fd_trans_host_to_target_data(fd
)(msg
.msg_iov
->iov_base
,
3341 MIN(msg
.msg_iov
->iov_len
, len
));
3343 if (!is_error(ret
)) {
3344 ret
= host_to_target_cmsg(msgp
, &msg
);
3346 if (!is_error(ret
)) {
3347 msgp
->msg_namelen
= tswap32(msg
.msg_namelen
);
3348 msgp
->msg_flags
= tswap32(msg
.msg_flags
);
3349 if (msg
.msg_name
!= NULL
&& msg
.msg_name
!= (void *)-1) {
3350 ret
= host_to_target_sockaddr(tswapal(msgp
->msg_name
),
3351 msg
.msg_name
, msg
.msg_namelen
);
3364 unlock_iovec(vec
, target_vec
, count
, !send
);
3370 static abi_long
do_sendrecvmsg(int fd
, abi_ulong target_msg
,
3371 int flags
, int send
)
3374 struct target_msghdr
*msgp
;
3376 if (!lock_user_struct(send
? VERIFY_READ
: VERIFY_WRITE
,
3380 return -TARGET_EFAULT
;
3382 ret
= do_sendrecvmsg_locked(fd
, msgp
, flags
, send
);
3383 unlock_user_struct(msgp
, target_msg
, send
? 0 : 1);
3387 /* We don't rely on the C library to have sendmmsg/recvmmsg support,
3388 * so it might not have this *mmsg-specific flag either.
3390 #ifndef MSG_WAITFORONE
3391 #define MSG_WAITFORONE 0x10000
3394 static abi_long
do_sendrecvmmsg(int fd
, abi_ulong target_msgvec
,
3395 unsigned int vlen
, unsigned int flags
,
3398 struct target_mmsghdr
*mmsgp
;
3402 if (vlen
> UIO_MAXIOV
) {
3406 mmsgp
= lock_user(VERIFY_WRITE
, target_msgvec
, sizeof(*mmsgp
) * vlen
, 1);
3408 return -TARGET_EFAULT
;
3411 for (i
= 0; i
< vlen
; i
++) {
3412 ret
= do_sendrecvmsg_locked(fd
, &mmsgp
[i
].msg_hdr
, flags
, send
);
3413 if (is_error(ret
)) {
3416 mmsgp
[i
].msg_len
= tswap32(ret
);
3417 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3418 if (flags
& MSG_WAITFORONE
) {
3419 flags
|= MSG_DONTWAIT
;
3423 unlock_user(mmsgp
, target_msgvec
, sizeof(*mmsgp
) * i
);
3425 /* Return number of datagrams sent if we sent any at all;
3426 * otherwise return the error.
3434 /* do_accept4() Must return target values and target errnos. */
3435 static abi_long
do_accept4(int fd
, abi_ulong target_addr
,
3436 abi_ulong target_addrlen_addr
, int flags
)
3438 socklen_t addrlen
, ret_addrlen
;
3443 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
3445 if (target_addr
== 0) {
3446 return get_errno(safe_accept4(fd
, NULL
, NULL
, host_flags
));
3449 /* linux returns EFAULT if addrlen pointer is invalid */
3450 if (get_user_u32(addrlen
, target_addrlen_addr
))
3451 return -TARGET_EFAULT
;
3453 if ((int)addrlen
< 0) {
3454 return -TARGET_EINVAL
;
3457 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3458 return -TARGET_EFAULT
;
3461 addr
= alloca(addrlen
);
3463 ret_addrlen
= addrlen
;
3464 ret
= get_errno(safe_accept4(fd
, addr
, &ret_addrlen
, host_flags
));
3465 if (!is_error(ret
)) {
3466 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3467 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3468 ret
= -TARGET_EFAULT
;
3474 /* do_getpeername() Must return target values and target errnos. */
3475 static abi_long
do_getpeername(int fd
, abi_ulong target_addr
,
3476 abi_ulong target_addrlen_addr
)
3478 socklen_t addrlen
, ret_addrlen
;
3482 if (get_user_u32(addrlen
, target_addrlen_addr
))
3483 return -TARGET_EFAULT
;
3485 if ((int)addrlen
< 0) {
3486 return -TARGET_EINVAL
;
3489 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3490 return -TARGET_EFAULT
;
3493 addr
= alloca(addrlen
);
3495 ret_addrlen
= addrlen
;
3496 ret
= get_errno(getpeername(fd
, addr
, &ret_addrlen
));
3497 if (!is_error(ret
)) {
3498 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3499 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3500 ret
= -TARGET_EFAULT
;
3506 /* do_getsockname() Must return target values and target errnos. */
3507 static abi_long
do_getsockname(int fd
, abi_ulong target_addr
,
3508 abi_ulong target_addrlen_addr
)
3510 socklen_t addrlen
, ret_addrlen
;
3514 if (get_user_u32(addrlen
, target_addrlen_addr
))
3515 return -TARGET_EFAULT
;
3517 if ((int)addrlen
< 0) {
3518 return -TARGET_EINVAL
;
3521 if (!access_ok(thread_cpu
, VERIFY_WRITE
, target_addr
, addrlen
)) {
3522 return -TARGET_EFAULT
;
3525 addr
= alloca(addrlen
);
3527 ret_addrlen
= addrlen
;
3528 ret
= get_errno(getsockname(fd
, addr
, &ret_addrlen
));
3529 if (!is_error(ret
)) {
3530 host_to_target_sockaddr(target_addr
, addr
, MIN(addrlen
, ret_addrlen
));
3531 if (put_user_u32(ret_addrlen
, target_addrlen_addr
)) {
3532 ret
= -TARGET_EFAULT
;
3538 /* do_socketpair() Must return target values and target errnos. */
3539 static abi_long
do_socketpair(int domain
, int type
, int protocol
,
3540 abi_ulong target_tab_addr
)
3545 target_to_host_sock_type(&type
);
3547 ret
= get_errno(socketpair(domain
, type
, protocol
, tab
));
3548 if (!is_error(ret
)) {
3549 if (put_user_s32(tab
[0], target_tab_addr
)
3550 || put_user_s32(tab
[1], target_tab_addr
+ sizeof(tab
[0])))
3551 ret
= -TARGET_EFAULT
;
3556 /* do_sendto() Must return target values and target errnos. */
3557 static abi_long
do_sendto(int fd
, abi_ulong msg
, size_t len
, int flags
,
3558 abi_ulong target_addr
, socklen_t addrlen
)
3562 void *copy_msg
= NULL
;
3565 if ((int)addrlen
< 0) {
3566 return -TARGET_EINVAL
;
3569 host_msg
= lock_user(VERIFY_READ
, msg
, len
, 1);
3571 return -TARGET_EFAULT
;
3572 if (fd_trans_target_to_host_data(fd
)) {
3573 copy_msg
= host_msg
;
3574 host_msg
= g_malloc(len
);
3575 memcpy(host_msg
, copy_msg
, len
);
3576 ret
= fd_trans_target_to_host_data(fd
)(host_msg
, len
);
3582 addr
= alloca(addrlen
+1);
3583 ret
= target_to_host_sockaddr(fd
, addr
, target_addr
, addrlen
);
3587 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, addr
, addrlen
));
3589 ret
= get_errno(safe_sendto(fd
, host_msg
, len
, flags
, NULL
, 0));
3594 host_msg
= copy_msg
;
3596 unlock_user(host_msg
, msg
, 0);
3600 /* do_recvfrom() Must return target values and target errnos. */
3601 static abi_long
do_recvfrom(int fd
, abi_ulong msg
, size_t len
, int flags
,
3602 abi_ulong target_addr
,
3603 abi_ulong target_addrlen
)
3605 socklen_t addrlen
, ret_addrlen
;
3613 host_msg
= lock_user(VERIFY_WRITE
, msg
, len
, 0);
3615 return -TARGET_EFAULT
;
3619 if (get_user_u32(addrlen
, target_addrlen
)) {
3620 ret
= -TARGET_EFAULT
;
3623 if ((int)addrlen
< 0) {
3624 ret
= -TARGET_EINVAL
;
3627 addr
= alloca(addrlen
);
3628 ret_addrlen
= addrlen
;
3629 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
,
3630 addr
, &ret_addrlen
));
3632 addr
= NULL
; /* To keep compiler quiet. */
3633 addrlen
= 0; /* To keep compiler quiet. */
3634 ret
= get_errno(safe_recvfrom(fd
, host_msg
, len
, flags
, NULL
, 0));
3636 if (!is_error(ret
)) {
3637 if (fd_trans_host_to_target_data(fd
)) {
3639 trans
= fd_trans_host_to_target_data(fd
)(host_msg
, MIN(ret
, len
));
3640 if (is_error(trans
)) {
3646 host_to_target_sockaddr(target_addr
, addr
,
3647 MIN(addrlen
, ret_addrlen
));
3648 if (put_user_u32(ret_addrlen
, target_addrlen
)) {
3649 ret
= -TARGET_EFAULT
;
3653 unlock_user(host_msg
, msg
, len
);
3656 unlock_user(host_msg
, msg
, 0);
3661 #ifdef TARGET_NR_socketcall
3662 /* do_socketcall() must return target values and target errnos. */
3663 static abi_long
do_socketcall(int num
, abi_ulong vptr
)
3665 static const unsigned nargs
[] = { /* number of arguments per operation */
3666 [TARGET_SYS_SOCKET
] = 3, /* domain, type, protocol */
3667 [TARGET_SYS_BIND
] = 3, /* fd, addr, addrlen */
3668 [TARGET_SYS_CONNECT
] = 3, /* fd, addr, addrlen */
3669 [TARGET_SYS_LISTEN
] = 2, /* fd, backlog */
3670 [TARGET_SYS_ACCEPT
] = 3, /* fd, addr, addrlen */
3671 [TARGET_SYS_GETSOCKNAME
] = 3, /* fd, addr, addrlen */
3672 [TARGET_SYS_GETPEERNAME
] = 3, /* fd, addr, addrlen */
3673 [TARGET_SYS_SOCKETPAIR
] = 4, /* domain, type, protocol, tab */
3674 [TARGET_SYS_SEND
] = 4, /* fd, msg, len, flags */
3675 [TARGET_SYS_RECV
] = 4, /* fd, msg, len, flags */
3676 [TARGET_SYS_SENDTO
] = 6, /* fd, msg, len, flags, addr, addrlen */
3677 [TARGET_SYS_RECVFROM
] = 6, /* fd, msg, len, flags, addr, addrlen */
3678 [TARGET_SYS_SHUTDOWN
] = 2, /* fd, how */
3679 [TARGET_SYS_SETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
3680 [TARGET_SYS_GETSOCKOPT
] = 5, /* fd, level, optname, optval, optlen */
3681 [TARGET_SYS_SENDMSG
] = 3, /* fd, msg, flags */
3682 [TARGET_SYS_RECVMSG
] = 3, /* fd, msg, flags */
3683 [TARGET_SYS_ACCEPT4
] = 4, /* fd, addr, addrlen, flags */
3684 [TARGET_SYS_RECVMMSG
] = 4, /* fd, msgvec, vlen, flags */
3685 [TARGET_SYS_SENDMMSG
] = 4, /* fd, msgvec, vlen, flags */
3687 abi_long a
[6]; /* max 6 args */
3690 /* check the range of the first argument num */
3691 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3692 if (num
< 1 || num
> TARGET_SYS_SENDMMSG
) {
3693 return -TARGET_EINVAL
;
3695 /* ensure we have space for args */
3696 if (nargs
[num
] > ARRAY_SIZE(a
)) {
3697 return -TARGET_EINVAL
;
3699 /* collect the arguments in a[] according to nargs[] */
3700 for (i
= 0; i
< nargs
[num
]; ++i
) {
3701 if (get_user_ual(a
[i
], vptr
+ i
* sizeof(abi_long
)) != 0) {
3702 return -TARGET_EFAULT
;
3705 /* now when we have the args, invoke the appropriate underlying function */
3707 case TARGET_SYS_SOCKET
: /* domain, type, protocol */
3708 return do_socket(a
[0], a
[1], a
[2]);
3709 case TARGET_SYS_BIND
: /* sockfd, addr, addrlen */
3710 return do_bind(a
[0], a
[1], a
[2]);
3711 case TARGET_SYS_CONNECT
: /* sockfd, addr, addrlen */
3712 return do_connect(a
[0], a
[1], a
[2]);
3713 case TARGET_SYS_LISTEN
: /* sockfd, backlog */
3714 return get_errno(listen(a
[0], a
[1]));
3715 case TARGET_SYS_ACCEPT
: /* sockfd, addr, addrlen */
3716 return do_accept4(a
[0], a
[1], a
[2], 0);
3717 case TARGET_SYS_GETSOCKNAME
: /* sockfd, addr, addrlen */
3718 return do_getsockname(a
[0], a
[1], a
[2]);
3719 case TARGET_SYS_GETPEERNAME
: /* sockfd, addr, addrlen */
3720 return do_getpeername(a
[0], a
[1], a
[2]);
3721 case TARGET_SYS_SOCKETPAIR
: /* domain, type, protocol, tab */
3722 return do_socketpair(a
[0], a
[1], a
[2], a
[3]);
3723 case TARGET_SYS_SEND
: /* sockfd, msg, len, flags */
3724 return do_sendto(a
[0], a
[1], a
[2], a
[3], 0, 0);
3725 case TARGET_SYS_RECV
: /* sockfd, msg, len, flags */
3726 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], 0, 0);
3727 case TARGET_SYS_SENDTO
: /* sockfd, msg, len, flags, addr, addrlen */
3728 return do_sendto(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3729 case TARGET_SYS_RECVFROM
: /* sockfd, msg, len, flags, addr, addrlen */
3730 return do_recvfrom(a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]);
3731 case TARGET_SYS_SHUTDOWN
: /* sockfd, how */
3732 return get_errno(shutdown(a
[0], a
[1]));
3733 case TARGET_SYS_SETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
3734 return do_setsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3735 case TARGET_SYS_GETSOCKOPT
: /* sockfd, level, optname, optval, optlen */
3736 return do_getsockopt(a
[0], a
[1], a
[2], a
[3], a
[4]);
3737 case TARGET_SYS_SENDMSG
: /* sockfd, msg, flags */
3738 return do_sendrecvmsg(a
[0], a
[1], a
[2], 1);
3739 case TARGET_SYS_RECVMSG
: /* sockfd, msg, flags */
3740 return do_sendrecvmsg(a
[0], a
[1], a
[2], 0);
3741 case TARGET_SYS_ACCEPT4
: /* sockfd, addr, addrlen, flags */
3742 return do_accept4(a
[0], a
[1], a
[2], a
[3]);
3743 case TARGET_SYS_RECVMMSG
: /* sockfd, msgvec, vlen, flags */
3744 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 0);
3745 case TARGET_SYS_SENDMMSG
: /* sockfd, msgvec, vlen, flags */
3746 return do_sendrecvmmsg(a
[0], a
[1], a
[2], a
[3], 1);
3748 qemu_log_mask(LOG_UNIMP
, "Unsupported socketcall: %d\n", num
);
3749 return -TARGET_EINVAL
;
3754 #define N_SHM_REGIONS 32
3756 static struct shm_region
{
3760 } shm_regions
[N_SHM_REGIONS
];
3762 #ifndef TARGET_SEMID64_DS
3763 /* asm-generic version of this struct */
3764 struct target_semid64_ds
3766 struct target_ipc_perm sem_perm
;
3767 abi_ulong sem_otime
;
3768 #if TARGET_ABI_BITS == 32
3769 abi_ulong __unused1
;
3771 abi_ulong sem_ctime
;
3772 #if TARGET_ABI_BITS == 32
3773 abi_ulong __unused2
;
3775 abi_ulong sem_nsems
;
3776 abi_ulong __unused3
;
3777 abi_ulong __unused4
;
3781 static inline abi_long
target_to_host_ipc_perm(struct ipc_perm
*host_ip
,
3782 abi_ulong target_addr
)
3784 struct target_ipc_perm
*target_ip
;
3785 struct target_semid64_ds
*target_sd
;
3787 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3788 return -TARGET_EFAULT
;
3789 target_ip
= &(target_sd
->sem_perm
);
3790 host_ip
->__key
= tswap32(target_ip
->__key
);
3791 host_ip
->uid
= tswap32(target_ip
->uid
);
3792 host_ip
->gid
= tswap32(target_ip
->gid
);
3793 host_ip
->cuid
= tswap32(target_ip
->cuid
);
3794 host_ip
->cgid
= tswap32(target_ip
->cgid
);
3795 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3796 host_ip
->mode
= tswap32(target_ip
->mode
);
3798 host_ip
->mode
= tswap16(target_ip
->mode
);
3800 #if defined(TARGET_PPC)
3801 host_ip
->__seq
= tswap32(target_ip
->__seq
);
3803 host_ip
->__seq
= tswap16(target_ip
->__seq
);
3805 unlock_user_struct(target_sd
, target_addr
, 0);
3809 static inline abi_long
host_to_target_ipc_perm(abi_ulong target_addr
,
3810 struct ipc_perm
*host_ip
)
3812 struct target_ipc_perm
*target_ip
;
3813 struct target_semid64_ds
*target_sd
;
3815 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3816 return -TARGET_EFAULT
;
3817 target_ip
= &(target_sd
->sem_perm
);
3818 target_ip
->__key
= tswap32(host_ip
->__key
);
3819 target_ip
->uid
= tswap32(host_ip
->uid
);
3820 target_ip
->gid
= tswap32(host_ip
->gid
);
3821 target_ip
->cuid
= tswap32(host_ip
->cuid
);
3822 target_ip
->cgid
= tswap32(host_ip
->cgid
);
3823 #if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3824 target_ip
->mode
= tswap32(host_ip
->mode
);
3826 target_ip
->mode
= tswap16(host_ip
->mode
);
3828 #if defined(TARGET_PPC)
3829 target_ip
->__seq
= tswap32(host_ip
->__seq
);
3831 target_ip
->__seq
= tswap16(host_ip
->__seq
);
3833 unlock_user_struct(target_sd
, target_addr
, 1);
3837 static inline abi_long
target_to_host_semid_ds(struct semid_ds
*host_sd
,
3838 abi_ulong target_addr
)
3840 struct target_semid64_ds
*target_sd
;
3842 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
3843 return -TARGET_EFAULT
;
3844 if (target_to_host_ipc_perm(&(host_sd
->sem_perm
),target_addr
))
3845 return -TARGET_EFAULT
;
3846 host_sd
->sem_nsems
= tswapal(target_sd
->sem_nsems
);
3847 host_sd
->sem_otime
= tswapal(target_sd
->sem_otime
);
3848 host_sd
->sem_ctime
= tswapal(target_sd
->sem_ctime
);
3849 unlock_user_struct(target_sd
, target_addr
, 0);
3853 static inline abi_long
host_to_target_semid_ds(abi_ulong target_addr
,
3854 struct semid_ds
*host_sd
)
3856 struct target_semid64_ds
*target_sd
;
3858 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
3859 return -TARGET_EFAULT
;
3860 if (host_to_target_ipc_perm(target_addr
,&(host_sd
->sem_perm
)))
3861 return -TARGET_EFAULT
;
3862 target_sd
->sem_nsems
= tswapal(host_sd
->sem_nsems
);
3863 target_sd
->sem_otime
= tswapal(host_sd
->sem_otime
);
3864 target_sd
->sem_ctime
= tswapal(host_sd
->sem_ctime
);
3865 unlock_user_struct(target_sd
, target_addr
, 1);
3869 struct target_seminfo
{
3882 static inline abi_long
host_to_target_seminfo(abi_ulong target_addr
,
3883 struct seminfo
*host_seminfo
)
3885 struct target_seminfo
*target_seminfo
;
3886 if (!lock_user_struct(VERIFY_WRITE
, target_seminfo
, target_addr
, 0))
3887 return -TARGET_EFAULT
;
3888 __put_user(host_seminfo
->semmap
, &target_seminfo
->semmap
);
3889 __put_user(host_seminfo
->semmni
, &target_seminfo
->semmni
);
3890 __put_user(host_seminfo
->semmns
, &target_seminfo
->semmns
);
3891 __put_user(host_seminfo
->semmnu
, &target_seminfo
->semmnu
);
3892 __put_user(host_seminfo
->semmsl
, &target_seminfo
->semmsl
);
3893 __put_user(host_seminfo
->semopm
, &target_seminfo
->semopm
);
3894 __put_user(host_seminfo
->semume
, &target_seminfo
->semume
);
3895 __put_user(host_seminfo
->semusz
, &target_seminfo
->semusz
);
3896 __put_user(host_seminfo
->semvmx
, &target_seminfo
->semvmx
);
3897 __put_user(host_seminfo
->semaem
, &target_seminfo
->semaem
);
3898 unlock_user_struct(target_seminfo
, target_addr
, 1);
3904 struct semid_ds
*buf
;
3905 unsigned short *array
;
3906 struct seminfo
*__buf
;
3909 union target_semun
{
3916 static inline abi_long
target_to_host_semarray(int semid
, unsigned short **host_array
,
3917 abi_ulong target_addr
)
3920 unsigned short *array
;
3922 struct semid_ds semid_ds
;
3925 semun
.buf
= &semid_ds
;
3927 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3929 return get_errno(ret
);
3931 nsems
= semid_ds
.sem_nsems
;
3933 *host_array
= g_try_new(unsigned short, nsems
);
3935 return -TARGET_ENOMEM
;
3937 array
= lock_user(VERIFY_READ
, target_addr
,
3938 nsems
*sizeof(unsigned short), 1);
3940 g_free(*host_array
);
3941 return -TARGET_EFAULT
;
3944 for(i
=0; i
<nsems
; i
++) {
3945 __get_user((*host_array
)[i
], &array
[i
]);
3947 unlock_user(array
, target_addr
, 0);
3952 static inline abi_long
host_to_target_semarray(int semid
, abi_ulong target_addr
,
3953 unsigned short **host_array
)
3956 unsigned short *array
;
3958 struct semid_ds semid_ds
;
3961 semun
.buf
= &semid_ds
;
3963 ret
= semctl(semid
, 0, IPC_STAT
, semun
);
3965 return get_errno(ret
);
3967 nsems
= semid_ds
.sem_nsems
;
3969 array
= lock_user(VERIFY_WRITE
, target_addr
,
3970 nsems
*sizeof(unsigned short), 0);
3972 return -TARGET_EFAULT
;
3974 for(i
=0; i
<nsems
; i
++) {
3975 __put_user((*host_array
)[i
], &array
[i
]);
3977 g_free(*host_array
);
3978 unlock_user(array
, target_addr
, 1);
3983 static inline abi_long
do_semctl(int semid
, int semnum
, int cmd
,
3984 abi_ulong target_arg
)
3986 union target_semun target_su
= { .buf
= target_arg
};
3988 struct semid_ds dsarg
;
3989 unsigned short *array
= NULL
;
3990 struct seminfo seminfo
;
3991 abi_long ret
= -TARGET_EINVAL
;
3998 /* In 64 bit cross-endian situations, we will erroneously pick up
3999 * the wrong half of the union for the "val" element. To rectify
4000 * this, the entire 8-byte structure is byteswapped, followed by
4001 * a swap of the 4 byte val field. In other cases, the data is
4002 * already in proper host byte order. */
4003 if (sizeof(target_su
.val
) != (sizeof(target_su
.buf
))) {
4004 target_su
.buf
= tswapal(target_su
.buf
);
4005 arg
.val
= tswap32(target_su
.val
);
4007 arg
.val
= target_su
.val
;
4009 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4013 err
= target_to_host_semarray(semid
, &array
, target_su
.array
);
4017 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4018 err
= host_to_target_semarray(semid
, target_su
.array
, &array
);
4025 err
= target_to_host_semid_ds(&dsarg
, target_su
.buf
);
4029 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4030 err
= host_to_target_semid_ds(target_su
.buf
, &dsarg
);
4036 arg
.__buf
= &seminfo
;
4037 ret
= get_errno(semctl(semid
, semnum
, cmd
, arg
));
4038 err
= host_to_target_seminfo(target_su
.__buf
, &seminfo
);
4046 ret
= get_errno(semctl(semid
, semnum
, cmd
, NULL
));
4053 struct target_sembuf
{
4054 unsigned short sem_num
;
4059 static inline abi_long
target_to_host_sembuf(struct sembuf
*host_sembuf
,
4060 abi_ulong target_addr
,
4063 struct target_sembuf
*target_sembuf
;
4066 target_sembuf
= lock_user(VERIFY_READ
, target_addr
,
4067 nsops
*sizeof(struct target_sembuf
), 1);
4069 return -TARGET_EFAULT
;
4071 for(i
=0; i
<nsops
; i
++) {
4072 __get_user(host_sembuf
[i
].sem_num
, &target_sembuf
[i
].sem_num
);
4073 __get_user(host_sembuf
[i
].sem_op
, &target_sembuf
[i
].sem_op
);
4074 __get_user(host_sembuf
[i
].sem_flg
, &target_sembuf
[i
].sem_flg
);
4077 unlock_user(target_sembuf
, target_addr
, 0);
4082 #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
4083 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
4086 * This macro is required to handle the s390 variants, which passes the
4087 * arguments in a different order than default.
4090 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4091 (__nsops), (__timeout), (__sops)
4093 #define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4094 (__nsops), 0, (__sops), (__timeout)
4097 static inline abi_long
do_semtimedop(int semid
,
4100 abi_long timeout
, bool time64
)
4102 struct sembuf
*sops
;
4103 struct timespec ts
, *pts
= NULL
;
4109 if (target_to_host_timespec64(pts
, timeout
)) {
4110 return -TARGET_EFAULT
;
4113 if (target_to_host_timespec(pts
, timeout
)) {
4114 return -TARGET_EFAULT
;
4119 if (nsops
> TARGET_SEMOPM
) {
4120 return -TARGET_E2BIG
;
4123 sops
= g_new(struct sembuf
, nsops
);
4125 if (target_to_host_sembuf(sops
, ptr
, nsops
)) {
4127 return -TARGET_EFAULT
;
4130 ret
= -TARGET_ENOSYS
;
4131 #ifdef __NR_semtimedop
4132 ret
= get_errno(safe_semtimedop(semid
, sops
, nsops
, pts
));
4135 if (ret
== -TARGET_ENOSYS
) {
4136 ret
= get_errno(safe_ipc(IPCOP_semtimedop
, semid
,
4137 SEMTIMEDOP_IPC_ARGS(nsops
, sops
, (long)pts
)));
4145 struct target_msqid_ds
4147 struct target_ipc_perm msg_perm
;
4148 abi_ulong msg_stime
;
4149 #if TARGET_ABI_BITS == 32
4150 abi_ulong __unused1
;
4152 abi_ulong msg_rtime
;
4153 #if TARGET_ABI_BITS == 32
4154 abi_ulong __unused2
;
4156 abi_ulong msg_ctime
;
4157 #if TARGET_ABI_BITS == 32
4158 abi_ulong __unused3
;
4160 abi_ulong __msg_cbytes
;
4162 abi_ulong msg_qbytes
;
4163 abi_ulong msg_lspid
;
4164 abi_ulong msg_lrpid
;
4165 abi_ulong __unused4
;
4166 abi_ulong __unused5
;
4169 static inline abi_long
target_to_host_msqid_ds(struct msqid_ds
*host_md
,
4170 abi_ulong target_addr
)
4172 struct target_msqid_ds
*target_md
;
4174 if (!lock_user_struct(VERIFY_READ
, target_md
, target_addr
, 1))
4175 return -TARGET_EFAULT
;
4176 if (target_to_host_ipc_perm(&(host_md
->msg_perm
),target_addr
))
4177 return -TARGET_EFAULT
;
4178 host_md
->msg_stime
= tswapal(target_md
->msg_stime
);
4179 host_md
->msg_rtime
= tswapal(target_md
->msg_rtime
);
4180 host_md
->msg_ctime
= tswapal(target_md
->msg_ctime
);
4181 host_md
->__msg_cbytes
= tswapal(target_md
->__msg_cbytes
);
4182 host_md
->msg_qnum
= tswapal(target_md
->msg_qnum
);
4183 host_md
->msg_qbytes
= tswapal(target_md
->msg_qbytes
);
4184 host_md
->msg_lspid
= tswapal(target_md
->msg_lspid
);
4185 host_md
->msg_lrpid
= tswapal(target_md
->msg_lrpid
);
4186 unlock_user_struct(target_md
, target_addr
, 0);
4190 static inline abi_long
host_to_target_msqid_ds(abi_ulong target_addr
,
4191 struct msqid_ds
*host_md
)
4193 struct target_msqid_ds
*target_md
;
4195 if (!lock_user_struct(VERIFY_WRITE
, target_md
, target_addr
, 0))
4196 return -TARGET_EFAULT
;
4197 if (host_to_target_ipc_perm(target_addr
,&(host_md
->msg_perm
)))
4198 return -TARGET_EFAULT
;
4199 target_md
->msg_stime
= tswapal(host_md
->msg_stime
);
4200 target_md
->msg_rtime
= tswapal(host_md
->msg_rtime
);
4201 target_md
->msg_ctime
= tswapal(host_md
->msg_ctime
);
4202 target_md
->__msg_cbytes
= tswapal(host_md
->__msg_cbytes
);
4203 target_md
->msg_qnum
= tswapal(host_md
->msg_qnum
);
4204 target_md
->msg_qbytes
= tswapal(host_md
->msg_qbytes
);
4205 target_md
->msg_lspid
= tswapal(host_md
->msg_lspid
);
4206 target_md
->msg_lrpid
= tswapal(host_md
->msg_lrpid
);
4207 unlock_user_struct(target_md
, target_addr
, 1);
4211 struct target_msginfo
{
4219 unsigned short int msgseg
;
4222 static inline abi_long
host_to_target_msginfo(abi_ulong target_addr
,
4223 struct msginfo
*host_msginfo
)
4225 struct target_msginfo
*target_msginfo
;
4226 if (!lock_user_struct(VERIFY_WRITE
, target_msginfo
, target_addr
, 0))
4227 return -TARGET_EFAULT
;
4228 __put_user(host_msginfo
->msgpool
, &target_msginfo
->msgpool
);
4229 __put_user(host_msginfo
->msgmap
, &target_msginfo
->msgmap
);
4230 __put_user(host_msginfo
->msgmax
, &target_msginfo
->msgmax
);
4231 __put_user(host_msginfo
->msgmnb
, &target_msginfo
->msgmnb
);
4232 __put_user(host_msginfo
->msgmni
, &target_msginfo
->msgmni
);
4233 __put_user(host_msginfo
->msgssz
, &target_msginfo
->msgssz
);
4234 __put_user(host_msginfo
->msgtql
, &target_msginfo
->msgtql
);
4235 __put_user(host_msginfo
->msgseg
, &target_msginfo
->msgseg
);
4236 unlock_user_struct(target_msginfo
, target_addr
, 1);
4240 static inline abi_long
do_msgctl(int msgid
, int cmd
, abi_long ptr
)
4242 struct msqid_ds dsarg
;
4243 struct msginfo msginfo
;
4244 abi_long ret
= -TARGET_EINVAL
;
4252 if (target_to_host_msqid_ds(&dsarg
,ptr
))
4253 return -TARGET_EFAULT
;
4254 ret
= get_errno(msgctl(msgid
, cmd
, &dsarg
));
4255 if (host_to_target_msqid_ds(ptr
,&dsarg
))
4256 return -TARGET_EFAULT
;
4259 ret
= get_errno(msgctl(msgid
, cmd
, NULL
));
4263 ret
= get_errno(msgctl(msgid
, cmd
, (struct msqid_ds
*)&msginfo
));
4264 if (host_to_target_msginfo(ptr
, &msginfo
))
4265 return -TARGET_EFAULT
;
4272 struct target_msgbuf
{
4277 static inline abi_long
do_msgsnd(int msqid
, abi_long msgp
,
4278 ssize_t msgsz
, int msgflg
)
4280 struct target_msgbuf
*target_mb
;
4281 struct msgbuf
*host_mb
;
4285 return -TARGET_EINVAL
;
4288 if (!lock_user_struct(VERIFY_READ
, target_mb
, msgp
, 0))
4289 return -TARGET_EFAULT
;
4290 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4292 unlock_user_struct(target_mb
, msgp
, 0);
4293 return -TARGET_ENOMEM
;
4295 host_mb
->mtype
= (abi_long
) tswapal(target_mb
->mtype
);
4296 memcpy(host_mb
->mtext
, target_mb
->mtext
, msgsz
);
4297 ret
= -TARGET_ENOSYS
;
4299 ret
= get_errno(safe_msgsnd(msqid
, host_mb
, msgsz
, msgflg
));
4302 if (ret
== -TARGET_ENOSYS
) {
4304 ret
= get_errno(safe_ipc(IPCOP_msgsnd
, msqid
, msgsz
, msgflg
,
4307 ret
= get_errno(safe_ipc(IPCOP_msgsnd
, msqid
, msgsz
, msgflg
,
4313 unlock_user_struct(target_mb
, msgp
, 0);
4319 #if defined(__sparc__)
4320 /* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
4321 #define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
4322 #elif defined(__s390x__)
4323 /* The s390 sys_ipc variant has only five parameters. */
4324 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4325 ((long int[]){(long int)__msgp, __msgtyp})
4327 #define MSGRCV_ARGS(__msgp, __msgtyp) \
4328 ((long int[]){(long int)__msgp, __msgtyp}), 0
4332 static inline abi_long
do_msgrcv(int msqid
, abi_long msgp
,
4333 ssize_t msgsz
, abi_long msgtyp
,
4336 struct target_msgbuf
*target_mb
;
4338 struct msgbuf
*host_mb
;
4342 return -TARGET_EINVAL
;
4345 if (!lock_user_struct(VERIFY_WRITE
, target_mb
, msgp
, 0))
4346 return -TARGET_EFAULT
;
4348 host_mb
= g_try_malloc(msgsz
+ sizeof(long));
4350 ret
= -TARGET_ENOMEM
;
4353 ret
= -TARGET_ENOSYS
;
4355 ret
= get_errno(safe_msgrcv(msqid
, host_mb
, msgsz
, msgtyp
, msgflg
));
4358 if (ret
== -TARGET_ENOSYS
) {
4359 ret
= get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv
), msqid
, msgsz
,
4360 msgflg
, MSGRCV_ARGS(host_mb
, msgtyp
)));
4365 abi_ulong target_mtext_addr
= msgp
+ sizeof(abi_ulong
);
4366 target_mtext
= lock_user(VERIFY_WRITE
, target_mtext_addr
, ret
, 0);
4367 if (!target_mtext
) {
4368 ret
= -TARGET_EFAULT
;
4371 memcpy(target_mb
->mtext
, host_mb
->mtext
, ret
);
4372 unlock_user(target_mtext
, target_mtext_addr
, ret
);
4375 target_mb
->mtype
= tswapal(host_mb
->mtype
);
4379 unlock_user_struct(target_mb
, msgp
, 1);
4384 static inline abi_long
target_to_host_shmid_ds(struct shmid_ds
*host_sd
,
4385 abi_ulong target_addr
)
4387 struct target_shmid_ds
*target_sd
;
4389 if (!lock_user_struct(VERIFY_READ
, target_sd
, target_addr
, 1))
4390 return -TARGET_EFAULT
;
4391 if (target_to_host_ipc_perm(&(host_sd
->shm_perm
), target_addr
))
4392 return -TARGET_EFAULT
;
4393 __get_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4394 __get_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4395 __get_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4396 __get_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4397 __get_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4398 __get_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4399 __get_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4400 unlock_user_struct(target_sd
, target_addr
, 0);
4404 static inline abi_long
host_to_target_shmid_ds(abi_ulong target_addr
,
4405 struct shmid_ds
*host_sd
)
4407 struct target_shmid_ds
*target_sd
;
4409 if (!lock_user_struct(VERIFY_WRITE
, target_sd
, target_addr
, 0))
4410 return -TARGET_EFAULT
;
4411 if (host_to_target_ipc_perm(target_addr
, &(host_sd
->shm_perm
)))
4412 return -TARGET_EFAULT
;
4413 __put_user(host_sd
->shm_segsz
, &target_sd
->shm_segsz
);
4414 __put_user(host_sd
->shm_atime
, &target_sd
->shm_atime
);
4415 __put_user(host_sd
->shm_dtime
, &target_sd
->shm_dtime
);
4416 __put_user(host_sd
->shm_ctime
, &target_sd
->shm_ctime
);
4417 __put_user(host_sd
->shm_cpid
, &target_sd
->shm_cpid
);
4418 __put_user(host_sd
->shm_lpid
, &target_sd
->shm_lpid
);
4419 __put_user(host_sd
->shm_nattch
, &target_sd
->shm_nattch
);
4420 unlock_user_struct(target_sd
, target_addr
, 1);
4424 struct target_shminfo
{
4432 static inline abi_long
host_to_target_shminfo(abi_ulong target_addr
,
4433 struct shminfo
*host_shminfo
)
4435 struct target_shminfo
*target_shminfo
;
4436 if (!lock_user_struct(VERIFY_WRITE
, target_shminfo
, target_addr
, 0))
4437 return -TARGET_EFAULT
;
4438 __put_user(host_shminfo
->shmmax
, &target_shminfo
->shmmax
);
4439 __put_user(host_shminfo
->shmmin
, &target_shminfo
->shmmin
);
4440 __put_user(host_shminfo
->shmmni
, &target_shminfo
->shmmni
);
4441 __put_user(host_shminfo
->shmseg
, &target_shminfo
->shmseg
);
4442 __put_user(host_shminfo
->shmall
, &target_shminfo
->shmall
);
4443 unlock_user_struct(target_shminfo
, target_addr
, 1);
4447 struct target_shm_info
{
4452 abi_ulong swap_attempts
;
4453 abi_ulong swap_successes
;
4456 static inline abi_long
host_to_target_shm_info(abi_ulong target_addr
,
4457 struct shm_info
*host_shm_info
)
4459 struct target_shm_info
*target_shm_info
;
4460 if (!lock_user_struct(VERIFY_WRITE
, target_shm_info
, target_addr
, 0))
4461 return -TARGET_EFAULT
;
4462 __put_user(host_shm_info
->used_ids
, &target_shm_info
->used_ids
);
4463 __put_user(host_shm_info
->shm_tot
, &target_shm_info
->shm_tot
);
4464 __put_user(host_shm_info
->shm_rss
, &target_shm_info
->shm_rss
);
4465 __put_user(host_shm_info
->shm_swp
, &target_shm_info
->shm_swp
);
4466 __put_user(host_shm_info
->swap_attempts
, &target_shm_info
->swap_attempts
);
4467 __put_user(host_shm_info
->swap_successes
, &target_shm_info
->swap_successes
);
4468 unlock_user_struct(target_shm_info
, target_addr
, 1);
4472 static inline abi_long
do_shmctl(int shmid
, int cmd
, abi_long buf
)
4474 struct shmid_ds dsarg
;
4475 struct shminfo shminfo
;
4476 struct shm_info shm_info
;
4477 abi_long ret
= -TARGET_EINVAL
;
4485 if (target_to_host_shmid_ds(&dsarg
, buf
))
4486 return -TARGET_EFAULT
;
4487 ret
= get_errno(shmctl(shmid
, cmd
, &dsarg
));
4488 if (host_to_target_shmid_ds(buf
, &dsarg
))
4489 return -TARGET_EFAULT
;
4492 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shminfo
));
4493 if (host_to_target_shminfo(buf
, &shminfo
))
4494 return -TARGET_EFAULT
;
4497 ret
= get_errno(shmctl(shmid
, cmd
, (struct shmid_ds
*)&shm_info
));
4498 if (host_to_target_shm_info(buf
, &shm_info
))
4499 return -TARGET_EFAULT
;
4504 ret
= get_errno(shmctl(shmid
, cmd
, NULL
));
4511 #ifndef TARGET_FORCE_SHMLBA
4512 /* For most architectures, SHMLBA is the same as the page size;
4513 * some architectures have larger values, in which case they should
4514 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
4515 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
4516 * and defining its own value for SHMLBA.
4518 * The kernel also permits SHMLBA to be set by the architecture to a
4519 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
4520 * this means that addresses are rounded to the large size if
4521 * SHM_RND is set but addresses not aligned to that size are not rejected
4522 * as long as they are at least page-aligned. Since the only architecture
4523 * which uses this is ia64 this code doesn't provide for that oddity.
4525 static inline abi_ulong
target_shmlba(CPUArchState
*cpu_env
)
4527 return TARGET_PAGE_SIZE
;
4531 static inline abi_ulong
do_shmat(CPUArchState
*cpu_env
,
4532 int shmid
, abi_ulong shmaddr
, int shmflg
)
4534 CPUState
*cpu
= env_cpu(cpu_env
);
4537 struct shmid_ds shm_info
;
4541 /* shmat pointers are always untagged */
4543 /* find out the length of the shared memory segment */
4544 ret
= get_errno(shmctl(shmid
, IPC_STAT
, &shm_info
));
4545 if (is_error(ret
)) {
4546 /* can't get length, bail out */
4550 shmlba
= target_shmlba(cpu_env
);
4552 if (shmaddr
& (shmlba
- 1)) {
4553 if (shmflg
& SHM_RND
) {
4554 shmaddr
&= ~(shmlba
- 1);
4556 return -TARGET_EINVAL
;
4559 if (!guest_range_valid_untagged(shmaddr
, shm_info
.shm_segsz
)) {
4560 return -TARGET_EINVAL
;
4566 * We're mapping shared memory, so ensure we generate code for parallel
4567 * execution and flush old translations. This will work up to the level
4568 * supported by the host -- anything that requires EXCP_ATOMIC will not
4569 * be atomic with respect to an external process.
4571 if (!(cpu
->tcg_cflags
& CF_PARALLEL
)) {
4572 cpu
->tcg_cflags
|= CF_PARALLEL
;
4577 host_raddr
= shmat(shmid
, (void *)g2h_untagged(shmaddr
), shmflg
);
4579 abi_ulong mmap_start
;
4581 /* In order to use the host shmat, we need to honor host SHMLBA. */
4582 mmap_start
= mmap_find_vma(0, shm_info
.shm_segsz
, MAX(SHMLBA
, shmlba
));
4584 if (mmap_start
== -1) {
4586 host_raddr
= (void *)-1;
4588 host_raddr
= shmat(shmid
, g2h_untagged(mmap_start
),
4589 shmflg
| SHM_REMAP
);
4592 if (host_raddr
== (void *)-1) {
4594 return get_errno((long)host_raddr
);
4596 raddr
=h2g((unsigned long)host_raddr
);
4598 page_set_flags(raddr
, raddr
+ shm_info
.shm_segsz
- 1,
4599 PAGE_VALID
| PAGE_RESET
| PAGE_READ
|
4600 (shmflg
& SHM_RDONLY
? 0 : PAGE_WRITE
));
4602 for (i
= 0; i
< N_SHM_REGIONS
; i
++) {
4603 if (!shm_regions
[i
].in_use
) {
4604 shm_regions
[i
].in_use
= true;
4605 shm_regions
[i
].start
= raddr
;
4606 shm_regions
[i
].size
= shm_info
.shm_segsz
;
4616 static inline abi_long
do_shmdt(abi_ulong shmaddr
)
4621 /* shmdt pointers are always untagged */
4625 for (i
= 0; i
< N_SHM_REGIONS
; ++i
) {
4626 if (shm_regions
[i
].in_use
&& shm_regions
[i
].start
== shmaddr
) {
4627 shm_regions
[i
].in_use
= false;
4628 page_set_flags(shmaddr
, shmaddr
+ shm_regions
[i
].size
- 1, 0);
4632 rv
= get_errno(shmdt(g2h_untagged(shmaddr
)));
4639 #ifdef TARGET_NR_ipc
4640 /* ??? This only works with linear mappings. */
4641 /* do_ipc() must return target values and target errnos. */
4642 static abi_long
do_ipc(CPUArchState
*cpu_env
,
4643 unsigned int call
, abi_long first
,
4644 abi_long second
, abi_long third
,
4645 abi_long ptr
, abi_long fifth
)
4650 version
= call
>> 16;
4655 ret
= do_semtimedop(first
, ptr
, second
, 0, false);
4657 case IPCOP_semtimedop
:
4659 * The s390 sys_ipc variant has only five parameters instead of six
4660 * (as for default variant) and the only difference is the handling of
4661 * SEMTIMEDOP where on s390 the third parameter is used as a pointer
4662 * to a struct timespec where the generic variant uses fifth parameter.
4664 #if defined(TARGET_S390X)
4665 ret
= do_semtimedop(first
, ptr
, second
, third
, TARGET_ABI_BITS
== 64);
4667 ret
= do_semtimedop(first
, ptr
, second
, fifth
, TARGET_ABI_BITS
== 64);
4672 ret
= get_errno(semget(first
, second
, third
));
4675 case IPCOP_semctl
: {
4676 /* The semun argument to semctl is passed by value, so dereference the
4679 get_user_ual(atptr
, ptr
);
4680 ret
= do_semctl(first
, second
, third
, atptr
);
4685 ret
= get_errno(msgget(first
, second
));
4689 ret
= do_msgsnd(first
, ptr
, second
, third
);
4693 ret
= do_msgctl(first
, second
, ptr
);
4700 struct target_ipc_kludge
{
4705 if (!lock_user_struct(VERIFY_READ
, tmp
, ptr
, 1)) {
4706 ret
= -TARGET_EFAULT
;
4710 ret
= do_msgrcv(first
, tswapal(tmp
->msgp
), second
, tswapal(tmp
->msgtyp
), third
);
4712 unlock_user_struct(tmp
, ptr
, 0);
4716 ret
= do_msgrcv(first
, ptr
, second
, fifth
, third
);
4725 raddr
= do_shmat(cpu_env
, first
, ptr
, second
);
4726 if (is_error(raddr
))
4727 return get_errno(raddr
);
4728 if (put_user_ual(raddr
, third
))
4729 return -TARGET_EFAULT
;
4733 ret
= -TARGET_EINVAL
;
4738 ret
= do_shmdt(ptr
);
4742 /* IPC_* flag values are the same on all linux platforms */
4743 ret
= get_errno(shmget(first
, second
, third
));
4746 /* IPC_* and SHM_* command values are the same on all linux platforms */
4748 ret
= do_shmctl(first
, second
, ptr
);
4751 qemu_log_mask(LOG_UNIMP
, "Unsupported ipc call: %d (version %d)\n",
4753 ret
= -TARGET_ENOSYS
;
4760 /* kernel structure types definitions */
4762 #define STRUCT(name, ...) STRUCT_ ## name,
4763 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
4765 #include "syscall_types.h"
4769 #undef STRUCT_SPECIAL
4771 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
4772 #define STRUCT_SPECIAL(name)
4773 #include "syscall_types.h"
4775 #undef STRUCT_SPECIAL
4777 #define MAX_STRUCT_SIZE 4096
4779 #ifdef CONFIG_FIEMAP
4780 /* So fiemap access checks don't overflow on 32 bit systems.
4781 * This is very slightly smaller than the limit imposed by
4782 * the underlying kernel.
4784 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4785 / sizeof(struct fiemap_extent))
4787 static abi_long
do_ioctl_fs_ioc_fiemap(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4788 int fd
, int cmd
, abi_long arg
)
4790 /* The parameter for this ioctl is a struct fiemap followed
4791 * by an array of struct fiemap_extent whose size is set
4792 * in fiemap->fm_extent_count. The array is filled in by the
4795 int target_size_in
, target_size_out
;
4797 const argtype
*arg_type
= ie
->arg_type
;
4798 const argtype extent_arg_type
[] = { MK_STRUCT(STRUCT_fiemap_extent
) };
4801 int i
, extent_size
= thunk_type_size(extent_arg_type
, 0);
4805 assert(arg_type
[0] == TYPE_PTR
);
4806 assert(ie
->access
== IOC_RW
);
4808 target_size_in
= thunk_type_size(arg_type
, 0);
4809 argptr
= lock_user(VERIFY_READ
, arg
, target_size_in
, 1);
4811 return -TARGET_EFAULT
;
4813 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4814 unlock_user(argptr
, arg
, 0);
4815 fm
= (struct fiemap
*)buf_temp
;
4816 if (fm
->fm_extent_count
> FIEMAP_MAX_EXTENTS
) {
4817 return -TARGET_EINVAL
;
4820 outbufsz
= sizeof (*fm
) +
4821 (sizeof(struct fiemap_extent
) * fm
->fm_extent_count
);
4823 if (outbufsz
> MAX_STRUCT_SIZE
) {
4824 /* We can't fit all the extents into the fixed size buffer.
4825 * Allocate one that is large enough and use it instead.
4827 fm
= g_try_malloc(outbufsz
);
4829 return -TARGET_ENOMEM
;
4831 memcpy(fm
, buf_temp
, sizeof(struct fiemap
));
4834 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, fm
));
4835 if (!is_error(ret
)) {
4836 target_size_out
= target_size_in
;
4837 /* An extent_count of 0 means we were only counting the extents
4838 * so there are no structs to copy
4840 if (fm
->fm_extent_count
!= 0) {
4841 target_size_out
+= fm
->fm_mapped_extents
* extent_size
;
4843 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size_out
, 0);
4845 ret
= -TARGET_EFAULT
;
4847 /* Convert the struct fiemap */
4848 thunk_convert(argptr
, fm
, arg_type
, THUNK_TARGET
);
4849 if (fm
->fm_extent_count
!= 0) {
4850 p
= argptr
+ target_size_in
;
4851 /* ...and then all the struct fiemap_extents */
4852 for (i
= 0; i
< fm
->fm_mapped_extents
; i
++) {
4853 thunk_convert(p
, &fm
->fm_extents
[i
], extent_arg_type
,
4858 unlock_user(argptr
, arg
, target_size_out
);
4868 static abi_long
do_ioctl_ifconf(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
4869 int fd
, int cmd
, abi_long arg
)
4871 const argtype
*arg_type
= ie
->arg_type
;
4875 struct ifconf
*host_ifconf
;
4877 const argtype ifreq_arg_type
[] = { MK_STRUCT(STRUCT_sockaddr_ifreq
) };
4878 const argtype ifreq_max_type
[] = { MK_STRUCT(STRUCT_ifmap_ifreq
) };
4879 int target_ifreq_size
;
4884 abi_long target_ifc_buf
;
4888 assert(arg_type
[0] == TYPE_PTR
);
4889 assert(ie
->access
== IOC_RW
);
4892 target_size
= thunk_type_size(arg_type
, 0);
4894 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
4896 return -TARGET_EFAULT
;
4897 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
4898 unlock_user(argptr
, arg
, 0);
4900 host_ifconf
= (struct ifconf
*)(unsigned long)buf_temp
;
4901 target_ifc_buf
= (abi_long
)(unsigned long)host_ifconf
->ifc_buf
;
4902 target_ifreq_size
= thunk_type_size(ifreq_max_type
, 0);
4904 if (target_ifc_buf
!= 0) {
4905 target_ifc_len
= host_ifconf
->ifc_len
;
4906 nb_ifreq
= target_ifc_len
/ target_ifreq_size
;
4907 host_ifc_len
= nb_ifreq
* sizeof(struct ifreq
);
4909 outbufsz
= sizeof(*host_ifconf
) + host_ifc_len
;
4910 if (outbufsz
> MAX_STRUCT_SIZE
) {
4912 * We can't fit all the extents into the fixed size buffer.
4913 * Allocate one that is large enough and use it instead.
4915 host_ifconf
= g_try_malloc(outbufsz
);
4917 return -TARGET_ENOMEM
;
4919 memcpy(host_ifconf
, buf_temp
, sizeof(*host_ifconf
));
4922 host_ifc_buf
= (char *)host_ifconf
+ sizeof(*host_ifconf
);
4924 host_ifconf
->ifc_len
= host_ifc_len
;
4926 host_ifc_buf
= NULL
;
4928 host_ifconf
->ifc_buf
= host_ifc_buf
;
4930 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_ifconf
));
4931 if (!is_error(ret
)) {
4932 /* convert host ifc_len to target ifc_len */
4934 nb_ifreq
= host_ifconf
->ifc_len
/ sizeof(struct ifreq
);
4935 target_ifc_len
= nb_ifreq
* target_ifreq_size
;
4936 host_ifconf
->ifc_len
= target_ifc_len
;
4938 /* restore target ifc_buf */
4940 host_ifconf
->ifc_buf
= (char *)(unsigned long)target_ifc_buf
;
4942 /* copy struct ifconf to target user */
4944 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
4946 return -TARGET_EFAULT
;
4947 thunk_convert(argptr
, host_ifconf
, arg_type
, THUNK_TARGET
);
4948 unlock_user(argptr
, arg
, target_size
);
4950 if (target_ifc_buf
!= 0) {
4951 /* copy ifreq[] to target user */
4952 argptr
= lock_user(VERIFY_WRITE
, target_ifc_buf
, target_ifc_len
, 0);
4953 for (i
= 0; i
< nb_ifreq
; i
++) {
4954 thunk_convert(argptr
+ i
* target_ifreq_size
,
4955 host_ifc_buf
+ i
* sizeof(struct ifreq
),
4956 ifreq_arg_type
, THUNK_TARGET
);
4958 unlock_user(argptr
, target_ifc_buf
, target_ifc_len
);
4963 g_free(host_ifconf
);
4969 #if defined(CONFIG_USBFS)
4970 #if HOST_LONG_BITS > 64
4971 #error USBDEVFS thunks do not support >64 bit hosts yet.
4974 uint64_t target_urb_adr
;
4975 uint64_t target_buf_adr
;
4976 char *target_buf_ptr
;
4977 struct usbdevfs_urb host_urb
;
4980 static GHashTable
*usbdevfs_urb_hashtable(void)
4982 static GHashTable
*urb_hashtable
;
4984 if (!urb_hashtable
) {
4985 urb_hashtable
= g_hash_table_new(g_int64_hash
, g_int64_equal
);
4987 return urb_hashtable
;
4990 static void urb_hashtable_insert(struct live_urb
*urb
)
4992 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4993 g_hash_table_insert(urb_hashtable
, urb
, urb
);
4996 static struct live_urb
*urb_hashtable_lookup(uint64_t target_urb_adr
)
4998 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
4999 return g_hash_table_lookup(urb_hashtable
, &target_urb_adr
);
5002 static void urb_hashtable_remove(struct live_urb
*urb
)
5004 GHashTable
*urb_hashtable
= usbdevfs_urb_hashtable();
5005 g_hash_table_remove(urb_hashtable
, urb
);
5009 do_ioctl_usbdevfs_reapurb(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5010 int fd
, int cmd
, abi_long arg
)
5012 const argtype usbfsurb_arg_type
[] = { MK_STRUCT(STRUCT_usbdevfs_urb
) };
5013 const argtype ptrvoid_arg_type
[] = { TYPE_PTRVOID
, 0, 0 };
5014 struct live_urb
*lurb
;
5018 uintptr_t target_urb_adr
;
5021 target_size
= thunk_type_size(usbfsurb_arg_type
, THUNK_TARGET
);
5023 memset(buf_temp
, 0, sizeof(uint64_t));
5024 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5025 if (is_error(ret
)) {
5029 memcpy(&hurb
, buf_temp
, sizeof(uint64_t));
5030 lurb
= (void *)((uintptr_t)hurb
- offsetof(struct live_urb
, host_urb
));
5031 if (!lurb
->target_urb_adr
) {
5032 return -TARGET_EFAULT
;
5034 urb_hashtable_remove(lurb
);
5035 unlock_user(lurb
->target_buf_ptr
, lurb
->target_buf_adr
,
5036 lurb
->host_urb
.buffer_length
);
5037 lurb
->target_buf_ptr
= NULL
;
5039 /* restore the guest buffer pointer */
5040 lurb
->host_urb
.buffer
= (void *)(uintptr_t)lurb
->target_buf_adr
;
5042 /* update the guest urb struct */
5043 argptr
= lock_user(VERIFY_WRITE
, lurb
->target_urb_adr
, target_size
, 0);
5046 return -TARGET_EFAULT
;
5048 thunk_convert(argptr
, &lurb
->host_urb
, usbfsurb_arg_type
, THUNK_TARGET
);
5049 unlock_user(argptr
, lurb
->target_urb_adr
, target_size
);
5051 target_size
= thunk_type_size(ptrvoid_arg_type
, THUNK_TARGET
);
5052 /* write back the urb handle */
5053 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5056 return -TARGET_EFAULT
;
5059 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
5060 target_urb_adr
= lurb
->target_urb_adr
;
5061 thunk_convert(argptr
, &target_urb_adr
, ptrvoid_arg_type
, THUNK_TARGET
);
5062 unlock_user(argptr
, arg
, target_size
);
5069 do_ioctl_usbdevfs_discardurb(const IOCTLEntry
*ie
,
5070 uint8_t *buf_temp
__attribute__((unused
)),
5071 int fd
, int cmd
, abi_long arg
)
5073 struct live_urb
*lurb
;
5075 /* map target address back to host URB with metadata. */
5076 lurb
= urb_hashtable_lookup(arg
);
5078 return -TARGET_EFAULT
;
5080 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, &lurb
->host_urb
));
5084 do_ioctl_usbdevfs_submiturb(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5085 int fd
, int cmd
, abi_long arg
)
5087 const argtype
*arg_type
= ie
->arg_type
;
5092 struct live_urb
*lurb
;
5095 * each submitted URB needs to map to a unique ID for the
5096 * kernel, and that unique ID needs to be a pointer to
5097 * host memory. hence, we need to malloc for each URB.
5098 * isochronous transfers have a variable length struct.
5101 target_size
= thunk_type_size(arg_type
, THUNK_TARGET
);
5103 /* construct host copy of urb and metadata */
5104 lurb
= g_try_new0(struct live_urb
, 1);
5106 return -TARGET_ENOMEM
;
5109 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5112 return -TARGET_EFAULT
;
5114 thunk_convert(&lurb
->host_urb
, argptr
, arg_type
, THUNK_HOST
);
5115 unlock_user(argptr
, arg
, 0);
5117 lurb
->target_urb_adr
= arg
;
5118 lurb
->target_buf_adr
= (uintptr_t)lurb
->host_urb
.buffer
;
5120 /* buffer space used depends on endpoint type so lock the entire buffer */
5121 /* control type urbs should check the buffer contents for true direction */
5122 rw_dir
= lurb
->host_urb
.endpoint
& USB_DIR_IN
? VERIFY_WRITE
: VERIFY_READ
;
5123 lurb
->target_buf_ptr
= lock_user(rw_dir
, lurb
->target_buf_adr
,
5124 lurb
->host_urb
.buffer_length
, 1);
5125 if (lurb
->target_buf_ptr
== NULL
) {
5127 return -TARGET_EFAULT
;
5130 /* update buffer pointer in host copy */
5131 lurb
->host_urb
.buffer
= lurb
->target_buf_ptr
;
5133 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, &lurb
->host_urb
));
5134 if (is_error(ret
)) {
5135 unlock_user(lurb
->target_buf_ptr
, lurb
->target_buf_adr
, 0);
5138 urb_hashtable_insert(lurb
);
5143 #endif /* CONFIG_USBFS */
5145 static abi_long
do_ioctl_dm(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5146 int cmd
, abi_long arg
)
5149 struct dm_ioctl
*host_dm
;
5150 abi_long guest_data
;
5151 uint32_t guest_data_size
;
5153 const argtype
*arg_type
= ie
->arg_type
;
5155 void *big_buf
= NULL
;
5159 target_size
= thunk_type_size(arg_type
, 0);
5160 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5162 ret
= -TARGET_EFAULT
;
5165 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5166 unlock_user(argptr
, arg
, 0);
5168 /* buf_temp is too small, so fetch things into a bigger buffer */
5169 big_buf
= g_malloc0(((struct dm_ioctl
*)buf_temp
)->data_size
* 2);
5170 memcpy(big_buf
, buf_temp
, target_size
);
5174 guest_data
= arg
+ host_dm
->data_start
;
5175 if ((guest_data
- arg
) < 0) {
5176 ret
= -TARGET_EINVAL
;
5179 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5180 host_data
= (char*)host_dm
+ host_dm
->data_start
;
5182 argptr
= lock_user(VERIFY_READ
, guest_data
, guest_data_size
, 1);
5184 ret
= -TARGET_EFAULT
;
5188 switch (ie
->host_cmd
) {
5190 case DM_LIST_DEVICES
:
5193 case DM_DEV_SUSPEND
:
5196 case DM_TABLE_STATUS
:
5197 case DM_TABLE_CLEAR
:
5199 case DM_LIST_VERSIONS
:
5203 case DM_DEV_SET_GEOMETRY
:
5204 /* data contains only strings */
5205 memcpy(host_data
, argptr
, guest_data_size
);
5208 memcpy(host_data
, argptr
, guest_data_size
);
5209 *(uint64_t*)host_data
= tswap64(*(uint64_t*)argptr
);
5213 void *gspec
= argptr
;
5214 void *cur_data
= host_data
;
5215 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5216 int spec_size
= thunk_type_size(arg_type
, 0);
5219 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5220 struct dm_target_spec
*spec
= cur_data
;
5224 thunk_convert(spec
, gspec
, arg_type
, THUNK_HOST
);
5225 slen
= strlen((char*)gspec
+ spec_size
) + 1;
5227 spec
->next
= sizeof(*spec
) + slen
;
5228 strcpy((char*)&spec
[1], gspec
+ spec_size
);
5230 cur_data
+= spec
->next
;
5235 ret
= -TARGET_EINVAL
;
5236 unlock_user(argptr
, guest_data
, 0);
5239 unlock_user(argptr
, guest_data
, 0);
5241 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5242 if (!is_error(ret
)) {
5243 guest_data
= arg
+ host_dm
->data_start
;
5244 guest_data_size
= host_dm
->data_size
- host_dm
->data_start
;
5245 argptr
= lock_user(VERIFY_WRITE
, guest_data
, guest_data_size
, 0);
5246 switch (ie
->host_cmd
) {
5251 case DM_DEV_SUSPEND
:
5254 case DM_TABLE_CLEAR
:
5256 case DM_DEV_SET_GEOMETRY
:
5257 /* no return data */
5259 case DM_LIST_DEVICES
:
5261 struct dm_name_list
*nl
= (void*)host_dm
+ host_dm
->data_start
;
5262 uint32_t remaining_data
= guest_data_size
;
5263 void *cur_data
= argptr
;
5264 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_name_list
) };
5265 int nl_size
= 12; /* can't use thunk_size due to alignment */
5268 uint32_t next
= nl
->next
;
5270 nl
->next
= nl_size
+ (strlen(nl
->name
) + 1);
5272 if (remaining_data
< nl
->next
) {
5273 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5276 thunk_convert(cur_data
, nl
, arg_type
, THUNK_TARGET
);
5277 strcpy(cur_data
+ nl_size
, nl
->name
);
5278 cur_data
+= nl
->next
;
5279 remaining_data
-= nl
->next
;
5283 nl
= (void*)nl
+ next
;
5288 case DM_TABLE_STATUS
:
5290 struct dm_target_spec
*spec
= (void*)host_dm
+ host_dm
->data_start
;
5291 void *cur_data
= argptr
;
5292 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_spec
) };
5293 int spec_size
= thunk_type_size(arg_type
, 0);
5296 for (i
= 0; i
< host_dm
->target_count
; i
++) {
5297 uint32_t next
= spec
->next
;
5298 int slen
= strlen((char*)&spec
[1]) + 1;
5299 spec
->next
= (cur_data
- argptr
) + spec_size
+ slen
;
5300 if (guest_data_size
< spec
->next
) {
5301 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5304 thunk_convert(cur_data
, spec
, arg_type
, THUNK_TARGET
);
5305 strcpy(cur_data
+ spec_size
, (char*)&spec
[1]);
5306 cur_data
= argptr
+ spec
->next
;
5307 spec
= (void*)host_dm
+ host_dm
->data_start
+ next
;
5313 void *hdata
= (void*)host_dm
+ host_dm
->data_start
;
5314 int count
= *(uint32_t*)hdata
;
5315 uint64_t *hdev
= hdata
+ 8;
5316 uint64_t *gdev
= argptr
+ 8;
5319 *(uint32_t*)argptr
= tswap32(count
);
5320 for (i
= 0; i
< count
; i
++) {
5321 *gdev
= tswap64(*hdev
);
5327 case DM_LIST_VERSIONS
:
5329 struct dm_target_versions
*vers
= (void*)host_dm
+ host_dm
->data_start
;
5330 uint32_t remaining_data
= guest_data_size
;
5331 void *cur_data
= argptr
;
5332 const argtype arg_type
[] = { MK_STRUCT(STRUCT_dm_target_versions
) };
5333 int vers_size
= thunk_type_size(arg_type
, 0);
5336 uint32_t next
= vers
->next
;
5338 vers
->next
= vers_size
+ (strlen(vers
->name
) + 1);
5340 if (remaining_data
< vers
->next
) {
5341 host_dm
->flags
|= DM_BUFFER_FULL_FLAG
;
5344 thunk_convert(cur_data
, vers
, arg_type
, THUNK_TARGET
);
5345 strcpy(cur_data
+ vers_size
, vers
->name
);
5346 cur_data
+= vers
->next
;
5347 remaining_data
-= vers
->next
;
5351 vers
= (void*)vers
+ next
;
5356 unlock_user(argptr
, guest_data
, 0);
5357 ret
= -TARGET_EINVAL
;
5360 unlock_user(argptr
, guest_data
, guest_data_size
);
5362 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5364 ret
= -TARGET_EFAULT
;
5367 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5368 unlock_user(argptr
, arg
, target_size
);
5375 static abi_long
do_ioctl_blkpg(const IOCTLEntry
*ie
, uint8_t *buf_temp
, int fd
,
5376 int cmd
, abi_long arg
)
5380 const argtype
*arg_type
= ie
->arg_type
;
5381 const argtype part_arg_type
[] = { MK_STRUCT(STRUCT_blkpg_partition
) };
5384 struct blkpg_ioctl_arg
*host_blkpg
= (void*)buf_temp
;
5385 struct blkpg_partition host_part
;
5387 /* Read and convert blkpg */
5389 target_size
= thunk_type_size(arg_type
, 0);
5390 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5392 ret
= -TARGET_EFAULT
;
5395 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5396 unlock_user(argptr
, arg
, 0);
5398 switch (host_blkpg
->op
) {
5399 case BLKPG_ADD_PARTITION
:
5400 case BLKPG_DEL_PARTITION
:
5401 /* payload is struct blkpg_partition */
5404 /* Unknown opcode */
5405 ret
= -TARGET_EINVAL
;
5409 /* Read and convert blkpg->data */
5410 arg
= (abi_long
)(uintptr_t)host_blkpg
->data
;
5411 target_size
= thunk_type_size(part_arg_type
, 0);
5412 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5414 ret
= -TARGET_EFAULT
;
5417 thunk_convert(&host_part
, argptr
, part_arg_type
, THUNK_HOST
);
5418 unlock_user(argptr
, arg
, 0);
5420 /* Swizzle the data pointer to our local copy and call! */
5421 host_blkpg
->data
= &host_part
;
5422 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, host_blkpg
));
5428 static abi_long
do_ioctl_rt(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5429 int fd
, int cmd
, abi_long arg
)
5431 const argtype
*arg_type
= ie
->arg_type
;
5432 const StructEntry
*se
;
5433 const argtype
*field_types
;
5434 const int *dst_offsets
, *src_offsets
;
5437 abi_ulong
*target_rt_dev_ptr
= NULL
;
5438 unsigned long *host_rt_dev_ptr
= NULL
;
5442 assert(ie
->access
== IOC_W
);
5443 assert(*arg_type
== TYPE_PTR
);
5445 assert(*arg_type
== TYPE_STRUCT
);
5446 target_size
= thunk_type_size(arg_type
, 0);
5447 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5449 return -TARGET_EFAULT
;
5452 assert(*arg_type
== (int)STRUCT_rtentry
);
5453 se
= struct_entries
+ *arg_type
++;
5454 assert(se
->convert
[0] == NULL
);
5455 /* convert struct here to be able to catch rt_dev string */
5456 field_types
= se
->field_types
;
5457 dst_offsets
= se
->field_offsets
[THUNK_HOST
];
5458 src_offsets
= se
->field_offsets
[THUNK_TARGET
];
5459 for (i
= 0; i
< se
->nb_fields
; i
++) {
5460 if (dst_offsets
[i
] == offsetof(struct rtentry
, rt_dev
)) {
5461 assert(*field_types
== TYPE_PTRVOID
);
5462 target_rt_dev_ptr
= argptr
+ src_offsets
[i
];
5463 host_rt_dev_ptr
= (unsigned long *)(buf_temp
+ dst_offsets
[i
]);
5464 if (*target_rt_dev_ptr
!= 0) {
5465 *host_rt_dev_ptr
= (unsigned long)lock_user_string(
5466 tswapal(*target_rt_dev_ptr
));
5467 if (!*host_rt_dev_ptr
) {
5468 unlock_user(argptr
, arg
, 0);
5469 return -TARGET_EFAULT
;
5472 *host_rt_dev_ptr
= 0;
5477 field_types
= thunk_convert(buf_temp
+ dst_offsets
[i
],
5478 argptr
+ src_offsets
[i
],
5479 field_types
, THUNK_HOST
);
5481 unlock_user(argptr
, arg
, 0);
5483 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5485 assert(host_rt_dev_ptr
!= NULL
);
5486 assert(target_rt_dev_ptr
!= NULL
);
5487 if (*host_rt_dev_ptr
!= 0) {
5488 unlock_user((void *)*host_rt_dev_ptr
,
5489 *target_rt_dev_ptr
, 0);
5494 static abi_long
do_ioctl_kdsigaccept(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5495 int fd
, int cmd
, abi_long arg
)
5497 int sig
= target_to_host_signal(arg
);
5498 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, sig
));
5501 static abi_long
do_ioctl_SIOCGSTAMP(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5502 int fd
, int cmd
, abi_long arg
)
5507 ret
= get_errno(safe_ioctl(fd
, SIOCGSTAMP
, &tv
));
5508 if (is_error(ret
)) {
5512 if (cmd
== (int)TARGET_SIOCGSTAMP_OLD
) {
5513 if (copy_to_user_timeval(arg
, &tv
)) {
5514 return -TARGET_EFAULT
;
5517 if (copy_to_user_timeval64(arg
, &tv
)) {
5518 return -TARGET_EFAULT
;
5525 static abi_long
do_ioctl_SIOCGSTAMPNS(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5526 int fd
, int cmd
, abi_long arg
)
5531 ret
= get_errno(safe_ioctl(fd
, SIOCGSTAMPNS
, &ts
));
5532 if (is_error(ret
)) {
5536 if (cmd
== (int)TARGET_SIOCGSTAMPNS_OLD
) {
5537 if (host_to_target_timespec(arg
, &ts
)) {
5538 return -TARGET_EFAULT
;
5541 if (host_to_target_timespec64(arg
, &ts
)) {
5542 return -TARGET_EFAULT
;
5550 static abi_long
do_ioctl_tiocgptpeer(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5551 int fd
, int cmd
, abi_long arg
)
5553 int flags
= target_to_host_bitmask(arg
, fcntl_flags_tbl
);
5554 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, flags
));
5560 static void unlock_drm_version(struct drm_version
*host_ver
,
5561 struct target_drm_version
*target_ver
,
5564 unlock_user(host_ver
->name
, target_ver
->name
,
5565 copy
? host_ver
->name_len
: 0);
5566 unlock_user(host_ver
->date
, target_ver
->date
,
5567 copy
? host_ver
->date_len
: 0);
5568 unlock_user(host_ver
->desc
, target_ver
->desc
,
5569 copy
? host_ver
->desc_len
: 0);
5572 static inline abi_long
target_to_host_drmversion(struct drm_version
*host_ver
,
5573 struct target_drm_version
*target_ver
)
5575 memset(host_ver
, 0, sizeof(*host_ver
));
5577 __get_user(host_ver
->name_len
, &target_ver
->name_len
);
5578 if (host_ver
->name_len
) {
5579 host_ver
->name
= lock_user(VERIFY_WRITE
, target_ver
->name
,
5580 target_ver
->name_len
, 0);
5581 if (!host_ver
->name
) {
5586 __get_user(host_ver
->date_len
, &target_ver
->date_len
);
5587 if (host_ver
->date_len
) {
5588 host_ver
->date
= lock_user(VERIFY_WRITE
, target_ver
->date
,
5589 target_ver
->date_len
, 0);
5590 if (!host_ver
->date
) {
5595 __get_user(host_ver
->desc_len
, &target_ver
->desc_len
);
5596 if (host_ver
->desc_len
) {
5597 host_ver
->desc
= lock_user(VERIFY_WRITE
, target_ver
->desc
,
5598 target_ver
->desc_len
, 0);
5599 if (!host_ver
->desc
) {
5606 unlock_drm_version(host_ver
, target_ver
, false);
5610 static inline void host_to_target_drmversion(
5611 struct target_drm_version
*target_ver
,
5612 struct drm_version
*host_ver
)
5614 __put_user(host_ver
->version_major
, &target_ver
->version_major
);
5615 __put_user(host_ver
->version_minor
, &target_ver
->version_minor
);
5616 __put_user(host_ver
->version_patchlevel
, &target_ver
->version_patchlevel
);
5617 __put_user(host_ver
->name_len
, &target_ver
->name_len
);
5618 __put_user(host_ver
->date_len
, &target_ver
->date_len
);
5619 __put_user(host_ver
->desc_len
, &target_ver
->desc_len
);
5620 unlock_drm_version(host_ver
, target_ver
, true);
5623 static abi_long
do_ioctl_drm(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5624 int fd
, int cmd
, abi_long arg
)
5626 struct drm_version
*ver
;
5627 struct target_drm_version
*target_ver
;
5630 switch (ie
->host_cmd
) {
5631 case DRM_IOCTL_VERSION
:
5632 if (!lock_user_struct(VERIFY_WRITE
, target_ver
, arg
, 0)) {
5633 return -TARGET_EFAULT
;
5635 ver
= (struct drm_version
*)buf_temp
;
5636 ret
= target_to_host_drmversion(ver
, target_ver
);
5637 if (!is_error(ret
)) {
5638 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, ver
));
5639 if (is_error(ret
)) {
5640 unlock_drm_version(ver
, target_ver
, false);
5642 host_to_target_drmversion(target_ver
, ver
);
5645 unlock_user_struct(target_ver
, arg
, 0);
5648 return -TARGET_ENOSYS
;
5651 static abi_long
do_ioctl_drm_i915_getparam(const IOCTLEntry
*ie
,
5652 struct drm_i915_getparam
*gparam
,
5653 int fd
, abi_long arg
)
5657 struct target_drm_i915_getparam
*target_gparam
;
5659 if (!lock_user_struct(VERIFY_READ
, target_gparam
, arg
, 0)) {
5660 return -TARGET_EFAULT
;
5663 __get_user(gparam
->param
, &target_gparam
->param
);
5664 gparam
->value
= &value
;
5665 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, gparam
));
5666 put_user_s32(value
, target_gparam
->value
);
5668 unlock_user_struct(target_gparam
, arg
, 0);
5672 static abi_long
do_ioctl_drm_i915(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5673 int fd
, int cmd
, abi_long arg
)
5675 switch (ie
->host_cmd
) {
5676 case DRM_IOCTL_I915_GETPARAM
:
5677 return do_ioctl_drm_i915_getparam(ie
,
5678 (struct drm_i915_getparam
*)buf_temp
,
5681 return -TARGET_ENOSYS
;
5687 static abi_long
do_ioctl_TUNSETTXFILTER(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
5688 int fd
, int cmd
, abi_long arg
)
5690 struct tun_filter
*filter
= (struct tun_filter
*)buf_temp
;
5691 struct tun_filter
*target_filter
;
5694 assert(ie
->access
== IOC_W
);
5696 target_filter
= lock_user(VERIFY_READ
, arg
, sizeof(*target_filter
), 1);
5697 if (!target_filter
) {
5698 return -TARGET_EFAULT
;
5700 filter
->flags
= tswap16(target_filter
->flags
);
5701 filter
->count
= tswap16(target_filter
->count
);
5702 unlock_user(target_filter
, arg
, 0);
5704 if (filter
->count
) {
5705 if (offsetof(struct tun_filter
, addr
) + filter
->count
* ETH_ALEN
>
5707 return -TARGET_EFAULT
;
5710 target_addr
= lock_user(VERIFY_READ
,
5711 arg
+ offsetof(struct tun_filter
, addr
),
5712 filter
->count
* ETH_ALEN
, 1);
5714 return -TARGET_EFAULT
;
5716 memcpy(filter
->addr
, target_addr
, filter
->count
* ETH_ALEN
);
5717 unlock_user(target_addr
, arg
+ offsetof(struct tun_filter
, addr
), 0);
5720 return get_errno(safe_ioctl(fd
, ie
->host_cmd
, filter
));
5723 IOCTLEntry ioctl_entries
[] = {
5724 #define IOCTL(cmd, access, ...) \
5725 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5726 #define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5727 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
5728 #define IOCTL_IGNORE(cmd) \
5729 { TARGET_ ## cmd, 0, #cmd },
5734 /* ??? Implement proper locking for ioctls. */
5735 /* do_ioctl() Must return target values and target errnos. */
5736 static abi_long
do_ioctl(int fd
, int cmd
, abi_long arg
)
5738 const IOCTLEntry
*ie
;
5739 const argtype
*arg_type
;
5741 uint8_t buf_temp
[MAX_STRUCT_SIZE
];
5747 if (ie
->target_cmd
== 0) {
5749 LOG_UNIMP
, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd
);
5750 return -TARGET_ENOTTY
;
5752 if (ie
->target_cmd
== cmd
)
5756 arg_type
= ie
->arg_type
;
5758 return ie
->do_ioctl(ie
, buf_temp
, fd
, cmd
, arg
);
5759 } else if (!ie
->host_cmd
) {
5760 /* Some architectures define BSD ioctls in their headers
5761 that are not implemented in Linux. */
5762 return -TARGET_ENOTTY
;
5765 switch(arg_type
[0]) {
5768 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
));
5774 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, arg
));
5778 target_size
= thunk_type_size(arg_type
, 0);
5779 switch(ie
->access
) {
5781 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5782 if (!is_error(ret
)) {
5783 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5785 return -TARGET_EFAULT
;
5786 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5787 unlock_user(argptr
, arg
, target_size
);
5791 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5793 return -TARGET_EFAULT
;
5794 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5795 unlock_user(argptr
, arg
, 0);
5796 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5800 argptr
= lock_user(VERIFY_READ
, arg
, target_size
, 1);
5802 return -TARGET_EFAULT
;
5803 thunk_convert(buf_temp
, argptr
, arg_type
, THUNK_HOST
);
5804 unlock_user(argptr
, arg
, 0);
5805 ret
= get_errno(safe_ioctl(fd
, ie
->host_cmd
, buf_temp
));
5806 if (!is_error(ret
)) {
5807 argptr
= lock_user(VERIFY_WRITE
, arg
, target_size
, 0);
5809 return -TARGET_EFAULT
;
5810 thunk_convert(argptr
, buf_temp
, arg_type
, THUNK_TARGET
);
5811 unlock_user(argptr
, arg
, target_size
);
5817 qemu_log_mask(LOG_UNIMP
,
5818 "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5819 (long)cmd
, arg_type
[0]);
5820 ret
= -TARGET_ENOTTY
;
5826 static const bitmask_transtbl iflag_tbl
[] = {
5827 { TARGET_IGNBRK
, TARGET_IGNBRK
, IGNBRK
, IGNBRK
},
5828 { TARGET_BRKINT
, TARGET_BRKINT
, BRKINT
, BRKINT
},
5829 { TARGET_IGNPAR
, TARGET_IGNPAR
, IGNPAR
, IGNPAR
},
5830 { TARGET_PARMRK
, TARGET_PARMRK
, PARMRK
, PARMRK
},
5831 { TARGET_INPCK
, TARGET_INPCK
, INPCK
, INPCK
},
5832 { TARGET_ISTRIP
, TARGET_ISTRIP
, ISTRIP
, ISTRIP
},
5833 { TARGET_INLCR
, TARGET_INLCR
, INLCR
, INLCR
},
5834 { TARGET_IGNCR
, TARGET_IGNCR
, IGNCR
, IGNCR
},
5835 { TARGET_ICRNL
, TARGET_ICRNL
, ICRNL
, ICRNL
},
5836 { TARGET_IUCLC
, TARGET_IUCLC
, IUCLC
, IUCLC
},
5837 { TARGET_IXON
, TARGET_IXON
, IXON
, IXON
},
5838 { TARGET_IXANY
, TARGET_IXANY
, IXANY
, IXANY
},
5839 { TARGET_IXOFF
, TARGET_IXOFF
, IXOFF
, IXOFF
},
5840 { TARGET_IMAXBEL
, TARGET_IMAXBEL
, IMAXBEL
, IMAXBEL
},
5841 { TARGET_IUTF8
, TARGET_IUTF8
, IUTF8
, IUTF8
},
5845 static const bitmask_transtbl oflag_tbl
[] = {
5846 { TARGET_OPOST
, TARGET_OPOST
, OPOST
, OPOST
},
5847 { TARGET_OLCUC
, TARGET_OLCUC
, OLCUC
, OLCUC
},
5848 { TARGET_ONLCR
, TARGET_ONLCR
, ONLCR
, ONLCR
},
5849 { TARGET_OCRNL
, TARGET_OCRNL
, OCRNL
, OCRNL
},
5850 { TARGET_ONOCR
, TARGET_ONOCR
, ONOCR
, ONOCR
},
5851 { TARGET_ONLRET
, TARGET_ONLRET
, ONLRET
, ONLRET
},
5852 { TARGET_OFILL
, TARGET_OFILL
, OFILL
, OFILL
},
5853 { TARGET_OFDEL
, TARGET_OFDEL
, OFDEL
, OFDEL
},
5854 { TARGET_NLDLY
, TARGET_NL0
, NLDLY
, NL0
},
5855 { TARGET_NLDLY
, TARGET_NL1
, NLDLY
, NL1
},
5856 { TARGET_CRDLY
, TARGET_CR0
, CRDLY
, CR0
},
5857 { TARGET_CRDLY
, TARGET_CR1
, CRDLY
, CR1
},
5858 { TARGET_CRDLY
, TARGET_CR2
, CRDLY
, CR2
},
5859 { TARGET_CRDLY
, TARGET_CR3
, CRDLY
, CR3
},
5860 { TARGET_TABDLY
, TARGET_TAB0
, TABDLY
, TAB0
},
5861 { TARGET_TABDLY
, TARGET_TAB1
, TABDLY
, TAB1
},
5862 { TARGET_TABDLY
, TARGET_TAB2
, TABDLY
, TAB2
},
5863 { TARGET_TABDLY
, TARGET_TAB3
, TABDLY
, TAB3
},
5864 { TARGET_BSDLY
, TARGET_BS0
, BSDLY
, BS0
},
5865 { TARGET_BSDLY
, TARGET_BS1
, BSDLY
, BS1
},
5866 { TARGET_VTDLY
, TARGET_VT0
, VTDLY
, VT0
},
5867 { TARGET_VTDLY
, TARGET_VT1
, VTDLY
, VT1
},
5868 { TARGET_FFDLY
, TARGET_FF0
, FFDLY
, FF0
},
5869 { TARGET_FFDLY
, TARGET_FF1
, FFDLY
, FF1
},
5873 static const bitmask_transtbl cflag_tbl
[] = {
5874 { TARGET_CBAUD
, TARGET_B0
, CBAUD
, B0
},
5875 { TARGET_CBAUD
, TARGET_B50
, CBAUD
, B50
},
5876 { TARGET_CBAUD
, TARGET_B75
, CBAUD
, B75
},
5877 { TARGET_CBAUD
, TARGET_B110
, CBAUD
, B110
},
5878 { TARGET_CBAUD
, TARGET_B134
, CBAUD
, B134
},
5879 { TARGET_CBAUD
, TARGET_B150
, CBAUD
, B150
},
5880 { TARGET_CBAUD
, TARGET_B200
, CBAUD
, B200
},
5881 { TARGET_CBAUD
, TARGET_B300
, CBAUD
, B300
},
5882 { TARGET_CBAUD
, TARGET_B600
, CBAUD
, B600
},
5883 { TARGET_CBAUD
, TARGET_B1200
, CBAUD
, B1200
},
5884 { TARGET_CBAUD
, TARGET_B1800
, CBAUD
, B1800
},
5885 { TARGET_CBAUD
, TARGET_B2400
, CBAUD
, B2400
},
5886 { TARGET_CBAUD
, TARGET_B4800
, CBAUD
, B4800
},
5887 { TARGET_CBAUD
, TARGET_B9600
, CBAUD
, B9600
},
5888 { TARGET_CBAUD
, TARGET_B19200
, CBAUD
, B19200
},
5889 { TARGET_CBAUD
, TARGET_B38400
, CBAUD
, B38400
},
5890 { TARGET_CBAUD
, TARGET_B57600
, CBAUD
, B57600
},
5891 { TARGET_CBAUD
, TARGET_B115200
, CBAUD
, B115200
},
5892 { TARGET_CBAUD
, TARGET_B230400
, CBAUD
, B230400
},
5893 { TARGET_CBAUD
, TARGET_B460800
, CBAUD
, B460800
},
5894 { TARGET_CSIZE
, TARGET_CS5
, CSIZE
, CS5
},
5895 { TARGET_CSIZE
, TARGET_CS6
, CSIZE
, CS6
},
5896 { TARGET_CSIZE
, TARGET_CS7
, CSIZE
, CS7
},
5897 { TARGET_CSIZE
, TARGET_CS8
, CSIZE
, CS8
},
5898 { TARGET_CSTOPB
, TARGET_CSTOPB
, CSTOPB
, CSTOPB
},
5899 { TARGET_CREAD
, TARGET_CREAD
, CREAD
, CREAD
},
5900 { TARGET_PARENB
, TARGET_PARENB
, PARENB
, PARENB
},
5901 { TARGET_PARODD
, TARGET_PARODD
, PARODD
, PARODD
},
5902 { TARGET_HUPCL
, TARGET_HUPCL
, HUPCL
, HUPCL
},
5903 { TARGET_CLOCAL
, TARGET_CLOCAL
, CLOCAL
, CLOCAL
},
5904 { TARGET_CRTSCTS
, TARGET_CRTSCTS
, CRTSCTS
, CRTSCTS
},
5908 static const bitmask_transtbl lflag_tbl
[] = {
5909 { TARGET_ISIG
, TARGET_ISIG
, ISIG
, ISIG
},
5910 { TARGET_ICANON
, TARGET_ICANON
, ICANON
, ICANON
},
5911 { TARGET_XCASE
, TARGET_XCASE
, XCASE
, XCASE
},
5912 { TARGET_ECHO
, TARGET_ECHO
, ECHO
, ECHO
},
5913 { TARGET_ECHOE
, TARGET_ECHOE
, ECHOE
, ECHOE
},
5914 { TARGET_ECHOK
, TARGET_ECHOK
, ECHOK
, ECHOK
},
5915 { TARGET_ECHONL
, TARGET_ECHONL
, ECHONL
, ECHONL
},
5916 { TARGET_NOFLSH
, TARGET_NOFLSH
, NOFLSH
, NOFLSH
},
5917 { TARGET_TOSTOP
, TARGET_TOSTOP
, TOSTOP
, TOSTOP
},
5918 { TARGET_ECHOCTL
, TARGET_ECHOCTL
, ECHOCTL
, ECHOCTL
},
5919 { TARGET_ECHOPRT
, TARGET_ECHOPRT
, ECHOPRT
, ECHOPRT
},
5920 { TARGET_ECHOKE
, TARGET_ECHOKE
, ECHOKE
, ECHOKE
},
5921 { TARGET_FLUSHO
, TARGET_FLUSHO
, FLUSHO
, FLUSHO
},
5922 { TARGET_PENDIN
, TARGET_PENDIN
, PENDIN
, PENDIN
},
5923 { TARGET_IEXTEN
, TARGET_IEXTEN
, IEXTEN
, IEXTEN
},
5924 { TARGET_EXTPROC
, TARGET_EXTPROC
, EXTPROC
, EXTPROC
},
5928 static void target_to_host_termios (void *dst
, const void *src
)
5930 struct host_termios
*host
= dst
;
5931 const struct target_termios
*target
= src
;
5934 target_to_host_bitmask(tswap32(target
->c_iflag
), iflag_tbl
);
5936 target_to_host_bitmask(tswap32(target
->c_oflag
), oflag_tbl
);
5938 target_to_host_bitmask(tswap32(target
->c_cflag
), cflag_tbl
);
5940 target_to_host_bitmask(tswap32(target
->c_lflag
), lflag_tbl
);
5941 host
->c_line
= target
->c_line
;
5943 memset(host
->c_cc
, 0, sizeof(host
->c_cc
));
5944 host
->c_cc
[VINTR
] = target
->c_cc
[TARGET_VINTR
];
5945 host
->c_cc
[VQUIT
] = target
->c_cc
[TARGET_VQUIT
];
5946 host
->c_cc
[VERASE
] = target
->c_cc
[TARGET_VERASE
];
5947 host
->c_cc
[VKILL
] = target
->c_cc
[TARGET_VKILL
];
5948 host
->c_cc
[VEOF
] = target
->c_cc
[TARGET_VEOF
];
5949 host
->c_cc
[VTIME
] = target
->c_cc
[TARGET_VTIME
];
5950 host
->c_cc
[VMIN
] = target
->c_cc
[TARGET_VMIN
];
5951 host
->c_cc
[VSWTC
] = target
->c_cc
[TARGET_VSWTC
];
5952 host
->c_cc
[VSTART
] = target
->c_cc
[TARGET_VSTART
];
5953 host
->c_cc
[VSTOP
] = target
->c_cc
[TARGET_VSTOP
];
5954 host
->c_cc
[VSUSP
] = target
->c_cc
[TARGET_VSUSP
];
5955 host
->c_cc
[VEOL
] = target
->c_cc
[TARGET_VEOL
];
5956 host
->c_cc
[VREPRINT
] = target
->c_cc
[TARGET_VREPRINT
];
5957 host
->c_cc
[VDISCARD
] = target
->c_cc
[TARGET_VDISCARD
];
5958 host
->c_cc
[VWERASE
] = target
->c_cc
[TARGET_VWERASE
];
5959 host
->c_cc
[VLNEXT
] = target
->c_cc
[TARGET_VLNEXT
];
5960 host
->c_cc
[VEOL2
] = target
->c_cc
[TARGET_VEOL2
];
5963 static void host_to_target_termios (void *dst
, const void *src
)
5965 struct target_termios
*target
= dst
;
5966 const struct host_termios
*host
= src
;
5969 tswap32(host_to_target_bitmask(host
->c_iflag
, iflag_tbl
));
5971 tswap32(host_to_target_bitmask(host
->c_oflag
, oflag_tbl
));
5973 tswap32(host_to_target_bitmask(host
->c_cflag
, cflag_tbl
));
5975 tswap32(host_to_target_bitmask(host
->c_lflag
, lflag_tbl
));
5976 target
->c_line
= host
->c_line
;
5978 memset(target
->c_cc
, 0, sizeof(target
->c_cc
));
5979 target
->c_cc
[TARGET_VINTR
] = host
->c_cc
[VINTR
];
5980 target
->c_cc
[TARGET_VQUIT
] = host
->c_cc
[VQUIT
];
5981 target
->c_cc
[TARGET_VERASE
] = host
->c_cc
[VERASE
];
5982 target
->c_cc
[TARGET_VKILL
] = host
->c_cc
[VKILL
];
5983 target
->c_cc
[TARGET_VEOF
] = host
->c_cc
[VEOF
];
5984 target
->c_cc
[TARGET_VTIME
] = host
->c_cc
[VTIME
];
5985 target
->c_cc
[TARGET_VMIN
] = host
->c_cc
[VMIN
];
5986 target
->c_cc
[TARGET_VSWTC
] = host
->c_cc
[VSWTC
];
5987 target
->c_cc
[TARGET_VSTART
] = host
->c_cc
[VSTART
];
5988 target
->c_cc
[TARGET_VSTOP
] = host
->c_cc
[VSTOP
];
5989 target
->c_cc
[TARGET_VSUSP
] = host
->c_cc
[VSUSP
];
5990 target
->c_cc
[TARGET_VEOL
] = host
->c_cc
[VEOL
];
5991 target
->c_cc
[TARGET_VREPRINT
] = host
->c_cc
[VREPRINT
];
5992 target
->c_cc
[TARGET_VDISCARD
] = host
->c_cc
[VDISCARD
];
5993 target
->c_cc
[TARGET_VWERASE
] = host
->c_cc
[VWERASE
];
5994 target
->c_cc
[TARGET_VLNEXT
] = host
->c_cc
[VLNEXT
];
5995 target
->c_cc
[TARGET_VEOL2
] = host
->c_cc
[VEOL2
];
5998 static const StructEntry struct_termios_def
= {
5999 .convert
= { host_to_target_termios
, target_to_host_termios
},
6000 .size
= { sizeof(struct target_termios
), sizeof(struct host_termios
) },
6001 .align
= { __alignof__(struct target_termios
), __alignof__(struct host_termios
) },
6002 .print
= print_termios
,
6005 static const bitmask_transtbl mmap_flags_tbl
[] = {
6006 { TARGET_MAP_SHARED
, TARGET_MAP_SHARED
, MAP_SHARED
, MAP_SHARED
},
6007 { TARGET_MAP_PRIVATE
, TARGET_MAP_PRIVATE
, MAP_PRIVATE
, MAP_PRIVATE
},
6008 { TARGET_MAP_FIXED
, TARGET_MAP_FIXED
, MAP_FIXED
, MAP_FIXED
},
6009 { TARGET_MAP_ANONYMOUS
, TARGET_MAP_ANONYMOUS
,
6010 MAP_ANONYMOUS
, MAP_ANONYMOUS
},
6011 { TARGET_MAP_GROWSDOWN
, TARGET_MAP_GROWSDOWN
,
6012 MAP_GROWSDOWN
, MAP_GROWSDOWN
},
6013 { TARGET_MAP_DENYWRITE
, TARGET_MAP_DENYWRITE
,
6014 MAP_DENYWRITE
, MAP_DENYWRITE
},
6015 { TARGET_MAP_EXECUTABLE
, TARGET_MAP_EXECUTABLE
,
6016 MAP_EXECUTABLE
, MAP_EXECUTABLE
},
6017 { TARGET_MAP_LOCKED
, TARGET_MAP_LOCKED
, MAP_LOCKED
, MAP_LOCKED
},
6018 { TARGET_MAP_NORESERVE
, TARGET_MAP_NORESERVE
,
6019 MAP_NORESERVE
, MAP_NORESERVE
},
6020 { TARGET_MAP_HUGETLB
, TARGET_MAP_HUGETLB
, MAP_HUGETLB
, MAP_HUGETLB
},
6021 /* MAP_STACK had been ignored by the kernel for quite some time.
6022 Recognize it for the target insofar as we do not want to pass
6023 it through to the host. */
6024 { TARGET_MAP_STACK
, TARGET_MAP_STACK
, 0, 0 },
6029 * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
6030 * TARGET_I386 is defined if TARGET_X86_64 is defined
6032 #if defined(TARGET_I386)
6034 /* NOTE: there is really one LDT for all the threads */
6035 static uint8_t *ldt_table
;
6037 static abi_long
read_ldt(abi_ulong ptr
, unsigned long bytecount
)
6044 size
= TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
;
6045 if (size
> bytecount
)
6047 p
= lock_user(VERIFY_WRITE
, ptr
, size
, 0);
6049 return -TARGET_EFAULT
;
6050 /* ??? Should this by byteswapped? */
6051 memcpy(p
, ldt_table
, size
);
6052 unlock_user(p
, ptr
, size
);
6056 /* XXX: add locking support */
6057 static abi_long
write_ldt(CPUX86State
*env
,
6058 abi_ulong ptr
, unsigned long bytecount
, int oldmode
)
6060 struct target_modify_ldt_ldt_s ldt_info
;
6061 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6062 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
6063 int seg_not_present
, useable
, lm
;
6064 uint32_t *lp
, entry_1
, entry_2
;
6066 if (bytecount
!= sizeof(ldt_info
))
6067 return -TARGET_EINVAL
;
6068 if (!lock_user_struct(VERIFY_READ
, target_ldt_info
, ptr
, 1))
6069 return -TARGET_EFAULT
;
6070 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
6071 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6072 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6073 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6074 unlock_user_struct(target_ldt_info
, ptr
, 0);
6076 if (ldt_info
.entry_number
>= TARGET_LDT_ENTRIES
)
6077 return -TARGET_EINVAL
;
6078 seg_32bit
= ldt_info
.flags
& 1;
6079 contents
= (ldt_info
.flags
>> 1) & 3;
6080 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6081 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6082 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6083 useable
= (ldt_info
.flags
>> 6) & 1;
6087 lm
= (ldt_info
.flags
>> 7) & 1;
6089 if (contents
== 3) {
6091 return -TARGET_EINVAL
;
6092 if (seg_not_present
== 0)
6093 return -TARGET_EINVAL
;
6095 /* allocate the LDT */
6097 env
->ldt
.base
= target_mmap(0,
6098 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
,
6099 PROT_READ
|PROT_WRITE
,
6100 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
6101 if (env
->ldt
.base
== -1)
6102 return -TARGET_ENOMEM
;
6103 memset(g2h_untagged(env
->ldt
.base
), 0,
6104 TARGET_LDT_ENTRIES
* TARGET_LDT_ENTRY_SIZE
);
6105 env
->ldt
.limit
= 0xffff;
6106 ldt_table
= g2h_untagged(env
->ldt
.base
);
6109 /* NOTE: same code as Linux kernel */
6110 /* Allow LDTs to be cleared by the user. */
6111 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6114 read_exec_only
== 1 &&
6116 limit_in_pages
== 0 &&
6117 seg_not_present
== 1 &&
6125 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6126 (ldt_info
.limit
& 0x0ffff);
6127 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6128 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6129 (ldt_info
.limit
& 0xf0000) |
6130 ((read_exec_only
^ 1) << 9) |
6132 ((seg_not_present
^ 1) << 15) |
6134 (limit_in_pages
<< 23) |
6138 entry_2
|= (useable
<< 20);
6140 /* Install the new entry ... */
6142 lp
= (uint32_t *)(ldt_table
+ (ldt_info
.entry_number
<< 3));
6143 lp
[0] = tswap32(entry_1
);
6144 lp
[1] = tswap32(entry_2
);
6148 /* specific and weird i386 syscalls */
6149 static abi_long
do_modify_ldt(CPUX86State
*env
, int func
, abi_ulong ptr
,
6150 unsigned long bytecount
)
6156 ret
= read_ldt(ptr
, bytecount
);
6159 ret
= write_ldt(env
, ptr
, bytecount
, 1);
6162 ret
= write_ldt(env
, ptr
, bytecount
, 0);
6165 ret
= -TARGET_ENOSYS
;
6171 #if defined(TARGET_ABI32)
6172 abi_long
do_set_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6174 uint64_t *gdt_table
= g2h_untagged(env
->gdt
.base
);
6175 struct target_modify_ldt_ldt_s ldt_info
;
6176 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6177 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
;
6178 int seg_not_present
, useable
, lm
;
6179 uint32_t *lp
, entry_1
, entry_2
;
6182 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6183 if (!target_ldt_info
)
6184 return -TARGET_EFAULT
;
6185 ldt_info
.entry_number
= tswap32(target_ldt_info
->entry_number
);
6186 ldt_info
.base_addr
= tswapal(target_ldt_info
->base_addr
);
6187 ldt_info
.limit
= tswap32(target_ldt_info
->limit
);
6188 ldt_info
.flags
= tswap32(target_ldt_info
->flags
);
6189 if (ldt_info
.entry_number
== -1) {
6190 for (i
=TARGET_GDT_ENTRY_TLS_MIN
; i
<=TARGET_GDT_ENTRY_TLS_MAX
; i
++) {
6191 if (gdt_table
[i
] == 0) {
6192 ldt_info
.entry_number
= i
;
6193 target_ldt_info
->entry_number
= tswap32(i
);
6198 unlock_user_struct(target_ldt_info
, ptr
, 1);
6200 if (ldt_info
.entry_number
< TARGET_GDT_ENTRY_TLS_MIN
||
6201 ldt_info
.entry_number
> TARGET_GDT_ENTRY_TLS_MAX
)
6202 return -TARGET_EINVAL
;
6203 seg_32bit
= ldt_info
.flags
& 1;
6204 contents
= (ldt_info
.flags
>> 1) & 3;
6205 read_exec_only
= (ldt_info
.flags
>> 3) & 1;
6206 limit_in_pages
= (ldt_info
.flags
>> 4) & 1;
6207 seg_not_present
= (ldt_info
.flags
>> 5) & 1;
6208 useable
= (ldt_info
.flags
>> 6) & 1;
6212 lm
= (ldt_info
.flags
>> 7) & 1;
6215 if (contents
== 3) {
6216 if (seg_not_present
== 0)
6217 return -TARGET_EINVAL
;
6220 /* NOTE: same code as Linux kernel */
6221 /* Allow LDTs to be cleared by the user. */
6222 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
6223 if ((contents
== 0 &&
6224 read_exec_only
== 1 &&
6226 limit_in_pages
== 0 &&
6227 seg_not_present
== 1 &&
6235 entry_1
= ((ldt_info
.base_addr
& 0x0000ffff) << 16) |
6236 (ldt_info
.limit
& 0x0ffff);
6237 entry_2
= (ldt_info
.base_addr
& 0xff000000) |
6238 ((ldt_info
.base_addr
& 0x00ff0000) >> 16) |
6239 (ldt_info
.limit
& 0xf0000) |
6240 ((read_exec_only
^ 1) << 9) |
6242 ((seg_not_present
^ 1) << 15) |
6244 (limit_in_pages
<< 23) |
6249 /* Install the new entry ... */
6251 lp
= (uint32_t *)(gdt_table
+ ldt_info
.entry_number
);
6252 lp
[0] = tswap32(entry_1
);
6253 lp
[1] = tswap32(entry_2
);
6257 static abi_long
do_get_thread_area(CPUX86State
*env
, abi_ulong ptr
)
6259 struct target_modify_ldt_ldt_s
*target_ldt_info
;
6260 uint64_t *gdt_table
= g2h_untagged(env
->gdt
.base
);
6261 uint32_t base_addr
, limit
, flags
;
6262 int seg_32bit
, contents
, read_exec_only
, limit_in_pages
, idx
;
6263 int seg_not_present
, useable
, lm
;
6264 uint32_t *lp
, entry_1
, entry_2
;
6266 lock_user_struct(VERIFY_WRITE
, target_ldt_info
, ptr
, 1);
6267 if (!target_ldt_info
)
6268 return -TARGET_EFAULT
;
6269 idx
= tswap32(target_ldt_info
->entry_number
);
6270 if (idx
< TARGET_GDT_ENTRY_TLS_MIN
||
6271 idx
> TARGET_GDT_ENTRY_TLS_MAX
) {
6272 unlock_user_struct(target_ldt_info
, ptr
, 1);
6273 return -TARGET_EINVAL
;
6275 lp
= (uint32_t *)(gdt_table
+ idx
);
6276 entry_1
= tswap32(lp
[0]);
6277 entry_2
= tswap32(lp
[1]);
6279 read_exec_only
= ((entry_2
>> 9) & 1) ^ 1;
6280 contents
= (entry_2
>> 10) & 3;
6281 seg_not_present
= ((entry_2
>> 15) & 1) ^ 1;
6282 seg_32bit
= (entry_2
>> 22) & 1;
6283 limit_in_pages
= (entry_2
>> 23) & 1;
6284 useable
= (entry_2
>> 20) & 1;
6288 lm
= (entry_2
>> 21) & 1;
6290 flags
= (seg_32bit
<< 0) | (contents
<< 1) |
6291 (read_exec_only
<< 3) | (limit_in_pages
<< 4) |
6292 (seg_not_present
<< 5) | (useable
<< 6) | (lm
<< 7);
6293 limit
= (entry_1
& 0xffff) | (entry_2
& 0xf0000);
6294 base_addr
= (entry_1
>> 16) |
6295 (entry_2
& 0xff000000) |
6296 ((entry_2
& 0xff) << 16);
6297 target_ldt_info
->base_addr
= tswapal(base_addr
);
6298 target_ldt_info
->limit
= tswap32(limit
);
6299 target_ldt_info
->flags
= tswap32(flags
);
6300 unlock_user_struct(target_ldt_info
, ptr
, 1);
6304 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
6306 return -TARGET_ENOSYS
;
6309 abi_long
do_arch_prctl(CPUX86State
*env
, int code
, abi_ulong addr
)
6316 case TARGET_ARCH_SET_GS
:
6317 case TARGET_ARCH_SET_FS
:
6318 if (code
== TARGET_ARCH_SET_GS
)
6322 cpu_x86_load_seg(env
, idx
, 0);
6323 env
->segs
[idx
].base
= addr
;
6325 case TARGET_ARCH_GET_GS
:
6326 case TARGET_ARCH_GET_FS
:
6327 if (code
== TARGET_ARCH_GET_GS
)
6331 val
= env
->segs
[idx
].base
;
6332 if (put_user(val
, addr
, abi_ulong
))
6333 ret
= -TARGET_EFAULT
;
6336 ret
= -TARGET_EINVAL
;
6341 #endif /* defined(TARGET_ABI32 */
6342 #endif /* defined(TARGET_I386) */
6345 * These constants are generic. Supply any that are missing from the host.
6348 # define PR_SET_NAME 15
6349 # define PR_GET_NAME 16
6351 #ifndef PR_SET_FP_MODE
6352 # define PR_SET_FP_MODE 45
6353 # define PR_GET_FP_MODE 46
6354 # define PR_FP_MODE_FR (1 << 0)
6355 # define PR_FP_MODE_FRE (1 << 1)
6357 #ifndef PR_SVE_SET_VL
6358 # define PR_SVE_SET_VL 50
6359 # define PR_SVE_GET_VL 51
6360 # define PR_SVE_VL_LEN_MASK 0xffff
6361 # define PR_SVE_VL_INHERIT (1 << 17)
6363 #ifndef PR_PAC_RESET_KEYS
6364 # define PR_PAC_RESET_KEYS 54
6365 # define PR_PAC_APIAKEY (1 << 0)
6366 # define PR_PAC_APIBKEY (1 << 1)
6367 # define PR_PAC_APDAKEY (1 << 2)
6368 # define PR_PAC_APDBKEY (1 << 3)
6369 # define PR_PAC_APGAKEY (1 << 4)
6371 #ifndef PR_SET_TAGGED_ADDR_CTRL
6372 # define PR_SET_TAGGED_ADDR_CTRL 55
6373 # define PR_GET_TAGGED_ADDR_CTRL 56
6374 # define PR_TAGGED_ADDR_ENABLE (1UL << 0)
6376 #ifndef PR_MTE_TCF_SHIFT
6377 # define PR_MTE_TCF_SHIFT 1
6378 # define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
6379 # define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
6380 # define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
6381 # define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
6382 # define PR_MTE_TAG_SHIFT 3
6383 # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
6385 #ifndef PR_SET_IO_FLUSHER
6386 # define PR_SET_IO_FLUSHER 57
6387 # define PR_GET_IO_FLUSHER 58
6389 #ifndef PR_SET_SYSCALL_USER_DISPATCH
6390 # define PR_SET_SYSCALL_USER_DISPATCH 59
6392 #ifndef PR_SME_SET_VL
6393 # define PR_SME_SET_VL 63
6394 # define PR_SME_GET_VL 64
6395 # define PR_SME_VL_LEN_MASK 0xffff
6396 # define PR_SME_VL_INHERIT (1 << 17)
6399 #include "target_prctl.h"
6401 static abi_long
do_prctl_inval0(CPUArchState
*env
)
6403 return -TARGET_EINVAL
;
6406 static abi_long
do_prctl_inval1(CPUArchState
*env
, abi_long arg2
)
6408 return -TARGET_EINVAL
;
6411 #ifndef do_prctl_get_fp_mode
6412 #define do_prctl_get_fp_mode do_prctl_inval0
6414 #ifndef do_prctl_set_fp_mode
6415 #define do_prctl_set_fp_mode do_prctl_inval1
6417 #ifndef do_prctl_sve_get_vl
6418 #define do_prctl_sve_get_vl do_prctl_inval0
6420 #ifndef do_prctl_sve_set_vl
6421 #define do_prctl_sve_set_vl do_prctl_inval1
6423 #ifndef do_prctl_reset_keys
6424 #define do_prctl_reset_keys do_prctl_inval1
6426 #ifndef do_prctl_set_tagged_addr_ctrl
6427 #define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
6429 #ifndef do_prctl_get_tagged_addr_ctrl
6430 #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
6432 #ifndef do_prctl_get_unalign
6433 #define do_prctl_get_unalign do_prctl_inval1
6435 #ifndef do_prctl_set_unalign
6436 #define do_prctl_set_unalign do_prctl_inval1
6438 #ifndef do_prctl_sme_get_vl
6439 #define do_prctl_sme_get_vl do_prctl_inval0
6441 #ifndef do_prctl_sme_set_vl
6442 #define do_prctl_sme_set_vl do_prctl_inval1
6445 static abi_long
do_prctl(CPUArchState
*env
, abi_long option
, abi_long arg2
,
6446 abi_long arg3
, abi_long arg4
, abi_long arg5
)
6451 case PR_GET_PDEATHSIG
:
6454 ret
= get_errno(prctl(PR_GET_PDEATHSIG
, &deathsig
,
6456 if (!is_error(ret
) &&
6457 put_user_s32(host_to_target_signal(deathsig
), arg2
)) {
6458 return -TARGET_EFAULT
;
6462 case PR_SET_PDEATHSIG
:
6463 return get_errno(prctl(PR_SET_PDEATHSIG
, target_to_host_signal(arg2
),
6467 void *name
= lock_user(VERIFY_WRITE
, arg2
, 16, 1);
6469 return -TARGET_EFAULT
;
6471 ret
= get_errno(prctl(PR_GET_NAME
, (uintptr_t)name
,
6473 unlock_user(name
, arg2
, 16);
6478 void *name
= lock_user(VERIFY_READ
, arg2
, 16, 1);
6480 return -TARGET_EFAULT
;
6482 ret
= get_errno(prctl(PR_SET_NAME
, (uintptr_t)name
,
6484 unlock_user(name
, arg2
, 0);
6487 case PR_GET_FP_MODE
:
6488 return do_prctl_get_fp_mode(env
);
6489 case PR_SET_FP_MODE
:
6490 return do_prctl_set_fp_mode(env
, arg2
);
6492 return do_prctl_sve_get_vl(env
);
6494 return do_prctl_sve_set_vl(env
, arg2
);
6496 return do_prctl_sme_get_vl(env
);
6498 return do_prctl_sme_set_vl(env
, arg2
);
6499 case PR_PAC_RESET_KEYS
:
6500 if (arg3
|| arg4
|| arg5
) {
6501 return -TARGET_EINVAL
;
6503 return do_prctl_reset_keys(env
, arg2
);
6504 case PR_SET_TAGGED_ADDR_CTRL
:
6505 if (arg3
|| arg4
|| arg5
) {
6506 return -TARGET_EINVAL
;
6508 return do_prctl_set_tagged_addr_ctrl(env
, arg2
);
6509 case PR_GET_TAGGED_ADDR_CTRL
:
6510 if (arg2
|| arg3
|| arg4
|| arg5
) {
6511 return -TARGET_EINVAL
;
6513 return do_prctl_get_tagged_addr_ctrl(env
);
6515 case PR_GET_UNALIGN
:
6516 return do_prctl_get_unalign(env
, arg2
);
6517 case PR_SET_UNALIGN
:
6518 return do_prctl_set_unalign(env
, arg2
);
6520 case PR_CAP_AMBIENT
:
6521 case PR_CAPBSET_READ
:
6522 case PR_CAPBSET_DROP
:
6523 case PR_GET_DUMPABLE
:
6524 case PR_SET_DUMPABLE
:
6525 case PR_GET_KEEPCAPS
:
6526 case PR_SET_KEEPCAPS
:
6527 case PR_GET_SECUREBITS
:
6528 case PR_SET_SECUREBITS
:
6531 case PR_GET_TIMERSLACK
:
6532 case PR_SET_TIMERSLACK
:
6534 case PR_MCE_KILL_GET
:
6535 case PR_GET_NO_NEW_PRIVS
:
6536 case PR_SET_NO_NEW_PRIVS
:
6537 case PR_GET_IO_FLUSHER
:
6538 case PR_SET_IO_FLUSHER
:
6539 /* Some prctl options have no pointer arguments and we can pass on. */
6540 return get_errno(prctl(option
, arg2
, arg3
, arg4
, arg5
));
6542 case PR_GET_CHILD_SUBREAPER
:
6543 case PR_SET_CHILD_SUBREAPER
:
6544 case PR_GET_SPECULATION_CTRL
:
6545 case PR_SET_SPECULATION_CTRL
:
6546 case PR_GET_TID_ADDRESS
:
6548 return -TARGET_EINVAL
;
6552 /* Was used for SPE on PowerPC. */
6553 return -TARGET_EINVAL
;
6560 case PR_GET_SECCOMP
:
6561 case PR_SET_SECCOMP
:
6562 case PR_SET_SYSCALL_USER_DISPATCH
:
6563 case PR_GET_THP_DISABLE
:
6564 case PR_SET_THP_DISABLE
:
6567 /* Disable to prevent the target disabling stuff we need. */
6568 return -TARGET_EINVAL
;
6571 qemu_log_mask(LOG_UNIMP
, "Unsupported prctl: " TARGET_ABI_FMT_ld
"\n",
6573 return -TARGET_EINVAL
;
6577 #define NEW_STACK_SIZE 0x40000
6580 static pthread_mutex_t clone_lock
= PTHREAD_MUTEX_INITIALIZER
;
6583 pthread_mutex_t mutex
;
6584 pthread_cond_t cond
;
6587 abi_ulong child_tidptr
;
6588 abi_ulong parent_tidptr
;
6592 static void *clone_func(void *arg
)
6594 new_thread_info
*info
= arg
;
6599 rcu_register_thread();
6600 tcg_register_thread();
6604 ts
= (TaskState
*)cpu
->opaque
;
6605 info
->tid
= sys_gettid();
6607 if (info
->child_tidptr
)
6608 put_user_u32(info
->tid
, info
->child_tidptr
);
6609 if (info
->parent_tidptr
)
6610 put_user_u32(info
->tid
, info
->parent_tidptr
);
6611 qemu_guest_random_seed_thread_part2(cpu
->random_seed
);
6612 /* Enable signals. */
6613 sigprocmask(SIG_SETMASK
, &info
->sigmask
, NULL
);
6614 /* Signal to the parent that we're ready. */
6615 pthread_mutex_lock(&info
->mutex
);
6616 pthread_cond_broadcast(&info
->cond
);
6617 pthread_mutex_unlock(&info
->mutex
);
6618 /* Wait until the parent has finished initializing the tls state. */
6619 pthread_mutex_lock(&clone_lock
);
6620 pthread_mutex_unlock(&clone_lock
);
6626 /* do_fork() Must return host values and target errnos (unlike most
6627 do_*() functions). */
6628 static int do_fork(CPUArchState
*env
, unsigned int flags
, abi_ulong newsp
,
6629 abi_ulong parent_tidptr
, target_ulong newtls
,
6630 abi_ulong child_tidptr
)
6632 CPUState
*cpu
= env_cpu(env
);
6636 CPUArchState
*new_env
;
6639 flags
&= ~CLONE_IGNORED_FLAGS
;
6641 /* Emulate vfork() with fork() */
6642 if (flags
& CLONE_VFORK
)
6643 flags
&= ~(CLONE_VFORK
| CLONE_VM
);
6645 if (flags
& CLONE_VM
) {
6646 TaskState
*parent_ts
= (TaskState
*)cpu
->opaque
;
6647 new_thread_info info
;
6648 pthread_attr_t attr
;
6650 if (((flags
& CLONE_THREAD_FLAGS
) != CLONE_THREAD_FLAGS
) ||
6651 (flags
& CLONE_INVALID_THREAD_FLAGS
)) {
6652 return -TARGET_EINVAL
;
6655 ts
= g_new0(TaskState
, 1);
6656 init_task_state(ts
);
6658 /* Grab a mutex so that thread setup appears atomic. */
6659 pthread_mutex_lock(&clone_lock
);
6662 * If this is our first additional thread, we need to ensure we
6663 * generate code for parallel execution and flush old translations.
6664 * Do this now so that the copy gets CF_PARALLEL too.
6666 if (!(cpu
->tcg_cflags
& CF_PARALLEL
)) {
6667 cpu
->tcg_cflags
|= CF_PARALLEL
;
6671 /* we create a new CPU instance. */
6672 new_env
= cpu_copy(env
);
6673 /* Init regs that differ from the parent. */
6674 cpu_clone_regs_child(new_env
, newsp
, flags
);
6675 cpu_clone_regs_parent(env
, flags
);
6676 new_cpu
= env_cpu(new_env
);
6677 new_cpu
->opaque
= ts
;
6678 ts
->bprm
= parent_ts
->bprm
;
6679 ts
->info
= parent_ts
->info
;
6680 ts
->signal_mask
= parent_ts
->signal_mask
;
6682 if (flags
& CLONE_CHILD_CLEARTID
) {
6683 ts
->child_tidptr
= child_tidptr
;
6686 if (flags
& CLONE_SETTLS
) {
6687 cpu_set_tls (new_env
, newtls
);
6690 memset(&info
, 0, sizeof(info
));
6691 pthread_mutex_init(&info
.mutex
, NULL
);
6692 pthread_mutex_lock(&info
.mutex
);
6693 pthread_cond_init(&info
.cond
, NULL
);
6695 if (flags
& CLONE_CHILD_SETTID
) {
6696 info
.child_tidptr
= child_tidptr
;
6698 if (flags
& CLONE_PARENT_SETTID
) {
6699 info
.parent_tidptr
= parent_tidptr
;
6702 ret
= pthread_attr_init(&attr
);
6703 ret
= pthread_attr_setstacksize(&attr
, NEW_STACK_SIZE
);
6704 ret
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
6705 /* It is not safe to deliver signals until the child has finished
6706 initializing, so temporarily block all signals. */
6707 sigfillset(&sigmask
);
6708 sigprocmask(SIG_BLOCK
, &sigmask
, &info
.sigmask
);
6709 cpu
->random_seed
= qemu_guest_random_seed_thread_part1();
6711 ret
= pthread_create(&info
.thread
, &attr
, clone_func
, &info
);
6712 /* TODO: Free new CPU state if thread creation failed. */
6714 sigprocmask(SIG_SETMASK
, &info
.sigmask
, NULL
);
6715 pthread_attr_destroy(&attr
);
6717 /* Wait for the child to initialize. */
6718 pthread_cond_wait(&info
.cond
, &info
.mutex
);
6723 pthread_mutex_unlock(&info
.mutex
);
6724 pthread_cond_destroy(&info
.cond
);
6725 pthread_mutex_destroy(&info
.mutex
);
6726 pthread_mutex_unlock(&clone_lock
);
6728 /* if no CLONE_VM, we consider it is a fork */
6729 if (flags
& CLONE_INVALID_FORK_FLAGS
) {
6730 return -TARGET_EINVAL
;
6733 /* We can't support custom termination signals */
6734 if ((flags
& CSIGNAL
) != TARGET_SIGCHLD
) {
6735 return -TARGET_EINVAL
;
6738 #if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
6739 if (flags
& CLONE_PIDFD
) {
6740 return -TARGET_EINVAL
;
6744 /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
6745 if ((flags
& CLONE_PIDFD
) && (flags
& CLONE_PARENT_SETTID
)) {
6746 return -TARGET_EINVAL
;
6749 if (block_signals()) {
6750 return -QEMU_ERESTARTSYS
;
6756 /* Child Process. */
6757 cpu_clone_regs_child(env
, newsp
, flags
);
6759 /* There is a race condition here. The parent process could
6760 theoretically read the TID in the child process before the child
6761 tid is set. This would require using either ptrace
6762 (not implemented) or having *_tidptr to point at a shared memory
6763 mapping. We can't repeat the spinlock hack used above because
6764 the child process gets its own copy of the lock. */
6765 if (flags
& CLONE_CHILD_SETTID
)
6766 put_user_u32(sys_gettid(), child_tidptr
);
6767 if (flags
& CLONE_PARENT_SETTID
)
6768 put_user_u32(sys_gettid(), parent_tidptr
);
6769 ts
= (TaskState
*)cpu
->opaque
;
6770 if (flags
& CLONE_SETTLS
)
6771 cpu_set_tls (env
, newtls
);
6772 if (flags
& CLONE_CHILD_CLEARTID
)
6773 ts
->child_tidptr
= child_tidptr
;
6775 cpu_clone_regs_parent(env
, flags
);
6776 if (flags
& CLONE_PIDFD
) {
6778 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
6779 int pid_child
= ret
;
6780 pid_fd
= pidfd_open(pid_child
, 0);
6782 fcntl(pid_fd
, F_SETFD
, fcntl(pid_fd
, F_GETFL
)
6788 put_user_u32(pid_fd
, parent_tidptr
);
6792 g_assert(!cpu_in_exclusive_context(cpu
));
6797 /* warning : doesn't handle linux specific flags... */
6798 static int target_to_host_fcntl_cmd(int cmd
)
6803 case TARGET_F_DUPFD
:
6804 case TARGET_F_GETFD
:
6805 case TARGET_F_SETFD
:
6806 case TARGET_F_GETFL
:
6807 case TARGET_F_SETFL
:
6808 case TARGET_F_OFD_GETLK
:
6809 case TARGET_F_OFD_SETLK
:
6810 case TARGET_F_OFD_SETLKW
:
6813 case TARGET_F_GETLK
:
6816 case TARGET_F_SETLK
:
6819 case TARGET_F_SETLKW
:
6822 case TARGET_F_GETOWN
:
6825 case TARGET_F_SETOWN
:
6828 case TARGET_F_GETSIG
:
6831 case TARGET_F_SETSIG
:
6834 #if TARGET_ABI_BITS == 32
6835 case TARGET_F_GETLK64
:
6838 case TARGET_F_SETLK64
:
6841 case TARGET_F_SETLKW64
:
6845 case TARGET_F_SETLEASE
:
6848 case TARGET_F_GETLEASE
:
6851 #ifdef F_DUPFD_CLOEXEC
6852 case TARGET_F_DUPFD_CLOEXEC
:
6853 ret
= F_DUPFD_CLOEXEC
;
6856 case TARGET_F_NOTIFY
:
6860 case TARGET_F_GETOWN_EX
:
6865 case TARGET_F_SETOWN_EX
:
6870 case TARGET_F_SETPIPE_SZ
:
6873 case TARGET_F_GETPIPE_SZ
:
6878 case TARGET_F_ADD_SEALS
:
6881 case TARGET_F_GET_SEALS
:
6886 ret
= -TARGET_EINVAL
;
6890 #if defined(__powerpc64__)
6891 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6892 * is not supported by kernel. The glibc fcntl call actually adjusts
6893 * them to 5, 6 and 7 before making the syscall(). Since we make the
6894 * syscall directly, adjust to what is supported by the kernel.
6896 if (ret
>= F_GETLK64
&& ret
<= F_SETLKW64
) {
6897 ret
-= F_GETLK64
- 5;
6904 #define FLOCK_TRANSTBL \
6906 TRANSTBL_CONVERT(F_RDLCK); \
6907 TRANSTBL_CONVERT(F_WRLCK); \
6908 TRANSTBL_CONVERT(F_UNLCK); \
6911 static int target_to_host_flock(int type
)
6913 #define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6915 #undef TRANSTBL_CONVERT
6916 return -TARGET_EINVAL
;
6919 static int host_to_target_flock(int type
)
6921 #define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6923 #undef TRANSTBL_CONVERT
6924 /* if we don't know how to convert the value coming
6925 * from the host we copy to the target field as-is
6930 static inline abi_long
copy_from_user_flock(struct flock64
*fl
,
6931 abi_ulong target_flock_addr
)
6933 struct target_flock
*target_fl
;
6936 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6937 return -TARGET_EFAULT
;
6940 __get_user(l_type
, &target_fl
->l_type
);
6941 l_type
= target_to_host_flock(l_type
);
6945 fl
->l_type
= l_type
;
6946 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
6947 __get_user(fl
->l_start
, &target_fl
->l_start
);
6948 __get_user(fl
->l_len
, &target_fl
->l_len
);
6949 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
6950 unlock_user_struct(target_fl
, target_flock_addr
, 0);
6954 static inline abi_long
copy_to_user_flock(abi_ulong target_flock_addr
,
6955 const struct flock64
*fl
)
6957 struct target_flock
*target_fl
;
6960 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
6961 return -TARGET_EFAULT
;
6964 l_type
= host_to_target_flock(fl
->l_type
);
6965 __put_user(l_type
, &target_fl
->l_type
);
6966 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
6967 __put_user(fl
->l_start
, &target_fl
->l_start
);
6968 __put_user(fl
->l_len
, &target_fl
->l_len
);
6969 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
6970 unlock_user_struct(target_fl
, target_flock_addr
, 1);
6974 typedef abi_long
from_flock64_fn(struct flock64
*fl
, abi_ulong target_addr
);
6975 typedef abi_long
to_flock64_fn(abi_ulong target_addr
, const struct flock64
*fl
);
6977 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6978 struct target_oabi_flock64
{
6986 static inline abi_long
copy_from_user_oabi_flock64(struct flock64
*fl
,
6987 abi_ulong target_flock_addr
)
6989 struct target_oabi_flock64
*target_fl
;
6992 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
6993 return -TARGET_EFAULT
;
6996 __get_user(l_type
, &target_fl
->l_type
);
6997 l_type
= target_to_host_flock(l_type
);
7001 fl
->l_type
= l_type
;
7002 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
7003 __get_user(fl
->l_start
, &target_fl
->l_start
);
7004 __get_user(fl
->l_len
, &target_fl
->l_len
);
7005 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
7006 unlock_user_struct(target_fl
, target_flock_addr
, 0);
7010 static inline abi_long
copy_to_user_oabi_flock64(abi_ulong target_flock_addr
,
7011 const struct flock64
*fl
)
7013 struct target_oabi_flock64
*target_fl
;
7016 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
7017 return -TARGET_EFAULT
;
7020 l_type
= host_to_target_flock(fl
->l_type
);
7021 __put_user(l_type
, &target_fl
->l_type
);
7022 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
7023 __put_user(fl
->l_start
, &target_fl
->l_start
);
7024 __put_user(fl
->l_len
, &target_fl
->l_len
);
7025 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
7026 unlock_user_struct(target_fl
, target_flock_addr
, 1);
7031 static inline abi_long
copy_from_user_flock64(struct flock64
*fl
,
7032 abi_ulong target_flock_addr
)
7034 struct target_flock64
*target_fl
;
7037 if (!lock_user_struct(VERIFY_READ
, target_fl
, target_flock_addr
, 1)) {
7038 return -TARGET_EFAULT
;
7041 __get_user(l_type
, &target_fl
->l_type
);
7042 l_type
= target_to_host_flock(l_type
);
7046 fl
->l_type
= l_type
;
7047 __get_user(fl
->l_whence
, &target_fl
->l_whence
);
7048 __get_user(fl
->l_start
, &target_fl
->l_start
);
7049 __get_user(fl
->l_len
, &target_fl
->l_len
);
7050 __get_user(fl
->l_pid
, &target_fl
->l_pid
);
7051 unlock_user_struct(target_fl
, target_flock_addr
, 0);
7055 static inline abi_long
copy_to_user_flock64(abi_ulong target_flock_addr
,
7056 const struct flock64
*fl
)
7058 struct target_flock64
*target_fl
;
7061 if (!lock_user_struct(VERIFY_WRITE
, target_fl
, target_flock_addr
, 0)) {
7062 return -TARGET_EFAULT
;
7065 l_type
= host_to_target_flock(fl
->l_type
);
7066 __put_user(l_type
, &target_fl
->l_type
);
7067 __put_user(fl
->l_whence
, &target_fl
->l_whence
);
7068 __put_user(fl
->l_start
, &target_fl
->l_start
);
7069 __put_user(fl
->l_len
, &target_fl
->l_len
);
7070 __put_user(fl
->l_pid
, &target_fl
->l_pid
);
7071 unlock_user_struct(target_fl
, target_flock_addr
, 1);
7075 static abi_long
do_fcntl(int fd
, int cmd
, abi_ulong arg
)
7077 struct flock64 fl64
;
7079 struct f_owner_ex fox
;
7080 struct target_f_owner_ex
*target_fox
;
7083 int host_cmd
= target_to_host_fcntl_cmd(cmd
);
7085 if (host_cmd
== -TARGET_EINVAL
)
7089 case TARGET_F_GETLK
:
7090 ret
= copy_from_user_flock(&fl64
, arg
);
7094 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7096 ret
= copy_to_user_flock(arg
, &fl64
);
7100 case TARGET_F_SETLK
:
7101 case TARGET_F_SETLKW
:
7102 ret
= copy_from_user_flock(&fl64
, arg
);
7106 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7109 case TARGET_F_GETLK64
:
7110 case TARGET_F_OFD_GETLK
:
7111 ret
= copy_from_user_flock64(&fl64
, arg
);
7115 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7117 ret
= copy_to_user_flock64(arg
, &fl64
);
7120 case TARGET_F_SETLK64
:
7121 case TARGET_F_SETLKW64
:
7122 case TARGET_F_OFD_SETLK
:
7123 case TARGET_F_OFD_SETLKW
:
7124 ret
= copy_from_user_flock64(&fl64
, arg
);
7128 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fl64
));
7131 case TARGET_F_GETFL
:
7132 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
7134 ret
= host_to_target_bitmask(ret
, fcntl_flags_tbl
);
7138 case TARGET_F_SETFL
:
7139 ret
= get_errno(safe_fcntl(fd
, host_cmd
,
7140 target_to_host_bitmask(arg
,
7145 case TARGET_F_GETOWN_EX
:
7146 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
7148 if (!lock_user_struct(VERIFY_WRITE
, target_fox
, arg
, 0))
7149 return -TARGET_EFAULT
;
7150 target_fox
->type
= tswap32(fox
.type
);
7151 target_fox
->pid
= tswap32(fox
.pid
);
7152 unlock_user_struct(target_fox
, arg
, 1);
7158 case TARGET_F_SETOWN_EX
:
7159 if (!lock_user_struct(VERIFY_READ
, target_fox
, arg
, 1))
7160 return -TARGET_EFAULT
;
7161 fox
.type
= tswap32(target_fox
->type
);
7162 fox
.pid
= tswap32(target_fox
->pid
);
7163 unlock_user_struct(target_fox
, arg
, 0);
7164 ret
= get_errno(safe_fcntl(fd
, host_cmd
, &fox
));
7168 case TARGET_F_SETSIG
:
7169 ret
= get_errno(safe_fcntl(fd
, host_cmd
, target_to_host_signal(arg
)));
7172 case TARGET_F_GETSIG
:
7173 ret
= host_to_target_signal(get_errno(safe_fcntl(fd
, host_cmd
, arg
)));
7176 case TARGET_F_SETOWN
:
7177 case TARGET_F_GETOWN
:
7178 case TARGET_F_SETLEASE
:
7179 case TARGET_F_GETLEASE
:
7180 case TARGET_F_SETPIPE_SZ
:
7181 case TARGET_F_GETPIPE_SZ
:
7182 case TARGET_F_ADD_SEALS
:
7183 case TARGET_F_GET_SEALS
:
7184 ret
= get_errno(safe_fcntl(fd
, host_cmd
, arg
));
7188 ret
= get_errno(safe_fcntl(fd
, cmd
, arg
));
7196 static inline int high2lowuid(int uid
)
7204 static inline int high2lowgid(int gid
)
7212 static inline int low2highuid(int uid
)
7214 if ((int16_t)uid
== -1)
7220 static inline int low2highgid(int gid
)
7222 if ((int16_t)gid
== -1)
7227 static inline int tswapid(int id
)
7232 #define put_user_id(x, gaddr) put_user_u16(x, gaddr)
7234 #else /* !USE_UID16 */
7235 static inline int high2lowuid(int uid
)
7239 static inline int high2lowgid(int gid
)
7243 static inline int low2highuid(int uid
)
7247 static inline int low2highgid(int gid
)
7251 static inline int tswapid(int id
)
7256 #define put_user_id(x, gaddr) put_user_u32(x, gaddr)
7258 #endif /* USE_UID16 */
7260 /* We must do direct syscalls for setting UID/GID, because we want to
7261 * implement the Linux system call semantics of "change only for this thread",
7262 * not the libc/POSIX semantics of "change for all threads in process".
7263 * (See http://ewontfix.com/17/ for more details.)
7264 * We use the 32-bit version of the syscalls if present; if it is not
7265 * then either the host architecture supports 32-bit UIDs natively with
7266 * the standard syscall, or the 16-bit UID is the best we can do.
7268 #ifdef __NR_setuid32
7269 #define __NR_sys_setuid __NR_setuid32
7271 #define __NR_sys_setuid __NR_setuid
7273 #ifdef __NR_setgid32
7274 #define __NR_sys_setgid __NR_setgid32
7276 #define __NR_sys_setgid __NR_setgid
7278 #ifdef __NR_setresuid32
7279 #define __NR_sys_setresuid __NR_setresuid32
7281 #define __NR_sys_setresuid __NR_setresuid
7283 #ifdef __NR_setresgid32
7284 #define __NR_sys_setresgid __NR_setresgid32
7286 #define __NR_sys_setresgid __NR_setresgid
7289 _syscall1(int, sys_setuid
, uid_t
, uid
)
7290 _syscall1(int, sys_setgid
, gid_t
, gid
)
7291 _syscall3(int, sys_setresuid
, uid_t
, ruid
, uid_t
, euid
, uid_t
, suid
)
7292 _syscall3(int, sys_setresgid
, gid_t
, rgid
, gid_t
, egid
, gid_t
, sgid
)
7294 void syscall_init(void)
7297 const argtype
*arg_type
;
7300 thunk_init(STRUCT_MAX
);
7302 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
7303 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
7304 #include "syscall_types.h"
7306 #undef STRUCT_SPECIAL
7308 /* we patch the ioctl size if necessary. We rely on the fact that
7309 no ioctl has all the bits at '1' in the size field */
7311 while (ie
->target_cmd
!= 0) {
7312 if (((ie
->target_cmd
>> TARGET_IOC_SIZESHIFT
) & TARGET_IOC_SIZEMASK
) ==
7313 TARGET_IOC_SIZEMASK
) {
7314 arg_type
= ie
->arg_type
;
7315 if (arg_type
[0] != TYPE_PTR
) {
7316 fprintf(stderr
, "cannot patch size for ioctl 0x%x\n",
7321 size
= thunk_type_size(arg_type
, 0);
7322 ie
->target_cmd
= (ie
->target_cmd
&
7323 ~(TARGET_IOC_SIZEMASK
<< TARGET_IOC_SIZESHIFT
)) |
7324 (size
<< TARGET_IOC_SIZESHIFT
);
7327 /* automatic consistency check if same arch */
7328 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7329 (defined(__x86_64__) && defined(TARGET_X86_64))
7330 if (unlikely(ie
->target_cmd
!= ie
->host_cmd
)) {
7331 fprintf(stderr
, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
7332 ie
->name
, ie
->target_cmd
, ie
->host_cmd
);
7339 #ifdef TARGET_NR_truncate64
7340 static inline abi_long
target_truncate64(CPUArchState
*cpu_env
, const char *arg1
,
7345 if (regpairs_aligned(cpu_env
, TARGET_NR_truncate64
)) {
7349 return get_errno(truncate64(arg1
, target_offset64(arg2
, arg3
)));
7353 #ifdef TARGET_NR_ftruncate64
7354 static inline abi_long
target_ftruncate64(CPUArchState
*cpu_env
, abi_long arg1
,
7359 if (regpairs_aligned(cpu_env
, TARGET_NR_ftruncate64
)) {
7363 return get_errno(ftruncate64(arg1
, target_offset64(arg2
, arg3
)));
7367 #if defined(TARGET_NR_timer_settime) || \
7368 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
7369 static inline abi_long
target_to_host_itimerspec(struct itimerspec
*host_its
,
7370 abi_ulong target_addr
)
7372 if (target_to_host_timespec(&host_its
->it_interval
, target_addr
+
7373 offsetof(struct target_itimerspec
,
7375 target_to_host_timespec(&host_its
->it_value
, target_addr
+
7376 offsetof(struct target_itimerspec
,
7378 return -TARGET_EFAULT
;
7385 #if defined(TARGET_NR_timer_settime64) || \
7386 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
7387 static inline abi_long
target_to_host_itimerspec64(struct itimerspec
*host_its
,
7388 abi_ulong target_addr
)
7390 if (target_to_host_timespec64(&host_its
->it_interval
, target_addr
+
7391 offsetof(struct target__kernel_itimerspec
,
7393 target_to_host_timespec64(&host_its
->it_value
, target_addr
+
7394 offsetof(struct target__kernel_itimerspec
,
7396 return -TARGET_EFAULT
;
7403 #if ((defined(TARGET_NR_timerfd_gettime) || \
7404 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
7405 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
7406 static inline abi_long
host_to_target_itimerspec(abi_ulong target_addr
,
7407 struct itimerspec
*host_its
)
7409 if (host_to_target_timespec(target_addr
+ offsetof(struct target_itimerspec
,
7411 &host_its
->it_interval
) ||
7412 host_to_target_timespec(target_addr
+ offsetof(struct target_itimerspec
,
7414 &host_its
->it_value
)) {
7415 return -TARGET_EFAULT
;
7421 #if ((defined(TARGET_NR_timerfd_gettime64) || \
7422 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
7423 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
7424 static inline abi_long
host_to_target_itimerspec64(abi_ulong target_addr
,
7425 struct itimerspec
*host_its
)
7427 if (host_to_target_timespec64(target_addr
+
7428 offsetof(struct target__kernel_itimerspec
,
7430 &host_its
->it_interval
) ||
7431 host_to_target_timespec64(target_addr
+
7432 offsetof(struct target__kernel_itimerspec
,
7434 &host_its
->it_value
)) {
7435 return -TARGET_EFAULT
;
7441 #if defined(TARGET_NR_adjtimex) || \
7442 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
7443 static inline abi_long
target_to_host_timex(struct timex
*host_tx
,
7444 abi_long target_addr
)
7446 struct target_timex
*target_tx
;
7448 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
7449 return -TARGET_EFAULT
;
7452 __get_user(host_tx
->modes
, &target_tx
->modes
);
7453 __get_user(host_tx
->offset
, &target_tx
->offset
);
7454 __get_user(host_tx
->freq
, &target_tx
->freq
);
7455 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7456 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
7457 __get_user(host_tx
->status
, &target_tx
->status
);
7458 __get_user(host_tx
->constant
, &target_tx
->constant
);
7459 __get_user(host_tx
->precision
, &target_tx
->precision
);
7460 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7461 __get_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7462 __get_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7463 __get_user(host_tx
->tick
, &target_tx
->tick
);
7464 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7465 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
7466 __get_user(host_tx
->shift
, &target_tx
->shift
);
7467 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
7468 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7469 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7470 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7471 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7472 __get_user(host_tx
->tai
, &target_tx
->tai
);
7474 unlock_user_struct(target_tx
, target_addr
, 0);
7478 static inline abi_long
host_to_target_timex(abi_long target_addr
,
7479 struct timex
*host_tx
)
7481 struct target_timex
*target_tx
;
7483 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
7484 return -TARGET_EFAULT
;
7487 __put_user(host_tx
->modes
, &target_tx
->modes
);
7488 __put_user(host_tx
->offset
, &target_tx
->offset
);
7489 __put_user(host_tx
->freq
, &target_tx
->freq
);
7490 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7491 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
7492 __put_user(host_tx
->status
, &target_tx
->status
);
7493 __put_user(host_tx
->constant
, &target_tx
->constant
);
7494 __put_user(host_tx
->precision
, &target_tx
->precision
);
7495 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7496 __put_user(host_tx
->time
.tv_sec
, &target_tx
->time
.tv_sec
);
7497 __put_user(host_tx
->time
.tv_usec
, &target_tx
->time
.tv_usec
);
7498 __put_user(host_tx
->tick
, &target_tx
->tick
);
7499 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7500 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
7501 __put_user(host_tx
->shift
, &target_tx
->shift
);
7502 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
7503 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7504 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7505 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7506 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7507 __put_user(host_tx
->tai
, &target_tx
->tai
);
7509 unlock_user_struct(target_tx
, target_addr
, 1);
7515 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
7516 static inline abi_long
target_to_host_timex64(struct timex
*host_tx
,
7517 abi_long target_addr
)
7519 struct target__kernel_timex
*target_tx
;
7521 if (copy_from_user_timeval64(&host_tx
->time
, target_addr
+
7522 offsetof(struct target__kernel_timex
,
7524 return -TARGET_EFAULT
;
7527 if (!lock_user_struct(VERIFY_READ
, target_tx
, target_addr
, 1)) {
7528 return -TARGET_EFAULT
;
7531 __get_user(host_tx
->modes
, &target_tx
->modes
);
7532 __get_user(host_tx
->offset
, &target_tx
->offset
);
7533 __get_user(host_tx
->freq
, &target_tx
->freq
);
7534 __get_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7535 __get_user(host_tx
->esterror
, &target_tx
->esterror
);
7536 __get_user(host_tx
->status
, &target_tx
->status
);
7537 __get_user(host_tx
->constant
, &target_tx
->constant
);
7538 __get_user(host_tx
->precision
, &target_tx
->precision
);
7539 __get_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7540 __get_user(host_tx
->tick
, &target_tx
->tick
);
7541 __get_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7542 __get_user(host_tx
->jitter
, &target_tx
->jitter
);
7543 __get_user(host_tx
->shift
, &target_tx
->shift
);
7544 __get_user(host_tx
->stabil
, &target_tx
->stabil
);
7545 __get_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7546 __get_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7547 __get_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7548 __get_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7549 __get_user(host_tx
->tai
, &target_tx
->tai
);
7551 unlock_user_struct(target_tx
, target_addr
, 0);
7555 static inline abi_long
host_to_target_timex64(abi_long target_addr
,
7556 struct timex
*host_tx
)
7558 struct target__kernel_timex
*target_tx
;
7560 if (copy_to_user_timeval64(target_addr
+
7561 offsetof(struct target__kernel_timex
, time
),
7563 return -TARGET_EFAULT
;
7566 if (!lock_user_struct(VERIFY_WRITE
, target_tx
, target_addr
, 0)) {
7567 return -TARGET_EFAULT
;
7570 __put_user(host_tx
->modes
, &target_tx
->modes
);
7571 __put_user(host_tx
->offset
, &target_tx
->offset
);
7572 __put_user(host_tx
->freq
, &target_tx
->freq
);
7573 __put_user(host_tx
->maxerror
, &target_tx
->maxerror
);
7574 __put_user(host_tx
->esterror
, &target_tx
->esterror
);
7575 __put_user(host_tx
->status
, &target_tx
->status
);
7576 __put_user(host_tx
->constant
, &target_tx
->constant
);
7577 __put_user(host_tx
->precision
, &target_tx
->precision
);
7578 __put_user(host_tx
->tolerance
, &target_tx
->tolerance
);
7579 __put_user(host_tx
->tick
, &target_tx
->tick
);
7580 __put_user(host_tx
->ppsfreq
, &target_tx
->ppsfreq
);
7581 __put_user(host_tx
->jitter
, &target_tx
->jitter
);
7582 __put_user(host_tx
->shift
, &target_tx
->shift
);
7583 __put_user(host_tx
->stabil
, &target_tx
->stabil
);
7584 __put_user(host_tx
->jitcnt
, &target_tx
->jitcnt
);
7585 __put_user(host_tx
->calcnt
, &target_tx
->calcnt
);
7586 __put_user(host_tx
->errcnt
, &target_tx
->errcnt
);
7587 __put_user(host_tx
->stbcnt
, &target_tx
->stbcnt
);
7588 __put_user(host_tx
->tai
, &target_tx
->tai
);
7590 unlock_user_struct(target_tx
, target_addr
, 1);
7595 #ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
7596 #define sigev_notify_thread_id _sigev_un._tid
7599 static inline abi_long
target_to_host_sigevent(struct sigevent
*host_sevp
,
7600 abi_ulong target_addr
)
7602 struct target_sigevent
*target_sevp
;
7604 if (!lock_user_struct(VERIFY_READ
, target_sevp
, target_addr
, 1)) {
7605 return -TARGET_EFAULT
;
7608 /* This union is awkward on 64 bit systems because it has a 32 bit
7609 * integer and a pointer in it; we follow the conversion approach
7610 * used for handling sigval types in signal.c so the guest should get
7611 * the correct value back even if we did a 64 bit byteswap and it's
7612 * using the 32 bit integer.
7614 host_sevp
->sigev_value
.sival_ptr
=
7615 (void *)(uintptr_t)tswapal(target_sevp
->sigev_value
.sival_ptr
);
7616 host_sevp
->sigev_signo
=
7617 target_to_host_signal(tswap32(target_sevp
->sigev_signo
));
7618 host_sevp
->sigev_notify
= tswap32(target_sevp
->sigev_notify
);
7619 host_sevp
->sigev_notify_thread_id
= tswap32(target_sevp
->_sigev_un
._tid
);
7621 unlock_user_struct(target_sevp
, target_addr
, 1);
7625 #if defined(TARGET_NR_mlockall)
7626 static inline int target_to_host_mlockall_arg(int arg
)
7630 if (arg
& TARGET_MCL_CURRENT
) {
7631 result
|= MCL_CURRENT
;
7633 if (arg
& TARGET_MCL_FUTURE
) {
7634 result
|= MCL_FUTURE
;
7637 if (arg
& TARGET_MCL_ONFAULT
) {
7638 result
|= MCL_ONFAULT
;
7646 static inline int target_to_host_msync_arg(abi_long arg
)
7648 return ((arg
& TARGET_MS_ASYNC
) ? MS_ASYNC
: 0) |
7649 ((arg
& TARGET_MS_INVALIDATE
) ? MS_INVALIDATE
: 0) |
7650 ((arg
& TARGET_MS_SYNC
) ? MS_SYNC
: 0) |
7651 (arg
& ~(TARGET_MS_ASYNC
| TARGET_MS_INVALIDATE
| TARGET_MS_SYNC
));
7654 #if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
7655 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
7656 defined(TARGET_NR_newfstatat))
7657 static inline abi_long
host_to_target_stat64(CPUArchState
*cpu_env
,
7658 abi_ulong target_addr
,
7659 struct stat
*host_st
)
7661 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
7662 if (cpu_env
->eabi
) {
7663 struct target_eabi_stat64
*target_st
;
7665 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7666 return -TARGET_EFAULT
;
7667 memset(target_st
, 0, sizeof(struct target_eabi_stat64
));
7668 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7669 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7670 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7671 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7673 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7674 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7675 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7676 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7677 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7678 __put_user(host_st
->st_size
, &target_st
->st_size
);
7679 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7680 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7681 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7682 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7683 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7684 #ifdef HAVE_STRUCT_STAT_ST_ATIM
7685 __put_user(host_st
->st_atim
.tv_nsec
, &target_st
->target_st_atime_nsec
);
7686 __put_user(host_st
->st_mtim
.tv_nsec
, &target_st
->target_st_mtime_nsec
);
7687 __put_user(host_st
->st_ctim
.tv_nsec
, &target_st
->target_st_ctime_nsec
);
7689 unlock_user_struct(target_st
, target_addr
, 1);
7693 #if defined(TARGET_HAS_STRUCT_STAT64)
7694 struct target_stat64
*target_st
;
7696 struct target_stat
*target_st
;
7699 if (!lock_user_struct(VERIFY_WRITE
, target_st
, target_addr
, 0))
7700 return -TARGET_EFAULT
;
7701 memset(target_st
, 0, sizeof(*target_st
));
7702 __put_user(host_st
->st_dev
, &target_st
->st_dev
);
7703 __put_user(host_st
->st_ino
, &target_st
->st_ino
);
7704 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7705 __put_user(host_st
->st_ino
, &target_st
->__st_ino
);
7707 __put_user(host_st
->st_mode
, &target_st
->st_mode
);
7708 __put_user(host_st
->st_nlink
, &target_st
->st_nlink
);
7709 __put_user(host_st
->st_uid
, &target_st
->st_uid
);
7710 __put_user(host_st
->st_gid
, &target_st
->st_gid
);
7711 __put_user(host_st
->st_rdev
, &target_st
->st_rdev
);
7712 /* XXX: better use of kernel struct */
7713 __put_user(host_st
->st_size
, &target_st
->st_size
);
7714 __put_user(host_st
->st_blksize
, &target_st
->st_blksize
);
7715 __put_user(host_st
->st_blocks
, &target_st
->st_blocks
);
7716 __put_user(host_st
->st_atime
, &target_st
->target_st_atime
);
7717 __put_user(host_st
->st_mtime
, &target_st
->target_st_mtime
);
7718 __put_user(host_st
->st_ctime
, &target_st
->target_st_ctime
);
7719 #ifdef HAVE_STRUCT_STAT_ST_ATIM
7720 __put_user(host_st
->st_atim
.tv_nsec
, &target_st
->target_st_atime_nsec
);
7721 __put_user(host_st
->st_mtim
.tv_nsec
, &target_st
->target_st_mtime_nsec
);
7722 __put_user(host_st
->st_ctim
.tv_nsec
, &target_st
->target_st_ctime_nsec
);
7724 unlock_user_struct(target_st
, target_addr
, 1);
7731 #if defined(TARGET_NR_statx) && defined(__NR_statx)
7732 static inline abi_long
host_to_target_statx(struct target_statx
*host_stx
,
7733 abi_ulong target_addr
)
7735 struct target_statx
*target_stx
;
7737 if (!lock_user_struct(VERIFY_WRITE
, target_stx
, target_addr
, 0)) {
7738 return -TARGET_EFAULT
;
7740 memset(target_stx
, 0, sizeof(*target_stx
));
7742 __put_user(host_stx
->stx_mask
, &target_stx
->stx_mask
);
7743 __put_user(host_stx
->stx_blksize
, &target_stx
->stx_blksize
);
7744 __put_user(host_stx
->stx_attributes
, &target_stx
->stx_attributes
);
7745 __put_user(host_stx
->stx_nlink
, &target_stx
->stx_nlink
);
7746 __put_user(host_stx
->stx_uid
, &target_stx
->stx_uid
);
7747 __put_user(host_stx
->stx_gid
, &target_stx
->stx_gid
);
7748 __put_user(host_stx
->stx_mode
, &target_stx
->stx_mode
);
7749 __put_user(host_stx
->stx_ino
, &target_stx
->stx_ino
);
7750 __put_user(host_stx
->stx_size
, &target_stx
->stx_size
);
7751 __put_user(host_stx
->stx_blocks
, &target_stx
->stx_blocks
);
7752 __put_user(host_stx
->stx_attributes_mask
, &target_stx
->stx_attributes_mask
);
7753 __put_user(host_stx
->stx_atime
.tv_sec
, &target_stx
->stx_atime
.tv_sec
);
7754 __put_user(host_stx
->stx_atime
.tv_nsec
, &target_stx
->stx_atime
.tv_nsec
);
7755 __put_user(host_stx
->stx_btime
.tv_sec
, &target_stx
->stx_btime
.tv_sec
);
7756 __put_user(host_stx
->stx_btime
.tv_nsec
, &target_stx
->stx_btime
.tv_nsec
);
7757 __put_user(host_stx
->stx_ctime
.tv_sec
, &target_stx
->stx_ctime
.tv_sec
);
7758 __put_user(host_stx
->stx_ctime
.tv_nsec
, &target_stx
->stx_ctime
.tv_nsec
);
7759 __put_user(host_stx
->stx_mtime
.tv_sec
, &target_stx
->stx_mtime
.tv_sec
);
7760 __put_user(host_stx
->stx_mtime
.tv_nsec
, &target_stx
->stx_mtime
.tv_nsec
);
7761 __put_user(host_stx
->stx_rdev_major
, &target_stx
->stx_rdev_major
);
7762 __put_user(host_stx
->stx_rdev_minor
, &target_stx
->stx_rdev_minor
);
7763 __put_user(host_stx
->stx_dev_major
, &target_stx
->stx_dev_major
);
7764 __put_user(host_stx
->stx_dev_minor
, &target_stx
->stx_dev_minor
);
7766 unlock_user_struct(target_stx
, target_addr
, 1);
7772 static int do_sys_futex(int *uaddr
, int op
, int val
,
7773 const struct timespec
*timeout
, int *uaddr2
,
7776 #if HOST_LONG_BITS == 64
7777 #if defined(__NR_futex)
7778 /* always a 64-bit time_t, it doesn't define _time64 version */
7779 return sys_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7782 #else /* HOST_LONG_BITS == 64 */
7783 #if defined(__NR_futex_time64)
7784 if (sizeof(timeout
->tv_sec
) == 8) {
7785 /* _time64 function on 32bit arch */
7786 return sys_futex_time64(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7789 #if defined(__NR_futex)
7790 /* old function on 32bit arch */
7791 return sys_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
);
7793 #endif /* HOST_LONG_BITS == 64 */
7794 g_assert_not_reached();
7797 static int do_safe_futex(int *uaddr
, int op
, int val
,
7798 const struct timespec
*timeout
, int *uaddr2
,
7801 #if HOST_LONG_BITS == 64
7802 #if defined(__NR_futex)
7803 /* always a 64-bit time_t, it doesn't define _time64 version */
7804 return get_errno(safe_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
));
7806 #else /* HOST_LONG_BITS == 64 */
7807 #if defined(__NR_futex_time64)
7808 if (sizeof(timeout
->tv_sec
) == 8) {
7809 /* _time64 function on 32bit arch */
7810 return get_errno(safe_futex_time64(uaddr
, op
, val
, timeout
, uaddr2
,
7814 #if defined(__NR_futex)
7815 /* old function on 32bit arch */
7816 return get_errno(safe_futex(uaddr
, op
, val
, timeout
, uaddr2
, val3
));
7818 #endif /* HOST_LONG_BITS == 64 */
7819 return -TARGET_ENOSYS
;
7822 /* ??? Using host futex calls even when target atomic operations
7823 are not really atomic probably breaks things. However implementing
7824 futexes locally would make futexes shared between multiple processes
7825 tricky. However they're probably useless because guest atomic
7826 operations won't work either. */
7827 #if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
7828 static int do_futex(CPUState
*cpu
, bool time64
, target_ulong uaddr
,
7829 int op
, int val
, target_ulong timeout
,
7830 target_ulong uaddr2
, int val3
)
7832 struct timespec ts
, *pts
= NULL
;
7833 void *haddr2
= NULL
;
7836 /* We assume FUTEX_* constants are the same on both host and target. */
7837 #ifdef FUTEX_CMD_MASK
7838 base_op
= op
& FUTEX_CMD_MASK
;
7844 case FUTEX_WAIT_BITSET
:
7847 case FUTEX_WAIT_REQUEUE_PI
:
7849 haddr2
= g2h(cpu
, uaddr2
);
7852 case FUTEX_LOCK_PI2
:
7855 case FUTEX_WAKE_BITSET
:
7856 case FUTEX_TRYLOCK_PI
:
7857 case FUTEX_UNLOCK_PI
:
7861 val
= target_to_host_signal(val
);
7864 case FUTEX_CMP_REQUEUE
:
7865 case FUTEX_CMP_REQUEUE_PI
:
7866 val3
= tswap32(val3
);
7871 * For these, the 4th argument is not TIMEOUT, but VAL2.
7872 * But the prototype of do_safe_futex takes a pointer, so
7873 * insert casts to satisfy the compiler. We do not need
7874 * to tswap VAL2 since it's not compared to guest memory.
7876 pts
= (struct timespec
*)(uintptr_t)timeout
;
7878 haddr2
= g2h(cpu
, uaddr2
);
7881 return -TARGET_ENOSYS
;
7886 ? target_to_host_timespec64(pts
, timeout
)
7887 : target_to_host_timespec(pts
, timeout
)) {
7888 return -TARGET_EFAULT
;
7891 return do_safe_futex(g2h(cpu
, uaddr
), op
, val
, pts
, haddr2
, val3
);
7895 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7896 static abi_long
do_name_to_handle_at(abi_long dirfd
, abi_long pathname
,
7897 abi_long handle
, abi_long mount_id
,
7900 struct file_handle
*target_fh
;
7901 struct file_handle
*fh
;
7905 unsigned int size
, total_size
;
7907 if (get_user_s32(size
, handle
)) {
7908 return -TARGET_EFAULT
;
7911 name
= lock_user_string(pathname
);
7913 return -TARGET_EFAULT
;
7916 total_size
= sizeof(struct file_handle
) + size
;
7917 target_fh
= lock_user(VERIFY_WRITE
, handle
, total_size
, 0);
7919 unlock_user(name
, pathname
, 0);
7920 return -TARGET_EFAULT
;
7923 fh
= g_malloc0(total_size
);
7924 fh
->handle_bytes
= size
;
7926 ret
= get_errno(name_to_handle_at(dirfd
, path(name
), fh
, &mid
, flags
));
7927 unlock_user(name
, pathname
, 0);
7929 /* man name_to_handle_at(2):
7930 * Other than the use of the handle_bytes field, the caller should treat
7931 * the file_handle structure as an opaque data type
7934 memcpy(target_fh
, fh
, total_size
);
7935 target_fh
->handle_bytes
= tswap32(fh
->handle_bytes
);
7936 target_fh
->handle_type
= tswap32(fh
->handle_type
);
7938 unlock_user(target_fh
, handle
, total_size
);
7940 if (put_user_s32(mid
, mount_id
)) {
7941 return -TARGET_EFAULT
;
7949 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7950 static abi_long
do_open_by_handle_at(abi_long mount_fd
, abi_long handle
,
7953 struct file_handle
*target_fh
;
7954 struct file_handle
*fh
;
7955 unsigned int size
, total_size
;
7958 if (get_user_s32(size
, handle
)) {
7959 return -TARGET_EFAULT
;
7962 total_size
= sizeof(struct file_handle
) + size
;
7963 target_fh
= lock_user(VERIFY_READ
, handle
, total_size
, 1);
7965 return -TARGET_EFAULT
;
7968 fh
= g_memdup(target_fh
, total_size
);
7969 fh
->handle_bytes
= size
;
7970 fh
->handle_type
= tswap32(target_fh
->handle_type
);
7972 ret
= get_errno(open_by_handle_at(mount_fd
, fh
,
7973 target_to_host_bitmask(flags
, fcntl_flags_tbl
)));
7977 unlock_user(target_fh
, handle
, total_size
);
7983 #if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7985 static abi_long
do_signalfd4(int fd
, abi_long mask
, int flags
)
7988 target_sigset_t
*target_mask
;
7992 if (flags
& ~(TARGET_O_NONBLOCK_MASK
| TARGET_O_CLOEXEC
)) {
7993 return -TARGET_EINVAL
;
7995 if (!lock_user_struct(VERIFY_READ
, target_mask
, mask
, 1)) {
7996 return -TARGET_EFAULT
;
7999 target_to_host_sigset(&host_mask
, target_mask
);
8001 host_flags
= target_to_host_bitmask(flags
, fcntl_flags_tbl
);
8003 ret
= get_errno(signalfd(fd
, &host_mask
, host_flags
));
8005 fd_trans_register(ret
, &target_signalfd_trans
);
8008 unlock_user_struct(target_mask
, mask
, 0);
8014 /* Map host to target signal numbers for the wait family of syscalls.
8015 Assume all other status bits are the same. */
8016 int host_to_target_waitstatus(int status
)
8018 if (WIFSIGNALED(status
)) {
8019 return host_to_target_signal(WTERMSIG(status
)) | (status
& ~0x7f);
8021 if (WIFSTOPPED(status
)) {
8022 return (host_to_target_signal(WSTOPSIG(status
)) << 8)
8028 static int open_self_cmdline(CPUArchState
*cpu_env
, int fd
)
8030 CPUState
*cpu
= env_cpu(cpu_env
);
8031 struct linux_binprm
*bprm
= ((TaskState
*)cpu
->opaque
)->bprm
;
8034 for (i
= 0; i
< bprm
->argc
; i
++) {
8035 size_t len
= strlen(bprm
->argv
[i
]) + 1;
8037 if (write(fd
, bprm
->argv
[i
], len
) != len
) {
8045 static int open_self_maps(CPUArchState
*cpu_env
, int fd
)
8047 CPUState
*cpu
= env_cpu(cpu_env
);
8048 TaskState
*ts
= cpu
->opaque
;
8049 GSList
*map_info
= read_self_maps();
8053 for (s
= map_info
; s
; s
= g_slist_next(s
)) {
8054 MapInfo
*e
= (MapInfo
*) s
->data
;
8056 if (h2g_valid(e
->start
)) {
8057 unsigned long min
= e
->start
;
8058 unsigned long max
= e
->end
;
8059 int flags
= page_get_flags(h2g(min
));
8062 max
= h2g_valid(max
- 1) ?
8063 max
: (uintptr_t) g2h_untagged(GUEST_ADDR_MAX
) + 1;
8065 if (page_check_range(h2g(min
), max
- min
, flags
) == -1) {
8070 if (h2g(max
) == ts
->info
->stack_limit
) {
8072 if (h2g(min
) == ts
->info
->stack_limit
) {
8079 count
= dprintf(fd
, TARGET_ABI_FMT_ptr
"-" TARGET_ABI_FMT_ptr
8080 " %c%c%c%c %08" PRIx64
" %s %"PRId64
,
8081 h2g(min
), h2g(max
- 1) + 1,
8082 (flags
& PAGE_READ
) ? 'r' : '-',
8083 (flags
& PAGE_WRITE_ORG
) ? 'w' : '-',
8084 (flags
& PAGE_EXEC
) ? 'x' : '-',
8085 e
->is_priv
? 'p' : 's',
8086 (uint64_t) e
->offset
, e
->dev
, e
->inode
);
8088 dprintf(fd
, "%*s%s\n", 73 - count
, "", path
);
8095 free_self_maps(map_info
);
8097 #ifdef TARGET_VSYSCALL_PAGE
8099 * We only support execution from the vsyscall page.
8100 * This is as if CONFIG_LEGACY_VSYSCALL_XONLY=y from v5.3.
8102 count
= dprintf(fd
, TARGET_FMT_lx
"-" TARGET_FMT_lx
8103 " --xp 00000000 00:00 0",
8104 TARGET_VSYSCALL_PAGE
, TARGET_VSYSCALL_PAGE
+ TARGET_PAGE_SIZE
);
8105 dprintf(fd
, "%*s%s\n", 73 - count
, "", "[vsyscall]");
8111 static int open_self_stat(CPUArchState
*cpu_env
, int fd
)
8113 CPUState
*cpu
= env_cpu(cpu_env
);
8114 TaskState
*ts
= cpu
->opaque
;
8115 g_autoptr(GString
) buf
= g_string_new(NULL
);
8118 for (i
= 0; i
< 44; i
++) {
8121 g_string_printf(buf
, FMT_pid
" ", getpid());
8122 } else if (i
== 1) {
8124 gchar
*bin
= g_strrstr(ts
->bprm
->argv
[0], "/");
8125 bin
= bin
? bin
+ 1 : ts
->bprm
->argv
[0];
8126 g_string_printf(buf
, "(%.15s) ", bin
);
8127 } else if (i
== 2) {
8129 g_string_assign(buf
, "R "); /* we are running right now */
8130 } else if (i
== 3) {
8132 g_string_printf(buf
, FMT_pid
" ", getppid());
8133 } else if (i
== 21) {
8135 g_string_printf(buf
, "%" PRIu64
" ", ts
->start_boottime
);
8136 } else if (i
== 27) {
8138 g_string_printf(buf
, TARGET_ABI_FMT_ld
" ", ts
->info
->start_stack
);
8140 /* for the rest, there is MasterCard */
8141 g_string_printf(buf
, "0%c", i
== 43 ? '\n' : ' ');
8144 if (write(fd
, buf
->str
, buf
->len
) != buf
->len
) {
8152 static int open_self_auxv(CPUArchState
*cpu_env
, int fd
)
8154 CPUState
*cpu
= env_cpu(cpu_env
);
8155 TaskState
*ts
= cpu
->opaque
;
8156 abi_ulong auxv
= ts
->info
->saved_auxv
;
8157 abi_ulong len
= ts
->info
->auxv_len
;
8161 * Auxiliary vector is stored in target process stack.
8162 * read in whole auxv vector and copy it to file
8164 ptr
= lock_user(VERIFY_READ
, auxv
, len
, 0);
8168 r
= write(fd
, ptr
, len
);
8175 lseek(fd
, 0, SEEK_SET
);
8176 unlock_user(ptr
, auxv
, len
);
8182 static int is_proc_myself(const char *filename
, const char *entry
)
8184 if (!strncmp(filename
, "/proc/", strlen("/proc/"))) {
8185 filename
+= strlen("/proc/");
8186 if (!strncmp(filename
, "self/", strlen("self/"))) {
8187 filename
+= strlen("self/");
8188 } else if (*filename
>= '1' && *filename
<= '9') {
8190 snprintf(myself
, sizeof(myself
), "%d/", getpid());
8191 if (!strncmp(filename
, myself
, strlen(myself
))) {
8192 filename
+= strlen(myself
);
8199 if (!strcmp(filename
, entry
)) {
8206 static void excp_dump_file(FILE *logfile
, CPUArchState
*env
,
8207 const char *fmt
, int code
)
8210 CPUState
*cs
= env_cpu(env
);
8212 fprintf(logfile
, fmt
, code
);
8213 fprintf(logfile
, "Failing executable: %s\n", exec_path
);
8214 cpu_dump_state(cs
, logfile
, 0);
8215 open_self_maps(env
, fileno(logfile
));
8219 void target_exception_dump(CPUArchState
*env
, const char *fmt
, int code
)
8221 /* dump to console */
8222 excp_dump_file(stderr
, env
, fmt
, code
);
8224 /* dump to log file */
8225 if (qemu_log_separate()) {
8226 FILE *logfile
= qemu_log_trylock();
8228 excp_dump_file(logfile
, env
, fmt
, code
);
8229 qemu_log_unlock(logfile
);
8233 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
8234 defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA) || \
8235 defined(TARGET_RISCV) || defined(TARGET_S390X)
8236 static int is_proc(const char *filename
, const char *entry
)
8238 return strcmp(filename
, entry
) == 0;
8242 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
8243 static int open_net_route(CPUArchState
*cpu_env
, int fd
)
8250 fp
= fopen("/proc/net/route", "r");
8257 read
= getline(&line
, &len
, fp
);
8258 dprintf(fd
, "%s", line
);
8262 while ((read
= getline(&line
, &len
, fp
)) != -1) {
8264 uint32_t dest
, gw
, mask
;
8265 unsigned int flags
, refcnt
, use
, metric
, mtu
, window
, irtt
;
8268 fields
= sscanf(line
,
8269 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8270 iface
, &dest
, &gw
, &flags
, &refcnt
, &use
, &metric
,
8271 &mask
, &mtu
, &window
, &irtt
);
8275 dprintf(fd
, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8276 iface
, tswap32(dest
), tswap32(gw
), flags
, refcnt
, use
,
8277 metric
, tswap32(mask
), mtu
, window
, irtt
);
8287 #if defined(TARGET_SPARC)
8288 static int open_cpuinfo(CPUArchState
*cpu_env
, int fd
)
8290 dprintf(fd
, "type\t\t: sun4u\n");
8295 #if defined(TARGET_HPPA)
8296 static int open_cpuinfo(CPUArchState
*cpu_env
, int fd
)
8300 num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
8301 for (i
= 0; i
< num_cpus
; i
++) {
8302 dprintf(fd
, "processor\t: %d\n", i
);
8303 dprintf(fd
, "cpu family\t: PA-RISC 1.1e\n");
8304 dprintf(fd
, "cpu\t\t: PA7300LC (PCX-L2)\n");
8305 dprintf(fd
, "capabilities\t: os32\n");
8306 dprintf(fd
, "model\t\t: 9000/778/B160L - "
8307 "Merlin L2 160 QEMU (9000/778/B160L)\n\n");
8313 #if defined(TARGET_RISCV)
8314 static int open_cpuinfo(CPUArchState
*cpu_env
, int fd
)
8317 int num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
8318 RISCVCPU
*cpu
= env_archcpu(cpu_env
);
8319 const RISCVCPUConfig
*cfg
= riscv_cpu_cfg((CPURISCVState
*) cpu_env
);
8320 char *isa_string
= riscv_isa_string(cpu
);
8324 mmu
= (cpu_env
->xl
== MXL_RV32
) ? "sv32" : "sv48";
8329 for (i
= 0; i
< num_cpus
; i
++) {
8330 dprintf(fd
, "processor\t: %d\n", i
);
8331 dprintf(fd
, "hart\t\t: %d\n", i
);
8332 dprintf(fd
, "isa\t\t: %s\n", isa_string
);
8333 dprintf(fd
, "mmu\t\t: %s\n", mmu
);
8334 dprintf(fd
, "uarch\t\t: qemu\n\n");
8342 #if defined(TARGET_S390X)
8344 * Emulate what a Linux kernel running in qemu-system-s390x -M accel=tcg would
8345 * show in /proc/cpuinfo.
8347 * Skip the following in order to match the missing support in op_ecag():
8348 * - show_cacheinfo().
8349 * - show_cpu_topology().
8352 * Use fixed values for certain fields:
8353 * - bogomips per cpu - from a qemu-system-s390x run.
8354 * - max thread id = 0, since SMT / SIGP_SET_MULTI_THREADING is not supported.
8356 * Keep the code structure close to arch/s390/kernel/processor.c.
8359 static void show_facilities(int fd
)
8361 size_t sizeof_stfl_bytes
= 2048;
8362 g_autofree
uint8_t *stfl_bytes
= g_new0(uint8_t, sizeof_stfl_bytes
);
8365 dprintf(fd
, "facilities :");
8366 s390_get_feat_block(S390_FEAT_TYPE_STFL
, stfl_bytes
);
8367 for (bit
= 0; bit
< sizeof_stfl_bytes
* 8; bit
++) {
8368 if (test_be_bit(bit
, stfl_bytes
)) {
8369 dprintf(fd
, " %d", bit
);
8375 static int cpu_ident(unsigned long n
)
8377 return deposit32(0, CPU_ID_BITS
- CPU_PHYS_ADDR_BITS
, CPU_PHYS_ADDR_BITS
,
8381 static void show_cpu_summary(CPUArchState
*cpu_env
, int fd
)
8383 S390CPUModel
*model
= env_archcpu(cpu_env
)->model
;
8384 int num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
8385 uint32_t elf_hwcap
= get_elf_hwcap();
8386 const char *hwcap_str
;
8389 dprintf(fd
, "vendor_id : IBM/S390\n"
8390 "# processors : %i\n"
8391 "bogomips per cpu: 13370.00\n",
8393 dprintf(fd
, "max thread id : 0\n");
8394 dprintf(fd
, "features\t: ");
8395 for (i
= 0; i
< sizeof(elf_hwcap
) * 8; i
++) {
8396 if (!(elf_hwcap
& (1 << i
))) {
8399 hwcap_str
= elf_hwcap_str(i
);
8401 dprintf(fd
, "%s ", hwcap_str
);
8405 show_facilities(fd
);
8406 for (i
= 0; i
< num_cpus
; i
++) {
8407 dprintf(fd
, "processor %d: "
8409 "identification = %06X, "
8411 i
, model
->cpu_ver
, cpu_ident(i
), model
->def
->type
);
8415 static void show_cpu_ids(CPUArchState
*cpu_env
, int fd
, unsigned long n
)
8417 S390CPUModel
*model
= env_archcpu(cpu_env
)->model
;
8419 dprintf(fd
, "version : %02X\n", model
->cpu_ver
);
8420 dprintf(fd
, "identification : %06X\n", cpu_ident(n
));
8421 dprintf(fd
, "machine : %04X\n", model
->def
->type
);
8424 static void show_cpuinfo(CPUArchState
*cpu_env
, int fd
, unsigned long n
)
8426 dprintf(fd
, "\ncpu number : %ld\n", n
);
8427 show_cpu_ids(cpu_env
, fd
, n
);
8430 static int open_cpuinfo(CPUArchState
*cpu_env
, int fd
)
8432 int num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
8435 show_cpu_summary(cpu_env
, fd
);
8436 for (i
= 0; i
< num_cpus
; i
++) {
8437 show_cpuinfo(cpu_env
, fd
, i
);
8443 #if defined(TARGET_M68K)
8444 static int open_hardware(CPUArchState
*cpu_env
, int fd
)
8446 dprintf(fd
, "Model:\t\tqemu-m68k\n");
8451 static int do_openat(CPUArchState
*cpu_env
, int dirfd
, const char *pathname
, int flags
, mode_t mode
)
8454 const char *filename
;
8455 int (*fill
)(CPUArchState
*cpu_env
, int fd
);
8456 int (*cmp
)(const char *s1
, const char *s2
);
8458 const struct fake_open
*fake_open
;
8459 static const struct fake_open fakes
[] = {
8460 { "maps", open_self_maps
, is_proc_myself
},
8461 { "stat", open_self_stat
, is_proc_myself
},
8462 { "auxv", open_self_auxv
, is_proc_myself
},
8463 { "cmdline", open_self_cmdline
, is_proc_myself
},
8464 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
8465 { "/proc/net/route", open_net_route
, is_proc
},
8467 #if defined(TARGET_SPARC) || defined(TARGET_HPPA) || \
8468 defined(TARGET_RISCV) || defined(TARGET_S390X)
8469 { "/proc/cpuinfo", open_cpuinfo
, is_proc
},
8471 #if defined(TARGET_M68K)
8472 { "/proc/hardware", open_hardware
, is_proc
},
8474 { NULL
, NULL
, NULL
}
8477 if (is_proc_myself(pathname
, "exe")) {
8478 return safe_openat(dirfd
, exec_path
, flags
, mode
);
8481 for (fake_open
= fakes
; fake_open
->filename
; fake_open
++) {
8482 if (fake_open
->cmp(pathname
, fake_open
->filename
)) {
8487 if (fake_open
->filename
) {
8489 char filename
[PATH_MAX
];
8492 fd
= memfd_create("qemu-open", 0);
8494 if (errno
!= ENOSYS
) {
8497 /* create temporary file to map stat to */
8498 tmpdir
= getenv("TMPDIR");
8501 snprintf(filename
, sizeof(filename
), "%s/qemu-open.XXXXXX", tmpdir
);
8502 fd
= mkstemp(filename
);
8509 if ((r
= fake_open
->fill(cpu_env
, fd
))) {
8515 lseek(fd
, 0, SEEK_SET
);
8520 return safe_openat(dirfd
, path(pathname
), flags
, mode
);
8523 static int do_execveat(CPUArchState
*cpu_env
, int dirfd
,
8524 abi_long pathname
, abi_long guest_argp
,
8525 abi_long guest_envp
, int flags
)
8528 char **argp
, **envp
;
8537 for (gp
= guest_argp
; gp
; gp
+= sizeof(abi_ulong
)) {
8538 if (get_user_ual(addr
, gp
)) {
8539 return -TARGET_EFAULT
;
8547 for (gp
= guest_envp
; gp
; gp
+= sizeof(abi_ulong
)) {
8548 if (get_user_ual(addr
, gp
)) {
8549 return -TARGET_EFAULT
;
8557 argp
= g_new0(char *, argc
+ 1);
8558 envp
= g_new0(char *, envc
+ 1);
8560 for (gp
= guest_argp
, q
= argp
; gp
; gp
+= sizeof(abi_ulong
), q
++) {
8561 if (get_user_ual(addr
, gp
)) {
8567 *q
= lock_user_string(addr
);
8574 for (gp
= guest_envp
, q
= envp
; gp
; gp
+= sizeof(abi_ulong
), q
++) {
8575 if (get_user_ual(addr
, gp
)) {
8581 *q
= lock_user_string(addr
);
8589 * Although execve() is not an interruptible syscall it is
8590 * a special case where we must use the safe_syscall wrapper:
8591 * if we allow a signal to happen before we make the host
8592 * syscall then we will 'lose' it, because at the point of
8593 * execve the process leaves QEMU's control. So we use the
8594 * safe syscall wrapper to ensure that we either take the
8595 * signal as a guest signal, or else it does not happen
8596 * before the execve completes and makes it the other
8597 * program's problem.
8599 p
= lock_user_string(pathname
);
8604 if (is_proc_myself(p
, "exe")) {
8605 ret
= get_errno(safe_execveat(dirfd
, exec_path
, argp
, envp
, flags
));
8607 ret
= get_errno(safe_execveat(dirfd
, p
, argp
, envp
, flags
));
8610 unlock_user(p
, pathname
, 0);
8615 ret
= -TARGET_EFAULT
;
8618 for (gp
= guest_argp
, q
= argp
; *q
; gp
+= sizeof(abi_ulong
), q
++) {
8619 if (get_user_ual(addr
, gp
) || !addr
) {
8622 unlock_user(*q
, addr
, 0);
8624 for (gp
= guest_envp
, q
= envp
; *q
; gp
+= sizeof(abi_ulong
), q
++) {
8625 if (get_user_ual(addr
, gp
) || !addr
) {
8628 unlock_user(*q
, addr
, 0);
8636 #define TIMER_MAGIC 0x0caf0000
8637 #define TIMER_MAGIC_MASK 0xffff0000
8639 /* Convert QEMU provided timer ID back to internal 16bit index format */
8640 static target_timer_t
get_timer_id(abi_long arg
)
8642 target_timer_t timerid
= arg
;
8644 if ((timerid
& TIMER_MAGIC_MASK
) != TIMER_MAGIC
) {
8645 return -TARGET_EINVAL
;
8650 if (timerid
>= ARRAY_SIZE(g_posix_timers
)) {
8651 return -TARGET_EINVAL
;
8657 static int target_to_host_cpu_mask(unsigned long *host_mask
,
8659 abi_ulong target_addr
,
8662 unsigned target_bits
= sizeof(abi_ulong
) * 8;
8663 unsigned host_bits
= sizeof(*host_mask
) * 8;
8664 abi_ulong
*target_mask
;
8667 assert(host_size
>= target_size
);
8669 target_mask
= lock_user(VERIFY_READ
, target_addr
, target_size
, 1);
8671 return -TARGET_EFAULT
;
8673 memset(host_mask
, 0, host_size
);
8675 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
8676 unsigned bit
= i
* target_bits
;
8679 __get_user(val
, &target_mask
[i
]);
8680 for (j
= 0; j
< target_bits
; j
++, bit
++) {
8681 if (val
& (1UL << j
)) {
8682 host_mask
[bit
/ host_bits
] |= 1UL << (bit
% host_bits
);
8687 unlock_user(target_mask
, target_addr
, 0);
8691 static int host_to_target_cpu_mask(const unsigned long *host_mask
,
8693 abi_ulong target_addr
,
8696 unsigned target_bits
= sizeof(abi_ulong
) * 8;
8697 unsigned host_bits
= sizeof(*host_mask
) * 8;
8698 abi_ulong
*target_mask
;
8701 assert(host_size
>= target_size
);
8703 target_mask
= lock_user(VERIFY_WRITE
, target_addr
, target_size
, 0);
8705 return -TARGET_EFAULT
;
8708 for (i
= 0 ; i
< target_size
/ sizeof(abi_ulong
); i
++) {
8709 unsigned bit
= i
* target_bits
;
8712 for (j
= 0; j
< target_bits
; j
++, bit
++) {
8713 if (host_mask
[bit
/ host_bits
] & (1UL << (bit
% host_bits
))) {
8717 __put_user(val
, &target_mask
[i
]);
8720 unlock_user(target_mask
, target_addr
, target_size
);
8724 #ifdef TARGET_NR_getdents
8725 static int do_getdents(abi_long dirfd
, abi_long arg2
, abi_long count
)
8727 g_autofree
void *hdirp
= NULL
;
8729 int hlen
, hoff
, toff
;
8730 int hreclen
, treclen
;
8731 off64_t prev_diroff
= 0;
8733 hdirp
= g_try_malloc(count
);
8735 return -TARGET_ENOMEM
;
8738 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8739 hlen
= sys_getdents(dirfd
, hdirp
, count
);
8741 hlen
= sys_getdents64(dirfd
, hdirp
, count
);
8744 hlen
= get_errno(hlen
);
8745 if (is_error(hlen
)) {
8749 tdirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
8751 return -TARGET_EFAULT
;
8754 for (hoff
= toff
= 0; hoff
< hlen
; hoff
+= hreclen
, toff
+= treclen
) {
8755 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8756 struct linux_dirent
*hde
= hdirp
+ hoff
;
8758 struct linux_dirent64
*hde
= hdirp
+ hoff
;
8760 struct target_dirent
*tde
= tdirp
+ toff
;
8764 namelen
= strlen(hde
->d_name
);
8765 hreclen
= hde
->d_reclen
;
8766 treclen
= offsetof(struct target_dirent
, d_name
) + namelen
+ 2;
8767 treclen
= QEMU_ALIGN_UP(treclen
, __alignof(struct target_dirent
));
8769 if (toff
+ treclen
> count
) {
8771 * If the host struct is smaller than the target struct, or
8772 * requires less alignment and thus packs into less space,
8773 * then the host can return more entries than we can pass
8777 toff
= -TARGET_EINVAL
; /* result buffer is too small */
8781 * Return what we have, resetting the file pointer to the
8782 * location of the first record not returned.
8784 lseek64(dirfd
, prev_diroff
, SEEK_SET
);
8788 prev_diroff
= hde
->d_off
;
8789 tde
->d_ino
= tswapal(hde
->d_ino
);
8790 tde
->d_off
= tswapal(hde
->d_off
);
8791 tde
->d_reclen
= tswap16(treclen
);
8792 memcpy(tde
->d_name
, hde
->d_name
, namelen
+ 1);
8795 * The getdents type is in what was formerly a padding byte at the
8796 * end of the structure.
8798 #ifdef EMULATE_GETDENTS_WITH_GETDENTS
8799 type
= *((uint8_t *)hde
+ hreclen
- 1);
8803 *((uint8_t *)tde
+ treclen
- 1) = type
;
8806 unlock_user(tdirp
, arg2
, toff
);
8809 #endif /* TARGET_NR_getdents */
8811 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
8812 static int do_getdents64(abi_long dirfd
, abi_long arg2
, abi_long count
)
8814 g_autofree
void *hdirp
= NULL
;
8816 int hlen
, hoff
, toff
;
8817 int hreclen
, treclen
;
8818 off64_t prev_diroff
= 0;
8820 hdirp
= g_try_malloc(count
);
8822 return -TARGET_ENOMEM
;
8825 hlen
= get_errno(sys_getdents64(dirfd
, hdirp
, count
));
8826 if (is_error(hlen
)) {
8830 tdirp
= lock_user(VERIFY_WRITE
, arg2
, count
, 0);
8832 return -TARGET_EFAULT
;
8835 for (hoff
= toff
= 0; hoff
< hlen
; hoff
+= hreclen
, toff
+= treclen
) {
8836 struct linux_dirent64
*hde
= hdirp
+ hoff
;
8837 struct target_dirent64
*tde
= tdirp
+ toff
;
8840 namelen
= strlen(hde
->d_name
) + 1;
8841 hreclen
= hde
->d_reclen
;
8842 treclen
= offsetof(struct target_dirent64
, d_name
) + namelen
;
8843 treclen
= QEMU_ALIGN_UP(treclen
, __alignof(struct target_dirent64
));
8845 if (toff
+ treclen
> count
) {
8847 * If the host struct is smaller than the target struct, or
8848 * requires less alignment and thus packs into less space,
8849 * then the host can return more entries than we can pass
8853 toff
= -TARGET_EINVAL
; /* result buffer is too small */
8857 * Return what we have, resetting the file pointer to the
8858 * location of the first record not returned.
8860 lseek64(dirfd
, prev_diroff
, SEEK_SET
);
8864 prev_diroff
= hde
->d_off
;
8865 tde
->d_ino
= tswap64(hde
->d_ino
);
8866 tde
->d_off
= tswap64(hde
->d_off
);
8867 tde
->d_reclen
= tswap16(treclen
);
8868 tde
->d_type
= hde
->d_type
;
8869 memcpy(tde
->d_name
, hde
->d_name
, namelen
);
8872 unlock_user(tdirp
, arg2
, toff
);
8875 #endif /* TARGET_NR_getdents64 */
8877 #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
8878 _syscall2(int, pivot_root
, const char *, new_root
, const char *, put_old
)
8881 #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
8882 #define __NR_sys_open_tree __NR_open_tree
8883 _syscall3(int, sys_open_tree
, int, __dfd
, const char *, __filename
,
8884 unsigned int, __flags
)
8887 #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
8888 #define __NR_sys_move_mount __NR_move_mount
8889 _syscall5(int, sys_move_mount
, int, __from_dfd
, const char *, __from_pathname
,
8890 int, __to_dfd
, const char *, __to_pathname
, unsigned int, flag
)
8893 /* This is an internal helper for do_syscall so that it is easier
8894 * to have a single return point, so that actions, such as logging
8895 * of syscall results, can be performed.
8896 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
8898 static abi_long
do_syscall1(CPUArchState
*cpu_env
, int num
, abi_long arg1
,
8899 abi_long arg2
, abi_long arg3
, abi_long arg4
,
8900 abi_long arg5
, abi_long arg6
, abi_long arg7
,
8903 CPUState
*cpu
= env_cpu(cpu_env
);
8905 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
8906 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
8907 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
8908 || defined(TARGET_NR_statx)
8911 #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
8912 || defined(TARGET_NR_fstatfs)
8918 case TARGET_NR_exit
:
8919 /* In old applications this may be used to implement _exit(2).
8920 However in threaded applications it is used for thread termination,
8921 and _exit_group is used for application termination.
8922 Do thread termination if we have more then one thread. */
8924 if (block_signals()) {
8925 return -QEMU_ERESTARTSYS
;
8928 pthread_mutex_lock(&clone_lock
);
8930 if (CPU_NEXT(first_cpu
)) {
8931 TaskState
*ts
= cpu
->opaque
;
8933 if (ts
->child_tidptr
) {
8934 put_user_u32(0, ts
->child_tidptr
);
8935 do_sys_futex(g2h(cpu
, ts
->child_tidptr
),
8936 FUTEX_WAKE
, INT_MAX
, NULL
, NULL
, 0);
8939 object_unparent(OBJECT(cpu
));
8940 object_unref(OBJECT(cpu
));
8942 * At this point the CPU should be unrealized and removed
8943 * from cpu lists. We can clean-up the rest of the thread
8944 * data without the lock held.
8947 pthread_mutex_unlock(&clone_lock
);
8951 rcu_unregister_thread();
8955 pthread_mutex_unlock(&clone_lock
);
8956 preexit_cleanup(cpu_env
, arg1
);
8958 return 0; /* avoid warning */
8959 case TARGET_NR_read
:
8960 if (arg2
== 0 && arg3
== 0) {
8961 return get_errno(safe_read(arg1
, 0, 0));
8963 if (!(p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0)))
8964 return -TARGET_EFAULT
;
8965 ret
= get_errno(safe_read(arg1
, p
, arg3
));
8967 fd_trans_host_to_target_data(arg1
)) {
8968 ret
= fd_trans_host_to_target_data(arg1
)(p
, ret
);
8970 unlock_user(p
, arg2
, ret
);
8973 case TARGET_NR_write
:
8974 if (arg2
== 0 && arg3
== 0) {
8975 return get_errno(safe_write(arg1
, 0, 0));
8977 if (!(p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1)))
8978 return -TARGET_EFAULT
;
8979 if (fd_trans_target_to_host_data(arg1
)) {
8980 void *copy
= g_malloc(arg3
);
8981 memcpy(copy
, p
, arg3
);
8982 ret
= fd_trans_target_to_host_data(arg1
)(copy
, arg3
);
8984 ret
= get_errno(safe_write(arg1
, copy
, ret
));
8988 ret
= get_errno(safe_write(arg1
, p
, arg3
));
8990 unlock_user(p
, arg2
, 0);
8993 #ifdef TARGET_NR_open
8994 case TARGET_NR_open
:
8995 if (!(p
= lock_user_string(arg1
)))
8996 return -TARGET_EFAULT
;
8997 ret
= get_errno(do_openat(cpu_env
, AT_FDCWD
, p
,
8998 target_to_host_bitmask(arg2
, fcntl_flags_tbl
),
9000 fd_trans_unregister(ret
);
9001 unlock_user(p
, arg1
, 0);
9004 case TARGET_NR_openat
:
9005 if (!(p
= lock_user_string(arg2
)))
9006 return -TARGET_EFAULT
;
9007 ret
= get_errno(do_openat(cpu_env
, arg1
, p
,
9008 target_to_host_bitmask(arg3
, fcntl_flags_tbl
),
9010 fd_trans_unregister(ret
);
9011 unlock_user(p
, arg2
, 0);
9013 #if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9014 case TARGET_NR_name_to_handle_at
:
9015 ret
= do_name_to_handle_at(arg1
, arg2
, arg3
, arg4
, arg5
);
9018 #if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9019 case TARGET_NR_open_by_handle_at
:
9020 ret
= do_open_by_handle_at(arg1
, arg2
, arg3
);
9021 fd_trans_unregister(ret
);
9024 #if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
9025 case TARGET_NR_pidfd_open
:
9026 return get_errno(pidfd_open(arg1
, arg2
));
9028 #if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
9029 case TARGET_NR_pidfd_send_signal
:
9031 siginfo_t uinfo
, *puinfo
;
9034 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
9036 return -TARGET_EFAULT
;
9038 target_to_host_siginfo(&uinfo
, p
);
9039 unlock_user(p
, arg3
, 0);
9044 ret
= get_errno(pidfd_send_signal(arg1
, target_to_host_signal(arg2
),
9049 #if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
9050 case TARGET_NR_pidfd_getfd
:
9051 return get_errno(pidfd_getfd(arg1
, arg2
, arg3
));
9053 case TARGET_NR_close
:
9054 fd_trans_unregister(arg1
);
9055 return get_errno(close(arg1
));
9056 #if defined(__NR_close_range) && defined(TARGET_NR_close_range)
9057 case TARGET_NR_close_range
:
9058 ret
= get_errno(sys_close_range(arg1
, arg2
, arg3
));
9059 if (ret
== 0 && !(arg3
& CLOSE_RANGE_CLOEXEC
)) {
9061 maxfd
= MIN(arg2
, target_fd_max
);
9062 for (fd
= arg1
; fd
< maxfd
; fd
++) {
9063 fd_trans_unregister(fd
);
9070 return do_brk(arg1
);
9071 #ifdef TARGET_NR_fork
9072 case TARGET_NR_fork
:
9073 return get_errno(do_fork(cpu_env
, TARGET_SIGCHLD
, 0, 0, 0, 0));
9075 #ifdef TARGET_NR_waitpid
9076 case TARGET_NR_waitpid
:
9079 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, 0));
9080 if (!is_error(ret
) && arg2
&& ret
9081 && put_user_s32(host_to_target_waitstatus(status
), arg2
))
9082 return -TARGET_EFAULT
;
9086 #ifdef TARGET_NR_waitid
9087 case TARGET_NR_waitid
:
9091 ret
= get_errno(safe_waitid(arg1
, arg2
, &info
, arg4
, NULL
));
9092 if (!is_error(ret
) && arg3
&& info
.si_pid
!= 0) {
9093 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_siginfo_t
), 0)))
9094 return -TARGET_EFAULT
;
9095 host_to_target_siginfo(p
, &info
);
9096 unlock_user(p
, arg3
, sizeof(target_siginfo_t
));
9101 #ifdef TARGET_NR_creat /* not on alpha */
9102 case TARGET_NR_creat
:
9103 if (!(p
= lock_user_string(arg1
)))
9104 return -TARGET_EFAULT
;
9105 ret
= get_errno(creat(p
, arg2
));
9106 fd_trans_unregister(ret
);
9107 unlock_user(p
, arg1
, 0);
9110 #ifdef TARGET_NR_link
9111 case TARGET_NR_link
:
9114 p
= lock_user_string(arg1
);
9115 p2
= lock_user_string(arg2
);
9117 ret
= -TARGET_EFAULT
;
9119 ret
= get_errno(link(p
, p2
));
9120 unlock_user(p2
, arg2
, 0);
9121 unlock_user(p
, arg1
, 0);
9125 #if defined(TARGET_NR_linkat)
9126 case TARGET_NR_linkat
:
9130 return -TARGET_EFAULT
;
9131 p
= lock_user_string(arg2
);
9132 p2
= lock_user_string(arg4
);
9134 ret
= -TARGET_EFAULT
;
9136 ret
= get_errno(linkat(arg1
, p
, arg3
, p2
, arg5
));
9137 unlock_user(p
, arg2
, 0);
9138 unlock_user(p2
, arg4
, 0);
9142 #ifdef TARGET_NR_unlink
9143 case TARGET_NR_unlink
:
9144 if (!(p
= lock_user_string(arg1
)))
9145 return -TARGET_EFAULT
;
9146 ret
= get_errno(unlink(p
));
9147 unlock_user(p
, arg1
, 0);
9150 #if defined(TARGET_NR_unlinkat)
9151 case TARGET_NR_unlinkat
:
9152 if (!(p
= lock_user_string(arg2
)))
9153 return -TARGET_EFAULT
;
9154 ret
= get_errno(unlinkat(arg1
, p
, arg3
));
9155 unlock_user(p
, arg2
, 0);
9158 case TARGET_NR_execveat
:
9159 return do_execveat(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
);
9160 case TARGET_NR_execve
:
9161 return do_execveat(cpu_env
, AT_FDCWD
, arg1
, arg2
, arg3
, 0);
9162 case TARGET_NR_chdir
:
9163 if (!(p
= lock_user_string(arg1
)))
9164 return -TARGET_EFAULT
;
9165 ret
= get_errno(chdir(p
));
9166 unlock_user(p
, arg1
, 0);
9168 #ifdef TARGET_NR_time
9169 case TARGET_NR_time
:
9172 ret
= get_errno(time(&host_time
));
9175 && put_user_sal(host_time
, arg1
))
9176 return -TARGET_EFAULT
;
9180 #ifdef TARGET_NR_mknod
9181 case TARGET_NR_mknod
:
9182 if (!(p
= lock_user_string(arg1
)))
9183 return -TARGET_EFAULT
;
9184 ret
= get_errno(mknod(p
, arg2
, arg3
));
9185 unlock_user(p
, arg1
, 0);
9188 #if defined(TARGET_NR_mknodat)
9189 case TARGET_NR_mknodat
:
9190 if (!(p
= lock_user_string(arg2
)))
9191 return -TARGET_EFAULT
;
9192 ret
= get_errno(mknodat(arg1
, p
, arg3
, arg4
));
9193 unlock_user(p
, arg2
, 0);
9196 #ifdef TARGET_NR_chmod
9197 case TARGET_NR_chmod
:
9198 if (!(p
= lock_user_string(arg1
)))
9199 return -TARGET_EFAULT
;
9200 ret
= get_errno(chmod(p
, arg2
));
9201 unlock_user(p
, arg1
, 0);
9204 #ifdef TARGET_NR_lseek
9205 case TARGET_NR_lseek
:
9206 return get_errno(lseek(arg1
, arg2
, arg3
));
9208 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
9209 /* Alpha specific */
9210 case TARGET_NR_getxpid
:
9211 cpu_env
->ir
[IR_A4
] = getppid();
9212 return get_errno(getpid());
9214 #ifdef TARGET_NR_getpid
9215 case TARGET_NR_getpid
:
9216 return get_errno(getpid());
9218 case TARGET_NR_mount
:
9220 /* need to look at the data field */
9224 p
= lock_user_string(arg1
);
9226 return -TARGET_EFAULT
;
9232 p2
= lock_user_string(arg2
);
9235 unlock_user(p
, arg1
, 0);
9237 return -TARGET_EFAULT
;
9241 p3
= lock_user_string(arg3
);
9244 unlock_user(p
, arg1
, 0);
9246 unlock_user(p2
, arg2
, 0);
9247 return -TARGET_EFAULT
;
9253 /* FIXME - arg5 should be locked, but it isn't clear how to
9254 * do that since it's not guaranteed to be a NULL-terminated
9258 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, NULL
);
9260 ret
= mount(p
, p2
, p3
, (unsigned long)arg4
, g2h(cpu
, arg5
));
9262 ret
= get_errno(ret
);
9265 unlock_user(p
, arg1
, 0);
9267 unlock_user(p2
, arg2
, 0);
9269 unlock_user(p3
, arg3
, 0);
9273 #if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
9274 #if defined(TARGET_NR_umount)
9275 case TARGET_NR_umount
:
9277 #if defined(TARGET_NR_oldumount)
9278 case TARGET_NR_oldumount
:
9280 if (!(p
= lock_user_string(arg1
)))
9281 return -TARGET_EFAULT
;
9282 ret
= get_errno(umount(p
));
9283 unlock_user(p
, arg1
, 0);
9286 #if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
9287 case TARGET_NR_move_mount
:
9291 if (!arg2
|| !arg4
) {
9292 return -TARGET_EFAULT
;
9295 p2
= lock_user_string(arg2
);
9297 return -TARGET_EFAULT
;
9300 p4
= lock_user_string(arg4
);
9302 unlock_user(p2
, arg2
, 0);
9303 return -TARGET_EFAULT
;
9305 ret
= get_errno(sys_move_mount(arg1
, p2
, arg3
, p4
, arg5
));
9307 unlock_user(p2
, arg2
, 0);
9308 unlock_user(p4
, arg4
, 0);
9313 #if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
9314 case TARGET_NR_open_tree
:
9320 return -TARGET_EFAULT
;
9323 p2
= lock_user_string(arg2
);
9325 return -TARGET_EFAULT
;
9328 host_flags
= arg3
& ~TARGET_O_CLOEXEC
;
9329 if (arg3
& TARGET_O_CLOEXEC
) {
9330 host_flags
|= O_CLOEXEC
;
9333 ret
= get_errno(sys_open_tree(arg1
, p2
, host_flags
));
9335 unlock_user(p2
, arg2
, 0);
9340 #ifdef TARGET_NR_stime /* not on alpha */
9341 case TARGET_NR_stime
:
9345 if (get_user_sal(ts
.tv_sec
, arg1
)) {
9346 return -TARGET_EFAULT
;
9348 return get_errno(clock_settime(CLOCK_REALTIME
, &ts
));
9351 #ifdef TARGET_NR_alarm /* not on alpha */
9352 case TARGET_NR_alarm
:
9355 #ifdef TARGET_NR_pause /* not on alpha */
9356 case TARGET_NR_pause
:
9357 if (!block_signals()) {
9358 sigsuspend(&((TaskState
*)cpu
->opaque
)->signal_mask
);
9360 return -TARGET_EINTR
;
9362 #ifdef TARGET_NR_utime
9363 case TARGET_NR_utime
:
9365 struct utimbuf tbuf
, *host_tbuf
;
9366 struct target_utimbuf
*target_tbuf
;
9368 if (!lock_user_struct(VERIFY_READ
, target_tbuf
, arg2
, 1))
9369 return -TARGET_EFAULT
;
9370 tbuf
.actime
= tswapal(target_tbuf
->actime
);
9371 tbuf
.modtime
= tswapal(target_tbuf
->modtime
);
9372 unlock_user_struct(target_tbuf
, arg2
, 0);
9377 if (!(p
= lock_user_string(arg1
)))
9378 return -TARGET_EFAULT
;
9379 ret
= get_errno(utime(p
, host_tbuf
));
9380 unlock_user(p
, arg1
, 0);
9384 #ifdef TARGET_NR_utimes
9385 case TARGET_NR_utimes
:
9387 struct timeval
*tvp
, tv
[2];
9389 if (copy_from_user_timeval(&tv
[0], arg2
)
9390 || copy_from_user_timeval(&tv
[1],
9391 arg2
+ sizeof(struct target_timeval
)))
9392 return -TARGET_EFAULT
;
9397 if (!(p
= lock_user_string(arg1
)))
9398 return -TARGET_EFAULT
;
9399 ret
= get_errno(utimes(p
, tvp
));
9400 unlock_user(p
, arg1
, 0);
9404 #if defined(TARGET_NR_futimesat)
9405 case TARGET_NR_futimesat
:
9407 struct timeval
*tvp
, tv
[2];
9409 if (copy_from_user_timeval(&tv
[0], arg3
)
9410 || copy_from_user_timeval(&tv
[1],
9411 arg3
+ sizeof(struct target_timeval
)))
9412 return -TARGET_EFAULT
;
9417 if (!(p
= lock_user_string(arg2
))) {
9418 return -TARGET_EFAULT
;
9420 ret
= get_errno(futimesat(arg1
, path(p
), tvp
));
9421 unlock_user(p
, arg2
, 0);
9425 #ifdef TARGET_NR_access
9426 case TARGET_NR_access
:
9427 if (!(p
= lock_user_string(arg1
))) {
9428 return -TARGET_EFAULT
;
9430 ret
= get_errno(access(path(p
), arg2
));
9431 unlock_user(p
, arg1
, 0);
9434 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
9435 case TARGET_NR_faccessat
:
9436 if (!(p
= lock_user_string(arg2
))) {
9437 return -TARGET_EFAULT
;
9439 ret
= get_errno(faccessat(arg1
, p
, arg3
, 0));
9440 unlock_user(p
, arg2
, 0);
9443 #if defined(TARGET_NR_faccessat2)
9444 case TARGET_NR_faccessat2
:
9445 if (!(p
= lock_user_string(arg2
))) {
9446 return -TARGET_EFAULT
;
9448 ret
= get_errno(faccessat(arg1
, p
, arg3
, arg4
));
9449 unlock_user(p
, arg2
, 0);
9452 #ifdef TARGET_NR_nice /* not on alpha */
9453 case TARGET_NR_nice
:
9454 return get_errno(nice(arg1
));
9456 case TARGET_NR_sync
:
9459 #if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
9460 case TARGET_NR_syncfs
:
9461 return get_errno(syncfs(arg1
));
9463 case TARGET_NR_kill
:
9464 return get_errno(safe_kill(arg1
, target_to_host_signal(arg2
)));
9465 #ifdef TARGET_NR_rename
9466 case TARGET_NR_rename
:
9469 p
= lock_user_string(arg1
);
9470 p2
= lock_user_string(arg2
);
9472 ret
= -TARGET_EFAULT
;
9474 ret
= get_errno(rename(p
, p2
));
9475 unlock_user(p2
, arg2
, 0);
9476 unlock_user(p
, arg1
, 0);
9480 #if defined(TARGET_NR_renameat)
9481 case TARGET_NR_renameat
:
9484 p
= lock_user_string(arg2
);
9485 p2
= lock_user_string(arg4
);
9487 ret
= -TARGET_EFAULT
;
9489 ret
= get_errno(renameat(arg1
, p
, arg3
, p2
));
9490 unlock_user(p2
, arg4
, 0);
9491 unlock_user(p
, arg2
, 0);
9495 #if defined(TARGET_NR_renameat2)
9496 case TARGET_NR_renameat2
:
9499 p
= lock_user_string(arg2
);
9500 p2
= lock_user_string(arg4
);
9502 ret
= -TARGET_EFAULT
;
9504 ret
= get_errno(sys_renameat2(arg1
, p
, arg3
, p2
, arg5
));
9506 unlock_user(p2
, arg4
, 0);
9507 unlock_user(p
, arg2
, 0);
9511 #ifdef TARGET_NR_mkdir
9512 case TARGET_NR_mkdir
:
9513 if (!(p
= lock_user_string(arg1
)))
9514 return -TARGET_EFAULT
;
9515 ret
= get_errno(mkdir(p
, arg2
));
9516 unlock_user(p
, arg1
, 0);
9519 #if defined(TARGET_NR_mkdirat)
9520 case TARGET_NR_mkdirat
:
9521 if (!(p
= lock_user_string(arg2
)))
9522 return -TARGET_EFAULT
;
9523 ret
= get_errno(mkdirat(arg1
, p
, arg3
));
9524 unlock_user(p
, arg2
, 0);
9527 #ifdef TARGET_NR_rmdir
9528 case TARGET_NR_rmdir
:
9529 if (!(p
= lock_user_string(arg1
)))
9530 return -TARGET_EFAULT
;
9531 ret
= get_errno(rmdir(p
));
9532 unlock_user(p
, arg1
, 0);
9536 ret
= get_errno(dup(arg1
));
9538 fd_trans_dup(arg1
, ret
);
9541 #ifdef TARGET_NR_pipe
9542 case TARGET_NR_pipe
:
9543 return do_pipe(cpu_env
, arg1
, 0, 0);
9545 #ifdef TARGET_NR_pipe2
9546 case TARGET_NR_pipe2
:
9547 return do_pipe(cpu_env
, arg1
,
9548 target_to_host_bitmask(arg2
, fcntl_flags_tbl
), 1);
9550 case TARGET_NR_times
:
9552 struct target_tms
*tmsp
;
9554 ret
= get_errno(times(&tms
));
9556 tmsp
= lock_user(VERIFY_WRITE
, arg1
, sizeof(struct target_tms
), 0);
9558 return -TARGET_EFAULT
;
9559 tmsp
->tms_utime
= tswapal(host_to_target_clock_t(tms
.tms_utime
));
9560 tmsp
->tms_stime
= tswapal(host_to_target_clock_t(tms
.tms_stime
));
9561 tmsp
->tms_cutime
= tswapal(host_to_target_clock_t(tms
.tms_cutime
));
9562 tmsp
->tms_cstime
= tswapal(host_to_target_clock_t(tms
.tms_cstime
));
9565 ret
= host_to_target_clock_t(ret
);
9568 case TARGET_NR_acct
:
9570 ret
= get_errno(acct(NULL
));
9572 if (!(p
= lock_user_string(arg1
))) {
9573 return -TARGET_EFAULT
;
9575 ret
= get_errno(acct(path(p
)));
9576 unlock_user(p
, arg1
, 0);
9579 #ifdef TARGET_NR_umount2
9580 case TARGET_NR_umount2
:
9581 if (!(p
= lock_user_string(arg1
)))
9582 return -TARGET_EFAULT
;
9583 ret
= get_errno(umount2(p
, arg2
));
9584 unlock_user(p
, arg1
, 0);
9587 case TARGET_NR_ioctl
:
9588 return do_ioctl(arg1
, arg2
, arg3
);
9589 #ifdef TARGET_NR_fcntl
9590 case TARGET_NR_fcntl
:
9591 return do_fcntl(arg1
, arg2
, arg3
);
9593 case TARGET_NR_setpgid
:
9594 return get_errno(setpgid(arg1
, arg2
));
9595 case TARGET_NR_umask
:
9596 return get_errno(umask(arg1
));
9597 case TARGET_NR_chroot
:
9598 if (!(p
= lock_user_string(arg1
)))
9599 return -TARGET_EFAULT
;
9600 ret
= get_errno(chroot(p
));
9601 unlock_user(p
, arg1
, 0);
9603 #ifdef TARGET_NR_dup2
9604 case TARGET_NR_dup2
:
9605 ret
= get_errno(dup2(arg1
, arg2
));
9607 fd_trans_dup(arg1
, arg2
);
9611 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
9612 case TARGET_NR_dup3
:
9616 if ((arg3
& ~TARGET_O_CLOEXEC
) != 0) {
9619 host_flags
= target_to_host_bitmask(arg3
, fcntl_flags_tbl
);
9620 ret
= get_errno(dup3(arg1
, arg2
, host_flags
));
9622 fd_trans_dup(arg1
, arg2
);
9627 #ifdef TARGET_NR_getppid /* not on alpha */
9628 case TARGET_NR_getppid
:
9629 return get_errno(getppid());
9631 #ifdef TARGET_NR_getpgrp
9632 case TARGET_NR_getpgrp
:
9633 return get_errno(getpgrp());
9635 case TARGET_NR_setsid
:
9636 return get_errno(setsid());
9637 #ifdef TARGET_NR_sigaction
9638 case TARGET_NR_sigaction
:
9640 #if defined(TARGET_MIPS)
9641 struct target_sigaction act
, oact
, *pact
, *old_act
;
9644 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
9645 return -TARGET_EFAULT
;
9646 act
._sa_handler
= old_act
->_sa_handler
;
9647 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
.sig
[0]);
9648 act
.sa_flags
= old_act
->sa_flags
;
9649 unlock_user_struct(old_act
, arg2
, 0);
9655 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
, 0));
9657 if (!is_error(ret
) && arg3
) {
9658 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
9659 return -TARGET_EFAULT
;
9660 old_act
->_sa_handler
= oact
._sa_handler
;
9661 old_act
->sa_flags
= oact
.sa_flags
;
9662 old_act
->sa_mask
.sig
[0] = oact
.sa_mask
.sig
[0];
9663 old_act
->sa_mask
.sig
[1] = 0;
9664 old_act
->sa_mask
.sig
[2] = 0;
9665 old_act
->sa_mask
.sig
[3] = 0;
9666 unlock_user_struct(old_act
, arg3
, 1);
9669 struct target_old_sigaction
*old_act
;
9670 struct target_sigaction act
, oact
, *pact
;
9672 if (!lock_user_struct(VERIFY_READ
, old_act
, arg2
, 1))
9673 return -TARGET_EFAULT
;
9674 act
._sa_handler
= old_act
->_sa_handler
;
9675 target_siginitset(&act
.sa_mask
, old_act
->sa_mask
);
9676 act
.sa_flags
= old_act
->sa_flags
;
9677 #ifdef TARGET_ARCH_HAS_SA_RESTORER
9678 act
.sa_restorer
= old_act
->sa_restorer
;
9680 unlock_user_struct(old_act
, arg2
, 0);
9685 ret
= get_errno(do_sigaction(arg1
, pact
, &oact
, 0));
9686 if (!is_error(ret
) && arg3
) {
9687 if (!lock_user_struct(VERIFY_WRITE
, old_act
, arg3
, 0))
9688 return -TARGET_EFAULT
;
9689 old_act
->_sa_handler
= oact
._sa_handler
;
9690 old_act
->sa_mask
= oact
.sa_mask
.sig
[0];
9691 old_act
->sa_flags
= oact
.sa_flags
;
9692 #ifdef TARGET_ARCH_HAS_SA_RESTORER
9693 old_act
->sa_restorer
= oact
.sa_restorer
;
9695 unlock_user_struct(old_act
, arg3
, 1);
9701 case TARGET_NR_rt_sigaction
:
9704 * For Alpha and SPARC this is a 5 argument syscall, with
9705 * a 'restorer' parameter which must be copied into the
9706 * sa_restorer field of the sigaction struct.
9707 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
9708 * and arg5 is the sigsetsize.
9710 #if defined(TARGET_ALPHA)
9711 target_ulong sigsetsize
= arg4
;
9712 target_ulong restorer
= arg5
;
9713 #elif defined(TARGET_SPARC)
9714 target_ulong restorer
= arg4
;
9715 target_ulong sigsetsize
= arg5
;
9717 target_ulong sigsetsize
= arg4
;
9718 target_ulong restorer
= 0;
9720 struct target_sigaction
*act
= NULL
;
9721 struct target_sigaction
*oact
= NULL
;
9723 if (sigsetsize
!= sizeof(target_sigset_t
)) {
9724 return -TARGET_EINVAL
;
9726 if (arg2
&& !lock_user_struct(VERIFY_READ
, act
, arg2
, 1)) {
9727 return -TARGET_EFAULT
;
9729 if (arg3
&& !lock_user_struct(VERIFY_WRITE
, oact
, arg3
, 0)) {
9730 ret
= -TARGET_EFAULT
;
9732 ret
= get_errno(do_sigaction(arg1
, act
, oact
, restorer
));
9734 unlock_user_struct(oact
, arg3
, 1);
9738 unlock_user_struct(act
, arg2
, 0);
9742 #ifdef TARGET_NR_sgetmask /* not on alpha */
9743 case TARGET_NR_sgetmask
:
9746 abi_ulong target_set
;
9747 ret
= do_sigprocmask(0, NULL
, &cur_set
);
9749 host_to_target_old_sigset(&target_set
, &cur_set
);
9755 #ifdef TARGET_NR_ssetmask /* not on alpha */
9756 case TARGET_NR_ssetmask
:
9759 abi_ulong target_set
= arg1
;
9760 target_to_host_old_sigset(&set
, &target_set
);
9761 ret
= do_sigprocmask(SIG_SETMASK
, &set
, &oset
);
9763 host_to_target_old_sigset(&target_set
, &oset
);
9769 #ifdef TARGET_NR_sigprocmask
9770 case TARGET_NR_sigprocmask
:
9772 #if defined(TARGET_ALPHA)
9773 sigset_t set
, oldset
;
9778 case TARGET_SIG_BLOCK
:
9781 case TARGET_SIG_UNBLOCK
:
9784 case TARGET_SIG_SETMASK
:
9788 return -TARGET_EINVAL
;
9791 target_to_host_old_sigset(&set
, &mask
);
9793 ret
= do_sigprocmask(how
, &set
, &oldset
);
9794 if (!is_error(ret
)) {
9795 host_to_target_old_sigset(&mask
, &oldset
);
9797 cpu_env
->ir
[IR_V0
] = 0; /* force no error */
9800 sigset_t set
, oldset
, *set_ptr
;
9804 p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1);
9806 return -TARGET_EFAULT
;
9808 target_to_host_old_sigset(&set
, p
);
9809 unlock_user(p
, arg2
, 0);
9812 case TARGET_SIG_BLOCK
:
9815 case TARGET_SIG_UNBLOCK
:
9818 case TARGET_SIG_SETMASK
:
9822 return -TARGET_EINVAL
;
9828 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
9829 if (!is_error(ret
) && arg3
) {
9830 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
9831 return -TARGET_EFAULT
;
9832 host_to_target_old_sigset(p
, &oldset
);
9833 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
9839 case TARGET_NR_rt_sigprocmask
:
9842 sigset_t set
, oldset
, *set_ptr
;
9844 if (arg4
!= sizeof(target_sigset_t
)) {
9845 return -TARGET_EINVAL
;
9849 p
= lock_user(VERIFY_READ
, arg2
, sizeof(target_sigset_t
), 1);
9851 return -TARGET_EFAULT
;
9853 target_to_host_sigset(&set
, p
);
9854 unlock_user(p
, arg2
, 0);
9857 case TARGET_SIG_BLOCK
:
9860 case TARGET_SIG_UNBLOCK
:
9863 case TARGET_SIG_SETMASK
:
9867 return -TARGET_EINVAL
;
9873 ret
= do_sigprocmask(how
, set_ptr
, &oldset
);
9874 if (!is_error(ret
) && arg3
) {
9875 if (!(p
= lock_user(VERIFY_WRITE
, arg3
, sizeof(target_sigset_t
), 0)))
9876 return -TARGET_EFAULT
;
9877 host_to_target_sigset(p
, &oldset
);
9878 unlock_user(p
, arg3
, sizeof(target_sigset_t
));
9882 #ifdef TARGET_NR_sigpending
9883 case TARGET_NR_sigpending
:
9886 ret
= get_errno(sigpending(&set
));
9887 if (!is_error(ret
)) {
9888 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
9889 return -TARGET_EFAULT
;
9890 host_to_target_old_sigset(p
, &set
);
9891 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
9896 case TARGET_NR_rt_sigpending
:
9900 /* Yes, this check is >, not != like most. We follow the kernel's
9901 * logic and it does it like this because it implements
9902 * NR_sigpending through the same code path, and in that case
9903 * the old_sigset_t is smaller in size.
9905 if (arg2
> sizeof(target_sigset_t
)) {
9906 return -TARGET_EINVAL
;
9909 ret
= get_errno(sigpending(&set
));
9910 if (!is_error(ret
)) {
9911 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, sizeof(target_sigset_t
), 0)))
9912 return -TARGET_EFAULT
;
9913 host_to_target_sigset(p
, &set
);
9914 unlock_user(p
, arg1
, sizeof(target_sigset_t
));
9918 #ifdef TARGET_NR_sigsuspend
9919 case TARGET_NR_sigsuspend
:
9923 #if defined(TARGET_ALPHA)
9924 TaskState
*ts
= cpu
->opaque
;
9925 /* target_to_host_old_sigset will bswap back */
9926 abi_ulong mask
= tswapal(arg1
);
9927 set
= &ts
->sigsuspend_mask
;
9928 target_to_host_old_sigset(set
, &mask
);
9930 ret
= process_sigsuspend_mask(&set
, arg1
, sizeof(target_sigset_t
));
9935 ret
= get_errno(safe_rt_sigsuspend(set
, SIGSET_T_SIZE
));
9936 finish_sigsuspend_mask(ret
);
9940 case TARGET_NR_rt_sigsuspend
:
9944 ret
= process_sigsuspend_mask(&set
, arg1
, arg2
);
9948 ret
= get_errno(safe_rt_sigsuspend(set
, SIGSET_T_SIZE
));
9949 finish_sigsuspend_mask(ret
);
9952 #ifdef TARGET_NR_rt_sigtimedwait
9953 case TARGET_NR_rt_sigtimedwait
:
9956 struct timespec uts
, *puts
;
9959 if (arg4
!= sizeof(target_sigset_t
)) {
9960 return -TARGET_EINVAL
;
9963 if (!(p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1)))
9964 return -TARGET_EFAULT
;
9965 target_to_host_sigset(&set
, p
);
9966 unlock_user(p
, arg1
, 0);
9969 if (target_to_host_timespec(puts
, arg3
)) {
9970 return -TARGET_EFAULT
;
9975 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
9977 if (!is_error(ret
)) {
9979 p
= lock_user(VERIFY_WRITE
, arg2
, sizeof(target_siginfo_t
),
9982 return -TARGET_EFAULT
;
9984 host_to_target_siginfo(p
, &uinfo
);
9985 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
9987 ret
= host_to_target_signal(ret
);
9992 #ifdef TARGET_NR_rt_sigtimedwait_time64
9993 case TARGET_NR_rt_sigtimedwait_time64
:
9996 struct timespec uts
, *puts
;
9999 if (arg4
!= sizeof(target_sigset_t
)) {
10000 return -TARGET_EINVAL
;
10003 p
= lock_user(VERIFY_READ
, arg1
, sizeof(target_sigset_t
), 1);
10005 return -TARGET_EFAULT
;
10007 target_to_host_sigset(&set
, p
);
10008 unlock_user(p
, arg1
, 0);
10011 if (target_to_host_timespec64(puts
, arg3
)) {
10012 return -TARGET_EFAULT
;
10017 ret
= get_errno(safe_rt_sigtimedwait(&set
, &uinfo
, puts
,
10019 if (!is_error(ret
)) {
10021 p
= lock_user(VERIFY_WRITE
, arg2
,
10022 sizeof(target_siginfo_t
), 0);
10024 return -TARGET_EFAULT
;
10026 host_to_target_siginfo(p
, &uinfo
);
10027 unlock_user(p
, arg2
, sizeof(target_siginfo_t
));
10029 ret
= host_to_target_signal(ret
);
10034 case TARGET_NR_rt_sigqueueinfo
:
10038 p
= lock_user(VERIFY_READ
, arg3
, sizeof(target_siginfo_t
), 1);
10040 return -TARGET_EFAULT
;
10042 target_to_host_siginfo(&uinfo
, p
);
10043 unlock_user(p
, arg3
, 0);
10044 ret
= get_errno(sys_rt_sigqueueinfo(arg1
, target_to_host_signal(arg2
), &uinfo
));
10047 case TARGET_NR_rt_tgsigqueueinfo
:
10051 p
= lock_user(VERIFY_READ
, arg4
, sizeof(target_siginfo_t
), 1);
10053 return -TARGET_EFAULT
;
10055 target_to_host_siginfo(&uinfo
, p
);
10056 unlock_user(p
, arg4
, 0);
10057 ret
= get_errno(sys_rt_tgsigqueueinfo(arg1
, arg2
, target_to_host_signal(arg3
), &uinfo
));
10060 #ifdef TARGET_NR_sigreturn
10061 case TARGET_NR_sigreturn
:
10062 if (block_signals()) {
10063 return -QEMU_ERESTARTSYS
;
10065 return do_sigreturn(cpu_env
);
10067 case TARGET_NR_rt_sigreturn
:
10068 if (block_signals()) {
10069 return -QEMU_ERESTARTSYS
;
10071 return do_rt_sigreturn(cpu_env
);
10072 case TARGET_NR_sethostname
:
10073 if (!(p
= lock_user_string(arg1
)))
10074 return -TARGET_EFAULT
;
10075 ret
= get_errno(sethostname(p
, arg2
));
10076 unlock_user(p
, arg1
, 0);
10078 #ifdef TARGET_NR_setrlimit
10079 case TARGET_NR_setrlimit
:
10081 int resource
= target_to_host_resource(arg1
);
10082 struct target_rlimit
*target_rlim
;
10083 struct rlimit rlim
;
10084 if (!lock_user_struct(VERIFY_READ
, target_rlim
, arg2
, 1))
10085 return -TARGET_EFAULT
;
10086 rlim
.rlim_cur
= target_to_host_rlim(target_rlim
->rlim_cur
);
10087 rlim
.rlim_max
= target_to_host_rlim(target_rlim
->rlim_max
);
10088 unlock_user_struct(target_rlim
, arg2
, 0);
10090 * If we just passed through resource limit settings for memory then
10091 * they would also apply to QEMU's own allocations, and QEMU will
10092 * crash or hang or die if its allocations fail. Ideally we would
10093 * track the guest allocations in QEMU and apply the limits ourselves.
10094 * For now, just tell the guest the call succeeded but don't actually
10097 if (resource
!= RLIMIT_AS
&&
10098 resource
!= RLIMIT_DATA
&&
10099 resource
!= RLIMIT_STACK
) {
10100 return get_errno(setrlimit(resource
, &rlim
));
10106 #ifdef TARGET_NR_getrlimit
10107 case TARGET_NR_getrlimit
:
10109 int resource
= target_to_host_resource(arg1
);
10110 struct target_rlimit
*target_rlim
;
10111 struct rlimit rlim
;
10113 ret
= get_errno(getrlimit(resource
, &rlim
));
10114 if (!is_error(ret
)) {
10115 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
10116 return -TARGET_EFAULT
;
10117 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
10118 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
10119 unlock_user_struct(target_rlim
, arg2
, 1);
10124 case TARGET_NR_getrusage
:
10126 struct rusage rusage
;
10127 ret
= get_errno(getrusage(arg1
, &rusage
));
10128 if (!is_error(ret
)) {
10129 ret
= host_to_target_rusage(arg2
, &rusage
);
10133 #if defined(TARGET_NR_gettimeofday)
10134 case TARGET_NR_gettimeofday
:
10137 struct timezone tz
;
10139 ret
= get_errno(gettimeofday(&tv
, &tz
));
10140 if (!is_error(ret
)) {
10141 if (arg1
&& copy_to_user_timeval(arg1
, &tv
)) {
10142 return -TARGET_EFAULT
;
10144 if (arg2
&& copy_to_user_timezone(arg2
, &tz
)) {
10145 return -TARGET_EFAULT
;
10151 #if defined(TARGET_NR_settimeofday)
10152 case TARGET_NR_settimeofday
:
10154 struct timeval tv
, *ptv
= NULL
;
10155 struct timezone tz
, *ptz
= NULL
;
10158 if (copy_from_user_timeval(&tv
, arg1
)) {
10159 return -TARGET_EFAULT
;
10165 if (copy_from_user_timezone(&tz
, arg2
)) {
10166 return -TARGET_EFAULT
;
10171 return get_errno(settimeofday(ptv
, ptz
));
10174 #if defined(TARGET_NR_select)
10175 case TARGET_NR_select
:
10176 #if defined(TARGET_WANT_NI_OLD_SELECT)
10177 /* some architectures used to have old_select here
10178 * but now ENOSYS it.
10180 ret
= -TARGET_ENOSYS
;
10181 #elif defined(TARGET_WANT_OLD_SYS_SELECT)
10182 ret
= do_old_select(arg1
);
10184 ret
= do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
10188 #ifdef TARGET_NR_pselect6
10189 case TARGET_NR_pselect6
:
10190 return do_pselect6(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, false);
10192 #ifdef TARGET_NR_pselect6_time64
10193 case TARGET_NR_pselect6_time64
:
10194 return do_pselect6(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, true);
10196 #ifdef TARGET_NR_symlink
10197 case TARGET_NR_symlink
:
10200 p
= lock_user_string(arg1
);
10201 p2
= lock_user_string(arg2
);
10203 ret
= -TARGET_EFAULT
;
10205 ret
= get_errno(symlink(p
, p2
));
10206 unlock_user(p2
, arg2
, 0);
10207 unlock_user(p
, arg1
, 0);
10211 #if defined(TARGET_NR_symlinkat)
10212 case TARGET_NR_symlinkat
:
10215 p
= lock_user_string(arg1
);
10216 p2
= lock_user_string(arg3
);
10218 ret
= -TARGET_EFAULT
;
10220 ret
= get_errno(symlinkat(p
, arg2
, p2
));
10221 unlock_user(p2
, arg3
, 0);
10222 unlock_user(p
, arg1
, 0);
10226 #ifdef TARGET_NR_readlink
10227 case TARGET_NR_readlink
:
10230 p
= lock_user_string(arg1
);
10231 p2
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10233 ret
= -TARGET_EFAULT
;
10234 } else if (!arg3
) {
10235 /* Short circuit this for the magic exe check. */
10236 ret
= -TARGET_EINVAL
;
10237 } else if (is_proc_myself((const char *)p
, "exe")) {
10239 * Don't worry about sign mismatch as earlier mapping
10240 * logic would have thrown a bad address error.
10242 ret
= MIN(strlen(exec_path
), arg3
);
10243 /* We cannot NUL terminate the string. */
10244 memcpy(p2
, exec_path
, ret
);
10246 ret
= get_errno(readlink(path(p
), p2
, arg3
));
10248 unlock_user(p2
, arg2
, ret
);
10249 unlock_user(p
, arg1
, 0);
10253 #if defined(TARGET_NR_readlinkat)
10254 case TARGET_NR_readlinkat
:
10257 p
= lock_user_string(arg2
);
10258 p2
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
10260 ret
= -TARGET_EFAULT
;
10261 } else if (!arg4
) {
10262 /* Short circuit this for the magic exe check. */
10263 ret
= -TARGET_EINVAL
;
10264 } else if (is_proc_myself((const char *)p
, "exe")) {
10266 * Don't worry about sign mismatch as earlier mapping
10267 * logic would have thrown a bad address error.
10269 ret
= MIN(strlen(exec_path
), arg4
);
10270 /* We cannot NUL terminate the string. */
10271 memcpy(p2
, exec_path
, ret
);
10273 ret
= get_errno(readlinkat(arg1
, path(p
), p2
, arg4
));
10275 unlock_user(p2
, arg3
, ret
);
10276 unlock_user(p
, arg2
, 0);
10280 #ifdef TARGET_NR_swapon
10281 case TARGET_NR_swapon
:
10282 if (!(p
= lock_user_string(arg1
)))
10283 return -TARGET_EFAULT
;
10284 ret
= get_errno(swapon(p
, arg2
));
10285 unlock_user(p
, arg1
, 0);
10288 case TARGET_NR_reboot
:
10289 if (arg3
== LINUX_REBOOT_CMD_RESTART2
) {
10290 /* arg4 must be ignored in all other cases */
10291 p
= lock_user_string(arg4
);
10293 return -TARGET_EFAULT
;
10295 ret
= get_errno(reboot(arg1
, arg2
, arg3
, p
));
10296 unlock_user(p
, arg4
, 0);
10298 ret
= get_errno(reboot(arg1
, arg2
, arg3
, NULL
));
10301 #ifdef TARGET_NR_mmap
10302 case TARGET_NR_mmap
:
10303 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
10304 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
10305 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
10306 || defined(TARGET_S390X)
10309 abi_ulong v1
, v2
, v3
, v4
, v5
, v6
;
10310 if (!(v
= lock_user(VERIFY_READ
, arg1
, 6 * sizeof(abi_ulong
), 1)))
10311 return -TARGET_EFAULT
;
10312 v1
= tswapal(v
[0]);
10313 v2
= tswapal(v
[1]);
10314 v3
= tswapal(v
[2]);
10315 v4
= tswapal(v
[3]);
10316 v5
= tswapal(v
[4]);
10317 v6
= tswapal(v
[5]);
10318 unlock_user(v
, arg1
, 0);
10319 ret
= get_errno(target_mmap(v1
, v2
, v3
,
10320 target_to_host_bitmask(v4
, mmap_flags_tbl
),
10324 /* mmap pointers are always untagged */
10325 ret
= get_errno(target_mmap(arg1
, arg2
, arg3
,
10326 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
10332 #ifdef TARGET_NR_mmap2
10333 case TARGET_NR_mmap2
:
10335 #define MMAP_SHIFT 12
10337 ret
= target_mmap(arg1
, arg2
, arg3
,
10338 target_to_host_bitmask(arg4
, mmap_flags_tbl
),
10339 arg5
, arg6
<< MMAP_SHIFT
);
10340 return get_errno(ret
);
10342 case TARGET_NR_munmap
:
10343 arg1
= cpu_untagged_addr(cpu
, arg1
);
10344 return get_errno(target_munmap(arg1
, arg2
));
10345 case TARGET_NR_mprotect
:
10346 arg1
= cpu_untagged_addr(cpu
, arg1
);
10348 TaskState
*ts
= cpu
->opaque
;
10349 /* Special hack to detect libc making the stack executable. */
10350 if ((arg3
& PROT_GROWSDOWN
)
10351 && arg1
>= ts
->info
->stack_limit
10352 && arg1
<= ts
->info
->start_stack
) {
10353 arg3
&= ~PROT_GROWSDOWN
;
10354 arg2
= arg2
+ arg1
- ts
->info
->stack_limit
;
10355 arg1
= ts
->info
->stack_limit
;
10358 return get_errno(target_mprotect(arg1
, arg2
, arg3
));
10359 #ifdef TARGET_NR_mremap
10360 case TARGET_NR_mremap
:
10361 arg1
= cpu_untagged_addr(cpu
, arg1
);
10362 /* mremap new_addr (arg5) is always untagged */
10363 return get_errno(target_mremap(arg1
, arg2
, arg3
, arg4
, arg5
));
10365 /* ??? msync/mlock/munlock are broken for softmmu. */
10366 #ifdef TARGET_NR_msync
10367 case TARGET_NR_msync
:
10368 return get_errno(msync(g2h(cpu
, arg1
), arg2
,
10369 target_to_host_msync_arg(arg3
)));
10371 #ifdef TARGET_NR_mlock
10372 case TARGET_NR_mlock
:
10373 return get_errno(mlock(g2h(cpu
, arg1
), arg2
));
10375 #ifdef TARGET_NR_munlock
10376 case TARGET_NR_munlock
:
10377 return get_errno(munlock(g2h(cpu
, arg1
), arg2
));
10379 #ifdef TARGET_NR_mlockall
10380 case TARGET_NR_mlockall
:
10381 return get_errno(mlockall(target_to_host_mlockall_arg(arg1
)));
10383 #ifdef TARGET_NR_munlockall
10384 case TARGET_NR_munlockall
:
10385 return get_errno(munlockall());
10387 #ifdef TARGET_NR_truncate
10388 case TARGET_NR_truncate
:
10389 if (!(p
= lock_user_string(arg1
)))
10390 return -TARGET_EFAULT
;
10391 ret
= get_errno(truncate(p
, arg2
));
10392 unlock_user(p
, arg1
, 0);
10395 #ifdef TARGET_NR_ftruncate
10396 case TARGET_NR_ftruncate
:
10397 return get_errno(ftruncate(arg1
, arg2
));
10399 case TARGET_NR_fchmod
:
10400 return get_errno(fchmod(arg1
, arg2
));
10401 #if defined(TARGET_NR_fchmodat)
10402 case TARGET_NR_fchmodat
:
10403 if (!(p
= lock_user_string(arg2
)))
10404 return -TARGET_EFAULT
;
10405 ret
= get_errno(fchmodat(arg1
, p
, arg3
, 0));
10406 unlock_user(p
, arg2
, 0);
10409 case TARGET_NR_getpriority
:
10410 /* Note that negative values are valid for getpriority, so we must
10411 differentiate based on errno settings. */
10413 ret
= getpriority(arg1
, arg2
);
10414 if (ret
== -1 && errno
!= 0) {
10415 return -host_to_target_errno(errno
);
10417 #ifdef TARGET_ALPHA
10418 /* Return value is the unbiased priority. Signal no error. */
10419 cpu_env
->ir
[IR_V0
] = 0;
10421 /* Return value is a biased priority to avoid negative numbers. */
10425 case TARGET_NR_setpriority
:
10426 return get_errno(setpriority(arg1
, arg2
, arg3
));
10427 #ifdef TARGET_NR_statfs
10428 case TARGET_NR_statfs
:
10429 if (!(p
= lock_user_string(arg1
))) {
10430 return -TARGET_EFAULT
;
10432 ret
= get_errno(statfs(path(p
), &stfs
));
10433 unlock_user(p
, arg1
, 0);
10435 if (!is_error(ret
)) {
10436 struct target_statfs
*target_stfs
;
10438 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg2
, 0))
10439 return -TARGET_EFAULT
;
10440 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
10441 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
10442 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
10443 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
10444 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
10445 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
10446 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
10447 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
10448 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
10449 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
10450 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
10451 #ifdef _STATFS_F_FLAGS
10452 __put_user(stfs
.f_flags
, &target_stfs
->f_flags
);
10454 __put_user(0, &target_stfs
->f_flags
);
10456 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
10457 unlock_user_struct(target_stfs
, arg2
, 1);
10461 #ifdef TARGET_NR_fstatfs
10462 case TARGET_NR_fstatfs
:
10463 ret
= get_errno(fstatfs(arg1
, &stfs
));
10464 goto convert_statfs
;
10466 #ifdef TARGET_NR_statfs64
10467 case TARGET_NR_statfs64
:
10468 if (!(p
= lock_user_string(arg1
))) {
10469 return -TARGET_EFAULT
;
10471 ret
= get_errno(statfs(path(p
), &stfs
));
10472 unlock_user(p
, arg1
, 0);
10474 if (!is_error(ret
)) {
10475 struct target_statfs64
*target_stfs
;
10477 if (!lock_user_struct(VERIFY_WRITE
, target_stfs
, arg3
, 0))
10478 return -TARGET_EFAULT
;
10479 __put_user(stfs
.f_type
, &target_stfs
->f_type
);
10480 __put_user(stfs
.f_bsize
, &target_stfs
->f_bsize
);
10481 __put_user(stfs
.f_blocks
, &target_stfs
->f_blocks
);
10482 __put_user(stfs
.f_bfree
, &target_stfs
->f_bfree
);
10483 __put_user(stfs
.f_bavail
, &target_stfs
->f_bavail
);
10484 __put_user(stfs
.f_files
, &target_stfs
->f_files
);
10485 __put_user(stfs
.f_ffree
, &target_stfs
->f_ffree
);
10486 __put_user(stfs
.f_fsid
.__val
[0], &target_stfs
->f_fsid
.val
[0]);
10487 __put_user(stfs
.f_fsid
.__val
[1], &target_stfs
->f_fsid
.val
[1]);
10488 __put_user(stfs
.f_namelen
, &target_stfs
->f_namelen
);
10489 __put_user(stfs
.f_frsize
, &target_stfs
->f_frsize
);
10490 #ifdef _STATFS_F_FLAGS
10491 __put_user(stfs
.f_flags
, &target_stfs
->f_flags
);
10493 __put_user(0, &target_stfs
->f_flags
);
10495 memset(target_stfs
->f_spare
, 0, sizeof(target_stfs
->f_spare
));
10496 unlock_user_struct(target_stfs
, arg3
, 1);
10499 case TARGET_NR_fstatfs64
:
10500 ret
= get_errno(fstatfs(arg1
, &stfs
));
10501 goto convert_statfs64
;
10503 #ifdef TARGET_NR_socketcall
10504 case TARGET_NR_socketcall
:
10505 return do_socketcall(arg1
, arg2
);
10507 #ifdef TARGET_NR_accept
10508 case TARGET_NR_accept
:
10509 return do_accept4(arg1
, arg2
, arg3
, 0);
10511 #ifdef TARGET_NR_accept4
10512 case TARGET_NR_accept4
:
10513 return do_accept4(arg1
, arg2
, arg3
, arg4
);
10515 #ifdef TARGET_NR_bind
10516 case TARGET_NR_bind
:
10517 return do_bind(arg1
, arg2
, arg3
);
10519 #ifdef TARGET_NR_connect
10520 case TARGET_NR_connect
:
10521 return do_connect(arg1
, arg2
, arg3
);
10523 #ifdef TARGET_NR_getpeername
10524 case TARGET_NR_getpeername
:
10525 return do_getpeername(arg1
, arg2
, arg3
);
10527 #ifdef TARGET_NR_getsockname
10528 case TARGET_NR_getsockname
:
10529 return do_getsockname(arg1
, arg2
, arg3
);
10531 #ifdef TARGET_NR_getsockopt
10532 case TARGET_NR_getsockopt
:
10533 return do_getsockopt(arg1
, arg2
, arg3
, arg4
, arg5
);
10535 #ifdef TARGET_NR_listen
10536 case TARGET_NR_listen
:
10537 return get_errno(listen(arg1
, arg2
));
10539 #ifdef TARGET_NR_recv
10540 case TARGET_NR_recv
:
10541 return do_recvfrom(arg1
, arg2
, arg3
, arg4
, 0, 0);
10543 #ifdef TARGET_NR_recvfrom
10544 case TARGET_NR_recvfrom
:
10545 return do_recvfrom(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10547 #ifdef TARGET_NR_recvmsg
10548 case TARGET_NR_recvmsg
:
10549 return do_sendrecvmsg(arg1
, arg2
, arg3
, 0);
10551 #ifdef TARGET_NR_send
10552 case TARGET_NR_send
:
10553 return do_sendto(arg1
, arg2
, arg3
, arg4
, 0, 0);
10555 #ifdef TARGET_NR_sendmsg
10556 case TARGET_NR_sendmsg
:
10557 return do_sendrecvmsg(arg1
, arg2
, arg3
, 1);
10559 #ifdef TARGET_NR_sendmmsg
10560 case TARGET_NR_sendmmsg
:
10561 return do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 1);
10563 #ifdef TARGET_NR_recvmmsg
10564 case TARGET_NR_recvmmsg
:
10565 return do_sendrecvmmsg(arg1
, arg2
, arg3
, arg4
, 0);
10567 #ifdef TARGET_NR_sendto
10568 case TARGET_NR_sendto
:
10569 return do_sendto(arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10571 #ifdef TARGET_NR_shutdown
10572 case TARGET_NR_shutdown
:
10573 return get_errno(shutdown(arg1
, arg2
));
10575 #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
10576 case TARGET_NR_getrandom
:
10577 p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
10579 return -TARGET_EFAULT
;
10581 ret
= get_errno(getrandom(p
, arg2
, arg3
));
10582 unlock_user(p
, arg1
, ret
);
10585 #ifdef TARGET_NR_socket
10586 case TARGET_NR_socket
:
10587 return do_socket(arg1
, arg2
, arg3
);
10589 #ifdef TARGET_NR_socketpair
10590 case TARGET_NR_socketpair
:
10591 return do_socketpair(arg1
, arg2
, arg3
, arg4
);
10593 #ifdef TARGET_NR_setsockopt
10594 case TARGET_NR_setsockopt
:
10595 return do_setsockopt(arg1
, arg2
, arg3
, arg4
, (socklen_t
) arg5
);
10597 #if defined(TARGET_NR_syslog)
10598 case TARGET_NR_syslog
:
10603 case TARGET_SYSLOG_ACTION_CLOSE
: /* Close log */
10604 case TARGET_SYSLOG_ACTION_OPEN
: /* Open log */
10605 case TARGET_SYSLOG_ACTION_CLEAR
: /* Clear ring buffer */
10606 case TARGET_SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging */
10607 case TARGET_SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging */
10608 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL
: /* Set messages level */
10609 case TARGET_SYSLOG_ACTION_SIZE_UNREAD
: /* Number of chars */
10610 case TARGET_SYSLOG_ACTION_SIZE_BUFFER
: /* Size of the buffer */
10611 return get_errno(sys_syslog((int)arg1
, NULL
, (int)arg3
));
10612 case TARGET_SYSLOG_ACTION_READ
: /* Read from log */
10613 case TARGET_SYSLOG_ACTION_READ_CLEAR
: /* Read/clear msgs */
10614 case TARGET_SYSLOG_ACTION_READ_ALL
: /* Read last messages */
10617 return -TARGET_EINVAL
;
10622 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
10624 return -TARGET_EFAULT
;
10626 ret
= get_errno(sys_syslog((int)arg1
, p
, (int)arg3
));
10627 unlock_user(p
, arg2
, arg3
);
10631 return -TARGET_EINVAL
;
10636 case TARGET_NR_setitimer
:
10638 struct itimerval value
, ovalue
, *pvalue
;
10642 if (copy_from_user_timeval(&pvalue
->it_interval
, arg2
)
10643 || copy_from_user_timeval(&pvalue
->it_value
,
10644 arg2
+ sizeof(struct target_timeval
)))
10645 return -TARGET_EFAULT
;
10649 ret
= get_errno(setitimer(arg1
, pvalue
, &ovalue
));
10650 if (!is_error(ret
) && arg3
) {
10651 if (copy_to_user_timeval(arg3
,
10652 &ovalue
.it_interval
)
10653 || copy_to_user_timeval(arg3
+ sizeof(struct target_timeval
),
10655 return -TARGET_EFAULT
;
10659 case TARGET_NR_getitimer
:
10661 struct itimerval value
;
10663 ret
= get_errno(getitimer(arg1
, &value
));
10664 if (!is_error(ret
) && arg2
) {
10665 if (copy_to_user_timeval(arg2
,
10666 &value
.it_interval
)
10667 || copy_to_user_timeval(arg2
+ sizeof(struct target_timeval
),
10669 return -TARGET_EFAULT
;
10673 #ifdef TARGET_NR_stat
10674 case TARGET_NR_stat
:
10675 if (!(p
= lock_user_string(arg1
))) {
10676 return -TARGET_EFAULT
;
10678 ret
= get_errno(stat(path(p
), &st
));
10679 unlock_user(p
, arg1
, 0);
10682 #ifdef TARGET_NR_lstat
10683 case TARGET_NR_lstat
:
10684 if (!(p
= lock_user_string(arg1
))) {
10685 return -TARGET_EFAULT
;
10687 ret
= get_errno(lstat(path(p
), &st
));
10688 unlock_user(p
, arg1
, 0);
10691 #ifdef TARGET_NR_fstat
10692 case TARGET_NR_fstat
:
10694 ret
= get_errno(fstat(arg1
, &st
));
10695 #if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
10698 if (!is_error(ret
)) {
10699 struct target_stat
*target_st
;
10701 if (!lock_user_struct(VERIFY_WRITE
, target_st
, arg2
, 0))
10702 return -TARGET_EFAULT
;
10703 memset(target_st
, 0, sizeof(*target_st
));
10704 __put_user(st
.st_dev
, &target_st
->st_dev
);
10705 __put_user(st
.st_ino
, &target_st
->st_ino
);
10706 __put_user(st
.st_mode
, &target_st
->st_mode
);
10707 __put_user(st
.st_uid
, &target_st
->st_uid
);
10708 __put_user(st
.st_gid
, &target_st
->st_gid
);
10709 __put_user(st
.st_nlink
, &target_st
->st_nlink
);
10710 __put_user(st
.st_rdev
, &target_st
->st_rdev
);
10711 __put_user(st
.st_size
, &target_st
->st_size
);
10712 __put_user(st
.st_blksize
, &target_st
->st_blksize
);
10713 __put_user(st
.st_blocks
, &target_st
->st_blocks
);
10714 __put_user(st
.st_atime
, &target_st
->target_st_atime
);
10715 __put_user(st
.st_mtime
, &target_st
->target_st_mtime
);
10716 __put_user(st
.st_ctime
, &target_st
->target_st_ctime
);
10717 #if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
10718 __put_user(st
.st_atim
.tv_nsec
,
10719 &target_st
->target_st_atime_nsec
);
10720 __put_user(st
.st_mtim
.tv_nsec
,
10721 &target_st
->target_st_mtime_nsec
);
10722 __put_user(st
.st_ctim
.tv_nsec
,
10723 &target_st
->target_st_ctime_nsec
);
10725 unlock_user_struct(target_st
, arg2
, 1);
10730 case TARGET_NR_vhangup
:
10731 return get_errno(vhangup());
10732 #ifdef TARGET_NR_syscall
10733 case TARGET_NR_syscall
:
10734 return do_syscall(cpu_env
, arg1
& 0xffff, arg2
, arg3
, arg4
, arg5
,
10735 arg6
, arg7
, arg8
, 0);
10737 #if defined(TARGET_NR_wait4)
10738 case TARGET_NR_wait4
:
10741 abi_long status_ptr
= arg2
;
10742 struct rusage rusage
, *rusage_ptr
;
10743 abi_ulong target_rusage
= arg4
;
10744 abi_long rusage_err
;
10746 rusage_ptr
= &rusage
;
10749 ret
= get_errno(safe_wait4(arg1
, &status
, arg3
, rusage_ptr
));
10750 if (!is_error(ret
)) {
10751 if (status_ptr
&& ret
) {
10752 status
= host_to_target_waitstatus(status
);
10753 if (put_user_s32(status
, status_ptr
))
10754 return -TARGET_EFAULT
;
10756 if (target_rusage
) {
10757 rusage_err
= host_to_target_rusage(target_rusage
, &rusage
);
10766 #ifdef TARGET_NR_swapoff
10767 case TARGET_NR_swapoff
:
10768 if (!(p
= lock_user_string(arg1
)))
10769 return -TARGET_EFAULT
;
10770 ret
= get_errno(swapoff(p
));
10771 unlock_user(p
, arg1
, 0);
10774 case TARGET_NR_sysinfo
:
10776 struct target_sysinfo
*target_value
;
10777 struct sysinfo value
;
10778 ret
= get_errno(sysinfo(&value
));
10779 if (!is_error(ret
) && arg1
)
10781 if (!lock_user_struct(VERIFY_WRITE
, target_value
, arg1
, 0))
10782 return -TARGET_EFAULT
;
10783 __put_user(value
.uptime
, &target_value
->uptime
);
10784 __put_user(value
.loads
[0], &target_value
->loads
[0]);
10785 __put_user(value
.loads
[1], &target_value
->loads
[1]);
10786 __put_user(value
.loads
[2], &target_value
->loads
[2]);
10787 __put_user(value
.totalram
, &target_value
->totalram
);
10788 __put_user(value
.freeram
, &target_value
->freeram
);
10789 __put_user(value
.sharedram
, &target_value
->sharedram
);
10790 __put_user(value
.bufferram
, &target_value
->bufferram
);
10791 __put_user(value
.totalswap
, &target_value
->totalswap
);
10792 __put_user(value
.freeswap
, &target_value
->freeswap
);
10793 __put_user(value
.procs
, &target_value
->procs
);
10794 __put_user(value
.totalhigh
, &target_value
->totalhigh
);
10795 __put_user(value
.freehigh
, &target_value
->freehigh
);
10796 __put_user(value
.mem_unit
, &target_value
->mem_unit
);
10797 unlock_user_struct(target_value
, arg1
, 1);
10801 #ifdef TARGET_NR_ipc
10802 case TARGET_NR_ipc
:
10803 return do_ipc(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
10805 #ifdef TARGET_NR_semget
10806 case TARGET_NR_semget
:
10807 return get_errno(semget(arg1
, arg2
, arg3
));
10809 #ifdef TARGET_NR_semop
10810 case TARGET_NR_semop
:
10811 return do_semtimedop(arg1
, arg2
, arg3
, 0, false);
10813 #ifdef TARGET_NR_semtimedop
10814 case TARGET_NR_semtimedop
:
10815 return do_semtimedop(arg1
, arg2
, arg3
, arg4
, false);
10817 #ifdef TARGET_NR_semtimedop_time64
10818 case TARGET_NR_semtimedop_time64
:
10819 return do_semtimedop(arg1
, arg2
, arg3
, arg4
, true);
10821 #ifdef TARGET_NR_semctl
10822 case TARGET_NR_semctl
:
10823 return do_semctl(arg1
, arg2
, arg3
, arg4
);
10825 #ifdef TARGET_NR_msgctl
10826 case TARGET_NR_msgctl
:
10827 return do_msgctl(arg1
, arg2
, arg3
);
10829 #ifdef TARGET_NR_msgget
10830 case TARGET_NR_msgget
:
10831 return get_errno(msgget(arg1
, arg2
));
10833 #ifdef TARGET_NR_msgrcv
10834 case TARGET_NR_msgrcv
:
10835 return do_msgrcv(arg1
, arg2
, arg3
, arg4
, arg5
);
10837 #ifdef TARGET_NR_msgsnd
10838 case TARGET_NR_msgsnd
:
10839 return do_msgsnd(arg1
, arg2
, arg3
, arg4
);
10841 #ifdef TARGET_NR_shmget
10842 case TARGET_NR_shmget
:
10843 return get_errno(shmget(arg1
, arg2
, arg3
));
10845 #ifdef TARGET_NR_shmctl
10846 case TARGET_NR_shmctl
:
10847 return do_shmctl(arg1
, arg2
, arg3
);
10849 #ifdef TARGET_NR_shmat
10850 case TARGET_NR_shmat
:
10851 return do_shmat(cpu_env
, arg1
, arg2
, arg3
);
10853 #ifdef TARGET_NR_shmdt
10854 case TARGET_NR_shmdt
:
10855 return do_shmdt(arg1
);
10857 case TARGET_NR_fsync
:
10858 return get_errno(fsync(arg1
));
10859 case TARGET_NR_clone
:
10860 /* Linux manages to have three different orderings for its
10861 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
10862 * match the kernel's CONFIG_CLONE_* settings.
10863 * Microblaze is further special in that it uses a sixth
10864 * implicit argument to clone for the TLS pointer.
10866 #if defined(TARGET_MICROBLAZE)
10867 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg4
, arg6
, arg5
));
10868 #elif defined(TARGET_CLONE_BACKWARDS)
10869 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
));
10870 #elif defined(TARGET_CLONE_BACKWARDS2)
10871 ret
= get_errno(do_fork(cpu_env
, arg2
, arg1
, arg3
, arg5
, arg4
));
10873 ret
= get_errno(do_fork(cpu_env
, arg1
, arg2
, arg3
, arg5
, arg4
));
10876 #ifdef __NR_exit_group
10877 /* new thread calls */
10878 case TARGET_NR_exit_group
:
10879 preexit_cleanup(cpu_env
, arg1
);
10880 return get_errno(exit_group(arg1
));
10882 case TARGET_NR_setdomainname
:
10883 if (!(p
= lock_user_string(arg1
)))
10884 return -TARGET_EFAULT
;
10885 ret
= get_errno(setdomainname(p
, arg2
));
10886 unlock_user(p
, arg1
, 0);
10888 case TARGET_NR_uname
:
10889 /* no need to transcode because we use the linux syscall */
10891 struct new_utsname
* buf
;
10893 if (!lock_user_struct(VERIFY_WRITE
, buf
, arg1
, 0))
10894 return -TARGET_EFAULT
;
10895 ret
= get_errno(sys_uname(buf
));
10896 if (!is_error(ret
)) {
10897 /* Overwrite the native machine name with whatever is being
10899 g_strlcpy(buf
->machine
, cpu_to_uname_machine(cpu_env
),
10900 sizeof(buf
->machine
));
10901 /* Allow the user to override the reported release. */
10902 if (qemu_uname_release
&& *qemu_uname_release
) {
10903 g_strlcpy(buf
->release
, qemu_uname_release
,
10904 sizeof(buf
->release
));
10907 unlock_user_struct(buf
, arg1
, 1);
10911 case TARGET_NR_modify_ldt
:
10912 return do_modify_ldt(cpu_env
, arg1
, arg2
, arg3
);
10913 #if !defined(TARGET_X86_64)
10914 case TARGET_NR_vm86
:
10915 return do_vm86(cpu_env
, arg1
, arg2
);
10918 #if defined(TARGET_NR_adjtimex)
10919 case TARGET_NR_adjtimex
:
10921 struct timex host_buf
;
10923 if (target_to_host_timex(&host_buf
, arg1
) != 0) {
10924 return -TARGET_EFAULT
;
10926 ret
= get_errno(adjtimex(&host_buf
));
10927 if (!is_error(ret
)) {
10928 if (host_to_target_timex(arg1
, &host_buf
) != 0) {
10929 return -TARGET_EFAULT
;
10935 #if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10936 case TARGET_NR_clock_adjtime
:
10938 struct timex htx
, *phtx
= &htx
;
10940 if (target_to_host_timex(phtx
, arg2
) != 0) {
10941 return -TARGET_EFAULT
;
10943 ret
= get_errno(clock_adjtime(arg1
, phtx
));
10944 if (!is_error(ret
) && phtx
) {
10945 if (host_to_target_timex(arg2
, phtx
) != 0) {
10946 return -TARGET_EFAULT
;
10952 #if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
10953 case TARGET_NR_clock_adjtime64
:
10957 if (target_to_host_timex64(&htx
, arg2
) != 0) {
10958 return -TARGET_EFAULT
;
10960 ret
= get_errno(clock_adjtime(arg1
, &htx
));
10961 if (!is_error(ret
) && host_to_target_timex64(arg2
, &htx
)) {
10962 return -TARGET_EFAULT
;
10967 case TARGET_NR_getpgid
:
10968 return get_errno(getpgid(arg1
));
10969 case TARGET_NR_fchdir
:
10970 return get_errno(fchdir(arg1
));
10971 case TARGET_NR_personality
:
10972 return get_errno(personality(arg1
));
10973 #ifdef TARGET_NR__llseek /* Not on alpha */
10974 case TARGET_NR__llseek
:
10977 #if !defined(__NR_llseek)
10978 res
= lseek(arg1
, ((uint64_t)arg2
<< 32) | (abi_ulong
)arg3
, arg5
);
10980 ret
= get_errno(res
);
10985 ret
= get_errno(_llseek(arg1
, arg2
, arg3
, &res
, arg5
));
10987 if ((ret
== 0) && put_user_s64(res
, arg4
)) {
10988 return -TARGET_EFAULT
;
10993 #ifdef TARGET_NR_getdents
10994 case TARGET_NR_getdents
:
10995 return do_getdents(arg1
, arg2
, arg3
);
10996 #endif /* TARGET_NR_getdents */
10997 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
10998 case TARGET_NR_getdents64
:
10999 return do_getdents64(arg1
, arg2
, arg3
);
11000 #endif /* TARGET_NR_getdents64 */
11001 #if defined(TARGET_NR__newselect)
11002 case TARGET_NR__newselect
:
11003 return do_select(arg1
, arg2
, arg3
, arg4
, arg5
);
11005 #ifdef TARGET_NR_poll
11006 case TARGET_NR_poll
:
11007 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, false, false);
11009 #ifdef TARGET_NR_ppoll
11010 case TARGET_NR_ppoll
:
11011 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, true, false);
11013 #ifdef TARGET_NR_ppoll_time64
11014 case TARGET_NR_ppoll_time64
:
11015 return do_ppoll(arg1
, arg2
, arg3
, arg4
, arg5
, true, true);
11017 case TARGET_NR_flock
:
11018 /* NOTE: the flock constant seems to be the same for every
11020 return get_errno(safe_flock(arg1
, arg2
));
11021 case TARGET_NR_readv
:
11023 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
11025 ret
= get_errno(safe_readv(arg1
, vec
, arg3
));
11026 unlock_iovec(vec
, arg2
, arg3
, 1);
11028 ret
= -host_to_target_errno(errno
);
11032 case TARGET_NR_writev
:
11034 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
11036 ret
= get_errno(safe_writev(arg1
, vec
, arg3
));
11037 unlock_iovec(vec
, arg2
, arg3
, 0);
11039 ret
= -host_to_target_errno(errno
);
11043 #if defined(TARGET_NR_preadv)
11044 case TARGET_NR_preadv
:
11046 struct iovec
*vec
= lock_iovec(VERIFY_WRITE
, arg2
, arg3
, 0);
11048 unsigned long low
, high
;
11050 target_to_host_low_high(arg4
, arg5
, &low
, &high
);
11051 ret
= get_errno(safe_preadv(arg1
, vec
, arg3
, low
, high
));
11052 unlock_iovec(vec
, arg2
, arg3
, 1);
11054 ret
= -host_to_target_errno(errno
);
11059 #if defined(TARGET_NR_pwritev)
11060 case TARGET_NR_pwritev
:
11062 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
11064 unsigned long low
, high
;
11066 target_to_host_low_high(arg4
, arg5
, &low
, &high
);
11067 ret
= get_errno(safe_pwritev(arg1
, vec
, arg3
, low
, high
));
11068 unlock_iovec(vec
, arg2
, arg3
, 0);
11070 ret
= -host_to_target_errno(errno
);
11075 case TARGET_NR_getsid
:
11076 return get_errno(getsid(arg1
));
11077 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
11078 case TARGET_NR_fdatasync
:
11079 return get_errno(fdatasync(arg1
));
11081 case TARGET_NR_sched_getaffinity
:
11083 unsigned int mask_size
;
11084 unsigned long *mask
;
11087 * sched_getaffinity needs multiples of ulong, so need to take
11088 * care of mismatches between target ulong and host ulong sizes.
11090 if (arg2
& (sizeof(abi_ulong
) - 1)) {
11091 return -TARGET_EINVAL
;
11093 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
11095 mask
= alloca(mask_size
);
11096 memset(mask
, 0, mask_size
);
11097 ret
= get_errno(sys_sched_getaffinity(arg1
, mask_size
, mask
));
11099 if (!is_error(ret
)) {
11101 /* More data returned than the caller's buffer will fit.
11102 * This only happens if sizeof(abi_long) < sizeof(long)
11103 * and the caller passed us a buffer holding an odd number
11104 * of abi_longs. If the host kernel is actually using the
11105 * extra 4 bytes then fail EINVAL; otherwise we can just
11106 * ignore them and only copy the interesting part.
11108 int numcpus
= sysconf(_SC_NPROCESSORS_CONF
);
11109 if (numcpus
> arg2
* 8) {
11110 return -TARGET_EINVAL
;
11115 if (host_to_target_cpu_mask(mask
, mask_size
, arg3
, ret
)) {
11116 return -TARGET_EFAULT
;
11121 case TARGET_NR_sched_setaffinity
:
11123 unsigned int mask_size
;
11124 unsigned long *mask
;
11127 * sched_setaffinity needs multiples of ulong, so need to take
11128 * care of mismatches between target ulong and host ulong sizes.
11130 if (arg2
& (sizeof(abi_ulong
) - 1)) {
11131 return -TARGET_EINVAL
;
11133 mask_size
= (arg2
+ (sizeof(*mask
) - 1)) & ~(sizeof(*mask
) - 1);
11134 mask
= alloca(mask_size
);
11136 ret
= target_to_host_cpu_mask(mask
, mask_size
, arg3
, arg2
);
11141 return get_errno(sys_sched_setaffinity(arg1
, mask_size
, mask
));
11143 case TARGET_NR_getcpu
:
11145 unsigned cpu
, node
;
11146 ret
= get_errno(sys_getcpu(arg1
? &cpu
: NULL
,
11147 arg2
? &node
: NULL
,
11149 if (is_error(ret
)) {
11152 if (arg1
&& put_user_u32(cpu
, arg1
)) {
11153 return -TARGET_EFAULT
;
11155 if (arg2
&& put_user_u32(node
, arg2
)) {
11156 return -TARGET_EFAULT
;
11160 case TARGET_NR_sched_setparam
:
11162 struct target_sched_param
*target_schp
;
11163 struct sched_param schp
;
11166 return -TARGET_EINVAL
;
11168 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg2
, 1)) {
11169 return -TARGET_EFAULT
;
11171 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
11172 unlock_user_struct(target_schp
, arg2
, 0);
11173 return get_errno(sys_sched_setparam(arg1
, &schp
));
11175 case TARGET_NR_sched_getparam
:
11177 struct target_sched_param
*target_schp
;
11178 struct sched_param schp
;
11181 return -TARGET_EINVAL
;
11183 ret
= get_errno(sys_sched_getparam(arg1
, &schp
));
11184 if (!is_error(ret
)) {
11185 if (!lock_user_struct(VERIFY_WRITE
, target_schp
, arg2
, 0)) {
11186 return -TARGET_EFAULT
;
11188 target_schp
->sched_priority
= tswap32(schp
.sched_priority
);
11189 unlock_user_struct(target_schp
, arg2
, 1);
11193 case TARGET_NR_sched_setscheduler
:
11195 struct target_sched_param
*target_schp
;
11196 struct sched_param schp
;
11198 return -TARGET_EINVAL
;
11200 if (!lock_user_struct(VERIFY_READ
, target_schp
, arg3
, 1)) {
11201 return -TARGET_EFAULT
;
11203 schp
.sched_priority
= tswap32(target_schp
->sched_priority
);
11204 unlock_user_struct(target_schp
, arg3
, 0);
11205 return get_errno(sys_sched_setscheduler(arg1
, arg2
, &schp
));
11207 case TARGET_NR_sched_getscheduler
:
11208 return get_errno(sys_sched_getscheduler(arg1
));
11209 case TARGET_NR_sched_getattr
:
11211 struct target_sched_attr
*target_scha
;
11212 struct sched_attr scha
;
11214 return -TARGET_EINVAL
;
11216 if (arg3
> sizeof(scha
)) {
11217 arg3
= sizeof(scha
);
11219 ret
= get_errno(sys_sched_getattr(arg1
, &scha
, arg3
, arg4
));
11220 if (!is_error(ret
)) {
11221 target_scha
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11222 if (!target_scha
) {
11223 return -TARGET_EFAULT
;
11225 target_scha
->size
= tswap32(scha
.size
);
11226 target_scha
->sched_policy
= tswap32(scha
.sched_policy
);
11227 target_scha
->sched_flags
= tswap64(scha
.sched_flags
);
11228 target_scha
->sched_nice
= tswap32(scha
.sched_nice
);
11229 target_scha
->sched_priority
= tswap32(scha
.sched_priority
);
11230 target_scha
->sched_runtime
= tswap64(scha
.sched_runtime
);
11231 target_scha
->sched_deadline
= tswap64(scha
.sched_deadline
);
11232 target_scha
->sched_period
= tswap64(scha
.sched_period
);
11233 if (scha
.size
> offsetof(struct sched_attr
, sched_util_min
)) {
11234 target_scha
->sched_util_min
= tswap32(scha
.sched_util_min
);
11235 target_scha
->sched_util_max
= tswap32(scha
.sched_util_max
);
11237 unlock_user(target_scha
, arg2
, arg3
);
11241 case TARGET_NR_sched_setattr
:
11243 struct target_sched_attr
*target_scha
;
11244 struct sched_attr scha
;
11248 return -TARGET_EINVAL
;
11250 if (get_user_u32(size
, arg2
)) {
11251 return -TARGET_EFAULT
;
11254 size
= offsetof(struct target_sched_attr
, sched_util_min
);
11256 if (size
< offsetof(struct target_sched_attr
, sched_util_min
)) {
11257 if (put_user_u32(sizeof(struct target_sched_attr
), arg2
)) {
11258 return -TARGET_EFAULT
;
11260 return -TARGET_E2BIG
;
11263 zeroed
= check_zeroed_user(arg2
, sizeof(struct target_sched_attr
), size
);
11266 } else if (zeroed
== 0) {
11267 if (put_user_u32(sizeof(struct target_sched_attr
), arg2
)) {
11268 return -TARGET_EFAULT
;
11270 return -TARGET_E2BIG
;
11272 if (size
> sizeof(struct target_sched_attr
)) {
11273 size
= sizeof(struct target_sched_attr
);
11276 target_scha
= lock_user(VERIFY_READ
, arg2
, size
, 1);
11277 if (!target_scha
) {
11278 return -TARGET_EFAULT
;
11281 scha
.sched_policy
= tswap32(target_scha
->sched_policy
);
11282 scha
.sched_flags
= tswap64(target_scha
->sched_flags
);
11283 scha
.sched_nice
= tswap32(target_scha
->sched_nice
);
11284 scha
.sched_priority
= tswap32(target_scha
->sched_priority
);
11285 scha
.sched_runtime
= tswap64(target_scha
->sched_runtime
);
11286 scha
.sched_deadline
= tswap64(target_scha
->sched_deadline
);
11287 scha
.sched_period
= tswap64(target_scha
->sched_period
);
11288 if (size
> offsetof(struct target_sched_attr
, sched_util_min
)) {
11289 scha
.sched_util_min
= tswap32(target_scha
->sched_util_min
);
11290 scha
.sched_util_max
= tswap32(target_scha
->sched_util_max
);
11292 unlock_user(target_scha
, arg2
, 0);
11293 return get_errno(sys_sched_setattr(arg1
, &scha
, arg3
));
11295 case TARGET_NR_sched_yield
:
11296 return get_errno(sched_yield());
11297 case TARGET_NR_sched_get_priority_max
:
11298 return get_errno(sched_get_priority_max(arg1
));
11299 case TARGET_NR_sched_get_priority_min
:
11300 return get_errno(sched_get_priority_min(arg1
));
11301 #ifdef TARGET_NR_sched_rr_get_interval
11302 case TARGET_NR_sched_rr_get_interval
:
11304 struct timespec ts
;
11305 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
11306 if (!is_error(ret
)) {
11307 ret
= host_to_target_timespec(arg2
, &ts
);
11312 #ifdef TARGET_NR_sched_rr_get_interval_time64
11313 case TARGET_NR_sched_rr_get_interval_time64
:
11315 struct timespec ts
;
11316 ret
= get_errno(sched_rr_get_interval(arg1
, &ts
));
11317 if (!is_error(ret
)) {
11318 ret
= host_to_target_timespec64(arg2
, &ts
);
11323 #if defined(TARGET_NR_nanosleep)
11324 case TARGET_NR_nanosleep
:
11326 struct timespec req
, rem
;
11327 target_to_host_timespec(&req
, arg1
);
11328 ret
= get_errno(safe_nanosleep(&req
, &rem
));
11329 if (is_error(ret
) && arg2
) {
11330 host_to_target_timespec(arg2
, &rem
);
11335 case TARGET_NR_prctl
:
11336 return do_prctl(cpu_env
, arg1
, arg2
, arg3
, arg4
, arg5
);
11338 #ifdef TARGET_NR_arch_prctl
11339 case TARGET_NR_arch_prctl
:
11340 return do_arch_prctl(cpu_env
, arg1
, arg2
);
11342 #ifdef TARGET_NR_pread64
11343 case TARGET_NR_pread64
:
11344 if (regpairs_aligned(cpu_env
, num
)) {
11348 if (arg2
== 0 && arg3
== 0) {
11349 /* Special-case NULL buffer and zero length, which should succeed */
11352 p
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
11354 return -TARGET_EFAULT
;
11357 ret
= get_errno(pread64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
11358 unlock_user(p
, arg2
, ret
);
11360 case TARGET_NR_pwrite64
:
11361 if (regpairs_aligned(cpu_env
, num
)) {
11365 if (arg2
== 0 && arg3
== 0) {
11366 /* Special-case NULL buffer and zero length, which should succeed */
11369 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
11371 return -TARGET_EFAULT
;
11374 ret
= get_errno(pwrite64(arg1
, p
, arg3
, target_offset64(arg4
, arg5
)));
11375 unlock_user(p
, arg2
, 0);
11378 case TARGET_NR_getcwd
:
11379 if (!(p
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0)))
11380 return -TARGET_EFAULT
;
11381 ret
= get_errno(sys_getcwd1(p
, arg2
));
11382 unlock_user(p
, arg1
, ret
);
11384 case TARGET_NR_capget
:
11385 case TARGET_NR_capset
:
11387 struct target_user_cap_header
*target_header
;
11388 struct target_user_cap_data
*target_data
= NULL
;
11389 struct __user_cap_header_struct header
;
11390 struct __user_cap_data_struct data
[2];
11391 struct __user_cap_data_struct
*dataptr
= NULL
;
11392 int i
, target_datalen
;
11393 int data_items
= 1;
11395 if (!lock_user_struct(VERIFY_WRITE
, target_header
, arg1
, 1)) {
11396 return -TARGET_EFAULT
;
11398 header
.version
= tswap32(target_header
->version
);
11399 header
.pid
= tswap32(target_header
->pid
);
11401 if (header
.version
!= _LINUX_CAPABILITY_VERSION
) {
11402 /* Version 2 and up takes pointer to two user_data structs */
11406 target_datalen
= sizeof(*target_data
) * data_items
;
11409 if (num
== TARGET_NR_capget
) {
11410 target_data
= lock_user(VERIFY_WRITE
, arg2
, target_datalen
, 0);
11412 target_data
= lock_user(VERIFY_READ
, arg2
, target_datalen
, 1);
11414 if (!target_data
) {
11415 unlock_user_struct(target_header
, arg1
, 0);
11416 return -TARGET_EFAULT
;
11419 if (num
== TARGET_NR_capset
) {
11420 for (i
= 0; i
< data_items
; i
++) {
11421 data
[i
].effective
= tswap32(target_data
[i
].effective
);
11422 data
[i
].permitted
= tswap32(target_data
[i
].permitted
);
11423 data
[i
].inheritable
= tswap32(target_data
[i
].inheritable
);
11430 if (num
== TARGET_NR_capget
) {
11431 ret
= get_errno(capget(&header
, dataptr
));
11433 ret
= get_errno(capset(&header
, dataptr
));
11436 /* The kernel always updates version for both capget and capset */
11437 target_header
->version
= tswap32(header
.version
);
11438 unlock_user_struct(target_header
, arg1
, 1);
11441 if (num
== TARGET_NR_capget
) {
11442 for (i
= 0; i
< data_items
; i
++) {
11443 target_data
[i
].effective
= tswap32(data
[i
].effective
);
11444 target_data
[i
].permitted
= tswap32(data
[i
].permitted
);
11445 target_data
[i
].inheritable
= tswap32(data
[i
].inheritable
);
11447 unlock_user(target_data
, arg2
, target_datalen
);
11449 unlock_user(target_data
, arg2
, 0);
11454 case TARGET_NR_sigaltstack
:
11455 return do_sigaltstack(arg1
, arg2
, cpu_env
);
11457 #ifdef CONFIG_SENDFILE
11458 #ifdef TARGET_NR_sendfile
11459 case TARGET_NR_sendfile
:
11461 off_t
*offp
= NULL
;
11464 ret
= get_user_sal(off
, arg3
);
11465 if (is_error(ret
)) {
11470 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
11471 if (!is_error(ret
) && arg3
) {
11472 abi_long ret2
= put_user_sal(off
, arg3
);
11473 if (is_error(ret2
)) {
11480 #ifdef TARGET_NR_sendfile64
11481 case TARGET_NR_sendfile64
:
11483 off_t
*offp
= NULL
;
11486 ret
= get_user_s64(off
, arg3
);
11487 if (is_error(ret
)) {
11492 ret
= get_errno(sendfile(arg1
, arg2
, offp
, arg4
));
11493 if (!is_error(ret
) && arg3
) {
11494 abi_long ret2
= put_user_s64(off
, arg3
);
11495 if (is_error(ret2
)) {
11503 #ifdef TARGET_NR_vfork
11504 case TARGET_NR_vfork
:
11505 return get_errno(do_fork(cpu_env
,
11506 CLONE_VFORK
| CLONE_VM
| TARGET_SIGCHLD
,
11509 #ifdef TARGET_NR_ugetrlimit
11510 case TARGET_NR_ugetrlimit
:
11512 struct rlimit rlim
;
11513 int resource
= target_to_host_resource(arg1
);
11514 ret
= get_errno(getrlimit(resource
, &rlim
));
11515 if (!is_error(ret
)) {
11516 struct target_rlimit
*target_rlim
;
11517 if (!lock_user_struct(VERIFY_WRITE
, target_rlim
, arg2
, 0))
11518 return -TARGET_EFAULT
;
11519 target_rlim
->rlim_cur
= host_to_target_rlim(rlim
.rlim_cur
);
11520 target_rlim
->rlim_max
= host_to_target_rlim(rlim
.rlim_max
);
11521 unlock_user_struct(target_rlim
, arg2
, 1);
11526 #ifdef TARGET_NR_truncate64
11527 case TARGET_NR_truncate64
:
11528 if (!(p
= lock_user_string(arg1
)))
11529 return -TARGET_EFAULT
;
11530 ret
= target_truncate64(cpu_env
, p
, arg2
, arg3
, arg4
);
11531 unlock_user(p
, arg1
, 0);
11534 #ifdef TARGET_NR_ftruncate64
11535 case TARGET_NR_ftruncate64
:
11536 return target_ftruncate64(cpu_env
, arg1
, arg2
, arg3
, arg4
);
11538 #ifdef TARGET_NR_stat64
11539 case TARGET_NR_stat64
:
11540 if (!(p
= lock_user_string(arg1
))) {
11541 return -TARGET_EFAULT
;
11543 ret
= get_errno(stat(path(p
), &st
));
11544 unlock_user(p
, arg1
, 0);
11545 if (!is_error(ret
))
11546 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11549 #ifdef TARGET_NR_lstat64
11550 case TARGET_NR_lstat64
:
11551 if (!(p
= lock_user_string(arg1
))) {
11552 return -TARGET_EFAULT
;
11554 ret
= get_errno(lstat(path(p
), &st
));
11555 unlock_user(p
, arg1
, 0);
11556 if (!is_error(ret
))
11557 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11560 #ifdef TARGET_NR_fstat64
11561 case TARGET_NR_fstat64
:
11562 ret
= get_errno(fstat(arg1
, &st
));
11563 if (!is_error(ret
))
11564 ret
= host_to_target_stat64(cpu_env
, arg2
, &st
);
11567 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
11568 #ifdef TARGET_NR_fstatat64
11569 case TARGET_NR_fstatat64
:
11571 #ifdef TARGET_NR_newfstatat
11572 case TARGET_NR_newfstatat
:
11574 if (!(p
= lock_user_string(arg2
))) {
11575 return -TARGET_EFAULT
;
11577 ret
= get_errno(fstatat(arg1
, path(p
), &st
, arg4
));
11578 unlock_user(p
, arg2
, 0);
11579 if (!is_error(ret
))
11580 ret
= host_to_target_stat64(cpu_env
, arg3
, &st
);
11583 #if defined(TARGET_NR_statx)
11584 case TARGET_NR_statx
:
11586 struct target_statx
*target_stx
;
11590 p
= lock_user_string(arg2
);
11592 return -TARGET_EFAULT
;
11594 #if defined(__NR_statx)
11597 * It is assumed that struct statx is architecture independent.
11599 struct target_statx host_stx
;
11602 ret
= get_errno(sys_statx(dirfd
, p
, flags
, mask
, &host_stx
));
11603 if (!is_error(ret
)) {
11604 if (host_to_target_statx(&host_stx
, arg5
) != 0) {
11605 unlock_user(p
, arg2
, 0);
11606 return -TARGET_EFAULT
;
11610 if (ret
!= -TARGET_ENOSYS
) {
11611 unlock_user(p
, arg2
, 0);
11616 ret
= get_errno(fstatat(dirfd
, path(p
), &st
, flags
));
11617 unlock_user(p
, arg2
, 0);
11619 if (!is_error(ret
)) {
11620 if (!lock_user_struct(VERIFY_WRITE
, target_stx
, arg5
, 0)) {
11621 return -TARGET_EFAULT
;
11623 memset(target_stx
, 0, sizeof(*target_stx
));
11624 __put_user(major(st
.st_dev
), &target_stx
->stx_dev_major
);
11625 __put_user(minor(st
.st_dev
), &target_stx
->stx_dev_minor
);
11626 __put_user(st
.st_ino
, &target_stx
->stx_ino
);
11627 __put_user(st
.st_mode
, &target_stx
->stx_mode
);
11628 __put_user(st
.st_uid
, &target_stx
->stx_uid
);
11629 __put_user(st
.st_gid
, &target_stx
->stx_gid
);
11630 __put_user(st
.st_nlink
, &target_stx
->stx_nlink
);
11631 __put_user(major(st
.st_rdev
), &target_stx
->stx_rdev_major
);
11632 __put_user(minor(st
.st_rdev
), &target_stx
->stx_rdev_minor
);
11633 __put_user(st
.st_size
, &target_stx
->stx_size
);
11634 __put_user(st
.st_blksize
, &target_stx
->stx_blksize
);
11635 __put_user(st
.st_blocks
, &target_stx
->stx_blocks
);
11636 __put_user(st
.st_atime
, &target_stx
->stx_atime
.tv_sec
);
11637 __put_user(st
.st_mtime
, &target_stx
->stx_mtime
.tv_sec
);
11638 __put_user(st
.st_ctime
, &target_stx
->stx_ctime
.tv_sec
);
11639 unlock_user_struct(target_stx
, arg5
, 1);
11644 #ifdef TARGET_NR_lchown
11645 case TARGET_NR_lchown
:
11646 if (!(p
= lock_user_string(arg1
)))
11647 return -TARGET_EFAULT
;
11648 ret
= get_errno(lchown(p
, low2highuid(arg2
), low2highgid(arg3
)));
11649 unlock_user(p
, arg1
, 0);
11652 #ifdef TARGET_NR_getuid
11653 case TARGET_NR_getuid
:
11654 return get_errno(high2lowuid(getuid()));
11656 #ifdef TARGET_NR_getgid
11657 case TARGET_NR_getgid
:
11658 return get_errno(high2lowgid(getgid()));
11660 #ifdef TARGET_NR_geteuid
11661 case TARGET_NR_geteuid
:
11662 return get_errno(high2lowuid(geteuid()));
11664 #ifdef TARGET_NR_getegid
11665 case TARGET_NR_getegid
:
11666 return get_errno(high2lowgid(getegid()));
11668 case TARGET_NR_setreuid
:
11669 return get_errno(setreuid(low2highuid(arg1
), low2highuid(arg2
)));
11670 case TARGET_NR_setregid
:
11671 return get_errno(setregid(low2highgid(arg1
), low2highgid(arg2
)));
11672 case TARGET_NR_getgroups
:
11673 { /* the same code as for TARGET_NR_getgroups32 */
11674 int gidsetsize
= arg1
;
11675 target_id
*target_grouplist
;
11676 g_autofree gid_t
*grouplist
= NULL
;
11679 if (gidsetsize
> NGROUPS_MAX
|| gidsetsize
< 0) {
11680 return -TARGET_EINVAL
;
11682 if (gidsetsize
> 0) {
11683 grouplist
= g_try_new(gid_t
, gidsetsize
);
11685 return -TARGET_ENOMEM
;
11688 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
11689 if (!is_error(ret
) && gidsetsize
> 0) {
11690 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
,
11691 gidsetsize
* sizeof(target_id
), 0);
11692 if (!target_grouplist
) {
11693 return -TARGET_EFAULT
;
11695 for (i
= 0; i
< ret
; i
++) {
11696 target_grouplist
[i
] = tswapid(high2lowgid(grouplist
[i
]));
11698 unlock_user(target_grouplist
, arg2
,
11699 gidsetsize
* sizeof(target_id
));
11703 case TARGET_NR_setgroups
:
11704 { /* the same code as for TARGET_NR_setgroups32 */
11705 int gidsetsize
= arg1
;
11706 target_id
*target_grouplist
;
11707 g_autofree gid_t
*grouplist
= NULL
;
11710 if (gidsetsize
> NGROUPS_MAX
|| gidsetsize
< 0) {
11711 return -TARGET_EINVAL
;
11713 if (gidsetsize
> 0) {
11714 grouplist
= g_try_new(gid_t
, gidsetsize
);
11716 return -TARGET_ENOMEM
;
11718 target_grouplist
= lock_user(VERIFY_READ
, arg2
,
11719 gidsetsize
* sizeof(target_id
), 1);
11720 if (!target_grouplist
) {
11721 return -TARGET_EFAULT
;
11723 for (i
= 0; i
< gidsetsize
; i
++) {
11724 grouplist
[i
] = low2highgid(tswapid(target_grouplist
[i
]));
11726 unlock_user(target_grouplist
, arg2
,
11727 gidsetsize
* sizeof(target_id
));
11729 return get_errno(setgroups(gidsetsize
, grouplist
));
11731 case TARGET_NR_fchown
:
11732 return get_errno(fchown(arg1
, low2highuid(arg2
), low2highgid(arg3
)));
11733 #if defined(TARGET_NR_fchownat)
11734 case TARGET_NR_fchownat
:
11735 if (!(p
= lock_user_string(arg2
)))
11736 return -TARGET_EFAULT
;
11737 ret
= get_errno(fchownat(arg1
, p
, low2highuid(arg3
),
11738 low2highgid(arg4
), arg5
));
11739 unlock_user(p
, arg2
, 0);
11742 #ifdef TARGET_NR_setresuid
11743 case TARGET_NR_setresuid
:
11744 return get_errno(sys_setresuid(low2highuid(arg1
),
11746 low2highuid(arg3
)));
11748 #ifdef TARGET_NR_getresuid
11749 case TARGET_NR_getresuid
:
11751 uid_t ruid
, euid
, suid
;
11752 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
11753 if (!is_error(ret
)) {
11754 if (put_user_id(high2lowuid(ruid
), arg1
)
11755 || put_user_id(high2lowuid(euid
), arg2
)
11756 || put_user_id(high2lowuid(suid
), arg3
))
11757 return -TARGET_EFAULT
;
11762 #ifdef TARGET_NR_getresgid
11763 case TARGET_NR_setresgid
:
11764 return get_errno(sys_setresgid(low2highgid(arg1
),
11766 low2highgid(arg3
)));
11768 #ifdef TARGET_NR_getresgid
11769 case TARGET_NR_getresgid
:
11771 gid_t rgid
, egid
, sgid
;
11772 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
11773 if (!is_error(ret
)) {
11774 if (put_user_id(high2lowgid(rgid
), arg1
)
11775 || put_user_id(high2lowgid(egid
), arg2
)
11776 || put_user_id(high2lowgid(sgid
), arg3
))
11777 return -TARGET_EFAULT
;
11782 #ifdef TARGET_NR_chown
11783 case TARGET_NR_chown
:
11784 if (!(p
= lock_user_string(arg1
)))
11785 return -TARGET_EFAULT
;
11786 ret
= get_errno(chown(p
, low2highuid(arg2
), low2highgid(arg3
)));
11787 unlock_user(p
, arg1
, 0);
11790 case TARGET_NR_setuid
:
11791 return get_errno(sys_setuid(low2highuid(arg1
)));
11792 case TARGET_NR_setgid
:
11793 return get_errno(sys_setgid(low2highgid(arg1
)));
11794 case TARGET_NR_setfsuid
:
11795 return get_errno(setfsuid(arg1
));
11796 case TARGET_NR_setfsgid
:
11797 return get_errno(setfsgid(arg1
));
11799 #ifdef TARGET_NR_lchown32
11800 case TARGET_NR_lchown32
:
11801 if (!(p
= lock_user_string(arg1
)))
11802 return -TARGET_EFAULT
;
11803 ret
= get_errno(lchown(p
, arg2
, arg3
));
11804 unlock_user(p
, arg1
, 0);
11807 #ifdef TARGET_NR_getuid32
11808 case TARGET_NR_getuid32
:
11809 return get_errno(getuid());
11812 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11813 /* Alpha specific */
11814 case TARGET_NR_getxuid
:
11818 cpu_env
->ir
[IR_A4
]=euid
;
11820 return get_errno(getuid());
11822 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11823 /* Alpha specific */
11824 case TARGET_NR_getxgid
:
11828 cpu_env
->ir
[IR_A4
]=egid
;
11830 return get_errno(getgid());
11832 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11833 /* Alpha specific */
11834 case TARGET_NR_osf_getsysinfo
:
11835 ret
= -TARGET_EOPNOTSUPP
;
11837 case TARGET_GSI_IEEE_FP_CONTROL
:
11839 uint64_t fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11840 uint64_t swcr
= cpu_env
->swcr
;
11842 swcr
&= ~SWCR_STATUS_MASK
;
11843 swcr
|= (fpcr
>> 35) & SWCR_STATUS_MASK
;
11845 if (put_user_u64 (swcr
, arg2
))
11846 return -TARGET_EFAULT
;
11851 /* case GSI_IEEE_STATE_AT_SIGNAL:
11852 -- Not implemented in linux kernel.
11854 -- Retrieves current unaligned access state; not much used.
11855 case GSI_PROC_TYPE:
11856 -- Retrieves implver information; surely not used.
11857 case GSI_GET_HWRPB:
11858 -- Grabs a copy of the HWRPB; surely not used.
11863 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11864 /* Alpha specific */
11865 case TARGET_NR_osf_setsysinfo
:
11866 ret
= -TARGET_EOPNOTSUPP
;
11868 case TARGET_SSI_IEEE_FP_CONTROL
:
11870 uint64_t swcr
, fpcr
;
11872 if (get_user_u64 (swcr
, arg2
)) {
11873 return -TARGET_EFAULT
;
11877 * The kernel calls swcr_update_status to update the
11878 * status bits from the fpcr at every point that it
11879 * could be queried. Therefore, we store the status
11880 * bits only in FPCR.
11882 cpu_env
->swcr
= swcr
& (SWCR_TRAP_ENABLE_MASK
| SWCR_MAP_MASK
);
11884 fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11885 fpcr
&= ((uint64_t)FPCR_DYN_MASK
<< 32);
11886 fpcr
|= alpha_ieee_swcr_to_fpcr(swcr
);
11887 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11892 case TARGET_SSI_IEEE_RAISE_EXCEPTION
:
11894 uint64_t exc
, fpcr
, fex
;
11896 if (get_user_u64(exc
, arg2
)) {
11897 return -TARGET_EFAULT
;
11899 exc
&= SWCR_STATUS_MASK
;
11900 fpcr
= cpu_alpha_load_fpcr(cpu_env
);
11902 /* Old exceptions are not signaled. */
11903 fex
= alpha_ieee_fpcr_to_swcr(fpcr
);
11905 fex
>>= SWCR_STATUS_TO_EXCSUM_SHIFT
;
11906 fex
&= (cpu_env
)->swcr
;
11908 /* Update the hardware fpcr. */
11909 fpcr
|= alpha_ieee_swcr_to_fpcr(exc
);
11910 cpu_alpha_store_fpcr(cpu_env
, fpcr
);
11913 int si_code
= TARGET_FPE_FLTUNK
;
11914 target_siginfo_t info
;
11916 if (fex
& SWCR_TRAP_ENABLE_DNO
) {
11917 si_code
= TARGET_FPE_FLTUND
;
11919 if (fex
& SWCR_TRAP_ENABLE_INE
) {
11920 si_code
= TARGET_FPE_FLTRES
;
11922 if (fex
& SWCR_TRAP_ENABLE_UNF
) {
11923 si_code
= TARGET_FPE_FLTUND
;
11925 if (fex
& SWCR_TRAP_ENABLE_OVF
) {
11926 si_code
= TARGET_FPE_FLTOVF
;
11928 if (fex
& SWCR_TRAP_ENABLE_DZE
) {
11929 si_code
= TARGET_FPE_FLTDIV
;
11931 if (fex
& SWCR_TRAP_ENABLE_INV
) {
11932 si_code
= TARGET_FPE_FLTINV
;
11935 info
.si_signo
= SIGFPE
;
11937 info
.si_code
= si_code
;
11938 info
._sifields
._sigfault
._addr
= (cpu_env
)->pc
;
11939 queue_signal(cpu_env
, info
.si_signo
,
11940 QEMU_SI_FAULT
, &info
);
11946 /* case SSI_NVPAIRS:
11947 -- Used with SSIN_UACPROC to enable unaligned accesses.
11948 case SSI_IEEE_STATE_AT_SIGNAL:
11949 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
11950 -- Not implemented in linux kernel
11955 #ifdef TARGET_NR_osf_sigprocmask
11956 /* Alpha specific. */
11957 case TARGET_NR_osf_sigprocmask
:
11961 sigset_t set
, oldset
;
11964 case TARGET_SIG_BLOCK
:
11967 case TARGET_SIG_UNBLOCK
:
11970 case TARGET_SIG_SETMASK
:
11974 return -TARGET_EINVAL
;
11977 target_to_host_old_sigset(&set
, &mask
);
11978 ret
= do_sigprocmask(how
, &set
, &oldset
);
11980 host_to_target_old_sigset(&mask
, &oldset
);
11987 #ifdef TARGET_NR_getgid32
11988 case TARGET_NR_getgid32
:
11989 return get_errno(getgid());
11991 #ifdef TARGET_NR_geteuid32
11992 case TARGET_NR_geteuid32
:
11993 return get_errno(geteuid());
11995 #ifdef TARGET_NR_getegid32
11996 case TARGET_NR_getegid32
:
11997 return get_errno(getegid());
11999 #ifdef TARGET_NR_setreuid32
12000 case TARGET_NR_setreuid32
:
12001 return get_errno(setreuid(arg1
, arg2
));
12003 #ifdef TARGET_NR_setregid32
12004 case TARGET_NR_setregid32
:
12005 return get_errno(setregid(arg1
, arg2
));
12007 #ifdef TARGET_NR_getgroups32
12008 case TARGET_NR_getgroups32
:
12009 { /* the same code as for TARGET_NR_getgroups */
12010 int gidsetsize
= arg1
;
12011 uint32_t *target_grouplist
;
12012 g_autofree gid_t
*grouplist
= NULL
;
12015 if (gidsetsize
> NGROUPS_MAX
|| gidsetsize
< 0) {
12016 return -TARGET_EINVAL
;
12018 if (gidsetsize
> 0) {
12019 grouplist
= g_try_new(gid_t
, gidsetsize
);
12021 return -TARGET_ENOMEM
;
12024 ret
= get_errno(getgroups(gidsetsize
, grouplist
));
12025 if (!is_error(ret
) && gidsetsize
> 0) {
12026 target_grouplist
= lock_user(VERIFY_WRITE
, arg2
,
12027 gidsetsize
* 4, 0);
12028 if (!target_grouplist
) {
12029 return -TARGET_EFAULT
;
12031 for (i
= 0; i
< ret
; i
++) {
12032 target_grouplist
[i
] = tswap32(grouplist
[i
]);
12034 unlock_user(target_grouplist
, arg2
, gidsetsize
* 4);
12039 #ifdef TARGET_NR_setgroups32
12040 case TARGET_NR_setgroups32
:
12041 { /* the same code as for TARGET_NR_setgroups */
12042 int gidsetsize
= arg1
;
12043 uint32_t *target_grouplist
;
12044 g_autofree gid_t
*grouplist
= NULL
;
12047 if (gidsetsize
> NGROUPS_MAX
|| gidsetsize
< 0) {
12048 return -TARGET_EINVAL
;
12050 if (gidsetsize
> 0) {
12051 grouplist
= g_try_new(gid_t
, gidsetsize
);
12053 return -TARGET_ENOMEM
;
12055 target_grouplist
= lock_user(VERIFY_READ
, arg2
,
12056 gidsetsize
* 4, 1);
12057 if (!target_grouplist
) {
12058 return -TARGET_EFAULT
;
12060 for (i
= 0; i
< gidsetsize
; i
++) {
12061 grouplist
[i
] = tswap32(target_grouplist
[i
]);
12063 unlock_user(target_grouplist
, arg2
, 0);
12065 return get_errno(setgroups(gidsetsize
, grouplist
));
12068 #ifdef TARGET_NR_fchown32
12069 case TARGET_NR_fchown32
:
12070 return get_errno(fchown(arg1
, arg2
, arg3
));
12072 #ifdef TARGET_NR_setresuid32
12073 case TARGET_NR_setresuid32
:
12074 return get_errno(sys_setresuid(arg1
, arg2
, arg3
));
12076 #ifdef TARGET_NR_getresuid32
12077 case TARGET_NR_getresuid32
:
12079 uid_t ruid
, euid
, suid
;
12080 ret
= get_errno(getresuid(&ruid
, &euid
, &suid
));
12081 if (!is_error(ret
)) {
12082 if (put_user_u32(ruid
, arg1
)
12083 || put_user_u32(euid
, arg2
)
12084 || put_user_u32(suid
, arg3
))
12085 return -TARGET_EFAULT
;
12090 #ifdef TARGET_NR_setresgid32
12091 case TARGET_NR_setresgid32
:
12092 return get_errno(sys_setresgid(arg1
, arg2
, arg3
));
12094 #ifdef TARGET_NR_getresgid32
12095 case TARGET_NR_getresgid32
:
12097 gid_t rgid
, egid
, sgid
;
12098 ret
= get_errno(getresgid(&rgid
, &egid
, &sgid
));
12099 if (!is_error(ret
)) {
12100 if (put_user_u32(rgid
, arg1
)
12101 || put_user_u32(egid
, arg2
)
12102 || put_user_u32(sgid
, arg3
))
12103 return -TARGET_EFAULT
;
12108 #ifdef TARGET_NR_chown32
12109 case TARGET_NR_chown32
:
12110 if (!(p
= lock_user_string(arg1
)))
12111 return -TARGET_EFAULT
;
12112 ret
= get_errno(chown(p
, arg2
, arg3
));
12113 unlock_user(p
, arg1
, 0);
12116 #ifdef TARGET_NR_setuid32
12117 case TARGET_NR_setuid32
:
12118 return get_errno(sys_setuid(arg1
));
12120 #ifdef TARGET_NR_setgid32
12121 case TARGET_NR_setgid32
:
12122 return get_errno(sys_setgid(arg1
));
12124 #ifdef TARGET_NR_setfsuid32
12125 case TARGET_NR_setfsuid32
:
12126 return get_errno(setfsuid(arg1
));
12128 #ifdef TARGET_NR_setfsgid32
12129 case TARGET_NR_setfsgid32
:
12130 return get_errno(setfsgid(arg1
));
12132 #ifdef TARGET_NR_mincore
12133 case TARGET_NR_mincore
:
12135 void *a
= lock_user(VERIFY_NONE
, arg1
, arg2
, 0);
12137 return -TARGET_ENOMEM
;
12139 p
= lock_user_string(arg3
);
12141 ret
= -TARGET_EFAULT
;
12143 ret
= get_errno(mincore(a
, arg2
, p
));
12144 unlock_user(p
, arg3
, ret
);
12146 unlock_user(a
, arg1
, 0);
12150 #ifdef TARGET_NR_arm_fadvise64_64
12151 case TARGET_NR_arm_fadvise64_64
:
12152 /* arm_fadvise64_64 looks like fadvise64_64 but
12153 * with different argument order: fd, advice, offset, len
12154 * rather than the usual fd, offset, len, advice.
12155 * Note that offset and len are both 64-bit so appear as
12156 * pairs of 32-bit registers.
12158 ret
= posix_fadvise(arg1
, target_offset64(arg3
, arg4
),
12159 target_offset64(arg5
, arg6
), arg2
);
12160 return -host_to_target_errno(ret
);
12163 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
12165 #ifdef TARGET_NR_fadvise64_64
12166 case TARGET_NR_fadvise64_64
:
12167 #if defined(TARGET_PPC) || defined(TARGET_XTENSA)
12168 /* 6 args: fd, advice, offset (high, low), len (high, low) */
12176 /* 6 args: fd, offset (high, low), len (high, low), advice */
12177 if (regpairs_aligned(cpu_env
, num
)) {
12178 /* offset is in (3,4), len in (5,6) and advice in 7 */
12186 ret
= posix_fadvise(arg1
, target_offset64(arg2
, arg3
),
12187 target_offset64(arg4
, arg5
), arg6
);
12188 return -host_to_target_errno(ret
);
12191 #ifdef TARGET_NR_fadvise64
12192 case TARGET_NR_fadvise64
:
12193 /* 5 args: fd, offset (high, low), len, advice */
12194 if (regpairs_aligned(cpu_env
, num
)) {
12195 /* offset is in (3,4), len in 5 and advice in 6 */
12201 ret
= posix_fadvise(arg1
, target_offset64(arg2
, arg3
), arg4
, arg5
);
12202 return -host_to_target_errno(ret
);
12205 #else /* not a 32-bit ABI */
12206 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
12207 #ifdef TARGET_NR_fadvise64_64
12208 case TARGET_NR_fadvise64_64
:
12210 #ifdef TARGET_NR_fadvise64
12211 case TARGET_NR_fadvise64
:
12213 #ifdef TARGET_S390X
12215 case 4: arg4
= POSIX_FADV_NOREUSE
+ 1; break; /* make sure it's an invalid value */
12216 case 5: arg4
= POSIX_FADV_NOREUSE
+ 2; break; /* ditto */
12217 case 6: arg4
= POSIX_FADV_DONTNEED
; break;
12218 case 7: arg4
= POSIX_FADV_NOREUSE
; break;
12222 return -host_to_target_errno(posix_fadvise(arg1
, arg2
, arg3
, arg4
));
12224 #endif /* end of 64-bit ABI fadvise handling */
12226 #ifdef TARGET_NR_madvise
12227 case TARGET_NR_madvise
:
12228 return target_madvise(arg1
, arg2
, arg3
);
12230 #ifdef TARGET_NR_fcntl64
12231 case TARGET_NR_fcntl64
:
12235 from_flock64_fn
*copyfrom
= copy_from_user_flock64
;
12236 to_flock64_fn
*copyto
= copy_to_user_flock64
;
12239 if (!cpu_env
->eabi
) {
12240 copyfrom
= copy_from_user_oabi_flock64
;
12241 copyto
= copy_to_user_oabi_flock64
;
12245 cmd
= target_to_host_fcntl_cmd(arg2
);
12246 if (cmd
== -TARGET_EINVAL
) {
12251 case TARGET_F_GETLK64
:
12252 ret
= copyfrom(&fl
, arg3
);
12256 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
12258 ret
= copyto(arg3
, &fl
);
12262 case TARGET_F_SETLK64
:
12263 case TARGET_F_SETLKW64
:
12264 ret
= copyfrom(&fl
, arg3
);
12268 ret
= get_errno(safe_fcntl(arg1
, cmd
, &fl
));
12271 ret
= do_fcntl(arg1
, arg2
, arg3
);
12277 #ifdef TARGET_NR_cacheflush
12278 case TARGET_NR_cacheflush
:
12279 /* self-modifying code is handled automatically, so nothing needed */
12282 #ifdef TARGET_NR_getpagesize
12283 case TARGET_NR_getpagesize
:
12284 return TARGET_PAGE_SIZE
;
12286 case TARGET_NR_gettid
:
12287 return get_errno(sys_gettid());
12288 #ifdef TARGET_NR_readahead
12289 case TARGET_NR_readahead
:
12290 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
12291 if (regpairs_aligned(cpu_env
, num
)) {
12296 ret
= get_errno(readahead(arg1
, target_offset64(arg2
, arg3
) , arg4
));
12298 ret
= get_errno(readahead(arg1
, arg2
, arg3
));
12303 #ifdef TARGET_NR_setxattr
12304 case TARGET_NR_listxattr
:
12305 case TARGET_NR_llistxattr
:
12309 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
12311 return -TARGET_EFAULT
;
12314 p
= lock_user_string(arg1
);
12316 if (num
== TARGET_NR_listxattr
) {
12317 ret
= get_errno(listxattr(p
, b
, arg3
));
12319 ret
= get_errno(llistxattr(p
, b
, arg3
));
12322 ret
= -TARGET_EFAULT
;
12324 unlock_user(p
, arg1
, 0);
12325 unlock_user(b
, arg2
, arg3
);
12328 case TARGET_NR_flistxattr
:
12332 b
= lock_user(VERIFY_WRITE
, arg2
, arg3
, 0);
12334 return -TARGET_EFAULT
;
12337 ret
= get_errno(flistxattr(arg1
, b
, arg3
));
12338 unlock_user(b
, arg2
, arg3
);
12341 case TARGET_NR_setxattr
:
12342 case TARGET_NR_lsetxattr
:
12344 void *p
, *n
, *v
= 0;
12346 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
12348 return -TARGET_EFAULT
;
12351 p
= lock_user_string(arg1
);
12352 n
= lock_user_string(arg2
);
12354 if (num
== TARGET_NR_setxattr
) {
12355 ret
= get_errno(setxattr(p
, n
, v
, arg4
, arg5
));
12357 ret
= get_errno(lsetxattr(p
, n
, v
, arg4
, arg5
));
12360 ret
= -TARGET_EFAULT
;
12362 unlock_user(p
, arg1
, 0);
12363 unlock_user(n
, arg2
, 0);
12364 unlock_user(v
, arg3
, 0);
12367 case TARGET_NR_fsetxattr
:
12371 v
= lock_user(VERIFY_READ
, arg3
, arg4
, 1);
12373 return -TARGET_EFAULT
;
12376 n
= lock_user_string(arg2
);
12378 ret
= get_errno(fsetxattr(arg1
, n
, v
, arg4
, arg5
));
12380 ret
= -TARGET_EFAULT
;
12382 unlock_user(n
, arg2
, 0);
12383 unlock_user(v
, arg3
, 0);
12386 case TARGET_NR_getxattr
:
12387 case TARGET_NR_lgetxattr
:
12389 void *p
, *n
, *v
= 0;
12391 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
12393 return -TARGET_EFAULT
;
12396 p
= lock_user_string(arg1
);
12397 n
= lock_user_string(arg2
);
12399 if (num
== TARGET_NR_getxattr
) {
12400 ret
= get_errno(getxattr(p
, n
, v
, arg4
));
12402 ret
= get_errno(lgetxattr(p
, n
, v
, arg4
));
12405 ret
= -TARGET_EFAULT
;
12407 unlock_user(p
, arg1
, 0);
12408 unlock_user(n
, arg2
, 0);
12409 unlock_user(v
, arg3
, arg4
);
12412 case TARGET_NR_fgetxattr
:
12416 v
= lock_user(VERIFY_WRITE
, arg3
, arg4
, 0);
12418 return -TARGET_EFAULT
;
12421 n
= lock_user_string(arg2
);
12423 ret
= get_errno(fgetxattr(arg1
, n
, v
, arg4
));
12425 ret
= -TARGET_EFAULT
;
12427 unlock_user(n
, arg2
, 0);
12428 unlock_user(v
, arg3
, arg4
);
12431 case TARGET_NR_removexattr
:
12432 case TARGET_NR_lremovexattr
:
12435 p
= lock_user_string(arg1
);
12436 n
= lock_user_string(arg2
);
12438 if (num
== TARGET_NR_removexattr
) {
12439 ret
= get_errno(removexattr(p
, n
));
12441 ret
= get_errno(lremovexattr(p
, n
));
12444 ret
= -TARGET_EFAULT
;
12446 unlock_user(p
, arg1
, 0);
12447 unlock_user(n
, arg2
, 0);
12450 case TARGET_NR_fremovexattr
:
12453 n
= lock_user_string(arg2
);
12455 ret
= get_errno(fremovexattr(arg1
, n
));
12457 ret
= -TARGET_EFAULT
;
12459 unlock_user(n
, arg2
, 0);
12463 #endif /* CONFIG_ATTR */
12464 #ifdef TARGET_NR_set_thread_area
12465 case TARGET_NR_set_thread_area
:
12466 #if defined(TARGET_MIPS)
12467 cpu_env
->active_tc
.CP0_UserLocal
= arg1
;
12469 #elif defined(TARGET_CRIS)
12471 ret
= -TARGET_EINVAL
;
12473 cpu_env
->pregs
[PR_PID
] = arg1
;
12477 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
12478 return do_set_thread_area(cpu_env
, arg1
);
12479 #elif defined(TARGET_M68K)
12481 TaskState
*ts
= cpu
->opaque
;
12482 ts
->tp_value
= arg1
;
12486 return -TARGET_ENOSYS
;
12489 #ifdef TARGET_NR_get_thread_area
12490 case TARGET_NR_get_thread_area
:
12491 #if defined(TARGET_I386) && defined(TARGET_ABI32)
12492 return do_get_thread_area(cpu_env
, arg1
);
12493 #elif defined(TARGET_M68K)
12495 TaskState
*ts
= cpu
->opaque
;
12496 return ts
->tp_value
;
12499 return -TARGET_ENOSYS
;
12502 #ifdef TARGET_NR_getdomainname
12503 case TARGET_NR_getdomainname
:
12504 return -TARGET_ENOSYS
;
12507 #ifdef TARGET_NR_clock_settime
12508 case TARGET_NR_clock_settime
:
12510 struct timespec ts
;
12512 ret
= target_to_host_timespec(&ts
, arg2
);
12513 if (!is_error(ret
)) {
12514 ret
= get_errno(clock_settime(arg1
, &ts
));
12519 #ifdef TARGET_NR_clock_settime64
12520 case TARGET_NR_clock_settime64
:
12522 struct timespec ts
;
12524 ret
= target_to_host_timespec64(&ts
, arg2
);
12525 if (!is_error(ret
)) {
12526 ret
= get_errno(clock_settime(arg1
, &ts
));
12531 #ifdef TARGET_NR_clock_gettime
12532 case TARGET_NR_clock_gettime
:
12534 struct timespec ts
;
12535 ret
= get_errno(clock_gettime(arg1
, &ts
));
12536 if (!is_error(ret
)) {
12537 ret
= host_to_target_timespec(arg2
, &ts
);
12542 #ifdef TARGET_NR_clock_gettime64
12543 case TARGET_NR_clock_gettime64
:
12545 struct timespec ts
;
12546 ret
= get_errno(clock_gettime(arg1
, &ts
));
12547 if (!is_error(ret
)) {
12548 ret
= host_to_target_timespec64(arg2
, &ts
);
12553 #ifdef TARGET_NR_clock_getres
12554 case TARGET_NR_clock_getres
:
12556 struct timespec ts
;
12557 ret
= get_errno(clock_getres(arg1
, &ts
));
12558 if (!is_error(ret
)) {
12559 host_to_target_timespec(arg2
, &ts
);
12564 #ifdef TARGET_NR_clock_getres_time64
12565 case TARGET_NR_clock_getres_time64
:
12567 struct timespec ts
;
12568 ret
= get_errno(clock_getres(arg1
, &ts
));
12569 if (!is_error(ret
)) {
12570 host_to_target_timespec64(arg2
, &ts
);
12575 #ifdef TARGET_NR_clock_nanosleep
12576 case TARGET_NR_clock_nanosleep
:
12578 struct timespec ts
;
12579 if (target_to_host_timespec(&ts
, arg3
)) {
12580 return -TARGET_EFAULT
;
12582 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
12583 &ts
, arg4
? &ts
: NULL
));
12585 * if the call is interrupted by a signal handler, it fails
12586 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
12587 * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
12589 if (ret
== -TARGET_EINTR
&& arg4
&& arg2
!= TIMER_ABSTIME
&&
12590 host_to_target_timespec(arg4
, &ts
)) {
12591 return -TARGET_EFAULT
;
12597 #ifdef TARGET_NR_clock_nanosleep_time64
12598 case TARGET_NR_clock_nanosleep_time64
:
12600 struct timespec ts
;
12602 if (target_to_host_timespec64(&ts
, arg3
)) {
12603 return -TARGET_EFAULT
;
12606 ret
= get_errno(safe_clock_nanosleep(arg1
, arg2
,
12607 &ts
, arg4
? &ts
: NULL
));
12609 if (ret
== -TARGET_EINTR
&& arg4
&& arg2
!= TIMER_ABSTIME
&&
12610 host_to_target_timespec64(arg4
, &ts
)) {
12611 return -TARGET_EFAULT
;
12617 #if defined(TARGET_NR_set_tid_address)
12618 case TARGET_NR_set_tid_address
:
12620 TaskState
*ts
= cpu
->opaque
;
12621 ts
->child_tidptr
= arg1
;
12622 /* do not call host set_tid_address() syscall, instead return tid() */
12623 return get_errno(sys_gettid());
12627 case TARGET_NR_tkill
:
12628 return get_errno(safe_tkill((int)arg1
, target_to_host_signal(arg2
)));
12630 case TARGET_NR_tgkill
:
12631 return get_errno(safe_tgkill((int)arg1
, (int)arg2
,
12632 target_to_host_signal(arg3
)));
12634 #ifdef TARGET_NR_set_robust_list
12635 case TARGET_NR_set_robust_list
:
12636 case TARGET_NR_get_robust_list
:
12637 /* The ABI for supporting robust futexes has userspace pass
12638 * the kernel a pointer to a linked list which is updated by
12639 * userspace after the syscall; the list is walked by the kernel
12640 * when the thread exits. Since the linked list in QEMU guest
12641 * memory isn't a valid linked list for the host and we have
12642 * no way to reliably intercept the thread-death event, we can't
12643 * support these. Silently return ENOSYS so that guest userspace
12644 * falls back to a non-robust futex implementation (which should
12645 * be OK except in the corner case of the guest crashing while
12646 * holding a mutex that is shared with another process via
12649 return -TARGET_ENOSYS
;
12652 #if defined(TARGET_NR_utimensat)
12653 case TARGET_NR_utimensat
:
12655 struct timespec
*tsp
, ts
[2];
12659 if (target_to_host_timespec(ts
, arg3
)) {
12660 return -TARGET_EFAULT
;
12662 if (target_to_host_timespec(ts
+ 1, arg3
+
12663 sizeof(struct target_timespec
))) {
12664 return -TARGET_EFAULT
;
12669 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
12671 if (!(p
= lock_user_string(arg2
))) {
12672 return -TARGET_EFAULT
;
12674 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
12675 unlock_user(p
, arg2
, 0);
12680 #ifdef TARGET_NR_utimensat_time64
12681 case TARGET_NR_utimensat_time64
:
12683 struct timespec
*tsp
, ts
[2];
12687 if (target_to_host_timespec64(ts
, arg3
)) {
12688 return -TARGET_EFAULT
;
12690 if (target_to_host_timespec64(ts
+ 1, arg3
+
12691 sizeof(struct target__kernel_timespec
))) {
12692 return -TARGET_EFAULT
;
12697 ret
= get_errno(sys_utimensat(arg1
, NULL
, tsp
, arg4
));
12699 p
= lock_user_string(arg2
);
12701 return -TARGET_EFAULT
;
12703 ret
= get_errno(sys_utimensat(arg1
, path(p
), tsp
, arg4
));
12704 unlock_user(p
, arg2
, 0);
12709 #ifdef TARGET_NR_futex
12710 case TARGET_NR_futex
:
12711 return do_futex(cpu
, false, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
12713 #ifdef TARGET_NR_futex_time64
12714 case TARGET_NR_futex_time64
:
12715 return do_futex(cpu
, true, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
12717 #ifdef CONFIG_INOTIFY
12718 #if defined(TARGET_NR_inotify_init)
12719 case TARGET_NR_inotify_init
:
12720 ret
= get_errno(inotify_init());
12722 fd_trans_register(ret
, &target_inotify_trans
);
12726 #if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
12727 case TARGET_NR_inotify_init1
:
12728 ret
= get_errno(inotify_init1(target_to_host_bitmask(arg1
,
12729 fcntl_flags_tbl
)));
12731 fd_trans_register(ret
, &target_inotify_trans
);
12735 #if defined(TARGET_NR_inotify_add_watch)
12736 case TARGET_NR_inotify_add_watch
:
12737 p
= lock_user_string(arg2
);
12738 ret
= get_errno(inotify_add_watch(arg1
, path(p
), arg3
));
12739 unlock_user(p
, arg2
, 0);
12742 #if defined(TARGET_NR_inotify_rm_watch)
12743 case TARGET_NR_inotify_rm_watch
:
12744 return get_errno(inotify_rm_watch(arg1
, arg2
));
12748 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
12749 case TARGET_NR_mq_open
:
12751 struct mq_attr posix_mq_attr
;
12752 struct mq_attr
*pposix_mq_attr
;
12755 host_flags
= target_to_host_bitmask(arg2
, fcntl_flags_tbl
);
12756 pposix_mq_attr
= NULL
;
12758 if (copy_from_user_mq_attr(&posix_mq_attr
, arg4
) != 0) {
12759 return -TARGET_EFAULT
;
12761 pposix_mq_attr
= &posix_mq_attr
;
12763 p
= lock_user_string(arg1
- 1);
12765 return -TARGET_EFAULT
;
12767 ret
= get_errno(mq_open(p
, host_flags
, arg3
, pposix_mq_attr
));
12768 unlock_user (p
, arg1
, 0);
12772 case TARGET_NR_mq_unlink
:
12773 p
= lock_user_string(arg1
- 1);
12775 return -TARGET_EFAULT
;
12777 ret
= get_errno(mq_unlink(p
));
12778 unlock_user (p
, arg1
, 0);
12781 #ifdef TARGET_NR_mq_timedsend
12782 case TARGET_NR_mq_timedsend
:
12784 struct timespec ts
;
12786 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12788 if (target_to_host_timespec(&ts
, arg5
)) {
12789 return -TARGET_EFAULT
;
12791 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
12792 if (!is_error(ret
) && host_to_target_timespec(arg5
, &ts
)) {
12793 return -TARGET_EFAULT
;
12796 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
12798 unlock_user (p
, arg2
, arg3
);
12802 #ifdef TARGET_NR_mq_timedsend_time64
12803 case TARGET_NR_mq_timedsend_time64
:
12805 struct timespec ts
;
12807 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
12809 if (target_to_host_timespec64(&ts
, arg5
)) {
12810 return -TARGET_EFAULT
;
12812 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, &ts
));
12813 if (!is_error(ret
) && host_to_target_timespec64(arg5
, &ts
)) {
12814 return -TARGET_EFAULT
;
12817 ret
= get_errno(safe_mq_timedsend(arg1
, p
, arg3
, arg4
, NULL
));
12819 unlock_user(p
, arg2
, arg3
);
12824 #ifdef TARGET_NR_mq_timedreceive
12825 case TARGET_NR_mq_timedreceive
:
12827 struct timespec ts
;
12830 p
= lock_user (VERIFY_READ
, arg2
, arg3
, 1);
12832 if (target_to_host_timespec(&ts
, arg5
)) {
12833 return -TARGET_EFAULT
;
12835 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12837 if (!is_error(ret
) && host_to_target_timespec(arg5
, &ts
)) {
12838 return -TARGET_EFAULT
;
12841 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12844 unlock_user (p
, arg2
, arg3
);
12846 put_user_u32(prio
, arg4
);
12850 #ifdef TARGET_NR_mq_timedreceive_time64
12851 case TARGET_NR_mq_timedreceive_time64
:
12853 struct timespec ts
;
12856 p
= lock_user(VERIFY_READ
, arg2
, arg3
, 1);
12858 if (target_to_host_timespec64(&ts
, arg5
)) {
12859 return -TARGET_EFAULT
;
12861 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12863 if (!is_error(ret
) && host_to_target_timespec64(arg5
, &ts
)) {
12864 return -TARGET_EFAULT
;
12867 ret
= get_errno(safe_mq_timedreceive(arg1
, p
, arg3
,
12870 unlock_user(p
, arg2
, arg3
);
12872 put_user_u32(prio
, arg4
);
12878 /* Not implemented for now... */
12879 /* case TARGET_NR_mq_notify: */
12882 case TARGET_NR_mq_getsetattr
:
12884 struct mq_attr posix_mq_attr_in
, posix_mq_attr_out
;
12887 copy_from_user_mq_attr(&posix_mq_attr_in
, arg2
);
12888 ret
= get_errno(mq_setattr(arg1
, &posix_mq_attr_in
,
12889 &posix_mq_attr_out
));
12890 } else if (arg3
!= 0) {
12891 ret
= get_errno(mq_getattr(arg1
, &posix_mq_attr_out
));
12893 if (ret
== 0 && arg3
!= 0) {
12894 copy_to_user_mq_attr(arg3
, &posix_mq_attr_out
);
12900 #ifdef CONFIG_SPLICE
12901 #ifdef TARGET_NR_tee
12902 case TARGET_NR_tee
:
12904 ret
= get_errno(tee(arg1
,arg2
,arg3
,arg4
));
12908 #ifdef TARGET_NR_splice
12909 case TARGET_NR_splice
:
12911 loff_t loff_in
, loff_out
;
12912 loff_t
*ploff_in
= NULL
, *ploff_out
= NULL
;
12914 if (get_user_u64(loff_in
, arg2
)) {
12915 return -TARGET_EFAULT
;
12917 ploff_in
= &loff_in
;
12920 if (get_user_u64(loff_out
, arg4
)) {
12921 return -TARGET_EFAULT
;
12923 ploff_out
= &loff_out
;
12925 ret
= get_errno(splice(arg1
, ploff_in
, arg3
, ploff_out
, arg5
, arg6
));
12927 if (put_user_u64(loff_in
, arg2
)) {
12928 return -TARGET_EFAULT
;
12932 if (put_user_u64(loff_out
, arg4
)) {
12933 return -TARGET_EFAULT
;
12939 #ifdef TARGET_NR_vmsplice
12940 case TARGET_NR_vmsplice
:
12942 struct iovec
*vec
= lock_iovec(VERIFY_READ
, arg2
, arg3
, 1);
12944 ret
= get_errno(vmsplice(arg1
, vec
, arg3
, arg4
));
12945 unlock_iovec(vec
, arg2
, arg3
, 0);
12947 ret
= -host_to_target_errno(errno
);
12952 #endif /* CONFIG_SPLICE */
12953 #ifdef CONFIG_EVENTFD
12954 #if defined(TARGET_NR_eventfd)
12955 case TARGET_NR_eventfd
:
12956 ret
= get_errno(eventfd(arg1
, 0));
12958 fd_trans_register(ret
, &target_eventfd_trans
);
12962 #if defined(TARGET_NR_eventfd2)
12963 case TARGET_NR_eventfd2
:
12965 int host_flags
= arg2
& (~(TARGET_O_NONBLOCK_MASK
| TARGET_O_CLOEXEC
));
12966 if (arg2
& TARGET_O_NONBLOCK
) {
12967 host_flags
|= O_NONBLOCK
;
12969 if (arg2
& TARGET_O_CLOEXEC
) {
12970 host_flags
|= O_CLOEXEC
;
12972 ret
= get_errno(eventfd(arg1
, host_flags
));
12974 fd_trans_register(ret
, &target_eventfd_trans
);
12979 #endif /* CONFIG_EVENTFD */
12980 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
12981 case TARGET_NR_fallocate
:
12982 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
12983 ret
= get_errno(fallocate(arg1
, arg2
, target_offset64(arg3
, arg4
),
12984 target_offset64(arg5
, arg6
)));
12986 ret
= get_errno(fallocate(arg1
, arg2
, arg3
, arg4
));
12990 #if defined(CONFIG_SYNC_FILE_RANGE)
12991 #if defined(TARGET_NR_sync_file_range)
12992 case TARGET_NR_sync_file_range
:
12993 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
12994 #if defined(TARGET_MIPS)
12995 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
12996 target_offset64(arg5
, arg6
), arg7
));
12998 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg2
, arg3
),
12999 target_offset64(arg4
, arg5
), arg6
));
13000 #endif /* !TARGET_MIPS */
13002 ret
= get_errno(sync_file_range(arg1
, arg2
, arg3
, arg4
));
13006 #if defined(TARGET_NR_sync_file_range2) || \
13007 defined(TARGET_NR_arm_sync_file_range)
13008 #if defined(TARGET_NR_sync_file_range2)
13009 case TARGET_NR_sync_file_range2
:
13011 #if defined(TARGET_NR_arm_sync_file_range)
13012 case TARGET_NR_arm_sync_file_range
:
13014 /* This is like sync_file_range but the arguments are reordered */
13015 #if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
13016 ret
= get_errno(sync_file_range(arg1
, target_offset64(arg3
, arg4
),
13017 target_offset64(arg5
, arg6
), arg2
));
13019 ret
= get_errno(sync_file_range(arg1
, arg3
, arg4
, arg2
));
13024 #if defined(TARGET_NR_signalfd4)
13025 case TARGET_NR_signalfd4
:
13026 return do_signalfd4(arg1
, arg2
, arg4
);
13028 #if defined(TARGET_NR_signalfd)
13029 case TARGET_NR_signalfd
:
13030 return do_signalfd4(arg1
, arg2
, 0);
13032 #if defined(CONFIG_EPOLL)
13033 #if defined(TARGET_NR_epoll_create)
13034 case TARGET_NR_epoll_create
:
13035 return get_errno(epoll_create(arg1
));
13037 #if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
13038 case TARGET_NR_epoll_create1
:
13039 return get_errno(epoll_create1(target_to_host_bitmask(arg1
, fcntl_flags_tbl
)));
13041 #if defined(TARGET_NR_epoll_ctl)
13042 case TARGET_NR_epoll_ctl
:
13044 struct epoll_event ep
;
13045 struct epoll_event
*epp
= 0;
13047 if (arg2
!= EPOLL_CTL_DEL
) {
13048 struct target_epoll_event
*target_ep
;
13049 if (!lock_user_struct(VERIFY_READ
, target_ep
, arg4
, 1)) {
13050 return -TARGET_EFAULT
;
13052 ep
.events
= tswap32(target_ep
->events
);
13054 * The epoll_data_t union is just opaque data to the kernel,
13055 * so we transfer all 64 bits across and need not worry what
13056 * actual data type it is.
13058 ep
.data
.u64
= tswap64(target_ep
->data
.u64
);
13059 unlock_user_struct(target_ep
, arg4
, 0);
13062 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
13063 * non-null pointer, even though this argument is ignored.
13068 return get_errno(epoll_ctl(arg1
, arg2
, arg3
, epp
));
13072 #if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
13073 #if defined(TARGET_NR_epoll_wait)
13074 case TARGET_NR_epoll_wait
:
13076 #if defined(TARGET_NR_epoll_pwait)
13077 case TARGET_NR_epoll_pwait
:
13080 struct target_epoll_event
*target_ep
;
13081 struct epoll_event
*ep
;
13083 int maxevents
= arg3
;
13084 int timeout
= arg4
;
13086 if (maxevents
<= 0 || maxevents
> TARGET_EP_MAX_EVENTS
) {
13087 return -TARGET_EINVAL
;
13090 target_ep
= lock_user(VERIFY_WRITE
, arg2
,
13091 maxevents
* sizeof(struct target_epoll_event
), 1);
13093 return -TARGET_EFAULT
;
13096 ep
= g_try_new(struct epoll_event
, maxevents
);
13098 unlock_user(target_ep
, arg2
, 0);
13099 return -TARGET_ENOMEM
;
13103 #if defined(TARGET_NR_epoll_pwait)
13104 case TARGET_NR_epoll_pwait
:
13106 sigset_t
*set
= NULL
;
13109 ret
= process_sigsuspend_mask(&set
, arg5
, arg6
);
13115 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
13116 set
, SIGSET_T_SIZE
));
13119 finish_sigsuspend_mask(ret
);
13124 #if defined(TARGET_NR_epoll_wait)
13125 case TARGET_NR_epoll_wait
:
13126 ret
= get_errno(safe_epoll_pwait(epfd
, ep
, maxevents
, timeout
,
13131 ret
= -TARGET_ENOSYS
;
13133 if (!is_error(ret
)) {
13135 for (i
= 0; i
< ret
; i
++) {
13136 target_ep
[i
].events
= tswap32(ep
[i
].events
);
13137 target_ep
[i
].data
.u64
= tswap64(ep
[i
].data
.u64
);
13139 unlock_user(target_ep
, arg2
,
13140 ret
* sizeof(struct target_epoll_event
));
13142 unlock_user(target_ep
, arg2
, 0);
13149 #ifdef TARGET_NR_prlimit64
13150 case TARGET_NR_prlimit64
:
13152 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
13153 struct target_rlimit64
*target_rnew
, *target_rold
;
13154 struct host_rlimit64 rnew
, rold
, *rnewp
= 0;
13155 int resource
= target_to_host_resource(arg2
);
13157 if (arg3
&& (resource
!= RLIMIT_AS
&&
13158 resource
!= RLIMIT_DATA
&&
13159 resource
!= RLIMIT_STACK
)) {
13160 if (!lock_user_struct(VERIFY_READ
, target_rnew
, arg3
, 1)) {
13161 return -TARGET_EFAULT
;
13163 __get_user(rnew
.rlim_cur
, &target_rnew
->rlim_cur
);
13164 __get_user(rnew
.rlim_max
, &target_rnew
->rlim_max
);
13165 unlock_user_struct(target_rnew
, arg3
, 0);
13169 ret
= get_errno(sys_prlimit64(arg1
, resource
, rnewp
, arg4
? &rold
: 0));
13170 if (!is_error(ret
) && arg4
) {
13171 if (!lock_user_struct(VERIFY_WRITE
, target_rold
, arg4
, 1)) {
13172 return -TARGET_EFAULT
;
13174 __put_user(rold
.rlim_cur
, &target_rold
->rlim_cur
);
13175 __put_user(rold
.rlim_max
, &target_rold
->rlim_max
);
13176 unlock_user_struct(target_rold
, arg4
, 1);
13181 #ifdef TARGET_NR_gethostname
13182 case TARGET_NR_gethostname
:
13184 char *name
= lock_user(VERIFY_WRITE
, arg1
, arg2
, 0);
13186 ret
= get_errno(gethostname(name
, arg2
));
13187 unlock_user(name
, arg1
, arg2
);
13189 ret
= -TARGET_EFAULT
;
13194 #ifdef TARGET_NR_atomic_cmpxchg_32
13195 case TARGET_NR_atomic_cmpxchg_32
:
13197 /* should use start_exclusive from main.c */
13198 abi_ulong mem_value
;
13199 if (get_user_u32(mem_value
, arg6
)) {
13200 target_siginfo_t info
;
13201 info
.si_signo
= SIGSEGV
;
13203 info
.si_code
= TARGET_SEGV_MAPERR
;
13204 info
._sifields
._sigfault
._addr
= arg6
;
13205 queue_signal(cpu_env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
13209 if (mem_value
== arg2
)
13210 put_user_u32(arg1
, arg6
);
13214 #ifdef TARGET_NR_atomic_barrier
13215 case TARGET_NR_atomic_barrier
:
13216 /* Like the kernel implementation and the
13217 qemu arm barrier, no-op this? */
13221 #ifdef TARGET_NR_timer_create
13222 case TARGET_NR_timer_create
:
13224 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
13226 struct sigevent host_sevp
= { {0}, }, *phost_sevp
= NULL
;
13229 int timer_index
= next_free_host_timer();
13231 if (timer_index
< 0) {
13232 ret
= -TARGET_EAGAIN
;
13234 timer_t
*phtimer
= g_posix_timers
+ timer_index
;
13237 phost_sevp
= &host_sevp
;
13238 ret
= target_to_host_sigevent(phost_sevp
, arg2
);
13240 free_host_timer_slot(timer_index
);
13245 ret
= get_errno(timer_create(clkid
, phost_sevp
, phtimer
));
13247 free_host_timer_slot(timer_index
);
13249 if (put_user(TIMER_MAGIC
| timer_index
, arg3
, target_timer_t
)) {
13250 timer_delete(*phtimer
);
13251 free_host_timer_slot(timer_index
);
13252 return -TARGET_EFAULT
;
13260 #ifdef TARGET_NR_timer_settime
13261 case TARGET_NR_timer_settime
:
13263 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
13264 * struct itimerspec * old_value */
13265 target_timer_t timerid
= get_timer_id(arg1
);
13269 } else if (arg3
== 0) {
13270 ret
= -TARGET_EINVAL
;
13272 timer_t htimer
= g_posix_timers
[timerid
];
13273 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
13275 if (target_to_host_itimerspec(&hspec_new
, arg3
)) {
13276 return -TARGET_EFAULT
;
13279 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
13280 if (arg4
&& host_to_target_itimerspec(arg4
, &hspec_old
)) {
13281 return -TARGET_EFAULT
;
13288 #ifdef TARGET_NR_timer_settime64
13289 case TARGET_NR_timer_settime64
:
13291 target_timer_t timerid
= get_timer_id(arg1
);
13295 } else if (arg3
== 0) {
13296 ret
= -TARGET_EINVAL
;
13298 timer_t htimer
= g_posix_timers
[timerid
];
13299 struct itimerspec hspec_new
= {{0},}, hspec_old
= {{0},};
13301 if (target_to_host_itimerspec64(&hspec_new
, arg3
)) {
13302 return -TARGET_EFAULT
;
13305 timer_settime(htimer
, arg2
, &hspec_new
, &hspec_old
));
13306 if (arg4
&& host_to_target_itimerspec64(arg4
, &hspec_old
)) {
13307 return -TARGET_EFAULT
;
13314 #ifdef TARGET_NR_timer_gettime
13315 case TARGET_NR_timer_gettime
:
13317 /* args: timer_t timerid, struct itimerspec *curr_value */
13318 target_timer_t timerid
= get_timer_id(arg1
);
13322 } else if (!arg2
) {
13323 ret
= -TARGET_EFAULT
;
13325 timer_t htimer
= g_posix_timers
[timerid
];
13326 struct itimerspec hspec
;
13327 ret
= get_errno(timer_gettime(htimer
, &hspec
));
13329 if (host_to_target_itimerspec(arg2
, &hspec
)) {
13330 ret
= -TARGET_EFAULT
;
13337 #ifdef TARGET_NR_timer_gettime64
13338 case TARGET_NR_timer_gettime64
:
13340 /* args: timer_t timerid, struct itimerspec64 *curr_value */
13341 target_timer_t timerid
= get_timer_id(arg1
);
13345 } else if (!arg2
) {
13346 ret
= -TARGET_EFAULT
;
13348 timer_t htimer
= g_posix_timers
[timerid
];
13349 struct itimerspec hspec
;
13350 ret
= get_errno(timer_gettime(htimer
, &hspec
));
13352 if (host_to_target_itimerspec64(arg2
, &hspec
)) {
13353 ret
= -TARGET_EFAULT
;
13360 #ifdef TARGET_NR_timer_getoverrun
13361 case TARGET_NR_timer_getoverrun
:
13363 /* args: timer_t timerid */
13364 target_timer_t timerid
= get_timer_id(arg1
);
13369 timer_t htimer
= g_posix_timers
[timerid
];
13370 ret
= get_errno(timer_getoverrun(htimer
));
13376 #ifdef TARGET_NR_timer_delete
13377 case TARGET_NR_timer_delete
:
13379 /* args: timer_t timerid */
13380 target_timer_t timerid
= get_timer_id(arg1
);
13385 timer_t htimer
= g_posix_timers
[timerid
];
13386 ret
= get_errno(timer_delete(htimer
));
13387 free_host_timer_slot(timerid
);
13393 #if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
13394 case TARGET_NR_timerfd_create
:
13395 ret
= get_errno(timerfd_create(arg1
,
13396 target_to_host_bitmask(arg2
, fcntl_flags_tbl
)));
13398 fd_trans_register(ret
, &target_timerfd_trans
);
13403 #if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
13404 case TARGET_NR_timerfd_gettime
:
13406 struct itimerspec its_curr
;
13408 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
13410 if (arg2
&& host_to_target_itimerspec(arg2
, &its_curr
)) {
13411 return -TARGET_EFAULT
;
13417 #if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
13418 case TARGET_NR_timerfd_gettime64
:
13420 struct itimerspec its_curr
;
13422 ret
= get_errno(timerfd_gettime(arg1
, &its_curr
));
13424 if (arg2
&& host_to_target_itimerspec64(arg2
, &its_curr
)) {
13425 return -TARGET_EFAULT
;
13431 #if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
13432 case TARGET_NR_timerfd_settime
:
13434 struct itimerspec its_new
, its_old
, *p_new
;
13437 if (target_to_host_itimerspec(&its_new
, arg3
)) {
13438 return -TARGET_EFAULT
;
13445 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
13447 if (arg4
&& host_to_target_itimerspec(arg4
, &its_old
)) {
13448 return -TARGET_EFAULT
;
13454 #if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
13455 case TARGET_NR_timerfd_settime64
:
13457 struct itimerspec its_new
, its_old
, *p_new
;
13460 if (target_to_host_itimerspec64(&its_new
, arg3
)) {
13461 return -TARGET_EFAULT
;
13468 ret
= get_errno(timerfd_settime(arg1
, arg2
, p_new
, &its_old
));
13470 if (arg4
&& host_to_target_itimerspec64(arg4
, &its_old
)) {
13471 return -TARGET_EFAULT
;
13477 #if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
13478 case TARGET_NR_ioprio_get
:
13479 return get_errno(ioprio_get(arg1
, arg2
));
13482 #if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
13483 case TARGET_NR_ioprio_set
:
13484 return get_errno(ioprio_set(arg1
, arg2
, arg3
));
13487 #if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
13488 case TARGET_NR_setns
:
13489 return get_errno(setns(arg1
, arg2
));
13491 #if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
13492 case TARGET_NR_unshare
:
13493 return get_errno(unshare(arg1
));
13495 #if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
13496 case TARGET_NR_kcmp
:
13497 return get_errno(kcmp(arg1
, arg2
, arg3
, arg4
, arg5
));
13499 #ifdef TARGET_NR_swapcontext
13500 case TARGET_NR_swapcontext
:
13501 /* PowerPC specific. */
13502 return do_swapcontext(cpu_env
, arg1
, arg2
, arg3
);
13504 #ifdef TARGET_NR_memfd_create
13505 case TARGET_NR_memfd_create
:
13506 p
= lock_user_string(arg1
);
13508 return -TARGET_EFAULT
;
13510 ret
= get_errno(memfd_create(p
, arg2
));
13511 fd_trans_unregister(ret
);
13512 unlock_user(p
, arg1
, 0);
13515 #if defined TARGET_NR_membarrier && defined __NR_membarrier
13516 case TARGET_NR_membarrier
:
13517 return get_errno(membarrier(arg1
, arg2
));
13520 #if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
13521 case TARGET_NR_copy_file_range
:
13523 loff_t inoff
, outoff
;
13524 loff_t
*pinoff
= NULL
, *poutoff
= NULL
;
13527 if (get_user_u64(inoff
, arg2
)) {
13528 return -TARGET_EFAULT
;
13533 if (get_user_u64(outoff
, arg4
)) {
13534 return -TARGET_EFAULT
;
13538 /* Do not sign-extend the count parameter. */
13539 ret
= get_errno(safe_copy_file_range(arg1
, pinoff
, arg3
, poutoff
,
13540 (abi_ulong
)arg5
, arg6
));
13541 if (!is_error(ret
) && ret
> 0) {
13543 if (put_user_u64(inoff
, arg2
)) {
13544 return -TARGET_EFAULT
;
13548 if (put_user_u64(outoff
, arg4
)) {
13549 return -TARGET_EFAULT
;
13557 #if defined(TARGET_NR_pivot_root)
13558 case TARGET_NR_pivot_root
:
13561 p
= lock_user_string(arg1
); /* new_root */
13562 p2
= lock_user_string(arg2
); /* put_old */
13564 ret
= -TARGET_EFAULT
;
13566 ret
= get_errno(pivot_root(p
, p2
));
13568 unlock_user(p2
, arg2
, 0);
13569 unlock_user(p
, arg1
, 0);
13575 qemu_log_mask(LOG_UNIMP
, "Unsupported syscall: %d\n", num
);
13576 return -TARGET_ENOSYS
;
13581 abi_long
do_syscall(CPUArchState
*cpu_env
, int num
, abi_long arg1
,
13582 abi_long arg2
, abi_long arg3
, abi_long arg4
,
13583 abi_long arg5
, abi_long arg6
, abi_long arg7
,
13586 CPUState
*cpu
= env_cpu(cpu_env
);
13589 #ifdef DEBUG_ERESTARTSYS
13590 /* Debug-only code for exercising the syscall-restart code paths
13591 * in the per-architecture cpu main loops: restart every syscall
13592 * the guest makes once before letting it through.
13598 return -QEMU_ERESTARTSYS
;
13603 record_syscall_start(cpu
, num
, arg1
,
13604 arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
);
13606 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
13607 print_syscall(cpu_env
, num
, arg1
, arg2
, arg3
, arg4
, arg5
, arg6
);
13610 ret
= do_syscall1(cpu_env
, num
, arg1
, arg2
, arg3
, arg4
,
13611 arg5
, arg6
, arg7
, arg8
);
13613 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
13614 print_syscall_ret(cpu_env
, num
, ret
, arg1
, arg2
,
13615 arg3
, arg4
, arg5
, arg6
);
13618 record_syscall_return(cpu
, num
, ret
);